2021-07-23 12:33:55 +03:00
|
|
|
import orm
|
2023-01-13 18:02:32 +03:00
|
|
|
import db.mysql
|
2022-07-19 18:29:09 +03:00
|
|
|
import time
|
2021-07-23 12:33:55 +03:00
|
|
|
|
2022-07-13 18:03:30 +03:00
|
|
|
struct TestCustomSqlType {
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
custom string [sql_type: 'TEXT']
|
|
|
|
custom1 string [sql_type: 'VARCHAR(191)']
|
|
|
|
custom2 string [sql_type: 'datetime(3)']
|
|
|
|
custom3 string [sql_type: 'MEDIUMINT']
|
|
|
|
custom4 string [sql_type: 'DATETIME']
|
|
|
|
custom5 string [sql_type: 'datetime']
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TestCustomWrongSqlType {
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
custom string
|
|
|
|
custom1 string [sql_type: 'VARCHAR']
|
|
|
|
custom2 string [sql_type: 'money']
|
|
|
|
custom3 string [sql_type: 'xml']
|
|
|
|
}
|
|
|
|
|
2022-07-19 18:29:09 +03:00
|
|
|
struct TestTimeType {
|
|
|
|
mut:
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
username string
|
|
|
|
created_at time.Time [sql_type: 'DATETIME']
|
|
|
|
updated_at string [sql_type: 'DATETIME']
|
|
|
|
deleted_at time.Time
|
|
|
|
}
|
|
|
|
|
2022-07-27 00:59:32 +03:00
|
|
|
struct TestDefaultAtribute {
|
|
|
|
id string [primary; sql: serial]
|
|
|
|
name string
|
|
|
|
created_at string [default: 'CURRENT_TIMESTAMP'; sql_type: 'TIMESTAMP']
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:33:55 +03:00
|
|
|
fn test_mysql_orm() {
|
2022-07-27 00:59:32 +03:00
|
|
|
mut db := mysql.Connection{
|
2021-07-23 12:33:55 +03:00
|
|
|
host: 'localhost'
|
|
|
|
port: 3306
|
|
|
|
username: 'root'
|
|
|
|
password: ''
|
|
|
|
dbname: 'mysql'
|
|
|
|
}
|
2022-07-27 00:59:32 +03:00
|
|
|
db.connect() or { panic(err) }
|
|
|
|
defer {
|
|
|
|
db.close()
|
|
|
|
}
|
2021-07-23 12:33:55 +03:00
|
|
|
db.create('Test', [
|
|
|
|
orm.TableField{
|
|
|
|
name: 'id'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[int]().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'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[string]().idx
|
2021-07-23 12:33:55 +03:00
|
|
|
attrs: []
|
|
|
|
},
|
|
|
|
orm.TableField{
|
|
|
|
name: 'age'
|
2022-12-07 13:26:41 +03:00
|
|
|
typ: typeof[int]().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.int_to_primitive(101)]
|
|
|
|
}) or { panic(err) }
|
|
|
|
|
|
|
|
res := db.@select(orm.SelectConfig{
|
|
|
|
table: 'Test'
|
|
|
|
has_where: true
|
|
|
|
fields: ['id', 'name', 'age']
|
2022-12-07 13:26:41 +03:00
|
|
|
types: [typeof[int]().idx, typeof[string]().idx, typeof[i64]().idx]
|
2021-07-23 12:33:55 +03:00
|
|
|
}, orm.QueryData{}, orm.QueryData{
|
|
|
|
fields: ['name', 'age']
|
|
|
|
data: [orm.Primitive('Louis'), i64(101)]
|
2022-12-07 13:26:41 +03:00
|
|
|
types: [typeof[string]().idx, typeof[i64]().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 == 101
|
|
|
|
}
|
2022-07-13 18:03:30 +03:00
|
|
|
|
2022-07-27 00:59:32 +03:00
|
|
|
/** test orm sql type
|
|
|
|
* - verify if all type create by attribute sql_type has created
|
|
|
|
*/
|
2022-07-13 18:03:30 +03:00
|
|
|
sql db {
|
|
|
|
create table TestCustomSqlType
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-13 18:03:30 +03:00
|
|
|
|
|
|
|
mut result_custom_sql := db.query("
|
2023-04-04 08:23:06 +03:00
|
|
|
SELECT DATA_TYPE, COLUMN_TYPE
|
2022-07-13 18:03:30 +03:00
|
|
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
|
|
WHERE TABLE_NAME = 'TestCustomSqlType'
|
|
|
|
ORDER BY ORDINAL_POSITION
|
|
|
|
") or {
|
|
|
|
println(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
information_schema_custom_sql := [
|
|
|
|
{
|
|
|
|
'DATA_TYPE': 'bigint'
|
|
|
|
'COLUMN_TYPE': 'bigint unsigned'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'DATA_TYPE': 'text'
|
|
|
|
'COLUMN_TYPE': 'text'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'DATA_TYPE': 'varchar'
|
|
|
|
'COLUMN_TYPE': 'varchar(191)'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'DATA_TYPE': 'datetime'
|
|
|
|
'COLUMN_TYPE': 'datetime(3)'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'DATA_TYPE': 'mediumint'
|
|
|
|
'COLUMN_TYPE': 'mediumint'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'DATA_TYPE': 'datetime'
|
|
|
|
'COLUMN_TYPE': 'datetime'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'DATA_TYPE': 'datetime'
|
|
|
|
'COLUMN_TYPE': 'datetime'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2022-07-19 18:29:09 +03:00
|
|
|
sql db {
|
|
|
|
drop table TestCustomSqlType
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-19 18:29:09 +03:00
|
|
|
|
2022-07-13 18:03:30 +03:00
|
|
|
assert result_custom_sql.maps() == information_schema_custom_sql
|
2022-07-19 18:29:09 +03:00
|
|
|
|
2022-07-27 00:59:32 +03:00
|
|
|
/** test_orm_time_type
|
|
|
|
* - test time.Time v type with sql_type: 'TIMESTAMP'
|
|
|
|
* - test string v type with sql_type: 'TIMESTAMP'
|
|
|
|
* - test time.Time v type without
|
|
|
|
*/
|
|
|
|
today := time.parse('2022-07-16 15:13:27') or {
|
2022-07-19 18:29:09 +03:00
|
|
|
println(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
model := TestTimeType{
|
|
|
|
username: 'hitalo'
|
|
|
|
created_at: today
|
|
|
|
updated_at: today.str()
|
|
|
|
deleted_at: today
|
|
|
|
}
|
2022-07-13 18:03:30 +03:00
|
|
|
|
|
|
|
sql db {
|
2022-07-19 18:29:09 +03:00
|
|
|
create table TestTimeType
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-19 18:29:09 +03:00
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert model into TestTimeType
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-19 18:29:09 +03:00
|
|
|
|
|
|
|
results := sql db {
|
|
|
|
select from TestTimeType where username == 'hitalo'
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-19 18:29:09 +03:00
|
|
|
|
|
|
|
sql db {
|
|
|
|
drop table TestTimeType
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-19 18:29:09 +03:00
|
|
|
|
|
|
|
assert results[0].username == model.username
|
|
|
|
assert results[0].created_at == model.created_at
|
|
|
|
assert results[0].updated_at == model.updated_at
|
|
|
|
assert results[0].deleted_at == model.deleted_at
|
2022-07-27 00:59:32 +03:00
|
|
|
|
|
|
|
/** test default attribute
|
|
|
|
*/
|
|
|
|
sql db {
|
|
|
|
create table TestDefaultAtribute
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-27 00:59:32 +03:00
|
|
|
|
|
|
|
mut result_defaults := db.query("
|
|
|
|
SELECT COLUMN_DEFAULT
|
|
|
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
|
|
WHERE TABLE_NAME = 'TestDefaultAtribute'
|
|
|
|
ORDER BY ORDINAL_POSITION
|
|
|
|
") or {
|
|
|
|
println(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
mut information_schema_defaults_results := []string{}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
drop table TestDefaultAtribute
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-27 00:59:32 +03:00
|
|
|
|
|
|
|
information_schema_column_default_sql := [{
|
|
|
|
'COLUMN_DEFAULT': ''
|
|
|
|
}, {
|
|
|
|
'COLUMN_DEFAULT': ''
|
|
|
|
}, {
|
|
|
|
'COLUMN_DEFAULT': 'CURRENT_TIMESTAMP'
|
|
|
|
}]
|
|
|
|
assert information_schema_column_default_sql == result_defaults.maps()
|
2022-07-13 18:03:30 +03:00
|
|
|
}
|