89
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#include <QSqlQuery>
|
|
19 |
#include <QSqlError>
|
|
20 |
#include <QStringList>
|
|
21 |
#include <QSettings>
|
|
22 |
#include <QSqlDriver>
|
|
23 |
#include <QDebug>
|
|
24 |
#include "hsactivitystorage_p.h"
|
|
25 |
#include "hsactivity.h"
|
|
26 |
|
|
27 |
// -----------------------------------------------------------------------------
|
|
28 |
//
|
|
29 |
// -----------------------------------------------------------------------------
|
|
30 |
//
|
|
31 |
HsActivityStoragePrivate::HsActivityStoragePrivate()
|
|
32 |
{
|
|
33 |
// determine database localization
|
|
34 |
QSettings settings(ActivityOrganization, ActivityApplication);
|
|
35 |
settings.setValue(ActivityStorageProperty, ActivityDefaultStorage);
|
|
36 |
if (!settings.contains(ActivityStorageProperty)) {
|
|
37 |
settings.setValue(ActivityStorageProperty, ActivityDefaultStorage);
|
|
38 |
}
|
|
39 |
QString databaseFile = settings.value(ActivityStorageProperty).toString();
|
|
40 |
|
|
41 |
if (QSqlDatabase::contains(ActivityStorageName)) {
|
|
42 |
mConn = QSqlDatabase::database(ActivityStorageName);
|
|
43 |
} else {
|
|
44 |
mConn = QSqlDatabase::addDatabase(ActivityStorageDriver, ActivityStorageName);
|
|
45 |
mConn.setDatabaseName(databaseFile);
|
|
46 |
if (!mConn.open()) {
|
|
47 |
qWarning(qPrintable(mConn.lastError().text()));
|
|
48 |
return;
|
|
49 |
}
|
|
50 |
}
|
|
51 |
if (!checkTables()) {
|
|
52 |
recreateTables();
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
// -----------------------------------------------------------------------------
|
|
57 |
//
|
|
58 |
// -----------------------------------------------------------------------------
|
|
59 |
//
|
|
60 |
HsActivityStoragePrivate::~HsActivityStoragePrivate()
|
|
61 |
{
|
|
62 |
mConn.close();
|
|
63 |
}
|
|
64 |
|
|
65 |
// -----------------------------------------------------------------------------
|
|
66 |
//
|
|
67 |
// -----------------------------------------------------------------------------
|
|
68 |
//
|
|
69 |
int HsActivityStoragePrivate::addActivity(const QVariantHash &activity)
|
|
70 |
{
|
|
71 |
// stream whole entry to bytearray
|
|
72 |
QByteArray streamedData;
|
|
73 |
QDataStream stream(&streamedData, QIODevice::WriteOnly);
|
|
74 |
stream << activity;
|
|
75 |
|
|
76 |
//insert data
|
|
77 |
QSqlQuery query(mConn);
|
|
78 |
query.prepare(ActivitySelectActivityQuery);
|
|
79 |
bind(query, activity);
|
|
80 |
exec(query);
|
|
81 |
if(query.next()) {
|
|
82 |
return KErrGeneral;
|
|
83 |
}
|
|
84 |
query.prepare(ActivityInsertActivityQuery);
|
|
85 |
QVariantHash additionalData;
|
|
86 |
additionalData.insert(ActivityDataKeyword, streamedData);
|
|
87 |
bind(query, activity, additionalData);
|
|
88 |
exec(query);
|
|
89 |
return 0>=query.numRowsAffected() ? KErrGeneral : getSqlErrorCode(query);
|
|
90 |
}
|
|
91 |
|
|
92 |
// -----------------------------------------------------------------------------
|
|
93 |
//
|
|
94 |
// -----------------------------------------------------------------------------
|
|
95 |
//
|
|
96 |
int HsActivityStoragePrivate::updateActivity(const QVariantHash &activity)
|
|
97 |
{
|
|
98 |
// stream whole entry to bytearray
|
|
99 |
QByteArray streamedData;
|
|
100 |
QDataStream stream(&streamedData, QIODevice::WriteOnly);
|
|
101 |
stream << activity;
|
|
102 |
|
|
103 |
// update
|
|
104 |
QSqlQuery query(mConn);
|
|
105 |
query.prepare(ActivityUpdateActivityQuery);
|
|
106 |
QVariantHash additionalData;
|
|
107 |
additionalData.insert(ActivityDataKeyword, streamedData);
|
|
108 |
bind(query, activity, additionalData);
|
|
109 |
exec(query);
|
|
110 |
return 0>=query.numRowsAffected() ? KErrGeneral : getSqlErrorCode(query);
|
|
111 |
}
|
|
112 |
|
|
113 |
// -----------------------------------------------------------------------------
|
|
114 |
//
|
|
115 |
// -----------------------------------------------------------------------------
|
|
116 |
//
|
|
117 |
int HsActivityStoragePrivate::removeActivity(const QVariantHash &activity)
|
|
118 |
{
|
|
119 |
return exec(ActivityDeleteActivityQuery, activity);
|
|
120 |
}
|
|
121 |
|
|
122 |
// -----------------------------------------------------------------------------
|
|
123 |
//
|
|
124 |
// -----------------------------------------------------------------------------
|
|
125 |
//
|
|
126 |
int HsActivityStoragePrivate::removeApplicationActivities(const QVariantHash &activity)
|
|
127 |
{
|
|
128 |
return exec(ActivityDeleteApplicationActivitiesQuery, activity);
|
|
129 |
}
|
|
130 |
// -----------------------------------------------------------------------------
|
|
131 |
//
|
|
132 |
// -----------------------------------------------------------------------------
|
|
133 |
//
|
|
134 |
int HsActivityStoragePrivate::requestedActivityName(QString& result,
|
|
135 |
const QVariantHash &activity)
|
|
136 |
{
|
|
137 |
QSqlQuery query(mConn);
|
|
138 |
query.prepare(ActivitySelectActiveQuery);
|
|
139 |
bind(query, activity);
|
|
140 |
int retVal(KErrNone);
|
|
141 |
if (exec(query) && query.next()) {
|
|
142 |
result = query.value(0).toString();
|
|
143 |
} else {
|
|
144 |
retVal = KErrNotFound;
|
|
145 |
}
|
|
146 |
return retVal;
|
|
147 |
}
|
|
148 |
|
|
149 |
// -----------------------------------------------------------------------------
|
|
150 |
//
|
|
151 |
// -----------------------------------------------------------------------------
|
|
152 |
//
|
|
153 |
int HsActivityStoragePrivate::activities(QList<QVariantHash>& result)
|
|
154 |
{
|
|
155 |
return activities(result, ActivityActivitiesQuery, QVariantHash());
|
|
156 |
}
|
|
157 |
|
|
158 |
// -----------------------------------------------------------------------------
|
|
159 |
//
|
|
160 |
// -----------------------------------------------------------------------------
|
|
161 |
//
|
|
162 |
int HsActivityStoragePrivate::applicationActivities(QList<QVariantHash> & result,
|
|
163 |
const QVariantHash & condition)
|
|
164 |
{
|
|
165 |
return activities(result, ActivityApplicationActivitiesQuery, condition);
|
|
166 |
}
|
|
167 |
|
|
168 |
// -----------------------------------------------------------------------------
|
|
169 |
//
|
|
170 |
// -----------------------------------------------------------------------------
|
|
171 |
//
|
|
172 |
int HsActivityStoragePrivate::waitActivity(const QVariantHash &)
|
|
173 |
{
|
|
174 |
return KErrNotSupported;
|
|
175 |
}
|
|
176 |
|
|
177 |
// -----------------------------------------------------------------------------
|
|
178 |
//
|
|
179 |
// -----------------------------------------------------------------------------
|
|
180 |
//
|
|
181 |
int HsActivityStoragePrivate::launchActivity(const QVariantHash &)
|
|
182 |
{
|
|
183 |
return KErrNotSupported;
|
|
184 |
}
|
|
185 |
// -----------------------------------------------------------------------------
|
|
186 |
//
|
|
187 |
// -----------------------------------------------------------------------------
|
|
188 |
//
|
|
189 |
bool HsActivityStoragePrivate::checkTables()
|
|
190 |
{
|
|
191 |
return (QStringList("Activities") == mConn.tables());
|
|
192 |
}
|
|
193 |
|
|
194 |
// -----------------------------------------------------------------------------
|
|
195 |
//
|
|
196 |
// -----------------------------------------------------------------------------
|
|
197 |
//
|
|
198 |
void HsActivityStoragePrivate::recreateTables()
|
|
199 |
{
|
|
200 |
//start sql transaction
|
|
201 |
if (!mConn.transaction()) {
|
|
202 |
qErrnoWarning(qPrintable(mConn.lastError().text()));
|
|
203 |
return;
|
|
204 |
}
|
|
205 |
|
|
206 |
// drop any existing tables
|
|
207 |
QSqlQuery query(mConn);
|
|
208 |
foreach (const QString &tableName, mConn.tables()) {
|
|
209 |
query.prepare(ActivityDropQuery);
|
|
210 |
query.bindValue(ActivityTableKeyword, tableName);
|
|
211 |
exec(query);
|
|
212 |
}
|
|
213 |
|
|
214 |
// create new table
|
|
215 |
query.prepare(ActivityCreateQuery);
|
|
216 |
exec(query);
|
|
217 |
|
|
218 |
//finish sql transaction
|
|
219 |
if (!mConn.commit()) {
|
|
220 |
qErrnoWarning(qPrintable(mConn.lastError().text()));
|
|
221 |
}
|
|
222 |
}
|
|
223 |
|
|
224 |
// -----------------------------------------------------------------------------
|
|
225 |
//
|
|
226 |
// -----------------------------------------------------------------------------
|
|
227 |
//
|
|
228 |
int HsActivityStoragePrivate::getSqlErrorCode(const QSqlQuery& query)
|
|
229 |
{
|
|
230 |
const QSqlError err(query.lastError());
|
|
231 |
const QString errStr(err.text());
|
|
232 |
|
|
233 |
if (QSqlError ::NoError == err.type()) {
|
|
234 |
return 0;
|
|
235 |
} else {
|
|
236 |
qErrnoWarning(qPrintable(errStr));
|
|
237 |
return err.number();
|
|
238 |
}
|
|
239 |
}
|
|
240 |
|
|
241 |
// -----------------------------------------------------------------------------
|
|
242 |
//
|
|
243 |
// -----------------------------------------------------------------------------
|
|
244 |
//
|
|
245 |
bool HsActivityStoragePrivate::exec(QSqlQuery& query)
|
|
246 |
{
|
|
247 |
const bool retVal = query.exec();
|
|
248 |
qErrnoWarning(qPrintable(query.lastQuery()));
|
|
249 |
if (!retVal) {
|
|
250 |
getSqlErrorCode(query);
|
|
251 |
}
|
|
252 |
return retVal;
|
|
253 |
}
|
|
254 |
|
|
255 |
// -----------------------------------------------------------------------------
|
|
256 |
//
|
|
257 |
// -----------------------------------------------------------------------------
|
|
258 |
//
|
|
259 |
bool HsActivityStoragePrivate::exec(const QString &queryStr, const QVariantHash& params)
|
|
260 |
{
|
|
261 |
QSqlQuery query(mConn);
|
|
262 |
query.prepare(queryStr);
|
|
263 |
bind(query, params);
|
|
264 |
query.exec();
|
|
265 |
return getSqlErrorCode(query);
|
|
266 |
|
|
267 |
}
|
|
268 |
// -----------------------------------------------------------------------------
|
|
269 |
//
|
|
270 |
// -----------------------------------------------------------------------------
|
|
271 |
//
|
|
272 |
void HsActivityStoragePrivate::bind( QSqlQuery& query,
|
|
273 |
const QVariantHash &activity,
|
|
274 |
const QVariantHash &additionalData)
|
|
275 |
{
|
|
276 |
const QChar tag(' ');
|
|
277 |
QString queryString( query.lastQuery() );
|
|
278 |
QVariantHash::const_iterator iter;
|
|
279 |
int offset(0);
|
|
280 |
QStringList tokens;
|
|
281 |
|
|
282 |
//explode SQL query to tokens
|
|
283 |
do {
|
|
284 |
offset = queryString.indexOf(tag, 0);
|
|
285 |
if (0 < offset) {
|
|
286 |
tokens << queryString.left(offset++);
|
|
287 |
queryString = queryString.right(queryString.length() - offset);
|
|
288 |
} else {
|
|
289 |
if (0 < queryString.length()) {
|
|
290 |
tokens << queryString;
|
|
291 |
}
|
|
292 |
break;
|
|
293 |
}
|
|
294 |
} while (true);
|
|
295 |
|
|
296 |
//current Sql driver doesnt support proper query formating.
|
|
297 |
//reuest filtering data has to be binded in right order.
|
|
298 |
QStringList::iterator token = tokens.begin();
|
|
299 |
//iterate all tokens
|
|
300 |
for (; token != tokens.end(); token = tokens.erase(token)) {
|
|
301 |
//iterate all provided data and chcek if it match pattern
|
|
302 |
for ( iter = activity.constBegin();
|
|
303 |
iter != activity.constEnd();
|
|
304 |
++iter ) {
|
|
305 |
if( (*token).contains(iter.key()) ){
|
|
306 |
query.bindValue(iter.key(), iter.value());
|
|
307 |
break;
|
|
308 |
}
|
|
309 |
}
|
|
310 |
for (iter = additionalData.constBegin();
|
|
311 |
iter != additionalData.constEnd();
|
|
312 |
++iter) {
|
|
313 |
if ((*token).contains(iter.key())) {
|
|
314 |
query.bindValue(iter.key(), iter.value());
|
|
315 |
break;
|
|
316 |
}
|
|
317 |
}
|
|
318 |
}
|
|
319 |
}
|
|
320 |
|
|
321 |
// -----------------------------------------------------------------------------
|
|
322 |
//
|
|
323 |
// -----------------------------------------------------------------------------
|
|
324 |
//
|
|
325 |
int HsActivityStoragePrivate::activities(QList<QVariantHash> &results,
|
|
326 |
const QString &queryStr,
|
|
327 |
const QVariantHash &conditions)
|
|
328 |
{
|
|
329 |
results.clear();
|
|
330 |
|
|
331 |
QSqlQuery query(mConn);
|
|
332 |
query.prepare(queryStr);
|
|
333 |
bind(query, conditions);
|
|
334 |
if (exec(query)) {
|
|
335 |
QVariantHash activityEntry;
|
|
336 |
while (query.next()) {
|
|
337 |
activityEntry.clear();
|
|
338 |
QByteArray data(query.value(0).toByteArray());
|
|
339 |
QDataStream stream(&data, QIODevice::ReadOnly);
|
|
340 |
stream >> activityEntry;
|
|
341 |
results.append(activityEntry);
|
|
342 |
}
|
|
343 |
}
|
|
344 |
return getSqlErrorCode(query);
|
|
345 |
}
|