SqliteDatabase¶
- 
class lsst.daf.butler.registry.databases.sqlite.SqliteDatabase(*, connection: sqlalchemy.engine.base.Connection, origin: int, namespace: Optional[str] = None, writeable: bool = True)¶
- Bases: - lsst.daf.butler.registry.interfaces.Database- An implementation of the - Databaseinterface for SQLite3.- Parameters: - connection : sqlalchemy.engine.Connection
- An existing connection created by a previous call to - connect.
- origin : int
- An integer ID that should be used as the default for any datasets, quanta, or other entities that use a (autoincrement, origin) compound primary key. 
- namespace : str, optional
- The namespace (schema) this database is associated with. If - None, the default schema for the connection is used (which may be- None).
- writeable : bool, optional
- If - True, allow write operations on the database, including- CREATE TABLE.
 - Notes - The case where - namespace is not Noneis not yet tested, and may be broken; we need an API for attaching to different databases in order to write those tests, but haven’t yet worked out what is common/different across databases well enough to define it.- Methods Summary - connect(uri, *, filename, writeable)- Create a - sqlalchemy.engine.Connectionfrom a SQLAlchemy URI or filename.- declareStaticTables(*, create)- Return a context manager in which the database’s static DDL schema can be declared. - delete(table, columns, *rows)- Delete one or more rows from a table. - ensureTableExists(name, spec)- Ensure that a table with the given name and specification exists, creating it if necessary. - expandDatabaseEntityName(shrunk)- Retrieve the original name for a database entity that was too long to fit within the database engine’s limits. - fromConnection(connection, *, origin, …)- Create a new - Databasefrom an existing- sqlalchemy.engine.Connection.- fromUri(uri, *, origin, namespace, writeable)- Construct a database from a SQLAlchemy URI. - getExistingTable(name, spec)- Obtain an existing table with the given name and specification. - insert(table, *rows, returnIds)- Insert one or more rows into a table, optionally returning autoincrement primary key values. - isWriteable()- Return - Trueif this database can be modified by this client.- makeDefaultUri(root)- Create a default connection URI appropriate for the given root directory, or - Noneif there can be no such default.- query(sql, *args, **kwds)- Run a SELECT query against the database. - replace(table, *rows)- Insert one or more rows into a table, replacing any existing rows for which insertion of a new row would violate the primary key constraint. - shrinkDatabaseEntityName(original)- Return a version of the given name that fits within this database engine’s length limits for table, constraint, indexes, and sequence names. - sync(table, *, keys, Any], compared, …)- Insert into a table as necessary to ensure database contains values equivalent to the given ones. - transaction(*, interrupting)- Return a context manager that represents a transaction. - update(table, where, str], *rows)- Update one or more rows in a table. - Methods Documentation - 
classmethod connect(uri: Optional[str] = None, *, filename: Optional[str] = None, writeable: bool = True) → sqlalchemy.engine.base.Connection¶
- Create a - sqlalchemy.engine.Connectionfrom a SQLAlchemy URI or filename.- Parameters: - uri : str
- A SQLAlchemy URI connection string. 
- filename : str
- Name of the SQLite database file, or - Noneto use an in-memory database. Ignored if- uri is not None.
- origin : int
- An integer ID that should be used as the default for any datasets, quanta, or other entities that use a (autoincrement, origin) compound primary key. 
- writeable : bool, optional
- If - True, allow write operations on the database, including- CREATE TABLE.
 - Returns: - cs : sqlalchemy.engine.Connection
- A database connection and transaction state. 
 
- uri : 
 - 
declareStaticTables(*, create: bool) → AbstractContextManager[lsst.daf.butler.registry.interfaces._database.StaticTablesContext]¶
- Return a context manager in which the database’s static DDL schema can be declared. - Parameters: - Returns: - schema : StaticTablesContext
- A helper object that is used to add new tables. 
 - Raises: - Notes - A database’s static DDL schema must be declared before any dynamic tables are managed via calls to - ensureTableExistsor- getExistingTable. The order in which static schema tables are added inside the context block is unimportant; they will automatically be sorted and added in an order consistent with their foreign key relationships.- Examples - Given a - Databaseinstance- db:- with db.declareStaticTables(create=True) as schema: schema.addTable("table1", TableSpec(...)) schema.addTable("table2", TableSpec(...)) 
- schema : 
 - 
delete(table: sqlalchemy.sql.schema.Table, columns: Iterable[str], *rows) → int¶
- Delete one or more rows from a table. - Parameters: - table : sqlalchemy.schema.Table
- Table that rows should be deleted from. 
- columns: `~collections.abc.Iterable` of `str`
- The names of columns that will be used to constrain the rows to be deleted; these will be combined via - ANDto form the- WHEREclause of the delete query.
- *rows
- Positional arguments are the keys of rows to be deleted, as dictionaries mapping column name to value. The keys in all dictionaries must exactly the names in - columns.
 - Returns: - count : int
- Number of rows deleted. 
 - Raises: - ReadOnlyDatabaseError
- Raised if - isWriteablereturns- Falsewhen this method is called.
 - Notes - May be used inside transaction contexts, so implementations may not perform operations that interrupt transactions. - The default implementation should be sufficient for most derived classes. 
- table : 
 - 
ensureTableExists(name: str, spec: lsst.daf.butler.core.ddl.TableSpec) → sqlalchemy.sql.selectable.FromClause¶
- Ensure that a table with the given name and specification exists, creating it if necessary. - Parameters: - name : str
- Name of the table (not including namespace qualifiers). 
- spec : TableSpec
- Specification for the table. This will be used when creating the table, and may be used when obtaining an existing table to check for consistency, but no such check is guaranteed. 
 - Returns: - table : sqlalchemy.schema.Table
- SQLAlchemy representation of the table. 
 - Raises: - ReadOnlyDatabaseError
- Raised if - isWriteablereturns- False, and the table does not already exist.
- DatabaseConflictError
- Raised if the table exists but - specis inconsistent with its definition.
 - Notes - This method may not be called within transactions. It may be called on read-only databases if and only if the table does in fact already exist. - Subclasses may override this method, but usually should not need to. 
- name : 
 - 
expandDatabaseEntityName(shrunk: str) → str¶
- Retrieve the original name for a database entity that was too long to fit within the database engine’s limits. - Parameters: - original : str
- The original name. 
 - Returns: - shrunk : str
- The new, possibly shortened name. 
 
- original : 
 - 
classmethod fromConnection(connection: sqlalchemy.engine.base.Connection, *, origin: int, namespace: Optional[str] = None, writeable: bool = True) → lsst.daf.butler.registry.interfaces._database.Database¶
- Create a new - Databasefrom an existing- sqlalchemy.engine.Connection.- Parameters: - connection : sqllachemy.engine.Connection
- The connection for the the database. May be shared between - Databaseinstances.
- origin : int
- An integer ID that should be used as the default for any datasets, quanta, or other entities that use a (autoincrement, origin) compound primary key. 
- namespace : str, optional
- A different database namespace (i.e. schema) the new instance should be associated with. If - None(default), the namespace (if any) is inferred from the connection.
- writeable : bool, optional
- If - True, allow write operations on the database, including- CREATE TABLE.
 - Returns: - db : Database
- A new - Databaseinstance.
 - Notes - This method allows different - Databaseinstances to share the same connection, which is desirable when they represent different namespaces can be queried together. This also ties their transaction state, however; starting a transaction in any database automatically starts on in all other databases.
- connection : 
 - 
classmethod fromUri(uri: str, *, origin: int, namespace: Optional[str] = None, writeable: bool = True) → lsst.daf.butler.registry.interfaces._database.Database¶
- Construct a database from a SQLAlchemy URI. - Parameters: - uri : str
- A SQLAlchemy URI connection string. 
- origin : int
- An integer ID that should be used as the default for any datasets, quanta, or other entities that use a (autoincrement, origin) compound primary key. 
- namespace : str, optional
- A database namespace (i.e. schema) the new instance should be associated with. If - None(default), the namespace (if any) is inferred from the URI.
- writeable : bool, optional
- If - True, allow write operations on the database, including- CREATE TABLE.
 - Returns: - db : Database
- A new - Databaseinstance.
 
- uri : 
 - 
getExistingTable(name: str, spec: lsst.daf.butler.core.ddl.TableSpec) → Optional[sqlalchemy.sql.schema.Table]¶
- Obtain an existing table with the given name and specification. - Parameters: - name : str
- Name of the table (not including namespace qualifiers). 
- spec : TableSpec
- Specification for the table. This will be used when creating the SQLAlchemy representation of the table, and it is used to check that the actual table in the database is consistent. 
 - Returns: - Raises: - DatabaseConflictError
- Raised if the table exists but - specis inconsistent with its definition.
 - Notes - This method can be called within transactions and never modifies the database. - Subclasses may override this method, but usually should not need to. 
- name : 
 - 
insert(table: sqlalchemy.sql.schema.Table, *rows, returnIds: bool = False) → Optional[List[int]]¶
- Insert one or more rows into a table, optionally returning autoincrement primary key values. - Parameters: - table : sqlalchemy.schema.Table
- Table rows should be inserted into. 
- returnIds: `bool`
- If - True(- Falseis default), return the values of the table’s autoincrement primary key field (which much exist).
- *rows
- Positional arguments are the rows to be inserted, as dictionaries mapping column name to value. The keys in all dictionaries must be the same. 
 - Returns: - Raises: - ReadOnlyDatabaseError
- Raised if - isWriteablereturns- Falsewhen this method is called.
 - Notes - The default implementation uses bulk insert syntax when - returnIdsis- False, and a loop over single-row insert operations when it is- True.- Derived classes should reimplement when they can provide a more efficient implementation (especially for the latter case). - May be used inside transaction contexts, so implementations may not perform operations that interrupt transactions. 
- table : 
 - 
classmethod makeDefaultUri(root: str) → Optional[str]¶
- Create a default connection URI appropriate for the given root directory, or - Noneif there can be no such default.
 - 
query(sql: sqlalchemy.sql.selectable.FromClause, *args, **kwds) → sqlalchemy.engine.result.ResultProxy¶
- Run a SELECT query against the database. - Parameters: - sql : sqlalchemy.sql.FromClause
- A SQLAlchemy representation of a - SELECTquery.
- *args
- Additional positional arguments are forwarded to - sqlalchemy.engine.Connection.execute.
- **kwds
- Additional keyword arguments are forwarded to - sqlalchemy.engine.Connection.execute.
 - Returns: - result : sqlalchemy.engine.ResultProxy
- Query results. 
 - Notes - The default implementation should be sufficient for most derived classes. 
- sql : 
 - 
replace(table: sqlalchemy.sql.schema.Table, *rows) → None¶
- Insert one or more rows into a table, replacing any existing rows for which insertion of a new row would violate the primary key constraint. - Parameters: - table : sqlalchemy.schema.Table
- Table rows should be inserted into. 
- *rows
- Positional arguments are the rows to be inserted, as dictionaries mapping column name to value. The keys in all dictionaries must be the same. 
 - Raises: - ReadOnlyDatabaseError
- Raised if - isWriteablereturns- Falsewhen this method is called.
 - Notes - May be used inside transaction contexts, so implementations may not perform operations that interrupt transactions. - Implementations should raise a - sqlalchemy.exc.IntegrityErrorexception when a constraint other than the primary key would be violated.- Implementations are not required to support - replaceon tables with autoincrement keys.
- table : 
 - 
shrinkDatabaseEntityName(original: str) → str¶
- Return a version of the given name that fits within this database engine’s length limits for table, constraint, indexes, and sequence names. - Implementations should not assume that simple truncation is safe, because multiple long names often begin with the same prefix. - The default implementation simply returns the given name. - Parameters: - original : str
- The original name. 
 - Returns: - shrunk : str
- The new, possibly shortened name. 
 
- original : 
 - 
sync(table: sqlalchemy.sql.schema.Table, *, keys: Dict[str, Any], compared: Optional[Dict[str, Any]] = None, extra: Optional[Dict[str, Any]] = None, returning: Optional[Sequence[str]] = None) → Tuple[Optional[Dict[str, Any]], bool]¶
- Insert into a table as necessary to ensure database contains values equivalent to the given ones. - Parameters: - table : sqlalchemy.schema.Table
- Table to be queried and possibly inserted into. 
- keys : dict
- Column name-value pairs used to search for an existing row; must be a combination that can be used to select a single row if one exists. If such a row does not exist, these values are used in the insert. 
- compared : dict, optional
- Column name-value pairs that are compared to those in any existing row. If such a row does not exist, these rows are used in the insert. 
- extra : dict, optional
- Column name-value pairs that are ignored if a matching row exists, but used in an insert if one is necessary. 
- returning : Sequenceofstr, optional
- The names of columns whose values should be returned. 
 - Returns: - Raises: - DatabaseConflictError
- Raised if the values in - compareddo not match the values in the database.
- ReadOnlyDatabaseError
- Raised if - isWriteablereturns- False, and no matching record already exists.
 - Notes - This method may not be called within transactions. It may be called on read-only databases if and only if the matching row does in fact already exist. 
- table : 
 - 
transaction(*, interrupting: bool = False) → Iterator[T_co]¶
- Return a context manager that represents a transaction. - Parameters: 
 - 
update(table: sqlalchemy.sql.schema.Table, where: Dict[str, str], *rows) → int¶
- Update one or more rows in a table. - Parameters: - table : sqlalchemy.schema.Table
- Table containing the rows to be updated. 
- where : dict[str,str]
- A mapping from the names of columns that will be used to search for existing rows to the keys that will hold these values in the - rowsdictionaries. Note that these may not be the same due to SQLAlchemy limitations.
- *rows
- Positional arguments are the rows to be updated. The keys in all dictionaries must be the same, and may correspond to either a value in the - wheredictionary or the name of a column to be updated.
 - Returns: - count : int
- Number of rows matched (regardless of whether the update actually modified them). 
 - Raises: - ReadOnlyDatabaseError
- Raised if - isWriteablereturns- Falsewhen this method is called.
 - Notes - May be used inside transaction contexts, so implementations may not perform operations that interrupt transactions. - The default implementation should be sufficient for most derived classes. 
- table : 
 
- connection :