Diffstat (limited to 'libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp | 180 |
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 | |||
@@ -41,2 +41,3 @@ | |||
41 | 41 | ||
42 | #include <opie2/opimcontact.h> | ||
42 | #include <opie2/opimcontactfields.h> | 43 | #include <opie2/opimcontactfields.h> |
@@ -48,2 +49,3 @@ | |||
48 | 49 | ||
50 | using namespace Opie; | ||
49 | using namespace Opie::DB; | 51 | using namespace Opie::DB; |
@@ -52,4 +54,3 @@ using namespace Opie::DB; | |||
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 |
@@ -59,3 +60,3 @@ using namespace Opie::DB; | |||
59 | */ | 60 | */ |
60 | namespace Opie { | 61 | namespace { |
61 | /** | 62 | /** |
@@ -152,3 +153,3 @@ namespace Opie { | |||
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) |
@@ -170,3 +171,3 @@ namespace Opie { | |||
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 | ||
@@ -223,25 +224,17 @@ namespace Opie { | |||
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 { |
@@ -251,3 +244,2 @@ namespace Opie { | |||
251 | break; | 244 | break; |
252 | |||
253 | default: | 245 | default: |
@@ -270,3 +262,3 @@ namespace Opie { | |||
270 | + ",'" | 262 | + ",'" |
271 | + it.key() //.latin1() | 263 | + it.key() |
272 | + "'," | 264 | + "'," |
@@ -274,3 +266,3 @@ namespace Opie { | |||
274 | + ",'" | 266 | + ",'" |
275 | + it.data() //.latin1() | 267 | + it.data() |
276 | + "');"; | 268 | + "');"; |
@@ -278,3 +270,3 @@ namespace Opie { | |||
278 | // qu += "commit;"; | 270 | // qu += "commit;"; |
279 | qWarning("add %s", qu.latin1() ); | 271 | qDebug("add %s", qu.latin1() ); |
280 | return qu; | 272 | return qu; |
@@ -360,3 +352,3 @@ OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* a | |||
360 | { | 352 | { |
361 | qWarning("C'tor OPimContactAccessBackend_SQL starts"); | 353 | qDebug("C'tor OPimContactAccessBackend_SQL starts"); |
362 | QTime t; | 354 | QTime t; |
@@ -377,3 +369,3 @@ OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* a | |||
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 | } |
@@ -478,3 +470,3 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const | |||
478 | { | 470 | { |
479 | qWarning("OPimContactAccessBackend_SQL::find()"); | 471 | qDebug("OPimContactAccessBackend_SQL::find()"); |
480 | QTime t; | 472 | QTime t; |
@@ -485,3 +477,3 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const | |||
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; |
@@ -491,5 +483,14 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const | |||
491 | 483 | ||
492 | QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& d = QDateTime() ) | 484 | QArray<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 | ||
@@ -500,9 +501,67 @@ QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &qu | |||
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: |
@@ -512,7 +571,7 @@ QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &qu | |||
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 | ||
@@ -522,6 +581,8 @@ QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &qu | |||
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 | ||
@@ -549,3 +610,8 @@ const uint OPimContactAccessBackend_SQL::querySettings() | |||
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 | } |
@@ -563,6 +629,6 @@ bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const | |||
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 |
@@ -611,3 +677,3 @@ QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int ) | |||
611 | 677 | ||
612 | // qWarning("sorted query is: %s", query.latin1() ); | 678 | // qDebug("sorted query is: %s", query.latin1() ); |
613 | 679 | ||
@@ -622,3 +688,3 @@ QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int ) | |||
622 | 688 | ||
623 | qWarning("sorted needed %d ms!", t.elapsed() ); | 689 | qDebug("sorted needed %d ms!", t.elapsed() ); |
624 | return list; | 690 | return list; |
@@ -629,3 +695,3 @@ void OPimContactAccessBackend_SQL::update() | |||
629 | { | 695 | { |
630 | qWarning("Update starts"); | 696 | qDebug("Update starts"); |
631 | QTime t; | 697 | QTime t; |
@@ -645,3 +711,3 @@ void OPimContactAccessBackend_SQL::update() | |||
645 | 711 | ||
646 | qWarning("Update ends %d ms", t.elapsed() ); | 712 | qDebug("Update ends %d ms", t.elapsed() ); |
647 | } | 713 | } |
@@ -650,3 +716,3 @@ QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const | |||
650 | { | 716 | { |
651 | qWarning("extractUids"); | 717 | qDebug("extractUids"); |
652 | QTime t; | 718 | QTime t; |
@@ -656,3 +722,3 @@ QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const | |||
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 | ||
@@ -663,3 +729,3 @@ QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const | |||
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 | ||
@@ -698,3 +764,3 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co | |||
698 | 764 | ||
699 | // qWarning("Reading %s... found: %s", (*it).latin1(), value.latin1() ); | 765 | // qDebug("Reading %s... found: %s", (*it).latin1(), value.latin1() ); |
700 | 766 | ||
@@ -716,3 +782,3 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co | |||
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: |
@@ -726,4 +792,4 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co | |||
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 ); |
@@ -755,3 +821,3 @@ QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) c | |||
755 | 821 | ||
756 | qWarning("RequestCustom needed: %d ms", t.elapsed() ); | 822 | qDebug("RequestCustom needed: %d ms", t.elapsed() ); |
757 | return customMap; | 823 | return customMap; |