dtl
Category: functors | Component type: concept |
SelVal 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 as it is read from the database. The function returns true if the DataObj contains a valid set of fields, false otherwise. SelVal is called on each record read from a database via either DBView or IndexedDBView. The default behavior of a SelVal can be found in the DefaultSelValidate<DataObj> template. This particular functor returns true if all of the fields read into the BoundIOs have a value (that is, all fetched columns are non-NULL, tested using BoundIO::IsNull()), otherwise it returns false. Through the use of template specialization, you can customize the default behavior for this functor by writing your own DefaultSelValidate<DataObj> for that class of DataObj's.
None.
//Default SelVal function to make sure fields in a row selected from the database are valid
// Default select validation behavior ... data is valid if and only if
// there are no columns which are null.
// If there are other checks you wish to make, put them in
// your own SelVal functor.
// You can also specialize this template if you wish to have different default behavior
// for your data class.
template<class DataObj> class DefaultSelValidate {
public:
bool operator()(BoundIOs &boundIOs, DataObj &rowbuf)
{
for (BoundIOs::iterator b_it = boundIOs.begin();
b_it != boundIOs.end(); b_it++)
{
BoundIO &boundIO = (*b_it).second;
if (boundIO.IsColumn() && boundIO.IsNull())
return false; // found null column ... data is invalid
}
return true; // no nulls found ... data is OK
}
};
// Assign valid defaults for null values
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
};
// Validation behavior, assign default values for NULL fields
template<> class dtl::DefaultSelValidate<Example>
{
public:
bool operator()(BoundIOs &boundIOs, Example &rowbuf) {
if (boundIOs["INT_VALUE"].IsNull()) {
rowbuf.exampleInt = 0;
}
if (boundIOs["STRING_VALUE"].IsNull()) {
rowbuf.exampleStr = "";
}
if (boundIOs["DOUBLE_VALUE"].IsNull()) {
rowbuf.exampleDouble = 0;
}
if (boundIOs["EXAMPLE_LONG"].IsNull()) {
rowbuf.exampleLong = 0;
}
if (boundIOs["EXAMPLE_DATE"].IsNull()) {
const TIMESTAMP_STRUCT defaultDate = {2000, 1, 1, 0, 0, 0, 0};
rowbuf.exampleDate = defaultDate;
}
// Now check that values are in acceptable range
// Return false/failure if values out of range
if (rowbuf.exampleDouble > 100)
return false;
return true; // data is OK
}
};
// This function is a specialized version of DefaultSelValidate to copy
// information about NULL columns from the BoundIOs structure to the variant_row class
template<> class DefaultSelValidate<variant_row> {
public:
bool operator()(BoundIOs &boundIOs, variant_row &rowbuf)
{
rowbuf.ClearNulls();
for (BoundIOs::iterator b_it = boundIOs.begin();
b_it != boundIOs.end(); b_it++)
{
BoundIO &boundIO = (*b_it).second;
if ((boundIO.IsColumn() || boundIO.GetParamType() == SQL_PARAM_OUTPUT || boundIO.GetParamType() == SQL_PARAM_INPUT_OUTPUT)
&& boundIO.IsNull())
rowbuf.SetNull(boundIO.GetName()); // found null column ... record null status in rowbuf
}
return true; // assume data is OK
}
};
X | A type that is a model of SelVal |
a | Object of type X |
Name | Expression | Precondition | Semantics | Postcondition |
---|---|---|---|---|
Default constructor | X a() |
Construct the function object. | ||
Copy constructor | X a(constX &b) |
Copy construct the SelVal. | ||
Assignment operator | X& operator=(const X&b) |
Assignment copy | ||
Validate operator | void operator()(BoundIOs &boundIOs, DataObj &rowbuf)) |
This operator takes a BoundIOs object and a reference to a rowbuf holding a user defined data object. The job of the function is to validate the user defined data object and/or make any default assignments in the data object in the case of NULL fields. On exit, the function should return true if it was able to validate the data object, false otherwise. | The fields in DataObj that are bound to query parameters should be initialized with valid values if the function returns true. |
None.
BoundIOs, BCA, BPA, DBView, IndexedDBView
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.