dtl


DBView<DataObj, ParamObj>::update_iterator

Category: iterators Component type: type

Description

DBView<DataObj, ParamObj>::update_iterator is an Output Iterator that performs the updating of objects of type DataObj in a particular DBView (and thus the database). The actual DataObj that the update_iterator references is the new value that any records meeting the iterator's SQL query. The update_iterator generates this SQL query to perform the update as: "UPDATE " + tablename_from_view + "SET " + "<field1_fromBCA>=(?), <field2_fromBCA>=(?), ... " + posfix_clause_from_view. (But see BuildSpecialQry for how to override this.) The parameters in the SET clause from the BCA are automatically bound by the update_iterator to the fields in the DataObj. These parameters are set automatically upon assignment to the update_iterator.  All parameters in the postfix clause will be bound to the ParamObj. 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 update_iterator.h header file.

Example:

//Update objects in the database via an update_iterator





class Example

{

  public:                                // tablename.columnname:

	int exampleInt;                 // DB_EXAMPLE.INT_VALUE

	string exampleStr;              // DB_EXAMPLE.STRING_VALUE

	double exampleDouble;           // DB_EXAMPLE.DOUBLE_VALUE

	long exampleLong;               // DB_EXAMPLE.EXAMPLE_LONG

	TIMESTAMP_STRUCT exampleDate;   // DB_EXAMPLE.EXAMPLE_DATE



	Example(int exInt, const string &exStr, double exDouble, long exLong,

		const TIMESTAMP_STRUCT &exDate) :

	   exampleInt(exInt), exampleStr(exStr), exampleDouble(exDouble), exampleLong(exLong),

	   exampleDate(exDate)

	{ }



};



class BCAExampleObj

{

public:

	void operator()(BoundIOs &boundIOs, Example &rowbuf)

	{

		boundIOs["INT_VALUE"] == rowbuf.exampleInt;

		boundIOs["STRING_VALUE"] == rowbuf.exampleStr;

		boundIOs["DOUBLE_VALUE"] == rowbuf.exampleDouble;

		boundIOs["EXAMPLE_LONG"] == rowbuf.exampleLong;

		boundIOs["EXAMPLE_DATE"] == rowbuf.exampleDate;

	}

};



class ExampleParamObj

{

  public:

	int lowIntValue;

	int highIntValue;

	string strValue;

	TIMESTAMP_STRUCT dateValue;

};



class BPAParamObj

{

public:

	void operator()(BoundIOs &boundIOs, ExampleParamObj &paramObj)

	{

	  boundIOs[0] == paramObj.lowIntValue;

	  boundIOs[1] == paramObj.highIntValue;

	  boundIOs[2] == paramObj.strValue;

	  boundIOs[3] == paramObj.dateValue;

	}



};





// update Example object (with new values) meeting a query in the database

void UpdateData()

{ 

	// construct view

	DBView<Example, ExampleParamObj>

	   view("DB_EXAMPLE", BCAExampleObj(), 

	   "WHERE INT_VALUE BETWEEN (?) AND (?) AND "

	   "STRING_VALUE = (?) OR EXAMPLE_DATE = (?)", BPAParamObj());



	// build an updater for the view



	// *** SQL Query Generated for this update_iterator: ***

	// "UPDATE DB_EXAMPLE SET DOUBLE_VALUE = (?), EXAMPLE_DATE = (?), EXAMPLE_LONG = (?), INT_VALUE = (?), "

	// "STRING_VALUE = (?) WHERE INT_VALUE BETWEEN (?) AND (?) AND STRING_VALUE = (?) OR EXAMPLE_DATE = (?)"

	

	DBView<Example, ExampleParamObj>::update_iterator exampleUpdater = view;



	// set data fields we want to update to their desired values

	// exampleStr to "Updated" andsampleLong to 0

	Example updateMe;



	updateMe.exampleStr = "Updated";

	updateMe.exampleLong = 25;



	TIMESTAMP_STRUCT today = {2000, 9, 29, 0, 0, 0,0};



	updateMe = Example(2121, "Updated", 99.99, 25, today);



	// now set the parameters indicating which rows

	// we want the update applied

	exampleUpdater.Params().lowIntValue = 5;

	exampleUpdater.Params().highIntValue = 13;

	exampleUpdater.Params().strValue = "FindMe";



	TIMESTAMP_STRUCT paramDate = {1999, 11, 11, 0,0, 0, 0};

	exampleUpdater.Params().dateValue = paramDate;



	// execute the update

	*exampleUpdater = updateMe;

	exampleUpdater++;



	cout << exampleUpdater.GetLastCount() << " rows updated!" << endl;



	// now can perform other updates using the same updater object

	// make sure to put in your new values for both the data and parameter values

	// for the update

	// set data fields we want to update to their desired values

	// exampleStr to "Second Update" and exampleLong to 66

	TIMESTAMP_STRUCT tomorrow = {2000, 9, 30, 0, 0,0, 0};



	updateMe = Example(2222, "Second Update", 0.11111, 66, tomorrow);



	// now set the parameters indicating which rows

	// we want the update applied

	exampleUpdater.Params().lowIntValue = 21;

	exampleUpdater.Params().highIntValue = 30;

	exampleUpdater.Params().strValue = "To find";



	TIMESTAMP_STRUCT otherParamDate = {2001, 10, 31, 0, 0, 0, 0};

	exampleUpdater.Params().dateValue = otherParamDate;



	// execute the update

	*exampleUpdater = updateMe;

	exampleUpdater++;



	cout << exampleUpdater.GetLastCount() << " rows updated!" << endl;

}

Template parameters

Parameter Description Default
DataObj The type of object that will be updated in 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::update_iterator consists of a single type, DataObj.  
ParamObj The type of object that will be used to specify the postfix parameters to the DBView. 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::update_iterator() update_iterator Default constructor.
DBView::update_iterator(DBView<DataObj, ParamObj> &view, bool bPrepare = true) update_iterator See below.
DBView::update_iterator(const DBView::update_iterator&) Output Iterator The copy constructor
DBView::update_iterator& operator=(const DBView update_iterator&) Output Iterator The assignment operator
DBView::update_iterator& operator*() Output Iterator Proxy operators necessary to emulate *it = data. Return *this.
DBView::update_iterator& operator*(), DBView::update_iterator& operator=(const DataObj &data) Output Iterator Proxy operators necessary to emulate *it = data. Updates all DataObj's meeting the iterator's SQL query to the new value specified in the DataObj to the DBView. Return *this. See Note [1].
DBView::update_iterator& operator++() Output Iterator Preincrement.
const DBView::update_iterator operator++(int) Output Iterator Postincrement.
void swap(DBView::update_iterator &other) DBView::update_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::update_iterator.

Function Description
DBView::update_iterator(DBView<DataObj, ParamObj> &view, bool bPrepare = true) Creates an update_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::update_iterator &other) Swap *this with other.

Notes

[1] operator=() is the operation that actually applies the update to the database via the DBView. Each DBView::update_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 once on each call to operator=(). Note that previous versions of DTL wrote out to the database on operator++().

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.

SourceForge Logo

This site written using the ORB. [The ORB]