/******************************************************************************* ** ** Source File Name = showpic.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 : ** - Displays all employees with pictures, and prompts the user to ** select a picture (BLOB) from the emp_photo table ** - Writes a file to the current directory of the indicated type ** - xwud is used to display the xwd format (unix) ** - iconedit is used to display the bitmap format (OS/2) ** (edit this file to use a different viewer). ** ** 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[] ) { SQLHANDLE henv, hdbc, hstmt ; SQLRETURN rc ; /*--> SQLL1X33.SCRIPT */ SQLCHAR * stmt1 = ( SQLCHAR * ) "select employee.empno, firstnme || lastname as name " "from employee, emp_photo " "where employee.empno = emp_photo.empno and photo_format = ?" ; SQLCHAR * stmt2 = ( SQLCHAR * ) "SELECT picture FROM emp_photo " "WHERE empno = ? AND photo_format = ?" ; /*<-- */ SQLCHAR Photo_Format[11] = {""} ; SQLCHAR Desc[61] ; struct { SQLINTEGER ind; SQLCHAR s[30]; } Name; struct { SQLINTEGER ind; SQLCHAR s[7]; } Empno; SQLUINTEGER FOption = SQL_FILE_OVERWRITE; SQLINTEGER FNInd = 0; SQLINTEGER Pic_Length = 0; SQLSMALLINT FNLength = 13; SQLCHAR FName[13]; char buffer[256] = "" ; char showpic[255] = "N" ; /* 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 ) ; /* Get picture */ while (1) { printf("Which Picture Format [xwd (XWindows), bitmap, gif ]?\n"); gets((char *)Photo_Format); if (strcmp((char *)Photo_Format, "xwd") == 0 || strcmp((char *)Photo_Format, "bitmap") == 0 || strcmp((char *)Photo_Format, "gif") == 0) break; printf("Invalid Format!\n"); } rc = SQLBindParameter( hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 11, 0, Photo_Format, 0, NULL ) ; CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLExecDirect(hstmt, stmt1, SQL_NTS); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, Empno.s, 7, &Empno.ind); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, Name.s, 30, &Name.ind); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; printf("Empno Name \n"); printf("------- ---------------------\n"); while ((rc = SQLFetch(hstmt)) == SQL_SUCCESS) { printf("%-6s %-30s \n", Empno.s, Name.s); } if ( rc != SQL_NO_DATA_FOUND ) CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLFreeStmt( hstmt, SQL_UNBIND ) ; CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLFreeStmt( hstmt, SQL_RESET_PARAMS ) ; CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLFreeStmt( hstmt, SQL_CLOSE ) ; CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLEndTran( SQL_HANDLE_DBC, hdbc, SQL_COMMIT ) ; CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ; rc = SQLBindParameter( hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 7, 0, Empno.s, 0, &Empno.ind ) ; CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; rc = SQLBindParameter( hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 11, 0, Photo_Format, 0, NULL ) ; CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; /*--> */ /* Get Employee Number */ printf("Select a Employee Number from the list above\n"); gets((char *)Empno.s); /* Execute statement 2 which selects the picturee blob */ rc = SQLExecDirect(hstmt, stmt2, SQL_NTS); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; /* Bind blob column to a file */ rc = SQLBindFileToCol(hstmt, 1, FName, &FNLength, &FOption, 13, NULL, &FNInd); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; /* Set deferred FName argument to putput file name. */ switch (Photo_Format[0]) { case 'x': sprintf((char *)FName, "P%s.xwd", Empno.s, Photo_Format); sprintf(buffer, "xwud -in %s &", FName); printf("xwud will be used to display image\n"); break; case 'b': sprintf((char *)FName, "P%s.bmp", Empno.s, Photo_Format); sprintf(buffer, "iconedit %s", FName); #if !defined(DB2WIN) printf("iconedit will be used to display image\n"); #endif break; case 'g': sprintf((char *)FName, "P%s.gif", Empno.s, Photo_Format); printf("Will create file: %s\n", FName); break; default : sprintf((char *)FName, "P%s.pic", Empno.s, Photo_Format); printf("Unknown Format, will attempt to create file: %s \n", FName); } /* Fetch the blob column into the bound file */ rc = SQLFetch(hstmt); CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ; printf("%s has been sucessfully created, do you want to display it now? (Y/N)\n", FName) ; gets( (char *) showpic ) ; #if !defined(DB2WIN) /*If supported, execute local display tool with the file just created */ if ( (buffer[0] != '\0' ) && ( (showpic[0] == 'Y') || (showpic[0] == 'y') ) ) system(buffer); #endif /*<-- */ /* free statement resources */ rc = SQLEndTran( SQL_HANDLE_DBC, hdbc, SQL_COMMIT ) ; CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ; rc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt ) ; CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, 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 */