def load_dialect_impl(self, dialect): """ Get the dialect-specific underlying column type. """ if isinstance(dialect, sqlalchemy.dialects.postgresql.base.dialect): return PGUuid() elif isinstance(dialect, sqlalchemy.dialects.sqlite.base.dialect): # using string here for convenience; suboptimal return String(length = 32) else: # FIXME "small binary" type? return LargeBinary(length = 16)
def get_sqlalchemy_type(cls): db_type = cls.Attributes.db_type if db_type is not None: return db_type # must be above Unicode, because Uuid is Unicode's subclass if issubclass(cls, Uuid): return PGUuid(as_uuid=True) # must be above Unicode, because Point is Unicode's subclass elif issubclass(cls, Point): return PGGeometry("POINT", dimension=cls.Attributes.dim) # must be above Unicode, because Line is Unicode's subclass elif issubclass(cls, Line): return PGGeometry("LINESTRING", dimension=cls.Attributes.dim) # must be above Unicode, because Polygon is Unicode's subclass elif issubclass(cls, Polygon): return PGGeometry("POLYGON", dimension=cls.Attributes.dim) # must be above Unicode, because MultiPoint is Unicode's subclass elif issubclass(cls, MultiPoint): return PGGeometry("MULTIPOINT", dimension=cls.Attributes.dim) # must be above Unicode, because MultiLine is Unicode's subclass elif issubclass(cls, MultiLine): return PGGeometry("MULTILINESTRING", dimension=cls.Attributes.dim) # must be above Unicode, because MultiPolygon is Unicode's subclass elif issubclass(cls, MultiPolygon): return PGGeometry("MULTIPOLYGON", dimension=cls.Attributes.dim) # must be above Unicode, because String is Unicode's subclass elif issubclass(cls, String): if cls.Attributes.max_len == String.Attributes.max_len: # Default is arbitrary-length return sqlalchemy.Text else: return sqlalchemy.String(cls.Attributes.max_len) elif issubclass(cls, Unicode): if cls.Attributes.max_len == Unicode.Attributes.max_len: # Default is arbitrary-length return sqlalchemy.UnicodeText else: return sqlalchemy.Unicode(cls.Attributes.max_len) elif issubclass(cls, EnumBase): return sqlalchemy.Enum(*cls.__values__, name=cls.__type_name__) elif issubclass(cls, AnyXml): return PGXml elif issubclass(cls, AnyHtml): return PGHtml elif issubclass(cls, AnyDict): sa = cls.Attributes.store_as if isinstance(sa, c_json): return PGJson raise NotImplementedError(dict(cls=AnyDict, store_as=sa)) elif issubclass(cls, ByteArray): return sqlalchemy.LargeBinary elif issubclass(cls, (Integer64, UnsignedInteger64)): return sqlalchemy.BigInteger elif issubclass(cls, (Integer32, UnsignedInteger32)): return sqlalchemy.Integer elif issubclass(cls, (Integer16, UnsignedInteger16)): return sqlalchemy.SmallInteger elif issubclass(cls, (Integer8, UnsignedInteger8)): return sqlalchemy.SmallInteger elif issubclass(cls, Float): return FLOAT elif issubclass(cls, Double): return DOUBLE_PRECISION elif issubclass(cls, (Integer, UnsignedInteger)): return sqlalchemy.DECIMAL elif issubclass(cls, Decimal): return sqlalchemy.DECIMAL elif issubclass(cls, Boolean): return sqlalchemy.Boolean elif issubclass(cls, Date): return sqlalchemy.Date elif issubclass(cls, DateTime): if cls.Attributes.timezone is None: if cls.Attributes.as_time_zone is None: return sqlalchemy.DateTime(timezone=True) else: return sqlalchemy.DateTime(timezone=False) else: return sqlalchemy.DateTime(timezone=cls.Attributes.timezone) elif issubclass(cls, Time): return sqlalchemy.Time elif issubclass(cls, XmlModifier): retval = get_sqlalchemy_type(cls.type) return retval
def _get_sqlalchemy_type(cls): db_type = cls.Attributes.db_type if db_type is not None: return db_type # must be above Unicode, because Ltree is Unicode's subclass if issubclass(cls, Ltree): return PGLTree # must be above Unicode, because Ip*Address is Unicode's subclass if issubclass(cls, (IpAddress, Ipv4Address, Ipv6Address)): return PGInet # must be above Unicode, because Uuid is Unicode's subclass if issubclass(cls, Uuid): return PGUuid(as_uuid=True) # must be above Unicode, because Point is Unicode's subclass if issubclass(cls, Point): return PGGeometry("POINT", dimension=cls.Attributes.dim) # must be above Unicode, because Line is Unicode's subclass if issubclass(cls, Line): return PGGeometry("LINESTRING", dimension=cls.Attributes.dim) # must be above Unicode, because Polygon is Unicode's subclass if issubclass(cls, Polygon): return PGGeometry("POLYGON", dimension=cls.Attributes.dim) # must be above Unicode, because MultiPoint is Unicode's subclass if issubclass(cls, MultiPoint): return PGGeometry("MULTIPOINT", dimension=cls.Attributes.dim) # must be above Unicode, because MultiLine is Unicode's subclass if issubclass(cls, MultiLine): return PGGeometry("MULTILINESTRING", dimension=cls.Attributes.dim) # must be above Unicode, because MultiPolygon is Unicode's subclass if issubclass(cls, MultiPolygon): return PGGeometry("MULTIPOLYGON", dimension=cls.Attributes.dim) # must be above Unicode, because String is Unicode's subclass if issubclass(cls, String): if cls.Attributes.max_len == String.Attributes.max_len: # Default is arbitrary-length return sqlalchemy.Text else: return sqlalchemy.String(cls.Attributes.max_len) if issubclass(cls, Unicode): if cls.Attributes.max_len == Unicode.Attributes.max_len: # Default is arbitrary-length return sqlalchemy.UnicodeText else: return sqlalchemy.Unicode(cls.Attributes.max_len) if issubclass(cls, EnumBase): return sqlalchemy.Enum(*cls.__values__, name=cls.__type_name__) if issubclass(cls, AnyXml): return PGXml if issubclass(cls, AnyHtml): return PGHtml if issubclass(cls, (Any, AnyDict)): sa = cls.Attributes.store_as if sa is None: return None if isinstance(sa, c_json): return PGJson if isinstance(sa, c_jsonb): return PGJsonB raise NotImplementedError(dict(cls=cls, store_as=sa)) if issubclass(cls, ByteArray): return sqlalchemy.LargeBinary if issubclass(cls, (Integer64, UnsignedInteger64)): return sqlalchemy.BigInteger if issubclass(cls, (Integer32, UnsignedInteger32)): return sqlalchemy.Integer if issubclass(cls, (Integer16, UnsignedInteger16)): return sqlalchemy.SmallInteger if issubclass(cls, (Integer8, UnsignedInteger8)): return sqlalchemy.SmallInteger if issubclass(cls, Float): return FLOAT if issubclass(cls, Double): return DOUBLE_PRECISION if issubclass(cls, (Integer, UnsignedInteger)): return sqlalchemy.DECIMAL if issubclass(cls, Decimal): return sqlalchemy.DECIMAL if issubclass(cls, Boolean): if cls.Attributes.store_as is bool: return sqlalchemy.Boolean if cls.Attributes.store_as is int: return sqlalchemy.SmallInteger raise ValueError("Boolean.store_as has invalid value %r" % cls.Attributes.store_as) if issubclass(cls, Date): return sqlalchemy.Date if issubclass(cls, DateTime): if cls.Attributes.timezone is None: if cls.Attributes.as_timezone is None: return sqlalchemy.DateTime(timezone=True) else: return sqlalchemy.DateTime(timezone=False) else: return sqlalchemy.DateTime(timezone=cls.Attributes.timezone) if issubclass(cls, Time): return sqlalchemy.Time if issubclass(cls, Duration): return sqlalchemy.dialects.postgresql.base.INTERVAL if issubclass(cls, XmlModifier): retval = _get_sqlalchemy_type(cls.type) return retval