locationdataharvester/mylocationsengine/src/lookupmaptiledb.cpp
changeset 26 f3533f6eae3f
parent 20 cd10d5b85554
child 30 96df3ab41000
equal deleted inserted replaced
24:ccec19943943 26:f3533f6eae3f
    19 #include "mylocationlogger.h"
    19 #include "mylocationlogger.h"
    20 #include "lookupmaptiledb.h"
    20 #include "lookupmaptiledb.h"
    21 
    21 
    22 _LIT( KSelectAllFrom, "SELECT * FROM " );
    22 _LIT( KSelectAllFrom, "SELECT * FROM " );
    23 _LIT(KQueryToDB,"SELECT * FROM cntmaptilelookuptable WHERE cntuid = %d AND source = %d");
    23 _LIT(KQueryToDB,"SELECT * FROM cntmaptilelookuptable WHERE cntuid = %d AND source = %d");
       
    24 _LIT( KSelectfilepathFrom, "SELECT filepath FROM " );
       
    25 _LIT(KQueryMaptile, "SELECT filepath FROM cntmaptilelookuptable WHERE filepath = '%S'");
       
    26 // string 'where'
       
    27 _LIT( KStringWhere, " WHERE " );
       
    28 // string ' = '
       
    29 _LIT( KStringEqual, " = " );
       
    30 
       
    31 _LIT(KQueryByMaptileFetchingState,"SELECT * FROM cntmaptilelookuptable WHERE fetchingstatus = %d");
    24 
    32 
    25 // -----------------------------------------------------------------------------
    33 // -----------------------------------------------------------------------------
    26 // CLookupMapTileDatabase::CLookupMapTileDatabase()
    34 // CLookupMapTileDatabase::CLookupMapTileDatabase()
    27 // Default constructor.
    35 // Default constructor.
    28 // -----------------------------------------------------------------------------
    36 // -----------------------------------------------------------------------------
   146 
   154 
   147     columns->AddL(TDbCol(NColSource, EDbColUint32));
   155     columns->AddL(TDbCol(NColSource, EDbColUint32));
   148 
   156 
   149     // add file path type column
   157     // add file path type column
   150     columns->AddL(TDbCol(NCntColFilePath, EDbColText16));
   158     columns->AddL(TDbCol(NCntColFilePath, EDbColText16));
       
   159     
       
   160     //MK 
       
   161     // add map tile fetching status to the db
       
   162     columns->AddL(TDbCol(MapTileFetchingStatus, EDbColUint32));
       
   163     
   151 
   164 
   152     // Create a table
   165     // Create a table
   153     User::LeaveIfError(aDatabase.CreateTable(KMapTileLookupTable, *columns));
   166     User::LeaveIfError(aDatabase.CreateTable(KMapTileLookupTable, *columns));
   154 
   167 
   155     // cleanup the column set
   168     // cleanup the column set
   185     myView.InsertL();
   198     myView.InsertL();
   186     // set the fields
   199     // set the fields
   187     myView.SetColL(KColumncntUid, aLookupItem.iUid);
   200     myView.SetColL(KColumncntUid, aLookupItem.iUid);
   188     myView.SetColL(KColumnSource, aLookupItem.iSource);
   201     myView.SetColL(KColumnSource, aLookupItem.iSource);
   189     myView.SetColL(KColumnFilePath, aLookupItem.iFilePath);
   202     myView.SetColL(KColumnFilePath, aLookupItem.iFilePath);
       
   203     myView.SetColL(KColumnMapTileFetchingStatus, aLookupItem.iFetchingStatus); //MK
   190 
   204 
   191     myView.PutL();
   205     myView.PutL();
   192 
   206 
   193     CleanupStack::PopAndDestroy(&myView); // myView
   207     CleanupStack::PopAndDestroy(&myView); // myView
   194     iItemsDatabase.Commit();
   208     iItemsDatabase.Commit();
   225     if (myView.AtRow())
   239     if (myView.AtRow())
   226     {
   240     {
   227         // found the entry. update it.
   241         // found the entry. update it.
   228         myView.UpdateL();
   242         myView.UpdateL();
   229         myView.SetColL(KColumnFilePath, aLookupItem.iFilePath);
   243         myView.SetColL(KColumnFilePath, aLookupItem.iFilePath);
       
   244         myView.SetColL(KColumnMapTileFetchingStatus, aLookupItem.iFetchingStatus); //MK
   230         myView.PutL();
   245         myView.PutL();
   231     }
   246     }
   232 
   247 
   233     CleanupStack::PopAndDestroy(&myView); // myView
   248     CleanupStack::PopAndDestroy(&myView); // myView
   234     iItemsDatabase.Commit();
   249     iItemsDatabase.Commit();
   240 // -----------------------------------------------------------------------------
   255 // -----------------------------------------------------------------------------
   241 // CLookupMapTileDatabase::DeleteEntryL()
   256 // CLookupMapTileDatabase::DeleteEntryL()
   242 // Deletes an entry from the lookup table.
   257 // Deletes an entry from the lookup table.
   243 // -----------------------------------------------------------------------------
   258 // -----------------------------------------------------------------------------
   244 //
   259 //
   245 void CLookupMapTileDatabase::DeleteEntryL(const TLookupItem& aLookupItem)
   260 void CLookupMapTileDatabase::DeleteEntryL(TLookupItem& aLookupItem)
   246 {
   261 {
   247     __TRACE_CALLSTACK;// Create the query to find the row to be deleted.
   262     __TRACE_CALLSTACK;// Create the query to find the row to be deleted.
   248 
   263 
   249     TFileName queryBuffer;
   264     TFileName queryBuffer;
   250     queryBuffer.Format(KQueryToDB, aLookupItem.iUid, aLookupItem.iSource);
   265     queryBuffer.Format(KQueryToDB, aLookupItem.iUid, aLookupItem.iSource);
   269     myView.FirstL();
   284     myView.FirstL();
   270 
   285 
   271     // Delete the entry found.
   286     // Delete the entry found.
   272     if (myView.AtRow())
   287     if (myView.AtRow())
   273     {
   288     {
   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();
   289         myView.GetL();
   323         if (aLookupItem.iUid == myView.ColUint(KColumncntUid))
   290         if (aLookupItem.iUid == myView.ColUint(KColumncntUid))
   324         {
   291         {
   325             aLookupItem.iFilePath.Copy(myView.ColDes16(KColumnFilePath));
   292             aLookupItem.iFilePath.Copy(myView.ColDes16(KColumnFilePath));
       
   293             
   326         }
   294         }
       
   295         myView.DeleteL();
       
   296     }
       
   297 
       
   298     CleanupStack::PopAndDestroy(&myView); // myView
       
   299     iItemsDatabase.Commit();
       
   300     // compacts the databse, by physicaly removing deleted data.
       
   301     iItemsDatabase.Compact();
       
   302 
       
   303     Close();
       
   304 }
       
   305 
       
   306 // -----------------------------------------------------------------------------
       
   307 // CLookupMapTileDatabase::DeleteMapTileL()
       
   308 // Deletes an maptile if there's no reference to maptile in lookupdb
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 void CLookupMapTileDatabase::DeleteMapTileL( const TLookupItem& aLookupItem)
       
   312 {
       
   313     __TRACE_CALLSTACK;// Create the query to find the row to be deleted.
       
   314 
       
   315     TFileName queryBuffer;    
       
   316     queryBuffer.Format(KQueryMaptile, &aLookupItem.iFilePath);
       
   317     
       
   318     TInt ret = Open();
       
   319     if (ret != KErrNone)
       
   320     {
       
   321        Close();
       
   322        Open();      
       
   323     }
       
   324     
       
   325     iItemsDatabase.Begin();
       
   326 
       
   327     RDbView myView;
       
   328     // query buffer finds only the selected item row.
       
   329     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   330     CleanupClosePushL(myView);
       
   331 
       
   332     myView.EvaluateAll();
       
   333 
       
   334     // positions the cursor on the first row of the rowset
       
   335     myView.FirstL();
       
   336 
       
   337     // Delete if no reference to maptile
       
   338     if (!myView.AtRow())
       
   339     {
       
   340         ret = iFsSession.Delete(aLookupItem.iFilePath);     
       
   341     }
       
   342         
       
   343     CleanupStack::PopAndDestroy(&myView); // myView
       
   344 
       
   345     Close();
       
   346 }
       
   347 
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // CLookupMapTileDatabase::FindEntriesByMapTileFetchingStatusL()
       
   351 // Finds a list of lookup items given a landmark uid.
       
   352 // -----------------------------------------------------------------------------
       
   353 //
       
   354 void CLookupMapTileDatabase::FindEntriesByMapTileFetchingStateL(const TUint32 aFetchingState,
       
   355         RArray<TLookupItem>& aLookupItemArray)
       
   356 {
       
   357     __TRACE_CALLSTACK;// Create a query to find the item.
       
   358     TFileName queryBuffer;
       
   359     queryBuffer.Format(KQueryByMaptileFetchingState,aFetchingState);
       
   360     
       
   361     TInt ret = Open();
       
   362         if (ret != KErrNone)
       
   363         {
       
   364            Close();
       
   365            ret= Open();
       
   366            
       
   367         }
       
   368       iItemsDatabase.Begin();
       
   369 
       
   370     
       
   371     // Create a view of the table with the above query.
       
   372     RDbView myView;
       
   373     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   374     CleanupClosePushL(myView);
       
   375     myView.EvaluateAll();
       
   376     myView.FirstL();
       
   377 
       
   378     while (myView.AtRow())
       
   379     {
       
   380         // Item found. get the details.
       
   381         myView.GetL();
       
   382         TLookupItem newItem;
       
   383         newItem.iUid = myView.ColUint(KColumnUid);
       
   384         newItem.iSource = myView.ColUint(KColumnSource);
       
   385         aLookupItemArray.Append(newItem);
       
   386         myView.NextL();
       
   387     }
       
   388 
       
   389     CleanupStack::PopAndDestroy(&myView); // myView
       
   390 }
       
   391 
       
   392 // -----------------------------------------------------------------------------
       
   393 // CLookupMapTileDatabase::FindEntryL()
       
   394 // Finds an entry in the lookup table.
       
   395 // -----------------------------------------------------------------------------
       
   396 //
       
   397 TBool CLookupMapTileDatabase::FindEntryL(TLookupItem& aLookupItem)
       
   398 {
       
   399     __TRACE_CALLSTACK;// used to store return value
       
   400     TBool retVal = EFalse;
       
   401     // Create a query to find the item.
       
   402     TFileName queryBuffer;
       
   403     queryBuffer.Format(KQueryToDB, aLookupItem.iUid, aLookupItem.iSource);
       
   404 
       
   405    TInt ret = Open();
       
   406     if (ret != KErrNone)
       
   407     {
       
   408 		Close();
       
   409         Open();
       
   410        
       
   411     }
       
   412 	 iItemsDatabase.Begin();
       
   413 
       
   414     // Create a view of the table with the above query.
       
   415     RDbView myView;
       
   416     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   417     CleanupClosePushL(myView);
       
   418     myView.EvaluateAll();
       
   419     myView.FirstL();
       
   420 
       
   421     if (myView.AtRow())
       
   422     {
       
   423         // Item found. get the details.
       
   424         myView.GetL();
   327         retVal = ETrue;
   425         retVal = ETrue;
   328     }
   426     }
   329 
   427 
   330     CleanupStack::PopAndDestroy(&myView); // myView
   428     CleanupStack::PopAndDestroy(&myView); // myView
   331     Close();
   429     Close();
   332     return retVal;
   430     return retVal;
   333 }
   431 }
       
   432 
       
   433 // -----------------------------------------------------------------------------
       
   434 // CLookupMapTileDatabase::FindEntryByFIlePathLL()
       
   435 // Finds an entry in the lookup table for maptile image file
       
   436 // -----------------------------------------------------------------------------
       
   437 //
       
   438 TBool CLookupMapTileDatabase::FindEntryByFilePathL(const TDesC& aFilePath)
       
   439 {
       
   440     __TRACE_CALLSTACK;// used to store return value
       
   441     TBool retVal = EFalse;
       
   442    
       
   443     TInt ret = Open();
       
   444     if (ret != KErrNone)
       
   445     {
       
   446         Close();
       
   447         Open();
       
   448        
       
   449     }
       
   450     
       
   451     iItemsDatabase.Begin();
       
   452     
       
   453     // Create a query to find the item.
       
   454     TFileName queryBuffer;
       
   455     _LIT(KFormatSpec, "%S%S%S%S%S'%S'");
       
   456     
       
   457     queryBuffer.Format(KFormatSpec, 
       
   458         &KSelectfilepathFrom(), 
       
   459         &KMapTileLookupTable(),
       
   460         &KStringWhere(),
       
   461         &NCntColFilePath(),
       
   462         &KStringEqual(),
       
   463         &aFilePath);
       
   464     
       
   465     // Create a view of the table with the above query.
       
   466     RDbView myView;
       
   467     TInt retPrep = myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   468     CleanupClosePushL(myView);
       
   469     myView.EvaluateAll();
       
   470     myView.FirstL();
       
   471 
       
   472     if (myView.AtRow())
       
   473     {
       
   474         // Item found, return true
       
   475         retVal = ETrue;
       
   476     }
       
   477 
       
   478     CleanupStack::PopAndDestroy(&myView); // myView
       
   479     Close();
       
   480 
       
   481     return retVal;
       
   482 }
       
   483 
   334 // End of file
   484 // End of file
   335 
   485