dtl


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

	*exampleUpdater = updateMe;

	// 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++;

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

	*exampleUpdater = updateMe;

	// 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++;

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

[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