|
1 /* |
|
2 * Copyright (c) 2007 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: This class is responsible for managing all database access |
|
15 * databases. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 |
|
22 #include <sqldb.h> |
|
23 #include <badesca.h> |
|
24 #include <f32file.h> |
|
25 // PREQ2536 the files sqlrowsetutil.h and sqlrowsetutil.cpp has been removed |
|
26 //#ifdef __WINSCW__ |
|
27 //#include <sqlrowsetutil.h> |
|
28 //#endif |
|
29 #include <sysutil.h> |
|
30 #ifdef __RAMDISK_PERF_ENABLE |
|
31 #include <centralrepository.h> |
|
32 #include <bautils.h> |
|
33 #include <mpxinternalcrkeys.h> |
|
34 #endif //__RAMDISK_PERF_ENABLE |
|
35 |
|
36 #include <mpxlog.h> |
|
37 |
|
38 #include "mpxdbcommondef.h" |
|
39 #include "mpxtable.h" |
|
40 #include "mpxdbmanager.h" |
|
41 |
|
42 // CONSTANTS |
|
43 |
|
44 // Version of Database |
|
45 const TInt KMPXDbVersion[] = {6,0,0}; |
|
46 |
|
47 _LIT8( KMCSqlConfig, "cache_size=1024; page_size=16384; " ); |
|
48 |
|
49 _LIT(KSecureFilePath, "%S[%x]%S"); |
|
50 _LIT(KRootDrive, "C:"); |
|
51 _LIT(KAliasName, "%1SDrive"); |
|
52 _LIT(KBeginTransaction, "BEGIN TRANSACTION"); |
|
53 _LIT(KCommitTransaction, "COMMIT TRANSACTION"); |
|
54 _LIT(KRollbackTransaction, "ROLLBACK TRANSACTION"); |
|
55 _LIT(KOrderByToken, "ORDER BY"); |
|
56 _LIT(KDBNameToken, ":dbname"); |
|
57 _LIT(KPlDBNameToken, ":pldbname"); |
|
58 _LIT(KUnionAllToken, " UNION ALL "); |
|
59 _LIT(KSelectToken, "SELECT"); |
|
60 |
|
61 //for database deletion |
|
62 _LIT( KDBFilePath, "\\private\\10281e17\\" ); |
|
63 _LIT( KDBFilePattern, "*.db*" ); |
|
64 |
|
65 #ifdef _DEBUG |
|
66 _LIT(KTableQuery, "SELECT * FROM %S"); |
|
67 _LIT(KAttachedTableQuery, "SELECT * FROM :dbname.%S"); |
|
68 _LIT(KFindAllCDriveTablesQuery, "SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name"); |
|
69 _LIT(KFindAllAttachedTablesQuery, "SELECT name FROM :dbname.sqlite_master WHERE type = 'table' ORDER BY name"); |
|
70 _LIT(KNameColumn, "name"); |
|
71 #endif |
|
72 |
|
73 const TInt KMaxLogQuery = 248; |
|
74 const TInt KBufIncrement = 10; |
|
75 |
|
76 #ifdef __RAMDISK_PERF_ENABLE |
|
77 _LIT(KSecurePath, "[%x]%S"); |
|
78 _LIT(KRAMAliasName, "%S"); |
|
79 _LIT( KDummyDbFile, "%c:\\private\\10281e17\\dummydb.dat" ); |
|
80 const TInt64 KMPMegaByte = 1048576; |
|
81 const TInt64 KMPEstimatedSongInBytes = KMPMegaByte * 2; |
|
82 const TInt KMPEstimatedSizePerDBEntry = 3000; // worst scenario, can be lower if needed |
|
83 const TInt KMPMinimumRAMSizeToRun = 6 * KMPMegaByte; |
|
84 // if RAM is lower than 5MB, doesn't seem enough for SQL as well. |
|
85 // so we set this number to move back DBs before being kicked out |
|
86 |
|
87 #endif //__RAMDISK_PERF_ENABLE |
|
88 |
|
89 // Used to suppress overflow when appending formatted text to a buffer. |
|
90 class TOverflowHandle : |
|
91 public TDesOverflow |
|
92 { |
|
93 public: |
|
94 TOverflowHandle() : |
|
95 iFlag(EFalse) |
|
96 { |
|
97 } |
|
98 |
|
99 virtual void Overflow(TDes& /* aDes */) |
|
100 { |
|
101 iFlag = ETrue; |
|
102 return; |
|
103 } |
|
104 |
|
105 TBool GetOverflowFlag() |
|
106 { |
|
107 TBool flag(iFlag); |
|
108 iFlag = EFalse; |
|
109 return flag; |
|
110 } |
|
111 protected: |
|
112 TBool iFlag; |
|
113 }; |
|
114 |
|
115 // ============================ MEMBER FUNCTIONS ============================== |
|
116 |
|
117 // ---------------------------------------------------------------------------- |
|
118 // Constructor |
|
119 // ---------------------------------------------------------------------------- |
|
120 // |
|
121 EXPORT_C CMPXDbManager::CMPXDbManager( |
|
122 RFs& aFs) : |
|
123 iFs(aFs), |
|
124 iRAMDiskPerfEnabled(EFalse), |
|
125 iMaximumAllowedRAMDiskSpaceToCopy(0), |
|
126 iRAMInUse(EFalse) |
|
127 { |
|
128 MPX_FUNC("CMPXDbManager::CMPXDbManager"); |
|
129 } |
|
130 |
|
131 // ---------------------------------------------------------------------------- |
|
132 // Second phase constructor. |
|
133 // ---------------------------------------------------------------------------- |
|
134 // |
|
135 EXPORT_C void CMPXDbManager::ConstructL( |
|
136 const TFileName& aDatabaseFile) |
|
137 { |
|
138 MPX_FUNC("CMPXDbManager::ConstructL"); |
|
139 iDbFile = aDatabaseFile.AllocL(); |
|
140 |
|
141 #ifdef __RAMDISK_PERF_ENABLE |
|
142 TInt flags( 0 ); |
|
143 CRepository* repository = CRepository::NewLC( KCRUidMPXMPFeatures ); |
|
144 User::LeaveIfError( repository->Get( KMPXMPLocalVariation, flags )); |
|
145 MPX_DEBUG2("CMPXDbManager::ConstructL KMPXMPLocalVariation %d", flags); |
|
146 iRAMDiskPerfEnabled = static_cast<TBool>( flags & KMPXEnableRAMDisk ); |
|
147 |
|
148 TInt temp; |
|
149 User::LeaveIfError( repository->Get( KMAXAllowedDiskSpaceToCopy, temp) ); |
|
150 iMaximumAllowedRAMDiskSpaceToCopy = temp * KMPMegaByte; |
|
151 CleanupStack::PopAndDestroy(repository); |
|
152 |
|
153 if ( iRAMDiskPerfEnabled ) |
|
154 { |
|
155 MPX_DEBUG1("CMPXDbManager::ConstructL RAMDisk performance is enabled."); |
|
156 MPX_DEBUG2("CMPXDbManager::ConstructL RAMDisk iMaximumAllowedRAMDiskSpaceToCopy=%Lu", iMaximumAllowedRAMDiskSpaceToCopy); |
|
157 if ( GetRAMDiskPath() != KErrNone ) |
|
158 { |
|
159 // Error finding ram drive, disable ram disk |
|
160 iRAMDiskPerfEnabled = EFalse; |
|
161 } |
|
162 } |
|
163 else |
|
164 { |
|
165 MPX_DEBUG2("CMPXDbManager::ConstructL RAMDisk performance is NOT enabled flag=%d", flags); |
|
166 MPX_DEBUG2("CMPXDbManager::ConstructL RAMDisk iMaximumAllowedRAMDiskSpaceToCopy=%Lu", iMaximumAllowedRAMDiskSpaceToCopy); |
|
167 } |
|
168 #endif //__RAMDISK_PERF_ENABLE |
|
169 } |
|
170 |
|
171 // ---------------------------------------------------------------------------- |
|
172 // Destructor |
|
173 // ---------------------------------------------------------------------------- |
|
174 // |
|
175 EXPORT_C CMPXDbManager::~CMPXDbManager() |
|
176 { |
|
177 MPX_FUNC("CMPXDbManager::~CMPXDbManager"); |
|
178 |
|
179 // Close the state array |
|
180 iPreparedStatements.Close(); |
|
181 |
|
182 // Close and destroy all RSQLStatements |
|
183 TInt c( iStatements.Count() ); |
|
184 for( TInt i=0; i<c; ++i ) |
|
185 { |
|
186 iStatements[i]->Close(); |
|
187 } |
|
188 iStatements.ResetAndDestroy(); |
|
189 |
|
190 iTables.Close(); |
|
191 CloseAllDatabases(); |
|
192 |
|
193 delete iDbFile; |
|
194 iDatabaseHandles.Close(); |
|
195 } |
|
196 |
|
197 // ---------------------------------------------------------------------------- |
|
198 // Checks if all databases have been initialized. |
|
199 // ---------------------------------------------------------------------------- |
|
200 // |
|
201 EXPORT_C TBool CMPXDbManager::IsInitialized() |
|
202 { |
|
203 MPX_FUNC("CMPXDbManager::IsInitialized"); |
|
204 return iInitialized; |
|
205 } |
|
206 |
|
207 // ---------------------------------------------------------------------------- |
|
208 // Begins a transaction on all databases. |
|
209 // ---------------------------------------------------------------------------- |
|
210 // |
|
211 EXPORT_C void CMPXDbManager::BeginL() |
|
212 { |
|
213 MPX_FUNC("CMPXDbManager::BeginL"); |
|
214 |
|
215 ASSERT(iTransactionCount >= 0); |
|
216 |
|
217 if (++iTransactionCount == 1) |
|
218 { |
|
219 DoBeginL(); |
|
220 } |
|
221 } |
|
222 |
|
223 void CMPXDbManager::DoBeginL() |
|
224 { |
|
225 MPX_FUNC("CMPXDbManager::DoBeginL"); |
|
226 |
|
227 TInt err = iDatabase.Exec(KBeginTransaction); |
|
228 |
|
229 // transforms SQL error to KErrNotReady |
|
230 if( (err <= KSqlErrGeneral && err >= KSqlErrNotDb) || err == KSqlErrStmtExpired ) |
|
231 { |
|
232 User::Leave(KErrNotReady); |
|
233 } |
|
234 else |
|
235 { |
|
236 User::LeaveIfError(err); |
|
237 } |
|
238 } |
|
239 |
|
240 // ---------------------------------------------------------------------------- |
|
241 // Copy all DBs to RAM disk |
|
242 // ---------------------------------------------------------------------------- |
|
243 // |
|
244 EXPORT_C void CMPXDbManager::CopyDBsToRamL( TBool aIsMTPInUse ) |
|
245 { |
|
246 #ifdef __RAMDISK_PERF_ENABLE |
|
247 MPX_DEBUG1("-->CMPXDbManager::CopyDBsToRamL"); |
|
248 if( iRAMDiskPerfEnabled ) |
|
249 { |
|
250 if ( !IsRamDiskSpaceAvailable() ) |
|
251 { |
|
252 return; |
|
253 } |
|
254 |
|
255 // Check if we are over the allowed ram space. |
|
256 TInt dbSize=0; |
|
257 TInt err = GetTotalDatabasesSize(dbSize); |
|
258 if ( err || (dbSize > iMaximumAllowedRAMDiskSpaceToCopy) ) |
|
259 { |
|
260 MPX_DEBUG2("<--CMPXDbManager::CopyDBsToRamL Over the allowed Ram disk limit %Lu", iMaximumAllowedRAMDiskSpaceToCopy ); |
|
261 return; |
|
262 } |
|
263 |
|
264 TInt transactionCount = iTransactionCount; |
|
265 if (iTransactionCount > 0) |
|
266 { |
|
267 iTransactionCount = 0; |
|
268 DoCommitL(); |
|
269 } |
|
270 |
|
271 TInt count(iDatabaseHandles.Count()); |
|
272 for ( TInt i = 0; i < count ; ++i ) |
|
273 { |
|
274 if ( ! iDatabaseHandles[i].iOpen ) |
|
275 { |
|
276 MPX_DEBUG1("CMPXDbManager::CopyDBsToRamL DB not open (assuming drive is not present)"); |
|
277 continue; |
|
278 } |
|
279 if ( iDatabaseHandles[i].iUseRAMdb ) |
|
280 { |
|
281 // already used |
|
282 MPX_DEBUG1("CMPXDbManager::CopyDBsToRamL iUseRAMdb already ETrue"); |
|
283 continue; |
|
284 } |
|
285 CloseDatabaseAtIndexL( i ); // let leave: not much we can do if we can't close the original DB |
|
286 DoCopyDBToRam( i, aIsMTPInUse ); // copies if it can |
|
287 TRAPD( err, OpenDatabaseAtIndexL( i ) ); |
|
288 if ( err != KErrNone ) |
|
289 { |
|
290 MPX_DEBUG2("CMPXDbManager::CopyDBsToRamL OpenDatabaseAtIndexL leave=%d", err); |
|
291 RemoveDummyFile(i); |
|
292 if ( iDatabaseHandles[i].iUseRAMdb ) |
|
293 { |
|
294 // go back to disk DB |
|
295 TRAP_IGNORE(CloseDatabaseAtIndexL( i )); |
|
296 iDatabaseHandles[i].iUseRAMdb = EFalse; |
|
297 OpenDatabaseAtIndexL( i ); |
|
298 continue; |
|
299 } |
|
300 else |
|
301 { |
|
302 User::Leave( err ); |
|
303 } |
|
304 } |
|
305 } |
|
306 |
|
307 if (transactionCount > 0) |
|
308 { |
|
309 DoBeginL(); |
|
310 iTransactionCount = transactionCount; |
|
311 } |
|
312 } |
|
313 iRAMInUse = ETrue; |
|
314 |
|
315 MPX_DEBUG1("<--CMPXDbManager::CopyDBsToRamL"); |
|
316 #endif //__RAMDISK_PERF_ENABLE |
|
317 |
|
318 } |
|
319 |
|
320 |
|
321 // ---------------------------------------------------------------------------- |
|
322 // CMPXDbManager::DoCopyDBToRam |
|
323 // ---------------------------------------------------------------------------- |
|
324 // |
|
325 TBool CMPXDbManager::DoCopyDBToRam( TInt aIndex, TBool aIsMTPInUse ) |
|
326 { |
|
327 #ifdef __RAMDISK_PERF_ENABLE |
|
328 MPX_DEBUG2("-->CMPXDbManager::DoCopyDBsToRam drive=%d", (TInt)iDatabaseHandles[aIndex].iDrive); |
|
329 DatabaseHandle& database = iDatabaseHandles[aIndex]; |
|
330 TInt err = KErrNone; |
|
331 |
|
332 delete database.iOrigFullFilePath; |
|
333 database.iOrigFullFilePath = 0; |
|
334 delete database.iTargetFullFilePath; |
|
335 database.iTargetFullFilePath = 0; |
|
336 TRAP (err, |
|
337 database.iOrigFullFilePath = CreateFullFilenameL( database.iDrive ); |
|
338 database.iUseRAMdb = ETrue; // must turn this on to create RAM filename |
|
339 database.iTargetFullFilePath = CreateFullFilenameL( database.iDrive ); |
|
340 BaflUtils::EnsurePathExistsL( iFs, *database.iTargetFullFilePath )); |
|
341 database.iUseRAMdb = EFalse; |
|
342 if (err != KErrNone) |
|
343 { |
|
344 MPX_DEBUG1("CMPXDbManager::DoCopyDBsToRamL() CreateFilenameL or EnsurePathExistsL failed"); |
|
345 return EFalse; |
|
346 } |
|
347 MPX_DEBUG2("RAMDisk src path=%S", database.iOrigFullFilePath); |
|
348 MPX_DEBUG2("RAMDisk dst path=%S", database.iTargetFullFilePath); |
|
349 |
|
350 if (!BlockDiskSpace( aIndex, aIsMTPInUse ) ) |
|
351 { |
|
352 MPX_DEBUG1("CMPXDbManager::DoCopyDBsToRamL() BlockDiskSpace failed"); |
|
353 return EFalse; // continue for next drive |
|
354 } |
|
355 |
|
356 if ( BaflUtils::CopyFile(iFs, *database.iOrigFullFilePath, *database.iTargetFullFilePath ) != KErrNone ) |
|
357 { |
|
358 RemoveDummyFile( aIndex ); |
|
359 return EFalse; |
|
360 } |
|
361 MPX_DEBUG2("RAMDisk Database copied=%d", (TInt)database.iDrive); |
|
362 database.iUseRAMdb = ETrue; // succeeded moving DB to RAM |
|
363 MPX_DEBUG1("<--CMPXDbManager::DoCopyDBsToRamL"); |
|
364 return ETrue; |
|
365 #else |
|
366 return EFalse; |
|
367 #endif //__RAMDISK_PERF_ENABLE |
|
368 } |
|
369 |
|
370 // ---------------------------------------------------------------------------- |
|
371 // Copy all DBs from RAM disk back to normal drives |
|
372 // ---------------------------------------------------------------------------- |
|
373 // |
|
374 EXPORT_C void CMPXDbManager::CopyDBsFromRamL() |
|
375 { |
|
376 MPX_FUNC("CMPXDbManager::CopyDBsFromRamL"); |
|
377 #ifdef __RAMDISK_PERF_ENABLE |
|
378 if( iRAMDiskPerfEnabled ) |
|
379 { |
|
380 TInt transactionCount = iTransactionCount; |
|
381 if (iTransactionCount > 0) |
|
382 { |
|
383 iTransactionCount = 0; |
|
384 TRAP_IGNORE( DoCommitL() ); |
|
385 } |
|
386 |
|
387 TInt count(iDatabaseHandles.Count()); |
|
388 TInt leaveError = KErrNone; |
|
389 iRAMInUse = EFalse; |
|
390 // Should not leave until all the databases have been copied from RAM drive. |
|
391 for (TInt i = 0; i < count; ++i) |
|
392 { |
|
393 if ( !iDatabaseHandles[i].iUseRAMdb ) |
|
394 { |
|
395 continue; |
|
396 } |
|
397 TRAPD( error, CloseDatabaseAtIndexL( i ) ); |
|
398 if ( error ) |
|
399 { |
|
400 // Can't close db on RAM drive, so cleanup. |
|
401 MPX_DEBUG2("CMPXDbManager::CopyDBsFromRamL CloseDatabaseAtIndexL fail: error = %d", error); |
|
402 // Delete database on RAM drive. |
|
403 BaflUtils::DeleteFile(iFs, *iDatabaseHandles[i].iTargetFullFilePath); |
|
404 // Delete dummy file |
|
405 RemoveDummyFile(i); |
|
406 } |
|
407 else |
|
408 { |
|
409 DoCopyDBFromRam(i); |
|
410 } |
|
411 iDatabaseHandles[i].iUseRAMdb = EFalse; |
|
412 // open db from drive |
|
413 TRAP( error, OpenDatabaseAtIndexL( i ) ); |
|
414 if ( error && !leaveError ) |
|
415 { |
|
416 leaveError = error; |
|
417 } |
|
418 } |
|
419 |
|
420 // leave if error |
|
421 User::LeaveIfError(leaveError); |
|
422 |
|
423 if (transactionCount > 0) |
|
424 { |
|
425 DoBeginL(); |
|
426 iTransactionCount = transactionCount; |
|
427 } |
|
428 } |
|
429 #endif //__RAMDISK_PERF_ENABLE |
|
430 } |
|
431 |
|
432 |
|
433 // ---------------------------------------------------------------------------- |
|
434 // CMPXDbManager::DoCopyDBsToRam |
|
435 // ---------------------------------------------------------------------------- |
|
436 // |
|
437 void CMPXDbManager::DoCopyDBFromRam( TInt aIndex ) |
|
438 { |
|
439 #ifdef __RAMDISK_PERF_ENABLE |
|
440 MPX_DEBUG1("-->CMPXDbManager::DoCopyDBsFromRam"); |
|
441 DatabaseHandle& database = iDatabaseHandles[aIndex]; |
|
442 |
|
443 //Copy Db from RAM to replace dummy file |
|
444 TRAPD(error, ReplaceFileL( *database.iTargetFullFilePath, database.iDummyFilePath)); |
|
445 MPX_DEBUG2("CMPXDbManager::CopyDBsFromRam RAMDisk copied over dummy, error=%d", error); |
|
446 |
|
447 // done with RAM DB (whether copying succeeded or not) so can delete it |
|
448 // can ignore errors since we cannot do anything if this fails |
|
449 BaflUtils::DeleteFile(iFs, *database.iTargetFullFilePath); |
|
450 MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRam RAM DB deleted"); |
|
451 |
|
452 if ( error == KErrNone ) |
|
453 { |
|
454 // Delete old DB on drive |
|
455 // Can ignore error: either original does not exist or something is wrong and can't help it |
|
456 BaflUtils::DeleteFile(iFs, *database.iOrigFullFilePath); |
|
457 MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRam old DB on drive deleted"); |
|
458 |
|
459 // Rename dummy file to be original file name |
|
460 error = BaflUtils::RenameFile(iFs, database.iDummyFilePath, *database.iOrigFullFilePath); |
|
461 MPX_DEBUG2("CMPXDbManager::CopyDBsFromRam dummy file renamed, error=%d", error); |
|
462 if ( error ) |
|
463 { |
|
464 // Error renaming dummy file, delete dummy file. |
|
465 RemoveDummyFile(aIndex); |
|
466 } |
|
467 } |
|
468 else |
|
469 { |
|
470 RemoveDummyFile(aIndex); |
|
471 MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRam dummy file deleted"); |
|
472 } |
|
473 |
|
474 MPX_DEBUG1("<--CMPXDbManager::DoCopyDBsFromRam"); |
|
475 #endif //__RAMDISK_PERF_ENABLE |
|
476 } |
|
477 |
|
478 // ---------------------------------------------------------------------------- |
|
479 // CMPXDbManager::ReplaceFileL |
|
480 // |
|
481 // Replaces a file with another writing over the destination file. |
|
482 // Leaves on error. |
|
483 // Implementation follows CFileMan::Copy except that |
|
484 // - we don't resize target file to zero |
|
485 // - we can assume that source file already exists |
|
486 // - we don't copy file attributes & timestamp |
|
487 // ---------------------------------------------------------------------------- |
|
488 // |
|
489 void CMPXDbManager::ReplaceFileL( const TDesC& aSrcName, const TDesC& aDstName ) |
|
490 { |
|
491 // open files |
|
492 RFile srcFile; |
|
493 User::LeaveIfError( srcFile.Open(iFs, aSrcName, EFileRead|EFileShareReadersOnly) ); |
|
494 CleanupClosePushL( srcFile ); |
|
495 |
|
496 RFile dstFile; |
|
497 TInt error = dstFile.Open(iFs, aDstName, EFileWrite|EFileWriteDirectIO|EFileShareExclusive); |
|
498 if (error == KErrNotFound) |
|
499 { |
|
500 error = dstFile.Create(iFs, aDstName, EFileWrite|EFileWriteDirectIO|EFileShareExclusive); |
|
501 } |
|
502 User::LeaveIfError ( error ); |
|
503 CleanupClosePushL( dstFile ); |
|
504 |
|
505 // resize destination file |
|
506 TInt remainingBytes; |
|
507 User::LeaveIfError( srcFile.Size(remainingBytes) ); |
|
508 User::LeaveIfError( dstFile.SetSize(remainingBytes) ); |
|
509 |
|
510 // allocate buffer |
|
511 const TInt KBigBufSize = 512 * 1024; |
|
512 const TInt KMediumBufSize = 32 * 1024; |
|
513 const TInt KSmallBufSize = 4 * 1024; |
|
514 HBufC8* bufPtr=HBufC8::New( Min(KBigBufSize, remainingBytes) ); |
|
515 if (bufPtr==NULL) |
|
516 bufPtr=HBufC8::New(KMediumBufSize); |
|
517 if (bufPtr==NULL) |
|
518 bufPtr=HBufC8::New(KSmallBufSize); |
|
519 if (bufPtr == NULL) |
|
520 User::Leave(KErrNoMemory); |
|
521 CleanupStack::PushL(bufPtr); |
|
522 |
|
523 // copy |
|
524 TPtr8 copyBuf=bufPtr->Des(); |
|
525 TInt pos=0; |
|
526 while( remainingBytes > 0 ) |
|
527 { |
|
528 TInt s = Min( remainingBytes, copyBuf.MaxSize() ); |
|
529 TInt ret = srcFile.Read(pos, copyBuf, s); |
|
530 if (ret == KErrNone && copyBuf.Length()!= s ) |
|
531 { |
|
532 ret = KErrCorrupt; |
|
533 } |
|
534 if (ret == KErrNone) |
|
535 { |
|
536 ret = dstFile.Write(pos, copyBuf, s); |
|
537 } |
|
538 User::LeaveIfError (ret); |
|
539 pos += s; |
|
540 remainingBytes -= s; |
|
541 } |
|
542 User::LeaveIfError( dstFile.Flush() ); |
|
543 CleanupStack::PopAndDestroy(3); // bufPtr, dstFile, srcFile |
|
544 } |
|
545 |
|
546 // ---------------------------------------------------------------------------- |
|
547 // CMPXDbManager::RemoveDummyFile |
|
548 // ---------------------------------------------------------------------------- |
|
549 // |
|
550 void CMPXDbManager::RemoveDummyFile( TInt index ) |
|
551 { |
|
552 #ifdef __RAMDISK_PERF_ENABLE |
|
553 MPX_DEBUG1("-->CMPXDbManager::RemoveDummyFile"); |
|
554 |
|
555 if ( iDatabaseHandles[index].iDummyFilePath.Length() ) |
|
556 { |
|
557 BaflUtils::DeleteFile(iFs, iDatabaseHandles[index].iDummyFilePath); |
|
558 iDatabaseHandles[index].iDummyFilePath.Zero(); |
|
559 } |
|
560 MPX_DEBUG1("<--CMPXDbManager::RemoveDummyFile"); |
|
561 #endif //__RAMDISK_PERF_ENABLE |
|
562 |
|
563 } |
|
564 |
|
565 |
|
566 // ---------------------------------------------------------------------------- |
|
567 // Commits a transaction on all databases. |
|
568 // ---------------------------------------------------------------------------- |
|
569 // |
|
570 EXPORT_C void CMPXDbManager::CommitL() |
|
571 { |
|
572 MPX_FUNC("CMPXDbManager::CommitL"); |
|
573 |
|
574 if(iTransactionCount > 0) |
|
575 { |
|
576 if (--iTransactionCount == 0) |
|
577 { |
|
578 DoCommitL(); |
|
579 } |
|
580 } |
|
581 } |
|
582 |
|
583 // ---------------------------------------------------------------------------- |
|
584 // Commits a transaction on all databases. |
|
585 // ---------------------------------------------------------------------------- |
|
586 // |
|
587 void CMPXDbManager::DoCommitL() |
|
588 { |
|
589 MPX_FUNC("CMPXDbManager::DoCommitL"); |
|
590 TInt err = iDatabase.Exec(KCommitTransaction); |
|
591 |
|
592 // transforms SQL error to KErrNotReady |
|
593 if( (err <= KSqlErrGeneral && err >= KSqlErrNotDb) || err == KSqlErrStmtExpired ) |
|
594 { |
|
595 MPX_DEBUG2("CMPXDbManager::CommitL failed err=%d", err); |
|
596 User::Leave(KErrNotReady); |
|
597 } |
|
598 else |
|
599 { |
|
600 User::LeaveIfError(err); |
|
601 } |
|
602 } |
|
603 |
|
604 // ---------------------------------------------------------------------------- |
|
605 // Rolls back a transaction on all databases. |
|
606 // ---------------------------------------------------------------------------- |
|
607 // |
|
608 EXPORT_C void CMPXDbManager::RollbackL() |
|
609 { |
|
610 MPX_FUNC("CMPXDbManager::RollbackL"); |
|
611 |
|
612 if(iTransactionCount > 0) |
|
613 { |
|
614 if (--iTransactionCount == 0) |
|
615 { |
|
616 TInt err = iDatabase.Exec(KRollbackTransaction); |
|
617 |
|
618 // transforms SQL error to KErrNotReady |
|
619 if( (err <= KSqlErrGeneral && err >= KSqlErrNotDb) || err == KSqlErrStmtExpired ) |
|
620 { |
|
621 User::Leave(KErrNotReady); |
|
622 } |
|
623 else |
|
624 { |
|
625 User::LeaveIfError(err); |
|
626 } |
|
627 } |
|
628 } |
|
629 } |
|
630 |
|
631 // ---------------------------------------------------------------------------- |
|
632 // Rolls back a transaction on all databases. |
|
633 // ---------------------------------------------------------------------------- |
|
634 // |
|
635 EXPORT_C TBool CMPXDbManager::InTransaction() |
|
636 { |
|
637 MPX_FUNC("CMPXDbManager::InTransaction"); |
|
638 return iDatabase.InTransaction(); |
|
639 } |
|
640 |
|
641 // ---------------------------------------------------------------------------- |
|
642 // Tries to create and open the databases on all specified drives. |
|
643 // ---------------------------------------------------------------------------- |
|
644 // |
|
645 EXPORT_C void CMPXDbManager::InitDatabasesL( |
|
646 RArray<TInt> aDrives) |
|
647 { |
|
648 MPX_FUNC("CMPXDbManager::InitDatabasesL"); |
|
649 |
|
650 CloseAllDatabases(); |
|
651 |
|
652 TDriveUnit cdrive(KRootDrive()); |
|
653 |
|
654 CreateDatabaseL(cdrive); |
|
655 OpenRootDatabaseL(); |
|
656 |
|
657 TInt count(aDrives.Count()); |
|
658 for (TInt i = 0; i < count; ++i) |
|
659 { |
|
660 TDriveUnit drive(aDrives[i]); |
|
661 if ((drive != cdrive) && !IsRemoteDrive(static_cast<TDriveNumber>(aDrives[i]))) |
|
662 { |
|
663 const TDesC& driveName = drive.Name(); |
|
664 |
|
665 DatabaseHandle handle; |
|
666 |
|
667 handle.iDrive = aDrives[i]; |
|
668 handle.iAliasname = HBufC::NewL(KAliasName().Length()); |
|
669 handle.iAliasname->Des().Format(KAliasName, &driveName); |
|
670 handle.iOpen = EFalse; |
|
671 #ifdef __RAMDISK_PERF_ENABLE |
|
672 handle.iOrigFullFilePath = HBufC::NewL(0); |
|
673 handle.iTargetFullFilePath = HBufC::NewL(0); |
|
674 handle.iDummyFilePath.Zero(); |
|
675 handle.iUseRAMdb = EFalse; |
|
676 #endif //__RAMDISK_PERF_ENABLE |
|
677 |
|
678 TInt index = iDatabaseHandles.Count(); |
|
679 iDatabaseHandles.AppendL(handle); |
|
680 |
|
681 TVolumeInfo vol; |
|
682 if (iFs.Volume(vol, drive) == KErrNone) |
|
683 { |
|
684 CreateDatabaseL(drive); |
|
685 AttachDatabaseL( index ); |
|
686 } |
|
687 } |
|
688 } |
|
689 |
|
690 iInitialized = ETrue; |
|
691 } |
|
692 |
|
693 // ---------------------------------------------------------------------------- |
|
694 // Opens a specified database. |
|
695 // ---------------------------------------------------------------------------- |
|
696 // |
|
697 EXPORT_C void CMPXDbManager::OpenDatabaseL( |
|
698 TInt aDrive) |
|
699 { |
|
700 MPX_FUNC("CMPXDbManager::OpenDatabaseL"); |
|
701 |
|
702 if (iInitialized == EFalse) |
|
703 { |
|
704 User::Leave(KErrNotReady); |
|
705 } |
|
706 |
|
707 TDriveUnit drive(aDrive); |
|
708 TDriveUnit cdrive(KRootDrive()); |
|
709 TBool found(EFalse); |
|
710 |
|
711 if ((drive != cdrive) && !IsRemoteDrive(static_cast<TDriveNumber>(aDrive))) |
|
712 { |
|
713 TInt count(iDatabaseHandles.Count()); |
|
714 for (TInt i = 0; i < count; ++i) |
|
715 { |
|
716 if (iDatabaseHandles[i].iDrive == aDrive) |
|
717 { |
|
718 MPX_DEBUG2("CMPXDbManager::OpenDatabaseL found %d", aDrive); |
|
719 TInt transactionCount = iTransactionCount; |
|
720 if (iTransactionCount > 0) |
|
721 { |
|
722 iTransactionCount = 0; |
|
723 DoCommitL(); |
|
724 } |
|
725 OpenDatabaseAtIndexL( i ); |
|
726 if (transactionCount > 0) |
|
727 { |
|
728 DoBeginL(); |
|
729 iTransactionCount = transactionCount; |
|
730 } |
|
731 found = ETrue; |
|
732 break; |
|
733 } |
|
734 } |
|
735 } |
|
736 if (!found) |
|
737 { |
|
738 MPX_DEBUG1("CMPXDbManager::OpenDatabaseL not found"); |
|
739 User::Leave(KErrArgument); |
|
740 } |
|
741 |
|
742 // Close all prepared statements if a db is opened |
|
743 // |
|
744 ResetPreparedQueries(); |
|
745 } |
|
746 |
|
747 void CMPXDbManager::OpenDatabaseAtIndexL( TInt aIndex ) |
|
748 { |
|
749 DatabaseHandle & database = iDatabaseHandles[aIndex]; |
|
750 if (!database.iOpen) |
|
751 { |
|
752 MPX_DEBUG1("CMPXDbManager::OpenDatabaseAtIndexL not open"); |
|
753 // make sure the database is created |
|
754 CreateDatabaseL( TDriveUnit(database.iDrive) ); |
|
755 AttachDatabaseL( aIndex ); |
|
756 } |
|
757 } |
|
758 |
|
759 // ---------------------------------------------------------------------------- |
|
760 // Closes a specified database. |
|
761 // ---------------------------------------------------------------------------- |
|
762 // |
|
763 EXPORT_C void CMPXDbManager::CloseDatabaseL( |
|
764 TInt aDrive) |
|
765 { |
|
766 MPX_FUNC("CMPXDbManager::CloseDatabaseL"); |
|
767 |
|
768 if (iInitialized == EFalse) |
|
769 { |
|
770 User::Leave(KErrNotReady); |
|
771 } |
|
772 |
|
773 TDriveUnit drive(aDrive); |
|
774 TDriveUnit cdrive(KRootDrive()); |
|
775 TBool found(EFalse); |
|
776 |
|
777 if ((drive != cdrive) && !IsRemoteDrive(static_cast<TDriveNumber>(aDrive))) |
|
778 { |
|
779 TInt count(iDatabaseHandles.Count()); |
|
780 for (TInt i = 0; i < count; ++i) |
|
781 { |
|
782 if (iDatabaseHandles[i].iDrive == aDrive) |
|
783 { |
|
784 TBool inTransaction = InTransaction(); |
|
785 TInt transactionCount = iTransactionCount; |
|
786 iTransactionCount = 0; |
|
787 |
|
788 if (inTransaction) //if the transaction is ongoing, try committing |
|
789 { |
|
790 //if transaction committing fails, try roll-back |
|
791 TInt error = iDatabase.Exec( KCommitTransaction ); |
|
792 if ( error != KErrNone ) |
|
793 { |
|
794 //The error is ignored since we can't do nothing about it |
|
795 iDatabase.Exec( KRollbackTransaction ); |
|
796 } |
|
797 } |
|
798 |
|
799 #ifdef __RAMDISK_PERF_ENABLE |
|
800 if ( iRAMDiskPerfEnabled && iDatabaseHandles[i].iUseRAMdb ) |
|
801 { |
|
802 MPX_DEBUG2("CMPXDbManager::CloseDatabaseL found %d at RAM", aDrive); |
|
803 TRAPD( err, CloseDatabaseAtIndexL( i ) ); |
|
804 if ( err != KErrNone ) |
|
805 { |
|
806 // Can't close db on RAM drive, so cleanup. |
|
807 MPX_DEBUG2("CMPXDbManager::CloseDatabaseL CloseDatabaseAtIndexL fail: error = %d", err); |
|
808 // Delete dummy file |
|
809 RemoveDummyFile(i); |
|
810 iDatabaseHandles[i].iUseRAMdb = EFalse; |
|
811 // Delete database on RAM drive. |
|
812 User::LeaveIfError( BaflUtils::DeleteFile(iFs, *iDatabaseHandles[i].iTargetFullFilePath) ); |
|
813 } |
|
814 else |
|
815 { |
|
816 DoCopyDBFromRam(i); |
|
817 } |
|
818 iDatabaseHandles[i].iUseRAMdb = EFalse; |
|
819 } |
|
820 else |
|
821 #endif |
|
822 { |
|
823 MPX_DEBUG2("CMPXDbManager::CloseDatabaseL found %d", aDrive); |
|
824 CloseDatabaseAtIndexL( i ); |
|
825 } |
|
826 |
|
827 //Let MTP handle the transcation if there is any |
|
828 if ( inTransaction ) |
|
829 { |
|
830 DoBeginL(); |
|
831 iTransactionCount = transactionCount; |
|
832 } |
|
833 |
|
834 found = ETrue; |
|
835 break; |
|
836 } |
|
837 } |
|
838 } |
|
839 if (!found) |
|
840 { |
|
841 MPX_DEBUG1("CMPXDbManager::CloseDatabaseL not found"); |
|
842 User::Leave(KErrArgument); |
|
843 } |
|
844 |
|
845 } |
|
846 |
|
847 void CMPXDbManager::CloseDatabaseAtIndexL( TInt aIndex ) |
|
848 { |
|
849 // Close all prepared statements if a db is closed |
|
850 // |
|
851 ResetPreparedQueries(); |
|
852 |
|
853 if (iDatabaseHandles[aIndex].iOpen) |
|
854 { |
|
855 MPX_DEBUG1("CMPXDbManager::CloseDatabaseAtIndexL found open"); |
|
856 DetachDatabaseL( aIndex ); |
|
857 } |
|
858 } |
|
859 |
|
860 // ---------------------------------------------------------------------------- |
|
861 // Closes all databases. |
|
862 // ---------------------------------------------------------------------------- |
|
863 // |
|
864 EXPORT_C void CMPXDbManager::CloseAllDatabases() |
|
865 { |
|
866 MPX_FUNC("CMPXDbManager::CloseAllDatabases"); |
|
867 |
|
868 // Close all prepared statements if a db is closed |
|
869 // |
|
870 ResetPreparedQueries(); |
|
871 |
|
872 TInt count(iDatabaseHandles.Count()); |
|
873 for (TInt i = 0; i < count; ++i) |
|
874 { |
|
875 delete iDatabaseHandles[i].iAliasname; |
|
876 iDatabaseHandles[i].iAliasname = 0; |
|
877 #ifdef __RAMDISK_PERF_ENABLE |
|
878 RemoveDummyFile(i); |
|
879 delete iDatabaseHandles[i].iOrigFullFilePath; |
|
880 iDatabaseHandles[i].iOrigFullFilePath = 0; |
|
881 delete iDatabaseHandles[i].iTargetFullFilePath; |
|
882 iDatabaseHandles[i].iTargetFullFilePath = 0; |
|
883 #endif //__RAMDISK_PERF_ENABLE |
|
884 } |
|
885 |
|
886 iDatabaseHandles.Reset(); |
|
887 iDatabase.Close(); |
|
888 iInitialized = EFalse; |
|
889 } |
|
890 |
|
891 // ---------------------------------------------------------------------------- |
|
892 // Open all databases. |
|
893 // ---------------------------------------------------------------------------- |
|
894 // |
|
895 EXPORT_C void CMPXDbManager::OpenAllDatabasesL() |
|
896 { |
|
897 MPX_FUNC("CMPXDbManager::OpenAllDatabasesL"); |
|
898 |
|
899 if (!iInitialized) |
|
900 { |
|
901 OpenRootDatabaseL(); |
|
902 } |
|
903 |
|
904 TInt count(iDatabaseHandles.Count()); |
|
905 for (TInt i = 0; i < count; ++i) |
|
906 { |
|
907 TVolumeInfo vol; |
|
908 if (iFs.Volume(vol, iDatabaseHandles[i].iDrive) == KErrNone) |
|
909 { |
|
910 AttachDatabaseL( i ); |
|
911 } |
|
912 } |
|
913 iInitialized = ETrue; |
|
914 |
|
915 // Close all prepared statements if a db is closed |
|
916 // |
|
917 ResetPreparedQueries(); |
|
918 } |
|
919 |
|
920 // ---------------------------------------------------------------------------- |
|
921 // Checks if the database on a specified drive is open. |
|
922 // ---------------------------------------------------------------------------- |
|
923 // |
|
924 EXPORT_C TBool CMPXDbManager::IsOpen( |
|
925 TInt aDrive) const |
|
926 { |
|
927 MPX_FUNC("CMPXDbManager::IsOpen"); |
|
928 |
|
929 TDriveUnit drive(aDrive); |
|
930 TDriveUnit cdrive(KRootDrive()); |
|
931 |
|
932 if (!iInitialized) |
|
933 { |
|
934 return EFalse; |
|
935 } |
|
936 else if (drive == cdrive) |
|
937 { |
|
938 return ETrue; |
|
939 } |
|
940 else |
|
941 { |
|
942 TInt count(iDatabaseHandles.Count()); |
|
943 for (TInt i = 0; i < count; ++i) |
|
944 { |
|
945 if (iDatabaseHandles[i].iDrive == aDrive) |
|
946 { |
|
947 return iDatabaseHandles[i].iOpen; |
|
948 } |
|
949 } |
|
950 } |
|
951 |
|
952 return EFalse; |
|
953 } |
|
954 |
|
955 // ---------------------------------------------------------------------------- |
|
956 // Returns the number of currently open databases. |
|
957 // ---------------------------------------------------------------------------- |
|
958 // |
|
959 EXPORT_C TInt CMPXDbManager::DatabaseCount() const |
|
960 { |
|
961 MPX_FUNC("CMPXDbManager::DatabaseCount"); |
|
962 |
|
963 TInt openCount(0); |
|
964 if (iInitialized) |
|
965 { |
|
966 ++openCount; |
|
967 |
|
968 TInt count(iDatabaseHandles.Count()); |
|
969 for (TInt i = 0; i < count; ++i) |
|
970 { |
|
971 if (iDatabaseHandles[i].iOpen) |
|
972 { |
|
973 ++openCount; |
|
974 } |
|
975 } |
|
976 } |
|
977 |
|
978 return openCount; |
|
979 } |
|
980 |
|
981 // ---------------------------------------------------------------------------- |
|
982 // Returns the drive corresponding to a given index. |
|
983 // ---------------------------------------------------------------------------- |
|
984 // |
|
985 EXPORT_C TInt CMPXDbManager::DbDrive( |
|
986 TInt aIndex) const |
|
987 { |
|
988 MPX_FUNC("CMPXDbManager::DbDrive"); |
|
989 |
|
990 ASSERT((aIndex >= 0) || (aIndex < iDatabaseHandles.Count())); |
|
991 return iDatabaseHandles[aIndex].iDrive; |
|
992 } |
|
993 |
|
994 // ---------------------------------------------------------------------------- |
|
995 // Recreate a specified database. |
|
996 // ---------------------------------------------------------------------------- |
|
997 // |
|
998 EXPORT_C void CMPXDbManager::RecreateDatabaseL( |
|
999 TInt aDrive) |
|
1000 { |
|
1001 MPX_FUNC("CMPXDbManager::RecreateDatabaseL"); |
|
1002 |
|
1003 if (iInitialized == EFalse) |
|
1004 { |
|
1005 User::Leave(KErrNotReady); |
|
1006 } |
|
1007 |
|
1008 TInt index = KErrNotFound; |
|
1009 |
|
1010 if (aDrive == EDriveC) |
|
1011 { |
|
1012 index = iDatabaseHandles.Count(); |
|
1013 } |
|
1014 else |
|
1015 { |
|
1016 TInt count(iDatabaseHandles.Count()); |
|
1017 for (TInt i = 0; i < count; ++i) |
|
1018 { |
|
1019 if ((iDatabaseHandles[i].iDrive == aDrive) && (iDatabaseHandles[i].iOpen)) |
|
1020 { |
|
1021 index = i; |
|
1022 break; |
|
1023 } |
|
1024 } |
|
1025 } |
|
1026 if ( index >= 0 ) |
|
1027 { |
|
1028 HBufC * filename = CreateFilenameL(aDrive); |
|
1029 CleanupStack::PushL(filename); |
|
1030 |
|
1031 TRAPD(err, DoRecreateDatabaseL(filename)); |
|
1032 if(err < 0) |
|
1033 { |
|
1034 TDriveUnit drive_unit(aDrive); |
|
1035 |
|
1036 if(aDrive == EDriveC) |
|
1037 { |
|
1038 iDatabase.Close(); |
|
1039 iInitialized = EFalse; |
|
1040 |
|
1041 RSqlDatabase::Delete(*filename); |
|
1042 CreateDatabaseL(drive_unit); |
|
1043 |
|
1044 User::LeaveIfError(iDatabase.Open(*filename)); |
|
1045 iInitialized = ETrue; |
|
1046 } |
|
1047 else |
|
1048 { |
|
1049 DetachDatabaseL( index ); |
|
1050 |
|
1051 RSqlDatabase::Delete(*filename); |
|
1052 CreateDatabaseL(drive_unit); |
|
1053 |
|
1054 AttachDatabaseL( index ); |
|
1055 } |
|
1056 } |
|
1057 |
|
1058 CleanupStack::PopAndDestroy(filename); |
|
1059 } |
|
1060 else |
|
1061 { |
|
1062 User::Leave(KErrNotFound); |
|
1063 } |
|
1064 } |
|
1065 |
|
1066 // ---------------------------------------------------------------------------- |
|
1067 // Recreate all databases. |
|
1068 // ---------------------------------------------------------------------------- |
|
1069 // |
|
1070 EXPORT_C void CMPXDbManager::RecreateAllDatabasesL() |
|
1071 { |
|
1072 MPX_FUNC("CMPXDbManager::RecreateAllDatabasesL"); |
|
1073 |
|
1074 if (iInitialized == EFalse) |
|
1075 { |
|
1076 User::Leave(KErrNotReady); |
|
1077 } |
|
1078 |
|
1079 // Recreate on drive C |
|
1080 RecreateDatabaseL(EDriveC); |
|
1081 |
|
1082 // Recreate all attached drives |
|
1083 TInt count(iDatabaseHandles.Count()); |
|
1084 for (TInt i = 0; i < count; ++i) |
|
1085 { |
|
1086 if (iDatabaseHandles[i].iOpen) |
|
1087 { |
|
1088 RecreateDatabaseL(iDatabaseHandles[i].iDrive); |
|
1089 } |
|
1090 } |
|
1091 } |
|
1092 |
|
1093 // ---------------------------------------------------------------------------- |
|
1094 // Returns current DB version |
|
1095 // ---------------------------------------------------------------------------- |
|
1096 // |
|
1097 EXPORT_C TVersion CMPXDbManager::Version() const |
|
1098 { |
|
1099 MPX_FUNC("CMPXDbManager::Version"); |
|
1100 return TVersion(KMPXDbVersion[0], KMPXDbVersion[1], KMPXDbVersion[2]); |
|
1101 } |
|
1102 |
|
1103 // ---------------------------------------------------------------------------- |
|
1104 // Registes a table with the database |
|
1105 // ---------------------------------------------------------------------------- |
|
1106 // |
|
1107 EXPORT_C void CMPXDbManager::RegisterTableL( |
|
1108 MMPXTable& aTable) |
|
1109 { |
|
1110 MPX_FUNC("CMPXDbManager::RegisterTableL"); |
|
1111 iTables.AppendL(&aTable); |
|
1112 } |
|
1113 |
|
1114 // ---------------------------------------------------------------------------- |
|
1115 // Executes a select query with variable number of parameters |
|
1116 // The query is executed on all available databases with a format like: |
|
1117 // |
|
1118 // <query on database1> UNION ALL <query on database2> ... |
|
1119 // |
|
1120 // The query string passed in by the caller must have the ":dbname" prefix for all |
|
1121 // the tables in the FROM clause. This will be replaced with the right alias for |
|
1122 // attached databases or with no alias for the C database. |
|
1123 // |
|
1124 // In case the original query contains an ORDER BY clause, this will be extracted |
|
1125 // and added at the end of the union query. |
|
1126 // |
|
1127 // Note: Running the union query seems to be similar in speed even if one of the |
|
1128 // databases is empty and therefore no optimization was done for this case. |
|
1129 // ---------------------------------------------------------------------------- |
|
1130 // |
|
1131 EXPORT_C RSqlStatement CMPXDbManager::ExecuteSelectQueryL( |
|
1132 TRefByValue<const TDesC> aFmt, |
|
1133 ...) |
|
1134 { |
|
1135 MPX_FUNC("CMPXDatabase::ExecuteSelectQueryL"); |
|
1136 |
|
1137 VA_LIST list; |
|
1138 VA_START(list, aFmt); |
|
1139 |
|
1140 // Will reallocate |
|
1141 HBufC* selectBuf = FormatQueryLC(aFmt, list); |
|
1142 RSqlStatement statement = ExecuteSelectQueryOnAllDrivesL(selectBuf->Des()); |
|
1143 CleanupStack::PopAndDestroy(selectBuf); |
|
1144 |
|
1145 VA_END(list); |
|
1146 |
|
1147 return statement; |
|
1148 } |
|
1149 |
|
1150 // ---------------------------------------------------------------------------- |
|
1151 // Executes a select query against a specified drive |
|
1152 // ---------------------------------------------------------------------------- |
|
1153 // |
|
1154 EXPORT_C RSqlStatement CMPXDbManager::ExecuteSelectQueryL( |
|
1155 TInt aDrive, |
|
1156 TRefByValue<const TDesC> aFmt, |
|
1157 ...) |
|
1158 { |
|
1159 MPX_FUNC("CMPXDatabase::ExecuteSelectQueryL"); |
|
1160 |
|
1161 VA_LIST list; |
|
1162 VA_START(list, aFmt); |
|
1163 |
|
1164 // Will reallocate |
|
1165 HBufC* selectBuf = FormatQueryLC(aFmt, list); |
|
1166 RSqlStatement statement = ExecuteSelectQueryOnDriveL(aDrive, selectBuf->Des()); |
|
1167 CleanupStack::PopAndDestroy(selectBuf); |
|
1168 |
|
1169 VA_END(list); |
|
1170 |
|
1171 return statement; |
|
1172 } |
|
1173 |
|
1174 // ---------------------------------------------------------------------------- |
|
1175 // CMPXDbManager::ExecuteSelectQueryL |
|
1176 // ---------------------------------------------------------------------------- |
|
1177 // |
|
1178 EXPORT_C RSqlStatement& CMPXDbManager::ExecuteSelectQueryL( |
|
1179 TUint aStatementId, |
|
1180 TInt aFirstValue, |
|
1181 TInt aSecondValue, |
|
1182 TRefByValue<const TDesC> aFmt, |
|
1183 ...) |
|
1184 { |
|
1185 MPX_FUNC("CMPXDatabase::ExecuteOffsetSelectQueryL"); |
|
1186 |
|
1187 // Prepare the Query first |
|
1188 VA_LIST list; |
|
1189 VA_START(list, aFmt); |
|
1190 RSqlStatement& statement = PrepareQueryL( aStatementId, aFmt, list ); |
|
1191 VA_END(list); |
|
1192 |
|
1193 // Bind the Limit and Offset variables |
|
1194 User::LeaveIfError(statement.BindInt(0, aFirstValue)); |
|
1195 User::LeaveIfError(statement.BindInt(1, aSecondValue)); |
|
1196 |
|
1197 return statement; |
|
1198 } |
|
1199 |
|
1200 // ---------------------------------------------------------------------------- |
|
1201 // CMPXDbManager::ExecuteSelectQueryL |
|
1202 // ---------------------------------------------------------------------------- |
|
1203 // |
|
1204 EXPORT_C RSqlStatement& CMPXDbManager::ExecuteSelectQueryL( TUint aStatementId, |
|
1205 const TDesC& aFirstValue, |
|
1206 TInt aSecondValue, |
|
1207 TRefByValue<const TDesC> aFmt, ...) |
|
1208 { |
|
1209 MPX_FUNC("CMPXDbManager::ExecuteMediaAscQueryL"); |
|
1210 |
|
1211 // Prepare the Query first |
|
1212 VA_LIST list; |
|
1213 VA_START(list, aFmt); |
|
1214 RSqlStatement& statement = PrepareQueryL( aStatementId, aFmt, list ); |
|
1215 VA_END(list); |
|
1216 |
|
1217 // bind the title and limit values |
|
1218 User::LeaveIfError(statement.BindText(0, aFirstValue)); |
|
1219 User::LeaveIfError(statement.BindInt(1, aSecondValue)); |
|
1220 |
|
1221 return statement; |
|
1222 } |
|
1223 |
|
1224 // ---------------------------------------------------------------------------- |
|
1225 // Executes a query that does not return a record set (INSERT, UPDATE, DELETE, |
|
1226 // CREATE, DROP, etc). |
|
1227 // |
|
1228 // If a valid drive is specified then the query is only executed only on |
|
1229 // that drive. If KDbManagerAllDrives is specified then the query is executed |
|
1230 // separately on each available drive. |
|
1231 // ---------------------------------------------------------------------------- |
|
1232 // |
|
1233 EXPORT_C void CMPXDbManager::ExecuteQueryL( |
|
1234 TInt aDrive, |
|
1235 TRefByValue<const TDesC> aFmt, |
|
1236 ...) |
|
1237 { |
|
1238 MPX_FUNC("CMPXDatabase::ExecuteQueryL"); |
|
1239 |
|
1240 // make sure there is enough space on all drives affected |
|
1241 CheckDiskSpaceL(aDrive); |
|
1242 |
|
1243 VA_LIST list; |
|
1244 VA_START(list, aFmt); |
|
1245 |
|
1246 HBufC* selectBuf = FormatQueryLC(aFmt, list); |
|
1247 TPtr selectBufPtr = selectBuf->Des(); |
|
1248 TInt dbCount(iDatabaseHandles.Count()); |
|
1249 |
|
1250 // a specified drive or all drives |
|
1251 TInt loopCount = (aDrive == KDbManagerAllDrives) ? (dbCount + 1) : 1; |
|
1252 TBool queryExecuted(EFalse); // flag to check if the query was executed at least once |
|
1253 for (TInt j = 0; j < loopCount; ++j) |
|
1254 { |
|
1255 HBufC* query = HBufC::NewLC(selectBufPtr.Length() + KBufIncrement); |
|
1256 TPtr queryPtr = query->Des(); |
|
1257 queryPtr.Copy(selectBufPtr); |
|
1258 if (aDrive == EDriveC) // if C drive only |
|
1259 { |
|
1260 RemoveDriveAlias(queryPtr); |
|
1261 } |
|
1262 else // all drives or a particular drive other than C drive |
|
1263 { |
|
1264 if (aDrive == 0) // all drives |
|
1265 { |
|
1266 if (j == dbCount) // C drive |
|
1267 { |
|
1268 RemoveDriveAlias(queryPtr); |
|
1269 } |
|
1270 else //all other drives, except C drive |
|
1271 { |
|
1272 if (iDatabaseHandles[j].iOpen) |
|
1273 { |
|
1274 ReplaceDriveAlias(queryPtr, *(iDatabaseHandles[j].iAliasname)); |
|
1275 } |
|
1276 } |
|
1277 } |
|
1278 else //a particular drive, other than C drive |
|
1279 { |
|
1280 for (TInt i = 0; i < dbCount; ++i) |
|
1281 { |
|
1282 if (iDatabaseHandles[i].iOpen && iDatabaseHandles[i].iDrive == aDrive) |
|
1283 { |
|
1284 ReplaceDriveAlias(queryPtr, *(iDatabaseHandles[i].iAliasname)); |
|
1285 break; |
|
1286 } |
|
1287 } |
|
1288 } |
|
1289 } |
|
1290 TInt dbnamePos = queryPtr.Find(KDBNameToken);// check if the query was created correctly |
|
1291 if (dbnamePos == KErrNotFound) |
|
1292 { |
|
1293 // log the query |
|
1294 TPtrC ptr(query->Left(KMaxLogQuery)); |
|
1295 MPX_DEBUG2("Query: %S", &ptr); |
|
1296 |
|
1297 User::LeaveIfError(ExecuteSqlStatement(iDatabase, queryPtr)); |
|
1298 queryExecuted = ETrue; |
|
1299 } |
|
1300 CleanupStack::PopAndDestroy(query); |
|
1301 } //for (TInt j = 0; j < loopCount; ++j) |
|
1302 CleanupStack::PopAndDestroy(selectBuf); |
|
1303 VA_END(list); |
|
1304 if (!queryExecuted && aDrive != 0) |
|
1305 { |
|
1306 // the requested drive(s) is not open |
|
1307 User::Leave(KErrNotFound); |
|
1308 } |
|
1309 } |
|
1310 |
|
1311 // ---------------------------------------------------------------------------- |
|
1312 // CMPXDbManager::FormatQueryLC |
|
1313 // ---------------------------------------------------------------------------- |
|
1314 // |
|
1315 HBufC* CMPXDbManager::FormatQueryLC( |
|
1316 TRefByValue<const TDesC> aFmt, |
|
1317 VA_LIST aList) |
|
1318 { |
|
1319 MPX_FUNC("CMPXDatabase::FormatQueryLC"); |
|
1320 |
|
1321 TOverflowHandle overflow; |
|
1322 |
|
1323 HBufC* selectBuf = HBufC::NewLC(TDesC(aFmt).Length());//will reallocate |
|
1324 selectBuf->Des().AppendFormatList(aFmt, aList, &overflow); |
|
1325 while (overflow.GetOverflowFlag()) |
|
1326 { |
|
1327 TInt len = selectBuf->Des().MaxLength() + KBufIncrement; |
|
1328 CleanupStack::PopAndDestroy(selectBuf); |
|
1329 selectBuf = HBufC::NewLC(len); |
|
1330 selectBuf->Des().AppendFormatList(aFmt, aList, &overflow); |
|
1331 } |
|
1332 |
|
1333 return selectBuf; |
|
1334 } |
|
1335 |
|
1336 // ---------------------------------------------------------------------------- |
|
1337 // Executes a select query against a specified drive |
|
1338 // ---------------------------------------------------------------------------- |
|
1339 // |
|
1340 EXPORT_C RSqlStatement CMPXDbManager::ExecuteSelectQueryOnAllDrivesL( |
|
1341 TInt aDrive, |
|
1342 TRefByValue<const TDesC> aFmt, |
|
1343 ...) |
|
1344 { |
|
1345 MPX_FUNC("CMPXDatabase::ExecuteSelectQueryL"); |
|
1346 |
|
1347 VA_LIST list; |
|
1348 VA_START(list, aFmt); |
|
1349 |
|
1350 // Will reallocate |
|
1351 HBufC* selectBuf = FormatQueryLC(aFmt, list); |
|
1352 RSqlStatement statement = ExecuteSelectQueryOnAllDrivesL(aDrive, selectBuf->Des()); |
|
1353 CleanupStack::PopAndDestroy(selectBuf); |
|
1354 |
|
1355 VA_END(list); |
|
1356 |
|
1357 return statement; |
|
1358 } |
|
1359 |
|
1360 // ---------------------------------------------------------------------------- |
|
1361 // CMPXDbManager::ExecuteSelectQueryOnAllDrivesL |
|
1362 // ---------------------------------------------------------------------------- |
|
1363 // |
|
1364 RSqlStatement CMPXDbManager::ExecuteSelectQueryOnAllDrivesL( |
|
1365 TPtr aQuery) |
|
1366 { |
|
1367 MPX_FUNC("CMPXDatabase::ExecuteSelectQueryOnAllDrivesL"); |
|
1368 |
|
1369 TInt dbCount = iDatabaseHandles.Count(); |
|
1370 HBufC* query = HBufC::NewLC(aQuery.Length() * (dbCount + 1) + |
|
1371 KUnionAllToken().Length() * dbCount); |
|
1372 TPtr queryPtr = query->Des(); |
|
1373 HBufC* selectOutBuf = NULL; |
|
1374 TInt enclosed = aQuery.Mid(1, aQuery.Length() - 1).Find(KSelectToken); |
|
1375 if (enclosed != KErrNotFound) |
|
1376 { |
|
1377 enclosed++;//to compensate the indent |
|
1378 selectOutBuf = HBufC::NewLC(aQuery.Length() * (dbCount + 1) + |
|
1379 KUnionAllToken().Length() * dbCount); |
|
1380 selectOutBuf->Des().Copy(aQuery.Left(enclosed)); |
|
1381 selectOutBuf->Des().Append(aQuery.Right(1));//the closing bracket |
|
1382 aQuery.Delete(0, enclosed); |
|
1383 aQuery.Delete(aQuery.Length() - 1, 1); |
|
1384 } |
|
1385 |
|
1386 HBufC* orderBuf = NULL; |
|
1387 TInt orderPos = aQuery.Find(KOrderByToken); |
|
1388 if (orderPos != KErrNotFound) |
|
1389 { |
|
1390 orderBuf = aQuery.Right(aQuery.Length() - orderPos).AllocL(); |
|
1391 aQuery.Delete(orderPos, aQuery.Length() - orderPos); |
|
1392 } |
|
1393 queryPtr.Append(aQuery);// for cdrive |
|
1394 RemoveDriveAlias(queryPtr); |
|
1395 for (TInt i = 0; i < dbCount; ++i)//for other drives |
|
1396 { |
|
1397 if (iDatabaseHandles[i].iOpen) |
|
1398 { |
|
1399 queryPtr.Append(KUnionAllToken); |
|
1400 queryPtr.Append(aQuery); |
|
1401 ReplaceDriveAlias(queryPtr, *(iDatabaseHandles[i].iAliasname)); |
|
1402 } |
|
1403 } |
|
1404 if (orderBuf) |
|
1405 { |
|
1406 queryPtr.Append(orderBuf->Des()); |
|
1407 } |
|
1408 delete orderBuf; |
|
1409 if (enclosed != KErrNotFound) |
|
1410 { |
|
1411 selectOutBuf->Des().Insert(enclosed, query->Des()); |
|
1412 queryPtr.Copy(selectOutBuf->Des()); |
|
1413 CleanupStack::PopAndDestroy(selectOutBuf); |
|
1414 } |
|
1415 |
|
1416 // Log the query string before execution |
|
1417 TPtrC ptr(query->Left(KMaxLogQuery)); |
|
1418 MPX_DEBUG2("Query: %S", &ptr); |
|
1419 |
|
1420 // Return a temporary statement and not a member variable. |
|
1421 // This ensures that a copy is done and a second embedded query can be |
|
1422 // executed while the first result set is processed. |
|
1423 RSqlStatement statement; |
|
1424 User::LeaveIfError(statement.Prepare(iDatabase, queryPtr)); |
|
1425 CleanupStack::PopAndDestroy(query); |
|
1426 |
|
1427 return statement; |
|
1428 } |
|
1429 |
|
1430 // ---------------------------------------------------------------------------- |
|
1431 // CMPXDbManager::ExecuteSelectQueryOnAllDrivesL |
|
1432 // ---------------------------------------------------------------------------- |
|
1433 // |
|
1434 RSqlStatement CMPXDbManager::ExecuteSelectQueryOnAllDrivesL( TInt aDrive, |
|
1435 TPtr aQuery) |
|
1436 { |
|
1437 MPX_FUNC("CMPXDatabase::ExecuteSelectQueryOnAllDrivesL"); |
|
1438 |
|
1439 TInt dbCount = iDatabaseHandles.Count(); |
|
1440 HBufC* query = HBufC::NewLC(aQuery.Length() * (dbCount + 1) + |
|
1441 KUnionAllToken().Length() * dbCount); |
|
1442 TPtr queryPtr = query->Des(); |
|
1443 HBufC* selectOutBuf = NULL; |
|
1444 TInt enclosed = aQuery.Mid(1, aQuery.Length() - 1).Find(KSelectToken); |
|
1445 if (enclosed != KErrNotFound) |
|
1446 { |
|
1447 enclosed++;//to compensate the indent |
|
1448 selectOutBuf = HBufC::NewLC(aQuery.Length() * (dbCount + 1) + |
|
1449 KUnionAllToken().Length() * dbCount); |
|
1450 selectOutBuf->Des().Copy(aQuery.Left(enclosed)); |
|
1451 selectOutBuf->Des().Append(aQuery.Right(1));//the closing bracket |
|
1452 aQuery.Delete(0, enclosed); |
|
1453 aQuery.Delete(aQuery.Length() - 1, 1); |
|
1454 } |
|
1455 |
|
1456 HBufC* orderBuf = NULL; |
|
1457 TInt orderPos = aQuery.Find(KOrderByToken); |
|
1458 if (orderPos != KErrNotFound) |
|
1459 { |
|
1460 orderBuf = aQuery.Right(aQuery.Length() - orderPos).AllocL(); |
|
1461 aQuery.Delete(orderPos, aQuery.Length() - orderPos); |
|
1462 } |
|
1463 |
|
1464 //remove KPlDBNameToken |
|
1465 if ( aDrive == EDriveC )//if playlist on c drive |
|
1466 { |
|
1467 RemoveDriveAlias(aQuery,KPlDBNameToken); |
|
1468 } |
|
1469 else |
|
1470 {//for other drives |
|
1471 for (TInt i = 0; i < dbCount; ++i) |
|
1472 { |
|
1473 if (iDatabaseHandles[i].iOpen && (iDatabaseHandles[i].iDrive == aDrive)) |
|
1474 { |
|
1475 ReplaceDriveAlias(aQuery, *(iDatabaseHandles[i].iAliasname), |
|
1476 KPlDBNameToken); |
|
1477 break; |
|
1478 } |
|
1479 } |
|
1480 } |
|
1481 |
|
1482 queryPtr.Append(aQuery);// for cdrive |
|
1483 RemoveDriveAlias(queryPtr); |
|
1484 for (TInt i = 0; i < dbCount; ++i)//for other drives |
|
1485 { |
|
1486 if (iDatabaseHandles[i].iOpen) |
|
1487 { |
|
1488 queryPtr.Append(KUnionAllToken); |
|
1489 queryPtr.Append(aQuery); |
|
1490 ReplaceDriveAlias(queryPtr, *(iDatabaseHandles[i].iAliasname)); |
|
1491 } |
|
1492 } |
|
1493 |
|
1494 if (orderBuf) |
|
1495 { |
|
1496 queryPtr.Append(orderBuf->Des()); |
|
1497 } |
|
1498 delete orderBuf; |
|
1499 if (enclosed != KErrNotFound) |
|
1500 { |
|
1501 selectOutBuf->Des().Insert(enclosed, query->Des()); |
|
1502 queryPtr.Copy(selectOutBuf->Des()); |
|
1503 CleanupStack::PopAndDestroy(selectOutBuf); |
|
1504 } |
|
1505 |
|
1506 // Log the query string before execution |
|
1507 TPtrC ptr(query->Left(KMaxLogQuery)); |
|
1508 MPX_DEBUG2("Query: %S", &ptr); |
|
1509 |
|
1510 // Return a temporary statement and not a member variable. |
|
1511 // This ensures that a copy is done and a second embedded query can be |
|
1512 // executed while the first result set is processed. |
|
1513 RSqlStatement statement; |
|
1514 TInt err(statement.Prepare(iDatabase, queryPtr)); |
|
1515 User::LeaveIfError(err); |
|
1516 CleanupStack::PopAndDestroy(query); |
|
1517 |
|
1518 return statement; |
|
1519 } |
|
1520 |
|
1521 // ---------------------------------------------------------------------------- |
|
1522 // CMPXDbManager::ExecuteSelectQueryOnDriveLryLC |
|
1523 // ---------------------------------------------------------------------------- |
|
1524 // |
|
1525 RSqlStatement CMPXDbManager::ExecuteSelectQueryOnDriveL( |
|
1526 TInt aDrive, |
|
1527 TPtr aQuery) |
|
1528 { |
|
1529 MPX_FUNC("CMPXDatabase::ExecuteSelectQueryOnDriveL"); |
|
1530 |
|
1531 RSqlStatement statement; |
|
1532 if (KDbManagerAllDrives == aDrive) |
|
1533 { |
|
1534 statement = ExecuteSelectQueryOnAllDrivesL(aQuery); |
|
1535 } |
|
1536 else |
|
1537 { |
|
1538 TInt dbCount(iDatabaseHandles.Count()); |
|
1539 |
|
1540 // flag to check if the query was executed at least once |
|
1541 TBool queryExecuted = EFalse; |
|
1542 |
|
1543 HBufC* query = HBufC::NewLC(aQuery.Length() + KBufIncrement); |
|
1544 TPtr queryPtr = query->Des(); |
|
1545 queryPtr.Copy(aQuery); |
|
1546 if (aDrive == EDriveC) //if C drive |
|
1547 { |
|
1548 RemoveDriveAlias(queryPtr); |
|
1549 } |
|
1550 else // drive other than C drive |
|
1551 { |
|
1552 for (TInt i = 0; i < dbCount; ++i) |
|
1553 { |
|
1554 if (iDatabaseHandles[i].iOpen && (iDatabaseHandles[i].iDrive == aDrive)) |
|
1555 { |
|
1556 ReplaceDriveAlias(queryPtr, *(iDatabaseHandles[i].iAliasname)); |
|
1557 break; |
|
1558 } |
|
1559 } |
|
1560 } |
|
1561 |
|
1562 TInt dbnamePos = queryPtr.Find(KDBNameToken);// check if the query was created correctly |
|
1563 if (dbnamePos == KErrNotFound) |
|
1564 { |
|
1565 // Log the query string before execution |
|
1566 TPtrC ptr(query->Left(KMaxLogQuery)); |
|
1567 MPX_DEBUG2("Query: %S", &ptr); |
|
1568 |
|
1569 User::LeaveIfError(statement.Prepare(iDatabase, queryPtr)); |
|
1570 queryExecuted = ETrue; |
|
1571 } |
|
1572 CleanupStack::PopAndDestroy(query); |
|
1573 |
|
1574 if (!queryExecuted) |
|
1575 { |
|
1576 // the requested drive(s) is not open |
|
1577 User::Leave(KErrNotFound); |
|
1578 } |
|
1579 } |
|
1580 |
|
1581 return statement; |
|
1582 } |
|
1583 |
|
1584 // ---------------------------------------------------------------------------- |
|
1585 // Prepare a query for execution on all open database. This query's lifetime |
|
1586 // is owned by the dbmanager |
|
1587 // ---------------------------------------------------------------------------- |
|
1588 // |
|
1589 RSqlStatement& CMPXDbManager::PrepareQueryL( TUint aStatementId, |
|
1590 TRefByValue<const TDesC> aFmt, |
|
1591 VA_LIST aList ) |
|
1592 { |
|
1593 // Try to find the query first if it has been created |
|
1594 TInt index(KErrNotFound); |
|
1595 TInt c(iPreparedStatements.Count()); |
|
1596 |
|
1597 for( TInt i=0; i<c; ++i ) |
|
1598 { |
|
1599 if( iPreparedStatements[i].iId == aStatementId ) |
|
1600 { |
|
1601 index = i; |
|
1602 break; |
|
1603 } |
|
1604 } |
|
1605 |
|
1606 // If the index isn't found we create a new query statement |
|
1607 // |
|
1608 if( index == KErrNotFound ) |
|
1609 { |
|
1610 RSqlStatement* newStatement = new(ELeave) RSqlStatement(); |
|
1611 CleanupStack::PushL(newStatement); |
|
1612 |
|
1613 TSqlStatementState newState; |
|
1614 newState.iId = aStatementId; |
|
1615 newState.iPrepared = EFalse; |
|
1616 iPreparedStatements.AppendL( newState ); |
|
1617 |
|
1618 TInt err = iStatements.Append( newStatement ); // ownership x-fer |
|
1619 if (err != KErrNone) |
|
1620 { |
|
1621 iPreparedStatements.Remove(c); |
|
1622 User::Leave(err); |
|
1623 } |
|
1624 CleanupStack::Pop(newStatement); |
|
1625 index = c; |
|
1626 } |
|
1627 |
|
1628 // Finally create the statement |
|
1629 if ( !iPreparedStatements[index].iPrepared ) |
|
1630 { |
|
1631 |
|
1632 // Will reallocate |
|
1633 HBufC* selectBuf = FormatQueryLC(aFmt, aList); |
|
1634 TPtr selectBufPtr = selectBuf->Des(); |
|
1635 TInt dbCount = iDatabaseHandles.Count(); |
|
1636 HBufC* query = HBufC::NewLC(selectBufPtr.Length() * (dbCount + 1) + |
|
1637 KUnionAllToken().Length() * dbCount); |
|
1638 TPtr queryPtr = query->Des(); |
|
1639 HBufC* selectOutBuf = NULL; |
|
1640 TInt enclosed = selectBufPtr.Mid(1,selectBufPtr.Length() - 1).Find(KSelectToken); |
|
1641 if (enclosed != KErrNotFound) |
|
1642 { |
|
1643 enclosed++;//to compensate the indent |
|
1644 selectOutBuf = HBufC::NewLC(selectBufPtr.Length() * (dbCount + 1) + |
|
1645 KUnionAllToken().Length() * dbCount); |
|
1646 selectOutBuf->Des().Copy(selectBufPtr.Left(enclosed)); |
|
1647 selectOutBuf->Des().Append(selectBufPtr.Right(1));//the closing bracket |
|
1648 selectBufPtr.Delete(0, enclosed); |
|
1649 selectBufPtr.Delete(selectBufPtr.Length() - 1, 1); |
|
1650 } |
|
1651 |
|
1652 HBufC* orderBuf = NULL; |
|
1653 TInt orderPos = selectBufPtr.Find(KOrderByToken); |
|
1654 if (orderPos != KErrNotFound) |
|
1655 { |
|
1656 orderBuf = selectBufPtr.Right(selectBufPtr.Length() - orderPos).AllocL(); |
|
1657 selectBufPtr.Delete(orderPos, selectBufPtr.Length() - orderPos); |
|
1658 } |
|
1659 queryPtr.Append(selectBufPtr);// for cdrive |
|
1660 RemoveDriveAlias(queryPtr); |
|
1661 for (TInt i = 0; i < dbCount; ++i)//for other drives |
|
1662 { |
|
1663 if (iDatabaseHandles[i].iOpen) |
|
1664 { |
|
1665 queryPtr.Append(KUnionAllToken); |
|
1666 queryPtr.Append(selectBufPtr); |
|
1667 ReplaceDriveAlias(queryPtr, *(iDatabaseHandles[i].iAliasname)); |
|
1668 } |
|
1669 } |
|
1670 if (orderBuf) |
|
1671 { |
|
1672 queryPtr.Append(orderBuf->Des()); |
|
1673 } |
|
1674 delete orderBuf; |
|
1675 if (enclosed != KErrNotFound) |
|
1676 { |
|
1677 selectOutBuf->Des().Insert(enclosed, query->Des()); |
|
1678 queryPtr.Copy(selectOutBuf->Des()); |
|
1679 CleanupStack::PopAndDestroy(selectOutBuf); |
|
1680 } |
|
1681 |
|
1682 // Log the query string before execution |
|
1683 TPtrC ptr(query->Left(KMaxLogQuery)); |
|
1684 MPX_DEBUG2("Query: %S", &ptr); |
|
1685 |
|
1686 // use the member variable statement |
|
1687 User::LeaveIfError(iStatements[index]->Prepare(iDatabase, queryPtr)); |
|
1688 CleanupStack::PopAndDestroy(2, selectBuf); //query |
|
1689 |
|
1690 iPreparedStatements[index].iPrepared = ETrue; |
|
1691 } |
|
1692 else |
|
1693 { |
|
1694 iStatements[index]->Reset(); |
|
1695 } |
|
1696 |
|
1697 return *iStatements[index]; |
|
1698 } |
|
1699 |
|
1700 // ---------------------------------------------------------------------------- |
|
1701 // Resets all prepared queries |
|
1702 // ---------------------------------------------------------------------------- |
|
1703 // |
|
1704 void CMPXDbManager::ResetPreparedQueries() |
|
1705 { |
|
1706 MPX_FUNC("CMPXDbManager::ResetPreparedQueries"); |
|
1707 iPreparedStatements.Reset(); |
|
1708 |
|
1709 TInt c( iStatements.Count() ); |
|
1710 for( TInt i=0; i<c; ++i ) |
|
1711 { |
|
1712 iStatements[i]->Close(); |
|
1713 } |
|
1714 iStatements.ResetAndDestroy(); |
|
1715 } |
|
1716 |
|
1717 // ---------------------------------------------------------------------------- |
|
1718 // Asks all registered tables to create themselves |
|
1719 // ---------------------------------------------------------------------------- |
|
1720 // |
|
1721 EXPORT_C void CMPXDbManager::CreateTablesL( |
|
1722 RSqlDatabase& aDatabase) |
|
1723 { |
|
1724 MPX_FUNC("CMPXDbManager::CreateTablesL"); |
|
1725 |
|
1726 CreateTablesL(aDatabase, EFalse); |
|
1727 } |
|
1728 |
|
1729 // ---------------------------------------------------------------------------- |
|
1730 // CleanupTransaction: close transaction when creating DB |
|
1731 // ---------------------------------------------------------------------------- |
|
1732 // |
|
1733 static void CleanupTransaction(TAny * aDatabase) |
|
1734 { |
|
1735 TInt err = ((RSqlDatabase*)aDatabase)->Exec(KRollbackTransaction); |
|
1736 MPX_DEBUG2("CMPXDbManager CleanupTransaction rollback, error %d", err); |
|
1737 } |
|
1738 |
|
1739 // ---------------------------------------------------------------------------- |
|
1740 // CMPXDbManager::CreateTablesL |
|
1741 // ---------------------------------------------------------------------------- |
|
1742 // |
|
1743 void CMPXDbManager::CreateTablesL( |
|
1744 RSqlDatabase& aDatabase, |
|
1745 TBool aCorrupt) |
|
1746 { |
|
1747 MPX_FUNC("CMPXDbManager::CreateTablesL"); |
|
1748 TInt err = aDatabase.Exec(KBeginTransaction); |
|
1749 if (err < 0) |
|
1750 { |
|
1751 MPX_DEBUG2("SQL BEGIN TRANSACTION error %d", err); |
|
1752 User::Leave (err); |
|
1753 } |
|
1754 CleanupStack::PushL(TCleanupItem(&CleanupTransaction, &aDatabase)); |
|
1755 TInt count(iTables.Count()); |
|
1756 for (TInt i = 0; i < count; ++i) |
|
1757 { |
|
1758 iTables[i]->CreateTableL(aDatabase, aCorrupt); |
|
1759 } |
|
1760 err = aDatabase.Exec(KCommitTransaction); |
|
1761 if (err < 0) |
|
1762 { |
|
1763 MPX_DEBUG2("SQL COMMIT TRANSACTION error %d", err); |
|
1764 User::Leave (err); |
|
1765 } |
|
1766 CleanupStack::Pop(); |
|
1767 } |
|
1768 |
|
1769 // ---------------------------------------------------------------------------- |
|
1770 // Opens root database on C-drive |
|
1771 // ---------------------------------------------------------------------------- |
|
1772 // |
|
1773 void CMPXDbManager::OpenRootDatabaseL() |
|
1774 { |
|
1775 MPX_FUNC("CMPXDbManager::OpenRootDatabaseL"); |
|
1776 TDriveUnit cdrive(KRootDrive()); |
|
1777 HBufC * filename = CreateFilenameL(cdrive); |
|
1778 CleanupStack::PushL(filename); |
|
1779 User::LeaveIfError(iDatabase.Open(*filename)); |
|
1780 |
|
1781 CleanupStack::PopAndDestroy(filename); |
|
1782 } |
|
1783 |
|
1784 // ---------------------------------------------------------------------------- |
|
1785 // Creates a specified database. |
|
1786 // ---------------------------------------------------------------------------- |
|
1787 // |
|
1788 void CMPXDbManager::CreateDatabaseL( |
|
1789 TDriveUnit aDrive) |
|
1790 { |
|
1791 MPX_FUNC("CMPXDbManager::CreateDatabaseL"); |
|
1792 |
|
1793 RSqlDatabase database; |
|
1794 CleanupClosePushL(database); |
|
1795 |
|
1796 HBufC* filename = CreateFilenameL(aDrive); |
|
1797 CleanupStack::PushL(filename); |
|
1798 |
|
1799 if (database.Open(filename->Des()) != KErrNone) |
|
1800 { |
|
1801 MPX_DEBUG3("CMPXDbManager::CreateDatabaseL - cannot open db on drive %d %S", TInt(aDrive), filename); |
|
1802 |
|
1803 // close the database first |
|
1804 database.Close(); |
|
1805 DoCreateDatabaseL( aDrive ); |
|
1806 } |
|
1807 else |
|
1808 { |
|
1809 TBool tableOK(ETrue); |
|
1810 |
|
1811 // try to detect any corrupt tables |
|
1812 TInt count(iTables.Count()); |
|
1813 for (TInt i = 0; i < count; ++i) |
|
1814 { |
|
1815 // ask the table to check its structure |
|
1816 if (!iTables[i]->CheckTableL(database)) |
|
1817 { |
|
1818 tableOK = EFalse; |
|
1819 break; |
|
1820 } |
|
1821 } |
|
1822 |
|
1823 if (!tableOK) |
|
1824 { |
|
1825 // close the database first |
|
1826 database.Close(); |
|
1827 |
|
1828 // delete database and create database |
|
1829 DoCreateDatabaseL( aDrive ); |
|
1830 } |
|
1831 } |
|
1832 CleanupStack::PopAndDestroy(filename); |
|
1833 CleanupStack::PopAndDestroy(&database); |
|
1834 } |
|
1835 |
|
1836 // ---------------------------------------------------------------------------- |
|
1837 // Attaches a specified database. |
|
1838 // ---------------------------------------------------------------------------- |
|
1839 // |
|
1840 void CMPXDbManager::AttachDatabaseL( TInt aIndex ) |
|
1841 { |
|
1842 MPX_FUNC("CMPXDbManager::AttachDatabaseL"); |
|
1843 ASSERT( aIndex < iDatabaseHandles.Count() ); |
|
1844 DatabaseHandle & database = iDatabaseHandles[ aIndex ]; |
|
1845 if (!database.iOpen) |
|
1846 { |
|
1847 HBufC* filename = CreateFilenameL( database.iDrive ); |
|
1848 CleanupStack::PushL(filename); |
|
1849 |
|
1850 #ifdef __RAMDISK_PERF_ENABLE |
|
1851 if( database.iUseRAMdb ) |
|
1852 { |
|
1853 delete database.iAliasname; |
|
1854 database.iAliasname = HBufC::NewL(KAliasName().Length()); |
|
1855 HBufC* temp = HBufC::NewLC(2); // form of DE, DF, DX,... |
|
1856 temp->Des().Append(iRAMDrive); // length == 2 |
|
1857 TDriveUnit pdrive( database.iDrive ); |
|
1858 temp->Des().Append(pdrive.Name().Left(1)); //length == 2+ 1 |
|
1859 database.iAliasname->Des().Format(KRAMAliasName, temp); |
|
1860 MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - RAM change aliasname of %S", database.iAliasname ); |
|
1861 CleanupStack::PopAndDestroy(temp); |
|
1862 } |
|
1863 else |
|
1864 #endif //__RAMDISK_PERF_ENABLE |
|
1865 { |
|
1866 delete database.iAliasname; |
|
1867 TDriveUnit drive( database.iDrive ); |
|
1868 const TDesC& driveName = drive.Name(); |
|
1869 database.iAliasname = HBufC::NewL(KAliasName().Length()); |
|
1870 database.iAliasname->Des().Format(KAliasName, &driveName); |
|
1871 MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - normal change aliasname of %S", database.iAliasname); |
|
1872 } |
|
1873 |
|
1874 TInt err = iDatabase.Attach( *filename, *database.iAliasname ); |
|
1875 MPX_DEBUG2("CMPXDbManager::AttachDatabaseL - Attach Error =%d", err); |
|
1876 User::LeaveIfError(err); |
|
1877 database.iOpen = ETrue; |
|
1878 |
|
1879 CleanupStack::PopAndDestroy(filename); |
|
1880 } |
|
1881 else |
|
1882 { |
|
1883 MPX_DEBUG1("CMPXDbManager::AttachDatabaseL - found already open"); |
|
1884 } |
|
1885 } |
|
1886 |
|
1887 // ---------------------------------------------------------------------------- |
|
1888 // Detaches a specified database. |
|
1889 // ---------------------------------------------------------------------------- |
|
1890 // |
|
1891 void CMPXDbManager::DetachDatabaseL( TInt aIndex ) |
|
1892 { |
|
1893 MPX_FUNC("CMPXDbManager::DetachDatabaseL"); |
|
1894 |
|
1895 ASSERT( iInitialized && aIndex < iDatabaseHandles.Count() ); |
|
1896 DatabaseHandle & database = iDatabaseHandles[ aIndex ]; |
|
1897 if ( database.iOpen ) |
|
1898 { |
|
1899 MPX_DEBUG2("CMPXDbManager::DetachDatabaseL iAliasname=%S is open",database.iAliasname ); |
|
1900 TInt err = iDatabase.Detach(*(database.iAliasname)); |
|
1901 if ( err ) |
|
1902 { |
|
1903 MPX_DEBUG2("CMPXDbManager::DetachDatabaseL detach failed Error=%d", err); |
|
1904 } |
|
1905 User::LeaveIfError(err); |
|
1906 database.iOpen = EFalse; |
|
1907 } |
|
1908 } |
|
1909 |
|
1910 // ---------------------------------------------------------------------------- |
|
1911 // Creates the absolute database filename on a specified drive. |
|
1912 // ---------------------------------------------------------------------------- |
|
1913 // |
|
1914 HBufC* CMPXDbManager::CreateFilenameL( |
|
1915 TDriveUnit aDrive) |
|
1916 { |
|
1917 MPX_FUNC("CMPXDbManager::CreateFilenameL"); |
|
1918 |
|
1919 HBufC* filename = HBufC::NewL(KMaxFileName); |
|
1920 |
|
1921 const TDesC& securefilePath = KSecureFilePath; |
|
1922 TDriveUnit cdrive(KRootDrive()); |
|
1923 |
|
1924 #ifdef __RAMDISK_PERF_ENABLE |
|
1925 TInt index(GetDatabaseIndex((TInt)aDrive)); |
|
1926 if ( index >=0 && iDatabaseHandles[index].iUseRAMdb && aDrive != cdrive ) |
|
1927 { |
|
1928 MPX_DEBUG1("CMPXDbManager::CreateFilenameL - use RAMDisk"); |
|
1929 TFileName path; |
|
1930 path.Append(iRAMDrive); |
|
1931 path.Append(_L(":")); |
|
1932 TBuf<2> d; |
|
1933 d.Append(aDrive.Name()); |
|
1934 TFileName temp; |
|
1935 temp.Append(d.Left(1)); // attach original drive name |
|
1936 temp.Append(iDbFile->Des()); |
|
1937 filename->Des().Format(securefilePath, &path, User::Identity().iUid, &temp); |
|
1938 MPX_DEBUG3("CMPXDbManager::CreateFilenameL - path=%S filename=%S", &path, filename); |
|
1939 } |
|
1940 else |
|
1941 #endif //__RAMDISK_PERF_ENABLE |
|
1942 { |
|
1943 MPX_DEBUG1("CMPXDbManager::CreateFilenameL - use normal drive"); |
|
1944 const TDesC& driveName = aDrive.Name(); |
|
1945 filename->Des().Format(securefilePath, &driveName, User::Identity().iUid, iDbFile); |
|
1946 } |
|
1947 |
|
1948 MPX_DEBUG2("CMPXDbManager::CreateFilenameL filename = %S", filename); |
|
1949 return filename; |
|
1950 } |
|
1951 |
|
1952 // ---------------------------------------------------------------------------- |
|
1953 // Replaces :dbname with a drive alias |
|
1954 // ---------------------------------------------------------------------------- |
|
1955 // |
|
1956 void CMPXDbManager::ReplaceDriveAlias( |
|
1957 TDes& aQuery, |
|
1958 const TDesC& aAlias) |
|
1959 { |
|
1960 // MPX_FUNC("CMPXDbManager::ReplaceDriveAlias"); |
|
1961 |
|
1962 TInt dbnamePos(aQuery.Find(KDBNameToken)); |
|
1963 while (dbnamePos != KErrNotFound) |
|
1964 { |
|
1965 aQuery.Delete(dbnamePos, KDBNameToken().Length()); |
|
1966 aQuery.Insert(dbnamePos, aAlias); |
|
1967 dbnamePos = aQuery.Find(KDBNameToken); |
|
1968 } |
|
1969 } |
|
1970 |
|
1971 // ---------------------------------------------------------------------------- |
|
1972 // Replaces :dbname with a drive alias |
|
1973 // ---------------------------------------------------------------------------- |
|
1974 // |
|
1975 void CMPXDbManager::ReplaceDriveAlias( |
|
1976 TDes& aQuery, |
|
1977 const TDesC& aAlias, |
|
1978 const TDesC& aToKen) |
|
1979 { |
|
1980 |
|
1981 TInt dbnamePos(aQuery.Find(aToKen)); |
|
1982 while (dbnamePos != KErrNotFound) |
|
1983 { |
|
1984 aQuery.Delete(dbnamePos, aToKen.Length()); |
|
1985 aQuery.Insert(dbnamePos, aAlias); |
|
1986 dbnamePos = aQuery.Find(aToKen); |
|
1987 } |
|
1988 } |
|
1989 |
|
1990 // ---------------------------------------------------------------------------- |
|
1991 // Removes :dbname |
|
1992 // ---------------------------------------------------------------------------- |
|
1993 // |
|
1994 void CMPXDbManager::RemoveDriveAlias( |
|
1995 TDes& aQuery) |
|
1996 { |
|
1997 MPX_FUNC("CMPXDbManager::RemoveDriveAlias"); |
|
1998 |
|
1999 TInt dbnamePos(aQuery.Find(KDBNameToken)); |
|
2000 while (dbnamePos != KErrNotFound) |
|
2001 { |
|
2002 aQuery.Delete(dbnamePos, KDBNameToken().Length() + 1); |
|
2003 dbnamePos = aQuery.Find(KDBNameToken); |
|
2004 } |
|
2005 } |
|
2006 |
|
2007 |
|
2008 // ---------------------------------------------------------------------------- |
|
2009 // Removes :dbname |
|
2010 // ---------------------------------------------------------------------------- |
|
2011 // |
|
2012 void CMPXDbManager::RemoveDriveAlias( |
|
2013 TDes& aQuery,const TDesC& aToKen) |
|
2014 { |
|
2015 MPX_FUNC("CMPXDbManager::RemoveDriveAlias"); |
|
2016 |
|
2017 TInt dbnamePos(aQuery.Find(aToKen)); |
|
2018 while (dbnamePos != KErrNotFound) |
|
2019 { |
|
2020 aQuery.Delete(dbnamePos, aToKen.Length() + 1); |
|
2021 dbnamePos = aQuery.Find(aToKen); |
|
2022 } |
|
2023 } |
|
2024 |
|
2025 |
|
2026 // ---------------------------------------------------------------------------- |
|
2027 // CMPXDbManager::CheckDiskSpaceL |
|
2028 // ---------------------------------------------------------------------------- |
|
2029 // |
|
2030 EXPORT_C void CMPXDbManager::CheckDiskSpaceL( |
|
2031 TInt aDrive) |
|
2032 { |
|
2033 MPX_FUNC("CMPXDbManager::CheckDiskSpaceL"); |
|
2034 |
|
2035 // LTAN-7GH6BZ, crash if eject memory card when adding song to existing playlist |
|
2036 // due to special timing issue, it is possible drive number is -1 and create a |
|
2037 // panic when use for TDriveUnit |
|
2038 MPX_DEBUG2("aDrive = %d", aDrive); |
|
2039 |
|
2040 if (aDrive < 0) |
|
2041 { |
|
2042 MPX_DEBUG1("invalid driveId, leave with KErrNotReady"); |
|
2043 User::Leave(KErrNotReady); |
|
2044 } |
|
2045 |
|
2046 EnsureDiskSpaceL(aDrive); |
|
2047 } |
|
2048 |
|
2049 // ---------------------------------------------------------------------------- |
|
2050 // Regenerate all databases. |
|
2051 // ---------------------------------------------------------------------------- |
|
2052 // |
|
2053 EXPORT_C void CMPXDbManager::RegenerateAllDatabasesL() |
|
2054 { |
|
2055 MPX_DEBUG1("CMPXDbManager::RegenerateAllDatabasesL Enter"); |
|
2056 ResetPreparedQueries(); //just in case ... |
|
2057 TInt handles(iDatabaseHandles.Count()); |
|
2058 for (TInt i = 0; i < handles; ++i) |
|
2059 { |
|
2060 iDatabaseHandles[i].iOpen = EFalse; //attach will open them again |
|
2061 } |
|
2062 iDatabase.Close(); //close the database before deleting the file |
|
2063 iInitialized = EFalse; |
|
2064 |
|
2065 MPX_DEBUG1("RegenerateAllDatabasesL: Regenerating main DB on C:"); |
|
2066 HBufC * filename = CreateFilenameL(EDriveC); |
|
2067 CleanupStack::PushL(filename); |
|
2068 RSqlDatabase::Delete(*filename); |
|
2069 TDriveUnit cdrive(KRootDrive()); |
|
2070 CreateDatabaseL(cdrive); |
|
2071 User::LeaveIfError(iDatabase.Open(*filename)); // will set handle status later |
|
2072 CleanupStack::PopAndDestroy(filename); |
|
2073 MPX_DEBUG1("RegenerateAllDatabasesL: DB regeneration complete"); |
|
2074 |
|
2075 // Recreate all attached drives |
|
2076 TInt count(iDatabaseHandles.Count()); |
|
2077 for (TInt i = 0; i < count; ++i) |
|
2078 { |
|
2079 if (iDatabaseHandles[i].iDrive != EDriveC) |
|
2080 { |
|
2081 MPX_DEBUG2("RegenerateAllDatabasesL: Regenerating DB on %d",iDatabaseHandles[i].iDrive); |
|
2082 TVolumeInfo volumeInfo; |
|
2083 TInt err = iFs.Volume(volumeInfo,iDatabaseHandles[i].iDrive); |
|
2084 if(err != KErrNone) |
|
2085 { |
|
2086 continue; //if drive is not currently accessible, skip |
|
2087 } |
|
2088 filename = CreateFilenameL(iDatabaseHandles[i].iDrive); |
|
2089 CleanupStack::PushL(filename); |
|
2090 MPX_DEBUG1("RegenerateAllDatabasesL: Detaching DB"); |
|
2091 err = iDatabase.Detach(*(iDatabaseHandles[i].iAliasname)); //ignore the error if any |
|
2092 MPX_DEBUG2("RegenerateAllDatabasesL: Detached[err=%d]; Deleting DB",err); |
|
2093 err = RSqlDatabase::Delete(*filename); |
|
2094 MPX_DEBUG2("RegenerateAllDatabasesL: Deleted[err=%d]; Creating new DB",err); |
|
2095 TDriveUnit drive(iDatabaseHandles[i].iDrive); |
|
2096 CreateDatabaseL(drive); |
|
2097 MPX_DEBUG1("RegenerateAllDatabasesL: Attaching new DB"); |
|
2098 AttachDatabaseL( i ); |
|
2099 MPX_DEBUG1("RegenerateAllDatabasesL: DB regeneration complete"); |
|
2100 CleanupStack::PopAndDestroy(filename); |
|
2101 } |
|
2102 else |
|
2103 { |
|
2104 iDatabaseHandles[i].iOpen = ETrue; //if we got here it is opened |
|
2105 } |
|
2106 } |
|
2107 iInitialized = ETrue; |
|
2108 MPX_DEBUG1("CMPXDbManager::RegenerateAllDatabasesL Exit"); |
|
2109 } |
|
2110 |
|
2111 // ---------------------------------------------------------------------------- |
|
2112 // CMPXDbManager::DoRecreateDatabaseL |
|
2113 // ---------------------------------------------------------------------------- |
|
2114 // |
|
2115 void CMPXDbManager::DoRecreateDatabaseL(HBufC * aFilename) |
|
2116 { |
|
2117 RSqlDatabase database; |
|
2118 CleanupClosePushL(database); |
|
2119 |
|
2120 User::LeaveIfError(database.Open(aFilename->Des())); |
|
2121 |
|
2122 TInt count(iTables.Count()); |
|
2123 for (TInt i = 0; i < count; ++i) |
|
2124 { |
|
2125 iTables[i]->DropTableL(database); |
|
2126 iTables[i]->CreateTableL(database, EFalse); |
|
2127 } |
|
2128 CleanupStack::PopAndDestroy(&database); |
|
2129 } |
|
2130 |
|
2131 // ---------------------------------------------------------------------------- |
|
2132 // CMPXDbManager::ExecuteSqlStatement |
|
2133 // ---------------------------------------------------------------------------- |
|
2134 // |
|
2135 TInt CMPXDbManager::ExecuteSqlStatement(RSqlDatabase& aDatabase,const TDesC& aStatement) |
|
2136 { |
|
2137 MPX_FUNC("CMPXDbManager::ExecuteSqlStatement"); |
|
2138 TInt result( KErrNone ); |
|
2139 RSqlStatement sqlStatement; |
|
2140 //Prepare and execute SQL statement |
|
2141 result = sqlStatement.Prepare(aDatabase, aStatement); |
|
2142 if (result == KErrNone) |
|
2143 { |
|
2144 result = sqlStatement.Exec(); |
|
2145 //If the database schema was changed or the session expired repeat all the steps |
|
2146 if((result == KSqlErrStmtExpired) || (result == KSqlErrSchema)) |
|
2147 { |
|
2148 sqlStatement.Close(); |
|
2149 result = sqlStatement.Prepare(aDatabase, aStatement); |
|
2150 if (result == KErrNone) |
|
2151 { |
|
2152 result = sqlStatement.Exec(); |
|
2153 } |
|
2154 } |
|
2155 sqlStatement.Close(); |
|
2156 } |
|
2157 return result; |
|
2158 } |
|
2159 |
|
2160 #ifdef _DEBUG |
|
2161 |
|
2162 // ---------------------------------------------------------------------------- |
|
2163 // Returns the number of columns from a specified SQL statement |
|
2164 // ---------------------------------------------------------------------------- |
|
2165 // |
|
2166 TInt CMPXDbManager::GetColumnCountL( |
|
2167 RSqlStatement& aStatement) |
|
2168 { |
|
2169 TInt columnCount(0); |
|
2170 |
|
2171 // Using TSqlRowSetUtil causes linker errors on ARMv5 UDEB |
|
2172 // Enabling this functionality for WINSCW UDEB only |
|
2173 // PREQ2536 the files sqlrowsetutil.h and sqlrowsetutil.cpp has been removed |
|
2174 //#ifdef __WINSCW__ |
|
2175 // |
|
2176 // HBufC* headers = TSqlRowSetUtil::GetDeclColumnTypesL(aStatement); |
|
2177 // CleanupStack::PushL(headers); |
|
2178 // |
|
2179 // // Count the number of semicolons to get the number of columns |
|
2180 // TPtr headerPtr = headers->Des(); |
|
2181 // TInt location(headerPtr.Locate(';')); |
|
2182 // while ((location != KErrNotFound) && (location < headers->Length())) |
|
2183 // { |
|
2184 // ++columnCount; |
|
2185 // if (++location < headers->Length()) |
|
2186 // { |
|
2187 // headerPtr = headers->Des().Mid(location); |
|
2188 // location = headerPtr.Locate(';'); |
|
2189 // } |
|
2190 // } |
|
2191 // CleanupStack::PopAndDestroy(headers); |
|
2192 // |
|
2193 //#else |
|
2194 (void)aStatement; |
|
2195 //#endif |
|
2196 |
|
2197 return columnCount; |
|
2198 } |
|
2199 |
|
2200 // ---------------------------------------------------------------------------- |
|
2201 // Prints the table values from a specified SQL query |
|
2202 // ---------------------------------------------------------------------------- |
|
2203 // |
|
2204 void CMPXDbManager::PrintTableValuesL( |
|
2205 RSqlStatement& aStatement) |
|
2206 { |
|
2207 TInt columnCount(GetColumnCountL(aStatement)); |
|
2208 TInt err(KErrNone); |
|
2209 HBufC* tableRow = HBufC::NewLC(255 * columnCount); |
|
2210 TPtr tableRowPtr = tableRow->Des(); |
|
2211 |
|
2212 while ((err = aStatement.Next()) == KSqlAtRow) |
|
2213 { |
|
2214 tableRowPtr.Zero(); |
|
2215 TInt error(KErrNone); |
|
2216 for (TInt index = 0; (error == KErrNone) && (index < columnCount); ++index) |
|
2217 { |
|
2218 if (index !=0) |
|
2219 { |
|
2220 tableRowPtr.Append(','); |
|
2221 } |
|
2222 switch (aStatement.ColumnType(index)) |
|
2223 { |
|
2224 case ESqlNull: |
|
2225 tableRowPtr.Append(_L("<NULL>")); |
|
2226 break; |
|
2227 |
|
2228 case ESqlInt: |
|
2229 { |
|
2230 tableRowPtr.AppendFormat(_L("%u"), aStatement.ColumnInt(index)); |
|
2231 } |
|
2232 break; |
|
2233 |
|
2234 case ESqlInt64: |
|
2235 { |
|
2236 tableRowPtr.AppendFormat(_L("%lu"), aStatement.ColumnInt64(index)); |
|
2237 } |
|
2238 break; |
|
2239 |
|
2240 case ESqlReal: |
|
2241 { |
|
2242 tableRowPtr.AppendFormat(_L("%f"), aStatement.ColumnReal(index)); |
|
2243 } |
|
2244 break; |
|
2245 |
|
2246 case ESqlText: |
|
2247 { |
|
2248 TPtrC columnValue; |
|
2249 error = aStatement.ColumnText(index, columnValue); |
|
2250 if (error == KErrNone) |
|
2251 { |
|
2252 tableRowPtr.AppendFormat(_L("%S"), &columnValue); |
|
2253 } |
|
2254 } |
|
2255 break; |
|
2256 |
|
2257 case ESqlBinary: |
|
2258 { |
|
2259 TPtrC8 columnValue; |
|
2260 error = aStatement.ColumnBinary(index, columnValue); |
|
2261 if (error == KErrNone) |
|
2262 { |
|
2263 tableRowPtr.AppendFormat(_L("%S"), &columnValue); |
|
2264 } |
|
2265 } |
|
2266 break; |
|
2267 |
|
2268 default : |
|
2269 ASSERT(EFalse); |
|
2270 } |
|
2271 |
|
2272 if (tableRowPtr.Length() > 255) |
|
2273 { |
|
2274 tableRowPtr.SetLength(255); |
|
2275 MPX_DEBUG2("%S", tableRow); |
|
2276 tableRowPtr.Zero(); |
|
2277 } |
|
2278 } |
|
2279 if (tableRowPtr.Length() > 0) |
|
2280 { |
|
2281 tableRowPtr.SetLength(Min(tableRowPtr.Length(), 255)); |
|
2282 MPX_DEBUG2("%S", tableRow); |
|
2283 } |
|
2284 } |
|
2285 CleanupStack::PopAndDestroy(tableRow); |
|
2286 if (err != KSqlAtEnd) |
|
2287 { |
|
2288 User::Leave(err); |
|
2289 } |
|
2290 } |
|
2291 |
|
2292 // ---------------------------------------------------------------------------- |
|
2293 // Finds all the tables on the main or attached drives |
|
2294 // ---------------------------------------------------------------------------- |
|
2295 // |
|
2296 void CMPXDbManager::FindAllTablesL( |
|
2297 const TDesC& aAlias, |
|
2298 RArray<HBufC*>& aTableName) |
|
2299 { |
|
2300 RSqlStatement statement; |
|
2301 CleanupClosePushL(statement); |
|
2302 |
|
2303 if (aAlias == KNullDesC) |
|
2304 { |
|
2305 statement.Prepare(iDatabase, KFindAllCDriveTablesQuery); |
|
2306 } |
|
2307 else |
|
2308 { |
|
2309 HBufC* query = KFindAllAttachedTablesQuery().AllocL(); |
|
2310 CleanupStack::PushL(query); |
|
2311 TPtr queryPtr = query->Des(); |
|
2312 ReplaceDriveAlias(queryPtr, aAlias); |
|
2313 statement.Prepare(iDatabase, queryPtr); |
|
2314 CleanupStack::PopAndDestroy(query); |
|
2315 } |
|
2316 |
|
2317 TInt err(KErrNone); |
|
2318 |
|
2319 while ((err = statement.Next()) == KSqlAtRow) |
|
2320 { |
|
2321 TPtrC val = statement.ColumnTextL(statement.ColumnIndex(KNameColumn)); |
|
2322 aTableName.AppendL(val.AllocL()); |
|
2323 } |
|
2324 if (err != KSqlAtEnd) |
|
2325 { |
|
2326 User::Leave(err); |
|
2327 } |
|
2328 |
|
2329 CleanupStack::PopAndDestroy(&statement); |
|
2330 } |
|
2331 |
|
2332 // ---------------------------------------------------------------------------- |
|
2333 // Prints the tables on the main or attached drives |
|
2334 // ---------------------------------------------------------------------------- |
|
2335 // |
|
2336 void CMPXDbManager::PrintTableL( |
|
2337 const TDesC& aAlias, |
|
2338 const TDesC& aTableName) |
|
2339 { |
|
2340 RSqlStatement statement; |
|
2341 CleanupClosePushL(statement); |
|
2342 |
|
2343 if (aAlias == KNullDesC) |
|
2344 { |
|
2345 HBufC* selectQuery = HBufC::NewLC(KTableQuery().Length() + aTableName.Length()); |
|
2346 selectQuery->Des().Format(KTableQuery, &aTableName); |
|
2347 User::LeaveIfError(statement.Prepare(iDatabase, *selectQuery)); |
|
2348 CleanupStack::PopAndDestroy(selectQuery); |
|
2349 } |
|
2350 else |
|
2351 { |
|
2352 HBufC* selectQuery = HBufC::NewLC(KAttachedTableQuery().Length() + aTableName.Length()); |
|
2353 selectQuery->Des().Format(KAttachedTableQuery, &aTableName); |
|
2354 |
|
2355 TPtr selectQueryPtr(selectQuery->Des()); |
|
2356 ReplaceDriveAlias(selectQueryPtr, aAlias); |
|
2357 User::LeaveIfError(statement.Prepare(iDatabase, *selectQuery)); |
|
2358 |
|
2359 CleanupStack::PopAndDestroy(selectQuery); |
|
2360 } |
|
2361 |
|
2362 PrintTableValuesL(statement); |
|
2363 CleanupStack::PopAndDestroy(&statement); |
|
2364 } |
|
2365 |
|
2366 #endif |
|
2367 |
|
2368 // ---------------------------------------------------------------------------- |
|
2369 // Prints all the tables on the main and attached drives |
|
2370 // ---------------------------------------------------------------------------- |
|
2371 // |
|
2372 EXPORT_C void CMPXDbManager::PrintDatabaseL() |
|
2373 { |
|
2374 #ifdef _DEBUG |
|
2375 if (iInitialized) |
|
2376 { |
|
2377 // C-Drive |
|
2378 RArray<HBufC*> tableNames; |
|
2379 FindAllTablesL(KNullDesC(), tableNames); |
|
2380 |
|
2381 MPX_DEBUG1("### Drive C ###"); |
|
2382 |
|
2383 TInt tableCount(tableNames.Count()); |
|
2384 for (TInt i = 0; i < tableCount; ++i) |
|
2385 { |
|
2386 MPX_DEBUG2("# %S #", tableNames[i]); |
|
2387 MPX_TRAPD(error, PrintTableL(KNullDesC, *tableNames[i])); |
|
2388 delete tableNames[i]; |
|
2389 if (error != KErrNone) |
|
2390 { |
|
2391 if (error != KErrPermissionDenied) |
|
2392 { |
|
2393 User::Leave(error); |
|
2394 } |
|
2395 else |
|
2396 { |
|
2397 MPX_DEBUG1("Unable to print table"); |
|
2398 } |
|
2399 } |
|
2400 } |
|
2401 tableNames.Close(); |
|
2402 |
|
2403 // Each attached drive |
|
2404 TInt count(iDatabaseHandles.Count()); |
|
2405 for (TInt i = 0; i < count; ++i) |
|
2406 { |
|
2407 if (iDatabaseHandles[i].iOpen) |
|
2408 { |
|
2409 FindAllTablesL(iDatabaseHandles[i].iAliasname->Des(), tableNames); |
|
2410 |
|
2411 TDriveUnit driveUnit(iDatabaseHandles[i].iDrive); |
|
2412 const TDesC& name = driveUnit.Name(); |
|
2413 MPX_DEBUG2("### Drive %S ###", &name); |
|
2414 |
|
2415 for (TInt j = 0; j < tableCount; ++j) |
|
2416 { |
|
2417 MPX_DEBUG2("# %S #", tableNames[j]); |
|
2418 MPX_TRAPD(error, PrintTableL(iDatabaseHandles[i].iAliasname->Des(), *tableNames[j])); |
|
2419 delete tableNames[j]; |
|
2420 if (error != KErrNone) |
|
2421 { |
|
2422 if (error != KErrPermissionDenied) |
|
2423 { |
|
2424 User::Leave(error); |
|
2425 } |
|
2426 else |
|
2427 { |
|
2428 MPX_DEBUG1("Unable to print table"); |
|
2429 } |
|
2430 } |
|
2431 } |
|
2432 tableNames.Close(); |
|
2433 } |
|
2434 } |
|
2435 } |
|
2436 #endif |
|
2437 } |
|
2438 |
|
2439 // ---------------------------------------------------------------------------- |
|
2440 // Prints all the tables on the main and attached drives |
|
2441 // ---------------------------------------------------------------------------- |
|
2442 // |
|
2443 EXPORT_C RFs& CMPXDbManager::Fs() |
|
2444 { |
|
2445 return iFs; |
|
2446 } |
|
2447 |
|
2448 |
|
2449 |
|
2450 // --------------------------------------------------------------------------- |
|
2451 // CMPXDbManager::IsRemoteDrive |
|
2452 // --------------------------------------------------------------------------- |
|
2453 // |
|
2454 EXPORT_C TBool CMPXDbManager::IsRemoteDrive(TDriveNumber aDrive) |
|
2455 { |
|
2456 TDriveInfo driveInfo; |
|
2457 TBool isRemoteDrive(EFalse); |
|
2458 if (iFs.Drive(driveInfo, aDrive) == KErrNone) |
|
2459 { |
|
2460 isRemoteDrive = driveInfo.iDriveAtt & KDriveAttRemote; |
|
2461 } |
|
2462 return isRemoteDrive; |
|
2463 } |
|
2464 |
|
2465 // --------------------------------------------------------------------------- |
|
2466 // CMPXDbManager::DoCreateDatabaseL |
|
2467 // --------------------------------------------------------------------------- |
|
2468 // |
|
2469 void CMPXDbManager::DoCreateDatabaseL( TDriveUnit aDrive ) |
|
2470 { |
|
2471 MPX_FUNC( "CMPXDbManager::DoCreateDatabaseL" ); |
|
2472 |
|
2473 RSqlDatabase database; |
|
2474 CleanupClosePushL(database); |
|
2475 |
|
2476 HBufC* filename = CreateFilenameL(aDrive); |
|
2477 CleanupStack::PushL(filename); |
|
2478 |
|
2479 // remove old databases before creating/replacing new database |
|
2480 TInt driveNameLen = aDrive.Name().Length(); |
|
2481 |
|
2482 TFileName dbFileName; |
|
2483 |
|
2484 #ifdef __RAMDISK_PERF_ENABLE |
|
2485 TInt index(GetDatabaseIndex((TInt)aDrive)); |
|
2486 if( index >= 0 && iDatabaseHandles[index].iUseRAMdb ) |
|
2487 { |
|
2488 dbFileName.Append(iRAMDrive); // RAM |
|
2489 dbFileName.Append(_L(":")); // RAM |
|
2490 } |
|
2491 else |
|
2492 #endif //__RAMDISK_PERF_ENABLE |
|
2493 { |
|
2494 MPX_DEBUG1("CMPXDbManager::CreateDatabaseL - E:"); |
|
2495 dbFileName.Append(aDrive.Name()); //initialise with drive name |
|
2496 } |
|
2497 |
|
2498 |
|
2499 MPX_DEBUG2("CMPXDbManager::CreateDatabaseL - dbFileName=%S", &dbFileName); |
|
2500 dbFileName.Append(KDBFilePath); // append private path |
|
2501 |
|
2502 //append file name |
|
2503 dbFileName.Append(filename->Right((filename->Length())- driveNameLen)); |
|
2504 |
|
2505 // locate the offset position where version info starts in file name |
|
2506 TInt pos = dbFileName.LocateReverse('v'); |
|
2507 |
|
2508 //replace version info with wildcards |
|
2509 dbFileName.Replace(pos, (dbFileName.Length()- pos), KDBFilePattern); |
|
2510 |
|
2511 CFileMan* fileManager = CFileMan::NewL(iFs); |
|
2512 TInt ret = fileManager->Delete(dbFileName); |
|
2513 delete fileManager; |
|
2514 fileManager = NULL; |
|
2515 |
|
2516 // create the database now |
|
2517 RSqlSecurityPolicy securityPolicy; |
|
2518 CleanupClosePushL(securityPolicy); |
|
2519 |
|
2520 TSecurityPolicy policy(TSecurityPolicy::EAlwaysPass); |
|
2521 securityPolicy.Create(policy); |
|
2522 |
|
2523 TSecurityPolicy schemaPolicy(TSecurityPolicy::EAlwaysPass); |
|
2524 TSecurityPolicy readPolicy(TSecurityPolicy::EAlwaysPass); |
|
2525 TSecurityPolicy writePolicy(TSecurityPolicy::EAlwaysPass); |
|
2526 |
|
2527 User::LeaveIfError(securityPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, schemaPolicy)); |
|
2528 User::LeaveIfError(securityPolicy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, readPolicy)); |
|
2529 User::LeaveIfError(securityPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, writePolicy)); |
|
2530 |
|
2531 |
|
2532 const TDesC8& config = KMCSqlConfig; |
|
2533 |
|
2534 TBool corrupt(EFalse); |
|
2535 TInt err = database.Create(filename->Des(), securityPolicy, &config); |
|
2536 if (KErrAlreadyExists == err) |
|
2537 { |
|
2538 MPX_DEBUG1("CMPXDbManager::DoCreateDatabaseL - could not create the database"); |
|
2539 |
|
2540 // the file already exists and it is corrupted |
|
2541 // make sure we delete the file |
|
2542 User::LeaveIfError(database.Delete(*filename)); |
|
2543 |
|
2544 MPX_DEBUG1("CMPXDbManager::DoCreateDatabaseL - deleted the database"); |
|
2545 |
|
2546 // try again |
|
2547 err = database.Create(filename->Des(), securityPolicy, &config); |
|
2548 |
|
2549 // the database could not be opened but the file exists |
|
2550 corrupt = ETrue; |
|
2551 } |
|
2552 User::LeaveIfError(err); |
|
2553 |
|
2554 MPX_DEBUG1("CMPXDbManager::DoCreateDatabaseL - created the database"); |
|
2555 |
|
2556 CleanupStack::PopAndDestroy(&securityPolicy); |
|
2557 |
|
2558 CreateTablesL(database, corrupt); |
|
2559 |
|
2560 CleanupStack::PopAndDestroy(filename); |
|
2561 CleanupStack::PopAndDestroy(&database); |
|
2562 } |
|
2563 |
|
2564 // --------------------------------------------------------------------------- |
|
2565 // CMPXDbManager::GetRAMDiskPath |
|
2566 // --------------------------------------------------------------------------- |
|
2567 // |
|
2568 TInt CMPXDbManager::GetRAMDiskPath() |
|
2569 { |
|
2570 TInt error = KErrNotSupported; |
|
2571 #ifdef __RAMDISK_PERF_ENABLE |
|
2572 MPX_DEBUG1("-->CMPXDbManager::GetRAMDiskPath"); |
|
2573 TDriveList driveList; |
|
2574 TBool ramDriveFound = EFalse; |
|
2575 TInt driveOffset = 'A'; |
|
2576 iRAMFolder.Zero(); |
|
2577 |
|
2578 error = iFs.DriveList( driveList ); |
|
2579 if ( error == KErrNone ) |
|
2580 { |
|
2581 for ( TInt i = 0; i < driveList.Length(); i++ ) |
|
2582 { |
|
2583 if ( driveList[i] != 0 ) |
|
2584 { |
|
2585 TDriveInfo info; |
|
2586 TInt err = iFs.Drive( info, i ); |
|
2587 if ( !err && info.iType == EMediaRam ) |
|
2588 { |
|
2589 iRAMDrive = driveOffset + i; |
|
2590 iRAMFolder.Append(iRAMDrive); |
|
2591 iRAMFolder.Append(_L(":")); |
|
2592 iRAMFolder.Append(KDBFilePath); |
|
2593 ramDriveFound = ETrue; |
|
2594 MPX_DEBUG2("RAMDisk path=%S", &iRAMFolder); |
|
2595 break; |
|
2596 } |
|
2597 } |
|
2598 } |
|
2599 |
|
2600 // Check if ram drive is found. |
|
2601 if ( !ramDriveFound ) |
|
2602 { |
|
2603 error = KErrNotFound; |
|
2604 } |
|
2605 } |
|
2606 MPX_DEBUG2("CMPXDbManager::GetRAMDiskPath Get DriveList error=%d", error); |
|
2607 MPX_DEBUG1("<--CMPXDbManager::GetRAMDiskPath"); |
|
2608 #endif //__RAMDISK_PERF_ENABLE |
|
2609 return error; |
|
2610 } |
|
2611 |
|
2612 // --------------------------------------------------------------------------- |
|
2613 // CMPXDbManager::IsRamDiskSpaceAvailable |
|
2614 // --------------------------------------------------------------------------- |
|
2615 // |
|
2616 TBool CMPXDbManager::IsRamDiskSpaceAvailable() |
|
2617 { |
|
2618 |
|
2619 #ifdef __RAMDISK_PERF_ENABLE |
|
2620 |
|
2621 MPX_DEBUG1("-->CMPXDbManager::IsDiskSpaceAvailable" ); |
|
2622 TInt driveIndex; |
|
2623 RFs::CharToDrive(iRAMDrive, driveIndex); |
|
2624 TVolumeInfo vol; |
|
2625 TInt err = iFs.Volume( vol, driveIndex ); |
|
2626 if ( err == KErrNone ) |
|
2627 { |
|
2628 MPX_DEBUG2("CMPXDbManager::IsRamDiskSpaceAvailable Free in bytes =%Lu", vol.iFree); |
|
2629 if ( vol.iFree > KMPMinimumRAMSizeToRun ) |
|
2630 { |
|
2631 MPX_DEBUG1("CMPXDbManager::IsRamDiskSpaceAvailable Ok to copy"); |
|
2632 return ETrue; |
|
2633 } |
|
2634 MPX_DEBUG1("CMPXDbManager::IsRamDiskSpaceAvailable NOT Ok to copy"); |
|
2635 return EFalse; |
|
2636 } |
|
2637 |
|
2638 MPX_DEBUG2("CMPXDbManager::IsRamDiskSpaceAvailable Disk Not available to use. %d", err); |
|
2639 MPX_DEBUG1("<--CMPXDbManager::IsDiskSpaceAvailable"); |
|
2640 |
|
2641 #endif //__RAMDISK_PERF_ENABLE |
|
2642 |
|
2643 return EFalse; |
|
2644 } |
|
2645 |
|
2646 |
|
2647 // --------------------------------------------------------------------------- |
|
2648 // CMPXDbManager::BlockDiskSpaceL |
|
2649 // --------------------------------------------------------------------------- |
|
2650 // |
|
2651 TBool CMPXDbManager::BlockDiskSpace( TInt aIndex, TBool aIsMTPInUse ) |
|
2652 { |
|
2653 #ifdef __RAMDISK_PERF_ENABLE |
|
2654 |
|
2655 MPX_DEBUG2("-->CMPXDbManager::BlockDiskSpaceL %d", aIndex ); |
|
2656 DatabaseHandle & database = iDatabaseHandles[aIndex]; |
|
2657 // if current DB size can not fit in RAM, abort now |
|
2658 TInt ramDrive; |
|
2659 RFs::CharToDrive(iRAMDrive, ramDrive); |
|
2660 TVolumeInfo vol; |
|
2661 TInt err = iFs.Volume( vol, ramDrive ); |
|
2662 TEntry origDb; |
|
2663 iFs.Entry( *database.iOrigFullFilePath, origDb ); |
|
2664 if ( vol.iFree <= origDb.iSize + KMPMinimumRAMSizeToRun ) |
|
2665 { |
|
2666 MPX_DEBUG1("-->CMPXDbManager::BlockDiskSpaceL Not enough even for copy original DB file, leave" ); |
|
2667 return EFalse; |
|
2668 } |
|
2669 |
|
2670 // ensure you have the disk volume and database |
|
2671 err = iFs.Volume( vol, database.iDrive ); |
|
2672 if (err != KErrNone) |
|
2673 { |
|
2674 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Volume not available on drive %d", database.iDrive); |
|
2675 return EFalse; |
|
2676 } |
|
2677 |
|
2678 // Check if the drive has enough space to block |
|
2679 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Disk total free space in bytes =%Lu", vol.iFree); |
|
2680 TInt64 blockingSize( CalculateInitalDummyDBSize( vol, origDb.iSize, aIsMTPInUse )); |
|
2681 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Disk blocking size =%Lu", blockingSize); |
|
2682 if ( vol.iFree <= blockingSize + 1*KMPMegaByte ) |
|
2683 { |
|
2684 MPX_DEBUG1("CMPXDbManager::BlockDiskSpaceL NOk to block"); |
|
2685 return EFalse; |
|
2686 } |
|
2687 |
|
2688 // Create and resize the dummy file |
|
2689 TChar ch; |
|
2690 RFs::DriveToChar(database.iDrive, ch ); |
|
2691 database.iDummyFilePath.Format( KDummyDbFile, (TUint)ch); |
|
2692 RFile dummyDb; |
|
2693 err = dummyDb.Replace( iFs, database.iDummyFilePath, EFileWrite ); |
|
2694 if (err != KErrNone) |
|
2695 { |
|
2696 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't open dummy file %d", err); |
|
2697 database.iDummyFilePath.Zero(); |
|
2698 return EFalse; |
|
2699 } |
|
2700 err = dummyDb.SetSize( blockingSize ); |
|
2701 if ( err ) |
|
2702 { |
|
2703 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't resize dummy file %d", err); |
|
2704 dummyDb.Close(); |
|
2705 RemoveDummyFile(aIndex); |
|
2706 return EFalse; |
|
2707 } |
|
2708 |
|
2709 dummyDb.Close(); |
|
2710 MPX_DEBUG1("CMPXDbManager::BlockDiskSpaceL Ok to block"); |
|
2711 MPX_DEBUG1("<--CMPXDbManager::BlockDiskSpace"); |
|
2712 |
|
2713 return ETrue; |
|
2714 #else |
|
2715 return EFalse; |
|
2716 #endif //__RAMDISK_PERF_ENABLE |
|
2717 } |
|
2718 |
|
2719 |
|
2720 // --------------------------------------------------------------------------- |
|
2721 // CMPXDbManager::CalculateInitalDummyDBSizeL |
|
2722 // |
|
2723 //a) MTP case |
|
2724 //------------- |
|
2725 // totalNumOfSongsCanFit = <disk free space> / 2 MB; |
|
2726 // metadataSize = totalNumOfSongsCanFit * 3000B |
|
2727 // estimatedDBSize = metadataSize + <orig DB size>; |
|
2728 // dummyDBSize = MIN (iMaximumAllowedRAMDiskSpaceToCopy , estimatedDBSize ) |
|
2729 |
|
2730 //b) Harvesting case |
|
2731 //------------------- |
|
2732 // totalNumOfSongsCanFit = <disk total size>/ 2 MB |
|
2733 // metadataSize = totalNumOfSongsCanFit * 3000B |
|
2734 // estimatedSize = metadataSize+ <orig DB size> |
|
2735 // freeDiskSpace = <disk free space> - 1 MB |
|
2736 // dummyDBSize = MIN (freeDiskSpace, iMaximumAllowedRAMDiskSpaceToCopy , estimatedSize); |
|
2737 // |
|
2738 // --------------------------------------------------------------------------- |
|
2739 // |
|
2740 TInt64 CMPXDbManager::CalculateInitalDummyDBSize( const TVolumeInfo& aVol, TInt aOrigDbSize, TBool aIsMTPInUse ) |
|
2741 { |
|
2742 #ifdef __RAMDISK_PERF_ENABLE |
|
2743 MPX_DEBUG1("-->CMPXDbManager::CalculateInitalDummyDBSize"); |
|
2744 |
|
2745 if ( aIsMTPInUse ) |
|
2746 { |
|
2747 TInt64 totalNumOfSongsCanFit = aVol.iFree / KMPEstimatedSongInBytes; |
|
2748 MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize aVol.iFree=%Lu", aVol.iFree ); |
|
2749 MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize totalNumOfSongsCanFit=%Lu", totalNumOfSongsCanFit ); |
|
2750 TInt64 estimatedSize = totalNumOfSongsCanFit * (TInt64) KMPEstimatedSizePerDBEntry + aOrigDbSize; |
|
2751 MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize (MTP case) estimated DB size from calculation=%Lu", estimatedSize ); |
|
2752 if ( estimatedSize > iMaximumAllowedRAMDiskSpaceToCopy ) |
|
2753 { |
|
2754 MPX_DEBUG2("<--CMPXDbManager::CalculateInitalDummyDBSize returned iMaximumAllowedRAMDiskSpaceToCopy %d", iMaximumAllowedRAMDiskSpaceToCopy); |
|
2755 return iMaximumAllowedRAMDiskSpaceToCopy; |
|
2756 } |
|
2757 else |
|
2758 { |
|
2759 MPX_DEBUG2("<--CMPXDbManager::CalculateInitalDummyDBSize returned %Lu", estimatedSize ); |
|
2760 return estimatedSize; |
|
2761 } |
|
2762 } |
|
2763 else |
|
2764 { |
|
2765 TInt64 totalNumOfSongsCanFit = aVol.iSize / KMPEstimatedSongInBytes; |
|
2766 TInt64 estimatedSize = totalNumOfSongsCanFit * (TInt64) KMPEstimatedSizePerDBEntry + aOrigDbSize; |
|
2767 MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize estimated DB size from calculation=%Lu", estimatedSize ); |
|
2768 if ( estimatedSize > iMaximumAllowedRAMDiskSpaceToCopy ) |
|
2769 { |
|
2770 MPX_DEBUG1("<--CMPXDbManager::CalculateInitalDummyDBSize"); |
|
2771 // If estimated size is larger than expected free RAM size, |
|
2772 // and if the RAM size is larger than free disk space, |
|
2773 // then use free disk space. 1*KMPMegaByte prevent MP to use up all diskspace |
|
2774 //return iMaximumAllowedRAMDiskSpaceToCopy > aVol.iFree - 1*KMPMegaByte |
|
2775 // ? aVol.iFree - 1*KMPMegaByte : iMaximumAllowedRAMDiskSpaceToCopy; |
|
2776 return iMaximumAllowedRAMDiskSpaceToCopy; |
|
2777 } |
|
2778 else |
|
2779 { |
|
2780 MPX_DEBUG1("<--CMPXDbManager::CalculateInitalDummyDBSize"); |
|
2781 // If estimated size is larger than disk free size, use free diskspace size, |
|
2782 //return estimatedSize > aVol.iFree - 1*KMPMegaByte |
|
2783 // ? aVol.iFree - 1*KMPMegaByte : estimatedSize; |
|
2784 return estimatedSize; |
|
2785 } |
|
2786 } |
|
2787 |
|
2788 #endif //__RAMDISK_PERF_ENABLE |
|
2789 } |
|
2790 |
|
2791 |
|
2792 // --------------------------------------------------------------------------- |
|
2793 // CMPXDbManager::GetDatabaseIndex |
|
2794 // --------------------------------------------------------------------------- |
|
2795 // |
|
2796 TInt CMPXDbManager::GetDatabaseIndex(TInt aDrive) |
|
2797 { |
|
2798 #ifdef __RAMDISK_PERF_ENABLE |
|
2799 MPX_DEBUG2("-->CMPXDbManager::GetDatabaseIndex %d", aDrive ); |
|
2800 TInt count(iDatabaseHandles.Count()); |
|
2801 for (TInt i = 0; i < count; ++i) |
|
2802 { |
|
2803 if ( iDatabaseHandles[i].iDrive == aDrive ) |
|
2804 { |
|
2805 return i; |
|
2806 } |
|
2807 } |
|
2808 #endif //__RAMDISK_PERF_ENABLE |
|
2809 MPX_DEBUG1("<--CMPXDbManager::GetDatabaseIndex returned -1"); |
|
2810 return -1; |
|
2811 } |
|
2812 |
|
2813 |
|
2814 // --------------------------------------------------------------------------- |
|
2815 // CMPXDbManager::EnsureRamSpaceL |
|
2816 // --------------------------------------------------------------------------- |
|
2817 // |
|
2818 EXPORT_C void CMPXDbManager::EnsureRamSpaceL() |
|
2819 { |
|
2820 #ifdef __RAMDISK_PERF_ENABLE |
|
2821 MPX_DEBUG1("-->CMPXDbManager::EnsureRamSpaceL"); |
|
2822 |
|
2823 if ( iRAMInUse ) |
|
2824 { |
|
2825 TVolumeInfo vol; |
|
2826 TInt driveIndex; |
|
2827 RFs::CharToDrive( iRAMDrive, driveIndex ); |
|
2828 |
|
2829 TInt errRAM = iFs.Volume( vol, driveIndex ); |
|
2830 if ( errRAM == KErrNone && vol.iFree < KMPMinimumRAMSizeToRun ) |
|
2831 { |
|
2832 // RAM not enough, copy back to normal drive and continue to harvest. |
|
2833 MPX_DEBUG1("CMPXDbManager::EnsureRamSpaceL RAM diskspace is full, copy dbs back"); |
|
2834 CopyDBsFromRamL(); |
|
2835 } |
|
2836 else |
|
2837 { |
|
2838 TInt size=0; |
|
2839 TInt err = GetTotalRamDatabasesSizeL(size); |
|
2840 if ( err || (size > iMaximumAllowedRAMDiskSpaceToCopy) ) |
|
2841 { |
|
2842 // Databases using too much RAM space, copy back to normal drive and continue to harvest. |
|
2843 if ( err ) |
|
2844 { |
|
2845 MPX_DEBUG2("CMPXDbManager::EnsureRamSpaceL Get DBs Size Err = %d, copy dbs back", err); |
|
2846 } |
|
2847 else |
|
2848 { |
|
2849 MPX_DEBUG2("CMPXDbManager::EnsureRamSpaceL DBs using too much RAM space size = %d, copy dbs back", size); |
|
2850 } |
|
2851 CopyDBsFromRamL(); |
|
2852 } |
|
2853 } |
|
2854 } |
|
2855 MPX_DEBUG1("<--CMPXDbManager::EnsureRamSpaceL"); |
|
2856 #endif //__RAMDISK_PERF_ENABLE |
|
2857 } |
|
2858 |
|
2859 |
|
2860 // --------------------------------------------------------------------------- |
|
2861 // CMPXDbManager::EnsureDiskSpaceL |
|
2862 // --------------------------------------------------------------------------- |
|
2863 // |
|
2864 void CMPXDbManager::EnsureDiskSpaceL(TInt aDrive) |
|
2865 { |
|
2866 MPX_DEBUG2("-->CMPXDbManager::EnsureDiskSpaceL for drive %d", aDrive); |
|
2867 // handle the case of C drive |
|
2868 TDriveUnit drive(aDrive); |
|
2869 TDriveUnit cdrive(KRootDrive()); |
|
2870 |
|
2871 if(drive == cdrive) |
|
2872 { |
|
2873 if (SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, 0, aDrive)) |
|
2874 { |
|
2875 MPX_DEBUG1("CMPXDbManager::EnsureDiskSpaceL Error diskspace full"); |
|
2876 User::Leave(KErrDiskFull); |
|
2877 } |
|
2878 |
|
2879 return; |
|
2880 } |
|
2881 |
|
2882 // handle other drives (eg. removable EDriveE) |
|
2883 TInt count(iDatabaseHandles.Count()); |
|
2884 for (TInt i = 0; i < count; ++i) |
|
2885 { |
|
2886 DatabaseHandle& database = iDatabaseHandles[i]; |
|
2887 if (((KDbManagerAllDrives == aDrive) || |
|
2888 (aDrive == database.iDrive)) && |
|
2889 database.iOpen |
|
2890 #ifdef __RAMDISK_PERF_ENABLE |
|
2891 && !database.iUseRAMdb |
|
2892 #endif |
|
2893 ) |
|
2894 { |
|
2895 if (SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, 0, |
|
2896 database.iDrive)) |
|
2897 { |
|
2898 MPX_DEBUG1("CMPXDbManager::EnsureDiskSpaceL Error diskspace full"); |
|
2899 User::Leave(KErrDiskFull); |
|
2900 } |
|
2901 } |
|
2902 |
|
2903 if (aDrive == database.iDrive) |
|
2904 { |
|
2905 // exit if just one drive to check |
|
2906 break; |
|
2907 } |
|
2908 } |
|
2909 MPX_DEBUG1("<--CMPXDbManager::EnsureDiskSpaceL"); |
|
2910 } |
|
2911 |
|
2912 |
|
2913 // --------------------------------------------------------------------------- |
|
2914 // CMPXDbManager::DoBackupDBs |
|
2915 // --------------------------------------------------------------------------- |
|
2916 // |
|
2917 /*EXPORT_C void CMPXDbManager::BackupDBsL() |
|
2918 { |
|
2919 #ifdef __RAMDISK_PERF_ENABLE |
|
2920 MPX_DEBUG1("-->CMPXDbManager::BackupDBsL"); |
|
2921 |
|
2922 TInt transactionCount = iTransactionCount; |
|
2923 if (iTransactionCount > 0) |
|
2924 { |
|
2925 iTransactionCount = 0; |
|
2926 DoCommitL(); |
|
2927 } |
|
2928 |
|
2929 TInt count(iDatabaseHandles.Count()); |
|
2930 for (TInt i = 0; i < count && iDatabaseHandles[i].iUseRAMdb ; ++i) |
|
2931 { |
|
2932 CloseDatabaseAtIndexL( i ); |
|
2933 |
|
2934 TInt err= BaflUtils::CopyFile(iFs, |
|
2935 iDatabaseHandles[i].iTargetFullFilePath->Des(), |
|
2936 iDatabaseHandles[i].iOrigFullFilePath->Des()); |
|
2937 |
|
2938 MPX_DEBUG2("CMPXDbManager::BackupDBsL err = %d", err); |
|
2939 |
|
2940 OpenDatabaseAtIndexL( i ); |
|
2941 } |
|
2942 |
|
2943 if (transactionCount > 0) |
|
2944 { |
|
2945 DoBeginL(); |
|
2946 iTransactionCount = transactionCount; |
|
2947 } |
|
2948 MPX_DEBUG1("<--CMPXDbManager::BackupDBsL"); |
|
2949 #endif //__RAMDISK_PERF_ENABLE |
|
2950 }*/ |
|
2951 |
|
2952 // --------------------------------------------------------------------------- |
|
2953 // CMPXDbManager::GetTotalDatabasesSize |
|
2954 // --------------------------------------------------------------------------- |
|
2955 // |
|
2956 TInt CMPXDbManager::GetTotalDatabasesSize(TInt& aSize) |
|
2957 { |
|
2958 MPX_FUNC("CMPXDbManager::GetTotalDatabasesSize"); |
|
2959 TInt err = KErrNotSupported; |
|
2960 #ifdef __RAMDISK_PERF_ENABLE |
|
2961 TInt size=0; |
|
2962 TInt count(iDatabaseHandles.Count()); |
|
2963 err = KErrNone; |
|
2964 for ( TInt i = 0; i < count ; ++i ) |
|
2965 { |
|
2966 // Generate database name. |
|
2967 TFileName dbFilename; |
|
2968 TDriveUnit drive(iDatabaseHandles[i].iDrive); |
|
2969 dbFilename.Append(drive.Name()); |
|
2970 dbFilename.Append(KDBFilePath); |
|
2971 TFileName filename; |
|
2972 filename.Format(KSecurePath, User::Identity().iUid, iDbFile); //x:\private\10281e17\[sldfdsf]mpxv2_5.db |
|
2973 dbFilename.Append(filename); |
|
2974 MPX_DEBUG2("CMPXDbManager::GetTotalDatabasesSize - Database name = %S", &dbFilename); |
|
2975 TEntry entry; |
|
2976 err = iFs.Entry( dbFilename, entry ); |
|
2977 if (err == KErrNotFound || err == KErrNotReady ) |
|
2978 { |
|
2979 MPX_DEBUG3("CMPXDbManager::GetTotalDatabasesSize - Ignored %S, error = %d", &dbFilename, err); |
|
2980 err = KErrNone; |
|
2981 continue; |
|
2982 } |
|
2983 if ( err != KErrNone ) |
|
2984 { |
|
2985 break; |
|
2986 } |
|
2987 MPX_DEBUG3("CMPXDbManager::GetTotalDatabasesSize - Size of Db %S = %d", &dbFilename, entry.iSize); |
|
2988 // sum up size |
|
2989 size += entry.iSize; |
|
2990 } |
|
2991 aSize = size; |
|
2992 MPX_DEBUG2("CMPXDbManager::GetTotalDatabasesSize - Total Size of Dbs = %d", size); |
|
2993 |
|
2994 #endif //__RAMDISK_PERF_ENABLE |
|
2995 MPX_DEBUG2("CMPXDbManager::GetTotalDatabasesSize - Return err = %d", err); |
|
2996 return err; |
|
2997 } |
|
2998 |
|
2999 // --------------------------------------------------------------------------- |
|
3000 // CMPXDbManager::GetTotalRamDatabasesSize |
|
3001 // --------------------------------------------------------------------------- |
|
3002 // |
|
3003 TInt CMPXDbManager::GetTotalRamDatabasesSizeL(TInt& aSize) |
|
3004 { |
|
3005 MPX_FUNC("CMPXDbManager::GetTotalRamDatabasesSize"); |
|
3006 TInt err = KErrNotSupported; |
|
3007 #ifdef __RAMDISK_PERF_ENABLE |
|
3008 TInt size=0; |
|
3009 TInt count(iDatabaseHandles.Count()); |
|
3010 err = KErrNone; |
|
3011 for ( TInt i = 0; i < count ; ++i ) |
|
3012 { |
|
3013 // make sure this db is in ram drive. |
|
3014 if ( !iDatabaseHandles[i].iUseRAMdb ) |
|
3015 { |
|
3016 continue; |
|
3017 } |
|
3018 // Generate database name. |
|
3019 TFileName dbFilename; |
|
3020 dbFilename.Append(iRAMFolder); |
|
3021 TBuf<2> d; |
|
3022 TDriveUnit drive(iDatabaseHandles[i].iDrive); |
|
3023 d.Append(drive.Name()); |
|
3024 HBufC* temp = HBufC::NewLC(KMaxFileName); |
|
3025 temp->Des().Append(d.Left(1)); |
|
3026 temp->Des().Append(iDbFile->Des()); |
|
3027 TFileName filename; |
|
3028 filename.Format(KSecurePath, User::Identity().iUid, temp); |
|
3029 CleanupStack::PopAndDestroy(temp); |
|
3030 dbFilename.Append(filename); |
|
3031 MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSizeL - Database name = %S", &dbFilename); |
|
3032 TEntry entry; |
|
3033 err = iFs.Entry( dbFilename, entry ); |
|
3034 if ( (err != KErrNone) && (err != KErrNotFound) ) |
|
3035 { |
|
3036 break; |
|
3037 } |
|
3038 MPX_DEBUG3("CMPXDbManager::GetTotalRamDatabasesSizeL - Size of Db %S = %d", &dbFilename, entry.iSize); |
|
3039 // sum up size |
|
3040 size += entry.iSize; |
|
3041 } |
|
3042 aSize = size; |
|
3043 MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSizeL - Total Size of Dbs = %d", size); |
|
3044 #endif //__RAMDISK_PERF_ENABLE |
|
3045 MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSizeL - Return err = %d", err); |
|
3046 return err; |
|
3047 } |
|
3048 |
|
3049 // ---------------------------------------------------------------------------- |
|
3050 // Creates the absolute database filename on a specified drive. |
|
3051 // ---------------------------------------------------------------------------- |
|
3052 // |
|
3053 HBufC* CMPXDbManager::CreateFullFilenameL(TDriveUnit aDrive) |
|
3054 { |
|
3055 MPX_FUNC("CMPXDbManager::CreateFullFilenameL"); |
|
3056 |
|
3057 HBufC* filename = HBufC::NewL(KMaxFileName); |
|
3058 const TDesC& securefilePath = KSecureFilePath; |
|
3059 TDriveUnit cdrive(KRootDrive()); |
|
3060 |
|
3061 #ifdef __RAMDISK_PERF_ENABLE |
|
3062 TInt index(GetDatabaseIndex((TInt)aDrive)); |
|
3063 if ( index >=0 && iDatabaseHandles[index].iUseRAMdb && aDrive != cdrive ) |
|
3064 { |
|
3065 MPX_DEBUG1("CMPXDbManager::CreateFullFilenameL - use RAMDisk"); |
|
3066 TFileName path; |
|
3067 path.Append(iRAMDrive); |
|
3068 path.Append(_L(":")); |
|
3069 path.Append(KDBFilePath); |
|
3070 TBuf<2> d; |
|
3071 d.Append(aDrive.Name()); |
|
3072 TFileName temp; |
|
3073 temp.Append(d.Left(1)); // attach original drive name |
|
3074 temp.Append(iDbFile->Des()); |
|
3075 filename->Des().Format(securefilePath, &path, User::Identity().iUid, &temp); |
|
3076 MPX_DEBUG3("CMPXDbManager::CreateFullFilenameL - path=%S filename=%S", &path, filename); |
|
3077 } |
|
3078 else |
|
3079 #endif //__RAMDISK_PERF_ENABLE |
|
3080 { |
|
3081 MPX_DEBUG1("CMPXDbManager::CreateFullFilenameL - use normal drive"); |
|
3082 TFileName dbPath; |
|
3083 dbPath.Append(aDrive.Name()); |
|
3084 dbPath.Append(KDBFilePath); |
|
3085 filename->Des().Format(securefilePath, &dbPath, User::Identity().iUid, iDbFile); |
|
3086 } |
|
3087 |
|
3088 MPX_DEBUG2("CMPXDbManager::CreateFullFilenameL filename = %S", filename); |
|
3089 return filename; |
|
3090 } |
|
3091 |
|
3092 // End of File |