Databases Connection and Queries
Project description
Databases Connection and Queries
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 variables
- 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 at
session.bind - SYNC sessions defaults:
autoflush is Trueexpire_on_commit is Trueecho is False
- ASYNC sessions defaults:
autoflush is Trueexpire_on_commit is Falseecho is False
SQLITE
class Sqlite(
filepath: Optional[str] = None,
echo: Optional[bool] = None,
autoflush: Optional[bool] = None,
expire_on_commit: Optional[bool] = None,
extra_engine_args: Optional[dict] = None,
)
Session
import sqlalchemy as sa
from ddcDatabases import DBUtils, Sqlite
with Sqlite() as session:
utils = DBUtils(session)
stmt = sa.select(TableModel).where(TableModel.id == 1)
results = utils.fetchall(stmt)
for row in results:
print(row)
Sync Engine
from ddcDatabases import Sqlite
with Sqlite() as session:
engine = session.bind
...
MSSQL
class MSSQL(
host: Optional[str] = None,
port: Optional[int] = None,
user: 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,
autoflush: Optional[bool] = None,
expire_on_commit: Optional[bool] = None,
extra_engine_args: Optional[dict] = None,
)
Sync Example
import sqlalchemy as sa
from ddcDatabases import DBUtils, MSSQL
with MSSQL() as session:
stmt = sa.select(TableModel).where(TableModel.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(TableModel).where(TableModel.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() as session:
engine = session.bind
...
Async Engine
from ddcDatabases import MSSQL
async with MSSQL() as session:
engine = await session.bind
...
PostgreSQL
- Using driver psycopg2 as default
class DBPostgres(
host: Optional[str] = None,
port: Optional[int] = None,
user: Optional[str] = None,
password: Optional[str] = None,
database: Optional[str] = None,
echo: Optional[bool] = None,
autoflush: Optional[bool] = None,
expire_on_commit: Optional[bool] = None,
engine_args: Optional[dict] = None,
)
Sync Example
import sqlalchemy as sa
from ddcDatabases import DBUtils, PostgreSQL
with PostgreSQL() as session:
stmt = sa.select(TableModel).where(TableModel.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(TableModel).where(TableModel.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() as session:
engine = session.bind
...
Async Engine
from ddcDatabases import PostgreSQL
async with PostgreSQL() as session:
engine = await session.bind
...
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 using Poe
poetry update --with test
poe tests
License
Released under the MIT License
Buy me a cup of coffee
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ddcdatabases-1.0.18.tar.gz.
File metadata
- Download URL: ddcdatabases-1.0.18.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ed18c1532af27778ba66ebe4d7f2181d67f02d833d7f5e314fcbb6c482e32fe
|
|
| MD5 |
9071b7e41703f207fabfb5cdc0cac589
|
|
| BLAKE2b-256 |
38cdabaacb7efaa80dfaa291f6c08e8396810b8bfb79650496569d164f64ce32
|
Provenance
The following attestation bundles were made for ddcdatabases-1.0.18.tar.gz:
Publisher:
workflow.yml on ddc/ddcDatabases
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ddcdatabases-1.0.18.tar.gz -
Subject digest:
8ed18c1532af27778ba66ebe4d7f2181d67f02d833d7f5e314fcbb6c482e32fe - Sigstore transparency entry: 156737620
- Sigstore integration time:
-
Permalink:
ddc/ddcDatabases@9c84173def575251edfbcced95e563aa3493b0da -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ddc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@9c84173def575251edfbcced95e563aa3493b0da -
Trigger Event:
push
-
Statement type:
File details
Details for the file ddcdatabases-1.0.18-py3-none-any.whl.
File metadata
- Download URL: ddcdatabases-1.0.18-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1156dfbccad8ae4597b4e390355a89e4b6d61ccc0b907a8750afd9a4b35362d
|
|
| MD5 |
b5466871afefd3119415b6149122a673
|
|
| BLAKE2b-256 |
4a3ca1091567bad7615e48af9941ce366ff6154789087df4528965ec8334ef54
|
Provenance
The following attestation bundles were made for ddcdatabases-1.0.18-py3-none-any.whl:
Publisher:
workflow.yml on ddc/ddcDatabases
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ddcdatabases-1.0.18-py3-none-any.whl -
Subject digest:
f1156dfbccad8ae4597b4e390355a89e4b6d61ccc0b907a8750afd9a4b35362d - Sigstore transparency entry: 156737621
- Sigstore integration time:
-
Permalink:
ddc/ddcDatabases@9c84173def575251edfbcced95e563aa3493b0da -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ddc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@9c84173def575251edfbcced95e563aa3493b0da -
Trigger Event:
push
-
Statement type: