Custom Database Queries
Project description
Few Utility Functions
Install All databases dependencies
pip install ddcDatabases[all]
Install MSSQL
pip install ddcDatabases[mssql]
Install PostgreSQL
pip install ddcDatabases[pgsql]
Databases
- Parameters for all classes are declared as OPTIONAL falling back to .env file
- All examples are using db_utils.py
- By default, the MSSQL class will open a session to the database, but the engine can be available
SQLITE
class Sqlite(
file_path: Optional[str] = None,
echo: Optional[bool] = None,
)
Session
import sqlalchemy as sa
from ddcDatabases import DBUtils, Sqlite
with Sqlite() as session:
utils = DBUtils(session)
stmt = sa.select(Table).where(Table.id == 1)
results = utils.fetchall(stmt)
for row in results:
print(row)
Sync Engine
from ddcDatabases import Sqlite
with Sqlite().engine() as engine:
...
MSSQL
class MSSQL(
host: Optional[str] = None,
port: Optional[int] = None,
username: Optional[str] = None,
password: Optional[str] = None,
database: Optional[str] = None,
schema: Optional[str] = None,
echo: Optional[bool] = None,
pool_size: Optional[int] = None,
max_overflow: Optional[int] = None
)
Sync Example
import sqlalchemy as sa
from ddcDatabases import DBUtils, MSSQL
with MSSQL() as session:
stmt = sa.select(Table).where(Table.id == 1)
db_utils = DBUtils(session)
results = db_utils.fetchall(stmt)
for row in results:
print(row)
Async Example
import sqlalchemy as sa
from ddcDatabases import DBUtilsAsync, MSSQL
async with MSSQL() as session:
stmt = sa.select(Table).where(Table.id == 1)
db_utils = DBUtilsAsync(session)
results = await db_utils.fetchall(stmt)
for row in results:
print(row)
Sync Engine
from ddcDatabases import MSSQL
with MSSQL().engine() as engine:
...
Async Engine
from ddcDatabases import MSSQL
async with MSSQL().async_engine() as engine:
...
PostgreSQL
- Using driver psycopg2 as default
class DBPostgres(
host: Optional[str] = None,
port: Optional[int] = None,
username: Optional[str] = None,
password: Optional[str] = None,
database: Optional[str] = None,
echo: Optional[bool] = None,
)
Sync Example
import sqlalchemy as sa
from ddcDatabases import DBUtils, PostgreSQL
with PostgreSQL() as session:
stmt = sa.select(Table).where(Table.id == 1)
db_utils = DBUtils(session)
results = db_utils.fetchall(stmt)
for row in results:
print(row)
Async Example
import sqlalchemy as sa
from ddcDatabases import DBUtilsAsync, PostgreSQL
async with PostgreSQL() as session:
stmt = sa.select(Table).where(Table.id == 1)
db_utils = DBUtilsAsync(session)
results = await db_utils.fetchall(stmt)
for row in results:
print(row)
Sync Engine
from ddcDatabases import PostgreSQL
with PostgreSQL().engine() as engine:
...
Async Engine
from ddcDatabases import PostgreSQL
async with PostgreSQL().async_engine() as engine:
...
DBUtils and DBUtilsAsync
- Take an open session as parameter
- Can use SQLAlchemy statements
- Execute function can be used to update, insert or any SQLAlchemy.text
from ddcDatabases import DBUtils
db_utils = DBUtils(session)
db_utils.fetchall(stmt) # returns a list of RowMapping
db_utils.fetchvalue(stmt) # fetch a single value, returning as string
db_utils.insert(stmt) # insert into model table
db_utils.deleteall(model) # delete all records from model
db_utils.insertbulk(model, list[dict]) # insert records into model from a list of dicts
db_utils.execute(stmt) # this is the actual execute from session
Source Code
Build
poetry build -f wheel
Run Tests and Get Coverage Report
poetry run coverage run --omit=./tests/* --source=./ddcDatabases -m pytest -v && poetry run coverage report
License
Released under the MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ddcdatabases-1.0.9.tar.gz
(7.7 kB
view details)
Built Distribution
File details
Details for the file ddcdatabases-1.0.9.tar.gz
.
File metadata
- Download URL: ddcdatabases-1.0.9.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c09d81997aaf9533b07ccf8bfaff61a85ebdfa6b029a77596770249905c586b3 |
|
MD5 | 9bbdd3855dce49344b734e6ccbdeb972 |
|
BLAKE2b-256 | af3f86fed33d9bf074e24d466d12e5161af82f46df46dd39dd0746d9ff403ea2 |
Provenance
The following attestation bundles were made for ddcdatabases-1.0.9.tar.gz
:
Publisher:
workflow.yml
on ddc/ddcDatabases
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
ddcdatabases-1.0.9.tar.gz
- Subject digest:
c09d81997aaf9533b07ccf8bfaff61a85ebdfa6b029a77596770249905c586b3
- Sigstore transparency entry: 152265574
- Sigstore integration time:
- Predicate type:
File details
Details for the file ddcdatabases-1.0.9-py3-none-any.whl
.
File metadata
- Download URL: ddcdatabases-1.0.9-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ac9cbfbf3002a1f57cda83ed26cb85135b0bf90dc7f99fa21fcb8765257bf59 |
|
MD5 | 50eaa580c9e4a00e283dd12c958c47da |
|
BLAKE2b-256 | 665ae12a616cd8dcaf25ea9d2185df8fcd776c2a837c3435c79a39ad3cebead9 |
Provenance
The following attestation bundles were made for ddcdatabases-1.0.9-py3-none-any.whl
:
Publisher:
workflow.yml
on ddc/ddcDatabases
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
ddcdatabases-1.0.9-py3-none-any.whl
- Subject digest:
9ac9cbfbf3002a1f57cda83ed26cb85135b0bf90dc7f99fa21fcb8765257bf59
- Sigstore transparency entry: 152265576
- Sigstore integration time:
- Predicate type: