API

All public methods included in the module. See the Examples pages for some usage examples.

Client Object

class momoko.Client(settings)

The Client class is a wrapper for Pool, BatchQuery and QueryChain. It also provides the execute and callproc functions.

Parameters:settings – A dictionary that is passed to the Pool object.
batch(queries, callback)

Run a batch of queries all at once.

Note: Every query needs a free connection. So if three queries are are executed, three free connections are used.

A dictionary with queries looks like this:

{
    'query1': ['SELECT 42, 12, %s, %s;', (23, 56)],
    'query2': 'SELECT 1, 2, 3, 4, 5;',
    'query3': 'SELECT 465767, 4567, 3454;'
}

A query with paramaters is contained in a list: ['some sql here %s, %s', ('and some', 'paramaters here')]. A query without paramaters doesn’t need to be in a list.

Parameters:
  • queries – A dictionary with all the queries.
  • callback – The function that needs to be executed once all the queries are finished.
Returns:

A dictionary with the same keys as the given queries with the resulting cursors as values.

callproc(procname, parameters=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 – The name of the procedure.
  • parameters – A sequence with parameters. This is None by default.
  • callback – A callable that is executed once the procedure is finised.
chain(queries, callback)

Run a chain of queries in the given order.

A list/tuple with queries looks like this:

(
    ['SELECT 42, 12, %s, 11;', (23,)],
    'SELECT 1, 2, 3, 4, 5;'
)

A query with paramaters is contained in a list: ['some sql here %s, %s', ('and some', 'paramaters here')]. A query without paramaters doesn’t need to be in a list.

Parameters:
  • queries – A tuple or with all the queries.
  • callback – The function that needs to be executed once all the queries are finished.
Returns:

A list with the resulting cursors.

close()

Close all connections in the connection pool.

execute(operation, parameters=(), callback=None)

Prepare and execute a database operation (query or command).

Parameters may be provided as sequence or mapping and will be bound to variables in the operation. Variables are specified either with positional (%s) or named (%(name)s) placeholders. See Passing parameters to SQL queries [1] in the Psycopg2 documentation.

Parameters:
  • operation – The database operation (an SQL query or command).
  • parameters – A tuple, list or dictionary with parameters. This is an empty tuple by default.
  • callback – A callable that is executed once the operation is finised.

AdispClient Object

class momoko.AdispClient(settings)

The AdispClient class is a wrapper for Pool and uses adisp to let the developer use the execute, callproc, chain and batch functions in a blocking style. The chain and batch functions are slightly different than the two in Client.

Parameters:settings – A dictionary that is passed to the Pool object.
batch(*args, **kwargs)

Run a batch of queries all at once.

Note: Every query needs a free connection. So if three queries are are executed, three free connections are used.

A dictionary with queries looks like this:

{
    'query1': ['SELECT 42, 12, %s, %s;', (23, 56)],
    'query2': 'SELECT 1, 2, 3, 4, 5;',
    'query3': 'SELECT 465767, 4567, 3454;'
}

A query with paramaters is contained in a list: ['some sql here %s, %s', ('and some', 'paramaters here')]. A query without paramaters doesn’t need to be in a list.

Parameters:
  • queries – A dictionary with all the queries.
  • callback – The function that needs to be executed once all the queries are finished.
Returns:

A dictionary with the same keys as the given queries with the resulting cursors as values.

callproc(*args, **kwargs)

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 – The name of the procedure.
  • parameters – A sequence with parameters. This is None by default.
  • callback – A callable that is executed once the procedure is finised.
chain(*args, **kwargs)

Run a chain of queries in the given order.

A list/tuple with queries looks like this:

(
    ['SELECT 42, 12, %s, 11;', (23,)],
    'SELECT 1, 2, 3, 4, 5;'
)

A query with paramaters is contained in a list: ['some sql here %s, %s', ('and some', 'paramaters here')]. A query without paramaters doesn’t need to be in a list.

Parameters:
  • queries – A tuple or with all the queries.
  • callback – The function that needs to be executed once all the queries are finished.
Returns:

A list with the resulting cursors.

close()

Close all connections in the connection pool.

execute(*args, **kwargs)

Prepare and execute a database operation (query or command).

Parameters may be provided as sequence or mapping and will be bound to variables in the operation. Variables are specified either with positional (%s) or named (%(name)s) placeholders. See Passing parameters to SQL queries [1] in the Psycopg2 documentation.

Parameters:
  • operation – The database operation (an SQL query or command).
  • parameters – A tuple, list or dictionary with parameters. This is an empty tuple by default.
  • callback – A callable that is executed once the operation is finised.

Pool Object

class momoko.Pool(min_conn=1, max_conn=20, cleanup_timeout=10, *args, **kwargs)

A connection pool that manages PostgreSQL connections and cursors.

Parameters:
  • min_conn – The minimum amount of connections that is created when a connection pool is created.
  • max_conn – The maximum amount of connections the connection pool can have. If the amount of connections exceeds the limit a PoolError exception is raised.
  • host – The database host address (defaults to UNIX socket if not provided)
  • database – The database name
  • user – User name used to authenticate
  • password – Password used to authenticate
close()

Close all open connections in the pool.

new_cursor(function, func_args=(), callback=None, connection=None)

Create a new cursor.

If there’s no connection available, a new connection will be created and new_cursor will be called again after the connection has been made.

Parameters:
  • functionexecute, executemany or callproc.
  • func_args – A tuple with the arguments for the specified function.
  • callback – A callable that is executed once the operation is done.

QueryChain Object

class momoko.QueryChain(db, queries, callback)

Run a chain of queries in the given order.

A list/tuple with queries looks like this:

(
    ['SELECT 42, 12, %s, 11;', (23,)],
    'SELECT 1, 2, 3, 4, 5;'
)

A query with paramaters is contained in a list: ['some sql here %s, %s', ('and some', 'paramaters here')]. A query without paramaters doesn’t need to be in a list.

Parameters:
  • db – A momoko.Client or momoko.AdispClient instance.
  • queries – A tuple or with all the queries.
  • callback – The function that needs to be executed once all the queries are finished.
Returns:

A list with the resulting cursors is passed on to the callback.

BatchQuery Object

class momoko.BatchQuery(db, queries, callback)

Run a batch of queries all at once.

Note: Every query needs a free connection. So if three queries are are executed, three free connections are used.

A dictionary with queries looks like this:

{
    'query1': ['SELECT 42, 12, %s, %s;', (23, 56)],
    'query2': 'SELECT 1, 2, 3, 4, 5;',
    'query3': 'SELECT 465767, 4567, 3454;'
}

A query with paramaters is contained in a list: ['some sql here %s, %s', ('and some', 'paramaters here')]. A query without paramaters doesn’t need to be in a list.

Parameters:
  • db – A momoko.Client or momoko.AdispClient instance.
  • queries – A dictionary with all the queries.
  • callback – The function that needs to be executed once all the queries are finished.
Returns:

A dictionary with the same keys as the given queries with the resulting cursors as values is passed on to the callback.

Poller Object

class momoko.Poller(connection, callbacks=())

A poller that polls the PostgreSQL connection and calls the callbacks when the connection state is POLL_OK.

Parameters:
  • connection – The connection that needs to be polled.
  • callbacks – A tuple/list of callbacks.
start()

Start polling the connection.

Table Of Contents

Previous topic

Examples

This Page