1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 /* $ModDesc: sqlite3 provider */
19 /* $CompileFlags: pkgconfversion("sqlite3","3.3") pkgconfincludes("sqlite3","/sqlite3.h","") */
20 /* $LinkerFlags: pkgconflibs("sqlite3","/libsqlite3.so","-lsqlite3") */
24 typedef std::map<std::string, reference<SQLConn> > ConnMap;
26 class SQLite3Result : public SQLResult
31 std::vector<std::string> columns;
32 std::vector<SQLEntries> fieldlists;
34 SQLite3Result() : currentrow(0), rows(0)
47 virtual bool GetRow(SQLEntries& result)
49 if (currentrow < rows)
51 result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
61 virtual void GetCols(std::vector<std::string>& result)
63 result.assign(columns.begin(), columns.end());
67 class SQLConn : public refcountbase
71 reference<ConfigTag> config;
74 SQLConn(ConfigTag* tag) : config(tag)
76 std::string host = tag->getString("hostname");
77 if (sqlite3_open_v2(host.c_str(), &conn, SQLITE_OPEN_READWRITE, 0) != SQLITE_OK)
79 ServerInstance->Logs->Log("m_sqlite3",DEFAULT, "WARNING: Could not open DB with id: " + tag->getString("id"));
86 sqlite3_interrupt(conn);
90 void Query(SQLQuery* query)
94 int err = sqlite3_prepare_v2(conn, query->query.c_str(), query->query.length(), &stmt, NULL);
97 SQLerror error(SQL_QSEND_FAIL, sqlite3_errmsg(conn));
98 query->OnError(error);
101 int cols = sqlite3_column_count(stmt);
102 res.columns.resize(cols);
103 for(int i=0; i < cols; i++)
105 res.columns[i] = sqlite3_column_name(stmt, i);
109 err = sqlite3_step(stmt);
110 if (err == SQLITE_ROW)
113 res.fieldlists.resize(res.rows + 1);
114 res.fieldlists[res.rows].resize(cols);
115 for(int i=0; i < cols; i++)
117 const char* txt = (const char*)sqlite3_column_text(stmt, i);
119 res.fieldlists[res.rows][i] = SQLEntry(txt);
123 else if (err == SQLITE_DONE)
125 query->OnResult(res);
130 SQLerror error(SQL_QREPLY_FAIL, sqlite3_errmsg(conn));
131 query->OnError(error);
135 sqlite3_finalize(stmt);
139 class SQLiteProvider : public SQLProvider
144 SQLiteProvider(Module* Parent) : SQLProvider(Parent, "SQL/SQLite") {}
146 std::string FormatQuery(const std::string& q, const ParamL& p)
149 unsigned int param = 0;
150 for(std::string::size_type i = 0; i < q.length(); i++)
156 // TODO numbered parameter support ('?1')
157 if (param < p.size())
159 char* escaped = sqlite3_mprintf("%q", p[param++].c_str());
161 sqlite3_free(escaped);
168 std::string FormatQuery(const std::string& q, const ParamM& p)
171 for(std::string::size_type i = 0; i < q.length(); i++)
179 while (i < q.length() && isalpha(q[i]))
180 field.push_back(q[i++]);
183 ParamM::const_iterator it = p.find(field);
186 char* escaped = sqlite3_mprintf("%q", it->second.c_str());
188 sqlite3_free(escaped);
195 void submit(SQLQuery* query)
197 ConnMap::iterator iter = hosts.find(query->dbid);
198 if (iter == hosts.end())
200 SQLerror err(SQL_BAD_DBID);
205 iter->second->Query(query);
211 class ModuleSQLite3 : public Module
214 SQLiteProvider sqlserv;
224 ServerInstance->Modules->AddService(sqlserv);
228 Implementation eventlist[] = { I_OnRehash };
229 ServerInstance->Modules->Attach(eventlist, this, 1);
232 virtual ~ModuleSQLite3()
238 sqlserv.hosts.clear();
239 ConfigTagList tags = ServerInstance->Config->ConfTags("database");
240 for(ConfigIter i = tags.first; i != tags.second; i++)
242 sqlserv.hosts.insert(std::make_pair(i->second->getString("id"), new SQLConn(i->second)));
246 void OnRehash(User* user)
253 return Version("sqlite3 provider", VF_VENDOR);
257 MODULE_INIT(ModuleSQLite3)