0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

ddl2cpp: allow inline column constraints ("PRIMARY KEY") but don't break auto-id

This commit is contained in:
Bernd Lörwald 2023-01-30 13:36:42 +01:00 committed by Roland Bock
parent 82758a2046
commit 7379e0001f

View File

@ -227,21 +227,6 @@ ddlAutoKeywords = [
] ]
ddlAutoValue = pp.Or(map(pp.CaselessLiteral, sorted(ddlAutoKeywords, reverse=True))) 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 = [ ddlConstraintKeywords = [
"CONSTRAINT", "CONSTRAINT",
"PRIMARY", "PRIMARY",
@ -258,6 +243,22 @@ ddlConstraint = pp.Group(
+ ddlExpression + ddlExpression
).setResultsName("isConstraint") ).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 # CREATE TABLE parser
ddlIfNotExists = pp.Group( ddlIfNotExists = pp.Group(
pp.CaselessLiteral("IF") + pp.CaselessLiteral("NOT") + pp.CaselessLiteral("EXISTS") pp.CaselessLiteral("IF") + pp.CaselessLiteral("NOT") + pp.CaselessLiteral("EXISTS")
@ -403,6 +404,23 @@ def testTable():
""" """
result = ddlCreateTable.parseString(text, parseAll=True) 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(): def testParser():
testBoolean() testBoolean()
@ -420,6 +438,7 @@ def testParser():
testMathExpression() testMathExpression()
testRational() testRational()
testTable() testTable()
testPrimaryKeyAutoIncrement()
# CODE GENERATOR # CODE GENERATOR