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

Merge pull request #249 from Erroneous1/feature/split_tables

Add option to ddl2cpp to enable splitting tables
This commit is contained in:
Roland Bock 2018-09-25 16:38:51 +02:00 committed by GitHub
commit 9d430b2e20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -85,6 +85,26 @@ def usage(optionalArgs = {}):
Usage: ddl2cpp <path to ddl> <path to target (without extension, e.g. /tmp/MyTable)> <namespace>\n\ Usage: ddl2cpp <path to ddl> <path to target (without extension, e.g. /tmp/MyTable)> <namespace>\n\
ddl2cpp -help') ddl2cpp -help')
def beginHeader(pathToHeader, nsList):
header = open(pathToHeader, 'w')
print('// generated by ' + ' '.join(sys.argv), file=header)
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)
print('#include <' + INCLUDE + '/data_types.h>', file=header)
print('#include <' + INCLUDE + '/char_sequence.h>', file=header)
print('', file=header)
for ns in nsList:
print('namespace ' + ns, file=header)
print('{', file=header)
return header
def endHeader(header, nsList):
for ns in nsList:
print('} // namespace ' + ns, file=header)
print('#endif', file=header)
header.close()
def help_message(): def help_message():
arg_string = '\n' arg_string = '\n'
@ -118,6 +138,7 @@ optionalArgs = {
'-warn-on-parse': "warn about unusable headers, but continue", # warnOnParse = True '-warn-on-parse': "warn about unusable headers, but continue", # warnOnParse = True
'-auto-id': "Assume column 'id' to have an automatic value as if AUTO_INCREMENT was specified (e.g. implicit for SQLite ROWID)", # autoId = True '-auto-id': "Assume column 'id' to have an automatic value as if AUTO_INCREMENT was specified (e.g. implicit for SQLite ROWID)", # autoId = True
'-identity-naming': "Use table and column names from the ddl (defaults to UpperCamelCase for tables and lowerCamelCase for columns", # identityNaming = True '-identity-naming': "Use table and column names from the ddl (defaults to UpperCamelCase for tables and lowerCamelCase for columns", # identityNaming = True
'-split-tables': "Make a header for each table name, using target as a directory", # splitTables = True
'-help': "show this help" '-help': "show this help"
} }
@ -135,6 +156,7 @@ warnOnParse = False
parseError = "Parsing error, possible reason: can't parse default value for a field" parseError = "Parsing error, possible reason: can't parse default value for a field"
autoId = False autoId = False
identityNaming = False identityNaming = False
splitTables = False
if len(sys.argv) >= 4: if len(sys.argv) >= 4:
@ -155,7 +177,7 @@ if identityNaming:
pathToDdl = sys.argv[firstPositional] pathToDdl = sys.argv[firstPositional]
pathToHeader = sys.argv[firstPositional + 1] + '.h' pathToHeader = sys.argv[firstPositional + 1] + ('/' if splitTables else '.h')
namespace = sys.argv[firstPositional + 2] namespace = sys.argv[firstPositional + 2]
@ -297,21 +319,14 @@ def escape_if_reserved(name):
# PROCESS DDL # PROCESS DDL
tableCreations = ddl.parseFile(pathToDdl) tableCreations = ddl.parseFile(pathToDdl)
header = open(pathToHeader, 'w') header = 0
print('// generated by ' + ' '.join(sys.argv), file=header) if not splitTables:
print('#ifndef '+get_include_guard_name(namespace, pathToHeader), file=header) header = beginHeader(pathToHeader, nsList)
print('#define '+get_include_guard_name(namespace, pathToHeader), file=header)
print('', file=header)
print('#include <' + INCLUDE + '/table.h>', file=header)
print('#include <' + INCLUDE + '/data_types.h>', file=header)
print('#include <' + INCLUDE + '/char_sequence.h>', file=header)
print('', file=header)
for ns in nsList:
print('namespace ' + ns, file=header)
print('{', file=header)
DataTypeError = False DataTypeError = False
for create in tableCreations: for create in tableCreations:
sqlTableName = create.tableName sqlTableName = create.tableName
if splitTables:
header = beginHeader(pathToHeader + sqlTableName + '.h', nsList)
tableClass = toClassName(sqlTableName) tableClass = toClassName(sqlTableName)
tableMember = toMemberName(sqlTableName) tableMember = toMemberName(sqlTableName)
tableNamespace = tableClass + '_' tableNamespace = tableClass + '_'
@ -385,10 +400,11 @@ for create in tableCreations:
print(' };', file=header) print(' };', file=header)
print(' };', file=header) print(' };', file=header)
print(' };', file=header) print(' };', file=header)
if splitTables:
endHeader(header, nsList)
for ns in nsList: if not splitTables:
print('} // namespace ' + ns, file=header) endHeader(header, nsList)
print('#endif', file=header)
if (DataTypeError): if (DataTypeError):
print("Error: unsupported datatypes." ) print("Error: unsupported datatypes." )
print("Possible solutions:") print("Possible solutions:")