Diffstat (limited to 'libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp | 579 |
1 files changed, 292 insertions, 287 deletions
diff --git a/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp b/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp index f4f3c94..105c106 100644 --- a/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp +++ b/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp | |||
@@ -10,445 +10,450 @@ | |||
10 | .="- .-=="i, .._ License as published by the Free Software | 10 | .="- .-=="i, .._ License as published by the Free Software |
11 | - . .-<_> .<> Foundation; either version 2 of the License, | 11 | - . .-<_> .<> Foundation; either version 2 of the License, |
12 | ._= =} : or (at your option) any later version. | 12 | ._= =} : or (at your option) any later version. |
13 | .%`+i> _;_. | 13 | .%`+i> _;_. |
14 | .i_,=:_. -<s. This program is distributed in the hope that | 14 | .i_,=:_. -<s. This program is distributed in the hope that |
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
16 | : .. .:, . . . without even the implied warranty of | 16 | : .. .:, . . . without even the implied warranty of |
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | 18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU |
19 | ..}^=.= = ; Library General Public License for more | 19 | ..}^=.= = ; Library General Public License for more |
20 | ++= -. .` .: details. | 20 | ++= -. .` .: details. |
21 | : = ...= . :.=- | 21 | : = ...= . :.=- |
22 | -. .:....=;==+<; You should have received a copy of the GNU | 22 | -. .:....=;==+<; You should have received a copy of the GNU |
23 | -_. . . )=. = Library General Public License along with | 23 | -_. . . )=. = Library General Public License along with |
24 | -- :-=` this library; see the file COPYING.LIB. | 24 | -- :-=` this library; see the file COPYING.LIB. |
25 | If not, write to the Free Software Foundation, | 25 | If not, write to the Free Software Foundation, |
26 | Inc., 59 Temple Place - Suite 330, | 26 | Inc., 59 Temple Place - Suite 330, |
27 | Boston, MA 02111-1307, USA. | 27 | Boston, MA 02111-1307, USA. |
28 | */ | 28 | */ |
29 | /* | 29 | /* |
30 | * SQL Backend for the OPIE-Calender Database. | 30 | * SQL Backend for the OPIE-Calender Database. |
31 | * | 31 | * |
32 | */ | 32 | */ |
33 | 33 | ||
34 | #include <stdio.h> | 34 | /* OPIE */ |
35 | #include <stdlib.h> | ||
36 | |||
37 | #include <qarray.h> | ||
38 | #include <qstringlist.h> | ||
39 | |||
40 | #include <qpe/global.h> | ||
41 | |||
42 | #include <opie2/osqldriver.h> | 35 | #include <opie2/osqldriver.h> |
43 | #include <opie2/osqlmanager.h> | 36 | #include <opie2/osqlmanager.h> |
44 | #include <opie2/osqlquery.h> | 37 | #include <opie2/osqlquery.h> |
45 | 38 | ||
46 | #include <opie2/opimrecurrence.h> | 39 | #include <opie2/opimrecurrence.h> |
47 | #include <opie2/odatebookaccessbackend_sql.h> | 40 | #include <opie2/odatebookaccessbackend_sql.h> |
41 | #include <opie2/odebug.h> | ||
42 | |||
43 | #include <qpe/global.h> | ||
44 | |||
45 | /* QT */ | ||
46 | #include <qarray.h> | ||
47 | #include <qstringlist.h> | ||
48 | |||
49 | /* STD */ | ||
50 | #include <stdio.h> | ||
51 | #include <stdlib.h> | ||
52 | |||
48 | 53 | ||
49 | using namespace Opie::DB; | 54 | using namespace Opie::DB; |
50 | 55 | ||
51 | namespace { | 56 | namespace { |
52 | /** | 57 | /** |
53 | * a find query for custom elements | 58 | * a find query for custom elements |
54 | */ | 59 | */ |
55 | class FindCustomQuery : public OSQLQuery { | 60 | class FindCustomQuery : public OSQLQuery { |
56 | public: | 61 | public: |
57 | FindCustomQuery(int uid); | 62 | FindCustomQuery(int uid); |
58 | FindCustomQuery(const QArray<int>& ); | 63 | FindCustomQuery(const QArray<int>& ); |
59 | ~FindCustomQuery(); | 64 | ~FindCustomQuery(); |
60 | QString query()const; | 65 | QString query()const; |
61 | private: | 66 | private: |
62 | QString single()const; | 67 | QString single()const; |
63 | QString multi()const; | 68 | QString multi()const; |
64 | QArray<int> m_uids; | 69 | QArray<int> m_uids; |
65 | int m_uid; | 70 | int m_uid; |
66 | }; | 71 | }; |
67 | 72 | ||
68 | FindCustomQuery::FindCustomQuery(int uid) | 73 | FindCustomQuery::FindCustomQuery(int uid) |
69 | : OSQLQuery(), m_uid( uid ) { | 74 | : OSQLQuery(), m_uid( uid ) { |
70 | } | 75 | } |
71 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) | 76 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) |
72 | : OSQLQuery(), m_uids( ints ){ | 77 | : OSQLQuery(), m_uids( ints ){ |
73 | } | 78 | } |
74 | FindCustomQuery::~FindCustomQuery() { | 79 | FindCustomQuery::~FindCustomQuery() { |
75 | } | 80 | } |
76 | QString FindCustomQuery::query()const{ | 81 | QString FindCustomQuery::query()const{ |
77 | // if ( m_uids.count() == 0 ) | 82 | // if ( m_uids.count() == 0 ) |
78 | return single(); | 83 | return single(); |
79 | } | 84 | } |
80 | QString FindCustomQuery::single()const{ | 85 | QString FindCustomQuery::single()const{ |
81 | QString qu = "select uid, type, value from custom_data where uid = "; | 86 | QString qu = "select uid, type, value from custom_data where uid = "; |
82 | qu += QString::number(m_uid); | 87 | qu += QString::number(m_uid); |
83 | return qu; | 88 | return qu; |
84 | } | 89 | } |
85 | } | 90 | } |
86 | 91 | ||
87 | 92 | ||
88 | namespace Opie { | 93 | namespace Opie { |
89 | 94 | ||
90 | 95 | ||
91 | ODateBookAccessBackend_SQL::ODateBookAccessBackend_SQL( const QString& , | 96 | ODateBookAccessBackend_SQL::ODateBookAccessBackend_SQL( const QString& , |
92 | const QString& fileName ) | 97 | const QString& fileName ) |
93 | : ODateBookAccessBackend(), m_driver( NULL ) | 98 | : ODateBookAccessBackend(), m_driver( NULL ) |
94 | { | 99 | { |
95 | m_fileName = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.db" ) : fileName; | 100 | m_fileName = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.db" ) : fileName; |
96 | 101 | ||
97 | // Get the standart sql-driver from the OSQLManager.. | 102 | // Get the standart sql-driver from the OSQLManager.. |
98 | OSQLManager man; | 103 | OSQLManager man; |
99 | m_driver = man.standard(); | 104 | m_driver = man.standard(); |
100 | m_driver->setUrl( m_fileName ); | 105 | m_driver->setUrl( m_fileName ); |
101 | 106 | ||
102 | initFields(); | 107 | initFields(); |
103 | 108 | ||
104 | load(); | 109 | load(); |
105 | } | 110 | } |
106 | 111 | ||
107 | ODateBookAccessBackend_SQL::~ODateBookAccessBackend_SQL() { | 112 | ODateBookAccessBackend_SQL::~ODateBookAccessBackend_SQL() { |
108 | if( m_driver ) | 113 | if( m_driver ) |
109 | delete m_driver; | 114 | delete m_driver; |
110 | } | 115 | } |
111 | 116 | ||
112 | void ODateBookAccessBackend_SQL::initFields() | 117 | void ODateBookAccessBackend_SQL::initFields() |
113 | { | 118 | { |
114 | 119 | ||
115 | // This map contains the translation of the fieldtype id's to | 120 | // This map contains the translation of the fieldtype id's to |
116 | // the names of the table columns | 121 | // the names of the table columns |
117 | m_fieldMap.insert( OPimEvent::FUid, "uid" ); | 122 | m_fieldMap.insert( OPimEvent::FUid, "uid" ); |
118 | m_fieldMap.insert( OPimEvent::FCategories, "Categories" ); | 123 | m_fieldMap.insert( OPimEvent::FCategories, "Categories" ); |
119 | m_fieldMap.insert( OPimEvent::FDescription, "Description" ); | 124 | m_fieldMap.insert( OPimEvent::FDescription, "Description" ); |
120 | m_fieldMap.insert( OPimEvent::FLocation, "Location" ); | 125 | m_fieldMap.insert( OPimEvent::FLocation, "Location" ); |
121 | m_fieldMap.insert( OPimEvent::FType, "Type" ); | 126 | m_fieldMap.insert( OPimEvent::FType, "Type" ); |
122 | m_fieldMap.insert( OPimEvent::FAlarm, "Alarm" ); | 127 | m_fieldMap.insert( OPimEvent::FAlarm, "Alarm" ); |
123 | m_fieldMap.insert( OPimEvent::FSound, "Sound" ); | 128 | m_fieldMap.insert( OPimEvent::FSound, "Sound" ); |
124 | m_fieldMap.insert( OPimEvent::FRType, "RType" ); | 129 | m_fieldMap.insert( OPimEvent::FRType, "RType" ); |
125 | m_fieldMap.insert( OPimEvent::FRWeekdays, "RWeekdays" ); | 130 | m_fieldMap.insert( OPimEvent::FRWeekdays, "RWeekdays" ); |
126 | m_fieldMap.insert( OPimEvent::FRPosition, "RPosition" ); | 131 | m_fieldMap.insert( OPimEvent::FRPosition, "RPosition" ); |
127 | m_fieldMap.insert( OPimEvent::FRFreq, "RFreq" ); | 132 | m_fieldMap.insert( OPimEvent::FRFreq, "RFreq" ); |
128 | m_fieldMap.insert( OPimEvent::FRHasEndDate, "RHasEndDate" ); | 133 | m_fieldMap.insert( OPimEvent::FRHasEndDate, "RHasEndDate" ); |
129 | m_fieldMap.insert( OPimEvent::FREndDate, "REndDate" ); | 134 | m_fieldMap.insert( OPimEvent::FREndDate, "REndDate" ); |
130 | m_fieldMap.insert( OPimEvent::FRCreated, "RCreated" ); | 135 | m_fieldMap.insert( OPimEvent::FRCreated, "RCreated" ); |
131 | m_fieldMap.insert( OPimEvent::FRExceptions, "RExceptions" ); | 136 | m_fieldMap.insert( OPimEvent::FRExceptions, "RExceptions" ); |
132 | m_fieldMap.insert( OPimEvent::FStart, "Start" ); | 137 | m_fieldMap.insert( OPimEvent::FStart, "Start" ); |
133 | m_fieldMap.insert( OPimEvent::FEnd, "End" ); | 138 | m_fieldMap.insert( OPimEvent::FEnd, "End" ); |
134 | m_fieldMap.insert( OPimEvent::FNote, "Note" ); | 139 | m_fieldMap.insert( OPimEvent::FNote, "Note" ); |
135 | m_fieldMap.insert( OPimEvent::FTimeZone, "TimeZone" ); | 140 | m_fieldMap.insert( OPimEvent::FTimeZone, "TimeZone" ); |
136 | m_fieldMap.insert( OPimEvent::FRecParent, "RecParent" ); | 141 | m_fieldMap.insert( OPimEvent::FRecParent, "RecParent" ); |
137 | m_fieldMap.insert( OPimEvent::FRecChildren, "Recchildren" ); | 142 | m_fieldMap.insert( OPimEvent::FRecChildren, "Recchildren" ); |
138 | 143 | ||
139 | // Create a map that maps the column name to the id | 144 | // Create a map that maps the column name to the id |
140 | QMapConstIterator<int, QString> it; | 145 | QMapConstIterator<int, QString> it; |
141 | for ( it = m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ | 146 | for ( it = m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ |
142 | m_reverseFieldMap.insert( it.data(), it.key() ); | 147 | m_reverseFieldMap.insert( it.data(), it.key() ); |
143 | } | 148 | } |
144 | 149 | ||
145 | } | 150 | } |
146 | 151 | ||
147 | bool ODateBookAccessBackend_SQL::load() | 152 | bool ODateBookAccessBackend_SQL::load() |
148 | { | 153 | { |
149 | if (!m_driver->open() ) | 154 | if (!m_driver->open() ) |
150 | return false; | 155 | return false; |
151 | 156 | ||
152 | // Don't expect that the database exists. | 157 | // Don't expect that the database exists. |
153 | // It is save here to create the table, even if it | 158 | // It is save here to create the table, even if it |
154 | // do exist. ( Is that correct for all databases ?? ) | 159 | // do exist. ( Is that correct for all databases ?? ) |
155 | QStringqu = "create table datebook( uid INTEGER PRIMARY KEY "; | 160 | QString qu = "create table datebook( uid INTEGER PRIMARY KEY "; |
156 | 161 | ||
157 | QMap<int, QString>::Iterator it; | 162 | QMap<int, QString>::Iterator it; |
158 | for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ | 163 | for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ |
159 | qu += QString( ",%1 VARCHAR(10)" ).arg( it.data() ); | 164 | qu += QString( ",%1 VARCHAR(10)" ).arg( it.data() ); |
160 | } | 165 | } |
161 | qu += " );"; | 166 | qu += " );"; |
162 | 167 | ||
163 | qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );"; | 168 | qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );"; |
164 | 169 | ||
165 | qWarning( "command: %s", qu.latin1() ); | 170 | owarn << "command: " << qu << "" << oendl; |
166 | 171 | ||
167 | OSQLRawQuery raw( qu ); | 172 | OSQLRawQuery raw( qu ); |
168 | OSQLResult res = m_driver->query( &raw ); | 173 | OSQLResult res = m_driver->query( &raw ); |
169 | if ( res.state() != OSQLResult::Success ) | 174 | if ( res.state() != OSQLResult::Success ) |
170 | return false; | 175 | return false; |
171 | 176 | ||
172 | update(); | 177 | update(); |
173 | 178 | ||
174 | return true; | 179 | return true; |
175 | } | 180 | } |
176 | 181 | ||
177 | void ODateBookAccessBackend_SQL::update() | 182 | void ODateBookAccessBackend_SQL::update() |
178 | { | 183 | { |
179 | 184 | ||
180 | QString qu = "select uid from datebook"; | 185 | QString qu = "select uid from datebook"; |
181 | OSQLRawQuery raw( qu ); | 186 | OSQLRawQuery raw( qu ); |
182 | OSQLResult res = m_driver->query( &raw ); | 187 | OSQLResult res = m_driver->query( &raw ); |
183 | if ( res.state() != OSQLResult::Success ){ | 188 | if ( res.state() != OSQLResult::Success ){ |
184 | // m_uids.clear(); | 189 | // m_uids.clear(); |
185 | return; | 190 | return; |
186 | } | 191 | } |
187 | 192 | ||
188 | m_uids = extractUids( res ); | 193 | m_uids = extractUids( res ); |
189 | 194 | ||
190 | } | 195 | } |
191 | 196 | ||
192 | bool ODateBookAccessBackend_SQL::reload() | 197 | bool ODateBookAccessBackend_SQL::reload() |
193 | { | 198 | { |
194 | return load(); | 199 | return load(); |
195 | } | 200 | } |
196 | 201 | ||
197 | bool ODateBookAccessBackend_SQL::save() | 202 | bool ODateBookAccessBackend_SQL::save() |
198 | { | 203 | { |
199 | return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) | 204 | return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) |
200 | } | 205 | } |
201 | 206 | ||
202 | QArray<int> ODateBookAccessBackend_SQL::allRecords()const | 207 | QArray<int> ODateBookAccessBackend_SQL::allRecords()const |
203 | { | 208 | { |
204 | return m_uids; | 209 | return m_uids; |
205 | } | 210 | } |
206 | 211 | ||
207 | QArray<int> ODateBookAccessBackend_SQL::queryByExample(const OPimEvent&, int, const QDateTime& ) { | 212 | QArray<int> ODateBookAccessBackend_SQL::queryByExample(const OPimEvent&, int, const QDateTime& ) { |
208 | return QArray<int>(); | 213 | return QArray<int>(); |
209 | } | 214 | } |
210 | 215 | ||
211 | void ODateBookAccessBackend_SQL::clear() | 216 | void ODateBookAccessBackend_SQL::clear() |
212 | { | 217 | { |
213 | QString qu = "drop table datebook;"; | 218 | QString qu = "drop table datebook;"; |
214 | qu += "drop table custom_data;"; | 219 | qu += "drop table custom_data;"; |
215 | 220 | ||
216 | OSQLRawQuery raw( qu ); | 221 | OSQLRawQuery raw( qu ); |
217 | OSQLResult res = m_driver->query( &raw ); | 222 | OSQLResult res = m_driver->query( &raw ); |
218 | 223 | ||
219 | reload(); | 224 | reload(); |
220 | } | 225 | } |
221 | 226 | ||
222 | 227 | ||
223 | OPimEvent ODateBookAccessBackend_SQL::find( int uid ) const{ | 228 | OPimEvent ODateBookAccessBackend_SQL::find( int uid ) const{ |
224 | qDebug( "ODateBookAccessBackend_SQL::find( %d )", uid ); | 229 | odebug << "ODateBookAccessBackend_SQL::find( " << uid << " )" << oendl; |
225 | 230 | ||
226 | QString qu = "select *"; | 231 | QString qu = "select *"; |
227 | qu += "from datebook where uid = " + QString::number(uid); | 232 | qu += "from datebook where uid = " + QString::number(uid); |
228 | 233 | ||
229 | qDebug( "Query: %s", qu.latin1() ); | 234 | odebug << "Query: " << qu << "" << oendl; |
230 | 235 | ||
231 | OSQLRawQuery raw( qu ); | 236 | OSQLRawQuery raw( qu ); |
232 | OSQLResult res = m_driver->query( &raw ); | 237 | OSQLResult res = m_driver->query( &raw ); |
233 | 238 | ||
234 | OSQLResultItem resItem = res.first(); | 239 | OSQLResultItem resItem = res.first(); |
235 | 240 | ||
236 | // Create Map for date event and insert UID | 241 | // Create Map for date event and insert UID |
237 | QMap<int,QString> dateEventMap; | 242 | QMap<int,QString> dateEventMap; |
238 | dateEventMap.insert( OPimEvent::FUid, QString::number( uid ) ); | 243 | dateEventMap.insert( OPimEvent::FUid, QString::number( uid ) ); |
239 | 244 | ||
240 | // Now insert the data out of the columns into the map. | 245 | // Now insert the data out of the columns into the map. |
241 | QMapConstIterator<int, QString> it; | 246 | QMapConstIterator<int, QString> it; |
242 | for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ | 247 | for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ |
243 | dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) ); | 248 | dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) ); |
244 | } | 249 | } |
245 | 250 | ||
246 | // Last step: Put map into date event, add custom map and return it | 251 | // Last step: Put map into date event, add custom map and return it |
247 | OPimEvent retDate( dateEventMap ); | 252 | OPimEvent retDate( dateEventMap ); |
248 | retDate.setExtraMap( requestCustom( uid ) ); | 253 | retDate.setExtraMap( requestCustom( uid ) ); |
249 | 254 | ||
250 | qDebug( "ODateBookAccessBackend_SQL::find( %d ) end", uid ); | 255 | odebug << "ODateBookAccessBackend_SQL::find( " << uid << " ) end" << oendl; |
251 | return retDate; | 256 | return retDate; |
252 | } | 257 | } |
253 | 258 | ||
254 | // FIXME: Speed up update of uid's.. | 259 | // FIXME: Speed up update of uid's.. |
255 | bool ODateBookAccessBackend_SQL::add( const OPimEvent& ev ) | 260 | bool ODateBookAccessBackend_SQL::add( const OPimEvent& ev ) |
256 | { | 261 | { |
257 | QMap<int,QString> eventMap = ev.toMap(); | 262 | QMap<int,QString> eventMap = ev.toMap(); |
258 | 263 | ||
259 | QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() ); | 264 | QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() ); |
260 | QMap<int, QString>::Iterator it; | 265 | QMap<int, QString>::Iterator it; |
261 | for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ | 266 | for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ |
262 | if ( !eventMap[it.key()].isEmpty() ) | 267 | if ( !eventMap[it.key()].isEmpty() ) |
263 | qu += QString( ",\"%1\"" ).arg( eventMap[it.key()] ); | 268 | qu += QString( ",\"%1\"" ).arg( eventMap[it.key()] ); |
264 | else | 269 | else |
265 | qu += QString( ",\"\"" ); | 270 | qu += QString( ",\"\"" ); |
266 | } | 271 | } |
267 | qu += " );"; | 272 | qu += " );"; |
268 | 273 | ||
269 | // Add custom entries | 274 | // Add custom entries |
270 | int id = 0; | 275 | int id = 0; |
271 | QMap<QString, QString> customMap = ev.toExtraMap(); | 276 | QMap<QString, QString> customMap = ev.toExtraMap(); |
272 | for( QMap<QString, QString>::Iterator it = customMap.begin(); | 277 | for( QMap<QString, QString>::Iterator it = customMap.begin(); |
273 | it != customMap.end(); ++it ){ | 278 | it != customMap.end(); ++it ){ |
274 | qu += "insert into custom_data VALUES(" | 279 | qu += "insert into custom_data VALUES(" |
275 | + QString::number( ev.uid() ) | 280 | + QString::number( ev.uid() ) |
276 | + "," | 281 | + "," |
277 | + QString::number( id++ ) | 282 | + QString::number( id++ ) |
278 | + ",'" | 283 | + ",'" |
279 | + it.key() //.latin1() | 284 | + it.key() //.latin1() |
280 | + "'," | 285 | + "'," |
281 | + "0" // Priority for future enhancements | 286 | + "0" // Priority for future enhancements |
282 | + ",'" | 287 | + ",'" |
283 | + it.data() //.latin1() | 288 | + it.data() //.latin1() |
284 | + "');"; | 289 | + "');"; |
285 | } | 290 | } |
286 | qWarning("add %s", qu.latin1() ); | 291 | owarn << "add " << qu << "" << oendl; |
287 | 292 | ||
288 | OSQLRawQuery raw( qu ); | 293 | OSQLRawQuery raw( qu ); |
289 | OSQLResult res = m_driver->query( &raw ); | 294 | OSQLResult res = m_driver->query( &raw ); |
290 | if ( res.state() != OSQLResult::Success ){ | 295 | if ( res.state() != OSQLResult::Success ){ |
291 | return false; | 296 | return false; |
292 | } | 297 | } |
293 | 298 | ||
294 | // Update list of uid's | 299 | // Update list of uid's |
295 | update(); | 300 | update(); |
296 | 301 | ||
297 | return true; | 302 | return true; |
298 | } | 303 | } |
299 | 304 | ||
300 | // FIXME: Speed up update of uid's.. | 305 | // FIXME: Speed up update of uid's.. |
301 | bool ODateBookAccessBackend_SQL::remove( int uid ) | 306 | bool ODateBookAccessBackend_SQL::remove( int uid ) |
302 | { | 307 | { |
303 | QString qu = "DELETE from datebook where uid = " | 308 | QString qu = "DELETE from datebook where uid = " |
304 | + QString::number( uid ) + ";"; | 309 | + QString::number( uid ) + ";"; |
305 | qu += "DELETE from custom_data where uid = " | 310 | qu += "DELETE from custom_data where uid = " |
306 | + QString::number( uid ) + ";"; | 311 | + QString::number( uid ) + ";"; |
307 | 312 | ||
308 | OSQLRawQuery raw( qu ); | 313 | OSQLRawQuery raw( qu ); |
309 | OSQLResult res = m_driver->query( &raw ); | 314 | OSQLResult res = m_driver->query( &raw ); |
310 | if ( res.state() != OSQLResult::Success ){ | 315 | if ( res.state() != OSQLResult::Success ){ |
311 | return false; | 316 | return false; |
312 | } | 317 | } |
313 | 318 | ||
314 | // Update list of uid's | 319 | // Update list of uid's |
315 | update(); | 320 | update(); |
316 | 321 | ||
317 | return true; | 322 | return true; |
318 | } | 323 | } |
319 | 324 | ||
320 | bool ODateBookAccessBackend_SQL::replace( const OPimEvent& ev ) | 325 | bool ODateBookAccessBackend_SQL::replace( const OPimEvent& ev ) |
321 | { | 326 | { |
322 | remove( ev.uid() ); | 327 | remove( ev.uid() ); |
323 | return add( ev ); | 328 | return add( ev ); |
324 | } | 329 | } |
325 | 330 | ||
326 | QArray<int> ODateBookAccessBackend_SQL::rawEvents()const | 331 | QArray<int> ODateBookAccessBackend_SQL::rawEvents()const |
327 | { | 332 | { |
328 | return allRecords(); | 333 | return allRecords(); |
329 | } | 334 | } |
330 | 335 | ||
331 | QArray<int> ODateBookAccessBackend_SQL::rawRepeats()const | 336 | QArray<int> ODateBookAccessBackend_SQL::rawRepeats()const |
332 | { | 337 | { |
333 | QString qu = "select uid from datebook where RType!=\"\" AND RType!=\"NoRepeat\""; | 338 | QString qu = "select uid from datebook where RType!=\"\" AND RType!=\"NoRepeat\""; |
334 | OSQLRawQuery raw( qu ); | 339 | OSQLRawQuery raw( qu ); |
335 | OSQLResult res = m_driver->query( &raw ); | 340 | OSQLResult res = m_driver->query( &raw ); |
336 | if ( res.state() != OSQLResult::Success ){ | 341 | if ( res.state() != OSQLResult::Success ){ |
337 | QArray<int> nix; | 342 | QArray<int> nix; |
338 | return nix; | 343 | return nix; |
339 | } | 344 | } |
340 | 345 | ||
341 | return extractUids( res ); | 346 | return extractUids( res ); |
342 | } | 347 | } |
343 | 348 | ||
344 | QArray<int> ODateBookAccessBackend_SQL::nonRepeats()const | 349 | QArray<int> ODateBookAccessBackend_SQL::nonRepeats()const |
345 | { | 350 | { |
346 | QString qu = "select uid from datebook where RType=\"\" or RType=\"NoRepeat\""; | 351 | QString qu = "select uid from datebook where RType=\"\" or RType=\"NoRepeat\""; |
347 | OSQLRawQuery raw( qu ); | 352 | OSQLRawQuery raw( qu ); |
348 | OSQLResult res = m_driver->query( &raw ); | 353 | OSQLResult res = m_driver->query( &raw ); |
349 | if ( res.state() != OSQLResult::Success ){ | 354 | if ( res.state() != OSQLResult::Success ){ |
350 | QArray<int> nix; | 355 | QArray<int> nix; |
351 | return nix; | 356 | return nix; |
352 | } | 357 | } |
353 | 358 | ||
354 | return extractUids( res ); | 359 | return extractUids( res ); |
355 | } | 360 | } |
356 | 361 | ||
357 | OPimEvent::ValueList ODateBookAccessBackend_SQL::directNonRepeats() | 362 | OPimEvent::ValueList ODateBookAccessBackend_SQL::directNonRepeats() |
358 | { | 363 | { |
359 | QArray<int> nonRepUids = nonRepeats(); | 364 | QArray<int> nonRepUids = nonRepeats(); |
360 | OPimEvent::ValueList list; | 365 | OPimEvent::ValueList list; |
361 | 366 | ||
362 | for (uint i = 0; i < nonRepUids.count(); ++i ){ | 367 | for (uint i = 0; i < nonRepUids.count(); ++i ){ |
363 | list.append( find( nonRepUids[i] ) ); | 368 | list.append( find( nonRepUids[i] ) ); |
364 | } | 369 | } |
365 | 370 | ||
366 | return list; | 371 | return list; |
367 | 372 | ||
368 | } | 373 | } |
369 | OPimEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats() | 374 | OPimEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats() |
370 | { | 375 | { |
371 | QArray<int> rawRepUids = rawRepeats(); | 376 | QArray<int> rawRepUids = rawRepeats(); |
372 | OPimEvent::ValueList list; | 377 | OPimEvent::ValueList list; |
373 | 378 | ||
374 | for (uint i = 0; i < rawRepUids.count(); ++i ){ | 379 | for (uint i = 0; i < rawRepUids.count(); ++i ){ |
375 | list.append( find( rawRepUids[i] ) ); | 380 | list.append( find( rawRepUids[i] ) ); |
376 | } | 381 | } |
377 | 382 | ||
378 | return list; | 383 | return list; |
379 | } | 384 | } |
380 | 385 | ||
381 | 386 | ||
382 | QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const | 387 | QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const |
383 | { | 388 | { |
384 | 389 | ||
385 | QString qu = "SELECT uid FROM datebook WHERE ("; | 390 | QString qu = "SELECT uid FROM datebook WHERE ("; |
386 | 391 | ||
387 | // Do it make sense to search other fields, too ? | 392 | // Do it make sense to search other fields, too ? |
388 | qu += " rlike(\""+ r.pattern() + "\", Location ) OR"; | 393 | qu += " rlike(\""+ r.pattern() + "\", Location ) OR"; |
389 | qu += " rlike(\""+ r.pattern() + "\", Note )"; | 394 | qu += " rlike(\""+ r.pattern() + "\", Note )"; |
390 | |||
391 | qu += " )"; | ||
392 | 395 | ||
393 | qDebug( "query: %s", qu.latin1() ); | 396 | qu += " )"; |
394 | 397 | ||
395 | OSQLRawQuery raw( qu ); | 398 | odebug << "query: " << qu << "" << oendl; |
396 | OSQLResult res = m_driver->query( &raw ); | ||
397 | 399 | ||
398 | return extractUids( res ); | 400 | OSQLRawQuery raw( qu ); |
401 | OSQLResult res = m_driver->query( &raw ); | ||
402 | |||
403 | return extractUids( res ); | ||
399 | 404 | ||
400 | 405 | ||
401 | 406 | ||
402 | } | 407 | } |
403 | 408 | ||
404 | /* ===== Private Functions ========================================== */ | 409 | /* ===== Private Functions ========================================== */ |
405 | 410 | ||
406 | QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const | 411 | QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const |
407 | { | 412 | { |
408 | qWarning("extractUids"); | 413 | owarn << "extractUids" << oendl; |
409 | QTime t; | 414 | QTime t; |
410 | t.start(); | 415 | t.start(); |
411 | OSQLResultItem::ValueList list = res.results(); | 416 | OSQLResultItem::ValueList list = res.results(); |
412 | OSQLResultItem::ValueList::Iterator it; | 417 | OSQLResultItem::ValueList::Iterator it; |
413 | QArray<int> ints(list.count() ); | 418 | QArray<int> ints(list.count() ); |
414 | qWarning(" count = %d", list.count() ); | 419 | owarn << " count = " << list.count() << "" << oendl; |
415 | 420 | ||
416 | int i = 0; | 421 | int i = 0; |
417 | for (it = list.begin(); it != list.end(); ++it ) { | 422 | for (it = list.begin(); it != list.end(); ++it ) { |
418 | ints[i] = (*it).data("uid").toInt(); | 423 | ints[i] = (*it).data("uid").toInt(); |
419 | i++; | 424 | i++; |
420 | } | 425 | } |
421 | qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() ); | 426 | owarn << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl; |
422 | 427 | ||
423 | return ints; | 428 | return ints; |
424 | 429 | ||
425 | } | 430 | } |
426 | 431 | ||
427 | QMap<QString, QString> ODateBookAccessBackend_SQL::requestCustom( int uid ) const | 432 | QMap<QString, QString> ODateBookAccessBackend_SQL::requestCustom( int uid ) const |
428 | { | 433 | { |
429 | QTime t; | 434 | QTime t; |
430 | t.start(); | 435 | t.start(); |
431 | 436 | ||
432 | QMap<QString, QString> customMap; | 437 | QMap<QString, QString> customMap; |
433 | 438 | ||
434 | FindCustomQuery query( uid ); | 439 | FindCustomQuery query( uid ); |
435 | OSQLResult res_custom = m_driver->query( &query ); | 440 | OSQLResult res_custom = m_driver->query( &query ); |
436 | 441 | ||
437 | if ( res_custom.state() == OSQLResult::Failure ) { | 442 | if ( res_custom.state() == OSQLResult::Failure ) { |
438 | qWarning("OSQLResult::Failure in find query !!"); | 443 | owarn << "OSQLResult::Failure in find query !!" << oendl; |
439 | QMap<QString, QString> empty; | 444 | QMap<QString, QString> empty; |
440 | return empty; | 445 | return empty; |
441 | } | 446 | } |
442 | 447 | ||
443 | OSQLResultItem::ValueList list = res_custom.results(); | 448 | OSQLResultItem::ValueList list = res_custom.results(); |
444 | OSQLResultItem::ValueList::Iterator it = list.begin(); | 449 | OSQLResultItem::ValueList::Iterator it = list.begin(); |
445 | for ( ; it != list.end(); ++it ) { | 450 | for ( ; it != list.end(); ++it ) { |
446 | customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); | 451 | customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); |
447 | } | 452 | } |
448 | 453 | ||
449 | qDebug("RequestCustom needed: %d ms", t.elapsed() ); | 454 | odebug << "RequestCustom needed: " << t.elapsed() << " ms" << oendl; |
450 | return customMap; | 455 | return customMap; |
451 | } | 456 | } |
452 | 457 | ||
453 | 458 | ||
454 | } | 459 | } |