ast: SqlExpr

pull/5394/head
Alexander Medvednikov 2020-06-16 12:14:22 +02:00
parent 2daf915371
commit e8f8defc3e
7 changed files with 72 additions and 17 deletions

View File

@ -1,6 +1,7 @@
import os import os
import pg import pg
import term import term
import sqlite
struct Modules { struct Modules {
id int id int
@ -10,7 +11,30 @@ struct Modules {
//nr_downloads int //nr_downloads int
} }
fn test_orm() { fn test_orm_sqlite() {
db := sqlite.connect(':memory:') or { panic(err) }
/*
db.exec("drop table if exists users")
db.exec("create table users (id integer primary key, name text default '');")
db.exec("insert into users (name) values ('Sam')")
db.exec("insert into users (name) values ('Peter')")
db.exec("insert into users (name) values ('Kate')")
nr_users := sql db {
//select count from modules
}
assert nr_users == 3
println('nr_users=')
println(nr_users)
//nr_modules := db.select count from modules
//nr_modules := db.select count from Modules where id == 1
//nr_modules := db.select count from Modules where
//name == 'Bob' && id == 1
*/
}
fn test_orm_pg() {
dbname := os.getenv('VDB_NAME') dbname := os.getenv('VDB_NAME')
dbuser := os.getenv('VDB_USER') dbuser := os.getenv('VDB_USER')
if dbname == '' || dbuser == '' { if dbname == '' || dbuser == '' {
@ -18,6 +42,7 @@ fn test_orm() {
return return
} }
db := pg.connect(dbname: dbname, user: dbuser) or { panic(err) } db := pg.connect(dbname: dbname, user: dbuser) or { panic(err) }
_ = db
/* /*
//nr_modules := db.select count from modules //nr_modules := db.select count from modules
//nr_modules := db.select count from Modules where id == 1 //nr_modules := db.select count from Modules where id == 1

View File

@ -27,9 +27,11 @@ fn C.sqlite3_finalize()
fn C.sqlite3_column_count(voidptr) int fn C.sqlite3_column_count(voidptr) int
// Opens the connection with a database. // Opens the connection with a database.
pub fn connect(path string) DB { pub fn connect(path string) ?DB {
db := &C.sqlite3(0) db := &C.sqlite3(0)
C.sqlite3_open(path.str, &db) if C.sqlite3_open(path.str, &db) != 0 {
return error('sqlite db error')
}
return DB{ return DB{
conn: db conn: db
} }

View File

@ -11,9 +11,9 @@ pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl
pub type Expr = AnonFn | ArrayInit | AsCast | AssignExpr | Assoc | BoolLiteral | CallExpr | pub type Expr = AnonFn | ArrayInit | AsCast | AssignExpr | Assoc | BoolLiteral | CallExpr |
CastExpr | CharLiteral | ComptimeCall | ConcatExpr | EnumVal | FloatLiteral | Ident | IfExpr | CastExpr | CharLiteral | ComptimeCall | ConcatExpr | EnumVal | FloatLiteral | Ident | IfExpr |
IfGuardExpr | IndexExpr | InfixExpr | IntegerLiteral | MapInit | MatchExpr | None | OrExpr | IfGuardExpr | IndexExpr | InfixExpr | IntegerLiteral | Likely | MapInit | MatchExpr | None |
ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectorExpr | SizeOf | StringInterLiteral | OrExpr | ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectorExpr | SizeOf | SqlExpr |
StringLiteral | StructInit | Type | TypeOf | Likely StringInterLiteral | StringLiteral | StructInit | Type | TypeOf
pub type Stmt = AssertStmt | AssignStmt | Attr | Block | BranchStmt | Comment | CompIf | ConstDecl | pub type Stmt = AssertStmt | AssignStmt | Attr | Block | BranchStmt | Comment | CompIf | ConstDecl |
DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt | GlobalDecl | GoStmt | DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt | GlobalDecl | GoStmt |
@ -764,8 +764,8 @@ pub:
pub struct Likely { pub struct Likely {
pub: pub:
expr Expr expr Expr
pos token.Position pos token.Position
is_likely bool // false for _unlikely_ is_likely bool // false for _unlikely_
} }
@ -796,7 +796,6 @@ pub:
method_name string method_name string
left Expr left Expr
is_vweb bool is_vweb bool
// vweb_stmts []Stmt
vweb_tmpl File vweb_tmpl File
pub mut: pub mut:
sym table.TypeSymbol sym table.TypeSymbol
@ -804,8 +803,12 @@ pub mut:
pub struct None { pub struct None {
pub: pub:
pos token.Position pos token.Position
foo int // todo foo int // todo
}
pub struct SqlExpr {
typ table.Type
} }
[inline] [inline]

View File

@ -10,7 +10,6 @@ import v.pref
import v.token import v.token
import v.util import v.util
import v.depgraph import v.depgraph
import term
// NB: keywords after 'new' are reserved in C++ // NB: keywords after 'new' are reserved in C++
const ( const (
@ -1481,6 +1480,9 @@ fn (mut g Gen) expr(node ast.Expr) {
ast.None { ast.None {
g.write('opt_none()') g.write('opt_none()')
} }
ast.OrExpr {
// this should never appear here
}
ast.ParExpr { ast.ParExpr {
g.write('(') g.write('(')
g.expr(it.expr) g.expr(it.expr)
@ -1501,6 +1503,9 @@ fn (mut g Gen) expr(node ast.Expr) {
// g.write(')') // g.write(')')
g.is_amp = false g.is_amp = false
} }
ast.RangeExpr {
// Only used in IndexExpr
}
ast.SizeOf { ast.SizeOf {
mut styp := it.type_name mut styp := it.type_name
if it.type_name == '' { if it.type_name == '' {
@ -1521,6 +1526,9 @@ fn (mut g Gen) expr(node ast.Expr) {
*/ */
g.write('sizeof($styp)') g.write('sizeof($styp)')
} }
ast.SqlExpr {
g.write('// sql expression')
}
ast.StringLiteral { ast.StringLiteral {
if it.is_raw { if it.is_raw {
escaped_val := it.val.replace_each(['"', '\\"', '\\', '\\\\']) escaped_val := it.val.replace_each(['"', '\\"', '\\', '\\\\'])
@ -1583,10 +1591,10 @@ fn (mut g Gen) expr(node ast.Expr) {
g.expr(it.expr) g.expr(it.expr)
g.write(')') g.write(')')
} }
else { //else {
// #printf("node=%d\n", node.typ); // #printf("node=%d\n", node.typ);
println(term.red('cgen.expr(): bad node ' + typeof(node))) //println(term.red('cgen.expr(): bad node ' + typeof(node)))
} //}
} }
} }

View File

@ -613,6 +613,9 @@ fn (mut g JsGen) expr(node ast.Expr) {
ast.SizeOf { ast.SizeOf {
// TODO // TODO
} }
ast.SqlExpr{
// TODO
}
ast.StringInterLiteral { ast.StringInterLiteral {
g.gen_string_inter_literal(it) g.gen_string_inter_literal(it)
} }

View File

@ -19,8 +19,12 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
node = p.parse_assign_ident() node = p.parse_assign_ident()
} }
.name { .name {
node = p.name_expr() if p.tok.lit == 'sql' && p.peek_tok.kind == .name {
p.is_stmt_ident = is_stmt_ident node = p.sql_expr()
} else {
node = p.name_expr()
p.is_stmt_ident = is_stmt_ident
}
} }
.string { .string {
node = p.string_expr() node = p.string_expr()

View File

@ -0,0 +1,10 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module parser
import v.ast
fn (mut p Parser) sql_expr() ast.SqlExpr {
return ast.SqlExpr{}
}