summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp
Unidiff
Diffstat (limited to 'libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp') (more/less context) (show whitespace changes)
-rw-r--r--libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp43
1 files changed, 24 insertions, 19 deletions
diff --git a/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp b/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp
index f4f3c94..105c106 100644
--- a/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp
+++ b/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp
@@ -22,38 +22,43 @@
22 -. .:....=;==+<; You should have received a copy of the GNU 22 -. .:....=;==+<; You should have received a copy of the GNU
23 -_. . . )=. = Library General Public License along with 23 -_. . . )=. = Library General Public License along with
24 -- :-=` this library; see the file COPYING.LIB. 24 -- :-=` this library; see the file COPYING.LIB.
25 If not, write to the Free Software Foundation, 25 If not, write to the Free Software Foundation,
26 Inc., 59 Temple Place - Suite 330, 26 Inc., 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA. 27 Boston, MA 02111-1307, USA.
28*/ 28*/
29/* 29/*
30 * SQL Backend for the OPIE-Calender Database. 30 * SQL Backend for the OPIE-Calender Database.
31 * 31 *
32 */ 32 */
33 33
34#include <stdio.h> 34/* OPIE */
35#include <stdlib.h>
36
37#include <qarray.h>
38#include <qstringlist.h>
39
40#include <qpe/global.h>
41
42#include <opie2/osqldriver.h> 35#include <opie2/osqldriver.h>
43#include <opie2/osqlmanager.h> 36#include <opie2/osqlmanager.h>
44#include <opie2/osqlquery.h> 37#include <opie2/osqlquery.h>
45 38
46#include <opie2/opimrecurrence.h> 39#include <opie2/opimrecurrence.h>
47#include <opie2/odatebookaccessbackend_sql.h> 40#include <opie2/odatebookaccessbackend_sql.h>
41#include <opie2/odebug.h>
42
43#include <qpe/global.h>
44
45/* QT */
46#include <qarray.h>
47#include <qstringlist.h>
48
49/* STD */
50#include <stdio.h>
51#include <stdlib.h>
52
48 53
49using namespace Opie::DB; 54using namespace Opie::DB;
50 55
51namespace { 56namespace {
52 /** 57 /**
53 * a find query for custom elements 58 * a find query for custom elements
54 */ 59 */
55 class FindCustomQuery : public OSQLQuery { 60 class FindCustomQuery : public OSQLQuery {
56 public: 61 public:
57 FindCustomQuery(int uid); 62 FindCustomQuery(int uid);
58 FindCustomQuery(const QArray<int>& ); 63 FindCustomQuery(const QArray<int>& );
59 ~FindCustomQuery(); 64 ~FindCustomQuery();
@@ -153,25 +158,25 @@ bool ODateBookAccessBackend_SQL::load()
153 // It is save here to create the table, even if it 158 // It is save here to create the table, even if it
154 // do exist. ( Is that correct for all databases ?? ) 159 // do exist. ( Is that correct for all databases ?? )
155 QStringqu = "create table datebook( uid INTEGER PRIMARY KEY "; 160 QStringqu = "create table datebook( uid INTEGER PRIMARY KEY ";
156 161
157 QMap<int, QString>::Iterator it; 162 QMap<int, QString>::Iterator it;
158 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 163 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
159 qu += QString( ",%1 VARCHAR(10)" ).arg( it.data() ); 164 qu += QString( ",%1 VARCHAR(10)" ).arg( it.data() );
160 } 165 }
161 qu += " );"; 166 qu += " );";
162 167
163 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );"; 168 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );";
164 169
165 qWarning( "command: %s", qu.latin1() ); 170 owarn << "command: " << qu << "" << oendl;
166 171
167 OSQLRawQuery raw( qu ); 172 OSQLRawQuery raw( qu );
168 OSQLResult res = m_driver->query( &raw ); 173 OSQLResult res = m_driver->query( &raw );
169 if ( res.state() != OSQLResult::Success ) 174 if ( res.state() != OSQLResult::Success )
170 return false; 175 return false;
171 176
172 update(); 177 update();
173 178
174 return true; 179 return true;
175} 180}
176 181
177void ODateBookAccessBackend_SQL::update() 182void ODateBookAccessBackend_SQL::update()
@@ -212,51 +217,51 @@ void ODateBookAccessBackend_SQL::clear()
212{ 217{
213 QString qu = "drop table datebook;"; 218 QString qu = "drop table datebook;";
214 qu += "drop table custom_data;"; 219 qu += "drop table custom_data;";
215 220
216 OSQLRawQuery raw( qu ); 221 OSQLRawQuery raw( qu );
217 OSQLResult res = m_driver->query( &raw ); 222 OSQLResult res = m_driver->query( &raw );
218 223
219 reload(); 224 reload();
220} 225}
221 226
222 227
223OPimEvent ODateBookAccessBackend_SQL::find( int uid ) const{ 228OPimEvent ODateBookAccessBackend_SQL::find( int uid ) const{
224 qDebug( "ODateBookAccessBackend_SQL::find( %d )", uid ); 229 odebug << "ODateBookAccessBackend_SQL::find( " << uid << " )" << oendl;
225 230
226 QString qu = "select *"; 231 QString qu = "select *";
227 qu += "from datebook where uid = " + QString::number(uid); 232 qu += "from datebook where uid = " + QString::number(uid);
228 233
229 qDebug( "Query: %s", qu.latin1() ); 234 odebug << "Query: " << qu << "" << oendl;
230 235
231 OSQLRawQuery raw( qu ); 236 OSQLRawQuery raw( qu );
232 OSQLResult res = m_driver->query( &raw ); 237 OSQLResult res = m_driver->query( &raw );
233 238
234 OSQLResultItem resItem = res.first(); 239 OSQLResultItem resItem = res.first();
235 240
236 // Create Map for date event and insert UID 241 // Create Map for date event and insert UID
237 QMap<int,QString> dateEventMap; 242 QMap<int,QString> dateEventMap;
238 dateEventMap.insert( OPimEvent::FUid, QString::number( uid ) ); 243 dateEventMap.insert( OPimEvent::FUid, QString::number( uid ) );
239 244
240 // Now insert the data out of the columns into the map. 245 // Now insert the data out of the columns into the map.
241 QMapConstIterator<int, QString> it; 246 QMapConstIterator<int, QString> it;
242 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 247 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
243 dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) ); 248 dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) );
244 } 249 }
245 250
246 // Last step: Put map into date event, add custom map and return it 251 // Last step: Put map into date event, add custom map and return it
247 OPimEvent retDate( dateEventMap ); 252 OPimEvent retDate( dateEventMap );
248 retDate.setExtraMap( requestCustom( uid ) ); 253 retDate.setExtraMap( requestCustom( uid ) );
249 254
250 qDebug( "ODateBookAccessBackend_SQL::find( %d ) end", uid ); 255 odebug << "ODateBookAccessBackend_SQL::find( " << uid << " ) end" << oendl;
251 return retDate; 256 return retDate;
252} 257}
253 258
254// FIXME: Speed up update of uid's.. 259// FIXME: Speed up update of uid's..
255bool ODateBookAccessBackend_SQL::add( const OPimEvent& ev ) 260bool ODateBookAccessBackend_SQL::add( const OPimEvent& ev )
256{ 261{
257 QMap<int,QString> eventMap = ev.toMap(); 262 QMap<int,QString> eventMap = ev.toMap();
258 263
259 QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() ); 264 QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() );
260 QMap<int, QString>::Iterator it; 265 QMap<int, QString>::Iterator it;
261 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 266 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
262 if ( !eventMap[it.key()].isEmpty() ) 267 if ( !eventMap[it.key()].isEmpty() )
@@ -274,25 +279,25 @@ bool ODateBookAccessBackend_SQL::add( const OPimEvent& ev )
274 qu += "insert into custom_data VALUES(" 279 qu += "insert into custom_data VALUES("
275 + QString::number( ev.uid() ) 280 + QString::number( ev.uid() )
276 + "," 281 + ","
277 + QString::number( id++ ) 282 + QString::number( id++ )
278 + ",'" 283 + ",'"
279 + it.key() //.latin1() 284 + it.key() //.latin1()
280 + "'," 285 + "',"
281 + "0" // Priority for future enhancements 286 + "0" // Priority for future enhancements
282 + ",'" 287 + ",'"
283 + it.data() //.latin1() 288 + it.data() //.latin1()
284 + "');"; 289 + "');";
285 } 290 }
286 qWarning("add %s", qu.latin1() ); 291 owarn << "add " << qu << "" << oendl;
287 292
288 OSQLRawQuery raw( qu ); 293 OSQLRawQuery raw( qu );
289 OSQLResult res = m_driver->query( &raw ); 294 OSQLResult res = m_driver->query( &raw );
290 if ( res.state() != OSQLResult::Success ){ 295 if ( res.state() != OSQLResult::Success ){
291 return false; 296 return false;
292 } 297 }
293 298
294 // Update list of uid's 299 // Update list of uid's
295 update(); 300 update();
296 301
297 return true; 302 return true;
298} 303}
@@ -381,74 +386,74 @@ OPimEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats()
381 386
382QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const 387QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const
383{ 388{
384 389
385 QString qu = "SELECT uid FROM datebook WHERE ("; 390 QString qu = "SELECT uid FROM datebook WHERE (";
386 391
387 // Do it make sense to search other fields, too ? 392 // Do it make sense to search other fields, too ?
388 qu += " rlike(\""+ r.pattern() + "\", Location ) OR"; 393 qu += " rlike(\""+ r.pattern() + "\", Location ) OR";
389 qu += " rlike(\""+ r.pattern() + "\", Note )"; 394 qu += " rlike(\""+ r.pattern() + "\", Note )";
390 395
391 qu += " )"; 396 qu += " )";
392 397
393 qDebug( "query: %s", qu.latin1() ); 398 odebug << "query: " << qu << "" << oendl;
394 399
395 OSQLRawQuery raw( qu ); 400 OSQLRawQuery raw( qu );
396 OSQLResult res = m_driver->query( &raw ); 401 OSQLResult res = m_driver->query( &raw );
397 402
398 return extractUids( res ); 403 return extractUids( res );
399 404
400 405
401 406
402} 407}
403 408
404/* ===== Private Functions ========================================== */ 409/* ===== Private Functions ========================================== */
405 410
406QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const 411QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const
407{ 412{
408 qWarning("extractUids"); 413 owarn << "extractUids" << oendl;
409 QTime t; 414 QTime t;
410 t.start(); 415 t.start();
411 OSQLResultItem::ValueList list = res.results(); 416 OSQLResultItem::ValueList list = res.results();
412 OSQLResultItem::ValueList::Iterator it; 417 OSQLResultItem::ValueList::Iterator it;
413 QArray<int> ints(list.count() ); 418 QArray<int> ints(list.count() );
414 qWarning(" count = %d", list.count() ); 419 owarn << " count = " << list.count() << "" << oendl;
415 420
416 int i = 0; 421 int i = 0;
417 for (it = list.begin(); it != list.end(); ++it ) { 422 for (it = list.begin(); it != list.end(); ++it ) {
418 ints[i] = (*it).data("uid").toInt(); 423 ints[i] = (*it).data("uid").toInt();
419 i++; 424 i++;
420 } 425 }
421 qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() ); 426 owarn << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl;
422 427
423 return ints; 428 return ints;
424 429
425} 430}
426 431
427QMap<QString, QString> ODateBookAccessBackend_SQL::requestCustom( int uid ) const 432QMap<QString, QString> ODateBookAccessBackend_SQL::requestCustom( int uid ) const
428{ 433{
429 QTime t; 434 QTime t;
430 t.start(); 435 t.start();
431 436
432 QMap<QString, QString> customMap; 437 QMap<QString, QString> customMap;
433 438
434 FindCustomQuery query( uid ); 439 FindCustomQuery query( uid );
435 OSQLResult res_custom = m_driver->query( &query ); 440 OSQLResult res_custom = m_driver->query( &query );
436 441
437 if ( res_custom.state() == OSQLResult::Failure ) { 442 if ( res_custom.state() == OSQLResult::Failure ) {
438 qWarning("OSQLResult::Failure in find query !!"); 443 owarn << "OSQLResult::Failure in find query !!" << oendl;
439 QMap<QString, QString> empty; 444 QMap<QString, QString> empty;
440 return empty; 445 return empty;
441 } 446 }
442 447
443 OSQLResultItem::ValueList list = res_custom.results(); 448 OSQLResultItem::ValueList list = res_custom.results();
444 OSQLResultItem::ValueList::Iterator it = list.begin(); 449 OSQLResultItem::ValueList::Iterator it = list.begin();
445 for ( ; it != list.end(); ++it ) { 450 for ( ; it != list.end(); ++it ) {
446 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); 451 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) );
447 } 452 }
448 453
449 qDebug("RequestCustom needed: %d ms", t.elapsed() ); 454 odebug << "RequestCustom needed: " << t.elapsed() << " ms" << oendl;
450 return customMap; 455 return customMap;
451} 456}
452 457
453 458
454} 459}