os/persistentdata/persistentstorage/sql/TEST/t_sqlite.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) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <e32std.h>
sl@0
    17
#include <f32file.h>
sl@0
    18
#include <e32test.h>
sl@0
    19
#include <stdio.h>
sl@0
    20
#include <stdlib.h>
sl@0
    21
#include <string.h> 
sl@0
    22
#include <hal.h>
sl@0
    23
#include <utf.h>				//CnvUtfConverter
sl@0
    24
#include "sqlite3.h"
sl@0
    25
#include "SqliteSymbian.h"
sl@0
    26
sl@0
    27
#define UNUSED_VAR(a) a = (a)
sl@0
    28
sl@0
    29
//Forward declaration
sl@0
    30
struct Mem;
sl@0
    31
sl@0
    32
static RTest 			TheTest(_L ("t_sqlite.exe"));
sl@0
    33
static CTrapCleanup*	TheTrapCleanup = NULL;
sl@0
    34
static RFs 				TheFs;
sl@0
    35
sl@0
    36
//File-local buffer used for converted to UTF16 (from UTF8) strings or
sl@0
    37
//for a temporary file name buffer.
sl@0
    38
static TUint16 TheFileName[KMaxFileName + 1];
sl@0
    39
sl@0
    40
_LIT(KTestDir, "c:\\test\\");
sl@0
    41
sl@0
    42
const char KDbName1[] = "c:\\test\\t_sqlite1.db";
sl@0
    43
const char KDbName2[] = "c:\\test\\t_sqlite2.db";
sl@0
    44
_LIT(KContactsFile, "c:\\test\\Contacts.DB");
sl@0
    45
sl@0
    46
sqlite3 *TheDb1 = NULL, *TheDb2 = NULL;
sl@0
    47
sl@0
    48
static TBuf<4096> TheBuf1;
sl@0
    49
static TBuf<4096> TheBuf2;
sl@0
    50
static TBuf8<512> TheBuf3;
sl@0
    51
sl@0
    52
static TInt TheCmpFCallCnt = 0;
sl@0
    53
static TInt TheCmpCCallCnt = 0;
sl@0
    54
sl@0
    55
_LIT(KSimpleContactsSqlFile, "z:\\test\\add_simple_contacts.sql");
sl@0
    56
sl@0
    57
//In order to be able to compile the test, the following variables are defined (used inside the OS porting layer, when _SQLPROFILER macro is defined)
sl@0
    58
#ifdef _SQLPROFILER
sl@0
    59
TInt TheSqlSrvProfilerFileRead = 0;
sl@0
    60
TInt TheSqlSrvProfilerFileWrite = 0;
sl@0
    61
TInt TheSqlSrvProfilerFileSync = 0;
sl@0
    62
TInt TheSqlSrvProfilerFileSetSize = 0;
sl@0
    63
#endif
sl@0
    64
sl@0
    65
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    66
sl@0
    67
//Creates TPtrC object which points to the unicode presentation of aData.
sl@0
    68
//aData argument is expected to point to UTF8 encoded, zero terminated string.
sl@0
    69
//The function returns a TPtrC, non-zero terminated object pointing to the unicode presentation of aData.
sl@0
    70
//If the length of the returned TPtrC object is 0 - it means that unicode conversion of aData failed, 
sl@0
    71
//because the aData is too long or is NULL. 
sl@0
    72
//Max allowed aData length is KMaxFileName (excluding terminating 0 character).
sl@0
    73
static TPtrC ConvertToUtf16(const char *aData)
sl@0
    74
	{
sl@0
    75
	TPtrC ptr(0, 0);
sl@0
    76
	if(aData)
sl@0
    77
		{
sl@0
    78
		TInt len = mbstowcs(reinterpret_cast <wchar_t*> (TheFileName), aData, KMaxFileName + 1);
sl@0
    79
		//Check converted string length. If it is longer than KMaxFileName characters, then aData is too long.
sl@0
    80
		if(len > 0 && len <= KMaxFileName)
sl@0
    81
			{
sl@0
    82
			ptr.Set(TheFileName, len);
sl@0
    83
			}
sl@0
    84
		}
sl@0
    85
	return ptr;
sl@0
    86
	}
sl@0
    87
sl@0
    88
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    89
sl@0
    90
static void DeleteTestFiles()
sl@0
    91
	{
sl@0
    92
   	sqlite3_close(TheDb2);
sl@0
    93
   	sqlite3_close(TheDb1);
sl@0
    94
   	
sl@0
    95
	TheFs.Delete(ConvertToUtf16(KDbName2));
sl@0
    96
	TheFs.Delete(ConvertToUtf16(KDbName1));
sl@0
    97
	TheFs.Delete(KContactsFile);
sl@0
    98
	}
sl@0
    99
sl@0
   100
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   101
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   102
//Test macros and functions
sl@0
   103
static void Check(TInt aValue, TInt aLine)
sl@0
   104
	{
sl@0
   105
	if(!aValue)
sl@0
   106
		{
sl@0
   107
		DeleteTestFiles();
sl@0
   108
		TheTest(EFalse, aLine);
sl@0
   109
		}
sl@0
   110
	}
sl@0
   111
static void Check(TInt aValue, TInt aExpected, TInt aLine)
sl@0
   112
	{
sl@0
   113
	if(aValue != aExpected)
sl@0
   114
		{
sl@0
   115
		DeleteTestFiles();
sl@0
   116
		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
sl@0
   117
		TheTest(EFalse, aLine);
sl@0
   118
		}
sl@0
   119
	}
sl@0
   120
#define TEST(arg) ::Check((arg), __LINE__)
sl@0
   121
#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
sl@0
   122
sl@0
   123
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   124
sl@0
   125
static void CreatePrivateDirs()
sl@0
   126
	{
sl@0
   127
	RFs fs;
sl@0
   128
	TEST2(fs.Connect(), KErrNone);
sl@0
   129
	for(TInt i=0;i<('Z'-'A');++i)
sl@0
   130
		{
sl@0
   131
		fs.CreatePrivatePath(i);
sl@0
   132
		}
sl@0
   133
	fs.Close();
sl@0
   134
	}
sl@0
   135
sl@0
   136
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   137
sl@0
   138
static void PrintConfig(const TDesC& aDbFilePath)
sl@0
   139
	{
sl@0
   140
	RDebug::Print(_L("================= Configuration ================\r\n"));	
sl@0
   141
	RDebug::Print(_L("Cache page size %dK, pages %d, total %dK\r\n"), SQLITE_DEFAULT_PAGE_SIZE/1024, SQLITE_DEFAULT_CACHE_SIZE, SQLITE_DEFAULT_PAGE_SIZE * SQLITE_DEFAULT_CACHE_SIZE/1024);	
sl@0
   142
	RDebug::Print(_L("Temp cache page size %dK, pages %d, total %dK\r\n"), SQLITE_DEFAULT_PAGE_SIZE/1024, SQLITE_DEFAULT_TEMP_CACHE_SIZE, SQLITE_DEFAULT_PAGE_SIZE * SQLITE_DEFAULT_TEMP_CACHE_SIZE/1024);	
sl@0
   143
	_LIT(K1, "On");
sl@0
   144
	_LIT(K2, "Off");
sl@0
   145
	RDebug::Print(_L("Autovacuum: %S\r\n"), SQLITE_DEFAULT_AUTOVACUUM ? &K1 : &K2);	
sl@0
   146
	#ifdef SQLITE_DEBUG
sl@0
   147
		RDebug::Print(_L("Debug: On\r\n"));	
sl@0
   148
	#else
sl@0
   149
		RDebug::Print(_L("Debug: Off\r\n"));	
sl@0
   150
	#endif
sl@0
   151
	RDebug::Print(_L("Db file: %S\r\n"), &aDbFilePath);
sl@0
   152
	RDebug::Print(_L("================================================\r\n"));	
sl@0
   153
	}
sl@0
   154
	
sl@0
   155
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   156
sl@0
   157
static TInt callback(void */*NotUsed*/, TInt argc, char **argv, char **azColName)
sl@0
   158
	{
sl@0
   159
	TInt i;
sl@0
   160
	for(i=0; i<argc; i++)
sl@0
   161
		{
sl@0
   162
		TPtrC8 colName((const TUint8*)azColName[i]);
sl@0
   163
		TPtrC8 colVal((const TUint8*)(argv[i] ? argv[i] : "NULL"));
sl@0
   164
		TheBuf2.Copy(colName);
sl@0
   165
		TheBuf2.Append(_L(" = "));
sl@0
   166
		TheBuf1.Copy(colVal);
sl@0
   167
		TheBuf2.Append(TheBuf1);
sl@0
   168
    	RDebug::Print(_L("%S\r\n"), &TheBuf2);
sl@0
   169
		}
sl@0
   170
  	RDebug::Print(_L("\n"));
sl@0
   171
  	return 0;
sl@0
   172
	}
sl@0
   173
sl@0
   174
static TInt Compare(void*, TInt size1, const void* p1, TInt size2, const void* p2)
sl@0
   175
	{
sl@0
   176
	TPtrC8 ptr1((TUint8*)p1, size1);
sl@0
   177
	TPtrC8 ptr2((TUint8*)p2, size2);
sl@0
   178
	
sl@0
   179
	TInt res = ptr1.CompareF(ptr2);
sl@0
   180
	return res;		
sl@0
   181
	}
sl@0
   182
sl@0
   183
static TInt Compare2(void*, TInt size1, const void* p1, TInt size2, const void* p2)
sl@0
   184
	{
sl@0
   185
	TPtrC8 ptr1((TUint8*)p1, size1);
sl@0
   186
	TPtrC8 ptr2((TUint8*)p2, size2);
sl@0
   187
	
sl@0
   188
	TInt res = -ptr1.Compare(ptr2);
sl@0
   189
	return res;		
sl@0
   190
	}
sl@0
   191
sl@0
   192
sl@0
   193
static void Func(sqlite3_context* ctx, TInt nCnt, sqlite3_value** pp)
sl@0
   194
	{
sl@0
   195
	for(TInt i=0;i<nCnt;++i)
sl@0
   196
		{
sl@0
   197
		TInt v = sqlite3_value_int(*pp++);
sl@0
   198
		v = v;
sl@0
   199
		}
sl@0
   200
	sqlite3_result_int(ctx, 564);		
sl@0
   201
	}
sl@0
   202
sl@0
   203
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   204
sl@0
   205
//Create database, table, view, INSERT, SELECT, DELETE sql statements, register collations, 
sl@0
   206
//user defined functions, CREATE INDEX, CREATE VIEW sql statements, GROUP BY, HAVING, EXCEPT,...
sl@0
   207
static void DoTests1()
sl@0
   208
	{
sl@0
   209
	char *zErrMsg = 0;
sl@0
   210
	TInt rc;
sl@0
   211
sl@0
   212
	rc = sqlite3_open(KDbName1, &TheDb1);
sl@0
   213
  	if(rc)
sl@0
   214
  		{
sl@0
   215
  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
sl@0
   216
  		RDebug::Print(_L("Can't open database, err %d, msg: %S\n"), rc, &p);
sl@0
   217
    	TEST(0);
sl@0
   218
  		}
sl@0
   219
sl@0
   220
	rc = sqlite3_create_collation(TheDb1, "Clt1", SQLITE_UTF8, NULL, &Compare);
sl@0
   221
  	if(rc)
sl@0
   222
  		{
sl@0
   223
  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
sl@0
   224
  		RDebug::Print(_L("Can't create collation, err %d, msg: %S\n"), rc, &p);
sl@0
   225
    	TEST(0);
sl@0
   226
  		}
sl@0
   227
sl@0
   228
	rc = sqlite3_create_collation(TheDb1, "Clt2", SQLITE_UTF8, NULL, &Compare2);
sl@0
   229
  	if(rc)
sl@0
   230
  		{
sl@0
   231
  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
sl@0
   232
  		RDebug::Print(_L("Can't create collation, err %d, msg: %S\n"), rc, &p);
sl@0
   233
    	TEST(0);
sl@0
   234
  		}
sl@0
   235
sl@0
   236
	rc = sqlite3_create_function(TheDb1, "Func", 2, SQLITE_UTF8, NULL, Func, NULL, NULL);
sl@0
   237
  	if(rc)
sl@0
   238
  		{
sl@0
   239
  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
sl@0
   240
  		RDebug::Print(_L("Can't create UDF, err %d, msg: %S\n"), rc, &p);
sl@0
   241
    	TEST(0);
sl@0
   242
  		}
sl@0
   243
sl@0
   244
	rc = sqlite3_exec(TheDb1, "\
sl@0
   245
	             BEGIN TRANSACTION;\
sl@0
   246
	             CREATE TABLE Pn(Id INTEGER PRIMARY KEY AUTOINCREMENT,\
sl@0
   247
	                             Name TEXT NOT NULL,\
sl@0
   248
	                             Surname TEXT NOT NULL COLLATE Clt1);\
sl@0
   249
	             CREATE INDEX PnSurname ON Pn(Surname);\
sl@0
   250
	             CREATE TABLE Addr(Id INTEGER PRIMARY KEY AUTOINCREMENT,\
sl@0
   251
	                               PnId INTEGER,\
sl@0
   252
	                               CityId INTEGER,\
sl@0
   253
	                               Location TEXT DEFAULT 'address');\
sl@0
   254
	             CREATE INDEX AddrPnId ON Addr(PnId);\
sl@0
   255
	             CREATE INDEX AddrCityId ON Addr(CityId);\
sl@0
   256
	             CREATE TABLE City(ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT Default 'None');\
sl@0
   257
	             CREATE TABLE Shop(Id INTEGER, Name TEXT);\
sl@0
   258
	             CREATE TABLE Sales(ShopId INTEGER, M MONEY, D TIMESTAMP, D1 DATE, T1 TIME);\
sl@0
   259
	             CREATE TRIGGER TrgPnDel BEFORE DELETE ON Pn \
sl@0
   260
	             BEGIN \
sl@0
   261
					SELECT Func(2,8);\
sl@0
   262
	                SELECT CASE WHEN ((SELECT COUNT(*) FROM Addr WHERE Addr.PnId = old.Id) > 0) \
sl@0
   263
	                            THEN RAISE (ABORT, 'Foreign key constraint violation!!!') \
sl@0
   264
	                END;\
sl@0
   265
	             END;\
sl@0
   266
	             CREATE VIEW V1 AS SELECT * FROM Pn;\
sl@0
   267
	             CREATE VIEW V2 AS SELECT * FROM Addr;\
sl@0
   268
	             CREATE VIEW V3 AS SELECT * FROM Sales;\
sl@0
   269
	             CREATE VIEW V4 AS SELECT * FROM Shop;\
sl@0
   270
	             COMMIT TRANSACTION;", callback, 0, &zErrMsg);
sl@0
   271
	             
sl@0
   272
	if(rc != SQLITE_OK)
sl@0
   273
		{
sl@0
   274
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   275
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   276
    	sqlite3_free(zErrMsg);
sl@0
   277
    	TEST(0);
sl@0
   278
		}
sl@0
   279
sl@0
   280
	rc = sqlite3_exec(TheDb1, 
sl@0
   281
	"BEGIN TRANSACTION;\
sl@0
   282
	 INSERT INTO Pn(Name, Surname) VALUES('Aaaa1', 'Aaaa2');\
sl@0
   283
	 INSERT INTO Pn(Name, Surname) VALUES('Bbbb1', 'Bbbb2');\
sl@0
   284
	 INSERT INTO Pn(Name, Surname) VALUES('Cccc1', 'Cccc2');\
sl@0
   285
	 INSERT INTO Pn(Name, Surname) VALUES('Dddd1', 'Dddd2');\
sl@0
   286
	 INSERT INTO City(Name) VALUES('London');\
sl@0
   287
	 INSERT INTO City(Name) VALUES('Manchester');\
sl@0
   288
	 INSERT INTO Addr(PnId, CityId, Location) SELECT Pn.Id, City.Id, 'Here' FROM Pn, City WHERE Pn.Surname = 'Cccc2' AND City.Name = 'London';\
sl@0
   289
	 INSERT INTO Addr(PnId, CityId, Location) SELECT Pn.Id, City.Id, 'There' FROM Pn, City WHERE Pn.Surname = 'Bbbb2' AND City.Name = 'Manchester';\
sl@0
   290
	 INSERT INTO Shop(Id, Name) VALUES(1, 'Shop-1');\
sl@0
   291
	 INSERT INTO Shop(Id, Name) VALUES(2, 'Shop-2');\
sl@0
   292
	 INSERT INTO Shop(Id, Name) VALUES(3, 'Shop-3');\
sl@0
   293
	 INSERT INTO Sales(ShopId, M, D, D1, T1) VALUES(1, 123.0, '2005-01-01', CURRENT_DATE, '12:34:17');\
sl@0
   294
	 INSERT INTO Sales(ShopId, M, D, D1, T1) VALUES(2, 100.0, '2005-01-01', '2005-01-27', '12:34:18');\
sl@0
   295
	 INSERT INTO Sales(ShopId, M, D, D1, T1) VALUES(2, 200.0, '2005-01-02', '2005-01-28', '12:34:19');\
sl@0
   296
	 INSERT INTO Sales(ShopId, M, D, D1, T1) VALUES(3, 200.0, '2005-01-03', '2005-01-29', '12:34:23');\
sl@0
   297
	 COMMIT TRANSACTION;", callback, 0, &zErrMsg);
sl@0
   298
	if(rc != SQLITE_OK)
sl@0
   299
		{
sl@0
   300
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   301
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   302
    	sqlite3_free(zErrMsg);
sl@0
   303
    	TEST(0);
sl@0
   304
		}
sl@0
   305
sl@0
   306
	rc = sqlite3_exec(TheDb1, "SELECT COUNT(*) FROM Shop", callback, 0, &zErrMsg);
sl@0
   307
	if(rc != SQLITE_OK)
sl@0
   308
		{
sl@0
   309
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   310
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   311
    	sqlite3_free(zErrMsg);
sl@0
   312
    	TEST(0);
sl@0
   313
		}
sl@0
   314
sl@0
   315
	for(TInt i=0;i<500;++i)
sl@0
   316
		{
sl@0
   317
		_LIT8(KSqlStmt, "INSERT INTO Shop(Id, Name) VALUES(%d, 'Shop-%d')\x0");
sl@0
   318
		TheBuf3.Format(KSqlStmt, i+1, i+1);
sl@0
   319
		rc = sqlite3_exec(TheDb1, (const char*)TheBuf3.Ptr(), callback, 0, &zErrMsg);
sl@0
   320
		if(rc != SQLITE_OK)
sl@0
   321
			{
sl@0
   322
	  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   323
	  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   324
	    	sqlite3_free(zErrMsg);
sl@0
   325
    		TEST(0);
sl@0
   326
			}
sl@0
   327
		}
sl@0
   328
sl@0
   329
	_LIT8(KSqlStmt2, "DELETE FROM Shop WHERE Id > 400\x0");
sl@0
   330
	rc = sqlite3_exec(TheDb1, (const char*)KSqlStmt2().Ptr(), callback, 0, &zErrMsg);
sl@0
   331
	if(rc != SQLITE_OK)
sl@0
   332
		{
sl@0
   333
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   334
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   335
    	sqlite3_free(zErrMsg);
sl@0
   336
   		TEST(0);
sl@0
   337
		}
sl@0
   338
sl@0
   339
	_LIT8(KSqlStmt3, "SELECT COUNT(*) FROM Shop\x0");
sl@0
   340
	rc = sqlite3_exec(TheDb1, (const char*)KSqlStmt3().Ptr(), callback, 0, &zErrMsg);
sl@0
   341
	if(rc != SQLITE_OK)
sl@0
   342
		{
sl@0
   343
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   344
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   345
    	sqlite3_free(zErrMsg);
sl@0
   346
   		TEST(0);
sl@0
   347
		}
sl@0
   348
sl@0
   349
	rc = sqlite3_exec(TheDb1, "SELECT Pn.Surname, Addr.Location, City.Name FROM Pn \
sl@0
   350
	      INNER JOIN Addr ON Pn.Id = Addr.PnId \
sl@0
   351
	      INNER JOIN City ON Addr.CityId = City.Id \
sl@0
   352
	      ORDER BY Surname COLLATE Clt2", callback, 0, &zErrMsg);
sl@0
   353
	if(rc != SQLITE_OK)
sl@0
   354
		{
sl@0
   355
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   356
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   357
    	sqlite3_free(zErrMsg);
sl@0
   358
   		TEST(0);
sl@0
   359
		}
sl@0
   360
	      
sl@0
   361
	rc = sqlite3_exec(TheDb1, "SELECT * FROM Addr", callback, 0, &zErrMsg);
sl@0
   362
	if(rc != SQLITE_OK)
sl@0
   363
		{
sl@0
   364
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   365
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   366
    	sqlite3_free(zErrMsg);
sl@0
   367
   		TEST(0);
sl@0
   368
		}
sl@0
   369
	
sl@0
   370
	rc = sqlite3_exec(TheDb1, "SELECT * FROM Sales WHERE D1 > '2005-08-27'", callback, 0, &zErrMsg);
sl@0
   371
	if(rc != SQLITE_OK)
sl@0
   372
		{
sl@0
   373
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   374
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   375
    	sqlite3_free(zErrMsg);
sl@0
   376
   		TEST(0);
sl@0
   377
		}
sl@0
   378
	
sl@0
   379
	rc = sqlite3_exec(TheDb1, "SELECT V1.* FROM V1 EXCEPT SELECT V1.* FROM V1 WHERE V1.Id = 2", callback, 0, &zErrMsg);
sl@0
   380
	if(rc != SQLITE_OK)
sl@0
   381
		{
sl@0
   382
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   383
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   384
    	sqlite3_free(zErrMsg);
sl@0
   385
   		TEST(0);
sl@0
   386
		}
sl@0
   387
sl@0
   388
	rc = sqlite3_exec(TheDb1, "SELECT V3.D, SUM(V3.M) FROM V3 GROUP BY V3.D HAVING SUM(V3.M) > 210.0", callback, 0, &zErrMsg);
sl@0
   389
	if(rc != SQLITE_OK)
sl@0
   390
		{
sl@0
   391
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   392
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   393
    	sqlite3_free(zErrMsg);
sl@0
   394
   		TEST(0);
sl@0
   395
		}
sl@0
   396
	
sl@0
   397
	rc = sqlite3_exec(TheDb1, "SELECT V4.Name, SUM(V3.M) FROM V4, V3 WHERE V4.Id = V3.ShopId GROUP BY V4.Id", callback, 0, &zErrMsg);
sl@0
   398
	if(rc != SQLITE_OK)
sl@0
   399
		{
sl@0
   400
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   401
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   402
    	sqlite3_free(zErrMsg);
sl@0
   403
   		TEST(0);
sl@0
   404
		}
sl@0
   405
sl@0
   406
	sqlite3_close(TheDb1);
sl@0
   407
	TheFs.Delete(ConvertToUtf16(KDbName1));
sl@0
   408
	}
sl@0
   409
sl@0
   410
//Generic SQL tests.
sl@0
   411
static void DoTests2()
sl@0
   412
	{
sl@0
   413
	TheFs.Delete(ConvertToUtf16(KDbName1));
sl@0
   414
	
sl@0
   415
	char *zErrMsg = 0;
sl@0
   416
	TInt rc;
sl@0
   417
sl@0
   418
	rc = sqlite3_open(KDbName1, &TheDb1);
sl@0
   419
  	if(rc != SQLITE_OK)
sl@0
   420
  		{
sl@0
   421
  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
sl@0
   422
  		RDebug::Print(_L("Can't open database, err %d, msg: %S\n"), rc, &p);
sl@0
   423
    	TEST(0);
sl@0
   424
  		}
sl@0
   425
  		
sl@0
   426
	rc = sqlite3_exec(TheDb1, "PRAGMA auto_vacuum = 1", callback, 0, &zErrMsg);
sl@0
   427
	if(rc != SQLITE_OK)
sl@0
   428
		{
sl@0
   429
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   430
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   431
    	sqlite3_free(zErrMsg);
sl@0
   432
   		TEST(0);
sl@0
   433
		}
sl@0
   434
	
sl@0
   435
	rc = sqlite3_exec(TheDb1, "\
sl@0
   436
	             BEGIN TRANSACTION;\
sl@0
   437
	             CREATE TABLE Shop(Id INTEGER, Name TEXT);\
sl@0
   438
	             CREATE INDEX ShopName ON Shop(Name);\
sl@0
   439
	             COMMIT TRANSACTION;", callback, 0, &zErrMsg);
sl@0
   440
	if(rc != SQLITE_OK)
sl@0
   441
		{
sl@0
   442
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   443
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   444
    	sqlite3_free(zErrMsg);
sl@0
   445
   		TEST(0);
sl@0
   446
		}
sl@0
   447
		
sl@0
   448
	rc = sqlite3_exec(TheDb1, "BEGIN TRANSACTION", callback, 0, &zErrMsg);
sl@0
   449
	if(rc != SQLITE_OK)
sl@0
   450
		{
sl@0
   451
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   452
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   453
    	sqlite3_free(zErrMsg);
sl@0
   454
   		TEST(0);
sl@0
   455
		}
sl@0
   456
	
sl@0
   457
	for(TInt i=0;i<1000;++i)
sl@0
   458
		{
sl@0
   459
		_LIT8(KSqlStmt, "INSERT INTO Shop(Id, Name) VALUES(%d, 'Shop-%d')\x0");
sl@0
   460
		TheBuf3.Format(KSqlStmt, i, i);
sl@0
   461
		rc = sqlite3_exec(TheDb1, (const char*)TheBuf3.Ptr(), callback, 0, &zErrMsg);
sl@0
   462
		if(rc != SQLITE_OK)
sl@0
   463
			{
sl@0
   464
	  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   465
	  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   466
	    	sqlite3_free(zErrMsg);
sl@0
   467
	   		TEST(0);
sl@0
   468
			}
sl@0
   469
		}
sl@0
   470
		
sl@0
   471
	rc = sqlite3_exec(TheDb1, "COMMIT TRANSACTION", callback, 0, &zErrMsg);
sl@0
   472
	if(rc != SQLITE_OK)
sl@0
   473
		{
sl@0
   474
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   475
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   476
    	sqlite3_free(zErrMsg);
sl@0
   477
   		TEST(0);
sl@0
   478
		}
sl@0
   479
sl@0
   480
	rc = sqlite3_exec(TheDb1, "SELECT COUNT(*) FROM Shop", callback, 0, &zErrMsg);
sl@0
   481
	if(rc != SQLITE_OK)
sl@0
   482
		{
sl@0
   483
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   484
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   485
    	sqlite3_free(zErrMsg);
sl@0
   486
   		TEST(0);
sl@0
   487
		}
sl@0
   488
sl@0
   489
	rc = sqlite3_exec(TheDb1, "BEGIN TRANSACTION", callback, 0, &zErrMsg);
sl@0
   490
	if(rc != SQLITE_OK)
sl@0
   491
		{
sl@0
   492
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   493
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   494
    	sqlite3_free(zErrMsg);
sl@0
   495
   		TEST(0);
sl@0
   496
		}
sl@0
   497
	
sl@0
   498
	rc = sqlite3_exec(TheDb1, "DELETE FROM Shop WHERE Id > 100", callback, 0, &zErrMsg);
sl@0
   499
	if(rc != SQLITE_OK)
sl@0
   500
		{
sl@0
   501
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   502
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   503
    	sqlite3_free(zErrMsg);
sl@0
   504
   		TEST(0);
sl@0
   505
		}
sl@0
   506
		
sl@0
   507
	rc = sqlite3_exec(TheDb1, "COMMIT TRANSACTION", callback, 0, &zErrMsg);
sl@0
   508
	if(rc != SQLITE_OK)
sl@0
   509
		{
sl@0
   510
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   511
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   512
    	sqlite3_free(zErrMsg);
sl@0
   513
   		TEST(0);
sl@0
   514
		}
sl@0
   515
sl@0
   516
	rc = sqlite3_exec(TheDb1, "SELECT COUNT(*) FROM Shop", callback, 0, &zErrMsg);
sl@0
   517
	if(rc != SQLITE_OK)
sl@0
   518
		{
sl@0
   519
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
   520
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
   521
    	sqlite3_free(zErrMsg);
sl@0
   522
   		TEST(0);
sl@0
   523
		}
sl@0
   524
sl@0
   525
	sqlite3_close(TheDb1);
sl@0
   526
	TheFs.Delete(ConvertToUtf16(KDbName1));
sl@0
   527
	}
sl@0
   528
sl@0
   529
//Accented column names test
sl@0
   530
static void AccentedColumnNamesTestL()
sl@0
   531
	{
sl@0
   532
	TheFs.Delete(ConvertToUtf16(KDbName1));
sl@0
   533
sl@0
   534
	//Open database
sl@0
   535
	TPtrC tmp = ConvertToUtf16(KDbName1);
sl@0
   536
	TBuf<KMaxFileName + 1> fname;
sl@0
   537
	fname.Copy(tmp);
sl@0
   538
	TInt rc = sqlite3_open16(fname.PtrZ(), &TheDb1);//!!!!16-bit encoding!!!!!
sl@0
   539
  	if(rc != SQLITE_OK)
sl@0
   540
  		{
sl@0
   541
  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
sl@0
   542
  		RDebug::Print(_L("Can't open database, err %d, msg: %S\n"), rc, &p);
sl@0
   543
    	TEST(0);
sl@0
   544
  		}
sl@0
   545
	
sl@0
   546
	//Create table
sl@0
   547
	_LIT(KSqlStrZ, "CREATE TABLE abc(col_\u00C4 integer, col_A text)\0");
sl@0
   548
//	_LIT(KSqlStrZ, "CREATE TABLE abc(col_a integer, col_A text)\0");
sl@0
   549
//	_LIT(KSqlStrZ, "CREATE TABLE abc(col_Ä integer, col_ä text)\0");
sl@0
   550
//	_LIT(KSqlStrZ, "CREATE TABLE abc(col_Ä integer, col_A\x308 text)\0");
sl@0
   551
	sqlite3_stmt* stmtHandle = NULL;
sl@0
   552
	const void* stmtTailZ = NULL;
sl@0
   553
	rc = sqlite3_prepare16_v2(TheDb1, KSqlStrZ().Ptr(), -1, &stmtHandle, &stmtTailZ);
sl@0
   554
	if(rc != SQLITE_OK)
sl@0
   555
		{
sl@0
   556
		const void* errMsgZ = sqlite3_errmsg16(TheDb1);
sl@0
   557
		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
sl@0
   558
		RDebug::Print(_L("'sqlite3_prepare16_v2()' failed, err %d, error msg: \"%S\"\r\n"), rc, &msg);
sl@0
   559
    	TEST(0);
sl@0
   560
		}
sl@0
   561
	rc = sqlite3_step(stmtHandle);
sl@0
   562
	TEST(rc == SQLITE_DONE);
sl@0
   563
sl@0
   564
	sqlite3_finalize(stmtHandle);
sl@0
   565
	stmtHandle = NULL;
sl@0
   566
sl@0
   567
	//Select from the table
sl@0
   568
	_LIT(KSqlStrZ2, "SELECT * FROM abc WHERE :prm_\u00C4 = col_\u00C4 and :prm_\u00E4 = col_A and :prm_A = col_A and :prm_a = col_\u00C4\0");
sl@0
   569
	rc = sqlite3_prepare16_v2(TheDb1, KSqlStrZ2().Ptr(), -1, &stmtHandle, &stmtTailZ);
sl@0
   570
	if(rc != SQLITE_OK)
sl@0
   571
		{
sl@0
   572
		const void* errMsgZ = sqlite3_errmsg16(TheDb1);
sl@0
   573
		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
sl@0
   574
		RDebug::Print(_L("'sqlite3_prepare16_v2()' failed, err %d, error msg: \"%S\"\r\n"), rc, &msg);
sl@0
   575
    	TEST(0);
sl@0
   576
		}
sl@0
   577
sl@0
   578
	//parameter checks
sl@0
   579
	TBuf<16> prmNames[5];
sl@0
   580
 	TInt prmCount = sqlite3_bind_parameter_count(stmtHandle);
sl@0
   581
 	for(TInt i=1;i<=prmCount;++i)
sl@0
   582
 		{
sl@0
   583
 		const char* prmNameZ = sqlite3_bind_parameter_name(stmtHandle, i);
sl@0
   584
		TPtrC8 name8(reinterpret_cast <const TUint8*> (prmNameZ), strlen(prmNameZ));
sl@0
   585
		HBufC* name = CnvUtfConverter::ConvertToUnicodeFromUtf8L(name8);
sl@0
   586
		prmNames[i] = *name;
sl@0
   587
		delete name;
sl@0
   588
 		}
sl@0
   589
 	TInt prmIndex0_1 = sqlite3_bind_parameter_index(stmtHandle, ":prm_Ä");
sl@0
   590
 	prmIndex0_1 = prmIndex0_1;
sl@0
   591
 	TInt prmIndex1_1 = sqlite3_bind_parameter_index(stmtHandle, ":prm_ä");
sl@0
   592
 	prmIndex1_1 = prmIndex1_1;
sl@0
   593
 	TInt prmIndex2_1 = sqlite3_bind_parameter_index(stmtHandle, ":prm_A");
sl@0
   594
 	prmIndex2_1 = prmIndex2_1;
sl@0
   595
 	TInt prmIndex3_1 = sqlite3_bind_parameter_index(stmtHandle, ":prm_a");
sl@0
   596
 	prmIndex3_1 = prmIndex3_1;
sl@0
   597
	
sl@0
   598
	TBuf8<16> name8;
sl@0
   599
	
sl@0
   600
	HBufC8* name = CnvUtfConverter::ConvertFromUnicodeToUtf8L(prmNames[1]);
sl@0
   601
	name8 = *name;
sl@0
   602
	delete name;
sl@0
   603
 	TInt prmIndex0_2 = sqlite3_bind_parameter_index(stmtHandle, (const char*)name8.PtrZ());
sl@0
   604
 	prmIndex0_2 = prmIndex0_2;
sl@0
   605
 	
sl@0
   606
	name = CnvUtfConverter::ConvertFromUnicodeToUtf8L(prmNames[2]);
sl@0
   607
	name8 = *name;
sl@0
   608
	delete name;
sl@0
   609
 	TInt prmIndex1_2 = sqlite3_bind_parameter_index(stmtHandle, (const char*)name8.PtrZ());
sl@0
   610
 	prmIndex1_2 = prmIndex1_2;
sl@0
   611
 	
sl@0
   612
	name = CnvUtfConverter::ConvertFromUnicodeToUtf8L(prmNames[3]);
sl@0
   613
	name8 = *name;
sl@0
   614
	delete name;
sl@0
   615
 	TInt prmIndex2_2 = sqlite3_bind_parameter_index(stmtHandle, (const char*)name8.PtrZ());
sl@0
   616
 	prmIndex2_2 = prmIndex2_2;
sl@0
   617
 	
sl@0
   618
	name = CnvUtfConverter::ConvertFromUnicodeToUtf8L(prmNames[4]);
sl@0
   619
	name8 = *name;
sl@0
   620
	delete name;
sl@0
   621
 	TInt prmIndex3_2 = sqlite3_bind_parameter_index(stmtHandle, (const char*)name8.PtrZ());
sl@0
   622
 	prmIndex3_2 = prmIndex3_2;
sl@0
   623
	
sl@0
   624
	//Column checks
sl@0
   625
	TPtrC colName1(_L("col_\u00C4"));
sl@0
   626
	TPtrC colName2(_L("col_A"));
sl@0
   627
	
sl@0
   628
	const void* p = sqlite3_column_name16(stmtHandle, 0);
sl@0
   629
	TPtrC pp1((const TUint16*)p, wcslen(reinterpret_cast <const wchar_t*> (p)));
sl@0
   630
	TEST(colName1 == pp1);
sl@0
   631
	
sl@0
   632
	p = sqlite3_column_name16(stmtHandle, 1);
sl@0
   633
	TPtrC pp2((const TUint16*)p, wcslen(reinterpret_cast <const wchar_t*> (p)));
sl@0
   634
	TEST(colName2 == pp2);
sl@0
   635
		
sl@0
   636
	sqlite3_finalize(stmtHandle);
sl@0
   637
	
sl@0
   638
	sqlite3_close(TheDb1);
sl@0
   639
	TheFs.Delete(ConvertToUtf16(KDbName1));
sl@0
   640
	}
sl@0
   641
sl@0
   642
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   643
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   644
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   645
//////////////              "Add Contacts" test case                  ////////////////////////////
sl@0
   646
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   647
sl@0
   648
//Read SQL file - 8-bit, zero-terminated string
sl@0
   649
static char* ReadSQL2(const TDesC& aSqlFileName)
sl@0
   650
	{
sl@0
   651
	RFile file;
sl@0
   652
	TEST2(file.Open(TheFs, aSqlFileName, EFileRead), KErrNone);
sl@0
   653
	
sl@0
   654
	TInt size = 0;
sl@0
   655
	TEST2(file.Size(size), KErrNone);
sl@0
   656
	
sl@0
   657
	char* sql = new char [size + 1];
sl@0
   658
	TEST(sql != NULL);
sl@0
   659
	
sl@0
   660
	TPtr8 ptr((TUint8*)sql, size + 1);
sl@0
   661
	TEST2(file.Read(ptr, size), KErrNone);
sl@0
   662
	*(sql + size) = 0;
sl@0
   663
sl@0
   664
	file.Close();
sl@0
   665
	return sql;
sl@0
   666
	}
sl@0
   667
sl@0
   668
//Read SQL file - 16-bit, zero-terminated string
sl@0
   669
static HBufC* ReadSQL16(const TDesC& aSqlFileName)
sl@0
   670
	{
sl@0
   671
	RFile file;
sl@0
   672
	TEST2(file.Open(TheFs, aSqlFileName, EFileRead), KErrNone);
sl@0
   673
	
sl@0
   674
	TInt size = 0;
sl@0
   675
	TEST2(file.Size(size), KErrNone);
sl@0
   676
sl@0
   677
	char* sql = new char [size];
sl@0
   678
	TEST(sql != NULL);
sl@0
   679
	TPtr8 ptr((TUint8*)sql, size);
sl@0
   680
	TEST2(file.Read(ptr, size), KErrNone);
sl@0
   681
	
sl@0
   682
	HBufC* sql16 = HBufC::New(size + 1);
sl@0
   683
	TEST(sql16 != NULL);
sl@0
   684
	TPtr16 ptr16 = sql16->Des();
sl@0
   685
	ptr16.Copy(ptr);
sl@0
   686
	ptr16.Append(TChar(0));
sl@0
   687
sl@0
   688
	delete [] sql;
sl@0
   689
	file.Close();
sl@0
   690
	return sql16;
sl@0
   691
	}
sl@0
   692
sl@0
   693
//Prints file size in bytes
sl@0
   694
static void PrintFileSize(const TDesC& aPath)
sl@0
   695
	{
sl@0
   696
	RFile file;
sl@0
   697
	TEST2(file.Open(TheFs, aPath, EFileRead), KErrNone);
sl@0
   698
	TInt size = 0;
sl@0
   699
	TEST2(file.Size(size), KErrNone);
sl@0
   700
	file.Close();
sl@0
   701
	RDebug::Print(_L("File \"%S\", size: %d\r\n"), &aPath, size);
sl@0
   702
	}
sl@0
   703
sl@0
   704
//Executes 8-bit SQL statement	
sl@0
   705
static void ExecSql(sqlite3* aDbHandle, const char* aSqlStmt, const TDesC& aMsg)
sl@0
   706
	{
sl@0
   707
	TEST(aDbHandle != NULL);
sl@0
   708
	TEST(aSqlStmt != NULL);
sl@0
   709
	
sl@0
   710
	char* errMsg = NULL;
sl@0
   711
	TTime t1;
sl@0
   712
	t1.UniversalTime();
sl@0
   713
	TInt rc = sqlite3_exec(aDbHandle, aSqlStmt, callback, 0, &errMsg);
sl@0
   714
	if(rc != SQLITE_OK)
sl@0
   715
		{
sl@0
   716
		TPtrC8 ptr8((const TUint8*)errMsg, strlen(errMsg));
sl@0
   717
		TheBuf1.Copy(ptr8);
sl@0
   718
		RDebug::Print(_L("'sqlite3_exec()' failed, err %d, error msg: \"%S\"\t\n"), rc, &TheBuf1);
sl@0
   719
		TEST(0);
sl@0
   720
		}
sl@0
   721
	TTime t2;
sl@0
   722
	t2.UniversalTime();
sl@0
   723
	TTimeIntervalMicroSeconds diffTime = t2.MicroSecondsFrom(t1); 
sl@0
   724
	diffTime = diffTime.Int64() / 1000;
sl@0
   725
	RDebug::Print(_L("%S, time: %d ms\r\n"), &aMsg, (TInt)diffTime.Int64());
sl@0
   726
	}
sl@0
   727
sl@0
   728
//This function searches aString argument for ';' occurences.
sl@0
   729
//Every time when it finds a ';' character, the function places a 0 right after the ';' and
sl@0
   730
//tests the just created, zero-terminated substring if it is a comlpete SQL statement.
sl@0
   731
//If it is a SQL statement, the function returns it and modifies aString argument to point right after the found
sl@0
   732
//SQL string. If it is not SQL statement, the function will continue the searching.
sl@0
   733
//If there is no ';' inside aString argument, the function returns the same string as a return result and
sl@0
   734
//modifies aString argument - sets it to TPtr(NULL, 0, 0).
sl@0
   735
//
sl@0
   736
//The function expects aString argument to be zero-terminated.
sl@0
   737
static TPtrC GetFirstSqlStmt(TPtr& aString)
sl@0
   738
	{
sl@0
   739
	const TChar KDelimitier(';');
sl@0
   740
	TPtr str(const_cast <TUint16*> (aString.Ptr()), aString.Length(), aString.Length());
sl@0
   741
	TInt afterDelimitierPos = 0;
sl@0
   742
	TInt pos;
sl@0
   743
	while((pos = str.Locate(KDelimitier) + 1) > 0 && pos < str.Length())
sl@0
   744
		{
sl@0
   745
		//There is a possibility that the string which terminates with the found ';' is SQL statement.
sl@0
   746
		//Zero terminate the string placing a zero right after ';' character and test it using sqlite3_complete16()
sl@0
   747
		//call. If it is not SQL string, restore the original character and continue searching.
sl@0
   748
		afterDelimitierPos += pos;
sl@0
   749
		TChar ch = aString[afterDelimitierPos];
sl@0
   750
		aString[afterDelimitierPos] = 0;
sl@0
   751
		TInt res = sqlite3_complete16(aString.Ptr());
sl@0
   752
		aString[afterDelimitierPos] = ch;
sl@0
   753
		if(res)
sl@0
   754
			{
sl@0
   755
			str.Set(const_cast <TUint16*> (aString.Ptr()), afterDelimitierPos, afterDelimitierPos);	
sl@0
   756
			//Replace the found ';' character with 0.
sl@0
   757
			str[afterDelimitierPos - 1] = 0;
sl@0
   758
			aString.Set(const_cast <TUint16*> (aString.Ptr()) + afterDelimitierPos, aString.Length() - afterDelimitierPos, aString.Length() - afterDelimitierPos);
sl@0
   759
			return str;
sl@0
   760
			}
sl@0
   761
		str.Set(const_cast <TUint16*> (str.Ptr()) + pos, str.Length() - pos, str.Length() - pos);	
sl@0
   762
		}
sl@0
   763
	//aString argument does not contain valid SQL statement or there is no ';' character inside aString.
sl@0
   764
	//Set aString to TPtr(NULL, 0, 0) and return the original string.
sl@0
   765
	aString.Set(NULL, 0, 0);
sl@0
   766
	return str;
sl@0
   767
	}
sl@0
   768
	
sl@0
   769
//Executes 16-bit SQL statement	
sl@0
   770
static void ExecSql16(sqlite3* aDbHandle, TDes& aSqlStmtZ, const TDesC& aMsg)
sl@0
   771
	{
sl@0
   772
	TEST(aDbHandle != NULL);
sl@0
   773
		
sl@0
   774
	TTime t1;
sl@0
   775
	t1.UniversalTime();
sl@0
   776
	
sl@0
   777
	TPtr sqlLeftZ(const_cast <TUint16*> (aSqlStmtZ.Ptr()), aSqlStmtZ.Length(), aSqlStmtZ.Length());
sl@0
   778
	while(sqlLeftZ.Length() > 0)
sl@0
   779
		{
sl@0
   780
		TPtrC sql = GetFirstSqlStmt(sqlLeftZ);
sl@0
   781
		sqlite3_stmt* stmtHandle = NULL;
sl@0
   782
		const void* stmtTailZ = NULL;
sl@0
   783
		TInt err = sqlite3_prepare16_v2(aDbHandle, sql.Ptr(), -1, &stmtHandle, &stmtTailZ);
sl@0
   784
		__ASSERT_ALWAYS(!stmtTailZ || User::StringLength((const TUint16*)stmtTailZ) == 0, User::Invariant());
sl@0
   785
		if(stmtHandle)
sl@0
   786
			{
sl@0
   787
			if(err == SQLITE_OK)
sl@0
   788
				{
sl@0
   789
				while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
   790
					{
sl@0
   791
					}
sl@0
   792
				}
sl@0
   793
			sqlite3_finalize(stmtHandle);
sl@0
   794
			}
sl@0
   795
		if(err != SQLITE_DONE && err != SQLITE_OK)
sl@0
   796
			{
sl@0
   797
			const void* errMsgZ = sqlite3_errmsg16(aDbHandle);
sl@0
   798
			TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
sl@0
   799
			RDebug::Print(_L("'sqlite3_exec16()' failed, err %d, error msg: \"%S\"\t\n"), err, &msg);
sl@0
   800
			TEST(0);
sl@0
   801
			}
sl@0
   802
		}
sl@0
   803
	TTime t2;
sl@0
   804
	t2.UniversalTime();
sl@0
   805
	TTimeIntervalMicroSeconds diffTime = t2.MicroSecondsFrom(t1); 
sl@0
   806
	diffTime = diffTime.Int64() / 1000;
sl@0
   807
	RDebug::Print(_L("%S, time: %d ms\r\n"), &aMsg, (TInt)diffTime.Int64());
sl@0
   808
	}
sl@0
   809
sl@0
   810
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   811
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   812
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   813
//////////////              "Search" test case                        ////////////////////////////
sl@0
   814
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   815
sl@0
   816
struct TPnName
sl@0
   817
	{
sl@0
   818
	TPnName(const TDesC8& aFirstName, const TDesC8& aSurname) :
sl@0
   819
		iFirstName(aFirstName),
sl@0
   820
		iSurname(aSurname)
sl@0
   821
		{
sl@0
   822
		}
sl@0
   823
	const TPtrC8 iFirstName;
sl@0
   824
	const TPtrC8 iSurname;
sl@0
   825
	};
sl@0
   826
sl@0
   827
const TInt KNamesCnt = 100;
sl@0
   828
	
sl@0
   829
const TPnName KNames[KNamesCnt] = 
sl@0
   830
	{
sl@0
   831
		TPnName(_L8("Kauh"), _L8("Mollel")),
sl@0
   832
		TPnName(_L8("Be"), _L8("Balcalertthawnd")),
sl@0
   833
		TPnName(_L8("Joba"), _L8("Hah")),
sl@0
   834
		TPnName(_L8("Mal"), _L8("Sinianna")),
sl@0
   835
		TPnName(_L8("Alip"), _L8("Hanittrinke")),
sl@0
   836
		TPnName(_L8("Ris"), _L8("Aba")),
sl@0
   837
		TPnName(_L8("Nirindrilo"), _L8("Oangah")),
sl@0
   838
		TPnName(_L8("An"), _L8("Mck")),
sl@0
   839
		TPnName(_L8("Ris"), _L8("Jat")),
sl@0
   840
		TPnName(_L8("Ja"), _L8("R")),
sl@0
   841
		TPnName(_L8("Pary"), _L8("Sawngethwnes")),
sl@0
   842
		TPnName(_L8("Main"), _L8("Stonstc")),
sl@0
   843
		TPnName(_L8("Joldan"), _L8("Misonialonss")),
sl@0
   844
		TPnName(_L8("Ja"), _L8("Beetth")),
sl@0
   845
		TPnName(_L8("An"), _L8("Magill")),
sl@0
   846
		TPnName(_L8("Ste"), _L8("Hakegstolbebilance")),
sl@0
   847
		TPnName(_L8("Laelefattal"), _L8("Bume")),
sl@0
   848
		TPnName(_L8("Anortoausl"), _L8("Kenoonssssoninals")),
sl@0
   849
		TPnName(_L8("Sthnill"), _L8("Huere")),
sl@0
   850
		TPnName(_L8("Elatandy"), _L8("Miadhelbi")),
sl@0
   851
		TPnName(_L8("Nevieohageridik"), _L8("Baronirgeriallyemo")),
sl@0
   852
		TPnName(_L8("Dertrry"), _L8("Miches")),
sl@0
   853
		TPnName(_L8("Tan-"), _L8("Sonagutlly")),
sl@0
   854
		TPnName(_L8("Mazianer"), _L8("Wi")),
sl@0
   855
		TPnName(_L8("Kesadrin"), _L8("Swixohar")),
sl@0
   856
		TPnName(_L8("Juhnn"), _L8("Vezuins")),
sl@0
   857
		TPnName(_L8("Geri"), _L8("Okun-Mamar")),
sl@0
   858
		TPnName(_L8("Jol"), _L8("Hadir")),
sl@0
   859
		TPnName(_L8("Lon"), _L8("Fonernginire")),
sl@0
   860
		TPnName(_L8("Brrk"), _L8("El")),
sl@0
   861
		TPnName(_L8("So"), _L8("Thanas")),
sl@0
   862
		TPnName(_L8("Timon"), _L8("Matarol")),
sl@0
   863
		TPnName(_L8("Clicartif"), _L8("Sandhinth")),
sl@0
   864
		TPnName(_L8("Dan"), _L8("Brl")),
sl@0
   865
		TPnName(_L8("An"), _L8("Danss")),
sl@0
   866
		TPnName(_L8("Y"), _L8("Gianstes")),
sl@0
   867
		TPnName(_L8("Gralilas"), _L8("Beny")),
sl@0
   868
		TPnName(_L8("Vamean"), _L8("Matesstel")),
sl@0
   869
		TPnName(_L8("Ch"), _L8("Inrinez")),
sl@0
   870
		TPnName(_L8("Ra"), _L8("Lusieing")),
sl@0
   871
		TPnName(_L8("Gerik"), _L8("Mawoshar")),
sl@0
   872
		TPnName(_L8("Nobrd"), _L8("Kerokilirtsoug")),
sl@0
   873
		TPnName(_L8("Norichnik"), _L8("Balmo")),
sl@0
   874
		TPnName(_L8("Anddra"), _L8("Fit")),
sl@0
   875
		TPnName(_L8("Maily"), _L8("Tanyerohetsphinbr")),
sl@0
   876
		TPnName(_L8("Frsa"), _L8("Huntorrenerkh")),
sl@0
   877
		TPnName(_L8("Gi"), _L8("Spandaveees")),
sl@0
   878
		TPnName(_L8("Jollminenipaninderal"), _L8("Vartzury")),
sl@0
   879
		TPnName(_L8("Ankshr"), _L8("Terawloleral")),
sl@0
   880
		TPnName(_L8("An"), _L8("La")),
sl@0
   881
		TPnName(_L8("Ma"), _L8("Brnd")),
sl@0
   882
		TPnName(_L8("Sonerdalmon"), _L8("Bo")),
sl@0
   883
		TPnName(_L8("Nis"), _L8("Tapeworrt")),
sl@0
   884
		TPnName(_L8("Shand"), _L8("Hacllik")),
sl@0
   885
		TPnName(_L8("San"), _L8("Sh")),
sl@0
   886
		TPnName(_L8("Mico"), _L8("Javiaros")),
sl@0
   887
		TPnName(_L8("Hub"), _L8("Warey")),
sl@0
   888
		TPnName(_L8("Mambew"), _L8("Maw")),
sl@0
   889
		TPnName(_L8("Honik"), _L8("Fantscerstetoringu")),
sl@0
   890
		TPnName(_L8("Da"), _L8("Saneelur")),
sl@0
   891
		TPnName(_L8("Aberecalahayondorttelin"), _L8("Futtesesoxok")),
sl@0
   892
		TPnName(_L8("Dor"), _L8("Lelek")),
sl@0
   893
		TPnName(_L8("Matin"), _L8("Fure")),
sl@0
   894
		TPnName(_L8("Niasietolf"), _L8("Jonones")),
sl@0
   895
		TPnName(_L8("Das"), _L8("Hoeonds")),
sl@0
   896
		TPnName(_L8("Anchn"), _L8("Svss")),
sl@0
   897
		TPnName(_L8("Dor"), _L8("Bolunatrk")),
sl@0
   898
		TPnName(_L8("Casah"), _L8("Brilllundonsssoug")),
sl@0
   899
		TPnName(_L8("Iapew"), _L8("Bagukak")),
sl@0
   900
		TPnName(_L8("Lieni"), _L8("MoncNicel")),
sl@0
   901
		TPnName(_L8("Adewalyary"), _L8("Buradesorobbrerans")),
sl@0
   902
		TPnName(_L8("Tos"), _L8("Gis")),
sl@0
   903
		TPnName(_L8("Vi"), _L8("Berk")),
sl@0
   904
		TPnName(_L8("Jorya"), _L8("upmarone")),
sl@0
   905
		TPnName(_L8("Iatew"), _L8("Hend")),
sl@0
   906
		TPnName(_L8("Liag"), _L8("Brsmall")),
sl@0
   907
		TPnName(_L8("Al"), _L8("Spahay")),
sl@0
   908
		TPnName(_L8("El"), _L8("Sy")),
sl@0
   909
		TPnName(_L8("Pary"), _L8("Trl")),
sl@0
   910
		TPnName(_L8("Br"), _L8("Usouroneis")),
sl@0
   911
		TPnName(_L8("Sirnilly"), _L8("Olay")),
sl@0
   912
		TPnName(_L8("Fell"), _L8("Bouphies")),
sl@0
   913
		TPnName(_L8("Man"), _L8("Haz")),
sl@0
   914
		TPnName(_L8("Dare"), _L8("Was")),
sl@0
   915
		TPnName(_L8("Fahnahopephrtex"), _L8("Gat")),
sl@0
   916
		TPnName(_L8("Har"), _L8("Handfffebinneickiasse")),
sl@0
   917
		TPnName(_L8("Gerlai"), _L8("Boravirg")),
sl@0
   918
		TPnName(_L8("Miss"), _L8("Us")),
sl@0
   919
		TPnName(_L8("Caushatattoatot"), _L8("Wes")),
sl@0
   920
		TPnName(_L8("Eizicay"), _L8("Gunbss")),
sl@0
   921
		TPnName(_L8("Pan"), _L8("Hilesertatickesobss-")),
sl@0
   922
		TPnName(_L8("Anaw"), _L8("Mangar")),
sl@0
   923
		TPnName(_L8("Korba"), _L8("Siansolan")),
sl@0
   924
		TPnName(_L8("Darl"), _L8("Haginijelso")),
sl@0
   925
		TPnName(_L8("Ral"), _L8("Veddddkisocackeluisowowone")),
sl@0
   926
		TPnName(_L8("La"), _L8("Wawethl")),
sl@0
   927
		TPnName(_L8("Y"), _L8("Wisonkend")),
sl@0
   928
		TPnName(_L8("Evimiat"), _L8("JondepssooncClille")),
sl@0
   929
		TPnName(_L8("Rin"), _L8("DulatoliacKark")),
sl@0
   930
		TPnName(_L8("Shegeiew"), _L8("Ass"))
sl@0
   931
	};
sl@0
   932
	
sl@0
   933
/////////////////////////////////////////////////////////////
sl@0
   934
//  16-bit strings
sl@0
   935
/////////////////////////////////////////////////////////////
sl@0
   936
sl@0
   937
static TInt CmpF16(void*, TInt size1, const void* p1, TInt size2, const void* p2)
sl@0
   938
	{
sl@0
   939
	++TheCmpFCallCnt;
sl@0
   940
	
sl@0
   941
	TPtrC16 ptr1((TUint16*)p1, size1/2);
sl@0
   942
	TPtrC16 ptr2((TUint16*)p2, size2/2);
sl@0
   943
	
sl@0
   944
	TInt res = ptr1.CompareF(ptr2);
sl@0
   945
	return res;		
sl@0
   946
	}
sl@0
   947
sl@0
   948
static TInt CmpC16(void*, TInt size1, const void* p1, TInt size2, const void* p2)
sl@0
   949
	{
sl@0
   950
	++TheCmpCCallCnt;
sl@0
   951
	
sl@0
   952
	TPtrC16 ptr1((TUint16*)p1, size1/2);
sl@0
   953
	TPtrC16 ptr2((TUint16*)p2, size2/2);
sl@0
   954
	TInt res = ptr1.CompareC(ptr2);
sl@0
   955
	return res;		
sl@0
   956
	}
sl@0
   957
sl@0
   958
static void SearchDbTest16(const TDesC& aDbFilePath)
sl@0
   959
	{
sl@0
   960
	TheFs.Delete(aDbFilePath);
sl@0
   961
	
sl@0
   962
	TBuf<KMaxFileName + 1> fname;
sl@0
   963
	fname.Copy(aDbFilePath);
sl@0
   964
	//Open database
sl@0
   965
	RDebug::Print(_L("Open database\r\n"));
sl@0
   966
	sqlite3 *dbHandle = NULL;
sl@0
   967
	TInt rc = sqlite3_open16(fname.PtrZ(), &dbHandle);//!!!!16-bit encoding!!!!!
sl@0
   968
	if(rc != SQLITE_OK)
sl@0
   969
		{
sl@0
   970
		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
sl@0
   971
		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
sl@0
   972
		RDebug::Print(_L("'sqlite3_open()' failed, file %S, err %d, error msg: \"%S\"\t\n"), &aDbFilePath, rc, &msg);
sl@0
   973
		TEST(0);
sl@0
   974
		}
sl@0
   975
	TEST(dbHandle != NULL);
sl@0
   976
	//Create "CompareF" collation	
sl@0
   977
	RDebug::Print(_L("Create \"CompareF\" collation\r\n"));
sl@0
   978
	_LIT(KCmpF, "CmpF16\x0");
sl@0
   979
	rc = sqlite3_create_collation16(dbHandle, (const char*)(KCmpF().Ptr()), SQLITE_UTF16 | SQLITE_UTF16_ALIGNED, NULL, &CmpF16);
sl@0
   980
  	if(rc)
sl@0
   981
  		{
sl@0
   982
		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
sl@0
   983
		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
sl@0
   984
		RDebug::Print(_L("Err=%S\r\n"), &msg);
sl@0
   985
		TEST(0);
sl@0
   986
  		}
sl@0
   987
	//Create "CompareC" collation	
sl@0
   988
	RDebug::Print(_L("Create \"CompareC\" collation\r\n"));
sl@0
   989
	_LIT(KCmpC, "CmpC16\x0");
sl@0
   990
	rc = sqlite3_create_collation16(dbHandle, (const char*)(KCmpC().Ptr()), SQLITE_UTF16 | SQLITE_UTF16_ALIGNED, NULL, &CmpC16);
sl@0
   991
  	if(rc)
sl@0
   992
  		{
sl@0
   993
		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
sl@0
   994
		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
sl@0
   995
		RDebug::Print(_L("Err=%S\r\n"), &msg);
sl@0
   996
		TEST(0);
sl@0
   997
  		}
sl@0
   998
	//Create database schema
sl@0
   999
	TheCmpFCallCnt = TheCmpCCallCnt = 0;
sl@0
  1000
	RDebug::Print(_L("Create database schema\r\n"));
sl@0
  1001
	HBufC16* createSqlZ = ReadSQL16(_L("z:\\test\\contacts_schema_to_vendors.sql"));
sl@0
  1002
	TPtr sql = createSqlZ->Des();
sl@0
  1003
	ExecSql16(dbHandle, sql, _L("Create schema"));
sl@0
  1004
	delete createSqlZ;
sl@0
  1005
	RDebug::Print(_L("CmpF() call cnt %d, CmpC() call cnt %d\r\n"), TheCmpFCallCnt, TheCmpCCallCnt);
sl@0
  1006
	//Add 1001 "simple" contacts
sl@0
  1007
	TheCmpFCallCnt = TheCmpCCallCnt = 0;
sl@0
  1008
	RDebug::Print(_L("Add 1001 \"simple\" contacts\r\n"));
sl@0
  1009
	HBufC16* addSqlZ = ReadSQL16(KSimpleContactsSqlFile);
sl@0
  1010
	RDebug::Print(_L("--\r\n"));
sl@0
  1011
	sql.Set(addSqlZ->Des());
sl@0
  1012
	ExecSql16(dbHandle, sql, _L("Add simple contacts"));
sl@0
  1013
	delete addSqlZ;
sl@0
  1014
	RDebug::Print(_L("CmpF() call cnt %d, CmpC() call cnt %d\r\n"), TheCmpFCallCnt, TheCmpCCallCnt);
sl@0
  1015
	//Print the number of records
sl@0
  1016
	RDebug::Print(_L("Print the number of records\r\n"));
sl@0
  1017
	TBuf<40> testSql(_L("SELECT COUNT(*) FROM CONTACTS"));
sl@0
  1018
	testSql.Append(TChar(0));
sl@0
  1019
	ExecSql16(dbHandle, testSql, _L("--"));
sl@0
  1020
	
sl@0
  1021
	//Create index: "First name, Last name"
sl@0
  1022
	TheCmpFCallCnt = TheCmpCCallCnt = 0;
sl@0
  1023
	RDebug::Print(_L("Create index: \"First name, Last name\"\r\n"));
sl@0
  1024
	TBuf<100> createIndexStmt(_L("CREATE INDEX Idx1 ON identitytable(cm_firstname COLLATE CmpC16, cm_lastname COLLATE CmpC16)"));
sl@0
  1025
	createIndexStmt.Append(TChar(0));
sl@0
  1026
	ExecSql16(dbHandle, createIndexStmt, _L("Create index"));
sl@0
  1027
	RDebug::Print(_L("CmpF() call cnt %d, CmpC() call cnt %d\r\n"), TheCmpFCallCnt, TheCmpCCallCnt);
sl@0
  1028
sl@0
  1029
	RDebug::Print(_L("Close database\r\n"));
sl@0
  1030
   	sqlite3_close(dbHandle);
sl@0
  1031
sl@0
  1032
	PrintFileSize(aDbFilePath);
sl@0
  1033
	}
sl@0
  1034
sl@0
  1035
/////////////////////////////////////////////////////////////
sl@0
  1036
//  8-bit strings
sl@0
  1037
/////////////////////////////////////////////////////////////
sl@0
  1038
sl@0
  1039
static TInt CmpF8(void*, TInt size1, const void* p1, TInt size2, const void* p2)
sl@0
  1040
	{
sl@0
  1041
	++TheCmpFCallCnt;
sl@0
  1042
	TPtrC8 ptr1((TUint8*)p1, size1);
sl@0
  1043
	TPtrC8 ptr2((TUint8*)p2, size2);
sl@0
  1044
	
sl@0
  1045
	TInt res = ptr1.CompareF(ptr2);
sl@0
  1046
	return res;		
sl@0
  1047
	}
sl@0
  1048
sl@0
  1049
static TInt CmpC8(void*, TInt size1, const void* p1, TInt size2, const void* p2)
sl@0
  1050
	{
sl@0
  1051
	TPtrC8 ptr1((TUint8*)p1, size1);
sl@0
  1052
	TPtrC8 ptr2((TUint8*)p2, size2);
sl@0
  1053
	TInt res = ptr1.CompareC(ptr2);
sl@0
  1054
	return res;		
sl@0
  1055
	}
sl@0
  1056
sl@0
  1057
static void SearchDbTest8(const TDesC& aDbFilePath, const TDesC& aAddContactsFile, const TDesC& aMsg)
sl@0
  1058
	{
sl@0
  1059
	TheFs.Delete(aDbFilePath);
sl@0
  1060
	
sl@0
  1061
	TBuf8<KMaxFileName + 1> fname;
sl@0
  1062
	fname.Copy(aDbFilePath);
sl@0
  1063
	
sl@0
  1064
	RDebug::Print(_L("%S\r\n"), &aMsg);
sl@0
  1065
	RDebug::Print(_L("Open database\r\n"));
sl@0
  1066
	sqlite3 *dbHandle = NULL;
sl@0
  1067
	TInt rc = sqlite3_open((const char*)fname.PtrZ(), &dbHandle);
sl@0
  1068
	if(rc != SQLITE_OK)
sl@0
  1069
		{
sl@0
  1070
		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
sl@0
  1071
		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
sl@0
  1072
		RDebug::Print(_L("'sqlite3_open()' failed, file %S, err %d, error msg: \"%S\"\r\n"), &aDbFilePath, rc, &msg);
sl@0
  1073
		TEST(0);
sl@0
  1074
		}
sl@0
  1075
	TEST(dbHandle != NULL);
sl@0
  1076
	
sl@0
  1077
	RDebug::Print(_L("Create 'CompareF' collation\r\n"));
sl@0
  1078
	_LIT8(KCmpF, "CmpF8\x0");
sl@0
  1079
	rc = sqlite3_create_collation(dbHandle, (const char*)(KCmpF().Ptr()), SQLITE_UTF8, NULL, &CmpF8);
sl@0
  1080
  	if(rc)
sl@0
  1081
  		{
sl@0
  1082
		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
sl@0
  1083
		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
sl@0
  1084
		RDebug::Print(_L("'sqlite3_create_collation()' failed, file %S, err %d, error msg: \"%S\"\r\n"), &aDbFilePath, rc, &msg);
sl@0
  1085
		TEST(0);
sl@0
  1086
  		}
sl@0
  1087
sl@0
  1088
	RDebug::Print(_L("Create 'CompareC' collation\r\n"));
sl@0
  1089
	_LIT8(KCmpC, "CmpC8\x0");
sl@0
  1090
	rc = sqlite3_create_collation(dbHandle, (const char*)(KCmpC().Ptr()), SQLITE_UTF8, NULL, &CmpC8);
sl@0
  1091
  	if(rc)
sl@0
  1092
  		{
sl@0
  1093
		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
sl@0
  1094
		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
sl@0
  1095
		RDebug::Print(_L("'sqlite3_create_collation()' failed, file %S, err %d, error msg: \"%S\"\r\n"), &aDbFilePath, rc, &msg);
sl@0
  1096
		TEST(0);
sl@0
  1097
  		}
sl@0
  1098
sl@0
  1099
	RDebug::Print(_L("Create database schema\r\n"));
sl@0
  1100
	char* createSqlZ = ReadSQL2(_L("z:\\test\\contacts_schema_to_vendors.sql"));
sl@0
  1101
	ExecSql(dbHandle, createSqlZ, _L("Create schema"));
sl@0
  1102
	delete [] createSqlZ;
sl@0
  1103
sl@0
  1104
	RDebug::Print(_L("Add 1001 contacts\r\n"));
sl@0
  1105
	char* addSqlZ = ReadSQL2(aAddContactsFile);
sl@0
  1106
	ExecSql(dbHandle, addSqlZ, _L("Add contacts"));
sl@0
  1107
	delete [] addSqlZ;
sl@0
  1108
sl@0
  1109
	RDebug::Print(_L("Print the number of records\r\n"));
sl@0
  1110
	const char testSql[] = {"SELECT COUNT(*) FROM CONTACTS"};
sl@0
  1111
	ExecSql(dbHandle, testSql, _L("SELECT COUNT(*)"));
sl@0
  1112
	
sl@0
  1113
	RDebug::Print(_L("Create index (using 'CompareF' collation): 'FirstName, Surname'\r\n"));
sl@0
  1114
	_LIT8(KCreateIndexStmt, "CREATE INDEX Idx1 ON identitytable(cm_firstname COLLATE CmpF8, cm_lastname COLLATE CmpF8)\x0");
sl@0
  1115
	ExecSql(dbHandle, (const char*)(KCreateIndexStmt().Ptr()), _L("Create index"));
sl@0
  1116
	RDebug::Print(_L("CompareF() called %d times\r\n"), TheCmpFCallCnt);
sl@0
  1117
sl@0
  1118
/* BEGIN OF - TEST CASE 1 "Select all contacts which first name begins with 'a' " */	
sl@0
  1119
sl@0
  1120
	RDebug::Print(_L("Prepare 'Select all contacts where the first name begins with 'A'' SQL string\r\n"));
sl@0
  1121
	_LIT8(KSearchStmt, "SELECT cm_firstname, cm_lastname FROM identitytable WHERE cm_firstname LIKE 'A%'\x0");
sl@0
  1122
	sqlite3_stmt* stmtHandle = NULL;
sl@0
  1123
	const char* stmtTailZ = NULL;
sl@0
  1124
	TTime t1;
sl@0
  1125
	t1.UniversalTime();
sl@0
  1126
	rc = sqlite3_prepare_v2(dbHandle, (const char*)(KSearchStmt().Ptr()), -1, &stmtHandle, &stmtTailZ);
sl@0
  1127
	TEST2(rc, SQLITE_OK);
sl@0
  1128
	TEST(stmtHandle != NULL);
sl@0
  1129
	TEST(!stmtTailZ || strlen(stmtTailZ) == 0);
sl@0
  1130
	TTime t2;
sl@0
  1131
	t2.UniversalTime();
sl@0
  1132
	TTimeIntervalMicroSeconds diffTime = t2.MicroSecondsFrom(t1); 
sl@0
  1133
	diffTime = diffTime.Int64() / 1000;
sl@0
  1134
	TInt t = (TInt)diffTime.Int64();
sl@0
  1135
	RDebug::Print(_L("'Prepare SQL statement' time: %d ms\r\n"), t);
sl@0
  1136
sl@0
  1137
	RDebug::Print(_L("Step the prepared SQL statement\r\n"));
sl@0
  1138
	TInt totalCmpFCnt = 0;
sl@0
  1139
	t1.UniversalTime();
sl@0
  1140
	TheCmpFCallCnt = 0;
sl@0
  1141
	TInt recordCnt = 0;
sl@0
  1142
	while((rc = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
  1143
		{
sl@0
  1144
		//const TUint8* firstName = sqlite3_column_text(stmtHandle, 0);
sl@0
  1145
		//TPtrC8 p(firstName, strlen((const char*)firstName));
sl@0
  1146
		//TBuf<100> p1; p1.Copy(p);
sl@0
  1147
		//const TUint8* surname = sqlite3_column_text(stmtHandle, 1);
sl@0
  1148
		//p.Set(surname, strlen((const char*)surname));
sl@0
  1149
		//TBuf<100> p2; p2.Copy(p);
sl@0
  1150
		//RDebug::Print(_L("Found rec: %S, %S\r\n"), &p1, &p2);
sl@0
  1151
		++recordCnt;
sl@0
  1152
		}
sl@0
  1153
	totalCmpFCnt += TheCmpFCallCnt;
sl@0
  1154
	TEST(rc == SQLITE_OK || rc == SQLITE_DONE);
sl@0
  1155
	t2.UniversalTime();
sl@0
  1156
	diffTime = t2.MicroSecondsFrom(t1); 
sl@0
  1157
	diffTime = diffTime.Int64() / 1000;
sl@0
  1158
	t = (TInt)diffTime.Int64();
sl@0
  1159
	RDebug::Print(_L("'Stepping' time: %d ms, found records: %d\r\n"), t, recordCnt);
sl@0
  1160
	RDebug::Print(_L("Total 'search' ('CompareF' used) operations=%d\r\n"), totalCmpFCnt);
sl@0
  1161
sl@0
  1162
	sqlite3_finalize(stmtHandle);
sl@0
  1163
	stmtHandle = NULL;
sl@0
  1164
sl@0
  1165
/* END OF - TEST CASE 1 "Select all contacts which first name begins with 'a' " */	
sl@0
  1166
sl@0
  1167
/*	BEGIN OF - TEST CASE 2 "Do 100 searches in 1001 contacts" */
sl@0
  1168
sl@0
  1169
	RDebug::Print(_L("Prepare 'SELECT FirstName, Surname...' SQL string\r\n"));
sl@0
  1170
	_LIT8(KSearchStmt2, "SELECT cm_firstname, cm_lastname FROM identitytable WHERE cm_firstname = :Prm1 AND cm_lastname = :Prm2\x0");
sl@0
  1171
	stmtHandle = NULL;
sl@0
  1172
	stmtTailZ = NULL;
sl@0
  1173
	t1.UniversalTime();
sl@0
  1174
	rc = sqlite3_prepare_v2(dbHandle, (const char*)(KSearchStmt2().Ptr()), -1, &stmtHandle, &stmtTailZ);
sl@0
  1175
	TEST2(rc, SQLITE_OK);
sl@0
  1176
	TEST(stmtHandle != NULL);
sl@0
  1177
	TEST(!stmtTailZ || strlen(stmtTailZ) == 0);
sl@0
  1178
	t2.UniversalTime();
sl@0
  1179
	diffTime = t2.MicroSecondsFrom(t1); 
sl@0
  1180
	diffTime = diffTime.Int64() / 1000;
sl@0
  1181
	t = (TInt)diffTime.Int64();
sl@0
  1182
	RDebug::Print(_L("'Prepare SQL statement' time: %d ms\r\n"), t);
sl@0
  1183
	
sl@0
  1184
	TInt idxPrm1 = sqlite3_bind_parameter_index(stmtHandle, ":Prm1");
sl@0
  1185
	TEST(idxPrm1 > 0);
sl@0
  1186
	TInt idxPrm2 = sqlite3_bind_parameter_index(stmtHandle, ":Prm2");
sl@0
  1187
	TEST(idxPrm2 > 0);
sl@0
  1188
	
sl@0
  1189
	RDebug::Print(_L("Do %d searches using the prepared SQL statement\r\n"), KNamesCnt);
sl@0
  1190
	totalCmpFCnt = 0;
sl@0
  1191
	t1.UniversalTime();
sl@0
  1192
	for(TInt i=0;i<KNamesCnt;++i)
sl@0
  1193
		{
sl@0
  1194
		const TDesC8& firstName = KNames[i].iFirstName;
sl@0
  1195
		rc = sqlite3_bind_text(stmtHandle, idxPrm1, (const char*)firstName.Ptr(), firstName.Length(), SQLITE_STATIC);
sl@0
  1196
		TEST2(rc, SQLITE_OK);
sl@0
  1197
		
sl@0
  1198
		const TDesC8& surname = KNames[i].iSurname;
sl@0
  1199
		rc = sqlite3_bind_text(stmtHandle, idxPrm2, (const char*)surname.Ptr(), surname.Length(), SQLITE_STATIC);
sl@0
  1200
		TEST2(rc, SQLITE_OK);
sl@0
  1201
		
sl@0
  1202
		TheCmpFCallCnt = 0;
sl@0
  1203
		TInt recordCnt = 0;
sl@0
  1204
		while((rc = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
  1205
			{
sl@0
  1206
			++recordCnt;
sl@0
  1207
			}
sl@0
  1208
		totalCmpFCnt += TheCmpFCallCnt;
sl@0
  1209
		TEST(recordCnt == 1);
sl@0
  1210
		rc = sqlite3_reset(stmtHandle);
sl@0
  1211
		TEST2(rc, SQLITE_OK);
sl@0
  1212
		}
sl@0
  1213
	t2.UniversalTime();
sl@0
  1214
	diffTime = t2.MicroSecondsFrom(t1); 
sl@0
  1215
	diffTime = diffTime.Int64() / 1000;
sl@0
  1216
	t = (TInt)diffTime.Int64();
sl@0
  1217
	RDebug::Print(_L("'Search' time: %d ms\r\n"), t);
sl@0
  1218
	RDebug::Print(_L("Total 'search' ('CompareF' used) operations=%d, average_per_iter=%d\r\n"), totalCmpFCnt, totalCmpFCnt/KNamesCnt);
sl@0
  1219
sl@0
  1220
	sqlite3_finalize(stmtHandle);
sl@0
  1221
	stmtHandle = NULL;
sl@0
  1222
	
sl@0
  1223
/* END OF - TEST CASE 2 "Do 100 searches in 1001 contacts" */	
sl@0
  1224
sl@0
  1225
	t1.UniversalTime();
sl@0
  1226
	sqlite3_finalize(stmtHandle);
sl@0
  1227
	t2.UniversalTime();
sl@0
  1228
	diffTime = t2.MicroSecondsFrom(t1); 
sl@0
  1229
	diffTime = diffTime.Int64() / 1000;
sl@0
  1230
	t = (TInt)diffTime.Int64();
sl@0
  1231
	RDebug::Print(_L("'Finalize SQL statement' time: %d ms\r\n"), t);
sl@0
  1232
sl@0
  1233
   	sqlite3_close(dbHandle);
sl@0
  1234
	PrintFileSize(aDbFilePath);
sl@0
  1235
	}
sl@0
  1236
sl@0
  1237
static void TwoDatabasesTest()
sl@0
  1238
	{
sl@0
  1239
	(void)sqlite3SymbianLastOsError();
sl@0
  1240
	sqlite3 *dbHandle1 = NULL;
sl@0
  1241
	TInt rc1 = sqlite3_open(KDbName1, &dbHandle1);
sl@0
  1242
	if(rc1 != SQLITE_OK)
sl@0
  1243
		{
sl@0
  1244
		rc1 = sqlite3SymbianLastOsError();
sl@0
  1245
		}
sl@0
  1246
sl@0
  1247
	(void)sqlite3SymbianLastOsError();
sl@0
  1248
	sqlite3 *dbHandle2 = NULL;
sl@0
  1249
	TInt rc2 = sqlite3_open(KDbName1, &dbHandle2);
sl@0
  1250
	if(rc2 != SQLITE_OK)
sl@0
  1251
		{
sl@0
  1252
		rc2 = sqlite3SymbianLastOsError();
sl@0
  1253
		}
sl@0
  1254
	
sl@0
  1255
   	sqlite3_close(dbHandle2);
sl@0
  1256
sl@0
  1257
   	sqlite3_close(dbHandle1);
sl@0
  1258
	
sl@0
  1259
	(void)TheFs.Delete(ConvertToUtf16(KDbName1));
sl@0
  1260
	
sl@0
  1261
	TEST2(rc1, KErrNone);
sl@0
  1262
	TEST2(rc2, KErrInUse);//EFileRead | EFileWrite file open mode!
sl@0
  1263
	}
sl@0
  1264
sl@0
  1265
static void QuickTest()
sl@0
  1266
	{
sl@0
  1267
	(void)TheFs.Delete(ConvertToUtf16(KDbName1));
sl@0
  1268
sl@0
  1269
	sqlite3 *dbHandle1 = NULL;
sl@0
  1270
	TInt err = sqlite3_open(KDbName1, &dbHandle1);
sl@0
  1271
	TEST2(err, SQLITE_OK);
sl@0
  1272
sl@0
  1273
	err = sqlite3_exec(dbHandle1, "CREATE TABLE A(Fld1 INTEGER, Fld2 TEXT);", 0, 0, 0);
sl@0
  1274
	TEST2(err, SQLITE_OK);
sl@0
  1275
sl@0
  1276
	sqlite3_stmt* stmtHandle = NULL;
sl@0
  1277
	_LIT(KSql1, "INSERT INTO A(Fld1, Fld2) VALUES(1, :Val)\x0");
sl@0
  1278
	err = sqlite3_prepare16_v2(dbHandle1, KSql1().Ptr(), -1, &stmtHandle, NULL);
sl@0
  1279
	TEST2(err, SQLITE_OK);
sl@0
  1280
sl@0
  1281
	_LIT(KPrmData, "U012");
sl@0
  1282
	err = sqlite3_bind_text16(stmtHandle, 1, (const void*)KPrmData().Ptr(), KPrmData().Length() * sizeof(TText), SQLITE_STATIC);
sl@0
  1283
	TEST2(err, SQLITE_OK);
sl@0
  1284
sl@0
  1285
	while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
sl@0
  1286
		{
sl@0
  1287
		}
sl@0
  1288
	if(err == SQLITE_ERROR)	//It may be "out of memory" problem
sl@0
  1289
		{
sl@0
  1290
		err = sqlite3_reset(stmtHandle);
sl@0
  1291
		__ASSERT_ALWAYS(err != SQLITE_OK, User::Invariant());
sl@0
  1292
		}
sl@0
  1293
sl@0
  1294
   	sqlite3_finalize(stmtHandle);
sl@0
  1295
   	
sl@0
  1296
	_LIT(KSql2, "SELECT * FROM A WHERE Fld1 = 1\x0");
sl@0
  1297
	err = sqlite3_prepare16_v2(dbHandle1, KSql2().Ptr(), -1, &stmtHandle, NULL);
sl@0
  1298
	TEST2(err, SQLITE_OK);
sl@0
  1299
sl@0
  1300
	err = sqlite3_step(stmtHandle);
sl@0
  1301
	TEST2(err, SQLITE_ROW);
sl@0
  1302
sl@0
  1303
	const void* data = sqlite3_column_text16(stmtHandle, 1);
sl@0
  1304
	UNUSED_VAR(data);
sl@0
  1305
	TInt charLength  = sqlite3_column_bytes16(stmtHandle, 1) / sizeof(TUint16);
sl@0
  1306
	UNUSED_VAR(charLength);
sl@0
  1307
   	
sl@0
  1308
   	sqlite3_finalize(stmtHandle);
sl@0
  1309
   	
sl@0
  1310
   	sqlite3_close(dbHandle1);
sl@0
  1311
sl@0
  1312
	(void)TheFs.Delete(ConvertToUtf16(KDbName1));
sl@0
  1313
	}
sl@0
  1314
sl@0
  1315
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
  1316
sl@0
  1317
static void TemdDbTest()
sl@0
  1318
	{
sl@0
  1319
	sqlite3 *dbHandle = NULL;
sl@0
  1320
	TInt rc = sqlite3_open(KDbName1, &dbHandle);
sl@0
  1321
	if(rc != SQLITE_OK)
sl@0
  1322
		{
sl@0
  1323
		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
sl@0
  1324
		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
sl@0
  1325
		RDebug::Print(_L("'sqlite3_open()' failed, err %d, error msg: \"%S\"\r\n"), rc, &msg);
sl@0
  1326
		TEST(0);
sl@0
  1327
		}
sl@0
  1328
	TEST(dbHandle != NULL);
sl@0
  1329
sl@0
  1330
	char *zErrMsg = 0;
sl@0
  1331
sl@0
  1332
	_LIT8(KSql1, "CREATE TEMP TABLE A(F1 INTEGER)\x0");
sl@0
  1333
	rc = sqlite3_exec(dbHandle, reinterpret_cast <const char*> (KSql1().Ptr()), callback, 0, &zErrMsg);
sl@0
  1334
	if(rc != SQLITE_OK)
sl@0
  1335
		{
sl@0
  1336
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
  1337
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
  1338
    	sqlite3_free(zErrMsg);
sl@0
  1339
    	TEST(0);
sl@0
  1340
		}
sl@0
  1341
sl@0
  1342
	_LIT8(KSql2, "INSERT INTO A(F1) VALUES(2)\x0");
sl@0
  1343
	rc = sqlite3_exec(dbHandle, reinterpret_cast <const char*> (KSql2().Ptr()), callback, 0, &zErrMsg);
sl@0
  1344
	if(rc != SQLITE_OK)
sl@0
  1345
		{
sl@0
  1346
  		TPtrC p = ConvertToUtf16(zErrMsg);
sl@0
  1347
  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
sl@0
  1348
    	sqlite3_free(zErrMsg);
sl@0
  1349
    	TEST(0);
sl@0
  1350
		}
sl@0
  1351
sl@0
  1352
   	sqlite3_close(dbHandle);
sl@0
  1353
	(void)TheFs.Delete(ConvertToUtf16(KDbName1));
sl@0
  1354
	}
sl@0
  1355
sl@0
  1356
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
  1357
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
  1358
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
  1359
sl@0
  1360
static void CreateTestDir()
sl@0
  1361
    {
sl@0
  1362
    RFs fs;
sl@0
  1363
	TInt err = fs.Connect();
sl@0
  1364
	TEST2(err, KErrNone);
sl@0
  1365
sl@0
  1366
	err = fs.MkDir(KTestDir);
sl@0
  1367
	TEST(err == KErrNone || err == KErrAlreadyExists);
sl@0
  1368
	
sl@0
  1369
	fs.Close();
sl@0
  1370
	}
sl@0
  1371
sl@0
  1372
static void DoTestsL()
sl@0
  1373
	{
sl@0
  1374
	TheTest.Next(_L("Attempt to open twice the same database file in the same thread"));
sl@0
  1375
	TwoDatabasesTest();
sl@0
  1376
sl@0
  1377
	TheTest.Next(_L("TEMP database test"));
sl@0
  1378
	TemdDbTest();
sl@0
  1379
sl@0
  1380
	TheTest.Next(_L("Quick test"));
sl@0
  1381
	QuickTest();
sl@0
  1382
sl@0
  1383
	TheTest.Next(_L("SQLite tests 1"));
sl@0
  1384
	DoTests1();
sl@0
  1385
	
sl@0
  1386
	TheTest.Next(_L("SQLite tests 2"));
sl@0
  1387
	DoTests2();
sl@0
  1388
sl@0
  1389
	TheTest.Next(_L("Accented column names"));
sl@0
  1390
	AccentedColumnNamesTestL();
sl@0
  1391
	
sl@0
  1392
	TFileName fname;
sl@0
  1393
	User::CommandLine(fname);
sl@0
  1394
	TParse parse;
sl@0
  1395
	parse.Set(fname, &KContactsFile, 0);
sl@0
  1396
	const TDesC& dbFilePath = parse.FullName();
sl@0
  1397
	
sl@0
  1398
	PrintConfig(dbFilePath);
sl@0
  1399
sl@0
  1400
	TheTest.Next(_L("String searching tests. 16-bit strings. Simple contacts"));
sl@0
  1401
	SearchDbTest16(dbFilePath);
sl@0
  1402
sl@0
  1403
	TheTest.Next(_L("String searching tests. 8-bit strings. Simple contacts"));
sl@0
  1404
	SearchDbTest8(dbFilePath, KSimpleContactsSqlFile, _L("Search simple contacts"));
sl@0
  1405
	}
sl@0
  1406
sl@0
  1407
TInt E32Main()
sl@0
  1408
	{
sl@0
  1409
	TheTest.Title();
sl@0
  1410
	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-LEGACY-T_SQLITE-0001 SQLite tests "));
sl@0
  1411
	
sl@0
  1412
	TheTrapCleanup = CTrapCleanup::New ();
sl@0
  1413
	__ASSERT_ALWAYS(TheTrapCleanup != NULL, User::Invariant());
sl@0
  1414
sl@0
  1415
	__UHEAP_MARK;
sl@0
  1416
sl@0
  1417
	CreateTestDir();
sl@0
  1418
	
sl@0
  1419
	TheTest(TheFs.Connect() == KErrNone);
sl@0
  1420
	DeleteTestFiles();
sl@0
  1421
sl@0
  1422
	CreatePrivateDirs();
sl@0
  1423
sl@0
  1424
	//Init sqlite library
sl@0
  1425
	sqlite3SymbianLibInit();
sl@0
  1426
sl@0
  1427
	TRAPD(err, DoTestsL());
sl@0
  1428
	
sl@0
  1429
	sqlite3SymbianLibFinalize();
sl@0
  1430
sl@0
  1431
	CloseSTDLIB();
sl@0
  1432
	
sl@0
  1433
	DeleteTestFiles();
sl@0
  1434
	
sl@0
  1435
	TEST2(err, KErrNone);
sl@0
  1436
	
sl@0
  1437
	TheFs.Close();
sl@0
  1438
sl@0
  1439
	__UHEAP_MARKEND;
sl@0
  1440
sl@0
  1441
	delete TheTrapCleanup;
sl@0
  1442
sl@0
  1443
sl@0
  1444
	TheTest.End();
sl@0
  1445
	TheTest.Close();
sl@0
  1446
sl@0
  1447
sl@0
  1448
	return KErrNone;
sl@0
  1449
	}