#include "dba/dba.h"
#include <iostream>
class Foo : public dba::Storeable {
DECLARE_STORE_TABLE();
public:
int mIntVal;
};
BEGIN_STORE_TABLE(Foo, dba::Storeable, "fake_relation_name")
BIND_INT(Foo::mIntVal,dba::Int,"intval")
END_STORE_TABLE()
class Bar : public dba::Storeable {
DECLARE_STORE_TABLE()
public:
int mBase;
Foo mFoo;
};
BEGIN_STORE_TABLE(Bar, dba::Storeable, "bar_table")
BIND_INT(Bar::mBase,dba::Int,"base")
BIND_STB(Bar::mFoo)
END_STORE_TABLE()
dba::SQL counter_create(
"CREATE TABLE debea_object_count ("
" id INT"
")");
dba::SQL bar_create(
"CREATE TABLE bar_table ("
" id INT PRIMARY KEY,"
" base INT,"
" intval INT"
")");
void
storeStb(dba::SQLArchive& pAr) {
dba::SQLOStream ostream = pAr.getOStream();
ostream.open();
Bar b;
b.mBase = 5;
b.mFoo.mIntVal = 6;
ostream.put(&b);
std::cout << "Bar was stored with id = " << b.getId() << std::endl;
ostream.destroy();
int id, base, intval;
std::cout << "Bar data:" << std::endl;
std::auto_ptr<dba::DbResult> res(pAr.getIStream().sendQuery(
dba::SQL("SELECT id, base, intval FROM bar_table").into(id).into(base).into(intval)
));
while(res->fetchRow()) {
std::cout <<
"id: "<< id <<
" base: " << base <<
" intval: " << intval <<
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(bar_create);
ar.getOStream().sendUpdate(dba::SQL("INSERT INTO debea_object_count VALUES (:d)") << 1);
storeStb(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;
};
};