From f88efada675178cea05460df751793b8e57d9926 Mon Sep 17 00:00:00 2001 From: rbock Date: Sun, 5 Mar 2017 19:50:55 +0100 Subject: [PATCH] Allow for "IF NOT EXISTS" in createTable in ddl --- scripts/ddl2cpp | 5 +++-- tests/sample.sql | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/ddl2cpp b/scripts/ddl2cpp index 14d54d03..603080bf 100755 --- a/scripts/ddl2cpp +++ b/scripts/ddl2cpp @@ -185,7 +185,8 @@ ddlConstraint = Or([ ddlWord("UNIQUE"), ]) ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlFunctionWord("NOW"), ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments]))) -createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlName.setResultsName("tableName") + "(" + Group(delimitedList(ddlColumn)).setResultsName("columns") + ")").setResultsName("create") +ddlIfNotExists = Optional(Group(ddlWord("IF") + ddlWord("NOT") + ddlWord("EXISTS")).setResultsName("ifNotExists")) +createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlIfNotExists + ddlName.setResultsName("tableName") + "(" + Group(delimitedList(ddlColumn)).setResultsName("columns") + ")").setResultsName("create") #ddlString.setDebug(True) #uncomment to debug pyparsing ddl = ZeroOrMore(Suppress(SkipTo(createTable, False)) + createTable) @@ -222,7 +223,7 @@ types = { } ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlFunctionWord("NOW"), ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments]))) -createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlName.setResultsName("tableName") + "(" + Group(delimitedList(ddlColumn)).setResultsName("columns") + ")").setResultsName("create") +createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlIfNotExists + ddlName.setResultsName("tableName") + "(" + Group(delimitedList(ddlColumn)).setResultsName("columns") + ")").setResultsName("create") ddlComment = oneOf(["--", "#"]) + restOfLine if failOnParse: diff --git a/tests/sample.sql b/tests/sample.sql index e6ff21bd..f46f7dd7 100644 --- a/tests/sample.sql +++ b/tests/sample.sql @@ -24,7 +24,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -CREATE TABLE tab_foo +CREATE TABLE IF NOT EXISTS tab_foo ( delta varchar(255), epsilon bigint,