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