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

Transaction context manager, provides convenience methods for queries. More...

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

Public Member Functions

def __init__ (self, db, commit=True, exclusive=False, schema=None, lazy=False, itersize=None, **__)
 Creates a transaction context manager. More...
 
def __enter__ (self)
 Context manager entry, opens cursor, returns Transaction object. More...
 
def __exit__ (self, exc_type, exc_val, exc_trace)
 Context manager exit, closes cursor, commits or rolls back as specified on creation. More...
 
def close (self, commit=None)
 Closes the transaction, performing commit or rollback as specified, and releases database connection back to connection pool. More...
 
def commit (self)
 Commits pending actions, if any. 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 rollback (self)
 Rolls back pending actions, if any. More...
 
- Public Member Functions inherited from dblite.api.Transaction
def __init__ (self, db, commit=True, exclusive=False, **kwargs)
 Constructs a new transaction. More...
 
def __del__ (self)
 Closes the transaction, 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...
 

Properties

 closed = property
 Whether transaction is currently not open. More...
 
 cursor = property
 Database engine cursor object, or `None` if closed. More...
 
 database = property
 Returns transaction Database instance. More...
 
- Properties inherited from dblite.api.Transaction
 database = property
 Returns transaction Database instance. 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...
 

Additional Inherited Members

- 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...
 

Detailed Description

Transaction context manager, provides convenience methods for queries.

Supports server-side cursors; those can only be used for making a single query.

Must be closed explicitly if not used as context manager in a with-block. Block can be exited early by raising Rollback.

Definition at line 634 of file postgres.py.

Constructor & Destructor Documentation

◆ __init__()

def dblite.engines.postgres.Transaction.__init__ (   self,
  db,
  commit = True,
  exclusive = False,
  schema = None,
  lazy = False,
  itersize = None,
**  __ 
)

Creates a transaction context manager.

Context is breakable by raising Rollback.

Parameters
dbDatabase instance
commitwhether transaction commits automatically at exiting with-block
exclusivewhether entering a with-block is exclusive over other Transaction instances Database
schemasearch_path to use in this transaction
lazyif true, uses a server-side cursor to fetch results from server iteratively in batches instead of all at once, supports one single query only
itersizebatch size for server-side cursor (defaults to 2000 rows)

Definition at line 651 of file postgres.py.

Member Function Documentation

◆ __enter__()

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

Context manager entry, opens cursor, returns Transaction object.

Reimplemented from dblite.api.Transaction.

Definition at line 662 of file postgres.py.

◆ __exit__()

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

Context manager exit, closes cursor, commits or rolls back as specified on creation.

Reimplemented from dblite.api.Transaction.

Definition at line 675 of file postgres.py.

◆ close()

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

Closes the transaction, performing commit or rollback as specified, and releases database connection back to connection pool.

Required if not using transaction as context manager in a with-block.

Parameters
commit`True` for explicit commit, `False` for explicit rollback, `None` defaults to `commit` flag from creation

Reimplemented from dblite.api.Transaction.

Definition at line 698 of file postgres.py.

◆ commit()

def dblite.engines.postgres.Transaction.commit (   self)

Commits pending actions, if any.

Reimplemented from dblite.api.Transaction.

Definition at line 745 of file postgres.py.

◆ execute()

def dblite.engines.postgres.Transaction.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 716 of file postgres.py.

◆ executemany()

def dblite.engines.postgres.Transaction.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 728 of file postgres.py.

◆ executescript()

def dblite.engines.postgres.Transaction.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 740 of file postgres.py.

◆ rollback()

def dblite.engines.postgres.Transaction.rollback (   self)

Rolls back pending actions, if any.

Reimplemented from dblite.api.Transaction.

Definition at line 749 of file postgres.py.

Property Documentation

◆ closed

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

Whether transaction is currently not open.

Definition at line 755 of file postgres.py.

◆ cursor

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

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

Definition at line 762 of file postgres.py.

◆ database

dblite.engines.postgres.Transaction.database = property
static

Returns transaction Database instance.

Definition at line 771 of file postgres.py.


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