shinichi-takii / ddlparse

DDL parase and Convert to BigQuery JSON schema and DDL statements
https://pypi.org/project/ddlparse/
BSD 3-Clause "New" or "Revised" License
87 stars 29 forks source link

Add support constraint name with quotes #15

Closed ck-fm0211 closed 6 years ago

ck-fm0211 commented 6 years ago

Problem

DDLの CONSTRAINT 句で指定する制約名が、ダブルクォート( " )で囲われているとエラーになる。

patern A: OK

code

$ cat test_none_quote.py
from ddlparse import DdlParse

sample_ddl = """
CREATE TABLE "SAMPLE_B"
(    "COL1" CHAR(10),
     "COL2" VARCHAR2(200),
      CONSTRAINT SAMPLE_B_PK PRIMARY KEY ("COL1")
)
"""

table = DdlParse().parse(sample_ddl)
parser = DdlParse(sample_ddl)
table = parser.parse()

print(table.to_bigquery_fields())

result

$ python test_none_quote.py
[{"name": "COL1", "type": "STRING", "mode": "REQUIRED"},{"name": "COL2", "type": "STRING", "mode": "NULLABLE"}]

patern B: NG

code

$ cat test_quote.py
from ddlparse import DdlParse

sample_ddl = """
CREATE TABLE "SAMPLE_A"
(    "COL1" CHAR(10),
     "COL2" VARCHAR2(200),
      CONSTRAINT "SAMPLE_A_PK" PRIMARY KEY ("COL1")
)
"""

table = DdlParse().parse(sample_ddl)
parser = DdlParse(sample_ddl)
table = parser.parse()

print(table.to_bigquery_fields())

result

$ python test_quote.py
Traceback (most recent call last):
  File "test_quote.py", line 15, in <module>
    print(table.to_bigquery_fields())
  File "/Users/anonymous/anaconda3/lib/python3.6/site-packages/ddlparse/ddlparse.py", line 305, in to_bigquery_fields
    return self._columns.to_bigquery_fields(name_case)
  File "/Users/anonymous/anaconda3/lib/python3.6/site-packages/ddlparse/ddlparse.py", line 253, in to_bigquery_fields
    bq_fields.append(col.to_bigquery_field(name_case))
  File "/Users/anonymous/anaconda3/lib/python3.6/site-packages/ddlparse/ddlparse.py", line 217, in to_bigquery_field
    return '{{"name": "{}", "type": "{}", "mode": "{}"}}'.format(self._get_name(name_case), self.bigquery_data_type, self.bigquery_mode)
  File "/Users/anonymous/anaconda3/lib/python3.6/site-packages/ddlparse/ddlparse.py", line 206, in bigquery_data_type
    raise ValueError("Unknown data type : '{}'".format(self._data_type))
ValueError: Unknown data type : 'SAMPLE_A_PK'

Environment

$ pip freeze | grep ddlparse
ddlparse==1.1.2