Skip to content

Commit c2f489c

Browse files
committed
mssql: fix DML alias targets, byte offsets, dead keyword
Address review findings on the initial engine: - DELETE dropped its FROM clause, so the T-SQL join form (DELETE b FROM books b JOIN ...) failed to resolve. UPDATE and DELETE now resolve an alias target against the FROM clause: the matching relation becomes the statement's target, and the ON conditions of any inner join dissolved by pulling it out move to the WHERE clause. Relations under outer joins are left alone. - AST Location fields carried teesql's UTF-16 code-unit offsets while StmtLocation/StmtLen are byte offsets; the converter now maps locations through the same byte-offset table as statement spans. - Drop the unreachable "within group" reserved-keyword case: keywords are checked one identifier token at a time. - Cover INSERT ... OUTPUT and the UPDATE/DELETE alias forms end to end under analyze_dml/mssql. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FrnQ4EoZ3WCWWrSsm8bhCP
1 parent aacc881 commit c2f489c

8 files changed

Lines changed: 317 additions & 77 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "analyze",
3+
"args": ["--dialect", "mssql", "--schema", "schema.sql", "query.sql"],
4+
"contexts": ["base"]
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- name: CreateAuthor :one
2+
INSERT INTO authors (name, bio) OUTPUT INSERTED.id VALUES (@name, @bio);
3+
4+
-- name: UpdateAuthorAlias :exec
5+
UPDATE a SET name = @name FROM authors a WHERE a.id = @id;
6+
7+
-- name: UpdateBookPrices :exec
8+
UPDATE b SET price = @price FROM books b JOIN authors a ON b.author_id = a.id WHERE a.name = @author;
9+
10+
-- name: DeleteBooksByAuthor :exec
11+
DELETE b FROM books b INNER JOIN authors a ON b.author_id = a.id WHERE a.name = @name;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TABLE authors (
2+
id BIGINT IDENTITY(1,1) PRIMARY KEY,
3+
name NVARCHAR(100) NOT NULL,
4+
bio NVARCHAR(MAX)
5+
);
6+
7+
CREATE TABLE books (
8+
id BIGINT IDENTITY(1,1) PRIMARY KEY,
9+
author_id BIGINT NOT NULL,
10+
title NVARCHAR(200) NOT NULL,
11+
price DECIMAL(10,2)
12+
);
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
[
2+
{
3+
"name": "CreateAuthor",
4+
"cmd": ":one",
5+
"columns": [
6+
{
7+
"name": "id",
8+
"data_type": "bigint",
9+
"not_null": true,
10+
"is_array": false,
11+
"table": "authors"
12+
}
13+
],
14+
"params": [
15+
{
16+
"number": 1,
17+
"column": {
18+
"name": "name",
19+
"data_type": "nvarchar",
20+
"not_null": true,
21+
"is_array": false,
22+
"table": "authors"
23+
}
24+
},
25+
{
26+
"number": 2,
27+
"column": {
28+
"name": "bio",
29+
"data_type": "nvarchar",
30+
"not_null": false,
31+
"is_array": false,
32+
"table": "authors"
33+
}
34+
}
35+
]
36+
},
37+
{
38+
"name": "UpdateAuthorAlias",
39+
"cmd": ":exec",
40+
"columns": [],
41+
"params": [
42+
{
43+
"number": 1,
44+
"column": {
45+
"name": "name",
46+
"data_type": "nvarchar",
47+
"not_null": true,
48+
"is_array": false,
49+
"table": "authors"
50+
}
51+
},
52+
{
53+
"number": 2,
54+
"column": {
55+
"name": "id",
56+
"data_type": "bigint",
57+
"not_null": true,
58+
"is_array": false,
59+
"table": "authors"
60+
}
61+
}
62+
]
63+
},
64+
{
65+
"name": "UpdateBookPrices",
66+
"cmd": ":exec",
67+
"columns": [],
68+
"params": [
69+
{
70+
"number": 1,
71+
"column": {
72+
"name": "price",
73+
"data_type": "decimal",
74+
"not_null": false,
75+
"is_array": false,
76+
"table": "books"
77+
}
78+
},
79+
{
80+
"number": 2,
81+
"column": {
82+
"name": "name",
83+
"data_type": "nvarchar",
84+
"not_null": true,
85+
"is_array": false,
86+
"table": "authors"
87+
}
88+
}
89+
]
90+
},
91+
{
92+
"name": "DeleteBooksByAuthor",
93+
"cmd": ":exec",
94+
"columns": [],
95+
"params": [
96+
{
97+
"number": 1,
98+
"column": {
99+
"name": "name",
100+
"data_type": "nvarchar",
101+
"not_null": true,
102+
"is_array": false,
103+
"table": "authors"
104+
}
105+
}
106+
]
107+
}
108+
]

0 commit comments

Comments
 (0)