internetradio2.0/sessionlogsrc/irsessiondb.cpp
changeset 14 896e9dbc5f19
parent 12 608f67c22514
child 15 065198191975
equal deleted inserted replaced
12:608f67c22514 14:896e9dbc5f19
     1 /*
       
     2 * Copyright (c) 2006-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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bautils.h>
       
    20 
       
    21 #include "irdebug.h" 
       
    22 #include "irsessiondb.h"
       
    23 #include "irsessionloggerutility.h"
       
    24 
       
    25 _LIT(KSessionTable,"Session"); 
       
    26 _LIT(KID,"id");
       
    27 _LIT(KSessionLogCol,  "SessionData");
       
    28 _LIT(KSessionIndex,"SessionIndex");
       
    29 
       
    30 const TInt KMaxNoSession = 15;
       
    31 
       
    32 
       
    33 // ======== LOCAL FUNCTIONS ========
       
    34 
       
    35 
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // Function : NewL()
       
    40 // two phased construction
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43   CIRSessionDb* CIRSessionDb::NewL()
       
    44 	{
       
    45 	IRLOG_DEBUG( "CIRSessionDb::NewL" );
       
    46 	CIRSessionDb* self;
       
    47 	self=CIRSessionDb::NewLC();
       
    48 	CleanupStack::Pop(self);
       
    49 	IRLOG_DEBUG( "CIRSessionDb::NewL - Exiting." );
       
    50 	return self;
       
    51 	}
       
    52  
       
    53  
       
    54 // ---------------------------------------------------------------------------
       
    55 // Function : NewLC()
       
    56 // Two phased construction
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59   CIRSessionDb* CIRSessionDb::NewLC()
       
    60 	{
       
    61 	IRLOG_DEBUG( "CIRSessionDb::NewLC" );
       
    62 	CIRSessionDb *self;
       
    63 	self=new(ELeave)CIRSessionDb;
       
    64 	CleanupStack::PushL(self);
       
    65 	self->ConstructL();
       
    66 	IRLOG_DEBUG( "CIRSessionDb::NewLC - Exiting." );
       
    67 	return self;
       
    68 	}
       
    69  
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CIRSessionDb::~CIRSessionDb()
       
    73 // default destructor
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CIRSessionDb::~CIRSessionDb()
       
    77 	{
       
    78 	IRLOG_DEBUG( "CIRSessionDb::~CIRSessionDb" );
       
    79 	CloseDb();
       
    80 	iFsSession.Close();
       
    81 	IRLOG_DEBUG( "CIRSessionDb::~CIRSessionDb - Exiting." );
       
    82 	}
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // CIRSessionDb::CreateDbConditional()
       
    86 // Creates both the dbms files conditionally(only if not yet created)
       
    87 // calls CreateDbL()
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90  TInt CIRSessionDb::CreateDbConditional(TFileName &aSession)
       
    91 	{
       
    92 	IRLOG_DEBUG( "CIRSessionDb::CreateDbConditional" );
       
    93 	iDbFile.Copy(aSession);
       
    94 	if( !BaflUtils::FileExists(iFsSession, iDbFile) )
       
    95 		{
       
    96 		TRAPD(error,CreateDbL(iDbFile));
       
    97 		if( error )
       
    98 			{
       
    99 			IRLOG_DEBUG( "CIRSessionDb::CreateDbConditional - Exiting (1)." );
       
   100 			return error;
       
   101 			}	
       
   102 		}
       
   103 	IRLOG_DEBUG( "CIRSessionDb::CreateDbConditional - Exiting (2)." );
       
   104 	return KErrNone;
       
   105 	}
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // CIRSessionDb:AddSessionStartL()
       
   109 // adds the session log entry into data base
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void CIRSessionDb::AddSessionStartL(CIRSessionLogger& aSession)
       
   113 	{
       
   114 	IRLOG_DEBUG( "CIRSessionDb::AddSessionStartL" );
       
   115 	OpenDbL();
       
   116 	RDbTable sessionlogtable;
       
   117 	TInt error=sessionlogtable.Open(iSessionDb,KSessionTable,sessionlogtable.
       
   118 		EUpdatable);
       
   119 	CleanupClosePushL(sessionlogtable);
       
   120 	if( error )
       
   121 		{
       
   122 		CloseDb();
       
   123 		User::LeaveIfError(error);	
       
   124 		}
       
   125 	
       
   126 	//! arrange the presets in incresing order of index
       
   127 	sessionlogtable.SetIndex(KSessionIndex);
       
   128 	sessionlogtable.Reset();
       
   129 
       
   130 	//if session log is greater or equal to than 5
       
   131 	if( sessionlogtable.CountL() >= KMaxNoSession )
       
   132 		{
       
   133 		//first row is selected
       
   134 		sessionlogtable.FirstL();
       
   135 		//the current row is selected
       
   136 		sessionlogtable.GetL();
       
   137 		//delete that entry
       
   138 		sessionlogtable.DeleteL();
       
   139 		}	
       
   140     CleanupStack::PopAndDestroy(&sessionlogtable);
       
   141 	//Algorithm : else condition need not handle seperatly
       
   142 	//Algorithm : add sessionid and informations like
       
   143 	//starttime,connectedfrom,sessionid,connectiontype,channelid
       
   144 	//currentnetwork,homenetwork,sessiontable 
       
   145 	//Algorithm: if no. of session is greater than 5
       
   146 
       
   147 	_LIT(query,"SELECT * FROM %S");	
       
   148     HBufC* sqlStr=HBufC::NewLC(query().Length() + KSessionTable().Length());
       
   149 	sqlStr->Des().Format(query,&KSessionTable);
       
   150 	
       
   151 	// Create a view on the database
       
   152     RDbView view;     
       
   153     error = view.Prepare(iSessionDb,*sqlStr);
       
   154     if( error )
       
   155 		{
       
   156 		CloseDb();
       
   157 		User::LeaveIfError(error);	
       
   158 		}
       
   159     CleanupStack::PopAndDestroy(sqlStr);     
       
   160     CleanupClosePushL(view);
       
   161     error = view.EvaluateAll();
       
   162     if( error )
       
   163 		{
       
   164 		CloseDb();
       
   165 		User::LeaveIfError(error);	
       
   166 		}
       
   167     CDbColSet* columns = view.ColSetL();
       
   168     CleanupStack::PushL(columns);
       
   169     
       
   170     RDbColWriteStream writeStream;
       
   171 	TRAP(error,//trap start
       
   172 	   // Insert a row. Column order matches sql select statement
       
   173 	    view.InsertL();
       
   174 		//get index
       
   175 		view.SetColL(columns->ColNo(KID), aSession.SessionId());	
       
   176 		//!open stream
       
   177 		writeStream.OpenLC(view,columns->ColNo(KSessionLogCol));
       
   178 		aSession.ExternalizeL(writeStream);
       
   179 		writeStream.CommitL();
       
   180 		CleanupStack::PopAndDestroy(&writeStream);
       
   181  		);
       
   182  		
       
   183  	CleanupStack::PopAndDestroy(columns);
       
   184  	if( error!=KErrNone )
       
   185 	 	{
       
   186 	 	CloseDb();
       
   187 	 	User::LeaveIfError(error);
       
   188 	 	}
       
   189 	view.PutL();
       
   190  	CleanupStack::PopAndDestroy(&view);
       
   191  	CloseDb();
       
   192  	IRLOG_DEBUG( "CIRSessionDb::AddSessionStartL - Exiting." );
       
   193 	}
       
   194 
       
   195 // ---------------------------------------------------------------------------
       
   196 // CIRSessionDb::GetAllPresetL()
       
   197 // gets all the preset into an array 
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 void CIRSessionDb::GetAllSessionL(
       
   201 	CArrayPtrFlat<CIRSessionLogger>& aSessionDataList)
       
   202 	{
       
   203 	IRLOG_DEBUG( "CIRSessionDb::GetAllSessionL" );
       
   204 	OpenDbL();
       
   205 	//not sure about this resetanddestroy
       
   206 	 //! Open for preset master
       
   207 	aSessionDataList.ResetAndDestroy();	    
       
   208 	//! temp item for holding the retrived data
       
   209 	CIRSessionLogger *item;
       
   210 	RDbColReadStream instream;    
       
   211 	RDbTable table;
       
   212 	TInt error = table.Open(iSessionDb, KSessionTable, table.EReadOnly);
       
   213 	CleanupClosePushL(table);
       
   214 	if( error!=KErrNone )
       
   215 	 	{
       
   216 	 	//if open fails function leaves
       
   217 	 	CloseDb();
       
   218 	 	User::LeaveIfError(error);
       
   219 	 	}
       
   220 
       
   221 	CDbColSet* colSet = table.ColSetL();
       
   222 	CleanupStack::PushL(colSet);
       
   223 
       
   224 	//! arrange the presets in incresing order of index
       
   225 	table.SetIndex(KSessionIndex);
       
   226 	table.Reset();
       
   227 	//! recursively retrive the preset data from the master table
       
   228 	for (table.FirstL(); table.AtRow(); table.NextL())
       
   229 	    {
       
   230 	    item=CIRSessionLogger::NewL();
       
   231 	    CleanupStack::PushL(item);
       
   232 	    table.GetL();
       
   233 	    instream.OpenLC( table, colSet->ColNo( KSessionLogCol ) );
       
   234 		item->InternalizeL(instream);
       
   235 		//update sessionid
       
   236 	    aSessionDataList.AppendL(item);
       
   237 	    CleanupStack::PopAndDestroy(&instream);
       
   238 	    CleanupStack::Pop(item);
       
   239 	    }
       
   240 	    
       
   241 	CleanupStack::PopAndDestroy(colSet);
       
   242 	//!close the master table
       
   243 	CleanupStack::PopAndDestroy(&table);
       
   244 	CloseDb();
       
   245 	IRLOG_DEBUG( "CIRSessionDb::GetAllSessionL - Exiting." );
       
   246 	}
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 // Function : DeleteAllSession
       
   250 // delete all the session from session log entry
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 void CIRSessionDb::DeleteAllSessionL()
       
   254 	{ 
       
   255 	IRLOG_DEBUG( "CIRSessionDb::DeleteAllSessionL" ); 
       
   256 	//opening a data base 
       
   257 	OpenDbL();
       
   258 	RDbTable sessionlogtable;
       
   259 	//data base table opened
       
   260     TInt error=sessionlogtable.Open(iSessionDb,KSessionTable,sessionlogtable.
       
   261     	EUpdatable);
       
   262     CleanupClosePushL(sessionlogtable);
       
   263     if( error!=KErrNone )
       
   264 	 	{
       
   265 	 	//if error we leave
       
   266 	 	CloseDb();
       
   267 	 	User::LeaveIfError(error);
       
   268 	 	}
       
   269 	//data base begin 	
       
   270     error = iSessionDb.Begin();
       
   271     if( error!=KErrNone )
       
   272 	 	{
       
   273 	 	//if open fails function leaves
       
   274 	 	CloseDb();
       
   275 	 	User::LeaveIfError(error);
       
   276 	 	}
       
   277 
       
   278     //! arrange the presets in incresing order of index
       
   279     sessionlogtable.SetIndex(KSessionIndex);
       
   280     sessionlogtable.Reset();
       
   281     sessionlogtable.FirstL();
       
   282     while(sessionlogtable.AtRow())
       
   283 	    {
       
   284 	    //deleting all the rows in the table
       
   285 	    sessionlogtable.GetL();
       
   286 	    sessionlogtable.DeleteL();
       
   287 		sessionlogtable.FirstL();
       
   288 	    }
       
   289 	//saving the change
       
   290 	CleanupStack::PopAndDestroy(&sessionlogtable);
       
   291     iSessionDb.Commit();
       
   292     CloseDb();	
       
   293     IRLOG_DEBUG( "CIRSessionDb::DeleteAllSessionL - Exiting." );
       
   294 	}   
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // CIRSessionDb::ConstructL()
       
   298 // Standard 2nd phase construction
       
   299 // ---------------------------------------------------------------------------
       
   300 //
       
   301 void CIRSessionDb::ConstructL()
       
   302 	{
       
   303 	IRLOG_DEBUG( "CIRSessionDb::ConstructL" );
       
   304 	User::LeaveIfError(iFsSession.Connect());
       
   305 	IRLOG_DEBUG( "CIRSessionDb::ConstructL- Exiting." );	
       
   306 	}
       
   307 
       
   308 // ---------------------------------------------------------------------------
       
   309 // CIRSessionDb::CloseDb()
       
   310 // Closes the database 
       
   311 // ---------------------------------------------------------------------------
       
   312 //
       
   313 void CIRSessionDb::CloseDb()
       
   314 	{
       
   315 	IRLOG_DEBUG( "CIRSessionDb::CloseDb" );
       
   316 	iSessionDb.Close();
       
   317 	IRLOG_DEBUG( "CIRSessionDb::CloseDb - Exiting." );
       
   318 	}
       
   319 
       
   320 
       
   321 // ---------------------------------------------------------------------------
       
   322 // Function : CreateSessionTableL
       
   323 // creates sessionlogtable with two column one is sessionid and rest of session
       
   324 // log data
       
   325 // ---------------------------------------------------------------------------
       
   326 //SessionTable
       
   327 //---------------------------
       
   328 //| KID   |	 KSessionLogCol |
       
   329 //---------------------------
       
   330 //|TInt32 | EDbColLongText8 |
       
   331 //---------------------------
       
   332 //
       
   333 void CIRSessionDb::CreateSessionTableL()
       
   334 	{
       
   335 	IRLOG_DEBUG( "CIRSessionDb::CreateSessionTableL" );
       
   336 	//start time of session
       
   337 	TDbCol sessionid(KID, EDbColInt32);
       
   338 	sessionid.iAttributes = TDbCol::ENotNull; 
       
   339 	
       
   340     //!this column is used to store preset data
       
   341     //!The column stores a potentially large amount of non-Unicode text data.
       
   342     TDbCol sessionlogcol(KSessionLogCol, EDbColLongText8);
       
   343    	sessionlogcol.iAttributes = TDbCol::ENotNull; 
       
   344    	
       
   345 	CDbColSet* sessionlogcolset = CDbColSet::NewLC();
       
   346 	sessionlogcolset->AddL(sessionid);
       
   347 	sessionlogcolset->AddL(sessionlogcol);
       
   348 
       
   349 	// Create the sessionlog table with two columns
       
   350 	
       
   351 	User::LeaveIfError(iSessionDb.CreateTable(KSessionTable,
       
   352 		 *sessionlogcolset));
       
   353 	
       
   354 	//Create the KeyIndex for the table
       
   355 	CreateSessionIndexL();
       
   356 	CleanupStack::PopAndDestroy(sessionlogcolset);
       
   357 	IRLOG_DEBUG( "CIRSessionDb::CreateSessionTableL - Exiting." );	
       
   358 	}
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // Function : CreateSessionIndexL
       
   362 // sets sessionid as the primary key
       
   363 // ---------------------------------------------------------------------------
       
   364 //
       
   365 void CIRSessionDb::CreateSessionIndexL()
       
   366 	{
       
   367 	IRLOG_DEBUG( "CIRSessionDb::CreateSessionIndexL" );
       
   368 	TDbKeyCol sessionid(KID);
       
   369 	CDbKey* index = CDbKey::NewLC();
       
   370 	index->AddL(sessionid);
       
   371 	User::LeaveIfError(iSessionDb.CreateIndex( KSessionIndex, KSessionTable,
       
   372 		*index));
       
   373 	CleanupStack::PopAndDestroy(index);
       
   374 	IRLOG_DEBUG( "CIRSessionDb::CreateSessionIndexL - Exiting." );
       
   375 	}
       
   376 	
       
   377 // ---------------------------------------------------------------------------
       
   378 // CIRSessionDb::OpenDbL()
       
   379 // opening the data base 
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382  void CIRSessionDb::OpenDbL()
       
   383 	{
       
   384 	IRLOG_DEBUG( "CIRSessionDb::OpenDbL" );
       
   385 	CloseDb();
       
   386 	TInt error = KErrNone;
       
   387 	if( !BaflUtils::FileExists(iFsSession, iDbFile) )
       
   388 	    {
       
   389 	    //if file doesn't exist function leaves with error code
       
   390 	    //KErrNotFound
       
   391 	    error = KErrNotFound;
       
   392 	    User::LeaveIfError(error);
       
   393 	    }
       
   394 
       
   395 	error = iSessionDb.Open(iFsSession,iDbFile);	
       
   396 	if( error!=KErrNone )
       
   397 		{
       
   398 		//if database is failed to open then 
       
   399 		//function leaves
       
   400 		IRLOG_ERROR2( "CIRSessionDb::OpenDbL - Opening session database failed (%d)", error );
       
   401 		User::LeaveIfError(error);
       
   402 		}
       
   403 	if( iSessionDb.IsDamaged() || !iSessionDb.InTransaction() )
       
   404 		{
       
   405 		//if data base is damaged then 
       
   406 		//it tried to recover
       
   407 		//if recovery is not possible function leaves
       
   408 		error = iSessionDb.Recover();
       
   409 		if ( KErrNone == error)
       
   410 			{
       
   411 			//if recovered data base is compacted
       
   412 			error = iSessionDb.Compact();	
       
   413 			}
       
   414 	    User::LeaveIfError(error);	
       
   415 		}
       
   416 	IRLOG_DEBUG( "CIRSessionDb::OpenDbL - Exiting." );
       
   417 	}
       
   418 	
       
   419 
       
   420 // ---------------------------------------------------------------------------
       
   421 // CIRSessionDb::CreateDbL()
       
   422 // Creates both the dbms files 
       
   423 // calls CreateFavMasterTableL(),CreateFavUrlTableL
       
   424 // database filename
       
   425 // ---------------------------------------------------------------------------
       
   426 //
       
   427  void CIRSessionDb::CreateDbL(TFileName& aSession)
       
   428 	{
       
   429 	IRLOG_DEBUG( "CIRSessionDb::CreateDbL" );
       
   430 	CloseDb();
       
   431 	TInt error = iSessionDb.Replace(iFsSession,aSession);
       
   432 	if ( error != KErrNone )
       
   433 	    {
       
   434 	    IRLOG_ERROR2( "CIRSessionDb::CreateDbL - Creating session database failed (%d)", error );
       
   435 	    }
       
   436 	User::LeaveIfError(error);
       
   437 	CreateSessionTableL();
       
   438 	IRLOG_DEBUG( "CIRSessionDb::CreateDbL - Exiting." );
       
   439 	}
       
   440 
       
   441 
       
   442 
       
   443 
       
   444 
       
   445 
       
   446 
       
   447 
       
   448 
       
   449