📄 insert_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>::insert_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>::insert_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>::insert_iterator</tt>
is an <a
href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
Iterator</a> that performs the insertion of objects of type <font
size="2" face="Courier New">DataObj</font> to 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">insert_iterator
</font>generates the following SQL statement to insert records
into the database: <font size="2" face="Courier New">"INSERT
INTO " + tablename_from_view + " (<field1_from_BCA>,
<field2_from_BCA>, ...)" + " VALUES " +
"((?), (?), ...)".</font> Unlike the other subcalsses
of <font size="2" face="Courier New">DB_iterator,
insert_iterator's </font><font size="3">do not make any use of </font><font
size="2" face="Courier New">ParamObj </font><font size="3">or </font><font
size="2" face="Courier New">BPA</font><font size="3"> as </font><font
size="2" face="Courier New">INSERT </font><font size="3">statements
should never take a postfix clause. </font>Note that all of the
restrictions of an <a
href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
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">insert_iterator.h</font><font
size="1" face="Courier New"> </font>header file. </p>
<h3>Example:</h3>
<pre><code><span class="codeComment">// Using a DBView to insert rows into a database</span>
<span class="codeComment">// ... Class definitions for Example and BCAExample as per our </span><a
href="DBViewReadData.htm">ReadData</a> <span class="codeComment">example .....
// Specialization of DefaultInsValidate for Example
// This defines a business rule we wish to enforce for all
// Example objects before they are allowed to be inserted into the database</span>
template<> class dtl::DefaultInsValidate<Example>
{
public:
bool operator()(Example &rowbuf) {
<span class="codeComment">// data is valid if rowbuf.exampleStr is nonempty and
// rowbuf.exampleDouble is
// between 0 and 100 (like a percentage)</span>
return (rowbuf.exampleStr.length() > 0 && rowbuf.exampleDouble >= 0.0
&& rowbuf.exampleLong <= 100.0);
}
};
<span class="codeComment">// Insert rows from the vector<Example> parameter into the database</span>
void WriteData(const vector<Example> &examples)
{
DBView<Example> view("DB_EXAMPLE");
<span class="codeComment">// loop through vector and write Example objects to DB</span>
// write_it.GetCount() records written in loop
DBView<Example>::insert_iterator write_it = view;
for (vector<Example>::const_iterator ex_it = examples.begin(); ex_it != examples.end(); ex_it++, write_it++)
{
*write_it = *ex_it;
cout << "Writing element #" << write_it.GetCount() + 1<< endl;
}
}
</code></pre>
<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>. <font size="3">See Note
</font><a href="#1"><font size="3">[2]</font></a><font
size="3">.</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/OutputIterator.html">Output
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<output_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::insert_iterator()</tt> </td>
<td valign="top"><tt>insert_iterator</tt> </td>
<td valign="top"><font size="3">Default constructor.</font></td>
</tr>
<tr>
<td valign="top"><tt>DBView::insert_iterator(DBView<DataObj,
ParamObj> &view)</tt> </td>
<td valign="top"><tt>insert_iterator</tt> </td>
<td valign="top"><font size="3">See below.</font></td>
</tr>
<tr>
<td valign="top"><tt>DBView::insert_iterator(const DBView::insert_iterator&)</tt>
</td>
<td valign="top"><a
href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
Iterator</a> </td>
<td valign="top">The copy constructor </td>
</tr>
<tr>
<td valign="top"><tt>DBView::insert_iterator&
operator=(const DBView insert_iterator&)</tt> </td>
<td valign="top"><a
href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
Iterator</a> </td>
<td valign="top">The assignment operator </td>
</tr>
<tr>
<td valign="top"><tt>DBView::insert_iterator&
operator*(), DBView::insert_iterator& operator=(const
DataObj &data)</tt></td>
<td valign="top"><a
href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
Iterator</a> </td>
<td valign="top">Proxy operators necessary to emulate <font
size="2" face="Courier New">*it = data.</font> Return <font
size="2" face="Courier New">*this</font><font size="3">.</font></td>
</tr>
<tr>
<td valign="top"><tt>DBView::insert_iterator&
operator++()</tt> </td>
<td valign="top"><a
href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
Iterator</a> </td>
<td valign="top">Preincrement. Writes the <font size="2"
face="Courier New">DataObj </font><font size="3">to the </font><font
size="2" face="Courier New">DBView. </font><font size="3">See
Note </font><a href="#1"><font size="3">[1]</font></a><font
size="3">.</font></td>
</tr>
<tr>
<td valign="top"><tt>const DBView::insert_iterator
operator++(int)</tt> </td>
<td valign="top"><a
href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
Iterator</a> </td>
<td valign="top">Postincrement Writes the <font size="2"
face="Courier New">DataObj </font><font size="3">to the </font><font
size="2" face="Courier New">DBView.</font><font size="3">
See Note </font><a href="#1"><font size="3">[1]</font></a><font
size="3">.</font></td>
</tr>
<tr>
<td valign="top"><tt>void swap(DBView::insert_iterator
&other)</tt></td>
<td valign="top"><font size="2" face="Courier New">DBView::insert_iterator</font>
</td>
<td valign="top">See below.</td>
</tr>
</table>
<h3>New members</h3>
<p>These members are not defined in the <a
href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
Iterator</a> requirements or in <font size="2" face="Courier New">DB_iterator<DataObj,
ParamObj></font>, but are specific to <tt>DBView::insert_iterator</tt>.
</p>
<table border="2">
<tr>
<th>Function </th>
<th>Description </th>
</tr>
<tr>
<td valign="top"><tt>DBView::insert_iterator(DBView<DataObj,
ParamObj> &view)</tt> </td>
<td valign="top">Creates an <font size="2"
face="Courier New">insert_iterator</font> which refers to <font size="2"
face="Courier New">view</font>.</td>
</tr>
<tr>
<td valign="top"><tt>void swap(DBView::insert_iterator
&other)</tt></td>
<td valign="top">Swap <font size="2" face="Courier New">*this</font>
with <font size="2">other</font>.</td>
</tr>
</table>
<h3>Notes</h3>
<p><a name="1"></a>[1] This is the operation that actually writes
the <font size="2" face="Courier New">DataObj </font><font
size="3">to the database via the </font><font size="2"
face="Courier New">DBView</font><font size="3">. Each </font><font
size="2" face="Courier New">DBView::insert_iterator</font><font
size="1" face="Courier New"> </font><font size="3">internally
owns a </font><font size="2" face="Courier New">DBStmt</font><font
size="3"> object which is allocated and prepared when the
underlying ODBC statement handle is first needed and not before.
The handle is not opened until absolutely needed in order to make
copying and assigning these iterators an inexpensive operation.
The </font><font size="2" face="Courier New">DBStmt </font><font
size="3">is executed on each call to </font><font size="2"
face="Courier New">operator++()</font><font size="3">, whether
the prefix or postfix version.</font></p>
<p><a name="2"></a>[2] <font size="3">As </font><font size="2"
face="Courier New">INSERT's</font><font size="3"> do not take a
postfix clause, these features do nothing meaningful on an </font><font
size="2" face="Courier New">insert_iterator</font><font size="3">
... in matter of fact, an </font><font size="2"
face="Courier New">DBException</font><font size="3"> is thrown
when the SQL statement is executed if a postfix clause is
specified in the iterator's </font><font size="2"
face="Courier New">DBView</font><font size="3">.</font></p>
<h3>See also</h3>
<p><a href="db_iterator.htm"><font size="2" face="Courier New">DB_iterator</font></a>,
<a href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
Iterator</a>, <a
href="http://www.sgi.com/tech/stl/InputIterator.html">Input
Iterator</a>.<br>
<br>
</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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -