-
Notifications
You must be signed in to change notification settings - Fork 270
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
refactor/scoped session fix #151
Closed
+995
−852
Closed
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
1b671e2
refactor, fix scoped session
d23ed8a
remove unused import
b5f0300
fix logger
022a3da
remove test_database, rename param
a3b32c4
fix exception formatting
663e6bd
simplify _session, execute
f8afbcc
fix is_postgres
62b9982
abstract away engine creation
da613be
fix pylint errors
4a593dd
rename _parse_command
6fcf7ed
rename _bind_params
a4989eb
wrap flask wrapper
e8827bf
factor out sanitizer
bd330a7
promote paramstyle and placeholders to instance variables
db4cce1
pass token type/value around
88adfb9
rename methods
a4a8810
move escape_verbatim_colons to constructor
f618840
reorder methods
9a153fe
use else
456cea5
escape args and kwargs in constructor
713368f
reorder methods
2187d95
refactor logger
758910e
fix style
32db777
factor out utility functions
1f62283
factor out session utility functions
c0534e2
factor out sql utility functions
37fba4f
remove underscore from util functions
e06131c
fix style
86f981f
reorder imports
67a7f0c
remove manual tests
839b1f1
add statement tests, rollback on error in autocommit
9302a1e
move operation check to Statement
f6912d2
use statement factory
08a6636
use remove instead of rollback
7f9b77c
rename _sanitized_statement
944934f
abstract away catch_warnings
006657e
remove BEGIN and COMMIT
36fb280
Revert "remove BEGIN and COMMIT"
a6668c0
rename methods
1440ae5
add docstrings
9e9cb0b
update cs50 docstrings
789bb40
use assertAlmostEqual
05a4d9d
enable autocommit by default
7bf7e16
avoid using sessions
0674b7c
delete transaction connection on failure
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
simplify _session, execute
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,20 +24,20 @@ def __init__(self, dialect, sql, *args, **kwargs): | |
def _parse(self): | ||
formatted_statements = sqlparse.format(self._sql, strip_comments=True).strip() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstring |
||
parsed_statements = sqlparse.parse(formatted_statements) | ||
num_of_statements = len(parsed_statements) | ||
if num_of_statements == 0: | ||
statement_count = len(parsed_statements) | ||
if statement_count == 0: | ||
raise RuntimeError("missing statement") | ||
elif num_of_statements > 1: | ||
elif statement_count > 1: | ||
raise RuntimeError("too many statements at once") | ||
|
||
return parsed_statements[0] | ||
|
||
|
||
def _parse_command(self): | ||
for token in self._statement: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstring |
||
if token.ttype in [sqlparse.tokens.Keyword, sqlparse.tokens.Keyword.DDL, sqlparse.tokens.Keyword.DML]: | ||
if _is_command_token(token): | ||
token_value = token.value.upper() | ||
if token_value in ["BEGIN", "DELETE", "INSERT", "SELECT", "START", "UPDATE"]: | ||
if token_value in {"BEGIN", "DELETE", "INSERT", "SELECT", "START", "UPDATE"}: | ||
command = token_value | ||
break | ||
else: | ||
|
@@ -49,11 +49,11 @@ def _parse_command(self): | |
def _bind_params(self): | ||
tokens = self._tokenize() | ||
paramstyle, placeholders = self._parse_placeholders(tokens) | ||
if paramstyle in [Paramstyle.FORMAT, Paramstyle.QMARK]: | ||
if paramstyle in {Paramstyle.FORMAT, Paramstyle.QMARK}: | ||
tokens = self._bind_format_or_qmark(placeholders, tokens) | ||
elif paramstyle == Paramstyle.NUMERIC: | ||
tokens = self._bind_numeric(placeholders, tokens) | ||
if paramstyle in [Paramstyle.NAMED, Paramstyle.PYFORMAT]: | ||
if paramstyle in {Paramstyle.NAMED, Paramstyle.PYFORMAT}: | ||
tokens = self._bind_named_or_pyformat(placeholders, tokens) | ||
|
||
tokens = _escape_verbatim_colons(tokens) | ||
|
@@ -154,10 +154,10 @@ def _escape(self, value): | |
sqlalchemy.types.Boolean().literal_processor(self._dialect)(value)) | ||
|
||
if isinstance(value, bytes): | ||
if self._dialect.name in ["mysql", "sqlite"]: | ||
if self._dialect.name in {"mysql", "sqlite"}: | ||
# https://dev.mysql.com/doc/refman/8.0/en/hexadecimal-literals.html | ||
return sqlparse.sql.Token(sqlparse.tokens.Other, f"x'{value.hex()}'") | ||
if self._dialect.name in ["postgres", "postgresql"]: | ||
if self._dialect.name in {"postgres", "postgresql"}: | ||
# https://dba.stackexchange.com/a/203359 | ||
return sqlparse.sql.Token(sqlparse.tokens.Other, f"'\\x{value.hex()}'") | ||
|
||
|
@@ -235,7 +235,7 @@ def _parse_placeholder(token): | |
if token.value == "%s": | ||
return Paramstyle.FORMAT, None | ||
|
||
# E.g., %(foo) | ||
# E.g., %(foo)s | ||
matches = re.search(r"%\((\w+)\)s$", token.value) | ||
if matches: | ||
return Paramstyle.PYFORMAT, matches.group(1) | ||
|
@@ -253,6 +253,10 @@ def _escape_verbatim_colons(tokens): | |
return tokens | ||
|
||
|
||
def _is_command_token(token): | ||
return token.ttype in {sqlparse.tokens.Keyword, sqlparse.tokens.Keyword.DDL, sqlparse.tokens.Keyword.DML} | ||
|
||
|
||
def _is_string_literal(token): | ||
return token.ttype in [sqlparse.tokens.Literal.String, sqlparse.tokens.Literal.String.Single] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No \n