dtl_introduction.htm

来自「The goal of this library is to make ODBC」· HTM 代码 · 共 1,132 行 · 第 1/4 页

HTM
1,132 行
字号
</font><a name="_Toc500425987"></a><font face="Arial"><b><i>When
you don't know what you need, dynamic queries:<br>
</i></b></font><font face="Times Roman">The queries shown above
assume that you know exactly what your target table looks like
and are able to define static objects to go against known fields
in these tables. In practice, you often end up in the situation
where you have a query with an unknown number of columns with
unknown types and you want to bind a dynamic object to this query.
To solve this problem, our library has two additional containers
called DynamicDBView and DynamicIndexedDBView which perform
binding to a variant row class. This variant row class allows for
an arbitrary number of fields, with each field being of an
arbitrary type</font><a href="#FNT1"><font size="2"
face="Times Roman"><sup>2</sup></font></a><font
face="Times Roman">. The type and number of fields in variant row
are determined at run-time by querying the underlying database to
find the number of fields in the query and the type of each field
that is to be returned. To illustrate, we present an example:<br>
<br>
<strong><pre><code><span class="codeComment">// Using a DynamicDBView to read rows from the database.

// Read the contents of a table and print the resulting rows</span>
void SimpleDynamicRead() {

	<span class="codeComment">// Our query will be &quot;SELECT * FROM DB_EXAMPLE&quot;</span>
	DynamicDBView&lt;&gt; view(&quot;DB_EXAMPLE&quot;, &quot;*&quot;);

	<span class="codeComment">// NOTE: We need to construct r from the view itself since we
	// don't know what fields the table will contain.
	// We therefore make a call to the DataObj() function to have the
	// table return us a template row with the correct number of fields
	// and field types.
	// We use this construction since we can't be guaranteed that the table
	// is non-empty &amp; we want to still display column names in this case.</span>
	variant_row s(view.DataObj());

	<span class="codeComment">// Print out the column names</span>
	vector&lt;string&gt; colNames = s.GetNames();
	for (vector&lt;string&gt;::iterator name_it = colNames.begin(); name_it != colNames.end(); name_it++)
	{
		cout &lt;&lt; (*name_it) &lt;&lt; &quot; &quot;;
	}
	cout &lt;&lt; endl;

	<span class="codeComment">// Print out all rows and columns from our query</span>
	DynamicDBView&lt;&gt;::select_iterator print_it = view.begin();
	for (print_it = view.begin(); print_it != view.end(); print_it++)
	{
		variant_row r = *print_it;
		for (size_t i = 0; i &lt; r.size(); i++)
		{
			cout &lt;&lt; r[i] &lt;&lt; &quot; &quot;;
		}
		cout &lt;&lt; endl;
	}
}</code></pre> </strong><br>
<br>
Unlike the DBView code presented above, in DynamicDBView there is
no notion of a BCA to bind records to a particular class since
the assumption is that DynamicDBView will always bind to a
variant_row object. Therefore, the DynamicDBView is constructed
by specifying a table name and a list of fields to select from
the table (in this case we use &quot;*&quot; to specify all
fields in the table). When we go to retrieve rows from our table,
the row iterator returns </font><font face="Fixedsys">variant_row
</font><font face="Times Roman">objects. Essentially, </font><font
face="Fixedsys">variant_row </font><font face="Times Roman">is an
array of varying types designed to hold the fields from our query.
</font><font face="Fixedsys">variant_row </font><font
face="Times Roman">is constructed when the query is first
executed, at which time the view interrogates the database in
order to find out the number and types of fields that will be
returned. Here we use three methods from </font><font
face="Fixedsys">variant_row </font><font face="Times Roman">in
order to display our results.<br>
First, we call </font><font face="Fixedsys">GetNames() </font><font
face="Times Roman">in order to obtain a vector of the field names
in our query. To retrieve the field names, we must first
initialize a </font><font face="Fixedsys">variant_row </font><font
face="Times Roman">object from the view:<br>
<br>
</font><font face="Fixedsys">variant_row s(view.DataObj());<br>
<br>
</font><font face="Times Roman">It is crucial that we initialize
all </font><font face="Fixedsys">variant_row </font><font
face="Times Roman">objects that we want to use from our view
class. This is because a single </font><font face="Fixedsys">variant_row
</font><font face="Times Roman">object is shared by all dynamic
views and therefore they have to initialize their particular
version at runtime to tell </font><font face="Fixedsys">variant_row
</font><font face="Times Roman">what fields it will need to hold
from the query. The second method that we use from </font><font
face="Fixedsys">variant_row </font><font face="Times Roman">is
the </font><font face="Fixedsys">size() </font><font
face="Times Roman">method. This returns the number of fields in
our row. Finally, we access individual fields within a row via
the </font><font face="Fixedsys">[] </font><font
face="Times Roman">operator. The </font><font face="Fixedsys">[] </font><font
face="Times Roman">operator returns a </font><font
face="Fixedsys">variant_field </font><font face="Times Roman">object
that we can use to read, write or print individual fields.
Individual fields may be specified by either field name or field
number. To illustrate, we continue with a second example that
uses DynamicIndexedDBView. What this example does is to repeat
the IndexedViewExample code shown above; but it uses a </font><font
face="Fixedsys">variant_row </font><font face="Times Roman">object
to do all its work rather than a specialized Example class.<br>
<br>
<strong><pre><code><span class="codeComment">// Using a DynamicIndexedDBView to read, update and insert records in a database.

// Dynamic IndexedDBView example

// ... classes as in </code><a href="IndexedDBViewExample.htm"><code>IndexedDBView example</code></a> <code> ....</span>

void DynamicIndexedViewExample()
{
 DynamicDBView&lt;ParamObjExample&gt; dynamic_view(&quot;DB_EXAMPLE&quot;,
	 &quot;INT_VALUE, STRING_VALUE, DOUBLE_VALUE, EXAMPLE_LONG,  EXAMPLE_DATE&quot;,
	 &quot;WHERE INT_VALUE BETWEEN (?) AND (?) OR &quot;
	  &quot;STRING_VALUE = (?) OR EXAMPLE_DATE &lt;= (?) ORDER BY EXAMPLE_LONG&quot;,
	  BPAExampleObj());

 DynamicIndexedDBView&lt; DynamicDBView&lt;ParamObjExample&gt; &gt;  
	 indexed_view(dynamic_view, 
         &quot;UNIQUE PrimaryIndex; STRING_VALUE;&quot;
         &quot;IndexLongDate; EXAMPLE_LONG, EXAMPLE_DATE&quot;,
	 BOUND, USE_ALL_FIELDS, cb_ptr_fun(SetParamsExample));

 <span class="codeComment">// Find the item where the STRING_VALUE matches the string  &quot;Foozle&quot;</span>
 DynamicIndexedDBView&lt; DynamicDBView&lt;ParamObjExample&gt; &gt;::indexed_iterator idxview_it =  indexed_view.find(string(&quot;Foozle&quot;));


 <span class="codeComment">// Update the item with the key of &quot;Foozle&quot;, to read  &quot;Fizzle&quot; instead</span>
 if (idxview_it != indexed_view.end()) {
  variant_row replacement;
  replacement = *idxview_it;
  replacement[&quot;STRING_VALUE&quot;] =   string(&quot;Fizzle&quot;);
  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;
 DynamicIndexedDBView&lt; DynamicDBView&lt;ParamObjExample&gt; &gt;::indexed_pair 
   pr = indexed_view.equal_range_AK(&quot;IndexLongDate&quot;, long_criteria, date_criteria);

 idxview_it = pr.first;

 cout << "*** Size before erase calls: " &lt;&lt; indexed_view.size() &lt;&lt; " ***" &lt;&lt; endl; 

 <span class="codeComment">// Remove all rows that matched 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>
   DynamicIndexedDBView&lt; DynamicDBView&lt;ParamObjExample&gt; &gt;::indexed_iterator deleteMe="idxview_it;" 
   idxview_it++; 
   indexed_view.erase(deleteMe); 
 } 
 cout &lt;&lt; "*** Size after erase calls: " &lt;&lt; indexed_view.size() &lt;&lt; " ***" &lt;&lt; endl; 
 
 <span class="codeComment">// Finally, insert a new item into the container </span>
 pair&lt;DynamicIndexedDBView&lt; DynamicDBView&lt;ParamObjExample&gt; &gt;::iterator, bool&gt; ins_pr; 
 
 variant_row r(indexed_view.DataObj()); 
 r["INT_VALUE"]=459; 
 r["STRING_VALUE"]=string(&quot;Unique String #1&quot;); 
 r["DOUBLE_VALUE"]=3.5; 
 r["EXAMPLE_LONG"]=1; 
 r["EXAMPLE_DATE"]=date_criteria; 
 ins_pr=indexed_view.insert(r);
 cout &lt;&lt; "insertion succeded=" &lt;&lt;  (ins_pr.second == true ? " true": " false") &lt;&lt; endl; 
}</code></pre>

 </strong><br>
</font><a name="_Toc500425988"></a><font face="Arial"><em><strong>Using
STL Algorithms, the Table Difference Function:</strong></em><b><i><br>
</i></b></font><font face="Times Roman">As a final example, we
show how our library's compliance with the STL standards allows
us to take easy advantage of native STL algorithms. If we pass
two table containers to the function below, it can use the
standard STL algorithms to easily perform a 'difference'
operation showing any changed records in the tables.</font></p>

<pre><font face="Times Roman"><code><strong>
<span class="codeComment">// Table difference function.
// Takes two containers and prints out the differences (via set difference) between the containers.
// container 1 = &quot;original&quot; values, container 2 = &quot;new&quot; values</span>
template&lt;class Container&gt; void TableDiff(ostream &amp;o, const Container &amp;cont1, const Container &amp;cont2)
{
   typedef Container::value_type value_type;

   <span class="codeComment">// copy container data into sets as set_symmetric_difference needs a sorted list to do its work</span>
   multiset&lt;value_type&gt; set1;
   multiset&lt;value_type&gt; set2;

   <span class="codeComment">// Slight workaround here, M$ compiler 6.0 STL library can only work with pointers not iterators
   // Therefore, cannot do this at set construction time as recommended by the standard</span>
   copy(cont1.begin(), cont1.end(), inserter(set1, set1.begin()));
   copy(cont2.begin(), cont2.end(), inserter(set2, set2.begin()));

   <span class="codeComment">// Show set1 - set2 = deleted / changed items</span>
   o &lt;&lt; &quot;deleted / changed items:&quot; &lt;&lt; endl;
   set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(),
				  ostream_iterator&lt;value_type&gt;(o, &quot;\n&quot;));
  
   <span class="codeComment">// Show set2 - set1 = inserted / changed items</span>
   o &lt;&lt; &quot;inserted / changed items:&quot; &lt;&lt; endl;
   set_difference(set2.begin(), set2.end(), set1.begin(), set1.end(),
				  ostream_iterator&lt;value_type&gt;(o, &quot;\n&quot;));

#if 0
   <span class="codeComment">// Show all differences as single set</span>
   set_symmetric_difference(set1.begin(), set1.end(), set2.begin(), set2.end(),
				  ostream_iterator&lt;value_type&gt;(o, &quot;\n&quot;));
#endif
}
</strong></code></font></pre>

<pre><font face="Times Roman"><code><strong>
<span class="codeComment">// Show the difference between the rows in two tables</span>
void TestTableDiff()
{
  <span class="codeComment">// Use two DBViews to directly difference the contents of two tables</span>
  DBView&lt;Example&gt; new_table(&quot;DB_EXAMPLE&quot;);
  DBView&lt;Example&gt; old_table(&quot;DB_EXAMPLE_BACKUP&quot;);
  TableDiff(cout, old_table, new_table);

  cout &lt;&lt; &quot;--- should be same for IndexedDBViews --- &quot; &lt;&lt; endl;

  <span class="codeComment">// now do the same thing for an IndexedDBView</span>
  IndexedDBView&lt;DBView&lt;Example&gt; &gt; new_idx_table(new_table, &quot;PrimaryIndex; STRING_VALUE&quot;);
  IndexedDBView&lt;DBView&lt;Example&gt; &gt; old_idx_table(old_table, &quot;PrimaryIndex; STRING_VALUE&quot;);
  TableDiff(cout, old_idx_table, new_idx_table);

}
</strong></code></font></pre>

<p><font face="Times Roman"><br>
</font><a name="_Toc500425989"></a><font face="Arial"><b><i>Conclusion:<br>
</i></b></font><font face="Times Roman">In the foregoing article
we presented an STL centric paradigm for reading, writing and
updating table data from an ODBC data source. The library we
presented is centered around the notion of representing database
table operations via standard STL iterators and containers. Our
presentation was at an overview level for these iterators and
containers; full technical details have been left to the
reference documentation that we provide with the library. The
advantage of following the STL iterator and container paradigm is
that we are able to plug our database abstractions into a wide
variety of STL algorithms for data storage, indexing and
manipulation. In addition, the C++ reflection mechanism that we
introduced to bind iterators to database tables allows us to add
powerful automatic indexing and lookup features to our container
representations.<br>
<br>
<br>
</font></p>

<hr align="left">

<p><a name="FNT1"></a></p>

<p><font face="Times Roman">2 Our variant row type uses a
template mechanism to be able to hold values of common database
types. It is loosely based on the variant_t class proposed by
Fernando Cacciola. See F. Cacciola (2000). &quot;An Improved
Variant Type Based on Member Templates,&quot; <i>C++ Users
Journal</i> Oct 2000, p. 10.</font> </p>

<p>
<hr>

<p><a href="index.htm"><img src="dtl_home.gif" alt="[DTL Home]"
width="54" height="54"></a> <br>
</p>

<p>Copyright 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?