breezeblocks package

Submodules

breezeblocks.database module

class breezeblocks.database.Database(dbapi_module=None, dsn=None, *, connect_args=None, connect_kwargs=None, on_connect=None, minconn=10, maxconn=20)

Bases: object

Proxies the database at the URI provided.

Refer to your DBAPI module documentation for what the content of connect_args and connect_kwargs should be.

Parameters:
  • dbapi_module – The DBAPI 2.0 module to use for this database.
  • dsn – The DSN (connection string) for this database. This will be pre-pended to connect_args if present.
  • connect_args*args for calls to dbapi.connect.
  • connect_kwargs**kwargs for calls to dbapi.connect.
  • on_connect – A SQL script to be executed per-connection. If provided, it is executed each time a connection is taken out of the connection pool.
  • minconn – Number of standby connections for this database.
  • maxconn – Limit on open connections to this database.
connect()

Returns a new connection to the database.

delete(table)

Starts building a delete in this database.

Parameters:table – The table to delete rows from.
Returns:A delete builder for the table provided.
insert(table, columns=[])

Starts building an insert in this database.

Parameters:
  • table – The table to insert into.
  • columns – A list of the columns to set values of.
Returns:

An Insert builder for the table and columns provided.

query(*queryables)

Starts building a query in this database.

Any arguments should be selectable expressions, such as columns or values that should end up in the result rows of the query.

update(table)

Starts building an update in this database.

Parameters:table – The table to update rows of.
Returns:An Update builder for the table provided.

breezeblocks.dml_builders module

class breezeblocks.dml_builders.DeleteBuilder(table, db=None)

Bases: object

get()

Get a delete object for the current state of the builder.

Returns:A finished, executable Delete.
where(*conditions)

Adds filtering conditions to the rows to delete.

Parameters:conditions – The expression to filter with.
Returns:self for method chaining.
class breezeblocks.dml_builders.InsertBuilder(table, columns=[], db=None)

Bases: object

add_columns(*columns)

Adds all columns in the arguments to the insert statement.

Parameters:columns – All arguments provided to the method. Column names as strings and Column objects are accepted.
Returns:self for method chaining.
get()

Get an insert object for the current state of the builder.

Returns:A finished, executable Insert.
class breezeblocks.dml_builders.UpdateBuilder(table, db=None)

Bases: object

get()

Get an update object for the current state of the builder.

Returns:A finished, executable Update.
set_(column, expr)

Adds a column-value pair to the update statement.

Parameters:
  • column – A column in the table to set to a value. Column names as strings and Column objects are accepted.
  • expr – An expression to set the column value to.
Returns:

self for method chaining.

where(*conditions)

Adds filtering conditions to the rows to update.

Parameters:conditions – The expressions to filter with.
Returns:self for method chaining.

breezeblocks.exceptions module

Exceptions raised explicitly in BreezeBlocks modules.

BreezeBlocks will use these when the module can easily predict that an error would occur based on something passed to BreezeBlocks itself. An example of this would be trying to instantiate a query without providing a database argument.

Most database-related errors will still come from the underlying DBAPI module. BreezeBlocks does not try to catch any mistakes you make in telling it about your schema.

exception breezeblocks.exceptions.BreezeBlocksError

Bases: Exception

Base class for all BreezeBlocks errors.

exception breezeblocks.exceptions.BuilderError(message)

Bases: breezeblocks.exceptions.BreezeBlocksError

An error raised during statement building.

exception breezeblocks.exceptions.DeleteError(message)

Bases: breezeblocks.exceptions.BreezeBlocksError

An error occuring during creation of a delete statement.

exception breezeblocks.exceptions.InsertError(message)

Bases: breezeblocks.exceptions.BreezeBlocksError

An error occuring during creation of a insert statement.

exception breezeblocks.exceptions.MissingColumnError(column_name, table=None)

Bases: breezeblocks.exceptions.BreezeBlocksError

An error raise when trying to find a column that does not exist.

set_table()
exception breezeblocks.exceptions.MissingModuleError

Bases: breezeblocks.exceptions.BreezeBlocksError

An error occuring when a Database object would not have a DBAPI module.

When a user does not provide a DBAPI module, the program will try to find one that it can use based on the DSN provided. This will fail if one of these conditions is met:

  1. The DSN does not correspond to DBAPI module known by BreezeBlocks.
  2. The correct DBAPI module cannot be imported.
exception breezeblocks.exceptions.QueryError(message)

Bases: breezeblocks.exceptions.BreezeBlocksError

An error occuring during creation of a query.

exception breezeblocks.exceptions.UnsupportedModuleError

Bases: breezeblocks.exceptions.BreezeBlocksError

An error occuring when the provided DBAPI module is not fully supported.

This error generally means that the use case is intended to be supported, and the same code is expected to not cause an error in the future, assuming correct use of the package.

exception breezeblocks.exceptions.UpdateError(message)

Bases: breezeblocks.exceptions.BreezeBlocksError

An error occuring during creation of a update statement.

breezeblocks.pool module

Implements a connection pool for use in this package.

class breezeblocks.pool.ConnectionPool(dbapi_module, pool_size, conn_limit, on_connect, *connect_args, **connect_kwargs)

Bases: object

A pool of DBAPI 2.0 connections.

Parameters:
  • dbapi_module – The DBAPI module to connect through.
  • pool_size – Number of standby connections in the pool.
  • conn_limit – Maximum number of open connections from the pool.
  • on_connect – A SQL script to execute for each connection. It is executed for a connection each time it is taken out.
  • connect_args*args for calls to dbapi.connect.
  • connect_kwargs**kwargs for calls to dbapi.connect.
get(block=True, timeout=None)

Returns a wrapped connection object from the pool.

As ConnectionPool._getconn, may raise queue.Empty.

put(wrapped_conn, block=True, timeout=None)

Returns a wrapped connection to the pool.

class breezeblocks.pool.CursorProxy(cursor)

Bases: object

Simple proxy for a DBAPI cursor.

Useful when a DBAPI cursor is a native object, so a weak reference to it can be held. Also makes it such that the cursor can act as a context manager if it could not already.

class breezeblocks.pool.PooledConnection(pool, conn)

Bases: object

Wraps a DBAPI 2.0 connection for use with connection pools.

This class calls DBAPI 2.0 methods on its underlying connection. It only guarantees that it can handle the DBAPI-compliant subset of any underlying cursor’s functionality.

Bundles a connection with a specific pool.

Parameters:
  • pool – The source connection pool.
  • conn – The underlying connection.
close()

Puts the connection back in the pool.

commit()

Commits changes to the underlying connection.

cursor()

Allocates a cursor from the underlying connection.

Also store a weak-reference to this cursor so it can be closed when the connection is “closed”.

rollback()

Rolls back the underlying connection.

breezeblocks.query_builder module

class breezeblocks.query_builder.QueryBuilder(db=None)

Bases: object

clone(db=None)

Get a query builder object with the same state as this one.

This is achieved by cloning the state and assigning the clone as the state of a new query builder. The new query builder is then returned. If a query were constructed from both right after calling this, then both queries would produce equivalent SQL.

Parameters:db – The database the new querybuilder will be for. If omitted or None it will use the same one as this builder.
Returns:The clone of this query builder
distinct()

Set the distinct flag for the query being built to True.

from_(*table_exprs)

Adds table expressions to the from clause of a query.

Parameters:table_exprs – All arguments provided to the method. Each argument must be a table or a table-like expression to be added to the from clause.
Returns:self for method chaining.
get()

Get a query object for the current state of the builder.

This function is what kicks off the SQL string building and assembling the list of parameters. The results of that will be used to construct the Query object.

Returns:A finished, executable Query.
group_by(*column_exprs)

Sets a grouping for returned records.

Parameters:column_exprs – All arguments provided to the method. Each argument should be a column expression by which rows in the output expression can be grouped.
Returns:self for method chaining.
having(*conditions)

Adds conditions to the HAVING clause of a query.

Used for filtering conditions that should be applied after grouping.

Parameters:conditions – All arguments provided to the method. Each argument should be an expression that will result in a boolean value when the generated SQL is executed.
Returns:self for method chaining.
order_by(*exprs, ascending=True, nulls=None)

Adds statements to the ORDER BY clause of a query.

Used for specifying an ordering for the result set. All expression in a single invocation of this method share their sort order and placement of nulls. Invoke this method multiple times in order to specify different values for these.

Parameters:
  • exprs – The columns to order the result set by. Each argument should be a column expression by which rows in the result set can be ordered.
  • ascending – Flag determining whether to sort in ascending or descending order. Defaults to True (ascending).
  • nulls – Sets where in the sort order nulls belong. Valid values are “FIRST”, “LAST”, and None.

:return self for method chaining.

select(*args)

Adds expressions to the select clause of this query.

Parameters:args – All arguments provided to the method. Each argument should be a selectable expression. The only other possible argument is a table-like argument, from which all rows are to be selected.
Returns:self for method chaining.
where(*conditions)

Adds conditions to the where clause of a query.

Parameters:conditions – All arguments provided to the method. Each argument should be an expression that will result in a boolean value when the generated SQL is executed.
Returns:self for method chaining.

Module contents

BreezeBlocks Python Querying package.