swh.core.db package#

Submodules#

Module contents#

swh.core.db.render_array(data) str[source]#

Render the data as a postgresql array

swh.core.db.render_array_element(element) str[source]#

Render an element from an array.

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 by BaseDb.copy_to().)

swh.core.db.escape_copy_column(column: str) str[source]#

Escape the text representation of a column for use by COPY.

swh.core.db.typecast_bytea(value, cur)[source]#
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

classmethod from_pool(pool: AbstractConnectionPool) BaseDbType[source]#
put_conn() None[source]#
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.

mktemp(tblname: str, cur: cursor | None = None)[source]#