/****************************************************************************** ** ** Source File Name = descrptr.c 1.3 ** ** Licensed Materials - Property of IBM ** ** (C) COPYRIGHT International Business Machines Corp. 1995, 1999 ** All Rights Reserved. ** ** US Government Users Restricted Rights - Use, duplication or ** disclosure restricted by GSA ADP Schedule Contract with IBM Corp. ** ** ** PURPOSE : ** ** Demonstrates simple use of descriptors. This is a modification of the ** CLI sample program prepare.c ** ** For more information about these samples see the README file. ** ** For more information on programming in CLI see the: ** - "Building CLI Applications" section of the Application Building Guide, and the ** - CLI Guide and Reference. ** ** For more information on the SQL language see the SQL Reference. ** ********************************************************************************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sqlcli1.h> #include "samputil.h" /* Header file for CLI sample code */ /* For the Macintosh environment when generating 68K applications */ #ifdef DB268K /* Need to include ASLM for 68K applications */ #include <LibraryManager.h> #endif /* * Global Variables for user id and password. * To keep samples simple, not a recommended practice. */ extern SQLCHAR server[SQL_MAX_DSN_LENGTH + 1] ; extern SQLCHAR uid[MAX_UID_LENGTH + 1] ; extern SQLCHAR pwd[MAX_PWD_LENGTH + 1] ; /** main **/ int main( int argc, char * argv[] ) { int colCount; SQLHANDLE henv, hdbc, hstmt, hIPDdesc, hIRDdesc ; SQLRETURN rc ; SQLSMALLINT desc_smallint; SQLCHAR * desc_char[25] ; /*--> SQLL1X60.SCRIPT */ SQLCHAR * sqlstmt = ( SQLCHAR * ) "SELECT deptname, location from org where division = ? " ; /*<-- */ struct { SQLINTEGER ind ; SQLCHAR s[15] ; SQLINTEGER pad[512]; } deptname, location, division ; /* Make the results from SQLGetDescField() more meaningful by mapping */ /* the returned values to the string defined in the CLI header file. */ static char ALLOCTYPES[][21] = { "- No 0 Value-", "SQL_DESC_ALLOC_AUTO", "SQL_DESC_ALLOC_USER" }; static char PARAMTYPE[][24] = { "- No 0 Value-", "SQL_PARAM_INPUT", "SQL_PARAM_INPUT_OUTPUT", "- No 3 Value -", "SQL_PARAM_OUTPUT" }; /* For the Macintosh environment when generating 68K applications */ #ifdef DB268K /* Before making any API calls for 68K environment, need to initialize the Library Manager */ InitLibraryManager(0,kCurrentZone,kNormalMemory); atexit(CleanupLibraryManager); #endif /*--> */ /* macro to initalize server, uid and pwd */ INIT_UID_PWD ; /* allocate an environment handle */ rc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ) ; if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ; /* allocate a connect handle, and connect */ rc = DBconnect( henv, &hdbc ) ; if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ; rc = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ; CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ; /* Added for descrptr.c */ /* Use SQLGetStmtAttr() to get implicit parameter descriptor handle */ rc = SQLGetStmtAttr ( hstmt, SQL_ATTR_IMP_PARAM_DESC, &hIPDdesc, SQL_IS_POINTER, NULL); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; /* Added for descrptr.c */ /* Use SQLGetStmtAttr() to get implicit row descriptor handle */ rc = SQLGetStmtAttr ( hstmt, SQL_ATTR_IMP_ROW_DESC, &hIRDdesc, SQL_IS_POINTER, NULL); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; /* Added for descrptr.c */ /* Call SQLGetDescField() to see how the header field */ /* SQL_DESC_ALLOC_TYPE is set. */ rc = SQLGetDescField( hIPDdesc, 0, /* ignored for header fields */ SQL_DESC_ALLOC_TYPE, &desc_smallint, /* The result */ SQL_IS_SMALLINT, NULL ); /* ignored */ CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; /* Print the descriptor information */ printf("The IPD header descriptor field SQL_DESC_ALLOC_TYPE is %s\n", ALLOCTYPES[desc_smallint]); /* prepare statement for multiple use */ rc = SQLPrepare(hstmt, sqlstmt, SQL_NTS); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; /* bind division to parameter marker in sqlstmt */ rc = SQLBindParameter( hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, division.s, 11, NULL ) ; CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; /* bind deptname to first column in the result set */ rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, (SQLPOINTER) deptname.s, 15, &deptname.ind); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, (SQLPOINTER) location.s, 14, &location.ind); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; /* Added for descrptr.c */ /* Call SQLGetDescField() to see how the descriptor record */ /* field SQL_DESC_PARAMETER_TYPE is set */ rc = SQLGetDescField( hIPDdesc, 1, /* Look at the parameter */ SQL_DESC_PARAMETER_TYPE, &desc_smallint, /* The result */ SQL_IS_SMALLINT, NULL ); /* ignored */ CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; printf("The IPD record descriptor field SQL_DESC_PARAMETER_TYPE is %s\n", PARAMTYPE[desc_smallint]); strcpy( (char *) division.s, "Eastern"); rc = SQLExecute(hstmt); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; printf("\nDepartments in %s Division:\n", division.s); printf("Department Location\n"); printf("-------------- -------------\n"); while ( ( rc = SQLFetch( hstmt ) ) == SQL_SUCCESS ) { CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; printf( "%-14.14s %-13.13s \n", deptname.s, location.s ) ; if ( rc != SQL_NO_DATA_FOUND ) CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; } /* Added for descrptr.c */ /* Print out some implementation row descriptor fields */ /* from the last SQLFetch() above */ for (colCount = 1; colCount <=2; colCount++) { printf("\nInformation for column %i\n",colCount); /* Call SQLGetDescField() to see how the descriptor record */ /* field SQL_DESC_TYPE_NAME is set */ rc = SQLGetDescField( hIRDdesc, colCount, SQL_DESC_TYPE_NAME, /* record field */ desc_char, /* The result */ 25, NULL ); /* ignored */ CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; printf(" - IRD record descriptor field SQL_DESC_TYPE_NAME is %s\n", desc_char); /* Call SQLGetDescField() to see how the descriptor record */ /* field SQL_DESC_LABEL is set */ rc = SQLGetDescField( hIRDdesc, colCount, SQL_DESC_LABEL, /* record field */ desc_char, /* The result */ 25, NULL ); /* ignored */ CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; printf(" - IRD record descriptor field SQL_DESC_LABEL is %s\n", desc_char); } /* End of the for statement */ /*<-- */ rc = SQLFreeStmt( hstmt, SQL_CLOSE ) ; CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt ) ; CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLEndTran( SQL_HANDLE_DBC, hdbc, SQL_ROLLBACK ) ; CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ; printf( "\n>Disconnecting .....\n" ) ; rc = SQLDisconnect( hdbc ) ; CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ; rc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc ) ; CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ; rc = SQLFreeHandle( SQL_HANDLE_ENV, henv ) ; if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ; return( SQL_SUCCESS ) ; } /* end main */