Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
surister committed Jun 4, 2024
1 parent 122b80f commit 95bffb7
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions cratedb_sqlparse_py/cratedb_sqlparse/parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from typing import List

from antlr4 import CommonTokenStream, InputStream, Token, RecognitionException
from antlr4 import CommonTokenStream, InputStream, RecognitionException, Token
from antlr4.error.ErrorListener import ErrorListener

from cratedb_sqlparse.generated_parser.SqlBaseLexer import SqlBaseLexer
Expand Down Expand Up @@ -44,17 +44,19 @@ def error_message(self):
@property
def original_query_with_error_marked(self):
query = self.offending_token.source[1].strdata
offending_token_text: str = query[self.offending_token.start: self.offending_token.stop + 1]
query_lines: list = query.split('\n')
offending_token_text: str = query[self.offending_token.start : self.offending_token.stop + 1]
query_lines: list = query.split("\n")

offending_line: str = query_lines[self.line - 1]

# White spaces from the beginning of the offending line to the offending text, so the '^'
# chars are correctly placed below the offending token.
newline_offset = offending_line.index(offending_token_text)
newline = offending_line + '\n' + (
' ' * newline_offset + '^' * (
self.offending_token.stop - self.offending_token.start + 1))
newline = (
offending_line
+ "\n"
+ (" " * newline_offset + "^" * (self.offending_token.stop - self.offending_token.start + 1))
)

query_lines[self.line - 1] = newline

Expand All @@ -70,7 +72,7 @@ def line(self):
return self.offending_token.line

def __repr__(self):
return f'{type(self.e).__qualname__}'
return f"{type(self.e).__qualname__}"

def __str__(self):
return repr(self)
Expand All @@ -94,7 +96,7 @@ def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
msg=msg,
offending_token=offendingSymbol,
e=e,
query=e.ctx.parser.getTokenStream().getText(e.ctx.start, e.offendingToken.tokenIndex)
query=e.ctx.parser.getTokenStream().getText(e.ctx.start, e.offendingToken.tokenIndex),
)
raise error

Expand Down Expand Up @@ -148,8 +150,7 @@ def query(self) -> str:
"""
Returns the query, comments and ';' are not included.
"""
return self.ctx.parser.getTokenStream().getText(start=self.ctx.start,
stop=self.ctx.stop)
return self.ctx.parser.getTokenStream().getText(start=self.ctx.start, stop=self.ctx.stop)

@property
def type(self):
Expand All @@ -167,8 +168,8 @@ def find_suitable_error(statement, errors):
# We clean the error_query of ';' and spaces because ironically,
# we can get the full query in the error handler but not in the context.
error_query = error.query
if error_query.endswith(';'):
error_query = error_query[:len(error_query) - 1]
if error_query.endswith(";"):
error_query = error_query[: len(error_query) - 1]

if error_query.lstrip().rstrip() == statement.query:
statement.exception = error
Expand All @@ -192,8 +193,7 @@ def sqlparse(query: str, raise_exception: bool = False) -> List[Statement]:
tree = parser.statements()

statements_context: list[SqlBaseParser.StatementContext] = list(
filter(lambda children: isinstance(children, SqlBaseParser.StatementContext),
tree.children)
filter(lambda children: isinstance(children, SqlBaseParser.StatementContext), tree.children)
)

statements = []
Expand Down Expand Up @@ -222,7 +222,7 @@ def sqlparse(query: str, raise_exception: bool = False) -> List[Statement]:

if len(error_listener.errors) > 1:
logging.error(
'Could not match errors to queries, too much ambiguity, open an issue with this error and the query.'
"Could not match errors to queries, too much ambiguity, open an issue with this error and the query."
)

return statements

0 comments on commit 95bffb7

Please sign in to comment.