home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cppsql.zip
/
SQLSRC.ZIP
/
SQL.CPP
< prev
next >
Wrap
Text File
|
1992-09-18
|
15KB
|
596 lines
// :H1 SQL.CPP: START OF SPECIFICATIONS
//----------------------------------------------------------------------
//
// Module Name: SQL.CPP
//
// Description: SQL 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 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
// $P1 P0001979 V100 920608 GLH : Fix problems caused when start database
// started from application
//
// END-OF-SPECIFICATIONS -----------------------------------------------
// Use the following define to create a unit 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"
// :H1 USER-DEFINED INCLUDE FILES. SELECTIVITY USED WHERE POSSIBLE.
#include "portable.h"
#include "sql.hpp"
extern "C"
{
#include "sqlc.h"
}
// :H1 SQL: SQL Constructor
//----------------------------------------------------------------------
//
// Function Name: SQL constructor
//
// Purpose: This method is the SQL object constructor.
//
// Description: This method will save the name of the database.
//
// Input:
// name Database name to be used
// access access is SHARED or EXCLUSIVE, default is SHARED
// drive drive to create database on
//
// Output:
// none
//
//----------------------------------------------------------------------
SQL::SQL (const CHAR *name, CHAR access, CHAR drive)
{
// save database name
database = strdup (name);
// save database access
use = access;
// save database drive
build_drive = drive;
//6@P1D
// initialize id
id = 0;
}
// :H1 Create: Create SQL Database
//----------------------------------------------------------------------
//
// Function Name: Create
//
// Purpose: This method creates an SQL database.
//
// Description: This method creates an SQL database. The database must still
// be opened to be used.
//
// Input:
// comment comment to be placed into database
// bind_path path for SQL bind file
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Create (const CHAR *comment, const CHAR *bind_path)
{
USHORT len;
CHAR *path;
// get bind path length
len = strlen (bind_path);
// if get memory for bind path error
if ( (path = new CHAR [strlen (bind_path) + 15]) == 0)
{
// set error location
id = SQL_ID_10;
// set error return code
rc = SQL_MEMORY_ERROR;
return (rc);
}
// copy bind path
strcpy (path, bind_path);
// if path specified
if (len > 0)
// if end of path does not have a \
if (path [len-1] != '\\')
strcat (path, "\\");
// add bind file name
strcat (path, BIND_FILE);
// if error creating database
if ( (rc = sql_create (database, (CHAR *)comment, path,
build_drive)) != 0)
{
// set error location
id = SQL_ID_2;
return (rc);
}
return (0);
}
// :H1 Open: Open SQL Database
//----------------------------------------------------------------------
//
// Function Name: Open
//
// Purpose: This method opens an SQL database.
//
// Description: This method attempts to open an SQL database.
//
// Input:
// none
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Open (VOID)
{
// if error opening database
if ( (rc = sql_open (database, use)) != 0)
{
// set error location
id = SQL_ID_4;
return (rc);
}
return (0);
}
// :H1 SQL: Close SQL Database
//----------------------------------------------------------------------
//
// Function Name: Close
//
// Purpose: This method will close an SQL database.
//
// Description: This method will close an SQL database.
//
// Input:
// none
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Close (VOID)
{
// if close database error
if ( (rc = sql_close ()) != 0 )
{
// set error location
id = SQL_ID_5;
return (rc);
}
return (0);
}
// :H1 Delete: Delete SQL Database
//----------------------------------------------------------------------
//
// Function Name: Delete
//
// Purpose: This method will delete an SQL database.
//
// Description: This method will delete an SQL database.
//
// Input:
// none
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Delete (VOID)
{
// if error deleting database
if ( (rc = sql_delete (database)) != 0)
{
// set error location
id = SQL_ID_7;
return (rc);
}
return (0);
}
// :H1 Get_buffer_size : Get the database buffer size
//----------------------------------------------------------------------
//
// Function Name: Get_buffer_size
//
// Purpose: This method will get the buffer size for an SQL database.
//
// Description: This method will get the size for the specified SQL database.
//
// Input:
// size size of buffer
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Get_buffer_size (USHORT& size)
{
// if get pool size error
if ( (rc = sql_get_pool_size (database, &size)) != 0 )
{
// set error location
id = SQL_ID_11;
return (rc);
}
return (0);
}
// :H1 Set_buffer_size : Set the database buffer size
//----------------------------------------------------------------------
//
// Function Name: Set_buffer_size
//
// Purpose: This method will set the buffer size for an SQL database.
//
// Description: This method will set the size for the specified SQL database.
//
// Input:
// size size of buffer
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Set_buffer_size (USHORT size)
{
// if set pool size error
if ( (rc = sql_set_pool_size (database, size)) != 0 )
{
// set error location
id = SQL_ID_12;
return (rc);
}
return (0);
}
// :H1 Get_log_size : Get the database log file size
//----------------------------------------------------------------------
//
// Function Name: Get_log_size
//
// Purpose: This method will get the log file size for an SQL database.
//
// Description: This method will get the size for the specified SQL database.
//
// Input:
// size size of log file
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Get_log_size (USHORT& size)
{
// if get log file size error
if ( (rc = sql_get_log_size (database, &size)) != 0 )
{
// set error location
id = SQL_ID_13;
return (rc);
}
return (0);
}
// :H1 Set_log_size : Set the database log file size
//----------------------------------------------------------------------
//
// Function Name: Set_log_size
//
// Purpose: This method will set the log file size for an SQL database.
//
// Description: This method will set the size for the specified SQL database.
//
// Input:
// size size of buffer
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Set_log_size (USHORT size)
{
// if set log file size error
if ( (rc = sql_set_log_size (database, size)) != 0 )
{
// set error location
id = SQL_ID_14;
return (rc);
}
return (0);
}
// :H1 Get_log_number : Get the number of database log files
//----------------------------------------------------------------------
//
// Function Name: Get_log_number
//
// Purpose: This method will get the number log files for an SQL database.
//
// Description: This method will get the number of primary and seconday
// log files.
//
// Input:
// num_pri number of primary log files
// num_sec number of secondary log files
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Get_log_number (USHORT& num_pri, USHORT& num_sec)
{
// if get number of log file error
if ( (rc = sql_get_log_number (database, &num_pri, &num_sec)) != 0 )
{
// set error location
id = SQL_ID_16;
return (rc);
}
return (0);
}
// :H1 Set_log_number: Set the number of database log files
//----------------------------------------------------------------------
//
// Function Name: Set_log_number
//
// Purpose: This method will set the number log files for an SQL database.
//
// Description: This method will set the number of primary and seconday
// log files.
//
// Input:
// num_pri number of primary log files
// num_sec number of secondary log files
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Set_log_number (USHORT num_pri, USHORT num_sec)
{
// if get number of log file error
if ( (rc = sql_set_log_number (database, num_pri, num_sec)) != 0 )
{
// set error location
id = SQL_ID_17;
return (rc);
}
return (0);
}
// :H1 Commit: Commit Changes To Database
//----------------------------------------------------------------------
//
// Function Name: Commit
//
// Purpose: This method will commit the changes to the database.
//
// Description: This method will commit all the changes that have been
// made to the database.
//
// Input:
// none
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Commit (VOID)
{
// if commit error
if ( (rc = sql_commit ()) != 0)
// set error location
id = SQL_ID_3;
return (rc);
}
// :H1 Rollback: Rollback Changes From Database
//----------------------------------------------------------------------
//
// Function Name: Rollback
//
// Purpose: This method will rollback changes to the database.
//
// Description: This method will rollback all the changes that have been
// made to the database.
//
// Input:
// none
//
// Output:
// return code 0 = successful, not 0 = error
//
//----------------------------------------------------------------------
SHORT SQL::Rollback (VOID)
{
// if rollback error
if ( (rc = sql_rollback ()) != 0)
// set error location
id = SQL_ID_18;
return (rc);
}
// :H1 SQL: SQL Destructor
//----------------------------------------------------------------------
//
// Function Name: SQL destructor
//
// Purpose: This method is the SQL object destructor.
//
// Description: This method will free the database name, close
// the database, and stop the database manager.
//
// Input:
// none
//
// Output:
// none
//
//----------------------------------------------------------------------
SQL::~SQL ()
{
// delete database name
delete database;
// if close database error
if ( (rc = sql_close ()) != 0 )
{
// set error location
id = SQL_ID_8;
return;
}
// if stop database error
if ( (rc = sql_stop ()) != 0 )
{
// set error location
id = SQL_ID_9;
return;
}
}
#ifdef TEST
SHORT _astart; // for SD386 to find main
// This code is used to test the sql object.
SHORT main ()
{
SHORT rc;
SHORT id;
SQL database ("test"); // create sql object
USHORT size;
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);
exit (-1);
}
printf ("Database created...\n");
// if error getting database buffer size
if ( (rc = database.Get_buffer_size (size)) != 0 )
{
printf ("Error getting database buffer size, rc = %d\n", rc);
exit (-1);
}
// if not error opening database
if ( (rc = database.Open ()) == 0 )
{
printf ("No error reopening database, rc = %d\n", rc);
database.Close ();
exit (-1);
}
// get open database error
database.Error (rc, id);
printf ("open database rc = %d, id = %d\n", rc, id);
printf ("Database closing...\n");
// if error closing database
if ( (rc = database.Close ()) != 0 )
{
printf ("Error closing database, rc = %d\n", rc);
exit (-1);
}
printf ("Database closed...\n");
printf ("Database opening...\n");
// if error opening database
if ( (rc = database.Open ()) != 0 )
{
printf ("Error opening database, rc = %d\n", rc);
exit (-1);
}
printf ("Database opened...\n");
printf ("Press any key to complete program and delete data base\n");
getchar ();
// if error deleting database
if ( (rc = database.Delete ()) != 0 )
{
printf ("Error deleting database, rc = %d\n", rc);
exit (-1);
}
printf ("Unit test completed successfully\n");
}
#endif