Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite: Support PRAGMA statements #4431

Merged
merged 4 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/sqlfluff/dialects/dialect_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,56 @@ class TransactionStatementSegment(ansi.TransactionStatementSegment):
)


class PragmaReferenceSegment(ansi.ObjectReferenceSegment):
"""A Pragma object."""

type = "pragma_reference"


class PragmaStatementSegment(BaseSegment):
"""A Pragma Statement.

As per https://www.sqlite.org/pragma.html
"""

type = "pragma_statement"

_pragma_value = OneOf(
Ref("LiteralGrammar"),
Ref("BooleanLiteralGrammar"),
"YES",
"NO",
"ON",
"OFF",
"NONE",
"FULL",
"INCREMENTAL",
"DELETE",
"TRUNCATE",
"PERSIST",
"MEMORY",
"WAL",
"NORMAL",
"EXCLUSIVE",
"FAST",
"EXTRA",
"DEFAULT",
"FILE",
"PASSIVE",
"RESTART",
"RESET",
)

match_grammar = Sequence(
"PRAGMA",
Ref("PragmaReferenceSegment"),
Bracketed(_pragma_value, optional=True),
Sequence(
Ref("EqualsSegment"), OptionallyBracketed(_pragma_value), optional=True
),
)


class StatementSegment(ansi.StatementSegment):
"""Overriding StatementSegment to allow for additional segment parsing."""

Expand All @@ -465,6 +515,7 @@ class StatementSegment(ansi.StatementSegment):
Ref("DropViewStatementSegment"),
Ref("ExplainStatementSegment"),
Ref("InsertStatementSegment"),
Ref("PragmaStatementSegment"),
Ref("SelectableGrammar"),
Ref("TransactionStatementSegment"),
Ref("UpdateStatementSegment"),
Expand Down
15 changes: 15 additions & 0 deletions src/sqlfluff/dialects/dialect_sqlite_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,19 @@
"DATE",
"DATETIME",
"ROWID",
"YES",
"OFF",
"NONE",
"INCREMENTAL",
"TRUNCATE",
"PERSIST",
"MEMORY",
"WAL",
"NORMAL",
"FAST",
"EXTRA",
"FILE",
"PASSIVE",
"RESTART",
"RESET",
]
29 changes: 29 additions & 0 deletions test/fixtures/dialects/sqlite/pragma.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PRAGMA analysis_limit = 7;

PRAGMA schema.application_id;

PRAGMA schema.auto_vacuum = INCREMENTAL;

PRAGMA automatic_index = TRUE;

PRAGMA schema.cache_size = -500;

PRAGMA collation_list;

PRAGMA data_store_directory = 'directory-name';

PRAGMA encoding = 'UTF-16be';

PRAGMA schema.foreign_key_check('table-name');

PRAGMA schema.journal_mode = WAL;

PRAGMA schema.locking_mode = NORMAL;

PRAGMA schema.secure_delete = FAST;

PRAGMA schema.synchronous = 0;

PRAGMA temp_store = DEFAULT;

PRAGMA schema.wal_checkpoint(FULL);
158 changes: 158 additions & 0 deletions test/fixtures/dialects/sqlite/pragma.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# YML test files are auto-generated from SQL files and should not be edited by
# hand. To help enforce this, the "hash" field in the file must match a hash
# computed by SQLFluff when running the tests. Please run
# `python test/generate_parse_fixture_yml.py` to generate them after adding or
# altering SQL files.
_hash: 8e46290d567297bf2f869781593433e524a9205419e5b20bf5bd978b665737f2
file:
- statement:
pragma_statement:
keyword: PRAGMA
pragma_reference:
naked_identifier: analysis_limit
comparison_operator:
raw_comparison_operator: '='
numeric_literal: '7'
- statement_terminator: ;
- statement:
pragma_statement:
keyword: PRAGMA
pragma_reference:
- naked_identifier: schema
- dot: .
- naked_identifier: application_id
- statement_terminator: ;
- statement:
pragma_statement:
- keyword: PRAGMA
- pragma_reference:
- naked_identifier: schema
- dot: .
- naked_identifier: auto_vacuum
- comparison_operator:
raw_comparison_operator: '='
- keyword: INCREMENTAL
- statement_terminator: ;
- statement:
pragma_statement:
keyword: PRAGMA
pragma_reference:
naked_identifier: automatic_index
comparison_operator:
raw_comparison_operator: '='
boolean_literal: 'TRUE'
- statement_terminator: ;
- statement:
pragma_statement:
keyword: PRAGMA
pragma_reference:
- naked_identifier: schema
- dot: .
- naked_identifier: cache_size
comparison_operator:
raw_comparison_operator: '='
numeric_literal:
sign_indicator: '-'
numeric_literal: '500'
- statement_terminator: ;
- statement:
pragma_statement:
keyword: PRAGMA
pragma_reference:
naked_identifier: collation_list
- statement_terminator: ;
- statement:
pragma_statement:
keyword: PRAGMA
pragma_reference:
naked_identifier: data_store_directory
comparison_operator:
raw_comparison_operator: '='
quoted_literal: "'directory-name'"
- statement_terminator: ;
- statement:
pragma_statement:
keyword: PRAGMA
pragma_reference:
naked_identifier: encoding
comparison_operator:
raw_comparison_operator: '='
quoted_literal: "'UTF-16be'"
- statement_terminator: ;
- statement:
pragma_statement:
keyword: PRAGMA
pragma_reference:
- naked_identifier: schema
- dot: .
- naked_identifier: foreign_key_check
bracketed:
start_bracket: (
quoted_literal: "'table-name'"
end_bracket: )
- statement_terminator: ;
- statement:
pragma_statement:
- keyword: PRAGMA
- pragma_reference:
- naked_identifier: schema
- dot: .
- naked_identifier: journal_mode
- comparison_operator:
raw_comparison_operator: '='
- keyword: WAL
- statement_terminator: ;
- statement:
pragma_statement:
- keyword: PRAGMA
- pragma_reference:
- naked_identifier: schema
- dot: .
- naked_identifier: locking_mode
- comparison_operator:
raw_comparison_operator: '='
- keyword: NORMAL
- statement_terminator: ;
- statement:
pragma_statement:
- keyword: PRAGMA
- pragma_reference:
- naked_identifier: schema
- dot: .
- naked_identifier: secure_delete
- comparison_operator:
raw_comparison_operator: '='
- keyword: FAST
- statement_terminator: ;
- statement:
pragma_statement:
keyword: PRAGMA
pragma_reference:
- naked_identifier: schema
- dot: .
- naked_identifier: synchronous
comparison_operator:
raw_comparison_operator: '='
numeric_literal: '0'
- statement_terminator: ;
- statement:
pragma_statement:
- keyword: PRAGMA
- pragma_reference:
naked_identifier: temp_store
- comparison_operator:
raw_comparison_operator: '='
- keyword: DEFAULT
- statement_terminator: ;
- statement:
pragma_statement:
keyword: PRAGMA
pragma_reference:
- naked_identifier: schema
- dot: .
- naked_identifier: wal_checkpoint
bracketed:
start_bracket: (
keyword: FULL
end_bracket: )
- statement_terminator: ;