/** Helper function. Get the next available itemId for the item. Reads all existing ItemIds for the specified DscId, then calculates last+1 or first-1 depending on aPos. Will never return 0 as its reserved to mean not yet persistent. */ TInt CDscDatabase::GetNextItemIdL(TDscPosition aPos, const TUid& aDscId) const { RDbView view; CleanupClosePushL(view); RBuf sqlCmd; CleanupClosePushL(sqlCmd); sqlCmd.CreateL(KSqlQueryAllItemIdsLength); sqlCmd.Format(KSqlQueryAllItemIds, &KItemIdCol, &KItemTable,&KDscIdCol, aDscId, &KItemIdCol ); DebugPrint(sqlCmd); User::LeaveIfError(view.Prepare(iDatabase, sqlCmd)); User::LeaveIfError(view.EvaluateAll()); CleanupStack::PopAndDestroy(&sqlCmd); TInt nextId = 1; //add first item with id=1 and reserve 0 to mean "not yet persistent" if (aPos==ELast && view.LastL()) { //add at ELast: pos =max of itemId+1 view.GetL(); nextId = view.ColInt(1); if(KMaxTInt == nextId) { User::Leave(KErrOverflow); } //increase, make sure to not use 0 as itemid in the database nextId = (-1==nextId) ? (nextId+2) : (nextId+1); } else if (aPos==EFirst && view.FirstL()) { //add at EFirst: pos=min of itemId-1 view.GetL(); nextId = view.ColInt(1); if(KMinTInt == nextId) { User::Leave(KErrUnderflow); } //decrease, but reserve 0 to mean "not yet persistent" nextId = (1==nextId) ? (nextId-2) : (nextId-1); } CleanupStack::PopAndDestroy(&view); return nextId; }
/** @SYMTestCaseID SYSLIB-DBMS-CT-3407 @SYMTestCaseDesc Test for defect DEF103023 - DBMS requires ReadDeviceData and WriteDeviceData capability to read from the db. The current test application has no capabilities at all. "C:TestDB.DB" database is a secure shared database with: - no "READ" polycy (no restrictions apply to the database read operations); - "WRITE" policy with "WriteUserData" capability defined; - "SCHEMA" policy with "NetworkServices" capability defined; - table C has no defined securoty policy, so the database security policy will be used; The current test application should be able to: - begin/commit/rollback a "read-only" transaction; But should fail if: - begin a transaction and try to modify the database within the transaction; This test function asserts the test cases described above. @SYMTestPriority High @SYMTestActions Test for defect DEF103023 - DBMS requires ReadDeviceData and WriteDeviceData capability to read from the db. @SYMTestExpectedResults Test must not fail @SYMDEF DEF103023 */ void DEF103023L() { TheTest.Printf(_L("Begin a transaction. Read-only operations tested\n")); TInt err = TheDb.Begin(); TEST2(err, KErrNone); TheTest.Printf(_L("Perform some read-only operations inside the transaction\n")); err = TheView.Prepare(TheDb, _L("SELECT * FROM C")); TEST2(err, KErrNone); err = TheView.EvaluateAll(); TEST2(err, KErrNone); TInt cnt = TheView.CountL(); TEST(cnt > 0); TBool rc = TheView.FirstL(); TEST(rc); TheView.GetL(); TInt val = TheView.ColInt32(1); rc = TheView.LastL(); TEST(rc); rc = TheView.NextL(); TEST(!rc); rc = TheView.PreviousL(); TEST(rc); TheView.BeginningL(); TheView.EndL(); TheView.Close(); TheTest.Printf(_L("Commit a transaction\n")); err = TheDb.Commit(); TEST2(err, KErrNone); // TheTest.Printf(_L("Begin a transaction. Read-only operations tested\n")); err = TheDb.Begin(); TEST2(err, KErrNone); err = TheView.Prepare(TheDb, _L("SELECT * FROM C")); TEST2(err, KErrNone); err = TheView.EvaluateAll(); TEST2(err, KErrNone); cnt = TheView.CountL(); TEST(cnt > 0); TheView.Close(); TheTest.Printf(_L("Rollback a transaction\n")); TheDb.Rollback(); // TheTest.Printf(_L("Begin a transaction. Tested operations violate the database security\n")); err = TheDb.Begin(); TEST2(err, KErrNone); err = TheView.Prepare(TheDb, _L("SELECT * FROM C")); TEST2(err, KErrNone); err = TheView.EvaluateAll(); TEST2(err, KErrNone); rc = TheView.FirstL(); TEST(rc); TheView.GetL(); TheTest.Printf(_L("An attempt to update a record within the transaction\n")); TRAP(err, TheView.UpdateL()); TEST2(err, KErrPermissionDenied); TheTest.Printf(_L("An attempt to delete a record within the transaction\n")); TRAP(err, TheView.DeleteL()); TEST2(err, KErrPermissionDenied); TheTest.Printf(_L("An attempt to insert a record within the transaction\n")); TRAP(err, TheView.InsertL()); TEST2(err, KErrPermissionDenied); TheView.Close(); TheTest.Printf(_L("An attempt to modify the database schema within the transaction\n")); err = TheDb.Execute(_L("CREATE TABLE C2(Id INTEGER, Z INTEGER)")); TEST2(err, KErrPermissionDenied); TheTest.Printf(_L("An attempt to execute an INSERT statement within the transaction\n")); err = TheDb.Execute(_L("INSERT INTO C VALUES(100)")); TEST2(err, KErrPermissionDenied); TheTest.Printf(_L("An attempt to modify the database within the transaction using RDbUpdate\n")); RDbUpdate update; err = update.Execute(TheDb, _L("INSERT INTO C VALUES(200)")); TEST2(err, KErrPermissionDenied); update.Close(); TheTest.Printf(_L("Rollback a transaction\n")); TheDb.Rollback(); }