Skip to main content

Library to generate DDL for different dialects

Project description

Simple DDL Generator

badge1 badge2 badge3 workflow

What is it?

Simple DDL Generator generate SQL DDL from 3 different inputs. Idea of the generator same as for parser to support as much as possible DDLs in future.

Simple DDL Generator generate SQL DDL from 3 input formats - 1st from output Simple DDL Parser (https://github.com/xnuinside/simple-ddl-parser), 2nd from py-models-parser - https://github.com/xnuinside/py-models-parser. Or you can directly pass TableMeta classes (https://github.com/xnuinside/table-meta) to generator

Generate DDL from Django, SQLAlchemy, Dataclasses, Pydantic models and other

Generator can generate DDL from all models that supported & parsed by https://github.com/xnuinside/py-models-parser.

If you need DDL generation from another Python Model types - open issue request to add support for this models in parser.

How to use

As usually - more samples in tests/

pip install simple-ddl-generator

Generate / Modify using existed DDL with Simple-DDL-Parser

Sample how you can modify your DDL using Simple DDL Parser & Simple DDL Parser

from simple_ddl_generator import DDLGenerator
from simple_ddl_parser import DDLParser

# take initial DDL
ddl = """CREATE EXTERNAL TABLE IF NOT EXISTS database.table_name
    (
        day_long_nm     string,
        calendar_dt     date,
        source_batch_id string,
        field_qty       decimal(10, 0),
        field_bool      boolean,
        field_float     float,
        create_tmst     timestamp,
        field_double    double,
        field_long      bigint
    ) PARTITIONED BY (batch_id int);"""
# get result from parser
data = DDLParser(ddl).run(group_by_type=True, output_mode="bigquery")

# rename, for example, table name

data["tables"][0]["table_name"] = "new_table_name"
g = DDLGenerator(data)
g.generate()
print(g.result)

# and result will be:

"""
CREATE EXTERNAL TABLE "database.new_table_name" (
day_long_nm string,
calendar_dt date,
source_batch_id string,
field_qty decimal(10, 0),
field_bool boolean,
field_float float,
create_tmst timestamp,
field_double double,
field_long bigint)
PARTITIONED BY (batch_id int);
"""

Generate DDL from various Python Models with py-models-parser

    from simple_ddl_generator import DDLGenerator
    from py_models_parser import parse

    # you can also read them from file
    model_from = """
        class Material(BaseModel):

            id: int
            title: str
            description: Optional[str]
            link: str = 'http://'
            type: Optional[MaterialType]
            additional_properties: Optional[Json]
            created_at: Optional[datetime.datetime] = datetime.datetime.now()
            updated_at: Optional[datetime.datetime]
        """
    # get data with parser
    result = parse(model_from)

    # pass data to DDL Generator
    g = DDLGenerator(result)
    g.generate()
    print(g.result)

    # resul will be

    """CREATE TABLE "Material" (
id INTEGER,
title VARCHAR,
description VARCHAR,
link VARCHAR DEFAULT 'http://',
type MaterialType,
additional_properties JSON,
created_at DATETIME DEFAULT now(),
updated_at DATETIME);
"""

Changelog

v0.2.0

  1. Updated parser version in tests.

  2. Added support for EXTERNAL & IF NOT EXISTS statetements.

  3. Added support for using py-models-parser output as input and added sample in README.md:

DDL Generation from Pydantic, SQLAlchemy and other python models.

v0.1.0

Base Generator Functionality with several test cases.

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

simple-ddl-generator-0.2.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

simple_ddl_generator-0.2.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file simple-ddl-generator-0.2.0.tar.gz.

File metadata

  • Download URL: simple-ddl-generator-0.2.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.12 Darwin/19.6.0

File hashes

Hashes for simple-ddl-generator-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a8d22a69d1a28b4f047dcd886f5ec70f952f400692bc38bb34fcfff34e603e4f
MD5 04fc2b7a9e325ed52279f1d3f8a0852f
BLAKE2b-256 6ed5bc87c5a7aca0f1c0f7c13a5246dd1654ea8fbdc7929b6552835384b3c9fb

See more details on using hashes here.

File details

Details for the file simple_ddl_generator-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_ddl_generator-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0511fcdfcda3feade86c843c7138a94b84c2743ee7a98dd985da0884b5844392
MD5 b19261b664a51b6c524770a958cca41a
BLAKE2b-256 ee374f2da7cef1d93514597c24fcb77732a4cd77de445d0c421a2c7c903028e4

See more details on using hashes here.

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