Skip to content

Commit 506ce01

Browse files
cpcloudkszucs
authored andcommitted
refactor(sqlalchemy): clean up quoting implementation
1 parent 9e80231 commit 506ce01

File tree

7 files changed

+13
-14
lines changed

7 files changed

+13
-14
lines changed

ibis/backends/base/sql/alchemy/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,10 @@ def insert(
576576
f"The given obj is of type {type(obj).__name__} ."
577577
)
578578

579+
def _quote(self, name: str) -> str:
580+
"""Quote an identifier."""
581+
return self.con.dialect.identifier_preparer.quote(name)
582+
579583
def _get_temp_view_definition(
580584
self,
581585
name: str,
@@ -613,7 +617,7 @@ def _create_temp_view(self, view: sa.Table, definition: sa.sql.Selectable) -> No
613617
raw_name = view.name
614618
if raw_name not in self._temp_views and raw_name in self.list_tables():
615619
raise ValueError(f"{raw_name} already exists as a table or view")
616-
name = self.con.dialect.identifier_preparer.quote_identifier(view.name)
620+
name = self._quote(raw_name)
617621
lines, params = self._get_compiled_statement(definition, name)
618622
with self.begin() as con:
619623
for line in lines:

ibis/backends/duckdb/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ def normalize_filenames(source_list):
4242
return list(map(util.normalize_filename, source_list))
4343

4444

45-
def _quote(name: str):
46-
return _dialect.identifier_preparer.quote(name)
47-
48-
4945
def _format_kwargs(kwargs):
5046
return (
5147
f"{k}='{v}'" if isinstance(v, str) else f"{k}={v!r}" for k, v in kwargs.items()
@@ -237,7 +233,7 @@ def read_csv(
237233
if not table_name:
238234
table_name = f"ibis_read_csv_{next(csv_n)}"
239235

240-
quoted_table_name = _quote(table_name)
236+
quoted_table_name = self._quote(table_name)
241237

242238
# auto_detect and columns collide, so we set auto_detect=True
243239
# unless COLUMNS has been specified
@@ -309,7 +305,7 @@ def read_parquet(
309305
dataset = str(source_list)
310306
table_name = table_name or f"ibis_read_parquet_{next(pa_n)}"
311307

312-
quoted_table_name = _quote(table_name)
308+
quoted_table_name = self._quote(table_name)
313309
sql = f"""CREATE OR REPLACE VIEW {quoted_table_name} AS
314310
SELECT * FROM read_parquet({dataset})"""
315311

@@ -362,7 +358,7 @@ def read_postgres(self, uri, table_name=None):
362358
"`table_name` is required when registering a postgres table"
363359
)
364360
self._load_extensions(["postgres_scanner"])
365-
quoted_table_name = _quote(table_name)
361+
quoted_table_name = self._quote(table_name)
366362
sql = (
367363
f"CREATE OR REPLACE VIEW {quoted_table_name} AS "
368364
f"SELECT * FROM postgres_scan_pushdown('{uri}', 'public', '{table_name}')"

ibis/backends/mssql/tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
)
8181
def test_get_schema_from_query(con, server_type, expected_type):
8282
raw_name = f"tmp_{ibis.util.guid()}"
83-
name = con.con.dialect.identifier_preparer.quote_identifier(raw_name)
83+
name = con._quote(raw_name)
8484
expected_schema = ibis.schema(dict(x=expected_type))
8585
try:
8686
with con.begin() as c:

ibis/backends/mysql/tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
)
6161
def test_get_schema_from_query(con, mysql_type, expected_type):
6262
raw_name = ibis.util.guid()
63-
name = con.con.dialect.identifier_preparer.quote_identifier(raw_name)
63+
name = con._quote(raw_name)
6464
# temporary tables get cleaned up by the db when the session ends, so we
6565
# don't need to explicitly drop the table
6666
with con.begin() as c:

ibis/backends/postgres/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def udf(
175175

176176
def _metadata(self, query: str) -> Iterable[tuple[str, dt.DataType]]:
177177
raw_name = util.guid()
178-
name = self.con.dialect.identifier_preparer.quote_identifier(raw_name)
178+
name = self._quote(raw_name)
179179
type_info_sql = f"""\
180180
SELECT
181181
attname,

ibis/backends/postgres/tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def test_create_and_drop_table(con, temp_table, params):
218218
)
219219
def test_get_schema_from_query(con, pg_type, expected_type):
220220
raw_name = ibis.util.guid()
221-
name = con.con.dialect.identifier_preparer.quote_identifier(raw_name)
221+
name = con._quote(raw_name)
222222
with con.begin() as c:
223223
c.execute(
224224
sa.text(f"CREATE TEMPORARY TABLE {name} (x {pg_type}, y {pg_type}[])")

ibis/backends/sqlite/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ def attach(self, name: str, path: str | Path) -> None:
182182
>>> con1.attach("new", "new.db")
183183
>>> con1.list_tables(database="new")
184184
"""
185-
quoted_name = self.con.dialect.identifier_preparer.quote(name)
186-
self.raw_sql(f"ATTACH DATABASE {str(path)!r} AS {quoted_name}")
185+
self.raw_sql(f"ATTACH DATABASE {str(path)!r} AS {self._quote(name)}")
187186

188187
def _get_sqla_table(
189188
self, name: str, schema: str | None = None, autoload: bool = True, **_: Any

0 commit comments

Comments
 (0)