summaryrefslogtreecommitdiff
path: root/libopie
Unidiff
Diffstat (limited to 'libopie') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie/pim/ocontactaccessbackend_sql.cpp5
-rw-r--r--libopie/pim/ocontactaccessbackend_sql.h10
-rw-r--r--libopie/pim/odatebookaccessbackend_sql.cpp4
-rw-r--r--libopie/pim/odatebookaccessbackend_sql.h7
-rw-r--r--libopie/pim/otodoaccesssql.cpp1
-rw-r--r--libopie/pim/otodoaccesssql.h13
6 files changed, 31 insertions, 9 deletions
diff --git a/libopie/pim/ocontactaccessbackend_sql.cpp b/libopie/pim/ocontactaccessbackend_sql.cpp
index a5be4c8..d20df56 100644
--- a/libopie/pim/ocontactaccessbackend_sql.cpp
+++ b/libopie/pim/ocontactaccessbackend_sql.cpp
@@ -1,572 +1,575 @@
1/* 1/*
2 * SQL Backend for the OPIE-Contact Database. 2 * SQL Backend for the OPIE-Contact Database.
3 * 3 *
4 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) 4 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de)
5 * 5 *
6 * ===================================================================== 6 * =====================================================================
7 *This program is free software; you can redistribute it and/or 7 *This program is free software; you can redistribute it and/or
8 *modify it under the terms of the GNU Library General Public 8 *modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
11 * ===================================================================== 11 * =====================================================================
12 * ===================================================================== 12 * =====================================================================
13 * Version: $Id$ 13 * Version: $Id$
14 * ===================================================================== 14 * =====================================================================
15 * History: 15 * History:
16 * $Log$ 16 * $Log$
17 * Revision 1.5 2004/03/14 13:50:35 alwin
18 * namespace correction
19 *
17 * Revision 1.4 2003/12/22 10:19:26 eilers 20 * Revision 1.4 2003/12/22 10:19:26 eilers
18 * Finishing implementation of sql-backend for datebook. But I have to 21 * Finishing implementation of sql-backend for datebook. But I have to
19 * port the PIM datebook application to use it, before I could debug the 22 * port the PIM datebook application to use it, before I could debug the
20 * whole stuff. 23 * whole stuff.
21 * Thus, PIM-Database backend is finished, but highly experimental. And some 24 * Thus, PIM-Database backend is finished, but highly experimental. And some
22 * parts are still generic. For instance, the "queryByExample()" methods are 25 * parts are still generic. For instance, the "queryByExample()" methods are
23 * not (or not fully) implemented. Todo: custom-entries not stored. 26 * not (or not fully) implemented. Todo: custom-entries not stored.
24 * The big show stopper: matchRegExp() (needed by OpieSearch) needs regular 27 * The big show stopper: matchRegExp() (needed by OpieSearch) needs regular
25 * expression search in the database, which is not supported by sqlite ! 28 * expression search in the database, which is not supported by sqlite !
26 * Therefore we need either an extended sqlite or a workaround which would 29 * Therefore we need either an extended sqlite or a workaround which would
27 * be very slow and memory consuming.. 30 * be very slow and memory consuming..
28 * 31 *
29 * Revision 1.3 2003/12/08 15:18:10 eilers 32 * Revision 1.3 2003/12/08 15:18:10 eilers
30 * Committing unfinished sql implementation before merging to libopie2 starts.. 33 * Committing unfinished sql implementation before merging to libopie2 starts..
31 * 34 *
32 * Revision 1.2 2003/09/29 07:44:26 eilers 35 * Revision 1.2 2003/09/29 07:44:26 eilers
33 * Improvement of PIM-SQL Databases, but search queries are still limited. 36 * Improvement of PIM-SQL Databases, but search queries are still limited.
34 * Addressbook: Changed table layout. Now, we just need 1/3 of disk-space. 37 * Addressbook: Changed table layout. Now, we just need 1/3 of disk-space.
35 * Todo: Started to add new attributes. Some type conversions missing. 38 * Todo: Started to add new attributes. Some type conversions missing.
36 * 39 *
37 * Revision 1.1 2003/09/22 14:31:16 eilers 40 * Revision 1.1 2003/09/22 14:31:16 eilers
38 * Added first experimental incarnation of sql-backend for addressbook. 41 * Added first experimental incarnation of sql-backend for addressbook.
39 * Some modifications to be able to compile the todo sql-backend. 42 * Some modifications to be able to compile the todo sql-backend.
40 * A lot of changes fill follow... 43 * A lot of changes fill follow...
41 * 44 *
42 */ 45 */
43 46
44#include "ocontactaccessbackend_sql.h" 47#include "ocontactaccessbackend_sql.h"
45 48
46#include <qarray.h> 49#include <qarray.h>
47#include <qdatetime.h> 50#include <qdatetime.h>
48#include <qstringlist.h> 51#include <qstringlist.h>
49 52
50#include <qpe/global.h> 53#include <qpe/global.h>
51#include <qpe/recordfields.h> 54#include <qpe/recordfields.h>
52 55
53#include <opie/ocontactfields.h> 56#include <opie/ocontactfields.h>
54#include <opie/oconversion.h> 57#include <opie/oconversion.h>
55#include <opie2/osqldriver.h> 58#include <opie2/osqldriver.h>
56#include <opie2/osqlresult.h> 59#include <opie2/osqlresult.h>
57#include <opie2/osqlmanager.h> 60#include <opie2/osqlmanager.h>
58#include <opie2/osqlquery.h> 61#include <opie2/osqlquery.h>
59 62
60 63using namespace Opie::DB;
61 64
62 65
63// If defined, we use a horizontal table ( uid, attr1, attr2, attr3, ..., attrn ) instead 66// If defined, we use a horizontal table ( uid, attr1, attr2, attr3, ..., attrn ) instead
64// vertical like "uid, type, value". 67// vertical like "uid, type, value".
65// DON'T DEACTIVATE THIS DEFINE IN PRODUCTIVE ENVIRONMENTS !! 68// DON'T DEACTIVATE THIS DEFINE IN PRODUCTIVE ENVIRONMENTS !!
66#define __STORE_HORIZONTAL_ 69#define __STORE_HORIZONTAL_
67 70
68// Distinct loading is not very fast. If I expect that every person has just 71// Distinct loading is not very fast. If I expect that every person has just
69// one (and always one) 'Last Name', I can request all uid's for existing lastnames, 72// one (and always one) 'Last Name', I can request all uid's for existing lastnames,
70// which is faster.. 73// which is faster..
71// But this may not be true for all entries, like company contacts.. 74// But this may not be true for all entries, like company contacts..
72// The current AddressBook application handles this problem, but other may not.. (eilers) 75// The current AddressBook application handles this problem, but other may not.. (eilers)
73#define __USE_SUPERFAST_LOADQUERY 76#define __USE_SUPERFAST_LOADQUERY
74 77
75 78
76/* 79/*
77 * Implementation of used query types 80 * Implementation of used query types
78 * CREATE query 81 * CREATE query
79 * LOAD query 82 * LOAD query
80 * INSERT 83 * INSERT
81 * REMOVE 84 * REMOVE
82 * CLEAR 85 * CLEAR
83 */ 86 */
84namespace { 87namespace {
85 /** 88 /**
86 * CreateQuery for the Todolist Table 89 * CreateQuery for the Todolist Table
87 */ 90 */
88 class CreateQuery : public OSQLQuery { 91 class CreateQuery : public OSQLQuery {
89 public: 92 public:
90 CreateQuery(); 93 CreateQuery();
91 ~CreateQuery(); 94 ~CreateQuery();
92 QString query()const; 95 QString query()const;
93 }; 96 };
94 97
95 /** 98 /**
96 * Clears (delete) a Table 99 * Clears (delete) a Table
97 */ 100 */
98 class ClearQuery : public OSQLQuery { 101 class ClearQuery : public OSQLQuery {
99 public: 102 public:
100 ClearQuery(); 103 ClearQuery();
101 ~ClearQuery(); 104 ~ClearQuery();
102 QString query()const; 105 QString query()const;
103 106
104 }; 107 };
105 108
106 109
107 /** 110 /**
108 * LoadQuery 111 * LoadQuery
109 * this one queries for all uids 112 * this one queries for all uids
110 */ 113 */
111 class LoadQuery : public OSQLQuery { 114 class LoadQuery : public OSQLQuery {
112 public: 115 public:
113 LoadQuery(); 116 LoadQuery();
114 ~LoadQuery(); 117 ~LoadQuery();
115 QString query()const; 118 QString query()const;
116 }; 119 };
117 120
118 /** 121 /**
119 * inserts/adds a OContact to the table 122 * inserts/adds a OContact to the table
120 */ 123 */
121 class InsertQuery : public OSQLQuery { 124 class InsertQuery : public OSQLQuery {
122 public: 125 public:
123 InsertQuery(const OContact& ); 126 InsertQuery(const OContact& );
124 ~InsertQuery(); 127 ~InsertQuery();
125 QString query()const; 128 QString query()const;
126 private: 129 private:
127 OContact m_contact; 130 OContact m_contact;
128 }; 131 };
129 132
130 133
131 /** 134 /**
132 * removes one from the table 135 * removes one from the table
133 */ 136 */
134 class RemoveQuery : public OSQLQuery { 137 class RemoveQuery : public OSQLQuery {
135 public: 138 public:
136 RemoveQuery(int uid ); 139 RemoveQuery(int uid );
137 ~RemoveQuery(); 140 ~RemoveQuery();
138 QString query()const; 141 QString query()const;
139 private: 142 private:
140 int m_uid; 143 int m_uid;
141 }; 144 };
142 145
143 /** 146 /**
144 * a find query for noncustom elements 147 * a find query for noncustom elements
145 */ 148 */
146 class FindQuery : public OSQLQuery { 149 class FindQuery : public OSQLQuery {
147 public: 150 public:
148 FindQuery(int uid); 151 FindQuery(int uid);
149 FindQuery(const QArray<int>& ); 152 FindQuery(const QArray<int>& );
150 ~FindQuery(); 153 ~FindQuery();
151 QString query()const; 154 QString query()const;
152 private: 155 private:
153 QString single()const; 156 QString single()const;
154 QString multi()const; 157 QString multi()const;
155 QArray<int> m_uids; 158 QArray<int> m_uids;
156 int m_uid; 159 int m_uid;
157 }; 160 };
158 161
159 /** 162 /**
160 * a find query for custom elements 163 * a find query for custom elements
161 */ 164 */
162 class FindCustomQuery : public OSQLQuery { 165 class FindCustomQuery : public OSQLQuery {
163 public: 166 public:
164 FindCustomQuery(int uid); 167 FindCustomQuery(int uid);
165 FindCustomQuery(const QArray<int>& ); 168 FindCustomQuery(const QArray<int>& );
166 ~FindCustomQuery(); 169 ~FindCustomQuery();
167 QString query()const; 170 QString query()const;
168 private: 171 private:
169 QString single()const; 172 QString single()const;
170 QString multi()const; 173 QString multi()const;
171 QArray<int> m_uids; 174 QArray<int> m_uids;
172 int m_uid; 175 int m_uid;
173 }; 176 };
174 177
175 178
176 179
177 // We using three tables to store the information: 180 // We using three tables to store the information:
178 // 1. addressbook : It contains General information about the contact (non custom) 181 // 1. addressbook : It contains General information about the contact (non custom)
179 // 2. custom_data : Not official supported entries 182 // 2. custom_data : Not official supported entries
180 // All tables are connected by the uid of the contact. 183 // All tables are connected by the uid of the contact.
181 // Maybe I should add a table for meta-information ? 184 // Maybe I should add a table for meta-information ?
182 CreateQuery::CreateQuery() : OSQLQuery() {} 185 CreateQuery::CreateQuery() : OSQLQuery() {}
183 CreateQuery::~CreateQuery() {} 186 CreateQuery::~CreateQuery() {}
184 QString CreateQuery::query()const { 187 QString CreateQuery::query()const {
185 QString qu; 188 QString qu;
186#ifdef __STORE_HORIZONTAL_ 189#ifdef __STORE_HORIZONTAL_
187 190
188 qu += "create table addressbook( uid PRIMARY KEY "; 191 qu += "create table addressbook( uid PRIMARY KEY ";
189 192
190 QStringList fieldList = OContactFields::untrfields( false ); 193 QStringList fieldList = OContactFields::untrfields( false );
191 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 194 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
192 qu += QString( ",\"%1\" VARCHAR(10)" ).arg( *it ); 195 qu += QString( ",\"%1\" VARCHAR(10)" ).arg( *it );
193 } 196 }
194 qu += " );"; 197 qu += " );";
195 198
196 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );"; 199 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );";
197 200
198#else 201#else
199 202
200 qu += "create table addressbook( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id));"; 203 qu += "create table addressbook( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id));";
201 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );"; 204 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );";
202 // qu += "create table dates( uid PRIMARY KEY, type, day, month, year, hour, minute, second );"; 205 // qu += "create table dates( uid PRIMARY KEY, type, day, month, year, hour, minute, second );";
203 206
204#endif // __STORE_HORIZONTAL_ 207#endif // __STORE_HORIZONTAL_
205 return qu; 208 return qu;
206 } 209 }
207 210
208 ClearQuery::ClearQuery() 211 ClearQuery::ClearQuery()
209 : OSQLQuery() {} 212 : OSQLQuery() {}
210 ClearQuery::~ClearQuery() {} 213 ClearQuery::~ClearQuery() {}
211 QString ClearQuery::query()const { 214 QString ClearQuery::query()const {
212 QString qu = "drop table addressbook;"; 215 QString qu = "drop table addressbook;";
213 qu += "drop table custom_data;"; 216 qu += "drop table custom_data;";
214 // qu += "drop table dates;"; 217 // qu += "drop table dates;";
215 return qu; 218 return qu;
216 } 219 }
217 220
218 221
219 LoadQuery::LoadQuery() : OSQLQuery() {} 222 LoadQuery::LoadQuery() : OSQLQuery() {}
220 LoadQuery::~LoadQuery() {} 223 LoadQuery::~LoadQuery() {}
221 QString LoadQuery::query()const { 224 QString LoadQuery::query()const {
222 QString qu; 225 QString qu;
223#ifdef __STORE_HORIZONTAL_ 226#ifdef __STORE_HORIZONTAL_
224 qu += "select uid from addressbook"; 227 qu += "select uid from addressbook";
225#else 228#else
226# ifndef __USE_SUPERFAST_LOADQUERY 229# ifndef __USE_SUPERFAST_LOADQUERY
227 qu += "select distinct uid from addressbook"; 230 qu += "select distinct uid from addressbook";
228# else 231# else
229 qu += "select uid from addressbook where type = 'Last Name'"; 232 qu += "select uid from addressbook where type = 'Last Name'";
230# endif // __USE_SUPERFAST_LOADQUERY 233# endif // __USE_SUPERFAST_LOADQUERY
231#endif // __STORE_HORIZONTAL_ 234#endif // __STORE_HORIZONTAL_
232 235
233 return qu; 236 return qu;
234 } 237 }
235 238
236 239
237 InsertQuery::InsertQuery( const OContact& contact ) 240 InsertQuery::InsertQuery( const OContact& contact )
238 : OSQLQuery(), m_contact( contact ) { 241 : OSQLQuery(), m_contact( contact ) {
239 } 242 }
240 243
241 InsertQuery::~InsertQuery() { 244 InsertQuery::~InsertQuery() {
242 } 245 }
243 246
244 /* 247 /*
245 * converts from a OContact to a query 248 * converts from a OContact to a query
246 */ 249 */
247 QString InsertQuery::query()const{ 250 QString InsertQuery::query()const{
248 251
249#ifdef __STORE_HORIZONTAL_ 252#ifdef __STORE_HORIZONTAL_
250 QString qu; 253 QString qu;
251 qu += "insert into addressbook VALUES( " + 254 qu += "insert into addressbook VALUES( " +
252 QString::number( m_contact.uid() ); 255 QString::number( m_contact.uid() );
253 256
254 // Get all information out of the contact-class 257 // Get all information out of the contact-class
255 // Remember: The category is stored in contactMap, too ! 258 // Remember: The category is stored in contactMap, too !
256 QMap<int, QString> contactMap = m_contact.toMap(); 259 QMap<int, QString> contactMap = m_contact.toMap();
257 260
258 QStringList fieldList = OContactFields::untrfields( false ); 261 QStringList fieldList = OContactFields::untrfields( false );
259 QMap<QString, int> translate = OContactFields::untrFieldsToId(); 262 QMap<QString, int> translate = OContactFields::untrFieldsToId();
260 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 263 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
261 // Convert Column-String to Id and get value for this id.. 264 // Convert Column-String to Id and get value for this id..
262 // Hmmm.. Maybe not very cute solution.. 265 // Hmmm.. Maybe not very cute solution..
263 int id = translate[*it]; 266 int id = translate[*it];
264 switch ( id ){ 267 switch ( id ){
265 case Qtopia::Birthday:{ 268 case Qtopia::Birthday:{
266 // These entries should stored in a special format 269 // These entries should stored in a special format
267 // year-month-day 270 // year-month-day
268 QDate day = m_contact.birthday(); 271 QDate day = m_contact.birthday();
269 if ( day.isValid() ){ 272 if ( day.isValid() ){
270 qu += QString(",\"%1-%2-%3\"") 273 qu += QString(",\"%1-%2-%3\"")
271 .arg( day.year() ) 274 .arg( day.year() )
272 .arg( day.month() ) 275 .arg( day.month() )
273 .arg( day.day() ); 276 .arg( day.day() );
274 } else { 277 } else {
275 qu += ",\"\""; 278 qu += ",\"\"";
276 } 279 }
277 } 280 }
278 break; 281 break;
279 case Qtopia::Anniversary:{ 282 case Qtopia::Anniversary:{
280 // These entries should stored in a special format 283 // These entries should stored in a special format
281 // year-month-day 284 // year-month-day
282 QDate day = m_contact.anniversary(); 285 QDate day = m_contact.anniversary();
283 if ( day.isValid() ){ 286 if ( day.isValid() ){
284 qu += QString(",\"%1-%2-%3\"") 287 qu += QString(",\"%1-%2-%3\"")
285 .arg( day.year() ) 288 .arg( day.year() )
286 .arg( day.month() ) 289 .arg( day.month() )
287 .arg( day.day() ); 290 .arg( day.day() );
288 } else { 291 } else {
289 qu += ",\"\""; 292 qu += ",\"\"";
290 } 293 }
291 } 294 }
292 break; 295 break;
293 296
294 default: 297 default:
295 qu += QString( ",\"%1\"" ).arg( contactMap[id] ); 298 qu += QString( ",\"%1\"" ).arg( contactMap[id] );
296 } 299 }
297 } 300 }
298 qu += " );"; 301 qu += " );";
299 302
300 303
301#else 304#else
302 // Get all information out of the contact-class 305 // Get all information out of the contact-class
303 // Remember: The category is stored in contactMap, too ! 306 // Remember: The category is stored in contactMap, too !
304 QMap<int, QString> contactMap = m_contact.toMap(); 307 QMap<int, QString> contactMap = m_contact.toMap();
305 308
306 QMap<QString, QString> addressbook_db; 309 QMap<QString, QString> addressbook_db;
307 310
308 // Get the translation from the ID to the String 311 // Get the translation from the ID to the String
309 QMap<int, QString> transMap = OContactFields::idToUntrFields(); 312 QMap<int, QString> transMap = OContactFields::idToUntrFields();
310 313
311 for( QMap<int, QString>::Iterator it = contactMap.begin(); 314 for( QMap<int, QString>::Iterator it = contactMap.begin();
312 it != contactMap.end(); ++it ){ 315 it != contactMap.end(); ++it ){
313 switch ( it.key() ){ 316 switch ( it.key() ){
314 case Qtopia::Birthday:{ 317 case Qtopia::Birthday:{
315 // These entries should stored in a special format 318 // These entries should stored in a special format
316 // year-month-day 319 // year-month-day
317 QDate day = m_contact.birthday(); 320 QDate day = m_contact.birthday();
318 addressbook_db.insert( transMap[it.key()], 321 addressbook_db.insert( transMap[it.key()],
319 QString("%1-%2-%3") 322 QString("%1-%2-%3")
320 .arg( day.year() ) 323 .arg( day.year() )
321 .arg( day.month() ) 324 .arg( day.month() )
322 .arg( day.day() ) ); 325 .arg( day.day() ) );
323 } 326 }
324 break; 327 break;
325 case Qtopia::Anniversary:{ 328 case Qtopia::Anniversary:{
326 // These entries should stored in a special format 329 // These entries should stored in a special format
327 // year-month-day 330 // year-month-day
328 QDate day = m_contact.anniversary(); 331 QDate day = m_contact.anniversary();
329 addressbook_db.insert( transMap[it.key()], 332 addressbook_db.insert( transMap[it.key()],
330 QString("%1-%2-%3") 333 QString("%1-%2-%3")
331 .arg( day.year() ) 334 .arg( day.year() )
332 .arg( day.month() ) 335 .arg( day.month() )
333 .arg( day.day() ) ); 336 .arg( day.day() ) );
334 } 337 }
335 break; 338 break;
336 case Qtopia::AddressUid: // Ignore UID 339 case Qtopia::AddressUid: // Ignore UID
337 break; 340 break;
338 default: // Translate id to String 341 default: // Translate id to String
339 addressbook_db.insert( transMap[it.key()], it.data() ); 342 addressbook_db.insert( transMap[it.key()], it.data() );
340 break; 343 break;
341 } 344 }
342 345
343 } 346 }
344 347
345 // Now convert this whole stuff into a SQL String, beginning with 348 // Now convert this whole stuff into a SQL String, beginning with
346 // the addressbook table.. 349 // the addressbook table..
347 QString qu; 350 QString qu;
348 // qu += "begin transaction;"; 351 // qu += "begin transaction;";
349 int id = 0; 352 int id = 0;
350 for( QMap<QString, QString>::Iterator it = addressbook_db.begin(); 353 for( QMap<QString, QString>::Iterator it = addressbook_db.begin();
351 it != addressbook_db.end(); ++it ){ 354 it != addressbook_db.end(); ++it ){
352 qu += "insert into addressbook VALUES(" 355 qu += "insert into addressbook VALUES("
353 + QString::number( m_contact.uid() ) 356 + QString::number( m_contact.uid() )
354 + "," 357 + ","
355 + QString::number( id++ ) 358 + QString::number( id++ )
356 + ",'" 359 + ",'"
357 + it.key() //.latin1() 360 + it.key() //.latin1()
358 + "'," 361 + "',"
359 + "0" // Priority for future enhancements 362 + "0" // Priority for future enhancements
360 + ",'" 363 + ",'"
361 + it.data() //.latin1() 364 + it.data() //.latin1()
362 + "');"; 365 + "');";
363 } 366 }
364 367
365 #endif //__STORE_HORIZONTAL_ 368 #endif //__STORE_HORIZONTAL_
366 // Now add custom data.. 369 // Now add custom data..
367#ifdef __STORE_HORIZONTAL_ 370#ifdef __STORE_HORIZONTAL_
368 int id = 0; 371 int id = 0;
369#endif 372#endif
370 id = 0; 373 id = 0;
371 QMap<QString, QString> customMap = m_contact.toExtraMap(); 374 QMap<QString, QString> customMap = m_contact.toExtraMap();
372 for( QMap<QString, QString>::Iterator it = customMap.begin(); 375 for( QMap<QString, QString>::Iterator it = customMap.begin();
373 it != customMap.end(); ++it ){ 376 it != customMap.end(); ++it ){
374 qu += "insert into custom_data VALUES(" 377 qu += "insert into custom_data VALUES("
375 + QString::number( m_contact.uid() ) 378 + QString::number( m_contact.uid() )
376 + "," 379 + ","
377 + QString::number( id++ ) 380 + QString::number( id++ )
378 + ",'" 381 + ",'"
379 + it.key() //.latin1() 382 + it.key() //.latin1()
380 + "'," 383 + "',"
381 + "0" // Priority for future enhancements 384 + "0" // Priority for future enhancements
382 + ",'" 385 + ",'"
383 + it.data() //.latin1() 386 + it.data() //.latin1()
384 + "');"; 387 + "');";
385 } 388 }
386 // qu += "commit;"; 389 // qu += "commit;";
387 qWarning("add %s", qu.latin1() ); 390 qWarning("add %s", qu.latin1() );
388 return qu; 391 return qu;
389 } 392 }
390 393
391 394
392 RemoveQuery::RemoveQuery(int uid ) 395 RemoveQuery::RemoveQuery(int uid )
393 : OSQLQuery(), m_uid( uid ) {} 396 : OSQLQuery(), m_uid( uid ) {}
394 RemoveQuery::~RemoveQuery() {} 397 RemoveQuery::~RemoveQuery() {}
395 QString RemoveQuery::query()const { 398 QString RemoveQuery::query()const {
396 QString qu = "DELETE from addressbook where uid = " 399 QString qu = "DELETE from addressbook where uid = "
397 + QString::number(m_uid) + ";"; 400 + QString::number(m_uid) + ";";
398 qu += "DELETE from custom_data where uid = " 401 qu += "DELETE from custom_data where uid = "
399 + QString::number(m_uid) + ";"; 402 + QString::number(m_uid) + ";";
400 return qu; 403 return qu;
401 } 404 }
402 405
403 406
404 407
405 408
406 FindQuery::FindQuery(int uid) 409 FindQuery::FindQuery(int uid)
407 : OSQLQuery(), m_uid( uid ) { 410 : OSQLQuery(), m_uid( uid ) {
408 } 411 }
409 FindQuery::FindQuery(const QArray<int>& ints) 412 FindQuery::FindQuery(const QArray<int>& ints)
410 : OSQLQuery(), m_uids( ints ){ 413 : OSQLQuery(), m_uids( ints ){
411 } 414 }
412 FindQuery::~FindQuery() { 415 FindQuery::~FindQuery() {
413 } 416 }
414 QString FindQuery::query()const{ 417 QString FindQuery::query()const{
415 // if ( m_uids.count() == 0 ) 418 // if ( m_uids.count() == 0 )
416 return single(); 419 return single();
417 } 420 }
418 /* 421 /*
419 else 422 else
420 return multi(); 423 return multi();
421 } 424 }
422 QString FindQuery::multi()const { 425 QString FindQuery::multi()const {
423 QString qu = "select uid, type, value from addressbook where"; 426 QString qu = "select uid, type, value from addressbook where";
424 for (uint i = 0; i < m_uids.count(); i++ ) { 427 for (uint i = 0; i < m_uids.count(); i++ ) {
425 qu += " UID = " + QString::number( m_uids[i] ) + " OR"; 428 qu += " UID = " + QString::number( m_uids[i] ) + " OR";
426 } 429 }
427 qu.remove( qu.length()-2, 2 ); // Hmmmm.. 430 qu.remove( qu.length()-2, 2 ); // Hmmmm..
428 return qu; 431 return qu;
429 } 432 }
430 */ 433 */
431#ifdef __STORE_HORIZONTAL_ 434#ifdef __STORE_HORIZONTAL_
432 QString FindQuery::single()const{ 435 QString FindQuery::single()const{
433 QString qu = "select *"; 436 QString qu = "select *";
434 qu += " from addressbook where uid = " + QString::number(m_uid); 437 qu += " from addressbook where uid = " + QString::number(m_uid);
435 438
436 // qWarning("find query: %s", qu.latin1() ); 439 // qWarning("find query: %s", qu.latin1() );
437 return qu; 440 return qu;
438 } 441 }
439#else 442#else
440 QString FindQuery::single()const{ 443 QString FindQuery::single()const{
441 QString qu = "select uid, type, value from addressbook where uid = "; 444 QString qu = "select uid, type, value from addressbook where uid = ";
442 qu += QString::number(m_uid); 445 qu += QString::number(m_uid);
443 return qu; 446 return qu;
444 } 447 }
445#endif 448#endif
446 449
447 450
448 FindCustomQuery::FindCustomQuery(int uid) 451 FindCustomQuery::FindCustomQuery(int uid)
449 : OSQLQuery(), m_uid( uid ) { 452 : OSQLQuery(), m_uid( uid ) {
450 } 453 }
451 FindCustomQuery::FindCustomQuery(const QArray<int>& ints) 454 FindCustomQuery::FindCustomQuery(const QArray<int>& ints)
452 : OSQLQuery(), m_uids( ints ){ 455 : OSQLQuery(), m_uids( ints ){
453 } 456 }
454 FindCustomQuery::~FindCustomQuery() { 457 FindCustomQuery::~FindCustomQuery() {
455 } 458 }
456 QString FindCustomQuery::query()const{ 459 QString FindCustomQuery::query()const{
457 // if ( m_uids.count() == 0 ) 460 // if ( m_uids.count() == 0 )
458 return single(); 461 return single();
459 } 462 }
460 QString FindCustomQuery::single()const{ 463 QString FindCustomQuery::single()const{
461 QString qu = "select uid, type, value from custom_data where uid = "; 464 QString qu = "select uid, type, value from custom_data where uid = ";
462 qu += QString::number(m_uid); 465 qu += QString::number(m_uid);
463 return qu; 466 return qu;
464 } 467 }
465 468
466}; 469};
467 470
468 471
469/* --------------------------------------------------------------------------- */ 472/* --------------------------------------------------------------------------- */
470 473
471OContactAccessBackend_SQL::OContactAccessBackend_SQL ( const QString& /* appname */, 474OContactAccessBackend_SQL::OContactAccessBackend_SQL ( const QString& /* appname */,
472 const QString& filename ): 475 const QString& filename ):
473 OContactAccessBackend(), m_changed(false), m_driver( NULL ) 476 OContactAccessBackend(), m_changed(false), m_driver( NULL )
474{ 477{
475 qWarning("C'tor OContactAccessBackend_SQL starts"); 478 qWarning("C'tor OContactAccessBackend_SQL starts");
476 QTime t; 479 QTime t;
477 t.start(); 480 t.start();
478 481
479 /* Expecting to access the default filename if nothing else is set */ 482 /* Expecting to access the default filename if nothing else is set */
480 if ( filename.isEmpty() ){ 483 if ( filename.isEmpty() ){
481 m_fileName = Global::applicationFileName( "addressbook","addressbook.db" ); 484 m_fileName = Global::applicationFileName( "addressbook","addressbook.db" );
482 } else 485 } else
483 m_fileName = filename; 486 m_fileName = filename;
484 487
485 // Get the standart sql-driver from the OSQLManager.. 488 // Get the standart sql-driver from the OSQLManager..
486 OSQLManager man; 489 OSQLManager man;
487 m_driver = man.standard(); 490 m_driver = man.standard();
488 m_driver->setUrl( m_fileName ); 491 m_driver->setUrl( m_fileName );
489 492
490 load(); 493 load();
491 494
492 qWarning("C'tor OContactAccessBackend_SQL ends: %d ms", t.elapsed() ); 495 qWarning("C'tor OContactAccessBackend_SQL ends: %d ms", t.elapsed() );
493} 496}
494 497
495OContactAccessBackend_SQL::~OContactAccessBackend_SQL () 498OContactAccessBackend_SQL::~OContactAccessBackend_SQL ()
496{ 499{
497 if( m_driver ) 500 if( m_driver )
498 delete m_driver; 501 delete m_driver;
499} 502}
500 503
501bool OContactAccessBackend_SQL::load () 504bool OContactAccessBackend_SQL::load ()
502{ 505{
503 if (!m_driver->open() ) 506 if (!m_driver->open() )
504 return false; 507 return false;
505 508
506 // Don't expect that the database exists. 509 // Don't expect that the database exists.
507 // It is save here to create the table, even if it 510 // It is save here to create the table, even if it
508 // do exist. ( Is that correct for all databases ?? ) 511 // do exist. ( Is that correct for all databases ?? )
509 CreateQuery creat; 512 CreateQuery creat;
510 OSQLResult res = m_driver->query( &creat ); 513 OSQLResult res = m_driver->query( &creat );
511 514
512 update(); 515 update();
513 516
514 return true; 517 return true;
515 518
516} 519}
517 520
518bool OContactAccessBackend_SQL::reload() 521bool OContactAccessBackend_SQL::reload()
519{ 522{
520 return load(); 523 return load();
521} 524}
522 525
523bool OContactAccessBackend_SQL::save() 526bool OContactAccessBackend_SQL::save()
524{ 527{
525 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) 528 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers)
526} 529}
527 530
528 531
529void OContactAccessBackend_SQL::clear () 532void OContactAccessBackend_SQL::clear ()
530{ 533{
531 ClearQuery cle; 534 ClearQuery cle;
532 OSQLResult res = m_driver->query( &cle ); 535 OSQLResult res = m_driver->query( &cle );
533 536
534 reload(); 537 reload();
535} 538}
536 539
537bool OContactAccessBackend_SQL::wasChangedExternally() 540bool OContactAccessBackend_SQL::wasChangedExternally()
538{ 541{
539 return false; 542 return false;
540} 543}
541 544
542QArray<int> OContactAccessBackend_SQL::allRecords() const 545QArray<int> OContactAccessBackend_SQL::allRecords() const
543{ 546{
544 547
545 // FIXME: Think about cute handling of changed tables.. 548 // FIXME: Think about cute handling of changed tables..
546 // Thus, we don't have to call update here... 549 // Thus, we don't have to call update here...
547 if ( m_changed ) 550 if ( m_changed )
548 ((OContactAccessBackend_SQL*)this)->update(); 551 ((OContactAccessBackend_SQL*)this)->update();
549 552
550 return m_uids; 553 return m_uids;
551} 554}
552 555
553bool OContactAccessBackend_SQL::add ( const OContact &newcontact ) 556bool OContactAccessBackend_SQL::add ( const OContact &newcontact )
554{ 557{
555 InsertQuery ins( newcontact ); 558 InsertQuery ins( newcontact );
556 OSQLResult res = m_driver->query( &ins ); 559 OSQLResult res = m_driver->query( &ins );
557 560
558 if ( res.state() == OSQLResult::Failure ) 561 if ( res.state() == OSQLResult::Failure )
559 return false; 562 return false;
560 563
561 int c = m_uids.count(); 564 int c = m_uids.count();
562 m_uids.resize( c+1 ); 565 m_uids.resize( c+1 );
563 m_uids[c] = newcontact.uid(); 566 m_uids[c] = newcontact.uid();
564 567
565 return true; 568 return true;
566} 569}
567 570
568 571
569bool OContactAccessBackend_SQL::remove ( int uid ) 572bool OContactAccessBackend_SQL::remove ( int uid )
570{ 573{
571 RemoveQuery rem( uid ); 574 RemoveQuery rem( uid );
572 OSQLResult res = m_driver->query(&rem ); 575 OSQLResult res = m_driver->query(&rem );
diff --git a/libopie/pim/ocontactaccessbackend_sql.h b/libopie/pim/ocontactaccessbackend_sql.h
index b8f1d8d..f553760 100644
--- a/libopie/pim/ocontactaccessbackend_sql.h
+++ b/libopie/pim/ocontactaccessbackend_sql.h
@@ -1,101 +1,107 @@
1/* 1/*
2 * SQL Backend for the OPIE-Contact Database. 2 * SQL Backend for the OPIE-Contact Database.
3 * 3 *
4 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) 4 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de)
5 * 5 *
6 * ===================================================================== 6 * =====================================================================
7 *This program is free software; you can redistribute it and/or 7 *This program is free software; you can redistribute it and/or
8 *modify it under the terms of the GNU Library General Public 8 *modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
11 * ===================================================================== 11 * =====================================================================
12 * 12 *
13 * 13 *
14 * ===================================================================== 14 * =====================================================================
15 * Version: $Id$ 15 * Version: $Id$
16 * ===================================================================== 16 * =====================================================================
17 * History: 17 * History:
18 * $Log$ 18 * $Log$
19 * Revision 1.3 2004/03/14 13:50:35 alwin
20 * namespace correction
21 *
19 * Revision 1.2 2003/12/08 15:18:11 eilers 22 * Revision 1.2 2003/12/08 15:18:11 eilers
20 * Committing unfinished sql implementation before merging to libopie2 starts.. 23 * Committing unfinished sql implementation before merging to libopie2 starts..
21 * 24 *
22 * Revision 1.1 2003/09/22 14:31:16 eilers 25 * Revision 1.1 2003/09/22 14:31:16 eilers
23 * Added first experimental incarnation of sql-backend for addressbook. 26 * Added first experimental incarnation of sql-backend for addressbook.
24 * Some modifications to be able to compile the todo sql-backend. 27 * Some modifications to be able to compile the todo sql-backend.
25 * A lot of changes fill follow... 28 * A lot of changes fill follow...
26 * 29 *
27 * 30 *
28 */ 31 */
29 32
30#ifndef _OContactAccessBackend_SQL_ 33#ifndef _OContactAccessBackend_SQL_
31#define _OContactAccessBackend_SQL_ 34#define _OContactAccessBackend_SQL_
32 35
33#include "ocontactaccessbackend.h" 36#include "ocontactaccessbackend.h"
34#include "ocontactaccess.h" 37#include "ocontactaccess.h"
35 38
36#include <qlist.h> 39#include <qlist.h>
37#include <qdict.h> 40#include <qdict.h>
38 41
42namespace Opie { namespace DB {
39class OSQLDriver; 43class OSQLDriver;
40class OSQLResult; 44class OSQLResult;
41class OSQLResultItem; 45class OSQLResultItem;
42 46
47}}
48
43/* the default xml implementation */ 49/* the default xml implementation */
44/** 50/**
45 * This class is the SQL implementation of a Contact backend 51 * This class is the SQL implementation of a Contact backend
46 * it does implement everything available for OContact. 52 * it does implement everything available for OContact.
47 * @see OPimAccessBackend for more information of available methods 53 * @see OPimAccessBackend for more information of available methods
48 */ 54 */
49class OContactAccessBackend_SQL : public OContactAccessBackend { 55class OContactAccessBackend_SQL : public OContactAccessBackend {
50 public: 56 public:
51 OContactAccessBackend_SQL ( const QString& appname, const QString& filename = QString::null ); 57 OContactAccessBackend_SQL ( const QString& appname, const QString& filename = QString::null );
52 58
53 ~OContactAccessBackend_SQL (); 59 ~OContactAccessBackend_SQL ();
54 60
55 bool save(); 61 bool save();
56 62
57 bool load (); 63 bool load ();
58 64
59 void clear (); 65 void clear ();
60 66
61 bool wasChangedExternally(); 67 bool wasChangedExternally();
62 68
63 QArray<int> allRecords() const; 69 QArray<int> allRecords() const;
64 70
65 OContact find ( int uid ) const; 71 OContact find ( int uid ) const;
66 // FIXME: Add lookahead-cache support ! 72 // FIXME: Add lookahead-cache support !
67 //OContact find(int uid, const QArray<int>&, uint cur, Frontend::CacheDirection )const; 73 //OContact find(int uid, const QArray<int>&, uint cur, Frontend::CacheDirection )const;
68 74
69 QArray<int> queryByExample ( const OContact &query, int settings, 75 QArray<int> queryByExample ( const OContact &query, int settings,
70 const QDateTime& d ); 76 const QDateTime& d );
71 77
72 QArray<int> matchRegexp( const QRegExp &r ) const; 78 QArray<int> matchRegexp( const QRegExp &r ) const;
73 79
74 const uint querySettings(); 80 const uint querySettings();
75 81
76 bool hasQuerySettings (uint querySettings) const; 82 bool hasQuerySettings (uint querySettings) const;
77 83
78 // Currently only asc implemented.. 84 // Currently only asc implemented..
79 QArray<int> sorted( bool asc, int , int , int ); 85 QArray<int> sorted( bool asc, int , int , int );
80 bool add ( const OContact &newcontact ); 86 bool add ( const OContact &newcontact );
81 87
82 bool replace ( const OContact &contact ); 88 bool replace ( const OContact &contact );
83 89
84 bool remove ( int uid ); 90 bool remove ( int uid );
85 bool reload(); 91 bool reload();
86 92
87 private: 93 private:
88 QArray<int> extractUids( OSQLResult& res ) const; 94 QArray<int> extractUids( Opie::DB::OSQLResult& res ) const;
89 QMap<int, QString> requestNonCustom( int uid ) const; 95 QMap<int, QString> requestNonCustom( int uid ) const;
90 QMap<QString, QString> requestCustom( int uid ) const; 96 QMap<QString, QString> requestCustom( int uid ) const;
91 void update(); 97 void update();
92 98
93 protected: 99 protected:
94 bool m_changed; 100 bool m_changed;
95 QString m_fileName; 101 QString m_fileName;
96 QArray<int> m_uids; 102 QArray<int> m_uids;
97 103
98 OSQLDriver* m_driver; 104 Opie::DB::OSQLDriver* m_driver;
99}; 105};
100 106
101#endif 107#endif
diff --git a/libopie/pim/odatebookaccessbackend_sql.cpp b/libopie/pim/odatebookaccessbackend_sql.cpp
index 756f405..44dd2bc 100644
--- a/libopie/pim/odatebookaccessbackend_sql.cpp
+++ b/libopie/pim/odatebookaccessbackend_sql.cpp
@@ -1,367 +1,371 @@
1/* 1/*
2 * SQL Backend for the OPIE-Calender Database. 2 * SQL Backend for the OPIE-Calender Database.
3 * 3 *
4 * Copyright (c) 2003 by Stefan Eilers (Eilers.Stefan@epost.de) 4 * Copyright (c) 2003 by Stefan Eilers (Eilers.Stefan@epost.de)
5 * 5 *
6 * ===================================================================== 6 * =====================================================================
7 *This program is free software; you can redistribute it and/or 7 *This program is free software; you can redistribute it and/or
8 *modify it under the terms of the GNU Library General Public 8 *modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
11 * ===================================================================== 11 * =====================================================================
12 * ===================================================================== 12 * =====================================================================
13 * Version: $Id$ 13 * Version: $Id$
14 * ===================================================================== 14 * =====================================================================
15 * History: 15 * History:
16 * $Log$ 16 * $Log$
17 * Revision 1.4 2004/03/14 13:50:35 alwin
18 * namespace correction
19 *
17 * Revision 1.3 2003/12/22 11:41:39 eilers 20 * Revision 1.3 2003/12/22 11:41:39 eilers
18 * Fixing stupid bug, found by sourcode review.. 21 * Fixing stupid bug, found by sourcode review..
19 * 22 *
20 * Revision 1.2 2003/12/22 10:19:26 eilers 23 * Revision 1.2 2003/12/22 10:19:26 eilers
21 * Finishing implementation of sql-backend for datebook. But I have to 24 * Finishing implementation of sql-backend for datebook. But I have to
22 * port the PIM datebook application to use it, before I could debug the 25 * port the PIM datebook application to use it, before I could debug the
23 * whole stuff. 26 * whole stuff.
24 * Thus, PIM-Database backend is finished, but highly experimental. And some 27 * Thus, PIM-Database backend is finished, but highly experimental. And some
25 * parts are still generic. For instance, the "queryByExample()" methods are 28 * parts are still generic. For instance, the "queryByExample()" methods are
26 * not (or not fully) implemented. Todo: custom-entries not stored. 29 * not (or not fully) implemented. Todo: custom-entries not stored.
27 * The big show stopper: matchRegExp() (needed by OpieSearch) needs regular 30 * The big show stopper: matchRegExp() (needed by OpieSearch) needs regular
28 * expression search in the database, which is not supported by sqlite ! 31 * expression search in the database, which is not supported by sqlite !
29 * Therefore we need either an extended sqlite or a workaround which would 32 * Therefore we need either an extended sqlite or a workaround which would
30 * be very slow and memory consuming.. 33 * be very slow and memory consuming..
31 * 34 *
32 * Revision 1.1 2003/12/08 15:18:12 eilers 35 * Revision 1.1 2003/12/08 15:18:12 eilers
33 * Committing unfinished sql implementation before merging to libopie2 starts.. 36 * Committing unfinished sql implementation before merging to libopie2 starts..
34 * 37 *
35 * 38 *
36 */ 39 */
37 40
38#include <stdio.h> 41#include <stdio.h>
39#include <stdlib.h> 42#include <stdlib.h>
40 43
41#include <qarray.h> 44#include <qarray.h>
42#include <qstringlist.h> 45#include <qstringlist.h>
43 46
44#include <qpe/global.h> 47#include <qpe/global.h>
45 48
46#include <opie2/osqldriver.h> 49#include <opie2/osqldriver.h>
47#include <opie2/osqlmanager.h> 50#include <opie2/osqlmanager.h>
48#include <opie2/osqlquery.h> 51#include <opie2/osqlquery.h>
49 52
50#include "orecur.h" 53#include "orecur.h"
51#include "odatebookaccessbackend_sql.h" 54#include "odatebookaccessbackend_sql.h"
52 55
56using namespace Opie::DB;
53 57
54 58
55ODateBookAccessBackend_SQL::ODateBookAccessBackend_SQL( const QString& , 59ODateBookAccessBackend_SQL::ODateBookAccessBackend_SQL( const QString& ,
56 const QString& fileName ) 60 const QString& fileName )
57 : ODateBookAccessBackend(), m_driver( NULL ) 61 : ODateBookAccessBackend(), m_driver( NULL )
58{ 62{
59 m_fileName = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.db" ) : fileName; 63 m_fileName = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.db" ) : fileName;
60 64
61 // Get the standart sql-driver from the OSQLManager.. 65 // Get the standart sql-driver from the OSQLManager..
62 OSQLManager man; 66 OSQLManager man;
63 m_driver = man.standard(); 67 m_driver = man.standard();
64 m_driver->setUrl( m_fileName ); 68 m_driver->setUrl( m_fileName );
65 69
66 initFields(); 70 initFields();
67 71
68 load(); 72 load();
69} 73}
70 74
71ODateBookAccessBackend_SQL::~ODateBookAccessBackend_SQL() { 75ODateBookAccessBackend_SQL::~ODateBookAccessBackend_SQL() {
72 if( m_driver ) 76 if( m_driver )
73 delete m_driver; 77 delete m_driver;
74} 78}
75 79
76void ODateBookAccessBackend_SQL::initFields() 80void ODateBookAccessBackend_SQL::initFields()
77{ 81{
78 82
79 // This map contains the translation of the fieldtype id's to 83 // This map contains the translation of the fieldtype id's to
80 // the names of the table columns 84 // the names of the table columns
81 m_fieldMap.insert( OEvent::FUid, "uid" ); 85 m_fieldMap.insert( OEvent::FUid, "uid" );
82 m_fieldMap.insert( OEvent::FCategories, "Categories" ); 86 m_fieldMap.insert( OEvent::FCategories, "Categories" );
83 m_fieldMap.insert( OEvent::FDescription, "Description" ); 87 m_fieldMap.insert( OEvent::FDescription, "Description" );
84 m_fieldMap.insert( OEvent::FLocation, "Location" ); 88 m_fieldMap.insert( OEvent::FLocation, "Location" );
85 m_fieldMap.insert( OEvent::FType, "Type" ); 89 m_fieldMap.insert( OEvent::FType, "Type" );
86 m_fieldMap.insert( OEvent::FAlarm, "Alarm" ); 90 m_fieldMap.insert( OEvent::FAlarm, "Alarm" );
87 m_fieldMap.insert( OEvent::FSound, "Sound" ); 91 m_fieldMap.insert( OEvent::FSound, "Sound" );
88 m_fieldMap.insert( OEvent::FRType, "RType" ); 92 m_fieldMap.insert( OEvent::FRType, "RType" );
89 m_fieldMap.insert( OEvent::FRWeekdays, "RWeekdays" ); 93 m_fieldMap.insert( OEvent::FRWeekdays, "RWeekdays" );
90 m_fieldMap.insert( OEvent::FRPosition, "RPosition" ); 94 m_fieldMap.insert( OEvent::FRPosition, "RPosition" );
91 m_fieldMap.insert( OEvent::FRFreq, "RFreq" ); 95 m_fieldMap.insert( OEvent::FRFreq, "RFreq" );
92 m_fieldMap.insert( OEvent::FRHasEndDate, "RHasEndDate" ); 96 m_fieldMap.insert( OEvent::FRHasEndDate, "RHasEndDate" );
93 m_fieldMap.insert( OEvent::FREndDate, "REndDate" ); 97 m_fieldMap.insert( OEvent::FREndDate, "REndDate" );
94 m_fieldMap.insert( OEvent::FRCreated, "RCreated" ); 98 m_fieldMap.insert( OEvent::FRCreated, "RCreated" );
95 m_fieldMap.insert( OEvent::FRExceptions, "RExceptions" ); 99 m_fieldMap.insert( OEvent::FRExceptions, "RExceptions" );
96 m_fieldMap.insert( OEvent::FStart, "Start" ); 100 m_fieldMap.insert( OEvent::FStart, "Start" );
97 m_fieldMap.insert( OEvent::FEnd, "End" ); 101 m_fieldMap.insert( OEvent::FEnd, "End" );
98 m_fieldMap.insert( OEvent::FNote, "Note" ); 102 m_fieldMap.insert( OEvent::FNote, "Note" );
99 m_fieldMap.insert( OEvent::FTimeZone, "TimeZone" ); 103 m_fieldMap.insert( OEvent::FTimeZone, "TimeZone" );
100 m_fieldMap.insert( OEvent::FRecParent, "RecParent" ); 104 m_fieldMap.insert( OEvent::FRecParent, "RecParent" );
101 m_fieldMap.insert( OEvent::FRecChildren, "Recchildren" ); 105 m_fieldMap.insert( OEvent::FRecChildren, "Recchildren" );
102 106
103 // Create a map that maps the column name to the id 107 // Create a map that maps the column name to the id
104 QMapConstIterator<int, QString> it; 108 QMapConstIterator<int, QString> it;
105 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 109 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
106 m_reverseFieldMap.insert( it.data(), it.key() ); 110 m_reverseFieldMap.insert( it.data(), it.key() );
107 } 111 }
108 112
109} 113}
110 114
111bool ODateBookAccessBackend_SQL::load() 115bool ODateBookAccessBackend_SQL::load()
112{ 116{
113 if (!m_driver->open() ) 117 if (!m_driver->open() )
114 return false; 118 return false;
115 119
116 // Don't expect that the database exists. 120 // Don't expect that the database exists.
117 // It is save here to create the table, even if it 121 // It is save here to create the table, even if it
118 // do exist. ( Is that correct for all databases ?? ) 122 // do exist. ( Is that correct for all databases ?? )
119 QStringqu = "create table datebook( uid INTEGER PRIMARY KEY "; 123 QStringqu = "create table datebook( uid INTEGER PRIMARY KEY ";
120 124
121 QMap<int, QString>::Iterator it; 125 QMap<int, QString>::Iterator it;
122 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 126 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
123 qu += QString( ",%1 VARCHAR(10)" ).arg( it.data() ); 127 qu += QString( ",%1 VARCHAR(10)" ).arg( it.data() );
124 } 128 }
125 qu += " );"; 129 qu += " );";
126 130
127 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );"; 131 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );";
128 132
129 qWarning( "command: %s", qu.latin1() ); 133 qWarning( "command: %s", qu.latin1() );
130 134
131 OSQLRawQuery raw( qu ); 135 OSQLRawQuery raw( qu );
132 OSQLResult res = m_driver->query( &raw ); 136 OSQLResult res = m_driver->query( &raw );
133 if ( res.state() != OSQLResult::Success ) 137 if ( res.state() != OSQLResult::Success )
134 return false; 138 return false;
135 139
136 update(); 140 update();
137 141
138 return true; 142 return true;
139} 143}
140 144
141void ODateBookAccessBackend_SQL::update() 145void ODateBookAccessBackend_SQL::update()
142{ 146{
143 147
144 QString qu = "select uid from datebook"; 148 QString qu = "select uid from datebook";
145 OSQLRawQuery raw( qu ); 149 OSQLRawQuery raw( qu );
146 OSQLResult res = m_driver->query( &raw ); 150 OSQLResult res = m_driver->query( &raw );
147 if ( res.state() != OSQLResult::Success ){ 151 if ( res.state() != OSQLResult::Success ){
148 // m_uids.clear(); 152 // m_uids.clear();
149 return; 153 return;
150 } 154 }
151 155
152 m_uids = extractUids( res ); 156 m_uids = extractUids( res );
153 157
154} 158}
155 159
156bool ODateBookAccessBackend_SQL::reload() 160bool ODateBookAccessBackend_SQL::reload()
157{ 161{
158 return load(); 162 return load();
159} 163}
160 164
161bool ODateBookAccessBackend_SQL::save() 165bool ODateBookAccessBackend_SQL::save()
162{ 166{
163 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) 167 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers)
164} 168}
165 169
166QArray<int> ODateBookAccessBackend_SQL::allRecords()const 170QArray<int> ODateBookAccessBackend_SQL::allRecords()const
167{ 171{
168 return m_uids; 172 return m_uids;
169} 173}
170 174
171QArray<int> ODateBookAccessBackend_SQL::queryByExample(const OEvent&, int, const QDateTime& ) { 175QArray<int> ODateBookAccessBackend_SQL::queryByExample(const OEvent&, int, const QDateTime& ) {
172 return QArray<int>(); 176 return QArray<int>();
173} 177}
174 178
175void ODateBookAccessBackend_SQL::clear() 179void ODateBookAccessBackend_SQL::clear()
176{ 180{
177 QString qu = "drop table datebook;"; 181 QString qu = "drop table datebook;";
178 qu += "drop table custom_data;"; 182 qu += "drop table custom_data;";
179 183
180 OSQLRawQuery raw( qu ); 184 OSQLRawQuery raw( qu );
181 OSQLResult res = m_driver->query( &raw ); 185 OSQLResult res = m_driver->query( &raw );
182 186
183 reload(); 187 reload();
184} 188}
185 189
186 190
187OEvent ODateBookAccessBackend_SQL::find( int uid ) const{ 191OEvent ODateBookAccessBackend_SQL::find( int uid ) const{
188 QString qu = "select *"; 192 QString qu = "select *";
189 qu += "from datebook where uid = " + QString::number(uid); 193 qu += "from datebook where uid = " + QString::number(uid);
190 194
191 OSQLRawQuery raw( qu ); 195 OSQLRawQuery raw( qu );
192 OSQLResult res = m_driver->query( &raw ); 196 OSQLResult res = m_driver->query( &raw );
193 197
194 OSQLResultItem resItem = res.first(); 198 OSQLResultItem resItem = res.first();
195 199
196 // Create Map for date event and insert UID 200 // Create Map for date event and insert UID
197 QMap<int,QString> dateEventMap; 201 QMap<int,QString> dateEventMap;
198 dateEventMap.insert( OEvent::FUid, QString::number( uid ) ); 202 dateEventMap.insert( OEvent::FUid, QString::number( uid ) );
199 203
200 // Now insert the data out of the columns into the map. 204 // Now insert the data out of the columns into the map.
201 QMapConstIterator<int, QString> it; 205 QMapConstIterator<int, QString> it;
202 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 206 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
203 dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) ); 207 dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) );
204 } 208 }
205 209
206 // Last step: Put map into date event and return it 210 // Last step: Put map into date event and return it
207 OEvent retDate( dateEventMap ); 211 OEvent retDate( dateEventMap );
208 212
209 return retDate; 213 return retDate;
210} 214}
211 215
212// FIXME: Speed up update of uid's.. 216// FIXME: Speed up update of uid's..
213bool ODateBookAccessBackend_SQL::add( const OEvent& ev ) 217bool ODateBookAccessBackend_SQL::add( const OEvent& ev )
214{ 218{
215 QMap<int,QString> eventMap = ev.toMap(); 219 QMap<int,QString> eventMap = ev.toMap();
216 220
217 QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() ); 221 QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() );
218 QMap<int, QString>::Iterator it; 222 QMap<int, QString>::Iterator it;
219 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 223 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
220 if ( !eventMap[it.key()].isEmpty() ) 224 if ( !eventMap[it.key()].isEmpty() )
221 qu += QString( ",\"%1\"" ).arg( eventMap[it.key()] ); 225 qu += QString( ",\"%1\"" ).arg( eventMap[it.key()] );
222 else 226 else
223 qu += QString( ",\"\"" ); 227 qu += QString( ",\"\"" );
224 } 228 }
225 qu += " );"; 229 qu += " );";
226 230
227 // Add custom entries 231 // Add custom entries
228 int id = 0; 232 int id = 0;
229 QMap<QString, QString> customMap = ev.toExtraMap(); 233 QMap<QString, QString> customMap = ev.toExtraMap();
230 for( QMap<QString, QString>::Iterator it = customMap.begin(); 234 for( QMap<QString, QString>::Iterator it = customMap.begin();
231 it != customMap.end(); ++it ){ 235 it != customMap.end(); ++it ){
232 qu += "insert into custom_data VALUES(" 236 qu += "insert into custom_data VALUES("
233 + QString::number( ev.uid() ) 237 + QString::number( ev.uid() )
234 + "," 238 + ","
235 + QString::number( id++ ) 239 + QString::number( id++ )
236 + ",'" 240 + ",'"
237 + it.key() //.latin1() 241 + it.key() //.latin1()
238 + "'," 242 + "',"
239 + "0" // Priority for future enhancements 243 + "0" // Priority for future enhancements
240 + ",'" 244 + ",'"
241 + it.data() //.latin1() 245 + it.data() //.latin1()
242 + "');"; 246 + "');";
243 } 247 }
244 qWarning("add %s", qu.latin1() ); 248 qWarning("add %s", qu.latin1() );
245 249
246 OSQLRawQuery raw( qu ); 250 OSQLRawQuery raw( qu );
247 OSQLResult res = m_driver->query( &raw ); 251 OSQLResult res = m_driver->query( &raw );
248 if ( res.state() != OSQLResult::Success ){ 252 if ( res.state() != OSQLResult::Success ){
249 return false; 253 return false;
250 } 254 }
251 255
252 // Update list of uid's 256 // Update list of uid's
253 update(); 257 update();
254 258
255 return true; 259 return true;
256} 260}
257 261
258// FIXME: Speed up update of uid's.. 262// FIXME: Speed up update of uid's..
259bool ODateBookAccessBackend_SQL::remove( int uid ) 263bool ODateBookAccessBackend_SQL::remove( int uid )
260{ 264{
261 QString qu = "DELETE from datebook where uid = " 265 QString qu = "DELETE from datebook where uid = "
262 + QString::number( uid ) + ";"; 266 + QString::number( uid ) + ";";
263 qu += "DELETE from custom_data where uid = " 267 qu += "DELETE from custom_data where uid = "
264 + QString::number( uid ) + ";"; 268 + QString::number( uid ) + ";";
265 269
266 OSQLRawQuery raw( qu ); 270 OSQLRawQuery raw( qu );
267 OSQLResult res = m_driver->query( &raw ); 271 OSQLResult res = m_driver->query( &raw );
268 if ( res.state() != OSQLResult::Success ){ 272 if ( res.state() != OSQLResult::Success ){
269 return false; 273 return false;
270 } 274 }
271 275
272 // Update list of uid's 276 // Update list of uid's
273 update(); 277 update();
274 278
275 return true; 279 return true;
276} 280}
277 281
278bool ODateBookAccessBackend_SQL::replace( const OEvent& ev ) 282bool ODateBookAccessBackend_SQL::replace( const OEvent& ev )
279{ 283{
280 remove( ev.uid() ); 284 remove( ev.uid() );
281 return add( ev ); 285 return add( ev );
282} 286}
283 287
284QArray<int> ODateBookAccessBackend_SQL::rawEvents()const 288QArray<int> ODateBookAccessBackend_SQL::rawEvents()const
285{ 289{
286 return allRecords(); 290 return allRecords();
287} 291}
288 292
289QArray<int> ODateBookAccessBackend_SQL::rawRepeats()const 293QArray<int> ODateBookAccessBackend_SQL::rawRepeats()const
290{ 294{
291 QString qu = "select uid from datebook where RType!=\"\" AND RType!=\"NoRepeat\""; 295 QString qu = "select uid from datebook where RType!=\"\" AND RType!=\"NoRepeat\"";
292 OSQLRawQuery raw( qu ); 296 OSQLRawQuery raw( qu );
293 OSQLResult res = m_driver->query( &raw ); 297 OSQLResult res = m_driver->query( &raw );
294 if ( res.state() != OSQLResult::Success ){ 298 if ( res.state() != OSQLResult::Success ){
295 QArray<int> nix; 299 QArray<int> nix;
296 return nix; 300 return nix;
297 } 301 }
298 302
299 return extractUids( res ); 303 return extractUids( res );
300} 304}
301 305
302QArray<int> ODateBookAccessBackend_SQL::nonRepeats()const 306QArray<int> ODateBookAccessBackend_SQL::nonRepeats()const
303{ 307{
304 QString qu = "select uid from datebook where RType=\"\" or RType=\"NoRepeat\""; 308 QString qu = "select uid from datebook where RType=\"\" or RType=\"NoRepeat\"";
305 OSQLRawQuery raw( qu ); 309 OSQLRawQuery raw( qu );
306 OSQLResult res = m_driver->query( &raw ); 310 OSQLResult res = m_driver->query( &raw );
307 if ( res.state() != OSQLResult::Success ){ 311 if ( res.state() != OSQLResult::Success ){
308 QArray<int> nix; 312 QArray<int> nix;
309 return nix; 313 return nix;
310 } 314 }
311 315
312 return extractUids( res ); 316 return extractUids( res );
313} 317}
314 318
315OEvent::ValueList ODateBookAccessBackend_SQL::directNonRepeats() 319OEvent::ValueList ODateBookAccessBackend_SQL::directNonRepeats()
316{ 320{
317 QArray<int> nonRepUids = nonRepeats(); 321 QArray<int> nonRepUids = nonRepeats();
318 OEvent::ValueList list; 322 OEvent::ValueList list;
319 323
320 for (uint i = 0; i < nonRepUids.count(); ++i ){ 324 for (uint i = 0; i < nonRepUids.count(); ++i ){
321 list.append( find( nonRepUids[i] ) ); 325 list.append( find( nonRepUids[i] ) );
322 } 326 }
323 327
324 return list; 328 return list;
325 329
326} 330}
327OEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats() 331OEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats()
328{ 332{
329 QArray<int> rawRepUids = rawRepeats(); 333 QArray<int> rawRepUids = rawRepeats();
330 OEvent::ValueList list; 334 OEvent::ValueList list;
331 335
332 for (uint i = 0; i < rawRepUids.count(); ++i ){ 336 for (uint i = 0; i < rawRepUids.count(); ++i ){
333 list.append( find( rawRepUids[i] ) ); 337 list.append( find( rawRepUids[i] ) );
334 } 338 }
335 339
336 return list; 340 return list;
337} 341}
338 342
339 343
340QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const 344QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const
341{ 345{
342 QArray<int> null; 346 QArray<int> null;
343 return null; 347 return null;
344} 348}
345 349
346/* ===== Private Functions ========================================== */ 350/* ===== Private Functions ========================================== */
347 351
348QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const 352QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const
349{ 353{
350 qWarning("extractUids"); 354 qWarning("extractUids");
351 QTime t; 355 QTime t;
352 t.start(); 356 t.start();
353 OSQLResultItem::ValueList list = res.results(); 357 OSQLResultItem::ValueList list = res.results();
354 OSQLResultItem::ValueList::Iterator it; 358 OSQLResultItem::ValueList::Iterator it;
355 QArray<int> ints(list.count() ); 359 QArray<int> ints(list.count() );
356 qWarning(" count = %d", list.count() ); 360 qWarning(" count = %d", list.count() );
357 361
358 int i = 0; 362 int i = 0;
359 for (it = list.begin(); it != list.end(); ++it ) { 363 for (it = list.begin(); it != list.end(); ++it ) {
360 ints[i] = (*it).data("uid").toInt(); 364 ints[i] = (*it).data("uid").toInt();
361 i++; 365 i++;
362 } 366 }
363 qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() ); 367 qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() );
364 368
365 return ints; 369 return ints;
366 370
367} 371}
diff --git a/libopie/pim/odatebookaccessbackend_sql.h b/libopie/pim/odatebookaccessbackend_sql.h
index f39e154..ba514bc 100644
--- a/libopie/pim/odatebookaccessbackend_sql.h
+++ b/libopie/pim/odatebookaccessbackend_sql.h
@@ -1,62 +1,65 @@
1#ifndef OPIE_DATE_BOOK_ACCESS_BACKEND_SQL__H 1#ifndef OPIE_DATE_BOOK_ACCESS_BACKEND_SQL__H
2#define OPIE_DATE_BOOK_ACCESS_BACKEND_SQL__H 2#define OPIE_DATE_BOOK_ACCESS_BACKEND_SQL__H
3 3
4#include <qmap.h> 4#include <qmap.h>
5#include <opie2/osqlresult.h> 5#include <opie2/osqlresult.h>
6 6
7#include "odatebookaccessbackend.h" 7#include "odatebookaccessbackend.h"
8 8
9namespace Opie { namespace DB {
9class OSQLDriver; 10class OSQLDriver;
10 11
12}}
13
11/** 14/**
12 * This is the default SQL implementation for DateBoook SQL storage 15 * This is the default SQL implementation for DateBoook SQL storage
13 * It fully implements the interface 16 * It fully implements the interface
14 * @see ODateBookAccessBackend 17 * @see ODateBookAccessBackend
15 * @see OPimAccessBackend 18 * @see OPimAccessBackend
16 */ 19 */
17class ODateBookAccessBackend_SQL : public ODateBookAccessBackend { 20class ODateBookAccessBackend_SQL : public ODateBookAccessBackend {
18public: 21public:
19 ODateBookAccessBackend_SQL( const QString& appName, 22 ODateBookAccessBackend_SQL( const QString& appName,
20 const QString& fileName = QString::null); 23 const QString& fileName = QString::null);
21 ~ODateBookAccessBackend_SQL(); 24 ~ODateBookAccessBackend_SQL();
22 25
23 bool load(); 26 bool load();
24 bool reload(); 27 bool reload();
25 bool save(); 28 bool save();
26 29
27 QArray<int> allRecords()const; 30 QArray<int> allRecords()const;
28 QArray<int> matchRegexp(const QRegExp &r) const; 31 QArray<int> matchRegexp(const QRegExp &r) const;
29 QArray<int> queryByExample( const OEvent&, int, const QDateTime& d = QDateTime() ); 32 QArray<int> queryByExample( const OEvent&, int, const QDateTime& d = QDateTime() );
30 OEvent find( int uid )const; 33 OEvent find( int uid )const;
31 void clear(); 34 void clear();
32 bool add( const OEvent& ev ); 35 bool add( const OEvent& ev );
33 bool remove( int uid ); 36 bool remove( int uid );
34 bool replace( const OEvent& ev ); 37 bool replace( const OEvent& ev );
35 38
36 QArray<UID> rawEvents()const; 39 QArray<UID> rawEvents()const;
37 QArray<UID> rawRepeats()const; 40 QArray<UID> rawRepeats()const;
38 QArray<UID> nonRepeats()const; 41 QArray<UID> nonRepeats()const;
39 42
40 OEvent::ValueList directNonRepeats(); 43 OEvent::ValueList directNonRepeats();
41 OEvent::ValueList directRawRepeats(); 44 OEvent::ValueList directRawRepeats();
42 45
43private: 46private:
44 bool loadFile(); 47 bool loadFile();
45 QString m_fileName; 48 QString m_fileName;
46 QArray<int> m_uids; 49 QArray<int> m_uids;
47 50
48 QMap<int, QString> m_fieldMap; 51 QMap<int, QString> m_fieldMap;
49 QMap<QString, int> m_reverseFieldMap; 52 QMap<QString, int> m_reverseFieldMap;
50 53
51 OSQLDriver* m_driver; 54 Opie::DB::OSQLDriver* m_driver;
52 55
53 class Private; 56 class Private;
54 Private *d; 57 Private *d;
55 58
56 void initFields(); 59 void initFields();
57 void update(); 60 void update();
58 QArray<int> extractUids( OSQLResult& res ) const; 61 QArray<int> extractUids( Opie::DB::OSQLResult& res ) const;
59 62
60}; 63};
61 64
62#endif 65#endif
diff --git a/libopie/pim/otodoaccesssql.cpp b/libopie/pim/otodoaccesssql.cpp
index 3764c7e..fd01a42 100644
--- a/libopie/pim/otodoaccesssql.cpp
+++ b/libopie/pim/otodoaccesssql.cpp
@@ -1,527 +1,528 @@
1 1
2#include <qdatetime.h> 2#include <qdatetime.h>
3 3
4#include <qpe/global.h> 4#include <qpe/global.h>
5 5
6#include <opie2/osqldriver.h> 6#include <opie2/osqldriver.h>
7#include <opie2/osqlresult.h> 7#include <opie2/osqlresult.h>
8#include <opie2/osqlmanager.h> 8#include <opie2/osqlmanager.h>
9#include <opie2/osqlquery.h> 9#include <opie2/osqlquery.h>
10 10
11#include "otodoaccesssql.h" 11#include "otodoaccesssql.h"
12#include "opimstate.h" 12#include "opimstate.h"
13#include "opimnotifymanager.h" 13#include "opimnotifymanager.h"
14#include "orecur.h" 14#include "orecur.h"
15 15
16using namespace Opie::DB;
16/* 17/*
17 * first some query 18 * first some query
18 * CREATE query 19 * CREATE query
19 * LOAD query 20 * LOAD query
20 * INSERT 21 * INSERT
21 * REMOVE 22 * REMOVE
22 * CLEAR 23 * CLEAR
23 */ 24 */
24namespace { 25namespace {
25 /** 26 /**
26 * CreateQuery for the Todolist Table 27 * CreateQuery for the Todolist Table
27 */ 28 */
28 class CreateQuery : public OSQLQuery { 29 class CreateQuery : public OSQLQuery {
29 public: 30 public:
30 CreateQuery(); 31 CreateQuery();
31 ~CreateQuery(); 32 ~CreateQuery();
32 QString query()const; 33 QString query()const;
33 }; 34 };
34 35
35 /** 36 /**
36 * LoadQuery 37 * LoadQuery
37 * this one queries for all uids 38 * this one queries for all uids
38 */ 39 */
39 class LoadQuery : public OSQLQuery { 40 class LoadQuery : public OSQLQuery {
40 public: 41 public:
41 LoadQuery(); 42 LoadQuery();
42 ~LoadQuery(); 43 ~LoadQuery();
43 QString query()const; 44 QString query()const;
44 }; 45 };
45 46
46 /** 47 /**
47 * inserts/adds a OTodo to the table 48 * inserts/adds a OTodo to the table
48 */ 49 */
49 class InsertQuery : public OSQLQuery { 50 class InsertQuery : public OSQLQuery {
50 public: 51 public:
51 InsertQuery(const OTodo& ); 52 InsertQuery(const OTodo& );
52 ~InsertQuery(); 53 ~InsertQuery();
53 QString query()const; 54 QString query()const;
54 private: 55 private:
55 OTodo m_todo; 56 OTodo m_todo;
56 }; 57 };
57 58
58 /** 59 /**
59 * removes one from the table 60 * removes one from the table
60 */ 61 */
61 class RemoveQuery : public OSQLQuery { 62 class RemoveQuery : public OSQLQuery {
62 public: 63 public:
63 RemoveQuery(int uid ); 64 RemoveQuery(int uid );
64 ~RemoveQuery(); 65 ~RemoveQuery();
65 QString query()const; 66 QString query()const;
66 private: 67 private:
67 int m_uid; 68 int m_uid;
68 }; 69 };
69 70
70 /** 71 /**
71 * Clears (delete) a Table 72 * Clears (delete) a Table
72 */ 73 */
73 class ClearQuery : public OSQLQuery { 74 class ClearQuery : public OSQLQuery {
74 public: 75 public:
75 ClearQuery(); 76 ClearQuery();
76 ~ClearQuery(); 77 ~ClearQuery();
77 QString query()const; 78 QString query()const;
78 79
79 }; 80 };
80 81
81 /** 82 /**
82 * a find query 83 * a find query
83 */ 84 */
84 class FindQuery : public OSQLQuery { 85 class FindQuery : public OSQLQuery {
85 public: 86 public:
86 FindQuery(int uid); 87 FindQuery(int uid);
87 FindQuery(const QArray<int>& ); 88 FindQuery(const QArray<int>& );
88 ~FindQuery(); 89 ~FindQuery();
89 QString query()const; 90 QString query()const;
90 private: 91 private:
91 QString single()const; 92 QString single()const;
92 QString multi()const; 93 QString multi()const;
93 QArray<int> m_uids; 94 QArray<int> m_uids;
94 int m_uid; 95 int m_uid;
95 }; 96 };
96 97
97 /** 98 /**
98 * overdue query 99 * overdue query
99 */ 100 */
100 class OverDueQuery : public OSQLQuery { 101 class OverDueQuery : public OSQLQuery {
101 public: 102 public:
102 OverDueQuery(); 103 OverDueQuery();
103 ~OverDueQuery(); 104 ~OverDueQuery();
104 QString query()const; 105 QString query()const;
105 }; 106 };
106 class EffQuery : public OSQLQuery { 107 class EffQuery : public OSQLQuery {
107 public: 108 public:
108 EffQuery( const QDate&, const QDate&, bool inc ); 109 EffQuery( const QDate&, const QDate&, bool inc );
109 ~EffQuery(); 110 ~EffQuery();
110 QString query()const; 111 QString query()const;
111 private: 112 private:
112 QString with()const; 113 QString with()const;
113 QString out()const; 114 QString out()const;
114 QDate m_start; 115 QDate m_start;
115 QDate m_end; 116 QDate m_end;
116 bool m_inc :1; 117 bool m_inc :1;
117 }; 118 };
118 119
119 120
120 CreateQuery::CreateQuery() : OSQLQuery() {} 121 CreateQuery::CreateQuery() : OSQLQuery() {}
121 CreateQuery::~CreateQuery() {} 122 CreateQuery::~CreateQuery() {}
122 QString CreateQuery::query()const { 123 QString CreateQuery::query()const {
123 QString qu; 124 QString qu;
124 qu += "create table todolist( uid PRIMARY KEY, categories, completed, "; 125 qu += "create table todolist( uid PRIMARY KEY, categories, completed, ";
125 qu += "description, summary, priority, DueDate, progress , state, "; 126 qu += "description, summary, priority, DueDate, progress , state, ";
126 // This is the recurrance-stuff .. Exceptions are currently not supported (see ORecur.cpp) ! (eilers) 127 // This is the recurrance-stuff .. Exceptions are currently not supported (see ORecur.cpp) ! (eilers)
127 qu += "RType, RWeekdays, RPosition, RFreq, RHasEndDate, EndDate, Created, Exceptions, "; 128 qu += "RType, RWeekdays, RPosition, RFreq, RHasEndDate, EndDate, Created, Exceptions, ";
128 qu += "reminders, alarms, maintainer, startdate, completeddate);"; 129 qu += "reminders, alarms, maintainer, startdate, completeddate);";
129 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );"; 130 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );";
130 return qu; 131 return qu;
131 } 132 }
132 133
133 LoadQuery::LoadQuery() : OSQLQuery() {} 134 LoadQuery::LoadQuery() : OSQLQuery() {}
134 LoadQuery::~LoadQuery() {} 135 LoadQuery::~LoadQuery() {}
135 QString LoadQuery::query()const { 136 QString LoadQuery::query()const {
136 QString qu; 137 QString qu;
137 // We do not need "distinct" here. The primary key is always unique.. 138 // We do not need "distinct" here. The primary key is always unique..
138 //qu += "select distinct uid from todolist"; 139 //qu += "select distinct uid from todolist";
139 qu += "select uid from todolist"; 140 qu += "select uid from todolist";
140 141
141 return qu; 142 return qu;
142 } 143 }
143 144
144 InsertQuery::InsertQuery( const OTodo& todo ) 145 InsertQuery::InsertQuery( const OTodo& todo )
145 : OSQLQuery(), m_todo( todo ) { 146 : OSQLQuery(), m_todo( todo ) {
146 } 147 }
147 InsertQuery::~InsertQuery() { 148 InsertQuery::~InsertQuery() {
148 } 149 }
149 /* 150 /*
150 * converts from a OTodo to a query 151 * converts from a OTodo to a query
151 * we leave out X-Ref + Alarms 152 * we leave out X-Ref + Alarms
152 */ 153 */
153 QString InsertQuery::query()const{ 154 QString InsertQuery::query()const{
154 155
155 int year, month, day; 156 int year, month, day;
156 year = month = day = 0; 157 year = month = day = 0;
157 if (m_todo.hasDueDate() ) { 158 if (m_todo.hasDueDate() ) {
158 QDate date = m_todo.dueDate(); 159 QDate date = m_todo.dueDate();
159 year = date.year(); 160 year = date.year();
160 month = date.month(); 161 month = date.month();
161 day = date.day(); 162 day = date.day();
162 } 163 }
163 int sYear = 0, sMonth = 0, sDay = 0; 164 int sYear = 0, sMonth = 0, sDay = 0;
164 if( m_todo.hasStartDate() ){ 165 if( m_todo.hasStartDate() ){
165 QDate sDate = m_todo.startDate(); 166 QDate sDate = m_todo.startDate();
166 sYear = sDate.year(); 167 sYear = sDate.year();
167 sMonth= sDate.month(); 168 sMonth= sDate.month();
168 sDay = sDate.day(); 169 sDay = sDate.day();
169 } 170 }
170 171
171 int eYear = 0, eMonth = 0, eDay = 0; 172 int eYear = 0, eMonth = 0, eDay = 0;
172 if( m_todo.hasCompletedDate() ){ 173 if( m_todo.hasCompletedDate() ){
173 QDate eDate = m_todo.completedDate(); 174 QDate eDate = m_todo.completedDate();
174 eYear = eDate.year(); 175 eYear = eDate.year();
175 eMonth= eDate.month(); 176 eMonth= eDate.month();
176 eDay = eDate.day(); 177 eDay = eDate.day();
177 } 178 }
178 QString qu; 179 QString qu;
179 QMap<int, QString> recMap = m_todo.recurrence().toMap(); 180 QMap<int, QString> recMap = m_todo.recurrence().toMap();
180 qu = "insert into todolist VALUES(" 181 qu = "insert into todolist VALUES("
181 + QString::number( m_todo.uid() ) + "," 182 + QString::number( m_todo.uid() ) + ","
182 + "'" + m_todo.idsToString( m_todo.categories() ) + "'" + "," 183 + "'" + m_todo.idsToString( m_todo.categories() ) + "'" + ","
183 + QString::number( m_todo.isCompleted() ) + "," 184 + QString::number( m_todo.isCompleted() ) + ","
184 + "'" + m_todo.description() + "'" + "," 185 + "'" + m_todo.description() + "'" + ","
185 + "'" + m_todo.summary() + "'" + "," 186 + "'" + m_todo.summary() + "'" + ","
186 + QString::number(m_todo.priority() ) + "," 187 + QString::number(m_todo.priority() ) + ","
187 + "'" + QString::number(year) + "-" 188 + "'" + QString::number(year) + "-"
188 + QString::number(month) 189 + QString::number(month)
189 + "-" + QString::number( day ) + "'" + "," 190 + "-" + QString::number( day ) + "'" + ","
190 + QString::number( m_todo.progress() ) + "," 191 + QString::number( m_todo.progress() ) + ","
191 + QString::number( m_todo.state().state() ) + "," 192 + QString::number( m_todo.state().state() ) + ","
192 + "'" + recMap[ ORecur::RType ] + "'" + "," 193 + "'" + recMap[ ORecur::RType ] + "'" + ","
193 + "'" + recMap[ ORecur::RWeekdays ] + "'" + "," 194 + "'" + recMap[ ORecur::RWeekdays ] + "'" + ","
194 + "'" + recMap[ ORecur::RPosition ] + "'" + "," 195 + "'" + recMap[ ORecur::RPosition ] + "'" + ","
195 + "'" + recMap[ ORecur::RFreq ] + "'" + "," 196 + "'" + recMap[ ORecur::RFreq ] + "'" + ","
196 + "'" + recMap[ ORecur::RHasEndDate ] + "'" + "," 197 + "'" + recMap[ ORecur::RHasEndDate ] + "'" + ","
197 + "'" + recMap[ ORecur::EndDate ] + "'" + "," 198 + "'" + recMap[ ORecur::EndDate ] + "'" + ","
198 + "'" + recMap[ ORecur::Created ] + "'" + "," 199 + "'" + recMap[ ORecur::Created ] + "'" + ","
199 + "'" + recMap[ ORecur::Exceptions ] + "'" + ","; 200 + "'" + recMap[ ORecur::Exceptions ] + "'" + ",";
200 201
201 if ( m_todo.hasNotifiers() ) { 202 if ( m_todo.hasNotifiers() ) {
202 OPimNotifyManager manager = m_todo.notifiers(); 203 OPimNotifyManager manager = m_todo.notifiers();
203 qu += "'" + manager.remindersToString() + "'" + "," 204 qu += "'" + manager.remindersToString() + "'" + ","
204 + "'" + manager.alarmsToString() + "'" + ","; 205 + "'" + manager.alarmsToString() + "'" + ",";
205 } 206 }
206 else{ 207 else{
207 qu += QString( "''" ) + "," 208 qu += QString( "''" ) + ","
208 + "''" + ","; 209 + "''" + ",";
209 } 210 }
210 211
211 qu += QString( "''" ) + QString( "," ) // Maintainers (cur. not supported !) 212 qu += QString( "''" ) + QString( "," ) // Maintainers (cur. not supported !)
212 + "'" + QString::number(sYear) + "-" 213 + "'" + QString::number(sYear) + "-"
213 + QString::number(sMonth) 214 + QString::number(sMonth)
214 + "-" + QString::number(sDay) + "'" + "," 215 + "-" + QString::number(sDay) + "'" + ","
215 + "'" + QString::number(eYear) + "-" 216 + "'" + QString::number(eYear) + "-"
216 + QString::number(eMonth) 217 + QString::number(eMonth)
217 + "-"+QString::number(eDay) + "'" 218 + "-"+QString::number(eDay) + "'"
218 + ")"; 219 + ")";
219 220
220 qWarning("add %s", qu.latin1() ); 221 qWarning("add %s", qu.latin1() );
221 return qu; 222 return qu;
222 } 223 }
223 224
224 RemoveQuery::RemoveQuery(int uid ) 225 RemoveQuery::RemoveQuery(int uid )
225 : OSQLQuery(), m_uid( uid ) {} 226 : OSQLQuery(), m_uid( uid ) {}
226 RemoveQuery::~RemoveQuery() {} 227 RemoveQuery::~RemoveQuery() {}
227 QString RemoveQuery::query()const { 228 QString RemoveQuery::query()const {
228 QString qu = "DELETE from todolist where uid = " + QString::number(m_uid); 229 QString qu = "DELETE from todolist where uid = " + QString::number(m_uid);
229 return qu; 230 return qu;
230 } 231 }
231 232
232 233
233 ClearQuery::ClearQuery() 234 ClearQuery::ClearQuery()
234 : OSQLQuery() {} 235 : OSQLQuery() {}
235 ClearQuery::~ClearQuery() {} 236 ClearQuery::~ClearQuery() {}
236 QString ClearQuery::query()const { 237 QString ClearQuery::query()const {
237 QString qu = "drop table todolist"; 238 QString qu = "drop table todolist";
238 return qu; 239 return qu;
239 } 240 }
240 FindQuery::FindQuery(int uid) 241 FindQuery::FindQuery(int uid)
241 : OSQLQuery(), m_uid(uid ) { 242 : OSQLQuery(), m_uid(uid ) {
242 } 243 }
243 FindQuery::FindQuery(const QArray<int>& ints) 244 FindQuery::FindQuery(const QArray<int>& ints)
244 : OSQLQuery(), m_uids(ints){ 245 : OSQLQuery(), m_uids(ints){
245 } 246 }
246 FindQuery::~FindQuery() { 247 FindQuery::~FindQuery() {
247 } 248 }
248 QString FindQuery::query()const{ 249 QString FindQuery::query()const{
249 if (m_uids.count() == 0 ) 250 if (m_uids.count() == 0 )
250 return single(); 251 return single();
251 else 252 else
252 return multi(); 253 return multi();
253 } 254 }
254 QString FindQuery::single()const{ 255 QString FindQuery::single()const{
255 QString qu = "select * from todolist where uid = " + QString::number(m_uid); 256 QString qu = "select * from todolist where uid = " + QString::number(m_uid);
256 return qu; 257 return qu;
257 } 258 }
258 QString FindQuery::multi()const { 259 QString FindQuery::multi()const {
259 QString qu = "select * from todolist where "; 260 QString qu = "select * from todolist where ";
260 for (uint i = 0; i < m_uids.count(); i++ ) { 261 for (uint i = 0; i < m_uids.count(); i++ ) {
261 qu += " UID = " + QString::number( m_uids[i] ) + " OR"; 262 qu += " UID = " + QString::number( m_uids[i] ) + " OR";
262 } 263 }
263 qu.remove( qu.length()-2, 2 ); 264 qu.remove( qu.length()-2, 2 );
264 return qu; 265 return qu;
265 } 266 }
266 267
267 OverDueQuery::OverDueQuery(): OSQLQuery() {} 268 OverDueQuery::OverDueQuery(): OSQLQuery() {}
268 OverDueQuery::~OverDueQuery() {} 269 OverDueQuery::~OverDueQuery() {}
269 QString OverDueQuery::query()const { 270 QString OverDueQuery::query()const {
270 QDate date = QDate::currentDate(); 271 QDate date = QDate::currentDate();
271 QString str; 272 QString str;
272 str = QString("select uid from todolist where DueDate ='%1-%2-%3'").arg(date.year() ).arg(date.month() ).arg(date.day() ); 273 str = QString("select uid from todolist where DueDate ='%1-%2-%3'").arg(date.year() ).arg(date.month() ).arg(date.day() );
273 274
274 return str; 275 return str;
275 } 276 }
276 277
277 278
278 EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc ) 279 EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc )
279 : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {} 280 : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {}
280 EffQuery::~EffQuery() {} 281 EffQuery::~EffQuery() {}
281 QString EffQuery::query()const { 282 QString EffQuery::query()const {
282 return m_inc ? with() : out(); 283 return m_inc ? with() : out();
283 } 284 }
284 QString EffQuery::with()const { 285 QString EffQuery::with()const {
285 QString str; 286 QString str;
286 str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ") 287 str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ")
287 .arg( m_start.year() ).arg( m_start.month() ).arg( m_start.day() ) 288 .arg( m_start.year() ).arg( m_start.month() ).arg( m_start.day() )
288 .arg( m_end .year() ).arg( m_end .month() ).arg( m_end .day() ); 289 .arg( m_end .year() ).arg( m_end .month() ).arg( m_end .day() );
289 return str; 290 return str;
290 } 291 }
291 QString EffQuery::out()const { 292 QString EffQuery::out()const {
292 QString str; 293 QString str;
293 str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'") 294 str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'")
294 .arg(m_start.year() ).arg(m_start.month() ).arg( m_start.day() ) 295 .arg(m_start.year() ).arg(m_start.month() ).arg( m_start.day() )
295 .arg(m_end. year() ).arg(m_end. month() ).arg(m_end.day() ); 296 .arg(m_end. year() ).arg(m_end. month() ).arg(m_end.day() );
296 297
297 return str; 298 return str;
298 } 299 }
299}; 300};
300 301
301OTodoAccessBackendSQL::OTodoAccessBackendSQL( const QString& file ) 302OTodoAccessBackendSQL::OTodoAccessBackendSQL( const QString& file )
302 : OTodoAccessBackend(), m_dict(15), m_driver(NULL), m_dirty(true) 303 : OTodoAccessBackend(), m_dict(15), m_driver(NULL), m_dirty(true)
303{ 304{
304 QString fi = file; 305 QString fi = file;
305 if ( fi.isEmpty() ) 306 if ( fi.isEmpty() )
306 fi = Global::applicationFileName( "todolist", "todolist.db" ); 307 fi = Global::applicationFileName( "todolist", "todolist.db" );
307 OSQLManager man; 308 OSQLManager man;
308 m_driver = man.standard(); 309 m_driver = man.standard();
309 m_driver->setUrl(fi); 310 m_driver->setUrl(fi);
310 // fillDict(); 311 // fillDict();
311} 312}
312 313
313OTodoAccessBackendSQL::~OTodoAccessBackendSQL(){ 314OTodoAccessBackendSQL::~OTodoAccessBackendSQL(){
314 if( m_driver ) 315 if( m_driver )
315 delete m_driver; 316 delete m_driver;
316} 317}
317 318
318bool OTodoAccessBackendSQL::load(){ 319bool OTodoAccessBackendSQL::load(){
319 if (!m_driver->open() ) 320 if (!m_driver->open() )
320 return false; 321 return false;
321 322
322 CreateQuery creat; 323 CreateQuery creat;
323 OSQLResult res = m_driver->query(&creat ); 324 OSQLResult res = m_driver->query(&creat );
324 325
325 m_dirty = true; 326 m_dirty = true;
326 return true; 327 return true;
327} 328}
328bool OTodoAccessBackendSQL::reload(){ 329bool OTodoAccessBackendSQL::reload(){
329 return load(); 330 return load();
330} 331}
331 332
332bool OTodoAccessBackendSQL::save(){ 333bool OTodoAccessBackendSQL::save(){
333 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) 334 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers)
334} 335}
335QArray<int> OTodoAccessBackendSQL::allRecords()const { 336QArray<int> OTodoAccessBackendSQL::allRecords()const {
336 if (m_dirty ) 337 if (m_dirty )
337 update(); 338 update();
338 339
339 return m_uids; 340 return m_uids;
340} 341}
341QArray<int> OTodoAccessBackendSQL::queryByExample( const OTodo& , int, const QDateTime& ){ 342QArray<int> OTodoAccessBackendSQL::queryByExample( const OTodo& , int, const QDateTime& ){
342 QArray<int> ints(0); 343 QArray<int> ints(0);
343 return ints; 344 return ints;
344} 345}
345OTodo OTodoAccessBackendSQL::find(int uid ) const{ 346OTodo OTodoAccessBackendSQL::find(int uid ) const{
346 FindQuery query( uid ); 347 FindQuery query( uid );
347 return todo( m_driver->query(&query) ); 348 return todo( m_driver->query(&query) );
348 349
349} 350}
350OTodo OTodoAccessBackendSQL::find( int uid, const QArray<int>& ints, 351OTodo OTodoAccessBackendSQL::find( int uid, const QArray<int>& ints,
351 uint cur, Frontend::CacheDirection dir ) const{ 352 uint cur, Frontend::CacheDirection dir ) const{
352 uint CACHE = readAhead(); 353 uint CACHE = readAhead();
353 qWarning("searching for %d", uid ); 354 qWarning("searching for %d", uid );
354 QArray<int> search( CACHE ); 355 QArray<int> search( CACHE );
355 uint size =0; 356 uint size =0;
356 OTodo to; 357 OTodo to;
357 358
358 // we try to cache CACHE items 359 // we try to cache CACHE items
359 switch( dir ) { 360 switch( dir ) {
360 /* forward */ 361 /* forward */
361 case 0: // FIXME: Not a good style to use magic numbers here (eilers) 362 case 0: // FIXME: Not a good style to use magic numbers here (eilers)
362 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) { 363 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) {
363 qWarning("size %d %d", size, ints[i] ); 364 qWarning("size %d %d", size, ints[i] );
364 search[size] = ints[i]; 365 search[size] = ints[i];
365 size++; 366 size++;
366 } 367 }
367 break; 368 break;
368 /* reverse */ 369 /* reverse */
369 case 1: // FIXME: Not a good style to use magic numbers here (eilers) 370 case 1: // FIXME: Not a good style to use magic numbers here (eilers)
370 for (uint i = cur; i != 0 && size < CACHE; i-- ) { 371 for (uint i = cur; i != 0 && size < CACHE; i-- ) {
371 search[size] = ints[i]; 372 search[size] = ints[i];
372 size++; 373 size++;
373 } 374 }
374 break; 375 break;
375 } 376 }
376 search.resize( size ); 377 search.resize( size );
377 FindQuery query( search ); 378 FindQuery query( search );
378 OSQLResult res = m_driver->query( &query ); 379 OSQLResult res = m_driver->query( &query );
379 if ( res.state() != OSQLResult::Success ) 380 if ( res.state() != OSQLResult::Success )
380 return to; 381 return to;
381 382
382 return todo( res ); 383 return todo( res );
383} 384}
384void OTodoAccessBackendSQL::clear() { 385void OTodoAccessBackendSQL::clear() {
385 ClearQuery cle; 386 ClearQuery cle;
386 OSQLResult res = m_driver->query( &cle ); 387 OSQLResult res = m_driver->query( &cle );
387 CreateQuery qu; 388 CreateQuery qu;
388 res = m_driver->query(&qu); 389 res = m_driver->query(&qu);
389} 390}
390bool OTodoAccessBackendSQL::add( const OTodo& t) { 391bool OTodoAccessBackendSQL::add( const OTodo& t) {
391 InsertQuery ins( t ); 392 InsertQuery ins( t );
392 OSQLResult res = m_driver->query( &ins ); 393 OSQLResult res = m_driver->query( &ins );
393 394
394 if ( res.state() == OSQLResult::Failure ) 395 if ( res.state() == OSQLResult::Failure )
395 return false; 396 return false;
396 int c = m_uids.count(); 397 int c = m_uids.count();
397 m_uids.resize( c+1 ); 398 m_uids.resize( c+1 );
398 m_uids[c] = t.uid(); 399 m_uids[c] = t.uid();
399 400
400 return true; 401 return true;
401} 402}
402bool OTodoAccessBackendSQL::remove( int uid ) { 403bool OTodoAccessBackendSQL::remove( int uid ) {
403 RemoveQuery rem( uid ); 404 RemoveQuery rem( uid );
404 OSQLResult res = m_driver->query(&rem ); 405 OSQLResult res = m_driver->query(&rem );
405 406
406 if ( res.state() == OSQLResult::Failure ) 407 if ( res.state() == OSQLResult::Failure )
407 return false; 408 return false;
408 409
409 m_dirty = true; 410 m_dirty = true;
410 return true; 411 return true;
411} 412}
412/* 413/*
413 * FIXME better set query 414 * FIXME better set query
414 * but we need the cache for that 415 * but we need the cache for that
415 * now we remove 416 * now we remove
416 */ 417 */
417bool OTodoAccessBackendSQL::replace( const OTodo& t) { 418bool OTodoAccessBackendSQL::replace( const OTodo& t) {
418 remove( t.uid() ); 419 remove( t.uid() );
419 bool b= add(t); 420 bool b= add(t);
420 m_dirty = false; // we changed some stuff but the UID stayed the same 421 m_dirty = false; // we changed some stuff but the UID stayed the same
421 return b; 422 return b;
422} 423}
423QArray<int> OTodoAccessBackendSQL::overDue() { 424QArray<int> OTodoAccessBackendSQL::overDue() {
424 OverDueQuery qu; 425 OverDueQuery qu;
425 return uids( m_driver->query(&qu ) ); 426 return uids( m_driver->query(&qu ) );
426} 427}
427QArray<int> OTodoAccessBackendSQL::effectiveToDos( const QDate& s, 428QArray<int> OTodoAccessBackendSQL::effectiveToDos( const QDate& s,
428 const QDate& t, 429 const QDate& t,
429 bool u) { 430 bool u) {
430 EffQuery ef(s, t, u ); 431 EffQuery ef(s, t, u );
431 return uids (m_driver->query(&ef) ); 432 return uids (m_driver->query(&ef) );
432} 433}
433/* 434/*
434 * 435 *
435 */ 436 */
436QArray<int> OTodoAccessBackendSQL::sorted( bool asc, int sortOrder, 437QArray<int> OTodoAccessBackendSQL::sorted( bool asc, int sortOrder,
437 int sortFilter, int cat ) { 438 int sortFilter, int cat ) {
438 qWarning("sorted %d, %d", asc, sortOrder ); 439 qWarning("sorted %d, %d", asc, sortOrder );
439 QString query; 440 QString query;
440 query = "select uid from todolist WHERE "; 441 query = "select uid from todolist WHERE ";
441 442
442 /* 443 /*
443 * Sort Filter stuff 444 * Sort Filter stuff
444 * not that straight forward 445 * not that straight forward
445 * FIXME: Replace magic numbers 446 * FIXME: Replace magic numbers
446 * 447 *
447 */ 448 */
448 /* Category */ 449 /* Category */
449 if ( sortFilter & 1 ) { 450 if ( sortFilter & 1 ) {
450 QString str; 451 QString str;
451 if (cat != 0 ) str = QString::number( cat ); 452 if (cat != 0 ) str = QString::number( cat );
452 query += " categories like '%" +str+"%' AND"; 453 query += " categories like '%" +str+"%' AND";
453 } 454 }
454 /* Show only overdue */ 455 /* Show only overdue */
455 if ( sortFilter & 2 ) { 456 if ( sortFilter & 2 ) {
456 QDate date = QDate::currentDate(); 457 QDate date = QDate::currentDate();
457 QString due; 458 QString due;
458 QString base; 459 QString base;
459 base = QString("DueDate <= '%1-%2-%3' AND completed = 0").arg( date.year() ).arg( date.month() ).arg( date.day() ); 460 base = QString("DueDate <= '%1-%2-%3' AND completed = 0").arg( date.year() ).arg( date.month() ).arg( date.day() );
460 query += " " + base + " AND"; 461 query += " " + base + " AND";
461 } 462 }
462 /* not show completed */ 463 /* not show completed */
463 if ( sortFilter & 4 ) { 464 if ( sortFilter & 4 ) {
464 query += " completed = 0 AND"; 465 query += " completed = 0 AND";
465 }else{ 466 }else{
466 query += " ( completed = 1 OR completed = 0) AND"; 467 query += " ( completed = 1 OR completed = 0) AND";
467 } 468 }
468 /* srtip the end */ 469 /* srtip the end */
469 query = query.remove( query.length()-3, 3 ); 470 query = query.remove( query.length()-3, 3 );
470 471
471 472
472 /* 473 /*
473 * sort order stuff 474 * sort order stuff
474 * quite straight forward 475 * quite straight forward
475 */ 476 */
476 query += "ORDER BY "; 477 query += "ORDER BY ";
477 switch( sortOrder ) { 478 switch( sortOrder ) {
478 /* completed */ 479 /* completed */
479 case 0: 480 case 0:
480 query += "completed"; 481 query += "completed";
481 break; 482 break;
482 case 1: 483 case 1:
483 query += "priority"; 484 query += "priority";
484 break; 485 break;
485 case 2: 486 case 2:
486 query += "summary"; 487 query += "summary";
487 break; 488 break;
488 case 3: 489 case 3:
489 query += "DueDate"; 490 query += "DueDate";
490 break; 491 break;
491 } 492 }
492 493
493 if ( !asc ) { 494 if ( !asc ) {
494 qWarning("not ascending!"); 495 qWarning("not ascending!");
495 query += " DESC"; 496 query += " DESC";
496 } 497 }
497 498
498 qWarning( query ); 499 qWarning( query );
499 OSQLRawQuery raw(query ); 500 OSQLRawQuery raw(query );
500 return uids( m_driver->query(&raw) ); 501 return uids( m_driver->query(&raw) );
501} 502}
502bool OTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{ 503bool OTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{
503 if ( str == "0-0-0" ) 504 if ( str == "0-0-0" )
504 return false; 505 return false;
505 else{ 506 else{
506 int day, year, month; 507 int day, year, month;
507 QStringList list = QStringList::split("-", str ); 508 QStringList list = QStringList::split("-", str );
508 year = list[0].toInt(); 509 year = list[0].toInt();
509 month = list[1].toInt(); 510 month = list[1].toInt();
510 day = list[2].toInt(); 511 day = list[2].toInt();
511 da.setYMD( year, month, day ); 512 da.setYMD( year, month, day );
512 return true; 513 return true;
513 } 514 }
514} 515}
515OTodo OTodoAccessBackendSQL::todo( const OSQLResult& res) const{ 516OTodo OTodoAccessBackendSQL::todo( const OSQLResult& res) const{
516 if ( res.state() == OSQLResult::Failure ) { 517 if ( res.state() == OSQLResult::Failure ) {
517 OTodo to; 518 OTodo to;
518 return to; 519 return to;
519 } 520 }
520 521
521 OSQLResultItem::ValueList list = res.results(); 522 OSQLResultItem::ValueList list = res.results();
522 OSQLResultItem::ValueList::Iterator it = list.begin(); 523 OSQLResultItem::ValueList::Iterator it = list.begin();
523 qWarning("todo1"); 524 qWarning("todo1");
524 OTodo to = todo( (*it) ); 525 OTodo to = todo( (*it) );
525 cache( to ); 526 cache( to );
526 ++it; 527 ++it;
527 528
diff --git a/libopie/pim/otodoaccesssql.h b/libopie/pim/otodoaccesssql.h
index 1c55567..72214de 100644
--- a/libopie/pim/otodoaccesssql.h
+++ b/libopie/pim/otodoaccesssql.h
@@ -1,56 +1,61 @@
1#ifndef OPIE_PIM_ACCESS_SQL_H 1#ifndef OPIE_PIM_ACCESS_SQL_H
2#define OPIE_PIM_ACCESS_SQL_H 2#define OPIE_PIM_ACCESS_SQL_H
3 3
4#include <qasciidict.h> 4#include <qasciidict.h>
5 5
6#include "otodoaccessbackend.h" 6#include "otodoaccessbackend.h"
7 7
8namespace Opie{
9namespace DB {
8class OSQLDriver; 10class OSQLDriver;
9class OSQLResult; 11class OSQLResult;
10class OSQLResultItem; 12class OSQLResultItem;
13}
14}
15
11class OTodoAccessBackendSQL : public OTodoAccessBackend { 16class OTodoAccessBackendSQL : public OTodoAccessBackend {
12public: 17public:
13 OTodoAccessBackendSQL( const QString& file ); 18 OTodoAccessBackendSQL( const QString& file );
14 ~OTodoAccessBackendSQL(); 19 ~OTodoAccessBackendSQL();
15 20
16 bool load(); 21 bool load();
17 bool reload(); 22 bool reload();
18 bool save(); 23 bool save();
19 QArray<int> allRecords()const; 24 QArray<int> allRecords()const;
20 25
21 QArray<int> queryByExample( const OTodo& t, int settings, const QDateTime& d = QDateTime() ); 26 QArray<int> queryByExample( const OTodo& t, int settings, const QDateTime& d = QDateTime() );
22 OTodo find(int uid)const; 27 OTodo find(int uid)const;
23 OTodo find(int uid, const QArray<int>&, uint cur, Frontend::CacheDirection )const; 28 OTodo find(int uid, const QArray<int>&, uint cur, Frontend::CacheDirection )const;
24 void clear(); 29 void clear();
25 bool add( const OTodo& t ); 30 bool add( const OTodo& t );
26 bool remove( int uid ); 31 bool remove( int uid );
27 bool replace( const OTodo& t ); 32 bool replace( const OTodo& t );
28 33
29 QArray<int> overDue(); 34 QArray<int> overDue();
30 QArray<int> effectiveToDos( const QDate& start, 35 QArray<int> effectiveToDos( const QDate& start,
31 const QDate& end, bool includeNoDates ); 36 const QDate& end, bool includeNoDates );
32 QArray<int> sorted(bool asc, int sortOrder, int sortFilter, int cat ); 37 QArray<int> sorted(bool asc, int sortOrder, int sortFilter, int cat );
33 38
34 QBitArray supports()const; 39 QBitArray supports()const;
35 QArray<int> matchRegexp( const QRegExp &r ) const; 40 QArray<int> matchRegexp( const QRegExp &r ) const;
36 void removeAllCompleted(); 41 void removeAllCompleted();
37 42
38 43
39private: 44private:
40 void update()const; 45 void update()const;
41 void fillDict(); 46 void fillDict();
42 inline bool date( QDate& date, const QString& )const; 47 inline bool date( QDate& date, const QString& )const;
43 inline OTodo todo( const OSQLResult& )const; 48 inline OTodo todo( const Opie::DB::OSQLResult& )const;
44 inline OTodo todo( OSQLResultItem& )const; 49 inline OTodo todo( Opie::DB::OSQLResultItem& )const;
45 inline QArray<int> uids( const OSQLResult& )const; 50 inline QArray<int> uids( const Opie::DB::OSQLResult& )const;
46 OTodo todo( int uid )const; 51 OTodo todo( int uid )const;
47 QBitArray sup() const; 52 QBitArray sup() const;
48 53
49 QAsciiDict<int> m_dict; 54 QAsciiDict<int> m_dict;
50 OSQLDriver* m_driver; 55 Opie::DB::OSQLDriver* m_driver;
51 QArray<int> m_uids; 56 QArray<int> m_uids;
52 bool m_dirty : 1; 57 bool m_dirty : 1;
53}; 58};
54 59
55 60
56#endif 61#endif