dtl


InsVal

Category: functors Component type: concept

Description

InsVal is a function object (this can be a wrapped function pointer if you use cb_ptr_fun_w_ret()) that is called to to validate a user defined DataObj before it is inserted into the database. The function returns true if the DataObj contains a valid set of fields, false otherwise. InsVal is called on each record inserted or updated into the database via either DBView or IndexedDBView. The default behavior of a InsVal can be found in the DefaultInsValidate<DataObj> template. This functor always assumes the object is valid and thus returns true . Through the use of template specialization, you can customize the default behavior for this functor by writing your own DefaultInsValidate<DataObj> for that class of DataObj's.

Refinement of

None.

Associated types

BoundIOs.

Example 1:

//Default InsVal function

// Insertion is by default, always valid.
// If any RI constraints are violated,
// DB will return errors when the actual insert is performed
// which occurs after InsValidate returns.
// If there are certain business rule constraints you wish to enforce, put them in
// your own InsValidate functor.
// You can also specialize this template if you wish to have different default behavior
// for your data class.
template<class DataObj> class DefaultInsValidate {
public:
bool operator()(BoundIOs &boundIOs, DataObj &rowbuf)
{
	return true;
}
};

Example 2:

// Enforce business rule for inserted rows

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

// specialization of DefaultInsValidate for Example
// can have any number of InsValidate functions for a single data class just as with
// ParamObj's ... below is general business rule constraint we wish to enforce for all Examples
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);
}
};

Example 3:

Example: InsVal for variant_row - check the variant row null flags and use this to decide whether or not to write NULL values to the database.

// this example shows how to use the null status flag information from variant_row's data fields
// to properly set the corresponding flags in the BoundIOs for a record to insert

// here we want to make sure that any NULL data in the variant_row
// gets reflected in the resulting parameters that are sent to the database
// *** Note: This example is the actual implementation of the DefaultInsValidate
// for variant_row in the DTL library ***


template<> class dtl::DefaultInsValidate<variant_row>
{
public:

	bool operator()(BoundIOs &boundIOs, variant_row &rowbuf) {

		boundIOs.ClearNull(); // clear null on all BoundIO's

		for (BoundIOs::iterator b_it = boundIOs.begin();
				b_it != boundIOs.end(); b_it++)
		{
			BoundIO &boundIO = (*b_it).second;

			if (boundIO.IsParam()) // rowbufs could have parameters in the case of sql_iterator
				try{
					if(rowbuf[boundIO.GetName()].IsNull())
						boundIO.SetNull();
					else
						boundIO.ClearNull();
				}
				catch(...) 
				{
					// This parameter # is not part of the rowbuf, lives in ParamBuf instead
					boundIO.ClearNull();
				}
			else
				if(rowbuf[boundIO.GetName()].IsNull())
					boundIO.SetNull();
				else
					boundIO.ClearNull();

		}
		return true;
	}

	~DefaultInsValidate() {};
};

Notation

X A type that is a model of InsVal
a Object of type X

Expression semantics

Name Expression Precondition Semantics Postcondition
Default constructor
X a()
  Construct the function object.  
Copy constructor
X a(const X &b)
  Copy construct the InsVal..  
Assignment operator
X& operator=(const X&b)
  Assignment copy  
Validate operator
void operator()(BoundIOs &boundIOs, DataObj &rowbuf)
  This operator takes references to a rowbuf holding a user defined data object and its related boundIOs. The job of the operator is to validate this DataObj. On exit, the operator should return true if it was able to validate the data object, false otherwise. The fields in DataObj should contain valid values if the function returns true.

Notes

None.

See also

BoundIOs, BCA, BPA, DBView, IndexedDBView


[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