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

merge with devel with regression

This commit is contained in:
strangeqargo 2016-05-14 16:11:48 +03:00
parent 11f40dc20c
commit 32dfdf7f4d

View File

@ -38,7 +38,9 @@ ERROR_STRANGE_PARSING = 20
from pyparsing import CaselessLiteral, Literal, SkipTo, restOfLine, oneOf, ZeroOrMore, Optional, Combine, Suppress, \ 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 # HELPERS
@ -53,6 +55,7 @@ def repl_func(m):
else: else:
return m.group(1) + m.group(2).upper() return m.group(1) + m.group(2).upper()
def repl_func_for_args(m): def repl_func_for_args(m):
if m.group(1) == '-': if m.group(1) == '-':
return m.group(2).upper() return m.group(2).upper()
@ -85,7 +88,8 @@ def help_message():
if argument == '-help': if argument == '-help':
continue continue
arg_string = arg_string + ' [-[no]'+argument+'] ' + optionalArgs[argument] + '\n' arg_string = arg_string + ' [-[no]'+argument+'] ' + optionalArgs[argument] + '\n'
print('Usage: ddl2cpp [-help] ' + arg_string + ' <path to ddl> <path to target (without extension, e.g. /tmp/MyTable)> <namespace>') print('Usage: ddl2cpp [-help] ' + arg_string +
' <path to ddl> <path to target (without extension, e.g. /tmp/MyTable)> <namespace>')
sys.exit(0) sys.exit(0)
optionalArgs = { optionalArgs = {
@ -139,6 +143,11 @@ NAMESPACE = 'sqlpp'
def ddlWord(string): def ddlWord(string):
return WordStart(alphanums + "_") + CaselessLiteral(string) + WordEnd(alphanums + "_") 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("`")]) ddlString = Or([QuotedString("'"), QuotedString("\"", escQuote='""'), QuotedString("`")])
negativeSign = Literal('-') negativeSign = Literal('-')
ddlNum = Combine(Optional(negativeSign) + Word(nums + ".")) ddlNum = Combine(Optional(negativeSign) + Word(nums + "."))
@ -157,6 +166,14 @@ ddlConstraint = Or([
ddlWord("INDEX"), ddlWord("INDEX"),
ddlWord("UNIQUE"), 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 # MAP SQL TYPES
types = { types = {
@ -205,6 +222,7 @@ if warnOnParse:
# PROCESS DDL # PROCESS DDL
tableCreations = ddl.parseFile(pathToDdl)
header = open(pathToHeader, 'w') header = open(pathToHeader, 'w')
print('// generated by ' + ' '.join(sys.argv), file=header) print('// generated by ' + ' '.join(sys.argv), file=header)