Backported mysql changes

This commit is contained in:
David Anderson 2004-09-02 09:07:07 +00:00
parent af6349454d
commit 961b92db2c
2 changed files with 71 additions and 49 deletions

View File

@ -31,26 +31,21 @@
* version. * version.
*/ */
#include <stdarg.h>
#include "pgsql_amx.h" #include "pgsql_amx.h"
#include "amxxmodule.h" #include "amxxmodule.h"
CVector<SQLResult*> Results; CVector<SQLResult*> Results;
CVector<SQL*> DBList; CVector<SQL*> DBList;
int sql_exists(const char* host,const char* user,const char* pass,const char* dbase) { void SqlError(AMX *amx, const char *fmt, ...)
unsigned int i = 0; {
int id = 0; va_list p;
for (i=0; i<DBList.size(); i++) { va_start(p,fmt);
id++; static char buf[512];
if ((DBList[i]->Host.compare(host) == 0) && vsprintf(buf, fmt, p);
(DBList[i]->Username.compare(user) == 0) && va_end(p);
(DBList[i]->Password.compare(pass) == 0) && MF_Log("SQL Error: %s (plugin \"%s\" line %d)", buf, MF_GetScriptName(MF_FindScriptByAmx(amx)), amx->curline); \
(DBList[i]->Database.compare(dbase) == 0) &&
(!DBList[i]->isFree)) {
return id;
}
}
return -1;
} }
// /////////////////////////////// // ///////////////////////////////
@ -69,14 +64,11 @@ static cell AMX_NATIVE_CALL sql_connect(AMX *amx, cell *params) // 6 param
i = 0; i = 0;
if (!strlen(host) || !strlen(user) || !strlen(dbname)) { if (!strlen(host) || !strlen(user) || !strlen(dbname)) {
MF_RaiseAmxError(amx, AMX_ERR_NATIVE); SqlError(amx, "Could not connect");
return -1; return -1;
} }
int id = sql_exists(host,user,pass,dbname); int id = -1;
if (id > 0)
return id;
SQL *c=NULL; SQL *c=NULL;
@ -113,7 +105,7 @@ static cell AMX_NATIVE_CALL sql_error(AMX *amx, cell *params) // 3 params
unsigned int id = params[1]-1; unsigned int id = params[1]-1;
if (id >= DBList.size() || DBList[id]->isFree) if (id >= DBList.size() || DBList[id]->isFree)
{ {
MF_RaiseAmxError(amx, AMX_ERR_NATIVE); SqlError(amx, "Invalid DB handle: %d", id);
return 0; return 0;
} }
@ -143,8 +135,7 @@ static cell AMX_NATIVE_CALL sql_query(AMX *amx, cell *params) // 2 params
unsigned int id = params[1]-1; unsigned int id = params[1]-1;
if (id >= DBList.size() || DBList[id]->isFree) { if (id >= DBList.size() || DBList[id]->isFree) {
MF_Log("Invalid Database Handle %d", id); SqlError(amx, "Invalid database handle %d", id);
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
return -1; return -1;
} }
@ -165,10 +156,14 @@ static cell AMX_NATIVE_CALL sql_nextrow(AMX *amx, cell *params) // 1 param
{ {
unsigned int id = params[1]-1; unsigned int id = params[1]-1;
if (id == -1)
{
return 0;
}
if (id >= Results.size() || Results[id]->isFree) if (id >= Results.size() || Results[id]->isFree)
{ {
MF_Log("Invalid result handle %d", id); SqlError(amx, "Invalid result handle %d", id);
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
return 0; return 0;
} }
@ -183,10 +178,11 @@ static cell AMX_NATIVE_CALL sql_nextrow(AMX *amx, cell *params) // 1 param
// - close connection // - close connection
static cell AMX_NATIVE_CALL sql_close(AMX *amx, cell *params) // 1 param static cell AMX_NATIVE_CALL sql_close(AMX *amx, cell *params) // 1 param
{ {
unsigned int id = params[1]-1; cell *addr = MF_GetAmxAddr(amx, params[1]);
unsigned int id = (*addr)-1;
if (id >= DBList.size() || DBList[id]->isFree) { if (id >= DBList.size() || DBList[id]->isFree) {
MF_Log("Invalid Database Handle %d", id); SqlError(amx, "Invalid database handle %d", id);
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
return 0; return 0;
} }
@ -194,6 +190,8 @@ static cell AMX_NATIVE_CALL sql_close(AMX *amx, cell *params) // 1 param
sql->Disconnect(); sql->Disconnect();
*addr = 0;
return 1; return 1;
} }
@ -207,8 +205,7 @@ static cell AMX_NATIVE_CALL sql_getfield(AMX *amx, cell *params) // 2-4 params
if (id >= Results.size() || Results[id]->isFree) if (id >= Results.size() || Results[id]->isFree)
{ {
MF_Log("Invalid result handle %d", id); SqlError(amx, "Invalid result handle %d", id);
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
return 0; return 0;
} }
@ -252,8 +249,7 @@ static cell AMX_NATIVE_CALL sql_getresult(AMX *amx, cell *params) // 4 params
if (id >= Results.size() || Results[id]->isFree) if (id >= Results.size() || Results[id]->isFree)
{ {
MF_Log("Invalid result handle %d", id); SqlError(amx, "Invalid result handle %d", id);
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
return 0; return 0;
} }
@ -291,12 +287,15 @@ static cell AMX_NATIVE_CALL sql_getresult(AMX *amx, cell *params) // 4 params
static cell AMX_NATIVE_CALL sql_free_result(AMX *amx, cell *params) static cell AMX_NATIVE_CALL sql_free_result(AMX *amx, cell *params)
{ {
unsigned int id = params[1]-1; cell *addr = MF_GetAmxAddr(amx, params[1]);
unsigned int id = (*addr)-1;
if (id == -1)
return 0;
if (id >= Results.size() || Results[id]->isFree) if (id >= Results.size() || Results[id]->isFree)
{ {
MF_Log("Invalid result handle %d", id); SqlError(amx, "Invalid result handle %d", id);
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
return 0; return 0;
} }
@ -304,6 +303,8 @@ static cell AMX_NATIVE_CALL sql_free_result(AMX *amx, cell *params)
Result->FreeResult(); Result->FreeResult();
*addr = 0;
return 1; return 1;
} }
@ -311,6 +312,9 @@ static cell AMX_NATIVE_CALL sql_num_rows(AMX *amx, cell *params)
{ {
unsigned int id = params[1]-1; unsigned int id = params[1]-1;
if (id == -1)
return 0;
if (id >= Results.size() || Results[id]->isFree) if (id >= Results.size() || Results[id]->isFree)
{ {
MF_Log("Invalid result handle %d", id); MF_Log("Invalid result handle %d", id);

View File

@ -26,6 +26,9 @@ SQLResult::SQLResult()
{ {
isFree = true; isFree = true;
RowCount = 0; RowCount = 0;
Fields.clear();
res = NULL;
row = 0;
} }
SQLResult::~SQLResult() SQLResult::~SQLResult()
@ -106,14 +109,6 @@ int SQL::Query(const char *query)
return -1; return -1;
} }
SQLResult *p = new SQLResult;
int ret = p->Query(this, query);
if (ret < 1)
{
delete p;
return ret;
}
unsigned int i = 0; unsigned int i = 0;
int id = -1; int id = -1;
@ -126,14 +121,34 @@ int SQL::Query(const char *query)
} }
if (id < 0) { if (id < 0) {
SQLResult *p = new SQLResult;
int ret = p->Query(this, query);
if (ret != 0)
{
delete p;
if (ret == -1)
return 0;
else
return -1;
} else {
Results.push_back(p); Results.push_back(p);
return Results.size(); return Results.size();
}
} else { } else {
SQLResult *r = Results[id]; SQLResult *r = Results[id];
Results[id] = p; int ret = Results[id]->Query(this, query);
delete r; if (ret != 0)
{
if (ret == -1)
return 0;
else
return -1;
} else {
return (id + 1); return (id + 1);
} }
}
} }
int SQLResult::Query(SQL *cn, const char *query) int SQLResult::Query(SQL *cn, const char *query)
@ -154,7 +169,7 @@ int SQLResult::Query(SQL *cn, const char *query)
RowCount = PQntuples(res); RowCount = PQntuples(res);
if (RowCount < 1) if (RowCount < 1)
return 0; return -1;
int i = 0; int i = 0;
const char *fld; const char *fld;
@ -164,7 +179,7 @@ int SQLResult::Query(SQL *cn, const char *query)
Fields.push_back(fld); Fields.push_back(fld);
} }
return 1; return 0;
} }
bool SQLResult::Nextrow() bool SQLResult::Nextrow()
@ -189,6 +204,9 @@ void SQLResult::FreeResult()
PQclear(res); PQclear(res);
Fields.clear(); Fields.clear();
res = 0;
row = 0;
isFree = true;
} }
const char *SQLResult::GetField(unsigned int field) const char *SQLResult::GetField(unsigned int field)