summaryrefslogtreecommitdiff
path: root/libopie2
Unidiff
Diffstat (limited to 'libopie2') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp8
1 files changed, 7 insertions, 1 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
index 6aaa14c..f684f8c 100644
--- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
+++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
@@ -587,129 +587,129 @@ UIDArray OPimContactAccessBackend_SQL::queryByExample ( const UIDArray& uidlist,
587 587
588 if ( settings & OPimContactAccess::DateDiff ) { 588 if ( settings & OPimContactAccess::DateDiff ) {
589 // To handle datediffs correctly, we need to remove the year information from 589 // To handle datediffs correctly, we need to remove the year information from
590 // the birthday and anniversary. 590 // the birthday and anniversary.
591 // To do this efficiently, we will create a temporary table which contains the 591 // To do this efficiently, we will create a temporary table which contains the
592 // information we need and do the query on it. 592 // information we need and do the query on it.
593 // This table is just visible for this process and will be removed 593 // This table is just visible for this process and will be removed
594 // automatically after using. 594 // automatically after using.
595 datediff_query = "SELECT uid,substr(\"Birthday\", 6, 10) as \"BirthdayMD\", substr(\"Anniversary\", 6, 10) as \"AnniversaryMD\" FROM addressbook WHERE ( \"Birthday\" != '' OR \"Anniversary\" != '' ) AND "; 595 datediff_query = "SELECT uid,substr(\"Birthday\", 6, 10) as \"BirthdayMD\", substr(\"Anniversary\", 6, 10) as \"AnniversaryMD\" FROM addressbook WHERE ( \"Birthday\" != '' OR \"Anniversary\" != '' ) AND ";
596 datediff_query += QString( " (\"%1MD\" <= '%2-%3\' AND \"%4MD\" >= '%5-%6')" ) 596 datediff_query += QString( " (\"%1MD\" <= '%2-%3\' AND \"%4MD\" >= '%5-%6')" )
597 .arg( *it ) 597 .arg( *it )
598 //.arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) ) 598 //.arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) )
599 .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) ) 599 .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) )
600 .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) ) 600 .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) )
601 .arg( *it ) 601 .arg( *it )
602 //.arg( QString::number( startDate.year() ).rightJustify( 4, '0' ) ) 602 //.arg( QString::number( startDate.year() ).rightJustify( 4, '0' ) )
603 .arg( QString::number( startDate.month() ).rightJustify( 2, '0' ) ) 603 .arg( QString::number( startDate.month() ).rightJustify( 2, '0' ) )
604 .arg( QString::number( startDate.day() ).rightJustify( 2, '0' ) ) ; 604 .arg( QString::number( startDate.day() ).rightJustify( 2, '0' ) ) ;
605 } 605 }
606 606
607 if ( settings & OPimContactAccess::DateYear ){ 607 if ( settings & OPimContactAccess::DateYear ){
608 searchQuery += QString( " (\"%1\" LIKE '%2-%')" ) 608 searchQuery += QString( " (\"%1\" LIKE '%2-%')" )
609 .arg( *it ) 609 .arg( *it )
610 .arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) ); 610 .arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) );
611 } 611 }
612 612
613 if ( settings & OPimContactAccess::DateMonth ){ 613 if ( settings & OPimContactAccess::DateMonth ){
614 if ( settings & OPimContactAccess::DateYear ) 614 if ( settings & OPimContactAccess::DateYear )
615 searchQuery += " AND"; 615 searchQuery += " AND";
616 616
617 searchQuery += QString( " (\"%1\" LIKE '%-%2-%')" ) 617 searchQuery += QString( " (\"%1\" LIKE '%-%2-%')" )
618 .arg( *it ) 618 .arg( *it )
619 .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) ); 619 .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) );
620 } 620 }
621 621
622 if ( settings & OPimContactAccess::DateDay ){ 622 if ( settings & OPimContactAccess::DateDay ){
623 if ( ( settings & OPimContactAccess::DateYear ) 623 if ( ( settings & OPimContactAccess::DateYear )
624 || ( settings & OPimContactAccess::DateMonth ) ) 624 || ( settings & OPimContactAccess::DateMonth ) )
625 searchQuery += " AND"; 625 searchQuery += " AND";
626 626
627 searchQuery += QString( " (\"%1\" LIKE '%-%-%2')" ) 627 searchQuery += QString( " (\"%1\" LIKE '%-%-%2')" )
628 .arg( *it ) 628 .arg( *it )
629 .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) ); 629 .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) );
630 } 630 }
631 631
632 break; 632 break;
633 default: 633 default:
634 // Switching between case sensitive and insensitive... 634 // Switching between case sensitive and insensitive...
635 // LIKE is not case sensitive, GLOB is case sensitive 635 // LIKE is not case sensitive, GLOB is case sensitive
636 // Do exist a better solution to switch this ? 636 // Do exist a better solution to switch this ?
637 if ( settings & OPimContactAccess::IgnoreCase ) 637 if ( settings & OPimContactAccess::IgnoreCase )
638 searchQuery += " (\"" + *it + "\"" + " LIKE " + "'" 638 searchQuery += " (\"" + *it + "\"" + " LIKE " + "'"
639 + queryStr.replace(QRegExp("\\*"),"%") + "'" + ")"; 639 + queryStr.replace(QRegExp("\\*"),"%") + "'" + ")";
640 else 640 else
641 searchQuery += " (\"" + *it + "\"" + " GLOB " + "'" 641 searchQuery += " (\"" + *it + "\"" + " GLOB " + "'"
642 + queryStr + "'" + ")"; 642 + queryStr + "'" + ")";
643 643
644 } 644 }
645 } 645 }
646 646
647 delete endDate; 647 delete endDate;
648 648
649 // The following if line is a replacement for 649 // The following if line is a replacement for
650 // if ( searchQuery.endsWith( "AND" ) ) 650 // if ( searchQuery.endsWith( "AND" ) )
651 if ( searchQuery.findRev( "AND" ) == ( searchQuery.length() - 3 ) ){ 651 if ( searchQuery.findRev( "AND" ) == static_cast<int>( searchQuery.length() - 3 ) ){
652 odebug << "remove AND" << oendl; 652 odebug << "remove AND" << oendl;
653 searchQuery.remove( searchQuery.length()-3, 3 ); // Hmmmm.. 653 searchQuery.remove( searchQuery.length()-3, 3 ); // Hmmmm..
654 } 654 }
655 655
656 } 656 }
657 657
658 // Now compose the complete query 658 // Now compose the complete query
659 QString qu = "SELECT uid FROM addressbook WHERE " + uid_query; 659 QString qu = "SELECT uid FROM addressbook WHERE " + uid_query;
660 660
661 if ( !datediff_query.isEmpty() && !searchQuery.isEmpty() ){ 661 if ( !datediff_query.isEmpty() && !searchQuery.isEmpty() ){
662 // If we use DateDiff, we have to intersect two queries. 662 // If we use DateDiff, we have to intersect two queries.
663 qu = datediff_query + QString( " INTERSECT " ) + qu + searchQuery; 663 qu = datediff_query + QString( " INTERSECT " ) + qu + searchQuery;
664 } else if ( datediff_query.isEmpty() && !searchQuery.isEmpty() ){ 664 } else if ( datediff_query.isEmpty() && !searchQuery.isEmpty() ){
665 qu += searchQuery; 665 qu += searchQuery;
666 } else if ( !datediff_query.isEmpty() && searchQuery.isEmpty() ){ 666 } else if ( !datediff_query.isEmpty() && searchQuery.isEmpty() ){
667 qu = datediff_query; 667 qu = datediff_query;
668 } else if ( datediff_query.isEmpty() && searchQuery.isEmpty() ){ 668 } else if ( datediff_query.isEmpty() && searchQuery.isEmpty() ){
669 UIDArray empty; 669 UIDArray empty;
670 return empty; 670 return empty;
671 } 671 }
672 672
673 odebug << "queryByExample query: " << qu << "" << oendl; 673 odebug << "queryByExample query: " << qu << "" << oendl;
674 674
675 // Execute query and return the received uid's 675 // Execute query and return the received uid's
676 OSQLRawQuery raw( qu ); 676 OSQLRawQuery raw( qu );
677 OSQLResult res = m_driver->query( &raw ); 677 OSQLResult res = m_driver->query( &raw );
678 if ( res.state() != OSQLResult::Success ){ 678 if ( res.state() != OSQLResult::Success ){
679 UIDArray empty; 679 UIDArray empty;
680 return empty; 680 return empty;
681 } 681 }
682 682
683 UIDArray list = extractUids( res ); 683 UIDArray list = extractUids( res );
684 684
685 return list; 685 return list;
686} 686}
687 687
688UIDArray OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const 688UIDArray OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const
689{ 689{
690#if 0 690#if 0
691 QArray<int> nix(0); 691 QArray<int> nix(0);
692 return nix; 692 return nix;
693 693
694#else 694#else
695 QString qu = "SELECT uid FROM addressbook WHERE ("; 695 QString qu = "SELECT uid FROM addressbook WHERE (";
696 QString searchlist; 696 QString searchlist;
697 697
698 QStringList fieldList = OPimContactFields::untrfields( false ); 698 QStringList fieldList = OPimContactFields::untrfields( false );
699 // QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); 699 // QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
700 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ 700 for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
701 if ( !searchlist.isEmpty() ) 701 if ( !searchlist.isEmpty() )
702 searchlist += " OR "; 702 searchlist += " OR ";
703 searchlist += " rlike(\""+ r.pattern() + "\",\"" + *it + "\") "; 703 searchlist += " rlike(\""+ r.pattern() + "\",\"" + *it + "\") ";
704 } 704 }
705 705
706 qu = qu + searchlist + ")"; 706 qu = qu + searchlist + ")";
707 707
708 odebug << "query: " << qu << "" << oendl; 708 odebug << "query: " << qu << "" << oendl;
709 709
710 OSQLRawQuery raw( qu ); 710 OSQLRawQuery raw( qu );
711 OSQLResult res = m_driver->query( &raw ); 711 OSQLResult res = m_driver->query( &raw );
712 712
713 return extractUids( res ); 713 return extractUids( res );
714 714
715 715
@@ -812,128 +812,134 @@ UIDArray OPimContactAccessBackend_SQL::sorted( const UIDArray& ar, bool asc, int
812 query += " AND ( \"Anniversary\" != '' )"; 812 query += " AND ( \"Anniversary\" != '' )";
813 } 813 }
814 814
815 if ( filter & OPimContactAccess::DoNotShowWithoutBirthday ){ 815 if ( filter & OPimContactAccess::DoNotShowWithoutBirthday ){
816 query += " AND ( \"Birthday\" != '' )"; 816 query += " AND ( \"Birthday\" != '' )";
817 } 817 }
818 818
819 if ( filter & OPimContactAccess::DoNotShowWithoutHomeAddress ){ 819 if ( filter & OPimContactAccess::DoNotShowWithoutHomeAddress ){
820 // Expect that no Street means no Address, too! (eilers) 820 // Expect that no Street means no Address, too! (eilers)
821 query += " AND ( \"Home Street\" != '' )"; 821 query += " AND ( \"Home Street\" != '' )";
822 } 822 }
823 823
824 if ( filter & OPimContactAccess::DoNotShowWithoutBusinessAddress ){ 824 if ( filter & OPimContactAccess::DoNotShowWithoutBusinessAddress ){
825 // Expect that no Street means no Address, too! (eilers) 825 // Expect that no Street means no Address, too! (eilers)
826 query += " AND ( \"Business Street\" != '' )"; 826 query += " AND ( \"Business Street\" != '' )";
827 } 827 }
828 828
829 } 829 }
830 830
831 query += " ORDER BY"; 831 query += " ORDER BY";
832 832
833 switch ( sortOrder ) { 833 switch ( sortOrder ) {
834 case OPimContactAccess::SortSummary: 834 case OPimContactAccess::SortSummary:
835 query += " \"Notes\""; 835 query += " \"Notes\"";
836 break; 836 break;
837 case OPimContactAccess::SortByCategory: 837 case OPimContactAccess::SortByCategory:
838 query += " \"Categories\""; 838 query += " \"Categories\"";
839 break; 839 break;
840 case OPimContactAccess::SortByDate: 840 case OPimContactAccess::SortByDate:
841 query += " \"\""; 841 query += " \"\"";
842 break; 842 break;
843 case OPimContactAccess::SortTitle: 843 case OPimContactAccess::SortTitle:
844 query += " \"Name Title\""; 844 query += " \"Name Title\"";
845 break; 845 break;
846 case OPimContactAccess::SortFirstName: 846 case OPimContactAccess::SortFirstName:
847 query += " \"First Name\""; 847 query += " \"First Name\"";
848 break; 848 break;
849 case OPimContactAccess::SortMiddleName: 849 case OPimContactAccess::SortMiddleName:
850 query += " \"Middle Name\""; 850 query += " \"Middle Name\"";
851 break; 851 break;
852 case OPimContactAccess::SortLastName: 852 case OPimContactAccess::SortLastName:
853 query += " \"Last Name\""; 853 query += " \"Last Name\"";
854 break; 854 break;
855 case OPimContactAccess::SortFileAsName: 855 case OPimContactAccess::SortFileAsName:
856 query += " \"File As\""; 856 query += " \"File As\"";
857 break; 857 break;
858 case OPimContactAccess::SortSuffix: 858 case OPimContactAccess::SortSuffix:
859 query += " \"Suffix\""; 859 query += " \"Suffix\"";
860 break; 860 break;
861 case OPimContactAccess::SortEmail: 861 case OPimContactAccess::SortEmail:
862 query += " \"Default Email\""; 862 query += " \"Default Email\"";
863 break; 863 break;
864 case OPimContactAccess::SortNickname: 864 case OPimContactAccess::SortNickname:
865 query += " \"Nickname\""; 865 query += " \"Nickname\"";
866 break; 866 break;
867 case OPimContactAccess::SortAnniversary: 867 case OPimContactAccess::SortAnniversary:
868 query += " \"Anniversary\""; 868 query += " \"Anniversary\"";
869 break; 869 break;
870 case OPimContactAccess::SortBirthday: 870 case OPimContactAccess::SortBirthday:
871 query += " \"Birthday\""; 871 query += " \"Birthday\"";
872 break; 872 break;
873 case OPimContactAccess::SortGender: 873 case OPimContactAccess::SortGender:
874 query += " \"Gender\""; 874 query += " \"Gender\"";
875 break; 875 break;
876 case OPimContactAccess::SortBirthdayWithoutYear:
877 query += " substr(\"Birthday\", 6, 10)";
878 break;
879 case OPimContactAccess::SortAnniversaryWithoutYear:
880 query += " substr(\"Anniversary\", 6, 10)";
881 break;
876 default: 882 default:
877 query += " \"Last Name\""; 883 query += " \"Last Name\"";
878 } 884 }
879 885
880 if ( !asc ) 886 if ( !asc )
881 query += " DESC"; 887 query += " DESC";
882 888
883 889
884 odebug << "sorted query is: " << query << "" << oendl; 890 odebug << "sorted query is: " << query << "" << oendl;
885 891
886 OSQLRawQuery raw( query ); 892 OSQLRawQuery raw( query );
887 OSQLResult res = m_driver->query( &raw ); 893 OSQLResult res = m_driver->query( &raw );
888 if ( res.state() != OSQLResult::Success ){ 894 if ( res.state() != OSQLResult::Success ){
889 UIDArray empty; 895 UIDArray empty;
890 return empty; 896 return empty;
891 } 897 }
892 898
893 UIDArray list = extractUids( res ); 899 UIDArray list = extractUids( res );
894 900
895 odebug << "sorted needed " << t.elapsed() << " ms!" << oendl; 901 odebug << "sorted needed " << t.elapsed() << " ms!" << oendl;
896 return list; 902 return list;
897} 903}
898 904
899 905
900void OPimContactAccessBackend_SQL::update() 906void OPimContactAccessBackend_SQL::update()
901{ 907{
902 odebug << "Update starts" << oendl; 908 odebug << "Update starts" << oendl;
903 QTime t; 909 QTime t;
904 t.start(); 910 t.start();
905 911
906 // Now load the database set and extract the uid's 912 // Now load the database set and extract the uid's
907 // which will be held locally 913 // which will be held locally
908 914
909 LoadQuery lo; 915 LoadQuery lo;
910 OSQLResult res = m_driver->query(&lo); 916 OSQLResult res = m_driver->query(&lo);
911 if ( res.state() != OSQLResult::Success ) 917 if ( res.state() != OSQLResult::Success )
912 return; 918 return;
913 919
914 m_uids = extractUids( res ); 920 m_uids = extractUids( res );
915 921
916 m_changed = false; 922 m_changed = false;
917 923
918 odebug << "Update ends " << t.elapsed() << " ms" << oendl; 924 odebug << "Update ends " << t.elapsed() << " ms" << oendl;
919} 925}
920 926
921UIDArray OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const 927UIDArray OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const
922{ 928{
923 odebug << "extractUids" << oendl; 929 odebug << "extractUids" << oendl;
924 QTime t; 930 QTime t;
925 t.start(); 931 t.start();
926 OSQLResultItem::ValueList list = res.results(); 932 OSQLResultItem::ValueList list = res.results();
927 OSQLResultItem::ValueList::Iterator it; 933 OSQLResultItem::ValueList::Iterator it;
928 UIDArray ints(list.count() ); 934 UIDArray ints(list.count() );
929 odebug << " count = " << list.count() << "" << oendl; 935 odebug << " count = " << list.count() << "" << oendl;
930 936
931 int i = 0; 937 int i = 0;
932 for (it = list.begin(); it != list.end(); ++it ) { 938 for (it = list.begin(); it != list.end(); ++it ) {
933 ints[i] = (*it).data("uid").toInt(); 939 ints[i] = (*it).data("uid").toInt();
934 i++; 940 i++;
935 } 941 }
936 odebug << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl; 942 odebug << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl;
937 943
938 return ints; 944 return ints;
939 945