Skip to main content

"Client de bases de données pour le cadriciel Scrippy"

Project description

Build Status License Language

Scrippy, my scrangourou friend

scrippy_db

Generic database client for the Scrippy framework.

Requirements

Python modules

List of required modules

The modules listed below will be installed automatically.

  • psycopg2-binary
  • cx-Oracle
  • mysqlclient

Installation

Manual

git clone https://codeberg.org/scrippy/scrippy-db.git
cd scrippy-db
python -m pip install -r requirements.txt
make install

With pip

pip install scrippy-db

Usage

The scrippy_db.db module provides the Db object, which is intended to offer database usage functionality.

Connection to a database can be done either by directly providing connection parameters (username, host, database, port, password) to the constructor, or by providing the name of the service to connect to.

The db_type parameter allows you to specify the type of database (postgres, oracle, or mysql). The default value of this parameter is postgres.

Query execution is performed with the Db.execute() method, which accepts the following parameters:

  • request: The query itself (required)
  • params: The query parameters in the exact order of appearance within the query (optional)
  • verbose: Boolean indicating whether the query and its result should appear in the log. The log level must be set to at least debug.

A query may contain one or more variable parameters requiring the query to be adapted to these parameters.

For security reasons, some practices are strictly to be avoided, while others must be applied imperatively.

The parameters of a query must be passed in a tuple to the Db.execute() method, which will check the validity of your parameters.

Never try to manage the interpolation of parameters inside the query yourself.

Example

Data retrieval and conditional update of the database.

from scrippy_db import db

db_user = "harry.fink"
db_password = "dead_parrot"
db_host = "flying.circus"
db_port = "5432"
db_base = "monty_python"
db_type = "postgres"

app_name = "spanish_inquisition"
app_version = "0.42.0"
app_status = "Deployed"
date = datetime.datetime.now().strftime('%Y%m%d %H:%M:%S')

with db.Db(username=db_user,
           host=db_host,
           port=db_port,
           database=db_base,
           password=db_password,
           db_type=db_type) as database:
  # Check application status
  req = """select app_status, app_version, date
            from apps
            where app_name=%s;"""
  params = (app_name, )
  current_status = database.execute(req, params, verbose=True)
  if current_status != None:
    # The application is already registered, we display its current status
    # We update its status
    params = {"app_name": app_name,
              "app_version": app_version,
              "app_status": app_status,
              "date": datetime.datetime.now().strftime('%Y%m%d %H:%M:%S')}
    req = """insert into apps
                (app_name, app_version, app_status, date)
                values (%(app_name)s, %(app_version)s, %(app_status)s, %(date)s);"""
    result = database.execute(req, params, verbose=True)
  else:
    # The application has never been registered, we register the application and its status.
    req = """insert into apps
            (app_name, app_version, app_status, date)
            values (%(app_name)s, %(app_version)s, %(app_status)s, %(date)s);"""
    params = (app_name, app_version, app_status, date)
    result = database.execute(req, params)

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

scrippy-db-1.1.76.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

scrippy_db-1.1.76-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file scrippy-db-1.1.76.tar.gz.

File metadata

  • Download URL: scrippy-db-1.1.76.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for scrippy-db-1.1.76.tar.gz
Algorithm Hash digest
SHA256 5b793a0fc4b4b65175a538c60472b8fdafca2e9fcef89ca218dc00a1afbb1b40
MD5 9c9126b48580f0cc274eeb730de207b3
BLAKE2b-256 f14265ac22ca0494af78ff3b3b246ac108f7e30a4676a0be13c01daa1f557b31

See more details on using hashes here.

File details

Details for the file scrippy_db-1.1.76-py3-none-any.whl.

File metadata

  • Download URL: scrippy_db-1.1.76-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for scrippy_db-1.1.76-py3-none-any.whl
Algorithm Hash digest
SHA256 87562e33398ca5130301068eb6789976acb96dabc4199c3fe0c18f1bb7e87357
MD5 3e1fca3e43db7f4457b11e6ebf56921e
BLAKE2b-256 be69da6bffada53578e1a1eb3349a84b594a23a17fc61a517f77f95abd33a19c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page