2017-04-15 18:27:12 +02:00
|
|
|
# engine/strategies.py
|
|
|
|
# Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
|
|
|
|
# <see AUTHORS file>
|
|
|
|
#
|
|
|
|
# This module is part of SQLAlchemy and is released under
|
|
|
|
# the MIT License: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
"""Strategies for creating new instances of Engine types.
|
|
|
|
|
|
|
|
These are semi-private implementation classes which provide the
|
|
|
|
underlying behavior for the "strategy" keyword argument available on
|
|
|
|
:func:`~sqlalchemy.engine.create_engine`. Current available options are
|
|
|
|
``plain``, ``threadlocal``, and ``mock``.
|
|
|
|
|
|
|
|
New strategies can be added via new ``EngineStrategy`` classes.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from operator import attrgetter
|
|
|
|
|
|
|
|
from sqlalchemy.engine import base, threadlocal, url
|
2017-04-15 18:27:12 +02:00
|
|
|
from sqlalchemy import util, event
|
2010-05-07 19:33:49 +02:00
|
|
|
from sqlalchemy import pool as poollib
|
2017-04-15 18:27:12 +02:00
|
|
|
from sqlalchemy.sql import schema
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
strategies = {}
|
|
|
|
|
|
|
|
|
|
|
|
class EngineStrategy(object):
|
2017-04-15 18:27:12 +02:00
|
|
|
"""An adaptor that processes input arguments and produces an Engine.
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
Provides a ``create`` method that receives input arguments and
|
|
|
|
produces an instance of base.Engine or a subclass.
|
2017-04-15 18:27:12 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
strategies[self.name] = self
|
|
|
|
|
|
|
|
def create(self, *args, **kwargs):
|
|
|
|
"""Given arguments, returns a new Engine instance."""
|
|
|
|
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class DefaultEngineStrategy(EngineStrategy):
|
2017-04-15 18:27:12 +02:00
|
|
|
"""Base class for built-in strategies."""
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
def create(self, name_or_url, **kwargs):
|
|
|
|
# create url.URL object
|
|
|
|
u = url.make_url(name_or_url)
|
|
|
|
|
2017-04-15 18:27:12 +02:00
|
|
|
plugins = u._instantiate_plugins(kwargs)
|
|
|
|
|
|
|
|
u.query.pop('plugin', None)
|
|
|
|
|
|
|
|
entrypoint = u._get_entrypoint()
|
|
|
|
dialect_cls = entrypoint.get_dialect_cls(u)
|
|
|
|
|
|
|
|
if kwargs.pop('_coerce_config', False):
|
|
|
|
def pop_kwarg(key, default=None):
|
|
|
|
value = kwargs.pop(key, default)
|
|
|
|
if key in dialect_cls.engine_config_types:
|
|
|
|
value = dialect_cls.engine_config_types[key](value)
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
pop_kwarg = kwargs.pop
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
dialect_args = {}
|
|
|
|
# consume dialect arguments from kwargs
|
|
|
|
for k in util.get_cls_kwargs(dialect_cls):
|
|
|
|
if k in kwargs:
|
2017-04-15 18:27:12 +02:00
|
|
|
dialect_args[k] = pop_kwarg(k)
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
dbapi = kwargs.pop('module', None)
|
|
|
|
if dbapi is None:
|
|
|
|
dbapi_args = {}
|
|
|
|
for k in util.get_func_kwargs(dialect_cls.dbapi):
|
|
|
|
if k in kwargs:
|
2017-04-15 18:27:12 +02:00
|
|
|
dbapi_args[k] = pop_kwarg(k)
|
2010-05-07 19:33:49 +02:00
|
|
|
dbapi = dialect_cls.dbapi(**dbapi_args)
|
|
|
|
|
|
|
|
dialect_args['dbapi'] = dbapi
|
|
|
|
|
2017-04-15 18:27:12 +02:00
|
|
|
for plugin in plugins:
|
|
|
|
plugin.handle_dialect_kwargs(dialect_cls, dialect_args)
|
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
# create dialect
|
|
|
|
dialect = dialect_cls(**dialect_args)
|
|
|
|
|
|
|
|
# assemble connection arguments
|
|
|
|
(cargs, cparams) = dialect.create_connect_args(u)
|
2017-04-15 18:27:12 +02:00
|
|
|
cparams.update(pop_kwarg('connect_args', {}))
|
|
|
|
cargs = list(cargs) # allow mutability
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
# look for existing pool or create
|
2017-04-15 18:27:12 +02:00
|
|
|
pool = pop_kwarg('pool', None)
|
2010-05-07 19:33:49 +02:00
|
|
|
if pool is None:
|
2017-04-15 18:27:12 +02:00
|
|
|
def connect(connection_record=None):
|
|
|
|
if dialect._has_events:
|
|
|
|
for fn in dialect.dispatch.do_connect:
|
|
|
|
connection = fn(
|
|
|
|
dialect, connection_record, cargs, cparams)
|
|
|
|
if connection is not None:
|
|
|
|
return connection
|
|
|
|
return dialect.connect(*cargs, **cparams)
|
|
|
|
|
|
|
|
creator = pop_kwarg('creator', connect)
|
|
|
|
|
|
|
|
poolclass = pop_kwarg('poolclass', None)
|
|
|
|
if poolclass is None:
|
|
|
|
poolclass = dialect_cls.get_pool_class(u)
|
|
|
|
pool_args = {
|
|
|
|
'dialect': dialect
|
|
|
|
}
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
# consume pool arguments from kwargs, translating a few of
|
|
|
|
# the arguments
|
|
|
|
translate = {'logging_name': 'pool_logging_name',
|
|
|
|
'echo': 'echo_pool',
|
|
|
|
'timeout': 'pool_timeout',
|
|
|
|
'recycle': 'pool_recycle',
|
2017-04-15 18:27:12 +02:00
|
|
|
'events': 'pool_events',
|
|
|
|
'use_threadlocal': 'pool_threadlocal',
|
|
|
|
'reset_on_return': 'pool_reset_on_return'}
|
2010-05-07 19:33:49 +02:00
|
|
|
for k in util.get_cls_kwargs(poolclass):
|
|
|
|
tk = translate.get(k, k)
|
|
|
|
if tk in kwargs:
|
2017-04-15 18:27:12 +02:00
|
|
|
pool_args[k] = pop_kwarg(tk)
|
|
|
|
|
|
|
|
for plugin in plugins:
|
|
|
|
plugin.handle_pool_kwargs(poolclass, pool_args)
|
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
pool = poolclass(creator, **pool_args)
|
|
|
|
else:
|
|
|
|
if isinstance(pool, poollib._DBProxy):
|
|
|
|
pool = pool.get_pool(*cargs, **cparams)
|
|
|
|
else:
|
|
|
|
pool = pool
|
|
|
|
|
2017-04-15 18:27:12 +02:00
|
|
|
pool._dialect = dialect
|
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
# create engine.
|
|
|
|
engineclass = self.engine_cls
|
|
|
|
engine_args = {}
|
|
|
|
for k in util.get_cls_kwargs(engineclass):
|
|
|
|
if k in kwargs:
|
2017-04-15 18:27:12 +02:00
|
|
|
engine_args[k] = pop_kwarg(k)
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
_initialize = kwargs.pop('_initialize', True)
|
2017-04-15 18:27:12 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
# all kwargs should be consumed
|
|
|
|
if kwargs:
|
|
|
|
raise TypeError(
|
|
|
|
"Invalid argument(s) %s sent to create_engine(), "
|
|
|
|
"using configuration %s/%s/%s. Please check that the "
|
|
|
|
"keyword arguments are appropriate for this combination "
|
|
|
|
"of components." % (','.join("'%s'" % k for k in kwargs),
|
|
|
|
dialect.__class__.__name__,
|
|
|
|
pool.__class__.__name__,
|
|
|
|
engineclass.__name__))
|
2017-04-15 18:27:12 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
engine = engineclass(pool, dialect, u, **engine_args)
|
|
|
|
|
|
|
|
if _initialize:
|
|
|
|
do_on_connect = dialect.on_connect()
|
|
|
|
if do_on_connect:
|
2017-04-15 18:27:12 +02:00
|
|
|
def on_connect(dbapi_connection, connection_record):
|
|
|
|
conn = getattr(
|
|
|
|
dbapi_connection, '_sqla_unwrap', dbapi_connection)
|
2010-05-07 19:33:49 +02:00
|
|
|
if conn is None:
|
|
|
|
return
|
|
|
|
do_on_connect(conn)
|
2017-04-15 18:27:12 +02:00
|
|
|
|
|
|
|
event.listen(pool, 'first_connect', on_connect)
|
|
|
|
event.listen(pool, 'connect', on_connect)
|
|
|
|
|
|
|
|
def first_connect(dbapi_connection, connection_record):
|
|
|
|
c = base.Connection(engine, connection=dbapi_connection,
|
|
|
|
_has_events=False)
|
|
|
|
c._execution_options = util.immutabledict()
|
2010-05-07 19:33:49 +02:00
|
|
|
dialect.initialize(c)
|
2017-04-15 18:27:12 +02:00
|
|
|
event.listen(pool, 'first_connect', first_connect, once=True)
|
|
|
|
|
|
|
|
dialect_cls.engine_created(engine)
|
|
|
|
if entrypoint is not dialect_cls:
|
|
|
|
entrypoint.engine_created(engine)
|
|
|
|
|
|
|
|
for plugin in plugins:
|
|
|
|
plugin.engine_created(engine)
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
return engine
|
|
|
|
|
|
|
|
|
|
|
|
class PlainEngineStrategy(DefaultEngineStrategy):
|
|
|
|
"""Strategy for configuring a regular Engine."""
|
|
|
|
|
|
|
|
name = 'plain'
|
|
|
|
engine_cls = base.Engine
|
2017-04-15 18:27:12 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
PlainEngineStrategy()
|
|
|
|
|
|
|
|
|
|
|
|
class ThreadLocalEngineStrategy(DefaultEngineStrategy):
|
2017-04-15 18:27:12 +02:00
|
|
|
"""Strategy for configuring an Engine with threadlocal behavior."""
|
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
name = 'threadlocal'
|
|
|
|
engine_cls = threadlocal.TLEngine
|
|
|
|
|
|
|
|
ThreadLocalEngineStrategy()
|
|
|
|
|
|
|
|
|
|
|
|
class MockEngineStrategy(EngineStrategy):
|
|
|
|
"""Strategy for configuring an Engine-like object with mocked execution.
|
|
|
|
|
|
|
|
Produces a single mock Connectable object which dispatches
|
|
|
|
statement execution to a passed-in function.
|
2017-04-15 18:27:12 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
name = 'mock'
|
2017-04-15 18:27:12 +02:00
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
def create(self, name_or_url, executor, **kwargs):
|
|
|
|
# create url.URL object
|
|
|
|
u = url.make_url(name_or_url)
|
|
|
|
|
|
|
|
dialect_cls = u.get_dialect()
|
|
|
|
|
|
|
|
dialect_args = {}
|
|
|
|
# consume dialect arguments from kwargs
|
|
|
|
for k in util.get_cls_kwargs(dialect_cls):
|
|
|
|
if k in kwargs:
|
|
|
|
dialect_args[k] = kwargs.pop(k)
|
|
|
|
|
|
|
|
# create dialect
|
|
|
|
dialect = dialect_cls(**dialect_args)
|
|
|
|
|
|
|
|
return MockEngineStrategy.MockConnection(dialect, executor)
|
|
|
|
|
|
|
|
class MockConnection(base.Connectable):
|
|
|
|
def __init__(self, dialect, execute):
|
|
|
|
self._dialect = dialect
|
|
|
|
self.execute = execute
|
|
|
|
|
|
|
|
engine = property(lambda s: s)
|
|
|
|
dialect = property(attrgetter('_dialect'))
|
|
|
|
name = property(lambda s: s._dialect.name)
|
|
|
|
|
2017-04-15 18:27:12 +02:00
|
|
|
schema_for_object = schema._schema_getter(None)
|
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
def contextual_connect(self, **kwargs):
|
|
|
|
return self
|
|
|
|
|
2017-04-15 18:27:12 +02:00
|
|
|
def execution_options(self, **kw):
|
|
|
|
return self
|
|
|
|
|
2010-05-07 19:33:49 +02:00
|
|
|
def compiler(self, statement, parameters, **kwargs):
|
|
|
|
return self._dialect.compiler(
|
|
|
|
statement, parameters, engine=self, **kwargs)
|
|
|
|
|
|
|
|
def create(self, entity, **kwargs):
|
|
|
|
kwargs['checkfirst'] = False
|
|
|
|
from sqlalchemy.engine import ddl
|
2017-04-15 18:27:12 +02:00
|
|
|
|
|
|
|
ddl.SchemaGenerator(
|
|
|
|
self.dialect, self, **kwargs).traverse_single(entity)
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
def drop(self, entity, **kwargs):
|
|
|
|
kwargs['checkfirst'] = False
|
|
|
|
from sqlalchemy.engine import ddl
|
2017-04-15 18:27:12 +02:00
|
|
|
ddl.SchemaDropper(
|
|
|
|
self.dialect, self, **kwargs).traverse_single(entity)
|
|
|
|
|
|
|
|
def _run_visitor(self, visitorcallable, element,
|
|
|
|
connection=None,
|
|
|
|
**kwargs):
|
|
|
|
kwargs['checkfirst'] = False
|
|
|
|
visitorcallable(self.dialect, self,
|
|
|
|
**kwargs).traverse_single(element)
|
2010-05-07 19:33:49 +02:00
|
|
|
|
|
|
|
def execute(self, object, *multiparams, **params):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
MockEngineStrategy()
|