Various bits of support for SQLAlchemy and declarative.
Run ‘reahl componentinfo reahl-sqlalchemysupport’ for configuration information.
For using SqlAlchemy with Reahl¶
- sqlalchemysupport.Session = <sqlalchemy.orm.scoping.scoped_session object>¶
- sqlalchemysupport.Base = <class 'abc.Base'>¶
- sqlalchemysupport.metadata = MetaData(bind=None)¶
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¶
Returns the name that will be used in the database for a primary key, given:
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:
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:
table_name – The name of the table to which the index belongs.
column_name – The name of the column that is indexed.
- 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
query – The
Queryobject to adapt.
map_function – An optional function to map each instance returned (similar to function in the standard
- __init__(query, map_function=<function QueryAsSequence.<lambda>>)¶
Initialize self. See help(type(self)) for accurate signature.
Returns the number of items that would be returned by executing the query.
Returns the items requested by executing an modifed query representing only the requested slice.
- class reahl.sqlalchemysupport.sqlalchemysupport.PersistedField(class_to_query, default=None, required=False, required_message=None, label=None, readable=None, writable=None)¶
reahl.component.modelinterface.Fieldwhich 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.
class_to_query – The class to query by id from the Session.
reahl.component.modelinterface.Fieldfor other arguments.)
Override this method on a subclass to specify how that subclass transforms the unparsed_input (a string) into a representative Python object.
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.
- class reahl.sqlalchemysupport.sqlalchemysupport.SqlAlchemyControl(echo=False)¶
An ORMControl for dealing with SQLAlchemy.
A context manager for code that needs to run in a nested transaction.
Changed in version 5.0: Changed to yield a TransactionVeto which can be used to override when the transaction will be committed or not.
Creates the SQLAlchemy Engine, bind it to the metadata and instrument the persisted classes for the current reahlsystem.root_egg.
Disposes the current SQLAlchemy Engine and .remove() the Session.
Commits the current transaction. Programmers should not need to deal with such transaction management explicitly, since the framework already manages transactions itself.
Rolls back the current transaction. Programmers should not need to deal with such transaction management explicitly, since the framework already manages transactions itself.