diff --git a/CMakeLists.txt b/CMakeLists.txt index 86222f8..78d76e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,9 +2,13 @@ cmake_minimum_required(VERSION 2.8.12) project(OdbCMake) +option (AT_ONCE "Build all structures into one file." FALSE) + +set(TARGET_DB "sqlite") + list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules") -find_package(ODB REQUIRED COMPONENTS qt sqlite OPTIONAL_COMPONENTS mysql pgsql) +find_package(ODB REQUIRED COMPONENTS ${TARGET_DB} OPTIONAL_COMPONENTS pgsql) include(${ODB_USE_FILE}) set(OdbCMake_SOURCES @@ -12,13 +16,22 @@ set(OdbCMake_SOURCES database.h) set(OdbCMake_ODB_HEADERS - person.h) + person.h + employee.h) -odb_compile(OdbCMake_SOURCES FILES ${OdbCMake_ODB_HEADERS} DB sqlite GENERATE_QUERY GENERATE_SESSION) +if (AT_ONCE) + message(STATUS "All database code in one file personal_*") + odb_compile(OdbCMake_SOURCES FILES ${OdbCMake_ODB_HEADERS} DB ${TARGET_DB} GENERATE_QUERY GENERATE_SESSION INPUT_NAME "personal") +else() + odb_compile(OdbCMake_SOURCES FILES ${OdbCMake_ODB_HEADERS} DB ${TARGET_DB} GENERATE_QUERY GENERATE_SESSION) +endif() add_executable(odbcmake ${OdbCMake_SOURCES} ${OdbCMake_ODB_HEADERS}) + +target_compile_definitions(odbcmake PUBLIC AT_ONCE) + target_link_libraries(odbcmake ${ODB_LIBRARIES}) target_include_directories(odbcmake diff --git a/README b/README index 6036a71..f2a54d2 100644 --- a/README +++ b/README @@ -1,4 +1,12 @@ CMake Find Module and UseFile for ODB. -See example for information on useage, i'm too lazy to write documentation right now. +### Windows ODB installation notes: +Install all odb components into some directory and setup environment variable CMAKE_PREFIX_PATH=your_odb_installation_directory. +Directory must contain sub-directories bin for executables(especially odb.exe) and dlls, +include/odb for odb headers and lib for lib files. +For example c:\odb\bin, c\odb\include and c:\odb\lib. Note: when you copy odb.exe - do not copy only exe file - you need all directories from archive. + +### Generate project Windows/Linux +* One structure per file. Simply run cmake without options. +* All persistent structures in one file - set option AT_ONCE=ON. For example cmake . -DAT_ONCE=ON diff --git a/cmake/Modules/UseODB.cmake b/cmake/Modules/UseODB.cmake index 7aeb3b8..e2b6316 100644 --- a/cmake/Modules/UseODB.cmake +++ b/cmake/Modules/UseODB.cmake @@ -19,7 +19,8 @@ function(odb_compile outvar) HEADER_PROLOGUE INLINE_PROLOGUE SOURCE_PROLOGUE HEADER_EPILOGUE INLINE_EPILOGUE SOURCE_EPILOGUE MULTI_DATABASE - PROFILE) + PROFILE + INPUT_NAME) set(multiValueParams FILES INCLUDE DB) cmake_parse_arguments(PARAM "${options}" "${oneValueParams}" "${multiValueParams}" ${ARGN}) @@ -107,6 +108,11 @@ function(odb_compile outvar) list(APPEND ODB_ARGS --profile "${PARAM_PROFILE}") endif() + if(PARAM_INPUT_NAME) + list(APPEND ODB_ARGS --at-once) + list(APPEND ODB_ARGS --input-name "${PARAM_INPUT_NAME}") + endif() + list(APPEND ODB_ARGS --output-dir "${ODB_COMPILE_OUTPUT_DIR}") list(APPEND ODB_ARGS --hxx-suffix "${ODB_COMPILE_HEADER_SUFFIX}") list(APPEND ODB_ARGS --ixx-suffix "${ODB_COMPILE_INLINE_SUFFIX}") @@ -135,9 +141,9 @@ function(odb_compile outvar) file(REMOVE_RECURSE "${ODB_COMPILE_OUTPUT_DIR}") file(MAKE_DIRECTORY "${ODB_COMPILE_OUTPUT_DIR}") - foreach(input ${PARAM_FILES}) - get_filename_component(fname "${input}" NAME_WE) - set(outputs) + if(PARAM_INPUT_NAME) + get_filename_component(fname "${PARAM_INPUT_NAME}" NAME_WE) + set(outputs) foreach(sfx ${ODB_COMPILE_FILE_SUFFIX}) string(REGEX REPLACE ":.*$" "" pfx "${sfx}") @@ -149,6 +155,27 @@ function(odb_compile outvar) list(APPEND outputs "${output}") endif() endforeach() + + add_custom_command(OUTPUT ${outputs} + COMMAND ${ODB_EXECUTABLE} ${ODB_ARGS} ${PARAM_FILES} + DEPENDS ${PARAM_FILES} + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + VERBATIM) + else() + foreach(input ${PARAM_FILES}) + get_filename_component(fname "${input}" NAME_WE) + set(outputs) + + foreach(sfx ${ODB_COMPILE_FILE_SUFFIX}) + string(REGEX REPLACE ":.*$" "" pfx "${sfx}") + string(REGEX REPLACE "^.*:" "" sfx "${sfx}") + + if(NOT "${PARAM_MULTI_DATABASE}" MATCHES "static" OR NOT "${pfx}" MATCHES "common") + set(output "${ODB_COMPILE_OUTPUT_DIR}/${fname}${sfx}${ODB_COMPILE_SOURCE_SUFFIX}") + list(APPEND ${outvar} "${output}") + list(APPEND outputs "${output}") + endif() + endforeach() if(ODB_COMPILE_DEBUG) set(_msg "${ODB_EXECUTABLE} ${ODB_ARGS} ${input}") @@ -161,7 +188,8 @@ function(odb_compile outvar) DEPENDS "${input}" WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" VERBATIM) - endforeach() + endforeach() + endif() set(${outvar} ${${outvar}} PARENT_SCOPE) endfunction() diff --git a/driver.cpp b/driver.cpp index 59a07df..f704499 100644 --- a/driver.cpp +++ b/driver.cpp @@ -10,8 +10,14 @@ #include "database.h" // create_database #include "person.h" -#include "person_odb.h" +#include "employee.h" +#ifdef AT_ONCE + #include "personal_odb.h" +#else +#include "person_odb.h" +#include "employee_odb.h" +#endif using namespace std; using namespace odb::core; diff --git a/employee.h b/employee.h new file mode 100644 index 0000000..bb53937 --- /dev/null +++ b/employee.h @@ -0,0 +1,32 @@ +// file : hello/employee.hxx +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + + +#include +#include // std::size_t + +#include + +#pragma db object +class employee +{ +public: + std::string position() const { return position_; } + unsigned short experience() const { return experience_; } + +private: + friend class odb::access; + + employee () {} + + #pragma db id auto + unsigned long id_; + + std::string position_; + unsigned short experience_; +}; + +#endif // EMPLOYEE_HXX