1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_longcol.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,372 @@
1.4 +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include <d32dbms.h>
1.20 +#include <s32file.h>
1.21 +#include <e32test.h>
1.22 +
1.23 +#include "crccheck.h"
1.24 +
1.25 +#undef __UHEAP_MARK
1.26 +#undef __UHEAP_MARKEND
1.27 +#define __UHEAP_MARK
1.28 +#define __UHEAP_MARKEND
1.29 +
1.30 +LOCAL_D TDBMS_CRCChecks TheCrcChecker;
1.31 +
1.32 +#ifndef __linux__ //No CRC test on LINUX
1.33 +#ifdef __TOOLS2__
1.34 +const TPtrC KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_LONGCOL.CRC");
1.35 +#else
1.36 +const TPtrC KCrcRecord=_L("C:\\dbms-tst\\T_LONGCOL.CRC");
1.37 +#endif
1.38 +#endif
1.39 +
1.40 +static RTest TheTest(_L("T_LONGCOL"));
1.41 +static CTrapCleanup* TheTrapCleanup;
1.42 +const TInt KTestCleanupStack=0x40;
1.43 +RFs TheFsSession;
1.44 +
1.45 +#ifdef __TOOLS2__
1.46 +const TPtrC KTestDir=_L(".\\dbms-tst\\");
1.47 +#else
1.48 +const TPtrC KTestDir=_L("C:\\dbms-tst\\");
1.49 +#endif
1.50 +
1.51 +#ifndef __TOOLS2__
1.52 +const TPtrC KSearchTestDbPath = _L("c:\\dbms-tst\\eposlmserachtest.ldb");
1.53 +#else
1.54 +const TPtrC KSearchTestDbPath = _L(".\\eposlmserachtest.ldb");
1.55 +#endif
1.56 +
1.57 +//Delete "aFullName" file.
1.58 +static void DeleteDataFile(const TDesC& aFullName)
1.59 + {
1.60 + RFs fsSession;
1.61 + TInt err = fsSession.Connect();
1.62 + if(err == KErrNone)
1.63 + {
1.64 + TEntry entry;
1.65 + if(fsSession.Entry(aFullName, entry) == KErrNone)
1.66 + {
1.67 + RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
1.68 + err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
1.69 + if(err != KErrNone)
1.70 + {
1.71 + RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
1.72 + }
1.73 + err = fsSession.Delete(aFullName);
1.74 + if(err != KErrNone)
1.75 + {
1.76 + RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
1.77 + }
1.78 + }
1.79 + fsSession.Close();
1.80 + }
1.81 + else
1.82 + {
1.83 + RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
1.84 + }
1.85 + }
1.86 +
1.87 +///////////////////////////////////////////////////////////////////////////////////////
1.88 +///////////////////////////////////////////////////////////////////////////////////////
1.89 +//Tests macros and functions.
1.90 +//If (!aValue) then the test will be panicked, the test data files will be deleted.
1.91 +static void Check(TInt aValue, TInt aLine)
1.92 + {
1.93 + if(!aValue)
1.94 + {
1.95 + ::DeleteDataFile(KSearchTestDbPath);
1.96 + TheTest(EFalse, aLine);
1.97 + }
1.98 + }
1.99 +//If (aValue != aExpected) then the test will be panicked, the test data files will be deleted.
1.100 +static void Check(TInt aValue, TInt aExpected, TInt aLine)
1.101 + {
1.102 + if(aValue != aExpected)
1.103 + {
1.104 + RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
1.105 + ::DeleteDataFile(KSearchTestDbPath);
1.106 + TheTest(EFalse, aLine);
1.107 + }
1.108 + }
1.109 +//Use these to test conditions.
1.110 +#define TEST(arg) ::Check((arg), __LINE__)
1.111 +#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
1.112 +
1.113 +///////////////////////////////////////////////////////////////////////////////////////
1.114 +
1.115 +static void Leave(TInt aLine, TInt aError)
1.116 + {
1.117 + RDebug::Print(_L("*** Leave. Error: %d, line: %d\r\n"), aError, aLine);
1.118 + User::Leave(aError);
1.119 + }
1.120 +static void LeaveIfError(TInt aLine, TInt aError)
1.121 + {
1.122 + if(aError < KErrNone)
1.123 + {
1.124 + ::Leave(aLine, aError);
1.125 + }
1.126 + }
1.127 +#define __LEAVE(err) ::Leave(__LINE__, err)
1.128 +#define __LEAVE_IF_ERROR(err) ::LeaveIfError(__LINE__, err)
1.129 +
1.130 +///////////////////////////////////////////////////////////////////////////////////////
1.131 +///////////////////////////////////////////////////////////////////////////////////////
1.132 +
1.133 +//
1.134 +// Prepare the test directory.
1.135 +//
1.136 +static void SetupTestDirectory()
1.137 + {
1.138 + TInt r=TheFsSession.MkDir(KTestDir);
1.139 + TEST(r==KErrNone || r==KErrAlreadyExists);
1.140 + r=TheFsSession.SetSessionPath(KTestDir);
1.141 + TEST2(r,KErrNone);
1.142 +// On TOOLS2 - RFs::SetSessionPath() will affect all RFs Sessions,
1.143 +// the two RFs need same session path anyway
1.144 +#ifdef __WINSCW__
1.145 + r=TheCrcChecker.SetSessionPath(KTestDir);
1.146 + TEST2(r,KErrNone);
1.147 +#endif
1.148 + }
1.149 +
1.150 +static TInt SearchFor( const TPtrC& aSearchString, RDbNamedDatabase& aDb )
1.151 + {
1.152 + _LIT( KSQLSearchStatement, "select Notes from CDs where Notes like '%S'" );
1.153 + TBuf<512> query;
1.154 + query.Format( KSQLSearchStatement, &aSearchString );
1.155 +
1.156 + // Display query
1.157 + _LIT( KQueryFormatter, "\r\n %S\r\n" );
1.158 + TBuf<512> msg;
1.159 + msg.Format( KQueryFormatter, &query );
1.160 + TheTest.Printf( msg );
1.161 +
1.162 + // create a view on the database
1.163 + RDbView view;
1.164 + // use EDbCompareCollated in order to search case-insensitive
1.165 + __LEAVE_IF_ERROR( view.Prepare( aDb, TDbQuery( query, EDbCompareCollated ) ) );
1.166 + __LEAVE_IF_ERROR( view.EvaluateAll() );
1.167 +
1.168 + // iterate across the row set
1.169 + TInt noOfMatches = 0;
1.170 + for ( view.FirstL(); view.AtRow(); view.NextL() )
1.171 + {
1.172 + // retrieve the row
1.173 + view.GetL();
1.174 + noOfMatches++;
1.175 + }
1.176 +
1.177 + // Display no of matches
1.178 + _LIT( KNoOfMatchFormatter, " Found %d matches\r\n" );
1.179 + msg.Format( KNoOfMatchFormatter, noOfMatches );
1.180 + TheTest.Printf( msg );
1.181 +
1.182 + // close the view
1.183 + view.Close();
1.184 + return noOfMatches;
1.185 + }
1.186 +
1.187 +/**
1.188 +@SYMTestCaseID SYSLIB-DBMS-CT-0645
1.189 +@SYMTestCaseDesc Searching for data from a database
1.190 +@SYMTestPriority Medium
1.191 +@SYMTestActions Tests for EDbColText,EDbColLongText column type
1.192 +@SYMTestExpectedResults Test must not fail
1.193 +@SYMREQ REQ0000
1.194 +*/
1.195 +static void TestSearchL( TInt aIndex )
1.196 + {
1.197 + // Default database
1.198 + _LIT( KComposer1, "Elgar" );
1.199 + _LIT( KCol1, "Artist" );
1.200 + _LIT( KCol2, "Notes" );
1.201 + _LIT( KTable, "CDs" );
1.202 +
1.203 + TInt err = TheFsSession.Delete( KSearchTestDbPath );
1.204 + if ( ( err != KErrNone ) && ( err != KErrNotFound ) && ( err != KErrPathNotFound ) )
1.205 + {
1.206 + __LEAVE( err );
1.207 + }
1.208 +
1.209 + RDbNamedDatabase db;
1.210 + CleanupClosePushL( db );
1.211 + __LEAVE_IF_ERROR( db.Create( TheFsSession, KSearchTestDbPath ) );
1.212 +
1.213 + //
1.214 + // Create the database table
1.215 + //
1.216 + // Create a table definition
1.217 + CDbColSet* columns = CDbColSet::NewLC();
1.218 + // add the columns
1.219 + columns->AddL( TDbCol( KCol1, EDbColText ) );
1.220 + if ( aIndex == 0 )
1.221 + columns->AddL( TDbCol( KCol2, EDbColText ) );
1.222 + else
1.223 + columns->AddL( TDbCol( KCol2, EDbColLongText ) );
1.224 + // if the column is of type "EDbColText" instead,
1.225 + // all searching is working properly
1.226 + // Create a table
1.227 + __LEAVE_IF_ERROR( db.CreateTable( KTable, *columns ) );
1.228 + // cleanup the column set
1.229 + CleanupStack::PopAndDestroy( columns );
1.230 +
1.231 + //
1.232 + // Add data
1.233 + //
1.234 + // create a view on the database
1.235 + _LIT( KSQLStatement, "select Artist,Notes from CDs order by Artist" );
1.236 + RDbView view;
1.237 + __LEAVE_IF_ERROR( view.Prepare( db, TDbQuery( KSQLStatement, EDbCompareNormal ) ) );
1.238 + __LEAVE_IF_ERROR( view.EvaluateAll() );
1.239 +
1.240 + // Get the structure of rowset
1.241 + CDbColSet* colSet = view.ColSetL();
1.242 + // insert a row
1.243 + view.InsertL();
1.244 + view.SetColL( colSet->ColNo( KCol1 ), KComposer1 ); // Artist
1.245 + // Use the stream
1.246 + // RDbColWriteStream out;
1.247 + // TDbColNo col = colSet->ColNo( KCol2 ); // Ordinal position of long column
1.248 + //
1.249 + // out.OpenLC( view, col );
1.250 + // out.WriteL( _L( "Some additional comment here." ) );
1.251 + // out.Close();
1.252 + //
1.253 + // CleanupStack::PopAndDestroy(); // out
1.254 + view.SetColL( colSet->ColNo( KCol2 ), _L( "Some additional comment here." ) );
1.255 + view.PutL();
1.256 + // close the view
1.257 + view.Close();
1.258 + delete colSet;
1.259 +
1.260 + //
1.261 + // Display the data
1.262 + //
1.263 + _LIT( KRowFormatter, "\r\n Artist: %S \r\n Notes: %S \r\n" );
1.264 + __LEAVE_IF_ERROR( view.Prepare( db, TDbQuery( KSQLStatement, EDbCompareNormal ) ) );
1.265 + __LEAVE_IF_ERROR( view.EvaluateAll() );
1.266 +
1.267 + // Get the structure of the rowset
1.268 + colSet = view.ColSetL();
1.269 + // iterate across the row set
1.270 + for ( view.FirstL(); view.AtRow(); view.NextL() )
1.271 + {
1.272 + // retrieve the row
1.273 + view.GetL();
1.274 + // while the rowset is on this row, can use a TPtrC to
1.275 + // refer to any text columns
1.276 + TPtrC artist = view.ColDes( colSet->ColNo( KCol1 ) );
1.277 + // and a stream for long columns
1.278 + RDbColReadStream in;
1.279 + TDbColNo col = colSet->ColNo( KCol2 ); // Ordinal position of long column
1.280 + TBuf<256> notes; // Buffer for out notes
1.281 + in.OpenLC( view, col );
1.282 + in.ReadL( notes, view.ColLength( col ) );
1.283 + in.Close();
1.284 + CleanupStack::PopAndDestroy(); // in
1.285 + // Display the artist and notes
1.286 + TBuf<512> msg;
1.287 + msg.Format( KRowFormatter, &artist, ¬es );
1.288 + TheTest.Printf( msg );
1.289 + }
1.290 + // close the view
1.291 + view.Close();
1.292 + delete colSet;
1.293 +
1.294 + //
1.295 + // Search for the data
1.296 + //
1.297 + TInt result;
1.298 + result = SearchFor( _L( "Some*" ), db ); // matches
1.299 + TEST(result == 1);
1.300 + result = SearchFor( _L( "some*" ), db ); // defect causes no match, should match
1.301 + TEST(result == 1);
1.302 + result = SearchFor( _L( "*some*" ), db ); // matches
1.303 + TEST(result == 1);
1.304 + result = SearchFor( _L( "s?me*" ), db ); // matches
1.305 + TEST(result == 1);
1.306 + result = SearchFor( _L( "Some additional comment here." ), db ); // matches
1.307 + TEST(result == 1);
1.308 + result = SearchFor( _L( "some additional comment here." ), db ); // defect causes no match, should match
1.309 + TEST(result == 1);
1.310 +
1.311 + CleanupStack::PopAndDestroy( &db );
1.312 + TheCrcChecker.GenerateCrcL(KSearchTestDbPath);
1.313 + }
1.314 +
1.315 +//
1.316 +// Initialise the cleanup stack.
1.317 +//
1.318 +static void setupCleanup()
1.319 + {
1.320 + TheTrapCleanup=CTrapCleanup::New();
1.321 + TEST(TheTrapCleanup!=NULL);
1.322 + TRAPD(r,\
1.323 + {\
1.324 + for (TInt i=KTestCleanupStack;i>0;i--)\
1.325 + CleanupStack::PushL((TAny*)0);\
1.326 + CleanupStack::Pop(KTestCleanupStack);\
1.327 + });
1.328 + TEST2(r,KErrNone);
1.329 + }
1.330 +
1.331 +//
1.332 +// Test streaming conversions.
1.333 +//
1.334 +GLDEF_C TInt E32Main()
1.335 + {
1.336 + TheTest.Title();
1.337 + setupCleanup();
1.338 + __UHEAP_MARK;
1.339 +
1.340 + TInt err = TheFsSession.Connect();
1.341 + TEST2(err, KErrNone);
1.342 + ::SetupTestDirectory();
1.343 +
1.344 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0645 Testing DBMS - EDbColText "));
1.345 + TRAP(err, TestSearchL( 0 ));
1.346 + TEST2(err,KErrNone);
1.347 + TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0645 Testing DBMS - EDbColLongText "));
1.348 + TRAP(err, TestSearchL( 1 ));
1.349 + TEST2(err,KErrNone);
1.350 + ::DeleteDataFile(KSearchTestDbPath);
1.351 +
1.352 +#ifndef __linux__
1.353 +#ifndef __TOOLS2__
1.354 + TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
1.355 + TheTest(err==KErrNone);
1.356 + TheTest(lc==KErrNone);
1.357 +#else
1.358 + TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
1.359 + TPtrC errmsg;
1.360 + TheCrcChecker.ErrorReportL(err, errmsg);
1.361 + RDebug::Print(errmsg);
1.362 + TheTest(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
1.363 +#endif
1.364 +#endif
1.365 +
1.366 + TheTest.End();
1.367 +
1.368 + TheFsSession.Close();
1.369 +
1.370 + __UHEAP_MARKEND;
1.371 + delete TheTrapCleanup;
1.372 + TheTest.Close();
1.373 + return 0;
1.374 + }
1.375 +