symhelp/helpmodel/src/HLPSRCH.CPP
changeset 0 1f04cf54edd8
equal deleted inserted replaced
-1:000000000000 0:1f04cf54edd8
       
     1 // Copyright (c) 1999-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 "HLPSRCH.H"
       
    17 
       
    18 #include <d32dbms.h>
       
    19 
       
    20 #include "Hlpsqlconsts.h"
       
    21 #include "hlppanic.h"
       
    22 
       
    23 
       
    24 //
       
    25 // CHlpSQLSearch class
       
    26 //
       
    27 CHlpSQLSearch::CHlpSQLSearch(MHlpDbObserver& aObserver)
       
    28 :	iObserver(&aObserver)
       
    29 	{
       
    30 	}
       
    31 
       
    32 CHlpSQLSearch::~CHlpSQLSearch()
       
    33 	{
       
    34 	delete iSQLStatement;
       
    35 	delete iSQLEvaluator;
       
    36 	}
       
    37 
       
    38 void CHlpSQLSearch::ConstructL()
       
    39 	{
       
    40 	iSQLStatement = new (ELeave) CHlpSQLBuffer();
       
    41 	iSQLStatement->ConstructL(256);
       
    42 	iSQLEvaluator = CHlpSQLEvaluator::NewL(CActive::EPriorityLow);
       
    43 	}
       
    44 
       
    45 CHlpSQLSearch* CHlpSQLSearch::NewL(MHlpDbObserver& aObserver)
       
    46 	{
       
    47 	CHlpSQLSearch* self = new(ELeave) CHlpSQLSearch(aObserver);
       
    48 	CleanupStack::PushL(self);
       
    49 	self->ConstructL();
       
    50 	CleanupStack::Pop(); // self
       
    51 	return self;
       
    52 	}
       
    53 
       
    54 void CHlpSQLSearch::SetDatabase(CHlpDatabase& aDatabase)
       
    55 	{
       
    56 	iDatabase = &aDatabase;
       
    57 	iView = &aDatabase.View();
       
    58 	}
       
    59 
       
    60 void CHlpSQLSearch::CancelEvaluator()
       
    61 	{
       
    62 	Reset();
       
    63 	}
       
    64 
       
    65 void CHlpSQLSearch::Reset()
       
    66 	{
       
    67 	iSQLEvaluator->Cancel();
       
    68 	iSQLStatement->Reset();
       
    69 	iFlags &= (~EFlagsAsynchronousSearch|EFlagsPerformSearch);
       
    70 	}
       
    71 
       
    72 void CHlpSQLSearch::SearchL(TInt aType, const TDesC& aCriterion)
       
    73 	{
       
    74 	__ASSERT_ALWAYS(iDatabase, Panic(EHlpDatabaseNotInTransaction));
       
    75 
       
    76 	Reset();
       
    77 	switch(aType)
       
    78 		{
       
    79 	case EIndexList:
       
    80 		IndexListL();
       
    81 		break;
       
    82 	case ECategoryList:
       
    83 		CategoryListL();
       
    84 		break;
       
    85 	case ETopicListForCategory:
       
    86 		TopicListForCategoryL(aCriterion);	
       
    87 		break;
       
    88 	case ETopicListForCategoryUID:
       
    89 		TopicListForCategoryUIDL(aCriterion);	
       
    90 		break;
       
    91 	case EContextSearch:
       
    92 		ContextSearchL(aCriterion);
       
    93 		break;
       
    94 	case EIndexSearch:
       
    95 		IndexIdSearchL(aCriterion);
       
    96 		break;
       
    97 	case EQuickSearch:
       
    98 		TextSearchL(aCriterion, EFalse);
       
    99 		break;
       
   100 	case EFullTextSearch:
       
   101 		TextSearchL(aCriterion, ETrue);
       
   102 		break;
       
   103 	case ETopicIdSearch:
       
   104 		TopicIdSearchL(aCriterion);
       
   105 		break;
       
   106 	default:
       
   107 		Panic(EHlpInvalidQuery);
       
   108 		break;
       
   109 		}	
       
   110 
       
   111 	BuildViewL();
       
   112 	}
       
   113 
       
   114 void CHlpSQLSearch::ContextSearchL(const TDesC& aCriterion)
       
   115 	{
       
   116 	iFlags |= EFlagsPerformSearch;
       
   117 	iSQLStatement->AppendSQLL(KSQLContextSearch());
       
   118 	iSQLStatement->AppendL(aCriterion);
       
   119 	iSQLStatement->AppendSQLL(KSQLSingleInvComma());
       
   120 	}
       
   121 
       
   122 			
       
   123 void CHlpSQLSearch::IndexIdSearchL(const TDesC& aCriterion)
       
   124 	{
       
   125 	iFlags |= EFlagsPerformSearch;
       
   126 	iSQLStatement->AppendSQLL(KSQLIndexIdSearch());
       
   127 	iSQLStatement->AppendL(aCriterion);
       
   128 	}
       
   129 
       
   130 void CHlpSQLSearch::TopicIdSearchL(const TDesC& aCriterion)
       
   131 	{
       
   132 	iSQLStatement->AppendSQLL(KSQLTopicSearchProlog());
       
   133 	iSQLStatement->AppendSQLL(KSQLTopicIdColumn());
       
   134 	iSQLStatement->AppendSQLL(KSQLEqualOperator());
       
   135 	iSQLStatement->AppendL(aCriterion);
       
   136 	}
       
   137 
       
   138 void CHlpSQLSearch::TextSearchL(const TDesC& aCriterion, TBool aFullText)
       
   139 	{
       
   140 	// Text searching is asynchronous
       
   141 	iFlags |= EFlagsAsynchronousSearch;
       
   142 	iSQLStatement->AppendSQLL(KSQLTopicIdSearchProlog());
       
   143 	iSQLStatement->AppendSQLL(KSQLTxtSrchTopicTitle());
       
   144 	iSQLStatement->AppendL(aCriterion);
       
   145 	if	(aFullText)
       
   146 		{
       
   147 		iSQLStatement->AppendSQLL(KSQLTxtSrchTopicText());
       
   148 		iSQLStatement->AppendL(aCriterion);
       
   149 		}
       
   150 	iSQLStatement->AppendSQLL(KSQLTxtSrchCat());
       
   151 	iSQLStatement->AppendL(aCriterion);
       
   152 	iSQLStatement->AppendSQLL(KSQLTxtSrchSynonym());
       
   153 	iSQLStatement->AppendL(aCriterion);
       
   154 	iSQLStatement->AppendSQLL(KSQLLikeClosingInvComma());
       
   155 	}
       
   156 
       
   157 
       
   158 
       
   159 
       
   160 
       
   161 void CHlpSQLSearch::TopicIdSearchL()
       
   162 	{
       
   163 	iFlags &= (~EFlagsPerformSearch);
       
   164 	if	(iView->CountL())
       
   165 		{
       
   166 		iSQLStatement->AppendSQLL(KSQLTopicIdSearchProlog());
       
   167 		iView->FirstL();
       
   168 		CDbColSet* colset = iView->ColSetL();
       
   169 		TDbColNo topicCol = colset->ColNo(KSQLTopicIdColumn);
       
   170 		delete colset;
       
   171 		while(!iView->AtEnd())
       
   172 			{
       
   173 			iSQLStatement->AppendSQLL(KSQLTopicIdColumn());
       
   174 			iSQLStatement->AppendSQLL(KSQLEqualOperator());
       
   175 			iView->GetL();
       
   176 			iSQLStatement->AppendL(iView->ColUint32(topicCol));
       
   177 			iView->NextL();
       
   178 			if	(!iView->AtEnd())
       
   179 				iSQLStatement->AppendSQLL(KSQLOrOperator());
       
   180 			}
       
   181 		}
       
   182 	}
       
   183 
       
   184 void CHlpSQLSearch::CategoryListL()
       
   185 	{
       
   186 	iSQLStatement->AppendSQLL(KSQLCategoryList());
       
   187 	iFlags &= (~EFlagsAsynchronousSearch);
       
   188 	}
       
   189 
       
   190 void CHlpSQLSearch::IndexListL()
       
   191 	{
       
   192 	iSQLStatement->AppendSQLL(KSQLIndexList());
       
   193 	iFlags &= (~EFlagsAsynchronousSearch);
       
   194 	}
       
   195 
       
   196 void CHlpSQLSearch::TopicListForCategoryL(const TDesC& aCriterion)
       
   197 	{
       
   198 	iSQLStatement->AppendSQLL(KSQLTopicByCategoryProlog());
       
   199 	iSQLStatement->AppendSQLL(KSQLSingleInvComma());
       
   200 	iSQLStatement->AppendL(aCriterion);
       
   201 	iSQLStatement->AppendSQLL(KSQLSingleInvComma());
       
   202 	iFlags &= (~EFlagsAsynchronousSearch);
       
   203 	}
       
   204 
       
   205 void CHlpSQLSearch::TopicListForCategoryUIDL(const TDesC& aCriterion)
       
   206 	{
       
   207 	iSQLStatement->AppendSQLL(KSQLTopicByCategoryUIDProlog());
       
   208 	iSQLStatement->AppendL(aCriterion);
       
   209 	iFlags &= (~EFlagsAsynchronousSearch);
       
   210 	}
       
   211 
       
   212 
       
   213 void CHlpSQLSearch::HandleSearchEventL(TInt aEvent)
       
   214 	{
       
   215 	switch(aEvent)
       
   216 		{
       
   217 	case ESearchComplete:
       
   218 		if	(iFlags & EFlagsPerformSearch)
       
   219 			{
       
   220 			ReportEventToObserverL(ESearchInProgress);
       
   221 			Reset();
       
   222 			TopicIdSearchL();
       
   223 			BuildViewL();
       
   224 			}
       
   225 		else
       
   226 			ReportEventToObserverL(ESearchComplete);
       
   227 		break;
       
   228 	default:
       
   229 		ReportEventToObserverL(aEvent);
       
   230 		break;
       
   231 		}
       
   232 	}
       
   233 
       
   234 
       
   235 void CHlpSQLSearch::BuildViewL()
       
   236 	{
       
   237 	iView->Close();	
       
   238 	TDbQuery query(iSQLStatement->SearchStatement(), EDbCompareFolded);
       
   239 	User::LeaveIfError(iView->Prepare(iDatabase->Database(), query, TDbWindow::EUnlimited, RDbRowSet::EReadOnly));
       
   240 
       
   241 	if	(iFlags & EFlagsAsynchronousSearch)
       
   242 		iSQLEvaluator->Initialize(*iView, *this);
       
   243 	else
       
   244 		{
       
   245 		User::LeaveIfError(iView->EvaluateAll());
       
   246 		if	(iView->CountL())
       
   247 			ReportEventToObserverL(ESearchComplete);
       
   248 		else
       
   249 			ReportEventToObserverL(ENoRecordsFound);
       
   250 		}
       
   251 	}
       
   252 
       
   253 
       
   254 //
       
   255 // CHlpSQLEvaluator class
       
   256 //
       
   257 
       
   258 CHlpSQLEvaluator::CHlpSQLEvaluator(TInt aPriority)
       
   259 :	CActive(aPriority)
       
   260 	{
       
   261 	CActiveScheduler::Add(this);
       
   262 	}
       
   263 
       
   264 CHlpSQLEvaluator::~CHlpSQLEvaluator()
       
   265 	{
       
   266 	Cancel();
       
   267 	}
       
   268 
       
   269 CHlpSQLEvaluator* CHlpSQLEvaluator::NewLC(TInt aPriority)
       
   270 	{
       
   271 	CHlpSQLEvaluator* self = new (ELeave) CHlpSQLEvaluator(aPriority);
       
   272 	CleanupStack::PushL(self);
       
   273 	return self;
       
   274 	}
       
   275 
       
   276 CHlpSQLEvaluator* CHlpSQLEvaluator::NewL(TInt aPriority)
       
   277 	{
       
   278 	CHlpSQLEvaluator* self = CHlpSQLEvaluator::NewLC(aPriority);
       
   279 	CleanupStack::Pop();
       
   280 	return self;
       
   281 	}
       
   282 
       
   283 void CHlpSQLEvaluator::Initialize(RDbView& aView, MHlpPrivObserver& aObserver)
       
   284 	{
       
   285 	Cancel();
       
   286 	iView = &aView;
       
   287 	iObserver = &aObserver;
       
   288 	Start();
       
   289 	}
       
   290 
       
   291 
       
   292 void CHlpSQLEvaluator::Start()
       
   293 	{
       
   294 	// Kludge an asynch request
       
   295 	iStatus = KRequestPending;
       
   296 	SetActive();
       
   297 	TRequestStatus* pS = &iStatus;
       
   298 	User::RequestComplete(pS, KErrNone);
       
   299 	}
       
   300 
       
   301 void CHlpSQLEvaluator::RunL()
       
   302 	{
       
   303 	if	(iStatus != KErrNone)
       
   304 		{
       
   305 		// Panic in debug mode
       
   306 		__ASSERT_DEBUG(0, Panic(EHlpAsynchSearchError));
       
   307 
       
   308 		// In release mode return nothing found
       
   309 		iObserver->HandleSearchEventL(ENoRecordsFound);
       
   310 		}
       
   311 	else
       
   312 		{
       
   313 		TInt more = iView->Evaluate();
       
   314 		TInt count = (more >= 0)? iView->CountL() : 0;
       
   315 		if	(more > 0)
       
   316 			{
       
   317 			Start();
       
   318 			iObserver->HandleSearchEventL(ESearchInProgress);
       
   319 			}
       
   320 		else 
       
   321 			iObserver->HandleSearchEventL((count)? ESearchComplete : ENoRecordsFound);
       
   322 		}
       
   323 	}
       
   324 
       
   325 void CHlpSQLEvaluator::DoCancel()
       
   326 	{
       
   327 	TRAP_IGNORE(iObserver->HandleSearchEventL(EHlpSearchCancelled)); // API is part of framework, could leave 
       
   328 	}
       
   329 
       
   330 
       
   331 
       
   332 
       
   333 //
       
   334 // CHlpSQLBuffer class
       
   335 //
       
   336 
       
   337 
       
   338 void CHlpSQLBuffer::ConstructL(TInt aBufferSize)
       
   339 	{
       
   340 	iSQLStatement = HBufC::NewL(aBufferSize);
       
   341 	}
       
   342 
       
   343 CHlpSQLBuffer::CHlpSQLBuffer()
       
   344 	{
       
   345 	}
       
   346 
       
   347 CHlpSQLBuffer::~CHlpSQLBuffer()
       
   348 	{
       
   349 	delete iSQLStatement;
       
   350 	}
       
   351 
       
   352 void CHlpSQLBuffer::AppendL(TInt aNum)
       
   353 	{
       
   354 	TBuf<32> buf;
       
   355 	buf.AppendNum(aNum);
       
   356 	AppendTextL(buf);
       
   357 	}
       
   358 
       
   359 void CHlpSQLBuffer::AppendL(const TDesC& aDes)
       
   360 	{
       
   361 	_LIT(KHlpModelInvertedComma, "'");
       
   362 
       
   363 	// If the text contains inverted commas, then these must be escaped before
       
   364 	// passing down to DBMS.
       
   365 	TInt pos = aDes.Find(KHlpModelInvertedComma);
       
   366 	if	(pos != KErrNotFound)
       
   367 		{
       
   368 		// Worst case scenario is that every character needs escaping...
       
   369 		const TInt length = aDes.Length();
       
   370 		HBufC* escapedText = HBufC::NewLC(length * 2);
       
   371 		TPtr pText(escapedText->Des());
       
   372 		pText.Copy(aDes);
       
   373 		// Run through the text inserting extra apostrophes:
       
   374 		for(TInt i=0; i<length; i++)
       
   375 			{
       
   376 			const TPtrC pChar(pText.Mid(i, 1));
       
   377 			if	(pChar == KHlpModelInvertedComma)
       
   378 				{
       
   379 				pText.Insert(i, KHlpModelInvertedComma);
       
   380 
       
   381 				// Skip over the newly inserted character
       
   382 				++i;
       
   383 				}
       
   384 			}
       
   385 		// Perform a normal append of the resulting escaped text:
       
   386 		AppendTextL(*escapedText);
       
   387 		CleanupStack::PopAndDestroy(escapedText);
       
   388 		}
       
   389 	else
       
   390 		{
       
   391 		// No apostrophes so no escaping needed:
       
   392 		AppendTextL(aDes);
       
   393 		}
       
   394 	}
       
   395 
       
   396 void CHlpSQLBuffer::AppendSQLL(const TDesC& aDes)
       
   397 	{
       
   398 	AppendTextL(aDes);
       
   399 	}
       
   400 
       
   401 void CHlpSQLBuffer::AppendTextL(const TDesC& aDes)
       
   402 	{
       
   403 	TPtr tempPtr(iSQLStatement->Des());
       
   404 	const TInt KExtraBufferSize = 100;
       
   405 	if ( (tempPtr.Length() + aDes.Length()) <= tempPtr.MaxLength() )
       
   406 		{
       
   407 		tempPtr.Append(aDes);
       
   408 		}
       
   409 	else
       
   410 		{
       
   411 		const TInt newSize = Max(KExtraBufferSize, aDes.Length()+iSQLStatement->Length());
       
   412 		iSQLStatement = iSQLStatement->ReAllocL(newSize);
       
   413 		iSQLStatement->Des().Append(aDes);
       
   414 		}
       
   415 	}
       
   416 
       
   417 const TDesC& CHlpSQLBuffer::SearchStatement() const
       
   418 	{
       
   419 	return *iSQLStatement;
       
   420 	}
       
   421 
       
   422 void CHlpSQLBuffer::Reset()
       
   423 	{
       
   424 	iSQLStatement->Des().Zero();
       
   425 	}
       
   426 
       
   427