parser: check orm sql statements, using undefined variables in where expr (fix #13367) (#13368)

pull/13374/head
yuyi 2022-02-05 17:32:41 +08:00 committed by GitHub
parent c9a8d6448d
commit 173b4652fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/orm_using_undefined_var_in_where_err.vv:24:35: error: undefined variable: `email`
22 |
23 | _ := sql db {
24 | select from User where email == email
| ~~~~~
25 | }
26 | }

View File

@ -0,0 +1,26 @@
import time
import sqlite
struct User {
id int [primary; sql: serial]
created_at time.Time
updated_at time.Time
email string
password string
name string
}
fn main() {
db := sqlite.connect(':memory:') ?
sql db {
create table User
}
m := User{}
sql db {
insert m into User
}
_ := sql db {
select from User where email == email
}
}

View File

@ -38,6 +38,13 @@ fn (mut p Parser) sql_expr() ast.Expr {
query_one = true
}
}
if e.right is ast.Ident {
if !p.scope.known_var(e.right.name) {
p.check_undefined_variables([e.left], e.right) or {
return p.error_with_pos(err.msg, e.right.pos)
}
}
}
}
}
mut has_limit := false