📄 indexedviewexample.cpp
字号:
// example illustrating the use of an IndexedDBView for Example objects
#include "IndexedViewExample.h"
const TIMESTAMP_STRUCT then = {2000, 12, 15, 0, 0, 0, 0};
// Example of using an IndexDBView to read, insert and update records in a container / database
void IndexedViewExample()
{
typedef DBView<Example, ParamObjExample> DBV;
DBV view("DB_EXAMPLE", DefaultBCA<Example>(),
"WHERE INT_VALUE BETWEEN (?) AND (?) OR "
"STRING_VALUE = (?) OR EXAMPLE_DATE <= (?) ORDER BY EXAMPLE_LONG",
BPAExampleObj());
// make the functor needed for SetParams out of SetParamsExample() by calling
// cb_ptr_fun(SetParamsExample)
IndexedDBView<DBV> indexed_view(view, "PrimaryIndex; STRING_VALUE; UNIQUE AlternateIndex; EXAMPLE_LONG, EXAMPLE_DATE",
BOUND, USE_ALL_FIELDS,
cb_ptr_fun(SetParamsExample));
try
{
// Find the item where the STRING_VALUE matches the string "Foozle"
IndexedDBView<DBV>::indexed_iterator idxview_it = indexed_view.find(string("Foozle"));
// Update the item with the key of "Foozle", to read "Fizzle" instead
if (idxview_it != indexed_view.end()) {
Example replacement;
replacement = *idxview_it;
replacement.SetExampleStr("Fizzle");
TIMESTAMP_STRUCT date = {2003, 3, 3, 0, 0, 0, 0};
replacement.SetExampleDate(date);
indexed_view.replace(idxview_it, replacement);
}
// Now find a second set of items using AlternateIndex
// The STL convention for equal_range is to return a pair consisting of:
// 1. an iterator referring to the beginning of the list of found items
// 2. an iterator pointing to the end of the list of found items.
// We will remove all items in this range.
const TIMESTAMP_STRUCT ts_date_criteria = {2000, 1, 1, 0, 0, 0, 0};
jtime_c date_criteria(ts_date_criteria);
long long_criteria = 33;
IndexedDBView<DBV>::indexed_pair pr = indexed_view.equal_range_AK ("AlternateIndex", long_criteria, date_criteria);
idxview_it = pr.first;
cout << "*** Size before erase calls: " << indexed_view.size() << " ***"
<< endl;
// Remove all items that match the criteria in our equal_range_AK lookup
while (idxview_it != pr.second)
{
// As iterator is invalidated upon an erase(), use a
// temporary iterator to point to DataObj to erase.
// Increment idxview_it before we erase so it will still be valid
// when we erase the DataObj.
IndexedDBView<DBV>::indexed_iterator deleteMe = idxview_it;
++idxview_it;
indexed_view.erase(deleteMe);
}
cout << "*** Size after erase calls: " << indexed_view.size() << " ***"
<< endl;
// Insert a new item into the container
pair<IndexedDBView<DBV>::iterator, bool> ins_pr;
cout << "We will now try to insert three items. Only the first item will succeed!" << endl;
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;
ins_pr = indexed_view.insert(Example(311, "", 3.99, 91, then)); // should fail on InsValidate()
cout << "insertion succeded = " << (ins_pr.second == true ? "true": "false") << endl;
ins_pr = indexed_view.insert(Example(222, "Positron", -34.77, 29, then)); // should fail (ditto)
cout << "insertion succeded = " << (ins_pr.second == true ? "true": "false") << endl;
}
catch (...)
{
typedef LoggingHandler<Example, ParamObjExample>::LoggedTriple LoggedTriple;
// retrieve the LoggingHandler object from the IndexedDBView
LoggingHandler<Example, ParamObjExample> log_handler =
indexed_view.get_io_handler((LoggingHandler<Example, ParamObjExample> *) NULL);
// the log is a vector of (error message, DataObj, ParamObj) triples,
// (error message, Example object, ParamObjExample object) in this case
// the error itself along with the relevant DataObj and ParamObj that resulted with
// the error
vector<LoggedTriple> error_log = log_handler.GetLog();
// nothing to do if no errors occurred
if (error_log.empty())
return;
cout << "*** Error Log in IndexedViewExample(): " << error_log.size() << " errors recorded! ***"
<< endl;
// print out the errors
for (vector<LoggedTriple>::const_iterator log_it = error_log.begin();
log_it != error_log.end(); log_it++)
{
cout << "*** Error Log Entry ***" << endl;
cout << "* error message *" << endl;
cout << (*log_it).errmsg << endl;
cout << "* relevant Example object *" << endl;
cout << (*log_it).dataObj << endl;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -