API
Classes, methods and stuff.
Connections
-
class momoko.Pool(dsn, connection_factory=None, size=1, callback=None, ioloop=None)
Asynchronous connection pool.
The pool manages database connections and passes operations to connections.
See momoko.Connection for documentation about the dsn and
connection_factory parameters. These are used by the connection pool when
a new connection is created.
Parameters: |
- size (integer) – Amount of connections created upon initialization. Defaults to 1.
- callback (callable) – A callable that’s called after all the connections are created. Defaults to None.
- ioloop – An instance of Tornado’s IOLoop. Defaults to None.
|
-
callproc(procname, parameters=(), cursor_factory=None, callback=None)
Call a stored database procedure with the given name.
See momoko.Connection.callproc() for documentation about the
parameters.
-
close()
Close the connection pool.
-
execute(operation, parameters=(), cursor_factory=None, callback=None)
Prepare and execute a database operation (query or command).
See momoko.Connection.execute() for documentation about the
parameters.
-
mogrify(operation, parameters=(), callback=None)
Return a query string after arguments binding.
See momoko.Connection.mogrify() for documentation about the
parameters.
-
register_hstore(unicode=False, callback=None)
Register adapter and typecaster for dict-hstore conversions.
See momoko.Connection.register_hstore() for documentation about
the parameters. This method has no globally parameter, because it
already registers hstore to all the connections in the pool.
-
transaction(statements, cursor_factory=None, callback=None)
Run a sequence of SQL queries in a database transaction.
See momoko.Connection.transaction() for documentation about the
parameters.
-
class momoko.Connection(dsn, connection_factory=None, callback=None, ioloop=None)
Create an asynchronous connection.
-
busy()
Check if the connection is busy or not.
-
callproc(procname, parameters=(), cursor_factory=None, callback=None)
Call a stored database procedure with the given name.
The sequence of parameters must contain one entry for each argument that
the procedure expects. The result of the call is returned as modified copy
of the input sequence. Input parameters are left untouched, output and
input/output parameters replaced with possibly new values.
The procedure may also provide a result set as output. This must then be
made available through the standard fetch*() methods.
Parameters: |
- procname (string) – The name of the database procedure.
- parameters (tuple/list) – A list or tuple with query parameters. See Passing parameters to SQL queries
for more information. Defaults to an empty tuple.
- cursor_factory – The cursor_factory argument can be used to create non-standard cursors.
The class returned must be a subclass of psycopg2.extensions.cursor.
See Connection and cursor factories for details. Defaults to None.
- callback (callable) – A callable that is executed when the query has finished. It must accept
two positional parameters. The first one being the cursor and the second
one None or an instance of an exception if an error has occurred,
in that case the first parameter will be None. Defaults to None.
|
-
close()
Remove the connection from the IO loop and close it.
-
closed
Indicates whether the connection is closed or not.
-
execute(operation, parameters=(), cursor_factory=None, callback=None)
Prepare and execute a database operation (query or command).
Parameters: |
- operation (string) – An SQL query.
- parameters (tuple/list) – A list or tuple with query parameters. See Passing parameters to SQL queries
for more information. Defaults to an empty tuple.
- cursor_factory – The cursor_factory argument can be used to create non-standard cursors.
The class returned must be a subclass of psycopg2.extensions.cursor.
See Connection and cursor factories for details. Defaults to None.
- callback (callable) – A callable that is executed when the query has finished. It must accept
two positional parameters. The first one being the cursor and the second
one None or an instance of an exception if an error has occurred,
in that case the first parameter will be None. Defaults to None.
|
-
mogrify(operation, parameters=(), callback=None)
Return a query string after arguments binding.
The string returned is exactly the one that would be sent to the database
running the execute() method or similar.
Parameters: |
- operation (string) – An SQL query.
- parameters (tuple/list) – A list or tuple with query parameters. See Passing parameters to SQL queries
for more information. Defaults to an empty tuple.
- callback (callable) – A callable that is executed when the query has finished. It must accept
two positional parameters. The first one being the resulting query as
a byte string and the second one None or an instance of an exception
if an error has occurred. Defaults to None.
|
-
register_hstore(globally=False, unicode=False, callback=None)
Register adapter and typecaster for dict-hstore conversions.
More information on the hstore datatype can be found on the
Psycopg2 documentation.
Parameters: |
- globally (boolean) – Register the adapter globally, not only on this connection.
- unicode (boolean) – If True, keys and values returned from the database will be unicode
instead of str. The option is not available on Python 3.
|
-
transaction(statements, cursor_factory=None, callback=None)
Run a sequence of SQL queries in a database transaction.
Parameters: |
- statements (tuple/list) –
List or tuple containing SQL queries with or without parameters. An item
can be a string (SQL query without parameters) or a tuple/list with two items,
an SQL query and a tuple/list wuth parameters.
See Passing parameters to SQL queries for more information.
- cursor_factory – The cursor_factory argument can be used to create non-standard cursors.
The class returned must be a subclass of psycopg2.extensions.cursor.
See Connection and cursor factories for details. Defaults to None.
- callback (callable) – A callable that is executed when the transaction has finished. It must accept
two positional parameters. The first one being a list of cursors in the same
order as the given statements and the second one None or an instance of
an exception if an error has occurred, in that case the first parameter is
an empty list. Defaults to None.
|
Utilities
-
class momoko.Op(func, *args, **kwargs)
Run a single asynchronous operation.
Behaves like tornado.gen.Task, but raises an exception (one of Psycop2’s
exceptions) when an error occurs related to Psycopg2 or PostgreSQL.
-
class momoko.WaitOp(key)
Return the argument passed to the result of a previous tornado.gen.Callback.
Behaves like tornado.gen.Wait, but raises an exception (one of Psycop2’s
exceptions) when an error occurs related to Psycopg2 or PostgreSQL.
-
class momoko.WaitAllOps(keys)
Return the results of multiple previous tornado.gen.Callback.
Behaves like tornado.gen.WaitAll, but raises an exception (one of Psycop2’s
exceptions) when an error occurs related to Psycopg2 or PostgreSQL.
Exceptions
-
class momoko.PoolError
The PoolError exception is raised when something goes wrong in the connection
pool. When the maximum amount is exceeded for example.