📄 select_iterator.htm
字号:
<html>
<head>
<style>
CODE {COLOR: #990000;}
.code{COLOR: #990000}
.codeComment{COLOR: #008000}
.codeHighlight{BACKGROUND-COLOR: #FFFF00}
.codeFileName{FONT-WEIGHT: bold;}
</style>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="Author" content="Mike Gradman">
<meta name="KeyWords"
content="DTL, Oracle, ODBC, database API, C++, Template Library">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<!--
-- Copyright 2000
-- Michael Gradman & Corwin Joy
--
-- Permission to use, copy, modify, distribute and sell this software
-- and its documentation for any purpose is hereby granted without fee,
-- provided that the above copyright notice appears in all copies and
-- that both that copyright notice and this permission notice appear
-- in supporting documentation. Corwin Joy & Michael Gradman make no
-- representations about the suitability of this software for any
-- purpose. It is provided "as is" without express or implied warranty.
--
--
-- Copyright (c) 1996-1999
-- Silicon Graphics Computer Systems, Inc.
--
-- Permission to use, copy, modify, distribute and sell this software
-- and its documentation for any purpose is hereby granted without fee,
-- provided that the above copyright notice appears in all copies and
-- that both that copyright notice and this permission notice appear
-- in supporting documentation. Silicon Graphics makes no
-- representations about the suitability of this software for any
-- purpose. It is provided "as is" without express or implied warranty.
--
-- Copyright (c) 1994
-- Hewlett-Packard Company
--
-- Permission to use, copy, modify, distribute and sell this software
-- and its documentation for any purpose is hereby granted without fee,
-- provided that the above copyright notice appears in all copies and
-- that both that copyright notice and this permission notice appear
-- in supporting documentation. Hewlett-Packard Company makes no
-- representations about the suitability of this software for any
-- purpose. It is provided "as is" without express or implied warranty.
--
-->
<!-- Generated by htmldoc -->
<title>DBView<DataObj,ParamObj>::select_iterator</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000EE"
vlink="#551A8B" alink="#FF0000">
<p><font size="6" face="Bookman Old Style"><em><strong><u>dtl</u></strong></em></font></p>
<p><img src="stat.gif" width="6" height="6"> <!--end header--> <br>
</p>
<h1>DBView<DataObj, ParamObj>::select_iterator</h1>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img src="iterator.gif" width="194" height="38"></td>
<td align="right"><img src="type.gif" width="194"
height="39"></td>
</tr>
<tr>
<td valign="top"><b>Category</b>: iterators</td>
<td align="right" valign="top"><b>Component type</b>:
type</td>
</tr>
</table>
<h3>Description</h3>
<p><tt>DBView<DataObj, ParamObj>::select_iterator</tt> is
an <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input
Iterator</a> that performs the reading of objects of type <font
size="2" face="Courier New">DataObj</font> from a particular <font
size="2" face="Courier New">DBView</font><font size="2"> </font>(and
thus the database). The <font size="2" face="Courier New">select_iterator
</font>generates the following SQL statement to read records from
the database: <font size="2" face="Courier New">"SELECT
" + "<field1_fromBCA>, <field2_fromBCA>,
... " + "FROM " +
"<tablename1_from_view>, <tablename2_from_view>,
... " + posfix_clause_from_view. </font>Note that all of the
restrictions of an <a
href="http://www.sgi.com/tech/stl/InputIterator.html">Input
Iterator</a> must be obeyed, including the restrictions on the
ordering of <tt>operator*</tt> and <tt>operator++</tt>
operations. </p>
<h3>Definition</h3>
<p>Defined in the <font size="2" face="Courier New">select_iterator.h
</font>header file. </p>
<h3>Example:</h3>
<h3>Accessing a Table in Four Easy Steps:</h3>
<p><font face="Times Roman"><br>
<strong>1. Define an object to hold the rows from your query.</strong></font></p>
<p><font face="Times Roman"><strong>2. Define an association
between fields in your query and fields in your object. This is
what we call a 'BCA', which is short for Bind Column Addresses.
In the example below, this is done via the functor "BCAExample".
The job of the BCA is to equate SQL fields with object fields via
the '==' operator which will then establish ODBC bindings to move
data to or from a user query.</strong></font></p>
<p><font face="Times Roman"><strong>3. Create a view to select
records from. This view is built from the template DBView and
establishes which table(s) you want to access, what fields you
want to look at (via the BCA), and an optional where clause to
further limit the set of records that you are working with. The
DBView template forms a semi-Container in the STL sense.</strong></font><a
href="#FNT0"><font size="2" face="Times Roman"><sup><strong>1</strong></sup></font></a><font
face="Times Roman"><strong>.</strong></font></p>
<p><font face="Times Roman"><strong>4. Use the DBView container
to obtain an iterator to SELECT, INSERT, UPDATE or DELETE records
from your view. These iterators may be used to either populate
STL containers or apply algorithms from the Standard Template
library.<br>
</strong></font></p>
<pre><code>
In all the examples that follow we will assume that our database contains a table called DB_EXAMPLE of the form
SQL> desc db_example;
Name Type
------------------------------- --------
INT_VALUE INTEGER
STRING_VALUE VARCHAR
DOUBLE_VALUE FLOAT
EXAMPLE_LONG INTEGER
EXAMPLE_DATE DATE
<span class="codeComment">// STEP 1 ////
// "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">// STEP 2 ////</span>
<span class="codeComment">// Create an association between table columns and fields in our object</span>
template<> class dtl::DefaultBCA<Example>
{
public:
void operator()(BoundIOs &cols, Example &rowbuf)
{
cols["INT_VALUE"] == rowbuf.exampleInt;
cols["STRING_VALUE"] == rowbuf.exampleStr;
cols["DOUBLE_VALUE"] == rowbuf.exampleDouble;
cols["EXAMPLE_LONG"] == rowbuf.exampleLong;
cols["EXAMPLE_DATE"] == rowbuf.exampleDate;
}
}
<span class="codeComment">// STEP 3 & 4
// Read the contents of the DB_EXAMPLE table and return a vector of the
// resulting rows</span>
vector<Example> ReadData() {
// Read the data
vector<Example> results;
DBView<Example> view("DB_EXAMPLE");
DBView<Example>::select_iterator read_it = view.begin();
for ( ; read_it != view.end(); read_it++)
{
results.push_back(*read_it);
}
return results;
}
</code></pre>
<p><a name="FNT0"></a></p>
<p><font face="Times Roman">1</font><font face="Arial"> </font><font
face="Times Roman">See </font><a
href="http://www.sgi.com/tech/stl/Container.html"><font
color="#0000FF" face="Times Roman"><u>http://www.sgi.com/tech/stl/Container.html</u></font></a><font
color="#0000FF" face="Times Roman"> </font><font
face="Times Roman">for the definition of an STL container, we
call DBView a <b>semi</b> container because it supports all
standard container methods <b>except</b> size(), max_size() and
empty(). We explain why these were left out by design in the
documentation for the DBView template. <br>
</font></p>
<h3>Template parameters</h3>
<table border="2">
<tr>
<th>Parameter </th>
<th>Description </th>
<th>Default </th>
</tr>
<tr>
<td valign="top"><tt>DataObj</tt> </td>
<td valign="top">The type of object that will be written
to the <font size="2" face="Courier New">DBView</font>.
This object will be bound through use of the <a
href="BCA.htm"><font size="1" face="Courier New">BCA</font></a><font
size="2"> </font>to the appropriate columns in the
database. The set of value types of an <tt>DBView::insert_iterator</tt>
consists of a single type, <font size="2"
face="Courier New">DataObj</font>. </td>
<td valign="top"> </td>
</tr>
<tr>
<td valign="top"><tt>ParamObj</tt> </td>
<td valign="top">The type of object that will be used to
specify the postfix parameters to the <font size="2"
face="Courier New">DBView</font>.</td>
<td valign="top"><font size="2" face="Courier New">DefaultParamObj<DataObj></font>
</td>
</tr>
</table>
<h3>Model of</h3>
<p><a href="http://www.sgi.com/tech/stl/InputIterator.html">Input
Iterator</a> </p>
<h3>Type requirements</h3>
<p><font size="2" face="Courier New">DataObj </font>and <font
size="2" face="Courier New">ParamObj</font> must each fulfill the
following requirements:. </p>
<ul>
<li>Be of a type that is not primitive or of type <font
size="2" face="Courier New">string</font><font size="2">.</font></li>
<li><font size="3">Have a publicly accessible copy
constructor (the default is OK).</font></li>
<li><font size="3">Have a publicly accessible assignment
operator (the default is OK).</font></li>
</ul>
<h3>Public base classes</h3>
<p><font size="2" face="Courier New">DB_iterator<DataObj,
ParamObj>, iterator<input_iterator_tag, DataObj></font></p>
<h3>Members</h3>
<table border="2">
<tr>
<th>Member </th>
<th>Where defined </th>
<th>Description </th>
</tr>
<tr>
<td valign="top"><tt>DBView::select_iterator()</tt> </td>
<td valign="top"><tt>select_iterator</tt> </td>
<td valign="top"><font size="3">Default constructor.</font></td>
</tr> <tr>
<td valign="top"><tt>DBView::select_iterator(DBView<DataObj,
ParamObj> &view)</tt> </td>
<td valign="top"><tt>select_iterator</tt> </td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -