1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

orm: default attribute (#15221)

This commit is contained in:
Hitalo de Jesus do Rosário Souza
2022-07-26 18:59:32 -03:00
committed by GitHub
parent c976a691ad
commit e5e750d533
11 changed files with 450 additions and 90 deletions

View File

@@ -1,6 +1,7 @@
import orm
import mysql
import time
import v.ast
struct TestCustomSqlType {
id int [primary; sql: serial]
@@ -29,20 +30,28 @@ mut:
deleted_at time.Time
}
struct TestDefaultAtribute {
id string [primary; sql: serial]
name string
created_at string [default: 'CURRENT_TIMESTAMP'; sql_type: 'TIMESTAMP']
}
fn test_mysql_orm() {
mut mdb := mysql.Connection{
mut db := mysql.Connection{
host: 'localhost'
port: 3306
username: 'root'
password: ''
dbname: 'mysql'
}
mdb.connect() or { panic(err) }
db := orm.Connection(mdb)
db.connect() or { panic(err) }
defer {
db.close()
}
db.create('Test', [
orm.TableField{
name: 'id'
typ: 7
typ: ast.int_type_idx
attrs: [
StructAttribute{
name: 'primary'
@@ -57,12 +66,12 @@ fn test_mysql_orm() {
},
orm.TableField{
name: 'name'
typ: 20
typ: ast.string_type_idx
attrs: []
},
orm.TableField{
name: 'age'
typ: 7
typ: ast.int_type_idx
},
]) or { panic(err) }
@@ -75,11 +84,11 @@ fn test_mysql_orm() {
table: 'Test'
has_where: true
fields: ['id', 'name', 'age']
types: [7, 20, 8]
types: [ast.int_type_idx, ast.string_type_idx, ast.i64_type_idx]
}, orm.QueryData{}, orm.QueryData{
fields: ['name', 'age']
data: [orm.Primitive('Louis'), i64(101)]
types: [20, 8]
types: [ast.string_type_idx, ast.i64_type_idx]
is_and: [true, true]
kinds: [.eq, .eq]
}) or { panic(err) }
@@ -88,8 +97,6 @@ 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
@@ -104,22 +111,10 @@ fn test_mysql_orm() {
if age is i64 {
assert age == 101
}
}
fn test_orm() {
mut db := mysql.Connection{
host: 'localhost'
port: 3306
username: 'root'
password: ''
dbname: 'mysql'
}
db.connect() or {
println(err)
panic(err)
}
/** test orm sql type
* - verify if all type create by attribute sql_type has created
*/
sql db {
create table TestCustomSqlType
}
@@ -168,27 +163,19 @@ fn test_orm() {
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 {
/** 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 {
println(err)
panic(err)
}
today := time.parse('2022-07-16 15:13:27')?
model := TestTimeType{
username: 'hitalo'
created_at: today
@@ -212,10 +199,38 @@ fn test_orm_time_type() ? {
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
/** test default attribute
*/
sql db {
create table TestDefaultAtribute
}
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
}
information_schema_column_default_sql := [{
'COLUMN_DEFAULT': ''
}, {
'COLUMN_DEFAULT': ''
}, {
'COLUMN_DEFAULT': 'CURRENT_TIMESTAMP'
}]
assert information_schema_column_default_sql == result_defaults.maps()
}

View File

@@ -124,7 +124,7 @@ pub fn (db Connection) insert(table string, data orm.QueryData) ? {
mut converted_primitive_array := db.factory_orm_primitive_converted_from_sql(table,
data)?
converted_data := orm.QueryData{
converted_primitive_data := orm.QueryData{
fields: data.fields
data: converted_primitive_array
types: []
@@ -132,17 +132,19 @@ pub fn (db Connection) insert(table string, data orm.QueryData) ? {
is_and: []
}
query := orm.orm_stmt_gen(table, '`', .insert, false, '?', 1, converted_data, orm.QueryData{})
query, converted_data := orm.orm_stmt_gen(table, '`', .insert, false, '?', 1, converted_primitive_data,
orm.QueryData{})
mysql_stmt_worker(db, query, converted_data, orm.QueryData{})?
}
pub fn (db Connection) update(table string, data orm.QueryData, where orm.QueryData) ? {
query := orm.orm_stmt_gen(table, '`', .update, false, '?', 1, data, where)
query, _ := orm.orm_stmt_gen(table, '`', .update, false, '?', 1, data, where)
mysql_stmt_worker(db, query, data, where)?
}
pub fn (db Connection) delete(table string, where orm.QueryData) ? {
query := orm.orm_stmt_gen(table, '`', .delete, false, '?', 1, orm.QueryData{}, where)
query, _ := orm.orm_stmt_gen(table, '`', .delete, false, '?', 1, orm.QueryData{},
where)
mysql_stmt_worker(db, query, orm.QueryData{}, where)?
}
@@ -158,7 +160,7 @@ pub fn (db Connection) last_id() orm.Primitive {
// table
pub fn (db Connection) create(table string, fields []orm.TableField) ? {
query := orm.orm_table_gen(table, '`', false, 0, fields, mysql_type_from_v, false) or {
query := orm.orm_table_gen(table, '`', true, 0, fields, mysql_type_from_v, false) or {
return err
}
mysql_stmt_worker(db, query, orm.QueryData{}, orm.QueryData{})?