当前位置:编程学习 > C/C++ >>

AMPS:数据库访问模块源码解读

  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.
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,