os/persistentdata/persistentstorage/dbms/tdbms/t_dbalter.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <d32dbms.h>
sl@0
    17
#include <s32file.h>
sl@0
    18
#include <e32test.h>
sl@0
    19
sl@0
    20
LOCAL_D RTest test(_L("t_dbalter : Test AlterTable"));
sl@0
    21
LOCAL_D CTrapCleanup* TheTrapCleanup;
sl@0
    22
LOCAL_D CFileStore* TheStore;
sl@0
    23
LOCAL_D RDbStoreDatabase TheDatabase;
sl@0
    24
LOCAL_D RDbTable TheTable;
sl@0
    25
LOCAL_D RFs TheFs;
sl@0
    26
sl@0
    27
const TInt KTestCleanupStack=0x20;
sl@0
    28
const TPtrC KTestDir=_L("C:\\DBMS-TST\\");
sl@0
    29
const TPtrC KTestFile=_L("T_ALTER.DB");
sl@0
    30
const TPtrC KTableName(_S("Table"));
sl@0
    31
const TPtrC KTableName2(_S("Table2"));
sl@0
    32
const TPtrC KIndexName(_S("Index"));
sl@0
    33
sl@0
    34
TInt KRecords=100;
sl@0
    35
sl@0
    36
const TUint KCol1Data=0;
sl@0
    37
const TInt KCol2Data=2;
sl@0
    38
const TPtrC KCol3Data=_L("three");
sl@0
    39
const TUint8 _Col4Data[80]={4,4,4,4,0,0xff,2,2,1};
sl@0
    40
const TPtrC8 KCol4Data(_Col4Data,sizeof(_Col4Data));
sl@0
    41
const TUint KCol5Data=1;
sl@0
    42
const TInt KCol6Data=5;
sl@0
    43
const TPtrC KCol7Data=_L("six");
sl@0
    44
const TPtrC KCol8Data=_L("column number eight = #8");
sl@0
    45
const TUint8 _Col9Data[400]={1,2,3,4,5,6,7,8,9,10};
sl@0
    46
const TPtrC8 KCol9Data(_Col9Data,sizeof(_Col9Data));
sl@0
    47
sl@0
    48
const TText* const KColumn1=_S("c1");
sl@0
    49
const TText* const KColumn2=_S("c2");
sl@0
    50
const TText* const KColumn3=_S("c3");
sl@0
    51
const TText* const KColumn4=_S("c4");
sl@0
    52
const TText* const KColumn5=_S("c5");
sl@0
    53
const TText* const KColumn6=_S("c6");
sl@0
    54
const TText* const KColumn7=_S("c7");
sl@0
    55
const TText* const KColumn8=_S("c8");
sl@0
    56
const TText* const KColumn9=_S("c9");
sl@0
    57
const TText* const KColumn10=_S("c10");
sl@0
    58
const TText* const KColumn11=_S("c11");
sl@0
    59
const TPtrC KColumns[]=
sl@0
    60
	{
sl@0
    61
	KColumn1,
sl@0
    62
	KColumn2,
sl@0
    63
	KColumn3,
sl@0
    64
	KColumn4,
sl@0
    65
	KColumn5,
sl@0
    66
	KColumn6,
sl@0
    67
	KColumn7,
sl@0
    68
	KColumn8,
sl@0
    69
	KColumn9
sl@0
    70
	};
sl@0
    71
sl@0
    72
class Set
sl@0
    73
	{
sl@0
    74
public:
sl@0
    75
	struct SColDef
sl@0
    76
		{
sl@0
    77
		const TText* iName;
sl@0
    78
		TDbColType iType;
sl@0
    79
		TInt iAttributes;
sl@0
    80
		TInt iMaxLength;
sl@0
    81
		};
sl@0
    82
	static SColDef const Basic[];
sl@0
    83
	static SColDef const Bad[];
sl@0
    84
	static SColDef const Incompatible1[];
sl@0
    85
	static SColDef const Incompatible2[];
sl@0
    86
	static SColDef const Incompatible3[];
sl@0
    87
	static SColDef const Different[];
sl@0
    88
	static SColDef const Extended[];
sl@0
    89
	static SColDef const LongerText[];
sl@0
    90
	static SColDef const TextToLongText[];
sl@0
    91
	static SColDef const Column3[];
sl@0
    92
	static SColDef const DropSome[];
sl@0
    93
	static SColDef const DropAndAdd[];
sl@0
    94
public:
sl@0
    95
	static CDbColSet* CreateL(const SColDef* aDef);
sl@0
    96
	};
sl@0
    97
// the basic column definition
sl@0
    98
enum TCol {EBit,EInt,EText,ELong,EBitNull,EIntNull,ETextNull,ELongNull,EExtra};
sl@0
    99
Set::SColDef const Set::Basic[]=
sl@0
   100
	{
sl@0
   101
	{KColumn1,EDbColBit,TDbCol::ENotNull,-1},
sl@0
   102
	{KColumn2,EDbColInt32,TDbCol::ENotNull,-1},
sl@0
   103
	{KColumn3,EDbColText,TDbCol::ENotNull,-1},
sl@0
   104
	{KColumn4,EDbColLongBinary,TDbCol::ENotNull,-1},
sl@0
   105
	{KColumn5,EDbColBit,0,-1},
sl@0
   106
	{KColumn6,EDbColInt32,0,-1},
sl@0
   107
	{KColumn7,EDbColText,0,-1},
sl@0
   108
	{KColumn8,EDbColText,0,50},
sl@0
   109
	{0}
sl@0
   110
	};
sl@0
   111
// a basically invalid set
sl@0
   112
Set::SColDef const Set::Bad[]=
sl@0
   113
	{
sl@0
   114
	{KColumn9,EDbColInt32,0,-1},
sl@0
   115
	{KColumn9,EDbColInt32,0,-1},
sl@0
   116
	{0}
sl@0
   117
	};
sl@0
   118
// an incompatible set with Basic
sl@0
   119
Set::SColDef const Set::Incompatible1[]=
sl@0
   120
	{
sl@0
   121
	{KColumn1,EDbColInt32,TDbCol::ENotNull,-1},	// retype a column
sl@0
   122
	{0}
sl@0
   123
	};
sl@0
   124
Set::SColDef const Set::Incompatible2[]=
sl@0
   125
	{
sl@0
   126
	{KColumn5,EDbColBit,TDbCol::ENotNull,-1},	// change attributes
sl@0
   127
	{0}
sl@0
   128
	};
sl@0
   129
Set::SColDef const Set::Incompatible3[]=
sl@0
   130
	{
sl@0
   131
	{KColumn8,EDbColText,0,49},	// shrink a text column
sl@0
   132
	{0}
sl@0
   133
	};
sl@0
   134
// a wildly different set
sl@0
   135
Set::SColDef const Set::Different[]=
sl@0
   136
	{
sl@0
   137
	{KColumn11,EDbColInt32,0,-1},
sl@0
   138
	{KColumn4,EDbColLongBinary,TDbCol::ENotNull,-1},
sl@0
   139
	{KColumn10,EDbColBit,TDbCol::ENotNull,-1},
sl@0
   140
	{KColumn3,EDbColText,TDbCol::ENotNull,-1},
sl@0
   141
	{0}
sl@0
   142
	};
sl@0
   143
// basic + 1 column
sl@0
   144
Set::SColDef const Set::Extended[]=
sl@0
   145
	{
sl@0
   146
	{KColumn1,EDbColBit,TDbCol::ENotNull,-1},
sl@0
   147
	{KColumn2,EDbColInt32,TDbCol::ENotNull,-1},
sl@0
   148
	{KColumn3,EDbColText,TDbCol::ENotNull,-1},
sl@0
   149
	{KColumn4,EDbColLongBinary,TDbCol::ENotNull,-1},
sl@0
   150
	{KColumn5,EDbColBit,0,-1},
sl@0
   151
	{KColumn6,EDbColInt32,0,-1},
sl@0
   152
	{KColumn7,EDbColText,0,-1},
sl@0
   153
	{KColumn8,EDbColText,0,50},
sl@0
   154
	{KColumn9,EDbColLongBinary,0,-1},		// add this column
sl@0
   155
	{0}
sl@0
   156
	};
sl@0
   157
// Extended with a longer text column
sl@0
   158
Set::SColDef const Set::LongerText[]=
sl@0
   159
	{
sl@0
   160
	{KColumn1,EDbColBit,TDbCol::ENotNull,-1},
sl@0
   161
	{KColumn2,EDbColInt32,TDbCol::ENotNull,-1},
sl@0
   162
	{KColumn3,EDbColText,TDbCol::ENotNull,-1},
sl@0
   163
	{KColumn4,EDbColLongBinary,TDbCol::ENotNull,-1},
sl@0
   164
	{KColumn5,EDbColBit,0,-1},
sl@0
   165
	{KColumn6,EDbColInt32,0,-1},
sl@0
   166
	{KColumn7,EDbColText,0,-1},
sl@0
   167
	{KColumn8,EDbColText,0,51},			// longer definition
sl@0
   168
	{KColumn9,EDbColLongBinary,0,-1},
sl@0
   169
	{0}
sl@0
   170
	};
sl@0
   171
// Extended with a text->LongText column
sl@0
   172
Set::SColDef const Set::TextToLongText[]=
sl@0
   173
	{
sl@0
   174
	{KColumn1,EDbColBit,TDbCol::ENotNull,-1},
sl@0
   175
	{KColumn2,EDbColInt32,TDbCol::ENotNull,-1},
sl@0
   176
	{KColumn3,EDbColText,TDbCol::ENotNull,-1},
sl@0
   177
	{KColumn4,EDbColLongBinary,TDbCol::ENotNull,-1},
sl@0
   178
	{KColumn5,EDbColBit,0,-1},
sl@0
   179
	{KColumn6,EDbColInt32,0,-1},
sl@0
   180
	{KColumn7,EDbColText,0,-1},
sl@0
   181
	{KColumn8,EDbColLongText,0,-1},		// longer still
sl@0
   182
	{KColumn9,EDbColLongBinary,0,-1},
sl@0
   183
	{0}
sl@0
   184
	};
sl@0
   185
Set::SColDef const Set::Column3[]=
sl@0
   186
	{
sl@0
   187
	{KColumn3,EDbColText,TDbCol::ENotNull,-1},
sl@0
   188
	{0}
sl@0
   189
	};
sl@0
   190
Set::SColDef const Set::DropSome[]=
sl@0
   191
	{
sl@0
   192
	{KColumn1,EDbColBit,TDbCol::ENotNull,-1},
sl@0
   193
	{KColumn2,EDbColInt32,TDbCol::ENotNull,-1},
sl@0
   194
	{KColumn6,EDbColInt32,0,-1},
sl@0
   195
	{KColumn7,EDbColText,0,-1},
sl@0
   196
	{0}
sl@0
   197
	};
sl@0
   198
Set::SColDef const Set::DropAndAdd[]=
sl@0
   199
	{
sl@0
   200
	{KColumn2,EDbColInt32,TDbCol::ENotNull,-1},
sl@0
   201
	{KColumn7,EDbColText,0,-1},
sl@0
   202
	{KColumn10,EDbColBinary,0,-1},
sl@0
   203
	{0}
sl@0
   204
	};
sl@0
   205
sl@0
   206
CDbColSet* Set::CreateL(const SColDef* aDef)
sl@0
   207
	{
sl@0
   208
	CDbColSet *set=CDbColSet::NewLC();
sl@0
   209
	for (;aDef->iName;++aDef)
sl@0
   210
		{
sl@0
   211
		TDbCol col(TPtrC(aDef->iName),aDef->iType);
sl@0
   212
		col.iAttributes=aDef->iAttributes;
sl@0
   213
		if (aDef->iMaxLength>=0)
sl@0
   214
			col.iMaxLength=aDef->iMaxLength;
sl@0
   215
		set->AddL(col);
sl@0
   216
		}
sl@0
   217
	CleanupStack::Pop();
sl@0
   218
	return set;
sl@0
   219
	}
sl@0
   220
sl@0
   221
//
sl@0
   222
// Create the database-in-a-store
sl@0
   223
//
sl@0
   224
LOCAL_C void CreateDatabaseL()
sl@0
   225
	{
sl@0
   226
	CFileStore* store=CPermanentFileStore::ReplaceLC(TheFs,KTestFile,EFileRead|EFileWrite);
sl@0
   227
	store->SetTypeL(KPermanentFileStoreLayoutUid);
sl@0
   228
	TStreamId id;
sl@0
   229
	    id=TheDatabase.CreateL(store);
sl@0
   230
	store->SetRootL(id);
sl@0
   231
	store->CommitL();
sl@0
   232
	CleanupStack::Pop();
sl@0
   233
	TheStore=store;
sl@0
   234
	}
sl@0
   235
sl@0
   236
//
sl@0
   237
// Open the database-in-a-store
sl@0
   238
//
sl@0
   239
LOCAL_C void OpenDatabaseL()
sl@0
   240
	{
sl@0
   241
	CFileStore* store=CFileStore::OpenLC(TheFs,KTestFile,EFileRead|EFileWrite);
sl@0
   242
	TStreamId id=store->Root();
sl@0
   243
	    TheDatabase.OpenL(store,id);
sl@0
   244
	CleanupStack::Pop();
sl@0
   245
	TheStore=store;
sl@0
   246
	}
sl@0
   247
sl@0
   248
LOCAL_C void CloseDatabaseL()
sl@0
   249
	{
sl@0
   250
	TheDatabase.Close();
sl@0
   251
	delete TheStore;
sl@0
   252
	}
sl@0
   253
sl@0
   254
LOCAL_C void DestroyDatabaseL()
sl@0
   255
	{
sl@0
   256
	TheDatabase.Destroy();
sl@0
   257
	TheStore->CommitL();
sl@0
   258
	delete TheStore;
sl@0
   259
	}
sl@0
   260
sl@0
   261
LOCAL_C CDbColSet* TableDefinitionL(const TDesC& aTable)
sl@0
   262
	{
sl@0
   263
	RDbTable table;
sl@0
   264
	test(table.Open(TheDatabase,aTable,table.EReadOnly)==KErrNone);
sl@0
   265
	CDbColSet* cs=table.ColSetL();
sl@0
   266
	table.Close();
sl@0
   267
	return cs;
sl@0
   268
	}
sl@0
   269
sl@0
   270
//
sl@0
   271
// Compare two column sets
sl@0
   272
//
sl@0
   273
LOCAL_C void Compare(const CDbColSet& aLeft,const CDbColSet& aRight)
sl@0
   274
	{
sl@0
   275
	test(aLeft.Count()==aRight.Count());
sl@0
   276
	for (TDbColSetIter iter(aLeft);iter;++iter)
sl@0
   277
		{
sl@0
   278
		const TDbCol* pRight=aRight.Col(iter->iName);
sl@0
   279
		test(pRight!=NULL);
sl@0
   280
		test(iter->iType==pRight->iType);
sl@0
   281
		test(iter->iMaxLength==KDbUndefinedLength || pRight->iMaxLength==KDbUndefinedLength || iter->iMaxLength==pRight->iMaxLength);
sl@0
   282
		test((iter->iAttributes&pRight->iAttributes)==iter->iAttributes);
sl@0
   283
		}
sl@0
   284
	}
sl@0
   285
sl@0
   286
/**
sl@0
   287
@SYMTestCaseID          SYSLIB-DBMS-CT-0575
sl@0
   288
@SYMTestCaseDesc        Store database test
sl@0
   289
                        Test for altering the table with different column definitions
sl@0
   290
@SYMTestPriority        Medium
sl@0
   291
@SYMTestActions        	Test for RDbStoreDatabase::AlterTable(),RDbStoreDatabase::DropIndex()
sl@0
   292
@SYMTestExpectedResults Test must not fail
sl@0
   293
@SYMREQ                 REQ0000
sl@0
   294
*/
sl@0
   295
LOCAL_C void TestEmptyTableL()
sl@0
   296
	{
sl@0
   297
	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0575 Create table "));
sl@0
   298
	CreateDatabaseL();
sl@0
   299
	CDbColSet* set=Set::CreateL(Set::Basic);
sl@0
   300
	test(TheDatabase.CreateTable(KTableName,*set)==KErrNone);
sl@0
   301
	test.Next(_L("Alter non existant table"));
sl@0
   302
	test(TheDatabase.AlterTable(KTableName2,*set)==KErrNotFound);
sl@0
   303
	delete set;
sl@0
   304
//
sl@0
   305
	test.Next(_L("Alter to bad definitions"));
sl@0
   306
	set=Set::CreateL(Set::Bad);
sl@0
   307
	test(TheDatabase.AlterTable(KTableName,*set)!=KErrNone);
sl@0
   308
	delete set;
sl@0
   309
	set=Set::CreateL(Set::Incompatible1);
sl@0
   310
	test(TheDatabase.AlterTable(KTableName,*set)!=KErrNone);
sl@0
   311
	delete set;
sl@0
   312
	set=Set::CreateL(Set::Incompatible2);
sl@0
   313
	test(TheDatabase.AlterTable(KTableName,*set)!=KErrNone);
sl@0
   314
	delete set;
sl@0
   315
	set=Set::CreateL(Set::Incompatible3);
sl@0
   316
	test(TheDatabase.AlterTable(KTableName,*set)!=KErrNone);
sl@0
   317
	delete set;
sl@0
   318
//
sl@0
   319
	test.Next(_L("Drop an indexed column"));
sl@0
   320
	CDbKey* key=CDbKey::NewLC();
sl@0
   321
	key->AddL(TPtrC(KColumn2));
sl@0
   322
	key->MakeUnique();
sl@0
   323
	test(TheDatabase.CreateIndex(KIndexName,KTableName,*key)==KErrNone);
sl@0
   324
	CleanupStack::PopAndDestroy();
sl@0
   325
	set=TableDefinitionL(KTableName);
sl@0
   326
	set->Remove(TPtrC(KColumn2));
sl@0
   327
	test(TheDatabase.AlterTable(KTableName,*set)!=KErrNone);
sl@0
   328
	test(TheDatabase.DropIndex(KIndexName,KTableName)==KErrNone);
sl@0
   329
	delete set;
sl@0
   330
//
sl@0
   331
	test.Next(_L("Extend an indexed text column"));
sl@0
   332
	set=Set::CreateL(Set::Extended);
sl@0
   333
	test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   334
	delete set;
sl@0
   335
	key=CDbKey::NewLC();
sl@0
   336
	key->AddL(TPtrC(KColumn8));
sl@0
   337
	key->MakeUnique();
sl@0
   338
	test(TheDatabase.CreateIndex(KIndexName,KTableName,*key)==KErrNone);
sl@0
   339
	CleanupStack::PopAndDestroy();
sl@0
   340
	set=Set::CreateL(Set::LongerText);
sl@0
   341
	test(TheDatabase.AlterTable(KTableName,*set)!=KErrNone);
sl@0
   342
	test(TheDatabase.DropIndex(KIndexName,KTableName)==KErrNone);
sl@0
   343
//
sl@0
   344
	test.Next(_L("Extend a text column"));
sl@0
   345
	test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   346
	delete set;
sl@0
   347
//
sl@0
   348
	test.Next(_L("Extend a text column to a LongText column"));
sl@0
   349
	set=Set::CreateL(Set::TextToLongText);
sl@0
   350
	test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   351
	delete set;
sl@0
   352
//
sl@0
   353
	test.Next(_L("Alter to a very different set"));
sl@0
   354
	set=Set::CreateL(Set::Different);
sl@0
   355
	test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   356
	CloseDatabaseL();
sl@0
   357
	OpenDatabaseL();
sl@0
   358
	CDbColSet* def=TableDefinitionL(KTableName);
sl@0
   359
	Compare(*set,*def);
sl@0
   360
	delete def;
sl@0
   361
	delete set;
sl@0
   362
	test.End();
sl@0
   363
	test(TheDatabase.DropTable(KTableName)==KErrNone);
sl@0
   364
	DestroyDatabaseL();
sl@0
   365
	}
sl@0
   366
sl@0
   367
class Map
sl@0
   368
	{
sl@0
   369
public:
sl@0
   370
	Map();
sl@0
   371
	void Init(RDbRowSet& aSet);
sl@0
   372
	inline TDbColNo operator[](TInt aCol) const
sl@0
   373
		{return iMap[aCol];}
sl@0
   374
private:
sl@0
   375
	TDbColNo iMap[EExtra+1];
sl@0
   376
	};
sl@0
   377
sl@0
   378
Map::Map()
sl@0
   379
	{
sl@0
   380
	}
sl@0
   381
sl@0
   382
void Map::Init(RDbRowSet& aSet)
sl@0
   383
	{
sl@0
   384
	CDbColSet* set=NULL;
sl@0
   385
	TRAPD(errCode, set=aSet.ColSetL());
sl@0
   386
	if(errCode != KErrNone)
sl@0
   387
		{
sl@0
   388
		return;
sl@0
   389
		}
sl@0
   390
	for (TInt ii=EBit;ii<=EExtra;++ii)
sl@0
   391
		iMap[ii]=set->ColNo(KColumns[ii]);
sl@0
   392
	if(set)
sl@0
   393
		delete set;
sl@0
   394
	}
sl@0
   395
sl@0
   396
//
sl@0
   397
// Build the table for Altering
sl@0
   398
//
sl@0
   399
LOCAL_C void BuildTableL(const Set::SColDef* aDef=Set::Basic)
sl@0
   400
	{
sl@0
   401
	CDbColSet* set=Set::CreateL(aDef);
sl@0
   402
	test(TheDatabase.CreateTable(KTableName,*set)==KErrNone);
sl@0
   403
	delete set;
sl@0
   404
	TheDatabase.Begin();
sl@0
   405
	test(TheTable.Open(TheDatabase,KTableName,TheTable.EInsertOnly)==KErrNone);
sl@0
   406
	Map map;
sl@0
   407
	map.Init(TheTable);
sl@0
   408
	for (TInt ii=0;ii<KRecords;++ii)
sl@0
   409
		{
sl@0
   410
		TheTable.InsertL();
sl@0
   411
		TheTable.SetColL(map[EBit],KCol1Data);
sl@0
   412
		TheTable.SetColL(map[EInt],KCol2Data);
sl@0
   413
		TheTable.SetColL(map[EText],KCol3Data);
sl@0
   414
		TheTable.SetColL(map[ELong],KCol4Data);
sl@0
   415
		if ((ii%EBitNull)==0)
sl@0
   416
			TheTable.SetColL(map[EBitNull],KCol5Data);
sl@0
   417
		if ((ii%EIntNull)==0)
sl@0
   418
			TheTable.SetColL(map[EIntNull],KCol6Data);
sl@0
   419
		if ((ii%ETextNull)==0)
sl@0
   420
			TheTable.SetColL(map[ETextNull],KCol7Data);
sl@0
   421
		if ((ii%ELongNull)==0)
sl@0
   422
			TheTable.SetColL(map[ELongNull],KCol8Data);
sl@0
   423
		if (map[EExtra] && (ii%EExtra)==0)
sl@0
   424
			TheTable.SetColL(map[EExtra],KCol9Data);
sl@0
   425
		TheTable.PutL();
sl@0
   426
		}
sl@0
   427
	TheTable.Close();
sl@0
   428
	test(TheDatabase.Commit()==KErrNone);
sl@0
   429
	}
sl@0
   430
sl@0
   431
LOCAL_C void CheckBlobL(TDbColNo aCol,const TDesC8& aData)
sl@0
   432
	{
sl@0
   433
	test(TheTable.ColSize(aCol)==aData.Size());
sl@0
   434
	TBuf8<500> buf;
sl@0
   435
	__ASSERT_DEBUG(buf.MaxLength()>=aData.Length(),User::Invariant());
sl@0
   436
	RDbColReadStream str;
sl@0
   437
	str.OpenLC(TheTable,aCol);
sl@0
   438
	str.ReadL(buf,aData.Length());
sl@0
   439
	CleanupStack::PopAndDestroy();
sl@0
   440
	test(buf==aData);
sl@0
   441
	}
sl@0
   442
sl@0
   443
#if defined(UNICODE)
sl@0
   444
LOCAL_C void CheckBlobL(TDbColNo aCol,const TDesC16& aData)
sl@0
   445
	{
sl@0
   446
	test(TheTable.ColSize(aCol)==aData.Size());
sl@0
   447
	TBuf16<500> buf;
sl@0
   448
	__ASSERT_DEBUG(buf.MaxLength()>=aData.Length(),User::Invariant());
sl@0
   449
	RDbColReadStream str;
sl@0
   450
	str.OpenLC(TheTable,aCol);
sl@0
   451
	str.ReadL(buf,aData.Length());
sl@0
   452
	CleanupStack::PopAndDestroy();
sl@0
   453
	test(buf==aData);
sl@0
   454
	}
sl@0
   455
#endif
sl@0
   456
sl@0
   457
//
sl@0
   458
// Check that the columns which still exist, still contain the same stuff
sl@0
   459
// New columns should be Null
sl@0
   460
//
sl@0
   461
LOCAL_C void CheckTableL()
sl@0
   462
	{
sl@0
   463
	test(TheTable.Open(TheDatabase,KTableName,TheTable.EReadOnly)==KErrNone);
sl@0
   464
	Map map;
sl@0
   465
	map.Init(TheTable);
sl@0
   466
sl@0
   467
	for (TInt ii=0;ii<KRecords;++ii)
sl@0
   468
		{
sl@0
   469
		test(TheTable.NextL());
sl@0
   470
		TheTable.GetL();
sl@0
   471
		if (map[EBit])
sl@0
   472
			test(TheTable.ColUint(map[EBit])==KCol1Data);
sl@0
   473
		if (map[EInt])
sl@0
   474
			test(TheTable.ColInt(map[EInt])==KCol2Data);
sl@0
   475
		if (map[EText])
sl@0
   476
			test(TheTable.ColDes(map[EText])==KCol3Data);
sl@0
   477
		if (map[ELong])
sl@0
   478
			CheckBlobL(map[ELong],KCol4Data);
sl@0
   479
		for (TInt jj=EBitNull;jj<=EExtra;++jj)
sl@0
   480
			{
sl@0
   481
			if (!map[jj])
sl@0
   482
				continue;
sl@0
   483
			if (ii%jj)
sl@0
   484
				test(TheTable.IsColNull(map[jj]));
sl@0
   485
			else
sl@0
   486
				{
sl@0
   487
				switch (jj)
sl@0
   488
					{
sl@0
   489
				case EBitNull:
sl@0
   490
					test(TheTable.ColUint(map[EBitNull])==KCol5Data);
sl@0
   491
					break;
sl@0
   492
				case EIntNull:
sl@0
   493
					test(TheTable.ColInt(map[EIntNull])==KCol6Data);
sl@0
   494
					break;
sl@0
   495
				case ETextNull:
sl@0
   496
					test(TheTable.ColDes(map[ETextNull])==KCol7Data);
sl@0
   497
					break;
sl@0
   498
				case ELongNull:
sl@0
   499
					CheckBlobL(map[ELongNull],KCol8Data);
sl@0
   500
					break;
sl@0
   501
				case EExtra:
sl@0
   502
					CheckBlobL(map[EExtra],KCol9Data);
sl@0
   503
					break;
sl@0
   504
					}
sl@0
   505
				}
sl@0
   506
			}
sl@0
   507
		}
sl@0
   508
	TheTable.Close();
sl@0
   509
	}
sl@0
   510
sl@0
   511
/**
sl@0
   512
@SYMTestCaseID          SYSLIB-DBMS-CT-0576
sl@0
   513
@SYMTestCaseDesc        Test a full table
sl@0
   514
@SYMTestPriority        Medium
sl@0
   515
@SYMTestActions        	Tests for altering the table
sl@0
   516
@SYMTestExpectedResults Test must not fail
sl@0
   517
@SYMREQ                 REQ0000
sl@0
   518
*/
sl@0
   519
LOCAL_C void TestFullTableL()
sl@0
   520
	{
sl@0
   521
	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0576 Create database "));
sl@0
   522
	CreateDatabaseL();
sl@0
   523
//
sl@0
   524
	test.Next(_L("Add non-null column"));
sl@0
   525
	BuildTableL();
sl@0
   526
	CDbColSet* set=TableDefinitionL(KTableName);
sl@0
   527
	TDbCol col10=TDbCol(TPtrC(KColumn10),EDbColInt32);
sl@0
   528
	col10.iAttributes=TDbCol::ENotNull;
sl@0
   529
	set->AddL(col10);
sl@0
   530
	test(TheDatabase.AlterTable(KTableName,*set)!=KErrNone);
sl@0
   531
//
sl@0
   532
	test.Next(_L("Add nullable column"));
sl@0
   533
	set->Remove(col10.iName);
sl@0
   534
	col10.iAttributes=0;
sl@0
   535
	set->AddL(col10);
sl@0
   536
	test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   537
	CheckTableL();
sl@0
   538
//
sl@0
   539
	test.Next(_L("Drop columns one by one"));
sl@0
   540
	while (set->Count()>1)
sl@0
   541
		{
sl@0
   542
		set->Remove((*set)[1].iName);
sl@0
   543
		test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   544
		CheckTableL();
sl@0
   545
		}
sl@0
   546
	delete set;
sl@0
   547
	test(TheDatabase.DropTable(KTableName)==KErrNone);
sl@0
   548
//
sl@0
   549
	test.Next(_L("Extend a text column"));
sl@0
   550
	BuildTableL(Set::Extended);
sl@0
   551
	set=Set::CreateL(Set::LongerText);
sl@0
   552
	test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   553
	delete set;
sl@0
   554
	CheckTableL();
sl@0
   555
//
sl@0
   556
	test.Next(_L("Extend it to a LongText column"));
sl@0
   557
	set=Set::CreateL(Set::TextToLongText);
sl@0
   558
	test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   559
	delete set;
sl@0
   560
	CheckTableL();
sl@0
   561
//
sl@0
   562
	test.Next(_L("Drop all except one"));
sl@0
   563
	set=Set::CreateL(Set::Column3);
sl@0
   564
	test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   565
	delete set;
sl@0
   566
	CheckTableL();
sl@0
   567
	test(TheDatabase.DropTable(KTableName)==KErrNone);
sl@0
   568
	test.Next(_L("Drop single column"));
sl@0
   569
	for (TInt ii=EBit;ii<=EExtra;++ii)
sl@0
   570
		{
sl@0
   571
		BuildTableL(Set::Extended);
sl@0
   572
		CDbColSet* set=TableDefinitionL(KTableName);
sl@0
   573
		set->Remove(KColumns[ii]);
sl@0
   574
		test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   575
		delete set;
sl@0
   576
		CheckTableL();
sl@0
   577
		test(TheDatabase.DropTable(KTableName)==KErrNone);
sl@0
   578
		}
sl@0
   579
	test.Next(_L("Drop multiple columns"));
sl@0
   580
	BuildTableL();
sl@0
   581
	set=Set::CreateL(Set::DropSome);
sl@0
   582
	test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   583
	delete set;
sl@0
   584
	CheckTableL();
sl@0
   585
	test.Next(_L("Drop and add together"));
sl@0
   586
	set=Set::CreateL(Set::DropAndAdd);
sl@0
   587
	test(TheDatabase.AlterTable(KTableName,*set)==KErrNone);
sl@0
   588
	delete set;
sl@0
   589
	CheckTableL();
sl@0
   590
	test(TheDatabase.DropTable(KTableName)==KErrNone);
sl@0
   591
	test.End();
sl@0
   592
	DestroyDatabaseL();
sl@0
   593
	}
sl@0
   594
sl@0
   595
LOCAL_C void Test()
sl@0
   596
	{
sl@0
   597
	__UHEAP_MARK;
sl@0
   598
//
sl@0
   599
	test.Start(_L("Alter empty table"));
sl@0
   600
	TRAPD(r,TestEmptyTableL();)
sl@0
   601
	test(r==KErrNone);
sl@0
   602
	__UHEAP_CHECK(0);
sl@0
   603
	test.Next(_L("Alter full table"));
sl@0
   604
	TRAP(r,TestFullTableL();)
sl@0
   605
	test(r==KErrNone);
sl@0
   606
	test.End();
sl@0
   607
//
sl@0
   608
	__UHEAP_MARKEND;
sl@0
   609
	}
sl@0
   610
sl@0
   611
//
sl@0
   612
// Prepare the test directory.
sl@0
   613
//
sl@0
   614
LOCAL_C void setupTestDirectory()
sl@0
   615
    {
sl@0
   616
	TInt r=TheFs.Connect();
sl@0
   617
	test(r==KErrNone);
sl@0
   618
//
sl@0
   619
	r=TheFs.MkDir(KTestDir);
sl@0
   620
	test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   621
	r=TheFs.SetSessionPath(KTestDir);
sl@0
   622
	test(r==KErrNone);
sl@0
   623
	}
sl@0
   624
sl@0
   625
//
sl@0
   626
// Initialise the cleanup stack.
sl@0
   627
//
sl@0
   628
LOCAL_C void setupCleanup()
sl@0
   629
    {
sl@0
   630
	TheTrapCleanup=CTrapCleanup::New();
sl@0
   631
	test(TheTrapCleanup!=NULL);
sl@0
   632
	TRAPD(r,\
sl@0
   633
		{\
sl@0
   634
		for (TInt i=KTestCleanupStack;i>0;i--)\
sl@0
   635
			CleanupStack::PushL((TAny*)0);\
sl@0
   636
		CleanupStack::Pop(KTestCleanupStack);\
sl@0
   637
		});
sl@0
   638
	test(r==KErrNone);
sl@0
   639
	}
sl@0
   640
sl@0
   641
LOCAL_C void DeleteDataFile(const TDesC& aFullName)
sl@0
   642
	{
sl@0
   643
	RFs fsSession;
sl@0
   644
	TInt err = fsSession.Connect();
sl@0
   645
	if(err == KErrNone)
sl@0
   646
		{
sl@0
   647
		TEntry entry;
sl@0
   648
		if(fsSession.Entry(aFullName, entry) == KErrNone)
sl@0
   649
			{
sl@0
   650
			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
sl@0
   651
			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
sl@0
   652
			if(err != KErrNone)
sl@0
   653
				{
sl@0
   654
				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
sl@0
   655
				}
sl@0
   656
			err = fsSession.Delete(aFullName);
sl@0
   657
			if(err != KErrNone)
sl@0
   658
				{
sl@0
   659
				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
sl@0
   660
				}
sl@0
   661
			}
sl@0
   662
		fsSession.Close();
sl@0
   663
		}
sl@0
   664
	else
sl@0
   665
		{
sl@0
   666
		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
sl@0
   667
		}
sl@0
   668
	}
sl@0
   669
sl@0
   670
//
sl@0
   671
// Test streaming conversions.
sl@0
   672
//
sl@0
   673
GLDEF_C TInt E32Main()
sl@0
   674
    {
sl@0
   675
	test.Title();
sl@0
   676
	setupTestDirectory();
sl@0
   677
	setupCleanup();
sl@0
   678
	__UHEAP_MARK;
sl@0
   679
//
sl@0
   680
	test.Start(_L("Standard database"));
sl@0
   681
	Test();
sl@0
   682
	test.Next(_L("Secure database"));
sl@0
   683
	Test();
sl@0
   684
sl@0
   685
	// clean up data files used by this test - must be done before call to End() - DEF047652
sl@0
   686
	_LIT(KTestDbName, "C:\\DBMS-TST\\T_ALTER.DB");
sl@0
   687
	::DeleteDataFile(KTestDbName);
sl@0
   688
sl@0
   689
	test.End();
sl@0
   690
//
sl@0
   691
	__UHEAP_MARKEND;
sl@0
   692
	delete TheTrapCleanup;
sl@0
   693
sl@0
   694
	TheFs.Close();
sl@0
   695
	test.Close();
sl@0
   696
	return 0;
sl@0
   697
    }