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

📄 mdbtool.cpp

📁 跨平台C++基础库
💻 CPP
字号:

#include "MCRT/mdbtool.h"

#include "MCRT/mdbtoolimplitf.h"

MRecordSet::MRecordSet()
:m_pMRecordSetImpl(NULL)
{
}

MRecordSet::~MRecordSet()
{
    if ( this->m_pMRecordSetImpl != NULL )
    {
        this->m_pMRecordSetImpl->Destroy();
        this->m_pMRecordSetImpl =   NULL;
    }
}

int MRecordSet::GetRows()
{
    // check self's status
    if ( ! this->IsInitialized() )
    {
        return 0;
    }

    return this->m_pMRecordSetImpl->GetRows();
}

int MRecordSet::GetCols()
{
    // check self's status
    if ( ! this->IsInitialized() )
    {
        return 0;
    }

    return this->m_pMRecordSetImpl->GetCols();
}

const char* MRecordSet::GetFieldName( int col )
{
    // check self's status
    if ( ! this->IsInitialized() )
    {
        return NULL;
    }

    return this->m_pMRecordSetImpl->GetFieldName( col );
}

const char* MRecordSet::GetData( int row, int col, int* pLenData )
{
    // check self's status
    if ( ! this->IsInitialized() )
    {
        return NULL;
    }

    return this->m_pMRecordSetImpl->GetData( row, col, pLenData );
}

MDBTool::MDBTool()
:m_pMDBToolImpl(NULL)
{
}

MDBTool::~MDBTool()
{
    this->Fini();
}

int MDBTool::Init( const char* pDLLDBToolImpl )
{
    int retVal  =   E_SUCCESS;

    // check parameter
    if ( pDLLDBToolImpl == NULL )
    {
        return E_PARAM_POINTER_NULL;
    }

    // check self's status
    if ( this->IsInitialized() )
    {
        this->Fini();
    }

    // load DLL
    std::string strDLLNameDBTool    =   MDll::GetFullDLLName( pDLLDBToolImpl );
    retVal  =   this->m_dll.Load( strDLLNameDBTool.c_str() );
    if ( retVal != E_SUCCESS )
    {
        return retVal;
    }

    // get function addr
    this->CreateMRecordSetImpl                  =
        (pfnCreateMRecordSetImpl)( this->m_dll.GetSymbol( "CreateMRecordSetImpl" ) );
    pfnCreateMDBToolImpl    CreateMDBToolImpl   =
        (pfnCreateMDBToolImpl)( this->m_dll.GetSymbol( "CreateMDBToolImpl" ) );
    if ( (this->CreateMRecordSetImpl == NULL) || (CreateMDBToolImpl == NULL) )
    {
        this->Fini();
        return E_PARAM_INVALID_VALUE;
    }

    // create MDBToolImpl instance
    this->m_pMDBToolImpl    =   CreateMDBToolImpl();
    if ( this->m_pMDBToolImpl == NULL )
    {
        this->Fini();
        return E_MEMORY_INSUFFICIENT;
    }

    return E_SUCCESS;
}

int MDBTool::Fini()
{
    this->CreateMRecordSetImpl  =   NULL;

    if ( this->m_pMDBToolImpl != NULL )
    {
        this->m_pMDBToolImpl->Close();
        this->m_pMDBToolImpl->Destroy();
        this->m_pMDBToolImpl    =   NULL;
    }

    this->m_dll.Unload();

    return E_SUCCESS;
}

int MDBTool::Connect(
    const char*     pDBSvr,
    const char*     pUsername,
    const char*     pPassword,
    const char*     pDBName,
    unsigned int    port
    )
{
    // check self's status
    if ( ! this->IsInitialized() )
    {
        return E_OBJECT_INVALID_STATUS;
    }

    return this->m_pMDBToolImpl->Connect( pDBSvr, pUsername, pPassword, pDBName, port );
}

int MDBTool::Close()
{
    // check self's status
    if ( ! this->IsInitialized() )
    {
        return E_OBJECT_INVALID_STATUS;
    }

    return this->m_pMDBToolImpl->Close();
}

int MDBTool::Query(
          const char*   pStmt,
          int           lenStmt,
          MRecordSet*   pMRecordSet,
          char*         pBufErrMsg
          )
{
    // check self's status
    if ( ! this->IsInitialized() )
    {
        return E_OBJECT_INVALID_STATUS;
    }

    // create MRecordSetImpl
    if ( pMRecordSet->m_pMRecordSetImpl != NULL )
    {
        pMRecordSet->m_pMRecordSetImpl->Destroy();
        pMRecordSet->m_pMRecordSetImpl  =   NULL;
    }
    pMRecordSet->m_pMRecordSetImpl  =   this->CreateMRecordSetImpl();
    if ( pMRecordSet->m_pMRecordSetImpl == NULL )
    {
        return E_MEMORY_INSUFFICIENT;
    }

    int retVal  =   E_SUCCESS;

    ACE_Guard<ACE_Thread_Mutex> guard( this->m_mutexDB );
    retVal  =   this->m_pMDBToolImpl->Query( pStmt, lenStmt, pMRecordSet->m_pMRecordSetImpl );
    if ( (retVal != E_SUCCESS) && (pBufErrMsg != NULL) )
    {
        this->m_pMDBToolImpl->GetLastError( pBufErrMsg );
    }

    return retVal;
}

int MDBTool::Exec(
         const char*        pStmt,
         int                lenStmt,
         int*               pRowsAffected,
         char*              pBufErrMsg
         ) 
{
    // check self's status
    if ( ! this->IsInitialized() )
    {
        return E_OBJECT_INVALID_STATUS;
    }

    int retVal  =   E_SUCCESS;

    ACE_Guard<ACE_Thread_Mutex> guard( this->m_mutexDB );
    retVal  =   this->m_pMDBToolImpl->Exec( pStmt, lenStmt, pRowsAffected );
    if ( (retVal != E_SUCCESS) && (pBufErrMsg != NULL) )
    {
        this->m_pMDBToolImpl->GetLastError( pBufErrMsg );
    }

    return retVal;
}

⌨️ 快捷键说明

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