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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: scrippy-db-1.1.78.tar.gz
  • Upload date:
  • Size: 6.7 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.78.tar.gz
Algorithm Hash digest
SHA256 0eabddd83e9d34ecf941938405aa208bad7aac672ec0ac8cabe0bd4959a11a91
MD5 8e27b711ff9790657fea6b7589ceb6c3
BLAKE2b-256 0a1cc457309155c4374dfa97d9606737fd18bd80345022f9af937e1a5fce4a57

See more details on using hashes here.

File details

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

File metadata

  • Download URL: scrippy_db-1.1.78-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.78-py3-none-any.whl
Algorithm Hash digest
SHA256 0b260c224616f6d98af7799950228ec1457bb6a66f4aac32659fa518066460a8
MD5 f6df21de11b808a2b044376045dacf68
BLAKE2b-256 3b744b5b73a7d75e3d91e4d978b15a8dc8cbe15610055e478c39047c59c77662

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