From 7379e0001f3f88a077b74b959964d661a105abaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernd=20L=C3=B6rwald?= Date: Mon, 30 Jan 2023 13:36:42 +0100 Subject: [PATCH] ddl2cpp: allow inline column constraints ("PRIMARY KEY") but don't break auto-id --- scripts/ddl2cpp | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/scripts/ddl2cpp b/scripts/ddl2cpp index ee087860..8405cdd0 100755 --- a/scripts/ddl2cpp +++ b/scripts/ddl2cpp @@ -227,21 +227,6 @@ ddlAutoKeywords = [ ] ddlAutoValue = pp.Or(map(pp.CaselessLiteral, sorted(ddlAutoKeywords, reverse=True))) -ddlColumn = pp.Group( - ddlName("name") - + ddlType("type") - + pp.Suppress(pp.Optional(ddlWidth)) - + pp.Suppress(pp.Optional(ddlTimezone)) - + pp.ZeroOrMore( - ddlUnsigned("isUnsigned") - | ddlNotNull("notNull") - | pp.CaselessLiteral("null") - | ddlAutoValue("hasAutoValue") - | ddlDefaultValue("hasDefaultValue") - | pp.Suppress(ddlExpression) - ) -) - ddlConstraintKeywords = [ "CONSTRAINT", "PRIMARY", @@ -258,6 +243,22 @@ ddlConstraint = pp.Group( + ddlExpression ).setResultsName("isConstraint") +ddlColumn = pp.Group( + ddlName("name") + + ddlType("type") + + pp.Suppress(pp.Optional(ddlWidth)) + + pp.Suppress(pp.Optional(ddlTimezone)) + + pp.ZeroOrMore( + ddlUnsigned("isUnsigned") + | ddlNotNull("notNull") + | pp.CaselessLiteral("null") + | ddlAutoValue("hasAutoValue") + | ddlDefaultValue("hasDefaultValue") + | pp.Suppress(pp.OneOrMore(pp.Or(map(pp.CaselessLiteral, sorted(ddlConstraintKeywords, reverse=True))))) + | pp.Suppress(ddlExpression) + ) +) + # CREATE TABLE parser ddlIfNotExists = pp.Group( pp.CaselessLiteral("IF") + pp.CaselessLiteral("NOT") + pp.CaselessLiteral("EXISTS") @@ -403,6 +404,23 @@ def testTable(): """ result = ddlCreateTable.parseString(text, parseAll=True) +def testPrimaryKeyAutoIncrement(): + for text in [ + "CREATE TABLE tab (col INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY)", # mysql + "CREATE TABLE tab (col INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT)", # mysql + "CREATE TABLE tab (col INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)", # sqlite + ]: + result = ddlCreateTable.parseString(text, parseAll=True) + assert len(result) == 1 + table = result[0] + assert table.tableName == "tab" + assert len(table.columns) == 1 + column = table.columns[0] + assert not column.isConstraint + assert column.name == "col" + assert column.type == "integer" + assert column.notNull + assert column.hasAutoValue def testParser(): testBoolean() @@ -420,6 +438,7 @@ def testParser(): testMathExpression() testRational() testTable() + testPrimaryKeyAutoIncrement() # CODE GENERATOR