📄 sybasedb.c
字号:
/****************************************************** * SybaseDb.cpp: implementation of the CDatabase class. * */#define _AFXDLL/*#include "stdafx.h"*/#include "sybasedb.h"#ifdef SYBASE#include <stdio.h>#include <string.h>#include <time.h>#include <stdarg.h>#define RETURN_IF(pDatabase, bSuccess,szErrorMsg) \ if(bSuccess != CS_SUCCEED) \ {\ DB_SetLastError(pDatabase, szErrorMsg);\ return FALSE;\ }/****************************************************** * Construction/Destruction */void DB_Construct(CDatabase *pDatabase){ pDatabase->m_bOpened = FALSE; pDatabase->m_pContext = NULL; pDatabase->m_pConnection = NULL; pDatabase->m_pCommand = NULL;}void DB_Destruct(CDatabase *pDatabase){ DB_Close(pDatabase, TRUE);}BOOL DB_Open(CDatabase *pDatabase,char *pServerName, char *pDatabaseName, char *pUserName, char *pPassword){ CS_RETCODE rc; CS_BOOL bBulk = CS_TRUE; char szSQL[256]; if(pDatabase->m_bOpened) { DB_SetLastError(pDatabase, "Sybase DB_Open Client 已经打开一个数据库!"); return FALSE; } rc = cs_ctx_alloc(EX_CTLIB_VERSION, &pDatabase->m_pContext); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 分配环境指针失败!"); rc = ct_init(pDatabase->m_pContext, EX_CTLIB_VERSION); if(rc!=CS_SUCCEED) { /* Deallocate the CS_CONTEXT structure */ cs_ctx_drop(pDatabase->m_pContext); pDatabase->m_pContext = NULL; RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 初始化失败!"); } rc = ct_con_alloc(pDatabase->m_pContext,&pDatabase->m_pConnection); if(rc!=CS_SUCCEED) { /* Deallocate the CS_CONTEXT structure */ ct_exit(pDatabase->m_pContext, CS_FORCE_EXIT); cs_ctx_drop(pDatabase->m_pContext); pDatabase->m_pContext = NULL; RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 分配连接指针失败!"); } rc = ct_con_props(pDatabase->m_pConnection,CS_SET,CS_USERNAME,pUserName,CS_NULLTERM,NULL); if(rc!=CS_SUCCEED) { /* Deallocate the CS_CONNECTION structure */ ct_con_drop(pDatabase->m_pConnection); pDatabase->m_pConnection = NULL; /* Deallocate the CS_CONTEXT structure */ ct_exit(pDatabase->m_pContext, CS_FORCE_EXIT); cs_ctx_drop(pDatabase->m_pContext); pDatabase->m_pContext = NULL; DB_SetLastError(pDatabase, "Sybase DB_Open Client 设置用户名属性失败!"); return FALSE; } rc = ct_con_props(pDatabase->m_pConnection,CS_SET,CS_PASSWORD,pPassword,CS_NULLTERM,NULL); if(rc!=CS_SUCCEED) { /* Deallocate the CS_CONNECTION structure */ ct_con_drop(pDatabase->m_pConnection); pDatabase->m_pConnection = NULL; /* Deallocate the CS_CONTEXT structure */ ct_exit(pDatabase->m_pContext, CS_FORCE_EXIT); cs_ctx_drop(pDatabase->m_pContext); pDatabase->m_pContext = NULL; DB_SetLastError(pDatabase, "Sybase DB_Open Client 设置用户登录口令属性失败!"); return FALSE; } rc = ct_con_props(pDatabase->m_pConnection, CS_SET, CS_BULK_LOGIN, (CS_VOID *)&bBulk, CS_UNUSED, NULL); if(rc!=CS_SUCCEED) { /* Deallocate the CS_CONNECTION structure */ ct_con_drop(pDatabase->m_pConnection); pDatabase->m_pConnection = NULL; /* Deallocate the CS_CONTEXT structure */ ct_exit(pDatabase->m_pContext, CS_FORCE_EXIT); cs_ctx_drop(pDatabase->m_pContext); pDatabase->m_pContext = NULL; DB_SetLastError(pDatabase, "Sybase DB_Open Client 设置用户登录口令属性失败!"); return FALSE; } rc = ct_connect(pDatabase->m_pConnection,pServerName,(pServerName == NULL) ? 0 : CS_NULLTERM); if(rc!=CS_SUCCEED) { /* Deallocate the CS_CONNECTION structure */ ct_con_drop(pDatabase->m_pConnection); pDatabase->m_pConnection = NULL; /* Deallocate the CS_CONTEXT structure */ ct_exit(pDatabase->m_pContext, CS_FORCE_EXIT); cs_ctx_drop(pDatabase->m_pContext); pDatabase->m_pContext = NULL; DB_SetLastError(pDatabase, "Sybase DB_Open Client 连接数据库服务器失败!"); return FALSE; } sprintf(szSQL, "use %s", pDatabaseName); if(!DB_ExecuteSQL(pDatabase, szSQL)) { /* DB_Close the connection */ ct_close(pDatabase->m_pConnection, CS_FORCE_CLOSE); /* Deallocate the CS_CONNECTION structure */ ct_con_drop(pDatabase->m_pConnection); pDatabase->m_pConnection = NULL; /* Deallocate the CS_CONTEXT structure */ ct_exit(pDatabase->m_pContext, CS_FORCE_EXIT); cs_ctx_drop(pDatabase->m_pContext); pDatabase->m_pContext = NULL; DB_SetLastError(pDatabase, "Sybase DB_Open Client 连接数据库失败!"); return FALSE; } pDatabase->m_bOpened = TRUE; /* ** Install client and server message handlers. */ /* Function to call when client-Library signals error *//* ct_callback(pDatabase->m_pContext, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *)ClientMsgHandler);*/ /* Function to call when server sends a message to client *//* ct_callback(pDatabase->m_pContext, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *)ServerMsgHandler);*/ /* Function to call when common libraries signals error */ cs_config(pDatabase->m_pContext, CS_SET, CS_MESSAGE_CB, (CS_VOID *)CommonMsgHandler, CS_UNUSED, NULL); return TRUE;}BOOL DB_Close(CDatabase *pDatabase, BOOL bForceClose){ CS_INT nCloseMode; if(!pDatabase->m_bOpened) return TRUE; if(pDatabase->m_pCommand) { ct_cancel(NULL, pDatabase->m_pCommand, CS_CANCEL_ALL); ct_cmd_drop(pDatabase->m_pCommand); pDatabase->m_pCommand = NULL; } if(bForceClose) nCloseMode = CS_FORCE_CLOSE; else nCloseMode = CS_UNUSED; /* DB_Close the connection */ ct_close(pDatabase->m_pConnection, nCloseMode); /* Deallocate the CS_CONNECTION structure */ ct_con_drop(pDatabase->m_pConnection); pDatabase->m_pConnection = NULL; /* Deallocate the CS_CONTEXT structure */ ct_exit(pDatabase->m_pContext, CS_FORCE_EXIT); cs_ctx_drop(pDatabase->m_pContext); pDatabase->m_pContext = NULL; pDatabase->m_bOpened = FALSE; return TRUE;}BOOL DB_IsOpen(CDatabase *pDatabase){ return(pDatabase->m_bOpened);}BOOL DB_ExecuteSQL(CDatabase *pDatabase, char *pSQL){ CS_COMMAND* pCommand; CS_RETCODE rc; /* if(!pDatabase->m_bOpened) RETURN_IF(pDatabase, CS_FAIL, "Sybase DB_Open Client 数据库未打开!"); */ rc = ct_cmd_alloc(pDatabase->m_pConnection, &pCommand); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 分配命令结构指针失败!"); rc = ct_command(pCommand, CS_LANG_CMD, (CS_CHAR*)pSQL, CS_NULLTERM, CS_UNUSED); if(rc!=CS_SUCCEED) { DB_SetLastError(pDatabase, "Sybase DB_Open Client 初始化命令失败!"); ct_cmd_drop(pCommand); return FALSE; } rc = ct_send(pCommand); if(rc!=CS_SUCCEED) { DB_SetLastError(pDatabase, "Sybase DB_Open Client 向数据库服务器发送命令失败!"); ct_cmd_drop(pCommand); return FALSE; } rc = DB_HandleResults(pDatabase, pCommand); ct_cmd_drop(pCommand); if(rc == CS_SUCCEED) return TRUE; DB_SetLastError(pDatabase, "Sybase DB_Open Client 执行SQL语句失败!"); return FALSE;}void DB_SetLastError(CDatabase *pDatabase, char *pErrorMsg){ if(strlen(pErrorMsg) > ERROR_BUFFER_LEN) { strncpy(pDatabase->m_szErrorBuffer, pErrorMsg, ERROR_BUFFER_LEN-4); strcpy(pDatabase->m_szErrorBuffer+ERROR_BUFFER_LEN-4,"..."); } else { strcpy(pDatabase->m_szErrorBuffer, pErrorMsg); }}void DB_GetLastError(CDatabase *pDatabase, char *pErrorBuffer, int cbErrorBuffer){ int nLen; nLen = strlen(pDatabase->m_szErrorBuffer); if(cbErrorBuffer <= nLen) { strncpy(pErrorBuffer, pDatabase->m_szErrorBuffer, nLen-4); strcpy(pErrorBuffer+nLen-4,"..."); } else { strcpy(pErrorBuffer, pDatabase->m_szErrorBuffer); }}BOOL DB_SQLPrepare(CDatabase *pDatabase, char *pSQL, void *ppBindVar[], int cbBindVar){ CS_RETCODE rc; CS_INT nResultType; CS_DATAFMT DataFormat; int i; if(pDatabase->m_pCommand) { ct_cancel(NULL, pDatabase->m_pCommand, CS_CANCEL_ALL); ct_cmd_drop(pDatabase->m_pCommand); pDatabase->m_pCommand = NULL; } rc = ct_cmd_alloc(pDatabase->m_pConnection, &pDatabase->m_pCommand); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 分配命令结构指针失败!"); rc = ct_command(pDatabase->m_pCommand, CS_LANG_CMD, (CS_CHAR*)pSQL, CS_NULLTERM, CS_UNUSED); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 初始化命令失败!"); rc = ct_send(pDatabase->m_pCommand); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 发送命令失败!"); while(CS_SUCCEED == (rc = ct_results(pDatabase->m_pCommand, &nResultType))) { switch(nResultType) { case CS_CMD_SUCCEED : case CS_CMD_DONE : break; case CS_CMD_FAIL : ct_cancel(NULL, pDatabase->m_pCommand, CS_CANCEL_ALL); ct_cmd_drop(pDatabase->m_pCommand); pDatabase->m_pCommand = NULL; DB_SetLastError(pDatabase, "Sybase DB_Open Client 发送命令失败!"); return FALSE; case CS_ROW_RESULT : { for (i=0; i<cbBindVar; i++) { ct_describe(pDatabase->m_pCommand, i+1, &DataFormat); DataFormat.count=1; if (DataFormat.datatype==CS_CHAR_TYPE) { DataFormat.format=CS_FMT_PADBLANK; } ct_bind(pDatabase->m_pCommand,i+1,&DataFormat,ppBindVar[i],NULL,NULL); } } return TRUE; default : /* We get something that unexpected */ ct_cancel(NULL, pDatabase->m_pCommand, CS_CANCEL_ALL); ct_cmd_drop(pDatabase->m_pCommand); pDatabase->m_pCommand = NULL; DB_SetLastError(pDatabase, "Sybase DB_Open Client 发送命令失败!"); return FALSE; } } return FALSE;}BOOL DB_SQLFetch(CDatabase *pDatabase){ if(CS_SUCCEED == ct_fetch(pDatabase->m_pCommand, CS_UNUSED, CS_UNUSED, CS_UNUSED, NULL)) { return TRUE; } return FALSE;}BOOL DB_SQLDone(CDatabase *pDatabase){ if(pDatabase->m_pCommand) { ct_cancel(NULL, pDatabase->m_pCommand, CS_CANCEL_ALL); ct_cmd_drop(pDatabase->m_pCommand); pDatabase->m_pCommand = NULL; } return TRUE;}BOOL DB_DynamicSQLPrepare(CDatabase *pDatabase, char *pSQL){ CS_RETCODE rc; if(pDatabase->m_pCommand) { ct_cancel(NULL, pDatabase->m_pCommand, CS_CANCEL_ALL); ct_cmd_drop(pDatabase->m_pCommand); pDatabase->m_pCommand = NULL; } rc = ct_cmd_alloc(pDatabase->m_pConnection, &pDatabase->m_pCommand); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 分配命令结构指针失败!"); rc = ct_dynamic(pDatabase->m_pCommand, CS_PREPARE, "my_dynamic_sql", CS_NULLTERM, (CS_CHAR*)pSQL, CS_NULLTERM); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 初始化命令失败!"); rc = ct_send(pDatabase->m_pCommand); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 发送命令失败!"); rc = DB_HandleResults(pDatabase, pDatabase->m_pCommand); return (rc == CS_SUCCEED);} BOOL DB_DynamicSQLSendParam(CDatabase *pDatabase, void *pParam, int nType, BOOL bByRef){ CS_RETCODE rc; CS_DATAFMT ParamFmt; int nTypeLen=255; rc = ct_dynamic(pDatabase->m_pCommand, CS_EXECUTE, "my_dynamic_sql", CS_NULLTERM, NULL, CS_UNUSED); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 初始化命令失败!"); memset(&ParamFmt,0,sizeof(CS_DATAFMT)); if(nType == CS_CHAR_TYPE) nTypeLen = strlen((char*)pParam); ParamFmt.status = (bByRef?CS_RETURN:CS_INPUTVALUE); ParamFmt.datatype = nType; ParamFmt.maxlength = 255; ParamFmt.locale = NULL; ParamFmt.format = CS_FMT_NULLTERM; rc = ct_param(pDatabase->m_pCommand,&ParamFmt,(CS_VOID*)pParam, nTypeLen,CS_UNUSED); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 发送命令失败!"); return TRUE;}BOOL DB_DynamicSQLExecute(CDatabase *pDatabase){ CS_RETCODE rc; rc = ct_send(pDatabase->m_pCommand); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 发送命令失败!"); rc = DB_HandleResults(pDatabase, pDatabase->m_pCommand); return (rc == CS_SUCCEED);}BOOL DB_DynamicSQLDone(CDatabase *pDatabase){ CS_RETCODE rc; rc = ct_dynamic(pDatabase->m_pCommand, CS_DEALLOC, "my_dynamic_sql", CS_NULLTERM, NULL, CS_UNUSED); if(rc==CS_SUCCEED) { rc = ct_send(pDatabase->m_pCommand); if(rc==CS_SUCCEED) { rc = DB_HandleResults(pDatabase, pDatabase->m_pCommand); } } if( rc!=CS_SUCCEED) { DB_SetLastError(pDatabase, "Sybase DB_Open Client 执行命令失败!"); } ct_cmd_drop(pDatabase->m_pCommand); pDatabase->m_pCommand = NULL; return (rc == CS_SUCCEED);}BOOL DB_BlkCopyInPrepare(CDatabase *pDatabase, char *pTableName, int nColumn){ CS_RETCODE rc; rc = blk_alloc(pDatabase->m_pConnection, EX_BLK_VERSION, &pDatabase->m_pBlkDesc); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 分配批拷贝描述指针失败!"); if(CS_FAIL == (rc = blk_init(pDatabase->m_pBlkDesc, CS_BLK_IN, pTableName, CS_NULLTERM))) { RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 批拷贝初始化失败!"); } pDatabase->m_nBlkRowCount = 0; pDatabase->m_nColumnCount = nColumn; return TRUE;}BOOL DB_BlkCopyInExecute(CDatabase *pDatabase, void *ppVarArray[], long nLenArray[]){ CS_DATAFMT DataFmt; CS_RETCODE rc; int i; long nLen; for(i=1;i<=pDatabase->m_nColumnCount;i++) { blk_describe(pDatabase->m_pBlkDesc, i, &DataFmt); DataFmt.locale = 0; DataFmt.count = 1; if(DataFmt.datatype == CS_NUMERIC_TYPE || DataFmt.datatype == CS_DECIMAL_TYPE) DataFmt.datatype = CS_FLOAT_TYPE; nLen = DB_GetColumnLen(&DataFmt); rc = blk_bind(pDatabase->m_pBlkDesc, i, &DataFmt, ppVarArray[i-1], &nLenArray[i-1]/*&nLen*/, NULL); RETURN_IF(pDatabase, rc, "Sybase DB_Open Client 批拷贝变量绑定失败!"); } if (blk_rowxfer (pDatabase->m_pBlkDesc) == CS_FAIL) { DB_SetLastError(pDatabase, "Sybase DB_Open Client 批拷贝行传输失败!"); return FALSE; } pDatabase->m_nBlkRowCount++; return TRUE;}BOOL DB_BlkCopyInDone(CDatabase *pDatabase){ /* ** ALL the rows sent so clear up */ if (blk_done(pDatabase->m_pBlkDesc, CS_BLK_ALL, &pDatabase->m_nBlkRowCount) == CS_FAIL) { DB_SetLastError(pDatabase, "Sybase DB_Open Client 批拷贝失败!"); return FALSE; } if (blk_drop(pDatabase->m_pBlkDesc) == CS_FAIL) { DB_SetLastError(pDatabase, "Sybase DB_Open Client 删除批拷贝描述字失败!"); return FALSE; } pDatabase->m_pBlkDesc = NULL; return TRUE;}/*--------------------------------------------------------------函数名:ClientMsgHandler描述:在Client-Library 函数发生错误时调用该函数*/CS_INT ClientMsgHandler(CS_CONTEXT* pContext, CS_CONNECTION* pConnection, CS_CLIENTMSG* errmsg){ char szErrorMsg[1024]; char szError[512];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -