home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cppsql.zip
/
SQLSRC.ZIP
/
SQLC.SQC
< prev
next >
Wrap
Text File
|
1992-09-18
|
23KB
|
808 lines
/* :H1 SQLC: START OF SPECIFICATIONS
//----------------------------------------------------------------------
//
// Module Name: SQLC
//
// Description: This module interfaces to the SQL database.
//
// Product Classification:
// IBM Internal Use Only
// (C) Copyright IBM Corp. 1992
//
// Status: New
//
// Initial Author: George L. Havens
//
// Function: This module provides a C interface to the SQL routines
// required by the SQL object.
//
// Notes: This module must be compiled into a C module using SQLPREP.
//
// Dependencies: Description of any dependency this module
// has on any other module
//
// Restrictions: *************************WARNING************************
// DO NOT MAKE MODIFICATIONS IN THE SQLC.C FILE.
// MAKE MODIFICATIONS IN THE SQLC.SQC FILE AND RUN SQLPREP TO
// CREATE THE SQLC.C FILE.
//
// Compiler: Zortech C++
//
// Change Activity -----------------------------------------------------
//
// $MOD(SQLC) COMP(XREFGEN) PROD(WRTSVILL): Interface to SQL database.
//
// FLAG REASON VERS DATE WHO DESCRIPTION
// ---- -------- ---- ------ --- -----------
// $D0 XXXXXX V100 920224 GLH : Initial level
//
// END-OF-SPECIFICATIONS -----------------------------------------------*/
#include "stdio.h"
#include "sqlenv.h"
#include "sqlutil.h"
#include "portable.h"
static cursor_open = FALSE;
EXEC SQL INCLUDE SQLCA;
EXEC SQL BEGIN DECLARE SECTION;
char host_string[200];
EXEC SQL END DECLARE SECTION;
EXEC SQL DECLARE C1 CURSOR FOR S1;
struct sqlda *host_var;
/* :H1 sql_create: Create SQL Database
//----------------------------------------------------------------------
//
// Function Name: Create database
//
// Purpose: This function will create an SQL database.
//
// Description: This function will create an SQL database using the
// SQL API.
//
// Input:
// name Database name to be created
// comment Comment to be inserted into the database
// bind_path Bind file path name
// drive Drive to build database on
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_create (CHAR *name, CHAR *comment, CHAR *bind_path, CHAR drive)
{
/* create database */
sqlecred (name, /* database name */
drive, /* database drive */
comment, /* comment string */
0, /* codepage */
&sqlca); /* sqlca */
/* if create error */
if (sqlca.sqlcode != 0)
return (sqlca.sqlcode);
/* bind database to program */
sqlabind (bind_path, name, (char *)0, "NUL:", "DEF", &sqlca);
return (sqlca.sqlcode);
}
/* :H1 sql_delete: Delete SQL Database
//----------------------------------------------------------------------
//
// Function Name: Delete database
//
// Purpose: This function will delete an SQL database.
//
// Description: This function deletes an SQL database using the SQL
// API.
//
// Input:
// name Database name to be deleted
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_delete (CHAR *name)
{
/* delete database */
sqledrpd (name, &sqlca);
return (sqlca.sqlcode);
}
/* :H1 sql_start: Start SQL Database Manager
//----------------------------------------------------------------------
//
// Function Name: Start database manager
//
// Purpose: This function will start the database manager.
//
// Description: This function starts the database manager using the SQL
// API. It does not return an error if the database manager has
// already been started.
//
// Input:
// none
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_start ()
{
SHORT rc;
/* start database manager */
rc = sqlestar ();
/* if database manager already active */
if (rc == SQLE_RC_INVSTRT)
/* set to return OK */
rc = 0;
return (rc);
}
/* :H1 sql_stop: Stop SQL Database Manager
//----------------------------------------------------------------------
//
// Function Name: Stop database manager
//
// Purpose: This function will stop the database manager.
//
// Description: This function stops the database manager using the SQL
// API.
//
// Input:
// none
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_stop ()
{
/* stop database manager */
sqlestop (&sqlca);
return (sqlca.sqlcode);
}
/* :H1 sql_open: Open Database
//----------------------------------------------------------------------
//
// Function Name: Open a database
//
// Purpose: This function will connect a database to the application.
//
// Description: This function binds and connects to the database using
// the SQL API.
//
// Input:
// name Database name to be opened.
// use use database access, shared or exclusive
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_open (CHAR *name, CHAR use)
{
/* start using database */
sqlestrd (name, use, &sqlca);
return (sqlca.sqlcode);
}
/* :H1 sql_close: Close Database
//----------------------------------------------------------------------
//
// Function Name: Close a database
//
// Purpose: This function will disconnect a database from the application.
//
// Description: This function disconnects from the database using
// the SQL API. It will also close the cursor if it is open.
//
// Input:
// none
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_close ()
{
/* if cursor is open */
if (cursor_open == TRUE)
{
/* close cursor */
EXEC SQL CLOSE C1;
/* if close cursor error */
if (sqlca.sqlcode != 0)
return (sqlca.sqlcode);
/* set cursor is not open */
cursor_open = FALSE;
}
/* stop using database */
sqlestpd (&sqlca);
return (sqlca.sqlcode);
}
/* :H1 sql_dynamic: Dynamic SQL Statements
//----------------------------------------------------------------------
//
// Function Name: sql_dynamic
//
// Purpose: This function will perform dynamic SQL statements.
//
// Description: This function executes a dynamic SQL statement.
//
// Input:
// statement dynamic SQL statement to be executed
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_dynamic (CHAR *statement)
{
/* move statement to host variable */
strcpy (host_string, statement);
EXEC SQL EXECUTE IMMEDIATE :host_string;
return (sqlca.sqlcode);
}
/* :H1 sql_desc: Dynamic SQL Statements Using Descriptors
//----------------------------------------------------------------------
//
// Function Name: sql_desc
//
// Purpose: This function will perform dynamic SQL statements
// using descriptors.
//
// Description: This function executes a dynamic SQL statement using
// descriptors.
//
// Input:
// statement dynamic SQL statement to be executed
// var pointer to SQLDA
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_desc (CHAR *statement, struct sqlda *var)
{
/* move statement to host variable */
strcpy (host_string, statement);
host_var = var;
EXEC SQL PREPARE S2 FROM :host_string;
/* if prepare error */
if (sqlca.sqlcode != 0)
return (sqlca.sqlcode);
EXEC SQL EXECUTE S2 USING DESCRIPTOR :*host_var;
return (sqlca.sqlcode);
}
/* :H1 sql_fetch: Dynamic SQL Fetch Statement Using Descriptors
//----------------------------------------------------------------------
//
// Function Name: sql_fetch
//
// Purpose: This function will perform dynamic SQL fetch statement
// using descriptors.
//
// Description: This function executes a fetch SQL statement using
// descriptors.
//
// Input:
// initial initial search?
// statement statement to run
// var pointer to SQLDA
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_fetch (BOOLEAN initial, CHAR *statement, struct sqlda *var)
{
if (initial == TRUE)
{
/* move statement to host variable */
strcpy (host_string, statement);
host_var = var;
/* if cursor already open */
if (cursor_open == TRUE)
{
/* close cursor */
EXEC SQL CLOSE C1;
/* if close cursor error */
if (sqlca.sqlcode != 0)
return (sqlca.sqlcode);
/* set cursor is not open */
cursor_open = FALSE;
}
EXEC SQL PREPARE S1 FROM :host_string;
/* if prepare error */
if (sqlca.sqlcode != 0)
return (sqlca.sqlcode);
EXEC SQL OPEN C1;
/* if open cursor error */
if (sqlca.sqlcode != 0)
return (sqlca.sqlcode);
/* set cursor is open */
cursor_open = TRUE;
}
EXEC SQL FETCH C1 USING DESCRIPTOR :*host_var;
return (sqlca.sqlcode);
}
/* :H1 sql_close_cur: Close SQL Cursor
//----------------------------------------------------------------------
//
// Function Name: sql_close_cur
//
// Purpose: This function will close the SQL cursor.
//
// Description: This function closes the cursor.
//
// Input:
// none
//
// Output:
// return 0 = successful
//
//----------------------------------------------------------------------*/
SHORT sql_close_cur (VOID)
{
/* if cursor already open */
if (cursor_open == TRUE)
{
/* close cursor */
EXEC SQL CLOSE C1;
/* set cursor not open */
cursor_open = FALSE;
}
return (0);
}
/* :H1 sql_delete_row: Delete Current Row
//----------------------------------------------------------------------
//
// Function Name: sql_delete_row
//
// Purpose: This function will delete the current row.
//
// Description: This function deletes the row described by the
// current cursor.
//
// Input:
// statement dynamic SQL delete statement to be executed
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_delete_row (CHAR *statement)
{
/* move statement to host variable */
strcpy (host_string, statement);
/* add cursor name for search to statement */
strcat (host_string, " C1");
EXEC SQL EXECUTE IMMEDIATE :host_string;
return (sqlca.sqlcode);
}
/* :H1 sql_commit: Commit data to database
//----------------------------------------------------------------------
//
// Function Name: sql_commit
//
// Purpose: This function will commit the data to the database
//
// Description: This function will commit the outstanding data
// to the database.
//
// Input:
// none
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_commit (VOID)
{
/* if cursor is open */
if (cursor_open == TRUE)
{
/* close cursor */
EXEC SQL CLOSE C1;
/* if close cursor error */
if (sqlca.sqlcode != 0)
return (sqlca.sqlcode);
/* set cursor is not open */
cursor_open = FALSE;
}
/* commit data to database */
EXEC SQL COMMIT;
return (sqlca.sqlcode);
}
/* :H1 sql_get_buffer_size: Get buffer pool size
//----------------------------------------------------------------------
//
// Function Name: sql_get_buffer_size
//
// Purpose: This function will get the buffer pool size for the database.
//
// Description: This function will get the buffer pool size for the
// database.
//
// Input:
// name database name to query
// size pointer to where to return size
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_get_pool_size (CHAR *name, USHORT *size)
{
struct sqlfupd request;
/* build request */
request.token = SQLF_DBTN_BUFFPAGE;
request.ptrvalue = (CHAR *)size;
/* get buffer pool size */
sqlfxdbc (name, /* database name */
"", /* password */
1, /* count */
&request, /* list of items */
&sqlca); /* sqlca */
return (sqlca.sqlcode);
}
/* :H1 sql_set_buffer_size: Set buffer pool size
//----------------------------------------------------------------------
//
// Function Name: sql_set_buffer_size
//
// Purpose: This function will set the buffer pool size for the database.
//
// Description: This function will set the buffer pool size for the
// database. It will update the sqlenseg for database
// manager if necessary.
//
// Input:
// name database name to update
// size size to set
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_set_pool_size (CHAR *name, USHORT size)
{
struct sqlfupd request [2];
USHORT sqlenseg;
USHORT numdb;
USHORT dbheap;
USHORT locklist;
USHORT min_len;
SHORT rc;
/* build get sqlenseg and numdb request */
request[0].token = SQLF_KTN_SQLENSEG;
request[0].ptrvalue = (CHAR *)&sqlenseg;
request[1].token = SQLF_KTN_NUMDB;
request[1].ptrvalue = (CHAR *)&numdb;
/* get sqlenseg and numdb for database manager */
sqlfxsys (2, /* count */
&request[0], /* list of items */
&sqlca); /* sqlca */
/* if get sqlenseg and numdb error */
if (sqlca.sqlcode != 0)
return (sqlca.sqlcode);
/* build get dbheap and locklist request */
request[0].token = SQLF_DBTN_DBHEAP;
request[0].ptrvalue = (CHAR *)&dbheap;
request[1].token = SQLF_DBTN_LOCKLIST;
request[1].ptrvalue = (CHAR *)&locklist;
/* get dbheap and locklist for database */
sqlfxdbc (name, /* database name */
"", /* password */
2, /* count */
&request[0], /* list of items */
&sqlca); /* sqlca */
/* compute minimum sqlenseg required */
min_len = ((numdb - 1) * 7) + dbheap + ((size + 15) / 16) +
((locklist + 15) / 16) + 3;
/* add for roundoff errors */
min_len += 5;
/* if sqlenseg is less than the minimum required */
if (sqlenseg < min_len)
{
/* stop database manager */
sql_stop ();
/* build set sqlenseg request */
request[0].token = SQLF_KTN_SQLENSEG;
request[0].ptrvalue = (CHAR *)&min_len;
/* set sqlenseg for database manager */
sqlfusys (1, /* count */
&request[0], /* list of items */
&sqlca); /* sqlca */
/* if set sqlenseg error */
if (sqlca.sqlcode != 0)
return (sqlca.sqlcode);
/* if error starting database manager so changes will
take effect */
if ( (rc = sql_start ()) != 0)
return (rc);
}
/* build set size request */
request[0].token = SQLF_DBTN_BUFFPAGE;
request[0].ptrvalue = (CHAR *)&size;
/* set buffer pool size */
sqlfeudb (name, /* database name */
"", /* password */
1, /* count */
&request[0], /* list of items */
&sqlca); /* sqlca */
return (sqlca.sqlcode);
}
/* :H1 sql_get_log_size: Get log file size
//----------------------------------------------------------------------
//
// Function Name: sql_get_log_size
//
// Purpose: This function will get the log size for the database.
//
// Description: This function will use the SQL API to get the log file
// size of the specified database.
//
// Input:
// name database name to query
// size pointer to where to return size
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_get_log_size (CHAR *name, USHORT *size)
{
struct sqlfupd request;
/* build request */
request.token = SQLF_DBTN_LOGFILSIZ;
request.ptrvalue = (CHAR *)size;
/* get log file size */
sqlfxdbc (name, /* database name */
"", /* password */
1, /* count */
&request, /* list of items */
&sqlca); /* sqlca */
return (sqlca.sqlcode);
}
/* :H1 sql_set_log_size: Set log file size
//----------------------------------------------------------------------
//
// Function Name: sql_set_log_size
//
// Purpose: This function will set the log file size for the database.
//
// Description: This function will use the SQL API to set the log
// file size for the specified database.
//
// Input:
// name database name to update
// size size to set
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_set_log_size (CHAR *name, USHORT size)
{
struct sqlfupd request;
/* build set size request */
request.token = SQLF_DBTN_LOGFILSIZ;
request.ptrvalue = (CHAR *)&size;
/* set log file size */
sqlfeudb (name, /* database name */
"", /* password */
1, /* count */
&request, /* list of items */
&sqlca); /* sqlca */
return (sqlca.sqlcode);
}
/* :H1 sql_get_log_number: Get number of log files for database
//----------------------------------------------------------------------
//
// Function Name: sql_get_log_number
//
// Purpose: This function will get the number of logs for the database.
//
// Description: This function will use the SQL API to get the number of
// log files for specified database.
//
// Input:
// name database name to query
// primary pointer to where to return number of primary log files
// secondary pointer to where to return number of secondary log files
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_get_log_number (CHAR *name, USHORT *primary, USHORT *secondary)
{
struct sqlfupd request[2];
/* build request */
request[0].token = SQLF_DBTN_LOGPRIMARY;
request[0].ptrvalue = (CHAR *)primary;
request[1].token = SQLF_DBTN_LOGSECOND;
request[1].ptrvalue = (CHAR *)secondary;
/* get number of log files */
sqlfxdbc (name, /* database name */
"", /* password */
2, /* count */
&request[0], /* list of items */
&sqlca); /* sqlca */
return (sqlca.sqlcode);
}
/* :H1 sql_set_log_number: Set number of log files for database
//----------------------------------------------------------------------
//
// Function Name: sql_set_log_number
//
// Purpose: This function will set the number of logs for the database.
//
// Description: This function will use the SQL API to set the number of
// log files for specified database.
//
// Input:
// name database name to query
// primary number of primary log files
// secondary number of secondary log files
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_set_log_number (CHAR *name, USHORT primary, USHORT secondary)
{
struct sqlfupd request[2];
/* build request */
request[0].token = SQLF_DBTN_LOGPRIMARY;
request[0].ptrvalue = (CHAR *)&primary;
request[1].token = SQLF_DBTN_LOGSECOND;
request[1].ptrvalue = (CHAR *)&secondary;
/* set number of log files */
sqlfeudb (name, /* database name */
"", /* password */
2, /* count */
&request[0], /* list of items */
&sqlca); /* sqlca */
return (sqlca.sqlcode);
}
/* :H1 sql_rollback: Rollback data from database
//----------------------------------------------------------------------
//
// Function Name: sql_rollback
//
// Purpose: This function will rollback the data from the database
//
// Description: This function will rollback the outstanding data
// from the database.
//
// Input:
// none
//
// Output:
// return returns SQL return code
//
//----------------------------------------------------------------------*/
SHORT sql_rollback (VOID)
{
/* if cursor is open */
if (cursor_open == TRUE)
{
/* close cursor */
EXEC SQL CLOSE C1;
/* if close cursor error */
if (sqlca.sqlcode != 0)
return (sqlca.sqlcode);
/* set cursor is not open */
cursor_open = FALSE;
}
/* rollback data from database */
EXEC SQL ROLLBACK;
return (sqlca.sqlcode);
}