diff --git a/scripts/ddl2cpp b/scripts/ddl2cpp index c368338f..ec26dd26 100755 --- a/scripts/ddl2cpp +++ b/scripts/ddl2cpp @@ -38,7 +38,9 @@ ERROR_STRANGE_PARSING = 20 from pyparsing import CaselessLiteral, Literal, SkipTo, restOfLine, oneOf, ZeroOrMore, Optional, Combine, Suppress, \ - WordStart, WordEnd, Word, alphas, alphanums, nums, QuotedString, nestedExpr, MatchFirst, OneOrMore, delimitedList, Or, Group, ParseException + WordStart, WordEnd, Word, alphas, alphanums, nums, QuotedString, nestedExpr, MatchFirst, OneOrMore, delimitedList, \ + Or, Group, ParseException + # HELPERS @@ -53,6 +55,7 @@ def repl_func(m): else: return m.group(1) + m.group(2).upper() + def repl_func_for_args(m): if m.group(1) == '-': return m.group(2).upper() @@ -85,7 +88,8 @@ def help_message(): if argument == '-help': continue arg_string = arg_string + ' [-[no]'+argument+'] ' + optionalArgs[argument] + '\n' - print('Usage: ddl2cpp [-help] ' + arg_string + ' ') + print('Usage: ddl2cpp [-help] ' + arg_string + + ' ') sys.exit(0) optionalArgs = { @@ -139,6 +143,11 @@ NAMESPACE = 'sqlpp' def ddlWord(string): return WordStart(alphanums + "_") + CaselessLiteral(string) + WordEnd(alphanums + "_") +# This function should be refactored if we find some database function which needs parameters +# Right now it works only for something like NOW() in MySQL default field value +def ddlFunctionWord(string): + return CaselessLiteral(string) + OneOrMore("(") + ZeroOrMore(" ") + OneOrMore(")") + ddlString = Or([QuotedString("'"), QuotedString("\"", escQuote='""'), QuotedString("`")]) negativeSign = Literal('-') ddlNum = Combine(Optional(negativeSign) + Word(nums + ".")) @@ -157,6 +166,14 @@ ddlConstraint = Or([ ddlWord("INDEX"), ddlWord("UNIQUE"), ]) +ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlFunctionWord("NOW"), ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments]))) +createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlName.setResultsName("tableName") + "(" + Group(delimitedList(ddlColumn)).setResultsName("columns") + ")").setResultsName("create") + + +ddl = ZeroOrMore(Suppress(SkipTo(createTable, False)) + createTable) + +ddlComment = oneOf(["--", "#"]) + restOfLine +ddl.ignore(ddlComment) # MAP SQL TYPES types = { @@ -205,6 +222,7 @@ if warnOnParse: # PROCESS DDL +tableCreations = ddl.parseFile(pathToDdl) header = open(pathToHeader, 'w') print('// generated by ' + ' '.join(sys.argv), file=header)