Beispiel #1
0
void CInitBase::Init(void)
{
    m_pCfg = event_config_new();
    if (NULL == m_pCfg)
    {
        Q_EXCEPTION(Q_RTN_FAILE, "%s", "event_config_new error.");
    }

#ifdef Q_IOCP
    evthread_use_windows_threads();
    event_config_set_flag(m_pCfg, EVENT_BASE_FLAG_STARTUP_IOCP);
#endif

    m_pBase = event_base_new_with_config(m_pCfg);
    if (NULL == m_pBase)
    {
        Q_EXCEPTION(Q_RTN_FAILE, "%s", "event_base_new error.");
    }

#ifdef Q_IOCP
    Q_Printf("event version %s, using %s", event_get_version(), "IOCP");
#else
    Q_Printf("event version %s, using %s", event_get_version(), event_base_get_method(m_pBase));
#endif
}
Beispiel #2
0
int CSQLite3Link::execDML(const char* szSQL)
{
    if (NULL == szSQL)
    {
        Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_NULLPOINTER);
    }

    checkDB();

    char* pszError = NULL;
    std::string strTmp;

    int iRtn = sqlite3_exec(m_pDB, szSQL, 0, 0, &pszError);
    if (SQLITE_OK == iRtn)
    {
        iRtn = sqlite3_changes(m_pDB);

        return iRtn;
    }

    strTmp = std::string(pszError);
    sqlite3_free(pszError);

    Q_EXCEPTION(iRtn, "%s", strTmp.c_str());
}
Beispiel #3
0
int CSQLite3Statement::execDML(void)
{
    checkDB();
    checkVM();

    int iRtn = sqlite3_step(m_pVM);
    if (SQLITE_DONE == iRtn)
    {
        int iRowsChanged = sqlite3_changes(m_pDB);

        iRtn = sqlite3_reset(m_pVM);
        if (iRtn != SQLITE_OK)
        {
            Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
        }

        return iRowsChanged;
    }
    else
    {
        int iErrorCode = sqlite3_errcode(m_pDB);
        std::string strErrorMsg = sqlite3_errmsg(m_pDB);

        (void)sqlite3_reset(m_pVM);

        Q_EXCEPTION(iErrorCode, "%s", strErrorMsg.c_str());
    }
}
Beispiel #4
0
CDBQuery* CSQLite3Link::execQuery(const char* szSQL)
{
    if (NULL == szSQL)
    {
        Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_NULLPOINTER);
    }

    checkDB();

    CSQLite3Query *pQuery = NULL;

    sqlite3_stmt* pVM = compile(szSQL);
    int iRtn = sqlite3_step(pVM);
    if (SQLITE_DONE == iRtn)
    {
        try
        {
            pQuery = new CSQLite3Query(m_pDB, pVM, true);
        }
        catch(std::bad_alloc &)
        {
            (void)sqlite3_finalize(pVM);
            Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_ALLOCMEMORY);
        }

        return pQuery;
    }
    else if (SQLITE_ROW == iRtn)
    {
        // at least 1 row
        try
        {
            pQuery = new CSQLite3Query(m_pDB, pVM, false);
        }
        catch(std::bad_alloc &)
        {
            (void)sqlite3_finalize(pVM);
            Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_ALLOCMEMORY);
        }

        return pQuery;
    }
    else
    {
        int iErrorCode = sqlite3_errcode(m_pDB);
        std::string strErrorMsg = sqlite3_errmsg(m_pDB);

        (void)sqlite3_finalize(pVM);

        Q_EXCEPTION(iErrorCode, "%s", strErrorMsg.c_str());
    }
}
Beispiel #5
0
void CSQLite3Statement::checkVM(void)
{
    if (NULL == m_pVM)
    {
        Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_NULLPOINTER);
    }
}
Beispiel #6
0
void CSQLite3Link::checkDB()
{
    if (NULL == m_pDB)
    {
        Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_NULLPOINTER);
    }
}
Beispiel #7
0
void CSQLite3Query::nextRow(void)
{
    checkVM();

    int iRtn = sqlite3_step(m_pVM);

    if (SQLITE_DONE == iRtn)
    {
        // no rows
        m_bEof = true;
    }
    else if (SQLITE_ROW == iRtn)
    {
        // more rows, nothing to do
    }
    else
    {
        int iErrorCode = sqlite3_errcode(m_pDB);
        std::string strErrorMsg = sqlite3_errmsg(m_pDB);

        (void)sqlite3_finalize(m_pVM);
        m_pVM = NULL;

        Q_EXCEPTION(iErrorCode, "%s", strErrorMsg.c_str());
    }
}
Beispiel #8
0
CSockPair::CSockPair(void) : m_ReadFD(Q_INVALID_SOCK), m_WriteFD(Q_INVALID_SOCK)
{
    int iRtn = Init();
    if (Q_RTN_OK != iRtn)
    {
        Q_EXCEPTION(iRtn, "%s", "init socket pair error");
    }
}
Beispiel #9
0
CMutex::CMutex(void)
{
    int iRtn = Init();
    if (Q_RTN_OK != iRtn)
    {
        Q_EXCEPTION(iRtn, "%s", Q_Error2Str(iRtn));
    }
}
Beispiel #10
0
void CSQLite3Statement::CheckParam(int iParam)
{
    if(iParam < 0
        || iParam >= m_iCols)
    {
        Q_EXCEPTION(Q_ERROR_DATABASE, "invalid field index requested, %d", iParam);
    }
}
Beispiel #11
0
CDBQuery *CSQLite3Statement::execQuery(void)
{
    checkDB();
    checkVM();

    CSQLite3Query *pQuery = NULL;

    int iRtn = sqlite3_step(m_pVM);
    if (SQLITE_DONE == iRtn)
    {
        try
        {
            pQuery = new CSQLite3Query(m_pDB, m_pVM, true, false);
        }
        catch(std::bad_alloc &)
        {
            Q_EXCEPTION(Q_ERROR_ALLOCMEMORY, "%s", Q_EXCEPTION_ALLOCMEMORY);
        }

        return pQuery;
    }
    else if (SQLITE_ROW == iRtn)
    {
        try
        {
            pQuery = new CSQLite3Query(m_pDB, m_pVM, false, false);
        }
        catch(std::bad_alloc &)
        {
            Q_EXCEPTION(Q_ERROR_ALLOCMEMORY, "%s", Q_EXCEPTION_ALLOCMEMORY);
        }

        return pQuery;
    }
    else
    {
        int iErrorCode = sqlite3_errcode(m_pDB);
        std::string strErrorMsg = sqlite3_errmsg(m_pDB);

        (void)sqlite3_reset(m_pVM);

        Q_EXCEPTION(iErrorCode, "%s", strErrorMsg.c_str());
    }
}
Beispiel #12
0
void CSQLite3Statement::bindFloat(const int iField, const double dValue)
{
    checkVM();
    CheckParam(iField);

    int iRtn = sqlite3_bind_double(m_pVM, iField + 1, dValue);
    if (iRtn != SQLITE_OK)
    {
        Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
    }
}
Beispiel #13
0
void CSQLite3Statement::bindString(const int iField, const char* pszValue)
{
    checkVM();
    CheckParam(iField);

    int iRtn = sqlite3_bind_text(m_pVM, iField + 1, pszValue, -1, SQLITE_TRANSIENT);
    if (iRtn != SQLITE_OK)
    {
        Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
    }
}
Beispiel #14
0
void CSQLite3Statement::bindNull(const int iField)
{
    checkVM();
    CheckParam(iField);

    int iRtn = sqlite3_bind_null(m_pVM, iField + 1);
    if (iRtn != SQLITE_OK)
    {
        Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
    }
}
Beispiel #15
0
int CSQLite3Query::fieldDataType(int iCol)
{
    checkVM();

    if (iCol < 0 || iCol > m_iCols-1)
    {
        Q_EXCEPTION(Q_ERROR_DATABASE, "invalid field index requested, %d", iCol);
    }

    return sqlite3_column_type(m_pVM, iCol);
}
Beispiel #16
0
sqlite3_stmt* CSQLite3Link::compile(const char* szSQL)
{
    if (NULL == szSQL)
    {
        Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_NULLPOINTER);
    }

    checkDB();

    const char* szTail=0;
    sqlite3_stmt* pVM;

    int iRtn = sqlite3_prepare_v2(m_pDB, szSQL, -1, &pVM, &szTail);
    if (iRtn != SQLITE_OK)
    {
        Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
    }

    return pVM;
}
Beispiel #17
0
int CSQLite3Statement::bindParameterIndex(const char* pszParam)
{
    checkVM();

    int iParam = sqlite3_bind_parameter_index(m_pVM, pszParam);
    if (!iParam)
    {
        Q_EXCEPTION(Q_ERROR_DATABASE, "parameter '%s' is not valid for this statement", pszParam);
    }

    return iParam;
}
Beispiel #18
0
void CSQLite3Statement::freeVM(void)
{
    if (NULL != m_pVM)
    {
        int iRtn = sqlite3_finalize(m_pVM);
        m_pVM = NULL;
        if (iRtn != SQLITE_OK)
        {
            Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
        }
    }
}
Beispiel #19
0
void CSQLite3Statement::bindBlob(const int iField, const unsigned char* blobValue, const size_t iLen)
{
    checkVM();
    CheckParam(iField);

    int iRtn = sqlite3_bind_blob(m_pVM, iField + 1,
        (const void*)blobValue, (int)iLen, SQLITE_TRANSIENT);
    if (iRtn != SQLITE_OK)
    {
        Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
    }
}
Beispiel #20
0
void CSQLite3Query::freeRes(void)
{
    if (m_pVM && m_bOwnVM)
    {
        int iRtn = sqlite3_finalize(m_pVM);
        m_pVM = NULL;
        if (iRtn != SQLITE_OK)
        {
            Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
        }
    }
}
Beispiel #21
0
void CSQLite3Link::open(CDBUrl &objDBUrl)
{
    int iRtn =  0;

    iRtn = sqlite3_open(objDBUrl.getDB().c_str(), &m_pDB);
    if (iRtn != SQLITE_OK)
    {
        Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
    }

    setBusyTimeout(m_iBusyTimeoutMs);
}
Beispiel #22
0
void CSQLite3Link::close(void)
{
    if (m_pDB)
    {
        if (SQLITE_OK == sqlite3_close(m_pDB))
        {
            m_pDB = NULL;
        }
        else
        {
            Q_EXCEPTION(Q_ERROR_DATABASE, "%s", "unable to close database.");
        }
    }
}
Beispiel #23
0
const unsigned char* CSQLite3Query::getBlobField(const int &iField, int &iLen)
{
    checkVM();

    iLen = 0;

    if (iField < 0 || iField > m_iCols - 1)
    {
        Q_EXCEPTION(Q_ERROR_DATABASE, "invalid field index requested, %d", iField);
    }

    iLen = sqlite3_column_bytes(m_pVM, iField);

    return (const unsigned char*)sqlite3_column_blob(m_pVM, iField);
}
Beispiel #24
0
void CMutex::Lock(void)
{
    int iRtn = Q_RTN_OK;

#ifdef Q_OS_WIN32
    iRtn = WaitForSingleObject(m_Mutex, INFINITE);
    iRtn = (WAIT_OBJECT_0 == iRtn ? Q_RTN_OK : Q_Error());
#else
    iRtn = pthread_mutex_lock(&m_Mutex);
#endif

    if (Q_RTN_OK != iRtn)
    {
        Q_EXCEPTION(iRtn, "%s", Q_Error2Str(iRtn));
    }
}
Beispiel #25
0
CDBStatement *CSQLite3Link::compileStatement(const char* pszSQL)
{
    sqlite3_stmt* pVM = compile(pszSQL);
    CSQLite3Statement *pStatMem = NULL;

    try
    {
        pStatMem = new CSQLite3Statement(m_pDB, pVM);
    }
    catch(std::bad_alloc &)
    {
        (void)sqlite3_finalize(pVM);
        Q_EXCEPTION(Q_ERROR_ALLOCMEMORY, "%s", Q_EXCEPTION_ALLOCMEMORY);
    }

    return pStatMem;
}
Beispiel #26
0
void CMutex::unLock(void)
{
    int iRtn = Q_RTN_OK;

#ifdef Q_OS_WIN32
    BOOL bRtn = FALSE;

    bRtn = ReleaseMutex(m_Mutex);
    iRtn = (FALSE == bRtn ? Q_Error() : Q_RTN_OK);
#else
    iRtn = pthread_mutex_unlock(&m_Mutex);
#endif

    if (Q_RTN_OK != iRtn)
    {
        Q_EXCEPTION(iRtn, "%s", Q_Error2Str(iRtn));
    }
}
Beispiel #27
0
int CSQLite3Query::fieldIndex(const char* pszField)
{
    checkVM();

    if (pszField)
    {
        for (int iField = 0; iField < m_iCols; iField++)
        {
            const char* pszTemp = sqlite3_column_name(m_pVM, iField);
            if (0 == strcmp(pszField, pszTemp))
            {
                return iField;
            }
        }
    }

    Q_EXCEPTION(Q_ERROR_DATABASE, "invalid field name requested, %s", pszField);
}