locationdataharvester/mylocationsengine/src/mylocationslookupdb.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: Mylocation database lookup table implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <BAUTILS.H>
       
    19 #include <EPos_CPosLmCategoryManager.h>
       
    20 #include "mylocationslookupdb.h"
       
    21 #include "mylocationlogger.h"
       
    22 // select all from
       
    23 _LIT( KSelectAllFrom, "SELECT * FROM " );
       
    24 // string 'where'
       
    25 _LIT( KStringWhere, " WHERE " );
       
    26 // string ' = '
       
    27 _LIT( KStringEqual, " = " );
       
    28 // string 'And'
       
    29 _LIT( KStringAnd, " AND " );
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CLookupDatabase::CLookupDatabase()
       
    33 // Default constructor.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CLookupDatabase::CLookupDatabase()
       
    37 {
       
    38 }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CLookupDatabase::~CLookupDatabase()
       
    42 // Destructor.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CLookupDatabase::~CLookupDatabase()
       
    46 {
       
    47     __TRACE_CALLSTACK;// close the database
       
    48     iItemsDatabase.Close();
       
    49     // close the file session
       
    50     iFsSession.Close();
       
    51 }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CLookupDatabase::~CLookupDatabase()
       
    55 // Creates an object of this class and pushes to cleanup stack.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CLookupDatabase* CLookupDatabase::NewLC(const TDesC& aLookupTableName)
       
    59 {
       
    60     CLookupDatabase* self = new (ELeave) CLookupDatabase;
       
    61     CleanupStack::PushL(self);
       
    62     self->ConstructL(aLookupTableName);
       
    63     return self;
       
    64 }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CLookupDatabase::NewL()
       
    68 // Creates an object of this class.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CLookupDatabase* CLookupDatabase::NewL(const TDesC& aLookupTableName)
       
    72 {
       
    73     CLookupDatabase* self = CLookupDatabase::NewLC(aLookupTableName);
       
    74     CleanupStack::Pop(self);
       
    75     return self;
       
    76 }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CLookupDatabase::ConstructL()
       
    80 // 2nd phase contructor.
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 void CLookupDatabase::ConstructL(const TDesC& aLookupTableName)
       
    84 {
       
    85     __TRACE_CALLSTACK;
       
    86     User::LeaveIfError(iFsSession.Connect());
       
    87 
       
    88     //create private path
       
    89     User::LeaveIfError(iFsSession.CreatePrivatePath(RFs::GetSystemDrive()));
       
    90     // private path with no drive on it
       
    91     iFsSession.PrivatePath(iDbFileName);
       
    92 
       
    93     TFindFile PrivFolder(iFsSession);
       
    94     // find out the drive
       
    95     if (KErrNone == PrivFolder.FindByDir(iDbFileName, KNullDesC))
       
    96     {
       
    97         iFsSession.MkDir(KLookupDbPath);
       
    98         iDbFileName.Copy(KLookupDbPath);
       
    99         iDbFileName.Append(aLookupTableName);
       
   100 
       
   101         if (!BaflUtils::FileExists(iFsSession, iDbFileName))
       
   102         { // no database exists so we make one
       
   103             User::LeaveIfError(iItemsDatabase.Create(iFsSession, iDbFileName));
       
   104             // and will create the only table needed for it
       
   105             CreateTableL(iItemsDatabase);
       
   106 
       
   107             //close the database
       
   108             iItemsDatabase.Close();
       
   109         }
       
   110     }
       
   111 }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CLookupDatabase::Open()
       
   115 // Opens the lookup database.
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 TInt CLookupDatabase::Open()
       
   119 {
       
   120     __TRACE_CALLSTACK;
       
   121     return iItemsDatabase.Open(iFsSession, iDbFileName);
       
   122 }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CLookupDatabase::Close()
       
   126 // Closes the lookup database.
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CLookupDatabase::Close()
       
   130 {
       
   131     __TRACE_CALLSTACK;
       
   132     iItemsDatabase.Close();
       
   133 }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CLookupDatabase::CreateTableL()
       
   137 // Creates a lookup table.
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 void CLookupDatabase::CreateTableL(RDbDatabase& aDatabase)
       
   141 {
       
   142     __TRACE_CALLSTACK;// Create a table definition
       
   143     CDbColSet* columns = CDbColSet::NewLC();
       
   144 
       
   145     // Add Columns
       
   146 
       
   147     // Add uid column
       
   148     columns->AddL(TDbCol(NColUid, EDbColUint32));
       
   149 
       
   150     // add source type column
       
   151     columns->AddL(TDbCol(NColSource, EDbColUint32));
       
   152 
       
   153     // add landmark uid column
       
   154     columns->AddL(TDbCol(NColLmUid, EDbColUint32));
       
   155 
       
   156     // Create a table
       
   157     User::LeaveIfError(aDatabase.CreateTable(KLookupTable, *columns));
       
   158 
       
   159     // cleanup the column set
       
   160     CleanupStack::PopAndDestroy(columns);
       
   161 }
       
   162 
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CLookupDatabase::CreateEntryL()
       
   166 // Creates an entry in the lookup table.
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 void CLookupDatabase::CreateEntryL(const TLookupItem& aLookupItem)
       
   170 {
       
   171     __TRACE_CALLSTACK;// create a query for the view
       
   172     TFileName queryBuffer;
       
   173     queryBuffer.Copy(KSelectAllFrom);
       
   174     queryBuffer.Append(KLookupTable);
       
   175 
       
   176     iItemsDatabase.Begin();
       
   177 
       
   178     RDbView myView;
       
   179     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   180     CleanupClosePushL(myView);
       
   181 
       
   182     // Inert the item
       
   183     myView.InsertL();
       
   184 
       
   185     // set the fields
       
   186     myView.SetColL(KColumnUid, aLookupItem.iUid);
       
   187     myView.SetColL(KColumnSource, aLookupItem.iSource);
       
   188     myView.SetColL(KColumnLmkUid, aLookupItem.iLmId);
       
   189 
       
   190     myView.PutL();
       
   191 
       
   192     CleanupStack::PopAndDestroy(&myView); // myView
       
   193     iItemsDatabase.Commit();
       
   194 }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CLookupDatabase::UpdateEntryL()
       
   198 // Updates an entry in the lookup table.
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 void CLookupDatabase::UpdateEntryL(const TLookupItem& aLookupItem)
       
   202 {
       
   203     __TRACE_CALLSTACK;// Create the query to find the row to be updated.
       
   204     TFileName queryBuffer;
       
   205     queryBuffer.Copy(KSelectAllFrom);
       
   206     queryBuffer.Append(KLookupTable);
       
   207     queryBuffer.Append(KStringWhere);
       
   208     queryBuffer.Append(NColUid);
       
   209     queryBuffer.Append(KStringEqual);
       
   210     queryBuffer.AppendNum(aLookupItem.iUid);
       
   211     queryBuffer.Append(KStringAnd);
       
   212     queryBuffer.Append(NColSource);
       
   213     queryBuffer.Append(KStringEqual);
       
   214     queryBuffer.AppendNum(aLookupItem.iSource);
       
   215 
       
   216     iItemsDatabase.Begin();
       
   217 
       
   218     // Create a view of the table based on the query created.
       
   219     RDbView myView;
       
   220     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   221     CleanupClosePushL(myView);
       
   222 
       
   223     myView.EvaluateAll();
       
   224     myView.FirstL();
       
   225 
       
   226     if (myView.AtRow())
       
   227     {
       
   228         // found the entry. update it.
       
   229         myView.UpdateL();
       
   230         myView.SetColL(KColumnLmkUid, aLookupItem.iLmId);
       
   231         myView.PutL();
       
   232     }
       
   233 
       
   234     CleanupStack::PopAndDestroy(&myView); // myView
       
   235     iItemsDatabase.Commit();
       
   236 
       
   237 }
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // CLookupDatabase::DeleteEntryL()
       
   241 // Deletes an entry from the lookup table.
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 void CLookupDatabase::DeleteEntryL(const TLookupItem& aLookupItem)
       
   245 {
       
   246     __TRACE_CALLSTACK;// Create the query to find the row to be deleted.
       
   247     TFileName queryBuffer;
       
   248     queryBuffer.Copy(KSelectAllFrom);
       
   249     queryBuffer.Append(KLookupTable);
       
   250     queryBuffer.Append(KStringWhere);
       
   251     queryBuffer.Append(NColUid);
       
   252     queryBuffer.Append(KStringEqual);
       
   253     queryBuffer.AppendNum(aLookupItem.iUid);
       
   254     queryBuffer.Append(KStringAnd);
       
   255     queryBuffer.Append(NColSource);
       
   256     queryBuffer.Append(KStringEqual);
       
   257     queryBuffer.AppendNum(aLookupItem.iSource);
       
   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         myView.DeleteL();
       
   276     }
       
   277 
       
   278     CleanupStack::PopAndDestroy(&myView); // myView
       
   279     iItemsDatabase.Commit();
       
   280     // compacts the databse, by physicaly removing deleted data.
       
   281     iItemsDatabase.Compact();
       
   282 }
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CLookupDatabase::FindEntryL()
       
   286 // Finds an entry in the lookup table.
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 TBool CLookupDatabase::FindEntryL(TLookupItem& aLookupItem)
       
   290 {
       
   291     __TRACE_CALLSTACK;// used to store return value
       
   292     TBool retVal = EFalse;
       
   293 
       
   294     // Create a query to find the item.
       
   295     TFileName queryBuffer;
       
   296     queryBuffer.Copy(KSelectAllFrom);
       
   297     queryBuffer.Append(KLookupTable);
       
   298     queryBuffer.Append(KStringWhere);
       
   299     queryBuffer.Append(NColUid);
       
   300     queryBuffer.Append(KStringEqual);
       
   301     queryBuffer.AppendNum(aLookupItem.iUid);
       
   302     queryBuffer.Append(KStringAnd);
       
   303     queryBuffer.Append(NColSource);
       
   304     queryBuffer.Append(KStringEqual);
       
   305     queryBuffer.AppendNum(aLookupItem.iSource);
       
   306 
       
   307     // Create a view of the table with the above query.
       
   308     RDbView myView;
       
   309     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   310     CleanupClosePushL(myView);
       
   311     myView.EvaluateAll();
       
   312     myView.FirstL();
       
   313 
       
   314     if (myView.AtRow())
       
   315     {
       
   316         // Item found. get the details.
       
   317         myView.GetL();
       
   318 
       
   319         aLookupItem.iUid = myView.ColUint(KColumnUid);
       
   320         aLookupItem.iSource = myView.ColUint(KColumnSource);
       
   321         aLookupItem.iLmId = myView.ColUint(KColumnLmkUid);
       
   322         retVal = ETrue;
       
   323     }
       
   324 
       
   325     CleanupStack::PopAndDestroy(&myView); // myView
       
   326 
       
   327     return retVal;
       
   328 }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // CLookupDatabase::FindEntriesByLandmarkIdL()
       
   332 // Finds a list of lookup items given a landmark uid.
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 void CLookupDatabase::FindEntriesByLandmarkIdL(const TUint32 aLandmarkId,
       
   336         RArray<TLookupItem>& aLookupItemArray)
       
   337 {
       
   338     __TRACE_CALLSTACK;// Create a query to find the item.
       
   339     TFileName queryBuffer;
       
   340     queryBuffer.Copy(KSelectAllFrom);
       
   341     queryBuffer.Append(KLookupTable);
       
   342     queryBuffer.Append(KStringWhere);
       
   343     queryBuffer.Append(NColLmUid);
       
   344     queryBuffer.Append(KStringEqual);
       
   345     queryBuffer.AppendNum(aLandmarkId);
       
   346 
       
   347     // Create a view of the table with the above query.
       
   348     RDbView myView;
       
   349     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   350     CleanupClosePushL(myView);
       
   351     myView.EvaluateAll();
       
   352     myView.FirstL();
       
   353 
       
   354     while (myView.AtRow())
       
   355     {
       
   356         // Item found. get the details.
       
   357         myView.GetL();
       
   358         TLookupItem newItem;
       
   359         newItem.iUid = myView.ColUint(KColumnUid);
       
   360         newItem.iSource = myView.ColUint(KColumnSource);
       
   361         newItem.iLmId = myView.ColUint(KColumnLmkUid);
       
   362         aLookupItemArray.Append(newItem);
       
   363         myView.NextL();
       
   364     }
       
   365 
       
   366     CleanupStack::PopAndDestroy(&myView); // myView
       
   367 }
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CLookupDatabase::FindEntriesByLandmarkIdL()
       
   371 // Finds a list of lookup items given a landmark uid.
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 void CLookupDatabase::FindEntriesBySourceTypeL(const TUint32 aSourceType,
       
   375         RArray<TLookupItem>& aLookupItemArray)
       
   376 {
       
   377     __TRACE_CALLSTACK;// Create a query to find the item.
       
   378     TFileName queryBuffer;
       
   379     queryBuffer.Copy(KSelectAllFrom);
       
   380     queryBuffer.Append(KLookupTable);
       
   381     queryBuffer.Append(KStringWhere);
       
   382     queryBuffer.Append(NColSource);
       
   383     queryBuffer.Append(KStringEqual);
       
   384     queryBuffer.AppendNum(aSourceType);
       
   385 
       
   386     // Create a view of the table with the above query.
       
   387     RDbView myView;
       
   388     myView.Prepare(iItemsDatabase, TDbQuery(queryBuffer));
       
   389     CleanupClosePushL(myView);
       
   390     myView.EvaluateAll();
       
   391     myView.FirstL();
       
   392 
       
   393     while (myView.AtRow())
       
   394     {
       
   395         // Item found. get the details.
       
   396         myView.GetL();
       
   397 
       
   398         TLookupItem newItem;
       
   399         newItem.iUid = myView.ColUint(KColumnUid);
       
   400         newItem.iSource = myView.ColUint(KColumnSource);
       
   401         newItem.iLmId = myView.ColUint(KColumnLmkUid);
       
   402         aLookupItemArray.Append(newItem);
       
   403         myView.NextL();
       
   404     }
       
   405 
       
   406     CleanupStack::PopAndDestroy(&myView); // myView
       
   407 }
       
   408 
       
   409 // End of file
       
   410