class Example
{
public:
int exampleInt;
string exampleStr;
double exampleDouble;
long exampleLong;
TIMESTAMP_STRUCT exampleDate;
Example(int exInt, const string &exStr, double exDouble, long exLong,
const TIMESTAMP_STRUCT &exDate) :
exampleInt(exInt), exampleStr(exStr), exampleDouble(exDouble), exampleLong(exLong),
exampleDate(exDate)
{ }
};
class ParamObjExample
{
public:
int lowIntValue;
int highIntValue;
string strValue;
TIMESTAMP_STRUCT dateValue;
};
class BCAExampleObj
{
public:
void operator()(BoundIOs &boundIOs, Example &rowbuf)
{
boundIOs["INT_VALUE"] == rowbuf.exampleInt;
boundIOs["STRING_VALUE"] == rowbuf.exampleStr;
boundIOs["DOUBLE_VALUE"] == rowbuf.exampleDouble;
boundIOs["EXAMPLE_LONG"] == rowbuf.exampleLong;
boundIOs["EXAMPLE_DATE"] == rowbuf.exampleDate;
}
};
class BPAExampleObj
{
public:
void operator()(BoundIOs &boundIOs, ParamObjExample ¶mObj)
{
boundIOs[0] == paramObj.lowIntValue;
boundIOs[1] == paramObj.highIntValue;
boundIOs[2] == paramObj.strValue;
boundIOs[3] == paramObj.dateValue;
}
};
void SetParamsExample(ParamObjExample ¶ms)
{
params.lowIntValue = 2;
params.highIntValue = 8;
params.strValue = "Example";
TIMESTAMP_STRUCT paramDate = {2000, 1, 1, 0, 0, 0, 0};
params.dateValue = paramDate;
}
void IndexedViewExample()
{
typedef DBView<Example, ParamObjExample> DBV;
DBV view("DB_EXAMPLE", BCAExampleObj(),
"WHERE INT_VALUE BETWEEN (?) AND (?) OR "
"STRING_VALUE = (?) OR EXAMPLE_DATE <= (?) ORDER BY EXAMPLE_LONG",
BPAExampleObj());
IndexedDBView<DBV> indexed_view(view, "UNIQUE PrimaryIndex; STRING_VALUE; AlternateIndex; EXAMPLE_LONG, EXAMPLE_DATE",
BOUND, USE_ALL_FIELDS, cb_ptr_fun(SetParamsExample));
IndexedDBView<DBV>::iterator idxview_it = indexed_view.find(string("Foozle"));
if (idxview_it != indexed_view.end()) {
Example replacement;
replacement = *idxview_it;
replacement.exampleStr = "Fizzle";
indexed_view.replace(idxview_it, replacement);
}
const TIMESTAMP_STRUCT date_criteria = {2000, 1, 1, 0, 0, 0, 0};
long long_criteria = 33;
pair<IndexedDBView<DBV>::iterator, IndexedDBView<DBV>::iterator> pr =
indexed_view.equal_range_AK ("AlternateIndex", long_criteria, date_criteria);
idxview_it = pr.first;
cout << "*** Size before erase calls: " << indexed_view.size() << " ***"
<< endl;
while (idxview_it != pr.second)
{
IndexedDBView<DBV>::iterator deleteMe = idxview_it;
idxview_it++;
indexed_view.erase(deleteMe);
}
cout << "*** Size after erase calls: " << indexed_view.size() << " ***"
<< endl;
pair<IndexedDBView<DBV>::iterator, bool> ins_pr;
ins_pr = indexed_view.insert(Example(459, "Unique String #1", 3.4, 1, date_criteria));
cout << "insertion succeded = " << (ins_pr.second == true ? "true": "false") << endl;
}