Quick startBelow are list of tasks that demonstrates the simplest usage scenario of library taken from Quickstart example.Examples of more advanced features can be found in Examples section. To store class in database we have to derive from dba::Storeable and declare that we add table with list of members that we want to store (store table): class Foo : public dba::Storeable { DECLARE_STORE_TABLE(); public: int mIntVal; std::string mStrVal; }; DECLARE_STORE_TABLE is a macro that declares store table for a class. In a cpp file we have to define what member of class will be stored in a database and how: BEGIN_STORE_TABLE(Foo, dba::Storeable, "foo_table") //we store mStrVal in varchar database field named //"strval" using dba::String as conversion filter BIND_STR(Foo::mStrVal,dba::String,"strval") //we store mIntVal in numeric database field named //"intval" using dba::Int32 as conversion filter BIND_INT(Foo::mIntVal,dba::Int,"intval") END_STORE_TABLE() We have to define SQL statements for SQL database schema: //begin of SQL schema dba::SQL counter_create( "CREATE TABLE debea_object_count (" " id INT" ")"); dba::SQL foo_create( "CREATE TABLE foo_table (" " id INT PRIMARY KEY," " intval INT," " strval VARCHAR" ")"); //end of SQL schema We created additional field in foo_table named "id". This is required by dba::Storeable class. debea_object_count table is needed for dba::GenericFetcher to store last assigned id. First we load sqlite3 plugin and setup database: //this will load database plugin and create new empty database ar.open("dbasqlite3-static", "dbname=foobasefile.sqt3"); //create needed tables sending simple SQL ar.getOStream().sendUpdate(counter_create); ar.getOStream().sendUpdate(foo_create); Now we can load and store object of Foo class this way: Foo c1;
c1.mIntVal = 12;
c1.mStrVal = "test string";
//write object in database
dba::SQLOStream ostream = ar.getOStream();
ostream.open();
ostream.put(&c1); //id of object is assigned here
std::cout << "Foo c1 was stored with id = " << c1.getId() << std::endl;
//avoid creation new connection for istream
//by releasing dba::DbConnection from ostream
ostream.destroy();
//read first object from database
Foo c2;
dba::SQLIStream istream = ar.getIStream();
istream.open(c2);
while (istream.getNext(&c2)) {
std::cout << "readed Foo c2 with id: " << c2.getId() << std::endl;
};
//now c2 should be equal to c1
if ((c1.mIntVal == c2.mIntVal)
&& (c1.mStrVal == c2.mStrVal))
std::cerr << "Foo is Foo!" << std::endl;
|
|
||||||||||
| Back | |||||||||||
Generated by doxygen at Fri Feb 26 10:46:15 2010
