Skip to content

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

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
package_dir={"": "src"},
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="9.2.5"
version="9.2.6"
)
24 changes: 14 additions & 10 deletions src/cs50/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Copy link
Member

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?

full_statement = full_statement.upper()

# set of possible commands
commands = {"BEGIN", "CREATE VIEW", "DELETE", "INSERT", "SELECT", "START", "UPDATE"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# check if the full_statement starts with any command
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Check if... for PEP

command = next((cmd for cmd in commands if full_statement.startswith(cmd)), None)

# Flatten statement
tokens = list(statements[0].flatten())
Expand Down Expand Up @@ -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":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why special-case CREATE VIEW instead of just CREATE for, e.g., CREATE TABLE also?

ret = True

# If constraint violated
except sqlalchemy.exc.IntegrityError as e:
self._logger.error(termcolor.colored(_statement, "red"))
Expand Down