当前位置:   article > 正文

C++ SQL ORM

C++ SQL ORM

 测试代码 

  1. //
  2. // Created by www on 2024/8/7.
  3. //
  4. #include "sqlitepp/database.h"
  5. #include "sqlitepp/condition.h"
  6. #include <iostream>
  7. using namespace sqlitepp;
  8. using namespace sqlitepp::literals;
  9. enum class test_enum {
  10. hello
  11. };
  12. void testCondition() {
  13. std::string s;
  14. {
  15. // ==
  16. auto q = "t"_c == test_enum::hello;
  17. auto p = q.as_partial();
  18. std::cout << p.query << std::endl;
  19. std::holds_alternative<orm::db_integer_type>(p.params[0]);
  20. }
  21. {
  22. // !=
  23. auto q = "t"_c != test_enum::hello;
  24. auto p = q.as_partial();
  25. std::cout << p.query << std::endl;
  26. }
  27. {
  28. // NOT
  29. auto q = !("t"_c == "hello");
  30. auto p = q.as_partial();
  31. std::cout << p.query << std::endl;
  32. }
  33. {
  34. // is NULL
  35. auto q = "t"_c == nullptr;
  36. auto p = q.as_partial();
  37. std::cout << p.query << std::endl;
  38. }
  39. {
  40. // is NOT NULL
  41. auto q = "t"_c != nullptr;
  42. auto p = q.as_partial();
  43. std::cout << p.query << std::endl;
  44. }
  45. {
  46. // AND
  47. auto q = "t"_c == "hello" && "t2"_c == "test";
  48. auto p = q.as_partial();
  49. std::cout << p.query << std::endl;
  50. }
  51. {
  52. // OR
  53. auto q = "t"_c == "hello" || "t2"_c == "test";
  54. auto p = q.as_partial();
  55. std::cout << p.query << std::endl;
  56. }
  57. { // BETWEEN
  58. auto q = "t"_c.between(static_cast<int64_t>(10),static_cast<int64_t>(20));
  59. auto p = q.as_partial();
  60. std::cout << p.query << std::endl;
  61. }
  62. { // NOT BETWEEN
  63. auto q = "t"_c.not_between(static_cast<int64_t>(10),static_cast<int64_t>(20));
  64. auto p = q.as_partial();
  65. std::cout << p.query << std::endl;
  66. }
  67. {
  68. // LIKE
  69. auto q = "t"_c.like("test%");
  70. auto p = q.as_partial();
  71. std::cout << p.query << std::endl;
  72. }
  73. {
  74. // GLOB
  75. auto q = "t"_c.glob("test%");
  76. auto p = q.as_partial();
  77. std::cout << p.query << std::endl;
  78. }
  79. { // >
  80. auto q = "t"_c > static_cast<int64_t>(10);
  81. auto p = q.as_partial();
  82. std::cout << p.query << std::endl;
  83. }
  84. { // >
  85. auto q = "t"_c >= static_cast<int64_t>(10);
  86. auto p = q.as_partial();
  87. std::cout << p.query << std::endl;
  88. }
  89. { // >
  90. auto q = "t"_c < static_cast<int64_t>(10);
  91. auto p = q.as_partial();
  92. std::cout << p.query << std::endl;
  93. }
  94. { // >
  95. auto q = "t"_c <= static_cast<int64_t>(10);
  96. auto p = q.as_partial();
  97. std::cout << p.query << std::endl;
  98. }
  99. }
  100. #include "include/sqlitepp/database.h"
  101. #include "include/sqlitepp/orm.h"
  102. using namespace sqlitepp;
  103. using namespace sqlitepp::orm;
  104. struct my_entity : orm::entity {
  105. enum class e_test {
  106. hello = 10,
  107. hello2 = 20
  108. };
  109. e_test t {e_test::hello};
  110. std::optional<int64_t> test_optional {};
  111. using entity::entity;
  112. static const orm::class_info _class_info;
  113. const orm::class_info& get_class_info() const noexcept override { return _class_info; }
  114. };
  115. const orm::class_info my_entity::_class_info = orm::builder<my_entity>("e")
  116. .field("t", &my_entity::t)
  117. .field("test_optional", &my_entity::test_optional)
  118. .build();
  119. void testORM() {
  120. { // ReadWriteEnum
  121. database db;
  122. my_entity e(db);
  123. auto& info = e.get_class_info();
  124. auto* field = info.get_field_by_name("t");
  125. auto val = field->getter(&e);
  126. std::cout << std::get<db_integer_type>(val) << std::endl;
  127. e.t = my_entity::e_test::hello2;
  128. val = field->getter(&e);
  129. std::cout << std::get<db_integer_type>(val) << std::endl;
  130. val = db_integer_type{10};
  131. field->setter(&e, val);
  132. std::cout << (int)e.t << std::endl;
  133. }
  134. { // ReadWriteOptional
  135. database db;
  136. my_entity e(db);
  137. auto& info = e.get_class_info();
  138. auto* field = info.get_field_by_name("test_optional");
  139. auto val = field->getter(&e);
  140. e.test_optional = 100;
  141. val = field->getter(&e);
  142. std::cout << std::get<db_integer_type>(val) << std::endl;
  143. val = db_integer_type{10};
  144. field->setter(&e, val);
  145. std::cout << e.test_optional.value() << std::endl;
  146. val = db_null_type{};
  147. field->setter(&e, val);
  148. std::cout << e.test_optional.has_value() << std::endl;
  149. }
  150. { // IsModified
  151. database db;
  152. my_entity e(db);
  153. auto& info = e.get_class_info();
  154. db.exec(generate_create_table(info));
  155. std::cout << (e.is_modified()) << std::endl;
  156. e.save();
  157. std::cout << (e.is_modified()) << std::endl;
  158. e.test_optional = 1337;
  159. std::cout << (e.is_modified()) << std::endl;
  160. e.save();
  161. std::cout << (e.is_modified()) << std::endl;
  162. e.test_optional.reset();
  163. std::cout << (e.is_modified()) << std::endl;
  164. e.reset();
  165. std::cout << (e.is_modified()) << std::endl;
  166. std::cout << e.test_optional.value() << std::endl;
  167. }
  168. }
  169. int main() {
  170. database db;
  171. testCondition();
  172. testORM();
  173. return 0;
  174. }
输出
  1. `t` = ?
  2. `t` <> ?
  3. NOT (`t` = ?)
  4. `t` IS NULL
  5. `t` IS NOT NULL
  6. (`t` = ?) AND (`t2` = ?)
  7. (`t` = ?) OR (`t2` = ?)
  8. `t` BETWEEN ? AND ?
  9. `t` NOT BETWEEN ? AND ?
  10. `t` LIKE ?
  11. `t` GLOB ?
  12. `t` > ?
  13. `t` >= ?
  14. `t` < ?
  15. `t` <= ?
  16. 10
  17. 20
  18. 10
  19. 100
  20. 10
  21. 0
  22. 1
  23. 0
  24. 1
  25. 0
  26. 1
  27. 0
  28. 1337
参考

https://github.com/Thalhammer/sqlitepp

我去年买了个表/sqlite3


创作不易,小小的支持一下吧!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/949365
推荐阅读
相关标签
  

闽ICP备14008679号