Package: ddlparse
DDL ParseDDL parase and Convert to BigQuery JSON schema and DDL statements module, available in Python.FeaturesDDL parse and get table schema information.Currently, only theCREATE TABLEstatement is supported.Convert toBigQuery JSON schemaandBigQuery DDL statements.Supported databases are MySQL/MariaDB, PostgreSQL, Oracle, Redshift.RequirementPython >= 3.5pyparsingInstallationInstallpip install:$pipinstallddlparsecommand install:$pythonsetup.pyinstallUpdatepip update:$pipinstallddlparse--upgradeUsageExampleimportjsonfromddlparseimportDdlParsesample_ddl="""CREATE TABLE My_Schema.Sample_Table (Id integer PRIMARY KEY COMMENT 'User ID',Name varchar(100) NOT NULL COMMENT 'User name',Total bigint NOT NULL,Avg decimal(5,1) NOT NULL,Point int(10) unsigned,Zerofill_Id integer unsigned zerofill NOT NULL,Created_At date, -- Oracle 'DATE' -> BigQuery 'DATETIME'UNIQUE (NAME));"""# parse pattern (1-1)table=DdlParse().parse(sample_ddl)# parse pattern (1-2) : Specify source databasetable=DdlParse().parse(ddl=sample_ddl,source_database=DdlParse.DATABASE.oracle)# parse pattern (2-1)parser=DdlParse(sample_ddl)table=parser.parse()print("* BigQuery Fields * : normal")print(table.to_bigquery_fields())# parse pattern (2-2) : Specify source databaseparser=DdlParse(ddl=sample_ddl,source_database=DdlParse.DATABASE.oracle)table=parser.parse()# parse pattern (3-1)parser=DdlParse()parser.ddl=sample_ddltable=parser.parse()# parse pattern (3-2) : Specify source databaseparser=DdlParse()parser.source_database=DdlParse.DATABASE.oracleparser.ddl=sample_ddltable=parser.parse()print("* BigQuery Fields * : Oracle")print(table.to_bigquery_fields())print("* TABLE *")print("schema ={}: name ={}: is_temp ={}".format(table.schema,table.name,table.is_temp))print("* BigQuery Fields *")print(table.to_bigquery_fields())print("* BigQuery Fields - column name to lower case / upper case *")print(table.to_bigquery_fields(DdlParse.NAME_CASE.lower))print(table.to_bigquery_fields(DdlParse.NAME_CASE.upper))print("* COLUMN *")forcolintable.columns.values():col_info={}col_info["name"]=col.namecol_info["data_type"]=col.data_typecol_info["length"]=col.lengthcol_info["precision(=length)"]=col.precisioncol_info["scale"]=col.scalecol_info["is_unsigned"]=col.is_unsignedcol_info["is_zerofill"]=col.is_zerofillcol_info["constraint"]=col.constraintcol_info["not_null"]=col.not_nullcol_info["PK"]=col.primary_keycol_info["unique"]=col.uniquecol_info["auto_increment"]=col.auto_incrementcol_info["distkey"]=col.distkeycol_info["sortkey"]=col.sortkeycol_info["encode"]=col.encodecol_info["default"]=col.defaultcol_info["character_set"]=col.character_setcol_info["bq_legacy_data_type"]=col.bigquery_legacy_data_typecol_info["bq_standard_data_type"]=col.bigquery_standard_data_typecol_info["comment"]=col.commentcol_info["description(=comment)"]=col.descriptioncol_info["bigquery_field"]=json.loads(col.to_bigquery_field())print(json.dumps(col_info,indent=2,ensure_ascii=False))print("* DDL (CREATE TABLE) statements *")print(table.to_bigquery_ddl())print("* DDL (CREATE TABLE) statements - dataset name, table name and column name to lower case / upper case *")print(table.to_bigquery_ddl(DdlParse.NAME_CASE.lower))print(table.to_bigquery_ddl(DdlParse.NAME_CASE.upper))print("* Get Column object (case insensitive) *")print(table.columns["total"])print(table.columns["total"].data_type)LicenseBSD 3-Clause LicenseAuthorShinichi Takiishinichi.takii@shaketh.comLinksRepository :https://github.com/shinichi-takii/ddlparsePyPI Package :https://pypi.org/project/ddlparse/Special Thankspyparsing :https://github.com/pyparsing/pyparsing