Flake8-SQL is a flake8
plugin that looks for SQL queries and checks then against an
opinionated style. This style mostly follows SQL Style Guide, but differ in the two following
ways. Firstly alignement should be with the INTO
rather than
INSERT
keyword, i.e.
INSERT INTO table (columns) VALUES (values)
Secondly JOIN
should be aligned to the left of the river, i.e.
SELECT * FROM table1 JOIN table2 ON ...
All the SQL reserved keywords should be uppercase.
All the non SQL keywords should be snake_case, start with a letter and not end with an _. Due to a limitation snake_case is checks ensure that the word is lowercase.
Avoid using abbreviated keywords instead use the full length version.
Commas should be followed by whitespace, but not preceded.
Equals should be surrounded with whitespace.
The root keywords SELECT
, FROM
, INSERT
, VALUES
, DELETE
FROM
, WHERE
, UPDATE
, AND
, OR
and SET
should be
on separate lines (unless the entire query is on one line).
Semicolons must be at the end of the line.
The root keywords SELECT
, FROM
, INSERT
, VALUES
,
WHERE
, UPDATE
, AND
, OR
, JOIN
and SET
should be
right aligned i.e.
SELECT * FROM table
Any subquery should be aligned to the right of the river i.e.
SELECT * FROM table WHERE column IN (SELECT column FROM table)
Any tokens should be aligned to the right of the river i.e
SELECT column1, column2 FROM table
At times it is simpler to use a reserved keyword as an identifier than
go to the effort to avoid it. To allow for this set the
sql-excepted-names
option to a comma separated list of these
names.
String constants are sought out in the code and considered SQL if they
contain select from, insert into values, update set or delete from in
order. This may and is likely to lead to false positives, in which case
simply add # noqa
to have this plugin ignore the string.
F-Strings are formatted with the formatted values, {...}
, replaced
with the constant formatted_value
before being linted. This leads
to the error message referring to formatted_value
rather than what
was actually written.