//Deletes some records from the test table using "delete" transaction. //Do not put TEST or TEST2 macro calls here (except for record count checks)! //The method must leave if some of the calls inside fail. static void DeleteRecordsL() { RDbTable tbl; CleanupClosePushL(tbl); LEAVE_IF_ERROR(tbl.Open(TheDb, KTestTableName, RDbRowSet::EUpdatable)); TEST(tbl.CountL() == KTestRecordsCount); TheDb.Begin(); tbl.FirstL(); for(TInt i=0;i<(KTestRecordsCount/2);++i) { tbl.DeleteL(); tbl.NextL(); } TInt err = TheDb.Commit(); if(err != KErrNone) { TheDb.Rollback(); LEAVE(err); } TEST(tbl.CountL() == (KTestRecordsCount / 2)); CleanupStack::PopAndDestroy(&tbl); }
// --------------------------------------------------------------------------- // CIRNmsLogDb:AddNmsLogStartL() // adds the NmsLog log entry into data base // --------------------------------------------------------------------------- // void CIRNmsLogDb::AddNmsLogStartL( CIRNmsLogger& aNmsLog ) { IRLOG_DEBUG( "CIRNmsLogDb::AddNmsLogStartL" ); OpenDbL(); RDbTable nmsLogtable; TInt error=nmsLogtable.Open( iNmsLogDb, KNmsLogTable, nmsLogtable. EUpdatable ); CleanupClosePushL( nmsLogtable ); if ( error ) { CloseDb(); User::LeaveIfError( error ); } //! arrange the presets in incresing order of index nmsLogtable.SetIndex( KNmsLogIndex ); nmsLogtable.Reset(); //if NmsLog log is greater or equal to than 5 if ( nmsLogtable.CountL() >= KMaxNoNmsLog ) { //first row is selected nmsLogtable.FirstL(); //the current row is selected nmsLogtable.GetL(); //delete that entry nmsLogtable.DeleteL(); } CleanupStack::PopAndDestroy( &nmsLogtable ); //Algorithm : else condition need not handle seperatly //Algorithm : add NmsLogid and informations like //starttime,connectedfrom,NmsLogid,connectiontype,channelid //currentnetwork,homenetwork,NmsLogtable //Algorithm: if no. of NmsLog is greater than 5 _LIT( query, "SELECT * FROM %S" ); HBufC* sqlStr=HBufC::NewLC( query().Length() + KNmsLogTable().Length() ); sqlStr->Des().Format( query, &KNmsLogTable ); // Create a view on the database RDbView view; error = view.Prepare( iNmsLogDb, *sqlStr ); if ( error ) { CloseDb(); User::LeaveIfError( error ); } CleanupStack::PopAndDestroy( sqlStr ); CleanupClosePushL( view ); error = view.EvaluateAll(); if ( error ) { CloseDb(); User::LeaveIfError( error ); } CDbColSet* columns = view.ColSetL(); CleanupStack::PushL( columns ); RDbColWriteStream writeStream; TRAP( error, //trap start // Insert a row. Column order matches sql select statement view.InsertL(); //get index view.SetColL( columns->ColNo( KID ), aNmsLog.NmsLogId() ); //!open stream writeStream.OpenLC( view, columns->ColNo( KNmsLogCol ) ); aNmsLog.ExternalizeL( writeStream ); writeStream.CommitL(); CleanupStack::PopAndDestroy( &writeStream ); );
/** @SYMTestCaseID SYSLIB-DBMS-CT-0581 @SYMTestCaseDesc Tests the database definition and enquiry functions @SYMTestPriority Medium @SYMTestActions Tests for bookmark which saves the current location of a rowset. @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ LOCAL_C void TestBookmark() { test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0581 creating alien bookmark ")); CDbColSet* cs=CDbColSet::NewLC(); TDbCol col(_L("column"),EDbColUint8); col.iAttributes=TDbCol::ENotNull+TDbCol::EAutoIncrement; cs->AddL(col); test (TheDatabase.CreateTable(KOtherTable,*cs)==KErrNone); CleanupStack::PopAndDestroy(); RDbTable extra; test (extra.Open(TheDatabase,KOtherTable)==KErrNone); extra.InsertL(); extra.PutL(); TDbBookmark alien=extra.Bookmark(); extra.Close(); // test.Next(_L("Alien bookmark")); test (TheTable.Open(TheDatabase,KTableName)==KErrNone); TRAPD(r,TheTable.GotoL(alien)); test (r==KErrNotFound); test (TheTable.SetIndex(KIndexInt)==KErrNone); TRAP(r,TheTable.GotoL(alien)); test (r==KErrNotFound); test (TheTable.SetIndex(KIndexText)==KErrNone); TRAP(r,TheTable.GotoL(alien)); test (r==KErrNotFound); // test.Next(_L("Cross-view bookmarks")); TheTable.LastL(); // indexed view TheTable.PreviousL(); TDbBookmark mark=TheTable.Bookmark(); test (extra.Open(TheDatabase,KTableName)==KErrNone); TRAP(r,extra.GotoL(mark)); test (r==KErrNone); test (extra.PreviousL()); TRAP(r,TheTable.GotoL(extra.Bookmark())); test (r==KErrNone); extra.Close(); // test.Next(_L("Bookmark persistence")); TheTable.Close(); test (TheTable.Open(TheDatabase,KTableName)==KErrNone); TRAP(r,TheTable.GotoL(mark)); test (r==KErrNone); TheTable.Close(); // test.Next(_L("Delete alien record")); test (extra.Open(TheDatabase,KOtherTable)==KErrNone); TRAP(r, extra.GotoL(mark)); test (r==KErrNotFound); TRAP(r,extra.GotoL(alien)); test (r==KErrNone); extra.DeleteL(); TRAP(r,extra.GotoL(alien)); test (r==KErrNotFound); extra.Close(); // test.Next(_L("Delete extra table")); test (TheDatabase.DropTable(KOtherTable)==KErrNone); test (TheTable.Open(TheDatabase,KTableName)==KErrNone); TRAP(r,TheTable.GotoL(alien)); test (r==KErrNotFound); TheTable.Close(); // test.End(); }
// ----------------------------------------------------------------------------- // CLogTask::DoExecuteL // Helper to executes the task. // (other items were commented in a header). // ----------------------------------------------------------------------------- // void CLogTask::DoExecuteL() { RDbs dbSession; User::LeaveIfError( dbSession.Connect() ); CleanupClosePushL( dbSession ); // Construct the db format string HBufC* formatString = HBufC::NewLC( KLogSecureFormat().Length() + KLogAccessPolicyUid.Name().Length() ); TPtr ptr( formatString->Des() ); TUidName uidStr = KLogAccessPolicyUid.Name(); ptr.Format( KLogSecureFormat, &uidStr ); RDbNamedDatabase dbs; // Try to open the db TInt err = dbs.Open( dbSession, KLogDatabaseName, *formatString ); if ( err == KErrNotFound ) { // DB not exist, create it User::LeaveIfError( dbs.Create( dbSession, KLogDatabaseName, *formatString ) ); } else if ( err != KErrNone ) { User::Leave( err ); } CleanupClosePushL( dbs ); // See if the log table already exists TBool needCompact( EFalse ); RDbTable table; err = table.Open( dbs, KLogTableName ); if ( err != KErrNone ) { // Table does not exist // Create the table User::LeaveIfError( dbs.Execute( KLogCreateTableSQL ) ); } else { // Check that we don't have too many rows CleanupClosePushL( table ); if ( table.CountL( RDbTable::EQuick ) >= KMaxLogEntryCount ) { // Delete the first row table.FirstL(); table.DeleteL(); needCompact = ETrue; } CleanupStack::PopAndDestroy( &table ); } // Add entry to the table // Time HBufC* timeString = HBufC::NewLC( KMaxTimeStringLength ); TPtr timePtr( timeString->Des() ); timePtr.Num( iLogEntry.iTime.Int64() ); // Version HBufC* version = ConstructVersionStringLC( iLogEntry.iVersion.iMajor, iLogEntry.iVersion.iMinor, iLogEntry.iVersion.iBuild ); // Construct the sql query HBufC* sqlQuery = HBufC::NewLC( KLogInsertSQLFormat().Length() + KMaxLogNameLength + KMaxLogVendorLength + version->Length() + timeString->Length() + KExtraCharsInSql ); TPtr sqlPtr( sqlQuery->Des() ); sqlPtr.Format( KLogInsertSQLFormat, timeString, iLogEntry.iUid.iUid, &(iLogEntry.iName), &(iLogEntry.iVendor), version, iLogEntry.iAction, iLogEntry.iIsStartup ); // Execute the query User::LeaveIfError( dbs.Execute( *sqlQuery ) ); // Compact the db if needed if ( needCompact ) { dbs.Compact(); } CleanupStack::PopAndDestroy( 6 ); // sqlQuery, version, timeString, dbs, formatString, dbSession }