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.

132 lines
3.8 KiB

2 years ago
  1. /**
  2. * This code is an implementation of this article https://www.tutorialspoint.com/sqlite/sqlite_insert_query.htm
  3. * with C++ specific features like creating object and inserting within
  4. * single line and separately, inserting a subclass object and inserting a vector.
  5. */
  6. #include <sqlite_orm/sqlite_orm.h>
  7. #include <string>
  8. #include <iostream>
  9. using std::cout;
  10. using std::endl;
  11. struct Employee {
  12. int id;
  13. std::string name;
  14. int age;
  15. std::string address;
  16. double salary;
  17. };
  18. struct DetailedEmployee : public Employee {
  19. std::string birthDate;
  20. };
  21. int main(int, char**) {
  22. using namespace sqlite_orm;
  23. auto storage = make_storage("insert.sqlite",
  24. make_table("COMPANY",
  25. make_column("ID", &Employee::id, primary_key()),
  26. make_column("NAME", &Employee::name),
  27. make_column("AGE", &Employee::age),
  28. make_column("ADDRESS", &Employee::address),
  29. make_column("SALARY", &Employee::salary)));
  30. storage.sync_schema();
  31. storage.remove_all<Employee>();
  32. Employee paul{
  33. -1,
  34. "Paul",
  35. 32,
  36. "California",
  37. 20000.00,
  38. };
  39. // insert returns inserted id
  40. paul.id = storage.insert(paul);
  41. storage.insert(Employee{
  42. -1,
  43. "Allen",
  44. 25,
  45. "Texas",
  46. 15000.00,
  47. });
  48. DetailedEmployee teddy;
  49. teddy.name = "Teddy";
  50. teddy.age = 23;
  51. teddy.address = "Norway";
  52. teddy.salary = 20000.00;
  53. // to insert subclass object as a superclass you have to specify type explicitly
  54. teddy.id = storage.insert<Employee>(teddy);
  55. std::vector<Employee> otherEmployees;
  56. otherEmployees.push_back(Employee{
  57. -1,
  58. "Mark",
  59. 25,
  60. "Rich-Mond",
  61. 65000.00,
  62. });
  63. otherEmployees.push_back(Employee{
  64. -1,
  65. "David",
  66. 27,
  67. "Texas",
  68. 85000.00,
  69. });
  70. otherEmployees.push_back(Employee{
  71. -1,
  72. "Kim",
  73. 22,
  74. "South-Hall",
  75. 45000.00,
  76. });
  77. // transaction is optional. It is used here to optimize sqlite usage - every insert opens
  78. // and closes database. So triple insert will open and close the db three times.
  79. // Transaction openes and closes the db only once.
  80. storage.transaction([&] {
  81. for(auto& employee: otherEmployees) {
  82. storage.insert(employee);
  83. }
  84. return true; // commit
  85. });
  86. Employee james{
  87. -1,
  88. "James",
  89. 24,
  90. "Houston",
  91. 10000.00,
  92. };
  93. james.id = storage.insert(james);
  94. cout << "---------------------" << endl;
  95. for(auto& employee: storage.iterate<Employee>()) {
  96. cout << storage.dump(employee) << endl;
  97. }
  98. // INSERT INTO COMPANY(ID, NAME, AGE, ADDRESS, SALARY)
  99. // VALUES (3, 'Sofia', 26, 'Madrid', 15000.0)
  100. // (4, 'Doja', 26, 'LA', 25000.0)
  101. // ON CONFLICT(ID) DO UPDATE SET NAME = excluded.NAME,
  102. // AGE = excluded.AGE,
  103. // ADDRESS = excluded.ADDRESS,
  104. // SALARY = excluded.SALARY
  105. storage.insert(
  106. into<Employee>(),
  107. columns(&Employee::id, &Employee::name, &Employee::age, &Employee::address, &Employee::salary),
  108. values(std::make_tuple(3, "Sofia", 26, "Madrid", 15000.0), std::make_tuple(4, "Doja", 26, "LA", 25000.0)),
  109. on_conflict(&Employee::id)
  110. .do_update(set(c(&Employee::name) = excluded(&Employee::name),
  111. c(&Employee::age) = excluded(&Employee::age),
  112. c(&Employee::address) = excluded(&Employee::address),
  113. c(&Employee::salary) = excluded(&Employee::salary))));
  114. return 0;
  115. }