4 * Author: Matthias Crauwels <ultimate_@wol.be>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
32 #include "sqlChannel.h"
41 const sqlChannel::flagType
sqlChannel::F_BLOCKED
= 0x00000001 ;
42 const sqlChannel::flagType
sqlChannel::F_ALERT
= 0x00000002 ;
44 const int sqlChannel::EV_MISC
= 1 ; /* Uncategorized event */
45 const int sqlChannel::EV_NOTE
= 2 ; /* Miscellaneous notes */
46 const int sqlChannel::EV_CHANFIX
= 3 ; /* Manual chanfixes */
47 const int sqlChannel::EV_BLOCK
= 4 ; /* Channel block */
48 const int sqlChannel::EV_UNBLOCK
= 5 ; /* Channel unblock */
49 const int sqlChannel::EV_ALERT
= 6 ; /* Channel alert */
50 const int sqlChannel::EV_UNALERT
= 7 ; /* Channel unalert */
51 const int sqlChannel::EV_REQUESTOP
= 8 ; /* Requestops */
52 const int sqlChannel::EV_TEMPBLOCK
= 9 ; /* Temp channel block */
53 const int sqlChannel::EV_UNTEMPBLOCK
= 10 ; /* Temp channel unblock */
54 const int sqlChannel::EV_SIMULATE
= 11 ; /* Fix simulation */
56 unsigned long int sqlChannel::maxUserId
= 0;
58 sqlChannel::sqlChannel(sqlManager
* _myManager
) :
69 myManager
= _myManager
;
72 void sqlChannel::setAllMembers(dbHandle
* theDB
, int row
)
74 id
= atoi(theDB
->GetValue(row
, 0));
75 channel
= theDB
->GetValue(row
, 1);
76 flags
= atoi(theDB
->GetValue(row
, 2));
79 if (id
> maxUserId
) maxUserId
= id
;
83 * This function inserts a brand new channel into the DB.
85 bool sqlChannel::Insert(dbHandle
* cacheCon
)
87 /* Get a connection instance to our backend */
88 //dbHandle* cacheCon = myManager->getConnection();
90 /* Grab the next available user id */
93 /* Create the INSERT statement */
94 std::stringstream insertString
;
95 insertString
<< "INSERT INTO channels "
96 << "(id, channel, flags) "
100 << "'" << escapeSQLChars(channel
) << "', "
105 if (!cacheCon
->Exec(insertString
.str())) {
106 elog
<< "sqlChannel::Insert> Something went wrong: "
107 << cacheCon
->ErrorMessage()
115 /* Dispose of our connection instance */
116 //myManager->removeConnection(cacheCon);
119 } // sqlChannel::Insert()
121 bool sqlChannel::Delete(dbHandle
* cacheCon
)
125 /* Get a connection instance to our backend */
126 //dbHandle* cacheCon = myManager->getConnection();
128 /* Create the DELETE statement */
129 std::stringstream deleteString
;
130 deleteString
<< "DELETE FROM channels "
131 << "WHERE id = '" << id
<< "'"
134 if (!cacheCon
->Exec(deleteString
.str())) {
135 elog
<< "sqlChannel::Delete> Something went wrong: "
136 << cacheCon
->ErrorMessage()
142 /* Dispose of our connection instance */
143 //myManager->removeConnection(cacheCon);
148 bool sqlChannel::commit(dbHandle
* cacheCon
)
152 /* Get a connection instance to our backend */
153 //dbHandle* cacheCon = myManager->getConnection();
155 /* Create the UPDATE statement */
156 std::stringstream chanCommit
;
157 chanCommit
<< "UPDATE channels SET "
158 << "flags = " << flags
163 if (!cacheCon
->Exec(chanCommit
.str())) {
164 elog
<< "sqlChannel::commit> Something went wrong: "
165 << cacheCon
->ErrorMessage()
171 /* Dispose of our connection instance */
172 //myManager->removeConnection(cacheCon);
178 * This method writes a 'notes' record, recording an event that has
179 * occured in this channel.
182 void sqlChannel::addNote(dbHandle
* cacheCon
, unsigned short eventType
, iClient
* theUser
,
183 const std::string
& theMessage
)
185 unsigned int num_notes
= countNotes(cacheCon
, 0);
186 while (num_notes
>= MAXNOTECOUNT
) {
187 if (!deleteOldestNote(cacheCon
))
192 /* Get a connection instance to our backend */
193 //dbHandle* cacheCon = myManager->getConnection();
195 /* Create the INSERT statement */
196 std::stringstream theLog
;
197 theLog
<< "INSERT INTO notes (ts, channelID, user_name, event, message) "
199 << "now()::abstime::int4"
203 << theUser
->getAccount()
208 << escapeSQLChars(theMessage
)
212 if (!cacheCon
->Exec(theLog
.str())) {
213 elog
<< "sqlChannel::addNote> Something went wrong: "
214 << cacheCon
->ErrorMessage()
218 /* Dispose of our connection instance */
219 //myManager->removeConnection(cacheCon);
224 const std::string
sqlChannel::getLastNote(dbHandle
* cacheCon
, unsigned short eventType
, time_t& eventTime
)
228 /* Get a connection instance to our backend */
229 //dbHandle* cacheCon = myManager->getConnection();
231 /* Retrieve the last note */
232 std::stringstream queryString
;
233 queryString
<< "SELECT message,ts"
234 << " FROM notes WHERE channelID = "
238 << " ORDER BY ts DESC LIMIT 1"
241 if (cacheCon
->Exec(queryString
.str(),true)) {
242 if (cacheCon
->Tuples() > 0) {
243 std::string note
= cacheCon
->GetValue(0, 0);
244 eventTime
= atoi(cacheCon
->GetValue(0, 1));
249 /* Dispose of our connection instance */
250 //myManager->removeConnection(cacheCon);
255 bool sqlChannel::deleteNote(dbHandle
* cacheCon
, unsigned int messageId
)
259 /* Get a connection instance to our backend */
260 //dbHandle* cacheCon = myManager->getConnection();
262 /* Create the DELETE statement */
263 std::stringstream deleteString
;
264 deleteString
<< "DELETE FROM notes WHERE channelID = "
270 if (!cacheCon
->Exec(deleteString
.str())) {
271 elog
<< "sqlChannel::deleteNote> Something went wrong: "
272 << cacheCon
->ErrorMessage()
278 /* Dispose of our connection instance */
279 //myManager->removeConnection(cacheCon);
284 bool sqlChannel::deleteOldestNote(dbHandle
* cacheCon
)
288 /* Get a connection instance to our backend */
289 //dbHandle* cacheCon = myManager->getConnection();
291 /* Retrieve the id of the oldest note */
292 std::stringstream selectString
;
293 selectString
<< "SELECT id FROM notes WHERE channelID = "
295 << " ORDER BY ts ASC LIMIT 1"
298 if (cacheCon
->Exec(selectString
.str(),true)) {
299 if (cacheCon
->Tuples() > 0) {
300 unsigned int note_id
= atoi(cacheCon
->GetValue(0, 0));
302 std::stringstream deleteString
;
303 deleteString
<< "DELETE FROM notes WHERE id = "
307 if (cacheCon
->Exec(deleteString
.str(),true))
312 /* Dispose of our connection instance */
313 //myManager->removeConnection(cacheCon);
318 bool sqlChannel::deleteAllNotes(dbHandle
* cacheCon
)
322 /* Get a connection instance to our backend */
323 //dbHandle* cacheCon = myManager->getConnection();
325 /* Create the DELETE statement */
326 std::stringstream deleteString
;
327 deleteString
<< "DELETE FROM notes WHERE channelID = "
331 if (!cacheCon
->Exec(deleteString
.str())) {
332 elog
<< "sqlChannel::deleteAllNotes> Something went wrong: "
333 << cacheCon
->ErrorMessage()
339 /* Dispose of our connection instance */
340 //myManager->removeConnection(cacheCon);
345 size_t sqlChannel::countNotes(dbHandle
* cacheCon
, unsigned short eventType
)
347 /* Get a connection instance to our backend */
348 //dbHandle* cacheCon = myManager->getConnection();
350 /* Count the notes */
351 std::stringstream queryString
;
352 queryString
<< "SELECT count(id) FROM notes WHERE channelID = "
356 queryString
<< " AND event = "
361 size_t num_notes
= 0;
363 if (cacheCon
->Exec(queryString
.str(),true))
364 num_notes
= atoi(cacheCon
->GetValue(0, 0));
366 /* Dispose of our connection instance */
367 //myManager->removeConnection(cacheCon);
372 sqlChannel::~sqlChannel()
374 // No heap space allocated
379 } // namespace gnuworld