Skip to main content

Convenient Wrapper around AioSQL and a Database Connection

Project description

AnoDB

Convenient Wrapper around aiosql and a Database Connection.

Status Tests Coverage Python Databases Drivers Version Badges License

Description

This class creates a persistent database connection and imports SQL queries from a file as simple Python functions.

If the connection is broken, a new connection is attempted with increasing throttling delays.

Compared to aiosql, the point is not to need to pass a connection as an argument on each call: The DB class embeds both connection and query methods.

For concurrent programming (threads, greenlets…), a relevant setup should also consider thread-locals and pooling issues at some higher level.

Example

Install the module with pip install anodb or whatever method you like. Once available:

import anodb
# parameters: driver, connection string, SQL file
db = anodb.DB("sqlite3", "test.db", "test.sql")

db.do_some_insert(key=1, val="hello")
db.do_some_update(key=1, val="world")
print("data", db.do_some_select(key=1))
db.commit()

db.close()

With file test.sql containing something like:

-- name: do_some_select
SELECT * FROM Stuff WHERE key = :key;

-- name: do_some_insert!
INSERT INTO Stuff(key, val) VALUES (:key, :val);

-- name: do_some_update!
UPDATE Stuff SET val = :val WHERE key = :key;

Documentation

The anodb module provides the DB class which embeds both a PEP 249 database connection (providing methods commit, rollback, cursor, close and its connect counterpart to re-connect) and SQL queries wrapped into dynamically generated functions by aiosql. Such functions may be loaded from a string (add_queries_from_str) or a path (add_queries_from_path).

The DB constructor parameters are:

  • db the name of the database driver: sqlite3, psycopg, pymysql, see aiosql documentation for a list of supported drivers.
  • conn an optional connection string used to initiate a connection with the driver. For instance, psycopg accepts a libpq connection string such as: "host=db1.my.org port=5432 dbname=acme user=calvin …".
  • queries a path name or list of path names from which to read query definitions.
  • options a dictionary or string to pass additional connection parameters.
  • auto_reconnect whether to attempt a reconnection if the connection is lost. Default is True. Reconnection attempts are throttled exponentially following powers of two delays from 0.001 and capped at 30.0 seconds.
  • kwargs_only whether to only accept named parameters to python functions.
  • exception function to re-process database exceptions.
  • debug whether to generate debugging messages. Default is False.
  • other named parameters are passed as additional connection parameters.
import anodb

db = anodb.DB("sqlite3", "acme.db", "acme-queries.sql")
db = anodb.DB("duckdb", "acme.db", "acme-queries.sql")
db = anodb.DB("psycopg", "host=localhost dbname=acme", "acme-queries.sql")
db = anodb.DB("psycopg", None, "acme-queries.sql", host="localhost", user="calvin", password="...", dbname="acme")
db = anodb.DB("psycopg2", "host=localhost dbname=acme", "acme-queries.sql")
db = anodb.DB("pygresql", None, "acme-queries.sql", host="localhost:5432", user="calvin", password="...", database="acme")
db = anodb.DB("pg8000", None, "acme-queries.sql", host="localhost", port=5432, user="calvin", password="...", database="acme")
db = anodb.DB("MySQLdb", None, "acme-queries.sql", host="localhost", port=3306, user="calvin", password="...", database="acme")
db = anodb.DB("pymysql", None, "acme-queries.sql", host="localhost", port=3306, user="calvin", password="...", database="acme")
db = anodb.DB("mysql-connector", None, "acme-queries.sql", host="localhost", port=3306, user="calvin", password="...", database="acme")
db = anodb.DB("mariadb", None, "acme-queries.sql", host="localhost", port=3306, user="calvin", password="...", database="acme")

Versions

Sources, documentation and issues are available on GitHub.

See all versions and get packages from PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anodb-9.7.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

anodb-9.7-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file anodb-9.7.tar.gz.

File metadata

  • Download URL: anodb-9.7.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for anodb-9.7.tar.gz
Algorithm Hash digest
SHA256 4ed73982fd0c17d5927ccdc955706a97eccf651902277796103bd197aca8b0d5
MD5 b0e67d6c109625b4a66b00a234a8aa43
BLAKE2b-256 d0017ccfb73007dca25520136ae18839c0573a946a4595797e84180636218c5b

See more details on using hashes here.

Provenance

File details

Details for the file anodb-9.7-py3-none-any.whl.

File metadata

  • Download URL: anodb-9.7-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for anodb-9.7-py3-none-any.whl
Algorithm Hash digest
SHA256 5cf7fb8e685cead620141843c91a298aa20bfdeb5b6689d51a0bb3f2da3f213f
MD5 f8d302a3432ac80ae826d42974ba23c2
BLAKE2b-256 db6de73bd4c75c6af595d915e5094b8bb8fe9eac7f0a8c30c057822c9815c00c

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page