/****************************************************************************
** (c) 版权所有 IBM Corp. 2007 保留所有权利。
**
** 以下源代码示例("示例")属于国际
** 商业机器公司或其子公司之一(" IBM "),并且是
** 有版权和许可证,不得出售。 您可以使用、复制、修改和
** 在不向 IBM 付费的情况下,以任何形式分发样品,目的是
** 协助您开发应用程序。
**
** 示例代码是在 "原样 "的基础上提供给您的,不对以下方面作出保证
** 任何一种。 IBM 在此,本公司明确声明不作任何保证,无论是明示的还是
** 默示保证,包括但不限于以下默示保证
** 适销性和特定用途的适用性。 一些辖区确实
** 不允许排除或限制默示保证,因此上述
** 限制或排除条款可能不适用于您。 IBM SHALL NOT BE LIABLE FOR
** 您因使用、复制、修改或执行以下行为而遭受的任何损失
** 分发样品,即使 IBM 已被告知可能出现以下情况
** 此类损害赔偿。
*****************************************************************************
**
** 源文件名称: cli_info.C
**
** 示例:在客户端一级设置和获取信息
**
** DB2 使用的应用程序接口:
** sqleseti -- 设置客户信息
** sqleqryi -- 查询客户信息
** sqlesetc -- SET CLIENT
** sqleqryc -- 查询客户端
**
** 使用的结构:
** sqle_client_info
** sqle_conn_setting
** sqlca
**
**
*****************************************************************************
*
* 有关开发嵌入式 SQL 应用程序的信息,请参阅《开发嵌入式 SQL 应用程序》一书。
*
* 有关 DB2 API 的更多信息,请参阅《管理 API 参考》。
*
* 有关编程、编译和运行的最新信息 DB2
* 申请请访问 DB2 信息中心:
* http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/
#include <string.h>
#include <sqlenv.h>
#include " utilapi.h "
#if ((__cplusplus >= 199711L ) &&!定义了 DB2HP &&!定义了 DB2AIX )|| \
( DB2LINUX && ( __LP64__ || (__gnuc__ >= 3)) ) )
#include <iostream>
using namespace std;
#else
#include < iostream.h >
#endif
类 ApInfo
{
public:
static int ClientAppNameSetGet( );
static int ClientUseridSetGet( );
static int ClientWorkstationSetGet( );
static int ClientSuffixForAccountingStringSetGet( );
static int ClientConnectionAttrsSetGet( );
};
int ApInfo::ClientAppNameSetGet( )
{
struct sqlca sqlca;
struct sqle_client_info clientAppInfo[1 ];
无符号短 dbAliasLen;
char dbAlias[SQL_ALIAS_SZ + 1];
cout << "\n-----------------------------------------------------------";
cout << ""\nUSE THE DB2 APIs:" << endl;
cout << " sqleseti -- SET CLIENT INFORMATION" << endl;
cout << " sqleqryi -- 查询客户信息" << endl;
cout << "TO SET/GET THE CLIENT APPLICATION NAME:" << endl;
// 指定所有连接
dbAliasLen = 0;
strcpy( dbAlias, "");
// 初始化 clientAppInfo
clientAppInfo[0 ].type = SQLE_CLIENT_INFO_APLNAME;
clientAppInfo[0].pValue =
(char *)new char[SQLE_CLIENT_APPLNAME_MAX_LEN + 1];
// 设置客户端应用程序名称
strcpy( clientAppInfo[0].pValue, "ClientApplicationName" );
clientAppInfo[0 ].length = strlen((char *)clientAppInfo[0].pValue );
cout << "\n 将客户端应用程序名称设置为值:" << endl;
cout << " " << clientAppInfo[0].pValue << endl;
// 设置客户信息
sqleseti( dbAliasLen, dbAlias, 1, &clientAppInfo[0 ], &sqlca);
DB2_API_CHECK( "客户端应用程序名称--设置");
// 获取客户端应用程序名称
strcpy( clientAppInfo[0].pValue, "");
cout << " 获取客户端应用程序名称。" << endl;
// 查询客户信息
sqleqryi( dbAliasLen, dbAlias, 1, &clientAppInfo[0 ], &sqlca);
DB2_API_CHECK( "客户端应用程序名称--获取");
cout << " 客户应用程序名称是:" << endl;
cout << " " << clientAppInfo[0].pValue << endl;
// 释放分配的内存
删除 [] clientAppInfo[0].pValue;
return 0;
} //ApInfo::ClientAppNameSetGet
int ApInfo::ClientUseridSetGet( )
{
struct sqlca sqlca;
struct sqle_client_info clientAppInfo[1 ];
无符号短 dbAliasLen;
char dbAlias[SQL_ALIAS_SZ + 1];
cout << "\n-----------------------------------------------------------";
cout << ""\nUSE THE DB2 APIs:" << endl;
cout << " sqleseti -- SET CLIENT INFORMATION" << endl;
cout << " sqleqryi -- 查询客户信息" << endl;
cout << "TO SET/GET THE CLIENT USER ID:" << endl;
// 指定所有连接
dbAliasLen = 0;
strcpy( dbAlias, "");
// 初始化 clientAppInfo
clientAppInfo[0 ].type = (unsigned short)SQLE_CLIENT_INFO_USERID;
clientAppInfo[0].pValue = (char *)new char[SQLE_CLIENT_USERID_MAX_LEN + 1];
// 设置客户端用户 ID
strcpy( clientAppInfo[0].pValue, "ClientUserid" );
clientAppInfo[0 ].length = strlen((char *)clientAppInfo[0].pValue );
cout << "\n 将客户端用户 ID 设置为值:" << endl;
cout << " " << clientAppInfo[0].pValue << endl;
// 设置客户信息
sqleseti( dbAliasLen, dbAlias, 1, &clientAppInfo[0 ], &sqlca);
DB2_API_CHECK( "客户用户 ID -- 设置");
// 获取客户端用户 ID
strcpy( clientAppInfo[0].pValue, "");
cout << " 获取客户端用户 ID。" << endl;
// 查询客户信息
sqleqryi( dbAliasLen, dbAlias, 1, &clientAppInfo[0 ], &sqlca);
DB2_API_CHECK( "客户用户 ID -- 获取");
cout << " 客户端用户 ID 是:" << endl;
cout << " " << clientAppInfo[0].pValue << endl;
// 释放分配的内存
删除 [] clientAppInfo[0].pValue;
return 0;
} //ApInfo::ClientUseridSetGet
int ApInfo::ClientWorkstationSetGet( )
{
struct sqlca sqlca;
struct sqle_client_info clientAppInfo[1 ];
无符号短 dbAliasLen;
char dbAlias[SQL_ALIAS_SZ + 1];
cout << "\n-----------------------------------------------------------";
cout << ""\nUSE THE DB2 APIs:" << endl;
cout << " sqleseti -- SET CLIENT INFORMATION" << endl;
cout << " sqleqryi -- 查询客户信息" << endl;
cout << "TO SET/GET THE CLIENT WORKSTATION NAME:" << endl;
// 指定所有连接
dbAliasLen = 0;
strcpy( dbAlias, "");
// 初始化 clientAppInfo
clientAppInfo[0 ].type = SQLE_CLIENT_INFO_WRKSTNNAME;
clientAppInfo[0].pValue =
(char *)new char[SQLE_CLIENT_WRKSTNNAME_MAX_LEN + 1];
// 设置客户端工作站
strcpy( clientAppInfo[0].pValue, "ClientWorkstation" );
clientAppInfo[0 ].length = strlen((char *)clientAppInfo[0].pValue );
cout << "\n 将客户端工作站名称设置为值:";
cout << "\n " << clientAppInfo[0].pValue << endl;
// 设置客户信息
sqleseti( dbAliasLen, dbAlias, 1, &clientAppInfo[0 ], &sqlca);
DB2_API_CHECK( "客户工作站名称--设置");
// 获取客户端工作站
strcpy( clientAppInfo[0].pValue, "");
cout << " 获取客户端工作站名称。" << endl;
// 查询客户信息
sqleqryi( dbAliasLen, dbAlias, 1, &clientAppInfo[0 ], &sqlca);
DB2_API_CHECK( "客户工作站名称 -- 获取");
cout << " 客户端工作站名称是:" << endl;
cout << " " << clientAppInfo[0].pValue << endl;
// 释放分配的内存
删除 [] clientAppInfo[0].pValue;
return 0;
} //ApInfo::ClientWorkstationSetGet
int ApInfo::ClientSuffixForAccountingStringSetGet( )
{
struct sqlca sqlca;
struct sqle_client_info clientAppInfo[1 ];
char clientAccStrSuffix[ ] = "ClientSuffixForAccountingString";
无符号短 dbAliasLen;
char dbAlias[SQL_ALIAS_SZ + 1];
cout << "\n-----------------------------------------------------------";
cout << ""\nUSE THE DB2 APIs:" << endl;
cout << " sqleseti -- SET CLIENT INFORMATION" << endl;
cout << " sqleqryi -- 查询客户信息" << endl;
cout << "TO SET/GET THE CLIENT SUFFIX FOR THE ACCOUNTING STRING:";
cout << endl;
// 指定所有连接
dbAliasLen = 0;
strcpy( dbAlias, "");
// 初始化 clientAppInfo
clientAppInfo[0 ].type = SQLE_CLIENT_INFO_ACCTSTR;
clientAppInfo[0].pValue =
(char *)new char[SQLE_CLIENT_APPLNAME_MAX_LEN + 1];
// 为会计字符串设置客户后缀
strcpy( clientAppInfo[0].pValue, "ClientSuffixForAccountingString" );
clientAppInfo[0 ].length = strlen((char *)clientAppInfo[0].pValue );
cout << "\n 使用 DB2 API sqleseti 设置客户端后缀";
cout << "\n 为会计字符串的值:";
cout << "\n " << clientAppInfo[0].pValue << endl;
// 设置客户信息
sqleseti( dbAliasLen, dbAlias, 1, &clientAppInfo[0 ], &sqlca);
DB2_API_CHECK( "会计字符串的客户后缀 -- 设置");
// 为会计字符串获取客户后缀
strcpy( clientAppInfo[0].pValue, "");
cout << " 获取会计字符串的客户后缀。" << endl;
// 查询客户信息
sqleqryi( dbAliasLen, dbAlias, 1, &clientAppInfo[0 ], &sqlca);
DB2_API_CHECK( "会计字符串的客户后缀 -- 获取");
cout << " 会计字符串的客户后缀是:" << endl;
cout << " " << clientAppInfo[0].pValue << endl;
// 释放分配的内存
删除 [] clientAppInfo[0].pValue;
return 0;
} //ApInfo::ClientSuffixForAccountingStringSetGet
int ApInfo::ClientConnectionAttrsSetGet( )
{
struct sqlca sqlca;
struct sqle_conn_setting clientAppInfo[8 ];
cout << "\n-----------------------------------------------------------";
cout << ""\nUSE THE DB2 APIs:" << endl;
cout << " sqlesetc -- SET CLIENT" << endl;
cout << " sqleqryc -- QUERY CLIENT" << endl;
cout << "TO SET/GET THE CLIENT CONNECTION ATTRIBUTES:" << endl;
// 初始化 clientAppInfo
clientAppInfo[0 ].type = SQL_CONNECT_TYPE;
clientAppInfo[1 ].type = SQL_RULES;
clientAppInfo[2 ].type = SQL_DISCONNECT;
clientAppInfo[3 ].type = SQL_SYNCPOINT;
clientAppInfo[4 ].type = SQL_MAX_NETBIOS_CONNECTIONS;
clientAppInfo[5 ].type = SQL_DEFERRED_PREPARE;
clientAppInfo[6 ].type = SQL_CONNECT_NODE;
clientAppInfo[7 ].type = SQL_ATTACH_NODE;
clientAppInfo[0 ].value = SQL_CONNECT_2;
clientAppInfo[1 ].value = SQL_RULES_STD;
clientAppInfo[2 ].value = SQL_DISCONNECT_CONND;
clientAppInfo[3 ].value = SQL_SYNC_ONEPHASE;
clientAppInfo[4 ].value = 16;
clientAppInfo[5 ].value = SQL_DEFERRED_PREPARE_YES;
clientAppInfo[6 ].value = 3;
clientAppInfo[7 ].value = 3;
// 设置客户端连接属性
cout << "\n 将客户端连接属性设置为以下值:";
cout << "\n SQL_CONNECT_TYPE = SQL_CONNECT_2 ";
cout << "\n SQL_RULES = SQL_RULES_STD";
cout << "\n SQL_DISCONNECT = SQL_DISCONNECT_CONND";
cout << "\n SQL_SYNCPOINT = SQL_SYNC_ONEPHASE";
cout << "\n SQL_MAX_NETBIOS_CONNECTIONS = 16";
cout << "\n SQL_DEFERRED_PREPARE = SQL_DEFERRED_PREPARE_YES";
cout << "\n SQL_CONNECT_NODE = 3";
cout << "\n SQL_ATTACH_NODE = 3" << endl;
// 设置客户端
sqlesetc( &clientAppInfo[0 ], 8, &sqlca);
DB2_API_CHECK( "客户端连接属性 - 设置");
// 获取客户端连接属性
// 重置 clientAppInfo
clientAppInfo[0 ].value = SQL_CONNECT_1;
clientAppInfo[1 ].value = SQL_RULES_DB2;
clientAppInfo[2 ].value = SQL_DISCONNECT_EXPL;
clientAppInfo[3 ].value = SQL_SYNC_TWOPHASE;
clientAppInfo[4 ].value = 1;
clientAppInfo[5 ].value = SQL_DEFERRED_PREPARE_NO;
clientAppInfo[6 ].value = 1;
clientAppInfo[7 ].value = 1;
cout << " 获取客户端连接属性。" << endl;
// 查询客户端
sqleqryc( &clientAppInfo[0 ], 8, &sqlca);
DB2_API_CHECK( "Client Conn. Attrs. -- get");
cout << " 客户端连接属性为:" << endl;
switch ( clientAppInfo[0 ].value)
{
个案 SQL_CONNECT_1:
cout << " SQL_CONNECT_TYPE = SQL_CONNECT_1 ";
cout << endl;
break;
个案 SQL_CONNECT_2:
cout << " SQL_CONNECT_TYPE = SQL_CONNECT_2 ";
cout << endl;
break;
缺省值:
break;
}
switch ( clientAppInfo[1 ].value)
{
个案 SQL_RULES_DB2:
cout << " SQL_RULES = SQL_RULES_DB2 ";
cout << endl;
break;
案例 SQL_RULES_STD:
cout << " SQL_RULES = SQL_RULES_STD";
cout << endl;
break;
缺省值:
break;
}
switch ( clientAppInfo[2 ].value)
{
案例 SQL_DISCONNECT_EXPL:
cout << " SQL_DISCONNECT = SQL_DISCONNECT_EXPL";
cout << endl;
break;
案例 SQL_DISCONNECT_COND:
cout << " SQL_DISCONNECT = SQL_DISCONNECT_CONND";
cout << endl;
break;
案例 SQL_DISCONNECT_AUTO:
cout << " SQL_DISCONNECT = SQL_DISCONNECT_EXPL";
cout << endl;
break;
缺省值:
break;
}
switch ( clientAppInfo[3 ].value)
{
案例 SQL_SYNC_TWOPHASE:
cout << " SQL_SYNCPOINT = SQL_SYNC_TWOPHASE" << endl;
break;
案例 SQL_SYNC_ONEPHASE:
cout << " SQL_SYNCPOINT = SQL_SYNC_ONEPHASE" << endl;
break;
情况 SQL_SYNC_NONE:
cout << " SQL_SYNCPOINT = SQL_SYNC_NONE" << endl;
break;
缺省值:
break;
}
cout << " SQL_MAX_NETBIOS_CONNECTIONS = "
<< clientAppInfo[4 ].value << endl;
switch ( clientAppInfo[5 ].value)
{
例 SQL_DEFERRED_PREPARE_NO:
cout << " SQL_DEFERRED_PREPARE = SQL_DEFERRED_PREPARE_NO"
<< endl;
break;
情况 SQL_DEFERRED_PREPARE_YES:
cout << " SQL_DEFERRED_PREPARE = SQL_DEFERRED_PREPARE_YES"
<< endl;
break;
案例 SQL_DEFERRED_PREPARE_ALL:
cout << " SQL_DEFERRED_PREPARE = SQL_DEFERRED_PREPARE_ALL"
<< endl;
break;
缺省值:
break;
}
cout << " SQL_CONNECT_NODE = "
<< clientAppInfo[6 ].value << endl;
cout << " SQL_ATTACH_NODE = "
<< clientAppInfo[7 ].value << endl;
return 0;
} //ApInfo::ClientConnectionAttrsSetGet
int main(int argc, char *argv[])
{
int rc = 0;
ApInfo 信息
// 检查命令行参数
如果 (argc!= 1)
{
cout << ""\nUSAGE: " << argv[0] << endl;
return 1;
}
cout << ""\nHOW TO SET AND GET INFOMATION AT THE CLIENT LEVEL."
<< endl;
rc = info.ClientAppNameSetGet( );
rc = info.ClientUseridSetGet( );
rc = info.ClientWorkstationSetGet( );
rc = info.ClientSuffixForAccountingStringSetGet( );
rc = info.ClientConnectionAttrsSetGet( );
return 0;
}//main