sqlite3 mini-orm work
This commit is contained in:
99
repertory/librepertory/include/database/db_delete.hpp
Normal file
99
repertory/librepertory/include/database/db_delete.hpp
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
Copyright <2018-2024> <scott.e.graves@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#ifndef INCLUDE_DATABASE_DB_DELETE_HPP_
|
||||
#define INCLUDE_DATABASE_DB_DELETE_HPP_
|
||||
|
||||
#include "database/db_common.hpp"
|
||||
#include "utils/error_utils.hpp"
|
||||
|
||||
namespace repertory::db {
|
||||
class db_delete final {
|
||||
public:
|
||||
struct context final {
|
||||
context(sqlite3 &db3_, std::string table_name_)
|
||||
: db3(db3_), table_name(std::move(table_name_)) {}
|
||||
|
||||
sqlite3 &db3;
|
||||
std::string table_name;
|
||||
|
||||
std::vector<comp_data_t> ands{};
|
||||
db3_stmt_t stmt{nullptr};
|
||||
};
|
||||
|
||||
using row = db_row<context>;
|
||||
|
||||
public:
|
||||
db_delete(sqlite3 &db3, std::string table_name)
|
||||
: context_(std::make_shared<context>(db3, table_name)) {}
|
||||
|
||||
db_delete(std::shared_ptr<context> ctx) : context_(std::move(ctx)) {}
|
||||
|
||||
public:
|
||||
struct db_where final {
|
||||
db_where(std::shared_ptr<context> ctx, std::string column_name)
|
||||
: context_(std::move(ctx)), column_name_(std::move(column_name)) {}
|
||||
|
||||
public:
|
||||
struct db_where_next final {
|
||||
db_where_next(std::shared_ptr<context> ctx) : context_(std::move(ctx)) {}
|
||||
|
||||
private:
|
||||
std::shared_ptr<context> context_;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto and_where(std::string column_name) const -> db_where {
|
||||
return db_where{context_, column_name};
|
||||
}
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string {
|
||||
return db_delete{context_}.dump();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result<context> {
|
||||
return db_delete{context_}.go();
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
std::shared_ptr<context> context_;
|
||||
std::string column_name_;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto equals(db_types_t value) const -> db_where_next {
|
||||
context_->ands.emplace_back(comp_data_t{column_name_, "=", value});
|
||||
return db_where_next{context_};
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
std::shared_ptr<context> context_;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result<context>;
|
||||
|
||||
[[nodiscard]] auto where(std::string column_name) const -> db_where;
|
||||
};
|
||||
} // namespace repertory::db
|
||||
|
||||
#endif // INCLUDE_DATABASE_DB_DELETE_HPP_
|
@ -38,7 +38,6 @@ public:
|
||||
std::vector<comp_data_t> ands{};
|
||||
std::vector<std::string> columns{};
|
||||
std::map<std::string, std::string> count_columns{};
|
||||
bool delete_query{false};
|
||||
std::optional<std::int32_t> limit;
|
||||
std::optional<std::pair<std::string, bool>> order_by;
|
||||
db3_stmt_t stmt{nullptr};
|
||||
@ -81,8 +80,8 @@ public:
|
||||
return db_select{context_}.limit(value);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto order_by(std::string column_name, bool ascending) const
|
||||
-> db_select {
|
||||
[[nodiscard]] auto order_by(std::string column_name,
|
||||
bool ascending) const -> db_select {
|
||||
return db_select{context_}.order_by(column_name, ascending);
|
||||
}
|
||||
};
|
||||
@ -104,10 +103,8 @@ private:
|
||||
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 delete_query() -> db_select &;
|
||||
[[nodiscard]] auto count(std::string column_name,
|
||||
std::string as_column_name) -> db_select &;
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
@ -115,8 +112,8 @@ public:
|
||||
|
||||
[[nodiscard]] auto limit(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 -> db_where;
|
||||
};
|
||||
|
@ -36,6 +36,8 @@ public:
|
||||
|
||||
std::vector<comp_data_t> ands{};
|
||||
std::map<std::string, db_types_t> values{};
|
||||
std::optional<std::int32_t> limit;
|
||||
std::optional<std::pair<std::string, bool>> order_by;
|
||||
db3_stmt_t stmt{nullptr};
|
||||
};
|
||||
|
||||
@ -65,6 +67,15 @@ public:
|
||||
[[nodiscard]] auto go() const -> db_result<context> {
|
||||
return db_update{context_}.go();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto limit(std::int32_t value) const -> db_update {
|
||||
return db_update{context_}.limit(value);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto order_by(std::string column_name,
|
||||
bool ascending) const -> db_update {
|
||||
return db_update{context_}.order_by(column_name, ascending);
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
|
Reference in New Issue
Block a user