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 }
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()); }
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()); } }
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()); } }
void CSQLite3Statement::checkVM(void) { if (NULL == m_pVM) { Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_NULLPOINTER); } }
void CSQLite3Link::checkDB() { if (NULL == m_pDB) { Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_NULLPOINTER); } }
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()); } }
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"); } }
CMutex::CMutex(void) { int iRtn = Init(); if (Q_RTN_OK != iRtn) { Q_EXCEPTION(iRtn, "%s", Q_Error2Str(iRtn)); } }
void CSQLite3Statement::CheckParam(int iParam) { if(iParam < 0 || iParam >= m_iCols) { Q_EXCEPTION(Q_ERROR_DATABASE, "invalid field index requested, %d", iParam); } }
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()); } }
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)); } }
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)); } }
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)); } }
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); }
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; }
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; }
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)); } } }
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)); } }
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)); } } }
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); }
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."); } } }
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); }
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)); } }
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; }
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)); } }
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); }