summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp24
-rw-r--r--libopie2/opiepim/backend/otodoaccesssql.cpp75
2 files changed, 80 insertions, 19 deletions
diff --git a/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp b/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp
index 8a8cb0b..495a8dd 100644
--- a/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp
+++ b/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp
@@ -1,432 +1,452 @@
1/* 1/*
2 This file is part of the Opie Project 2 This file is part of the Opie Project
3 Copyright (C) Stefan Eilers (Eilers.Stefan@epost.de) 3 Copyright (C) Stefan Eilers (Eilers.Stefan@epost.de)
4 =. Copyright (C) The Opie Team <opie-devel@handhelds.org> 4 =. Copyright (C) The Opie Team <opie-devel@handhelds.org>
5 .=l. 5 .=l.
6 .>+-= 6 .>+-=
7 _;:, .> :=|. This program is free software; you can 7 _;:, .> :=|. This program is free software; you can
8.> <`_, > . <= redistribute it and/or modify it under 8.> <`_, > . <= redistribute it and/or modify it under
9:`=1 )Y*s>-.-- : the terms of the GNU Library General Public 9:`=1 )Y*s>-.-- : the terms of the GNU Library General Public
10.="- .-=="i, .._ License as published by the Free Software 10.="- .-=="i, .._ License as published by the Free Software
11 - . .-<_> .<> Foundation; either version 2 of the License, 11 - . .-<_> .<> Foundation; either version 2 of the License,
12 ._= =} : or (at your option) any later version. 12 ._= =} : or (at your option) any later version.
13 .%`+i> _;_. 13 .%`+i> _;_.
14 .i_,=:_. -<s. This program is distributed in the hope that 14 .i_,=:_. -<s. This program is distributed in the hope that
15 + . -:. = it will be useful, but WITHOUT ANY WARRANTY; 15 + . -:. = it will be useful, but WITHOUT ANY WARRANTY;
16 : .. .:, . . . without even the implied warranty of 16 : .. .:, . . . without even the implied warranty of
17 =_ + =;=|` MERCHANTABILITY or FITNESS FOR A 17 =_ + =;=|` MERCHANTABILITY or FITNESS FOR A
18 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU 18 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU
19..}^=.= = ; Library General Public License for more 19..}^=.= = ; Library General Public License for more
20++= -. .` .: details. 20++= -. .` .: details.
21 : = ...= . :.=- 21 : = ...= . :.=-
22 -. .:....=;==+<; You should have received a copy of the GNU 22 -. .:....=;==+<; You should have received a copy of the GNU
23 -_. . . )=. = Library General Public License along with 23 -_. . . )=. = Library General Public License along with
24 -- :-=` this library; see the file COPYING.LIB. 24 -- :-=` this library; see the file COPYING.LIB.
25 If not, write to the Free Software Foundation, 25 If not, write to the Free Software Foundation,
26 Inc., 59 Temple Place - Suite 330, 26 Inc., 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA. 27 Boston, MA 02111-1307, USA.
28*/ 28*/
29/* 29/*
30 * SQL Backend for the OPIE-Calender Database. 30 * SQL Backend for the OPIE-Calender Database.
31 * 31 *
32 */ 32 */
33 33
34#include <stdio.h> 34#include <stdio.h>
35#include <stdlib.h> 35#include <stdlib.h>
36 36
37#include <qarray.h> 37#include <qarray.h>
38#include <qstringlist.h> 38#include <qstringlist.h>
39 39
40#include <qpe/global.h> 40#include <qpe/global.h>
41 41
42#include <opie2/osqldriver.h> 42#include <opie2/osqldriver.h>
43#include <opie2/osqlmanager.h> 43#include <opie2/osqlmanager.h>
44#include <opie2/osqlquery.h> 44#include <opie2/osqlquery.h>
45 45
46#include <opie2/opimrecurrence.h> 46#include <opie2/opimrecurrence.h>
47#include <opie2/odatebookaccessbackend_sql.h> 47#include <opie2/odatebookaccessbackend_sql.h>
48 48
49using namespace Opie::DB; 49using namespace Opie::DB;
50 50
51namespace { 51namespace {
52 /** 52 /**
53 * a find query for custom elements 53 * a find query for custom elements
54 */ 54 */
55 class FindCustomQuery : public OSQLQuery { 55 class FindCustomQuery : public OSQLQuery {
56 public: 56 public:
57 FindCustomQuery(int uid); 57 FindCustomQuery(int uid);
58 FindCustomQuery(const QArray<int>& ); 58 FindCustomQuery(const QArray<int>& );
59 ~FindCustomQuery(); 59 ~FindCustomQuery();
60 QString query()const; 60 QString query()const;
61 private: 61 private:
62 QString single()const; 62 QString single()const;
63 QString multi()const; 63 QString multi()const;
64 QArray<int> m_uids; 64 QArray<int> m_uids;
65 int m_uid; 65 int m_uid;
66 }; 66 };
67 67
68 FindCustomQuery::FindCustomQuery(int uid) 68 FindCustomQuery::FindCustomQuery(int uid)
69 : OSQLQuery(), m_uid( uid ) { 69 : OSQLQuery(), m_uid( uid ) {
70 } 70 }
71 FindCustomQuery::FindCustomQuery(const QArray<int>& ints) 71 FindCustomQuery::FindCustomQuery(const QArray<int>& ints)
72 : OSQLQuery(), m_uids( ints ){ 72 : OSQLQuery(), m_uids( ints ){
73 } 73 }
74 FindCustomQuery::~FindCustomQuery() { 74 FindCustomQuery::~FindCustomQuery() {
75 } 75 }
76 QString FindCustomQuery::query()const{ 76 QString FindCustomQuery::query()const{
77 // if ( m_uids.count() == 0 ) 77 // if ( m_uids.count() == 0 )
78 return single(); 78 return single();
79 } 79 }
80 QString FindCustomQuery::single()const{ 80 QString FindCustomQuery::single()const{
81 QString qu = "select uid, type, value from custom_data where uid = "; 81 QString qu = "select uid, type, value from custom_data where uid = ";
82 qu += QString::number(m_uid); 82 qu += QString::number(m_uid);
83 return qu; 83 return qu;
84 } 84 }
85} 85}
86 86
87 87
88namespace Opie { 88namespace Opie {
89 89
90 90
91ODateBookAccessBackend_SQL::ODateBookAccessBackend_SQL( const QString& , 91ODateBookAccessBackend_SQL::ODateBookAccessBackend_SQL( const QString& ,
92 const QString& fileName ) 92 const QString& fileName )
93 : ODateBookAccessBackend(), m_driver( NULL ) 93 : ODateBookAccessBackend(), m_driver( NULL )
94{ 94{
95 m_fileName = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.db" ) : fileName; 95 m_fileName = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.db" ) : fileName;
96 96
97 // Get the standart sql-driver from the OSQLManager.. 97 // Get the standart sql-driver from the OSQLManager..
98 OSQLManager man; 98 OSQLManager man;
99 m_driver = man.standard(); 99 m_driver = man.standard();
100 m_driver->setUrl( m_fileName ); 100 m_driver->setUrl( m_fileName );
101 101
102 initFields(); 102 initFields();
103 103
104 load(); 104 load();
105} 105}
106 106
107ODateBookAccessBackend_SQL::~ODateBookAccessBackend_SQL() { 107ODateBookAccessBackend_SQL::~ODateBookAccessBackend_SQL() {
108 if( m_driver ) 108 if( m_driver )
109 delete m_driver; 109 delete m_driver;
110} 110}
111 111
112void ODateBookAccessBackend_SQL::initFields() 112void ODateBookAccessBackend_SQL::initFields()
113{ 113{
114 114
115 // This map contains the translation of the fieldtype id's to 115 // This map contains the translation of the fieldtype id's to
116 // the names of the table columns 116 // the names of the table columns
117 m_fieldMap.insert( OPimEvent::FUid, "uid" ); 117 m_fieldMap.insert( OPimEvent::FUid, "uid" );
118 m_fieldMap.insert( OPimEvent::FCategories, "Categories" ); 118 m_fieldMap.insert( OPimEvent::FCategories, "Categories" );
119 m_fieldMap.insert( OPimEvent::FDescription, "Description" ); 119 m_fieldMap.insert( OPimEvent::FDescription, "Description" );
120 m_fieldMap.insert( OPimEvent::FLocation, "Location" ); 120 m_fieldMap.insert( OPimEvent::FLocation, "Location" );
121 m_fieldMap.insert( OPimEvent::FType, "Type" ); 121 m_fieldMap.insert( OPimEvent::FType, "Type" );
122 m_fieldMap.insert( OPimEvent::FAlarm, "Alarm" ); 122 m_fieldMap.insert( OPimEvent::FAlarm, "Alarm" );
123 m_fieldMap.insert( OPimEvent::FSound, "Sound" ); 123 m_fieldMap.insert( OPimEvent::FSound, "Sound" );
124 m_fieldMap.insert( OPimEvent::FRType, "RType" ); 124 m_fieldMap.insert( OPimEvent::FRType, "RType" );
125 m_fieldMap.insert( OPimEvent::FRWeekdays, "RWeekdays" ); 125 m_fieldMap.insert( OPimEvent::FRWeekdays, "RWeekdays" );
126 m_fieldMap.insert( OPimEvent::FRPosition, "RPosition" ); 126 m_fieldMap.insert( OPimEvent::FRPosition, "RPosition" );
127 m_fieldMap.insert( OPimEvent::FRFreq, "RFreq" ); 127 m_fieldMap.insert( OPimEvent::FRFreq, "RFreq" );
128 m_fieldMap.insert( OPimEvent::FRHasEndDate, "RHasEndDate" ); 128 m_fieldMap.insert( OPimEvent::FRHasEndDate, "RHasEndDate" );
129 m_fieldMap.insert( OPimEvent::FREndDate, "REndDate" ); 129 m_fieldMap.insert( OPimEvent::FREndDate, "REndDate" );
130 m_fieldMap.insert( OPimEvent::FRCreated, "RCreated" ); 130 m_fieldMap.insert( OPimEvent::FRCreated, "RCreated" );
131 m_fieldMap.insert( OPimEvent::FRExceptions, "RExceptions" ); 131 m_fieldMap.insert( OPimEvent::FRExceptions, "RExceptions" );
132 m_fieldMap.insert( OPimEvent::FStart, "Start" ); 132 m_fieldMap.insert( OPimEvent::FStart, "Start" );
133 m_fieldMap.insert( OPimEvent::FEnd, "End" ); 133 m_fieldMap.insert( OPimEvent::FEnd, "End" );
134 m_fieldMap.insert( OPimEvent::FNote, "Note" ); 134 m_fieldMap.insert( OPimEvent::FNote, "Note" );
135 m_fieldMap.insert( OPimEvent::FTimeZone, "TimeZone" ); 135 m_fieldMap.insert( OPimEvent::FTimeZone, "TimeZone" );
136 m_fieldMap.insert( OPimEvent::FRecParent, "RecParent" ); 136 m_fieldMap.insert( OPimEvent::FRecParent, "RecParent" );
137 m_fieldMap.insert( OPimEvent::FRecChildren, "Recchildren" ); 137 m_fieldMap.insert( OPimEvent::FRecChildren, "Recchildren" );
138 138
139 // Create a map that maps the column name to the id 139 // Create a map that maps the column name to the id
140 QMapConstIterator<int, QString> it; 140 QMapConstIterator<int, QString> it;
141 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 141 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
142 m_reverseFieldMap.insert( it.data(), it.key() ); 142 m_reverseFieldMap.insert( it.data(), it.key() );
143 } 143 }
144 144
145} 145}
146 146
147bool ODateBookAccessBackend_SQL::load() 147bool ODateBookAccessBackend_SQL::load()
148{ 148{
149 if (!m_driver->open() ) 149 if (!m_driver->open() )
150 return false; 150 return false;
151 151
152 // Don't expect that the database exists. 152 // Don't expect that the database exists.
153 // It is save here to create the table, even if it 153 // It is save here to create the table, even if it
154 // do exist. ( Is that correct for all databases ?? ) 154 // do exist. ( Is that correct for all databases ?? )
155 QStringqu = "create table datebook( uid INTEGER PRIMARY KEY "; 155 QStringqu = "create table datebook( uid INTEGER PRIMARY KEY ";
156 156
157 QMap<int, QString>::Iterator it; 157 QMap<int, QString>::Iterator it;
158 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 158 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
159 qu += QString( ",%1 VARCHAR(10)" ).arg( it.data() ); 159 qu += QString( ",%1 VARCHAR(10)" ).arg( it.data() );
160 } 160 }
161 qu += " );"; 161 qu += " );";
162 162
163 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );"; 163 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );";
164 164
165 qWarning( "command: %s", qu.latin1() ); 165 qWarning( "command: %s", qu.latin1() );
166 166
167 OSQLRawQuery raw( qu ); 167 OSQLRawQuery raw( qu );
168 OSQLResult res = m_driver->query( &raw ); 168 OSQLResult res = m_driver->query( &raw );
169 if ( res.state() != OSQLResult::Success ) 169 if ( res.state() != OSQLResult::Success )
170 return false; 170 return false;
171 171
172 update(); 172 update();
173 173
174 return true; 174 return true;
175} 175}
176 176
177void ODateBookAccessBackend_SQL::update() 177void ODateBookAccessBackend_SQL::update()
178{ 178{
179 179
180 QString qu = "select uid from datebook"; 180 QString qu = "select uid from datebook";
181 OSQLRawQuery raw( qu ); 181 OSQLRawQuery raw( qu );
182 OSQLResult res = m_driver->query( &raw ); 182 OSQLResult res = m_driver->query( &raw );
183 if ( res.state() != OSQLResult::Success ){ 183 if ( res.state() != OSQLResult::Success ){
184 // m_uids.clear(); 184 // m_uids.clear();
185 return; 185 return;
186 } 186 }
187 187
188 m_uids = extractUids( res ); 188 m_uids = extractUids( res );
189 189
190} 190}
191 191
192bool ODateBookAccessBackend_SQL::reload() 192bool ODateBookAccessBackend_SQL::reload()
193{ 193{
194 return load(); 194 return load();
195} 195}
196 196
197bool ODateBookAccessBackend_SQL::save() 197bool ODateBookAccessBackend_SQL::save()
198{ 198{
199 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) 199 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers)
200} 200}
201 201
202QArray<int> ODateBookAccessBackend_SQL::allRecords()const 202QArray<int> ODateBookAccessBackend_SQL::allRecords()const
203{ 203{
204 return m_uids; 204 return m_uids;
205} 205}
206 206
207QArray<int> ODateBookAccessBackend_SQL::queryByExample(const OPimEvent&, int, const QDateTime& ) { 207QArray<int> ODateBookAccessBackend_SQL::queryByExample(const OPimEvent&, int, const QDateTime& ) {
208 return QArray<int>(); 208 return QArray<int>();
209} 209}
210 210
211void ODateBookAccessBackend_SQL::clear() 211void ODateBookAccessBackend_SQL::clear()
212{ 212{
213 QString qu = "drop table datebook;"; 213 QString qu = "drop table datebook;";
214 qu += "drop table custom_data;"; 214 qu += "drop table custom_data;";
215 215
216 OSQLRawQuery raw( qu ); 216 OSQLRawQuery raw( qu );
217 OSQLResult res = m_driver->query( &raw ); 217 OSQLResult res = m_driver->query( &raw );
218 218
219 reload(); 219 reload();
220} 220}
221 221
222 222
223OPimEvent ODateBookAccessBackend_SQL::find( int uid ) const{ 223OPimEvent ODateBookAccessBackend_SQL::find( int uid ) const{
224 qDebug( "ODateBookAccessBackend_SQL::find( %d )", uid );
225
224 QString qu = "select *"; 226 QString qu = "select *";
225 qu += "from datebook where uid = " + QString::number(uid); 227 qu += "from datebook where uid = " + QString::number(uid);
226 228
227 OSQLRawQuery raw( qu ); 229 OSQLRawQuery raw( qu );
228 OSQLResult res = m_driver->query( &raw ); 230 OSQLResult res = m_driver->query( &raw );
229 231
230 OSQLResultItem resItem = res.first(); 232 OSQLResultItem resItem = res.first();
231 233
232 // Create Map for date event and insert UID 234 // Create Map for date event and insert UID
233 QMap<int,QString> dateEventMap; 235 QMap<int,QString> dateEventMap;
234 dateEventMap.insert( OPimEvent::FUid, QString::number( uid ) ); 236 dateEventMap.insert( OPimEvent::FUid, QString::number( uid ) );
235 237
236 // Now insert the data out of the columns into the map. 238 // Now insert the data out of the columns into the map.
237 QMapConstIterator<int, QString> it; 239 QMapConstIterator<int, QString> it;
238 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 240 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
239 dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) ); 241 dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) );
240 } 242 }
241 243
242 // Last step: Put map into date event, add custom map and return it 244 // Last step: Put map into date event, add custom map and return it
243 OPimEvent retDate( dateEventMap ); 245 OPimEvent retDate( dateEventMap );
244 retDate.setExtraMap( requestCustom( uid ) ); 246 retDate.setExtraMap( requestCustom( uid ) );
247
248 qDebug( "ODateBookAccessBackend_SQL::find() end", uid );
245 return retDate; 249 return retDate;
246} 250}
247 251
248// FIXME: Speed up update of uid's.. 252// FIXME: Speed up update of uid's..
249bool ODateBookAccessBackend_SQL::add( const OPimEvent& ev ) 253bool ODateBookAccessBackend_SQL::add( const OPimEvent& ev )
250{ 254{
251 QMap<int,QString> eventMap = ev.toMap(); 255 QMap<int,QString> eventMap = ev.toMap();
252 256
253 QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() ); 257 QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() );
254 QMap<int, QString>::Iterator it; 258 QMap<int, QString>::Iterator it;
255 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ 259 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
256 if ( !eventMap[it.key()].isEmpty() ) 260 if ( !eventMap[it.key()].isEmpty() )
257 qu += QString( ",\"%1\"" ).arg( eventMap[it.key()] ); 261 qu += QString( ",\"%1\"" ).arg( eventMap[it.key()] );
258 else 262 else
259 qu += QString( ",\"\"" ); 263 qu += QString( ",\"\"" );
260 } 264 }
261 qu += " );"; 265 qu += " );";
262 266
263 // Add custom entries 267 // Add custom entries
264 int id = 0; 268 int id = 0;
265 QMap<QString, QString> customMap = ev.toExtraMap(); 269 QMap<QString, QString> customMap = ev.toExtraMap();
266 for( QMap<QString, QString>::Iterator it = customMap.begin(); 270 for( QMap<QString, QString>::Iterator it = customMap.begin();
267 it != customMap.end(); ++it ){ 271 it != customMap.end(); ++it ){
268 qu += "insert into custom_data VALUES(" 272 qu += "insert into custom_data VALUES("
269 + QString::number( ev.uid() ) 273 + QString::number( ev.uid() )
270 + "," 274 + ","
271 + QString::number( id++ ) 275 + QString::number( id++ )
272 + ",'" 276 + ",'"
273 + it.key() //.latin1() 277 + it.key() //.latin1()
274 + "'," 278 + "',"
275 + "0" // Priority for future enhancements 279 + "0" // Priority for future enhancements
276 + ",'" 280 + ",'"
277 + it.data() //.latin1() 281 + it.data() //.latin1()
278 + "');"; 282 + "');";
279 } 283 }
280 qWarning("add %s", qu.latin1() ); 284 qWarning("add %s", qu.latin1() );
281 285
282 OSQLRawQuery raw( qu ); 286 OSQLRawQuery raw( qu );
283 OSQLResult res = m_driver->query( &raw ); 287 OSQLResult res = m_driver->query( &raw );
284 if ( res.state() != OSQLResult::Success ){ 288 if ( res.state() != OSQLResult::Success ){
285 return false; 289 return false;
286 } 290 }
287 291
288 // Update list of uid's 292 // Update list of uid's
289 update(); 293 update();
290 294
291 return true; 295 return true;
292} 296}
293 297
294// FIXME: Speed up update of uid's.. 298// FIXME: Speed up update of uid's..
295bool ODateBookAccessBackend_SQL::remove( int uid ) 299bool ODateBookAccessBackend_SQL::remove( int uid )
296{ 300{
297 QString qu = "DELETE from datebook where uid = " 301 QString qu = "DELETE from datebook where uid = "
298 + QString::number( uid ) + ";"; 302 + QString::number( uid ) + ";";
299 qu += "DELETE from custom_data where uid = " 303 qu += "DELETE from custom_data where uid = "
300 + QString::number( uid ) + ";"; 304 + QString::number( uid ) + ";";
301 305
302 OSQLRawQuery raw( qu ); 306 OSQLRawQuery raw( qu );
303 OSQLResult res = m_driver->query( &raw ); 307 OSQLResult res = m_driver->query( &raw );
304 if ( res.state() != OSQLResult::Success ){ 308 if ( res.state() != OSQLResult::Success ){
305 return false; 309 return false;
306 } 310 }
307 311
308 // Update list of uid's 312 // Update list of uid's
309 update(); 313 update();
310 314
311 return true; 315 return true;
312} 316}
313 317
314bool ODateBookAccessBackend_SQL::replace( const OPimEvent& ev ) 318bool ODateBookAccessBackend_SQL::replace( const OPimEvent& ev )
315{ 319{
316 remove( ev.uid() ); 320 remove( ev.uid() );
317 return add( ev ); 321 return add( ev );
318} 322}
319 323
320QArray<int> ODateBookAccessBackend_SQL::rawEvents()const 324QArray<int> ODateBookAccessBackend_SQL::rawEvents()const
321{ 325{
322 return allRecords(); 326 return allRecords();
323} 327}
324 328
325QArray<int> ODateBookAccessBackend_SQL::rawRepeats()const 329QArray<int> ODateBookAccessBackend_SQL::rawRepeats()const
326{ 330{
327 QString qu = "select uid from datebook where RType!=\"\" AND RType!=\"NoRepeat\""; 331 QString qu = "select uid from datebook where RType!=\"\" AND RType!=\"NoRepeat\"";
328 OSQLRawQuery raw( qu ); 332 OSQLRawQuery raw( qu );
329 OSQLResult res = m_driver->query( &raw ); 333 OSQLResult res = m_driver->query( &raw );
330 if ( res.state() != OSQLResult::Success ){ 334 if ( res.state() != OSQLResult::Success ){
331 QArray<int> nix; 335 QArray<int> nix;
332 return nix; 336 return nix;
333 } 337 }
334 338
335 return extractUids( res ); 339 return extractUids( res );
336} 340}
337 341
338QArray<int> ODateBookAccessBackend_SQL::nonRepeats()const 342QArray<int> ODateBookAccessBackend_SQL::nonRepeats()const
339{ 343{
340 QString qu = "select uid from datebook where RType=\"\" or RType=\"NoRepeat\""; 344 QString qu = "select uid from datebook where RType=\"\" or RType=\"NoRepeat\"";
341 OSQLRawQuery raw( qu ); 345 OSQLRawQuery raw( qu );
342 OSQLResult res = m_driver->query( &raw ); 346 OSQLResult res = m_driver->query( &raw );
343 if ( res.state() != OSQLResult::Success ){ 347 if ( res.state() != OSQLResult::Success ){
344 QArray<int> nix; 348 QArray<int> nix;
345 return nix; 349 return nix;
346 } 350 }
347 351
348 return extractUids( res ); 352 return extractUids( res );
349} 353}
350 354
351OPimEvent::ValueList ODateBookAccessBackend_SQL::directNonRepeats() 355OPimEvent::ValueList ODateBookAccessBackend_SQL::directNonRepeats()
352{ 356{
353 QArray<int> nonRepUids = nonRepeats(); 357 QArray<int> nonRepUids = nonRepeats();
354 OPimEvent::ValueList list; 358 OPimEvent::ValueList list;
355 359
356 for (uint i = 0; i < nonRepUids.count(); ++i ){ 360 for (uint i = 0; i < nonRepUids.count(); ++i ){
357 list.append( find( nonRepUids[i] ) ); 361 list.append( find( nonRepUids[i] ) );
358 } 362 }
359 363
360 return list; 364 return list;
361 365
362} 366}
363OPimEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats() 367OPimEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats()
364{ 368{
365 QArray<int> rawRepUids = rawRepeats(); 369 QArray<int> rawRepUids = rawRepeats();
366 OPimEvent::ValueList list; 370 OPimEvent::ValueList list;
367 371
368 for (uint i = 0; i < rawRepUids.count(); ++i ){ 372 for (uint i = 0; i < rawRepUids.count(); ++i ){
369 list.append( find( rawRepUids[i] ) ); 373 list.append( find( rawRepUids[i] ) );
370 } 374 }
371 375
372 return list; 376 return list;
373} 377}
374 378
375 379
376QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const 380QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const
377{ 381{
378 QArray<int> null; 382
379 return null; 383 QString qu = "SELECT uid FROM datebook WHERE (";
384
385 // Do it make sense to search other fields, too ?
386 qu += " rlike(\""+ r.pattern() + "\", Location ) OR";
387 qu += " rlike(\""+ r.pattern() + "\", Note )";
388
389 qu += " )";
390
391 qDebug( "query: %s", qu.latin1() );
392
393 OSQLRawQuery raw( qu );
394 OSQLResult res = m_driver->query( &raw );
395
396 return extractUids( res );
397
398
399
380} 400}
381 401
382/* ===== Private Functions ========================================== */ 402/* ===== Private Functions ========================================== */
383 403
384QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const 404QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const
385{ 405{
386 qWarning("extractUids"); 406 qWarning("extractUids");
387 QTime t; 407 QTime t;
388 t.start(); 408 t.start();
389 OSQLResultItem::ValueList list = res.results(); 409 OSQLResultItem::ValueList list = res.results();
390 OSQLResultItem::ValueList::Iterator it; 410 OSQLResultItem::ValueList::Iterator it;
391 QArray<int> ints(list.count() ); 411 QArray<int> ints(list.count() );
392 qWarning(" count = %d", list.count() ); 412 qWarning(" count = %d", list.count() );
393 413
394 int i = 0; 414 int i = 0;
395 for (it = list.begin(); it != list.end(); ++it ) { 415 for (it = list.begin(); it != list.end(); ++it ) {
396 ints[i] = (*it).data("uid").toInt(); 416 ints[i] = (*it).data("uid").toInt();
397 i++; 417 i++;
398 } 418 }
399 qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() ); 419 qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() );
400 420
401 return ints; 421 return ints;
402 422
403} 423}
404 424
405QMap<QString, QString> ODateBookAccessBackend_SQL::requestCustom( int uid ) const 425QMap<QString, QString> ODateBookAccessBackend_SQL::requestCustom( int uid ) const
406{ 426{
407 QTime t; 427 QTime t;
408 t.start(); 428 t.start();
409 429
410 QMap<QString, QString> customMap; 430 QMap<QString, QString> customMap;
411 431
412 FindCustomQuery query( uid ); 432 FindCustomQuery query( uid );
413 OSQLResult res_custom = m_driver->query( &query ); 433 OSQLResult res_custom = m_driver->query( &query );
414 434
415 if ( res_custom.state() == OSQLResult::Failure ) { 435 if ( res_custom.state() == OSQLResult::Failure ) {
416 qWarning("OSQLResult::Failure in find query !!"); 436 qWarning("OSQLResult::Failure in find query !!");
417 QMap<QString, QString> empty; 437 QMap<QString, QString> empty;
418 return empty; 438 return empty;
419 } 439 }
420 440
421 OSQLResultItem::ValueList list = res_custom.results(); 441 OSQLResultItem::ValueList list = res_custom.results();
422 OSQLResultItem::ValueList::Iterator it = list.begin(); 442 OSQLResultItem::ValueList::Iterator it = list.begin();
423 for ( ; it != list.end(); ++it ) { 443 for ( ; it != list.end(); ++it ) {
424 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); 444 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) );
425 } 445 }
426 446
427 qDebug("RequestCustom needed: %d ms", t.elapsed() ); 447 qDebug("RequestCustom needed: %d ms", t.elapsed() );
428 return customMap; 448 return customMap;
429} 449}
430 450
431 451
432} 452}
diff --git a/libopie2/opiepim/backend/otodoaccesssql.cpp b/libopie2/opiepim/backend/otodoaccesssql.cpp
index b4170fc..ef036d5 100644
--- a/libopie2/opiepim/backend/otodoaccesssql.cpp
+++ b/libopie2/opiepim/backend/otodoaccesssql.cpp
@@ -1,839 +1,880 @@
1/* 1/*
2 This file is part of the Opie Project 2 This file is part of the Opie Project
3 Copyright (C) Stefan Eilers (Eilers.Stefan@epost.de) 3 Copyright (C) Stefan Eilers (Eilers.Stefan@epost.de)
4 Copyright (C) Holger Freyther (zecke@handhelds.org) 4 Copyright (C) Holger Freyther (zecke@handhelds.org)
5 =. Copyright (C) The Opie Team <opie-devel@handhelds.org> 5 =. Copyright (C) The Opie Team <opie-devel@handhelds.org>
6 .=l. 6 .=l.
7 .>+-= 7 .>+-=
8 _;:, .> :=|. This program is free software; you can 8 _;:, .> :=|. This program is free software; you can
9.> <`_, > . <= redistribute it and/or modify it under 9.> <`_, > . <= redistribute it and/or modify it under
10:`=1 )Y*s>-.-- : the terms of the GNU Library General Public 10:`=1 )Y*s>-.-- : the terms of the GNU Library General Public
11.="- .-=="i, .._ License as published by the Free Software 11.="- .-=="i, .._ License as published by the Free Software
12 - . .-<_> .<> Foundation; either version 2 of the License, 12 - . .-<_> .<> Foundation; either version 2 of the License,
13 ._= =} : or (at your option) any later version. 13 ._= =} : or (at your option) any later version.
14 .%`+i> _;_. 14 .%`+i> _;_.
15 .i_,=:_. -<s. This program is distributed in the hope that 15 .i_,=:_. -<s. This program is distributed in the hope that
16 + . -:. = it will be useful, but WITHOUT ANY WARRANTY; 16 + . -:. = it will be useful, but WITHOUT ANY WARRANTY;
17 : .. .:, . . . without even the implied warranty of 17 : .. .:, . . . without even the implied warranty of
18 =_ + =;=|` MERCHANTABILITY or FITNESS FOR A 18 =_ + =;=|` MERCHANTABILITY or FITNESS FOR A
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#include <qdatetime.h>
32#include <qmap.h> 32#include <qmap.h>
33#include <qstring.h> 33#include <qstring.h>
34 34
35#include <qpe/global.h> 35#include <qpe/global.h>
36 36
37#include <opie2/osqldriver.h> 37#include <opie2/osqldriver.h>
38#include <opie2/osqlresult.h> 38#include <opie2/osqlresult.h>
39#include <opie2/osqlmanager.h> 39#include <opie2/osqlmanager.h>
40#include <opie2/osqlquery.h> 40#include <opie2/osqlquery.h>
41 41
42#include <opie2/otodoaccesssql.h> 42#include <opie2/otodoaccesssql.h>
43#include <opie2/opimstate.h> 43#include <opie2/opimstate.h>
44#include <opie2/opimnotifymanager.h> 44#include <opie2/opimnotifymanager.h>
45#include <opie2/opimrecurrence.h> 45#include <opie2/opimrecurrence.h>
46 46
47using namespace Opie::DB; 47using namespace Opie::DB;
48 48
49using namespace Opie; 49using namespace Opie;
50/* 50/*
51 * first some query 51 * first some query
52 * CREATE query 52 * CREATE query
53 * LOAD query 53 * LOAD query
54 * INSERT 54 * INSERT
55 * REMOVE 55 * REMOVE
56 * CLEAR 56 * CLEAR
57 */ 57 */
58namespace { 58namespace {
59 /** 59 /**
60 * CreateQuery for the Todolist Table 60 * CreateQuery for the Todolist Table
61 */ 61 */
62 class CreateQuery : public OSQLQuery { 62 class CreateQuery : public OSQLQuery {
63 public: 63 public:
64 CreateQuery(); 64 CreateQuery();
65 ~CreateQuery(); 65 ~CreateQuery();
66 QString query()const; 66 QString query()const;
67 }; 67 };
68 68
69 /** 69 /**
70 * LoadQuery 70 * LoadQuery
71 * this one queries for all uids 71 * this one queries for all uids
72 */ 72 */
73 class LoadQuery : public OSQLQuery { 73 class LoadQuery : public OSQLQuery {
74 public: 74 public:
75 LoadQuery(); 75 LoadQuery();
76 ~LoadQuery(); 76 ~LoadQuery();
77 QString query()const; 77 QString query()const;
78 }; 78 };
79 79
80 /** 80 /**
81 * inserts/adds a OPimTodo to the table 81 * inserts/adds a OPimTodo to the table
82 */ 82 */
83 class InsertQuery : public OSQLQuery { 83 class InsertQuery : public OSQLQuery {
84 public: 84 public:
85 InsertQuery(const OPimTodo& ); 85 InsertQuery(const OPimTodo& );
86 ~InsertQuery(); 86 ~InsertQuery();
87 QString query()const; 87 QString query()const;
88 private: 88 private:
89 OPimTodo m_todo; 89 OPimTodo m_todo;
90 }; 90 };
91 91
92 /** 92 /**
93 * removes one from the table 93 * removes one from the table
94 */ 94 */
95 class RemoveQuery : public OSQLQuery { 95 class RemoveQuery : public OSQLQuery {
96 public: 96 public:
97 RemoveQuery(int uid ); 97 RemoveQuery(int uid );
98 ~RemoveQuery(); 98 ~RemoveQuery();
99 QString query()const; 99 QString query()const;
100 private: 100 private:
101 int m_uid; 101 int m_uid;
102 }; 102 };
103 103
104 /** 104 /**
105 * Clears (delete) a Table 105 * Clears (delete) a Table
106 */ 106 */
107 class ClearQuery : public OSQLQuery { 107 class ClearQuery : public OSQLQuery {
108 public: 108 public:
109 ClearQuery(); 109 ClearQuery();
110 ~ClearQuery(); 110 ~ClearQuery();
111 QString query()const; 111 QString query()const;
112 112
113 }; 113 };
114 114
115 /** 115 /**
116 * a find query 116 * a find query
117 */ 117 */
118 class FindQuery : public OSQLQuery { 118 class FindQuery : public OSQLQuery {
119 public: 119 public:
120 FindQuery(int uid); 120 FindQuery(int uid);
121 FindQuery(const QArray<int>& ); 121 FindQuery(const QArray<int>& );
122 ~FindQuery(); 122 ~FindQuery();
123 QString query()const; 123 QString query()const;
124 private: 124 private:
125 QString single()const; 125 QString single()const;
126 QString multi()const; 126 QString multi()const;
127 QArray<int> m_uids; 127 QArray<int> m_uids;
128 int m_uid; 128 int m_uid;
129 }; 129 };
130 130
131 /** 131 /**
132 * overdue query 132 * overdue query
133 */ 133 */
134 class OverDueQuery : public OSQLQuery { 134 class OverDueQuery : public OSQLQuery {
135 public: 135 public:
136 OverDueQuery(); 136 OverDueQuery();
137 ~OverDueQuery(); 137 ~OverDueQuery();
138 QString query()const; 138 QString query()const;
139 }; 139 };
140 class EffQuery : public OSQLQuery { 140 class EffQuery : public OSQLQuery {
141 public: 141 public:
142 EffQuery( const QDate&, const QDate&, bool inc ); 142 EffQuery( const QDate&, const QDate&, bool inc );
143 ~EffQuery(); 143 ~EffQuery();
144 QString query()const; 144 QString query()const;
145 private: 145 private:
146 QString with()const; 146 QString with()const;
147 QString out()const; 147 QString out()const;
148 QDate m_start; 148 QDate m_start;
149 QDate m_end; 149 QDate m_end;
150 bool m_inc :1; 150 bool m_inc :1;
151 }; 151 };
152 152
153 153
154 /** 154 /**
155 * a find query for custom elements 155 * a find query for custom elements
156 */ 156 */
157 class FindCustomQuery : public OSQLQuery { 157 class FindCustomQuery : public OSQLQuery {
158 public: 158 public:
159 FindCustomQuery(int uid); 159 FindCustomQuery(int uid);
160 FindCustomQuery(const QArray<int>& ); 160 FindCustomQuery(const QArray<int>& );
161 ~FindCustomQuery(); 161 ~FindCustomQuery();
162 QString query()const; 162 QString query()const;
163 private: 163 private:
164 QString single()const; 164 QString single()const;
165 QString multi()const; 165 QString multi()const;
166 QArray<int> m_uids; 166 QArray<int> m_uids;
167 int m_uid; 167 int m_uid;
168 }; 168 };
169 169
170 170
171 171
172 CreateQuery::CreateQuery() : OSQLQuery() {} 172 CreateQuery::CreateQuery() : OSQLQuery() {}
173 CreateQuery::~CreateQuery() {} 173 CreateQuery::~CreateQuery() {}
174 QString CreateQuery::query()const { 174 QString CreateQuery::query()const {
175 QString qu; 175 QString qu;
176 qu += "create table todolist( uid PRIMARY KEY, categories, completed, "; 176 qu += "create table todolist( uid PRIMARY KEY, categories, completed, ";
177 qu += "description, summary, priority, DueDate, progress , state, "; 177 qu += "description, summary, priority, DueDate, progress , state, ";
178 // This is the recurrance-stuff .. Exceptions are currently not supported (see OPimRecurrence.cpp) ! (eilers) 178 // This is the recurrance-stuff .. Exceptions are currently not supported (see OPimRecurrence.cpp) ! (eilers)
179 qu += "RType, RWeekdays, RPosition, RFreq, RHasEndDate, EndDate, Created, Exceptions, "; 179 qu += "RType, RWeekdays, RPosition, RFreq, RHasEndDate, EndDate, Created, Exceptions, ";
180 qu += "reminders, alarms, maintainer, startdate, completeddate);"; 180 qu += "reminders, alarms, maintainer, startdate, completeddate);";
181 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );"; 181 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );";
182 return qu; 182 return qu;
183 } 183 }
184 184
185 LoadQuery::LoadQuery() : OSQLQuery() {} 185 LoadQuery::LoadQuery() : OSQLQuery() {}
186 LoadQuery::~LoadQuery() {} 186 LoadQuery::~LoadQuery() {}
187 QString LoadQuery::query()const { 187 QString LoadQuery::query()const {
188 QString qu; 188 QString qu;
189 // We do not need "distinct" here. The primary key is always unique.. 189 // We do not need "distinct" here. The primary key is always unique..
190 //qu += "select distinct uid from todolist"; 190 //qu += "select distinct uid from todolist";
191 qu += "select uid from todolist"; 191 qu += "select uid from todolist";
192 192
193 return qu; 193 return qu;
194 } 194 }
195 195
196 InsertQuery::InsertQuery( const OPimTodo& todo ) 196 InsertQuery::InsertQuery( const OPimTodo& todo )
197 : OSQLQuery(), m_todo( todo ) { 197 : OSQLQuery(), m_todo( todo ) {
198 } 198 }
199 InsertQuery::~InsertQuery() { 199 InsertQuery::~InsertQuery() {
200 } 200 }
201 /* 201 /*
202 * converts from a OPimTodo to a query 202 * converts from a OPimTodo to a query
203 * we leave out X-Ref + Maintainer 203 * we leave out X-Ref + Maintainer
204 * FIXME: Implement/Finish toMap()/fromMap() into OpimTodo to move the encoding 204 * FIXME: Implement/Finish toMap()/fromMap() into OpimTodo to move the encoding
205 * decoding stuff there.. (eilers) 205 * decoding stuff there.. (eilers)
206 */ 206 */
207 QString InsertQuery::query()const{ 207 QString InsertQuery::query()const{
208 208
209 int year, month, day; 209 int year, month, day;
210 year = month = day = 0; 210 year = month = day = 0;
211 if (m_todo.hasDueDate() ) { 211 if (m_todo.hasDueDate() ) {
212 QDate date = m_todo.dueDate(); 212 QDate date = m_todo.dueDate();
213 year = date.year(); 213 year = date.year();
214 month = date.month(); 214 month = date.month();
215 day = date.day(); 215 day = date.day();
216 } 216 }
217 int sYear = 0, sMonth = 0, sDay = 0; 217 int sYear = 0, sMonth = 0, sDay = 0;
218 if( m_todo.hasStartDate() ){ 218 if( m_todo.hasStartDate() ){
219 QDate sDate = m_todo.startDate(); 219 QDate sDate = m_todo.startDate();
220 sYear = sDate.year(); 220 sYear = sDate.year();
221 sMonth= sDate.month(); 221 sMonth= sDate.month();
222 sDay = sDate.day(); 222 sDay = sDate.day();
223 } 223 }
224 224
225 int eYear = 0, eMonth = 0, eDay = 0; 225 int eYear = 0, eMonth = 0, eDay = 0;
226 if( m_todo.hasCompletedDate() ){ 226 if( m_todo.hasCompletedDate() ){
227 QDate eDate = m_todo.completedDate(); 227 QDate eDate = m_todo.completedDate();
228 eYear = eDate.year(); 228 eYear = eDate.year();
229 eMonth= eDate.month(); 229 eMonth= eDate.month();
230 eDay = eDate.day(); 230 eDay = eDate.day();
231 } 231 }
232 QString qu; 232 QString qu;
233 QMap<int, QString> recMap = m_todo.recurrence().toMap(); 233 QMap<int, QString> recMap = m_todo.recurrence().toMap();
234 qu = "insert into todolist VALUES(" 234 qu = "insert into todolist VALUES("
235 + QString::number( m_todo.uid() ) + "," 235 + QString::number( m_todo.uid() ) + ","
236 + "'" + m_todo.idsToString( m_todo.categories() ) + "'" + "," 236 + "'" + m_todo.idsToString( m_todo.categories() ) + "'" + ","
237 + QString::number( m_todo.isCompleted() ) + "," 237 + QString::number( m_todo.isCompleted() ) + ","
238 + "'" + m_todo.description() + "'" + "," 238 + "'" + m_todo.description() + "'" + ","
239 + "'" + m_todo.summary() + "'" + "," 239 + "'" + m_todo.summary() + "'" + ","
240 + QString::number(m_todo.priority() ) + "," 240 + QString::number(m_todo.priority() ) + ","
241 + "'" + QString::number(year).rightJustify( 4, '0' ) + "-" 241 + "'" + QString::number(year).rightJustify( 4, '0' ) + "-"
242 + QString::number(month).rightJustify( 2, '0' ) 242 + QString::number(month).rightJustify( 2, '0' )
243 + "-" + QString::number( day ).rightJustify( 2, '0' )+ "'" + "," 243 + "-" + QString::number( day ).rightJustify( 2, '0' )+ "'" + ","
244 + QString::number( m_todo.progress() ) + "," 244 + QString::number( m_todo.progress() ) + ","
245 + QString::number( m_todo.state().state() ) + "," 245 + QString::number( m_todo.state().state() ) + ","
246 + "'" + recMap[ OPimRecurrence::RType ] + "'" + "," 246 + "'" + recMap[ OPimRecurrence::RType ] + "'" + ","
247 + "'" + recMap[ OPimRecurrence::RWeekdays ] + "'" + "," 247 + "'" + recMap[ OPimRecurrence::RWeekdays ] + "'" + ","
248 + "'" + recMap[ OPimRecurrence::RPosition ] + "'" + "," 248 + "'" + recMap[ OPimRecurrence::RPosition ] + "'" + ","
249 + "'" + recMap[ OPimRecurrence::RFreq ] + "'" + "," 249 + "'" + recMap[ OPimRecurrence::RFreq ] + "'" + ","
250 + "'" + recMap[ OPimRecurrence::RHasEndDate ] + "'" + "," 250 + "'" + recMap[ OPimRecurrence::RHasEndDate ] + "'" + ","
251 + "'" + recMap[ OPimRecurrence::EndDate ] + "'" + "," 251 + "'" + recMap[ OPimRecurrence::EndDate ] + "'" + ","
252 + "'" + recMap[ OPimRecurrence::Created ] + "'" + "," 252 + "'" + recMap[ OPimRecurrence::Created ] + "'" + ","
253 + "'" + recMap[ OPimRecurrence::Exceptions ] + "'" + ","; 253 + "'" + recMap[ OPimRecurrence::Exceptions ] + "'" + ",";
254 254
255 if ( m_todo.hasNotifiers() ) { 255 if ( m_todo.hasNotifiers() ) {
256 OPimNotifyManager manager = m_todo.notifiers(); 256 OPimNotifyManager manager = m_todo.notifiers();
257 qu += "'" + manager.remindersToString() + "'" + "," 257 qu += "'" + manager.remindersToString() + "'" + ","
258 + "'" + manager.alarmsToString() + "'" + ","; 258 + "'" + manager.alarmsToString() + "'" + ",";
259 } 259 }
260 else{ 260 else{
261 qu += QString( "''" ) + "," 261 qu += QString( "''" ) + ","
262 + "''" + ","; 262 + "''" + ",";
263 } 263 }
264 264
265 qu += QString( "''" ) + QString( "," ) // Maintainers (cur. not supported !) 265 qu += QString( "''" ) + QString( "," ) // Maintainers (cur. not supported !)
266 + "'" + QString::number(sYear).rightJustify( 4, '0' ) + "-" 266 + "'" + QString::number(sYear).rightJustify( 4, '0' ) + "-"
267 + QString::number(sMonth).rightJustify( 2, '0' ) 267 + QString::number(sMonth).rightJustify( 2, '0' )
268 + "-" + QString::number(sDay).rightJustify( 2, '0' )+ "'" + "," 268 + "-" + QString::number(sDay).rightJustify( 2, '0' )+ "'" + ","
269 + "'" + QString::number(eYear).rightJustify( 4, '0' ) + "-" 269 + "'" + QString::number(eYear).rightJustify( 4, '0' ) + "-"
270 + QString::number(eMonth).rightJustify( 2, '0' ) 270 + QString::number(eMonth).rightJustify( 2, '0' )
271 + "-"+QString::number(eDay).rightJustify( 2, '0' ) + "'" 271 + "-"+QString::number(eDay).rightJustify( 2, '0' ) + "'"
272 + ")"; 272 + ")";
273 273
274 // Save custom Entries: 274 // Save custom Entries:
275 int id = 0; 275 int id = 0;
276 id = 0; 276 id = 0;
277 QMap<QString, QString> customMap = m_todo.toExtraMap(); 277 QMap<QString, QString> customMap = m_todo.toExtraMap();
278 for( QMap<QString, QString>::Iterator it = customMap.begin(); 278 for( QMap<QString, QString>::Iterator it = customMap.begin();
279 it != customMap.end(); ++it ){ 279 it != customMap.end(); ++it ){
280 qu += "insert into custom_data VALUES(" 280 qu += "insert into custom_data VALUES("
281 + QString::number( m_todo.uid() ) 281 + QString::number( m_todo.uid() )
282 + "," 282 + ","
283 + QString::number( id++ ) 283 + QString::number( id++ )
284 + ",'" 284 + ",'"
285 + it.key() 285 + it.key()
286 + "'," 286 + "',"
287 + "0" // Priority for future enhancements 287 + "0" // Priority for future enhancements
288 + ",'" 288 + ",'"
289 + it.data() 289 + it.data()
290 + "');"; 290 + "');";
291 } 291 }
292 292
293 293
294 qDebug("add %s", qu.latin1() ); 294 qDebug("add %s", qu.latin1() );
295 return qu; 295 return qu;
296 } 296 }
297 297
298 RemoveQuery::RemoveQuery(int uid ) 298 RemoveQuery::RemoveQuery(int uid )
299 : OSQLQuery(), m_uid( uid ) {} 299 : OSQLQuery(), m_uid( uid ) {}
300 RemoveQuery::~RemoveQuery() {} 300 RemoveQuery::~RemoveQuery() {}
301 QString RemoveQuery::query()const { 301 QString RemoveQuery::query()const {
302 QString qu = "DELETE from todolist where uid = " + QString::number(m_uid); 302 QString qu = "DELETE FROM todolist WHERE uid = " + QString::number(m_uid) + " ;";
303 qu += "DELETE FROM custom_data WHERE uid = " + QString::number(m_uid);
303 return qu; 304 return qu;
304 } 305 }
305 306
306 307
307 ClearQuery::ClearQuery() 308 ClearQuery::ClearQuery()
308 : OSQLQuery() {} 309 : OSQLQuery() {}
309 ClearQuery::~ClearQuery() {} 310 ClearQuery::~ClearQuery() {}
310 QString ClearQuery::query()const { 311 QString ClearQuery::query()const {
311 QString qu = "drop table todolist"; 312 QString qu = "drop table todolist";
312 return qu; 313 return qu;
313 } 314 }
314 FindQuery::FindQuery(int uid) 315 FindQuery::FindQuery(int uid)
315 : OSQLQuery(), m_uid(uid ) { 316 : OSQLQuery(), m_uid(uid ) {
316 } 317 }
317 FindQuery::FindQuery(const QArray<int>& ints) 318 FindQuery::FindQuery(const QArray<int>& ints)
318 : OSQLQuery(), m_uids(ints){ 319 : OSQLQuery(), m_uids(ints){
319 } 320 }
320 FindQuery::~FindQuery() { 321 FindQuery::~FindQuery() {
321 } 322 }
322 QString FindQuery::query()const{ 323 QString FindQuery::query()const{
323 if (m_uids.count() == 0 ) 324 if (m_uids.count() == 0 )
324 return single(); 325 return single();
325 else 326 else
326 return multi(); 327 return multi();
327 } 328 }
328 QString FindQuery::single()const{ 329 QString FindQuery::single()const{
329 QString qu = "select * from todolist where uid = " + QString::number(m_uid); 330 QString qu = "select * from todolist where uid = " + QString::number(m_uid);
330 return qu; 331 return qu;
331 } 332 }
332 QString FindQuery::multi()const { 333 QString FindQuery::multi()const {
333 QString qu = "select * from todolist where "; 334 QString qu = "select * from todolist where ";
334 for (uint i = 0; i < m_uids.count(); i++ ) { 335 for (uint i = 0; i < m_uids.count(); i++ ) {
335 qu += " UID = " + QString::number( m_uids[i] ) + " OR"; 336 qu += " UID = " + QString::number( m_uids[i] ) + " OR";
336 } 337 }
337 qu.remove( qu.length()-2, 2 ); 338 qu.remove( qu.length()-2, 2 );
338 return qu; 339 return qu;
339 } 340 }
340 341
341 OverDueQuery::OverDueQuery(): OSQLQuery() {} 342 OverDueQuery::OverDueQuery(): OSQLQuery() {}
342 OverDueQuery::~OverDueQuery() {} 343 OverDueQuery::~OverDueQuery() {}
343 QString OverDueQuery::query()const { 344 QString OverDueQuery::query()const {
344 QDate date = QDate::currentDate(); 345 QDate date = QDate::currentDate();
345 QString str; 346 QString str;
346 str = QString("select uid from todolist where DueDate ='%1-%2-%3'") 347 str = QString("select uid from todolist where DueDate ='%1-%2-%3'")
347 .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) 348 .arg( QString::number( date.year() ).rightJustify( 4, '0' ) )
348 .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) 349 .arg( QString::number( date.month() ).rightJustify( 2, '0' ) )
349 .arg( QString::number( date.day() ) .rightJustify( 2, '0' ) ); 350 .arg( QString::number( date.day() ) .rightJustify( 2, '0' ) );
350 351
351 return str; 352 return str;
352 } 353 }
353 354
354 355
355 EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc ) 356 EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc )
356 : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {} 357 : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {}
357 EffQuery::~EffQuery() {} 358 EffQuery::~EffQuery() {}
358 QString EffQuery::query()const { 359 QString EffQuery::query()const {
359 return m_inc ? with() : out(); 360 return m_inc ? with() : out();
360 } 361 }
361 QString EffQuery::with()const { 362 QString EffQuery::with()const {
362 QString str; 363 QString str;
363 str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ") 364 str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ")
364 .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) 365 .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) )
365 .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) 366 .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) )
366 .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) 367 .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) )
367 .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) ) 368 .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) )
368 .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) ) 369 .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) )
369 .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) ); 370 .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) );
370 return str; 371 return str;
371 } 372 }
372 QString EffQuery::out()const { 373 QString EffQuery::out()const {
373 QString str; 374 QString str;
374 str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'") 375 str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'")
375 .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) 376 .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) )
376 .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) 377 .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) )
377 .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) 378 .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) )
378 .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) ) 379 .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) )
379 .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) ) 380 .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) )
380 .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) ); 381 .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) );
381 382
382 return str; 383 return str;
383 } 384 }
384 385
385 FindCustomQuery::FindCustomQuery(int uid) 386 FindCustomQuery::FindCustomQuery(int uid)
386 : OSQLQuery(), m_uid( uid ) { 387 : OSQLQuery(), m_uid( uid ) {
387 } 388 }
388 FindCustomQuery::FindCustomQuery(const QArray<int>& ints) 389 FindCustomQuery::FindCustomQuery(const QArray<int>& ints)
389 : OSQLQuery(), m_uids( ints ){ 390 : OSQLQuery(), m_uids( ints ){
390 } 391 }
391 FindCustomQuery::~FindCustomQuery() { 392 FindCustomQuery::~FindCustomQuery() {
392 } 393 }
393 QString FindCustomQuery::query()const{ 394 QString FindCustomQuery::query()const{
394 return single(); // Multiple requests not supported ! 395 return single(); // Multiple requests not supported !
395 } 396 }
396 QString FindCustomQuery::single()const{ 397 QString FindCustomQuery::single()const{
397 QString qu = "select uid, type, value from custom_data where uid = "; 398 QString qu = "select uid, type, value from custom_data where uid = ";
398 qu += QString::number(m_uid); 399 qu += QString::number(m_uid);
399 return qu; 400 return qu;
400 } 401 }
401 402
402}; 403};
403 404
404 405
405namespace Opie { 406namespace Opie {
406OPimTodoAccessBackendSQL::OPimTodoAccessBackendSQL( const QString& file ) 407OPimTodoAccessBackendSQL::OPimTodoAccessBackendSQL( const QString& file )
407 : OPimTodoAccessBackend(),/* m_dict(15),*/ m_driver(NULL), m_dirty(true) 408 : OPimTodoAccessBackend(),/* m_dict(15),*/ m_driver(NULL), m_dirty(true)
408{ 409{
409 QString fi = file; 410 QString fi = file;
410 if ( fi.isEmpty() ) 411 if ( fi.isEmpty() )
411 fi = Global::applicationFileName( "todolist", "todolist.db" ); 412 fi = Global::applicationFileName( "todolist", "todolist.db" );
412 OSQLManager man; 413 OSQLManager man;
413 m_driver = man.standard(); 414 m_driver = man.standard();
414 m_driver->setUrl(fi); 415 m_driver->setUrl(fi);
415 // fillDict(); 416 // fillDict();
416} 417}
417 418
418OPimTodoAccessBackendSQL::~OPimTodoAccessBackendSQL(){ 419OPimTodoAccessBackendSQL::~OPimTodoAccessBackendSQL(){
419 if( m_driver ) 420 if( m_driver )
420 delete m_driver; 421 delete m_driver;
421} 422}
422 423
423bool OPimTodoAccessBackendSQL::load(){ 424bool OPimTodoAccessBackendSQL::load(){
424 if (!m_driver->open() ) 425 if (!m_driver->open() )
425 return false; 426 return false;
426 427
427 CreateQuery creat; 428 CreateQuery creat;
428 OSQLResult res = m_driver->query(&creat ); 429 OSQLResult res = m_driver->query(&creat );
429 430
430 m_dirty = true; 431 m_dirty = true;
431 return true; 432 return true;
432} 433}
433bool OPimTodoAccessBackendSQL::reload(){ 434bool OPimTodoAccessBackendSQL::reload(){
434 return load(); 435 return load();
435} 436}
436 437
437bool OPimTodoAccessBackendSQL::save(){ 438bool OPimTodoAccessBackendSQL::save(){
438 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) 439 return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers)
439} 440}
440QArray<int> OPimTodoAccessBackendSQL::allRecords()const { 441QArray<int> OPimTodoAccessBackendSQL::allRecords()const {
441 if (m_dirty ) 442 if (m_dirty )
442 update(); 443 update();
443 444
444 return m_uids; 445 return m_uids;
445} 446}
446QArray<int> OPimTodoAccessBackendSQL::queryByExample( const OPimTodo& , int, const QDateTime& ){ 447QArray<int> OPimTodoAccessBackendSQL::queryByExample( const OPimTodo& , int, const QDateTime& ){
447 QArray<int> ints(0); 448 QArray<int> ints(0);
448 return ints; 449 return ints;
449} 450}
450OPimTodo OPimTodoAccessBackendSQL::find(int uid ) const{ 451OPimTodo OPimTodoAccessBackendSQL::find(int uid ) const{
451 FindQuery query( uid ); 452 FindQuery query( uid );
452 return todo( m_driver->query(&query) ); 453 return todo( m_driver->query(&query) );
453 454
454} 455}
455OPimTodo OPimTodoAccessBackendSQL::find( int uid, const QArray<int>& ints, 456OPimTodo OPimTodoAccessBackendSQL::find( int uid, const QArray<int>& ints,
456 uint cur, Frontend::CacheDirection dir ) const{ 457 uint cur, Frontend::CacheDirection dir ) const{
457 uint CACHE = readAhead(); 458 uint CACHE = readAhead();
458 qDebug("searching for %d", uid ); 459 qDebug("searching for %d", uid );
459 QArray<int> search( CACHE ); 460 QArray<int> search( CACHE );
460 uint size =0; 461 uint size =0;
461 OPimTodo to; 462 OPimTodo to;
462 463
463 // we try to cache CACHE items 464 // we try to cache CACHE items
464 switch( dir ) { 465 switch( dir ) {
465 /* forward */ 466 /* forward */
466 case 0: // FIXME: Not a good style to use magic numbers here (eilers) 467 case 0: // FIXME: Not a good style to use magic numbers here (eilers)
467 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) { 468 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) {
468 qDebug("size %d %d", size, ints[i] ); 469 qDebug("size %d %d", size, ints[i] );
469 search[size] = ints[i]; 470 search[size] = ints[i];
470 size++; 471 size++;
471 } 472 }
472 break; 473 break;
473 /* reverse */ 474 /* reverse */
474 case 1: // FIXME: Not a good style to use magic numbers here (eilers) 475 case 1: // FIXME: Not a good style to use magic numbers here (eilers)
475 for (uint i = cur; i != 0 && size < CACHE; i-- ) { 476 for (uint i = cur; i != 0 && size < CACHE; i-- ) {
476 search[size] = ints[i]; 477 search[size] = ints[i];
477 size++; 478 size++;
478 } 479 }
479 break; 480 break;
480 } 481 }
481 search.resize( size ); 482 search.resize( size );
482 FindQuery query( search ); 483 FindQuery query( search );
483 OSQLResult res = m_driver->query( &query ); 484 OSQLResult res = m_driver->query( &query );
484 if ( res.state() != OSQLResult::Success ) 485 if ( res.state() != OSQLResult::Success )
485 return to; 486 return to;
486 487
487 return todo( res ); 488 return todo( res );
488} 489}
489void OPimTodoAccessBackendSQL::clear() { 490void OPimTodoAccessBackendSQL::clear() {
490 ClearQuery cle; 491 ClearQuery cle;
491 OSQLResult res = m_driver->query( &cle ); 492 OSQLResult res = m_driver->query( &cle );
492 CreateQuery qu; 493 CreateQuery qu;
493 res = m_driver->query(&qu); 494 res = m_driver->query(&qu);
494} 495}
495bool OPimTodoAccessBackendSQL::add( const OPimTodo& t) { 496bool OPimTodoAccessBackendSQL::add( const OPimTodo& t) {
496 InsertQuery ins( t ); 497 InsertQuery ins( t );
497 OSQLResult res = m_driver->query( &ins ); 498 OSQLResult res = m_driver->query( &ins );
498 499
499 if ( res.state() == OSQLResult::Failure ) 500 if ( res.state() == OSQLResult::Failure )
500 return false; 501 return false;
501 int c = m_uids.count(); 502 int c = m_uids.count();
502 m_uids.resize( c+1 ); 503 m_uids.resize( c+1 );
503 m_uids[c] = t.uid(); 504 m_uids[c] = t.uid();
504 505
505 return true; 506 return true;
506} 507}
507bool OPimTodoAccessBackendSQL::remove( int uid ) { 508bool OPimTodoAccessBackendSQL::remove( int uid ) {
508 RemoveQuery rem( uid ); 509 RemoveQuery rem( uid );
509 OSQLResult res = m_driver->query(&rem ); 510 OSQLResult res = m_driver->query(&rem );
510 511
511 if ( res.state() == OSQLResult::Failure ) 512 if ( res.state() == OSQLResult::Failure )
512 return false; 513 return false;
513 514
514 m_dirty = true; 515 m_dirty = true;
515 return true; 516 return true;
516} 517}
517/* 518/*
518 * FIXME better set query 519 * FIXME better set query
519 * but we need the cache for that 520 * but we need the cache for that
520 * now we remove 521 * now we remove
521 */ 522 */
522bool OPimTodoAccessBackendSQL::replace( const OPimTodo& t) { 523bool OPimTodoAccessBackendSQL::replace( const OPimTodo& t) {
523 remove( t.uid() ); 524 remove( t.uid() );
524 bool b= add(t); 525 bool b= add(t);
525 m_dirty = false; // we changed some stuff but the UID stayed the same 526 m_dirty = false; // we changed some stuff but the UID stayed the same
526 return b; 527 return b;
527} 528}
528QArray<int> OPimTodoAccessBackendSQL::overDue() { 529QArray<int> OPimTodoAccessBackendSQL::overDue() {
529 OverDueQuery qu; 530 OverDueQuery qu;
530 return uids( m_driver->query(&qu ) ); 531 return uids( m_driver->query(&qu ) );
531} 532}
532QArray<int> OPimTodoAccessBackendSQL::effectiveToDos( const QDate& s, 533QArray<int> OPimTodoAccessBackendSQL::effectiveToDos( const QDate& s,
533 const QDate& t, 534 const QDate& t,
534 bool u) { 535 bool u) {
535 EffQuery ef(s, t, u ); 536 EffQuery ef(s, t, u );
536 return uids (m_driver->query(&ef) ); 537 return uids (m_driver->query(&ef) );
537} 538}
538/* 539/*
539 * 540 *
540 */ 541 */
541QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder, 542QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder,
542 int sortFilter, int cat ) { 543 int sortFilter, int cat ) {
543 qDebug("sorted %d, %d", asc, sortOrder ); 544 qDebug("sorted %d, %d", asc, sortOrder );
544 QString query; 545 QString query;
545 query = "select uid from todolist WHERE "; 546 query = "select uid from todolist WHERE ";
546 547
547 /* 548 /*
548 * Sort Filter stuff 549 * Sort Filter stuff
549 * not that straight forward 550 * not that straight forward
550 * FIXME: Replace magic numbers 551 * FIXME: Replace magic numbers
551 * 552 *
552 */ 553 */
553 /* Category */ 554 /* Category */
554 if ( sortFilter & 1 ) { 555 if ( sortFilter & 1 ) {
555 QString str; 556 QString str;
556 if (cat != 0 ) str = QString::number( cat ); 557 if (cat != 0 ) str = QString::number( cat );
557 query += " categories like '%" +str+"%' AND"; 558 query += " categories like '%" +str+"%' AND";
558 } 559 }
559 /* Show only overdue */ 560 /* Show only overdue */
560 if ( sortFilter & 2 ) { 561 if ( sortFilter & 2 ) {
561 QDate date = QDate::currentDate(); 562 QDate date = QDate::currentDate();
562 QString due; 563 QString due;
563 QString base; 564 QString base;
564 base = QString("DueDate <= '%1-%2-%3' AND completed = 0") 565 base = QString("DueDate <= '%1-%2-%3' AND completed = 0")
565 .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) 566 .arg( QString::number( date.year() ).rightJustify( 4, '0' ) )
566 .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) 567 .arg( QString::number( date.month() ).rightJustify( 2, '0' ) )
567 .arg( QString::number( date.day() ).rightJustify( 2, '0' ) ); 568 .arg( QString::number( date.day() ).rightJustify( 2, '0' ) );
568 query += " " + base + " AND"; 569 query += " " + base + " AND";
569 } 570 }
570 /* not show completed */ 571 /* not show completed */
571 if ( sortFilter & 4 ) { 572 if ( sortFilter & 4 ) {
572 query += " completed = 0 AND"; 573 query += " completed = 0 AND";
573 }else{ 574 }else{
574 query += " ( completed = 1 OR completed = 0) AND"; 575 query += " ( completed = 1 OR completed = 0) AND";
575 } 576 }
576 /* strip the end */ 577 /* strip the end */
577 query = query.remove( query.length()-3, 3 ); 578 query = query.remove( query.length()-3, 3 );
578 579
579 580
580 /* 581 /*
581 * sort order stuff 582 * sort order stuff
582 * quite straight forward 583 * quite straight forward
583 */ 584 */
584 query += "ORDER BY "; 585 query += "ORDER BY ";
585 switch( sortOrder ) { 586 switch( sortOrder ) {
586 /* completed */ 587 /* completed */
587 case 0: 588 case 0:
588 query += "completed"; 589 query += "completed";
589 break; 590 break;
590 case 1: 591 case 1:
591 query += "priority"; 592 query += "priority";
592 break; 593 break;
593 case 2: 594 case 2:
594 query += "summary"; 595 query += "summary";
595 break; 596 break;
596 case 3: 597 case 3:
597 query += "DueDate"; 598 query += "DueDate";
598 break; 599 break;
599 } 600 }
600 601
601 if ( !asc ) { 602 if ( !asc ) {
602 qDebug("not ascending!"); 603 qDebug("not ascending!");
603 query += " DESC"; 604 query += " DESC";
604 } 605 }
605 606
606 qDebug( query ); 607 qDebug( query );
607 OSQLRawQuery raw(query ); 608 OSQLRawQuery raw(query );
608 return uids( m_driver->query(&raw) ); 609 return uids( m_driver->query(&raw) );
609} 610}
610bool OPimTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{ 611bool OPimTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{
611 if ( str == "0-0-0" ) 612 if ( str == "0-0-0" )
612 return false; 613 return false;
613 else{ 614 else{
614 int day, year, month; 615 int day, year, month;
615 QStringList list = QStringList::split("-", str ); 616 QStringList list = QStringList::split("-", str );
616 year = list[0].toInt(); 617 year = list[0].toInt();
617 month = list[1].toInt(); 618 month = list[1].toInt();
618 day = list[2].toInt(); 619 day = list[2].toInt();
619 da.setYMD( year, month, day ); 620 da.setYMD( year, month, day );
620 return true; 621 return true;
621 } 622 }
622} 623}
623OPimTodo OPimTodoAccessBackendSQL::todo( const OSQLResult& res ) const{ 624OPimTodo OPimTodoAccessBackendSQL::todo( const OSQLResult& res ) const{
624 if ( res.state() == OSQLResult::Failure ) { 625 if ( res.state() == OSQLResult::Failure ) {
625 OPimTodo to; 626 OPimTodo to;
626 return to; 627 return to;
627 } 628 }
628 629
629 OSQLResultItem::ValueList list = res.results(); 630 OSQLResultItem::ValueList list = res.results();
630 OSQLResultItem::ValueList::Iterator it = list.begin(); 631 OSQLResultItem::ValueList::Iterator it = list.begin();
631 qDebug("todo1"); 632 qDebug("todo1");
632 OPimTodo to = todo( (*it) ); 633 OPimTodo to = todo( (*it) );
633 cache( to ); 634 cache( to );
634 ++it; 635 ++it;
635 636
636 for ( ; it != list.end(); ++it ) { 637 for ( ; it != list.end(); ++it ) {
637 qDebug("caching"); 638 qDebug("caching");
638 cache( todo( (*it) ) ); 639 cache( todo( (*it) ) );
639 } 640 }
640 return to; 641 return to;
641} 642}
642OPimTodo OPimTodoAccessBackendSQL::todo( OSQLResultItem& item )const { 643OPimTodo OPimTodoAccessBackendSQL::todo( OSQLResultItem& item )const {
643 qDebug("todo(ResultItem)"); 644 qDebug("todo(ResultItem)");
644 645
645 // Request information from addressbook table and create the OPimTodo-object. 646 // Request information from addressbook table and create the OPimTodo-object.
646 647
647 bool hasDueDate = false; QDate dueDate = QDate::currentDate(); 648 bool hasDueDate = false; QDate dueDate = QDate::currentDate();
648 hasDueDate = date( dueDate, item.data("DueDate") ); 649 hasDueDate = date( dueDate, item.data("DueDate") );
649 QStringList cats = QStringList::split(";", item.data("categories") ); 650 QStringList cats = QStringList::split(";", item.data("categories") );
650 651
651 qDebug("Item is completed: %d", item.data("completed").toInt() ); 652 qDebug("Item is completed: %d", item.data("completed").toInt() );
652 653
653 OPimTodo to( (bool)item.data("completed").toInt(), item.data("priority").toInt(), 654 OPimTodo to( (bool)item.data("completed").toInt(), item.data("priority").toInt(),
654 cats, item.data("summary"), item.data("description"), 655 cats, item.data("summary"), item.data("description"),
655 item.data("progress").toUShort(), hasDueDate, dueDate, 656 item.data("progress").toUShort(), hasDueDate, dueDate,
656 item.data("uid").toInt() ); 657 item.data("uid").toInt() );
657 658
658 bool isOk; 659 bool isOk;
659 int prioInt = QString( item.data("priority") ).toInt( &isOk ); 660 int prioInt = QString( item.data("priority") ).toInt( &isOk );
660 if ( isOk ) 661 if ( isOk )
661 to.setPriority( prioInt ); 662 to.setPriority( prioInt );
662 663
663 bool hasStartDate = false; QDate startDate = QDate::currentDate(); 664 bool hasStartDate = false; QDate startDate = QDate::currentDate();
664 hasStartDate = date( startDate, item.data("startdate") ); 665 hasStartDate = date( startDate, item.data("startdate") );
665 bool hasCompletedDate = false; QDate completedDate = QDate::currentDate(); 666 bool hasCompletedDate = false; QDate completedDate = QDate::currentDate();
666 hasCompletedDate = date( completedDate, item.data("completeddate") ); 667 hasCompletedDate = date( completedDate, item.data("completeddate") );
667 668
668 if ( hasStartDate ) 669 if ( hasStartDate )
669 to.setStartDate( startDate ); 670 to.setStartDate( startDate );
670 if ( hasCompletedDate ) 671 if ( hasCompletedDate )
671 to.setCompletedDate( completedDate ); 672 to.setCompletedDate( completedDate );
672 673
673 OPimNotifyManager& manager = to.notifiers(); 674 OPimNotifyManager& manager = to.notifiers();
674 manager.alarmsFromString( item.data("alarms") ); 675 manager.alarmsFromString( item.data("alarms") );
675 manager.remindersFromString( item.data("reminders") ); 676 manager.remindersFromString( item.data("reminders") );
676 677
677 OPimState pimState; 678 OPimState pimState;
678 pimState.setState( QString( item.data("state") ).toInt() ); 679 pimState.setState( QString( item.data("state") ).toInt() );
679 to.setState( pimState ); 680 to.setState( pimState );
680 681
681 QMap<int, QString> recMap; 682 QMap<int, QString> recMap;
682 recMap.insert( OPimRecurrence::RType , item.data("RType") ); 683 recMap.insert( OPimRecurrence::RType , item.data("RType") );
683 recMap.insert( OPimRecurrence::RWeekdays , item.data("RWeekdays") ); 684 recMap.insert( OPimRecurrence::RWeekdays , item.data("RWeekdays") );
684 recMap.insert( OPimRecurrence::RPosition , item.data("RPosition") ); 685 recMap.insert( OPimRecurrence::RPosition , item.data("RPosition") );
685 recMap.insert( OPimRecurrence::RFreq , item.data("RFreq") ); 686 recMap.insert( OPimRecurrence::RFreq , item.data("RFreq") );
686 recMap.insert( OPimRecurrence::RHasEndDate, item.data("RHasEndDate") ); 687 recMap.insert( OPimRecurrence::RHasEndDate, item.data("RHasEndDate") );
687 recMap.insert( OPimRecurrence::EndDate , item.data("EndDate") ); 688 recMap.insert( OPimRecurrence::EndDate , item.data("EndDate") );
688 recMap.insert( OPimRecurrence::Created , item.data("Created") ); 689 recMap.insert( OPimRecurrence::Created , item.data("Created") );
689 recMap.insert( OPimRecurrence::Exceptions , item.data("Exceptions") ); 690 recMap.insert( OPimRecurrence::Exceptions , item.data("Exceptions") );
690 691
691 OPimRecurrence recur; 692 OPimRecurrence recur;
692 recur.fromMap( recMap ); 693 recur.fromMap( recMap );
693 to.setRecurrence( recur ); 694 to.setRecurrence( recur );
694 695
695 // Finally load the custom-entries for this UID and put it into the created object 696 // Finally load the custom-entries for this UID and put it into the created object
696 to.setExtraMap( requestCustom( to.uid() ) ); 697 to.setExtraMap( requestCustom( to.uid() ) );
697 698
698 return to; 699 return to;
699} 700}
700OPimTodo OPimTodoAccessBackendSQL::todo( int uid )const { 701OPimTodo OPimTodoAccessBackendSQL::todo( int uid )const {
701 FindQuery find( uid ); 702 FindQuery find( uid );
702 return todo( m_driver->query(&find) ); 703 return todo( m_driver->query(&find) );
703} 704}
704/* 705/*
705 * update the dict 706 * update the dict
706 */ 707 */
707void OPimTodoAccessBackendSQL::fillDict() { 708void OPimTodoAccessBackendSQL::fillDict() {
708 709
709#if 0 710#if 0
710 /* initialize dict */ 711 /* initialize dict */
711 /* 712 /*
712 * UPDATE dict if you change anything!!! 713 * UPDATE dict if you change anything!!!
713 * FIXME: Isn't this dict obsolete ? (eilers) 714 * FIXME: Isn't this dict obsolete ? (eilers)
714 */ 715 */
715 m_dict.setAutoDelete( TRUE ); 716 m_dict.setAutoDelete( TRUE );
716 m_dict.insert("Categories" , new int(OPimTodo::Category) ); 717 m_dict.insert("Categories" , new int(OPimTodo::Category) );
717 m_dict.insert("Uid" , new int(OPimTodo::Uid) ); 718 m_dict.insert("Uid" , new int(OPimTodo::Uid) );
718 m_dict.insert("HasDate" , new int(OPimTodo::HasDate) ); 719 m_dict.insert("HasDate" , new int(OPimTodo::HasDate) );
719 m_dict.insert("Completed" , new int(OPimTodo::Completed) ); 720 m_dict.insert("Completed" , new int(OPimTodo::Completed) );
720 m_dict.insert("Description" , new int(OPimTodo::Description) ); 721 m_dict.insert("Description" , new int(OPimTodo::Description) );
721 m_dict.insert("Summary" , new int(OPimTodo::Summary) ); 722 m_dict.insert("Summary" , new int(OPimTodo::Summary) );
722 m_dict.insert("Priority" , new int(OPimTodo::Priority) ); 723 m_dict.insert("Priority" , new int(OPimTodo::Priority) );
723 m_dict.insert("DateDay" , new int(OPimTodo::DateDay) ); 724 m_dict.insert("DateDay" , new int(OPimTodo::DateDay) );
724 m_dict.insert("DateMonth" , new int(OPimTodo::DateMonth) ); 725 m_dict.insert("DateMonth" , new int(OPimTodo::DateMonth) );
725 m_dict.insert("DateYear" , new int(OPimTodo::DateYear) ); 726 m_dict.insert("DateYear" , new int(OPimTodo::DateYear) );
726 m_dict.insert("Progress" , new int(OPimTodo::Progress) ); 727 m_dict.insert("Progress" , new int(OPimTodo::Progress) );
727 m_dict.insert("Completed", new int(OPimTodo::Completed) ); // Why twice ? (eilers) 728 m_dict.insert("Completed", new int(OPimTodo::Completed) ); // Why twice ? (eilers)
728 m_dict.insert("CrossReference", new int(OPimTodo::CrossReference) ); 729 m_dict.insert("CrossReference", new int(OPimTodo::CrossReference) );
729// m_dict.insert("HasAlarmDateTime",new int(OPimTodo::HasAlarmDateTime) ); // old stuff (eilers) 730// m_dict.insert("HasAlarmDateTime",new int(OPimTodo::HasAlarmDateTime) ); // old stuff (eilers)
730// m_dict.insert("AlarmDateTime", new int(OPimTodo::AlarmDateTime) ); // old stuff (eilers) 731// m_dict.insert("AlarmDateTime", new int(OPimTodo::AlarmDateTime) ); // old stuff (eilers)
731 732
732#endif 733#endif
733} 734}
734/* 735/*
735 * need to be const so let's fool the 736 * need to be const so let's fool the
736 * compiler :( 737 * compiler :(
737 */ 738 */
738void OPimTodoAccessBackendSQL::update()const { 739void OPimTodoAccessBackendSQL::update()const {
739 ((OPimTodoAccessBackendSQL*)this)->m_dirty = false; 740 ((OPimTodoAccessBackendSQL*)this)->m_dirty = false;
740 LoadQuery lo; 741 LoadQuery lo;
741 OSQLResult res = m_driver->query(&lo); 742 OSQLResult res = m_driver->query(&lo);
742 if ( res.state() != OSQLResult::Success ) 743 if ( res.state() != OSQLResult::Success )
743 return; 744 return;
744 745
745 ((OPimTodoAccessBackendSQL*)this)->m_uids = uids( res ); 746 ((OPimTodoAccessBackendSQL*)this)->m_uids = uids( res );
746} 747}
747QArray<int> OPimTodoAccessBackendSQL::uids( const OSQLResult& res) const{ 748QArray<int> OPimTodoAccessBackendSQL::uids( const OSQLResult& res) const{
748 749
749 OSQLResultItem::ValueList list = res.results(); 750 OSQLResultItem::ValueList list = res.results();
750 OSQLResultItem::ValueList::Iterator it; 751 OSQLResultItem::ValueList::Iterator it;
751 QArray<int> ints(list.count() ); 752 QArray<int> ints(list.count() );
752 qDebug(" count = %d", list.count() ); 753 qDebug(" count = %d", list.count() );
753 754
754 int i = 0; 755 int i = 0;
755 for (it = list.begin(); it != list.end(); ++it ) { 756 for (it = list.begin(); it != list.end(); ++it ) {
756 ints[i] = (*it).data("uid").toInt(); 757 ints[i] = (*it).data("uid").toInt();
757 i++; 758 i++;
758 } 759 }
759 return ints; 760 return ints;
760} 761}
761 762
762QArray<int> OPimTodoAccessBackendSQL::matchRegexp( const QRegExp &r ) const 763QArray<int> OPimTodoAccessBackendSQL::matchRegexp( const QRegExp &r ) const
763{ 764{
764 765
765#warning OPimTodoAccessBackendSQL::matchRegexp() not implemented !!
766
767#if 0 766#if 0
767 QArray<int> empty;
768 return empty;
768 769
769 Copied from xml-backend by not adapted to sql (eilers) 770#else
771 QString qu = "SELECT uid FROM todolist WHERE (";
770 772
771 QArray<int> m_currentQuery( m_events.count() ); 773 // Do it make sense to search other fields, too ?
772 uint arraycounter = 0; 774 qu += " rlike(\""+ r.pattern() + "\",\"description\") OR";
775 qu += " rlike(\""+ r.pattern() + "\",\"summary\")";
776
777 qu += ")";
773 778
779 qDebug( "query: %s", qu.latin1() );
774 780
781 OSQLRawQuery raw( qu );
782 OSQLResult res = m_driver->query( &raw );
775 783
776 QMap<int, OPimTodo>::ConstIterator it; 784 return uids( res );
777 for (it = m_events.begin(); it != m_events.end(); ++it ) {
778 if ( it.data().match( r ) )
779 m_currentQuery[arraycounter++] = it.data().uid();
780 785
781 }
782 // Shrink to fit..
783 m_currentQuery.resize(arraycounter);
784 786
785 return m_currentQuery;
786#endif 787#endif
787 QArray<int> empty; 788
788 return empty;
789} 789}
790QBitArray OPimTodoAccessBackendSQL::supports()const { 790QBitArray OPimTodoAccessBackendSQL::supports()const {
791 791
792 return sup(); 792 return sup();
793} 793}
794 794
795QBitArray OPimTodoAccessBackendSQL::sup() const{ 795QBitArray OPimTodoAccessBackendSQL::sup() const{
796 796
797 QBitArray ar( OPimTodo::CompletedDate + 1 ); 797 QBitArray ar( OPimTodo::CompletedDate + 1 );
798 ar.fill( true ); 798 ar.fill( true );
799 ar[OPimTodo::CrossReference] = false; 799 ar[OPimTodo::CrossReference] = false;
800 ar[OPimTodo::State ] = false; 800 ar[OPimTodo::State ] = false;
801 ar[OPimTodo::Reminders] = false; 801 ar[OPimTodo::Reminders] = false;
802 ar[OPimTodo::Notifiers] = false; 802 ar[OPimTodo::Notifiers] = false;
803 ar[OPimTodo::Maintainer] = false; 803 ar[OPimTodo::Maintainer] = false;
804 804
805 return ar; 805 return ar;
806} 806}
807 807
808void OPimTodoAccessBackendSQL::removeAllCompleted(){ 808void OPimTodoAccessBackendSQL::removeAllCompleted(){
809#warning OPimTodoAccessBackendSQL::removeAllCompleted() not implemented !! 809 // First we need the uids from all entries which are
810 // completed. Then, we just have to remove them...
811
812 QString qu = "SELECT uid FROM todolist WHERE completed = 1";
813
814 OSQLRawQuery raw( qu );
815 OSQLResult res = m_driver->query( &raw );
816
817 QArray<int> completed_uids = uids( res );
810 818
819 qDebug( "Number of completed: %d", completed_uids.size() );
820
821 if ( completed_uids.size() == 0 )
822 return;
823
824 qu = "DELETE FROM todolist WHERE (";
825 QString query;
826
827 for ( int i = 0; i < completed_uids.size(); i++ ){
828 if ( !query.isEmpty() )
829 query += " OR ";
830 query += QString( "uid = %1" ).arg( completed_uids[i] );
831 }
832 qu += query + " );";
833
834 // Put remove of custom entries in this query to speed up..
835 qu += "DELETE FORM custom_data WHERE (";
836 query = "";
837
838 for ( int i = 0; i < completed_uids.size(); i++ ){
839 if ( !query.isEmpty() )
840 query += " OR ";
841 query += QString( "uid = %1" ).arg( completed_uids[i] );
842 }
843 qu += query + " );";
844
845 qDebug( "query: %s", qu.latin1() );
846
847 OSQLRawQuery raw2( qu );
848 res = m_driver->query( &raw2 );
849 if ( res.state() == OSQLResult::Failure ) {
850 qWarning("OPimTodoAccessBackendSQL::removeAllCompleted():Failure in query: %s", qu.latin1() );
851 }
811} 852}
812 853
813 854
814QMap<QString, QString> OPimTodoAccessBackendSQL::requestCustom( int uid ) const 855QMap<QString, QString> OPimTodoAccessBackendSQL::requestCustom( int uid ) const
815{ 856{
816 QMap<QString, QString> customMap; 857 QMap<QString, QString> customMap;
817 858
818 FindCustomQuery query( uid ); 859 FindCustomQuery query( uid );
819 OSQLResult res_custom = m_driver->query( &query ); 860 OSQLResult res_custom = m_driver->query( &query );
820 861
821 if ( res_custom.state() == OSQLResult::Failure ) { 862 if ( res_custom.state() == OSQLResult::Failure ) {
822 qWarning("OSQLResult::Failure in find query !!"); 863 qWarning("OSQLResult::Failure in find query !!");
823 QMap<QString, QString> empty; 864 QMap<QString, QString> empty;
824 return empty; 865 return empty;
825 } 866 }
826 867
827 OSQLResultItem::ValueList list = res_custom.results(); 868 OSQLResultItem::ValueList list = res_custom.results();
828 OSQLResultItem::ValueList::Iterator it = list.begin(); 869 OSQLResultItem::ValueList::Iterator it = list.begin();
829 for ( ; it != list.end(); ++it ) { 870 for ( ; it != list.end(); ++it ) {
830 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); 871 customMap.insert( (*it).data( "type" ), (*it).data( "value" ) );
831 } 872 }
832 873
833 return customMap; 874 return customMap;
834} 875}
835 876
836 877
837 878
838 879
839} 880}