static cell_t SQL_PrepareQuery(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, *error; size_t maxlength = (size_t)params[4]; pContext->LocalToString(params[2], &query); pContext->LocalToString(params[3], &error); IPreparedQuery *qr = db->PrepareQuery(query, error, maxlength); if (!qr) { return BAD_HANDLE; } Handle_t hndl = g_HandleSys.CreateHandle(hStmtType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL); if (hndl == BAD_HANDLE) { qr->Destroy(); return BAD_HANDLE; } return hndl; }
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 AMX_NATIVE_CALL SQL_PrepareQuery(AMX *amx, cell *params) { IDatabase *pDb = (IDatabase *)GetHandle(params[1], Handle_Database); if (!pDb) { MF_LogError(amx, AMX_ERR_NATIVE, "Invalid database handle: %d", params[1]); return 0; } int len; char *fmt = MF_FormatAmxString(amx, params, 2, &len); IQuery *pQuery = pDb->PrepareQuery(fmt); if (!pQuery) return 0; AmxQueryInfo *qinfo = new AmxQueryInfo; qinfo->pQuery = pQuery; memset(&qinfo->info, 0, sizeof(QueryInfo)); return MakeHandle(qinfo, Handle_Query, FreeQuery); }