26
26
from sqlalchemy .sql import crud
27
27
from sqlalchemy .sql import compiler
28
28
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
30
30
31
31
32
32
def rewrite_update (clauseelement , multiparams , params ):
@@ -198,7 +198,21 @@ def visit_insert(self, insert_stmt, asfrom=False, **kw):
198
198
"selectable" : insert_stmt })
199
199
200
200
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
202
216
203
217
if not crud_params and \
204
218
not self .dialect .supports_default_values and \
@@ -207,7 +221,7 @@ def visit_insert(self, insert_stmt, asfrom=False, **kw):
207
221
"The '%s' dialect with current database version settings does "
208
222
"not support empty inserts." % self .dialect .name )
209
223
210
- if insert_stmt . _has_multi_parameters :
224
+ if _has_multi_parameters :
211
225
if not self .dialect .supports_multivalues_insert :
212
226
raise NotImplementedError (
213
227
"The '%s' dialect with current database "
@@ -262,7 +276,7 @@ def visit_insert(self, insert_stmt, asfrom=False, **kw):
262
276
text += " (%s)" % self .process (self ._insert_from_select , ** kw )
263
277
elif not crud_params and supports_default_values :
264
278
text += " DEFAULT VALUES"
265
- elif insert_stmt . _has_multi_parameters :
279
+ elif _has_multi_parameters :
266
280
text += " VALUES %s" % (
267
281
", " .join (
268
282
"(%s)" % (
@@ -294,7 +308,17 @@ def visit_update(self, update_stmt, **kw):
294
308
295
309
self .isupdate = True
296
310
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
298
322
299
323
text = 'UPDATE '
300
324
@@ -384,20 +408,34 @@ def _get_crud_params(compiler, stmt, **kw):
384
408
required = True ))
385
409
for c in stmt .table .columns ]
386
410
387
- if stmt ._has_multi_parameters :
388
- stmt_parameters = stmt .parameters [0 ]
389
- else :
390
- stmt_parameters = stmt .parameters
391
-
392
411
# getters - these are normally just column.key,
393
412
# but in the case of mysql multi-table update, the rules for
394
413
# .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 :
396
427
_column_as_key , _getattr_col_key , _col_bind_name = \
397
428
crud ._key_getters_for_crud_column (compiler , stmt )
429
+ _has_multi_parameters = stmt ._has_multi_parameters
398
430
else :
399
431
_column_as_key , _getattr_col_key , _col_bind_name = \
400
432
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
401
439
402
440
# if we have statement parameters - set defaults in the
403
441
# compiled params
0 commit comments