dtl_introduction.htm
来自「The goal of this library is to make ODBC」· HTM 代码 · 共 1,132 行 · 第 1/4 页
HTM
1,132 行
<br>
</font></p>
<p><br>
<strong><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> </strong></p>
<p><font face="Times Roman"><br>
To understand how IndexedDBView works we begin with the
constructor definition<br>
<br>
</font><font face="Fixedsys">IndexedDBView(DBView<DataObj,
ParamObj> &view, <br>
const string &IndexNamesAndFields,<br>
BoundMode bm = UNBOUND, KeyMode km = USE_ALL_FIELDS, <br>
SetParamsFn SetParams = NULL);<br>
</font><font face="Times Roman"><br>
<br>
The first parameter here is a view object; this defines the SQL
Query that will be used to read and write records as described in
the previous two examples. The second parameter is
IndexNamesAndFields; this defines indexes on the rows in the
container and we will examine it in more detail shortly. The
BoundMode and KeyMode control whether or not changes to the
container data are synchronized with the database, and if so what
key fields are used for the synchronization. If BoundMode =
BOUND, then any changes to the container are sent to the database.
If BoundMode = UNBOUND then any changes to the container will
only apply locally. Finally, the SetParams function allows the
user to pass in an explicit function for setting parameters in
the where clause for the view if they so desire.<br>
<br>
The IndexNamesAndFields parameter is interesting.
IndexNamesAndFields is used to automatically create named indexes
into our rows. In the above example we have <br>
<br>
</font><font face="Fixedsys">IndexNamesAndFields = "UNIQUE
PrimaryIndex; STRING_VALUE; AlternateIndex; EXAMPLE_LONG,
EXAMPLE_DATE";<br>
</font><font face="Times Roman"><br>
What this does is create two indexes on the data that is read
into the container. The first index is designated to be a UNIQUE
with the name "PrimaryIndex" and is based on the field
called STRING_VALUE. Because this key is designated as unique
this forms a constraint on the container whereby every entry for
(STRING_VALUE) must be unique in order for the associated row to
be added to the table. The second index is created with the name
"AlternateIndex" and is based on the fields
EXAMPLE_LONG and EXAMPLE_DATE. AlternateIndex is not designated
to be unique here and is created only to provide a way to quickly
look up rows based on the values in the EXAMPLE_LONG and
EXAMPLE_DATE fields.<br>
<br>
Why do we care about this? Doesn't the normal STL associative
container already provide lookup and retrieval using keys? Well,
the normal associative containers in STL have two limitations
that we found quite tedious to work with in practice. The first
limitation is that if you want an STL container to provide lookup
capabilities then you need to manually write comparison functions
for each class and index that you want to use. As the number of
tables and indexes grow, manually maintaining these comparison
functions gets to be a bit tedious. The IndexNamesAndFields
syntax can automatically create indexes given a list of field
names. The internal comparison functions that are created are
slightly slower than using hand made comparison operators, but,
the performance difference is not that great and we feel that the
loss is more than made up for by the increased ease of use and
maintainability. The second limitation is that the STL containers
only support a single index on the data. We found this rather
confining since we often want to be able to search the same set
of rows quickly using various subsets of the row fields. For this
reason, IndexNamesAndFields allows you to create multiple indexes
on the rows in your container. To see how these features are used
to search based on the PrimaryIndex and AlternateIndex we examine
the following lines from the above example:<br>
<br>
</font><font face="Fixedsys">idxview_it = indexed_view.find(string("Foozle"));<br>
<br>
pr = indexed_view.equal_range_AK("IndexLongDate",
long_criteria, date_criteria);<br>
<br>
</font><font face="Times Roman">Standard STL containers provide a
find method to locate objects in the container. This method is
typically defined as follows:<br>
<br>
<i>container< DataObj >::find<br>
</i></font><font face="Courier"><i><tt>const_iterator </tt></i><b><i><tt>find</tt></i></b><i><tt>(const
</tt></i></font><font face="Times Roman"><i>DataObj </i></font><font
face="Courier"><i><tt>& key) const;<br>
</tt></i></font><font face="Times Roman"><i>The find member
function returns an iterator that designates the earliest element
in the controlled sequence whose sort key equals </i></font><font
face="Courier"><i><tt>key</tt></i> </font><font
face="Times Roman"><i>. If no such element exists, the iterator
equals end().<br>
</i><br>
In the IndexDBView container, we overload the find() function
with multiple versions :<br>
<br>
template<class DataField> indexed_iterator find(const
DataField &df1) // One field find<br>
template<class DataField1, class DataField2>
indexed_iterator find(const DataField1 &df1, const DataField2
&df2) { }; // Two field find <br>
<br>
// Three field find, four field find, etc.<br>
<br>
template<> indexed_iterator find<DataObj>(const
DataObj &key) // Standard find <br>
<br>
As per the standard, we provide a find(DataObj) method to locate
elements in the container. Our default find method uses the first
index passed into the IndexDBView constructor to locate objects,
and will return a match based only on the fields in that index.
In addition to the default find method, we have added overloaded
versions of the find method to perform a find using only the
fields needed by the index. For example, in the case of </font><font
face="Fixedsys">indexed_view.find(string("Foozle")) </font><font
face="Times Roman">, the find() function resolves to
find<DataField> (const DataField &df1). This is useful,
because it allows us to execute a find by directly supplying the
criteria fields that we care about rather than having to manually
initialize an entire data object just to perform a find operation.<br>
<br>
In addition to find() operations using the primary index, we can
also find an object based upon any of the indexes named in the
constructor for IndexDBView. This is done via the find_AK
function. For example, we could say </font><font face="Fixedsys">indexed_view.find_AK("AlternateIndex",
long_criteria, date_criteria) </font><font face="Times Roman">,
which would find the first element that matches the criteria
provided by long_criteria and date_criteria using the fields
named in the "AlternateIndex" to determine if we have a
match.<br>
<br>
Finally, you will notice that the above code has calls to insert(),
replace() and erase() methods for IndexedDBView. One major
difference between the IndexedDBView container and a standard
container is that any changes made to the items in our container
can be automatically propagated back to the database. If we
construct the container to initialize in what we call "Bound"
mode then any changes made to the container are also sent to the
database. In our example, when we call the erase() method, this
removes the item in the container and also deletes the underlying
record in the database. Similarly, insert() and replace() will
modify both container and the database.<br>
<br>
<br>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?