diff --git a/duckdb_engine/__init__.py b/duckdb_engine/__init__.py index 1bf5803c..32b6a5ff 100644 --- a/duckdb_engine/__init__.py +++ b/duckdb_engine/__init__.py @@ -1,4 +1,6 @@ +import os import re +import uuid import warnings from typing import ( TYPE_CHECKING, @@ -476,6 +478,9 @@ def initialize(self, connection: "Connection") -> None: def create_connect_args(self, url: URL) -> Tuple[tuple, dict]: opts = url.translate_connect_args(database="database") opts["url_config"] = dict(url.query) + user = opts["url_config"].pop("user", None) + if user is not None: + opts["database"] += f"?user={user}" return (), opts @classmethod diff --git a/duckdb_engine/tests/test_basic.py b/duckdb_engine/tests/test_basic.py index 58ab760b..24e52cbd 100644 --- a/duckdb_engine/tests/test_basic.py +++ b/duckdb_engine/tests/test_basic.py @@ -606,3 +606,36 @@ def test_close(engine: Engine) -> None: with engine.connect() as conn: res = conn.execute(text("select 1")) res.close() + + +def test_with_cache(tmp_path: Path) -> None: + tmp_db_path = str(tmp_path / "db_cached") + engine1 = create_engine(f"duckdb:///{tmp_db_path}?threads=1") + engine2 = create_engine(f"duckdb:///{tmp_db_path}?threads=2") + with engine1.connect() as conn1: + with engine2.connect() as conn2: + res1 = conn1.execute( + text("select value from duckdb_settings() where name = 'threads'") + ).fetchall() + res2 = conn2.execute( + text("select value from duckdb_settings() where name = 'threads'") + ).fetchall() + assert res1 == res2 + assert res1[0][0] == "1" + + +def test_no_cache(tmp_path: Path) -> None: + tmp_db_path = str(tmp_path / "db_no_cache") + engine1 = create_engine(f"duckdb:///{tmp_db_path}?threads=1&user=1") + engine2 = create_engine(f"duckdb:///{tmp_db_path}?threads=2&user=2") + with engine1.connect() as conn1: + with engine2.connect() as conn2: + res1 = conn1.execute( + text("select value from duckdb_settings() where name = 'threads'") + ).fetchall() + res2 = conn2.execute( + text("select value from duckdb_settings() where name = 'threads'") + ).fetchall() + assert res1 != res2 + assert res1[0][0] == "1" + assert res2[0][0] == "2"