From fd939975ee0c61000467b8a0f18488226dd5812a Mon Sep 17 00:00:00 2001 From: rbock Date: Mon, 6 Nov 2017 21:37:10 +0100 Subject: [PATCH] Scan for unsigned integral columns --- scripts/ddl2cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/scripts/ddl2cpp b/scripts/ddl2cpp index 2deae826..ff5ab756 100755 --- a/scripts/ddl2cpp +++ b/scripts/ddl2cpp @@ -169,6 +169,7 @@ ddlNum = Combine(Optional(negativeSign) + Word(nums + ".")) ddlTerm = Word(alphanums + "_$") ddlName = Or([ddlTerm, ddlString]) ddlArguments = "(" + delimitedList(Or([ddlString, ddlTerm, ddlNum])) + ")" +ddlUnsigned = ddlWord("unsigned").setResultsName("isUnsigned") ddlNotNull = Group(ddlWord("NOT") + ddlWord("NULL")).setResultsName("notNull") ddlDefaultValue = ddlWord("DEFAULT").setResultsName("hasDefaultValue") ddlAutoValue = Or([ @@ -186,7 +187,7 @@ ddlConstraint = Or([ ddlWord("INDEX"), ddlWord("UNIQUE"), ]) -ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlFunctionWord("NOW"), ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments]))) +ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlUnsigned, ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlFunctionWord("NOW"), ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments]))) 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 @@ -222,12 +223,17 @@ types = { 'datetime': 'time_point', 'timestamp': 'time_point', 'enum': 'text', # MYSQL - 'set': 'text', # MYSQL + 'set': 'text', # MYSQL, + 'tinyint unsigned': 'tinyint_unsigned', #MYSQL + 'smallint unsigned': 'smallint_unsigned', #MYSQL + 'integer unsigned': 'integer_unsigned', #MYSQL + 'int unsigned': 'integer_unsigned', #MYSQL + 'bigint unsigned': 'bigint_unsigned', #MYSQL } -ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlFunctionWord("NOW"), ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments]))) -createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlIfNotExists + ddlName.setResultsName("tableName") + "(" + Group(delimitedList(ddlColumn)).setResultsName("columns") + ")").setResultsName("create") -ddlComment = oneOf(["--", "#"]) + restOfLine +# ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlUnsigned, ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlFunctionWord("NOW"), ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments]))) +#createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlIfNotExists + ddlName.setResultsName("tableName") + "(" + Group(delimitedList(ddlColumn)).setResultsName("columns") + ")").setResultsName("create") +#ddlComment = oneOf(["--", "#"]) + restOfLine if failOnParse: ddl = OneOrMore(Suppress(SkipTo(createTable, False)) + createTable) @@ -272,6 +278,7 @@ for create in tableCreations: print(' namespace ' + tableNamespace, file=header) print(' {', file=header) for column in create.columns: + print(column); if column.isConstraint: continue sqlColumnName = column[0] @@ -279,6 +286,8 @@ for create in tableCreations: tableTemplateParameters += ',\n ' + tableNamespace + '::' + columnClass columnMember = toMemberName(sqlColumnName) sqlColumnType = column[1].lower() + if column.isUnsigned: + sqlColumnType = sqlColumnType + ' unsigned'; if sqlColumnType == 'timestamp' and timestampWarning: print("Warning: timestamp is mapped to sqlpp::time_point like datetime") print("Warning: You have to take care of timezones yourself")