PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/skink/lib/sqlalchemy/engine/__init__.py

http://github.com/heynemann/skink
Python | 260 lines | 249 code | 1 blank | 10 comment | 0 complexity | 20f83a14e3fe6a8c3e3b183edc93a994 MD5 | raw file
  1. # engine/__init__.py
  2. # Copyright (C) 2005, 2006, 2007, 2008, 2009 Michael Bayer mike_mp@zzzcomputing.com
  3. #
  4. # This module is part of SQLAlchemy and is released under
  5. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. """SQL connections, SQL execution and high-level DB-API interface.
  7. The engine package defines the basic components used to interface
  8. DB-API modules with higher-level statement construction,
  9. connection-management, execution and result contexts. The primary
  10. "entry point" class into this package is the Engine and it's public
  11. constructor ``create_engine()``.
  12. This package includes:
  13. base.py
  14. Defines interface classes and some implementation classes which
  15. comprise the basic components used to interface between a DB-API,
  16. constructed and plain-text statements, connections, transactions,
  17. and results.
  18. default.py
  19. Contains default implementations of some of the components defined
  20. in base.py. All current database dialects use the classes in
  21. default.py as base classes for their own database-specific
  22. implementations.
  23. strategies.py
  24. The mechanics of constructing ``Engine`` objects are represented
  25. here. Defines the ``EngineStrategy`` class which represents how
  26. to go from arguments specified to the ``create_engine()``
  27. function, to a fully constructed ``Engine``, including
  28. initialization of connection pooling, dialects, and specific
  29. subclasses of ``Engine``.
  30. threadlocal.py
  31. The ``TLEngine`` class is defined here, which is a subclass of
  32. the generic ``Engine`` and tracks ``Connection`` and
  33. ``Transaction`` objects against the identity of the current
  34. thread. This allows certain programming patterns based around
  35. the concept of a "thread-local connection" to be possible.
  36. The ``TLEngine`` is created by using the "threadlocal" engine
  37. strategy in conjunction with the ``create_engine()`` function.
  38. url.py
  39. Defines the ``URL`` class which represents the individual
  40. components of a string URL passed to ``create_engine()``. Also
  41. defines a basic module-loading strategy for the dialect specifier
  42. within a URL.
  43. """
  44. import sqlalchemy.databases
  45. from sqlalchemy.engine.base import (
  46. BufferedColumnResultProxy,
  47. BufferedColumnRow,
  48. BufferedRowResultProxy,
  49. Compiled,
  50. Connectable,
  51. Connection,
  52. DefaultRunner,
  53. Dialect,
  54. Engine,
  55. ExecutionContext,
  56. NestedTransaction,
  57. ResultProxy,
  58. RootTransaction,
  59. RowProxy,
  60. SchemaIterator,
  61. Transaction,
  62. TwoPhaseTransaction
  63. )
  64. from sqlalchemy.engine import strategies
  65. from sqlalchemy import util
  66. __all__ = (
  67. 'BufferedColumnResultProxy',
  68. 'BufferedColumnRow',
  69. 'BufferedRowResultProxy',
  70. 'Compiled',
  71. 'Connectable',
  72. 'Connection',
  73. 'DefaultRunner',
  74. 'Dialect',
  75. 'Engine',
  76. 'ExecutionContext',
  77. 'NestedTransaction',
  78. 'ResultProxy',
  79. 'RootTransaction',
  80. 'RowProxy',
  81. 'SchemaIterator',
  82. 'Transaction',
  83. 'TwoPhaseTransaction',
  84. 'create_engine',
  85. 'engine_from_config',
  86. )
  87. default_strategy = 'plain'
  88. def create_engine(*args, **kwargs):
  89. """Create a new Engine instance.
  90. The standard method of specifying the engine is via URL as the
  91. first positional argument, to indicate the appropriate database
  92. dialect and connection arguments, with additional keyword
  93. arguments sent as options to the dialect and resulting Engine.
  94. The URL is a string in the form
  95. ``dialect://user:password@host/dbname[?key=value..]``, where
  96. ``dialect`` is a name such as ``mysql``, ``oracle``, ``postgres``,
  97. etc. Alternatively, the URL can be an instance of
  98. :class:`~sqlalchemy.engine.url.URL`.
  99. `**kwargs` takes a wide variety of options which are routed
  100. towards their appropriate components. Arguments may be
  101. specific to the Engine, the underlying Dialect, as well as the
  102. Pool. Specific dialects also accept keyword arguments that
  103. are unique to that dialect. Here, we describe the parameters
  104. that are common to most ``create_engine()`` usage.
  105. :param assert_unicode=False: When set to ``True`` alongside
  106. convert_unicode=``True``, asserts that incoming string bind
  107. parameters are instances of ``unicode``, otherwise raises an
  108. error. Only takes effect when ``convert_unicode==True``. This
  109. flag is also available on the ``String`` type and its
  110. descendants. New in 0.4.2.
  111. :param connect_args: a dictionary of options which will be
  112. passed directly to the DBAPI's ``connect()`` method as
  113. additional keyword arguments.
  114. :param convert_unicode=False: if set to True, all
  115. String/character based types will convert Unicode values to raw
  116. byte values going into the database, and all raw byte values to
  117. Python Unicode coming out in result sets. This is an
  118. engine-wide method to provide unicode conversion across the
  119. board. For unicode conversion on a column-by-column level, use
  120. the ``Unicode`` column type instead, described in `types`.
  121. :param creator: a callable which returns a DBAPI connection.
  122. This creation function will be passed to the underlying
  123. connection pool and will be used to create all new database
  124. connections. Usage of this function causes connection
  125. parameters specified in the URL argument to be bypassed.
  126. :param echo=False: if True, the Engine will log all statements
  127. as well as a repr() of their parameter lists to the engines
  128. logger, which defaults to sys.stdout. The ``echo`` attribute of
  129. ``Engine`` can be modified at any time to turn logging on and
  130. off. If set to the string ``"debug"``, result rows will be
  131. printed to the standard output as well. This flag ultimately
  132. controls a Python logger; see `dbengine_logging` at the end of
  133. this chapter for information on how to configure logging
  134. directly.
  135. :param echo_pool=False: if True, the connection pool will log
  136. all checkouts/checkins to the logging stream, which defaults to
  137. sys.stdout. This flag ultimately controls a Python logger; see
  138. `dbengine_logging` for information on how to configure logging
  139. directly.
  140. :param encoding='utf-8': the encoding to use for all Unicode
  141. translations, both by engine-wide unicode conversion as well as
  142. the ``Unicode`` type object.
  143. :param label_length=None: optional integer value which limits
  144. the size of dynamically generated column labels to that many
  145. characters. If less than 6, labels are generated as
  146. "_(counter)". If ``None``, the value of
  147. ``dialect.max_identifier_length`` is used instead.
  148. :param module=None: used by database implementations which
  149. support multiple DBAPI modules, this is a reference to a DBAPI2
  150. module to be used instead of the engine's default module. For
  151. PostgreSQL, the default is psycopg2. For Oracle, it's cx_Oracle.
  152. :param pool=None: an already-constructed instance of
  153. :class:`~sqlalchemy.pool.Pool`, such as a
  154. :class:`~sqlalchemy.pool.QueuePool` instance. If non-None, this
  155. pool will be used directly as the underlying connection pool
  156. for the engine, bypassing whatever connection parameters are
  157. present in the URL argument. For information on constructing
  158. connection pools manually, see `pooling`.
  159. :param poolclass=None: a :class:`~sqlalchemy.pool.Pool`
  160. subclass, which will be used to create a connection pool
  161. instance using the connection parameters given in the URL. Note
  162. this differs from ``pool`` in that you don't actually
  163. instantiate the pool in this case, you just indicate what type
  164. of pool to be used.
  165. :param max_overflow=10: the number of connections to allow in
  166. connection pool "overflow", that is connections that can be
  167. opened above and beyond the pool_size setting, which defaults
  168. to five. this is only used with :class:`~sqlalchemy.pool.QueuePool`.
  169. :param pool_size=5: the number of connections to keep open
  170. inside the connection pool. This used with :class:`~sqlalchemy.pool.QueuePool` as
  171. well as :class:`~sqlalchemy.pool.SingletonThreadPool`.
  172. :param pool_recycle=-1: this setting causes the pool to recycle
  173. connections after the given number of seconds has passed. It
  174. defaults to -1, or no timeout. For example, setting to 3600
  175. means connections will be recycled after one hour. Note that
  176. MySQL in particular will ``disconnect automatically`` if no
  177. activity is detected on a connection for eight hours (although
  178. this is configurable with the MySQLDB connection itself and the
  179. server configuration as well).
  180. :param pool_timeout=30: number of seconds to wait before giving
  181. up on getting a connection from the pool. This is only used
  182. with :class:`~sqlalchemy.pool.QueuePool`.
  183. :param strategy='plain': used to invoke alternate :class:`~sqlalchemy.engine.base.Engine.`
  184. implementations. Currently available is the ``threadlocal``
  185. strategy, which is described in :ref:`threadlocal_strategy`.
  186. """
  187. strategy = kwargs.pop('strategy', default_strategy)
  188. strategy = strategies.strategies[strategy]
  189. return strategy.create(*args, **kwargs)
  190. def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs):
  191. """Create a new Engine instance using a configuration dictionary.
  192. The dictionary is typically produced from a config file where keys
  193. are prefixed, such as sqlalchemy.url, sqlalchemy.echo, etc. The
  194. 'prefix' argument indicates the prefix to be searched for.
  195. A select set of keyword arguments will be "coerced" to their
  196. expected type based on string values. In a future release, this
  197. functionality will be expanded and include dialect-specific
  198. arguments.
  199. """
  200. opts = _coerce_config(configuration, prefix)
  201. opts.update(kwargs)
  202. url = opts.pop('url')
  203. return create_engine(url, **opts)
  204. def _coerce_config(configuration, prefix):
  205. """Convert configuration values to expected types."""
  206. options = dict((key[len(prefix):], configuration[key])
  207. for key in configuration
  208. if key.startswith(prefix))
  209. for option, type_ in (
  210. ('convert_unicode', bool),
  211. ('pool_timeout', int),
  212. ('echo', bool),
  213. ('echo_pool', bool),
  214. ('pool_recycle', int),
  215. ('pool_size', int),
  216. ('max_overflow', int),
  217. ('pool_threadlocal', bool),
  218. ):
  219. util.coerce_kw_type(options, option, type_)
  220. return options