From 4c53ffcb3f1be1942a259e8f588d327714277500 Mon Sep 17 00:00:00 2001 From: Aaron Bishop Date: Tue, 25 Sep 2018 08:43:10 -0400 Subject: [PATCH] Add option to ddl2cpp to enable splitting tables into individual header files --- scripts/ddl2cpp | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/scripts/ddl2cpp b/scripts/ddl2cpp index 1649b78e..dc679d5f 100755 --- a/scripts/ddl2cpp +++ b/scripts/ddl2cpp @@ -85,6 +85,26 @@ def usage(optionalArgs = {}): Usage: ddl2cpp \n\ 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(): arg_string = '\n' @@ -118,6 +138,7 @@ optionalArgs = { '-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 '-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" } @@ -135,6 +156,7 @@ warnOnParse = False parseError = "Parsing error, possible reason: can't parse default value for a field" autoId = False identityNaming = False +splitTables = False if len(sys.argv) >= 4: @@ -155,7 +177,7 @@ if identityNaming: pathToDdl = sys.argv[firstPositional] -pathToHeader = sys.argv[firstPositional + 1] + '.h' +pathToHeader = sys.argv[firstPositional + 1] + ('/' if splitTables else '.h') namespace = sys.argv[firstPositional + 2] @@ -297,21 +319,14 @@ def escape_if_reserved(name): # PROCESS DDL tableCreations = ddl.parseFile(pathToDdl) -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) +header = 0 +if not splitTables: + header = beginHeader(pathToHeader, nsList) DataTypeError = False for create in tableCreations: sqlTableName = create.tableName + if splitTables: + header = beginHeader(pathToHeader + sqlTableName + '.h', nsList) tableClass = toClassName(sqlTableName) tableMember = toMemberName(sqlTableName) tableNamespace = tableClass + '_' @@ -385,10 +400,11 @@ for create in tableCreations: print(' };', file=header) print(' };', file=header) print(' };', file=header) + if splitTables: + endHeader(header, nsList) -for ns in nsList: - print('} // namespace ' + ns, file=header) -print('#endif', file=header) +if not splitTables: + endHeader(header, nsList) if (DataTypeError): print("Error: unsupported datatypes." ) print("Possible solutions:")