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