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.
Metadata
Release files for type2sql 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| type2sql-0.1.0.tar.gz | 9.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| type2sql-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.5 kB
Release files / type2sql-0.1.0.tar.gz
| Download URL | type2sql-0.1.0.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6b7073de0030a64f1749fa68c5d1f7103991a8bf402a4d0668bc9b5c9eddd8f7
|
|
BLAKE2b-256 checksum How to use checksums |
e13d92ee030510de310da237534b5471214a111067dc9df06da0b816769539af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.4
|
Release files / type2sql-0.1.0-py3-none-any.whl
| Download URL | type2sql-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c2813268a5fb808a9d3aee94f94c4e3c1cc58c54ee84857cde5aed83a9a55890
|
|
BLAKE2b-256 checksum How to use checksums |
fe68fbf7b244e9da27f323d6a71cb4a86ebd70631b14662421fdea18c0406aa1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.4
|
Security for type2sql 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.