File tree Expand file tree Collapse file tree 2 files changed +72
-2
lines changed
crates/pgt_statement_splitter/src Expand file tree Collapse file tree 2 files changed +72
-2
lines changed Original file line number Diff line number Diff line change @@ -92,7 +92,7 @@ mod tests {
92
92
assert_eq ! (
93
93
self . result. ranges. len( ) ,
94
94
expected. len( ) ,
95
- "Expected {} statements for input {}, got {}: {:?}" ,
95
+ "Expected {} statements for input\n {} \n got {}:\n {:?}" ,
96
96
expected. len( ) ,
97
97
self . input,
98
98
self . result. ranges. len( ) ,
@@ -133,6 +133,40 @@ mod tests {
133
133
}
134
134
}
135
135
136
+ #[ test]
137
+ fn begin_commit ( ) {
138
+ Tester :: from (
139
+ "BEGIN;
140
+ SELECT 1;
141
+ COMMIT;" ,
142
+ )
143
+ . expect_statements ( vec ! [ "BEGIN;" , "SELECT 1;" , "COMMIT;" ] ) ;
144
+ }
145
+
146
+ #[ test]
147
+ fn begin_atomic ( ) {
148
+ Tester :: from (
149
+ "CREATE OR REPLACE FUNCTION public.test_fn(some_in TEXT)
150
+ RETURNS TEXT
151
+ LANGUAGE sql
152
+ IMMUTABLE
153
+ STRICT
154
+ BEGIN ATOMIC
155
+ SELECT $1 || 'foo';
156
+ END;" ,
157
+ )
158
+ . expect_statements ( vec ! [
159
+ "CREATE OR REPLACE FUNCTION public.test_fn(some_in TEXT)
160
+ RETURNS TEXT
161
+ LANGUAGE sql
162
+ IMMUTABLE
163
+ STRICT
164
+ BEGIN ATOMIC
165
+ SELECT $1 || 'foo';
166
+ END;" ,
167
+ ] ) ;
168
+ }
169
+
136
170
#[ test]
137
171
fn ts_with_timezone ( ) {
138
172
Tester :: from ( "alter table foo add column bar timestamp with time zone;" ) . expect_statements (
Original file line number Diff line number Diff line change @@ -58,6 +58,33 @@ pub(crate) fn statement(p: &mut Splitter) {
58
58
p. close_stmt ( ) ;
59
59
}
60
60
61
+ pub ( crate ) fn begin_end ( p : & mut Splitter ) {
62
+ p. expect ( SyntaxKind :: BEGIN_KW ) ;
63
+
64
+ let mut depth = 1 ;
65
+
66
+ loop {
67
+ match p. current ( ) {
68
+ SyntaxKind :: BEGIN_KW => {
69
+ p. advance ( ) ;
70
+ depth += 1 ;
71
+ }
72
+ SyntaxKind :: END_KW | SyntaxKind :: EOF => {
73
+ if p. current ( ) == SyntaxKind :: END_KW {
74
+ p. advance ( ) ;
75
+ }
76
+ depth -= 1 ;
77
+ if depth == 0 {
78
+ break ;
79
+ }
80
+ }
81
+ _ => {
82
+ p. advance ( ) ;
83
+ }
84
+ }
85
+ }
86
+ }
87
+
61
88
pub ( crate ) fn parenthesis ( p : & mut Splitter ) {
62
89
p. expect ( SyntaxKind :: L_PAREN ) ;
63
90
@@ -163,6 +190,14 @@ pub(crate) fn unknown(p: &mut Splitter, exclude: &[SyntaxKind]) {
163
190
SyntaxKind :: L_PAREN => {
164
191
parenthesis ( p) ;
165
192
}
193
+ SyntaxKind :: BEGIN_KW => {
194
+ if p. look_ahead ( true ) != SyntaxKind :: SEMICOLON {
195
+ // BEGIN; should be treated as a statement terminator
196
+ begin_end ( p) ;
197
+ } else {
198
+ p. advance ( ) ;
199
+ }
200
+ }
166
201
t => match at_statement_start ( t, exclude) {
167
202
Some ( SyntaxKind :: SELECT_KW ) => {
168
203
let prev = p. look_back ( true ) ;
@@ -188,6 +223,8 @@ pub(crate) fn unknown(p: &mut Splitter, exclude: &[SyntaxKind]) {
188
223
// for revoke
189
224
SyntaxKind :: REVOKE_KW ,
190
225
SyntaxKind :: COMMA ,
226
+ // for BEGIN ATOMIC
227
+ SyntaxKind :: ATOMIC_KW ,
191
228
]
192
229
. iter ( )
193
230
. all ( |x| Some ( x) != prev. as_ref ( ) )
@@ -255,7 +292,6 @@ pub(crate) fn unknown(p: &mut Splitter, exclude: &[SyntaxKind]) {
255
292
}
256
293
p. advance ( ) ;
257
294
}
258
-
259
295
Some ( SyntaxKind :: CREATE_KW ) => {
260
296
let prev = p. look_back ( true ) ;
261
297
if [
You can’t perform that action at this time.
0 commit comments