# Databases Session Connections and ORM Queries
[![Donate](https://img.shields.io/badge/Donate-PayPal-brightgreen.svg?style=plastic)](https://www.paypal.com/ncp/payment/6G9Z78QHUD4RJ)
[![License](https://img.shields.io/pypi/l/ddcDatabases)](https://github.com/ddc/ddcDatabases/blob/master/LICENSE)
[![PyPi](https://img.shields.io/pypi/v/ddcDatabases.svg)](https://pypi.python.org/pypi/ddcDatabases)
[![PyPI Downloads](https://static.pepy.tech/badge/ddcDatabases)](https://pepy.tech/projects/ddcDatabases)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A//actions-badge.atrox.dev/ddc/ddcDatabases/badge?ref=main&label=build&logo=none)](https://actions-badge.atrox.dev/ddc/ddcDatabases/goto?ref=main)
[![Python](https://img.shields.io/pypi/pyversions/ddcDatabases.svg)](https://www.python.org)
[//]: # ([![codecov](https://codecov.io/github/ddc/ddcDatabases/graph/badge.svg?token=E942EZII4Q)](https://codecov.io/github/ddc/ddcDatabases))
# Install only sqlite
```shell
pip install ddcDatabases
```
# Install All databases
```shell
pip install ddcDatabases[all]
```
# Install MSSQL
```shell
pip install ddcDatabases[mssql]
```
# Install MYSQL
```shell
pip install ddcDatabases[mysql]
```
# Install PostgreSQL
```shell
pip install ddcDatabases[pgsql]
```
# Install Oracle
```shell
pip install ddcDatabases[oracle]
```
# Install MONGODB
```shell
pip install ddcDatabases[mongodb]
```
# Databases
+ Parameters for all classes are declared as OPTIONAL falling back to [.env](./ddcDatabases/.env.example) file variables
+ 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 at `session.bind`
+ SYNC sessions defaults:
+ `autoflush is True`
+ `expire_on_commit is True`
+ `echo is False`
+ ASYNC sessions defaults:
+ `autoflush is True`
+ `expire_on_commit is False`
+ `echo 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
```python
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)
```
# 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
```python
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
```python
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)
```
# PostgreSQL or MySQL
```
class PostgreSQL(
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 Examples
```python
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)
```
```python
import sqlalchemy as sa
from ddcDatabases import DBUtils, MySQL
with MySQL() as session:
stmt = sa.text("SELECT * FROM users")
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(TableModel).where(TableModel.id == 1)
db_utils = DBUtilsAsync(session)
results = await db_utils.fetchall(stmt)
for row in results:
print(row)
```
# Oracle
```
class Oracle(
host: Optional[str] = None,
port: Optional[int] = None,
user: Optional[str] = None,
password: Optional[str] = None,
servicename: Optional[str] = None,
echo: Optional[bool] = None,
autoflush: Optional[bool] = None,
expire_on_commit: Optional[bool] = None,
extra_engine_args: Optional[dict] = None,
```
#### Sync Example using arguments instead of .env file
```python
credentials = {
"host": "127.0.0.1",
"user": "system",
"password": "oracle",
"servicename": "xe",
"echo": False,
}
import sqlalchemy as sa
from ddcDatabases import DBUtils, Oracle
with Oracle(**credentials) as session:
stmt = sa.text("SELECT * FROM system.help")
db_utils = DBUtils(session)
results = db_utils.fetchall(stmt)
for row in results:
print(row)
```
# MongoDB
```
class PostgreSQL(
host: Optional[str] = None,
port: Optional[int] = None,
user: Optional[str] = None,
password: Optional[str] = None,
database: Optional[str] = None,
batch_size: Optional[int] = None,
limit: Optional[int] = None,
)
```
#### Sync Example using arguments instead of .env file
```python
credentials = {
"host": "127.0.0.1",
"user": "admin",
"password": "admin",
"database": "admin",
}
from ddcDatabases.mongodb import MongoDB
from bson.objectid import ObjectId
with MongoDB(**credentials) as mongodb:
query = {"_id": ObjectId("6772cf60f27e7e068e9d8985")}
collection = "movies"
with mongodb.cursor(collection, query) as cursor:
for each in cursor:
print(each)
```
# ORM Engines
Using PostgreSQL as example
#### Sync Engine
```python
from ddcDatabases import PostgreSQL
with PostgreSQL() as session:
engine = session.bind
...
```
#### Async Engine
```python
from ddcDatabases import PostgreSQL
async with PostgreSQL() as session:
engine = await session.bind
...
```
# ORM 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 using Poe
```shell
poetry update --with test
poe tests
```
# License
Released under the [MIT License](LICENSE)
# Buy me a cup of coffee
+ [GitHub Sponsor](https://github.com/sponsors/ddc)
+ [ko-fi](https://ko-fi.com/ddcsta)
+ [Paypal](https://www.paypal.com/ncp/payment/6G9Z78QHUD4RJ)
Raw data
{
"_id": null,
"home_page": "https://pypi.org/project/ddcDatabases",
"name": "ddcDatabases",
"maintainer": "Daniel Costa",
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "python3, python-3, python, databases, database, ddcDatabases, mongodb, mongodb-database, mssql, mssql-database, mysql, mysql-database, oracle, oracle-database, pgsql, postgres, postgresql, postgressql, postgresql-database, sqlite, sqlite3, sqlite3-database",
"author": "Daniel Costa",
"author_email": "danieldcsta@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/80/fc/4aa7e2a435508c6feffe0e65026651a55072ef32f1d38912d689a2aef901/ddcdatabases-1.0.21.tar.gz",
"platform": null,
"description": "# Databases Session Connections and ORM Queries\n\n[![Donate](https://img.shields.io/badge/Donate-PayPal-brightgreen.svg?style=plastic)](https://www.paypal.com/ncp/payment/6G9Z78QHUD4RJ)\n[![License](https://img.shields.io/pypi/l/ddcDatabases)](https://github.com/ddc/ddcDatabases/blob/master/LICENSE)\n[![PyPi](https://img.shields.io/pypi/v/ddcDatabases.svg)](https://pypi.python.org/pypi/ddcDatabases)\n[![PyPI Downloads](https://static.pepy.tech/badge/ddcDatabases)](https://pepy.tech/projects/ddcDatabases)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A//actions-badge.atrox.dev/ddc/ddcDatabases/badge?ref=main&label=build&logo=none)](https://actions-badge.atrox.dev/ddc/ddcDatabases/goto?ref=main)\n[![Python](https://img.shields.io/pypi/pyversions/ddcDatabases.svg)](https://www.python.org)\n\n[//]: # ([![codecov](https://codecov.io/github/ddc/ddcDatabases/graph/badge.svg?token=E942EZII4Q)](https://codecov.io/github/ddc/ddcDatabases))\n\n\n\n# Install only sqlite\n```shell\npip install ddcDatabases\n```\n\n\n\n# Install All databases\n```shell\npip install ddcDatabases[all]\n```\n\n\n\n# Install MSSQL\n```shell\npip install ddcDatabases[mssql]\n```\n\n\n\n# Install MYSQL\n```shell\npip install ddcDatabases[mysql]\n```\n\n\n\n# Install PostgreSQL\n```shell\npip install ddcDatabases[pgsql]\n```\n\n\n\n# Install Oracle\n```shell\npip install ddcDatabases[oracle]\n```\n\n\n\n# Install MONGODB\n```shell\npip install ddcDatabases[mongodb]\n```\n\n\n\n# Databases\n+ Parameters for all classes are declared as OPTIONAL falling back to [.env](./ddcDatabases/.env.example) file variables\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 at `session.bind`\n+ SYNC sessions defaults:\n + `autoflush is True`\n + `expire_on_commit is True`\n + `echo is False`\n+ ASYNC sessions defaults:\n + `autoflush is True`\n + `expire_on_commit is False`\n + `echo is False`\n\n\n\n# SQLITE\n```\nclass Sqlite(\n filepath: Optional[str] = None,\n echo: Optional[bool] = None,\n autoflush: Optional[bool] = None,\n expire_on_commit: Optional[bool] = None,\n extra_engine_args: Optional[dict] = 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(TableModel).where(TableModel.id == 1)\n results = utils.fetchall(stmt)\n for row in results:\n print(row)\n```\n\n\n\n\n\n# MSSQL\n```\nclass MSSQL( \n host: Optional[str] = None,\n port: Optional[int] = None,\n user: 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 autoflush: Optional[bool] = None,\n expire_on_commit: Optional[bool] = None,\n extra_engine_args: Optional[dict] = 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(TableModel).where(TableModel.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(TableModel).where(TableModel.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\n\n\n\n\n# PostgreSQL or MySQL\n```\nclass PostgreSQL(\n host: Optional[str] = None,\n port: Optional[int] = None,\n user: Optional[str] = None,\n password: Optional[str] = None,\n database: Optional[str] = None,\n echo: Optional[bool] = None,\n autoflush: Optional[bool] = None,\n expire_on_commit: Optional[bool] = None,\n engine_args: Optional[dict] = None,\n)\n```\n\n#### Sync Examples\n```python\nimport sqlalchemy as sa\nfrom ddcDatabases import DBUtils, PostgreSQL\nwith PostgreSQL() as session:\n stmt = sa.select(TableModel).where(TableModel.id == 1)\n db_utils = DBUtils(session)\n results = db_utils.fetchall(stmt)\n for row in results:\n print(row)\n```\n```python\nimport sqlalchemy as sa\nfrom ddcDatabases import DBUtils, MySQL\nwith MySQL() as session:\n stmt = sa.text(\"SELECT * FROM users\")\n db_utils = DBUtils(session)\n results = db_utils.fetchall(stmt)\n for row in results:\n print(row)\n```\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(TableModel).where(TableModel.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\n\n\n# Oracle\n```\nclass Oracle(\n host: Optional[str] = None,\n port: Optional[int] = None,\n user: Optional[str] = None,\n password: Optional[str] = None,\n servicename: Optional[str] = None,\n echo: Optional[bool] = None,\n autoflush: Optional[bool] = None,\n expire_on_commit: Optional[bool] = None,\n extra_engine_args: Optional[dict] = None,\n```\n#### Sync Example using arguments instead of .env file\n```python\ncredentials = {\n \"host\": \"127.0.0.1\",\n \"user\": \"system\",\n \"password\": \"oracle\",\n \"servicename\": \"xe\",\n \"echo\": False,\n}\n\nimport sqlalchemy as sa\nfrom ddcDatabases import DBUtils, Oracle\nwith Oracle(**credentials) as session:\n stmt = sa.text(\"SELECT * FROM system.help\")\n db_utils = DBUtils(session)\n results = db_utils.fetchall(stmt)\n for row in results:\n print(row)\n```\n\n\n\n\n\n\n\n\n# MongoDB\n```\nclass PostgreSQL(\n host: Optional[str] = None,\n port: Optional[int] = None,\n user: Optional[str] = None,\n password: Optional[str] = None,\n database: Optional[str] = None,\n batch_size: Optional[int] = None,\n limit: Optional[int] = None,\n)\n```\n#### Sync Example using arguments instead of .env file\n```python\ncredentials = {\n \"host\": \"127.0.0.1\",\n \"user\": \"admin\",\n \"password\": \"admin\",\n \"database\": \"admin\",\n}\n\nfrom ddcDatabases.mongodb import MongoDB\nfrom bson.objectid import ObjectId\nwith MongoDB(**credentials) as mongodb:\n query = {\"_id\": ObjectId(\"6772cf60f27e7e068e9d8985\")}\n collection = \"movies\"\n with mongodb.cursor(collection, query) as cursor:\n for each in cursor:\n print(each)\n```\n\n\n\n\n\n\n\n\n# ORM Engines\nUsing PostgreSQL as example\n\n#### Sync Engine\n```python\nfrom ddcDatabases import PostgreSQL\nwith PostgreSQL() as session:\n engine = session.bind\n ...\n```\n\n#### Async Engine\n```python\nfrom ddcDatabases import PostgreSQL\nasync with PostgreSQL() as session:\n engine = await session.bind\n ...\n```\n\n\n\n\n# ORM 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\n# Run Tests and Get Coverage Report using Poe\n```shell\npoetry update --with test\npoe tests\n```\n\n\n\n# License\nReleased under the [MIT License](LICENSE)\n\n\n\n# Buy me a cup of coffee\n+ [GitHub Sponsor](https://github.com/sponsors/ddc)\n+ [ko-fi](https://ko-fi.com/ddcsta)\n+ [Paypal](https://www.paypal.com/ncp/payment/6G9Z78QHUD4RJ)\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Databases Connection and Queries",
"version": "1.0.21",
"project_urls": {
"Homepage": "https://pypi.org/project/ddcDatabases",
"Repository": "https://github.com/ddc/ddcDatabases"
},
"split_keywords": [
"python3",
" python-3",
" python",
" databases",
" database",
" ddcdatabases",
" mongodb",
" mongodb-database",
" mssql",
" mssql-database",
" mysql",
" mysql-database",
" oracle",
" oracle-database",
" pgsql",
" postgres",
" postgresql",
" postgressql",
" postgresql-database",
" sqlite",
" sqlite3",
" sqlite3-database"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "84e6a5d198b7ba0f40a5c55335e4e91224d4390174b02e0a1194bb9d9b76eb78",
"md5": "0232557b4876d73873574a147de3d6d2",
"sha256": "a04f2dead8b26070f25d9f6d3d858a4570e1dec926f788212d29f5c182d3042b"
},
"downloads": -1,
"filename": "ddcdatabases-1.0.21-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0232557b4876d73873574a147de3d6d2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 13724,
"upload_time": "2024-12-31T13:07:05",
"upload_time_iso_8601": "2024-12-31T13:07:05.256828Z",
"url": "https://files.pythonhosted.org/packages/84/e6/a5d198b7ba0f40a5c55335e4e91224d4390174b02e0a1194bb9d9b76eb78/ddcdatabases-1.0.21-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "80fc4aa7e2a435508c6feffe0e65026651a55072ef32f1d38912d689a2aef901",
"md5": "b13e2e50931039140fa25464dcbd256b",
"sha256": "52bcdeb40e90c0b658ecb2e682fae4548ae6f7ac5b7ab031ea0def854fe4fad0"
},
"downloads": -1,
"filename": "ddcdatabases-1.0.21.tar.gz",
"has_sig": false,
"md5_digest": "b13e2e50931039140fa25464dcbd256b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 11025,
"upload_time": "2024-12-31T13:07:08",
"upload_time_iso_8601": "2024-12-31T13:07:08.343874Z",
"url": "https://files.pythonhosted.org/packages/80/fc/4aa7e2a435508c6feffe0e65026651a55072ef32f1d38912d689a2aef901/ddcdatabases-1.0.21.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-31 13:07:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ddc",
"github_project": "ddcDatabases",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "ddcdatabases"
}