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)

Sessions

reahl.sqlalchemysupport.sqlalchemysupport.session_scoped(cls)

A decorator for making a class session-scoped.

It adds a relationship to the user_session on any decorated Entity and ensures that the Entity will be deleted if the UserSession is deleted.

Two classmethods are also added to the decorated class:

classmethod for_session(cls, user_session, **kwargs)

This method assumes that there should be only one instance of the Entity class for a given UserSession. When called, it will return that instance if it exists. If it does not exist, it will construct the instance. The kwargs supplied will be passed along when creating the instance.

classmethod for_current_session(cls, **kwargs)

Works the same as for_session() except that you need not pass a UserSession, the current UserSession is assumed.

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).
__init__(query, map_function=<function QueryAsSequence.<lambda>>)

Initialize self. See help(type(self)) for accurate signature.

__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.)

parse_input(unparsed_input)

Override this method on a subclass to specify how that subclass transforms the unparsed_input (a string) into a representative Python object.

unparse_input(parsed_value)

Override this method on a subclass to specify how that subclass transforms a given Python object (parsed_value) to a string that represents it to a user.

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.