Adds support for JSONB in PostgresQL

Passes dialect to instances of `Types` for vendor-specific type mappings.
This commit is contained in:
Ben Fasoli 2020-03-25 20:29:58 -07:00
parent aeaab50043
commit 7fee6da44d
2 changed files with 15 additions and 3 deletions

View File

@ -45,9 +45,9 @@ class Database(object):
if len(schema_qs): if len(schema_qs):
schema = schema_qs.pop() schema = schema_qs.pop()
self.types = Types()
self.schema = schema self.schema = schema
self.engine = create_engine(url, **engine_kwargs) self.engine = create_engine(url, **engine_kwargs)
self.types = Types(self.engine.dialect.name)
self.url = url self.url = url
self.row_type = row_type self.row_type = row_type
self.ensure_schema = ensure_schema self.ensure_schema = ensure_schema

View File

@ -15,14 +15,26 @@ class Types(object):
boolean = Boolean boolean = Boolean
date = Date date = Date
datetime = DateTime datetime = DateTime
json = JSON
def guess(self, sample): def __init__(self, dialect = None):
self._dialect = dialect
@property
def json(self):
if self._dialect is not None and self._dialect == 'postgresql':
from sqlalchemy.dialects.postgresql import JSONB
return JSONB
return JSON
def guess(self, sample, dialect = None):
"""Given a single sample, guess the column type for the field. """Given a single sample, guess the column type for the field.
If the sample is an instance of an SQLAlchemy type, the type will be If the sample is an instance of an SQLAlchemy type, the type will be
used instead. used instead.
""" """
if dialect is not None:
self._dialect = dialect
if isinstance(sample, TypeEngine): if isinstance(sample, TypeEngine):
return sample return sample
if isinstance(sample, bool): if isinstance(sample, bool):