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

Database instance. More...

Inheritance diagram for dblite.api.Database:
Inheritance graph

Public Member Functions

def __del__ (self)
 Closes the database, if open. 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 open (self)
 Opens database connection if not already open. More...
 
def transaction (self, commit=True, exclusive=None, **kwargs)
 Returns a transaction context manager. 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

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

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

Database instance.

Usable as an auto-closing context manager.

Queries directly on the Database object use autocommit mode.

Note that the database connection is not opened immediately on construction.

Definition at line 379 of file api.py.

Constructor & Destructor Documentation

◆ __del__()

def dblite.api.Database.__del__ (   self)

Closes the database, if open.

Definition at line 405 of file api.py.

Member Function Documentation

◆ __enter__()

def dblite.api.Database.__enter__ (   self)

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

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

Definition at line 397 of file api.py.

◆ __exit__()

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

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

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

Definition at line 401 of file api.py.

◆ close()

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

Definition at line 419 of file api.py.

◆ open()

def dblite.api.Database.open (   self)

Opens database connection if not already open.

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

Definition at line 409 of file api.py.

◆ transaction()

def dblite.api.Database.transaction (   self,
  commit = True,
  exclusive = None,
**  kwargs 
)

Returns a transaction context manager.

Context is breakable by raising Rollback.

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

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

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

Definition at line 394 of file api.py.

Property Documentation

◆ row_factory

dblite.api.Database.row_factory = property
static

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

Definition at line 424 of file api.py.


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