2014-01-28 20:53:22 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
##
|
2015-02-15 19:00:21 +01:00
|
|
|
# Copyright (c) 2013-2015, Roland Bock
|
2014-01-28 20:53:22 +01:00
|
|
|
# All rights reserved.
|
2015-10-13 21:25:10 +02:00
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without modification,
|
2014-01-28 20:53:22 +01:00
|
|
|
# are permitted provided that the following conditions are met:
|
2015-10-13 21:25:10 +02:00
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
2014-01-28 20:53:22 +01:00
|
|
|
# this list of conditions and the following disclaimer.
|
2015-10-13 21:25:10 +02:00
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
2014-01-28 20:53:22 +01:00
|
|
|
# and/or other materials provided with the distribution.
|
2015-10-13 21:25:10 +02: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-28 20:53:22 +01:00
|
|
|
# OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
##
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
|
2015-12-23 18:28:15 +01:00
|
|
|
from pyparsing import CaselessLiteral, Literal, SkipTo, restOfLine, oneOf, ZeroOrMore, Optional, Combine, \
|
2014-01-28 20:53:22 +01:00
|
|
|
WordStart, WordEnd, Word, alphas, alphanums, nums, QuotedString, nestedExpr, MatchFirst, OneOrMore, delimitedList, Or, Group
|
|
|
|
|
|
|
|
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-23 18:28:15 +01:00
|
|
|
negativeSign = Literal('-')
|
|
|
|
ddlNum = Combine(Optional(negativeSign) + Word(nums + "."))
|
2014-01-28 20:53:22 +01:00
|
|
|
ddlTerm = Word(alphas, alphanums + "_$")
|
|
|
|
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-13 21:25:10 +02:00
|
|
|
ddlWord("CONSTRAINT"),
|
|
|
|
ddlWord("PRIMARY"),
|
|
|
|
ddlWord("FOREIGN"),
|
2014-01-28 20:53:22 +01:00
|
|
|
ddlWord("KEY"),
|
2015-10-13 21:25:10 +02:00
|
|
|
ddlWord("INDEX"),
|
|
|
|
ddlWord("UNIQUE"),
|
2014-01-28 20:53:22 +01:00
|
|
|
])
|
|
|
|
ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments])))
|
|
|
|
createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlTerm.setResultsName("tableName") + "(" + Group(delimitedList(ddlColumn)).setResultsName("columns") + ")").setResultsName("create")
|
|
|
|
|
|
|
|
|
|
|
|
ddl = ZeroOrMore(SkipTo(createTable, True))
|
|
|
|
|
|
|
|
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-14 18:17:53 +02:00
|
|
|
'tinyblob': 'blob',
|
|
|
|
'blob': 'blob',
|
|
|
|
'mediumblob': 'blob',
|
|
|
|
'longblob': 'blob',
|
2014-01-28 20:53:22 +01:00
|
|
|
'bool': 'boolean',
|
2015-10-13 21:25:10 +02:00
|
|
|
'boolean': 'boolean',
|
2014-01-28 20:53:22 +01:00
|
|
|
'double': 'floating_point',
|
|
|
|
'float': 'floating_point',
|
2015-10-29 16:41:45 +01:00
|
|
|
'date' : 'day_point',
|
|
|
|
'datetime' : 'time_point',
|
2014-01-28 20:53:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# PROCESS DDL
|
|
|
|
if (len(sys.argv) != 4):
|
|
|
|
print('Usage: ddl2cpp <path to ddl> <path to target (without extension, e.g. /tmp/MyTable)> <namespace>')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
pathToDdl = sys.argv[1]
|
|
|
|
pathToHeader = sys.argv[2] + '.h'
|
|
|
|
namespace = sys.argv[3]
|
|
|
|
ddlFile = open(pathToDdl, 'r')
|
|
|
|
header = open(pathToHeader, 'w')
|
|
|
|
|
2015-10-13 21:25:10 +02:00
|
|
|
print('// generated by ' + ' '.join(sys.argv), file=header)
|
2014-01-28 20:53:22 +01: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-29 22:21:46 +01:00
|
|
|
print('#include <' + INCLUDE + '/data_types.h>', file=header)
|
2014-11-30 19:40:34 +01:00
|
|
|
print('#include <' + INCLUDE + '/char_sequence.h>', file=header)
|
2014-01-28 20:53:22 +01:00
|
|
|
print('', file=header)
|
|
|
|
print('namespace ' + namespace, file=header)
|
|
|
|
print('{', file=header)
|
|
|
|
|
|
|
|
tableCreations = ddl.parseFile(pathToDdl)
|
|
|
|
|
|
|
|
for tableCreation in tableCreations:
|
|
|
|
sqlTableName = tableCreation.create.tableName
|
|
|
|
tableClass = toClassName(sqlTableName)
|
|
|
|
tableMember = toMemberName(sqlTableName)
|
|
|
|
tableNamespace = tableClass + '_'
|
|
|
|
tableTemplateParameters = tableClass
|
|
|
|
print(' namespace ' + tableNamespace, file=header)
|
|
|
|
print(' {', file=header)
|
|
|
|
for column in tableCreation.create.columns:
|
|
|
|
if column.isConstraint:
|
|
|
|
continue
|
|
|
|
sqlColumnName = column[0]
|
|
|
|
columnClass = toClassName(sqlColumnName)
|
|
|
|
tableTemplateParameters += ',\n ' + tableNamespace + '::' + columnClass
|
|
|
|
columnMember = toMemberName(sqlColumnName)
|
|
|
|
sqlColumnType = column[1].lower()
|
|
|
|
columnCanBeNull = not column.notNull
|
|
|
|
print(' struct ' + columnClass, file=header)
|
|
|
|
print(' {', file=header)
|
2014-11-30 19:40:34 +01:00
|
|
|
print(' struct _alias_t', file=header)
|
2014-01-28 20:53:22 +01:00
|
|
|
print(' {', file=header)
|
2015-06-01 22:12:32 +02:00
|
|
|
print(' static constexpr const char _literal[] = "' + sqlColumnName + '";', file=header)
|
2014-11-30 19:40:34 +01:00
|
|
|
print(' using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;', file=header)
|
2014-01-28 20:53:22 +01: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-23 18:06:33 +02:00
|
|
|
traitslist = [NAMESPACE + '::' + types[sqlColumnType]];
|
2014-01-28 20:53:22 +01:00
|
|
|
requireInsert = True
|
|
|
|
if column.hasAutoValue:
|
2014-07-23 18:06:33 +02:00
|
|
|
traitslist.append(NAMESPACE + '::tag::must_not_insert');
|
|
|
|
traitslist.append(NAMESPACE + '::tag::must_not_update');
|
2014-01-28 20:53:22 +01:00
|
|
|
requireInsert = False
|
|
|
|
if not column.notNull:
|
2014-07-23 18:06:33 +02:00
|
|
|
traitslist.append(NAMESPACE + '::tag::can_be_null');
|
2014-01-28 20:53:22 +01:00
|
|
|
requireInsert = False
|
|
|
|
if column.hasDefaultValue:
|
|
|
|
requireInsert = False
|
|
|
|
if requireInsert:
|
2014-07-23 18:06:33 +02:00
|
|
|
traitslist.append(NAMESPACE + '::tag::require_insert');
|
|
|
|
print(' using _traits = ' + NAMESPACE + '::make_traits<' + ', '.join(traitslist) + '>;', file=header)
|
2014-01-28 20:53:22 +01:00
|
|
|
print(' };', file=header)
|
|
|
|
print(' }', file=header)
|
|
|
|
print('', file=header)
|
|
|
|
|
|
|
|
print(' struct ' + tableClass + ': ' + NAMESPACE + '::table_t<' + tableTemplateParameters + '>', file=header)
|
|
|
|
print(' {', file=header)
|
2014-11-30 19:40:34 +01:00
|
|
|
print(' struct _alias_t', file=header)
|
2014-01-28 20:53:22 +01:00
|
|
|
print(' {', file=header)
|
2015-06-01 22:12:32 +02:00
|
|
|
print(' static constexpr const char _literal[] = "' + sqlTableName + '";', file=header)
|
2014-11-30 19:40:34 +01:00
|
|
|
print(' using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;', file=header)
|
2014-01-28 20:53:22 +01: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)
|
|
|
|
|