mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
orm: add delete stmt (#5725)
This commit is contained in:
parent
4d7d1eb0c4
commit
2a7a6fa1eb
@ -209,6 +209,15 @@ fn test_orm_sqlite() {
|
|||||||
select from User order by age desc limit 1 offset offs
|
select from User order by age desc limit 1 offset offs
|
||||||
}
|
}
|
||||||
assert second_oldest.age == 31
|
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() {
|
fn test_orm_pg() {
|
||||||
|
@ -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("')
|
g.write('sqlite3_stmt* $g.sql_stmt_name = ${dbtype}__DB_init_stmt($db_name, tos_lit("')
|
||||||
if node.kind == .insert {
|
if node.kind == .insert {
|
||||||
g.write('INSERT INTO `${util.strip_mod_name(node.table_name)}` (')
|
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 ')
|
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 {
|
if node.kind == .insert {
|
||||||
for i, field in node.fields {
|
for i, field in node.fields {
|
||||||
@ -61,8 +63,10 @@ fn (mut g Gen) sql_stmt(node ast.SqlStmt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
g.write(' WHERE ')
|
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.expr_to_sql(node.where_expr)
|
||||||
}
|
}
|
||||||
g.writeln('"));')
|
g.writeln('"));')
|
||||||
|
@ -127,17 +127,19 @@ fn (mut p Parser) sql_stmt() ast.SqlStmt {
|
|||||||
}
|
}
|
||||||
mut inserted_var_name := ''
|
mut inserted_var_name := ''
|
||||||
mut table_name := ''
|
mut table_name := ''
|
||||||
expr := p.expr(0)
|
if kind != .delete {
|
||||||
match expr {
|
expr := p.expr(0)
|
||||||
ast.Ident {
|
match expr {
|
||||||
if kind == .insert {
|
ast.Ident {
|
||||||
inserted_var_name = expr.name
|
if kind == .insert {
|
||||||
} else if kind == .update {
|
inserted_var_name = expr.name
|
||||||
table_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
|
n = p.check_name() // into
|
||||||
@ -160,7 +162,10 @@ fn (mut p Parser) sql_stmt() ast.SqlStmt {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if kind == .delete && n != 'from' {
|
||||||
|
p.error('expecting `from`')
|
||||||
}
|
}
|
||||||
|
|
||||||
mut table_type := table.Type(0)
|
mut table_type := table.Type(0)
|
||||||
mut where_expr := ast.Expr{}
|
mut where_expr := ast.Expr{}
|
||||||
if kind == .insert {
|
if kind == .insert {
|
||||||
@ -177,6 +182,12 @@ fn (mut p Parser) sql_stmt() ast.SqlStmt {
|
|||||||
}
|
}
|
||||||
p.check_sql_keyword('where')
|
p.check_sql_keyword('where')
|
||||||
where_expr = p.expr(0)
|
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)
|
p.check(.rcbr)
|
||||||
return ast.SqlStmt{
|
return ast.SqlStmt{
|
||||||
|
Loading…
Reference in New Issue
Block a user