Untitled diff

Created Diff never expires
15 removals
70 lines
45 additions
99 lines
#include <iostream>
#include <iostream>
#include <memory>
#include <memory>
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/backend/Sqlite3.h>
#include <Wt/Dbo/backend/Sqlite3.h>


using namespace std;
using namespace std;
namespace dbo = Wt::Dbo;
namespace dbo = Wt::Dbo;


class EntityA
class EntityA
{
{
public:
public:
string name {"Jef"};
string name {"Jef"};


template<class Action>
template<class Action>
void persist(Action& a)
void persist(Action& a)
{
{
dbo::field(a, name, "name");
dbo::field(a, name, "name");
}
}
};
};


class EntityB
class EntityB
{
{
public:
public:
string name {"Jef Jr."};
string name {"Jef Jr."};
dbo::ptr<EntityA> parent;

static shared_ptr<dbo::Session> session;
static void setSession(shared_ptr<dbo::Session> s)
{
session = s;
}


template<class Action>
template<class Action>
void persist(Action& a)
void persist(Action& a)
{
{
dbo::field(a, name, "name");
dbo::field(a, name, "name");
dbo::belongsTo(a, parent, "parent");
dbo::belongsTo(a, parent, "parent");
}
}

unique_ptr<EntityA> getParent() const
{
dbo::Transaction t{*session};
return make_unique<EntityA>(*parent);
}

void setParent(dbo::ptr<EntityA> p)
{
parent = p;
}

void setParent(unique_ptr<EntityA> p)
{
parent.reset(move(p));
}

private:
dbo::ptr<EntityA> parent {nullptr};
};
};


dbo::ptr<EntityB> getAnyB(dbo::Session& session)
shared_ptr<dbo::Session> EntityB::session = nullptr;

dbo::ptr<EntityB> getAnyB(shared_ptr<dbo::Session> session)
{
{
dbo::Transaction t{session};
dbo::Transaction t{*session};


return session.find<EntityB>().limit(1);
return session->find<EntityB>().limit(1);
}
}


int main()
int main()
{
{
dbo::Session session;
auto session = make_shared<dbo::Session>();


session.setConnection([](){
session->setConnection([](){
auto sqlite3 = make_unique<dbo::backend::Sqlite3>(":memory:");
auto sqlite3 = make_unique<dbo::backend::Sqlite3>(":memory:");
sqlite3->setProperty("show-queries", "true");
sqlite3->setProperty("show-queries", "true");


return move(sqlite3);
return move(sqlite3);
}());
}());


session.mapClass<EntityA>("entity_a");
session->mapClass<EntityA>("entity_a");
session.mapClass<EntityB>("entity_b");
session->mapClass<EntityB>("entity_b");


session.createTables();
session->createTables();

EntityB::setSession(session);


{
{
dbo::Transaction t{session};
dbo::Transaction t{*session};


auto b = make_unique<EntityB>();
auto b = make_unique<EntityB>();
b->parent = session.add(make_unique<EntityA>());
b->setParent(
session.add(move(b));
session->add(make_unique<EntityA>())
);
session->add(move(b));
}
}


//dbo::Transaction t{session}; // This "fixes" the problem, but it's VERY inconvenient
cout << getAnyB(session)->getParent()->name;
cout << getAnyB(session)->parent->name; // Exception: Dbo load(): no active transaction


return 0;
return 0;
}
}