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.

75 lines
2.8 KiB

2 years ago
  1. #include <sqlite_orm/sqlite_orm.h>
  2. #include <string>
  3. #include <iostream>
  4. using namespace sqlite_orm;
  5. using std::cout;
  6. using std::endl;
  7. int main() {
  8. struct Contact {
  9. int id = 0;
  10. std::string firstName;
  11. std::string lastName;
  12. std::string email;
  13. std::string phone;
  14. #ifndef SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
  15. Contact() {}
  16. Contact(int id, std::string firstName, std::string lastName, std::string email, std::string phone) :
  17. id{id}, firstName{std::move(firstName)}, lastName{std::move(lastName)}, email{std::move(email)},
  18. phone{std::move(phone)} {}
  19. #endif
  20. };
  21. struct Product {
  22. int id = 0;
  23. std::string name;
  24. float listPrice = 0;
  25. float discount = 0;
  26. #ifndef SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
  27. Product() {}
  28. Product(int id, std::string name, float listPrice, float discount) :
  29. id{id}, name{std::move(name)}, listPrice{listPrice}, discount{discount} {}
  30. #endif
  31. };
  32. auto storage = make_storage(":memory:",
  33. make_table("contacts",
  34. make_column("contact_id", &Contact::id, primary_key()),
  35. make_column("first_name", &Contact::firstName),
  36. make_column("last_name", &Contact::lastName),
  37. make_column("email", &Contact::email),
  38. make_column("phone", &Contact::phone),
  39. check(length(&Contact::phone) >= 10)),
  40. make_table("products",
  41. make_column("product_id", &Product::id, primary_key()),
  42. make_column("product_name", &Product::name),
  43. make_column("list_price", &Product::listPrice),
  44. make_column("discount", &Product::discount, default_value(0)),
  45. check(c(&Product::listPrice) >= &Product::discount and
  46. c(&Product::discount) >= 0 and c(&Product::listPrice) >= 0)));
  47. storage.sync_schema();
  48. try {
  49. storage.insert(Contact{0, "John", "Doe", {}, "408123456"});
  50. } catch(const std::system_error& e) {
  51. cout << e.what() << endl;
  52. }
  53. storage.insert(Contact{0, "John", "Doe", {}, "(408)-123-456"});
  54. try {
  55. storage.insert(Product{0, "New Product", 900, 1000});
  56. } catch(const std::system_error& e) {
  57. cout << e.what() << endl;
  58. }
  59. try {
  60. storage.insert(Product{0, "New XFactor", 1000, -10});
  61. } catch(const std::system_error& e) {
  62. cout << e.what() << endl;
  63. }
  64. return 0;
  65. }