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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5838e4593bf040b850df3887d15a1823a695932e310f952090c9f98910e93387
|
|
| MD5 |
4e2c401c4c1103a100a906c26dbccdf2
|
|
| BLAKE2b-256 |
0e5d6533d307779cdbcb85807832872e575a0269b0d742df11366c62ecdad41e
|
Provenance
The following attestation bundles were made for sqlclz-0.0.1.tar.gz:
Publisher:
python-publish.yml on ytsimon2004/sqlclz
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlclz-0.0.1.tar.gz -
Subject digest:
5838e4593bf040b850df3887d15a1823a695932e310f952090c9f98910e93387 - Sigstore transparency entry: 646519757
- Sigstore integration time:
-
Permalink:
ytsimon2004/sqlclz@9229f05d0a74aeca22d292af7ad562799262c266 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/ytsimon2004
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9229f05d0a74aeca22d292af7ad562799262c266 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aba2857eab3e40f2807f7b5dbbc3c54972d940b1c13bc475e65d96e0b873c2ee
|
|
| MD5 |
86f19e06f9891661e023355ebb836a35
|
|
| BLAKE2b-256 |
e6f5087df51d67484f033d01193720ba63072a14146778ff6bbb4abcbb9c711a
|
Provenance
The following attestation bundles were made for sqlclz-0.0.1-py3-none-any.whl:
Publisher:
python-publish.yml on ytsimon2004/sqlclz
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlclz-0.0.1-py3-none-any.whl -
Subject digest:
aba2857eab3e40f2807f7b5dbbc3c54972d940b1c13bc475e65d96e0b873c2ee - Sigstore transparency entry: 646519778
- Sigstore integration time:
-
Permalink:
ytsimon2004/sqlclz@9229f05d0a74aeca22d292af7ad562799262c266 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/ytsimon2004
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9229f05d0a74aeca22d292af7ad562799262c266 -
Trigger Event:
push
-
Statement type: