Skip to content

Commit ed2e79a

Browse files
authored
handle return for delete and update (#229)
* delete and update return handling * revert * fmt
1 parent 17d44aa commit ed2e79a

File tree

7 files changed

+293
-3
lines changed

7 files changed

+293
-3
lines changed

src/ts_generator/sql_parser/translate_stmt.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,17 @@ pub async fn translate_stmt(
5050
Statement::Delete(delete) => match &delete.from {
5151
FromTable::WithFromKeyword(from) => {
5252
let table_name = get_default_table(from);
53-
let table_name = table_name.as_str();
53+
let table_name_str = table_name.as_str();
5454
let selection = delete.selection.to_owned().unwrap();
55-
translate_delete(ts_query, &selection, table_name, db_conn).await?;
55+
translate_delete(ts_query, &selection, table_name_str, db_conn).await?;
56+
57+
// Handle RETURNING clause if present
58+
if delete.returning.is_some() {
59+
let returning = delete.returning.clone().unwrap();
60+
let query_for_logging = sql_statement.to_string();
61+
let query_for_logging_str = &query_for_logging.as_str();
62+
translate_insert_returning(ts_query, &returning, table_name_str, db_conn, query_for_logging_str).await?;
63+
}
5664
}
5765
FromTable::WithoutKeyword(_) => Err(TsGeneratorError::FromWithoutKeyword(sql_statement.to_string()))?,
5866
},
@@ -61,7 +69,7 @@ pub async fn translate_stmt(
6169
assignments,
6270
from,
6371
selection,
64-
returning: _,
72+
returning,
6573
or: _,
6674
limit: _,
6775
} => {
@@ -79,6 +87,24 @@ pub async fn translate_stmt(
7987
};
8088

8189
translate_update(ts_query, table, assignments, &from_table, selection, db_conn).await?;
90+
91+
// Handle RETURNING clause if present
92+
if returning.is_some() {
93+
let returning_items = returning.clone().unwrap();
94+
// Extract table name from TableWithJoins
95+
let table_name = get_default_table(&vec![table.clone()]);
96+
let table_name_str = table_name.as_str();
97+
let query_for_logging = sql_statement.to_string();
98+
let query_for_logging_str = &query_for_logging.as_str();
99+
translate_insert_returning(
100+
ts_query,
101+
&returning_items,
102+
table_name_str,
103+
db_conn,
104+
query_for_logging_str,
105+
)
106+
.await?;
107+
}
82108
}
83109
_ => {}
84110
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export type DeleteReturningAllParams = [number];
2+
3+
export interface IDeleteReturningAllResult {
4+
flavorText: string | null;
5+
id: number;
6+
inventoryId: number | null;
7+
name: string;
8+
rarity: string | null;
9+
}
10+
11+
export interface IDeleteReturningAllQuery {
12+
params: DeleteReturningAllParams;
13+
result: IDeleteReturningAllResult;
14+
}
15+
16+
export type DeleteReturningSpecificParams = [number];
17+
18+
export interface IDeleteReturningSpecificResult {
19+
id: number;
20+
name: string;
21+
}
22+
23+
export interface IDeleteReturningSpecificQuery {
24+
params: DeleteReturningSpecificParams;
25+
result: IDeleteReturningSpecificResult;
26+
}
27+
28+
export type DeleteReturningWithAliasParams = [number];
29+
30+
export interface IDeleteReturningWithAliasResult {
31+
deletedId: number;
32+
deletedName: string;
33+
}
34+
35+
export interface IDeleteReturningWithAliasQuery {
36+
params: DeleteReturningWithAliasParams;
37+
result: IDeleteReturningWithAliasResult;
38+
}
39+
40+
export type DeleteReturningExpressionParams = [number];
41+
42+
export interface IDeleteReturningExpressionResult {
43+
id: number;
44+
upperName: string;
45+
}
46+
47+
export interface IDeleteReturningExpressionQuery {
48+
params: DeleteReturningExpressionParams;
49+
result: IDeleteReturningExpressionResult;
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export type DeleteReturningAllParams = [number];
2+
3+
export interface IDeleteReturningAllResult {
4+
flavorText: string | null;
5+
id: number;
6+
inventoryId: number | null;
7+
name: string;
8+
rarity: string | null;
9+
}
10+
11+
export interface IDeleteReturningAllQuery {
12+
params: DeleteReturningAllParams;
13+
result: IDeleteReturningAllResult;
14+
}
15+
16+
export type DeleteReturningSpecificParams = [number];
17+
18+
export interface IDeleteReturningSpecificResult {
19+
id: number;
20+
name: string;
21+
}
22+
23+
export interface IDeleteReturningSpecificQuery {
24+
params: DeleteReturningSpecificParams;
25+
result: IDeleteReturningSpecificResult;
26+
}
27+
28+
export type DeleteReturningWithAliasParams = [number];
29+
30+
export interface IDeleteReturningWithAliasResult {
31+
deletedId: number;
32+
deletedName: string;
33+
}
34+
35+
export interface IDeleteReturningWithAliasQuery {
36+
params: DeleteReturningWithAliasParams;
37+
result: IDeleteReturningWithAliasResult;
38+
}
39+
40+
export type DeleteReturningExpressionParams = [number];
41+
42+
export interface IDeleteReturningExpressionResult {
43+
id: number;
44+
upperName: string;
45+
}
46+
47+
export interface IDeleteReturningExpressionQuery {
48+
params: DeleteReturningExpressionParams;
49+
result: IDeleteReturningExpressionResult;
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { sql } from 'sqlx-ts'
2+
3+
// Issue #226: DELETE with RETURNING clause should generate types
4+
const deleteReturningAll = sql`
5+
-- @name: delete returning all
6+
DELETE FROM items
7+
WHERE id = $1
8+
RETURNING *
9+
`
10+
11+
const deleteReturningSpecific = sql`
12+
-- @name: delete returning specific
13+
DELETE FROM items
14+
WHERE id = $1
15+
RETURNING id, name
16+
`
17+
18+
const deleteReturningWithAlias = sql`
19+
-- @name: delete returning with alias
20+
DELETE FROM items
21+
WHERE id = $1
22+
RETURNING id AS deleted_id, name AS deleted_name
23+
`
24+
25+
const deleteReturningExpression = sql`
26+
-- @name: delete returning expression
27+
DELETE FROM items
28+
WHERE id = $1
29+
RETURNING id, UPPER(name) AS upper_name
30+
`
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export type UpdateReturningAllParams = [string, number];
2+
3+
export interface IUpdateReturningAllResult {
4+
flavorText: string | null;
5+
id: number;
6+
inventoryId: number | null;
7+
name: string;
8+
rarity: string | null;
9+
}
10+
11+
export interface IUpdateReturningAllQuery {
12+
params: UpdateReturningAllParams;
13+
result: IUpdateReturningAllResult;
14+
}
15+
16+
export type UpdateReturningSpecificParams = [string, number];
17+
18+
export interface IUpdateReturningSpecificResult {
19+
id: number;
20+
name: string;
21+
}
22+
23+
export interface IUpdateReturningSpecificQuery {
24+
params: UpdateReturningSpecificParams;
25+
result: IUpdateReturningSpecificResult;
26+
}
27+
28+
export type UpdateReturningWithAliasParams = [string, number];
29+
30+
export interface IUpdateReturningWithAliasResult {
31+
updatedId: number;
32+
updatedName: string;
33+
}
34+
35+
export interface IUpdateReturningWithAliasQuery {
36+
params: UpdateReturningWithAliasParams;
37+
result: IUpdateReturningWithAliasResult;
38+
}
39+
40+
export type UpdateReturningExpressionParams = [string, number];
41+
42+
export interface IUpdateReturningExpressionResult {
43+
id: number;
44+
lowerName: string;
45+
}
46+
47+
export interface IUpdateReturningExpressionQuery {
48+
params: UpdateReturningExpressionParams;
49+
result: IUpdateReturningExpressionResult;
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export type UpdateReturningAllParams = [string, number];
2+
3+
export interface IUpdateReturningAllResult {
4+
flavorText: string | null;
5+
id: number;
6+
inventoryId: number | null;
7+
name: string;
8+
rarity: string | null;
9+
}
10+
11+
export interface IUpdateReturningAllQuery {
12+
params: UpdateReturningAllParams;
13+
result: IUpdateReturningAllResult;
14+
}
15+
16+
export type UpdateReturningSpecificParams = [string, number];
17+
18+
export interface IUpdateReturningSpecificResult {
19+
id: number;
20+
name: string;
21+
}
22+
23+
export interface IUpdateReturningSpecificQuery {
24+
params: UpdateReturningSpecificParams;
25+
result: IUpdateReturningSpecificResult;
26+
}
27+
28+
export type UpdateReturningWithAliasParams = [string, number];
29+
30+
export interface IUpdateReturningWithAliasResult {
31+
updatedId: number;
32+
updatedName: string;
33+
}
34+
35+
export interface IUpdateReturningWithAliasQuery {
36+
params: UpdateReturningWithAliasParams;
37+
result: IUpdateReturningWithAliasResult;
38+
}
39+
40+
export type UpdateReturningExpressionParams = [string, number];
41+
42+
export interface IUpdateReturningExpressionResult {
43+
id: number;
44+
lowerName: string;
45+
}
46+
47+
export interface IUpdateReturningExpressionQuery {
48+
params: UpdateReturningExpressionParams;
49+
result: IUpdateReturningExpressionResult;
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { sql } from 'sqlx-ts'
2+
3+
// Issue #226: UPDATE with RETURNING clause should generate types
4+
const updateReturningAll = sql`
5+
-- @name: update returning all
6+
UPDATE items
7+
SET name = $1
8+
WHERE id = $2
9+
RETURNING *
10+
`
11+
12+
const updateReturningSpecific = sql`
13+
-- @name: update returning specific
14+
UPDATE items
15+
SET name = $1
16+
WHERE id = $2
17+
RETURNING id, name
18+
`
19+
20+
const updateReturningWithAlias = sql`
21+
-- @name: update returning with alias
22+
UPDATE items
23+
SET name = $1
24+
WHERE id = $2
25+
RETURNING id AS updated_id, name AS updated_name
26+
`
27+
28+
const updateReturningExpression = sql`
29+
-- @name: update returning expression
30+
UPDATE items
31+
SET name = $1
32+
WHERE id = $2
33+
RETURNING id, LOWER(name) AS lower_name
34+
`

0 commit comments

Comments
 (0)