os/persistentdata/persistentstorage/sql/TEST/t_sqlite.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sql/TEST/t_sqlite.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1449 @@
     1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <e32std.h>
    1.20 +#include <f32file.h>
    1.21 +#include <e32test.h>
    1.22 +#include <stdio.h>
    1.23 +#include <stdlib.h>
    1.24 +#include <string.h> 
    1.25 +#include <hal.h>
    1.26 +#include <utf.h>				//CnvUtfConverter
    1.27 +#include "sqlite3.h"
    1.28 +#include "SqliteSymbian.h"
    1.29 +
    1.30 +#define UNUSED_VAR(a) a = (a)
    1.31 +
    1.32 +//Forward declaration
    1.33 +struct Mem;
    1.34 +
    1.35 +static RTest 			TheTest(_L ("t_sqlite.exe"));
    1.36 +static CTrapCleanup*	TheTrapCleanup = NULL;
    1.37 +static RFs 				TheFs;
    1.38 +
    1.39 +//File-local buffer used for converted to UTF16 (from UTF8) strings or
    1.40 +//for a temporary file name buffer.
    1.41 +static TUint16 TheFileName[KMaxFileName + 1];
    1.42 +
    1.43 +_LIT(KTestDir, "c:\\test\\");
    1.44 +
    1.45 +const char KDbName1[] = "c:\\test\\t_sqlite1.db";
    1.46 +const char KDbName2[] = "c:\\test\\t_sqlite2.db";
    1.47 +_LIT(KContactsFile, "c:\\test\\Contacts.DB");
    1.48 +
    1.49 +sqlite3 *TheDb1 = NULL, *TheDb2 = NULL;
    1.50 +
    1.51 +static TBuf<4096> TheBuf1;
    1.52 +static TBuf<4096> TheBuf2;
    1.53 +static TBuf8<512> TheBuf3;
    1.54 +
    1.55 +static TInt TheCmpFCallCnt = 0;
    1.56 +static TInt TheCmpCCallCnt = 0;
    1.57 +
    1.58 +_LIT(KSimpleContactsSqlFile, "z:\\test\\add_simple_contacts.sql");
    1.59 +
    1.60 +//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)
    1.61 +#ifdef _SQLPROFILER
    1.62 +TInt TheSqlSrvProfilerFileRead = 0;
    1.63 +TInt TheSqlSrvProfilerFileWrite = 0;
    1.64 +TInt TheSqlSrvProfilerFileSync = 0;
    1.65 +TInt TheSqlSrvProfilerFileSetSize = 0;
    1.66 +#endif
    1.67 +
    1.68 +///////////////////////////////////////////////////////////////////////////////////////
    1.69 +
    1.70 +//Creates TPtrC object which points to the unicode presentation of aData.
    1.71 +//aData argument is expected to point to UTF8 encoded, zero terminated string.
    1.72 +//The function returns a TPtrC, non-zero terminated object pointing to the unicode presentation of aData.
    1.73 +//If the length of the returned TPtrC object is 0 - it means that unicode conversion of aData failed, 
    1.74 +//because the aData is too long or is NULL. 
    1.75 +//Max allowed aData length is KMaxFileName (excluding terminating 0 character).
    1.76 +static TPtrC ConvertToUtf16(const char *aData)
    1.77 +	{
    1.78 +	TPtrC ptr(0, 0);
    1.79 +	if(aData)
    1.80 +		{
    1.81 +		TInt len = mbstowcs(reinterpret_cast <wchar_t*> (TheFileName), aData, KMaxFileName + 1);
    1.82 +		//Check converted string length. If it is longer than KMaxFileName characters, then aData is too long.
    1.83 +		if(len > 0 && len <= KMaxFileName)
    1.84 +			{
    1.85 +			ptr.Set(TheFileName, len);
    1.86 +			}
    1.87 +		}
    1.88 +	return ptr;
    1.89 +	}
    1.90 +
    1.91 +///////////////////////////////////////////////////////////////////////////////////////
    1.92 +
    1.93 +static void DeleteTestFiles()
    1.94 +	{
    1.95 +   	sqlite3_close(TheDb2);
    1.96 +   	sqlite3_close(TheDb1);
    1.97 +   	
    1.98 +	TheFs.Delete(ConvertToUtf16(KDbName2));
    1.99 +	TheFs.Delete(ConvertToUtf16(KDbName1));
   1.100 +	TheFs.Delete(KContactsFile);
   1.101 +	}
   1.102 +
   1.103 +///////////////////////////////////////////////////////////////////////////////////////
   1.104 +///////////////////////////////////////////////////////////////////////////////////////
   1.105 +//Test macros and functions
   1.106 +static void Check(TInt aValue, TInt aLine)
   1.107 +	{
   1.108 +	if(!aValue)
   1.109 +		{
   1.110 +		DeleteTestFiles();
   1.111 +		TheTest(EFalse, aLine);
   1.112 +		}
   1.113 +	}
   1.114 +static void Check(TInt aValue, TInt aExpected, TInt aLine)
   1.115 +	{
   1.116 +	if(aValue != aExpected)
   1.117 +		{
   1.118 +		DeleteTestFiles();
   1.119 +		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
   1.120 +		TheTest(EFalse, aLine);
   1.121 +		}
   1.122 +	}
   1.123 +#define TEST(arg) ::Check((arg), __LINE__)
   1.124 +#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
   1.125 +
   1.126 +///////////////////////////////////////////////////////////////////////////////////////
   1.127 +
   1.128 +static void CreatePrivateDirs()
   1.129 +	{
   1.130 +	RFs fs;
   1.131 +	TEST2(fs.Connect(), KErrNone);
   1.132 +	for(TInt i=0;i<('Z'-'A');++i)
   1.133 +		{
   1.134 +		fs.CreatePrivatePath(i);
   1.135 +		}
   1.136 +	fs.Close();
   1.137 +	}
   1.138 +
   1.139 +///////////////////////////////////////////////////////////////////////////////////////
   1.140 +
   1.141 +static void PrintConfig(const TDesC& aDbFilePath)
   1.142 +	{
   1.143 +	RDebug::Print(_L("================= Configuration ================\r\n"));	
   1.144 +	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);	
   1.145 +	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);	
   1.146 +	_LIT(K1, "On");
   1.147 +	_LIT(K2, "Off");
   1.148 +	RDebug::Print(_L("Autovacuum: %S\r\n"), SQLITE_DEFAULT_AUTOVACUUM ? &K1 : &K2);	
   1.149 +	#ifdef SQLITE_DEBUG
   1.150 +		RDebug::Print(_L("Debug: On\r\n"));	
   1.151 +	#else
   1.152 +		RDebug::Print(_L("Debug: Off\r\n"));	
   1.153 +	#endif
   1.154 +	RDebug::Print(_L("Db file: %S\r\n"), &aDbFilePath);
   1.155 +	RDebug::Print(_L("================================================\r\n"));	
   1.156 +	}
   1.157 +	
   1.158 +///////////////////////////////////////////////////////////////////////////////////////
   1.159 +
   1.160 +static TInt callback(void */*NotUsed*/, TInt argc, char **argv, char **azColName)
   1.161 +	{
   1.162 +	TInt i;
   1.163 +	for(i=0; i<argc; i++)
   1.164 +		{
   1.165 +		TPtrC8 colName((const TUint8*)azColName[i]);
   1.166 +		TPtrC8 colVal((const TUint8*)(argv[i] ? argv[i] : "NULL"));
   1.167 +		TheBuf2.Copy(colName);
   1.168 +		TheBuf2.Append(_L(" = "));
   1.169 +		TheBuf1.Copy(colVal);
   1.170 +		TheBuf2.Append(TheBuf1);
   1.171 +    	RDebug::Print(_L("%S\r\n"), &TheBuf2);
   1.172 +		}
   1.173 +  	RDebug::Print(_L("\n"));
   1.174 +  	return 0;
   1.175 +	}
   1.176 +
   1.177 +static TInt Compare(void*, TInt size1, const void* p1, TInt size2, const void* p2)
   1.178 +	{
   1.179 +	TPtrC8 ptr1((TUint8*)p1, size1);
   1.180 +	TPtrC8 ptr2((TUint8*)p2, size2);
   1.181 +	
   1.182 +	TInt res = ptr1.CompareF(ptr2);
   1.183 +	return res;		
   1.184 +	}
   1.185 +
   1.186 +static TInt Compare2(void*, TInt size1, const void* p1, TInt size2, const void* p2)
   1.187 +	{
   1.188 +	TPtrC8 ptr1((TUint8*)p1, size1);
   1.189 +	TPtrC8 ptr2((TUint8*)p2, size2);
   1.190 +	
   1.191 +	TInt res = -ptr1.Compare(ptr2);
   1.192 +	return res;		
   1.193 +	}
   1.194 +
   1.195 +
   1.196 +static void Func(sqlite3_context* ctx, TInt nCnt, sqlite3_value** pp)
   1.197 +	{
   1.198 +	for(TInt i=0;i<nCnt;++i)
   1.199 +		{
   1.200 +		TInt v = sqlite3_value_int(*pp++);
   1.201 +		v = v;
   1.202 +		}
   1.203 +	sqlite3_result_int(ctx, 564);		
   1.204 +	}
   1.205 +
   1.206 +///////////////////////////////////////////////////////////////////////////////////////
   1.207 +
   1.208 +//Create database, table, view, INSERT, SELECT, DELETE sql statements, register collations, 
   1.209 +//user defined functions, CREATE INDEX, CREATE VIEW sql statements, GROUP BY, HAVING, EXCEPT,...
   1.210 +static void DoTests1()
   1.211 +	{
   1.212 +	char *zErrMsg = 0;
   1.213 +	TInt rc;
   1.214 +
   1.215 +	rc = sqlite3_open(KDbName1, &TheDb1);
   1.216 +  	if(rc)
   1.217 +  		{
   1.218 +  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
   1.219 +  		RDebug::Print(_L("Can't open database, err %d, msg: %S\n"), rc, &p);
   1.220 +    	TEST(0);
   1.221 +  		}
   1.222 +
   1.223 +	rc = sqlite3_create_collation(TheDb1, "Clt1", SQLITE_UTF8, NULL, &Compare);
   1.224 +  	if(rc)
   1.225 +  		{
   1.226 +  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
   1.227 +  		RDebug::Print(_L("Can't create collation, err %d, msg: %S\n"), rc, &p);
   1.228 +    	TEST(0);
   1.229 +  		}
   1.230 +
   1.231 +	rc = sqlite3_create_collation(TheDb1, "Clt2", SQLITE_UTF8, NULL, &Compare2);
   1.232 +  	if(rc)
   1.233 +  		{
   1.234 +  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
   1.235 +  		RDebug::Print(_L("Can't create collation, err %d, msg: %S\n"), rc, &p);
   1.236 +    	TEST(0);
   1.237 +  		}
   1.238 +
   1.239 +	rc = sqlite3_create_function(TheDb1, "Func", 2, SQLITE_UTF8, NULL, Func, NULL, NULL);
   1.240 +  	if(rc)
   1.241 +  		{
   1.242 +  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
   1.243 +  		RDebug::Print(_L("Can't create UDF, err %d, msg: %S\n"), rc, &p);
   1.244 +    	TEST(0);
   1.245 +  		}
   1.246 +
   1.247 +	rc = sqlite3_exec(TheDb1, "\
   1.248 +	             BEGIN TRANSACTION;\
   1.249 +	             CREATE TABLE Pn(Id INTEGER PRIMARY KEY AUTOINCREMENT,\
   1.250 +	                             Name TEXT NOT NULL,\
   1.251 +	                             Surname TEXT NOT NULL COLLATE Clt1);\
   1.252 +	             CREATE INDEX PnSurname ON Pn(Surname);\
   1.253 +	             CREATE TABLE Addr(Id INTEGER PRIMARY KEY AUTOINCREMENT,\
   1.254 +	                               PnId INTEGER,\
   1.255 +	                               CityId INTEGER,\
   1.256 +	                               Location TEXT DEFAULT 'address');\
   1.257 +	             CREATE INDEX AddrPnId ON Addr(PnId);\
   1.258 +	             CREATE INDEX AddrCityId ON Addr(CityId);\
   1.259 +	             CREATE TABLE City(ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT Default 'None');\
   1.260 +	             CREATE TABLE Shop(Id INTEGER, Name TEXT);\
   1.261 +	             CREATE TABLE Sales(ShopId INTEGER, M MONEY, D TIMESTAMP, D1 DATE, T1 TIME);\
   1.262 +	             CREATE TRIGGER TrgPnDel BEFORE DELETE ON Pn \
   1.263 +	             BEGIN \
   1.264 +					SELECT Func(2,8);\
   1.265 +	                SELECT CASE WHEN ((SELECT COUNT(*) FROM Addr WHERE Addr.PnId = old.Id) > 0) \
   1.266 +	                            THEN RAISE (ABORT, 'Foreign key constraint violation!!!') \
   1.267 +	                END;\
   1.268 +	             END;\
   1.269 +	             CREATE VIEW V1 AS SELECT * FROM Pn;\
   1.270 +	             CREATE VIEW V2 AS SELECT * FROM Addr;\
   1.271 +	             CREATE VIEW V3 AS SELECT * FROM Sales;\
   1.272 +	             CREATE VIEW V4 AS SELECT * FROM Shop;\
   1.273 +	             COMMIT TRANSACTION;", callback, 0, &zErrMsg);
   1.274 +	             
   1.275 +	if(rc != SQLITE_OK)
   1.276 +		{
   1.277 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.278 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.279 +    	sqlite3_free(zErrMsg);
   1.280 +    	TEST(0);
   1.281 +		}
   1.282 +
   1.283 +	rc = sqlite3_exec(TheDb1, 
   1.284 +	"BEGIN TRANSACTION;\
   1.285 +	 INSERT INTO Pn(Name, Surname) VALUES('Aaaa1', 'Aaaa2');\
   1.286 +	 INSERT INTO Pn(Name, Surname) VALUES('Bbbb1', 'Bbbb2');\
   1.287 +	 INSERT INTO Pn(Name, Surname) VALUES('Cccc1', 'Cccc2');\
   1.288 +	 INSERT INTO Pn(Name, Surname) VALUES('Dddd1', 'Dddd2');\
   1.289 +	 INSERT INTO City(Name) VALUES('London');\
   1.290 +	 INSERT INTO City(Name) VALUES('Manchester');\
   1.291 +	 INSERT INTO Addr(PnId, CityId, Location) SELECT Pn.Id, City.Id, 'Here' FROM Pn, City WHERE Pn.Surname = 'Cccc2' AND City.Name = 'London';\
   1.292 +	 INSERT INTO Addr(PnId, CityId, Location) SELECT Pn.Id, City.Id, 'There' FROM Pn, City WHERE Pn.Surname = 'Bbbb2' AND City.Name = 'Manchester';\
   1.293 +	 INSERT INTO Shop(Id, Name) VALUES(1, 'Shop-1');\
   1.294 +	 INSERT INTO Shop(Id, Name) VALUES(2, 'Shop-2');\
   1.295 +	 INSERT INTO Shop(Id, Name) VALUES(3, 'Shop-3');\
   1.296 +	 INSERT INTO Sales(ShopId, M, D, D1, T1) VALUES(1, 123.0, '2005-01-01', CURRENT_DATE, '12:34:17');\
   1.297 +	 INSERT INTO Sales(ShopId, M, D, D1, T1) VALUES(2, 100.0, '2005-01-01', '2005-01-27', '12:34:18');\
   1.298 +	 INSERT INTO Sales(ShopId, M, D, D1, T1) VALUES(2, 200.0, '2005-01-02', '2005-01-28', '12:34:19');\
   1.299 +	 INSERT INTO Sales(ShopId, M, D, D1, T1) VALUES(3, 200.0, '2005-01-03', '2005-01-29', '12:34:23');\
   1.300 +	 COMMIT TRANSACTION;", callback, 0, &zErrMsg);
   1.301 +	if(rc != SQLITE_OK)
   1.302 +		{
   1.303 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.304 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.305 +    	sqlite3_free(zErrMsg);
   1.306 +    	TEST(0);
   1.307 +		}
   1.308 +
   1.309 +	rc = sqlite3_exec(TheDb1, "SELECT COUNT(*) FROM Shop", callback, 0, &zErrMsg);
   1.310 +	if(rc != SQLITE_OK)
   1.311 +		{
   1.312 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.313 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.314 +    	sqlite3_free(zErrMsg);
   1.315 +    	TEST(0);
   1.316 +		}
   1.317 +
   1.318 +	for(TInt i=0;i<500;++i)
   1.319 +		{
   1.320 +		_LIT8(KSqlStmt, "INSERT INTO Shop(Id, Name) VALUES(%d, 'Shop-%d')\x0");
   1.321 +		TheBuf3.Format(KSqlStmt, i+1, i+1);
   1.322 +		rc = sqlite3_exec(TheDb1, (const char*)TheBuf3.Ptr(), callback, 0, &zErrMsg);
   1.323 +		if(rc != SQLITE_OK)
   1.324 +			{
   1.325 +	  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.326 +	  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.327 +	    	sqlite3_free(zErrMsg);
   1.328 +    		TEST(0);
   1.329 +			}
   1.330 +		}
   1.331 +
   1.332 +	_LIT8(KSqlStmt2, "DELETE FROM Shop WHERE Id > 400\x0");
   1.333 +	rc = sqlite3_exec(TheDb1, (const char*)KSqlStmt2().Ptr(), callback, 0, &zErrMsg);
   1.334 +	if(rc != SQLITE_OK)
   1.335 +		{
   1.336 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.337 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.338 +    	sqlite3_free(zErrMsg);
   1.339 +   		TEST(0);
   1.340 +		}
   1.341 +
   1.342 +	_LIT8(KSqlStmt3, "SELECT COUNT(*) FROM Shop\x0");
   1.343 +	rc = sqlite3_exec(TheDb1, (const char*)KSqlStmt3().Ptr(), callback, 0, &zErrMsg);
   1.344 +	if(rc != SQLITE_OK)
   1.345 +		{
   1.346 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.347 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.348 +    	sqlite3_free(zErrMsg);
   1.349 +   		TEST(0);
   1.350 +		}
   1.351 +
   1.352 +	rc = sqlite3_exec(TheDb1, "SELECT Pn.Surname, Addr.Location, City.Name FROM Pn \
   1.353 +	      INNER JOIN Addr ON Pn.Id = Addr.PnId \
   1.354 +	      INNER JOIN City ON Addr.CityId = City.Id \
   1.355 +	      ORDER BY Surname COLLATE Clt2", callback, 0, &zErrMsg);
   1.356 +	if(rc != SQLITE_OK)
   1.357 +		{
   1.358 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.359 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.360 +    	sqlite3_free(zErrMsg);
   1.361 +   		TEST(0);
   1.362 +		}
   1.363 +	      
   1.364 +	rc = sqlite3_exec(TheDb1, "SELECT * FROM Addr", callback, 0, &zErrMsg);
   1.365 +	if(rc != SQLITE_OK)
   1.366 +		{
   1.367 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.368 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.369 +    	sqlite3_free(zErrMsg);
   1.370 +   		TEST(0);
   1.371 +		}
   1.372 +	
   1.373 +	rc = sqlite3_exec(TheDb1, "SELECT * FROM Sales WHERE D1 > '2005-08-27'", callback, 0, &zErrMsg);
   1.374 +	if(rc != SQLITE_OK)
   1.375 +		{
   1.376 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.377 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.378 +    	sqlite3_free(zErrMsg);
   1.379 +   		TEST(0);
   1.380 +		}
   1.381 +	
   1.382 +	rc = sqlite3_exec(TheDb1, "SELECT V1.* FROM V1 EXCEPT SELECT V1.* FROM V1 WHERE V1.Id = 2", callback, 0, &zErrMsg);
   1.383 +	if(rc != SQLITE_OK)
   1.384 +		{
   1.385 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.386 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.387 +    	sqlite3_free(zErrMsg);
   1.388 +   		TEST(0);
   1.389 +		}
   1.390 +
   1.391 +	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);
   1.392 +	if(rc != SQLITE_OK)
   1.393 +		{
   1.394 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.395 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.396 +    	sqlite3_free(zErrMsg);
   1.397 +   		TEST(0);
   1.398 +		}
   1.399 +	
   1.400 +	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);
   1.401 +	if(rc != SQLITE_OK)
   1.402 +		{
   1.403 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.404 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.405 +    	sqlite3_free(zErrMsg);
   1.406 +   		TEST(0);
   1.407 +		}
   1.408 +
   1.409 +	sqlite3_close(TheDb1);
   1.410 +	TheFs.Delete(ConvertToUtf16(KDbName1));
   1.411 +	}
   1.412 +
   1.413 +//Generic SQL tests.
   1.414 +static void DoTests2()
   1.415 +	{
   1.416 +	TheFs.Delete(ConvertToUtf16(KDbName1));
   1.417 +	
   1.418 +	char *zErrMsg = 0;
   1.419 +	TInt rc;
   1.420 +
   1.421 +	rc = sqlite3_open(KDbName1, &TheDb1);
   1.422 +  	if(rc != SQLITE_OK)
   1.423 +  		{
   1.424 +  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
   1.425 +  		RDebug::Print(_L("Can't open database, err %d, msg: %S\n"), rc, &p);
   1.426 +    	TEST(0);
   1.427 +  		}
   1.428 +  		
   1.429 +	rc = sqlite3_exec(TheDb1, "PRAGMA auto_vacuum = 1", callback, 0, &zErrMsg);
   1.430 +	if(rc != SQLITE_OK)
   1.431 +		{
   1.432 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.433 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.434 +    	sqlite3_free(zErrMsg);
   1.435 +   		TEST(0);
   1.436 +		}
   1.437 +	
   1.438 +	rc = sqlite3_exec(TheDb1, "\
   1.439 +	             BEGIN TRANSACTION;\
   1.440 +	             CREATE TABLE Shop(Id INTEGER, Name TEXT);\
   1.441 +	             CREATE INDEX ShopName ON Shop(Name);\
   1.442 +	             COMMIT TRANSACTION;", callback, 0, &zErrMsg);
   1.443 +	if(rc != SQLITE_OK)
   1.444 +		{
   1.445 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.446 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.447 +    	sqlite3_free(zErrMsg);
   1.448 +   		TEST(0);
   1.449 +		}
   1.450 +		
   1.451 +	rc = sqlite3_exec(TheDb1, "BEGIN TRANSACTION", callback, 0, &zErrMsg);
   1.452 +	if(rc != SQLITE_OK)
   1.453 +		{
   1.454 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.455 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.456 +    	sqlite3_free(zErrMsg);
   1.457 +   		TEST(0);
   1.458 +		}
   1.459 +	
   1.460 +	for(TInt i=0;i<1000;++i)
   1.461 +		{
   1.462 +		_LIT8(KSqlStmt, "INSERT INTO Shop(Id, Name) VALUES(%d, 'Shop-%d')\x0");
   1.463 +		TheBuf3.Format(KSqlStmt, i, i);
   1.464 +		rc = sqlite3_exec(TheDb1, (const char*)TheBuf3.Ptr(), callback, 0, &zErrMsg);
   1.465 +		if(rc != SQLITE_OK)
   1.466 +			{
   1.467 +	  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.468 +	  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.469 +	    	sqlite3_free(zErrMsg);
   1.470 +	   		TEST(0);
   1.471 +			}
   1.472 +		}
   1.473 +		
   1.474 +	rc = sqlite3_exec(TheDb1, "COMMIT TRANSACTION", callback, 0, &zErrMsg);
   1.475 +	if(rc != SQLITE_OK)
   1.476 +		{
   1.477 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.478 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.479 +    	sqlite3_free(zErrMsg);
   1.480 +   		TEST(0);
   1.481 +		}
   1.482 +
   1.483 +	rc = sqlite3_exec(TheDb1, "SELECT COUNT(*) FROM Shop", callback, 0, &zErrMsg);
   1.484 +	if(rc != SQLITE_OK)
   1.485 +		{
   1.486 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.487 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.488 +    	sqlite3_free(zErrMsg);
   1.489 +   		TEST(0);
   1.490 +		}
   1.491 +
   1.492 +	rc = sqlite3_exec(TheDb1, "BEGIN TRANSACTION", callback, 0, &zErrMsg);
   1.493 +	if(rc != SQLITE_OK)
   1.494 +		{
   1.495 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.496 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.497 +    	sqlite3_free(zErrMsg);
   1.498 +   		TEST(0);
   1.499 +		}
   1.500 +	
   1.501 +	rc = sqlite3_exec(TheDb1, "DELETE FROM Shop WHERE Id > 100", callback, 0, &zErrMsg);
   1.502 +	if(rc != SQLITE_OK)
   1.503 +		{
   1.504 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.505 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.506 +    	sqlite3_free(zErrMsg);
   1.507 +   		TEST(0);
   1.508 +		}
   1.509 +		
   1.510 +	rc = sqlite3_exec(TheDb1, "COMMIT TRANSACTION", callback, 0, &zErrMsg);
   1.511 +	if(rc != SQLITE_OK)
   1.512 +		{
   1.513 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.514 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.515 +    	sqlite3_free(zErrMsg);
   1.516 +   		TEST(0);
   1.517 +		}
   1.518 +
   1.519 +	rc = sqlite3_exec(TheDb1, "SELECT COUNT(*) FROM Shop", callback, 0, &zErrMsg);
   1.520 +	if(rc != SQLITE_OK)
   1.521 +		{
   1.522 +  		TPtrC p = ConvertToUtf16(zErrMsg);
   1.523 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
   1.524 +    	sqlite3_free(zErrMsg);
   1.525 +   		TEST(0);
   1.526 +		}
   1.527 +
   1.528 +	sqlite3_close(TheDb1);
   1.529 +	TheFs.Delete(ConvertToUtf16(KDbName1));
   1.530 +	}
   1.531 +
   1.532 +//Accented column names test
   1.533 +static void AccentedColumnNamesTestL()
   1.534 +	{
   1.535 +	TheFs.Delete(ConvertToUtf16(KDbName1));
   1.536 +
   1.537 +	//Open database
   1.538 +	TPtrC tmp = ConvertToUtf16(KDbName1);
   1.539 +	TBuf<KMaxFileName + 1> fname;
   1.540 +	fname.Copy(tmp);
   1.541 +	TInt rc = sqlite3_open16(fname.PtrZ(), &TheDb1);//!!!!16-bit encoding!!!!!
   1.542 +  	if(rc != SQLITE_OK)
   1.543 +  		{
   1.544 +  		TPtrC p = ConvertToUtf16(sqlite3_errmsg(TheDb1));
   1.545 +  		RDebug::Print(_L("Can't open database, err %d, msg: %S\n"), rc, &p);
   1.546 +    	TEST(0);
   1.547 +  		}
   1.548 +	
   1.549 +	//Create table
   1.550 +	_LIT(KSqlStrZ, "CREATE TABLE abc(col_\u00C4 integer, col_A text)\0");
   1.551 +//	_LIT(KSqlStrZ, "CREATE TABLE abc(col_a integer, col_A text)\0");
   1.552 +//	_LIT(KSqlStrZ, "CREATE TABLE abc(col_Ä integer, col_ä text)\0");
   1.553 +//	_LIT(KSqlStrZ, "CREATE TABLE abc(col_Ä integer, col_A\x308 text)\0");
   1.554 +	sqlite3_stmt* stmtHandle = NULL;
   1.555 +	const void* stmtTailZ = NULL;
   1.556 +	rc = sqlite3_prepare16_v2(TheDb1, KSqlStrZ().Ptr(), -1, &stmtHandle, &stmtTailZ);
   1.557 +	if(rc != SQLITE_OK)
   1.558 +		{
   1.559 +		const void* errMsgZ = sqlite3_errmsg16(TheDb1);
   1.560 +		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
   1.561 +		RDebug::Print(_L("'sqlite3_prepare16_v2()' failed, err %d, error msg: \"%S\"\r\n"), rc, &msg);
   1.562 +    	TEST(0);
   1.563 +		}
   1.564 +	rc = sqlite3_step(stmtHandle);
   1.565 +	TEST(rc == SQLITE_DONE);
   1.566 +
   1.567 +	sqlite3_finalize(stmtHandle);
   1.568 +	stmtHandle = NULL;
   1.569 +
   1.570 +	//Select from the table
   1.571 +	_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");
   1.572 +	rc = sqlite3_prepare16_v2(TheDb1, KSqlStrZ2().Ptr(), -1, &stmtHandle, &stmtTailZ);
   1.573 +	if(rc != SQLITE_OK)
   1.574 +		{
   1.575 +		const void* errMsgZ = sqlite3_errmsg16(TheDb1);
   1.576 +		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
   1.577 +		RDebug::Print(_L("'sqlite3_prepare16_v2()' failed, err %d, error msg: \"%S\"\r\n"), rc, &msg);
   1.578 +    	TEST(0);
   1.579 +		}
   1.580 +
   1.581 +	//parameter checks
   1.582 +	TBuf<16> prmNames[5];
   1.583 + 	TInt prmCount = sqlite3_bind_parameter_count(stmtHandle);
   1.584 + 	for(TInt i=1;i<=prmCount;++i)
   1.585 + 		{
   1.586 + 		const char* prmNameZ = sqlite3_bind_parameter_name(stmtHandle, i);
   1.587 +		TPtrC8 name8(reinterpret_cast <const TUint8*> (prmNameZ), strlen(prmNameZ));
   1.588 +		HBufC* name = CnvUtfConverter::ConvertToUnicodeFromUtf8L(name8);
   1.589 +		prmNames[i] = *name;
   1.590 +		delete name;
   1.591 + 		}
   1.592 + 	TInt prmIndex0_1 = sqlite3_bind_parameter_index(stmtHandle, ":prm_Ä");
   1.593 + 	prmIndex0_1 = prmIndex0_1;
   1.594 + 	TInt prmIndex1_1 = sqlite3_bind_parameter_index(stmtHandle, ":prm_ä");
   1.595 + 	prmIndex1_1 = prmIndex1_1;
   1.596 + 	TInt prmIndex2_1 = sqlite3_bind_parameter_index(stmtHandle, ":prm_A");
   1.597 + 	prmIndex2_1 = prmIndex2_1;
   1.598 + 	TInt prmIndex3_1 = sqlite3_bind_parameter_index(stmtHandle, ":prm_a");
   1.599 + 	prmIndex3_1 = prmIndex3_1;
   1.600 +	
   1.601 +	TBuf8<16> name8;
   1.602 +	
   1.603 +	HBufC8* name = CnvUtfConverter::ConvertFromUnicodeToUtf8L(prmNames[1]);
   1.604 +	name8 = *name;
   1.605 +	delete name;
   1.606 + 	TInt prmIndex0_2 = sqlite3_bind_parameter_index(stmtHandle, (const char*)name8.PtrZ());
   1.607 + 	prmIndex0_2 = prmIndex0_2;
   1.608 + 	
   1.609 +	name = CnvUtfConverter::ConvertFromUnicodeToUtf8L(prmNames[2]);
   1.610 +	name8 = *name;
   1.611 +	delete name;
   1.612 + 	TInt prmIndex1_2 = sqlite3_bind_parameter_index(stmtHandle, (const char*)name8.PtrZ());
   1.613 + 	prmIndex1_2 = prmIndex1_2;
   1.614 + 	
   1.615 +	name = CnvUtfConverter::ConvertFromUnicodeToUtf8L(prmNames[3]);
   1.616 +	name8 = *name;
   1.617 +	delete name;
   1.618 + 	TInt prmIndex2_2 = sqlite3_bind_parameter_index(stmtHandle, (const char*)name8.PtrZ());
   1.619 + 	prmIndex2_2 = prmIndex2_2;
   1.620 + 	
   1.621 +	name = CnvUtfConverter::ConvertFromUnicodeToUtf8L(prmNames[4]);
   1.622 +	name8 = *name;
   1.623 +	delete name;
   1.624 + 	TInt prmIndex3_2 = sqlite3_bind_parameter_index(stmtHandle, (const char*)name8.PtrZ());
   1.625 + 	prmIndex3_2 = prmIndex3_2;
   1.626 +	
   1.627 +	//Column checks
   1.628 +	TPtrC colName1(_L("col_\u00C4"));
   1.629 +	TPtrC colName2(_L("col_A"));
   1.630 +	
   1.631 +	const void* p = sqlite3_column_name16(stmtHandle, 0);
   1.632 +	TPtrC pp1((const TUint16*)p, wcslen(reinterpret_cast <const wchar_t*> (p)));
   1.633 +	TEST(colName1 == pp1);
   1.634 +	
   1.635 +	p = sqlite3_column_name16(stmtHandle, 1);
   1.636 +	TPtrC pp2((const TUint16*)p, wcslen(reinterpret_cast <const wchar_t*> (p)));
   1.637 +	TEST(colName2 == pp2);
   1.638 +		
   1.639 +	sqlite3_finalize(stmtHandle);
   1.640 +	
   1.641 +	sqlite3_close(TheDb1);
   1.642 +	TheFs.Delete(ConvertToUtf16(KDbName1));
   1.643 +	}
   1.644 +
   1.645 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.646 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.647 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.648 +//////////////              "Add Contacts" test case                  ////////////////////////////
   1.649 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.650 +
   1.651 +//Read SQL file - 8-bit, zero-terminated string
   1.652 +static char* ReadSQL2(const TDesC& aSqlFileName)
   1.653 +	{
   1.654 +	RFile file;
   1.655 +	TEST2(file.Open(TheFs, aSqlFileName, EFileRead), KErrNone);
   1.656 +	
   1.657 +	TInt size = 0;
   1.658 +	TEST2(file.Size(size), KErrNone);
   1.659 +	
   1.660 +	char* sql = new char [size + 1];
   1.661 +	TEST(sql != NULL);
   1.662 +	
   1.663 +	TPtr8 ptr((TUint8*)sql, size + 1);
   1.664 +	TEST2(file.Read(ptr, size), KErrNone);
   1.665 +	*(sql + size) = 0;
   1.666 +
   1.667 +	file.Close();
   1.668 +	return sql;
   1.669 +	}
   1.670 +
   1.671 +//Read SQL file - 16-bit, zero-terminated string
   1.672 +static HBufC* ReadSQL16(const TDesC& aSqlFileName)
   1.673 +	{
   1.674 +	RFile file;
   1.675 +	TEST2(file.Open(TheFs, aSqlFileName, EFileRead), KErrNone);
   1.676 +	
   1.677 +	TInt size = 0;
   1.678 +	TEST2(file.Size(size), KErrNone);
   1.679 +
   1.680 +	char* sql = new char [size];
   1.681 +	TEST(sql != NULL);
   1.682 +	TPtr8 ptr((TUint8*)sql, size);
   1.683 +	TEST2(file.Read(ptr, size), KErrNone);
   1.684 +	
   1.685 +	HBufC* sql16 = HBufC::New(size + 1);
   1.686 +	TEST(sql16 != NULL);
   1.687 +	TPtr16 ptr16 = sql16->Des();
   1.688 +	ptr16.Copy(ptr);
   1.689 +	ptr16.Append(TChar(0));
   1.690 +
   1.691 +	delete [] sql;
   1.692 +	file.Close();
   1.693 +	return sql16;
   1.694 +	}
   1.695 +
   1.696 +//Prints file size in bytes
   1.697 +static void PrintFileSize(const TDesC& aPath)
   1.698 +	{
   1.699 +	RFile file;
   1.700 +	TEST2(file.Open(TheFs, aPath, EFileRead), KErrNone);
   1.701 +	TInt size = 0;
   1.702 +	TEST2(file.Size(size), KErrNone);
   1.703 +	file.Close();
   1.704 +	RDebug::Print(_L("File \"%S\", size: %d\r\n"), &aPath, size);
   1.705 +	}
   1.706 +
   1.707 +//Executes 8-bit SQL statement	
   1.708 +static void ExecSql(sqlite3* aDbHandle, const char* aSqlStmt, const TDesC& aMsg)
   1.709 +	{
   1.710 +	TEST(aDbHandle != NULL);
   1.711 +	TEST(aSqlStmt != NULL);
   1.712 +	
   1.713 +	char* errMsg = NULL;
   1.714 +	TTime t1;
   1.715 +	t1.UniversalTime();
   1.716 +	TInt rc = sqlite3_exec(aDbHandle, aSqlStmt, callback, 0, &errMsg);
   1.717 +	if(rc != SQLITE_OK)
   1.718 +		{
   1.719 +		TPtrC8 ptr8((const TUint8*)errMsg, strlen(errMsg));
   1.720 +		TheBuf1.Copy(ptr8);
   1.721 +		RDebug::Print(_L("'sqlite3_exec()' failed, err %d, error msg: \"%S\"\t\n"), rc, &TheBuf1);
   1.722 +		TEST(0);
   1.723 +		}
   1.724 +	TTime t2;
   1.725 +	t2.UniversalTime();
   1.726 +	TTimeIntervalMicroSeconds diffTime = t2.MicroSecondsFrom(t1); 
   1.727 +	diffTime = diffTime.Int64() / 1000;
   1.728 +	RDebug::Print(_L("%S, time: %d ms\r\n"), &aMsg, (TInt)diffTime.Int64());
   1.729 +	}
   1.730 +
   1.731 +//This function searches aString argument for ';' occurences.
   1.732 +//Every time when it finds a ';' character, the function places a 0 right after the ';' and
   1.733 +//tests the just created, zero-terminated substring if it is a comlpete SQL statement.
   1.734 +//If it is a SQL statement, the function returns it and modifies aString argument to point right after the found
   1.735 +//SQL string. If it is not SQL statement, the function will continue the searching.
   1.736 +//If there is no ';' inside aString argument, the function returns the same string as a return result and
   1.737 +//modifies aString argument - sets it to TPtr(NULL, 0, 0).
   1.738 +//
   1.739 +//The function expects aString argument to be zero-terminated.
   1.740 +static TPtrC GetFirstSqlStmt(TPtr& aString)
   1.741 +	{
   1.742 +	const TChar KDelimitier(';');
   1.743 +	TPtr str(const_cast <TUint16*> (aString.Ptr()), aString.Length(), aString.Length());
   1.744 +	TInt afterDelimitierPos = 0;
   1.745 +	TInt pos;
   1.746 +	while((pos = str.Locate(KDelimitier) + 1) > 0 && pos < str.Length())
   1.747 +		{
   1.748 +		//There is a possibility that the string which terminates with the found ';' is SQL statement.
   1.749 +		//Zero terminate the string placing a zero right after ';' character and test it using sqlite3_complete16()
   1.750 +		//call. If it is not SQL string, restore the original character and continue searching.
   1.751 +		afterDelimitierPos += pos;
   1.752 +		TChar ch = aString[afterDelimitierPos];
   1.753 +		aString[afterDelimitierPos] = 0;
   1.754 +		TInt res = sqlite3_complete16(aString.Ptr());
   1.755 +		aString[afterDelimitierPos] = ch;
   1.756 +		if(res)
   1.757 +			{
   1.758 +			str.Set(const_cast <TUint16*> (aString.Ptr()), afterDelimitierPos, afterDelimitierPos);	
   1.759 +			//Replace the found ';' character with 0.
   1.760 +			str[afterDelimitierPos - 1] = 0;
   1.761 +			aString.Set(const_cast <TUint16*> (aString.Ptr()) + afterDelimitierPos, aString.Length() - afterDelimitierPos, aString.Length() - afterDelimitierPos);
   1.762 +			return str;
   1.763 +			}
   1.764 +		str.Set(const_cast <TUint16*> (str.Ptr()) + pos, str.Length() - pos, str.Length() - pos);	
   1.765 +		}
   1.766 +	//aString argument does not contain valid SQL statement or there is no ';' character inside aString.
   1.767 +	//Set aString to TPtr(NULL, 0, 0) and return the original string.
   1.768 +	aString.Set(NULL, 0, 0);
   1.769 +	return str;
   1.770 +	}
   1.771 +	
   1.772 +//Executes 16-bit SQL statement	
   1.773 +static void ExecSql16(sqlite3* aDbHandle, TDes& aSqlStmtZ, const TDesC& aMsg)
   1.774 +	{
   1.775 +	TEST(aDbHandle != NULL);
   1.776 +		
   1.777 +	TTime t1;
   1.778 +	t1.UniversalTime();
   1.779 +	
   1.780 +	TPtr sqlLeftZ(const_cast <TUint16*> (aSqlStmtZ.Ptr()), aSqlStmtZ.Length(), aSqlStmtZ.Length());
   1.781 +	while(sqlLeftZ.Length() > 0)
   1.782 +		{
   1.783 +		TPtrC sql = GetFirstSqlStmt(sqlLeftZ);
   1.784 +		sqlite3_stmt* stmtHandle = NULL;
   1.785 +		const void* stmtTailZ = NULL;
   1.786 +		TInt err = sqlite3_prepare16_v2(aDbHandle, sql.Ptr(), -1, &stmtHandle, &stmtTailZ);
   1.787 +		__ASSERT_ALWAYS(!stmtTailZ || User::StringLength((const TUint16*)stmtTailZ) == 0, User::Invariant());
   1.788 +		if(stmtHandle)
   1.789 +			{
   1.790 +			if(err == SQLITE_OK)
   1.791 +				{
   1.792 +				while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
   1.793 +					{
   1.794 +					}
   1.795 +				}
   1.796 +			sqlite3_finalize(stmtHandle);
   1.797 +			}
   1.798 +		if(err != SQLITE_DONE && err != SQLITE_OK)
   1.799 +			{
   1.800 +			const void* errMsgZ = sqlite3_errmsg16(aDbHandle);
   1.801 +			TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
   1.802 +			RDebug::Print(_L("'sqlite3_exec16()' failed, err %d, error msg: \"%S\"\t\n"), err, &msg);
   1.803 +			TEST(0);
   1.804 +			}
   1.805 +		}
   1.806 +	TTime t2;
   1.807 +	t2.UniversalTime();
   1.808 +	TTimeIntervalMicroSeconds diffTime = t2.MicroSecondsFrom(t1); 
   1.809 +	diffTime = diffTime.Int64() / 1000;
   1.810 +	RDebug::Print(_L("%S, time: %d ms\r\n"), &aMsg, (TInt)diffTime.Int64());
   1.811 +	}
   1.812 +
   1.813 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.814 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.815 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.816 +//////////////              "Search" test case                        ////////////////////////////
   1.817 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.818 +
   1.819 +struct TPnName
   1.820 +	{
   1.821 +	TPnName(const TDesC8& aFirstName, const TDesC8& aSurname) :
   1.822 +		iFirstName(aFirstName),
   1.823 +		iSurname(aSurname)
   1.824 +		{
   1.825 +		}
   1.826 +	const TPtrC8 iFirstName;
   1.827 +	const TPtrC8 iSurname;
   1.828 +	};
   1.829 +
   1.830 +const TInt KNamesCnt = 100;
   1.831 +	
   1.832 +const TPnName KNames[KNamesCnt] = 
   1.833 +	{
   1.834 +		TPnName(_L8("Kauh"), _L8("Mollel")),
   1.835 +		TPnName(_L8("Be"), _L8("Balcalertthawnd")),
   1.836 +		TPnName(_L8("Joba"), _L8("Hah")),
   1.837 +		TPnName(_L8("Mal"), _L8("Sinianna")),
   1.838 +		TPnName(_L8("Alip"), _L8("Hanittrinke")),
   1.839 +		TPnName(_L8("Ris"), _L8("Aba")),
   1.840 +		TPnName(_L8("Nirindrilo"), _L8("Oangah")),
   1.841 +		TPnName(_L8("An"), _L8("Mck")),
   1.842 +		TPnName(_L8("Ris"), _L8("Jat")),
   1.843 +		TPnName(_L8("Ja"), _L8("R")),
   1.844 +		TPnName(_L8("Pary"), _L8("Sawngethwnes")),
   1.845 +		TPnName(_L8("Main"), _L8("Stonstc")),
   1.846 +		TPnName(_L8("Joldan"), _L8("Misonialonss")),
   1.847 +		TPnName(_L8("Ja"), _L8("Beetth")),
   1.848 +		TPnName(_L8("An"), _L8("Magill")),
   1.849 +		TPnName(_L8("Ste"), _L8("Hakegstolbebilance")),
   1.850 +		TPnName(_L8("Laelefattal"), _L8("Bume")),
   1.851 +		TPnName(_L8("Anortoausl"), _L8("Kenoonssssoninals")),
   1.852 +		TPnName(_L8("Sthnill"), _L8("Huere")),
   1.853 +		TPnName(_L8("Elatandy"), _L8("Miadhelbi")),
   1.854 +		TPnName(_L8("Nevieohageridik"), _L8("Baronirgeriallyemo")),
   1.855 +		TPnName(_L8("Dertrry"), _L8("Miches")),
   1.856 +		TPnName(_L8("Tan-"), _L8("Sonagutlly")),
   1.857 +		TPnName(_L8("Mazianer"), _L8("Wi")),
   1.858 +		TPnName(_L8("Kesadrin"), _L8("Swixohar")),
   1.859 +		TPnName(_L8("Juhnn"), _L8("Vezuins")),
   1.860 +		TPnName(_L8("Geri"), _L8("Okun-Mamar")),
   1.861 +		TPnName(_L8("Jol"), _L8("Hadir")),
   1.862 +		TPnName(_L8("Lon"), _L8("Fonernginire")),
   1.863 +		TPnName(_L8("Brrk"), _L8("El")),
   1.864 +		TPnName(_L8("So"), _L8("Thanas")),
   1.865 +		TPnName(_L8("Timon"), _L8("Matarol")),
   1.866 +		TPnName(_L8("Clicartif"), _L8("Sandhinth")),
   1.867 +		TPnName(_L8("Dan"), _L8("Brl")),
   1.868 +		TPnName(_L8("An"), _L8("Danss")),
   1.869 +		TPnName(_L8("Y"), _L8("Gianstes")),
   1.870 +		TPnName(_L8("Gralilas"), _L8("Beny")),
   1.871 +		TPnName(_L8("Vamean"), _L8("Matesstel")),
   1.872 +		TPnName(_L8("Ch"), _L8("Inrinez")),
   1.873 +		TPnName(_L8("Ra"), _L8("Lusieing")),
   1.874 +		TPnName(_L8("Gerik"), _L8("Mawoshar")),
   1.875 +		TPnName(_L8("Nobrd"), _L8("Kerokilirtsoug")),
   1.876 +		TPnName(_L8("Norichnik"), _L8("Balmo")),
   1.877 +		TPnName(_L8("Anddra"), _L8("Fit")),
   1.878 +		TPnName(_L8("Maily"), _L8("Tanyerohetsphinbr")),
   1.879 +		TPnName(_L8("Frsa"), _L8("Huntorrenerkh")),
   1.880 +		TPnName(_L8("Gi"), _L8("Spandaveees")),
   1.881 +		TPnName(_L8("Jollminenipaninderal"), _L8("Vartzury")),
   1.882 +		TPnName(_L8("Ankshr"), _L8("Terawloleral")),
   1.883 +		TPnName(_L8("An"), _L8("La")),
   1.884 +		TPnName(_L8("Ma"), _L8("Brnd")),
   1.885 +		TPnName(_L8("Sonerdalmon"), _L8("Bo")),
   1.886 +		TPnName(_L8("Nis"), _L8("Tapeworrt")),
   1.887 +		TPnName(_L8("Shand"), _L8("Hacllik")),
   1.888 +		TPnName(_L8("San"), _L8("Sh")),
   1.889 +		TPnName(_L8("Mico"), _L8("Javiaros")),
   1.890 +		TPnName(_L8("Hub"), _L8("Warey")),
   1.891 +		TPnName(_L8("Mambew"), _L8("Maw")),
   1.892 +		TPnName(_L8("Honik"), _L8("Fantscerstetoringu")),
   1.893 +		TPnName(_L8("Da"), _L8("Saneelur")),
   1.894 +		TPnName(_L8("Aberecalahayondorttelin"), _L8("Futtesesoxok")),
   1.895 +		TPnName(_L8("Dor"), _L8("Lelek")),
   1.896 +		TPnName(_L8("Matin"), _L8("Fure")),
   1.897 +		TPnName(_L8("Niasietolf"), _L8("Jonones")),
   1.898 +		TPnName(_L8("Das"), _L8("Hoeonds")),
   1.899 +		TPnName(_L8("Anchn"), _L8("Svss")),
   1.900 +		TPnName(_L8("Dor"), _L8("Bolunatrk")),
   1.901 +		TPnName(_L8("Casah"), _L8("Brilllundonsssoug")),
   1.902 +		TPnName(_L8("Iapew"), _L8("Bagukak")),
   1.903 +		TPnName(_L8("Lieni"), _L8("MoncNicel")),
   1.904 +		TPnName(_L8("Adewalyary"), _L8("Buradesorobbrerans")),
   1.905 +		TPnName(_L8("Tos"), _L8("Gis")),
   1.906 +		TPnName(_L8("Vi"), _L8("Berk")),
   1.907 +		TPnName(_L8("Jorya"), _L8("upmarone")),
   1.908 +		TPnName(_L8("Iatew"), _L8("Hend")),
   1.909 +		TPnName(_L8("Liag"), _L8("Brsmall")),
   1.910 +		TPnName(_L8("Al"), _L8("Spahay")),
   1.911 +		TPnName(_L8("El"), _L8("Sy")),
   1.912 +		TPnName(_L8("Pary"), _L8("Trl")),
   1.913 +		TPnName(_L8("Br"), _L8("Usouroneis")),
   1.914 +		TPnName(_L8("Sirnilly"), _L8("Olay")),
   1.915 +		TPnName(_L8("Fell"), _L8("Bouphies")),
   1.916 +		TPnName(_L8("Man"), _L8("Haz")),
   1.917 +		TPnName(_L8("Dare"), _L8("Was")),
   1.918 +		TPnName(_L8("Fahnahopephrtex"), _L8("Gat")),
   1.919 +		TPnName(_L8("Har"), _L8("Handfffebinneickiasse")),
   1.920 +		TPnName(_L8("Gerlai"), _L8("Boravirg")),
   1.921 +		TPnName(_L8("Miss"), _L8("Us")),
   1.922 +		TPnName(_L8("Caushatattoatot"), _L8("Wes")),
   1.923 +		TPnName(_L8("Eizicay"), _L8("Gunbss")),
   1.924 +		TPnName(_L8("Pan"), _L8("Hilesertatickesobss-")),
   1.925 +		TPnName(_L8("Anaw"), _L8("Mangar")),
   1.926 +		TPnName(_L8("Korba"), _L8("Siansolan")),
   1.927 +		TPnName(_L8("Darl"), _L8("Haginijelso")),
   1.928 +		TPnName(_L8("Ral"), _L8("Veddddkisocackeluisowowone")),
   1.929 +		TPnName(_L8("La"), _L8("Wawethl")),
   1.930 +		TPnName(_L8("Y"), _L8("Wisonkend")),
   1.931 +		TPnName(_L8("Evimiat"), _L8("JondepssooncClille")),
   1.932 +		TPnName(_L8("Rin"), _L8("DulatoliacKark")),
   1.933 +		TPnName(_L8("Shegeiew"), _L8("Ass"))
   1.934 +	};
   1.935 +	
   1.936 +/////////////////////////////////////////////////////////////
   1.937 +//  16-bit strings
   1.938 +/////////////////////////////////////////////////////////////
   1.939 +
   1.940 +static TInt CmpF16(void*, TInt size1, const void* p1, TInt size2, const void* p2)
   1.941 +	{
   1.942 +	++TheCmpFCallCnt;
   1.943 +	
   1.944 +	TPtrC16 ptr1((TUint16*)p1, size1/2);
   1.945 +	TPtrC16 ptr2((TUint16*)p2, size2/2);
   1.946 +	
   1.947 +	TInt res = ptr1.CompareF(ptr2);
   1.948 +	return res;		
   1.949 +	}
   1.950 +
   1.951 +static TInt CmpC16(void*, TInt size1, const void* p1, TInt size2, const void* p2)
   1.952 +	{
   1.953 +	++TheCmpCCallCnt;
   1.954 +	
   1.955 +	TPtrC16 ptr1((TUint16*)p1, size1/2);
   1.956 +	TPtrC16 ptr2((TUint16*)p2, size2/2);
   1.957 +	TInt res = ptr1.CompareC(ptr2);
   1.958 +	return res;		
   1.959 +	}
   1.960 +
   1.961 +static void SearchDbTest16(const TDesC& aDbFilePath)
   1.962 +	{
   1.963 +	TheFs.Delete(aDbFilePath);
   1.964 +	
   1.965 +	TBuf<KMaxFileName + 1> fname;
   1.966 +	fname.Copy(aDbFilePath);
   1.967 +	//Open database
   1.968 +	RDebug::Print(_L("Open database\r\n"));
   1.969 +	sqlite3 *dbHandle = NULL;
   1.970 +	TInt rc = sqlite3_open16(fname.PtrZ(), &dbHandle);//!!!!16-bit encoding!!!!!
   1.971 +	if(rc != SQLITE_OK)
   1.972 +		{
   1.973 +		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
   1.974 +		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
   1.975 +		RDebug::Print(_L("'sqlite3_open()' failed, file %S, err %d, error msg: \"%S\"\t\n"), &aDbFilePath, rc, &msg);
   1.976 +		TEST(0);
   1.977 +		}
   1.978 +	TEST(dbHandle != NULL);
   1.979 +	//Create "CompareF" collation	
   1.980 +	RDebug::Print(_L("Create \"CompareF\" collation\r\n"));
   1.981 +	_LIT(KCmpF, "CmpF16\x0");
   1.982 +	rc = sqlite3_create_collation16(dbHandle, (const char*)(KCmpF().Ptr()), SQLITE_UTF16 | SQLITE_UTF16_ALIGNED, NULL, &CmpF16);
   1.983 +  	if(rc)
   1.984 +  		{
   1.985 +		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
   1.986 +		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
   1.987 +		RDebug::Print(_L("Err=%S\r\n"), &msg);
   1.988 +		TEST(0);
   1.989 +  		}
   1.990 +	//Create "CompareC" collation	
   1.991 +	RDebug::Print(_L("Create \"CompareC\" collation\r\n"));
   1.992 +	_LIT(KCmpC, "CmpC16\x0");
   1.993 +	rc = sqlite3_create_collation16(dbHandle, (const char*)(KCmpC().Ptr()), SQLITE_UTF16 | SQLITE_UTF16_ALIGNED, NULL, &CmpC16);
   1.994 +  	if(rc)
   1.995 +  		{
   1.996 +		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
   1.997 +		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
   1.998 +		RDebug::Print(_L("Err=%S\r\n"), &msg);
   1.999 +		TEST(0);
  1.1000 +  		}
  1.1001 +	//Create database schema
  1.1002 +	TheCmpFCallCnt = TheCmpCCallCnt = 0;
  1.1003 +	RDebug::Print(_L("Create database schema\r\n"));
  1.1004 +	HBufC16* createSqlZ = ReadSQL16(_L("z:\\test\\contacts_schema_to_vendors.sql"));
  1.1005 +	TPtr sql = createSqlZ->Des();
  1.1006 +	ExecSql16(dbHandle, sql, _L("Create schema"));
  1.1007 +	delete createSqlZ;
  1.1008 +	RDebug::Print(_L("CmpF() call cnt %d, CmpC() call cnt %d\r\n"), TheCmpFCallCnt, TheCmpCCallCnt);
  1.1009 +	//Add 1001 "simple" contacts
  1.1010 +	TheCmpFCallCnt = TheCmpCCallCnt = 0;
  1.1011 +	RDebug::Print(_L("Add 1001 \"simple\" contacts\r\n"));
  1.1012 +	HBufC16* addSqlZ = ReadSQL16(KSimpleContactsSqlFile);
  1.1013 +	RDebug::Print(_L("--\r\n"));
  1.1014 +	sql.Set(addSqlZ->Des());
  1.1015 +	ExecSql16(dbHandle, sql, _L("Add simple contacts"));
  1.1016 +	delete addSqlZ;
  1.1017 +	RDebug::Print(_L("CmpF() call cnt %d, CmpC() call cnt %d\r\n"), TheCmpFCallCnt, TheCmpCCallCnt);
  1.1018 +	//Print the number of records
  1.1019 +	RDebug::Print(_L("Print the number of records\r\n"));
  1.1020 +	TBuf<40> testSql(_L("SELECT COUNT(*) FROM CONTACTS"));
  1.1021 +	testSql.Append(TChar(0));
  1.1022 +	ExecSql16(dbHandle, testSql, _L("--"));
  1.1023 +	
  1.1024 +	//Create index: "First name, Last name"
  1.1025 +	TheCmpFCallCnt = TheCmpCCallCnt = 0;
  1.1026 +	RDebug::Print(_L("Create index: \"First name, Last name\"\r\n"));
  1.1027 +	TBuf<100> createIndexStmt(_L("CREATE INDEX Idx1 ON identitytable(cm_firstname COLLATE CmpC16, cm_lastname COLLATE CmpC16)"));
  1.1028 +	createIndexStmt.Append(TChar(0));
  1.1029 +	ExecSql16(dbHandle, createIndexStmt, _L("Create index"));
  1.1030 +	RDebug::Print(_L("CmpF() call cnt %d, CmpC() call cnt %d\r\n"), TheCmpFCallCnt, TheCmpCCallCnt);
  1.1031 +
  1.1032 +	RDebug::Print(_L("Close database\r\n"));
  1.1033 +   	sqlite3_close(dbHandle);
  1.1034 +
  1.1035 +	PrintFileSize(aDbFilePath);
  1.1036 +	}
  1.1037 +
  1.1038 +/////////////////////////////////////////////////////////////
  1.1039 +//  8-bit strings
  1.1040 +/////////////////////////////////////////////////////////////
  1.1041 +
  1.1042 +static TInt CmpF8(void*, TInt size1, const void* p1, TInt size2, const void* p2)
  1.1043 +	{
  1.1044 +	++TheCmpFCallCnt;
  1.1045 +	TPtrC8 ptr1((TUint8*)p1, size1);
  1.1046 +	TPtrC8 ptr2((TUint8*)p2, size2);
  1.1047 +	
  1.1048 +	TInt res = ptr1.CompareF(ptr2);
  1.1049 +	return res;		
  1.1050 +	}
  1.1051 +
  1.1052 +static TInt CmpC8(void*, TInt size1, const void* p1, TInt size2, const void* p2)
  1.1053 +	{
  1.1054 +	TPtrC8 ptr1((TUint8*)p1, size1);
  1.1055 +	TPtrC8 ptr2((TUint8*)p2, size2);
  1.1056 +	TInt res = ptr1.CompareC(ptr2);
  1.1057 +	return res;		
  1.1058 +	}
  1.1059 +
  1.1060 +static void SearchDbTest8(const TDesC& aDbFilePath, const TDesC& aAddContactsFile, const TDesC& aMsg)
  1.1061 +	{
  1.1062 +	TheFs.Delete(aDbFilePath);
  1.1063 +	
  1.1064 +	TBuf8<KMaxFileName + 1> fname;
  1.1065 +	fname.Copy(aDbFilePath);
  1.1066 +	
  1.1067 +	RDebug::Print(_L("%S\r\n"), &aMsg);
  1.1068 +	RDebug::Print(_L("Open database\r\n"));
  1.1069 +	sqlite3 *dbHandle = NULL;
  1.1070 +	TInt rc = sqlite3_open((const char*)fname.PtrZ(), &dbHandle);
  1.1071 +	if(rc != SQLITE_OK)
  1.1072 +		{
  1.1073 +		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
  1.1074 +		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
  1.1075 +		RDebug::Print(_L("'sqlite3_open()' failed, file %S, err %d, error msg: \"%S\"\r\n"), &aDbFilePath, rc, &msg);
  1.1076 +		TEST(0);
  1.1077 +		}
  1.1078 +	TEST(dbHandle != NULL);
  1.1079 +	
  1.1080 +	RDebug::Print(_L("Create 'CompareF' collation\r\n"));
  1.1081 +	_LIT8(KCmpF, "CmpF8\x0");
  1.1082 +	rc = sqlite3_create_collation(dbHandle, (const char*)(KCmpF().Ptr()), SQLITE_UTF8, NULL, &CmpF8);
  1.1083 +  	if(rc)
  1.1084 +  		{
  1.1085 +		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
  1.1086 +		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
  1.1087 +		RDebug::Print(_L("'sqlite3_create_collation()' failed, file %S, err %d, error msg: \"%S\"\r\n"), &aDbFilePath, rc, &msg);
  1.1088 +		TEST(0);
  1.1089 +  		}
  1.1090 +
  1.1091 +	RDebug::Print(_L("Create 'CompareC' collation\r\n"));
  1.1092 +	_LIT8(KCmpC, "CmpC8\x0");
  1.1093 +	rc = sqlite3_create_collation(dbHandle, (const char*)(KCmpC().Ptr()), SQLITE_UTF8, NULL, &CmpC8);
  1.1094 +  	if(rc)
  1.1095 +  		{
  1.1096 +		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
  1.1097 +		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
  1.1098 +		RDebug::Print(_L("'sqlite3_create_collation()' failed, file %S, err %d, error msg: \"%S\"\r\n"), &aDbFilePath, rc, &msg);
  1.1099 +		TEST(0);
  1.1100 +  		}
  1.1101 +
  1.1102 +	RDebug::Print(_L("Create database schema\r\n"));
  1.1103 +	char* createSqlZ = ReadSQL2(_L("z:\\test\\contacts_schema_to_vendors.sql"));
  1.1104 +	ExecSql(dbHandle, createSqlZ, _L("Create schema"));
  1.1105 +	delete [] createSqlZ;
  1.1106 +
  1.1107 +	RDebug::Print(_L("Add 1001 contacts\r\n"));
  1.1108 +	char* addSqlZ = ReadSQL2(aAddContactsFile);
  1.1109 +	ExecSql(dbHandle, addSqlZ, _L("Add contacts"));
  1.1110 +	delete [] addSqlZ;
  1.1111 +
  1.1112 +	RDebug::Print(_L("Print the number of records\r\n"));
  1.1113 +	const char testSql[] = {"SELECT COUNT(*) FROM CONTACTS"};
  1.1114 +	ExecSql(dbHandle, testSql, _L("SELECT COUNT(*)"));
  1.1115 +	
  1.1116 +	RDebug::Print(_L("Create index (using 'CompareF' collation): 'FirstName, Surname'\r\n"));
  1.1117 +	_LIT8(KCreateIndexStmt, "CREATE INDEX Idx1 ON identitytable(cm_firstname COLLATE CmpF8, cm_lastname COLLATE CmpF8)\x0");
  1.1118 +	ExecSql(dbHandle, (const char*)(KCreateIndexStmt().Ptr()), _L("Create index"));
  1.1119 +	RDebug::Print(_L("CompareF() called %d times\r\n"), TheCmpFCallCnt);
  1.1120 +
  1.1121 +/* BEGIN OF - TEST CASE 1 "Select all contacts which first name begins with 'a' " */	
  1.1122 +
  1.1123 +	RDebug::Print(_L("Prepare 'Select all contacts where the first name begins with 'A'' SQL string\r\n"));
  1.1124 +	_LIT8(KSearchStmt, "SELECT cm_firstname, cm_lastname FROM identitytable WHERE cm_firstname LIKE 'A%'\x0");
  1.1125 +	sqlite3_stmt* stmtHandle = NULL;
  1.1126 +	const char* stmtTailZ = NULL;
  1.1127 +	TTime t1;
  1.1128 +	t1.UniversalTime();
  1.1129 +	rc = sqlite3_prepare_v2(dbHandle, (const char*)(KSearchStmt().Ptr()), -1, &stmtHandle, &stmtTailZ);
  1.1130 +	TEST2(rc, SQLITE_OK);
  1.1131 +	TEST(stmtHandle != NULL);
  1.1132 +	TEST(!stmtTailZ || strlen(stmtTailZ) == 0);
  1.1133 +	TTime t2;
  1.1134 +	t2.UniversalTime();
  1.1135 +	TTimeIntervalMicroSeconds diffTime = t2.MicroSecondsFrom(t1); 
  1.1136 +	diffTime = diffTime.Int64() / 1000;
  1.1137 +	TInt t = (TInt)diffTime.Int64();
  1.1138 +	RDebug::Print(_L("'Prepare SQL statement' time: %d ms\r\n"), t);
  1.1139 +
  1.1140 +	RDebug::Print(_L("Step the prepared SQL statement\r\n"));
  1.1141 +	TInt totalCmpFCnt = 0;
  1.1142 +	t1.UniversalTime();
  1.1143 +	TheCmpFCallCnt = 0;
  1.1144 +	TInt recordCnt = 0;
  1.1145 +	while((rc = sqlite3_step(stmtHandle)) == SQLITE_ROW)
  1.1146 +		{
  1.1147 +		//const TUint8* firstName = sqlite3_column_text(stmtHandle, 0);
  1.1148 +		//TPtrC8 p(firstName, strlen((const char*)firstName));
  1.1149 +		//TBuf<100> p1; p1.Copy(p);
  1.1150 +		//const TUint8* surname = sqlite3_column_text(stmtHandle, 1);
  1.1151 +		//p.Set(surname, strlen((const char*)surname));
  1.1152 +		//TBuf<100> p2; p2.Copy(p);
  1.1153 +		//RDebug::Print(_L("Found rec: %S, %S\r\n"), &p1, &p2);
  1.1154 +		++recordCnt;
  1.1155 +		}
  1.1156 +	totalCmpFCnt += TheCmpFCallCnt;
  1.1157 +	TEST(rc == SQLITE_OK || rc == SQLITE_DONE);
  1.1158 +	t2.UniversalTime();
  1.1159 +	diffTime = t2.MicroSecondsFrom(t1); 
  1.1160 +	diffTime = diffTime.Int64() / 1000;
  1.1161 +	t = (TInt)diffTime.Int64();
  1.1162 +	RDebug::Print(_L("'Stepping' time: %d ms, found records: %d\r\n"), t, recordCnt);
  1.1163 +	RDebug::Print(_L("Total 'search' ('CompareF' used) operations=%d\r\n"), totalCmpFCnt);
  1.1164 +
  1.1165 +	sqlite3_finalize(stmtHandle);
  1.1166 +	stmtHandle = NULL;
  1.1167 +
  1.1168 +/* END OF - TEST CASE 1 "Select all contacts which first name begins with 'a' " */	
  1.1169 +
  1.1170 +/*	BEGIN OF - TEST CASE 2 "Do 100 searches in 1001 contacts" */
  1.1171 +
  1.1172 +	RDebug::Print(_L("Prepare 'SELECT FirstName, Surname...' SQL string\r\n"));
  1.1173 +	_LIT8(KSearchStmt2, "SELECT cm_firstname, cm_lastname FROM identitytable WHERE cm_firstname = :Prm1 AND cm_lastname = :Prm2\x0");
  1.1174 +	stmtHandle = NULL;
  1.1175 +	stmtTailZ = NULL;
  1.1176 +	t1.UniversalTime();
  1.1177 +	rc = sqlite3_prepare_v2(dbHandle, (const char*)(KSearchStmt2().Ptr()), -1, &stmtHandle, &stmtTailZ);
  1.1178 +	TEST2(rc, SQLITE_OK);
  1.1179 +	TEST(stmtHandle != NULL);
  1.1180 +	TEST(!stmtTailZ || strlen(stmtTailZ) == 0);
  1.1181 +	t2.UniversalTime();
  1.1182 +	diffTime = t2.MicroSecondsFrom(t1); 
  1.1183 +	diffTime = diffTime.Int64() / 1000;
  1.1184 +	t = (TInt)diffTime.Int64();
  1.1185 +	RDebug::Print(_L("'Prepare SQL statement' time: %d ms\r\n"), t);
  1.1186 +	
  1.1187 +	TInt idxPrm1 = sqlite3_bind_parameter_index(stmtHandle, ":Prm1");
  1.1188 +	TEST(idxPrm1 > 0);
  1.1189 +	TInt idxPrm2 = sqlite3_bind_parameter_index(stmtHandle, ":Prm2");
  1.1190 +	TEST(idxPrm2 > 0);
  1.1191 +	
  1.1192 +	RDebug::Print(_L("Do %d searches using the prepared SQL statement\r\n"), KNamesCnt);
  1.1193 +	totalCmpFCnt = 0;
  1.1194 +	t1.UniversalTime();
  1.1195 +	for(TInt i=0;i<KNamesCnt;++i)
  1.1196 +		{
  1.1197 +		const TDesC8& firstName = KNames[i].iFirstName;
  1.1198 +		rc = sqlite3_bind_text(stmtHandle, idxPrm1, (const char*)firstName.Ptr(), firstName.Length(), SQLITE_STATIC);
  1.1199 +		TEST2(rc, SQLITE_OK);
  1.1200 +		
  1.1201 +		const TDesC8& surname = KNames[i].iSurname;
  1.1202 +		rc = sqlite3_bind_text(stmtHandle, idxPrm2, (const char*)surname.Ptr(), surname.Length(), SQLITE_STATIC);
  1.1203 +		TEST2(rc, SQLITE_OK);
  1.1204 +		
  1.1205 +		TheCmpFCallCnt = 0;
  1.1206 +		TInt recordCnt = 0;
  1.1207 +		while((rc = sqlite3_step(stmtHandle)) == SQLITE_ROW)
  1.1208 +			{
  1.1209 +			++recordCnt;
  1.1210 +			}
  1.1211 +		totalCmpFCnt += TheCmpFCallCnt;
  1.1212 +		TEST(recordCnt == 1);
  1.1213 +		rc = sqlite3_reset(stmtHandle);
  1.1214 +		TEST2(rc, SQLITE_OK);
  1.1215 +		}
  1.1216 +	t2.UniversalTime();
  1.1217 +	diffTime = t2.MicroSecondsFrom(t1); 
  1.1218 +	diffTime = diffTime.Int64() / 1000;
  1.1219 +	t = (TInt)diffTime.Int64();
  1.1220 +	RDebug::Print(_L("'Search' time: %d ms\r\n"), t);
  1.1221 +	RDebug::Print(_L("Total 'search' ('CompareF' used) operations=%d, average_per_iter=%d\r\n"), totalCmpFCnt, totalCmpFCnt/KNamesCnt);
  1.1222 +
  1.1223 +	sqlite3_finalize(stmtHandle);
  1.1224 +	stmtHandle = NULL;
  1.1225 +	
  1.1226 +/* END OF - TEST CASE 2 "Do 100 searches in 1001 contacts" */	
  1.1227 +
  1.1228 +	t1.UniversalTime();
  1.1229 +	sqlite3_finalize(stmtHandle);
  1.1230 +	t2.UniversalTime();
  1.1231 +	diffTime = t2.MicroSecondsFrom(t1); 
  1.1232 +	diffTime = diffTime.Int64() / 1000;
  1.1233 +	t = (TInt)diffTime.Int64();
  1.1234 +	RDebug::Print(_L("'Finalize SQL statement' time: %d ms\r\n"), t);
  1.1235 +
  1.1236 +   	sqlite3_close(dbHandle);
  1.1237 +	PrintFileSize(aDbFilePath);
  1.1238 +	}
  1.1239 +
  1.1240 +static void TwoDatabasesTest()
  1.1241 +	{
  1.1242 +	(void)sqlite3SymbianLastOsError();
  1.1243 +	sqlite3 *dbHandle1 = NULL;
  1.1244 +	TInt rc1 = sqlite3_open(KDbName1, &dbHandle1);
  1.1245 +	if(rc1 != SQLITE_OK)
  1.1246 +		{
  1.1247 +		rc1 = sqlite3SymbianLastOsError();
  1.1248 +		}
  1.1249 +
  1.1250 +	(void)sqlite3SymbianLastOsError();
  1.1251 +	sqlite3 *dbHandle2 = NULL;
  1.1252 +	TInt rc2 = sqlite3_open(KDbName1, &dbHandle2);
  1.1253 +	if(rc2 != SQLITE_OK)
  1.1254 +		{
  1.1255 +		rc2 = sqlite3SymbianLastOsError();
  1.1256 +		}
  1.1257 +	
  1.1258 +   	sqlite3_close(dbHandle2);
  1.1259 +
  1.1260 +   	sqlite3_close(dbHandle1);
  1.1261 +	
  1.1262 +	(void)TheFs.Delete(ConvertToUtf16(KDbName1));
  1.1263 +	
  1.1264 +	TEST2(rc1, KErrNone);
  1.1265 +	TEST2(rc2, KErrInUse);//EFileRead | EFileWrite file open mode!
  1.1266 +	}
  1.1267 +
  1.1268 +static void QuickTest()
  1.1269 +	{
  1.1270 +	(void)TheFs.Delete(ConvertToUtf16(KDbName1));
  1.1271 +
  1.1272 +	sqlite3 *dbHandle1 = NULL;
  1.1273 +	TInt err = sqlite3_open(KDbName1, &dbHandle1);
  1.1274 +	TEST2(err, SQLITE_OK);
  1.1275 +
  1.1276 +	err = sqlite3_exec(dbHandle1, "CREATE TABLE A(Fld1 INTEGER, Fld2 TEXT);", 0, 0, 0);
  1.1277 +	TEST2(err, SQLITE_OK);
  1.1278 +
  1.1279 +	sqlite3_stmt* stmtHandle = NULL;
  1.1280 +	_LIT(KSql1, "INSERT INTO A(Fld1, Fld2) VALUES(1, :Val)\x0");
  1.1281 +	err = sqlite3_prepare16_v2(dbHandle1, KSql1().Ptr(), -1, &stmtHandle, NULL);
  1.1282 +	TEST2(err, SQLITE_OK);
  1.1283 +
  1.1284 +	_LIT(KPrmData, "U012");
  1.1285 +	err = sqlite3_bind_text16(stmtHandle, 1, (const void*)KPrmData().Ptr(), KPrmData().Length() * sizeof(TText), SQLITE_STATIC);
  1.1286 +	TEST2(err, SQLITE_OK);
  1.1287 +
  1.1288 +	while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
  1.1289 +		{
  1.1290 +		}
  1.1291 +	if(err == SQLITE_ERROR)	//It may be "out of memory" problem
  1.1292 +		{
  1.1293 +		err = sqlite3_reset(stmtHandle);
  1.1294 +		__ASSERT_ALWAYS(err != SQLITE_OK, User::Invariant());
  1.1295 +		}
  1.1296 +
  1.1297 +   	sqlite3_finalize(stmtHandle);
  1.1298 +   	
  1.1299 +	_LIT(KSql2, "SELECT * FROM A WHERE Fld1 = 1\x0");
  1.1300 +	err = sqlite3_prepare16_v2(dbHandle1, KSql2().Ptr(), -1, &stmtHandle, NULL);
  1.1301 +	TEST2(err, SQLITE_OK);
  1.1302 +
  1.1303 +	err = sqlite3_step(stmtHandle);
  1.1304 +	TEST2(err, SQLITE_ROW);
  1.1305 +
  1.1306 +	const void* data = sqlite3_column_text16(stmtHandle, 1);
  1.1307 +	UNUSED_VAR(data);
  1.1308 +	TInt charLength  = sqlite3_column_bytes16(stmtHandle, 1) / sizeof(TUint16);
  1.1309 +	UNUSED_VAR(charLength);
  1.1310 +   	
  1.1311 +   	sqlite3_finalize(stmtHandle);
  1.1312 +   	
  1.1313 +   	sqlite3_close(dbHandle1);
  1.1314 +
  1.1315 +	(void)TheFs.Delete(ConvertToUtf16(KDbName1));
  1.1316 +	}
  1.1317 +
  1.1318 +//////////////////////////////////////////////////////////////////////////////////////////////////
  1.1319 +
  1.1320 +static void TemdDbTest()
  1.1321 +	{
  1.1322 +	sqlite3 *dbHandle = NULL;
  1.1323 +	TInt rc = sqlite3_open(KDbName1, &dbHandle);
  1.1324 +	if(rc != SQLITE_OK)
  1.1325 +		{
  1.1326 +		const void* errMsgZ = sqlite3_errmsg16(dbHandle);
  1.1327 +		TPtrC msg(reinterpret_cast <const TText16*> (errMsgZ), wcslen(reinterpret_cast <const wchar_t*> (errMsgZ)));
  1.1328 +		RDebug::Print(_L("'sqlite3_open()' failed, err %d, error msg: \"%S\"\r\n"), rc, &msg);
  1.1329 +		TEST(0);
  1.1330 +		}
  1.1331 +	TEST(dbHandle != NULL);
  1.1332 +
  1.1333 +	char *zErrMsg = 0;
  1.1334 +
  1.1335 +	_LIT8(KSql1, "CREATE TEMP TABLE A(F1 INTEGER)\x0");
  1.1336 +	rc = sqlite3_exec(dbHandle, reinterpret_cast <const char*> (KSql1().Ptr()), callback, 0, &zErrMsg);
  1.1337 +	if(rc != SQLITE_OK)
  1.1338 +		{
  1.1339 +  		TPtrC p = ConvertToUtf16(zErrMsg);
  1.1340 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
  1.1341 +    	sqlite3_free(zErrMsg);
  1.1342 +    	TEST(0);
  1.1343 +		}
  1.1344 +
  1.1345 +	_LIT8(KSql2, "INSERT INTO A(F1) VALUES(2)\x0");
  1.1346 +	rc = sqlite3_exec(dbHandle, reinterpret_cast <const char*> (KSql2().Ptr()), callback, 0, &zErrMsg);
  1.1347 +	if(rc != SQLITE_OK)
  1.1348 +		{
  1.1349 +  		TPtrC p = ConvertToUtf16(zErrMsg);
  1.1350 +  		RDebug::Print(_L("SQL error %d, msg: %S\n"), rc, &p);
  1.1351 +    	sqlite3_free(zErrMsg);
  1.1352 +    	TEST(0);
  1.1353 +		}
  1.1354 +
  1.1355 +   	sqlite3_close(dbHandle);
  1.1356 +	(void)TheFs.Delete(ConvertToUtf16(KDbName1));
  1.1357 +	}
  1.1358 +
  1.1359 +//////////////////////////////////////////////////////////////////////////////////////////////////
  1.1360 +//////////////////////////////////////////////////////////////////////////////////////////////////
  1.1361 +//////////////////////////////////////////////////////////////////////////////////////////////////
  1.1362 +
  1.1363 +static void CreateTestDir()
  1.1364 +    {
  1.1365 +    RFs fs;
  1.1366 +	TInt err = fs.Connect();
  1.1367 +	TEST2(err, KErrNone);
  1.1368 +
  1.1369 +	err = fs.MkDir(KTestDir);
  1.1370 +	TEST(err == KErrNone || err == KErrAlreadyExists);
  1.1371 +	
  1.1372 +	fs.Close();
  1.1373 +	}
  1.1374 +
  1.1375 +static void DoTestsL()
  1.1376 +	{
  1.1377 +	TheTest.Next(_L("Attempt to open twice the same database file in the same thread"));
  1.1378 +	TwoDatabasesTest();
  1.1379 +
  1.1380 +	TheTest.Next(_L("TEMP database test"));
  1.1381 +	TemdDbTest();
  1.1382 +
  1.1383 +	TheTest.Next(_L("Quick test"));
  1.1384 +	QuickTest();
  1.1385 +
  1.1386 +	TheTest.Next(_L("SQLite tests 1"));
  1.1387 +	DoTests1();
  1.1388 +	
  1.1389 +	TheTest.Next(_L("SQLite tests 2"));
  1.1390 +	DoTests2();
  1.1391 +
  1.1392 +	TheTest.Next(_L("Accented column names"));
  1.1393 +	AccentedColumnNamesTestL();
  1.1394 +	
  1.1395 +	TFileName fname;
  1.1396 +	User::CommandLine(fname);
  1.1397 +	TParse parse;
  1.1398 +	parse.Set(fname, &KContactsFile, 0);
  1.1399 +	const TDesC& dbFilePath = parse.FullName();
  1.1400 +	
  1.1401 +	PrintConfig(dbFilePath);
  1.1402 +
  1.1403 +	TheTest.Next(_L("String searching tests. 16-bit strings. Simple contacts"));
  1.1404 +	SearchDbTest16(dbFilePath);
  1.1405 +
  1.1406 +	TheTest.Next(_L("String searching tests. 8-bit strings. Simple contacts"));
  1.1407 +	SearchDbTest8(dbFilePath, KSimpleContactsSqlFile, _L("Search simple contacts"));
  1.1408 +	}
  1.1409 +
  1.1410 +TInt E32Main()
  1.1411 +	{
  1.1412 +	TheTest.Title();
  1.1413 +	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-LEGACY-T_SQLITE-0001 SQLite tests "));
  1.1414 +	
  1.1415 +	TheTrapCleanup = CTrapCleanup::New ();
  1.1416 +	__ASSERT_ALWAYS(TheTrapCleanup != NULL, User::Invariant());
  1.1417 +
  1.1418 +	__UHEAP_MARK;
  1.1419 +
  1.1420 +	CreateTestDir();
  1.1421 +	
  1.1422 +	TheTest(TheFs.Connect() == KErrNone);
  1.1423 +	DeleteTestFiles();
  1.1424 +
  1.1425 +	CreatePrivateDirs();
  1.1426 +
  1.1427 +	//Init sqlite library
  1.1428 +	sqlite3SymbianLibInit();
  1.1429 +
  1.1430 +	TRAPD(err, DoTestsL());
  1.1431 +	
  1.1432 +	sqlite3SymbianLibFinalize();
  1.1433 +
  1.1434 +	CloseSTDLIB();
  1.1435 +	
  1.1436 +	DeleteTestFiles();
  1.1437 +	
  1.1438 +	TEST2(err, KErrNone);
  1.1439 +	
  1.1440 +	TheFs.Close();
  1.1441 +
  1.1442 +	__UHEAP_MARKEND;
  1.1443 +
  1.1444 +	delete TheTrapCleanup;
  1.1445 +
  1.1446 +
  1.1447 +	TheTest.End();
  1.1448 +	TheTest.Close();
  1.1449 +
  1.1450 +
  1.1451 +	return KErrNone;
  1.1452 +	}