2021-07-23 12:33:55 +03:00
|
|
|
module orm
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
pub const (
|
2022-12-07 13:26:41 +03:00
|
|
|
num64 = [typeof[i64]().idx, typeof[u64]().idx]
|
2022-12-06 16:44:25 +03:00
|
|
|
nums = [
|
2022-12-07 13:26:41 +03:00
|
|
|
typeof[i8]().idx,
|
|
|
|
typeof[i16]().idx,
|
|
|
|
typeof[int]().idx,
|
|
|
|
typeof[u8]().idx,
|
|
|
|
typeof[u16]().idx,
|
|
|
|
typeof[u32]().idx,
|
|
|
|
typeof[bool]().idx,
|
2021-09-08 05:53:39 +03:00
|
|
|
]
|
2022-12-06 16:44:25 +03:00
|
|
|
float = [
|
2022-12-07 13:26:41 +03:00
|
|
|
typeof[f32]().idx,
|
|
|
|
typeof[f64]().idx,
|
2021-09-08 05:53:39 +03:00
|
|
|
]
|
2022-12-07 13:26:41 +03:00
|
|
|
type_string = typeof[string]().idx
|
2022-12-06 16:44:25 +03:00
|
|
|
time = -2
|
|
|
|
serial = -1
|
|
|
|
type_idx = {
|
2022-12-07 13:26:41 +03:00
|
|
|
'i8': typeof[i8]().idx
|
|
|
|
'i16': typeof[i16]().idx
|
|
|
|
'int': typeof[int]().idx
|
|
|
|
'i64': typeof[i64]().idx
|
|
|
|
'u8': typeof[u8]().idx
|
|
|
|
'u16': typeof[u16]().idx
|
|
|
|
'u32': typeof[u32]().idx
|
|
|
|
'u64': typeof[u64]().idx
|
|
|
|
'f32': typeof[f32]().idx
|
|
|
|
'f64': typeof[f64]().idx
|
|
|
|
'bool': typeof[bool]().idx
|
|
|
|
'string': typeof[string]().idx
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
string_max_len = 2048
|
|
|
|
)
|
|
|
|
|
2021-09-10 20:56:55 +03:00
|
|
|
pub type Primitive = InfixType
|
|
|
|
| bool
|
|
|
|
| f32
|
|
|
|
| f64
|
|
|
|
| i16
|
|
|
|
| i64
|
|
|
|
| i8
|
|
|
|
| int
|
|
|
|
| string
|
|
|
|
| time.Time
|
|
|
|
| u16
|
|
|
|
| u32
|
|
|
|
| u64
|
2022-04-15 15:55:39 +03:00
|
|
|
| u8
|
2021-07-23 12:33:55 +03:00
|
|
|
|
|
|
|
pub enum OperationKind {
|
|
|
|
neq // !=
|
|
|
|
eq // ==
|
|
|
|
gt // >
|
|
|
|
lt // <
|
|
|
|
ge // >=
|
|
|
|
le // <=
|
2023-04-23 03:40:54 +03:00
|
|
|
orm_like // LIKE
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum MathOperationKind {
|
|
|
|
add // +
|
|
|
|
sub // -
|
|
|
|
mul // *
|
|
|
|
div // /
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum StmtKind {
|
|
|
|
insert
|
|
|
|
update
|
|
|
|
delete
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum OrderType {
|
|
|
|
asc
|
|
|
|
desc
|
|
|
|
}
|
|
|
|
|
2023-02-16 12:34:16 +03:00
|
|
|
pub enum SQLDialect {
|
|
|
|
default
|
|
|
|
sqlite
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:33:55 +03:00
|
|
|
fn (kind OperationKind) to_str() string {
|
|
|
|
str := match kind {
|
|
|
|
.neq { '!=' }
|
|
|
|
.eq { '=' }
|
|
|
|
.gt { '>' }
|
|
|
|
.lt { '<' }
|
|
|
|
.ge { '>=' }
|
|
|
|
.le { '<=' }
|
2023-04-23 03:40:54 +03:00
|
|
|
.orm_like { 'LIKE' }
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (kind OrderType) to_str() string {
|
|
|
|
return match kind {
|
|
|
|
.desc {
|
|
|
|
'DESC'
|
|
|
|
}
|
|
|
|
.asc {
|
|
|
|
'ASC'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +03:00
|
|
|
// Examples for QueryData in SQL: abc == 3 && b == 'test'
|
|
|
|
// => fields[abc, b]; data[3, 'test']; types[index of int, index of string]; kinds[.eq, .eq]; is_and[true];
|
|
|
|
// Every field, data, type & kind of operation in the expr share the same index in the arrays
|
|
|
|
// is_and defines how they're addicted to each other either and or or
|
2022-09-09 21:08:48 +03:00
|
|
|
// parentheses defines which fields will be inside ()
|
2021-07-23 12:33:55 +03:00
|
|
|
pub struct QueryData {
|
|
|
|
pub:
|
2022-09-09 21:08:48 +03:00
|
|
|
fields []string
|
|
|
|
data []Primitive
|
|
|
|
types []int
|
|
|
|
parentheses [][]int
|
|
|
|
kinds []OperationKind
|
|
|
|
is_and []bool
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct InfixType {
|
|
|
|
pub:
|
|
|
|
name string
|
|
|
|
operator MathOperationKind
|
|
|
|
right Primitive
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TableField {
|
|
|
|
pub:
|
|
|
|
name string
|
|
|
|
typ int
|
|
|
|
is_time bool
|
|
|
|
default_val string
|
|
|
|
is_arr bool
|
|
|
|
attrs []StructAttribute
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +03:00
|
|
|
// table - Table name
|
|
|
|
// is_count - Either the data will be returned or an integer with the count
|
|
|
|
// has_where - Select all or use a where expr
|
|
|
|
// has_order - Order the results
|
|
|
|
// order - Name of the column which will be ordered
|
|
|
|
// order_type - Type of order (asc, desc)
|
|
|
|
// has_limit - Limits the output data
|
|
|
|
// primary - Name of the primary field
|
|
|
|
// has_offset - Add an offset to the result
|
|
|
|
// fields - Fields to select
|
|
|
|
// types - Types to select
|
2021-07-23 12:33:55 +03:00
|
|
|
pub struct SelectConfig {
|
|
|
|
pub:
|
|
|
|
table string
|
|
|
|
is_count bool
|
|
|
|
has_where bool
|
|
|
|
has_order bool
|
|
|
|
order string
|
|
|
|
order_type OrderType
|
|
|
|
has_limit bool
|
|
|
|
primary string = 'id' // should be set if primary is different than 'id' and 'has_limit' is false
|
|
|
|
has_offset bool
|
|
|
|
fields []string
|
|
|
|
types []int
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +03:00
|
|
|
// Interfaces gets called from the backend and can be implemented
|
|
|
|
// Since the orm supports arrays aswell, they have to be returned too.
|
|
|
|
// A row is represented as []Primitive, where the data is connected to the fields of the struct by their
|
|
|
|
// index. The indices are mapped with the SelectConfig.field array. This is the mapping for a struct.
|
|
|
|
// To have an array, there has to be an array of structs, basically [][]Primitive
|
|
|
|
//
|
|
|
|
// Every function without last_id() returns an optional, which returns an error if present
|
|
|
|
// last_id returns the last inserted id of the db
|
2021-07-23 12:33:55 +03:00
|
|
|
pub interface Connection {
|
2022-10-26 11:26:28 +03:00
|
|
|
@select(config SelectConfig, data QueryData, where QueryData) ![][]Primitive
|
|
|
|
insert(table string, data QueryData) !
|
|
|
|
update(table string, data QueryData, where QueryData) !
|
|
|
|
delete(table string, where QueryData) !
|
|
|
|
create(table string, fields []TableField) !
|
|
|
|
drop(table string) !
|
2023-01-29 17:00:23 +03:00
|
|
|
last_id() int
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +03:00
|
|
|
// Generates an sql stmt, from universal parameter
|
|
|
|
// q - The quotes character, which can be different in every type, so it's variable
|
|
|
|
// num - Stmt uses nums at prepared statements (? or ?1)
|
2023-02-16 12:34:16 +03:00
|
|
|
// qm - Character for prepared statement, qm because of quotation mark like in sqlite
|
2022-05-26 22:53:09 +03:00
|
|
|
// start_pos - When num is true, it's the start position of the counter
|
2023-02-16 12:34:16 +03:00
|
|
|
pub fn orm_stmt_gen(sql_dialect SQLDialect, table string, q string, kind StmtKind, num bool, qm string, start_pos int, data QueryData, where QueryData) (string, QueryData) {
|
2021-07-23 12:33:55 +03:00
|
|
|
mut str := ''
|
|
|
|
mut c := start_pos
|
2022-07-27 00:59:32 +03:00
|
|
|
mut data_fields := []string{}
|
|
|
|
mut data_data := []Primitive{}
|
2021-07-23 12:33:55 +03:00
|
|
|
|
|
|
|
match kind {
|
|
|
|
.insert {
|
2021-08-10 23:31:09 +03:00
|
|
|
mut values := []string{}
|
2022-07-27 00:59:32 +03:00
|
|
|
mut select_fields := []string{}
|
|
|
|
|
|
|
|
for i in 0 .. data.fields.len {
|
|
|
|
if data.data.len > 0 {
|
|
|
|
match data.data[i].type_name() {
|
|
|
|
'string' {
|
|
|
|
if (data.data[i] as string).len == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'time.Time' {
|
|
|
|
if (data.data[i] as time.Time).unix == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
data_data << data.data[i]
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
select_fields << '${q}${data.fields[i]}${q}'
|
2022-07-27 00:59:32 +03:00
|
|
|
values << factory_insert_qm_value(num, qm, c)
|
|
|
|
data_fields << data.fields[i]
|
|
|
|
c++
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
2021-08-10 23:31:09 +03:00
|
|
|
|
2023-02-16 12:34:16 +03:00
|
|
|
str += 'INSERT INTO ${q}${table}${q} '
|
|
|
|
|
|
|
|
are_values_empty := values.len == 0
|
|
|
|
|
|
|
|
if sql_dialect == .sqlite && are_values_empty {
|
|
|
|
str += 'DEFAULT VALUES'
|
|
|
|
} else {
|
|
|
|
str += '('
|
|
|
|
str += select_fields.join(', ')
|
|
|
|
str += ') VALUES ('
|
|
|
|
str += values.join(', ')
|
|
|
|
str += ')'
|
|
|
|
}
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
.update {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += 'UPDATE ${q}${table}${q} SET '
|
2021-07-23 12:33:55 +03:00
|
|
|
for i, field in data.fields {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${q}${field}${q} = '
|
2021-07-23 12:33:55 +03:00
|
|
|
if data.data.len > i {
|
|
|
|
d := data.data[i]
|
|
|
|
if d is InfixType {
|
|
|
|
op := match d.operator {
|
|
|
|
.add {
|
|
|
|
'+'
|
|
|
|
}
|
|
|
|
.sub {
|
|
|
|
'-'
|
|
|
|
}
|
|
|
|
.mul {
|
|
|
|
'*'
|
|
|
|
}
|
|
|
|
.div {
|
|
|
|
'/'
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${d.name} ${op} ${qm}'
|
2021-07-23 12:33:55 +03:00
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${qm}'
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${qm}'
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
if num {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${c}'
|
2021-07-23 12:33:55 +03:00
|
|
|
c++
|
|
|
|
}
|
|
|
|
if i < data.fields.len - 1 {
|
|
|
|
str += ', '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str += ' WHERE '
|
|
|
|
}
|
|
|
|
.delete {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += 'DELETE FROM ${q}${table}${q} WHERE '
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if kind == .update || kind == .delete {
|
|
|
|
for i, field in where.fields {
|
2022-09-09 21:08:48 +03:00
|
|
|
mut pre_par := false
|
|
|
|
mut post_par := false
|
|
|
|
for par in where.parentheses {
|
|
|
|
if i in par {
|
|
|
|
pre_par = par[0] == i
|
|
|
|
post_par = par[1] == i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pre_par {
|
|
|
|
str += '('
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${q}${field}${q} ${where.kinds[i].to_str()} ${qm}'
|
2021-07-23 12:33:55 +03:00
|
|
|
if num {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${c}'
|
2021-07-23 12:33:55 +03:00
|
|
|
c++
|
|
|
|
}
|
2022-09-09 21:08:48 +03:00
|
|
|
if post_par {
|
|
|
|
str += ')'
|
|
|
|
}
|
2021-07-23 12:33:55 +03:00
|
|
|
if i < where.fields.len - 1 {
|
|
|
|
str += ' AND '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str += ';'
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
$if trace_orm_stmt ? {
|
|
|
|
eprintln('> orm_stmt sql_dialect: ${sql_dialect} | table: ${table} | kind: ${kind} | query: ${str}')
|
|
|
|
}
|
|
|
|
$if trace_orm ? {
|
|
|
|
eprintln('> orm: ${str}')
|
|
|
|
}
|
2022-07-27 00:59:32 +03:00
|
|
|
return str, QueryData{
|
|
|
|
fields: data_fields
|
|
|
|
data: data_data
|
|
|
|
types: data.types
|
|
|
|
kinds: data.kinds
|
|
|
|
is_and: data.is_and
|
|
|
|
}
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +03:00
|
|
|
// Generates an sql select stmt, from universal parameter
|
|
|
|
// orm - See SelectConfig
|
|
|
|
// q, num, qm, start_pos - see orm_stmt_gen
|
|
|
|
// where - See QueryData
|
|
|
|
pub fn orm_select_gen(orm SelectConfig, q string, num bool, qm string, start_pos int, where QueryData) string {
|
2021-07-23 12:33:55 +03:00
|
|
|
mut str := 'SELECT '
|
|
|
|
|
|
|
|
if orm.is_count {
|
|
|
|
str += 'COUNT(*)'
|
|
|
|
} else {
|
|
|
|
for i, field in orm.fields {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${q}${field}${q}'
|
2021-07-23 12:33:55 +03:00
|
|
|
if i < orm.fields.len - 1 {
|
|
|
|
str += ', '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
str += ' FROM ${q}${orm.table}${q}'
|
2021-07-23 12:33:55 +03:00
|
|
|
|
|
|
|
mut c := start_pos
|
|
|
|
|
|
|
|
if orm.has_where {
|
|
|
|
str += ' WHERE '
|
|
|
|
for i, field in where.fields {
|
2022-09-09 21:08:48 +03:00
|
|
|
mut pre_par := false
|
|
|
|
mut post_par := false
|
|
|
|
for par in where.parentheses {
|
|
|
|
if i in par {
|
|
|
|
pre_par = par[0] == i
|
|
|
|
post_par = par[1] == i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pre_par {
|
|
|
|
str += '('
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${q}${field}${q} ${where.kinds[i].to_str()} ${qm}'
|
2021-07-23 12:33:55 +03:00
|
|
|
if num {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${c}'
|
2021-07-23 12:33:55 +03:00
|
|
|
c++
|
|
|
|
}
|
2022-09-09 21:08:48 +03:00
|
|
|
if post_par {
|
|
|
|
str += ')'
|
|
|
|
}
|
2021-07-23 12:33:55 +03:00
|
|
|
if i < where.fields.len - 1 {
|
|
|
|
if where.is_and[i] {
|
|
|
|
str += ' AND '
|
|
|
|
} else {
|
|
|
|
str += ' OR '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: do not order, if the user did not want it explicitly,
|
2021-12-23 17:43:22 +03:00
|
|
|
// ordering is *slow*, especially if there are no indexes!
|
2021-07-23 12:33:55 +03:00
|
|
|
if orm.has_order {
|
2021-12-23 17:43:22 +03:00
|
|
|
str += ' ORDER BY '
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${q}${orm.order}${q} '
|
2021-07-23 12:33:55 +03:00
|
|
|
str += orm.order_type.to_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
if orm.has_limit {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += ' LIMIT ${qm}'
|
2021-07-23 12:33:55 +03:00
|
|
|
if num {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${c}'
|
2021-07-23 12:33:55 +03:00
|
|
|
c++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if orm.has_offset {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += ' OFFSET ${qm}'
|
2021-07-23 12:33:55 +03:00
|
|
|
if num {
|
2022-11-15 16:53:13 +03:00
|
|
|
str += '${c}'
|
2021-07-23 12:33:55 +03:00
|
|
|
c++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str += ';'
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
$if trace_orm_query ? {
|
|
|
|
eprintln('> orm_query: ${str}')
|
|
|
|
}
|
|
|
|
$if trace_orm ? {
|
|
|
|
eprintln('> orm: ${str}')
|
|
|
|
}
|
2021-07-23 12:33:55 +03:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +03:00
|
|
|
// Generates an sql table stmt, from universal parameter
|
|
|
|
// table - Table name
|
|
|
|
// q - see orm_stmt_gen
|
|
|
|
// defaults - enables default values in stmt
|
|
|
|
// def_unique_len - sets default unique length for texts
|
|
|
|
// fields - See TableField
|
|
|
|
// sql_from_v - Function which maps type indices to sql type names
|
|
|
|
// alternative - Needed for msdb
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn orm_table_gen(table string, q string, defaults bool, def_unique_len int, fields []TableField, sql_from_v fn (int) !string, alternative bool) !string {
|
2022-11-15 16:53:13 +03:00
|
|
|
mut str := 'CREATE TABLE IF NOT EXISTS ${q}${table}${q} ('
|
2021-07-23 12:33:55 +03:00
|
|
|
|
|
|
|
if alternative {
|
2022-11-15 16:53:13 +03:00
|
|
|
str = 'IF NOT EXISTS (SELECT * FROM sysobjects WHERE name=${q}${table}${q} and xtype=${q}U${q}) CREATE TABLE ${q}${table}${q} ('
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mut fs := []string{}
|
|
|
|
mut unique_fields := []string{}
|
|
|
|
mut unique := map[string][]string{}
|
|
|
|
mut primary := ''
|
|
|
|
|
|
|
|
for field in fields {
|
|
|
|
if field.is_arr {
|
|
|
|
continue
|
|
|
|
}
|
2022-07-27 00:59:32 +03:00
|
|
|
mut default_val := field.default_val
|
2021-07-23 12:33:55 +03:00
|
|
|
mut no_null := false
|
|
|
|
mut is_unique := false
|
|
|
|
mut is_skip := false
|
|
|
|
mut unique_len := 0
|
2021-08-16 14:59:23 +03:00
|
|
|
mut field_name := sql_field_name(field)
|
2022-07-13 18:03:30 +03:00
|
|
|
mut ctyp := sql_from_v(sql_field_type(field)) or {
|
|
|
|
field_name = '${field_name}_id'
|
2022-10-26 11:26:28 +03:00
|
|
|
sql_from_v(7)!
|
2022-07-13 18:03:30 +03:00
|
|
|
}
|
2021-07-23 12:33:55 +03:00
|
|
|
for attr in field.attrs {
|
|
|
|
match attr.name {
|
2023-05-02 18:00:54 +03:00
|
|
|
'sql' {
|
|
|
|
// [sql:'-']
|
|
|
|
if attr.arg == '-' {
|
|
|
|
is_skip = true
|
|
|
|
}
|
|
|
|
}
|
2021-07-23 12:33:55 +03:00
|
|
|
'primary' {
|
|
|
|
primary = field.name
|
|
|
|
}
|
|
|
|
'unique' {
|
|
|
|
if attr.arg != '' {
|
|
|
|
if attr.kind == .string {
|
2021-08-16 14:59:23 +03:00
|
|
|
unique[attr.arg] << field_name
|
2021-07-23 12:33:55 +03:00
|
|
|
continue
|
|
|
|
} else if attr.kind == .number {
|
|
|
|
unique_len = attr.arg.int()
|
|
|
|
is_unique = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is_unique = true
|
|
|
|
}
|
|
|
|
'nonull' {
|
|
|
|
no_null = true
|
|
|
|
}
|
|
|
|
'skip' {
|
|
|
|
is_skip = true
|
|
|
|
}
|
2022-07-13 18:03:30 +03:00
|
|
|
'sql_type' {
|
|
|
|
if attr.kind != .string {
|
2023-05-02 18:00:54 +03:00
|
|
|
return error("sql_type attribute needs to be string. Try [sql_type: '${attr.arg}'] instead of [sql_type: ${attr.arg}]")
|
2022-07-13 18:03:30 +03:00
|
|
|
}
|
|
|
|
ctyp = attr.arg
|
|
|
|
}
|
2022-07-27 00:59:32 +03:00
|
|
|
'default' {
|
|
|
|
if attr.kind != .string {
|
2023-05-02 18:00:54 +03:00
|
|
|
return error("default attribute needs to be string. Try [default: '${attr.arg}'] instead of [default: ${attr.arg}]")
|
2022-07-27 00:59:32 +03:00
|
|
|
}
|
|
|
|
if default_val == '' {
|
|
|
|
default_val = attr.arg
|
|
|
|
}
|
|
|
|
}
|
2021-07-23 12:33:55 +03:00
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_skip {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mut stmt := ''
|
|
|
|
if ctyp == '' {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('Unknown type (${field.typ}) for field ${field.name} in struct ${table}')
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
stmt = '${q}${field_name}${q} ${ctyp}'
|
2022-07-27 00:59:32 +03:00
|
|
|
if defaults && default_val != '' {
|
2022-11-15 16:53:13 +03:00
|
|
|
stmt += ' DEFAULT ${default_val}'
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
if no_null {
|
|
|
|
stmt += ' NOT NULL'
|
|
|
|
}
|
|
|
|
if is_unique {
|
2022-11-15 16:53:13 +03:00
|
|
|
mut f := 'UNIQUE(${q}${field_name}${q}'
|
2021-07-23 12:33:55 +03:00
|
|
|
if ctyp == 'TEXT' && def_unique_len > 0 {
|
|
|
|
if unique_len > 0 {
|
2022-11-15 16:53:13 +03:00
|
|
|
f += '(${unique_len})'
|
2021-07-23 12:33:55 +03:00
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
f += '(${def_unique_len})'
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f += ')'
|
|
|
|
unique_fields << f
|
|
|
|
}
|
|
|
|
fs << stmt
|
|
|
|
}
|
|
|
|
if primary == '' {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('A primary key is required for ${table}')
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
if unique.len > 0 {
|
|
|
|
for k, v in unique {
|
|
|
|
mut tmp := []string{}
|
|
|
|
for f in v {
|
2022-11-15 16:53:13 +03:00
|
|
|
tmp << '${q}${f}${q}'
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
fs << '/* ${k} */UNIQUE(${tmp.join(', ')})'
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
fs << 'PRIMARY KEY(${q}${primary}${q})'
|
2021-07-23 12:33:55 +03:00
|
|
|
fs << unique_fields
|
|
|
|
str += fs.join(', ')
|
|
|
|
str += ');'
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
$if trace_orm_create ? {
|
|
|
|
eprintln('> orm_create table: ${table} | query: ${str}')
|
|
|
|
}
|
|
|
|
$if trace_orm ? {
|
|
|
|
eprintln('> orm: ${str}')
|
|
|
|
}
|
2021-07-23 12:33:55 +03:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +03:00
|
|
|
// Get's the sql field type
|
2021-07-23 12:33:55 +03:00
|
|
|
fn sql_field_type(field TableField) int {
|
|
|
|
mut typ := field.typ
|
|
|
|
if field.is_time {
|
|
|
|
return -2
|
|
|
|
}
|
|
|
|
for attr in field.attrs {
|
|
|
|
if attr.kind == .plain && attr.name == 'sql' && attr.arg != '' {
|
|
|
|
if attr.arg.to_lower() == 'serial' {
|
|
|
|
typ = -1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
typ = orm.type_idx[attr.arg]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:53:09 +03:00
|
|
|
// Get's the sql field name
|
2021-07-23 12:33:55 +03:00
|
|
|
fn sql_field_name(field TableField) string {
|
|
|
|
mut name := field.name
|
|
|
|
for attr in field.attrs {
|
|
|
|
if attr.name == 'sql' && attr.has_arg && attr.kind == .string {
|
|
|
|
name = attr.arg
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// needed for backend functions
|
|
|
|
|
|
|
|
pub fn bool_to_primitive(b bool) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn f32_to_primitive(b f32) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn f64_to_primitive(b f64) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn i8_to_primitive(b i8) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn i16_to_primitive(b i16) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn int_to_primitive(b int) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
2022-12-09 21:34:34 +03:00
|
|
|
// int_literal_to_primitive handles int literal value
|
|
|
|
pub fn int_literal_to_primitive(b int) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// float_literal_to_primitive handles float literal value
|
|
|
|
pub fn float_literal_to_primitive(b f64) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:33:55 +03:00
|
|
|
pub fn i64_to_primitive(b i64) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
2022-04-15 15:55:39 +03:00
|
|
|
pub fn u8_to_primitive(b u8) Primitive {
|
2021-07-23 12:33:55 +03:00
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn u16_to_primitive(b u16) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn u32_to_primitive(b u32) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn u64_to_primitive(b u64) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn string_to_primitive(b string) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn time_to_primitive(b time.Time) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn infix_to_primitive(b InfixType) Primitive {
|
|
|
|
return Primitive(b)
|
|
|
|
}
|
2022-07-27 00:59:32 +03:00
|
|
|
|
|
|
|
fn factory_insert_qm_value(num bool, qm string, c int) string {
|
|
|
|
if num {
|
2022-11-15 16:53:13 +03:00
|
|
|
return '${qm}${c}'
|
2022-07-27 00:59:32 +03:00
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
return '${qm}'
|
2022-07-27 00:59:32 +03:00
|
|
|
}
|
|
|
|
}
|