📄 indexeddbviewexample.htm
字号:
<pre><code><span class="codeComment">// "Example" class to hold rows from our database table</span>
class Example
{
public: <span class="codeComment">// tablename.columnname:</span>
int exampleInt; <span class="codeComment">// DB_EXAMPLE.INT_VALUE</span>
string exampleStr; <span class="codeComment">// DB_EXAMPLE.STRING_VALUE</span>
double exampleDouble; <span class="codeComment">// DB_EXAMPLE.DOUBLE_VALUE</span>
long exampleLong; <span class="codeComment">// DB_EXAMPLE.EXAMPLE_LONG</span>
TIMESTAMP_STRUCT exampleDate; <span class="codeComment">// DB_EXAMPLE.EXAMPLE_DATE</span>
Example(int exInt, const string &exStr, double exDouble, long exLong,
const TIMESTAMP_STRUCT &exDate) :
exampleInt(exInt), exampleStr(exStr), exampleDouble(exDouble), exampleLong(exLong),
exampleDate(exDate)
{ }
};
<span class="codeComment">// Parameter object to hold parameters for dynamic SQL query below </span>
class ParamObjExample
{
public:
int lowIntValue;
int highIntValue;
string strValue;
TIMESTAMP_STRUCT dateValue;
};
<span class="codeComment">// Create an association between table columns and fields in our object</span>
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;
}
};
<span class="codeComment">// Create an association between query parameters and fields in our parameters object</span>
class BPAExampleObj
{
public:
void operator()(BoundIOs &boundIOs, ParamObjExample &paramObj)
{
boundIOs[0] == paramObj.lowIntValue;
boundIOs[1] == paramObj.highIntValue;
boundIOs[2] == paramObj.strValue;
boundIOs[3] == paramObj.dateValue;
}
};
<span class="codeComment">// Set parameters function for Example ... used by IndexedDBView<Example> to set dynamic query parameters
// Dynamic query parameters are indicated by (?) in our query string for the IndexedDBView</span>
void SetParamsExample(ParamObjExample &params)
{
<span class="codeComment">// set parameter values</span>
params.lowIntValue = 2;
params.highIntValue = 8;
params.strValue = "Example";
TIMESTAMP_STRUCT paramDate = {2000, 1, 1, 0, 0, 0, 0};
params.dateValue = paramDate;
}
<span class="codeComment">// Example of using an IndexDBView to read, insert and update records in a container / database</span>
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));
<span class="codeComment">// Find the item where the STRING_VALUE matches the string "Foozle"</span>
IndexedDBView<DBV>::indexed_iterator idxview_it = indexed_view.find(string("Foozle"));
<span class="codeComment">// Update the item with the key of "Foozle", to read "Fizzle" instead</span>
if (idxview_it != indexed_view.end()) {
Example replacement;
replacement = *idxview_it;
replacement.exampleStr = "Fizzle";
indexed_view.replace(idxview_it, replacement);
}
<span class="codeComment">// 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.</span>
const TIMESTAMP_STRUCT date_criteria = {2000, 1, 1, 0, 0, 0, 0};
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;
<span class="codeComment">// Remove all items that match the criteria in our equal_range_AK lookup</span>
while (idxview_it != pr.second)
{
<span class="codeComment">// 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.</span>
IndexedDBView<DBV>::indexed_iterator deleteMe = idxview_it;
idxview_it++;
indexed_view.erase(deleteMe);
}
cout << "*** Size after erase calls: " << indexed_view.size() << " ***"
<< endl;
<span class="codeComment">// Finally, insert a new item into the container</span>
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;
}
</code></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -