Diffstat (limited to 'libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp | 144 |
1 files changed, 120 insertions, 24 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp index 3d284f7..9375f43 100644 --- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp +++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp | |||
@@ -124,35 +124,35 @@ namespace { | |||
124 | /** | 124 | /** |
125 | * a find query for noncustom elements | 125 | * a find query for noncustom elements |
126 | */ | 126 | */ |
127 | class FindQuery : public OSQLQuery { | 127 | class FindQuery : public OSQLQuery { |
128 | public: | 128 | public: |
129 | FindQuery(int uid); | 129 | FindQuery(int uid); |
130 | FindQuery(const QArray<int>& ); | 130 | FindQuery(const UIDArray& ); |
131 | ~FindQuery(); | 131 | ~FindQuery(); |
132 | QString query()const; | 132 | QString query()const; |
133 | private: | 133 | private: |
134 | QString single()const; | 134 | QString single()const; |
135 | QString multi()const; | 135 | QString multi()const; |
136 | QArray<int> m_uids; | 136 | UIDArray m_uids; |
137 | int m_uid; | 137 | int m_uid; |
138 | }; | 138 | }; |
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 QArray<int>& ); | 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 | QArray<int> 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: |
@@ -291,13 +291,13 @@ 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 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 ) |
@@ -326,13 +326,13 @@ namespace { | |||
326 | } | 326 | } |
327 | 327 | ||
328 | 328 | ||
329 | FindCustomQuery::FindCustomQuery(int uid) | 329 | FindCustomQuery::FindCustomQuery(int uid) |
330 | : OSQLQuery(), m_uid( uid ) { | 330 | : OSQLQuery(), m_uid( uid ) { |
331 | } | 331 | } |
332 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) | 332 | FindCustomQuery::FindCustomQuery(const UIDArray& ints) |
333 | : OSQLQuery(), m_uids( ints ){ | 333 | : OSQLQuery(), m_uids( ints ){ |
334 | } | 334 | } |
335 | FindCustomQuery::~FindCustomQuery() { | 335 | FindCustomQuery::~FindCustomQuery() { |
336 | } | 336 | } |
337 | QString FindCustomQuery::query()const{ | 337 | QString FindCustomQuery::query()const{ |
338 | // if ( m_uids.count() == 0 ) | 338 | // if ( m_uids.count() == 0 ) |
@@ -419,13 +419,13 @@ void OPimContactAccessBackend_SQL::clear () | |||
419 | 419 | ||
420 | bool OPimContactAccessBackend_SQL::wasChangedExternally() | 420 | bool OPimContactAccessBackend_SQL::wasChangedExternally() |
421 | { | 421 | { |
422 | return false; | 422 | return false; |
423 | } | 423 | } |
424 | 424 | ||
425 | QArray<int> OPimContactAccessBackend_SQL::allRecords() const | 425 | UIDArray OPimContactAccessBackend_SQL::allRecords() const |
426 | { | 426 | { |
427 | 427 | ||
428 | // FIXME: Think about cute handling of changed tables.. | 428 | // FIXME: Think about cute handling of changed tables.. |
429 | // Thus, we don't have to call update here... | 429 | // Thus, we don't have to call update here... |
430 | if ( m_changed ) | 430 | if ( m_changed ) |
431 | ((OPimContactAccessBackend_SQL*)this)->update(); | 431 | ((OPimContactAccessBackend_SQL*)this)->update(); |
@@ -482,13 +482,13 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const | |||
482 | retContact.setExtraMap( requestCustom( uid ) ); | 482 | retContact.setExtraMap( requestCustom( uid ) ); |
483 | 483 | ||
484 | odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl; | 484 | odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl; |
485 | return retContact; | 485 | return retContact; |
486 | } | 486 | } |
487 | 487 | ||
488 | OPimContact OPimContactAccessBackend_SQL::find( int uid, const QArray<int>& queryUids, uint current, Frontend::CacheDirection direction ) const | 488 | OPimContact OPimContactAccessBackend_SQL::find( int uid, const UIDArray& queryUids, uint current, Frontend::CacheDirection direction ) const |
489 | { | 489 | { |
490 | odebug << "OPimContactAccessBackend_SQL::find( ..multi.. )" << oendl; | 490 | odebug << "OPimContactAccessBackend_SQL::find( ..multi.. )" << oendl; |
491 | odebug << "searching for " << uid << "" << oendl; | 491 | odebug << "searching for " << uid << "" << oendl; |
492 | 492 | ||
493 | QTime t; | 493 | QTime t; |
494 | t.start(); | 494 | t.start(); |
@@ -525,13 +525,14 @@ OPimContact OPimContactAccessBackend_SQL::find( int uid, const QArray<int>& quer | |||
525 | 525 | ||
526 | odebug << "OPimContactAccessBackend_SQL::find( ..multi.. ) needed: " << t.elapsed() << " ms" << oendl; | 526 | odebug << "OPimContactAccessBackend_SQL::find( ..multi.. ) needed: " << t.elapsed() << " ms" << oendl; |
527 | return retContact; | 527 | return retContact; |
528 | } | 528 | } |
529 | 529 | ||
530 | 530 | ||
531 | QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& qd ) | 531 | UIDArray OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, |
532 | const QDateTime& qd ) const | ||
532 | { | 533 | { |
533 | QString qu = "SELECT uid FROM addressbook WHERE"; | 534 | QString qu = "SELECT uid FROM addressbook WHERE"; |
534 | QString searchQuery =""; | 535 | QString searchQuery =""; |
535 | 536 | ||
536 | QDate startDate; | 537 | QDate startDate; |
537 | 538 | ||
@@ -636,22 +637,22 @@ QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &qu | |||
636 | odebug << "queryByExample query: " << qu << "" << oendl; | 637 | odebug << "queryByExample query: " << qu << "" << oendl; |
637 | 638 | ||
638 | // Execute query and return the received uid's | 639 | // Execute query and return the received uid's |
639 | OSQLRawQuery raw( qu ); | 640 | OSQLRawQuery raw( qu ); |
640 | OSQLResult res = m_driver->query( &raw ); | 641 | OSQLResult res = m_driver->query( &raw ); |
641 | if ( res.state() != OSQLResult::Success ){ | 642 | if ( res.state() != OSQLResult::Success ){ |
642 | QArray<int> empty; | 643 | UIDArray empty; |
643 | return empty; | 644 | return empty; |
644 | } | 645 | } |
645 | 646 | ||
646 | QArray<int> list = extractUids( res ); | 647 | UIDArray list = extractUids( res ); |
647 | 648 | ||
648 | return list; | 649 | return list; |
649 | } | 650 | } |
650 | 651 | ||
651 | QArray<int> OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const | 652 | UIDArray OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const |
652 | { | 653 | { |
653 | #if 0 | 654 | #if 0 |
654 | QArray<int> nix(0); | 655 | QArray<int> nix(0); |
655 | return nix; | 656 | return nix; |
656 | 657 | ||
657 | #else | 658 | #else |
@@ -676,13 +677,13 @@ QArray<int> OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const | |||
676 | return extractUids( res ); | 677 | return extractUids( res ); |
677 | 678 | ||
678 | 679 | ||
679 | #endif | 680 | #endif |
680 | } | 681 | } |
681 | 682 | ||
682 | const uint OPimContactAccessBackend_SQL::querySettings() | 683 | const uint OPimContactAccessBackend_SQL::querySettings() const |
683 | { | 684 | { |
684 | return OPimContactAccess::IgnoreCase | 685 | return OPimContactAccess::IgnoreCase |
685 | | OPimContactAccess::WildCards | 686 | | OPimContactAccess::WildCards |
686 | | OPimContactAccess::DateDiff | 687 | | OPimContactAccess::DateDiff |
687 | | OPimContactAccess::DateYear | 688 | | OPimContactAccess::DateYear |
688 | | OPimContactAccess::DateMonth | 689 | | OPimContactAccess::DateMonth |
@@ -735,33 +736,128 @@ bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const | |||
735 | default: | 736 | default: |
736 | return ( false ); | 737 | return ( false ); |
737 | } | 738 | } |
738 | 739 | ||
739 | } | 740 | } |
740 | 741 | ||
741 | QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int ) | 742 | UIDArray OPimContactAccessBackend_SQL::sorted( const UIDArray& ar, bool asc, int sortOrder, |
743 | int filter, const QArray<int>& categories )const | ||
742 | { | 744 | { |
743 | QTime t; | 745 | QTime t; |
744 | t.start(); | 746 | t.start(); |
745 | 747 | ||
746 | QString query = "SELECT uid FROM addressbook "; | 748 | QString query = "SELECT uid FROM addressbook"; |
747 | query += "ORDER BY \"Last Name\" "; | 749 | |
750 | query += " WHERE ("; | ||
751 | for ( uint i = 0; i < ar.count(); i++ ) { | ||
752 | query += " uid = " + QString::number( ar[i] ) + " OR"; | ||
753 | } | ||
754 | query.remove( query.length()-2, 2 ); // Hmmmm.. | ||
755 | query += ")"; | ||
756 | |||
757 | |||
758 | if ( filter != OPimBase::FilterOff ){ | ||
759 | if ( filter & OPimContactAccess::DoNotShowWithCategory ){ | ||
760 | query += " AND ( \"Categories\" == '' )"; | ||
761 | } else if ( filter & OPimBase::FilterCategory ){ | ||
762 | query += " AND ("; | ||
763 | for ( uint i = 0; i < categories.count(); i++ ){ | ||
764 | query += "\"Categories\" LIKE"; | ||
765 | query += QString( " '%" ) + QString::number( categories[i] ) + "%' OR"; | ||
766 | } | ||
767 | query.remove( query.length()-2, 2 ); // Hmmmm.. | ||
768 | query += ")"; | ||
769 | } | ||
770 | |||
771 | if ( filter & OPimContactAccess::DoNotShowWithoutChildren ){ | ||
772 | query += " AND ( \"Children\" != '' )"; | ||
773 | } | ||
774 | |||
775 | if ( filter & OPimContactAccess::DoNotShowWithoutAnniversary ){ | ||
776 | query += " AND ( \"Anniversary\" != '' )"; | ||
777 | } | ||
778 | |||
779 | if ( filter & OPimContactAccess::DoNotShowWithoutBirthday ){ | ||
780 | query += " AND ( \"Birthday\" != '' )"; | ||
781 | } | ||
782 | |||
783 | if ( filter & OPimContactAccess::DoNotShowWithoutHomeAddress ){ | ||
784 | // Expect that no Street means no Address, too! (eilers) | ||
785 | query += " AND ( \"Home Street\" != '' )"; | ||
786 | } | ||
787 | |||
788 | if ( filter & OPimContactAccess::DoNotShowWithoutBusinessAddress ){ | ||
789 | // Expect that no Street means no Address, too! (eilers) | ||
790 | query += " AND ( \"Business Street\" != '' )"; | ||
791 | } | ||
792 | |||
793 | } | ||
794 | |||
795 | query += " ORDER BY"; | ||
796 | |||
797 | switch ( sortOrder ) { | ||
798 | case OPimContactAccess::SortSummary: | ||
799 | query += " \"Notes\""; | ||
800 | break; | ||
801 | case OPimContactAccess::SortByCategory: | ||
802 | query += " \"Categories\""; | ||
803 | break; | ||
804 | case OPimContactAccess::SortByDate: | ||
805 | query += " \"\""; | ||
806 | break; | ||
807 | case OPimContactAccess::SortTitle: | ||
808 | query += " \"Name Title\""; | ||
809 | break; | ||
810 | case OPimContactAccess::SortFirstName: | ||
811 | query += " \"First Name\""; | ||
812 | break; | ||
813 | case OPimContactAccess::SortMiddleName: | ||
814 | query += " \"Middle Name\""; | ||
815 | break; | ||
816 | case OPimContactAccess::SortLastName: | ||
817 | query += " \"Last Name\""; | ||
818 | break; | ||
819 | case OPimContactAccess::SortFileAsName: | ||
820 | query += " \"File As\""; | ||
821 | break; | ||
822 | case OPimContactAccess::SortSuffix: | ||
823 | query += " \"Suffix\""; | ||
824 | break; | ||
825 | case OPimContactAccess::SortEmail: | ||
826 | query += " \"Default Email\""; | ||
827 | break; | ||
828 | case OPimContactAccess::SortNickname: | ||
829 | query += " \"Nickname\""; | ||
830 | break; | ||
831 | case OPimContactAccess::SortAnniversary: | ||
832 | query += " \"Anniversary\""; | ||
833 | break; | ||
834 | case OPimContactAccess::SortBirthday: | ||
835 | query += " \"Birthday\""; | ||
836 | break; | ||
837 | case OPimContactAccess::SortGender: | ||
838 | query += " \"Gender\""; | ||
839 | break; | ||
840 | default: | ||
841 | query += " \"Last Name\""; | ||
842 | } | ||
748 | 843 | ||
749 | if ( !asc ) | 844 | if ( !asc ) |
750 | query += "DESC"; | 845 | query += " DESC"; |
846 | |||
751 | 847 | ||
752 | // odebug << "sorted query is: " << query << "" << oendl; | 848 | odebug << "sorted query is: " << query << "" << oendl; |
753 | 849 | ||
754 | OSQLRawQuery raw( query ); | 850 | OSQLRawQuery raw( query ); |
755 | OSQLResult res = m_driver->query( &raw ); | 851 | OSQLResult res = m_driver->query( &raw ); |
756 | if ( res.state() != OSQLResult::Success ){ | 852 | if ( res.state() != OSQLResult::Success ){ |
757 | QArray<int> empty; | 853 | UIDArray empty; |
758 | return empty; | 854 | return empty; |
759 | } | 855 | } |
760 | 856 | ||
761 | QArray<int> list = extractUids( res ); | 857 | UIDArray list = extractUids( res ); |
762 | 858 | ||
763 | odebug << "sorted needed " << t.elapsed() << " ms!" << oendl; | 859 | odebug << "sorted needed " << t.elapsed() << " ms!" << oendl; |
764 | return list; | 860 | return list; |
765 | } | 861 | } |
766 | 862 | ||
767 | 863 | ||
@@ -783,20 +879,20 @@ void OPimContactAccessBackend_SQL::update() | |||
783 | 879 | ||
784 | m_changed = false; | 880 | m_changed = false; |
785 | 881 | ||
786 | odebug << "Update ends " << t.elapsed() << " ms" << oendl; | 882 | odebug << "Update ends " << t.elapsed() << " ms" << oendl; |
787 | } | 883 | } |
788 | 884 | ||
789 | QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const | 885 | UIDArray OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const |
790 | { | 886 | { |
791 | odebug << "extractUids" << oendl; | 887 | odebug << "extractUids" << oendl; |
792 | QTime t; | 888 | QTime t; |
793 | t.start(); | 889 | t.start(); |
794 | OSQLResultItem::ValueList list = res.results(); | 890 | OSQLResultItem::ValueList list = res.results(); |
795 | OSQLResultItem::ValueList::Iterator it; | 891 | OSQLResultItem::ValueList::Iterator it; |
796 | QArray<int> ints(list.count() ); | 892 | UIDArray ints(list.count() ); |
797 | odebug << " count = " << list.count() << "" << oendl; | 893 | odebug << " count = " << list.count() << "" << oendl; |
798 | 894 | ||
799 | int i = 0; | 895 | int i = 0; |
800 | for (it = list.begin(); it != list.end(); ++it ) { | 896 | for (it = list.begin(); it != list.end(); ++it ) { |
801 | ints[i] = (*it).data("uid").toInt(); | 897 | ints[i] = (*it).data("uid").toInt(); |
802 | i++; | 898 | i++; |
@@ -834,19 +930,19 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co | |||
834 | << " ms, mapping: " << t3needed << " ms" << oendl; | 930 | << " ms, mapping: " << t3needed << " ms" << oendl; |
835 | 931 | ||
836 | return nonCustomMap; | 932 | return nonCustomMap; |
837 | } | 933 | } |
838 | 934 | ||
839 | /* Returns contact requested by uid and fills cache with contacts requested by uids in the cachelist */ | 935 | /* Returns contact requested by uid and fills cache with contacts requested by uids in the cachelist */ |
840 | OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const QArray<int>& uidlist )const | 936 | OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const UIDArray& uidlist )const |
841 | { | 937 | { |
842 | // We want to get all contacts with one query. | 938 | // We want to get all contacts with one query. |
843 | // We don't have to add the given uid to the uidlist, it is expected to be there already (see opimrecordlist.h). | 939 | // We don't have to add the given uid to the uidlist, it is expected to be there already (see opimrecordlist.h). |
844 | // All contacts will be stored in the cache, afterwards the contact with the user id "uid" will be returned | 940 | // All contacts will be stored in the cache, afterwards the contact with the user id "uid" will be returned |
845 | // by using the cache.. | 941 | // by using the cache.. |
846 | QArray<int> cachelist = uidlist; | 942 | UIDArray cachelist = uidlist; |
847 | OPimContact retContact; | 943 | OPimContact retContact; |
848 | 944 | ||
849 | odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl; | 945 | odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl; |
850 | 946 | ||
851 | QTime t; | 947 | QTime t; |
852 | t.start(); | 948 | t.start(); |