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