OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME |
This sample tests date format masks for output control plus Boolean and Char calculated fields. More about calculated fields CalcFields or a test sample ooftst29
#include "oofile.h" // the general oofile library
This procedure defines the calculater for our boolean field. The methods will be defined later.
class overdueCalc : public dbBoolCalculater{
public:
virtual dbCalculater* clone() const {return new overdueCalc(*this);};
virtual bool calc(const dbBool*);
};
This procedure defines the calculater for our char field. The methods will be defined later.
class statusCalc : public dbCharCalculater{
public:
virtual dbCalculater* clone() const {return new statusCalc(*this);};
virtual const char* calc(const dbChar*);
};
Now we define our table class. We have two ordinary fields as well as the two calculated fields.
DECLARE_CLASS(dbInvoicing)
dbUlong InvoiceNumber;
dbDate DateDue;
These are our two calculated fields.
dbBool IsOverdue;
dbChar Status;
dbInvoicing() : dbTable() ,
InvoiceNumber("Invoice Number"),
DateDue("Date Due"),
IsOverdue("Is Overdue"),
Status(0, "Status") // use 0 length as doesn't matter for calc field
{
Note that we must declare the calculater to use on these fields.
IsOverdue.calculateWith(new overdueCalc);
Status.calculateWith(new statusCalc);
We also set the default sort order for this table.
setSortOrder(dbSorter() << DateDue << InvoiceNumber);
};
void addTestData();
void addInvoice(unsigned long invNumber, const char* due);
};
Here we define the calculation method for the boolean calculater. It
calculates if the invoice is overdue. Normally we would do a check like
this: bool ret = (theTable->DateDue
- dbDate::currentDate()) > 31;
but we've fixed a date to make our test results predictable. Instead the
procedure finds the difference between the invoice's date and the given date and
then sees if this difference is greater than the specified time (31 days in this
case).
bool
overdueCalc::calc(const dbBool* theField)
{
dbInvoicing* theTable = (dbInvoicing*) (theField->fieldTable()); // get the table on which we operate
long daysDue = oofDate("7th May 1996") - theTable->DateDue;
bool ret = daysDue > 31;
return ret;
}
This is the calculation method for the char calculater. Note that it is dependant on the other calculated field. this determines if the invoice is overdue and returns the word "Overdue" if it is, otherwise it returns an empty string. Note that this field only needs to look at the other filed, this will automatically call the other field's calculater to recalculate the IsOverdue field
const char*
statusCalc::calc(const dbChar* theField)
{
dbInvoicing* theTable = (dbInvoicing*) (theField->fieldTable()); // get the table on which we operate
if (theTable->IsOverdue)
return "Overdue";
else
return "";
}
This procedure is for adding a single record to the database.
void
dbInvoicing::addInvoice(unsigned long invNumber, const char* due)
{
newRecord();
InvoiceNumber = invNumber;
DateDue = due;
saveRecord();
}
This procedure adds a number of records, creating our test data.
void
dbInvoicing::addTestData()
{
addInvoice(12345, "5th May 1996");
addInvoice(12349, "5th May 1996");
addInvoice(12346, "5th Feb 1996");
addInvoice(12347, "4-1-1996");
addInvoice(12348, "4-1-1996");
addInvoice(12342, "4-1-1996");
addInvoice(12343, "6th May 1996");
}
int
main()
{
cout << "OOFILE Validation Suite - Test 33\n"
<< "Show date formatting and char and bool calculaters"
<< endl << endl;
Here we declare our global variables for our table and connection, then we create our connection, adding new test data.
dbConnect_ram theDB; dbInvoicing Invoicing; theDB.newConnection(); Invoicing.addTestData();
Now we test the date format mask. This will mean that when we print out the database, all the dates will be printed in this form. it is very much like a printf format string, but with the special usage of Day, ddth, DD, Month, MMM, YY or YYYY. These correspond to the different ways of representing the parts of dates. This way, we can use whatever format we wish including the ability to determine whatever delimiters we wish. below are some sbasic examples.
Testing: dbDate format mask "ddth Month YYYY"
Invoicing.DateDue.formatMask("ddth Month YYYY");
cout << "All test records\n" << Invoicing;
We will do a search that will find the records that have overdue invoices. Note: the following is for demonstrating the bool calculater. A real database search would be better with an indexed search on the dbDate.
Testing: search on calculated bool field
cout << "\n\nSearching For Overdue records, by a calculated bool field\n"; Invoicing.search(Invoicing.IsOverdue);
Now we will two more date formats on the records we have selected.
Testing: dbDate format mask "Day, DD-MMM-YY"
Invoicing.DateDue.formatMask("Day, DD-MMM-YY");
cout << Invoicing << endl;
Testing: dbDate format mask "DD/MM/YYYY"
Invoicing.DateDue.formatMask("DD/MM/YYYY");
cout << Invoicing << endl;
cout << endl << "done" << endl; return EXIT_SUCCESS; }
(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001