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.cpp154
1 files changed, 124 insertions, 30 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
index dda23cc..abfd944 100644
--- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
+++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
@@ -291,46 +291,47 @@ namespace {
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 QArray<int>& ints) 297 FindQuery::FindQuery(const QArray<int>& 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
306 return multi();
305 } 307 }
306 /* 308
307 else 309 QString FindQuery::multi()const {
308 return multi(); 310 QString qu = "select * from addressbook where";
309 } 311 for (uint i = 0; i < m_uids.count(); i++ ) {
310 QString FindQuery::multi()const { 312 qu += " uid = " + QString::number( m_uids[i] ) + " OR";
311 QString qu = "select uid, type, value from addressbook where"; 313 }
312 for (uint i = 0; i < m_uids.count(); i++ ) { 314 qu.remove( qu.length()-2, 2 ); // Hmmmm..
313 qu += " UID = " + QString::number( m_uids[i] ) + " OR"; 315
314 } 316 odebug << "find query: " << qu << "" << oendl;
315 qu.remove( qu.length()-2, 2 ); // Hmmmm.. 317 return qu;
316 return qu;
317 } 318 }
318 */
319 QString FindQuery::single()const{
320 QString qu = "select *";
321 qu += " from addressbook where uid = " + QString::number(m_uid);
322 319
323 // owarn << "find query: " << qu << "" << oendl; 320 QString FindQuery::single()const{
324 return qu; 321 QString qu = "select *";
322 qu += " from addressbook where uid = " + QString::number(m_uid);
323
324 // owarn << "find query: " << qu << "" << oendl;
325 return qu;
325 } 326 }
326 327
327 328
328 FindCustomQuery::FindCustomQuery(int uid) 329 FindCustomQuery::FindCustomQuery(int uid)
329 : OSQLQuery(), m_uid( uid ) { 330 : OSQLQuery(), m_uid( uid ) {
330 } 331 }
331 FindCustomQuery::FindCustomQuery(const QArray<int>& ints) 332 FindCustomQuery::FindCustomQuery(const QArray<int>& ints)
332 : OSQLQuery(), m_uids( ints ){ 333 : OSQLQuery(), m_uids( ints ){
333 } 334 }
334 FindCustomQuery::~FindCustomQuery() { 335 FindCustomQuery::~FindCustomQuery() {
335 } 336 }
336 QString FindCustomQuery::query()const{ 337 QString FindCustomQuery::query()const{
@@ -464,35 +465,76 @@ bool OPimContactAccessBackend_SQL::remove ( int uid )
464 465
465bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact ) 466bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact )
466{ 467{
467 if ( !remove( contact.uid() ) ) 468 if ( !remove( contact.uid() ) )
468 return false; 469 return false;
469 470
470 return add( contact ); 471 return add( contact );
471} 472}
472 473
473 474
474OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const 475OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
475{ 476{
476 odebug << "OPimContactAccessBackend_SQL::find()" << oendl; 477 odebug << "OPimContactAccessBackend_SQL::find(" << uid << ")" << oendl;
477 QTime t; 478 QTime t;
478 t.start(); 479 t.start();
479 480
480 OPimContact retContact( requestNonCustom( uid ) ); 481 OPimContact retContact( requestNonCustom( uid ) );
481 retContact.setExtraMap( requestCustom( uid ) ); 482 retContact.setExtraMap( requestCustom( uid ) );
482 483
483 odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl; 484 odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl;
484 return retContact; 485 return retContact;
485} 486}
486 487
488OPimContact OPimContactAccessBackend_SQL::find( int uid, const QArray<int>& queryUids, uint current, Frontend::CacheDirection direction ) const
489{
490 odebug << "OPimContactAccessBackend_SQL::find( ..multi.. )" << oendl;
491 odebug << "searching for " << uid << "" << oendl;
492
493 QTime t;
494 t.start();
495
496 uint numReadAhead = readAhead();
497 QArray<int> searchList( numReadAhead );
498
499 uint size =0;
500
501 // Build an array with all elements which should be requested and cached
502 // We will just request "numReadAhead" elements, starting from "current" position in
503 // the list of many uids !
504 switch( direction ) {
505 /* forward */
506 case Frontend::Forward:
507 for ( uint i = current; i < queryUids.count() && size < numReadAhead; i++ ) {
508 searchList[size] = queryUids[i];
509 size++;
510 }
511 break;
512 /* reverse */
513 case Frontend::Reverse:
514 for ( uint i = current; i != 0 && size < numReadAhead; i-- ) {
515 searchList[size] = queryUids[i];
516 size++;
517 }
518 break;
519 }
520
521 //Shrink to real size..
522 searchList.resize( size );
523
524 OPimContact retContact( requestContactsAndCache( uid, searchList ) );
525
526 odebug << "OPimContactAccessBackend_SQL::find( ..multi.. ) needed: " << t.elapsed() << " ms" << oendl;
527 return retContact;
528}
487 529
488 530
489QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& qd ) 531QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& qd )
490{ 532{
491 QString qu = "SELECT uid FROM addressbook WHERE"; 533 QString qu = "SELECT uid FROM addressbook WHERE";
492 QString searchQuery =""; 534 QString searchQuery ="";
493 535
494 QDate startDate; 536 QDate startDate;
495 537
496 if ( qd.isValid() ) 538 if ( qd.isValid() )
497 startDate = qd.date(); 539 startDate = qd.date();
498 else 540 else
@@ -759,47 +801,104 @@ QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const
759 } 801 }
760 odebug << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl; 802 odebug << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl;
761 803
762 return ints; 804 return ints;
763 805
764} 806}
765 807
766QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const 808QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const
767{ 809{
768 QTime t; 810 QTime t;
769 t.start(); 811 t.start();
770 812
771 QMap<int, QString> nonCustomMap;
772
773 int t2needed = 0; 813 int t2needed = 0;
774 int t3needed = 0; 814 int t3needed = 0;
775 QTime t2; 815 QTime t2;
776 t2.start(); 816 t2.start();
777 FindQuery query( uid ); 817 FindQuery query( uid );
778 OSQLResult res_noncustom = m_driver->query( &query ); 818 OSQLResult res_noncustom = m_driver->query( &query );
779 t2needed = t2.elapsed(); 819 t2needed = t2.elapsed();
780 820
781 OSQLResultItem resItem = res_noncustom.first(); 821 OSQLResultItem resItem = res_noncustom.first();
782 822
823 QMap<int, QString> nonCustomMap;
783 QTime t3; 824 QTime t3;
784 t3.start(); 825 t3.start();
826 nonCustomMap = fillNonCustomMap( resItem );
827 t3needed = t3.elapsed();
828
829
830 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
831 odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
832 << " ms, mapping: " << t3needed << " ms" << oendl;
833
834 return nonCustomMap;
835}
836
837/* Returns contact requested by uid and fills cache with contacts requested by uids in the cachelist */
838OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const QArray<int>& uidlist )const
839{
840 // We want to get all contacts with one query.
841 // We don't have to add the given uid to the uidlist, it is expected to be there already (see opimrecordlist.h).
842 // All contacts will be stored in the cache, afterwards the contact with the user id "uid" will be returned
843 // by using the cache..
844 QArray<int> cachelist = uidlist;
845
846 odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl;
847
848 QTime t;
849 t.start();
850
851 int t2needed = 0;
852 int t3needed = 0;
853 QTime t2;
854 t2.start();
855 FindQuery query( cachelist );
856 OSQLResult res_noncustom = m_driver->query( &query );
857 t2needed = t2.elapsed();
858
859 QMap<int, QString> nonCustomMap;
860 QTime t3;
861 t3.start();
862 OSQLResultItem resItem = res_noncustom.first();
863 do {
864 OPimContact contact( fillNonCustomMap( resItem ) );
865 contact.setExtraMap( requestCustom( contact.uid() ) );
866 odebug << "Caching uid: " << contact.uid() << oendl;
867 cache( contact );
868 resItem = res_noncustom.next();
869 } while ( ! res_noncustom.atEnd() ); //atEnd() is true if we are past(!) the list !!
870 t3needed = t3.elapsed();
871
872
873 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
874 odebug << "RequestContactsAndCache needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
875 << " ms, mapping: " << t3needed << " ms" << oendl;
876
877 return cacheFind( uid );
878}
879
880QMap<int, QString> OPimContactAccessBackend_SQL::fillNonCustomMap( const OSQLResultItem& resultItem ) const
881{
882 QMap<int, QString> nonCustomMap;
883
785 // Now loop through all columns 884 // Now loop through all columns
786 QStringList fieldList = OPimContactFields::untrfields( false ); 885 QStringList fieldList = OPimContactFields::untrfields( false );
787 QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); 886 QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
788 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 887 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
789 // Get data for the selected column and store it with the 888 // Get data for the selected column and store it with the
790 // corresponding id into the map.. 889 // corresponding id into the map..
791 890
792 int id = translate[*it]; 891 int id = translate[*it];
793 QString value = resItem.data( (*it) ); 892 QString value = resultItem.data( (*it) );
794 893
795 // odebug << "Reading " << (*it) << "... found: " << value << "" << oendl; 894 // odebug << "Reading " << (*it) << "... found: " << value << "" << oendl;
796 895
797 switch( id ){ 896 switch( id ){
798 case Qtopia::Birthday: 897 case Qtopia::Birthday:
799 case Qtopia::Anniversary:{ 898 case Qtopia::Anniversary:{
800 // Birthday and Anniversary are encoded special ( yyyy-mm-dd ) 899 // Birthday and Anniversary are encoded special ( yyyy-mm-dd )
801 QStringList list = QStringList::split( '-', value ); 900 QStringList list = QStringList::split( '-', value );
802 QStringList::Iterator lit = list.begin(); 901 QStringList::Iterator lit = list.begin();
803 int year = (*lit).toInt(); 902 int year = (*lit).toInt();
804 int month = (*(++lit)).toInt(); 903 int month = (*(++lit)).toInt();
805 int day = (*(++lit)).toInt(); 904 int day = (*(++lit)).toInt();
@@ -807,35 +906,30 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
807 QDate date( year, month, day ); 906 QDate date( year, month, day );
808 nonCustomMap.insert( id, OPimDateConversion::dateToString( date ) ); 907 nonCustomMap.insert( id, OPimDateConversion::dateToString( date ) );
809 } 908 }
810 } 909 }
811 break; 910 break;
812 case Qtopia::AddressCategory: 911 case Qtopia::AddressCategory:
813 odebug << "Category is: " << value << "" << oendl; 912 odebug << "Category is: " << value << "" << oendl;
814 default: 913 default:
815 nonCustomMap.insert( id, value ); 914 nonCustomMap.insert( id, value );
816 } 915 }
817 } 916 }
818 917
819 // First insert uid 918 nonCustomMap.insert( Qtopia::AddressUid, resultItem.data( "uid" ) );
820 nonCustomMap.insert( Qtopia::AddressUid, resItem.data( "uid" ) );
821 t3needed = t3.elapsed();
822
823 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
824 odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
825 << " ms, mapping: " << t3needed << " ms" << oendl;
826 919
827 return nonCustomMap; 920 return nonCustomMap;
828} 921}
829 922
923
830QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const 924QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const
831{ 925{
832 QTime t; 926 QTime t;
833 t.start(); 927 t.start();
834 928
835 QMap<QString, QString> customMap; 929 QMap<QString, QString> customMap;
836 930
837 FindCustomQuery query( uid ); 931 FindCustomQuery query( uid );
838 OSQLResult res_custom = m_driver->query( &query ); 932 OSQLResult res_custom = m_driver->query( &query );
839 933
840 if ( res_custom.state() == OSQLResult::Failure ) { 934 if ( res_custom.state() == OSQLResult::Failure ) {
841 owarn << "OSQLResult::Failure in find query !!" << oendl; 935 owarn << "OSQLResult::Failure in find query !!" << oendl;