dblite  1.3.0
Simple query interface for SQL databases
dblite.engines.postgres.Database Class Reference

Convenience wrapper around psycopg2.ConnectionPool and Cursor. More...

Inheritance diagram for dblite.engines.postgres.Database:
Inheritance graph

Public Member Functions

def __init__ (self, opts, **kwargs)
 Creates a new Database instance for Postgres. More...
 
def __enter__ (self)
 Context manager entry, opens database if not already open, returns Database object. More...
 
def __exit__ (self, exc_type, exc_val, exc_trace)
 Context manager exit, closes database and any pending transactions if open. More...
 
def close (self, commit=None)
 Closes the database and any pending transactions, if open. More...
 
def execute (self, sql, args=())
 Executes SQL statement, returns psycopg cursor. More...
 
def executemany (self, sql, args)
 Executes the SQL statement against all parameter sequences. More...
 
def executescript (self, sql)
 Executes the SQL as script of any number of statements. More...
 
def init_pool (cls, db, minconn=POOL_SIZE[0], maxconn=POOL_SIZE[1], **kwargs)
 Initializes connection pool for Database if not already initialized. More...
 
def make_cursor (self, commit=False, autocommit=False, schema=None, lazy=False, itersize=None)
 Context manager for psycopg connection cursor. More...
 
def open (self)
 Opens database connection if not already open. More...
 
def transaction (self, commit=True, exclusive=False, **kwargs)
 Returns a transaction context manager. More...
 
- Public Member Functions inherited from dblite.api.Database
def __del__ (self)
 Closes the database, if open. More...
 
- Public Member Functions inherited from dblite.api.Queryable
def delete (self, table, where=(), **kwargs)
 Convenience wrapper for database DELETE, returns affected row count. More...
 
def fetchall (self, table, cols="*", where=(), group=(), order=(), limit=(), **kwargs)
 Convenience wrapper for database SELECT and fetch all. More...
 
def fetchone (self, table, cols="*", where=(), group=(), order=(), limit=(), **kwargs)
 Convenience wrapper for database SELECT and fetch one. More...
 
def select (self, table, cols="*", where=(), group=(), order=(), limit=(), **kwargs)
 Convenience wrapper for database SELECT, returns database cursor. More...
 
def update (self, table, values, where=(), **kwargs)
 Convenience wrapper for database UPDATE, returns affected row count. More...
 
- Public Member Functions inherited from dblite.engines.postgres.Queryable
def insert (self, table, values=(), **kwargs)
 Convenience wrapper for database INSERT, returns inserted row ID. More...
 
def insertmany (self, table, rows=(), **kwargs)
 Convenience wrapper for database multiple INSERTs, returns list of inserted row IDs. More...
 
def makeSQL (self, action, table, cols="*", where=(), group=(), order=(), limit=(), values=(), kwargs=None)
 Returns (SQL statement string, parameter dict). More...
 
def quote (cls, value, force=False)
 Returns identifier in quotes and proper-escaped for queries, if value needs quoting (has non-alphanumerics, starts with number, or is reserved). More...
 

Public Attributes

 CONVERTERS
 
 dsn
 Data Source Name, as URL like `"postgresql://user@host/dbname"`. More...
 

Static Public Attributes

dictionary ADAPTERS = {}
 Registered adapters for Python->SQL, as {typeclass: converter}. More...
 
dictionary CONVERTERS = {}
 Registered converters for SQL->Python pending application, as {typename: converter}. More...
 
 MUTEX = collections.defaultdict(threading.RLock)
 Mutexes for exclusive transactions, as {Database instance: lock}. More...
 
tuple POOL_SIZE = (1, 4)
 Connection pool default size per Database. More...
 
dictionary POOLS = {}
 Connection pools, as {Database: psycopg2.pool.ConnectionPool}. More...
 
 ROW_FACTORY = None
 Registered row factory. More...
 
- Static Public Attributes inherited from dblite.api.Queryable
 ENGINE = None
 Underlying database engine, "sqlite" for SQLite3 and "postgres" for PostgreSQL. More...
 
- Static Public Attributes inherited from dblite.engines.postgres.Queryable
string ENGINE = "postgres"
 Name of underlying database engine. More...
 
tuple OPS
 Recognized binary operators for makeSQL() More...
 

Properties

 closed = property
 Whether database connection is currently not open. More...
 
 cursor = property
 Database engine cursor object, or `None` if closed. More...
 
 row_factory = property
 The custom row factory, if any, as `function(cursor, row tuple)`. More...
 
- Properties inherited from dblite.api.Database
 row_factory = property
 The custom row factory, if any, as `function(cursor, row tuple)`. More...
 
- Properties inherited from dblite.api.Queryable
 closed = property
 Whether currently not open. More...
 
 cursor = property
 Database engine cursor object, or `None` if closed. More...
 

Detailed Description

Convenience wrapper around psycopg2.ConnectionPool and Cursor.

Queries directly on the Database object use autocommit mode.

Definition at line 361 of file postgres.py.

Constructor & Destructor Documentation

◆ __init__()

def dblite.engines.postgres.Database.__init__ (   self,
  opts,
**  kwargs 
)

Creates a new Database instance for Postgres.

By default uses a pool of 1..4 connections.

Connection parameters can also be specified in OS environment, via standard Postgres environment variables like `PGUSER` and `PGPASSWORD`.

Parameters
optsPostgres connection string, or options dictionary as `dict(dbname=.., user=.., password=.., host=.., port=.., ..)`
kwargsadditional arguments given to engine constructor, e.g. `minconn=1, maxconn=4`

Definition at line 394 of file postgres.py.

Member Function Documentation

◆ __enter__()

def dblite.engines.postgres.Database.__enter__ (   self)

Context manager entry, opens database if not already open, returns Database object.

Reimplemented from dblite.api.Database.

Definition at line 406 of file postgres.py.

◆ __exit__()

def dblite.engines.postgres.Database.__exit__ (   self,
  exc_type,
  exc_val,
  exc_trace 
)

Context manager exit, closes database and any pending transactions if open.

Reimplemented from dblite.api.Database.

Definition at line 412 of file postgres.py.

◆ close()

def dblite.engines.postgres.Database.close (   self,
  commit = None 
)

Closes the database and any pending transactions, if open.

Parameters
commit`True` for explicit commit on open transactions, `False` for explicit rollback on open transactions, `None` defaults to `commit` flag from transaction creations

Reimplemented from dblite.api.Database.

Definition at line 471 of file postgres.py.

◆ execute()

def dblite.engines.postgres.Database.execute (   self,
  sql,
  args = () 
)

Executes SQL statement, returns psycopg cursor.

Parameters
sqlSQL statement to execute, with psycopg-specific parameter bindings, if any
argsdictionary for %(name)s placeholders, or a sequence for positional s placeholders, or None

Reimplemented from dblite.api.Queryable.

Definition at line 426 of file postgres.py.

◆ executemany()

def dblite.engines.postgres.Database.executemany (   self,
  sql,
  args 
)

Executes the SQL statement against all parameter sequences.

Parameters
sqlSQL statement to execute, with psycopg-specific parameter bindings
argsiterable of query parameters, as dictionaries for %(name)s placeholders or sequences for positional s placeholders

Reimplemented from dblite.api.Queryable.

Definition at line 438 of file postgres.py.

◆ executescript()

def dblite.engines.postgres.Database.executescript (   self,
  sql 
)

Executes the SQL as script of any number of statements.

Reloads internal schema structure from database.

Parameters
sqlscript with one or more SQL statements

Reimplemented from dblite.api.Queryable.

Definition at line 450 of file postgres.py.

◆ init_pool()

def dblite.engines.postgres.Database.init_pool (   cls,
  db,
  minconn = POOL_SIZE[0],
  maxconn = POOL_SIZE[1],
**  kwargs 
)

Initializes connection pool for Database if not already initialized.

Definition at line 583 of file postgres.py.

◆ make_cursor()

def dblite.engines.postgres.Database.make_cursor (   self,
  commit = False,
  autocommit = False,
  schema = None,
  lazy = False,
  itersize = None 
)

Context manager for psycopg connection cursor.

Creates a new cursor on an unused connection and closes it when exiting context, committing changes if specified.

Parameters
commitcommit at the end on success
autocommitconnection autocommit mode
schemaname of Postgres schema to use, if not using default `"public"`
lazyif true, returns a named server-side cursor that fetches rows iteratively in batches; only supports making a single query
itersizebatch size in rows for server-side cursor
Returns
psycopg2 Cursor

Definition at line 549 of file postgres.py.

◆ open()

def dblite.engines.postgres.Database.open (   self)

Opens database connection if not already open.

Reimplemented from dblite.api.Database.

Definition at line 456 of file postgres.py.

◆ transaction()

def dblite.engines.postgres.Database.transaction (   self,
  commit = True,
  exclusive = False,
**  kwargs 
)

Returns a transaction context manager.

Context is breakable by raising Rollback.

Parameters
commitwhether transaction commits at exiting with-block
exclusivewhether entering a with-block is exclusive over other Transaction instances on this Database
kwargsengine-specific arguments, like `schema="other", lazy=True` for Postgres

Reimplemented from dblite.api.Database.

Definition at line 530 of file postgres.py.

Member Data Documentation

◆ ADAPTERS

dictionary dblite.engines.postgres.Database.ADAPTERS = {}
static

Registered adapters for Python->SQL, as {typeclass: converter}.

Definition at line 364 of file postgres.py.

◆ CONVERTERS [1/2]

dictionary dblite.engines.postgres.Database.CONVERTERS = {}
static

Registered converters for SQL->Python pending application, as {typename: converter}.

Definition at line 367 of file postgres.py.

◆ CONVERTERS [2/2]

dblite.engines.postgres.Database.CONVERTERS

Definition at line 599 of file postgres.py.

◆ dsn

dblite.engines.postgres.Database.dsn

Data Source Name, as URL like `"postgresql://user@host/dbname"`.

Definition at line 397 of file postgres.py.

◆ MUTEX

dblite.engines.postgres.Database.MUTEX = collections.defaultdict(threading.RLock)
static

Mutexes for exclusive transactions, as {Database instance: lock}.

Definition at line 370 of file postgres.py.

◆ POOL_SIZE

tuple dblite.engines.postgres.Database.POOL_SIZE = (1, 4)
static

Connection pool default size per Database.

Definition at line 373 of file postgres.py.

◆ POOLS

dictionary dblite.engines.postgres.Database.POOLS = {}
static

Connection pools, as {Database: psycopg2.pool.ConnectionPool}.

Definition at line 376 of file postgres.py.

◆ ROW_FACTORY

dblite.engines.postgres.Database.ROW_FACTORY = None
static

Registered row factory.

Definition at line 379 of file postgres.py.

Property Documentation

◆ closed

dblite.engines.postgres.Database.closed = property
static

Whether database connection is currently not open.

Definition at line 485 of file postgres.py.

◆ cursor

dblite.engines.postgres.Database.cursor = property
static

Database engine cursor object, or `None` if closed.

Definition at line 493 of file postgres.py.

◆ row_factory

dblite.engines.postgres.Database.row_factory = property
static

The custom row factory, if any, as `function(cursor, row tuple)`.

Definition at line 501 of file postgres.py.


The documentation for this class was generated from the following file: