orm: handle missing rows

pull/5520/head
Alexander Medvednikov 2020-06-26 20:38:17 +02:00
parent d2a4762c6a
commit d52f1da7a2
3 changed files with 25 additions and 16 deletions

View File

@ -162,6 +162,12 @@ fn test_orm_sqlite() {
}
assert kate3.age == 34
assert kate3.name == 'Kate N'
//
no_user := sql db {
select from User where id == 30
}
assert no_user.name == '' // TODO optional
assert no_user.age == 0
}
struct User {

View File

@ -3054,13 +3054,11 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype table.Type) ?bool {
g.write('${str_fn_name}(')
g.expr(expr)
g.write(')')
}
else if sym.kind == .alias && (sym.info as table.Alias).parent_type == table.string_type {
} else if sym.kind == .alias && (sym.info as table.Alias).parent_type == table.string_type {
// handle string aliases
g.expr(expr)
return true
}
else if sym.kind == .enum_ {
} else if sym.kind == .enum_ {
is_var := match expr {
ast.SelectorExpr { true }
ast.Ident { true }
@ -3331,7 +3329,8 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table.
g.writeln('\tstring err = ${cvar_name}.v_error;')
g.writeln('\tint errcode = ${cvar_name}.ecode;')
stmts := or_block.stmts
if stmts.len > 0 && stmts[or_block.stmts.len - 1] is ast.ExprStmt &&
if stmts.len > 0 &&
stmts[or_block.stmts.len - 1] is ast.ExprStmt &&
(stmts[stmts.len - 1] as ast.ExprStmt).typ != table.void_type {
g.indent++
for i, stmt in stmts {
@ -3730,6 +3729,12 @@ fn (mut g Gen) go_stmt(node ast.GoStmt) {
g.writeln('$wrapper_struct_name *$arg_tmp_var = malloc(sizeof(thread_arg_$name));')
if expr.is_method {
g.write('$arg_tmp_var->arg0 = ')
// TODO is this needed?
/*
if false && !expr.return_type.is_ptr() {
g.write('&')
}
*/
g.expr(expr.left)
g.writeln(';')
}

View File

@ -27,7 +27,7 @@ fn (mut g Gen) sql_stmt(node ast.SqlStmt) {
g.write('sqlite3_stmt* $g.sql_stmt_name = ${dbtype}__DB_init_stmt($db_name, tos_lit("')
if node.kind == .insert {
g.write('insert into $node.table_name (')
} else {
} else {
g.write('update $node.table_name set ')
}
if node.kind == .insert {
@ -40,7 +40,7 @@ fn (mut g Gen) sql_stmt(node ast.SqlStmt) {
g.write(', ')
}
}
g.write( ') values (')
g.write(') values (')
for i, field in node.fields {
if field.name == 'id' {
continue
@ -60,14 +60,10 @@ fn (mut g Gen) sql_stmt(node ast.SqlStmt) {
}
}
g.write(' where ')
}
if node.kind == .update {
g.expr_to_sql(node.where_expr)
}
g.writeln('"));')
if node.kind == .insert {
// build the object now (`x.name = ... x.id == ...`)
@ -185,6 +181,9 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
g.writeln('\tif (_step_res$tmp == SQLITE_DONE) break;')
g.writeln('\tif (_step_res$tmp == SQLITE_ROW) ;') // another row
g.writeln('\telse if (_step_res$tmp != SQLITE_OK) break;')
} else {
g.writeln('printf("RES: %d\\n", _step_res$tmp) ;')
g.writeln('\tif (_step_res$tmp == SQLITE_OK || _step_res$tmp == SQLITE_ROW) {')
}
for i, field in node.fields {
mut func := 'sqlite3_column_int'
@ -197,8 +196,8 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
}
if node.is_array {
g.writeln('\t array_push(&${tmp}_array, _MOV(($elem_type_str[]){ $tmp }));')
g.writeln('} // for')
}
g.writeln('}')
g.writeln('sqlite3_finalize($g.sql_stmt_name);')
if node.is_array {
g.writeln('$cur_line ${tmp}_array; ') // `array_User users = tmp_array;`
@ -234,8 +233,8 @@ fn (mut g Gen) expr_to_sql(expr ast.Expr) {
.le { g.write(' <= ') }
.and { g.write(' and ') }
.logical_or { g.write(' or ') }
.plus { g.write(' + ') }
.minus{ g.write(' - ') }
.plus { g.write(' + ') }
.minus { g.write(' - ') }
.mul { g.write(' * ') }
.div { g.write(' / ') }
else {}
@ -289,8 +288,7 @@ fn (mut g Gen) expr_to_sql(expr ast.Expr) {
}
ident := expr.expr as ast.Ident
g.sql_bind_int(ident.name + '.' + expr.field_name)
}
else {
} else {
verror('bad sql type=$expr.typ selector expr=$expr.field_name')
}
}