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.

143 lines
3.3 KiB

2 years ago
  1. //
  2. // This example is implemented from here https://www.tutorialspoint.com/sqlite/sqlite_distinct_keyword.htm
  3. //
  4. #include <sqlite_orm/sqlite_orm.h>
  5. #include <iostream>
  6. using std::cout;
  7. using std::endl;
  8. struct Employee {
  9. int id;
  10. std::string name;
  11. int age;
  12. std::string address;
  13. double salary;
  14. };
  15. auto initStorage(const std::string& path) {
  16. using namespace sqlite_orm;
  17. return make_storage(path,
  18. make_table("COMPANY",
  19. make_column("ID", &Employee::id, primary_key()),
  20. make_column("NAME", &Employee::name),
  21. make_column("AGE", &Employee::age),
  22. make_column("ADDRESS", &Employee::address),
  23. make_column("SALARY", &Employee::salary)));
  24. }
  25. using Storage = decltype(initStorage(""));
  26. int main(int, char**) {
  27. Storage storage = initStorage("distinct.sqlite");
  28. // sync schema in case this is a very first launch
  29. storage.sync_schema();
  30. // remove all employees if it is not the very first launch
  31. storage.remove_all<Employee>();
  32. // now let's insert start values to wirk with. We'll make it within a transaction
  33. // for better perfomance
  34. storage.begin_transaction();
  35. storage.insert(Employee{
  36. -1,
  37. "Paul",
  38. 32,
  39. "California",
  40. 20000.0,
  41. });
  42. storage.insert(Employee{
  43. -1,
  44. "Allen",
  45. 25,
  46. "Texas",
  47. 15000.0,
  48. });
  49. storage.insert(Employee{
  50. -1,
  51. "Teddy",
  52. 23,
  53. "Norway",
  54. 20000.0,
  55. });
  56. storage.insert(Employee{
  57. -1,
  58. "Mark",
  59. 25,
  60. "Rich-Mond",
  61. 65000.0,
  62. });
  63. storage.insert(Employee{
  64. -1,
  65. "David",
  66. 27,
  67. "Texas",
  68. 85000.0,
  69. });
  70. storage.insert(Employee{
  71. -1,
  72. "Kim",
  73. 22,
  74. "South-Hall",
  75. 45000.0,
  76. });
  77. storage.insert(Employee{
  78. -1,
  79. "James",
  80. 24,
  81. "Houston",
  82. 10000.0,
  83. });
  84. storage.insert(Employee{
  85. -1,
  86. "Paul",
  87. 24,
  88. "Houston",
  89. 20000.0,
  90. });
  91. storage.insert(Employee{
  92. -1,
  93. "James",
  94. 44,
  95. "Norway",
  96. 5000.0,
  97. });
  98. storage.insert(Employee{
  99. -1,
  100. "James",
  101. 45,
  102. "Texas",
  103. 5000.0,
  104. });
  105. storage.commit();
  106. // SELECT 'NAME' FROM 'COMPANY'
  107. auto pureNames = storage.select(&Employee::name);
  108. cout << "NAME" << endl;
  109. cout << "----------" << endl;
  110. for(auto& name: pureNames) {
  111. cout << name << endl;
  112. }
  113. cout << endl;
  114. using namespace sqlite_orm;
  115. // SELECT DISTINCT 'NAME' FROM 'COMPANY'
  116. auto distinctNames = storage.select(distinct(&Employee::name));
  117. cout << "NAME" << endl;
  118. cout << "----------" << endl;
  119. for(auto& name: distinctNames) {
  120. cout << name << endl;
  121. }
  122. cout << endl;
  123. // SELECT DISTINCT 'ADDRESS', 'NAME' FROM 'COMPANY'
  124. auto severalColumns = storage.select(distinct(columns(&Employee::address, &Employee::name)));
  125. cout << "ADDRESS" << '\t' << "NAME" << endl;
  126. cout << "----------" << endl;
  127. for(auto& t: severalColumns) {
  128. cout << std::get<0>(t) << '\t' << std::get<1>(t) << endl;
  129. }
  130. return 0;
  131. }