Skip to main content

Dynamic generation of SQL queries

Project description

PyPI version Licence badge Python Actions Status Dependencies Downloads per month Last commit

chocolatine

Image

Chocolatine is a python library for dynamicaly generate SQL queries.

Why Chocolatine ?

If you know Python programming langage but you are not at your ease with SQL (or you don't want to manage SQL queries by yourself), you can use Chocolatine to generate some SQL queries for you. Of course, there are many other open source projects to do that, but honestly, they are more complex than most people expects from them (SQLAlchemy, Django ORM, etc...).

Installation

pip install chocolatine

Examples

Concatenation & filtering :

from chocolatine import Query, Col as _

query = Query().table("customer")\
               .select(
                    "customer_id",
                    (_("first_name") & ' ' & _("last_name")).upper().alias("name")
                )\
               .filter(_("first_name") >> "%E")
print(query)

Output :

SELECT customer_id, UPPER(CONCAT(first_name, ' ', last_name)) AS name
FROM customer
WHERE first_name LIKE '%E'

Group by, aggregation & filtering :

from chocolatine import Query, sum, Col as _

query = Query().table("payment")\
               .select(
                    "customer_id",
                    count().alias("payment_count"),
                    sum("amount").alias(">:total_amount")
                )\
               .group_by("customer_id")\
               .filter(count() > 1 & sum("amount") > 5.00)\
               .filter(_("customer_id") != 3)
print(query)

Output :

SELECT customer_id, COUNT(*) AS payment_count, SUM(amount) AS total_amount
FROM payment
WHERE customer_id != 3
GROUP BY customer_id
HAVING COUNT(*) > 1 AND SUM(amount) > 5.00
ORDER BY total_amount

Join :

from chocolatine import Query, Col as _

query = Query().table("film")\
               .select(
                    "title",
                    "film_id",
                    (_("first_name") & " " & _("last_name")).alias("name")
                )\
               .join("film_actor", "film_id")\
               .join("actor", "actor_id")\
               .build()
print(query)

Output :

SELECT title, film_id, CONCAT(first_name, ' ', last_name) AS name
FROM film
INNER JOIN film_actor
USING film_id
INNER JOIN actor
USING actor_id

SQL dialect

For now, Chocolatine is only designed to generate MySQL queries.

Basic functionnalities

  • Select, Insert, Update, Create, Alter & Delete queries
  • Distinct
  • Limit
  • Aliases
  • Ordering
  • Group by & aggregations
  • Joins
  • SQL functions
  • Concatenations
  • Unions
  • Case-When
  • Where / Having

Advanced functionnalities

  • Dynamic type checking
  • Protection against SQL injection attacks
  • Calls orders doesn't matter (except for join clauses)
  • Compact or extended SQL expressions
  • Whole system to deal with conditions :
    • Logical operators : equal, not equal, greater, lower, etc...
    • Boolean operators : and, or, not
    • Priority order with parenthesis
  • Automatic handling of filter conditions to fill the having or where clause depending on the given columns
  • Shortcuts:
    • Alias on column/table name
    • Help functions : Asc, Desc, Sum, Count, Upper, Lower, Concat, Second, Minute, Hour, Day, Month, Year
    • Column ordering
    • Like/In
  • Auto id on create queries (optionnal)
  • Nested queries
  • Choc-expr expression for SQL queries templating

Choc-expr library

  • Choc-expr is a mini templating librairie specially designed to handle such complex langage as SQL
  • It is a very conscise language designed for easy reading
  • See https://github.com/pe-brian/choc-expr for more informations

Tests

python pytest

Install Sakila database (mySQL) with Docker

docker run -p 3306:3306 -d sakiladb/mysql:latest

Contributors

  • peb-8 (main code)
  • ryry-shi (testing)

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

chocolatine-0.91.0.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

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

chocolatine-0.91.0-py3-none-any.whl (31.3 kB view details)

Uploaded Python 3

File details

Details for the file chocolatine-0.91.0.tar.gz.

File metadata

  • Download URL: chocolatine-0.91.0.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for chocolatine-0.91.0.tar.gz
Algorithm Hash digest
SHA256 71cb1636ae30d67e4a1eb5204e4ef845feb0d0fb7669a30910d63ea1c2f0ed32
MD5 80408e83e6fba75d82eb21b5c02ae8f4
BLAKE2b-256 6c6d349948f63f94c2e1d6363748828523d00a941761cebf5943d9589e306f15

See more details on using hashes here.

Provenance

The following attestation bundles were made for chocolatine-0.91.0.tar.gz:

Publisher: publish-package.yml on pe-brian/chocolatine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chocolatine-0.91.0-py3-none-any.whl.

File metadata

  • Download URL: chocolatine-0.91.0-py3-none-any.whl
  • Upload date:
  • Size: 31.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for chocolatine-0.91.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cee49a9ff7e997912cc4238521a7e1b33d7e64dcdcac6d25dbaa826eb788d7e0
MD5 5737277ecb7759f0b7a93fe229df7a9e
BLAKE2b-256 b7de00aad53be7ffc5cf7b110059afce5c5f3b34e47d24b82dfa317fcc1eecee

See more details on using hashes here.

Provenance

The following attestation bundles were made for chocolatine-0.91.0-py3-none-any.whl:

Publisher: publish-package.yml on pe-brian/chocolatine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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