added sqlite connection class (progress)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
add_subdirectory(sqlite)
|
||||
@@ -0,0 +1,17 @@
|
||||
set(HEADER
|
||||
include/sqlite_connection.hpp
|
||||
include/sqlite_error.hpp
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
src/sqlite_connection.cpp
|
||||
src/sqlite_error.cpp
|
||||
)
|
||||
|
||||
add_library(matador-sqlite SHARED ${SOURCES} ${HEADER})
|
||||
target_include_directories(matador-sqlite PRIVATE
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
${PROJECT_SOURCE_DIR}/backends/sqlite/include
|
||||
${SQLite3_INCLUDE_DIRS})
|
||||
|
||||
target_link_libraries(matador-sqlite matador ${SQLite3_LIBRARIES})
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef QUERY_SQLITE_CONNECTION_HPP
|
||||
#define QUERY_SQLITE_CONNECTION_HPP
|
||||
|
||||
#include "matador/sql/connection_impl.hpp"
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
namespace matador::backends::sqlite {
|
||||
|
||||
class sqlite_connection : public matador::sql::connection_impl
|
||||
{
|
||||
public:
|
||||
explicit sqlite_connection(sql::connection_info info);
|
||||
void open() override;
|
||||
void close() override;
|
||||
bool is_open() override;
|
||||
|
||||
void execute(const std::string &stmt) override;
|
||||
void prepare(const std::string &stmt) override;
|
||||
|
||||
private:
|
||||
sqlite3 *sqlite_db_{};
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_SQLITE_CONNECTION_HPP
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef QUERY_SQLITE_ERROR_HPP
|
||||
#define QUERY_SQLITE_ERROR_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
struct sqlite3;
|
||||
|
||||
namespace matador::backends::sqlite {
|
||||
|
||||
void throw_sqlite_error(int ec, sqlite3 *db, const std::string &source);
|
||||
void throw_sqlite_error(int ec, sqlite3 *db, const std::string &source, const std::string &sql);
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_SQLITE_ERROR_HPP
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "sqlite_connection.hpp"
|
||||
#include "sqlite_error.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace matador::backends::sqlite {
|
||||
|
||||
sqlite_connection::sqlite_connection(sql::connection_info info)
|
||||
: connection_impl(std::move(info)) {
|
||||
|
||||
}
|
||||
|
||||
void sqlite_connection::open()
|
||||
{
|
||||
const auto ret = sqlite3_open(info().database.c_str(), &sqlite_db_);
|
||||
|
||||
if (ret != SQLITE_OK) {
|
||||
throw_sqlite_error(ret, sqlite_db_, "open");
|
||||
}
|
||||
}
|
||||
|
||||
void sqlite_connection::close()
|
||||
{
|
||||
int ret = sqlite3_close(sqlite_db_);
|
||||
|
||||
throw_sqlite_error(ret, sqlite_db_, "close");
|
||||
|
||||
sqlite_db_ = nullptr;
|
||||
}
|
||||
|
||||
bool sqlite_connection::is_open()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void sqlite_connection::execute(const std::string &stmt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void sqlite_connection::prepare(const std::string &stmt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "sqlite_error.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
namespace matador::backends::sqlite {
|
||||
|
||||
void throw_sqlite_error(int ec, sqlite3 *db, const std::string &source)
|
||||
{
|
||||
if (ec != SQLITE_OK) {
|
||||
std::stringstream msg;
|
||||
msg << "sqlite error (" << source << "): " << sqlite3_errmsg(db);
|
||||
throw std::logic_error(msg.str());
|
||||
}
|
||||
}
|
||||
|
||||
void throw_sqlite_error(int ec, sqlite3 *db, const std::string &source, const std::string &sql)
|
||||
{
|
||||
if (ec != SQLITE_OK) {
|
||||
std::stringstream msg;
|
||||
msg << "sqlite error (" << source << ", sql: " << sql << "): " << sqlite3_errmsg(db);
|
||||
throw std::logic_error(msg.str());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user