From ebeef84be9b9b87d0d2a1ed5e8a394428f6e42be Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 3 Dec 2024 18:17:51 -0300 Subject: [PATCH] orm: fix update stmt with enum value (fix #23031) (#23037) --- cmd/tools/vtest-self.v | 4 ++++ vlib/v/gen/c/orm.v | 4 +++- vlib/v/tests/orm_update_test.v | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/orm_update_test.v diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index 29dce360e3f5d0..45fdd36b9bdcce 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -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', @@ -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', @@ -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 = [ @@ -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', diff --git a/vlib/v/gen/c/orm.v b/vlib/v/gen/c/orm.v index a7f5dccc524af9..a1f3b69e2c7338 100644 --- a/vlib/v/gen/c/orm.v +++ b/vlib/v/gen/c/orm.v @@ -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) { @@ -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 { diff --git a/vlib/v/tests/orm_update_test.v b/vlib/v/tests/orm_update_test.v new file mode 100644 index 00000000000000..a13dac460ba9eb --- /dev/null +++ b/vlib/v/tests/orm_update_test.v @@ -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 +}