Skip to content
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

fix: use numeric_dollar where available #836

Merged
merged 5 commits into from
Dec 5, 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
10 changes: 8 additions & 2 deletions duckdb_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import duckdb
import sqlalchemy
from packaging.version import Version
from sqlalchemy import pool, text
from sqlalchemy import types as sqltypes
from sqlalchemy import util
Expand All @@ -29,6 +30,7 @@
from .datatypes import ISCHEMA_NAMES, register_extension_types

__version__ = "0.9.2"
sqlalchemy_version = Version(sqlalchemy.__version__)

if TYPE_CHECKING:
from sqlalchemy.base import Connection
Expand All @@ -39,7 +41,7 @@


class DBAPI:
paramstyle = duckdb.paramstyle
paramstyle = "numeric_dollar" if sqlalchemy_version >= Version("2.0.0") else "qmark"
apilevel = duckdb.apilevel
threadsafety = duckdb.threadsafety

Expand Down Expand Up @@ -134,7 +136,11 @@ def execute(
try:
if statement.lower() == "commit": # this is largely for ipython-sql
self.__c.commit()
elif statement.lower() in ("register", "register(?, ?)"):
elif statement.lower() in (
"register",
"register(?, ?)",
"register($1, $2)",
):
assert parameters and len(parameters) == 2, parameters
view_name, df = parameters
self.__c.register(view_name, df)
Expand Down
11 changes: 4 additions & 7 deletions duckdb_engine/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@


def test_361(engine: Engine) -> None:
importorskip("sqlalchemy", "2.0.0")

Check warning on line 433 in duckdb_engine/tests/test_basic.py

View check run for this annotation

Codecov / codecov/patch

duckdb_engine/tests/test_basic.py#L433

Added line #L433 was not covered by tests

with engine.connect() as conn:
conn.execute(text("create table test (dt date);"))
conn.execute(text("insert into test values ('2022-01-01');"))
Expand All @@ -440,10 +442,5 @@
part = "year"
date_part = func.date_part(part, test.c.dt)

stmt = (
select(date_part)
.select_from(test)
.group_by(date_part)
.compile(dialect=engine.dialect, compile_kwargs={"literal_binds": True})
)
conn.execute(stmt).fetchall()
stmt = select(date_part).select_from(test).group_by(date_part)
assert conn.execute(stmt).fetchall() == [(2022,)]

Check warning on line 446 in duckdb_engine/tests/test_basic.py

View check run for this annotation

Codecov / codecov/patch

duckdb_engine/tests/test_basic.py#L445-L446

Added lines #L445 - L446 were not covered by tests