123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #pragma once
- //stl
- #include <iostream>
- #include <sstream>
- #include <vector>
- //local
- #include "../xml/xml_context.hpp"
- /**
- * @brief 用于定义一个以 _Table 为模板参数的空结构体。
- * @tparam _Table 要用于结构体的类型。
- */
- template<typename _Table>
- struct _update_ {};
- /**
- * @brief 更新指定的 web_after_config_info 实例:先根据 id 删除已有项,再追加新的值并提交更改。
- */
- template<>
- struct _update_<web_after_config_info> {
- /**
- * @brief 更新或添加 web_after_config_info 配置信息到 XML 上下文中。
- * @param value 要更新或添加的 web_after_config_info 对象。
- */
- static void invoke(web_after_config_info const&value) {
- xml_context::from<web_after_config_info>().
- remove([value](auto it) {return it->id == value.id; }).
- submit();
-
- xml_context::from<web_after_config_info>().
- append(value).
- submit();
- }
- };
- /**
- * @brief 更新或添加 web_general_config_info 配置信息到 XML 上下文中。
- */
- template<>
- struct _update_<web_general_config_info> {
- /**
- * @brief 检查是否存在指定的 key,如果不存在则添加新的配置项,否则更新现有配置项。
- * @param value 要更新或添加的 web_general_config_info 对象。
- */
- static void invoke(web_general_config_info const& value) {
- if (xml_context::from<web_general_config_info>().where([value](auto it) {return it->key == value.key; }).empty()) {
- web_general_config_info info{
- .id = 0,
- .modify_time = v3::datetime::current_time_stamp(),
- .key = value.key,
- .value = value.value,
- .enable = value.enable,
- .remarks = value.remarks };
- xml_context::from<web_general_config_info>().
- max(info.id, [](auto it) {return it->id; }).
- inc(info.id).
- append(info).
- submit();
- }
- else {
- xml_context::from<web_general_config_info>().
- where([value](auto it)
- {
- return it->key == value.key;
- }).
- update([value](auto it)
- {
- it->modify_time = v3::datetime::current_time_stamp();
- it->key = value.key;
- it->value = value.value;
- it->enable = value.enable;
- it->remarks = value.remarks;
- return true;
- }).
- submit();
- }
- }
- };
- /**
- * @brief 用于更新指定表或表项的数据。
- */
- class xml_config {
- public:
- /**
- * @brief 更新指定表的数据。
- * @tparam _Table 要更新的数据表类型。
- * @param value 要更新的数据表对象的常量引用。
- */
- template<typename _Table>
- static void update(_Table const& value) {
- _update_<_Table>::invoke(value);
- }
- /**
- * @brief 使用给定的值集合更新表项。
- * @tparam _Table 表示表项类型的模板参数。
- * @param values 包含要更新的表项的常量引用向量。
- */
- template<typename _Table>
- static void update(std::vector<_Table> const& values) {
- for (auto const& value : values) {
- _update_<_Table>::invoke(value);
- }
- }
- };
|