Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@ 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
driver.cpp
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
Expand Down
10 changes: 9 additions & 1 deletion README
Original file line number Diff line number Diff line change
@@ -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

38 changes: 33 additions & 5 deletions cmake/Modules/UseODB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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}")
Expand All @@ -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}")
Expand All @@ -161,7 +188,8 @@ function(odb_compile outvar)
DEPENDS "${input}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM)
endforeach()
endforeach()
endif()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intendation of this file was messed up, making it nearly impossible to compare.
Please use tabs for intendation.

set(${outvar} ${${outvar}} PARENT_SCOPE)
endfunction()
8 changes: 7 additions & 1 deletion driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
32 changes: 32 additions & 0 deletions employee.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// file : hello/employee.hxx
// copyright : not copyrighted - public domain

#ifndef EMPLOYEE_HXX
#define EMPLOYEE_HXX


#include <string>
#include <cstddef> // std::size_t

#include <odb/core.hxx>

#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