2014-01-29 03:53:22 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
##
|
2015-02-16 02:00:21 +08:00
|
|
|
# Copyright (c) 2013-2015, Roland Bock
|
2014-01-29 03:53:22 +08:00
|
|
|
# All rights reserved.
|
2015-10-14 03:25:10 +08:00
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without modification,
|
2014-01-29 03:53:22 +08:00
|
|
|
# are permitted provided that the following conditions are met:
|
2015-10-14 03:25:10 +08:00
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
2014-01-29 03:53:22 +08:00
|
|
|
# this list of conditions and the following disclaimer.
|
2015-10-14 03:25:10 +08:00
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
2014-01-29 03:53:22 +08:00
|
|
|
# and/or other materials provided with the distribution.
|
2015-10-14 03:25:10 +08:00
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
|
|
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
2014-01-29 03:53:22 +08:00
|
|
|
# OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
##
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import os
|
2016-05-14 19:30:08 +08:00
|
|
|
|
2014-01-29 03:53:22 +08:00
|
|
|
|
2016-05-05 06:58:53 +08:00
|
|
|
# error codes, we should refactor this later
|
2016-05-06 04:08:27 +08:00
|
|
|
ERROR_BAD_ARGS = 1
|
2016-05-05 06:58:53 +08:00
|
|
|
ERROR_DATA_TYPE = 10
|
|
|
|
ERROR_STRANGE_PARSING = 20
|
2016-03-20 02:46:51 +08:00
|
|
|
|
2016-05-06 04:08:27 +08:00
|
|
|
|
2016-05-05 06:58:53 +08:00
|
|
|
from pyparsing import CaselessLiteral, Literal, SkipTo, restOfLine, oneOf, ZeroOrMore, Optional, Combine, Suppress, \
|
2016-05-14 21:11:48 +08:00
|
|
|
WordStart, WordEnd, Word, alphas, alphanums, nums, QuotedString, nestedExpr, MatchFirst, OneOrMore, delimitedList, \
|
|
|
|
Or, Group, ParseException
|
|
|
|
|
2014-01-29 03:53:22 +08:00
|
|
|
|
|
|
|
# HELPERS
|
2016-05-05 06:58:53 +08:00
|
|
|
|
2014-01-29 03:53:22 +08:00
|
|
|
def get_include_guard_name(namespace, inputfile):
|
|
|
|
val = re.sub("[^A-Za-z]+", "_", namespace + '_' + os.path.basename(inputfile))
|
|
|
|
return val.upper()
|
|
|
|
|
2016-05-05 06:58:53 +08:00
|
|
|
|
2014-01-29 03:53:22 +08:00
|
|
|
def repl_func(m):
|
2016-05-05 06:58:53 +08:00
|
|
|
if m.group(1) == '_':
|
|
|
|
return m.group(2).upper()
|
|
|
|
else:
|
|
|
|
return m.group(1) + m.group(2).upper()
|
|
|
|
|
2016-05-14 21:11:48 +08:00
|
|
|
|
2016-05-05 06:58:53 +08:00
|
|
|
def repl_func_for_args(m):
|
|
|
|
if m.group(1) == '-':
|
2014-01-29 03:53:22 +08:00
|
|
|
return m.group(2).upper()
|
|
|
|
else:
|
|
|
|
return m.group(1) + m.group(2).upper()
|
|
|
|
|
|
|
|
def toClassName(s):
|
|
|
|
return re.sub("(^|\s|[_0-9])(\S)", repl_func, s)
|
|
|
|
|
2016-05-05 06:58:53 +08:00
|
|
|
|
2014-01-29 03:53:22 +08:00
|
|
|
def toMemberName(s):
|
|
|
|
return re.sub("(\s|_|[0-9])(\S)", repl_func, s)
|
|
|
|
|
|
|
|
|
2016-05-05 06:58:53 +08:00
|
|
|
def setArgumentBool(s, bool_value):
|
|
|
|
first_lower = lambda s: s[:1].lower() + s[1:] if s else '' # http://stackoverflow.com/a/3847369/5006740
|
|
|
|
var_name = first_lower(re.sub("(\s|-|[0-9])(\S)", repl_func_for_args, s))
|
|
|
|
globals()[var_name] = bool_value
|
|
|
|
|
|
|
|
|
2016-05-14 19:30:08 +08:00
|
|
|
def usage(optionalArgs = {}):
|
|
|
|
print('\
|
|
|
|
Usage: ddl2cpp <path to ddl> <path to target (without extension, e.g. /tmp/MyTable)> <namespace>\n\
|
|
|
|
ddl2cpp -help')
|
|
|
|
|
2016-05-14 20:57:26 +08:00
|
|
|
|
|
|
|
def help_message():
|
|
|
|
arg_string = '\n'
|
2016-05-15 06:02:46 +08:00
|
|
|
pad = 0
|
|
|
|
padding = 0
|
|
|
|
for argument in list(optionalArgs.keys()):
|
|
|
|
if len(argument) > pad:
|
|
|
|
pad = len(argument)
|
2016-05-14 20:57:26 +08:00
|
|
|
for argument in list(optionalArgs.keys()):
|
|
|
|
if argument == '-help':
|
2016-05-14 19:30:08 +08:00
|
|
|
continue
|
2016-05-15 06:02:46 +08:00
|
|
|
if len(argument) < pad:
|
|
|
|
padding = " " * (pad - len(argument))
|
|
|
|
else:
|
|
|
|
padding = ''
|
|
|
|
arg_string = arg_string + ' [-[no]'+argument+']: ' + padding + optionalArgs[argument] + '\n'
|
2016-05-15 06:05:08 +08:00
|
|
|
print('Usage: ddl2cpp [-help]\n\n OPTIONAL ARGUMENTS:\n' + arg_string +' \n \
|
2016-05-15 06:02:46 +08:00
|
|
|
<path to ddl> <path to target> <namespace>\n\
|
|
|
|
\n\
|
|
|
|
<path to ddl> path to your SQL database/table definitions (SHOW CREATE TABLE SomeTable) \n\
|
|
|
|
<path to target> path to a generated C++ header file. Without extension (no *.h). \n\
|
|
|
|
<namespace> namespace you want. Usually a project/database name\n')
|
2016-05-14 19:30:08 +08:00
|
|
|
sys.exit(0)
|
2016-05-06 04:08:27 +08:00
|
|
|
|
2016-05-14 19:30:08 +08:00
|
|
|
optionalArgs = {
|
2016-05-06 04:08:27 +08:00
|
|
|
# if -some-key is present, it will set variable someKey to True
|
|
|
|
# if -no-some-key is present, it will set variable someKey to False
|
2016-05-14 19:30:08 +08:00
|
|
|
'-timestamp-warning': "show warning about mysql timestamp data type", # timeStampWarning = True
|
2016-05-06 04:08:27 +08:00
|
|
|
# '-no-time-stamp-warning' # timeStampWarning = False
|
2016-05-14 19:30:08 +08:00
|
|
|
'-fail-on-parse': "abort instead of silent genereation of unusable headers", # failOnParse = True
|
|
|
|
'-warn-on-parse': "warn about unusable headers, but continue", # warnOnParse = True
|
2017-06-03 06:41:44 +08:00
|
|
|
'-auto-id': "Assume column 'id' to have an automatic value as if AUTO_INCREMENT was specified (e.g. implicit for SQLite ROWID)", # autoId = True
|
2016-05-14 19:30:08 +08:00
|
|
|
'-help': "show this help"
|
|
|
|
}
|
2016-05-05 06:58:53 +08:00
|
|
|
|
2016-05-14 20:57:26 +08:00
|
|
|
if '-help' in sys.argv:
|
|
|
|
help_message()
|
2016-05-05 06:58:53 +08:00
|
|
|
# ARGUMENT PARSING
|
|
|
|
if len(sys.argv) < (4):
|
2016-05-06 04:08:27 +08:00
|
|
|
usage(optionalArgs)
|
|
|
|
sys.exit(ERROR_BAD_ARGS)
|
2016-05-05 06:58:53 +08:00
|
|
|
|
|
|
|
firstPositional = 1
|
|
|
|
timestampWarning = True
|
2016-05-05 07:40:44 +08:00
|
|
|
failOnParse = False
|
2016-05-06 04:08:27 +08:00
|
|
|
warnOnParse = False
|
|
|
|
parseError = "Parsing error, possible reason: can't parse default value for a field"
|
2017-06-03 06:41:44 +08:00
|
|
|
autoId = False
|
2016-05-05 06:58:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) >= 4:
|
|
|
|
for arg in sys.argv:
|
2016-05-06 04:08:27 +08:00
|
|
|
noArg = arg.replace('-no-', '-')
|
2016-05-14 19:30:08 +08:00
|
|
|
if arg in list(optionalArgs.keys()):
|
2016-05-05 06:58:53 +08:00
|
|
|
setArgumentBool(arg, True)
|
|
|
|
firstPositional += 1
|
|
|
|
elif noArg in optionalArgs:
|
|
|
|
setArgumentBool(noArg, False)
|
|
|
|
firstPositional += 1
|
2016-05-05 07:40:44 +08:00
|
|
|
else:
|
2016-05-06 04:08:27 +08:00
|
|
|
pass
|
2016-05-05 06:58:53 +08:00
|
|
|
|
|
|
|
pathToDdl = sys.argv[firstPositional]
|
2016-05-06 04:08:27 +08:00
|
|
|
|
2016-05-05 06:58:53 +08:00
|
|
|
pathToHeader = sys.argv[firstPositional + 1] + '.h'
|
|
|
|
namespace = sys.argv[firstPositional + 2]
|
2016-05-06 04:08:27 +08:00
|
|
|
|
2016-05-05 06:58:53 +08:00
|
|
|
|
|
|
|
INCLUDE = 'sqlpp11'
|
|
|
|
NAMESPACE = 'sqlpp'
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-29 03:53:22 +08:00
|
|
|
# PARSER
|
|
|
|
def ddlWord(string):
|
|
|
|
return WordStart(alphanums + "_") + CaselessLiteral(string) + WordEnd(alphanums + "_")
|
|
|
|
|
2016-05-14 21:11:48 +08:00
|
|
|
# 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(")")
|
|
|
|
|
2016-06-10 00:18:15 +08:00
|
|
|
ddlString = Or([QuotedString("'"), QuotedString("\"", escQuote='""'), QuotedString("`")])
|
2015-12-24 01:28:15 +08:00
|
|
|
negativeSign = Literal('-')
|
2016-06-10 00:18:15 +08:00
|
|
|
ddlNum = Combine(Optional(negativeSign) + Word(nums + "."))
|
2016-06-10 00:19:37 +08:00
|
|
|
ddlTerm = Word(alphanums + "_$")
|
2016-06-10 00:18:15 +08:00
|
|
|
ddlName = Or([ddlTerm, ddlString])
|
2014-01-29 03:53:22 +08:00
|
|
|
ddlArguments = "(" + delimitedList(Or([ddlString, ddlTerm, ddlNum])) + ")"
|
2017-11-07 04:37:10 +08:00
|
|
|
ddlUnsigned = ddlWord("unsigned").setResultsName("isUnsigned")
|
2014-01-29 03:53:22 +08:00
|
|
|
ddlNotNull = Group(ddlWord("NOT") + ddlWord("NULL")).setResultsName("notNull")
|
2016-05-05 06:58:53 +08:00
|
|
|
ddlDefaultValue = ddlWord("DEFAULT").setResultsName("hasDefaultValue")
|
2017-03-06 02:13:26 +08:00
|
|
|
ddlAutoValue = Or([
|
|
|
|
ddlWord("AUTO_INCREMENT"),
|
|
|
|
ddlWord("SMALLSERIAL"),
|
|
|
|
ddlWord("SERIAL"),
|
|
|
|
ddlWord("BIGSERIAL"),
|
|
|
|
]).setResultsName("hasAutoValue")
|
2016-06-10 00:18:15 +08:00
|
|
|
ddlColumnComment = Group(ddlWord("COMMENT") + ddlString).setResultsName("comment")
|
2014-01-29 03:53:22 +08:00
|
|
|
ddlConstraint = Or([
|
2015-10-14 03:25:10 +08:00
|
|
|
ddlWord("CONSTRAINT"),
|
|
|
|
ddlWord("PRIMARY"),
|
|
|
|
ddlWord("FOREIGN"),
|
2014-01-29 03:53:22 +08:00
|
|
|
ddlWord("KEY"),
|
2015-10-14 03:25:10 +08:00
|
|
|
ddlWord("INDEX"),
|
|
|
|
ddlWord("UNIQUE"),
|
2014-01-29 03:53:22 +08:00
|
|
|
])
|
2017-11-07 04:37:10 +08:00
|
|
|
ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlUnsigned, ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlFunctionWord("NOW"), ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments])))
|
2017-03-06 02:50:55 +08:00
|
|
|
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")
|
2016-06-10 00:19:37 +08:00
|
|
|
#ddlString.setDebug(True) #uncomment to debug pyparsing
|
2016-05-14 21:11:48 +08:00
|
|
|
|
|
|
|
ddl = ZeroOrMore(Suppress(SkipTo(createTable, False)) + createTable)
|
|
|
|
|
|
|
|
ddlComment = oneOf(["--", "#"]) + restOfLine
|
|
|
|
ddl.ignore(ddlComment)
|
2014-01-29 03:53:22 +08:00
|
|
|
|
|
|
|
# MAP SQL TYPES
|
|
|
|
types = {
|
|
|
|
'tinyint': 'tinyint',
|
|
|
|
'smallint': 'smallint',
|
2017-03-06 02:13:26 +08:00
|
|
|
'smallserial': 'smallint', # PostgreSQL
|
2014-01-29 03:53:22 +08:00
|
|
|
'integer': 'integer',
|
|
|
|
'int': 'integer',
|
2017-03-06 02:13:26 +08:00
|
|
|
'serial': 'integer', # PostgreSQL
|
2017-04-26 04:23:59 +08:00
|
|
|
'mediumint' : 'integer',
|
2014-01-29 03:53:22 +08:00
|
|
|
'bigint': 'bigint',
|
2017-03-06 02:13:26 +08:00
|
|
|
'bigserial': 'bigint', # PostgreSQL
|
2014-01-29 03:53:22 +08:00
|
|
|
'char': 'char_',
|
|
|
|
'varchar': 'varchar',
|
|
|
|
'text': 'text',
|
2014-07-15 00:17:53 +08:00
|
|
|
'tinyblob': 'blob',
|
|
|
|
'blob': 'blob',
|
|
|
|
'mediumblob': 'blob',
|
|
|
|
'longblob': 'blob',
|
2014-01-29 03:53:22 +08:00
|
|
|
'bool': 'boolean',
|
2015-10-14 03:25:10 +08:00
|
|
|
'boolean': 'boolean',
|
2014-01-29 03:53:22 +08:00
|
|
|
'double': 'floating_point',
|
|
|
|
'float': 'floating_point',
|
2016-05-05 06:58:53 +08:00
|
|
|
'date': 'day_point',
|
|
|
|
'datetime': 'time_point',
|
|
|
|
'timestamp': 'time_point',
|
|
|
|
'enum': 'text', # MYSQL
|
2017-11-07 04:37:10 +08:00
|
|
|
'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
|
2014-01-29 03:53:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-05 06:58:53 +08:00
|
|
|
if failOnParse:
|
|
|
|
ddl = OneOrMore(Suppress(SkipTo(createTable, False)) + createTable)
|
|
|
|
ddl.ignore(ddlComment)
|
|
|
|
try:
|
|
|
|
tableCreations = ddl.parseFile(pathToDdl)
|
|
|
|
except ParseException as e:
|
2016-05-06 04:08:27 +08:00
|
|
|
print(parseError + '. Exiting [-no-fail-on-parse]')
|
|
|
|
sys.exit(ERROR_STRANGE_PARSING)
|
2016-05-05 06:58:53 +08:00
|
|
|
else:
|
|
|
|
ddl = ZeroOrMore(Suppress(SkipTo(createTable, False)) + createTable)
|
|
|
|
ddl.ignore(ddlComment)
|
|
|
|
tableCreations = ddl.parseFile(pathToDdl)
|
2016-03-20 02:46:51 +08:00
|
|
|
|
2016-05-05 07:40:44 +08:00
|
|
|
if warnOnParse:
|
2016-05-06 04:08:27 +08:00
|
|
|
print(parseError + '. Continuing [-no-warn-on-parse]')
|
2016-05-05 06:58:53 +08:00
|
|
|
|
2017-01-07 13:16:50 +08:00
|
|
|
nsList = namespace.split('::')
|
2016-05-05 06:58:53 +08:00
|
|
|
|
2017-11-27 02:19:26 +08:00
|
|
|
def escape_if_reserved(name):
|
|
|
|
reserved_names = [
|
|
|
|
'GROUP',
|
|
|
|
'ORDER',
|
|
|
|
]
|
|
|
|
if name.upper() in reserved_names:
|
|
|
|
return '!{}'.format(name)
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
2016-05-05 07:40:44 +08:00
|
|
|
# PROCESS DDL
|
2016-05-14 21:11:48 +08:00
|
|
|
tableCreations = ddl.parseFile(pathToDdl)
|
2014-01-29 03:53:22 +08:00
|
|
|
|
|
|
|
header = open(pathToHeader, 'w')
|
2015-10-14 03:25:10 +08:00
|
|
|
print('// generated by ' + ' '.join(sys.argv), file=header)
|
2014-01-29 03:53:22 +08:00
|
|
|
print('#ifndef '+get_include_guard_name(namespace, pathToHeader), file=header)
|
|
|
|
print('#define '+get_include_guard_name(namespace, pathToHeader), file=header)
|
|
|
|
print('', file=header)
|
|
|
|
print('#include <' + INCLUDE + '/table.h>', file=header)
|
2015-10-30 05:21:46 +08:00
|
|
|
print('#include <' + INCLUDE + '/data_types.h>', file=header)
|
2014-12-01 02:40:34 +08:00
|
|
|
print('#include <' + INCLUDE + '/char_sequence.h>', file=header)
|
2014-01-29 03:53:22 +08:00
|
|
|
print('', file=header)
|
2017-01-07 13:16:50 +08:00
|
|
|
for ns in nsList:
|
|
|
|
print('namespace ' + ns, file=header)
|
|
|
|
print('{', file=header)
|
2016-05-05 06:58:53 +08:00
|
|
|
DataTypeError = False
|
2016-03-20 00:31:40 +08:00
|
|
|
for create in tableCreations:
|
2016-03-15 15:08:13 +08:00
|
|
|
sqlTableName = create.tableName
|
2014-01-29 03:53:22 +08:00
|
|
|
tableClass = toClassName(sqlTableName)
|
|
|
|
tableMember = toMemberName(sqlTableName)
|
|
|
|
tableNamespace = tableClass + '_'
|
|
|
|
tableTemplateParameters = tableClass
|
|
|
|
print(' namespace ' + tableNamespace, file=header)
|
|
|
|
print(' {', file=header)
|
2016-03-15 15:08:13 +08:00
|
|
|
for column in create.columns:
|
2014-01-29 03:53:22 +08:00
|
|
|
if column.isConstraint:
|
|
|
|
continue
|
|
|
|
sqlColumnName = column[0]
|
|
|
|
columnClass = toClassName(sqlColumnName)
|
|
|
|
tableTemplateParameters += ',\n ' + tableNamespace + '::' + columnClass
|
|
|
|
columnMember = toMemberName(sqlColumnName)
|
|
|
|
sqlColumnType = column[1].lower()
|
2017-11-07 04:37:10 +08:00
|
|
|
if column.isUnsigned:
|
|
|
|
sqlColumnType = sqlColumnType + ' unsigned';
|
2016-03-20 02:46:51 +08:00
|
|
|
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")
|
|
|
|
print("You can disable this warning using -no-timestamp-warning")
|
2014-01-29 03:53:22 +08:00
|
|
|
columnCanBeNull = not column.notNull
|
|
|
|
print(' struct ' + columnClass, file=header)
|
|
|
|
print(' {', file=header)
|
2014-12-01 02:40:34 +08:00
|
|
|
print(' struct _alias_t', file=header)
|
2014-01-29 03:53:22 +08:00
|
|
|
print(' {', file=header)
|
2017-11-27 02:19:26 +08:00
|
|
|
print(' static constexpr const char _literal[] = "' + escape_if_reserved(sqlColumnName) + '";', file=header)
|
2014-12-01 02:40:34 +08:00
|
|
|
print(' using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;', file=header)
|
2014-01-29 03:53:22 +08:00
|
|
|
print(' template<typename T>', file=header)
|
|
|
|
print(' struct _member_t', file=header)
|
|
|
|
print(' {', file=header)
|
|
|
|
print(' T ' + columnMember + ';', file=header)
|
|
|
|
print(' T& operator()() { return ' + columnMember + '; }', file=header)
|
|
|
|
print(' const T& operator()() const { return ' + columnMember + '; }', file=header)
|
|
|
|
print(' };', file=header)
|
|
|
|
print(' };', file=header)
|
2016-03-22 02:38:37 +08:00
|
|
|
try:
|
2016-05-05 06:58:53 +08:00
|
|
|
traitslist = [NAMESPACE + '::' + types[sqlColumnType]]
|
2016-03-22 02:38:37 +08:00
|
|
|
except KeyError as e:
|
2016-03-22 05:27:01 +08:00
|
|
|
print ('Error: datatype "' + sqlColumnType + '"" is not supported.')
|
2016-03-22 02:48:11 +08:00
|
|
|
DataTypeError = True
|
2014-01-29 03:53:22 +08:00
|
|
|
requireInsert = True
|
2017-06-03 06:41:44 +08:00
|
|
|
column.hasAutoValue = column.hasAutoValue or (autoId and sqlColumnName == 'id')
|
2014-01-29 03:53:22 +08:00
|
|
|
if column.hasAutoValue:
|
2016-05-05 06:58:53 +08:00
|
|
|
traitslist.append(NAMESPACE + '::tag::must_not_insert')
|
|
|
|
traitslist.append(NAMESPACE + '::tag::must_not_update')
|
2014-01-29 03:53:22 +08:00
|
|
|
requireInsert = False
|
|
|
|
if not column.notNull:
|
2016-05-05 06:58:53 +08:00
|
|
|
traitslist.append(NAMESPACE + '::tag::can_be_null')
|
2014-01-29 03:53:22 +08:00
|
|
|
requireInsert = False
|
|
|
|
if column.hasDefaultValue:
|
|
|
|
requireInsert = False
|
|
|
|
if requireInsert:
|
2016-05-05 06:58:53 +08:00
|
|
|
traitslist.append(NAMESPACE + '::tag::require_insert')
|
2014-07-24 00:06:33 +08:00
|
|
|
print(' using _traits = ' + NAMESPACE + '::make_traits<' + ', '.join(traitslist) + '>;', file=header)
|
2014-01-29 03:53:22 +08:00
|
|
|
print(' };', file=header)
|
2017-08-09 23:50:26 +08:00
|
|
|
print(' } // namespace ' + tableNamespace, file=header)
|
2014-01-29 03:53:22 +08:00
|
|
|
print('', file=header)
|
|
|
|
|
|
|
|
print(' struct ' + tableClass + ': ' + NAMESPACE + '::table_t<' + tableTemplateParameters + '>', file=header)
|
|
|
|
print(' {', file=header)
|
2014-12-01 02:40:34 +08:00
|
|
|
print(' struct _alias_t', file=header)
|
2014-01-29 03:53:22 +08:00
|
|
|
print(' {', file=header)
|
2015-06-02 04:12:32 +08:00
|
|
|
print(' static constexpr const char _literal[] = "' + sqlTableName + '";', file=header)
|
2014-12-01 02:40:34 +08:00
|
|
|
print(' using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;', file=header)
|
2014-01-29 03:53:22 +08:00
|
|
|
print(' template<typename T>', file=header)
|
|
|
|
print(' struct _member_t', file=header)
|
|
|
|
print(' {', file=header)
|
|
|
|
print(' T ' + tableMember + ';', file=header)
|
|
|
|
print(' T& operator()() { return ' + tableMember + '; }', file=header)
|
|
|
|
print(' const T& operator()() const { return ' + tableMember + '; }', file=header)
|
|
|
|
print(' };', file=header)
|
|
|
|
print(' };', file=header)
|
|
|
|
print(' };', file=header)
|
|
|
|
|
2017-01-07 13:16:50 +08:00
|
|
|
for ns in nsList:
|
|
|
|
print('} // namespace ' + ns, file=header)
|
2014-01-29 03:53:22 +08:00
|
|
|
print('#endif', file=header)
|
2016-03-22 02:48:11 +08:00
|
|
|
if (DataTypeError):
|
|
|
|
print("Error: unsupported datatypes." )
|
2016-03-22 05:27:01 +08:00
|
|
|
print("Possible solutions:")
|
|
|
|
print("A) Implement this datatype (examples: sqlpp11/data_types)" )
|
|
|
|
print("B) Extend/upgrade ddl2cpp (edit types map)" )
|
|
|
|
print("C) Raise an issue on github" )
|
2016-03-22 02:48:11 +08:00
|
|
|
sys.exit(10) #return non-zero error code, we might need it for automation
|