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.77.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

scrippy_db-1.1.77-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: scrippy-db-1.1.77.tar.gz
  • Upload date:
  • Size: 6.4 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.77.tar.gz
Algorithm Hash digest
SHA256 fc923d0cba99efaf5fc36091934e9e4f51c91083d65bdf458d448b20069f6211
MD5 944ba8735684b2b2d329d45544169a34
BLAKE2b-256 de3f58483ccc3f4a58acf5e723607595909e5ee72b2e67d5070d6ed9ffe5d8ca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: scrippy_db-1.1.77-py3-none-any.whl
  • Upload date:
  • Size: 6.1 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.77-py3-none-any.whl
Algorithm Hash digest
SHA256 ed5f77bf098f90b29fb5a095438642ae0ff2bbede4aadcaaf6afa795c3b47f48
MD5 dc48cf4250641b5e3dc21448d13d654d
BLAKE2b-256 5caca736bd4b6f7a77623f5891d20103d273e2a3d2a4161e6b6b3f9539fa9a39

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