Diffstat (limited to 'libopie/pim/ocontactaccessbackend_sql.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie/pim/ocontactaccessbackend_sql.cpp | 945 |
1 files changed, 0 insertions, 945 deletions
diff --git a/libopie/pim/ocontactaccessbackend_sql.cpp b/libopie/pim/ocontactaccessbackend_sql.cpp deleted file mode 100644 index d20df56..0000000 --- a/libopie/pim/ocontactaccessbackend_sql.cpp +++ b/dev/null | |||
@@ -1,945 +0,0 @@ | |||
1 | /* | ||
2 | * SQL Backend for the OPIE-Contact Database. | ||
3 | * | ||
4 | * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) | ||
5 | * | ||
6 | * ===================================================================== | ||
7 | *This program is free software; you can redistribute it and/or | ||
8 | *modify it under the terms of the GNU Library General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2 of the License, or (at your option) any later version. | ||
11 | * ===================================================================== | ||
12 | * ===================================================================== | ||
13 | * Version: $Id$ | ||
14 | * ===================================================================== | ||
15 | * History: | ||
16 | * $Log$ | ||
17 | * Revision 1.5 2004/03/14 13:50:35 alwin | ||
18 | * namespace correction | ||
19 | * | ||
20 | * Revision 1.4 2003/12/22 10:19:26 eilers | ||
21 | * 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 | ||
23 | * whole stuff. | ||
24 | * Thus, PIM-Database backend is finished, but highly experimental. And some | ||
25 | * parts are still generic. For instance, the "queryByExample()" methods are | ||
26 | * not (or not fully) implemented. Todo: custom-entries not stored. | ||
27 | * The big show stopper: matchRegExp() (needed by OpieSearch) needs regular | ||
28 | * expression search in the database, which is not supported by sqlite ! | ||
29 | * Therefore we need either an extended sqlite or a workaround which would | ||
30 | * be very slow and memory consuming.. | ||
31 | * | ||
32 | * Revision 1.3 2003/12/08 15:18:10 eilers | ||
33 | * Committing unfinished sql implementation before merging to libopie2 starts.. | ||
34 | * | ||
35 | * Revision 1.2 2003/09/29 07:44:26 eilers | ||
36 | * Improvement of PIM-SQL Databases, but search queries are still limited. | ||
37 | * Addressbook: Changed table layout. Now, we just need 1/3 of disk-space. | ||
38 | * Todo: Started to add new attributes. Some type conversions missing. | ||
39 | * | ||
40 | * Revision 1.1 2003/09/22 14:31:16 eilers | ||
41 | * Added first experimental incarnation of sql-backend for addressbook. | ||
42 | * Some modifications to be able to compile the todo sql-backend. | ||
43 | * A lot of changes fill follow... | ||
44 | * | ||
45 | */ | ||
46 | |||
47 | #include "ocontactaccessbackend_sql.h" | ||
48 | |||
49 | #include <qarray.h> | ||
50 | #include <qdatetime.h> | ||
51 | #include <qstringlist.h> | ||
52 | |||
53 | #include <qpe/global.h> | ||
54 | #include <qpe/recordfields.h> | ||
55 | |||
56 | #include <opie/ocontactfields.h> | ||
57 | #include <opie/oconversion.h> | ||
58 | #include <opie2/osqldriver.h> | ||
59 | #include <opie2/osqlresult.h> | ||
60 | #include <opie2/osqlmanager.h> | ||
61 | #include <opie2/osqlquery.h> | ||
62 | |||
63 | using namespace Opie::DB; | ||
64 | |||
65 | |||
66 | // If defined, we use a horizontal table ( uid, attr1, attr2, attr3, ..., attrn ) instead | ||
67 | // vertical like "uid, type, value". | ||
68 | // DON'T DEACTIVATE THIS DEFINE IN PRODUCTIVE ENVIRONMENTS !! | ||
69 | #define __STORE_HORIZONTAL_ | ||
70 | |||
71 | // Distinct loading is not very fast. If I expect that every person has just | ||
72 | // one (and always one) 'Last Name', I can request all uid's for existing lastnames, | ||
73 | // which is faster.. | ||
74 | // But this may not be true for all entries, like company contacts.. | ||
75 | // The current AddressBook application handles this problem, but other may not.. (eilers) | ||
76 | #define __USE_SUPERFAST_LOADQUERY | ||
77 | |||
78 | |||
79 | /* | ||
80 | * Implementation of used query types | ||
81 | * CREATE query | ||
82 | * LOAD query | ||
83 | * INSERT | ||
84 | * REMOVE | ||
85 | * CLEAR | ||
86 | */ | ||
87 | namespace { | ||
88 | /** | ||
89 | * CreateQuery for the Todolist Table | ||
90 | */ | ||
91 | class CreateQuery : public OSQLQuery { | ||
92 | public: | ||
93 | CreateQuery(); | ||
94 | ~CreateQuery(); | ||
95 | QString query()const; | ||
96 | }; | ||
97 | |||
98 | /** | ||
99 | * Clears (delete) a Table | ||
100 | */ | ||
101 | class ClearQuery : public OSQLQuery { | ||
102 | public: | ||
103 | ClearQuery(); | ||
104 | ~ClearQuery(); | ||
105 | QString query()const; | ||
106 | |||
107 | }; | ||
108 | |||
109 | |||
110 | /** | ||
111 | * LoadQuery | ||
112 | * this one queries for all uids | ||
113 | */ | ||
114 | class LoadQuery : public OSQLQuery { | ||
115 | public: | ||
116 | LoadQuery(); | ||
117 | ~LoadQuery(); | ||
118 | QString query()const; | ||
119 | }; | ||
120 | |||
121 | /** | ||
122 | * inserts/adds a OContact to the table | ||
123 | */ | ||
124 | class InsertQuery : public OSQLQuery { | ||
125 | public: | ||
126 | InsertQuery(const OContact& ); | ||
127 | ~InsertQuery(); | ||
128 | QString query()const; | ||
129 | private: | ||
130 | OContact m_contact; | ||
131 | }; | ||
132 | |||
133 | |||
134 | /** | ||
135 | * removes one from the table | ||
136 | */ | ||
137 | class RemoveQuery : public OSQLQuery { | ||
138 | public: | ||
139 | RemoveQuery(int uid ); | ||
140 | ~RemoveQuery(); | ||
141 | QString query()const; | ||
142 | private: | ||
143 | int m_uid; | ||
144 | }; | ||
145 | |||
146 | /** | ||
147 | * a find query for noncustom elements | ||
148 | */ | ||
149 | class FindQuery : public OSQLQuery { | ||
150 | public: | ||
151 | FindQuery(int uid); | ||
152 | FindQuery(const QArray<int>& ); | ||
153 | ~FindQuery(); | ||
154 | QString query()const; | ||
155 | private: | ||
156 | QString single()const; | ||
157 | QString multi()const; | ||
158 | QArray<int> m_uids; | ||
159 | int m_uid; | ||
160 | }; | ||
161 | |||
162 | /** | ||
163 | * a find query for custom elements | ||
164 | */ | ||
165 | class FindCustomQuery : public OSQLQuery { | ||
166 | public: | ||
167 | FindCustomQuery(int uid); | ||
168 | FindCustomQuery(const QArray<int>& ); | ||
169 | ~FindCustomQuery(); | ||
170 | QString query()const; | ||
171 | private: | ||
172 | QString single()const; | ||
173 | QString multi()const; | ||
174 | QArray<int> m_uids; | ||
175 | int m_uid; | ||
176 | }; | ||
177 | |||
178 | |||
179 | |||
180 | // We using three tables to store the information: | ||
181 | // 1. addressbook : It contains General information about the contact (non custom) | ||
182 | // 2. custom_data : Not official supported entries | ||
183 | // All tables are connected by the uid of the contact. | ||
184 | // Maybe I should add a table for meta-information ? | ||
185 | CreateQuery::CreateQuery() : OSQLQuery() {} | ||
186 | CreateQuery::~CreateQuery() {} | ||
187 | QString CreateQuery::query()const { | ||
188 | QString qu; | ||
189 | #ifdef __STORE_HORIZONTAL_ | ||
190 | |||
191 | qu += "create table addressbook( uid PRIMARY KEY "; | ||
192 | |||
193 | QStringList fieldList = OContactFields::untrfields( false ); | ||
194 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ | ||
195 | qu += QString( ",\"%1\" VARCHAR(10)" ).arg( *it ); | ||
196 | } | ||
197 | qu += " );"; | ||
198 | |||
199 | qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );"; | ||
200 | |||
201 | #else | ||
202 | |||
203 | qu += "create table addressbook( 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) );"; | ||
205 | // qu += "create table dates( uid PRIMARY KEY, type, day, month, year, hour, minute, second );"; | ||
206 | |||
207 | #endif // __STORE_HORIZONTAL_ | ||
208 | return qu; | ||
209 | } | ||
210 | |||
211 | ClearQuery::ClearQuery() | ||
212 | : OSQLQuery() {} | ||
213 | ClearQuery::~ClearQuery() {} | ||
214 | QString ClearQuery::query()const { | ||
215 | QString qu = "drop table addressbook;"; | ||
216 | qu += "drop table custom_data;"; | ||
217 | // qu += "drop table dates;"; | ||
218 | return qu; | ||
219 | } | ||
220 | |||
221 | |||
222 | LoadQuery::LoadQuery() : OSQLQuery() {} | ||
223 | LoadQuery::~LoadQuery() {} | ||
224 | QString LoadQuery::query()const { | ||
225 | QString qu; | ||
226 | #ifdef __STORE_HORIZONTAL_ | ||
227 | qu += "select uid from addressbook"; | ||
228 | #else | ||
229 | # ifndef __USE_SUPERFAST_LOADQUERY | ||
230 | qu += "select distinct uid from addressbook"; | ||
231 | # else | ||
232 | qu += "select uid from addressbook where type = 'Last Name'"; | ||
233 | # endif // __USE_SUPERFAST_LOADQUERY | ||
234 | #endif // __STORE_HORIZONTAL_ | ||
235 | |||
236 | return qu; | ||
237 | } | ||
238 | |||
239 | |||
240 | InsertQuery::InsertQuery( const OContact& contact ) | ||
241 | : OSQLQuery(), m_contact( contact ) { | ||
242 | } | ||
243 | |||
244 | InsertQuery::~InsertQuery() { | ||
245 | } | ||
246 | |||
247 | /* | ||
248 | * converts from a OContact to a query | ||
249 | */ | ||
250 | QString InsertQuery::query()const{ | ||
251 | |||
252 | #ifdef __STORE_HORIZONTAL_ | ||
253 | QString qu; | ||
254 | qu += "insert into addressbook VALUES( " + | ||
255 | QString::number( m_contact.uid() ); | ||
256 | |||
257 | // Get all information out of the contact-class | ||
258 | // Remember: The category is stored in contactMap, too ! | ||
259 | QMap<int, QString> contactMap = m_contact.toMap(); | ||
260 | |||
261 | QStringList fieldList = OContactFields::untrfields( false ); | ||
262 | QMap<QString, int> translate = OContactFields::untrFieldsToId(); | ||
263 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ | ||
264 | // Convert Column-String to Id and get value for this id.. | ||
265 | // Hmmm.. Maybe not very cute solution.. | ||
266 | int id = translate[*it]; | ||
267 | switch ( id ){ | ||
268 | case Qtopia::Birthday:{ | ||
269 | // These entries should stored in a special format | ||
270 | // year-month-day | ||
271 | QDate day = m_contact.birthday(); | ||
272 | if ( day.isValid() ){ | ||
273 | qu += QString(",\"%1-%2-%3\"") | ||
274 | .arg( day.year() ) | ||
275 | .arg( day.month() ) | ||
276 | .arg( day.day() ); | ||
277 | } else { | ||
278 | qu += ",\"\""; | ||
279 | } | ||
280 | } | ||
281 | break; | ||
282 | case Qtopia::Anniversary:{ | ||
283 | // These entries should stored in a special format | ||
284 | // year-month-day | ||
285 | QDate day = m_contact.anniversary(); | ||
286 | if ( day.isValid() ){ | ||
287 | qu += QString(",\"%1-%2-%3\"") | ||
288 | .arg( day.year() ) | ||
289 | .arg( day.month() ) | ||
290 | .arg( day.day() ); | ||
291 | } else { | ||
292 | qu += ",\"\""; | ||
293 | } | ||
294 | } | ||
295 | break; | ||
296 | |||
297 | default: | ||
298 | qu += QString( ",\"%1\"" ).arg( contactMap[id] ); | ||
299 | } | ||
300 | } | ||
301 | qu += " );"; | ||
302 | |||
303 | |||
304 | #else | ||
305 | // Get all information out of the contact-class | ||
306 | // Remember: The category is stored in contactMap, too ! | ||
307 | QMap<int, QString> contactMap = m_contact.toMap(); | ||
308 | |||
309 | QMap<QString, QString> addressbook_db; | ||
310 | |||
311 | // Get the translation from the ID to the String | ||
312 | QMap<int, QString> transMap = OContactFields::idToUntrFields(); | ||
313 | |||
314 | for( QMap<int, QString>::Iterator it = contactMap.begin(); | ||
315 | it != contactMap.end(); ++it ){ | ||
316 | switch ( it.key() ){ | ||
317 | case Qtopia::Birthday:{ | ||
318 | // These entries should stored in a special format | ||
319 | // year-month-day | ||
320 | QDate day = m_contact.birthday(); | ||
321 | addressbook_db.insert( transMap[it.key()], | ||
322 | QString("%1-%2-%3") | ||
323 | .arg( day.year() ) | ||
324 | .arg( day.month() ) | ||
325 | .arg( day.day() ) ); | ||
326 | } | ||
327 | break; | ||
328 | case Qtopia::Anniversary:{ | ||
329 | // These entries should stored in a special format | ||
330 | // year-month-day | ||
331 | QDate day = m_contact.anniversary(); | ||
332 | addressbook_db.insert( transMap[it.key()], | ||
333 | QString("%1-%2-%3") | ||
334 | .arg( day.year() ) | ||
335 | .arg( day.month() ) | ||
336 | .arg( day.day() ) ); | ||
337 | } | ||
338 | break; | ||
339 | case Qtopia::AddressUid: // Ignore UID | ||
340 | break; | ||
341 | default: // Translate id to String | ||
342 | addressbook_db.insert( transMap[it.key()], it.data() ); | ||
343 | break; | ||
344 | } | ||
345 | |||
346 | } | ||
347 | |||
348 | // Now convert this whole stuff into a SQL String, beginning with | ||
349 | // the addressbook table.. | ||
350 | QString qu; | ||
351 | // qu += "begin transaction;"; | ||
352 | int id = 0; | ||
353 | for( QMap<QString, QString>::Iterator it = addressbook_db.begin(); | ||
354 | it != addressbook_db.end(); ++it ){ | ||
355 | qu += "insert into addressbook VALUES(" | ||
356 | + QString::number( m_contact.uid() ) | ||
357 | + "," | ||
358 | + QString::number( id++ ) | ||
359 | + ",'" | ||
360 | + it.key() //.latin1() | ||
361 | + "'," | ||
362 | + "0" // Priority for future enhancements | ||
363 | + ",'" | ||
364 | + it.data() //.latin1() | ||
365 | + "');"; | ||
366 | } | ||
367 | |||
368 | #endif //__STORE_HORIZONTAL_ | ||
369 | // Now add custom data.. | ||
370 | #ifdef __STORE_HORIZONTAL_ | ||
371 | int id = 0; | ||
372 | #endif | ||
373 | id = 0; | ||
374 | QMap<QString, QString> customMap = m_contact.toExtraMap(); | ||
375 | for( QMap<QString, QString>::Iterator it = customMap.begin(); | ||
376 | it != customMap.end(); ++it ){ | ||
377 | qu += "insert into custom_data VALUES(" | ||
378 | + QString::number( m_contact.uid() ) | ||
379 | + "," | ||
380 | + QString::number( id++ ) | ||
381 | + ",'" | ||
382 | + it.key() //.latin1() | ||
383 | + "'," | ||
384 | + "0" // Priority for future enhancements | ||
385 | + ",'" | ||
386 | + it.data() //.latin1() | ||
387 | + "');"; | ||
388 | } | ||
389 | // qu += "commit;"; | ||
390 | qWarning("add %s", qu.latin1() ); | ||
391 | return qu; | ||
392 | } | ||
393 | |||
394 | |||
395 | RemoveQuery::RemoveQuery(int uid ) | ||
396 | : OSQLQuery(), m_uid( uid ) {} | ||
397 | RemoveQuery::~RemoveQuery() {} | ||
398 | QString RemoveQuery::query()const { | ||
399 | QString qu = "DELETE from addressbook where uid = " | ||
400 | + QString::number(m_uid) + ";"; | ||
401 | qu += "DELETE from custom_data where uid = " | ||
402 | + QString::number(m_uid) + ";"; | ||
403 | return qu; | ||
404 | } | ||
405 | |||
406 | |||
407 | |||
408 | |||
409 | FindQuery::FindQuery(int uid) | ||
410 | : OSQLQuery(), m_uid( uid ) { | ||
411 | } | ||
412 | FindQuery::FindQuery(const QArray<int>& ints) | ||
413 | : OSQLQuery(), m_uids( ints ){ | ||
414 | } | ||
415 | FindQuery::~FindQuery() { | ||
416 | } | ||
417 | QString FindQuery::query()const{ | ||
418 | // if ( m_uids.count() == 0 ) | ||
419 | return single(); | ||
420 | } | ||
421 | /* | ||
422 | else | ||
423 | return multi(); | ||
424 | } | ||
425 | QString FindQuery::multi()const { | ||
426 | QString qu = "select uid, type, value from addressbook where"; | ||
427 | for (uint i = 0; i < m_uids.count(); i++ ) { | ||
428 | qu += " UID = " + QString::number( m_uids[i] ) + " OR"; | ||
429 | } | ||
430 | qu.remove( qu.length()-2, 2 ); // Hmmmm.. | ||
431 | return qu; | ||
432 | } | ||
433 | */ | ||
434 | #ifdef __STORE_HORIZONTAL_ | ||
435 | QString FindQuery::single()const{ | ||
436 | QString qu = "select *"; | ||
437 | qu += " from addressbook where uid = " + QString::number(m_uid); | ||
438 | |||
439 | // qWarning("find query: %s", qu.latin1() ); | ||
440 | return qu; | ||
441 | } | ||
442 | #else | ||
443 | QString FindQuery::single()const{ | ||
444 | QString qu = "select uid, type, value from addressbook where uid = "; | ||
445 | qu += QString::number(m_uid); | ||
446 | return qu; | ||
447 | } | ||
448 | #endif | ||
449 | |||
450 | |||
451 | FindCustomQuery::FindCustomQuery(int uid) | ||
452 | : OSQLQuery(), m_uid( uid ) { | ||
453 | } | ||
454 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) | ||
455 | : OSQLQuery(), m_uids( ints ){ | ||
456 | } | ||
457 | FindCustomQuery::~FindCustomQuery() { | ||
458 | } | ||
459 | QString FindCustomQuery::query()const{ | ||
460 | // if ( m_uids.count() == 0 ) | ||
461 | return single(); | ||
462 | } | ||
463 | QString FindCustomQuery::single()const{ | ||
464 | QString qu = "select uid, type, value from custom_data where uid = "; | ||
465 | qu += QString::number(m_uid); | ||
466 | return qu; | ||
467 | } | ||
468 | |||
469 | }; | ||
470 | |||
471 | |||
472 | /* --------------------------------------------------------------------------- */ | ||
473 | |||
474 | OContactAccessBackend_SQL::OContactAccessBackend_SQL ( const QString& /* appname */, | ||
475 | const QString& filename ): | ||
476 | OContactAccessBackend(), m_changed(false), m_driver( NULL ) | ||
477 | { | ||
478 | qWarning("C'tor OContactAccessBackend_SQL starts"); | ||
479 | QTime t; | ||
480 | t.start(); | ||
481 | |||
482 | /* Expecting to access the default filename if nothing else is set */ | ||
483 | if ( filename.isEmpty() ){ | ||
484 | m_fileName = Global::applicationFileName( "addressbook","addressbook.db" ); | ||
485 | } else | ||
486 | m_fileName = filename; | ||
487 | |||
488 | // Get the standart sql-driver from the OSQLManager.. | ||
489 | OSQLManager man; | ||
490 | m_driver = man.standard(); | ||
491 | m_driver->setUrl( m_fileName ); | ||
492 | |||
493 | load(); | ||
494 | |||
495 | qWarning("C'tor OContactAccessBackend_SQL ends: %d ms", t.elapsed() ); | ||
496 | } | ||
497 | |||
498 | OContactAccessBackend_SQL::~OContactAccessBackend_SQL () | ||
499 | { | ||
500 | if( m_driver ) | ||
501 | delete m_driver; | ||
502 | } | ||
503 | |||
504 | bool OContactAccessBackend_SQL::load () | ||
505 | { | ||
506 | if (!m_driver->open() ) | ||
507 | return false; | ||
508 | |||
509 | // Don't expect that the database exists. | ||
510 | // It is save here to create the table, even if it | ||
511 | // do exist. ( Is that correct for all databases ?? ) | ||
512 | CreateQuery creat; | ||
513 | OSQLResult res = m_driver->query( &creat ); | ||
514 | |||
515 | update(); | ||
516 | |||
517 | return true; | ||
518 | |||
519 | } | ||
520 | |||
521 | bool OContactAccessBackend_SQL::reload() | ||
522 | { | ||
523 | return load(); | ||
524 | } | ||
525 | |||
526 | bool OContactAccessBackend_SQL::save() | ||
527 | { | ||
528 | return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) | ||
529 | } | ||
530 | |||
531 | |||
532 | void OContactAccessBackend_SQL::clear () | ||
533 | { | ||
534 | ClearQuery cle; | ||
535 | OSQLResult res = m_driver->query( &cle ); | ||
536 | |||
537 | reload(); | ||
538 | } | ||
539 | |||
540 | bool OContactAccessBackend_SQL::wasChangedExternally() | ||
541 | { | ||
542 | return false; | ||
543 | } | ||
544 | |||
545 | QArray<int> OContactAccessBackend_SQL::allRecords() const | ||
546 | { | ||
547 | |||
548 | // FIXME: Think about cute handling of changed tables.. | ||
549 | // Thus, we don't have to call update here... | ||
550 | if ( m_changed ) | ||
551 | ((OContactAccessBackend_SQL*)this)->update(); | ||
552 | |||
553 | return m_uids; | ||
554 | } | ||
555 | |||
556 | bool OContactAccessBackend_SQL::add ( const OContact &newcontact ) | ||
557 | { | ||
558 | InsertQuery ins( newcontact ); | ||
559 | OSQLResult res = m_driver->query( &ins ); | ||
560 | |||
561 | if ( res.state() == OSQLResult::Failure ) | ||
562 | return false; | ||
563 | |||
564 | int c = m_uids.count(); | ||
565 | m_uids.resize( c+1 ); | ||
566 | m_uids[c] = newcontact.uid(); | ||
567 | |||
568 | return true; | ||
569 | } | ||
570 | |||
571 | |||
572 | bool OContactAccessBackend_SQL::remove ( int uid ) | ||
573 | { | ||
574 | RemoveQuery rem( uid ); | ||
575 | OSQLResult res = m_driver->query(&rem ); | ||
576 | |||
577 | if ( res.state() == OSQLResult::Failure ) | ||
578 | return false; | ||
579 | |||
580 | m_changed = true; | ||
581 | |||
582 | return true; | ||
583 | } | ||
584 | |||
585 | bool OContactAccessBackend_SQL::replace ( const OContact &contact ) | ||
586 | { | ||
587 | if ( !remove( contact.uid() ) ) | ||
588 | return false; | ||
589 | |||
590 | return add( contact ); | ||
591 | } | ||
592 | |||
593 | |||
594 | OContact OContactAccessBackend_SQL::find ( int uid ) const | ||
595 | { | ||
596 | qWarning("OContactAccessBackend_SQL::find()"); | ||
597 | QTime t; | ||
598 | t.start(); | ||
599 | |||
600 | OContact retContact( requestNonCustom( uid ) ); | ||
601 | retContact.setExtraMap( requestCustom( uid ) ); | ||
602 | |||
603 | qWarning("OContactAccessBackend_SQL::find() needed: %d ms", t.elapsed() ); | ||
604 | return retContact; | ||
605 | } | ||
606 | |||
607 | |||
608 | |||
609 | QArray<int> OContactAccessBackend_SQL::queryByExample ( const OContact &query, int settings, const QDateTime& d = QDateTime() ) | ||
610 | { | ||
611 | QString qu = "SELECT uid FROM addressbook WHERE"; | ||
612 | |||
613 | QMap<int, QString> queryFields = query.toMap(); | ||
614 | QStringList fieldList = OContactFields::untrfields( false ); | ||
615 | QMap<QString, int> translate = OContactFields::untrFieldsToId(); | ||
616 | |||
617 | // Convert every filled field to a SQL-Query | ||
618 | bool isAnyFieldSelected = false; | ||
619 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ | ||
620 | int id = translate[*it]; | ||
621 | QString queryStr = queryFields[id]; | ||
622 | if ( !queryStr.isEmpty() ){ | ||
623 | isAnyFieldSelected = true; | ||
624 | switch( id ){ | ||
625 | default: | ||
626 | // Switching between case sensitive and insensitive... | ||
627 | // LIKE is not case sensitive, GLOB is case sensitive | ||
628 | // Do exist a better solution to switch this ? | ||
629 | if ( settings & OContactAccess::IgnoreCase ) | ||
630 | qu += "(\"" + *it + "\"" + " LIKE " + "'" | ||
631 | + queryStr.replace(QRegExp("\\*"),"%") + "'" + ") AND "; | ||
632 | else | ||
633 | qu += "(\"" + *it + "\"" + " GLOB " + "'" | ||
634 | + queryStr + "'" + ") AND "; | ||
635 | |||
636 | } | ||
637 | } | ||
638 | } | ||
639 | // Skip trailing "AND" | ||
640 | if ( isAnyFieldSelected ) | ||
641 | qu = qu.left( qu.length() - 4 ); | ||
642 | |||
643 | qWarning( "queryByExample query: %s", qu.latin1() ); | ||
644 | |||
645 | // Execute query and return the received uid's | ||
646 | OSQLRawQuery raw( qu ); | ||
647 | OSQLResult res = m_driver->query( &raw ); | ||
648 | if ( res.state() != OSQLResult::Success ){ | ||
649 | QArray<int> empty; | ||
650 | return empty; | ||
651 | } | ||
652 | |||
653 | QArray<int> list = extractUids( res ); | ||
654 | |||
655 | return list; | ||
656 | } | ||
657 | |||
658 | QArray<int> OContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const | ||
659 | { | ||
660 | QArray<int> nix(0); | ||
661 | return nix; | ||
662 | } | ||
663 | |||
664 | const uint OContactAccessBackend_SQL::querySettings() | ||
665 | { | ||
666 | return OContactAccess::IgnoreCase | ||
667 | || OContactAccess::WildCards; | ||
668 | } | ||
669 | |||
670 | bool OContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const | ||
671 | { | ||
672 | /* OContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay | ||
673 | * may be added with any of the other settings. IgnoreCase should never used alone. | ||
674 | * Wildcards, RegExp, ExactMatch should never used at the same time... | ||
675 | */ | ||
676 | |||
677 | // Step 1: Check whether the given settings are supported by this backend | ||
678 | if ( ( querySettings & ( | ||
679 | OContactAccess::IgnoreCase | ||
680 | | OContactAccess::WildCards | ||
681 | // | OContactAccess::DateDiff | ||
682 | // | OContactAccess::DateYear | ||
683 | // | OContactAccess::DateMonth | ||
684 | // | OContactAccess::DateDay | ||
685 | // | OContactAccess::RegExp | ||
686 | // | OContactAccess::ExactMatch | ||
687 | ) ) != querySettings ) | ||
688 | return false; | ||
689 | |||
690 | // Step 2: Check whether the given combinations are ok.. | ||
691 | |||
692 | // IngoreCase alone is invalid | ||
693 | if ( querySettings == OContactAccess::IgnoreCase ) | ||
694 | return false; | ||
695 | |||
696 | // WildCards, RegExp and ExactMatch should never used at the same time | ||
697 | switch ( querySettings & ~( OContactAccess::IgnoreCase | ||
698 | | OContactAccess::DateDiff | ||
699 | | OContactAccess::DateYear | ||
700 | | OContactAccess::DateMonth | ||
701 | | OContactAccess::DateDay | ||
702 | ) | ||
703 | ){ | ||
704 | case OContactAccess::RegExp: | ||
705 | return ( true ); | ||
706 | case OContactAccess::WildCards: | ||
707 | return ( true ); | ||
708 | case OContactAccess::ExactMatch: | ||
709 | return ( true ); | ||
710 | case 0: // one of the upper removed bits were set.. | ||
711 | return ( true ); | ||
712 | default: | ||
713 | return ( false ); | ||
714 | } | ||
715 | |||
716 | } | ||
717 | |||
718 | QArray<int> OContactAccessBackend_SQL::sorted( bool asc, int , int , int ) | ||
719 | { | ||
720 | QTime t; | ||
721 | t.start(); | ||
722 | |||
723 | #ifdef __STORE_HORIZONTAL_ | ||
724 | QString query = "SELECT uid FROM addressbook "; | ||
725 | query += "ORDER BY \"Last Name\" "; | ||
726 | #else | ||
727 | QString query = "SELECT uid FROM addressbook WHERE type = 'Last Name' "; | ||
728 | query += "ORDER BY upper( value )"; | ||
729 | #endif | ||
730 | |||
731 | if ( !asc ) | ||
732 | query += "DESC"; | ||
733 | |||
734 | // qWarning("sorted query is: %s", query.latin1() ); | ||
735 | |||
736 | OSQLRawQuery raw( query ); | ||
737 | OSQLResult res = m_driver->query( &raw ); | ||
738 | if ( res.state() != OSQLResult::Success ){ | ||
739 | QArray<int> empty; | ||
740 | return empty; | ||
741 | } | ||
742 | |||
743 | QArray<int> list = extractUids( res ); | ||
744 | |||
745 | qWarning("sorted needed %d ms!", t.elapsed() ); | ||
746 | return list; | ||
747 | } | ||
748 | |||
749 | |||
750 | void OContactAccessBackend_SQL::update() | ||
751 | { | ||
752 | qWarning("Update starts"); | ||
753 | QTime t; | ||
754 | t.start(); | ||
755 | |||
756 | // Now load the database set and extract the uid's | ||
757 | // which will be held locally | ||
758 | |||
759 | LoadQuery lo; | ||
760 | OSQLResult res = m_driver->query(&lo); | ||
761 | if ( res.state() != OSQLResult::Success ) | ||
762 | return; | ||
763 | |||
764 | m_uids = extractUids( res ); | ||
765 | |||
766 | m_changed = false; | ||
767 | |||
768 | qWarning("Update ends %d ms", t.elapsed() ); | ||
769 | } | ||
770 | |||
771 | QArray<int> OContactAccessBackend_SQL::extractUids( OSQLResult& res ) const | ||
772 | { | ||
773 | qWarning("extractUids"); | ||
774 | QTime t; | ||
775 | t.start(); | ||
776 | OSQLResultItem::ValueList list = res.results(); | ||
777 | OSQLResultItem::ValueList::Iterator it; | ||
778 | QArray<int> ints(list.count() ); | ||
779 | qWarning(" count = %d", list.count() ); | ||
780 | |||
781 | int i = 0; | ||
782 | for (it = list.begin(); it != list.end(); ++it ) { | ||
783 | ints[i] = (*it).data("uid").toInt(); | ||
784 | i++; | ||
785 | } | ||
786 | qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() ); | ||
787 | |||
788 | return ints; | ||
789 | |||
790 | } | ||
791 | |||
792 | #ifdef __STORE_HORIZONTAL_ | ||
793 | QMap<int, QString> OContactAccessBackend_SQL::requestNonCustom( int uid ) const | ||
794 | { | ||
795 | QTime t; | ||
796 | t.start(); | ||
797 | |||
798 | QMap<int, QString> nonCustomMap; | ||
799 | |||
800 | int t2needed = 0; | ||
801 | int t3needed = 0; | ||
802 | QTime t2; | ||
803 | t2.start(); | ||
804 | FindQuery query( uid ); | ||
805 | OSQLResult res_noncustom = m_driver->query( &query ); | ||
806 | t2needed = t2.elapsed(); | ||
807 | |||
808 | OSQLResultItem resItem = res_noncustom.first(); | ||
809 | |||
810 | QTime t3; | ||
811 | t3.start(); | ||
812 | // Now loop through all columns | ||
813 | QStringList fieldList = OContactFields::untrfields( false ); | ||
814 | QMap<QString, int> translate = OContactFields::untrFieldsToId(); | ||
815 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ | ||
816 | // Get data for the selected column and store it with the | ||
817 | // corresponding id into the map.. | ||
818 | |||
819 | int id = translate[*it]; | ||
820 | QString value = resItem.data( (*it) ); | ||
821 | |||
822 | // qWarning("Reading %s... found: %s", (*it).latin1(), value.latin1() ); | ||
823 | |||
824 | switch( id ){ | ||
825 | case Qtopia::Birthday: | ||
826 | case Qtopia::Anniversary:{ | ||
827 | // Birthday and Anniversary are encoded special ( yyyy-mm-dd ) | ||
828 | QStringList list = QStringList::split( '-', value ); | ||
829 | QStringList::Iterator lit = list.begin(); | ||
830 | int year = (*lit).toInt(); | ||
831 | int month = (*(++lit)).toInt(); | ||
832 | int day = (*(++lit)).toInt(); | ||
833 | if ( ( day != 0 ) && ( month != 0 ) && ( year != 0 ) ){ | ||
834 | QDate date( year, month, day ); | ||
835 | nonCustomMap.insert( id, OConversion::dateToString( date ) ); | ||
836 | } | ||
837 | } | ||
838 | break; | ||
839 | case Qtopia::AddressCategory: | ||
840 | qWarning("Category is: %s", value.latin1() ); | ||
841 | default: | ||
842 | nonCustomMap.insert( id, value ); | ||
843 | } | ||
844 | } | ||
845 | |||
846 | // First insert uid | ||
847 | nonCustomMap.insert( Qtopia::AddressUid, resItem.data( "uid" ) ); | ||
848 | t3needed = t3.elapsed(); | ||
849 | |||
850 | // qWarning("Adding UID: %s", resItem.data( "uid" ).latin1() ); | ||
851 | qWarning("RequestNonCustom needed: insg.:%d ms, query: %d ms, mapping: %d ms", | ||
852 | t.elapsed(), t2needed, t3needed ); | ||
853 | |||
854 | return nonCustomMap; | ||
855 | } | ||
856 | #else | ||
857 | |||
858 | QMap<int, QString> OContactAccessBackend_SQL::requestNonCustom( int uid ) const | ||
859 | { | ||
860 | QTime t; | ||
861 | t.start(); | ||
862 | |||
863 | QMap<int, QString> nonCustomMap; | ||
864 | |||
865 | int t2needed = 0; | ||
866 | QTime t2; | ||
867 | t2.start(); | ||
868 | FindQuery query( uid ); | ||
869 | OSQLResult res_noncustom = m_driver->query( &query ); | ||
870 | t2needed = t2.elapsed(); | ||
871 | |||
872 | if ( res_noncustom.state() == OSQLResult::Failure ) { | ||
873 | qWarning("OSQLResult::Failure in find query !!"); | ||
874 | QMap<int, QString> empty; | ||
875 | return empty; | ||
876 | } | ||
877 | |||
878 | int t3needed = 0; | ||
879 | QTime t3; | ||
880 | t3.start(); | ||
881 | QMap<QString, int> translateMap = OContactFields::untrFieldsToId(); | ||
882 | |||
883 | OSQLResultItem::ValueList list = res_noncustom.results(); | ||
884 | OSQLResultItem::ValueList::Iterator it = list.begin(); | ||
885 | for ( ; it != list.end(); ++it ) { | ||
886 | if ( (*it).data("type") != "" ){ | ||
887 | int typeId = translateMap[(*it).data( "type" )]; | ||
888 | switch( typeId ){ | ||
889 | case Qtopia::Birthday: | ||
890 | case Qtopia::Anniversary:{ | ||
891 | // Birthday and Anniversary are encoded special ( yyyy-mm-dd ) | ||
892 | QStringList list = QStringList::split( '-', (*it).data( "value" ) ); | ||
893 | QStringList::Iterator lit = list.begin(); | ||
894 | int year = (*lit).toInt(); | ||
895 | qWarning("1. %s", (*lit).latin1()); | ||
896 | int month = (*(++lit)).toInt(); | ||
897 | qWarning("2. %s", (*lit).latin1()); | ||
898 | int day = (*(++lit)).toInt(); | ||
899 | qWarning("3. %s", (*lit).latin1()); | ||
900 | qWarning( "RequestNonCustom->Converting:%s to Year: %d, Month: %d, Day: %d ", (*it).data( "value" ).latin1(), year, month, day ); | ||
901 | QDate date( year, month, day ); | ||
902 | nonCustomMap.insert( typeId, OConversion::dateToString( date ) ); | ||
903 | } | ||
904 | break; | ||
905 | default: | ||
906 | nonCustomMap.insert( typeId, | ||
907 | (*it).data( "value" ) ); | ||
908 | } | ||
909 | } | ||
910 | } | ||
911 | // Add UID to Map.. | ||
912 | nonCustomMap.insert( Qtopia::AddressUid, QString::number( uid ) ); | ||
913 | t3needed = t3.elapsed(); | ||
914 | |||
915 | qWarning("RequestNonCustom needed: insg.:%d ms, query: %d ms, mapping: %d ms", t.elapsed(), t2needed, t3needed ); | ||
916 | return nonCustomMap; | ||
917 | } | ||
918 | |||
919 | #endif // __STORE_HORIZONTAL_ | ||
920 | |||
921 | QMap<QString, QString> OContactAccessBackend_SQL::requestCustom( int uid ) const | ||
922 | { | ||
923 | QTime t; | ||
924 | t.start(); | ||
925 | |||
926 | QMap<QString, QString> customMap; | ||
927 | |||
928 | FindCustomQuery query( uid ); | ||
929 | OSQLResult res_custom = m_driver->query( &query ); | ||
930 | |||
931 | if ( res_custom.state() == OSQLResult::Failure ) { | ||
932 | qWarning("OSQLResult::Failure in find query !!"); | ||
933 | QMap<QString, QString> empty; | ||
934 | return empty; | ||
935 | } | ||
936 | |||
937 | OSQLResultItem::ValueList list = res_custom.results(); | ||
938 | OSQLResultItem::ValueList::Iterator it = list.begin(); | ||
939 | for ( ; it != list.end(); ++it ) { | ||
940 | customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); | ||
941 | } | ||
942 | |||
943 | qWarning("RequestCustom needed: %d ms", t.elapsed() ); | ||
944 | return customMap; | ||
945 | } | ||