mtpfws/mtpfw/src/cmtppkgidstore.cpp
changeset 0 d0791faffa3f
child 47 63cf70d3ecd8
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "cmtppkgidstore.h"
       
    17 #include "dbutility.h"
       
    18 
       
    19 /**
       
    20 Two-phase construction
       
    21 @param aDatabase    The reference to the database object
       
    22 @return pointer to the created <pkgid, dpid> map instance
       
    23 */
       
    24 CMTPPkgIDStore* CMTPPkgIDStore::NewL(RDbDatabase& aDatabase)
       
    25     {
       
    26     CMTPPkgIDStore* self = new (ELeave) CMTPPkgIDStore(aDatabase);
       
    27     CleanupStack::PushL(self);
       
    28     self->ConstructL();
       
    29     CleanupStack::Pop(self);
       
    30     return self;
       
    31     }
       
    32 
       
    33 
       
    34 /**
       
    35 Standard c++ constructor
       
    36 */  
       
    37 CMTPPkgIDStore::CMTPPkgIDStore(RDbDatabase& aDatabase)
       
    38     :iDatabase(aDatabase)
       
    39     {
       
    40     }
       
    41 
       
    42 /**
       
    43 Second phase constructor.
       
    44 */    
       
    45 void CMTPPkgIDStore::ConstructL()
       
    46     {
       
    47     CreatePkgIDStoreTableL();
       
    48     }
       
    49 
       
    50 /**
       
    51 Create table to store the mapping from object handle to other properties 
       
    52 (data provider id, storage id, formatcode, etc.)
       
    53 */    
       
    54 void CMTPPkgIDStore::CreatePkgIDStoreTableL()
       
    55     {   
       
    56     _LIT(KSQLPkgIDTableName, "PkgIDStore");
       
    57     if (!DBUtility::IsTableExistsL(iDatabase, KSQLPkgIDTableName))
       
    58         {
       
    59         _LIT(KSQLCreatePkgIDTableText,
       
    60             "CREATE TABLE PkgIDStore (DataProviderId UNSIGNED INTEGER, PkgId UNSIGNED INTEGER)");
       
    61         User::LeaveIfError(iDatabase.Execute(KSQLCreatePkgIDTableText));            
       
    62         }
       
    63     _LIT(KSQLGetPKGID, "SELECT * FROM PkgIDStore");
       
    64     iSqlStatement.Format(KSQLGetPKGID);    
       
    65     RDbView view;
       
    66     CleanupClosePushL(view);
       
    67     User::LeaveIfError(view.Prepare(iDatabase, TDbQuery(iSqlStatement)));
       
    68     User::LeaveIfError(view.Evaluate());
       
    69     while (view.NextL())
       
    70         {
       
    71         view.GetL();
       
    72         iDPIDs.AppendL(view.ColInt64(EPkgIDStoreDataProviderId));
       
    73         iPkgIDs.AppendL(view.ColInt64(EPkgIDStorePKGID));
       
    74         }
       
    75     CleanupStack::PopAndDestroy(&view); 
       
    76     }
       
    77     
       
    78 /**
       
    79 Destructor.
       
    80 */    
       
    81 CMTPPkgIDStore::~CMTPPkgIDStore()
       
    82     {
       
    83     iDPIDs.Close();
       
    84     iPkgIDs.Close();
       
    85     }
       
    86 
       
    87 
       
    88 void CMTPPkgIDStore::InsertPkgIdL(TUint aDPId, TUint aPkgId)
       
    89     {       
       
    90     TInt index = 0;
       
    91     TRAPD(err, index = iDPIDs.FindL(aDPId));
       
    92     if(KErrNotFound == err)
       
    93         {
       
    94         _LIT(KSQLInsertPkgIDObjectText, "INSERT INTO PkgIDStore (DataProviderId, PkgId) VALUES (%u, %u)");
       
    95         iSqlStatement.Format(KSQLInsertPkgIDObjectText, aDPId, aPkgId);
       
    96         User::LeaveIfError(iDatabase.Execute(iSqlStatement));
       
    97         iDPIDs.AppendL(aDPId);
       
    98         iPkgIDs.AppendL(aPkgId);
       
    99         }
       
   100     else
       
   101         {
       
   102         if(aPkgId != iPkgIDs[index])
       
   103             {
       
   104             _LIT(KSQLSetPkgIDObjectText, "UPDATE PkgIDStore SET PkgId = %u WHERE DataProviderId = %u");
       
   105             iSqlStatement.Format(KSQLSetPkgIDObjectText, aPkgId,aDPId);
       
   106             User::LeaveIfError(iDatabase.Execute(iSqlStatement));
       
   107             iPkgIDs[index] = aPkgId;
       
   108             }
       
   109         }
       
   110     }
       
   111 
       
   112 const RArray<TUint>& CMTPPkgIDStore::DPIDL() const
       
   113     {
       
   114     __ASSERT_DEBUG((iPkgIDs.Count() == iDPIDs.Count()), User::Invariant());
       
   115     return iDPIDs;
       
   116     }
       
   117 
       
   118 TUint CMTPPkgIDStore::PKGIDL (TUint aIndex) const
       
   119     {
       
   120     __ASSERT_DEBUG((aIndex < iPkgIDs.Count()), User::Invariant());
       
   121     return iPkgIDs[aIndex];
       
   122     }
       
   123 
       
   124 TInt CMTPPkgIDStore::RemoveL(TUint aDpId)
       
   125     {
       
   126     TInt index = iDPIDs.Find(aDpId);
       
   127     if(KErrNotFound != index)
       
   128         {
       
   129         iDPIDs.Remove(index);
       
   130         iPkgIDs.Remove(index);
       
   131         _LIT(KSQLDeleteObjectText, "DELETE FROM PkgIDStore WHERE DataProviderId = %u");
       
   132         iSqlStatement.Format(KSQLDeleteObjectText, aDpId);
       
   133         User::LeaveIfError(iDatabase.Execute(iSqlStatement));
       
   134         }
       
   135     return index;
       
   136     }