SchemaBuilder

class lsst.daf.butler.SchemaBuilder(config, limited=False)

Bases: object

Builds a Schema step-by-step.

Parameters:
config : SchemaConfig

Configuration to parse.

limited : bool

If True, ignore tables, views, and associated foreign keys whose config descriptions include a “limited” key set to False.

Attributes:
metadata : sqlalchemy.MetaData

The sqlalchemy schema description.

tables : dict

A mapping from table or view name to the associated SQLAlchemy object. Note that this contains both true tables and views.

views : set

The names of all entries in tables that are actually implemented as views.

Attributes Summary

VALID_COLUMN_TYPES

Methods Summary

addColumn(table, columnDescription) Add a column to a table.
addForeignKeyConstraint(table, …) Add a ForeignKeyConstraint to a table.
addTable(tableName, tableDescription) Add a table to the schema metadata.
isIncluded(name) Return True if the named table or view should be included in this schema.
isView(name) Return True if the named table should be added / has been added as a view.
makeColumn(columnDescription) Make a Column entry for addition to a Table.
normalizeForeignKeyConstraint(…) Convert configuration for a ForeignKeyConstraint to standard form and return the target table.

Attributes Documentation

VALID_COLUMN_TYPES = {'blob': <class 'sqlalchemy.sql.sqltypes.LargeBinary'>, 'bool': <class 'sqlalchemy.sql.sqltypes.Boolean'>, 'datetime': <class 'sqlalchemy.sql.sqltypes.DateTime'>, 'float': <class 'sqlalchemy.sql.sqltypes.Float'>, 'hash': <class 'lsst.daf.butler.core.schema.Base64Bytes'>, 'int': <class 'sqlalchemy.sql.sqltypes.Integer'>, 'region': <class 'lsst.daf.butler.core.schema.Base64Region'>, 'string': <class 'sqlalchemy.sql.sqltypes.String'>}

Methods Documentation

addColumn(table, columnDescription)

Add a column to a table.

Parameters:
table : sqlalchemy.Table, sqlalchemy.expression.TableClause, str

The table.

columnDescription : dict

Description of the column to be created. Should always contain:

  • name, descriptive name
  • type, valid column type

May contain:

  • nullable, entry can be null
  • primary_key, mark this column as primary key
  • foreign_key, link to other table
  • length, length of the field
  • nbytes, length of decoded string (only for type=='hash')
  • doc, docstring
addForeignKeyConstraint(table, constraintDescription)

Add a ForeignKeyConstraint to a table.

If the table or the ForeignKeyConstraint’s target are views, or should not be included in this schema (because it is limited), does nothing.

Parameters:
table : sqlalchemy.Table or str

The table.

constraintDescription : dict

Description of the ForeignKeyConstraint to be created. Should always contain: - src, list of source column names - tgt, list of target column names May also contain: - onDelete, one of “SET NULL” or “CASCADE”.

addTable(tableName, tableDescription)

Add a table to the schema metadata.

Parameters:
tableName : str

Key of the table.

tableDescription : dict

Table description.

Requires: - columns, a list of column descriptions - foreignKeys, a list of foreign-key constraint descriptions

Raises:
ValueError

If a table with the given name already exists, or if the table name or one or more column names are not lowercase.

isIncluded(name)

Return True if the named table or view should be included in this schema.

Parameters:
name : str

Name of a table or view. Does not need to have been added.

Returns:
included : bool

Whether the table or view should be included in the schema.

isView(name)

Return True if the named table should be added / has been added as a view.

Parameters:
name : str

Name of a table or view. Does not need to have been added.

Returns:
view : bool

Whether the table should be added / has been added as a view.

makeColumn(columnDescription)

Make a Column entry for addition to a Table.

Parameters:
columnDescription : dict

Description of the column to be created. Should always contain: - name, descriptive name - type, valid column type May contain: - nullable, entry can be null - primary_key, mark this column as primary key - length, length of the field - autoinc, indicates column should auto increment - nbytes, length of decoded string (only for type=='hash') - doc, docstring

Returns:
c : sqlalchemy.Column

The created Column entry.

Raises:
ValueError

If the column description contains unsupported arguments or if the column name is not lowercase.

normalizeForeignKeyConstraint(constraintDescription)

Convert configuration for a ForeignKeyConstraint to standard form and return the target table.

Parameters:
constraintDescription : dict

Description of the ForeignKeyConstraint to be created. Should always contain:

  • src, list of source column names or single source column name.
  • tgt, list of (table-qualified) target column names or single target column name.

May also contain:

  • onDelete, one of “SET NULL” or “CASCADE”.
Returns:
src : tuple

Sequence of field names in the local table.

tgt : tuple

Sequence of table-qualified field names in the remote table.

tgtTable : str

Name of the target table.

onDelete : str, optional

One of “SET NULL”, “CASCADE”, or None.