Skip to content

Commit 95e6158

Browse files
Fix TupleType import failure across SQLAlchemy versions (#192)
Signed-off-by: Balram Choudhary <[email protected]>
1 parent f443df5 commit 95e6158

File tree

2 files changed

+65
-32
lines changed

2 files changed

+65
-32
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ A TCP/IP connection can be specified as the following::
6969
from sqlalchemy import create_engine
7070
7171
e = create_engine("db2+ibm_db://user:pass@host[:port]/database")
72+
or
73+
e = create_engine("ibm_db_sa://user:pass@host[:port]/database")
7274
```
7375

7476
For a local socket connection, exclude the "host" and "port" portions::
@@ -77,11 +79,13 @@ For a local socket connection, exclude the "host" and "port" portions::
7779
from sqlalchemy import create_engine
7880
7981
e = create_engine("db2+ibm_db://user:pass@/database")
82+
or
83+
e = create_engine("ibm_db_sa://user:pass@/database")
8084
```
8185

8286
Supported Databases
8387
-------------------
84-
- IBM DB2 Universal Database for Linux/Unix/Windows versions 9.7 onwards
88+
- IBM DB2 Database for Linux/Unix/Windows versions 11.5 onwards
8589
- IBM Db2 on Cloud
8690
- IBM Db2 on ZOS
8791
- IBM Db2 on Iseries

ibm_db_sa/base.py

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,67 @@
3535
m = re.match(r"^\s*(\d+)\.(\d+)", SA_VERSION_STR)
3636
SA_VERSION_MM = (int(m.group(1)), int(m.group(2))) if m else (0, 0)
3737

38+
# SQLAlchemy >= 2.0
3839
if SA_VERSION_MM >= (2, 0):
39-
from sqlalchemy.sql.sqltypes import NullType, NULLTYPE, _Binary
40-
from sqlalchemy.sql.sqltypes import (
41-
ARRAY, BIGINT, BigInteger, BINARY, BLOB, BOOLEAN, Boolean,
42-
CHAR, CLOB, Concatenable, DATE, Date, DATETIME, DateTime,
43-
DECIMAL, DOUBLE, Double, DOUBLE_PRECISION, Enum, FLOAT, Float,
44-
Indexable, INT, INTEGER, Integer, Interval, JSON, LargeBinary,
45-
MatchType, NCHAR, NUMERIC, Numeric, NVARCHAR,
46-
PickleType, REAL, SchemaType, SMALLINT, SmallInteger, String,
47-
STRINGTYPE, TEXT, Text, TIME, Time, TIMESTAMP, TupleType,
48-
Unicode, UnicodeText, UUID, Uuid, VARBINARY, VARCHAR
49-
)
50-
from sqlalchemy.sql.type_api import (
51-
adapt_type, ExternalType, to_instance, TypeDecorator, TypeEngine,
52-
UserDefinedType, Variant
53-
)
40+
from sqlalchemy.sql.sqltypes import (
41+
Integer, SmallInteger, BigInteger, String, Text, Unicode, UnicodeText,
42+
Boolean, Date, Time, DateTime, Interval,
43+
Float, Numeric, DECIMAL,
44+
Enum, LargeBinary, JSON, PickleType, REAL,
45+
CHAR, VARCHAR, NCHAR, NVARCHAR, BINARY, VARBINARY,
46+
CLOB, BLOB, SchemaType, TupleType, UUID, Uuid,
47+
)
48+
from sqlalchemy.sql.type_api import (
49+
TypeEngine, TypeDecorator, UserDefinedType, Variant, ExternalType,
50+
)
51+
from sqlalchemy.sql.sqltypes import NullType
52+
# SQLAlchemy >= 1.4 and < 2.0
53+
elif SA_VERSION_MM >= (1, 4):
54+
from sqlalchemy.sql.sqltypes import (
55+
Integer, SmallInteger, BigInteger, String, Text, Unicode, UnicodeText,
56+
Boolean, Date, Time, DateTime, Interval,
57+
Float, Numeric, DECIMAL,
58+
Enum, LargeBinary, JSON, PickleType, REAL,
59+
CHAR, VARCHAR, NCHAR, NVARCHAR,
60+
BINARY, VARBINARY, CLOB, BLOB, SchemaType, TupleType,
61+
)
62+
from sqlalchemy.sql.type_api import (
63+
TypeEngine, TypeDecorator, UserDefinedType, Variant, ExternalType,
64+
)
65+
from sqlalchemy.sql.sqltypes import NullType
66+
UUID = None
67+
Uuid = None
68+
# SQLAlchemy <= 1.3
5469
else:
55-
from sqlalchemy.sql.sqltypes import NullType, NULLTYPE, _Binary
56-
from sqlalchemy.sql.sqltypes import (
57-
ARRAY, BIGINT, BigInteger, BINARY, BLOB, BOOLEAN, Boolean,
58-
CHAR, CLOB, Concatenable, DATE, Date, DATETIME, DateTime,
59-
DECIMAL, Enum, FLOAT, Float, Indexable, INT, INTEGER, Integer,
60-
Interval, JSON, LargeBinary, MatchType, NCHAR,
61-
NUMERIC, Numeric, NVARCHAR, PickleType, REAL,
62-
SchemaType, SMALLINT, SmallInteger, String, STRINGTYPE, TEXT,
63-
Text, TIME, Time, TIMESTAMP, TupleType, Unicode, UnicodeText,
64-
VARBINARY, VARCHAR
65-
)
66-
from sqlalchemy.sql.type_api import (
67-
adapt_type, ExternalType, to_instance, TypeDecorator, TypeEngine,
68-
UserDefinedType, Variant
69-
)
70+
from sqlalchemy.sql.sqltypes import (
71+
Integer, SmallInteger, BigInteger, String, Text, Unicode, UnicodeText,
72+
Boolean, Date, Time, DateTime, Interval,
73+
Float, Numeric, DECIMAL,
74+
Enum, LargeBinary, JSON, PickleType, REAL,
75+
CHAR, VARCHAR, NCHAR, NVARCHAR,
76+
BINARY, VARBINARY, CLOB, BLOB, SchemaType,
77+
)
78+
from sqlalchemy.sql.type_api import (
79+
TypeEngine, TypeDecorator, UserDefinedType, Variant,
80+
)
81+
from sqlalchemy.sql.sqltypes import NullType
82+
# Not available in SQLAlchemy 1.3
83+
TupleType = None
84+
ExternalType = None
85+
UUID = None
86+
Uuid = None
87+
88+
# Stable aliases for internal use (all SA versions)
89+
BOOLEAN = Boolean
90+
INTEGER = Integer
91+
SMALLINT = SmallInteger
92+
BIGINT = BigInteger
93+
NUMERIC = Numeric
94+
FLOAT = Float
95+
DATE = Date
96+
TIME = Time
97+
DATETIME = DateTime
98+
TIMESTAMP = DateTime
7099

71100
# as documented from:
72101
# http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0001095.htm
@@ -555,7 +584,7 @@ def visit_cast(self, cast, **kw):
555584
SMALLINT, SmallInteger,
556585
INTEGER, Integer,
557586
BIGINT, BigInteger,
558-
DECIMAL, NUMERIC, Float, REAL, DOUBLE, Double, Numeric,
587+
DECIMAL, NUMERIC, Float, REAL, DOUBLE, Numeric,
559588
DATE, Date, TIME, Time, TIMESTAMP, DateTime,
560589
BOOLEAN, Boolean,
561590
NullType

0 commit comments

Comments
 (0)