os/persistentdata/persistentstorage/dbms/tdbms/t_dbsql.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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
// MSVC++ up to 5.0 has problems with expanding inline functions
sl@0
    17
// This disables the mad warnings for the whole project
sl@0
    18
#if defined(NDEBUG) && defined(__VC32__) && _MSC_VER<=1100
sl@0
    19
#pragma warning(disable : 4710)			// function not expanded. MSVC 5.0 is stupid
sl@0
    20
#endif
sl@0
    21
sl@0
    22
#include <d32dbms.h>
sl@0
    23
#include <f32file.h>
sl@0
    24
#include <e32test.h>
sl@0
    25
#include <e32math.h>
sl@0
    26
sl@0
    27
// constructing literal TInt64 from compiler 64 bit integer rep
sl@0
    28
sl@0
    29
#ifndef I64LIT
sl@0
    30
#if defined __GCC32__ || defined __EABI__
sl@0
    31
#define _LINT64(val) MAKE_TINT64(TUint(val##LL>>32),TUint(val##LL&0xffffffffu))
sl@0
    32
#else
sl@0
    33
#define _LINT64(val) MAKE_TINT64(TUint(__int64(val)>>32),TUint(__int64(val)&0xffffffffu))
sl@0
    34
#endif
sl@0
    35
#else // I64LIT
sl@0
    36
#define _LINT64 I64LIT
sl@0
    37
#endif // I64LIT
sl@0
    38
sl@0
    39
LOCAL_D RTest TheTest(_L("t_dbsql: DBMS SQL parsing and execution tests"));
sl@0
    40
sl@0
    41
LOCAL_D RFs TheFs;
sl@0
    42
LOCAL_D CTrapCleanup* TheTrapCleanup;
sl@0
    43
LOCAL_D RDbs TheDbs;
sl@0
    44
LOCAL_D RDbNamedDatabase TheDatabase;
sl@0
    45
LOCAL_D RDbTable TheTable;
sl@0
    46
LOCAL_D RDbView TheView;
sl@0
    47
LOCAL_D TBuf<1024> TheSql;
sl@0
    48
sl@0
    49
const TInt KTestCleanupStack=0x20;
sl@0
    50
const TPtrC KTestDatabase=_L("c:\\dbms-tst\\t_sql.db");
sl@0
    51
sl@0
    52
#define elementsof(array) (sizeof(array)/sizeof(array[0]))
sl@0
    53
sl@0
    54
LOCAL_C void TestCleanup()
sl@0
    55
	{
sl@0
    56
	TheTable.Close();
sl@0
    57
	TheView.Close();
sl@0
    58
	TheDatabase.Close();
sl@0
    59
	TheDbs.Close();
sl@0
    60
	TheFs.Close();
sl@0
    61
	/////////////
sl@0
    62
	RFs fsSession;
sl@0
    63
	TInt err = fsSession.Connect();
sl@0
    64
	if(err == KErrNone)
sl@0
    65
		{
sl@0
    66
		TEntry entry;
sl@0
    67
		if(fsSession.Entry(KTestDatabase, entry) == KErrNone)
sl@0
    68
			{
sl@0
    69
			RDebug::Print(_L("Deleting \"%S\" file.\n"), &KTestDatabase);
sl@0
    70
			err = fsSession.SetAtt(KTestDatabase, 0, KEntryAttReadOnly);
sl@0
    71
			if(err != KErrNone)
sl@0
    72
				{
sl@0
    73
				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &KTestDatabase);
sl@0
    74
				}
sl@0
    75
			err = fsSession.Delete(KTestDatabase);
sl@0
    76
			if(err != KErrNone)
sl@0
    77
				{
sl@0
    78
				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &KTestDatabase);
sl@0
    79
				}
sl@0
    80
			}
sl@0
    81
		fsSession.Close();
sl@0
    82
		}
sl@0
    83
	else
sl@0
    84
		{
sl@0
    85
		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &KTestDatabase);
sl@0
    86
		}
sl@0
    87
	}
sl@0
    88
sl@0
    89
LOCAL_C void CloseDatabase()
sl@0
    90
	{
sl@0
    91
	TheDatabase.Close();
sl@0
    92
	}
sl@0
    93
sl@0
    94
LOCAL_C void Disconnect()
sl@0
    95
	{
sl@0
    96
	TheDbs.ResourceCheck();
sl@0
    97
	TheDbs.Close();
sl@0
    98
	}
sl@0
    99
sl@0
   100
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   101
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   102
//Tests macros and functions.
sl@0
   103
//If (!aValue) then the test will be panicked, the test data files will be deleted.
sl@0
   104
static void Check(TInt aValue, TInt aLine)
sl@0
   105
	{
sl@0
   106
	if(!aValue)
sl@0
   107
		{
sl@0
   108
        RDebug::Print(_L("*** Expression evaluated to false\r\n"));
sl@0
   109
		::TestCleanup();
sl@0
   110
		TheTest(EFalse, aLine);
sl@0
   111
		}
sl@0
   112
	}
sl@0
   113
//If (aValue != aExpected) then the test will be panicked, the test data files will be deleted.
sl@0
   114
static void Check(TInt aValue, TInt aExpected, TInt aLine)
sl@0
   115
	{
sl@0
   116
	if(aValue != aExpected)
sl@0
   117
		{
sl@0
   118
		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
sl@0
   119
		::TestCleanup();
sl@0
   120
		TheTest(EFalse, aLine);
sl@0
   121
		}
sl@0
   122
	}
sl@0
   123
//Use these to test conditions.
sl@0
   124
#define TEST(arg) ::Check((arg), __LINE__)
sl@0
   125
#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
sl@0
   126
sl@0
   127
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   128
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   129
sl@0
   130
// Create the database
sl@0
   131
LOCAL_C void CreateDatabase()
sl@0
   132
	{
sl@0
   133
	TInt r=TheDatabase.Replace(TheFs,KTestDatabase);
sl@0
   134
	TEST2(r, KErrNone);
sl@0
   135
	}
sl@0
   136
sl@0
   137
LOCAL_C void Connect()
sl@0
   138
	{
sl@0
   139
	TInt r=TheDbs.Connect();
sl@0
   140
	TEST2(r, KErrNone);
sl@0
   141
	TheDbs.ResourceMark();
sl@0
   142
	}
sl@0
   143
sl@0
   144
// Open the database through the server
sl@0
   145
LOCAL_C void ShareDatabase()
sl@0
   146
	{
sl@0
   147
	TInt r=TheDatabase.Open(TheDbs,KTestDatabase);
sl@0
   148
	TEST2(r, KErrNone);
sl@0
   149
	}
sl@0
   150
sl@0
   151
struct TSelectTest
sl@0
   152
	{
sl@0
   153
	const TText* iSearchCondition;
sl@0
   154
	TUint iResultSet;
sl@0
   155
	};
sl@0
   156
#define ROWS(n,m) (((~0u)<<(n))&((2u<<(m))-1))
sl@0
   157
#define ROW(n) ROWS(n,n)
sl@0
   158
#define NO_ROWS 0
sl@0
   159
#define ALL_ROWS (~1u)		// all rows which are not NULL
sl@0
   160
sl@0
   161
class TestPredicateBase
sl@0
   162
	{
sl@0
   163
protected:
sl@0
   164
	static void Create(const TText* aType);
sl@0
   165
	static void Test(const TSelectTest* aTest,TInt aCount,TInt aRows, TBool aLog);
sl@0
   166
	static void TestViewL(const TSelectTest* aTest,TInt aCount,TInt aRows, TBool aLog=EFalse);
sl@0
   167
	};
sl@0
   168
sl@0
   169
// Create the table for the predicate tests
sl@0
   170
void TestPredicateBase::Create(const TText* aType)
sl@0
   171
	{
sl@0
   172
	TheTest.Next(TPtrC(aType));
sl@0
   173
	TheDatabase.Begin();
sl@0
   174
	TheSql.Format(_L("CREATE TABLE Compare (Id COUNTER,Test %s)"),aType);
sl@0
   175
	TInt r=TheDatabase.Execute(TheSql);
sl@0
   176
	TEST2(r, KErrNone);
sl@0
   177
	r=TheTable.Open(TheDatabase,_L("Compare"),TheTable.EInsertOnly);
sl@0
   178
	TEST2(r, KErrNone);
sl@0
   179
	}
sl@0
   180
sl@0
   181
// Test the predicate on the table, then on the indexed table
sl@0
   182
void TestPredicateBase::Test(const TSelectTest* aTest,TInt aCount,TInt aRows, TBool aLog)
sl@0
   183
	{
sl@0
   184
    if(aLog)
sl@0
   185
        {
sl@0
   186
        TheTest.Printf(_L("TestPredicateBase::Test\r\n"));
sl@0
   187
        }
sl@0
   188
	TheTable.Close();
sl@0
   189
	TInt r=TheDatabase.Commit();
sl@0
   190
	if(aLog)
sl@0
   191
        {
sl@0
   192
        TheTest.Printf(_L("Commit %d\r\n"), r);
sl@0
   193
        }
sl@0
   194
	TEST2(r, KErrNone);
sl@0
   195
	TRAPD(errCode, TestViewL(aTest,aCount,aRows, aLog));
sl@0
   196
	if(aLog)
sl@0
   197
        {
sl@0
   198
        TheTest.Printf(_L("TestViewL %d"), errCode);
sl@0
   199
        }
sl@0
   200
sl@0
   201
	TEST2(errCode, KErrNone);
sl@0
   202
	r=TheDatabase.Execute(_L("CREATE INDEX Key ON Compare (Test)"));
sl@0
   203
   if(aLog)
sl@0
   204
        {
sl@0
   205
        TheTest.Printf(_L("Execute %d"), r);
sl@0
   206
        }
sl@0
   207
sl@0
   208
	TEST2(r, KErrNone);
sl@0
   209
	TRAP(errCode,TestViewL(aTest,aCount,aRows, aLog));
sl@0
   210
	if(aLog)
sl@0
   211
        {
sl@0
   212
        TheTest.Printf(_L("TestViewL %d"), errCode);
sl@0
   213
	    }
sl@0
   214
sl@0
   215
	TEST2(errCode, KErrNone);
sl@0
   216
	r=TheDatabase.Execute(_L("DROP TABLE Compare"));
sl@0
   217
    if(aLog)
sl@0
   218
        {
sl@0
   219
        TheTest.Printf(_L("Execute %d"), r);
sl@0
   220
        }
sl@0
   221
sl@0
   222
	TEST2(r, KErrNone);
sl@0
   223
	}
sl@0
   224
sl@0
   225
// Test the predicate on the table
sl@0
   226
void TestPredicateBase::TestViewL(const TSelectTest* aTest,TInt aCount,TInt aRows, TBool /*aLog*/)
sl@0
   227
	{
sl@0
   228
	TUint rowMask=(2u<<aRows)-1;
sl@0
   229
	for (;--aCount>=0;++aTest)
sl@0
   230
		{
sl@0
   231
		TheSql.Format(_L("SELECT Id FROM Compare WHERE Test %s"),aTest->iSearchCondition);
sl@0
   232
		TInt r=TheView.Prepare(TheDatabase,TheSql,TheView.EReadOnly);
sl@0
   233
		if(r!=KErrNone)
sl@0
   234
		    {
sl@0
   235
            TheTest.Printf(_L("Prepare r= %d aCount= %d  statement %S\r\n"), r, aCount, &TheSql);
sl@0
   236
		    }
sl@0
   237
		TEST2(r, KErrNone);
sl@0
   238
		TBool ignoreRow0=TheView.Unevaluated();
sl@0
   239
		r=TheView.EvaluateAll();
sl@0
   240
 		TEST2(r, KErrNone);
sl@0
   241
		TUint rows=0;
sl@0
   242
		while (TheView.NextL())
sl@0
   243
			{
sl@0
   244
 			TheView.GetL();
sl@0
   245
			rows|=1u<<TheView.ColUint(1);
sl@0
   246
			}
sl@0
   247
		if (ignoreRow0)
sl@0
   248
		    {
sl@0
   249
  			TEST((rows&~ROW(0))==(aTest->iResultSet&rowMask&~ROW(0)));
sl@0
   250
		    }
sl@0
   251
		else
sl@0
   252
		    {
sl@0
   253
 			TEST(rows==(aTest->iResultSet&rowMask));
sl@0
   254
		    }
sl@0
   255
		TheView.Close();
sl@0
   256
		}
sl@0
   257
	}
sl@0
   258
sl@0
   259
typedef void (*FSetColL)(TDbColNo aCol,const TAny* aVal);
sl@0
   260
sl@0
   261
void WriteRowsL(const TAny* aValues,TInt aRows,TInt aSize,FSetColL aSetColL, TBool aLog)
sl@0
   262
	{
sl@0
   263
	for (TInt row=0;row<=aRows;++row)
sl@0
   264
		{
sl@0
   265
        if(aLog)
sl@0
   266
            {
sl@0
   267
            TheTest.Printf(_L("row = %d"), row);
sl@0
   268
            }
sl@0
   269
		TheTable.InsertL();
sl@0
   270
		TEST(TheTable.ColUint(1)==TUint(row));
sl@0
   271
		if (row>0)
sl@0
   272
			{	// first row is always null
sl@0
   273
			aSetColL(2,aValues);
sl@0
   274
			aValues=PtrAdd(aValues,aSize);
sl@0
   275
			}
sl@0
   276
		TheTable.PutL();
sl@0
   277
		}
sl@0
   278
	}
sl@0
   279
sl@0
   280
template <class T>
sl@0
   281
struct SetCol
sl@0
   282
	{
sl@0
   283
	static void SetColL(TDbColNo aCol,const TAny* aVal)
sl@0
   284
		{
sl@0
   285
		TheTable.SetColL(aCol,*(const T*)aVal);
sl@0
   286
		}
sl@0
   287
	};
sl@0
   288
sl@0
   289
template <class T>
sl@0
   290
inline void WriteRowsL(const T* aValues,TUint aRows, TBool aLog)
sl@0
   291
	{
sl@0
   292
	WriteRowsL(aValues,aRows,sizeof(T),&SetCol<T>::SetColL, aLog);
sl@0
   293
	}
sl@0
   294
sl@0
   295
template <class T>
sl@0
   296
class TestPredicate : public TestPredicateBase
sl@0
   297
	{
sl@0
   298
public:
sl@0
   299
	static void RunL();
sl@0
   300
	};
sl@0
   301
sl@0
   302
// Test the Predicate operators for all types
sl@0
   303
template <class T>
sl@0
   304
void TestPredicate<T>::RunL()
sl@0
   305
	{
sl@0
   306
	Create(T::KType);
sl@0
   307
	TBool log = EFalse;
sl@0
   308
	if((TPtrC(T::KType)).CompareF(_L("TIME"))==0)
sl@0
   309
	    {
sl@0
   310
        log = ETrue;
sl@0
   311
	    }
sl@0
   312
	WriteRowsL(T::KValues,elementsof(T::KValues), log);
sl@0
   313
	Test(T::KTests,elementsof(T::KTests),elementsof(T::KValues),log);
sl@0
   314
	}
sl@0
   315
sl@0
   316
struct TypeBit
sl@0
   317
	{
sl@0
   318
public:
sl@0
   319
	static const TText* const KType;
sl@0
   320
	static const TUint KValues[];
sl@0
   321
	static const TSelectTest KTests[];
sl@0
   322
	};
sl@0
   323
const TText* const TypeBit::KType=_S("BIT");
sl@0
   324
const TUint TypeBit::KValues[]={0,1};
sl@0
   325
const TSelectTest TypeBit::KTests[]=
sl@0
   326
	{
sl@0
   327
	{_S("IS NULL"),ROW(0)},
sl@0
   328
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   329
	{_S("=0"),ROW(1)},
sl@0
   330
	{_S("<>0"),ROW(2)},
sl@0
   331
	{_S(">0"),ROW(2)},
sl@0
   332
	{_S("<0"),NO_ROWS},
sl@0
   333
	{_S("<=0"),ROW(1)},
sl@0
   334
	{_S(">=0"),ALL_ROWS},
sl@0
   335
	{_S("=1"),ROW(2)},
sl@0
   336
	{_S("<>1"),ROW(1)},
sl@0
   337
	{_S(">1"),NO_ROWS},
sl@0
   338
	{_S("<1"),ROW(1)},
sl@0
   339
	{_S("<=1"),ALL_ROWS},
sl@0
   340
	{_S(">=1"),ROW(2)}
sl@0
   341
	};
sl@0
   342
sl@0
   343
struct TypeUint8
sl@0
   344
	{
sl@0
   345
public:
sl@0
   346
	static const TText* const KType;
sl@0
   347
	static const TUint KValues[];
sl@0
   348
	static const TSelectTest KTests[];
sl@0
   349
	};
sl@0
   350
const TText* const TypeUint8::KType=_S("UNSIGNED TINYINT");
sl@0
   351
const TUint TypeUint8::KValues[]={0,1,127,128,254,255};
sl@0
   352
const TSelectTest TypeUint8::KTests[]=
sl@0
   353
	{
sl@0
   354
	{_S("IS NULL"),ROW(0)},
sl@0
   355
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   356
	{_S("=0"),ROW(1)},
sl@0
   357
	{_S("=3"),NO_ROWS},
sl@0
   358
	{_S("<2"),ROWS(1,2)},
sl@0
   359
	{_S(">=0"),ALL_ROWS},
sl@0
   360
	{_S("<128"),ROWS(1,3)},
sl@0
   361
	{_S("<>1"),ALL_ROWS-ROW(2)},
sl@0
   362
	{_S(">255"),NO_ROWS}
sl@0
   363
	};
sl@0
   364
sl@0
   365
struct TypeUint16
sl@0
   366
	{
sl@0
   367
public:
sl@0
   368
	static const TText* const KType;
sl@0
   369
	static const TUint KValues[];
sl@0
   370
	static const TSelectTest KTests[];
sl@0
   371
	};
sl@0
   372
const TText* const TypeUint16::KType=_S("UNSIGNED SMALLINT");
sl@0
   373
const TUint TypeUint16::KValues[]={0,1,5000,32767,32768,65534,65535};
sl@0
   374
const TSelectTest TypeUint16::KTests[]=
sl@0
   375
	{
sl@0
   376
	{_S("IS NULL"),ROW(0)},
sl@0
   377
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   378
	{_S("=0"),ROW(1)},
sl@0
   379
	{_S("=3"),NO_ROWS},
sl@0
   380
	{_S("<2"),ROWS(1,2)},
sl@0
   381
	{_S(">=32768"),ROWS(5,7)},
sl@0
   382
	{_S("<32767"),ROWS(1,3)},
sl@0
   383
	{_S("<>1"),ALL_ROWS-ROW(2)},
sl@0
   384
	{_S(">65535"),NO_ROWS}
sl@0
   385
	};
sl@0
   386
sl@0
   387
struct TypeUint32
sl@0
   388
	{
sl@0
   389
public:
sl@0
   390
	static const TText* const KType;
sl@0
   391
	static const TUint KValues[];
sl@0
   392
	static const TSelectTest KTests[];
sl@0
   393
	};
sl@0
   394
const TText* const TypeUint32::KType=_S("UNSIGNED INTEGER");
sl@0
   395
const TUint TypeUint32::KValues[]={0,1,2147483647u,2147483648u,3000000000u,4294967294u,4294967295u};
sl@0
   396
const TSelectTest TypeUint32::KTests[]=
sl@0
   397
	{
sl@0
   398
	{_S("IS NULL"),ROW(0)},
sl@0
   399
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   400
	{_S("=0"),ROW(1)},
sl@0
   401
	{_S("=3"),NO_ROWS},
sl@0
   402
	{_S("<2"),ROWS(1,2)},
sl@0
   403
	{_S(">=2147483648"),ROWS(4,7)},
sl@0
   404
	{_S("<2147483647"),ROWS(1,2)},
sl@0
   405
	{_S("<>3000000000"),ALL_ROWS-ROW(5)},
sl@0
   406
	{_S(">4294967295"),NO_ROWS}
sl@0
   407
	};
sl@0
   408
sl@0
   409
struct TypeInt8
sl@0
   410
	{
sl@0
   411
public:
sl@0
   412
	static const TText* const KType;
sl@0
   413
	static const TInt KValues[];
sl@0
   414
	static const TSelectTest KTests[];
sl@0
   415
	};
sl@0
   416
const TText* const TypeInt8::KType=_S("TINYINT");
sl@0
   417
const TInt TypeInt8::KValues[]={-128,-1,0,1,127};
sl@0
   418
const TSelectTest TypeInt8::KTests[]=
sl@0
   419
	{
sl@0
   420
	{_S("IS NULL"),ROW(0)},
sl@0
   421
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   422
	{_S("=0"),ROW(3)},
sl@0
   423
	{_S("=-1"),ROW(2)},
sl@0
   424
	{_S("<2"),ROWS(1,4)},
sl@0
   425
	{_S(">=-128"),ALL_ROWS},
sl@0
   426
	{_S("<128"),ALL_ROWS},
sl@0
   427
	{_S("<>1"),ALL_ROWS-ROW(4)}
sl@0
   428
	};
sl@0
   429
sl@0
   430
struct TypeInt16
sl@0
   431
	{
sl@0
   432
public:
sl@0
   433
	static const TText* const KType;
sl@0
   434
	static const TInt KValues[];
sl@0
   435
	static const TSelectTest KTests[];
sl@0
   436
	};
sl@0
   437
const TText* const TypeInt16::KType=_S("SMALLINT");
sl@0
   438
const TInt TypeInt16::KValues[]={-32768,-32767,-1,0,1,32766,32767};
sl@0
   439
const TSelectTest TypeInt16::KTests[]=
sl@0
   440
	{
sl@0
   441
	{_S("IS NULL"),ROW(0)},
sl@0
   442
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   443
	{_S("=0"),ROW(4)},
sl@0
   444
	{_S("<>0"),ALL_ROWS-ROW(4)},
sl@0
   445
	{_S("=-1"),ROW(3)},
sl@0
   446
	{_S("<2"),ROWS(1,5)},
sl@0
   447
	{_S(">=-40000"),ALL_ROWS},
sl@0
   448
	{_S("<32766"),ROWS(1,5)},
sl@0
   449
	{_S("=40"),NO_ROWS}
sl@0
   450
	};
sl@0
   451
sl@0
   452
struct TypeInt32
sl@0
   453
	{
sl@0
   454
public:
sl@0
   455
	static const TText* const KType;
sl@0
   456
	static const TInt KValues[];
sl@0
   457
	static const TSelectTest KTests[];
sl@0
   458
	};
sl@0
   459
const TText* const TypeInt32::KType=_S("INTEGER");
sl@0
   460
const TInt TypeInt32::KValues[]={0x80000000/*-2147483648*/,-2147483647,-1,0,1,2147483646,2147483647};
sl@0
   461
const TSelectTest TypeInt32::KTests[]=
sl@0
   462
	{
sl@0
   463
	{_S("IS NULL"),ROW(0)},
sl@0
   464
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   465
	{_S("=0"),ROW(4)},
sl@0
   466
	{_S("<>0"),ALL_ROWS-ROW(4)},
sl@0
   467
	{_S("=-1"),ROW(3)},
sl@0
   468
	{_S("<2"),ROWS(1,5)},
sl@0
   469
	{_S(">=-2147483648"),ALL_ROWS},
sl@0
   470
	{_S("<2147483646"),ROWS(1,5)},
sl@0
   471
	{_S(">2147483647"),NO_ROWS},
sl@0
   472
	{_S("=40"),NO_ROWS}
sl@0
   473
	};
sl@0
   474
sl@0
   475
struct TypeInt64
sl@0
   476
	{
sl@0
   477
public:
sl@0
   478
	static const TText* const KType;
sl@0
   479
	static const TInt64 KValues[];
sl@0
   480
	static const TSelectTest KTests[];
sl@0
   481
	};
sl@0
   482
const TText* const TypeInt64::KType=_S("BIGINT");
sl@0
   483
const TInt64 TypeInt64::KValues[]=
sl@0
   484
	{
sl@0
   485
	MAKE_TINT64(0x80000000, 0x00000000), // min int64
sl@0
   486
	_LINT64(-4294967296),
sl@0
   487
	TInt(0x80000000),			// -2147483648!
sl@0
   488
	-1,
sl@0
   489
	0u,
sl@0
   490
	1u,
sl@0
   491
	2147483647u,
sl@0
   492
	_LINT64(2147483648),
sl@0
   493
	_LINT64(4294967295),
sl@0
   494
	_LINT64(4294967296),
sl@0
   495
	_LINT64(9223372036854775807)		// max int64
sl@0
   496
	};
sl@0
   497
const TSelectTest TypeInt64::KTests[]=
sl@0
   498
	{
sl@0
   499
	{_S("IS NULL"),ROW(0)},
sl@0
   500
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   501
	{_S("=0"),ROW(5)},
sl@0
   502
	{_S("<>0"),ALL_ROWS-ROW(5)},
sl@0
   503
	{_S("=-1"),ROW(4)},
sl@0
   504
	{_S("<2"),ROWS(1,6)},
sl@0
   505
	{_S(">=-9223372036854775808"),ALL_ROWS},
sl@0
   506
	{_S("<4294967296"),ROWS(1,9)},
sl@0
   507
	{_S(">9223372036854775806"),ROW(11)},
sl@0
   508
	{_S("=40"),NO_ROWS}
sl@0
   509
	};
sl@0
   510
sl@0
   511
struct TypeReal32
sl@0
   512
	{
sl@0
   513
public:
sl@0
   514
	static const TText* const KType;
sl@0
   515
	static const TReal32 KValues[];
sl@0
   516
	static const TSelectTest KTests[];
sl@0
   517
	};
sl@0
   518
const TText* const TypeReal32::KType=_S("REAL");
sl@0
   519
const TReal32 TypeReal32::KValues[]=
sl@0
   520
	{
sl@0
   521
	-KMaxTReal32,
sl@0
   522
	-1.0f,
sl@0
   523
	-KMinTReal32,
sl@0
   524
	0.0f,
sl@0
   525
	KMinTReal32,
sl@0
   526
	1.0f,
sl@0
   527
	KMaxTReal32
sl@0
   528
	};
sl@0
   529
const TSelectTest TypeReal32::KTests[]=
sl@0
   530
	{
sl@0
   531
	{_S("IS NULL"),ROW(0)},
sl@0
   532
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   533
	{_S("=0"),ROW(4)},
sl@0
   534
	{_S("<>0.0"),ALL_ROWS-ROW(4)},
sl@0
   535
	{_S("=-1"),ROW(2)},
sl@0
   536
	{_S("<2e1"),ROWS(1,6)},
sl@0
   537
	{_S(">=-100000000000000"),ROWS(2,7)},
sl@0
   538
	{_S("<1e-36"),ROWS(1,5)},
sl@0
   539
	{_S(">1e15"),ROW(7)},
sl@0
   540
	{_S("=.5"),NO_ROWS},
sl@0
   541
	{_S("<=-.0"),ROWS(1,4)},
sl@0
   542
	{_S("<1e40"),ALL_ROWS}
sl@0
   543
	};
sl@0
   544
sl@0
   545
struct TypeReal64
sl@0
   546
	{
sl@0
   547
public:
sl@0
   548
	static const TText* const KType;
sl@0
   549
	static const TReal64 KValues[];
sl@0
   550
	static const TSelectTest KTests[];
sl@0
   551
	};
sl@0
   552
const TText* const TypeReal64::KType=_S("DOUBLE");
sl@0
   553
const TReal64 TypeReal64::KValues[]=
sl@0
   554
	{
sl@0
   555
	-KMaxTReal64,
sl@0
   556
	-1.0f,
sl@0
   557
	-KMinTReal64,
sl@0
   558
	0.0f,
sl@0
   559
	KMinTReal64,
sl@0
   560
	1.0f,
sl@0
   561
	KMaxTReal64
sl@0
   562
	};
sl@0
   563
const TSelectTest TypeReal64::KTests[]=
sl@0
   564
	{
sl@0
   565
	{_S("IS NULL"),ROW(0)},
sl@0
   566
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   567
	{_S("=0"),ROW(4)},
sl@0
   568
	{_S("<>0"),ALL_ROWS-ROW(4)},
sl@0
   569
	{_S("=-1"),ROW(2)},
sl@0
   570
	{_S("<2"),ROWS(1,6)},
sl@0
   571
	{_S(">=-100000000000000"),ROWS(2,7)},
sl@0
   572
	{_S("<1e-300"),ROWS(1,5)},
sl@0
   573
	{_S(">1e15"),ROW(7)},
sl@0
   574
	{_S("=.5"),NO_ROWS},
sl@0
   575
	{_S("<=0"),ROWS(1,4)},
sl@0
   576
	{_S("<1e40"),ROWS(1,6)}
sl@0
   577
	};
sl@0
   578
sl@0
   579
struct TypeTime
sl@0
   580
	{
sl@0
   581
public:
sl@0
   582
	static const TText* const KType;
sl@0
   583
	static const TTime KValues[];
sl@0
   584
	static const TSelectTest KTests[];
sl@0
   585
	};
sl@0
   586
const TText* const TypeTime::KType=_S("TIME");
sl@0
   587
const TTime TypeTime::KValues[]=
sl@0
   588
	{
sl@0
   589
	TInt64(0u),				// zero date/time
sl@0
   590
	_L(":085815"),			// 8:58:15am
sl@0
   591
	_L("19181010:110000"),	// 11am, 11 Nov 1918
sl@0
   592
	_L("19750226:"),		// midnight, 27 Mar 1975
sl@0
   593
	_L("19961130:235959"),	// 11:59:59pm, 31 Dec 1996
sl@0
   594
	_L("19970000:"),		// midnight, 1 Jan 1997
sl@0
   595
	_L("19970611:210000"),	// 9pm, 12 July 1997
sl@0
   596
	_L("19980309:214500"),	// 9:45pm, 10 April 1998
sl@0
   597
	_L("20700608:")			// midnight, 9 July 2070
sl@0
   598
	};
sl@0
   599
const TSelectTest TypeTime::KTests[]=
sl@0
   600
	{
sl@0
   601
	{_S("IS NULL"),ROW(0)},
sl@0
   602
	{_S("IS NOT NULL"),ALL_ROWS},
sl@0
   603
	{_S("=#12am#"),ROW(1)},
sl@0
   604
	{_S("<#Jan 1 2100#"),ALL_ROWS},
sl@0
   605
	{_S("<>#31/12/1996 23:59:59#"),ALL_ROWS-ROW(5)},
sl@0
   606
	{_S("<#9a#"),ROWS(1,2)},
sl@0
   607
	{_S(">=#11:59:59pm, 31 Dec 1996#"),ROWS(5,9)},
sl@0
   608
	{_S("=#9:45pm 10 April, 1998#"),ROW(8)},
sl@0
   609
	{_S("=#8:58:15#"),ROW(2)}
sl@0
   610
	};
sl@0
   611
sl@0
   612
struct TypeText
sl@0
   613
	{
sl@0
   614
public:
sl@0
   615
	static const TText* const KType;
sl@0
   616
	static const TText* const KValues[];
sl@0
   617
	static TSelectTest KTests[];
sl@0
   618
	};
sl@0
   619
const TText* const TypeText::KType=_S("VARCHAR(100)");
sl@0
   620
const TText* const TypeText::KValues[]=
sl@0
   621
	{
sl@0
   622
	_S(""),					// this should be equivalent to NULL
sl@0
   623
	_S("a"),
sl@0
   624
	_S("aa"),
sl@0
   625
	_S("aba"),
sl@0
   626
	_S("like"),
sl@0
   627
	_S("abcdefghijklmnopqrstuvwxyzlike"),
sl@0
   628
	_S("likeabcdefghijklmnopqrstuvwxyz"),
sl@0
   629
	_S("abcdefghijklmnopqrstuvwxyzlikeabcdefghijklmnopqrstuvwxyz"),
sl@0
   630
	_S("abcdefghijklmnopqrstuvwxyzliveabcdefghijklmnopqrstuvwxyz"),
sl@0
   631
	_S("l'ke"),
sl@0
   632
	_S("'Tis")
sl@0
   633
	};
sl@0
   634
TSelectTest TypeText::KTests[]=
sl@0
   635
	{
sl@0
   636
	{_S("IS NULL"),ROWS(0,1)},
sl@0
   637
	{_S("IS NOT NULL"),ALL_ROWS-ROW(1)},
sl@0
   638
//
sl@0
   639
	{_S("=''"),ROWS(0,1)},					// equivalent to IS NULL
sl@0
   640
	{_S("<>''"),ALL_ROWS-ROW(1)},			// equivalent to IS NOT NULL
sl@0
   641
	{_S(">''"),ALL_ROWS-ROW(1)},			// equivalent to IS NOT NULL
sl@0
   642
	{_S("<''"),NO_ROWS},					// expression is trivially false
sl@0
   643
	{_S("<=''"),ROWS(0,1)},
sl@0
   644
	{_S(">=''"),ALL_ROWS+ROW(0)},			// expression is trivially true
sl@0
   645
//
sl@0
   646
	{_S("LIKE ''"),ROWS(0,1)},			// synonomous with IS NULL
sl@0
   647
	{_S("NOT LIKE ''"),ALL_ROWS-ROW(1)},
sl@0
   648
	{_S("LIKE '?*'"),ALL_ROWS-ROW(1)},	// synonomous with IS NOT NULL
sl@0
   649
	{_S("NOT LIKE '?*'"),ROWS(0,1)},
sl@0
   650
	{_S("LIKE '*'"),ALL_ROWS+ROW(0)},	// trivially true
sl@0
   651
	{_S("NOT LIKE '*'"),NO_ROWS},
sl@0
   652
//
sl@0
   653
	{_S("='a'"),ROW(2)},
sl@0
   654
	{_S("<'ab'"),ROWS(0,3)+ROW(11)},
sl@0
   655
	{_S("<'abc'"),ROWS(0,4)+ROW(11)},
sl@0
   656
	{_S("<'b'"),ROWS(0,4)+ROW(6)+ROWS(8,9)+ROW(11)},
sl@0
   657
	{_S(">'abc'"),ROWS(5,10)},
sl@0
   658
	{_S("='like'"),ROW(5)},
sl@0
   659
	{_S("='l''ke'"),ROW(10)},
sl@0
   660
	{_S("='''Tis'"),ROW(11)},
sl@0
   661
//
sl@0
   662
	{_S("LIKE 'a'"),ROW(2)},
sl@0
   663
	{_S("LIKE 'a*'"),ROWS(2,4)+ROW(6)+ROWS(8,9)},
sl@0
   664
	{_S("LIKE '*a'"),ROWS(2,4)},
sl@0
   665
	{_S("LIKE 'a*a'"),ROWS(3,4)},
sl@0
   666
	{_S("LIKE '*a*'"),ROWS(2,4)+ROWS(6,9)},
sl@0
   667
//
sl@0
   668
	{_S("LIKE 'like'"),ROW(5)},
sl@0
   669
	{_S("LIKE 'l?ke'"),ROW(5)+ROW(10)},
sl@0
   670
	{_S("LIKE 'like*'"),ROW(5)+ROW(7)},
sl@0
   671
	{_S("LIKE '*like'"),ROWS(5,6)},
sl@0
   672
	{_S("LIKE '*like*'"),ROWS(5,8)},
sl@0
   673
	{_S("LIKE '*likeit*'"),NO_ROWS},
sl@0
   674
	{_S("LIKE '*li?e*'"),ROWS(5,9)},
sl@0
   675
	{_S("LIKE '?*li?e*'"),ROW(6)+ROWS(8,9)},
sl@0
   676
	{_S("LIKE '*li?e*?'"),ROWS(7,9)},
sl@0
   677
	{_S("LIKE '?*li?e*?'"),ROWS(8,9)},
sl@0
   678
	{_S("LIKE '*?k?*'"),ROWS(5,10)},
sl@0
   679
	{_S("LIKE '*?i?e*'"),ROWS(5,9)},
sl@0
   680
//
sl@0
   681
	{_S("LIKE '*e*'"),ROWS(5,10)},
sl@0
   682
	{_S("LIKE '*z*k*e*'"),ROW(6)+ROW(8)},
sl@0
   683
	{_S("LIKE '\?\?k?'"),ROW(5)+ROW(10)},
sl@0
   684
	{_S("LIKE '\?\?k*'"),ROW(5)+ROW(7)+ROW(10)},
sl@0
   685
	{_S("LIKE '*''*'"),ROWS(10,11)},
sl@0
   686
	{_S("LIKE '?''\?\?'"),ROW(10)},
sl@0
   687
	{_S("LIKE '?'"),ROW(2)},
sl@0
   688
	{_S("LIKE '\?\?\?\?'"),ROW(5)+ROWS(10,11)},
sl@0
   689
	{_S("LIKE '\?\?*\?\?'"),ROWS(5,11)},
sl@0
   690
	{_S("LIKE '''*'"),ROW(11)}
sl@0
   691
	};
sl@0
   692
sl@0
   693
TEMPLATE_SPECIALIZATION struct SetCol<const TText* const>
sl@0
   694
	{
sl@0
   695
	static void SetColL(TDbColNo aCol,const TAny* aVal)
sl@0
   696
		{
sl@0
   697
		TheTable.SetColL(aCol,TPtrC(*(const TText* const*)aVal));
sl@0
   698
		}
sl@0
   699
	};
sl@0
   700
TEMPLATE_SPECIALIZATION struct SetCol<const TText*>
sl@0
   701
	{
sl@0
   702
	static void SetColL(TDbColNo aCol,const TAny* aVal)
sl@0
   703
		{
sl@0
   704
		TheTable.SetColL(aCol,TPtrC(*(const TText* const*)aVal));
sl@0
   705
		}
sl@0
   706
	};
sl@0
   707
sl@0
   708
struct TypeLongText
sl@0
   709
	{
sl@0
   710
public:
sl@0
   711
	static const TText* const KType;
sl@0
   712
	};
sl@0
   713
const TText* const TypeLongText::KType=_S("LONG VARCHAR");
sl@0
   714
sl@0
   715
class TestPredicate2 : public TestPredicateBase
sl@0
   716
	{
sl@0
   717
public:
sl@0
   718
	static void RunL();
sl@0
   719
	};
sl@0
   720
sl@0
   721
// write rows equivalent to TypeText and use its tests
sl@0
   722
void TestPredicate2::RunL()
sl@0
   723
	{
sl@0
   724
	Create(TypeLongText::KType);
sl@0
   725
	TBuf<1022> fill=_S("abcdefghijklmnopqrstuvqxyz");
sl@0
   726
	fill.AppendFill('.',fill.MaxLength()-fill.Length());
sl@0
   727
sl@0
   728
	for (TInt row=0;row<=TInt(elementsof(TypeText::KValues));++row)
sl@0
   729
		{
sl@0
   730
		TheTable.InsertL();
sl@0
   731
		TheTable.SetColL(1,row);
sl@0
   732
		if (row>0)
sl@0
   733
			{
sl@0
   734
			RDbColWriteStream blob;
sl@0
   735
			blob.OpenLC(TheTable,2);
sl@0
   736
			switch (row)
sl@0
   737
				{
sl@0
   738
			case 0:
sl@0
   739
				break;
sl@0
   740
			case 1: case 2: case 3: case 4: case 5: case 10: case 11:
sl@0
   741
				blob.WriteL(TPtrC(TypeText::KValues[row-1]));
sl@0
   742
				break;
sl@0
   743
			case 6:
sl@0
   744
				blob.WriteL(fill);
sl@0
   745
				blob.WriteL(_L("like"));
sl@0
   746
				break;
sl@0
   747
			case 7:
sl@0
   748
				blob.WriteL(_L("like"));
sl@0
   749
				blob.WriteL(fill);
sl@0
   750
				break;
sl@0
   751
			case 8:
sl@0
   752
				blob.WriteL(fill);
sl@0
   753
				blob.WriteL(_L("like"));
sl@0
   754
				blob.WriteL(fill);
sl@0
   755
				break;
sl@0
   756
			case 9:
sl@0
   757
				blob.WriteL(fill);
sl@0
   758
				blob.WriteL(_L("live"));
sl@0
   759
				blob.WriteL(fill);
sl@0
   760
				break;
sl@0
   761
				}
sl@0
   762
			blob.CommitL();
sl@0
   763
			CleanupStack::PopAndDestroy();
sl@0
   764
			}
sl@0
   765
		TheTable.PutL();
sl@0
   766
		}
sl@0
   767
	TheTable.Close();
sl@0
   768
	TInt r=TheDatabase.Commit();
sl@0
   769
	TEST2(r, KErrNone);
sl@0
   770
	TestViewL(TypeText::KTests,elementsof(TypeText::KTests),elementsof(TypeText::KValues));
sl@0
   771
	CDbKey& key=*CDbKey::NewLC();
sl@0
   772
	key.AddL(TDbKeyCol(_L("Test"),120));
sl@0
   773
	r=TheDatabase.CreateIndex(_L("Key"),_L("Compare"),key);
sl@0
   774
	TEST2(r, KErrNone);
sl@0
   775
	CleanupStack::PopAndDestroy();
sl@0
   776
	TestViewL(TypeText::KTests,elementsof(TypeText::KTests),elementsof(TypeText::KValues));
sl@0
   777
	r=TheDatabase.Execute(_L("DROP TABLE Compare"));
sl@0
   778
	TEST2(r, KErrNone);
sl@0
   779
	}
sl@0
   780
sl@0
   781
/**
sl@0
   782
* Utility for DEF063276 fix.
sl@0
   783
*/
sl@0
   784
sl@0
   785
_LIT(KTypeTextKTests44, "Z:\\test\\TypeTextKTests44.dat");
sl@0
   786
_LIT(KTypeTextKTests46, "Z:\\test\\TypeTextKTests46.dat");
sl@0
   787
_LIT(KTypeTextKTests47, "Z:\\test\\TypeTextKTests47.dat");
sl@0
   788
sl@0
   789
static void ReadDesc(TDes& aDes, const TDesC& aFilename, RFs& aFs)
sl@0
   790
	{
sl@0
   791
    TheTest.Printf(_L("---ReadDesc(), aFilename=%S\r\n"), &aFilename);
sl@0
   792
	RFile file;
sl@0
   793
	TInt err = file.Open(aFs, aFilename, EFileRead);
sl@0
   794
	TheTest.Printf(_L("Open file aFilename=%S err = %d\r\n"), &aFilename, err);
sl@0
   795
	TEST2(err, KErrNone);
sl@0
   796
sl@0
   797
	TPtr8 ptr(reinterpret_cast<TUint8*>(const_cast<TUint16*>(aDes.Ptr())), aDes.MaxSize());
sl@0
   798
	err = file.Read(ptr);
sl@0
   799
	TheTest.Printf(_L("Read file aFilename=%S err = %d\r\n"), &aFilename, err);
sl@0
   800
	TEST2(err, KErrNone);
sl@0
   801
	aDes.SetLength(ptr.Length() / sizeof(TText));
sl@0
   802
	file.Close();
sl@0
   803
	}
sl@0
   804
sl@0
   805
/**
sl@0
   806
@SYMTestCaseID          SYSLIB-DBMS-CT-0633
sl@0
   807
@SYMTestCaseDesc        Tests the Predicate operators for all types
sl@0
   808
@SYMTestPriority        Medium
sl@0
   809
@SYMTestActions         Attempt to check with different types
sl@0
   810
@SYMTestExpectedResults Test must not fail
sl@0
   811
@SYMREQ                 REQ0000
sl@0
   812
*/
sl@0
   813
LOCAL_C void TestPredicatesL()
sl@0
   814
	{
sl@0
   815
	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0633 init "));
sl@0
   816
	CreateDatabase();
sl@0
   817
	TestPredicate<TypeBit>::RunL();
sl@0
   818
	TestPredicate<TypeUint8>::RunL();
sl@0
   819
	TestPredicate<TypeUint16>::RunL();
sl@0
   820
	TestPredicate<TypeUint32>::RunL();
sl@0
   821
	TestPredicate<TypeInt8>::RunL();
sl@0
   822
	TestPredicate<TypeInt16>::RunL();
sl@0
   823
	TestPredicate<TypeInt32>::RunL();
sl@0
   824
	TestPredicate<TypeInt64>::RunL();
sl@0
   825
	TestPredicate<TypeReal32>::RunL();
sl@0
   826
	TestPredicate<TypeReal64>::RunL();
sl@0
   827
	TestPredicate<TypeTime>::RunL();
sl@0
   828
sl@0
   829
	/**
sl@0
   830
	* Work around for DEF063276.
sl@0
   831
	* These literals are now loaded from Z:\test\TypeTextKTests44.dat,
sl@0
   832
	* Z:\test\data\TypeTextKTests44.dat and Z:\test\TypeTextKTests44.dat respectively.
sl@0
   833
	* Bullseye Coverage corrupts these literals to avoid this they are stored in files as to not be touched by Bullseye Coverage.
sl@0
   834
	*/
sl@0
   835
sl@0
   836
	TBuf<16> buf44;
sl@0
   837
	ReadDesc(buf44, KTypeTextKTests44, TheFs);
sl@0
   838
	TypeText::KTests[44].iSearchCondition = const_cast<TText*>(buf44.PtrZ());
sl@0
   839
sl@0
   840
	TBuf<32> buf46(TypeText::KTests[46].iSearchCondition);
sl@0
   841
	ReadDesc(buf46, KTypeTextKTests46, TheFs);
sl@0
   842
	TypeText::KTests[46].iSearchCondition = const_cast<TText*>(buf46.PtrZ());
sl@0
   843
sl@0
   844
	TBuf<32> buf47(TypeText::KTests[47].iSearchCondition);
sl@0
   845
	ReadDesc(buf47, KTypeTextKTests47, TheFs);
sl@0
   846
	TypeText::KTests[47].iSearchCondition = const_cast<TText*>(buf47.PtrZ());
sl@0
   847
sl@0
   848
	// End fix.
sl@0
   849
sl@0
   850
	TestPredicate<TypeText>::RunL();
sl@0
   851
	TestPredicate2::RunL();
sl@0
   852
	CloseDatabase();
sl@0
   853
	TheTest.End();
sl@0
   854
	}
sl@0
   855
sl@0
   856
/**
sl@0
   857
@SYMTestCaseID          SYSLIB-DBMS-CT-0634
sl@0
   858
@SYMTestCaseDesc        DML Query test
sl@0
   859
                        Test for RDbNamedDatabase::Execute() function
sl@0
   860
@SYMTestPriority        Medium
sl@0
   861
@SYMTestActions         Tests for CREATE TABLE,CREATE UNIQUE INDEX,INSET INTO,UPDATE,DELETE,DROP queries
sl@0
   862
@SYMTestExpectedResults Test must not fail
sl@0
   863
@SYMREQ                 REQ0000
sl@0
   864
*/
sl@0
   865
LOCAL_C void TestDataModificationlanguage()
sl@0
   866
	{
sl@0
   867
	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0634 init "));
sl@0
   868
	Connect();
sl@0
   869
	ShareDatabase();
sl@0
   870
	TInt r=TheDatabase.Execute(_L("CREATE TABLE test (ID INTEGER NOT NULL,SALARY DOUBLE)"));
sl@0
   871
	TEST2(r, KErrNone);
sl@0
   872
	r=TheDatabase.Execute(_L("CREATE UNIQUE INDEX key ON test (ID)"));
sl@0
   873
	TEST2(r, KErrNone);
sl@0
   874
sl@0
   875
	TheTest.Next(_L("insert-statements"));
sl@0
   876
	r=TheDatabase.Execute(_L("INSERT INTO test VALUES (0,0)"));
sl@0
   877
	TEST(r==1);
sl@0
   878
	r=TheDatabase.Execute(_L("INSERT INTO test (ID) VALUES (1)"));
sl@0
   879
	TEST(r==1);
sl@0
   880
	r=TheDatabase.Execute(_L("INSERT INTO test (SALARY,ID) VALUES (20.4,2)"));
sl@0
   881
	TEST(r==1);
sl@0
   882
sl@0
   883
	TheTest.Next(_L("update-statements"));
sl@0
   884
	r=TheDatabase.Execute(_L("UPDATE test SET SALARY=30000 WHERE ID=1"));
sl@0
   885
	TEST(r==1);
sl@0
   886
sl@0
   887
	TheTest.Next(_L("delete-statements"));
sl@0
   888
	r=TheDatabase.Execute(_L("DELETE FROM test WHERE SALARY<40"));
sl@0
   889
	TEST(r==2);
sl@0
   890
	r=TheDatabase.Execute(_L("DELETE FROM test"));
sl@0
   891
	TEST(r==1);
sl@0
   892
	r=TheDatabase.Execute(_L("DROP TABLE test"));
sl@0
   893
	TEST2(r, KErrNone);
sl@0
   894
//
sl@0
   895
	TheTest.Next(_L("larger table"));
sl@0
   896
	r=TheDatabase.Execute(_L("CREATE TABLE test (ID COUNTER,DATA INTEGER)"));
sl@0
   897
	TEST2(r, KErrNone);
sl@0
   898
sl@0
   899
	TheTest.Next(_L("insert"));
sl@0
   900
	r=TheDatabase.Begin();
sl@0
   901
	TEST2(r, KErrNone);
sl@0
   902
	TBuf<256> sql;
sl@0
   903
	for (TInt ii=0;ii<100;++ii)
sl@0
   904
		{
sl@0
   905
		sql.Format(_L("INSERT INTO test (DATA) VALUES (%D)"),100-ii);
sl@0
   906
		r=TheDatabase.Execute(sql);
sl@0
   907
		TEST(r==1);
sl@0
   908
		}
sl@0
   909
	r=TheDatabase.Commit();
sl@0
   910
	TEST2(r, KErrNone);
sl@0
   911
sl@0
   912
	TheTest.Next(_L("update"));
sl@0
   913
	r=TheDatabase.Execute(_L("UPDATE test SET DATA=200 WHERE ID>=40 AND ID<60"));
sl@0
   914
	TEST(r==20);
sl@0
   915
sl@0
   916
	TheTest.Next(_L("delete"));
sl@0
   917
	r=TheDatabase.Execute(_L("DELETE FROM test WHERE DATA>90"));
sl@0
   918
	TEST(r==30);
sl@0
   919
sl@0
   920
	TheTest.Next(_L("update"));
sl@0
   921
	r=TheDatabase.Execute(_L("UPDATE test SET DATA=-1"));
sl@0
   922
	TEST(r==70);
sl@0
   923
sl@0
   924
	TheTest.Next(_L("delete"));
sl@0
   925
	r=TheDatabase.Execute(_L("DELETE FROM test"));
sl@0
   926
	TEST(r==70);
sl@0
   927
	r=TheDatabase.Execute(_L("DROP TABLE test"));
sl@0
   928
	TEST2(r, KErrNone);
sl@0
   929
	CloseDatabase();
sl@0
   930
	Disconnect();
sl@0
   931
	TheTest.End();
sl@0
   932
	}
sl@0
   933
sl@0
   934
/**
sl@0
   935
@SYMTestCaseID          SYSLIB-DBMS-CT-0635
sl@0
   936
@SYMTestCaseDesc        DBMS SQL parsing and execution tests.Tests for database index order
sl@0
   937
						Test for RDbNamedDatabase::Execute() function
sl@0
   938
@SYMTestPriority        Medium
sl@0
   939
@SYMTestActions         Tests for order by index
sl@0
   940
@SYMTestExpectedResults Test must not fail
sl@0
   941
@SYMREQ                 REQ0000
sl@0
   942
*/
sl@0
   943
LOCAL_C void TestOrderByL()
sl@0
   944
	{
sl@0
   945
	static const TReal TestDataSalary[]={10,34000,15000,53200,17800,240000};
sl@0
   946
	static const TText* const TestDataNames[]={_S("gopher"),_S("james '007' bond"),_S("moneypenny"),_S("Q"),_S("james '007' bond"),_S("M")};
sl@0
   947
//
sl@0
   948
	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0635 init "));
sl@0
   949
	Connect();
sl@0
   950
	ShareDatabase();
sl@0
   951
	TheDatabase.Begin();
sl@0
   952
	TInt r=TheDatabase.Execute(_L("CREATE TABLE test (ID INTEGER NOT NULL,SALARY DOUBLE,NAME VARCHAR)"));
sl@0
   953
	TEST2(r, KErrNone);
sl@0
   954
	r=TheDatabase.Execute(_L("CREATE UNIQUE INDEX key ON test (ID)"));
sl@0
   955
	TEST2(r, KErrNone);
sl@0
   956
sl@0
   957
	TheTest.Next(_L("insert data"));
sl@0
   958
	r=TheView.Prepare(TheDatabase,_L("SELECT ID,SALARY,NAME FROM test"),TheView.EInsertOnly);
sl@0
   959
	TEST2(r, KErrNone);
sl@0
   960
	TInt ii;
sl@0
   961
	for (ii=0;ii<6;++ii)
sl@0
   962
		{
sl@0
   963
		TheView.InsertL();
sl@0
   964
		TheView.SetColL(1,6-ii);
sl@0
   965
		TheView.SetColL(2,TestDataSalary[ii]);
sl@0
   966
		TheView.SetColL(3,TPtrC(TestDataNames[ii]));
sl@0
   967
		TheView.PutL();
sl@0
   968
		}
sl@0
   969
	r=TheDatabase.Commit();
sl@0
   970
	TEST2(r, KErrNone);
sl@0
   971
	TheView.Close();
sl@0
   972
sl@0
   973
	TheTest.Next(_L("Order by <index>"));
sl@0
   974
	// test the index is used here
sl@0
   975
	r=TheView.Prepare(TheDatabase,_L("SELECT ID FROM test ORDER BY ID"));
sl@0
   976
	TEST2(r, KErrNone);
sl@0
   977
	TEST(!TheView.Unevaluated());
sl@0
   978
	TInt c=0;
sl@0
   979
	if (TheView.FirstL())
sl@0
   980
		{
sl@0
   981
		++c;
sl@0
   982
		TheView.GetL();
sl@0
   983
		TInt last=TheView.ColInt(1);
sl@0
   984
		while (TheView.NextL())
sl@0
   985
			{
sl@0
   986
			++c;
sl@0
   987
			TheView.GetL();
sl@0
   988
			TInt v=TheView.ColInt(1);
sl@0
   989
			TEST(v>last);
sl@0
   990
			last=v;
sl@0
   991
			}
sl@0
   992
		}
sl@0
   993
	TEST(c==6);
sl@0
   994
	TEST(c==TheView.CountL());
sl@0
   995
	TheView.Close();
sl@0
   996
sl@0
   997
	TheTest.Next(_L("Order by <no-index> 1"));
sl@0
   998
	// test that no index is used here
sl@0
   999
	r=TheView.Prepare(TheDatabase,_L("SELECT SALARY FROM test ORDER BY SALARY"));
sl@0
  1000
	TEST2(r, KErrNone);
sl@0
  1001
	TEST(TheView.Unevaluated());
sl@0
  1002
	r=TheView.EvaluateAll();
sl@0
  1003
	TEST2(r, KErrNone);
sl@0
  1004
	c=0;
sl@0
  1005
	if (TheView.FirstL())
sl@0
  1006
		{
sl@0
  1007
		++c;
sl@0
  1008
		TheView.GetL();
sl@0
  1009
		TReal last=TheView.ColReal(1);
sl@0
  1010
		while (TheView.NextL())
sl@0
  1011
			{
sl@0
  1012
			++c;
sl@0
  1013
			TheView.GetL();
sl@0
  1014
			TReal v=TheView.ColReal(1);
sl@0
  1015
			TEST(v>=last);
sl@0
  1016
			last=v;
sl@0
  1017
			}
sl@0
  1018
		}
sl@0
  1019
	TEST(c==6);
sl@0
  1020
	TEST(c==TheView.CountL());
sl@0
  1021
	TheView.Close();
sl@0
  1022
sl@0
  1023
	TheTest.Next(_L("Order by <no-index> 2"));
sl@0
  1024
	// test that no index is used here
sl@0
  1025
	r=TheView.Prepare(TheDatabase,_L("SELECT SALARY FROM test ORDER BY SALARY,NAME"));
sl@0
  1026
	TEST2(r, KErrNone);
sl@0
  1027
	TEST(TheView.Unevaluated());
sl@0
  1028
	r=TheView.EvaluateAll();
sl@0
  1029
	TEST2(r, KErrNone);
sl@0
  1030
	c=0;
sl@0
  1031
	if (TheView.FirstL())
sl@0
  1032
		{
sl@0
  1033
		++c;
sl@0
  1034
		TheView.GetL();
sl@0
  1035
		TReal last=TheView.ColReal(1);
sl@0
  1036
		while (TheView.NextL())
sl@0
  1037
			{
sl@0
  1038
			++c;
sl@0
  1039
			TheView.GetL();
sl@0
  1040
			TReal v=TheView.ColReal(1);
sl@0
  1041
			TEST(v>=last);
sl@0
  1042
			last=v;
sl@0
  1043
			}
sl@0
  1044
		}
sl@0
  1045
	TEST(c==6);
sl@0
  1046
	TEST(c==TheView.CountL());
sl@0
  1047
	TheView.Close();
sl@0
  1048
sl@0
  1049
	TheTest.Next(_L("Order by <no-index> 3"));
sl@0
  1050
	// test that no index is used here
sl@0
  1051
	r=TheView.Prepare(TheDatabase,_L("SELECT NAME FROM test ORDER BY NAME"));
sl@0
  1052
	TEST2(r, KErrNone);
sl@0
  1053
	TEST(TheView.Unevaluated());
sl@0
  1054
	r=TheView.EvaluateAll();
sl@0
  1055
	TEST2(r, KErrNone);
sl@0
  1056
	c=0;
sl@0
  1057
	if (TheView.FirstL())
sl@0
  1058
		{
sl@0
  1059
		++c;
sl@0
  1060
		TheView.GetL();
sl@0
  1061
		TBuf<50> last=TheView.ColDes(1);
sl@0
  1062
		while (TheView.NextL())
sl@0
  1063
			{
sl@0
  1064
			++c;
sl@0
  1065
			TheView.GetL();
sl@0
  1066
			TPtrC v=TheView.ColDes(1);
sl@0
  1067
			TEST(v>=last);
sl@0
  1068
			last=v;
sl@0
  1069
			}
sl@0
  1070
		}
sl@0
  1071
	TEST(c==6);
sl@0
  1072
	TEST(c==TheView.CountL());
sl@0
  1073
	TheView.Close();
sl@0
  1074
sl@0
  1075
	TheTest.Next(_L("Order by <no-index> 4"));
sl@0
  1076
	// test that no index is used here
sl@0
  1077
	r=TheView.Prepare(TheDatabase,_L("SELECT NAME FROM test ORDER BY NAME,SALARY"));
sl@0
  1078
	TEST2(r, KErrNone);
sl@0
  1079
	TEST(TheView.Unevaluated());
sl@0
  1080
	r=TheView.EvaluateAll();
sl@0
  1081
	TEST2(r, KErrNone);
sl@0
  1082
	c=0;
sl@0
  1083
	if (TheView.FirstL())
sl@0
  1084
		{
sl@0
  1085
		++c;
sl@0
  1086
		TheView.GetL();
sl@0
  1087
		TBuf<50> last=TheView.ColDes(1);
sl@0
  1088
		while (TheView.NextL())
sl@0
  1089
			{
sl@0
  1090
			++c;
sl@0
  1091
			TheView.GetL();
sl@0
  1092
			TPtrC v=TheView.ColDes(1);
sl@0
  1093
			TEST(v>=last);
sl@0
  1094
			last=v;
sl@0
  1095
			}
sl@0
  1096
		}
sl@0
  1097
	TEST(c==6);
sl@0
  1098
	TEST(c==TheView.CountL());
sl@0
  1099
	TheView.Close();
sl@0
  1100
sl@0
  1101
	TheTest.Next(_L("Order by + search <no-index>"));
sl@0
  1102
	// test that no index is used here
sl@0
  1103
	r=TheView.Prepare(TheDatabase,_L("SELECT ID,SALARY FROM test WHERE SALARY>30000 ORDER BY SALARY DESC"));
sl@0
  1104
	TEST2(r, KErrNone);
sl@0
  1105
	TEST(TheView.Unevaluated());
sl@0
  1106
	r=TheView.EvaluateAll();
sl@0
  1107
	TEST2(r, KErrNone);
sl@0
  1108
	c=0;
sl@0
  1109
	if (TheView.FirstL())
sl@0
  1110
		{
sl@0
  1111
		++c;
sl@0
  1112
		TheView.GetL();
sl@0
  1113
		TReal last=TheView.ColReal(2);
sl@0
  1114
		while (TheView.NextL())
sl@0
  1115
			{
sl@0
  1116
			++c;
sl@0
  1117
			TheView.GetL();
sl@0
  1118
			TReal v=TheView.ColReal(2);
sl@0
  1119
			TEST(v<=last);
sl@0
  1120
			last=v;
sl@0
  1121
			}
sl@0
  1122
		}
sl@0
  1123
	TEST(c==3);
sl@0
  1124
	TEST(c==TheView.CountL());
sl@0
  1125
	TheView.Close();
sl@0
  1126
//
sl@0
  1127
	CloseDatabase();
sl@0
  1128
	Disconnect();
sl@0
  1129
sl@0
  1130
	TheTest.Next(_L("Order by <index> Finished"));
sl@0
  1131
	TheTest.End();
sl@0
  1132
	}
sl@0
  1133
sl@0
  1134
LOCAL_C void doMain()
sl@0
  1135
	{
sl@0
  1136
	TheTest.Start(_L("Predicate tests"));
sl@0
  1137
	TRAPD(errCode, TestPredicatesL());
sl@0
  1138
	TEST2(errCode, KErrNone);
sl@0
  1139
sl@0
  1140
	TheTest.Next(_L("DML execution"));
sl@0
  1141
	TestDataModificationlanguage();
sl@0
  1142
sl@0
  1143
	TheTest.Next(_L("ORDER BY clause"));
sl@0
  1144
	TRAP(errCode, TestOrderByL());
sl@0
  1145
	TEST2(errCode, KErrNone);
sl@0
  1146
	TheTest.End();
sl@0
  1147
	}
sl@0
  1148
sl@0
  1149
// Prepare the test directory.
sl@0
  1150
LOCAL_C void setupTestDirectory()
sl@0
  1151
    {
sl@0
  1152
	TInt r=TheFs.Connect();
sl@0
  1153
	TEST2(r, KErrNone);
sl@0
  1154
//
sl@0
  1155
	r=TheFs.MkDir(KTestDatabase);
sl@0
  1156
	TEST(r==KErrNone || r==KErrAlreadyExists);
sl@0
  1157
	}
sl@0
  1158
sl@0
  1159
// Initialise the cleanup stack.
sl@0
  1160
LOCAL_C void setupCleanup()
sl@0
  1161
    {
sl@0
  1162
	TheTrapCleanup=CTrapCleanup::New();
sl@0
  1163
	TEST(TheTrapCleanup!=NULL);
sl@0
  1164
	TRAPD(r,\
sl@0
  1165
		{\
sl@0
  1166
		for (TInt i=KTestCleanupStack;i>0;i--)\
sl@0
  1167
			CleanupStack::PushL((TAny*)0);\
sl@0
  1168
		CleanupStack::Pop(KTestCleanupStack);\
sl@0
  1169
		});
sl@0
  1170
	TEST2(r, KErrNone);
sl@0
  1171
	}
sl@0
  1172
sl@0
  1173
// Test streaming conversions.
sl@0
  1174
GLDEF_C TInt E32Main()
sl@0
  1175
    {
sl@0
  1176
	TheTest.Title();
sl@0
  1177
	setupTestDirectory();
sl@0
  1178
	setupCleanup();
sl@0
  1179
	__UHEAP_MARK;
sl@0
  1180
//
sl@0
  1181
	TheTest.Start(_L("Standard database"));
sl@0
  1182
	doMain();
sl@0
  1183
sl@0
  1184
	// clean up data file used by this test - must be done before call to End() - DEF047652
sl@0
  1185
	::TestCleanup();
sl@0
  1186
	TheTest.End();
sl@0
  1187
//
sl@0
  1188
	__UHEAP_MARKEND;
sl@0
  1189
sl@0
  1190
sl@0
  1191
	delete TheTrapCleanup;
sl@0
  1192
	TheTest.Close();
sl@0
  1193
	return 0;
sl@0
  1194
    }