Skip to content

Commit

Permalink
v1.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
ddc committed Nov 28, 2024
1 parent c1161ed commit c2907e7
Show file tree
Hide file tree
Showing 16 changed files with 405 additions and 271 deletions.
24 changes: 12 additions & 12 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# MSSQL
MSSQL_HOST="localhost"
MSSQL_PORT="1433"
MSSQL_USER="sa"
MSSQL_PASSWORD="L0caldev"
MSSQL_DB_SCHEMA="dbo"
MSSQL_DATABASE="master"
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="master"
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_FILE_PATH=sqlite.db
SQLITE_ECHO=False
2 changes: 1 addition & 1 deletion .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 Down
4 changes: 2 additions & 2 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- main

jobs:
test_build:
build:
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down Expand Up @@ -38,7 +38,7 @@ jobs:
release:
runs-on: ubuntu-latest
needs:
- test_build
- build
env:
GITHUB_TOKEN: ${{ github.token }}
steps:
Expand Down
168 changes: 113 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,41 @@ pip install ddcDatabases[pgsql]
```



# Databases
+ 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 Sqlite(db_file_path: str, echo=False)
class Sqlite(
file_path: Optional[str] = None,
echo: Optional[bool] = None,
)
```

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

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


Expand All @@ -45,96 +66,133 @@ with Sqlite() as session:

## MSSQL
```
class MSSQL(db_file_path: str, echo=False)
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.mssql import MSSQL
from ddcDatabases import DBUtils, MSSQL
with MSSQL() as session:
stmt = sa.select(Table).where(Table.id == 1)
results = session.fetchall(stmt)
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, 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:
...
```





## PostgreSQL
+ Using driver "psycopg2" as default
+ Using driver [psycopg2](https://pypi.org/project/psycopg2/) as default
```
class DBPostgres(future=True, echo=False, drivername="psycopg2", **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 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, 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)
```

+ 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, 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)
```


## DBPOSTGRES ASYNC
+ Using driver "asyncpg"
```
class DBPostgresAsync(future=True, echo=False, drivername="asyncpg", **kwargs)
#### Sync Engine
```python
from ddcDatabases import PostgreSQL
with PostgreSQL().engine() as engine:
...
```

#### Async Engine
```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:
stmt = sa.select(Table).where(Table.id == 1)
db_utils = DBUtilsAsync(session)
results = await db_utils.fetchall(stmt)
from ddcDatabases import PostgreSQL
async with PostgreSQL().async_engine() as engine:
...
```

+ DBUTILS ASYNC
+ Uses SQLAlchemy statements



## 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 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 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
```shell
Expand Down
7 changes: 5 additions & 2 deletions ddcDatabases/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import logging
from importlib.metadata import version
from typing import Literal, NamedTuple
from .db_utils import DBUtils, DBUtilsAsync
from .mssql import MSSQL
from .postgresql import PostgreSQL
from .sqlite import Sqlite


__all__ = (
"Sqlite",
"PostgreSQL",
"DBUtils",
"DBUtilsAsync",
"MSSQL",
"PostgreSQL",
"Sqlite",
)


Expand Down
8 changes: 4 additions & 4 deletions ddcDatabases/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import sys
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy import RowMapping
from sqlalchemy.engine import URL
from sqlalchemy.engine.result import MappingResult
from sqlalchemy.ext.asyncio import AsyncMappingResult, AsyncSession
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session
from .exceptions import (
DBDeleteAllDataException,
Expand Down Expand Up @@ -65,7 +65,7 @@ class DBUtils:
def __init__(self, session):
self.session = session

def fetchall(self, stmt) -> MappingResult:
def fetchall(self, stmt) -> list[RowMapping]:
cursor = None
try:
cursor = self.session.execute(stmt)
Expand Down Expand Up @@ -129,7 +129,7 @@ class DBUtilsAsync:
def __init__(self, session):
self.session = session

async def fetchall(self, stmt) -> AsyncMappingResult:
async def fetchall(self, stmt) -> list[RowMapping]:
cursor = None
try:
cursor = await self.session.execute(stmt)
Expand Down
Loading

0 comments on commit c2907e7

Please sign in to comment.