Extras

Miscellaneous helpers for working with psycopg2.

class kirameki.extras.Result(lastrowid, rowcount)

A namedtuple() representing the result of a execute() call.

class kirameki.extras.SimpleConnection

A class inheriting from connection and SimpleConnectionMixin.

class kirameki.extras.SimpleConnectionMixin

A connection mixin providing helper methods.

callproc(procname, parameters=None)

Call a single stored procedure without constructing a cursor.

See cursor.callproc().

Returns

an iterator over rows returned by the call.

callproc_one(procname, parameters=None)

Call a single stored procedure without constructing a cursor and return a single row.

See cursor.callproc(). Warns when more than one row is returned.

Returns

a single row

execute(query, vars=None)

Execute a single query without constructing a cursor.

See cursor.execute().

Returns

a namedtuple providing two fields: lastrowid and rowcount.

Return type

Result

query(query, vars=None)

Execute a single query without constructing a cursor.

See cursor.execute().

Returns

an iterator over rows returned by the call.

query_one(query, vars=None)

Execute a single query without constructing a cursor.

See cursor.execute(). Warns when more than one row is returned.

Returns

a single row

transaction(isolation_level=None, readonly=None, deferrable=None)

Start a transaction context.

See connection.set_session() for more information.

Parameters
  • isolation_level (Union[str, int], optional) – set session isolation level in this context, default behavior is to not change current isolation level i.e. None

  • readonly (bool, optional) – make session read-only in this context, default behavior is to not change current read-only state

  • deferrable (bool, optional) – make session deferrable in this context, default behavior is to not change current deferrable state

Raises

psycopg2.OperationalError – when invalid values are provided or the database state is invalid (e.g. in-transaction)

Returns

a transactional control object that is also a context manager

Return type

Transaction

transaction_class

The transaction class.

alias of kirameki.extras.Transaction

class kirameki.extras.Transaction

A transaction context manager.

begin()

Begin a transaction block. You should only call this method after calling commit() with begin=False.

commit(begin=True)

Commit the transaction.

Parameters

begin (bool, optional) – begin a new transaction if this transaction commits successfully, defaults to True

rollback()

Rollback the migration.

savepoint(name)

Start a savepoint context.

Parameters

name (str) – savepoint name

Returns

a savepoint control object that is also a context manager

savepoint_class

The savepoint class to use.

alias of kirameki.extras.Savepoint

class kirameki.extras.Savepoint

A savepoint context manager.

begin()

Create a savepoint. You should only call this method after calling commit() with begin=False.

commit(begin=True)

Release the savepoint.

Parameters

begin (bool, optional) – create a new savepoint if this savepoint is released successfully, defaults to True

rollback()

Rollback the savepoint.

savepoint(name)

Create a new, nested savepoint of the same type.

Parameters

name (str) – savepoint name

kirameki.extras.set_session(conn, **kwargs)

Set session parameters within a context.

This context manager sets given session parameters on enter and restores them to their previous values on exit.

See connection.set_session().