os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_dbmsscript.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1998-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 <f32file.h>
    18 #include <e32test.h>
    19 #include <e32math.h>
    20 
    21 #include "crccheck.h"
    22 
    23 #undef __UHEAP_MARK
    24 #define __UHEAP_MARK
    25 #undef __UHEAP_MARKEND
    26 #define __UHEAP_MARKEND
    27 
    28 LOCAL_D TDBMS_CRCChecks TheCrcChecker;
    29 
    30 #ifndef __linux__ //No CRC test on LINUX
    31 #ifdef __TOOLS2__
    32 const TPtrC	KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_DBMSSCRIPT.CRC");
    33 #else
    34 const TPtrC	KCrcRecord=_L("C:\\dbms-tst\\T_DBMSSCRIPT.CRC");
    35 #endif
    36 #endif
    37 
    38 LOCAL_D RTest test(_L("T_DBMSSCRIPT"));
    39 LOCAL_D CTrapCleanup* TheTrapCleanup;
    40 LOCAL_D RDbNamedDatabase TheDatabase;
    41 LOCAL_D RFs TheFs;
    42 LOCAL_D RDbView TheView;
    43 //
    44 
    45 #ifdef __TOOLS2__
    46 const TPtrC KTestDatabase=_L(".\\dbms-tst\\t_script.db");
    47 #else
    48 const TPtrC KTestDatabase=_L("C:\\dbms-tst\\t_script.db");
    49 #endif
    50 
    51 const TPtrC KRomScriptFile=_L("z:\\test\\t_script.txt");
    52 
    53 #ifndef __TOOLS2__
    54 const TPtrC KScriptFile=_L("c:\\dbms-tst\\t_script.txt");
    55 #else
    56 const TPtrC KScriptFile=_L(".\\dbms-tst\\t_script.txt");
    57 #endif
    58 
    59 #ifndef __TOOLS2__
    60 const TPtrC KOutputFile=_L("c:\\dbms-tst\\t_script.log");
    61 #else
    62 const TPtrC KOutputFile=_L(".\\dbms-tst\\t_script.log");
    63 #endif
    64 
    65 const TInt KTestCleanupStack=0x20;
    66 const TPtrC KDDLKeywords[]={_L("CREATE"),_L("DROP"),_L("ALTER")};
    67 const TPtrC KDMLKeywords[]={_L("INSERT"),_L("DELETE"),_L("UPDATE")};
    68 const TPtrC KQueryKeywords[]={_L("SELECT")};
    69 const TPtrC KScriptKeywords[]={_L("PRINT"),_L("ROWS"),_L("COMPARE"),_L("ERROR"),_L("!"),
    70 							   _L("POPULATE"),_L("RESULTS"),_L("BUILD"),_L("QUERY"),
    71 							   _L("NORMAL"),_L("FOLDED"),_L("COLLATED"),_L("START"),_L("STOP"),
    72 							   _L("LOAD"),_L("ECHO"),_L("WINDOW"),_L("ACCESS")};
    73 const TPtrC KRowIdColName=_L("Rw");
    74 enum TKeyword {EPrint,ERows,ECompare,EError,EComment,EPopulate,EResults,EBuild,EQuery,ENormal,
    75                EFolded,ECollated,EStart,EStop,ELoad,EEcho,EWindow,EAccess,ENumKeywords,EUnknown};
    76 //
    77 typedef TBuf<256> TScriptLine;
    78 typedef TBuf<256> TScriptToken;
    79 //
    80 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
    81 
    82 class TScript;
    83 class TTimer
    84 	{
    85 public:
    86 	inline TTimer(TScript& aScript);
    87 	void Start(const TDesC& aDes);
    88 	void Stop(const TDesC& aDes);
    89 private:
    90 	TUint iTicks;
    91 	TScript& iScript;
    92 	};
    93 
    94 class TResults
    95 	{
    96 public:
    97 	inline TResults(); 
    98 public:
    99 	RDbView& iView;
   100 	TInt iError;
   101 	TInt iRows;
   102 	TInt iLineNo;
   103 	TScriptLine iLine;
   104 	};
   105 
   106 class TSqlStatement
   107 	{
   108 private:
   109 	enum TSqlType {EDML,EDDL,EQuery,EUnknown};
   110 public:
   111 	inline TSqlStatement(const TDesC& aSql,TResults& aResults); 
   112 	TInt Execute(TDbTextComparison aTextComparison=EDbCompareNormal,
   113 		RDbRowSet::TAccess aAccess=RDbRowSet::EReadOnly,TBool aWindow=EFalse) const;
   114 private:
   115 	void ExecuteSql(TDbTextComparison aTextComparison) const;
   116 	void ExecuteQuery(RDbRowSet::TAccess aAccess,TBool aWindow) const;
   117 	TSqlType SqlType() const;
   118 private:
   119 	const TDesC& iSql;
   120 	TResults& iResults;
   121 	};
   122 
   123 class TScript
   124 	{
   125 public:
   126 	TScript();
   127 #ifdef __TOOLS2__
   128 	~TScript() { iFile.Close(); };
   129 #endif
   130 	TInt ReadNextStatement();
   131 	void GetNextTokenFromStatement(TDes& aToken);
   132 	void GetNextTokenFromLine(TDes& aToken);
   133 	TPtrC Statement();
   134 	TInt IntValue();
   135 	void WriteLine(const TDesC& aLine);
   136 	void WriteError(const TDesC& aLine);
   137 	void WriteSqlError(const TResults& aResults,const TDesC& aLine); 
   138 	void WriteComment(const TDesC& aLine);
   139 	TKeyword Keyword(const TDesC& aKeyword) const;
   140 	void ConsumeLine();
   141 	void ConsumeStatement();
   142 	inline TInt LineNo() const;
   143 private:
   144 	TInt ReadNextLine();
   145 	TInt AppendNextLine();
   146 	TBool IsStatement();
   147 private:
   148 	TFileText iInput;
   149 	TFileText iOutput;
   150 	TScriptLine iBuf;
   151 	TLex iStatement;
   152 	TLex iLine;
   153 	TInt iLineNo;
   154 #ifdef __TOOLS2__
   155 	RFile iFile;
   156 #endif
   157 	};
   158 
   159 class TScriptEngine
   160 	{
   161 public:
   162 	inline TScriptEngine();
   163 	void RunL();
   164 private:
   165 	void ExecuteL();
   166 	TInt ExecuteScriptL();
   167 	TInt ExecuteSql(const TDesC& aSql);
   168 	TInt ExecuteSql();
   169 	// keyword operations
   170 	void DoPrintL();
   171 	void DoComment();
   172 	void DoError();
   173 	void DoEcho();
   174 	void DoWindow();
   175 	void DoAccess();
   176 	void DoRows();
   177 	void DoCompareL();
   178 	void DoPopulate();
   179 	void DoResultsL();
   180 	void DoBuildTable();
   181 	void DoQuery();
   182 	void DoTextComparison(TKeyword aKeyword);
   183 	void DoStartTimer();
   184 	void DoStopTimer();
   185 	void DoLoadDb();
   186 	//
   187 	void PrintL(RDbRowSet& aRowSet);
   188 	void CompareL(RDbRowSet& aRowSet);
   189 	void CompareValues(RDbRowSet& aRowSet,TDbColNo ColNo,TDbColType aType,const TDesC& aToken);
   190 	void FatalError(const TDesC& aLine);
   191 	void FatalError();
   192 	void FatalSqlError(const TDesC& aLine);
   193 	void TestForNoError(); 
   194 private:
   195 	TScript iScript;
   196 	TResults iResults;
   197 	TDbTextComparison iTextComparison;
   198 	TTimer iTimer;
   199 	TBool iEcho;
   200 	TBool iWindow;
   201 	RDbRowSet::TAccess iAccess;
   202 	
   203 	};
   204 
   205 //
   206 // class TTimer
   207 //
   208 
   209 inline TTimer::TTimer(TScript& aScript)
   210 	: iScript(aScript)
   211 	{}
   212 
   213 void TTimer::Start(const TDesC& aDes)
   214 	{
   215 	TScriptLine line;
   216 	line.Format(_L("%S: "),&aDes);
   217 	iScript.WriteLine(line);
   218 	iTicks=User::TickCount();
   219 	}
   220 
   221 void TTimer::Stop(const TDesC& aDes)
   222 	{
   223 	TScriptLine line;
   224 	line.Format(_L("%S: "),&aDes);
   225 #ifdef __EPOC32__
   226 #define TICK_TIME 15625
   227 #else
   228 #define	TICK_TIME 100000
   229 #endif
   230 	TInt microSec=(User::TickCount()-iTicks)*TICK_TIME;
   231 	TUint sec=microSec/1000000;
   232 	TUint centi=(microSec/10000)-sec*100;
   233 	line.AppendFormat(_L("%u.%02us\n"),sec,centi);
   234 	iScript.WriteLine(line);
   235 	}
   236 
   237 //
   238 // class TResults
   239 //
   240 
   241 inline TResults::TResults()
   242 	: iView(TheView),iError(KErrNone)
   243 	{}
   244 
   245 
   246 //
   247 // class TSqlStatement
   248 //
   249 
   250 inline TSqlStatement::TSqlStatement(const TDesC& aSql,TResults& aResults)
   251 	: iSql(aSql),iResults(aResults)
   252 	{}
   253 
   254 void TSqlStatement::ExecuteSql(TDbTextComparison aTextComparison) const
   255 //
   256 // executes DML or DDL
   257 //
   258 	{
   259 	TInt r=TheDatabase.Execute(iSql,aTextComparison);
   260 	if (r<0)
   261 		iResults.iError=r;
   262 	else
   263 		{
   264 		iResults.iError=KErrNone;
   265 		iResults.iRows=r;
   266 		}
   267 	}
   268 
   269 void TSqlStatement::ExecuteQuery(RDbRowSet::TAccess aAccess,TBool aWindow) const
   270 	{
   271 	iResults.iView.Close();			// discard any previous queries
   272 	TInt& err=iResults.iError;
   273 	if (aWindow)
   274 		err=iResults.iView.Prepare(TheDatabase,iSql,KDbUnlimitedWindow,aAccess);
   275 	else
   276 		err=iResults.iView.Prepare(TheDatabase,iSql,aAccess);
   277 	if (err==KErrNone)
   278 		err=iResults.iView.EvaluateAll();
   279 	}
   280 
   281 TSqlStatement::TSqlType TSqlStatement::SqlType() const
   282 //
   283 // determines the type of sql statement by matching keywords
   284 //
   285 	{
   286 	for (TUint i=0;i<ARRAY_SIZE(KDDLKeywords);++i)
   287 		{
   288 		if (iSql.FindF(KDDLKeywords[i])==0)		
   289 			return EDDL;
   290 		}
   291 	for (TUint ii=0;ii<ARRAY_SIZE(KDMLKeywords);++ii)
   292 		{
   293 		if (iSql.FindF(KDMLKeywords[ii])==0)
   294 			return EDML;
   295 		}
   296 	for (TUint j=0;j<ARRAY_SIZE(KQueryKeywords);++j)
   297 		{
   298 		if (iSql.FindF(KQueryKeywords[j])==0)
   299 			return EQuery;
   300 		}
   301 	return EUnknown;
   302 	}
   303 
   304 TInt TSqlStatement::Execute(TDbTextComparison aTextComparison,RDbRowSet::TAccess aAccess,TBool aWindow) const
   305 //
   306 // executes the sql statement
   307 //
   308 	{
   309 	TInt r=KErrNone;
   310 	switch (SqlType())
   311 		{
   312 	case EDDL:
   313 	case EDML:
   314 		ExecuteSql(aTextComparison);
   315 		break;
   316 	case EQuery:
   317 		ExecuteQuery(aAccess,aWindow);
   318 		break;
   319 	case EUnknown:
   320 	default:
   321 		r=KErrNotFound;
   322 		break;
   323 		}
   324 	return r;
   325 	}
   326 
   327 
   328 //
   329 // class TScript
   330 //
   331 
   332 TScript::TScript()
   333 	: iLineNo(0)
   334 	{
   335 	RFile file;
   336 	TInt r=file.Open(TheFs,KScriptFile,EFileRead);
   337 #ifndef __TOOLS2__
   338 	if (r!=KErrNone)
   339 		r=file.Open(TheFs,KRomScriptFile,EFileRead);
   340 #endif
   341 	test(r==KErrNone);
   342 	iInput.Set(file);
   343 
   344 #ifdef __TOOLS2__
   345 	RFile file2;
   346 	test(file2.Replace(TheFs,KOutputFile,EFileWrite)==KErrNone);
   347 	iFile = file2;
   348 	iOutput.Set(file2);
   349 #else
   350 	test(file.Replace(TheFs,KOutputFile,EFileWrite)==KErrNone);
   351 	iOutput.Set(file);
   352 #endif
   353 	}
   354 
   355 inline TInt TScript::LineNo() const
   356 	{return iLineNo;}
   357 
   358 TBool TScript::IsStatement()
   359 //
   360 //	checks for keywords which possibly conform to the usual statement format (ie end in ';')
   361 //
   362 	{
   363 	if (iLine.Remainder().Length()==0)	// null statement
   364 		return ETrue;
   365 	TPtrC line=iLine.Remainder();
   366 	TKeyword keyword=Keyword(line);
   367 	switch (keyword)
   368 		{
   369 	case EPrint: 
   370 	case ERows:
   371 	case EBuild:
   372 	case EError:
   373 	case EQuery:
   374 	case ENormal:
   375 	case EFolded:
   376 	case ECollated:
   377 	case EStart:
   378 	case EStop:
   379 	case ELoad:
   380 	case EEcho:
   381 	case EWindow:
   382 	case EAccess:
   383 	case EUnknown:						// could be sql
   384 		return ETrue;
   385 	case EComment:
   386 	case EPopulate:
   387 	case ECompare:
   388 	case EResults:
   389 		iStatement=line;				// not a statement, so make it the whole line
   390 		return EFalse;
   391 	default:
   392 		test(0);
   393 		return EFalse;
   394 		}
   395 	}
   396 
   397 TInt TScript::ReadNextStatement()
   398 //
   399 // reads the next non-blank statement or line
   400 //
   401 	{
   402 	TInt r=ReadNextLine();
   403 	if (r!=KErrNone || !IsStatement())
   404 		return r;
   405 	TChar c=0;
   406 	while (c!=';')
   407 		{
   408 		c=iLine.Get();
   409 		if (!c)							// nothing left to read
   410 			{
   411 			r=AppendNextLine();
   412 			if (r!=KErrNone)
   413 				return r;
   414 			}
   415 		}
   416 	iLine.UnGet();						// the semi-colon
   417 	iStatement=iLine.MarkedToken();
   418 	iLine.Get();						// the semi-colon
   419 	iLine.SkipSpaceAndMark();
   420 	return KErrNone;
   421 	}
   422 
   423 TPtrC TScript::Statement()
   424 	{
   425 	return iStatement.Remainder();
   426 	}
   427 
   428 void TScript::ConsumeLine()
   429 	{
   430 	iLine=TPtrC();
   431 	iStatement=TPtrC();
   432 	}
   433 
   434 void TScript::ConsumeStatement()
   435 	{
   436 	iLine=iLine.Remainder();
   437 	iStatement=TPtrC();
   438 	}
   439 
   440 TInt TScript::ReadNextLine()
   441 //
   442 // reads the next non-blank line into iLine
   443 //
   444 	{
   445 	while (iLine.Remainder().Length()==0)
   446 		{
   447 		TInt r=iInput.Read(iBuf);
   448 		if (r!=KErrNone)
   449 			return r;
   450 		if (iBuf.Length()>0 && iBuf[0]==0xfeff)	// unicode stream marker
   451 			iBuf.Delete(0,1);
   452 		iLineNo++;
   453 		iLine=iBuf;
   454 		iLine.SkipSpaceAndMark();
   455 		} 
   456 	return KErrNone;
   457 	}
   458 
   459 TInt TScript::AppendNextLine()
   460 //
   461 // adds next line from file to iLine
   462 //
   463 	{
   464 	TScriptLine line;
   465 	do {
   466 		TInt r=iInput.Read(line);
   467 		if (r!=KErrNone)
   468 			return r;
   469 		iLineNo++;
   470 		} while (line.Length()==0);
   471 	iBuf=iLine.MarkedToken();
   472 	iBuf.Append(line);
   473 	iLine=iBuf;
   474 	return KErrNone;
   475 	}
   476 
   477 void TScript::WriteError(const TDesC& aLine)
   478 	{
   479 	TScriptLine line;
   480 	line.Format(_L("Error at line %d: %S\n"),iLineNo,&aLine);
   481 	WriteLine(line);
   482 	test.Printf(line);
   483 	}
   484 
   485 void TScript::WriteSqlError(const TResults& aResults,const TDesC& aLine)
   486 	{
   487 	TScriptLine line;
   488 	line.Format(_L("Error at line %d: %S :-\n"),aResults.iLineNo,&aLine);
   489 	WriteLine(line);
   490 	test.Printf(line);
   491 	line.Format(_L("\t%S\n"),&aResults.iLine);
   492 	WriteLine(line);
   493 	test.Printf(line);
   494 	}
   495 
   496 void TScript::WriteLine(const TDesC& aLine)
   497 	{
   498 	TScriptLine l=aLine;
   499 	l.Append('\r');
   500 	iOutput.Write(l);
   501 	}
   502 
   503 void TScript::WriteComment(const TDesC& aLine)
   504 	{
   505 	TScriptLine line;	
   506 	line.Format(_L("\n%S"),&aLine);
   507 	WriteLine(line);
   508 	}
   509 
   510 TInt TScript::IntValue()
   511 //
   512 // returns the integer n from the ' = n ' which must follow in the statement
   513 //
   514 	{
   515 	TScriptToken keyword;
   516 	GetNextTokenFromStatement(keyword);
   517 	if (keyword.Compare(_L("="))!=0)
   518 		WriteError(_L("expected '=' missing"));
   519 	iStatement.SkipSpaceAndMark();
   520 	TInt num=0;
   521 	TInt err=iStatement.Val(num);
   522 	if (err!=KErrNone)
   523 		WriteError(_L("expected number missing"));
   524 	return num;
   525 	}
   526 
   527 TKeyword TScript::Keyword(const TDesC& aKeyword) const
   528 	{
   529 	for (TInt ii=0; ii<ENumKeywords; ++ii)
   530 		{
   531 		if (aKeyword.FindF(KScriptKeywords[ii])==0)
   532 			return TKeyword(ii);
   533 		}
   534 	return EUnknown;
   535 	}
   536 
   537 void TScript::GetNextTokenFromStatement(TDes& aToken)
   538 //
   539 // gets the next token from iStatement
   540 //
   541 	{
   542 	iStatement.SkipSpaceAndMark();
   543 	TUint c;
   544 	do
   545 		{
   546 		c=iStatement.Get();
   547 		if (c=='=' || c=='!')
   548 			break;
   549 		} while (iStatement.Peek().IsAlphaDigit());
   550 	aToken=iStatement.MarkedToken();
   551 	iStatement.SkipSpaceAndMark();
   552 	}
   553 
   554 void TScript::GetNextTokenFromLine(TDes& aToken)
   555 //
   556 // gets the next token from iLine
   557 //
   558 	{
   559 	iLine.SkipSpaceAndMark();
   560 	TUint c=0;
   561 	TChar cc=c;
   562 	TBool literal=EFalse;
   563 	do
   564 		{
   565 		c=iLine.Get();
   566 		if (!c)
   567 			{
   568 			AppendNextLine();
   569 			iLine.SkipSpaceAndMark();
   570 			c=iLine.Get();
   571 			}
   572 		if (c=='\'' || c=='#')
   573 			literal=!literal;
   574 		if ((c==',' || c=='(' || c==')' || c=='{' || c=='}' || c=='!' ) && !literal)
   575 			break;
   576 		cc=iLine.Peek();
   577 		} while (cc.IsAlphaDigit() || literal || TUint(cc)=='.' || TUint(cc)=='+' || TUint(cc)=='-');
   578 	aToken=iLine.MarkedToken();
   579 	if (TUint(cc)==';')					// ignore semi-colons - they're optional
   580 		iLine.Get();
   581 	iLine.SkipSpaceAndMark();
   582 	if (c=='!')
   583 		{
   584 		iStatement=iLine.Remainder();
   585 		TPtrC comment=iStatement.Remainder();
   586 		WriteComment(comment); 
   587 		iLine=TPtrC();
   588 		GetNextTokenFromLine(aToken);
   589 		}
   590 	}
   591 
   592 
   593 //
   594 // class TScriptEngine
   595 //
   596 
   597 TScriptEngine::TScriptEngine()
   598 	: iTextComparison(EDbCompareNormal),iTimer(iScript),iEcho(EFalse),iWindow(EFalse),iAccess(RDbRowSet::EReadOnly)
   599 	{} 
   600 
   601 
   602 void TScriptEngine::RunL() 
   603 //
   604 // runs the script file
   605 //
   606 	{
   607 	while (iScript.ReadNextStatement()!=KErrEof)
   608 		ExecuteL();
   609 	TestForNoError(); 
   610 	}
   611 
   612 void TScriptEngine::TestForNoError() 
   613 	{
   614 	if (iResults.iError!=KErrNone)
   615 		{
   616 		TScriptLine line;
   617 		line.Format(_L("unexpected error %d"),iResults.iError);
   618 		FatalSqlError(line);
   619 		}
   620 	}
   621 
   622 void TScriptEngine::ExecuteL()
   623 	{
   624 	if (ExecuteScriptL()!=KErrNone && ExecuteSql()!=KErrNone)
   625 		FatalSqlError(_L("syntax error"));
   626 	}
   627 
   628 TInt TScriptEngine::ExecuteScriptL()
   629 	{
   630 	TKeyword keyword=iScript.Keyword(iScript.Statement());
   631 	if (keyword!=EError)
   632 		TestForNoError(); 
   633 	switch (keyword)
   634 		{
   635 	case EPrint:	
   636 		DoPrintL();
   637 		break;
   638 	case ERows:
   639 		DoRows();
   640 		break;
   641 	case ELoad:
   642 		DoLoadDb();
   643 		break;
   644 	case EEcho:
   645 		DoEcho();
   646 		break;
   647 	case EWindow:
   648 		DoWindow();
   649 		break;
   650 	case EAccess:
   651 		DoAccess();
   652 		break;
   653 	case ECompare:
   654 		DoCompareL();
   655 		break;
   656 	case EError:
   657 		DoError();
   658 		break;
   659 	case EPopulate:
   660 		DoPopulate();
   661 		break;
   662 	case EComment:
   663 		DoComment();
   664 		break;
   665 	case EResults:
   666 		DoResultsL();
   667 		break;
   668 	case EBuild:
   669 		DoBuildTable();
   670 		break;
   671 	case EQuery:
   672 		DoQuery();
   673 		break;
   674 	case ENormal:
   675 	case EFolded:
   676 	case ECollated:
   677 		DoTextComparison(keyword);
   678 		break;
   679 	case EStart:
   680 		DoStartTimer();
   681 		break;
   682 	case EStop:
   683 		DoStopTimer();
   684 		break;
   685 	case EUnknown:
   686 	default:
   687 		return KErrNotFound;
   688 		}
   689 	return KErrNone;
   690 	}
   691 
   692 TInt TScriptEngine::ExecuteSql()
   693 	{
   694 	return ExecuteSql(iScript.Statement());
   695 	}
   696 
   697 TInt TScriptEngine::ExecuteSql(const TDesC& aSql)
   698 	{
   699 	if (iEcho)
   700 		{
   701 		TScriptLine line(_L("\nSQL:\t"));
   702 		line.Append(aSql);
   703 		iScript.WriteLine(line);
   704 		}
   705 	iResults.iLineNo=iScript.LineNo();
   706 	iResults.iLine=aSql;
   707 	TSqlStatement statement(aSql,iResults);
   708 	return statement.Execute(iTextComparison,iAccess,iWindow);
   709 	}
   710 
   711 void TScriptEngine::DoStartTimer()
   712 	{
   713 	// test its right function
   714 	TScriptToken keyword;
   715 	iScript.GetNextTokenFromStatement(keyword);
   716 	test(keyword.CompareF(_L("START"))==0);
   717 	iScript.GetNextTokenFromStatement(keyword);
   718 	test(keyword.CompareF(_L("TIMER"))==0);
   719 	//
   720 	iTimer.Start(_L("Timer started"));
   721 	}
   722 
   723 void TScriptEngine::DoStopTimer()
   724 	{
   725 	// test its right function
   726 	TScriptToken keyword;
   727 	iScript.GetNextTokenFromStatement(keyword);
   728 	test(keyword.CompareF(_L("STOP"))==0);
   729 	iScript.GetNextTokenFromStatement(keyword);
   730 	test(keyword.CompareF(_L("TIMER"))==0);
   731 	//
   732 	iTimer.Stop(_L("Timer stopped"));
   733 	}
   734 
   735 void TScriptEngine::DoTextComparison(TKeyword aKeyword)
   736 	{
   737 	TPtrC line;
   738 	switch (aKeyword)
   739 		{
   740 	case ENormal:
   741 		iTextComparison=EDbCompareNormal;
   742 		line.Set(_L("[Normal text comparison]"));
   743 		break;
   744 	case EFolded:
   745 		iTextComparison=EDbCompareFolded;
   746 		line.Set(_L("[Folded text comparison]"));
   747 		break;
   748 	case ECollated:
   749 		iTextComparison=EDbCompareCollated;
   750 		line.Set(_L("[Collated text comparison]"));
   751 		break;
   752 	default:
   753 		test(0);
   754 		}
   755 	iScript.WriteLine(line);
   756 	}
   757 
   758 void TScriptEngine::DoComment()
   759 	{
   760 	// test its right function
   761 	TScriptToken keyword;
   762 	iScript.GetNextTokenFromStatement(keyword);
   763 	test(keyword.CompareF(_L("!"))==0);
   764 	//
   765 	TPtrC comment=iScript.Statement();
   766 	iScript.WriteComment(comment);
   767 	iScript.ConsumeLine();
   768 	}
   769 
   770 void TScriptEngine::DoError()
   771 	{
   772 	// test its right function
   773 	TScriptToken keyword;
   774 	iScript.GetNextTokenFromStatement(keyword);
   775 	test(keyword.CompareF(_L("ERROR"))==0);
   776 	//
   777 	TScriptLine line;
   778 	if (iScript.Statement().Length()==0)
   779 		{
   780 		if (iResults.iError>=0)
   781 			FatalSqlError(_L("no error when one was expected"));
   782 		line=_L("\t\tERROR OK");
   783 		}
   784 	else
   785 		{
   786 		TInt err=iScript.IntValue();
   787 		if (iResults.iError!=err)
   788 			{
   789 			line.Format(_L("expected error %d, actual error %d"),err,iResults.iError);
   790 			FatalSqlError(line);
   791 			}
   792 		line.Format(_L("\t\tERROR=%D OK"),err);
   793 		}
   794 	iResults.iError=0;
   795 	iScript.WriteLine(line);
   796 	}
   797 
   798 void TScriptEngine::DoRows()
   799 	{
   800 	// test its right function
   801 	TScriptToken keyword;
   802 	iScript.GetNextTokenFromStatement(keyword);
   803 	test(keyword.CompareF(_L("ROWS"))==0);
   804 	//
   805 	TScriptLine line;
   806 	TInt rows=iScript.IntValue();
   807 	if (iResults.iRows!=rows)
   808 		{
   809 		line.Format(_L("expected rows %d, actual rows %d"),rows,iResults.iRows);
   810 		FatalSqlError(line);
   811 		}
   812 	line.Format(_L("\t\tROWS=%D OK"),rows);
   813 	iScript.WriteLine(line);
   814 	}
   815 
   816 void TScriptEngine::DoLoadDb()
   817 	{
   818 	// test its right function
   819 	TScriptToken token;
   820 	iScript.GetNextTokenFromStatement(token);
   821 	test(token.CompareF(_L("LOAD"))==0);
   822 	//
   823 	TFileName database(iScript.Statement());
   824 	TheDatabase.Close();
   825 
   826 	TInt err = TheCrcChecker.GenerateCrcL(KTestDatabase);
   827 	test(err==KErrNone);
   828 
   829 	TScriptLine line;
   830 	line.Format(_L("Opening database: %S"),&database);
   831 	iScript.WriteLine(line);
   832 	test(TheDatabase.Open(TheFs,database)==KErrNone);
   833 	}
   834 
   835 void TScriptEngine::DoEcho()
   836 	{
   837 	// test its right function
   838 	TScriptToken keyword;
   839 	iScript.GetNextTokenFromStatement(keyword);
   840 	test(keyword.CompareF(_L("ECHO"))==0);
   841 	//
   842 	iScript.GetNextTokenFromStatement(keyword);
   843 	if (keyword.CompareF(_L("OFF"))==0)
   844 		{
   845 		iEcho=EFalse;
   846 		iScript.WriteLine(_L("Echo is off"));
   847 		}
   848 	else if (keyword.CompareF(_L("ON"))==0)
   849 		{
   850 		iEcho=ETrue;
   851 		iScript.WriteLine(_L("Echo is on"));
   852 		}
   853 	else
   854 		FatalError(_L("Expected ON|OFF to follow ECHO statement"));
   855 	}
   856 
   857 void TScriptEngine::DoWindow()
   858 	{
   859 	// test its right function
   860 	TScriptToken keyword;
   861 	iScript.GetNextTokenFromStatement(keyword);
   862 	test(keyword.CompareF(_L("WINDOW"))==0);
   863 	//
   864 	iScript.GetNextTokenFromStatement(keyword);
   865 	if (keyword.CompareF(_L("OFF"))==0)
   866 		{
   867 		iWindow=EFalse;
   868 		iScript.WriteLine(_L("Window is off"));
   869 		}
   870 	else if (keyword.CompareF(_L("ON"))==0)
   871 		{
   872 		iWindow=ETrue;
   873 		iScript.WriteLine(_L("Window is on"));
   874 		}
   875 	else
   876 		FatalError(_L("Expected ON|OFF to follow WINDOW statement"));
   877 	}
   878 
   879 void TScriptEngine::DoAccess()
   880 	{
   881 	// test its right function
   882 	TScriptToken keyword;
   883 	iScript.GetNextTokenFromStatement(keyword);
   884 	test(keyword.CompareF(_L("ACCESS"))==0);
   885 	//
   886 	iScript.GetNextTokenFromStatement(keyword);
   887 	if (keyword.CompareF(_L("UPDATE"))==0)
   888 		{
   889 		iAccess=RDbRowSet::EUpdatable;
   890 		iScript.WriteLine(_L("Access is updateable"));
   891 		}
   892 	else if (keyword.CompareF(_L("READ"))==0)
   893 		{
   894 		iAccess=RDbRowSet::EReadOnly;
   895 		iScript.WriteLine(_L("Access is read only"));
   896 		}
   897 	else if (keyword.CompareF(_L("INSERT"))==0)
   898 		{
   899 		iAccess=RDbRowSet::EInsertOnly;
   900 		iScript.WriteLine(_L("Access is insert only"));
   901 		}
   902 	else
   903 		FatalError(_L("Expected UPDATE|INSERT|READ to follow ACCESS statement"));
   904 	}
   905 
   906 void TScriptEngine::DoResultsL()
   907 	{
   908 	// test its right function
   909 	TScriptToken token;
   910 	iScript.GetNextTokenFromLine(token);
   911 	test(token.CompareF(_L("RESULTS"))==0);
   912 	//
   913 	iScript.GetNextTokenFromLine(token);
   914 	if (token.Compare(_L("{"))!=0)		
   915 		FatalError(_L("missing '{'"));
   916 	iScript.GetNextTokenFromLine(token);					// first value
   917 	TLex value;
   918 	RDbRowSet& rowset=iResults.iView;
   919 	CDbColSet* colset=rowset.ColSetL();
   920 	CleanupStack::PushL(colset);
   921 	TDbColNo colno=colset->ColNo(KRowIdColName);
   922 	CArrayFixFlat<TInt>* rowIdScript=new CArrayFixFlat<TInt>(4);
   923 	CleanupStack::PushL(rowIdScript);
   924 	CArrayFixFlat<TInt>* rowIdView=new CArrayFixFlat<TInt>(4);
   925 	CleanupStack::PushL(rowIdView);
   926 	rowset.BeginningL();
   927 	while (rowset.NextL())				
   928 		{
   929 		rowset.GetL();
   930 		TUint rIdScript;
   931 		value=token;
   932 		if (value.Val(rIdScript)!=KErrNone)
   933 			{
   934 			TScriptLine line;
   935 			line.Format(_L("Unable to extract row id from \"%S\""),&token);
   936 			FatalError(line);
   937 			}
   938 		TUint rIdView=rowset.ColUint(colno);
   939 		rowIdScript->AppendL(rIdScript);
   940 		rowIdView->AppendL(rIdView);
   941 		iScript.GetNextTokenFromLine(token);			
   942 		if (token.Compare(_L(","))==0 || token.Compare(_L("}"))==0)
   943 			{
   944 			if (rowIdScript->Count())
   945 				{
   946 				TKeyArrayFix key(0,ECmpTInt);
   947 				rowIdScript->Sort(key);
   948 				rowIdView->Sort(key);
   949 				for (TInt ii=0;ii<rowIdScript->Count();++ii)
   950 					{
   951 					TInt expectedId=(*rowIdScript)[ii];
   952 					TInt actualId=(*rowIdView)[ii];
   953 					if (actualId!=expectedId)
   954 						{
   955 						TScriptLine line;
   956 						line.Format(_L("expected row id %d, actual row id %d"),actualId,expectedId);
   957 						FatalError(line);
   958 						}
   959 					}
   960 				rowIdScript->Reset();
   961 				rowIdView->Reset();
   962 				}
   963 			if (token.Compare(_L(","))==0)
   964 				iScript.GetNextTokenFromLine(token);
   965 			}
   966 		}
   967 	if (token.Compare(_L("}"))!=0)
   968 		FatalError(_L("too many results expected"));
   969 	CleanupStack::PopAndDestroy(3);	// arrays + colset
   970 	iScript.ConsumeStatement();
   971 	}
   972 
   973 void TScriptEngine::DoBuildTable()
   974 //
   975 // same as Sql create statement, but adds a counter
   976 //
   977 	{
   978 	// test its right function
   979 	TScriptToken keyword;
   980 	iScript.GetNextTokenFromStatement(keyword);
   981 	test(keyword.CompareF(_L("BUILD"))==0);
   982 	//
   983 	TScriptLine sql(_L("CREATE "));
   984 	sql.Append(iScript.Statement());
   985 	TInt pos=sql.Find(_L("("));
   986 	sql.Insert(++pos,_L("Rw COUNTER,"));
   987 	iScript.ConsumeStatement();
   988 	ExecuteSql(sql);
   989 	}
   990 
   991 void TScriptEngine::DoQuery()
   992 //
   993 // same as Sql select statement, but makes sure counter is included
   994 //
   995 	{
   996 	// test its right function
   997 	TScriptToken keyword;
   998 	iScript.GetNextTokenFromStatement(keyword);
   999 	test(keyword.CompareF(_L("QUERY"))==0);
  1000 	//
  1001 	TScriptLine sql(iScript.Statement());
  1002 	if (sql.Find(_L("*"))!=0)
  1003 		{
  1004 		sql.Insert(0,_L(","));
  1005 		sql.Insert(0,KRowIdColName);
  1006 		}
  1007 	sql.Insert(0,_L("SELECT "));
  1008 	iScript.ConsumeStatement();
  1009 	ExecuteSql(sql);
  1010 	}
  1011 
  1012 void TScriptEngine::FatalError(const TDesC& aLine)
  1013 	{
  1014 	iScript.WriteError(aLine);
  1015 	test(0);
  1016 	}
  1017 
  1018 void TScriptEngine::FatalError()
  1019 	{
  1020 	FatalError(_L("wrong expected value"));
  1021 	}
  1022 
  1023 void TScriptEngine::FatalSqlError(const TDesC& aLine)
  1024 	{
  1025 	iScript.WriteSqlError(iResults,aLine);
  1026 	test(0);
  1027 	}
  1028 
  1029 void TScriptEngine::DoPopulate()
  1030 	{
  1031 	// check its right function
  1032 	TScriptToken token;
  1033 	iScript.GetNextTokenFromLine(token);
  1034 	test(token.CompareF(_L("POPULATE"))==0);
  1035 	//
  1036 	TScriptLine sqlbase=_L("INSERT INTO ");
  1037 	iScript.GetNextTokenFromLine(token);	// table name
  1038 	sqlbase.AppendFormat(_L("%S "),&token);	
  1039 	iScript.GetNextTokenFromLine(token);
  1040 	if (token.Compare(_L("("))==0)			// optional column names present?
  1041 		{			
  1042 		for (;;) 							
  1043 			{
  1044 			sqlbase.AppendFormat(token);
  1045 			if (token.Compare(_L(")"))==0)
  1046 				break;
  1047 			iScript.GetNextTokenFromLine(token);		
  1048 			} 
  1049 		iScript.GetNextTokenFromLine(token);
  1050 		}
  1051 	if (token.Compare(_L("{"))!=0)		
  1052 		FatalError(_L("missing '{'"));
  1053 	sqlbase.AppendFormat(_L(" VALUES ("));
  1054 	iScript.GetNextTokenFromLine(token);	// first value
  1055 	TheDatabase.Begin();					// all in same transaction
  1056 	for (;;)
  1057 		{
  1058 		if (token.Compare(_L("}"))==0)	
  1059 			break;
  1060 		TScriptLine sql=sqlbase;
  1061 		for (;;)
  1062 			{
  1063 			sql.Append(token);
  1064 			iScript.GetNextTokenFromLine(token);
  1065 			if (token.Compare(_L(","))==0)
  1066 				{
  1067 				sql.Append(token);			// comma
  1068 				iScript.GetNextTokenFromLine(token);
  1069 				}
  1070 			else
  1071 				break;
  1072 			}
  1073 		sql.AppendFormat(_L(")"));
  1074 		ExecuteSql(sql);
  1075 		TestForNoError();
  1076 		}
  1077 	TheDatabase.Commit();
  1078 	iScript.ConsumeStatement();
  1079 	}
  1080 
  1081 void TScriptEngine::DoPrintL() 
  1082 	{
  1083 	// check its right function
  1084 	TScriptToken keyword;
  1085 	iScript.GetNextTokenFromStatement(keyword);
  1086 	test(keyword.CompareF(_L("PRINT"))==0);
  1087 	//
  1088 	iScript.GetNextTokenFromStatement(keyword);
  1089 	if (keyword.CompareF(_L("VIEW"))==0)
  1090 		PrintL(iResults.iView);
  1091 	else if (keyword.CompareF(_L("TABLE"))==0)
  1092 		{
  1093 		iScript.GetNextTokenFromStatement(keyword);	// name of table
  1094 		RDbTable table;
  1095 		TInt err=table.Open(TheDatabase,keyword,table.EReadOnly);
  1096 		if (err!=KErrNone)
  1097 			FatalError(_L("unable to open table"));
  1098 		PrintL(table);
  1099 		table.Close();
  1100 		}
  1101 	else
  1102 		FatalError(_L("expected VIEW or TABLE keyword not present"));
  1103 	}
  1104 
  1105 void TScriptEngine::DoCompareL() 
  1106 	{
  1107 	// check its right function
  1108 	TScriptToken keyword;
  1109 	iScript.GetNextTokenFromLine(keyword);
  1110 	test(keyword.CompareF(_L("COMPARE"))==0);
  1111 	//
  1112 	iScript.GetNextTokenFromLine(keyword);
  1113 	if (keyword.CompareF(_L("VIEW"))==0)
  1114 		CompareL(iResults.iView);
  1115 	else if (keyword.CompareF(_L("TABLE"))==0)
  1116 		{
  1117 		iScript.GetNextTokenFromLine(keyword);	// name of table
  1118 		RDbTable table;
  1119 		TInt err=table.Open(TheDatabase,keyword,table.EReadOnly);
  1120 		if (err!=KErrNone)
  1121 			FatalError(_L("unable to open table"));
  1122 		CompareL(table);
  1123 		table.Close();
  1124 		}
  1125 	else
  1126 		FatalError(_L("expected VIEW or TABLE keyword not present"));
  1127 	}
  1128 
  1129 void TScriptEngine::CompareL(RDbRowSet& aRowSet) 
  1130 	{
  1131 	TScriptToken token;
  1132 	iScript.GetNextTokenFromLine(token);
  1133 	TBool rowIdMode=EFalse;
  1134 	CArrayFixFlat<TInt>* rowIdToTest=new CArrayFixFlat<TInt>(4);
  1135 	CleanupStack::PushL(rowIdToTest);
  1136 	if (token.Compare(_L("("))==0)				// optional row ids present?
  1137 		{
  1138 		rowIdMode=ETrue;
  1139 		iScript.GetNextTokenFromLine(token);	// first value
  1140 		TLex value;
  1141 		TInt rowId;
  1142 		for (;;) 							
  1143 			{
  1144 			value=token;
  1145 			test (value.Val(rowId)==KErrNone);
  1146 			rowIdToTest->AppendL(rowId);		// add row id to array
  1147 			iScript.GetNextTokenFromLine(token);
  1148 			if (token.Compare(_L(")"))==0)
  1149 				break;
  1150 			if (token.Compare(_L(","))==0)
  1151 				iScript.GetNextTokenFromLine(token);					
  1152 			} 
  1153 		iScript.GetNextTokenFromLine(token);
  1154 		}
  1155 	if (token.Compare(_L("{"))!=0)		
  1156 		FatalError(_L("missing '{'"));
  1157 	TInt columns=aRowSet.ColCount();
  1158 	CDbColSet* colset=aRowSet.ColSetL();
  1159 	aRowSet.BeginningL();
  1160 	while (aRowSet.NextL())
  1161 		{
  1162 		aRowSet.GetL();
  1163 		if (rowIdMode)
  1164 			{
  1165 			TInt currentId=aRowSet.ColUint(colset->ColNo(KRowIdColName));
  1166 			TBool toTest=EFalse;
  1167 			for (TInt jj=0; jj<rowIdToTest->Count(); ++jj)
  1168 				{
  1169 				if (currentId==(*rowIdToTest)[jj])
  1170 					toTest=ETrue;
  1171 				}
  1172 			if (!toTest)
  1173 				continue;
  1174 			}
  1175 		for (TInt ii=1;ii<=columns;++ii)
  1176 			{
  1177 			if (rowIdMode && ii==colset->ColNo(KRowIdColName))	// ignore row id column
  1178 				continue;
  1179 			const TDbCol& col=(*colset)[ii];
  1180 			iScript.GetNextTokenFromLine(token);				// value
  1181 			if (token.Compare(_L(","))==0)
  1182 				iScript.GetNextTokenFromLine(token);			// ignore comma
  1183 			if (aRowSet.IsColNull(ii))
  1184 				{
  1185 				if (token.CompareF(_L("NULL"))!=0)
  1186 					FatalError(_L("NULL expected"));
  1187 				continue;
  1188 				}
  1189 			CompareValues(aRowSet,ii,col.iType,token); 
  1190 			}
  1191 		}
  1192 	delete colset;
  1193 	CleanupStack::PopAndDestroy();				// rowIdToTest
  1194 	iScript.GetNextTokenFromLine(token);		// look for closing '}'
  1195 	if (token.Compare(_L("}"))!=0)		
  1196 		FatalError(_L("missing '}'"));
  1197 	iScript.ConsumeStatement();
  1198 	}
  1199 
  1200 void TScriptEngine::CompareValues(RDbRowSet& aRowSet,TDbColNo ColNo,TDbColType aType,const TDesC& aToken) 
  1201 //
  1202 // compares the value from a rowset aRowset, colimn number aColNo and of type aType, with the value
  1203 // contained in the descriptor aToken
  1204 //
  1205 	{
  1206 	TLex value=aToken;
  1207 	switch (aType)
  1208 		{
  1209 	case EDbColInt32:
  1210 		{
  1211 		TInt num;
  1212 		test (value.Val(num)==KErrNone);
  1213 		if (num!=aRowSet.ColInt(ColNo))
  1214 			FatalError();
  1215 		break;
  1216 		}
  1217 	case EDbColInt8:
  1218 		{
  1219 		TInt8 num8;
  1220 		test (value.Val(num8)==KErrNone);
  1221 		if (num8!=aRowSet.ColInt8(ColNo))
  1222 			FatalError();
  1223 		break;
  1224 		}
  1225 	case EDbColInt16:
  1226 		{
  1227 		TInt16 num16;
  1228 		test (value.Val(num16)==KErrNone);
  1229 		if (num16!=aRowSet.ColInt16(ColNo))
  1230 			FatalError();
  1231 		break;
  1232 		}
  1233 	case EDbColInt64:
  1234 		{
  1235 		TInt64 num64;
  1236 		test (value.Val(num64)==KErrNone);
  1237 		if (num64!=aRowSet.ColInt64(ColNo))
  1238 			FatalError();
  1239 		break;
  1240 		}
  1241 	case EDbColUint8:
  1242 		{
  1243 		TUint8 numu8;
  1244 		test (value.Val(numu8,EDecimal)==KErrNone);
  1245 		if (numu8!=aRowSet.ColUint8(ColNo))
  1246 			FatalError();
  1247 		break;
  1248 		}
  1249 	case EDbColUint16:
  1250 		{
  1251 		TUint16 numu16;
  1252 		test (value.Val(numu16,EDecimal)==KErrNone);
  1253 		if (numu16!=aRowSet.ColUint16(ColNo))
  1254 			FatalError();
  1255 		break;
  1256 		}
  1257 	case EDbColUint32:
  1258 		{
  1259 		TUint32 numu32;
  1260 		test (value.Val(numu32,EDecimal)==KErrNone);
  1261 		if (numu32!=aRowSet.ColUint32(ColNo))
  1262 			FatalError();
  1263 		break;
  1264 		}
  1265 	case EDbColReal32:
  1266 		{
  1267 		TReal32 numr32;
  1268 		test (value.Val(numr32)==KErrNone);
  1269 		if (numr32!=aRowSet.ColReal32(ColNo))
  1270 			FatalError();
  1271 		break;
  1272 		}
  1273 	case EDbColReal64:
  1274 		{
  1275 		TReal64 numr64;
  1276 		test (value.Val(numr64)==KErrNone);
  1277 		if (numr64!=aRowSet.ColReal64(ColNo))
  1278 			FatalError();
  1279 		break;
  1280 		}
  1281 	case EDbColText8:
  1282 	case EDbColText16:
  1283 		{
  1284 		TPtrC text=aToken.Mid(1,aToken.Length()-2);			// skip quotes
  1285 		if (text.CompareF(aRowSet.ColDes(ColNo))!=0)
  1286 			FatalError();
  1287 		break;
  1288 		}
  1289 	case EDbColDateTime:
  1290 		{
  1291 		TScriptLine time=aToken.Mid(1,aToken.Length()-2);	// skip hashes
  1292 		TTime t1;
  1293 		t1.Parse(time);
  1294 		TTime t2(aRowSet.ColTime(ColNo).DateTime());
  1295 		if (t1!=t2)
  1296 			FatalError();
  1297 		break;
  1298 		}
  1299 	default:
  1300 		break;
  1301 		}
  1302 	}
  1303 
  1304 void TScriptEngine::PrintL(RDbRowSet& aRowSet) 
  1305 	{
  1306 	iScript.WriteLine(TPtrC());
  1307 	TInt columns=aRowSet.ColCount();	
  1308 	CDbColSet* colset=aRowSet.ColSetL();
  1309 	TScriptLine line;
  1310 	for (TInt i=1;i<=columns;++i)
  1311 		{
  1312 		const TDbCol& col=(*colset)[i];
  1313 		line.AppendFormat(_L("%S\t"),&col.iName);
  1314 		}
  1315 	iScript.WriteLine(line);
  1316 	aRowSet.BeginningL();
  1317 	while (aRowSet.NextL())
  1318 		{
  1319 		line=TPtrC();
  1320 		for (TInt ii=1;ii<=columns;++ii)
  1321 			{
  1322 			const TDbCol& col=(*colset)[ii];
  1323 			aRowSet.GetL();
  1324 			if (aRowSet.IsColNull(ii))
  1325 				{
  1326 				line.AppendFormat(_L("NULL\t"));
  1327 				continue;
  1328 				}
  1329 			switch (col.iType)
  1330 				{
  1331 			case EDbColInt32:
  1332 				line.AppendFormat(_L("%d\t"),aRowSet.ColInt(ii));
  1333 				break;
  1334 			case EDbColInt8:
  1335 				line.AppendFormat(_L("%d\t"),aRowSet.ColInt8(ii));
  1336 				break;
  1337 			case EDbColInt16:
  1338 				line.AppendFormat(_L("%d\t"),aRowSet.ColInt16(ii));
  1339 				break;
  1340 			case EDbColInt64:
  1341 				line.AppendFormat(_L("%ld\t"),aRowSet.ColInt64(ii));
  1342 				break;
  1343 			case EDbColUint8:
  1344 				line.AppendFormat(_L("%u\t"),aRowSet.ColUint8(ii));
  1345 				break;
  1346 			case EDbColUint16:
  1347 				line.AppendFormat(_L("%u\t"),aRowSet.ColUint16(ii));
  1348 				break;
  1349 			case EDbColUint32:
  1350 				line.AppendFormat(_L("%u\t"),aRowSet.ColUint(ii));
  1351 				break;
  1352 			case EDbColReal32:
  1353 				line.AppendFormat(_L("%f\t"),aRowSet.ColReal32(ii));
  1354 				break;
  1355 			case EDbColReal64:
  1356 				line.AppendFormat(_L("%f\t"),aRowSet.ColReal64(ii));
  1357 				break;
  1358 			case EDbColText:
  1359 				line.Append(aRowSet.ColDes(ii));
  1360 				line.Append('\t');
  1361 				break;
  1362 			case EDbColDateTime:
  1363 				{
  1364 				TDateTime time(aRowSet.ColTime(ii).DateTime());
  1365 				line.AppendFormat(_L("%d:%d:%d %d/%d/%d"),time.Hour(),time.Minute(),time.Second(),time.Day(),time.Month(),time.Year());
  1366 				}	
  1367 				break;
  1368 			case EDbColLongText:
  1369 				{
  1370 				RDbColReadStream blob;
  1371 				blob.OpenLC(aRowSet,ii);
  1372 				TScriptLine text;
  1373 				blob.ReadL(text,aRowSet.ColLength(ii));
  1374 				CleanupStack::PopAndDestroy();
  1375 				line.AppendFormat(_L("%S\t"),&text);
  1376 				}
  1377 			default:
  1378 				break;
  1379 				}
  1380 			}
  1381 		iScript.WriteLine(line);
  1382 		}
  1383 	iScript.WriteLine(TPtrC());
  1384 	delete colset;
  1385 	}
  1386 
  1387 
  1388 LOCAL_C void CreateDatabaseL()
  1389 //
  1390 // Create the database
  1391 //
  1392 	{
  1393 	test(TheDatabase.Replace(TheFs,KTestDatabase)==KErrNone);
  1394 	}
  1395 
  1396 LOCAL_C void CloseDatabase()
  1397 //
  1398 // Close the database
  1399 //
  1400 	{
  1401 	TheDatabase.Close();
  1402 
  1403 	TInt err = TheCrcChecker.GenerateCrcL(KTestDatabase);
  1404 	test(err==KErrNone);
  1405 	}
  1406 
  1407 LOCAL_C void SetupTestDirectory()
  1408 //
  1409 // Prepare the test directory.
  1410 //
  1411     {
  1412 	TInt err=TheFs.Connect();
  1413 	test(err==KErrNone);
  1414 //
  1415 	err=TheFs.MkDir(KTestDatabase);
  1416 	test(err==KErrNone || err==KErrAlreadyExists);
  1417 	}
  1418 
  1419 LOCAL_C void SetupCleanup()
  1420 //
  1421 // Initialise the cleanup stack.
  1422 //
  1423     {
  1424 	TheTrapCleanup=CTrapCleanup::New();
  1425 	test(TheTrapCleanup!=NULL);
  1426 	TRAPD(err,\
  1427 		{\
  1428 		for (TInt i=KTestCleanupStack;i>0;i--)\
  1429 			CleanupStack::PushL((TAny*)0);\
  1430 		CleanupStack::Pop(KTestCleanupStack);\
  1431 		});
  1432 	test(err==KErrNone);
  1433 	}
  1434 
  1435 /**
  1436 @SYMTestCaseID          SYSLIB-DBMS-CT-0632
  1437 @SYMTestCaseDesc        Executes the script files
  1438 @SYMTestPriority        Medium
  1439 @SYMTestActions         Start the script engine 
  1440 @SYMTestExpectedResults Test must not fail
  1441 @SYMREQ                 REQ0000
  1442 */
  1443 LOCAL_C void RunScriptL()
  1444 	{
  1445 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0632 Running script "));
  1446 	CreateDatabaseL();
  1447 	TScriptEngine script;
  1448 	script.RunL();
  1449 	CloseDatabase();
  1450 	TheView.Close();
  1451 	}
  1452 
  1453 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
  1454 	{
  1455 	RFs fsSession;
  1456 	TInt err = fsSession.Connect();
  1457 	if(err == KErrNone)
  1458 		{
  1459 		TEntry entry;
  1460 		if(fsSession.Entry(aFullName, entry) == KErrNone)
  1461 			{
  1462 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
  1463 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
  1464 			if(err != KErrNone) 
  1465 				{
  1466 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
  1467 				}
  1468 			err = fsSession.Delete(aFullName);
  1469 			if(err != KErrNone) 
  1470 				{
  1471 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
  1472 				}
  1473 			}
  1474 		fsSession.Close();
  1475 		}
  1476 	else
  1477 		{
  1478 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
  1479 		}
  1480 	}
  1481 
  1482 GLDEF_C TInt E32Main()
  1483     {
  1484 	test.Title();
  1485 	SetupTestDirectory();
  1486 	SetupCleanup();
  1487 	__UHEAP_MARK;
  1488 //
  1489 	TRAPD(err,RunScriptL());
  1490 	test(err==KErrNone);
  1491 
  1492 	//deletion of data files must be done before call to end - DEF047652
  1493 	::DeleteDataFile(KTestDatabase);
  1494 	::DeleteDataFile(KOutputFile);//Comment this line if you want topo keep t_script.log file.	
  1495 	
  1496 
  1497 #ifndef __linux__
  1498 #ifndef __TOOLS2__ 
  1499 	TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
  1500 	test(err==KErrNone);
  1501 	test(lc==KErrNone);
  1502 #else
  1503 	TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
  1504 	TPtrC errmsg;
  1505 	TheCrcChecker.ErrorReportL(err, errmsg);
  1506 	RDebug::Print(errmsg);
  1507 	test(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
  1508 #endif
  1509 #endif
  1510 	
  1511 	test.End();
  1512 //
  1513 	__UHEAP_MARKEND;
  1514 
  1515 	delete TheTrapCleanup;
  1516 
  1517 	TheFs.Close();
  1518 	test.Close();
  1519 
  1520 	return 0;
  1521     }