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
#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"); }