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

orm: fix null strings (#8497)

This commit is contained in:
Louis Schmieder 2021-02-01 21:44:09 +01:00 committed by GitHub
parent 8bf3fe5d48
commit 969f19daf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -29,6 +29,7 @@ fn test_orm_sqlite() {
db.exec("insert into User (name, age) values ('Sam', 29)") db.exec("insert into User (name, age) values ('Sam', 29)")
db.exec("insert into User (name, age) values ('Peter', 31)") db.exec("insert into User (name, age) values ('Peter', 31)")
db.exec("insert into User (name, age, is_customer) values ('Kate', 30, 1)") db.exec("insert into User (name, age, is_customer) values ('Kate', 30, 1)")
nr_all_users := sql db { nr_all_users := sql db {
select count from User select count from User
} }
@ -213,6 +214,12 @@ fn test_orm_sqlite() {
select from User order by age desc limit 1 select from User order by age desc limit 1
} }
assert updated_oldest.age == 31 assert updated_oldest.age == 31
db.exec('insert into User (name, age) values (NULL, 31)')
null_user := sql db {
select from User where id == 5
}
assert null_user.name == ''
} }
fn test_orm_pg() { fn test_orm_pg() {

View File

@ -226,7 +226,11 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
mut func := 'sqlite3_column_int' mut func := 'sqlite3_column_int'
if field.typ == table.string_type { if field.typ == table.string_type {
func = 'sqlite3_column_text' func = 'sqlite3_column_text'
g.writeln('${tmp}.$field.name = tos_clone(${func}($g.sql_stmt_name, $i));') string_data := g.new_tmp_var()
g.writeln('byteptr $string_data = ${func}($g.sql_stmt_name, $i);')
g.writeln('if ($string_data != NULL) {')
g.writeln('\t${tmp}.$field.name = tos_clone($string_data);')
g.writeln('}')
} else { } else {
g.writeln('${tmp}.$field.name = ${func}($g.sql_stmt_name, $i);') g.writeln('${tmp}.$field.name = ${func}($g.sql_stmt_name, $i);')
} }