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

Add the possibility to keep the table and column names as in the DDL

This commit is contained in:
Sylvain Joubert 2018-02-08 12:02:17 +01:00
parent f6503992f7
commit d2d9a1693c
4 changed files with 64 additions and 19 deletions

View File

@ -48,28 +48,32 @@ def get_include_guard_name(namespace, inputfile):
val = re.sub("[^A-Za-z]+", "_", namespace + '_' + os.path.basename(inputfile)) val = re.sub("[^A-Za-z]+", "_", namespace + '_' + os.path.basename(inputfile))
return val.upper() return val.upper()
def identity_naming_func(s):
return s
def repl_func(m): def repl_camel_case_func(m):
if m.group(1) == '_': if m.group(1) == '_':
return m.group(2).upper() return m.group(2).upper()
else: else:
return m.group(1) + m.group(2).upper() 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): def repl_func_for_args(m):
if m.group(1) == '-': if m.group(1) == '-':
return m.group(2).upper() return m.group(2).upper()
else: else:
return m.group(1) + m.group(2).upper() 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): def setArgumentBool(s, bool_value):
first_lower = lambda s: s[:1].lower() + s[1:] if s else '' # http://stackoverflow.com/a/3847369/5006740 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)) 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 '-fail-on-parse': "abort instead of silent genereation of unusable headers", # failOnParse = True
'-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
'-help': "show this help" '-help': "show this help"
} }
@ -129,6 +134,7 @@ failOnParse = False
warnOnParse = False 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
if len(sys.argv) >= 4: if len(sys.argv) >= 4:
@ -143,6 +149,10 @@ if len(sys.argv) >= 4:
else: else:
pass pass
if identityNaming:
toClassName = identity_naming_func
toMemberName = identity_naming_func
pathToDdl = sys.argv[firstPositional] pathToDdl = sys.argv[firstPositional]
pathToHeader = sys.argv[firstPositional + 1] + '.h' pathToHeader = sys.argv[firstPositional + 1] + '.h'

View File

@ -58,16 +58,26 @@ if (${PYTHONINTERP_FOUND})
"${CMAKE_CURRENT_BINARY_DIR}/fail" "${CMAKE_CURRENT_BINARY_DIR}/fail"
test) test)
set(sqlpp.test.generated.sample "${CMAKE_CURRENT_BINARY_DIR}/Sample") foreach(sample_name sample sample_identity_naming)
include_directories(${CMAKE_CURRENT_BINARY_DIR}) set(sqlpp.test.generated.sample.include "${CMAKE_CURRENT_BINARY_DIR}/${sample_name}")
add_custom_command( include_directories(${CMAKE_CURRENT_BINARY_DIR})
OUTPUT "${sqlpp.test.generated.sample}.h" set(use_identity_naming)
COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_LIST_DIR}/../scripts/ddl2cpp" "${CMAKE_CURRENT_LIST_DIR}/ddl2cpp_sample_good.sql" "${sqlpp.test.generated.sample}" test if(sample_name STREQUAL "sample_identity_naming")
DEPENDS "${CMAKE_CURRENT_LIST_DIR}/ddl2cpp_sample_good.sql" set(use_identity_naming -identity-naming)
VERBATIM) 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") add_executable(sqlpp.test.compiled.${sample_name} ${sample_name}.cpp "${sqlpp.test.generated.sample.include}.h")
target_link_libraries(sqlpp.test.compiled.sample PRIVATE sqlpp11) target_link_libraries(sqlpp.test.compiled.${sample_name} PRIVATE sqlpp11)
endforeach()
endif() endif()
endif() endif()

View File

@ -1,5 +1,15 @@
#include <Sample.h> #include <sample.h>
int main() 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;
} }

View File

@ -0,0 +1,15 @@
#include <sample_identity_naming.h>
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;
}