Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orm: fix code generate for option time #20031

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/tools/vtest-self.v
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ const skip_with_fsanitize_memory = [
'vlib/orm/orm_custom_operators_test.v',
'vlib/orm/orm_fk_test.v',
'vlib/orm/orm_references_test.v',
'vlib/orm/orm_option_time_test.v',
'vlib/db/sqlite/sqlite_test.v',
'vlib/db/sqlite/sqlite_orm_test.v',
'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
Expand Down Expand Up @@ -223,6 +224,7 @@ const skip_on_ubuntu_musl = [
'vlib/orm/orm_custom_operators_test.v',
'vlib/orm/orm_fk_test.v',
'vlib/orm/orm_references_test.v',
'vlib/orm/orm_option_time_test.v',
'vlib/v/tests/orm_enum_test.v',
'vlib/v/tests/orm_sub_struct_test.v',
'vlib/v/tests/orm_sub_array_struct_test.v',
Expand Down
54 changes: 54 additions & 0 deletions vlib/orm/orm_option_time_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import db.sqlite
import time

@[table: 'foos']
struct Foo {
id int @[primary; sql: serial]
name string
created_at time.Time @[default: 'CURRENT_TIME']
updated_at ?string @[sql_type: 'TIMESTAMP']
deleted_at ?time.Time
children []Child @[fkey: 'parent_id']
}

struct Child {
id int @[primary; sql: serial]
parent_id int
name string
}

fn test_main() {
mut db := sqlite.connect(':memory:') or { panic(err) }
defer {
db.close() or { panic(err) }
}

sql db {
create table Foo
}!
foo := Foo{
name: 'abc'
created_at: time.now()
// updated_at defaults to none
// deleted_at defaults to none
children: [
Child{
name: 'abc'
},
Child{
name: 'def'
},
]
}

sql db {
insert foo into Foo
}!

data := sql db {
select from Foo
}![0]
assert data.id == 1
assert data.updated_at == none
assert data.deleted_at == none
}
4 changes: 3 additions & 1 deletion vlib/v/gen/c/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -370,20 +370,22 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
}
mut sym := g.table.sym(field.typ)
mut typ := sym.cname
mut ctyp := sym.cname
if sym.kind == .struct_ && typ != 'time__Time' {
g.writeln('(*(orm__Primitive*) array_get(${last_ids_arr}, ${structs})),')
structs++
continue
}
// fields processed hereafter can be NULL...
if typ == 'time__Time' {
ctyp = 'time__Time'
typ = 'time'
} else if sym.kind == .enum_ {
typ = 'i64'
}
var := '${node.object_var}${member_access_type}${c_name(field.name)}'
if field.typ.has_flag(.option) {
g.writeln('${var}.state == 2? _const_orm__null_primitive : orm__${typ}_to_primitive(*(${typ}*)(${var}.data)),')
g.writeln('${var}.state == 2? _const_orm__null_primitive : orm__${typ}_to_primitive(*(${ctyp}*)(${var}.data)),')
} else {
g.writeln('orm__${typ}_to_primitive(${var}),')
}
Expand Down
Loading