Skip to content

Commit 78f15ec

Browse files
committed
Add minimal SQLAlchemy 1.4 compatibility for CrateCompiler
> A major initiative in the 1.4 series is to approach the model of both Core SQL statements as well as the ORM Query to allow for an efficient, cacheable model of statement creation and compilation, where the compilation step would be cached, based on a cache key generated by the created statement object, which itself is newly created for each use. While there is probably more to it, this patch tries to at least make the CrateCompiler work with the new subsystem infrastructure.
1 parent 8ba3b99 commit 78f15ec

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

src/crate/client/sqlalchemy/compiler.py

+49-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from sqlalchemy.sql import crud
2727
from sqlalchemy.sql import compiler
2828
from .types import MutableDict
29-
from .sa_version import SA_1_1, SA_VERSION
29+
from .sa_version import SA_1_1, SA_1_4, SA_VERSION
3030

3131

3232
def rewrite_update(clauseelement, multiparams, params):
@@ -198,7 +198,21 @@ def visit_insert(self, insert_stmt, asfrom=False, **kw):
198198
"selectable": insert_stmt})
199199

200200
self.isinsert = True
201-
crud_params = crud._get_crud_params(self, insert_stmt, **kw)
201+
202+
if SA_VERSION >= SA_1_4:
203+
# Minimal patch to be compatible with SQLAlchemy 1.4.
204+
# For a more thorough implementation, please follow
205+
# https://github.com/sqlalchemy/sqlalchemy/commit/851fb8f5a661c66ee76308181118369c8c4df9e0.
206+
# See also https://github.com/crate/crate-python/pull/391.
207+
compile_state = insert_stmt._compile_state_factory(
208+
insert_stmt, self, **kw
209+
)
210+
insert_stmt = compile_state.statement
211+
crud_params = crud._get_crud_params(self, insert_stmt, compile_state, **kw)
212+
_has_multi_parameters = compile_state._has_multi_parameters
213+
else:
214+
crud_params = crud._get_crud_params(self, insert_stmt, **kw)
215+
_has_multi_parameters = insert_stmt._has_multi_parameters
202216

203217
if not crud_params and \
204218
not self.dialect.supports_default_values and \
@@ -207,7 +221,7 @@ def visit_insert(self, insert_stmt, asfrom=False, **kw):
207221
"The '%s' dialect with current database version settings does "
208222
"not support empty inserts." % self.dialect.name)
209223

210-
if insert_stmt._has_multi_parameters:
224+
if _has_multi_parameters:
211225
if not self.dialect.supports_multivalues_insert:
212226
raise NotImplementedError(
213227
"The '%s' dialect with current database "
@@ -262,7 +276,7 @@ def visit_insert(self, insert_stmt, asfrom=False, **kw):
262276
text += " (%s)" % self.process(self._insert_from_select, **kw)
263277
elif not crud_params and supports_default_values:
264278
text += " DEFAULT VALUES"
265-
elif insert_stmt._has_multi_parameters:
279+
elif _has_multi_parameters:
266280
text += " VALUES %s" % (
267281
", ".join(
268282
"(%s)" % (
@@ -294,7 +308,17 @@ def visit_update(self, update_stmt, **kw):
294308

295309
self.isupdate = True
296310

297-
extra_froms = update_stmt._extra_froms
311+
if SA_VERSION >= SA_1_4:
312+
# Minimal patch to be compatible with SQLAlchemy 1.4.
313+
# For a more thorough implementation, please follow
314+
# https://github.com/sqlalchemy/sqlalchemy/commit/851fb8f5a661c66ee76308181118369c8c4df9e0.
315+
# See also https://github.com/crate/crate-python/pull/391.
316+
compile_state = update_stmt._compile_state_factory(
317+
update_stmt, compiler, **kw
318+
)
319+
extra_froms = compile_state._extra_froms
320+
else:
321+
extra_froms = update_stmt._extra_froms
298322

299323
text = 'UPDATE '
300324

@@ -384,20 +408,34 @@ def _get_crud_params(compiler, stmt, **kw):
384408
required=True))
385409
for c in stmt.table.columns]
386410

387-
if stmt._has_multi_parameters:
388-
stmt_parameters = stmt.parameters[0]
389-
else:
390-
stmt_parameters = stmt.parameters
391-
392411
# getters - these are normally just column.key,
393412
# but in the case of mysql multi-table update, the rules for
394413
# .key must conditionally take tablename into account
395-
if SA_VERSION >= SA_1_1:
414+
if SA_VERSION >= SA_1_4:
415+
# Minimal patch to be compatible with SQLAlchemy 1.4.
416+
# For a more thorough implementation, please follow
417+
# https://github.com/sqlalchemy/sqlalchemy/commit/851fb8f5a661c66ee76308181118369c8c4df9e0.
418+
# See also https://github.com/crate/crate-python/pull/391.
419+
compile_state = stmt._compile_state_factory(
420+
stmt, compiler, **kw
421+
)
422+
stmt = compile_state.statement
423+
_column_as_key, _getattr_col_key, _col_bind_name = \
424+
crud._key_getters_for_crud_column(compiler, stmt, compile_state)
425+
_has_multi_parameters = compile_state._has_multi_parameters
426+
elif SA_VERSION >= SA_1_1:
396427
_column_as_key, _getattr_col_key, _col_bind_name = \
397428
crud._key_getters_for_crud_column(compiler, stmt)
429+
_has_multi_parameters = stmt._has_multi_parameters
398430
else:
399431
_column_as_key, _getattr_col_key, _col_bind_name = \
400432
crud._key_getters_for_crud_column(compiler)
433+
_has_multi_parameters = stmt._has_multi_parameters
434+
435+
if _has_multi_parameters:
436+
stmt_parameters = stmt.parameters[0]
437+
else:
438+
stmt_parameters = stmt.parameters
401439

402440
# if we have statement parameters - set defaults in the
403441
# compiled params

src/crate/client/sqlalchemy/sa_version.py

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
SA_VERSION = V(sa.__version__)
2626

2727
SA_1_1 = V('1.1a0')
28+
SA_1_4 = V('1.4.0b1')

0 commit comments

Comments
 (0)