//----------------------------------------------------------------------------//
// dbstep_t2.cpp - This example shows a simple object/relational
// mapping between class TestRecord and table TestTable.
//----------------------------------------------------------------------------//
#include <iostream>
#include <fstream>
#ifdef __BORLANDC__
#include <conio.h>
#endif
#define XML_FNAME "test_xml.xml"
#include "dbstep_test.h"
int main (int argc, char** argv)
{
// here we get driver code
int drv=getDriver(argc, argv);
// init parameter map
DBSTEP::StringMapType param_map;
try
{
if (drv == DBSTEP_FBIRD)
param_map ["dbname"] = "dbstep_test.gdb";
else
{
param_map ["dbname"] = "dbstep_test";
param_map ["host"] = "localhost";
param_map ["user"] = "dbstep_user";
param_map ["password"] = "dbstep_test";
}
param_map ["autocommit"] = "1";
// new database object
DBSTEP::Db db(drv);
// opens connection
db.openConnection(param_map);
{
TestRecord t(&db);
//t.setQueryMode(DBSTEP_QUERY_EXEC_PREPARED);
// fills TestRecord fields ...
t.name="insert'd1";
t.livel=9;
// ... and inserts the record
t.insert();
t.name="inserted''2";
t.livel=10;
t.insert();
// selects records, 't' acts as a cursor here
// firebird needs the 'FOR UPDATE' claues otherwise the cursor in becomes invalid
// by default, select methods on records prepare and execute the query. You can modify
// this behaviour
if ((drv == DBSTEP_FBIRD) || (drv == DBSTEP_ODBC))
t.selectCond("ORDER BY livel FOR UPDATE");
else
t.selectCond("ORDER BY livel");
// loops over selected records
while (t.next())
{
std::cout << "\nNew record: ";
t.print();
if (t.id == 1)
{
// here we try quoting functions
t.name = "up'dated";
// we can update records with a cursor, if table has a primary key
// if update field does not exist an exception is raised
t.clearUpdateFieldList();
t.addUpdateField("name");
t.update();
}
if (t.id == 2)
{
t.name = "upd'doublequote''2";
t.livel = 123;
t.clearUpdateFieldList();
t.update();
}
}
t.id = 1;
// we can remove records with a cursor, if table has a primary key
t.remove();
t.id = 2;
t.remove();
int th = 6;
int tl = 4;
// parametrized query
DBSTEP::Query q(&db);
q=" WHERE id <=", th, " AND livel >=", tl, " ORDER BY id";
t.selectCond(&q);
while (t.next())
{
std::cout << "\nUpdated: ";
std::cout << " id : " << t.id;
std::cout << " livel : " << t.livel;
std::cout << " name : " << t.name << std::endl;
}
// finds record by primary key
t.find(3);
t.selectCond("ORDER BY id");
// preliminary XML output
/*
std::ofstream os;
os.open(XML_FNAME, ios::trunc);
os << "<data>\n";
t.xmlWriteTable(os);
os << "\n</data>\n";
os.close();
*/
std::cout << "\nFIND: ";
std::cout << " id : " << t.id;
std::cout << " livel : " << t.livel;
std::cout << " name : " << t.name << std::endl;
}
// cleanup
db.closeConnection();
}
catch (errException e)
{
std::cout << "\nERROR: " << DBSTEP::Params::instance()->getErrManager()->first()->getName() << std::endl;
}
// cleans up
DBSTEP::Params::instance()->dispose();
#ifdef __BORLANDC__
getch();
#endif
return 0;
}
syntax highlighted by Code2HTML, v. 0.9.1