Module reahl.sqlalchemysupport

Various bits of support for SQLAlchemy and declarative.

For using SqlAlchemy with Reahl

sqlalchemysupport.Session = <sqlalchemy.orm.scoping.scoped_session object>
sqlalchemysupport.Base = <class 'abc.Base'>
sqlalchemysupport.metadata = MetaData(bind=None)

Names of database objects

reahl.sqlalchemysupport.sqlalchemysupport.pk_name(table_name)

Returns the name that will be used in the database for a primary key, given:

Parameters:table_name – The name of the table to which the primary key belongs.
reahl.sqlalchemysupport.sqlalchemysupport.fk_name(table_name, column_name, other_table_name)

Returns the name that will be used in the database for a foreign key, given:

Parameters:
  • table_name – The name of the table from which the foreign key points.
  • column_name – The name of the column in which the foreign key pointer is stored.
  • other_table_name – The name of the table to which this foreign key points.
reahl.sqlalchemysupport.sqlalchemysupport.ix_name(table_name, column_name)

Returns the name that will be used in the database for an index key, given:

Parameters:
  • table_name – The name of the table to which the index belongs.
  • column_name – The name of the column that is indexed.

QueryAsSequence

class reahl.sqlalchemysupport.sqlalchemysupport.QueryAsSequence(query, map_function=<function QueryAsSequence.<lambda>>)

Used to adapt a SqlAlchemy Query to behave like a normal Python sequence type.

QueryAsSequence only implements a few useful methods, not the full collections.abc.Sequence protocol.

Parameters:
  • query – The Query object to adapt.
  • map_function – An optional function to map each instance returned (similar to function in the standard map() function).
__len__()

Returns the number of items that would be returned by executing the query.

__getitem__(key)

Returns the items requested by executing an modifed query representing only the requested slice.

sort(key=None, reverse=False)

Modifies the query to be ordered as requested.

Parameters:
  • key – A SqlAlchemy order_by criterion to be used for sorting.
  • reverse – If True, use descending order.

PersistedField

class reahl.sqlalchemysupport.sqlalchemysupport.PersistedField(class_to_query, default=None, required=False, required_message=None, label=None, readable=None, writable=None)

A reahl.component.modelinterface.Field which takes an integer as input, and yields an instance of class_to_query as parsed Python object. The Python object returned is selected from the Session. The instance with ‘id’ equal to the given integer is the one returned.

Parameters:class_to_query – The class to query by id from the Session.

(See reahl.component.modelinterface.Field for other arguments.)

SqlAlchemyControl

class reahl.sqlalchemysupport.sqlalchemysupport.SqlAlchemyControl(echo=False)

An ORMControl for dealing with SQLAlchemy.

nested_transaction()

A context manager for code that needs to run in a nested transaction.

connect()

Creates the SQLAlchemy Engine, bind it to the metadata and instrument the persisted classes for the current reahlsystem.root_egg.

disconnect()

Disposes the current SQLAlchemy Engine and .remove() the Session.

commit()

Commits the current transaction. Programmers should not need to deal with such transaction management explicitly, since the framework already manages transactions itself.

rollback()

Rolls back the current transaction. Programmers should not need to deal with such transaction management explicitly, since the framework already manages transactions itself.