Parse SQL string and return Node representing parse tree.
(sql, verbose=False)
| 344 | |
| 345 | |
| 346 | def parse_sql(sql, verbose=False): |
| 347 | """Parse SQL string and return Node representing parse tree.""" |
| 348 | tokens = sql_tokenizer.tokenize_sql(sql) |
| 349 | |
| 350 | rules = [] |
| 351 | for rule_string in RULES: |
| 352 | rules.append(Rule.from_string(rule_string)) |
| 353 | |
| 354 | # Add rule `T -> token` for every non-keyword token in input. |
| 355 | for token in set(tokens): |
| 356 | if token not in KEYWORDS: |
| 357 | rules.append(Rule("T", token)) |
| 358 | |
| 359 | # Re-split on spaces. |
| 360 | tokens = " ".join(tokens).split(" ") |
| 361 | if verbose: |
| 362 | print("Parsing SQL:") |
| 363 | for idx, token in enumerate(tokens): |
| 364 | print("%s - %s" % (idx, token)) |
| 365 | |
| 366 | nodes = _run_parser(tokens, rules, verbose=verbose) |
| 367 | if not nodes: |
| 368 | raise ValueError("No parse for SQL: %s" % sql) |
| 369 | elif len(nodes) > 1: |
| 370 | raise ValueError("Multiple parses for SQL: %s (%s)" % (sql, nodes)) |
| 371 | return nodes[0] |
nothing calls this directly
no test coverage detected
searching dependent graphs…