#include "dba/dba.h"
#include <iostream>
class Bar : public dba::Storeable {
DECLARE_STORE_TABLE()
public:
int mBarValue;
};
BEGIN_STORE_TABLE(Bar, dba::Storeable, "bar_table")
BIND_INT(Bar::mBarValue,dba::Int,"value")
END_STORE_TABLE()
class CuBar : public dba::Storeable {
DECLARE_STORE_TABLE()
public:
int mBarValue;
};
BEGIN_STORE_TABLE(CuBar, dba::Storeable, NULL)
BIND_INT(CuBar::mBarValue,dba::Int,"value")
END_STORE_TABLE()
class Foo : public dba::Storeable {
DECLARE_STORE_TABLE();
public:
int mIntVal;
CuBar mSpecialCuBar;
std::list<Bar> mBars;
std::list<CuBar> mCuBars;
};
BEGIN_STORE_TABLE(Foo, dba::Storeable, "foo_table")
BIND_INT(Foo::mIntVal,dba::Int,"intval")
BIND_COL(Foo::mBars, dba::stdList<Bar>, "fk_foo")
BIND_CLA(Foo::mCuBars, dba::stdList<CuBar>, "fk_foo",1,"cubar_table")
BIND_CLA(Foo::mSpecialCuBar, dba::Single, "fk_foo",2,"cubar_table")
END_STORE_TABLE()
const char* counter_create =
"CREATE TABLE debea_object_count ("
" id INT"
")";
const char* counter_init =
"INSERT INTO debea_object_count VALUES (1)";
const char* foo_create =
"CREATE TABLE foo_table ("
" id INT PRIMARY KEY,"
" intval INT"
")";
const char* bar_create =
"CREATE TABLE bar_table ("
" id INT PRIMARY KEY,"
" fk_foo INT NOT NULL,"
" value INT"
")";
const char* cubar_create =
"CREATE TABLE cubar_table ("
" id INT PRIMARY KEY,"
" fk_foo INT NOT NULL,"
" dba_coll_id INT NOT NULL,"
" value INT"
")";
void
storeFoo(dba::SQLArchive& pAr) {
dba::SQLOStream ostream = pAr.getOStream();
ostream.open();
CuBar special;
special.mBarValue = 777;
Foo f;
f.mIntVal = 7;
f.mSpecialCuBar = special;
for (int i = 10; i < 20; i++) {
Bar b;
b.mBarValue = i;
f.mBars.push_back(b);
if (i < 15) {
CuBar c;
c.mBarValue = i+100;
f.mCuBars.push_back(c);
};
};
ostream.put(&f);
std::cout << "Foo was stored with id = " << f.getId() << std::endl;
ostream.destroy();
dba::SQLIStream istream = pAr.getIStream();
Foo loaded;
istream.setWhereId(f.getId());
istream.get(&loaded);
std::cout << "Foo 'loaded' object id " << loaded.getId() <<
",mIntVal=" << loaded.mIntVal << std::endl;
std::cout
<< "Foo.mSpecialCuBar id " << loaded.mSpecialCuBar.getId()
<< ",mBarVal=" << loaded.mSpecialCuBar.mBarValue
<< std::endl;
for(std::list<Bar>::const_iterator it = loaded.mBars.begin();
it != loaded.mBars.end(); it++)
{
std::cout << "Bar id=" << it->getId() <<
",mBarVal=" << it->mBarValue << std::endl;
};
for(std::list<CuBar>::const_iterator it = loaded.mCuBars.begin();
it != loaded.mCuBars.end(); it++)
{
std::cout << "cuBar id=" << it->getId() <<
",mBarVal=" << it->mBarValue << std::endl;
};
std::cout << "=======================" << std::endl;
};
int
main (int argc, char** argv) {
try {
dba::SQLArchive ar;
ar.setIdFetcher(new dba::GenericFetcher());
unlink("foobasefile.sqt3");
ar.open("dbasqlite3-static", "dbname=foobasefile.sqt3");
ar.getOStream().sendUpdate(counter_create);
ar.getOStream().sendUpdate(counter_init);
ar.getOStream().sendUpdate(foo_create);
ar.getOStream().sendUpdate(bar_create);
ar.getOStream().sendUpdate(cubar_create);
storeFoo(ar);
return 0;
} catch (const dba::SQLException& pEx) {
std::cout << "SQL Error: " << pEx.what() << std::endl;
std::cout << "While executing: " << std::endl
<< pEx.getQuery() << std::endl;
} catch (const dba::Exception& pEx) {
std::cout << "Error: " << pEx.what() << std::endl;
return -1;
};
};