home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cppsql.zip
/
SQLSRC.ZIP
/
SQLTAB.CPP
< prev
next >
Wrap
Text File
|
1992-09-18
|
35KB
|
1,250 lines
// :H1 SQLTAB.CPP: START OF SPECIFICATIONS
//----------------------------------------------------------------------
//
// Module Name: SQLTAB.CPP
//
// Description: SQL Table Interface.
//
// Product Classification:
// IBM Internal Use Only
// (C) Copyright IBM Corp. 1992
//
// Status: New
//
// Initial Author: George L. Havens
//
// Function: To provide an interface to tables for SQL databases.
//
// Notes: None.
//
// Dependencies: Requires PORTABLE.H include.
// This module requires SQL.C for the SQL C interface.
//
// Restrictions: None.
//
// Compiler: Zortech C++
//
// Change Activity -----------------------------------------------------
//
// $MOD(module) COMP(component) PROD(product): Description...
//
// FLAG REASON VERS DATE WHO DESCRIPTION
// ---- -------- ---- ------ --- -----------
// V100 920224 GLH : Initial level
//
// END-OF-SPECIFICATIONS -----------------------------------------------
// Use the following define to create a test version.
//#define TEST
// :H1 SYSTEM INCLUDE FILES. SELECTIVITY USED WHERE POSSIBLE.
#include "stdio.h"
#include "stdlib.h"
#include "stdarg.h"
#include "string.h"
#include "ctype.h"
extern "C"
{
#include "sqlda.h"
#include "sql.h"
}
// :H1 USER-DEFINED INCLUDE FILES. SELECTIVITY USED WHERE POSSIBLE.
#include "portable.h"
extern "C"
{
#include "sqlc.h"
}
#ifdef TEST
#include "sql.hpp"
#endif
#include "sqltab.hpp"
#define MAX_COL_LEN 20 // maximum column name length
// :H1 Table: Table Constructor
//----------------------------------------------------------------------
//
// Function Name: Table constructor
//
// Purpose: This method will create an SQL table object.
//
// Description: This method will create an SQL table object. It
// will create a SQLDA structure for the table
// format specified.
//
// Input:
// name Table name
// format table format
// format must be a string using %d, %ld, or %#s
// where # is the string length not including the 0 terminator
// col column names seperated by ','
// Output:
// none
//
//----------------------------------------------------------------------
Table::Table (const CHAR *name, const CHAR *format, const CHAR *col)
{
const CHAR *ptr = format;
SHORT i;
SHORT len;
// save table name
table = new CHAR [strlen (name) + 1];
strcpy (table, name);
// save column names
columns = new CHAR [strlen (col) + 1];
strcpy (columns, col);
// initialize number of columns
cols = 0;
// initialize to database not searched
record_found = FALSE;
// while % in format
while ( (ptr = strchr (ptr, '%')) != 0)
{
// update number of columns
cols++;
// skip to next character
ptr++;
}
// if no % in format
if (cols == 0)
{
// set no % in format
rc = SQLTAB_NO_PERCENT_ERROR;
id = SQLTAB_ID_1;
return;
}
// create sqlda structure
var = (struct sqlda *)new CHAR [SQLDASIZE (cols)];
var->sqln = var->sqld = cols;
// initialize format pointer
ptr = format;
// for all columns
for (i = 0; i < cols; i++)
{
// if % not found in format
if ( (ptr = strchr (ptr, '%')) == 0)
break;
// skip to character following %
ptr++;
// if small integer format
if (*ptr == 'd')
{
// set up variable data area
var->sqlvar[i].sqltype = SQL_TYP_SMALL;
var->sqlvar[i].sqldata = 0;
var->sqlvar[i].sqllen = sizeof (SHORT);
var->sqlvar[i].sqlind = 0;
}
// else if long integer format
else if (*ptr == 'l' && *(ptr + 1) == 'd')
{
// set up variable data area
var->sqlvar[i].sqltype = SQL_TYP_INTEGER;
var->sqlvar[i].sqldata = 0;
var->sqlvar[i].sqllen = sizeof (LONG);
var->sqlvar[i].sqlind = 0;
}
// else if string format
else if (sscanf ((char*) ptr, "%ds", &len) == 1)
{
// set up variable data area
var->sqlvar[i].sqltype = SQL_TYP_CSTR;
var->sqlvar[i].sqldata = 0;
var->sqlvar[i].sqllen = len + 1;
var->sqlvar[i].sqlind = 0;
}
// else set error occurred
else
{
rc = SQLTAB_INVALID_FORMAT_STRING;
id = SQLTAB_ID_2;
break;
}
}
// set no error occurred
rc = 0;
}
// :H1 Create: Create Table
//----------------------------------------------------------------------
//
// Function Name: Create
//
// Purpose: This method will create an SQL table.
//
// Description: This method will create an SQL table by using
// dynamic SQL.
//
// Input:
// none
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
Table::Create (VOID)
{
SHORT i;
CHAR *statement = new CHAR [SQL_MAXCOLS * MAX_COL_LEN];
CHAR *col_name;
SHORT length;
CHAR temp [20];
CHAR *names;
// build create table statement
strcpy (statement, "CREATE TABLE ");
strcat (statement, table);
strcat (statement, "(");
// get copy of column names
names = strdup (columns);
// get pointer to start of columns names
col_name = strtok (names, ",");
// for all columns names
for (i = 0; i < cols && col_name != 0; i++)
{
// add column name to statement
strcat (statement, col_name);
// switch on variable type
switch (var->sqlvar[i].sqltype)
{
case SQL_TYP_SMALL: // small integer format
// build create table string
strcat (statement, " SMALLINT");
break;
case SQL_TYP_INTEGER: // long integer format
// build create table string
strcat (statement, " INTEGER");
break;
case SQL_TYP_CSTR: // C string format
// build create table string
sprintf (temp, " VARCHAR(%d)", var->sqlvar[i].sqllen);
strcat (statement, temp);
break;
}
// if not the last column
if (i < cols - 1)
strcat (statement, ",");
else
strcat (statement, ")");
// get next column name
col_name = strtok (NULL, ",");
}
// if all columns not processed
if (i < cols)
{
// free the create table statement
free (statement);
// free the names string
free (names);
// set error location
id = SQLTAB_ID_6;
// set error return code
rc = SQLTAB_INVALID_NUMBER_OF_COLUMNS;
return (rc);
}
// if error creating table
if ( (rc = sql_dynamic (statement)) != 0)
{
// free the create table statement
free (statement);
// free the names string
free (names);
// set error location
id = SQLTAB_ID_3;
return (rc);
}
// free the create table statement
free (statement);
// free the names string
free (names);
return (0);
}
// :H1 Create: Insert Data
//----------------------------------------------------------------------
//
// Function Name: Insert
//
// Purpose: This method will insert data into an SQL table.
//
// Description: This method will insert a variable data into the
// table columns.
//
// Input:
// num_cols number of colums to insert (must match number of columns
// in the table description)
// variable data to be inserted
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT Table::Insert (SHORT num_cols, ...)
{
SHORT i;
CHAR *statement = new CHAR [SQL_MAXCOLS * 2 + 30];
va_list arg_ptr;
// if invalid number of columns specified
if (num_cols != cols)
{
// free the insert table statement
free (statement);
// set error location
id = SQLTAB_ID_13;
// set error return code
rc = SQLTAB_INVALID_NUMBER_OF_COLUMNS;
return (rc);
}
// build insert table statement
sprintf (statement, "INSERT INTO %s VALUES(", table);
// get pointer to start of variable list
va_start (arg_ptr, num_cols);
// for all columns
for (i = 0; i < cols; i++)
{
// switch on variable type
switch (var->sqlvar[i].sqltype)
{
case SQL_TYP_SMALL: // small integer format
// put pointer to value in control block
var->sqlvar[i].sqldata = (unsigned CHAR *)arg_ptr;
// skip to next variable
va_arg (arg_ptr, SHORT);
break;
case SQL_TYP_INTEGER: // long integer format
// put pointer to value in control block
var->sqlvar[i].sqldata = (unsigned CHAR *)arg_ptr;
// skip to next variable
va_arg (arg_ptr, LONG);
break;
case SQL_TYP_CSTR: // C string format
// put pointer to value in control block and skip
// to next variable
var->sqlvar[i].sqldata =
(unsigned char *)va_arg (arg_ptr, CHAR *);
break;
}
// if not the last column
if (i < cols - 1)
strcat (statement, "?,");
else
strcat (statement, "?)");
}
// reset pointer to column list
va_end (arg_ptr);
// if error inserting into table
if ( (rc = sql_desc (statement, var)) != 0)
{
// free the insert table statement
free (statement);
// set error location
id = SQLTAB_ID_4;
return (rc);
}
// free the insert table statement
free (statement);
return (0);
}
// :H1 Search: Table Search
//----------------------------------------------------------------------
//
// Function Name: Search
//
// Purpose: This method will search an SQL table for a specified value(s).
//
// Description: This method will search for a match on the specified
// value(s). This is done by building a SELECT record
// for the specified values. If multiple conditions
// are specified they are AND'ed together.
//
// Input:
// initial initial search with this format?
// format table format
// format must be a string using %d, %ld, or %#s for
// fields to be searched and %*d, %*ld, and %*#s for
// those not being searched.
// # is the string length to search not including the
// 0 terminator. If no fields to be searched are specified
// the next row will be returned.
//
// variable pointers where to store EVERY field in the row
// variable data to be seached for match
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT Table::Search (BOOLEAN initial, const CHAR *format, ...)
{
const CHAR *ptr = format;
SHORT i;
SHORT len;
va_list arg_ptr;
CHAR *statement;
CHAR *col_name;
CHAR temp[30];
CHAR *names;
BOOLEAN first_search = TRUE;
// get pointer to start of variable list
va_start (arg_ptr, format);
// for all columns
for (i = 0; i < cols; i++)
{
// switch on variable type
switch (var->sqlvar[i].sqltype)
{
case SQL_TYP_SMALL: // small integer format
// get pointer to data and skip to next variable
var->sqlvar[i].sqldata = (unsigned char *)va_arg (arg_ptr, SHORT *);
break;
case SQL_TYP_INTEGER: // long integer format
// get pointer to data and skip to next variable
var->sqlvar[i].sqldata = (unsigned char *)va_arg (arg_ptr, LONG *);
break;
case SQL_TYP_CSTR: // C string format
// get pointer to data and skip to next variable
var->sqlvar[i].sqldata = (unsigned char *)va_arg (arg_ptr, CHAR **);
break;
}
}
// if initial search
if (initial == TRUE)
{
// get memory for statement
statement = new CHAR [SQL_MAXRECL];
// get copy of column names
names = strdup (columns);
// initialize pointer to format
ptr = format;
// create search statement
sprintf (statement, "SELECT * FROM %s WHERE ", table);
// get pointer to start of columns names
col_name = strtok (names, ",");
// for all columns
for (i = 0; i < cols && col_name != 0; i++)
{
// if % not found in format
if ( (ptr = strchr (ptr, '%')) == 0)
break;
// skip to character following %
ptr++;
// if skip this field
if (*ptr == '*')
{
}
else
{
// if not the first search condition
if (first_search == FALSE)
{
// add the AND condition to the statement
strcat (statement, "AND ");
}
else
{
// set not the first search condition
first_search = FALSE;
}
// if small integer format
if (*ptr == 'd')
{
// add variable to search
strcat (statement, col_name);
sprintf (temp, " = %d", va_arg (arg_ptr, SHORT));
strcat (statement, temp);
}
// else if long integer format
else if (*ptr == 'l' && *(ptr + 1) == 'd')
{
// add variable to search
strcat (statement, col_name);
sprintf (temp, " = %ld", va_arg (arg_ptr, LONG));
strcat (statement, temp);
}
// else if string format
else if (sscanf ((char*) ptr, "%ds", &len) == 1)
{
// if only part of the string is to be searched
if (len < var->sqlvar[i].sqllen - 1)
{
// add variable to search
strcat (statement, " SUBSTR (");
strcat (statement, col_name);
strcat (statement, ",1,");
sprintf (temp, "%d)='",len);
strcat (statement, temp);
strcat (statement, va_arg (arg_ptr, CHAR *));
strcat (statement, "'");
}
// else if invalid string length
else if (len > var->sqlvar[i].sqllen - 1)
{
// free the create table statement
free (statement);
// free the names string
free (names);
// set error location
id = SQLTAB_ID_8;
// set error return code
rc = SQLTAB_INVALID_FORMAT_STRING;
return (rc);
}
else
{
// add variable to search
strcat (statement, col_name);
strcat (statement, " = '");
strcat (statement, va_arg (arg_ptr, CHAR *));
strcat (statement, "'");
}
}
// else set error occurred
else
{
// free the create table statement
free (statement);
// free the names string
free (names);
// set error location
id = SQLTAB_ID_5;
// set error return code
rc = SQLTAB_INVALID_FORMAT_STRING;
return (rc);
}
}
// get pointer to next of column name
col_name = strtok (NULL, ",");
}
// if all columns not processed
if (i < cols)
{
// free the create table statement
free (statement);
// free the names string
free (names);
// set error location
id = SQLTAB_ID_7;
// set error return code
rc = SQLTAB_INVALID_NUMBER_OF_COLUMNS;
return (rc);
}
if // no search arguments specified
(first_search == TRUE)
// create get any row statement
sprintf (statement, "SELECT * FROM %s", table);
// get the row selected
rc = sql_fetch (initial, statement, var);
// free the create table statement
free (statement);
// free the names string
free (names);
}
else
{
// get the row selected
rc = sql_fetch (initial, 0, var);
}
// if row found
if (rc == 0)
// set record found
record_found = TRUE;
else
{
// set record not found
record_found = FALSE;
// set error location
id = SQLTAB_ID_14;
}
return (rc);
}
// :H1 Delete: Delete Row
//----------------------------------------------------------------------
//
// Function Name: Delete
//
// Purpose: This method will search an SQL table for a specified value(s)
// and delete the rows found.
//
// Description: This method will search for a match on the specified
// value(s). This is done by building a DELETE record
// for the specified values. If multiple conditions
// are specified they are AND'ed together.
//
// Input:
// format table format
// format must be a string using %d, %ld, or %#s for
// fields to be searched and %*d, %*ld, and %*#s for
// those not being searched.
// # is the string length to search not including the
// 0 terminator. If no fields to be searched are specified
// all the rows will be deleted.
// variable data to be seached for match
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT Table::Delete (const CHAR *format, ...)
{
const CHAR *ptr = format;
SHORT i;
SHORT len;
va_list arg_ptr;
CHAR *statement = new CHAR [SQL_MAXRECL];
CHAR *col_name;
CHAR temp[30];
CHAR *names;
BOOLEAN first_search = TRUE;
// get copy of column names
names = strdup (columns);
// get pointer to start of variable list
va_start (arg_ptr, format);
// initialize pointer to format
ptr = format;
// create delete statement
sprintf (statement, "DELETE FROM %s WHERE ", table);
// get pointer to start of columns names
col_name = strtok (names, ",");
// for all columns
for (i = 0; i < cols && col_name != 0; i++)
{
// if % not found in format
if ( (ptr = strchr (ptr, '%')) == 0)
break;
// skip to character following %
ptr++;
// if skip this field
if (*ptr == '*')
{
}
else
{
// if not the first condition
if (first_search == FALSE)
{
// add the AND condition to the statement
strcat (statement, "AND ");
}
else
{
// set not the first condition
first_search = FALSE;
}
// if small integer format
if (*ptr == 'd')
{
// add variable to search
strcat (statement, col_name);
sprintf (temp, " = %d", va_arg (arg_ptr, SHORT));
strcat (statement, temp);
}
// else if long integer format
else if (*ptr == 'l' && *(ptr + 1) == 'd')
{
// add variable to search
strcat (statement, col_name);
sprintf (temp, " = %ld", va_arg (arg_ptr, LONG));
strcat (statement, temp);
}
// else if string format
else if (sscanf ((char*) ptr, "%ds", &len) == 1)
{
// if only part of the string is to be searched
if (len < var->sqlvar[i].sqllen - 1)
{
// add variable to search
strcat (statement, " SUBSTR (");
strcat (statement, col_name);
strcat (statement, ",1,");
sprintf (temp, "%d)='",len);
strcat (statement, temp);
strcat (statement, va_arg (arg_ptr, CHAR *));
strcat (statement, "'");
}
// else if invalid string length
else if (len > var->sqlvar[i].sqllen - 1)
{
// free the delete statement
free (statement);
// free the names string
free (names);
// set error location
id = SQLTAB_ID_9;
// set error return code
rc = SQLTAB_INVALID_FORMAT_STRING;
return (rc);
}
else
{
// add variable to search
strcat (statement, col_name);
strcat (statement, " = '");
strcat (statement, va_arg (arg_ptr, CHAR *));
strcat (statement, "'");
}
}
// else set error occurred
else
{
// free the delete statement
free (statement);
// free the names string
free (names);
// set error location
id = SQLTAB_ID_10;
// set error return code
rc = SQLTAB_INVALID_FORMAT_STRING;
return (rc);
}
}
// get pointer to next of column name
col_name = strtok (NULL, ",");
}
// if all columns not processed
if (i < cols)
{
// free the delete statement
free (statement);
// free the names string
free (names);
// set error location
id = SQLTAB_ID_11;
// set error return code
rc = SQLTAB_INVALID_NUMBER_OF_COLUMNS;
return (rc);
}
if // no search arguments specified
(first_search == TRUE)
// create delete all rows statement
sprintf (statement, "DELETE FROM %s", table);
// if get the row selected error
if ( (rc = sql_dynamic (statement)) != 0)
// set error location
id = SQLTAB_ID_15;
// free the delete statement
free (statement);
// free the names string
free (names);
return (rc);
}
// :H1 DeleteCurrent: Delete Current Row
//----------------------------------------------------------------------
//
// Function Name: DeleteCurrent
//
// Purpose: This method will delete the current row found by using the
// search statement.
//
// Description: This method will delete the current row that was found using
// the search statement. This is done by building a DELETE
// current record.
//
// Input:
// none
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT Table::DeleteCurrent (VOID)
{
CHAR *statement = new CHAR [SQL_MAXRECL];
// if search record was not found using the search statement
if (record_found == FALSE)
{
// free the delete statement
free (statement);
// set error location
id = SQLTAB_ID_12;
// set error return code
rc = SQLTAB_NO_SEARCH_DONE;
return (rc);
}
// create delete statement
sprintf (statement, "DELETE FROM %s WHERE CURRENT OF", table);
// if delete the current row error
if ( (rc = sql_delete_row (statement)) != 0)
// set error location
id = SQLTAB_ID_16;
// free the delete statement
free (statement);
return (rc);
}
// :H1 Index: Index the database
//----------------------------------------------------------------------
//
// Function Name: Index
//
// Purpose: This method will create an index for the specified columns.
//
// Description: This method will create an index for the specified
// columns using the SQL create index command. Only one index
// can be created per table.
//
// Input:
// format table format
// format must be a string using %d, %ld, or %#s for
// fields to be searched and %*d, %*ld, and %*#s for
// those not being indexed.
// # is the string length to search not including the
// 0 terminator. If no fields to be indexed are specified
// an error will be returned.
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT Table::Index (const CHAR *format)
{
const CHAR *ptr = format;
SHORT i;
CHAR *statement = new CHAR [SQL_MAXRECL];
CHAR *col_name;
CHAR *names;
BOOLEAN first_col;
// get copy of column names
names = strdup (columns);
// initialize pointer to format
ptr = format;
// create index statement
sprintf (statement, "CREATE INDEX %s ON %s (", table, table);
// get pointer to start of columns names
col_name = strtok (names, ",");
// for all columns
for (i = 0; i < cols && col_name != 0; i++)
{
// if % not found in format
if ( (ptr = strchr (ptr, '%')) == 0)
break;
// skip to character following %
ptr++;
// if skip this field
if (*ptr == '*')
{
}
else
{
// if not the first column
if (first_col == FALSE)
{
// add a , to separate the columns
strcat (statement, ",");
}
else
{
// set not the first column
first_col = FALSE;
}
// add variable to index
strcat (statement, col_name);
}
// get pointer to next of column name
col_name = strtok (NULL, ",");
}
// add closing ( to statement
strcat (statement, ")");
// if all columns not processed
if (i < cols)
{
// free the index statement
free (statement);
// free the names string
free (names);
// set error location
id = SQLTAB_ID_18;
// set error return code
rc = SQLTAB_INVALID_NUMBER_OF_COLUMNS;
return (rc);
}
if // no index arguments specified
(first_col == TRUE)
{
// free the index statement
free (statement);
// free the names string
free (names);
// set error location
id = SQLTAB_ID_19;
// set error return code
rc = SQLTAB_INVALID_FORMAT_STRING;
return (rc);
}
// if index selected column error
if ( (rc = sql_dynamic (statement)) != 0)
// set error location
id = SQLTAB_ID_20;
// free the index statement
free (statement);
// free the names string
free (names);
return (rc);
}
// :H1 ~Table: Table Destructor
//----------------------------------------------------------------------
//
// Function Name: Table destructor
//
// Purpose: This method will delete an table object.
//
// Description: This method will delete the table object and
// release the table name.
//
// Input:
// none
//
// Output:
// none
//
//----------------------------------------------------------------------
Table::~Table ()
{
// delete table name
delete table;
}
#ifdef TEST
SHORT _astart; // for SD386 to find main
// This code is used to test the sql object.
VOID main ()
{
SHORT rc, id;
SQL database ("test"); // create sql object
Table table ("FORMAT", "%d %ld %4s", "SHORTVAR,LONGVAR,STRINGVAR");
SHORT shortvar;
LONG longvar;
CHAR stringvar [5];
printf ("Creating database...\n");
// if error creating database
if ( (rc = database.Create ("This is a test database")) != 0 )
{
printf ("Error creating database, rc = %d\n", rc);
// if error opening database
if ( (rc = database.Open ()) != 0 )
{
printf ("Error opening database, rc = %d\n", rc);
exit (-1);
}
else
printf ("Database opened...\n");
}
else
printf ("Database created...\n");
printf ("Creating table...\n");
if ( (rc = table.Create ()) != 0 )
{
printf ("Error creating table, rc = %d\n", rc);
database.Close ();
exit (-1);
}
else
printf ("Table created...\n");
if ( (rc = table.Create ()) == 0 )
{
printf ("No error on 2nd table create, rc = %d\n", rc);
database.Close ();
exit (-1);
}
table.Error (rc, id);
printf ("2nd create table rc = %d, id = %d\n", rc, id);
printf ("Adding indexing\n");
if ( (rc = table.Index ("%d %*ld %*4s")) != 0 )
{
printf ("Error creating index, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if ( (rc = table.Index ("%*d %*ld %*4s")) == 0 )
{
printf ("Error creating index with no index specified, rc = %d\n", rc);
database.Close ();
exit (-1);
}
printf ("Inserting data...\n");
if ( (rc = table.Insert (3, 1, 2L, "ABC" )) != 0 )
{
printf ("Error inserting data, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if ( (rc = table.Insert (3, 3, 4L, "DEF")) != 0 )
{
printf ("Error inserting data, rc = %d\n", rc);
database.Close ();
exit (-1);
}
printf ("Data inserted...\n");
if ( (rc = table.Insert (0, 1, 2)) != SQLTAB_INVALID_NUMBER_OF_COLUMNS)
{
printf ("Error on invalid number of columns, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if ( (rc = table.Search (TRUE, "%d %*d %*4s",
&shortvar, &longvar, &stringvar, 1)) != 0)
{
printf ("Error on table search, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if (shortvar != 1 || longvar != 2 || (strcmp (stringvar, "ABC")!= 0))
{
printf ("Error on table search, incorrect record\n");
database.Close ();
exit (-1);
}
if ( (rc = table.Search (FALSE, "%d %*d %*4s",
&shortvar, &longvar, &stringvar, 1)) == 0)
{
printf ("Error on table search, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if ( (rc = table.Search (TRUE, "%*d %*d %2s",
&shortvar, &longvar, &stringvar, "AB")) != 0)
{
printf ("Error on partial string search, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if (shortvar != 1 || longvar != 2 || (strcmp (stringvar, "ABC")!= 0))
{
printf ("Error on partial string search, incorrect record\n");
database.Close ();
exit (-1);
}
if ( (rc = table.Search (TRUE, "%*d %*d %*2s",
&shortvar, &longvar, &stringvar)) != 0)
{
printf ("Error on search for all rows, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if (shortvar != 1 || longvar != 2 || (strcmp (stringvar, "ABC")!= 0))
{
printf ("Error on search for all rows, incorrect record\n");
database.Close ();
exit (-1);
}
if ( (rc = table.DeleteCurrent ()) != 0)
{
printf ("Error on delete current row, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if ( (rc = table.Delete ("%d %*d %*4s", 3)) != 0)
{
printf ("Error on table row delete, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if ( (rc = table.Commit ()) != 0 )
{
printf ("Error commiting data, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if ( (rc = table.Insert (3, 5, 6L, "GHI")) != 0 )
{
printf ("Error inserting data, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if ( (rc = table.Delete ("%*d %*d %*4s")) != 0)
{
printf ("Error on delete all rows, rc = %d\n", rc);
database.Close ();
exit (-1);
}
if ( (rc = database.Close ()) != 0)
{
printf ("Error on close database, rc = %d\n", rc);
exit (-1);
}
printf ("Unit test completed successfully\n");
}
#endif