ddlparse icon indicating copy to clipboard operation
ddlparse copied to clipboard

DDL containing more than one CREATE statement

Open SarathVakacharla opened this issue 6 years ago • 3 comments

If the DDL string contains more than one create statements, only the first one is getting parsed and the parse method returns just one table all the time. Need support for ddl containing potentially multiple create statements and parsing should return list of tables

SarathVakacharla avatar Jul 11 '18 17:07 SarathVakacharla

@SarathVakacharla Thank you for the report. I just noticed the issue ticket. I would like to consider a solution.

shinichi-takii avatar Sep 21 '18 03:09 shinichi-takii

@shinichi-takii - would you accept a PR with this capability? I'd like to simply iterate with pyparse @ https://github.com/shinichi-takii/ddlparse/blob/7328656ee807d14960999a98ace8cd76f0fe3ff8/ddlparse/ddlparse.py#L557

clowtown avatar May 22 '19 17:05 clowtown

Using it with sqlparse package can achieve what I want.

import re
import sqlparse
from ddlparse import DdlParse

def main():
    with open("/path/to/ddl.sql", "r") as ddl_file:
        sql = ddl_file.read()
        ddls = sqlparse.split(sql)
        tmp_tables_regex = re.compile("^CREATE TABLE.*.*\(")
        for ddl in ddls:
            if(tmp_tables_regex.match(ddl)):
                parser = DdlParse(ddl=ddl, source_database=DdlParse.DATABASE.postgresql)
                table = parser.parse()
                print("table_name: %s"%table.name)
                for column in table.columns.values():
                    print("     column_name: %s, data_type: %s"%(column.name,column.bigquery_standard_data_type))

if __name__ == "__main__":
    main()

Valdanitooooo avatar Aug 20 '20 14:08 Valdanitooooo