-
Notifications
You must be signed in to change notification settings - Fork 279
Adds support for 'CREATE VIEW' statement #176
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,7 @@ def __init__(self, url, **kwargs): | |
import sqlalchemy | ||
import sqlalchemy.orm | ||
import threading | ||
|
||
# Temporary fix for missing sqlite3 module on the buildpack stack | ||
try: | ||
import sqlite3 | ||
|
@@ -149,15 +149,15 @@ def execute(self, sql, *args, **kwargs): | |
if len(args) > 0 and len(kwargs) > 0: | ||
raise RuntimeError("cannot pass both positional and named parameters") | ||
|
||
# Infer command from (unflattened) statement | ||
for token in statements[0]: | ||
if token.ttype in [sqlparse.tokens.Keyword, sqlparse.tokens.Keyword.DDL, sqlparse.tokens.Keyword.DML]: | ||
token_value = token.value.upper() | ||
if token_value in ["BEGIN", "DELETE", "INSERT", "SELECT", "START", "UPDATE"]: | ||
command = token_value | ||
break | ||
else: | ||
command = None | ||
# Infer command from flattened statement to a single string separated by spaces | ||
full_statement = ' '.join(str(token) for token in statements[0].tokens if token.ttype in [sqlparse.tokens.Keyword, sqlparse.tokens.Keyword.DDL, sqlparse.tokens.Keyword.DML]) | ||
full_statement = full_statement.upper() | ||
|
||
# set of possible commands | ||
commands = {"BEGIN", "CREATE VIEW", "DELETE", "INSERT", "SELECT", "START", "UPDATE"} | ||
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. Will https://github.com/cs50/python-cs50/pull/176/files#diff-d12a5fb846ae94c77fc681c21dd86952fa2213c659ec05fe213dbe4e47468056R139 normalize whitespace, even if user provides, e.g., |
||
|
||
# check if the full_statement starts with any command | ||
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.
|
||
command = next((cmd for cmd in commands if full_statement.startswith(cmd)), None) | ||
|
||
# Flatten statement | ||
tokens = list(statements[0].flatten()) | ||
|
@@ -393,6 +393,10 @@ def teardown_appcontext(exception): | |
elif command in ["DELETE", "UPDATE"]: | ||
ret = result.rowcount | ||
|
||
# If CREATE VIEW, return True | ||
elif command == "CREATE VIEW": | ||
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. Why special-case |
||
ret = True | ||
|
||
# If constraint violated | ||
except sqlalchemy.exc.IntegrityError as e: | ||
self._logger.error(termcolor.colored(_statement, "red")) | ||
|
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.
Seems unnecessary to flatten whole statement if you only need first 1–2 tokens?