178 { |
178 { |
179 res = CompareCIDL( view, aID ); |
179 res = CompareCIDL( view, aID ); |
180 } |
180 } |
181 view.NextL(); |
181 view.NextL(); |
182 } |
182 } |
183 |
183 |
184 CleanupStack::PopAndDestroy(); // view |
184 CleanupStack::PopAndDestroy(); // view |
185 |
185 |
186 return res; |
186 return res; |
187 } |
187 } |
188 |
188 |
189 // ----------------------------------------------------------------------------- |
189 // ----------------------------------------------------------------------------- |
190 // RDRMReplayCache::InCacheL |
190 // RDRMReplayCache::InCacheL |
191 // |
191 // |
192 // Check whether the given entry is in cache. Overloaded. |
192 // Check whether the given entry is in cache. Overloaded. |
193 // ----------------------------------------------------------------------------- |
193 // ----------------------------------------------------------------------------- |
194 // |
194 // |
195 TBool RDRMReplayCache::InCacheL( const TDesC8& aID ) |
195 TBool RDRMReplayCache::InCacheL( const TDesC8& aID ) |
196 { |
196 { |
197 TBool res = EFalse; |
197 TBool res = EFalse; |
198 |
198 |
199 RDbView view; |
199 RDbView view; |
200 InitViewLC( view, EPlainDb, EFalse ); |
200 InitViewLC( view, EPlainDb, EFalse ); |
201 |
201 |
202 view.FirstL(); |
202 view.FirstL(); |
203 |
203 |
204 while ( view.AtRow() && !res ) |
204 while ( view.AtRow() && !res ) |
205 { |
205 { |
206 view.GetL(); |
206 view.GetL(); |
207 |
207 |
208 res = CompareCIDL( view, aID ); |
208 res = CompareCIDL( view, aID ); |
209 |
209 |
210 view.NextL(); |
210 view.NextL(); |
211 } |
211 } |
212 |
212 |
213 CleanupStack::PopAndDestroy(); // view |
213 CleanupStack::PopAndDestroy(); // view |
214 |
214 |
215 return res; |
215 return res; |
216 } |
216 } |
217 |
217 |
218 // ----------------------------------------------------------------------------- |
218 // ----------------------------------------------------------------------------- |
219 // RDRMReplayCache::AddL |
219 // RDRMReplayCache::AddL |
220 // |
220 // |
221 // Add an entry to the database. Overloaded. |
221 // Add an entry to the database. Overloaded. |
222 // ----------------------------------------------------------------------------- |
222 // ----------------------------------------------------------------------------- |
223 // |
223 // |
224 void RDRMReplayCache::AddL( const TDesC8& aID, |
224 void RDRMReplayCache::AddL( const TDesC8& aID, |
225 const TTime& aTime, |
225 const TTime& aTime, |
226 const TTime& aInsertionTime ) |
226 const TTime& aInsertionTime ) |
227 { |
227 { |
228 RDbColWriteStream stream; |
228 RDbColWriteStream stream; |
229 RDbView view; |
229 RDbView view; |
230 |
230 |
231 PushL( iTimeDb ); |
231 PushL( iTimeDb ); |
232 |
232 |
233 InitViewLC( view, ETimeDb, ETrue ); |
233 InitViewLC( view, ETimeDb, ETrue ); |
234 CompactViewL( view ); |
234 CompactViewL( view ); |
235 |
235 |
236 // Oldest ones are in the list earlier, and as long as clock works |
236 // Oldest ones are in the list earlier, and as long as clock works |
237 // correctly, insertiontime(n-1)<insertiontime(n). |
237 // correctly, insertiontime(n-1)<insertiontime(n). |
238 view.EndL(); |
238 view.EndL(); |
239 |
239 |
240 User::LeaveIfError( iTimeDb.Begin() ); |
240 User::LeaveIfError( iTimeDb.Begin() ); |
241 |
241 |
242 view.InsertL(); |
242 view.InsertL(); |
243 |
243 |
244 InsertIdL( view, aID ); |
244 InsertIdL( view, aID ); |
245 view.SetColL( KDbViewInsertionTimeOrdinal, aInsertionTime.Int64() ); |
245 view.SetColL( KDbViewInsertionTimeOrdinal, aInsertionTime.Int64() ); |
246 view.SetColL( KDbViewTimeOrdinal, aTime.Int64() ); |
246 view.SetColL( KDbViewTimeOrdinal, aTime.Int64() ); |
247 |
247 |
248 view.PutL(); |
248 view.PutL(); |
249 |
249 |
250 CleanupStack::PopAndDestroy(); // view |
250 CleanupStack::PopAndDestroy(); // view |
251 |
251 |
252 User::LeaveIfError( iTimeDb.Commit() ); |
252 User::LeaveIfError( iTimeDb.Commit() ); |
253 User::LeaveIfError( iTimeDb.Compact() ); |
253 User::LeaveIfError( iTimeDb.Compact() ); |
254 |
254 |
255 Pop(); // iTimeDb |
255 Pop(); // iTimeDb |
256 } |
256 } |
257 |
257 |
258 // ----------------------------------------------------------------------------- |
258 // ----------------------------------------------------------------------------- |
259 // RDRMReplayCache::AddL |
259 // RDRMReplayCache::AddL |
260 // |
260 // |
261 // Add an entry to the database. Overloaded. |
261 // Add an entry to the database. Overloaded. |
262 // ----------------------------------------------------------------------------- |
262 // ----------------------------------------------------------------------------- |
263 // |
263 // |
264 void RDRMReplayCache::AddL( const TDesC8& aID, |
264 void RDRMReplayCache::AddL( const TDesC8& aID, |
265 const TTime& aInsertionTime ) |
265 const TTime& aInsertionTime ) |
266 { |
266 { |
267 |
267 |
268 RDbView view; |
268 RDbView view; |
269 |
269 |
270 PushL( iPlainDb ); |
270 PushL( iPlainDb ); |
271 |
271 |
272 InitViewLC( view, EPlainDb, ETrue ); |
272 InitViewLC( view, EPlainDb, ETrue ); |
273 CompactViewL( view ); |
273 CompactViewL( view ); |
274 |
274 |
275 view .EndL(); |
275 view .EndL(); |
276 |
276 |
277 User::LeaveIfError( iPlainDb.Begin() ); |
277 User::LeaveIfError( iPlainDb.Begin() ); |
278 |
278 |
279 view.InsertL(); |
279 view.InsertL(); |
280 InsertIdL( view, aID ); |
280 InsertIdL( view, aID ); |
281 view.SetColL( KDbViewInsertionTimeOrdinal, aInsertionTime.Int64() ); |
281 view.SetColL( KDbViewInsertionTimeOrdinal, aInsertionTime.Int64() ); |
282 |
282 |
283 view.PutL(); |
283 view.PutL(); |
284 |
284 |
285 CleanupStack::PopAndDestroy(); // view |
285 CleanupStack::PopAndDestroy(); // view |
286 |
286 |
287 User::LeaveIfError( iPlainDb.Commit() ); |
287 User::LeaveIfError( iPlainDb.Commit() ); |
288 User::LeaveIfError( iPlainDb.Compact() ); |
288 User::LeaveIfError( iPlainDb.Compact() ); |
289 |
289 |
290 Pop(); // iPlainDb |
290 Pop(); // iPlainDb |
291 } |
291 } |
292 |
292 |
293 // ----------------------------------------------------------------------------- |
293 // ----------------------------------------------------------------------------- |
294 // RDRMReplayCache::InitDbL |
294 // RDRMReplayCache::InitDbL |
295 // |
295 // |
296 // Initialize the databases. |
296 // Initialize the databases. |
297 // ----------------------------------------------------------------------------- |
297 // ----------------------------------------------------------------------------- |
298 // |
298 // |
299 void RDRMReplayCache::InitDbL( RDbNamedDatabase& aDb, |
299 void RDRMReplayCache::InitDbL( RDbNamedDatabase& aDb, |
300 const TDesC& aFileName, |
300 const TDesC& aFileName, |
301 TDatabaseId aId ) |
301 TDatabaseId aId ) |
302 { |
302 { |
303 TInt error = KErrNone; |
303 TInt error = KErrNone; |
304 TBool exists = BaflUtils::FileExists( *iFs, aFileName ); |
304 TBool exists = BaflUtils::FileExists( *iFs, aFileName ); |
305 |
305 |
306 if ( exists ) |
306 if ( exists ) |
307 { |
307 { |
308 TRAP( error, OpenDbL( aDb, aFileName ) ); |
308 TRAP( error, OpenDbL( aDb, aFileName ) ); |
309 } |
309 } |
310 if ( error || !exists ) |
310 if ( error || !exists ) |
311 { |
311 { |
312 ReplaceDbL( aDb, aFileName, aId ); |
312 ReplaceDbL( aDb, aFileName, aId ); |
313 } |
313 } |
314 } |
314 } |
315 |
315 |
316 // ----------------------------------------------------------------------------- |
316 // ----------------------------------------------------------------------------- |
317 // RDRMReplayCache::~RDRMReplayCache |
317 // RDRMReplayCache::~RDRMReplayCache |
318 // |
318 // |
319 // Destructor. |
319 // Destructor. |
320 // ----------------------------------------------------------------------------- |
320 // ----------------------------------------------------------------------------- |
321 // |
321 // |
322 RDRMReplayCache::~RDRMReplayCache() |
322 RDRMReplayCache::~RDRMReplayCache() |
323 { |
323 { |
324 } |
324 } |
325 |
325 |
326 // ----------------------------------------------------------------------------- |
326 // ----------------------------------------------------------------------------- |
327 // RDRMReplayCache::OpenDbL |
327 // RDRMReplayCache::OpenDbL |
328 // |
328 // |
329 // Open the database. |
329 // Open the database. |
330 // ----------------------------------------------------------------------------- |
330 // ----------------------------------------------------------------------------- |
331 // |
331 // |
332 void RDRMReplayCache::OpenDbL( RDbNamedDatabase& aDb, |
332 void RDRMReplayCache::OpenDbL( RDbNamedDatabase& aDb, |
333 const TDesC& aFileName ) |
333 const TDesC& aFileName ) |
334 { |
334 { |
335 CDbTableNames* tables = NULL; |
335 CDbTableNames* tables = NULL; |
336 |
336 |
337 User::LeaveIfError( aDb.Open( *iFs, aFileName ) ); |
337 User::LeaveIfError( aDb.Open( *iFs, aFileName ) ); |
338 CleanupClosePushL( aDb ); |
338 CleanupClosePushL( aDb ); |
339 |
339 |
340 if ( aDb.IsDamaged() ) |
340 if ( aDb.IsDamaged() ) |
341 { |
341 { |
342 User::LeaveIfError( aDb.Recover() ); |
342 User::LeaveIfError( aDb.Recover() ); |
343 } |
343 } |
344 |
344 |
345 // Sanity check. |
345 // Sanity check. |
346 tables = aDb.TableNamesL(); |
346 tables = aDb.TableNamesL(); |
347 CleanupStack::PushL( tables ); |
347 CleanupStack::PushL( tables ); |
348 |
348 |
349 if ( tables->Count() != 1 || ( *tables )[ 0 ].Compare( KReplayCacheTable ) ) |
349 if ( tables->Count() != 1 || ( *tables )[ 0 ].Compare( KReplayCacheTable ) ) |
350 { |
350 { |
351 User::Leave( KErrCorrupt ); |
351 User::Leave( KErrCorrupt ); |
352 } |
352 } |
353 |
353 |
354 CleanupStack::PopAndDestroy(); // tables |
354 CleanupStack::PopAndDestroy(); // tables |
355 CleanupStack::Pop(); // aDb |
355 CleanupStack::Pop(); // aDb |
356 } |
356 } |
357 |
357 |
358 // ----------------------------------------------------------------------------- |
358 // ----------------------------------------------------------------------------- |
359 // RDRMReplayCache::ReplaceDbL |
359 // RDRMReplayCache::ReplaceDbL |
360 // |
360 // |
361 // Replace the database. |
361 // Replace the database. |
362 // ----------------------------------------------------------------------------- |
362 // ----------------------------------------------------------------------------- |
363 // |
363 // |
364 void RDRMReplayCache::ReplaceDbL( RDbNamedDatabase& aDb, |
364 void RDRMReplayCache::ReplaceDbL( RDbNamedDatabase& aDb, |
365 const TDesC& aFileName, |
365 const TDesC& aFileName, |
366 TDatabaseId aId ) |
366 TDatabaseId aId ) |
367 { |
367 { |
368 CDbColSet* colSet = NULL; |
368 CDbColSet* colSet = NULL; |
369 // CDbKey* key = NULL; |
369 // CDbKey* key = NULL; |
370 TDbCol cidCol( KCIDColName, EDbColLongText8 ); |
370 TDbCol cidCol( KCIDColName, EDbColLongText8 ); |
371 TDbCol insertionTimeCol( KInsertionTimeColName, EDbColInt64 ); |
371 TDbCol insertionTimeCol( KInsertionTimeColName, EDbColInt64 ); |
372 TDbCol timeCol( KTimeColName, EDbColInt64 ); |
372 TDbCol timeCol( KTimeColName, EDbColInt64 ); |
373 |
373 |
374 TDoDeleteFile deletefile = { iFs, &aFileName }; |
374 TDoDeleteFile deletefile = { iFs, &aFileName }; |
375 |
375 |
376 TCleanupItem item( DoDeleteFile, &deletefile ); |
376 TCleanupItem item( DoDeleteFile, &deletefile ); |
377 CleanupStack::PushL( item ); |
377 CleanupStack::PushL( item ); |
378 |
378 |
379 User::LeaveIfError( aDb.Replace( *iFs, aFileName ) ); |
379 User::LeaveIfError( aDb.Replace( *iFs, aFileName ) ); |
380 CleanupClosePushL( aDb ); |
380 CleanupClosePushL( aDb ); |
381 |
381 |
382 // Add columns |
382 // Add columns |
383 colSet = CDbColSet::NewLC(); |
383 colSet = CDbColSet::NewLC(); |
384 colSet->AddL( cidCol ); |
384 colSet->AddL( cidCol ); |
385 colSet->AddL( insertionTimeCol ); |
385 colSet->AddL( insertionTimeCol ); |
386 |
386 |
387 if ( aId == ETimeDb ) |
387 if ( aId == ETimeDb ) |
388 { |
388 { |
389 colSet->AddL( timeCol ); |
389 colSet->AddL( timeCol ); |
390 } |
390 } |
391 |
391 |
392 User::LeaveIfError( aDb.Begin() ); |
392 User::LeaveIfError( aDb.Begin() ); |
393 User::LeaveIfError( aDb.CreateTable( KReplayCacheTable, *colSet ) ); |
393 User::LeaveIfError( aDb.CreateTable( KReplayCacheTable, *colSet ) ); |
394 User::LeaveIfError( aDb.Commit() ); |
394 User::LeaveIfError( aDb.Commit() ); |
395 |
395 |
396 CleanupStack::PopAndDestroy(); // colSet |
396 CleanupStack::PopAndDestroy(); // colSet |
397 CleanupStack::Pop(); // aDb |
397 CleanupStack::Pop(); // aDb |
398 CleanupStack::Pop(); // item |
398 CleanupStack::Pop(); // item |
399 } |
399 } |
400 |
400 |
401 // ----------------------------------------------------------------------------- |
401 // ----------------------------------------------------------------------------- |
402 // RDRMReplayCache::InitViewLC |
402 // RDRMReplayCache::InitViewLC |
403 // |
403 // |
404 // Initialize the view. |
404 // Initialize the view. |
405 // ----------------------------------------------------------------------------- |
405 // ----------------------------------------------------------------------------- |
406 // |
406 // |
407 void RDRMReplayCache::InitViewLC( RDbView& aView, |
407 void RDRMReplayCache::InitViewLC( RDbView& aView, |
408 TDatabaseId aId, |
408 TDatabaseId aId, |
409 TBool aUpdate ) |
409 TBool aUpdate ) |
410 { |
410 { |
411 RDbDatabase* db = ( aId == ETimeDb ? &iTimeDb : &iPlainDb ); |
411 RDbDatabase* db = ( aId == ETimeDb ? &iTimeDb : &iPlainDb ); |
412 |
412 |
413 User::LeaveIfError( |
413 User::LeaveIfError( |
414 aView.Prepare( *db, |
414 aView.Prepare( *db, |
415 TDbQuery( KViewInitQuery, EDbCompareCollated ), |
415 TDbQuery( KViewInitQuery, EDbCompareCollated ), |
416 aUpdate ? RDbRowSet::EUpdatable : RDbRowSet::EReadOnly ) ); |
416 aUpdate ? RDbRowSet::EUpdatable : RDbRowSet::EReadOnly ) ); |
417 |
417 |
418 CleanupClosePushL( aView ); |
418 CleanupClosePushL( aView ); |
419 |
419 |
420 User::LeaveIfError( aView.EvaluateAll() ); |
420 User::LeaveIfError( aView.EvaluateAll() ); |
421 } |
421 } |
422 |
422 |
423 // ----------------------------------------------------------------------------- |
423 // ----------------------------------------------------------------------------- |
424 // RDRMReplayCache::CompareCIDL |
424 // RDRMReplayCache::CompareCIDL |
425 // |
425 // |
426 // Compare whether the rowset's CID matches the given CID. |
426 // Compare whether the rowset's CID matches the given CID. |
427 // ----------------------------------------------------------------------------- |
427 // ----------------------------------------------------------------------------- |
428 // |
428 // |
429 TBool RDRMReplayCache::CompareCIDL( RDbRowSet& aView, |
429 TBool RDRMReplayCache::CompareCIDL( RDbRowSet& aView, |
430 const TDesC8& aCID ) |
430 const TDesC8& aCID ) |
431 { |
431 { |
432 TBool res = EFalse; |
432 TBool res = EFalse; |
433 |
433 |
434 TInt size = aView.ColLength( KDbViewIDOrdinal ); |
434 TInt size = aView.ColLength( KDbViewIDOrdinal ); |
435 |
435 |
436 RDbColReadStream colData; |
436 RDbColReadStream colData; |
437 colData.OpenLC( aView, KDbViewIDOrdinal ); |
437 colData.OpenLC( aView, KDbViewIDOrdinal ); |
438 |
438 |
439 // The data contains also the cardinality of the CID data, but anyway... |
439 // The data contains also the cardinality of the CID data, but anyway... |
440 HBufC8* des = HBufC8::NewLC( colData, size ); |
440 HBufC8* des = HBufC8::NewLC( colData, size ); |
441 |
441 |
442 if ( aCID.CompareC( *des ) == 0 ) |
442 if ( aCID.CompareC( *des ) == 0 ) |
443 { |
443 { |
444 res = ETrue; |
444 res = ETrue; |
445 } |
445 } |
446 |
446 |
447 CleanupStack::PopAndDestroy(); // des |
447 CleanupStack::PopAndDestroy(); // des |
448 CleanupStack::PopAndDestroy(); // colData |
448 CleanupStack::PopAndDestroy(); // colData |
449 |
449 |
450 return res; |
450 return res; |
451 } |
451 } |
452 |
452 |
453 // ----------------------------------------------------------------------------- |
453 // ----------------------------------------------------------------------------- |
454 // RDRMReplayCache::PushL |
454 // RDRMReplayCache::PushL |
455 // |
455 // |
456 // Push a cleanup item to cleanup stack. |
456 // Push a cleanup item to cleanup stack. |
457 // ----------------------------------------------------------------------------- |
457 // ----------------------------------------------------------------------------- |
458 // |
458 // |
459 void RDRMReplayCache::PushL( RDbDatabase& aDb ) |
459 void RDRMReplayCache::PushL( RDbDatabase& aDb ) |
460 { |
460 { |
461 TCleanupItem item( DoRollBack, &aDb ); |
461 TCleanupItem item( DoRollBack, &aDb ); |
462 CleanupStack::PushL( item ); |
462 CleanupStack::PushL( item ); |
463 } |
463 } |