Skip to main content

A Pythonic DSL for SQL conditions and expressions.

Project description

expressQL

expressQL is a flexible, Pythonic Domain-Specific Language (DSL) for constructing complex SQL conditions and expressions safely and expressively.
It is designed to reduce boilerplate, prevent common SQL mistakes, and allow arithmetic, logical, and chained comparisons directly in Python syntax.

Features

✅ Arithmetic expressions with automatic SQL translation
✅ Logical composition (AND, OR, NOT) using natural Python operators
✅ Chained inequalities (50 < col("age") < 80)
✅ SQL-safe placeholder management
✅ Null-safe operations (is_null, not_null)
✅ Set membership (IN, NOT IN)
✅ Supports custom SQL functions (Func(...))
✅ Fluent API for advanced condition building

Quick Example

from expressql import col, cols, Func

age, salary, department = cols("age", "salary", "department")

condition = ((age > 30) * (department == "HR")) + (salary > 50000)
#alternatively
#condition = ((age > 30) & (department == "HR")) & (salary > 5000)

print(condition.placeholder_pair())
# ('((age > ?) AND (department = ?)) OR (salary > ?)', [30, 'HR', 50000])

Key Concepts

1️⃣ Expressions & Comparisons

from expressql import col

age = col("age")
condition = (age + 10) > 50

SQL:

(age + 10) > 50

2️⃣ Chained Conditions

score = col("score")
cond = (50 < score) < 80  # Equivalent to 50 < score < 80

SQL:

(score > 50 AND score < 80)

3️⃣ Logical Composition

Use * or & for AND, + or | for OR, and ~ for NOT:

salary = col("salary")
dept = col("department")
cond = (salary > 40000) * (dept == "IT")

SQL:

(salary > 40000 AND department = 'IT')

4️⃣ Functions

from expressql import Func

total = col("salary") + col("bonus")
cond = Func("LOG", total) > 10

SQL:

LOG((salary + bonus)) > 10

5️⃣ NULL and Set Operations

city = col("city")
region = col("region")

cond = city.is_null + region.isin(["North", "South"])

SQL:

(city IS NULL OR region IN ('North', 'South'))

Advanced Usage

Check the provided examples:

python simple_examples.py
python complex_examples.py

These showcase arithmetic, chains, null logic, function usage, and complex logical combinations.

FAQ

Why doesn't expressQL include full query builders?
This module focuses on expressions and conditions. I will make (or probably already did) a module that integrates these functionalities into query building.

Can you make the column name validation more permissive?
I'm not sure why you would want that. However, I have a version that does a simpler check and allows passing forgiven characters. If it proves relevant, I will probably update it.

Every condition string comes wrapped in brackets, is there any way to avoid it? The conditions wrap themselves in brackets to pass it to other functions that might be calling it. Avoiding this could be implemented by setting a check '_first = True' into the functions, but it's just one extra pair of parenthesis on the final expression

🔥 Tip
If you're using this in a larger query builder or ORM, let me know —
I might have an expressQL-querybuilder in the works 👀.


Contributing

Contributions are welcome!
If you have suggestions for improvements, new features, or find any bugs, feel free to open an issue or submit a pull request.
I'm especially interested in ideas for better query builders and integrations with ORMs.

Roadmap

  • 📌 More built-in SQL functions (expressQL.functions)
  • 📌 Chain-aware logical optimizations
  • 📌 Full Query Builder integration (maybe expressQL-querybuilder)
  • 📌 Async support and better placeholder systems (for more DB engines)
  • 📌 Better error tracing and SQL preview options

License

MIT License — free for personal and commercial use.

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

expressql-0.1.0.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

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

expressql-0.1.0-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

Details for the file expressql-0.1.0.tar.gz.

File metadata

  • Download URL: expressql-0.1.0.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for expressql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 802c249b8bdded56d1100282213c3eb8f7a3fe040f042df08589d6275ed17af7
MD5 a62fb27d4ac4f66f3878ed534e059019
BLAKE2b-256 bbba7aff70d154b10eabb43372ec133568ba0fb77158a81582e2bc0209ec32d9

See more details on using hashes here.

File details

Details for the file expressql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: expressql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for expressql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7bda550d4beb6cd11741c7476ed960d13d19504eed1342bc4119af7a51524317
MD5 70e68093fc724ab9b0be829eda5698fc
BLAKE2b-256 3effebe2c8d5cba7b34b98c1f30addd1c3f6408632e4ff04ed646191f5a114ca

See more details on using hashes here.

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