Declarative SQLite table builder and schema manager with integration support for recordsQL.
Project description
tablesQLite
A declarative SQLite table builder and schema manager for Python. Define your database tables with rich column constraints, generate SQL DDL statements, and parse existing schemas back into Python objects.
Features
- Declarative Table Definitions: Define tables using
SQLTableInfoandSQLColumnInfoclasses with full constraint support - Rich Column Constraints: Support for
NOT NULL,DEFAULT,CHECK,UNIQUE,PRIMARY KEY, andFOREIGN KEYconstraints - SQL Generation: Generate
CREATE TABLE,ALTER TABLE, andDROP TABLESQL statements - Schema Parsing: Parse existing SQL schema strings back into Python objects
- Type Safety: Full type hints throughout the codebase
- Integration Ready: Seamlessly integrates with recordsQL for DML operations
- Utility Functions: Helper functions for type conversion, foreign key validation, and schema migrations
Installation
pip install tablesqlite
Quick Start
Define a Table
from tablesqlite import SQLColumnInfo, SQLTableInfo
from expressQL import parse_condition
# Define columns with constraints
columns = [
SQLColumnInfo("id", "INTEGER", primary_key=True),
SQLColumnInfo("name", "TEXT", not_null=True),
SQLColumnInfo("email", "TEXT", unique=True),
SQLColumnInfo(
"age",
"INTEGER",
not_null=True,
check=parse_condition("age >= 18")
),
SQLColumnInfo("balance", "REAL", default_value=0.0),
SQLColumnInfo("created_at", "DATETIME", default_value="CURRENT_TIMESTAMP"),
]
# Create table definition
users_table = SQLTableInfo(name="users", columns=columns)
# Generate CREATE TABLE SQL
query, params = users_table.create_query()
print(query)
Output:
CREATE TABLE "users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"email" TEXT UNIQUE,
"age" INTEGER NOT NULL CHECK (age >= 18),
"balance" REAL DEFAULT 0.0,
"created_at" DATETIME DEFAULT CURRENT_TIMESTAMP
);
Parse Existing Schema
from tablesqlite import SQLTableInfo
schema = """
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER CHECK (age >= 18)
);
"""
table_info = SQLTableInfo.from_sql_schema(schema)
print(f"Table: {table_info.name}")
for col in table_info.columns:
print(f" - {col.name}: {col.data_type}")
Foreign Key Support
from tablesqlite import SQLColumnInfo, SQLTableInfo
# Single-column foreign key (inline)
owner_id_column = SQLColumnInfo(
"owner_id",
"INTEGER",
not_null=True,
foreign_key={"table": "owners", "column": "id"}
)
# Multi-column foreign key (table-level)
orders_table = SQLTableInfo(
name="orders",
columns=[
SQLColumnInfo("id", "INTEGER", primary_key=True),
SQLColumnInfo("customer_id", "INTEGER", not_null=True),
SQLColumnInfo("store_id", "INTEGER", not_null=True),
],
foreign_keys=[
{
"columns": ["customer_id", "store_id"],
"ref_table": "customer_stores",
"ref_columns": ["customer_id", "store_id"]
}
]
)
Column Operations
from tablesqlite import SQLColumnInfo, SQLTableInfo
table = SQLTableInfo(name="users", columns=[
SQLColumnInfo("id", "INTEGER", primary_key=True),
SQLColumnInfo("name", "TEXT"),
])
# Add a new column
new_column = SQLColumnInfo("email", "TEXT", unique=True)
query, params = table.add_column_query(new_column)
# ALTER TABLE "users" ADD COLUMN "email" TEXT UNIQUE
# Drop a column
query, params = table.drop_column_query("name")
# ALTER TABLE "users" DROP COLUMN "name"
# Rename a column
query, params = table.rename_column_query("email", "user_email")
# ALTER TABLE "users" RENAME COLUMN "email" TO "user_email"
Utility Functions
tablesQLite provides helpful utility functions for common tasks:
from enum import IntEnum
from tablesqlite import (
convert_enum_value,
validate_foreign_keys,
generate_migration,
SQLTableInfo,
SQLColumnInfo
)
# Type conversion helper for IntEnum
class Status(IntEnum):
PENDING = 1
ACTIVE = 2
COMPLETED = 3
status = convert_enum_value("2", Status) # Returns Status.ACTIVE
status = convert_enum_value(1, Status) # Returns Status.PENDING
# Validate foreign key relationships
users = SQLTableInfo("users", [SQLColumnInfo("id", "INTEGER", primary_key=True)])
posts = SQLTableInfo("posts", [
SQLColumnInfo("id", "INTEGER", primary_key=True),
SQLColumnInfo("user_id", "INTEGER", foreign_key={"table": "users", "column": "id"})
])
tables = {"users": users, "posts": posts}
errors = validate_foreign_keys(posts, tables)
if errors:
print("Foreign key errors:", errors)
# Generate migration statements
old_table = SQLTableInfo("users", [
SQLColumnInfo("id", "INTEGER", primary_key=True),
SQLColumnInfo("name", "TEXT")
])
new_table = SQLTableInfo("users", [
SQLColumnInfo("id", "INTEGER", primary_key=True),
SQLColumnInfo("name", "TEXT"),
SQLColumnInfo("email", "TEXT")
])
migrations = generate_migration(old_table, new_table)
for sql, params in migrations:
print(sql)
# Output: ALTER TABLE "users" ADD COLUMN "email" TEXT
API Reference
SQLColumnInfo
Represents a column definition with full constraint support.
Constructor Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
required | Column name |
data_type |
str |
required | SQL data type (e.g., "INTEGER", "TEXT") |
not_null |
bool |
False |
NOT NULL constraint |
default_value |
str | int | float | Unknown |
unknown |
Default value |
primary_key |
bool |
False |
PRIMARY KEY constraint |
cid |
int | Unknown |
unknown |
Column ID |
unique |
bool |
False |
UNIQUE constraint |
foreign_key |
dict | None |
None |
Foreign key definition |
check |
SQLCondition | None |
None |
CHECK constraint |
SQLTableInfo
Represents a table definition with columns and constraints.
Constructor Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
required | Table name |
columns |
Iterable[SQLColumnInfo] |
unknown |
Table columns |
database_path |
str | Unknown |
unknown |
Database file path |
foreign_keys |
list[dict] |
None |
Table-level foreign keys |
Key Methods:
create_query()- Generate CREATE TABLE SQLdrop_query(if_exists=False)- Generate DROP TABLE SQLrename_query(new_name)- Generate RENAME TABLE SQLadd_column_query(column)- Generate ADD COLUMN SQLdrop_column_query(column_name)- Generate DROP COLUMN SQLrename_column_query(old_name, new_name)- Generate RENAME COLUMN SQLfrom_sql_schema(schema)- Parse SQL schema string into SQLTableInfo
Integration with recordsQL
tablesQLite focuses on DDL (Data Definition Language) operations. For DML (Data Manipulation Language) operations like INSERT, UPDATE, SELECT, and DELETE, use recordsQL.
See INTEGRATION_EXAMPLE.md for a complete example of using tablesQLite with recordsQL.
from recordsQL.integrations.tablesqlite import add_query_methods
# Add insert/update/select/delete methods to SQLTableInfo
add_query_methods()
# Now you can use:
# table_info.insert_query(data)
# table_info.select_query(columns)
# table_info.update_query(data, condition)
# table_info.delete_query(condition)
Dependencies
- expressQL - SQL expression builder
- sortedcontainers - Sorted container types
Documentation
Full documentation is available online and can be built locally.
Online Documentation
Visit the full documentation for detailed guides and API reference.
Building Documentation Locally
# Install documentation dependencies
pip install tablesqlite[docs]
# Build HTML documentation
cd docs
make html
# Open docs/_build/html/index.html in your browser
The documentation includes:
- Installation guide
- Quick start tutorial
- Comprehensive usage guide
- Complete API reference
- Integration examples with recordsQL
Development
# Install development dependencies
pip install tablesqlite[dev]
# Run linting
ruff check .
# Run type checking
mypy tablesqlite
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Changelog
See CHANGELOG.md for a list of changes.
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 tablesqlite-0.1.9.tar.gz.
File metadata
- Download URL: tablesqlite-0.1.9.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f248f7c87f41eb4efefba66dfadeefb05bd3cc84ae080ce14384e95395af4168
|
|
| MD5 |
af9a9abbb6541a1052ed506de4e2a1cc
|
|
| BLAKE2b-256 |
e5a499039433d66c802f73383e4df280cb7c622148ebb57323939fe2b130cab3
|
Provenance
The following attestation bundles were made for tablesqlite-0.1.9.tar.gz:
Publisher:
python-publish.yml on Grayjou/tablesqlite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tablesqlite-0.1.9.tar.gz -
Subject digest:
f248f7c87f41eb4efefba66dfadeefb05bd3cc84ae080ce14384e95395af4168 - Sigstore transparency entry: 832778849
- Sigstore integration time:
-
Permalink:
Grayjou/tablesqlite@12fe597acbcfffdaeac2b61f079d3ccf6bc1ee6f -
Branch / Tag:
refs/tags/v0.1.9 - Owner: https://github.com/Grayjou
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@12fe597acbcfffdaeac2b61f079d3ccf6bc1ee6f -
Trigger Event:
release
-
Statement type:
File details
Details for the file tablesqlite-0.1.9-py3-none-any.whl.
File metadata
- Download URL: tablesqlite-0.1.9-py3-none-any.whl
- Upload date:
- Size: 33.9 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 |
76c208ee5921ef87d6b491d5b1814d26fc0a9d465a41b53f2dbd22d76540496f
|
|
| MD5 |
474c9892847b7832a09e80109b71b206
|
|
| BLAKE2b-256 |
4bc223c622ea9cffe57352e542a03d5f280f4f445d9f949860b0c3dd7311d5ba
|
Provenance
The following attestation bundles were made for tablesqlite-0.1.9-py3-none-any.whl:
Publisher:
python-publish.yml on Grayjou/tablesqlite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tablesqlite-0.1.9-py3-none-any.whl -
Subject digest:
76c208ee5921ef87d6b491d5b1814d26fc0a9d465a41b53f2dbd22d76540496f - Sigstore transparency entry: 832778850
- Sigstore integration time:
-
Permalink:
Grayjou/tablesqlite@12fe597acbcfffdaeac2b61f079d3ccf6bc1ee6f -
Branch / Tag:
refs/tags/v0.1.9 - Owner: https://github.com/Grayjou
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@12fe597acbcfffdaeac2b61f079d3ccf6bc1ee6f -
Trigger Event:
release
-
Statement type: