]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlite3.cpp
Compare the result of ldap_search_ext_s to LDAP_SUCCESS....
[user/henk/code/inspircd.git] / src / modules / extra / m_sqlite3.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2009 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2007, 2009 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include <sqlite3.h>
25 #include "sql.h"
26
27 #ifdef WINDOWS
28 # pragma comment(lib, "sqlite3.lib")
29 #endif
30
31 /* $ModDesc: sqlite3 provider */
32 /* $CompileFlags: pkgconfversion("sqlite3","3.3") pkgconfincludes("sqlite3","/sqlite3.h","") */
33 /* $LinkerFlags: pkgconflibs("sqlite3","/libsqlite3.so","-lsqlite3") */
34 /* $NoPedantic */
35
36 class SQLConn;
37 typedef std::map<std::string, SQLConn*> ConnMap;
38
39 class SQLite3Result : public SQLResult
40 {
41  public:
42         int currentrow;
43         int rows;
44         std::vector<std::string> columns;
45         std::vector<SQLEntries> fieldlists;
46
47         SQLite3Result() : currentrow(0), rows(0)
48         {
49         }
50
51         ~SQLite3Result()
52         {
53         }
54
55         virtual int Rows()
56         {
57                 return rows;
58         }
59
60         virtual bool GetRow(SQLEntries& result)
61         {
62                 if (currentrow < rows)
63                 {
64                         result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
65                         currentrow++;
66                         return true;
67                 }
68                 else
69                 {
70                         result.clear();
71                         return false;
72                 }
73         }
74
75         virtual void GetCols(std::vector<std::string>& result)
76         {
77                 result.assign(columns.begin(), columns.end());
78         }
79 };
80
81 class SQLConn : public SQLProvider
82 {
83  private:
84         sqlite3* conn;
85         reference<ConfigTag> config;
86
87  public:
88         SQLConn(Module* Parent, ConfigTag* tag) : SQLProvider(Parent, "SQL/" + tag->getString("id")), config(tag)
89         {
90                 std::string host = tag->getString("hostname");
91                 if (sqlite3_open_v2(host.c_str(), &conn, SQLITE_OPEN_READWRITE, 0) != SQLITE_OK)
92                 {
93                         ServerInstance->Logs->Log("m_sqlite3",DEFAULT, "WARNING: Could not open DB with id: " + tag->getString("id"));
94                         conn = NULL;
95                 }
96         }
97
98         ~SQLConn()
99         {
100                 sqlite3_interrupt(conn);
101                 sqlite3_close(conn);
102         }
103
104         void Query(SQLQuery* query, const std::string& q)
105         {
106                 SQLite3Result res;
107                 sqlite3_stmt *stmt;
108                 int err = sqlite3_prepare_v2(conn, q.c_str(), q.length(), &stmt, NULL);
109                 if (err != SQLITE_OK)
110                 {
111                         SQLerror error(SQL_QSEND_FAIL, sqlite3_errmsg(conn));
112                         query->OnError(error);
113                         return;
114                 }
115                 int cols = sqlite3_column_count(stmt);
116                 res.columns.resize(cols);
117                 for(int i=0; i < cols; i++)
118                 {
119                         res.columns[i] = sqlite3_column_name(stmt, i);
120                 }
121                 while (1)
122                 {
123                         err = sqlite3_step(stmt);
124                         if (err == SQLITE_ROW)
125                         {
126                                 // Add the row
127                                 res.fieldlists.resize(res.rows + 1);
128                                 res.fieldlists[res.rows].resize(cols);
129                                 for(int i=0; i < cols; i++)
130                                 {
131                                         const char* txt = (const char*)sqlite3_column_text(stmt, i);
132                                         if (txt)
133                                                 res.fieldlists[res.rows][i] = SQLEntry(txt);
134                                 }
135                                 res.rows++;
136                         }
137                         else if (err == SQLITE_DONE)
138                         {
139                                 query->OnResult(res);
140                                 break;
141                         }
142                         else
143                         {
144                                 SQLerror error(SQL_QREPLY_FAIL, sqlite3_errmsg(conn));
145                                 query->OnError(error);
146                                 break;
147                         }
148                 }
149                 sqlite3_finalize(stmt);
150         }
151
152         virtual void submit(SQLQuery* query, const std::string& q)
153         {
154                 Query(query, q);
155                 delete query;
156         }
157
158         virtual void submit(SQLQuery* query, const std::string& q, const ParamL& p)
159         {
160                 std::string res;
161                 unsigned int param = 0;
162                 for(std::string::size_type i = 0; i < q.length(); i++)
163                 {
164                         if (q[i] != '?')
165                                 res.push_back(q[i]);
166                         else
167                         {
168                                 if (param < p.size())
169                                 {
170                                         char* escaped = sqlite3_mprintf("%q", p[param++].c_str());
171                                         res.append(escaped);
172                                         sqlite3_free(escaped);
173                                 }
174                         }
175                 }
176                 submit(query, res);
177         }
178
179         virtual void submit(SQLQuery* query, const std::string& q, const ParamM& p)
180         {
181                 std::string res;
182                 for(std::string::size_type i = 0; i < q.length(); i++)
183                 {
184                         if (q[i] != '$')
185                                 res.push_back(q[i]);
186                         else
187                         {
188                                 std::string field;
189                                 i++;
190                                 while (i < q.length() && isalnum(q[i]))
191                                         field.push_back(q[i++]);
192                                 i--;
193
194                                 ParamM::const_iterator it = p.find(field);
195                                 if (it != p.end())
196                                 {
197                                         char* escaped = sqlite3_mprintf("%q", it->second.c_str());
198                                         res.append(escaped);
199                                         sqlite3_free(escaped);
200                                 }
201                         }
202                 }
203                 submit(query, res);
204         }
205 };
206
207 class ModuleSQLite3 : public Module
208 {
209  private:
210         ConnMap conns;
211
212  public:
213         ModuleSQLite3()
214         {
215         }
216
217         void init()
218         {
219                 ReadConf();
220
221                 Implementation eventlist[] = { I_OnRehash };
222                 ServerInstance->Modules->Attach(eventlist, this, 1);
223         }
224
225         virtual ~ModuleSQLite3()
226         {
227                 ClearConns();
228         }
229
230         void ClearConns()
231         {
232                 for(ConnMap::iterator i = conns.begin(); i != conns.end(); i++)
233                 {
234                         SQLConn* conn = i->second;
235                         ServerInstance->Modules->DelService(*conn);
236                         delete conn;
237                 }
238                 conns.clear();
239         }
240
241         void ReadConf()
242         {
243                 ClearConns();
244                 ConfigTagList tags = ServerInstance->Config->ConfTags("database");
245                 for(ConfigIter i = tags.first; i != tags.second; i++)
246                 {
247                         if (i->second->getString("module", "sqlite") != "sqlite")
248                                 continue;
249                         SQLConn* conn = new SQLConn(this, i->second);
250                         conns.insert(std::make_pair(i->second->getString("id"), conn));
251                         ServerInstance->Modules->AddService(*conn);
252                 }
253         }
254
255         void OnRehash(User* user)
256         {
257                 ReadConf();
258         }
259
260         Version GetVersion()
261         {
262                 return Version("sqlite3 provider", VF_VENDOR);
263         }
264 };
265
266 MODULE_INIT(ModuleSQLite3)