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.

42 lines
1.3 KiB

2 years ago
  1. #include <sqlite_orm/sqlite_orm.h>
  2. #include <string>
  3. #include <memory>
  4. #include <iostream>
  5. using std::cerr;
  6. using std::cout;
  7. using std::endl;
  8. struct Entry {
  9. int id;
  10. std::string uniqueColumn;
  11. std::unique_ptr<std::string> nullableColumn;
  12. };
  13. int main(int, char**) {
  14. using namespace sqlite_orm;
  15. auto storage = make_storage("unique.sqlite",
  16. make_table("unique_test",
  17. make_column("id", &Entry::id, primary_key().autoincrement()),
  18. make_column("unique_text", &Entry::uniqueColumn, unique()),
  19. make_column("nullable_text", &Entry::nullableColumn)));
  20. storage.sync_schema();
  21. storage.remove_all<Entry>();
  22. try {
  23. auto sameString = "Bebe Rexha";
  24. auto id1 = storage.insert(Entry{0, sameString, std::make_unique<std::string>("The way I are")});
  25. cout << "inserted " << storage.dump(storage.get<Entry>(id1)) << endl;
  26. // it's ok but the next line will throw std::system_error
  27. auto id2 = storage.insert(Entry{0, sameString, std::make_unique<std::string>("I got you")});
  28. cout << "inserted " << storage.dump(storage.get<Entry>(id2)) << endl;
  29. } catch(const std::system_error& e) {
  30. cerr << e.what() << endl;
  31. }
  32. return 0;
  33. }