Skip to content

Commit

Permalink
orm: fix update stmt with enum value (fix vlang#23031) (vlang#23037)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 3, 2024
1 parent 31ce668 commit ebeef84
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/tools/vtest-self.v
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ const skip_with_fsanitize_memory = [
'vlib/v/tests/orm_array_field_test.v',
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
'vlib/v/tests/orm_create_several_tables_test.v',
'vlib/v/tests/orm_update_test.v',
'vlib/vweb/tests/vweb_test.v',
'vlib/vweb/csrf/csrf_test.v',
'vlib/net/http/request_test.v',
Expand All @@ -204,6 +205,7 @@ const skip_with_fsanitize_address = [
'vlib/v/tests/orm_sub_array_struct_test.v',
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
'vlib/v/tests/orm_create_several_tables_test.v',
'vlib/v/tests/orm_update_test.v',
]
const skip_with_fsanitize_undefined = [
'do_not_remove',
Expand All @@ -215,6 +217,7 @@ const skip_with_fsanitize_undefined = [
'vlib/v/tests/orm_sub_array_struct_test.v',
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
'vlib/v/tests/orm_create_several_tables_test.v',
'vlib/v/tests/orm_update_test.v',
'vlib/v/tests/project_with_cpp_code/compiling_cpp_files_with_a_cplusplus_compiler_test.c.v', // fails compilation with: undefined reference to vtable for __cxxabiv1::__function_type_info'
]
const skip_with_werror = [
Expand Down Expand Up @@ -274,6 +277,7 @@ const skip_on_ubuntu_musl = [
'vlib/v/tests/orm_array_field_test.v',
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
'vlib/v/tests/orm_create_several_tables_test.v',
'vlib/v/tests/orm_update_test.v',
'vlib/v/tests/sql_statement_inside_fn_call_test.v',
'vlib/clipboard/clipboard_test.v',
'vlib/vweb/tests/vweb_test.v',
Expand Down
4 changes: 3 additions & 1 deletion vlib/v/gen/c/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
ctyp = 'time__Time'
typ = 'time'
} else if sym.kind == .enum {
typ = 'i64'
typ = g.table.sym(g.table.final_type(field.typ)).cname
}
var := '${node.object_var}${member_access_type}${c_name(field.name)}'
if field.typ.has_flag(.option) {
Expand Down Expand Up @@ -604,6 +604,8 @@ fn (mut g Gen) write_orm_primitive(t ast.Type, expr ast.Expr) {

if t.has_flag(.option) {
typ = 'option_${typ}'
} else if g.table.final_sym(t).kind == .enum {
typ = g.table.sym(g.table.final_type(t)).cname
}
g.write('orm__${typ}_to_primitive(')
if expr is ast.CallExpr {
Expand Down
41 changes: 41 additions & 0 deletions vlib/v/tests/orm_update_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import db.sqlite

struct Person {
name string
height Height
}

enum Height as u8 {
tall
small
}

fn test_main() {
db := sqlite.connect(':memory:')!

sql db {
create table Person
}!

a := Person{'A', Height.small}
b := Person{'A', Height.tall}

sql db {
insert a into Person
}!

sql db {
insert b into Person
}!

new_height := Height.small
sql db {
update Person set height = new_height where height == Height.tall
}!

rows := sql db {
select from Person where height == Height.small
}!

assert rows.len == 2
}

0 comments on commit ebeef84

Please sign in to comment.