Skip to main content

SQL Query Builder

Project description

QuickQL

A fluent SQL query builder for Python that makes it easy to construct complex SQL queries programmatically.

Features

  • ๐Ÿš€ Fluent Interface: Build queries using method chaining
  • ๐Ÿ“‹ Comprehensive SQL Support: SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, WITH (CTEs)
  • ๐ŸŽฏ Type-Safe: Built with type hints for better IDE support
  • ๐Ÿงช Well Tested: Comprehensive test suite with >95% coverage
  • ๐Ÿ”ง Python 3.7+: Supports Python 3.7 and above
  • ๐Ÿ“ฆ Zero Dependencies: No external dependencies in production

Installation

# Using uv (recommended)
uv add quickql

# Using pip
pip install quickql

Quick Start

from quickql import Query

# Simple SELECT query
query = Query().SELECT("name", "email").FROM("users").WHERE("active = 1")
print(query)

Output:

SELECT
    name, email
FROM
    users
WHERE
    active = 1

Advanced Usage

Complex Queries with JOINs

query = (Query()
    .SELECT("u.name", "u.email", "p.title")
    .FROM("users u")
    .add("LEFT JOIN", "posts p ON u.id = p.user_id")
    .WHERE("u.active = 1")
    .WHERE("p.published = 1")
    .ORDER_BY("u.name")
    .LIMIT("10"))

Using Common Table Expressions (CTEs)

cte_query = """
SELECT user_id, COUNT(*) as post_count
FROM posts 
WHERE created_at > '2023-01-01'
GROUP BY user_id
"""

query = (Query()
    .WITH(("active_posters", cte_query))
    .SELECT("u.name", "ap.post_count")
    .FROM("users u")
    .add("JOIN", "active_posters ap ON u.id = ap.user_id")
    .ORDER_BY("ap.post_count DESC"))

Aggregate Queries

query = (Query()
    .SELECT("department", "COUNT(*) as employee_count", "AVG(salary) as avg_salary")
    .FROM("employees")
    .WHERE("active = 1")
    .GROUP_BY("department")
    .HAVING("COUNT(*) > 5")
    .ORDER_BY("avg_salary DESC"))

Development

Prerequisites

  • Python 3.7+
  • uv (recommended) or pip

Setup Development Environment

# Clone the repository
git clone https://github.com/yourusername/quickql.git
cd quickql

# Run the setup script (installs dependencies and runs tests)
python setup_dev.py

# Or manually with uv
uv venv
uv pip install -e ".[dev]"

Running Tests

# Run all tests
uv run pytest tests/ -v

# Run with coverage
uv run pytest tests/ --cov=src --cov-report=html

# Run specific test categories
python run_tests.py basic      # Basic functionality tests
python run_tests.py building   # Query building tests
python run_tests.py edge       # Edge case tests
python run_tests.py integration # Integration tests
python run_tests.py coverage   # Tests with coverage report

Code Quality

This project uses Ruff for linting and formatting:

# Check code quality
uv run ruff check .

# Format code
uv run ruff format .

# Check formatting without changing files
uv run ruff format --check .

Project Structure

quickql/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ quickql/
โ”‚       โ”œโ”€โ”€ __init__.py          # Package exports
โ”‚       โ””โ”€โ”€ builder.py           # Main Query builder
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ conftest.py             # Test fixtures
โ”‚   โ”œโ”€โ”€ test_query_basic.py     # Basic functionality tests
โ”‚   โ”œโ”€โ”€ test_query_building.py  # Query building tests
โ”‚   โ”œโ”€โ”€ test_edge_cases.py      # Edge cases and error handling
โ”‚   โ””โ”€โ”€ test_integration.py     # Integration tests
โ”œโ”€โ”€ .github/
โ”‚   โ””โ”€โ”€ workflows/
โ”‚       โ””โ”€โ”€ ci.yml              # GitHub Actions CI
โ”œโ”€โ”€ pyproject.toml              # Project configuration
โ”œโ”€โ”€ setup_dev.py               # Development setup script
โ”œโ”€โ”€ run_tests.py               # Test runner script
โ””โ”€โ”€ README.md

API Reference

Query Class

The main Query class provides a fluent interface for building SQL queries.

Methods

  • SELECT(*columns) - Add columns to SELECT clause
  • FROM(*tables) - Add tables to FROM clause
  • WHERE(condition) - Add WHERE conditions (chained with AND)
  • GROUP_BY(*columns) - Add GROUP BY columns
  • HAVING(condition) - Add HAVING conditions (chained with AND)
  • ORDER_BY(*columns) - Add ORDER BY columns
  • LIMIT(limit) - Add LIMIT clause
  • WITH((name, query)) - Add Common Table Expression
  • add(clause, *args) - Generic method to add any clause

JOIN Operations

Use the add() method for JOINs:

query.add("INNER JOIN", "table2 ON table1.id = table2.foreign_id")
query.add("LEFT JOIN", "table3 ON table1.id = table3.foreign_id")
query.add("RIGHT JOIN", "table4 ON table1.id = table4.foreign_id")
query.add("FULL JOIN", "table5 ON table1.id = table5.foreign_id")

Flags

Some clauses support flags:

query.add("SELECT DISTINCT", "column")  # SELECT DISTINCT
query.add("SELECT ALL", "column")       # SELECT ALL

Releasing

Creating Releases

This project uses automated PyPI publishing via GitHub Actions. To create a release:

# Verify the build works
python verify_build.py

# Create a release (this will trigger PyPI publishing)
python release.py 0.1.1

# Or create a release candidate (publishes to TestPyPI)
python release.py 0.2.0rc1

The release process:

  1. Updates version in pyproject.toml
  2. Creates a git tag
  3. Pushes the tag to GitHub
  4. GitHub Actions automatically builds and publishes to PyPI
  5. Creates a GitHub release with signed artifacts

For detailed setup instructions, see PUBLISHING.md.

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests (python run_tests.py all)
  5. Run code quality checks (uv run ruff check . && uv run ruff format --check .)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

v0.1.0 (Initial Release)

  • Fluent query builder interface
  • Support for all major SQL clauses
  • JOIN operations support
  • Common Table Expressions (WITH)
  • Comprehensive test suite
  • Python 3.7+ support

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

quickql-0.1.0.tar.gz (48.0 kB view details)

Uploaded Source

Built Distribution

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

quickql-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for quickql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1dd175f805ae260b26187420a4160d2aa511ea99be1339d973667c41c248f6b0
MD5 06bd8266e7a12c4b2699950117ded54e
BLAKE2b-256 31415c70a587a0de35bc26b39e8d7355cc8718fcc6afb7e32d0d2f2c97b4c519

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickql-0.1.0.tar.gz:

Publisher: ci.yml on arunksoman/QuickQL

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

File details

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

File metadata

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

File hashes

Hashes for quickql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cbe99d7e413dee926528f8cf576f3a088aed110acac9b2e3667dfcefb336d488
MD5 a16b52d2fd0ac70bca8f37373e26f964
BLAKE2b-256 51cec46b505013acf9913d628d5ea0fa7351580df2cca72966393a1ffaf78744

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickql-0.1.0-py3-none-any.whl:

Publisher: ci.yml on arunksoman/QuickQL

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