Optional(GROUP_BY + delimitedList(Group(selectColumn))("groupby")) + Optional(HAVING + expr("having")) + Optional(LIMIT + expr("limit")) + Optional(OFFSET + expr("offset")))) ordered_sql << Group( Group( Group(unordered_sql + ZeroOrMore((UNION_ALL | UNION) + unordered_sql)) ("union"))("from") + Optional(ORDER_BY + delimitedList(Group(sortColumn))("orderby")) + Optional(LIMIT + expr("limit")) + Optional(OFFSET + expr("offset"))).addParseAction(to_union_call) statement = Group( Group( Optional(WITH + delimitedList( Group( ident("name") + AS + LB + ordered_sql("value") + Literal(")").suppress()))))("with") + ordered_sql("query")).addParseAction(to_with_clause) SQLParser = statement # IGNORE SOME COMMENTS oracleSqlComment = Literal("--") + restOfLine mySqlComment = Literal("#") + restOfLine engine.add_ignore(oracleSqlComment) engine.add_ignore(mySqlComment) engine.release()
# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/. # # Contact: Kyle Lahnakoski ([email protected]) # from __future__ import absolute_import, division, unicode_literals from mo_parsing.engine import Engine from moz_sql_parser.keywords import * from moz_sql_parser.utils import * from moz_sql_parser.row_clause import row_clause engine = Engine().use() engine.add_ignore(Literal("--") + restOfLine) engine.add_ignore(Literal("#") + restOfLine) # IDENTIFIER literal_string = Regex(r'\"(\"\"|[^"])*\"').addParseAction(unquote) mysql_ident = Regex(r"\`(\`\`|[^`])*\`").addParseAction(unquote) sqlserver_ident = Regex(r"\[(\]\]|[^\]])*\]").addParseAction(unquote) ident = Combine(~RESERVED + (delimitedList( Literal("*") | literal_string | mysql_ident | sqlserver_ident | Word(IDENT_CHAR), separator=".", combine=True, ))).set_parser_name("identifier") # EXPRESSIONS expr = Forward()