summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp8
1 files changed, 7 insertions, 1 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
index 6aaa14c..f684f8c 100644
--- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
+++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
@@ -139,949 +139,955 @@ namespace {
139 139
140 /** 140 /**
141 * a find query for custom elements 141 * a find query for custom elements
142 */ 142 */
143 class FindCustomQuery : public OSQLQuery { 143 class FindCustomQuery : public OSQLQuery {
144 public: 144 public:
145 FindCustomQuery(int uid); 145 FindCustomQuery(int uid);
146 FindCustomQuery(const UIDArray& ); 146 FindCustomQuery(const UIDArray& );
147 ~FindCustomQuery(); 147 ~FindCustomQuery();
148 QString query()const; 148 QString query()const;
149 private: 149 private:
150 QString single()const; 150 QString single()const;
151 QString multi()const; 151 QString multi()const;
152 UIDArray m_uids; 152 UIDArray m_uids;
153 int m_uid; 153 int m_uid;
154 }; 154 };
155 155
156 156
157 157
158 // We using two tables to store the information: 158 // We using two tables to store the information:
159 // 1. addressbook : It contains General information about the contact (non custom) 159 // 1. addressbook : It contains General information about the contact (non custom)
160 // 2. custom_data : Not official supported entries 160 // 2. custom_data : Not official supported entries
161 // All tables are connected by the uid of the contact. 161 // All tables are connected by the uid of the contact.
162 // Maybe I should add a table for meta-information ? 162 // Maybe I should add a table for meta-information ?
163 CreateQuery::CreateQuery() : OSQLQuery() {} 163 CreateQuery::CreateQuery() : OSQLQuery() {}
164 CreateQuery::~CreateQuery() {} 164 CreateQuery::~CreateQuery() {}
165 QString CreateQuery::query()const { 165 QString CreateQuery::query()const {
166 QString qu; 166 QString qu;
167 167
168 qu += "create table addressbook( uid PRIMARY KEY "; 168 qu += "create table addressbook( uid PRIMARY KEY ";
169 169
170 QStringList fieldList = OPimContactFields::untrfields( false ); 170 QStringList fieldList = OPimContactFields::untrfields( false );
171 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 171 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
172 qu += QString( ",\"%1\" VARCHAR(10)" ).arg( *it ); 172 qu += QString( ",\"%1\" VARCHAR(10)" ).arg( *it );
173 } 173 }
174 qu += " );"; 174 qu += " );";
175 175
176 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );"; 176 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );";
177 177
178 return qu; 178 return qu;
179 } 179 }
180 180
181 ClearQuery::ClearQuery() 181 ClearQuery::ClearQuery()
182 : OSQLQuery() {} 182 : OSQLQuery() {}
183 ClearQuery::~ClearQuery() {} 183 ClearQuery::~ClearQuery() {}
184 QString ClearQuery::query()const { 184 QString ClearQuery::query()const {
185 QString qu = "drop table addressbook;"; 185 QString qu = "drop table addressbook;";
186 qu += "drop table custom_data;"; 186 qu += "drop table custom_data;";
187// qu += "drop table dates;"; 187// qu += "drop table dates;";
188 return qu; 188 return qu;
189 } 189 }
190 190
191 191
192 LoadQuery::LoadQuery() : OSQLQuery() {} 192 LoadQuery::LoadQuery() : OSQLQuery() {}
193 LoadQuery::~LoadQuery() {} 193 LoadQuery::~LoadQuery() {}
194 QString LoadQuery::query()const { 194 QString LoadQuery::query()const {
195 QString qu; 195 QString qu;
196 qu += "select uid from addressbook"; 196 qu += "select uid from addressbook";
197 197
198 return qu; 198 return qu;
199 } 199 }
200 200
201 201
202 InsertQuery::InsertQuery( const OPimContact& contact ) 202 InsertQuery::InsertQuery( const OPimContact& contact )
203 : OSQLQuery(), m_contact( contact ) { 203 : OSQLQuery(), m_contact( contact ) {
204 } 204 }
205 205
206 InsertQuery::~InsertQuery() { 206 InsertQuery::~InsertQuery() {
207 } 207 }
208 208
209 /* 209 /*
210 * converts from a OPimContact to a query 210 * converts from a OPimContact to a query
211 */ 211 */
212 QString InsertQuery::query()const{ 212 QString InsertQuery::query()const{
213 213
214 QString qu; 214 QString qu;
215 qu += "insert into addressbook VALUES( " + 215 qu += "insert into addressbook VALUES( " +
216 QString::number( m_contact.uid() ); 216 QString::number( m_contact.uid() );
217 217
218 // Get all information out of the contact-class 218 // Get all information out of the contact-class
219 // Remember: The category is stored in contactMap, too ! 219 // Remember: The category is stored in contactMap, too !
220 QMap<int, QString> contactMap = m_contact.toMap(); 220 QMap<int, QString> contactMap = m_contact.toMap();
221 221
222 QStringList fieldList = OPimContactFields::untrfields( false ); 222 QStringList fieldList = OPimContactFields::untrfields( false );
223 QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); 223 QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
224 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 224 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
225 // Convert Column-String to Id and get value for this id.. 225 // Convert Column-String to Id and get value for this id..
226 // Hmmm.. Maybe not very cute solution.. 226 // Hmmm.. Maybe not very cute solution..
227 int id = translate[*it]; 227 int id = translate[*it];
228 switch ( id ){ 228 switch ( id ){
229 case Qtopia::Birthday: 229 case Qtopia::Birthday:
230 case Qtopia::Anniversary:{ 230 case Qtopia::Anniversary:{
231 QDate day; 231 QDate day;
232 if ( id == Qtopia::Birthday ){ 232 if ( id == Qtopia::Birthday ){
233 day = m_contact.birthday(); 233 day = m_contact.birthday();
234 } else { 234 } else {
235 day = m_contact.anniversary(); 235 day = m_contact.anniversary();
236 } 236 }
237 // These entries should stored in a special format 237 // These entries should stored in a special format
238 // year-month-day 238 // year-month-day
239 if ( day.isValid() ){ 239 if ( day.isValid() ){
240 qu += QString(",\"%1-%2-%3\"") 240 qu += QString(",\"%1-%2-%3\"")
241 .arg( QString::number( day.year() ).rightJustify( 4, '0' ) ) 241 .arg( QString::number( day.year() ).rightJustify( 4, '0' ) )
242 .arg( QString::number( day.month() ).rightJustify( 2, '0' ) ) 242 .arg( QString::number( day.month() ).rightJustify( 2, '0' ) )
243 .arg( QString::number( day.day() ).rightJustify( 2, '0' ) ); 243 .arg( QString::number( day.day() ).rightJustify( 2, '0' ) );
244 } else { 244 } else {
245 qu += ",\"\""; 245 qu += ",\"\"";
246 } 246 }
247 } 247 }
248 break; 248 break;
249 default: 249 default:
250 qu += QString( ",\"%1\"" ).arg( contactMap[id] ); 250 qu += QString( ",\"%1\"" ).arg( contactMap[id] );
251 } 251 }
252 } 252 }
253 qu += " );"; 253 qu += " );";
254 254
255 255
256 // Now add custom data.. 256 // Now add custom data..
257 int id = 0; 257 int id = 0;
258 id = 0; 258 id = 0;
259 QMap<QString, QString> customMap = m_contact.toExtraMap(); 259 QMap<QString, QString> customMap = m_contact.toExtraMap();
260 for( QMap<QString, QString>::Iterator it = customMap.begin(); 260 for( QMap<QString, QString>::Iterator it = customMap.begin();
261 it != customMap.end(); ++it ){ 261 it != customMap.end(); ++it ){
262 qu += "insert into custom_data VALUES(" 262 qu += "insert into custom_data VALUES("
263 + QString::number( m_contact.uid() ) 263 + QString::number( m_contact.uid() )
264 + "," 264 + ","
265 + QString::number( id++ ) 265 + QString::number( id++ )
266 + ",'" 266 + ",'"
267 + it.key() 267 + it.key()
268 + "'," 268 + "',"
269 + "0" // Priority for future enhancements 269 + "0" // Priority for future enhancements
270 + ",'" 270 + ",'"
271 + it.data() 271 + it.data()
272 + "');"; 272 + "');";
273 } 273 }
274 // qu += "commit;"; 274 // qu += "commit;";
275 odebug << "add " << qu << "" << oendl; 275 odebug << "add " << qu << "" << oendl;
276 return qu; 276 return qu;
277 } 277 }
278 278
279 279
280 RemoveQuery::RemoveQuery(int uid ) 280 RemoveQuery::RemoveQuery(int uid )
281 : OSQLQuery(), m_uid( uid ) {} 281 : OSQLQuery(), m_uid( uid ) {}
282 282
283 RemoveQuery::~RemoveQuery() {} 283 RemoveQuery::~RemoveQuery() {}
284 284
285 QString RemoveQuery::query()const { 285 QString RemoveQuery::query()const {
286 QString qu = "DELETE from addressbook where uid = " 286 QString qu = "DELETE from addressbook where uid = "
287 + QString::number(m_uid) + ";"; 287 + QString::number(m_uid) + ";";
288 qu += "DELETE from custom_data where uid = " 288 qu += "DELETE from custom_data where uid = "
289 + QString::number(m_uid) + ";"; 289 + QString::number(m_uid) + ";";
290 return qu; 290 return qu;
291 } 291 }
292 292
293 293
294 FindQuery::FindQuery(int uid) 294 FindQuery::FindQuery(int uid)
295 : OSQLQuery(), m_uid( uid ) { 295 : OSQLQuery(), m_uid( uid ) {
296 } 296 }
297 FindQuery::FindQuery(const UIDArray& ints) 297 FindQuery::FindQuery(const UIDArray& ints)
298 : OSQLQuery(), m_uids( ints ){ 298 : OSQLQuery(), m_uids( ints ){
299 } 299 }
300 FindQuery::~FindQuery() { 300 FindQuery::~FindQuery() {
301 } 301 }
302 QString FindQuery::query()const{ 302 QString FindQuery::query()const{
303 if ( m_uids.count() == 0 ) 303 if ( m_uids.count() == 0 )
304 return single(); 304 return single();
305 else 305 else
306 return multi(); 306 return multi();
307 } 307 }
308 308
309 309
310 310
311 QString FindQuery::multi()const { 311 QString FindQuery::multi()const {
312 QString qu = "select * from addressbook where"; 312 QString qu = "select * from addressbook where";
313 for (uint i = 0; i < m_uids.count(); i++ ) { 313 for (uint i = 0; i < m_uids.count(); i++ ) {
314 qu += " uid = " + QString::number( m_uids[i] ) + " OR"; 314 qu += " uid = " + QString::number( m_uids[i] ) + " OR";
315 } 315 }
316 qu.remove( qu.length()-2, 2 ); // Hmmmm.. 316 qu.remove( qu.length()-2, 2 ); // Hmmmm..
317 317
318 odebug << "find query: " << qu << "" << oendl; 318 odebug << "find query: " << qu << "" << oendl;
319 return qu; 319 return qu;
320 } 320 }
321 321
322 QString FindQuery::single()const{ 322 QString FindQuery::single()const{
323 QString qu = "select *"; 323 QString qu = "select *";
324 qu += " from addressbook where uid = " + QString::number(m_uid); 324 qu += " from addressbook where uid = " + QString::number(m_uid);
325 325
326 // owarn << "find query: " << qu << "" << oendl; 326 // owarn << "find query: " << qu << "" << oendl;
327 return qu; 327 return qu;
328 } 328 }
329 329
330 330
331 FindCustomQuery::FindCustomQuery(int uid) 331 FindCustomQuery::FindCustomQuery(int uid)
332 : OSQLQuery(), m_uid( uid ) { 332 : OSQLQuery(), m_uid( uid ) {
333 } 333 }
334 FindCustomQuery::FindCustomQuery(const UIDArray& ints) 334 FindCustomQuery::FindCustomQuery(const UIDArray& ints)
335 : OSQLQuery(), m_uids( ints ){ 335 : OSQLQuery(), m_uids( ints ){
336 } 336 }
337 FindCustomQuery::~FindCustomQuery() { 337 FindCustomQuery::~FindCustomQuery() {
338 } 338 }
339 QString FindCustomQuery::query()const{ 339 QString FindCustomQuery::query()const{
340// if ( m_uids.count() == 0 ) 340// if ( m_uids.count() == 0 )
341 return single(); 341 return single();
342 } 342 }
343 QString FindCustomQuery::single()const{ 343 QString FindCustomQuery::single()const{
344 QString qu = "select uid, type, value from custom_data where uid = "; 344 QString qu = "select uid, type, value from custom_data where uid = ";
345 qu += QString::number(m_uid); 345 qu += QString::number(m_uid);
346 return qu; 346 return qu;
347 } 347 }
348 348
349}; 349};
350 350
351 351
352/* --------------------------------------------------------------------------- */ 352/* --------------------------------------------------------------------------- */
353 353
354namespace Opie { 354namespace Opie {
355 355
356OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* appname */, 356OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* appname */,
357 const QString& filename ): 357 const QString& filename ):
358 OPimContactAccessBackend(), m_changed(false), m_driver( NULL ) 358 OPimContactAccessBackend(), m_changed(false), m_driver( NULL )
359{ 359{
360 odebug << "C'tor OPimContactAccessBackend_SQL starts" << oendl; 360 odebug << "C'tor OPimContactAccessBackend_SQL starts" << oendl;
361 QTime t; 361 QTime t;
362 t.start(); 362 t.start();
363 363
364 /* Expecting to access the default filename if nothing else is set */ 364 /* Expecting to access the default filename if nothing else is set */
365 if ( filename.isEmpty() ){ 365 if ( filename.isEmpty() ){
366 m_fileName = Global::applicationFileName( "addressbook","addressbook.db" ); 366 m_fileName = Global::applicationFileName( "addressbook","addressbook.db" );
367 } else 367 } else
368 m_fileName = filename; 368 m_fileName = filename;
369 369
370 // Get the standart sql-driver from the OSQLManager.. 370 // Get the standart sql-driver from the OSQLManager..
371 OSQLManager man; 371 OSQLManager man;
372 m_driver = man.standard(); 372 m_driver = man.standard();
373 m_driver->setUrl( m_fileName ); 373 m_driver->setUrl( m_fileName );
374 374
375 load(); 375 load();
376 376
377 odebug << "C'tor OPimContactAccessBackend_SQL ends: " << t.elapsed() << " ms" << oendl; 377 odebug << "C'tor OPimContactAccessBackend_SQL ends: " << t.elapsed() << " ms" << oendl;
378} 378}
379 379
380OPimContactAccessBackend_SQL::~OPimContactAccessBackend_SQL () 380OPimContactAccessBackend_SQL::~OPimContactAccessBackend_SQL ()
381{ 381{
382 if( m_driver ) 382 if( m_driver )
383 delete m_driver; 383 delete m_driver;
384} 384}
385 385
386bool OPimContactAccessBackend_SQL::load () 386bool OPimContactAccessBackend_SQL::load ()
387{ 387{
388 if (!m_driver->open() ) 388 if (!m_driver->open() )
389 return false; 389 return false;
390 390
391 // Don't expect that the database exists. 391 // Don't expect that the database exists.
392 // It is save here to create the table, even if it 392 // It is save here to create the table, even if it
393 // do exist. ( Is that correct for all databases ?? ) 393 // do exist. ( Is that correct for all databases ?? )
394 CreateQuery creat; 394 CreateQuery creat;
395 OSQLResult res = m_driver->query( &creat ); 395 OSQLResult res = m_driver->query( &creat );
396 396
397 update(); 397 update();
398 398
399 return true; 399 return true;
400 400
401} 401}
402 402
403bool OPimContactAccessBackend_SQL::reload() 403bool OPimContactAccessBackend_SQL::reload()
404{ 404{
405 return load(); 405 return load();
406} 406}
407 407
408bool OPimContactAccessBackend_SQL::save() 408bool OPimContactAccessBackend_SQL::save()
409{ 409{
410 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) 410 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers)
411} 411}
412 412
413 413
414void OPimContactAccessBackend_SQL::clear () 414void OPimContactAccessBackend_SQL::clear ()
415{ 415{
416 ClearQuery cle; 416 ClearQuery cle;
417 OSQLResult res = m_driver->query( &cle ); 417 OSQLResult res = m_driver->query( &cle );
418 418
419 reload(); 419 reload();
420} 420}
421 421
422bool OPimContactAccessBackend_SQL::wasChangedExternally() 422bool OPimContactAccessBackend_SQL::wasChangedExternally()
423{ 423{
424 return false; 424 return false;
425} 425}
426 426
427UIDArray OPimContactAccessBackend_SQL::allRecords() const 427UIDArray OPimContactAccessBackend_SQL::allRecords() const
428{ 428{
429 429
430 // FIXME: Think about cute handling of changed tables.. 430 // FIXME: Think about cute handling of changed tables..
431 // Thus, we don't have to call update here... 431 // Thus, we don't have to call update here...
432 if ( m_changed ) 432 if ( m_changed )
433 ((OPimContactAccessBackend_SQL*)this)->update(); 433 ((OPimContactAccessBackend_SQL*)this)->update();
434 434
435 return m_uids; 435 return m_uids;
436} 436}
437 437
438bool OPimContactAccessBackend_SQL::add ( const OPimContact &newcontact ) 438bool OPimContactAccessBackend_SQL::add ( const OPimContact &newcontact )
439{ 439{
440 odebug << "add in contact SQL-Backend" << oendl; 440 odebug << "add in contact SQL-Backend" << oendl;
441 InsertQuery ins( newcontact ); 441 InsertQuery ins( newcontact );
442 OSQLResult res = m_driver->query( &ins ); 442 OSQLResult res = m_driver->query( &ins );
443 443
444 if ( res.state() == OSQLResult::Failure ) 444 if ( res.state() == OSQLResult::Failure )
445 return false; 445 return false;
446 446
447 int c = m_uids.count(); 447 int c = m_uids.count();
448 m_uids.resize( c+1 ); 448 m_uids.resize( c+1 );
449 m_uids[c] = newcontact.uid(); 449 m_uids[c] = newcontact.uid();
450 450
451 return true; 451 return true;
452} 452}
453 453
454 454
455bool OPimContactAccessBackend_SQL::remove ( int uid ) 455bool OPimContactAccessBackend_SQL::remove ( int uid )
456{ 456{
457 RemoveQuery rem( uid ); 457 RemoveQuery rem( uid );
458 OSQLResult res = m_driver->query(&rem ); 458 OSQLResult res = m_driver->query(&rem );
459 459
460 if ( res.state() == OSQLResult::Failure ) 460 if ( res.state() == OSQLResult::Failure )
461 return false; 461 return false;
462 462
463 m_changed = true; 463 m_changed = true;
464 464
465 return true; 465 return true;
466} 466}
467 467
468bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact ) 468bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact )
469{ 469{
470 if ( !remove( contact.uid() ) ) 470 if ( !remove( contact.uid() ) )
471 return false; 471 return false;
472 472
473 return add( contact ); 473 return add( contact );
474} 474}
475 475
476 476
477OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const 477OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
478{ 478{
479 odebug << "OPimContactAccessBackend_SQL::find(" << uid << ")" << oendl; 479 odebug << "OPimContactAccessBackend_SQL::find(" << uid << ")" << oendl;
480 QTime t; 480 QTime t;
481 t.start(); 481 t.start();
482 482
483 OPimContact retContact( requestNonCustom( uid ) ); 483 OPimContact retContact( requestNonCustom( uid ) );
484 retContact.setExtraMap( requestCustom( uid ) ); 484 retContact.setExtraMap( requestCustom( uid ) );
485 485
486 odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl; 486 odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl;
487 return retContact; 487 return retContact;
488} 488}
489 489
490OPimContact OPimContactAccessBackend_SQL::find( int uid, const UIDArray& queryUids, uint current, Frontend::CacheDirection direction ) const 490OPimContact OPimContactAccessBackend_SQL::find( int uid, const UIDArray& queryUids, uint current, Frontend::CacheDirection direction ) const
491{ 491{
492 odebug << "OPimContactAccessBackend_SQL::find( ..multi.. )" << oendl; 492 odebug << "OPimContactAccessBackend_SQL::find( ..multi.. )" << oendl;
493 odebug << "searching for " << uid << "" << oendl; 493 odebug << "searching for " << uid << "" << oendl;
494 494
495 QTime t; 495 QTime t;
496 t.start(); 496 t.start();
497 497
498 uint numReadAhead = readAhead(); 498 uint numReadAhead = readAhead();
499 QArray<int> searchList( numReadAhead ); 499 QArray<int> searchList( numReadAhead );
500 500
501 uint size =0; 501 uint size =0;
502 502
503 // Build an array with all elements which should be requested and cached 503 // Build an array with all elements which should be requested and cached
504 // We will just request "numReadAhead" elements, starting from "current" position in 504 // We will just request "numReadAhead" elements, starting from "current" position in
505 // the list of many uids ! 505 // the list of many uids !
506 switch( direction ) { 506 switch( direction ) {
507 /* forward */ 507 /* forward */
508 case Frontend::Forward: 508 case Frontend::Forward:
509 for ( uint i = current; i < queryUids.count() && size < numReadAhead; i++ ) { 509 for ( uint i = current; i < queryUids.count() && size < numReadAhead; i++ ) {
510 searchList[size] = queryUids[i]; 510 searchList[size] = queryUids[i];
511 size++; 511 size++;
512 } 512 }
513 break; 513 break;
514 /* reverse */ 514 /* reverse */
515 case Frontend::Reverse: 515 case Frontend::Reverse:
516 for ( uint i = current; i != 0 && size < numReadAhead; i-- ) { 516 for ( uint i = current; i != 0 && size < numReadAhead; i-- ) {
517 searchList[size] = queryUids[i]; 517 searchList[size] = queryUids[i];
518 size++; 518 size++;
519 } 519 }
520 break; 520 break;
521 } 521 }
522 522
523 //Shrink to real size.. 523 //Shrink to real size..
524 searchList.resize( size ); 524 searchList.resize( size );
525 525
526 OPimContact retContact( requestContactsAndCache( uid, searchList ) ); 526 OPimContact retContact( requestContactsAndCache( uid, searchList ) );
527 527
528 odebug << "OPimContactAccessBackend_SQL::find( ..multi.. ) needed: " << t.elapsed() << " ms" << oendl; 528 odebug << "OPimContactAccessBackend_SQL::find( ..multi.. ) needed: " << t.elapsed() << " ms" << oendl;
529 return retContact; 529 return retContact;
530} 530}
531 531
532 532
533UIDArray OPimContactAccessBackend_SQL::queryByExample ( const UIDArray& uidlist, const OPimContact &query, int settings, 533UIDArray OPimContactAccessBackend_SQL::queryByExample ( const UIDArray& uidlist, const OPimContact &query, int settings,
534 const QDateTime& qd ) const 534 const QDateTime& qd ) const
535{ 535{
536 QString searchQuery = ""; 536 QString searchQuery = "";
537 QString datediff_query = ""; 537 QString datediff_query = "";
538 QString uid_query = ""; 538 QString uid_query = "";
539 539
540 // Just add uid's selection if we really try to search in a subset of all uids! Otherwise this would 540 // Just add uid's selection if we really try to search in a subset of all uids! Otherwise this would
541 // just take time and memory! 541 // just take time and memory!
542 if ( uidlist.count() != m_uids.count() ) { 542 if ( uidlist.count() != m_uids.count() ) {
543 uid_query += " ("; 543 uid_query += " (";
544 544
545 for ( uint i = 0; i < uidlist.count(); i++ ) { 545 for ( uint i = 0; i < uidlist.count(); i++ ) {
546 uid_query += " uid = " + QString::number( uidlist[i] ) + " OR"; 546 uid_query += " uid = " + QString::number( uidlist[i] ) + " OR";
547 } 547 }
548 uid_query.remove( uid_query.length()-2, 2 ); // Hmmmm.. 548 uid_query.remove( uid_query.length()-2, 2 ); // Hmmmm..
549 uid_query += " ) AND "; 549 uid_query += " ) AND ";
550 } 550 }
551 551
552 552
553 QDate startDate; 553 QDate startDate;
554 554
555 if ( qd.isValid() ) 555 if ( qd.isValid() )
556 startDate = qd.date(); 556 startDate = qd.date();
557 else 557 else
558 startDate = QDate::currentDate(); 558 startDate = QDate::currentDate();
559 559
560 560
561 QMap<int, QString> queryFields = query.toMap(); 561 QMap<int, QString> queryFields = query.toMap();
562 QStringList fieldList = OPimContactFields::untrfields( false ); 562 QStringList fieldList = OPimContactFields::untrfields( false );
563 QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); 563 QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
564 564
565 // Convert every filled field to a SQL-Query 565 // Convert every filled field to a SQL-Query
566// bool isAnyFieldSelected = false; 566// bool isAnyFieldSelected = false;
567 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 567 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
568 568
569 int id = translate[*it]; 569 int id = translate[*it];
570 QString queryStr = queryFields[id]; 570 QString queryStr = queryFields[id];
571 QDate* endDate = 0l; 571 QDate* endDate = 0l;
572 572
573 if ( !queryStr.isEmpty() ){ 573 if ( !queryStr.isEmpty() ){
574 // If something is alredy stored in the query, add an "AND" 574 // If something is alredy stored in the query, add an "AND"
575 // to the end of the string to prepare for the next .. 575 // to the end of the string to prepare for the next ..
576 if ( !searchQuery.isEmpty() ) 576 if ( !searchQuery.isEmpty() )
577 searchQuery += " AND"; 577 searchQuery += " AND";
578 578
579// isAnyFieldSelected = true; 579// isAnyFieldSelected = true;
580 switch( id ){ 580 switch( id ){
581 case Qtopia::Birthday: 581 case Qtopia::Birthday:
582 endDate = new QDate( query.birthday() ); 582 endDate = new QDate( query.birthday() );
583 // Fall through ! 583 // Fall through !
584 case Qtopia::Anniversary: 584 case Qtopia::Anniversary:
585 if ( endDate == 0l ) 585 if ( endDate == 0l )
586 endDate = new QDate( query.anniversary() ); 586 endDate = new QDate( query.anniversary() );
587 587
588 if ( settings & OPimContactAccess::DateDiff ) { 588 if ( settings & OPimContactAccess::DateDiff ) {
589 // To handle datediffs correctly, we need to remove the year information from 589 // To handle datediffs correctly, we need to remove the year information from
590 // the birthday and anniversary. 590 // the birthday and anniversary.
591 // To do this efficiently, we will create a temporary table which contains the 591 // To do this efficiently, we will create a temporary table which contains the
592 // information we need and do the query on it. 592 // information we need and do the query on it.
593 // This table is just visible for this process and will be removed 593 // This table is just visible for this process and will be removed
594 // automatically after using. 594 // automatically after using.
595 datediff_query = "SELECT uid,substr(\"Birthday\", 6, 10) as \"BirthdayMD\", substr(\"Anniversary\", 6, 10) as \"AnniversaryMD\" FROM addressbook WHERE ( \"Birthday\" != '' OR \"Anniversary\" != '' ) AND "; 595 datediff_query = "SELECT uid,substr(\"Birthday\", 6, 10) as \"BirthdayMD\", substr(\"Anniversary\", 6, 10) as \"AnniversaryMD\" FROM addressbook WHERE ( \"Birthday\" != '' OR \"Anniversary\" != '' ) AND ";
596 datediff_query += QString( " (\"%1MD\" <= '%2-%3\' AND \"%4MD\" >= '%5-%6')" ) 596 datediff_query += QString( " (\"%1MD\" <= '%2-%3\' AND \"%4MD\" >= '%5-%6')" )
597 .arg( *it ) 597 .arg( *it )
598 //.arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) ) 598 //.arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) )
599 .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) ) 599 .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) )
600 .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) ) 600 .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) )
601 .arg( *it ) 601 .arg( *it )
602 //.arg( QString::number( startDate.year() ).rightJustify( 4, '0' ) ) 602 //.arg( QString::number( startDate.year() ).rightJustify( 4, '0' ) )
603 .arg( QString::number( startDate.month() ).rightJustify( 2, '0' ) ) 603 .arg( QString::number( startDate.month() ).rightJustify( 2, '0' ) )
604 .arg( QString::number( startDate.day() ).rightJustify( 2, '0' ) ) ; 604 .arg( QString::number( startDate.day() ).rightJustify( 2, '0' ) ) ;
605 } 605 }
606 606
607 if ( settings & OPimContactAccess::DateYear ){ 607 if ( settings & OPimContactAccess::DateYear ){
608 searchQuery += QString( " (\"%1\" LIKE '%2-%')" ) 608 searchQuery += QString( " (\"%1\" LIKE '%2-%')" )
609 .arg( *it ) 609 .arg( *it )
610 .arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) ); 610 .arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) );
611 } 611 }
612 612
613 if ( settings & OPimContactAccess::DateMonth ){ 613 if ( settings & OPimContactAccess::DateMonth ){
614 if ( settings & OPimContactAccess::DateYear ) 614 if ( settings & OPimContactAccess::DateYear )
615 searchQuery += " AND"; 615 searchQuery += " AND";
616 616
617 searchQuery += QString( " (\"%1\" LIKE '%-%2-%')" ) 617 searchQuery += QString( " (\"%1\" LIKE '%-%2-%')" )
618 .arg( *it ) 618 .arg( *it )
619 .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) ); 619 .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) );
620 } 620 }
621 621
622 if ( settings & OPimContactAccess::DateDay ){ 622 if ( settings & OPimContactAccess::DateDay ){
623 if ( ( settings & OPimContactAccess::DateYear ) 623 if ( ( settings & OPimContactAccess::DateYear )
624 || ( settings & OPimContactAccess::DateMonth ) ) 624 || ( settings & OPimContactAccess::DateMonth ) )
625 searchQuery += " AND"; 625 searchQuery += " AND";
626 626
627 searchQuery += QString( " (\"%1\" LIKE '%-%-%2')" ) 627 searchQuery += QString( " (\"%1\" LIKE '%-%-%2')" )
628 .arg( *it ) 628 .arg( *it )
629 .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) ); 629 .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) );
630 } 630 }
631 631
632 break; 632 break;
633 default: 633 default:
634 // Switching between case sensitive and insensitive... 634 // Switching between case sensitive and insensitive...
635 // LIKE is not case sensitive, GLOB is case sensitive 635 // LIKE is not case sensitive, GLOB is case sensitive
636 // Do exist a better solution to switch this ? 636 // Do exist a better solution to switch this ?
637 if ( settings & OPimContactAccess::IgnoreCase ) 637 if ( settings & OPimContactAccess::IgnoreCase )
638 searchQuery += " (\"" + *it + "\"" + " LIKE " + "'" 638 searchQuery += " (\"" + *it + "\"" + " LIKE " + "'"
639 + queryStr.replace(QRegExp("\\*"),"%") + "'" + ")"; 639 + queryStr.replace(QRegExp("\\*"),"%") + "'" + ")";
640 else 640 else
641 searchQuery += " (\"" + *it + "\"" + " GLOB " + "'" 641 searchQuery += " (\"" + *it + "\"" + " GLOB " + "'"
642 + queryStr + "'" + ")"; 642 + queryStr + "'" + ")";
643 643
644 } 644 }
645 } 645 }
646 646
647 delete endDate; 647 delete endDate;
648 648
649 // The following if line is a replacement for 649 // The following if line is a replacement for
650 // if ( searchQuery.endsWith( "AND" ) ) 650 // if ( searchQuery.endsWith( "AND" ) )
651 if ( searchQuery.findRev( "AND" ) == ( searchQuery.length() - 3 ) ){ 651 if ( searchQuery.findRev( "AND" ) == static_cast<int>( searchQuery.length() - 3 ) ){
652 odebug << "remove AND" << oendl; 652 odebug << "remove AND" << oendl;
653 searchQuery.remove( searchQuery.length()-3, 3 ); // Hmmmm.. 653 searchQuery.remove( searchQuery.length()-3, 3 ); // Hmmmm..
654 } 654 }
655 655
656 } 656 }
657 657
658 // Now compose the complete query 658 // Now compose the complete query
659 QString qu = "SELECT uid FROM addressbook WHERE " + uid_query; 659 QString qu = "SELECT uid FROM addressbook WHERE " + uid_query;
660 660
661 if ( !datediff_query.isEmpty() && !searchQuery.isEmpty() ){ 661 if ( !datediff_query.isEmpty() && !searchQuery.isEmpty() ){
662 // If we use DateDiff, we have to intersect two queries. 662 // If we use DateDiff, we have to intersect two queries.
663 qu = datediff_query + QString( " INTERSECT " ) + qu + searchQuery; 663 qu = datediff_query + QString( " INTERSECT " ) + qu + searchQuery;
664 } else if ( datediff_query.isEmpty() && !searchQuery.isEmpty() ){ 664 } else if ( datediff_query.isEmpty() && !searchQuery.isEmpty() ){
665 qu += searchQuery; 665 qu += searchQuery;
666 } else if ( !datediff_query.isEmpty() && searchQuery.isEmpty() ){ 666 } else if ( !datediff_query.isEmpty() && searchQuery.isEmpty() ){
667 qu = datediff_query; 667 qu = datediff_query;
668 } else if ( datediff_query.isEmpty() && searchQuery.isEmpty() ){ 668 } else if ( datediff_query.isEmpty() && searchQuery.isEmpty() ){
669 UIDArray empty; 669 UIDArray empty;
670 return empty; 670 return empty;
671 } 671 }
672 672
673 odebug << "queryByExample query: " << qu << "" << oendl; 673 odebug << "queryByExample query: " << qu << "" << oendl;
674 674
675 // Execute query and return the received uid's 675 // Execute query and return the received uid's
676 OSQLRawQuery raw( qu ); 676 OSQLRawQuery raw( qu );
677 OSQLResult res = m_driver->query( &raw ); 677 OSQLResult res = m_driver->query( &raw );
678 if ( res.state() != OSQLResult::Success ){ 678 if ( res.state() != OSQLResult::Success ){
679 UIDArray empty; 679 UIDArray empty;
680 return empty; 680 return empty;
681 } 681 }
682 682
683 UIDArray list = extractUids( res ); 683 UIDArray list = extractUids( res );
684 684
685 return list; 685 return list;
686} 686}
687 687
688UIDArray OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const 688UIDArray OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const
689{ 689{
690#if 0 690#if 0
691 QArray<int> nix(0); 691 QArray<int> nix(0);
692 return nix; 692 return nix;
693 693
694#else 694#else
695 QString qu = "SELECT uid FROM addressbook WHERE ("; 695 QString qu = "SELECT uid FROM addressbook WHERE (";
696 QString searchlist; 696 QString searchlist;
697 697
698 QStringList fieldList = OPimContactFields::untrfields( false ); 698 QStringList fieldList = OPimContactFields::untrfields( false );
699 // QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); 699 // QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
700 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 700 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
701 if ( !searchlist.isEmpty() ) 701 if ( !searchlist.isEmpty() )
702 searchlist += " OR "; 702 searchlist += " OR ";
703 searchlist += " rlike(\""+ r.pattern() + "\",\"" + *it + "\") "; 703 searchlist += " rlike(\""+ r.pattern() + "\",\"" + *it + "\") ";
704 } 704 }
705 705
706 qu = qu + searchlist + ")"; 706 qu = qu + searchlist + ")";
707 707
708 odebug << "query: " << qu << "" << oendl; 708 odebug << "query: " << qu << "" << oendl;
709 709
710 OSQLRawQuery raw( qu ); 710 OSQLRawQuery raw( qu );
711 OSQLResult res = m_driver->query( &raw ); 711 OSQLResult res = m_driver->query( &raw );
712 712
713 return extractUids( res ); 713 return extractUids( res );
714 714
715 715
716#endif 716#endif
717} 717}
718 718
719const uint OPimContactAccessBackend_SQL::querySettings() const 719const uint OPimContactAccessBackend_SQL::querySettings() const
720{ 720{
721 return OPimContactAccess::IgnoreCase 721 return OPimContactAccess::IgnoreCase
722 | OPimContactAccess::WildCards 722 | OPimContactAccess::WildCards
723 | OPimContactAccess::DateDiff 723 | OPimContactAccess::DateDiff
724 | OPimContactAccess::DateYear 724 | OPimContactAccess::DateYear
725 | OPimContactAccess::DateMonth 725 | OPimContactAccess::DateMonth
726 | OPimContactAccess::DateDay 726 | OPimContactAccess::DateDay
727 ; 727 ;
728} 728}
729 729
730bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const 730bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const
731{ 731{
732 /* OPimContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay 732 /* OPimContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay
733 * may be added with any of the other settings. IgnoreCase should never used alone. 733 * may be added with any of the other settings. IgnoreCase should never used alone.
734 * Wildcards, RegExp, ExactMatch should never used at the same time... 734 * Wildcards, RegExp, ExactMatch should never used at the same time...
735 */ 735 */
736 736
737 // Step 1: Check whether the given settings are supported by this backend 737 // Step 1: Check whether the given settings are supported by this backend
738 if ( ( querySettings & ( 738 if ( ( querySettings & (
739 OPimContactAccess::IgnoreCase 739 OPimContactAccess::IgnoreCase
740 | OPimContactAccess::WildCards 740 | OPimContactAccess::WildCards
741 | OPimContactAccess::DateDiff 741 | OPimContactAccess::DateDiff
742 | OPimContactAccess::DateYear 742 | OPimContactAccess::DateYear
743 | OPimContactAccess::DateMonth 743 | OPimContactAccess::DateMonth
744 | OPimContactAccess::DateDay 744 | OPimContactAccess::DateDay
745// | OPimContactAccess::RegExp 745// | OPimContactAccess::RegExp
746// | OPimContactAccess::ExactMatch 746// | OPimContactAccess::ExactMatch
747 ) ) != querySettings ) 747 ) ) != querySettings )
748 return false; 748 return false;
749 749
750 // Step 2: Check whether the given combinations are ok.. 750 // Step 2: Check whether the given combinations are ok..
751 751
752 // IngoreCase alone is invalid 752 // IngoreCase alone is invalid
753 if ( querySettings == OPimContactAccess::IgnoreCase ) 753 if ( querySettings == OPimContactAccess::IgnoreCase )
754 return false; 754 return false;
755 755
756 // WildCards, RegExp and ExactMatch should never used at the same time 756 // WildCards, RegExp and ExactMatch should never used at the same time
757 switch ( querySettings & ~( OPimContactAccess::IgnoreCase 757 switch ( querySettings & ~( OPimContactAccess::IgnoreCase
758 | OPimContactAccess::DateDiff 758 | OPimContactAccess::DateDiff
759 | OPimContactAccess::DateYear 759 | OPimContactAccess::DateYear
760 | OPimContactAccess::DateMonth 760 | OPimContactAccess::DateMonth
761 | OPimContactAccess::DateDay 761 | OPimContactAccess::DateDay
762 ) 762 )
763 ){ 763 ){
764 case OPimContactAccess::RegExp: 764 case OPimContactAccess::RegExp:
765 return ( true ); 765 return ( true );
766 case OPimContactAccess::WildCards: 766 case OPimContactAccess::WildCards:
767 return ( true ); 767 return ( true );
768 case OPimContactAccess::ExactMatch: 768 case OPimContactAccess::ExactMatch:
769 return ( true ); 769 return ( true );
770 case 0: // one of the upper removed bits were set.. 770 case 0: // one of the upper removed bits were set..
771 return ( true ); 771 return ( true );
772 default: 772 default:
773 return ( false ); 773 return ( false );
774 } 774 }
775 775
776} 776}
777 777
778UIDArray OPimContactAccessBackend_SQL::sorted( const UIDArray& ar, bool asc, int sortOrder, 778UIDArray OPimContactAccessBackend_SQL::sorted( const UIDArray& ar, bool asc, int sortOrder,
779 int filter, const QArray<int>& categories )const 779 int filter, const QArray<int>& categories )const
780{ 780{
781 QTime t; 781 QTime t;
782 t.start(); 782 t.start();
783 783
784 QString query = "SELECT uid FROM addressbook"; 784 QString query = "SELECT uid FROM addressbook";
785 785
786 query += " WHERE ("; 786 query += " WHERE (";
787 for ( uint i = 0; i < ar.count(); i++ ) { 787 for ( uint i = 0; i < ar.count(); i++ ) {
788 query += " uid = " + QString::number( ar[i] ) + " OR"; 788 query += " uid = " + QString::number( ar[i] ) + " OR";
789 } 789 }
790 query.remove( query.length()-2, 2 ); // Hmmmm.. 790 query.remove( query.length()-2, 2 ); // Hmmmm..
791 query += ")"; 791 query += ")";
792 792
793 793
794 if ( filter != OPimBase::FilterOff ){ 794 if ( filter != OPimBase::FilterOff ){
795 if ( filter & OPimContactAccess::DoNotShowWithCategory ){ 795 if ( filter & OPimContactAccess::DoNotShowWithCategory ){
796 query += " AND ( \"Categories\" == '' )"; 796 query += " AND ( \"Categories\" == '' )";
797 } else if ( filter & OPimBase::FilterCategory ){ 797 } else if ( filter & OPimBase::FilterCategory ){
798 query += " AND ("; 798 query += " AND (";
799 for ( uint i = 0; i < categories.count(); i++ ){ 799 for ( uint i = 0; i < categories.count(); i++ ){
800 query += "\"Categories\" LIKE"; 800 query += "\"Categories\" LIKE";
801 query += QString( " '%" ) + QString::number( categories[i] ) + "%' OR"; 801 query += QString( " '%" ) + QString::number( categories[i] ) + "%' OR";
802 } 802 }
803 query.remove( query.length()-2, 2 ); // Hmmmm.. 803 query.remove( query.length()-2, 2 ); // Hmmmm..
804 query += ")"; 804 query += ")";
805 } 805 }
806 806
807 if ( filter & OPimContactAccess::DoNotShowWithoutChildren ){ 807 if ( filter & OPimContactAccess::DoNotShowWithoutChildren ){
808 query += " AND ( \"Children\" != '' )"; 808 query += " AND ( \"Children\" != '' )";
809 } 809 }
810 810
811 if ( filter & OPimContactAccess::DoNotShowWithoutAnniversary ){ 811 if ( filter & OPimContactAccess::DoNotShowWithoutAnniversary ){
812 query += " AND ( \"Anniversary\" != '' )"; 812 query += " AND ( \"Anniversary\" != '' )";
813 } 813 }
814 814
815 if ( filter & OPimContactAccess::DoNotShowWithoutBirthday ){ 815 if ( filter & OPimContactAccess::DoNotShowWithoutBirthday ){
816 query += " AND ( \"Birthday\" != '' )"; 816 query += " AND ( \"Birthday\" != '' )";
817 } 817 }
818 818
819 if ( filter & OPimContactAccess::DoNotShowWithoutHomeAddress ){ 819 if ( filter & OPimContactAccess::DoNotShowWithoutHomeAddress ){
820 // Expect that no Street means no Address, too! (eilers) 820 // Expect that no Street means no Address, too! (eilers)
821 query += " AND ( \"Home Street\" != '' )"; 821 query += " AND ( \"Home Street\" != '' )";
822 } 822 }
823 823
824 if ( filter & OPimContactAccess::DoNotShowWithoutBusinessAddress ){ 824 if ( filter & OPimContactAccess::DoNotShowWithoutBusinessAddress ){
825 // Expect that no Street means no Address, too! (eilers) 825 // Expect that no Street means no Address, too! (eilers)
826 query += " AND ( \"Business Street\" != '' )"; 826 query += " AND ( \"Business Street\" != '' )";
827 } 827 }
828 828
829 } 829 }
830 830
831 query += " ORDER BY"; 831 query += " ORDER BY";
832 832
833 switch ( sortOrder ) { 833 switch ( sortOrder ) {
834 case OPimContactAccess::SortSummary: 834 case OPimContactAccess::SortSummary:
835 query += " \"Notes\""; 835 query += " \"Notes\"";
836 break; 836 break;
837 case OPimContactAccess::SortByCategory: 837 case OPimContactAccess::SortByCategory:
838 query += " \"Categories\""; 838 query += " \"Categories\"";
839 break; 839 break;
840 case OPimContactAccess::SortByDate: 840 case OPimContactAccess::SortByDate:
841 query += " \"\""; 841 query += " \"\"";
842 break; 842 break;
843 case OPimContactAccess::SortTitle: 843 case OPimContactAccess::SortTitle:
844 query += " \"Name Title\""; 844 query += " \"Name Title\"";
845 break; 845 break;
846 case OPimContactAccess::SortFirstName: 846 case OPimContactAccess::SortFirstName:
847 query += " \"First Name\""; 847 query += " \"First Name\"";
848 break; 848 break;
849 case OPimContactAccess::SortMiddleName: 849 case OPimContactAccess::SortMiddleName:
850 query += " \"Middle Name\""; 850 query += " \"Middle Name\"";
851 break; 851 break;
852 case OPimContactAccess::SortLastName: 852 case OPimContactAccess::SortLastName:
853 query += " \"Last Name\""; 853 query += " \"Last Name\"";
854 break; 854 break;
855 case OPimContactAccess::SortFileAsName: 855 case OPimContactAccess::SortFileAsName:
856 query += " \"File As\""; 856 query += " \"File As\"";
857 break; 857 break;
858 case OPimContactAccess::SortSuffix: 858 case OPimContactAccess::SortSuffix:
859 query += " \"Suffix\""; 859 query += " \"Suffix\"";
860 break; 860 break;
861 case OPimContactAccess::SortEmail: 861 case OPimContactAccess::SortEmail:
862 query += " \"Default Email\""; 862 query += " \"Default Email\"";
863 break; 863 break;
864 case OPimContactAccess::SortNickname: 864 case OPimContactAccess::SortNickname:
865 query += " \"Nickname\""; 865 query += " \"Nickname\"";
866 break; 866 break;
867 case OPimContactAccess::SortAnniversary: 867 case OPimContactAccess::SortAnniversary:
868 query += " \"Anniversary\""; 868 query += " \"Anniversary\"";
869 break; 869 break;
870 case OPimContactAccess::SortBirthday: 870 case OPimContactAccess::SortBirthday:
871 query += " \"Birthday\""; 871 query += " \"Birthday\"";
872 break; 872 break;
873 case OPimContactAccess::SortGender: 873 case OPimContactAccess::SortGender:
874 query += " \"Gender\""; 874 query += " \"Gender\"";
875 break; 875 break;
876 case OPimContactAccess::SortBirthdayWithoutYear:
877 query += " substr(\"Birthday\", 6, 10)";
878 break;
879 case OPimContactAccess::SortAnniversaryWithoutYear:
880 query += " substr(\"Anniversary\", 6, 10)";
881 break;
876 default: 882 default:
877 query += " \"Last Name\""; 883 query += " \"Last Name\"";
878 } 884 }
879 885
880 if ( !asc ) 886 if ( !asc )
881 query += " DESC"; 887 query += " DESC";
882 888
883 889
884 odebug << "sorted query is: " << query << "" << oendl; 890 odebug << "sorted query is: " << query << "" << oendl;
885 891
886 OSQLRawQuery raw( query ); 892 OSQLRawQuery raw( query );
887 OSQLResult res = m_driver->query( &raw ); 893 OSQLResult res = m_driver->query( &raw );
888 if ( res.state() != OSQLResult::Success ){ 894 if ( res.state() != OSQLResult::Success ){
889 UIDArray empty; 895 UIDArray empty;
890 return empty; 896 return empty;
891 } 897 }
892 898
893 UIDArray list = extractUids( res ); 899 UIDArray list = extractUids( res );
894 900
895 odebug << "sorted needed " << t.elapsed() << " ms!" << oendl; 901 odebug << "sorted needed " << t.elapsed() << " ms!" << oendl;
896 return list; 902 return list;
897} 903}
898 904
899 905
900void OPimContactAccessBackend_SQL::update() 906void OPimContactAccessBackend_SQL::update()
901{ 907{
902 odebug << "Update starts" << oendl; 908 odebug << "Update starts" << oendl;
903 QTime t; 909 QTime t;
904 t.start(); 910 t.start();
905 911
906 // Now load the database set and extract the uid's 912 // Now load the database set and extract the uid's
907 // which will be held locally 913 // which will be held locally
908 914
909 LoadQuery lo; 915 LoadQuery lo;
910 OSQLResult res = m_driver->query(&lo); 916 OSQLResult res = m_driver->query(&lo);
911 if ( res.state() != OSQLResult::Success ) 917 if ( res.state() != OSQLResult::Success )
912 return; 918 return;
913 919
914 m_uids = extractUids( res ); 920 m_uids = extractUids( res );
915 921
916 m_changed = false; 922 m_changed = false;
917 923
918 odebug << "Update ends " << t.elapsed() << " ms" << oendl; 924 odebug << "Update ends " << t.elapsed() << " ms" << oendl;
919} 925}
920 926
921UIDArray OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const 927UIDArray OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const
922{ 928{
923 odebug << "extractUids" << oendl; 929 odebug << "extractUids" << oendl;
924 QTime t; 930 QTime t;
925 t.start(); 931 t.start();
926 OSQLResultItem::ValueList list = res.results(); 932 OSQLResultItem::ValueList list = res.results();
927 OSQLResultItem::ValueList::Iterator it; 933 OSQLResultItem::ValueList::Iterator it;
928 UIDArray ints(list.count() ); 934 UIDArray ints(list.count() );
929 odebug << " count = " << list.count() << "" << oendl; 935 odebug << " count = " << list.count() << "" << oendl;
930 936
931 int i = 0; 937 int i = 0;
932 for (it = list.begin(); it != list.end(); ++it ) { 938 for (it = list.begin(); it != list.end(); ++it ) {
933 ints[i] = (*it).data("uid").toInt(); 939 ints[i] = (*it).data("uid").toInt();
934 i++; 940 i++;
935 } 941 }
936 odebug << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl; 942 odebug << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl;
937 943
938 return ints; 944 return ints;
939 945
940} 946}
941 947
942QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const 948QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const
943{ 949{
944 QTime t; 950 QTime t;
945 t.start(); 951 t.start();
946 952
947 int t2needed = 0; 953 int t2needed = 0;
948 int t3needed = 0; 954 int t3needed = 0;
949 QTime t2; 955 QTime t2;
950 t2.start(); 956 t2.start();
951 FindQuery query( uid ); 957 FindQuery query( uid );
952 OSQLResult res_noncustom = m_driver->query( &query ); 958 OSQLResult res_noncustom = m_driver->query( &query );
953 t2needed = t2.elapsed(); 959 t2needed = t2.elapsed();
954 960
955 OSQLResultItem resItem = res_noncustom.first(); 961 OSQLResultItem resItem = res_noncustom.first();
956 962
957 QMap<int, QString> nonCustomMap; 963 QMap<int, QString> nonCustomMap;
958 QTime t3; 964 QTime t3;
959 t3.start(); 965 t3.start();
960 nonCustomMap = fillNonCustomMap( resItem ); 966 nonCustomMap = fillNonCustomMap( resItem );
961 t3needed = t3.elapsed(); 967 t3needed = t3.elapsed();
962 968
963 969
964 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl; 970 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
965 odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed 971 odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
966 << " ms, mapping: " << t3needed << " ms" << oendl; 972 << " ms, mapping: " << t3needed << " ms" << oendl;
967 973
968 return nonCustomMap; 974 return nonCustomMap;
969} 975}
970 976
971/* Returns contact requested by uid and fills cache with contacts requested by uids in the cachelist */ 977/* Returns contact requested by uid and fills cache with contacts requested by uids in the cachelist */
972OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const UIDArray& uidlist )const 978OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const UIDArray& uidlist )const
973{ 979{
974 // We want to get all contacts with one query. 980 // We want to get all contacts with one query.
975 // We don't have to add the given uid to the uidlist, it is expected to be there already (see opimrecordlist.h). 981 // We don't have to add the given uid to the uidlist, it is expected to be there already (see opimrecordlist.h).
976 // All contacts will be stored in the cache, afterwards the contact with the user id "uid" will be returned 982 // All contacts will be stored in the cache, afterwards the contact with the user id "uid" will be returned
977 // by using the cache.. 983 // by using the cache..
978 UIDArray cachelist = uidlist; 984 UIDArray cachelist = uidlist;
979 OPimContact retContact; 985 OPimContact retContact;
980 986
981 odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl; 987 odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl;
982 988
983 QTime t; 989 QTime t;
984 t.start(); 990 t.start();
985 991
986 int t2needed = 0; 992 int t2needed = 0;
987 int t3needed = 0; 993 int t3needed = 0;
988 QTime t2; 994 QTime t2;
989 t2.start(); 995 t2.start();
990 FindQuery query( cachelist ); 996 FindQuery query( cachelist );
991 OSQLResult res_noncustom = m_driver->query( &query ); 997 OSQLResult res_noncustom = m_driver->query( &query );
992 t2needed = t2.elapsed(); 998 t2needed = t2.elapsed();
993 999
994 QMap<int, QString> nonCustomMap; 1000 QMap<int, QString> nonCustomMap;
995 QTime t3; 1001 QTime t3;
996 t3.start(); 1002 t3.start();
997 OSQLResultItem resItem = res_noncustom.first(); 1003 OSQLResultItem resItem = res_noncustom.first();
998 do { 1004 do {
999 OPimContact contact( fillNonCustomMap( resItem ) ); 1005 OPimContact contact( fillNonCustomMap( resItem ) );
1000 contact.setExtraMap( requestCustom( contact.uid() ) ); 1006 contact.setExtraMap( requestCustom( contact.uid() ) );
1001 odebug << "Caching uid: " << contact.uid() << oendl; 1007 odebug << "Caching uid: " << contact.uid() << oendl;
1002 cache( contact ); 1008 cache( contact );
1003 if ( contact.uid() == uid ) 1009 if ( contact.uid() == uid )
1004 retContact = contact; 1010 retContact = contact;
1005 resItem = res_noncustom.next(); 1011 resItem = res_noncustom.next();
1006 } while ( ! res_noncustom.atEnd() ); //atEnd() is true if we are past(!) the list !! 1012 } while ( ! res_noncustom.atEnd() ); //atEnd() is true if we are past(!) the list !!
1007 t3needed = t3.elapsed(); 1013 t3needed = t3.elapsed();
1008 1014
1009 1015
1010 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl; 1016 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
1011 odebug << "RequestContactsAndCache needed: insg.:" << t.elapsed() << " ms, query: " << t2needed 1017 odebug << "RequestContactsAndCache needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
1012 << " ms, mapping: " << t3needed << " ms" << oendl; 1018 << " ms, mapping: " << t3needed << " ms" << oendl;
1013 1019
1014 return retContact; 1020 return retContact;
1015} 1021}
1016 1022
1017QMap<int, QString> OPimContactAccessBackend_SQL::fillNonCustomMap( const OSQLResultItem& resultItem ) const 1023QMap<int, QString> OPimContactAccessBackend_SQL::fillNonCustomMap( const OSQLResultItem& resultItem ) const
1018{ 1024{
1019 QMap<int, QString> nonCustomMap; 1025 QMap<int, QString> nonCustomMap;
1020 1026
1021 // Now loop through all columns 1027 // Now loop through all columns
1022 QStringList fieldList = OPimContactFields::untrfields( false ); 1028 QStringList fieldList = OPimContactFields::untrfields( false );
1023 QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); 1029 QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
1024 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 1030 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
1025 // Get data for the selected column and store it with the 1031 // Get data for the selected column and store it with the
1026 // corresponding id into the map.. 1032 // corresponding id into the map..
1027 1033
1028 int id = translate[*it]; 1034 int id = translate[*it];
1029 QString value = resultItem.data( (*it) ); 1035 QString value = resultItem.data( (*it) );
1030 1036
1031 // odebug << "Reading " << (*it) << "... found: " << value << "" << oendl; 1037 // odebug << "Reading " << (*it) << "... found: " << value << "" << oendl;
1032 1038
1033 switch( id ){ 1039 switch( id ){
1034 case Qtopia::Birthday: 1040 case Qtopia::Birthday:
1035 case Qtopia::Anniversary:{ 1041 case Qtopia::Anniversary:{
1036 // Birthday and Anniversary are encoded special ( yyyy-mm-dd ) 1042 // Birthday and Anniversary are encoded special ( yyyy-mm-dd )
1037 QStringList list = QStringList::split( '-', value ); 1043 QStringList list = QStringList::split( '-', value );
1038 QStringList::Iterator lit = list.begin(); 1044 QStringList::Iterator lit = list.begin();
1039 int year = (*lit).toInt(); 1045 int year = (*lit).toInt();
1040 int month = (*(++lit)).toInt(); 1046 int month = (*(++lit)).toInt();
1041 int day = (*(++lit)).toInt(); 1047 int day = (*(++lit)).toInt();
1042 if ( ( day != 0 ) && ( month != 0 ) && ( year != 0 ) ){ 1048 if ( ( day != 0 ) && ( month != 0 ) && ( year != 0 ) ){
1043 QDate date( year, month, day ); 1049 QDate date( year, month, day );
1044 nonCustomMap.insert( id, OPimDateConversion::dateToString( date ) ); 1050 nonCustomMap.insert( id, OPimDateConversion::dateToString( date ) );
1045 } 1051 }
1046 } 1052 }
1047 break; 1053 break;
1048 case Qtopia::AddressCategory: 1054 case Qtopia::AddressCategory:
1049 odebug << "Category is: " << value << "" << oendl; 1055 odebug << "Category is: " << value << "" << oendl;
1050 default: 1056 default:
1051 nonCustomMap.insert( id, value ); 1057 nonCustomMap.insert( id, value );
1052 } 1058 }
1053 } 1059 }
1054 1060
1055 nonCustomMap.insert( Qtopia::AddressUid, resultItem.data( "uid" ) ); 1061 nonCustomMap.insert( Qtopia::AddressUid, resultItem.data( "uid" ) );
1056 1062
1057 return nonCustomMap; 1063 return nonCustomMap;
1058} 1064}
1059 1065
1060 1066
1061QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const 1067QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const
1062{ 1068{
1063 QTime t; 1069 QTime t;
1064 t.start(); 1070 t.start();
1065 1071
1066 QMap<QString, QString> customMap; 1072 QMap<QString, QString> customMap;
1067 1073
1068 FindCustomQuery query( uid ); 1074 FindCustomQuery query( uid );
1069 OSQLResult res_custom = m_driver->query( &query ); 1075 OSQLResult res_custom = m_driver->query( &query );
1070 1076
1071 if ( res_custom.state() == OSQLResult::Failure ) { 1077 if ( res_custom.state() == OSQLResult::Failure ) {
1072 owarn << "OSQLResult::Failure in find query !!" << oendl; 1078 owarn << "OSQLResult::Failure in find query !!" << oendl;
1073 QMap<QString, QString> empty; 1079 QMap<QString, QString> empty;
1074 return empty; 1080 return empty;
1075 } 1081 }
1076 1082
1077 OSQLResultItem::ValueList list = res_custom.results(); 1083 OSQLResultItem::ValueList list = res_custom.results();
1078 OSQLResultItem::ValueList::Iterator it = list.begin(); 1084 OSQLResultItem::ValueList::Iterator it = list.begin();
1079 for ( ; it != list.end(); ++it ) { 1085 for ( ; it != list.end(); ++it ) {
1080 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); 1086 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) );
1081 } 1087 }
1082 1088
1083 odebug << "RequestCustom needed: " << t.elapsed() << " ms" << oendl; 1089 odebug << "RequestCustom needed: " << t.elapsed() << " ms" << oendl;
1084 return customMap; 1090 return customMap;
1085} 1091}
1086 1092
1087} 1093}