How to build an RWDBCriterion with dynamic restrictions

Article ID: 1076
Last updated: 02 Feb, 2008
Article ID: 1076
Last updated: 02 Feb, 2008
Revision: 1
Views: 4238
Posted: 03 Nov, 1998
by Dean J.
Updated: 02 Feb, 2008
by Dean J.
Problem


When building a query, the restrictions are not known until run-time.


Cause


The information needed to build the RWDBCriterion is unknown at compile time.


Action


The following sample code shows how to build the RWDBCriterion with information that is read from a file. Note that the structure of the query is already known but the values are not known. However, the same approach will work when the information is coming from a user or some other source.
void main() {
RWDBManager::setErrorHandler(errorHandler);

// FILL IN CONNECTION PARAMETERS!!
RWDBDatabase db = RWDBManager::database(params);

RWDBTracer &tracer = db.tracer();
tracer.setOn(RWDBTracer::SQL);
tracer.stream(cout);

// Get a table and a selector
RWDBTable tbl = db.table("test1");

RWDBSelector sel = db.selector();
// set the select list
sel << tbl["id"];

// Build an RWDBCriterion from RWDBExpr's and data read from file
RWCString f;
ifstream in("test.data");

RWDBExpr expr1(tbl["name"]);
RWDBCriterion crit1, crit2;
f.readToken(in);
if (!f.isNull()) {
crit1 = expr1 == f.data();
f.readToken(in);

while (!f.isNull()) { // have another token
crit2 = expr1 == f.data();
crit1 = crit1 || crit2;
f.readToken(in);
}
}
sel.where(crit1);
RWDBReader rdr = sel.reader();
while(rdr())
cout << "a row" << endl;
}
This article was:   Helpful | Not helpful
Report an issue
Article ID: 1076
Last updated: 02 Feb, 2008
Revision: 1
Views: 4238
Posted: 03 Nov, 1998 by Dean J.
Updated: 02 Feb, 2008 by Dean J.
Also listed in


Others in this category