make transaction possible to use :)

This commit is contained in:
Bartosz Wieczorek 2018-03-12 14:17:35 +01:00
parent 3dc4d99722
commit e440049c4b
21 changed files with 160 additions and 117 deletions

View File

@ -21,10 +21,8 @@ include(cmake/cotire.cmake)
set(CMAKE_INSTALL_RPATH "${HUNTER_INSTALL_PREFIX}/lib")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
include_directories(${CMAKE_BINARY_DIR}/external/include SYSTEM)
include_directories(${CMAKE_BINARY_DIR}/external/include/date SYSTEM)
include_directories(${CMAKE_BINARY_DIR}/local/include SYSTEM)
include_directories(${CMAKE_BINARY_DIR}/local/include/date SYSTEM)
include_directories(share SYSTEM)
add_subdirectory(src)
#add_subdirectory(tests)
#add_subdirectory(benchmarks)

View File

@ -20,8 +20,25 @@ find_package(spdlog CONFIG REQUIRED)
hunter_add_package(PostgreSQL)
find_package(PostgreSQL REQUIRED)
set(MAKE_CMD "${CMAKE_CURRENT_SOURCE_DIR}/cmake/install_deps.sh")
MESSAGE("COMMAND: ${MAKE_CMD} ${CMAKE_COMMAND} ${CMAKE_GENERATOR} ${CMAKE_CXX_COMPILER} ${CMAKE_C_COMPILER} ${CMAKE_PREFIX_PATH} ${Boost_LIBRARY_DIR_DEBUG}")
#execute_process(
# COMMAND x-terminal-emulator -e "sudo ${MAKE_CMD} ${CMAKE_GENERATOR} ${CMAKE_COMMAND} ${CMAKE_CXX_COMPILER} ${CMAKE_C_COMPILER} ${CMAKE_PREFIX_PATH}"
# WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/external )
set(INSTALL_DEPS_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/cmake/install_deps.sh")
execute_process(
COMMAND ${INSTALL_DEPS_SCRIPT}
${CMAKE_COMMAND}
${CMAKE_GENERATOR}
${CMAKE_CXX_COMPILER}
${CMAKE_C_COMPILER}
${CMAKE_PREFIX_PATH}
${Boost_LIBRARY_DIR_DEBUG}
${CMAKE_BINARY_DIR}/local
${CMAKE_BUILD_TYPE}
# TIMEOUT <seconds>
RESULT_VARIABLE rv
OUTPUT_VARIABLE ov
ERROR_VARIABLE ev
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/_3rdParty
)
message("rv='${rv}'")
message("ov='${ov}'")
message("ev='${ev}'")

View File

@ -4,6 +4,12 @@ CMAKE_CXX_COMPILER=$3
CMAKE_C_COMPILER=$4
CMAKE_PREFIX_PATH=$5
CMAKE_RPATH=$6
CMAKE_INSTALL_PREFIX=$7
CMAKE_BUILD_TYPE=$8
if [ ! -d ${CMAKE_INSTALL_PREFIX} ]; then
mkdir -p ${CMAKE_INSTALL_PREFIX}
fi
# clone all
if [ ! -d date/.git ]; then
@ -30,35 +36,54 @@ else
cd wt; git pull; cd ../
fi
if [ ! -d ChaiScript/.git ]; then
git clone -b develop --depth=1 https://github.com/ChaiScript/ChaiScript.git
else
cd ChaiScript; git pull; cd ../
fi
# install all
mkdir date-build; cd date-build
${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ../date -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} \
${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ../date\
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}\
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} \
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}\
-DTZ_CXX_STANDARD=14
${CMAKE_COMMAND} --build . --target install
cd ../
mkdir sqlpp11-build; cd sqlpp11-build
${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ../sqlpp11 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}\
${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ../sqlpp11\
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}\
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}\
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}\
-DENABLE_TESTS=FALSE
${CMAKE_COMMAND} --build . --target install
cd ../
mkdir sqlpp11-connector-postgresql-build; cd sqlpp11-connector-postgresql-build
${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ../sqlpp11-connector-postgresql -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}\
${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ../sqlpp11-connector-postgresql\
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}\
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}\
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}\
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}\
-DENABLE_TESTS=FALSE
${CMAKE_COMMAND} --build . --target install
cd ../
mkdir wt-build; cd wt-build
${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ../wt -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} \
-DBoost_NO_BOOST_CMAKE=FALSE\
${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ../wt\
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}\
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}\
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}\
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}\
-DCMAKE_INSTALL_RPATH=${CMAKE_RPATH}\
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}\
-DBoost_NO_BOOST_CMAKE=FALSE\
-DBoost_NO_SYSTEM_PATHS=TRUE\
-DBUILD_EXAMPLES=OFF\
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}\
-DINSTALL_RESOURCES=OFF\
-DINSTALL_RESOURCES=ON\
-DENABLE_SSL=OFF\
-DENABLE_HARU=OFF\
-DENABLE_PANGO=OFF\
@ -75,5 +100,19 @@ ${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ../wt -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COM
${CMAKE_COMMAND} --build . --target install
cd ../
sleep 100000
mkdir ChaiScript-build; cd ChaiScript-build
${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ../ChaiScript\
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}\
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}\
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}\
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}\
-DMULTITHREAD_SUPPORT_ENABLED=TRUE\
-DDYNLOAD_ENABLED=FALSE\
-DBUILD_MODULES=TRUE\
-DBUILD_SAMPLES=FALSE\
-DRUN_FUZZY_TESTS=FALSE\
-DUSE_STD_MAKE_SHARED=TRUE\
-DRUN_PERFORMANCE_TESTS=FALSE\
-DBUILD_IN_CPP17_MODE=TRUE
${CMAKE_COMMAND} --build . --target install
cd ../

View File

@ -13,7 +13,7 @@ class PgParametersRepository : public ParametersRepository {
public:
PgParametersRepository(db::PgConnection&db);
std::unique_ptr< Parameter > create(std::string name, std::string description) const override;
void create(std::string name, std::optional< Unit > unit, parameter::Type type) const override;
std::unique_ptr< Parameter > search(std::string name) const override;
private:

View File

@ -3,6 +3,7 @@
#include <memory>
#include <sqlpp11/postgresql/postgresql.h>
#include <sqlpp11/transaction.h>
namespace eedb::db {
class PgConfig;
@ -11,16 +12,37 @@ class PgConnection {
public:
PgConnection(std::shared_ptr< eedb::db::PgConfig > config);
sqlpp::postgresql::connection * native() {
return _conn.get();
}
template < typename T >
auto operator()(T && t) {
return _conn->operator()(std::forward< T >(t));
return _conn.operator()(std::forward< T >(t));
}
auto execute(const std::string & query) {
return _conn.execute(query);
}
void start_transaction(sqlpp::isolation_level level = sqlpp::isolation_level::undefined) {
_in_transaction_count++;
_conn.start_transaction(level);
}
void commit_transaction() {
_in_transaction_count--;
_conn.commit_transaction();
}
void report_rollback_failure(const std::string & message) noexcept {
_conn.report_rollback_failure(message);
}
void rollback_transaction(bool report) {
_in_transaction_count--;
_conn.rollback_transaction(report);
}
private:
std::unique_ptr< sqlpp::postgresql::connection > _conn;
sqlpp::postgresql::connection _conn;
int _in_transaction_count{0};
};
} // namespace eedb::db

View File

@ -11,8 +11,8 @@ void exec_file(PgConnection & db, const std::experimental::filesystem::path & fi
std::ifstream stream(filepath);
std::string filebuf{std::istreambuf_iterator< char >(stream), std::istreambuf_iterator< char >()};
db.native()->execute("SET client_min_messages=WARNING;");
db.native()->execute(filebuf);
db.execute("SET client_min_messages=WARNING;");
db.execute(filebuf);
std::cerr.clear();
}

View File

@ -3,6 +3,5 @@
namespace eedb::db {
PgConnection::PgConnection(std::shared_ptr< eedb::db::PgConfig > config)
: _conn(std::make_unique< sqlpp::postgresql::connection >(config)) {}
PgConnection::PgConnection(std::shared_ptr< eedb::db::PgConfig > config) : _conn(config) {}
} // namespace eedb::db

View File

@ -1,19 +1,37 @@
#include <eedb/db/pg/ParametersRepository.hpp>
#include <sqlpp11/sqlpp11.h>
#include <eedb/db/User.hpp>
#include <eedb/db/pg/Parameter.hpp>
#include <eedb/db/pg/connection.hpp>
#include <eedb/db/pg/model/all.hpp>
#include <sqlpp11/sqlpp11.h>
namespace eedb{
struct PgParametersRepository::PgParametersRepositoryPriv {};
namespace eedb {
struct PgParametersRepository::PgParametersRepositoryPriv {
PgParametersRepositoryPriv(db::PgConnection & db, User * user = nullptr) : _db{db}, _user{user} {}
PgParametersRepository::PgParametersRepository(db::PgConnection & db) : _priv{spimpl::make_unique_impl< PgParametersRepositoryPriv >()} {}
void create(std::string name, std::optional< Unit > unit, parameter::Type type) {
using namespace sqlpp;
_db(postgresql::insert_into(t_parameter) //
.set(t_parameter.owner = _user->uid(), t_parameter.name = name)
.returning(t_parameter.id));
}
std::unique_ptr<Parameter> PgParametersRepository::create(std::string name, std::string description) const
{
private:
db::PgConnection & _db;
User * _user;
// std::map< std::string, PgParameter > _cache;
};
PgParametersRepository::PgParametersRepository(db::PgConnection & db) : _priv{spimpl::make_unique_impl< PgParametersRepositoryPriv >(db)} {}
void PgParametersRepository::create(std::string name, std::optional<Unit> unit, parameter::Type type) const {
_priv->create(std::move(name), std::move(unit), type);
}
std::unique_ptr<Parameter> PgParametersRepository::search(std::string name) const
{
std::unique_ptr< Parameter > PgParametersRepository::search(std::string name) const {
return {};
}
} // namespace eedb

View File

@ -107,10 +107,12 @@ class PgTestDatabasePrepare {
template < typename T >
class DbTestBase : public testing::Test {
public:
DbTestBase() : transaction{sqlpp::start_transaction(db())} {}
static void SetUpTestCase() {
_test_db = std::make_unique< PgTestDatabasePrepare >();
// _test_db->db().native()->start_transaction();
_test_db->db().native()->execute("SET synchronous_commit=off;");
_test_db->db().execute("SET synchronous_commit=off;");
eedb::db::exec_file(_test_db->db(), "/home/bwieczor/src/eedb/sql/schema.sql");
// _test_db->db().native()->commit_transaction();
}
@ -123,6 +125,11 @@ class DbTestBase : public testing::Test {
return _test_db->db();
}
virtual ~DbTestBase(){
transaction.rollback();
}
protected:
static std::unique_ptr< PgTestDatabasePrepare > _test_db;
sqlpp::transaction_t< eedb::db::PgConnection > transaction;
};

View File

@ -10,7 +10,6 @@ class PgAuthIdentitiesTest : public DbTestBase< PgAuthIdentitiesTest > {
public:
PgAuthIdentitiesTest() : user{db()} {
using namespace testing;
db().native()->start_transaction();
user._init();
user._createIdentity({"identity", "provider"});
@ -19,10 +18,6 @@ class PgAuthIdentitiesTest : public DbTestBase< PgAuthIdentitiesTest > {
sut = std::make_unique< eedb::PgAuthIdentities >(db(), &user);
}
~PgAuthIdentitiesTest() {
db().native()->rollback_transaction(false);
}
protected:
eedb::db::pg::UserMock user;
std::unique_ptr< eedb::PgAuthIdentities > sut;

View File

@ -8,17 +8,12 @@ class PgAuthIdentityTest : public DbTestBase< PgAuthIdentityTest > {
public:
PgAuthIdentityTest() {
using namespace testing;
db().native()->start_transaction();
}
void initCached() {
sut = std::make_unique< eedb::PgAuthIdentity >(db(), "identity", "provider");
}
~PgAuthIdentityTest() {
db().native()->rollback_transaction(false);
}
protected:
std::unique_ptr< eedb::PgAuthIdentity > sut;
};

View File

@ -16,8 +16,6 @@ class PgAuthTokenTest : public DbTestBase< PgAuthTokenTest > {
public:
PgAuthTokenTest() : user{db()} {
using namespace testing;
db().native()->start_transaction();
user._init();
user._expect_call_uid();
@ -39,10 +37,6 @@ class PgAuthTokenTest : public DbTestBase< PgAuthTokenTest > {
db()(update(t_auth_token).set(t_auth_token.expires = time_point_cast< microseconds >(exp)).where(t_auth_token.id == sutId));
}
~PgAuthTokenTest() {
db().native()->rollback_transaction(false);
}
protected:
eedb::db::pg::UserMock user;

View File

@ -11,7 +11,6 @@ class PgAuthTokensTest : public DbTestBase< PgAuthTokensTest > {
public:
PgAuthTokensTest() : user{db()} {
using namespace testing;
db().native()->start_transaction();
user._init();
user._expect_call_uid();
@ -23,10 +22,6 @@ class PgAuthTokensTest : public DbTestBase< PgAuthTokensTest > {
token._init(db(), role, user, hash);
}
~PgAuthTokensTest() {
db().native()->rollback_transaction(false);
}
protected:
eedb::db::pg::UserMock user;
std::unique_ptr< eedb::PgAuthTokens > sut;

View File

@ -8,16 +8,9 @@
class PgCategoriesTest : public DbTestBase< PgCategoriesTest > {
public:
PgCategoriesTest() {
db().native()->start_transaction();
//sut = std::make_unique< eedb::PgCategories >(_categories);
}
~PgCategoriesTest() {
db().native()->rollback_transaction(false);
}
protected:
// std::vector< UniquePtrMockWrapper< eedb::db:: > > _categories;

View File

@ -9,16 +9,11 @@
class PgCategoriesRepositoryTest : public DbTestBase< PgCategoriesRepositoryTest > {
public:
PgCategoriesRepositoryTest() : user{db()} {
db().native()->start_transaction();
user._init();
user._expect_call_uid();
sut = std::make_unique< eedb::PgCategoriesRepository >(db(), &user);
}
~PgCategoriesRepositoryTest() {
db().native()->rollback_transaction(false);
}
protected:
eedb::db::pg::UserMock user;
std::unique_ptr< eedb::PgCategoriesRepository > sut;

View File

@ -7,7 +7,6 @@
class PgCategoryTest : public DbTestBase< PgCategoryTest > {
public:
PgCategoryTest() : user{db()} {
db().native()->start_transaction();
user._init();
user._expect_call_uid();
sut = std::make_unique< eedb::PgCategory >(db(), &user);
@ -19,10 +18,6 @@ class PgCategoryTest : public DbTestBase< PgCategoryTest > {
.returning(t_category.id));
}
~PgCategoryTest() {
db().native()->rollback_transaction(false);
}
protected:
eedb::db::pg::UserMock user;
std::unique_ptr< eedb::PgCategory > sut;

View File

@ -16,7 +16,6 @@ class PgItemsRepositoryTest : public DbTestBase< PgItemsRepositoryTest > {
public:
PgItemsRepositoryTest() : user{db()}, category{db(), &user}, numericParameter{db()}, textParameter{db()}, listParameter{db()} {
using namespace testing;
db().native()->start_transaction();
user._init(); // create fake user
user._expect_call_uid();
category._init_simple("main");
@ -42,10 +41,6 @@ class PgItemsRepositoryTest : public DbTestBase< PgItemsRepositoryTest > {
eedb::Value{std::vector{1.2, 3.4, 5.6, 7.8}, &listParameter});
}
~PgItemsRepositoryTest() {
db().native()->rollback_transaction(false);
}
protected:
eedb::db::pg::UserMock user;
eedb::db::pg::CategoryMock category;

View File

@ -8,16 +8,11 @@
class PgParametersRepositoryTest : public DbTestBase< PgParametersRepositoryTest > {
public:
PgParametersRepositoryTest() : user{db()}, parameter{db()} {
db().native()->start_transaction();
user._init();
parameter._init("paraemter");
parameter._init("parameter");
sut = std::make_unique< eedb::PgParametersRepository >(db());
}
~PgParametersRepositoryTest() {
db().native()->rollback_transaction(false);
}
protected:
eedb::db::pg::UserMock user;
eedb::db::pg::ParameterMock parameter;
@ -36,10 +31,10 @@ TEST_F(PgParametersRepositoryTest, searchForBadName){
EXPECT_FALSE(sut->search("bad_parameter"));
}
TEST_F(PgParametersRepositoryTest, init3){
TEST_F(PgParametersRepositoryTest, createWillNotThrow) {
// EXPECT_NO_THROW(sut->create("name", "description"));
}
TEST_F(PgParametersRepositoryTest, init4){
TEST_F(PgParametersRepositoryTest, createSameThrowsException) {
}

View File

@ -14,8 +14,6 @@
class PgUserTest : public DbTestBase< PgUserTest > {
public:
PgUserTest() {
db().native()->start_transaction();
auto info_ = std::make_unique< eedb::AuthInfoConst >(
eedb::Password{"bcrypt", "OM/Z1c4WBFXvwkxh", "$2y$07$RyytUhDhLDbAPjf0b0r2Y.dsg.FlQ7L.xzWHMmoelI81u0MfBrW7q"},
eedb::Email{"email@eedb.pl"});
@ -45,10 +43,6 @@ class PgUserTest : public DbTestBase< PgUserTest > {
sut = std::make_unique< eedb::PgUser >(db(), uid);
}
~PgUserTest() {
db().native()->rollback_transaction(false);
}
protected:
int64_t uid;
std::unique_ptr< eedb::PgUser > sut;

View File

@ -12,11 +12,6 @@ class PgUsersTest : public DbTestBase< PgUsersTest > {
public:
PgUsersTest() {
sut = std::make_unique< eedb::PgUsers >(db());
db().native()->start_transaction();
}
~PgUsersTest() {
db().native()->rollback_transaction(false);
}
auto createUser(std::string name, std::string email) {

View File

@ -10,6 +10,20 @@
namespace eedb {
class Parameter;
class Parameters;
namespace parameter {
enum class Type {
Numeric = 0x1 << 0,
NumericList,
NumericRange,
Text = 0x1 << 4,
TextList,
Script = 0x1 << 8
};
} // namespace parameter
class ParametersRepository {
public:
@ -18,9 +32,8 @@ class ParametersRepository {
/**
* @brief create
* @param name
* @return
*/
virtual std::unique_ptr< Parameter > create(std::string name, std::string description) const = 0;
virtual void create(std::string name, std::optional< Unit > unit, parameter::Type type) const = 0;
/**
* @brief search
@ -37,17 +50,6 @@ namespace details {
class Parameters : public details::ParameterPtrIteratorRange {};
enum class Type {
Numeric = 0x1 << 0,
NumericList,
NumericRange,
Text = 0x1 << 4,
TextList,
Script = 0x1 << 8
};
/**
* @brief The Parameter class
*/
@ -59,7 +61,7 @@ class Parameter {
* @brief type
* @return supported type od data
*/
Type type() const noexcept(true) {
parameter::Type type() const noexcept(true) {
return _type;
}
@ -82,6 +84,6 @@ class Parameter {
virtual std::string_view description() const = 0;
protected:
Type _type;
parameter::Type _type;
};
} // namespace eedb