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.cpp5
1 files changed, 4 insertions, 1 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
index abfd944..221e977 100644
--- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
+++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
@@ -653,298 +653,301 @@ QArray<int> OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const
653 return nix; 653 return nix;
654 654
655#else 655#else
656 QString qu = "SELECT uid FROM addressbook WHERE ("; 656 QString qu = "SELECT uid FROM addressbook WHERE (";
657 QString searchlist; 657 QString searchlist;
658 658
659 QStringList fieldList = OPimContactFields::untrfields( false ); 659 QStringList fieldList = OPimContactFields::untrfields( false );
660 // QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); 660 // QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
661 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 661 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
662 if ( !searchlist.isEmpty() ) 662 if ( !searchlist.isEmpty() )
663 searchlist += " OR "; 663 searchlist += " OR ";
664 searchlist += " rlike(\""+ r.pattern() + "\",\"" + *it + "\") "; 664 searchlist += " rlike(\""+ r.pattern() + "\",\"" + *it + "\") ";
665 } 665 }
666 666
667 qu = qu + searchlist + ")"; 667 qu = qu + searchlist + ")";
668 668
669 odebug << "query: " << qu << "" << oendl; 669 odebug << "query: " << qu << "" << oendl;
670 670
671 OSQLRawQuery raw( qu ); 671 OSQLRawQuery raw( qu );
672 OSQLResult res = m_driver->query( &raw ); 672 OSQLResult res = m_driver->query( &raw );
673 673
674 return extractUids( res ); 674 return extractUids( res );
675 675
676 676
677#endif 677#endif
678} 678}
679 679
680const uint OPimContactAccessBackend_SQL::querySettings() 680const uint OPimContactAccessBackend_SQL::querySettings()
681{ 681{
682 return OPimContactAccess::IgnoreCase 682 return OPimContactAccess::IgnoreCase
683 | OPimContactAccess::WildCards 683 | OPimContactAccess::WildCards
684 | OPimContactAccess::DateDiff 684 | OPimContactAccess::DateDiff
685 | OPimContactAccess::DateYear 685 | OPimContactAccess::DateYear
686 | OPimContactAccess::DateMonth 686 | OPimContactAccess::DateMonth
687 | OPimContactAccess::DateDay 687 | OPimContactAccess::DateDay
688 ; 688 ;
689} 689}
690 690
691bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const 691bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const
692{ 692{
693 /* OPimContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay 693 /* OPimContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay
694 * may be added with any of the other settings. IgnoreCase should never used alone. 694 * may be added with any of the other settings. IgnoreCase should never used alone.
695 * Wildcards, RegExp, ExactMatch should never used at the same time... 695 * Wildcards, RegExp, ExactMatch should never used at the same time...
696 */ 696 */
697 697
698 // Step 1: Check whether the given settings are supported by this backend 698 // Step 1: Check whether the given settings are supported by this backend
699 if ( ( querySettings & ( 699 if ( ( querySettings & (
700 OPimContactAccess::IgnoreCase 700 OPimContactAccess::IgnoreCase
701 | OPimContactAccess::WildCards 701 | OPimContactAccess::WildCards
702 | OPimContactAccess::DateDiff 702 | OPimContactAccess::DateDiff
703 | OPimContactAccess::DateYear 703 | OPimContactAccess::DateYear
704 | OPimContactAccess::DateMonth 704 | OPimContactAccess::DateMonth
705 | OPimContactAccess::DateDay 705 | OPimContactAccess::DateDay
706// | OPimContactAccess::RegExp 706// | OPimContactAccess::RegExp
707// | OPimContactAccess::ExactMatch 707// | OPimContactAccess::ExactMatch
708 ) ) != querySettings ) 708 ) ) != querySettings )
709 return false; 709 return false;
710 710
711 // Step 2: Check whether the given combinations are ok.. 711 // Step 2: Check whether the given combinations are ok..
712 712
713 // IngoreCase alone is invalid 713 // IngoreCase alone is invalid
714 if ( querySettings == OPimContactAccess::IgnoreCase ) 714 if ( querySettings == OPimContactAccess::IgnoreCase )
715 return false; 715 return false;
716 716
717 // WildCards, RegExp and ExactMatch should never used at the same time 717 // WildCards, RegExp and ExactMatch should never used at the same time
718 switch ( querySettings & ~( OPimContactAccess::IgnoreCase 718 switch ( querySettings & ~( OPimContactAccess::IgnoreCase
719 | OPimContactAccess::DateDiff 719 | OPimContactAccess::DateDiff
720 | OPimContactAccess::DateYear 720 | OPimContactAccess::DateYear
721 | OPimContactAccess::DateMonth 721 | OPimContactAccess::DateMonth
722 | OPimContactAccess::DateDay 722 | OPimContactAccess::DateDay
723 ) 723 )
724 ){ 724 ){
725 case OPimContactAccess::RegExp: 725 case OPimContactAccess::RegExp:
726 return ( true ); 726 return ( true );
727 case OPimContactAccess::WildCards: 727 case OPimContactAccess::WildCards:
728 return ( true ); 728 return ( true );
729 case OPimContactAccess::ExactMatch: 729 case OPimContactAccess::ExactMatch:
730 return ( true ); 730 return ( true );
731 case 0: // one of the upper removed bits were set.. 731 case 0: // one of the upper removed bits were set..
732 return ( true ); 732 return ( true );
733 default: 733 default:
734 return ( false ); 734 return ( false );
735 } 735 }
736 736
737} 737}
738 738
739QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int ) 739QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int )
740{ 740{
741 QTime t; 741 QTime t;
742 t.start(); 742 t.start();
743 743
744 QString query = "SELECT uid FROM addressbook "; 744 QString query = "SELECT uid FROM addressbook ";
745 query += "ORDER BY \"Last Name\" "; 745 query += "ORDER BY \"Last Name\" ";
746 746
747 if ( !asc ) 747 if ( !asc )
748 query += "DESC"; 748 query += "DESC";
749 749
750 // odebug << "sorted query is: " << query << "" << oendl; 750 // odebug << "sorted query is: " << query << "" << oendl;
751 751
752 OSQLRawQuery raw( query ); 752 OSQLRawQuery raw( query );
753 OSQLResult res = m_driver->query( &raw ); 753 OSQLResult res = m_driver->query( &raw );
754 if ( res.state() != OSQLResult::Success ){ 754 if ( res.state() != OSQLResult::Success ){
755 QArray<int> empty; 755 QArray<int> empty;
756 return empty; 756 return empty;
757 } 757 }
758 758
759 QArray<int> list = extractUids( res ); 759 QArray<int> list = extractUids( res );
760 760
761 odebug << "sorted needed " << t.elapsed() << " ms!" << oendl; 761 odebug << "sorted needed " << t.elapsed() << " ms!" << oendl;
762 return list; 762 return list;
763} 763}
764 764
765 765
766void OPimContactAccessBackend_SQL::update() 766void OPimContactAccessBackend_SQL::update()
767{ 767{
768 odebug << "Update starts" << oendl; 768 odebug << "Update starts" << oendl;
769 QTime t; 769 QTime t;
770 t.start(); 770 t.start();
771 771
772 // Now load the database set and extract the uid's 772 // Now load the database set and extract the uid's
773 // which will be held locally 773 // which will be held locally
774 774
775 LoadQuery lo; 775 LoadQuery lo;
776 OSQLResult res = m_driver->query(&lo); 776 OSQLResult res = m_driver->query(&lo);
777 if ( res.state() != OSQLResult::Success ) 777 if ( res.state() != OSQLResult::Success )
778 return; 778 return;
779 779
780 m_uids = extractUids( res ); 780 m_uids = extractUids( res );
781 781
782 m_changed = false; 782 m_changed = false;
783 783
784 odebug << "Update ends " << t.elapsed() << " ms" << oendl; 784 odebug << "Update ends " << t.elapsed() << " ms" << oendl;
785} 785}
786 786
787QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const 787QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const
788{ 788{
789 odebug << "extractUids" << oendl; 789 odebug << "extractUids" << oendl;
790 QTime t; 790 QTime t;
791 t.start(); 791 t.start();
792 OSQLResultItem::ValueList list = res.results(); 792 OSQLResultItem::ValueList list = res.results();
793 OSQLResultItem::ValueList::Iterator it; 793 OSQLResultItem::ValueList::Iterator it;
794 QArray<int> ints(list.count() ); 794 QArray<int> ints(list.count() );
795 odebug << " count = " << list.count() << "" << oendl; 795 odebug << " count = " << list.count() << "" << oendl;
796 796
797 int i = 0; 797 int i = 0;
798 for (it = list.begin(); it != list.end(); ++it ) { 798 for (it = list.begin(); it != list.end(); ++it ) {
799 ints[i] = (*it).data("uid").toInt(); 799 ints[i] = (*it).data("uid").toInt();
800 i++; 800 i++;
801 } 801 }
802 odebug << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl; 802 odebug << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl;
803 803
804 return ints; 804 return ints;
805 805
806} 806}
807 807
808QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const 808QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const
809{ 809{
810 QTime t; 810 QTime t;
811 t.start(); 811 t.start();
812 812
813 int t2needed = 0; 813 int t2needed = 0;
814 int t3needed = 0; 814 int t3needed = 0;
815 QTime t2; 815 QTime t2;
816 t2.start(); 816 t2.start();
817 FindQuery query( uid ); 817 FindQuery query( uid );
818 OSQLResult res_noncustom = m_driver->query( &query ); 818 OSQLResult res_noncustom = m_driver->query( &query );
819 t2needed = t2.elapsed(); 819 t2needed = t2.elapsed();
820 820
821 OSQLResultItem resItem = res_noncustom.first(); 821 OSQLResultItem resItem = res_noncustom.first();
822 822
823 QMap<int, QString> nonCustomMap; 823 QMap<int, QString> nonCustomMap;
824 QTime t3; 824 QTime t3;
825 t3.start(); 825 t3.start();
826 nonCustomMap = fillNonCustomMap( resItem ); 826 nonCustomMap = fillNonCustomMap( resItem );
827 t3needed = t3.elapsed(); 827 t3needed = t3.elapsed();
828 828
829 829
830 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl; 830 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
831 odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed 831 odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
832 << " ms, mapping: " << t3needed << " ms" << oendl; 832 << " ms, mapping: " << t3needed << " ms" << oendl;
833 833
834 return nonCustomMap; 834 return nonCustomMap;
835} 835}
836 836
837/* Returns contact requested by uid and fills cache with contacts requested by uids in the cachelist */ 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 838OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const QArray<int>& uidlist )const
839{ 839{
840 // We want to get all contacts with one query. 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). 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 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.. 843 // by using the cache..
844 QArray<int> cachelist = uidlist; 844 QArray<int> cachelist = uidlist;
845 OPimContact retContact;
845 846
846 odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl; 847 odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl;
847 848
848 QTime t; 849 QTime t;
849 t.start(); 850 t.start();
850 851
851 int t2needed = 0; 852 int t2needed = 0;
852 int t3needed = 0; 853 int t3needed = 0;
853 QTime t2; 854 QTime t2;
854 t2.start(); 855 t2.start();
855 FindQuery query( cachelist ); 856 FindQuery query( cachelist );
856 OSQLResult res_noncustom = m_driver->query( &query ); 857 OSQLResult res_noncustom = m_driver->query( &query );
857 t2needed = t2.elapsed(); 858 t2needed = t2.elapsed();
858 859
859 QMap<int, QString> nonCustomMap; 860 QMap<int, QString> nonCustomMap;
860 QTime t3; 861 QTime t3;
861 t3.start(); 862 t3.start();
862 OSQLResultItem resItem = res_noncustom.first(); 863 OSQLResultItem resItem = res_noncustom.first();
863 do { 864 do {
864 OPimContact contact( fillNonCustomMap( resItem ) ); 865 OPimContact contact( fillNonCustomMap( resItem ) );
865 contact.setExtraMap( requestCustom( contact.uid() ) ); 866 contact.setExtraMap( requestCustom( contact.uid() ) );
866 odebug << "Caching uid: " << contact.uid() << oendl; 867 odebug << "Caching uid: " << contact.uid() << oendl;
867 cache( contact ); 868 cache( contact );
869 if ( contact.uid() == uid )
870 retContact = contact;
868 resItem = res_noncustom.next(); 871 resItem = res_noncustom.next();
869 } while ( ! res_noncustom.atEnd() ); //atEnd() is true if we are past(!) the list !! 872 } while ( ! res_noncustom.atEnd() ); //atEnd() is true if we are past(!) the list !!
870 t3needed = t3.elapsed(); 873 t3needed = t3.elapsed();
871 874
872 875
873 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl; 876 // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
874 odebug << "RequestContactsAndCache needed: insg.:" << t.elapsed() << " ms, query: " << t2needed 877 odebug << "RequestContactsAndCache needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
875 << " ms, mapping: " << t3needed << " ms" << oendl; 878 << " ms, mapping: " << t3needed << " ms" << oendl;
876 879
877 return cacheFind( uid ); 880 return retContact;
878} 881}
879 882
880QMap<int, QString> OPimContactAccessBackend_SQL::fillNonCustomMap( const OSQLResultItem& resultItem ) const 883QMap<int, QString> OPimContactAccessBackend_SQL::fillNonCustomMap( const OSQLResultItem& resultItem ) const
881{ 884{
882 QMap<int, QString> nonCustomMap; 885 QMap<int, QString> nonCustomMap;
883 886
884 // Now loop through all columns 887 // Now loop through all columns
885 QStringList fieldList = OPimContactFields::untrfields( false ); 888 QStringList fieldList = OPimContactFields::untrfields( false );
886 QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); 889 QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
887 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 890 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
888 // Get data for the selected column and store it with the 891 // Get data for the selected column and store it with the
889 // corresponding id into the map.. 892 // corresponding id into the map..
890 893
891 int id = translate[*it]; 894 int id = translate[*it];
892 QString value = resultItem.data( (*it) ); 895 QString value = resultItem.data( (*it) );
893 896
894 // odebug << "Reading " << (*it) << "... found: " << value << "" << oendl; 897 // odebug << "Reading " << (*it) << "... found: " << value << "" << oendl;
895 898
896 switch( id ){ 899 switch( id ){
897 case Qtopia::Birthday: 900 case Qtopia::Birthday:
898 case Qtopia::Anniversary:{ 901 case Qtopia::Anniversary:{
899 // Birthday and Anniversary are encoded special ( yyyy-mm-dd ) 902 // Birthday and Anniversary are encoded special ( yyyy-mm-dd )
900 QStringList list = QStringList::split( '-', value ); 903 QStringList list = QStringList::split( '-', value );
901 QStringList::Iterator lit = list.begin(); 904 QStringList::Iterator lit = list.begin();
902 int year = (*lit).toInt(); 905 int year = (*lit).toInt();
903 int month = (*(++lit)).toInt(); 906 int month = (*(++lit)).toInt();
904 int day = (*(++lit)).toInt(); 907 int day = (*(++lit)).toInt();
905 if ( ( day != 0 ) && ( month != 0 ) && ( year != 0 ) ){ 908 if ( ( day != 0 ) && ( month != 0 ) && ( year != 0 ) ){
906 QDate date( year, month, day ); 909 QDate date( year, month, day );
907 nonCustomMap.insert( id, OPimDateConversion::dateToString( date ) ); 910 nonCustomMap.insert( id, OPimDateConversion::dateToString( date ) );
908 } 911 }
909 } 912 }
910 break; 913 break;
911 case Qtopia::AddressCategory: 914 case Qtopia::AddressCategory:
912 odebug << "Category is: " << value << "" << oendl; 915 odebug << "Category is: " << value << "" << oendl;
913 default: 916 default:
914 nonCustomMap.insert( id, value ); 917 nonCustomMap.insert( id, value );
915 } 918 }
916 } 919 }
917 920
918 nonCustomMap.insert( Qtopia::AddressUid, resultItem.data( "uid" ) ); 921 nonCustomMap.insert( Qtopia::AddressUid, resultItem.data( "uid" ) );
919 922
920 return nonCustomMap; 923 return nonCustomMap;
921} 924}
922 925
923 926
924QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const 927QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const
925{ 928{
926 QTime t; 929 QTime t;
927 t.start(); 930 t.start();
928 931
929 QMap<QString, QString> customMap; 932 QMap<QString, QString> customMap;
930 933
931 FindCustomQuery query( uid ); 934 FindCustomQuery query( uid );
932 OSQLResult res_custom = m_driver->query( &query ); 935 OSQLResult res_custom = m_driver->query( &query );
933 936
934 if ( res_custom.state() == OSQLResult::Failure ) { 937 if ( res_custom.state() == OSQLResult::Failure ) {
935 owarn << "OSQLResult::Failure in find query !!" << oendl; 938 owarn << "OSQLResult::Failure in find query !!" << oendl;
936 QMap<QString, QString> empty; 939 QMap<QString, QString> empty;
937 return empty; 940 return empty;
938 } 941 }
939 942
940 OSQLResultItem::ValueList list = res_custom.results(); 943 OSQLResultItem::ValueList list = res_custom.results();
941 OSQLResultItem::ValueList::Iterator it = list.begin(); 944 OSQLResultItem::ValueList::Iterator it = list.begin();
942 for ( ; it != list.end(); ++it ) { 945 for ( ; it != list.end(); ++it ) {
943 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); 946 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) );
944 } 947 }
945 948
946 odebug << "RequestCustom needed: " << t.elapsed() << " ms" << oendl; 949 odebug << "RequestCustom needed: " << t.elapsed() << " ms" << oendl;
947 return customMap; 950 return customMap;
948} 951}
949 952
950} 953}