Skip to content

Commit 21c1c9a

Browse files
committed
compiler/sqlite: validate qualified references in correlated subqueries
- Validate qualified column references across scopes - Reject invalid qualified refs in correlated subqueries - Validate qualified refs in subqueries Signed-off-by: Amirhossein Akhlaghpour <[email protected]>
1 parent 67e865b commit 21c1c9a

File tree

12 files changed

+321
-1
lines changed

12 files changed

+321
-1
lines changed

internal/compiler/analyze.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool)
181181
return nil, err
182182
}
183183

184+
if c.conf.Engine == config.EngineSQLite {
185+
if err := check(validate.ValidateSQLiteQualifiedColumnRefs(raw.Stmt)); err != nil {
186+
return nil, err
187+
}
188+
}
189+
184190
params, err := c.resolveCatalogRefs(qc, rvs, refs, namedParams, embeds)
185191
if err := check(err); err != nil {
186192
return nil, err
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- name: GetByPublicID :one
2+
SELECT *
3+
FROM locations l
4+
WHERE l.public_id = ?
5+
AND EXISTS (
6+
SELECT 1
7+
FROM projects p
8+
WHERE p.id = location.project_id
9+
);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CREATE TABLE organizations (
2+
id INTEGER PRIMARY KEY
3+
);
4+
5+
CREATE TABLE organization_members (
6+
id INTEGER PRIMARY KEY,
7+
organization_id INTEGER NOT NULL,
8+
account_id INTEGER NOT NULL
9+
);
10+
11+
CREATE TABLE projects (
12+
id INTEGER PRIMARY KEY,
13+
organization_id INTEGER NOT NULL
14+
);
15+
16+
CREATE TABLE locations (
17+
id INTEGER PRIMARY KEY,
18+
public_id TEXT UNIQUE NOT NULL,
19+
project_id INTEGER NOT NULL
20+
) STRICT;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: "2"
2+
3+
sql:
4+
- engine: "sqlite"
5+
schema: "schema.sql"
6+
queries: "query.sql"
7+
rules:
8+
- sqlc/db-prepare
9+
gen:
10+
go:
11+
package: "db"
12+
out: "generated"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# package db
2+
query.sql:8:18: table alias "location" does not exist
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# package querytest
2-
query.sql:1:1: sqlite3: SQL logic error: no such column: p.id
2+
query.sql:4:9: table alias "p" does not exist
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- name: GetByPublicID :one
2+
SELECT *
3+
FROM locations l
4+
WHERE l.public_id = ?
5+
AND EXISTS (
6+
SELECT 1
7+
FROM projects p
8+
WHERE p.id = location.project_id
9+
);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CREATE TABLE organizations (
2+
id INTEGER PRIMARY KEY
3+
);
4+
5+
CREATE TABLE organization_members (
6+
id INTEGER PRIMARY KEY,
7+
organization_id INTEGER NOT NULL,
8+
account_id INTEGER NOT NULL
9+
);
10+
11+
CREATE TABLE projects (
12+
id INTEGER PRIMARY KEY,
13+
organization_id INTEGER NOT NULL
14+
);
15+
16+
CREATE TABLE locations (
17+
id INTEGER PRIMARY KEY,
18+
public_id TEXT UNIQUE NOT NULL,
19+
project_id INTEGER NOT NULL
20+
) STRICT;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: "2"
2+
3+
sql:
4+
- engine: "sqlite"
5+
schema: "schema.sql"
6+
queries: "query.sql"
7+
rules:
8+
- sqlc/db-prepare
9+
gen:
10+
go:
11+
package: "db"
12+
out: "generated"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# package db
2+
query.sql:8:18: table alias "location" does not exist

0 commit comments

Comments
 (0)