summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
blob: 175d62abfbc710e7d4ebe72f4dda38a4d3ed867b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
/*
                             This file is part of the Opie Project
                             Copyright (C) Stefan Eilers <eilers.stefan@epost.de>
              =.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
            .=l.
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This program is distributed in the hope that
     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;      Library General Public License for more
++=   -.     .`     .:       details.
 :     =  ...= . :.=-
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           Library General Public License along with
    --        :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.
*/
/*
 * SQL Backend for the OPIE-Contact Database.
 */

#include "ocontactaccessbackend_sql.h"

/* OPIE */
#include <opie2/opimcontact.h>
#include <opie2/opimcontactfields.h>
#include <opie2/opimdateconversion.h>
#include <opie2/osqldriver.h>
#include <opie2/osqlresult.h>
#include <opie2/osqlmanager.h>
#include <opie2/osqlquery.h>
#include <opie2/odebug.h>

#include <qpe/global.h>
#include <qpe/recordfields.h>

/* QT */
#include <qarray.h>
#include <qdatetime.h>
#include <qstringlist.h>


using namespace Opie;
using namespace Opie::DB;


/*
 * Implementation of used query types * CREATE query
 * LOAD query
 * INSERT
 * REMOVE
 * CLEAR
 */
namespace {
    /**
     * CreateQuery for the Todolist Table
     */
    class CreateQuery : public OSQLQuery {
    public:
        CreateQuery();
        ~CreateQuery();
        QString query()const;
    };

    /**
     * Clears (delete) a Table
     */
    class ClearQuery : public OSQLQuery {
    public:
        ClearQuery();
        ~ClearQuery();
        QString query()const;

    };


    /**
     * LoadQuery
     * this one queries for all uids
     */
    class LoadQuery : public OSQLQuery {
    public:
        LoadQuery();
        ~LoadQuery();
        QString query()const;
    };

    /**
     * inserts/adds a OPimContact to the table
     */
    class InsertQuery : public OSQLQuery {
    public:
        InsertQuery(const OPimContact& );
        ~InsertQuery();
        QString query()const;
    private:
        OPimContact m_contact;
    };


    /**
     * removes one from the table
     */
    class RemoveQuery : public OSQLQuery {
    public:
        RemoveQuery(int uid );
        ~RemoveQuery();
        QString query()const;
    private:
        int m_uid;
    };

    /**
     * a find query for noncustom elements
     */
    class FindQuery : public OSQLQuery {
    public:
        FindQuery(int uid);
        FindQuery(const UIDArray& );
        ~FindQuery();
        QString query()const;
    private:
        QString single()const;
        QString multi()const;
        UIDArray m_uids;
        int m_uid;
    };

    /**
     * a find query for custom elements
     */
    class FindCustomQuery : public OSQLQuery {
    public:
        FindCustomQuery(int uid);
        FindCustomQuery(const UIDArray& );
        ~FindCustomQuery();
        QString query()const;
    private:
        QString single()const;
        QString multi()const;
        UIDArray m_uids;
        int m_uid;
    };



    // We using two tables to store the information:
    // 1. addressbook  : It contains General information about the contact (non custom)
    // 2. custom_data  : Not official supported entries
    // All tables are connected by the uid of the contact.
    // Maybe I should add a table for meta-information ?
    CreateQuery::CreateQuery() : OSQLQuery() {}
    CreateQuery::~CreateQuery() {}
    QString CreateQuery::query()const {
        QString qu;

        qu += "create table addressbook( uid PRIMARY KEY ";

        QStringList fieldList = OPimContactFields::untrfields( false );
        for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
            qu += QString( ",\"%1\" VARCHAR(10)" ).arg( *it );
        }
        qu += " );";

        qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );";

        return qu;
    }

    ClearQuery::ClearQuery()
        : OSQLQuery() {}
    ClearQuery::~ClearQuery() {}
    QString ClearQuery::query()const {
        QString qu = "drop table addressbook;";
        qu += "drop table custom_data;";
//      qu += "drop table dates;";
        return qu;
    }


    LoadQuery::LoadQuery() : OSQLQuery() {}
    LoadQuery::~LoadQuery() {}
    QString LoadQuery::query()const {
        QString qu;
        qu += "select uid from addressbook";

        return qu;
    }


    InsertQuery::InsertQuery( const OPimContact& contact )
        : OSQLQuery(), m_contact( contact ) {
    }

    InsertQuery::~InsertQuery() {
    }

    /*
     * converts from a OPimContact to a query
     */
    QString InsertQuery::query()const{

        QString qu;
        qu  += "insert into addressbook VALUES( " +
            QString::number( m_contact.uid() );

        // Get all information out of the contact-class
        // Remember: The category is stored in contactMap, too !
        QMap<int, QString> contactMap = m_contact.toMap();

        QStringList fieldList = OPimContactFields::untrfields( false );
        QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
        for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
            // Convert Column-String to Id and get value for this id..
            // Hmmm.. Maybe not very cute solution..
            int id = translate[*it];
            switch ( id ){
            case Qtopia::Birthday:
            case Qtopia::Anniversary:{
                QDate day;
                if ( id == Qtopia::Birthday ){
                    day = m_contact.birthday();
                } else {
                    day = m_contact.anniversary();
                }
                // These entries should stored in a special format
                // year-month-day
                if ( day.isValid() ){
                    qu += QString(",\"%1-%2-%3\"")
                        .arg( QString::number( day.year() ).rightJustify( 4, '0' ) )
                        .arg( QString::number( day.month() ).rightJustify( 2, '0' ) )
                        .arg( QString::number( day.day() ).rightJustify( 2, '0' ) );
                } else {
                    qu += ",\"\"";
                }
            }
                break;
            default:
                qu += QString( ",\"%1\"" ).arg( contactMap[id] );
            }
        }
        qu += " );";


        // Now add custom data..
        int id = 0;
        id = 0;
        QMap<QString, QString> customMap = m_contact.toExtraMap();
        for( QMap<QString, QString>::Iterator it = customMap.begin();
             it != customMap.end(); ++it ){
            qu  += "insert into custom_data VALUES("
                +  QString::number( m_contact.uid() )
                + ","
                +  QString::number( id++ )
                + ",'"
                + it.key()
                + "',"
                + "0" // Priority for future enhancements
                + ",'"
                + it.data()
                + "');";
        }
        // qu  += "commit;";
        odebug << "add " << qu << "" << oendl;
        return qu;
    }


    RemoveQuery::RemoveQuery(int uid )
        : OSQLQuery(), m_uid( uid ) {}

    RemoveQuery::~RemoveQuery() {}

    QString RemoveQuery::query()const {
        QString qu = "DELETE from addressbook where uid = "
            + QString::number(m_uid) + ";";
        qu += "DELETE from custom_data where uid = "
            + QString::number(m_uid) + ";";
        return qu;
    }


    FindQuery::FindQuery(int uid)
        : OSQLQuery(), m_uid( uid ) {
    }
    FindQuery::FindQuery(const UIDArray& ints)
        : OSQLQuery(), m_uids( ints ){
    }
    FindQuery::~FindQuery() {
    }
    QString FindQuery::query()const{
           if ( m_uids.count() == 0 )
                   return single();
           else
                   return multi();
    }



    QString FindQuery::multi()const {
	    QString qu = "select * from addressbook where";
	    for (uint i = 0; i < m_uids.count(); i++ ) {
		    qu += " uid = " + QString::number( m_uids[i] ) + " OR";
	    }
	    qu.remove( qu.length()-2, 2 ); // Hmmmm..

	    odebug << "find query: " << qu << "" << oendl;
	    return qu;
    }

    QString FindQuery::single()const{
	    QString qu = "select *";
	    qu += " from addressbook where uid = " + QString::number(m_uid);

	    // owarn << "find query: " << qu << "" << oendl;
	    return qu;
    }


    FindCustomQuery::FindCustomQuery(int uid)
        : OSQLQuery(), m_uid( uid ) {
    }
    FindCustomQuery::FindCustomQuery(const UIDArray& ints)
        : OSQLQuery(), m_uids( ints ){
    }
    FindCustomQuery::~FindCustomQuery() {
    }
    QString FindCustomQuery::query()const{
//      if ( m_uids.count() == 0 )
            return single();
    }
    QString FindCustomQuery::single()const{
        QString qu = "select uid, type, value from custom_data where uid = ";
        qu += QString::number(m_uid);
        return qu;
    }

};


/* --------------------------------------------------------------------------- */

namespace Opie {

OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* appname */,
                               const QString& filename ):
    OPimContactAccessBackend(), m_changed(false), m_driver( NULL )
{
    odebug << "C'tor OPimContactAccessBackend_SQL starts" << oendl;
    QTime t;
    t.start();

    /* Expecting to access the default filename if nothing else is set */
    if ( filename.isEmpty() ){
        m_fileName = Global::applicationFileName( "addressbook","addressbook.db" );
    } else
        m_fileName = filename;

    // Get the standart sql-driver from the OSQLManager..
    OSQLManager man;
    m_driver = man.standard();
    m_driver->setUrl( m_fileName );

    load();

    odebug << "C'tor OPimContactAccessBackend_SQL ends: " << t.elapsed() << " ms" << oendl;
}

OPimContactAccessBackend_SQL::~OPimContactAccessBackend_SQL ()
{
    if( m_driver )
        delete m_driver;
}

bool OPimContactAccessBackend_SQL::load ()
{
    if (!m_driver->open() )
        return false;

    // Don't expect that the database exists.
    // It is save here to create the table, even if it
    // do exist. ( Is that correct for all databases ?? )
    CreateQuery creat;
    OSQLResult res = m_driver->query( &creat );

    update();

    return true;

}

bool OPimContactAccessBackend_SQL::reload()
{
    return load();
}

bool OPimContactAccessBackend_SQL::save()
{
    return m_driver->close();  // Shouldn't m_driver->sync be better than close ? (eilers)
}


void OPimContactAccessBackend_SQL::clear ()
{
    ClearQuery cle;
    OSQLResult res = m_driver->query( &cle );

    reload();
}

bool OPimContactAccessBackend_SQL::wasChangedExternally()
{
    return false;
}

UIDArray OPimContactAccessBackend_SQL::allRecords() const
{

    // FIXME: Think about cute handling of changed tables..
    // Thus, we don't have to call update here...
    if ( m_changed )
        ((OPimContactAccessBackend_SQL*)this)->update();

    return m_uids;
}

bool OPimContactAccessBackend_SQL::add ( const OPimContact &newcontact )
{
    odebug << "add in contact SQL-Backend" << oendl;
    InsertQuery ins( newcontact );
    OSQLResult res = m_driver->query( &ins );

    if ( res.state() == OSQLResult::Failure )
        return false;

    int c = m_uids.count();
    m_uids.resize( c+1 );
    m_uids[c] = newcontact.uid();

    return true;
}


bool OPimContactAccessBackend_SQL::remove ( int uid )
{
    RemoveQuery rem( uid );
    OSQLResult res = m_driver->query(&rem );

    if ( res.state() == OSQLResult::Failure )
        return false;

    m_changed = true;

    return true;
}

bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact )
{
    if ( !remove( contact.uid() ) )
        return false;

    return add( contact );
}


OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
{
    odebug << "OPimContactAccessBackend_SQL::find(" << uid << ")" << oendl;
    QTime t;
    t.start();

    OPimContact retContact( requestNonCustom( uid ) );
    retContact.setExtraMap( requestCustom( uid ) );

    odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl;
    return retContact;
}

OPimContact OPimContactAccessBackend_SQL::find( int uid, const UIDArray& queryUids, uint current, Frontend::CacheDirection direction ) const
{
    odebug << "OPimContactAccessBackend_SQL::find( ..multi.. )" << oendl;
    odebug << "searching for " << uid << "" << oendl;

    QTime t;
    t.start();

    uint numReadAhead = readAhead();
    QArray<int> searchList( numReadAhead );

    uint size =0;

    // Build an array with all elements which should be requested and cached
    // We will just request "numReadAhead" elements, starting from "current" position in
    // the list of many uids !
    switch( direction ) {
        /* forward */
    case Frontend::Forward:
        for ( uint i = current; i < queryUids.count() && size < numReadAhead; i++ ) {
            searchList[size] = queryUids[i];
            size++;
        }
        break;
        /* reverse */
    case Frontend::Reverse:
        for ( uint i = current; i != 0 && size <  numReadAhead; i-- ) {
            searchList[size] = queryUids[i];
            size++;
        }
        break;
    }

    //Shrink to real size..
    searchList.resize( size );

    OPimContact retContact( requestContactsAndCache( uid, searchList ) );

    odebug << "OPimContactAccessBackend_SQL::find( ..multi.. ) needed: " << t.elapsed() << " ms" << oendl;
    return retContact;
}


UIDArray OPimContactAccessBackend_SQL::queryByExample ( const UIDArray& uidlist, const OPimContact &query, int settings, 
							   const QDateTime& qd ) const
{
    QString searchQuery = "";
    QString datediff_query = "";
    QString uid_query = "";

    // Just add uid's selection if we really try to search in a subset of all uids! Otherwise this would
    // just take time and memory!
    if ( uidlist.count() != m_uids.count() ) {
	    uid_query += " (";

	    for ( uint i = 0; i < uidlist.count(); i++ ) {
		    uid_query += " uid = " + QString::number( uidlist[i] ) + " OR";
	    }
	    uid_query.remove( uid_query.length()-2, 2 ); // Hmmmm..
	    uid_query += " ) AND ";
    }


    QDate startDate;

    if ( qd.isValid() )
        startDate = qd.date();
    else
        startDate = QDate::currentDate();


    QMap<int, QString> queryFields = query.toMap();
    QStringList fieldList = OPimContactFields::untrfields( false );
    QMap<QString, int> translate = OPimContactFields::untrFieldsToId();

    // Convert every filled field to a SQL-Query
//  bool isAnyFieldSelected = false;
    for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){

        int id = translate[*it];
        QString queryStr = queryFields[id];
        QDate* endDate = 0l;

        if ( !queryStr.isEmpty() ){
            // If something is alredy stored in the query, add an "AND"
            // to the end of the string to prepare for the next ..
            if ( !searchQuery.isEmpty() )
                searchQuery += " AND";

//          isAnyFieldSelected = true;
            switch( id ){
            case Qtopia::Birthday:
                endDate = new QDate( query.birthday() );
                // Fall through !
            case Qtopia::Anniversary:
                if ( endDate == 0l )
                    endDate = new QDate( query.anniversary() );

                if ( settings & OPimContactAccess::DateDiff ) {
		    // To handle datediffs correctly, we need to remove the year information from
		    // the birthday and anniversary. 
		    // To do this efficiently, we will create a temporary table which contains the
		    // information we need and do the query on it. 
		    // This table is just visible for this process and will be removed
		    // automatically after using.
		    datediff_query = "CREATE TEMP TABLE bs ( uid, \"Birthday\", \"Anniversary\" );";
		    datediff_query += "INSERT INTO bs SELECT uid,substr(\"Birthday\", 6, 10),substr(\"Anniversary\", 6, 10) FROM addressbook WHERE ( \"Birthday\" != '' OR \"Anniversary\" != '' );";
		    datediff_query += QString( "SELECT uid FROM bs WHERE " ) + uid_query;
                    datediff_query += QString( " (\"%1\" <= '%2-%3\' AND \"%4\" >= '%5-%6')" )
                        .arg( *it )
			    //.arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) )
                        .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) )
                        .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) )
                        .arg( *it )
			    //.arg( QString::number( startDate.year() ).rightJustify( 4, '0' ) )
                        .arg( QString::number( startDate.month() ).rightJustify( 2, '0' ) )
                        .arg( QString::number( startDate.day() ).rightJustify( 2, '0' ) ) ;
                }

                if ( settings & OPimContactAccess::DateYear ){
                    searchQuery += QString( " (\"%1\" LIKE '%2-%')" )
                        .arg( *it )
                        .arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) );
                }

                if ( settings & OPimContactAccess::DateMonth ){
                    if ( settings & OPimContactAccess::DateYear )
                        searchQuery += " AND";

                    searchQuery += QString( " (\"%1\" LIKE '%-%2-%')" )
                        .arg( *it )
                        .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) );
                }

                if ( settings & OPimContactAccess::DateDay ){
                    if ( ( settings & OPimContactAccess::DateYear )
                         || ( settings & OPimContactAccess::DateMonth ) )
                        searchQuery += " AND";

                    searchQuery += QString( " (\"%1\" LIKE '%-%-%2')" )
                        .arg( *it )
                        .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) );
                }

                break;
            default:
                // Switching between case sensitive and insensitive...
                // LIKE is not case sensitive, GLOB is case sensitive
                // Do exist a better solution to switch this ?
                if ( settings & OPimContactAccess::IgnoreCase )
                    searchQuery += " (\"" + *it + "\"" + " LIKE " + "'"
                        + queryStr.replace(QRegExp("\\*"),"%") + "'" + ")";
                else
                    searchQuery += " (\"" + *it + "\"" + " GLOB " + "'"
                        + queryStr + "'" + ")";

            }
        }

        delete endDate;

	// The following if line is a replacement for 
	// if ( searchQuery.endsWith( "AND" ) )
	if ( searchQuery.findRev( "AND" ) == ( searchQuery.length() - 3 ) ){
		odebug << "remove AND" << oendl;
		searchQuery.remove( searchQuery.length()-3, 3 ); // Hmmmm..
	}

    }

    // Now compose the complete query
    QString qu = "SELECT uid FROM addressbook WHERE " + uid_query; 

    if ( !datediff_query.isEmpty() && !searchQuery.isEmpty() ){
	    // If we use DateDiff, we have to intersect two queries.
	    qu = datediff_query + QString( " INTERSECT " ) + qu + searchQuery;
    } else if ( datediff_query.isEmpty() && !searchQuery.isEmpty() ){
	    qu += searchQuery;
    } else if ( !datediff_query.isEmpty() && searchQuery.isEmpty() ){
	    qu = datediff_query;
    } else if ( datediff_query.isEmpty() && searchQuery.isEmpty() ){
	    UIDArray empty;
	    return empty;
    } 

    odebug << "queryByExample query: " << qu << "" << oendl;

    // Execute query and return the received uid's
    OSQLRawQuery raw( qu );
    OSQLResult res = m_driver->query( &raw );
    if ( res.state() != OSQLResult::Success ){
        UIDArray empty;
        return empty;
    }

    UIDArray list = extractUids( res );

    // Remove temp table if created
    if ( !datediff_query.isEmpty( ) ){
	    qu = "DROP TABLE bs";
	    OSQLRawQuery raw( qu );
	    OSQLResult res = m_driver->query( &raw );
    }

    return list;
}

UIDArray OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const
{
#if 0
    QArray<int> nix(0);
    return nix;

#else
    QString qu = "SELECT uid FROM addressbook WHERE (";
    QString searchlist;

    QStringList fieldList = OPimContactFields::untrfields( false );
    // QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
    for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
        if ( !searchlist.isEmpty() )
            searchlist += " OR ";
        searchlist += " rlike(\""+ r.pattern() + "\",\"" + *it + "\") ";
    }

    qu = qu + searchlist + ")";

    odebug << "query: " << qu << "" << oendl;

    OSQLRawQuery raw( qu );
    OSQLResult res = m_driver->query( &raw );

    return extractUids( res );


#endif
}

const uint OPimContactAccessBackend_SQL::querySettings() const
{
    return OPimContactAccess::IgnoreCase
        | OPimContactAccess::WildCards
        | OPimContactAccess::DateDiff
        | OPimContactAccess::DateYear
        | OPimContactAccess::DateMonth
        | OPimContactAccess::DateDay
        ;
}

bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const
{
    /* OPimContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay
     * may be added with any of the other settings. IgnoreCase should never used alone.
     * Wildcards, RegExp, ExactMatch should never used at the same time...
     */

    // Step 1: Check whether the given settings are supported by this backend
    if ( ( querySettings & (
                OPimContactAccess::IgnoreCase
                | OPimContactAccess::WildCards
                | OPimContactAccess::DateDiff
                | OPimContactAccess::DateYear
                | OPimContactAccess::DateMonth
                | OPimContactAccess::DateDay
//              | OPimContactAccess::RegExp
//              | OPimContactAccess::ExactMatch
                   ) ) != querySettings )
        return false;

    // Step 2: Check whether the given combinations are ok..

    // IngoreCase alone is invalid
    if ( querySettings == OPimContactAccess::IgnoreCase )
        return false;

    // WildCards, RegExp and ExactMatch should never used at the same time
    switch ( querySettings & ~( OPimContactAccess::IgnoreCase
                    | OPimContactAccess::DateDiff
                    | OPimContactAccess::DateYear
                    | OPimContactAccess::DateMonth
                    | OPimContactAccess::DateDay
                    )
         ){
    case OPimContactAccess::RegExp:
        return ( true );
    case OPimContactAccess::WildCards:
        return ( true );
    case OPimContactAccess::ExactMatch:
        return ( true );
    case 0: // one of the upper removed bits were set..
        return ( true );
    default:
        return ( false );
    }

}

UIDArray OPimContactAccessBackend_SQL::sorted( const UIDArray& ar, bool asc, int sortOrder,
                                  int filter, const QArray<int>& categories )const 
{
    QTime t;
    t.start();

    QString query = "SELECT uid FROM addressbook";

    query += " WHERE (";
    for ( uint i = 0; i < ar.count(); i++ ) {
	    query += " uid = " + QString::number( ar[i] ) + " OR";
    }
    query.remove( query.length()-2, 2 ); // Hmmmm..
    query += ")";


    if ( filter != OPimBase::FilterOff ){
	    if ( filter & OPimContactAccess::DoNotShowWithCategory ){
		    query += " AND ( \"Categories\" == '' )";
	    } else if ( filter & OPimBase::FilterCategory ){
		    query += " AND (";
		    for ( uint i = 0; i < categories.count(); i++ ){
			    query += "\"Categories\" LIKE";
			    query += QString( " '%" ) + QString::number( categories[i] ) + "%' OR";
		    }
		    query.remove( query.length()-2, 2 ); // Hmmmm..
		    query += ")";
	    }

	    if ( filter & OPimContactAccess::DoNotShowWithoutChildren ){
		    query += " AND ( \"Children\" != '' )";
	    }

	    if ( filter & OPimContactAccess::DoNotShowWithoutAnniversary ){
		    query += " AND ( \"Anniversary\" != '' )";
	    }

	    if ( filter & OPimContactAccess::DoNotShowWithoutBirthday ){
		    query += " AND ( \"Birthday\" != '' )";
	    }

	    if ( filter & OPimContactAccess::DoNotShowWithoutHomeAddress ){
		    // Expect that no Street means no Address, too! (eilers)
		    query += " AND ( \"Home Street\" != '' )";
	    }

	    if ( filter & OPimContactAccess::DoNotShowWithoutBusinessAddress ){
		    // Expect that no Street means no Address, too! (eilers)
		    query += " AND ( \"Business Street\" != '' )";
	    } 

    }
   
    query += " ORDER BY";
    
    switch ( sortOrder ) {
    case OPimContactAccess::SortSummary:
	    query += " \"Notes\"";
	    break;
    case OPimContactAccess::SortByCategory:
	    query += " \"Categories\"";
	    break;
    case OPimContactAccess::SortByDate:
	    query += " \"\"";
	    break;
    case OPimContactAccess::SortTitle:
	    query += " \"Name Title\"";
	    break;
    case OPimContactAccess::SortFirstName:
	    query += " \"First Name\"";
	    break;
    case OPimContactAccess::SortMiddleName:
	    query += " \"Middle Name\"";
	    break;
    case OPimContactAccess::SortLastName:
	    query += " \"Last Name\"";
	    break;
   case OPimContactAccess::SortFileAsName:
	    query += " \"File As\"";
	    break;
    case OPimContactAccess::SortSuffix:
	    query += " \"Suffix\"";
	    break;
    case OPimContactAccess::SortEmail:
	    query += " \"Default Email\"";
	    break;
    case OPimContactAccess::SortNickname:
	    query += " \"Nickname\"";
	    break;
    case OPimContactAccess::SortAnniversary:
	    query += " \"Anniversary\"";
	    break;
    case OPimContactAccess::SortBirthday:
	    query += " \"Birthday\"";
	    break;
    case OPimContactAccess::SortGender:
	    query += " \"Gender\"";
	    break;
    default:
	   query += " \"Last Name\"";
    }

    if ( !asc )
        query += " DESC";


    odebug << "sorted query is: " << query << "" << oendl;

    OSQLRawQuery raw( query );
    OSQLResult res = m_driver->query( &raw );
    if ( res.state() != OSQLResult::Success ){
        UIDArray empty;
        return empty;
    }

    UIDArray list = extractUids( res );

    odebug << "sorted needed " << t.elapsed() << " ms!" << oendl;
    return list;
}


void OPimContactAccessBackend_SQL::update()
{
    odebug << "Update starts" << oendl;
    QTime t;
    t.start();

    // Now load the database set and extract the uid's
    // which will be held locally

    LoadQuery lo;
    OSQLResult res = m_driver->query(&lo);
    if ( res.state() != OSQLResult::Success )
        return;

    m_uids = extractUids( res );

    m_changed = false;

    odebug << "Update ends " << t.elapsed() << " ms" << oendl;
}

UIDArray OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const
{
    odebug << "extractUids" << oendl;
    QTime t;
    t.start();
    OSQLResultItem::ValueList list = res.results();
    OSQLResultItem::ValueList::Iterator it;
    UIDArray ints(list.count() );
    odebug << " count = " << list.count() << "" << oendl;

    int i = 0;
    for (it = list.begin(); it != list.end(); ++it ) {
        ints[i] =  (*it).data("uid").toInt();
        i++;
    }
    odebug << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl;

    return ints;

}

QMap<int, QString>  OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const
{
    QTime t;
    t.start();

    int t2needed = 0;
    int t3needed = 0;
    QTime t2;
    t2.start();
    FindQuery query( uid );
    OSQLResult res_noncustom = m_driver->query( &query );
    t2needed = t2.elapsed();

    OSQLResultItem resItem = res_noncustom.first();

    QMap<int, QString> nonCustomMap;
    QTime t3;
    t3.start();
    nonCustomMap = fillNonCustomMap( resItem );
    t3needed = t3.elapsed();


    // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
    odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
           << " ms, mapping: " << t3needed << " ms" << oendl;

    return nonCustomMap;
}

/* Returns contact requested by uid and fills cache with contacts requested by uids in the cachelist */
OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const UIDArray& uidlist )const
{
	// We want to get all contacts with one query.
	// We don't have to add the given uid to the uidlist, it is expected to be there already (see opimrecordlist.h).
	// All contacts will be stored in the cache, afterwards the contact with the user id "uid" will be returned
	// by using the cache..
	UIDArray cachelist = uidlist;
	OPimContact retContact;

	odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl;

	QTime t;
	t.start();

	int t2needed = 0;
	int t3needed = 0;
	QTime t2;
	t2.start();
	FindQuery query( cachelist );
	OSQLResult res_noncustom = m_driver->query( &query );
	t2needed = t2.elapsed();

	QMap<int, QString> nonCustomMap;
	QTime t3;
	t3.start();
	OSQLResultItem resItem = res_noncustom.first();
	do {
		OPimContact contact( fillNonCustomMap( resItem ) );
		contact.setExtraMap( requestCustom( contact.uid() ) );
		odebug << "Caching uid: " << contact.uid() << oendl;
		cache( contact );
		if ( contact.uid() == uid )
			retContact = contact;
		resItem = res_noncustom.next();
	} while ( ! res_noncustom.atEnd() ); //atEnd() is true if we are past(!) the list !!
	t3needed = t3.elapsed();


	// odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl;
	odebug << "RequestContactsAndCache needed: insg.:" << t.elapsed() << " ms, query: " << t2needed
	       << " ms, mapping: " << t3needed << " ms" << oendl;

	return retContact;
}

QMap<int, QString> OPimContactAccessBackend_SQL::fillNonCustomMap( const OSQLResultItem& resultItem ) const
{
    QMap<int, QString> nonCustomMap;

    // Now loop through all columns
    QStringList fieldList = OPimContactFields::untrfields( false );
    QMap<QString, int> translate = OPimContactFields::untrFieldsToId();
    for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
        // Get data for the selected column and store it with the
        // corresponding id into the map..

        int id =  translate[*it];
        QString value = resultItem.data( (*it) );

        // odebug << "Reading " << (*it) << "... found: " << value << "" << oendl;

        switch( id ){
        case Qtopia::Birthday:
        case Qtopia::Anniversary:{
            // Birthday and Anniversary are encoded special ( yyyy-mm-dd )
            QStringList list = QStringList::split( '-', value );
            QStringList::Iterator lit = list.begin();
            int year  = (*lit).toInt();
            int month = (*(++lit)).toInt();
            int day   = (*(++lit)).toInt();
            if ( ( day != 0 ) && ( month != 0 ) && ( year != 0 ) ){
                 QDate date( year, month, day );
                 nonCustomMap.insert( id, OPimDateConversion::dateToString( date ) );
            }
        }
            break;
        case Qtopia::AddressCategory:
            odebug << "Category is: " << value << "" << oendl;
        default:
            nonCustomMap.insert( id, value );
        }
    }

    nonCustomMap.insert( Qtopia::AddressUid, resultItem.data( "uid" ) );

    return nonCustomMap;
}


QMap<QString, QString>  OPimContactAccessBackend_SQL::requestCustom( int uid ) const
{
    QTime t;
    t.start();

    QMap<QString, QString> customMap;

    FindCustomQuery query( uid );
    OSQLResult res_custom = m_driver->query( &query );

    if ( res_custom.state() == OSQLResult::Failure ) {
        owarn << "OSQLResult::Failure in find query !!" << oendl;
        QMap<QString, QString> empty;
        return empty;
    }

    OSQLResultItem::ValueList list = res_custom.results();
    OSQLResultItem::ValueList::Iterator it = list.begin();
    for ( ; it != list.end(); ++it ) {
        customMap.insert( (*it).data( "type" ), (*it).data( "value" ) );
    }

    odebug << "RequestCustom needed: " << t.elapsed() << " ms" << oendl;
    return customMap;
}

}