2021-07-23 12:33:55 +03:00
|
|
|
import orm
|
|
|
|
|
|
|
|
fn test_orm_stmt_gen_update() {
|
2023-02-16 12:34:16 +03:00
|
|
|
query, _ := orm.orm_stmt_gen(.default, 'Test', "'", .update, true, '?', 0, orm.QueryData{
|
2021-07-23 12:33:55 +03:00
|
|
|
fields: ['test', 'a']
|
|
|
|
data: []
|
|
|
|
types: []
|
|
|
|
kinds: []
|
|
|
|
}, orm.QueryData{
|
|
|
|
fields: ['id', 'name']
|
|
|
|
data: []
|
|
|
|
types: []
|
|
|
|
kinds: [.ge, .eq]
|
|
|
|
})
|
|
|
|
assert query == "UPDATE 'Test' SET 'test' = ?0, 'a' = ?1 WHERE 'id' >= ?2 AND 'name' = ?3;"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_orm_stmt_gen_insert() {
|
2023-02-16 12:34:16 +03:00
|
|
|
query, _ := orm.orm_stmt_gen(.default, 'Test', "'", .insert, true, '?', 0, orm.QueryData{
|
2021-07-23 12:33:55 +03:00
|
|
|
fields: ['test', 'a']
|
|
|
|
data: []
|
|
|
|
types: []
|
|
|
|
kinds: []
|
|
|
|
}, orm.QueryData{})
|
|
|
|
assert query == "INSERT INTO 'Test' ('test', 'a') VALUES (?0, ?1);"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_orm_stmt_gen_delete() {
|
2023-02-16 12:34:16 +03:00
|
|
|
query, _ := orm.orm_stmt_gen(.default, 'Test', "'", .delete, true, '?', 0, orm.QueryData{
|
2021-07-23 12:33:55 +03:00
|
|
|
fields: ['test', 'a']
|
|
|
|
data: []
|
|
|
|
types: []
|
|
|
|
kinds: []
|
|
|
|
}, orm.QueryData{
|
|
|
|
fields: ['id', 'name']
|
|
|
|
data: []
|
|
|
|
types: []
|
|
|
|
kinds: [.ge, .eq]
|
|
|
|
})
|
|
|
|
assert query == "DELETE FROM 'Test' WHERE 'id' >= ?0 AND '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{})
|
|
|
|
|
2021-12-23 17:43:22 +03:00
|
|
|
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table';"
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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{})
|
|
|
|
|
2021-12-23 17:43:22 +03:00
|
|
|
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' LIMIT ?0;"
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
|
|
|
})
|
|
|
|
|
2021-12-23 17:43:22 +03:00
|
|
|
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' WHERE 'abc' = ?0 AND 'test' > ?1;"
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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{})
|
|
|
|
|
2021-12-23 17:43:22 +03:00
|
|
|
assert query == "SELECT 'id', 'test', 'abc' FROM 'test_table' OFFSET ?0;"
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[int]().idx
|
2021-07-23 12:33:55 +03:00
|
|
|
default_val: '10'
|
|
|
|
attrs: [
|
|
|
|
StructAttribute{
|
|
|
|
name: 'primary'
|
|
|
|
},
|
|
|
|
StructAttribute{
|
|
|
|
name: 'sql'
|
|
|
|
has_arg: true
|
|
|
|
arg: 'serial'
|
|
|
|
kind: .plain
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'test'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[string]().idx
|
2021-07-23 12:33:55 +03:00
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'abc'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[i64]().idx
|
2021-07-23 12:33:55 +03:00
|
|
|
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'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[int]().idx
|
2021-07-23 12:33:55 +03:00
|
|
|
default_val: '10'
|
|
|
|
attrs: [
|
|
|
|
StructAttribute{
|
|
|
|
name: 'primary'
|
|
|
|
},
|
|
|
|
StructAttribute{
|
|
|
|
name: 'sql'
|
|
|
|
has_arg: true
|
|
|
|
arg: 'serial'
|
|
|
|
kind: .plain
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'test'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[string]().idx
|
2021-07-23 12:33:55 +03:00
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'abc'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[i64]().idx
|
2021-07-23 12:33:55 +03:00
|
|
|
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'));"
|
2021-08-06 06:21:59 +03:00
|
|
|
|
|
|
|
unique_query := orm.orm_table_gen('test_table', "'", true, 0, [
|
|
|
|
orm.TableField{
|
|
|
|
name: 'id'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[int]().idx
|
2021-08-06 06:21:59 +03:00
|
|
|
default_val: '10'
|
|
|
|
attrs: [
|
|
|
|
StructAttribute{
|
|
|
|
name: 'primary'
|
|
|
|
},
|
|
|
|
StructAttribute{
|
|
|
|
name: 'sql'
|
|
|
|
has_arg: true
|
|
|
|
arg: 'serial'
|
|
|
|
kind: .plain
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'test'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[string]().idx
|
2021-08-06 06:21:59 +03:00
|
|
|
attrs: [
|
|
|
|
StructAttribute{
|
|
|
|
name: 'unique'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'abc'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[i64]().idx
|
2021-08-06 06:21:59 +03:00
|
|
|
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, 'abc' INT64 DEFAULT 6754, PRIMARY KEY('id'), UNIQUE('test'));"
|
|
|
|
|
|
|
|
mult_unique_query := orm.orm_table_gen('test_table', "'", true, 0, [
|
|
|
|
orm.TableField{
|
|
|
|
name: 'id'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[int]().idx
|
2021-08-06 06:21:59 +03:00
|
|
|
default_val: '10'
|
|
|
|
attrs: [
|
|
|
|
StructAttribute{
|
|
|
|
name: 'primary'
|
|
|
|
},
|
|
|
|
StructAttribute{
|
|
|
|
name: 'sql'
|
|
|
|
has_arg: true
|
|
|
|
arg: 'serial'
|
|
|
|
kind: .plain
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'test'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[string]().idx
|
2021-08-06 06:21:59 +03:00
|
|
|
attrs: [
|
|
|
|
StructAttribute{
|
|
|
|
name: 'unique'
|
|
|
|
has_arg: true
|
|
|
|
arg: 'test'
|
|
|
|
kind: .string
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'abc'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[i64]().idx
|
2021-08-06 06:21:59 +03:00
|
|
|
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'));"
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
|
2022-10-26 11:26:28 +03:00
|
|
|
fn sql_type_from_v(typ int) !string {
|
2021-07-23 12:33:55 +03:00
|
|
|
return if typ in orm.nums {
|
|
|
|
'INT'
|
|
|
|
} else if typ in orm.num64 {
|
|
|
|
'INT64'
|
|
|
|
} else if typ in orm.float {
|
|
|
|
'DOUBLE'
|
2022-12-06 16:44:25 +03:00
|
|
|
} else if typ == orm.type_string {
|
2021-07-23 12:33:55 +03:00
|
|
|
'TEXT'
|
|
|
|
} else if typ == -1 {
|
|
|
|
'SERIAL'
|
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
error('Unknown type ${typ}')
|
2021-07-23 12:33:55 +03:00
|
|
|
}
|
|
|
|
}
|