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-03-15 15:08:13 +08:00
|
|
|
import pprint
|
2014-01-29 03:53:22 +08:00
|
|
|
|
2016-03-20 00:31:40 +08:00
|
|
|
from pyparsing import CaselessLiteral, Literal, SkipTo, restOfLine, oneOf, ZeroOrMore, Optional, Combine, Suppress, \
|
2014-01-29 03:53:22 +08:00
|
|
|
WordStart, WordEnd, Word, alphas, alphanums, nums, QuotedString, nestedExpr, MatchFirst, OneOrMore, delimitedList, Or, Group
|
|
|
|
|
2016-03-20 02:46:51 +08:00
|
|
|
def usage():
|
|
|
|
print('Usage: ddl2cpp [-no-timestamp-warning] <path to ddl> <path to target (without extension, e.g. /tmp/MyTable)> <namespace>')
|
|
|
|
|
|
|
|
if len(sys.argv) not in (4,5):
|
|
|
|
usage()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
firstPositional = 1
|
|
|
|
timestampWarning = True
|
|
|
|
if len(sys.argv) == 5:
|
|
|
|
if sys.argv[1] == '-no-timestamp-warning':
|
|
|
|
firstPositional += 1
|
|
|
|
timestampWarning = False
|
|
|
|
else:
|
|
|
|
usage()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
pathToDdl = sys.argv[firstPositional]
|
|
|
|
pathToHeader = sys.argv[firstPositional + 1] + '.h'
|
|
|
|
namespace = sys.argv[firstPositional + 2]
|
|
|
|
|
2014-01-29 03:53:22 +08:00
|
|
|
INCLUDE = 'sqlpp11'
|
|
|
|
NAMESPACE = 'sqlpp'
|
|
|
|
|
|
|
|
# HELPERS
|
|
|
|
def get_include_guard_name(namespace, inputfile):
|
|
|
|
val = re.sub("[^A-Za-z]+", "_", namespace + '_' + os.path.basename(inputfile))
|
|
|
|
return val.upper()
|
|
|
|
|
|
|
|
def repl_func(m):
|
|
|
|
if (m.group(1) == '_'):
|
|
|
|
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)
|
|
|
|
|
|
|
|
def toMemberName(s):
|
|
|
|
return re.sub("(\s|_|[0-9])(\S)", repl_func, s)
|
|
|
|
|
|
|
|
|
|
|
|
# PARSER
|
|
|
|
def ddlWord(string):
|
|
|
|
return WordStart(alphanums + "_") + CaselessLiteral(string) + WordEnd(alphanums + "_")
|
|
|
|
|
|
|
|
ddlString = Or([QuotedString("'"), QuotedString("\"", escQuote='""'), QuotedString("`")])
|
2015-12-24 01:28:15 +08:00
|
|
|
negativeSign = Literal('-')
|
|
|
|
ddlNum = Combine(Optional(negativeSign) + Word(nums + "."))
|
2014-01-29 03:53:22 +08:00
|
|
|
ddlTerm = Word(alphas, alphanums + "_$")
|
2016-03-20 02:46:51 +08:00
|
|
|
ddlName = Or([ddlTerm, ddlString])
|
2014-01-29 03:53:22 +08:00
|
|
|
ddlArguments = "(" + delimitedList(Or([ddlString, ddlTerm, ddlNum])) + ")"
|
|
|
|
ddlNotNull = Group(ddlWord("NOT") + ddlWord("NULL")).setResultsName("notNull")
|
|
|
|
ddlDefaultValue = ddlWord("DEFAULT").setResultsName("hasDefaultValue");
|
|
|
|
ddlAutoValue = ddlWord("AUTO_INCREMENT").setResultsName("hasAutoValue");
|
|
|
|
ddlColumnComment = Group(ddlWord("COMMENT") + ddlString).setResultsName("comment")
|
|
|
|
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
|
|
|
])
|
|
|
|
ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments])))
|
2016-03-20 02:46:51 +08:00
|
|
|
createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlName.setResultsName("tableName") + "(" + Group(delimitedList(ddlColumn)).setResultsName("columns") + ")").setResultsName("create")
|
2014-01-29 03:53:22 +08:00
|
|
|
|
|
|
|
|
2016-03-20 00:31:40 +08:00
|
|
|
ddl = ZeroOrMore(Suppress(SkipTo(createTable, False)) + createTable)
|
2014-01-29 03:53:22 +08:00
|
|
|
|
|
|
|
ddlComment = oneOf(["--", "#"]) + restOfLine
|
|
|
|
ddl.ignore(ddlComment)
|
|
|
|
|
|
|
|
# MAP SQL TYPES
|
|
|
|
types = {
|
|
|
|
'tinyint': 'tinyint',
|
|
|
|
'smallint': 'smallint',
|
|
|
|
'integer': 'integer',
|
|
|
|
'int': 'integer',
|
|
|
|
'bigint': 'bigint',
|
|
|
|
'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',
|
2015-10-29 23:41:45 +08:00
|
|
|
'date' : 'day_point',
|
|
|
|
'datetime' : 'time_point',
|
2016-03-20 02:46:51 +08:00
|
|
|
'timestamp' : 'time_point',
|
2014-01-29 03:53:22 +08:00
|
|
|
}
|
|
|
|
|
2016-03-20 02:46:51 +08:00
|
|
|
|
2014-01-29 03:53:22 +08:00
|
|
|
# PROCESS DDL
|
2016-03-20 02:46:51 +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)
|
|
|
|
print('namespace ' + namespace, file=header)
|
|
|
|
print('{', file=header)
|
|
|
|
|
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()
|
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)
|
2015-06-02 04:12:32 +08:00
|
|
|
print(' static constexpr const char _literal[] = "' + 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)
|
2014-07-24 00:06:33 +08:00
|
|
|
traitslist = [NAMESPACE + '::' + types[sqlColumnType]];
|
2014-01-29 03:53:22 +08:00
|
|
|
requireInsert = True
|
|
|
|
if column.hasAutoValue:
|
2014-07-24 00:06:33 +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:
|
2014-07-24 00:06:33 +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:
|
2014-07-24 00:06:33 +08:00
|
|
|
traitslist.append(NAMESPACE + '::tag::require_insert');
|
|
|
|
print(' using _traits = ' + NAMESPACE + '::make_traits<' + ', '.join(traitslist) + '>;', file=header)
|
2014-01-29 03:53:22 +08:00
|
|
|
print(' };', file=header)
|
|
|
|
print(' }', file=header)
|
|
|
|
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)
|
|
|
|
|
|
|
|
print('}', file=header)
|
|
|
|
print('#endif', file=header)
|
|
|
|
|