author | zecke <zecke> | 2004-05-22 10:17:20 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-05-22 10:17:20 (UTC) |
commit | 77f189ec4b959a2874942b00b070ea60c091a7fa (patch) (unidiff) | |
tree | 57e36d30a275db1b139ccc5995080b553f33b0f0 /libopie2/opiepim | |
parent | 35b6ab7a844145378a6e427c4666fa65b63d3cfd (diff) | |
download | opie-77f189ec4b959a2874942b00b070ea60c091a7fa.zip opie-77f189ec4b959a2874942b00b070ea60c091a7fa.tar.gz opie-77f189ec4b959a2874942b00b070ea60c091a7fa.tar.bz2 |
Fix inserting data with custom tables
Also I don't like the string comparsions. Are they really needed?
-rw-r--r-- | libopie2/opiepim/backend/otodoaccesssql.cpp | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/libopie2/opiepim/backend/otodoaccesssql.cpp b/libopie2/opiepim/backend/otodoaccesssql.cpp index ef036d5..132b5a6 100644 --- a/libopie2/opiepim/backend/otodoaccesssql.cpp +++ b/libopie2/opiepim/backend/otodoaccesssql.cpp | |||
@@ -1,880 +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 | ||
47 | using namespace Opie::DB; | 47 | using namespace Opie::DB; |
48 | 48 | ||
49 | using namespace Opie; | 49 | using 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 | */ |
58 | namespace { | 58 | namespace { |
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 | qu += "DELETE FROM custom_data WHERE uid = " + QString::number(m_uid); |
304 | return qu; | 304 | return qu; |
305 | } | 305 | } |
306 | 306 | ||
307 | 307 | ||
308 | ClearQuery::ClearQuery() | 308 | ClearQuery::ClearQuery() |
309 | : OSQLQuery() {} | 309 | : OSQLQuery() {} |
310 | ClearQuery::~ClearQuery() {} | 310 | ClearQuery::~ClearQuery() {} |
311 | QString ClearQuery::query()const { | 311 | QString ClearQuery::query()const { |
312 | QString qu = "drop table todolist"; | 312 | QString qu = "drop table todolist"; |
313 | return qu; | 313 | return qu; |
314 | } | 314 | } |
315 | FindQuery::FindQuery(int uid) | 315 | FindQuery::FindQuery(int uid) |
316 | : OSQLQuery(), m_uid(uid ) { | 316 | : OSQLQuery(), m_uid(uid ) { |
317 | } | 317 | } |
318 | FindQuery::FindQuery(const QArray<int>& ints) | 318 | FindQuery::FindQuery(const QArray<int>& ints) |
319 | : OSQLQuery(), m_uids(ints){ | 319 | : OSQLQuery(), m_uids(ints){ |
320 | } | 320 | } |
321 | FindQuery::~FindQuery() { | 321 | FindQuery::~FindQuery() { |
322 | } | 322 | } |
323 | QString FindQuery::query()const{ | 323 | QString FindQuery::query()const{ |
324 | if (m_uids.count() == 0 ) | 324 | if (m_uids.count() == 0 ) |
325 | return single(); | 325 | return single(); |
326 | else | 326 | else |
327 | return multi(); | 327 | return multi(); |
328 | } | 328 | } |
329 | QString FindQuery::single()const{ | 329 | QString FindQuery::single()const{ |
330 | QString qu = "select * from todolist where uid = " + QString::number(m_uid); | 330 | QString qu = "select * from todolist where uid = " + QString::number(m_uid); |
331 | return qu; | 331 | return qu; |
332 | } | 332 | } |
333 | QString FindQuery::multi()const { | 333 | QString FindQuery::multi()const { |
334 | QString qu = "select * from todolist where "; | 334 | QString qu = "select * from todolist where "; |
335 | for (uint i = 0; i < m_uids.count(); i++ ) { | 335 | for (uint i = 0; i < m_uids.count(); i++ ) { |
336 | qu += " UID = " + QString::number( m_uids[i] ) + " OR"; | 336 | qu += " UID = " + QString::number( m_uids[i] ) + " OR"; |
337 | } | 337 | } |
338 | qu.remove( qu.length()-2, 2 ); | 338 | qu.remove( qu.length()-2, 2 ); |
339 | return qu; | 339 | return qu; |
340 | } | 340 | } |
341 | 341 | ||
342 | OverDueQuery::OverDueQuery(): OSQLQuery() {} | 342 | OverDueQuery::OverDueQuery(): OSQLQuery() {} |
343 | OverDueQuery::~OverDueQuery() {} | 343 | OverDueQuery::~OverDueQuery() {} |
344 | QString OverDueQuery::query()const { | 344 | QString OverDueQuery::query()const { |
345 | QDate date = QDate::currentDate(); | 345 | QDate date = QDate::currentDate(); |
346 | QString str; | 346 | QString str; |
347 | str = QString("select uid from todolist where DueDate ='%1-%2-%3'") | 347 | str = QString("select uid from todolist where DueDate ='%1-%2-%3'") |
348 | .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) | 348 | .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) |
349 | .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) | 349 | .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) |
350 | .arg( QString::number( date.day() ) .rightJustify( 2, '0' ) ); | 350 | .arg( QString::number( date.day() ) .rightJustify( 2, '0' ) ); |
351 | 351 | ||
352 | return str; | 352 | return str; |
353 | } | 353 | } |
354 | 354 | ||
355 | 355 | ||
356 | EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc ) | 356 | EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc ) |
357 | : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {} | 357 | : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {} |
358 | EffQuery::~EffQuery() {} | 358 | EffQuery::~EffQuery() {} |
359 | QString EffQuery::query()const { | 359 | QString EffQuery::query()const { |
360 | return m_inc ? with() : out(); | 360 | return m_inc ? with() : out(); |
361 | } | 361 | } |
362 | QString EffQuery::with()const { | 362 | QString EffQuery::with()const { |
363 | QString str; | 363 | QString str; |
364 | 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' ") |
365 | .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) | 365 | .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) |
366 | .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) | 366 | .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) |
367 | .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) | 367 | .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) |
368 | .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) ) | 368 | .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) ) |
369 | .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) ) | 369 | .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) ) |
370 | .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) ); | 370 | .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) ); |
371 | return str; | 371 | return str; |
372 | } | 372 | } |
373 | QString EffQuery::out()const { | 373 | QString EffQuery::out()const { |
374 | QString str; | 374 | QString str; |
375 | 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'") |
376 | .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) | 376 | .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) |
377 | .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) | 377 | .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) |
378 | .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) | 378 | .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) |
379 | .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) ) | 379 | .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) ) |
380 | .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) ) | 380 | .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) ) |
381 | .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) ); | 381 | .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) ); |
382 | 382 | ||
383 | return str; | 383 | return str; |
384 | } | 384 | } |
385 | 385 | ||
386 | FindCustomQuery::FindCustomQuery(int uid) | 386 | FindCustomQuery::FindCustomQuery(int uid) |
387 | : OSQLQuery(), m_uid( uid ) { | 387 | : OSQLQuery(), m_uid( uid ) { |
388 | } | 388 | } |
389 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) | 389 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) |
390 | : OSQLQuery(), m_uids( ints ){ | 390 | : OSQLQuery(), m_uids( ints ){ |
391 | } | 391 | } |
392 | FindCustomQuery::~FindCustomQuery() { | 392 | FindCustomQuery::~FindCustomQuery() { |
393 | } | 393 | } |
394 | QString FindCustomQuery::query()const{ | 394 | QString FindCustomQuery::query()const{ |
395 | return single(); // Multiple requests not supported ! | 395 | return single(); // Multiple requests not supported ! |
396 | } | 396 | } |
397 | QString FindCustomQuery::single()const{ | 397 | QString FindCustomQuery::single()const{ |
398 | QString qu = "select uid, type, value from custom_data where uid = "; | 398 | QString qu = "select uid, type, value from custom_data where uid = "; |
399 | qu += QString::number(m_uid); | 399 | qu += QString::number(m_uid); |
400 | return qu; | 400 | return qu; |
401 | } | 401 | } |
402 | 402 | ||
403 | }; | 403 | }; |
404 | 404 | ||
405 | 405 | ||
406 | namespace Opie { | 406 | namespace Opie { |
407 | OPimTodoAccessBackendSQL::OPimTodoAccessBackendSQL( const QString& file ) | 407 | OPimTodoAccessBackendSQL::OPimTodoAccessBackendSQL( const QString& file ) |
408 | : OPimTodoAccessBackend(),/* m_dict(15),*/ m_driver(NULL), m_dirty(true) | 408 | : OPimTodoAccessBackend(),/* m_dict(15),*/ m_driver(NULL), m_dirty(true) |
409 | { | 409 | { |
410 | QString fi = file; | 410 | QString fi = file; |
411 | if ( fi.isEmpty() ) | 411 | if ( fi.isEmpty() ) |
412 | fi = Global::applicationFileName( "todolist", "todolist.db" ); | 412 | fi = Global::applicationFileName( "todolist", "todolist.db" ); |
413 | OSQLManager man; | 413 | OSQLManager man; |
414 | m_driver = man.standard(); | 414 | m_driver = man.standard(); |
415 | m_driver->setUrl(fi); | 415 | m_driver->setUrl(fi); |
416 | // fillDict(); | 416 | // fillDict(); |
417 | } | 417 | } |
418 | 418 | ||
419 | OPimTodoAccessBackendSQL::~OPimTodoAccessBackendSQL(){ | 419 | OPimTodoAccessBackendSQL::~OPimTodoAccessBackendSQL(){ |
420 | if( m_driver ) | 420 | if( m_driver ) |
421 | delete m_driver; | 421 | delete m_driver; |
422 | } | 422 | } |
423 | 423 | ||
424 | bool OPimTodoAccessBackendSQL::load(){ | 424 | bool OPimTodoAccessBackendSQL::load(){ |
425 | if (!m_driver->open() ) | 425 | if (!m_driver->open() ) |
426 | return false; | 426 | return false; |
427 | 427 | ||
428 | CreateQuery creat; | 428 | CreateQuery creat; |
429 | OSQLResult res = m_driver->query(&creat ); | 429 | OSQLResult res = m_driver->query(&creat ); |
430 | 430 | ||
431 | m_dirty = true; | 431 | m_dirty = true; |
432 | return true; | 432 | return true; |
433 | } | 433 | } |
434 | bool OPimTodoAccessBackendSQL::reload(){ | 434 | bool OPimTodoAccessBackendSQL::reload(){ |
435 | return load(); | 435 | return load(); |
436 | } | 436 | } |
437 | 437 | ||
438 | bool OPimTodoAccessBackendSQL::save(){ | 438 | bool OPimTodoAccessBackendSQL::save(){ |
439 | 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) |
440 | } | 440 | } |
441 | QArray<int> OPimTodoAccessBackendSQL::allRecords()const { | 441 | QArray<int> OPimTodoAccessBackendSQL::allRecords()const { |
442 | if (m_dirty ) | 442 | if (m_dirty ) |
443 | update(); | 443 | update(); |
444 | 444 | ||
445 | return m_uids; | 445 | return m_uids; |
446 | } | 446 | } |
447 | QArray<int> OPimTodoAccessBackendSQL::queryByExample( const OPimTodo& , int, const QDateTime& ){ | 447 | QArray<int> OPimTodoAccessBackendSQL::queryByExample( const OPimTodo& , int, const QDateTime& ){ |
448 | QArray<int> ints(0); | 448 | QArray<int> ints(0); |
449 | return ints; | 449 | return ints; |
450 | } | 450 | } |
451 | OPimTodo OPimTodoAccessBackendSQL::find(int uid ) const{ | 451 | OPimTodo OPimTodoAccessBackendSQL::find(int uid ) const{ |
452 | FindQuery query( uid ); | 452 | FindQuery query( uid ); |
453 | return todo( m_driver->query(&query) ); | 453 | return todo( m_driver->query(&query) ); |
454 | 454 | ||
455 | } | 455 | } |
456 | OPimTodo OPimTodoAccessBackendSQL::find( int uid, const QArray<int>& ints, | 456 | OPimTodo OPimTodoAccessBackendSQL::find( int uid, const QArray<int>& ints, |
457 | uint cur, Frontend::CacheDirection dir ) const{ | 457 | uint cur, Frontend::CacheDirection dir ) const{ |
458 | uint CACHE = readAhead(); | 458 | uint CACHE = readAhead(); |
459 | qDebug("searching for %d", uid ); | 459 | qDebug("searching for %d", uid ); |
460 | QArray<int> search( CACHE ); | 460 | QArray<int> search( CACHE ); |
461 | uint size =0; | 461 | uint size =0; |
462 | OPimTodo to; | 462 | OPimTodo to; |
463 | 463 | ||
464 | // we try to cache CACHE items | 464 | // we try to cache CACHE items |
465 | switch( dir ) { | 465 | switch( dir ) { |
466 | /* forward */ | 466 | /* forward */ |
467 | 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) |
468 | for (uint i = cur; i < ints.count() && size < CACHE; i++ ) { | 468 | for (uint i = cur; i < ints.count() && size < CACHE; i++ ) { |
469 | qDebug("size %d %d", size, ints[i] ); | 469 | qDebug("size %d %d", size, ints[i] ); |
470 | search[size] = ints[i]; | 470 | search[size] = ints[i]; |
471 | size++; | 471 | size++; |
472 | } | 472 | } |
473 | break; | 473 | break; |
474 | /* reverse */ | 474 | /* reverse */ |
475 | 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) |
476 | for (uint i = cur; i != 0 && size < CACHE; i-- ) { | 476 | for (uint i = cur; i != 0 && size < CACHE; i-- ) { |
477 | search[size] = ints[i]; | 477 | search[size] = ints[i]; |
478 | size++; | 478 | size++; |
479 | } | 479 | } |
480 | break; | 480 | break; |
481 | } | 481 | } |
482 | search.resize( size ); | 482 | search.resize( size ); |
483 | FindQuery query( search ); | 483 | FindQuery query( search ); |
484 | OSQLResult res = m_driver->query( &query ); | 484 | OSQLResult res = m_driver->query( &query ); |
485 | if ( res.state() != OSQLResult::Success ) | 485 | if ( res.state() != OSQLResult::Success ) |
486 | return to; | 486 | return to; |
487 | 487 | ||
488 | return todo( res ); | 488 | return todo( res ); |
489 | } | 489 | } |
490 | void OPimTodoAccessBackendSQL::clear() { | 490 | void OPimTodoAccessBackendSQL::clear() { |
491 | ClearQuery cle; | 491 | ClearQuery cle; |
492 | OSQLResult res = m_driver->query( &cle ); | 492 | OSQLResult res = m_driver->query( &cle ); |
493 | CreateQuery qu; | 493 | CreateQuery qu; |
494 | res = m_driver->query(&qu); | 494 | res = m_driver->query(&qu); |
495 | } | 495 | } |
496 | bool OPimTodoAccessBackendSQL::add( const OPimTodo& t) { | 496 | bool OPimTodoAccessBackendSQL::add( const OPimTodo& t) { |
497 | InsertQuery ins( t ); | 497 | InsertQuery ins( t ); |
498 | OSQLResult res = m_driver->query( &ins ); | 498 | OSQLResult res = m_driver->query( &ins ); |
499 | 499 | ||
500 | if ( res.state() == OSQLResult::Failure ) | 500 | if ( res.state() == OSQLResult::Failure ) |
501 | return false; | 501 | return false; |
502 | int c = m_uids.count(); | 502 | int c = m_uids.count(); |
503 | m_uids.resize( c+1 ); | 503 | m_uids.resize( c+1 ); |
504 | m_uids[c] = t.uid(); | 504 | m_uids[c] = t.uid(); |
505 | 505 | ||
506 | return true; | 506 | return true; |
507 | } | 507 | } |
508 | bool OPimTodoAccessBackendSQL::remove( int uid ) { | 508 | bool OPimTodoAccessBackendSQL::remove( int uid ) { |
509 | RemoveQuery rem( uid ); | 509 | RemoveQuery rem( uid ); |
510 | OSQLResult res = m_driver->query(&rem ); | 510 | OSQLResult res = m_driver->query(&rem ); |
511 | 511 | ||
512 | if ( res.state() == OSQLResult::Failure ) | 512 | if ( res.state() == OSQLResult::Failure ) |
513 | return false; | 513 | return false; |
514 | 514 | ||
515 | m_dirty = true; | 515 | m_dirty = true; |
516 | return true; | 516 | return true; |
517 | } | 517 | } |
518 | /* | 518 | /* |
519 | * FIXME better set query | 519 | * FIXME better set query |
520 | * but we need the cache for that | 520 | * but we need the cache for that |
521 | * now we remove | 521 | * now we remove |
522 | */ | 522 | */ |
523 | bool OPimTodoAccessBackendSQL::replace( const OPimTodo& t) { | 523 | bool OPimTodoAccessBackendSQL::replace( const OPimTodo& t) { |
524 | remove( t.uid() ); | 524 | remove( t.uid() ); |
525 | bool b= add(t); | 525 | bool b= add(t); |
526 | 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 |
527 | return b; | 527 | return b; |
528 | } | 528 | } |
529 | QArray<int> OPimTodoAccessBackendSQL::overDue() { | 529 | QArray<int> OPimTodoAccessBackendSQL::overDue() { |
530 | OverDueQuery qu; | 530 | OverDueQuery qu; |
531 | return uids( m_driver->query(&qu ) ); | 531 | return uids( m_driver->query(&qu ) ); |
532 | } | 532 | } |
533 | QArray<int> OPimTodoAccessBackendSQL::effectiveToDos( const QDate& s, | 533 | QArray<int> OPimTodoAccessBackendSQL::effectiveToDos( const QDate& s, |
534 | const QDate& t, | 534 | const QDate& t, |
535 | bool u) { | 535 | bool u) { |
536 | EffQuery ef(s, t, u ); | 536 | EffQuery ef(s, t, u ); |
537 | return uids (m_driver->query(&ef) ); | 537 | return uids (m_driver->query(&ef) ); |
538 | } | 538 | } |
539 | /* | 539 | /* |
540 | * | 540 | * |
541 | */ | 541 | */ |
542 | QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder, | 542 | QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder, |
543 | int sortFilter, int cat ) { | 543 | int sortFilter, int cat ) { |
544 | qDebug("sorted %d, %d", asc, sortOrder ); | 544 | qDebug("sorted %d, %d", asc, sortOrder ); |
545 | QString query; | 545 | QString query; |
546 | query = "select uid from todolist WHERE "; | 546 | query = "select uid from todolist WHERE "; |
547 | 547 | ||
548 | /* | 548 | /* |
549 | * Sort Filter stuff | 549 | * Sort Filter stuff |
550 | * not that straight forward | 550 | * not that straight forward |
551 | * FIXME: Replace magic numbers | 551 | * FIXME: Replace magic numbers |
552 | * | 552 | * |
553 | */ | 553 | */ |
554 | /* Category */ | 554 | /* Category */ |
555 | if ( sortFilter & 1 ) { | 555 | if ( sortFilter & 1 ) { |
556 | QString str; | 556 | QString str; |
557 | if (cat != 0 ) str = QString::number( cat ); | 557 | if (cat != 0 ) str = QString::number( cat ); |
558 | query += " categories like '%" +str+"%' AND"; | 558 | query += " categories like '%" +str+"%' AND"; |
559 | } | 559 | } |
560 | /* Show only overdue */ | 560 | /* Show only overdue */ |
561 | if ( sortFilter & 2 ) { | 561 | if ( sortFilter & 2 ) { |
562 | QDate date = QDate::currentDate(); | 562 | QDate date = QDate::currentDate(); |
563 | QString due; | 563 | QString due; |
564 | QString base; | 564 | QString base; |
565 | base = QString("DueDate <= '%1-%2-%3' AND completed = 0") | 565 | base = QString("DueDate <= '%1-%2-%3' AND completed = 0") |
566 | .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) | 566 | .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) |
567 | .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) | 567 | .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) |
568 | .arg( QString::number( date.day() ).rightJustify( 2, '0' ) ); | 568 | .arg( QString::number( date.day() ).rightJustify( 2, '0' ) ); |
569 | query += " " + base + " AND"; | 569 | query += " " + base + " AND"; |
570 | } | 570 | } |
571 | /* not show completed */ | 571 | /* not show completed */ |
572 | if ( sortFilter & 4 ) { | 572 | if ( sortFilter & 4 ) { |
573 | query += " completed = 0 AND"; | 573 | query += " completed = 0 AND"; |
574 | }else{ | 574 | }else{ |
575 | query += " ( completed = 1 OR completed = 0) AND"; | 575 | query += " ( completed = 1 OR completed = 0) AND"; |
576 | } | 576 | } |
577 | /* strip the end */ | 577 | /* strip the end */ |
578 | query = query.remove( query.length()-3, 3 ); | 578 | query = query.remove( query.length()-3, 3 ); |
579 | 579 | ||
580 | 580 | ||
581 | /* | 581 | /* |
582 | * sort order stuff | 582 | * sort order stuff |
583 | * quite straight forward | 583 | * quite straight forward |
584 | */ | 584 | */ |
585 | query += "ORDER BY "; | 585 | query += "ORDER BY "; |
586 | switch( sortOrder ) { | 586 | switch( sortOrder ) { |
587 | /* completed */ | 587 | /* completed */ |
588 | case 0: | 588 | case 0: |
589 | query += "completed"; | 589 | query += "completed"; |
590 | break; | 590 | break; |
591 | case 1: | 591 | case 1: |
592 | query += "priority"; | 592 | query += "priority"; |
593 | break; | 593 | break; |
594 | case 2: | 594 | case 2: |
595 | query += "summary"; | 595 | query += "summary"; |
596 | break; | 596 | break; |
597 | case 3: | 597 | case 3: |
598 | query += "DueDate"; | 598 | query += "DueDate"; |
599 | break; | 599 | break; |
600 | } | 600 | } |
601 | 601 | ||
602 | if ( !asc ) { | 602 | if ( !asc ) { |
603 | qDebug("not ascending!"); | 603 | qDebug("not ascending!"); |
604 | query += " DESC"; | 604 | query += " DESC"; |
605 | } | 605 | } |
606 | 606 | ||
607 | qDebug( query ); | 607 | qDebug( query ); |
608 | OSQLRawQuery raw(query ); | 608 | OSQLRawQuery raw(query ); |
609 | return uids( m_driver->query(&raw) ); | 609 | return uids( m_driver->query(&raw) ); |
610 | } | 610 | } |
611 | bool OPimTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{ | 611 | bool OPimTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{ |
612 | if ( str == "0-0-0" ) | 612 | if ( str == "0-0-0" ) |
613 | return false; | 613 | return false; |
614 | else{ | 614 | else{ |
615 | int day, year, month; | 615 | int day, year, month; |
616 | QStringList list = QStringList::split("-", str ); | 616 | QStringList list = QStringList::split("-", str ); |
617 | year = list[0].toInt(); | 617 | year = list[0].toInt(); |
618 | month = list[1].toInt(); | 618 | month = list[1].toInt(); |
619 | day = list[2].toInt(); | 619 | day = list[2].toInt(); |
620 | da.setYMD( year, month, day ); | 620 | da.setYMD( year, month, day ); |
621 | return true; | 621 | return true; |
622 | } | 622 | } |
623 | } | 623 | } |
624 | OPimTodo OPimTodoAccessBackendSQL::todo( const OSQLResult& res ) const{ | 624 | OPimTodo OPimTodoAccessBackendSQL::todo( const OSQLResult& res ) const{ |
625 | if ( res.state() == OSQLResult::Failure ) { | 625 | if ( res.state() == OSQLResult::Failure ) { |
626 | OPimTodo to; | 626 | OPimTodo to; |
627 | return to; | 627 | return to; |
628 | } | 628 | } |
629 | 629 | ||
630 | OSQLResultItem::ValueList list = res.results(); | 630 | OSQLResultItem::ValueList list = res.results(); |
631 | OSQLResultItem::ValueList::Iterator it = list.begin(); | 631 | OSQLResultItem::ValueList::Iterator it = list.begin(); |
632 | qDebug("todo1"); | 632 | qDebug("todo1"); |
633 | OPimTodo to = todo( (*it) ); | 633 | OPimTodo to = todo( (*it) ); |
634 | cache( to ); | 634 | cache( to ); |
635 | ++it; | 635 | ++it; |
636 | 636 | ||
637 | for ( ; it != list.end(); ++it ) { | 637 | for ( ; it != list.end(); ++it ) { |
638 | qDebug("caching"); | 638 | qDebug("caching"); |
639 | cache( todo( (*it) ) ); | 639 | cache( todo( (*it) ) ); |
640 | } | 640 | } |
641 | return to; | 641 | return to; |
642 | } | 642 | } |
643 | OPimTodo OPimTodoAccessBackendSQL::todo( OSQLResultItem& item )const { | 643 | OPimTodo OPimTodoAccessBackendSQL::todo( OSQLResultItem& item )const { |
644 | qDebug("todo(ResultItem)"); | 644 | qDebug("todo(ResultItem)"); |
645 | 645 | ||
646 | // Request information from addressbook table and create the OPimTodo-object. | 646 | // Request information from addressbook table and create the OPimTodo-object. |
647 | 647 | ||
648 | bool hasDueDate = false; QDate dueDate = QDate::currentDate(); | 648 | bool hasDueDate = false; QDate dueDate = QDate::currentDate(); |
649 | hasDueDate = date( dueDate, item.data("DueDate") ); | 649 | hasDueDate = date( dueDate, item.data("DueDate") ); |
650 | QStringList cats = QStringList::split(";", item.data("categories") ); | 650 | QStringList cats = QStringList::split(";", item.data("categories") ); |
651 | 651 | ||
652 | qDebug("Item is completed: %d", item.data("completed").toInt() ); | 652 | qDebug("Item is completed: %d", item.data("completed").toInt() ); |
653 | 653 | ||
654 | OPimTodo to( (bool)item.data("completed").toInt(), item.data("priority").toInt(), | 654 | OPimTodo to( (bool)item.data("completed").toInt(), item.data("priority").toInt(), |
655 | cats, item.data("summary"), item.data("description"), | 655 | cats, item.data("summary"), item.data("description"), |
656 | item.data("progress").toUShort(), hasDueDate, dueDate, | 656 | item.data("progress").toUShort(), hasDueDate, dueDate, |
657 | item.data("uid").toInt() ); | 657 | item.data("uid").toInt() ); |
658 | 658 | ||
659 | bool isOk; | 659 | bool isOk; |
660 | int prioInt = QString( item.data("priority") ).toInt( &isOk ); | 660 | int prioInt = QString( item.data("priority") ).toInt( &isOk ); |
661 | if ( isOk ) | 661 | if ( isOk ) |
662 | to.setPriority( prioInt ); | 662 | to.setPriority( prioInt ); |
663 | 663 | ||
664 | bool hasStartDate = false; QDate startDate = QDate::currentDate(); | 664 | bool hasStartDate = false; QDate startDate = QDate::currentDate(); |
665 | hasStartDate = date( startDate, item.data("startdate") ); | 665 | hasStartDate = date( startDate, item.data("startdate") ); |
666 | bool hasCompletedDate = false; QDate completedDate = QDate::currentDate(); | 666 | bool hasCompletedDate = false; QDate completedDate = QDate::currentDate(); |
667 | hasCompletedDate = date( completedDate, item.data("completeddate") ); | 667 | hasCompletedDate = date( completedDate, item.data("completeddate") ); |
668 | 668 | ||
669 | if ( hasStartDate ) | 669 | if ( hasStartDate ) |
670 | to.setStartDate( startDate ); | 670 | to.setStartDate( startDate ); |
671 | if ( hasCompletedDate ) | 671 | if ( hasCompletedDate ) |
672 | to.setCompletedDate( completedDate ); | 672 | to.setCompletedDate( completedDate ); |
673 | 673 | ||
674 | OPimNotifyManager& manager = to.notifiers(); | 674 | OPimNotifyManager& manager = to.notifiers(); |
675 | manager.alarmsFromString( item.data("alarms") ); | 675 | manager.alarmsFromString( item.data("alarms") ); |
676 | manager.remindersFromString( item.data("reminders") ); | 676 | manager.remindersFromString( item.data("reminders") ); |
677 | 677 | ||
678 | OPimState pimState; | 678 | OPimState pimState; |
679 | pimState.setState( QString( item.data("state") ).toInt() ); | 679 | pimState.setState( QString( item.data("state") ).toInt() ); |
680 | to.setState( pimState ); | 680 | to.setState( pimState ); |
681 | 681 | ||
682 | QMap<int, QString> recMap; | 682 | QMap<int, QString> recMap; |
683 | recMap.insert( OPimRecurrence::RType , item.data("RType") ); | 683 | recMap.insert( OPimRecurrence::RType , item.data("RType") ); |
684 | recMap.insert( OPimRecurrence::RWeekdays , item.data("RWeekdays") ); | 684 | recMap.insert( OPimRecurrence::RWeekdays , item.data("RWeekdays") ); |
685 | recMap.insert( OPimRecurrence::RPosition , item.data("RPosition") ); | 685 | recMap.insert( OPimRecurrence::RPosition , item.data("RPosition") ); |
686 | recMap.insert( OPimRecurrence::RFreq , item.data("RFreq") ); | 686 | recMap.insert( OPimRecurrence::RFreq , item.data("RFreq") ); |
687 | recMap.insert( OPimRecurrence::RHasEndDate, item.data("RHasEndDate") ); | 687 | recMap.insert( OPimRecurrence::RHasEndDate, item.data("RHasEndDate") ); |
688 | recMap.insert( OPimRecurrence::EndDate , item.data("EndDate") ); | 688 | recMap.insert( OPimRecurrence::EndDate , item.data("EndDate") ); |
689 | recMap.insert( OPimRecurrence::Created , item.data("Created") ); | 689 | recMap.insert( OPimRecurrence::Created , item.data("Created") ); |
690 | recMap.insert( OPimRecurrence::Exceptions , item.data("Exceptions") ); | 690 | recMap.insert( OPimRecurrence::Exceptions , item.data("Exceptions") ); |
691 | 691 | ||
692 | OPimRecurrence recur; | 692 | OPimRecurrence recur; |
693 | recur.fromMap( recMap ); | 693 | recur.fromMap( recMap ); |
694 | to.setRecurrence( recur ); | 694 | to.setRecurrence( recur ); |
695 | 695 | ||
696 | // 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 |
697 | to.setExtraMap( requestCustom( to.uid() ) ); | 697 | to.setExtraMap( requestCustom( to.uid() ) ); |
698 | 698 | ||
699 | return to; | 699 | return to; |
700 | } | 700 | } |
701 | OPimTodo OPimTodoAccessBackendSQL::todo( int uid )const { | 701 | OPimTodo OPimTodoAccessBackendSQL::todo( int uid )const { |
702 | FindQuery find( uid ); | 702 | FindQuery find( uid ); |
703 | return todo( m_driver->query(&find) ); | 703 | return todo( m_driver->query(&find) ); |
704 | } | 704 | } |
705 | /* | 705 | /* |
706 | * update the dict | 706 | * update the dict |
707 | */ | 707 | */ |
708 | void OPimTodoAccessBackendSQL::fillDict() { | 708 | void OPimTodoAccessBackendSQL::fillDict() { |
709 | 709 | ||
710 | #if 0 | 710 | #if 0 |
711 | /* initialize dict */ | 711 | /* initialize dict */ |
712 | /* | 712 | /* |
713 | * UPDATE dict if you change anything!!! | 713 | * UPDATE dict if you change anything!!! |
714 | * FIXME: Isn't this dict obsolete ? (eilers) | 714 | * FIXME: Isn't this dict obsolete ? (eilers) |
715 | */ | 715 | */ |
716 | m_dict.setAutoDelete( TRUE ); | 716 | m_dict.setAutoDelete( TRUE ); |
717 | m_dict.insert("Categories" , new int(OPimTodo::Category) ); | 717 | m_dict.insert("Categories" , new int(OPimTodo::Category) ); |
718 | m_dict.insert("Uid" , new int(OPimTodo::Uid) ); | 718 | m_dict.insert("Uid" , new int(OPimTodo::Uid) ); |
719 | m_dict.insert("HasDate" , new int(OPimTodo::HasDate) ); | 719 | m_dict.insert("HasDate" , new int(OPimTodo::HasDate) ); |
720 | m_dict.insert("Completed" , new int(OPimTodo::Completed) ); | 720 | m_dict.insert("Completed" , new int(OPimTodo::Completed) ); |
721 | m_dict.insert("Description" , new int(OPimTodo::Description) ); | 721 | m_dict.insert("Description" , new int(OPimTodo::Description) ); |
722 | m_dict.insert("Summary" , new int(OPimTodo::Summary) ); | 722 | m_dict.insert("Summary" , new int(OPimTodo::Summary) ); |
723 | m_dict.insert("Priority" , new int(OPimTodo::Priority) ); | 723 | m_dict.insert("Priority" , new int(OPimTodo::Priority) ); |
724 | m_dict.insert("DateDay" , new int(OPimTodo::DateDay) ); | 724 | m_dict.insert("DateDay" , new int(OPimTodo::DateDay) ); |
725 | m_dict.insert("DateMonth" , new int(OPimTodo::DateMonth) ); | 725 | m_dict.insert("DateMonth" , new int(OPimTodo::DateMonth) ); |
726 | m_dict.insert("DateYear" , new int(OPimTodo::DateYear) ); | 726 | m_dict.insert("DateYear" , new int(OPimTodo::DateYear) ); |
727 | m_dict.insert("Progress" , new int(OPimTodo::Progress) ); | 727 | m_dict.insert("Progress" , new int(OPimTodo::Progress) ); |
728 | m_dict.insert("Completed", new int(OPimTodo::Completed) ); // Why twice ? (eilers) | 728 | m_dict.insert("Completed", new int(OPimTodo::Completed) ); // Why twice ? (eilers) |
729 | m_dict.insert("CrossReference", new int(OPimTodo::CrossReference) ); | 729 | m_dict.insert("CrossReference", new int(OPimTodo::CrossReference) ); |
730 | // m_dict.insert("HasAlarmDateTime",new int(OPimTodo::HasAlarmDateTime) ); // old stuff (eilers) | 730 | // m_dict.insert("HasAlarmDateTime",new int(OPimTodo::HasAlarmDateTime) ); // old stuff (eilers) |
731 | // m_dict.insert("AlarmDateTime", new int(OPimTodo::AlarmDateTime) ); // old stuff (eilers) | 731 | // m_dict.insert("AlarmDateTime", new int(OPimTodo::AlarmDateTime) ); // old stuff (eilers) |
732 | 732 | ||
733 | #endif | 733 | #endif |
734 | } | 734 | } |
735 | /* | 735 | /* |
736 | * need to be const so let's fool the | 736 | * need to be const so let's fool the |
737 | * compiler :( | 737 | * compiler :( |
738 | */ | 738 | */ |
739 | void OPimTodoAccessBackendSQL::update()const { | 739 | void OPimTodoAccessBackendSQL::update()const { |
740 | ((OPimTodoAccessBackendSQL*)this)->m_dirty = false; | 740 | ((OPimTodoAccessBackendSQL*)this)->m_dirty = false; |
741 | LoadQuery lo; | 741 | LoadQuery lo; |
742 | OSQLResult res = m_driver->query(&lo); | 742 | OSQLResult res = m_driver->query(&lo); |
743 | if ( res.state() != OSQLResult::Success ) | 743 | if ( res.state() != OSQLResult::Success ) |
744 | return; | 744 | return; |
745 | 745 | ||
746 | ((OPimTodoAccessBackendSQL*)this)->m_uids = uids( res ); | 746 | ((OPimTodoAccessBackendSQL*)this)->m_uids = uids( res ); |
747 | } | 747 | } |
748 | QArray<int> OPimTodoAccessBackendSQL::uids( const OSQLResult& res) const{ | 748 | QArray<int> OPimTodoAccessBackendSQL::uids( const OSQLResult& res) const{ |
749 | 749 | ||
750 | OSQLResultItem::ValueList list = res.results(); | 750 | OSQLResultItem::ValueList list = res.results(); |
751 | OSQLResultItem::ValueList::Iterator it; | 751 | OSQLResultItem::ValueList::Iterator it; |
752 | QArray<int> ints(list.count() ); | 752 | QArray<int> ints(list.count() ); |
753 | qDebug(" count = %d", list.count() ); | 753 | qDebug(" count = %d", list.count() ); |
754 | 754 | ||
755 | int i = 0; | 755 | int i = 0; |
756 | for (it = list.begin(); it != list.end(); ++it ) { | 756 | for (it = list.begin(); it != list.end(); ++it ) { |
757 | ints[i] = (*it).data("uid").toInt(); | 757 | ints[i] = (*it).data("uid").toInt(); |
758 | i++; | 758 | i++; |
759 | } | 759 | } |
760 | return ints; | 760 | return ints; |
761 | } | 761 | } |
762 | 762 | ||
763 | QArray<int> OPimTodoAccessBackendSQL::matchRegexp( const QRegExp &r ) const | 763 | QArray<int> OPimTodoAccessBackendSQL::matchRegexp( const QRegExp &r ) const |
764 | { | 764 | { |
765 | 765 | ||
766 | #if 0 | 766 | #if 0 |
767 | QArray<int> empty; | 767 | QArray<int> empty; |
768 | return empty; | 768 | return empty; |
769 | 769 | ||
770 | #else | 770 | #else |
771 | QString qu = "SELECT uid FROM todolist WHERE ("; | 771 | QString qu = "SELECT uid FROM todolist WHERE ("; |
772 | 772 | ||
773 | // Do it make sense to search other fields, too ? | 773 | // Do it make sense to search other fields, too ? |
774 | qu += " rlike(\""+ r.pattern() + "\",\"description\") OR"; | 774 | qu += " rlike(\""+ r.pattern() + "\",\"description\") OR"; |
775 | qu += " rlike(\""+ r.pattern() + "\",\"summary\")"; | 775 | qu += " rlike(\""+ r.pattern() + "\",\"summary\")"; |
776 | 776 | ||
777 | qu += ")"; | 777 | qu += ")"; |
778 | 778 | ||
779 | qDebug( "query: %s", qu.latin1() ); | 779 | qDebug( "query: %s", qu.latin1() ); |
780 | 780 | ||
781 | OSQLRawQuery raw( qu ); | 781 | OSQLRawQuery raw( qu ); |
782 | OSQLResult res = m_driver->query( &raw ); | 782 | OSQLResult res = m_driver->query( &raw ); |
783 | 783 | ||
784 | return uids( res ); | 784 | return uids( res ); |
785 | 785 | ||
786 | 786 | ||
787 | #endif | 787 | #endif |
788 | 788 | ||
789 | } | 789 | } |
790 | QBitArray OPimTodoAccessBackendSQL::supports()const { | 790 | QBitArray OPimTodoAccessBackendSQL::supports()const { |
791 | 791 | ||
792 | return sup(); | 792 | return sup(); |
793 | } | 793 | } |
794 | 794 | ||
795 | QBitArray OPimTodoAccessBackendSQL::sup() const{ | 795 | QBitArray 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 | ||
808 | void OPimTodoAccessBackendSQL::removeAllCompleted(){ | 808 | void OPimTodoAccessBackendSQL::removeAllCompleted(){ |
809 | // First we need the uids from all entries which are | 809 | // First we need the uids from all entries which are |
810 | // completed. Then, we just have to remove them... | 810 | // completed. Then, we just have to remove them... |
811 | 811 | ||
812 | QString qu = "SELECT uid FROM todolist WHERE completed = 1"; | 812 | QString qu = "SELECT uid FROM todolist WHERE completed = 1"; |
813 | 813 | ||
814 | OSQLRawQuery raw( qu ); | 814 | OSQLRawQuery raw( qu ); |
815 | OSQLResult res = m_driver->query( &raw ); | 815 | OSQLResult res = m_driver->query( &raw ); |
816 | 816 | ||
817 | QArray<int> completed_uids = uids( res ); | 817 | QArray<int> completed_uids = uids( res ); |
818 | 818 | ||
819 | qDebug( "Number of completed: %d", completed_uids.size() ); | 819 | qDebug( "Number of completed: %d", completed_uids.size() ); |
820 | 820 | ||
821 | if ( completed_uids.size() == 0 ) | 821 | if ( completed_uids.size() == 0 ) |
822 | return; | 822 | return; |
823 | 823 | ||
824 | qu = "DELETE FROM todolist WHERE ("; | 824 | qu = "DELETE FROM todolist WHERE ("; |
825 | QString query; | 825 | QString query; |
826 | 826 | ||
827 | for ( int i = 0; i < completed_uids.size(); i++ ){ | 827 | for ( int i = 0; i < completed_uids.size(); i++ ){ |
828 | if ( !query.isEmpty() ) | 828 | if ( !query.isEmpty() ) |
829 | query += " OR "; | 829 | query += " OR "; |
830 | query += QString( "uid = %1" ).arg( completed_uids[i] ); | 830 | query += QString( "uid = %1" ).arg( completed_uids[i] ); |
831 | } | 831 | } |
832 | qu += query + " );"; | 832 | qu += query + " );"; |
833 | 833 | ||
834 | // Put remove of custom entries in this query to speed up.. | 834 | // Put remove of custom entries in this query to speed up.. |
835 | qu += "DELETE FORM custom_data WHERE ("; | 835 | qu += "DELETE FORM custom_data WHERE ("; |
836 | query = ""; | 836 | query = ""; |
837 | 837 | ||
838 | for ( int i = 0; i < completed_uids.size(); i++ ){ | 838 | for ( int i = 0; i < completed_uids.size(); i++ ){ |
839 | if ( !query.isEmpty() ) | 839 | if ( !query.isEmpty() ) |
840 | query += " OR "; | 840 | query += " OR "; |
841 | query += QString( "uid = %1" ).arg( completed_uids[i] ); | 841 | query += QString( "uid = %1" ).arg( completed_uids[i] ); |
842 | } | 842 | } |
843 | qu += query + " );"; | 843 | qu += query + " );"; |
844 | 844 | ||
845 | qDebug( "query: %s", qu.latin1() ); | 845 | qDebug( "query: %s", qu.latin1() ); |
846 | 846 | ||
847 | OSQLRawQuery raw2( qu ); | 847 | OSQLRawQuery raw2( qu ); |
848 | res = m_driver->query( &raw2 ); | 848 | res = m_driver->query( &raw2 ); |
849 | if ( res.state() == OSQLResult::Failure ) { | 849 | if ( res.state() == OSQLResult::Failure ) { |
850 | qWarning("OPimTodoAccessBackendSQL::removeAllCompleted():Failure in query: %s", qu.latin1() ); | 850 | qWarning("OPimTodoAccessBackendSQL::removeAllCompleted():Failure in query: %s", qu.latin1() ); |
851 | } | 851 | } |
852 | } | 852 | } |
853 | 853 | ||
854 | 854 | ||
855 | QMap<QString, QString> OPimTodoAccessBackendSQL::requestCustom( int uid ) const | 855 | QMap<QString, QString> OPimTodoAccessBackendSQL::requestCustom( int uid ) const |
856 | { | 856 | { |
857 | QMap<QString, QString> customMap; | 857 | QMap<QString, QString> customMap; |
858 | 858 | ||
859 | FindCustomQuery query( uid ); | 859 | FindCustomQuery query( uid ); |
860 | OSQLResult res_custom = m_driver->query( &query ); | 860 | OSQLResult res_custom = m_driver->query( &query ); |
861 | 861 | ||
862 | if ( res_custom.state() == OSQLResult::Failure ) { | 862 | if ( res_custom.state() == OSQLResult::Failure ) { |
863 | qWarning("OSQLResult::Failure in find query !!"); | 863 | qWarning("OSQLResult::Failure in find query !!"); |
864 | QMap<QString, QString> empty; | 864 | QMap<QString, QString> empty; |
865 | return empty; | 865 | return empty; |
866 | } | 866 | } |
867 | 867 | ||
868 | OSQLResultItem::ValueList list = res_custom.results(); | 868 | OSQLResultItem::ValueList list = res_custom.results(); |
869 | OSQLResultItem::ValueList::Iterator it = list.begin(); | 869 | OSQLResultItem::ValueList::Iterator it = list.begin(); |
870 | for ( ; it != list.end(); ++it ) { | 870 | for ( ; it != list.end(); ++it ) { |
871 | customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); | 871 | customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); |
872 | } | 872 | } |
873 | 873 | ||
874 | return customMap; | 874 | return customMap; |
875 | } | 875 | } |
876 | 876 | ||
877 | 877 | ||
878 | 878 | ||
879 | 879 | ||
880 | } | 880 | } |