Compare commits

...

3 commits

5 changed files with 117 additions and 1 deletions

View file

@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.8)
project(KIMA2) project(KIMA2)
set(CMAKE_MODULE_PATH "${CMAKE_HOME_DIRECTORY}/cmake" ${CMAKE_MODULE_PATH})
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)

66
cmake/FindSQLite3.cmake Normal file
View file

@ -0,0 +1,66 @@
#ckwg +4
# Copyright 2010 by Kitware, Inc. All Rights Reserved. Please refer to
# KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
# Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.
# Locate the system installed SQLite3
# The following variables will be set:
#
# SQLite3_FOUND - Set to true if SQLite3 can be found
# SQLite3_INCLUDE_DIR - The path to the SQLite3 header files
# SQLite3_LIBRARY - The full path to the SQLite3 library
if( SQLite3_DIR )
if( SQLite3_FIND_VERSION )
find_package( SQLite3 ${SQLite3_FIND_VERSION} NO_MODULE)
else()
find_package( SQLite3 NO_MODULE)
endif()
elseif( NOT SQLite3_FOUND )
message(STATUS "Searching for sqlite3.h")
find_path( SQLite3_INCLUDE_DIR sqlite3.h )
message(STATUS "Searching for sqlite3.h - ${SQLite3_INCLUDE_DIR}")
message(STATUS "Searching for libsqlite3")
find_library( SQLite3_LIBRARY sqlite3 )
message(STATUS "Searching for libsqlite3 - ${SQLite3_LIBRARY}")
include( FindPackageHandleStandardArgs )
FIND_PACKAGE_HANDLE_STANDARD_ARGS( SQLite3 SQLite3_INCLUDE_DIR SQLite3_LIBRARY )
if( SQLITE3_FOUND )
# Determine the SQLite version found
file( READ ${SQLite3_INCLUDE_DIR}/sqlite3.h SQLite3_INCLUDE_FILE )
string( REGEX REPLACE
".*# *define *SQLITE_VERSION *\\\"([0-9\\.]+)\\\".*" "\\1"
SQLite3_VERSION "${SQLite3_INCLUDE_FILE}" )
string( REGEX REPLACE
"([0-9]+)\\.[0-9]+\\.[0-9]+" "\\1"
SQLite3_VERSION_MAJOR "${SQLite3_VERSION}" )
string( REGEX REPLACE
"[0-9]+\\.([0-9]+)\\.[0-9]+" "\\1"
SQLite3_VERSION_MINOR "${SQLite3_VERSION}" )
string( REGEX REPLACE
"[0-9]+\\.[0-9]+\\.([0-9])+" "\\1"
SQLite3_VERSION_PATCH "${SQLite3_VERSION}" )
# Determine version compatibility
if( SQLite3_FIND_VERSION )
if( SQLite3_FIND_VERSION_EXACT )
if( SQLite3_FIND_VERSION VERSION_EQUAL SQLite3_VERSION )
message( STATUS "SQLite3 version: ${SQLite3_VERSION}" )
set( SQLite3_FOUND TRUE )
endif()
else()
if( (SQLite3_FIND_VERSION VERSION_LESS SQLite3_VERSION) OR
(SQLite3_FIND_VERSION VERSION_EQUAL SQLite3_VERSION) )
message( STATUS "SQLite3 version: ${SQLite3_VERSION}" )
set( SQLite3_FOUND TRUE )
endif()
endif()
else()
message( STATUS "SQLite3 version: ${SQLite3_VERSION}" )
set( SQLite3_FOUND TRUE )
endif()
unset( SQLITE3_FOUND )
endif()
endif()

View file

@ -1,13 +1,17 @@
find_package(Boost 1.62 REQUIRED) find_package(Boost 1.62 REQUIRED)
find_package(SQLite3 REQUIRED)
set(CORE_HEADERS set(CORE_HEADERS
entity.h entity.h
database.h
) )
set(CORE_SOURCES set(CORE_SOURCES
entity.cpp entity.cpp
database.cpp
) )
add_library(core STATIC ${CORE_SOURCES}) add_library(core STATIC ${CORE_SOURCES})
target_link_libraries(core Boost::boost) target_link_libraries(core Boost::boost)
target_link_libraries(core sqlite3)

24
src/core/database.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "database.h"
#include <stdexcept>
Database::Database(const std::string& dbname) : db(nullptr)
{
const int errCode = sqlite3_open(dbname.c_str(), &db);
if(errCode) {
throw std::runtime_error("Could not open database file.");
}
}
Database::~Database()
{
sqlite3_close(db);
}
void Database::exec(const std::string& sql)
{
const int errCode = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, nullptr);
if(errCode) {
throw std::runtime_error("Error in SQL execution.");
}
}

20
src/core/database.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef DATABASE_H
#define DATABASE_H
#include <string>
#include <sqlite3.h>
class Database
{
public:
Database(const std::string& dbname);
~Database();
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;
void exec(const std::string& sql);
private:
sqlite3 *db;
};
#endif // DATABASE_H