orm: SqlStmt

pull/5479/head^2
Alexander Medvednikov 2020-06-24 14:32:14 +02:00
parent 13faf864cb
commit 090e6e936a
7 changed files with 23 additions and 9 deletions

View File

@ -123,6 +123,12 @@ fn test_orm_sqlite() {
println(customer) println(customer)
assert customer.is_customer == true assert customer.is_customer == true
assert customer.name == 'Kate' assert customer.name == 'Kate'
//
/*
sql db {
update User set age = 31 where name = 'Kate'
}
*/
} }

View File

@ -18,7 +18,7 @@ pub type Expr = AnonFn | ArrayInit | AsCast | Assoc | BoolLiteral | CallExpr | C
pub type Stmt = AssertStmt | AssignStmt | Attr | Block | BranchStmt | Comment | CompIf | pub type Stmt = AssertStmt | AssignStmt | Attr | Block | BranchStmt | Comment | CompIf |
ConstDecl | DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt | ConstDecl | DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt |
GlobalDecl | GoStmt | GotoLabel | GotoStmt | HashStmt | Import | InterfaceDecl | Module | GlobalDecl | GoStmt | GotoLabel | GotoStmt | HashStmt | Import | InterfaceDecl | Module |
Return | SqlInsertExpr | StructDecl | TypeDecl | UnsafeStmt Return | SqlStmt | StructDecl | TypeDecl | UnsafeStmt
pub type ScopeObject = ConstField | GlobalDecl | Var pub type ScopeObject = ConstField | GlobalDecl | Var
@ -808,8 +808,16 @@ pub enum SqlExprKind {
update update
} }
*/ */
pub struct SqlInsertExpr {
pub enum SqlStmtKind {
insert
update
delete
}
pub struct SqlStmt {
pub: pub:
kind SqlStmtKind
db_expr Expr // `db` in `sql db {` db_expr Expr // `db` in `sql db {`
table_name string table_name string
object_var_name string // `user` object_var_name string // `user`

View File

@ -1817,7 +1817,7 @@ fn (mut c Checker) stmt(node ast.Stmt) {
c.return_stmt(mut it) c.return_stmt(mut it)
c.scope_returns = true c.scope_returns = true
} }
ast.SqlInsertExpr { ast.SqlStmt {
c.sql_insert_expr(node) c.sql_insert_expr(node)
} }
ast.StructDecl { ast.StructDecl {
@ -2654,7 +2654,7 @@ fn (mut c Checker) sql_expr(node ast.SqlExpr) table.Type {
return node.typ return node.typ
} }
fn (mut c Checker) sql_insert_expr(node ast.SqlInsertExpr) table.Type { fn (mut c Checker) sql_insert_expr(node ast.SqlStmt ) table.Type {
c.expr(node.db_expr) c.expr(node.db_expr)
return table.void_type return table.void_type
} }

View File

@ -770,7 +770,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
g.write_autofree_stmts_when_needed(node) g.write_autofree_stmts_when_needed(node)
g.return_statement(node) g.return_statement(node)
} }
ast.SqlInsertExpr{ ast.SqlStmt{
g.sql_insert_expr(node) g.sql_insert_expr(node)
} }
ast.StructDecl { ast.StructDecl {

View File

@ -484,7 +484,7 @@ fn (mut g JsGen) stmt(node ast.Stmt) {
} }
g.gen_return_stmt(it) g.gen_return_stmt(it)
} }
ast.SqlInsertExpr{ ast.SqlStmt{
} }
ast.StructDecl { ast.StructDecl {
g.gen_struct_decl(it) g.gen_struct_decl(it)

View File

@ -14,7 +14,7 @@ const (
enum SqlExprSide { left right } enum SqlExprSide { left right }
fn (mut g Gen) sql_insert_expr(node ast.SqlInsertExpr) { fn (mut g Gen) sql_insert_expr(node ast.SqlStmt) {
sym := g.table.get_type_symbol(node.table_type) sym := g.table.get_type_symbol(node.table_type)
info := sym.info as table.Struct info := sym.info as table.Struct
fields := info.fields.filter(it.typ in [table.string_type, table.int_type, table.bool_type] && fields := info.fields.filter(it.typ in [table.string_type, table.int_type, table.bool_type] &&

View File

@ -103,7 +103,7 @@ fn (mut p Parser) sql_expr() ast.Expr {
} }
} }
fn (mut p Parser) sql_insert_expr() ast.SqlInsertExpr { fn (mut p Parser) sql_insert_expr() ast.SqlStmt{
p.inside_match = true p.inside_match = true
defer { p.inside_match = false } defer { p.inside_match = false }
// `sql db {` // `sql db {`
@ -130,7 +130,7 @@ fn (mut p Parser) sql_insert_expr() ast.SqlInsertExpr {
// fields := info.fields.filter(it.typ in [table.string_type, table.int_type, table.bool_type]) // fields := info.fields.filter(it.typ in [table.string_type, table.int_type, table.bool_type])
table_name := sym.name table_name := sym.name
p.check(.rcbr) p.check(.rcbr)
return ast.SqlInsertExpr{ return ast.SqlStmt{
db_expr: db_expr db_expr: db_expr
table_name: table_name table_name: table_name
table_type: table_type table_type: table_type