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-0.0.1.tar.gz (40.8 kB view details)

Uploaded Source

Built Distribution

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

sqlclz-0.0.1-py3-none-any.whl (45.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sqlclz-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5838e4593bf040b850df3887d15a1823a695932e310f952090c9f98910e93387
MD5 4e2c401c4c1103a100a906c26dbccdf2
BLAKE2b-256 0e5d6533d307779cdbcb85807832872e575a0269b0d742df11366c62ecdad41e

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlclz-0.0.1.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-0.0.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for sqlclz-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aba2857eab3e40f2807f7b5dbbc3c54972d940b1c13bc475e65d96e0b873c2ee
MD5 86f19e06f9891661e023355ebb836a35
BLAKE2b-256 e6f5087df51d67484f033d01193720ba63072a14146778ff6bbb4abcbb9c711a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlclz-0.0.1-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