diff --git a/scripts/ddl2cpp b/scripts/ddl2cpp index 8291b84d..495bdce7 100755 --- a/scripts/ddl2cpp +++ b/scripts/ddl2cpp @@ -48,28 +48,32 @@ def get_include_guard_name(namespace, inputfile): val = re.sub("[^A-Za-z]+", "_", namespace + '_' + os.path.basename(inputfile)) return val.upper() +def identity_naming_func(s): + return s -def repl_func(m): +def repl_camel_case_func(m): if m.group(1) == '_': return m.group(2).upper() else: return m.group(1) + m.group(2).upper() +def class_name_naming_func(s): + return re.sub("(^|\s|[_0-9])(\S)", repl_camel_case_func, s) + + +def member_name_naming_func(s): + return re.sub("(\s|_|[0-9])(\S)", repl_camel_case_func, s) + +toClassName = class_name_naming_func +toMemberName = member_name_naming_func + def repl_func_for_args(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) - - 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)) @@ -113,6 +117,7 @@ optionalArgs = { '-fail-on-parse': "abort instead of silent genereation of unusable headers", # failOnParse = 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 + '-identity-naming': "Use table and column names from the ddl (defaults to UpperCamelCase for tables and lowerCamelCase for columns", # identityNaming = True '-help': "show this help" } @@ -129,6 +134,7 @@ failOnParse = False warnOnParse = False parseError = "Parsing error, possible reason: can't parse default value for a field" autoId = False +identityNaming = False if len(sys.argv) >= 4: @@ -143,6 +149,10 @@ if len(sys.argv) >= 4: else: pass +if identityNaming: + toClassName = identity_naming_func + toMemberName = identity_naming_func + pathToDdl = sys.argv[firstPositional] pathToHeader = sys.argv[firstPositional + 1] + '.h' diff --git a/test_scripts/CMakeLists.txt b/test_scripts/CMakeLists.txt index fd3c482a..f2f4c34f 100644 --- a/test_scripts/CMakeLists.txt +++ b/test_scripts/CMakeLists.txt @@ -58,16 +58,26 @@ if (${PYTHONINTERP_FOUND}) "${CMAKE_CURRENT_BINARY_DIR}/fail" test) - set(sqlpp.test.generated.sample "${CMAKE_CURRENT_BINARY_DIR}/Sample") - include_directories(${CMAKE_CURRENT_BINARY_DIR}) - add_custom_command( - OUTPUT "${sqlpp.test.generated.sample}.h" - COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_LIST_DIR}/../scripts/ddl2cpp" "${CMAKE_CURRENT_LIST_DIR}/ddl2cpp_sample_good.sql" "${sqlpp.test.generated.sample}" test - DEPENDS "${CMAKE_CURRENT_LIST_DIR}/ddl2cpp_sample_good.sql" - VERBATIM) + foreach(sample_name sample sample_identity_naming) + set(sqlpp.test.generated.sample.include "${CMAKE_CURRENT_BINARY_DIR}/${sample_name}") + include_directories(${CMAKE_CURRENT_BINARY_DIR}) + set(use_identity_naming) + if(sample_name STREQUAL "sample_identity_naming") + set(use_identity_naming -identity-naming) + endif() + add_custom_command( + OUTPUT "${sqlpp.test.generated.sample.include}.h" + COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_LIST_DIR}/../scripts/ddl2cpp" + ${use_identity_naming} + "${CMAKE_CURRENT_LIST_DIR}/ddl2cpp_sample_good.sql" + "${sqlpp.test.generated.sample.include}" + test + DEPENDS "${CMAKE_CURRENT_LIST_DIR}/ddl2cpp_sample_good.sql" + VERBATIM) - add_executable(sqlpp.test.compiled.sample sample.cpp "${sqlpp.test.generated.sample}.h") - target_link_libraries(sqlpp.test.compiled.sample PRIVATE sqlpp11) + add_executable(sqlpp.test.compiled.${sample_name} ${sample_name}.cpp "${sqlpp.test.generated.sample.include}.h") + target_link_libraries(sqlpp.test.compiled.${sample_name} PRIVATE sqlpp11) + endforeach() endif() endif() diff --git a/test_scripts/sample.cpp b/test_scripts/sample.cpp index 6f48646e..62900760 100644 --- a/test_scripts/sample.cpp +++ b/test_scripts/sample.cpp @@ -1,5 +1,15 @@ -#include +#include int main() { + test::TabFoo tab_foo; + tab_foo.delta = "delta"; + tab_foo.Epsilon = 42; + tab_foo.omega = 3.14; + + test::TabBar tab_bar; + tab_bar.alpha = 42; + tab_bar.beta = "beta"; + tab_bar.gamma = true; + tab_bar.delta = 42; } diff --git a/test_scripts/sample_identity_naming.cpp b/test_scripts/sample_identity_naming.cpp new file mode 100644 index 00000000..4e745463 --- /dev/null +++ b/test_scripts/sample_identity_naming.cpp @@ -0,0 +1,15 @@ +#include + +int main() +{ + test::tab_foo tab_foo; + tab_foo.delta = "delta"; + tab_foo._epsilon = 42; + tab_foo.omega = 3.14; + + test::tab_bar tab_bar; + tab_bar.alpha = 42; + tab_bar.beta = "beta"; + tab_bar.gamma = true; + tab_bar.delta = 42; +}