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