os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_longcol.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-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 <d32dbms.h>
    17 #include <s32file.h>
    18 #include <e32test.h>
    19 
    20 #include "crccheck.h"
    21 
    22 #undef __UHEAP_MARK
    23 #undef __UHEAP_MARKEND
    24 #define __UHEAP_MARK
    25 #define __UHEAP_MARKEND
    26 
    27 LOCAL_D TDBMS_CRCChecks TheCrcChecker;
    28 
    29 #ifndef __linux__ //No CRC test on LINUX
    30 #ifdef __TOOLS2__
    31 const TPtrC	KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_LONGCOL.CRC");
    32 #else
    33 const TPtrC	KCrcRecord=_L("C:\\dbms-tst\\T_LONGCOL.CRC");
    34 #endif
    35 #endif
    36 
    37 static RTest TheTest(_L("T_LONGCOL"));
    38 static CTrapCleanup* TheTrapCleanup;
    39 const TInt KTestCleanupStack=0x40;
    40 RFs TheFsSession;
    41 
    42 #ifdef __TOOLS2__
    43 const TPtrC KTestDir=_L(".\\dbms-tst\\");
    44 #else
    45 const TPtrC KTestDir=_L("C:\\dbms-tst\\");
    46 #endif
    47 
    48 #ifndef __TOOLS2__
    49 const TPtrC KSearchTestDbPath = _L("c:\\dbms-tst\\eposlmserachtest.ldb");
    50 #else
    51 const TPtrC KSearchTestDbPath = _L(".\\eposlmserachtest.ldb");
    52 #endif
    53 
    54 //Delete "aFullName" file.
    55 static void DeleteDataFile(const TDesC& aFullName)
    56 	{
    57 	RFs fsSession;
    58 	TInt err = fsSession.Connect();
    59 	if(err == KErrNone)
    60 		{
    61 		TEntry entry;
    62 		if(fsSession.Entry(aFullName, entry) == KErrNone)
    63 			{
    64 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
    65 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
    66 			if(err != KErrNone) 
    67 				{
    68 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
    69 				}
    70 			err = fsSession.Delete(aFullName);
    71 			if(err != KErrNone) 
    72 				{
    73 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
    74 				}
    75 			}
    76 		fsSession.Close();
    77 		}
    78 	else
    79 		{
    80 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
    81 		}
    82 	}
    83 
    84 ///////////////////////////////////////////////////////////////////////////////////////
    85 ///////////////////////////////////////////////////////////////////////////////////////
    86 //Tests macros and functions.
    87 //If (!aValue) then the test will be panicked, the test data files will be deleted.
    88 static void Check(TInt aValue, TInt aLine)
    89 	{
    90 	if(!aValue)
    91 		{
    92 		::DeleteDataFile(KSearchTestDbPath);
    93 		TheTest(EFalse, aLine);
    94 		}
    95 	}
    96 //If (aValue != aExpected) then the test will be panicked, the test data files will be deleted.
    97 static void Check(TInt aValue, TInt aExpected, TInt aLine)
    98 	{
    99 	if(aValue != aExpected)
   100 		{
   101 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
   102 		::DeleteDataFile(KSearchTestDbPath);
   103 		TheTest(EFalse, aLine);
   104 		}
   105 	}
   106 //Use these to test conditions.
   107 #define TEST(arg) ::Check((arg), __LINE__)
   108 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
   109 
   110 ///////////////////////////////////////////////////////////////////////////////////////
   111 
   112 static void Leave(TInt aLine, TInt aError)
   113 	{
   114 	RDebug::Print(_L("*** Leave. Error: %d, line: %d\r\n"), aError, aLine);
   115 	User::Leave(aError);
   116 	}
   117 static void LeaveIfError(TInt aLine, TInt aError)
   118 	{
   119 	if(aError < KErrNone)
   120 		{
   121 		::Leave(aLine, aError);
   122 		}
   123 	}
   124 #define __LEAVE(err) ::Leave(__LINE__, err)
   125 #define __LEAVE_IF_ERROR(err) ::LeaveIfError(__LINE__, err)
   126 
   127 ///////////////////////////////////////////////////////////////////////////////////////
   128 ///////////////////////////////////////////////////////////////////////////////////////
   129 
   130 //
   131 // Prepare the test directory.
   132 //
   133 static void SetupTestDirectory()
   134     {
   135 	TInt r=TheFsSession.MkDir(KTestDir);
   136 	TEST(r==KErrNone || r==KErrAlreadyExists);
   137 	r=TheFsSession.SetSessionPath(KTestDir);
   138 	TEST2(r,KErrNone);
   139 // On TOOLS2 - RFs::SetSessionPath() will affect all RFs Sessions, 
   140 // the two RFs need same session path anyway
   141 #ifdef __WINSCW__ 
   142 	r=TheCrcChecker.SetSessionPath(KTestDir);
   143 	TEST2(r,KErrNone);
   144 #endif
   145 	}
   146 
   147 static TInt SearchFor( const TPtrC& aSearchString, RDbNamedDatabase& aDb )
   148 	{
   149 	_LIT( KSQLSearchStatement, "select Notes from CDs where Notes like '%S'" );
   150 	TBuf<512> query;
   151 	query.Format( KSQLSearchStatement, &aSearchString );
   152 	
   153 	// Display query
   154 	_LIT( KQueryFormatter, "\r\n %S\r\n" );
   155 	TBuf<512> msg;
   156 	msg.Format( KQueryFormatter, &query );
   157 	TheTest.Printf( msg );
   158 	
   159 	// create a view on the database
   160 	RDbView view;
   161 	// use EDbCompareCollated in order to search case-insensitive
   162 	__LEAVE_IF_ERROR( view.Prepare( aDb, TDbQuery( query, EDbCompareCollated ) ) );
   163 	__LEAVE_IF_ERROR( view.EvaluateAll() );
   164 	
   165 	// iterate across the row set
   166 	TInt noOfMatches = 0;
   167 	for ( view.FirstL(); view.AtRow(); view.NextL() )
   168 		{
   169 		// retrieve the row
   170 		view.GetL();
   171 		noOfMatches++;
   172 		}
   173 		
   174 	// Display no of matches
   175 	_LIT( KNoOfMatchFormatter, " Found %d matches\r\n" );
   176 	msg.Format( KNoOfMatchFormatter, noOfMatches );
   177 	TheTest.Printf( msg );
   178 	
   179 	// close the view
   180 	view.Close();
   181 	return noOfMatches;
   182 	}
   183 
   184 /**
   185 @SYMTestCaseID          SYSLIB-DBMS-CT-0645
   186 @SYMTestCaseDesc        Searching for data from a database
   187 @SYMTestPriority        Medium
   188 @SYMTestActions         Tests for EDbColText,EDbColLongText column type
   189 @SYMTestExpectedResults Test must not fail
   190 @SYMREQ                 REQ0000
   191 */
   192 static void TestSearchL( TInt aIndex )
   193 	{
   194 	// Default database
   195 	_LIT( KComposer1, "Elgar" );
   196 	_LIT( KCol1, "Artist" );
   197 	_LIT( KCol2, "Notes" );
   198 	_LIT( KTable, "CDs" );
   199 	
   200 	TInt err = TheFsSession.Delete( KSearchTestDbPath );
   201 	if ( ( err != KErrNone ) && ( err != KErrNotFound ) && ( err != KErrPathNotFound ) )
   202 		{
   203 		__LEAVE( err );
   204 		}
   205 		
   206 	RDbNamedDatabase db;
   207 	CleanupClosePushL( db );
   208 	__LEAVE_IF_ERROR( db.Create( TheFsSession, KSearchTestDbPath ) );
   209 	
   210 	//
   211 	// Create the database table
   212 	//
   213 	// Create a table definition
   214 	CDbColSet* columns = CDbColSet::NewLC();
   215 	// add the columns
   216 	columns->AddL( TDbCol( KCol1, EDbColText ) );
   217 	if ( aIndex == 0 )
   218 		columns->AddL( TDbCol( KCol2, EDbColText ) );
   219 	else
   220 		columns->AddL( TDbCol( KCol2, EDbColLongText ) );
   221 	// if the column is of type "EDbColText" instead,
   222 	// all searching is working properly
   223 	// Create a table
   224 	__LEAVE_IF_ERROR( db.CreateTable( KTable, *columns ) );
   225 	// cleanup the column set
   226 	CleanupStack::PopAndDestroy( columns );
   227 	
   228 	//
   229 	// Add data
   230 	//
   231 	// create a view on the database
   232 	_LIT( KSQLStatement, "select Artist,Notes from CDs order by Artist" );
   233 	RDbView view;
   234 	__LEAVE_IF_ERROR( view.Prepare( db, TDbQuery( KSQLStatement, EDbCompareNormal ) ) );
   235 	__LEAVE_IF_ERROR( view.EvaluateAll() );
   236 	
   237 	// Get the structure of rowset
   238 	CDbColSet* colSet = view.ColSetL();
   239 	// insert a row
   240 	view.InsertL();
   241 	view.SetColL( colSet->ColNo( KCol1 ), KComposer1 ); // Artist
   242 	// Use the stream
   243 	// RDbColWriteStream out;
   244 	// TDbColNo col = colSet->ColNo( KCol2 ); // Ordinal position of long column
   245 	//
   246 	// out.OpenLC( view, col );
   247 	// out.WriteL( _L( "Some additional comment here." ) );
   248 	// out.Close();
   249 	//
   250 	// CleanupStack::PopAndDestroy(); // out
   251 	view.SetColL( colSet->ColNo( KCol2 ), _L( "Some additional comment here." ) );
   252 	view.PutL();
   253 	// close the view
   254 	view.Close();
   255 	delete colSet;
   256 	
   257 	//
   258 	// Display the data
   259 	//
   260 	_LIT( KRowFormatter, "\r\n Artist: %S \r\n Notes: %S \r\n" );
   261 	__LEAVE_IF_ERROR( view.Prepare( db, TDbQuery( KSQLStatement, EDbCompareNormal ) ) );
   262 	__LEAVE_IF_ERROR( view.EvaluateAll() );
   263 	
   264 	// Get the structure of the rowset
   265 	colSet = view.ColSetL();
   266 	// iterate across the row set
   267 	for ( view.FirstL(); view.AtRow(); view.NextL() )
   268 		{
   269 		// retrieve the row
   270 		view.GetL();
   271 		// while the rowset is on this row, can use a TPtrC to
   272 		// refer to any text columns
   273 		TPtrC artist = view.ColDes( colSet->ColNo( KCol1 ) );
   274 		// and a stream for long columns
   275 		RDbColReadStream in;
   276 		TDbColNo col = colSet->ColNo( KCol2 ); // Ordinal position of long column
   277 		TBuf<256> notes; // Buffer for out notes
   278 		in.OpenLC( view, col );
   279 		in.ReadL( notes, view.ColLength( col ) );
   280 		in.Close();
   281 		CleanupStack::PopAndDestroy(); // in
   282 		// Display the artist and notes
   283 		TBuf<512> msg;
   284 		msg.Format( KRowFormatter, &artist, &notes );
   285 		TheTest.Printf( msg );
   286 		}
   287 	// close the view
   288 	view.Close();
   289 	delete colSet;
   290 	
   291 	//
   292 	// Search for the data
   293 	//
   294 	TInt result;
   295 	result = SearchFor( _L( "Some*" ), db ); // matches
   296 	TEST(result == 1);
   297 	result = SearchFor( _L( "some*" ), db ); // defect causes no match, should match
   298 	TEST(result == 1);
   299 	result = SearchFor( _L( "*some*" ), db ); // matches
   300 	TEST(result == 1);
   301 	result = SearchFor( _L( "s?me*" ), db ); // matches
   302 	TEST(result == 1);
   303 	result = SearchFor( _L( "Some additional comment here." ), db ); // matches
   304 	TEST(result == 1);
   305 	result = SearchFor( _L( "some additional comment here." ), db ); // defect causes no match, should match
   306 	TEST(result == 1);
   307 	
   308 	CleanupStack::PopAndDestroy( &db ); 
   309 	TheCrcChecker.GenerateCrcL(KSearchTestDbPath);
   310 	}
   311 
   312 //
   313 // Initialise the cleanup stack.
   314 //
   315 static void setupCleanup()
   316 	{
   317 	TheTrapCleanup=CTrapCleanup::New();
   318 	TEST(TheTrapCleanup!=NULL);
   319 	TRAPD(r,\
   320 		{\
   321 		for (TInt i=KTestCleanupStack;i>0;i--)\
   322 			CleanupStack::PushL((TAny*)0);\
   323 		CleanupStack::Pop(KTestCleanupStack);\
   324 		});
   325 	TEST2(r,KErrNone);
   326 	}
   327 	
   328 //
   329 // Test streaming conversions.
   330 //
   331 GLDEF_C TInt E32Main()
   332 	{
   333 	TheTest.Title();
   334 	setupCleanup();
   335 	__UHEAP_MARK;
   336 
   337 	TInt err = TheFsSession.Connect();
   338 	TEST2(err, KErrNone);
   339 	::SetupTestDirectory();
   340 
   341 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0645 Testing DBMS - EDbColText "));
   342 	TRAP(err, TestSearchL( 0 ));
   343 	TEST2(err,KErrNone);
   344 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0645 Testing DBMS - EDbColLongText "));
   345 	TRAP(err, TestSearchL( 1 ));
   346 	TEST2(err,KErrNone);
   347 	::DeleteDataFile(KSearchTestDbPath);
   348 
   349 #ifndef __linux__
   350 #ifndef __TOOLS2__
   351 	TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
   352 	TheTest(err==KErrNone);
   353 	TheTest(lc==KErrNone);
   354 #else
   355 	TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
   356 	TPtrC errmsg;
   357 	TheCrcChecker.ErrorReportL(err, errmsg);
   358 	RDebug::Print(errmsg);
   359 	TheTest(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
   360 #endif
   361 #endif
   362 
   363 	TheTest.End();
   364 
   365 	TheFsSession.Close();
   366 
   367 	__UHEAP_MARKEND;
   368 	delete TheTrapCleanup;
   369 	TheTest.Close();
   370 	return 0;
   371 	}
   372