persistentstorage/sql/TEST/t_sqlapi.cpp
changeset 0 08ec8eefde2f
child 8 fa9941cf3867
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include <bautils.h>
       
    18 #include <s32buf.h>				//MStreamBuf
       
    19 #include <sqldb.h>
       
    20 
       
    21 ///////////////////////////////////////////////////////////////////////////////////////
       
    22 
       
    23 RTest TheTest(_L("t_sqlapi test"));
       
    24 _LIT(KTestDir, "c:\\test\\");
       
    25 _LIT(KTestDbName1, "c:\\test\\t_sqlapi1.db");
       
    26 _LIT(KTestDbName2, "c:[1111CCCC]t_sqlapi2.db");
       
    27 _LIT(KTestDbName3, "C:\\TEST\\t_sqlapi3.db");
       
    28 _LIT(KTestDbName4, "C:[1111CCCC]D012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.db");
       
    29 _LIT(KTestDbName5, "C:\\TEST\\D012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.db");
       
    30 _LIT(KTestDbName6, "C:[1111CCCC]t_sqlapi6.db");
       
    31 _LIT(KTestDbName7, "C:[1111CCCC]t_sqlapi7.db");
       
    32 _LIT(KTestDbName8, "c:\\test\\t_sqlapi8.db");
       
    33 _LIT(KTestDbName9, "c:\\private\\1111CCCC\\t_sqlapi9.db");
       
    34 _LIT(KTestCfgDbName, "c:\\test\\t_sqlapi_cfg.db");
       
    35 _LIT(KTestCfgDbName2, "c:[1111CCCC]t_sqlapi_cfg.db");
       
    36 _LIT(KServerPrivateDir, "\\private\\10281e17\\");
       
    37 
       
    38 _LIT(KDbName7, "C:\\TEST\\t_sqlapi7_2.db");
       
    39 
       
    40 // used for the config test
       
    41 _LIT8(KServerConfigString1, "   ;  cache_size =  1024 ; page_size =1024 ;encoding =  \"UTF-8\"   ");
       
    42 _LIT8(KServerConfigString2, " badconfigstring ");
       
    43 _LIT8(KServerConfigString3, " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
       
    44 _LIT8(KServerConfigString4, "");
       
    45 
       
    46 const TUid KSecureUid = {0x1111CCCC};//The same as the UID in the MMP file
       
    47 
       
    48 _LIT(KCreateDbScript, "z:\\test\\contacts_schema_to_vendors.sql");
       
    49 _LIT(KFillDbScript, "z:\\test\\add_simple_contacts.sql");
       
    50 _LIT8(KCommitStr8, "COMMIT;");
       
    51 _LIT16(KCommitStr16, "COMMIT;");
       
    52 
       
    53 ///////////////////////////////////////////////////////////////////////////////////////
       
    54 
       
    55 void DeleteTestFiles()
       
    56 	{
       
    57 	RSqlDatabase::Delete(KTestDbName1);
       
    58 	RSqlDatabase::Delete(KTestDbName2);
       
    59 	RSqlDatabase::Delete(KTestDbName3);
       
    60 	RSqlDatabase::Delete(KTestDbName4);
       
    61 	RSqlDatabase::Delete(KTestDbName5);
       
    62 	RSqlDatabase::Delete(KTestDbName6);
       
    63 	RSqlDatabase::Delete(KTestDbName7);
       
    64 	RSqlDatabase::Delete(KTestDbName8);
       
    65 	RSqlDatabase::Delete(KTestDbName9);
       
    66 	RSqlDatabase::Delete(KTestCfgDbName);
       
    67 	RSqlDatabase::Delete(KTestCfgDbName2);
       
    68 	RSqlDatabase::Delete(KDbName7);
       
    69 	}
       
    70 
       
    71 ///////////////////////////////////////////////////////////////////////////////////////
       
    72 ///////////////////////////////////////////////////////////////////////////////////////
       
    73 //Test macros and functions
       
    74 void Check(TInt aValue, TInt aLine)
       
    75 	{
       
    76 	if(!aValue)
       
    77 		{
       
    78 		DeleteTestFiles();
       
    79 		TheTest(EFalse, aLine);
       
    80 		}
       
    81 	}
       
    82 void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    83 	{
       
    84 	if(aValue != aExpected)
       
    85 		{
       
    86 		DeleteTestFiles();
       
    87 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    88 		TheTest(EFalse, aLine);
       
    89 		}
       
    90 	}
       
    91 #define TEST(arg) ::Check((arg), __LINE__)
       
    92 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    93 
       
    94 ///////////////////////////////////////////////////////////////////////////////////////
       
    95 
       
    96 void CreateTestDir()
       
    97     {
       
    98     RFs fs;
       
    99 	TInt err = fs.Connect();
       
   100 	TEST2(err, KErrNone);
       
   101 
       
   102 	err = fs.MkDir(KTestDir);
       
   103 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
   104 
       
   105 	err = fs.CreatePrivatePath(EDriveC);	
       
   106 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
   107 	
       
   108 	fs.Close();
       
   109 	}
       
   110 
       
   111 ///////////////////////////////////////////////////////////////////////////////////////
       
   112 
       
   113 template <class DES, class BUF> void ExecSqlStmtOnDb(RSqlDatabase& aDb, const TDesC8& aSqlString, 
       
   114 												     TInt aExpectedError)
       
   115 	{
       
   116 	BUF sqlBuf;
       
   117 	sqlBuf.Copy(aSqlString);
       
   118 	
       
   119 	TInt rc = aDb.Exec(sqlBuf);
       
   120 	if(rc < 0 && SqlRetCodeClass(rc) == ESqlDbError)
       
   121 		{
       
   122 		TPtrC msg = aDb.LastErrorMessage();
       
   123 		RDebug::Print(_L("Execute SQL error - '%S'\r\n"), &msg);
       
   124 		}
       
   125 	if(aExpectedError < 0)
       
   126 		{
       
   127 		TEST2(rc, aExpectedError);
       
   128 		}
       
   129 	else
       
   130 		{
       
   131 		TEST(rc >= 0);
       
   132 		}
       
   133 	}
       
   134 
       
   135 template <class DES, class BUF>  RSqlStatement PrepareSqlStmt(RSqlDatabase& aDb, const TDesC8& aSqlString,
       
   136 															  TInt aExpectedError)
       
   137 	{
       
   138 	BUF sqlBuf;
       
   139 	sqlBuf.Copy(aSqlString);
       
   140 	
       
   141 	RSqlStatement stmt;
       
   142 	TInt rc = stmt.Prepare(aDb, sqlBuf);
       
   143 	if(rc != KErrNone && SqlRetCodeClass(rc) == ESqlDbError)
       
   144 		{
       
   145 		TPtrC msg = aDb.LastErrorMessage();
       
   146 		RDebug::Print(_L("Execute SQL error - '%S'\r\n"), &msg);
       
   147 		}
       
   148 	TEST2(rc, aExpectedError);
       
   149 	return stmt;
       
   150 	}
       
   151 
       
   152 void ExecSqlStmt(RSqlDatabase& aDb, RSqlStatement& aStmt, TInt aExpectedError)
       
   153 	{
       
   154 	TInt rc = aStmt.Exec();
       
   155 	if(rc < 0 && SqlRetCodeClass(rc) == ESqlDbError)
       
   156 		{
       
   157 		TPtrC msg = aDb.LastErrorMessage();
       
   158 		RDebug::Print(_L("Execute SQL error - '%S'\r\n"), &msg);
       
   159 		}
       
   160 	if(aExpectedError < 0)
       
   161 		{
       
   162 		TEST2(rc, aExpectedError);
       
   163 		}
       
   164 	else
       
   165 		{
       
   166 		TEST(rc >= 0);
       
   167 		}
       
   168 	}
       
   169 		
       
   170 ///////////////////////////////////////////////////////////////////////////////////////
       
   171 ///////////////////////////////////////////////////////////////////////////////////////
       
   172 
       
   173 /**
       
   174 @SYMTestCaseID			SYSLIB-SQL-CT-1601
       
   175 @SYMTestCaseDesc		Create/Open/Close database tests. Invalid database names, very long database names,
       
   176 						private databases, public databases, open twice database, create already
       
   177 						existing database, open non-exisitng database, open corrupted database.
       
   178 @SYMTestPriority		High
       
   179 @SYMTestActions			Tests for RSqlDatabase::Create(), RSqlDatabase::Open() methods.
       
   180 @SYMTestExpectedResults Test must not fail
       
   181 @SYMREQ					REQ5792
       
   182                         REQ5793
       
   183 */	
       
   184 void OpenCloseDatabaseTest()
       
   185 	{
       
   186 	RSqlDatabase db;
       
   187 	TSecurityPolicy defaultPolicy(TSecurityPolicy::EAlwaysPass);
       
   188 	RSqlSecurityPolicy securityPolicy;
       
   189 	TInt rc = securityPolicy.Create(defaultPolicy);
       
   190 	TEST2(rc, KErrNone);
       
   191 	
       
   192 	//Secure shared database file on a non-existing drive (A:)
       
   193 	_LIT(KDbPath1, "A:[1111CCCC]db1.db");
       
   194 	rc = db.Create(KDbPath1, securityPolicy);
       
   195 	TEST2(rc, KErrNotReady);
       
   196 	db.Close();
       
   197 
       
   198 	//Attempt to open a non-existing file.
       
   199 	_LIT(KDbFName, "c:\\test\\nofile.db");
       
   200 	rc = db.Open(KDbFName);
       
   201 	TEST2(rc, KErrNotFound);
       
   202 		
       
   203 	//Zero length database file name.
       
   204 	rc = db.Create(_L(""));
       
   205 	TEST2(rc,  KErrBadName);
       
   206 	db.Close();
       
   207 	
       
   208 	//Database file name containing only the drive name
       
   209 	rc = db.Create(_L("C:"));
       
   210 	TEST2(rc, KErrBadName);
       
   211 	db.Close();
       
   212 	
       
   213 	//Database file name containing only the path, without the file name
       
   214 	rc = db.Create(_L("C:\\TEST\\"));
       
   215 	TEST2(rc, KErrBadName);
       
   216 	db.Close();
       
   217 	
       
   218 	//Public shared database file on a non-existing drive (A:)	
       
   219 	_LIT(KDbPath2, "A:\\test\\db1.db");
       
   220 	rc = db.Create(KDbPath2);
       
   221 	TEST2(rc, KErrNotReady);
       
   222 	db.Close();
       
   223 
       
   224 	// create database with good config specified
       
   225 	rc = db.Create(KTestCfgDbName,&KServerConfigString1);
       
   226 	TEST2(rc, KErrNone);
       
   227 	db.Close();
       
   228 	TInt rc2 = RSqlDatabase::Delete(KTestCfgDbName);
       
   229 	TEST2(rc2, KErrNone);
       
   230 	
       
   231 	// create database with bad config specified
       
   232 	rc = db.Create(KTestCfgDbName,&KServerConfigString2);
       
   233 	TEST2(rc, KErrArgument);
       
   234 	db.Close();
       
   235 
       
   236 	// create database with long config specified
       
   237 	rc = db.Create(KTestCfgDbName,&KServerConfigString3);
       
   238 	TEST2(rc, KErrArgument);
       
   239 	db.Close();
       
   240 
       
   241 	// create database with empty config specified - not an error
       
   242 	rc = db.Create(KTestCfgDbName,&KServerConfigString4);
       
   243 	TEST2(rc, KErrNone);
       
   244 	db.Close();
       
   245 	rc2 = RSqlDatabase::Delete(KTestCfgDbName);
       
   246 	TEST2(rc2, KErrNone);
       
   247 		
       
   248 	//Secure shared database file on an existing drive (C:). 
       
   249 	//Very long database file name (> 90 characters) but still a valid name.
       
   250 	rc = db.Create(KTestDbName4, securityPolicy);
       
   251 	db.Close();
       
   252 	rc2 = RSqlDatabase::Delete(KTestDbName4);
       
   253 	TEST2(rc, KErrNone);
       
   254 	TEST2(rc2, KErrNone);
       
   255 
       
   256 	//Secure shared database file on an existing drive (C:). 
       
   257 	//Very long database file name (> 90 characters) but still a valid name.
       
   258 	//With config
       
   259 	rc = db.Create(KTestCfgDbName2, securityPolicy, &KServerConfigString1);
       
   260 	db.Close();
       
   261 	rc2 = RSqlDatabase::Delete(KTestCfgDbName2);
       
   262 	TEST2(rc, KErrNone);
       
   263 	TEST2(rc2, KErrNone);
       
   264 	
       
   265 	//Public shared database file on an existing drive (C:). 
       
   266 	//Very long database file name (> 90 characters) but still a valid name.
       
   267 	rc = db.Create(KTestDbName5);
       
   268 	db.Close();
       
   269 	rc2 = RSqlDatabase::Delete(KTestDbName5);
       
   270 	TEST2(rc, KErrNone);
       
   271 	TEST2(rc2, KErrNone);
       
   272 	
       
   273 	RFs fs;
       
   274 	TEST2(fs.Connect(), KErrNone);
       
   275 	TFileName privatePath;
       
   276 	TEST2(fs.PrivatePath(privatePath), KErrNone);
       
   277 	fs.Close();
       
   278 	
       
   279 	//Private shared database file on an existing drive (C:). 
       
   280 	//Very long database file name.
       
   281 	const TInt KMaxFileName2 = KMaxFileName - 40;//"-40" because the SQLITE engine creates a journal file if begins
       
   282 	                                             //a transaction. The name of the journal file is 
       
   283 	                                             //"<dbFileName>-journal.<ext>". It is obvious that if the 
       
   284 	                                             //database file name is too long but still valid and its creation
       
   285 	                                             //succeeds, the journal file creation may fail because the journal
       
   286 	                                             //file name becomes too long
       
   287 	TBuf<KMaxFileName2> dbPath;
       
   288 	_LIT(KExt, ".DB");
       
   289 	dbPath.Copy(_L("C:"));
       
   290 	dbPath.Append(KSecureUid.Name());
       
   291 	TInt len = KMaxFileName2 + 1 - (dbPath.Length() + KExt().Length() + privatePath.Length());
       
   292 	while(--len)
       
   293 		{
       
   294 		dbPath.Append(TChar('A'));	
       
   295 		}
       
   296 	dbPath.Append(KExt);	
       
   297 	TEST(dbPath.Length() == (KMaxFileName2 - privatePath.Length()));
       
   298 	rc = db.Create(dbPath, securityPolicy);
       
   299 	db.Close();
       
   300 	rc2 = RSqlDatabase::Delete(dbPath);
       
   301 	TEST2(rc, KErrNone);
       
   302 	TEST2(rc2, KErrNone);
       
   303 	
       
   304 	// Private database with config
       
   305 	TBuf<KMaxFileName> cfgPath;
       
   306 	cfgPath.Copy(_L("C:"));
       
   307 	cfgPath.Append(KSecureUid.Name());
       
   308 	cfgPath.Append(KExt);
       
   309 	rc = db.Create(cfgPath,securityPolicy,&KServerConfigString1);
       
   310 	db.Close();
       
   311 	rc2 = RSqlDatabase::Delete(cfgPath);
       
   312 	TEST2(rc, KErrNone);
       
   313 	TEST2(rc2, KErrNone);
       
   314 	
       
   315 	//Public shared database file on an existing drive (C:). 
       
   316 	//Very long database file name.
       
   317 	dbPath.Copy(_L("C:\\TEST\\D"));
       
   318 	len = KMaxFileName2 + 1 - (dbPath.Length() + KExt().Length());
       
   319 	while(--len)
       
   320 		{
       
   321 		dbPath.Append(TChar('A'));	
       
   322 		}
       
   323 	dbPath.Append(KExt);	
       
   324 	TEST(dbPath.Length() == KMaxFileName2);
       
   325 	rc = db.Create(dbPath);
       
   326 	db.Close();
       
   327 	rc2 = RSqlDatabase::Delete(dbPath);
       
   328 	TEST2(rc, KErrNone);
       
   329 	TEST2(rc2, KErrNone);
       
   330 
       
   331 	//Create/Close/Open/Close secure shared database test
       
   332 	rc = db.Create(KTestDbName6, securityPolicy);
       
   333 	db.Close();
       
   334 	rc2 = db.Open(KTestDbName6);
       
   335 	db.Close();
       
   336 	TInt rc3 = RSqlDatabase::Delete(KTestDbName6);
       
   337 	TEST2(rc, KErrNone);
       
   338 	TEST2(rc2, KErrNone);
       
   339 	TEST2(rc3, KErrNone);
       
   340 	
       
   341 	//An attempt to create already existing secure shared file.	
       
   342 	rc = db.Create(KTestDbName6, securityPolicy);
       
   343 	db.Close();
       
   344 	rc2 = db.Create(KTestDbName6, securityPolicy);
       
   345 	db.Close();
       
   346 	rc3 = RSqlDatabase::Delete(KTestDbName6);
       
   347 	TEST2(rc, KErrNone);
       
   348 	TEST2(rc2, KErrAlreadyExists);
       
   349 	TEST2(rc3, KErrNone);
       
   350 	
       
   351 	//An attempt to open twice the same database file using different RSqlDatabase objects
       
   352 	rc = db.Create(KTestDbName6, securityPolicy);
       
   353 	RSqlDatabase db2;	
       
   354 	rc2 = db2.Open(KTestDbName6);
       
   355 	db2.Close();
       
   356 	db.Close();
       
   357 	rc3 = RSqlDatabase::Delete(KTestDbName6);
       
   358 	TEST2(rc, KErrNone);
       
   359 	TEST2(rc2, KErrNone);//-- KErrInUse -- in case EFileRead | EFileWrite file open mode!
       
   360 	TEST2(rc3, KErrNone);
       
   361 
       
   362 	//An attempt to create secure shared database file on a read-only drive (Z:)
       
   363 	_LIT(KDbPath8, "Z:[1111CCCC]db1.db");
       
   364 	rc = db.Create(KDbPath8, securityPolicy);
       
   365 	TEST2(rc, KErrAccessDenied);
       
   366 	db.Close();
       
   367 
       
   368 	//An attempt to create non-secure shared database file on a read-only drive (Z:)
       
   369 	_LIT(KDbPath8a, "Z:\\db1.db");
       
   370 	rc = db.Create(KDbPath8a);
       
   371 	TEST2(rc, KErrAccessDenied);
       
   372 	db.Close();
       
   373 
       
   374 	//An attempt to open non-existing secure shared database file on a read-only drive (Z:)
       
   375 	rc = db.Open(KDbPath8);
       
   376 	TEST(rc == KErrNotFound || rc == KErrPathNotFound);
       
   377 	db.Close();
       
   378 
       
   379 	//An attempt to open existing public shared database file on a read-only drive (Z:)
       
   380 	_LIT(KDbPath9, "Z:\\TEST\\TestDb1.db");
       
   381 	rc = db.Open(KDbPath9);
       
   382 	TEST2(rc, KErrNone);
       
   383 	db.Close();
       
   384 	
       
   385 	//An attempt to open corrupted public shared database file on a read-only drive (Z:)
       
   386 	_LIT(KDbPath10, "Z:\\TEST\\CorruptDb.db");
       
   387 	rc = db.Open(KDbPath10);
       
   388 	// it will be KErrNotDb if SqlServer.cfg exists, else KErrNone if it doesn't
       
   389 	// this is because we can detect a corrupt database when we attempt to
       
   390 	// set the configuration. If there is no cfg file, then there will be no
       
   391 	// attempt to set the pragmas and so the corrupt file is undetected
       
   392 	TEST(rc==KSqlErrNotDb || rc==KErrNone);
       
   393 	db.Close();
       
   394 
       
   395 	//Copy the corrupted database file on drive C:
       
   396 	TEST2(fs.Connect(), KErrNone);
       
   397 	rc = BaflUtils::CopyFile(fs, KDbPath10, KTestDbName3);
       
   398 	TEST2(rc, KErrNone);
       
   399 	(void)fs.SetAtt(KTestDbName3, 0, KEntryAttReadOnly);
       
   400 	fs.Close();
       
   401 
       
   402 	//An attempt to open corrupted public shared database file on a drive (C:)
       
   403 	rc = db.Open(KTestDbName3);
       
   404 	TEST(rc == KSqlErrNotDb || rc == KErrNone);//Note: but it may be a different error code as well
       
   405 	db.Close();
       
   406 	(void)RSqlDatabase::Delete(KTestDbName3);
       
   407 	
       
   408 	//Create, Close, Open, Close and again Open database test
       
   409 	rc = db.Create(KTestDbName2, securityPolicy);
       
   410 	TEST2(rc, KErrNone);
       
   411 	db.Close();
       
   412 	rc = db.Open(KTestDbName2);
       
   413 	TEST2(rc, KErrNone);
       
   414 	db.Close();
       
   415 	rc = db.Open(KTestDbName2);
       
   416 	TEST2(rc, KErrNone);
       
   417 	db.Close();
       
   418 
       
   419 	//Open two connections to the same database.
       
   420 	rc = db.Open(KTestDbName2);
       
   421 	TEST2(rc, KErrNone);
       
   422 	rc = db2.Open(KTestDbName2);
       
   423 	TEST2(rc, KErrNone);
       
   424 	db2.Close();
       
   425 	db.Close();
       
   426 	
       
   427 	rc = RSqlDatabase::Delete(KTestDbName2);
       
   428 	TEST2(rc, KErrNone);
       
   429 	
       
   430 	securityPolicy.Close();
       
   431 	}
       
   432 
       
   433 ///////////////////////////////////////////////////////////////////////////////////////
       
   434 
       
   435 /**
       
   436 @SYMTestCaseID			SYSLIB-SQL-CT-1602
       
   437 @SYMTestCaseDesc		Setting database isolation level tests.
       
   438 @SYMTestPriority		High
       
   439 @SYMTestActions			Tests for RSqlDatabase::SetIsolationLevel() method.
       
   440 @SYMTestExpectedResults Test must not fail
       
   441 @SYMREQ					REQ5792
       
   442                         REQ5793
       
   443 */	
       
   444 void SetIsolationLevelTest()
       
   445 	{
       
   446 	(void)RSqlDatabase::Delete(KTestDbName1);
       
   447 
       
   448 	RSqlDatabase db;		
       
   449 	TInt err = db.Create(KTestDbName1);
       
   450 	TEST2(err, KErrNone);
       
   451 
       
   452 	err = db.SetIsolationLevel(RSqlDatabase::EReadCommitted);
       
   453 	TEST2(err, KErrNotSupported);
       
   454 	
       
   455 	err = db.SetIsolationLevel(RSqlDatabase::ERepeatableRead);
       
   456 	TEST2(err, KErrNotSupported);
       
   457 	
       
   458 	err = db.SetIsolationLevel(RSqlDatabase::EReadUncommitted);
       
   459 	TEST2(err, KErrNone);
       
   460 	
       
   461 	err = db.SetIsolationLevel(RSqlDatabase::ESerializable);
       
   462 	TEST2(err, KErrNone);
       
   463 	
       
   464 	db.Close();
       
   465 	
       
   466 	(void)RSqlDatabase::Delete(KTestDbName1);
       
   467 	}
       
   468 
       
   469 ///////////////////////////////////////////////////////////////////////////////////////
       
   470 
       
   471 /**
       
   472 @SYMTestCaseID			SYSLIB-SQL-CT-1603
       
   473 @SYMTestCaseDesc		Deleting database tests. Deleting non-existing database, opened database, 
       
   474 						database on non-existing drive, zero-length database name, only path (no file name),
       
   475 						rom drive based database.
       
   476 @SYMTestPriority		High
       
   477 @SYMTestActions			Tests for RSqlDatabase::Delete() method.
       
   478 @SYMTestExpectedResults Test must not fail
       
   479 @SYMREQ					REQ5792
       
   480                         REQ5793
       
   481 */	
       
   482 void DeleteDatabaseTest()
       
   483 	{
       
   484 	//An attempt to delete non-existing secure shared database
       
   485 	_LIT(KDbName1, "C:[1111CCCC]EE__900000.adb");
       
   486 	TInt err = RSqlDatabase::Delete(KDbName1);
       
   487 	TEST2(err, KErrNotFound);
       
   488 
       
   489 	//An attempt to delete non-existing public database
       
   490 	_LIT(KDbName2, "C:\\TEST\\__900000.adb");
       
   491 	err = RSqlDatabase::Delete(KDbName2);
       
   492 	TEST2(err, KErrNotFound);
       
   493 	
       
   494 	//Zero length database file name.
       
   495 	_LIT(KDbName3, "");
       
   496 	err = RSqlDatabase::Delete(KDbName3);
       
   497 	TEST2(err,  KErrBadName);
       
   498 	
       
   499 	//Database file name containing only the drive name
       
   500 	_LIT(KDbName4, "C:");
       
   501 	err = RSqlDatabase::Delete(KDbName4);
       
   502 	TEST2(err, KErrBadName);
       
   503 	
       
   504 	//Database file name containing only the path, without the file name
       
   505 	_LIT(KDbName5, "C:");
       
   506 	err = RSqlDatabase::Delete(KDbName5);
       
   507 	TEST2(err, KErrBadName);
       
   508 	
       
   509 	//Public shared database file on a non-existing drive (A:)	
       
   510 	_LIT(KDbName6, "A:\\test\\db1.db");
       
   511 	err = RSqlDatabase::Delete(KDbName6);
       
   512 	TEST2(err, KErrNotReady);
       
   513 
       
   514 	//An attempt to delete opened database.
       
   515 	RSqlDatabase db;	
       
   516 	err = db.Create(KDbName7);
       
   517 	TEST2(err, KErrNone);
       
   518 	err = RSqlDatabase::Delete(KDbName7);
       
   519 	TEST2(err, KErrInUse);
       
   520 	db.Close();
       
   521 	err = RSqlDatabase::Delete(KDbName7);
       
   522 	TEST2(err, KErrNone);
       
   523 
       
   524 	//An attempt to delete existing public shared database file on a read-only drive (Z:)
       
   525 	_LIT(KDbName8, "Z:\\TEST\\TestDb1.db");
       
   526 	err = RSqlDatabase::Delete(KDbName8);
       
   527 	TEST2(err, KErrAccessDenied);
       
   528 	
       
   529 	//Create secure database
       
   530 	TSecurityPolicy defaultPolicy(TSecurityPolicy::EAlwaysPass);
       
   531 	RSqlSecurityPolicy securityPolicy;
       
   532 	err = securityPolicy.Create(defaultPolicy);
       
   533 	TEST2(err, KErrNone);
       
   534 	err = db.Create(KTestDbName7, securityPolicy);
       
   535 	TEST2(err, KErrNone);
       
   536 	db.Close();
       
   537 	securityPolicy.Close();
       
   538 	
       
   539 	//Attempt to delete a secure public database.
       
   540 	//The calling application has no rights to delete a file with that name from the server's 
       
   541 	//private data cage.
       
   542    	err = RSqlDatabase::Delete(_L("C:[45454545]qq.db"));
       
   543 	TEST2(err, KErrPermissionDenied);
       
   544 
       
   545 	//Attempt to delete a secure public database. No drive specified.
       
   546 	//The calling application has no rights to delete a file with that name from the server's 
       
   547 	//private data cage.
       
   548 	err = RSqlDatabase::Delete(_L("[45454545]qq.db"));
       
   549 	TEST2(err, KErrPermissionDenied);
       
   550 
       
   551 	//Attempt to delete secure database specifying the full database path
       
   552 	TParse parse;
       
   553 	parse.Set(KTestDbName7, &KServerPrivateDir(), 0);
       
   554 	err = RSqlDatabase::Delete(parse.FullName());
       
   555 	TEST2(err, KErrArgument);
       
   556 
       
   557 	//Attempt to delete secure database specifying only the database name
       
   558 	err = RSqlDatabase::Delete(parse.NameAndExt());
       
   559 	//If C: is the system drive then the operation must pass.
       
   560 	TEST2(err, KErrNone);
       
   561 	}
       
   562 
       
   563 ///////////////////////////////////////////////////////////////////////////////////////
       
   564 
       
   565 /**
       
   566 @SYMTestCaseID			SYSLIB-SQL-CT-1640
       
   567 @SYMTestCaseDesc		Copying database tests. Copying:
       
   568 						- non-secure to non-secure database;
       
   569 						- non-secure to secure database;
       
   570 						- secure to non-secure database;
       
   571 						- secure to secure database;
       
   572 						- secure database, when the application is not the database creator (owner);
       
   573 @SYMTestPriority		High
       
   574 @SYMTestActions			Tests for RSqlDatabase::Copy() method.
       
   575 @SYMTestExpectedResults Test must not fail
       
   576 @SYMREQ					REQ5792
       
   577                         REQ5793
       
   578 */	
       
   579 void CopyDatabaseTest()
       
   580 	{
       
   581 	RSqlDatabase db;	
       
   582 
       
   583 	//Create secure database
       
   584 	TSecurityPolicy defaultPolicy(TSecurityPolicy::EAlwaysPass);
       
   585 	RSqlSecurityPolicy securityPolicy;
       
   586 	TInt err = securityPolicy.Create(defaultPolicy);
       
   587 	TEST2(err, KErrNone);
       
   588 	err = db.Create(KTestDbName7, securityPolicy);
       
   589 	TEST2(err, KErrNone);
       
   590 	db.Close();
       
   591 	securityPolicy.Close();
       
   592 	
       
   593 	//Create non-secure database
       
   594 	err = db.Create(KTestDbName1);
       
   595 	TEST2(err, KErrNone);
       
   596 	db.Close();
       
   597 
       
   598 	//Copy non-secure to non-secure database	
       
   599 	err = RSqlDatabase::Copy(KTestDbName1, KTestDbName8);
       
   600 	TEST2(err, KErrNone);
       
   601 
       
   602 	//Attempt to copy non-secure to secure database	
       
   603 	err = RSqlDatabase::Copy(KTestDbName1, _L("C:[99999999]pkk.db"));
       
   604 	TEST2(err, KErrPermissionDenied);
       
   605 
       
   606 	//Attempt to copy secure to non-secure database	
       
   607 	err = RSqlDatabase::Copy(KTestDbName7, _L("C:\\test\\asdf.db"));
       
   608 	TEST2(err, KErrPermissionDenied);
       
   609 
       
   610 	//Copy secure to secure database. The test application is the database owner.
       
   611 	err = RSqlDatabase::Copy(KTestDbName7, KTestDbName4);
       
   612 	TEST2(err, KErrNone);
       
   613 	err = RSqlDatabase::Delete(KTestDbName4);
       
   614 	TEST2(err, KErrNone);
       
   615 
       
   616 	//Attempt to copy secure to secure database. The test application is not the database owner.
       
   617 	err = RSqlDatabase::Copy(KTestDbName7, _L("C:[11111111]ff.db"));
       
   618 	TEST2(err, KErrPermissionDenied);
       
   619 	err = RSqlDatabase::Copy(_L("C:[11111111]ff.db"), _L("C:[22222222]ff.db"));
       
   620 	TEST2(err, KErrPermissionDenied);
       
   621 	err = RSqlDatabase::Copy(_L("C:[11111111]ff.db"), KTestDbName7);
       
   622 	TEST2(err, KErrPermissionDenied);
       
   623 	
       
   624 	(void)RSqlDatabase::Delete(KTestDbName8);
       
   625 	(void)RSqlDatabase::Delete(KTestDbName7);
       
   626 	(void)RSqlDatabase::Delete(KTestDbName4);
       
   627 	(void)RSqlDatabase::Delete(KTestDbName1);
       
   628 	}
       
   629 
       
   630 ///////////////////////////////////////////////////////////////////////////////////////
       
   631 
       
   632 /**
       
   633 @SYMTestCaseID			SYSLIB-SQL-CT-1604
       
   634 @SYMTestCaseDesc		Create a table with two integer columns. The second column has a default value NULL.
       
   635 						Check what will be the result of "column1 + column2" operation, if "column2" 
       
   636 						value is NULL.
       
   637 @SYMTestPriority		High
       
   638 @SYMTestActions			Tests mathematical operations with ESqlNull column values.
       
   639 @SYMTestExpectedResults Test must not fail
       
   640 @SYMREQ					REQ5792
       
   641                         REQ5793
       
   642 */	
       
   643 void NullColumnValues()
       
   644 	{
       
   645 	(void)RSqlDatabase::Delete(KTestDbName1);
       
   646 	
       
   647 	RSqlDatabase db;		
       
   648 	TInt err = db.Create(KTestDbName1);
       
   649 	TEST2(err, KErrNone);
       
   650 
       
   651 	err = db.Exec(_L8("CREATE TABLE test(int_fld integer, null_int_fld integer default null)"));
       
   652 	TEST(err >= 0);
       
   653 
       
   654 	err = db.Exec(_L8("INSERT INTO test(int_fld) values(200)"));
       
   655 	TEST2(err, 1);
       
   656 
       
   657 	RSqlStatement stmt;
       
   658 	
       
   659 	err = stmt.Prepare(db, _L8("SELECT * from test"));
       
   660 	TEST2(err, KErrNone);
       
   661 	
       
   662 	err = stmt.Next();
       
   663 	TEST2(err, KSqlAtRow);
       
   664 
       
   665 	TSqlColumnType colType = stmt.ColumnType(0);
       
   666 	TEST(colType == ESqlInt);
       
   667 	
       
   668 	colType = stmt.ColumnType(1);
       
   669 	TEST(colType == ESqlNull);
       
   670 	
       
   671 	TInt val = stmt.ColumnInt(0);
       
   672 	TEST(val == 200);
       
   673 	
       
   674 	val = stmt.ColumnInt(1);
       
   675 	TEST(val == 0);
       
   676 
       
   677 	stmt.Close();
       
   678 	
       
   679 	err = stmt.Prepare(db, _L8("SELECT (int_fld + null_int_fld) as res from test"));
       
   680 	TEST2(err, KErrNone);
       
   681 	
       
   682 	err = stmt.Next();
       
   683 	TEST2(err, KSqlAtRow);
       
   684 	
       
   685 	colType = stmt.ColumnType(0);
       
   686 	TEST(colType == ESqlNull);
       
   687 	
       
   688 	val = stmt.ColumnInt(0);
       
   689 	TEST(val == 0);
       
   690 	
       
   691 	stmt.Close();
       
   692 	db.Close();
       
   693 
       
   694 	(void)RSqlDatabase::Delete(KTestDbName1);
       
   695 	}
       
   696 
       
   697 ///////////////////////////////////////////////////////////////////////////////////////
       
   698 
       
   699 _LIT8(KStmt1, "CREATE TABLE A1(Fld1 INTEGER, Fld2 DOUBLE);\
       
   700 									   CREATE TABLE A2(Fld1 INTEGER, Fld2 DOUBLE);\
       
   701 									   CREATE TRIGGER TrgA1Ins BEFORE Insert ON A1\
       
   702 									   BEGIN\
       
   703 									      INSERT INTO A2(Fld1, Fld2) VALUES(new.Fld1, new.Fld2);\
       
   704 									   END;");
       
   705 _LIT8(KStmt2, "INSERT INTO A1(Fld1, Fld2) VALUES(1, 2.0)");
       
   706 _LIT8(KStmt3, "SELECT * FROM A2");
       
   707 _LIT8(KStmt4, "INSERT INTO A1(Fld1, Fld2) VALUES(2, 4.0); UPDATE A2 SET Fld2 = 11.3 WHERE Fld1 = 2");
       
   708 _LIT8(KStmt5, "");
       
   709 _LIT8(KStmt6, "INSERT INTO A1(Fld1, Fld2) VALUESa(6, 234.0);");
       
   710 _LIT8(KStmt7, "");
       
   711 _LIT8(KStmt8, ";;;;;");
       
   712 _LIT8(KStmt9, "INSERT INTO A1(Fld1, Fld2) VALUES(:v1, :v2)");
       
   713 
       
   714 /**
       
   715 @SYMTestCaseID			SYSLIB-SQL-CT-1605
       
   716 @SYMTestCaseDesc		SQL statements execution. Valid SQL statements. Empty SQL statement. 
       
   717 						More than one SQL statements, separated with ";". SQL statement which syntax is
       
   718 						incorrect. SQL statement with parameters. INSERT/SELECT/CREATE TABLE SQL statements.
       
   719 @SYMTestPriority		High
       
   720 @SYMTestActions			RSqlDatabase::Exec() tests, 16-bit and 8-bit SQL statements
       
   721 @SYMTestExpectedResults Test must not fail
       
   722 @SYMREQ					REQ5792
       
   723                         REQ5793
       
   724 */	
       
   725 template <class DES, class BUF> void ExecOnDbTest()
       
   726 	{
       
   727 	RSqlDatabase db;
       
   728 	TInt rc = db.Create(KTestDbName1);
       
   729 	TEST2(rc, KErrNone);
       
   730 
       
   731 	//Create two tables and a trigger	              	  
       
   732 	ExecSqlStmtOnDb<DES, BUF>(db, KStmt1, KErrNone); 
       
   733 
       
   734 	//INSERT sql statement execution
       
   735 	ExecSqlStmtOnDb<DES, BUF>(db, KStmt2, KErrNone); 
       
   736 
       
   737 	//SELECT sql statement execution
       
   738 	ExecSqlStmtOnDb<DES, BUF>(db, KStmt3, KErrNone); 
       
   739 
       
   740 	//Executing more than one SQL statement in a single Exec() call.
       
   741 	ExecSqlStmtOnDb<DES, BUF>(db, KStmt4, KErrNone); 
       
   742 
       
   743 	//Executing zero length SQL statement.
       
   744 	ExecSqlStmtOnDb<DES, BUF>(db, KStmt5, KErrNone); 
       
   745 	
       
   746 	//Executing a SQL statement with syntax errors.
       
   747 	ExecSqlStmtOnDb<DES, BUF>(db, KStmt6, KSqlErrGeneral); 
       
   748 
       
   749 	//Executing an empty SQL statement.
       
   750 	ExecSqlStmtOnDb<DES, BUF>(db, KStmt7, KErrNone); 
       
   751 	
       
   752 	//Executing a SQL string, which does not have any SQL statements inside, but has valid syntax.
       
   753 	ExecSqlStmtOnDb<DES, BUF>(db, KStmt8, KErrNone); 
       
   754 	
       
   755 	//Executing SQL statement with parameters. They will be set with NULL values, if not set explicitly.
       
   756 	ExecSqlStmtOnDb<DES, BUF>(db, KStmt9, KErrNone); 
       
   757 	
       
   758 	db.Close();
       
   759 
       
   760 	rc = RSqlDatabase::Delete(KTestDbName1);
       
   761 	TEST2(rc, KErrNone);
       
   762 	}
       
   763 
       
   764 _LIT8(KStmt10, "");
       
   765 _LIT8(KStmt11, ";   ;  ;;;");
       
   766 _LIT8(KStmt12, "CREATE TABLE AAA(Fld1 INTEGER, Fld2 VARCHAR(100))");
       
   767 _LIT8(KStmt13, "INSERT INTO AAA(Fld1, Fld2) VALUES(5, 'FldVal1-1234567890')");
       
   768 _LIT8(KStmt14, "SELECT fld1, fld2 FROM AAA");
       
   769 _LIT8(KStmt15, "SELECT fld2, fld1 FROM AAA");
       
   770 _LIT8(KStmt16, "SELECT fld2, fld1 FROM AAA WHERE Fld1 > :Prm1 AND fld2 = :PRM2");
       
   771 _LIT8(KStmt17, "INSERT INTO AAA(Fld1, Fld2) VALUES(:b, :a);");
       
   772 _LIT8(KStmt18, "CREATE TABLE BBB(Fld1 INTEGER, Fld2 BIGINT, Fld3 DOUBLE, \
       
   773 					                       Fld4 TEXT, Fld5 LONGBLOB, Fld6 TEXT NULL)");
       
   774 _LIT8(KStmt19, "INSERT INTO BBB(Fld1, Fld2, Fld3, Fld4, Fld5, Fld6)\
       
   775 	                                       VALUES(:V1, :V2, :V3, :V4, :V5, :V6)");
       
   776 _LIT8(KStmt20, "SELECT * FROM BBB");
       
   777 
       
   778 /**
       
   779 @SYMTestCaseID			SYSLIB-SQL-CT-1606
       
   780 @SYMTestCaseDesc		Preparing SQL statements. Moving to the next record. Retrieving and verifying 
       
   781 						the column types and values. Binding parameter values.
       
   782 @SYMTestPriority		High
       
   783 @SYMTestActions			RSqlStatement::Prepare(), RSqlStatement::Next() tests, 16-bit and 8-bit SQL statements.
       
   784 						RSqlStatement::ColumnIndex(), RSqlStatement::Column<DataType>(), RSqlStatement::Bind<DataType>(),
       
   785 						RSqlStatement::Column<DataType>().				
       
   786 @SYMTestExpectedResults Test must not fail
       
   787 @SYMREQ					REQ5792
       
   788                         REQ5793
       
   789 */	
       
   790 template <class DES, class BUF> void StatementTest()
       
   791 	{
       
   792 	RSqlDatabase db;	
       
   793 	TInt rc = db.Create(KTestDbName1);
       
   794 	TEST2(rc, KErrNone);
       
   795 
       
   796 	//Executing an empty SQL statement.
       
   797 	RSqlStatement stmt = PrepareSqlStmt<DES, BUF>(db, KStmt10, KErrArgument);
       
   798 	stmt.Close();
       
   799 
       
   800 	//Executing a SQL string, which does not have any SQL statements inside, but has valid syntax.
       
   801 	stmt = PrepareSqlStmt<DES, BUF>(db, KStmt11, KErrArgument); 
       
   802 	stmt.Close();
       
   803 	
       
   804 	//SQL statement without parameters. Create a table.
       
   805 	stmt = PrepareSqlStmt<DES, BUF>(db, KStmt12, KErrNone);
       
   806 	ExecSqlStmt(db, stmt, KErrNone);
       
   807 	stmt.Close();
       
   808 
       
   809 	//SQL statement without parameters. Insert a record into the table.
       
   810 	stmt = PrepareSqlStmt<DES, BUF>(db, KStmt13, KErrNone);
       
   811 	ExecSqlStmt(db, stmt, KErrNone);
       
   812 	stmt.Close();
       
   813 		
       
   814 	//Test RSqlStatement::ColumnIndex().
       
   815 	stmt = PrepareSqlStmt<DES, BUF>(db, KStmt14, KErrNone);
       
   816 	TInt idx1 = stmt.ColumnIndex(_L("FLD1"));
       
   817 	TEST(idx1 == 0);
       
   818 	TInt idx2 = stmt.ColumnIndex(_L("FlD2"));
       
   819 	TEST(idx2 == 1);
       
   820 	TInt idx3 = stmt.ColumnIndex(_L("fld3"));
       
   821 	TEST(idx3 < 0);
       
   822 	stmt.Close();
       
   823 	
       
   824 	stmt = PrepareSqlStmt<DES, BUF>(db, KStmt15, KErrNone);
       
   825 	idx1 = stmt.ColumnIndex(_L("FLD1"));
       
   826 	TEST(idx1 == 1);
       
   827 	idx2 = stmt.ColumnIndex(_L("FlD2"));
       
   828 	TEST(idx2 == 0);
       
   829 	idx3 = stmt.ColumnIndex(_L("fld3"));
       
   830 	TEST(idx3 < 0);
       
   831 	
       
   832 	//Test RSqlStatement::Column<DataType>() methods.
       
   833 	TInt recCnt = 0;
       
   834 	while((rc = stmt.Next()) == KSqlAtRow)
       
   835 		{
       
   836 		++recCnt;
       
   837 		
       
   838 		TInt intVal = stmt.ColumnInt(idx1);
       
   839 		TEST(intVal == 5);
       
   840 		
       
   841 		//Integer column value retrieved as a text.
       
   842 		TPtrC strVal1;
       
   843 		TInt err = stmt.ColumnText(idx1, strVal1);
       
   844 		TEST2(err, KErrNone);
       
   845 		TEST(strVal1 == KNullDesC);
       
   846 		
       
   847 		//Text column value retrieved as a binary
       
   848 		TBuf8<50> strVal3;
       
   849 		err = stmt.ColumnBinary(idx2, strVal3);
       
   850 		TEST2(err, KErrNone);
       
   851 		TEST(strVal3 == KNullDesC8);
       
   852 		
       
   853 		TPtrC strVal2;
       
   854 		err = stmt.ColumnText(idx2, strVal2);
       
   855 		TEST2(err, KErrNone);
       
   856 		TEST(strVal2 == _L("FldVal1-1234567890"));
       
   857 				
       
   858 		//Invalid column index. Panic: "SqlDb 5"
       
   859 		//intVal = stmt.ColumnInt(1002);
       
   860 		//intVal = stmt.ColumnInt(-24);
       
   861 		}
       
   862 	stmt.Close();
       
   863 	TEST2(rc, KSqlAtEnd);
       
   864 	TEST2(SqlRetCodeClass(rc), ESqlInformation);
       
   865 	TEST(recCnt == 1);
       
   866 	
       
   867 	//Test RSqlStatement::Bind<DataType>() methods.
       
   868 	stmt = PrepareSqlStmt<DES, BUF>(db, KStmt16, KErrNone);
       
   869 	idx1 = stmt.ColumnIndex(_L("FLD1"));
       
   870 	TEST(idx1 == 1);
       
   871 	idx2 = stmt.ColumnIndex(_L("FlD2"));
       
   872 	TEST(idx2 == 0);
       
   873 	TInt prmIdx1 = stmt.ParameterIndex(_L(":prm1"));
       
   874 	TEST(prmIdx1 == 0);
       
   875 	TInt prmIdx2 = stmt.ParameterIndex(_L(":prm2"));
       
   876 	TEST(prmIdx2 == 1);
       
   877 	rc = stmt.BindInt(prmIdx1, -4);
       
   878 	//No problem to bind whatever value type we like
       
   879 	//rc = stmt.BindBinary(prmIdx1, KSqlStmt4());
       
   880 	TEST2(rc, KErrNone);
       
   881 	rc = stmt.BindText(prmIdx2, _L("FldVal1-1234567890"));
       
   882 	TEST2(rc, KErrNone);
       
   883 	//Test RSqlStatement::Column<DataType>() methods.
       
   884 	recCnt = 0;
       
   885 	while((rc = stmt.Next()) == KSqlAtRow)
       
   886 		{
       
   887 		++recCnt;
       
   888 		TInt intVal = stmt.ColumnInt(idx1);
       
   889 		TEST(intVal == 5);
       
   890 		TPtrC strVal;
       
   891 		TInt err = stmt.ColumnText(idx2, strVal);
       
   892 		TEST2(err, KErrNone);
       
   893 		TEST(strVal == _L("FldVal1-1234567890"));
       
   894 		}
       
   895 	stmt.Close();
       
   896 	TEST2(rc, KSqlAtEnd);
       
   897 	TEST2(SqlRetCodeClass(rc), ESqlInformation);
       
   898 	TEST(recCnt == 1);
       
   899 	
       
   900 	//Test an INSERT SQL - prepare, bind, exec.
       
   901 	stmt = PrepareSqlStmt<DES, BUF>(db, KStmt17, KErrNone);
       
   902 	prmIdx1 = stmt.ParameterIndex(_L(":A"));
       
   903 	TEST(prmIdx1 == 1);
       
   904 	prmIdx2 = stmt.ParameterIndex(_L(":B"));
       
   905 	TEST(prmIdx2 == 0);
       
   906 	rc = stmt.BindInt(prmIdx1, 20);
       
   907 	TEST2(rc, KErrNone);
       
   908 	rc = stmt.BindText(prmIdx2, _L("FldVal2"));
       
   909 	TEST2(rc, KErrNone);
       
   910 	rc = stmt.Exec();
       
   911 	TEST2(rc, 1);
       
   912 	
       
   913 	stmt.Close();
       
   914 	
       
   915 	//Create a table with INTEGER, INT64, REAL, TEXT, BINARY, NULL, field types
       
   916 	stmt = PrepareSqlStmt<DES, BUF>(db, KStmt18, KErrNone);
       
   917 	ExecSqlStmt(db, stmt, KErrNone);
       
   918 	stmt.Close();
       
   919 	
       
   920 	enum {KLow = 1, KHigh = 2};
       
   921 	//Insert (KHigh - KLow + 1) records
       
   922 	stmt = PrepareSqlStmt<DES, BUF>(db, KStmt19, KErrNone);
       
   923 	TInt v32 = 1024;
       
   924 	TInt64 v64 = MAKE_TINT64(0x00FF00FF, 0x12345678);
       
   925 	TReal vReal = 234.75;
       
   926 	TBuf<10> vText;
       
   927 	TBuf8<10> vBinary;
       
   928 	for(TInt i=KLow;i<=KHigh;++i)
       
   929 		{
       
   930 		rc = stmt.BindInt(0, v32 * i);
       
   931 		TEST2(rc, KErrNone);
       
   932 		
       
   933 		rc = stmt.BindInt64(1, v64 * i);
       
   934 		TEST2(rc, KErrNone);
       
   935 		
       
   936 		rc = stmt.BindReal(2, vReal * i);
       
   937 		TEST2(rc, KErrNone);
       
   938 		
       
   939 		vText.Copy(_L("TEXT"));
       
   940 		vText.Append(TChar(i + '0'));
       
   941 		rc = stmt.BindText(3, vText);
       
   942 		TEST2(rc, KErrNone);
       
   943 
       
   944 		vBinary.Copy(_L("BINARY"));
       
   945 		vBinary.Append(TChar(i + '0'));
       
   946 		rc = stmt.BindBinary(4, vBinary);
       
   947 		TEST2(rc, KErrNone);
       
   948 
       
   949 		rc = stmt.BindNull(5);
       
   950 		TEST2(rc, KErrNone);
       
   951 
       
   952 		rc = stmt.Exec();
       
   953 		TEST2(rc, 1);
       
   954 
       
   955 		rc = stmt.Reset();
       
   956 		TEST2(rc, KErrNone);
       
   957 		}
       
   958 	stmt.Close();
       
   959 
       
   960 	//Read and test (KHigh - KLow + 1) records
       
   961 	stmt = PrepareSqlStmt<DES, BUF>(db, KStmt20, KErrNone);
       
   962 	recCnt = 0;
       
   963 	while((rc = stmt.Next()) == KSqlAtRow)
       
   964 		{
       
   965 		++recCnt;
       
   966 		TInt v1 = stmt.ColumnInt(0);
       
   967 		TEST(v1 == v32 * recCnt);
       
   968 
       
   969 		TInt64 v2 = stmt.ColumnInt64(1);
       
   970 		TEST(v2 == v64 * recCnt);
       
   971 
       
   972 		TReal v3 = stmt.ColumnReal(2);
       
   973 		TEST(Abs(v3 - vReal * recCnt) < 0.000001);
       
   974 		
       
   975 		vText.Copy(_L("TEXT"));
       
   976 		vText.Append(TChar(recCnt + '0'));
       
   977 		TPtrC v4;
       
   978 		TInt err = stmt.ColumnText(3, v4);
       
   979 		TEST2(err, KErrNone);
       
   980 		TEST(v4 == vText);
       
   981 		
       
   982 		vBinary.Copy(_L("BINARY"));
       
   983 		vBinary.Append(TChar(recCnt + '0'));
       
   984 		TPtrC8 v5;
       
   985 		err = stmt.ColumnBinary(4, v5);
       
   986 		TEST2(err, KErrNone);
       
   987 		TEST(v5 == vBinary);
       
   988 		
       
   989 		TBool b = stmt.IsNull(5);
       
   990 		TEST(b != 0);
       
   991 		}
       
   992 	stmt.Close();
       
   993 	TEST2(rc, KSqlAtEnd);
       
   994 	TEST2(SqlRetCodeClass(rc), ESqlInformation);
       
   995 	TEST(recCnt == (KHigh - KLow + 1));
       
   996 	
       
   997 	db.Close();
       
   998 
       
   999 	rc = RSqlDatabase::Delete(KTestDbName1);
       
  1000 	TEST2(rc, KErrNone);
       
  1001 	}
       
  1002 
       
  1003 /**
       
  1004 @SYMTestCaseID			SYSLIB-SQL-CT-1607
       
  1005 @SYMTestCaseDesc		Retrieving long text column values test.
       
  1006 						Retrieving long column values using RSqlStatement::ColumnText() when the recipient buffer
       
  1007 						is not big enough.
       
  1008 @SYMTestPriority		High
       
  1009 @SYMTestActions			RSqlColumnReadStream::ColumnText() test.
       
  1010 @SYMTestExpectedResults Test must not fail
       
  1011 @SYMREQ					REQ5792
       
  1012                         REQ5793
       
  1013 */	
       
  1014 void ColumnTextStreamTest()
       
  1015 	{
       
  1016 	RSqlDatabase db;	
       
  1017 	TInt rc = db.Create(KTestDbName1);
       
  1018 	TEST2(rc, KErrNone);
       
  1019 
       
  1020 	enum {KSqlBufSize = 64};
       
  1021 	
       
  1022 	//Create a table
       
  1023 	_LIT8(KSqlStmt1, "CREATE TABLE A(Fld1 INTEGER, Fld2 TEXT);");
       
  1024 	ExecSqlStmtOnDb<TDesC8, TBuf8<KSqlBufSize> >(db, KSqlStmt1(), KErrNone); 
       
  1025 	
       
  1026 	const TInt KTextLen = 3101;
       
  1027 
       
  1028 	_LIT(KSqlStmt2, "INSERT INTO A(Fld1, Fld2) VALUES(");
       
  1029 	
       
  1030 	//Allocate a buffer for the SQL statement.
       
  1031 	HBufC* buf = HBufC::New(KSqlStmt2().Length() + KTextLen + 10);
       
  1032 	TEST(buf != NULL);
       
  1033 
       
  1034 	TPtr sql = buf->Des();
       
  1035 	
       
  1036 	//Insert row 1
       
  1037 	
       
  1038 	const TChar KChar1('A');
       
  1039 	sql.Copy(KSqlStmt2);
       
  1040 	sql.Append(_L("1, '"));
       
  1041 	TInt i;
       
  1042 	for(i=0;i<KTextLen;++i)
       
  1043 		{
       
  1044 		sql.Append(KChar1);
       
  1045 		}
       
  1046 	sql.Append(_L("')"));
       
  1047 
       
  1048 	rc = db.Exec(sql);
       
  1049 	TEST2(rc, 1);
       
  1050 
       
  1051 	//Insert row 2
       
  1052 	
       
  1053 	const TChar KChar2('B');
       
  1054 	sql.Copy(KSqlStmt2);
       
  1055 	sql.Append(_L("2, '"));
       
  1056 	for(i=0;i<KTextLen;++i)
       
  1057 		{
       
  1058 		sql.Append(KChar2);
       
  1059 		}
       
  1060 	sql.Append(_L("')"));
       
  1061 
       
  1062 	rc = db.Exec(sql);
       
  1063 	TEST2(rc, 1);
       
  1064 
       
  1065 	//Prepare SELECT SQL statement
       
  1066 	_LIT8(KSqlStmt3, "SELECT * FROM A");
       
  1067 	RSqlStatement stmt = PrepareSqlStmt<TDesC, TBuf<KSqlBufSize> >(db, KSqlStmt3, KErrNone);
       
  1068 
       
  1069 	//Move on row 1
       
  1070 	rc = stmt.Next();
       
  1071 	TEST2(rc, KSqlAtRow);
       
  1072 
       
  1073 	//Read the long text column using a stream
       
  1074 	RSqlColumnReadStream columnStream;
       
  1075 	rc = columnStream.ColumnText(stmt, 1);
       
  1076 	columnStream.Close();
       
  1077 	TEST2(rc, KErrNone);
       
  1078 
       
  1079 	//...and the leaving version of ColumnText() 
       
  1080 	TRAP(rc, columnStream.ColumnTextL(stmt, 1));
       
  1081 	TEST2(rc, KErrNone);
       
  1082 	
       
  1083 	TInt size = stmt.ColumnSize(1);
       
  1084 	TPtr colData = buf->Des();
       
  1085 	TRAP(rc, columnStream.ReadL(colData, size));
       
  1086 	columnStream.Close();
       
  1087 	TEST2(rc, KErrNone);
       
  1088 	
       
  1089 	//Check the column value
       
  1090 	for(i=0;i<KTextLen;++i)
       
  1091 		{
       
  1092 		TEST(colData[i] == (TUint8)KChar1);	
       
  1093 		}
       
  1094 
       
  1095 	//Move on row 2
       
  1096 	rc = stmt.Next();
       
  1097 	TEST2(rc, KSqlAtRow);
       
  1098 
       
  1099 	//Read row 2 using ColumnText(TInt aColumnIndex, TPtrC& aPtr).
       
  1100 	TPtrC colDataPtr;
       
  1101 	rc = stmt.ColumnText(1, colDataPtr);
       
  1102 	TEST2(rc, KErrNone);
       
  1103 	
       
  1104 	//Check the column value
       
  1105 	for(i=0;i<KTextLen;++i)
       
  1106 		{
       
  1107 		TEST(colDataPtr[i] == (TUint8)KChar2);	
       
  1108 		}
       
  1109 
       
  1110 	//Read row 2 using ColumnText(TInt aColumnIndex, TDes& aDest).
       
  1111 	rc = stmt.ColumnText(1, colData);
       
  1112 	TEST2(rc, KErrNone);
       
  1113 	
       
  1114 	//Check the column value
       
  1115 	for(i=0;i<KTextLen;++i)
       
  1116 		{
       
  1117 		TEST(colData[i] == (TUint8)KChar2);	
       
  1118 		}
       
  1119 	
       
  1120 	//Read row 2 using a stream
       
  1121 	colData.Zero();
       
  1122 	rc = columnStream.ColumnText(stmt, 1);
       
  1123 	TEST2(rc, KErrNone);
       
  1124 	size = stmt.ColumnSize(1);
       
  1125 	TRAP(rc, columnStream.ReadL(colData, size));
       
  1126 	columnStream.Close();
       
  1127 	TEST2(rc, KErrNone);
       
  1128 	
       
  1129 	//Check the column value
       
  1130 	for(i=0;i<KTextLen;++i)
       
  1131 		{
       
  1132 		TEST(colData[i] == (TUint8)KChar2);	
       
  1133 		}
       
  1134 	
       
  1135 	//Read the column value using RSqlStatement::ColumnText(). 
       
  1136 	//The recipient buffer max length is smaller than the column value length.
       
  1137 	rc = stmt.Reset();
       
  1138 	TEST2(rc, KErrNone);
       
  1139 	rc = stmt.Next();
       
  1140 	TEST2(rc, KSqlAtRow);
       
  1141 	const TInt KBufMaxLen = 100;
       
  1142 	TBuf<KBufMaxLen> colBuf;
       
  1143 	rc = stmt.ColumnText(1, colBuf);
       
  1144 	TEST2(rc, KErrOverflow);
       
  1145 	//Check the column value
       
  1146 	for(i=0;i<KBufMaxLen;++i)
       
  1147 		{
       
  1148 		TEST(colBuf[i] == (TUint8)KChar1);	
       
  1149 		}
       
  1150 		
       
  1151 	stmt.Close();
       
  1152 	
       
  1153 	//Deallocate buf
       
  1154 	delete buf; 
       
  1155 	buf = NULL;
       
  1156 	
       
  1157 	db.Close();
       
  1158 
       
  1159 	rc = RSqlDatabase::Delete(KTestDbName1);
       
  1160 	TEST2(rc, KErrNone);
       
  1161 	}
       
  1162 
       
  1163 /**
       
  1164 @SYMTestCaseID			SYSLIB-SQL-CT-1621
       
  1165 @SYMTestCaseDesc		Retrieving long binary column values test.
       
  1166 						Retrieving long column values using RSqlStatement::ColumnBinary() when the recipient buffer
       
  1167 						is not big enough.
       
  1168 @SYMTestPriority		High
       
  1169 @SYMTestActions			RSqlColumnReadStream::ColumnBinary() test.
       
  1170 @SYMTestExpectedResults Test must not fail
       
  1171 @SYMREQ					REQ5792
       
  1172                         REQ5793
       
  1173 */	
       
  1174 void ColumnBinaryStreamTest()
       
  1175 	{
       
  1176 	RSqlDatabase db;	
       
  1177 	TInt rc = db.Create(KTestDbName1);
       
  1178 	TEST2(rc, KErrNone);
       
  1179 
       
  1180 	enum {KSqlBufSize = 64};
       
  1181 	
       
  1182 	//Create a table
       
  1183 	_LIT8(KSqlStmt1, "CREATE TABLE A(Fld1 INTEGER, Fld2 BLOB);");
       
  1184 	ExecSqlStmtOnDb<TDesC8, TBuf8<KSqlBufSize> >(db, KSqlStmt1(), KErrNone); 
       
  1185 	
       
  1186 	const TInt KDataLen = 3311;
       
  1187 
       
  1188 	_LIT8(KSqlStmt2, "INSERT INTO A(Fld1, Fld2) VALUES(");
       
  1189 	
       
  1190 	//Allocate a buffer for the SQL statement.
       
  1191 	HBufC8* buf = HBufC8::New(KSqlStmt2().Length() + KDataLen * 2 + 10);//"* 2" - SQL statement with HEX values
       
  1192 	TEST(buf != NULL);
       
  1193 
       
  1194 	TPtr8 sql = buf->Des();
       
  1195 	
       
  1196 	//Insert row 1
       
  1197 
       
  1198 	const TUint8 KHexVal1 = 0x7E;
       
  1199 	_LIT8(KHexValStr1, "7E");
       
  1200 	sql.Copy(KSqlStmt2);
       
  1201 	sql.Append(_L8("1, x'"));
       
  1202 	TInt i;
       
  1203 	for(i=0;i<KDataLen;++i)
       
  1204 		{
       
  1205 		sql.Append(KHexValStr1);
       
  1206 		}
       
  1207 	sql.Append(_L8("')"));
       
  1208 
       
  1209 	rc = db.Exec(sql);
       
  1210 	TEST2(rc, 1);
       
  1211 
       
  1212 	//Insert row 2
       
  1213 	
       
  1214 	const TUint8 KHexVal2 = 0xA3;
       
  1215 	_LIT8(KHexValStr2, "A3");
       
  1216 	sql.Copy(KSqlStmt2);
       
  1217 	sql.Append(_L8("2, x'"));
       
  1218 	for(i=0;i<KDataLen;++i)
       
  1219 		{
       
  1220 		sql.Append(KHexValStr2);
       
  1221 		}
       
  1222 	sql.Append(_L8("')"));
       
  1223 
       
  1224 	rc = db.Exec(sql);
       
  1225 	TEST2(rc, 1);
       
  1226 
       
  1227 	//Prepare SELECT SQL statement
       
  1228 	_LIT8(KSqlStmt3, "SELECT * FROM A");
       
  1229 	RSqlStatement stmt = PrepareSqlStmt<TDesC8, TBuf8<KSqlBufSize> >(db, KSqlStmt3, KErrNone);
       
  1230 
       
  1231 	//Move on row 1
       
  1232 	rc = stmt.Next();
       
  1233 	TEST2(rc, KSqlAtRow);
       
  1234 
       
  1235 	//Read the long binary column using a stream
       
  1236 	RSqlColumnReadStream columnStream;
       
  1237 	rc = columnStream.ColumnBinary(stmt, 1);
       
  1238 	columnStream.Close();
       
  1239 	TEST2(rc, KErrNone);
       
  1240 	
       
  1241 	//...and the leaving version of ColumnBinary() 
       
  1242 	TRAP(rc, columnStream.ColumnBinaryL(stmt, 1));
       
  1243 	TEST2(rc, KErrNone);
       
  1244 	
       
  1245 	TInt size = stmt.ColumnSize(1);
       
  1246 	TPtr8 colData = buf->Des();
       
  1247 	TRAP(rc, columnStream.ReadL(colData, size));
       
  1248 	columnStream.Close();
       
  1249 	TEST2(rc, KErrNone);
       
  1250 	
       
  1251 	//Check the column value
       
  1252 	for(i=0;i<KDataLen;++i)
       
  1253 		{
       
  1254 		TUint8 val = colData[i];
       
  1255 		TEST(val = KHexVal1);	
       
  1256 		}
       
  1257 
       
  1258 	//Move on row 2
       
  1259 	rc = stmt.Next();
       
  1260 	TEST2(rc, KSqlAtRow);
       
  1261 
       
  1262 	//Read row 2 using ColumnBinary(TInt aColumnIndex, TPtrC8& aPtr).
       
  1263 	TPtrC8 colDataPtr;
       
  1264 	rc = stmt.ColumnBinary(1, colDataPtr);
       
  1265 	TEST2(rc, KErrNone);
       
  1266 	
       
  1267 	//Check the column value
       
  1268 	for(i=0;i<KDataLen;++i)
       
  1269 		{
       
  1270 		TUint8 val = colDataPtr[i];
       
  1271 		TEST(val = KHexVal2);	
       
  1272 		}
       
  1273 
       
  1274 	//Read row 2 using ColumnBinary(TInt aColumnIndex, TDes8& aDest).
       
  1275 	rc = stmt.ColumnBinary(1, colData);
       
  1276 	TEST2(rc, KErrNone);
       
  1277 	
       
  1278 	//Check the column value
       
  1279 	for(i=0;i<KDataLen;++i)
       
  1280 		{
       
  1281 		TUint8 val = colData[i];
       
  1282 		TEST(val = KHexVal2);	
       
  1283 		}
       
  1284 	
       
  1285 	//Read row 2 using a stream
       
  1286 	colData.Zero();
       
  1287 	rc = columnStream.ColumnBinary(stmt, 1);
       
  1288 	TEST2(rc, KErrNone);
       
  1289 	size = stmt.ColumnSize(1);
       
  1290 	TRAP(rc, columnStream.ReadL(colData, size));
       
  1291 	columnStream.Close();
       
  1292 	TEST2(rc, KErrNone);
       
  1293 	
       
  1294 	//Check the column value
       
  1295 	for(i=0;i<KDataLen;++i)
       
  1296 		{
       
  1297 		TUint8 val = colData[i];
       
  1298 		TEST(val = KHexVal2);	
       
  1299 		}
       
  1300 		
       
  1301 	//Read the column value using RSqlStatement::ColumnBinary(). 
       
  1302 	//The recipient buffer max length is smaller than the column value length.
       
  1303 	rc = stmt.Reset();
       
  1304 	TEST2(rc, KErrNone);
       
  1305 	rc = stmt.Next();
       
  1306 	TEST2(rc, KSqlAtRow);
       
  1307 	const TInt KBufMaxLen = 100;
       
  1308 	TBuf8<KBufMaxLen> colBuf;
       
  1309 	rc = stmt.ColumnBinary(1, colBuf);
       
  1310 	TEST2(rc, KErrOverflow);
       
  1311 	//Check the column value
       
  1312 	for(i=0;i<KBufMaxLen;++i)
       
  1313 		{
       
  1314 		TUint8 val = colBuf[i];
       
  1315 		TEST(val = KHexVal2);	
       
  1316 		}
       
  1317 	
       
  1318 	stmt.Close();
       
  1319 	
       
  1320 	//Deallocate buf
       
  1321 	delete buf; 
       
  1322 	buf = NULL;
       
  1323 	
       
  1324 	db.Close();
       
  1325 
       
  1326 	rc = RSqlDatabase::Delete(KTestDbName1);
       
  1327 	TEST2(rc, KErrNone);
       
  1328 	}
       
  1329 
       
  1330 /**
       
  1331 @SYMTestCaseID			SYSLIB-SQL-CT-1608
       
  1332 @SYMTestCaseDesc		Setting long text parameter values test.
       
  1333 @SYMTestPriority		High
       
  1334 @SYMTestActions			RSqlParamWriteStream::BindText() test.
       
  1335 @SYMTestExpectedResults Test must not fail
       
  1336 @SYMREQ					REQ5792
       
  1337                         REQ5793
       
  1338 */	
       
  1339 void TextParameterStreamTest()
       
  1340 	{
       
  1341 	RSqlDatabase db;	
       
  1342 	TInt rc = db.Create(KTestDbName1);
       
  1343 	TEST2(rc, KErrNone);
       
  1344 
       
  1345 	enum {KSqlBufSize = 64};
       
  1346 	
       
  1347 	//Create a table
       
  1348 	_LIT8(KSqlStmt1, "CREATE TABLE A(Fld1 INTEGER, Fld2 TEXT);");
       
  1349 	ExecSqlStmtOnDb<TDesC8, TBuf8<KSqlBufSize> >(db, KSqlStmt1(), KErrNone); 
       
  1350 
       
  1351 	const TInt KTextLen = 3001;
       
  1352 
       
  1353 	_LIT(KSqlStmt2, "INSERT INTO A(Fld1, Fld2) VALUES(");
       
  1354 	
       
  1355 	//Allocate a buffer for the SQL statement.
       
  1356 	HBufC* buf = HBufC::New(KSqlStmt2().Length() + KTextLen + 10);
       
  1357 	TEST(buf != NULL);
       
  1358 
       
  1359 	TPtr sql = buf->Des();
       
  1360 	
       
  1361 	//Insert a row
       
  1362 
       
  1363 	const TChar KChar('g');
       
  1364 	sql.Copy(KSqlStmt2);
       
  1365 	sql.Append(_L("1, '"));
       
  1366 	TInt i;
       
  1367 	for(i=0;i<KTextLen;++i)
       
  1368 		{
       
  1369 		sql.Append(KChar);
       
  1370 		}
       
  1371 	sql.Append(_L("')"));
       
  1372 
       
  1373 	rc = db.Exec(sql);
       
  1374 	TEST2(rc, 1);
       
  1375 
       
  1376 	//Prepare parametrized SQL statement
       
  1377 	_LIT8(KSqlStmt3, "SELECT * FROM A WHERE Fld2 = :Val");
       
  1378 	RSqlStatement stmt = PrepareSqlStmt<TDesC, TBuf<KSqlBufSize> >(db, KSqlStmt3, KErrNone);
       
  1379 
       
  1380 	//Open the parameter stream
       
  1381 	RSqlParamWriteStream paramStream;
       
  1382 	rc = paramStream.BindText(stmt, 0);
       
  1383 	paramStream.Close();
       
  1384 	TEST2(rc, KErrNone);
       
  1385 
       
  1386 	//...and the leaving version of BindText()
       
  1387 	TRAP(rc, paramStream.BindTextL(stmt, 0));
       
  1388 	TEST2(rc, KErrNone);
       
  1389 	
       
  1390 	//Prepare and set the parameter value
       
  1391 	TPtr val = buf->Des();
       
  1392 	val.Zero();
       
  1393 	for(i=0;i<KTextLen;++i)
       
  1394 		{
       
  1395 		val.Append(KChar);
       
  1396 		}
       
  1397 	TRAP(rc, (paramStream.WriteL(val), paramStream.CommitL()));
       
  1398 	TEST2(rc, KErrNone);
       
  1399 
       
  1400 	//Move on row 1
       
  1401 	rc = stmt.Next();
       
  1402 	TEST2(rc, KSqlAtRow);
       
  1403 
       
  1404 	paramStream.Close();
       
  1405 
       
  1406 	//Read the row using ColumnText(TInt aColumnIndex, TPtrC& aPtr).
       
  1407 	TPtrC colDataPtr;
       
  1408 	rc = stmt.ColumnText(1, colDataPtr);
       
  1409 	TEST2(rc, KErrNone);
       
  1410 	
       
  1411 	//Check the column value
       
  1412 	for(i=0;i<KTextLen;++i)
       
  1413 		{
       
  1414 		TEST(colDataPtr[i] == (TUint8)KChar);	
       
  1415 		}
       
  1416 	
       
  1417 	stmt.Close();
       
  1418 
       
  1419 	//Deallocate buf
       
  1420 	delete buf; 
       
  1421 	buf = NULL;
       
  1422 
       
  1423 	///////////////////////////////////////////////////////////////////////////////////////////////	
       
  1424 	//Open a "short" text parameter. The streaming API should work with "short" text parameters too.
       
  1425 	_LIT(KTextVal, "U012");
       
  1426 
       
  1427 	//Delete all records
       
  1428 	_LIT8(KSqlStmt4, "DELETE FROM A");
       
  1429 	ExecSqlStmtOnDb<TDesC8, TBuf8<KSqlBufSize> >(db, KSqlStmt4(), KErrNone); 
       
  1430 
       
  1431 	//Prepare INSERT SQL statement.	
       
  1432 	_LIT8(KSqlStmt5, "INSERT INTO A(Fld1, Fld2) VALUES(1, :Val)");
       
  1433 	stmt = PrepareSqlStmt<TDesC, TBuf<KSqlBufSize> >(db, KSqlStmt5, KErrNone);
       
  1434 
       
  1435 	//Open the parameter stream
       
  1436 	rc = paramStream.BindText(stmt, 0);
       
  1437 	TEST2(rc, KErrNone);
       
  1438 
       
  1439 	//Prepare and set the parameter value
       
  1440 	TRAP(rc, (paramStream.WriteL(KTextVal), paramStream.CommitL()));
       
  1441 	TEST2(rc, KErrNone);
       
  1442 
       
  1443 	//Execute the prepared SQL statement
       
  1444 	rc = stmt.Exec();
       
  1445 	TEST2(rc, 1);
       
  1446 
       
  1447 	paramStream.Close();
       
  1448 	stmt.Close();
       
  1449 	
       
  1450 	//Prepare SELECT SQL statement
       
  1451 	_LIT8(KSqlStmt6, "SELECT * FROM A WHERE Fld1 = 1");
       
  1452 	stmt = PrepareSqlStmt<TDesC, TBuf<KSqlBufSize> >(db, KSqlStmt6, KErrNone);
       
  1453 
       
  1454 	//Move on row 1
       
  1455 	rc = stmt.Next();
       
  1456 	TEST2(rc, KSqlAtRow);
       
  1457 
       
  1458 	///////////////////////////////////////////////////////////////////////////////////////////////	
       
  1459 	//Open a stream for a "short" text column. The streaming API should work with "short" text columns too.
       
  1460 
       
  1461 	TBuf<20> columnVal;
       
  1462 	RSqlColumnReadStream columnStream;
       
  1463 	rc = columnStream.ColumnText(stmt, 1);
       
  1464 	TEST2(rc, KErrNone);
       
  1465 	TInt size = stmt.ColumnSize(1);
       
  1466 	TRAP(rc, columnStream.ReadL(columnVal, size));
       
  1467 	columnStream.Close();
       
  1468 	TEST2(rc, KErrNone);
       
  1469 	
       
  1470 	//Check the column value
       
  1471 	TEST(columnVal == KTextVal);
       
  1472 	
       
  1473 	stmt.Close();
       
  1474 	
       
  1475 	db.Close();
       
  1476 
       
  1477 	rc = RSqlDatabase::Delete(KTestDbName1);
       
  1478 	TEST2(rc, KErrNone);
       
  1479 	}
       
  1480 
       
  1481 /**
       
  1482 @SYMTestCaseID			SYSLIB-SQL-CT-1622
       
  1483 @SYMTestCaseDesc		Setting long binary parameter values test.
       
  1484 @SYMTestPriority		High
       
  1485 @SYMTestActions			RSqlParamWriteStream::BindBinary() test.
       
  1486 @SYMTestExpectedResults Test must not fail
       
  1487 @SYMREQ					REQ5792
       
  1488                         REQ5793
       
  1489 */	
       
  1490 void BinaryParameterStreamTest()
       
  1491 	{
       
  1492 	//Create a test database
       
  1493 	RSqlDatabase db;	
       
  1494 	TInt rc = db.Create(KTestDbName1);
       
  1495 	TEST2(rc, KErrNone);
       
  1496 
       
  1497 	enum {KSqlBufSize = 64};
       
  1498 	
       
  1499 	//Create a table
       
  1500 	_LIT8(KSqlStmt1, "CREATE TABLE A(Fld1 INTEGER, Fld2 BLOB);");
       
  1501 	ExecSqlStmtOnDb<TDesC8, TBuf8<KSqlBufSize> >(db, KSqlStmt1(), KErrNone); 
       
  1502 
       
  1503 	const TInt KDataLen = 3731;
       
  1504 
       
  1505 	_LIT8(KSqlStmt2, "INSERT INTO A(Fld1, Fld2) VALUES(");
       
  1506 	
       
  1507 	//Allocate a buffer for the SQL statement.
       
  1508 	HBufC8* buf = HBufC8::New(KSqlStmt2().Length() + KDataLen * 2 + 10);//"* 2" - SQL statement with HEX values
       
  1509 	TEST(buf != NULL);
       
  1510 
       
  1511 	TPtr8 sql = buf->Des();
       
  1512 	
       
  1513 	//Insert a row
       
  1514 
       
  1515 	const TUint8 KHexVal = 0xD3;
       
  1516 	_LIT8(KHexValStr, "D3");
       
  1517 	sql.Copy(KSqlStmt2);
       
  1518 	sql.Append(_L8("1, x'"));
       
  1519 	TInt i;
       
  1520 	for(i=0;i<KDataLen;++i)
       
  1521 		{
       
  1522 		sql.Append(KHexValStr);
       
  1523 		}
       
  1524 	sql.Append(_L8("')"));
       
  1525 
       
  1526 	rc = db.Exec(sql);
       
  1527 	TEST2(rc, 1);
       
  1528 
       
  1529 	//Prepare parametrized SQL statement
       
  1530 	_LIT8(KSqlStmt3, "SELECT * FROM A WHERE Fld2 = :Val");
       
  1531 	RSqlStatement stmt = PrepareSqlStmt<TDesC8, TBuf8<KSqlBufSize> >(db, KSqlStmt3, KErrNone);
       
  1532 
       
  1533 	//Open the parameter stream
       
  1534 	RSqlParamWriteStream paramStream;
       
  1535 	rc = paramStream.BindBinary(stmt, 0);
       
  1536 	TEST2(rc, KErrNone);
       
  1537 
       
  1538 	//Prepare and set the parameter value (NULL parameter value)
       
  1539 	TPtr8 prmVal = buf->Des();
       
  1540 	prmVal.SetLength(0);
       
  1541 	
       
  1542 	TRAP(rc, (paramStream.WriteL(prmVal), paramStream.CommitL()));
       
  1543 	TEST2(rc, KErrNone);
       
  1544 
       
  1545 	rc = stmt.Next();
       
  1546 	TEST2(rc, KSqlAtEnd);
       
  1547 	
       
  1548 	paramStream.Close();
       
  1549 	stmt.Reset();
       
  1550 	
       
  1551 	//Prepare and set the parameter value (non-NULL binary value)
       
  1552 	rc = paramStream.BindBinary(stmt, 0);
       
  1553 	TEST2(rc, KErrNone);
       
  1554 	
       
  1555 	prmVal.SetLength(KDataLen);
       
  1556 	for(i=0;i<KDataLen;++i)
       
  1557 		{
       
  1558 		prmVal[i] = KHexVal;
       
  1559 		}
       
  1560 	TRAP(rc, (paramStream.WriteL(prmVal), paramStream.CommitL()));
       
  1561 	TEST2(rc, KErrNone);
       
  1562 
       
  1563 	//Move on row 1
       
  1564 	rc = stmt.Next();
       
  1565 	TEST2(rc, KSqlAtRow);
       
  1566 
       
  1567 	paramStream.Close();
       
  1568 
       
  1569 	//Read the row using ColumnBinary(TInt aColumnIndex, TPtrC8& aPtr).
       
  1570 	TPtrC8 colDataPtr;
       
  1571 	rc = stmt.ColumnBinary(1, colDataPtr);
       
  1572 	TEST2(rc, KErrNone);
       
  1573 	
       
  1574 	//Check the column value
       
  1575 	for(i=0;i<KDataLen;++i)
       
  1576 		{
       
  1577 		TUint8 byte = colDataPtr[i];
       
  1578 		TEST(byte == KHexVal);	
       
  1579 		}
       
  1580 	
       
  1581 	stmt.Close();
       
  1582 
       
  1583 	//Deallocate buf
       
  1584 	delete buf; 
       
  1585 	buf = NULL;
       
  1586 
       
  1587 
       
  1588 	///////////////////////////////////////////////////////////////////////////////////////////////	
       
  1589 	//Open a "short" binary parameter. The streaming API should work with "short" binary parameters too.
       
  1590 	_LIT8(KBinVal, "\x1\x2\x3\x4");
       
  1591 
       
  1592 	//Delete all records
       
  1593 	_LIT8(KSqlStmt4, "DELETE FROM A");
       
  1594 	ExecSqlStmtOnDb<TDesC8, TBuf8<KSqlBufSize> >(db, KSqlStmt4(), KErrNone); 
       
  1595 
       
  1596 	//Prepare INSERT SQL statement.	
       
  1597 	_LIT8(KSqlStmt5, "INSERT INTO A(Fld1, Fld2) VALUES(1, :Val)");
       
  1598 	stmt = PrepareSqlStmt<TDesC, TBuf<KSqlBufSize> >(db, KSqlStmt5, KErrNone);
       
  1599 
       
  1600 	//Open the parameter stream
       
  1601 	rc = paramStream.BindBinary(stmt, 0);
       
  1602 	TEST2(rc, KErrNone);
       
  1603 
       
  1604 	//Prepare and set the parameter value
       
  1605 	TRAP(rc, (paramStream.WriteL(KBinVal), paramStream.CommitL()));
       
  1606 	TEST2(rc, KErrNone);
       
  1607 
       
  1608 	//Execute the prepared SQL statement
       
  1609 	rc = stmt.Exec();
       
  1610 	TEST2(rc, 1);
       
  1611 
       
  1612 	paramStream.Close();
       
  1613 	stmt.Close();
       
  1614 	
       
  1615 	//Prepare SELECT SQL statement
       
  1616 	_LIT8(KSqlStmt6, "SELECT * FROM A WHERE Fld1 = 1");
       
  1617 	stmt = PrepareSqlStmt<TDesC, TBuf<KSqlBufSize> >(db, KSqlStmt6, KErrNone);
       
  1618 
       
  1619 	//Move on row 1
       
  1620 	rc = stmt.Next();
       
  1621 	TEST2(rc, KSqlAtRow);
       
  1622 
       
  1623 	///////////////////////////////////////////////////////////////////////////////////////////////	
       
  1624 	//Open a stream for a "short" binary column. The streaming API should work with "short" binary columns too.
       
  1625 
       
  1626 	TBuf8<20> columnVal;
       
  1627 	RSqlColumnReadStream columnStream;
       
  1628 	rc = columnStream.ColumnBinary(stmt, 1);
       
  1629 	TEST2(rc, KErrNone);
       
  1630 	TInt size = stmt.ColumnSize(1);
       
  1631 	TRAP(rc, columnStream.ReadL(columnVal, size));
       
  1632 	columnStream.Close();
       
  1633 	TEST2(rc, KErrNone);
       
  1634 	
       
  1635 	//Check the column value
       
  1636 	TEST(columnVal == KBinVal);
       
  1637 	
       
  1638 	stmt.Close();
       
  1639 		
       
  1640 	db.Close();
       
  1641 
       
  1642 	rc = RSqlDatabase::Delete(KTestDbName1);
       
  1643 	TEST2(rc, KErrNone);
       
  1644 	}
       
  1645 
       
  1646 /**
       
  1647 @SYMTestCaseID			SYSLIB-SQL-CT-1634
       
  1648 @SYMTestCaseDesc		RSqlStatement test - nameless parameter.
       
  1649 						Tests RSqlStatement behaviour if the prepared statement has nameless parameters.
       
  1650 @SYMTestPriority		High
       
  1651 @SYMTestActions			RSqlStatement test - nameless parameter.
       
  1652 @SYMTestExpectedResults Test must not fail
       
  1653 @SYMREQ					REQ5792
       
  1654                         REQ5793
       
  1655 */	
       
  1656 void NamelessParameterTest()
       
  1657 	{
       
  1658 	//Create a test database
       
  1659 	RSqlDatabase db;	
       
  1660 	TInt rc = db.Create(KTestDbName1);
       
  1661 	TEST2(rc, KErrNone);
       
  1662 
       
  1663 	rc = db.Exec(_L("CREATE TABLE A(F1 INTEGER, F2 INTEGER, F3 INTEGER)"));
       
  1664 	TEST(rc >= 0);
       
  1665 
       
  1666 	RSqlStatement stmt;
       
  1667 	rc = stmt.Prepare(db, _L("SELECT * FROM A WHERE F1 = ? AND F2 = ? AND F3 = :Val"));
       
  1668 	TEST2(rc, KErrNone);
       
  1669 
       
  1670 	TEST(stmt.ParameterIndex(_L("?0")) == 0);
       
  1671 	TEST(stmt.ParameterIndex(_L("?1")) == 1);
       
  1672 	TEST(stmt.ParameterIndex(_L(":VAL")) == 2);
       
  1673 		
       
  1674 	stmt.Close();
       
  1675 
       
  1676 	db.Close();
       
  1677 	rc = RSqlDatabase::Delete(KTestDbName1);
       
  1678 	TEST2(rc, KErrNone);
       
  1679 	}
       
  1680 
       
  1681 //Reads a SQL file and returns the file content as HBUFC string.
       
  1682 //The caller is responsible for destroying the returned HBUFC object.
       
  1683 template <class HBUFC> HBUFC* ReadSqlScript(const TDesC& aSqlFileName)
       
  1684 	{
       
  1685 	RFs fs;
       
  1686 	TEST2(fs.Connect(), KErrNone);
       
  1687 	
       
  1688 	RFile file;
       
  1689 	TEST2(file.Open(fs, aSqlFileName, EFileRead), KErrNone);
       
  1690 	
       
  1691 	TInt size = 0;
       
  1692 	TEST2(file.Size(size), KErrNone);
       
  1693 	
       
  1694 	HBufC8* sql = HBufC8::New(size);
       
  1695 	TEST(sql != NULL);
       
  1696 	
       
  1697 	TPtr8 ptr = sql->Des();
       
  1698 	TEST2(file.Read(ptr, size), KErrNone);
       
  1699 
       
  1700 	file.Close();
       
  1701 	fs.Close();
       
  1702 	
       
  1703 	HBUFC* sql2 = HBUFC::New(size);
       
  1704 	TEST(sql2 != NULL);
       
  1705 	sql2->Des().Copy(sql->Des());
       
  1706 	delete sql;
       
  1707 	
       
  1708 	return sql2;
       
  1709 	}
       
  1710 //Explicit ReadSqlScript() template instantiations.
       
  1711 template HBufC8* ReadSqlScript<HBufC8>(const TDesC&);
       
  1712 template HBufC16* ReadSqlScript<HBufC16>(const TDesC&);
       
  1713 
       
  1714 //Searches for the next aCommitStr appearance in aSqlScript string and returns a PTRC object holding
       
  1715 //the SQL strings from the beginning of aSqlScript till the aCommitStr (including it).
       
  1716 template <class PTRC, class DESC> PTRC GetNextTrans(PTRC& aSqlScript, const DESC& aCommitStr)
       
  1717 	{
       
  1718 	PTRC res(NULL, 0);
       
  1719 	TInt pos = aSqlScript.FindF(aCommitStr);
       
  1720 	if(pos >= 0)
       
  1721 		{
       
  1722 		pos += aCommitStr.Length();
       
  1723 		res.Set(aSqlScript.Left(pos));
       
  1724 		aSqlScript.Set(aSqlScript.Mid(pos));
       
  1725 		}
       
  1726 	return res;
       
  1727 	}
       
  1728 //Explicit GetNextTrans() template instantiations.
       
  1729 template TPtrC8 GetNextTrans<TPtrC8, TDesC8>(TPtrC8&, const TDesC8&);
       
  1730 template TPtrC16 GetNextTrans<TPtrC16, TDesC16>(TPtrC16&, const TDesC16&);
       
  1731 
       
  1732 //Creates aDb database schema.
       
  1733 void CreateDbSchema(RSqlDatabase& aDb)
       
  1734 	{
       
  1735 	HBufC8* createDbScript = ReadSqlScript<HBufC8>(KCreateDbScript());
       
  1736 	TInt err = aDb.Exec(createDbScript->Des());
       
  1737 	TEST(err >= 0);	
       
  1738 	delete createDbScript;
       
  1739 	}
       
  1740 
       
  1741 /**
       
  1742 @SYMTestCaseID			SYSLIB-SQL-CT-1768
       
  1743 @SYMTestCaseDesc		The test creates a database.
       
  1744 						Then the test executes 8-bit and 16-bit sql statements asynchronously
       
  1745 						(using asynchronous versions of RSqlDatabase::Exec() and RSqlStatement::Exec())
       
  1746 @SYMTestPriority		High
       
  1747 @SYMTestActions			SQL, Asynchronous sql statements execution.
       
  1748 @SYMTestExpectedResults Test must not fail
       
  1749 @SYMREQ					REQ5792
       
  1750                         REQ5793
       
  1751 */	
       
  1752 void AsyncTest()
       
  1753 	{
       
  1754 	//////////////////////////////////////////////////////////////////////////
       
  1755 	//Asynchronous execution, 8-bit sql statements
       
  1756 	//Create the database
       
  1757 	RSqlDatabase db;
       
  1758 	TInt err = db.Create(KTestDbName1);
       
  1759 	TEST2(err, KErrNone);
       
  1760 	CreateDbSchema(db);
       
  1761 	//Read the sql script file and execute the statements 
       
  1762 	HBufC8* fillDbScript1 = ReadSqlScript<HBufC8>(KFillDbScript());
       
  1763 	TPtrC8 ptr1(fillDbScript1->Des());
       
  1764 	TPtrC8 sql1(GetNextTrans<TPtrC8, TDesC8>(ptr1, KCommitStr8()));
       
  1765 	TRequestStatus status;
       
  1766 	db.Exec(sql1, status);
       
  1767     TEST2(status.Int(), KRequestPending);
       
  1768     User::WaitForRequest(status); 
       
  1769     TEST(status.Int() >= 0);
       
  1770 	delete fillDbScript1;
       
  1771 	//////////////////////////////////////////////////////////////////////////
       
  1772 	//Asynchronous execution, RSqlStatement::Exec().
       
  1773 	RSqlStatement stmt;
       
  1774 	
       
  1775 	err = stmt.Prepare(db, _L("UPDATE IDENTITYTABLE SET CM_FIRSTNAME=:V1"));
       
  1776     TEST2(err, KErrNone);
       
  1777     err = stmt.BindText(0, _L("+++first+++"));
       
  1778     TEST2(err, KErrNone);
       
  1779 	stmt.Exec(status);
       
  1780     TEST2(status.Int(), KRequestPending);
       
  1781     User::WaitForRequest(status); 
       
  1782     TEST(status.Int() >= 0);
       
  1783 	stmt.Close();
       
  1784 	
       
  1785 	err = stmt.Prepare(db, _L("UPDATE IDENTITYTABLE SET CM_FIRSTNAME='+++first+++'"));
       
  1786     TEST2(err, KErrNone);
       
  1787 	stmt.Exec(status);
       
  1788     TEST2(status.Int(), KRequestPending);
       
  1789     User::WaitForRequest(status); 
       
  1790     TEST(status.Int() >= 0);
       
  1791 	stmt.Close();
       
  1792 	
       
  1793 	//Verify the UPDATE operation
       
  1794 	err = stmt.Prepare(db, _L("SELECT COUNT(*) FROM identitytable WHERE cm_firstname = '+++first+++'"));
       
  1795     TEST2(err, KErrNone);
       
  1796     err = stmt.Next();
       
  1797     TEST2(err, KSqlAtRow);
       
  1798     TInt cnt = stmt.ColumnInt(0);
       
  1799     TEST2(cnt, 64);
       
  1800     stmt.Close();
       
  1801 	//Close and delete the database
       
  1802 	db.Close();
       
  1803 	err = RSqlDatabase::Delete(KTestDbName1);
       
  1804 	TEST2(err, KErrNone);
       
  1805 	//////////////////////////////////////////////////////////////////////////
       
  1806 	//Asynchronous execution, 16-bit sql statements
       
  1807 	//Create the database
       
  1808 	err = db.Create(KTestDbName1);
       
  1809 	TEST2(err, KErrNone);
       
  1810 	CreateDbSchema(db);
       
  1811 	//Read the sql script file and execute the statements 
       
  1812 	HBufC16* fillDbScript2 = ReadSqlScript<HBufC16>(KFillDbScript());
       
  1813 	TPtrC16 ptr2(fillDbScript2->Des());
       
  1814 	TPtrC16 sql2(GetNextTrans<TPtrC16, TDesC16>(ptr2, KCommitStr16()));
       
  1815 	db.Exec(sql2, status);
       
  1816     TEST2(status.Int(), KRequestPending);
       
  1817     User::WaitForRequest(status); 
       
  1818     TEST(status.Int() >= 0);
       
  1819 	delete fillDbScript2;
       
  1820 	//Close and delete the database
       
  1821 	db.Close();
       
  1822 	err = RSqlDatabase::Delete(KTestDbName1);
       
  1823 	TEST2(err, KErrNone);
       
  1824 	}
       
  1825 
       
  1826 /**
       
  1827 @SYMTestCaseID			SYSLIB-SQL-CT-1816
       
  1828 @SYMTestCaseDesc		RSqlDatabase::Size() test.
       
  1829 						Call RSqlDatabase::Size() in various situations and check that the call does not fail.
       
  1830 @SYMTestPriority		High
       
  1831 @SYMTestActions			Call RSqlDatabase::Size() in various situations and check that the call does not fail.
       
  1832 @SYMTestExpectedResults Test must not fail
       
  1833 @SYMREQ					REQ7141
       
  1834 */
       
  1835 void SizeTest()
       
  1836 	{
       
  1837 	const TInt KTestDbCnt = 3;
       
  1838 	TPtrC dbNames[KTestDbCnt];
       
  1839 	dbNames[0].Set(KTestDbName1); //Non-secure shared database
       
  1840 	dbNames[1].Set(KTestDbName9); //Private database
       
  1841 	dbNames[2].Set(KTestDbName7); //Secure shared database
       
  1842 	
       
  1843 	TSecurityPolicy defaultPolicy(TSecurityPolicy::EAlwaysPass);
       
  1844 	RSqlSecurityPolicy securityPolicy;
       
  1845 	TInt rc = securityPolicy.Create(defaultPolicy);
       
  1846 	TEST2(rc, KErrNone);
       
  1847 	
       
  1848 	for(TInt i=0;i<KTestDbCnt;++i)
       
  1849 		{
       
  1850 		(void)RSqlDatabase::Delete(dbNames[i]);
       
  1851 		RSqlDatabase db;
       
  1852 		TInt rc = i == 2 ? db.Create(dbNames[i], securityPolicy) : db.Create(dbNames[i]);
       
  1853 		TEST2(rc, KErrNone);
       
  1854 		//Check database size
       
  1855 		TInt size1 = db.Size();
       
  1856 		TEST(size1 >= 0);
       
  1857 		//Insert some data and check the size again
       
  1858 		rc = db.Exec(_L("CREATE TABLE A(Id INTEGER, T TEXT)"));
       
  1859 		TEST(rc >= 0);
       
  1860 		rc = db.Exec(_L("INSERT INTO A VALUES(1, '1111111')"));
       
  1861 		TEST(rc >= 0);
       
  1862 		rc = db.Exec(_L("INSERT INTO A VALUES(2, '22222222222222')"));
       
  1863 		TEST(rc >= 0);
       
  1864 		TInt size2 = db.Size();
       
  1865 		TEST(size2 > size1);
       
  1866 		//Check the database size in a transaction
       
  1867 		rc = db.Exec(_L("BEGIN"));
       
  1868 		TEST(rc >= 0);
       
  1869 		TInt size3 = db.Size();
       
  1870 		TEST(size3 == size2);
       
  1871 		rc = db.Exec(_L("INSERT INTO A VALUES(3, '3333333333333333333333333333333333333333333333333333333333333')"));
       
  1872 		TEST(rc >= 0);
       
  1873 		rc = db.Exec(_L("INSERT INTO A VALUES(4, '4444444444444444444444444444444444444444444444444444444444444')"));
       
  1874 		TEST(rc >= 0);
       
  1875 		rc = db.Exec(_L("INSERT INTO A VALUES(5, '5555555555555555555555555555555555555555555555555555555555555')"));
       
  1876 		TEST(rc >= 0);
       
  1877 		TInt size4 = db.Size();
       
  1878 		TEST(size4 == size2);
       
  1879 		rc = db.Exec(_L("COMMIT"));
       
  1880 		TEST(rc >= 0);
       
  1881 		TInt size5 = db.Size();
       
  1882 		TEST(size5 == size2);
       
  1883 		//Cleanup
       
  1884 		db.Close();
       
  1885 		rc = RSqlDatabase::Delete(dbNames[i]);
       
  1886 		TEST2(rc, KErrNone);
       
  1887 		}
       
  1888 	securityPolicy.Close();
       
  1889 	}
       
  1890 
       
  1891 /**
       
  1892 @SYMTestCaseID			SYSLIB-SQL-CT-1817
       
  1893 @SYMTestCaseDesc		RSqlDatabase::InTransaction() test.
       
  1894 						Call RSqlDatabase::InTransaction() in various situations and check that the method reports 
       
  1895 						database's transaction state correctly.
       
  1896 @SYMTestPriority		High
       
  1897 @SYMTestActions			Call RSqlDatabase::InTransaction() in various situations and check that the method reports 
       
  1898 						database's transaction state correctly.
       
  1899 @SYMTestExpectedResults Test must not fail
       
  1900 @SYMREQ					REQ7141
       
  1901 */
       
  1902 void InTransactionTest()
       
  1903 	{
       
  1904 	(void)RSqlDatabase::Delete(KTestDbName1);
       
  1905 	RSqlDatabase db;
       
  1906 	TInt rc = db.Create(KTestDbName1);
       
  1907 	TEST2(rc, KErrNone);
       
  1908 	//
       
  1909 	TBool state = db.InTransaction();
       
  1910 	TEST(!state);
       
  1911 	rc = db.Exec(_L("BEGIN"));
       
  1912 	TEST(rc >= 0);
       
  1913 	state = db.InTransaction();
       
  1914 	TEST(!!state);
       
  1915 	rc = db.Exec(_L("ROLLBACK"));
       
  1916 	TEST(rc >= 0);
       
  1917 	state = db.InTransaction();
       
  1918 	TEST(!state);
       
  1919 	//Cleanup
       
  1920 	db.Close();
       
  1921 	rc = RSqlDatabase::Delete(KTestDbName1);
       
  1922 	TEST2(rc, KErrNone);
       
  1923 	}
       
  1924 
       
  1925 /**
       
  1926 @SYMTestCaseID			SYSLIB-SQL-UT-4039
       
  1927 @SYMTestCaseDesc		RSqlDatabase::Size(TSize&) functional test.
       
  1928 						The test creates a non-secure shared database, private database, secure shared database
       
  1929 						and tests that the new RSqlDatabase::Size(TSize&) can be used on the created databases.
       
  1930 @SYMTestPriority		High
       
  1931 @SYMTestActions			RSqlDatabase::Size(TSize&) functional test.
       
  1932 @SYMTestExpectedResults Test must not fail
       
  1933 @SYMREQ					REQ10407
       
  1934 */
       
  1935 void SizeTest2()
       
  1936 	{
       
  1937 	const TInt KTestDbCnt = 3;
       
  1938 	TPtrC dbNames[KTestDbCnt];
       
  1939 	dbNames[0].Set(KTestDbName1); //Non-secure shared database
       
  1940 	dbNames[1].Set(KTestDbName9); //Private database
       
  1941 	dbNames[2].Set(KTestDbName7); //Secure shared database
       
  1942 	
       
  1943 	TSecurityPolicy defaultPolicy(TSecurityPolicy::EAlwaysPass);
       
  1944 	RSqlSecurityPolicy securityPolicy;
       
  1945 	TInt rc = securityPolicy.Create(defaultPolicy);
       
  1946 	TEST2(rc, KErrNone);
       
  1947 	
       
  1948 	_LIT8(KConfig, "compaction=background");
       
  1949 	
       
  1950 	for(TInt i=0;i<KTestDbCnt;++i)
       
  1951 		{
       
  1952 		(void)RSqlDatabase::Delete(dbNames[i]);
       
  1953 		RSqlDatabase db;
       
  1954 		TInt rc = i == 2 ? db.Create(dbNames[i], securityPolicy, &KConfig) : db.Create(dbNames[i], &KConfig);
       
  1955 		TEST2(rc, KErrNone);
       
  1956 		//Check database size
       
  1957 		RSqlDatabase::TSize size;
       
  1958 		rc = db.Size(size);
       
  1959 		TEST2(rc, KErrNone);
       
  1960 		TEST(size.iSize > 0);
       
  1961 		TEST2(size.iFree, 0);
       
  1962 		//Insert some data and check the size again
       
  1963 		rc = db.Exec(_L("CREATE TABLE A(Id INTEGER, T TEXT)"));
       
  1964 		TEST(rc >= 0);
       
  1965 		const TInt KTestRecCnt = 50;
       
  1966 		rc = db.Exec(_L("BEGIN"));
       
  1967 		TEST(rc >= 0);
       
  1968 		for(TInt k=0;k<KTestRecCnt;++k)
       
  1969 			{
       
  1970 			TBuf<100> sql;
       
  1971 			sql.Format(_L("INSERT INTO A VALUES(%d, 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')"), k + 1);
       
  1972 			rc = db.Exec(sql);
       
  1973 			TEST2(rc, 1);
       
  1974 			}
       
  1975 		rc = db.Exec(_L("COMMIT"));
       
  1976 		TEST(rc >= 0);
       
  1977 		rc = db.Size(size);
       
  1978 		TEST2(rc, KErrNone);
       
  1979 		TEST(size.iSize > 0);
       
  1980 		TEST2(size.iFree, 0);
       
  1981 		//Delete the records and check the size again.
       
  1982 		rc = db.Exec(_L("DELETE FROM A"));
       
  1983 		rc = db.Size(size);
       
  1984 		TEST2(rc, KErrNone);
       
  1985 		TEST(size.iSize > 0);
       
  1986 		TEST(size.iFree > 0);
       
  1987 		//Cleanup
       
  1988 		db.Close();
       
  1989 		rc = RSqlDatabase::Delete(dbNames[i]);
       
  1990 		TEST2(rc, KErrNone);
       
  1991 		}
       
  1992 	securityPolicy.Close();
       
  1993 	}
       
  1994 
       
  1995 /**
       
  1996 @SYMTestCaseID			SYSLIB-SQL-UT-4040
       
  1997 @SYMTestCaseDesc		RSqlDatabase::Size(TSize&) on an attached database - functional test.
       
  1998 						The test creates a database and attaches another database. Then the test
       
  1999 						verifies that the new RSqlDatabase::Size(TSize&) method can be used on the attached 
       
  2000 						database. The test also calls the new method with an invalid attached database
       
  2001 						name and check that an appropriate error is reported.
       
  2002 @SYMTestPriority		High
       
  2003 @SYMTestActions			RSqlDatabase::Size(TSize&) on an attached database - functional test.
       
  2004 @SYMTestExpectedResults Test must not fail
       
  2005 @SYMREQ					REQ10407
       
  2006 */
       
  2007 void AttachedSizeTest2()
       
  2008 	{
       
  2009 	RSqlDatabase db;
       
  2010 	
       
  2011 	TInt err = db.Create(KTestDbName1);
       
  2012 	TEST2(err, KErrNone);
       
  2013 	err = db.Exec(_L("CREATE TABLE A(I INTEGER)"));
       
  2014 	TEST(err >= 0);
       
  2015 	db.Close();
       
  2016 	err = db.Create(KTestDbName8);
       
  2017 	TEST2(err, KErrNone);
       
  2018 	err = db.Exec(_L("CREATE TABLE B(J INTEGER)"));
       
  2019 	TEST(err >= 0);
       
  2020 	err = db.Exec(_L("BEGIN"));
       
  2021 	TEST(err >= 0);
       
  2022 	for(TInt i=0;i<1000;++i)
       
  2023 		{
       
  2024 		err = db.Exec(_L("INSERT INTO B(J) VALUES(1)"));
       
  2025 		TEST2(err, 1);
       
  2026 		}
       
  2027 	err = db.Exec(_L("COMMIT"));
       
  2028 	TEST(err >= 0);
       
  2029 	db.Close();
       
  2030 	
       
  2031 	err = db.Open(KTestDbName1);
       
  2032 	TEST2(err, KErrNone);
       
  2033 	_LIT(KAttachDbName, "B");
       
  2034 	err = db.Attach(KTestDbName8, KAttachDbName);
       
  2035 	TEST2(err, KErrNone);
       
  2036 	
       
  2037 	//Size(TSize&) - main and the attached databse
       
  2038 	RSqlDatabase::TSize	size1;
       
  2039 	err = db.Size(size1);
       
  2040 	TEST2(err, KErrNone);
       
  2041 	TEST(size1.iSize > 0);
       
  2042 	TEST(size1.iFree >= 0);
       
  2043 	RSqlDatabase::TSize	size2;
       
  2044 	err = db.Size(size2, KAttachDbName);
       
  2045 	TEST2(err, KErrNone);
       
  2046 	TEST(size2.iSize > 0);
       
  2047 	TEST(size2.iFree >= 0);
       
  2048 	TEST(size2.iSize > size1.iSize);
       
  2049 
       
  2050 	//Very long attached database name
       
  2051 	TBuf<KMaxFileName + 10> longDbName;
       
  2052 	longDbName.SetLength(longDbName.MaxLength());
       
  2053 	err = db.Size(size1, longDbName);
       
  2054 	TEST2(err, KErrBadName);
       
  2055 	
       
  2056 	//An attempt to get the size of a non-existing attached database
       
  2057 	err = db.Size(size1, _L("TheDbDoesNotExist"));
       
  2058 	TEST2(err, KSqlErrGeneral);
       
  2059 	TPtrC msg = db.LastErrorMessage();
       
  2060 	TheTest.Printf(_L("Non-existing attached database, error message: %S\r\n"), &msg);
       
  2061 	
       
  2062 	err = db.Detach(KAttachDbName);
       
  2063 	TEST2(err, KErrNone);
       
  2064 	db.Close();
       
  2065 	(void)RSqlDatabase::Delete(KTestDbName8);
       
  2066 	(void)RSqlDatabase::Delete(KTestDbName1);
       
  2067 	}
       
  2068 
       
  2069 /**
       
  2070 @SYMTestCaseID			SYSLIB-SQL-UT-4041
       
  2071 @SYMTestCaseDesc		RSqlDatabase::Size(TSize&) and different compaction modes - functional test.
       
  2072 						The test creates databases using all possible compaction modes and
       
  2073 						verifies that the new RSqlDatabase::Size(TSize&) method can be used on these 
       
  2074 						databases without any problems.
       
  2075 @SYMTestPriority		High
       
  2076 @SYMTestActions			RSqlDatabase::Size(TSize&) and different compaction modes - functional test.
       
  2077 @SYMTestExpectedResults Test must not fail
       
  2078 @SYMREQ					REQ10407
       
  2079 */
       
  2080 void DiffCompactModeSize2Test()
       
  2081 	{
       
  2082 	_LIT8(KAutoCompaction, "compaction = auto");
       
  2083 	RSqlDatabase db;
       
  2084 	TInt err = db.Create(KTestDbName1, &KAutoCompaction);
       
  2085 	TEST2(err, KErrNone);
       
  2086 	err = db.Exec(_L("CREATE TABLE A(I INTEGER)"));
       
  2087 	TEST(err >= 0);
       
  2088 	RSqlDatabase::TSize	size;
       
  2089 	err = db.Size(size);
       
  2090 	TEST2(err, KErrNone);
       
  2091 	TEST(size.iSize > 0);
       
  2092 	TEST(size.iFree >= 0);
       
  2093 	db.Close();
       
  2094 	(void)RSqlDatabase::Delete(KTestDbName1);
       
  2095 	
       
  2096 	_LIT8(KManualCompaction, "compaction = manual");
       
  2097 	err = db.Create(KTestDbName1, &KManualCompaction);
       
  2098 	TEST2(err, KErrNone);
       
  2099 	err = db.Exec(_L("CREATE TABLE A(I INTEGER)"));
       
  2100 	TEST(err >= 0);
       
  2101 	err = db.Size(size);
       
  2102 	TEST2(err, KErrNone);
       
  2103 	TEST(size.iSize > 0);
       
  2104 	TEST(size.iFree >= 0);
       
  2105 	db.Close();
       
  2106 	(void)RSqlDatabase::Delete(KTestDbName1);
       
  2107 	
       
  2108 	_LIT8(KBackgroundCompaction, "compaction = background");
       
  2109 	err = db.Create(KTestDbName1, &KBackgroundCompaction);
       
  2110 	TEST2(err, KErrNone);
       
  2111 	err = db.Exec(_L("CREATE TABLE A(I INTEGER)"));
       
  2112 	TEST(err >= 0);
       
  2113 	err = db.Size(size);
       
  2114 	TEST2(err, KErrNone);
       
  2115 	TEST(size.iSize > 0);
       
  2116 	TEST(size.iFree >= 0);
       
  2117 	db.Close();
       
  2118 	(void)RSqlDatabase::Delete(KTestDbName1);
       
  2119 	}
       
  2120 
       
  2121 void DoTestsL()
       
  2122 	{
       
  2123 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1601 Create/Open/Close database tests "));	
       
  2124 	OpenCloseDatabaseTest();
       
  2125 
       
  2126 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1602 SetIsolationLevel() database tests "));	
       
  2127 	SetIsolationLevelTest();
       
  2128 	
       
  2129 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1603 Delete database tests "));	
       
  2130 	DeleteDatabaseTest();
       
  2131 	
       
  2132 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1640 Copy database tests "));	
       
  2133 	CopyDatabaseTest();
       
  2134 	
       
  2135 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1604 Operations with NULL column values "));
       
  2136 	NullColumnValues();
       
  2137 	
       
  2138 	enum {KSqlBufSize = 1024};
       
  2139 	
       
  2140 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1605 RSqlDatabase::Exec() test. 16-bit SQL strings. "));	
       
  2141 	ExecOnDbTest<TDesC, TBuf<KSqlBufSize> >();
       
  2142 	
       
  2143 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1605 RSqlDatabase::Exec() test. 8-bit SQL strings. "));	
       
  2144 	ExecOnDbTest<TDesC8, TBuf8<KSqlBufSize> >();
       
  2145 	
       
  2146 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1606 RSqlStatement test. 16-bit SQL strings. "));	
       
  2147 	StatementTest<TDesC, TBuf<KSqlBufSize> >();
       
  2148 	
       
  2149 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1606 RSqlStatement test. 8-bit SQL strings. "));
       
  2150 	StatementTest<TDesC8, TBuf8<KSqlBufSize> >();
       
  2151 	
       
  2152 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1607 RSqlColumnReadStream test. Long text column "));
       
  2153 	ColumnTextStreamTest();
       
  2154 
       
  2155 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1621 RSqlColumnReadStream test. Long binary column "));
       
  2156 	ColumnBinaryStreamTest();
       
  2157 
       
  2158 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1608 RSqlParamWriteStream test. Long text parameter "));
       
  2159 	TextParameterStreamTest();
       
  2160 
       
  2161 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1622 RSqlParamWriteStream test. Long binary parameter "));
       
  2162 	BinaryParameterStreamTest();
       
  2163 
       
  2164 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1634 RSqlStatement test. Nameless parameter "));
       
  2165 	NamelessParameterTest();
       
  2166 
       
  2167 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1768 Asynchronous execution tests "));
       
  2168 	AsyncTest();	
       
  2169 	
       
  2170 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1816 RSqlDatabase::Size() tests "));
       
  2171 	SizeTest();
       
  2172 	
       
  2173 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1817 RSqlDatabase::InTransaction() tests "));
       
  2174 	InTransactionTest();
       
  2175 
       
  2176 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4039 RSqlDatabase::Size(RSqlDatabase::TSize&) tests"));
       
  2177 	SizeTest2();
       
  2178 
       
  2179 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4040 RSqlDatabase::Size(RSqlDatabase::TSize&) - attached database tests"));
       
  2180 	AttachedSizeTest2();
       
  2181 	
       
  2182 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4041 RSqlDatabase::Size(RSqlDatabase::TSize&) - different compaction modes tests"));
       
  2183 	DiffCompactModeSize2Test();
       
  2184 	}
       
  2185 
       
  2186 TInt E32Main()
       
  2187 	{
       
  2188 	TheTest.Title();
       
  2189 	
       
  2190 	CTrapCleanup* tc = CTrapCleanup::New();
       
  2191 	
       
  2192 	__UHEAP_MARK;
       
  2193 	
       
  2194 	CreateTestDir();
       
  2195 	DeleteTestFiles();
       
  2196 	TRAPD(err, DoTestsL());
       
  2197 	DeleteTestFiles();
       
  2198 	TEST2(err, KErrNone);
       
  2199 	
       
  2200 	__UHEAP_MARKEND;
       
  2201 	
       
  2202 	TheTest.End();
       
  2203 	TheTest.Close();
       
  2204 	
       
  2205 	delete tc;
       
  2206 	
       
  2207 	User::Heap().Check();
       
  2208 	return KErrNone;
       
  2209 	}