diff --git a/vlib/orm/orm_null_test.v b/vlib/orm/orm_null_test.v index 23b60b036a..bde01b3be4 100644 --- a/vlib/orm/orm_null_test.v +++ b/vlib/orm/orm_null_test.v @@ -103,13 +103,13 @@ struct Foo { mut: id u64 @[primary; sql: serial] a string - // b string [default: '"yes"'] - c ?string - d ?string = 'hi' - e int - // f int [default: 33] - g ?int - h ?int = 55 + b string @[default: '"yes"'] + c ?string + d ?string = 'hi' + e int + f int @[default: 33] + g ?int + h ?int = 55 } fn test_option_struct_fields_and_none() { @@ -118,12 +118,12 @@ fn test_option_struct_fields_and_none() { sql db { create table Foo }! - assert db.st.last == 'CREATE TABLE IF NOT EXISTS `foo` (`id` serial-type NOT NULL, `a` string-type NOT NULL, `c` string-type, `d` string-type, `e` int-type NOT NULL, `g` int-type, `h` int-type, PRIMARY KEY(`id`));' + assert db.st.last == 'CREATE TABLE IF NOT EXISTS `foo` (`id` serial-type NOT NULL, `a` string-type NOT NULL, `b` string-type DEFAULT "yes" NOT NULL, `c` string-type, `d` string-type, `e` int-type NOT NULL, `f` int-type DEFAULT 33 NOT NULL, `g` int-type, `h` int-type, PRIMARY KEY(`id`));' _ := sql db { select from Foo where e > 5 && c is none && c !is none && h == 2 }! - assert db.st.last == 'SELECT `id`, `a`, `c`, `d`, `e`, `g`, `h` FROM `foo` WHERE `e` > ? AND `c` IS NULL AND `c` IS NOT NULL AND `h` = ?;' + assert db.st.last == 'SELECT `id`, `a`, `b`, `c`, `d`, `e`, `f`, `g`, `h` FROM `foo` WHERE `e` > ? AND `c` IS NULL AND `c` IS NOT NULL AND `h` = ?;' assert db.st.data.len == 0 assert db.st.where.len == 2 assert db.st.where == [orm.Primitive(int(5)), orm.Primitive(int(2))] @@ -141,7 +141,7 @@ fn test_option_struct_fields_and_none() { res1 := sql db { select from Foo where id == id }! - assert db.st.last == 'SELECT `id`, `a`, `c`, `d`, `e`, `g`, `h` FROM `foo` WHERE `id` = ?;' + assert db.st.last == 'SELECT `id`, `a`, `b`, `c`, `d`, `e`, `f`, `g`, `h` FROM `foo` WHERE `id` = ?;' assert db.st.data.len == 0 assert db.st.where.len == 1 assert db.st.where == [orm.Primitive(int(id))] @@ -149,9 +149,11 @@ fn test_option_struct_fields_and_none() { assert res1[0] == Foo{ id: 1 a: '' + b: 'yes' c: none d: 'hi' e: 0 + f: 33 g: none h: 55 } @@ -168,7 +170,7 @@ fn test_option_struct_fields_and_none() { res2 := sql db { select from Foo where id == id }! - assert db.st.last == 'SELECT `id`, `a`, `c`, `d`, `e`, `g`, `h` FROM `foo` WHERE `id` = ?;' + assert db.st.last == 'SELECT `id`, `a`, `b`, `c`, `d`, `e`, `f`, `g`, `h` FROM `foo` WHERE `id` = ?;' assert db.st.data.len == 0 assert db.st.where.len == 1 assert db.st.where == [orm.Primitive(int(id))] @@ -176,9 +178,11 @@ fn test_option_struct_fields_and_none() { assert res2[0] == Foo{ id: 1 a: '' + b: 'yes' c: 'yo' d: none e: 0 + f: 33 g: 44 h: none } @@ -186,9 +190,15 @@ fn test_option_struct_fields_and_none() { assert sql db { select count from Foo where a == 'yo' }! == 0 + assert sql db { + select count from Foo where b == 'yes' + }! == 1 assert sql db { select count from Foo where d == 'yo' }! == 0 + assert sql db { + select count from Foo where f == 33 + }! == 1 assert sql db { select count from Foo where c == 'yo' }! == 1 diff --git a/vlib/v/gen/c/orm.v b/vlib/v/gen/c/orm.v index a206c67a0a..eb324738fa 100644 --- a/vlib/v/gen/c/orm.v +++ b/vlib/v/gen/c/orm.v @@ -177,9 +177,13 @@ fn (mut g Gen) write_orm_create_table(node ast.SqlStmtLine, table_name string, c for attr in field.attrs { g.write('(VAttribute){') g.indent++ - g.write(' .name = _SLIT("${attr.name}"),') + name1 := util.smart_quote(attr.name, false) + name := cescape_nonascii(name1) + g.write(' .name = _SLIT("${name}"),') g.write(' .has_arg = ${attr.has_arg},') - g.write(' .arg = _SLIT("${attr.arg}"),') + arg1 := util.smart_quote(attr.arg, false) + arg := cescape_nonascii(arg1) + g.write(' .arg = _SLIT("${arg}"),') g.write(' .kind = ${int(attr.kind)},') g.indent-- g.write('},')