summaryrefslogtreecommitdiff
path: root/doc/build/core/engines.rst
blob: b6192ac6984ec80e39ed79048c8748b656ffe83e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
.. _engines_toplevel:

====================
Engine Configuration
====================

The **Engine** is the starting point for any SQLAlchemy application. It's
"home base" for the actual database and its DBAPI, delivered to the SQLAlchemy
application through a connection pool and a **Dialect**, which describes how
to talk to a specific kind of database/DBAPI combination.

The general structure can be illustrated as follows:

.. image:: sqla_engine_arch.png

Where above, an :class:`~sqlalchemy.engine.Engine` references both a
:class:`~sqlalchemy.engine.base.Dialect` and a :class:`~sqlalchemy.pool.Pool`,
which together interpret the DBAPI's module functions as well as the behavior
of the database.

Creating an engine is just a matter of issuing a single call,
:func:`.create_engine()`::

    from sqlalchemy import create_engine
    engine = create_engine('postgresql://scott:tiger@localhost:5432/mydatabase')

The above engine creates a :class:`.Dialect` object tailored towards
PostgreSQL, as well as a :class:`.Pool` object which will establish a DBAPI
connection at ``localhost:5432`` when a connection request is first received.
Note that the :class:`.Engine` and its underlying :class:`.Pool` do **not**
establish the first actual DBAPI connection until the :meth:`.Engine.connect`
method is called, or an operation which is dependent on this method such as
:meth:`.Engine.execute` is invoked. In this way, :class:`.Engine` and
:class:`.Pool` can be said to have a *lazy initialization* behavior.

The :class:`.Engine`, once created, can either be used directly to interact with the database,
or can be passed to a :class:`.Session` object to work with the ORM.   This section
covers the details of configuring an :class:`.Engine`.   The next section, :ref:`connections_toplevel`,
will detail the usage API of the :class:`.Engine` and similar, typically for non-ORM
applications.


.. _supported_dbapis:

Supported Databases
====================

SQLAlchemy includes many :class:`~sqlalchemy.engine.base.Dialect` implementations for various
backends.   Dialects for the most common databases are included with SQLAlchemy; a handful
of others require an additional install of a separate dialect.

Those dialects which are included with SQLAlchemy are described under the section :ref:`dialect_toplevel`.
All dialects additionally require that an appropriate DBAPI driver is installed.

The table below summarizes the state of DBAPI support in this version of SQLAlchemy.  The values
translate as:

* yes / Python platform - The SQLAlchemy dialect is mostly or fully operational on the target platform.
* yes / OS platform - The DBAPI supports that platform.
* no / Python platform - The DBAPI does not support that platform, or there is no SQLAlchemy dialect support.
* no / OS platform - The DBAPI does not support that platform.
* partial - the DBAPI is partially usable on the target platform but has major unresolved issues.
* development - a development version of the dialect exists, but is not yet usable.
* thirdparty - the dialect itself is maintained by a third party, who should be consulted for
  information on current support.
* \* - indicates the given DBAPI is the "default" for SQLAlchemy, i.e. when just the database name is specified

===============================================================  ===========================  ===========  ===========   ===========  =================  ============
Driver                                                           Connect string               Py2K         Py3K          Jython       Unix               Windows
===============================================================  ===========================  ===========  ===========   ===========  =================  ============
**DB2/Informix IDS**
ibm-db_                                                          See `ibm-db`_                unknown      unknown       unknown      unknown            unknown
**Drizzle** :ref:`(docs) <drizzle_toplevel>`
mysql-python_                                                    ``drizzle+mysqldb``\*        yes          development   no           yes                yes
**Firebird / Interbase** :ref:`(docs) <firebird_toplevel>`
kinterbasdb_                                                     ``firebird+kinterbasdb``\*   yes          development   no           yes                yes
**Informix** :ref:`(docs) <informix_toplevel>`
informixdb_                                                      ``informix+informixdb``\*    yes          development   no           unknown            unknown
**MaxDB** :ref:`(docs) <maxdb_toplevel>`
sapdb_                                                           ``maxdb+sapdb``\*            development  development   no           yes                unknown
**Microsoft Access**
pyodbc_                                                          See `sqlalchemy-access`_     development  development   no           unknown            yes
**Microsoft SQL Server** :ref:`(docs) <mssql_toplevel>`
adodbapi_                                                        ``mssql+adodbapi``           development  development   no           no                 yes
`jTDS JDBC Driver`_                                              ``mssql+zxjdbc``             no           no            development  yes                yes
mxodbc_                                                          ``mssql+mxodbc``             yes          development   no           yes with FreeTDS_  yes
pyodbc_                                                          ``mssql+pyodbc``\*           yes          development   no           yes with FreeTDS_  yes
pymssql_                                                         ``mssql+pymssql``            yes          development   no           yes                yes
**MySQL** :ref:`(docs) <mysql_toplevel>`
`MySQL Connector/J`_                                             ``mysql+zxjdbc``             no           no            yes          yes                yes
`MySQL Connector/Python`_                                        ``mysql+mysqlconnector``     yes          development   no           yes                yes
mysql-python_                                                    ``mysql+mysqldb``\*          yes          development   no           yes                yes
OurSQL_                                                          ``mysql+oursql``             yes          yes           no           yes                yes
pymysql_                                                         ``mysql+pymysql``            yes          development   no           yes                yes
rdbms_ (Google Appengine)                                        ``mysql+gaerdbms``           yes          development   no           no                 no
**Oracle** :ref:`(docs) <oracle_toplevel>`
cx_oracle_                                                       ``oracle+cx_oracle``\*       yes          development   no           yes                yes
`Oracle JDBC Driver`_                                            ``oracle+zxjdbc``            no           no            yes          yes                yes
**Postgresql** :ref:`(docs) <postgresql_toplevel>`
pg8000_                                                          ``postgresql+pg8000``        yes          yes           no           yes                yes
`PostgreSQL JDBC Driver`_                                        ``postgresql+zxjdbc``        no           no            yes          yes                yes
psycopg2_                                                        ``postgresql+psycopg2``\*    yes          yes           no           yes                yes
pypostgresql_                                                    ``postgresql+pypostgresql``  no           yes           no           yes                yes
**SQLite** :ref:`(docs) <sqlite_toplevel>`
pysqlite_                                                        ``sqlite+pysqlite``\*        yes          yes           no           yes                yes
sqlite3_                                                         ``sqlite+pysqlite``\*        yes          yes           no           yes                yes
**Sybase ASE** :ref:`(docs) <sybase_toplevel>`
mxodbc_                                                          ``sybase+mxodbc``            development  development   no           yes                yes
pyodbc_                                                          ``sybase+pyodbc``\*          partial      development   no           unknown            unknown
python-sybase_                                                   ``sybase+pysybase``          yes [1]_     development   no           yes                yes
===============================================================  ===========================  ===========  ===========   ===========  =================  ============

.. [1] The Sybase dialect currently lacks the ability to reflect tables.
.. _psycopg2: http://www.initd.org/
.. _pg8000: http://pybrary.net/pg8000/
.. _sqlalchemy-access: https://bitbucket.org/zzzeek/sqlalchemy-access
.. _pypostgresql: http://python.projects.postgresql.org/
.. _mysql-python: http://sourceforge.net/projects/mysql-python
.. _MySQL Connector/Python: https://launchpad.net/myconnpy
.. _OurSQL: http://packages.python.org/oursql/
.. _pymysql: http://code.google.com/p/pymysql/
.. _rdbms: https://developers.google.com/cloud-sql/docs/developers_guide_python
.. _PostgreSQL JDBC Driver: http://jdbc.postgresql.org/
.. _sqlite3: http://docs.python.org/library/sqlite3.html
.. _pysqlite: http://pypi.python.org/pypi/pysqlite/
.. _MySQL Connector/J: http://dev.mysql.com/downloads/connector/j/
.. _cx_Oracle: http://cx-oracle.sourceforge.net/
.. _Oracle JDBC Driver: http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/index.html
.. _kinterbasdb:  http://firebirdsql.org/index.php?op=devel&sub=python
.. _pyodbc: http://code.google.com/p/pyodbc/
.. _mxodbc: http://www.egenix.com/products/python/mxODBC/
.. _FreeTDS: http://www.freetds.org/
.. _adodbapi: http://adodbapi.sourceforge.net/
.. _pymssql: http://code.google.com/p/pymssql/
.. _jTDS JDBC Driver: http://jtds.sourceforge.net/
.. _ibm-db: http://code.google.com/p/ibm-db/
.. _informixdb: http://informixdb.sourceforge.net/
.. _sapdb: http://www.sapdb.org/sapdbapi.html
.. _python-sybase: http://python-sybase.sourceforge.net/

Further detail on dialects is available at :ref:`dialect_toplevel`.


.. _create_engine_args:

Engine Creation API
===================

Keyword options can also be specified to :func:`~sqlalchemy.create_engine`,
following the string URL as follows:

.. sourcecode:: python+sql

    db = create_engine('postgresql://...', encoding='latin1', echo=True)

.. autofunction:: sqlalchemy.create_engine

.. autofunction:: sqlalchemy.engine_from_config

Database Urls
=============

SQLAlchemy indicates the source of an Engine strictly via `RFC-1738
<http://rfc.net/rfc1738.html>`_ style URLs, combined with optional keyword
arguments to specify options for the Engine. The form of the URL is::

    dialect+driver://username:password@host:port/database

Dialect names include the identifying name of the SQLAlchemy dialect which
include ``sqlite``, ``mysql``, ``postgresql``, ``oracle``, ``mssql``, and
``firebird``. The drivername is the name of the DBAPI to be used to connect to
the database using all lowercase letters. If not specified, a "default" DBAPI
will be imported if available - this default is typically the most widely
known driver available for that backend (i.e. cx_oracle, pysqlite/sqlite3,
psycopg2, mysqldb). For Jython connections, specify the `zxjdbc` driver, which
is the JDBC-DBAPI bridge included with Jython.

Postgresql
----------

The Postgresql dialect uses psycopg2 as the default DBAPI::

    # default
    engine = create_engine('postgresql://scott:tiger@localhost/mydatabase')

    # psycopg2
    engine = create_engine('postgresql+psycopg2://scott:tiger@localhost/mydatabase')

    # pg8000
    engine = create_engine('postgresql+pg8000://scott:tiger@localhost/mydatabase')

    # Jython
    engine = create_engine('postgresql+zxjdbc://scott:tiger@localhost/mydatabase')

More notes on connecting to Postgresql at :ref:`postgresql_toplevel`.

MySQL
-----

The MySQL dialect uses mysql-python as the default DBAPI::

    # default
    engine = create_engine('mysql://scott:tiger@localhost/foo')

    # mysql-python
    engine = create_engine('mysql+mysqldb://scott:tiger@localhost/foo')

    # OurSQL
    engine = create_engine('mysql+oursql://scott:tiger@localhost/foo')

More notes on connecting to MySQL at :ref:`mysql_toplevel`.

Oracle
------

cx_oracle is usualjy used here::

    engine = create_engine('oracle://scott:tiger@127.0.0.1:1521/sidname')

    engine = create_engine('oracle+cx_oracle://scott:tiger@tnsname')

More notes on connecting to Oracle at :ref:`oracle_toplevel`.

Microsoft SQL Server
--------------------

There are a few drivers for SQL Server, currently PyODBC is the most solid::

    engine = create_engine('mssql+pyodbc://mydsn')

More notes on connecting to SQL Server at :ref:`mssql_toplevel`.

SQLite
------

SQLite connects to file based databases. The same URL format is used, omitting
the hostname, and using the "file" portion as the filename of the database.
This has the effect of four slashes being present for an absolute file path::

    # sqlite://<nohostname>/<path>
    # where <path> is relative:
    engine = create_engine('sqlite:///foo.db')

    # or absolute, starting with a slash:
    engine = create_engine('sqlite:////absolute/path/to/foo.db')

To use a SQLite ``:memory:`` database, specify an empty URL::

    engine = create_engine('sqlite://')

More notes on connecting to SQLite at :ref:`sqlite_toplevel`.

Others
------

See :ref:`dialect_toplevel`, the top-level page for all dialect
documentation.

URL API
--------

.. autoclass:: sqlalchemy.engine.url.URL
    :members:

Pooling
=======

The :class:`.Engine` will ask the connection pool for a
connection when the ``connect()`` or ``execute()`` methods are called. The
default connection pool, :class:`~.QueuePool`, will open connections to the
database on an as-needed basis. As concurrent statements are executed,
:class:`.QueuePool` will grow its pool of connections to a
default size of five, and will allow a default "overflow" of ten. Since the
:class:`.Engine` is essentially "home base" for the
connection pool, it follows that you should keep a single
:class:`.Engine` per database established within an
application, rather than creating a new one for each connection.

.. note::

   :class:`.QueuePool` is not used by default for SQLite engines.  See
   :ref:`sqlite_toplevel` for details on SQLite connection pool usage.

For more information on connection pooling, see :ref:`pooling_toplevel`.



.. _custom_dbapi_args:

Custom DBAPI connect() arguments
=================================

Custom arguments used when issuing the ``connect()`` call to the underlying
DBAPI may be issued in three distinct ways. String-based arguments can be
passed directly from the URL string as query arguments:

.. sourcecode:: python+sql

    db = create_engine('postgresql://scott:tiger@localhost/test?argument1=foo&argument2=bar')

If SQLAlchemy's database connector is aware of a particular query argument, it
may convert its type from string to its proper type.

:func:`~sqlalchemy.create_engine` also takes an argument ``connect_args`` which is an additional dictionary that will be passed to ``connect()``.  This can be used when arguments of a type other than string are required, and SQLAlchemy's database connector has no type conversion logic present for that parameter:

.. sourcecode:: python+sql

    db = create_engine('postgresql://scott:tiger@localhost/test', connect_args = {'argument1':17, 'argument2':'bar'})

The most customizable connection method of all is to pass a ``creator``
argument, which specifies a callable that returns a DBAPI connection:

.. sourcecode:: python+sql

    def connect():
        return psycopg.connect(user='scott', host='localhost')

    db = create_engine('postgresql://', creator=connect)



.. _dbengine_logging:

Configuring Logging
====================

Python's standard `logging
<http://docs.python.org/library/logging.html>`_ module is used to
implement informational and debug log output with SQLAlchemy. This allows
SQLAlchemy's logging to integrate in a standard way with other applications
and libraries. The ``echo`` and ``echo_pool`` flags that are present on
:func:`~sqlalchemy.create_engine`, as well as the ``echo_uow`` flag used on
:class:`~sqlalchemy.orm.session.Session`, all interact with regular loggers.

This section assumes familiarity with the above linked logging module. All
logging performed by SQLAlchemy exists underneath the ``sqlalchemy``
namespace, as used by ``logging.getLogger('sqlalchemy')``. When logging has
been configured (i.e. such as via ``logging.basicConfig()``), the general
namespace of SA loggers that can be turned on is as follows:

* ``sqlalchemy.engine`` - controls SQL echoing.  set to ``logging.INFO`` for SQL query output, ``logging.DEBUG`` for query + result set output.
* ``sqlalchemy.dialects`` - controls custom logging for SQL dialects.  See the documentation of individual dialects for details.
* ``sqlalchemy.pool`` - controls connection pool logging.  set to ``logging.INFO`` or lower to log connection pool checkouts/checkins.
* ``sqlalchemy.orm`` - controls logging of various ORM functions.  set to ``logging.INFO`` for information on mapper configurations.

For example, to log SQL queries using Python logging instead of the ``echo=True`` flag::

    import logging

    logging.basicConfig()
    logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

By default, the log level is set to ``logging.WARN`` within the entire
``sqlalchemy`` namespace so that no log operations occur, even within an
application that has logging enabled otherwise.

The ``echo`` flags present as keyword arguments to
:func:`~sqlalchemy.create_engine` and others as well as the ``echo`` property
on :class:`~sqlalchemy.engine.Engine`, when set to ``True``, will first
attempt to ensure that logging is enabled. Unfortunately, the ``logging``
module provides no way of determining if output has already been configured
(note we are referring to if a logging configuration has been set up, not just
that the logging level is set). For this reason, any ``echo=True`` flags will
result in a call to ``logging.basicConfig()`` using sys.stdout as the
destination. It also sets up a default format using the level name, timestamp,
and logger name. Note that this configuration has the affect of being
configured **in addition** to any existing logger configurations. Therefore,
**when using Python logging, ensure all echo flags are set to False at all
times**, to avoid getting duplicate log lines.

The logger name of instance such as an :class:`~sqlalchemy.engine.Engine`
or :class:`~sqlalchemy.pool.Pool` defaults to using a truncated hex identifier
string. To set this to a specific name, use the "logging_name" and
"pool_logging_name" keyword arguments with :func:`sqlalchemy.create_engine`.

.. note::

   The SQLAlchemy :class:`.Engine` conserves Python function call overhead
   by only emitting log statements when the current logging level is detected
   as ``logging.INFO`` or ``logging.DEBUG``.  It only checks this level when
   a new connection is procured from the connection pool.  Therefore when
   changing the logging configuration for an already-running application, any
   :class:`.Connection` that's currently active, or more commonly a
   :class:`~.orm.session.Session` object that's active in a transaction, won't log any
   SQL according to the new configuration until a new :class:`.Connection`
   is procured (in the case of :class:`~.orm.session.Session`, this is
   after the current transaction ends and a new one begins).