CLIENT_LABEL variable support
You can set the CLIENT_LABEL variable in ODBC using the following methods:
The methods listed is in the “decreasing” order of priority, for example, setting CLIENT_LABEL in the “connection string” will have the highest priority even if it is set using other methods.
- Set as part of connection string: "DSN=MyDsn;CLIENT_LABEL=MyLabel"
- Use SQLSetConnectAttr( SQL_INFX_ATTR_CLIENT_LABEL )
- Specify in the .odbc.ini file (only on Linux/Unix): CLIENT_LABEL=MyLabel
- Use environment variable : "export CLIENT_LABEL=MyLabel"
- Use setnet32 CLIENT_LABEL variable
ODBC Sample Program
Following ODBC sample program shows the usage of the above mentioned methods. While running the sample program, you can run onstat -g env all | egrep "session|CLIENT_LABEL" on the Informix server command prompt.
/***************************************************************************
Licensed Materials - Property of HCL Technologies
*
"Restricted Materials of HCL"
*
HCL Informix ODBC Application
*
Copyright HCL 2020 All rights reserved.
*
Title: ClientLabel.c
*
Description: Support CLIENT_LABEL variable as part of connection string.
*
Author : Sheshnarayan Agrawal
*
***************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef NO_WIN32
#include <io.h>
#include <windows.h>
#include <conio.h>
#endif /NO_WIN32/
#define __REENTRANT
#include <signal.h>
#ifdef NO_WIN32
#include <sys/wait.h>
#include <pthread.h>
#endif
#include <time.h>
#include <infxcli.h>
#define ERRMSG_LEN 200
#define NAMELEN 300
#define NUM_OF_INSTANCE 2
SQLHDBC hdbc;
SQLHENV henv;
SQLHSTMT hstmt;
SQLCHAR connStrIn[NAMELEN];
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 */
}
void SetConnectionString()
{ //sprintf((char *) connStrIn, "DSN=sheshdsn"); sprintf((char *)connStrIn, "DSN=sheshdsn;CLIENT_LABEL=FromOdbcApp"); return; }
int main (long argc,
char* argv[])
{
/* Miscellaneous variables */
SQLRETURN rc = 0;
SQLINTEGER i = 0;
SQLINTEGER getSesID = 0;
SQLCHAR connStrOut[NAMELEN];
SQLSMALLINT connStrOutLen;
/* Allocate the Environment handle */
rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
if (rc != SQL_SUCCESS)
{ fprintf (stdout, "Environment Handle Allocation failed\nExiting!!"); exit (-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(main) in Step 1 – SQLSetEnvAttr failed\nExiting!!"))
exit (-1);
/* Allocate the connection handle */
rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error(main) in Step 2 – Connection Handle Allocation failed\nExiting!!"))
exit (-1);
/*
rc = SQLSetConnectAttr(hdbc, SQL_INFX_ATTR_CLIENT_LABEL, (SQLPOINTER)"FromSetConnect", SQL_NTS);
if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error(main) in Step 2 – Connection Handle Allocation failed\nExiting!!"))
exit (-1);
*/
/* Establish the database connection */
SetConnectionString();
rc = SQLDriverConnect (hdbc, NULL, connStrIn, SQL_NTS, connStrOut, NAMELEN, &connStrOutLen, SQL_DRIVER_NOPROMPT);
if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error(main) in Step 3 – SQLDriverConnect failed\nExiting!!"))
exit (-1);
char clientLabel[129];
/*
rc = SQLGetConnectAttr(hdbc, SQL_INFX_ATTR_CLIENT_LABEL, (char *)clientLabel, 128, NULL);
if( rc != SQL_NO_DATA)
{ if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error(main) in Step 2 -- Connection Handle Allocation failed\nExiting!!"))
exit (-1); printf("\nSQLGetConnectAttr(SQL_INFX_ATTR_CLIENT_LABEL) = %s \n", clientLabel); }
*/
printf("\nDatabase connection successful\n");
printf("\nHit <Enter> to exit the program \n");
SQLINTEGER in = getchar();
Exit:
/* CLEANUP: Close the statement handle
Free the statement handle
Disconnect from the datasource
Free the connection and environment handles
Exit
*/
/* 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);
return (rc);
}