Skip to main content

DB clients

Project description

Separate DB client from DivineGift

How to use it

Install this packages before use

  • cx_Oracle - Oracle driver
  • sqlalchemy-pytds - MSSQL driver
  • psycopg2-binary - Postgres driver

Usage

You should define dict with db_conn creditional. For example:

Oracle

Install oracle driver:

pip install cx_oracle
db_conn = {
    "db_user": "dbuser",             # username
    "db_pass": "dbpass",             # password
    "db_host": "dbhost",             # host (ip, fqdn). could be empty if we connect via tns
    "db_port": "",                   # port (string). could be empty if we connect via tns
    "db_name": "dbname",             # database name
    "db_schm": "",                   # db scheme if not equal username
    "dialect": "oracle"              # if use cx_Oracle or oracle+another_dialect
}

MSSQL

Install mssql driver:

pip install sqlalchemy-pytds
db_conn = {
    "db_user": "dbuser",             # username
    "db_pass": "dbpass",             # password
    "db_host": "",                   # host (ip, fqdn). could be empty if we connect via tns
    "db_port": "",                   # port (string). could be empty if we connect via tns
    "db_name": "dbname",             # database name
    "db_schm": "",                   # db scheme if not equal username
    "dialect": "mssql+pytds"         # mssql dialect
}

Postgres

Install postgres driver:

pip install psycopg2
db_conn = {
    "db_user": "dbuser",             # username
    "db_pass": "dbpass",             # password
    "db_host": "",                   # host (ip, fqdn). could be empty if we connect via tns
    "db_port": "",                   # port (string). could be empty if we connect via tns
    "db_name": "dbname",             # database name
    "db_schm": "",                   # db scheme if not equal username
    "dialect": "postgresql+psycopg2" # dialect
}

Create connection

Use class DBClient to create connection to DB. Old-styled functions, which contained in divinegift.db module directly, are deprecated but still works.

from divinegift.db import DBClient
connection = DBClient(db_conn)            # db_conn - variable which was described above
# Describe which fields you wants to method get_conn will returned (possible fields are 'engine', 'conn' and 'metadata')
engine, conn, metadata = connection.get_conn(fields=['engine', 'conn', 'metadata'])  

If you need to call stored procedure with db cursors you should use raw connection.

from divinegift.db import DBClient
connection = DBClient(db_conn, do_initialize=False)            # db_conn - variable which was described above
connection.set_raw_conn()
conn = connection.get_conn(fields='conn')  

Get data from sript (file or just string)

After you got "connection" variable you can get data from file or from str variable directly.

from divinegift.db import DBClient
connection = DBClient(db_conn)

result = connection.get_data('path/to/scripts/some_script.sql')
# or you can use str variable:
script = 'select * from dual'
result = connection.get_data(script)
print(result)
>>>[{'dummy': 'X'}]

You can use specific encoding for your files (by default it's 'cp1251'). Just put it into args:

result = connection.get_data('path/to/scripts/some_script.sql', encoding='utf8')

Also you can add some variables into your script (e.g. date) and then you can pass it into a function:

from divinegift.db import DBClient
connection = DBClient(db_conn)

script = """select * from dual
where dummy = '$param'"""
parameters = {'param': 'X'}
result = connection.get_data(script, **parameters)
# Or another variant
result = connection.get_data(script, param='X')
print(result)
>>>[{'dummy': 'X'}]

Run script without getting data

You can run script without recieving data. You should use divinegift.db.DBClient.run_script for this like get_data, e.g.:

from divinegift.db import Connection
connection = Connection(db_conn)
connection.run_script('path/to/scripts/some_script.sql')

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

dgdb-1.0.0a0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

dgdb-1.0.0a0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file dgdb-1.0.0a0.tar.gz.

File metadata

  • Download URL: dgdb-1.0.0a0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dgdb-1.0.0a0.tar.gz
Algorithm Hash digest
SHA256 e77015ec63ece2e1bf5edf246e92c5cf1b2484a6a4ebf0493c796a96b472a8f3
MD5 99879eb71f969b6d76bced5464a8ff6e
BLAKE2b-256 cce112bdd05d6ba283118a0c243cd4ab69929c4124b94dbf5fd02d2d5912f360

See more details on using hashes here.

File details

Details for the file dgdb-1.0.0a0-py3-none-any.whl.

File metadata

  • Download URL: dgdb-1.0.0a0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dgdb-1.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 816e23e9b7d9f1f5996de0f521eb96db0978e8df8336e5193d16db4f3ff71851
MD5 529950ae5f663c24a728d74939db5891
BLAKE2b-256 4be707a0c84a0c6019ecfcff7e83ce820d0575f7406de9a3204d6117b2f232b0

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