*/
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 = reinterpret_cast<const char *>(str); *p; ++p)
+ for (const char *p = str; *p; ++p)
{
if (*p == '?')
++n;
*/
std::string SQLhost::GetDSN()
{
- std::ostringstream conninfo("connect_timeout = '2'");
+ std::ostringstream conninfo("connect_timeout = '5'");
- if (ip.length())
- conninfo << " hostaddr = '" << ip << "'";
+ if (host.length())
+ conninfo << " host = '" << host << "'";
if (port)
conninfo << " port = '" << port << "'";
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);
};
-/** Used to resolve sql server hostnames
- */
-class SQLresolver : public Resolver
-{
- private:
- SQLhost host;
- Module* mod;
- public:
- SQLresolver(Module* m, InspIRCd* Instance, const SQLhost& hi, bool &cached)
- : Resolver(Instance, hi.host, DNS_QUERY_FORWARD, cached, (Module*)m), host(hi), mod(m)
- {
- }
-
- virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0);
-
- virtual void OnError(ResolverError e, const std::string &errormessage)
- {
- ServerInstance->Logs->Log("m_pgsql",DEBUG, "PgSQL: DNS lookup failed (%s), dying horribly", errormessage.c_str());
- }
-};
-
/** PgSQLresult is a subclass of the mostly-pure-virtual class SQLresult.
* All SQL providers must create their own subclass and define it's methods using that
* database library's data retriveal functions. The aim is to avoid a slow and inefficient process
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, I_OnUserRegister, I_OnCheckReady, I_OnUserDisconnect };
- ServerInstance->Modules->Attach(eventlist, this, 6);
+ 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");
}
- virtual void OnRehash(User* user, const std::string ¶meter)
+ virtual void OnRehash(User* user)
{
ReadConf();
}
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;
- int ipvalid;
host.id = conf.ReadValue("database", "id", i);
host.host = conf.ReadValue("database", "hostname", i);
if (HasHost(host))
continue;
-#ifdef IPV6
- if (strchr(host.host.c_str(),':'))
- {
- in6_addr blargle;
- ipvalid = inet_pton(AF_INET6, host.host.c_str(), &blargle);
- }
- else
-#endif
- {
- in_addr blargle;
- ipvalid = inet_aton(host.host.c_str(), &blargle);
- }
-
- if(ipvalid > 0)
- {
- /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */
- host.ip = host.host;
- this->AddConn(host);
- }
- else if(ipvalid == 0)
- {
- /* Conversion failed, assume it's a host */
- SQLresolver* resolver;
-
- try
- {
- bool cached;
- resolver = new SQLresolver(this, ServerInstance, host, cached);
- ServerInstance->AddResolver(resolver, cached);
- }
- catch(...)
- {
- /* THE WORLD IS COMING TO AN END! */
- }
- }
- else
- {
- /* Invalid address family, die horribly. */
- ServerInstance->Logs->Log("m_pgsql",DEBUG, "BUG: insp_aton failed returning -1, oh noes.");
- }
+ this->AddConn(host);
}
}
{
if (HasHost(hi))
{
- ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: A pgsql connection with id: %s already exists, possibly due to DNS delay. Aborting connection attempt.", hi.id.c_str());
+ ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: A pgsql connection with id: %s already exists. Aborting connection attempt.", hi.id.c_str());
return;
}
SQLConn* newconn;
- /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */
- 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);
}
};
-/* move this here to use AddConn, rather that than having the whole
- * module above SQLConn, since this is buggin me right now :/
- */
-void SQLresolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum)
-{
- if (!resultnum)
- {
- host.ip = result;
- ((ModulePgSQL*)mod)->AddConn(host);
- ((ModulePgSQL*)mod)->ClearOldConnections();
- }
-}
-
void ReconnectTimer::Tick(time_t time)
{
((ModulePgSQL*)mod)->ReadConf();