Skip to content

Commit

Permalink
Merge pull request #30 from BrainlessLabs/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
BrainlessLabs authored Mar 15, 2018
2 parents 1c2b874 + 372e39b commit 8025525
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
12 changes: 4 additions & 8 deletions include/blib/bun/DbBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@
#include "blib/bun/DbLogger.hpp"
#include <memory>

#define BUN_SQLITE

#ifdef BUN_SQLITE

#include <soci/sqlite3/soci-sqlite3.h>

#elif BUN_POSTGRES
#elif defined(BUN_POSTGRES)
#include <soci/postgresql/soci-postgresql.h>
#elif BUN_MYSQL
#elif defined(BUN_MYSQL)
#include <soci/mysql/soci-mysql.h>
#endif

Expand Down Expand Up @@ -57,9 +53,9 @@ namespace blib {
const auto backend_factory =
#ifdef BUN_SQLITE
soci::sqlite3;
#elif BUN_POSTGRES
#elif defined(BUN_POSTGRES)
soci::postgresql;
#elif BUN_MYSQL
#elif defined(BUN_MYSQL)
soci::mysql;
#endif
try {
Expand Down
34 changes: 24 additions & 10 deletions include/blib/bun/bun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
///////////////////////////////////////////////////////////////////////////////

/// @brief Make this 0 if no log is needed, else make it 1
#define QUERY_LOG_ON 1
#define QUERY_LOG_ON 0
/// @brief Log the query
#define QUERY_LOG(log_string) BOOST_PP_EXPR_IF(QUERY_LOG_ON, l().info(log_string))

Expand Down Expand Up @@ -107,14 +107,20 @@ namespace blib {
/// @brief Helper class to persist any primitive types.
/////////////////////////////////////////////////
template<typename T>
inline auto to_valid_query_string(T& val) -> T& {
return val;
inline auto to_valid_query_string(T const& val) -> T& {
T ret = val;
return ret;
}

inline auto to_valid_query_string(std::string const& val, std::string const sym = "\"") -> std::string {
const std::string ret_str = sym + val + sym;
return ret_str;
}

inline auto to_valid_query_string(const char* val, std::string const sym = "\"") -> std::string {
const std::string ret_str = sym + std::string(val) + sym;
return ret_str;
}
}
}
}
Expand Down Expand Up @@ -331,8 +337,16 @@ namespace blib {
inline static std::string const& create_table_sql() {
static const auto vecs = TypeMetaData<T>::tuple_type_pair();
static std::string sql;
const static std::string oid_high_type =
#if defined(BUN_SQLITE)
"BIGINT PRIMARY KEY AUTOINCREMENT";
#elif defined(BUN_POSTGRES)
"BIGSERIAL PRIMARY KEY";
#elif defined()
"BIGINT PRIMARY KEY AUTO_INCREMENT";
#endif
if (sql.empty()) {
sql = "CREATE TABLE IF NOT EXISTS '{}' (oid_high INTEGER PRIMARY KEY AUTOINCREMENT, oid_low INTEGER NOT NULL, oid_ref INTEGER";
sql = "CREATE TABLE IF NOT EXISTS \"{}\" (oid_high " + oid_high_type + ", oid_low BIGINT NOT NULL, oid_ref BIGINT";
boost::fusion::for_each(vecs, SqlString<T>::CreateTable(sql));
sql += ")";
}
Expand All @@ -345,7 +359,7 @@ namespace blib {
static const auto vecs = TypeMetaData<T>::tuple_type_pair();
static std::string sql;
if (sql.empty()) {
sql = "DROP TABLE '{}'";
sql = "DROP TABLE IF EXISTS \"{}\"";
}
return sql;
}
Expand All @@ -356,7 +370,7 @@ namespace blib {
static const auto vecs = TypeMetaData<T>::tuple_type_pair();
static std::string sql;
if (sql.empty()) {
sql = "DELETE FROM '{}' WHERE oid_high = :oid_high AND oid_low = :oid_low";
sql = "DELETE FROM \"{}\" WHERE oid_high = :oid_high AND oid_low = :oid_low";
}
return sql;
}
Expand All @@ -367,7 +381,7 @@ namespace blib {
static const auto vecs = TypeMetaData<T>::tuple_type_pair();
static std::string sql;
if (sql.empty()) {
sql = "INSERT INTO '{}' (oid_low";
sql = "INSERT INTO \"{}\" (oid_low";
boost::fusion::for_each(vecs, SqlString<T>::InsertRowNames(sql));
sql += ") VALUES ({}";
boost::fusion::for_each(vecs, SqlString<T>::InsertRowVal(sql));
Expand All @@ -382,7 +396,7 @@ namespace blib {
static const auto vecs = TypeMetaData<T>::tuple_type_pair();
static std::string sql;
if (sql.empty()) {
sql = "UPDATE '{}' SET ";
sql = "UPDATE \"{}\" SET ";
std::string sql1;
boost::fusion::for_each(vecs, SqlString<T>::UpdateRow(sql1));
sql += sql1 + " WHERE oid_high = {} AND oid_low = {}";
Expand All @@ -398,15 +412,15 @@ namespace blib {
if (sql.empty()) {
sql = "SELECT oid_high, oid_low";
boost::fusion::for_each(vecs, SqlString<T>::SelectRows(sql));
sql += " FROM '{}' ";
sql += " FROM \"{}\" ";
}
return sql;
}

/// @fn select_all_oid_sql
/// @brief Select Oids sql
inline static std::string const& select_all_oid_sql() {
static const std::string sql = "SELECT oid_high, oid_low FROM '{}'";
static const std::string sql = "SELECT oid_high, oid_low FROM \"{}\"";
return sql;
}
};
Expand Down
5 changes: 4 additions & 1 deletion src/Bun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ int main() {

// Connect the db. If the db is not there it will be created.
// It should include the whole path
bun::connect( "objects.db" );
// For SQLite
//bun::connect( "objects.db" );
// For PostGres
bun::connect("postgresql://localhost/postgres?user=postgres&password=postgres");
// Create the schema. We can create the schema multile times. If its already created
// it will be safely ignored
bun::createSchema<test::Person>();
Expand Down
8 changes: 4 additions & 4 deletions vs/bun/bun.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@
<Optimization>Disabled</Optimization>
<FunctionLevelLinking>false</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;BOOST_ALL_NO_LIB;FMT_HEADER_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)..\include;$(SolutionDir)..\include\third_party;$(SolutionDir)..\include\third_party\sqlite;$(BOOST_DIR);$(SOCI_DIR)\include</AdditionalIncludeDirectories>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;BOOST_ALL_NO_LIB;FMT_HEADER_ONLY;BUN_POSTGRES;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)..\include;$(SolutionDir)..\include\third_party;$(SolutionDir)..\include\third_party\sqlite;$(BOOST_DIR);$(SOCI_DIR)\include;$(POSTGRES)\include</AdditionalIncludeDirectories>
<WholeProgramOptimization>false</WholeProgramOptimization>
<EnablePREfast>true</EnablePREfast>
<BrowseInformation>false</BrowseInformation>
Expand All @@ -150,8 +150,8 @@
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(SOCI_DIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libsoci_core_4_0.lib;libsoci_sqlite3_4_0.lib;libsoci_empty_4_0.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(SOCI_DIR)\lib64;$(POSTGRES)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libsoci_core_4_0.lib;libsoci_sqlite3_4_0.lib;libsoci_empty_4_0.lib;libsoci_postgresql_4_0.lib;libpq.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down

0 comments on commit 8025525

Please sign in to comment.