gc3libs.persistence.sql

SQL-based storage of GC3pie objects.

class gc3libs.persistence.sql.SqlStore(url, table_name='store', idfactory=None, extra_fields={}, create=True, **extra_args)

Save and load objects in a SQL db, using python’s pickle module to serialize objects into a specific field.

Access to the DB is done via SQLAlchemy module, therefore any driver supported by SQLAlchemy will be supported by this class.

The url argument is used to access the store. It is supposed to be a gc3libs.url.Url class, and therefore may contain username, password, host and port if they are needed by the db used.

The table_name argument is the name of the table to create. By default it’s store.

The constructor will create the table_name table if it does not exist, but if there already is such a table it will assume the it’s schema is compatible with our needs. A minimal table schema is as follow:

The meaning of the fields is:

id: this is the id returned by the save() method and univoquely identify a stored object.

data: the serialization of the object.

state: if the object is a Task istance this wil lbe the current execution state of the job

Field Type Null Key Default
id data state int(11) blob varchar(128) NO YES YES PRI NULL NULL NULL

The extra_fields argument is used to extend the database. It must contain a mapping <column> : <function> where:

<column> is a sqlalchemy.Column object.

<function> is a function which takes the object to be saved as argument and returns the value to be stored into the database. Any exception raised by this function will be ignored. Classes GetAttribute and GetItem in module get provide convenient helpers to save object attributes into table columns.

For each extra column the save() method will call the corresponding <function> in order to get the correct value to store into the db.

Any extra keyword arguments are ignored for compatibility with FilesystemStore.

list()

Return list of IDs of saved Job objects.

This is an optional method; classes that do not implement it should raise a NotImplementedError exception.

load(id_)

Load a saved object given its ID, and return it.

remove(id_)

Delete a given object from persistent storage, given its ID.

replace(id_, obj)

Replace the object already saved with the given ID with a copy of obj.

save(obj)

Save an object, and return an ID.

gc3libs.persistence.sql.make_sqlstore(url, *args, **extra_args)

Return a SqlStore instance, given a SQLAlchemy URL and optional initialization arguments.

This function is a bridge between the generic factory functions provided by gc3libs.persistence.make_store() and gc3libs.persistence.register() and the class constructor SqlStore:class.

Examples:

| >>> ss1 = make_sqlstore(gc3libs.url.Url('sqlite:////tmp/foo.db'))
| >>> ss1.__class__.__name__
| 'SqlStore'
gc3libs.persistence.sql.sql_next_id_factory(db)

This function will return a function which can be used as next_id_fn argument for the IdFactory class constructor.

db is DB connection class conform to DB API2.0 specs (works also with SQLAlchemy engine types)

The function returned has signature:

sql_next_id(n=1)

the id returned is the maximum id field in the store table plus 1.