Step 1: Using the Date as Object Mode

In its default configuration, DB Link handles the date as a string. This is referred to as the date as object mode. In this mode, date strings must respect the format expected by the RDBMS. This format varies depending on the RDBMS being connecting to. Also, this format depends on the LOCALE setting.

Note

Some RDBMSs can handle time with milliseconds. The “date as string” mode does not allow you to get these milliseconds, whereas “date as object” does. Consequently, the “date as object” mode respects the precision of the data returned by the RDBMS.

To avoid these dependencies, DB Link provides a class (IldDateTime) to record a date. This object may be used to send or retrieve a date to or from the RDBMS. DB Link silently converts this to what is expected by the RDBMS (a specific structure).

This IldDateTime class provides logical accessors to build the date value. The sample PortStep1.cpp shows how to use this class to record date values to a table.

Since the default mode for dates is date as string, you first switch to date as object mode. To do so, the IldIldBase::setStringDateUse(IldBoolean) method is used:

request->setStringDateUse(IldFalse) ;

IldDateTime objects can now be used.

An insert query is run to insert new date values in a table. An array of parameters is used, with external binding, as described in DB Link Optimization Techniques.

if (!request->parse(insertStr))

IldDisplayError("Could not parse insert query", request) ;

 

if (!request->bindParam((IldUShort)0, IldDateTimeType, sizeof(IldDateTime),

dates, dateNulls))

IldDisplayError("Bind parameter failed : ", request) ;

The values for the date parameters are now set. This is done using the intuitive interface provided by the IldDateTime class.

for (i = 0 ; i < nbParam ; ++i) {

dates[i].setYear(1999) ;

dates[i].setMonth(10) ;

dates[i].setDay(i + 1) ;

dates[i].setHour(10) ;

dates[i].setMinute(30) ;

}

The query is now executed:

if (!request->execute(&rowCount, nbParam)) {

IldDisplayError("Could not execute insert query : ", request) ;

Ending(dbms) ;

exit(1) ;

}

Then the data is retrieved using the method displayData. This is done in string mode, since the values need only to be printed. Using this sample, you can check how the date strings will be affected by the RDBMS format and the LOCALE settings.

Conclusion

This step demonstrated the use of the IldDateTime class. This class provides an intuitive way to handle dates and exchange date data with the RDBMS, regardless of LOCALE settings. Without DB Link, this requires knowledge of the specific structures used by the RDBMS API. Such code is complicated and is not portable to other RDBMSs.

See source code.