New DBI API.
This commit is contained in:
parent
527ca0efb7
commit
4f3be95a36
|
@ -1,53 +1,6 @@
|
||||||
/* AMX Mod X
|
|
||||||
* PostgreSQL Module
|
|
||||||
*
|
|
||||||
* by David "BAILOPAN" Anderson
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms of the GNU General Public License as published by the
|
|
||||||
* Free Software Foundation; either version 2 of the License, or (at
|
|
||||||
* your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful, but
|
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software Foundation,
|
|
||||||
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
*
|
|
||||||
* In addition, as a special exception, the author gives permission to
|
|
||||||
* link the code of this program with the Half-Life Game Engine ("HL
|
|
||||||
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
|
|
||||||
* L.L.C ("Valve"). You must obey the GNU General Public License in all
|
|
||||||
* respects for all of the code used other than the HL Engine and MODs
|
|
||||||
* from Valve. If you modify this file, you may extend this exception
|
|
||||||
* to your version of the file, but you are not obligated to do so. If
|
|
||||||
* you do not wish to do so, delete this exception statement from your
|
|
||||||
* version.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "pgsql_amx.h"
|
#include "pgsql_amx.h"
|
||||||
|
|
||||||
std::string error;
|
using namespace std;
|
||||||
std::vector<pgdb*> dblist;
|
|
||||||
|
|
||||||
int sql_exists(const char* host,const char* user,const char* pass,const char* dbase) {
|
|
||||||
std::vector<pgdb*>::iterator i;
|
|
||||||
int id = 0;
|
|
||||||
for (i=dblist.begin(); i!=dblist.end(); i++) {
|
|
||||||
id++;
|
|
||||||
if (((*i)->host.compare(host) == 0) &&
|
|
||||||
((*i)->user.compare(user) == 0) &&
|
|
||||||
((*i)->pass.compare(pass) == 0) &&
|
|
||||||
((*i)->name.compare(dbase) == 0) &&
|
|
||||||
(!(*i)->free)) {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_ipaddr(const char *IP)
|
bool is_ipaddr(const char *IP)
|
||||||
{
|
{
|
||||||
|
@ -60,220 +13,225 @@ bool is_ipaddr(const char *IP)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pgdb::Kill()
|
SQL::SQL()
|
||||||
{
|
{
|
||||||
if (free)
|
isFree = true;
|
||||||
return;
|
|
||||||
PQfinish(cn);
|
|
||||||
host.clear();
|
|
||||||
user.clear();
|
|
||||||
pass.clear();
|
|
||||||
name.clear();
|
|
||||||
row = 0;
|
|
||||||
free = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int pgdb::Connect(const char *hh, const char *uu, const char *pp, const char *dd)
|
SQL::~SQL()
|
||||||
{
|
{
|
||||||
host.assign(hh);
|
if (!isFree)
|
||||||
user.assign(uu);
|
Disconnect();
|
||||||
pass.assign(pp);
|
}
|
||||||
name.assign(dd);
|
|
||||||
|
|
||||||
if (is_ipaddr(host.c_str())) {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SQL::Connect(const char *host, const char *user, const char *pass, const char *base)
|
||||||
|
{
|
||||||
|
Username.assign(user);
|
||||||
|
Password.assign(pass);
|
||||||
|
Database.assign(base);
|
||||||
|
Host.assign(host);
|
||||||
|
|
||||||
|
isFree = false;
|
||||||
|
int err = 0;
|
||||||
|
|
||||||
|
if (is_ipaddr(Host.c_str())) {
|
||||||
cstr.assign("hostaddr = '");
|
cstr.assign("hostaddr = '");
|
||||||
} else {
|
} else {
|
||||||
cstr.assign("host = '");
|
cstr.assign("host = '");
|
||||||
}
|
}
|
||||||
|
|
||||||
cstr.append(host);
|
cstr.append(Host);
|
||||||
cstr.append("' user = '");
|
cstr.append("' user = '");
|
||||||
cstr.append(user);
|
cstr.append(Username);
|
||||||
cstr.append("' pass = '");
|
cstr.append("' pass = '");
|
||||||
cstr.append(pass);
|
cstr.append(Password);
|
||||||
cstr.append("' name = '");
|
cstr.append("' name = '");
|
||||||
cstr.append(name);
|
cstr.append(Database);
|
||||||
cstr.append("'");
|
cstr.append("'");
|
||||||
|
|
||||||
cn = PQconnectdb(cstr.c_str());
|
cn = PQconnectdb(cstr.c_str());
|
||||||
|
|
||||||
if (PQstatus(cn) != CONNECTION_OK) {
|
if (PQstatus(cn) != CONNECTION_OK) {
|
||||||
err.assign(PQerrorMessage(cn));
|
Error(PQstatus(cn));
|
||||||
free = true;
|
PQfinish(cn);
|
||||||
lastError = PQstatus(cn);
|
isFree = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
free = false;
|
isFree = false;
|
||||||
return true;
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static cell AMX_NATIVE_CALL pgsql_connect(AMX *amx, cell *params)
|
void SQL::Disconnect()
|
||||||
{
|
{
|
||||||
int len;
|
if (isFree)
|
||||||
unsigned int i;
|
return;
|
||||||
pgdb *c = NULL;
|
|
||||||
|
|
||||||
const char *host = MF_GetAmxString(amx,params[1],0,&len);
|
Host.clear();
|
||||||
const char *user = MF_GetAmxString(amx,params[2],1,&len);
|
Username.clear();
|
||||||
const char *pass = MF_GetAmxString(amx,params[3],2,&len);
|
Password.clear();
|
||||||
const char *name = MF_GetAmxString(amx,params[4],3,&len);
|
Database.clear();
|
||||||
|
|
||||||
int id = sql_exists(host, user, pass, name);
|
PQfinish(cn);
|
||||||
|
|
||||||
if (id >= 0)
|
isFree = true;
|
||||||
return id;
|
}
|
||||||
|
|
||||||
id = -1;
|
int SQL::Query(const char *query)
|
||||||
for (i=0; i<dblist.size(); i++) {
|
{
|
||||||
if (dblist[i]->free) {
|
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) {
|
||||||
id = i;
|
id = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id < 0) {
|
if (id < 0) {
|
||||||
c = new pgdb;
|
Results.push_back(p);
|
||||||
dblist.push_back(c);
|
return Results.size();
|
||||||
id = dblist.size() - 1;
|
|
||||||
} else {
|
} else {
|
||||||
c = dblist[id];
|
SQLResult *r = Results[id];
|
||||||
|
Results[id] = p;
|
||||||
|
delete r;
|
||||||
|
return (id + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!c->Connect(host, user, pass, name)) {
|
|
||||||
MF_SetAmxString(amx, params[5], c->err.c_str(), params[6]);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return id+1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SQLResult::Query(SQL *cn, const char *query)
|
||||||
static cell AMX_NATIVE_CALL pgsql_error(AMX *amx, cell *params)
|
|
||||||
{
|
{
|
||||||
unsigned int id = params[1] - 1;
|
res = PQexec(cn->cn, query);
|
||||||
|
row = -1;
|
||||||
|
|
||||||
if (id >= dblist.size() || dblist[id]->free) {
|
sql = cn;
|
||||||
error.assign("Invalid handle.");
|
|
||||||
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
dblist[id]->err.assign(PQerrorMessage(dblist[id]->cn));
|
int queryResult = PQresultStatus(res);
|
||||||
MF_SetAmxString(amx, params[2], dblist[id]->err.c_str(), params[3]);
|
|
||||||
return dblist[id]->lastError;
|
|
||||||
}
|
|
||||||
|
|
||||||
static cell AMX_NATIVE_CALL pgsql_query(AMX *amx, cell *params)
|
if (queryResult != PGRES_COMMAND_OK)
|
||||||
{
|
{
|
||||||
unsigned int id = params[1] - 1;
|
cn->Error(queryResult);
|
||||||
|
|
||||||
if (id >= dblist.size() || dblist[id]->free) {
|
|
||||||
error.assign("Invalid handle.");
|
|
||||||
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pgdb *c = dblist[id];
|
|
||||||
|
|
||||||
if (c->res)
|
|
||||||
c->reset();
|
|
||||||
|
|
||||||
int i;
|
|
||||||
const char *query = MF_FormatAmxString(amx, params, 2, &i);
|
|
||||||
|
|
||||||
c->res = PQexec(c->cn, query);
|
|
||||||
c->row = 0;
|
|
||||||
|
|
||||||
if (PQresultStatus(c->res) != PGRES_COMMAND_OK) {
|
|
||||||
c->lastError = PQresultStatus(c->res);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return PQntuples(c->res);
|
RowCount = PQntuples(res);
|
||||||
}
|
|
||||||
|
|
||||||
static cell AMX_NATIVE_CALL pgsql_nextrow(AMX *amx, cell *params)
|
if (RowCount < 1)
|
||||||
{
|
|
||||||
unsigned int id = params[1] - 1;
|
|
||||||
|
|
||||||
if (id >= dblist.size() || dblist[id]->free) {
|
|
||||||
error.assign("Invalid handle.");
|
|
||||||
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
const char *fld;
|
||||||
|
for (i=0; i < PQnfields(res); i++)
|
||||||
|
{
|
||||||
|
fld = PQfname(res, i);
|
||||||
|
Fields.push_back(fld);
|
||||||
}
|
}
|
||||||
|
|
||||||
pgdb *c = dblist[id];
|
|
||||||
|
|
||||||
if (c->row > PQntuples(c->res))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
c->row++;
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static cell AMX_NATIVE_CALL pgsql_getfield(AMX *amx, cell *params)
|
bool SQLResult::Nextrow()
|
||||||
{
|
{
|
||||||
unsigned int id = params[1] - 1;
|
if (isFree)
|
||||||
int col = params[2];
|
return false;
|
||||||
|
|
||||||
if (id >= dblist.size() || dblist[id]->free) {
|
if (row >= RowCount)
|
||||||
error.assign("Invalid handle.");
|
{
|
||||||
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
row++;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SQLResult::FreeResult()
|
||||||
|
{
|
||||||
|
if (isFree)
|
||||||
|
return;
|
||||||
|
|
||||||
|
PQclear(res);
|
||||||
|
Fields.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *SQLResult::GetField(unsigned int field)
|
||||||
|
{
|
||||||
|
if (field > (unsigned int)PQnfields(res))
|
||||||
|
{
|
||||||
|
sql->Error(-1);
|
||||||
|
sql->ErrorStr.assign("Invalid field.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pgdb *c = dblist[id];
|
return PQgetvalue(res, row, field);
|
||||||
|
|
||||||
if (col-1 > PQnfields(c->res))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
char *field = PQgetvalue(c->res, c->row, col);
|
|
||||||
return MF_SetAmxString(amx, params[3], field?field:"", params[4]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static cell AMX_NATIVE_CALL pgsql_close(AMX *amx, cell *params)
|
const char *SQLResult::GetField(const char *field)
|
||||||
{
|
{
|
||||||
unsigned int id = params[1] - 1;
|
unsigned int fld;
|
||||||
|
int id = -1;
|
||||||
|
for (fld = 0; fld < Fields.size(); fld++)
|
||||||
|
{
|
||||||
|
if (strcmp(Fields[fld], field)==0)
|
||||||
|
{
|
||||||
|
id = fld;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (id >= dblist.size() || dblist[id]->free) {
|
if (id == -1)
|
||||||
error.assign("Invalid handle.");
|
{
|
||||||
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
|
sql->Error(-1);
|
||||||
|
sql->ErrorStr.assign("Invalid field.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pgdb *c = dblist[id];
|
return PQgetvalue(res, row, id);
|
||||||
|
|
||||||
c->Kill();
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static cell AMX_NATIVE_CALL dbi_type(AMX *amx, cell *params)
|
unsigned int SQLResult::NumRows()
|
||||||
{
|
{
|
||||||
return MF_SetAmxString(amx, params[1], "pgsql", params[2]);
|
if (isFree)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return RowCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnAmxxAttach()
|
|
||||||
{
|
|
||||||
MF_AddNatives(pgsql_exports);
|
|
||||||
}
|
|
||||||
|
|
||||||
AMX_NATIVE_INFO pgsql_exports[] = {
|
|
||||||
{"dbi_connect", pgsql_connect},
|
|
||||||
{"dbi_error", pgsql_error},
|
|
||||||
{"dbi_query", pgsql_query},
|
|
||||||
{"dbi_nextrow", pgsql_nextrow},
|
|
||||||
{"dbi_close", pgsql_close},
|
|
||||||
{"dbi_getfield", pgsql_getfield},
|
|
||||||
{"dbi_type", dbi_type},
|
|
||||||
{"pgsql_connect", pgsql_connect},
|
|
||||||
{"pgsql_error", pgsql_error},
|
|
||||||
{"pgsql_query", pgsql_query},
|
|
||||||
{"pgsql_nextrow", pgsql_nextrow},
|
|
||||||
{"pgsql_close", pgsql_close},
|
|
||||||
{"pgsql_getfield", pgsql_getfield},
|
|
||||||
|
|
||||||
{NULL, NULL},
|
|
||||||
};
|
|
||||||
|
|
|
@ -1,62 +0,0 @@
|
||||||
/* AMX Mod X
|
|
||||||
* PostgreSQL Module
|
|
||||||
*
|
|
||||||
* by David "BAILOPAN" Anderson
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms of the GNU General Public License as published by the
|
|
||||||
* Free Software Foundation; either version 2 of the License, or (at
|
|
||||||
* your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful, but
|
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software Foundation,
|
|
||||||
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
*
|
|
||||||
* In addition, as a special exception, the author gives permission to
|
|
||||||
* link the code of this program with the Half-Life Game Engine ("HL
|
|
||||||
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
|
|
||||||
* L.L.C ("Valve"). You must obey the GNU General Public License in all
|
|
||||||
* respects for all of the code used other than the HL Engine and MODs
|
|
||||||
* from Valve. If you modify this file, you may extend this exception
|
|
||||||
* to your version of the file, but you are not obligated to do so. If
|
|
||||||
* you do not wish to do so, delete this exception statement from your
|
|
||||||
* version.
|
|
||||||
*/
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <libpq-fe.h>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include "amxxmodule.h"
|
|
||||||
|
|
||||||
class pgdb
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void reset() { PQclear(res); row = 0; }
|
|
||||||
pgdb() { free = true; }
|
|
||||||
int Connect(const char *hh, const char *uu, const char *pp, const char *dd);
|
|
||||||
void Kill();
|
|
||||||
~pgdb() { Kill(); }
|
|
||||||
|
|
||||||
bool free;
|
|
||||||
PGconn *cn;
|
|
||||||
PGresult *res;
|
|
||||||
int row;
|
|
||||||
std::string host;
|
|
||||||
std::string user;
|
|
||||||
std::string pass;
|
|
||||||
std::string name;
|
|
||||||
std::string cstr;
|
|
||||||
std::string err;
|
|
||||||
int lastError;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern std::string error;
|
|
||||||
extern std::vector<pgdb*> dblist;
|
|
||||||
extern AMX_NATIVE_INFO pgsql_exports[];
|
|
|
@ -148,6 +148,9 @@
|
||||||
<Filter
|
<Filter
|
||||||
Name="Source Files"
|
Name="Source Files"
|
||||||
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat">
|
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat">
|
||||||
|
<File
|
||||||
|
RelativePath=".\pgsql.cpp">
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath="pgsql_amx.cpp">
|
RelativePath="pgsql_amx.cpp">
|
||||||
<FileConfiguration
|
<FileConfiguration
|
||||||
|
|
Loading…
Reference in New Issue
Block a user