mpxplugins/serviceplugins/collectionplugins/mpxsqlitedbhgplugin/src/mpxdbauxiliary.cpp
changeset 0 ff3acec5bc43
child 56 2cbbefa9af78
equal deleted inserted replaced
-1:000000000000 0:ff3acec5bc43
       
     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:  Responsible for interaction with the Auxiliary table.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <sqldb.h>
       
    21 #include <mpxlog.h>
       
    22 
       
    23 #include "mpxdbcommonutil.h"
       
    24 
       
    25 #include "mpxcollectiondbdef.h"
       
    26 #include "mpxdbmanager.h"
       
    27 #include "mpxdbpluginqueries.h"
       
    28 #include "mpxdbauxiliary.h"
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ==============================
       
    31 
       
    32 // ----------------------------------------------------------------------------
       
    33 // Two-phased constructor.
       
    34 // ----------------------------------------------------------------------------
       
    35 //
       
    36 CMPXDbAuxiliary* CMPXDbAuxiliary::NewL(
       
    37     CMPXDbManager& aDbManager)
       
    38     {
       
    39     MPX_FUNC("CMPXDbAuxiliary::NewL");
       
    40 
       
    41     CMPXDbAuxiliary* self = CMPXDbAuxiliary::NewLC(aDbManager);
       
    42     CleanupStack::Pop(self);
       
    43     return self;
       
    44     }
       
    45 
       
    46 // ----------------------------------------------------------------------------
       
    47 // Two-phased constructor.
       
    48 // ----------------------------------------------------------------------------
       
    49 //
       
    50 CMPXDbAuxiliary* CMPXDbAuxiliary::NewLC(
       
    51     CMPXDbManager& aDbManager)
       
    52     {
       
    53     MPX_FUNC("CMPXDbAuxiliary::NewLC");
       
    54 
       
    55     CMPXDbAuxiliary* self = new (ELeave) CMPXDbAuxiliary(aDbManager);
       
    56     CleanupStack::PushL(self);
       
    57     self->ConstructL();
       
    58     return self;
       
    59     }
       
    60 
       
    61 // ----------------------------------------------------------------------------
       
    62 // Destructor
       
    63 // ----------------------------------------------------------------------------
       
    64 //
       
    65 CMPXDbAuxiliary::~CMPXDbAuxiliary()
       
    66     {
       
    67     MPX_FUNC("CMPXDbAuxiliary::~CMPXDbAuxiliary");
       
    68     }
       
    69 
       
    70 // ----------------------------------------------------------------------------
       
    71 // Constructor
       
    72 // ----------------------------------------------------------------------------
       
    73 //
       
    74 CMPXDbAuxiliary::CMPXDbAuxiliary(
       
    75     CMPXDbManager& aDbManager) :
       
    76     CMPXDbTable(aDbManager)
       
    77     {
       
    78     MPX_FUNC("CMPXDbAuxiliary::CMPXDbAuxiliary");
       
    79     }
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 // Second phase constructor.
       
    83 // ----------------------------------------------------------------------------
       
    84 //
       
    85 void CMPXDbAuxiliary::ConstructL()
       
    86     {
       
    87     MPX_FUNC("CMPXDbAuxiliary::ConstructL");
       
    88     BaseConstructL();
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // CMPXDbAuxiliary::SetLastRefreshedTimeL
       
    93 // ----------------------------------------------------------------------------
       
    94 //
       
    95 void CMPXDbAuxiliary::SetLastRefreshedTimeL(
       
    96     TTime aTime)
       
    97     {
       
    98     MPX_FUNC("CMPXDbAuxiliary::SetLastRefreshedTimeL");
       
    99 
       
   100     // update all databases
       
   101     HBufC* time = MPXDbCommonUtil::TTimeToDesLC(aTime);
       
   102     iDbManager.ExecuteQueryL(KDbManagerAllDrives, KQueryAuxiliarySetTime, time);
       
   103     CleanupStack::PopAndDestroy(time);
       
   104     }
       
   105 
       
   106 // ----------------------------------------------------------------------------
       
   107 // CMPXDbAuxiliary::LastRefreshedTimeL
       
   108 // ----------------------------------------------------------------------------
       
   109 //
       
   110 TTime CMPXDbAuxiliary::LastRefreshedTimeL()
       
   111     {
       
   112     MPX_FUNC("CMPXDbAuxiliary::LastRefreshedTimeL");
       
   113 
       
   114     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(KQueryAuxiliaryGetTime));
       
   115     CleanupClosePushL(recordset);
       
   116 
       
   117     if (recordset.Next() != KSqlAtRow)
       
   118         {
       
   119         User::Leave(KErrCorrupt);
       
   120         }
       
   121 
       
   122     // read the time string and convert it to TTime
       
   123     TTime time(MPXDbCommonUtil::DesToTTimeL(
       
   124     	MPXDbCommonUtil::GetColumnTextL(recordset, KMPXTableDefaultIndex)));
       
   125     CleanupStack::PopAndDestroy(&recordset);
       
   126 
       
   127     return time;
       
   128     }
       
   129 
       
   130 // ----------------------------------------------------------------------------
       
   131 // CMPXDbAuxiliary::SetDBCorruptedL
       
   132 // ----------------------------------------------------------------------------
       
   133 //
       
   134 void CMPXDbAuxiliary::SetDBCorruptedL(
       
   135     TBool aCorrupt)
       
   136     {
       
   137     MPX_FUNC("CMPXDbAuxiliary::SetDBCorruptedL");
       
   138 
       
   139     // update all databases
       
   140     iDbManager.ExecuteQueryL(KDbManagerAllDrives, KQueryAuxiliarySetCorrupt,
       
   141         aCorrupt);
       
   142     }
       
   143 
       
   144 // ----------------------------------------------------------------------------
       
   145 // CMPXDbAuxiliary::DBCorruptedL
       
   146 // ----------------------------------------------------------------------------
       
   147 //
       
   148 TBool CMPXDbAuxiliary::DBCorruptedL()
       
   149     {
       
   150     MPX_FUNC("CMPXDbAuxiliary::DBCorruptedL");
       
   151     return (ExecuteSumQueryL(KQueryAuxiliaryGetCorrupt) > 0);
       
   152     }
       
   153 
       
   154 // ----------------------------------------------------------------------------
       
   155 // CMPXDbAuxiliary::SetSaveDeletedRecordCountL
       
   156 // ----------------------------------------------------------------------------
       
   157 //
       
   158 void CMPXDbAuxiliary::SetSaveDeletedRecordCountL(TInt aDrive,
       
   159     TUint32 aValue)
       
   160     {
       
   161     MPX_FUNC("CMPXDbAuxiliary::SetSaveDeletedRecordCountL");
       
   162 
       
   163     // update all databases
       
   164     iDbManager.ExecuteQueryL(aDrive, KQueryAuxiliarySetCount, aValue);
       
   165     }
       
   166 
       
   167 // ----------------------------------------------------------------------------
       
   168 // CMPXDbAuxiliary::SaveDeletedRecordCountL
       
   169 // ----------------------------------------------------------------------------
       
   170 //
       
   171 TUint32 CMPXDbAuxiliary::SaveDeletedRecordCountL()
       
   172     {
       
   173     MPX_FUNC("CMPXDbAuxiliary::SaveDeletedRecordCountL");
       
   174     return ExecuteSumQueryL(KQueryAuxiliaryGetCount);
       
   175     }
       
   176 
       
   177 
       
   178 // ----------------------------------------------------------------------------
       
   179 // CMPXDbAuxiliary::SaveDeletedRecordCountL
       
   180 // ----------------------------------------------------------------------------
       
   181 //
       
   182 TUint32 CMPXDbAuxiliary::SaveDeletedRecordCountL(TInt aDriveID)
       
   183     {
       
   184     MPX_FUNC("CMPXDbAuxiliary::SaveDeletedRecordCountL ");
       
   185     return ExecuteIntQueryL(aDriveID, KQueryAuxiliaryGetCount);
       
   186     }
       
   187 
       
   188 // ----------------------------------------------------------------------------
       
   189 // CMPXDbAuxiliary::IsRefreshedL
       
   190 // ----------------------------------------------------------------------------
       
   191 //
       
   192 TBool CMPXDbAuxiliary::IsRefreshedL()
       
   193     {
       
   194     MPX_FUNC("CMPXDbAuxiliary::IsRefreshedL");
       
   195 
       
   196     TBool refreshed(ETrue);
       
   197 
       
   198     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(KQueryAuxiliaryGetTime));
       
   199     CleanupClosePushL(recordset);
       
   200 
       
   201     TInt count(0);
       
   202     while (recordset.Next() == KSqlAtRow)
       
   203         {
       
   204         count++;
       
   205 
       
   206         // read the time string and convert it to TTime
       
   207         if (Time::NullTTime() == MPXDbCommonUtil::DesToTTimeL(
       
   208         	MPXDbCommonUtil::GetColumnTextL(recordset, KMPXTableDefaultIndex)))
       
   209             {
       
   210             refreshed = EFalse;
       
   211             break;
       
   212             }
       
   213         }
       
   214 
       
   215     CleanupStack::PopAndDestroy(&recordset);
       
   216     return refreshed && (count > 0);
       
   217     }
       
   218 
       
   219 // ----------------------------------------------------------------------------
       
   220 // CMPXDbAuxiliary::IdL
       
   221 // ----------------------------------------------------------------------------
       
   222 //
       
   223 TInt CMPXDbAuxiliary::IdL( TInt aDrive )
       
   224     {
       
   225     MPX_DEBUG1("CMPXDbAuxiliary::IdL <--");
       
   226     TInt id(0);
       
   227     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(aDrive, KQueryAuxiliaryGetId));
       
   228     CleanupClosePushL( recordset );
       
   229 
       
   230     while(recordset.Next() == KSqlAtRow )
       
   231         {
       
   232         id = recordset.ColumnInt(KMPXTableDefaultIndex);
       
   233         }
       
   234 
       
   235     CleanupStack::PopAndDestroy( &recordset );
       
   236     MPX_DEBUG1("CMPXDbAuxiliary::IdL -->");
       
   237     return id;
       
   238     }
       
   239 
       
   240 // ----------------------------------------------------------------------------
       
   241 // CMPXDbAuxiliary::SetIdL
       
   242 // ----------------------------------------------------------------------------
       
   243 //
       
   244 void CMPXDbAuxiliary::SetIdL( TInt aDrive, TInt aId )
       
   245     {
       
   246     MPX_DEBUG1("CMPXDbAuxiliary::SetIdL <--");
       
   247     iDbManager.ExecuteQueryL(aDrive,KQueryAuxiliarySetId, aId);
       
   248     MPX_DEBUG1("CMPXDbAuxiliary::SetIdL -->");
       
   249     }
       
   250 
       
   251 // ----------------------------------------------------------------------------
       
   252 // CMPXDbAuxiliary::CreateTableL
       
   253 // ----------------------------------------------------------------------------
       
   254 //
       
   255 void CMPXDbAuxiliary::CreateTableL(
       
   256     RSqlDatabase& aDatabase,
       
   257     TBool aCorruptTable)
       
   258     {
       
   259     MPX_FUNC("CMPXDbAuxiliary::CreateTableL");
       
   260 
       
   261     // create the table
       
   262     User::LeaveIfError(aDatabase.Exec(KAuxiliaryCreateTable));
       
   263 
       
   264     // insert the default record
       
   265     // use the same length as '%u' is longer than '0' or '1'
       
   266     HBufC* query = KQueryAuxiliaryInsert().AllocLC();
       
   267     query->Des().Format(KQueryAuxiliaryInsert, aCorruptTable);
       
   268     User::LeaveIfError(aDatabase.Exec(*query));
       
   269     CleanupStack::PopAndDestroy(query);
       
   270     }
       
   271 
       
   272 // ----------------------------------------------------------------------------
       
   273 // CMPXDbAuxiliary::DropTableL
       
   274 // ----------------------------------------------------------------------------
       
   275 //
       
   276 void CMPXDbAuxiliary::DropTableL(
       
   277     RSqlDatabase& aDatabase)
       
   278     {
       
   279     MPX_FUNC("CMPXDbAuxiliary::DropTableL");
       
   280     User::LeaveIfError(aDatabase.Exec(KAuxiliaryDropTable));
       
   281     }
       
   282 
       
   283 // ----------------------------------------------------------------------------
       
   284 // CMPXDbAuxiliary::CheckTableL
       
   285 // ----------------------------------------------------------------------------
       
   286 //
       
   287 TBool CMPXDbAuxiliary::CheckTableL(
       
   288     RSqlDatabase& aDatabase)
       
   289     {
       
   290     MPX_FUNC("CMPXDbAuxiliary::CheckTableL");
       
   291     return DoCheckTable(aDatabase, KAuxiliaryCheckTable);
       
   292     }
       
   293 
       
   294 // End of File