程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> AMPS:數據庫訪問模塊源碼解讀

AMPS:數據庫訪問模塊源碼解讀

編輯:C++入門知識

  AMPS做為一個中間件,也提供了訪問數據庫的接口,目前支持MySQL和Oracle(對我所喜愛PostgreSQL卻默認不支持,看來我的愛好屬於非主流哈~),下面要看的是DBEngine庫,它只做一件事情,就是封裝了不同數據庫操作的差異,提供一個數據庫統一訪問接口。下節再看針對不同數據庫的具體操作。   AMPS_DBEngine.h       #ifndef __HEADER_AMPS_DB_ENGINE_H    #define __HEADER_AMPS_DB_ENGINE_H       #ifdef __cplusplus    extern "C" {   #endif       #include "AMPS_Defines.h"    #include "AMPS_LinkList.h"    #include "AMPS_MemMgt.h"    #include "AMPS_EventSystem.h"    #include "AMPS_Core.h"    #include "AMPS_API.h"       typedef struct _BDEngineContext                 t_BDEngineContext;      struct _BDEngineContext   {       void*                   poDBHandle;       void*                   pvAMPSContext;          //pointer to framework context        e_AMPSDBType            oAMPSDBType;          char                    pchHostName[AMPS_MAX_IP_STRING_LENGTH];       char                    pchUserName[AMPS_SIZE_OF_DB_USER_NAME];       char                    pchPassword[AMPS_SIZE_OF_DB_PWD];       char                    pchDataBaseName[AMPS_SIZE_OF_DB_NAME];       unsigned int            unDBPort;       char                    pchUnixSocket[AMPS_SIZE_OF_DB_UNIX_SOCKET];       unsigned long           ulClientFlag;   };      void* DBEngine_Init(void* r_pvAMPSContext, e_AMPSDBType r_oAMPSDBType, char* r_pchHostName, char* r_pchUserName, char* r_pchPassword, char* r_pchDataBaseName);   void DBEngine_Cleanup(void* r_pvAMPSContext, void* r_pvBDEngineContext);   int DBEngine_Query(void* r_pvAMPSContext, void* r_pvBDEngineContext, const char* r_pcchQueryStatement, unsigned long r_ulLengthOfQuery, t_AMPSDBEngineResult* r_poAMPSDBEngineResult);   void DBEngine_FreeResults(void* r_pvAMPSContext, void* r_pvBDEngineContext, t_AMPSDBEngineResult* r_poAMPSDBEngineResult);      #ifdef __cplusplus    }   #endif       #endif //__HEADER_AMPS_DB_ENGINE_H     #ifndef __HEADER_AMPS_DB_ENGINE_H #define __HEADER_AMPS_DB_ENGINE_H   #ifdef __cplusplus extern "C" { #endif   #include "AMPS_Defines.h" #include "AMPS_LinkList.h" #include "AMPS_MemMgt.h" #include "AMPS_EventSystem.h" #include "AMPS_Core.h" #include "AMPS_API.h"   typedef struct _BDEngineContext t_BDEngineContext;   struct _BDEngineContext { void* poDBHandle; void* pvAMPSContext; //pointer to framework context e_AMPSDBType oAMPSDBType;   char pchHostName[AMPS_MAX_IP_STRING_LENGTH]; char pchUserName[AMPS_SIZE_OF_DB_USER_NAME]; char pchPassword[AMPS_SIZE_OF_DB_PWD]; char pchDataBaseName[AMPS_SIZE_OF_DB_NAME]; unsigned int unDBPort; char pchUnixSocket[AMPS_SIZE_OF_DB_UNIX_SOCKET]; unsigned long ulClientFlag; };   void* DBEngine_Init(void* r_pvAMPSContext, e_AMPSDBType r_oAMPSDBType, char* r_pchHostName, char* r_pchUserName, char* r_pchPassword, char* r_pchDataBaseName); void DBEngine_Cleanup(void* r_pvAMPSContext, void* r_pvBDEngineContext); int DBEngine_Query(void* r_pvAMPSContext, void* r_pvBDEngineContext, const char* r_pcchQueryStatement, unsigned long r_ulLengthOfQuery, t_AMPSDBEngineResult* r_poAMPSDBEngineResult); void DBEngine_FreeResults(void* r_pvAMPSContext, void* r_pvBDEngineContext, t_AMPSDBEngineResult* r_poAMPSDBEngineResult);   #ifdef __cplusplus } #endif   #endif //__HEADER_AMPS_DB_ENGINE_H   AMPS_DBEngine.c       [cpp]   #include "AMPS_DBEngine.h"    #include "AMPS_MySQL.h"    #include "AMPS_Oracle.h"       /*****************************************************************  函數名稱: DBEngine_Init  功能描述: 數據庫訪問初始化函數  入參::        void* r_pvAMPSContext APMS應用上下文        e_AMPSDBType r_oAMPSDBType 數據庫類型(MySQL或Oracle)        char* r_pchHostName 數據庫的監聽IP        char* r_pchUserName 數據庫訪問用戶名        char* r_pchPassword 數據庫訪問密碼        char* r_pchDataBaseName 數據庫名稱          出參:          返回值:        void* 數據庫訪問句柄    *****************************************************************/   void* DBEngine_Init(void* r_pvAMPSContext, e_AMPSDBType r_oAMPSDBType, char* r_pchHostName, char* r_pchUserName, char* r_pchPassword, char* r_pchDataBaseName)   {       t_BDEngineContext* poBDEngineContext = NULL;          //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");               poBDEngineContext = (t_BDEngineContext*)AMPS_InternalMalloc(sizeof(t_BDEngineContext));       if(NULL == poBDEngineContext)       {           TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poBDEngineContext.\n");           return NULL;       }          poBDEngineContext->pvAMPSContext = r_pvAMPSContext;       poBDEngineContext->oAMPSDBType = r_oAMPSDBType;          memcpy(poBDEngineContext->pchHostName, r_pchHostName, strlen(r_pchHostName));       //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Host Name is %s.\n", poBDEngineContext->pchHostName);        memcpy(poBDEngineContext->pchUserName, r_pchUserName, strlen(r_pchUserName));       //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "User Name is %s.\n", poBDEngineContext->pchUserName);        memcpy(poBDEngineContext->pchPassword, r_pchPassword, strlen(r_pchPassword));       //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Password is %s.\n", poBDEngineContext->pchPassword);        memcpy(poBDEngineContext->pchDataBaseName, r_pchDataBaseName, strlen(r_pchDataBaseName));       //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "DataBase Name is %s.\n", poBDEngineContext->pchDataBaseName);           /*根據不同的數據庫類型,進行訪問前的初始化*/       if(AMPS_DB_TYPE_MYSQL == poBDEngineContext->oAMPSDBType)       {           if(AMPS_SUCCESS != MySQL_Init(r_pvAMPSContext, poBDEngineContext))           {               TRACE(DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "MySQL_Init failed.\n");               return NULL;           }       }       else       if(AMPS_DB_TYPE_ORACLE == poBDEngineContext->oAMPSDBType)       {           if(AMPS_SUCCESS != Oracle_Init(r_pvAMPSContext, poBDEngineContext))           {               TRACE(DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Oracle_Init failed.\n");               return NULL;           }       }       else       {           TRACE(DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Invalid DB Type %d.\n", r_oAMPSDBType);           return NULL;       }          //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");        return poBDEngineContext;   }      /*****************************************************************  函數名稱: DBEngine_Cleanup  功能描述: 數據庫訪問完成後的資源銷毀函數  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvBDEngineContext 數據庫訪問句柄            出參:          返回值:        void    *****************************************************************/   void DBEngine_Cleanup(void* r_pvAMPSContext, void* r_pvBDEngineContext)   {       t_BDEngineContext* poBDEngineContext = r_pvBDEngineContext;          //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");           /*根據不同數據庫類型進行數據庫關閉等清理操作*/       if(AMPS_DB_TYPE_MYSQL == poBDEngineContext->oAMPSDBType)       {           MySQL_Cleanup(r_pvAMPSContext, poBDEngineContext);       }       else       if(AMPS_DB_TYPE_ORACLE == poBDEngineContext->oAMPSDBType)       {           Oracle_Cleanup(r_pvAMPSContext, poBDEngineContext);       }              AMPS_InternalFree(r_pvBDEngineContext);       //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");    }      /*****************************************************************  函數名稱: DBEngine_Query  功能描述: 數據庫操作語句執行函數  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvBDEngineContext 數據庫訪問句柄        char* r_pcchQueryStatement 需要執行的SQL語句        unsigned long r_ulLengthOfQuery SQL語句長度        t_AMPSDBEngineResult* r_poBDEngineResult 查詢完成後的結果集          出參:        t_AMPSDBEngineResult* r_poBDEngineResult 查詢完成後的結果集          返回值:        int    *****************************************************************/   int DBEngine_Query(void* r_pvAMPSContext, void* r_pvBDEngineContext, const char* r_pcchQueryStatement, unsigned long r_ulLengthOfQuery, t_AMPSDBEngineResult* r_poBDEngineResult)   {       t_BDEngineContext* poBDEngineContext = r_pvBDEngineContext;          //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");           /*按不同數據庫類型進行SQL語句的執行*/       if(AMPS_DB_TYPE_MYSQL == poBDEngineContext->oAMPSDBType)       {           MySQL_Query(r_pvAMPSContext, r_pvBDEngineContext, r_pcchQueryStatement, r_ulLengthOfQuery, r_poBDEngineResult);       }       else       if(AMPS_DB_TYPE_ORACLE == poBDEngineContext->oAMPSDBType)       {           Oracle_Query(r_pvAMPSContext, r_pvBDEngineContext, r_pcchQueryStatement, r_ulLengthOfQuery, r_poBDEngineResult);       }          //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");        return AMPS_SUCCESS;   }      /*****************************************************************  函數名稱: DBEngine_FreeResults  功能描述: 數據庫操作語句執行完成後的資源釋放函數  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvBDEngineContext 數據庫訪問句柄        t_AMPSDBEngineResult* r_poBDEngineResult 查詢完成後的結果集          出參:          返回值:        int    *****************************************************************/   void DBEngine_FreeResults(void* r_pvAMPSContext, void* r_pvBDEngineContext, t_AMPSDBEngineResult* r_poBDEngineResult)   {       t_BDEngineContext* poBDEngineContext = r_pvBDEngineContext;          //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");           /*按不同數據庫類型進行SQL語句的執行後的資源釋放*/          if(AMPS_DB_TYPE_MYSQL == poBDEngineContext->oAMPSDBType)       {           MySQL_FreeResults(r_pvAMPSContext, r_pvBDEngineContext, r_poBDEngineResult);       }       else       if(AMPS_DB_TYPE_ORACLE == poBDEngineContext->oAMPSDBType)       {           Oracle_FreeResults(r_pvAMPSContext, r_pvBDEngineContext, r_poBDEngineResult);       }          //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");    }     #include "AMPS_DBEngine.h" #include "AMPS_MySQL.h" #include "AMPS_Oracle.h"   /***************************************************************** 函數名稱: DBEngine_Init 功能描述: 數據庫訪問初始化函數 入參::       void* r_pvAMPSContext APMS應用上下文       e_AMPSDBType r_oAMPSDBType 數據庫類型(MySQL或Oracle)       char* r_pchHostName 數據庫的監聽IP       char* r_pchUserName 數據庫訪問用戶名       char* r_pchPassword 數據庫訪問密碼       char* r_pchDataBaseName 數據庫名稱        出參:        返回值:       void* 數據庫訪問句柄   *****************************************************************/ void* DBEngine_Init(void* r_pvAMPSContext, e_AMPSDBType r_oAMPSDBType, char* r_pchHostName, char* r_pchUserName, char* r_pchPassword, char* r_pchDataBaseName) { t_BDEngineContext* poBDEngineContext = NULL;   //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");   poBDEngineContext = (t_BDEngineContext*)AMPS_InternalMalloc(sizeof(t_BDEngineContext)); if(NULL == poBDEngineContext) { TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poBDEngineContext.\n");     return NULL;   }   poBDEngineContext->pvAMPSContext = r_pvAMPSContext; poBDEngineContext->oAMPSDBType = r_oAMPSDBType;   memcpy(poBDEngineContext->pchHostName, r_pchHostName, strlen(r_pchHostName)); //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Host Name is %s.\n", poBDEngineContext->pchHostName); memcpy(poBDEngineContext->pchUserName, r_pchUserName, strlen(r_pchUserName)); //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "User Name is %s.\n", poBDEngineContext->pchUserName); memcpy(poBDEngineContext->pchPassword, r_pchPassword, strlen(r_pchPassword)); //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Password is %s.\n", poBDEngineContext->pchPassword); memcpy(poBDEngineContext->pchDataBaseName, r_pchDataBaseName, strlen(r_pchDataBaseName)); //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "DataBase Name is %s.\n", poBDEngineContext->pchDataBaseName);       /*根據不同的數據庫類型,進行訪問前的初始化*/ if(AMPS_DB_TYPE_MYSQL == poBDEngineContext->oAMPSDBType) { if(AMPS_SUCCESS != MySQL_Init(r_pvAMPSContext, poBDEngineContext)) { TRACE(DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "MySQL_Init failed.\n"); return NULL; } } else if(AMPS_DB_TYPE_ORACLE == poBDEngineContext->oAMPSDBType) { if(AMPS_SUCCESS != Oracle_Init(r_pvAMPSContext, poBDEngineContext)) { TRACE(DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Oracle_Init failed.\n"); return NULL; } } else { TRACE(DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Invalid DB Type %d.\n", r_oAMPSDBType); return NULL; }   //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return poBDEngineContext; }   /***************************************************************** 函數名稱: DBEngine_Cleanup 功能描述: 數據庫訪問完成後的資源銷毀函數 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvBDEngineContext 數據庫訪問句柄          出參:        返回值:       void   *****************************************************************/ void DBEngine_Cleanup(void* r_pvAMPSContext, void* r_pvBDEngineContext) { t_BDEngineContext* poBDEngineContext = r_pvBDEngineContext;   //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*根據不同數據庫類型進行數據庫關閉等清理操作*/ if(AMPS_DB_TYPE_MYSQL == poBDEngineContext->oAMPSDBType) { MySQL_Cleanup(r_pvAMPSContext, poBDEngineContext); } else if(AMPS_DB_TYPE_ORACLE == poBDEngineContext->oAMPSDBType) { Oracle_Cleanup(r_pvAMPSContext, poBDEngineContext); }   AMPS_InternalFree(r_pvBDEngineContext); //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); }   /***************************************************************** 函數名稱: DBEngine_Query 功能描述: 數據庫操作語句執行函數 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvBDEngineContext 數據庫訪問句柄       char* r_pcchQueryStatement 需要執行的SQL語句       unsigned long r_ulLengthOfQuery SQL語句長度       t_AMPSDBEngineResult* r_poBDEngineResult 查詢完成後的結果集        出參:       t_AMPSDBEngineResult* r_poBDEngineResult 查詢完成後的結果集        返回值:       int   *****************************************************************/ int DBEngine_Query(void* r_pvAMPSContext, void* r_pvBDEngineContext, const char* r_pcchQueryStatement, unsigned long r_ulLengthOfQuery, t_AMPSDBEngineResult* r_poBDEngineResult) { t_BDEngineContext* poBDEngineContext = r_pvBDEngineContext;   //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*按不同數據庫類型進行SQL語句的執行*/ if(AMPS_DB_TYPE_MYSQL == poBDEngineContext->oAMPSDBType) { MySQL_Query(r_pvAMPSContext, r_pvBDEngineContext, r_pcchQueryStatement, r_ulLengthOfQuery, r_poBDEngineResult); } else if(AMPS_DB_TYPE_ORACLE == poBDEngineContext->oAMPSDBType) { Oracle_Query(r_pvAMPSContext, r_pvBDEngineContext, r_pcchQueryStatement, r_ulLengthOfQuery, r_poBDEngineResult); }   //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return AMPS_SUCCESS; }   /***************************************************************** 函數名稱: DBEngine_FreeResults 功能描述: 數據庫操作語句執行完成後的資源釋放函數 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvBDEngineContext 數據庫訪問句柄       t_AMPSDBEngineResult* r_poBDEngineResult 查詢完成後的結果集        出參:        返回值:       int   *****************************************************************/ void DBEngine_FreeResults(void* r_pvAMPSContext, void* r_pvBDEngineContext, t_AMPSDBEngineResult* r_poBDEngineResult) { t_BDEngineContext* poBDEngineContext = r_pvBDEngineContext;   //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*按不同數據庫類型進行SQL語句的執行後的資源釋放*/   if(AMPS_DB_TYPE_MYSQL == poBDEngineContext->oAMPSDBType) { MySQL_FreeResults(r_pvAMPSContext, r_pvBDEngineContext, r_poBDEngineResult); } else if(AMPS_DB_TYPE_ORACLE == poBDEngineContext->oAMPSDBType) { Oracle_FreeResults(r_pvAMPSContext, r_pvBDEngineContext, r_poBDEngineResult); }   //TRACE( DBE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); }    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved