summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/otodoaccesssql.cpp
Unidiff
Diffstat (limited to 'libopie2/opiepim/backend/otodoaccesssql.cpp') (more/less context) (show whitespace changes)
-rw-r--r--libopie2/opiepim/backend/otodoaccesssql.cpp48
1 files changed, 26 insertions, 22 deletions
diff --git a/libopie2/opiepim/backend/otodoaccesssql.cpp b/libopie2/opiepim/backend/otodoaccesssql.cpp
index 132b5a6..6f65c48 100644
--- a/libopie2/opiepim/backend/otodoaccesssql.cpp
+++ b/libopie2/opiepim/backend/otodoaccesssql.cpp
@@ -19,39 +19,43 @@
19 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU 19 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU
20..}^=.= = ; Library General Public License for more 20..}^=.= = ; Library General Public License for more
21++= -. .` .: details. 21++= -. .` .: details.
22 : = ...= . :.=- 22 : = ...= . :.=-
23 -. .:....=;==+<; You should have received a copy of the GNU 23 -. .:....=;==+<; You should have received a copy of the GNU
24 -_. . . )=. = Library General Public License along with 24 -_. . . )=. = Library General Public License along with
25 -- :-=` this library; see the file COPYING.LIB. 25 -- :-=` this library; see the file COPYING.LIB.
26 If not, write to the Free Software Foundation, 26 If not, write to the Free Software Foundation,
27 Inc., 59 Temple Place - Suite 330, 27 Inc., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. 28 Boston, MA 02111-1307, USA.
29*/ 29*/
30 30
31#include <qdatetime.h> 31/* OPIE */
32#include <qmap.h>
33#include <qstring.h>
34
35#include <qpe/global.h>
36
37#include <opie2/osqldriver.h> 32#include <opie2/osqldriver.h>
38#include <opie2/osqlresult.h> 33#include <opie2/osqlresult.h>
39#include <opie2/osqlmanager.h> 34#include <opie2/osqlmanager.h>
40#include <opie2/osqlquery.h> 35#include <opie2/osqlquery.h>
41 36
42#include <opie2/otodoaccesssql.h> 37#include <opie2/otodoaccesssql.h>
43#include <opie2/opimstate.h> 38#include <opie2/opimstate.h>
44#include <opie2/opimnotifymanager.h> 39#include <opie2/opimnotifymanager.h>
45#include <opie2/opimrecurrence.h> 40#include <opie2/opimrecurrence.h>
41#include <opie2/odebug.h>
42
43#include <qpe/global.h>
44
45/* QT */
46#include <qdatetime.h>
47#include <qmap.h>
48#include <qstring.h>
49
46 50
47using namespace Opie::DB; 51using namespace Opie::DB;
48 52
49using namespace Opie; 53using namespace Opie;
50/* 54/*
51 * first some query 55 * first some query
52 * CREATE query 56 * CREATE query
53 * LOAD query 57 * LOAD query
54 * INSERT 58 * INSERT
55 * REMOVE 59 * REMOVE
56 * CLEAR 60 * CLEAR
57 */ 61 */
@@ -282,25 +286,25 @@ namespace {
282 + "," 286 + ","
283 + QString::number( id++ ) 287 + QString::number( id++ )
284 + ",'" 288 + ",'"
285 + it.key() 289 + it.key()
286 + "'," 290 + "',"
287 + "0" // Priority for future enhancements 291 + "0" // Priority for future enhancements
288 + ",'" 292 + ",'"
289 + it.data() 293 + it.data()
290 + "');"; 294 + "');";
291 } 295 }
292 296
293 297
294 qDebug("add %s", qu.latin1() ); 298 odebug << "add " << qu << "" << oendl;
295 return qu; 299 return qu;
296 } 300 }
297 301
298 RemoveQuery::RemoveQuery(int uid ) 302 RemoveQuery::RemoveQuery(int uid )
299 : OSQLQuery(), m_uid( uid ) {} 303 : OSQLQuery(), m_uid( uid ) {}
300 RemoveQuery::~RemoveQuery() {} 304 RemoveQuery::~RemoveQuery() {}
301 QString RemoveQuery::query()const { 305 QString RemoveQuery::query()const {
302 QString qu = "DELETE FROM todolist WHERE uid = " + QString::number(m_uid) + " ;"; 306 QString qu = "DELETE FROM todolist WHERE uid = " + QString::number(m_uid) + " ;";
303 qu += "DELETE FROM custom_data WHERE uid = " + QString::number(m_uid); 307 qu += "DELETE FROM custom_data WHERE uid = " + QString::number(m_uid);
304 return qu; 308 return qu;
305 } 309 }
306 310
@@ -447,35 +451,35 @@ QArray<int> OPimTodoAccessBackendSQL::allRecords()const {
447QArray<int> OPimTodoAccessBackendSQL::queryByExample( const OPimTodo& , int, const QDateTime& ){ 451QArray<int> OPimTodoAccessBackendSQL::queryByExample( const OPimTodo& , int, const QDateTime& ){
448 QArray<int> ints(0); 452 QArray<int> ints(0);
449 return ints; 453 return ints;
450} 454}
451OPimTodo OPimTodoAccessBackendSQL::find(int uid ) const{ 455OPimTodo OPimTodoAccessBackendSQL::find(int uid ) const{
452 FindQuery query( uid ); 456 FindQuery query( uid );
453 return todo( m_driver->query(&query) ); 457 return todo( m_driver->query(&query) );
454 458
455} 459}
456OPimTodo OPimTodoAccessBackendSQL::find( int uid, const QArray<int>& ints, 460OPimTodo OPimTodoAccessBackendSQL::find( int uid, const QArray<int>& ints,
457 uint cur, Frontend::CacheDirection dir ) const{ 461 uint cur, Frontend::CacheDirection dir ) const{
458 uint CACHE = readAhead(); 462 uint CACHE = readAhead();
459 qDebug("searching for %d", uid ); 463 odebug << "searching for " << uid << "" << oendl;
460 QArray<int> search( CACHE ); 464 QArray<int> search( CACHE );
461 uint size =0; 465 uint size =0;
462 OPimTodo to; 466 OPimTodo to;
463 467
464 // we try to cache CACHE items 468 // we try to cache CACHE items
465 switch( dir ) { 469 switch( dir ) {
466 /* forward */ 470 /* forward */
467 case 0: // FIXME: Not a good style to use magic numbers here (eilers) 471 case 0: // FIXME: Not a good style to use magic numbers here (eilers)
468 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) { 472 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) {
469 qDebug("size %d %d", size, ints[i] ); 473 odebug << "size " << size << " " << ints[i] << "" << oendl;
470 search[size] = ints[i]; 474 search[size] = ints[i];
471 size++; 475 size++;
472 } 476 }
473 break; 477 break;
474 /* reverse */ 478 /* reverse */
475 case 1: // FIXME: Not a good style to use magic numbers here (eilers) 479 case 1: // FIXME: Not a good style to use magic numbers here (eilers)
476 for (uint i = cur; i != 0 && size < CACHE; i-- ) { 480 for (uint i = cur; i != 0 && size < CACHE; i-- ) {
477 search[size] = ints[i]; 481 search[size] = ints[i];
478 size++; 482 size++;
479 } 483 }
480 break; 484 break;
481 } 485 }
@@ -532,25 +536,25 @@ QArray<int> OPimTodoAccessBackendSQL::overDue() {
532} 536}
533QArray<int> OPimTodoAccessBackendSQL::effectiveToDos( const QDate& s, 537QArray<int> OPimTodoAccessBackendSQL::effectiveToDos( const QDate& s,
534 const QDate& t, 538 const QDate& t,
535 bool u) { 539 bool u) {
536 EffQuery ef(s, t, u ); 540 EffQuery ef(s, t, u );
537 return uids (m_driver->query(&ef) ); 541 return uids (m_driver->query(&ef) );
538} 542}
539/* 543/*
540 * 544 *
541 */ 545 */
542QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder, 546QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder,
543 int sortFilter, int cat ) { 547 int sortFilter, int cat ) {
544 qDebug("sorted %d, %d", asc, sortOrder ); 548 odebug << "sorted " << asc << ", " << sortOrder << "" << oendl;
545 QString query; 549 QString query;
546 query = "select uid from todolist WHERE "; 550 query = "select uid from todolist WHERE ";
547 551
548 /* 552 /*
549 * Sort Filter stuff 553 * Sort Filter stuff
550 * not that straight forward 554 * not that straight forward
551 * FIXME: Replace magic numbers 555 * FIXME: Replace magic numbers
552 * 556 *
553 */ 557 */
554 /* Category */ 558 /* Category */
555 if ( sortFilter & 1 ) { 559 if ( sortFilter & 1 ) {
556 QString str; 560 QString str;
@@ -591,74 +595,74 @@ QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder,
591 case 1: 595 case 1:
592 query += "priority"; 596 query += "priority";
593 break; 597 break;
594 case 2: 598 case 2:
595 query += "summary"; 599 query += "summary";
596 break; 600 break;
597 case 3: 601 case 3:
598 query += "DueDate"; 602 query += "DueDate";
599 break; 603 break;
600 } 604 }
601 605
602 if ( !asc ) { 606 if ( !asc ) {
603 qDebug("not ascending!"); 607 odebug << "not ascending!" << oendl;
604 query += " DESC"; 608 query += " DESC";
605 } 609 }
606 610
607 qDebug( query ); 611 odebug << query << oendl;
608 OSQLRawQuery raw(query ); 612 OSQLRawQuery raw(query );
609 return uids( m_driver->query(&raw) ); 613 return uids( m_driver->query(&raw) );
610} 614}
611bool OPimTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{ 615bool OPimTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{
612 if ( str == "0-0-0" ) 616 if ( str == "0-0-0" )
613 return false; 617 return false;
614 else{ 618 else{
615 int day, year, month; 619 int day, year, month;
616 QStringList list = QStringList::split("-", str ); 620 QStringList list = QStringList::split("-", str );
617 year = list[0].toInt(); 621 year = list[0].toInt();
618 month = list[1].toInt(); 622 month = list[1].toInt();
619 day = list[2].toInt(); 623 day = list[2].toInt();
620 da.setYMD( year, month, day ); 624 da.setYMD( year, month, day );
621 return true; 625 return true;
622 } 626 }
623} 627}
624OPimTodo OPimTodoAccessBackendSQL::todo( const OSQLResult& res ) const{ 628OPimTodo OPimTodoAccessBackendSQL::todo( const OSQLResult& res ) const{
625 if ( res.state() == OSQLResult::Failure ) { 629 if ( res.state() == OSQLResult::Failure ) {
626 OPimTodo to; 630 OPimTodo to;
627 return to; 631 return to;
628 } 632 }
629 633
630 OSQLResultItem::ValueList list = res.results(); 634 OSQLResultItem::ValueList list = res.results();
631 OSQLResultItem::ValueList::Iterator it = list.begin(); 635 OSQLResultItem::ValueList::Iterator it = list.begin();
632 qDebug("todo1"); 636 odebug << "todo1" << oendl;
633 OPimTodo to = todo( (*it) ); 637 OPimTodo to = todo( (*it) );
634 cache( to ); 638 cache( to );
635 ++it; 639 ++it;
636 640
637 for ( ; it != list.end(); ++it ) { 641 for ( ; it != list.end(); ++it ) {
638 qDebug("caching"); 642 odebug << "caching" << oendl;
639 cache( todo( (*it) ) ); 643 cache( todo( (*it) ) );
640 } 644 }
641 return to; 645 return to;
642} 646}
643OPimTodo OPimTodoAccessBackendSQL::todo( OSQLResultItem& item )const { 647OPimTodo OPimTodoAccessBackendSQL::todo( OSQLResultItem& item )const {
644 qDebug("todo(ResultItem)"); 648 odebug << "todo(ResultItem)" << oendl;
645 649
646 // Request information from addressbook table and create the OPimTodo-object. 650 // Request information from addressbook table and create the OPimTodo-object.
647 651
648 bool hasDueDate = false; QDate dueDate = QDate::currentDate(); 652 bool hasDueDate = false; QDate dueDate = QDate::currentDate();
649 hasDueDate = date( dueDate, item.data("DueDate") ); 653 hasDueDate = date( dueDate, item.data("DueDate") );
650 QStringList cats = QStringList::split(";", item.data("categories") ); 654 QStringList cats = QStringList::split(";", item.data("categories") );
651 655
652 qDebug("Item is completed: %d", item.data("completed").toInt() ); 656 odebug << "Item is completed: " << item.data("completed").toInt() << "" << oendl;
653 657
654 OPimTodo to( (bool)item.data("completed").toInt(), item.data("priority").toInt(), 658 OPimTodo to( (bool)item.data("completed").toInt(), item.data("priority").toInt(),
655 cats, item.data("summary"), item.data("description"), 659 cats, item.data("summary"), item.data("description"),
656 item.data("progress").toUShort(), hasDueDate, dueDate, 660 item.data("progress").toUShort(), hasDueDate, dueDate,
657 item.data("uid").toInt() ); 661 item.data("uid").toInt() );
658 662
659 bool isOk; 663 bool isOk;
660 int prioInt = QString( item.data("priority") ).toInt( &isOk ); 664 int prioInt = QString( item.data("priority") ).toInt( &isOk );
661 if ( isOk ) 665 if ( isOk )
662 to.setPriority( prioInt ); 666 to.setPriority( prioInt );
663 667
664 bool hasStartDate = false; QDate startDate = QDate::currentDate(); 668 bool hasStartDate = false; QDate startDate = QDate::currentDate();
@@ -741,25 +745,25 @@ void OPimTodoAccessBackendSQL::update()const {
741 LoadQuery lo; 745 LoadQuery lo;
742 OSQLResult res = m_driver->query(&lo); 746 OSQLResult res = m_driver->query(&lo);
743 if ( res.state() != OSQLResult::Success ) 747 if ( res.state() != OSQLResult::Success )
744 return; 748 return;
745 749
746 ((OPimTodoAccessBackendSQL*)this)->m_uids = uids( res ); 750 ((OPimTodoAccessBackendSQL*)this)->m_uids = uids( res );
747} 751}
748QArray<int> OPimTodoAccessBackendSQL::uids( const OSQLResult& res) const{ 752QArray<int> OPimTodoAccessBackendSQL::uids( const OSQLResult& res) const{
749 753
750 OSQLResultItem::ValueList list = res.results(); 754 OSQLResultItem::ValueList list = res.results();
751 OSQLResultItem::ValueList::Iterator it; 755 OSQLResultItem::ValueList::Iterator it;
752 QArray<int> ints(list.count() ); 756 QArray<int> ints(list.count() );
753 qDebug(" count = %d", list.count() ); 757 odebug << " count = " << list.count() << "" << oendl;
754 758
755 int i = 0; 759 int i = 0;
756 for (it = list.begin(); it != list.end(); ++it ) { 760 for (it = list.begin(); it != list.end(); ++it ) {
757 ints[i] = (*it).data("uid").toInt(); 761 ints[i] = (*it).data("uid").toInt();
758 i++; 762 i++;
759 } 763 }
760 return ints; 764 return ints;
761} 765}
762 766
763QArray<int> OPimTodoAccessBackendSQL::matchRegexp( const QRegExp &r ) const 767QArray<int> OPimTodoAccessBackendSQL::matchRegexp( const QRegExp &r ) const
764{ 768{
765 769
@@ -767,25 +771,25 @@ QArray<int> OPimTodoAccessBackendSQL::matchRegexp( const QRegExp &r ) const
767 QArray<int> empty; 771 QArray<int> empty;
768 return empty; 772 return empty;
769 773
770#else 774#else
771 QString qu = "SELECT uid FROM todolist WHERE ("; 775 QString qu = "SELECT uid FROM todolist WHERE (";
772 776
773 // Do it make sense to search other fields, too ? 777 // Do it make sense to search other fields, too ?
774 qu += " rlike(\""+ r.pattern() + "\",\"description\") OR"; 778 qu += " rlike(\""+ r.pattern() + "\",\"description\") OR";
775 qu += " rlike(\""+ r.pattern() + "\",\"summary\")"; 779 qu += " rlike(\""+ r.pattern() + "\",\"summary\")";
776 780
777 qu += ")"; 781 qu += ")";
778 782
779 qDebug( "query: %s", qu.latin1() ); 783 odebug << "query: " << qu << "" << oendl;
780 784
781 OSQLRawQuery raw( qu ); 785 OSQLRawQuery raw( qu );
782 OSQLResult res = m_driver->query( &raw ); 786 OSQLResult res = m_driver->query( &raw );
783 787
784 return uids( res ); 788 return uids( res );
785 789
786 790
787#endif 791#endif
788 792
789} 793}
790QBitArray OPimTodoAccessBackendSQL::supports()const { 794QBitArray OPimTodoAccessBackendSQL::supports()const {
791 795
@@ -807,25 +811,25 @@ QBitArray OPimTodoAccessBackendSQL::sup() const{
807 811
808void OPimTodoAccessBackendSQL::removeAllCompleted(){ 812void OPimTodoAccessBackendSQL::removeAllCompleted(){
809 // First we need the uids from all entries which are 813 // First we need the uids from all entries which are
810 // completed. Then, we just have to remove them... 814 // completed. Then, we just have to remove them...
811 815
812 QString qu = "SELECT uid FROM todolist WHERE completed = 1"; 816 QString qu = "SELECT uid FROM todolist WHERE completed = 1";
813 817
814 OSQLRawQuery raw( qu ); 818 OSQLRawQuery raw( qu );
815 OSQLResult res = m_driver->query( &raw ); 819 OSQLResult res = m_driver->query( &raw );
816 820
817 QArray<int> completed_uids = uids( res ); 821 QArray<int> completed_uids = uids( res );
818 822
819 qDebug( "Number of completed: %d", completed_uids.size() ); 823 odebug << "Number of completed: " << completed_uids.size() << "" << oendl;
820 824
821 if ( completed_uids.size() == 0 ) 825 if ( completed_uids.size() == 0 )
822 return; 826 return;
823 827
824 qu = "DELETE FROM todolist WHERE ("; 828 qu = "DELETE FROM todolist WHERE (";
825 QString query; 829 QString query;
826 830
827 for ( int i = 0; i < completed_uids.size(); i++ ){ 831 for ( int i = 0; i < completed_uids.size(); i++ ){
828 if ( !query.isEmpty() ) 832 if ( !query.isEmpty() )
829 query += " OR "; 833 query += " OR ";
830 query += QString( "uid = %1" ).arg( completed_uids[i] ); 834 query += QString( "uid = %1" ).arg( completed_uids[i] );
831 } 835 }
@@ -833,43 +837,43 @@ void OPimTodoAccessBackendSQL::removeAllCompleted(){
833 837
834 // Put remove of custom entries in this query to speed up.. 838 // Put remove of custom entries in this query to speed up..
835 qu += "DELETE FORM custom_data WHERE ("; 839 qu += "DELETE FORM custom_data WHERE (";
836 query = ""; 840 query = "";
837 841
838 for ( int i = 0; i < completed_uids.size(); i++ ){ 842 for ( int i = 0; i < completed_uids.size(); i++ ){
839 if ( !query.isEmpty() ) 843 if ( !query.isEmpty() )
840 query += " OR "; 844 query += " OR ";
841 query += QString( "uid = %1" ).arg( completed_uids[i] ); 845 query += QString( "uid = %1" ).arg( completed_uids[i] );
842 } 846 }
843 qu += query + " );"; 847 qu += query + " );";
844 848
845 qDebug( "query: %s", qu.latin1() ); 849 odebug << "query: " << qu << "" << oendl;
846 850
847 OSQLRawQuery raw2( qu ); 851 OSQLRawQuery raw2( qu );
848 res = m_driver->query( &raw2 ); 852 res = m_driver->query( &raw2 );
849 if ( res.state() == OSQLResult::Failure ) { 853 if ( res.state() == OSQLResult::Failure ) {
850 qWarning("OPimTodoAccessBackendSQL::removeAllCompleted():Failure in query: %s", qu.latin1() ); 854 owarn << "OPimTodoAccessBackendSQL::removeAllCompleted():Failure in query: " << qu << "" << oendl;
851 } 855 }
852} 856}
853 857
854 858
855QMap<QString, QString> OPimTodoAccessBackendSQL::requestCustom( int uid ) const 859QMap<QString, QString> OPimTodoAccessBackendSQL::requestCustom( int uid ) const
856{ 860{
857 QMap<QString, QString> customMap; 861 QMap<QString, QString> customMap;
858 862
859 FindCustomQuery query( uid ); 863 FindCustomQuery query( uid );
860 OSQLResult res_custom = m_driver->query( &query ); 864 OSQLResult res_custom = m_driver->query( &query );
861 865
862 if ( res_custom.state() == OSQLResult::Failure ) { 866 if ( res_custom.state() == OSQLResult::Failure ) {
863 qWarning("OSQLResult::Failure in find query !!"); 867 owarn << "OSQLResult::Failure in find query !!" << oendl;
864 QMap<QString, QString> empty; 868 QMap<QString, QString> empty;
865 return empty; 869 return empty;
866 } 870 }
867 871
868 OSQLResultItem::ValueList list = res_custom.results(); 872 OSQLResultItem::ValueList list = res_custom.results();
869 OSQLResultItem::ValueList::Iterator it = list.begin(); 873 OSQLResultItem::ValueList::Iterator it = list.begin();
870 for ( ; it != list.end(); ++it ) { 874 for ( ; it != list.end(); ++it ) {
871 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); 875 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) );
872 } 876 }
873 877
874 return customMap; 878 return customMap;
875} 879}