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

📄 wide_util.c

📁 sybase数据库ct library的开发,使用了所以有函数
💻 C
字号:
#include <stdio.h>
#include <ctpublic.h>
#include "example.h"
#include "exutils.h"
#include "wide_example.h"

/* 
** init_db(cntx_ptr)
** 
** Type of function:
**	initialize the library and callback.
**
** Purpose:
**	This function allocates the context and initialize the library.
**	It also install the callback routines. It is called at the very
**	beginning of this wide_curdyn program.
**
** Parameters:
**	cntx_ptr - Pointer to the contex structure.
**
** Return:
**	CS_SUCCEED if everything goes fine
**	otherwise a failure message will be displayed and failure code will
**	be returned.
*/
	
char	row_buffer[MAX_COLUMN][MAX_COLSIZE] ;

CS_RETCODE      init_db (cntx_ptr)
CS_CONTEXT      **cntx_ptr ;
{
         CS_RETCODE      retcode ;

         /* 
	 ** allocate a context 
	 */
         retcode = cs_ctx_alloc (EX_CTLIB_VERSION,cntx_ptr );
                 RETURN_IF ( retcode, "cs_ctx_alloc ");

         /* 	
	 ** initialize the library 
	 */
         retcode = ct_init (*cntx_ptr, EX_CTLIB_VERSION );
                 RETURN_IF ( retcode, "ct_init ");

         /* 
	 ** install the server message callback 
	 */
         retcode = ct_callback ( *cntx_ptr, NULL, CS_SET, CS_SERVERMSG_CB,
                                 (CS_VOID *)ex_servermsg_cb) ;
                 RETURN_IF ( retcode, "ct_callback ");

         /* 
	 ** install the client message callback 
	 */
         retcode = ct_callback (*cntx_ptr, NULL, CS_SET, CS_CLIENTMSG_CB,
                                 (CS_VOID *)ex_clientmsg_cb);
                 RETURN_IF ( retcode, "ct_callback: CLIENTMSG ");

         return retcode ;
}

/*
** cleanup_db(cntx_ptr,status)
**
** Type of function:
**      Close the connection and drop the context
**
** Purpose:
**      This function closes the connection to the server. It also clean up
**      the contex structure. This function is called at the end of this
**      wide_curupd program.
**
** Parameters:
**      cntx_ptr - Pointer to the contex structure.
**      status   - To specify type of exit.
**
** Return:
**      CS_SUCCEED if everything goes fine
**      otherwise a failure message will be displayed and failure code will
**      be returned.
*/

CS_RETCODE      cleanup_db ( cntx_ptr, status )
CS_CONTEXT      *cntx_ptr ;
CS_RETCODE      status ;
{
         CS_RETCODE      retcode ;
         CS_INT          exit_type ;

         exit_type = ( status != CS_SUCCEED ) ? CS_FORCE_EXIT : CS_UNUSED ;

         /* 
	 ** close and cleanup connection to the server 
	 */
         retcode = ct_exit (cntx_ptr, exit_type );
                 RETURN_IF ( retcode, "db_cleanup:ct_exit ");

         /* 
	 ** drop the context 
	 */
         retcode = cs_ctx_drop (cntx_ptr) ;
                 RETURN_IF ( retcode, "db_cleanup:cs_drop ");

         return retcode ;
}

/*
** connect_db(cntx_ptr, conn_ptr, user_name, password )
**
** Type of function:
**      connect to the server
**
** Purpose:
**	This function makes the connection to the server. It sets the
**	appropriate user name and password.
**
** Parameters:
**      cntx_ptr - Pointer to the contex structure.
**      conn_ptr - Pointer to the connection structure.
**	user_name- User name.
**	password - Password.
**
** Return:
**      CS_SUCCEED if everything goes fine
**      otherwise a failure message will be displayed and failure code will
**      be returned.
*/

CS_RETCODE      connect_db (cntx_ptr, conn_ptr, user_name, password )
CS_CONTEXT      *cntx_ptr ;
CS_CONNECTION   **conn_ptr ;
CS_CHAR         *user_name, *password ;
{
         CS_RETCODE      retcode ;

         /* 
	 ** Allocate a connection pointer 
	 */
         retcode = ct_con_alloc (cntx_ptr, conn_ptr );
                 RETURN_IF ( retcode, "connect_db: ct_con_alloc ");

         /* 
	 ** Set the username and password properties 
	 */
         retcode = ct_con_props ( *conn_ptr, CS_SET, CS_USERNAME, user_name,
                                         CS_NULLTERM, NULL );
                 RETURN_IF ( retcode, "connect_db: ct_con_props: username ");

         retcode = ct_con_props ( *conn_ptr, CS_SET, CS_PASSWORD, password,
                                         CS_NULLTERM, NULL );
                 RETURN_IF ( retcode, "connect_db: ct_con_props: password");

         /* 
	 ** connect to the server 
	 */
         retcode = ct_connect ( *conn_ptr, NULL, 0 ) ;
                 RETURN_IF ( retcode, "connect_db: ct_connect ") ;

         return  CS_SUCCEED ;
}

/*
** handle_returns( cmd_ptr)
**
** Type of function:
**      process the return result type.
**
** Purpose:
**
** Parameters:
**      cmd_ptr - Pointer to the command structure.
**
** Return:
**      CS_SUCCEED if everything goes fine
**      otherwise a failure message will be displayed and failure code will
**      be returned.
*/

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_PARAM_RESULT :
                                   printf("TYPE: PARAM 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_CURSOR_RESULT :
                                   printf("TYPE: CURSOR RESULT ");
                                   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 ;

                         default    :
                                   RETURN_IF ( CS_FAIL, "unknown results \n");
                                   break ;
                 } ;
         }
         if ( results_ok == CS_END_RESULTS )
                 return CS_SUCCEED ;
         return CS_FAIL ;
}

/*
** fetch_n_print ( cmd_ptr )
**
** Type of function:
**	fetch the bound data into host variable.
**
** Purpose:
**
** Parameters:
**      cmd_ptr - Pointer to the command structure.
**
** Return:
**      CS_SUCCEED if everything goes fine
**      otherwise a failure message will be displayed and failure code will
**      be returned.
*/

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 ", row_buffer[i-1] );
                 }
                 printf("\n");
                 single_row( cmd_ptr );
         }

         if ( retcode != CS_END_DATA)
                 RETURN_IF ( retcode, "fetch_n_print ") ;

         return CS_SUCCEED ;
}

⌨️ 快捷键说明

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