refactor
This commit is contained in:
@ -68,122 +68,72 @@ struct db_context_t {
|
||||
std::string table_name;
|
||||
};
|
||||
|
||||
class db_column final {
|
||||
public:
|
||||
db_column() noexcept = default;
|
||||
db_column(const db_column &) = default;
|
||||
db_column(db_column &&column) noexcept = default;
|
||||
~db_column() = default;
|
||||
|
||||
auto operator=(const db_column &) -> db_column & = default;
|
||||
auto operator=(db_column &&) -> db_column & = default;
|
||||
|
||||
db_column(std::int32_t index, std::string name, db_types_t value) noexcept
|
||||
: index_(index), name_(std::move(name)), value_(std::move(value)) {}
|
||||
|
||||
private:
|
||||
std::int32_t index_{};
|
||||
std::string name_;
|
||||
db_types_t value_;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto get_index() const -> std::int32_t { return index_; }
|
||||
|
||||
[[nodiscard]] auto get_name() const -> std::string { return name_; }
|
||||
|
||||
template <typename data_type>
|
||||
[[nodiscard]] auto get_value() const -> data_type {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
return std::visit(
|
||||
overloaded{
|
||||
[](const data_type &value) -> data_type { return value; },
|
||||
[](auto &&) -> data_type {
|
||||
throw utils::error::create_exception(
|
||||
function_name, {
|
||||
"data type not supported",
|
||||
});
|
||||
},
|
||||
},
|
||||
value_);
|
||||
}
|
||||
|
||||
#if defined(PROJECT_ENABLE_JSON)
|
||||
[[nodiscard]] auto get_value_as_json() const -> nlohmann::json;
|
||||
#endif // defined(PROJECT_ENABLE_JSON)
|
||||
};
|
||||
|
||||
template <typename ctx_t> class db_row final {
|
||||
public:
|
||||
db_row(std::shared_ptr<ctx_t> ctx) {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
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(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(ctx->stmt.get(), col);
|
||||
} break;
|
||||
|
||||
case SQLITE_TEXT: {
|
||||
const auto *text = reinterpret_cast<const char *>(
|
||||
sqlite3_column_text(ctx->stmt.get(), col));
|
||||
value = std::string(text == nullptr ? "" : text);
|
||||
} break;
|
||||
|
||||
default:
|
||||
throw utils::error::create_exception(function_name,
|
||||
{
|
||||
"column type not implemented",
|
||||
std::to_string(column_type),
|
||||
});
|
||||
}
|
||||
|
||||
columns_[name] = db_column{col, name, value};
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, db_column> columns_;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto get_columns() const -> std::vector<db_column> {
|
||||
std::vector<db_column> ret;
|
||||
for (const auto &item : columns_) {
|
||||
ret.push_back(item.second);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_column(std::int32_t index) const -> db_column {
|
||||
auto iter = std::find_if(columns_.begin(), columns_.end(),
|
||||
[&index](auto &&col) -> bool {
|
||||
return col.second.get_index() == index;
|
||||
});
|
||||
if (iter == columns_.end()) {
|
||||
throw std::out_of_range("");
|
||||
}
|
||||
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_column(std::string name) const -> db_column {
|
||||
return columns_.at(name);
|
||||
}
|
||||
};
|
||||
|
||||
struct db_result final {
|
||||
struct context final {
|
||||
db3_stmt_t stmt;
|
||||
};
|
||||
|
||||
using row = db_row<context>;
|
||||
class db_column final {
|
||||
public:
|
||||
db_column(std::int32_t index, std::string name, db_types_t value) noexcept;
|
||||
|
||||
db_result(sqlite3_stmt *stmt, std::int32_t res);
|
||||
db_column() noexcept = default;
|
||||
db_column(const db_column &) = default;
|
||||
db_column(db_column &&column) noexcept = default;
|
||||
|
||||
~db_column() = default;
|
||||
|
||||
auto operator=(const db_column &) -> db_column & = default;
|
||||
auto operator=(db_column &&) -> db_column & = default;
|
||||
|
||||
private:
|
||||
std::int32_t index_{};
|
||||
std::string name_;
|
||||
db_types_t value_;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto get_index() const -> std::int32_t { return index_; }
|
||||
|
||||
[[nodiscard]] auto get_name() const -> std::string { return name_; }
|
||||
|
||||
template <typename data_type>
|
||||
[[nodiscard]] auto get_value() const -> data_type {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
return std::visit(
|
||||
overloaded{
|
||||
[](const data_type &value) -> data_type { return value; },
|
||||
[](auto &&) -> data_type {
|
||||
throw utils::error::create_exception(
|
||||
function_name, {
|
||||
"data type not supported",
|
||||
});
|
||||
},
|
||||
},
|
||||
value_);
|
||||
}
|
||||
|
||||
#if defined(PROJECT_ENABLE_JSON)
|
||||
[[nodiscard]] auto get_value_as_json() const -> nlohmann::json;
|
||||
#endif // defined(PROJECT_ENABLE_JSON)
|
||||
};
|
||||
|
||||
class db_row final {
|
||||
public:
|
||||
db_row(std::shared_ptr<context> ctx);
|
||||
|
||||
private:
|
||||
std::map<std::string, db_column> columns_;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto get_columns() const -> std::vector<db_column>;
|
||||
|
||||
[[nodiscard]] auto get_column(std::int32_t index) const -> db_column;
|
||||
|
||||
[[nodiscard]] auto get_column(std::string name) const -> db_column;
|
||||
};
|
||||
|
||||
db_result(db3_stmt_t stmt, std::int32_t res);
|
||||
|
||||
db_result() = default;
|
||||
db_result(const db_result &) = default;
|
||||
@ -192,6 +142,8 @@ struct db_result final {
|
||||
auto operator=(const db_result &) -> db_result & = default;
|
||||
auto operator=(db_result &&) -> db_result & = default;
|
||||
|
||||
using row = db_row;
|
||||
|
||||
private:
|
||||
std::shared_ptr<context> ctx_;
|
||||
mutable std::int32_t res_{};
|
||||
|
@ -36,8 +36,6 @@ public:
|
||||
std::map<std::string, db_types_t> values;
|
||||
};
|
||||
|
||||
using row = db_row<context>;
|
||||
|
||||
public:
|
||||
db_insert(sqlite3 &db3, std::string table_name)
|
||||
: ctx_(std::make_shared<context>(&db3, table_name)) {}
|
||||
@ -48,15 +46,13 @@ private:
|
||||
std::shared_ptr<context> ctx_;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto or_replace() -> db_insert & {
|
||||
[[nodiscard]] auto or_replace() -> db_insert {
|
||||
ctx_->or_replace = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto column_value(std::string column_name, db_types_t value)
|
||||
|
||||
|
||||
-> db_insert &;
|
||||
-> db_insert;
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
|
@ -45,8 +45,6 @@ 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;
|
||||
};
|
||||
|
||||
@ -77,32 +75,26 @@ private:
|
||||
std::shared_ptr<context> ctx_;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto column(std::string column_name) -> db_select &;
|
||||
[[nodiscard]] auto column(std::string column_name) -> db_select;
|
||||
|
||||
[[nodiscard]] auto count(std::string column_name, std::string as_column_name)
|
||||
|
||||
|
||||
-> db_select &;
|
||||
-> db_select;
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result;
|
||||
|
||||
[[nodiscard]] auto group_by(std::string column_name) -> db_select &;
|
||||
|
||||
[[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 limit(std::int32_t value) -> db_select &;
|
||||
[[nodiscard]] auto limit(std::int32_t value) -> db_select;
|
||||
|
||||
[[nodiscard]] auto offset(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 &;
|
||||
-> db_select;
|
||||
|
||||
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
|
||||
};
|
||||
|
@ -69,22 +69,19 @@ private:
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto column_value(std::string column_name, db_types_t value)
|
||||
|
||||
-> db_update &;
|
||||
-> db_update;
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result;
|
||||
|
||||
[[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 limit(std::int32_t value) -> db_update;
|
||||
|
||||
[[nodiscard]] auto order_by(std::string column_name, bool ascending)
|
||||
|
||||
-> db_update &;
|
||||
-> db_update;
|
||||
|
||||
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
|
||||
};
|
||||
|
Reference in New Issue
Block a user