This commit is contained in:
2024-10-17 18:26:24 -05:00
parent c9f9c2a24c
commit 4a530c4703
14 changed files with 292 additions and 255 deletions

View File

@ -31,11 +31,7 @@ using db_types_t = std::variant<std::int64_t, std::string>;
struct sqlite3_deleter {
void operator()(sqlite3 *db3) const {
if (db3 != nullptr) {
#if defined(_WIN32)
sqlite3_close(db3);
#else // !defined(_WIN32)
sqlite3_close_v2(db3);
#endif // defined(_WIN32)
}
}
};
@ -123,21 +119,21 @@ public:
template <typename ctx_t> class db_row final {
public:
db_row(std::shared_ptr<ctx_t> context) {
auto column_count = sqlite3_column_count(context->stmt.get());
db_row(std::shared_ptr<ctx_t> ctx) {
auto column_count = sqlite3_column_count(ctx->stmt.get());
for (std::int32_t col = 0; col < column_count; col++) {
std::string name{sqlite3_column_name(context->stmt.get(), col)};
auto column_type = sqlite3_column_type(context->stmt.get(), col);
std::string name{sqlite3_column_name(ctx->stmt.get(), col)};
auto column_type = sqlite3_column_type(ctx->stmt.get(), col);
db_types_t value;
switch (column_type) {
case SQLITE_INTEGER: {
value = sqlite3_column_int64(context->stmt.get(), col);
value = sqlite3_column_int64(ctx->stmt.get(), col);
} break;
case SQLITE_TEXT: {
const auto *text = reinterpret_cast<const char *>(
sqlite3_column_text(context->stmt.get(), col));
sqlite3_column_text(ctx->stmt.get(), col));
value = std::string(text == nullptr ? "" : text);
} break;
@ -180,15 +176,17 @@ public:
};
template <typename ctx_t> struct db_result final {
db_result(std::shared_ptr<ctx_t> context, std::int32_t res)
: context_(std::move(context)), res_(res) {
db_result(std::shared_ptr<ctx_t> ctx, std::int32_t res)
: ctx_(std::move(ctx)), res_(res) {
if (res == SQLITE_OK) {
set_res(sqlite3_step(context_->stmt.get()));
set_res(sqlite3_step(ctx_->stmt.get()));
}
}
~db_result() { ctx_->clear(); }
private:
std::shared_ptr<ctx_t> context_;
std::shared_ptr<ctx_t> ctx_;
mutable std::int32_t res_;
private:
@ -212,8 +210,8 @@ public:
return false;
}
row = db_row{context_};
set_res(sqlite3_step(context_->stmt.get()));
row = db_row{ctx_};
set_res(sqlite3_step(ctx_->stmt.get()));
return true;
}
@ -224,7 +222,7 @@ public:
return;
}
set_res(sqlite3_step(context_->stmt.get()));
set_res(sqlite3_step(ctx_->stmt.get()));
}
};
} // namespace repertory::utils::db::sqlite

View File

@ -46,26 +46,28 @@ public:
using wd_t = where_data_t<w_t>;
std::unique_ptr<wd_t> where_data;
void clear();
};
using row = db_row<context>;
public:
db_delete(sqlite3 &db3, std::string table_name)
: context_(std::make_shared<context>(&db3, table_name)) {}
: ctx_(std::make_shared<context>(&db3, table_name)) {}
db_delete(std::shared_ptr<context> ctx) : context_(std::move(ctx)) {}
db_delete(std::shared_ptr<context> ctx) : ctx_(std::move(ctx)) {}
private:
std::shared_ptr<context> context_;
std::shared_ptr<context> ctx_;
public:
[[nodiscard]] auto dump() const -> std::string;
[[nodiscard]] auto go() const -> db_result<context>;
[[nodiscard]] auto group(context::w_t::group_func_t func)
-> context::w_t::wn_t;
[[nodiscard]] auto
group(context::w_t::group_func_t func) -> context::w_t::wn_t;
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
};

View File

@ -34,22 +34,24 @@ public:
bool or_replace{false};
std::map<std::string, db_types_t> values;
void clear();
};
using row = db_row<context>;
public:
db_insert(sqlite3 &db3, std::string table_name)
: context_(std::make_shared<context>(&db3, table_name)) {}
: ctx_(std::make_shared<context>(&db3, table_name)) {}
db_insert(std::shared_ptr<context> ctx) : context_(std::move(ctx)) {}
db_insert(std::shared_ptr<context> ctx) : ctx_(std::move(ctx)) {}
private:
std::shared_ptr<context> context_;
std::shared_ptr<context> ctx_;
public:
[[nodiscard]] auto or_replace() -> db_insert & {
context_->or_replace = true;
ctx_->or_replace = true;
return *this;
}

View File

@ -44,8 +44,8 @@ public:
[[nodiscard]] auto offset(std::int32_t value) -> db_select_op_t;
[[nodiscard]] auto order_by(std::string column_name, bool ascending)
-> db_select_op_t;
[[nodiscard]] auto order_by(std::string column_name,
bool ascending) -> db_select_op_t;
};
context(sqlite3 *db3_, std::string table_name_)
@ -63,24 +63,26 @@ public:
std::optional<std::pair<std::string, bool>> order_by;
std::unique_ptr<wd_t> where_data;
void clear();
};
using row = db_row<context>;
public:
db_select(sqlite3 &db3, std::string table_name)
: context_(std::make_shared<context>(&db3, table_name)) {}
: ctx_(std::make_shared<context>(&db3, table_name)) {}
db_select(std::shared_ptr<context> ctx) : context_(std::move(ctx)) {}
db_select(std::shared_ptr<context> ctx) : ctx_(std::move(ctx)) {}
private:
std::shared_ptr<context> context_;
std::shared_ptr<context> ctx_;
public:
[[nodiscard]] auto column(std::string column_name) -> db_select &;
[[nodiscard]] auto count(std::string column_name, std::string as_column_name)
-> db_select &;
[[nodiscard]] auto count(std::string column_name,
std::string as_column_name) -> db_select &;
[[nodiscard]] auto dump() const -> std::string;
@ -88,15 +90,15 @@ public:
[[nodiscard]] auto group_by(std::string column_name) -> db_select &;
[[nodiscard]] auto group(context::w_t::group_func_t func)
-> context::w_t::wn_t;
[[nodiscard]] auto
group(context::w_t::group_func_t func) -> context::w_t::wn_t;
[[nodiscard]] auto limit(std::int32_t value) -> db_select &;
[[nodiscard]] auto offset(std::int32_t value) -> db_select &;
[[nodiscard]] auto order_by(std::string column_name, bool ascending)
-> db_select &;
[[nodiscard]] auto order_by(std::string column_name,
bool ascending) -> db_select &;
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
};

View File

@ -43,8 +43,8 @@ public:
[[nodiscard]] auto limit(std::int32_t value) -> db_update_op_t;
[[nodiscard]] auto order_by(std::string column_name, bool ascending)
-> db_update_op_t;
[[nodiscard]] auto order_by(std::string column_name,
bool ascending) -> db_update_op_t;
};
using w_t = db_where_t<context, db_update_op_t>;
@ -55,34 +55,36 @@ public:
std::optional<std::pair<std::string, bool>> order_by;
std::unique_ptr<wd_t> where_data;
void clear();
};
using row = db_row<context>;
public:
db_update(sqlite3 &db3, std::string table_name)
: context_(std::make_shared<context>(&db3, table_name)) {}
: ctx_(std::make_shared<context>(&db3, table_name)) {}
db_update(std::shared_ptr<context> ctx) : context_(std::move(ctx)) {}
db_update(std::shared_ptr<context> ctx) : ctx_(std::move(ctx)) {}
private:
std::shared_ptr<context> context_;
std::shared_ptr<context> ctx_;
public:
[[nodiscard]] auto column_value(std::string column_name, db_types_t value)
-> db_update &;
[[nodiscard]] auto column_value(std::string column_name,
db_types_t value) -> db_update &;
[[nodiscard]] auto dump() const -> std::string;
[[nodiscard]] auto go() const -> db_result<context>;
[[nodiscard]] auto group(context::w_t::group_func_t func)
-> context::w_t::wn_t;
[[nodiscard]] auto
group(context::w_t::group_func_t func) -> context::w_t::wn_t;
[[nodiscard]] auto limit(std::int32_t value) -> db_update &;
[[nodiscard]] auto order_by(std::string column_name, bool ascending)
-> db_update &;
[[nodiscard]] auto order_by(std::string column_name,
bool ascending) -> db_update &;
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
};

View File

@ -157,8 +157,8 @@ template <typename ctx_t, typename op_t> struct db_where_t final {
using action_t = std::variant<db_comp_data_t, n_t, db_where_t>;
[[nodiscard]] static auto dump(std::int32_t &idx, auto &&actions)
-> std::string {
[[nodiscard]] static auto dump(std::int32_t &idx,
auto &&actions) -> std::string {
std::stringstream stream;
for (auto &&action : actions) {