Skip to content

Commit b6bd012

Browse files
committed
add support for set with = and ; in them
1 parent 8f69c9c commit b6bd012

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

simple_ddl_parser/dialects/sql.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ def parse_complex_type(p_list: List[str]) -> str:
162162
def p_c_type(self, p: List) -> None:
163163
"""c_type : id
164164
| id id
165+
| id id id id
166+
| id id id
165167
| id DOT id
166168
| tid
167169
| ARRAY

simple_ddl_parser/output/dialects.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ def update_bigquery_output(table_data: Dict) -> Dict:
4949
if table_data.get("schema"):
5050
table_data["dataset"] = table_data["schema"]
5151
del table_data["schema"]
52-
else:
53-
if not table_data.get("schema_name"):
54-
table_data["dataset"] = None
5552
return table_data
5653

5754

simple_ddl_parser/parser.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,29 @@ def pre_process_data(self, data):
106106
@staticmethod
107107
def process_set(tables: List, set_line: str) -> None:
108108
set_line = set_line.split()
109-
name = set_line[-2]
110-
value = set_line[-1]
109+
if set_line[-2] == "=":
110+
name = set_line[1]
111+
else:
112+
name = set_line[-2]
113+
value = set_line[-1].replace(";", "")
111114
tables.append({"name": name, "value": value})
112115

113116
def parse_set_statement(
114-
self, tables: List, line: str, set_line: Optional[str]
117+
self, tables: List, line: str, set_line: Optional[str], set_was_in_line: bool
115118
) -> Optional[str]:
116-
set_was_in_line = False
117119
if re.match(r"SET", line):
118120
set_was_in_line = True
119121
if not set_line:
120122
set_line = line
121123
else:
122124
self.process_set(tables, set_line)
123125
set_line = line
124-
elif set_line and len(set_line.split()) == 3:
126+
elif (set_line and len(set_line.split()) == 3) or (
127+
set_line and set_was_in_line
128+
):
125129
self.process_set(tables, set_line)
126130
set_line = None
131+
set_was_in_line = False
127132
return set_line, set_was_in_line
128133

129134
def check_new_statement_start(self, line: str, statement: str) -> bool:
@@ -142,7 +147,11 @@ def parse_data(self): # noqa: C901 need to refactor this
142147
data = self.pre_process_data(self.data)
143148
lines = data.replace("\\t", "").split("\\n")
144149
skip_line_words = ["USE", "GO"]
150+
145151
set_line = None
152+
153+
set_was_in_line = False
154+
146155
for num, line in enumerate(lines):
147156
line, block_comments = self.pre_process_line(line, block_comments)
148157
line = line.strip().replace("\n", "").replace("\t", "")
@@ -151,19 +160,21 @@ def parse_data(self): # noqa: C901 need to refactor this
151160
if line.startswith(word):
152161
skip = True
153162
break
154-
set_line, set_was_in_line = self.parse_set_statement(tables, line, set_line)
163+
set_line, set_was_in_line = self.parse_set_statement(
164+
tables, line, set_line, set_was_in_line
165+
)
155166
if line or num == len(lines) - 1:
156167
# to avoid issues when comma or parath are glued to column name
157168
new_statement = self.check_new_statement_start(line, statement)
158169

159-
final_line = line.endswith(";")
170+
final_line = line.endswith(";") and not set_was_in_line
160171

161172
if not skip and not set_was_in_line and not new_statement:
162173
if statement is None:
163174
statement = line
164175
else:
165176
statement += f" {line}"
166-
177+
print(statement)
167178
if final_line or new_statement:
168179
# end of sql operation, remove ; from end of line
169180
statement = statement[:-1]

0 commit comments

Comments
 (0)