Code generator for Python classes to flattened relational SQL Table types
Project description
type2sql
Code generator for Python classes to flattened relational SQL Table types
For generating SQL code, attributes are flattened, that is nested classes, and lists are generated as single SQL columns. A reverse process happens when generating Python code, that is, Python object can be instantiated from flattened SQL columns.
Annotation
Instances of type2sql.Meta class can be used to annotate Python types. Meta class supports the following, all optional, attributes:
- name: SQL column name, when omitted, Python attribute name is used
- size: the size of the SQL column. It can be either a pair of numbers specified as a tuple or a single number
- occurs: relevant when converting python attributes that are defined as
list. A list is flattened to create as many instances as specified by this parameter. Note that the name must be specified and must include{n}to give unique names to flattened column names - skip: if specified, this attribute is skipped when any SQL statements are generated. Other specifications, if specified, are ignored
- default: default value for the generated SQL column.
Supported Python types
Supported Python types:
| Python Type | SQL Type | Notes |
|---|---|---|
str |
VARCHAR |
|
int |
INTEGER |
|
float |
DECIMAL |
|
Decimal |
DECIMAL |
|
bool |
BOOLEAN |
|
datetime.date |
DATE |
|
datetime.time |
TIME |
|
datetime.datetime |
TIMESTAMP |
|
T |
any Python class with all of its attributes having one of the supported type | |
list[T] |
where T is one of the supported type | |
T | None |
Same as T |
generated SQL Column will allow NULL values |
Usage
Install type2sql as a dependency in other Python applications or modules that contain target classes for code generation. Use the following functions to generate the required code.
gen_select(<class>[, <table>])generatesSELECTSQL statementgen_ddl(<class>[, <table>])generatesCREATE TABLESQL statementgen_load(<class>[, <fn>])generates a Python function to instantiate Python type that accepts flattened values as parameters
Example
@dataclass
class Address:
street1: Annotated[str, Meta(name="ST1_NM", size=60)]
street2: Annotated[str | None, Meta(name="ST2_NM", size=60)]
city: Annotated[str, Meta(name="CITY_NM", size=30)]
zip: Annotated[str, Meta(name="ZIP_CD", size=9)]
@dataclass
class Customer:
name: Annotated[str, Meta(name="CUST_NM", size=60)]
addr: Annotated[list[Address], Meta(name="ADDR{n}_{sub}", occurs=2)]
print(gen_ddl(Customer, "FIN.CUSTOMER"))
The above script will print:
CREATE TABLE FIN.CUSTOMER (
CUST_NM VARCHAR(60) NOT NULL,
ADDR1_ST1_NM VARCHAR(60),
ADDR1_ST2_NM VARCHAR(60),
ADDR1_CITY_NM VARCHAR(30),
ADDR1_ZIP_CD VARCHAR(9),
ADDR2_ST1_NM VARCHAR(60),
ADDR2_ST2_NM VARCHAR(60),
ADDR2_CITY_NM VARCHAR(30),
ADDR2_ZIP_CD VARCHAR(9)
)
More examples can be found in the test suite under tests/ folder.
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 type2sql-0.1.0.tar.gz.
File metadata
- Download URL: type2sql-0.1.0.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b7073de0030a64f1749fa68c5d1f7103991a8bf402a4d0668bc9b5c9eddd8f7
|
|
| MD5 |
f9ef8ab896a808052a0651ae7c32a089
|
|
| BLAKE2b-256 |
e13d92ee030510de310da237534b5471214a111067dc9df06da0b816769539af
|
File details
Details for the file type2sql-0.1.0-py3-none-any.whl.
File metadata
- Download URL: type2sql-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2813268a5fb808a9d3aee94f94c4e3c1cc58c54ee84857cde5aed83a9a55890
|
|
| MD5 |
051c8f551a5e23f8c5e24fe6bd0cdb05
|
|
| BLAKE2b-256 |
fe68fbf7b244e9da27f323d6a71cb4a86ebd70631b14662421fdea18c0406aa1
|