Skip to content

Commit

Permalink
Added WHERE 1=1 rule
Browse files Browse the repository at this point in the history
- Prohibit the use of 'WHERE 1=1' in queries.
- Not fix compatible, but can be done in next iterations
  • Loading branch information
jordipuig37 committed Jun 7, 2024
1 parent 8cf9dea commit c346c9c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 13 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Ignore IDE files
.vscode
.idea
/.sqlfluff
**/.DS_Store

# Ignore Python cache and prebuilt things
Expand Down
5 changes: 5 additions & 0 deletions .sqlfluff
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[sqlfluff]
dialect = snowflake

[sqlfluff:layout:type:binary_operator]
line_position = trailing
16 changes: 13 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
[ ] enumerate rules

[ ] list all the rules we need to add to the plugin
[x] join and on in the same line -> disable default checks in the config
[x] join and on in the same line
[ ] disable default checks in the sqlfluff config
[ ] do not write 1 = 1 in where clause
[ ] subqueries
[ ] select * from final at the end of CTEs
[ ] name of object created in new indented line
[ ] the last ; should be in a separate line, when the last line of the statement is indented
...

[ ] setup the pipeline to turn this project into a PyPi (https://packaging.python.org/en/latest/tutorials/packaging-projects/)
[x] setup the pipeline to turn this project into a PyPI (https://packaging.python.org/en/latest/tutorials/packaging-projects/)
[ ] when first version is complete, release it to PyPI (not testpypi)
https://twine.readthedocs.io/en/latest/

[ ] setup flake8 and mypy linting for python code
[x] setup flake8 and mypy linting for python code

[ ] Curate and improve code samples, for the guideline .md and for testing
[ ] Think better and complete examples
[ ] Change fields and table names

[x] Check https://docs.sqlfluff.com/en/stable/gettingstarted.html#basic-usage
* https://docs.sqlfluff.com/en/stable/configuration.html#default-configuration

[ ] integrate the EasyQL plugin properly in the VS Code sqlfluff plugin
7 changes: 4 additions & 3 deletions src/sqlfluff_easy_ql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ def get_rules() -> List[Type[BaseRule]]:
"""
# i.e. we DO recommend importing here:
from sqlfluff_easy_ql.rules import (
Rule_EasyQL_L001, Rule_EasyQL_L002
Rule_EasyQL_L001,
Rule_EasyQL_L002,
Rule_EasyQL_L003
) # noqa: F811

return [Rule_EasyQL_L001, Rule_EasyQL_L002]
return [Rule_EasyQL_L001, Rule_EasyQL_L002, Rule_EasyQL_L003]


@hookimpl
Expand All @@ -40,5 +42,4 @@ def get_configs_info() -> dict:
"""Get rule config validations and descriptions."""
return {
"forbidden_columns": {"definition": "A list of column to forbid"},
"join_on_same_line": {"definition": "Whether this rule is on or not"}
}
4 changes: 2 additions & 2 deletions src/sqlfluff_easy_ql/easy_ql_default_config.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[sqlfluff:rules:EasyQL_L001]
forbidden_columns = bar, baaz

[sqlfluff:rules:EasyQL_L002]
join_on_same_line = True
[sqlfluff:layout:type:binary_operator]
line_position = trailing
29 changes: 25 additions & 4 deletions src/sqlfluff_easy_ql/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
BaseRule,
LintResult,
RuleContext,
LintResult,
LintFix
)
from sqlfluff.utils.functional import FunctionalContext, sp
from sqlfluff.core.parser import Sequence
from sqlfluff.core.rules.doc_decorators import document_fix_compatible
from sqlfluff.core.rules.crawlers import SegmentSeekerCrawler

from sqlfluff.core.rules.base import BaseRule, LintResult, RuleContext

# These two decorators allow plugins
# to be displayed in the sqlfluff docs
Expand Down Expand Up @@ -74,9 +79,6 @@ class Rule_EasyQL_L002(BaseRule):
def __init__(self, *args, **kwargs):
"""Overwrite __init__ to set config."""
super().__init__(*args, **kwargs)
with open("test_eval.txt", "a") as f:
f.write("logging init L2")
f.write("\n---\n")

def _eval(self, context: RuleContext):
"""We should not JOIN .. ON differnet lines."""
Expand All @@ -90,3 +92,22 @@ def _eval(self, context: RuleContext):
anchor=seg,
description=desc
)


class Rule_EasyQL_L003(BaseRule):
"""Prohibit the use of 'WHERE 1=1' in queries."""
groups = ("all",)
crawl_behaviour = SegmentSeekerCrawler({"where_clause"})
is_fix_compatible = False

def _eval(self, context: RuleContext):
text_segments = (
FunctionalContext(context)
.segment.children()
)

for idx, seg in enumerate(text_segments):
# Look for the pattern '1=1'
if "1=1" in seg.raw_upper.replace(" ", ""):
return LintResult(anchor=seg)
return None
53 changes: 53 additions & 0 deletions test/rules/test_cases/Rule_EasyQL_L003.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
rule: EasyQL_L003

no_where_used:
pass_str: |
SELECT
a,
sum(b)
FROM tbl
where_without_tautology:
pass_str: |
SELECT
a,
b,
c
FROM tbl
WHERE
a > b
where_1_eq_1_v1:
fail_str: |
SELECT
bar,
baaz
FROM tbl
WHERE 1 = 1
where_1_eq_1_v1:
fail_str: |
SELECT
bar,
baaz
FROM tbl
WHERE 1=1
AND bar=1
where_1_eq_1_v2:
fail_str: |
SELECT
bar,
baaz
FROM tbl
WHERE
1 = 1 AND bar = 1
where_1_eq_1_v3:
fail_str: |
SELECT
bar,
baaz
FROM tbl
WHERE
1 = 1

0 comments on commit c346c9c

Please sign in to comment.