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.

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.
  • debug whether to generate debugging messages. Default is False.
  • other named parameters are passed as additional connection parameters.
import anodb

db = anodb.DB("sqlite3", ":memory:", "acme-queries.sql")
db = anodb.DB("psycopg", "host=localhost dbname=acme", "acme-queries.sql")

Versions

Sources, documentation and issues are available on GitHub.

Latest version is 7.3 on 2023-01-21.

See all versions

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-7.3.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

anodb-7.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: anodb-7.3.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for anodb-7.3.tar.gz
Algorithm Hash digest
SHA256 04c3dd8376b1f6ec5bba0f943a38fd7cd8328d87442982b4106f8c2cc7490d94
MD5 d35bb75eadb4905550874650be6b52eb
BLAKE2b-256 3e7565a26fcc2223cd983b8d0e74f85b17c9c58637fa23f8423e3956897e7335

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: anodb-7.3-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for anodb-7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6ad7f001babc5c339134aeca0bc93cb9b465e4220a0de587c44fc087413167b8
MD5 59c2e287ff3780b717df3b216ec2b38b
BLAKE2b-256 1d61234e04fa7a66cdadcef3a9212272d917da42663566a7ffa20bdfe630531c

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