Skip to main content

Databases Connection and Queries

Project description

Databases Connection and Queries

License Python PyPi PyPI Downloads Build Status

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

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 using Poe

poetry update --with test
poe tests

License

Released under the MIT License

Buy me a cup of coffee

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

ddcdatabases-1.0.12.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ddcdatabases-1.0.12-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file ddcdatabases-1.0.12.tar.gz.

File metadata

  • Download URL: ddcdatabases-1.0.12.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for ddcdatabases-1.0.12.tar.gz
Algorithm Hash digest
SHA256 474209a8d98f387d47d7bec8685dd50af321d10e9ae504851ec3bc4d07a4760b
MD5 fc2bce2b064a0640192c481a18524397
BLAKE2b-256 28ff2e2ea07f0f42b530733866d73a5b890f324b496bbc247120661a7c1068c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for ddcdatabases-1.0.12.tar.gz:

Publisher: workflow.yml on ddc/ddcDatabases

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ddcdatabases-1.0.12-py3-none-any.whl.

File metadata

  • Download URL: ddcdatabases-1.0.12-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for ddcdatabases-1.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 bf6eb0a6cd6f6e24d6526cf1c0b04365700312709655ef56243f30abcbb2ce5e
MD5 1aed56b436f098b217aba3d328f94be9
BLAKE2b-256 91730167d2872679bbb64f7edf13b0a2c91a94b792f33c8a43289203da62b7fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for ddcdatabases-1.0.12-py3-none-any.whl:

Publisher: workflow.yml on ddc/ddcDatabases

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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