Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: cs50/python-cs50
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v9.3.2
Choose a base ref
...
head repository: cs50/python-cs50
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 10 commits
  • 5 files changed
  • 3 contributors

Commits on Dec 18, 2023

  1. updated style

    dmalan committed Dec 18, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    fc6647a View commit details
  2. fixed YAML

    dmalan committed Dec 18, 2023
    Copy the full SHA
    128f498 View commit details
  3. fixed merge

    dmalan committed Dec 18, 2023
    Copy the full SHA
    3b90369 View commit details
  4. updated for MySQL tests

    dmalan committed Dec 18, 2023
    Copy the full SHA
    e47cc35 View commit details
  5. updated SQLAlchemy versioning

    dmalan committed Dec 18, 2023
    Copy the full SHA
    dc6a43f View commit details

Commits on Mar 4, 2024

  1. updated workflow actions

    rongxin-liu committed Mar 4, 2024
    Copy the full SHA
    688af68 View commit details

Commits on May 2, 2024

  1. updated actions/github-script to version v7

    rongxin-liu committed May 2, 2024
    Copy the full SHA
    d67a26b View commit details

Commits on Oct 15, 2024

  1. adds support for VACUUM

    dmalan committed Oct 15, 2024
    Copy the full SHA
    2d5fd94 View commit details
  2. fixes raw strings

    dmalan committed Oct 15, 2024
    Copy the full SHA
    f81a0a3 View commit details
  3. Merge pull request #186 from cs50/vacuum

    Adds support for `VACUUM`, fixes raw strings
    rongxin-liu authored Oct 15, 2024
    Copy the full SHA
    e75afaf View commit details
Showing with 24 additions and 13 deletions.
  1. +3 −3 .github/workflows/main.yml
  2. +2 −2 docker-compose.yml
  3. +2 −2 setup.py
  4. +10 −5 src/cs50/sql.py
  5. +7 −1 tests/sql.py
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -20,8 +20,8 @@ jobs:
ports:
- 5432:5432
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.7'
check-latest: true
@@ -56,7 +56,7 @@ jobs:
- name: Create Release
if: ${{ github.ref == 'refs/heads/main' }}
uses: actions/github-script@v6
uses: actions/github-script@v7
with:
github-token: ${{ github.token }}
script: |
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ services:
- postgres
environment:
MYSQL_HOST: mysql
POSTGRESQL_HOST: postgresql
POSTGRESQL_HOST: postgres
links:
- mysql
- postgres
@@ -20,7 +20,7 @@ services:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
healthcheck:
test: ["CMD", "mysqladmin", "-uroot", "ping"]
image: cs50/mysql:8
image: cs50/mysql
ports:
- 3306:3306
postgres:
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -10,13 +10,13 @@
"Topic :: Software Development :: Libraries :: Python Modules"
],
description="CS50 library for Python",
install_requires=["Flask>=1.0", "packaging", "SQLAlchemy<3", "sqlparse", "termcolor", "wheel"],
install_requires=["Flask>=1.0", "packaging", "SQLAlchemy>=2,<3", "sqlparse", "termcolor", "wheel"],
keywords="cs50",
license="GPLv3",
long_description_content_type="text/markdown",
name="cs50",
package_dir={"": "src"},
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="9.3.2"
version="9.4.0"
)
15 changes: 10 additions & 5 deletions src/cs50/sql.py
Original file line number Diff line number Diff line change
@@ -71,9 +71,13 @@ def __init__(self, url, **kwargs):
# without isolation_level, PostgreSQL warns with "there is already a transaction in progress" for our own BEGIN and
# "there is no transaction in progress" for our own COMMIT
self._engine = sqlalchemy.create_engine(url, **kwargs).execution_options(
autocommit=False, isolation_level="AUTOCOMMIT"
autocommit=False, isolation_level="AUTOCOMMIT", no_parameters=True
)

# Avoid doubly escaping percent signs, since no_parameters=True anyway
# https://github.com/cs50/python-cs50/issues/171
self._engine.dialect.identifier_preparer._double_percents = False

# Get logger
self._logger = logging.getLogger("cs50")

@@ -173,6 +177,7 @@ def execute(self, sql, *args, **kwargs):
"SELECT",
"START",
"UPDATE",
"VACUUM",
}

# Check if the full_statement starts with any command
@@ -324,12 +329,12 @@ def execute(self, sql, *args, **kwargs):
sqlparse.tokens.Literal.String,
sqlparse.tokens.Literal.String.Single,
]:
token.value = re.sub("(^'|\s+):", r"\1\:", token.value)
token.value = re.sub(r"(^'|\s+):", r"\1\:", token.value)

# In identifier
# https://www.sqlite.org/lang_keywords.html
elif token.ttype == sqlparse.tokens.Literal.String.Symbol:
token.value = re.sub('(^"|\s+):', r"\1\:", token.value)
token.value = re.sub(r'(^"|\s+):', r"\1\:", token.value)

# Join tokens into statement
statement = "".join([str(token) for token in tokens])
@@ -374,7 +379,7 @@ def teardown_appcontext(exception):
)

# Check for start of transaction
if command in ["BEGIN", "START"]:
if command in ["BEGIN", "START", "VACUUM"]: # cannot VACUUM from within a transaction
self._autocommit = False

# Execute statement
@@ -385,7 +390,7 @@ def teardown_appcontext(exception):
connection.execute(sqlalchemy.text("COMMIT"))

# Check for end of transaction
if command in ["COMMIT", "ROLLBACK"]:
if command in ["COMMIT", "ROLLBACK", "VACUUM"]: # cannot VACUUM from within a transaction
self._autocommit = True

# Return value
8 changes: 7 additions & 1 deletion tests/sql.py
Original file line number Diff line number Diff line change
@@ -138,6 +138,12 @@ def test_lastrowid(self):
self.assertEqual(self.db.execute("INSERT INTO foo (firstname, lastname) VALUES('firstname', 'lastname')"), 1)
self.assertRaises(ValueError, self.db.execute, "INSERT INTO foo (id, firstname, lastname) VALUES(1, 'firstname', 'lastname')")

def test_url(self):
url = "https://www.amazon.es/Desesperaci%C3%B3n-BEST-SELLER-Stephen-King/dp/8497595890"
self.db.execute("CREATE TABLE foo(id SERIAL PRIMARY KEY, url TEXT)")
self.db.execute("INSERT INTO foo (url) VALUES(?)", url)
self.assertEqual(self.db.execute("SELECT url FROM foo")[0]["url"], url)

def tearDown(self):
self.db.execute("DROP TABLE cs50")
self.db.execute("DROP TABLE IF EXISTS foo")
@@ -330,7 +336,7 @@ def test_cte(self):
if __name__ == "__main__":
suite = unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(SQLiteTests),
#unittest.TestLoader().loadTestsFromTestCase(MySQLTests),
unittest.TestLoader().loadTestsFromTestCase(MySQLTests),
unittest.TestLoader().loadTestsFromTestCase(PostgresTests)
])