void CAlmSettingsServer::ConstructL(void)
{
  iBackupSession=CBaBackupSessionWrapper::NewL();
  iBackupSession->RegisterBackupOperationObserverL(*this);
  User::LeaveIfError(iSession.Connect());
  DbOpenL();
  StartL(KSettingsServerName);
}
void CAlmSettingsServer::LockNotifyL(TBool aState)
{
  if(aState!=iLock)
  {
    if(aState) DbClose();
    else if(!iBackup)
    {
      User::After(1000000);
      DbOpenL();
    }
    iLock=aState;
  }
}
void CAlmSettingsServer::HandleBackupOperationEventL(const TBackupOperationAttributes& aBackupOperationAttributes)
{
  TBool state=EFalse;
  if(aBackupOperationAttributes.iOperation==EStart) state=ETrue;
  if(state!=iBackup)
  {
    if(state) DbClose();
    else if(!iLock)
    {
      DbOpenL();
    }
    iBackup=state;
  }
}
LOCAL_C void WriteTableL()
	{
	DbOpenL();
	TInt r=TheTable.Open(TheDatabase,TableName);
	test (r==KErrNone);
	TheDatabase.Begin();
	for (TInt ii=0;ii<KRowCount;++ii)
		{
		TheTable.InsertL();
		TheTable.SetColL(1,TUint((ii*17)%KRowCount));
		TheTable.PutL();
		}
	r=TheDatabase.Commit();
	test (r==KErrNone);
	TheTable.Close();
	TheDatabase.Close();
	}
LOCAL_C void DatabaseL()
	{
	test.Start(_L("Adding and dropping tables"));
	DbCreateL();
// ensure the database locking list has been allocated
	TheDatabase.Begin();
	TheDatabase.Commit();
//
	CDbColSet *col=CDbColSet::NewLC();
//
	test.Next(_L("Empty Column Set"));
	__UHEAP_MARK;
	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
	__UHEAP_MARKEND;
//
	test.Next(_L("Invalid table name"));
	col->AddL(TDbCol(Column1,EDbColInt32));
	__UHEAP_MARK;
	test(TheDatabase.CreateTable(TableNameX,*col)!=KErrNone);
	__UHEAP_MARKEND;
//
	test.Next(_L("Invalid column name"));
	col->AddL(TDbCol(Column2X,EDbColBit));
	__UHEAP_MARK;
	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
	__UHEAP_MARKEND;
//
	test.Next(_L("Duplicate column name"));
	col->Remove(Column2X);
	col->AddL(TDbCol(Column1Fold,EDbColBit));
	__UHEAP_MARK;
	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
	__UHEAP_MARKEND;
//
	test.Next(_L("Invalid column type"));
	col->Remove(Column1);
	col->AddL(TDbCol(Column2,TDbColType(-1)));
	__UHEAP_MARK;
	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
	__UHEAP_MARKEND;
//
	test.Next(_L("Invalid maximum length"));
	col->Remove(Column2);
	col->AddL(TDbCol(Column2,EDbColInt32,0));
	__UHEAP_MARK;
	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
	__UHEAP_MARKEND;
//
	test.Next(_L("Invalid attributes"));
	col->Remove(Column2);
	TDbCol cc(Column2,EDbColInt32);
	cc.iAttributes=13;
	col->AddL(cc);
	__UHEAP_MARK;
	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
	__UHEAP_MARKEND;
//
	test.Next(_L("Adding/dropping a table name twice"));
	col->Remove(Column2);
	col->AddL(TDbCol(Column2,EDbColText8));
	__UHEAP_MARK;
	test(TheDatabase.CreateTable(TableName,*col)==KErrNone);
	test(TheDatabase.CreateTable(TableName,*col)==KErrAlreadyExists);
	test(TheDatabase.DropTable(TableNameX)!=KErrNone);
	test(TheDatabase.DropTable(TableName)==KErrNone);
	test(TheDatabase.DropTable(TableName)==KErrNotFound);
	__UHEAP_MARKEND;
//
	test.Next(_L("Adding and dropping indexes"));
	test(TheDatabase.CreateTable(TableName,*col)==KErrNone);
	TheDatabase.Close();
	CDbKey *key=CDbKey::NewLC();
	__UHEAP_MARK;
	DbOpenL();
	test(TheDatabase.CreateIndex(IndexName,TableName,*key)!=KErrNone);
	TheDatabase.Close();
	__UHEAP_MARKEND;
	key->AddL(Column2X());
	__UHEAP_MARK;
	DbOpenL();
	test(TheDatabase.CreateIndex(IndexName,TableName,*key)!=KErrNone);
	TheDatabase.Close();
	__UHEAP_MARKEND;
	key->Clear();
	key->AddL(Column1());
	__UHEAP_MARK;
	DbOpenL();
	test(TheDatabase.CreateIndex(TableNameX,TableName,*key)!=KErrNone);
	TheDatabase.Close();
	__UHEAP_CHECK(0);
	DbOpenL();
	test(TheDatabase.CreateIndex(IndexName,TableNameX,*key)!=KErrNone);
	TheDatabase.Close();
	__UHEAP_MARKEND;
	__UHEAP_MARK;
	DbOpenL();
	test(TheDatabase.CreateIndex(IndexName,TableName,*key)==KErrNone);
	test(TheDatabase.CreateIndex(IndexName,TableName,*key)==KErrAlreadyExists);
	test(TheDatabase.DropIndex(TableNameX,TableName)!=KErrNone);
	test(TheDatabase.DropIndex(IndexName,TableNameX)!=KErrNone);
	test(TheDatabase.DropIndex(IndexName,TableName)==KErrNone);
	test(TheDatabase.DropIndex(IndexName,TableName)==KErrNotFound);
	test(TheDatabase.DropTable(TableName)==KErrNone);
	test(TheDatabase.DropIndex(IndexName,TableName)==KErrNotFound);
	TheDatabase.Close();
	__UHEAP_MARKEND;
//
	test.Next(_L("Allocation failure during DDL"));
	TFailCreateTable fct;
	TFailAlterTable fat;
	TFailDropTable fdt;
	TFailCreateIndex fci;
	TFailDropIndex fdi;
	TheColSet=CDbColSet::NewL();
	TheColSet->AddL(TDbCol(Column1,EDbColUint16));
	TheKey=CDbKey::NewL();
	TheKey->AddL(Column1());
	fct.Test(KClientHeap,KClient);
	WriteTableL();
	TheColSet->AddL(TDbCol(Column2,EDbColText));
	fat.Test(KClientHeap,KClient);
	fci.Test(KClientHeap,KClient);
	fdi.Test(KClientHeap,KClient);
	fdt.Test(KClientHeap,KClient);
//
	test.Next(_L("Allocation failure during server DDL"));
	Connect();
	TheColSet->Remove(Column2);
	fct.Test(KClientHeap,KServer);
	WriteTableL();
	TheColSet->AddL(TDbCol(Column2,EDbColText));
	fat.Test(KClientHeap,KServer);
	fci.Test(KClientHeap,KServer);
	fdi.Test(KClientHeap,KServer);
	fdt.Test(KClientHeap,KServer);
//
	TheColSet->Remove(Column2);
	fct.Test(KServerHeap,KServer);
	WriteTableL();
	TheColSet->AddL(TDbCol(Column2,EDbColText));
	fat.Test(KServerHeap,KServer);
	fci.Test(KServerHeap,KServer);
	fdi.Test(KServerHeap,KServer);
	fdt.Test(KServerHeap,KServer);
	Disconnect();
//
	delete TheColSet;
	delete TheKey;

//
	test.Next(_L("Allocation failure on schema enquiry"));
	DbCreateL();
	test(TheDatabase.CreateTable(TableName,*col)==KErrNone);
	test(TheDatabase.CreateIndex(IndexName,TableName,*key)==KErrNone);
	CleanupStack::PopAndDestroy(2);	// columns set and key
	TheDatabase.Close();
	TFailDatabaseTables t4;
	TFailDatabaseColSet t5;
	TFailDatabaseIndexes t6;
	TFailDatabaseKeys t7;
	t4.Test(KClientHeap,KClient);
	t5.Test(KClientHeap,KClient);
	t6.Test(KClientHeap,KClient);
	t7.Test(KClientHeap,KClient);
//
	test.Next(_L("Allocation failure on server schema enquiry"));
	Connect();
	t4.Test(KClientHeap,KServer);
	t4.Test(KServerHeap,KServer);
	t5.Test(KClientHeap,KServer);
	t5.Test(KServerHeap,KServer);
	t6.Test(KClientHeap,KServer);
	t6.Test(KServerHeap,KServer);
	t7.Test(KClientHeap,KServer);
	t7.Test(KServerHeap,KServer);
	Disconnect();
	test.End();
	}
	void RunL()
		{DbOpenL();}
	void OpenDbL() const
		{DbOpenL();}