locationdataharvester/mylocationsengine/src/lookupmaptiledb.cpp
changeset 17 0f22fb80ebba
child 20 cd10d5b85554
equal deleted inserted replaced
15:13ae750350c9 17:0f22fb80ebba
       
     1 /*
       
     2 * Copyright (c) 2010 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: Maptile database lookup table source implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <BAUTILS.H>
       
    19 #include "mylocationlogger.h"
       
    20 #include "lookupmaptiledb.h"
       
    21 
       
    22 _LIT( KSelectAllFrom, "SELECT * FROM " );
       
    23 _LIT(KQueryToDB,"SELECT * FROM cntmaptilelookuptable WHERE cntuid = %d AND source = %d");
       
    24 
       
    25 // -----------------------------------------------------------------------------
       
    26 // CLookupMapTileDatabase::CLookupMapTileDatabase()
       
    27 // Default constructor.
       
    28 // -----------------------------------------------------------------------------
       
    29 //
       
    30 CLookupMapTileDatabase::CLookupMapTileDatabase()
       
    31 {
       
    32 }
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CLookupMapTileDatabase::~CLookupMapTileDatabase()
       
    36 // Destructor.
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CLookupMapTileDatabase::~CLookupMapTileDatabase()
       
    40 {
       
    41     __TRACE_CALLSTACK;// close the database
       
    42     iItemsDatabase.Close();
       
    43     // close the file session
       
    44     iFsSession.Close();
       
    45 }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CLookupMapTileDatabase::~CLookupMapTileDatabase()
       
    49 // Creates an object of this class and pushes to cleanup stack.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CLookupMapTileDatabase* CLookupMapTileDatabase::NewLC(
       
    53         const TDesC& aLookupTableName)
       
    54 {
       
    55     CLookupMapTileDatabase* self = new (ELeave) CLookupMapTileDatabase;
       
    56     CleanupStack::PushL(self);
       
    57     self->ConstructL(aLookupTableName);
       
    58     return self;
       
    59 }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CLookupMapTileDatabase::NewL()
       
    63 // Creates an object of this class.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CLookupMapTileDatabase* CLookupMapTileDatabase::NewL(
       
    67         const TDesC& aLookupTableName)
       
    68 {
       
    69     CLookupMapTileDatabase* self = CLookupMapTileDatabase::NewLC(
       
    70             aLookupTableName);
       
    71     CleanupStack::Pop(self);
       
    72     return self;
       
    73 }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CLookupMapTileDatabase::ConstructL()
       
    77 // 2nd phase contructor.
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void CLookupMapTileDatabase::ConstructL(const TDesC& aLookupTableName)
       
    81 {
       
    82     __TRACE_CALLSTACK;
       
    83     User::LeaveIfError(iFsSession.Connect());
       
    84 
       
    85     //create private path
       
    86     User::LeaveIfError(iFsSession.CreatePrivatePath(RFs::GetSystemDrive()));
       
    87     // private path with no drive on it
       
    88     iFsSession.PrivatePath(iDbFileName);
       
    89 
       
    90     TFindFile PrivFolder(iFsSession);
       
    91     // find out the drive
       
    92     if (KErrNone == PrivFolder.FindByDir(iDbFileName, KNullDesC))
       
    93     {
       
    94         iFsSession.MkDir(KLookupDbPath);
       
    95         iDbFileName.Copy(KLookupDbPath);
       
    96         iDbFileName.Append(aLookupTableName);
       
    97 
       
    98         if (!BaflUtils::FileExists(iFsSession, iDbFileName))
       
    99         { // no database exists so we make one
       
   100             User::LeaveIfError(iItemsDatabase.Create(iFsSession, iDbFileName));
       
   101             // and will create the only table needed for it
       
   102             CreateTableL(iItemsDatabase);
       
   103 
       
   104             //close the database
       
   105             iItemsDatabase.Close();
       
   106         }        
       
   107     }
       
   108 }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CLookupMapTileDatabase::Open()
       
   112 // Opens the lookup database.
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 TInt CLookupMapTileDatabase::Open()
       
   116 {
       
   117     __TRACE_CALLSTACK;
       
   118     return iItemsDatabase.Open(iFsSession, iDbFileName);
       
   119 }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CLookupMapTileDatabase::Close()
       
   123 // Closes the lookup database.
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 void CLookupMapTileDatabase::Close()
       
   127 {
       
   128     __TRACE_CALLSTACK;
       
   129     iItemsDatabase.Close();
       
   130 }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CLookupMapTileDatabase::CreateTableL()
       
   134 // Creates a lookup table.
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 void CLookupMapTileDatabase::CreateTableL(RDbDatabase& aDatabase)
       
   138 {
       
   139     __TRACE_CALLSTACK;// Create a table definition
       
   140     CDbColSet* columns = CDbColSet::NewLC();
       
   141 
       
   142     // Add Columns
       
   143 
       
   144     // Add uid column
       
   145     columns->AddL(TDbCol(NCntColUid, EDbColUint32));
       
   146 
       
   147     columns->AddL(TDbCol(NColSource, EDbColUint32));
       
   148 
       
   149     // add file path type column
       
   150     columns->AddL(TDbCol(NCntColFilePath, EDbColText16));
       
   151 
       
   152     // Create a table
       
   153     User::LeaveIfError(aDatabase.CreateTable(KMapTileLookupTable, *columns));
       
   154 
       
   155     // cleanup the column set
       
   156     CleanupStack::PopAndDestroy(columns);
       
   157 }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CLookupMapTileDatabase::CreateEntryL()
       
   161 // Creates an entry in the lookup table.
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 void CLookupMapTileDatabase::CreateEntryL(const TLookupItem& aLookupItem)
       
   165 {
       
   166     __TRACE_CALLSTACK;// create a query for the view
       
   167     TFileName queryBuffer;
       
   168     queryBuffer.Copy(KSelectAllFrom);
       
   169     queryBuffer.Append(KMapTileLookupTable);
       
   170 
       
   171     TInt ret = Open();
       
   172     if (ret != KErrNone)
       
   173     {
       
   174 		Close();
       
   175         Open();
       
   176        
       
   177     }
       
   178 	 iItemsDatabase.Begin();
       
   179     
       
   180     RDbView myView;
       
   181     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   182     CleanupClosePushL(myView);
       
   183 
       
   184     // Inert the item
       
   185     myView.InsertL();
       
   186     // set the fields
       
   187     myView.SetColL(KColumncntUid, aLookupItem.iUid);
       
   188     myView.SetColL(KColumnSource, aLookupItem.iSource);
       
   189     myView.SetColL(KColumnFilePath, aLookupItem.iFilePath);
       
   190 
       
   191     myView.PutL();
       
   192 
       
   193     CleanupStack::PopAndDestroy(&myView); // myView
       
   194     iItemsDatabase.Commit();
       
   195     Close();
       
   196 }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CLookupMapTileDatabase::UpdateEntryL()
       
   200 // Updates an entry in the lookup table.
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 void CLookupMapTileDatabase::UpdateEntryL(const TLookupItem& aLookupItem)
       
   204 {
       
   205     __TRACE_CALLSTACK;// Create the query to find the row to be updated.
       
   206     TFileName queryBuffer;
       
   207     queryBuffer.Format(KQueryToDB, aLookupItem.iUid, aLookupItem.iSource);
       
   208     TInt ret = Open();
       
   209     if (ret != KErrNone)
       
   210     {
       
   211 		Close();
       
   212         Open();
       
   213        
       
   214     }
       
   215 	 iItemsDatabase.Begin();
       
   216 
       
   217     // Create a view of the table based on the query created.
       
   218     RDbView myView;
       
   219     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   220     CleanupClosePushL(myView);
       
   221 
       
   222     myView.EvaluateAll();
       
   223     myView.FirstL();
       
   224 
       
   225     if (myView.AtRow())
       
   226     {
       
   227         // found the entry. update it.
       
   228         myView.UpdateL();
       
   229         myView.SetColL(KColumnFilePath, aLookupItem.iFilePath);
       
   230         myView.PutL();
       
   231     }
       
   232 
       
   233     CleanupStack::PopAndDestroy(&myView); // myView
       
   234     iItemsDatabase.Commit();
       
   235 
       
   236     Close();
       
   237 
       
   238 }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // CLookupMapTileDatabase::DeleteEntryL()
       
   242 // Deletes an entry from the lookup table.
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 void CLookupMapTileDatabase::DeleteEntryL(const TLookupItem& aLookupItem)
       
   246 {
       
   247     __TRACE_CALLSTACK;// Create the query to find the row to be deleted.
       
   248 
       
   249     TFileName queryBuffer;
       
   250     queryBuffer.Format(KQueryToDB, aLookupItem.iUid, aLookupItem.iSource);
       
   251 
       
   252     TInt ret = Open();
       
   253     if (ret != KErrNone)
       
   254     {
       
   255 		Close();
       
   256         Open();
       
   257        
       
   258     }
       
   259 	 iItemsDatabase.Begin();
       
   260 
       
   261     RDbView myView;
       
   262     // query buffer finds only the selected item row.
       
   263     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   264     CleanupClosePushL(myView);
       
   265 
       
   266     myView.EvaluateAll();
       
   267 
       
   268     // positions the cursor on the first row of the rowset
       
   269     myView.FirstL();
       
   270 
       
   271     // Delete the entry found.
       
   272     if (myView.AtRow())
       
   273     {
       
   274         myView.GetL();
       
   275         if (aLookupItem.iUid == myView.ColUint(KColumncntUid))
       
   276         {
       
   277             ret = iFsSession.Delete(myView.ColDes16(KColumnFilePath));
       
   278         }
       
   279         myView.DeleteL();
       
   280     }
       
   281 
       
   282     CleanupStack::PopAndDestroy(&myView); // myView
       
   283     iItemsDatabase.Commit();
       
   284     // compacts the databse, by physicaly removing deleted data.
       
   285     iItemsDatabase.Compact();
       
   286 
       
   287     Close();
       
   288 }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CLookupMapTileDatabase::FindEntryL()
       
   292 // Finds an entry in the lookup table.
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 TBool CLookupMapTileDatabase::FindEntryL(TLookupItem& aLookupItem)
       
   296 {
       
   297     __TRACE_CALLSTACK;// used to store return value
       
   298     TBool retVal = EFalse;
       
   299     // Create a query to find the item.
       
   300     TFileName queryBuffer;
       
   301     queryBuffer.Format(KQueryToDB, aLookupItem.iUid, aLookupItem.iSource);
       
   302 
       
   303    TInt ret = Open();
       
   304     if (ret != KErrNone)
       
   305     {
       
   306 		Close();
       
   307         Open();
       
   308        
       
   309     }
       
   310 	 iItemsDatabase.Begin();
       
   311 
       
   312     // Create a view of the table with the above query.
       
   313     RDbView myView;
       
   314     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   315     CleanupClosePushL(myView);
       
   316     myView.EvaluateAll();
       
   317     myView.FirstL();
       
   318 
       
   319     if (myView.AtRow())
       
   320     {
       
   321         // Item found. get the details.
       
   322         myView.GetL();
       
   323         if (aLookupItem.iUid == myView.ColUint(KColumncntUid))
       
   324         {
       
   325             aLookupItem.iFilePath.Copy(myView.ColDes16(KColumnFilePath));
       
   326         }
       
   327         retVal = ETrue;
       
   328     }
       
   329 
       
   330     CleanupStack::PopAndDestroy(&myView); // myView
       
   331     Close();
       
   332     return retVal;
       
   333 }
       
   334 // End of file
       
   335