dtl


DBView<DataObj, ParamObj>::insert_iterator

Category: iterators Component type: type

Description

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.

Definition

Defined in the insert_iterator.h header file.

Example:

// 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");

	// loop through vector and write Example objects to DB
	// 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;
	}
}

Template parameters

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> 

Model of

Output Iterator.

Type requirements

DataObj and ParamObj must each fulfill the following requirements:.

Public base classes

DB_iterator<DataObj, ParamObj>, iterator<output_iterator_tag, DataObj>

Members

Member Where defined Description
DBView::insert_iterator() insert_iterator Default constructor.
DBView::insert_iterator(DBView<DataObj, ParamObj> &view) 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*(), DBView::insert_iterator& operator=(const DataObj &data) Output Iterator Proxy operators necessary to emulate *it = data. Return *this.
DBView::insert_iterator& operator++() Output Iterator Preincrement. Writes the DataObj to the DBView. See Note [1].
const DBView::insert_iterator operator++(int) Output Iterator Postincrement Writes the DataObj to the DBView. See Note [1].
void swap(DBView::insert_iterator &other) DBView::insert_iterator See below.

New members

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) Creates an insert_iterator which refers to view.
void swap(DBView::insert_iterator &other) Swap *this with other.

Notes

[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++(), whether the prefix or postfix version.

[2] As INSERT's do not take a postfix clause, these features do nothing meaningful on an insert_iterator ... in matter of fact, an DBException is thrown when the SQL statement is executed if a postfix clause is specified in the iterator's DBView.

See also

DB_iterator, Output Iterator, Input Iterator.


[DTL Home]

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.

This site written using the ORB. [The ORB]

1