14.1.10. crate_anon.anonymise.dbholder

crate_anon/anonymise/dbholder.py


Copyright (C) 2015, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

This file is part of CRATE.

CRATE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CRATE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CRATE. If not, see <https://www.gnu.org/licenses/>.


Database “holder”.

class crate_anon.anonymise.dbholder.DatabaseHolder(name: str, url: str, srccfg: DatabaseSafeConfig = None, with_session: bool = False, with_conn: bool = True, reflect: bool = True, encoding: str = 'utf-8', echo: bool = False)[source]

Object to represent a connection to a database.

__init__(name: str, url: str, srccfg: DatabaseSafeConfig = None, with_session: bool = False, with_conn: bool = True, reflect: bool = True, encoding: str = 'utf-8', echo: bool = False) None[source]
Parameters
  • name – internal database name

  • url – SQLAlchemy URL

  • srccfgcrate_anon.anonymise.config.DatabaseSafeConfig

  • with_session – create an SQLAlchemy Session?

  • with_conn – create an SQLAlchemy connection (via an Engine)?

  • reflect – read the database structure (when required)?

  • encoding – passed to SQLAlchemy’s create_engine()

  • echo – passed to SQLAlchemy’s create_engine()

create_session() None[source]

Creates a database session, if not created to begin with.

enable_reflect() None[source]

Enables reflection, if it wasn’t enabled to begin with.

property metadata: sqlalchemy.sql.schema.MetaData

Returns the SQLAlchemy MetaData. If reflection is enabled, ensure the database has been reflected first.

property table_names: List[str]

Returns the table names from the database, if reflection is enabled. (Otherwise returns an empty list.)

update_metadata() None[source]

Updates the metadata, for example if a table has been dropped.