experimentum.Storage.SQLAlchemy package

Submodules

experimentum.Storage.SQLAlchemy.ColumnFactory module

Column factory class to create new columns.

Used to create new columns, indexes and foreign keys with the help of sqlalchemy.

class experimentum.Storage.SQLAlchemy.ColumnFactory.ColumnFactory

Bases: object

Factory class to create SQLAlchemy Columns.

create_foreign_key(key, col)

Create a foreign key column.

Parameters:
  • key (ForeignKey) – Foreign Key
  • col (Column) – column to create foreign key on
Returns:

foreign key column

Return type:

sqlalchemy.schema.Column

get_columns_and_indexes(blueprint)

Get Column and Index instances from the blueprint.

Parameters:blueprint (Blueprint) – Schema Blueprint to add/modify table.
Returns:dictionary with columns and indexes
Return type:dict
get_foreign_key(col, fkeys)

Get a foreign key from a column.

Parameters:
  • col (Column) – Column to get foreign key of.
  • fkeys (list) – List of foreign keys
Returns:

either a foreign key column if found or False

Return type:

sqlalchemy.schema.Column, False

static get_indexes(col, indexes, used_indexes=None)

Get all indexes and primary keys of a column.

Parameters:
  • col (Column) – The column
  • indexes (list) – List of indexes
  • used_indexes (list, optional) – Defaults to None. Used indexes cols for composite keys.
Returns:

with primary keys, indexes and used indexes

Return type:

tuple

get_type(col_type, params=None, unsigned=False)

Map the type to valid Column Types.

Notes

http://docs.sqlalchemy.org/en/latest/core/type_basics.html

Parameters:
  • col_type (str) – Type of column
  • params (dict, optional) – Defaults to None. Additional Column Options.
  • unsigned (bool, optional) – Defaults to False. If it is an unsigned integer or not.
Returns:

Type for new column

Return type:

sqlalchemy.types.TypeEngine

experimentum.Storage.SQLAlchemy.ColumnFactory.get_number_type(col_type, params, unsigned=False)

Create a number type column.

Parameters:
  • col_type (string) – Type of the column.
  • params (object) – Additional parameters.
  • unsigned (bool, optional) – Defaults to False. Whether or not it is an unsigned int.
Returns:

Number type like integer or float

Return type:

sqlalchemy.types.TypeEngine

experimentum.Storage.SQLAlchemy.ColumnFactory.get_string_type(col_type, params)

Create a string type column.

Parameters:
  • col_type (string) – Type of the column.
  • params (object) – Additional parameters.
Returns:

String type like char or text

Return type:

sqlalchemy.types.TypeEngine

experimentum.Storage.SQLAlchemy.ColumnFactory.get_time_type(col_type)

Create a time type column.

Parameters:col_type (string) – Type of the column.
Returns:Time type like date or timestamp
Return type:sqlalchemy.types.TypeEngine

experimentum.Storage.SQLAlchemy.Platform module

SQL Platform specific code.

Generate SQL queries for altering tables and columns because SQLAlchemy mostly does not support these types of operations.

class experimentum.Storage.SQLAlchemy.Platform.Platform

Bases: object

Gettings some sql platform specific queries.

engine

Database Engine

Type:sqlalchemy.engine.Engine
meta

Schema Metadata

Type:sqlalchemy.schema.MetaData
get_add_column_sql(table, column)

Get SQL for adding a column to a table.

Notes

Parameters:
  • table (str) – Name of the table
  • column (sqlalchemy.schema.Column) – Column to add
Returns:

str

static get_drop_columns_sql(table, columns)

Get a list of sql commands for dropping columns.

Parameters:
  • table (str) – name of the table
  • columns (list) – list of column names
Returns:

list

get_drop_key_constraint_sql(table, column, idx_type, idx_name)

Get a drop key constraint sql query.

Parameters:
  • table (str) – Name of table
  • column (sqlalchemy.schema.Column) – Column with the key
  • idx_type (str) – Type of index, i.e. primary, foreign, unique or index
  • idx_name (str) – Name of the index
Returns:

str

static get_foreign_key_action_sql(foreign_key)

Get ONUPDATE/ONDELETE actions.

Parameters:foreign_key (sqlalchemy.schema.ForeignKey) – ForeignKey to get action of.
Returns:dict
get_key_sql(table, column)

Get SQL for adding foreign key and primary key constraints.

Parameters:
  • table (str) – Name of the table
  • column (sqlalchemy.schema.Column) – Column to add contraints on
Returns:

List of strings with SQL commands

Return type:

list

static get_rename_sql(old, new)

Get the sql for renaming a table.

Parameters:
  • old (str) – old table name
  • new (str) – new table name
Returns:

str

is_mssql()

Check if current database driver is MSSQL.

Returns:boolean
is_mysql()

Check if current database driver is MySQL.

Returns:boolean
is_postgresql()

Check if current database driver is PostgreSQL.

Returns:boolean
is_sqlite()

Check if current database driver is SQLite.

Returns:boolean
set_engine(engine, meta)

Set engine and meta.

Parameters:
  • engine (sqlalchemy.engine.Engine) – Database Engine
  • meta (sqlalchemy.schema.MetaData) – Schema Metadata

experimentum.Storage.SQLAlchemy.Repository module

Implementation of the AbstractRepository.

Implements the AbstractRepository interface to use the SQLAlchemy ORM as a data store.

class experimentum.Storage.SQLAlchemy.Repository.QueryBuilder(repo, where)

Bases: object

Helper Class to build a SQLAlchemy Query.

repo

The Repository to query

Type:Repository
where

where condition to build

Type:list
__filter_cond

Filter Conditions

Type:list
__filter_cond_or

Filter Conditions that are connected with logical OR

Type:list
build(query)

Build the query.

Parameters:query (sqlalchemy.orm.query.Query) – Current query
Returns:Final query
Return type:sqlalchemy.orm.query.Query
build_where(query)

Build the query with the where conditions.

Parameters:query (sqlalchemy.orm.query.Query) – Current Query
Returns:Query with where conditions applied.
Return type:sqlalchemy.orm.query.Query
class experimentum.Storage.SQLAlchemy.Repository.Repository(**attributes)

Bases: experimentum.Storage.AbstractRepository.AbstractRepository

Implementation of the AbstractRepository Interface.

This implementation uses the SQLAlchemy ORM to implement the Repository Design Pattern. It uses the SQLAlchemy mapper function to map the Table Schema and the a Repository class. It also uses the listen method to hook up the before_* and after_* events.

classmethod all()

Get all entries for this specific repository from your data store.

Returns:List of all entires
Return type:list
create()

Save the repository content in your data store.

Returns:Self Instance
Return type:Repository
delete()

Delete the repository content from your data store.

Returns:Self Instance.
Return type:Repository
classmethod find(id)

Find an entry of this repository based on its id.

Parameters:id (int) – ID to search for.
Returns:Item which the concrete id
Return type:AbstractRepository
classmethod first(where=None)

Get first entry which satisfy a specific condition from your data store.

Parameters:where (list, optional) – Defaults to None. Where Condition
Returns:Item which satisfies the condition.
Return type:AbstractRepository
classmethod get(where=None)

Get all entries which satisfy a specific condition from your data store.

Parameters:where (list, optional) – Defaults to None. Where Condition
Returns:List of items which satisfy the condition.
Return type:list
static map_to_table(cls, repo, table, properties)

Get the SQLAlchemy mapper, i.e. map the tables to classes.

Parameters:
  • repo (Repository) – Repository to map
  • table (sqlalchemy.schema.table) – Table to map
  • properties (object) – relationships to map
static mapping(cls, store)

Map data store content to repository classes.

Example: Map a User Table in the data store to the UserRepository, which reflects the attributes of the User Table schema:

Repository.mapping(UserRepository, store)
Parameters:
update()

Update the repository content in your data store.

Returns:Self Instance.
Return type:Repository

experimentum.Storage.SQLAlchemy.SQLitePlatform module

SQLite specific sql commands and queries.

SQLite does not provide complete range of all sql commands, therefore some tricks are needed to emulate the behavior. See: https://www.sqlite.org/omitted.html

class experimentum.Storage.SQLAlchemy.SQLitePlatform.SQLitePlatform

Bases: experimentum.Storage.SQLAlchemy.Platform.Platform

SQLite specific sql commands and queries.

alter_table(table, cols, dropped)

Alter SQLite Table.

SQLite does not provide complete ALTER TABLE support, only RENAME TABLE and ADD COLUMN variants are supported. In order to add columns and keys we have to create a tempory table which holds the data of the table and then drop and recreate the table with the modified schema. Lastly we have to copy the data back into the new table and delete the temporary data table.

See: https://www.sqlite.org/omitted.html

Parameters:
  • table (str) – Name of the table
  • cols (list) – List with changed columns
  • dropped (dict) – Dictionary with list of columns and indexes to drop
experimentum.Storage.SQLAlchemy.SQLitePlatform.prepare_columns(columns, indexes, dropped)

Prepate columns so that they can be created for another table.

Parameters:
  • columns (list) – Columns of a table
  • indexes (list) – indexes of a table
  • dropped (object) – Columns and indexes to drop
Returns:

with columns and column names

Return type:

dict

experimentum.Storage.SQLAlchemy.Store module

Concrete Implementation of a data store based on SQLAlchemy.

Uses the SQLAlchemy ORM to implement the AbstractStore interface.

class experimentum.Storage.SQLAlchemy.Store.Store(app)

Bases: experimentum.Storage.AbstractStore.AbstractStore

Concrete Implementation of a data store based on SQLAlchemy.

app

Framework App class.

Type:App
engine

Defaults to None. Database engine.

Type:sqlalchemy.engine.Engine
meta

Defaults to None. Schema Meta Data.

Type:sqlqlchemy.schema.MetaData
platform

Basic SQL Statements because SQLAlchemy could not handle everything.

Type:Platform
sqlite_platform

SQLite specific sql statements.

Type:SQLitePlatform
alter(blueprint)

Alter Schema of the table.

Parameters:blueprint (Blueprint) – The new blueprint with updated schema.
create(blueprint)

Create a new Table with Columns and indexes.

Notes

http://docs.sqlalchemy.org/en/latest/core/metadata.html#creating-and-dropping-database-tables

Parameters:blueprint (Blueprint) – The Blueprint to create the table
drop(name, checkfirst=False)

Drop a table from the data store.

Notes

http://docs.sqlalchemy.org/en/latest/core/metadata.html#creating-and-dropping-database-tables

Parameters:
  • name (str) – Name of the table
  • checkfirst (bool, optional) – Defaults to False. Whether to check if exists first.
drop_if_exists(name)

Drop a table from the datastore if it exists.

Notes

http://docs.sqlalchemy.org/en/latest/core/metadata.html#creating-and-dropping-database-tables

Parameters:name (str) – Name of the table
has_column(table, column)

Check if a table has a specific column.

Parameters:
  • table (str) – Name of the table
  • column (str) – Name of the column
Returns:

boolean

has_table(table)

Check if the data store has a specific table.

Parameters:table (str) – Name of the Table
Returns:boolean
rename(old, new)

Rename a table.

Parameters:
  • old (str) – Old table name
  • new (str) – New table name
set_engine(engine)

Set database engine, metadata store, and platform specific handlers.

Parameters:engine (sqlalchemy.engine.Engine) – Database engine

Module contents

Implementation of the Storage based on SQLAlchemy.