update sqlitecpp
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
484a505f2c
commit
5efc0a6e86
3 changed files with 152 additions and 69 deletions
|
|
@ -1,68 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
|
||||
#include <cppbase/result.hpp>
|
||||
|
||||
#include <ikarus/id.h>
|
||||
#include <ikarus/objects/object_type.h>
|
||||
|
||||
#include <persistence/function_context.hpp>
|
||||
|
||||
template<std::invocable<sqlitecpp::Connection *, IkarusId> F>
|
||||
[[nodiscard]] cppbase::Result<IkarusId, sqlitecpp::TransactionError> insert_object(
|
||||
IkarusProject * project, FunctionContext * ctx, IkarusObjectType type, std::string_view name, F insert_function
|
||||
) {
|
||||
char const * object_type_str = ikarus_object_type_to_string(type);
|
||||
|
||||
VTRY(
|
||||
auto const id,
|
||||
project->get_db()
|
||||
->transact([&](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
|
||||
LOG_VERBOSE("creating {} in objects table", object_type_str);
|
||||
|
||||
TRY(db->execute("INSERT INTO `objects` (`object_type`, `name`) VALUES(?, ?);", static_cast<int>(type), name));
|
||||
|
||||
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Blueprint);
|
||||
|
||||
LOG_DEBUG("{} is {}", object_type_str, id);
|
||||
|
||||
TRY(insert_function(db, id));
|
||||
|
||||
return cppbase::ok(id);
|
||||
})
|
||||
.on_error([&](auto const& err) {
|
||||
ctx->set_error(
|
||||
fmt::format("unable to insert {} into database: {}", object_type_str, err),
|
||||
true,
|
||||
IkarusErrorInfo_Source_SubSystem,
|
||||
IkarusErrorInfo_Type_SubSystem_Database
|
||||
);
|
||||
})
|
||||
);
|
||||
|
||||
LOG_VERBOSE("successfully created blueprint");
|
||||
|
||||
return cppbase::ok(id);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void delete_object(IkarusProject * project, FunctionContext * ctx, T * object) {
|
||||
auto id = object->id;
|
||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(id));
|
||||
|
||||
TRYRV(, project->get_db()->execute("DELETE FROM `objects` WHERE `id` = ?", id).on_error([&](auto const& err) {
|
||||
ctx->set_error(
|
||||
fmt::format("failed to delete {} from objects table: {}", object_type_str, err),
|
||||
true,
|
||||
IkarusErrorInfo_Source_SubSystem,
|
||||
IkarusErrorInfo_Type_SubSystem_Database
|
||||
);
|
||||
}));
|
||||
|
||||
LOG_VERBOSE("{} was successfully deleted from database, freeing", object_type_str);
|
||||
|
||||
project->uncache(object);
|
||||
|
||||
LOG_VERBOSE("successfully deleted {}", object_type_str);
|
||||
}
|
||||
151
src/objects/util.hpp
Normal file
151
src/objects/util.hpp
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
|
||||
#include <cppbase/result.hpp>
|
||||
|
||||
#include <ikarus/id.h>
|
||||
#include <ikarus/objects/object_type.h>
|
||||
|
||||
#include <persistence/function_context.hpp>
|
||||
|
||||
namespace util {
|
||||
template<std::invocable<sqlitecpp::Connection *, IkarusId> F>
|
||||
[[nodiscard]] cppbase::Result<IkarusId, sqlitecpp::TransactionError> insert_object(
|
||||
IkarusProject * project, FunctionContext * ctx, IkarusObjectType type, std::string_view name, F insert_function
|
||||
) {
|
||||
char const * object_type_str = ikarus_object_type_to_string(type);
|
||||
|
||||
VTRY(
|
||||
auto const id,
|
||||
project->get_db()
|
||||
->transact([&](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
|
||||
LOG_VERBOSE("creating {} in objects table", object_type_str);
|
||||
|
||||
TRY(db->execute("INSERT INTO `objects` (`object_type`, `name`) VALUES(?, ?);", static_cast<int>(type), name));
|
||||
|
||||
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Blueprint);
|
||||
|
||||
LOG_DEBUG("{} is {}", object_type_str, id);
|
||||
|
||||
TRY(insert_function(db, id));
|
||||
|
||||
return cppbase::ok(id);
|
||||
})
|
||||
.on_error([&](auto const& err) {
|
||||
ctx->set_error(
|
||||
fmt::format("unable to insert {} into database: {}", object_type_str, err),
|
||||
true,
|
||||
IkarusErrorInfo_Source_SubSystem,
|
||||
IkarusErrorInfo_Type_SubSystem_Database
|
||||
);
|
||||
})
|
||||
);
|
||||
|
||||
LOG_VERBOSE("successfully created blueprint");
|
||||
|
||||
return cppbase::ok(id);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void delete_object(IkarusProject * project, FunctionContext * ctx, T * object) {
|
||||
auto id = object->id;
|
||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(id));
|
||||
|
||||
TRYRV(, project->get_db()->execute("DELETE FROM `objects` WHERE `id` = ?", id).on_error([&](auto const& err) {
|
||||
ctx->set_error(
|
||||
fmt::format("failed to delete {} from objects table: {}", object_type_str, err),
|
||||
true,
|
||||
IkarusErrorInfo_Source_SubSystem,
|
||||
IkarusErrorInfo_Type_SubSystem_Database
|
||||
);
|
||||
}));
|
||||
|
||||
LOG_VERBOSE("{} was successfully deleted from database, freeing", object_type_str);
|
||||
|
||||
project->uncache(object);
|
||||
|
||||
LOG_VERBOSE("successfully deleted {}", object_type_str);
|
||||
}
|
||||
|
||||
struct SingleQueryData {
|
||||
std::string_view table_name;
|
||||
std::string_view select_field_name;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
cppbase::Result<T, sqlitecpp::SingleQueryError> fetch_single_field(IkarusObject * object, SingleQueryData const& query_data) {
|
||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
||||
|
||||
LOG_VERBOSE("fetching property default value");
|
||||
|
||||
LOG_VERBOSE("project={};property={}", object->project->get_path().c_str(), object->id);
|
||||
|
||||
auto * ctx = object->project->get_function_context();
|
||||
|
||||
VTRY(
|
||||
T value,
|
||||
object->project->get_db()
|
||||
->query_one<T>(
|
||||
fmt::format("SELECT `{}` FROM `{}` WHERE `id` = ?", query_data.select_field_name, query_data.table_name),
|
||||
object->id
|
||||
)
|
||||
.on_error([&](auto const& err) {
|
||||
ctx->set_error(
|
||||
fmt::format("failed to fetch {} {} from database: {}", object_type_str, query_data.select_field_name, err),
|
||||
true,
|
||||
IkarusErrorInfo_Source_SubSystem,
|
||||
IkarusErrorInfo_Type_SubSystem_Database
|
||||
);
|
||||
})
|
||||
);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
struct MultipleBufferQueryData {
|
||||
std::string_view table_name;
|
||||
std::string_view select_field_name;
|
||||
std::string_view where_field_name;
|
||||
};
|
||||
|
||||
template<typename T, typename O>
|
||||
cppbase::Result<void, sqlitecpp::MultiQueryError> fetch_multiple_buffered(
|
||||
IkarusObject * object,
|
||||
MultipleBufferQueryData const& query_data,
|
||||
std::string_view relation_desc,
|
||||
T * buffer,
|
||||
size_t buffer_size
|
||||
) {
|
||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
||||
|
||||
LOG_VERBOSE("fetching {} {}", object_type_str, relation_desc);
|
||||
|
||||
LOG_VERBOSE("project={};{}={}", object->project->get_path().c_str(), object_type_str, object->id);
|
||||
|
||||
auto * ctx = object->project->get_function_context();
|
||||
|
||||
TRY(object->project->get_db()
|
||||
->query_many_buffered<T>(
|
||||
fmt::format(
|
||||
"SELECT `{}` FROM `{}` WHERE `{}` = ?",
|
||||
query_data.select_field_name,
|
||||
query_data.table_name,
|
||||
query_data.where_field_name
|
||||
),
|
||||
buffer,
|
||||
buffer_size,
|
||||
object->id
|
||||
)
|
||||
.on_error([&](auto const& err) {
|
||||
ctx->set_error(
|
||||
fmt::format("failed to fetch {} {} from database: {}", object_type_str, relation_desc, err),
|
||||
true,
|
||||
IkarusErrorInfo_Source_SubSystem,
|
||||
IkarusErrorInfo_Type_SubSystem_Database
|
||||
);
|
||||
}));
|
||||
|
||||
return cppbase::ok();
|
||||
}
|
||||
}
|
||||
2
vendor/sqlitecpp
vendored
2
vendor/sqlitecpp
vendored
|
|
@ -1 +1 @@
|
|||
Subproject commit 6e39dd241f8469784b8a356e7a9563fa58b0e2c4
|
||||
Subproject commit 2a93b8b1a8be03a9a9c4c72956b1111299de0ddd
|
||||
Loading…
Add table
Add a link
Reference in a new issue