Skip to content

Commit 173b465

Browse files
authored
parser: check orm sql statements, using undefined variables in where expr (fix #13367) (#13368)
1 parent c9a8d64 commit 173b465

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/orm_using_undefined_var_in_where_err.vv:24:35: error: undefined variable: `email`
2+
22 |
3+
23 | _ := sql db {
4+
24 | select from User where email == email
5+
| ~~~~~
6+
25 | }
7+
26 | }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import time
2+
import sqlite
3+
4+
struct User {
5+
id int [primary; sql: serial]
6+
created_at time.Time
7+
updated_at time.Time
8+
email string
9+
password string
10+
name string
11+
}
12+
13+
fn main() {
14+
db := sqlite.connect(':memory:') ?
15+
sql db {
16+
create table User
17+
}
18+
m := User{}
19+
sql db {
20+
insert m into User
21+
}
22+
23+
_ := sql db {
24+
select from User where email == email
25+
}
26+
}

vlib/v/parser/sql.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ fn (mut p Parser) sql_expr() ast.Expr {
3838
query_one = true
3939
}
4040
}
41+
if e.right is ast.Ident {
42+
if !p.scope.known_var(e.right.name) {
43+
p.check_undefined_variables([e.left], e.right) or {
44+
return p.error_with_pos(err.msg, e.right.pos)
45+
}
46+
}
47+
}
4148
}
4249
}
4350
mut has_limit := false

0 commit comments

Comments
 (0)