*/
enum SQLstatus { CREAD, CWRITE, WREAD, WWRITE, RREAD, RWRITE };
-unsigned long count(const char * const str, char a)
+static unsigned long count(const char * const str, char a)
{
unsigned long n = 0;
for (const char *p = str; *p; ++p)
class ReconnectTimer : public Timer
{
private:
- Module* mod;
+ Module* const mod;
public:
- ReconnectTimer(InspIRCd* SI, Module* m)
- : Timer(5, SI->Time(), false), mod(m)
+ ReconnectTimer(Module* m) : Timer(5, ServerInstance->Time(), false), mod(m)
{
}
virtual void Tick(time_t TIME);
class SQLConn : public EventHandler
{
private:
- InspIRCd* ServerInstance;
SQLhost confhost; /* The <database> entry */
Module* us; /* Pointer to the SQL provider itself */
PGconn* sql; /* PgSQL database connection handle */
time_t idle; /* Time we last heard from the database */
public:
- SQLConn(InspIRCd* SI, Module* self, const SQLhost& hi)
- : EventHandler(), ServerInstance(SI), confhost(hi), us(self), sql(NULL), status(CWRITE), qinprog(false)
+ SQLConn(Module* self, const SQLhost& hi)
+ : EventHandler(), confhost(hi), us(self), sql(NULL), status(CWRITE), qinprog(false)
{
- idle = this->ServerInstance->Time();
+ idle = ServerInstance->Time();
if(!DoConnect())
{
ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: Could not connect to database with id: " + ConvToStr(hi.id));
if(this->fd <= -1)
return false;
- if (!this->ServerInstance->SE->AddFd(this))
+ if (!ServerInstance->SE->AddFd(this, FD_WANT_NO_WRITE | FD_WANT_NO_READ))
{
ServerInstance->Logs->Log("m_pgsql",DEBUG, "BUG: Couldn't add pgsql socket to socket engine");
return false;
switch(PQconnectPoll(sql))
{
case PGRES_POLLING_WRITING:
- ServerInstance->SE->WantWrite(this);
+ ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_WRITE | FD_WANT_NO_READ);
status = CWRITE;
return true;
case PGRES_POLLING_READING:
+ ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
status = CREAD;
return true;
case PGRES_POLLING_FAILED:
return false;
case PGRES_POLLING_OK:
+ ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
status = WWRITE;
return DoConnectedPoll();
default:
if(!qinprog && queue.totalsize())
{
/* There's no query currently in progress, and there's queries in the queue. */
- SQLrequest& query = queue.front();
- DoQuery(query);
+ SQLrequest* query = queue.front();
+ DoQuery(*query);
}
if(PQconsumeInput(sql))
/* We just read stuff from the server, that counts as it being alive
* so update the idle-since time :p
*/
- idle = this->ServerInstance->Time();
+ idle = ServerInstance->Time();
if (PQisBusy(sql))
{
else if (qinprog)
{
/* Grab the request we're processing */
- SQLrequest& query = queue.front();
+ SQLrequest* query = queue.front();
/* Get a pointer to the module we're about to return the result to */
- Module* to = query.GetSource();
+ Module* to = query->source;
/* Fetch the result.. */
PGresult* result = PQgetResult(sql);
if(to)
{
/* ..and the result */
- PgSQLresult reply(us, to, query.id, result);
+ PgSQLresult reply(us, to, query->id, result);
/* Fix by brain, make sure the original query gets sent back in the reply */
- reply.query = query.query.q;
+ reply.query = query->query.q;
switch(PQresultStatus(result))
{
switch(PQresetPoll(sql))
{
case PGRES_POLLING_WRITING:
- ServerInstance->SE->WantWrite(this);
+ ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_WRITE | FD_WANT_NO_READ);
status = CWRITE;
return DoPoll();
case PGRES_POLLING_READING:
+ ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
status = CREAD;
return true;
case PGRES_POLLING_FAILED:
return false;
case PGRES_POLLING_OK:
+ ServerInstance->SE->ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
status = WWRITE;
return DoConnectedPoll();
default:
SQLerror Query(const SQLrequest &req)
{
- queue.push(req);
+ queue.push(new SQLrequest(req));
if(!qinprog && queue.totalsize())
{
/* There's no query currently in progress, and there's queries in the queue. */
- SQLrequest& query = queue.front();
- return DoQuery(query);
+ SQLrequest* query = queue.front();
+ return DoQuery(*query);
}
else
{
void Close()
{
- if (!this->ServerInstance->SE->DelFd(this))
+ if (!ServerInstance->SE->DelFd(this))
{
if (sql && PQstatus(sql) == CONNECTION_BAD)
{
- this->ServerInstance->SE->DelFd(this, true);
+ ServerInstance->SE->DelFd(this, true);
}
else
{
unsigned long currid;
char* sqlsuccess;
ReconnectTimer* retimer;
-
+ ServiceProvider sqlserv;
public:
- ModulePgSQL(InspIRCd* Me)
- : Module(Me), currid(0)
+ ModulePgSQL()
+ : currid(0), sqlserv(this, "SQL/pgsql", SERVICE_DATA)
{
- ServerInstance->Modules->UseInterface("SQLutils");
-
sqlsuccess = new char[strlen(SQLSUCCESS)+1];
strlcpy(sqlsuccess, SQLSUCCESS, strlen(SQLSUCCESS));
- if (!ServerInstance->Modules->PublishFeature("SQL", this))
- {
- throw ModuleException("BUG: PgSQL Unable to publish feature 'SQL'");
- }
-
ReadConf();
- ServerInstance->Modules->PublishInterface("SQL", this);
- Implementation eventlist[] = { I_OnUnloadModule, I_OnRequest, I_OnRehash };
- ServerInstance->Modules->Attach(eventlist, this, 3);
+ ServerInstance->Modules->AddService(sqlserv);
+ Implementation eventlist[] = { I_OnUnloadModule, I_OnRehash };
+ ServerInstance->Modules->Attach(eventlist, this, 2);
}
virtual ~ModulePgSQL()
ServerInstance->Timers->DelTimer(retimer);
ClearAllConnections();
delete[] sqlsuccess;
- ServerInstance->Modules->UnpublishInterface("SQL", this);
- ServerInstance->Modules->UnpublishFeature("SQL");
- ServerInstance->Modules->DoneWithInterface("SQLutils");
}
bool HostInConf(const SQLhost &h)
{
- ConfigReader conf(ServerInstance);
+ ConfigReader conf;
for(int i = 0; i < conf.Enumerate("database"); i++)
{
SQLhost host;
{
ClearOldConnections();
- ConfigReader conf(ServerInstance);
+ ConfigReader conf;
for(int i = 0; i < conf.Enumerate("database"); i++)
{
SQLhost host;
SQLConn* newconn;
- newconn = new SQLConn(ServerInstance, this, hi);
+ newconn = new SQLConn(this, hi);
connections.insert(std::make_pair(hi.id, newconn));
}
break;
}
}
- retimer = new ReconnectTimer(ServerInstance, this);
+ retimer = new ReconnectTimer(this);
ServerInstance->Timers->AddTimer(retimer);
}
- virtual const char* OnRequest(Request* request)
+ void OnRequest(Request& request)
{
- if(strcmp(SQLREQID, request->GetId()) == 0)
+ if(strcmp(SQLREQID, request.id) == 0)
{
- SQLrequest* req = (SQLrequest*)request;
+ SQLrequest* req = (SQLrequest*)&request;
ConnMap::iterator iter;
if((iter = connections.find(req->dbid)) != connections.end())
{
/* Execute query */
req->id = NewID();
req->error = iter->second->Query(*req);
-
- return (req->error.Id() == SQL_NO_ERROR) ? sqlsuccess : NULL;
}
else
{
req->error.Id(SQL_BAD_DBID);
- return NULL;
}
}
- return NULL;
}
- virtual void OnUnloadModule(Module* mod, const std::string& name)
+ virtual void OnUnloadModule(Module* mod)
{
/* When a module unloads we have to check all the pending queries for all our connections
* and set the Module* specifying where the query came from to NULL. If the query has already
virtual Version GetVersion()
{
- return Version("$Id$", VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
+ return Version("PostgreSQL Service Provider module for all other m_sql* modules, uses v2 of the SQL API", VF_VENDOR);
}
};