Skip to content

Commit

Permalink
feat: add ability to parse returning in sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking committed Dec 15, 2024
1 parent 213ca02 commit a0ffd3a
Show file tree
Hide file tree
Showing 3 changed files with 299 additions and 1 deletion.
35 changes: 34 additions & 1 deletion crates/lib-dialects/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ pub fn raw_dialect() -> Dialect {
Ref::new("ValuesClauseSegment"),
optionally_bracketed(vec_of_erased![Ref::new("SelectableGrammar")]),
Ref::new("DefaultValuesGrammar")
])
]),
Ref::new("ReturningClauseSegment").optional()
])
.to_matchable(),
)
Expand Down Expand Up @@ -782,5 +783,37 @@ pub fn raw_dialect() -> Dialect {
.to_matchable(),
);

sqlite_dialect.add([(
"ReturningClauseSegment".into(),
Sequence::new(vec_of_erased![
Ref::keyword("RETURNING"),
one_of(vec_of_erased![
Ref::new("StarSegment"),
Delimited::new(vec_of_erased![Sequence::new(vec_of_erased![
Ref::new("ExpressionSegment"),
Ref::new("AsAliasExpressionSegment").optional(),
])])
])
])
.to_matchable()
.into(),
)]);

sqlite_dialect.add([(
"AsAliasExpressionSegment".into(),
NodeMatcher::new(
SyntaxKind::AliasExpression,
Sequence::new(vec_of_erased![
MetaSegment::indent(),
Ref::keyword("AS"),
Ref::new("SingleIdentifierGrammar"),
MetaSegment::dedent(),
])
.to_matchable(),
)
.to_matchable()
.into(),
)]);

sqlite_dialect
}
15 changes: 15 additions & 0 deletions crates/lib-dialects/test/fixtures/dialects/sqlite/insert.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ SELECT * FROM (SELECT
INSERT INTO t1 DEFAULT VALUES;

INSERT INTO t1 (a, b, c) DEFAULT VALUES;

INSERT INTO t1 (a, b, c) VALUES (1, 2, 3), (4, 5, 6)
RETURNING *;

INSERT INTO t1 (a, b, c) VALUES (1, 2, 3), (4, 5, 6)
RETURNING a;

INSERT INTO t1 (a, b, c) VALUES (1, 2, 3), (4, 5, 6)
RETURNING a AS b;

INSERT INTO t1 (a, b, c) VALUES (1, 2, 3), (4, 5, 6)
RETURNING a, b;

INSERT INTO t1 (a, b, c) VALUES (1, 2, 3), (4, 5, 6)
RETURNING a AS aa, b AS bb;
250 changes: 250 additions & 0 deletions crates/lib-dialects/test/fixtures/dialects/sqlite/insert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,253 @@ file:
- keyword: DEFAULT
- keyword: VALUES
- statement_terminator: ;
- statement:
- insert_statement:
- keyword: INSERT
- keyword: INTO
- table_reference:
- naked_identifier: t1
- bracketed:
- start_bracket: (
- column_reference:
- naked_identifier: a
- comma: ','
- column_reference:
- naked_identifier: b
- comma: ','
- column_reference:
- naked_identifier: c
- end_bracket: )
- values_clause:
- keyword: VALUES
- bracketed:
- start_bracket: (
- expression:
- numeric_literal: '1'
- comma: ','
- expression:
- numeric_literal: '2'
- comma: ','
- expression:
- numeric_literal: '3'
- end_bracket: )
- comma: ','
- bracketed:
- start_bracket: (
- expression:
- numeric_literal: '4'
- comma: ','
- expression:
- numeric_literal: '5'
- comma: ','
- expression:
- numeric_literal: '6'
- end_bracket: )
- keyword: RETURNING
- star: '*'
- statement_terminator: ;
- statement:
- insert_statement:
- keyword: INSERT
- keyword: INTO
- table_reference:
- naked_identifier: t1
- bracketed:
- start_bracket: (
- column_reference:
- naked_identifier: a
- comma: ','
- column_reference:
- naked_identifier: b
- comma: ','
- column_reference:
- naked_identifier: c
- end_bracket: )
- values_clause:
- keyword: VALUES
- bracketed:
- start_bracket: (
- expression:
- numeric_literal: '1'
- comma: ','
- expression:
- numeric_literal: '2'
- comma: ','
- expression:
- numeric_literal: '3'
- end_bracket: )
- comma: ','
- bracketed:
- start_bracket: (
- expression:
- numeric_literal: '4'
- comma: ','
- expression:
- numeric_literal: '5'
- comma: ','
- expression:
- numeric_literal: '6'
- end_bracket: )
- keyword: RETURNING
- expression:
- column_reference:
- naked_identifier: a
- statement_terminator: ;
- statement:
- insert_statement:
- keyword: INSERT
- keyword: INTO
- table_reference:
- naked_identifier: t1
- bracketed:
- start_bracket: (
- column_reference:
- naked_identifier: a
- comma: ','
- column_reference:
- naked_identifier: b
- comma: ','
- column_reference:
- naked_identifier: c
- end_bracket: )
- values_clause:
- keyword: VALUES
- bracketed:
- start_bracket: (
- expression:
- numeric_literal: '1'
- comma: ','
- expression:
- numeric_literal: '2'
- comma: ','
- expression:
- numeric_literal: '3'
- end_bracket: )
- comma: ','
- bracketed:
- start_bracket: (
- expression:
- numeric_literal: '4'
- comma: ','
- expression:
- numeric_literal: '5'
- comma: ','
- expression:
- numeric_literal: '6'
- end_bracket: )
- keyword: RETURNING
- expression:
- column_reference:
- naked_identifier: a
- alias_expression:
- keyword: AS
- naked_identifier: b
- statement_terminator: ;
- statement:
- insert_statement:
- keyword: INSERT
- keyword: INTO
- table_reference:
- naked_identifier: t1
- bracketed:
- start_bracket: (
- column_reference:
- naked_identifier: a
- comma: ','
- column_reference:
- naked_identifier: b
- comma: ','
- column_reference:
- naked_identifier: c
- end_bracket: )
- values_clause:
- keyword: VALUES
- bracketed:
- start_bracket: (
- expression:
- numeric_literal: '1'
- comma: ','
- expression:
- numeric_literal: '2'
- comma: ','
- expression:
- numeric_literal: '3'
- end_bracket: )
- comma: ','
- bracketed:
- start_bracket: (
- expression:
- numeric_literal: '4'
- comma: ','
- expression:
- numeric_literal: '5'
- comma: ','
- expression:
- numeric_literal: '6'
- end_bracket: )
- keyword: RETURNING
- expression:
- column_reference:
- naked_identifier: a
- comma: ','
- expression:
- column_reference:
- naked_identifier: b
- statement_terminator: ;
- statement:
- insert_statement:
- keyword: INSERT
- keyword: INTO
- table_reference:
- naked_identifier: t1
- bracketed:
- start_bracket: (
- column_reference:
- naked_identifier: a
- comma: ','
- column_reference:
- naked_identifier: b
- comma: ','
- column_reference:
- naked_identifier: c
- end_bracket: )
- values_clause:
- keyword: VALUES
- bracketed:
- start_bracket: (
- expression:
- numeric_literal: '1'
- comma: ','
- expression:
- numeric_literal: '2'
- comma: ','
- expression:
- numeric_literal: '3'
- end_bracket: )
- comma: ','
- bracketed:
- start_bracket: (
- expression:
- numeric_literal: '4'
- comma: ','
- expression:
- numeric_literal: '5'
- comma: ','
- expression:
- numeric_literal: '6'
- end_bracket: )
- keyword: RETURNING
- expression:
- column_reference:
- naked_identifier: a
- alias_expression:
- keyword: AS
- naked_identifier: aa
- comma: ','
- expression:
- column_reference:
- naked_identifier: b
- alias_expression:
- keyword: AS
- naked_identifier: bb
- statement_terminator: ;

0 comments on commit a0ffd3a

Please sign in to comment.