swh.core.db package

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: psycopg2.extensions.connection, pool: Optional[psycopg2.pool.AbstractConnectionPool] = None)[source]

Bases: object

Base class for swh.*.*Db.

cf. swh.storage.db.Db, swh.archiver.db.ArchiverDb

static adapt_conn(conn: psycopg2.extensions.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.


connstring – libpq2 connection string

classmethod from_pool(pool: psycopg2.pool.AbstractConnectionPool) → BaseDbType[source]
put_conn() → None[source]
cursor(cur_arg: Optional[psycopg2.extensions.cursor] = None) → psycopg2.extensions.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[psycopg2.extensions.cursor][source]

context manager to execute within a DB transaction


a psycopg2 cursor

copy_to(items: Iterable[Mapping[str, Any]], tblname: str, columns: Iterable[str], cur: Optional[psycopg2.extensions.cursor] = None, item_cb: Optional[Callable[[Any], Any]] = None, default_values: Optional[Mapping[str, Any]] = None) → None[source]

Run the COPY command to insert the columns of each element of items into tblname.

  • 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: Optional[psycopg2.extensions.cursor] = None)[source]