amxmodx/dlls/pgsql/pgsql_amx.cpp

238 lines
3.0 KiB
C++
Raw Normal View History

2004-04-02 19:54:21 +00:00
#include "pgsql_amx.h"
2004-06-29 02:50:39 +00:00
using namespace std;
2004-04-02 19:54:21 +00:00
bool is_ipaddr(const char *IP)
{
do {
if ((int)(*(IP++)) > 0x37) {
return false;
}
} while (*IP);
return true;
}
2004-06-29 02:50:39 +00:00
SQL::SQL()
2004-05-28 06:28:49 +00:00
{
2004-06-29 02:50:39 +00:00
isFree = true;
}
SQL::~SQL()
{
if (!isFree)
Disconnect();
}
SQLResult::SQLResult()
{
isFree = true;
RowCount = 0;
}
SQLResult::~SQLResult()
{
if (!isFree)
FreeResult();
}
int SQL::Error(int code)
{
if (isFree)
return 0;
ErrorStr.assign(PQerrorMessage(cn));
ErrorCode = code;
return code;
2004-05-28 06:28:49 +00:00
}
2004-04-02 19:54:21 +00:00
2004-06-29 02:50:39 +00:00
int SQL::Connect(const char *host, const char *user, const char *pass, const char *base)
2004-04-02 19:54:21 +00:00
{
2004-06-29 02:50:39 +00:00
Username.assign(user);
Password.assign(pass);
Database.assign(base);
Host.assign(host);
2004-04-02 19:54:21 +00:00
2004-06-29 02:50:39 +00:00
isFree = false;
int err = 0;
if (is_ipaddr(Host.c_str())) {
2004-05-28 06:28:49 +00:00
cstr.assign("hostaddr = '");
2004-04-02 19:54:21 +00:00
} else {
2004-05-28 06:28:49 +00:00
cstr.assign("host = '");
2004-04-02 19:54:21 +00:00
}
2004-05-28 06:28:49 +00:00
2004-06-29 02:50:39 +00:00
cstr.append(Host);
2004-05-28 06:28:49 +00:00
cstr.append("' user = '");
2004-06-29 02:50:39 +00:00
cstr.append(Username);
2004-05-28 06:28:49 +00:00
cstr.append("' pass = '");
2004-06-29 02:50:39 +00:00
cstr.append(Password);
2004-05-28 06:28:49 +00:00
cstr.append("' name = '");
2004-06-29 02:50:39 +00:00
cstr.append(Database);
2004-05-28 06:28:49 +00:00
cstr.append("'");
cn = PQconnectdb(cstr.c_str());
2004-04-02 19:54:21 +00:00
2004-05-28 06:28:49 +00:00
if (PQstatus(cn) != CONNECTION_OK) {
2004-06-29 02:50:39 +00:00
Error(PQstatus(cn));
PQfinish(cn);
isFree = true;
2004-05-28 06:28:49 +00:00
return 0;
}
2004-06-29 02:50:39 +00:00
isFree = false;
return 1;
2004-04-02 19:54:21 +00:00
}
2004-06-29 02:50:39 +00:00
void SQL::Disconnect()
2004-04-02 19:54:21 +00:00
{
2004-06-29 02:50:39 +00:00
if (isFree)
return;
2004-05-28 06:28:49 +00:00
2004-06-29 02:50:39 +00:00
Host.clear();
Username.clear();
Password.clear();
Database.clear();
2004-05-28 06:28:49 +00:00
2004-06-29 02:50:39 +00:00
PQfinish(cn);
isFree = true;
}
int SQL::Query(const char *query)
{
if (isFree)
{
ErrorCode = -1;
return -1;
}
SQLResult *p = new SQLResult;
int ret = p->Query(this, query);
if (ret < 1)
{
delete p;
return ret;
}
unsigned int i = 0;
int id = -1;
for (i=0; i < Results.size(); i++)
{
if (Results[i]->isFree) {
2004-05-28 06:28:49 +00:00
id = i;
break;
2004-04-03 00:52:53 +00:00
}
}
2004-05-28 06:28:49 +00:00
if (id < 0) {
2004-06-29 02:50:39 +00:00
Results.push_back(p);
return Results.size();
2004-04-02 19:54:21 +00:00
} else {
2004-06-29 02:50:39 +00:00
SQLResult *r = Results[id];
Results[id] = p;
delete r;
return (id + 1);
2004-04-02 19:54:21 +00:00
}
}
2004-06-29 05:54:53 +00:00
int SQLResult::Query(SQL *cn, const char *query)
2004-05-28 06:28:49 +00:00
{
2004-06-29 02:50:39 +00:00
res = PQexec(cn->cn, query);
row = -1;
2004-04-02 19:54:21 +00:00
2004-06-29 02:50:39 +00:00
sql = cn;
2004-04-02 19:54:21 +00:00
2004-06-29 02:50:39 +00:00
int queryResult = PQresultStatus(res);
2004-05-28 06:28:49 +00:00
2004-06-29 02:50:39 +00:00
if (queryResult != PGRES_COMMAND_OK)
{
cn->Error(queryResult);
return -1;
2004-04-02 19:54:21 +00:00
}
2004-06-29 02:50:39 +00:00
RowCount = PQntuples(res);
2004-04-02 19:54:21 +00:00
2004-06-29 02:50:39 +00:00
if (RowCount < 1)
return 0;
2004-04-02 19:54:21 +00:00
2004-06-29 02:50:39 +00:00
int i = 0;
const char *fld;
for (i=0; i < PQnfields(res); i++)
{
fld = PQfname(res, i);
Fields.push_back(fld);
2004-04-02 19:54:21 +00:00
}
2004-06-29 02:50:39 +00:00
return 1;
2004-04-02 19:54:21 +00:00
}
2004-06-29 02:50:39 +00:00
bool SQLResult::Nextrow()
2004-04-02 19:54:21 +00:00
{
2004-06-29 02:50:39 +00:00
if (isFree)
return false;
2004-04-03 01:04:57 +00:00
2004-06-29 02:50:39 +00:00
if (row >= RowCount)
{
return false;
2004-04-02 19:54:21 +00:00
}
2004-06-29 02:50:39 +00:00
row++;
2004-04-02 19:54:21 +00:00
2004-06-29 02:50:39 +00:00
return true;
2004-04-02 19:54:21 +00:00
}
2004-06-29 02:50:39 +00:00
void SQLResult::FreeResult()
2004-04-02 19:54:21 +00:00
{
2004-06-29 02:50:39 +00:00
if (isFree)
return;
2004-04-03 01:04:57 +00:00
2004-06-29 02:50:39 +00:00
PQclear(res);
Fields.clear();
}
2004-04-03 01:04:57 +00:00
2004-06-29 02:50:39 +00:00
const char *SQLResult::GetField(unsigned int field)
{
if (field > (unsigned int)PQnfields(res))
{
sql->Error(-1);
sql->ErrorStr.assign("Invalid field.");
2004-04-02 19:54:21 +00:00
return 0;
2004-06-29 02:50:39 +00:00
}
2004-04-02 19:54:21 +00:00
2004-06-29 02:50:39 +00:00
return PQgetvalue(res, row, field);
2004-04-02 19:54:21 +00:00
}
2004-06-29 02:50:39 +00:00
const char *SQLResult::GetField(const char *field)
2004-04-02 19:54:21 +00:00
{
2004-06-29 02:50:39 +00:00
unsigned int fld;
int id = -1;
for (fld = 0; fld < Fields.size(); fld++)
{
if (strcmp(Fields[fld], field)==0)
{
id = fld;
break;
}
}
2004-05-28 06:28:49 +00:00
2004-06-29 02:50:39 +00:00
if (id == -1)
{
sql->Error(-1);
sql->ErrorStr.assign("Invalid field.");
2004-05-28 06:28:49 +00:00
return 0;
}
2004-06-29 02:50:39 +00:00
return PQgetvalue(res, row, id);
2004-04-02 19:54:21 +00:00
}
2004-06-29 02:50:39 +00:00
unsigned int SQLResult::NumRows()
2004-05-28 09:21:11 +00:00
{
2004-06-29 02:50:39 +00:00
if (isFree)
return 0;
2004-05-28 09:21:11 +00:00
2004-06-29 02:50:39 +00:00
return RowCount;
2004-05-28 06:28:49 +00:00
}