1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

strings: replace_each([]); orm: update

This commit is contained in:
Alexander Medvednikov
2019-12-10 14:32:12 +03:00
parent 4e69c40e12
commit cdfbb2978d
5 changed files with 177 additions and 1 deletions

View File

@ -1764,7 +1764,11 @@ fn (p mut Parser) var_expr(v Var) string {
name := p.tokens[p.token_idx].lit
if !name.contains('exec') && !name.starts_with('q_') {
p.next()
p.insert_query(fn_ph)
if name == 'insert' {
p.insert_query(fn_ph)
} else if name == 'update' {
p.update_query(fn_ph)
}
return 'void'
}
}

View File

@ -245,3 +245,63 @@ fn (p mut Parser) insert_query(fn_ph int) {
0, params, 0, 0, 0)')
}
// `db.update User set nr_orders=nr_orders+1`
fn (p mut Parser) update_query(fn_ph int) {
println('update query')
p.check_name()
table_name := p.check_name()
typ := p.table.find_type(table_name)
if typ.name == '' {
p.error('unknown type `$table_name`')
}
set := p.check_name()
if set != 'set' {
p.error('expected `set`')
}
if typ.fields.len == 0 {
p.error('V orm: update: empty fields in `$typ.name`')
}
if typ.fields[0].name != 'id' {
p.error('V orm: `id int` must be the first field in `$typ.name`')
}
field := p.check_name()
p.check(.assign)
for f in typ.fields {
if !(f.typ in ['string', 'int', 'bool']) {
println('orm: skipping $f.name')
continue
}
p.register_var({ f | is_mut: true, is_used:true, is_changed:true })
}
mut q := 'update ${typ.name}s set $field='
p.is_sql = true
set_typ, expr := p.tmp_expr()
p.is_sql = false
// TODO this hack should not be necessary
if set_typ == 'bool' {
if expr.trim_space() == '1' {
q += 'true'
}
else {
q += 'false'
}
} else {
q += expr
}
// where
if p.tok == .name && p.lit == 'where' {
p.next()
p.is_sql = true
_, wexpr := p.tmp_expr()
p.is_sql = false
q += ' where ' + wexpr
}
nr_vals := 0
p.cgen.insert_before('char* params[$nr_vals];')// + params)
p.cgen.set_placeholder(fn_ph, 'PQexecParams( ')
println('update q="$q"')
p.genln('.conn, "$q", $nr_vals, 0, params, 0, 0, 0)')
}