Skip to main content

Helper module for manage database connection

Project description

from zdb.mysql import ZMySQL

Zurck'z

ZDB Core, Layer for connect to mysql, postgresql or oracle from python


ZPy Database Core

Zurck'z Py

This package contains some helpers features for call function or stored procedures from python.

ZPy use the following packages:

  • mysql-connector-python
  • cx-Oracle
  • psycopg2

Requirements

  • Python 3.6+

Installation

Use the package manager pip to install py flask micro service core .

pip install zpy
pip install package_directory directory_to_install

Features

Contains some helper features with specific integrations.

  • Database
    • Functions executor
    • Stored Procedures executor
    • Autocommit is false by default
  • Utils
    • Context Manager Helper

Roadmap

  • ActiveRecord implementation
  • Cluster
  • Parallel parsed

Basic Usage

Basic Configuration

config = {
    "user": "",
    "password": "",
    "database": "",
    "host": "",
    "port": 3306
}

With single MySQL datasource

from zdb.mysql import ZMySQL
from zdb import ZDBTransact

# Create database instance for MySQL 
mysql_db = ZMySQL.from_of(user="", password="", host="", db_name="")

# If you only execute one operation you can call directly
# Connection automatically opened and commit and close
[user] = mysql_db.call("FN_GET_USER_BY_ID", list_params=[1])

# Open connection using Context Manager

with ZDBTransact(mysql_db) as tr:
    payments = mysql_db.call("FN_GET_USER_PAYMENTS", list_params=[1], connection=tr.session)

    for payment in payments:
        mysql_db.call("FN_UPDATE_PAYMENT", list_params=[payment['id']], connection=tr.session)

Multiple Datasources

# Define db mediator 
# Setup base configuration in ZMediator()
# The base configuration will be overwritten by add common values 
db_mngr = ZMediator(config, False)
.add_common("DB_NAME_1", "DB_USER", "DB_PASSWORD", True)  # Mark default ds
.add_common("DB_NAME_2", "DB_USER", "DB_PASSWORD")
.add_common("DB_NAME_3", "DB_USER", "DB_PASSWORD")

db_conn1 = db_mngr.default().new_connect()
db_conn2 = db_mngr.get("DB_NAME_1").new_connect()
db_conn3 = db_mngr.get("DB_NAME_3").new_connect()

try:
    # Execute function
    res = db_mngr.default().exec("FN_GET_USER_BY_ID(%d)", list_params=[1], ret_type=DBTypes.cursor)
    print(res)
    # Execute function
    res = db_mngr.get("DB_NAME_2").exec("FN_GET_USER_BY_ID(%d)", list_params=[1], ret_type=DBTypes.cursor)
    print(res)
    # Call sp
    res = db_mngr.get("DB_NAME_3").call("SP_GET_DATA", ret_type=DBTypes.cursor)
    print(res)
except Exception as e:
    logging.exception(e)
finally:
    # ⚠ Remember close opened connections
    db_conn1.close()
    db_conn2.close()
    db_conn3.close()
session = self.db.new_connect()
try:
    count = self.db.call('SP_GENERIC_GET_ROWS', list_params=['TAROLES'], ret_type=DBTypes.integer,
                         connection=session)
    return Pager.create(data.pagination, count)
finally:
    session.close()

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Authors

Noé Cruz

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

zpy_db_core-2.3.0.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

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

zpy_db_core-2.3.0-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file zpy_db_core-2.3.0.tar.gz.

File metadata

  • Download URL: zpy_db_core-2.3.0.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for zpy_db_core-2.3.0.tar.gz
Algorithm Hash digest
SHA256 efe24cbfc8f5ba1897da956623d3a5f37f2f03b3020c0805ac4a1078e6c99255
MD5 b646882f574597b7fb47260cdf27e6f1
BLAKE2b-256 f1da3dfb24f15a2a5973f7794132426e99c865d18abcead66b05cbcf1e141345

See more details on using hashes here.

File details

Details for the file zpy_db_core-2.3.0-py3-none-any.whl.

File metadata

  • Download URL: zpy_db_core-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 25.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for zpy_db_core-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd93da18d840288da42bdb16907be1f33fe96452e8f1affede68380c97f7c23a
MD5 a4eaa53f488c221cd3b94f566f4864fb
BLAKE2b-256 02bcbac0cf7e781b725597233b262e0b14124e1b341886daf2e578fe1e652b24

See more details on using hashes here.

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