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

Transaction context manager, breakable by raising Rollback. More...

Inheritance diagram for dblite.api.Transaction:
Inheritance graph

Public Member Functions

def __init__ (self, db, commit=True, exclusive=False, **kwargs)
 Constructs a new transaction. More...
 
def __del__ (self)
 Closes the transaction, if open. More...
 
def __enter__ (self)
 Context manager entry, returns Transaction object. More...
 
def __exit__ (self, exc_type, exc_val, exc_trace)
 Context manager exit, propagates raised errors except Rollback. More...
 
def close (self, commit=None)
 Closes the transaction, performing commit or rollback as specified. More...
 
def commit (self)
 Commits pending actions, if any. More...
 
def rollback (self)
 Rolls back pending actions, if any. 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 execute (self, sql, args=())
 Executes the SQL statement and returns database 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 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 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...
 
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...
 

Properties

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

Detailed Description

Transaction context manager, breakable by raising Rollback.

Note that in SQLite, a single connection has one shared transaction state, so it is highly recommended to use exclusive Transaction instances for any action queries, as concurrent transactions can interfere with one another otherwise.

Definition at line 450 of file api.py.

Constructor & Destructor Documentation

◆ __init__()

def dblite.api.Transaction.__init__ (   self,
  db,
  commit = True,
  exclusive = False,
**  kwargs 
)

Constructs a new transaction.

Note that parameter `exclusive` defaults to `True` when using SQLite.

Parameters
dbDatabase instance
commitwhether transaction commits automatically 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 in dblite.engines.sqlite.Transaction.

Definition at line 463 of file api.py.

◆ __del__()

def dblite.api.Transaction.__del__ (   self)

Closes the transaction, if open.

Definition at line 474 of file api.py.

Member Function Documentation

◆ __enter__()

def dblite.api.Transaction.__enter__ (   self)

Context manager entry, returns Transaction object.

Reimplemented in dblite.engines.postgres.Transaction, and dblite.engines.sqlite.Transaction.

Definition at line 466 of file api.py.

◆ __exit__()

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

Context manager exit, propagates raised errors except Rollback.

Reimplemented in dblite.engines.postgres.Transaction, and dblite.engines.sqlite.Transaction.

Definition at line 470 of file api.py.

◆ close()

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

Closes the transaction, performing commit or rollback as specified.

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 in dblite.engines.postgres.Transaction, and dblite.engines.sqlite.Transaction.

Definition at line 484 of file api.py.

◆ commit()

def dblite.api.Transaction.commit (   self)

Commits pending actions, if any.

Reimplemented in dblite.engines.postgres.Transaction, and dblite.engines.sqlite.Transaction.

Definition at line 487 of file api.py.

◆ rollback()

def dblite.api.Transaction.rollback (   self)

Rolls back pending actions, if any.

Reimplemented in dblite.engines.postgres.Transaction, and dblite.engines.sqlite.Transaction.

Definition at line 491 of file api.py.

Property Documentation

◆ database

dblite.api.Transaction.database = property
static

Returns transaction Database instance.

Definition at line 497 of file api.py.


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