v/vlib/orm/orm_fn_test.v
Tim Marston 756075380b
orm: add null handling and option fields (#19379)
* orm: added is none and !is none handling

* orm: added NullType, support option fields and deprecate [nonull]

Nullable DB fields are now determined by corresponding option struct field.  The
[nonull] attribute is deprecated and fields are all NOT NULL now, unless they
are option fields. New orm primitive, NullType, added to support passing none
values to db backends, which have been updated to support it.  Also, empty
string and 0 numberic values are no longer skipped during insert (since they may
be valid values).

* orm: fix [nonull] deprecation warning

* orm: add null handling to update and select

also, improved formatting for orm cgen, and removed optimised operand handling
of orm `is` and `!is` operators

* sqlite: read/report NULLs using new orm NullType

* postgres: returning data primitives now returns new orm.NullType

* orm: initialise NullType Primitives properly

* orm: do not smart cast operands inside sql

* orm: fix bad setting of option value

* orm: improve orm_null_test.v, adding/fixing selects

* orm: cleanup: rename NullType->Null, use serial const, cgen output

* orm: handle automatically generated fields more explicitly

During insert, fields which are
* [sql: serial]
* [default: whatever]
and where the data is a default value (e.g., 0, ""), those fields are not sent
to the db, so that the db can generate auto-increment or default values.  (This
was previously done only for [primary] fields, and not in all circumstances, but
that is not correct -- primary and serial/auto-increment fields are differnet.)

* orm: udpated README

* orm: select cgen fixes: read from uninit res; fail to init res

* orm: udpated tests

* orm: fix option sub-struct fields

* orm: fixed joins to option structs

Changed orm.write_orm_select() so that you pass to it the name of a resut
variable which it populates with the result (or not) and changed use of it in
sql_select_expr() and calls in write_orm_select() to populate substructs.

* orm: fix pg driver handling of NULL results

* orm: move runtime checks to comptime checker; cache checked tables

* orm: vfmt :(

* orm: markdown formatting

* orm: renamed orm.time_ and orm.enum_; updated db drivers

* checker: updated orm tests

* orm: fix issue setting up ast option values as orm primitives

* checker: ORM use of none/options and operations (added tests)

* orm: fixed tests

* db: clean code

* examples: remove orm nonull attributes

* orm: skip test memory santisation for orm_null_test.v

* orm: make the type-to-primitive converstion fns not public

* orm: mv object var c-code from checker->cgen; fix memory corruption

Code in checker/orm.v used the SqlStmtLine object field name to store c-specific
referenecs to option and array fields (for arrays of children).  I moved this
logic to cgen.  And fixed an issue introduced with option fields, where an array
of children was unpacked into a non-array result which could corrupt memory.

* orm: fixed vast error

* orm: skip 2 tests on ubuntu-musl which require sqlite3.h

* cgen: prevent casting a struct (string)

* v fmt orm_fkey_attribute.vv, orm_multidim_array.vv, orm_table_attributes.vv; run `VAUTOFIX=1 ./v vlib/v/compiler_errors_test.v`
2023-10-05 19:09:03 +03:00

312 lines
7.2 KiB
V

import orm
fn test_orm_stmt_gen_update() {
query_and, _ := orm.orm_stmt_gen(.default, 'Test', "'", .update, true, '?', 0, orm.QueryData{
fields: ['test', 'a']
data: []
types: []
kinds: []
}, orm.QueryData{
fields: ['id', 'name']
data: []
types: []
kinds: [.ge, .eq]
is_and: [true]
})
assert query_and == "UPDATE 'Test' SET 'test' = ?0, 'a' = ?1 WHERE 'id' >= ?2 AND 'name' = ?3;"
query_or, _ := orm.orm_stmt_gen(.default, 'Test', "'", .update, true, '?', 0, orm.QueryData{
fields: ['test', 'a']
data: []
types: []
kinds: []
}, orm.QueryData{
fields: ['id', 'name']
data: []
types: []
kinds: [.ge, .eq]
is_and: [false]
})
assert query_or == "UPDATE 'Test' SET 'test' = ?0, 'a' = ?1 WHERE 'id' >= ?2 OR 'name' = ?3;"
}
fn test_orm_stmt_gen_insert() {
query, _ := orm.orm_stmt_gen(.default, 'Test', "'", .insert, true, '?', 0, orm.QueryData{
fields: ['test', 'a']
data: []
types: []
kinds: []
}, orm.QueryData{})
assert query == "INSERT INTO 'Test' ('test', 'a') VALUES (?0, ?1);"
}
fn test_orm_stmt_gen_delete() {
query_and, _ := orm.orm_stmt_gen(.default, 'Test', "'", .delete, true, '?', 0, orm.QueryData{
fields: ['test', 'a']
data: []
types: []
kinds: []
}, orm.QueryData{
fields: ['id', 'name']
data: []
types: []
kinds: [.ge, .eq]
is_and: [true]
})
assert query_and == "DELETE FROM 'Test' WHERE 'id' >= ?0 AND 'name' = ?1;"
query_or, _ := orm.orm_stmt_gen(.default, 'Test', "'", .delete, true, '?', 0, orm.QueryData{
fields: ['test', 'a']
data: []
types: []
kinds: []
}, orm.QueryData{
fields: ['id', 'name']
data: []
types: []
kinds: [.ge, .eq]
is_and: [false]
})
assert query_or == "DELETE FROM 'Test' WHERE 'id' >= ?0 OR 'name' = ?1;"
}
fn get_select_fields() []string {
return ['id', 'test', 'abc']
}
fn test_orm_select_gen() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
}, "'", true, '?', 0, orm.QueryData{})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table';"
}
fn test_orm_select_gen_with_limit() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
has_limit: true
}, "'", true, '?', 0, orm.QueryData{})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' LIMIT ?0;"
}
fn test_orm_select_gen_with_where() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
has_where: true
}, "'", true, '?', 0, orm.QueryData{
fields: ['abc', 'test']
kinds: [.eq, .gt]
is_and: [true]
})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' WHERE 'abc' = ?0 AND 'test' > ?1;"
}
fn test_orm_select_gen_with_order() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
has_order: true
order_type: .desc
}, "'", true, '?', 0, orm.QueryData{})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' ORDER BY '' DESC;"
}
fn test_orm_select_gen_with_offset() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
has_offset: true
}, "'", true, '?', 0, orm.QueryData{})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' OFFSET ?0;"
}
fn test_orm_select_gen_with_all() {
query := orm.orm_select_gen(orm.SelectConfig{
table: 'test_table'
fields: get_select_fields()
has_limit: true
has_order: true
order_type: .desc
has_offset: true
has_where: true
}, "'", true, '?', 0, orm.QueryData{
fields: ['abc', 'test']
kinds: [.eq, .gt]
is_and: [true]
})
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' WHERE 'abc' = ?0 AND 'test' > ?1 ORDER BY '' DESC LIMIT ?2 OFFSET ?3;"
}
fn test_orm_table_gen() {
query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{
name: 'id'
typ: typeof[int]().idx
default_val: '10'
nullable: true
attrs: [
StructAttribute{
name: 'primary'
},
StructAttribute{
name: 'sql'
has_arg: true
arg: 'serial'
kind: .plain
},
]
},
orm.TableField{
name: 'test'
typ: typeof[string]().idx
nullable: true
},
orm.TableField{
name: 'abc'
typ: typeof[i64]().idx
nullable: true
default_val: '6754'
},
], sql_type_from_v, false) or { panic(err) }
assert query == "CREATE TABLE IF NOT EXISTS 'test_table' ('id' SERIAL DEFAULT 10, 'test' TEXT, 'abc' INT64 DEFAULT 6754, PRIMARY KEY('id'));"
alt_query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{
name: 'id'
typ: typeof[int]().idx
nullable: true
default_val: '10'
attrs: [
StructAttribute{
name: 'primary'
},
StructAttribute{
name: 'sql'
has_arg: true
arg: 'serial'
kind: .plain
},
]
},
orm.TableField{
name: 'test'
typ: typeof[string]().idx
nullable: true
},
orm.TableField{
name: 'abc'
typ: typeof[i64]().idx
nullable: true
default_val: '6754'
},
], sql_type_from_v, true) or { panic(err) }
assert alt_query == "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='test_table' and xtype='U') CREATE TABLE 'test_table' ('id' SERIAL DEFAULT 10, 'test' TEXT, 'abc' INT64 DEFAULT 6754, PRIMARY KEY('id'));"
unique_query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{
name: 'id'
typ: typeof[int]().idx
nullable: true
default_val: '10'
attrs: [
StructAttribute{
name: 'primary'
},
StructAttribute{
name: 'sql'
has_arg: true
arg: 'serial'
kind: .plain
},
]
},
orm.TableField{
name: 'test'
typ: typeof[string]().idx
attrs: [
StructAttribute{
name: 'unique'
},
]
},
orm.TableField{
name: 'abc'
typ: typeof[i64]().idx
default_val: '6754'
},
], sql_type_from_v, false) or { panic(err) }
assert unique_query == "CREATE TABLE IF NOT EXISTS 'test_table' ('id' SERIAL DEFAULT 10, 'test' TEXT NOT NULL, 'abc' INT64 DEFAULT 6754 NOT NULL, PRIMARY KEY('id'), UNIQUE('test'));"
mult_unique_query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{
name: 'id'
typ: typeof[int]().idx
nullable: true
default_val: '10'
attrs: [
StructAttribute{
name: 'primary'
},
StructAttribute{
name: 'sql'
has_arg: true
arg: 'serial'
kind: .plain
},
]
},
orm.TableField{
name: 'test'
typ: typeof[string]().idx
nullable: true
attrs: [
StructAttribute{
name: 'unique'
has_arg: true
arg: 'test'
kind: .string
},
]
},
orm.TableField{
name: 'abc'
typ: typeof[i64]().idx
nullable: true
default_val: '6754'
attrs: [
StructAttribute{
name: 'unique'
has_arg: true
arg: 'test'
kind: .string
},
]
},
], sql_type_from_v, false) or { panic(err) }
assert mult_unique_query == "CREATE TABLE IF NOT EXISTS 'test_table' ('id' SERIAL DEFAULT 10, 'test' TEXT, 'abc' INT64 DEFAULT 6754, /* test */UNIQUE('test', 'abc'), PRIMARY KEY('id'));"
}
fn sql_type_from_v(typ int) !string {
return if typ in orm.nums {
'INT'
} else if typ in orm.num64 {
'INT64'
} else if typ in orm.float {
'DOUBLE'
} else if typ == orm.type_string {
'TEXT'
} else if typ == orm.serial {
'SERIAL'
} else {
error('Unknown type ${typ}')
}
}