|
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 #include "activitystorage.h" |
|
18 #include "activityqueries.h" |
|
19 #include <bautils.h> |
|
20 #include <s32mem.h> |
|
21 |
|
22 _LIT(KDbName, "activity.db"); |
|
23 _LIT(KDbDrive, "c:"); |
|
24 const TInt KMaxPathLength = 256; |
|
25 |
|
26 // ----------------------------------------------------------------------------- |
|
27 // |
|
28 // ----------------------------------------------------------------------------- |
|
29 // |
|
30 CActivityStorage::CActivityStorage(RFs& session) |
|
31 : |
|
32 mFsSession(session) |
|
33 { |
|
34 // No implementation required |
|
35 } |
|
36 |
|
37 // ----------------------------------------------------------------------------- |
|
38 // |
|
39 // ----------------------------------------------------------------------------- |
|
40 // |
|
41 CActivityStorage::~CActivityStorage() |
|
42 { |
|
43 } |
|
44 |
|
45 // ----------------------------------------------------------------------------- |
|
46 // |
|
47 // ----------------------------------------------------------------------------- |
|
48 // |
|
49 CActivityStorage* CActivityStorage::NewL(RFs& session) |
|
50 { |
|
51 CActivityStorage* self = new (ELeave) CActivityStorage(session); |
|
52 CleanupStack::PushL(self); |
|
53 self->ConstructL(); |
|
54 CleanupStack::Pop(); // self; |
|
55 return self; |
|
56 } |
|
57 |
|
58 // ----------------------------------------------------------------------------- |
|
59 // |
|
60 // ----------------------------------------------------------------------------- |
|
61 // |
|
62 void CActivityStorage::ConstructL() |
|
63 { |
|
64 RBuf path; |
|
65 CleanupClosePushL( path ); |
|
66 path.CreateL(KMaxPathLength); |
|
67 User::LeaveIfError(mFsSession.PrivatePath(path )); |
|
68 path.Append(KDbName); |
|
69 path.Insert(0, KDbDrive); |
|
70 BaflUtils::EnsurePathExistsL(mFsSession, path); |
|
71 BaflUtils::FileExists(mFsSession, path) ? OpenDbL(path) : CreateDbL(path); |
|
72 CleanupStack::PopAndDestroy(&path); |
|
73 } |
|
74 |
|
75 // ----------------------------------------------------------------------------- |
|
76 // |
|
77 // ----------------------------------------------------------------------------- |
|
78 // |
|
79 void CActivityStorage::CreateDbL(const TDesC& databaseFile) |
|
80 { |
|
81 mFileStore = CPermanentFileStore::ReplaceL(mFsSession, |
|
82 databaseFile, |
|
83 EFileRead|EFileWrite); |
|
84 mFileStore->SetTypeL(mFileStore->Layout());// Set file store type |
|
85 TStreamId id = mActDb.CreateL(mFileStore);// Create stream object |
|
86 mFileStore->SetRootL(id);// Keep database id as root of store |
|
87 mFileStore->CommitL();// Complete creation by commiting |
|
88 CreateTableL(); |
|
89 } |
|
90 |
|
91 // ----------------------------------------------------------------------------- |
|
92 // |
|
93 // ----------------------------------------------------------------------------- |
|
94 // |
|
95 void CActivityStorage::OpenDbL(const TDesC& databaseFile) |
|
96 { |
|
97 mFileStore = CPermanentFileStore::OpenL(mFsSession, |
|
98 databaseFile, |
|
99 EFileRead|EFileWrite); |
|
100 mFileStore->SetTypeL(mFileStore->Layout()); /* Set file store type*/ |
|
101 mActDb.OpenL(mFileStore,mFileStore->Root()); |
|
102 CDbTableNames* tables = mActDb.TableNamesL(); |
|
103 CleanupStack::PushL(tables); |
|
104 if (0 == tables->Count()) { |
|
105 CreateTableL(); |
|
106 } |
|
107 CleanupStack::PopAndDestroy(tables); |
|
108 } |
|
109 |
|
110 // ----------------------------------------------------------------------------- |
|
111 // |
|
112 // ----------------------------------------------------------------------------- |
|
113 // |
|
114 void CActivityStorage::CreateTableL() |
|
115 { |
|
116 // Add the columns to column set |
|
117 CDbColSet* actColSet = CDbColSet::NewLC(); |
|
118 |
|
119 TDbCol appName(KApplicationColumnName, EDbColInt64);// Using default length |
|
120 appName.iAttributes = TDbCol::ENotNull; |
|
121 actColSet->AddL(appName); |
|
122 |
|
123 TDbCol actName(KActivityColumnName, EDbColText8);// Using default length |
|
124 actName.iAttributes = TDbCol::ENotNull; |
|
125 actColSet->AddL(actName); |
|
126 |
|
127 actColSet->AddL(TDbCol(KDataColumnName, EDbColLongText8));// Stream Data |
|
128 |
|
129 // Add the columns to index definition |
|
130 CDbKey* primaryKey = CDbKey::NewLC(); |
|
131 primaryKey->AddL(TDbKeyCol(KApplicationColumnName)); |
|
132 primaryKey->AddL(TDbKeyCol(KActivityColumnName)); |
|
133 primaryKey->MakePrimary(); |
|
134 |
|
135 // Create the table |
|
136 /*User::LeaveIfError(mActDb.CreateTable(KActivityTableName, |
|
137 *actColSet, |
|
138 *primaryKey));*/ |
|
139 User::LeaveIfError(mActDb.CreateTable(KActivityTableName, |
|
140 *actColSet)); |
|
141 /*User::LeaveIfError(mActDb.CreateIndex(KActivityIndexName, |
|
142 KActivityTableName, |
|
143 *primaryKey));*/ |
|
144 CleanupStack::PopAndDestroy(primaryKey); |
|
145 CleanupStack::PopAndDestroy(actColSet); |
|
146 } |
|
147 |
|
148 // ----------------------------------------------------------------------------- |
|
149 // |
|
150 // ----------------------------------------------------------------------------- |
|
151 // |
|
152 void CActivityStorage::AddActivityL(TInt appId, |
|
153 const TDesC8& actId, |
|
154 const TDesC8& data) |
|
155 { |
|
156 //verify if row already exists |
|
157 RDbView view; |
|
158 CleanupClosePushL(view); |
|
159 TRAPD( errNo, GetActivityForUpdateL(view, appId, actId)); |
|
160 if (KErrNone == errNo) { |
|
161 User::Leave(KErrAlreadyExists); |
|
162 } |
|
163 CleanupStack::PopAndDestroy(&view); |
|
164 |
|
165 //write table |
|
166 RDbTable table; |
|
167 CleanupClosePushL(table); |
|
168 User::LeaveIfError(table.Open(mActDb, KActivityTableName, table.EUpdatable)); |
|
169 CDbColSet *row = table.ColSetL(); |
|
170 CleanupStack::PushL(row); |
|
171 |
|
172 table.InsertL(); |
|
173 table.SetColL(row->ColNo(KApplicationColumnName), TInt64(appId)); |
|
174 table.SetColL(row->ColNo(KActivityColumnName), actId); |
|
175 |
|
176 //write blob data |
|
177 RDbColWriteStream stream; |
|
178 CleanupClosePushL(stream); |
|
179 stream.OpenL(table, row->ColNo(KDataColumnName)); |
|
180 stream.WriteL(data); |
|
181 CleanupStack::PopAndDestroy(&stream); |
|
182 |
|
183 table.PutL(); |
|
184 CleanupStack::PopAndDestroy(row); |
|
185 CleanupStack::PopAndDestroy(&table); |
|
186 } |
|
187 |
|
188 // ----------------------------------------------------------------------------- |
|
189 // |
|
190 // ----------------------------------------------------------------------------- |
|
191 // |
|
192 void CActivityStorage::UpdateActivityL(TInt appId, |
|
193 const TDesC8& actId, |
|
194 const TDesC8& data) |
|
195 { |
|
196 RDbView view; |
|
197 CleanupClosePushL(view); |
|
198 GetActivityForUpdateL(view, appId, actId); |
|
199 view.UpdateL(); |
|
200 CDbColSet* colSet = view.ColSetL(); |
|
201 CleanupStack::PushL(colSet); |
|
202 RDbColWriteStream writeStream; |
|
203 CleanupClosePushL(writeStream); |
|
204 writeStream.OpenL(view, colSet->ColNo(KDataColumnName)); |
|
205 writeStream.WriteL(data); |
|
206 CleanupStack::PopAndDestroy(&writeStream); |
|
207 view.PutL(); |
|
208 CleanupStack::PopAndDestroy(colSet); |
|
209 CleanupStack::PopAndDestroy(&view); |
|
210 } |
|
211 |
|
212 // ----------------------------------------------------------------------------- |
|
213 // |
|
214 // ----------------------------------------------------------------------------- |
|
215 // |
|
216 void CActivityStorage::DeleteActivityL(TInt appId,const TDesC8& actId) |
|
217 { |
|
218 HBufC *query(DeleteRowLC(appId, actId)); |
|
219 User::LeaveIfError(mActDb.Execute(*query)); |
|
220 CleanupStack::PopAndDestroy(query); |
|
221 } |
|
222 |
|
223 // ----------------------------------------------------------------------------- |
|
224 // |
|
225 // ----------------------------------------------------------------------------- |
|
226 // |
|
227 void CActivityStorage::DeleteActivitiesL(TInt appId) |
|
228 { |
|
229 HBufC *query(DeleteRowsLC(appId)); |
|
230 User::LeaveIfError(mActDb.Execute(*query)); |
|
231 CleanupStack::PopAndDestroy(query); |
|
232 } |
|
233 |
|
234 // ----------------------------------------------------------------------------- |
|
235 // |
|
236 // ----------------------------------------------------------------------------- |
|
237 // |
|
238 void CActivityStorage::ActivitiesL(RBuf8 &dst) |
|
239 { |
|
240 ActivitiesL(dst, KSelectRows() ); |
|
241 } |
|
242 |
|
243 // ----------------------------------------------------------------------------- |
|
244 // |
|
245 // ----------------------------------------------------------------------------- |
|
246 // |
|
247 void CActivityStorage::ActivitiesL(RBuf8 &dst,TInt appId) |
|
248 { |
|
249 HBufC *query(SelectRowsLC(appId)); |
|
250 ActivitiesL(dst, *query); |
|
251 CleanupStack::PopAndDestroy(query); |
|
252 } |
|
253 |
|
254 // ----------------------------------------------------------------------------- |
|
255 // |
|
256 // ----------------------------------------------------------------------------- |
|
257 // |
|
258 HBufC* CActivityStorage::SelectRowLC(TInt appId, const TDesC8& actId) const |
|
259 { |
|
260 return BuildQueryLC(KSelectRow(),appId, actId); |
|
261 } |
|
262 |
|
263 // ----------------------------------------------------------------------------- |
|
264 // |
|
265 // ----------------------------------------------------------------------------- |
|
266 // |
|
267 HBufC* CActivityStorage::SelectRowsLC(TInt appId) const |
|
268 { |
|
269 return BuildQueryLC(KSelectAppRows(), appId, KNullDesC8); |
|
270 } |
|
271 |
|
272 // ----------------------------------------------------------------------------- |
|
273 // |
|
274 // ----------------------------------------------------------------------------- |
|
275 // |
|
276 HBufC* CActivityStorage::DeleteRowLC(TInt appId, const TDesC8& actId) const |
|
277 { |
|
278 return BuildQueryLC(KDeleteRow(),appId, actId); |
|
279 } |
|
280 |
|
281 // ----------------------------------------------------------------------------- |
|
282 // |
|
283 // ----------------------------------------------------------------------------- |
|
284 // |
|
285 HBufC* CActivityStorage::DeleteRowsLC(TInt appId) const |
|
286 { |
|
287 return BuildQueryLC(KDeleteRows(),appId, KNullDesC8); |
|
288 } |
|
289 |
|
290 // ----------------------------------------------------------------------------- |
|
291 // |
|
292 // ----------------------------------------------------------------------------- |
|
293 // |
|
294 HBufC* CActivityStorage::BuildQueryLC(const TDesC& format, |
|
295 TInt appId, |
|
296 const TDesC8& actId) const |
|
297 { |
|
298 TBuf<16> appName; |
|
299 appName.AppendNum(appId); |
|
300 RBuf actName; |
|
301 CleanupClosePushL(actName); |
|
302 actName.CreateL(actId.Length()); |
|
303 actName.Copy(actId); |
|
304 HBufC* query = HBufC::NewL(format.Length() + |
|
305 appName.Length() + |
|
306 actName.Length() ); |
|
307 query->Des().AppendFormat(format, &appName, &actName); |
|
308 CleanupStack::PopAndDestroy(&actName); |
|
309 CleanupStack::PushL(query); |
|
310 return query; |
|
311 } |
|
312 |
|
313 // ----------------------------------------------------------------------------- |
|
314 // |
|
315 // ----------------------------------------------------------------------------- |
|
316 // |
|
317 TInt CActivityStorage::DataSizeL(RDbRowSet& data)const |
|
318 { |
|
319 TInt dataSize(sizeof(TInt)); |
|
320 CDbColSet* row = data.ColSetL(); |
|
321 CleanupStack::PushL(row); |
|
322 const TInt dataOffset(row->ColNo(KDataColumnName)); |
|
323 for (data.FirstL(); data.AtRow(); data.NextL()) { |
|
324 data.GetL(); |
|
325 dataSize += (sizeof(TInt) + |
|
326 data.ColLength(dataOffset)); |
|
327 } |
|
328 CleanupStack::PopAndDestroy(row); |
|
329 return dataSize; |
|
330 } |
|
331 |
|
332 // ----------------------------------------------------------------------------- |
|
333 // |
|
334 // ----------------------------------------------------------------------------- |
|
335 // |
|
336 void CActivityStorage::ExternalizeL(TDes8& dst, RDbRowSet& src)const |
|
337 { |
|
338 RDesWriteStream dstStream(dst); |
|
339 CleanupClosePushL(dstStream); |
|
340 dstStream.WriteInt32L(src.CountL()); |
|
341 RBuf8 data; |
|
342 CleanupClosePushL(data); |
|
343 CDbColSet* row = src.ColSetL(); |
|
344 CleanupStack::PushL(row); |
|
345 const TInt dataOffset(row->ColNo(KDataColumnName)); |
|
346 RDbColReadStream srcStream; |
|
347 CleanupClosePushL(srcStream); |
|
348 for (src.FirstL(); src.AtRow(); src.NextL()) { |
|
349 src.GetL(); |
|
350 if (data.MaxLength() < src.ColLength(dataOffset)) { |
|
351 data.ReAllocL(src.ColLength(dataOffset)); |
|
352 } |
|
353 data.SetLength(0); |
|
354 srcStream.OpenL(src,dataOffset); |
|
355 srcStream.ReadL(data, src.ColLength(dataOffset)); |
|
356 srcStream.Close(); |
|
357 dstStream.WriteInt32L(src.ColLength(dataOffset)); |
|
358 if (0 < src.ColLength(dataOffset)) { |
|
359 dstStream.WriteL(data, src.ColLength(dataOffset)); |
|
360 } |
|
361 } |
|
362 CleanupStack::PopAndDestroy(&srcStream); |
|
363 CleanupStack::PopAndDestroy(row); |
|
364 CleanupStack::PopAndDestroy(&data); |
|
365 CleanupStack::PopAndDestroy(&dstStream); |
|
366 } |
|
367 |
|
368 // ----------------------------------------------------------------------------- |
|
369 // |
|
370 // ----------------------------------------------------------------------------- |
|
371 // |
|
372 void CActivityStorage::ActivitiesL(RBuf8& dst, const TDesC& query) |
|
373 { |
|
374 RDbView view;// Create a view on the database |
|
375 CleanupClosePushL(view); |
|
376 User::LeaveIfError(view.Prepare(mActDb, TDbQuery(query), view.EReadOnly)); |
|
377 User::LeaveIfError(view.EvaluateAll()); |
|
378 const TInt dataSize(DataSizeL(view)); |
|
379 if(dst.MaxLength() < dataSize) { |
|
380 dst.ReAllocL(dataSize); |
|
381 } |
|
382 ExternalizeL(dst, view); |
|
383 CleanupStack::PopAndDestroy(&view); |
|
384 } |
|
385 |
|
386 // ----------------------------------------------------------------------------- |
|
387 // |
|
388 // ----------------------------------------------------------------------------- |
|
389 // |
|
390 void CActivityStorage::GetActivityForUpdateL(RDbView& view, TInt appId, const TDesC8& actId) |
|
391 { |
|
392 HBufC* query(SelectRowLC(appId, actId)); |
|
393 User::LeaveIfError(view.Prepare(mActDb, TDbQuery(*query), view.EUpdatable)); |
|
394 CleanupStack::PopAndDestroy(query); |
|
395 User::LeaveIfError(view.EvaluateAll()); |
|
396 if (!view.FirstL()) { |
|
397 User::Leave(KErrNotFound); |
|
398 } |
|
399 } |