os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_dbmsscript.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_dbmsscript.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1521 @@
     1.4 +// Copyright (c) 1998-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 <f32file.h>
    1.21 +#include <e32test.h>
    1.22 +#include <e32math.h>
    1.23 +
    1.24 +#include "crccheck.h"
    1.25 +
    1.26 +#undef __UHEAP_MARK
    1.27 +#define __UHEAP_MARK
    1.28 +#undef __UHEAP_MARKEND
    1.29 +#define __UHEAP_MARKEND
    1.30 +
    1.31 +LOCAL_D TDBMS_CRCChecks TheCrcChecker;
    1.32 +
    1.33 +#ifndef __linux__ //No CRC test on LINUX
    1.34 +#ifdef __TOOLS2__
    1.35 +const TPtrC	KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_DBMSSCRIPT.CRC");
    1.36 +#else
    1.37 +const TPtrC	KCrcRecord=_L("C:\\dbms-tst\\T_DBMSSCRIPT.CRC");
    1.38 +#endif
    1.39 +#endif
    1.40 +
    1.41 +LOCAL_D RTest test(_L("T_DBMSSCRIPT"));
    1.42 +LOCAL_D CTrapCleanup* TheTrapCleanup;
    1.43 +LOCAL_D RDbNamedDatabase TheDatabase;
    1.44 +LOCAL_D RFs TheFs;
    1.45 +LOCAL_D RDbView TheView;
    1.46 +//
    1.47 +
    1.48 +#ifdef __TOOLS2__
    1.49 +const TPtrC KTestDatabase=_L(".\\dbms-tst\\t_script.db");
    1.50 +#else
    1.51 +const TPtrC KTestDatabase=_L("C:\\dbms-tst\\t_script.db");
    1.52 +#endif
    1.53 +
    1.54 +const TPtrC KRomScriptFile=_L("z:\\test\\t_script.txt");
    1.55 +
    1.56 +#ifndef __TOOLS2__
    1.57 +const TPtrC KScriptFile=_L("c:\\dbms-tst\\t_script.txt");
    1.58 +#else
    1.59 +const TPtrC KScriptFile=_L(".\\dbms-tst\\t_script.txt");
    1.60 +#endif
    1.61 +
    1.62 +#ifndef __TOOLS2__
    1.63 +const TPtrC KOutputFile=_L("c:\\dbms-tst\\t_script.log");
    1.64 +#else
    1.65 +const TPtrC KOutputFile=_L(".\\dbms-tst\\t_script.log");
    1.66 +#endif
    1.67 +
    1.68 +const TInt KTestCleanupStack=0x20;
    1.69 +const TPtrC KDDLKeywords[]={_L("CREATE"),_L("DROP"),_L("ALTER")};
    1.70 +const TPtrC KDMLKeywords[]={_L("INSERT"),_L("DELETE"),_L("UPDATE")};
    1.71 +const TPtrC KQueryKeywords[]={_L("SELECT")};
    1.72 +const TPtrC KScriptKeywords[]={_L("PRINT"),_L("ROWS"),_L("COMPARE"),_L("ERROR"),_L("!"),
    1.73 +							   _L("POPULATE"),_L("RESULTS"),_L("BUILD"),_L("QUERY"),
    1.74 +							   _L("NORMAL"),_L("FOLDED"),_L("COLLATED"),_L("START"),_L("STOP"),
    1.75 +							   _L("LOAD"),_L("ECHO"),_L("WINDOW"),_L("ACCESS")};
    1.76 +const TPtrC KRowIdColName=_L("Rw");
    1.77 +enum TKeyword {EPrint,ERows,ECompare,EError,EComment,EPopulate,EResults,EBuild,EQuery,ENormal,
    1.78 +               EFolded,ECollated,EStart,EStop,ELoad,EEcho,EWindow,EAccess,ENumKeywords,EUnknown};
    1.79 +//
    1.80 +typedef TBuf<256> TScriptLine;
    1.81 +typedef TBuf<256> TScriptToken;
    1.82 +//
    1.83 +#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
    1.84 +
    1.85 +class TScript;
    1.86 +class TTimer
    1.87 +	{
    1.88 +public:
    1.89 +	inline TTimer(TScript& aScript);
    1.90 +	void Start(const TDesC& aDes);
    1.91 +	void Stop(const TDesC& aDes);
    1.92 +private:
    1.93 +	TUint iTicks;
    1.94 +	TScript& iScript;
    1.95 +	};
    1.96 +
    1.97 +class TResults
    1.98 +	{
    1.99 +public:
   1.100 +	inline TResults(); 
   1.101 +public:
   1.102 +	RDbView& iView;
   1.103 +	TInt iError;
   1.104 +	TInt iRows;
   1.105 +	TInt iLineNo;
   1.106 +	TScriptLine iLine;
   1.107 +	};
   1.108 +
   1.109 +class TSqlStatement
   1.110 +	{
   1.111 +private:
   1.112 +	enum TSqlType {EDML,EDDL,EQuery,EUnknown};
   1.113 +public:
   1.114 +	inline TSqlStatement(const TDesC& aSql,TResults& aResults); 
   1.115 +	TInt Execute(TDbTextComparison aTextComparison=EDbCompareNormal,
   1.116 +		RDbRowSet::TAccess aAccess=RDbRowSet::EReadOnly,TBool aWindow=EFalse) const;
   1.117 +private:
   1.118 +	void ExecuteSql(TDbTextComparison aTextComparison) const;
   1.119 +	void ExecuteQuery(RDbRowSet::TAccess aAccess,TBool aWindow) const;
   1.120 +	TSqlType SqlType() const;
   1.121 +private:
   1.122 +	const TDesC& iSql;
   1.123 +	TResults& iResults;
   1.124 +	};
   1.125 +
   1.126 +class TScript
   1.127 +	{
   1.128 +public:
   1.129 +	TScript();
   1.130 +#ifdef __TOOLS2__
   1.131 +	~TScript() { iFile.Close(); };
   1.132 +#endif
   1.133 +	TInt ReadNextStatement();
   1.134 +	void GetNextTokenFromStatement(TDes& aToken);
   1.135 +	void GetNextTokenFromLine(TDes& aToken);
   1.136 +	TPtrC Statement();
   1.137 +	TInt IntValue();
   1.138 +	void WriteLine(const TDesC& aLine);
   1.139 +	void WriteError(const TDesC& aLine);
   1.140 +	void WriteSqlError(const TResults& aResults,const TDesC& aLine); 
   1.141 +	void WriteComment(const TDesC& aLine);
   1.142 +	TKeyword Keyword(const TDesC& aKeyword) const;
   1.143 +	void ConsumeLine();
   1.144 +	void ConsumeStatement();
   1.145 +	inline TInt LineNo() const;
   1.146 +private:
   1.147 +	TInt ReadNextLine();
   1.148 +	TInt AppendNextLine();
   1.149 +	TBool IsStatement();
   1.150 +private:
   1.151 +	TFileText iInput;
   1.152 +	TFileText iOutput;
   1.153 +	TScriptLine iBuf;
   1.154 +	TLex iStatement;
   1.155 +	TLex iLine;
   1.156 +	TInt iLineNo;
   1.157 +#ifdef __TOOLS2__
   1.158 +	RFile iFile;
   1.159 +#endif
   1.160 +	};
   1.161 +
   1.162 +class TScriptEngine
   1.163 +	{
   1.164 +public:
   1.165 +	inline TScriptEngine();
   1.166 +	void RunL();
   1.167 +private:
   1.168 +	void ExecuteL();
   1.169 +	TInt ExecuteScriptL();
   1.170 +	TInt ExecuteSql(const TDesC& aSql);
   1.171 +	TInt ExecuteSql();
   1.172 +	// keyword operations
   1.173 +	void DoPrintL();
   1.174 +	void DoComment();
   1.175 +	void DoError();
   1.176 +	void DoEcho();
   1.177 +	void DoWindow();
   1.178 +	void DoAccess();
   1.179 +	void DoRows();
   1.180 +	void DoCompareL();
   1.181 +	void DoPopulate();
   1.182 +	void DoResultsL();
   1.183 +	void DoBuildTable();
   1.184 +	void DoQuery();
   1.185 +	void DoTextComparison(TKeyword aKeyword);
   1.186 +	void DoStartTimer();
   1.187 +	void DoStopTimer();
   1.188 +	void DoLoadDb();
   1.189 +	//
   1.190 +	void PrintL(RDbRowSet& aRowSet);
   1.191 +	void CompareL(RDbRowSet& aRowSet);
   1.192 +	void CompareValues(RDbRowSet& aRowSet,TDbColNo ColNo,TDbColType aType,const TDesC& aToken);
   1.193 +	void FatalError(const TDesC& aLine);
   1.194 +	void FatalError();
   1.195 +	void FatalSqlError(const TDesC& aLine);
   1.196 +	void TestForNoError(); 
   1.197 +private:
   1.198 +	TScript iScript;
   1.199 +	TResults iResults;
   1.200 +	TDbTextComparison iTextComparison;
   1.201 +	TTimer iTimer;
   1.202 +	TBool iEcho;
   1.203 +	TBool iWindow;
   1.204 +	RDbRowSet::TAccess iAccess;
   1.205 +	
   1.206 +	};
   1.207 +
   1.208 +//
   1.209 +// class TTimer
   1.210 +//
   1.211 +
   1.212 +inline TTimer::TTimer(TScript& aScript)
   1.213 +	: iScript(aScript)
   1.214 +	{}
   1.215 +
   1.216 +void TTimer::Start(const TDesC& aDes)
   1.217 +	{
   1.218 +	TScriptLine line;
   1.219 +	line.Format(_L("%S: "),&aDes);
   1.220 +	iScript.WriteLine(line);
   1.221 +	iTicks=User::TickCount();
   1.222 +	}
   1.223 +
   1.224 +void TTimer::Stop(const TDesC& aDes)
   1.225 +	{
   1.226 +	TScriptLine line;
   1.227 +	line.Format(_L("%S: "),&aDes);
   1.228 +#ifdef __EPOC32__
   1.229 +#define TICK_TIME 15625
   1.230 +#else
   1.231 +#define	TICK_TIME 100000
   1.232 +#endif
   1.233 +	TInt microSec=(User::TickCount()-iTicks)*TICK_TIME;
   1.234 +	TUint sec=microSec/1000000;
   1.235 +	TUint centi=(microSec/10000)-sec*100;
   1.236 +	line.AppendFormat(_L("%u.%02us\n"),sec,centi);
   1.237 +	iScript.WriteLine(line);
   1.238 +	}
   1.239 +
   1.240 +//
   1.241 +// class TResults
   1.242 +//
   1.243 +
   1.244 +inline TResults::TResults()
   1.245 +	: iView(TheView),iError(KErrNone)
   1.246 +	{}
   1.247 +
   1.248 +
   1.249 +//
   1.250 +// class TSqlStatement
   1.251 +//
   1.252 +
   1.253 +inline TSqlStatement::TSqlStatement(const TDesC& aSql,TResults& aResults)
   1.254 +	: iSql(aSql),iResults(aResults)
   1.255 +	{}
   1.256 +
   1.257 +void TSqlStatement::ExecuteSql(TDbTextComparison aTextComparison) const
   1.258 +//
   1.259 +// executes DML or DDL
   1.260 +//
   1.261 +	{
   1.262 +	TInt r=TheDatabase.Execute(iSql,aTextComparison);
   1.263 +	if (r<0)
   1.264 +		iResults.iError=r;
   1.265 +	else
   1.266 +		{
   1.267 +		iResults.iError=KErrNone;
   1.268 +		iResults.iRows=r;
   1.269 +		}
   1.270 +	}
   1.271 +
   1.272 +void TSqlStatement::ExecuteQuery(RDbRowSet::TAccess aAccess,TBool aWindow) const
   1.273 +	{
   1.274 +	iResults.iView.Close();			// discard any previous queries
   1.275 +	TInt& err=iResults.iError;
   1.276 +	if (aWindow)
   1.277 +		err=iResults.iView.Prepare(TheDatabase,iSql,KDbUnlimitedWindow,aAccess);
   1.278 +	else
   1.279 +		err=iResults.iView.Prepare(TheDatabase,iSql,aAccess);
   1.280 +	if (err==KErrNone)
   1.281 +		err=iResults.iView.EvaluateAll();
   1.282 +	}
   1.283 +
   1.284 +TSqlStatement::TSqlType TSqlStatement::SqlType() const
   1.285 +//
   1.286 +// determines the type of sql statement by matching keywords
   1.287 +//
   1.288 +	{
   1.289 +	for (TUint i=0;i<ARRAY_SIZE(KDDLKeywords);++i)
   1.290 +		{
   1.291 +		if (iSql.FindF(KDDLKeywords[i])==0)		
   1.292 +			return EDDL;
   1.293 +		}
   1.294 +	for (TUint ii=0;ii<ARRAY_SIZE(KDMLKeywords);++ii)
   1.295 +		{
   1.296 +		if (iSql.FindF(KDMLKeywords[ii])==0)
   1.297 +			return EDML;
   1.298 +		}
   1.299 +	for (TUint j=0;j<ARRAY_SIZE(KQueryKeywords);++j)
   1.300 +		{
   1.301 +		if (iSql.FindF(KQueryKeywords[j])==0)
   1.302 +			return EQuery;
   1.303 +		}
   1.304 +	return EUnknown;
   1.305 +	}
   1.306 +
   1.307 +TInt TSqlStatement::Execute(TDbTextComparison aTextComparison,RDbRowSet::TAccess aAccess,TBool aWindow) const
   1.308 +//
   1.309 +// executes the sql statement
   1.310 +//
   1.311 +	{
   1.312 +	TInt r=KErrNone;
   1.313 +	switch (SqlType())
   1.314 +		{
   1.315 +	case EDDL:
   1.316 +	case EDML:
   1.317 +		ExecuteSql(aTextComparison);
   1.318 +		break;
   1.319 +	case EQuery:
   1.320 +		ExecuteQuery(aAccess,aWindow);
   1.321 +		break;
   1.322 +	case EUnknown:
   1.323 +	default:
   1.324 +		r=KErrNotFound;
   1.325 +		break;
   1.326 +		}
   1.327 +	return r;
   1.328 +	}
   1.329 +
   1.330 +
   1.331 +//
   1.332 +// class TScript
   1.333 +//
   1.334 +
   1.335 +TScript::TScript()
   1.336 +	: iLineNo(0)
   1.337 +	{
   1.338 +	RFile file;
   1.339 +	TInt r=file.Open(TheFs,KScriptFile,EFileRead);
   1.340 +#ifndef __TOOLS2__
   1.341 +	if (r!=KErrNone)
   1.342 +		r=file.Open(TheFs,KRomScriptFile,EFileRead);
   1.343 +#endif
   1.344 +	test(r==KErrNone);
   1.345 +	iInput.Set(file);
   1.346 +
   1.347 +#ifdef __TOOLS2__
   1.348 +	RFile file2;
   1.349 +	test(file2.Replace(TheFs,KOutputFile,EFileWrite)==KErrNone);
   1.350 +	iFile = file2;
   1.351 +	iOutput.Set(file2);
   1.352 +#else
   1.353 +	test(file.Replace(TheFs,KOutputFile,EFileWrite)==KErrNone);
   1.354 +	iOutput.Set(file);
   1.355 +#endif
   1.356 +	}
   1.357 +
   1.358 +inline TInt TScript::LineNo() const
   1.359 +	{return iLineNo;}
   1.360 +
   1.361 +TBool TScript::IsStatement()
   1.362 +//
   1.363 +//	checks for keywords which possibly conform to the usual statement format (ie end in ';')
   1.364 +//
   1.365 +	{
   1.366 +	if (iLine.Remainder().Length()==0)	// null statement
   1.367 +		return ETrue;
   1.368 +	TPtrC line=iLine.Remainder();
   1.369 +	TKeyword keyword=Keyword(line);
   1.370 +	switch (keyword)
   1.371 +		{
   1.372 +	case EPrint: 
   1.373 +	case ERows:
   1.374 +	case EBuild:
   1.375 +	case EError:
   1.376 +	case EQuery:
   1.377 +	case ENormal:
   1.378 +	case EFolded:
   1.379 +	case ECollated:
   1.380 +	case EStart:
   1.381 +	case EStop:
   1.382 +	case ELoad:
   1.383 +	case EEcho:
   1.384 +	case EWindow:
   1.385 +	case EAccess:
   1.386 +	case EUnknown:						// could be sql
   1.387 +		return ETrue;
   1.388 +	case EComment:
   1.389 +	case EPopulate:
   1.390 +	case ECompare:
   1.391 +	case EResults:
   1.392 +		iStatement=line;				// not a statement, so make it the whole line
   1.393 +		return EFalse;
   1.394 +	default:
   1.395 +		test(0);
   1.396 +		return EFalse;
   1.397 +		}
   1.398 +	}
   1.399 +
   1.400 +TInt TScript::ReadNextStatement()
   1.401 +//
   1.402 +// reads the next non-blank statement or line
   1.403 +//
   1.404 +	{
   1.405 +	TInt r=ReadNextLine();
   1.406 +	if (r!=KErrNone || !IsStatement())
   1.407 +		return r;
   1.408 +	TChar c=0;
   1.409 +	while (c!=';')
   1.410 +		{
   1.411 +		c=iLine.Get();
   1.412 +		if (!c)							// nothing left to read
   1.413 +			{
   1.414 +			r=AppendNextLine();
   1.415 +			if (r!=KErrNone)
   1.416 +				return r;
   1.417 +			}
   1.418 +		}
   1.419 +	iLine.UnGet();						// the semi-colon
   1.420 +	iStatement=iLine.MarkedToken();
   1.421 +	iLine.Get();						// the semi-colon
   1.422 +	iLine.SkipSpaceAndMark();
   1.423 +	return KErrNone;
   1.424 +	}
   1.425 +
   1.426 +TPtrC TScript::Statement()
   1.427 +	{
   1.428 +	return iStatement.Remainder();
   1.429 +	}
   1.430 +
   1.431 +void TScript::ConsumeLine()
   1.432 +	{
   1.433 +	iLine=TPtrC();
   1.434 +	iStatement=TPtrC();
   1.435 +	}
   1.436 +
   1.437 +void TScript::ConsumeStatement()
   1.438 +	{
   1.439 +	iLine=iLine.Remainder();
   1.440 +	iStatement=TPtrC();
   1.441 +	}
   1.442 +
   1.443 +TInt TScript::ReadNextLine()
   1.444 +//
   1.445 +// reads the next non-blank line into iLine
   1.446 +//
   1.447 +	{
   1.448 +	while (iLine.Remainder().Length()==0)
   1.449 +		{
   1.450 +		TInt r=iInput.Read(iBuf);
   1.451 +		if (r!=KErrNone)
   1.452 +			return r;
   1.453 +		if (iBuf.Length()>0 && iBuf[0]==0xfeff)	// unicode stream marker
   1.454 +			iBuf.Delete(0,1);
   1.455 +		iLineNo++;
   1.456 +		iLine=iBuf;
   1.457 +		iLine.SkipSpaceAndMark();
   1.458 +		} 
   1.459 +	return KErrNone;
   1.460 +	}
   1.461 +
   1.462 +TInt TScript::AppendNextLine()
   1.463 +//
   1.464 +// adds next line from file to iLine
   1.465 +//
   1.466 +	{
   1.467 +	TScriptLine line;
   1.468 +	do {
   1.469 +		TInt r=iInput.Read(line);
   1.470 +		if (r!=KErrNone)
   1.471 +			return r;
   1.472 +		iLineNo++;
   1.473 +		} while (line.Length()==0);
   1.474 +	iBuf=iLine.MarkedToken();
   1.475 +	iBuf.Append(line);
   1.476 +	iLine=iBuf;
   1.477 +	return KErrNone;
   1.478 +	}
   1.479 +
   1.480 +void TScript::WriteError(const TDesC& aLine)
   1.481 +	{
   1.482 +	TScriptLine line;
   1.483 +	line.Format(_L("Error at line %d: %S\n"),iLineNo,&aLine);
   1.484 +	WriteLine(line);
   1.485 +	test.Printf(line);
   1.486 +	}
   1.487 +
   1.488 +void TScript::WriteSqlError(const TResults& aResults,const TDesC& aLine)
   1.489 +	{
   1.490 +	TScriptLine line;
   1.491 +	line.Format(_L("Error at line %d: %S :-\n"),aResults.iLineNo,&aLine);
   1.492 +	WriteLine(line);
   1.493 +	test.Printf(line);
   1.494 +	line.Format(_L("\t%S\n"),&aResults.iLine);
   1.495 +	WriteLine(line);
   1.496 +	test.Printf(line);
   1.497 +	}
   1.498 +
   1.499 +void TScript::WriteLine(const TDesC& aLine)
   1.500 +	{
   1.501 +	TScriptLine l=aLine;
   1.502 +	l.Append('\r');
   1.503 +	iOutput.Write(l);
   1.504 +	}
   1.505 +
   1.506 +void TScript::WriteComment(const TDesC& aLine)
   1.507 +	{
   1.508 +	TScriptLine line;	
   1.509 +	line.Format(_L("\n%S"),&aLine);
   1.510 +	WriteLine(line);
   1.511 +	}
   1.512 +
   1.513 +TInt TScript::IntValue()
   1.514 +//
   1.515 +// returns the integer n from the ' = n ' which must follow in the statement
   1.516 +//
   1.517 +	{
   1.518 +	TScriptToken keyword;
   1.519 +	GetNextTokenFromStatement(keyword);
   1.520 +	if (keyword.Compare(_L("="))!=0)
   1.521 +		WriteError(_L("expected '=' missing"));
   1.522 +	iStatement.SkipSpaceAndMark();
   1.523 +	TInt num=0;
   1.524 +	TInt err=iStatement.Val(num);
   1.525 +	if (err!=KErrNone)
   1.526 +		WriteError(_L("expected number missing"));
   1.527 +	return num;
   1.528 +	}
   1.529 +
   1.530 +TKeyword TScript::Keyword(const TDesC& aKeyword) const
   1.531 +	{
   1.532 +	for (TInt ii=0; ii<ENumKeywords; ++ii)
   1.533 +		{
   1.534 +		if (aKeyword.FindF(KScriptKeywords[ii])==0)
   1.535 +			return TKeyword(ii);
   1.536 +		}
   1.537 +	return EUnknown;
   1.538 +	}
   1.539 +
   1.540 +void TScript::GetNextTokenFromStatement(TDes& aToken)
   1.541 +//
   1.542 +// gets the next token from iStatement
   1.543 +//
   1.544 +	{
   1.545 +	iStatement.SkipSpaceAndMark();
   1.546 +	TUint c;
   1.547 +	do
   1.548 +		{
   1.549 +		c=iStatement.Get();
   1.550 +		if (c=='=' || c=='!')
   1.551 +			break;
   1.552 +		} while (iStatement.Peek().IsAlphaDigit());
   1.553 +	aToken=iStatement.MarkedToken();
   1.554 +	iStatement.SkipSpaceAndMark();
   1.555 +	}
   1.556 +
   1.557 +void TScript::GetNextTokenFromLine(TDes& aToken)
   1.558 +//
   1.559 +// gets the next token from iLine
   1.560 +//
   1.561 +	{
   1.562 +	iLine.SkipSpaceAndMark();
   1.563 +	TUint c=0;
   1.564 +	TChar cc=c;
   1.565 +	TBool literal=EFalse;
   1.566 +	do
   1.567 +		{
   1.568 +		c=iLine.Get();
   1.569 +		if (!c)
   1.570 +			{
   1.571 +			AppendNextLine();
   1.572 +			iLine.SkipSpaceAndMark();
   1.573 +			c=iLine.Get();
   1.574 +			}
   1.575 +		if (c=='\'' || c=='#')
   1.576 +			literal=!literal;
   1.577 +		if ((c==',' || c=='(' || c==')' || c=='{' || c=='}' || c=='!' ) && !literal)
   1.578 +			break;
   1.579 +		cc=iLine.Peek();
   1.580 +		} while (cc.IsAlphaDigit() || literal || TUint(cc)=='.' || TUint(cc)=='+' || TUint(cc)=='-');
   1.581 +	aToken=iLine.MarkedToken();
   1.582 +	if (TUint(cc)==';')					// ignore semi-colons - they're optional
   1.583 +		iLine.Get();
   1.584 +	iLine.SkipSpaceAndMark();
   1.585 +	if (c=='!')
   1.586 +		{
   1.587 +		iStatement=iLine.Remainder();
   1.588 +		TPtrC comment=iStatement.Remainder();
   1.589 +		WriteComment(comment); 
   1.590 +		iLine=TPtrC();
   1.591 +		GetNextTokenFromLine(aToken);
   1.592 +		}
   1.593 +	}
   1.594 +
   1.595 +
   1.596 +//
   1.597 +// class TScriptEngine
   1.598 +//
   1.599 +
   1.600 +TScriptEngine::TScriptEngine()
   1.601 +	: iTextComparison(EDbCompareNormal),iTimer(iScript),iEcho(EFalse),iWindow(EFalse),iAccess(RDbRowSet::EReadOnly)
   1.602 +	{} 
   1.603 +
   1.604 +
   1.605 +void TScriptEngine::RunL() 
   1.606 +//
   1.607 +// runs the script file
   1.608 +//
   1.609 +	{
   1.610 +	while (iScript.ReadNextStatement()!=KErrEof)
   1.611 +		ExecuteL();
   1.612 +	TestForNoError(); 
   1.613 +	}
   1.614 +
   1.615 +void TScriptEngine::TestForNoError() 
   1.616 +	{
   1.617 +	if (iResults.iError!=KErrNone)
   1.618 +		{
   1.619 +		TScriptLine line;
   1.620 +		line.Format(_L("unexpected error %d"),iResults.iError);
   1.621 +		FatalSqlError(line);
   1.622 +		}
   1.623 +	}
   1.624 +
   1.625 +void TScriptEngine::ExecuteL()
   1.626 +	{
   1.627 +	if (ExecuteScriptL()!=KErrNone && ExecuteSql()!=KErrNone)
   1.628 +		FatalSqlError(_L("syntax error"));
   1.629 +	}
   1.630 +
   1.631 +TInt TScriptEngine::ExecuteScriptL()
   1.632 +	{
   1.633 +	TKeyword keyword=iScript.Keyword(iScript.Statement());
   1.634 +	if (keyword!=EError)
   1.635 +		TestForNoError(); 
   1.636 +	switch (keyword)
   1.637 +		{
   1.638 +	case EPrint:	
   1.639 +		DoPrintL();
   1.640 +		break;
   1.641 +	case ERows:
   1.642 +		DoRows();
   1.643 +		break;
   1.644 +	case ELoad:
   1.645 +		DoLoadDb();
   1.646 +		break;
   1.647 +	case EEcho:
   1.648 +		DoEcho();
   1.649 +		break;
   1.650 +	case EWindow:
   1.651 +		DoWindow();
   1.652 +		break;
   1.653 +	case EAccess:
   1.654 +		DoAccess();
   1.655 +		break;
   1.656 +	case ECompare:
   1.657 +		DoCompareL();
   1.658 +		break;
   1.659 +	case EError:
   1.660 +		DoError();
   1.661 +		break;
   1.662 +	case EPopulate:
   1.663 +		DoPopulate();
   1.664 +		break;
   1.665 +	case EComment:
   1.666 +		DoComment();
   1.667 +		break;
   1.668 +	case EResults:
   1.669 +		DoResultsL();
   1.670 +		break;
   1.671 +	case EBuild:
   1.672 +		DoBuildTable();
   1.673 +		break;
   1.674 +	case EQuery:
   1.675 +		DoQuery();
   1.676 +		break;
   1.677 +	case ENormal:
   1.678 +	case EFolded:
   1.679 +	case ECollated:
   1.680 +		DoTextComparison(keyword);
   1.681 +		break;
   1.682 +	case EStart:
   1.683 +		DoStartTimer();
   1.684 +		break;
   1.685 +	case EStop:
   1.686 +		DoStopTimer();
   1.687 +		break;
   1.688 +	case EUnknown:
   1.689 +	default:
   1.690 +		return KErrNotFound;
   1.691 +		}
   1.692 +	return KErrNone;
   1.693 +	}
   1.694 +
   1.695 +TInt TScriptEngine::ExecuteSql()
   1.696 +	{
   1.697 +	return ExecuteSql(iScript.Statement());
   1.698 +	}
   1.699 +
   1.700 +TInt TScriptEngine::ExecuteSql(const TDesC& aSql)
   1.701 +	{
   1.702 +	if (iEcho)
   1.703 +		{
   1.704 +		TScriptLine line(_L("\nSQL:\t"));
   1.705 +		line.Append(aSql);
   1.706 +		iScript.WriteLine(line);
   1.707 +		}
   1.708 +	iResults.iLineNo=iScript.LineNo();
   1.709 +	iResults.iLine=aSql;
   1.710 +	TSqlStatement statement(aSql,iResults);
   1.711 +	return statement.Execute(iTextComparison,iAccess,iWindow);
   1.712 +	}
   1.713 +
   1.714 +void TScriptEngine::DoStartTimer()
   1.715 +	{
   1.716 +	// test its right function
   1.717 +	TScriptToken keyword;
   1.718 +	iScript.GetNextTokenFromStatement(keyword);
   1.719 +	test(keyword.CompareF(_L("START"))==0);
   1.720 +	iScript.GetNextTokenFromStatement(keyword);
   1.721 +	test(keyword.CompareF(_L("TIMER"))==0);
   1.722 +	//
   1.723 +	iTimer.Start(_L("Timer started"));
   1.724 +	}
   1.725 +
   1.726 +void TScriptEngine::DoStopTimer()
   1.727 +	{
   1.728 +	// test its right function
   1.729 +	TScriptToken keyword;
   1.730 +	iScript.GetNextTokenFromStatement(keyword);
   1.731 +	test(keyword.CompareF(_L("STOP"))==0);
   1.732 +	iScript.GetNextTokenFromStatement(keyword);
   1.733 +	test(keyword.CompareF(_L("TIMER"))==0);
   1.734 +	//
   1.735 +	iTimer.Stop(_L("Timer stopped"));
   1.736 +	}
   1.737 +
   1.738 +void TScriptEngine::DoTextComparison(TKeyword aKeyword)
   1.739 +	{
   1.740 +	TPtrC line;
   1.741 +	switch (aKeyword)
   1.742 +		{
   1.743 +	case ENormal:
   1.744 +		iTextComparison=EDbCompareNormal;
   1.745 +		line.Set(_L("[Normal text comparison]"));
   1.746 +		break;
   1.747 +	case EFolded:
   1.748 +		iTextComparison=EDbCompareFolded;
   1.749 +		line.Set(_L("[Folded text comparison]"));
   1.750 +		break;
   1.751 +	case ECollated:
   1.752 +		iTextComparison=EDbCompareCollated;
   1.753 +		line.Set(_L("[Collated text comparison]"));
   1.754 +		break;
   1.755 +	default:
   1.756 +		test(0);
   1.757 +		}
   1.758 +	iScript.WriteLine(line);
   1.759 +	}
   1.760 +
   1.761 +void TScriptEngine::DoComment()
   1.762 +	{
   1.763 +	// test its right function
   1.764 +	TScriptToken keyword;
   1.765 +	iScript.GetNextTokenFromStatement(keyword);
   1.766 +	test(keyword.CompareF(_L("!"))==0);
   1.767 +	//
   1.768 +	TPtrC comment=iScript.Statement();
   1.769 +	iScript.WriteComment(comment);
   1.770 +	iScript.ConsumeLine();
   1.771 +	}
   1.772 +
   1.773 +void TScriptEngine::DoError()
   1.774 +	{
   1.775 +	// test its right function
   1.776 +	TScriptToken keyword;
   1.777 +	iScript.GetNextTokenFromStatement(keyword);
   1.778 +	test(keyword.CompareF(_L("ERROR"))==0);
   1.779 +	//
   1.780 +	TScriptLine line;
   1.781 +	if (iScript.Statement().Length()==0)
   1.782 +		{
   1.783 +		if (iResults.iError>=0)
   1.784 +			FatalSqlError(_L("no error when one was expected"));
   1.785 +		line=_L("\t\tERROR OK");
   1.786 +		}
   1.787 +	else
   1.788 +		{
   1.789 +		TInt err=iScript.IntValue();
   1.790 +		if (iResults.iError!=err)
   1.791 +			{
   1.792 +			line.Format(_L("expected error %d, actual error %d"),err,iResults.iError);
   1.793 +			FatalSqlError(line);
   1.794 +			}
   1.795 +		line.Format(_L("\t\tERROR=%D OK"),err);
   1.796 +		}
   1.797 +	iResults.iError=0;
   1.798 +	iScript.WriteLine(line);
   1.799 +	}
   1.800 +
   1.801 +void TScriptEngine::DoRows()
   1.802 +	{
   1.803 +	// test its right function
   1.804 +	TScriptToken keyword;
   1.805 +	iScript.GetNextTokenFromStatement(keyword);
   1.806 +	test(keyword.CompareF(_L("ROWS"))==0);
   1.807 +	//
   1.808 +	TScriptLine line;
   1.809 +	TInt rows=iScript.IntValue();
   1.810 +	if (iResults.iRows!=rows)
   1.811 +		{
   1.812 +		line.Format(_L("expected rows %d, actual rows %d"),rows,iResults.iRows);
   1.813 +		FatalSqlError(line);
   1.814 +		}
   1.815 +	line.Format(_L("\t\tROWS=%D OK"),rows);
   1.816 +	iScript.WriteLine(line);
   1.817 +	}
   1.818 +
   1.819 +void TScriptEngine::DoLoadDb()
   1.820 +	{
   1.821 +	// test its right function
   1.822 +	TScriptToken token;
   1.823 +	iScript.GetNextTokenFromStatement(token);
   1.824 +	test(token.CompareF(_L("LOAD"))==0);
   1.825 +	//
   1.826 +	TFileName database(iScript.Statement());
   1.827 +	TheDatabase.Close();
   1.828 +
   1.829 +	TInt err = TheCrcChecker.GenerateCrcL(KTestDatabase);
   1.830 +	test(err==KErrNone);
   1.831 +
   1.832 +	TScriptLine line;
   1.833 +	line.Format(_L("Opening database: %S"),&database);
   1.834 +	iScript.WriteLine(line);
   1.835 +	test(TheDatabase.Open(TheFs,database)==KErrNone);
   1.836 +	}
   1.837 +
   1.838 +void TScriptEngine::DoEcho()
   1.839 +	{
   1.840 +	// test its right function
   1.841 +	TScriptToken keyword;
   1.842 +	iScript.GetNextTokenFromStatement(keyword);
   1.843 +	test(keyword.CompareF(_L("ECHO"))==0);
   1.844 +	//
   1.845 +	iScript.GetNextTokenFromStatement(keyword);
   1.846 +	if (keyword.CompareF(_L("OFF"))==0)
   1.847 +		{
   1.848 +		iEcho=EFalse;
   1.849 +		iScript.WriteLine(_L("Echo is off"));
   1.850 +		}
   1.851 +	else if (keyword.CompareF(_L("ON"))==0)
   1.852 +		{
   1.853 +		iEcho=ETrue;
   1.854 +		iScript.WriteLine(_L("Echo is on"));
   1.855 +		}
   1.856 +	else
   1.857 +		FatalError(_L("Expected ON|OFF to follow ECHO statement"));
   1.858 +	}
   1.859 +
   1.860 +void TScriptEngine::DoWindow()
   1.861 +	{
   1.862 +	// test its right function
   1.863 +	TScriptToken keyword;
   1.864 +	iScript.GetNextTokenFromStatement(keyword);
   1.865 +	test(keyword.CompareF(_L("WINDOW"))==0);
   1.866 +	//
   1.867 +	iScript.GetNextTokenFromStatement(keyword);
   1.868 +	if (keyword.CompareF(_L("OFF"))==0)
   1.869 +		{
   1.870 +		iWindow=EFalse;
   1.871 +		iScript.WriteLine(_L("Window is off"));
   1.872 +		}
   1.873 +	else if (keyword.CompareF(_L("ON"))==0)
   1.874 +		{
   1.875 +		iWindow=ETrue;
   1.876 +		iScript.WriteLine(_L("Window is on"));
   1.877 +		}
   1.878 +	else
   1.879 +		FatalError(_L("Expected ON|OFF to follow WINDOW statement"));
   1.880 +	}
   1.881 +
   1.882 +void TScriptEngine::DoAccess()
   1.883 +	{
   1.884 +	// test its right function
   1.885 +	TScriptToken keyword;
   1.886 +	iScript.GetNextTokenFromStatement(keyword);
   1.887 +	test(keyword.CompareF(_L("ACCESS"))==0);
   1.888 +	//
   1.889 +	iScript.GetNextTokenFromStatement(keyword);
   1.890 +	if (keyword.CompareF(_L("UPDATE"))==0)
   1.891 +		{
   1.892 +		iAccess=RDbRowSet::EUpdatable;
   1.893 +		iScript.WriteLine(_L("Access is updateable"));
   1.894 +		}
   1.895 +	else if (keyword.CompareF(_L("READ"))==0)
   1.896 +		{
   1.897 +		iAccess=RDbRowSet::EReadOnly;
   1.898 +		iScript.WriteLine(_L("Access is read only"));
   1.899 +		}
   1.900 +	else if (keyword.CompareF(_L("INSERT"))==0)
   1.901 +		{
   1.902 +		iAccess=RDbRowSet::EInsertOnly;
   1.903 +		iScript.WriteLine(_L("Access is insert only"));
   1.904 +		}
   1.905 +	else
   1.906 +		FatalError(_L("Expected UPDATE|INSERT|READ to follow ACCESS statement"));
   1.907 +	}
   1.908 +
   1.909 +void TScriptEngine::DoResultsL()
   1.910 +	{
   1.911 +	// test its right function
   1.912 +	TScriptToken token;
   1.913 +	iScript.GetNextTokenFromLine(token);
   1.914 +	test(token.CompareF(_L("RESULTS"))==0);
   1.915 +	//
   1.916 +	iScript.GetNextTokenFromLine(token);
   1.917 +	if (token.Compare(_L("{"))!=0)		
   1.918 +		FatalError(_L("missing '{'"));
   1.919 +	iScript.GetNextTokenFromLine(token);					// first value
   1.920 +	TLex value;
   1.921 +	RDbRowSet& rowset=iResults.iView;
   1.922 +	CDbColSet* colset=rowset.ColSetL();
   1.923 +	CleanupStack::PushL(colset);
   1.924 +	TDbColNo colno=colset->ColNo(KRowIdColName);
   1.925 +	CArrayFixFlat<TInt>* rowIdScript=new CArrayFixFlat<TInt>(4);
   1.926 +	CleanupStack::PushL(rowIdScript);
   1.927 +	CArrayFixFlat<TInt>* rowIdView=new CArrayFixFlat<TInt>(4);
   1.928 +	CleanupStack::PushL(rowIdView);
   1.929 +	rowset.BeginningL();
   1.930 +	while (rowset.NextL())				
   1.931 +		{
   1.932 +		rowset.GetL();
   1.933 +		TUint rIdScript;
   1.934 +		value=token;
   1.935 +		if (value.Val(rIdScript)!=KErrNone)
   1.936 +			{
   1.937 +			TScriptLine line;
   1.938 +			line.Format(_L("Unable to extract row id from \"%S\""),&token);
   1.939 +			FatalError(line);
   1.940 +			}
   1.941 +		TUint rIdView=rowset.ColUint(colno);
   1.942 +		rowIdScript->AppendL(rIdScript);
   1.943 +		rowIdView->AppendL(rIdView);
   1.944 +		iScript.GetNextTokenFromLine(token);			
   1.945 +		if (token.Compare(_L(","))==0 || token.Compare(_L("}"))==0)
   1.946 +			{
   1.947 +			if (rowIdScript->Count())
   1.948 +				{
   1.949 +				TKeyArrayFix key(0,ECmpTInt);
   1.950 +				rowIdScript->Sort(key);
   1.951 +				rowIdView->Sort(key);
   1.952 +				for (TInt ii=0;ii<rowIdScript->Count();++ii)
   1.953 +					{
   1.954 +					TInt expectedId=(*rowIdScript)[ii];
   1.955 +					TInt actualId=(*rowIdView)[ii];
   1.956 +					if (actualId!=expectedId)
   1.957 +						{
   1.958 +						TScriptLine line;
   1.959 +						line.Format(_L("expected row id %d, actual row id %d"),actualId,expectedId);
   1.960 +						FatalError(line);
   1.961 +						}
   1.962 +					}
   1.963 +				rowIdScript->Reset();
   1.964 +				rowIdView->Reset();
   1.965 +				}
   1.966 +			if (token.Compare(_L(","))==0)
   1.967 +				iScript.GetNextTokenFromLine(token);
   1.968 +			}
   1.969 +		}
   1.970 +	if (token.Compare(_L("}"))!=0)
   1.971 +		FatalError(_L("too many results expected"));
   1.972 +	CleanupStack::PopAndDestroy(3);	// arrays + colset
   1.973 +	iScript.ConsumeStatement();
   1.974 +	}
   1.975 +
   1.976 +void TScriptEngine::DoBuildTable()
   1.977 +//
   1.978 +// same as Sql create statement, but adds a counter
   1.979 +//
   1.980 +	{
   1.981 +	// test its right function
   1.982 +	TScriptToken keyword;
   1.983 +	iScript.GetNextTokenFromStatement(keyword);
   1.984 +	test(keyword.CompareF(_L("BUILD"))==0);
   1.985 +	//
   1.986 +	TScriptLine sql(_L("CREATE "));
   1.987 +	sql.Append(iScript.Statement());
   1.988 +	TInt pos=sql.Find(_L("("));
   1.989 +	sql.Insert(++pos,_L("Rw COUNTER,"));
   1.990 +	iScript.ConsumeStatement();
   1.991 +	ExecuteSql(sql);
   1.992 +	}
   1.993 +
   1.994 +void TScriptEngine::DoQuery()
   1.995 +//
   1.996 +// same as Sql select statement, but makes sure counter is included
   1.997 +//
   1.998 +	{
   1.999 +	// test its right function
  1.1000 +	TScriptToken keyword;
  1.1001 +	iScript.GetNextTokenFromStatement(keyword);
  1.1002 +	test(keyword.CompareF(_L("QUERY"))==0);
  1.1003 +	//
  1.1004 +	TScriptLine sql(iScript.Statement());
  1.1005 +	if (sql.Find(_L("*"))!=0)
  1.1006 +		{
  1.1007 +		sql.Insert(0,_L(","));
  1.1008 +		sql.Insert(0,KRowIdColName);
  1.1009 +		}
  1.1010 +	sql.Insert(0,_L("SELECT "));
  1.1011 +	iScript.ConsumeStatement();
  1.1012 +	ExecuteSql(sql);
  1.1013 +	}
  1.1014 +
  1.1015 +void TScriptEngine::FatalError(const TDesC& aLine)
  1.1016 +	{
  1.1017 +	iScript.WriteError(aLine);
  1.1018 +	test(0);
  1.1019 +	}
  1.1020 +
  1.1021 +void TScriptEngine::FatalError()
  1.1022 +	{
  1.1023 +	FatalError(_L("wrong expected value"));
  1.1024 +	}
  1.1025 +
  1.1026 +void TScriptEngine::FatalSqlError(const TDesC& aLine)
  1.1027 +	{
  1.1028 +	iScript.WriteSqlError(iResults,aLine);
  1.1029 +	test(0);
  1.1030 +	}
  1.1031 +
  1.1032 +void TScriptEngine::DoPopulate()
  1.1033 +	{
  1.1034 +	// check its right function
  1.1035 +	TScriptToken token;
  1.1036 +	iScript.GetNextTokenFromLine(token);
  1.1037 +	test(token.CompareF(_L("POPULATE"))==0);
  1.1038 +	//
  1.1039 +	TScriptLine sqlbase=_L("INSERT INTO ");
  1.1040 +	iScript.GetNextTokenFromLine(token);	// table name
  1.1041 +	sqlbase.AppendFormat(_L("%S "),&token);	
  1.1042 +	iScript.GetNextTokenFromLine(token);
  1.1043 +	if (token.Compare(_L("("))==0)			// optional column names present?
  1.1044 +		{			
  1.1045 +		for (;;) 							
  1.1046 +			{
  1.1047 +			sqlbase.AppendFormat(token);
  1.1048 +			if (token.Compare(_L(")"))==0)
  1.1049 +				break;
  1.1050 +			iScript.GetNextTokenFromLine(token);		
  1.1051 +			} 
  1.1052 +		iScript.GetNextTokenFromLine(token);
  1.1053 +		}
  1.1054 +	if (token.Compare(_L("{"))!=0)		
  1.1055 +		FatalError(_L("missing '{'"));
  1.1056 +	sqlbase.AppendFormat(_L(" VALUES ("));
  1.1057 +	iScript.GetNextTokenFromLine(token);	// first value
  1.1058 +	TheDatabase.Begin();					// all in same transaction
  1.1059 +	for (;;)
  1.1060 +		{
  1.1061 +		if (token.Compare(_L("}"))==0)	
  1.1062 +			break;
  1.1063 +		TScriptLine sql=sqlbase;
  1.1064 +		for (;;)
  1.1065 +			{
  1.1066 +			sql.Append(token);
  1.1067 +			iScript.GetNextTokenFromLine(token);
  1.1068 +			if (token.Compare(_L(","))==0)
  1.1069 +				{
  1.1070 +				sql.Append(token);			// comma
  1.1071 +				iScript.GetNextTokenFromLine(token);
  1.1072 +				}
  1.1073 +			else
  1.1074 +				break;
  1.1075 +			}
  1.1076 +		sql.AppendFormat(_L(")"));
  1.1077 +		ExecuteSql(sql);
  1.1078 +		TestForNoError();
  1.1079 +		}
  1.1080 +	TheDatabase.Commit();
  1.1081 +	iScript.ConsumeStatement();
  1.1082 +	}
  1.1083 +
  1.1084 +void TScriptEngine::DoPrintL() 
  1.1085 +	{
  1.1086 +	// check its right function
  1.1087 +	TScriptToken keyword;
  1.1088 +	iScript.GetNextTokenFromStatement(keyword);
  1.1089 +	test(keyword.CompareF(_L("PRINT"))==0);
  1.1090 +	//
  1.1091 +	iScript.GetNextTokenFromStatement(keyword);
  1.1092 +	if (keyword.CompareF(_L("VIEW"))==0)
  1.1093 +		PrintL(iResults.iView);
  1.1094 +	else if (keyword.CompareF(_L("TABLE"))==0)
  1.1095 +		{
  1.1096 +		iScript.GetNextTokenFromStatement(keyword);	// name of table
  1.1097 +		RDbTable table;
  1.1098 +		TInt err=table.Open(TheDatabase,keyword,table.EReadOnly);
  1.1099 +		if (err!=KErrNone)
  1.1100 +			FatalError(_L("unable to open table"));
  1.1101 +		PrintL(table);
  1.1102 +		table.Close();
  1.1103 +		}
  1.1104 +	else
  1.1105 +		FatalError(_L("expected VIEW or TABLE keyword not present"));
  1.1106 +	}
  1.1107 +
  1.1108 +void TScriptEngine::DoCompareL() 
  1.1109 +	{
  1.1110 +	// check its right function
  1.1111 +	TScriptToken keyword;
  1.1112 +	iScript.GetNextTokenFromLine(keyword);
  1.1113 +	test(keyword.CompareF(_L("COMPARE"))==0);
  1.1114 +	//
  1.1115 +	iScript.GetNextTokenFromLine(keyword);
  1.1116 +	if (keyword.CompareF(_L("VIEW"))==0)
  1.1117 +		CompareL(iResults.iView);
  1.1118 +	else if (keyword.CompareF(_L("TABLE"))==0)
  1.1119 +		{
  1.1120 +		iScript.GetNextTokenFromLine(keyword);	// name of table
  1.1121 +		RDbTable table;
  1.1122 +		TInt err=table.Open(TheDatabase,keyword,table.EReadOnly);
  1.1123 +		if (err!=KErrNone)
  1.1124 +			FatalError(_L("unable to open table"));
  1.1125 +		CompareL(table);
  1.1126 +		table.Close();
  1.1127 +		}
  1.1128 +	else
  1.1129 +		FatalError(_L("expected VIEW or TABLE keyword not present"));
  1.1130 +	}
  1.1131 +
  1.1132 +void TScriptEngine::CompareL(RDbRowSet& aRowSet) 
  1.1133 +	{
  1.1134 +	TScriptToken token;
  1.1135 +	iScript.GetNextTokenFromLine(token);
  1.1136 +	TBool rowIdMode=EFalse;
  1.1137 +	CArrayFixFlat<TInt>* rowIdToTest=new CArrayFixFlat<TInt>(4);
  1.1138 +	CleanupStack::PushL(rowIdToTest);
  1.1139 +	if (token.Compare(_L("("))==0)				// optional row ids present?
  1.1140 +		{
  1.1141 +		rowIdMode=ETrue;
  1.1142 +		iScript.GetNextTokenFromLine(token);	// first value
  1.1143 +		TLex value;
  1.1144 +		TInt rowId;
  1.1145 +		for (;;) 							
  1.1146 +			{
  1.1147 +			value=token;
  1.1148 +			test (value.Val(rowId)==KErrNone);
  1.1149 +			rowIdToTest->AppendL(rowId);		// add row id to array
  1.1150 +			iScript.GetNextTokenFromLine(token);
  1.1151 +			if (token.Compare(_L(")"))==0)
  1.1152 +				break;
  1.1153 +			if (token.Compare(_L(","))==0)
  1.1154 +				iScript.GetNextTokenFromLine(token);					
  1.1155 +			} 
  1.1156 +		iScript.GetNextTokenFromLine(token);
  1.1157 +		}
  1.1158 +	if (token.Compare(_L("{"))!=0)		
  1.1159 +		FatalError(_L("missing '{'"));
  1.1160 +	TInt columns=aRowSet.ColCount();
  1.1161 +	CDbColSet* colset=aRowSet.ColSetL();
  1.1162 +	aRowSet.BeginningL();
  1.1163 +	while (aRowSet.NextL())
  1.1164 +		{
  1.1165 +		aRowSet.GetL();
  1.1166 +		if (rowIdMode)
  1.1167 +			{
  1.1168 +			TInt currentId=aRowSet.ColUint(colset->ColNo(KRowIdColName));
  1.1169 +			TBool toTest=EFalse;
  1.1170 +			for (TInt jj=0; jj<rowIdToTest->Count(); ++jj)
  1.1171 +				{
  1.1172 +				if (currentId==(*rowIdToTest)[jj])
  1.1173 +					toTest=ETrue;
  1.1174 +				}
  1.1175 +			if (!toTest)
  1.1176 +				continue;
  1.1177 +			}
  1.1178 +		for (TInt ii=1;ii<=columns;++ii)
  1.1179 +			{
  1.1180 +			if (rowIdMode && ii==colset->ColNo(KRowIdColName))	// ignore row id column
  1.1181 +				continue;
  1.1182 +			const TDbCol& col=(*colset)[ii];
  1.1183 +			iScript.GetNextTokenFromLine(token);				// value
  1.1184 +			if (token.Compare(_L(","))==0)
  1.1185 +				iScript.GetNextTokenFromLine(token);			// ignore comma
  1.1186 +			if (aRowSet.IsColNull(ii))
  1.1187 +				{
  1.1188 +				if (token.CompareF(_L("NULL"))!=0)
  1.1189 +					FatalError(_L("NULL expected"));
  1.1190 +				continue;
  1.1191 +				}
  1.1192 +			CompareValues(aRowSet,ii,col.iType,token); 
  1.1193 +			}
  1.1194 +		}
  1.1195 +	delete colset;
  1.1196 +	CleanupStack::PopAndDestroy();				// rowIdToTest
  1.1197 +	iScript.GetNextTokenFromLine(token);		// look for closing '}'
  1.1198 +	if (token.Compare(_L("}"))!=0)		
  1.1199 +		FatalError(_L("missing '}'"));
  1.1200 +	iScript.ConsumeStatement();
  1.1201 +	}
  1.1202 +
  1.1203 +void TScriptEngine::CompareValues(RDbRowSet& aRowSet,TDbColNo ColNo,TDbColType aType,const TDesC& aToken) 
  1.1204 +//
  1.1205 +// compares the value from a rowset aRowset, colimn number aColNo and of type aType, with the value
  1.1206 +// contained in the descriptor aToken
  1.1207 +//
  1.1208 +	{
  1.1209 +	TLex value=aToken;
  1.1210 +	switch (aType)
  1.1211 +		{
  1.1212 +	case EDbColInt32:
  1.1213 +		{
  1.1214 +		TInt num;
  1.1215 +		test (value.Val(num)==KErrNone);
  1.1216 +		if (num!=aRowSet.ColInt(ColNo))
  1.1217 +			FatalError();
  1.1218 +		break;
  1.1219 +		}
  1.1220 +	case EDbColInt8:
  1.1221 +		{
  1.1222 +		TInt8 num8;
  1.1223 +		test (value.Val(num8)==KErrNone);
  1.1224 +		if (num8!=aRowSet.ColInt8(ColNo))
  1.1225 +			FatalError();
  1.1226 +		break;
  1.1227 +		}
  1.1228 +	case EDbColInt16:
  1.1229 +		{
  1.1230 +		TInt16 num16;
  1.1231 +		test (value.Val(num16)==KErrNone);
  1.1232 +		if (num16!=aRowSet.ColInt16(ColNo))
  1.1233 +			FatalError();
  1.1234 +		break;
  1.1235 +		}
  1.1236 +	case EDbColInt64:
  1.1237 +		{
  1.1238 +		TInt64 num64;
  1.1239 +		test (value.Val(num64)==KErrNone);
  1.1240 +		if (num64!=aRowSet.ColInt64(ColNo))
  1.1241 +			FatalError();
  1.1242 +		break;
  1.1243 +		}
  1.1244 +	case EDbColUint8:
  1.1245 +		{
  1.1246 +		TUint8 numu8;
  1.1247 +		test (value.Val(numu8,EDecimal)==KErrNone);
  1.1248 +		if (numu8!=aRowSet.ColUint8(ColNo))
  1.1249 +			FatalError();
  1.1250 +		break;
  1.1251 +		}
  1.1252 +	case EDbColUint16:
  1.1253 +		{
  1.1254 +		TUint16 numu16;
  1.1255 +		test (value.Val(numu16,EDecimal)==KErrNone);
  1.1256 +		if (numu16!=aRowSet.ColUint16(ColNo))
  1.1257 +			FatalError();
  1.1258 +		break;
  1.1259 +		}
  1.1260 +	case EDbColUint32:
  1.1261 +		{
  1.1262 +		TUint32 numu32;
  1.1263 +		test (value.Val(numu32,EDecimal)==KErrNone);
  1.1264 +		if (numu32!=aRowSet.ColUint32(ColNo))
  1.1265 +			FatalError();
  1.1266 +		break;
  1.1267 +		}
  1.1268 +	case EDbColReal32:
  1.1269 +		{
  1.1270 +		TReal32 numr32;
  1.1271 +		test (value.Val(numr32)==KErrNone);
  1.1272 +		if (numr32!=aRowSet.ColReal32(ColNo))
  1.1273 +			FatalError();
  1.1274 +		break;
  1.1275 +		}
  1.1276 +	case EDbColReal64:
  1.1277 +		{
  1.1278 +		TReal64 numr64;
  1.1279 +		test (value.Val(numr64)==KErrNone);
  1.1280 +		if (numr64!=aRowSet.ColReal64(ColNo))
  1.1281 +			FatalError();
  1.1282 +		break;
  1.1283 +		}
  1.1284 +	case EDbColText8:
  1.1285 +	case EDbColText16:
  1.1286 +		{
  1.1287 +		TPtrC text=aToken.Mid(1,aToken.Length()-2);			// skip quotes
  1.1288 +		if (text.CompareF(aRowSet.ColDes(ColNo))!=0)
  1.1289 +			FatalError();
  1.1290 +		break;
  1.1291 +		}
  1.1292 +	case EDbColDateTime:
  1.1293 +		{
  1.1294 +		TScriptLine time=aToken.Mid(1,aToken.Length()-2);	// skip hashes
  1.1295 +		TTime t1;
  1.1296 +		t1.Parse(time);
  1.1297 +		TTime t2(aRowSet.ColTime(ColNo).DateTime());
  1.1298 +		if (t1!=t2)
  1.1299 +			FatalError();
  1.1300 +		break;
  1.1301 +		}
  1.1302 +	default:
  1.1303 +		break;
  1.1304 +		}
  1.1305 +	}
  1.1306 +
  1.1307 +void TScriptEngine::PrintL(RDbRowSet& aRowSet) 
  1.1308 +	{
  1.1309 +	iScript.WriteLine(TPtrC());
  1.1310 +	TInt columns=aRowSet.ColCount();	
  1.1311 +	CDbColSet* colset=aRowSet.ColSetL();
  1.1312 +	TScriptLine line;
  1.1313 +	for (TInt i=1;i<=columns;++i)
  1.1314 +		{
  1.1315 +		const TDbCol& col=(*colset)[i];
  1.1316 +		line.AppendFormat(_L("%S\t"),&col.iName);
  1.1317 +		}
  1.1318 +	iScript.WriteLine(line);
  1.1319 +	aRowSet.BeginningL();
  1.1320 +	while (aRowSet.NextL())
  1.1321 +		{
  1.1322 +		line=TPtrC();
  1.1323 +		for (TInt ii=1;ii<=columns;++ii)
  1.1324 +			{
  1.1325 +			const TDbCol& col=(*colset)[ii];
  1.1326 +			aRowSet.GetL();
  1.1327 +			if (aRowSet.IsColNull(ii))
  1.1328 +				{
  1.1329 +				line.AppendFormat(_L("NULL\t"));
  1.1330 +				continue;
  1.1331 +				}
  1.1332 +			switch (col.iType)
  1.1333 +				{
  1.1334 +			case EDbColInt32:
  1.1335 +				line.AppendFormat(_L("%d\t"),aRowSet.ColInt(ii));
  1.1336 +				break;
  1.1337 +			case EDbColInt8:
  1.1338 +				line.AppendFormat(_L("%d\t"),aRowSet.ColInt8(ii));
  1.1339 +				break;
  1.1340 +			case EDbColInt16:
  1.1341 +				line.AppendFormat(_L("%d\t"),aRowSet.ColInt16(ii));
  1.1342 +				break;
  1.1343 +			case EDbColInt64:
  1.1344 +				line.AppendFormat(_L("%ld\t"),aRowSet.ColInt64(ii));
  1.1345 +				break;
  1.1346 +			case EDbColUint8:
  1.1347 +				line.AppendFormat(_L("%u\t"),aRowSet.ColUint8(ii));
  1.1348 +				break;
  1.1349 +			case EDbColUint16:
  1.1350 +				line.AppendFormat(_L("%u\t"),aRowSet.ColUint16(ii));
  1.1351 +				break;
  1.1352 +			case EDbColUint32:
  1.1353 +				line.AppendFormat(_L("%u\t"),aRowSet.ColUint(ii));
  1.1354 +				break;
  1.1355 +			case EDbColReal32:
  1.1356 +				line.AppendFormat(_L("%f\t"),aRowSet.ColReal32(ii));
  1.1357 +				break;
  1.1358 +			case EDbColReal64:
  1.1359 +				line.AppendFormat(_L("%f\t"),aRowSet.ColReal64(ii));
  1.1360 +				break;
  1.1361 +			case EDbColText:
  1.1362 +				line.Append(aRowSet.ColDes(ii));
  1.1363 +				line.Append('\t');
  1.1364 +				break;
  1.1365 +			case EDbColDateTime:
  1.1366 +				{
  1.1367 +				TDateTime time(aRowSet.ColTime(ii).DateTime());
  1.1368 +				line.AppendFormat(_L("%d:%d:%d %d/%d/%d"),time.Hour(),time.Minute(),time.Second(),time.Day(),time.Month(),time.Year());
  1.1369 +				}	
  1.1370 +				break;
  1.1371 +			case EDbColLongText:
  1.1372 +				{
  1.1373 +				RDbColReadStream blob;
  1.1374 +				blob.OpenLC(aRowSet,ii);
  1.1375 +				TScriptLine text;
  1.1376 +				blob.ReadL(text,aRowSet.ColLength(ii));
  1.1377 +				CleanupStack::PopAndDestroy();
  1.1378 +				line.AppendFormat(_L("%S\t"),&text);
  1.1379 +				}
  1.1380 +			default:
  1.1381 +				break;
  1.1382 +				}
  1.1383 +			}
  1.1384 +		iScript.WriteLine(line);
  1.1385 +		}
  1.1386 +	iScript.WriteLine(TPtrC());
  1.1387 +	delete colset;
  1.1388 +	}
  1.1389 +
  1.1390 +
  1.1391 +LOCAL_C void CreateDatabaseL()
  1.1392 +//
  1.1393 +// Create the database
  1.1394 +//
  1.1395 +	{
  1.1396 +	test(TheDatabase.Replace(TheFs,KTestDatabase)==KErrNone);
  1.1397 +	}
  1.1398 +
  1.1399 +LOCAL_C void CloseDatabase()
  1.1400 +//
  1.1401 +// Close the database
  1.1402 +//
  1.1403 +	{
  1.1404 +	TheDatabase.Close();
  1.1405 +
  1.1406 +	TInt err = TheCrcChecker.GenerateCrcL(KTestDatabase);
  1.1407 +	test(err==KErrNone);
  1.1408 +	}
  1.1409 +
  1.1410 +LOCAL_C void SetupTestDirectory()
  1.1411 +//
  1.1412 +// Prepare the test directory.
  1.1413 +//
  1.1414 +    {
  1.1415 +	TInt err=TheFs.Connect();
  1.1416 +	test(err==KErrNone);
  1.1417 +//
  1.1418 +	err=TheFs.MkDir(KTestDatabase);
  1.1419 +	test(err==KErrNone || err==KErrAlreadyExists);
  1.1420 +	}
  1.1421 +
  1.1422 +LOCAL_C void SetupCleanup()
  1.1423 +//
  1.1424 +// Initialise the cleanup stack.
  1.1425 +//
  1.1426 +    {
  1.1427 +	TheTrapCleanup=CTrapCleanup::New();
  1.1428 +	test(TheTrapCleanup!=NULL);
  1.1429 +	TRAPD(err,\
  1.1430 +		{\
  1.1431 +		for (TInt i=KTestCleanupStack;i>0;i--)\
  1.1432 +			CleanupStack::PushL((TAny*)0);\
  1.1433 +		CleanupStack::Pop(KTestCleanupStack);\
  1.1434 +		});
  1.1435 +	test(err==KErrNone);
  1.1436 +	}
  1.1437 +
  1.1438 +/**
  1.1439 +@SYMTestCaseID          SYSLIB-DBMS-CT-0632
  1.1440 +@SYMTestCaseDesc        Executes the script files
  1.1441 +@SYMTestPriority        Medium
  1.1442 +@SYMTestActions         Start the script engine 
  1.1443 +@SYMTestExpectedResults Test must not fail
  1.1444 +@SYMREQ                 REQ0000
  1.1445 +*/
  1.1446 +LOCAL_C void RunScriptL()
  1.1447 +	{
  1.1448 +	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0632 Running script "));
  1.1449 +	CreateDatabaseL();
  1.1450 +	TScriptEngine script;
  1.1451 +	script.RunL();
  1.1452 +	CloseDatabase();
  1.1453 +	TheView.Close();
  1.1454 +	}
  1.1455 +
  1.1456 +LOCAL_C void DeleteDataFile(const TDesC& aFullName)
  1.1457 +	{
  1.1458 +	RFs fsSession;
  1.1459 +	TInt err = fsSession.Connect();
  1.1460 +	if(err == KErrNone)
  1.1461 +		{
  1.1462 +		TEntry entry;
  1.1463 +		if(fsSession.Entry(aFullName, entry) == KErrNone)
  1.1464 +			{
  1.1465 +			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
  1.1466 +			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
  1.1467 +			if(err != KErrNone) 
  1.1468 +				{
  1.1469 +				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
  1.1470 +				}
  1.1471 +			err = fsSession.Delete(aFullName);
  1.1472 +			if(err != KErrNone) 
  1.1473 +				{
  1.1474 +				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
  1.1475 +				}
  1.1476 +			}
  1.1477 +		fsSession.Close();
  1.1478 +		}
  1.1479 +	else
  1.1480 +		{
  1.1481 +		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
  1.1482 +		}
  1.1483 +	}
  1.1484 +
  1.1485 +GLDEF_C TInt E32Main()
  1.1486 +    {
  1.1487 +	test.Title();
  1.1488 +	SetupTestDirectory();
  1.1489 +	SetupCleanup();
  1.1490 +	__UHEAP_MARK;
  1.1491 +//
  1.1492 +	TRAPD(err,RunScriptL());
  1.1493 +	test(err==KErrNone);
  1.1494 +
  1.1495 +	//deletion of data files must be done before call to end - DEF047652
  1.1496 +	::DeleteDataFile(KTestDatabase);
  1.1497 +	::DeleteDataFile(KOutputFile);//Comment this line if you want topo keep t_script.log file.	
  1.1498 +	
  1.1499 +
  1.1500 +#ifndef __linux__
  1.1501 +#ifndef __TOOLS2__ 
  1.1502 +	TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
  1.1503 +	test(err==KErrNone);
  1.1504 +	test(lc==KErrNone);
  1.1505 +#else
  1.1506 +	TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
  1.1507 +	TPtrC errmsg;
  1.1508 +	TheCrcChecker.ErrorReportL(err, errmsg);
  1.1509 +	RDebug::Print(errmsg);
  1.1510 +	test(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
  1.1511 +#endif
  1.1512 +#endif
  1.1513 +	
  1.1514 +	test.End();
  1.1515 +//
  1.1516 +	__UHEAP_MARKEND;
  1.1517 +
  1.1518 +	delete TheTrapCleanup;
  1.1519 +
  1.1520 +	TheFs.Close();
  1.1521 +	test.Close();
  1.1522 +
  1.1523 +	return 0;
  1.1524 +    }