dtl
Category: iterators | Component type: type |
DBView<DataObj, ParamObj>::insert_iterator is an Output Iterator that performs the insertion of objects of type DataObj to a particular DBView (and thus the database). The insert_iterator generates the following SQL statement to insert records into the database: "INSERT INTO " + tablename_from_view + " (<field1_from_BCA>, <field2_from_BCA>, ...)" + " VALUES " + "((?), (?), ...)". (But see BuildSpecialQry for how to override this.) Unlike the other subcalsses of DB_iterator, insert_iterators do not make any use of ParamObj or BPA as INSERT statements should never take a postfix clause. Note that all of the restrictions of an Output Iterator must be obeyed, including the restrictions on the ordering of operator* and operator++ operations.
Defined in the insert_iterator.h header file.
// Using a DBView to insert rows into a database
// ... Class definitions for Example and BCAExample as per our ReadData 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
template<> class dtl::DefaultInsValidate<Example>
{
public:
bool operator()(BoundIOs &boundIOs, Example &rowbuf) {
// data is valid if rowbuf.exampleStr is nonempty and
// rowbuf.exampleDouble is
// between 0 and 100 (like a percentage)
return (rowbuf.exampleStr.length() > 0 && rowbuf.exampleDouble >= 0.0
&& rowbuf.exampleLong <= 100.0);
}
};
// Insert rows from the vector<Example> parameter into the database
void WriteData(const vector<Example> &examples)
{
DBView<Example> view("DB_EXAMPLE");
DBView<Example>::insert_iterator write_it = view;
// loop through vector and write Example objects to DB
copy(examples.begin(), examples.end(), write_it);
}
Parameter | Description | Default |
---|---|---|
DataObj | The type of object that will be written to the DBView. This object will be bound through use of the BCA to the appropriate columns in the database. The set of value types of an DBView::insert_iterator consists of a single type, DataObj. | |
ParamObj | The type of object that will be used to specify the postfix parameters to the DBView. See Note [2]. | DefaultParamObj<DataObj> |
DataObj and ParamObj must each fulfill the following requirements:.
DB_iterator<DataObj, ParamObj>, iterator<output_iterator_tag, DataObj>
Member | Where defined | Description |
---|---|---|
DBView::insert_iterator() | insert_iterator | Default constructor. |
DBView::insert_iterator(DBView<DataObj, ParamObj> &view, bool bPrepare = true) | insert_iterator | See below. |
DBView::insert_iterator(const DBView::insert_iterator&) | Output Iterator | The copy constructor |
DBView::insert_iterator& operator=(const DBView insert_iterator&) | Output Iterator | The assignment operator |
DBView::insert_iterator& operator*() | Output Iterator | Proxy operators necessary to emulate *it = data. Return *this. |
DBView::insert_iterator& operator=(const DataObj &data) | Output Iterator | Proxy operators necessary to emulate *it = data. Return *this. Writes the DataObj to the DBView. See Note [1]. |
DBView::insert_iterator& operator++() | Output Iterator | Preincrement. |
const DBView::insert_iterator operator++(int) | Output Iterator | Postincrement |
void swap(DBView::insert_iterator &other) | DBView::insert_iterator | See below. |
These members are not defined in the Output Iterator requirements or in DB_iterator<DataObj, ParamObj>, but are specific to DBView::insert_iterator.
Function | Description |
---|---|
DBView::insert_iterator(DBView<DataObj, ParamObj> &view, bool bPrepare = true) | Creates an insert_iterator which refers to view. Set bPrepare to true if you want the iterator to use SQLPrepare() and SQLExecute() in the event you are executing the statement many times. Otherwise, the iterator will use SQLExecDirect(). |
void swap(DBView::insert_iterator &other) | Swap *this with other. |
[1] This is the operation that actually writes the DataObj to the database via the DBView. Each DBView::insert_iterator internally owns a DBStmt 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 DBStmt is executed on each call to operator=(). Note that previous versions of DTL wrote out to the database on operator++().
[2] As INSERT's do not take a postfix clause, these features do nothing meaningful on an insert_iterator ... in matter of fact, DTL ignores the postfix clause on insert operations.
DB_iterator,
Output
Iterator, Input
Iterator.
Copyright © 2002, Michael Gradman and 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 and Michael Gradman make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.