added sqlite connection class (progress)
This commit is contained in:
+13
-7
@@ -5,10 +5,11 @@ set(SQL_SOURCES
|
||||
sql/key_value_pair.cpp
|
||||
sql/basic_condition.cpp
|
||||
sql/connection.cpp
|
||||
sql/connection_intermediates.cpp
|
||||
sql/query_intermediates.cpp
|
||||
sql/record.cpp
|
||||
sql/connection_info.cpp
|
||||
sql/connection_impl.cpp)
|
||||
sql/connection_impl.cpp
|
||||
sql/session.cpp)
|
||||
|
||||
set(SQL_HEADER
|
||||
../include/matador/sql/dialect.hpp
|
||||
@@ -19,22 +20,27 @@ set(SQL_HEADER
|
||||
../include/matador/sql/basic_condition.hpp
|
||||
../include/matador/sql/condition.hpp
|
||||
../include/matador/sql/connection.hpp
|
||||
../include/matador/sql/connection_intermediates.hpp
|
||||
../include/matador/sql/query_intermediates.hpp
|
||||
../include/matador/sql/result.hpp
|
||||
../include/matador/sql/record.hpp
|
||||
../include/matador/sql/query_result.hpp
|
||||
../include/matador/sql/connection_impl.hpp
|
||||
../include/matador/sql/connection_info.hpp)
|
||||
../include/matador/sql/connection_info.hpp
|
||||
../include/matador/sql/connection_pool.hpp
|
||||
../include/matador/sql/session.hpp)
|
||||
|
||||
set(UTILS_HEADER
|
||||
../include/matador/utils/field_attributes.hpp
|
||||
../include/matador/utils/string.hpp
|
||||
../include/matador/utils/constraints.hpp)
|
||||
../include/matador/utils/constraints.hpp
|
||||
../include/matador/utils/library.hpp)
|
||||
|
||||
set(UTILS_SOURCES
|
||||
utils/field_attributes.cpp
|
||||
utils/string.cpp
|
||||
sql/condition.cpp)
|
||||
sql/condition.cpp
|
||||
utils/library.cpp)
|
||||
|
||||
add_library(matador STATIC ${SQL_SOURCES} ${SQL_HEADER} ${UTILS_SOURCES} ${UTILS_HEADER})
|
||||
set_target_properties(matador PROPERTIES LINKER_LANGUAGE CXX)
|
||||
target_include_directories(matador PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
||||
#set_target_properties(matador PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
+12
-21
@@ -1,39 +1,30 @@
|
||||
#include <utility>
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
connection::connection(std::string dns)
|
||||
: dns_(std::move(dns)) {}
|
||||
|
||||
connection::connection()
|
||||
: query_(dialect_) {}
|
||||
|
||||
query_create_intermediate connection::create()
|
||||
void connection::open()
|
||||
{
|
||||
return query_create_intermediate{*this, query_.create()};
|
||||
is_open_ = true;
|
||||
}
|
||||
|
||||
query_drop_intermediate connection::drop()
|
||||
void connection::close()
|
||||
{
|
||||
return query_drop_intermediate{*this, query_.drop()};
|
||||
is_open_ = false;
|
||||
}
|
||||
|
||||
query_select_intermediate connection::select(std::initializer_list<std::string> column_names)
|
||||
bool connection::is_open() const
|
||||
{
|
||||
return query_select_intermediate{*this, query_.select(column_names)};
|
||||
return is_open_;
|
||||
}
|
||||
|
||||
query_insert_intermediate connection::insert()
|
||||
const std::string &connection::dns() const
|
||||
{
|
||||
return query_insert_intermediate{*this, query_.insert()};
|
||||
}
|
||||
|
||||
query_update_intermediate connection::update(const std::string &table)
|
||||
{
|
||||
return query_update_intermediate{*this, query_.update(table)};
|
||||
}
|
||||
|
||||
query_delete_intermediate connection::remove()
|
||||
{
|
||||
return query_delete_intermediate{*this, query_.remove()};
|
||||
return dns_;
|
||||
}
|
||||
|
||||
query_result<record> connection::fetch(const std::string &sql)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#include "matador/sql/connection_intermediates.hpp"
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/query_intermediates.hpp"
|
||||
#include "matador/sql/session.hpp"
|
||||
#include "matador/sql/query_builder.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
connection &query_intermediate::db()
|
||||
session &query_intermediate::db()
|
||||
{
|
||||
return db_;
|
||||
}
|
||||
@@ -23,7 +23,7 @@ record query_select_finish::fetch_one()
|
||||
return db().fetch(query().compile()).begin();
|
||||
}
|
||||
|
||||
query_intermediate::query_intermediate(connection &db, query_builder &query)
|
||||
query_intermediate::query_intermediate(session &db, query_builder &query)
|
||||
: db_(db), query_(query) {}
|
||||
|
||||
query_offset_intermediate query_order_direction_intermediate::offset(size_t offset)
|
||||
@@ -0,0 +1,56 @@
|
||||
#include "matador/sql/session.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
session::session(connection_pool<connection> &pool)
|
||||
: pool_(pool)
|
||||
, query_(dialect_) {}
|
||||
|
||||
query_create_intermediate session::create()
|
||||
{
|
||||
return query_create_intermediate{*this, query_.create()};
|
||||
}
|
||||
|
||||
query_drop_intermediate session::drop()
|
||||
{
|
||||
return query_drop_intermediate{*this, query_.drop()};
|
||||
}
|
||||
|
||||
query_select_intermediate session::select(std::initializer_list<std::string> column_names)
|
||||
{
|
||||
return query_select_intermediate{*this, query_.select(column_names)};
|
||||
}
|
||||
|
||||
query_insert_intermediate session::insert()
|
||||
{
|
||||
return query_insert_intermediate{*this, query_.insert()};
|
||||
}
|
||||
|
||||
query_update_intermediate session::update(const std::string &table)
|
||||
{
|
||||
return query_update_intermediate{*this, query_.update(table)};
|
||||
}
|
||||
|
||||
query_delete_intermediate session::remove()
|
||||
{
|
||||
return query_delete_intermediate{*this, query_.remove()};
|
||||
}
|
||||
|
||||
query_result<record> session::fetch(const std::string &sql) {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
}
|
||||
return c->fetch(sql);
|
||||
}
|
||||
|
||||
std::pair<size_t, std::string> session::execute(const std::string &sql) {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
}
|
||||
return c->execute(sql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#include "matador/utils/library.hpp"
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
library::library(std::string lib)
|
||||
: lib_(std::move(lib))
|
||||
{}
|
||||
|
||||
library::~library()
|
||||
{
|
||||
unload();
|
||||
}
|
||||
|
||||
bool library::load()
|
||||
{
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
handle_ = LoadLibrary((lib_ + ".dll").c_str());
|
||||
#elif defined(__APPLE__)
|
||||
handle_ = dlopen(std::string("lib" + lib_ + ".dylib").c_str(), RTLD_LAZY);
|
||||
#else
|
||||
handle_ = dlopen(std::string("./lib" + lib_ + ".so").c_str(), RTLD_LAZY);
|
||||
#endif
|
||||
if (!handle_) {
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
DWORD errorMessageID = ::GetLastError();
|
||||
#else
|
||||
// TODO: handle win32 and linux error
|
||||
fprintf(stdout, "dlopen error: %s", dlerror());
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool library::load(const std::string &lib)
|
||||
{
|
||||
if (handle_) {
|
||||
return false;
|
||||
}
|
||||
lib_ = lib;
|
||||
return load();
|
||||
}
|
||||
|
||||
bool library::unload()
|
||||
{
|
||||
bool ret(true);
|
||||
if (handle_) {
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
ret = FreeLibrary(handle_) == TRUE;
|
||||
#else
|
||||
ret = dlclose(handle_) == 0;
|
||||
#endif
|
||||
if (!ret) {
|
||||
} else {
|
||||
handle_ = nullptr;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
func_ptr library::function(const std::string &f) const
|
||||
{
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
return GetProcAddress(handle_, f.c_str());
|
||||
#else
|
||||
return dlsym(handle_, f.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user