|
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 of music collection |
|
15 * databases. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #ifndef MPXDBMANAGER_H |
|
21 #define MPXDBMANAGER_H |
|
22 |
|
23 // INCLUDES |
|
24 |
|
25 #include <e32base.h> |
|
26 #include <f32file.h> |
|
27 #include <sqldb.h> |
|
28 #include <badesca.h> |
|
29 |
|
30 // FORWARD DECLARATIONS |
|
31 |
|
32 class MMPXTable; |
|
33 class RSqlStatement; |
|
34 |
|
35 // CONSTANTS |
|
36 |
|
37 const TInt KDbManagerAllDrives = 0; |
|
38 |
|
39 // CLASS DECLARATION |
|
40 |
|
41 /** |
|
42 * Generic class responsible for managing databases on multiple drives. |
|
43 * |
|
44 * @lib MPXDbPlugin.lib |
|
45 */ |
|
46 class CMPXDbManager : |
|
47 public CBase |
|
48 { |
|
49 protected: // Constructors |
|
50 |
|
51 /** |
|
52 * C++ default constructor |
|
53 * @param aFs file server session |
|
54 */ |
|
55 IMPORT_C CMPXDbManager(RFs& aFs); |
|
56 |
|
57 /** |
|
58 * The second phase constructor to safely construct things |
|
59 * that can leave |
|
60 * @param aDatabaseFile database filename |
|
61 */ |
|
62 IMPORT_C void ConstructL(const TFileName& aDatabaseFile); |
|
63 |
|
64 public: |
|
65 |
|
66 /** |
|
67 * Destructor |
|
68 */ |
|
69 IMPORT_C virtual ~CMPXDbManager(); |
|
70 |
|
71 public: // New methods |
|
72 |
|
73 /** |
|
74 * Checks if all databases have been initialized. |
|
75 * @return ETrue if initialized, EFalse otherwise |
|
76 */ |
|
77 IMPORT_C TBool IsInitialized(); |
|
78 |
|
79 /** |
|
80 * Begins a transaction on all databases. |
|
81 */ |
|
82 IMPORT_C void BeginL(); |
|
83 |
|
84 /** |
|
85 * Commits a transaction on all databases. |
|
86 */ |
|
87 IMPORT_C void CommitL(); |
|
88 |
|
89 /** |
|
90 * Rolls back a transaction on all databases. |
|
91 */ |
|
92 IMPORT_C void RollbackL(); |
|
93 |
|
94 /** |
|
95 * Checks if the database is currently in a transaction |
|
96 * |
|
97 * @return ETrue if database is currently in a transaction, EFalse otherwise |
|
98 */ |
|
99 IMPORT_C TBool InTransaction(); |
|
100 |
|
101 /** |
|
102 * Tries to create and open the databases on all specified drives. |
|
103 * If a drive is not valid (like an MC not plugged in) it will be skipped. |
|
104 * @param aDrives array of drives to create and open databases on. |
|
105 */ |
|
106 IMPORT_C void InitDatabasesL(RArray<TInt> aDrives); |
|
107 |
|
108 /** |
|
109 * Opens a specified database. |
|
110 * @param aDrive identifies the drive index of the database to open |
|
111 */ |
|
112 IMPORT_C void OpenDatabaseL(TInt aDrive); |
|
113 |
|
114 /** |
|
115 * Closes a specified database. |
|
116 * @param aDrive identifies the drive index of the database to close |
|
117 */ |
|
118 IMPORT_C void CloseDatabaseL(TInt aDrive); |
|
119 |
|
120 /** |
|
121 * Closes all open databases. |
|
122 */ |
|
123 IMPORT_C void CloseAllDatabases(); |
|
124 |
|
125 /** |
|
126 * Opens all open databases. |
|
127 */ |
|
128 IMPORT_C void OpenAllDatabasesL(); |
|
129 |
|
130 /** |
|
131 * Checks if the database on a specified drive is open. |
|
132 * @param aDrive identifies the drive index of the database to check |
|
133 * @return ETrue if the database is open, EFalse otherwise |
|
134 */ |
|
135 IMPORT_C TBool IsOpen(TInt aDrive) const; |
|
136 |
|
137 /** |
|
138 * Returns the number of currently open databases. |
|
139 * @return Number of databases |
|
140 */ |
|
141 IMPORT_C TInt DatabaseCount() const; |
|
142 |
|
143 /** |
|
144 * Returns the drive corresponding to a give index. |
|
145 * @param aIndex identifies the index in the list of drives the database uses |
|
146 * @return Drive index the database uses. |
|
147 */ |
|
148 IMPORT_C TInt DbDrive(TInt aIndex) const; |
|
149 |
|
150 /** |
|
151 * Recreate a specified database. |
|
152 * @param aDrive identifies the drive index ro recreate the database |
|
153 */ |
|
154 IMPORT_C void RecreateDatabaseL(TInt aDrive); |
|
155 |
|
156 /** |
|
157 * Recreate all databases. |
|
158 */ |
|
159 IMPORT_C void RecreateAllDatabasesL(); |
|
160 |
|
161 /** |
|
162 * Return current DB version |
|
163 * @return the version of db structure |
|
164 */ |
|
165 IMPORT_C TVersion Version() const; |
|
166 |
|
167 /** |
|
168 * Return current DB version |
|
169 * @return the version of db structure |
|
170 */ |
|
171 IMPORT_C void RegisterTableL(MMPXTable& aTable); |
|
172 |
|
173 /** |
|
174 * Executes a select query with variable number of parameters on all |
|
175 * available drives. |
|
176 * @param aFmt query format string |
|
177 * @return resulted result set |
|
178 */ |
|
179 IMPORT_C RSqlStatement ExecuteSelectQueryL(TRefByValue<const TDesC> aFmt, ...); |
|
180 |
|
181 /** |
|
182 * Executes a select query with variable number of parameters |
|
183 * against a specified drive |
|
184 * @param aDrive to execute query on |
|
185 * @param aFmt query format string |
|
186 * @return resulted result set |
|
187 */ |
|
188 IMPORT_C RSqlStatement ExecuteSelectQueryL(TInt aDrive, TRefByValue<const TDesC> aFmt, |
|
189 ...); |
|
190 |
|
191 /** |
|
192 * Executes a select query with variable number of parameters |
|
193 * on all available drives. Two values will be bound to the statement |
|
194 * @param aStatementId unique id of a statement to bind to |
|
195 * creates a new statement if the id is not found |
|
196 * @param aFirstValue TInt Value to bind as first argument |
|
197 * @param aSecondValue TInt Value to bind as second argument |
|
198 * @param aFmt query format string |
|
199 * @return resulted result set. The ownership is not passed so the caller should |
|
200 * not destroy this as it may be reused in future calls. |
|
201 */ |
|
202 IMPORT_C RSqlStatement& ExecuteSelectQueryL( TUint aStatementId, |
|
203 TInt aFirstValue, TInt aSecondValue, |
|
204 TRefByValue<const TDesC> aFmt, ...); |
|
205 |
|
206 /** |
|
207 * Executes a select query with variable number of parameters |
|
208 * on all available drives. Two values will be bound to the statement |
|
209 * @param aStatementId unique id of a statement to bind to |
|
210 * creates a new statement if the id is not found |
|
211 * @param aFirstValue TDesC Value to bind as first argument |
|
212 * @param aSecondValue TInt Value to bind as second argument |
|
213 * @param aFmt query format string |
|
214 * @return resulted result set. The ownership is not passed so the caller should |
|
215 * not destroy this as it may be reused in future calls. |
|
216 */ |
|
217 IMPORT_C RSqlStatement& ExecuteSelectQueryL( TUint aStatementId, |
|
218 const TDesC& aFirstValue, |
|
219 TInt aSecondValue, |
|
220 TRefByValue<const TDesC> aFmt, ...); |
|
221 /** |
|
222 * Executes a query that does not return a record set |
|
223 * @param aDrive to execute query on |
|
224 * @param aFmt query format string |
|
225 */ |
|
226 IMPORT_C void ExecuteQueryL(TInt aDrive, TRefByValue<const TDesC> aFmt, ...); |
|
227 |
|
228 /** |
|
229 * Prints the contents of all the databases to the log file |
|
230 * @param aDrive to execute query on |
|
231 */ |
|
232 IMPORT_C void PrintDatabaseL(); |
|
233 |
|
234 /** |
|
235 * Returns the file server session. |
|
236 * @return file server session |
|
237 */ |
|
238 IMPORT_C RFs& Fs(); |
|
239 |
|
240 /** |
|
241 * Checks if the space on the specified drive(s) is above the critical level |
|
242 * @param aDrive drive ID or KDbManagerAllDrives |
|
243 * @leave KErrDiskFull if the space on the specified drive(s) is below the |
|
244 * critical level |
|
245 */ |
|
246 IMPORT_C void CheckDiskSpaceL(TInt aDrive); |
|
247 |
|
248 /** |
|
249 * Completely recreate all databases. |
|
250 */ |
|
251 IMPORT_C void RegenerateAllDatabasesL(); |
|
252 |
|
253 /** |
|
254 * Checks if the spefified drive is a remove drive |
|
255 */ |
|
256 IMPORT_C TBool IsRemoteDrive(TDriveNumber aDrive); |
|
257 |
|
258 protected: |
|
259 |
|
260 /** |
|
261 * Creates a database on a specified drive. |
|
262 * @param aDrive identifies the database |
|
263 */ |
|
264 IMPORT_C void CreateDatabaseL(TInt aDrive); |
|
265 |
|
266 /** |
|
267 * Remove the database |
|
268 * @param aDrive identifies the database |
|
269 */ |
|
270 IMPORT_C void RemoveDatabaseL(TInt aDrive); |
|
271 |
|
272 /** |
|
273 * Create the all tables |
|
274 * @param aDatabase the database to add tables to |
|
275 */ |
|
276 IMPORT_C void CreateTablesL(RSqlDatabase& aDatabase); |
|
277 |
|
278 /** |
|
279 * Drop all tables |
|
280 * @param aDatabase the database to drop tables from |
|
281 */ |
|
282 IMPORT_C void DropTablesL(RSqlDatabase& aDatabase); |
|
283 |
|
284 public: |
|
285 |
|
286 /** |
|
287 * Executes a select query with variable number of parameters |
|
288 * against a specified drive |
|
289 * @param aDrive to execute query on |
|
290 * @param aFmt query format string |
|
291 * @return resulted result set |
|
292 */ |
|
293 IMPORT_C RSqlStatement ExecuteSelectQueryOnAllDrivesL(TInt aDrive, TRefByValue<const TDesC> aFmt, |
|
294 ...); |
|
295 |
|
296 /** |
|
297 * Copy all databases from RAM disk back to normal drive, E, F,... |
|
298 * |
|
299 * @return none |
|
300 */ |
|
301 IMPORT_C void CopyDBsFromRamL(); |
|
302 |
|
303 /** |
|
304 * Copy all databases to RAM disk back from normal drive, E, F,... |
|
305 * |
|
306 * @return none |
|
307 */ |
|
308 IMPORT_C void CopyDBsToRamL( TBool aIsMTPInUse = EFalse); |
|
309 |
|
310 |
|
311 /** |
|
312 * Check if RAM disk is enough to operatte. If not, DBs will be copied back to drives. |
|
313 * |
|
314 * @return TInt index to the database handler |
|
315 */ |
|
316 IMPORT_C void EnsureRamSpaceL() ; |
|
317 |
|
318 /** |
|
319 * Move DBs from RAMDisk to disks |
|
320 */ |
|
321 //IMPORT_C void BackupDBsL(); |
|
322 |
|
323 private: |
|
324 |
|
325 /** |
|
326 * Find available RAMDISK |
|
327 * @return error code |
|
328 */ |
|
329 TInt GetRAMDiskPath(); |
|
330 |
|
331 /** |
|
332 * Check if RAM disk is available to copy. |
|
333 * |
|
334 * @return ETrue if there is enough space, EFalse otherwise |
|
335 */ |
|
336 TBool IsRamDiskSpaceAvailable(); |
|
337 |
|
338 /** |
|
339 * To block a diskspace so that it can gurantee for a write back from RAM disk |
|
340 * |
|
341 * @return ETrue if the dummy file is created successfully, EFalse otherwise |
|
342 */ |
|
343 TBool BlockDiskSpace( TInt aIndex, TBool aIsMTPInUse = EFalse ); |
|
344 |
|
345 /** |
|
346 * To copy db from regular drive to RAM |
|
347 * |
|
348 * @return ETrue if succeed |
|
349 * |
|
350 * No-operation if fails |
|
351 */ |
|
352 TBool DoCopyDBToRam( TInt aIndex, TBool aIsMTPInUse ); |
|
353 |
|
354 /** |
|
355 * To copy db back regular drive from RAM |
|
356 * |
|
357 */ |
|
358 void DoCopyDBFromRam( TInt aIndex ); |
|
359 |
|
360 /** |
|
361 * To replace dummy file with new content |
|
362 * Writes over previous dummy file without freeing disk space |
|
363 */ |
|
364 void ReplaceFileL( const TDesC& aSrcName, const TDesC& aDstName ); |
|
365 |
|
366 /** |
|
367 * To calculate necessary file size of the dummy file |
|
368 * |
|
369 * @return TInt64 estimated file size |
|
370 */ |
|
371 TInt64 CalculateInitalDummyDBSize( const TVolumeInfo& aVol, TInt aOrigDbSize, TBool aIsMTPInUse = EFalse); |
|
372 |
|
373 /** |
|
374 * Get database index by giving drive index |
|
375 * |
|
376 * @return TInt index to the database handler |
|
377 */ |
|
378 TInt GetDatabaseIndex(TInt aDrive); |
|
379 |
|
380 /** |
|
381 * Sum up the total size in bytes of the databases. |
|
382 * |
|
383 * @param aSize - On return, the total size of the databases. |
|
384 * @return TInt System error. |
|
385 */ |
|
386 TInt GetTotalDatabasesSize(TInt& aSize); |
|
387 |
|
388 /** |
|
389 * Sum up the total size in bytes of the databases on the RAM drive. |
|
390 * |
|
391 * @param aSize - On return, the total size of the databases on the RAM drive. |
|
392 * @return TInt System error. |
|
393 */ |
|
394 TInt GetTotalRamDatabasesSizeL(TInt& aSize); |
|
395 |
|
396 /** |
|
397 * Remove dummy file |
|
398 * |
|
399 * @return TInt index to the database handler |
|
400 */ |
|
401 void RemoveDummyFile( TInt index ); |
|
402 |
|
403 /** |
|
404 * Check if disksapce is enough to operatte. If not, it leaves with KErrDiskFull |
|
405 * |
|
406 */ |
|
407 void EnsureDiskSpaceL(TInt aDrive) ; |
|
408 |
|
409 /** |
|
410 * Begin transaction, leaves on error |
|
411 */ |
|
412 void DoBeginL(); |
|
413 |
|
414 /** |
|
415 * Commit transaction, leaves on error |
|
416 */ |
|
417 void DoCommitL(); |
|
418 |
|
419 /** |
|
420 * Create full path and filename on a specified drive unit. |
|
421 * @param aDrive identifies the drive unit |
|
422 */ |
|
423 HBufC* CreateFullFilenameL(TDriveUnit aDrive); |
|
424 |
|
425 protected: // Types |
|
426 |
|
427 typedef struct |
|
428 { |
|
429 TInt iDrive; |
|
430 TBool iOpen; |
|
431 HBufC* iAliasname; |
|
432 #ifdef __RAMDISK_PERF_ENABLE |
|
433 TBool iUseRAMdb; |
|
434 HBufC* iOrigFullFilePath; |
|
435 HBufC* iTargetFullFilePath; |
|
436 TFileName iDummyFilePath; |
|
437 #endif //__RAMDISK_PERF_ENABLE |
|
438 } DatabaseHandle; |
|
439 |
|
440 protected: // Data |
|
441 |
|
442 HBufC* iDbFile; |
|
443 RArray<MMPXTable*> iTables; |
|
444 RSqlDatabase iDatabase; |
|
445 RArray<DatabaseHandle> iDatabaseHandles; |
|
446 |
|
447 private: |
|
448 |
|
449 /** |
|
450 * Create the all tables |
|
451 * @param aDatabase the database to add tables to |
|
452 * @param aCorrupt specifies whether the database is corrupted or not |
|
453 */ |
|
454 void CreateTablesL(RSqlDatabase& aDatabase, TBool aCorrupt); |
|
455 |
|
456 /** |
|
457 * Opens root database at C-drive. |
|
458 */ |
|
459 void OpenRootDatabaseL(); |
|
460 |
|
461 /** |
|
462 * Creates a specified database. |
|
463 * @param aDrive identifies the drive unit of the database to create |
|
464 */ |
|
465 void CreateDatabaseL(TDriveUnit aDrive); |
|
466 |
|
467 /** |
|
468 * Attached a specified database. |
|
469 * @param aIndex Index to iDatabaseHandles |
|
470 */ |
|
471 void AttachDatabaseL(TInt aIndex); |
|
472 |
|
473 /** |
|
474 * Detach a specified database. |
|
475 * @param aIndex Index to iDatabaseHandles |
|
476 */ |
|
477 void DetachDatabaseL(TInt aIndex); |
|
478 |
|
479 /** |
|
480 * Open database |
|
481 * @param aIndex Index to iDatabaseHandles |
|
482 */ |
|
483 void OpenDatabaseAtIndexL( TInt aIndex ); |
|
484 |
|
485 /** |
|
486 * Close database |
|
487 * @param aIndex Index to iDatabaseHandles |
|
488 */ |
|
489 void CloseDatabaseAtIndexL(TInt aIndex); |
|
490 |
|
491 /** |
|
492 * Create filename on a specified drive unit. |
|
493 * @param aDrive identifies the drive unit |
|
494 */ |
|
495 HBufC* CreateFilenameL(TDriveUnit aDrive); |
|
496 |
|
497 |
|
498 /** |
|
499 * Replaces specially formatted query symbols with a specified alias. |
|
500 * @param aAlias identifies the alias name to use in query |
|
501 */ |
|
502 void ReplaceDriveAlias(TDes& aQuery, const TDesC& aAlias); |
|
503 |
|
504 /** |
|
505 * Replaces specially formatted query symbols with a specified alias. |
|
506 * @param aAlias identifies the alias name to use in query |
|
507 * @param aToKen replace by aAlias |
|
508 */ |
|
509 void ReplaceDriveAlias(TDes& aQuery, const TDesC& aAlias, const TDesC& aToKen); |
|
510 |
|
511 /** |
|
512 * Removes specially formatted query symbols from the query |
|
513 * @param aQuery identifies the query to remove specially formatted symbols |
|
514 */ |
|
515 void RemoveDriveAlias(TDes& aQuery); |
|
516 |
|
517 /** |
|
518 * Removes specially formatted query symbols from the query |
|
519 * @param aQuery identifies the query to remove specially formatted symbols |
|
520 * @param aToKen identifies need remove string |
|
521 */ |
|
522 void RemoveDriveAlias(TDes& aQuery, const TDesC& aToKen); |
|
523 |
|
524 /** |
|
525 * Attempts to recreate database by dropping and creating tables |
|
526 * used inside RecreateDatabaseL |
|
527 * @param aFilename database filename |
|
528 */ |
|
529 void DoRecreateDatabaseL(HBufC * aFilename); |
|
530 |
|
531 /** |
|
532 * Executes SQL statement against the database. |
|
533 * |
|
534 * @param aDatabase database handle |
|
535 * @param aStatement sql statement buffer |
|
536 * @return KErrNone if successfull, error code otherwise. |
|
537 * |
|
538 */ |
|
539 TInt ExecuteSqlStatement(RSqlDatabase& aDatabase, const TDesC& aStatement); |
|
540 |
|
541 /** |
|
542 * Formats the query. |
|
543 * @param aFmt query format |
|
544 * @param aList variable parameter list |
|
545 * @return formatted query, the ownership is transferred. |
|
546 */ |
|
547 HBufC* FormatQueryLC(TRefByValue<const TDesC> aFmt, VA_LIST aList); |
|
548 |
|
549 /** |
|
550 * Executes an SQL query on all drives |
|
551 * @param aQuery query string |
|
552 * @return prepared SQL statement |
|
553 */ |
|
554 RSqlStatement ExecuteSelectQueryOnAllDrivesL(TPtr aQuery); |
|
555 |
|
556 /** |
|
557 * Executes an SQL query on all drives |
|
558 * @param aDrive drive to execute the query on |
|
559 * @param aQuery query string |
|
560 * @return prepared SQL statement |
|
561 */ |
|
562 RSqlStatement ExecuteSelectQueryOnAllDrivesL(TInt aDrive,TPtr aQuery); |
|
563 |
|
564 /** |
|
565 * Executes an SQL query on a specified drive |
|
566 * @param aDrive drive to execute the query on |
|
567 * @param aQuery query string |
|
568 * @return prepared SQL statement |
|
569 */ |
|
570 RSqlStatement ExecuteSelectQueryOnDriveL(TInt aDrive, TPtr aQuery); |
|
571 |
|
572 /** |
|
573 * Prepares an sql query given the statment |
|
574 * @param aStatementId unique identifier of the statement |
|
575 * @param aFmt query format string |
|
576 * @param aList variable parameter list |
|
577 * @return reference to the SQL statement |
|
578 */ |
|
579 RSqlStatement& PrepareQueryL( TUint aStatementId, |
|
580 TRefByValue<const TDesC> aFmt, |
|
581 VA_LIST aList ); |
|
582 |
|
583 /** |
|
584 * Resets all prepared queries |
|
585 */ |
|
586 void ResetPreparedQueries(); |
|
587 |
|
588 /** |
|
589 * Create Database |
|
590 */ |
|
591 void DoCreateDatabaseL( TDriveUnit aDrive ); |
|
592 |
|
593 #ifdef _DEBUG |
|
594 |
|
595 /** |
|
596 * Returns the number of columns from a specified SQL statement |
|
597 * @param aStatement identifies the SQL query to check |
|
598 */ |
|
599 TInt GetColumnCountL(RSqlStatement& aStatement); |
|
600 |
|
601 /** |
|
602 * Prints the table of results from a specified SQL query to the debug log |
|
603 * @param aStatement identifies the SQL query made |
|
604 */ |
|
605 void PrintTableValuesL(RSqlStatement& aStatement); |
|
606 |
|
607 /** |
|
608 * Finds all the tables on the main or attached drives |
|
609 * @param aAlias identifies the alias name to use if a attached drive KNullDesC if main drive |
|
610 * @param aTableName identifies the array of table names on the main or attached drive |
|
611 */ |
|
612 void FindAllTablesL(const TDesC& aAlias, RArray<HBufC*>& aTableName); |
|
613 |
|
614 /** |
|
615 * Prints the table on the main or attached drives |
|
616 * @param aAlias identifies the alias name to use if a attached drive KNullDesC if main drive |
|
617 * @param aTableName identifies the table name on the main or attached drive |
|
618 */ |
|
619 void PrintTableL(const TDesC& aAlias, const TDesC& aTableName); |
|
620 |
|
621 #endif |
|
622 private: |
|
623 |
|
624 /* |
|
625 * Structure to hold the state of a sql statement |
|
626 */ |
|
627 NONSHARABLE_STRUCT( TSqlStatementState ) |
|
628 { |
|
629 TBool iPrepared; |
|
630 TUint iId; |
|
631 }; |
|
632 |
|
633 private: |
|
634 |
|
635 TBool iInitialized; |
|
636 TInt iTransactionCount; |
|
637 RFs& iFs; |
|
638 |
|
639 RArray<TSqlStatementState> iPreparedStatements; |
|
640 RPointerArray<RSqlStatement> iStatements; |
|
641 |
|
642 // Defined for RAM disk performance |
|
643 TBool iRAMDiskPerfEnabled; // flag to indicate RAM disk feature is enabled from cenrep. |
|
644 TUint64 iMaximumAllowedRAMDiskSpaceToCopy; // maximum number of megabytes allow to do RAM disk operation. |
|
645 TFileName iRAMFolder; |
|
646 TChar iRAMDrive; |
|
647 TBool iRAMInUse; |
|
648 TInt64 iEstimatedDBSizes; |
|
649 }; |
|
650 |
|
651 #endif // MPXDBMANAGER_H |