mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
mysql: fix for adapting mysql types to v structs (#15100)
This commit is contained in:

committed by
GitHub

parent
041e90b2e2
commit
a13b8ff0c8
@ -1,5 +1,6 @@
|
||||
import orm
|
||||
import mysql
|
||||
import time
|
||||
|
||||
struct TestCustomSqlType {
|
||||
id int [primary; sql: serial]
|
||||
@ -19,6 +20,15 @@ struct TestCustomWrongSqlType {
|
||||
custom3 string [sql_type: 'xml']
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
fn test_mysql_orm() {
|
||||
mut mdb := mysql.Connection{
|
||||
host: 'localhost'
|
||||
@ -78,6 +88,8 @@ fn test_mysql_orm() {
|
||||
name := res[0][1]
|
||||
age := res[0][2]
|
||||
|
||||
mdb.close()
|
||||
|
||||
assert id is int
|
||||
if id is int {
|
||||
assert id == 1
|
||||
@ -153,9 +165,57 @@ fn test_orm() {
|
||||
},
|
||||
]
|
||||
|
||||
assert result_custom_sql.maps() == information_schema_custom_sql
|
||||
|
||||
sql db {
|
||||
drop table TestCustomSqlType
|
||||
}
|
||||
db.close()
|
||||
|
||||
assert result_custom_sql.maps() == information_schema_custom_sql
|
||||
}
|
||||
|
||||
fn test_orm_time_type() ? {
|
||||
mut db := mysql.Connection{
|
||||
host: 'localhost'
|
||||
port: 3306
|
||||
username: 'root'
|
||||
password: ''
|
||||
dbname: 'mysql'
|
||||
}
|
||||
|
||||
db.connect() or {
|
||||
println(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
today := time.parse('2022-07-16 15:13:27')?
|
||||
|
||||
model := TestTimeType{
|
||||
username: 'hitalo'
|
||||
created_at: today
|
||||
updated_at: today.str()
|
||||
deleted_at: today
|
||||
}
|
||||
|
||||
sql db {
|
||||
create table TestTimeType
|
||||
}
|
||||
|
||||
sql db {
|
||||
insert model into TestTimeType
|
||||
}
|
||||
|
||||
results := sql db {
|
||||
select from TestTimeType where username == 'hitalo'
|
||||
}
|
||||
|
||||
sql db {
|
||||
drop table TestTimeType
|
||||
}
|
||||
|
||||
db.close()
|
||||
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user