Skip to content

Commit 7379e00

Browse files
Bernd Lörwaldrbock
Bernd Lörwald
authored andcommitted
ddl2cpp: allow inline column constraints ("PRIMARY KEY") but don't break auto-id
1 parent 82758a2 commit 7379e00

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

scripts/ddl2cpp

+34-15
Original file line numberDiff line numberDiff line change
@@ -227,21 +227,6 @@ ddlAutoKeywords = [
227227
]
228228
ddlAutoValue = pp.Or(map(pp.CaselessLiteral, sorted(ddlAutoKeywords, reverse=True)))
229229

230-
ddlColumn = pp.Group(
231-
ddlName("name")
232-
+ ddlType("type")
233-
+ pp.Suppress(pp.Optional(ddlWidth))
234-
+ pp.Suppress(pp.Optional(ddlTimezone))
235-
+ pp.ZeroOrMore(
236-
ddlUnsigned("isUnsigned")
237-
| ddlNotNull("notNull")
238-
| pp.CaselessLiteral("null")
239-
| ddlAutoValue("hasAutoValue")
240-
| ddlDefaultValue("hasDefaultValue")
241-
| pp.Suppress(ddlExpression)
242-
)
243-
)
244-
245230
ddlConstraintKeywords = [
246231
"CONSTRAINT",
247232
"PRIMARY",
@@ -258,6 +243,22 @@ ddlConstraint = pp.Group(
258243
+ ddlExpression
259244
).setResultsName("isConstraint")
260245

246+
ddlColumn = pp.Group(
247+
ddlName("name")
248+
+ ddlType("type")
249+
+ pp.Suppress(pp.Optional(ddlWidth))
250+
+ pp.Suppress(pp.Optional(ddlTimezone))
251+
+ pp.ZeroOrMore(
252+
ddlUnsigned("isUnsigned")
253+
| ddlNotNull("notNull")
254+
| pp.CaselessLiteral("null")
255+
| ddlAutoValue("hasAutoValue")
256+
| ddlDefaultValue("hasDefaultValue")
257+
| pp.Suppress(pp.OneOrMore(pp.Or(map(pp.CaselessLiteral, sorted(ddlConstraintKeywords, reverse=True)))))
258+
| pp.Suppress(ddlExpression)
259+
)
260+
)
261+
261262
# CREATE TABLE parser
262263
ddlIfNotExists = pp.Group(
263264
pp.CaselessLiteral("IF") + pp.CaselessLiteral("NOT") + pp.CaselessLiteral("EXISTS")
@@ -403,6 +404,23 @@ def testTable():
403404
"""
404405
result = ddlCreateTable.parseString(text, parseAll=True)
405406

407+
def testPrimaryKeyAutoIncrement():
408+
for text in [
409+
"CREATE TABLE tab (col INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY)", # mysql
410+
"CREATE TABLE tab (col INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT)", # mysql
411+
"CREATE TABLE tab (col INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)", # sqlite
412+
]:
413+
result = ddlCreateTable.parseString(text, parseAll=True)
414+
assert len(result) == 1
415+
table = result[0]
416+
assert table.tableName == "tab"
417+
assert len(table.columns) == 1
418+
column = table.columns[0]
419+
assert not column.isConstraint
420+
assert column.name == "col"
421+
assert column.type == "integer"
422+
assert column.notNull
423+
assert column.hasAutoValue
406424

407425
def testParser():
408426
testBoolean()
@@ -420,6 +438,7 @@ def testParser():
420438
testMathExpression()
421439
testRational()
422440
testTable()
441+
testPrimaryKeyAutoIncrement()
423442

424443

425444
# CODE GENERATOR

0 commit comments

Comments
 (0)