Skip to content

Commit

Permalink
Merge pull request #2 from ddc/v1.0.8
Browse files Browse the repository at this point in the history
V1.0.8
  • Loading branch information
ddc authored Nov 28, 2024
2 parents 6246f7c + 49b087f commit 470582f
Show file tree
Hide file tree
Showing 17 changed files with 1,181 additions and 398 deletions.
25 changes: 25 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# MSSQL
MSSQL_HOST=localhost
MSSQL_PORT=1433
MSSQL_USER=sa
MSSQL_PASSWORD=L0caldev
MSSQL_DB_SCHEMA=dbo
MSSQL_DATABASE=master
MSSQL_POOL_SIZE=20
MSSQL_OVERFLOW=10
MSSQL_ECHO=False
MSSQL_ODBCDRIVER_VERSION=18


# PostgreSQL
POSTGRESQL_HOST=localhost
POSTGRESQL_PORT=5432
POSTGRESQL_USER=postgres
POSTGRESQL_PASSWORD=postgres
POSTGRESQL_DATABASE=postgres
POSTGRESQL_ECHO=False


# SQLite
SQLITE_FILE_PATH=sqlite.db
SQLITE_ECHO=False
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run Unit Tests on Aditional Branches
name: Run Unit Tests

on:
push:
Expand All @@ -25,15 +25,15 @@ jobs:
run: |
python -m pip install --upgrade pip setuptools wheel
python -m pip install poetry
python -m poetry update
- name: Install poetry dependencies
run: |
python -m poetry update
python -m poetry update --with test
python -m poetry install --extras all
- name: Test with pytest
- name: Run tests
run: |
python -m poetry run coverage run -m pytest -v
python -m poetry run coverage run --omit=./tests/* --source=./ddcDatabases -m pytest -v
- name: Generate Coverage Report
run: |
Expand Down
16 changes: 4 additions & 12 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test, Build, Release and Publish to PyPI
name: Build Release and Publish to PyPI

on:
push:
Expand All @@ -7,7 +7,7 @@ on:
- main

jobs:
test_build:
build:
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -25,15 +25,7 @@ jobs:
python -m pip install --upgrade pip setuptools wheel
python -m pip install poetry
- name: Install Poetry Dependencies
run: |
python -m poetry update
- name: Run tests
run: |
python -m poetry run coverage run -m pytest -v
- name: Build package
- name: Build package with poetry
run: |
python -m poetry build
Expand All @@ -46,7 +38,7 @@ jobs:
release:
runs-on: ubuntu-latest
needs:
- test_build
- build
env:
GITHUB_TOKEN: ${{ github.token }}
steps:
Expand Down
209 changes: 148 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,119 +6,206 @@
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A//actions-badge.atrox.dev/ddc/ddcDatabases/badge?ref=main&style=plastic&label=build&logo=none)](https://actions-badge.atrox.dev/ddc/ddcDatabases/goto?ref=main)


# Install
# Install All databases dependencies
```shell
pip install ddcDatabases
pip install ddcDatabases[all]
```

# Install MSSQL
```shell
pip install ddcDatabases[mssql]
```


# Install PostgreSQL
```shell
pip install ddcDatabases[pgsql]
```



# Databases
## DBSQLITE
+ Parameters for all classes are declared as OPTIONAL falling back to [.env](.env.example) file\
+ All examples are using [db_utils.py](ddcDatabases/db_utils.py)\
+ By default a session is always open\
+ But the engine can be available, examples bellow




## SQLITE
```
class DBSqlite(db_file_path: str, batch_size=100, echo=False, future=True)
class Sqlite(
file_path: Optional[str] = None,
echo: Optional[bool] = None,
)
```

#### Session
```python
import sqlalchemy as sa
from ddcDatabases import DBSqlite, DBUtils
dbsqlite = DBSqlite(database_file_path)
with dbsqlite.session() as session:
from ddcDatabases import DBUtils, Sqlite
with Sqlite() as session:
utils = DBUtils(session)
stmt = sa.select(Table).where(Table.id == 1)
db_utils = DBUtils(session)
results = db_utils.fetchall(stmt)
results = utils.fetchall(stmt)
for row in results:
print(row)
```

#### Sync Engine
```python
from ddcDatabases import Sqlite
with Sqlite().engine() as engine:
...
```




## DBPOSTGRES
+ Using driver "psycopg2" as default

## MSSQL
```
class DBPostgres(future=True, echo=False, drivername="psycopg2", **kwargs)
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
```python
import sqlalchemy as sa
from ddcDatabases import DBPostgres, DBUtils
db_configs = {
"username": username,
"password": password,
"host": host,
"port": port,
"database": database
}
dbpostgres = DBPostgres(**db_configs)
with dbpostgres.session() as session:
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)
```

+ DBUTILS
+ Uses SQLAlchemy statements
#### Async Example
```python
from ddcDatabases import DBUtils
db_utils = DBUtils(session)
db_utils.add(stmt)
db_utils.execute(stmt)
db_utils.fetchall(stmt)
db_utils.fetchone(stmt)
db_utils.fetch_value(stmt)
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
```python
from ddcDatabases import MSSQL
with MSSQL().engine() as engine:
...
```

#### Async Engine
```python
from ddcDatabases import MSSQL
async with MSSQL().async_engine() as engine:
...
```



## DBPOSTGRES ASYNC
+ Using driver "asyncpg"


## PostgreSQL
+ Using driver [psycopg2](https://pypi.org/project/psycopg2/) as default
```
class DBPostgresAsync(future=True, echo=False, drivername="asyncpg", **kwargs)
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
```python
import sqlalchemy as sa
from ddcDatabases import DBPostgresAsync, DBUtilsAsync
db_configs = {
"username": username,
"password": password,
"host": host,
"port": port,
"database": database
}
dbpostgres = DBPostgresAsync(**db_configs)
async with dbpostgres.session() as session:
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
```python
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)
```

+ DBUTILS ASYNC
+ Uses SQLAlchemy statements
#### Sync Engine
```python
from ddcDatabases import DBUtilsAsync
db_utils = DBUtilsAsync(session)
await db_utils.add(stmt)
await db_utils.execute(stmt)
await db_utils.fetchall(stmt)
await db_utils.fetchone(stmt)
await db_utils.fetch_value(stmt)
from ddcDatabases import PostgreSQL
with PostgreSQL().engine() as engine:
...
```

#### Async Engine
```python
from ddcDatabases import PostgreSQL
async with PostgreSQL().async_engine() as engine:
...
```

# Source Code
### Build
```shell
poetry build



## 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
```python
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
```


### Run Tests


# Source Code
### Build
```shell
poetry run coverage run -m pytest -v
poetry build -f wheel
```


### Get Coverage Report
### Run Tests and Get Coverage Report
```shell
poetry run coverage report
poetry run coverage run --omit=./tests/* --source=./ddcDatabases -m pytest -v && poetry run coverage report
```



# License
Released under the [MIT License](LICENSE)
11 changes: 6 additions & 5 deletions ddcDatabases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
from importlib.metadata import version
from typing import Literal, NamedTuple
from .db_utils import DBUtils, DBUtilsAsync
from .postgres import DBPostgres, DBPostgresAsync
from .sqlite import DBSqlite
from .mssql import MSSQL
from .postgresql import PostgreSQL
from .sqlite import Sqlite


__all__ = (
"DBSqlite",
"DBUtils",
"DBPostgres",
"DBPostgresAsync",
"DBUtilsAsync",
"MSSQL",
"PostgreSQL",
"Sqlite",
)


Expand Down
Loading

0 comments on commit 470582f

Please sign in to comment.