Skip to content

Instantly share code, notes, and snippets.

@jmorrill
Created March 12, 2018 07:01
Show Gist options
  • Save jmorrill/9d486c9616b4703610f15407dc86175b to your computer and use it in GitHub Desktop.
Save jmorrill/9d486c9616b4703610f15407dc86175b to your computer and use it in GitHub Desktop.
sqlite_orm_crash_repo.cpp
#include "stdafx.h"
#include "sqlite_orm.h"
#include <string>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
namespace details
{
struct MarvelHero {
int id;
std::string name;
std::string abilities;
};
static auto init_storage()
{
using namespace sqlite_orm;
return make_storage("iteration.sqlite",
make_table("marvel",
make_column("id",
&MarvelHero::id,
primary_key()),
make_column("name",
&MarvelHero::name),
make_column("abilities",
&MarvelHero::abilities)));
}
class db_holder
{
using db_storage_t = decltype(init_storage());
public:
db_holder() :
storage(init_storage())
{
storage.sync_schema();
}
db_storage_t storage;
};
class db_iterator_holder
{
std::shared_ptr<db_holder> _db;
using db_storage_query_t = decltype(_db->storage.iterate<MarvelHero>());
db_storage_query_t _query;
using db_storage_iterator_t = decltype(_query.begin());
/* if this is not a unique_ptr and only a field there is no crash
* if we add a move ctor to iterator_t "iterator_t(iterator_t&&) = default;"
* then the crash also goes away in this scenario */
std::unique_ptr<db_storage_iterator_t> _iterator;
public:
db_iterator_holder(std::shared_ptr<db_holder> db) :
_db(std::move(db)),
_query(_db->storage.iterate<MarvelHero>()),
_iterator(std::make_unique<db_storage_iterator_t>(_query.begin()))
{
}
std::vector<MarvelHero> next(size_t max_items)
{
std::vector<MarvelHero> heros;
heros.reserve(max_items);
for (size_t i = 0; i < max_items; ++i)
{
auto& it = *_iterator;
if (it == _query.end())
{
break;
}
auto hero = *it;
heros.push_back(hero);
++it;
}
return heros;
}
};
}
int main()
{
using namespace details;
auto db = std::make_shared<details::db_holder>();
db->storage.remove_all<MarvelHero>();
db->storage.insert(MarvelHero{ -1, "Tony Stark", "Iron man, playboy, billionaire, philanthropist" });
db->storage.insert(MarvelHero{ -1, "Thor", "Storm god" });
db->storage.insert(MarvelHero{ -1, "Vision", "Min Stone" });
db->storage.insert(MarvelHero{ -1, "Captain America", "Vibranium shield" });
db->storage.insert(MarvelHero{ -1, "Hulk", "Strength" });
db->storage.insert(MarvelHero{ -1, "Star Lord", "Humor" });
db->storage.insert(MarvelHero{ -1, "Peter Parker", "Spiderman" });
db->storage.insert(MarvelHero{ -1, "Clint Barton", "Hawkeye" });
db->storage.insert(MarvelHero{ -1, "Natasha Romanoff", "Black widow" });
db->storage.insert(MarvelHero{ -1, "Groot", "I am Groot!" });
auto db_it = std::make_unique<details::db_iterator_holder>(db);
std::vector<details::MarvelHero> heros;
do
{
heros = db_it->next(2);
for (const auto& hero : heros)
{
printf("%s\n", hero.name.c_str());
}
} while (!heros.empty());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment