summaryrefslogtreecommitdiff
authorzecke <zecke>2002-10-19 00:08:35 (UTC)
committer zecke <zecke>2002-10-19 00:08:35 (UTC)
commit549472ef07703ef27dc5f58a37e8f16c714fb2a1 (patch) (unidiff)
tree9742f2545ef3504e61a07cef2996aa29b9db5b90
parent598cfa66b57d61e15bc3c5ea3a9c8e7295864455 (diff)
downloadopie-549472ef07703ef27dc5f58a37e8f16c714fb2a1.zip
opie-549472ef07703ef27dc5f58a37e8f16c714fb2a1.tar.gz
opie-549472ef07703ef27dc5f58a37e8f16c714fb2a1.tar.bz2
Add void setReadAhead(uint) to the templates
OTodoAccessSQL use the read ahead value
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie/pim/opimaccessbackend.h16
-rw-r--r--libopie/pim/opimaccesstemplate.h7
-rw-r--r--libopie/pim/otodoaccesssql.cpp2
-rw-r--r--libopie2/opiepim/backend/opimaccessbackend.h16
-rw-r--r--libopie2/opiepim/backend/otodoaccesssql.cpp2
-rw-r--r--libopie2/opiepim/core/opimaccesstemplate.h7
6 files changed, 44 insertions, 6 deletions
diff --git a/libopie/pim/opimaccessbackend.h b/libopie/pim/opimaccessbackend.h
index 27d3cb8..0bd2723 100644
--- a/libopie/pim/opimaccessbackend.h
+++ b/libopie/pim/opimaccessbackend.h
@@ -1,127 +1,141 @@
1#ifndef OPIE_PIM_ACCESS_BACKEND 1#ifndef OPIE_PIM_ACCESS_BACKEND
2#define OPIE_PIM_ACCESS_BACKEND 2#define OPIE_PIM_ACCESS_BACKEND
3 3
4#include <qarray.h> 4#include <qarray.h>
5 5
6#include <opie/otemplatebase.h> 6#include <opie/otemplatebase.h>
7#include <opie/opimrecord.h> 7#include <opie/opimrecord.h>
8 8
9 9
10/** 10/**
11 * OPimAccessBackend is the base class 11 * OPimAccessBackend is the base class
12 * for all private backends 12 * for all private backends
13 * it operates on OPimRecord as the base class 13 * it operates on OPimRecord as the base class
14 * and it's responsible for fast manipulating 14 * and it's responsible for fast manipulating
15 * the resource the implementation takes care 15 * the resource the implementation takes care
16 * of 16 * of
17 */ 17 */
18template <class T = OPimRecord> 18template <class T = OPimRecord>
19class OPimAccessBackend { 19class OPimAccessBackend {
20public: 20public:
21 typedef OTemplateBase<T> Frontend; 21 typedef OTemplateBase<T> Frontend;
22 OPimAccessBackend(); 22 OPimAccessBackend();
23 virtual ~OPimAccessBackend(); 23 virtual ~OPimAccessBackend();
24 24
25 /** 25 /**
26 * load the resource 26 * load the resource
27 */ 27 */
28 virtual bool load() = 0; 28 virtual bool load() = 0;
29 29
30 /** 30 /**
31 * reload the resource 31 * reload the resource
32 */ 32 */
33 virtual bool reload() = 0; 33 virtual bool reload() = 0;
34 34
35 /** 35 /**
36 * save the resource and 36 * save the resource and
37 * all it's changes 37 * all it's changes
38 */ 38 */
39 virtual bool save() = 0; 39 virtual bool save() = 0;
40 40
41 /** 41 /**
42 * return an array of 42 * return an array of
43 * all available uids 43 * all available uids
44 */ 44 */
45 virtual QArray<int> allRecords()const = 0; 45 virtual QArray<int> allRecords()const = 0;
46 46
47 /** 47 /**
48 * queryByExample for T with the SortOrder 48 * queryByExample for T with the SortOrder
49 * sort 49 * sort
50 */ 50 */
51 virtual QArray<int> queryByExample( const T& t, int sort ) = 0; 51 virtual QArray<int> queryByExample( const T& t, int sort ) = 0;
52 52
53 /** 53 /**
54 * find the OPimRecord with uid @param uid 54 * find the OPimRecord with uid @param uid
55 * returns T and T.isEmpty() if nothing was found 55 * returns T and T.isEmpty() if nothing was found
56 */ 56 */
57 virtual T find(int uid )const = 0; 57 virtual T find(int uid )const = 0;
58 58
59 virtual T find(int uid, const QArray<int>& items, 59 virtual T find(int uid, const QArray<int>& items,
60 uint current, Frontend::CacheDirection )const ; 60 uint current, Frontend::CacheDirection )const ;
61 /** 61 /**
62 * clear the back end 62 * clear the back end
63 */ 63 */
64 virtual void clear() = 0; 64 virtual void clear() = 0;
65 65
66 /** 66 /**
67 * add T 67 * add T
68 */ 68 */
69 virtual bool add( const T& t ) = 0; 69 virtual bool add( const T& t ) = 0;
70 70
71 /** 71 /**
72 * remove 72 * remove
73 */ 73 */
74 virtual bool remove( int uid ) = 0; 74 virtual bool remove( int uid ) = 0;
75 75
76 /** 76 /**
77 * replace a record with T.uid() 77 * replace a record with T.uid()
78 */ 78 */
79 virtual bool replace( const T& t ) = 0; 79 virtual bool replace( const T& t ) = 0;
80 80
81 /* 81 /*
82 * setTheFrontEnd!!! 82 * setTheFrontEnd!!!
83 */ 83 */
84 void setFrontend( Frontend* front ); 84 void setFrontend( Frontend* front );
85 85
86 /**
87 * set the read ahead count
88 */
89 void setReadAhead( uint count );
86protected: 90protected:
87 void cache( const T& t )const; 91 void cache( const T& t )const;
88 92
89 /** 93 /**
90 * use a prime number here! 94 * use a prime number here!
91 */ 95 */
92 void setSaneCacheSize( int ); 96 void setSaneCacheSize( int );
93 97
98 uint readAhead()const;
99
94private: 100private:
95 Frontend* m_front; 101 Frontend* m_front;
102 uint m_read;
96 103
97}; 104};
98 105
99template <class T> 106template <class T>
100OPimAccessBackend<T>::OPimAccessBackend() { 107OPimAccessBackend<T>::OPimAccessBackend() {
101 m_front = 0l; 108 m_front = 0l;
102} 109}
103template <class T> 110template <class T>
104OPimAccessBackend<T>::~OPimAccessBackend() { 111OPimAccessBackend<T>::~OPimAccessBackend() {
105 112
106} 113}
107template <class T> 114template <class T>
108void OPimAccessBackend<T>::setFrontend( Frontend* fr ) { 115void OPimAccessBackend<T>::setFrontend( Frontend* fr ) {
109 m_front = fr; 116 m_front = fr;
110} 117}
111template <class T> 118template <class T>
112void OPimAccessBackend<T>::cache( const T& t )const { 119void OPimAccessBackend<T>::cache( const T& t )const {
113 if (m_front ) 120 if (m_front )
114 m_front->cache( t ); 121 m_front->cache( t );
115} 122}
116template <class T> 123template <class T>
117void OPimAccessBackend<T>::setSaneCacheSize( int size) { 124void OPimAccessBackend<T>::setSaneCacheSize( int size) {
118 if (m_front ) 125 if (m_front )
119 m_front->setSaneCacheSize( size ); 126 m_front->setSaneCacheSize( size );
120} 127}
121template <class T> 128template <class T>
122T OPimAccessBackend<T>::find( int uid, const QArray<int>&, 129T OPimAccessBackend<T>::find( int uid, const QArray<int>&,
123 uint, Frontend::CacheDirection )const { 130 uint, Frontend::CacheDirection )const {
124 return find( uid ); 131 return find( uid );
125} 132}
126 133template <class T>
134void OPimAccessBackend<T>::setReadAhead( uint count ) {
135 m_read = count;
136}
137template <class T>
138uint OPimAccessBackend<T>::readAhead()const {
139 return m_read;
140}
127#endif 141#endif
diff --git a/libopie/pim/opimaccesstemplate.h b/libopie/pim/opimaccesstemplate.h
index 50cb1e4..c5523a8 100644
--- a/libopie/pim/opimaccesstemplate.h
+++ b/libopie/pim/opimaccesstemplate.h
@@ -1,254 +1,259 @@
1#ifndef OPIE_PIM_ACCESS_TEMPLATE_H 1#ifndef OPIE_PIM_ACCESS_TEMPLATE_H
2#define OPIE_PIM_ACCESS_TEMPLATE_H 2#define OPIE_PIM_ACCESS_TEMPLATE_H
3 3
4#include <qarray.h> 4#include <qarray.h>
5 5
6#include <opie/opimrecord.h> 6#include <opie/opimrecord.h>
7#include <opie/opimaccessbackend.h> 7#include <opie/opimaccessbackend.h>
8#include <opie/orecordlist.h> 8#include <opie/orecordlist.h>
9 9
10#include "opimcache.h" 10#include "opimcache.h"
11#include "otemplatebase.h" 11#include "otemplatebase.h"
12 12
13/** 13/**
14 * Thats the frontend to our OPIE PIM 14 * Thats the frontend to our OPIE PIM
15 * Library. Either you want to use it's 15 * Library. Either you want to use it's
16 * interface or you want to implement 16 * interface or you want to implement
17 * your own Access lib 17 * your own Access lib
18 * Just create a OPimRecord and inherit from 18 * Just create a OPimRecord and inherit from
19 * the plugins 19 * the plugins
20 */ 20 */
21 21
22template <class T = OPimRecord > 22template <class T = OPimRecord >
23class OPimAccessTemplate : public OTemplateBase<T> { 23class OPimAccessTemplate : public OTemplateBase<T> {
24public: 24public:
25 typedef ORecordList<T> List; 25 typedef ORecordList<T> List;
26 typedef OPimAccessBackend<T> BackEnd; 26 typedef OPimAccessBackend<T> BackEnd;
27 typedef OPimCache<T> Cache; 27 typedef OPimCache<T> Cache;
28 28
29 /** 29 /**
30 * c'tor BackEnd 30 * c'tor BackEnd
31 */ 31 */
32 OPimAccessTemplate( BackEnd* end); 32 OPimAccessTemplate( BackEnd* end);
33 virtual ~OPimAccessTemplate(); 33 virtual ~OPimAccessTemplate();
34 34
35 /** 35 /**
36 * load from the backend 36 * load from the backend
37 */ 37 */
38 virtual bool load(); 38 virtual bool load();
39 39
40 /** Reload database. 40 /** Reload database.
41 * You should execute this function if the external database 41 * You should execute this function if the external database
42 * was changed. 42 * was changed.
43 * This function will load the external database and afterwards 43 * This function will load the external database and afterwards
44 * rejoin the local changes. Therefore the local database will be set consistent. 44 * rejoin the local changes. Therefore the local database will be set consistent.
45 */ 45 */
46 virtual bool reload(); 46 virtual bool reload();
47 47
48 /** Save contacts database. 48 /** Save contacts database.
49 * Save is more a "commit". After calling this function, all changes are public available. 49 * Save is more a "commit". After calling this function, all changes are public available.
50 * @return true if successful 50 * @return true if successful
51 */ 51 */
52 virtual bool save(); 52 virtual bool save();
53 53
54 /** 54 /**
55 * if the resource was changed externally 55 * if the resource was changed externally
56 * You should use the signal handling instead of polling possible changes ! 56 * You should use the signal handling instead of polling possible changes !
57 * zecke: Do you implement a signal for otodoaccess ? 57 * zecke: Do you implement a signal for otodoaccess ?
58 */ 58 */
59 bool wasChangedExternally()const; 59 bool wasChangedExternally()const;
60 60
61 /** 61 /**
62 * return a List of records 62 * return a List of records
63 * you can iterate over them 63 * you can iterate over them
64 */ 64 */
65 virtual List allRecords()const; 65 virtual List allRecords()const;
66 66
67 /** 67 /**
68 * queryByExample. 68 * queryByExample.
69 * @see otodoaccess, ocontactaccess 69 * @see otodoaccess, ocontactaccess
70 */ 70 */
71 virtual List queryByExample( const T& t, int querySettings ); 71 virtual List queryByExample( const T& t, int querySettings );
72 72
73 /** 73 /**
74 * find the OPimRecord uid 74 * find the OPimRecord uid
75 */ 75 */
76 virtual T find( int uid )const; 76 virtual T find( int uid )const;
77 77
78 /** 78 /**
79 * read ahead cache find method ;) 79 * read ahead cache find method ;)
80 */ 80 */
81 virtual T find( int uid, const QArray<int>&, 81 virtual T find( int uid, const QArray<int>&,
82 uint current, CacheDirection dir = Forward )const; 82 uint current, CacheDirection dir = Forward )const;
83 83
84 /* invalidate cache here */ 84 /* invalidate cache here */
85 /** 85 /**
86 * clears the backend and invalidates the backend 86 * clears the backend and invalidates the backend
87 */ 87 */
88 virtual void clear() ; 88 virtual void clear() ;
89 89
90 /** 90 /**
91 * add T to the backend 91 * add T to the backend
92 * @param t The item to add. 92 * @param t The item to add.
93 * @return <i>true</i> if added successfully. 93 * @return <i>true</i> if added successfully.
94 */ 94 */
95 virtual bool add( const T& t ) ; 95 virtual bool add( const T& t ) ;
96 96
97 /* only the uid matters */ 97 /* only the uid matters */
98 /** 98 /**
99 * remove T from the backend 99 * remove T from the backend
100 * @param t The item to remove 100 * @param t The item to remove
101 * @return <i>true</i> if successful. 101 * @return <i>true</i> if successful.
102 */ 102 */
103 virtual bool remove( const T& t ); 103 virtual bool remove( const T& t );
104 104
105 /** 105 /**
106 * remove the OPimRecord with uid 106 * remove the OPimRecord with uid
107 * @param uid The ID of the item to remove 107 * @param uid The ID of the item to remove
108 * @return <i>true</i> if successful. 108 * @return <i>true</i> if successful.
109 */ 109 */
110 virtual bool remove( int uid ); 110 virtual bool remove( int uid );
111 111
112 /** 112 /**
113 * replace T from backend 113 * replace T from backend
114 * @param t The item to replace 114 * @param t The item to replace
115 * @return <i>true</i> if successful. 115 * @return <i>true</i> if successful.
116 */ 116 */
117 virtual bool replace( const T& t) ; 117 virtual bool replace( const T& t) ;
118 118
119 void setReadAhead( uint count );
119 /** 120 /**
120 * @internal 121 * @internal
121 */ 122 */
122 void cache( const T& )const; 123 void cache( const T& )const;
123 void setSaneCacheSize( int ); 124 void setSaneCacheSize( int );
124protected: 125protected:
125 /** 126 /**
126 * invalidate the cache 127 * invalidate the cache
127 */ 128 */
128 void invalidateCache(); 129 void invalidateCache();
129 130
130 void setBackEnd( BackEnd* end ); 131 void setBackEnd( BackEnd* end );
131 /** 132 /**
132 * returns the backend 133 * returns the backend
133 */ 134 */
134 BackEnd* backEnd(); 135 BackEnd* backEnd();
135 BackEnd* m_backEnd; 136 BackEnd* m_backEnd;
136 Cache m_cache; 137 Cache m_cache;
137 138
138}; 139};
139 140
140template <class T> 141template <class T>
141OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end ) 142OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end )
142 : OTemplateBase<T>(), m_backEnd( end ) 143 : OTemplateBase<T>(), m_backEnd( end )
143{ 144{
144 if (end ) 145 if (end )
145 end->setFrontend( this ); 146 end->setFrontend( this );
146} 147}
147template <class T> 148template <class T>
148OPimAccessTemplate<T>::~OPimAccessTemplate() { 149OPimAccessTemplate<T>::~OPimAccessTemplate() {
149 qWarning("~OPimAccessTemplate<T>"); 150 qWarning("~OPimAccessTemplate<T>");
150 delete m_backEnd; 151 delete m_backEnd;
151} 152}
152template <class T> 153template <class T>
153bool OPimAccessTemplate<T>::load() { 154bool OPimAccessTemplate<T>::load() {
154 invalidateCache(); 155 invalidateCache();
155 return m_backEnd->load(); 156 return m_backEnd->load();
156} 157}
157template <class T> 158template <class T>
158bool OPimAccessTemplate<T>::reload() { 159bool OPimAccessTemplate<T>::reload() {
159 invalidateCache(); // zecke: I think this should be added (se) 160 invalidateCache(); // zecke: I think this should be added (se)
160 return m_backEnd->reload(); 161 return m_backEnd->reload();
161} 162}
162template <class T> 163template <class T>
163bool OPimAccessTemplate<T>::save() { 164bool OPimAccessTemplate<T>::save() {
164 return m_backEnd->save(); 165 return m_backEnd->save();
165} 166}
166template <class T> 167template <class T>
167OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const { 168OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const {
168 QArray<int> ints = m_backEnd->allRecords(); 169 QArray<int> ints = m_backEnd->allRecords();
169 List lis(ints, this ); 170 List lis(ints, this );
170 return lis; 171 return lis;
171} 172}
172template <class T> 173template <class T>
173OPimAccessTemplate<T>::List 174OPimAccessTemplate<T>::List
174OPimAccessTemplate<T>::queryByExample( const T& t, int sortOrder ) { 175OPimAccessTemplate<T>::queryByExample( const T& t, int sortOrder ) {
175 QArray<int> ints = m_backEnd->queryByExample( t, sortOrder ); 176 QArray<int> ints = m_backEnd->queryByExample( t, sortOrder );
176 177
177 List lis(ints, this ); 178 List lis(ints, this );
178 return lis; 179 return lis;
179} 180}
180template <class T> 181template <class T>
181T OPimAccessTemplate<T>::find( int uid ) const{ 182T OPimAccessTemplate<T>::find( int uid ) const{
182 T t = m_backEnd->find( uid ); 183 T t = m_backEnd->find( uid );
183 cache( t ); 184 cache( t );
184 return t; 185 return t;
185} 186}
186template <class T> 187template <class T>
187T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar, 188T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar,
188 uint current, CacheDirection dir )const { 189 uint current, CacheDirection dir )const {
189 /* 190 /*
190 * better do T.isEmpty() 191 * better do T.isEmpty()
191 * after a find this way we would 192 * after a find this way we would
192 * avoid two finds in QCache... 193 * avoid two finds in QCache...
193 */ 194 */
194 // qWarning("find it now %d", uid ); 195 // qWarning("find it now %d", uid );
195 if (m_cache.contains( uid ) ) { 196 if (m_cache.contains( uid ) ) {
196 return m_cache.find( uid ); 197 return m_cache.find( uid );
197 } 198 }
198 199
199 T t = m_backEnd->find( uid, ar, current, dir ); 200 T t = m_backEnd->find( uid, ar, current, dir );
200 cache( t ); 201 cache( t );
201 return t; 202 return t;
202} 203}
203template <class T> 204template <class T>
204void OPimAccessTemplate<T>::clear() { 205void OPimAccessTemplate<T>::clear() {
205 invalidateCache(); 206 invalidateCache();
206 m_backEnd->clear(); 207 m_backEnd->clear();
207} 208}
208template <class T> 209template <class T>
209bool OPimAccessTemplate<T>::add( const T& t ) { 210bool OPimAccessTemplate<T>::add( const T& t ) {
210 cache( t ); 211 cache( t );
211 return m_backEnd->add( t ); 212 return m_backEnd->add( t );
212} 213}
213template <class T> 214template <class T>
214bool OPimAccessTemplate<T>::remove( const T& t ) { 215bool OPimAccessTemplate<T>::remove( const T& t ) {
215 return remove( t.uid() ); 216 return remove( t.uid() );
216} 217}
217template <class T> 218template <class T>
218bool OPimAccessTemplate<T>::remove( int uid ) { 219bool OPimAccessTemplate<T>::remove( int uid ) {
219 m_cache.remove( uid ); 220 m_cache.remove( uid );
220 return m_backEnd->remove( uid ); 221 return m_backEnd->remove( uid );
221} 222}
222template <class T> 223template <class T>
223bool OPimAccessTemplate<T>::replace( const T& t ) { 224bool OPimAccessTemplate<T>::replace( const T& t ) {
224 m_cache.replace( t ); 225 m_cache.replace( t );
225 return m_backEnd->replace( t ); 226 return m_backEnd->replace( t );
226} 227}
227template <class T> 228template <class T>
228void OPimAccessTemplate<T>::invalidateCache() { 229void OPimAccessTemplate<T>::invalidateCache() {
229 m_cache.invalidate(); 230 m_cache.invalidate();
230} 231}
231template <class T> 232template <class T>
232OPimAccessTemplate<T>::BackEnd* OPimAccessTemplate<T>::backEnd() { 233OPimAccessTemplate<T>::BackEnd* OPimAccessTemplate<T>::backEnd() {
233 return m_backEnd; 234 return m_backEnd;
234} 235}
235template <class T> 236template <class T>
236bool OPimAccessTemplate<T>::wasChangedExternally()const { 237bool OPimAccessTemplate<T>::wasChangedExternally()const {
237 return false; 238 return false;
238} 239}
239template <class T> 240template <class T>
240void OPimAccessTemplate<T>::setBackEnd( BackEnd* end ) { 241void OPimAccessTemplate<T>::setBackEnd( BackEnd* end ) {
241 m_backEnd = end; 242 m_backEnd = end;
242 if (m_backEnd ) 243 if (m_backEnd )
243 m_backEnd->setFrontend( this ); 244 m_backEnd->setFrontend( this );
244} 245}
245template <class T> 246template <class T>
246void OPimAccessTemplate<T>::cache( const T& t ) const{ 247void OPimAccessTemplate<T>::cache( const T& t ) const{
247 /* hacky we need to work around the const*/ 248 /* hacky we need to work around the const*/
248 ((OPimAccessTemplate<T>*)this)->m_cache.add( t ); 249 ((OPimAccessTemplate<T>*)this)->m_cache.add( t );
249} 250}
250template <class T> 251template <class T>
251void OPimAccessTemplate<T>::setSaneCacheSize( int size ) { 252void OPimAccessTemplate<T>::setSaneCacheSize( int size ) {
252 m_cache.setSize( size ); 253 m_cache.setSize( size );
253} 254}
255template <class T>
256void OPimAccessTemplate<T>::setReadAhead( uint count ) {
257 m_backEnd->setReadAhead( count );
258}
254#endif 259#endif
diff --git a/libopie/pim/otodoaccesssql.cpp b/libopie/pim/otodoaccesssql.cpp
index 9ef6b7c..8c2ea3a 100644
--- a/libopie/pim/otodoaccesssql.cpp
+++ b/libopie/pim/otodoaccesssql.cpp
@@ -193,195 +193,195 @@ namespace {
193 } 193 }
194 QString FindQuery::single()const{ 194 QString FindQuery::single()const{
195 QString qu = "select uid, categories, completed, progress, summary, "; 195 QString qu = "select uid, categories, completed, progress, summary, ";
196 qu += "DueDate, priority, description from todolist where uid = " + QString::number(m_uid); 196 qu += "DueDate, priority, description from todolist where uid = " + QString::number(m_uid);
197 return qu; 197 return qu;
198 } 198 }
199 QString FindQuery::multi()const { 199 QString FindQuery::multi()const {
200 QString qu = "select uid, categories, completed, progress, summary, "; 200 QString qu = "select uid, categories, completed, progress, summary, ";
201 qu += "DueDate, priority, description from todolist where "; 201 qu += "DueDate, priority, description from todolist where ";
202 for (uint i = 0; i < m_uids.count(); i++ ) { 202 for (uint i = 0; i < m_uids.count(); i++ ) {
203 qu += " UID = " + QString::number( m_uids[i] ) + " OR"; 203 qu += " UID = " + QString::number( m_uids[i] ) + " OR";
204 } 204 }
205 qu.remove( qu.length()-2, 2 ); 205 qu.remove( qu.length()-2, 2 );
206 return qu; 206 return qu;
207 } 207 }
208 208
209 OverDueQuery::OverDueQuery(): OSQLQuery() {} 209 OverDueQuery::OverDueQuery(): OSQLQuery() {}
210 OverDueQuery::~OverDueQuery() {} 210 OverDueQuery::~OverDueQuery() {}
211 QString OverDueQuery::query()const { 211 QString OverDueQuery::query()const {
212 QDate date = QDate::currentDate(); 212 QDate date = QDate::currentDate();
213 QString str; 213 QString str;
214 str = QString("select uid from todolist where DueDate ='%1-%2-%3'").arg(date.year() ).arg(date.month() ).arg(date.day() ); 214 str = QString("select uid from todolist where DueDate ='%1-%2-%3'").arg(date.year() ).arg(date.month() ).arg(date.day() );
215 215
216 return str; 216 return str;
217 } 217 }
218 218
219 219
220 EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc ) 220 EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc )
221 : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {} 221 : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {}
222 EffQuery::~EffQuery() {} 222 EffQuery::~EffQuery() {}
223 QString EffQuery::query()const { 223 QString EffQuery::query()const {
224 return m_inc ? with() : out(); 224 return m_inc ? with() : out();
225 } 225 }
226 QString EffQuery::with()const { 226 QString EffQuery::with()const {
227 QString str; 227 QString str;
228 str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ") 228 str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ")
229 .arg( m_start.year() ).arg( m_start.month() ).arg( m_start.day() ) 229 .arg( m_start.year() ).arg( m_start.month() ).arg( m_start.day() )
230 .arg( m_end .year() ).arg( m_end .month() ).arg( m_end .day() ); 230 .arg( m_end .year() ).arg( m_end .month() ).arg( m_end .day() );
231 return str; 231 return str;
232 } 232 }
233 QString EffQuery::out()const { 233 QString EffQuery::out()const {
234 QString str; 234 QString str;
235 str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'") 235 str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'")
236 .arg(m_start.year() ).arg(m_start.month() ).arg( m_start.day() ) 236 .arg(m_start.year() ).arg(m_start.month() ).arg( m_start.day() )
237 .arg(m_end. year() ).arg(m_end. month() ).arg(m_end.day() ); 237 .arg(m_end. year() ).arg(m_end. month() ).arg(m_end.day() );
238 238
239 return str; 239 return str;
240 } 240 }
241}; 241};
242 242
243OTodoAccessBackendSQL::OTodoAccessBackendSQL( const QString& file ) 243OTodoAccessBackendSQL::OTodoAccessBackendSQL( const QString& file )
244 : OTodoAccessBackend(), m_dict(15), m_dirty(true) 244 : OTodoAccessBackend(), m_dict(15), m_dirty(true)
245{ 245{
246 QString fi = file; 246 QString fi = file;
247 if ( fi.isEmpty() ) 247 if ( fi.isEmpty() )
248 fi = Global::applicationFileName( "todolist", "todolist.db" ); 248 fi = Global::applicationFileName( "todolist", "todolist.db" );
249 OSQLManager man; 249 OSQLManager man;
250 m_driver = man.standard(); 250 m_driver = man.standard();
251 m_driver->setUrl(fi); 251 m_driver->setUrl(fi);
252 fillDict(); 252 fillDict();
253} 253}
254 254
255OTodoAccessBackendSQL::~OTodoAccessBackendSQL(){ 255OTodoAccessBackendSQL::~OTodoAccessBackendSQL(){
256} 256}
257bool OTodoAccessBackendSQL::load(){ 257bool OTodoAccessBackendSQL::load(){
258 if (!m_driver->open() ) 258 if (!m_driver->open() )
259 return false; 259 return false;
260 260
261 CreateQuery creat; 261 CreateQuery creat;
262 OSQLResult res = m_driver->query(&creat ); 262 OSQLResult res = m_driver->query(&creat );
263 263
264 m_dirty = true; 264 m_dirty = true;
265 return true; 265 return true;
266} 266}
267bool OTodoAccessBackendSQL::reload(){ 267bool OTodoAccessBackendSQL::reload(){
268 return load(); 268 return load();
269} 269}
270 270
271bool OTodoAccessBackendSQL::save(){ 271bool OTodoAccessBackendSQL::save(){
272 return m_driver->close(); 272 return m_driver->close();
273} 273}
274QArray<int> OTodoAccessBackendSQL::allRecords()const { 274QArray<int> OTodoAccessBackendSQL::allRecords()const {
275 if (m_dirty ) 275 if (m_dirty )
276 update(); 276 update();
277 277
278 return m_uids; 278 return m_uids;
279} 279}
280QArray<int> OTodoAccessBackendSQL::queryByExample( const OTodo& , int ){ 280QArray<int> OTodoAccessBackendSQL::queryByExample( const OTodo& , int ){
281 QArray<int> ints(0); 281 QArray<int> ints(0);
282 return ints; 282 return ints;
283} 283}
284OTodo OTodoAccessBackendSQL::find(int uid ) const{ 284OTodo OTodoAccessBackendSQL::find(int uid ) const{
285 FindQuery query( uid ); 285 FindQuery query( uid );
286 return todo( m_driver->query(&query) ); 286 return todo( m_driver->query(&query) );
287 287
288} 288}
289#define CACHE 32
290OTodo OTodoAccessBackendSQL::find( int uid, const QArray<int>& ints, 289OTodo OTodoAccessBackendSQL::find( int uid, const QArray<int>& ints,
291 uint cur, Frontend::CacheDirection dir ) const{ 290 uint cur, Frontend::CacheDirection dir ) const{
291 int CACHE = readAhead();
292 qWarning("searching for %d", uid ); 292 qWarning("searching for %d", uid );
293 QArray<int> search( CACHE ); 293 QArray<int> search( CACHE );
294 uint size =0; 294 uint size =0;
295 OTodo to; 295 OTodo to;
296 296
297 // we try to cache CACHE items 297 // we try to cache CACHE items
298 switch( dir ) { 298 switch( dir ) {
299 /* forward */ 299 /* forward */
300 case 0: 300 case 0:
301 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) { 301 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) {
302 qWarning("size %d %d", size, ints[i] ); 302 qWarning("size %d %d", size, ints[i] );
303 search[size] = ints[i]; 303 search[size] = ints[i];
304 size++; 304 size++;
305 } 305 }
306 break; 306 break;
307 /* reverse */ 307 /* reverse */
308 case 1: 308 case 1:
309 for (uint i = cur; i != 0 && size < CACHE; i-- ) { 309 for (uint i = cur; i != 0 && size < CACHE; i-- ) {
310 search[size] = ints[i]; 310 search[size] = ints[i];
311 size++; 311 size++;
312 } 312 }
313 break; 313 break;
314 } 314 }
315 search.resize( size ); 315 search.resize( size );
316 FindQuery query( search ); 316 FindQuery query( search );
317 OSQLResult res = m_driver->query( &query ); 317 OSQLResult res = m_driver->query( &query );
318 if ( res.state() != OSQLResult::Success ) 318 if ( res.state() != OSQLResult::Success )
319 return to; 319 return to;
320 320
321 return todo( res ); 321 return todo( res );
322} 322}
323void OTodoAccessBackendSQL::clear() { 323void OTodoAccessBackendSQL::clear() {
324 ClearQuery cle; 324 ClearQuery cle;
325 OSQLResult res = m_driver->query( &cle ); 325 OSQLResult res = m_driver->query( &cle );
326 CreateQuery qu; 326 CreateQuery qu;
327 res = m_driver->query(&qu); 327 res = m_driver->query(&qu);
328} 328}
329bool OTodoAccessBackendSQL::add( const OTodo& t) { 329bool OTodoAccessBackendSQL::add( const OTodo& t) {
330 InsertQuery ins( t ); 330 InsertQuery ins( t );
331 OSQLResult res = m_driver->query( &ins ); 331 OSQLResult res = m_driver->query( &ins );
332 332
333 if ( res.state() == OSQLResult::Failure ) 333 if ( res.state() == OSQLResult::Failure )
334 return false; 334 return false;
335 int c = m_uids.count(); 335 int c = m_uids.count();
336 m_uids.resize( c+1 ); 336 m_uids.resize( c+1 );
337 m_uids[c] = t.uid(); 337 m_uids[c] = t.uid();
338 338
339 return true; 339 return true;
340} 340}
341bool OTodoAccessBackendSQL::remove( int uid ) { 341bool OTodoAccessBackendSQL::remove( int uid ) {
342 RemoveQuery rem( uid ); 342 RemoveQuery rem( uid );
343 OSQLResult res = m_driver->query(&rem ); 343 OSQLResult res = m_driver->query(&rem );
344 344
345 if ( res.state() == OSQLResult::Failure ) 345 if ( res.state() == OSQLResult::Failure )
346 return false; 346 return false;
347 347
348 m_dirty = true; 348 m_dirty = true;
349 return true; 349 return true;
350} 350}
351/* 351/*
352 * FIXME better set query 352 * FIXME better set query
353 * but we need the cache for that 353 * but we need the cache for that
354 * now we remove 354 * now we remove
355 */ 355 */
356bool OTodoAccessBackendSQL::replace( const OTodo& t) { 356bool OTodoAccessBackendSQL::replace( const OTodo& t) {
357 remove( t.uid() ); 357 remove( t.uid() );
358 bool b= add(t); 358 bool b= add(t);
359 m_dirty = false; // we changed some stuff but the UID stayed the same 359 m_dirty = false; // we changed some stuff but the UID stayed the same
360 return b; 360 return b;
361} 361}
362QArray<int> OTodoAccessBackendSQL::overDue() { 362QArray<int> OTodoAccessBackendSQL::overDue() {
363 OverDueQuery qu; 363 OverDueQuery qu;
364 return uids( m_driver->query(&qu ) ); 364 return uids( m_driver->query(&qu ) );
365} 365}
366QArray<int> OTodoAccessBackendSQL::effectiveToDos( const QDate& s, 366QArray<int> OTodoAccessBackendSQL::effectiveToDos( const QDate& s,
367 const QDate& t, 367 const QDate& t,
368 bool u) { 368 bool u) {
369 EffQuery ef(s, t, u ); 369 EffQuery ef(s, t, u );
370 return uids (m_driver->query(&ef) ); 370 return uids (m_driver->query(&ef) );
371} 371}
372/* 372/*
373 * 373 *
374 */ 374 */
375QArray<int> OTodoAccessBackendSQL::sorted( bool asc, int sortOrder, 375QArray<int> OTodoAccessBackendSQL::sorted( bool asc, int sortOrder,
376 int sortFilter, int cat ) { 376 int sortFilter, int cat ) {
377 qWarning("sorted %d, %d", asc, sortOrder ); 377 qWarning("sorted %d, %d", asc, sortOrder );
378 QString query; 378 QString query;
379 query = "select uid from todolist WHERE "; 379 query = "select uid from todolist WHERE ";
380 380
381 /* 381 /*
382 * Sort Filter stuff 382 * Sort Filter stuff
383 * not that straight forward 383 * not that straight forward
384 * 384 *
385 */ 385 */
386 /* Category */ 386 /* Category */
387 if ( sortFilter & 1 ) { 387 if ( sortFilter & 1 ) {
diff --git a/libopie2/opiepim/backend/opimaccessbackend.h b/libopie2/opiepim/backend/opimaccessbackend.h
index 27d3cb8..0bd2723 100644
--- a/libopie2/opiepim/backend/opimaccessbackend.h
+++ b/libopie2/opiepim/backend/opimaccessbackend.h
@@ -1,127 +1,141 @@
1#ifndef OPIE_PIM_ACCESS_BACKEND 1#ifndef OPIE_PIM_ACCESS_BACKEND
2#define OPIE_PIM_ACCESS_BACKEND 2#define OPIE_PIM_ACCESS_BACKEND
3 3
4#include <qarray.h> 4#include <qarray.h>
5 5
6#include <opie/otemplatebase.h> 6#include <opie/otemplatebase.h>
7#include <opie/opimrecord.h> 7#include <opie/opimrecord.h>
8 8
9 9
10/** 10/**
11 * OPimAccessBackend is the base class 11 * OPimAccessBackend is the base class
12 * for all private backends 12 * for all private backends
13 * it operates on OPimRecord as the base class 13 * it operates on OPimRecord as the base class
14 * and it's responsible for fast manipulating 14 * and it's responsible for fast manipulating
15 * the resource the implementation takes care 15 * the resource the implementation takes care
16 * of 16 * of
17 */ 17 */
18template <class T = OPimRecord> 18template <class T = OPimRecord>
19class OPimAccessBackend { 19class OPimAccessBackend {
20public: 20public:
21 typedef OTemplateBase<T> Frontend; 21 typedef OTemplateBase<T> Frontend;
22 OPimAccessBackend(); 22 OPimAccessBackend();
23 virtual ~OPimAccessBackend(); 23 virtual ~OPimAccessBackend();
24 24
25 /** 25 /**
26 * load the resource 26 * load the resource
27 */ 27 */
28 virtual bool load() = 0; 28 virtual bool load() = 0;
29 29
30 /** 30 /**
31 * reload the resource 31 * reload the resource
32 */ 32 */
33 virtual bool reload() = 0; 33 virtual bool reload() = 0;
34 34
35 /** 35 /**
36 * save the resource and 36 * save the resource and
37 * all it's changes 37 * all it's changes
38 */ 38 */
39 virtual bool save() = 0; 39 virtual bool save() = 0;
40 40
41 /** 41 /**
42 * return an array of 42 * return an array of
43 * all available uids 43 * all available uids
44 */ 44 */
45 virtual QArray<int> allRecords()const = 0; 45 virtual QArray<int> allRecords()const = 0;
46 46
47 /** 47 /**
48 * queryByExample for T with the SortOrder 48 * queryByExample for T with the SortOrder
49 * sort 49 * sort
50 */ 50 */
51 virtual QArray<int> queryByExample( const T& t, int sort ) = 0; 51 virtual QArray<int> queryByExample( const T& t, int sort ) = 0;
52 52
53 /** 53 /**
54 * find the OPimRecord with uid @param uid 54 * find the OPimRecord with uid @param uid
55 * returns T and T.isEmpty() if nothing was found 55 * returns T and T.isEmpty() if nothing was found
56 */ 56 */
57 virtual T find(int uid )const = 0; 57 virtual T find(int uid )const = 0;
58 58
59 virtual T find(int uid, const QArray<int>& items, 59 virtual T find(int uid, const QArray<int>& items,
60 uint current, Frontend::CacheDirection )const ; 60 uint current, Frontend::CacheDirection )const ;
61 /** 61 /**
62 * clear the back end 62 * clear the back end
63 */ 63 */
64 virtual void clear() = 0; 64 virtual void clear() = 0;
65 65
66 /** 66 /**
67 * add T 67 * add T
68 */ 68 */
69 virtual bool add( const T& t ) = 0; 69 virtual bool add( const T& t ) = 0;
70 70
71 /** 71 /**
72 * remove 72 * remove
73 */ 73 */
74 virtual bool remove( int uid ) = 0; 74 virtual bool remove( int uid ) = 0;
75 75
76 /** 76 /**
77 * replace a record with T.uid() 77 * replace a record with T.uid()
78 */ 78 */
79 virtual bool replace( const T& t ) = 0; 79 virtual bool replace( const T& t ) = 0;
80 80
81 /* 81 /*
82 * setTheFrontEnd!!! 82 * setTheFrontEnd!!!
83 */ 83 */
84 void setFrontend( Frontend* front ); 84 void setFrontend( Frontend* front );
85 85
86 /**
87 * set the read ahead count
88 */
89 void setReadAhead( uint count );
86protected: 90protected:
87 void cache( const T& t )const; 91 void cache( const T& t )const;
88 92
89 /** 93 /**
90 * use a prime number here! 94 * use a prime number here!
91 */ 95 */
92 void setSaneCacheSize( int ); 96 void setSaneCacheSize( int );
93 97
98 uint readAhead()const;
99
94private: 100private:
95 Frontend* m_front; 101 Frontend* m_front;
102 uint m_read;
96 103
97}; 104};
98 105
99template <class T> 106template <class T>
100OPimAccessBackend<T>::OPimAccessBackend() { 107OPimAccessBackend<T>::OPimAccessBackend() {
101 m_front = 0l; 108 m_front = 0l;
102} 109}
103template <class T> 110template <class T>
104OPimAccessBackend<T>::~OPimAccessBackend() { 111OPimAccessBackend<T>::~OPimAccessBackend() {
105 112
106} 113}
107template <class T> 114template <class T>
108void OPimAccessBackend<T>::setFrontend( Frontend* fr ) { 115void OPimAccessBackend<T>::setFrontend( Frontend* fr ) {
109 m_front = fr; 116 m_front = fr;
110} 117}
111template <class T> 118template <class T>
112void OPimAccessBackend<T>::cache( const T& t )const { 119void OPimAccessBackend<T>::cache( const T& t )const {
113 if (m_front ) 120 if (m_front )
114 m_front->cache( t ); 121 m_front->cache( t );
115} 122}
116template <class T> 123template <class T>
117void OPimAccessBackend<T>::setSaneCacheSize( int size) { 124void OPimAccessBackend<T>::setSaneCacheSize( int size) {
118 if (m_front ) 125 if (m_front )
119 m_front->setSaneCacheSize( size ); 126 m_front->setSaneCacheSize( size );
120} 127}
121template <class T> 128template <class T>
122T OPimAccessBackend<T>::find( int uid, const QArray<int>&, 129T OPimAccessBackend<T>::find( int uid, const QArray<int>&,
123 uint, Frontend::CacheDirection )const { 130 uint, Frontend::CacheDirection )const {
124 return find( uid ); 131 return find( uid );
125} 132}
126 133template <class T>
134void OPimAccessBackend<T>::setReadAhead( uint count ) {
135 m_read = count;
136}
137template <class T>
138uint OPimAccessBackend<T>::readAhead()const {
139 return m_read;
140}
127#endif 141#endif
diff --git a/libopie2/opiepim/backend/otodoaccesssql.cpp b/libopie2/opiepim/backend/otodoaccesssql.cpp
index 9ef6b7c..8c2ea3a 100644
--- a/libopie2/opiepim/backend/otodoaccesssql.cpp
+++ b/libopie2/opiepim/backend/otodoaccesssql.cpp
@@ -193,195 +193,195 @@ namespace {
193 } 193 }
194 QString FindQuery::single()const{ 194 QString FindQuery::single()const{
195 QString qu = "select uid, categories, completed, progress, summary, "; 195 QString qu = "select uid, categories, completed, progress, summary, ";
196 qu += "DueDate, priority, description from todolist where uid = " + QString::number(m_uid); 196 qu += "DueDate, priority, description from todolist where uid = " + QString::number(m_uid);
197 return qu; 197 return qu;
198 } 198 }
199 QString FindQuery::multi()const { 199 QString FindQuery::multi()const {
200 QString qu = "select uid, categories, completed, progress, summary, "; 200 QString qu = "select uid, categories, completed, progress, summary, ";
201 qu += "DueDate, priority, description from todolist where "; 201 qu += "DueDate, priority, description from todolist where ";
202 for (uint i = 0; i < m_uids.count(); i++ ) { 202 for (uint i = 0; i < m_uids.count(); i++ ) {
203 qu += " UID = " + QString::number( m_uids[i] ) + " OR"; 203 qu += " UID = " + QString::number( m_uids[i] ) + " OR";
204 } 204 }
205 qu.remove( qu.length()-2, 2 ); 205 qu.remove( qu.length()-2, 2 );
206 return qu; 206 return qu;
207 } 207 }
208 208
209 OverDueQuery::OverDueQuery(): OSQLQuery() {} 209 OverDueQuery::OverDueQuery(): OSQLQuery() {}
210 OverDueQuery::~OverDueQuery() {} 210 OverDueQuery::~OverDueQuery() {}
211 QString OverDueQuery::query()const { 211 QString OverDueQuery::query()const {
212 QDate date = QDate::currentDate(); 212 QDate date = QDate::currentDate();
213 QString str; 213 QString str;
214 str = QString("select uid from todolist where DueDate ='%1-%2-%3'").arg(date.year() ).arg(date.month() ).arg(date.day() ); 214 str = QString("select uid from todolist where DueDate ='%1-%2-%3'").arg(date.year() ).arg(date.month() ).arg(date.day() );
215 215
216 return str; 216 return str;
217 } 217 }
218 218
219 219
220 EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc ) 220 EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc )
221 : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {} 221 : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {}
222 EffQuery::~EffQuery() {} 222 EffQuery::~EffQuery() {}
223 QString EffQuery::query()const { 223 QString EffQuery::query()const {
224 return m_inc ? with() : out(); 224 return m_inc ? with() : out();
225 } 225 }
226 QString EffQuery::with()const { 226 QString EffQuery::with()const {
227 QString str; 227 QString str;
228 str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ") 228 str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ")
229 .arg( m_start.year() ).arg( m_start.month() ).arg( m_start.day() ) 229 .arg( m_start.year() ).arg( m_start.month() ).arg( m_start.day() )
230 .arg( m_end .year() ).arg( m_end .month() ).arg( m_end .day() ); 230 .arg( m_end .year() ).arg( m_end .month() ).arg( m_end .day() );
231 return str; 231 return str;
232 } 232 }
233 QString EffQuery::out()const { 233 QString EffQuery::out()const {
234 QString str; 234 QString str;
235 str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'") 235 str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'")
236 .arg(m_start.year() ).arg(m_start.month() ).arg( m_start.day() ) 236 .arg(m_start.year() ).arg(m_start.month() ).arg( m_start.day() )
237 .arg(m_end. year() ).arg(m_end. month() ).arg(m_end.day() ); 237 .arg(m_end. year() ).arg(m_end. month() ).arg(m_end.day() );
238 238
239 return str; 239 return str;
240 } 240 }
241}; 241};
242 242
243OTodoAccessBackendSQL::OTodoAccessBackendSQL( const QString& file ) 243OTodoAccessBackendSQL::OTodoAccessBackendSQL( const QString& file )
244 : OTodoAccessBackend(), m_dict(15), m_dirty(true) 244 : OTodoAccessBackend(), m_dict(15), m_dirty(true)
245{ 245{
246 QString fi = file; 246 QString fi = file;
247 if ( fi.isEmpty() ) 247 if ( fi.isEmpty() )
248 fi = Global::applicationFileName( "todolist", "todolist.db" ); 248 fi = Global::applicationFileName( "todolist", "todolist.db" );
249 OSQLManager man; 249 OSQLManager man;
250 m_driver = man.standard(); 250 m_driver = man.standard();
251 m_driver->setUrl(fi); 251 m_driver->setUrl(fi);
252 fillDict(); 252 fillDict();
253} 253}
254 254
255OTodoAccessBackendSQL::~OTodoAccessBackendSQL(){ 255OTodoAccessBackendSQL::~OTodoAccessBackendSQL(){
256} 256}
257bool OTodoAccessBackendSQL::load(){ 257bool OTodoAccessBackendSQL::load(){
258 if (!m_driver->open() ) 258 if (!m_driver->open() )
259 return false; 259 return false;
260 260
261 CreateQuery creat; 261 CreateQuery creat;
262 OSQLResult res = m_driver->query(&creat ); 262 OSQLResult res = m_driver->query(&creat );
263 263
264 m_dirty = true; 264 m_dirty = true;
265 return true; 265 return true;
266} 266}
267bool OTodoAccessBackendSQL::reload(){ 267bool OTodoAccessBackendSQL::reload(){
268 return load(); 268 return load();
269} 269}
270 270
271bool OTodoAccessBackendSQL::save(){ 271bool OTodoAccessBackendSQL::save(){
272 return m_driver->close(); 272 return m_driver->close();
273} 273}
274QArray<int> OTodoAccessBackendSQL::allRecords()const { 274QArray<int> OTodoAccessBackendSQL::allRecords()const {
275 if (m_dirty ) 275 if (m_dirty )
276 update(); 276 update();
277 277
278 return m_uids; 278 return m_uids;
279} 279}
280QArray<int> OTodoAccessBackendSQL::queryByExample( const OTodo& , int ){ 280QArray<int> OTodoAccessBackendSQL::queryByExample( const OTodo& , int ){
281 QArray<int> ints(0); 281 QArray<int> ints(0);
282 return ints; 282 return ints;
283} 283}
284OTodo OTodoAccessBackendSQL::find(int uid ) const{ 284OTodo OTodoAccessBackendSQL::find(int uid ) const{
285 FindQuery query( uid ); 285 FindQuery query( uid );
286 return todo( m_driver->query(&query) ); 286 return todo( m_driver->query(&query) );
287 287
288} 288}
289#define CACHE 32
290OTodo OTodoAccessBackendSQL::find( int uid, const QArray<int>& ints, 289OTodo OTodoAccessBackendSQL::find( int uid, const QArray<int>& ints,
291 uint cur, Frontend::CacheDirection dir ) const{ 290 uint cur, Frontend::CacheDirection dir ) const{
291 int CACHE = readAhead();
292 qWarning("searching for %d", uid ); 292 qWarning("searching for %d", uid );
293 QArray<int> search( CACHE ); 293 QArray<int> search( CACHE );
294 uint size =0; 294 uint size =0;
295 OTodo to; 295 OTodo to;
296 296
297 // we try to cache CACHE items 297 // we try to cache CACHE items
298 switch( dir ) { 298 switch( dir ) {
299 /* forward */ 299 /* forward */
300 case 0: 300 case 0:
301 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) { 301 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) {
302 qWarning("size %d %d", size, ints[i] ); 302 qWarning("size %d %d", size, ints[i] );
303 search[size] = ints[i]; 303 search[size] = ints[i];
304 size++; 304 size++;
305 } 305 }
306 break; 306 break;
307 /* reverse */ 307 /* reverse */
308 case 1: 308 case 1:
309 for (uint i = cur; i != 0 && size < CACHE; i-- ) { 309 for (uint i = cur; i != 0 && size < CACHE; i-- ) {
310 search[size] = ints[i]; 310 search[size] = ints[i];
311 size++; 311 size++;
312 } 312 }
313 break; 313 break;
314 } 314 }
315 search.resize( size ); 315 search.resize( size );
316 FindQuery query( search ); 316 FindQuery query( search );
317 OSQLResult res = m_driver->query( &query ); 317 OSQLResult res = m_driver->query( &query );
318 if ( res.state() != OSQLResult::Success ) 318 if ( res.state() != OSQLResult::Success )
319 return to; 319 return to;
320 320
321 return todo( res ); 321 return todo( res );
322} 322}
323void OTodoAccessBackendSQL::clear() { 323void OTodoAccessBackendSQL::clear() {
324 ClearQuery cle; 324 ClearQuery cle;
325 OSQLResult res = m_driver->query( &cle ); 325 OSQLResult res = m_driver->query( &cle );
326 CreateQuery qu; 326 CreateQuery qu;
327 res = m_driver->query(&qu); 327 res = m_driver->query(&qu);
328} 328}
329bool OTodoAccessBackendSQL::add( const OTodo& t) { 329bool OTodoAccessBackendSQL::add( const OTodo& t) {
330 InsertQuery ins( t ); 330 InsertQuery ins( t );
331 OSQLResult res = m_driver->query( &ins ); 331 OSQLResult res = m_driver->query( &ins );
332 332
333 if ( res.state() == OSQLResult::Failure ) 333 if ( res.state() == OSQLResult::Failure )
334 return false; 334 return false;
335 int c = m_uids.count(); 335 int c = m_uids.count();
336 m_uids.resize( c+1 ); 336 m_uids.resize( c+1 );
337 m_uids[c] = t.uid(); 337 m_uids[c] = t.uid();
338 338
339 return true; 339 return true;
340} 340}
341bool OTodoAccessBackendSQL::remove( int uid ) { 341bool OTodoAccessBackendSQL::remove( int uid ) {
342 RemoveQuery rem( uid ); 342 RemoveQuery rem( uid );
343 OSQLResult res = m_driver->query(&rem ); 343 OSQLResult res = m_driver->query(&rem );
344 344
345 if ( res.state() == OSQLResult::Failure ) 345 if ( res.state() == OSQLResult::Failure )
346 return false; 346 return false;
347 347
348 m_dirty = true; 348 m_dirty = true;
349 return true; 349 return true;
350} 350}
351/* 351/*
352 * FIXME better set query 352 * FIXME better set query
353 * but we need the cache for that 353 * but we need the cache for that
354 * now we remove 354 * now we remove
355 */ 355 */
356bool OTodoAccessBackendSQL::replace( const OTodo& t) { 356bool OTodoAccessBackendSQL::replace( const OTodo& t) {
357 remove( t.uid() ); 357 remove( t.uid() );
358 bool b= add(t); 358 bool b= add(t);
359 m_dirty = false; // we changed some stuff but the UID stayed the same 359 m_dirty = false; // we changed some stuff but the UID stayed the same
360 return b; 360 return b;
361} 361}
362QArray<int> OTodoAccessBackendSQL::overDue() { 362QArray<int> OTodoAccessBackendSQL::overDue() {
363 OverDueQuery qu; 363 OverDueQuery qu;
364 return uids( m_driver->query(&qu ) ); 364 return uids( m_driver->query(&qu ) );
365} 365}
366QArray<int> OTodoAccessBackendSQL::effectiveToDos( const QDate& s, 366QArray<int> OTodoAccessBackendSQL::effectiveToDos( const QDate& s,
367 const QDate& t, 367 const QDate& t,
368 bool u) { 368 bool u) {
369 EffQuery ef(s, t, u ); 369 EffQuery ef(s, t, u );
370 return uids (m_driver->query(&ef) ); 370 return uids (m_driver->query(&ef) );
371} 371}
372/* 372/*
373 * 373 *
374 */ 374 */
375QArray<int> OTodoAccessBackendSQL::sorted( bool asc, int sortOrder, 375QArray<int> OTodoAccessBackendSQL::sorted( bool asc, int sortOrder,
376 int sortFilter, int cat ) { 376 int sortFilter, int cat ) {
377 qWarning("sorted %d, %d", asc, sortOrder ); 377 qWarning("sorted %d, %d", asc, sortOrder );
378 QString query; 378 QString query;
379 query = "select uid from todolist WHERE "; 379 query = "select uid from todolist WHERE ";
380 380
381 /* 381 /*
382 * Sort Filter stuff 382 * Sort Filter stuff
383 * not that straight forward 383 * not that straight forward
384 * 384 *
385 */ 385 */
386 /* Category */ 386 /* Category */
387 if ( sortFilter & 1 ) { 387 if ( sortFilter & 1 ) {
diff --git a/libopie2/opiepim/core/opimaccesstemplate.h b/libopie2/opiepim/core/opimaccesstemplate.h
index 50cb1e4..c5523a8 100644
--- a/libopie2/opiepim/core/opimaccesstemplate.h
+++ b/libopie2/opiepim/core/opimaccesstemplate.h
@@ -1,254 +1,259 @@
1#ifndef OPIE_PIM_ACCESS_TEMPLATE_H 1#ifndef OPIE_PIM_ACCESS_TEMPLATE_H
2#define OPIE_PIM_ACCESS_TEMPLATE_H 2#define OPIE_PIM_ACCESS_TEMPLATE_H
3 3
4#include <qarray.h> 4#include <qarray.h>
5 5
6#include <opie/opimrecord.h> 6#include <opie/opimrecord.h>
7#include <opie/opimaccessbackend.h> 7#include <opie/opimaccessbackend.h>
8#include <opie/orecordlist.h> 8#include <opie/orecordlist.h>
9 9
10#include "opimcache.h" 10#include "opimcache.h"
11#include "otemplatebase.h" 11#include "otemplatebase.h"
12 12
13/** 13/**
14 * Thats the frontend to our OPIE PIM 14 * Thats the frontend to our OPIE PIM
15 * Library. Either you want to use it's 15 * Library. Either you want to use it's
16 * interface or you want to implement 16 * interface or you want to implement
17 * your own Access lib 17 * your own Access lib
18 * Just create a OPimRecord and inherit from 18 * Just create a OPimRecord and inherit from
19 * the plugins 19 * the plugins
20 */ 20 */
21 21
22template <class T = OPimRecord > 22template <class T = OPimRecord >
23class OPimAccessTemplate : public OTemplateBase<T> { 23class OPimAccessTemplate : public OTemplateBase<T> {
24public: 24public:
25 typedef ORecordList<T> List; 25 typedef ORecordList<T> List;
26 typedef OPimAccessBackend<T> BackEnd; 26 typedef OPimAccessBackend<T> BackEnd;
27 typedef OPimCache<T> Cache; 27 typedef OPimCache<T> Cache;
28 28
29 /** 29 /**
30 * c'tor BackEnd 30 * c'tor BackEnd
31 */ 31 */
32 OPimAccessTemplate( BackEnd* end); 32 OPimAccessTemplate( BackEnd* end);
33 virtual ~OPimAccessTemplate(); 33 virtual ~OPimAccessTemplate();
34 34
35 /** 35 /**
36 * load from the backend 36 * load from the backend
37 */ 37 */
38 virtual bool load(); 38 virtual bool load();
39 39
40 /** Reload database. 40 /** Reload database.
41 * You should execute this function if the external database 41 * You should execute this function if the external database
42 * was changed. 42 * was changed.
43 * This function will load the external database and afterwards 43 * This function will load the external database and afterwards
44 * rejoin the local changes. Therefore the local database will be set consistent. 44 * rejoin the local changes. Therefore the local database will be set consistent.
45 */ 45 */
46 virtual bool reload(); 46 virtual bool reload();
47 47
48 /** Save contacts database. 48 /** Save contacts database.
49 * Save is more a "commit". After calling this function, all changes are public available. 49 * Save is more a "commit". After calling this function, all changes are public available.
50 * @return true if successful 50 * @return true if successful
51 */ 51 */
52 virtual bool save(); 52 virtual bool save();
53 53
54 /** 54 /**
55 * if the resource was changed externally 55 * if the resource was changed externally
56 * You should use the signal handling instead of polling possible changes ! 56 * You should use the signal handling instead of polling possible changes !
57 * zecke: Do you implement a signal for otodoaccess ? 57 * zecke: Do you implement a signal for otodoaccess ?
58 */ 58 */
59 bool wasChangedExternally()const; 59 bool wasChangedExternally()const;
60 60
61 /** 61 /**
62 * return a List of records 62 * return a List of records
63 * you can iterate over them 63 * you can iterate over them
64 */ 64 */
65 virtual List allRecords()const; 65 virtual List allRecords()const;
66 66
67 /** 67 /**
68 * queryByExample. 68 * queryByExample.
69 * @see otodoaccess, ocontactaccess 69 * @see otodoaccess, ocontactaccess
70 */ 70 */
71 virtual List queryByExample( const T& t, int querySettings ); 71 virtual List queryByExample( const T& t, int querySettings );
72 72
73 /** 73 /**
74 * find the OPimRecord uid 74 * find the OPimRecord uid
75 */ 75 */
76 virtual T find( int uid )const; 76 virtual T find( int uid )const;
77 77
78 /** 78 /**
79 * read ahead cache find method ;) 79 * read ahead cache find method ;)
80 */ 80 */
81 virtual T find( int uid, const QArray<int>&, 81 virtual T find( int uid, const QArray<int>&,
82 uint current, CacheDirection dir = Forward )const; 82 uint current, CacheDirection dir = Forward )const;
83 83
84 /* invalidate cache here */ 84 /* invalidate cache here */
85 /** 85 /**
86 * clears the backend and invalidates the backend 86 * clears the backend and invalidates the backend
87 */ 87 */
88 virtual void clear() ; 88 virtual void clear() ;
89 89
90 /** 90 /**
91 * add T to the backend 91 * add T to the backend
92 * @param t The item to add. 92 * @param t The item to add.
93 * @return <i>true</i> if added successfully. 93 * @return <i>true</i> if added successfully.
94 */ 94 */
95 virtual bool add( const T& t ) ; 95 virtual bool add( const T& t ) ;
96 96
97 /* only the uid matters */ 97 /* only the uid matters */
98 /** 98 /**
99 * remove T from the backend 99 * remove T from the backend
100 * @param t The item to remove 100 * @param t The item to remove
101 * @return <i>true</i> if successful. 101 * @return <i>true</i> if successful.
102 */ 102 */
103 virtual bool remove( const T& t ); 103 virtual bool remove( const T& t );
104 104
105 /** 105 /**
106 * remove the OPimRecord with uid 106 * remove the OPimRecord with uid
107 * @param uid The ID of the item to remove 107 * @param uid The ID of the item to remove
108 * @return <i>true</i> if successful. 108 * @return <i>true</i> if successful.
109 */ 109 */
110 virtual bool remove( int uid ); 110 virtual bool remove( int uid );
111 111
112 /** 112 /**
113 * replace T from backend 113 * replace T from backend
114 * @param t The item to replace 114 * @param t The item to replace
115 * @return <i>true</i> if successful. 115 * @return <i>true</i> if successful.
116 */ 116 */
117 virtual bool replace( const T& t) ; 117 virtual bool replace( const T& t) ;
118 118
119 void setReadAhead( uint count );
119 /** 120 /**
120 * @internal 121 * @internal
121 */ 122 */
122 void cache( const T& )const; 123 void cache( const T& )const;
123 void setSaneCacheSize( int ); 124 void setSaneCacheSize( int );
124protected: 125protected:
125 /** 126 /**
126 * invalidate the cache 127 * invalidate the cache
127 */ 128 */
128 void invalidateCache(); 129 void invalidateCache();
129 130
130 void setBackEnd( BackEnd* end ); 131 void setBackEnd( BackEnd* end );
131 /** 132 /**
132 * returns the backend 133 * returns the backend
133 */ 134 */
134 BackEnd* backEnd(); 135 BackEnd* backEnd();
135 BackEnd* m_backEnd; 136 BackEnd* m_backEnd;
136 Cache m_cache; 137 Cache m_cache;
137 138
138}; 139};
139 140
140template <class T> 141template <class T>
141OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end ) 142OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end )
142 : OTemplateBase<T>(), m_backEnd( end ) 143 : OTemplateBase<T>(), m_backEnd( end )
143{ 144{
144 if (end ) 145 if (end )
145 end->setFrontend( this ); 146 end->setFrontend( this );
146} 147}
147template <class T> 148template <class T>
148OPimAccessTemplate<T>::~OPimAccessTemplate() { 149OPimAccessTemplate<T>::~OPimAccessTemplate() {
149 qWarning("~OPimAccessTemplate<T>"); 150 qWarning("~OPimAccessTemplate<T>");
150 delete m_backEnd; 151 delete m_backEnd;
151} 152}
152template <class T> 153template <class T>
153bool OPimAccessTemplate<T>::load() { 154bool OPimAccessTemplate<T>::load() {
154 invalidateCache(); 155 invalidateCache();
155 return m_backEnd->load(); 156 return m_backEnd->load();
156} 157}
157template <class T> 158template <class T>
158bool OPimAccessTemplate<T>::reload() { 159bool OPimAccessTemplate<T>::reload() {
159 invalidateCache(); // zecke: I think this should be added (se) 160 invalidateCache(); // zecke: I think this should be added (se)
160 return m_backEnd->reload(); 161 return m_backEnd->reload();
161} 162}
162template <class T> 163template <class T>
163bool OPimAccessTemplate<T>::save() { 164bool OPimAccessTemplate<T>::save() {
164 return m_backEnd->save(); 165 return m_backEnd->save();
165} 166}
166template <class T> 167template <class T>
167OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const { 168OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const {
168 QArray<int> ints = m_backEnd->allRecords(); 169 QArray<int> ints = m_backEnd->allRecords();
169 List lis(ints, this ); 170 List lis(ints, this );
170 return lis; 171 return lis;
171} 172}
172template <class T> 173template <class T>
173OPimAccessTemplate<T>::List 174OPimAccessTemplate<T>::List
174OPimAccessTemplate<T>::queryByExample( const T& t, int sortOrder ) { 175OPimAccessTemplate<T>::queryByExample( const T& t, int sortOrder ) {
175 QArray<int> ints = m_backEnd->queryByExample( t, sortOrder ); 176 QArray<int> ints = m_backEnd->queryByExample( t, sortOrder );
176 177
177 List lis(ints, this ); 178 List lis(ints, this );
178 return lis; 179 return lis;
179} 180}
180template <class T> 181template <class T>
181T OPimAccessTemplate<T>::find( int uid ) const{ 182T OPimAccessTemplate<T>::find( int uid ) const{
182 T t = m_backEnd->find( uid ); 183 T t = m_backEnd->find( uid );
183 cache( t ); 184 cache( t );
184 return t; 185 return t;
185} 186}
186template <class T> 187template <class T>
187T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar, 188T OPimAccessTemplate<T>::find( int uid, const QArray<int>& ar,
188 uint current, CacheDirection dir )const { 189 uint current, CacheDirection dir )const {
189 /* 190 /*
190 * better do T.isEmpty() 191 * better do T.isEmpty()
191 * after a find this way we would 192 * after a find this way we would
192 * avoid two finds in QCache... 193 * avoid two finds in QCache...
193 */ 194 */
194 // qWarning("find it now %d", uid ); 195 // qWarning("find it now %d", uid );
195 if (m_cache.contains( uid ) ) { 196 if (m_cache.contains( uid ) ) {
196 return m_cache.find( uid ); 197 return m_cache.find( uid );
197 } 198 }
198 199
199 T t = m_backEnd->find( uid, ar, current, dir ); 200 T t = m_backEnd->find( uid, ar, current, dir );
200 cache( t ); 201 cache( t );
201 return t; 202 return t;
202} 203}
203template <class T> 204template <class T>
204void OPimAccessTemplate<T>::clear() { 205void OPimAccessTemplate<T>::clear() {
205 invalidateCache(); 206 invalidateCache();
206 m_backEnd->clear(); 207 m_backEnd->clear();
207} 208}
208template <class T> 209template <class T>
209bool OPimAccessTemplate<T>::add( const T& t ) { 210bool OPimAccessTemplate<T>::add( const T& t ) {
210 cache( t ); 211 cache( t );
211 return m_backEnd->add( t ); 212 return m_backEnd->add( t );
212} 213}
213template <class T> 214template <class T>
214bool OPimAccessTemplate<T>::remove( const T& t ) { 215bool OPimAccessTemplate<T>::remove( const T& t ) {
215 return remove( t.uid() ); 216 return remove( t.uid() );
216} 217}
217template <class T> 218template <class T>
218bool OPimAccessTemplate<T>::remove( int uid ) { 219bool OPimAccessTemplate<T>::remove( int uid ) {
219 m_cache.remove( uid ); 220 m_cache.remove( uid );
220 return m_backEnd->remove( uid ); 221 return m_backEnd->remove( uid );
221} 222}
222template <class T> 223template <class T>
223bool OPimAccessTemplate<T>::replace( const T& t ) { 224bool OPimAccessTemplate<T>::replace( const T& t ) {
224 m_cache.replace( t ); 225 m_cache.replace( t );
225 return m_backEnd->replace( t ); 226 return m_backEnd->replace( t );
226} 227}
227template <class T> 228template <class T>
228void OPimAccessTemplate<T>::invalidateCache() { 229void OPimAccessTemplate<T>::invalidateCache() {
229 m_cache.invalidate(); 230 m_cache.invalidate();
230} 231}
231template <class T> 232template <class T>
232OPimAccessTemplate<T>::BackEnd* OPimAccessTemplate<T>::backEnd() { 233OPimAccessTemplate<T>::BackEnd* OPimAccessTemplate<T>::backEnd() {
233 return m_backEnd; 234 return m_backEnd;
234} 235}
235template <class T> 236template <class T>
236bool OPimAccessTemplate<T>::wasChangedExternally()const { 237bool OPimAccessTemplate<T>::wasChangedExternally()const {
237 return false; 238 return false;
238} 239}
239template <class T> 240template <class T>
240void OPimAccessTemplate<T>::setBackEnd( BackEnd* end ) { 241void OPimAccessTemplate<T>::setBackEnd( BackEnd* end ) {
241 m_backEnd = end; 242 m_backEnd = end;
242 if (m_backEnd ) 243 if (m_backEnd )
243 m_backEnd->setFrontend( this ); 244 m_backEnd->setFrontend( this );
244} 245}
245template <class T> 246template <class T>
246void OPimAccessTemplate<T>::cache( const T& t ) const{ 247void OPimAccessTemplate<T>::cache( const T& t ) const{
247 /* hacky we need to work around the const*/ 248 /* hacky we need to work around the const*/
248 ((OPimAccessTemplate<T>*)this)->m_cache.add( t ); 249 ((OPimAccessTemplate<T>*)this)->m_cache.add( t );
249} 250}
250template <class T> 251template <class T>
251void OPimAccessTemplate<T>::setSaneCacheSize( int size ) { 252void OPimAccessTemplate<T>::setSaneCacheSize( int size ) {
252 m_cache.setSize( size ); 253 m_cache.setSize( size );
253} 254}
255template <class T>
256void OPimAccessTemplate<T>::setReadAhead( uint count ) {
257 m_backEnd->setReadAhead( count );
258}
254#endif 259#endif