locationdataharvester/maptileservice/src/maptiledblookuptable.cpp
changeset 41 b3dd5ec3089d
parent 30 96df3ab41000
equal deleted inserted replaced
38:793f76d9ab0c 41:b3dd5ec3089d
    14 * Description: 
    14 * Description: 
    15 *     Retrieving maptile path from lookup db implementation
    15 *     Retrieving maptile path from lookup db implementation
    16 *
    16 *
    17 */
    17 */
    18 
    18 
    19 #include <bautils.h>
    19 #include <QSqlDatabase>
       
    20 #include <QSqlQuery>
       
    21 #include <QSqlRecord>
       
    22 #include <QVariant>
       
    23 #include <QFile>
       
    24 #include <QTextStream>
       
    25 #include <locationservicedefines.h>
    20 #include <maptileservice.h>
    26 #include <maptileservice.h>
       
    27 #include "mylocationsdefines.h"
    21 #include "maptiledblookuptable.h"
    28 #include "maptiledblookuptable.h"
    22 
    29 
    23 // select all from
    30 // select all from
    24 _LIT( KSelectAllFrom, "SELECT * FROM " );
    31 const QString KSelectAllFrom( "SELECT * FROM " );
    25 
    32 
    26 // string 'where'
    33 // string 'where'
    27 _LIT( KStringWhere, " WHERE " );
    34 const QString KWhere( " WHERE " );
    28 
    35 
    29 // string ' = '
    36 // string ' = '
    30 _LIT( KStringEqual, " = " );
    37 const QString KEqual( " = " );
    31 
    38 
    32 // string 'And'
    39 // string 'AND'
    33 _LIT( KStringAnd, " AND " );
    40 const QString KAnd( " AND " );
    34 
    41 // string 'OR'
    35 
    42 const QString KOr( " OR " );
    36 _LIT(KQueryByMaptileState,"SELECT * FROM cntmaptilelookuptable WHERE cntuid = %d AND ( fetchingstatus = %d OR fetchingstatus = %d )");
    43 
    37 
    44 // string '( ' 
    38 // -----------------------------------------------------------------------------
    45 const QString KOpenBrace( "( " );
    39 // CLookupMapTileDatabase::CLookupMapTileDatabase()
    46 // string ' )'
       
    47 const QString KCloseBrace( " )" );
       
    48 
       
    49 // Maptile table name
       
    50 const QString KMaptileLookupTable( "maptilelookup " );
       
    51 // column source id
       
    52 const QString KSourceId( "sourceid" );
       
    53 // column source type
       
    54 const QString KSourceType( "sourcetype" );
       
    55 // column maptile path
       
    56 const QString KFilePath( "filepath" );
       
    57 // column fetching status
       
    58 const QString KStatus( "fetchingstatus" );
       
    59 
       
    60 // column user setting status
       
    61 const QString KUserSetting( "usersetting" );
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // LookupMapTileDatabase::LookupMapTileDatabase()
    40 // Default constructor.
    65 // Default constructor.
    41 // -----------------------------------------------------------------------------
    66 // -----------------------------------------------------------------------------
    42 //
    67 //
    43 CLookupMapTileDatabase::CLookupMapTileDatabase()
    68 LookupMapTileDatabase::LookupMapTileDatabase(  QObject *parent ) :
    44 {
    69         QObject( parent ),
    45 }
    70         mDb( NULL ),
    46 
    71         mDbOpen( false )
    47 // -----------------------------------------------------------------------------
    72 {
    48 // CLookupMapTileDatabase::~CLookupMapTileDatabase()
    73 	mDb = new QSqlDatabase();
       
    74     *mDb = QSqlDatabase::addDatabase( "QSQLITE" );
       
    75     mDb->setDatabaseName( KLocationDataLookupDbName );
       
    76     if (!mDb->open())
       
    77     {
       
    78         return;
       
    79     }
       
    80 
       
    81     // create lookup table if doesnot exist
       
    82     QSqlQuery query( *mDb );
       
    83     QString queryString;
       
    84     QTextStream ( &queryString ) << "create table if not exists " << KMaptileLookupTable << KOpenBrace << 
       
    85                        KSourceId << " int," <<
       
    86                        KSourceType << " int," <<
       
    87                        KFilePath << " varchar(255)," <<
       
    88                        KStatus << " int ," << 
       
    89                        KUserSetting << " bool " << "default 0" << KCloseBrace;
       
    90                        
       
    91                        
       
    92     query.exec( queryString );
       
    93     
       
    94     mDb->close();
       
    95 }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // LookupMapTileDatabase::~LookupMapTileDatabase()
    49 // Destructor.
    99 // Destructor.
    50 // -----------------------------------------------------------------------------
   100 // -----------------------------------------------------------------------------
    51 //
   101 //
    52 CLookupMapTileDatabase::~CLookupMapTileDatabase()
   102 LookupMapTileDatabase::~LookupMapTileDatabase()
    53 {
   103 {
    54 
   104     close();
    55     // close the database
   105     delete mDb;
    56 	iItemsDatabase.Close();
   106 }
    57 	
   107 
    58 	// close the file session
   108 // ---------------------------------------------------------
    59 	iFsSession.Close();
   109 // LookupMapTileDatabase::open()
       
   110 // ---------------------------------------------------------
       
   111 bool LookupMapTileDatabase::open()
       
   112 {
       
   113     if( !mDbOpen )
       
   114     {
       
   115         mDbOpen = mDb->open();
       
   116     }
       
   117     return mDbOpen;
       
   118 }
       
   119 
       
   120 // ---------------------------------------------------------
       
   121 // LookupMapTileDatabase::close()
       
   122 // ---------------------------------------------------------
       
   123 void LookupMapTileDatabase::close()
       
   124 {
       
   125     if( mDbOpen )
       
   126         mDb->close();
       
   127     mDbOpen = false;
       
   128 }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // LookupMapTileDatabase::createEntry()
       
   132 // Creates an entry in the lookup table.
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void LookupMapTileDatabase::createEntry( const MaptileLookupItem& aLookupItem )
       
   136 {
       
   137     if( mDbOpen )
       
   138     {
       
   139         QString queryString;
       
   140         QTextStream ( &queryString ) <<  
       
   141                 "INSERT INTO " << KMaptileLookupTable << 
       
   142                 KOpenBrace << KSourceId << ", " << KSourceType << ", " << KFilePath << ", " << KStatus << KCloseBrace <<
       
   143                 " VALUES " << 
       
   144                 KOpenBrace << ":sid" << ", " << ":stype"  << ", " << ":path" << ", " << ":status" << KCloseBrace;
       
   145                 
       
   146 
       
   147         QSqlQuery query(*mDb);
       
   148         query.prepare( queryString );
       
   149 
       
   150         query.bindValue(":sid", aLookupItem.iUid);
       
   151         query.bindValue(":stype", aLookupItem.iSource);
       
   152         query.bindValue(":path", aLookupItem.iFilePath);
       
   153         query.bindValue(":status", aLookupItem.iFetchingStatus);
       
   154         query.exec();
       
   155     }
       
   156 }
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // LookupMapTileDatabase::updateEntry()
       
   160 // Updates an entry in the lookup table.
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void LookupMapTileDatabase::updateEntry( const MaptileLookupItem& aLookupItem )
       
   164 {
       
   165     if( mDbOpen )
       
   166     {
       
   167         QString queryString; //UPDATE maptilelookup SET filepath = ?, status = ? WHERE sourceid = ? AND sourcetype = ?"
       
   168         QTextStream ( &queryString ) <<  
       
   169                 "UPDATE " << KMaptileLookupTable << " SET " <<
       
   170                 KFilePath << " = ?, " << KStatus << " = ? ," << KUserSetting << " = ? " <<
       
   171                 KWhere << 
       
   172                 KSourceId << " = ? " << KAnd  << KSourceType << " = ? ";
       
   173                 
       
   174         QSqlQuery query(*mDb);
       
   175         query.prepare( queryString );
       
   176     
       
   177         query.addBindValue( aLookupItem.iFilePath );
       
   178         query.addBindValue( aLookupItem.iFetchingStatus );   
       
   179         query.addBindValue( false ); 
       
   180         query.addBindValue( aLookupItem.iUid );
       
   181         query.addBindValue( aLookupItem.iSource );
       
   182         query.exec();
       
   183     }
       
   184 }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // LookupMapTileDatabase::deleteEntry()
       
   188 // Deletes an entry from the lookup table.
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void LookupMapTileDatabase::deleteEntry(MaptileLookupItem& aLookupItem)
       
   192 {
       
   193     if( mDbOpen )
       
   194     {
       
   195         QString queryString; // DELETE FROM lplookup WHERE sourceid = ? AND sourcetype = ?"
       
   196         QTextStream ( &queryString ) <<  
       
   197                 "DELETE FROM  " << KMaptileLookupTable <<
       
   198                 KWhere << 
       
   199                 KSourceId << " = ? " << KAnd  << KSourceType << " = ? ";
       
   200         
       
   201         QSqlQuery query(*mDb);
       
   202         query.prepare( queryString );
       
   203         
       
   204         query.addBindValue( aLookupItem.iUid );
       
   205         query.addBindValue( aLookupItem.iSource );
       
   206         
       
   207         query.exec();
       
   208     }
       
   209 }
       
   210 
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // LookupMapTileDatabase::deleteMapTile()
       
   214 // Deletes an maptile if there's no reference to maptile in lookupdb
       
   215 // -----------------------------------------------------------------------------
       
   216 //
       
   217 void LookupMapTileDatabase::deleteMapTile( const MaptileLookupItem& aLookupItem)
       
   218 {
       
   219     if( mDbOpen )
       
   220     {
       
   221         QString queryString; //  "SELECT filepath FROM maptilelookuptable WHERE filepath = ?"
       
   222         QTextStream ( &queryString ) <<  
       
   223                 KSelectAllFrom << KMaptileLookupTable << 
       
   224                 KWhere <<
       
   225                 KFilePath << KEqual << " ? " ;
       
   226         QSqlQuery query(*mDb);
       
   227         query.prepare( queryString );
       
   228         query.addBindValue( aLookupItem.iFilePath );
       
   229         
       
   230         query.exec();
       
   231         
       
   232         // Delete if no reference to maptile
       
   233         if ( !query.first() ) 
       
   234         {
       
   235             //delete all releted  maptile 
       
   236             QString temp = aLookupItem.iFilePath;
       
   237             temp.append(MAPTILE_IMAGE_PORTRAIT);       
       
   238             QFile file;
       
   239             file.remove(temp);
       
   240             
       
   241             temp = aLookupItem.iFilePath;
       
   242             temp.append(MAPTILE_IMAGE_CONTACT);
       
   243             temp.append(MAPTILE_IMAGE_LANDSCAPE);
       
   244             file.remove(temp);
       
   245             
       
   246             temp = aLookupItem.iFilePath;
       
   247             temp.append(MAPTILE_IMAGE_CALENDAR);
       
   248             temp.append(MAPTILE_IMAGE_LANDSCAPE);
       
   249             file.remove(temp);
       
   250             
       
   251             temp = aLookupItem.iFilePath;
       
   252             temp.append(MAPTILE_IMAGE_HURRIGANES);         
       
   253             file.remove(temp);
       
   254             
       
   255         }
       
   256     }
       
   257 }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // LookupMapTileDatabase::findEntriesByMapTileFetchingStatus()
       
   261 // Finds a list of lookup items given a fetching status.
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 void LookupMapTileDatabase::findEntriesByMapTileFetchingState(const quint32 aFetchingState,
       
   265         QList<MaptileLookupItem>& aLookupItemArray)
       
   266 {
       
   267     if( mDbOpen )
       
   268     {
       
   269         QString queryString; //  "SELECT * FROM maptilelookuptable WHERE fetchingstatus = %d"
       
   270         QTextStream ( &queryString ) <<  
       
   271                 KSelectAllFrom << KMaptileLookupTable << 
       
   272                 KWhere <<
       
   273                 KStatus << KEqual << " ? " ;
       
   274         QSqlQuery query(*mDb);
       
   275         query.prepare( queryString );
       
   276         query.addBindValue( aFetchingState );
       
   277         
       
   278         query.exec();
       
   279         
       
   280         while( query.next() )
       
   281         {    
       
   282             QSqlRecord rec = query.record();
       
   283             MaptileLookupItem lookupItem;
       
   284             lookupItem.iUid = query.value( rec.indexOf( KSourceId ) ).toUInt();
       
   285             lookupItem.iSource = query.value( rec.indexOf( KSourceType ) ).toUInt();
       
   286             lookupItem.iFilePath = query.value( rec.indexOf( KFilePath ) ).toString();
       
   287             lookupItem.iFetchingStatus = query.value( rec.indexOf( KStatus ) ).toUInt();
       
   288             lookupItem.iUserSetting = query.value( rec.indexOf( KUserSetting ) ).toBool();
       
   289             aLookupItemArray.append( lookupItem );
       
   290         }
       
   291     }
       
   292 }
       
   293 
       
   294 // -----------------------------------------------------------------------------
       
   295 // LookupMapTileDatabase::getAllCalendarIds()
       
   296 // Gets the list of calendar ids .
       
   297 // -----------------------------------------------------------------------------
       
   298 //
       
   299 void LookupMapTileDatabase::getAllCalendarIds( QList<quint32>& aIdArray )
       
   300 {
       
   301     if( mDbOpen )
       
   302     {
       
   303         QString queryString; //  "SELECT cntuid FROM maptilelookuptable WHERE sourcetype = %d");
       
   304         QTextStream ( &queryString ) <<  
       
   305                 KSelectAllFrom << KMaptileLookupTable << 
       
   306                 KWhere <<
       
   307                 KSourceType << KEqual << " ? " ;
       
   308         QSqlQuery query(*mDb);
       
   309         query.prepare( queryString );
       
   310         query.addBindValue( ESourceCalendar );
       
   311         
       
   312         query.exec();
       
   313         
       
   314         while( query.next() )
       
   315         {    
       
   316             QSqlRecord rec = query.record();
       
   317             quint32 id = query.value( rec.indexOf( KSourceId ) ).toUInt();
       
   318             aIdArray.append( id );
       
   319         }
       
   320     }
       
   321 }
       
   322 
       
   323 
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // LookupMapTileDatabase::resetEntry()
       
   327 // Reset the entry with null value and get the used maptile path as part of aLookupItem.
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 void LookupMapTileDatabase::resetEntry(MaptileLookupItem &aLookupItem)
       
   331 {
       
   332     if( mDbOpen )
       
   333     {
       
   334         // getEntry will replace the fetching status. so copy fetching status to temparory variable. 
       
   335 	      quint32 tempStatus = aLookupItem.iFetchingStatus;
       
   336 	      
       
   337 	      if( getEntry( aLookupItem ) )
       
   338 	      {
       
   339 	      	  // set file path to nullstring
       
   340 	          aLookupItem.iFilePath = "";
       
   341 	          aLookupItem.iFetchingStatus = tempStatus;
       
   342 	          // update entry in db
       
   343 	          updateEntry( aLookupItem );
       
   344 	      }
       
   345 	  }
    60 }
   346 }
    61  
   347  
    62 // -----------------------------------------------------------------------------
   348 // -----------------------------------------------------------------------------
    63 // CLookupMapTileDatabase::~CLookupMapTileDatabase()
   349 // LookupMapTileDatabase::findNumberOfAddress()
    64 // Creates an object of this class and pushes to cleanup stack.
   350 // find the number of address associated with the aId.
    65 // -----------------------------------------------------------------------------
   351 // -----------------------------------------------------------------------------
    66 //
   352 //
    67 CLookupMapTileDatabase* CLookupMapTileDatabase::NewLC( const TDesC& aLookupTableName )
   353 int LookupMapTileDatabase::findNumberOfAddress( int& aId )
    68 {
   354 {
       
   355     int count = 0;
       
   356 
       
   357     if( mDbOpen )
       
   358     {
       
   359         QString queryString; //  "SELECT * FROM maptilelookup WHERE sourceid = aId AND ( fetchingstatus = MapTileFetchingInProgress OR fetchingstatus = MapTileFetchingNetworkError )"
       
   360         QTextStream ( &queryString ) <<  
       
   361                 KSelectAllFrom << KMaptileLookupTable << 
       
   362                 KWhere <<
       
   363                 KSourceId << KEqual << aId << 
       
   364                 KAnd << 
       
   365                 KOpenBrace << KStatus << KEqual << MapTileService::MapTileFetchingInProgress << 
       
   366                               KOr << KStatus << KEqual << MapTileService::MapTileFetchingNetworkError << KCloseBrace ;
       
   367         QSqlQuery query(*mDb);
       
   368         query.exec( queryString );
       
   369         while( query.next() )  count++;
       
   370     }
    69     
   371     
    70     CLookupMapTileDatabase* self = new (ELeave) CLookupMapTileDatabase;
   372     return count;
    71     CleanupStack::PushL(self);
   373 }
    72     self->ConstructL( aLookupTableName );
   374 
    73     return self;
   375 // -----------------------------------------------------------------------------
    74 }
   376 // LookupMapTileDatabase::findEntry()
    75 
   377 // Finds an entry in the lookup table.
    76 
   378 // -----------------------------------------------------------------------------
    77 // -----------------------------------------------------------------------------
   379 //
    78 // CLookupMapTileDatabase::NewL()
   380 bool LookupMapTileDatabase::findEntry( const MaptileLookupItem& aLookupItem )
    79 // Creates an object of this class.
   381 { 
    80 // -----------------------------------------------------------------------------
   382     if( mDbOpen )
    81 //
   383     {
    82 CLookupMapTileDatabase* CLookupMapTileDatabase::NewL( const TDesC& aLookupTableName )
   384         QString queryString; //  "SELECT * FROM maptilelookup WHERE sourceid = aLookupItem.iUid AND sourcetype = aLookupItem.iSource"
    83 {
   385         QTextStream ( &queryString ) <<  
    84     CLookupMapTileDatabase* self = CLookupMapTileDatabase::NewLC( aLookupTableName );
   386                 KSelectAllFrom << KMaptileLookupTable << 
    85     CleanupStack::Pop( self );
   387                 KWhere <<
    86     return self;
   388                 KSourceId << KEqual << aLookupItem.iUid << 
    87 }
   389                 KAnd << 
    88  
   390                 KSourceType << KEqual << aLookupItem.iSource ;
    89 
   391         QSqlQuery query(*mDb);
    90 // -----------------------------------------------------------------------------
   392         query.exec( queryString );
    91 // CLookupMapTileDatabase::ConstructL()
   393         
    92 // 2nd phase contructor.
   394         if ( query.first() ) 
    93 // -----------------------------------------------------------------------------
   395         {
    94 //
   396             return true;
    95 void CLookupMapTileDatabase::ConstructL( const TDesC& aLookupTableName )
   397         }
    96 {
   398     }
    97    
   399     return false;
    98     User::LeaveIfError( iFsSession.Connect() );
   400 }
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // LookupMapTileDatabase::getEntry()
       
   404 // Gets a lookup item from the lookup table using source id and type.
       
   405 // -----------------------------------------------------------------------------
       
   406 //
       
   407 bool LookupMapTileDatabase::getEntry( MaptileLookupItem& aLookupItem )
       
   408 { 
       
   409     if( mDbOpen )
       
   410     {
       
   411         QString queryString; //  "SELECT * FROM maptilelookup WHERE sourceid = aLookupItem.iUid AND sourcetype = aLookupItem.iSource"
       
   412         QTextStream ( &queryString ) <<  
       
   413                 KSelectAllFrom << KMaptileLookupTable << 
       
   414                 KWhere <<
       
   415                 KSourceId << KEqual << aLookupItem.iUid << 
       
   416                 KAnd << 
       
   417                 KSourceType << KEqual << aLookupItem.iSource ;
       
   418         QSqlQuery query(*mDb);
       
   419         query.exec( queryString );
       
   420         
       
   421         if ( query.first() ) 
       
   422         {
       
   423             QSqlRecord rec = query.record();
       
   424             aLookupItem.iFilePath = query.value( rec.indexOf( KFilePath ) ).toString();
       
   425             aLookupItem.iFetchingStatus = query.value( rec.indexOf( KStatus ) ).toUInt();
       
   426             aLookupItem.iUserSetting = query.value( rec.indexOf( KUserSetting ) ).toBool();
       
   427             return true;
       
   428             		
       
   429         }
       
   430     }
       
   431     return false;
       
   432 }
       
   433 
       
   434 // -----------------------------------------------------------------------------
       
   435 // LookupMapTileDatabase::findEntryByFilePath()
       
   436 // Finds an entry in the lookup table for maptile image file
       
   437 // -----------------------------------------------------------------------------
       
   438 //
       
   439 bool LookupMapTileDatabase::findEntryByFilePath( const QString& aFilePath )
       
   440 {
       
   441     if( mDbOpen )
       
   442     {
       
   443         QString queryString; //  "SELECT filepath FROM maptilelookuptable WHERE filepath = ?"
       
   444         QTextStream ( &queryString ) <<  
       
   445                 KSelectAllFrom << KMaptileLookupTable << 
       
   446                 KWhere <<
       
   447                 KFilePath << KEqual << " ? " ;
       
   448         QSqlQuery query(*mDb);
       
   449         query.prepare( queryString );
       
   450         query.addBindValue( aFilePath );
       
   451         
       
   452         query.exec();
       
   453         
       
   454         if( query.first() )
       
   455         {
       
   456             return true;
       
   457         }
       
   458     }
    99     
   459     
   100     iDbFileName.Copy( KLookupDbPath );
   460     return false;
   101     iDbFileName.Append( aLookupTableName );
   461 }
   102     
   462 
   103     iDatabaseExists = EFalse; 
   463 // -----------------------------------------------------------------------------
   104 
   464 // LookupMapTileDatabase::updateUserSetting()
   105     if( BaflUtils::FileExists( iFsSession, iDbFileName ) )
   465 // Updates an entry in the lookup table.
   106     {	
   466 // -----------------------------------------------------------------------------
   107         // database exists 
   467 //
   108         iDatabaseExists = ETrue; 
   468 void LookupMapTileDatabase::updateUserSetting( const MaptileLookupItem& aLookupItem )
   109     }
   469 {
   110 }
   470     if( mDbOpen )
   111 
   471     {
   112 // -----------------------------------------------------------------------------
   472       
   113 // CLookupMapTileDatabase::FindNumberOfAddressL()
   473         QString queryString; 
   114 // find the number of address associated with the aId.
   474         QTextStream ( &queryString ) <<  
   115 // -----------------------------------------------------------------------------
   475                 "UPDATE " << KMaptileLookupTable << " SET " <<
   116 //
   476                     KStatus << " = ? ," << KUserSetting << " = ? " << 
   117 int CLookupMapTileDatabase::FindNumberOfAddressL( int& aId )
   477                 KWhere << 
   118 {
   478                 KSourceId << " = ? " << KAnd  << KSourceType << " = ? ";
   119     int count = 0;
   479                 
   120     
   480         QSqlQuery query(*mDb);
   121     // Create a query to find the item.
   481         query.prepare( queryString );
   122     TFileName queryBuffer;
   482         query.addBindValue( aLookupItem.iFetchingStatus );
   123     queryBuffer.Format( KQueryByMaptileState,aId,
   483         query.addBindValue( aLookupItem.iUserSetting );
   124              MapTileService::MapTileFetchingInProgress,
   484         query.addBindValue( aLookupItem.iUid );
   125              MapTileService::MapTileFetchingNetworkError );
   485         query.addBindValue( aLookupItem.iSource );
   126   
   486         query.exec();
   127     TInt ret = iItemsDatabase.Open( iFsSession, iDbFileName );
       
   128     
       
   129     if( ret != KErrNone )
       
   130     {          
       
   131         //if already opened , close and open again
       
   132         iItemsDatabase.Close();          
       
   133         User::LeaveIfError( iItemsDatabase.Open( iFsSession, iDbFileName ) );
       
   134     }
       
   135     
       
   136     User::LeaveIfError( iItemsDatabase.Begin() );       
       
   137     // Create a view of the table with the above query.
       
   138     RDbView myView;
       
   139     myView.Prepare( iItemsDatabase, TDbQuery( queryBuffer ) );
       
   140     CleanupClosePushL( myView );
       
   141     myView.EvaluateAll();
       
   142     myView.FirstL();
       
   143     
       
   144     
       
   145     while (myView.AtRow())
       
   146     {
       
   147         count++;
       
   148         myView.NextL();
       
   149     }
       
   150     
       
   151     CleanupStack::PopAndDestroy( &myView ); // myView
       
   152          
       
   153     //Close the database
       
   154     iItemsDatabase.Close();
       
   155 
       
   156     return count;
       
   157 }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CLookupMapTileDatabase::FindEntryL()
       
   161 // Finds an entry in the lookup table.
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 void CLookupMapTileDatabase::FindEntryL( TLookupItem& aLookupItem )
       
   165 { 
       
   166    
       
   167     // used to check whether entry available or not
       
   168     TBool entryAvailable = EFalse;
       
   169   
       
   170     if ( iDatabaseExists )
       
   171     {
       
   172 
       
   173         // Create a query to find the item.
       
   174         TFileName queryBuffer;
       
   175         queryBuffer.Copy( KSelectAllFrom );
       
   176         queryBuffer.Append( KMapTileLookupTable );
       
   177         queryBuffer.Append( KStringWhere );
       
   178         queryBuffer.Append( NCntColUid );
       
   179         queryBuffer.Append( KStringEqual );
       
   180         queryBuffer.AppendNum( aLookupItem.iUid );
       
   181         queryBuffer.Append( KStringAnd );
       
   182         queryBuffer.Append( NColSource );
       
   183         queryBuffer.Append( KStringEqual );
       
   184         queryBuffer.AppendNum( aLookupItem.iSource );
       
   185         
       
   186         TInt ret = iItemsDatabase.Open( iFsSession, iDbFileName );
       
   187         
       
   188         if( ret != KErrNone )
       
   189         {          
       
   190             //if already opened , close and open again
       
   191             iItemsDatabase.Close();          
       
   192             User::LeaveIfError( iItemsDatabase.Open( iFsSession, iDbFileName ) );
       
   193         }
       
   194         User::LeaveIfError( iItemsDatabase.Begin() );       
       
   195         // Create a view of the table with the above query.
       
   196         RDbView myView;
       
   197         myView.Prepare( iItemsDatabase, TDbQuery( queryBuffer ) );
       
   198         CleanupClosePushL( myView );
       
   199         myView.EvaluateAll();
       
   200         myView.FirstL();
       
   201     
       
   202         if( myView.AtRow() ) 
       
   203         {  
       
   204             // Item found. get the details.
       
   205             myView.GetL();      
       
   206             if( aLookupItem.iUid == myView.ColUint( KColumnUid ) )
       
   207             {               
       
   208                 aLookupItem.iFilePath.Copy( myView.ColDes16( KColumnFilePath ) );
       
   209                 aLookupItem.iFetchingStatus = myView.ColUint( KColumnMapTileFetchingStatus );
       
   210                 entryAvailable = ETrue;
       
   211             }      
       
   212         } 
       
   213     
       
   214         CleanupStack::PopAndDestroy( &myView ); // myView
       
   215         
       
   216         //Close the database
       
   217         iItemsDatabase.Close();
       
   218     }
       
   219    
       
   220     //No entry found 
       
   221     if( !entryAvailable )
       
   222     {
       
   223         User::Leave( KErrNotFound );
       
   224     }
   487     }
   225 }
   488 }
   226 
   489 
   227 // End of file
   490 // End of file
   228 
   491