#include "dba/dba.h"
#include <iostream>
class FooBase : public dba::Storeable {
DECLARE_STORE_TABLE();
public:
int mIntVal;
};
BEGIN_STORE_TABLE(FooBase, dba::Storeable, "foo_table")
BIND_INT(FooBase::mIntVal,dba::Int,"intval")
END_STORE_TABLE()
class FooDerived : public FooBase {
DECLARE_STORE_TABLE()
public:
int mDerivedVal;
};
BEGIN_STORE_TABLE(FooDerived, FooBase, "foo_derived")
BIND_INT(FooDerived::mDerivedVal,dba::Int,"derivedval")
END_STORE_TABLE()
class BarBase : public dba::Storeable {
DECLARE_STORE_TABLE()
public:
int mBase;
};
BEGIN_STORE_TABLE(BarBase, dba::Storeable, NULL)
BIND_INT(BarBase::mBase,dba::Int,"base")
END_STORE_TABLE()
class BarDerived : public BarBase {
DECLARE_STORE_TABLE()
public:
int mDerived;
};
BEGIN_STORE_TABLE(BarDerived, BarBase, "bar_table")
BIND_INT(BarDerived::mDerived,dba::Int,"derived")
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* foobase_create =
"CREATE TABLE foo_table ("
" id INT PRIMARY KEY,"
" intval INT"
")";
const char* fooderived_create =
"CREATE TABLE foo_derived ("
" id INT PRIMARY KEY,"
" derivedval INT"
")";
const char* custom_create =
"CREATE TABLE custom_table ("
" id INT PRIMARY KEY,"
" base INT"
")";
const char* bar_create =
"CREATE TABLE bar_table ("
" id INT PRIMARY KEY,"
" base INT,"
" derived INT"
")";
void
storeFoo(dba::SQLArchive& pAr) {
dba::SQLOStream ostream = pAr.getOStream();
ostream.open();
FooBase b;
b.mIntVal = 7;
ostream.put(&b);
std::cout << "FooBase was stored with id = "
<< b.getId() << std::endl;
FooDerived d;
d.mIntVal = 8;
d.mDerivedVal = 9;
ostream.put(&d);
std::cout << "FooDerived was stored with id = "
<< d.getId() << std::endl;
ostream.destroy();
std::cout << "FooBase data:" << std::endl;
std::auto_ptr<dba::DbResult> res(pAr.getIStream().sendQuery(
"SELECT id, intval FROM foo_table"
));
while(res->fetchRow()) {
std::cout << "id: "<< res->getInt(0) << " intval: "
<< res->getInt(1) << std::endl;
};
std::cout << "=======================" << std::endl;
std::cout << "FooDerived data:" << std::endl;
res.reset(pAr.getIStream().sendQuery(
"SELECT id, derivedval FROM foo_derived"
));
while(res->fetchRow()) {
std::cout << "id: "<< res->getInt(0) << " derivedval: "
<< res->getInt(1) << std::endl;
};
std::cout << "=======================" << std::endl;
};
void
storeBar(dba::SQLArchive& pAr) {
dba::SQLOStream ostream = pAr.getOStream();
ostream.open("custom_table");
BarBase b;
b.mBase = 7;
ostream.put(&b);
std::cout << "BarBase was stored with id = "
<< b.getId() << std::endl;
BarDerived d;
d.mBase = 8;
d.mDerived = 9;
ostream.open();
ostream.put(&d);
std::cout << "BarDerived was stored with id = "
<< d.getId() << std::endl;
ostream.destroy();
std::cout << "Custom table data:" << std::endl;
std::auto_ptr<dba::DbResult> res(pAr.getIStream().sendQuery(
"SELECT id, base FROM custom_table"
));
while(res->fetchRow()) {
std::cout << "id: "<< res->getInt(0) << " base: "
<< res->getInt(1) << std::endl;
};
std::cout << "=======================" << std::endl;
std::cout << "Bar table data:" << std::endl;
res.reset(pAr.getIStream().sendQuery(
"SELECT id, base, derived FROM bar_table"
));
while(res->fetchRow()) {
std::cout <<
"id: "<< res->getInt(0) <<
" base: " << res->getInt(1) <<
" derived: " << res->getInt(2)
<< 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(foobase_create);
ar.getOStream().sendUpdate(fooderived_create);
ar.getOStream().sendUpdate(custom_create);
ar.getOStream().sendUpdate(bar_create);
storeFoo(ar);
storeBar(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;
};
};