swh.core.db package#
Submodules#
- swh.core.db.common module
- swh.core.db.db_utils module
now()
stored_procedure()
jsonize()
connect_to_conninfo()
swh_db_version()
swh_db_versions()
swh_db_upgrade()
swh_db_module()
swh_set_db_module()
swh_set_db_version()
swh_db_flavor()
execute_values_generator()
import_swhmodule()
get_sql_for_package()
populate_database_for_package()
initialize_database_for_module()
get_database_info()
parse_dsn_or_dbname()
init_admin_extensions()
create_database_for_package()
execute_sqlfiles()
Module contents#
- swh.core.db.value_as_pg_text(data: Any) str [source]#
Render the given data in the postgresql text format.
NULL values are handled outside of this function (either by
render_array_element()
, or byBaseDb.copy_to()
.)
- swh.core.db.escape_copy_column(column: str) str [source]#
Escape the text representation of a column for use by COPY.
- class swh.core.db.BaseDb(conn: connection, pool: AbstractConnectionPool | None = None)[source]#
Bases:
object
Base class for swh.*.*Db.
cf. swh.storage.db.Db, swh.archiver.db.ArchiverDb
create a DB proxy
- Parameters:
conn – psycopg2 connection to the SWH DB
pool – psycopg2 pool of connections
- static adapt_conn(conn: connection)[source]#
Makes psycopg2 use ‘bytes’ to decode bytea instead of ‘memoryview’, for this connection.
- classmethod connect(*args, **kwargs) BaseDbType [source]#
factory method to create a DB proxy
Accepts all arguments of psycopg2.connect; only some specific possibilities are reported below.
- Parameters:
connstring – libpq2 connection string
- cursor(cur_arg: cursor | None = None) cursor [source]#
get a cursor: from cur_arg if given, or a fresh one otherwise
meant to avoid boilerplate if/then/else in methods that proxy stored procedures
- transaction() Iterator[cursor] [source]#
context manager to execute within a DB transaction
- Yields:
a psycopg2 cursor
- copy_to(items: Iterable[Mapping[str, Any]], tblname: str, columns: Iterable[str], cur: cursor | None = None, item_cb: Callable[[Any], Any] | None = None, default_values: Mapping[str, Any] | None = None) None [source]#
Run the COPY command to insert the columns of each element of items into tblname.
- Parameters:
items – dictionaries of data to copy into tblname.
tblname – name of the destination table.
columns – columns of the destination table. Elements of items must have these set as keys.
default_values – dictionary of default values to use when inserting entries in tblname.
cur – a db cursor; if not given, a new cursor will be created.
item_cb – optional callback, run on each element of items, when it is copied.