2021-07-23 12:33:55 +03:00
|
|
|
import orm
|
|
|
|
import sqlite
|
2021-09-08 05:53:39 +03:00
|
|
|
import v.ast
|
2021-07-23 12:33:55 +03:00
|
|
|
|
|
|
|
fn test_sqlite_orm() {
|
|
|
|
sdb := sqlite.connect(':memory:') or { panic(err) }
|
|
|
|
db := orm.Connection(sdb)
|
|
|
|
db.create('Test', [
|
|
|
|
orm.TableField{
|
|
|
|
name: 'id'
|
2021-09-08 05:53:39 +03:00
|
|
|
typ: ast.int_type_idx
|
2021-07-23 12:33:55 +03:00
|
|
|
attrs: [
|
|
|
|
StructAttribute{
|
|
|
|
name: 'primary'
|
|
|
|
},
|
|
|
|
StructAttribute{
|
|
|
|
name: 'sql'
|
|
|
|
has_arg: true
|
|
|
|
kind: .plain
|
|
|
|
arg: 'serial'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'name'
|
2021-09-08 05:53:39 +03:00
|
|
|
typ: ast.string_type_idx
|
2021-07-23 12:33:55 +03:00
|
|
|
attrs: []
|
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'age'
|
2021-09-08 05:53:39 +03:00
|
|
|
typ: ast.i64_type_idx
|
2021-07-23 12:33:55 +03:00
|
|
|
},
|
|
|
|
]) or { panic(err) }
|
|
|
|
|
|
|
|
db.insert('Test', orm.QueryData{
|
|
|
|
fields: ['name', 'age']
|
|
|
|
data: [orm.string_to_primitive('Louis'), orm.i64_to_primitive(100)]
|
|
|
|
}) or { panic(err) }
|
|
|
|
|
|
|
|
res := db.@select(orm.SelectConfig{
|
|
|
|
table: 'Test'
|
|
|
|
has_where: true
|
|
|
|
fields: ['id', 'name', 'age']
|
2021-09-08 05:53:39 +03:00
|
|
|
types: [ast.int_type_idx, ast.string_type_idx, ast.i64_type_idx]
|
2021-07-23 12:33:55 +03:00
|
|
|
}, orm.QueryData{}, orm.QueryData{
|
|
|
|
fields: ['name', 'age']
|
|
|
|
data: [orm.Primitive('Louis'), i64(100)]
|
2021-09-08 05:53:39 +03:00
|
|
|
types: [ast.string_type_idx, ast.i64_type_idx]
|
2021-07-23 12:33:55 +03:00
|
|
|
is_and: [true, true]
|
|
|
|
kinds: [.eq, .eq]
|
|
|
|
}) or { panic(err) }
|
|
|
|
|
|
|
|
id := res[0][0]
|
|
|
|
name := res[0][1]
|
|
|
|
age := res[0][2]
|
|
|
|
|
|
|
|
assert id is int
|
|
|
|
if id is int {
|
|
|
|
assert id == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
assert name is string
|
|
|
|
if name is string {
|
|
|
|
assert name == 'Louis'
|
|
|
|
}
|
|
|
|
|
|
|
|
assert age is i64
|
|
|
|
if age is i64 {
|
|
|
|
assert age == 100
|
|
|
|
}
|
|
|
|
}
|