File tree Expand file tree Collapse file tree 3 files changed +28
-4
lines changed Expand file tree Collapse file tree 3 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,13 @@ import { FrameClause } from "./WindowFrame";
14
14
import { Literal , StringLiteral } from "./Literal" ;
15
15
import { MysqlModifier } from "./dialects/Mysql" ;
16
16
import { ColumnDefinition } from "./CreateTable" ;
17
- import { PostgresqlOperator , PostgresqlOperatorExpr } from "./Node" ;
17
+ import {
18
+ DeleteStmt ,
19
+ InsertStmt ,
20
+ PostgresqlOperator ,
21
+ PostgresqlOperatorExpr ,
22
+ UpdateStmt ,
23
+ } from "./Node" ;
18
24
19
25
export type AllSelectNodes =
20
26
| CompoundSelectStmt
@@ -149,7 +155,8 @@ export interface CommonTableExpr extends BaseNode {
149
155
materializedKw ?:
150
156
| Keyword < "MATERIALIZED" >
151
157
| [ Keyword < "NOT" > , Keyword < "MATERIALIZED" > ] ;
152
- expr : ParenExpr < SubSelect > ;
158
+ // PostgreSQL supports UPDATE, DELETE, and INSERT in WITH clause
159
+ expr : ParenExpr < SubSelect | DeleteStmt | InsertStmt | UpdateStmt > ;
153
160
search ?: CteSearchClause ;
154
161
cycle ?: CteCycleClause ;
155
162
}
Original file line number Diff line number Diff line change @@ -191,7 +191,7 @@ common_table_expr
191
191
columns :(__ paren$list$column )?
192
192
asKw :(__ AS )
193
193
materialized :(__ cte_materialized )?
194
- select :(__ paren$compound_select_stmt )
194
+ expr :(__ paren$cte_expr )
195
195
search :(__ cte_search_clause )?
196
196
cycle :(__ cte_cycle_clause )? {
197
197
return loc ({
@@ -200,7 +200,7 @@ common_table_expr
200
200
columns: read (columns),
201
201
asKw: read (asKw),
202
202
materializedKw: read (materialized),
203
- expr: read (select ),
203
+ expr: read (expr ),
204
204
search: read (search),
205
205
cycle: read (cycle),
206
206
});
@@ -209,6 +209,10 @@ common_table_expr
209
209
cte_materialized
210
210
= kws :(NOT __ MATERIALIZED / MATERIALIZED ) { return read (kws); }
211
211
212
+ cte_expr
213
+ = compound_select_stmt
214
+ / stmt :(update_stmt / insert_stmt / delete_stmt ) & postgres { return stmt; }
215
+
212
216
cte_search_clause
213
217
= kw :(SEARCH __ (BREADTH / DEPTH ) __ FIRST __ BY __ ) columns :list$ident
214
218
setKw :(__ SET __ ) result :ident & postgres {
@@ -4944,6 +4948,7 @@ paren$__template__
4944
4948
4945
4949
paren$cast_arg = .
4946
4950
paren$compound_select_stmt = .
4951
+ paren$cte_expr = .
4947
4952
paren$empty_list = .
4948
4953
paren$entity_name = .
4949
4954
paren$expr = .
Original file line number Diff line number Diff line change @@ -60,5 +60,17 @@ describe("select WITH", () => {
60
60
SELECT * FROM search_graph
61
61
` ) ;
62
62
} ) ;
63
+
64
+ it ( "supports INSERT inside WITH" , ( ) => {
65
+ testWc ( `WITH t1 AS (INSERT INTO foo VALUES (1) RETURNING *) SELECT * FROM t1` ) ;
66
+ } ) ;
67
+
68
+ it ( "supports UPDATE inside WITH" , ( ) => {
69
+ testWc ( `WITH t1 AS (UPDATE foo SET bar = 1 RETURNING *) SELECT * FROM t1` ) ;
70
+ } ) ;
71
+
72
+ it ( "supports DELETE inside WITH" , ( ) => {
73
+ testWc ( `WITH t1 AS (DELETE FROM foo RETURNING *) SELECT * FROM t1` ) ;
74
+ } ) ;
63
75
} ) ;
64
76
} ) ;
You can’t perform that action at this time.
0 commit comments