|
dblite
1.3.0
Simple query interface for SQL databases
|
Convenience wrapper around psycopg2.ConnectionPool and Cursor. More...

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... | |
Convenience wrapper around psycopg2.ConnectionPool and Cursor.
Queries directly on the Database object use autocommit mode.
Definition at line 361 of file postgres.py.
| 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`.
| opts | Postgres connection string, or options dictionary as `dict(dbname=.., user=.., password=.., host=.., port=.., ..)` |
| kwargs | additional arguments given to engine constructor, e.g. `minconn=1, maxconn=4` |
Definition at line 394 of file postgres.py.
| 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.
| 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.
| def dblite.engines.postgres.Database.close | ( | self, | |
commit = None |
|||
| ) |
Closes the database and any pending transactions, if open.
| 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.
| def dblite.engines.postgres.Database.execute | ( | self, | |
| sql, | |||
args = () |
|||
| ) |
Executes SQL statement, returns psycopg cursor.
| sql | SQL statement to execute, with psycopg-specific parameter bindings, if any |
| args | dictionary 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.
| def dblite.engines.postgres.Database.executemany | ( | self, | |
| sql, | |||
| args | |||
| ) |
Executes the SQL statement against all parameter sequences.
| sql | SQL statement to execute, with psycopg-specific parameter bindings |
| args | iterable 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.
| def dblite.engines.postgres.Database.executescript | ( | self, | |
| sql | |||
| ) |
Executes the SQL as script of any number of statements.
Reloads internal schema structure from database.
| sql | script with one or more SQL statements |
Reimplemented from dblite.api.Queryable.
Definition at line 450 of file postgres.py.
| 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.
| 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.
| commit | commit at the end on success |
| autocommit | connection autocommit mode |
| schema | name of Postgres schema to use, if not using default `"public"` |
| lazy | if true, returns a named server-side cursor that fetches rows iteratively in batches; only supports making a single query |
| itersize | batch size in rows for server-side cursor |
Definition at line 549 of file postgres.py.
| 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.
| def dblite.engines.postgres.Database.transaction | ( | self, | |
commit = True, |
|||
exclusive = False, |
|||
| ** | kwargs | ||
| ) |
Returns a transaction context manager.
Context is breakable by raising Rollback.
| commit | whether transaction commits at exiting with-block |
| exclusive | whether entering a with-block is exclusive over other Transaction instances on this Database |
| kwargs | engine-specific arguments, like `schema="other", lazy=True` for Postgres |
Reimplemented from dblite.api.Database.
Definition at line 530 of file postgres.py.
|
static |
Registered adapters for Python->SQL, as {typeclass: converter}.
Definition at line 364 of file postgres.py.
|
static |
Registered converters for SQL->Python pending application, as {typename: converter}.
Definition at line 367 of file postgres.py.
| dblite.engines.postgres.Database.CONVERTERS |
Definition at line 599 of file postgres.py.
| dblite.engines.postgres.Database.dsn |
Data Source Name, as URL like `"postgresql://user@host/dbname"`.
Definition at line 397 of file postgres.py.
|
static |
Mutexes for exclusive transactions, as {Database instance: lock}.
Definition at line 370 of file postgres.py.
|
static |
Connection pool default size per Database.
Definition at line 373 of file postgres.py.
|
static |
Connection pools, as {Database: psycopg2.pool.ConnectionPool}.
Definition at line 376 of file postgres.py.
|
static |
Registered row factory.
Definition at line 379 of file postgres.py.
|
static |
Whether database connection is currently not open.
Definition at line 485 of file postgres.py.
|
static |
Database engine cursor object, or `None` if closed.
Definition at line 493 of file postgres.py.
|
static |
The custom row factory, if any, as `function(cursor, row tuple)`.
Definition at line 501 of file postgres.py.