Skip to content

Commit bcf9fff

Browse files
committed
SA14: Adjust CrateDB dialect compiler patch to SqlAlchemy 1.4.36
The original code for `visit_update` and `_get_crud_params` from SQLAlchemy 1.4.0b1 has been vendored into the CrateDB dialect the other day, in order to amend it due to dialect-specific purposes. This patch reflects the changes from SA 1.4.0b1 to SA 1.4.36 on this code.
1 parent 62fd182 commit bcf9fff

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

lgtm.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
queries:
2+
3+
# Suppress some LGTM warnings.
4+
5+
# A module is imported with the "import" and "import from" statements.
6+
# https://lgtm.com/rules/1818040193/
7+
- exclude: py/import-and-import-from

src/crate/client/sqlalchemy/compiler.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
from collections import defaultdict
2424

2525
import sqlalchemy as sa
26-
from sqlalchemy.sql import crud, selectable
27-
from sqlalchemy.sql import compiler
26+
from sqlalchemy.sql import compiler, crud, selectable
2827
from .types import MutableDict
2928
from .sa_version import SA_VERSION, SA_1_4
3029

@@ -400,6 +399,10 @@ def visit_update_14(self, update_stmt, **kw):
400399
else:
401400
dialect_hints = None
402401

402+
if update_stmt._independent_ctes:
403+
for cte in update_stmt._independent_ctes:
404+
cte._compiler_dispatch(self, **kw)
405+
403406
text += table_text
404407

405408
text += " SET "
@@ -459,8 +462,9 @@ def visit_update_14(self, update_stmt, **kw):
459462
update_stmt, self.returning or update_stmt._returning
460463
)
461464

462-
if self.ctes and toplevel:
463-
text = self._render_cte_clause() + text
465+
if self.ctes:
466+
nesting_level = len(self.stack) if not toplevel else None
467+
text = self._render_cte_clause(nesting_level=nesting_level) + text
464468

465469
self.stack.pop(-1)
466470

@@ -481,7 +485,7 @@ def _get_crud_params_14(compiler, stmt, compile_state, **kw):
481485
from sqlalchemy.sql.crud import _create_bind_param
482486
from sqlalchemy.sql.crud import REQUIRED
483487
from sqlalchemy.sql.crud import _get_stmt_parameter_tuples_params
484-
from sqlalchemy.sql.crud import _get_multitable_params
488+
from sqlalchemy.sql.crud import _get_update_multitable_params
485489
from sqlalchemy.sql.crud import _scan_insert_from_select_cols
486490
from sqlalchemy.sql.crud import _scan_cols
487491
from sqlalchemy import exc # noqa: F401
@@ -561,7 +565,7 @@ def _get_crud_params_14(compiler, stmt, compile_state, **kw):
561565
# special logic that only occurs for multi-table UPDATE
562566
# statements
563567
if compile_state.isupdate and compile_state.is_multitable:
564-
_get_multitable_params(
568+
_get_update_multitable_params(
565569
compiler,
566570
stmt,
567571
compile_state,
@@ -620,9 +624,18 @@ def _get_crud_params_14(compiler, stmt, compile_state, **kw):
620624

621625
if compile_state._has_multi_parameters:
622626
values = _extend_values_for_multiparams(
623-
compiler, stmt, compile_state, values, kw
627+
compiler,
628+
stmt,
629+
compile_state,
630+
values,
631+
_column_as_key,
632+
kw,
624633
)
625-
elif not values and compiler.for_executemany:
634+
elif (
635+
not values
636+
and compiler.for_executemany # noqa: W503
637+
and compiler.dialect.supports_default_metavalue # noqa: W503
638+
):
626639
# convert an "INSERT DEFAULT VALUES"
627640
# into INSERT (firstcol) VALUES (DEFAULT) which can be turned
628641
# into an in-place multi values. This supports

src/crate/client/sqlalchemy/dialect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# FIXME: Workaround to be able to use SQLAlchemy 1.4.
2626
# Caveat: This purges the ``cresultproxy`` extension
2727
# at runtime, so it will impose a speed bump.
28-
import crate.client.sqlalchemy.monkey # noqa:F401
28+
import crate.client.sqlalchemy.monkey # noqa:F401, lgtm[py/unused-import]
2929

3030
from sqlalchemy import types as sqltypes
3131
from sqlalchemy.engine import default, reflection

src/crate/client/sqlalchemy/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class Any(expression.ColumnElement):
168168

169169
def __init__(self, left, right, operator=operators.eq):
170170
self.type = sqltypes.Boolean()
171-
self.left = expression._literal_as_binds(left)
171+
self.left = expression.literal(left)
172172
self.right = right
173173
self.operator = operator
174174

0 commit comments

Comments
 (0)