ddlparse
ddlparse copied to clipboard
DDL containing more than one CREATE statement
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 Thank you for the report. I just noticed the issue ticket. I would like to consider a solution.
@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
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()