"Client de bases de données pour le cadriciel Scrippy"
Project description
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
- mysql-connector-python
Installation
With pip
pip install scrippy-db
Manual installation
git clone https://codeberg.org/scrippy/scrippy-db.git
cd scrippy-db
python -m pip install -r requirements.txt
make install
Usage
The scrippy_db.db
module provides the Database
object, which is intended to offer database usage functionality.
Connection to a database can be done either by directly providing connection parameters (db_type
, 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 (sqlite3
, postgres
, mysql
or oracle
).
Note that Oracle database is not tested as much as the other types of database.
Simple query
Query execution is performed with the Database.execute()
method, which accepts the following parameters:
query
: The query itself (required)params
: The query parameters in the exact order of appearance within the query (optional)verbose
: Boolean. If set toTrue
, each query, parameter and result will be sent to the logger.commit
: Boolean. When set toTrue
, a commit statement will be sent to the database immediately after the query execution. default toFalse
.exit_on_error
: Boolean. If set toTrue
, any error encountered while query execution will raise an error and exit the script after query rollback. Default toTrue
.
A query may contain one or more variable parameters requiring the query to be adapted to these parameters.
In conformance to Python Database API v2.0, parameters may be provided as sequence or mapping and will be bound to variables in the operation. If error occurs while query execution, the query will be rollbacked.
The query format depends on the database type. See the query formatting section below for further details.
For security reasons, never try to manage the interpolation of parameters inside the query yourself.
Multiple queries
A single query can be executed multiple times with multiple set of parameters using the Database.multi_query()
method, which accepts the following parameters:
query
: The query itself as a string with placeholders for parameters (required)paramlist
: A mandatory list of dictionaries specifying the needed parameters to be mapped on each execution of thequery
string.verbose
: Boolean. If set toTrue
, each query, parameter and result will be sent to the logger.commit
: Boolean. When set toTrue
, a commit statement will be sent to the database immediately after each query execution. default toFalse
.exit_on_error
: Boolean. If set toTrue
, any error encountered while query execution will raise an error and exit the script after query rollback. Default toTrue
.
In conformance to Python Database API v2.0, parameters may be provided as sequence or mapping and will be bound to variables in the operation. If error occurs while query execution, the query will be rollbacked.
This method is intended to insert data or update the database. It is not intended to retrieve data.
The query format depends on the database type. See the query formatting section below for further details.
For security reasons, never try to manage the interpolation of parameters inside the query yourself.
Transactions
Transactions can be performed withe the Database.transaction()
method, which accpets the following parmeters:
queries
: A list of dictionaries specifying all queries and needed parameters to be executed within the transaction. The SQL query to execute. The string query should use thenamed paramstyle
(see https://peps.python.org/pep-0249/#paramstyle) for parameters mapping.verbose
: Boolean. If set toTrue
, each query, parameter and result will be sent to the logger.exit_on_error
: Boolean. If set toTrue
, any error encountered while query execution will raise an error and exit the script after query rollback. Default toFalse
.
The whole transaction is committed only if all queries specified in the queries
argument are executed without error. If an error occurs within the transaction, the whole transaction is rollbacked.
This method is intended to insert data or update the database. It is not intended to retrieve data.
Each dictionary in the queries
list must provide at least a query
item specifying a query to execute. It may also provide a params
item which is a dictionary of parameters to be mapped onto the corresponding query
string (see the query()
method for further details).
The query format depends on the database type. See the query formatting section below for further details.
Query formatting
In order to prevent SQL injection, SQL queries must use placeholders for data binding.
While the principe is the same amongst the different supported database types, the placeholder formats vary.
Postgres and MySQL
Placeholders are identified by %(placeholder)s
. The name of the placeholders can then be utilized to transmit data to the query through a dictionary with keys matching the query parameter names.
Exemples:
query = "INSERT INTO people VALUES(%(firstname)s, %(lastname)s, %(age)s)"
data = {"firstname": "Luiggi", "lastname": "Vercotti", "age": 42}
query = "select * from people where age = %(age)s"
data = {"age": 42}
sqlite3 and Oracle
Placeholders are identified by a colon followed by the name of the placeholder. The name of the placeholders can then be utilized to transmit data to the query through a dictionary with keys matching the query parameter names.
Exemples:
query = "INSERT INTO people VALUES(:firstname, :lastname, :age)"
data = {"firstname": "Luiggi", "lastname": "Vercotti", "age": 42}
query = "select * from people where age = :age"
data = {"age": 42}
Examples
from scrippy_db import ScrippyDbError, logger
from scrippy_db.db import Database
# Simple query
with Database(db_type="pgsql", username="luiggi.vercotti", password="D3ADP4ARR0T",
host="db.flying.circus", port="5432", database="monty_python") as db:
query = "INSERT INTO people VALUES(:firstname, :lastname, :age)"
data = {"firstname": "Harry", "lastname": "Fink", "age": 42}
db.query(query=query, params=data, commit=True, exit_on_error=True)
# Multiple queries
with Database(db_type="pgsql", username="luiggi.vercotti", password="d34dP4rr0t",
host="db.flying.circus", port="5432", database="monty_python") as db:
query = "INSERT INTO people VALUES(:firstname, :lastname, :age)"
data = [{"firstname": "Harry", "lastname": "Fink", "age": 42},
{"firstname": "Eric", "lastname": "Parline", "age": 50},
{"firstname": None, "lastname": "Gumby", "age": 36}]
db.multi_query(query=query, params=data, commit=True,
exit_on_error=True, verbose=True)
# transaction
with Database(db_type="pgsql", username="luiggi.vercotti", password="d34dP4rr0t",
host="db.flying.circus", port="5432", database="monty_python") as db:
queries = [{"query": "INSERT INTO people VALUES(:firstname, :lastname, :age)",
"params": {"firstname": "Harry", "lastname": "Fink", "age": 42}},
{"query": "delete from people where age > :age", "params": 30}]
db.transaction(queries=queries, exit_on_error=True)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file scrippy-db-2.0.0.tar.gz
.
File metadata
- Download URL: scrippy-db-2.0.0.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2bdd463d0b9cfa2b6a29bf0cc6c02dc29b75d129b169803fbd9ab83ca0c3d61a |
|
MD5 | de6d1c239ec1fe8ff50e1c93a097eeea |
|
BLAKE2b-256 | 110dadde7d4dfaea835a81051bd0735caa3b05171f29c28ab646aa63f40713a6 |
File details
Details for the file scrippy_db-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: scrippy_db-2.0.0-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54cde1950b6821fc1b31ab4b227261481737c1c617dc910784853774051d738f |
|
MD5 | 992fac0492e47427a6adb70f8d7fae9f |
|
BLAKE2b-256 | 43b89c4ab19207a4d2dd1e07ad1a00d9faf705e1c744f36c0852f00d401610b4 |