static cell_t SQL_FieldNumToName(IPluginContext *pContext, const cell_t *params) { IQuery *query; HandleError err; if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None) { return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err); } IResultSet *rs = query->GetResultSet(); if (!rs) { return pContext->ThrowNativeError("No current result set"); } unsigned int field = params[2]; const char *fldname; if ((fldname = rs->FieldNumToName(field)) == NULL) { return pContext->ThrowNativeError("Invalid field index %d", field); } pContext->StringToLocalUTF8(params[3], params[4], fldname, NULL); return 1; }
static cell_t SQL_FetchSize(IPluginContext *pContext, const cell_t *params) { IQuery *query; HandleError err; if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None) { return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err); } IResultSet *rs = query->GetResultSet(); if (!rs) { return pContext->ThrowNativeError("No current result set"); } IResultRow *row = rs->CurrentRow(); if (!row) { return pContext->ThrowNativeError("Current result set has no fetched rows"); } if ((unsigned)params[2] >= rs->GetFieldCount()) { return pContext->ThrowNativeError("Invalid field index %d", params[2]); } return row->GetDataSize(params[2]); }
bool MyDatabase::DoSimpleQueryEx(const char *query, size_t len) { IQuery *pQuery = DoQueryEx(query, len); if (!pQuery) { return false; } pQuery->Destroy(); return true; }
bool MyDatabase::DoSimpleQuery(const char *query) { IQuery *pQuery = DoQuery(query); if (!pQuery) { return false; } pQuery->Destroy(); return true; }
static cell_t SQL_HasResultSet(IPluginContext *pContext, const cell_t *params) { IQuery *query; HandleError err; if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None) { return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err); } return query->GetResultSet() != NULL ? true : false; }
static cell_t SQL_FetchMoreResults(IPluginContext *pContext, const cell_t *params) { IQuery *query; HandleError err; if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None) { return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err); } return query->FetchMoreResults() ? 1 : 0; }
void MysqlThread::RunThread(IThreadHandle *pHandle) { DatabaseInfo info; info.database = m_db.chars(); info.pass = ""; info.user = ""; info.host = ""; info.port = 0; float save_time = m_qrInfo.queue_time; memset(&m_qrInfo, 0, sizeof(m_qrInfo)); m_qrInfo.queue_time = save_time; IDatabase *pDatabase = g_Sqlite.Connect(&info, &m_qrInfo.amxinfo.info.errorcode, m_qrInfo.amxinfo.error, 254); IQuery *pQuery = NULL; if (!pDatabase) { m_qrInfo.connect_success = false; m_qrInfo.query_success = false; } else { m_qrInfo.connect_success = true; pQuery = pDatabase->PrepareQuery(m_query.chars()); if (!pQuery->Execute2(&m_qrInfo.amxinfo.info, m_qrInfo.amxinfo.error, 254)) { m_qrInfo.query_success = false; } else { m_qrInfo.query_success = true; } } if (m_qrInfo.query_success && m_qrInfo.amxinfo.info.rs) { m_atomicResult.CopyFrom(m_qrInfo.amxinfo.info.rs); m_qrInfo.amxinfo.info.rs = &m_atomicResult; } if (pQuery) { m_qrInfo.amxinfo.pQuery = pQuery; } else { m_qrInfo.amxinfo.opt_ptr = new char[m_query.length() + 1]; strcpy(m_qrInfo.amxinfo.opt_ptr, m_query.chars()); } if (pDatabase) { pDatabase->FreeHandle(); pDatabase = NULL; } }
static cell_t SQL_FetchRow(IPluginContext *pContext, const cell_t *params) { IQuery *query; HandleError err; if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None) { return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err); } IResultSet *rs = query->GetResultSet(); if (!rs) { return pContext->ThrowNativeError("No current result set"); } return (rs->FetchRow() != NULL) ? true : false; }
static cell_t SQL_GetFieldCount(IPluginContext *pContext, const cell_t *params) { IQuery *query; HandleError err; if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None) { return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err); } IResultSet *rs = query->GetResultSet(); if (!rs) { return 0; } return rs->GetFieldCount(); }
//native Result:dbi_query2(Sql:_sql, &rows, _query[], {Float,_}:...); static cell AMX_NATIVE_CALL dbi_query2(AMX *amx, cell *params) { olddb_s *old = (olddb_s *)GetHandle(params[1], Handle_OldDb); if (!old) { MF_LogError(amx, AMX_ERR_NATIVE, "Invalid DBI handle %d", params[1]); return -1; } int len; char *queryString = MF_FormatAmxString(amx, params, 3, &len); IQuery *pQuery = old->pDatabase->PrepareQuery(queryString); QueryInfo info; old->error[0] = '\0'; old->errcode = 0; if (!pQuery->Execute2(&info, old->error, 254)) { old->errcode = info.errorcode; return -1; } else { cell *addr = MF_GetAmxAddr(amx, params[2]); *addr = static_cast<cell>(info.affected_rows); if (info.rs && info.rs->RowCount()) { oldresult_s *oldrs = new oldresult_s; int hndl; oldrs->info = info; oldrs->pQuery = pQuery; oldrs->firstCall = true; hndl = MakeHandle(oldrs, Handle_OldResult, FreeOldResult); return hndl; } else { pQuery->FreeHandle(); return 0; } } /** never reach here */ return 0; }
static cell_t SQL_Query(IPluginContext *pContext, const cell_t *params) { IDatabase *db = NULL; HandleError err; if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db)) != HandleError_None) { return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err); } char *query; pContext->LocalToString(params[2], &query); IQuery *qr; if (params[0] >= 3 && params[3] != -1) { qr = db->DoQueryEx(query, params[3]); } else { qr = db->DoQuery(query); } if (!qr) { return BAD_HANDLE; } CombinedQuery *c = new CombinedQuery; c->query = qr; c->db = db; Handle_t hndl = g_HandleSys.CreateHandle(hCombinedQueryType, c, pContext->GetIdentity(), g_pCoreIdent, NULL); if (hndl == BAD_HANDLE) { qr->Destroy(); delete c; return BAD_HANDLE; } return hndl; }
static cell_t SQL_FetchString(IPluginContext *pContext, const cell_t *params) { IQuery *query; HandleError err; if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None) { return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err); } IResultSet *rs = query->GetResultSet(); if (!rs) { return pContext->ThrowNativeError("No current result set"); } IResultRow *row = rs->CurrentRow(); if (!row) { return pContext->ThrowNativeError("Current result set has no fetched rows"); } const char *str; size_t length; DBResult res = row->GetString(params[2], &str, &length); if (res == DBVal_Error) { return pContext->ThrowNativeError("Error fetching data from field %d", params[2]); } else if (res == DBVal_TypeMismatch) { return pContext->ThrowNativeError("Could not fetch data in field %d as a string", params[2]); } pContext->StringToLocalUTF8(params[3], params[4], str, &length); cell_t *addr; pContext->LocalToPhysAddr(params[5], &addr); *addr = (cell_t)res; return (cell_t)length; }
static cell_t SQL_FetchInt(IPluginContext *pContext, const cell_t *params) { IQuery *query; HandleError err; if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None) { return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err); } IResultSet *rs = query->GetResultSet(); if (!rs) { return pContext->ThrowNativeError("No current result set"); } IResultRow *row = rs->CurrentRow(); if (!row) { return pContext->ThrowNativeError("Current result set has no fetched rows"); } int iv; DBResult res = row->GetInt(params[2], &iv); if (res == DBVal_Error) { return pContext->ThrowNativeError("Error fetching data from field %d", params[2]); } else if (res == DBVal_TypeMismatch) { return pContext->ThrowNativeError("Could not fetch data in field %d as an integer", params[2]); } cell_t *addr; pContext->LocalToPhysAddr(params[3], &addr); *addr = (cell_t)res; return iv; }
static cell_t SQL_FieldNameToNum(IPluginContext *pContext, const cell_t *params) { IQuery *query; HandleError err; if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None) { return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err); } IResultSet *rs = query->GetResultSet(); if (!rs) { return pContext->ThrowNativeError("No current result set"); } char *field; pContext->LocalToString(params[2], &field); cell_t *num; pContext->LocalToPhysAddr(params[3], &num); return rs->FieldNameToNum(field, (unsigned int *)num) ? 1 : 0; }