From 2a7a6fa1ebc1b42fa83cd5ffaff99f43fd6274ef Mon Sep 17 00:00:00 2001 From: Louis Schmieder Date: Tue, 7 Jul 2020 16:55:32 +0200 Subject: [PATCH] orm: add delete stmt (#5725) --- vlib/orm/orm_test.v | 9 +++++++++ vlib/v/gen/sql.v | 8 ++++++-- vlib/v/parser/sql.v | 31 +++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/vlib/orm/orm_test.v b/vlib/orm/orm_test.v index bc3a485858..f51d4135d6 100644 --- a/vlib/orm/orm_test.v +++ b/vlib/orm/orm_test.v @@ -209,6 +209,15 @@ fn test_orm_sqlite() { select from User order by age desc limit 1 offset offs } assert second_oldest.age == 31 + + sql db { + delete from User where age == 34 + } + + updated_oldest := sql db { + select from User order by age desc limit 1 + } + assert updated_oldest.age == 31 } fn test_orm_pg() { diff --git a/vlib/v/gen/sql.v b/vlib/v/gen/sql.v index cd183005d5..2082177074 100644 --- a/vlib/v/gen/sql.v +++ b/vlib/v/gen/sql.v @@ -28,8 +28,10 @@ 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 `${util.strip_mod_name(node.table_name)}` (') - } else { + } else if node.kind == .update { g.write('UPDATE `${util.strip_mod_name(node.table_name)}` SET ') + } else if node.kind == .delete { + g.write('DELETE FROM `${util.strip_mod_name(node.table_name)}` ') } if node.kind == .insert { for i, field in node.fields { @@ -61,8 +63,10 @@ fn (mut g Gen) sql_stmt(node ast.SqlStmt) { } } g.write(' WHERE ') + } else if node.kind == .delete { + g.write(' WHERE ') } - if node.kind == .update { + if node.kind == .update || node.kind == .delete { g.expr_to_sql(node.where_expr) } g.writeln('"));') diff --git a/vlib/v/parser/sql.v b/vlib/v/parser/sql.v index 0c9956bca5..bf597ac762 100644 --- a/vlib/v/parser/sql.v +++ b/vlib/v/parser/sql.v @@ -127,17 +127,19 @@ fn (mut p Parser) sql_stmt() ast.SqlStmt { } mut inserted_var_name := '' mut table_name := '' - expr := p.expr(0) - match expr { - ast.Ident { - if kind == .insert { - inserted_var_name = expr.name - } else if kind == .update { - table_name = expr.name + if kind != .delete { + expr := p.expr(0) + match expr { + ast.Ident { + if kind == .insert { + inserted_var_name = expr.name + } else if kind == .update { + table_name = expr.name + } + } + else { + p.error('can only insert variables') } - } - else { - p.error('can only insert variables') } } n = p.check_name() // into @@ -160,7 +162,10 @@ fn (mut p Parser) sql_stmt() ast.SqlStmt { break } } + } else if kind == .delete && n != 'from' { + p.error('expecting `from`') } + mut table_type := table.Type(0) mut where_expr := ast.Expr{} if kind == .insert { @@ -177,6 +182,12 @@ fn (mut p Parser) sql_stmt() ast.SqlStmt { } p.check_sql_keyword('where') where_expr = p.expr(0) + } else if kind == .delete { + table_type = p.parse_type() + sym := p.table.get_type_symbol(table_type) + table_name = sym.name + p.check_sql_keyword('where') + where_expr = p.expr(0) } p.check(.rcbr) return ast.SqlStmt{