From 090e6e936a7b4063236689b6c3dcf290434cbec3 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 24 Jun 2020 14:32:14 +0200 Subject: [PATCH] orm: SqlStmt --- vlib/orm/orm_test.v | 6 ++++++ vlib/v/ast/ast.v | 12 ++++++++++-- vlib/v/checker/checker.v | 4 ++-- vlib/v/gen/cgen.v | 2 +- vlib/v/gen/js/js.v | 2 +- vlib/v/gen/sql.v | 2 +- vlib/v/parser/sql.v | 4 ++-- 7 files changed, 23 insertions(+), 9 deletions(-) diff --git a/vlib/orm/orm_test.v b/vlib/orm/orm_test.v index 8bf3587a2f..e8a6e789fd 100644 --- a/vlib/orm/orm_test.v +++ b/vlib/orm/orm_test.v @@ -123,6 +123,12 @@ fn test_orm_sqlite() { println(customer) assert customer.is_customer == true assert customer.name == 'Kate' + // + /* + sql db { + update User set age = 31 where name = 'Kate' + } + */ } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 2b53e416f9..23de277e02 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -18,7 +18,7 @@ pub type Expr = AnonFn | ArrayInit | AsCast | Assoc | BoolLiteral | CallExpr | C pub type Stmt = AssertStmt | AssignStmt | Attr | Block | BranchStmt | Comment | CompIf | ConstDecl | DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt | GlobalDecl | GoStmt | GotoLabel | GotoStmt | HashStmt | Import | InterfaceDecl | Module | - Return | SqlInsertExpr | StructDecl | TypeDecl | UnsafeStmt + Return | SqlStmt | StructDecl | TypeDecl | UnsafeStmt pub type ScopeObject = ConstField | GlobalDecl | Var @@ -808,8 +808,16 @@ pub enum SqlExprKind { update } */ -pub struct SqlInsertExpr { + +pub enum SqlStmtKind { + insert + update + delete +} + +pub struct SqlStmt { pub: + kind SqlStmtKind db_expr Expr // `db` in `sql db {` table_name string object_var_name string // `user` diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5da6cca270..15c1e87409 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1817,7 +1817,7 @@ fn (mut c Checker) stmt(node ast.Stmt) { c.return_stmt(mut it) c.scope_returns = true } - ast.SqlInsertExpr { + ast.SqlStmt { c.sql_insert_expr(node) } ast.StructDecl { @@ -2654,7 +2654,7 @@ fn (mut c Checker) sql_expr(node ast.SqlExpr) table.Type { 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) return table.void_type } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index a7b8a33eca..772114f868 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -770,7 +770,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { g.write_autofree_stmts_when_needed(node) g.return_statement(node) } - ast.SqlInsertExpr{ + ast.SqlStmt{ g.sql_insert_expr(node) } ast.StructDecl { diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 9f63c191fc..e06630bbad 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -484,7 +484,7 @@ fn (mut g JsGen) stmt(node ast.Stmt) { } g.gen_return_stmt(it) } - ast.SqlInsertExpr{ + ast.SqlStmt{ } ast.StructDecl { g.gen_struct_decl(it) diff --git a/vlib/v/gen/sql.v b/vlib/v/gen/sql.v index cccf79b335..380be0c211 100644 --- a/vlib/v/gen/sql.v +++ b/vlib/v/gen/sql.v @@ -14,7 +14,7 @@ const ( 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) info := sym.info as table.Struct fields := info.fields.filter(it.typ in [table.string_type, table.int_type, table.bool_type] && diff --git a/vlib/v/parser/sql.v b/vlib/v/parser/sql.v index 78ef5c786d..e6700d9c3f 100644 --- a/vlib/v/parser/sql.v +++ b/vlib/v/parser/sql.v @@ -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 defer { p.inside_match = false } // `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]) table_name := sym.name p.check(.rcbr) - return ast.SqlInsertExpr{ + return ast.SqlStmt{ db_expr: db_expr table_name: table_name table_type: table_type