# Few Utility Functions
[![License](https://img.shields.io/github/license/ddc/ddcDatabases.svg?style=plastic)](https://github.com/ddc/ddcDatabases/blob/master/LICENSE)
[![Python](https://img.shields.io/badge/Python-3.10+-blue.svg?style=plastic)](https://www.python.org)
[![PyPi](https://img.shields.io/pypi/v/ddcDatabases.svg?style=plastic)](https://pypi.python.org/pypi/ddcDatabases)
[![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 All databases dependencies
```shell
pip install ddcDatabases[all]
```
# Install MSSQL
```shell
pip install ddcDatabases[mssql]
```
# Install PostgreSQL
```shell
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, 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
```python
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
```python
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
```python
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
```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](https://pypi.org/project/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
```python
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
```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)
```
#### Sync Engine
```python
from ddcDatabases import PostgreSQL
with PostgreSQL().engine() as engine:
...
```
#### Async Engine
```python
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
```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
```
# Source Code
### Build
```shell
poetry build -f wheel
```
### Run Tests and Get Coverage Report
```shell
poetry run coverage run --omit=./tests/* --source=./ddcDatabases -m pytest -v && poetry run coverage report
```
# License
Released under the [MIT License](LICENSE)
Raw data
{
"_id": null,
"home_page": "https://github.com/ddc/ddcDatabases",
"name": "ddcDatabases",
"maintainer": "Daniel Costa",
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "python3, databases, ddcDatabases",
"author": "Daniel Costa",
"author_email": "danieldcsta@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e7/f5/97ab9604af2cd8a0402c8df745f83f691aed0dacf86cc24c638f8d4ed35c/ddcdatabases-1.0.10.tar.gz",
"platform": null,
"description": "# Few Utility Functions\n\n[![License](https://img.shields.io/github/license/ddc/ddcDatabases.svg?style=plastic)](https://github.com/ddc/ddcDatabases/blob/master/LICENSE)\n[![Python](https://img.shields.io/badge/Python-3.10+-blue.svg?style=plastic)](https://www.python.org)\n[![PyPi](https://img.shields.io/pypi/v/ddcDatabases.svg?style=plastic)](https://pypi.python.org/pypi/ddcDatabases)\n[![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)\n\n\n# Install All databases dependencies\n```shell\npip install ddcDatabases[all]\n```\n\n# Install MSSQL\n```shell\npip install ddcDatabases[mssql]\n```\n\n\n# Install PostgreSQL\n```shell\npip install ddcDatabases[pgsql]\n```\n\n\n\n# Databases\n+ Parameters for all classes are declared as OPTIONAL falling back to [.env](.env.example) file\n+ All examples are using [db_utils.py](ddcDatabases/db_utils.py)\n+ By default, the MSSQL class will open a session to the database, but the engine can be available\n\n\n\n\n## SQLITE\n```\nclass Sqlite(\n file_path: Optional[str] = None,\n echo: Optional[bool] = None,\n)\n```\n\n#### Session\n```python\nimport sqlalchemy as sa\nfrom ddcDatabases import DBUtils, Sqlite\nwith Sqlite() as session:\n utils = DBUtils(session)\n stmt = sa.select(Table).where(Table.id == 1)\n results = utils.fetchall(stmt)\n for row in results:\n print(row)\n```\n\n#### Sync Engine\n```python\nfrom ddcDatabases import Sqlite\nwith Sqlite().engine() as engine:\n ...\n```\n\n\n\n\n\n## MSSQL\n```\nclass MSSQL( \n host: Optional[str] = None,\n port: Optional[int] = None,\n username: Optional[str] = None,\n password: Optional[str] = None,\n database: Optional[str] = None,\n schema: Optional[str] = None,\n echo: Optional[bool] = None,\n pool_size: Optional[int] = None,\n max_overflow: Optional[int] = None\n)\n```\n\n#### Sync Example\n```python\nimport sqlalchemy as sa\nfrom ddcDatabases import DBUtils, MSSQL\nwith MSSQL() as session:\n stmt = sa.select(Table).where(Table.id == 1)\n db_utils = DBUtils(session)\n results = db_utils.fetchall(stmt)\n for row in results:\n print(row)\n```\n\n#### Async Example\n```python\nimport sqlalchemy as sa\nfrom ddcDatabases import DBUtilsAsync, MSSQL\nasync with MSSQL() as session:\n stmt = sa.select(Table).where(Table.id == 1)\n db_utils = DBUtilsAsync(session)\n results = await db_utils.fetchall(stmt)\n for row in results:\n print(row)\n```\n\n#### Sync Engine\n```python\nfrom ddcDatabases import MSSQL\nwith MSSQL().engine() as engine:\n ...\n```\n\n#### Async Engine\n```python\nfrom ddcDatabases import MSSQL\nasync with MSSQL().async_engine() as engine:\n ...\n```\n\n\n\n\n\n## PostgreSQL\n+ Using driver [psycopg2](https://pypi.org/project/psycopg2/) as default\n```\nclass DBPostgres(\n host: Optional[str] = None,\n port: Optional[int] = None,\n username: Optional[str] = None,\n password: Optional[str] = None,\n database: Optional[str] = None,\n echo: Optional[bool] = None,\n)\n```\n\n#### Sync Example\n```python\nimport sqlalchemy as sa\nfrom ddcDatabases import DBUtils, PostgreSQL\nwith PostgreSQL() as session:\n stmt = sa.select(Table).where(Table.id == 1)\n db_utils = DBUtils(session)\n results = db_utils.fetchall(stmt)\n for row in results:\n print(row)\n```\n\n#### Async Example\n```python\nimport sqlalchemy as sa\nfrom ddcDatabases import DBUtilsAsync, PostgreSQL\nasync with PostgreSQL() as session:\n stmt = sa.select(Table).where(Table.id == 1)\n db_utils = DBUtilsAsync(session)\n results = await db_utils.fetchall(stmt)\n for row in results:\n print(row)\n```\n\n#### Sync Engine\n```python\nfrom ddcDatabases import PostgreSQL\nwith PostgreSQL().engine() as engine:\n ...\n```\n\n#### Async Engine\n```python\nfrom ddcDatabases import PostgreSQL\nasync with PostgreSQL().async_engine() as engine:\n ...\n```\n\n\n\n\n## DBUtils and DBUtilsAsync\n+ Take an open session as parameter\n+ Can use SQLAlchemy statements\n+ Execute function can be used to update, insert or any SQLAlchemy.text\n```python\nfrom ddcDatabases import DBUtils\ndb_utils = DBUtils(session)\ndb_utils.fetchall(stmt) # returns a list of RowMapping\ndb_utils.fetchvalue(stmt) # fetch a single value, returning as string\ndb_utils.insert(stmt) # insert into model table\ndb_utils.deleteall(model) # delete all records from model\ndb_utils.insertbulk(model, list[dict]) # insert records into model from a list of dicts\ndb_utils.execute(stmt) # this is the actual execute from session\n```\n\n\n\n\n# Source Code\n### Build\n```shell\npoetry build -f wheel\n```\n\n\n### Run Tests and Get Coverage Report\n```shell\npoetry run coverage run --omit=./tests/* --source=./ddcDatabases -m pytest -v && poetry run coverage report\n```\n\n\n\n# License\nReleased under the [MIT License](LICENSE)\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Custom Database Queries",
"version": "1.0.10",
"project_urls": {
"Homepage": "https://github.com/ddc/ddcDatabases",
"Repository": "https://github.com/ddc/ddcDatabases"
},
"split_keywords": [
"python3",
" databases",
" ddcdatabases"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8b610052e81c45726af37c9d438d1bd069f34e54329a7d2f640f4de80e35e303",
"md5": "60dac866cd91b75b611d106de80500c4",
"sha256": "e2104d8682153bd2e088bee452ea662605535d40f81bca7b83c81d6984474e8a"
},
"downloads": -1,
"filename": "ddcdatabases-1.0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "60dac866cd91b75b611d106de80500c4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 9661,
"upload_time": "2024-11-28T18:06:17",
"upload_time_iso_8601": "2024-11-28T18:06:17.191142Z",
"url": "https://files.pythonhosted.org/packages/8b/61/0052e81c45726af37c9d438d1bd069f34e54329a7d2f640f4de80e35e303/ddcdatabases-1.0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e7f597ab9604af2cd8a0402c8df745f83f691aed0dacf86cc24c638f8d4ed35c",
"md5": "9d0af51055ca9b67987602eb01d9513a",
"sha256": "8a257c7513c5d3adc227d0b68a037f8eda6a20fccfe514fe41137970144bab9a"
},
"downloads": -1,
"filename": "ddcdatabases-1.0.10.tar.gz",
"has_sig": false,
"md5_digest": "9d0af51055ca9b67987602eb01d9513a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 7742,
"upload_time": "2024-11-28T18:06:18",
"upload_time_iso_8601": "2024-11-28T18:06:18.311018Z",
"url": "https://files.pythonhosted.org/packages/e7/f5/97ab9604af2cd8a0402c8df745f83f691aed0dacf86cc24c638f8d4ed35c/ddcdatabases-1.0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-28 18:06:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ddc",
"github_project": "ddcDatabases",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "ddcdatabases"
}