code: hpp,dbal,cpp (6 files)

This commit is contained in:
2025-12-26 00:51:52 +00:00
parent 675715da2e
commit 53c6faae4d
6 changed files with 261 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
/**
* @file create_lua_script.hpp
* @brief Create Lua script operation
*/
#ifndef DBAL_CREATE_LUA_SCRIPT_HPP
#define DBAL_CREATE_LUA_SCRIPT_HPP
#include "dbal/types.hpp"
#include "dbal/errors.hpp"
#include "../../store/in_memory_store.hpp"
#include "../../validation/lua_script_validation.hpp"
namespace dbal {
namespace entities {
namespace lua_script {
/**
* Create a new Lua script in the store
*/
inline Result<LuaScript> create(InMemoryStore& store, const CreateLuaScriptInput& input) {
if (input.name.empty() || input.name.length() > 100) {
return Error::validationError("Name must be between 1 and 100 characters");
}
if (input.code.empty()) {
return Error::validationError("Script code cannot be empty");
}
if (!validation::isValidLuaSyntax(input.code)) {
return Error::validationError("Invalid Lua syntax");
}
LuaScript script;
script.id = store.generateId("lua_script", ++store.lua_script_counter);
script.name = input.name;
script.description = input.description;
script.code = input.code;
script.category = input.category;
script.is_active = input.is_active;
script.created_at = std::chrono::system_clock::now();
script.updated_at = script.created_at;
store.lua_scripts[script.id] = script;
return Result<LuaScript>(script);
}
} // namespace lua_script
} // namespace entities
} // namespace dbal
#endif

View File

@@ -0,0 +1,38 @@
/**
* @file delete_lua_script.hpp
* @brief Delete Lua script operation
*/
#ifndef DBAL_DELETE_LUA_SCRIPT_HPP
#define DBAL_DELETE_LUA_SCRIPT_HPP
#include "dbal/types.hpp"
#include "dbal/errors.hpp"
#include "../../store/in_memory_store.hpp"
namespace dbal {
namespace entities {
namespace lua_script {
/**
* Delete a Lua script by ID
*/
inline Result<bool> remove(InMemoryStore& store, const std::string& id) {
if (id.empty()) {
return Error::validationError("Script ID cannot be empty");
}
auto it = store.lua_scripts.find(id);
if (it == store.lua_scripts.end()) {
return Error::notFound("Lua script not found: " + id);
}
store.lua_scripts.erase(it);
return Result<bool>(true);
}
} // namespace lua_script
} // namespace entities
} // namespace dbal
#endif

View File

@@ -0,0 +1,36 @@
/**
* @file get_lua_script.hpp
* @brief Get Lua script by ID operation
*/
#ifndef DBAL_GET_LUA_SCRIPT_HPP
#define DBAL_GET_LUA_SCRIPT_HPP
#include "dbal/types.hpp"
#include "dbal/errors.hpp"
#include "../../store/in_memory_store.hpp"
namespace dbal {
namespace entities {
namespace lua_script {
/**
* Get a Lua script by ID
*/
inline Result<LuaScript> get(InMemoryStore& store, const std::string& id) {
if (id.empty()) {
return Error::validationError("Script ID cannot be empty");
}
auto it = store.lua_scripts.find(id);
if (it == store.lua_scripts.end()) {
return Error::notFound("Lua script not found: " + id);
}
return Result<LuaScript>(it->second);
}
} // namespace lua_script
} // namespace entities
} // namespace dbal
#endif

View File

@@ -0,0 +1,14 @@
/**
* @file index.hpp
* @brief Barrel include for Lua script operations
*/
#ifndef DBAL_LUA_SCRIPT_INDEX_HPP
#define DBAL_LUA_SCRIPT_INDEX_HPP
#include "create_lua_script.hpp"
#include "get_lua_script.hpp"
#include "update_lua_script.hpp"
#include "delete_lua_script.hpp"
#include "list_lua_scripts.hpp"
#endif

View File

@@ -0,0 +1,62 @@
/**
* @file list_lua_scripts.hpp
* @brief List Lua scripts with filtering and pagination
*/
#ifndef DBAL_LIST_LUA_SCRIPTS_HPP
#define DBAL_LIST_LUA_SCRIPTS_HPP
#include "dbal/types.hpp"
#include "dbal/errors.hpp"
#include "../../store/in_memory_store.hpp"
#include <algorithm>
namespace dbal {
namespace entities {
namespace lua_script {
/**
* List Lua scripts with filtering and pagination
*/
inline Result<std::vector<LuaScript>> list(InMemoryStore& store, const ListOptions& options) {
std::vector<LuaScript> scripts;
for (const auto& [id, script] : store.lua_scripts) {
bool matches = true;
if (options.filter.find("is_active") != options.filter.end()) {
bool filter_active = options.filter.at("is_active") == "true";
if (script.is_active != filter_active) matches = false;
}
if (options.filter.find("category") != options.filter.end()) {
if (script.category != options.filter.at("category")) matches = false;
}
if (matches) scripts.push_back(script);
}
if (options.sort.find("name") != options.sort.end()) {
std::sort(scripts.begin(), scripts.end(), [](const LuaScript& a, const LuaScript& b) {
return a.name < b.name;
});
} else if (options.sort.find("created_at") != options.sort.end()) {
std::sort(scripts.begin(), scripts.end(), [](const LuaScript& a, const LuaScript& b) {
return a.created_at < b.created_at;
});
}
int start = (options.page - 1) * options.limit;
int end = std::min(start + options.limit, static_cast<int>(scripts.size()));
if (start < static_cast<int>(scripts.size())) {
return Result<std::vector<LuaScript>>(std::vector<LuaScript>(scripts.begin() + start, scripts.begin() + end));
}
return Result<std::vector<LuaScript>>(std::vector<LuaScript>());
}
} // namespace lua_script
} // namespace entities
} // namespace dbal
#endif

View File

@@ -0,0 +1,61 @@
/**
* @file update_lua_script.hpp
* @brief Update Lua script operation
*/
#ifndef DBAL_UPDATE_LUA_SCRIPT_HPP
#define DBAL_UPDATE_LUA_SCRIPT_HPP
#include "dbal/types.hpp"
#include "dbal/errors.hpp"
#include "../../store/in_memory_store.hpp"
#include "../../validation/lua_script_validation.hpp"
namespace dbal {
namespace entities {
namespace lua_script {
/**
* Update an existing Lua script
*/
inline Result<LuaScript> update(InMemoryStore& store, const std::string& id, const UpdateLuaScriptInput& input) {
if (id.empty()) {
return Error::validationError("Script ID cannot be empty");
}
auto it = store.lua_scripts.find(id);
if (it == store.lua_scripts.end()) {
return Error::notFound("Lua script not found: " + id);
}
LuaScript& script = it->second;
if (input.name.has_value()) {
if (input.name.value().empty() || input.name.value().length() > 100) {
return Error::validationError("Name must be between 1 and 100 characters");
}
script.name = input.name.value();
}
if (input.code.has_value()) {
if (input.code.value().empty()) {
return Error::validationError("Script code cannot be empty");
}
if (!validation::isValidLuaSyntax(input.code.value())) {
return Error::validationError("Invalid Lua syntax");
}
script.code = input.code.value();
}
if (input.description.has_value()) script.description = input.description.value();
if (input.category.has_value()) script.category = input.category.value();
if (input.is_active.has_value()) script.is_active = input.is_active.value();
script.updated_at = std::chrono::system_clock::now();
return Result<LuaScript>(script);
}
} // namespace lua_script
} // namespace entities
} // namespace dbal
#endif