Skip to main content

Python functions to build a SQL (sqlite3) statement

Project description

sqlclz

A type-safe Python SQL query builder that uses NamedTuple classes to define database schemas and generate SQL statements.

Installation

pip install sqlclz

Quick Start

Define Your Schema

from typing import NamedTuple, Annotated
from sqlclz import named_tuple_table_class, PRIMARY


@named_tuple_table_class
class Person(NamedTuple):
    name: Annotated[str, PRIMARY()]
    age: int
    email: str

Create Tables and Insert Data

import sqlclz
from sqlclz import Connection

# Create a connection (in-memory SQLite database)
with Connection() as conn:
    # Create table
    conn.execute(sqlclz.create_table(Person))

    # Insert data
    conn.execute(
        sqlclz.insert_into(Person),
        [("Alice", 30, "alice@example.com"),
         ("Bob", 25, "bob@example.com")]
    )

    # Query data
    result = conn.execute(
        sqlclz.select_from(Person).where(Person.age > 25)
    ).fetchall()

    print(result)
    # Output: [('Alice', 30, 'alice@example.com')]

Type-Safe Queries

# SELECT with specific fields
query = sqlclz.select_from(Person.name, Person.email)

# WHERE conditions with type safety
query = sqlclz.select_from(Person).where(
    Person.age >= 18,
    Person.age <= 65
)

# ORDER BY
query = sqlclz.select_from(Person).order_by(sqlclz.desc(Person.age))

# LIMIT and OFFSET
query = sqlclz.select_from(Person).limit(10, offset=5)

Advanced Features

Foreign Keys

from sqlclz import foreign


@named_tuple_table_class
class Department(NamedTuple):
    id: Annotated[int, PRIMARY(auto_increment=True)]
    name: str


@named_tuple_table_class
class Employee(NamedTuple):
    id: Annotated[int, PRIMARY(auto_increment=True)]
    name: str
    dept_id: int

    @foreign(Department)
    def _department(self):
        return self.dept_id

Joins

# Join tables
query = sqlclz.select_from(
    Employee.name,
    Department.name
).join(
    Employee.dept_id == Department.id,
    by='inner'
)

Aggregations

# Count, sum, avg, etc.
query = sqlclz.select_from(
    sqlclz.count(Person.name),
    sqlclz.avg(Person.age)
).group_by(Person.email)

Features

  • Type-Safe: Leverage Python's type system to catch errors at development time
  • Pythonic: Define schemas using familiar NamedTuple syntax
  • SQL Generation: Automatically generate correct SQL statements
  • Foreign Keys: Support for foreign key relationships
  • Comprehensive: Supports SELECT, INSERT, UPDATE, DELETE, JOIN, and more
  • Window Functions: Support for advanced SQL window functions
  • CTE Support: Common Table Expressions (WITH clause)

Running Tests

# Install test dependencies
cd test/unit
curl -L -o chinook.zip https://www.sqlitetutorial.net/wp-content/uploads/2018/03/chinook.zip
unzip chinook.zip

# Run tests
python -m unittest discover -s test/unit -p "test*.py"

Documentation

For more examples and detailed documentation, see the test directory.

License

BSD 3-Clause

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

sqlclz-1.0.0.tar.gz (42.3 kB view details)

Uploaded Source

Built Distribution

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

sqlclz-1.0.0-py3-none-any.whl (47.5 kB view details)

Uploaded Python 3

File details

Details for the file sqlclz-1.0.0.tar.gz.

File metadata

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

File hashes

Hashes for sqlclz-1.0.0.tar.gz
Algorithm Hash digest
SHA256 54c4ed070bab704d498c858ea5a60e26cfbc58b103ee6ea1bb97df4111062704
MD5 c4efe802a0922a53a947354225c955a0
BLAKE2b-256 bf03cfb73e2f1684b0f4866503e9a57ef1de8bedce37ceb50daf772c2f4d60ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlclz-1.0.0.tar.gz:

Publisher: python-publish.yml on ytsimon2004/sqlclz

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

File details

Details for the file sqlclz-1.0.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for sqlclz-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 447c5904a74bd791fb89a7cf06ae57cdb6caab8029e7fa868a0424f52b87940d
MD5 3e3eb31402a91cf80512742ef61f07c1
BLAKE2b-256 b1a344f1273245db496b670de6477bb98640d7d3548489ca6cc9cd9709361a50

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlclz-1.0.0-py3-none-any.whl:

Publisher: python-publish.yml on ytsimon2004/sqlclz

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