Diffstat (limited to 'libopie/pim/odatebookaccessbackend_sql.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie/pim/odatebookaccessbackend_sql.cpp | 371 |
1 files changed, 0 insertions, 371 deletions
diff --git a/libopie/pim/odatebookaccessbackend_sql.cpp b/libopie/pim/odatebookaccessbackend_sql.cpp deleted file mode 100644 index 44dd2bc..0000000 --- a/libopie/pim/odatebookaccessbackend_sql.cpp +++ b/dev/null | |||
@@ -1,371 +0,0 @@ | |||
1 | /* | ||
2 | * SQL Backend for the OPIE-Calender Database. | ||
3 | * | ||
4 | * Copyright (c) 2003 by Stefan Eilers (Eilers.Stefan@epost.de) | ||
5 | * | ||
6 | * ===================================================================== | ||
7 | *This program is free software; you can redistribute it and/or | ||
8 | *modify it under the terms of the GNU Library General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2 of the License, or (at your option) any later version. | ||
11 | * ===================================================================== | ||
12 | * ===================================================================== | ||
13 | * Version: $Id$ | ||
14 | * ===================================================================== | ||
15 | * History: | ||
16 | * $Log$ | ||
17 | * Revision 1.4 2004/03/14 13:50:35 alwin | ||
18 | * namespace correction | ||
19 | * | ||
20 | * Revision 1.3 2003/12/22 11:41:39 eilers | ||
21 | * Fixing stupid bug, found by sourcode review.. | ||
22 | * | ||
23 | * Revision 1.2 2003/12/22 10:19:26 eilers | ||
24 | * Finishing implementation of sql-backend for datebook. But I have to | ||
25 | * port the PIM datebook application to use it, before I could debug the | ||
26 | * whole stuff. | ||
27 | * Thus, PIM-Database backend is finished, but highly experimental. And some | ||
28 | * parts are still generic. For instance, the "queryByExample()" methods are | ||
29 | * not (or not fully) implemented. Todo: custom-entries not stored. | ||
30 | * The big show stopper: matchRegExp() (needed by OpieSearch) needs regular | ||
31 | * expression search in the database, which is not supported by sqlite ! | ||
32 | * Therefore we need either an extended sqlite or a workaround which would | ||
33 | * be very slow and memory consuming.. | ||
34 | * | ||
35 | * Revision 1.1 2003/12/08 15:18:12 eilers | ||
36 | * Committing unfinished sql implementation before merging to libopie2 starts.. | ||
37 | * | ||
38 | * | ||
39 | */ | ||
40 | |||
41 | #include <stdio.h> | ||
42 | #include <stdlib.h> | ||
43 | |||
44 | #include <qarray.h> | ||
45 | #include <qstringlist.h> | ||
46 | |||
47 | #include <qpe/global.h> | ||
48 | |||
49 | #include <opie2/osqldriver.h> | ||
50 | #include <opie2/osqlmanager.h> | ||
51 | #include <opie2/osqlquery.h> | ||
52 | |||
53 | #include "orecur.h" | ||
54 | #include "odatebookaccessbackend_sql.h" | ||
55 | |||
56 | using namespace Opie::DB; | ||
57 | |||
58 | |||
59 | ODateBookAccessBackend_SQL::ODateBookAccessBackend_SQL( const QString& , | ||
60 | const QString& fileName ) | ||
61 | : ODateBookAccessBackend(), m_driver( NULL ) | ||
62 | { | ||
63 | m_fileName = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.db" ) : fileName; | ||
64 | |||
65 | // Get the standart sql-driver from the OSQLManager.. | ||
66 | OSQLManager man; | ||
67 | m_driver = man.standard(); | ||
68 | m_driver->setUrl( m_fileName ); | ||
69 | |||
70 | initFields(); | ||
71 | |||
72 | load(); | ||
73 | } | ||
74 | |||
75 | ODateBookAccessBackend_SQL::~ODateBookAccessBackend_SQL() { | ||
76 | if( m_driver ) | ||
77 | delete m_driver; | ||
78 | } | ||
79 | |||
80 | void ODateBookAccessBackend_SQL::initFields() | ||
81 | { | ||
82 | |||
83 | // This map contains the translation of the fieldtype id's to | ||
84 | // the names of the table columns | ||
85 | m_fieldMap.insert( OEvent::FUid, "uid" ); | ||
86 | m_fieldMap.insert( OEvent::FCategories, "Categories" ); | ||
87 | m_fieldMap.insert( OEvent::FDescription, "Description" ); | ||
88 | m_fieldMap.insert( OEvent::FLocation, "Location" ); | ||
89 | m_fieldMap.insert( OEvent::FType, "Type" ); | ||
90 | m_fieldMap.insert( OEvent::FAlarm, "Alarm" ); | ||
91 | m_fieldMap.insert( OEvent::FSound, "Sound" ); | ||
92 | m_fieldMap.insert( OEvent::FRType, "RType" ); | ||
93 | m_fieldMap.insert( OEvent::FRWeekdays, "RWeekdays" ); | ||
94 | m_fieldMap.insert( OEvent::FRPosition, "RPosition" ); | ||
95 | m_fieldMap.insert( OEvent::FRFreq, "RFreq" ); | ||
96 | m_fieldMap.insert( OEvent::FRHasEndDate, "RHasEndDate" ); | ||
97 | m_fieldMap.insert( OEvent::FREndDate, "REndDate" ); | ||
98 | m_fieldMap.insert( OEvent::FRCreated, "RCreated" ); | ||
99 | m_fieldMap.insert( OEvent::FRExceptions, "RExceptions" ); | ||
100 | m_fieldMap.insert( OEvent::FStart, "Start" ); | ||
101 | m_fieldMap.insert( OEvent::FEnd, "End" ); | ||
102 | m_fieldMap.insert( OEvent::FNote, "Note" ); | ||
103 | m_fieldMap.insert( OEvent::FTimeZone, "TimeZone" ); | ||
104 | m_fieldMap.insert( OEvent::FRecParent, "RecParent" ); | ||
105 | m_fieldMap.insert( OEvent::FRecChildren, "Recchildren" ); | ||
106 | |||
107 | // Create a map that maps the column name to the id | ||
108 | QMapConstIterator<int, QString> it; | ||
109 | for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ | ||
110 | m_reverseFieldMap.insert( it.data(), it.key() ); | ||
111 | } | ||
112 | |||
113 | } | ||
114 | |||
115 | bool ODateBookAccessBackend_SQL::load() | ||
116 | { | ||
117 | if (!m_driver->open() ) | ||
118 | return false; | ||
119 | |||
120 | // Don't expect that the database exists. | ||
121 | // It is save here to create the table, even if it | ||
122 | // do exist. ( Is that correct for all databases ?? ) | ||
123 | QStringqu = "create table datebook( uid INTEGER PRIMARY KEY "; | ||
124 | |||
125 | QMap<int, QString>::Iterator it; | ||
126 | for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ | ||
127 | qu += QString( ",%1 VARCHAR(10)" ).arg( it.data() ); | ||
128 | } | ||
129 | qu += " );"; | ||
130 | |||
131 | qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );"; | ||
132 | |||
133 | qWarning( "command: %s", qu.latin1() ); | ||
134 | |||
135 | OSQLRawQuery raw( qu ); | ||
136 | OSQLResult res = m_driver->query( &raw ); | ||
137 | if ( res.state() != OSQLResult::Success ) | ||
138 | return false; | ||
139 | |||
140 | update(); | ||
141 | |||
142 | return true; | ||
143 | } | ||
144 | |||
145 | void ODateBookAccessBackend_SQL::update() | ||
146 | { | ||
147 | |||
148 | QString qu = "select uid from datebook"; | ||
149 | OSQLRawQuery raw( qu ); | ||
150 | OSQLResult res = m_driver->query( &raw ); | ||
151 | if ( res.state() != OSQLResult::Success ){ | ||
152 | // m_uids.clear(); | ||
153 | return; | ||
154 | } | ||
155 | |||
156 | m_uids = extractUids( res ); | ||
157 | |||
158 | } | ||
159 | |||
160 | bool ODateBookAccessBackend_SQL::reload() | ||
161 | { | ||
162 | return load(); | ||
163 | } | ||
164 | |||
165 | bool ODateBookAccessBackend_SQL::save() | ||
166 | { | ||
167 | return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) | ||
168 | } | ||
169 | |||
170 | QArray<int> ODateBookAccessBackend_SQL::allRecords()const | ||
171 | { | ||
172 | return m_uids; | ||
173 | } | ||
174 | |||
175 | QArray<int> ODateBookAccessBackend_SQL::queryByExample(const OEvent&, int, const QDateTime& ) { | ||
176 | return QArray<int>(); | ||
177 | } | ||
178 | |||
179 | void ODateBookAccessBackend_SQL::clear() | ||
180 | { | ||
181 | QString qu = "drop table datebook;"; | ||
182 | qu += "drop table custom_data;"; | ||
183 | |||
184 | OSQLRawQuery raw( qu ); | ||
185 | OSQLResult res = m_driver->query( &raw ); | ||
186 | |||
187 | reload(); | ||
188 | } | ||
189 | |||
190 | |||
191 | OEvent ODateBookAccessBackend_SQL::find( int uid ) const{ | ||
192 | QString qu = "select *"; | ||
193 | qu += "from datebook where uid = " + QString::number(uid); | ||
194 | |||
195 | OSQLRawQuery raw( qu ); | ||
196 | OSQLResult res = m_driver->query( &raw ); | ||
197 | |||
198 | OSQLResultItem resItem = res.first(); | ||
199 | |||
200 | // Create Map for date event and insert UID | ||
201 | QMap<int,QString> dateEventMap; | ||
202 | dateEventMap.insert( OEvent::FUid, QString::number( uid ) ); | ||
203 | |||
204 | // Now insert the data out of the columns into the map. | ||
205 | QMapConstIterator<int, QString> it; | ||
206 | for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ | ||
207 | dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) ); | ||
208 | } | ||
209 | |||
210 | // Last step: Put map into date event and return it | ||
211 | OEvent retDate( dateEventMap ); | ||
212 | |||
213 | return retDate; | ||
214 | } | ||
215 | |||
216 | // FIXME: Speed up update of uid's.. | ||
217 | bool ODateBookAccessBackend_SQL::add( const OEvent& ev ) | ||
218 | { | ||
219 | QMap<int,QString> eventMap = ev.toMap(); | ||
220 | |||
221 | QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() ); | ||
222 | QMap<int, QString>::Iterator it; | ||
223 | for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ | ||
224 | if ( !eventMap[it.key()].isEmpty() ) | ||
225 | qu += QString( ",\"%1\"" ).arg( eventMap[it.key()] ); | ||
226 | else | ||
227 | qu += QString( ",\"\"" ); | ||
228 | } | ||
229 | qu += " );"; | ||
230 | |||
231 | // Add custom entries | ||
232 | int id = 0; | ||
233 | QMap<QString, QString> customMap = ev.toExtraMap(); | ||
234 | for( QMap<QString, QString>::Iterator it = customMap.begin(); | ||
235 | it != customMap.end(); ++it ){ | ||
236 | qu += "insert into custom_data VALUES(" | ||
237 | + QString::number( ev.uid() ) | ||
238 | + "," | ||
239 | + QString::number( id++ ) | ||
240 | + ",'" | ||
241 | + it.key() //.latin1() | ||
242 | + "'," | ||
243 | + "0" // Priority for future enhancements | ||
244 | + ",'" | ||
245 | + it.data() //.latin1() | ||
246 | + "');"; | ||
247 | } | ||
248 | qWarning("add %s", qu.latin1() ); | ||
249 | |||
250 | OSQLRawQuery raw( qu ); | ||
251 | OSQLResult res = m_driver->query( &raw ); | ||
252 | if ( res.state() != OSQLResult::Success ){ | ||
253 | return false; | ||
254 | } | ||
255 | |||
256 | // Update list of uid's | ||
257 | update(); | ||
258 | |||
259 | return true; | ||
260 | } | ||
261 | |||
262 | // FIXME: Speed up update of uid's.. | ||
263 | bool ODateBookAccessBackend_SQL::remove( int uid ) | ||
264 | { | ||
265 | QString qu = "DELETE from datebook where uid = " | ||
266 | + QString::number( uid ) + ";"; | ||
267 | qu += "DELETE from custom_data where uid = " | ||
268 | + QString::number( uid ) + ";"; | ||
269 | |||
270 | OSQLRawQuery raw( qu ); | ||
271 | OSQLResult res = m_driver->query( &raw ); | ||
272 | if ( res.state() != OSQLResult::Success ){ | ||
273 | return false; | ||
274 | } | ||
275 | |||
276 | // Update list of uid's | ||
277 | update(); | ||
278 | |||
279 | return true; | ||
280 | } | ||
281 | |||
282 | bool ODateBookAccessBackend_SQL::replace( const OEvent& ev ) | ||
283 | { | ||
284 | remove( ev.uid() ); | ||
285 | return add( ev ); | ||
286 | } | ||
287 | |||
288 | QArray<int> ODateBookAccessBackend_SQL::rawEvents()const | ||
289 | { | ||
290 | return allRecords(); | ||
291 | } | ||
292 | |||
293 | QArray<int> ODateBookAccessBackend_SQL::rawRepeats()const | ||
294 | { | ||
295 | QString qu = "select uid from datebook where RType!=\"\" AND RType!=\"NoRepeat\""; | ||
296 | OSQLRawQuery raw( qu ); | ||
297 | OSQLResult res = m_driver->query( &raw ); | ||
298 | if ( res.state() != OSQLResult::Success ){ | ||
299 | QArray<int> nix; | ||
300 | return nix; | ||
301 | } | ||
302 | |||
303 | return extractUids( res ); | ||
304 | } | ||
305 | |||
306 | QArray<int> ODateBookAccessBackend_SQL::nonRepeats()const | ||
307 | { | ||
308 | QString qu = "select uid from datebook where RType=\"\" or RType=\"NoRepeat\""; | ||
309 | OSQLRawQuery raw( qu ); | ||
310 | OSQLResult res = m_driver->query( &raw ); | ||
311 | if ( res.state() != OSQLResult::Success ){ | ||
312 | QArray<int> nix; | ||
313 | return nix; | ||
314 | } | ||
315 | |||
316 | return extractUids( res ); | ||
317 | } | ||
318 | |||
319 | OEvent::ValueList ODateBookAccessBackend_SQL::directNonRepeats() | ||
320 | { | ||
321 | QArray<int> nonRepUids = nonRepeats(); | ||
322 | OEvent::ValueList list; | ||
323 | |||
324 | for (uint i = 0; i < nonRepUids.count(); ++i ){ | ||
325 | list.append( find( nonRepUids[i] ) ); | ||
326 | } | ||
327 | |||
328 | return list; | ||
329 | |||
330 | } | ||
331 | OEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats() | ||
332 | { | ||
333 | QArray<int> rawRepUids = rawRepeats(); | ||
334 | OEvent::ValueList list; | ||
335 | |||
336 | for (uint i = 0; i < rawRepUids.count(); ++i ){ | ||
337 | list.append( find( rawRepUids[i] ) ); | ||
338 | } | ||
339 | |||
340 | return list; | ||
341 | } | ||
342 | |||
343 | |||
344 | QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const | ||
345 | { | ||
346 | QArray<int> null; | ||
347 | return null; | ||
348 | } | ||
349 | |||
350 | /* ===== Private Functions ========================================== */ | ||
351 | |||
352 | QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const | ||
353 | { | ||
354 | qWarning("extractUids"); | ||
355 | QTime t; | ||
356 | t.start(); | ||
357 | OSQLResultItem::ValueList list = res.results(); | ||
358 | OSQLResultItem::ValueList::Iterator it; | ||
359 | QArray<int> ints(list.count() ); | ||
360 | qWarning(" count = %d", list.count() ); | ||
361 | |||
362 | int i = 0; | ||
363 | for (it = list.begin(); it != list.end(); ++it ) { | ||
364 | ints[i] = (*it).data("uid").toInt(); | ||
365 | i++; | ||
366 | } | ||
367 | qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() ); | ||
368 | |||
369 | return ints; | ||
370 | |||
371 | } | ||