forked from IBM/sqlalchemy-ibmi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
1613 lines (1355 loc) · 58.9 KB
/
Copy pathbase.py
File metadata and controls
1613 lines (1355 loc) · 58.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# +--------------------------------------------------------------------------+
# | Licensed Materials - Property of IBM |
# | |
# | (C) Copyright IBM Corporation 2008, 2016. |
# +--------------------------------------------------------------------------+
# | This module complies with SQLAlchemy 0.8 and is |
# | Licensed under the Apache License, Version 2.0 (the "License"); |
# | you may not use this file except in compliance with the License. |
# | You may obtain a copy of the License at |
# | http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable |
# | law or agreed to in writing, software distributed under the License is |
# | distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
# | KIND, either express or implied. See the License for the specific |
# | language governing permissions and limitations under the License. |
# +--------------------------------------------------------------------------+
# | Authors: Alex Pitigoi, Abhigyan Agrawal, Rahul Priyadarshi |
# | Contributors: Jaimy Azle, Mike Bayer |
# +--------------------------------------------------------------------------+
"""
DBAPI Connection
----------------
This dialect uses the `pyodbc <https://github.com/mkleehammer/pyodbc>`_ DBAPI
and the `IBM i Access ODBC Driver
<https://www.ibm.com/support/pages/ibm-i-access-client-solutions>`_.
Connection string::
engine = create_engine("ibmi://user:password@host/rdbname[?key=value&key=value...]")
Connection Arguments
--------------------
The sqlalchemy-ibmi dialect supports multiple connection arguments that are
passed in the URL to the `create_engine
<https://docs.sqlalchemy.org/en/20/core/engines.html>`_ function.
Connection string keywords:
* ``current_schema`` - Define the default schema to use for unqualified names.
* ``library_list`` - Specify which IBM i libraries to add to the server job's
library list. Can be specified in the URL as a comma separated list, or as a
keyword argument to the create_engine function as a list of strings
* ``autocommit`` - If ``False``, Connection.commit must be called;
otherwise each statement is automatically committed.
Defaults to ``False``.
* ``readonly`` - If ``True``, the connection is set to read-only. Defaults to ``False``.
* ``timeout`` - The login timeout for the connection, in seconds.
* ``use_system_naming`` - If ``True``, the connection is set to use the System
naming convention, otherwise it will use the SQL naming convention.
Defaults to ``False``.
* ``trim_char_fields`` - If ``True``, all character fields will be returned
with trailing spaces truncated. Defaults to ``False``.
* ``ssl`` - If ``True`` a Secure Sockets Layer (SSL) connection will be used to
encrypt all client/server communication. If ``False``, only the password will
be encrypted. Defaults to ``False``.
create-engine arguments:
* ``fast_executemany`` - Enables PyODBC's `fast_executemany
<https://github.com/mkleehammer/pyodbc/wiki/Cursor#executemanysql-params-with-fast_executemanytrue>`_
option. Conversion between input and target types is mostly unsupported when this
feature is enabled. eg. Inserting a Decimal object into a Float column will
produce the error "Converting decimal loses precision". Defaults to ``False``.
Transaction Isolation Level / Autocommit
----------------------------------------
Db2 for i supports 5 isolation levels:
* ``SERIALIZABLE``: ``*RR``
* ``READ COMMITTED``: ``*CS``
* ``READ UNCOMMITTED``: ``*CHG``
* ``REPEATABLE READ``: ``*ALL``
* ``NO COMMIT``: ``*NC``
**At this time, sqlalchemy-ibmi supports all of these isolation levels
except NO COMMIT.**
Autocommit is supported on all available isolation levels.
To set isolation level globally::
engine = create_engine("ibmi://user:pass@host/", isolation_level='REPEATABLE_READ')
To set using per-connection execution options::
connection = engine.connect()
connection = connection.execution_options(
isolation_level="SERIALIZABLE"
)
Table Creation String Size
--------------------------
When creating a table with SQLAlchemy, Db2 for i requires that the size of
a String column be provided.
Provide the length for a String column as follows:
.. code-block:: python
:emphasize-lines: 4, 8
class User(Base):
__tablename__ = 'users'
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
name = Column(String(50))
users = Table('users', metadata,
Column('id', Integer, Sequence('user_id_seq'), primary_key=True),
Column('name', String(50)),
)
Literal Values and Untyped Parameters
-------------------------------------
SQLAlchemy will try to use parameter markers as much as possible, even for values
specified with the `literal
<https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.literal>`_,
`null <https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.null>`_,
`func <https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.func>`_
sql expression functions. Because Db2 for i doesn't support untyped parameter markers,
in places where the type is unknown, a CAST expression must be placed around it to
give it a type. sqlalchemy-ibmi will automatically do this based on the type object
provided to SQLAlchemy.
In some cases, SQLAlchemy allows specifying a Python object directly without a type
object. In this case, SQLAlchemy will deduce the type object based on the Python type:
+-------------------+-----------------+
| Python type | SQLAlchemy type |
+===================+=================+
| bool | Boolean |
+-------------------+-----------------+
| int | Integer |
+-------------------+-----------------+
| float | Float |
+-------------------+-----------------+
| str | Unicode |
+-------------------+-----------------+
| bytes | LargeBinary |
+-------------------+-----------------+
| decimal.Decimal | Numeric |
+-------------------+-----------------+
| datetime.datetime | DateTime |
+-------------------+-----------------+
| datetime.date | DateTime |
+-------------------+-----------------+
| datetime.time | DateTime |
+-------------------+-----------------+
The deduced SQLAlchemy type will be generic however, having no length, precision, or
scale defined. This causes problems when generating these CAST expressions. To support
handling the majority of cases, some types will be adjusted:
+-------------------+-----------------+
| Python type | SQLAlchemy type |
+===================+=================+
| int | BigInteger |
+-------------------+-----------------+
| str | Unicode(32739) |
+-------------------+-----------------+
In addition, Numeric types will be rendered as inline literals. On SQLAlchemy 1.4 and
up, this will be done using `render_literal_execute
<https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.BindParameter.render_literal_execute>`_
to support statement caching.
If the type used is not appropriate (eg. when specifying a >32k string), you must
specify the type (or use a `cast
<https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.cast>`_)::
too_big_for_varchar = 'a' * 32768
connection.execute(
select(literal(too_big_for_varchar, UnicodeText()))
).scalar()
Text search support
-------------------
The ColumnOperators.match function is implemented using a basic LIKE operation by
default. However, when `OmniFind Text Search Server for Db2 for i
<https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_75/rzash/rzashkickoff.htm>`_ is
installed, match will take advantage of the CONTAINS function that it provides.
""" # noqa E501
import datetime
import re
from collections import defaultdict
from sqlalchemy import (
select,
text,
schema as sa_schema,
exc,
util,
Table,
MetaData,
Column,
__version__ as _SA_Version,
)
from sqlalchemy.sql import compiler, operators
from sqlalchemy.sql.expression import and_, cast
from sqlalchemy.engine import default, reflection
from sqlalchemy.engine import cursor as _cursor
from sqlalchemy.types import (
BLOB,
CHAR,
CLOB,
DATE,
DATETIME,
INTEGER,
SMALLINT,
BIGINT,
DECIMAL,
NUMERIC,
REAL,
TIME,
TIMESTAMP,
VARCHAR,
FLOAT,
)
from sqlalchemy import types as sa_types
from .constants import RESERVED_WORDS
def get_sa_version():
"""Returns the SQLAlchemy version as a list of integers."""
version = [int(ver_token) for ver_token in _SA_Version.split(".")[0:2]]
return version
SA_Version = get_sa_version()
class IBMBoolean(sa_types.Boolean):
"""Represents a Db2 Boolean Column"""
def result_processor(self, _, coltype):
def process(value):
if value is None:
return None
return bool(value)
return process
def bind_processor(self, _):
def process(value):
if value is None:
return None
return "1" if value else "0"
return process
class IBMDate(sa_types.Date):
"""Represents a Db2 Date Column"""
def result_processor(self, _, coltype):
def process(value):
if value is None:
return None
if isinstance(value, datetime.datetime):
value = datetime.date(value.year, value.month, value.day)
return value
return process
def bind_processor(self, _):
def process(value):
if value is None:
return None
if isinstance(value, datetime.datetime):
value = datetime.date(value.year, value.month, value.day)
return str(value)
return process
class DOUBLE(sa_types.Numeric):
"""Represents a Db2 Double Column"""
__visit_name__ = "DOUBLE"
class GRAPHIC(sa_types.CHAR):
"""Represents a Db2 Graphic Column"""
__visit_name__ = "GRAPHIC"
class VARGRAPHIC(sa_types.Unicode):
"""Represents a Db2 Vargraphic Column"""
__visit_name__ = "VARGRAPHIC"
class DBCLOB(sa_types.CLOB):
"""Represents a Db2 Dbclob Column"""
__visit_name__ = "DBCLOB"
class XML(sa_types.Text):
"""Represents a Db2 XML Column"""
__visit_name__ = "XML"
COLSPECS = {
sa_types.Boolean: IBMBoolean,
sa_types.Date: IBMDate,
}
ISCHEMA_NAMES = {
"BLOB": BLOB,
"CHAR": CHAR,
"CHARACTER": CHAR,
"CLOB": CLOB,
"DATE": DATE,
"DATETIME": DATETIME,
"INTEGER": INTEGER,
"SMALLINT": SMALLINT,
"BIGINT": BIGINT,
"DECIMAL": DECIMAL,
"NUMERIC": NUMERIC,
"REAL": REAL,
"DOUBLE": DOUBLE,
"FLOAT": FLOAT,
"TIME": TIME,
"TIMESTAMP": TIMESTAMP,
"VARCHAR": VARCHAR,
"XML": XML,
"GRAPHIC": GRAPHIC,
"VARGRAPHIC": VARGRAPHIC,
"DBCLOB": DBCLOB,
}
class DB2TypeCompiler(compiler.GenericTypeCompiler):
"""IBM i Db2 Type Compiler"""
def visit_TIMESTAMP(self, type_, **kw):
precision = getattr(type_, "precision", None)
if precision is not None:
return f"TIMESTAMP({precision})"
else:
return "TIMESTAMP"
def visit_DATETIME(self, type_, **kw):
return self.visit_TIMESTAMP(type_, **kw)
def visit_FLOAT(self, type_, **kw):
return (
"FLOAT"
if type_.precision is None
else "FLOAT(%(precision)s)" % {"precision": type_.precision}
)
def visit_BOOLEAN(self, type_, **kw):
return self.visit_SMALLINT(type_, **kw)
def _extend(self, type_, name, ccsid=None, length=None):
text = name
if not length:
length = type_.length
if length:
text += f"({length})"
if ccsid:
text += f" CCSID {ccsid}"
# TODO: Handle collation instead of CCSID
# if type_.collation:
# text += ' COLLATE "%s"' % type_.collation
return text
def visit_CHAR(self, type_, **kw):
return self._extend(type_, "CHAR", 1208)
def visit_VARCHAR(self, type_, **kw):
return self._extend(type_, "VARCHAR", 1208)
def visit_CLOB(self, type_, **kw):
return self._extend(type_, "CLOB", ccsid=1208, length=type_.length or "2G")
def visit_NCHAR(self, type_, **kw):
return self._extend(type_, "NCHAR")
def visit_NVARCHAR(self, type_, **kw):
return self._extend(type_, "NVARCHAR")
def visit_NCLOB(self, type_, **kw):
return self._extend(type_, "NCLOB", length=type_.length or "1G")
def visit_TEXT(self, type_, **kw):
return self.visit_CLOB(type_, **kw)
def visit_BLOB(self, type_, **kw):
length = type_.length or "2G"
return f"BLOB({length})"
def visit_numeric(self, type_, **kw):
# For many databases, NUMERIC and DECIMAL are equivalent aliases, but for Db2
# NUMERIC is zoned while DECIMAL is packed. Packed format gives better space
# usage and performance, so we prefer that by default. If a user really wants
# zoned, they can use types.NUMERIC class instead.
return self.visit_DECIMAL(type_, **kw)
# dialect-specific types
# This is now part of SQLAlchemy as of 2.0. We can drop this function once
# we drop support for earlier versions.
def visit_DOUBLE(self, type_, **kw):
return "DOUBLE"
def visit_GRAPHIC(self, type_, **kw):
return self._extend(type_, "GRAPHIC")
def visit_VARGRAPHIC(self, type_, **kw):
return self._extend(type_, "VARGRAPHIC")
def visit_DBCLOB(self, type_, **kw):
return self._extend(type_, "DBCLOB", length=type_.length or "1G")
def visit_XML(self, type_, **kw):
return "XML"
class DB2Compiler(compiler.SQLCompiler):
"""IBM i Db2 compiler class"""
def get_cte_preamble(self, recursive):
return "WITH"
def visit_now_func(self, fn, **kw):
return "CURRENT_TIMESTAMP"
def for_update_clause(self, select, **kw):
if select.for_update == "read":
return " WITH RS USE AND KEEP SHARE LOCKS"
if select.for_update:
return " WITH RS USE AND KEEP UPDATE LOCKS"
return ""
def visit_mod_binary(self, binary, operator, **kw):
return "mod(%s, %s)" % (self.process(binary.left), self.process(binary.right))
def visit_match_op_binary(self, binary, operator, **kw):
if self.dialect.text_server_available:
return "CONTAINS (%s, %s) > 0" % (
self.process(binary.left),
self.process(binary.right),
)
binary.right.value = "%" + binary.right.value + "%"
return "%s LIKE %s" % (self.process(binary.left), self.process(binary.right))
def limit_clause(self, select, **kw):
# On Db2 for i, there is a separate OFFSET clause, but there is no separate
# LIMIT clause. Instead, LIMIT is treated as an alternate or "shortcut" syntax
# of a FETCH clause.
# Because of this, these work: "LIMIT x", "LIMIT x OFFSET y"
# but these do not: "OFFSET y", "LIMIT x OFFSET y ROWS"
#
# Because of this, if we want to use the LIMIT alternate form, we'd have to
# special case both LIMIT with OFFSET and OFFSET without LIMIT. However, if we
# use the traditional FETCH form we need no special cases.
#
# OFFSET is supported since IBM i 7.1 TR11 / IBM i 7.2 TR3
text = ""
if select._offset_clause is not None:
text += " OFFSET " + self.process(select._offset_clause, **kw) + " ROWS "
if select._limit_clause is not None:
text += (
" FETCH FIRST "
+ self.process(select._limit_clause, **kw)
+ " ROWS ONLY "
)
return text
def visit_sequence(self, sequence, **kw):
return "NEXT VALUE FOR %s" % sequence.name
def default_from(self):
# Db2 uses SYSIBM.SYSDUMMY1 table for row count
return " FROM SYSIBM.SYSDUMMY1"
def visit_function(self, func, result_map=None, **kwargs):
if func.name.upper() == "AVG":
return "AVG(DOUBLE(%s))" % (self.function_argspec(func, **kwargs))
if func.name.upper() == "CHAR_LENGTH":
return "CHAR_LENGTH(%s, %s)" % (
self.function_argspec(func, **kwargs),
"OCTETS",
)
return compiler.SQLCompiler.visit_function(self, func, **kwargs)
# TODO: this is wrong but need to know what Db2 is expecting here
# if func.name.upper() == "LENGTH":
# return "LENGTH('%s')" % func.compile().params[func.name + '_1']
# else:
# return compiler.SQLCompiler.visit_function(self, func, **kwargs)
def visit_cast(self, cast, **kw):
kw["_cast_applied"] = True
return super().visit_cast(cast, **kw)
def visit_savepoint(self, savepoint_stmt, **kw):
return "SAVEPOINT %(sid)s ON ROLLBACK RETAIN CURSORS" % {
"sid": self.preparer.format_savepoint(savepoint_stmt)
}
def visit_rollback_to_savepoint(self, savepoint_stmt, **kw):
return "ROLLBACK TO SAVEPOINT %(sid)s" % {
"sid": self.preparer.format_savepoint(savepoint_stmt)
}
def visit_release_savepoint(self, savepoint_stmt, **kw):
return "RELEASE TO SAVEPOINT %(sid)s" % {
"sid": self.preparer.format_savepoint(savepoint_stmt)
}
def visit_unary(self, unary, **kw):
usql = super().visit_unary(unary, **kw)
if unary.operator == operators.exists and kw.get(
"within_columns_clause", False
):
usql = f"CASE WHEN {usql} THEN 1 ELSE 0 END"
return usql
def visit_empty_set_op_expr(self, type_, expand_op, **kw):
if expand_op is operators.not_in_op:
return "(%s)) OR (1 = 1" % (
", ".join(
"CAST(NULL AS %s)"
% self.dialect.type_compiler.process(
INTEGER() if element._isnull else element
)
for element in type_
)
)
elif expand_op is operators.in_op:
return "(%s)) OR (1 != 1" % (
", ".join(
"CAST(NULL AS %s)"
% self.dialect.type_compiler.process(
INTEGER() if element._isnull else element
)
for element in type_
)
)
else:
return self.visit_empty_set_expr(type_)
def visit_empty_set_expr(self, element_types, **kw):
return "SELECT 1 FROM SYSIBM.SYSDUMMY1 WHERE 1!=1"
def visit_over(self, over, **kw):
"""Override window function handling to avoid CAST in frame clause.
IBM i DB2 doesn't support CAST expressions in ROWS BETWEEN clauses.
We need to render literal values directly instead of using bind parameters.
"""
# Render with literal binds to avoid CAST(? AS BIGINT) in frame clause
kw = kw.copy()
kw['literal_binds'] = True
return super().visit_over(over, **kw)
def visit_null(self, expr, **kw):
if not kw.get("within_columns_clause", False):
return "NULL"
# We can't use a NULL constant/literal in a parameter list without a type
# or we'll get SQL0206 - Column or global variable NULL not found.
# We can work around this by casting to a type, but at this point we don't
# know what the type was, and when using the null() function, there will
# not be a type anyway, so we pick an arbitrary type of INTEGER which is
# most compatible with other types other than BLOB, XML, and some others.
#
# As an optimization, if we detect we're already in a CAST expression, then
# we don't need to add another.
if kw.get("_cast_applied", False):
# We're in a cast expression, so no need to cast
return "NULL"
return "CAST(NULL AS INTEGER)"
def visit_bindparam(
self,
bindparam,
within_columns_clause=False,
literal_binds=False,
skip_bind_expression=False,
literal_execute=False,
render_postcompile=False,
**kwargs,
):
if within_columns_clause and not literal_binds:
# Db2 doesn't support untyped parameter markers so we need to add a CAST
# clause around them to the appropriate type.
#
# Default Python type to SQLAlchemy type mapping:
# | Python type | SQLAlchemy type |
# |-------------------|-----------------|
# | bool | Boolean |
# | int | Integer |
# | float | Float |
# | str | Unicode |
# | bytes | LargeBinary |
# | decimal.Decimal | Numeric |
# | datetime.datetime | DateTime |
# | datetime.date | DateTime |
# | datetime.time | DateTime |
#
# Most types just need a cast, but some types we handle specially since we
# don't know how big the value will be and by literals will have its
# attributes set to default eg. length, precision, and scale all set to
# None. Since we can't base anything from the bindparam value as all literal
# values will end up caching to the same statement, we must assume the worst
# case scenario and try to handle any possible value. We could render
# everything as literals using bindparam.render_literal_execute(), but that
# will impact statement caching on the server as well as cause problems with
# bytes and str literals over 32k.
#
# - Integer: Cast to BigInteger
# - Unicode: If no length was specified, set length to VARCHAR max length.
# This will cause issues if users specify a >32k literal, but
# this seems unlikely and using UnicodeText by default would
# cause extra network flows for each literal. If a user needs
# to query a >32k literal, they can specify the type for the
# literal themselves.
# - Decimal: Render as a literal if no precision was specified. There's no
# precision and scale values we can use which could cover all
# Decimal literals.
type_ = bindparam.type
use_cast = True
if isinstance(type_, sa_types.Numeric) and not isinstance(
type_, sa_types.Float
):
if not type_.precision:
# Render this value as a literal in post-process
use_cast = False
try:
bindparam = bindparam.render_literal_execute()
except AttributeError:
# SQLAlchemy 1.3 doesn't have render_literal_execute
literal_binds = True
elif isinstance(type_, sa_types.String):
if not type_.length:
type_ = type_.copy()
type_.length = 32739
elif isinstance(type_, sa_types.Integer):
type_ = sa_types.BigInteger()
elif isinstance(type_, sa_types.NullType):
# Can't cast to a NULL, just leave it as-is
use_cast = False
if use_cast:
return self.process(cast(bindparam, type_))
return super().visit_bindparam(
bindparam,
within_columns_clause,
literal_binds,
skip_bind_expression,
literal_execute=literal_execute,
render_postcompile=render_postcompile,
**kwargs,
)
class DB2DDLCompiler(compiler.DDLCompiler):
"""DDL Compiler for IBM i Db2"""
def get_column_specification(self, column, **kw):
col_spec = [self.preparer.format_column(column)]
col_spec.append(
self.dialect.type_compiler.process(column.type, type_expression=column)
)
# column-options: "NOT NULL"
if not column.nullable or column.primary_key:
col_spec.append("NOT NULL")
# default-clause:
default = self.get_column_default_string(column)
if default is not None:
col_spec.append("WITH DEFAULT")
col_spec.append(default)
if column is column.table._autoincrement_column:
col_spec.append("GENERATED BY DEFAULT")
col_spec.append("AS IDENTITY")
col_spec.append("(START WITH 1)")
column_spec = " ".join(col_spec)
return column_spec
def define_constraint_cascades(self, constraint):
text = ""
if constraint.ondelete is not None:
text += " ON DELETE %s" % constraint.ondelete
if constraint.onupdate is not None:
util.warn("Db2 does not support UPDATE CASCADE for foreign keys.")
return text
def visit_drop_constraint(self, drop, **kw):
constraint = drop.element
if isinstance(constraint, sa_schema.ForeignKeyConstraint):
qual = "FOREIGN KEY "
const = self.preparer.format_constraint(constraint)
elif isinstance(constraint, sa_schema.PrimaryKeyConstraint):
qual = "PRIMARY KEY "
const = ""
elif isinstance(constraint, sa_schema.UniqueConstraint):
qual = "UNIQUE "
const = self.preparer.format_constraint(constraint)
else:
qual = ""
const = self.preparer.format_constraint(constraint)
if (
hasattr(constraint, "uConstraint_as_index")
and constraint.uConstraint_as_index
):
return "DROP %s%s" % (qual, const)
return "ALTER TABLE %s DROP %s%s" % (
self.preparer.format_table(constraint.table),
qual,
const,
)
def visit_create_index(
self, create, include_schema=True, include_table_schema=True, **kw
):
sql = super().visit_create_index(
create, include_schema=include_schema, include_table_schema=include_table_schema, **kw
)
if getattr(create.element, "uConstraint_as_index", None):
sql += " EXCLUDE NULL KEYS"
return sql
class DB2IdentifierPreparer(compiler.IdentifierPreparer):
"""IBM i Db2 specific identifier preparer"""
reserved_words = RESERVED_WORDS
illegal_initial_characters = {str(x) for x in range(0, 10)}.union(["_", "$"])
class DB2ExecutionContext(default.DefaultExecutionContext):
"""IBM i Db2 Execution Context class"""
_select_lastrowid = False
_lastrowid = None
def get_lastrowid(self):
return self._lastrowid
def pre_exec(self):
if self.isinsert:
tbl = self.compiled.statement.table
seq_column = tbl._autoincrement_column
insert_has_sequence = seq_column is not None
# IBM i doesn't support RETURNING clause, so we fetch lastrowid
# using VALUES IDENTITY_VAL_LOCAL() in post_exec.
# We should fetch it whenever:
# - The table has an autoincrement column
# - No explicit RETURNING clause was specified (self.compiled.returning)
# - Not an inline INSERT
#
# Note: We ignore implicit_returning=False on the table because:
# 1. IBM i doesn't support RETURNING syntax anyway
# 2. implicit_returning=False just means "don't use RETURNING syntax"
# 3. But we still need to return the lastrowid for test compatibility
self._select_lastrowid = (
insert_has_sequence
and not self.compiled.returning
and not self.compiled.inline
)
def post_exec(self):
conn = self.root_connection
if self._select_lastrowid:
conn._cursor_execute(self.cursor, "VALUES IDENTITY_VAL_LOCAL()", (), self)
row = self.cursor.fetchall()[0]
if row[0] is not None:
self._lastrowid = int(row[0])
# Mark this as a DML statement with no user-facing cursor
# This ensures returns_rows is False even though we fetched lastrowid
self.cursor_fetch_strategy = _cursor._NO_CURSOR_DML
def fire_sequence(self, seq, type_):
return self._execute_scalar(
"VALUES NEXTVAL FOR "
+ self.connection.dialect.identifier_preparer.format_sequence(seq),
type_,
)
def _strtobool(val):
"""Convert a string representation of truth to boolean.
This replaces distutils.util.strtobool which was removed in Python 3.12.
True values are y, yes, t, true, on and 1.
False values are n, no, f, false, off and 0.
Raises ValueError if val is anything else.
This implementation follows PEP 632 guidance for replacing distutils functions.
"""
val = str(val).lower()
if val in {'y', 'yes', 't', 'true', 'on', '1'}:
return True
elif val in {'n', 'no', 'f', 'false', 'off', '0'}:
return False
else:
raise ValueError(f"Invalid truth value: {val}")
def to_bool(obj):
if isinstance(obj, bool):
return obj
return _strtobool(obj)
class IBMiDb2Dialect(default.DefaultDialect):
driver = "pyodbc"
name = "ibmi"
max_identifier_length = 128
encoding = "utf-8"
default_paramstyle = "qmark"
colspecs = COLSPECS
ischema_names = ISCHEMA_NAMES
postfetch_lastrowid = True
supports_native_boolean = False
supports_alter = True
supports_sequences = True
sequences_optional = True
supports_sane_multi_rowcount = False
supports_sane_rowcount_returning = True
supports_native_decimal = True
requires_name_normalize = True
supports_default_values = False
supports_empty_insert = False
supports_statement_cache = True
default_isolation_level = "READ UNCOMMITTED"
statement_compiler = DB2Compiler
ddl_compiler = DB2DDLCompiler
type_compiler = DB2TypeCompiler
preparer = DB2IdentifierPreparer
execution_ctx_cls = DB2ExecutionContext
def __init__(self, isolation_level=None, fast_executemany=False, **kw):
super().__init__(**kw)
self.isolation_level = isolation_level or self.default_isolation_level
self.fast_executemany = fast_executemany
def on_connect(self):
if self.isolation_level is not None:
def connect(conn):
self.set_isolation_level(conn, self.isolation_level)
return connect
else:
return None
def initialize(self, connection):
super().initialize(connection)
self.driver_version = self._get_driver_version(connection.connection)
self.text_server_available = self._check_text_server(connection)
@reflection.cache
def get_check_constraints(self, connection, table_name, schema=None, **kw):
current_schema = self.denormalize_name(schema or self.default_schema_name)
table_name = self.denormalize_name(table_name)
# Check if table exists
if not self.has_table(connection, table_name, schema):
raise exc.NoSuchTableError(
f"Table '{table_name}' not found in schema '{current_schema}'"
)
sysconst = self.sys_table_constraints
syschkconst = self.sys_check_constraints
query = select(
syschkconst.c.conname, syschkconst.c.chkclause
).where(
and_(
syschkconst.c.conschema == sysconst.c.conschema,
syschkconst.c.conname == sysconst.c.conname,
sysconst.c.tabschema == current_schema,
sysconst.c.tabname == table_name,
)
).order_by(syschkconst.c.conname)
check_consts = []
for res in connection.execute(query):
check_consts.append(
{"name": self.normalize_name(res[0]), "sqltext": res[1]}
)
return check_consts
def get_table_comment(self, connection, table_name, schema=None, **kw):
current_schema = self.denormalize_name(schema or self.default_schema_name)
table_name = self.denormalize_name(table_name)
if current_schema:
whereclause = and_(
self.sys_tables.c.tabschema == current_schema,
self.sys_tables.c.tabname == table_name,
)
else:
whereclause = self.sys_tables.c.tabname == table_name
select_statement = select(self.sys_tables.c.tabcomment).where(whereclause)
results = connection.execute(select_statement)
return {"text": results.scalar()}
@property
def _isolation_lookup(self):
return {
# IBM i terminology
"*CHG": self.dbapi.SQL_TXN_READ_UNCOMMITTED,
"*CS": self.dbapi.SQL_TXN_READ_COMMITTED,
"*ALL": self.dbapi.SQL_TXN_REPEATABLE_READ,
"*RR": self.dbapi.SQL_TXN_SERIALIZABLE,
# ODBC terminology
"SERIALIZABLE": self.dbapi.SQL_TXN_SERIALIZABLE,
"READ UNCOMMITTED": self.dbapi.SQL_TXN_READ_UNCOMMITTED,
"READ COMMITTED": self.dbapi.SQL_TXN_READ_COMMITTED,
"REPEATABLE READ": self.dbapi.SQL_TXN_REPEATABLE_READ,
}
def get_isolation_level_values(self, dbapi_conn):
return list(self._isolation_lookup)
# Methods merged from PyODBCConnector
def get_isolation_level(self, dbapi_conn):
# Return the stored isolation level. pyodbc doesn't provide a way to
# get attributes, only set them
return self.isolation_level
def set_isolation_level(self, connection, level):
"""Set the isolation level for this connection.
This method attempts to set the isolation level using ODBC attributes.
Due to IBM i ODBC driver limitations, this may fail with error HY011 if
called during a transaction.
"""
self.isolation_level = level
level = level.replace("_", " ")
if level in self._isolation_lookup:
connection.set_attr(
self.dbapi.SQL_ATTR_TXN_ISOLATION, self._isolation_lookup[level]
)
else:
raise exc.ArgumentError(
"Invalid value '%s' for isolation_level. "
"Valid isolation levels for %s are %s"
% (level, self.name, ", ".join(self._isolation_lookup.keys()))
)
def reset_isolation_level(self, connection):
self.set_isolation_level(connection, self.default_isolation_level)
@classmethod
def import_dbapi(cls):
return __import__("pyodbc")
# Backwards compatibility alias
@classmethod
def dbapi(cls):
return cls.import_dbapi()
DRIVER_KEYWORD_MAP = {
# SQLAlchemy kwd: (ODBC keyword, type, default)
#
# NOTE: We use the upper-case driver connection string value to work
# around a bug in the the 07.01.025 driver which causes it to do
# case-sensitive lookups. This should be fixed in the 07.01.026 driver
# and older versions are not affected, but we don't have to check
# anything since they are case-insensitive and allow the all-uppercase
# values just fine.
"system": ("SYSTEM", str, None),
"user": ("UID", str, None),
"password": ("PWD", str, None),
"database": ("DATABASE", str, None),
"use_system_naming": ("NAM", to_bool, False),