Reflect views.

Currently views does not appear in the list of tables, this allows to add views.
This commit is contained in:
Simon Conseil 2015-02-18 14:45:45 +01:00
parent e5c1aa31b4
commit d5a419cbe0
2 changed files with 17 additions and 10 deletions

View File

@ -12,14 +12,21 @@ from dataset.freeze.app import freeze
__all__ = ['Database', 'Table', 'freeze', 'connect'] __all__ = ['Database', 'Table', 'freeze', 'connect']
def connect(url=None, schema=None, reflectMetadata=True, engine_kwargs=None): def connect(url=None, schema=None, reflectMetadata=True, engine_kwargs=None,
reflect_views=True):
""" """
Opens a new connection to a database. *url* can be any valid `SQLAlchemy engine URL`_. Opens a new connection to a database.
If *url* is not defined it will try to use *DATABASE_URL* from environment variable.
Returns an instance of :py:class:`Database <dataset.Database>`. Set *reflectMetadata* to False if you *url* can be any valid `SQLAlchemy engine URL`_. If *url* is not defined
don't want the entire database schema to be pre-loaded. This significantly speeds up it will try to use *DATABASE_URL* from environment variable. Returns an
connecting to large databases with lots of tables. Additionally, *engine_kwargs* will be directly passed to instance of :py:class:`Database <dataset.Database>`. Set *reflectMetadata*
SQLAlchemy, e.g. set *engine_kwargs={'pool_recycle': 3600}* will avoid `DB connection timeout`_. to False if you don't want the entire database schema to be pre-loaded.
This significantly speeds up connecting to large databases with lots of
tables. *reflect_views* can be set to False if you don't want views to be
loaded. Additionally, *engine_kwargs* will be directly passed to
SQLAlchemy, e.g. set *engine_kwargs={'pool_recycle': 3600}* will avoid `DB
connection timeout`_.
:: ::
db = dataset.connect('sqlite:///factbook.db') db = dataset.connect('sqlite:///factbook.db')
@ -34,4 +41,4 @@ def connect(url=None, schema=None, reflectMetadata=True, engine_kwargs=None):
sqlite_datetime_fix() sqlite_datetime_fix()
return Database(url, schema=schema, reflectMetadata=reflectMetadata, return Database(url, schema=schema, reflectMetadata=reflectMetadata,
engine_kwargs=engine_kwargs) engine_kwargs=engine_kwargs, reflect_views=reflect_views)

View File

@ -25,7 +25,7 @@ log = logging.getLogger(__name__)
class Database(object): class Database(object):
def __init__(self, url, schema=None, reflectMetadata=True, def __init__(self, url, schema=None, reflectMetadata=True,
engine_kwargs=None): engine_kwargs=None, reflect_views=True):
if engine_kwargs is None: if engine_kwargs is None:
engine_kwargs = {} engine_kwargs = {}
@ -50,7 +50,7 @@ class Database(object):
self.metadata = MetaData(schema=schema) self.metadata = MetaData(schema=schema)
self.metadata.bind = self.engine self.metadata.bind = self.engine
if reflectMetadata: if reflectMetadata:
self.metadata.reflect(self.engine) self.metadata.reflect(self.engine, views=reflect_views)
self._tables = {} self._tables = {}
@property @property