0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

ddl2cpp: Require word breaks when parsing SQL keywords.

This commit is contained in:
MeanSquaredError 2024-06-11 15:07:58 +03:00 committed by Roland Bock
parent 8797dfd79e
commit 43fa5c445c

View File

@ -45,7 +45,8 @@ ddlString = (
ddlTerm = pp.Word(pp.alphas + "_", pp.alphanums + "_.$") ddlTerm = pp.Word(pp.alphas + "_", pp.alphanums + "_.$")
ddlName = pp.Or([ddlTerm, ddlString, pp.Combine(ddlString + "." + ddlString), pp.Combine(ddlTerm + ddlString)]) ddlName = pp.Or([ddlTerm, ddlString, pp.Combine(ddlString + "." + ddlString), pp.Combine(ddlTerm + ddlString)])
ddlOperator = pp.Or( ddlOperator = pp.Or(
map(pp.CaselessLiteral, ["+", "-", "*", "/", "<", "<=", ">", ">=", "=", "%", "DIV"]) map(pp.CaselessLiteral, ["+", "-", "*", "/", "<", "<=", ">", ">=", "=", "%"]),
pp.CaselessKeyword("DIV")
) )
ddlBracedExpression = pp.Forward() ddlBracedExpression = pp.Forward()
@ -162,44 +163,44 @@ def initDllParser():
# Column and constraint parsers # Column and constraint parsers
ddlBoolean = pp.Or( ddlBoolean = pp.Or(
map(pp.CaselessLiteral, sorted(ddlBooleanTypes, reverse=True)) map(pp.CaselessKeyword, sorted(ddlBooleanTypes, reverse=True))
).setParseAction(pp.replaceWith("boolean")) ).setParseAction(pp.replaceWith("boolean"))
ddlInteger = pp.Or( ddlInteger = pp.Or(
map(pp.CaselessLiteral, sorted(ddlIntegerTypes, reverse=True)) map(pp.CaselessKeyword, sorted(ddlIntegerTypes, reverse=True))
).setParseAction(pp.replaceWith("integer")) ).setParseAction(pp.replaceWith("integer"))
ddlSerial = ( ddlSerial = (
pp.Or(map(pp.CaselessLiteral, sorted(ddlSerialTypes, reverse=True))) pp.Or(map(pp.CaselessKeyword, sorted(ddlSerialTypes, reverse=True)))
.setParseAction(pp.replaceWith("integer")) .setParseAction(pp.replaceWith("integer"))
.setResultsName("hasAutoValue") .setResultsName("hasAutoValue")
) )
ddlFloatingPoint = pp.Or( ddlFloatingPoint = pp.Or(
map(pp.CaselessLiteral, sorted(ddlFloatingPointTypes, reverse=True)) map(pp.CaselessKeyword, sorted(ddlFloatingPointTypes, reverse=True))
).setParseAction(pp.replaceWith("floating_point")) ).setParseAction(pp.replaceWith("floating_point"))
ddlText = pp.Or( ddlText = pp.Or(
map(pp.CaselessLiteral, sorted(ddlTextTypes, reverse=True)) map(pp.CaselessKeyword, sorted(ddlTextTypes, reverse=True))
).setParseAction(pp.replaceWith("text")) ).setParseAction(pp.replaceWith("text"))
ddlBlob = pp.Or( ddlBlob = pp.Or(
map(pp.CaselessLiteral, sorted(ddlBlobTypes, reverse=True)) map(pp.CaselessKeyword, sorted(ddlBlobTypes, reverse=True))
).setParseAction(pp.replaceWith("blob")) ).setParseAction(pp.replaceWith("blob"))
ddlDate = ( ddlDate = (
pp.Or(map(pp.CaselessLiteral, sorted(ddlDateTypes, reverse=True))) pp.Or(map(pp.CaselessKeyword, sorted(ddlDateTypes, reverse=True)))
.setParseAction(pp.replaceWith("day_point")) .setParseAction(pp.replaceWith("day_point"))
.setResultsName("warnTimezone") .setResultsName("warnTimezone")
) )
ddlDateTime = pp.Or( ddlDateTime = pp.Or(
map(pp.CaselessLiteral, sorted(ddlDateTimeTypes, reverse=True)) map(pp.CaselessKeyword, sorted(ddlDateTimeTypes, reverse=True))
).setParseAction(pp.replaceWith("time_point")) ).setParseAction(pp.replaceWith("time_point"))
ddlTime = pp.Or( ddlTime = pp.Or(
map(pp.CaselessLiteral, sorted(ddlTimeTypes, reverse=True)) map(pp.CaselessKeyword, sorted(ddlTimeTypes, reverse=True))
).setParseAction(pp.replaceWith("time_of_day")) ).setParseAction(pp.replaceWith("time_of_day"))
ddlUnknown = pp.Word(pp.alphanums).setParseAction(pp.replaceWith("UNKNOWN")) ddlUnknown = pp.Word(pp.alphanums).setParseAction(pp.replaceWith("UNKNOWN"))
@ -217,19 +218,19 @@ def initDllParser():
| ddlUnknown | ddlUnknown
) )
ddlUnsigned = pp.CaselessLiteral("UNSIGNED").setResultsName("isUnsigned") ddlUnsigned = pp.CaselessKeyword("UNSIGNED").setResultsName("isUnsigned")
ddlDigits = "," + pp.Word(pp.nums) ddlDigits = "," + pp.Word(pp.nums)
ddlWidth = ddlLeft + pp.Word(pp.nums) + pp.Optional(ddlDigits) + ddlRight ddlWidth = ddlLeft + pp.Word(pp.nums) + pp.Optional(ddlDigits) + ddlRight
ddlTimezone = ( ddlTimezone = (
(pp.CaselessLiteral("with") | pp.CaselessLiteral("without")) (pp.CaselessKeyword("with") | pp.CaselessKeyword("without"))
+ pp.CaselessLiteral("time") + pp.CaselessKeyword("time")
+ pp.CaselessLiteral("zone") + pp.CaselessKeyword("zone")
) )
ddlNotNull = pp.Group( ddlNotNull = pp.Group(
pp.CaselessLiteral("NOT") + pp.CaselessLiteral("NULL") pp.CaselessKeyword("NOT") + pp.CaselessKeyword("NULL")
).setResultsName("notNull") ).setResultsName("notNull")
ddlDefaultValue = pp.CaselessLiteral("DEFAULT").setResultsName("hasDefaultValue") ddlDefaultValue = pp.CaselessKeyword("DEFAULT").setResultsName("hasDefaultValue")
ddlAutoKeywords = [ ddlAutoKeywords = [
"AUTO_INCREMENT", "AUTO_INCREMENT",
@ -242,7 +243,7 @@ def initDllParser():
"BIGSERIAL", "BIGSERIAL",
"GENERATED", "GENERATED",
] ]
ddlAutoValue = pp.Or(map(pp.CaselessLiteral, sorted(ddlAutoKeywords, reverse=True))) ddlAutoValue = pp.Or(map(pp.CaselessKeyword, sorted(ddlAutoKeywords, reverse=True)))
ddlConstraintKeywords = [ ddlConstraintKeywords = [
"CONSTRAINT", "CONSTRAINT",
@ -256,7 +257,7 @@ def initDllParser():
"PERIOD", "PERIOD",
] ]
ddlConstraint = pp.Group( ddlConstraint = pp.Group(
pp.Or(map(pp.CaselessLiteral, sorted(ddlConstraintKeywords, reverse=True))) pp.Or(map(pp.CaselessKeyword, sorted(ddlConstraintKeywords, reverse=True)))
+ ddlExpression + ddlExpression
).setResultsName("isConstraint") ).setResultsName("isConstraint")
@ -268,25 +269,25 @@ def initDllParser():
+ pp.ZeroOrMore( + pp.ZeroOrMore(
ddlUnsigned("isUnsigned") ddlUnsigned("isUnsigned")
| ddlNotNull("notNull") | ddlNotNull("notNull")
| pp.CaselessLiteral("null") | pp.CaselessKeyword("null")
| ddlAutoValue("hasAutoValue") | ddlAutoValue("hasAutoValue")
| ddlDefaultValue("hasDefaultValue") | ddlDefaultValue("hasDefaultValue")
| pp.Suppress(pp.OneOrMore(pp.Or(map(pp.CaselessLiteral, sorted(ddlConstraintKeywords, reverse=True))))) | pp.Suppress(pp.OneOrMore(pp.Or(map(pp.CaselessKeyword, sorted(ddlConstraintKeywords, reverse=True)))))
| pp.Suppress(ddlExpression) | 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.CaselessKeyword("IF") + pp.CaselessKeyword("NOT") + pp.CaselessKeyword("EXISTS")
).setResultsName("ifNotExists") ).setResultsName("ifNotExists")
ddlOrReplace = pp.Group( ddlOrReplace = pp.Group(
pp.CaselessLiteral("OR") + pp.CaselessLiteral("REPLACE") pp.CaselessKeyword("OR") + pp.CaselessKeyword("REPLACE")
).setResultsName("orReplace") ).setResultsName("orReplace")
ddlCreateTable = pp.Group( ddlCreateTable = pp.Group(
pp.CaselessLiteral("CREATE") pp.CaselessKeyword("CREATE")
+ pp.Suppress(pp.Optional(ddlOrReplace)) + pp.Suppress(pp.Optional(ddlOrReplace))
+ pp.CaselessLiteral("TABLE") + pp.CaselessKeyword("TABLE")
+ pp.Suppress(pp.Optional(ddlIfNotExists)) + pp.Suppress(pp.Optional(ddlIfNotExists))
+ ddlName.setResultsName("tableName") + ddlName.setResultsName("tableName")
+ ddlLeft + ddlLeft