xml_config.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. //stl
  3. #include <iostream>
  4. #include <sstream>
  5. #include <vector>
  6. //local
  7. #include "../xml/xml_context.hpp"
  8. /**
  9. * @brief 用于定义一个以 _Table 为模板参数的空结构体。
  10. * @tparam _Table 要用于结构体的类型。
  11. */
  12. template<typename _Table>
  13. struct _update_ {};
  14. /**
  15. * @brief 更新指定的 web_after_config_info 实例:先根据 id 删除已有项,再追加新的值并提交更改。
  16. */
  17. template<>
  18. struct _update_<web_after_config_info> {
  19. /**
  20. * @brief 更新或添加 web_after_config_info 配置信息到 XML 上下文中。
  21. * @param value 要更新或添加的 web_after_config_info 对象。
  22. */
  23. static void invoke(web_after_config_info const&value) {
  24. xml_context::from<web_after_config_info>().
  25. remove([value](auto it) {return it->id == value.id; }).
  26. submit();
  27. xml_context::from<web_after_config_info>().
  28. append(value).
  29. submit();
  30. }
  31. };
  32. /**
  33. * @brief 更新或添加 web_general_config_info 配置信息到 XML 上下文中。
  34. */
  35. template<>
  36. struct _update_<web_general_config_info> {
  37. /**
  38. * @brief 检查是否存在指定的 key,如果不存在则添加新的配置项,否则更新现有配置项。
  39. * @param value 要更新或添加的 web_general_config_info 对象。
  40. */
  41. static void invoke(web_general_config_info const& value) {
  42. if (xml_context::from<web_general_config_info>().where([value](auto it) {return it->key == value.key; }).empty()) {
  43. web_general_config_info info{
  44. .id = 0,
  45. .modify_time = v3::datetime::current_time_stamp(),
  46. .key = value.key,
  47. .value = value.value,
  48. .enable = value.enable,
  49. .remarks = value.remarks };
  50. xml_context::from<web_general_config_info>().
  51. max(info.id, [](auto it) {return it->id; }).
  52. inc(info.id).
  53. append(info).
  54. submit();
  55. }
  56. else {
  57. xml_context::from<web_general_config_info>().
  58. where([value](auto it)
  59. {
  60. return it->key == value.key;
  61. }).
  62. update([value](auto it)
  63. {
  64. it->modify_time = v3::datetime::current_time_stamp();
  65. it->key = value.key;
  66. it->value = value.value;
  67. it->enable = value.enable;
  68. it->remarks = value.remarks;
  69. return true;
  70. }).
  71. submit();
  72. }
  73. }
  74. };
  75. /**
  76. * @brief 用于更新指定表或表项的数据。
  77. */
  78. class xml_config {
  79. public:
  80. /**
  81. * @brief 更新指定表的数据。
  82. * @tparam _Table 要更新的数据表类型。
  83. * @param value 要更新的数据表对象的常量引用。
  84. */
  85. template<typename _Table>
  86. static void update(_Table const& value) {
  87. _update_<_Table>::invoke(value);
  88. }
  89. /**
  90. * @brief 使用给定的值集合更新表项。
  91. * @tparam _Table 表示表项类型的模板参数。
  92. * @param values 包含要更新的表项的常量引用向量。
  93. */
  94. template<typename _Table>
  95. static void update(std::vector<_Table> const& values) {
  96. for (auto const& value : values) {
  97. _update_<_Table>::invoke(value);
  98. }
  99. }
  100. };