zensols.dbpg package

Submodules

zensols.dbpg.postgres module

Postgres implementation of the ConnectionManager.

class zensols.dbpg.postgres.PostgresConnectionManager(db_name, host, port, user, password, create_db=True, capture_lastrowid=False, fast_insert=False)[source]

Bases: ConnectionManager

An Postgres connection factory.

DROP_SQL = 'drop owned by {user}'
EXISTS_SQL = "select count(*) from information_schema.tables where table_schema = 'public'"
__init__(db_name, host, port, user, password, create_db=True, capture_lastrowid=False, fast_insert=False)
capture_lastrowid: bool = False

If True, select the last row for each query.

create()[source]

Create a connection to the database.

create_db: bool = True

If True create the database if it does not already exist.

db_name: str

Database name on the server.

drop()[source]

Remove all objects from the database or the database itself.

For SQLite, this deletes the file. In database implementations, this might drop all objects from the database. Regardless, it is expected that create is able to recreate the database after this action.

Returns:

whether the database was dropped

execute(conn, sql, params, row_factory, map_fn)[source]

See execute().

Return type:

Tuple[Union[dict, tuple, DataFrame]]

execute_no_read(conn, sql, params=())[source]

Return database level information such as row IDs rather than the results of a query. Use this when inserting data to get a row ID.

Parameters:
  • conn – the connection object with the database

  • sql – the SQL statement used on the connection’s cursor

  • params – the parameters given to the SQL statement (populated with ?) in the statement

See:

DbPersister.execute_no_read().

Return type:

int

fast_insert: bool = False

If True use insertmany on the cursor for fast insert in to the database.

host: str

The host name of the database.

insert_rows(conn, sql, rows, errors, set_id_fn, map_fn)[source]

Insert a tuple of rows in the database and return the current row ID.

Parameters:
  • rows (list) – a sequence of tuples of data (or an object to be transformed, see map_fn in column order of the SQL provided by the entry insert_name

  • errors (str) – if this is the string raise then raise an error on any exception when invoking the database execute, otherwise use ignore to ignore errors

  • set_id_fn – a callable that is given the data to be inserted and the row ID returned from the row insert as parameters

  • map_fn – if not None, used to transform the given row in to a tuple that is used for the insertion

Return type:

int

Returns:

the rowid of the last row inserted

See InsertableBeanDbPersister.insert_rows().

password: str

The login password.

port: str

The host port of the database.

user: str

The user (if any) to log in with.

Module contents