| 1 |
""" |
|---|
| 2 |
|
|---|
| 3 |
Introduction |
|---|
| 4 |
============ |
|---|
| 5 |
|
|---|
| 6 |
B{sAsync} is an enhancement to the SQLAlchemy package that provides persistent |
|---|
| 7 |
dictionaries, text indexing and searching, and an access broker for |
|---|
| 8 |
conveniently managing database access, table setup, and |
|---|
| 9 |
transactions. Everything can be run in an asynchronous fashion using the |
|---|
| 10 |
Twisted framework and its deferred processing capabilities. |
|---|
| 11 |
|
|---|
| 12 |
Copyright (C) 2006-2007 by Edwin A. Suominen, U{http://www.eepatents.com} |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
Licensing |
|---|
| 16 |
========= |
|---|
| 17 |
|
|---|
| 18 |
This program is free software; you can redistribute it and/or modify it under |
|---|
| 19 |
the terms of the GNU General Public License (GPL) as published by the Free |
|---|
| 20 |
Software Foundation; either version 2 of the License, or (at your option) any |
|---|
| 21 |
later version. |
|---|
| 22 |
|
|---|
| 23 |
This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 24 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 25 |
FOR A PARTICULAR PURPOSE. See the file COPYING for more details. |
|---|
| 26 |
|
|---|
| 27 |
You should have received a copy of the GPL along with this program; if not, |
|---|
| 28 |
write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|---|
| 29 |
Boston, MA 02110-1301, USA. |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
Usage |
|---|
| 33 |
===== |
|---|
| 34 |
|
|---|
| 35 |
sAsync wraps your SQLAlchemy database access code inside asynchronous |
|---|
| 36 |
transactions. At the lowest level, it provides a @transact decorator for |
|---|
| 37 |
your database-access methods that makes them immediately return a Twisted |
|---|
| 38 |
Deferred object. |
|---|
| 39 |
|
|---|
| 40 |
For example, suppose you want to run a method that selects a list of row |
|---|
| 41 |
objects from a table. Instead of waiting around for your method to return the |
|---|
| 42 |
list, blocking everything else your program is trying to do, you decorate it |
|---|
| 43 |
with @transact and run it. It immediately hands you a Deferred, and |
|---|
| 44 |
you scribble the name of your callback function on it, handing the Deferred |
|---|
| 45 |
back to your decorated method. (You can also keep a copy of it (i.e., a |
|---|
| 46 |
reference to the object) around if you like, for example to add other |
|---|
| 47 |
callbacks.) |
|---|
| 48 |
|
|---|
| 49 |
Once you've attached your callback function to the Deferred result, you can |
|---|
| 50 |
go on with your business, knowing that SQLAlchemy will be cranking away behind |
|---|
| 51 |
the scenes (in a transaction-specific thread) to obtain a result for you. When |
|---|
| 52 |
the result is finally ready, your transact-decorated method will look at the |
|---|
| 53 |
Deferred, see the note you scribbled on it (Pls call this function with |
|---|
| 54 |
the result. Thx!), and give your function a call with the list of rows. It |
|---|
| 55 |
will supply the callback with the list as the function's argument. |
|---|
| 56 |
|
|---|
| 57 |
You can also do some asynchronous database operations on a higher level. For |
|---|
| 58 |
example, you can maintain a store of Python objects, with each object |
|---|
| 59 |
accessible (with deferred results) via a unique key. If that sounds like what a |
|---|
| 60 |
dictionary does, it should! The ''sAsync'' package also provides a |
|---|
| 61 |
dictionary-like object with database-persistent items that you can access in an |
|---|
| 62 |
asynchronous fashion. |
|---|
| 63 |
|
|---|
| 64 |
And, someday, someone will get the full-text searching capabilities working. Of |
|---|
| 65 |
course, the results of your potentially time-consuming searches will be done in |
|---|
| 66 |
the same asynchronous fashion. |
|---|
| 67 |
|
|---|
| 68 |
""" |
|---|
| 69 |
|
|---|
| 70 |
def engine(url, **kw): |
|---|
| 71 |
""" |
|---|
| 72 |
Specifies the parameters for creating an SQLAlchemy database engine that |
|---|
| 73 |
will be used as a default for all instances of L{AccessBroker} and all |
|---|
| 74 |
persistent objects based thereon. |
|---|
| 75 |
|
|---|
| 76 |
@param url: An RFC-1738 url to a database connection. |
|---|
| 77 |
|
|---|
| 78 |
@keyword strategy: The Strategy describes the general configuration used to |
|---|
| 79 |
create this Engine. The two available values are plain, which is the |
|---|
| 80 |
default, and threadlocal, which applies a 'thread-local context' to |
|---|
| 81 |
implicit executions performed by the Engine. This context is further |
|---|
| 82 |
described in Implicit Connection Contexts. |
|---|
| 83 |
|
|---|
| 84 |
@type strategy: 'plain'. |
|---|
| 85 |
|
|---|
| 86 |
@keyword pool: An instance of sqlalchemy.pool.Pool to be used as the |
|---|
| 87 |
underlying source for connections, overriding the engine's connect |
|---|
| 88 |
arguments (pooling is described in Connection Pooling). If C{None}, a |
|---|
| 89 |
default Pool (usually QueuePool, or SingletonThreadPool in the case of |
|---|
| 90 |
SQLite) will be created using the engine's connect arguments. |
|---|
| 91 |
|
|---|
| 92 |
@type pool: C{None} |
|---|
| 93 |
|
|---|
| 94 |
@keyword pool_size: The number of connections to keep open inside the |
|---|
| 95 |
connection pool. This is only used with QueuePool. |
|---|
| 96 |
|
|---|
| 97 |
@type pool_size: 5 |
|---|
| 98 |
|
|---|
| 99 |
@keyword max_overflow: The number of connections to allow in 'overflow,' |
|---|
| 100 |
that is connections that can be opened above and beyond the initial |
|---|
| 101 |
five. This is only used with QueuePool. |
|---|
| 102 |
|
|---|
| 103 |
@type max_overflow: 10 |
|---|
| 104 |
|
|---|
| 105 |
@keyword pool_timeout: number of seconds to wait before giving up on |
|---|
| 106 |
getting a connection from the pool. This is only used with QueuePool. |
|---|
| 107 |
|
|---|
| 108 |
@type pool_timeout: 30 |
|---|
| 109 |
|
|---|
| 110 |
@keyword echo: if C{True}, the Engine will log all statements as well as a |
|---|
| 111 |
repr() of their parameter lists to the engines logger, which defaults |
|---|
| 112 |
to sys.stdout. The echo attribute of ComposedSQLEngine can be modified |
|---|
| 113 |
at any time to turn logging on and off. If set to the string 'debug', |
|---|
| 114 |
result rows will be printed to the standard output as well. |
|---|
| 115 |
|
|---|
| 116 |
@type echo: C{False} |
|---|
| 117 |
|
|---|
| 118 |
@keyword logger: a file-like object where logging output can be sent, if |
|---|
| 119 |
echo is set to C{True}. Newlines will not be sent with log messages. This |
|---|
| 120 |
defaults to an internal logging object which references sys.stdout. |
|---|
| 121 |
|
|---|
| 122 |
@type logger: C{None} |
|---|
| 123 |
|
|---|
| 124 |
@keyword module: used by database implementations which support multiple |
|---|
| 125 |
DBAPI modules, this is a reference to a DBAPI2 module to be used |
|---|
| 126 |
instead of the engine's default module. For Postgres, the default is |
|---|
| 127 |
psycopg2, or psycopg1 if 2 cannot be found. For Oracle, its cx_Oracle. |
|---|
| 128 |
|
|---|
| 129 |
@type module: C{None} |
|---|
| 130 |
|
|---|
| 131 |
@keyword use_ansi: used only by Oracle; when C{False}, the Oracle driver |
|---|
| 132 |
attempts to support a particular 'quirk' of Oracle versions 8 and |
|---|
| 133 |
previous, that the LEFT OUTER JOIN SQL syntax is not supported, and the |
|---|
| 134 |
'Oracle join' syntax of using <column1>(+)=<column2> must be used in |
|---|
| 135 |
order to achieve a LEFT OUTER JOIN. |
|---|
| 136 |
|
|---|
| 137 |
@type use_ansi: C{True} |
|---|
| 138 |
|
|---|
| 139 |
@keyword threaded: used by cx_Oracle; sets the threaded parameter of the |
|---|
| 140 |
connection indicating thread-safe usage. cx_Oracle docs indicate |
|---|
| 141 |
setting this flag to C{False} will speed performance by 10-15%. While this |
|---|
| 142 |
defaults to C{False} in cx_Oracle, SQLAlchemy defaults it to C{True}, |
|---|
| 143 |
preferring stability over early optimization. |
|---|
| 144 |
|
|---|
| 145 |
@type threaded: C{True} |
|---|
| 146 |
|
|---|
| 147 |
@keyword use_oids: used only by Postgres, will enable the column name 'oid' |
|---|
| 148 |
as the object ID column, which is also used for the default sort order |
|---|
| 149 |
of tables. Postgres as of 8.1 has object IDs disabled by default. |
|---|
| 150 |
|
|---|
| 151 |
@type use_oids: C{False} |
|---|
| 152 |
|
|---|
| 153 |
@keyword convert_unicode: if set to C{True}, all String/character based types |
|---|
| 154 |
will convert Unicode values to raw byte values going into the database, |
|---|
| 155 |
and all raw byte values to Python Unicode coming out in result |
|---|
| 156 |
sets. This is an engine-wide method to provide unicode across the |
|---|
| 157 |
board. For unicode conversion on a column-by-column level, use the |
|---|
| 158 |
Unicode column type instead. |
|---|
| 159 |
|
|---|
| 160 |
@type convert_unicode: C{False} |
|---|
| 161 |
|
|---|
| 162 |
@keyword encoding: the encoding to use for all Unicode translations, both |
|---|
| 163 |
by engine-wide unicode conversion as well as the Unicode type object. |
|---|
| 164 |
|
|---|
| 165 |
@type encoding: 'utf-8' |
|---|
| 166 |
|
|---|
| 167 |
""" |
|---|
| 168 |
from database import AccessBroker |
|---|
| 169 |
AccessBroker.globalParams = url, kw |
|---|
| 170 |
|
|---|