os/persistentdata/persistentstorage/sql/TEST/t_sqlperformance.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) 2006-2010 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 <e32test.h>
sl@0
    17
#include <e32math.h>
sl@0
    18
#include <bautils.h>
sl@0
    19
#include <hal.h>
sl@0
    20
#include <stdlib.h>
sl@0
    21
#include <sqldb.h>
sl@0
    22
#include "t_sqlcmdlineutil.h"
sl@0
    23
#include "SqlSrvStrings.h"
sl@0
    24
#include "sqlite3.h"
sl@0
    25
#include "SqliteSymbian.h"
sl@0
    26
sl@0
    27
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    28
sl@0
    29
RTest TheTest(_L("t_sqlperformance test"));
sl@0
    30
RFs   TheFs;
sl@0
    31
TBuf<200> TheTestTitle;
sl@0
    32
TCmdLineParams TheCmdLineParams;
sl@0
    33
TBuf8<200> TheSqlConfigString;
sl@0
    34
sl@0
    35
_LIT(KUtf8,  "UTF8 ");
sl@0
    36
_LIT(KUtf16, "UTF16");
sl@0
    37
sl@0
    38
TFileName TheSecureDbName;
sl@0
    39
TFileName TheNonSecureDbName;
sl@0
    40
TFileName TheNonSecureDbName2;
sl@0
    41
TFileName TheNonSecureTmpDbName;
sl@0
    42
TFileName TheSglRecDbFileName;
sl@0
    43
sl@0
    44
_LIT(KSqlServerPrivateDir, "\\private\\10281e17\\");
sl@0
    45
sl@0
    46
_LIT(KCreateDbScript, "z:\\test\\contacts_schema_to_vendors.sql");
sl@0
    47
_LIT(KFillDbScript, "z:\\test\\add_simple_contacts.sql");
sl@0
    48
sl@0
    49
_LIT8(KCommitStr8, "COMMIT;");
sl@0
    50
_LIT16(KCommitStr16, "COMMIT;");
sl@0
    51
sl@0
    52
_LIT8(KUpdateSql8, "UPDATE IDENTITYTABLE SET CM_FIRSTNAME=:V1, CM_LASTNAME=:V2,CM_COMPANYNAME=:V3 WHERE PARENT_CMID=:ID");
sl@0
    53
_LIT16(KUpdateSql16, "UPDATE IDENTITYTABLE SET CM_FIRSTNAME=:V1, CM_LASTNAME=:V2,CM_COMPANYNAME=:V3 WHERE PARENT_CMID=:ID");
sl@0
    54
sl@0
    55
_LIT8(KUpdateSql2_8, "UPDATE IDENTITYTABLE SET CM_FIRSTNAME='%S%d',CM_LASTNAME='%S%d',CM_COMPANYNAME='%S%d' WHERE PARENT_CMID=%d");
sl@0
    56
_LIT16(KUpdateSql2_16, "UPDATE IDENTITYTABLE SET CM_FIRSTNAME='%S%d',CM_LASTNAME='%S%d',CM_COMPANYNAME='%S%d' WHERE PARENT_CMID=%d");
sl@0
    57
sl@0
    58
_LIT8(KSelectSql8, "SELECT CM_FIRSTNAME, CM_LASTNAME, CM_COMPANYNAME FROM IDENTITYTABLE WHERE PARENT_CMID > 50");
sl@0
    59
_LIT16(KSelectSql16, "SELECT CM_FIRSTNAME, CM_LASTNAME, CM_COMPANYNAME FROM IDENTITYTABLE WHERE PARENT_CMID > 50");
sl@0
    60
sl@0
    61
_LIT8(KDeleteSql8, "DELETE FROM IDENTITYTABLE WHERE PARENT_CMID > 50");
sl@0
    62
_LIT16(KDeleteSql16, "DELETE FROM IDENTITYTABLE WHERE PARENT_CMID > 50");
sl@0
    63
sl@0
    64
extern TPtrC GetFirstSqlStmt(TPtr& aString);
sl@0
    65
sl@0
    66
_LIT(KFirstName, "FirstName-");
sl@0
    67
_LIT(KLastName, "LastName-");
sl@0
    68
_LIT(KCompanyName, "CompanyName-");
sl@0
    69
sl@0
    70
_LIT(KFirstName2, "12345678-");
sl@0
    71
_LIT(KLastName2, "ABCDEFGHIJK-");
sl@0
    72
_LIT(KCompanyName2, "KKKKKKKKKK10-");
sl@0
    73
sl@0
    74
const char* KRawPrmName1 = ":V1";
sl@0
    75
const char* KRawPrmName2 = ":V2";
sl@0
    76
const char* KRawPrmName3 = ":V3";
sl@0
    77
const char* KRawPrmName4 = ":ID";
sl@0
    78
sl@0
    79
_LIT(KPrmName1, ":V1");	
sl@0
    80
_LIT(KPrmName2, ":V2");	
sl@0
    81
_LIT(KPrmName3, ":V3");	
sl@0
    82
_LIT(KPrmName4, ":ID");	
sl@0
    83
sl@0
    84
const TInt KTestTecordCount = 1000;
sl@0
    85
	
sl@0
    86
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    87
sl@0
    88
void TestEnvDestroy()
sl@0
    89
	{
sl@0
    90
	(void)RSqlDatabase::Delete(TheNonSecureTmpDbName);
sl@0
    91
	(void)RSqlDatabase::Delete(TheNonSecureDbName2);
sl@0
    92
	(void)RSqlDatabase::Delete(TheNonSecureDbName);
sl@0
    93
	(void)RSqlDatabase::Delete(TheSecureDbName);
sl@0
    94
	TheFs.Close();
sl@0
    95
	}
sl@0
    96
sl@0
    97
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    98
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    99
//Test macros and functions
sl@0
   100
void Check1(TInt aValue, TInt aLine)
sl@0
   101
	{
sl@0
   102
	if(!aValue)
sl@0
   103
		{
sl@0
   104
		TestEnvDestroy();
sl@0
   105
		TheTest.Printf(_L("*** Line %d\r\n"), aLine);
sl@0
   106
		TheTest(EFalse, aLine);
sl@0
   107
		}
sl@0
   108
	}
sl@0
   109
void Check2(TInt aValue, TInt aExpected, TInt aLine)
sl@0
   110
	{
sl@0
   111
	if(aValue != aExpected)
sl@0
   112
		{
sl@0
   113
		TestEnvDestroy();
sl@0
   114
		TheTest.Printf(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
sl@0
   115
		TheTest(EFalse, aLine);
sl@0
   116
		}
sl@0
   117
	}
sl@0
   118
#define TEST(arg) ::Check1((arg), __LINE__)
sl@0
   119
#define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
sl@0
   120
sl@0
   121
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   122
sl@0
   123
void TestEnvInit()
sl@0
   124
    {
sl@0
   125
	TInt err = TheFs.Connect();
sl@0
   126
	TEST2(err, KErrNone);
sl@0
   127
sl@0
   128
	err = TheFs.MkDir(TheNonSecureDbName);
sl@0
   129
	TEST(err == KErrNone || err == KErrAlreadyExists);
sl@0
   130
	}
sl@0
   131
sl@0
   132
//Reads a SQL file and returns the file content as HBUFC string.
sl@0
   133
//The caller is responsible for destroying the returned HBUFC object.
sl@0
   134
template <class HBUFC> HBUFC* ReadSqlScript(const TDesC& aSqlFileName)
sl@0
   135
	{
sl@0
   136
	RFile file;
sl@0
   137
	TEST2(file.Open(TheFs, aSqlFileName, EFileRead), KErrNone);
sl@0
   138
	
sl@0
   139
	TInt size = 0;
sl@0
   140
	TEST2(file.Size(size), KErrNone);
sl@0
   141
	
sl@0
   142
	HBufC8* sql = HBufC8::New(size);
sl@0
   143
	TEST(sql != NULL);
sl@0
   144
	
sl@0
   145
	TPtr8 ptr = sql->Des();
sl@0
   146
	TEST2(file.Read(ptr, size), KErrNone);
sl@0
   147
sl@0
   148
	file.Close();
sl@0
   149
	
sl@0
   150
	HBUFC* sql2 = HBUFC::New(size + 1);
sl@0
   151
	TEST(sql2 != NULL);
sl@0
   152
	sql2->Des().Copy(sql->Des());
sl@0
   153
	sql2->Des().Append(TChar(0));
sl@0
   154
	delete sql;
sl@0
   155
	
sl@0
   156
	return sql2;
sl@0
   157
	}
sl@0
   158
//Explicit ReadSqlScript() template instantiations.
sl@0
   159
template HBufC8* ReadSqlScript<HBufC8>(const TDesC&);
sl@0
   160
template HBufC16* ReadSqlScript<HBufC16>(const TDesC&);
sl@0
   161
sl@0
   162
//Searches for the next aCommitStr appearance in aSqlScript string and returns a PTRC object holding
sl@0
   163
//the SQL strings from the beginning of aSqlScript till the aCommitStr (including it).
sl@0
   164
template <class PTRC, class DESC> PTRC GetNextTrans(PTRC& aSqlScript, const DESC& aCommitStr)
sl@0
   165
	{
sl@0
   166
	PTRC res(NULL, 0);
sl@0
   167
	TInt pos = aSqlScript.FindF(aCommitStr);
sl@0
   168
	if(pos >= 0)
sl@0
   169
		{
sl@0
   170
		pos += aCommitStr.Length();
sl@0
   171
		res.Set(aSqlScript.Left(pos));
sl@0
   172
		aSqlScript.Set(aSqlScript.Mid(pos));
sl@0
   173
		}
sl@0
   174
	return res;
sl@0
   175
	}
sl@0
   176
//Explicit GetNextTrans() template instantiations.
sl@0
   177
template TPtrC8 GetNextTrans<TPtrC8, TDesC8>(TPtrC8&, const TDesC8&);
sl@0
   178
template TPtrC16 GetNextTrans<TPtrC16, TDesC16>(TPtrC16&, const TDesC16&);
sl@0
   179
sl@0
   180
//Prints aTicks parameter (converted to ms)
sl@0
   181
void PrintStats(TUint32 aStartTicks, TUint32 aEndTicks)
sl@0
   182
	{
sl@0
   183
	static TInt freq = 0;
sl@0
   184
	if(freq == 0)
sl@0
   185
		{
sl@0
   186
		TEST2(HAL::Get(HAL::EFastCounterFrequency, freq), KErrNone);
sl@0
   187
		}
sl@0
   188
	TInt64 diffTicks = (TInt64)aEndTicks - (TInt64)aStartTicks;
sl@0
   189
	if(diffTicks < 0)
sl@0
   190
		{
sl@0
   191
		diffTicks = KMaxTUint32 + diffTicks + 1;
sl@0
   192
		}
sl@0
   193
	const TInt KMicroSecIn1Sec = 1000000;
sl@0
   194
	TInt32 us = (diffTicks * KMicroSecIn1Sec) / freq;
sl@0
   195
	TheTest.Printf(_L("####Execution time: %d ms\r\n"), us / 1000);
sl@0
   196
	}
sl@0
   197
	
sl@0
   198
void PrintFileSize(const TDesC& aFileName)
sl@0
   199
	{
sl@0
   200
	TParse parse;
sl@0
   201
	parse.Set(aFileName, &KSqlServerPrivateDir(), 0);
sl@0
   202
	RFile file;
sl@0
   203
	TInt err = file.Open(TheFs, parse.FullName(), EFileRead);
sl@0
   204
	TEST2(err, KErrNone);
sl@0
   205
	TInt size = 0;
sl@0
   206
	err = file.Size(size);
sl@0
   207
	TEST2(err, KErrNone);
sl@0
   208
	TheTest.Printf(_L("####FileSize: %d\r\n"), size);
sl@0
   209
	file.Close();
sl@0
   210
	}
sl@0
   211
sl@0
   212
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   213
///////     SQL SERVER performance tests
sl@0
   214
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   215
	
sl@0
   216
enum TDbType {ENonSecureDb, ESecureDb};
sl@0
   217
sl@0
   218
//Template class offering Create() and Open() methods for creating/opening a RSqlDatabase object
sl@0
   219
template <TDbType TYPE> class TDbHelper
sl@0
   220
	{
sl@0
   221
public:	
sl@0
   222
	static void Create(const TDesC& aDbName);
sl@0
   223
	static RSqlDatabase Open(const TDesC& aDbName);
sl@0
   224
	};
sl@0
   225
sl@0
   226
//Creates aDb database schema.
sl@0
   227
void CreateDbSchema(RSqlDatabase& aDb)
sl@0
   228
	{
sl@0
   229
	HBufC8* createDbScript = ReadSqlScript<HBufC8>(KCreateDbScript());
sl@0
   230
	TInt err = aDb.Exec(createDbScript->Des());
sl@0
   231
	TEST(err >= 0);	
sl@0
   232
	delete createDbScript;
sl@0
   233
	}
sl@0
   234
sl@0
   235
//Explicit TDbHelper class specialization for creating/opening a nonsecure RSqlDatabase object
sl@0
   236
template <> class TDbHelper<ENonSecureDb>
sl@0
   237
	{
sl@0
   238
public:	
sl@0
   239
	static void Create(const TDesC& aDbName)
sl@0
   240
		{
sl@0
   241
		RSqlDatabase::Delete(aDbName);
sl@0
   242
		RSqlDatabase db;
sl@0
   243
		TInt err = db.Create(aDbName, &TheSqlConfigString);
sl@0
   244
		TEST2(err, KErrNone);
sl@0
   245
		CreateDbSchema(db);
sl@0
   246
		db.Close();
sl@0
   247
		}
sl@0
   248
	static RSqlDatabase Open(const TDesC& aDbName)
sl@0
   249
		{
sl@0
   250
		RSqlDatabase db;
sl@0
   251
		TInt err = db.Open(aDbName);
sl@0
   252
		TEST2(err, KErrNone);
sl@0
   253
		return db;
sl@0
   254
		}
sl@0
   255
	};
sl@0
   256
sl@0
   257
//Explicit TDbHelper class specialization for creating/opening a secure RSqlDatabase object
sl@0
   258
template <> class TDbHelper<ESecureDb>
sl@0
   259
	{
sl@0
   260
public:	
sl@0
   261
	static void Create(const TDesC& aDbName)
sl@0
   262
		{
sl@0
   263
		RSqlDatabase::Delete(aDbName);
sl@0
   264
		RSqlSecurityPolicy securityPolicy;
sl@0
   265
		TInt err = securityPolicy.Create(TSecurityPolicy(TSecurityPolicy::EAlwaysPass));
sl@0
   266
		TEST2(err, KErrNone);	
sl@0
   267
		securityPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, TSecurityPolicy(ECapabilityReadUserData, ECapabilityWriteUserData));
sl@0
   268
		securityPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, TSecurityPolicy(ECapabilityWriteUserData));
sl@0
   269
		securityPolicy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, TSecurityPolicy(ECapabilityReadUserData));
sl@0
   270
		RSqlDatabase db;
sl@0
   271
		err = db.Create(aDbName, securityPolicy, &TheSqlConfigString);
sl@0
   272
		TEST2(err, KErrNone);	
sl@0
   273
		securityPolicy.Close();
sl@0
   274
		CreateDbSchema(db);
sl@0
   275
		db.Close();
sl@0
   276
		}
sl@0
   277
	static RSqlDatabase Open(const TDesC& aDbName)
sl@0
   278
		{
sl@0
   279
		RSqlDatabase db;
sl@0
   280
		TInt err = db.Open(aDbName);
sl@0
   281
		TEST2(err, KErrNone);
sl@0
   282
		return db;
sl@0
   283
		}
sl@0
   284
	};
sl@0
   285
sl@0
   286
//Executes SQL script
sl@0
   287
template <class HBUFC, class PTRC, class DESC> void ExecuteSqlScript(RSqlDatabase& aDb, const TDesC& aScriptFileName, const DESC& aCommitStr)
sl@0
   288
	{
sl@0
   289
	HBUFC* fillDbScript = ReadSqlScript<HBUFC>(aScriptFileName);
sl@0
   290
	TUint32 start = User::FastCounter();
sl@0
   291
	PTRC ptr(fillDbScript->Des());
sl@0
   292
	PTRC sql(GetNextTrans<PTRC, DESC>(ptr, aCommitStr));
sl@0
   293
	while(sql.Length() > 0)
sl@0
   294
		{
sl@0
   295
		TInt err = aDb.Exec(sql);
sl@0
   296
		if(err == KErrNoMemory)
sl@0
   297
			{
sl@0
   298
			TheTest.Printf(_L("###ERROR 'Out of memory'! The test cannot be completed!\r\n"));
sl@0
   299
			return;	
sl@0
   300
			}
sl@0
   301
		TEST(err > 0);	
sl@0
   302
		sql.Set(GetNextTrans<PTRC, DESC>(ptr, aCommitStr));
sl@0
   303
		}
sl@0
   304
	TUint32 end = User::FastCounter();
sl@0
   305
	PrintStats(start, end);
sl@0
   306
	delete fillDbScript;
sl@0
   307
	}
sl@0
   308
//Explicit ExecuteSqlScript() template instantiations.
sl@0
   309
template void ExecuteSqlScript<HBufC8, TPtrC8, TDesC8>(RSqlDatabase&, const TDesC&, const TDesC8&);
sl@0
   310
template void ExecuteSqlScript<HBufC16, TPtrC16, TDesC16>(RSqlDatabase&, const TDesC&, const TDesC16&);
sl@0
   311
sl@0
   312
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   313
sl@0
   314
//"INSERT" test function
sl@0
   315
template <class HBUFC, class PTRC, class DESC, TDbType TYPE> void InsertTest(const TDesC& aDbFileName, const DESC& aCommitStr)
sl@0
   316
	{
sl@0
   317
	TheTest.Printf(_L("\"Insert\" test\r\n"));
sl@0
   318
	RSqlDatabase db = TDbHelper<TYPE>::Open(aDbFileName);
sl@0
   319
	ExecuteSqlScript<HBUFC, PTRC, DESC>(db, KFillDbScript, aCommitStr);
sl@0
   320
	db.Close();	
sl@0
   321
	}
sl@0
   322
//Explicit InsertTest() template instantiations.
sl@0
   323
template void InsertTest<HBufC8, TPtrC8, TDesC8, ENonSecureDb>(const TDesC&, const TDesC8&);
sl@0
   324
template void InsertTest<HBufC8, TPtrC8, TDesC8, ESecureDb>(const TDesC&, const TDesC8&);
sl@0
   325
template void InsertTest<HBufC16, TPtrC16, TDesC16, ENonSecureDb>(const TDesC&, const TDesC16&);
sl@0
   326
template void InsertTest<HBufC16, TPtrC16, TDesC16, ESecureDb>(const TDesC&, const TDesC16&);
sl@0
   327
sl@0
   328
//"UPDATE" test function (parametrized update)
sl@0
   329
template <class DESC, TDbType TYPE> void UpdateTest(const TDesC& aDbFileName, const DESC& aUpdateSql)
sl@0
   330
	{
sl@0
   331
	TheTest.Printf(_L("\"Update (parametrized)\" test\r\n"));
sl@0
   332
	RSqlDatabase db = TDbHelper<TYPE>::Open(aDbFileName);
sl@0
   333
	RSqlStatement stmt;
sl@0
   334
	TInt err = stmt.Prepare(db, aUpdateSql);
sl@0
   335
	TEST2(err, KErrNone);
sl@0
   336
	
sl@0
   337
	TInt firstNamePrmIdx = stmt.ParameterIndex(KPrmName1());
sl@0
   338
	TEST(firstNamePrmIdx >= 0);
sl@0
   339
	TInt lastNamePrmIdx = stmt.ParameterIndex(KPrmName2());
sl@0
   340
	TEST(lastNamePrmIdx >= 0);
sl@0
   341
	TInt companyNamePrmIdx = stmt.ParameterIndex(KPrmName3());
sl@0
   342
	TEST(companyNamePrmIdx >= 0);
sl@0
   343
	TInt idIdx = stmt.ParameterIndex(KPrmName4());
sl@0
   344
	TEST(idIdx >= 0);
sl@0
   345
	
sl@0
   346
	TUint32 start = User::FastCounter();
sl@0
   347
	for(TInt id=1;id<=KTestTecordCount;++id)
sl@0
   348
		{
sl@0
   349
		TBuf<20> buf;
sl@0
   350
		buf.Copy(KFirstName);
sl@0
   351
		buf.AppendNum(id);
sl@0
   352
		err = stmt.BindText(firstNamePrmIdx, buf);
sl@0
   353
		TEST2(err, KErrNone);
sl@0
   354
		buf.Copy(KLastName);
sl@0
   355
		buf.AppendNum(id);
sl@0
   356
		err = stmt.BindText(lastNamePrmIdx, buf);
sl@0
   357
		TEST2(err, KErrNone);
sl@0
   358
		buf.Copy(KCompanyName);
sl@0
   359
		buf.AppendNum(id);
sl@0
   360
		err = stmt.BindText(companyNamePrmIdx, buf);
sl@0
   361
		TEST2(err, KErrNone);
sl@0
   362
		err = stmt.BindInt(idIdx, id);
sl@0
   363
		TEST2(err, KErrNone);
sl@0
   364
		err = stmt.Exec();
sl@0
   365
		TEST(err > 0);	
sl@0
   366
		err = stmt.Reset();
sl@0
   367
		TEST2(err, KErrNone);
sl@0
   368
		}
sl@0
   369
	TUint32 end = User::FastCounter();
sl@0
   370
	PrintStats(start, end);
sl@0
   371
	stmt.Close();
sl@0
   372
	db.Close();
sl@0
   373
	}
sl@0
   374
//Explicit UpdateTest() template instantiations.
sl@0
   375
template void UpdateTest<TDesC8, ENonSecureDb>(const TDesC&, const TDesC8&);
sl@0
   376
template void UpdateTest<TDesC8, ESecureDb>(const TDesC&, const TDesC8&);
sl@0
   377
template void UpdateTest<TDesC16, ENonSecureDb>(const TDesC&, const TDesC16&);
sl@0
   378
template void UpdateTest<TDesC16, ESecureDb>(const TDesC&, const TDesC16&);
sl@0
   379
sl@0
   380
//"UPDATE" test function (without parameters) - SQL server
sl@0
   381
template <class BUF, class DESC, TDbType TYPE> void UpdateWPTest(const TDesC& aDbFileName, const DESC& aUpdateSql)
sl@0
   382
	{
sl@0
   383
	TheTest.Printf(_L("\"Update (without parameters)\" test\r\n"));
sl@0
   384
	RSqlDatabase db = TDbHelper<TYPE>::Open(aDbFileName);
sl@0
   385
	TBuf<200> fmtstr;
sl@0
   386
	fmtstr.Copy(aUpdateSql);
sl@0
   387
	
sl@0
   388
	TUint32 start = User::FastCounter();
sl@0
   389
	for(TInt id=1;id<=KTestTecordCount;++id)
sl@0
   390
		{
sl@0
   391
		TBuf<200> buf;
sl@0
   392
		buf.Format(fmtstr, &KFirstName2, id, &KLastName2, id, &KCompanyName2, id, id);
sl@0
   393
		BUF sql;
sl@0
   394
		sql.Copy(buf);
sl@0
   395
		TInt err = db.Exec(sql);
sl@0
   396
		TEST(err > 0);	
sl@0
   397
		}
sl@0
   398
	TUint32 end = User::FastCounter();
sl@0
   399
	PrintStats(start, end);
sl@0
   400
	db.Close();
sl@0
   401
	PrintFileSize(aDbFileName);
sl@0
   402
	}
sl@0
   403
//Explicit UpdateWPTest() template instantiations.
sl@0
   404
template void UpdateWPTest<TBuf8<200>, TDesC8, ENonSecureDb>(const TDesC&, const TDesC8&);
sl@0
   405
template void UpdateWPTest<TBuf8<200>, TDesC8, ESecureDb>(const TDesC&, const TDesC8&);
sl@0
   406
template void UpdateWPTest<TBuf16<200>, TDesC16, ENonSecureDb>(const TDesC&, const TDesC16&);
sl@0
   407
template void UpdateWPTest<TBuf16<200>, TDesC16, ESecureDb>(const TDesC&, const TDesC16&);
sl@0
   408
sl@0
   409
//"SELECT" test function
sl@0
   410
template <class DESC, TDbType TYPE> void SelectTest(const TDesC& aDbFileName, const DESC& aSelectSql)
sl@0
   411
	{
sl@0
   412
	TheTest.Printf(_L("\"Select\" test\r\n"));
sl@0
   413
	RSqlDatabase db = TDbHelper<TYPE>::Open(aDbFileName);
sl@0
   414
	RSqlStatement stmt;
sl@0
   415
	TInt err = stmt.Prepare(db, aSelectSql);
sl@0
   416
	TEST2(err, KErrNone);
sl@0
   417
	TUint32 start = User::FastCounter();
sl@0
   418
	while((err = stmt.Next()) == KSqlAtRow)
sl@0
   419
		{
sl@0
   420
		TBuf<20> buf;
sl@0
   421
		err = stmt.ColumnText(0, buf);			
sl@0
   422
		TEST2(err, KErrNone);
sl@0
   423
		TEST(buf.Length() > 0);
sl@0
   424
		err = stmt.ColumnText(1, buf);			
sl@0
   425
		TEST2(err, KErrNone);
sl@0
   426
		TEST(buf.Length() > 0);
sl@0
   427
		err = stmt.ColumnText(2, buf);			
sl@0
   428
		TEST2(err, KErrNone);
sl@0
   429
		TEST(buf.Length() > 0);
sl@0
   430
		}
sl@0
   431
	TEST2(err, KSqlAtEnd);
sl@0
   432
	TUint32 end = User::FastCounter();
sl@0
   433
	PrintStats(start, end);
sl@0
   434
	stmt.Close();
sl@0
   435
	db.Close();	
sl@0
   436
	}
sl@0
   437
//Explicit SelectTest() template instantiations.
sl@0
   438
template void SelectTest<TDesC8, ENonSecureDb>(const TDesC&, const TDesC8&);
sl@0
   439
template void SelectTest<TDesC8, ESecureDb>(const TDesC&, const TDesC8&);
sl@0
   440
template void SelectTest<TDesC16, ENonSecureDb>(const TDesC&, const TDesC16&);
sl@0
   441
template void SelectTest<TDesC16, ESecureDb>(const TDesC&, const TDesC16&);
sl@0
   442
sl@0
   443
//"DELETE" test function
sl@0
   444
template <class DESC, TDbType TYPE> void DeleteTest(const TDesC& aDbFileName, const DESC& aDeleteSql)
sl@0
   445
	{
sl@0
   446
	TheTest.Printf(_L("\"Delete\" test\r\n"));
sl@0
   447
	RSqlDatabase db = TDbHelper<TYPE>::Open(aDbFileName);
sl@0
   448
	TUint32 start = User::FastCounter();
sl@0
   449
	TInt err = db.Exec(aDeleteSql);
sl@0
   450
	TEST(err > 0);	
sl@0
   451
	TUint32 end = User::FastCounter();
sl@0
   452
	PrintStats(start, end);
sl@0
   453
	db.Close();	
sl@0
   454
	}
sl@0
   455
//Explicit SelectTest() template instantiations.
sl@0
   456
template void DeleteTest<TDesC8, ENonSecureDb>(const TDesC&, const TDesC8&);
sl@0
   457
template void DeleteTest<TDesC8, ESecureDb>(const TDesC&, const TDesC8&);
sl@0
   458
template void DeleteTest<TDesC16, ENonSecureDb>(const TDesC&, const TDesC16&);
sl@0
   459
template void DeleteTest<TDesC16, ESecureDb>(const TDesC&, const TDesC16&);
sl@0
   460
	
sl@0
   461
//Performance test function: INSERT, UPDATE, SELECT
sl@0
   462
template <class HBUFC, class PTRC, class DESC, TDbType TYPE> void PerformanceTest(const TDesC& aDbFileName,
sl@0
   463
																				  const DESC& aCommitStr,
sl@0
   464
																				  const DESC& aUpdateSql,
sl@0
   465
																				  const DESC& aSelectSql,
sl@0
   466
																				  const DESC& aDeleteSql)
sl@0
   467
	{
sl@0
   468
	CFileMan* fm = NULL;
sl@0
   469
	TRAPD(err, fm = CFileMan::NewL(TheFs));
sl@0
   470
	TEST2(err, KErrNone);
sl@0
   471
	
sl@0
   472
	TDbHelper<TYPE>::Create(aDbFileName);
sl@0
   473
	InsertTest<HBUFC, PTRC, DESC, TYPE>(aDbFileName, aCommitStr);
sl@0
   474
	PrintFileSize(aDbFileName);
sl@0
   475
	(void)fm->Copy(aDbFileName, TheNonSecureTmpDbName);
sl@0
   476
	
sl@0
   477
	UpdateTest<DESC, TYPE>(aDbFileName, aUpdateSql);
sl@0
   478
	PrintFileSize(aDbFileName);
sl@0
   479
	SelectTest<DESC, TYPE>(aDbFileName, aSelectSql);
sl@0
   480
	
sl@0
   481
	DeleteTest<DESC, TYPE>(aDbFileName, aDeleteSql);
sl@0
   482
	PrintFileSize(aDbFileName);
sl@0
   483
	
sl@0
   484
	(void)fm->Copy(TheNonSecureTmpDbName, aDbFileName);
sl@0
   485
	(void)fm->Delete(TheNonSecureTmpDbName);
sl@0
   486
	delete fm;
sl@0
   487
	}
sl@0
   488
//Explicit PerformanceTest() template instantiations.
sl@0
   489
template void PerformanceTest<HBufC8, TPtrC8, TDesC8, ENonSecureDb>(const TDesC&, const TDesC8&, const TDesC8&, const TDesC8&, const TDesC8&);
sl@0
   490
template void PerformanceTest<HBufC8, TPtrC8, TDesC8, ESecureDb>(const TDesC&, const TDesC8&, const TDesC8&, const TDesC8&, const TDesC8&);
sl@0
   491
template void PerformanceTest<HBufC16, TPtrC16, TDesC16, ENonSecureDb>(const TDesC&, const TDesC16&, const TDesC16&, const TDesC16&, const TDesC16&);
sl@0
   492
template void PerformanceTest<HBufC16, TPtrC16, TDesC16, ESecureDb>(const TDesC&, const TDesC16&, const TDesC16&, const TDesC16&, const TDesC16&);
sl@0
   493
sl@0
   494
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   495
///////     SQLITE API used directly  
sl@0
   496
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   497
sl@0
   498
//Template class offering Create() and Open() methods for creating/opening a sqlite3 handle.
sl@0
   499
template <TCmdLineParams::TDbEncoding TYPE> class TDbHelper2
sl@0
   500
	{
sl@0
   501
public:	
sl@0
   502
	static void Create(const TDesC& aDbName);
sl@0
   503
	static sqlite3* Open(const TDesC& aDbName);
sl@0
   504
	};
sl@0
   505
sl@0
   506
//If error - prints error message and panics the application
sl@0
   507
void PanicIfError(sqlite3* aDbHandle, TInt aErr)
sl@0
   508
	{
sl@0
   509
  	if(aErr != SQLITE_OK && aErr != SQLITE_DONE && aErr != SQLITE_ROW)
sl@0
   510
  		{
sl@0
   511
  		if(aDbHandle)
sl@0
   512
  			{
sl@0
   513
	  		TPtrC p((const TUint16*)sqlite3_errmsg16(aDbHandle));
sl@0
   514
	  		TheTest.Printf(_L("Database err %d, msg: %S\n\n"), aErr, &p);
sl@0
   515
			(void)sqlite3_close(aDbHandle);
sl@0
   516
  			}
sl@0
   517
    	TEST(0);
sl@0
   518
  		}
sl@0
   519
	}
sl@0
   520
sl@0
   521
//If error - prints error message and returns zero
sl@0
   522
TInt ReportIfError(sqlite3* aDbHandle, TInt aErr)
sl@0
   523
	{
sl@0
   524
  	if(aErr != SQLITE_OK && aErr != SQLITE_DONE && aErr != SQLITE_ROW)
sl@0
   525
  		{
sl@0
   526
  		if(aDbHandle)
sl@0
   527
  			{
sl@0
   528
	  		TPtrC p((const TUint16*)sqlite3_errmsg16(aDbHandle));
sl@0
   529
	  		TheTest.Printf(_L("Database err %d, msg: %S\n\n"), aErr, &p);
sl@0
   530
  			}
sl@0
   531
  		return ETrue;
sl@0
   532
  		}
sl@0
   533
  	return EFalse;
sl@0
   534
	}
sl@0
   535
sl@0
   536
//Explicit TDbHelper2 class specialization for creating/opening a database with UTF8 default encoding
sl@0
   537
template <> class TDbHelper2<TCmdLineParams::EDbUtf8>
sl@0
   538
	{
sl@0
   539
public:	
sl@0
   540
	static void Create(const TDesC& aDbFileName)
sl@0
   541
		{
sl@0
   542
		(void)TheFs.Delete(aDbFileName);
sl@0
   543
		HBufC8* dbName = HBufC8::New(aDbFileName.Length() + 1);
sl@0
   544
		TEST(dbName != NULL);
sl@0
   545
		dbName->Des().Copy(aDbFileName);
sl@0
   546
		sqlite3* dbHandle = NULL;
sl@0
   547
		TInt err = sqlite3_open((const char*)(dbName->Des().PtrZ()), &dbHandle);
sl@0
   548
		delete dbName;
sl@0
   549
		PanicIfError(dbHandle, err);
sl@0
   550
		HBufC8* createDbScript = ReadSqlScript<HBufC8>(KCreateDbScript());
sl@0
   551
		HBufC8* createDbScript2 = HBufC8::New(createDbScript->Des().Length() + 1);
sl@0
   552
		TEST(createDbScript2 != NULL);
sl@0
   553
		createDbScript2->Des().Copy(createDbScript->Des());
sl@0
   554
		delete createDbScript;
sl@0
   555
		createDbScript2->Des().Append(TChar(0));
sl@0
   556
		err = sqlite3_exec(dbHandle, (const char*)createDbScript2->Des().Ptr(), NULL, 0, NULL);
sl@0
   557
		PanicIfError(dbHandle, err);
sl@0
   558
		delete createDbScript2;
sl@0
   559
		(void)sqlite3_close(dbHandle);
sl@0
   560
		}
sl@0
   561
	static sqlite3* Open(const TDesC& aDbFileName)
sl@0
   562
		{
sl@0
   563
		HBufC8* dbName = HBufC8::New(aDbFileName.Length() + 1);
sl@0
   564
		TEST(dbName != NULL);
sl@0
   565
		dbName->Des().Copy(aDbFileName);
sl@0
   566
		sqlite3* dbHandle = NULL;
sl@0
   567
		TInt err = sqlite3_open((const char*)(dbName->Des().PtrZ()), &dbHandle);
sl@0
   568
		delete dbName;
sl@0
   569
		PanicIfError(dbHandle, err);
sl@0
   570
		return dbHandle;
sl@0
   571
		}
sl@0
   572
	};
sl@0
   573
sl@0
   574
//Explicit TDbHelper2 class specialization for creating/opening a database with UTF16 default encoding
sl@0
   575
template <> class TDbHelper2<TCmdLineParams::EDbUtf16>
sl@0
   576
	{
sl@0
   577
public:	
sl@0
   578
	static void Create(const TDesC& aDbFileName)
sl@0
   579
		{
sl@0
   580
		(void)TheFs.Delete(aDbFileName);
sl@0
   581
		HBufC16* dbName = HBufC16::New(aDbFileName.Length() + 1);
sl@0
   582
		TEST(dbName != NULL);
sl@0
   583
		dbName->Des().Copy(aDbFileName);
sl@0
   584
		sqlite3* dbHandle = NULL;
sl@0
   585
		TInt err = sqlite3_open16((const void*)(dbName->Des().PtrZ()), &dbHandle);
sl@0
   586
		delete dbName;
sl@0
   587
		PanicIfError(dbHandle, err);
sl@0
   588
		HBufC8* createDbScript = ReadSqlScript<HBufC8>(KCreateDbScript());
sl@0
   589
		HBufC8* createDbScript2 = HBufC8::New(createDbScript->Des().Length() + 1);
sl@0
   590
		TEST(createDbScript2 != NULL);
sl@0
   591
		createDbScript2->Des().Copy(createDbScript->Des());
sl@0
   592
		delete createDbScript;
sl@0
   593
		createDbScript2->Des().Append(TChar(0));
sl@0
   594
		err = sqlite3_exec(dbHandle, (const char*)createDbScript2->Des().Ptr(), NULL, 0, NULL);
sl@0
   595
		PanicIfError(dbHandle, err);
sl@0
   596
		delete createDbScript2;
sl@0
   597
		(void)sqlite3_close(dbHandle);
sl@0
   598
		}
sl@0
   599
	static sqlite3* Open(const TDesC& aDbFileName)
sl@0
   600
		{
sl@0
   601
		HBufC16* dbName = HBufC16::New(aDbFileName.Length() + 1);
sl@0
   602
		TEST(dbName != NULL);
sl@0
   603
		dbName->Des().Copy(aDbFileName);
sl@0
   604
		sqlite3* dbHandle = NULL;
sl@0
   605
		TInt err = sqlite3_open16((const void*)(dbName->Des().PtrZ()), &dbHandle);
sl@0
   606
		delete dbName;
sl@0
   607
		PanicIfError(dbHandle, err);
sl@0
   608
		return dbHandle;
sl@0
   609
		}
sl@0
   610
	};
sl@0
   611
sl@0
   612
template <class HBUFC, class PTRC, class DESC> void InsertTest2(sqlite3* aDbHandle, const TDesC& aScriptFileName, const DESC& aCommitStr);
sl@0
   613
sl@0
   614
//Explicit InsertTest2() template specialization for UTF8 encoded SQL strings
sl@0
   615
template <> void InsertTest2<HBufC8, TPtrC8, TDesC8>(sqlite3* aDbHandle, const TDesC& aScriptFileName, const TDesC8& aCommitStr)
sl@0
   616
	{
sl@0
   617
	TheTest.Printf(_L("\"Insert\" test\r\n"));
sl@0
   618
	HBufC8* fillDbScript = ReadSqlScript<HBufC8>(aScriptFileName);
sl@0
   619
	TUint32 start = User::FastCounter();
sl@0
   620
	TPtrC8 ptr(fillDbScript->Des());
sl@0
   621
	TPtrC8 sql(GetNextTrans<TPtrC8, TDesC8>(ptr, aCommitStr));
sl@0
   622
	while(sql.Length() > 0)
sl@0
   623
		{
sl@0
   624
		TUint8* p = (TUint8*)sql.Ptr();
sl@0
   625
		p[sql.Length() - 1] = 0;
sl@0
   626
		TInt err = sqlite3_exec(aDbHandle, (const char*)sql.Ptr(), NULL, 0, NULL);
sl@0
   627
		PanicIfError(aDbHandle, err);
sl@0
   628
		sql.Set(GetNextTrans<TPtrC8, TDesC8>(ptr, aCommitStr));
sl@0
   629
		}
sl@0
   630
	TUint32 end = User::FastCounter();
sl@0
   631
	PrintStats(start, end);
sl@0
   632
	delete fillDbScript;
sl@0
   633
	}
sl@0
   634
sl@0
   635
//Explicit InsertTest2() template specialization for UTF16 encoded SQL strings
sl@0
   636
template <> void InsertTest2<HBufC16, TPtrC16, TDesC16>(sqlite3* aDbHandle, const TDesC& aScriptFileName, const TDesC16& aCommitStr)
sl@0
   637
	{
sl@0
   638
	TheTest.Printf(_L("\"Insert\" test\r\n"));
sl@0
   639
	HBufC16* fillDbScript = ReadSqlScript<HBufC16>(aScriptFileName);
sl@0
   640
	TUint32 start = User::FastCounter();
sl@0
   641
	TPtrC16 ptr(fillDbScript->Des());
sl@0
   642
	TPtrC16 sql(GetNextTrans<TPtrC16, TDesC16>(ptr, aCommitStr));
sl@0
   643
	while(sql != KNullDesC16)
sl@0
   644
		{
sl@0
   645
		TPtr16 p((TUint16*)sql.Ptr(), sql.Length(), sql.Length());
sl@0
   646
		TPtrC16 current(KNullDesC16);
sl@0
   647
		while(p.Length() > 1) //"> 1" because it is a zero terminated string
sl@0
   648
			{
sl@0
   649
			current.Set(GetFirstSqlStmt(p));
sl@0
   650
			sqlite3_stmt* stmtHandle = NULL;
sl@0
   651
			const void* stmtTail = NULL;
sl@0
   652
			TInt err = sqlite3_prepare16_v2(aDbHandle, current.Ptr(), -1, &stmtHandle, &stmtTail);
sl@0
   653
			if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
sl@0
   654
				{
sl@0
   655
				if(err == SQLITE_OK)
sl@0
   656
					{
sl@0
   657
					while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
   658
						{
sl@0
   659
						}
sl@0
   660
					}
sl@0
   661
				TInt err2 = sqlite3_finalize(stmtHandle);
sl@0
   662
				TEST2(err2, SQLITE_OK);
sl@0
   663
				}
sl@0
   664
			if(ReportIfError(aDbHandle, err))
sl@0
   665
				{
sl@0
   666
				delete fillDbScript;
sl@0
   667
				return;
sl@0
   668
				}
sl@0
   669
  			}//while(p.Length() > 1)
sl@0
   670
		sql.Set(GetNextTrans<TPtrC16, TDesC16>(ptr, aCommitStr));
sl@0
   671
		}//while(sql != KNullDesC16)
sl@0
   672
	TUint32 end = User::FastCounter();
sl@0
   673
	PrintStats(start, end);
sl@0
   674
	delete fillDbScript;
sl@0
   675
	}
sl@0
   676
sl@0
   677
template <class HBUFC, class DESC> void UpdateTest2(sqlite3* aDbHandle, const DESC& aUpdateSql);
sl@0
   678
sl@0
   679
//Explicit UpdateTest2() template specialization for UTF8 encoded SQL strings
sl@0
   680
template <> void UpdateTest2<HBufC8, TDesC8>(sqlite3* aDbHandle, const TDesC8& aUpdateSql)
sl@0
   681
	{
sl@0
   682
	TheTest.Printf(_L("\"Update\" test\r\n"));
sl@0
   683
	HBufC8* sql = HBufC8::New(aUpdateSql.Length() + 1);
sl@0
   684
	TEST(sql != NULL);
sl@0
   685
	sql->Des().Copy(aUpdateSql);
sl@0
   686
	sql->Des().Append(TChar(0));
sl@0
   687
sl@0
   688
	sqlite3_stmt* stmtHandle = NULL;
sl@0
   689
	const char* stmtTail = NULL;
sl@0
   690
	TInt err = sqlite3_prepare_v2(aDbHandle, (const char*)sql->Des().Ptr(), -1, &stmtHandle, &stmtTail);
sl@0
   691
	delete sql;
sl@0
   692
	PanicIfError(aDbHandle, err);
sl@0
   693
	TInt firstNamePrmIdx = sqlite3_bind_parameter_index(stmtHandle, KRawPrmName1);
sl@0
   694
	TEST(firstNamePrmIdx >= 0);
sl@0
   695
	TInt lastNamePrmIdx = sqlite3_bind_parameter_index(stmtHandle, KRawPrmName2);
sl@0
   696
	TEST(lastNamePrmIdx >= 0);
sl@0
   697
	TInt companyNamePrmIdx = sqlite3_bind_parameter_index(stmtHandle, KRawPrmName3);
sl@0
   698
	TEST(companyNamePrmIdx >= 0);
sl@0
   699
	TInt idIdx = sqlite3_bind_parameter_index(stmtHandle, KRawPrmName4);
sl@0
   700
	TEST(idIdx >= 0);
sl@0
   701
	
sl@0
   702
	TUint32 start = User::FastCounter();
sl@0
   703
	for(TInt id=1;id<=KTestTecordCount;++id)
sl@0
   704
		{
sl@0
   705
		TBuf8<20> buf1;
sl@0
   706
		buf1.Copy(KFirstName);
sl@0
   707
		buf1.AppendNum(id);
sl@0
   708
		buf1.Append(TChar(0));
sl@0
   709
		err = sqlite3_bind_text(stmtHandle, firstNamePrmIdx, (const char*)buf1.Ptr(), -1, SQLITE_STATIC);
sl@0
   710
		TEST2(err, SQLITE_OK);
sl@0
   711
		TBuf8<20> buf2;
sl@0
   712
		buf2.Copy(KLastName);
sl@0
   713
		buf2.AppendNum(id);
sl@0
   714
		buf2.Append(TChar(0));
sl@0
   715
		err = sqlite3_bind_text(stmtHandle, lastNamePrmIdx, (const char*)buf2.Ptr(), -1, SQLITE_STATIC);
sl@0
   716
		TEST2(err, SQLITE_OK);
sl@0
   717
		TBuf8<20> buf3;
sl@0
   718
		buf3.Copy(KCompanyName);
sl@0
   719
		buf3.AppendNum(id);
sl@0
   720
		buf3.Append(TChar(0));
sl@0
   721
		err = sqlite3_bind_text(stmtHandle, companyNamePrmIdx, (const char*)buf3.Ptr(), -1, SQLITE_STATIC);
sl@0
   722
		TEST2(err, SQLITE_OK);
sl@0
   723
		err = sqlite3_bind_int(stmtHandle, idIdx, id);
sl@0
   724
		TEST2(err, SQLITE_OK);
sl@0
   725
		while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
   726
			{
sl@0
   727
			}
sl@0
   728
		PanicIfError(aDbHandle, err);
sl@0
   729
		err = sqlite3_reset(stmtHandle);
sl@0
   730
		PanicIfError(aDbHandle, err);
sl@0
   731
		}
sl@0
   732
	TUint32 end = User::FastCounter();
sl@0
   733
	PrintStats(start, end);
sl@0
   734
	err = sqlite3_finalize(stmtHandle);
sl@0
   735
	TEST2(err, SQLITE_OK);
sl@0
   736
	}
sl@0
   737
sl@0
   738
//Explicit UpdateTest2() template specialization for UTF16 encoded SQL strings
sl@0
   739
template <> void UpdateTest2<HBufC16, TDesC16>(sqlite3* aDbHandle, const TDesC16& aUpdateSql)
sl@0
   740
	{
sl@0
   741
	TheTest.Printf(_L("\"Update\" test\r\n"));
sl@0
   742
	HBufC16* sql = HBufC16::New(aUpdateSql.Length() + 1);
sl@0
   743
	TEST(sql != NULL);
sl@0
   744
	sql->Des().Copy(aUpdateSql);
sl@0
   745
	sql->Des().Append(TChar(0));
sl@0
   746
sl@0
   747
	sqlite3_stmt* stmtHandle = NULL;
sl@0
   748
	const void* stmtTail = NULL;
sl@0
   749
	TInt err = sqlite3_prepare16_v2(aDbHandle, (const void*)sql->Des().Ptr(), -1, &stmtHandle, &stmtTail);
sl@0
   750
	delete sql;
sl@0
   751
	PanicIfError(aDbHandle, err);
sl@0
   752
	TInt firstNamePrmIdx = sqlite3_bind_parameter_index(stmtHandle, KRawPrmName1);
sl@0
   753
	TEST(firstNamePrmIdx >= 0);
sl@0
   754
	TInt lastNamePrmIdx = sqlite3_bind_parameter_index(stmtHandle, KRawPrmName2);
sl@0
   755
	TEST(lastNamePrmIdx >= 0);
sl@0
   756
	TInt companyNamePrmIdx = sqlite3_bind_parameter_index(stmtHandle, KRawPrmName3);
sl@0
   757
	TEST(companyNamePrmIdx >= 0);
sl@0
   758
	TInt idIdx = sqlite3_bind_parameter_index(stmtHandle, KRawPrmName4);
sl@0
   759
	TEST(idIdx >= 0);
sl@0
   760
	
sl@0
   761
	TUint32 start = User::FastCounter();
sl@0
   762
	for(TInt id=1;id<=KTestTecordCount;++id)
sl@0
   763
		{
sl@0
   764
		TBuf16<20> buf1;
sl@0
   765
		buf1.Copy(KFirstName);
sl@0
   766
		buf1.AppendNum(id);
sl@0
   767
		buf1.Append(TChar(0));
sl@0
   768
		err = sqlite3_bind_text16(stmtHandle, firstNamePrmIdx, (const void*)buf1.Ptr(), -1, SQLITE_STATIC);
sl@0
   769
		TEST2(err, SQLITE_OK);
sl@0
   770
		TBuf16<20> buf2;
sl@0
   771
		buf2.Copy(KLastName);
sl@0
   772
		buf2.AppendNum(id);
sl@0
   773
		buf2.Append(TChar(0));
sl@0
   774
		err = sqlite3_bind_text16(stmtHandle, lastNamePrmIdx, (const void*)buf2.Ptr(), -1, SQLITE_STATIC);
sl@0
   775
		TEST2(err, SQLITE_OK);
sl@0
   776
		TBuf16<20> buf3;
sl@0
   777
		buf3.Copy(KCompanyName);
sl@0
   778
		buf3.AppendNum(id);
sl@0
   779
		buf3.Append(TChar(0));
sl@0
   780
		err = sqlite3_bind_text16(stmtHandle, companyNamePrmIdx, (const void*)buf3.Ptr(), -1, SQLITE_STATIC);
sl@0
   781
		TEST2(err, SQLITE_OK);
sl@0
   782
		err = sqlite3_bind_int(stmtHandle, idIdx, id);
sl@0
   783
		TEST2(err, SQLITE_OK);
sl@0
   784
		while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
   785
			{
sl@0
   786
			}
sl@0
   787
		PanicIfError(aDbHandle, err);
sl@0
   788
		err = sqlite3_reset(stmtHandle);
sl@0
   789
		PanicIfError(aDbHandle, err);
sl@0
   790
		}
sl@0
   791
	TUint32 end = User::FastCounter();
sl@0
   792
	PrintStats(start, end);
sl@0
   793
	err = sqlite3_finalize(stmtHandle);
sl@0
   794
	TEST2(err, SQLITE_OK);
sl@0
   795
	}
sl@0
   796
sl@0
   797
//"UPDATE" test function (without parameters) - SQLITE
sl@0
   798
template <class DESC> void UpdateWPTest2(const TDesC& aDbName, const DESC& aUpdateSql);
sl@0
   799
sl@0
   800
//Explicit UpdateWPTest2() template specialization for UTF8 encoded SQL strings
sl@0
   801
template <> void UpdateWPTest2<TDesC8>(const TDesC& aDbName, const TDesC8& aUpdateSql)
sl@0
   802
	{
sl@0
   803
	TheTest.Printf(_L("\"Update (without parameters)\" test\r\n"));
sl@0
   804
	TBuf<200> fmtstr;
sl@0
   805
	fmtstr.Copy(aUpdateSql);
sl@0
   806
sl@0
   807
	sqlite3SymbianLibInit();
sl@0
   808
	sqlite3* dbHandle = TDbHelper2<TCmdLineParams::EDbUtf8>::Open(aDbName);
sl@0
   809
	
sl@0
   810
	TUint32 start = User::FastCounter();
sl@0
   811
	for(TInt id=1;id<=KTestTecordCount;++id)
sl@0
   812
		{
sl@0
   813
		TBuf<200> buf;
sl@0
   814
		buf.Format(fmtstr, &KFirstName2, id, &KLastName2, id, &KCompanyName2, id, id);
sl@0
   815
		TBuf8<200> sql;
sl@0
   816
		sql.Copy(buf);
sl@0
   817
		sql.Append(0);
sl@0
   818
		TInt err = sqlite3_exec(dbHandle, (const char*)sql.Ptr(), 0, 0, 0);
sl@0
   819
		TEST2(err, SQLITE_OK);
sl@0
   820
		}
sl@0
   821
	TUint32 end = User::FastCounter();
sl@0
   822
	PrintStats(start, end);
sl@0
   823
	
sl@0
   824
	TInt err2 = sqlite3_close(dbHandle);
sl@0
   825
	TEST2(err2, SQLITE_OK);
sl@0
   826
	sqlite3SymbianLibFinalize();
sl@0
   827
	CloseSTDLIB();
sl@0
   828
	}
sl@0
   829
sl@0
   830
//Explicit UpdateWPTest2() template specialization for UTF16 encoded SQL strings
sl@0
   831
template <> void UpdateWPTest2<TDesC16>(const TDesC& aDbName, const TDesC16& aUpdateSql)
sl@0
   832
	{
sl@0
   833
	TheTest.Printf(_L("\"Update (without parameters)\" test\r\n"));
sl@0
   834
sl@0
   835
	sqlite3SymbianLibInit();
sl@0
   836
	sqlite3* dbHandle = TDbHelper2<TCmdLineParams::EDbUtf16>::Open(aDbName);
sl@0
   837
	
sl@0
   838
	TUint32 start = User::FastCounter();
sl@0
   839
	for(TInt id=1;id<=KTestTecordCount;++id)
sl@0
   840
		{
sl@0
   841
		TBuf<200> sql;
sl@0
   842
		sql.Format(aUpdateSql, &KFirstName2, id, &KLastName2, id, &KCompanyName2, id, id);
sl@0
   843
		sql.Append(0);
sl@0
   844
		sqlite3_stmt* stmtHandle = NULL;
sl@0
   845
		const void* stmtTail = NULL;
sl@0
   846
		TInt err = sqlite3_prepare16_v2(dbHandle, sql.Ptr(), -1, &stmtHandle, &stmtTail);
sl@0
   847
		TEST2(err, SQLITE_OK);
sl@0
   848
		if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
sl@0
   849
			{
sl@0
   850
			if(err == SQLITE_OK)
sl@0
   851
				{
sl@0
   852
				while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
   853
					{
sl@0
   854
					}
sl@0
   855
				}
sl@0
   856
			TInt err2 = sqlite3_finalize(stmtHandle);
sl@0
   857
			TEST2(err2, SQLITE_OK);
sl@0
   858
			TEST2(err, SQLITE_DONE);
sl@0
   859
			}
sl@0
   860
		}
sl@0
   861
	TUint32 end = User::FastCounter();
sl@0
   862
	PrintStats(start, end);
sl@0
   863
	
sl@0
   864
	TInt err2 = sqlite3_close(dbHandle);
sl@0
   865
	TEST2(err2, SQLITE_OK);
sl@0
   866
	sqlite3SymbianLibFinalize();
sl@0
   867
	CloseSTDLIB();
sl@0
   868
	}
sl@0
   869
sl@0
   870
template <class HBUFC, class DESC> void SelectTest2(sqlite3* aDbHandle, const DESC& aSelectSql);
sl@0
   871
sl@0
   872
//Explicit SelectTest2() template specialization for UTF8 encoded SQL strings
sl@0
   873
template <> void SelectTest2<HBufC8, TDesC8>(sqlite3* aDbHandle, const TDesC8& aSelectSql)
sl@0
   874
	{
sl@0
   875
	TheTest.Printf(_L("\"Select\" test\r\n"));
sl@0
   876
	HBufC8* sql = HBufC8::New(aSelectSql.Length() + 1);
sl@0
   877
	TEST(sql != NULL);
sl@0
   878
	sql->Des().Copy(aSelectSql);
sl@0
   879
	sql->Des().Append(TChar(0));
sl@0
   880
sl@0
   881
	sqlite3_stmt* stmtHandle = NULL;
sl@0
   882
	const char* stmtTail = NULL;
sl@0
   883
	TInt err = sqlite3_prepare_v2(aDbHandle, (const char*)sql->Des().Ptr(), -1, &stmtHandle, &stmtTail);
sl@0
   884
	delete sql;
sl@0
   885
	PanicIfError(aDbHandle, err);
sl@0
   886
	
sl@0
   887
	TUint32 start = User::FastCounter();
sl@0
   888
	while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
   889
		{
sl@0
   890
		TBuf8<20> buf;
sl@0
   891
		const unsigned char* p = sqlite3_column_text(stmtHandle, 0);
sl@0
   892
		buf.Copy(p, User::StringLength(p));
sl@0
   893
		TEST(buf.Length() > 0);
sl@0
   894
		p = sqlite3_column_text(stmtHandle, 1);
sl@0
   895
		buf.Copy(p, User::StringLength(p));
sl@0
   896
		TEST(buf.Length() > 0);
sl@0
   897
		p = sqlite3_column_text(stmtHandle, 2);
sl@0
   898
		buf.Copy(p, User::StringLength(p));
sl@0
   899
		TEST(buf.Length() > 0);
sl@0
   900
		}
sl@0
   901
	TEST2(err, SQLITE_DONE);
sl@0
   902
	TUint32 end = User::FastCounter();
sl@0
   903
	PrintStats(start, end);
sl@0
   904
	err = sqlite3_finalize(stmtHandle);
sl@0
   905
	TEST2(err, SQLITE_OK);
sl@0
   906
	}
sl@0
   907
sl@0
   908
//Explicit SelectTest2() template specialization for UTF16 encoded SQL strings
sl@0
   909
template <> void SelectTest2<HBufC16, TDesC16>(sqlite3* aDbHandle, const TDesC16& aSelectSql)
sl@0
   910
	{
sl@0
   911
	TheTest.Printf(_L("\"Select\" test\r\n"));
sl@0
   912
	HBufC16* sql = HBufC16::New(aSelectSql.Length() + 1);
sl@0
   913
	TEST(sql != NULL);
sl@0
   914
	sql->Des().Copy(aSelectSql);
sl@0
   915
	sql->Des().Append(TChar(0));
sl@0
   916
sl@0
   917
	sqlite3_stmt* stmtHandle = NULL;
sl@0
   918
	const void* stmtTail = NULL;
sl@0
   919
	TInt err = sqlite3_prepare16_v2(aDbHandle, (const void*)sql->Des().Ptr(), -1, &stmtHandle, &stmtTail);
sl@0
   920
	delete sql;
sl@0
   921
	PanicIfError(aDbHandle, err);
sl@0
   922
	
sl@0
   923
	TUint32 start = User::FastCounter();
sl@0
   924
	while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
   925
		{
sl@0
   926
		TBuf16<20> buf;
sl@0
   927
		const void* p = sqlite3_column_text16(stmtHandle, 0);
sl@0
   928
		buf.Copy((const TUint16*)p, User::StringLength((const TUint16*)p));
sl@0
   929
		TEST(buf.Length() > 0);
sl@0
   930
		p = sqlite3_column_text16(stmtHandle, 1);
sl@0
   931
		buf.Copy((const TUint16*)p, User::StringLength((const TUint16*)p));
sl@0
   932
		TEST(buf.Length() > 0);
sl@0
   933
		p = sqlite3_column_text16(stmtHandle, 2);
sl@0
   934
		buf.Copy((const TUint16*)p, User::StringLength((const TUint16*)p));
sl@0
   935
		TEST(buf.Length() > 0);
sl@0
   936
		}
sl@0
   937
	TEST2(err, SQLITE_DONE);
sl@0
   938
	TUint32 end = User::FastCounter();
sl@0
   939
	PrintStats(start, end);
sl@0
   940
	err = sqlite3_finalize(stmtHandle);
sl@0
   941
	TEST2(err, SQLITE_OK);
sl@0
   942
	}
sl@0
   943
sl@0
   944
template <class HBUFC, class DESC> void DeleteTest2(sqlite3* aDbHandle, const DESC& aDeleteSql);
sl@0
   945
sl@0
   946
//Explicit DeleteTest2() template specialization for UTF8 encoded SQL strings
sl@0
   947
template <> void DeleteTest2<HBufC8, TDesC8>(sqlite3* aDbHandle, const TDesC8& aDeleteSql)
sl@0
   948
	{
sl@0
   949
	TheTest.Printf(_L("\"Delete\" test\r\n"));
sl@0
   950
	HBufC8* sql = HBufC8::New(aDeleteSql.Length() + 1);
sl@0
   951
	TEST(sql != NULL);
sl@0
   952
	sql->Des().Copy(aDeleteSql);
sl@0
   953
	sql->Des().Append(TChar(0));
sl@0
   954
	TUint32 start = User::FastCounter();
sl@0
   955
	TInt err = sqlite3_exec(aDbHandle, (const char*)sql->Des().Ptr(), 0, 0, 0);
sl@0
   956
	TEST2(err, SQLITE_OK);
sl@0
   957
	TUint32 end = User::FastCounter();
sl@0
   958
	PrintStats(start, end);
sl@0
   959
	delete sql;
sl@0
   960
	}
sl@0
   961
sl@0
   962
//Explicit DeleteTest2() template specialization for UTF16 encoded SQL strings
sl@0
   963
template <> void DeleteTest2<HBufC16, TDesC16>(sqlite3* aDbHandle, const TDesC16& aDeleteSql)
sl@0
   964
	{
sl@0
   965
	TheTest.Printf(_L("\"Delete\" test\r\n"));
sl@0
   966
	HBufC16* sql = HBufC16::New(aDeleteSql.Length() + 1);
sl@0
   967
	TEST(sql != NULL);
sl@0
   968
	sql->Des().Copy(aDeleteSql);
sl@0
   969
	sql->Des().Append(TChar(0));
sl@0
   970
	TUint32 start = User::FastCounter();
sl@0
   971
	sqlite3_stmt* stmtHandle = NULL;
sl@0
   972
	const void* stmtTail = NULL;
sl@0
   973
	TInt err = sqlite3_prepare16_v2(aDbHandle, sql->Des().Ptr(), -1, &stmtHandle, &stmtTail);
sl@0
   974
	TEST2(err, SQLITE_OK);
sl@0
   975
	if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
sl@0
   976
		{
sl@0
   977
		if(err == SQLITE_OK)
sl@0
   978
			{
sl@0
   979
			while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
   980
				{
sl@0
   981
				}
sl@0
   982
			}
sl@0
   983
		TInt err2 = sqlite3_finalize(stmtHandle);
sl@0
   984
		TEST2(err2, SQLITE_OK);
sl@0
   985
		TEST2(err, SQLITE_DONE);
sl@0
   986
		}
sl@0
   987
	TUint32 end = User::FastCounter();
sl@0
   988
	PrintStats(start, end);
sl@0
   989
	delete sql;
sl@0
   990
	}
sl@0
   991
sl@0
   992
template <TCmdLineParams::TDbEncoding TYPE, class HBUFC, class PTRC, class DESC> void PerformanceTest2(
sl@0
   993
																					   const TDesC& aDbFileName, 
sl@0
   994
																					   const DESC& aCommitStr,
sl@0
   995
																					   const DESC& aUpdateSql,
sl@0
   996
																					   const DESC& aSelectSql,
sl@0
   997
																					   const DESC& aDeleteSql)
sl@0
   998
	{
sl@0
   999
	CFileMan* fm = NULL;
sl@0
  1000
	TRAPD(err, fm = CFileMan::NewL(TheFs));
sl@0
  1001
	TEST2(err, KErrNone);
sl@0
  1002
	
sl@0
  1003
	sqlite3SymbianLibInit();
sl@0
  1004
	TDbHelper2<TYPE>::Create(aDbFileName);
sl@0
  1005
	
sl@0
  1006
	sqlite3* dbHandle = TDbHelper2<TYPE>::Open(aDbFileName);
sl@0
  1007
	InsertTest2<HBUFC, PTRC, DESC>(dbHandle, KFillDbScript(), aCommitStr);
sl@0
  1008
	(void)sqlite3_close(dbHandle); dbHandle = NULL;
sl@0
  1009
	PrintFileSize(aDbFileName);
sl@0
  1010
sl@0
  1011
	dbHandle = TDbHelper2<TYPE>::Open(aDbFileName);
sl@0
  1012
	UpdateTest2<HBUFC, DESC>(dbHandle, aUpdateSql);
sl@0
  1013
	(void)sqlite3_close(dbHandle); dbHandle = NULL;
sl@0
  1014
sl@0
  1015
	dbHandle = TDbHelper2<TYPE>::Open(aDbFileName);
sl@0
  1016
	SelectTest2<HBUFC, DESC>(dbHandle, aSelectSql);
sl@0
  1017
	(void)sqlite3_close(dbHandle); dbHandle = NULL;
sl@0
  1018
sl@0
  1019
	(void)fm->Copy(aDbFileName, TheNonSecureTmpDbName);
sl@0
  1020
	
sl@0
  1021
	dbHandle = TDbHelper2<TYPE>::Open(aDbFileName);
sl@0
  1022
	DeleteTest2<HBUFC, DESC>(dbHandle, aDeleteSql);
sl@0
  1023
	(void)sqlite3_close(dbHandle); dbHandle = NULL;
sl@0
  1024
	PrintFileSize(aDbFileName);
sl@0
  1025
	
sl@0
  1026
	sqlite3SymbianLibFinalize();
sl@0
  1027
	CloseSTDLIB();
sl@0
  1028
sl@0
  1029
	(void)fm->Copy(TheNonSecureTmpDbName, aDbFileName);
sl@0
  1030
	(void)fm->Delete(TheNonSecureTmpDbName);
sl@0
  1031
	delete fm;
sl@0
  1032
	}
sl@0
  1033
//Explicit PerformanceTest2() template instantiations.
sl@0
  1034
template void PerformanceTest2<TCmdLineParams::EDbUtf8, HBufC8, TPtrC8, TDesC8>(const TDesC&, const TDesC8&, const TDesC8&, const TDesC8&, const TDesC8&);
sl@0
  1035
template void PerformanceTest2<TCmdLineParams::EDbUtf16, HBufC8, TPtrC8, TDesC8>(const TDesC&, const TDesC8&, const TDesC8&, const TDesC8&, const TDesC8&);
sl@0
  1036
template void PerformanceTest2<TCmdLineParams::EDbUtf8, HBufC16, TPtrC16, TDesC16>(const TDesC&, const TDesC16&, const TDesC16&, const TDesC16&, const TDesC16&);
sl@0
  1037
template void PerformanceTest2<TCmdLineParams::EDbUtf16, HBufC16, TPtrC16, TDesC16>(const TDesC&, const TDesC16&, const TDesC16&, const TDesC16&, const TDesC16&);
sl@0
  1038
sl@0
  1039
void ColumnValueAccessTest()
sl@0
  1040
	{
sl@0
  1041
	_LIT(KColName1, "Column1");
sl@0
  1042
	_LIT(KColName2, "Column2");
sl@0
  1043
	_LIT(KColName3, "Column3");
sl@0
  1044
	_LIT(KColName4, "Column4");
sl@0
  1045
	const TInt KColCount = 4;
sl@0
  1046
	TPtrC colNames[KColCount] = {KColName1(), KColName2(), KColName3(), KColName4()};
sl@0
  1047
	TBuf<100> sql;
sl@0
  1048
	
sl@0
  1049
	//Create a test database
sl@0
  1050
	RSqlDatabase db;
sl@0
  1051
	TInt err = db.Create(TheNonSecureDbName2, &TheSqlConfigString);
sl@0
  1052
	TEST2(err, KErrNone);
sl@0
  1053
	_LIT(KCreateSql, "CREATE TABLE A(%S INTEGER, %S INTEGER, %S INTEGER, %S INTEGER)");
sl@0
  1054
	sql.Format(KCreateSql, &colNames[0], &colNames[1], &colNames[2], &colNames[3]);
sl@0
  1055
	err = db.Exec(sql);
sl@0
  1056
	TEST(err >= 0);	
sl@0
  1057
	//Insert a record in the test database
sl@0
  1058
	_LIT(KInsertSql, "INSERT INTO A(%S, %S, %S, %S) VALUES(0, 1, 2, 3)");
sl@0
  1059
	sql.Format(KInsertSql, &colNames[0], &colNames[1], &colNames[2], &colNames[3]);
sl@0
  1060
	err = db.Exec(sql);
sl@0
  1061
	TEST2(err, 1);
sl@0
  1062
	//Retrieve the record
sl@0
  1063
	RSqlStatement stmt;
sl@0
  1064
	err = stmt.Prepare(db, _L8("SELECT * FROM A"));
sl@0
  1065
	TEST2(err, KErrNone);
sl@0
  1066
	err = stmt.Next();
sl@0
  1067
	TEST2(err, KSqlAtRow);
sl@0
  1068
	
sl@0
  1069
	//Init the random numbers generator
sl@0
  1070
	TInt64 seed = (TInt64)&ColumnValueAccessTest;
sl@0
  1071
	const TInt KTestCount = 20000;
sl@0
  1072
	TInt val;
sl@0
  1073
	
sl@0
  1074
	//Test 1: retrieve column values using the column index
sl@0
  1075
	TUint32 start = User::FastCounter();
sl@0
  1076
	for(TInt i1=0;i1<KTestCount;++i1)	
sl@0
  1077
		{
sl@0
  1078
		TInt colIdx = Math::Rand(seed) % KColCount;
sl@0
  1079
		val = stmt.ColumnInt(colIdx);
sl@0
  1080
		TEST(val == colIdx);
sl@0
  1081
		}
sl@0
  1082
	TUint32 end = User::FastCounter();
sl@0
  1083
	PrintStats(start, end);
sl@0
  1084
	
sl@0
  1085
	//Test 2: retrieve column values using the column name
sl@0
  1086
	start = User::FastCounter();
sl@0
  1087
	for(TInt i2=0;i2<KTestCount;++i2)	
sl@0
  1088
		{
sl@0
  1089
		TInt colNameIdx = Math::Rand(seed) % KColCount;
sl@0
  1090
		TInt colIdx = stmt.ColumnIndex(colNames[colNameIdx]);
sl@0
  1091
		val = stmt.ColumnInt(colIdx);
sl@0
  1092
		TEST(val == colIdx);
sl@0
  1093
		}
sl@0
  1094
	end = User::FastCounter();
sl@0
  1095
	PrintStats(start, end);
sl@0
  1096
	
sl@0
  1097
	//Cleanup
sl@0
  1098
	stmt.Close();
sl@0
  1099
	db.Close();
sl@0
  1100
	(void)RSqlDatabase::Delete(TheNonSecureDbName2);
sl@0
  1101
	}
sl@0
  1102
sl@0
  1103
///////////////////////////////////////////////////////////////////////////////////
sl@0
  1104
////////////      "Single record" INSERT/UPDATE/DELETE tests       ////////////////
sl@0
  1105
///////////////////////////////////////////////////////////////////////////////////
sl@0
  1106
sl@0
  1107
_LIT(KZDbName, "z:\\test\\default_avacon.dbSQL");
sl@0
  1108
sl@0
  1109
RSqlDatabase TheDb;
sl@0
  1110
sl@0
  1111
void OpenTestDb()
sl@0
  1112
	{
sl@0
  1113
	(void)RSqlDatabase::Delete(TheSglRecDbFileName);
sl@0
  1114
	TInt err = RSqlDatabase::Copy(KZDbName, TheSglRecDbFileName);
sl@0
  1115
	TEST2(err, KErrNone);
sl@0
  1116
	err = TheDb.Open(TheSglRecDbFileName);
sl@0
  1117
	TEST2(err, KErrNone);
sl@0
  1118
	}
sl@0
  1119
	
sl@0
  1120
void CloseTestDb()
sl@0
  1121
	{
sl@0
  1122
	TheDb.Close();
sl@0
  1123
	TInt err = RSqlDatabase::Delete(TheSglRecDbFileName);
sl@0
  1124
	TEST2(err, KErrNone);
sl@0
  1125
	}
sl@0
  1126
sl@0
  1127
void SingleUpdateTest()
sl@0
  1128
	{
sl@0
  1129
	OpenTestDb();
sl@0
  1130
	
sl@0
  1131
	_LIT(KUpdateSql, "UPDATE Table1 SET i2=41 WHERE i1 = 1");
sl@0
  1132
	
sl@0
  1133
	TUint32 start = User::FastCounter();
sl@0
  1134
	TInt err = TheDb.Exec(KUpdateSql);
sl@0
  1135
	TUint32 end = User::FastCounter();
sl@0
  1136
	TEST2(err, 1);
sl@0
  1137
sl@0
  1138
	PrintStats(start, end);
sl@0
  1139
	
sl@0
  1140
	CloseTestDb();
sl@0
  1141
	}
sl@0
  1142
sl@0
  1143
void SingleInsertTest()
sl@0
  1144
	{
sl@0
  1145
	OpenTestDb();
sl@0
  1146
sl@0
  1147
	_LIT(KInsertSql, "INSERT INTO Table1 (i1, i2, i3, i4, r5, t6, t7, t8, lt9, lb10) values(1001, 1, 2002, 1001, 1001.001001, '', 'attic attic baker', 'attic attic baker attic attic baker attic attic baker attic attic baker ', 'ATTIC ATTIC BAKER ATTIC ATTIC BAKER ATTIC ATTIC BAKER ATTIC ATTIC BAKER ', NULL)");
sl@0
  1148
	
sl@0
  1149
	TUint32 start = User::FastCounter();
sl@0
  1150
	TInt err = TheDb.Exec(KInsertSql);
sl@0
  1151
	TUint32 end = User::FastCounter();
sl@0
  1152
	TEST2(err, 1);
sl@0
  1153
sl@0
  1154
	PrintStats(start, end);
sl@0
  1155
	
sl@0
  1156
	CloseTestDb();
sl@0
  1157
	}
sl@0
  1158
sl@0
  1159
void SingleDeleteTest()
sl@0
  1160
	{
sl@0
  1161
	OpenTestDb();
sl@0
  1162
sl@0
  1163
	_LIT(KDeleteSql, "Delete from Table1 where i1 = 1");
sl@0
  1164
	
sl@0
  1165
	TUint32 start = User::FastCounter();
sl@0
  1166
	TInt err = TheDb.Exec(KDeleteSql);
sl@0
  1167
	TUint32 end = User::FastCounter();
sl@0
  1168
	TEST2(err, 1);
sl@0
  1169
sl@0
  1170
	PrintStats(start, end);
sl@0
  1171
	
sl@0
  1172
	CloseTestDb();
sl@0
  1173
	}
sl@0
  1174
	
sl@0
  1175
void SelectLikeQueryPerfTest()
sl@0
  1176
{
sl@0
  1177
	_LIT8(KServerConfigString1, "encoding =  \"UTF-8\"");
sl@0
  1178
	//create a sql db file
sl@0
  1179
	(void)RSqlDatabase::Delete(TheNonSecureDbName);
sl@0
  1180
	TInt err = TheDb.Create(TheNonSecureDbName, &KServerConfigString1);
sl@0
  1181
	TEST2(err, KErrNone);
sl@0
  1182
	
sl@0
  1183
	//Create a test table and insert some records
sl@0
  1184
	err = TheDb.Exec(_L("CREATE TABLE A(Id INTEGER PRIMARY KEY, Name TEXT)"));
sl@0
  1185
	TEST(err >= 0);
sl@0
  1186
	
sl@0
  1187
	//Add 100 records to the table
sl@0
  1188
	for(TInt k=0;k<100;++k)
sl@0
  1189
		{
sl@0
  1190
		TBuf<100> sql;
sl@0
  1191
		sql.Format(_L("INSERT INTO A VALUES(%d, 'Dvorák')"), k + 1);
sl@0
  1192
		err = TheDb.Exec(sql);
sl@0
  1193
		TEST2(err, 1);
sl@0
  1194
		}
sl@0
  1195
	
sl@0
  1196
	//Update one record  
sl@0
  1197
	err = TheDb.Exec(_L("UPDATE A SET Name='dvorak1' WHERE ID = 50"));
sl@0
  1198
	TEST2(err, 1);
sl@0
  1199
sl@0
  1200
	TUint32 start = User::FastCounter();
sl@0
  1201
	
sl@0
  1202
	//Create a statement object and issue a SELECT SQL statement + LIKE clause
sl@0
  1203
	//Test case 1 = full name search with LIKE
sl@0
  1204
	RSqlStatement stmt;
sl@0
  1205
	err = stmt.Prepare(TheDb, _L("SELECT COUNT(*) FROM A WHERE Name LIKE 'DVORAK1'"));
sl@0
  1206
	TEST2(err, KErrNone);
sl@0
  1207
	err = stmt.Next();
sl@0
  1208
	TEST2(err, KSqlAtRow);
sl@0
  1209
	TInt cnt = stmt.ColumnInt(0);
sl@0
  1210
	TEST2(cnt, 1);
sl@0
  1211
	
sl@0
  1212
	stmt.Close();
sl@0
  1213
	
sl@0
  1214
	TUint32 end = User::FastCounter();
sl@0
  1215
	TEST2(err, 1);
sl@0
  1216
	PrintStats(start, end);
sl@0
  1217
	
sl@0
  1218
	TheDb.Close();
sl@0
  1219
	(void)RSqlDatabase::Delete(TheNonSecureDbName);
sl@0
  1220
}
sl@0
  1221
sl@0
  1222
///////////////////////////////////////////////////////////////////////////////////
sl@0
  1223
///////////////////////////////////////////////////////////////////////////////////
sl@0
  1224
sl@0
  1225
/**
sl@0
  1226
@SYMTestCaseID			SYSLIB-SQL-CT-1648
sl@0
  1227
@SYMTestCaseDesc		SQL database performance tests.
sl@0
  1228
						Three test types used: "insert records", "update records" and "select records".
sl@0
  1229
						The tests are executed on: secure database, non-secure database, client side database.
sl@0
  1230
						Each test executed twice using UTF8 and UTF16 encoded SQL statements.
sl@0
  1231
@SYMTestPriority		High
sl@0
  1232
@SYMTestActions			SQL database performance tests.
sl@0
  1233
@SYMTestExpectedResults Test must not fail
sl@0
  1234
@SYMREQ					REQ5792
sl@0
  1235
                        REQ5793
sl@0
  1236
                        REQ5912
sl@0
  1237
                        REQ10273
sl@0
  1238
*/	
sl@0
  1239
void DoTests()
sl@0
  1240
	{
sl@0
  1241
	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1648 SQL performance tests"));
sl@0
  1242
	
sl@0
  1243
	TheTest.Printf(_L("Single \"update\" test\r\n"));
sl@0
  1244
	SingleUpdateTest();
sl@0
  1245
	
sl@0
  1246
	TheTest.Printf(_L("Single \"insert\" test\r\n"));
sl@0
  1247
	SingleInsertTest();
sl@0
  1248
	
sl@0
  1249
	TheTest.Printf(_L("Single \"delete\" test\r\n"));
sl@0
  1250
	SingleDeleteTest();
sl@0
  1251
	
sl@0
  1252
	TheTestTitle.Format(_L("SERVER, UTF8 SQL strings, non-secure, encoding: \"%S\", page size: %d\r\n"), 
sl@0
  1253
			TheCmdLineParams.iDbEncoding == TCmdLineParams::EDbUtf16 ? &KUtf16 : &KUtf8, TheCmdLineParams.iPageSize);
sl@0
  1254
	TheTest.Printf(TheTestTitle);
sl@0
  1255
	PerformanceTest<HBufC8, TPtrC8, TDesC8, ENonSecureDb>(TheNonSecureDbName, KCommitStr8(), KUpdateSql8(), KSelectSql8(), KDeleteSql8());
sl@0
  1256
sl@0
  1257
	TheTestTitle.Format(_L("SERVER, UTF8 SQL strings, non-secure, update test (without parameters), encoding: \"%S\", page size: %d\r\n"), 
sl@0
  1258
			TheCmdLineParams.iDbEncoding == TCmdLineParams::EDbUtf16 ? &KUtf16 : &KUtf8, TheCmdLineParams.iPageSize);
sl@0
  1259
	TheTest.Printf(TheTestTitle);
sl@0
  1260
	UpdateWPTest<TBuf8<200>, TDesC8, ENonSecureDb>(TheNonSecureDbName, KUpdateSql2_8());
sl@0
  1261
sl@0
  1262
	TheTestTitle.Format(_L("SERVER, UTF8 SQL strings, secure, encoding: \"%S\", page size: %d\r\n"), 
sl@0
  1263
			TheCmdLineParams.iDbEncoding == TCmdLineParams::EDbUtf16 ? &KUtf16 : &KUtf8, TheCmdLineParams.iPageSize);
sl@0
  1264
	TheTest.Printf(TheTestTitle);
sl@0
  1265
	PerformanceTest<HBufC8, TPtrC8, TDesC8, ESecureDb>(TheSecureDbName, KCommitStr8(), KUpdateSql8(), KSelectSql8(), KDeleteSql8());
sl@0
  1266
sl@0
  1267
	TheTest.Printf(_L("SQLITE, UTF8 encoded database, UTF8 SQL strings\r\n"));
sl@0
  1268
	PerformanceTest2<TCmdLineParams::EDbUtf8, HBufC8, TPtrC8, TDesC8>(TheNonSecureDbName, KCommitStr8(), KUpdateSql8(), KSelectSql8(), KDeleteSql8());
sl@0
  1269
sl@0
  1270
	TheTest.Printf(_L("SQLITE, UTF8 encoded database, UTF8 SQL strings, update test (without parameters)\r\n"));
sl@0
  1271
	UpdateWPTest2<TDesC8>(TheNonSecureDbName, KUpdateSql2_8());
sl@0
  1272
sl@0
  1273
	TheTest.Printf(_L("SQLITE, UTF16 encoded database, UTF8 SQL strings\r\n"));
sl@0
  1274
	PerformanceTest2<TCmdLineParams::EDbUtf16, HBufC8, TPtrC8, TDesC8>(TheNonSecureDbName, KCommitStr8(), KUpdateSql8(), KSelectSql8(), KDeleteSql8());
sl@0
  1275
sl@0
  1276
	TheTestTitle.Format(_L("SERVER, UTF16 SQL strings, non-secure, encoding: \"%S\", page size: %d\r\n"), 
sl@0
  1277
			TheCmdLineParams.iDbEncoding == TCmdLineParams::EDbUtf16 ? &KUtf16 : &KUtf8, TheCmdLineParams.iPageSize);
sl@0
  1278
	TheTest.Printf(TheTestTitle);
sl@0
  1279
	PerformanceTest<HBufC16, TPtrC16, TDesC16, ENonSecureDb>(TheNonSecureDbName, KCommitStr16(), KUpdateSql16(), KSelectSql16(), KDeleteSql16());
sl@0
  1280
	
sl@0
  1281
	TheTestTitle.Format(_L("SERVER, UTF16 SQL strings, non-secure, update test (without parameters), encoding: \"%S\", page size: %d\r\n"), 
sl@0
  1282
			TheCmdLineParams.iDbEncoding == TCmdLineParams::EDbUtf16 ? &KUtf16 : &KUtf8, TheCmdLineParams.iPageSize);
sl@0
  1283
	TheTest.Printf(TheTestTitle);
sl@0
  1284
	UpdateWPTest<TBuf16<200>, TDesC16, ENonSecureDb>(TheNonSecureDbName, KUpdateSql2_16());
sl@0
  1285
sl@0
  1286
	TheTestTitle.Format(_L("SERVER, UTF16 SQL strings, secure, encoding: \"%S\", page size: %d\r\n"), 
sl@0
  1287
			TheCmdLineParams.iDbEncoding == TCmdLineParams::EDbUtf16 ? &KUtf16 : &KUtf8, TheCmdLineParams.iPageSize);
sl@0
  1288
	TheTest.Printf(TheTestTitle);
sl@0
  1289
	PerformanceTest<HBufC16, TPtrC16, TDesC16, ESecureDb>(TheSecureDbName, KCommitStr16(), KUpdateSql16(), KSelectSql16(), KDeleteSql16());
sl@0
  1290
sl@0
  1291
	TheTest.Printf(_L("SQLITE, UTF8 encoded database, UTF16 SQL strings\r\n"));
sl@0
  1292
	PerformanceTest2<TCmdLineParams::EDbUtf8, HBufC16, TPtrC16, TDesC16>(TheNonSecureDbName, KCommitStr16(), KUpdateSql16(), KSelectSql16(), KDeleteSql16());
sl@0
  1293
sl@0
  1294
	TheTest.Printf(_L("SQLITE, UTF16 encoded database, UTF16 SQL strings\r\n"));
sl@0
  1295
	PerformanceTest2<TCmdLineParams::EDbUtf16, HBufC16, TPtrC16, TDesC16>(TheNonSecureDbName, KCommitStr16(), KUpdateSql16(), KSelectSql16(), KDeleteSql16());
sl@0
  1296
sl@0
  1297
	TheTest.Printf(_L("SQLITE, UTF16 encoded database, UTF16 SQL strings, update test (without parameters)\r\n"));
sl@0
  1298
	UpdateWPTest2<TDesC16>(TheNonSecureDbName, KUpdateSql2_16());
sl@0
  1299
sl@0
  1300
	TheTest.Printf(_L("Accessing column value by index or by name\r\n"));
sl@0
  1301
	ColumnValueAccessTest();
sl@0
  1302
	
sl@0
  1303
	TheTest.Printf(_L("Retrieving data from UTF8 Database using SELECT LIKE statements\r\n"));
sl@0
  1304
	SelectLikeQueryPerfTest();
sl@0
  1305
sl@0
  1306
	}
sl@0
  1307
sl@0
  1308
TInt E32Main()
sl@0
  1309
	{
sl@0
  1310
	TheTest.Title();
sl@0
  1311
sl@0
  1312
	CTrapCleanup* tc = CTrapCleanup::New();
sl@0
  1313
	TheTest(tc != NULL);
sl@0
  1314
sl@0
  1315
	GetCmdLineParamsAndSqlConfigString(TheTest, _L("t_sqlperformance"), TheCmdLineParams, TheSqlConfigString);
sl@0
  1316
	_LIT(KSecureDbName, "c:[2121212A]t_perfdb.db");
sl@0
  1317
	PrepareDbName(KSecureDbName, TheCmdLineParams.iDriveName, TheSecureDbName);
sl@0
  1318
	_LIT(KNonSecureDbName, "c:\\test\\t_perfdb.db");
sl@0
  1319
	PrepareDbName(KNonSecureDbName, TheCmdLineParams.iDriveName, TheNonSecureDbName);
sl@0
  1320
	_LIT(KNonSecureDbName2, "c:\\test\\t_perfdb2.db");
sl@0
  1321
	PrepareDbName(KNonSecureDbName2, TheCmdLineParams.iDriveName, TheNonSecureDbName2);
sl@0
  1322
	_LIT(KNonSecureTmpDbName, "c:\\test\\tmp.db");
sl@0
  1323
	PrepareDbName(KNonSecureTmpDbName, TheCmdLineParams.iDriveName, TheNonSecureTmpDbName);
sl@0
  1324
	_LIT(KSglRecDbName, "c:\\test\\default_avacon.dbSQL");
sl@0
  1325
	PrepareDbName(KSglRecDbName, TheCmdLineParams.iDriveName, TheSglRecDbFileName);
sl@0
  1326
sl@0
  1327
	TheTest.Printf(_L("==Databases: %S, %S, %S, %S, %S\r\n"), &TheSecureDbName, &TheNonSecureDbName, 
sl@0
  1328
															  &TheNonSecureDbName2, &TheNonSecureTmpDbName, 
sl@0
  1329
															  &TheSglRecDbFileName);
sl@0
  1330
	
sl@0
  1331
	__UHEAP_MARK;
sl@0
  1332
	
sl@0
  1333
	TestEnvInit();
sl@0
  1334
	DoTests();
sl@0
  1335
	TestEnvDestroy();
sl@0
  1336
sl@0
  1337
	__UHEAP_MARKEND;
sl@0
  1338
	
sl@0
  1339
	TheTest.End();
sl@0
  1340
	TheTest.Close();
sl@0
  1341
	
sl@0
  1342
	delete tc;
sl@0
  1343
sl@0
  1344
	User::Heap().Check();
sl@0
  1345
	return KErrNone;
sl@0
  1346
	}