OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME |
This header in included in ooftst2, 3, 4, 6, 9, 15 and 26.
It provides the declarations for the classes for two tables that are used in many of the subsequent tests and is suitable for demonstrating relationships.
Include file:
#include "oofios.h" // an easy way to get your stream libraries, regardless of platform
Declaring a relationship in the class
Type of relationship Need to know more?
The first thing we must do is declare the type of the relationship (whether 1:1, 1:N or M:N ). In the case below, we have a 1:N relationship, so there are many visits for each patient, but only 1 patient for each visit.
To declare the classes, we need two commands. These are DECLARE_REF and
DECLARE_SET.
DECLARE_REF is used for the 1 part of relationship, so the patients half of this
particular example. It essentially defines a reference for this table's records.
DECLARE_SET is used for the N or M part of relationships, so the visits part of
this example. It essentially declares a reference for sets of this table's
records.
Testing: Function DECLARE_REF
Testing: Function DECLARE_SET
DECLARE_REF(dbPatients) DECLARE_SET(dbVisits)
Now we are able to declare the classes for the tables.
Declaring the join field (multiple records)
Below, we declare the class for patients and add in 4 fields. Note that one of the fields is of type 'dbVisitsSet'. This is the field that we can reference related records through. It is a reference to the set of "visits" records related to this one.
DECLARE_CLASS(dbPatients) dbChar LastName, Othernames; dbVisitsSet Visits; dbLong PatientNo; dbLong Salary;
Now, we give a size and name characteristic (and index where appropriate) to each field. Note that we do not give any such information to the "Visits" field.
dbPatients() : dbTable("Patients"),
LastName(40, "Last Name", kIndexed),
Othernames(80, "Other names", kIndexed),
PatientNo("PatientNo", kIndexed),
Salary("Salary", kIndexed)
However, we do declare that this field is a join field and tell it what field it joins through (ie "PatientNo"). The related records will be a part of this record and can be referenced through this field using a pointer reference (eg Patient.Visits->Why).
Testing: declaration of join field of relationship
{
Visits.joinField(PatientNo);
};
These procedures will be for adding records to the table. Note that we include a procedure for adding the related visits.
void Add(const char *lname, const char *oname, const long salary); void AddVisit(const char* visitDate, const char* why); void AddTestData();
};
Declaring the join field (single record)
Now, we declare the class for the other table, including a field for the related record. Remember, that while there was a set of records in the other class, there will be only one record related to each of these ones.
DECLARE_CLASS(dbVisits) dbPatientsRef Patient; dbLong PatientNo; dbDate VisitDate; dbChar Why;
We again give information about the fields -> name, size and index (if any). We do not give this information for the related field of "Patient".
dbVisits() : dbTable("Visits"),
PatientNo("PatientNo", kIndexed),
VisitDate("VisitDate", kIndexed),
Why(200, "Reason for Visit", kIndexCompress)
We do, however, declare what field consitutes the joining field in the relationship. Note that this is also "PatientNo". Both records join at this field. See above for declaration of a class with a multi-record join field
{
Patient.joinField(PatientNo);
};
Note that we have not included any procedures for adding data. All of this will be done through the other class.
};
This procedure adds a Patient's record to the "Patients" table. It merely takes data for each field and assigns it to the field, then saves the record.
void dbPatients::Add(const char *lname, const char *oname, const long salary)
{
newRecord();
LastName = lname;
Othernames = oname;
Salary = salary;
PatientNo = (long) sequenceNumber();
saveRecord();
}
This procedure adds a visit record to the "Visits" table, but does it by referencing through the "Visits" field on the patient record that it is related to. Note how the references are made with a pointer to the field in the visit record. Note, here, that we do not call saveRecord(). We must remember to do this at a later date.
Testing: addition of related record through relationship
void dbPatients::AddVisit(const char* visitDate, const char* why)
{
Visits->newRecord();
Visits->VisitDate = visitDate;
Visits->Why = why;
}
This procedure serves to add a small database for test purposes. Note how each patient is added, followed by their visits and then we declare saveRecord() so the informatin is actually saved.
void dbPatients::AddTestData()
{
If using SmartHeap, debugging the following can take long enough to make folks wonder. There are a *lot* of places where OOFILE calls SmartHeap to check memory.
Each record, below, has a different format of date entered into the date field. This tests the readablilty of different date formats. More date field tests.
Here, we add a relatively average record.
Add("Smith", "John", 20000);
AddVisit("1/10/1994", "Sore Knee");
AddVisit("14/10/1994", "Measles");
saveRecord();
Here, a numeric field in the patient record is large so we can later test overflow problems
Add("DENT", "Trissa", 99999);
AddVisit("23-11-1994", "Flu");
saveRecord();
This is also a relatively average record.
Add("Dent", "Andy", 50000);
AddVisit("4.10.1994", "Flu");
saveRecord();
This record has no visits so we can test what happens when there are no records to access through the relationship.
Add("Taylor", "Ken", 75000);
}
(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001