-rw-r--r-- | libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp | 177 |
1 files changed, 1 insertions, 176 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp index d16d692..1ea35a8 100644 --- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp +++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp | |||
@@ -1,935 +1,760 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of the Opie Project | 2 | This file is part of the Opie Project |
3 | Copyright (C) The Main Author <main-author@whereever.org> | 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-Contact Database. | 30 | * SQL Backend for the OPIE-Contact Database. |
31 | */ | 31 | */ |
32 | 32 | ||
33 | #include "ocontactaccessbackend_sql.h" | 33 | #include "ocontactaccessbackend_sql.h" |
34 | 34 | ||
35 | #include <qarray.h> | 35 | #include <qarray.h> |
36 | #include <qdatetime.h> | 36 | #include <qdatetime.h> |
37 | #include <qstringlist.h> | 37 | #include <qstringlist.h> |
38 | 38 | ||
39 | #include <qpe/global.h> | 39 | #include <qpe/global.h> |
40 | #include <qpe/recordfields.h> | 40 | #include <qpe/recordfields.h> |
41 | 41 | ||
42 | #include <opie2/opimcontactfields.h> | 42 | #include <opie2/opimcontactfields.h> |
43 | #include <opie2/opimdateconversion.h> | 43 | #include <opie2/opimdateconversion.h> |
44 | #include <opie2/osqldriver.h> | 44 | #include <opie2/osqldriver.h> |
45 | #include <opie2/osqlresult.h> | 45 | #include <opie2/osqlresult.h> |
46 | #include <opie2/osqlmanager.h> | 46 | #include <opie2/osqlmanager.h> |
47 | #include <opie2/osqlquery.h> | 47 | #include <opie2/osqlquery.h> |
48 | 48 | ||
49 | using namespace Opie::DB; | 49 | using namespace Opie::DB; |
50 | 50 | ||
51 | 51 | ||
52 | // If defined, we use a horizontal table ( uid, attr1, attr2, attr3, ..., attrn ) instead | ||
53 | // vertical like "uid, type, value". | ||
54 | // DON'T DEACTIVATE THIS DEFINE IN PRODUCTIVE ENVIRONMENTS !! | ||
55 | #define __STORE_HORIZONTAL_ | ||
56 | |||
57 | // Distinct loading is not very fast. If I expect that every person has just | ||
58 | // one (and always one) 'Last Name', I can request all uid's for existing lastnames, | ||
59 | // which is faster.. | ||
60 | // But this may not be true for all entries, like company contacts.. | ||
61 | // The current AddressBook application handles this problem, but other may not.. (eilers) | ||
62 | #define __USE_SUPERFAST_LOADQUERY | ||
63 | |||
64 | |||
65 | /* | 52 | /* |
66 | * Implementation of used query types | 53 | * Implementation of used query types |
67 | * CREATE query | 54 | * CREATE query |
68 | * LOAD query | 55 | * LOAD query |
69 | * INSERT | 56 | * INSERT |
70 | * REMOVE | 57 | * REMOVE |
71 | * CLEAR | 58 | * CLEAR |
72 | */ | 59 | */ |
73 | namespace Opie { | 60 | namespace Opie { |
74 | /** | 61 | /** |
75 | * CreateQuery for the Todolist Table | 62 | * CreateQuery for the Todolist Table |
76 | */ | 63 | */ |
77 | class CreateQuery : public OSQLQuery { | 64 | class CreateQuery : public OSQLQuery { |
78 | public: | 65 | public: |
79 | CreateQuery(); | 66 | CreateQuery(); |
80 | ~CreateQuery(); | 67 | ~CreateQuery(); |
81 | QString query()const; | 68 | QString query()const; |
82 | }; | 69 | }; |
83 | 70 | ||
84 | /** | 71 | /** |
85 | * Clears (delete) a Table | 72 | * Clears (delete) a Table |
86 | */ | 73 | */ |
87 | class ClearQuery : public OSQLQuery { | 74 | class ClearQuery : public OSQLQuery { |
88 | public: | 75 | public: |
89 | ClearQuery(); | 76 | ClearQuery(); |
90 | ~ClearQuery(); | 77 | ~ClearQuery(); |
91 | QString query()const; | 78 | QString query()const; |
92 | 79 | ||
93 | }; | 80 | }; |
94 | 81 | ||
95 | 82 | ||
96 | /** | 83 | /** |
97 | * LoadQuery | 84 | * LoadQuery |
98 | * this one queries for all uids | 85 | * this one queries for all uids |
99 | */ | 86 | */ |
100 | class LoadQuery : public OSQLQuery { | 87 | class LoadQuery : public OSQLQuery { |
101 | public: | 88 | public: |
102 | LoadQuery(); | 89 | LoadQuery(); |
103 | ~LoadQuery(); | 90 | ~LoadQuery(); |
104 | QString query()const; | 91 | QString query()const; |
105 | }; | 92 | }; |
106 | 93 | ||
107 | /** | 94 | /** |
108 | * inserts/adds a OPimContact to the table | 95 | * inserts/adds a OPimContact to the table |
109 | */ | 96 | */ |
110 | class InsertQuery : public OSQLQuery { | 97 | class InsertQuery : public OSQLQuery { |
111 | public: | 98 | public: |
112 | InsertQuery(const OPimContact& ); | 99 | InsertQuery(const OPimContact& ); |
113 | ~InsertQuery(); | 100 | ~InsertQuery(); |
114 | QString query()const; | 101 | QString query()const; |
115 | private: | 102 | private: |
116 | OPimContact m_contact; | 103 | OPimContact m_contact; |
117 | }; | 104 | }; |
118 | 105 | ||
119 | 106 | ||
120 | /** | 107 | /** |
121 | * removes one from the table | 108 | * removes one from the table |
122 | */ | 109 | */ |
123 | class RemoveQuery : public OSQLQuery { | 110 | class RemoveQuery : public OSQLQuery { |
124 | public: | 111 | public: |
125 | RemoveQuery(int uid ); | 112 | RemoveQuery(int uid ); |
126 | ~RemoveQuery(); | 113 | ~RemoveQuery(); |
127 | QString query()const; | 114 | QString query()const; |
128 | private: | 115 | private: |
129 | int m_uid; | 116 | int m_uid; |
130 | }; | 117 | }; |
131 | 118 | ||
132 | /** | 119 | /** |
133 | * a find query for noncustom elements | 120 | * a find query for noncustom elements |
134 | */ | 121 | */ |
135 | class FindQuery : public OSQLQuery { | 122 | class FindQuery : public OSQLQuery { |
136 | public: | 123 | public: |
137 | FindQuery(int uid); | 124 | FindQuery(int uid); |
138 | FindQuery(const QArray<int>& ); | 125 | FindQuery(const QArray<int>& ); |
139 | ~FindQuery(); | 126 | ~FindQuery(); |
140 | QString query()const; | 127 | QString query()const; |
141 | private: | 128 | private: |
142 | QString single()const; | 129 | QString single()const; |
143 | QString multi()const; | 130 | QString multi()const; |
144 | QArray<int> m_uids; | 131 | QArray<int> m_uids; |
145 | int m_uid; | 132 | int m_uid; |
146 | }; | 133 | }; |
147 | 134 | ||
148 | /** | 135 | /** |
149 | * a find query for custom elements | 136 | * a find query for custom elements |
150 | */ | 137 | */ |
151 | class FindCustomQuery : public OSQLQuery { | 138 | class FindCustomQuery : public OSQLQuery { |
152 | public: | 139 | public: |
153 | FindCustomQuery(int uid); | 140 | FindCustomQuery(int uid); |
154 | FindCustomQuery(const QArray<int>& ); | 141 | FindCustomQuery(const QArray<int>& ); |
155 | ~FindCustomQuery(); | 142 | ~FindCustomQuery(); |
156 | QString query()const; | 143 | QString query()const; |
157 | private: | 144 | private: |
158 | QString single()const; | 145 | QString single()const; |
159 | QString multi()const; | 146 | QString multi()const; |
160 | QArray<int> m_uids; | 147 | QArray<int> m_uids; |
161 | int m_uid; | 148 | int m_uid; |
162 | }; | 149 | }; |
163 | 150 | ||
164 | 151 | ||
165 | 152 | ||
166 | // We using three tables to store the information: | 153 | // We using three tables to store the information: |
167 | // 1. addressbook : It contains General information about the contact (non custom) | 154 | // 1. addressbook : It contains General information about the contact (non custom) |
168 | // 2. custom_data : Not official supported entries | 155 | // 2. custom_data : Not official supported entries |
169 | // All tables are connected by the uid of the contact. | 156 | // All tables are connected by the uid of the contact. |
170 | // Maybe I should add a table for meta-information ? | 157 | // Maybe I should add a table for meta-information ? |
171 | CreateQuery::CreateQuery() : OSQLQuery() {} | 158 | CreateQuery::CreateQuery() : OSQLQuery() {} |
172 | CreateQuery::~CreateQuery() {} | 159 | CreateQuery::~CreateQuery() {} |
173 | QString CreateQuery::query()const { | 160 | QString CreateQuery::query()const { |
174 | QString qu; | 161 | QString qu; |
175 | #ifdef __STORE_HORIZONTAL_ | ||
176 | 162 | ||
177 | qu += "create table addressbook( uid PRIMARY KEY "; | 163 | qu += "create table addressbook( uid PRIMARY KEY "; |
178 | 164 | ||
179 | QStringList fieldList = OPimContactFields::untrfields( false ); | 165 | QStringList fieldList = OPimContactFields::untrfields( false ); |
180 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ | 166 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ |
181 | qu += QString( ",\"%1\" VARCHAR(10)" ).arg( *it ); | 167 | qu += QString( ",\"%1\" VARCHAR(10)" ).arg( *it ); |
182 | } | 168 | } |
183 | qu += " );"; | 169 | qu += " );"; |
184 | 170 | ||
185 | qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );"; | 171 | qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );"; |
186 | 172 | ||
187 | #else | ||
188 | |||
189 | qu += "create table addressbook( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id));"; | ||
190 | qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );"; | ||
191 | // qu += "create table dates( uid PRIMARY KEY, type, day, month, year, hour, minute, second );"; | ||
192 | |||
193 | #endif // __STORE_HORIZONTAL_ | ||
194 | return qu; | 173 | return qu; |
195 | } | 174 | } |
196 | 175 | ||
197 | ClearQuery::ClearQuery() | 176 | ClearQuery::ClearQuery() |
198 | : OSQLQuery() {} | 177 | : OSQLQuery() {} |
199 | ClearQuery::~ClearQuery() {} | 178 | ClearQuery::~ClearQuery() {} |
200 | QString ClearQuery::query()const { | 179 | QString ClearQuery::query()const { |
201 | QString qu = "drop table addressbook;"; | 180 | QString qu = "drop table addressbook;"; |
202 | qu += "drop table custom_data;"; | 181 | qu += "drop table custom_data;"; |
203 | // qu += "drop table dates;"; | 182 | // qu += "drop table dates;"; |
204 | return qu; | 183 | return qu; |
205 | } | 184 | } |
206 | 185 | ||
207 | 186 | ||
208 | LoadQuery::LoadQuery() : OSQLQuery() {} | 187 | LoadQuery::LoadQuery() : OSQLQuery() {} |
209 | LoadQuery::~LoadQuery() {} | 188 | LoadQuery::~LoadQuery() {} |
210 | QString LoadQuery::query()const { | 189 | QString LoadQuery::query()const { |
211 | QString qu; | 190 | QString qu; |
212 | #ifdef __STORE_HORIZONTAL_ | ||
213 | qu += "select uid from addressbook"; | 191 | qu += "select uid from addressbook"; |
214 | #else | ||
215 | # ifndef __USE_SUPERFAST_LOADQUERY | ||
216 | qu += "select distinct uid from addressbook"; | ||
217 | # else | ||
218 | qu += "select uid from addressbook where type = 'Last Name'"; | ||
219 | # endif // __USE_SUPERFAST_LOADQUERY | ||
220 | #endif // __STORE_HORIZONTAL_ | ||
221 | 192 | ||
222 | return qu; | 193 | return qu; |
223 | } | 194 | } |
224 | 195 | ||
225 | 196 | ||
226 | InsertQuery::InsertQuery( const OPimContact& contact ) | 197 | InsertQuery::InsertQuery( const OPimContact& contact ) |
227 | : OSQLQuery(), m_contact( contact ) { | 198 | : OSQLQuery(), m_contact( contact ) { |
228 | } | 199 | } |
229 | 200 | ||
230 | InsertQuery::~InsertQuery() { | 201 | InsertQuery::~InsertQuery() { |
231 | } | 202 | } |
232 | 203 | ||
233 | /* | 204 | /* |
234 | * converts from a OPimContact to a query | 205 | * converts from a OPimContact to a query |
235 | */ | 206 | */ |
236 | QString InsertQuery::query()const{ | 207 | QString InsertQuery::query()const{ |
237 | 208 | ||
238 | #ifdef __STORE_HORIZONTAL_ | ||
239 | QString qu; | 209 | QString qu; |
240 | qu += "insert into addressbook VALUES( " + | 210 | qu += "insert into addressbook VALUES( " + |
241 | QString::number( m_contact.uid() ); | 211 | QString::number( m_contact.uid() ); |
242 | 212 | ||
243 | // Get all information out of the contact-class | 213 | // Get all information out of the contact-class |
244 | // Remember: The category is stored in contactMap, too ! | 214 | // Remember: The category is stored in contactMap, too ! |
245 | QMap<int, QString> contactMap = m_contact.toMap(); | 215 | QMap<int, QString> contactMap = m_contact.toMap(); |
246 | 216 | ||
247 | QStringList fieldList = OPimContactFields::untrfields( false ); | 217 | QStringList fieldList = OPimContactFields::untrfields( false ); |
248 | QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); | 218 | QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); |
249 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ | 219 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ |
250 | // Convert Column-String to Id and get value for this id.. | 220 | // Convert Column-String to Id and get value for this id.. |
251 | // Hmmm.. Maybe not very cute solution.. | 221 | // Hmmm.. Maybe not very cute solution.. |
252 | int id = translate[*it]; | 222 | int id = translate[*it]; |
253 | switch ( id ){ | 223 | switch ( id ){ |
254 | case Qtopia::Birthday:{ | 224 | case Qtopia::Birthday:{ |
255 | // These entries should stored in a special format | 225 | // These entries should stored in a special format |
256 | // year-month-day | 226 | // year-month-day |
257 | QDate day = m_contact.birthday(); | 227 | QDate day = m_contact.birthday(); |
258 | if ( day.isValid() ){ | 228 | if ( day.isValid() ){ |
259 | qu += QString(",\"%1-%2-%3\"") | 229 | qu += QString(",\"%1-%2-%3\"") |
260 | .arg( day.year() ) | 230 | .arg( day.year() ) |
261 | .arg( day.month() ) | 231 | .arg( day.month() ) |
262 | .arg( day.day() ); | 232 | .arg( day.day() ); |
263 | } else { | 233 | } else { |
264 | qu += ",\"\""; | 234 | qu += ",\"\""; |
265 | } | 235 | } |
266 | } | 236 | } |
267 | break; | 237 | break; |
268 | case Qtopia::Anniversary:{ | 238 | case Qtopia::Anniversary:{ |
269 | // These entries should stored in a special format | 239 | // These entries should stored in a special format |
270 | // year-month-day | 240 | // year-month-day |
271 | QDate day = m_contact.anniversary(); | 241 | QDate day = m_contact.anniversary(); |
272 | if ( day.isValid() ){ | 242 | if ( day.isValid() ){ |
273 | qu += QString(",\"%1-%2-%3\"") | 243 | qu += QString(",\"%1-%2-%3\"") |
274 | .arg( day.year() ) | 244 | .arg( day.year() ) |
275 | .arg( day.month() ) | 245 | .arg( day.month() ) |
276 | .arg( day.day() ); | 246 | .arg( day.day() ); |
277 | } else { | 247 | } else { |
278 | qu += ",\"\""; | 248 | qu += ",\"\""; |
279 | } | 249 | } |
280 | } | 250 | } |
281 | break; | 251 | break; |
282 | 252 | ||
283 | default: | 253 | default: |
284 | qu += QString( ",\"%1\"" ).arg( contactMap[id] ); | 254 | qu += QString( ",\"%1\"" ).arg( contactMap[id] ); |
285 | } | 255 | } |
286 | } | 256 | } |
287 | qu += " );"; | 257 | qu += " );"; |
288 | 258 | ||
289 | 259 | ||
290 | #else | ||
291 | // Get all information out of the contact-class | ||
292 | // Remember: The category is stored in contactMap, too ! | ||
293 | QMap<int, QString> contactMap = m_contact.toMap(); | ||
294 | |||
295 | QMap<QString, QString> addressbook_db; | ||
296 | |||
297 | // Get the translation from the ID to the String | ||
298 | QMap<int, QString> transMap = OPimContactFields::idToUntrFields(); | ||
299 | |||
300 | for( QMap<int, QString>::Iterator it = contactMap.begin(); | ||
301 | it != contactMap.end(); ++it ){ | ||
302 | switch ( it.key() ){ | ||
303 | case Qtopia::Birthday:{ | ||
304 | // These entries should stored in a special format | ||
305 | // year-month-day | ||
306 | QDate day = m_contact.birthday(); | ||
307 | addressbook_db.insert( transMap[it.key()], | ||
308 | QString("%1-%2-%3") | ||
309 | .arg( day.year() ) | ||
310 | .arg( day.month() ) | ||
311 | .arg( day.day() ) ); | ||
312 | } | ||
313 | break; | ||
314 | case Qtopia::Anniversary:{ | ||
315 | // These entries should stored in a special format | ||
316 | // year-month-day | ||
317 | QDate day = m_contact.anniversary(); | ||
318 | addressbook_db.insert( transMap[it.key()], | ||
319 | QString("%1-%2-%3") | ||
320 | .arg( day.year() ) | ||
321 | .arg( day.month() ) | ||
322 | .arg( day.day() ) ); | ||
323 | } | ||
324 | break; | ||
325 | case Qtopia::AddressUid: // Ignore UID | ||
326 | break; | ||
327 | default: // Translate id to String | ||
328 | addressbook_db.insert( transMap[it.key()], it.data() ); | ||
329 | break; | ||
330 | } | ||
331 | |||
332 | } | ||
333 | |||
334 | // Now convert this whole stuff into a SQL String, beginning with | ||
335 | // the addressbook table.. | ||
336 | QString qu; | ||
337 | // qu += "begin transaction;"; | ||
338 | int id = 0; | ||
339 | for( QMap<QString, QString>::Iterator it = addressbook_db.begin(); | ||
340 | it != addressbook_db.end(); ++it ){ | ||
341 | qu += "insert into addressbook VALUES(" | ||
342 | + QString::number( m_contact.uid() ) | ||
343 | + "," | ||
344 | + QString::number( id++ ) | ||
345 | + ",'" | ||
346 | + it.key() //.latin1() | ||
347 | + "'," | ||
348 | + "0" // Priority for future enhancements | ||
349 | + ",'" | ||
350 | + it.data() //.latin1() | ||
351 | + "');"; | ||
352 | } | ||
353 | |||
354 | #endif //__STORE_HORIZONTAL_ | ||
355 | // Now add custom data.. | 260 | // Now add custom data.. |
356 | #ifdef __STORE_HORIZONTAL_ | ||
357 | int id = 0; | 261 | int id = 0; |
358 | #endif | ||
359 | id = 0; | 262 | id = 0; |
360 | QMap<QString, QString> customMap = m_contact.toExtraMap(); | 263 | QMap<QString, QString> customMap = m_contact.toExtraMap(); |
361 | for( QMap<QString, QString>::Iterator it = customMap.begin(); | 264 | for( QMap<QString, QString>::Iterator it = customMap.begin(); |
362 | it != customMap.end(); ++it ){ | 265 | it != customMap.end(); ++it ){ |
363 | qu += "insert into custom_data VALUES(" | 266 | qu += "insert into custom_data VALUES(" |
364 | + QString::number( m_contact.uid() ) | 267 | + QString::number( m_contact.uid() ) |
365 | + "," | 268 | + "," |
366 | + QString::number( id++ ) | 269 | + QString::number( id++ ) |
367 | + ",'" | 270 | + ",'" |
368 | + it.key() //.latin1() | 271 | + it.key() //.latin1() |
369 | + "'," | 272 | + "'," |
370 | + "0" // Priority for future enhancements | 273 | + "0" // Priority for future enhancements |
371 | + ",'" | 274 | + ",'" |
372 | + it.data() //.latin1() | 275 | + it.data() //.latin1() |
373 | + "');"; | 276 | + "');"; |
374 | } | 277 | } |
375 | // qu += "commit;"; | 278 | // qu += "commit;"; |
376 | qWarning("add %s", qu.latin1() ); | 279 | qWarning("add %s", qu.latin1() ); |
377 | return qu; | 280 | return qu; |
378 | } | 281 | } |
379 | 282 | ||
380 | 283 | ||
381 | RemoveQuery::RemoveQuery(int uid ) | 284 | RemoveQuery::RemoveQuery(int uid ) |
382 | : OSQLQuery(), m_uid( uid ) {} | 285 | : OSQLQuery(), m_uid( uid ) {} |
383 | RemoveQuery::~RemoveQuery() {} | 286 | RemoveQuery::~RemoveQuery() {} |
384 | QString RemoveQuery::query()const { | 287 | QString RemoveQuery::query()const { |
385 | QString qu = "DELETE from addressbook where uid = " | 288 | QString qu = "DELETE from addressbook where uid = " |
386 | + QString::number(m_uid) + ";"; | 289 | + QString::number(m_uid) + ";"; |
387 | qu += "DELETE from custom_data where uid = " | 290 | qu += "DELETE from custom_data where uid = " |
388 | + QString::number(m_uid) + ";"; | 291 | + QString::number(m_uid) + ";"; |
389 | return qu; | 292 | return qu; |
390 | } | 293 | } |
391 | 294 | ||
392 | 295 | ||
393 | 296 | ||
394 | 297 | ||
395 | FindQuery::FindQuery(int uid) | 298 | FindQuery::FindQuery(int uid) |
396 | : OSQLQuery(), m_uid( uid ) { | 299 | : OSQLQuery(), m_uid( uid ) { |
397 | } | 300 | } |
398 | FindQuery::FindQuery(const QArray<int>& ints) | 301 | FindQuery::FindQuery(const QArray<int>& ints) |
399 | : OSQLQuery(), m_uids( ints ){ | 302 | : OSQLQuery(), m_uids( ints ){ |
400 | } | 303 | } |
401 | FindQuery::~FindQuery() { | 304 | FindQuery::~FindQuery() { |
402 | } | 305 | } |
403 | QString FindQuery::query()const{ | 306 | QString FindQuery::query()const{ |
404 | // if ( m_uids.count() == 0 ) | 307 | // if ( m_uids.count() == 0 ) |
405 | return single(); | 308 | return single(); |
406 | } | 309 | } |
407 | /* | 310 | /* |
408 | else | 311 | else |
409 | return multi(); | 312 | return multi(); |
410 | } | 313 | } |
411 | QString FindQuery::multi()const { | 314 | QString FindQuery::multi()const { |
412 | QString qu = "select uid, type, value from addressbook where"; | 315 | QString qu = "select uid, type, value from addressbook where"; |
413 | for (uint i = 0; i < m_uids.count(); i++ ) { | 316 | for (uint i = 0; i < m_uids.count(); i++ ) { |
414 | qu += " UID = " + QString::number( m_uids[i] ) + " OR"; | 317 | qu += " UID = " + QString::number( m_uids[i] ) + " OR"; |
415 | } | 318 | } |
416 | qu.remove( qu.length()-2, 2 ); // Hmmmm.. | 319 | qu.remove( qu.length()-2, 2 ); // Hmmmm.. |
417 | return qu; | 320 | return qu; |
418 | } | 321 | } |
419 | */ | 322 | */ |
420 | #ifdef __STORE_HORIZONTAL_ | ||
421 | QString FindQuery::single()const{ | 323 | QString FindQuery::single()const{ |
422 | QString qu = "select *"; | 324 | QString qu = "select *"; |
423 | qu += " from addressbook where uid = " + QString::number(m_uid); | 325 | qu += " from addressbook where uid = " + QString::number(m_uid); |
424 | 326 | ||
425 | // qWarning("find query: %s", qu.latin1() ); | 327 | // qWarning("find query: %s", qu.latin1() ); |
426 | return qu; | 328 | return qu; |
427 | } | 329 | } |
428 | #else | ||
429 | QString FindQuery::single()const{ | ||
430 | QString qu = "select uid, type, value from addressbook where uid = "; | ||
431 | qu += QString::number(m_uid); | ||
432 | return qu; | ||
433 | } | ||
434 | #endif | ||
435 | 330 | ||
436 | 331 | ||
437 | FindCustomQuery::FindCustomQuery(int uid) | 332 | FindCustomQuery::FindCustomQuery(int uid) |
438 | : OSQLQuery(), m_uid( uid ) { | 333 | : OSQLQuery(), m_uid( uid ) { |
439 | } | 334 | } |
440 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) | 335 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) |
441 | : OSQLQuery(), m_uids( ints ){ | 336 | : OSQLQuery(), m_uids( ints ){ |
442 | } | 337 | } |
443 | FindCustomQuery::~FindCustomQuery() { | 338 | FindCustomQuery::~FindCustomQuery() { |
444 | } | 339 | } |
445 | QString FindCustomQuery::query()const{ | 340 | QString FindCustomQuery::query()const{ |
446 | // if ( m_uids.count() == 0 ) | 341 | // if ( m_uids.count() == 0 ) |
447 | return single(); | 342 | return single(); |
448 | } | 343 | } |
449 | QString FindCustomQuery::single()const{ | 344 | QString FindCustomQuery::single()const{ |
450 | QString qu = "select uid, type, value from custom_data where uid = "; | 345 | QString qu = "select uid, type, value from custom_data where uid = "; |
451 | qu += QString::number(m_uid); | 346 | qu += QString::number(m_uid); |
452 | return qu; | 347 | return qu; |
453 | } | 348 | } |
454 | 349 | ||
455 | }; | 350 | }; |
456 | 351 | ||
457 | 352 | ||
458 | /* --------------------------------------------------------------------------- */ | 353 | /* --------------------------------------------------------------------------- */ |
459 | 354 | ||
460 | namespace Opie { | 355 | namespace Opie { |
461 | 356 | ||
462 | OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* appname */, | 357 | OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* appname */, |
463 | const QString& filename ): | 358 | const QString& filename ): |
464 | OPimContactAccessBackend(), m_changed(false), m_driver( NULL ) | 359 | OPimContactAccessBackend(), m_changed(false), m_driver( NULL ) |
465 | { | 360 | { |
466 | qWarning("C'tor OPimContactAccessBackend_SQL starts"); | 361 | qWarning("C'tor OPimContactAccessBackend_SQL starts"); |
467 | QTime t; | 362 | QTime t; |
468 | t.start(); | 363 | t.start(); |
469 | 364 | ||
470 | /* Expecting to access the default filename if nothing else is set */ | 365 | /* Expecting to access the default filename if nothing else is set */ |
471 | if ( filename.isEmpty() ){ | 366 | if ( filename.isEmpty() ){ |
472 | m_fileName = Global::applicationFileName( "addressbook","addressbook.db" ); | 367 | m_fileName = Global::applicationFileName( "addressbook","addressbook.db" ); |
473 | } else | 368 | } else |
474 | m_fileName = filename; | 369 | m_fileName = filename; |
475 | 370 | ||
476 | // Get the standart sql-driver from the OSQLManager.. | 371 | // Get the standart sql-driver from the OSQLManager.. |
477 | OSQLManager man; | 372 | OSQLManager man; |
478 | m_driver = man.standard(); | 373 | m_driver = man.standard(); |
479 | m_driver->setUrl( m_fileName ); | 374 | m_driver->setUrl( m_fileName ); |
480 | 375 | ||
481 | load(); | 376 | load(); |
482 | 377 | ||
483 | qWarning("C'tor OPimContactAccessBackend_SQL ends: %d ms", t.elapsed() ); | 378 | qWarning("C'tor OPimContactAccessBackend_SQL ends: %d ms", t.elapsed() ); |
484 | } | 379 | } |
485 | 380 | ||
486 | OPimContactAccessBackend_SQL::~OPimContactAccessBackend_SQL () | 381 | OPimContactAccessBackend_SQL::~OPimContactAccessBackend_SQL () |
487 | { | 382 | { |
488 | if( m_driver ) | 383 | if( m_driver ) |
489 | delete m_driver; | 384 | delete m_driver; |
490 | } | 385 | } |
491 | 386 | ||
492 | bool OPimContactAccessBackend_SQL::load () | 387 | bool OPimContactAccessBackend_SQL::load () |
493 | { | 388 | { |
494 | if (!m_driver->open() ) | 389 | if (!m_driver->open() ) |
495 | return false; | 390 | return false; |
496 | 391 | ||
497 | // Don't expect that the database exists. | 392 | // Don't expect that the database exists. |
498 | // It is save here to create the table, even if it | 393 | // It is save here to create the table, even if it |
499 | // do exist. ( Is that correct for all databases ?? ) | 394 | // do exist. ( Is that correct for all databases ?? ) |
500 | CreateQuery creat; | 395 | CreateQuery creat; |
501 | OSQLResult res = m_driver->query( &creat ); | 396 | OSQLResult res = m_driver->query( &creat ); |
502 | 397 | ||
503 | update(); | 398 | update(); |
504 | 399 | ||
505 | return true; | 400 | return true; |
506 | 401 | ||
507 | } | 402 | } |
508 | 403 | ||
509 | bool OPimContactAccessBackend_SQL::reload() | 404 | bool OPimContactAccessBackend_SQL::reload() |
510 | { | 405 | { |
511 | return load(); | 406 | return load(); |
512 | } | 407 | } |
513 | 408 | ||
514 | bool OPimContactAccessBackend_SQL::save() | 409 | bool OPimContactAccessBackend_SQL::save() |
515 | { | 410 | { |
516 | return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) | 411 | return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) |
517 | } | 412 | } |
518 | 413 | ||
519 | 414 | ||
520 | void OPimContactAccessBackend_SQL::clear () | 415 | void OPimContactAccessBackend_SQL::clear () |
521 | { | 416 | { |
522 | ClearQuery cle; | 417 | ClearQuery cle; |
523 | OSQLResult res = m_driver->query( &cle ); | 418 | OSQLResult res = m_driver->query( &cle ); |
524 | 419 | ||
525 | reload(); | 420 | reload(); |
526 | } | 421 | } |
527 | 422 | ||
528 | bool OPimContactAccessBackend_SQL::wasChangedExternally() | 423 | bool OPimContactAccessBackend_SQL::wasChangedExternally() |
529 | { | 424 | { |
530 | return false; | 425 | return false; |
531 | } | 426 | } |
532 | 427 | ||
533 | QArray<int> OPimContactAccessBackend_SQL::allRecords() const | 428 | QArray<int> OPimContactAccessBackend_SQL::allRecords() const |
534 | { | 429 | { |
535 | 430 | ||
536 | // FIXME: Think about cute handling of changed tables.. | 431 | // FIXME: Think about cute handling of changed tables.. |
537 | // Thus, we don't have to call update here... | 432 | // Thus, we don't have to call update here... |
538 | if ( m_changed ) | 433 | if ( m_changed ) |
539 | ((OPimContactAccessBackend_SQL*)this)->update(); | 434 | ((OPimContactAccessBackend_SQL*)this)->update(); |
540 | 435 | ||
541 | return m_uids; | 436 | return m_uids; |
542 | } | 437 | } |
543 | 438 | ||
544 | bool OPimContactAccessBackend_SQL::add ( const OPimContact &newcontact ) | 439 | bool OPimContactAccessBackend_SQL::add ( const OPimContact &newcontact ) |
545 | { | 440 | { |
546 | InsertQuery ins( newcontact ); | 441 | InsertQuery ins( newcontact ); |
547 | OSQLResult res = m_driver->query( &ins ); | 442 | OSQLResult res = m_driver->query( &ins ); |
548 | 443 | ||
549 | if ( res.state() == OSQLResult::Failure ) | 444 | if ( res.state() == OSQLResult::Failure ) |
550 | return false; | 445 | return false; |
551 | 446 | ||
552 | int c = m_uids.count(); | 447 | int c = m_uids.count(); |
553 | m_uids.resize( c+1 ); | 448 | m_uids.resize( c+1 ); |
554 | m_uids[c] = newcontact.uid(); | 449 | m_uids[c] = newcontact.uid(); |
555 | 450 | ||
556 | return true; | 451 | return true; |
557 | } | 452 | } |
558 | 453 | ||
559 | 454 | ||
560 | bool OPimContactAccessBackend_SQL::remove ( int uid ) | 455 | bool OPimContactAccessBackend_SQL::remove ( int uid ) |
561 | { | 456 | { |
562 | RemoveQuery rem( uid ); | 457 | RemoveQuery rem( uid ); |
563 | OSQLResult res = m_driver->query(&rem ); | 458 | OSQLResult res = m_driver->query(&rem ); |
564 | 459 | ||
565 | if ( res.state() == OSQLResult::Failure ) | 460 | if ( res.state() == OSQLResult::Failure ) |
566 | return false; | 461 | return false; |
567 | 462 | ||
568 | m_changed = true; | 463 | m_changed = true; |
569 | 464 | ||
570 | return true; | 465 | return true; |
571 | } | 466 | } |
572 | 467 | ||
573 | bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact ) | 468 | bool OPimContactAccessBackend_SQL::replace ( const OPimContact &contact ) |
574 | { | 469 | { |
575 | if ( !remove( contact.uid() ) ) | 470 | if ( !remove( contact.uid() ) ) |
576 | return false; | 471 | return false; |
577 | 472 | ||
578 | return add( contact ); | 473 | return add( contact ); |
579 | } | 474 | } |
580 | 475 | ||
581 | 476 | ||
582 | OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const | 477 | OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const |
583 | { | 478 | { |
584 | qWarning("OPimContactAccessBackend_SQL::find()"); | 479 | qWarning("OPimContactAccessBackend_SQL::find()"); |
585 | QTime t; | 480 | QTime t; |
586 | t.start(); | 481 | t.start(); |
587 | 482 | ||
588 | OPimContact retContact( requestNonCustom( uid ) ); | 483 | OPimContact retContact( requestNonCustom( uid ) ); |
589 | retContact.setExtraMap( requestCustom( uid ) ); | 484 | retContact.setExtraMap( requestCustom( uid ) ); |
590 | 485 | ||
591 | qWarning("OPimContactAccessBackend_SQL::find() needed: %d ms", t.elapsed() ); | 486 | qWarning("OPimContactAccessBackend_SQL::find() needed: %d ms", t.elapsed() ); |
592 | return retContact; | 487 | return retContact; |
593 | } | 488 | } |
594 | 489 | ||
595 | 490 | ||
596 | 491 | ||
597 | QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& d = QDateTime() ) | 492 | QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& d = QDateTime() ) |
598 | { | 493 | { |
599 | QString qu = "SELECT uid FROM addressbook WHERE"; | 494 | QString qu = "SELECT uid FROM addressbook WHERE"; |
600 | 495 | ||
601 | QMap<int, QString> queryFields = query.toMap(); | 496 | QMap<int, QString> queryFields = query.toMap(); |
602 | QStringList fieldList = OPimContactFields::untrfields( false ); | 497 | QStringList fieldList = OPimContactFields::untrfields( false ); |
603 | QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); | 498 | QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); |
604 | 499 | ||
605 | // Convert every filled field to a SQL-Query | 500 | // Convert every filled field to a SQL-Query |
606 | bool isAnyFieldSelected = false; | 501 | bool isAnyFieldSelected = false; |
607 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ | 502 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ |
608 | int id = translate[*it]; | 503 | int id = translate[*it]; |
609 | QString queryStr = queryFields[id]; | 504 | QString queryStr = queryFields[id]; |
610 | if ( !queryStr.isEmpty() ){ | 505 | if ( !queryStr.isEmpty() ){ |
611 | isAnyFieldSelected = true; | 506 | isAnyFieldSelected = true; |
612 | switch( id ){ | 507 | switch( id ){ |
613 | default: | 508 | default: |
614 | // Switching between case sensitive and insensitive... | 509 | // Switching between case sensitive and insensitive... |
615 | // LIKE is not case sensitive, GLOB is case sensitive | 510 | // LIKE is not case sensitive, GLOB is case sensitive |
616 | // Do exist a better solution to switch this ? | 511 | // Do exist a better solution to switch this ? |
617 | if ( settings & OPimContactAccess::IgnoreCase ) | 512 | if ( settings & OPimContactAccess::IgnoreCase ) |
618 | qu += "(\"" + *it + "\"" + " LIKE " + "'" | 513 | qu += "(\"" + *it + "\"" + " LIKE " + "'" |
619 | + queryStr.replace(QRegExp("\\*"),"%") + "'" + ") AND "; | 514 | + queryStr.replace(QRegExp("\\*"),"%") + "'" + ") AND "; |
620 | else | 515 | else |
621 | qu += "(\"" + *it + "\"" + " GLOB " + "'" | 516 | qu += "(\"" + *it + "\"" + " GLOB " + "'" |
622 | + queryStr + "'" + ") AND "; | 517 | + queryStr + "'" + ") AND "; |
623 | 518 | ||
624 | } | 519 | } |
625 | } | 520 | } |
626 | } | 521 | } |
627 | // Skip trailing "AND" | 522 | // Skip trailing "AND" |
628 | if ( isAnyFieldSelected ) | 523 | if ( isAnyFieldSelected ) |
629 | qu = qu.left( qu.length() - 4 ); | 524 | qu = qu.left( qu.length() - 4 ); |
630 | 525 | ||
631 | qWarning( "queryByExample query: %s", qu.latin1() ); | 526 | qWarning( "queryByExample query: %s", qu.latin1() ); |
632 | 527 | ||
633 | // Execute query and return the received uid's | 528 | // Execute query and return the received uid's |
634 | OSQLRawQuery raw( qu ); | 529 | OSQLRawQuery raw( qu ); |
635 | OSQLResult res = m_driver->query( &raw ); | 530 | OSQLResult res = m_driver->query( &raw ); |
636 | if ( res.state() != OSQLResult::Success ){ | 531 | if ( res.state() != OSQLResult::Success ){ |
637 | QArray<int> empty; | 532 | QArray<int> empty; |
638 | return empty; | 533 | return empty; |
639 | } | 534 | } |
640 | 535 | ||
641 | QArray<int> list = extractUids( res ); | 536 | QArray<int> list = extractUids( res ); |
642 | 537 | ||
643 | return list; | 538 | return list; |
644 | } | 539 | } |
645 | 540 | ||
646 | QArray<int> OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const | 541 | QArray<int> OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const |
647 | { | 542 | { |
648 | QArray<int> nix(0); | 543 | QArray<int> nix(0); |
649 | return nix; | 544 | return nix; |
650 | } | 545 | } |
651 | 546 | ||
652 | const uint OPimContactAccessBackend_SQL::querySettings() | 547 | const uint OPimContactAccessBackend_SQL::querySettings() |
653 | { | 548 | { |
654 | return OPimContactAccess::IgnoreCase | 549 | return OPimContactAccess::IgnoreCase |
655 | || OPimContactAccess::WildCards; | 550 | || OPimContactAccess::WildCards; |
656 | } | 551 | } |
657 | 552 | ||
658 | bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const | 553 | bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const |
659 | { | 554 | { |
660 | /* OPimContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay | 555 | /* OPimContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay |
661 | * may be added with any of the other settings. IgnoreCase should never used alone. | 556 | * may be added with any of the other settings. IgnoreCase should never used alone. |
662 | * Wildcards, RegExp, ExactMatch should never used at the same time... | 557 | * Wildcards, RegExp, ExactMatch should never used at the same time... |
663 | */ | 558 | */ |
664 | 559 | ||
665 | // Step 1: Check whether the given settings are supported by this backend | 560 | // Step 1: Check whether the given settings are supported by this backend |
666 | if ( ( querySettings & ( | 561 | if ( ( querySettings & ( |
667 | OPimContactAccess::IgnoreCase | 562 | OPimContactAccess::IgnoreCase |
668 | | OPimContactAccess::WildCards | 563 | | OPimContactAccess::WildCards |
669 | // | OPimContactAccess::DateDiff | 564 | // | OPimContactAccess::DateDiff |
670 | // | OPimContactAccess::DateYear | 565 | // | OPimContactAccess::DateYear |
671 | // | OPimContactAccess::DateMonth | 566 | // | OPimContactAccess::DateMonth |
672 | // | OPimContactAccess::DateDay | 567 | // | OPimContactAccess::DateDay |
673 | // | OPimContactAccess::RegExp | 568 | // | OPimContactAccess::RegExp |
674 | // | OPimContactAccess::ExactMatch | 569 | // | OPimContactAccess::ExactMatch |
675 | ) ) != querySettings ) | 570 | ) ) != querySettings ) |
676 | return false; | 571 | return false; |
677 | 572 | ||
678 | // Step 2: Check whether the given combinations are ok.. | 573 | // Step 2: Check whether the given combinations are ok.. |
679 | 574 | ||
680 | // IngoreCase alone is invalid | 575 | // IngoreCase alone is invalid |
681 | if ( querySettings == OPimContactAccess::IgnoreCase ) | 576 | if ( querySettings == OPimContactAccess::IgnoreCase ) |
682 | return false; | 577 | return false; |
683 | 578 | ||
684 | // WildCards, RegExp and ExactMatch should never used at the same time | 579 | // WildCards, RegExp and ExactMatch should never used at the same time |
685 | switch ( querySettings & ~( OPimContactAccess::IgnoreCase | 580 | switch ( querySettings & ~( OPimContactAccess::IgnoreCase |
686 | | OPimContactAccess::DateDiff | 581 | | OPimContactAccess::DateDiff |
687 | | OPimContactAccess::DateYear | 582 | | OPimContactAccess::DateYear |
688 | | OPimContactAccess::DateMonth | 583 | | OPimContactAccess::DateMonth |
689 | | OPimContactAccess::DateDay | 584 | | OPimContactAccess::DateDay |
690 | ) | 585 | ) |
691 | ){ | 586 | ){ |
692 | case OPimContactAccess::RegExp: | 587 | case OPimContactAccess::RegExp: |
693 | return ( true ); | 588 | return ( true ); |
694 | case OPimContactAccess::WildCards: | 589 | case OPimContactAccess::WildCards: |
695 | return ( true ); | 590 | return ( true ); |
696 | case OPimContactAccess::ExactMatch: | 591 | case OPimContactAccess::ExactMatch: |
697 | return ( true ); | 592 | return ( true ); |
698 | case 0: // one of the upper removed bits were set.. | 593 | case 0: // one of the upper removed bits were set.. |
699 | return ( true ); | 594 | return ( true ); |
700 | default: | 595 | default: |
701 | return ( false ); | 596 | return ( false ); |
702 | } | 597 | } |
703 | 598 | ||
704 | } | 599 | } |
705 | 600 | ||
706 | QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int ) | 601 | QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int ) |
707 | { | 602 | { |
708 | QTime t; | 603 | QTime t; |
709 | t.start(); | 604 | t.start(); |
710 | 605 | ||
711 | #ifdef __STORE_HORIZONTAL_ | ||
712 | QString query = "SELECT uid FROM addressbook "; | 606 | QString query = "SELECT uid FROM addressbook "; |
713 | query += "ORDER BY \"Last Name\" "; | 607 | query += "ORDER BY \"Last Name\" "; |
714 | #else | ||
715 | QString query = "SELECT uid FROM addressbook WHERE type = 'Last Name' "; | ||
716 | query += "ORDER BY upper( value )"; | ||
717 | #endif | ||
718 | 608 | ||
719 | if ( !asc ) | 609 | if ( !asc ) |
720 | query += "DESC"; | 610 | query += "DESC"; |
721 | 611 | ||
722 | // qWarning("sorted query is: %s", query.latin1() ); | 612 | // qWarning("sorted query is: %s", query.latin1() ); |
723 | 613 | ||
724 | OSQLRawQuery raw( query ); | 614 | OSQLRawQuery raw( query ); |
725 | OSQLResult res = m_driver->query( &raw ); | 615 | OSQLResult res = m_driver->query( &raw ); |
726 | if ( res.state() != OSQLResult::Success ){ | 616 | if ( res.state() != OSQLResult::Success ){ |
727 | QArray<int> empty; | 617 | QArray<int> empty; |
728 | return empty; | 618 | return empty; |
729 | } | 619 | } |
730 | 620 | ||
731 | QArray<int> list = extractUids( res ); | 621 | QArray<int> list = extractUids( res ); |
732 | 622 | ||
733 | qWarning("sorted needed %d ms!", t.elapsed() ); | 623 | qWarning("sorted needed %d ms!", t.elapsed() ); |
734 | return list; | 624 | return list; |
735 | } | 625 | } |
736 | 626 | ||
737 | 627 | ||
738 | void OPimContactAccessBackend_SQL::update() | 628 | void OPimContactAccessBackend_SQL::update() |
739 | { | 629 | { |
740 | qWarning("Update starts"); | 630 | qWarning("Update starts"); |
741 | QTime t; | 631 | QTime t; |
742 | t.start(); | 632 | t.start(); |
743 | 633 | ||
744 | // Now load the database set and extract the uid's | 634 | // Now load the database set and extract the uid's |
745 | // which will be held locally | 635 | // which will be held locally |
746 | 636 | ||
747 | LoadQuery lo; | 637 | LoadQuery lo; |
748 | OSQLResult res = m_driver->query(&lo); | 638 | OSQLResult res = m_driver->query(&lo); |
749 | if ( res.state() != OSQLResult::Success ) | 639 | if ( res.state() != OSQLResult::Success ) |
750 | return; | 640 | return; |
751 | 641 | ||
752 | m_uids = extractUids( res ); | 642 | m_uids = extractUids( res ); |
753 | 643 | ||
754 | m_changed = false; | 644 | m_changed = false; |
755 | 645 | ||
756 | qWarning("Update ends %d ms", t.elapsed() ); | 646 | qWarning("Update ends %d ms", t.elapsed() ); |
757 | } | 647 | } |
758 | 648 | ||
759 | QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const | 649 | QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const |
760 | { | 650 | { |
761 | qWarning("extractUids"); | 651 | qWarning("extractUids"); |
762 | QTime t; | 652 | QTime t; |
763 | t.start(); | 653 | t.start(); |
764 | OSQLResultItem::ValueList list = res.results(); | 654 | OSQLResultItem::ValueList list = res.results(); |
765 | OSQLResultItem::ValueList::Iterator it; | 655 | OSQLResultItem::ValueList::Iterator it; |
766 | QArray<int> ints(list.count() ); | 656 | QArray<int> ints(list.count() ); |
767 | qWarning(" count = %d", list.count() ); | 657 | qWarning(" count = %d", list.count() ); |
768 | 658 | ||
769 | int i = 0; | 659 | int i = 0; |
770 | for (it = list.begin(); it != list.end(); ++it ) { | 660 | for (it = list.begin(); it != list.end(); ++it ) { |
771 | ints[i] = (*it).data("uid").toInt(); | 661 | ints[i] = (*it).data("uid").toInt(); |
772 | i++; | 662 | i++; |
773 | } | 663 | } |
774 | qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() ); | 664 | qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() ); |
775 | 665 | ||
776 | return ints; | 666 | return ints; |
777 | 667 | ||
778 | } | 668 | } |
779 | 669 | ||
780 | #ifdef __STORE_HORIZONTAL_ | ||
781 | QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const | 670 | QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const |
782 | { | 671 | { |
783 | QTime t; | 672 | QTime t; |
784 | t.start(); | 673 | t.start(); |
785 | 674 | ||
786 | QMap<int, QString> nonCustomMap; | 675 | QMap<int, QString> nonCustomMap; |
787 | 676 | ||
788 | int t2needed = 0; | 677 | int t2needed = 0; |
789 | int t3needed = 0; | 678 | int t3needed = 0; |
790 | QTime t2; | 679 | QTime t2; |
791 | t2.start(); | 680 | t2.start(); |
792 | FindQuery query( uid ); | 681 | FindQuery query( uid ); |
793 | OSQLResult res_noncustom = m_driver->query( &query ); | 682 | OSQLResult res_noncustom = m_driver->query( &query ); |
794 | t2needed = t2.elapsed(); | 683 | t2needed = t2.elapsed(); |
795 | 684 | ||
796 | OSQLResultItem resItem = res_noncustom.first(); | 685 | OSQLResultItem resItem = res_noncustom.first(); |
797 | 686 | ||
798 | QTime t3; | 687 | QTime t3; |
799 | t3.start(); | 688 | t3.start(); |
800 | // Now loop through all columns | 689 | // Now loop through all columns |
801 | QStringList fieldList = OPimContactFields::untrfields( false ); | 690 | QStringList fieldList = OPimContactFields::untrfields( false ); |
802 | QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); | 691 | QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); |
803 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ | 692 | for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ |
804 | // Get data for the selected column and store it with the | 693 | // Get data for the selected column and store it with the |
805 | // corresponding id into the map.. | 694 | // corresponding id into the map.. |
806 | 695 | ||
807 | int id = translate[*it]; | 696 | int id = translate[*it]; |
808 | QString value = resItem.data( (*it) ); | 697 | QString value = resItem.data( (*it) ); |
809 | 698 | ||
810 | // qWarning("Reading %s... found: %s", (*it).latin1(), value.latin1() ); | 699 | // qWarning("Reading %s... found: %s", (*it).latin1(), value.latin1() ); |
811 | 700 | ||
812 | switch( id ){ | 701 | switch( id ){ |
813 | case Qtopia::Birthday: | 702 | case Qtopia::Birthday: |
814 | case Qtopia::Anniversary:{ | 703 | case Qtopia::Anniversary:{ |
815 | // Birthday and Anniversary are encoded special ( yyyy-mm-dd ) | 704 | // Birthday and Anniversary are encoded special ( yyyy-mm-dd ) |
816 | QStringList list = QStringList::split( '-', value ); | 705 | QStringList list = QStringList::split( '-', value ); |
817 | QStringList::Iterator lit = list.begin(); | 706 | QStringList::Iterator lit = list.begin(); |
818 | int year = (*lit).toInt(); | 707 | int year = (*lit).toInt(); |
819 | int month = (*(++lit)).toInt(); | 708 | int month = (*(++lit)).toInt(); |
820 | int day = (*(++lit)).toInt(); | 709 | int day = (*(++lit)).toInt(); |
821 | if ( ( day != 0 ) && ( month != 0 ) && ( year != 0 ) ){ | 710 | if ( ( day != 0 ) && ( month != 0 ) && ( year != 0 ) ){ |
822 | QDate date( year, month, day ); | 711 | QDate date( year, month, day ); |
823 | nonCustomMap.insert( id, OPimDateConversion::dateToString( date ) ); | 712 | nonCustomMap.insert( id, OPimDateConversion::dateToString( date ) ); |
824 | } | 713 | } |
825 | } | 714 | } |
826 | break; | 715 | break; |
827 | case Qtopia::AddressCategory: | 716 | case Qtopia::AddressCategory: |
828 | qWarning("Category is: %s", value.latin1() ); | 717 | qWarning("Category is: %s", value.latin1() ); |
829 | default: | 718 | default: |
830 | nonCustomMap.insert( id, value ); | 719 | nonCustomMap.insert( id, value ); |
831 | } | 720 | } |
832 | } | 721 | } |
833 | 722 | ||
834 | // First insert uid | 723 | // First insert uid |
835 | nonCustomMap.insert( Qtopia::AddressUid, resItem.data( "uid" ) ); | 724 | nonCustomMap.insert( Qtopia::AddressUid, resItem.data( "uid" ) ); |
836 | t3needed = t3.elapsed(); | 725 | t3needed = t3.elapsed(); |
837 | 726 | ||
838 | // qWarning("Adding UID: %s", resItem.data( "uid" ).latin1() ); | 727 | // qWarning("Adding UID: %s", resItem.data( "uid" ).latin1() ); |
839 | qWarning("RequestNonCustom needed: insg.:%d ms, query: %d ms, mapping: %d ms", | 728 | qWarning("RequestNonCustom needed: insg.:%d ms, query: %d ms, mapping: %d ms", |
840 | t.elapsed(), t2needed, t3needed ); | 729 | t.elapsed(), t2needed, t3needed ); |
841 | 730 | ||
842 | return nonCustomMap; | 731 | return nonCustomMap; |
843 | } | 732 | } |
844 | #else | ||
845 | |||
846 | QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) const | ||
847 | { | ||
848 | QTime t; | ||
849 | t.start(); | ||
850 | |||
851 | QMap<int, QString> nonCustomMap; | ||
852 | |||
853 | int t2needed = 0; | ||
854 | QTime t2; | ||
855 | t2.start(); | ||
856 | FindQuery query( uid ); | ||
857 | OSQLResult res_noncustom = m_driver->query( &query ); | ||
858 | t2needed = t2.elapsed(); | ||
859 | |||
860 | if ( res_noncustom.state() == OSQLResult::Failure ) { | ||
861 | qWarning("OSQLResult::Failure in find query !!"); | ||
862 | QMap<int, QString> empty; | ||
863 | return empty; | ||
864 | } | ||
865 | |||
866 | int t3needed = 0; | ||
867 | QTime t3; | ||
868 | t3.start(); | ||
869 | QMap<QString, int> translateMap = OPimContactFields::untrFieldsToId(); | ||
870 | |||
871 | OSQLResultItem::ValueList list = res_noncustom.results(); | ||
872 | OSQLResultItem::ValueList::Iterator it = list.begin(); | ||
873 | for ( ; it != list.end(); ++it ) { | ||
874 | if ( (*it).data("type") != "" ){ | ||
875 | int typeId = translateMap[(*it).data( "type" )]; | ||
876 | switch( typeId ){ | ||
877 | case Qtopia::Birthday: | ||
878 | case Qtopia::Anniversary:{ | ||
879 | // Birthday and Anniversary are encoded special ( yyyy-mm-dd ) | ||
880 | QStringList list = QStringList::split( '-', (*it).data( "value" ) ); | ||
881 | QStringList::Iterator lit = list.begin(); | ||
882 | int year = (*lit).toInt(); | ||
883 | qWarning("1. %s", (*lit).latin1()); | ||
884 | int month = (*(++lit)).toInt(); | ||
885 | qWarning("2. %s", (*lit).latin1()); | ||
886 | int day = (*(++lit)).toInt(); | ||
887 | qWarning("3. %s", (*lit).latin1()); | ||
888 | qWarning( "RequestNonCustom->Converting:%s to Year: %d, Month: %d, Day: %d ", (*it).data( "value" ).latin1(), year, month, day ); | ||
889 | QDate date( year, month, day ); | ||
890 | nonCustomMap.insert( typeId, OPimDateConversion::dateToString( date ) ); | ||
891 | } | ||
892 | break; | ||
893 | default: | ||
894 | nonCustomMap.insert( typeId, | ||
895 | (*it).data( "value" ) ); | ||
896 | } | ||
897 | } | ||
898 | } | ||
899 | // Add UID to Map.. | ||
900 | nonCustomMap.insert( Qtopia::AddressUid, QString::number( uid ) ); | ||
901 | t3needed = t3.elapsed(); | ||
902 | |||
903 | qWarning("RequestNonCustom needed: insg.:%d ms, query: %d ms, mapping: %d ms", t.elapsed(), t2needed, t3needed ); | ||
904 | return nonCustomMap; | ||
905 | } | ||
906 | |||
907 | #endif // __STORE_HORIZONTAL_ | ||
908 | 733 | ||
909 | QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const | 734 | QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) const |
910 | { | 735 | { |
911 | QTime t; | 736 | QTime t; |
912 | t.start(); | 737 | t.start(); |
913 | 738 | ||
914 | QMap<QString, QString> customMap; | 739 | QMap<QString, QString> customMap; |
915 | 740 | ||
916 | FindCustomQuery query( uid ); | 741 | FindCustomQuery query( uid ); |
917 | OSQLResult res_custom = m_driver->query( &query ); | 742 | OSQLResult res_custom = m_driver->query( &query ); |
918 | 743 | ||
919 | if ( res_custom.state() == OSQLResult::Failure ) { | 744 | if ( res_custom.state() == OSQLResult::Failure ) { |
920 | qWarning("OSQLResult::Failure in find query !!"); | 745 | qWarning("OSQLResult::Failure in find query !!"); |
921 | QMap<QString, QString> empty; | 746 | QMap<QString, QString> empty; |
922 | return empty; | 747 | return empty; |
923 | } | 748 | } |
924 | 749 | ||
925 | OSQLResultItem::ValueList list = res_custom.results(); | 750 | OSQLResultItem::ValueList list = res_custom.results(); |
926 | OSQLResultItem::ValueList::Iterator it = list.begin(); | 751 | OSQLResultItem::ValueList::Iterator it = list.begin(); |
927 | for ( ; it != list.end(); ++it ) { | 752 | for ( ; it != list.end(); ++it ) { |
928 | customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); | 753 | customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); |
929 | } | 754 | } |
930 | 755 | ||
931 | qWarning("RequestCustom needed: %d ms", t.elapsed() ); | 756 | qWarning("RequestCustom needed: %d ms", t.elapsed() ); |
932 | return customMap; | 757 | return customMap; |
933 | } | 758 | } |
934 | 759 | ||
935 | } | 760 | } |