You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.4 KiB

2 years ago
  1. /**
  2. * Got it from here https://www.tutlane.com/tutorial/sqlite/sqlite-case-statement
  3. */
  4. #include <sqlite_orm/sqlite_orm.h>
  5. #include <iostream>
  6. using namespace sqlite_orm;
  7. using std::cout;
  8. using std::endl;
  9. int main() {
  10. struct Student {
  11. int id = 0;
  12. std::string name;
  13. std::string email;
  14. float marks = 0;
  15. #ifndef SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
  16. Student() {}
  17. Student(int id, std::string name, std::string email, float marks) :
  18. id{id}, name{std::move(name)}, email{std::move(email)}, marks{marks} {}
  19. #endif
  20. };
  21. auto storage = make_storage({},
  22. make_table("STUDENT",
  23. make_column("ID", &Student::id, primary_key()),
  24. make_column("NAME", &Student::name),
  25. make_column("EMAIL", &Student::email),
  26. make_column("MARKS", &Student::marks)));
  27. storage.sync_schema();
  28. storage.transaction([&storage] {
  29. storage.replace(Student{1, "Shweta", "shweta@gmail.com", 80});
  30. storage.replace(Student{2, "Yamini", "rani@gmail.com", 60});
  31. storage.replace(Student{3, "Sonal", "sonal@gmail.com", 50});
  32. storage.replace(Student{4, "Jagruti", "jagu@gmail.com", 30});
  33. return true;
  34. });
  35. // list all students
  36. for(auto& student: storage.iterate<Student>()) {
  37. cout << storage.dump(student) << endl;
  38. }
  39. cout << endl;
  40. { // without alias
  41. // SELECT ID, NAME, MARKS,
  42. // CASE
  43. // WHEN MARKS >=80 THEN 'A+'
  44. // WHEN MARKS >=70 THEN 'A'
  45. // WHEN MARKS >=60 THEN 'B'
  46. // WHEN MARKS >=50 THEN 'C'
  47. // ELSE 'Sorry!! Failed'
  48. // END
  49. // FROM STUDENT;
  50. auto rows = storage.select(columns(&Student::id,
  51. &Student::name,
  52. &Student::marks,
  53. case_<std::string>()
  54. .when(greater_or_equal(&Student::marks, 80), then("A+"))
  55. .when(greater_or_equal(&Student::marks, 70), then("A"))
  56. .when(greater_or_equal(&Student::marks, 60), then("B"))
  57. .when(greater_or_equal(&Student::marks, 50), then("C"))
  58. .else_("Sorry!! Failed")
  59. .end()));
  60. for(auto& row: rows) {
  61. cout << std::get<0>(row) << ' ' << std::get<1>(row) << ' ' << std::get<2>(row) << ' ' << std::get<3>(row)
  62. << endl;
  63. }
  64. cout << endl;
  65. }
  66. { // with alias
  67. struct GradeAlias : alias_tag {
  68. static const std::string& get() {
  69. static const std::string res = "Grade";
  70. return res;
  71. }
  72. };
  73. // SELECT ID, NAME, MARKS,
  74. // CASE
  75. // WHEN MARKS >=80 THEN 'A+'
  76. // WHEN MARKS >=70 THEN 'A'
  77. // WHEN MARKS >=60 THEN 'B'
  78. // WHEN MARKS >=50 THEN 'C'
  79. // ELSE 'Sorry!! Failed'
  80. // END as 'Grade'
  81. // FROM STUDENT;
  82. auto rows = storage.select(columns(&Student::id,
  83. &Student::name,
  84. &Student::marks,
  85. as<GradeAlias>(case_<std::string>()
  86. .when(greater_or_equal(&Student::marks, 80), then("A+"))
  87. .when(greater_or_equal(&Student::marks, 70), then("A"))
  88. .when(greater_or_equal(&Student::marks, 60), then("B"))
  89. .when(greater_or_equal(&Student::marks, 50), then("C"))
  90. .else_("Sorry!! Failed")
  91. .end())));
  92. for(auto& row: rows) {
  93. cout << std::get<0>(row) << ' ' << std::get<1>(row) << ' ' << std::get<2>(row) << ' ' << std::get<3>(row)
  94. << endl;
  95. }
  96. cout << endl;
  97. }
  98. return 0;
  99. }