Skip to content

Commit

Permalink
Types: Improve type mappings
Browse files Browse the repository at this point in the history
- Consequently use upper-case type definitions from `sqlalchemy.types`
- Add `timestamp without time zone` types (scalar and array)
- On SQLAlchemy 2, map `real` and `double{_precision}` types to the
  newly introduced `sqltypes.{DOUBLE,DOUBLE_PRECISION}` types

All of this is intended to improve reverse type lookups / reflections.
  • Loading branch information
amotl committed Jan 16, 2024
1 parent 07bba7b commit 120f8ea
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions src/sqlalchemy_cratedb/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,51 @@
from .type import ObjectArray, ObjectType

TYPES_MAP = {
"boolean": sqltypes.Boolean,
"short": sqltypes.SmallInteger,
"smallint": sqltypes.SmallInteger,
"boolean": sqltypes.BOOLEAN,
"short": sqltypes.SMALLINT,
"smallint": sqltypes.SMALLINT,
"timestamp": sqltypes.TIMESTAMP,
"timestamp with time zone": sqltypes.TIMESTAMP,
"timestamp without time zone": sqltypes.TIMESTAMP,
"object": ObjectType,
"integer": sqltypes.Integer,
"long": sqltypes.NUMERIC,
"bigint": sqltypes.NUMERIC,
"object_array": ObjectArray, # TODO: Can this also be improved to use `sqltypes.ARRAY`?
"integer": sqltypes.INTEGER,
"long": sqltypes.BIGINT,
"bigint": sqltypes.BIGINT,
"float": sqltypes.FLOAT,
"double": sqltypes.DECIMAL,
"double precision": sqltypes.DECIMAL,
"object_array": ObjectArray,
"float": sqltypes.Float,
"real": sqltypes.Float,
"string": sqltypes.String,
"text": sqltypes.String
"real": sqltypes.REAL,
"string": sqltypes.VARCHAR,
"text": sqltypes.VARCHAR,
}
try:
# SQLAlchemy >= 1.1
from sqlalchemy.types import ARRAY
TYPES_MAP["integer_array"] = ARRAY(sqltypes.Integer)
TYPES_MAP["boolean_array"] = ARRAY(sqltypes.Boolean)
TYPES_MAP["short_array"] = ARRAY(sqltypes.SmallInteger)
TYPES_MAP["smallint_array"] = ARRAY(sqltypes.SmallInteger)
TYPES_MAP["integer_array"] = ARRAY(sqltypes.INTEGER)
TYPES_MAP["boolean_array"] = ARRAY(sqltypes.BOOLEAN)
TYPES_MAP["short_array"] = ARRAY(sqltypes.SMALLINT)
TYPES_MAP["smallint_array"] = ARRAY(sqltypes.SMALLINT)
TYPES_MAP["timestamp_array"] = ARRAY(sqltypes.TIMESTAMP)
TYPES_MAP["timestamp with time zone_array"] = ARRAY(sqltypes.TIMESTAMP)
TYPES_MAP["long_array"] = ARRAY(sqltypes.NUMERIC)
TYPES_MAP["bigint_array"] = ARRAY(sqltypes.NUMERIC)
TYPES_MAP["double_array"] = ARRAY(sqltypes.DECIMAL)
TYPES_MAP["double precision_array"] = ARRAY(sqltypes.DECIMAL)
TYPES_MAP["float_array"] = ARRAY(sqltypes.Float)
TYPES_MAP["real_array"] = ARRAY(sqltypes.Float)
TYPES_MAP["string_array"] = ARRAY(sqltypes.String)
TYPES_MAP["text_array"] = ARRAY(sqltypes.String)
TYPES_MAP["timestamp without time zone_array"] = ARRAY(sqltypes.TIMESTAMP)
TYPES_MAP["long_array"] = ARRAY(sqltypes.BIGINT)
TYPES_MAP["bigint_array"] = ARRAY(sqltypes.BIGINT)
TYPES_MAP["float_array"] = ARRAY(sqltypes.FLOAT)
TYPES_MAP["real_array"] = ARRAY(sqltypes.REAL)
TYPES_MAP["string_array"] = ARRAY(sqltypes.VARCHAR)
TYPES_MAP["text_array"] = ARRAY(sqltypes.VARCHAR)
except Exception:
pass
try:
# SQLAlchemy >= 2.0
from sqlalchemy.types import DOUBLE, DOUBLE_PRECISION

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'DOUBLE' is not used.
Import of 'DOUBLE_PRECISION' is not used.
TYPES_MAP["real"] = sqltypes.DOUBLE
TYPES_MAP["real_array"] = ARRAY(sqltypes.DOUBLE)
TYPES_MAP["double"] = sqltypes.DOUBLE
TYPES_MAP["double_array"] = ARRAY(sqltypes.DOUBLE)
TYPES_MAP["double precision"] = sqltypes.DOUBLE_PRECISION
TYPES_MAP["double precision_array"] = ARRAY(sqltypes.DOUBLE_PRECISION)
except Exception:
pass

Expand Down

0 comments on commit 120f8ea

Please sign in to comment.