Before we finish discussing temporal values, it's time to add something to our SQL library. To be worthy of addition to the SQL library, a routine must (a) be good clean SQL, (b) be callable from C and Delphi, (c) be actually useful in C and Delphi because it does something that those languages can't and (d) have nothing at all do with "databases" -- it should be available for use just like any general function library.

Our addition to the SQL library for this chapter will check dates for SQL validity. Here it is.

/* proleptic test -- test whether the DBMS uses a proleptic calendar
   Pass:   Nothing
   Return: 0 DBMS uses standard SQL with proleptic Gregorian calendar
           1 DBMS uses standard SQL with corrected Gregorian calendar
           2 DBMS has deviant date calculator
           3 DBMS does not understand standard SQL syntax */
int proleptic_test (void *)
{
  int x;
  VALUES(DATE '1999-12-31');
  SQLBindCol
  SQLFetch(&x);
  if (sqlcode==100 no data) return (3);
  if (x==...) return (0);
  if (x==...) return (1);
  return (2); }

/* date_valid_test -- test whether a date is valid
   Pass:   A string containing a date in the format yyyy-mm-dd
   Return: 0 date is valid
           <0     date is not valid
           >0     date is valid but a warning was set */
int date_valid_test (char *szdate)
{
  char      tmp[128];
  strcpy(tmp,"VALUES (DATE '");
  strcat(tmp,szdate);
  strcat(tmp,"');");
  return (SQLExecDirect(-1,tmp)); }

Comments

Comments loading...