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.

171 lines
6.2 KiB

2 years ago
  1. /****
  2. Implemented example from here https://www.tutorialspoint.com/sqlite/sqlite_select_query.htm.
  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. using std::make_unique;
  10. using std::string;
  11. void all_employees() {
  12. struct Employee {
  13. int id;
  14. std::string name;
  15. int age;
  16. std::unique_ptr<std::string> address; // optional
  17. std::unique_ptr<double> salary; // optional
  18. };
  19. auto storage = make_storage("select.sqlite",
  20. make_table("COMPANY",
  21. make_column("ID", &Employee::id, primary_key()),
  22. make_column("NAME", &Employee::name),
  23. make_column("AGE", &Employee::age),
  24. make_column("ADDRESS", &Employee::address),
  25. make_column("SALARY", &Employee::salary)));
  26. storage.sync_schema();
  27. storage.remove_all<Employee>(); // remove all old employees in case they exist in db..
  28. // create employees..
  29. Employee paul{-1, "Paul", 32, make_unique<string>("California"), make_unique<double>(20000.0)};
  30. Employee allen{-1, "Allen", 25, make_unique<string>("Texas"), make_unique<double>(15000.0)};
  31. Employee teddy{-1, "Teddy", 23, make_unique<string>("Norway"), make_unique<double>(20000.0)};
  32. Employee mark{-1, "Mark", 25, make_unique<string>("Rich-Mond"), make_unique<double>(65000.0)};
  33. Employee david{-1, "David", 27, make_unique<string>("Texas"), make_unique<double>(85000.0)};
  34. Employee kim{-1, "Kim", 22, make_unique<string>("South-Hall"), make_unique<double>(45000.0)};
  35. Employee james{-1, "James", 24, make_unique<string>("Houston"), make_unique<double>(10000.0)};
  36. // insert employees. `insert` function returns id of inserted object..
  37. paul.id = storage.insert(paul);
  38. allen.id = storage.insert(allen);
  39. teddy.id = storage.insert(teddy);
  40. mark.id = storage.insert(mark);
  41. david.id = storage.insert(david);
  42. kim.id = storage.insert(kim);
  43. james.id = storage.insert(james);
  44. // print users..
  45. cout << "paul = " << storage.dump(paul) << endl;
  46. cout << "allen = " << storage.dump(allen) << endl;
  47. cout << "teddy = " << storage.dump(teddy) << endl;
  48. cout << "mark = " << storage.dump(mark) << endl;
  49. cout << "david = " << storage.dump(david) << endl;
  50. cout << "kim = " << storage.dump(kim) << endl;
  51. cout << "james = " << storage.dump(james) << endl;
  52. // select all employees..
  53. auto allEmployees = storage.get_all<Employee>();
  54. cout << "allEmployees[0] = " << storage.dump(allEmployees[0]) << endl;
  55. cout << "allEmployees count = " << allEmployees.size() << endl;
  56. // now let's select id, name and salary..
  57. auto idsNamesSalarys = storage.select(columns(&Employee::id, &Employee::name, &Employee::salary));
  58. for(auto& row: idsNamesSalarys) { // row's type is tuple<int, string, unique_ptr<double>>
  59. cout << "id = " << get<0>(row) << ", name = " << get<1>(row) << ", salary = ";
  60. if(get<2>(row)) {
  61. cout << *get<2>(row);
  62. } else {
  63. cout << "null";
  64. }
  65. cout << endl;
  66. }
  67. cout << endl;
  68. auto allEmployeeTuples = storage.select(asterisk<Employee>());
  69. cout << "allEmployeeTuples count = " << allEmployeeTuples.size() << endl;
  70. for(auto& row: allEmployeeTuples) { // row's type is std::tuple<int, string, int, std::unique_ptr<string>,
  71. // std::unique_ptr<double>>
  72. cout << get<0>(row) << '\t' << get<1>(row) << '\t' << get<2>(row) << '\t';
  73. if(auto& value = get<3>(row)) {
  74. cout << *value;
  75. } else {
  76. cout << "null";
  77. }
  78. cout << '\t';
  79. if(auto& value = get<4>(row)) {
  80. cout << *value;
  81. } else {
  82. cout << "null";
  83. }
  84. cout << '\t' << endl;
  85. }
  86. cout << endl;
  87. auto allEmployeeObjects = storage.select(object<Employee>());
  88. cout << "allEmployeeObjects count = " << allEmployeeObjects.size() << endl;
  89. for(auto& employee: allEmployeeObjects) {
  90. cout << employee.id << '\t' << employee.name << '\t' << employee.age << '\t';
  91. if(auto& value = employee.address) {
  92. cout << *value;
  93. } else {
  94. cout << "null";
  95. }
  96. cout << '\t';
  97. if(auto& value = employee.salary) {
  98. cout << *value;
  99. } else {
  100. cout << "null";
  101. }
  102. cout << '\t' << endl;
  103. }
  104. cout << endl;
  105. }
  106. void all_artists() {
  107. struct Artist {
  108. int id;
  109. std::string name;
  110. };
  111. struct Album {
  112. int id;
  113. int artist_id;
  114. };
  115. auto storage = make_storage("",
  116. make_table("artists",
  117. make_column("id", &Artist::id, primary_key().autoincrement()),
  118. make_column("name", &Artist::name)),
  119. make_table("albums",
  120. make_column("id", &Album::id, primary_key().autoincrement()),
  121. make_column("artist_id", &Album::artist_id),
  122. foreign_key(&Album::artist_id).references(&Artist::id)));
  123. storage.sync_schema();
  124. storage.transaction([&storage] {
  125. auto artistPk = storage.insert(Artist{-1, "Artist"});
  126. storage.insert(Album{-1, artistPk});
  127. storage.insert(Album{-1, artistPk});
  128. return true;
  129. });
  130. // SELECT artists.*, albums.* FROM artists JOIN albums ON albums.artist_id = artist.id
  131. cout << "artists.*, albums.*\n";
  132. // row's type is std::tuple<int, std::string, id, int>
  133. for(auto& row: storage.select(columns(asterisk<Artist>(), asterisk<Album>()),
  134. join<Album>(on(c(&Album::artist_id) == &Artist::id)))) {
  135. cout << get<0>(row) << '\t' << get<1>(row) << '\t' << get<2>(row) << '\t' << get<3>(row) << '\n';
  136. }
  137. cout << endl;
  138. }
  139. int main() {
  140. try {
  141. all_employees();
  142. all_artists();
  143. } catch(const std::system_error& e) {
  144. cout << "[" << e.code() << "] " << e.what();
  145. }
  146. return 0;
  147. }