⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diag_example.c

📁 sybase linux or unix develope library package
💻 C
字号:
/* Program name : diag_example.c **** Description  : This program accepts a SQL statement from the user and makes use of **		  ct_diag to report error messages. **		  The routine 'call_diag' is called every time a call is made that**		  could generate a error.**** Tests to try : (1) To test server messages, simply issue an invalid SQL statement**		      such as: select * from a non-existent table**		  (2) To test client-library messages, set the datatype for the bind**		      to a datatype different from the expected results.**		      ( Change datafmt.datatype ).**** References   : Open Client-Library/C reference manual. Refer to the sections**                on Inline error handling and ct_diag.***/#include<stdio.h>#include<ctpublic.h>#include<cstypes.h>#include<string.h>#include<stdlib.h>#include <example.h>/* Macro definitions for error handling */#define RETURN_IF(a,b) if (a != CS_SUCCEED) {fprintf(stdout,"error in %s\n",b); return a;}#define EXIT_IF(a) if (a != CS_SUCCEED)\        {fprintf(stderr, "FATAL ERROR! Line %d\n", __LINE__);exit(-1);}/* Global variables */CS_RETCODE         retcode;CS_CHAR		   rowbuffer[10][255]; /* Function declarations */int       	 call_diag       PROTOTYPE(( CS_CONNECTION  *));CS_RETCODE       print_ClientMsg PROTOTYPE(( CS_CLIENTMSG   *));CS_RETCODE       print_ServerMsg PROTOTYPE(( CS_SERVERMSG   *));CS_RETCODE       send_sql        PROTOTYPE(( CS_COMMAND     *,					     CS_CHAR	    *                                          ));CS_RETCODE       handle_returns PROTOTYPE (( CS_COMMAND     *));CS_RETCODE       bind_columns   PROTOTYPE (( CS_COMMAND     *));CS_RETCODE       fetch_n_print  PROTOTYPE (( CS_COMMAND     *));int main(){  	CS_CONTEXT        *context;  	CS_COMMAND        *cmd;  	CS_CONNECTION     *connection;	CS_CHAR		  sql_stmt[100];  		/* Get a context structure */        retcode = cs_ctx_alloc( CS_VERSION_100, &context );		EXIT_IF ( retcode );		/* Initialize client library */	  	retcode = ct_init( context, CS_VERSION_100 );		EXIT_IF ( retcode );		/* Allocate a connection pointer */  	retcode = ct_con_alloc( context, &connection );		EXIT_IF ( retcode );		/* Set the username and password properties */  	retcode = ct_con_props(connection, CS_SET, CS_USERNAME, EX_USERNAME, 				CS_NULLTERM, NULL);		EXIT_IF ( retcode );  	retcode = ct_con_props(connection, CS_SET, CS_PASSWORD, EX_PASSWORD, 				CS_NULLTERM, NULL);		EXIT_IF ( retcode );  		/* Establish a connection to the server */      	retcode = ct_connect(connection, NULL,  0);  	if (retcode != CS_SUCCEED)  	{    		printf("Connection to the Server failed. \n");		EXIT_IF ( retcode );  	} 		/* Initialize inline error handling */   	retcode = ct_diag(connection, CS_INIT, CS_UNUSED, CS_UNUSED, NULL);  	if (retcode != CS_SUCCEED)  	{    		printf("Initializing ct_diag failed. \n");		EXIT_IF ( retcode );  	}  		/* Allocate a command structure */    	retcode = ct_cmd_alloc(connection, &cmd);  	if (retcode != CS_SUCCEED)  	{    		printf("ct_cmd_alloc for cmd failed\n"); 		EXIT_IF ( retcode );  	}		/* Send a command to the server */	printf("Enter the SQL statement to execute: ");	fflush(stdin);	gets(sql_stmt);  	retcode = send_sql(cmd, sql_stmt);		/* check for errors*/        call_diag(connection);		/* handle results from previous command */  	retcode = handle_returns(cmd);		/* check for errors*/    	call_diag(connection);		/* Close connection */	retcode = ct_close( connection, CS_UNUSED );  	if (retcode != CS_SUCCEED)  	{    	    printf("Error in connection drop \n");	    EXIT_IF ( retcode );  	}	printf("End of program run!\n");	exit(0);}int call_diag(connection)CS_CONNECTION *connection ;{  	CS_RETCODE 	ret;	CS_CLIENTMSG	client_msg;	CS_SERVERMSG	server_msg;	CS_INT		i, client_msg_Count = 0, server_msg_Count = 0;	/* Check to see if there are any messages in the CS_CLIENTMSG structure */    	ret = ct_diag(connection, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED,      			  &client_msg_Count);	if (ret == CS_SUCCEED )	{		/* If messages exist, print them */		if (client_msg_Count)		{			for ( i = 1; i <= client_msg_Count; i++)			{    				retcode = ct_diag(connection, CS_GET, CS_CLIENTMSG_TYPE, 						i, (void *)&client_msg);					RETURN_IF ( retcode, " ct_diag client msg");				print_ClientMsg(&client_msg);			}		}	}	/* Check to see if there are any messages in the CS_SERVERMSG structure */    	ret = ct_diag(connection, CS_STATUS, CS_SERVERMSG_TYPE, 				CS_UNUSED, &server_msg_Count);	if (ret == CS_SUCCEED )	{		/* If messages exist, print them */		if (server_msg_Count)		{			for ( i = 1; i <= server_msg_Count; i++)			{    				retcode = ct_diag(connection, CS_GET, CS_SERVERMSG_TYPE, 						i, (void *)&server_msg);					RETURN_IF ( retcode, " ct_diag server msg ");				print_ServerMsg(&server_msg);			}		}	}	/* Clear the structures of existing error messages */	ct_diag(connection, CS_CLEAR, CS_SERVERMSG_TYPE, CS_UNUSED, &server_msg);	ct_diag(connection, CS_CLEAR, CS_CLIENTMSG_TYPE, CS_UNUSED, &client_msg);	return ;}CS_RETCODE print_ClientMsg(client_msg)CS_CLIENTMSG *client_msg;{    	fprintf(stderr, "\nNumber: layer (%ld), Origin (%ld) \ntext%s\n",                    CS_LAYER(client_msg->msgnumber),                    CS_ORIGIN(client_msg->msgnumber),    			     client_msg->msgstring);	fprintf(stderr, "SEVERITY = (%ld) NUMBER = (%ld)\n",                CS_SEVERITY(client_msg->msgnumber), CS_NUMBER(client_msg->msgnumber));        fprintf(stderr, "Message String: %s\n", client_msg->msgstring);	if (client_msg->osstringlen > 0)        {                fprintf(stderr, "\nOS error : %s\n", client_msg->osstring);        }	fflush(stdout);	fflush(stderr);	return CS_SUCCEED;}CS_RETCODE print_ServerMsg(srvmsg)CS_SERVERMSG *srvmsg;{        fprintf(stderr, "\nServer message:\n");        fprintf(stderr, "Message number: %ld, Severity %ld, ",                srvmsg->msgnumber, srvmsg->severity);        fprintf(stderr, "State %ld, Line %ld\n", srvmsg->state, srvmsg->line);         if (srvmsg->svrnlen > 0)        {                fprintf(stderr, "Server '%s'\n", srvmsg->svrname);        }         if (srvmsg->proclen > 0)        {                fprintf(stderr, " Procedure '%s'\n", srvmsg->proc);        }         fprintf(stderr, "Message String: %s\n", srvmsg->text);        fflush(stderr);	fflush(stdout); 	return CS_SUCCEED;} CS_RETCODE    send_sql (cmd_ptr, sqltext)CS_COMMAND      *cmd_ptr ;CS_CHAR         sqltext[100];{        CS_RETCODE      retcode ;         printf("SQL = %s\n", sqltext);		/* Build and send the command to the server */        retcode = ct_command (cmd_ptr, CS_LANG_CMD, sqltext,                                CS_NULLTERM, CS_UNUSED );                RETURN_IF ( retcode, "ct_command ");         retcode = ct_send (cmd_ptr);                RETURN_IF ( retcode, "ct_send ");        return CS_SUCCEED ;}CS_RETCODE	handle_returns ( cmd_ptr )CS_COMMAND	*cmd_ptr ;{	CS_RETCODE	results_ok  ;	CS_INT		result_type ;		  /* Process all returned result types */	while (( results_ok = ct_results ( cmd_ptr, &result_type)) == CS_SUCCEED )	{		switch ((int)result_type)		{			case CS_ROW_RESULT:					  printf("TYPE: ROW RESULT \n");					  results_ok = bind_columns ( cmd_ptr );					  RETURN_IF(results_ok, "bind columns");							  results_ok = fetch_n_print ( cmd_ptr );					  RETURN_IF(results_ok, "fetch_n_print");					  break ;			case CS_CMD_SUCCEED:						  printf("TYPE: CMD SUCCEEDED \n");					  break ;			case CS_CMD_DONE   :					  printf("TYPE : CMD DONE \n");				 	  break ;			case CS_CMD_FAIL   :					  printf("TYPE: CMD FAIL \n");					  break ;			case CS_STATUS_RESULT :					  printf("TYPE: STATUS RESULTS \n");					  results_ok = bind_columns ( cmd_ptr );					  RETURN_IF ( results_ok, "bind columns");							  results_ok = fetch_n_print (cmd_ptr);					  RETURN_IF ( results_ok, "fetch_n_print");					  break ;                        case CS_CURSOR_RESULT :                                          printf("TYPE: CURSOR RESULT \n");                                          results_ok = bind_columns ( cmd_ptr );                                          RETURN_IF(results_ok, "bind columns");                                          results_ok = fetch_n_print(cmd_ptr);                                          RETURN_IF ( results_ok, "fetch row");                                          break ;			default    :					  RETURN_IF ( CS_FAIL, "unknown results \n");					  break ;		} ;	}	if ( results_ok == CS_END_RESULTS )	 	return CS_SUCCEED ;	return CS_FAIL ;}CS_RETCODE 	bind_columns (cmd_ptr)CS_COMMAND	*cmd_ptr ;{	CS_RETCODE	retcode ;	CS_INT		i , num_cols ;	CS_DATAFMT	target_format ;	retcode = ct_res_info (cmd_ptr, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);		RETURN_IF ( retcode, "ct_bind : ct_res_info ");	for ( i = 1 ; i <= num_cols ; i++ )	{		memset ( &target_format, 0, CS_SIZEOF(target_format)) ;		target_format.datatype = CS_CHAR_TYPE ;		target_format.maxlength = 255  ;		target_format.count    = 1 ;		target_format.format   = CS_FMT_NULLTERM ;			/* Bind returned data to host variables */		retcode = ct_bind ( cmd_ptr, i, &target_format, rowbuffer[i-1],					NULL, NULL ) ;		RETURN_IF ( retcode, "ct_bind : ");	}	return CS_SUCCEED ;}CS_RETCODE	fetch_n_print ( cmd_ptr )CS_COMMAND	*cmd_ptr ;{	CS_RETCODE 	retcode ;	CS_INT		i, num_cols ;	retcode = ct_res_info(cmd_ptr, CS_NUMDATA, &num_cols, CS_UNUSED, NULL );		RETURN_IF ( retcode, "fetch_n_print: ct_res_info ");		 /* Fetch the bound data into host variables */	while (( (retcode = ct_fetch(cmd_ptr, CS_UNUSED, CS_UNUSED, CS_UNUSED,				(CS_INT *)NULL)) == CS_SUCCEED )		||	      ( retcode == CS_ROW_FAIL ))	{		if ( retcode == CS_ROW_FAIL )		{			printf("ct_fetch returned row fail \n");			continue;		}		for ( i = 1 ; i <= num_cols ; i++ )		{			printf(" %s \t ", rowbuffer[i-1] );		}		printf("\n");	}	/* Check to see that all rows have been fetched */	if ( retcode != CS_END_DATA)		 RETURN_IF ( retcode, "fetch_n_print ") ;		return CS_SUCCEED ;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -