123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- /***************************************************************************
- * Licensed Materials - Property of IBM and/or HCL
- *
- * IBM Informix Client-SDK
- *
- * Copyright IBM Corporation 1997, 2013 All rights reserved.
- * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
- *
- *
- *
- *
- *
- * Title: distsel.c
- *
- * Description: To select data from a column that uses a DISTINCT
- * datatype
- * -- the column selected is 'unit_price' of the 'item'
- * table, which is of distinct type DOLLAR based on
- * decimal
- *
- *
- ***************************************************************************
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifndef NO_WIN32
- #include <io.h>
- #include <windows.h>
- #include <conio.h>
- #endif /*NO_WIN32*/
- #include "infxcli.h"
- #define BUFFER_LEN 25
- #define ERRMSG_LEN 200
- SQLCHAR defDsn[] = "odbc_demo";
- SQLINTEGER checkError (SQLRETURN rc,
- SQLSMALLINT handleType,
- SQLHANDLE handle,
- SQLCHAR* errmsg)
- {
- SQLRETURN retcode = SQL_SUCCESS;
- SQLSMALLINT errNum = 1;
- SQLCHAR sqlState[6];
- SQLINTEGER nativeError;
- SQLCHAR errMsg[ERRMSG_LEN];
- SQLSMALLINT textLengthPtr;
- if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
- {
- while (retcode != SQL_NO_DATA)
- {
- retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
- if (retcode == SQL_INVALID_HANDLE)
- {
- fprintf (stderr, "checkError function was called with an invalid handle!!\n");
- return 1;
- }
- if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
- fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
- errNum++;
- }
- fprintf (stderr, "%s\n", errmsg);
- return 1; /* all errors on this handle have been reported */
- }
- else
- return 0; /* no errors to report */
- }
- int main (long argc,
- char* argv[])
- {
- /* Declare variables
- */
- /* Handles */
- SQLHDBC hdbc;
- SQLHENV henv;
- SQLHSTMT hstmt;
- /* Miscellaneous variables */
- SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
- SQLRETURN rc = 0;
- SQLINTEGER in;
- SQLCHAR verInfoBuffer[BUFFER_LEN];
- SQLSMALLINT verInfoLen;
- SQLCHAR* selectStmt = (SQLCHAR *) "SELECT item_num, description, unit_price FROM item";
- SQLCHAR description[BUFFER_LEN];
- SQLLEN item_num, cbItemNum, cbDescription, cbUnitPrice;
- SQLREAL unit_price;
- /* STEP 1. Get data source name from command line (or use default)
- ** Allocate the environment handle and set ODBC version
- ** Allocate the connection handle
- ** Establish the database connection
- ** Get version information from the database server
- ** -- if version < 9.x (not UDO enabled), exit with error message
- ** Allocate the statement handle
- */
- /* If(dsn is not explicitly passed in as arg) */
- if (argc != 2)
- {
- /* Use default dsn - odbc_demo */
- fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
- strcpy ((char *)dsn, (char *)defDsn);
- }
- else
- {
- /* Use specified dsn */
- strcpy ((char *)dsn, (char *)argv[1]);
- fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
- }
- /* Allocate the Environment handle */
- rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
- if (rc != SQL_SUCCESS)
- {
- fprintf (stdout, "Environment Handle Allocation failed\nExiting!!\n");
- return (1);
- }
- /* Set the ODBC version to 3.0 */
- rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
- if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!\n"))
- return (1);
- /* Allocate the connection handle */
- rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
- if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!\n"))
- return (1);
- /* Establish the database connection */
- rc = SQLConnect (hdbc, dsn, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!\n"))
- return (1);
- /* Get version information from the database server
- If version < 9.x (not UDO enabled), exit with error message */
- rc = SQLGetInfo (hdbc, SQL_DBMS_VER, verInfoBuffer, BUFFER_LEN, &verInfoLen);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLGetInfo failed\n"))
- return 1;
- if ((strncmp ((char *) verInfoBuffer, "09", 2)) < 0 )
- {
- fprintf (stdout, "\n** This test can only be run against UDO-enabled database server -- version 9 or higher **\n");
- return 1;
- }
- /* Allocate the statement handle */
- rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!\n"))
- return (1);
- fprintf (stdout, "STEP 1 done...connected to database\n");
- /* STEP 2. Retrieve data from the 'item' table and display the results
- ** Close the result set cursor
- */
- /* Retrieve data from the 'item' table */
- fprintf (stdout, "Retrieving data from 'item' table\n\n");
- rc = SQLExecDirect (hstmt, selectStmt, SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
- goto Exit;
- /* Bind the result set columns */
- rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &item_num, 0, &cbItemNum);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 1)\n"))
- goto Exit;
- rc = SQLBindCol (hstmt, 2, SQL_C_CHAR, description, BUFFER_LEN, &cbDescription);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 2)\n"))
- goto Exit;
- rc = SQLBindCol (hstmt, 3, SQL_C_FLOAT, &unit_price, 5, &cbUnitPrice);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 3)\n"))
- goto Exit;
- while (1)
- {
- /* Fetch the data */
- rc = SQLFetch (hstmt);
- if (rc == SQL_NO_DATA_FOUND)
- break;
- else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFetch failed\n"))
- goto Exit;
- /* Display the results */
- fprintf (stdout, "Item Num: %d, Unit_price (DISTINCT TYPE dollar): %5.2f, Description: %s\n", item_num, unit_price, description);
- }
- /* Close the result set cursor */
- rc = SQLCloseCursor(hstmt);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLCloseCursor failed\n"))
- goto Exit;
- fprintf (stdout, "\nSTEP 2 done...data retrieved from 'item' table\n");
- Exit:
- /* CLEANUP: Close the statement handle
- ** Free the statement handle
- ** Disconnect from the datasource
- ** Free the connection and environment handles
- ** Exit
- */
- /* Close the statement handle */
- SQLFreeStmt (hstmt, SQL_CLOSE);
- /* Free the statement handle */
- SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
- /* Disconnect from the data source */
- SQLDisconnect (hdbc);
- /* Free the environment handle and the database connection handle */
- SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
- SQLFreeHandle (SQL_HANDLE_ENV, henv);
- fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
- in = getchar ();
- return (rc);
- }
|