Skip to main content

sqruff_lib_dialects/
sqlite.rs

1use sqruff_lib_core::dialects::Dialect;
2use sqruff_lib_core::dialects::init::DialectKind;
3use sqruff_lib_core::dialects::syntax::SyntaxKind;
4use sqruff_lib_core::helpers::{Config, ToMatchable};
5use sqruff_lib_core::parser::grammar::anyof::{AnyNumberOf, one_of, optionally_bracketed};
6use sqruff_lib_core::parser::grammar::delimited::Delimited;
7use sqruff_lib_core::parser::grammar::sequence::{Bracketed, Sequence};
8use sqruff_lib_core::parser::grammar::{Anything, Nothing, Ref};
9use sqruff_lib_core::parser::lexer::Matcher;
10use sqruff_lib_core::parser::matchable::MatchableTrait;
11use sqruff_lib_core::parser::node_matcher::NodeMatcher;
12use sqruff_lib_core::parser::parsers::TypedParser;
13use sqruff_lib_core::parser::segments::meta::MetaSegment;
14use sqruff_lib_core::parser::types::ParseMode;
15
16use crate::sqlite_keywords::{RESERVED_KEYWORDS, UNRESERVED_KEYWORDS};
17use sqruff_lib_core::dialects::init::{DialectConfig, NullDialectConfig};
18use sqruff_lib_core::value::Value;
19
20/// Configuration for the SQLite dialect.
21pub type SQLiteDialectConfig = NullDialectConfig;
22
23pub fn dialect(config: Option<&Value>) -> Dialect {
24    // Parse and validate dialect configuration, falling back to defaults on failure
25    let _dialect_config: SQLiteDialectConfig = config
26        .map(SQLiteDialectConfig::from_value)
27        .unwrap_or_default();
28
29    raw_dialect().config(|dialect| dialect.expand())
30}
31
32pub fn raw_dialect() -> Dialect {
33    let sqlite_dialect = super::ansi::raw_dialect();
34    let mut sqlite_dialect = sqlite_dialect;
35    sqlite_dialect.name = DialectKind::Sqlite;
36
37    // Add lexer matchers for SQLite blob literals (X'...' or x'...')
38    // These must be inserted before single_quote to take precedence
39    sqlite_dialect.insert_lexer_matchers(
40        vec![Matcher::regex(
41            "bytes_single_quote",
42            r"[xX]'([^'\\]|\\.)*'",
43            SyntaxKind::BytesSingleQuote,
44        )],
45        "single_quote",
46    );
47
48    sqlite_dialect.sets_mut("reserved_keywords").clear();
49    sqlite_dialect
50        .sets_mut("reserved_keywords")
51        .extend(RESERVED_KEYWORDS);
52    sqlite_dialect.sets_mut("unreserved_keywords").clear();
53    sqlite_dialect
54        .sets_mut("unreserved_keywords")
55        .extend(UNRESERVED_KEYWORDS);
56
57    // SQLite supports CTEs with DML statements (INSERT, UPDATE, DELETE)
58    // since version 3.8.3. We add these to NonWithSelectableGrammar so
59    // WithCompoundStatementSegment can use them.
60    sqlite_dialect.add([(
61        "NonWithSelectableGrammar".into(),
62        one_of(vec![
63            Ref::new("SetExpressionSegment").to_matchable(),
64            optionally_bracketed(vec![Ref::new("SelectStatementSegment").to_matchable()])
65                .to_matchable(),
66            Ref::new("NonSetSelectableGrammar").to_matchable(),
67            Ref::new("UpdateStatementSegment").to_matchable(),
68            Ref::new("InsertStatementSegment").to_matchable(),
69            Ref::new("DeleteStatementSegment").to_matchable(),
70        ])
71        .to_matchable()
72        .into(),
73    )]);
74
75    sqlite_dialect.add([
76        // SQLite blob literal segment (X'...' or x'...')
77        (
78            "BytesQuotedLiteralSegment".into(),
79            TypedParser::new(SyntaxKind::BytesSingleQuote, SyntaxKind::BytesQuotedLiteral)
80                .to_matchable()
81                .into(),
82        ),
83        // Extend LiteralGrammar to include blob literals
84        (
85            "LiteralGrammar".into(),
86            sqlite_dialect
87                .grammar("LiteralGrammar")
88                .copy(
89                    Some(vec![Ref::new("BytesQuotedLiteralSegment").to_matchable()]),
90                    None,
91                    None,
92                    None,
93                    Vec::new(),
94                    false,
95                )
96                .into(),
97        ),
98        (
99            "ColumnConstraintDefaultGrammar".into(),
100            Ref::new("ExpressionSegment").to_matchable().into(),
101        ),
102        (
103            "BooleanBinaryOperatorGrammar".into(),
104            one_of(vec![
105                Ref::new("AndOperatorGrammar").to_matchable(),
106                Ref::new("OrOperatorGrammar").to_matchable(),
107                Ref::keyword("REGEXP").to_matchable(),
108            ])
109            .to_matchable()
110            .into(),
111        ),
112        (
113            "PrimaryKeyGrammar".into(),
114            Sequence::new(vec![
115                Ref::keyword("PRIMARY").to_matchable(),
116                Ref::keyword("KEY").to_matchable(),
117                Sequence::new(vec![Ref::keyword("AUTOINCREMENT").to_matchable()])
118                    .config(|config| {
119                        config.optional();
120                    })
121                    .to_matchable(),
122            ])
123            .to_matchable()
124            .into(),
125        ),
126        (
127            "TemporaryTransientGrammar".into(),
128            Ref::new("TemporaryGrammar").to_matchable().into(),
129        ),
130        (
131            "DateTimeLiteralGrammar".into(),
132            Sequence::new(vec![
133                one_of(vec![
134                    Ref::keyword("DATE").to_matchable(),
135                    Ref::keyword("DATETIME").to_matchable(),
136                ])
137                .to_matchable(),
138                TypedParser::new(SyntaxKind::SingleQuote, SyntaxKind::DateConstructorLiteral)
139                    .to_matchable(),
140            ])
141            .to_matchable()
142            .into(),
143        ),
144        (
145            "BaseExpressionElementGrammar".into(),
146            one_of(vec![
147                Ref::new("LiteralGrammar").to_matchable(),
148                Ref::new("BareFunctionSegment").to_matchable(),
149                Ref::new("FunctionSegment").to_matchable(),
150                Ref::new("ColumnReferenceSegment").to_matchable(),
151                Ref::new("ExpressionSegment").to_matchable(),
152                Sequence::new(vec![
153                    Ref::new("DatatypeSegment").to_matchable(),
154                    Ref::new("LiteralGrammar").to_matchable(),
155                ])
156                .to_matchable(),
157            ])
158            .to_matchable()
159            .into(),
160        ),
161        (
162            "AutoIncrementGrammar".into(),
163            Nothing::new().to_matchable().into(),
164        ),
165        (
166            "CommentClauseSegment".into(),
167            Nothing::new().to_matchable().into(),
168        ),
169        (
170            "IntervalExpressionSegment".into(),
171            Nothing::new().to_matchable().into(),
172        ),
173        (
174            "TimeZoneGrammar".into(),
175            Nothing::new().to_matchable().into(),
176        ),
177        (
178            "FetchClauseSegment".into(),
179            Nothing::new().to_matchable().into(),
180        ),
181        (
182            "TrimParametersGrammar".into(),
183            Nothing::new().to_matchable().into(),
184        ),
185        (
186            "LikeGrammar".into(),
187            Sequence::new(vec![Ref::keyword("LIKE").to_matchable()])
188                .to_matchable()
189                .into(),
190        ),
191        (
192            "OverlapsClauseSegment".into(),
193            Nothing::new().to_matchable().into(),
194        ),
195        (
196            "MLTableExpressionSegment".into(),
197            Nothing::new().to_matchable().into(),
198        ),
199        (
200            "MergeIntoLiteralGrammar".into(),
201            Nothing::new().to_matchable().into(),
202        ),
203        (
204            "SamplingExpressionSegment".into(),
205            Nothing::new().to_matchable().into(),
206        ),
207        (
208            "OrderByClauseTerminators".into(),
209            one_of(vec![
210                Ref::keyword("LIMIT").to_matchable(),
211                Ref::keyword("WINDOW").to_matchable(),
212                Ref::new("FrameClauseUnitGrammar").to_matchable(),
213            ])
214            .to_matchable()
215            .into(),
216        ),
217        (
218            "WhereClauseTerminatorGrammar".into(),
219            one_of(vec![
220                Ref::keyword("LIMIT").to_matchable(),
221                Sequence::new(vec![
222                    Ref::keyword("GROUP").to_matchable(),
223                    Ref::keyword("BY").to_matchable(),
224                ])
225                .to_matchable(),
226                Sequence::new(vec![
227                    Ref::keyword("ORDER").to_matchable(),
228                    Ref::keyword("BY").to_matchable(),
229                ])
230                .to_matchable(),
231                Ref::keyword("WINDOW").to_matchable(),
232            ])
233            .to_matchable()
234            .into(),
235        ),
236        (
237            "FromClauseTerminatorGrammar".into(),
238            one_of(vec![
239                Ref::keyword("WHERE").to_matchable(),
240                Ref::keyword("LIMIT").to_matchable(),
241                Sequence::new(vec![
242                    Ref::keyword("GROUP").to_matchable(),
243                    Ref::keyword("BY").to_matchable(),
244                ])
245                .to_matchable(),
246                Sequence::new(vec![
247                    Ref::keyword("ORDER").to_matchable(),
248                    Ref::keyword("BY").to_matchable(),
249                ])
250                .to_matchable(),
251                Ref::keyword("WINDOW").to_matchable(),
252                Ref::new("SetOperatorSegment").to_matchable(),
253                Ref::new("WithNoSchemaBindingClauseSegment").to_matchable(),
254                Ref::new("WithDataClauseSegment").to_matchable(),
255            ])
256            .to_matchable()
257            .into(),
258        ),
259        (
260            "GroupByClauseTerminatorGrammar".into(),
261            one_of(vec![
262                Sequence::new(vec![
263                    Ref::keyword("ORDER").to_matchable(),
264                    Ref::keyword("BY").to_matchable(),
265                ])
266                .to_matchable(),
267                Ref::keyword("LIMIT").to_matchable(),
268                Ref::keyword("HAVING").to_matchable(),
269                Ref::keyword("WINDOW").to_matchable(),
270            ])
271            .to_matchable()
272            .into(),
273        ),
274        (
275            "PostFunctionGrammar".into(),
276            Sequence::new(vec![
277                Ref::new("FilterClauseGrammar").optional().to_matchable(),
278                Ref::new("OverClauseSegment").to_matchable(),
279            ])
280            .to_matchable()
281            .into(),
282        ),
283        (
284            "IgnoreRespectNullsGrammar".into(),
285            Nothing::new().to_matchable().into(),
286        ),
287        (
288            "SelectClauseTerminatorGrammar".into(),
289            one_of(vec![
290                Ref::keyword("FROM").to_matchable(),
291                Ref::keyword("WHERE").to_matchable(),
292                Sequence::new(vec![
293                    Ref::keyword("ORDER").to_matchable(),
294                    Ref::keyword("BY").to_matchable(),
295                ])
296                .to_matchable(),
297                Ref::keyword("LIMIT").to_matchable(),
298                Ref::new("SetOperatorSegment").to_matchable(),
299            ])
300            .to_matchable()
301            .into(),
302        ),
303        (
304            "FunctionContentsGrammar".into(),
305            AnyNumberOf::new(vec![
306                Ref::new("ExpressionSegment").to_matchable(),
307                Sequence::new(vec![
308                    Ref::new("ExpressionSegment").to_matchable(),
309                    Ref::keyword("AS").to_matchable(),
310                    Ref::new("DatatypeSegment").to_matchable(),
311                ])
312                .to_matchable(),
313                Sequence::new(vec![
314                    Ref::new("TrimParametersGrammar").to_matchable(),
315                    Ref::new("ExpressionSegment")
316                        .optional()
317                        .exclude(Ref::keyword("FROM"))
318                        .config(|config| {
319                            config.exclude = Ref::keyword("FROM").to_matchable().into();
320                        })
321                        .to_matchable(),
322                    Ref::keyword("FROM").to_matchable(),
323                    Ref::new("ExpressionSegment").to_matchable(),
324                ])
325                .to_matchable(),
326                Sequence::new(vec![
327                    one_of(vec![
328                        Ref::new("DatetimeUnitSegment").to_matchable(),
329                        Ref::new("ExpressionSegment").to_matchable(),
330                    ])
331                    .to_matchable(),
332                    Ref::keyword("FROM").to_matchable(),
333                    Ref::new("ExpressionSegment").to_matchable(),
334                ])
335                .to_matchable(),
336                Sequence::new(vec![
337                    Ref::keyword("DISTINCT").optional().to_matchable(),
338                    one_of(vec![
339                        Ref::new("StarSegment").to_matchable(),
340                        Delimited::new(vec![
341                            Ref::new("FunctionContentsExpressionGrammar").to_matchable(),
342                        ])
343                        .to_matchable(),
344                    ])
345                    .to_matchable(),
346                ])
347                .to_matchable(),
348                Ref::new("OrderByClauseSegment").to_matchable(),
349                Sequence::new(vec![
350                    one_of(vec![
351                        Ref::new("QuotedLiteralSegment").to_matchable(),
352                        Ref::new("SingleIdentifierGrammar").to_matchable(),
353                        Ref::new("ColumnReferenceSegment").to_matchable(),
354                    ])
355                    .to_matchable(),
356                    Ref::keyword("IN").to_matchable(),
357                    one_of(vec![
358                        Ref::new("QuotedLiteralSegment").to_matchable(),
359                        Ref::new("SingleIdentifierGrammar").to_matchable(),
360                        Ref::new("ColumnReferenceSegment").to_matchable(),
361                    ])
362                    .to_matchable(),
363                ])
364                .to_matchable(),
365                Ref::new("IndexColumnDefinitionSegment").to_matchable(),
366            ])
367            .to_matchable()
368            .into(),
369        ),
370        (
371            "Expression_A_Unary_Operator_Grammar".into(),
372            one_of(vec![
373                Ref::new("SignedSegmentGrammar")
374                    .exclude(Sequence::new(vec![
375                        Ref::new("QualifiedNumericLiteralSegment").to_matchable(),
376                    ]))
377                    .config(|config| {
378                        config.exclude = Sequence::new(vec![
379                            Ref::new("QualifiedNumericLiteralSegment").to_matchable(),
380                        ])
381                        .to_matchable()
382                        .into();
383                    })
384                    .to_matchable(),
385                Ref::new("TildeSegment").to_matchable(),
386                Ref::new("NotOperatorGrammar").to_matchable(),
387            ])
388            .to_matchable()
389            .into(),
390        ),
391        (
392            "IsClauseGrammar".into(),
393            one_of(vec![
394                Ref::keyword("NULL").to_matchable(),
395                Ref::new("BooleanLiteralGrammar").to_matchable(),
396            ])
397            .to_matchable()
398            .into(),
399        ),
400    ]);
401    sqlite_dialect.add([(
402        "SetOperatorSegment".into(),
403        NodeMatcher::new(SyntaxKind::SetOperator, |_| {
404            one_of(vec![
405                Sequence::new(vec![
406                    Ref::keyword("UNION").to_matchable(),
407                    one_of(vec![
408                        Ref::keyword("DISTINCT").to_matchable(),
409                        Ref::keyword("ALL").to_matchable(),
410                    ])
411                    .config(|config| {
412                        config.optional();
413                    })
414                    .to_matchable(),
415                ])
416                .to_matchable(),
417                Sequence::new(vec![
418                    one_of(vec![
419                        Ref::keyword("INTERSECT").to_matchable(),
420                        Ref::keyword("EXCEPT").to_matchable(),
421                    ])
422                    .to_matchable(),
423                    Ref::keyword("ALL").optional().to_matchable(),
424                ])
425                .to_matchable(),
426            ])
427            .config(|config| {
428                config.exclude = Sequence::new(vec![
429                    Ref::keyword("EXCEPT").to_matchable(),
430                    Bracketed::new(vec![Anything::new().to_matchable()]).to_matchable(),
431                ])
432                .to_matchable()
433                .into();
434            })
435            .to_matchable()
436        })
437        .to_matchable()
438        .into(),
439    )]);
440
441    sqlite_dialect.replace_grammar(
442        "DatatypeSegment",
443        one_of(vec![
444            Sequence::new(vec![
445                Ref::keyword("DOUBLE").to_matchable(),
446                Ref::keyword("PRECISION").to_matchable(),
447            ])
448            .to_matchable(),
449            Sequence::new(vec![
450                Ref::keyword("UNSIGNED").to_matchable(),
451                Ref::keyword("BIG").to_matchable(),
452                Ref::keyword("INT").to_matchable(),
453            ])
454            .to_matchable(),
455            Sequence::new(vec![
456                one_of(vec![
457                    Sequence::new(vec![
458                        one_of(vec![
459                            Ref::keyword("VARYING").to_matchable(),
460                            Ref::keyword("NATIVE").to_matchable(),
461                        ])
462                        .to_matchable(),
463                        one_of(vec![Ref::keyword("CHARACTER").to_matchable()]).to_matchable(),
464                    ])
465                    .to_matchable(),
466                    Ref::new("DatatypeIdentifierSegment").to_matchable(),
467                ])
468                .to_matchable(),
469                Ref::new("BracketedArguments").optional().to_matchable(),
470            ])
471            .to_matchable(),
472        ])
473        .to_matchable(),
474    );
475    sqlite_dialect.add([(
476        "TableEndClauseSegment".into(),
477        NodeMatcher::new(SyntaxKind::TableEndClauseSegment, |_| {
478            Delimited::new(vec![
479                Sequence::new(vec![
480                    Ref::keyword("WITHOUT").to_matchable(),
481                    Ref::keyword("ROWID").to_matchable(),
482                ])
483                .to_matchable(),
484                Ref::keyword("STRICT").to_matchable(),
485            ])
486            .to_matchable()
487        })
488        .to_matchable()
489        .into(),
490    )]);
491
492    sqlite_dialect.replace_grammar(
493        "ValuesClauseSegment",
494        Sequence::new(vec![
495            Ref::keyword("VALUES").to_matchable(),
496            Delimited::new(vec![
497                Sequence::new(vec![
498                    Bracketed::new(vec![
499                        Delimited::new(vec![
500                            Ref::keyword("DEFAULT").to_matchable(),
501                            Ref::new("ExpressionSegment").to_matchable(),
502                        ])
503                        .to_matchable(),
504                    ])
505                    .config(|config| {
506                        config.parse_mode(ParseMode::Greedy);
507                    })
508                    .to_matchable(),
509                ])
510                .to_matchable(),
511            ])
512            .to_matchable(),
513        ])
514        .to_matchable(),
515    );
516    sqlite_dialect.add([
517        (
518            "IndexColumnDefinitionSegment".into(),
519            NodeMatcher::new(SyntaxKind::IndexColumnDefinition, |_| {
520                Sequence::new(vec![
521                    one_of(vec![
522                        Ref::new("SingleIdentifierGrammar").to_matchable(),
523                        Ref::new("ExpressionSegment").to_matchable(),
524                    ])
525                    .to_matchable(),
526                    one_of(vec![
527                        Ref::keyword("ASC").to_matchable(),
528                        Ref::keyword("DESC").to_matchable(),
529                    ])
530                    .config(|config| {
531                        config.optional();
532                    })
533                    .to_matchable(),
534                ])
535                .to_matchable()
536            })
537            .to_matchable()
538            .into(),
539        ),
540        (
541            "InsertStatementSegment".into(),
542            NodeMatcher::new(SyntaxKind::InsertStatement, |_| {
543                Sequence::new(vec![
544                    one_of(vec![
545                        Sequence::new(vec![
546                            Ref::keyword("INSERT").to_matchable(),
547                            Sequence::new(vec![
548                                Ref::keyword("OR").to_matchable(),
549                                one_of(vec![
550                                    Ref::keyword("ABORT").to_matchable(),
551                                    Ref::keyword("FAIL").to_matchable(),
552                                    Ref::keyword("IGNORE").to_matchable(),
553                                    Ref::keyword("REPLACE").to_matchable(),
554                                    Ref::keyword("ROLLBACK").to_matchable(),
555                                ])
556                                .to_matchable(),
557                            ])
558                            .config(|config| {
559                                config.optional();
560                            })
561                            .to_matchable(),
562                        ])
563                        .to_matchable(),
564                        Ref::keyword("REPLACE").to_matchable(),
565                    ])
566                    .to_matchable(),
567                    Ref::keyword("INTO").to_matchable(),
568                    Ref::new("TableReferenceSegment").to_matchable(),
569                    Ref::new("BracketedColumnReferenceListGrammar")
570                        .optional()
571                        .to_matchable(),
572                    one_of(vec![
573                        Ref::new("ValuesClauseSegment").to_matchable(),
574                        optionally_bracketed(vec![Ref::new("SelectableGrammar").to_matchable()])
575                            .to_matchable(),
576                        Ref::new("DefaultValuesGrammar").to_matchable(),
577                    ])
578                    .to_matchable(),
579                    Ref::new("ReturningClauseSegment").optional().to_matchable(),
580                ])
581                .to_matchable()
582            })
583            .to_matchable()
584            .into(),
585        ),
586        (
587            "DeleteStatementSegment".into(),
588            NodeMatcher::new(SyntaxKind::DeleteStatement, |_| {
589                Sequence::new(vec![
590                    Ref::keyword("DELETE").to_matchable(),
591                    Ref::new("FromClauseSegment").to_matchable(),
592                    Ref::new("WhereClauseSegment").optional().to_matchable(),
593                    Ref::new("ReturningClauseSegment").optional().to_matchable(),
594                ])
595                .to_matchable()
596            })
597            .to_matchable()
598            .into(),
599        ),
600        (
601            "UpdateStatementSegment".into(),
602            NodeMatcher::new(SyntaxKind::UpdateStatement, |_| {
603                Sequence::new(vec![
604                    Ref::keyword("UPDATE").to_matchable(),
605                    Ref::new("TableReferenceSegment").to_matchable(),
606                    Ref::new("AliasExpressionSegment")
607                        .exclude(Ref::keyword("SET"))
608                        .optional()
609                        .to_matchable(),
610                    Ref::new("SetClauseListSegment").to_matchable(),
611                    Ref::new("FromClauseSegment").optional().to_matchable(),
612                    Ref::new("WhereClauseSegment").optional().to_matchable(),
613                    Ref::new("ReturningClauseSegment").optional().to_matchable(),
614                ])
615                .to_matchable()
616            })
617            .to_matchable()
618            .into(),
619        ),
620    ]);
621
622    let column_constraint = sqlite_dialect
623        .grammar("ColumnConstraintSegment")
624        .match_grammar(&sqlite_dialect)
625        .unwrap()
626        .copy(
627            Some(vec![
628                one_of(vec![
629                    Ref::keyword("DEFERRABLE").to_matchable(),
630                    Sequence::new(vec![
631                        Ref::keyword("NOT").to_matchable(),
632                        Ref::keyword("DEFERRABLE").to_matchable(),
633                    ])
634                    .to_matchable(),
635                ])
636                .config(|config| {
637                    config.optional();
638                })
639                .to_matchable(),
640                one_of(vec![
641                    Sequence::new(vec![
642                        Ref::keyword("INITIALLY").to_matchable(),
643                        Ref::keyword("DEFERRED").to_matchable(),
644                    ])
645                    .to_matchable(),
646                    Sequence::new(vec![
647                        Ref::keyword("INITIALLY").to_matchable(),
648                        Ref::keyword("IMMEDIATE").to_matchable(),
649                    ])
650                    .to_matchable(),
651                ])
652                .config(|config| {
653                    config.optional();
654                })
655                .to_matchable(),
656            ]),
657            None,
658            None,
659            None,
660            Vec::new(),
661            false,
662        );
663    sqlite_dialect.replace_grammar("ColumnConstraintSegment", column_constraint);
664
665    sqlite_dialect.replace_grammar(
666        "TableConstraintSegment",
667        Sequence::new(vec![
668            Sequence::new(vec![
669                Ref::keyword("CONSTRAINT").to_matchable(),
670                Ref::new("ObjectReferenceSegment").to_matchable(),
671            ])
672            .config(|config| {
673                config.optional();
674            })
675            .to_matchable(),
676            one_of(vec![
677                Sequence::new(vec![
678                    Ref::keyword("CHECK").to_matchable(),
679                    Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()])
680                        .to_matchable(),
681                ])
682                .to_matchable(),
683                Sequence::new(vec![
684                    Ref::keyword("UNIQUE").to_matchable(),
685                    Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
686                ])
687                .to_matchable(),
688                Sequence::new(vec![
689                    Ref::new("PrimaryKeyGrammar").to_matchable(),
690                    Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
691                ])
692                .to_matchable(),
693                Sequence::new(vec![
694                    Ref::new("ForeignKeyGrammar").to_matchable(),
695                    Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
696                    Ref::new("ReferenceDefinitionGrammar").to_matchable(),
697                ])
698                .to_matchable(),
699            ])
700            .to_matchable(),
701            one_of(vec![
702                Ref::keyword("DEFERRABLE").to_matchable(),
703                Sequence::new(vec![
704                    Ref::keyword("NOT").to_matchable(),
705                    Ref::keyword("DEFERRABLE").to_matchable(),
706                ])
707                .to_matchable(),
708            ])
709            .config(|config| {
710                config.optional();
711            })
712            .to_matchable(),
713            one_of(vec![
714                Sequence::new(vec![
715                    Ref::keyword("INITIALLY").to_matchable(),
716                    Ref::keyword("DEFERRED").to_matchable(),
717                ])
718                .to_matchable(),
719                Sequence::new(vec![
720                    Ref::keyword("INITIALLY").to_matchable(),
721                    Ref::keyword("IMMEDIATE").to_matchable(),
722                ])
723                .to_matchable(),
724            ])
725            .config(|config| {
726                config.optional();
727            })
728            .to_matchable(),
729        ])
730        .to_matchable(),
731    );
732
733    sqlite_dialect.replace_grammar(
734        "TransactionStatementSegment",
735        Sequence::new(vec![
736            one_of(vec![
737                Ref::keyword("BEGIN").to_matchable(),
738                Ref::keyword("COMMIT").to_matchable(),
739                Ref::keyword("ROLLBACK").to_matchable(),
740                Ref::keyword("END").to_matchable(),
741            ])
742            .to_matchable(),
743            one_of(vec![Ref::keyword("TRANSACTION").to_matchable()])
744                .config(|config| {
745                    config.optional();
746                })
747                .to_matchable(),
748            Sequence::new(vec![
749                Ref::keyword("TO").to_matchable(),
750                Ref::keyword("SAVEPOINT").to_matchable(),
751                Ref::new("ObjectReferenceSegment").to_matchable(),
752            ])
753            .config(|config| {
754                config.optional();
755            })
756            .to_matchable(),
757        ])
758        .to_matchable(),
759    );
760
761    sqlite_dialect.add([(
762        "PragmaReferenceSegment".into(),
763        NodeMatcher::new(SyntaxKind::PragmaReference, |sqlite_dialect| {
764            sqlite_dialect
765                .grammar("ObjectReferenceSegment")
766                .match_grammar(sqlite_dialect)
767                .unwrap()
768        })
769        .to_matchable()
770        .into(),
771    )]);
772
773    sqlite_dialect.add([(
774        "PragmaStatementSegment".into(),
775        NodeMatcher::new(SyntaxKind::PragmaStatement, |_| {
776            let pragma_value = one_of(vec![
777                Ref::new("LiteralGrammar").to_matchable(),
778                Ref::new("BooleanLiteralGrammar").to_matchable(),
779                Ref::keyword("YES").to_matchable(),
780                Ref::keyword("NO").to_matchable(),
781                Ref::keyword("ON").to_matchable(),
782                Ref::keyword("OFF").to_matchable(),
783                Ref::keyword("NONE").to_matchable(),
784                Ref::keyword("FULL").to_matchable(),
785                Ref::keyword("INCREMENTAL").to_matchable(),
786                Ref::keyword("DELETE").to_matchable(),
787                Ref::keyword("TRUNCATE").to_matchable(),
788                Ref::keyword("PERSIST").to_matchable(),
789                Ref::keyword("MEMORY").to_matchable(),
790                Ref::keyword("WAL").to_matchable(),
791                Ref::keyword("NORMAL").to_matchable(),
792                Ref::keyword("EXCLUSIVE").to_matchable(),
793                Ref::keyword("FAST").to_matchable(),
794                Ref::keyword("EXTRA").to_matchable(),
795                Ref::keyword("DEFAULT").to_matchable(),
796                Ref::keyword("FILE").to_matchable(),
797                Ref::keyword("PASSIVE").to_matchable(),
798                Ref::keyword("RESTART").to_matchable(),
799                Ref::keyword("RESET").to_matchable(),
800            ]);
801
802            Sequence::new(vec![
803                Ref::keyword("PRAGMA").to_matchable(),
804                Ref::new("PragmaReferenceSegment").to_matchable(),
805                Bracketed::new(vec![pragma_value.clone().to_matchable()])
806                    .config(|config| {
807                        config.optional();
808                    })
809                    .to_matchable(),
810                Sequence::new(vec![
811                    Ref::new("EqualsSegment").to_matchable(),
812                    optionally_bracketed(vec![pragma_value.to_matchable()]).to_matchable(),
813                ])
814                .config(|config| {
815                    config.optional();
816                })
817                .to_matchable(),
818            ])
819            .to_matchable()
820        })
821        .to_matchable()
822        .into(),
823    )]);
824
825    sqlite_dialect.replace_grammar(
826        "CreateTriggerStatementSegment",
827        Sequence::new(vec![
828            Ref::keyword("CREATE").to_matchable(),
829            Ref::new("TemporaryGrammar").optional().to_matchable(),
830            Ref::keyword("TRIGGER").to_matchable(),
831            Ref::new("IfNotExistsGrammar").optional().to_matchable(),
832            Ref::new("TriggerReferenceSegment").to_matchable(),
833            one_of(vec![
834                Ref::keyword("BEFORE").to_matchable(),
835                Ref::keyword("AFTER").to_matchable(),
836                Sequence::new(vec![
837                    Ref::keyword("INSTEAD").to_matchable(),
838                    Ref::keyword("OF").to_matchable(),
839                ])
840                .to_matchable(),
841            ])
842            .config(|config| {
843                config.optional();
844            })
845            .to_matchable(),
846            one_of(vec![
847                Ref::keyword("DELETE").to_matchable(),
848                Ref::keyword("INSERT").to_matchable(),
849                Sequence::new(vec![
850                    Ref::keyword("UPDATE").to_matchable(),
851                    Sequence::new(vec![
852                        Ref::keyword("OF").to_matchable(),
853                        Delimited::new(vec![Ref::new("ColumnReferenceSegment").to_matchable()])
854                            .to_matchable(),
855                    ])
856                    .config(|config| {
857                        config.optional();
858                    })
859                    .to_matchable(),
860                ])
861                .to_matchable(),
862            ])
863            .to_matchable(),
864            Ref::keyword("ON").to_matchable(),
865            Ref::new("TableReferenceSegment").to_matchable(),
866            Sequence::new(vec![
867                Ref::keyword("FOR").to_matchable(),
868                Ref::keyword("EACH").to_matchable(),
869                Ref::keyword("ROW").to_matchable(),
870            ])
871            .config(|config| {
872                config.optional();
873            })
874            .to_matchable(),
875            Sequence::new(vec![
876                Ref::keyword("WHEN").to_matchable(),
877                Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()]).to_matchable(),
878            ])
879            .config(|config| {
880                config.optional();
881            })
882            .to_matchable(),
883            Ref::keyword("BEGIN").to_matchable(),
884            Delimited::new(vec![
885                Ref::new("UpdateStatementSegment").to_matchable(),
886                Ref::new("InsertStatementSegment").to_matchable(),
887                Ref::new("DeleteStatementSegment").to_matchable(),
888                Ref::new("SelectableGrammar").to_matchable(),
889            ])
890            .config(|config| {
891                config.delimiter(
892                    AnyNumberOf::new(vec![Ref::new("DelimiterGrammar").to_matchable()]).config(
893                        |config| {
894                            config.min_times = 1;
895                        },
896                    ),
897                );
898                config.allow_trailing = true;
899            })
900            .to_matchable(),
901            Ref::keyword("END").to_matchable(),
902        ])
903        .to_matchable(),
904    );
905    sqlite_dialect.add([(
906        "UnorderedSelectStatementSegment".into(),
907        NodeMatcher::new(SyntaxKind::SelectStatement, |_| {
908            Sequence::new(vec![
909                Ref::new("SelectClauseSegment").to_matchable(),
910                MetaSegment::dedent().to_matchable(),
911                Ref::new("FromClauseSegment").optional().to_matchable(),
912                Ref::new("WhereClauseSegment").optional().to_matchable(),
913                Ref::new("GroupByClauseSegment").optional().to_matchable(),
914                Ref::new("HavingClauseSegment").optional().to_matchable(),
915                Ref::new("OverlapsClauseSegment").optional().to_matchable(),
916                Ref::new("NamedWindowSegment").optional().to_matchable(),
917            ])
918            .to_matchable()
919        })
920        .to_matchable()
921        .into(),
922    )]);
923
924    sqlite_dialect.add([(
925        "SelectStatementSegment".into(),
926        NodeMatcher::new(SyntaxKind::SelectStatement, |sqlite_dialect| {
927            sqlite_dialect
928                .grammar("UnorderedSelectStatementSegment")
929                .match_grammar(sqlite_dialect)
930                .unwrap()
931                .copy(
932                    Some(vec![
933                        Ref::new("OrderByClauseSegment").optional().to_matchable(),
934                        Ref::new("FetchClauseSegment").optional().to_matchable(),
935                        Ref::new("LimitClauseSegment").optional().to_matchable(),
936                        Ref::new("NamedWindowSegment").optional().to_matchable(),
937                    ]),
938                    None,
939                    None,
940                    None,
941                    Vec::new(),
942                    false,
943                )
944        })
945        .to_matchable()
946        .into(),
947    )]);
948
949    sqlite_dialect.replace_grammar(
950        "CreateIndexStatementSegment",
951        Sequence::new(vec![
952            Ref::keyword("CREATE").to_matchable(),
953            Ref::keyword("UNIQUE").optional().to_matchable(),
954            Ref::keyword("INDEX").to_matchable(),
955            Ref::new("IfNotExistsGrammar").optional().to_matchable(),
956            Ref::new("IndexReferenceSegment").to_matchable(),
957            Ref::keyword("ON").to_matchable(),
958            Ref::new("TableReferenceSegment").to_matchable(),
959            Sequence::new(vec![
960                Bracketed::new(vec![
961                    Delimited::new(vec![
962                        Ref::new("IndexColumnDefinitionSegment").to_matchable(),
963                    ])
964                    .to_matchable(),
965                ])
966                .to_matchable(),
967            ])
968            .to_matchable(),
969            Ref::new("WhereClauseSegment").optional().to_matchable(),
970        ])
971        .to_matchable(),
972    );
973
974    sqlite_dialect.replace_grammar(
975        "StatementSegment",
976        one_of(vec![
977            Ref::new("AlterTableStatementSegment").to_matchable(),
978            Ref::new("CreateIndexStatementSegment").to_matchable(),
979            Ref::new("CreateTableStatementSegment").to_matchable(),
980            Ref::new("CreateTriggerStatementSegment").to_matchable(),
981            Ref::new("CreateViewStatementSegment").to_matchable(),
982            Ref::new("DeleteStatementSegment").to_matchable(),
983            Ref::new("DropIndexStatementSegment").to_matchable(),
984            Ref::new("DropTableStatementSegment").to_matchable(),
985            Ref::new("DropTriggerStatementSegment").to_matchable(),
986            Ref::new("DropViewStatementSegment").to_matchable(),
987            Ref::new("ExplainStatementSegment").to_matchable(),
988            Ref::new("InsertStatementSegment").to_matchable(),
989            Ref::new("PragmaStatementSegment").to_matchable(),
990            Ref::new("SelectableGrammar").to_matchable(),
991            Ref::new("TransactionStatementSegment").to_matchable(),
992            Ref::new("UpdateStatementSegment").to_matchable(),
993            Bracketed::new(vec![Ref::new("StatementSegment").to_matchable()]).to_matchable(),
994        ])
995        .to_matchable(),
996    );
997
998    sqlite_dialect.replace_grammar(
999        "AlterTableStatementSegment",
1000        Sequence::new(vec![
1001            Ref::keyword("ALTER").to_matchable(),
1002            Ref::keyword("TABLE").to_matchable(),
1003            Ref::new("TableReferenceSegment").to_matchable(),
1004            one_of(vec![
1005                Sequence::new(vec![
1006                    Ref::keyword("RENAME").to_matchable(),
1007                    Ref::keyword("TO").to_matchable(),
1008                    one_of(vec![
1009                        one_of(vec![
1010                            Ref::new("ParameterNameSegment").to_matchable(),
1011                            Ref::new("QuotedIdentifierSegment").to_matchable(),
1012                        ])
1013                        .to_matchable(),
1014                    ])
1015                    .to_matchable(),
1016                ])
1017                .to_matchable(),
1018                Sequence::new(vec![
1019                    Ref::keyword("RENAME").to_matchable(),
1020                    Ref::keyword("COLUMN").optional().to_matchable(),
1021                    Ref::new("ColumnReferenceSegment").to_matchable(),
1022                    Ref::keyword("TO").to_matchable(),
1023                    Ref::new("ColumnReferenceSegment").to_matchable(),
1024                ])
1025                .to_matchable(),
1026                Sequence::new(vec![
1027                    Ref::keyword("ADD").to_matchable(),
1028                    Ref::keyword("COLUMN").optional().to_matchable(),
1029                    Ref::new("ColumnDefinitionSegment").to_matchable(),
1030                ])
1031                .to_matchable(),
1032                Sequence::new(vec![
1033                    Ref::keyword("DROP").to_matchable(),
1034                    Ref::keyword("COLUMN").optional().to_matchable(),
1035                    Ref::new("ColumnReferenceSegment").to_matchable(),
1036                ])
1037                .to_matchable(),
1038            ])
1039            .to_matchable(),
1040        ])
1041        .to_matchable(),
1042    );
1043
1044    sqlite_dialect.add([(
1045        "ReturningClauseSegment".into(),
1046        Sequence::new(vec![
1047            Ref::keyword("RETURNING").to_matchable(),
1048            MetaSegment::indent().to_matchable(),
1049            one_of(vec![
1050                Ref::new("StarSegment").to_matchable(),
1051                Delimited::new(vec![
1052                    Sequence::new(vec![
1053                        Ref::new("ExpressionSegment").to_matchable(),
1054                        Ref::new("AsAliasExpressionSegment")
1055                            .optional()
1056                            .to_matchable(),
1057                    ])
1058                    .to_matchable(),
1059                ])
1060                .to_matchable(),
1061            ])
1062            .to_matchable(),
1063            MetaSegment::dedent().to_matchable(),
1064        ])
1065        .to_matchable()
1066        .into(),
1067    )]);
1068
1069    sqlite_dialect.add([(
1070        "AsAliasExpressionSegment".into(),
1071        NodeMatcher::new(SyntaxKind::AliasExpression, |_| {
1072            Sequence::new(vec![
1073                MetaSegment::indent().to_matchable(),
1074                Ref::keyword("AS").to_matchable(),
1075                Ref::new("SingleIdentifierGrammar").to_matchable(),
1076                MetaSegment::dedent().to_matchable(),
1077            ])
1078            .to_matchable()
1079        })
1080        .to_matchable()
1081        .into(),
1082    )]);
1083
1084    sqlite_dialect
1085}