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.

70 lines
2.3 KiB

2 years ago
  1. /**
  2. * Ths example is implemented from here https://www.tutorialspoint.com/sqlite/sqlite_having_clause.htm
  3. */
  4. #include <sqlite_orm/sqlite_orm.h>
  5. #include <string>
  6. #include <iostream>
  7. using std::cout;
  8. using std::endl;
  9. struct Employee {
  10. int id;
  11. std::string name;
  12. int age;
  13. std::string address;
  14. float salary;
  15. };
  16. int main() {
  17. using namespace sqlite_orm;
  18. auto storage = make_storage("having.sqlite",
  19. make_table("COMPANY",
  20. make_column("ID", &Employee::id, primary_key()),
  21. make_column("NAME", &Employee::name),
  22. make_column("AGE", &Employee::age),
  23. make_column("ADDRESS", &Employee::address),
  24. make_column("SALARY", &Employee::salary)));
  25. storage.sync_schema();
  26. storage.remove_all<Employee>();
  27. storage.replace(Employee{1, "Paul", 32, "California", 20000.0});
  28. storage.replace(Employee{2, "Allen", 25, "Texas", 15000.0});
  29. storage.replace(Employee{3, "Teddy", 23, "Norway", 20000.0});
  30. storage.replace(Employee{4, "Mark", 25, "Rich-Mond", 65000.0});
  31. storage.replace(Employee{5, "David", 27, "Texas", 85000.0});
  32. storage.replace(Employee{6, "Kim", 22, "South-Hall", 45000.0});
  33. storage.replace(Employee{7, "James", 24, "Houston", 10000.0});
  34. storage.replace(Employee{8, "Paul", 24, "Houston", 20000.0});
  35. storage.replace(Employee{9, "James", 44, "Norway", 5000.0});
  36. storage.replace(Employee{10, "James", 45, "Texas", 5000.0});
  37. {
  38. // SELECT *
  39. // FROM COMPANY
  40. // GROUP BY name
  41. // HAVING count(name) < 2;
  42. auto rows = storage.get_all<Employee>(group_by(&Employee::name).having(lesser_than(count(&Employee::name), 2)));
  43. for(auto& employee: rows) {
  44. cout << storage.dump(employee) << endl;
  45. }
  46. cout << endl;
  47. }
  48. {
  49. // SELECT *
  50. // FROM COMPANY
  51. // GROUP BY name
  52. // HAVING count(name) > 2;
  53. auto rows =
  54. storage.get_all<Employee>(group_by(&Employee::name).having(greater_than(count(&Employee::name), 2)));
  55. for(auto& employee: rows) {
  56. cout << storage.dump(employee) << endl;
  57. }
  58. cout << endl;
  59. }
  60. return 0;
  61. }