summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
Unidiff
Diffstat (limited to 'libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp180
1 files changed, 123 insertions, 57 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
index 1ea35a8..3142f75 100644
--- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
+++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
@@ -39,6 +39,7 @@
39#include <qpe/global.h> 39#include <qpe/global.h>
40#include <qpe/recordfields.h> 40#include <qpe/recordfields.h>
41 41
42#include <opie2/opimcontact.h>
42#include <opie2/opimcontactfields.h> 43#include <opie2/opimcontactfields.h>
43#include <opie2/opimdateconversion.h> 44#include <opie2/opimdateconversion.h>
44#include <opie2/osqldriver.h> 45#include <opie2/osqldriver.h>
@@ -46,18 +47,18 @@
46#include <opie2/osqlmanager.h> 47#include <opie2/osqlmanager.h>
47#include <opie2/osqlquery.h> 48#include <opie2/osqlquery.h>
48 49
50using namespace Opie;
49using namespace Opie::DB; 51using namespace Opie::DB;
50 52
51 53
52/* 54/*
53 * Implementation of used query types 55 * Implementation of used query types * CREATE query
54 * CREATE query
55 * LOAD query 56 * LOAD query
56 * INSERT 57 * INSERT
57 * REMOVE 58 * REMOVE
58 * CLEAR 59 * CLEAR
59 */ 60 */
60namespace Opie { 61namespace {
61 /** 62 /**
62 * CreateQuery for the Todolist Table 63 * CreateQuery for the Todolist Table
63 */ 64 */
@@ -150,7 +151,7 @@ namespace Opie {
150 151
151 152
152 153
153 // We using three tables to store the information: 154 // We using two tables to store the information:
154 // 1. addressbook : It contains General information about the contact (non custom) 155 // 1. addressbook : It contains General information about the contact (non custom)
155 // 2. custom_data : Not official supported entries 156 // 2. custom_data : Not official supported entries
156 // All tables are connected by the uid of the contact. 157 // All tables are connected by the uid of the contact.
@@ -168,7 +169,7 @@ namespace Opie {
168 } 169 }
169 qu += " );"; 170 qu += " );";
170 171
171 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );"; 172 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );";
172 173
173 return qu; 174 return qu;
174 } 175 }
@@ -221,35 +222,26 @@ namespace Opie {
221 // Hmmm.. Maybe not very cute solution.. 222 // Hmmm.. Maybe not very cute solution..
222 int id = translate[*it]; 223 int id = translate[*it];
223 switch ( id ){ 224 switch ( id ){
224 case Qtopia::Birthday:{ 225 case Qtopia::Birthday:
225 // These entries should stored in a special format 226 case Qtopia::Anniversary:{
226 // year-month-day 227 QDate day;
227 QDate day = m_contact.birthday(); 228 if ( id == Qtopia::Birthday ){
228 if ( day.isValid() ){ 229 day = m_contact.birthday();
229 qu += QString(",\"%1-%2-%3\"")
230 .arg( day.year() )
231 .arg( day.month() )
232 .arg( day.day() );
233 } else { 230 } else {
234 qu += ",\"\""; 231 day = m_contact.anniversary();
235 } 232 }
236 }
237 break;
238 case Qtopia::Anniversary:{
239 // These entries should stored in a special format 233 // These entries should stored in a special format
240 // year-month-day 234 // year-month-day
241 QDate day = m_contact.anniversary();
242 if ( day.isValid() ){ 235 if ( day.isValid() ){
243 qu += QString(",\"%1-%2-%3\"") 236 qu += QString(",\"%1-%2-%3\"")
244 .arg( day.year() ) 237 .arg( QString::number( day.year() ).rightJustify( 4, '0' ) )
245 .arg( day.month() ) 238 .arg( QString::number( day.month() ).rightJustify( 2, '0' ) )
246 .arg( day.day() ); 239 .arg( QString::number( day.day() ).rightJustify( 2, '0' ) );
247 } else { 240 } else {
248 qu += ",\"\""; 241 qu += ",\"\"";
249 } 242 }
250 } 243 }
251 break; 244 break;
252
253 default: 245 default:
254 qu += QString( ",\"%1\"" ).arg( contactMap[id] ); 246 qu += QString( ",\"%1\"" ).arg( contactMap[id] );
255 } 247 }
@@ -268,15 +260,15 @@ namespace Opie {
268 + "," 260 + ","
269 + QString::number( id++ ) 261 + QString::number( id++ )
270 + ",'" 262 + ",'"
271 + it.key() //.latin1() 263 + it.key()
272 + "'," 264 + "',"
273 + "0" // Priority for future enhancements 265 + "0" // Priority for future enhancements
274 + ",'" 266 + ",'"
275 + it.data() //.latin1() 267 + it.data()
276 + "');"; 268 + "');";
277 } 269 }
278 // qu += "commit;"; 270 // qu += "commit;";
279 qWarning("add %s", qu.latin1() ); 271 qDebug("add %s", qu.latin1() );
280 return qu; 272 return qu;
281 } 273 }
282 274
@@ -358,7 +350,7 @@ OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* a
358 const QString& filename ): 350 const QString& filename ):
359 OPimContactAccessBackend(), m_changed(false), m_driver( NULL ) 351 OPimContactAccessBackend(), m_changed(false), m_driver( NULL )
360{ 352{
361 qWarning("C'tor OPimContactAccessBackend_SQL starts"); 353 qDebug("C'tor OPimContactAccessBackend_SQL starts");
362 QTime t; 354 QTime t;
363 t.start(); 355 t.start();
364 356
@@ -375,7 +367,7 @@ OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* a
375 367
376 load(); 368 load();
377 369
378 qWarning("C'tor OPimContactAccessBackend_SQL ends: %d ms", t.elapsed() ); 370 qDebug("C'tor OPimContactAccessBackend_SQL ends: %d ms", t.elapsed() );
379} 371}
380 372
381OPimContactAccessBackend_SQL::~OPimContactAccessBackend_SQL () 373OPimContactAccessBackend_SQL::~OPimContactAccessBackend_SQL ()
@@ -476,54 +468,123 @@ bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact )
476 468
477OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const 469OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
478{ 470{
479 qWarning("OPimContactAccessBackend_SQL::find()"); 471 qDebug("OPimContactAccessBackend_SQL::find()");
480 QTime t; 472 QTime t;
481 t.start(); 473 t.start();
482 474
483 OPimContact retContact( requestNonCustom( uid ) ); 475 OPimContact retContact( requestNonCustom( uid ) );
484 retContact.setExtraMap( requestCustom( uid ) ); 476 retContact.setExtraMap( requestCustom( uid ) );
485 477
486 qWarning("OPimContactAccessBackend_SQL::find() needed: %d ms", t.elapsed() ); 478 qDebug("OPimContactAccessBackend_SQL::find() needed: %d ms", t.elapsed() );
487 return retContact; 479 return retContact;
488} 480}
489 481
490 482
491 483
492QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& d = QDateTime() ) 484QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& qd )
493{ 485{
494 QString qu = "SELECT uid FROM addressbook WHERE"; 486 QString qu = "SELECT uid FROM addressbook WHERE";
487 QString searchQuery ="";
488
489 QDate startDate;
490
491 if ( qd.isValid() )
492 startDate = qd.date();
493 else
494 startDate = QDate::currentDate();
495
495 496
496 QMap<int, QString> queryFields = query.toMap(); 497 QMap<int, QString> queryFields = query.toMap();
497 QStringList fieldList = OPimContactFields::untrfields( false ); 498 QStringList fieldList = OPimContactFields::untrfields( false );
498 QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); 499 QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
499 500
500 // Convert every filled field to a SQL-Query 501 // Convert every filled field to a SQL-Query
501 bool isAnyFieldSelected = false; 502 // bool isAnyFieldSelected = false;
502 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 503 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
504
503 int id = translate[*it]; 505 int id = translate[*it];
504 QString queryStr = queryFields[id]; 506 QString queryStr = queryFields[id];
507 QDate* endDate = 0l;
508
505 if ( !queryStr.isEmpty() ){ 509 if ( !queryStr.isEmpty() ){
506 isAnyFieldSelected = true; 510 // If something is alredy stored in the query, add an "AND"
511 // to the end of the string to prepare for the next ..
512 if ( !searchQuery.isEmpty() )
513 searchQuery += " AND";
514
515 // isAnyFieldSelected = true;
507 switch( id ){ 516 switch( id ){
517 case Qtopia::Birthday:
518 endDate = new QDate( query.birthday() );
519 // Fall through !
520 case Qtopia::Anniversary:
521 if ( endDate == 0l )
522 endDate = new QDate( query.anniversary() );
523
524 if ( settings & OPimContactAccess::DateDiff ) {
525 searchQuery += QString( " (\"%1\" <= '%2-%3-%4\' AND \"%5\" >= '%6-%7-%8')" )
526 .arg( *it )
527 .arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) )
528 .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) )
529 .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) )
530 .arg( *it )
531 .arg( QString::number( startDate.year() ).rightJustify( 4, '0' ) )
532 .arg( QString::number( startDate.month() ).rightJustify( 2, '0' ) )
533 .arg( QString::number( startDate.day() ).rightJustify( 2, '0' ) ) ;
534 }
535
536 if ( settings & OPimContactAccess::DateYear ){
537 if ( settings & OPimContactAccess::DateDiff )
538 searchQuery += " AND";
539
540 searchQuery += QString( " (\"%1\" LIKE '%2-%')" )
541 .arg( *it )
542 .arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) );
543 }
544
545 if ( settings & OPimContactAccess::DateMonth ){
546 if ( ( settings & OPimContactAccess::DateDiff )
547 || ( settings & OPimContactAccess::DateYear ) )
548 searchQuery += " AND";
549
550 searchQuery += QString( " (\"%1\" LIKE '%-%2-%')" )
551 .arg( *it )
552 .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) );
553 }
554
555 if ( settings & OPimContactAccess::DateDay ){
556 if ( ( settings & OPimContactAccess::DateDiff )
557 || ( settings & OPimContactAccess::DateYear )
558 || ( settings & OPimContactAccess::DateMonth ) )
559 searchQuery += " AND";
560
561 searchQuery += QString( " (\"%1\" LIKE '%-%-%2')" )
562 .arg( *it )
563 .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) );
564 }
565
566 break;
508 default: 567 default:
509 // Switching between case sensitive and insensitive... 568 // Switching between case sensitive and insensitive...
510 // LIKE is not case sensitive, GLOB is case sensitive 569 // LIKE is not case sensitive, GLOB is case sensitive
511 // Do exist a better solution to switch this ? 570 // Do exist a better solution to switch this ?
512 if ( settings & OPimContactAccess::IgnoreCase ) 571 if ( settings & OPimContactAccess::IgnoreCase )
513 qu += "(\"" + *it + "\"" + " LIKE " + "'" 572 searchQuery += "(\"" + *it + "\"" + " LIKE " + "'"
514 + queryStr.replace(QRegExp("\\*"),"%") + "'" + ") AND "; 573 + queryStr.replace(QRegExp("\\*"),"%") + "'" + ")";
515 else 574 else
516 qu += "(\"" + *it + "\"" + " GLOB " + "'" 575 searchQuery += "(\"" + *it + "\"" + " GLOB " + "'"
517 + queryStr + "'" + ") AND "; 576 + queryStr + "'" + ")";
518 577
519 } 578 }
520 } 579 }
521 } 580 }
522 // Skip trailing "AND" 581 // Skip trailing "AND"
523 if ( isAnyFieldSelected ) 582 // if ( isAnyFieldSelected )
524 qu = qu.left( qu.length() - 4 ); 583 // qu = qu.left( qu.length() - 4 );
584
585 qu += searchQuery;
525 586
526 qWarning( "queryByExample query: %s", qu.latin1() ); 587 qDebug( "queryByExample query: %s", qu.latin1() );
527 588
528 // Execute query and return the received uid's 589 // Execute query and return the received uid's
529 OSQLRawQuery raw( qu ); 590 OSQLRawQuery raw( qu );
@@ -547,7 +608,12 @@ QArray<int> OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const
547const uint OPimContactAccessBackend_SQL::querySettings() 608const uint OPimContactAccessBackend_SQL::querySettings()
548{ 609{
549 return OPimContactAccess::IgnoreCase 610 return OPimContactAccess::IgnoreCase
550 || OPimContactAccess::WildCards; 611 || OPimContactAccess::WildCards
612 || OPimContactAccess::DateDiff
613 || OPimContactAccess::DateYear
614 || OPimContactAccess::DateMonth
615 || OPimContactAccess::DateDay
616 ;
551} 617}
552 618
553bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const 619bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const
@@ -561,10 +627,10 @@ bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const
561 if ( ( querySettings & ( 627 if ( ( querySettings & (
562 OPimContactAccess::IgnoreCase 628 OPimContactAccess::IgnoreCase
563 | OPimContactAccess::WildCards 629 | OPimContactAccess::WildCards
564 // | OPimContactAccess::DateDiff 630 | OPimContactAccess::DateDiff
565 // | OPimContactAccess::DateYear 631 | OPimContactAccess::DateYear
566 // | OPimContactAccess::DateMonth 632 | OPimContactAccess::DateMonth
567 // | OPimContactAccess::DateDay 633 | OPimContactAccess::DateDay
568 // | OPimContactAccess::RegExp 634 // | OPimContactAccess::RegExp
569 // | OPimContactAccess::ExactMatch 635 // | OPimContactAccess::ExactMatch
570 ) ) != querySettings ) 636 ) ) != querySettings )
@@ -609,7 +675,7 @@ QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int )
609 if ( !asc ) 675 if ( !asc )
610 query += "DESC"; 676 query += "DESC";
611 677
612 // qWarning("sorted query is: %s", query.latin1() ); 678 // qDebug("sorted query is: %s", query.latin1() );
613 679
614 OSQLRawQuery raw( query ); 680 OSQLRawQuery raw( query );
615 OSQLResult res = m_driver->query( &raw ); 681 OSQLResult res = m_driver->query( &raw );
@@ -620,14 +686,14 @@ QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int )
620 686
621 QArray<int> list = extractUids( res ); 687 QArray<int> list = extractUids( res );
622 688
623 qWarning("sorted needed %d ms!", t.elapsed() ); 689 qDebug("sorted needed %d ms!", t.elapsed() );
624 return list; 690 return list;
625} 691}
626 692
627 693
628void OPimContactAccessBackend_SQL::update() 694void OPimContactAccessBackend_SQL::update()
629{ 695{
630 qWarning("Update starts"); 696 qDebug("Update starts");
631 QTime t; 697 QTime t;
632 t.start(); 698 t.start();
633 699
@@ -643,25 +709,25 @@ void OPimContactAccessBackend_SQL::update()
643 709
644 m_changed = false; 710 m_changed = false;
645 711
646 qWarning("Update ends %d ms", t.elapsed() ); 712 qDebug("Update ends %d ms", t.elapsed() );
647} 713}
648 714
649QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const 715QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const
650{ 716{
651 qWarning("extractUids"); 717 qDebug("extractUids");
652 QTime t; 718 QTime t;
653 t.start(); 719 t.start();
654 OSQLResultItem::ValueList list = res.results(); 720 OSQLResultItem::ValueList list = res.results();
655 OSQLResultItem::ValueList::Iterator it; 721 OSQLResultItem::ValueList::Iterator it;
656 QArray<int> ints(list.count() ); 722 QArray<int> ints(list.count() );
657 qWarning(" count = %d", list.count() ); 723 qDebug(" count = %d", list.count() );
658 724
659 int i = 0; 725 int i = 0;
660 for (it = list.begin(); it != list.end(); ++it ) { 726 for (it = list.begin(); it != list.end(); ++it ) {
661 ints[i] = (*it).data("uid").toInt(); 727 ints[i] = (*it).data("uid").toInt();
662 i++; 728 i++;
663 } 729 }
664 qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() ); 730 qDebug("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() );
665 731
666 return ints; 732 return ints;
667 733
@@ -696,7 +762,7 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
696 int id = translate[*it]; 762 int id = translate[*it];
697 QString value = resItem.data( (*it) ); 763 QString value = resItem.data( (*it) );
698 764
699 // qWarning("Reading %s... found: %s", (*it).latin1(), value.latin1() ); 765 // qDebug("Reading %s... found: %s", (*it).latin1(), value.latin1() );
700 766
701 switch( id ){ 767 switch( id ){
702 case Qtopia::Birthday: 768 case Qtopia::Birthday:
@@ -714,7 +780,7 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
714 } 780 }
715 break; 781 break;
716 case Qtopia::AddressCategory: 782 case Qtopia::AddressCategory:
717 qWarning("Category is: %s", value.latin1() ); 783 qDebug("Category is: %s", value.latin1() );
718 default: 784 default:
719 nonCustomMap.insert( id, value ); 785 nonCustomMap.insert( id, value );
720 } 786 }
@@ -724,8 +790,8 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
724 nonCustomMap.insert( Qtopia::AddressUid, resItem.data( "uid" ) ); 790 nonCustomMap.insert( Qtopia::AddressUid, resItem.data( "uid" ) );
725 t3needed = t3.elapsed(); 791 t3needed = t3.elapsed();
726 792
727 // qWarning("Adding UID: %s", resItem.data( "uid" ).latin1() ); 793 // qDebug("Adding UID: %s", resItem.data( "uid" ).latin1() );
728 qWarning("RequestNonCustom needed: insg.:%d ms, query: %d ms, mapping: %d ms", 794 qDebug("RequestNonCustom needed: insg.:%d ms, query: %d ms, mapping: %d ms",
729 t.elapsed(), t2needed, t3needed ); 795 t.elapsed(), t2needed, t3needed );
730 796
731 return nonCustomMap; 797 return nonCustomMap;
@@ -753,7 +819,7 @@ QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) c
753 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); 819 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) );
754 } 820 }
755 821
756 qWarning("RequestCustom needed: %d ms", t.elapsed() ); 822 qDebug("RequestCustom needed: %d ms", t.elapsed() );
757 return customMap; 823 return customMap;
758} 824}
759 825