metadataengine/server/src/mdsfindsqlclause.cpp
changeset 0 c53acadfccc6
child 14 646a02f170b9
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Class constructs SQL search clauses for MdS.*
       
    15 */
       
    16 
       
    17 // INCLUDE FILES
       
    18 #include "mdsfindsqlclause.h"
       
    19 #include "mdsfindsqlclausedef.h"
       
    20 #include "mdsclausebuffer.h"
       
    21 #include "mdcserializationbuffer.h"
       
    22 #include "mdsquerycriteriadeserialization.h"
       
    23 #include "mdsobjectdef.h"
       
    24 #include "mderange.h"
       
    25 #include "mdsschema.h"
       
    26 #include "mdsnamespacedef.h"
       
    27 #include "mdspropertydef.h"
       
    28 #include "mdcquery.h"
       
    29 #include "mdeinternalerror.h"
       
    30 
       
    31 const TInt KQueryBufInitialSize = 512;
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // NewL
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CMdSFindSqlClause* CMdSFindSqlClause::NewL(CMdsSchema& aSchema)
       
    38     {
       
    39     CMdSFindSqlClause* self = NewLC(aSchema);
       
    40     CleanupStack::Pop( self );
       
    41     return self;
       
    42     }
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // NewLC
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CMdSFindSqlClause* CMdSFindSqlClause::NewLC(CMdsSchema& aSchema)
       
    49     {
       
    50     CMdSFindSqlClause* self = new ( ELeave ) CMdSFindSqlClause(aSchema);
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL();
       
    53     return self;
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // ~CMdSSqlClause
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 CMdSFindSqlClause::~CMdSFindSqlClause()
       
    61     {
       
    62     delete iQueryBuf;
       
    63 
       
    64     iResultRow.Close();
       
    65     iVariables.Close();
       
    66 
       
    67     // doesn't own property definitions
       
    68 	iPropertyFilters.Close();
       
    69 	
       
    70 	iFreeText.ResetAndDestroy();
       
    71 	iFreeText.Close();
       
    72 	
       
    73 	if( iSourceObjectDefs )
       
    74 		{
       
    75 		iSourceObjectDefs->Close();
       
    76 
       
    77 		delete iSourceObjectDefs;
       
    78 		}
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // CMdSSqlClause
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 CMdSFindSqlClause::CMdSFindSqlClause(CMdsSchema& aSchema) : 
       
    86     iVariables(), iQueryBuf( NULL ), iSchema( aSchema ), 
       
    87     iSearchCriteria( NULL ), iSerializedBuffer( NULL ), 
       
    88     iUserLevel( EUserLevelNone ), iNamespaceDef( NULL ), iObjectDef( NULL ), 
       
    89     iSourceObjectDef ( NULL ), iSourceObjectDefs( NULL ), 
       
    90     iAppendToResultRow( ETrue ), iIncludeFreetexts( ETrue ), 
       
    91     iIncludeNotPresent( EFalse ), iNoObjectLocking( ETrue ), iPlaceholdersOnly( EFalse )
       
    92     {
       
    93     }
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // ConstructL
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 void CMdSFindSqlClause::ConstructL()
       
   100     {
       
   101     // Construct empty buffer for sql query
       
   102     iQueryBuf = CMdsClauseBuffer::NewL( KQueryBufInitialSize );
       
   103     iQueryBuf->AppendL( KEmpty );
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 // AsText           Returns sql clause
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 const TDesC& CMdSFindSqlClause::AsTextL() const
       
   111     {
       
   112     return iQueryBuf->ConstBufferL();
       
   113     }
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // Variables        Query variables
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 RRowData& CMdSFindSqlClause::Variables()
       
   120     {
       
   121     return iVariables;
       
   122     }
       
   123 
       
   124 // ---------------------------------------------------------------------------
       
   125 // Variables        Query result row
       
   126 // ---------------------------------------------------------------------------
       
   127 //
       
   128 RRowData& CMdSFindSqlClause::ResultRow()
       
   129     {
       
   130     return iResultRow;
       
   131     }
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // Limit        Query row limit
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 TUint32 CMdSFindSqlClause::Limit()
       
   138 	{
       
   139 	return iSearchCriteria->iLimit;
       
   140 	}
       
   141 	
       
   142 TBool CMdSFindSqlClause::IncludesFreetexts()
       
   143 	{
       
   144 	return iIncludeFreetexts;
       
   145 	}
       
   146 
       
   147 TQueryType CMdSFindSqlClause::QueryType()
       
   148 	{
       
   149 	return (TQueryType)iSearchCriteria->iQueryType;
       
   150 	}
       
   151 
       
   152 TQueryResultMode CMdSFindSqlClause::ResultMode()
       
   153 	{
       
   154 	return (TQueryResultMode)iSearchCriteria->iQueryResultType;
       
   155 	}
       
   156 
       
   157 RPointerArray<HBufC>& CMdSFindSqlClause::QueryFreeText()
       
   158 	{
       
   159 	return iFreeText;
       
   160 	}
       
   161 
       
   162 TBool CMdSFindSqlClause::NoObjectLocking()
       
   163 	{
       
   164 	return iNoObjectLocking;
       
   165 	}
       
   166 
       
   167 // ---------------------------------------------------------------------------
       
   168 // ObjectDef        Query object definition
       
   169 // ---------------------------------------------------------------------------
       
   170 //
       
   171 CMdsObjectDef* CMdSFindSqlClause::ObjectDef()
       
   172 	{
       
   173 	return iObjectDef;
       
   174 	}
       
   175 
       
   176 RPointerArray<CMdsPropertyDef>& CMdSFindSqlClause::PropertyFilters()
       
   177 	{
       
   178 	return iPropertyFilters;
       
   179 	}
       
   180 
       
   181 // ---------------------------------------------------------------------------
       
   182 // NamespaceDef        Query namespace definition
       
   183 // ---------------------------------------------------------------------------
       
   184 //
       
   185 CMdsNamespaceDef* CMdSFindSqlClause::NamespaceDef()
       
   186 	{
       
   187 	return iNamespaceDef;
       
   188 	}
       
   189 
       
   190 // ---------------------------------------------------------------------------
       
   191 // CreateL          Creates whole sql clause
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 void CMdSFindSqlClause::CreateL( 
       
   195 	CMdCSerializationBuffer& aSerializedCriteria, 
       
   196 	TUserLevel aUserLevel )
       
   197     {
       
   198     iUserLevel = aUserLevel;
       
   199 
       
   200     iSerializedBuffer = &aSerializedCriteria;
       
   201 
       
   202     iSearchCriteria = CONST_CAST( TMdCSearchCriteria*, 
       
   203     		&TMdCSearchCriteria::GetFromBufferL( aSerializedCriteria ) );
       
   204 
       
   205 	// can freetexts drop from results
       
   206 	if( ( iSearchCriteria->iOptimizationFlags & EContainsFreetextCondition ) == 0 )
       
   207 		{
       
   208 		iIncludeFreetexts = EFalse;
       
   209 		}
       
   210 
       
   211 	if( iSearchCriteria->iOptimizationFlags & EContainsNotPresentCondition )
       
   212 		{
       
   213 		iIncludeNotPresent = ETrue;
       
   214 		}
       
   215 
       
   216 	if( iSearchCriteria->iOptimizationFlags & EContainsObjectLocking )
       
   217 		{
       
   218 		iNoObjectLocking = EFalse;
       
   219 		}
       
   220 	
       
   221 	if( iSearchCriteria->iOptimizationFlags & EContainsPlaceholdersOnly )
       
   222         {
       
   223         iPlaceholdersOnly = ETrue;
       
   224         }
       
   225 	
       
   226 
       
   227 	// get namespace def from new schema definition
       
   228 	iNamespaceDef = const_cast<CMdsNamespaceDef*>(iSchema.GetNamespaceByIdL( 
       
   229 			iSearchCriteria->iNamespaceDefId ));
       
   230 
       
   231 	if ( iNamespaceDef )
       
   232 		{
       
   233 		iNamespaceIdDes.Num( iNamespaceDef->GetId() );
       
   234 		}
       
   235 	else
       
   236 		{
       
   237 		User::Leave( KErrMdEUnknownNamespaceDef );
       
   238 		}
       
   239 
       
   240 	iObjectDef = NULL;
       
   241 
       
   242 	if( iSearchCriteria->iObjectDefId != KNoDefId )
       
   243 		{
       
   244 		// get object def from new schema definition
       
   245 		iObjectDef = CONST_CAST( CMdsObjectDef*, 
       
   246 				iNamespaceDef->GetObjectByIdL( iSearchCriteria->iObjectDefId ) );
       
   247 
       
   248 		if ( !iObjectDef )
       
   249 			{
       
   250 			User::Leave( KErrMdEUnknownObjectDef );
       
   251 			}
       
   252 		}
       
   253 
       
   254 	iSourceObjectDef = NULL;
       
   255 	iSourceObjectDefs = NULL;
       
   256 
       
   257 	if ( iSearchCriteria->iObjectDefIds.iPtr.iCount > 0 )
       
   258 		{
       
   259 		iSourceObjectDefs = new (ELeave) RArray<TDefId>( 
       
   260 				iSearchCriteria->iObjectDefIds.iPtr.iCount );
       
   261 		iSourceObjectDefs->ReserveL( iSearchCriteria->iObjectDefIds.iPtr.iCount );
       
   262 
       
   263 		iSerializedBuffer->PositionL( iSearchCriteria->iObjectDefIds.iPtr.iOffset );
       
   264 
       
   265 		for( TUint32 i = 0; i < iSearchCriteria->iObjectDefIds.iPtr.iCount; i++ )
       
   266 			{
       
   267 			TDefId objectDefId;
       
   268 			iSerializedBuffer->ReceiveL( objectDefId );			
       
   269 			iSourceObjectDefs->Append( objectDefId );
       
   270 			}
       
   271 		}
       
   272 	else
       
   273 		{
       
   274 		iSourceObjectDef = iObjectDef;
       
   275 		}
       
   276 
       
   277 	if( !iSourceObjectDefs )
       
   278 		{
       
   279 		// Construct beginning of the select clause 
       
   280 		// Format: SELECT [column list/count(*)] FROM table name
       
   281 		AppendSelectStatementL();
       
   282 
       
   283 		AppendWhereStatementL();
       
   284 
       
   285 		// if query type is object query and freetext are included
       
   286 		// "GROUP BY BO.ObjectId"
       
   287 		if( iIncludeFreetexts )
       
   288 			{
       
   289 		    AppendGroupByL();
       
   290 			}
       
   291 		}
       
   292 	else
       
   293 		{
       
   294 		const TInt sourceObjectDefsCount = iSourceObjectDefs->Count();
       
   295 		for( TInt i = 0; i < sourceObjectDefsCount; i++ )
       
   296 			{
       
   297 			const TDefId sourceObjectDefId = (*iSourceObjectDefs)[i];
       
   298 			iSourceObjectDef = CONST_CAST( CMdsObjectDef*, 
       
   299 					iNamespaceDef->GetObjectByIdL( sourceObjectDefId ) );
       
   300 
       
   301 			if( i > 0 )
       
   302 				{
       
   303 				iAppendToResultRow = EFalse;
       
   304 				iQueryBuf->AppendL( KUnionAll );
       
   305 				}
       
   306 
       
   307 			// Construct beginning of the select clause 
       
   308 			// Format: SELECT [column list/count(*)] FROM table name
       
   309 			AppendSelectStatementL();
       
   310 			
       
   311 			AppendWhereStatementL();
       
   312 			    
       
   313 			// if query type is object query and freetext are included
       
   314 			// "GROUP BY BO.ObjectId"
       
   315 			if( iIncludeFreetexts )
       
   316 				{
       
   317 			    AppendGroupByL();
       
   318 				}
       
   319 			}
       
   320 		}
       
   321 
       
   322     // ORDER BY clause (if such exists)
       
   323     AppendOrderByL();
       
   324 
       
   325     // LIMIT and OFFSET clauses (if such exists)
       
   326     AppendLimitAndOffsetL();
       
   327 
       
   328     // SQL clause must end to semicolon
       
   329     iQueryBuf->AppendL( KSemicolon );
       
   330     }
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 // AppendPropertyFiltersL       Appends the property filters
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 void CMdSFindSqlClause::AppendPropertyFiltersL( TBool aDistinct )
       
   337 	{
       
   338 	if( iSearchCriteria->iPropertyFilters.iPtr.iCount > 0 )
       
   339 		{
       
   340 		iSerializedBuffer->PositionL( 
       
   341 				iSearchCriteria->iPropertyFilters.iPtr.iOffset );
       
   342 
       
   343 		for ( TUint32 i = 0; i < iSearchCriteria->iPropertyFilters.iPtr.iCount; ++i )
       
   344 			{
       
   345 			TDefId id;
       
   346 			iSerializedBuffer->ReceiveL( id );
       
   347 
       
   348 			if ( !aDistinct )
       
   349 				{
       
   350 				iQueryBuf->AppendL( KComma );
       
   351 				}
       
   352 
       
   353 			AppendColumnByPropertyDefIdL( id, KNoDefId, ETrue );
       
   354 			}
       
   355 
       
   356 		iQueryBuf->AppendL( KSpace );
       
   357 		}
       
   358 	}
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // AppendSelectStatementL       Appends the SELECT statements
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 void CMdSFindSqlClause::AppendSelectStatementL()
       
   365     {
       
   366     switch( iSearchCriteria->iQueryType )
       
   367     	{
       
   368     	case EQueryTypeObject:
       
   369     		AppendSelectForObjectL();
       
   370     		AppendFromForObjectL();
       
   371 	    	break;
       
   372 	    case EQueryTypeRelation:
       
   373 	    	AppendSelectForRelationL();
       
   374 	    	AppendFromForRelationL();
       
   375 	    	break;
       
   376         case EQueryTypeEvent:
       
   377 	    	AppendSelectForEventL();
       
   378   	 		AppendFromForEventL();
       
   379 	    	break;
       
   380 	    default:
       
   381 #ifdef _DEBUG
       
   382             User::Panic( _L("MdSFCASS") , KErrMdEUnknownQueryType );
       
   383 #endif
       
   384 	    	User::Leave( KErrMdEUnknownQueryType );
       
   385     	}
       
   386     }
       
   387 
       
   388 // ---------------------------------------------------------------------------
       
   389 // AppendSelectCountStatementL        SELECT statements for count query
       
   390 // ---------------------------------------------------------------------------
       
   391 //
       
   392 void CMdSFindSqlClause::AppendSelectCountStatementL()
       
   393     {
       
   394     // if result rows needs to be grouped return just "1"
       
   395     if( iIncludeFreetexts )
       
   396 		{
       
   397 		// "SELECT 1 "
       
   398 		iQueryBuf->AppendL( KSelect1 );
       
   399 		}
       
   400 	else
       
   401 		{
       
   402 		// "SELECT count(*) "
       
   403     	iQueryBuf->AppendL( KSelectCount );
       
   404 		}
       
   405 
       
   406 	// expected result count
       
   407     if( iAppendToResultRow )
       
   408     	{
       
   409     	iResultRow.AppendL( TColumn( EColumnUint32 ) );
       
   410     	}
       
   411     }
       
   412 
       
   413 // ---------------------------------------------------------------------------
       
   414 // AppendSelectDistinctStatementL   SELECT statements for distinct value query
       
   415 // ---------------------------------------------------------------------------
       
   416 //
       
   417 void CMdSFindSqlClause::AppendSelectDistinctStatementL()
       
   418 	{
       
   419 	// "SELECT DISTINCT "
       
   420 	iQueryBuf->AppendL( KSelectDistinct );
       
   421 
       
   422 	const TUint32 position = iSerializedBuffer->Position();
       
   423 
       
   424 	// "column name" (from property filter)
       
   425 	AppendPropertyFiltersL( ETrue );
       
   426 
       
   427 	iSerializedBuffer->PositionL( position );
       
   428 	}
       
   429 
       
   430 // ---------------------------------------------------------------------------
       
   431 // AppendSelectObjectIdStatementL        SELECT statements for object id query
       
   432 // ---------------------------------------------------------------------------
       
   433 //
       
   434 void CMdSFindSqlClause::AppendSelectObjectIdStatementL()
       
   435 	{
       
   436 	// "SELECT BO.ObjectId "
       
   437 	iQueryBuf->AppendL( KSelectObjectIds );
       
   438 
       
   439 	// expected result object ID
       
   440 	if( iAppendToResultRow )
       
   441 		{
       
   442 		iResultRow.AppendL( TColumn( EColumnTItemId ) );
       
   443 		}
       
   444 	}
       
   445 
       
   446 // ---------------------------------------------------------------------------
       
   447 // AppendSelectEventAllStatementL        SELECT statements for event query
       
   448 // ---------------------------------------------------------------------------
       
   449 //
       
   450 void CMdSFindSqlClause::AppendSelectAllFromEventStatementL()
       
   451 	{
       
   452 	// "SELECT * "
       
   453 	iQueryBuf->AppendL( KSelectAllFrom );
       
   454 
       
   455 	if( iAppendToResultRow )
       
   456 		{
       
   457 		// expected result Events 
       
   458 	    iResultRow.AppendL( TColumn( EColumnTItemId ) );
       
   459 	    iResultRow.AppendL( TColumn( EColumnTItemId ) );
       
   460 	    iResultRow.AppendL( TColumn( EColumnTDefId ) );
       
   461 	    iResultRow.AppendL( TColumn( EColumnTime ) );
       
   462 	    iResultRow.AppendL( TColumn( EColumnDes16 ) );
       
   463 	    iResultRow.AppendL( TColumn( EColumnDes16 ) );
       
   464 		}
       
   465 	}
       
   466 
       
   467 // ---------------------------------------------------------------------------
       
   468 // AppendSelectRelationAllStatementL        SELECT statements for Relaltion query
       
   469 // ---------------------------------------------------------------------------
       
   470 //
       
   471 void CMdSFindSqlClause::AppendSelectAllFromRelationStatementL()
       
   472 	{
       
   473 	// "SELECT * "
       
   474 	iQueryBuf->AppendL( KSelectAllFrom );
       
   475 
       
   476 	if( iAppendToResultRow )
       
   477 		{
       
   478 		// expected result Relation
       
   479 	    iResultRow.AppendL( TColumn( EColumnTItemId ) );
       
   480 	    iResultRow.AppendL( TColumn( EColumnUint32 ) );
       
   481 	    iResultRow.AppendL( TColumn( EColumnTDefId ) );
       
   482 	    iResultRow.AppendL( TColumn( EColumnTItemId ) );
       
   483 	    iResultRow.AppendL( TColumn( EColumnTItemId ) );
       
   484 	    iResultRow.AppendL( TColumn( EColumnInt32 ) );
       
   485 	    iResultRow.AppendL( TColumn( EColumnInt64 ) );
       
   486 	    iResultRow.AppendL( TColumn( EColumnInt64 ) );
       
   487 	    iResultRow.AppendL( TColumn( EColumnTime ) );
       
   488 		}
       
   489 	}
       
   490 
       
   491 // ---------------------------------------------------------------------------
       
   492 // AppendSelectEventItemStatementL        SELECT statements for event id query
       
   493 // ---------------------------------------------------------------------------
       
   494 //
       
   495 void CMdSFindSqlClause::AppendSelectEventItemStatementL()
       
   496     {
       
   497 	// "SELECT Event + NamespaceId.EventId "
       
   498 	iQueryBuf->AppendL( KSelectEventIds );
       
   499 	iQueryBuf->AppendL( iNamespaceIdDes );
       
   500 	iQueryBuf->AppendL( KDotEventId );
       
   501 
       
   502 	if( iAppendToResultRow )
       
   503 		{
       
   504 		// expected result event
       
   505 	    iResultRow.AppendL( TColumn( EColumnTItemId ) );
       
   506 		}
       
   507     }
       
   508 
       
   509 // ---------------------------------------------------------------------------
       
   510 // AppendSelectRealtionItemStatementL        SELECT statements for Realtion id query
       
   511 // ---------------------------------------------------------------------------
       
   512 //AppendSelectRelationItemStatementL
       
   513 void CMdSFindSqlClause::AppendSelectRealtionItemStatementL()
       
   514     {
       
   515 	 //"SELECT Relation.EventId "
       
   516 	iQueryBuf->AppendL( KSelectRelationIds );
       
   517     iQueryBuf->AppendL( iNamespaceIdDes );
       
   518     iQueryBuf->AppendL( KDotRelationId );
       
   519 	
       
   520 	if( iAppendToResultRow )
       
   521 		{
       
   522 		//expected result relation
       
   523 		iResultRow.AppendL( TColumn( EColumnTItemId ) );
       
   524 		}
       
   525     }
       
   526 
       
   527 // ---------------------------------------------------------------------------
       
   528 // AppendSelectObjectItemStatementL    SELECT statements for object item query
       
   529 // ---------------------------------------------------------------------------
       
   530 //
       
   531 void CMdSFindSqlClause::AppendSelectObjectItemStatementL()
       
   532 	{
       
   533 	if( iAppendToResultRow )
       
   534 		{
       
   535 		// add expected fixed "base" object's columns
       
   536 		iResultRow.AppendL( TColumn( EColumnTItemId ) ); // object ID
       
   537 		iResultRow.AppendL( TColumn( EColumnTDefId ) ); // object definition ID
       
   538 		iResultRow.AppendL( TColumn( EColumnUint32 ) ); // flags
       
   539 		iResultRow.AppendL( TColumn( EColumnUint32 ) ); // media ID
       
   540 		iResultRow.AppendL( TColumn( EColumnUint32 ) ); // UsageCount
       
   541 		iResultRow.AppendL( TColumn( EColumnInt64 ) );  // GUID high
       
   542 		iResultRow.AppendL( TColumn( EColumnInt64 ) );  // GUID low
       
   543 		iResultRow.AppendL( TColumn( EColumnDes16 ) );  // URI
       
   544 		}
       
   545 
       
   546 	if( iSearchCriteria->iPropertyFilters.iPtr.iCount > 0 )
       
   547 		{
       
   548 		// "SELECT BO...."
       
   549 		iQueryBuf->AppendL( KSelectPropertyFilterBegin );
       
   550 
       
   551 		const TUint32 position = iSerializedBuffer->Position();
       
   552 
       
   553 		AppendPropertyFiltersL();
       
   554 
       
   555 		iSerializedBuffer->PositionL( position );
       
   556 		}
       
   557 	else
       
   558 		{
       
   559 		// No property filters so get all properties
       
   560 		
       
   561 		// "SELECT BO.* "
       
   562 		iQueryBuf->AppendL( KSelectAllFromBaseObject );
       
   563 	
       
   564 		const TInt propCount = iObjectDef->GetAllPropertiesCount();
       
   565 		TBool addBaseObjectId = EFalse;
       
   566 		
       
   567 		if( KBaseObjectDefId != iObjectDef->GetId() )
       
   568 			{
       
   569 			if( !iSourceObjectDefs )
       
   570 				{
       
   571 				// ",O.* "
       
   572 				iQueryBuf->AppendL( KAllFromObject );
       
   573 				}
       
   574 			else
       
   575 				{
       
   576 				// ",O.ObjectId"
       
   577 				iQueryBuf->AppendL( KComma );
       
   578 				iQueryBuf->AppendL( KObjectObjectId );
       
   579 
       
   580 				// no filters, so add all property names
       
   581 				const TInt lookupObjectPropertyCount = 
       
   582 					iObjectDef->GetPropertiesCount();
       
   583 				
       
   584 				for( TInt i = 0; i < lookupObjectPropertyCount; i++)
       
   585 					{
       
   586 					iQueryBuf->AppendL( KComma );
       
   587 					const CMdsPropertyDef* propDef = iObjectDef->GetProperty( i );
       
   588 					iQueryBuf->AppendL( propDef->GetName() );
       
   589 					}
       
   590 				iQueryBuf->AppendL( KSpace );
       
   591 				}
       
   592 			addBaseObjectId = ETrue;
       
   593 			}
       
   594 
       
   595 		if (propCount > 0)
       
   596 			{
       
   597 			// add base object properties, object ID and the rest properties
       
   598 			for( TUint32 i = 0; i < propCount; i++ )
       
   599 				{
       
   600 				const CMdsPropertyDef& propDef = iObjectDef->GetPropertyColumnL( i ).iPropertyDef;
       
   601 
       
   602 				// add column for object ID when object definition is not "base" object
       
   603 				if( addBaseObjectId && KBaseObjectBasicValueColumnOffset < propDef.GetId() )
       
   604 					{
       
   605 					if( iAppendToResultRow )
       
   606 						{
       
   607 			    	    // add expected column for object ID
       
   608 						iResultRow.AppendL( TColumn( EColumnTItemId ) ); // object ID
       
   609 						}
       
   610 					addBaseObjectId = EFalse;
       
   611 					}
       
   612 
       
   613 				if( iAppendToResultRow )
       
   614 					{
       
   615 					// add expected columns	from object
       
   616 					iResultRow.AppendL( TColumn( propDef.GetSqlType() ) );
       
   617 					}
       
   618 				}
       
   619 				
       
   620 			if( addBaseObjectId )
       
   621 				{
       
   622 				if( iAppendToResultRow )
       
   623 					{
       
   624 					// add expected column for object ID
       
   625 					iResultRow.AppendL( TColumn( EColumnTItemId ) ); // object ID
       
   626 					}
       
   627 				}
       
   628 			}
       
   629 		}
       
   630 
       
   631 	if( iIncludeFreetexts )
       
   632 		{
       
   633 		// ",count(D.Word),sum(length(D.Word)) "
       
   634 		iQueryBuf->AppendL( KSelectFreeTextCountAndTotalLength );
       
   635 
       
   636 		if( iAppendToResultRow )
       
   637 			{
       
   638 			iResultRow.AppendL( TColumn( EColumnUint32 ) );
       
   639 			iResultRow.AppendL( TColumn( EColumnUint32 ) );
       
   640 			}
       
   641 		}
       
   642 	}
       
   643 
       
   644 // ---------------------------------------------------------------------------
       
   645 // AppendFromForObjectL           FROM statements for object query
       
   646 // ---------------------------------------------------------------------------
       
   647 //
       
   648 void CMdSFindSqlClause::AppendFromForObjectL()
       
   649 	{
       
   650 	// "FROM BaseObject" + namespace def id + " AS BO "
       
   651 	iQueryBuf->AppendL( KFromBaseObject );
       
   652 	iQueryBuf->AppendL( iNamespaceIdDes );
       
   653 	iQueryBuf->AppendL( KAsBaseObject );
       
   654 
       
   655 	// if object definitions is BaseObject
       
   656 	if( KBaseObjectDefId != iSourceObjectDef->GetId() )
       
   657 		{
       
   658 		// "," + object def's table + namespace def id + " AS O ON BO.ObjectId=O.ObjectId "
       
   659         iQueryBuf->AppendL( KComma );
       
   660 
       
   661 		AppendTableByObjectDefIdL( iSourceObjectDef->GetId() );
       
   662 
       
   663         iQueryBuf->AppendL( KAsObjectOnEqual );
       
   664 		}
       
   665 
       
   666 	if( iIncludeFreetexts )
       
   667 		{
       
   668 		// Append free text join
       
   669 		// "LEFT JOIN TextSearchDictionary" + namespace def id + " 
       
   670 		// AS D ON D.WordId IN(SELECT WordId FROM TextSearch" + 
       
   671 		// namespace def id + " WHERE BO.ObjectId=ObjectId) "
       
   672 		iQueryBuf->AppendL( KFreeTextLeftJoinBegin );
       
   673 		iQueryBuf->AppendL( iNamespaceIdDes );
       
   674 		iQueryBuf->AppendL( KFreeTextLeftJoinMiddle );
       
   675 		iQueryBuf->AppendL( iNamespaceIdDes );
       
   676 		iQueryBuf->AppendL( KFreeTextLeftJoinEnd );
       
   677 		}
       
   678 	}
       
   679 
       
   680 
       
   681 // ---------------------------------------------------------------------------
       
   682 // AppendFromForObjectL           FROM statements for Event query
       
   683 // ---------------------------------------------------------------------------
       
   684 //
       
   685 void CMdSFindSqlClause::AppendFromForEventL()
       
   686     {
       
   687    	iQueryBuf->AppendL( KFromEvent );
       
   688 	iQueryBuf->AppendL( iNamespaceIdDes );
       
   689    	iQueryBuf->AppendL( KSpace );
       
   690     }
       
   691 
       
   692 // ---------------------------------------------------------------------------
       
   693 // AppendFromForRelationL           FROM statements for Relation query
       
   694 // ---------------------------------------------------------------------------
       
   695 //
       
   696 void CMdSFindSqlClause::AppendFromForRelationL()
       
   697     {
       
   698    	iQueryBuf->AppendL( KFromRealtions );
       
   699 	iQueryBuf->AppendL( iNamespaceIdDes );
       
   700 	iQueryBuf->AppendL( KSpace );
       
   701     }
       
   702 
       
   703 
       
   704 // ---------------------------------------------------------------------------
       
   705 // AppendWhereStatementL           WHERE statements for query
       
   706 // ---------------------------------------------------------------------------
       
   707 //
       
   708 void CMdSFindSqlClause::AppendWhereStatementL()
       
   709 	{
       
   710 	// move position to begin of the root condition
       
   711 	iSerializedBuffer->PositionL( iSearchCriteria->iRootCondition );
       
   712 
       
   713 	const TMdCLogicCondition& condition = TMdCLogicCondition::GetFromBufferL( 
       
   714 			*iSerializedBuffer );
       
   715 
       
   716 	if( EConditionTypeLogic == condition.iConditionType )
       
   717 		{
       
   718 		// if root condition's child count is more than zero add where statement
       
   719 		if( condition.iChildConditions.iPtr.iCount > 0 )
       
   720 			{
       
   721 			// "WHERE("root condition")"
       
   722 			iQueryBuf->AppendL( KWhere );
       
   723 
       
   724 			if( EQueryTypeObject == iSearchCriteria->iQueryType )
       
   725 				{
       
   726 				// remove "deleted" objects from result
       
   727 				iQueryBuf->AppendL( KGetNotFlagAnd );
       
   728 				iVariables.AppendL( TColumn( EMdEObjectFlagRemoved ) );
       
   729 
       
   730 				if( EUserLevelDeviceAccess != iUserLevel )
       
   731 					{
       
   732 					if( iIncludeNotPresent )
       
   733 						{
       
   734 						// If client application doesn't have device capability limit to
       
   735 						// only non-confidential (confidential flag is not set)
       
   736 						// "(NOT Flags&?)AND "
       
   737 						iQueryBuf->AppendL( KGetNotFlagAnd );
       
   738 						iVariables.AppendL( TColumn( EMdEObjectFlagConfidential ) );
       
   739 						}
       
   740 					else
       
   741 						{
       
   742 						// If client application doesn't have device capability limit to
       
   743 						// only non-confidential (confidential flag is not set)
       
   744 						// and not present objects
       
   745 						// "(NOT Flags&?)AND "
       
   746 						iQueryBuf->AppendL( KGetNotFlagAnd );
       
   747 						iVariables.AppendL( TColumn( EMdEObjectFlagConfidential | EMdEObjectFlagNotPresent ) );
       
   748 						}
       
   749 					}
       
   750 				else
       
   751 					{
       
   752 					if( !iIncludeNotPresent )
       
   753 						{
       
   754 						// Drop not present objects
       
   755 						// "(NOT Flags&?)AND "
       
   756 						iQueryBuf->AppendL( KGetNotFlagAnd );
       
   757 						iVariables.AppendL( TColumn( EMdEObjectFlagNotPresent ) );
       
   758 						}
       
   759 					}
       
   760 				}
       
   761 
       
   762 			if ( EQueryTypeRelation == iSearchCriteria->iQueryType )
       
   763 				{
       
   764 				// add "(NOT Flags&?)AND " with not present flag
       
   765 				iQueryBuf->AppendL( KGetNotFlagAnd );
       
   766 				iVariables.AppendL( TColumn( EMdERelationFlagNotPresent ) );
       
   767 				// add "(NOT Flags&?)AND " with deleted flag
       
   768 				iQueryBuf->AppendL( KGetNotFlagAnd );
       
   769 				iVariables.AppendL( TColumn( EMdERelationFlagDeleted ) );
       
   770 				}
       
   771 
       
   772 			iQueryBuf->AppendL( KLeftBracket );
       
   773 
       
   774 			// move position to begin of the root condition
       
   775 			iSerializedBuffer->PositionL( iSearchCriteria->iRootCondition );
       
   776 
       
   777 			ConditionStatementL();
       
   778 
       
   779 			iQueryBuf->AppendL( KRightBracket );
       
   780 			}
       
   781 		else if( EQueryTypeObject == iSearchCriteria->iQueryType )
       
   782 			{
       
   783 			// remove "deleted" objects from result
       
   784 			iQueryBuf->AppendL( KWhereNotFlag );
       
   785 			iVariables.AppendL( TColumn( EMdEObjectFlagRemoved ) );
       
   786 
       
   787 			if( EUserLevelDeviceAccess != iUserLevel )
       
   788 				{
       
   789 				if( iIncludeNotPresent )
       
   790 					{
       
   791 					// If client application doesn't have device capability limit to
       
   792 					// only non-confidential (confidential flag is not set)
       
   793 					// "WHERE(NOT Flags&?)"
       
   794 					iQueryBuf->AppendL( KAnd );
       
   795 					iQueryBuf->AppendL( KGetNotFlag );
       
   796 					iVariables.AppendL( TColumn( EMdEObjectFlagConfidential ) );
       
   797 					}
       
   798 				else
       
   799 					{
       
   800 					// If client application doesn't have device capability limit to
       
   801 					// only non-confidential (confidential flag is not set)
       
   802 					// and not present objects
       
   803 					// "WHERE(NOT Flags&?)"
       
   804 					iQueryBuf->AppendL( KAnd );
       
   805 					iQueryBuf->AppendL( KGetNotFlag );
       
   806 					iVariables.AppendL( TColumn( EMdEObjectFlagConfidential | EMdEObjectFlagNotPresent ) );
       
   807 					}
       
   808 				}
       
   809 			else
       
   810 				{
       
   811 				if( !iIncludeNotPresent )
       
   812 					{
       
   813 					// Drop not present objects
       
   814 					// "WHERE(NOT Flags&?)"
       
   815 					iQueryBuf->AppendL( KAnd );
       
   816 					iQueryBuf->AppendL( KGetNotFlag );
       
   817 					iVariables.AppendL( TColumn( EMdEObjectFlagNotPresent ) );
       
   818 					}
       
   819 				}
       
   820 			}
       
   821 		else if ( EQueryTypeRelation == iSearchCriteria->iQueryType )
       
   822 			{
       
   823 			// add "WHERE(NOT Flags&?)" with not present flag
       
   824 			iQueryBuf->AppendL( KWhereNotFlag );
       
   825 			iVariables.AppendL( TColumn( EMdERelationFlagNotPresent ) );
       
   826 			// add "AND (NOT Flags&?)" with deleted flag
       
   827 			iQueryBuf->AppendL( KAnd );
       
   828 			iQueryBuf->AppendL( KGetNotFlag );
       
   829 			iVariables.AppendL( TColumn( EMdERelationFlagDeleted ) );
       
   830 			}
       
   831 		}
       
   832 	else
       
   833 		{
       
   834 		User::Leave( KErrMdEUnknownConditionType );
       
   835 		}
       
   836 	}
       
   837 
       
   838 // ---------------------------------------------------------------------------
       
   839 // AppendSelectForObjectGroupL      SELECT statements for object query
       
   840 // ---------------------------------------------------------------------------
       
   841 //
       
   842 void CMdSFindSqlClause::AppendSelectForObjectL()
       
   843     {
       
   844     switch( iSearchCriteria->iQueryResultType )
       
   845 	    {
       
   846 	    case EQueryResultModeCount:
       
   847 	        // Format: SELECT count(*) FROM
       
   848 	        AppendSelectCountStatementL();
       
   849 	        break;
       
   850 	    case EQueryResultModeDistinctValues:
       
   851 	        // Format: SELECT "wanted property's column" FROM
       
   852 	        AppendSelectDistinctStatementL();
       
   853 	        break;
       
   854 	    case EQueryResultModeItem:
       
   855 	    	AppendSelectObjectItemStatementL();
       
   856 	    	break;
       
   857 	    case EQueryResultModeId:
       
   858 	    	AppendSelectObjectIdStatementL();
       
   859 	    	break;
       
   860 
       
   861 	    default:
       
   862 	    	{
       
   863 #ifdef _DEBUG
       
   864             User::Panic( _L("MdSFCSeO") , KErrMdEUnknownQueryResultMode );
       
   865 #endif
       
   866 	    	User::Leave( KErrMdEUnknownQueryResultMode );
       
   867 	    	}
       
   868 	    }
       
   869     }
       
   870 
       
   871 // ---------------------------------------------------------------------------
       
   872 // AppendSelectForRelationGroupL        SELECT statements for relation query
       
   873 // ---------------------------------------------------------------------------
       
   874 //
       
   875 void CMdSFindSqlClause::AppendSelectForRelationL()
       
   876     {
       
   877     switch( iSearchCriteria->iQueryResultType )
       
   878         {
       
   879 	    case EQueryResultModeCount:
       
   880 	        // Format: SELECT count(*) FROM
       
   881 	        AppendSelectCountStatementL();
       
   882 	        break;
       
   883 	    case EQueryResultModeItem:
       
   884 	    	// Format: SELECT * FROM   //  SELECT * FROM Relation
       
   885 	    	AppendSelectAllFromRelationStatementL();
       
   886 	    	break;
       
   887 	    case EQueryResultModeId:
       
   888 	    	// SELECT Relation.RelationId FROM // SELECT 
       
   889 	    	AppendSelectRealtionItemStatementL();
       
   890 	    	break;
       
   891 
       
   892 	    default:
       
   893 	    	{
       
   894 #ifdef _DEBUG
       
   895             User::Panic( _L("MdSFCSeR") , KErrMdEUnknownQueryResultMode );
       
   896 #endif
       
   897 	    	User::Leave( KErrMdEUnknownQueryResultMode );
       
   898 	    	}
       
   899         }
       
   900     }
       
   901 
       
   902 // ---------------------------------------------------------------------------
       
   903 // AppendSelectForEventL()       SELECT statements for event query
       
   904 // ---------------------------------------------------------------------------
       
   905 //
       
   906 void CMdSFindSqlClause::AppendSelectForEventL()
       
   907     {
       
   908     switch( iSearchCriteria->iQueryResultType )
       
   909         {
       
   910 	    case EQueryResultModeCount:
       
   911 	        // Format: SELECT count(*) FROM
       
   912 	        AppendSelectCountStatementL();
       
   913 	        break;
       
   914 	    case EQueryResultModeItem:
       
   915 	    	// Format: SELECT * FROM   //  SELECT * FROM Event
       
   916 	    	AppendSelectAllFromEventStatementL();
       
   917 	    	break;
       
   918 	    case EQueryResultModeId:
       
   919 	    	// SELECT EventDef.EventId FROM // SELECT EventDef.EventDefId FROM Event
       
   920 	    	AppendSelectEventItemStatementL();
       
   921 	    	break;
       
   922 
       
   923 	    default:
       
   924 	    	{
       
   925 #ifdef _DEBUG
       
   926             User::Panic( _L("MdSFCSeE") , KErrMdEUnknownQueryResultMode );
       
   927 #endif	    	
       
   928 	    	User::Leave( KErrMdEUnknownQueryResultMode );
       
   929 	    	}
       
   930         }
       
   931     }
       
   932 
       
   933 // ---------------------------------------------------------------------------
       
   934 // ConditionStatementL       Recursive parsing of condition statements
       
   935 // ---------------------------------------------------------------------------
       
   936 //
       
   937 void CMdSFindSqlClause::ConditionStatementL(TBool aNegated, TAppendIn aAppendIdIn)
       
   938     {
       
   939     const TMdCCondition& condition = TMdCCondition::GetFromBufferL( *iSerializedBuffer );
       
   940 
       
   941     TBool negated = condition.iNegated;
       
   942 
       
   943 	if( aNegated )
       
   944 		{
       
   945 		negated = !negated;
       
   946 		}
       
   947 
       
   948 	const TConditionType conditionType = (TConditionType)condition.iConditionType;
       
   949 
       
   950 	if( aAppendIdIn != EAppendInFalse )
       
   951 		{
       
   952 		if ( EConditionTypeLogic != conditionType )
       
   953 			{
       
   954 			if ( aAppendIdIn == EAppendInTrue )
       
   955 				{
       
   956 				AppendIdInL();
       
   957 				}
       
   958 			else if (aAppendIdIn == EAppendInRelationLeft)
       
   959 				{
       
   960 				iQueryBuf->AppendL( KRelationLeftObjectIdIn );
       
   961 				}
       
   962 			else if (aAppendIdIn == EAppendInRelationRight)
       
   963 				{
       
   964 				iQueryBuf->AppendL( KRelationRightObjectIdIn );
       
   965 				}
       
   966 			else
       
   967 				{
       
   968 #ifdef _DEBUG
       
   969 				User::Panic( _L("MdSFCCo1") , KErrCorrupt );
       
   970 #endif
       
   971 				User::Leave( KErrCorrupt );
       
   972 				}
       
   973 			}
       
   974 
       
   975 		// Add recursive child criteria surrounded by "("child")".
       
   976 		iQueryBuf->AppendL( KLeftBracket );
       
   977 		}
       
   978 
       
   979 	switch( conditionType )
       
   980 		{
       
   981 		case EConditionTypeLogic:
       
   982 			AppendLogicConditionL( negated, aAppendIdIn );
       
   983 			break;
       
   984 		case EConditionTypeObject:
       
   985 			AppendObjectConditionL( negated );
       
   986 			break;
       
   987 		case EConditionTypeProperty:
       
   988 			AppendPropertyConditionL( negated );
       
   989 			break;
       
   990 		case EConditionTypePropertyIntRange:
       
   991 		case EConditionTypePropertyInt64Range:
       
   992 		case EConditionTypePropertyUintRange:
       
   993 		case EConditionTypePropertyRealRange:
       
   994 		case EConditionTypePropertyTimeRange:
       
   995 			AppendPropertyRangeConditionL( negated );
       
   996 			break;
       
   997 		case EConditionTypePropertyText:
       
   998 			AppendPropertyTextConditionL( negated );
       
   999 			break;
       
  1000 		case EConditionTypePropertyBool:
       
  1001 			AppendPropertyBoolConditionL( negated );
       
  1002 			break;
       
  1003 		case EConditionTypeRelation:
       
  1004 			AppendRelationConditionL( negated );
       
  1005 			break;
       
  1006 		case EConditionTypeEvent:
       
  1007 			AppendEventConditionL( negated );
       
  1008 			break;
       
  1009 		default: // Unknown condition type
       
  1010 #ifdef _DEBUG
       
  1011             User::Panic( _L("MdSFCCo2") , KErrMdEUnknownConditionType );
       
  1012 #endif
       
  1013 			User::Leave( KErrMdEUnknownConditionType );
       
  1014 		}
       
  1015 
       
  1016 	if( aAppendIdIn )
       
  1017 		{		
       
  1018 		iQueryBuf->AppendL( KRightBracket );
       
  1019 		}
       
  1020     }
       
  1021 
       
  1022 // ---------------------------------------------------------------------------
       
  1023 // AppendObjectId        Help method for appending object ID clause.
       
  1024 // ---------------------------------------------------------------------------
       
  1025 //
       
  1026 void CMdSFindSqlClause::AppendObjectIdL()
       
  1027 	{
       
  1028 	if( KBaseObjectDefId == iObjectDef->GetId() )
       
  1029 		{
       
  1030 		// "BO.ObjectId"
       
  1031 		iQueryBuf->AppendL( KBaseObjectObjectId );
       
  1032 		}
       
  1033 	else
       
  1034 		{
       
  1035 		// "O.ObjectId"
       
  1036 		iQueryBuf->AppendL( KObjectObjectId );
       
  1037 		}
       
  1038 	}
       
  1039 
       
  1040 // ---------------------------------------------------------------------------
       
  1041 // AppendIdIn                    			Help method for appending object, 
       
  1042 //											event or relation ID IN clause.
       
  1043 // ---------------------------------------------------------------------------
       
  1044 //
       
  1045 void CMdSFindSqlClause::AppendIdInL()
       
  1046 	{
       
  1047 	switch( iSearchCriteria->iQueryType )
       
  1048 		{
       
  1049 		case EQueryTypeObject:
       
  1050 			// "BO.ObjectId" or "O.ObjectId"
       
  1051 			AppendObjectIdL();
       
  1052 			break;
       
  1053 		case EQueryTypeRelation:
       
  1054 			iQueryBuf->AppendL( KRelationId );
       
  1055 			break;
       
  1056 		case EQueryTypeEvent:
       
  1057 			iQueryBuf->AppendL( KEventId );
       
  1058 			break;
       
  1059 		default:
       
  1060 #ifdef _DEBUG
       
  1061             User::Panic( _L("MdSFCAII") , KErrMdEUnknownQueryType );
       
  1062 #endif
       
  1063 			User::Leave( KErrMdEUnknownQueryType );
       
  1064 		}
       
  1065 
       
  1066 	// " IN"
       
  1067 	iQueryBuf->AppendL( KIn );
       
  1068 	}
       
  1069 
       
  1070 
       
  1071 // ---------------------------------------------------------------------------
       
  1072 // AppendLogicConditionL        Help method for appending logic condition.
       
  1073 // ---------------------------------------------------------------------------
       
  1074 //
       
  1075 void CMdSFindSqlClause::AppendLogicConditionL(TBool aNegated, TAppendIn aAppendIdIn)
       
  1076 	{
       
  1077 	const TMdCLogicCondition& condition = TMdCLogicCondition::GetFromBufferL( 
       
  1078 			*iSerializedBuffer );
       
  1079 
       
  1080     // Add recursive all children.
       
  1081     for( TUint32 i = 0; i < condition.iChildConditions.iPtr.iCount; i++)
       
  1082 		{
       
  1083 		// get offset and move position to begin of child condition
       
  1084 		iSerializedBuffer->PositionL( condition.iChildConditions.iPtr.iOffset + 
       
  1085 				CMdCSerializationBuffer::KRequiredSizeForTUint32 * i );
       
  1086 
       
  1087 		TUint32 childOffset;
       
  1088 		iSerializedBuffer->ReceiveL( childOffset );
       
  1089 
       
  1090 		iSerializedBuffer->PositionL( childOffset );
       
  1091 
       
  1092 		// If not first add operator
       
  1093 		if( i > 0 )
       
  1094 			{
       
  1095 			switch( condition.iOperator )
       
  1096 				{
       
  1097 				case ELogicConditionOperatorAnd:
       
  1098 					iQueryBuf->AppendL( KAnd );
       
  1099 					break;
       
  1100 				case ELogicConditionOperatorOr:
       
  1101 					iQueryBuf->AppendL( KOr );
       
  1102 					break;
       
  1103 				default:
       
  1104 #ifdef _DEBUG
       
  1105 					User::Panic( _L("MdSFCALC") , KErrMdEUnknownConditionCompareMethod );
       
  1106 #endif
       
  1107 					User::Leave( KErrMdEUnknownConditionCompareMethod ); 
       
  1108 				}
       
  1109 			}
       
  1110 
       
  1111 		ConditionStatementL( aNegated, aAppendIdIn );
       
  1112 		}
       
  1113 	}
       
  1114 
       
  1115 // ---------------------------------------------------------------------------
       
  1116 // AppendObjectConditionL        Help method for appending object condition.
       
  1117 // ---------------------------------------------------------------------------
       
  1118 //
       
  1119 void CMdSFindSqlClause::AppendObjectConditionL(TBool aNegated)
       
  1120 	{
       
  1121 	const TMdCObjectCondition& condition = TMdCObjectCondition::GetFromBufferL( 
       
  1122 			*iSerializedBuffer );
       
  1123 
       
  1124 	// "SELECT ObjectId FROM BaseObject"namespace def ID" WHERE(...)"
       
  1125 	iQueryBuf->AppendL( KSelectObjectIdFromBaseObject );
       
  1126 	iQueryBuf->AppendL( iNamespaceIdDes );
       
  1127 	iQueryBuf->AppendL( KSpace );
       
  1128 	iQueryBuf->AppendL( KWhere );
       
  1129 
       
  1130 	iQueryBuf->AppendL( KLeftBracket );
       
  1131 
       
  1132 	// remove "deleted" objects from result
       
  1133 	iQueryBuf->AppendL( KGetNotFlagAnd );
       
  1134 	iVariables.AppendL( TColumn( EMdEObjectFlagRemoved ) );
       
  1135 	
       
  1136    if( iPlaceholdersOnly )
       
  1137         {
       
  1138         // Drop non-placeholder  objects
       
  1139         // "(Flags&?)AND "
       
  1140         iQueryBuf->AppendL( KGetFlagAnd );
       
  1141         iVariables.AppendL( TColumn( EMdEObjectFlagPlaceholder ) );
       
  1142         }
       
  1143 
       
  1144 	if( EUserLevelDeviceAccess == iUserLevel )
       
  1145 		{
       
  1146 		switch( condition.iConfidentialityLevel )
       
  1147 			{
       
  1148 			// Only non-confidential (confidential flag is not set)
       
  1149 			// "(NOT Flags&?)AND "
       
  1150 			case EObjectConditionLevelNormal:
       
  1151 				iQueryBuf->AppendL( KGetNotFlagAnd );
       
  1152 				iVariables.AppendL( TColumn( EMdEObjectFlagConfidential ) );
       
  1153 				break;
       
  1154 			// Only confidential (confidential flag is set)
       
  1155 			// "(Flags&?)AND "
       
  1156 			case EObjectConditionLevelConfidential:
       
  1157 				iQueryBuf->AppendL( KGetFlagAnd );
       
  1158 				iVariables.AppendL( TColumn( EMdEObjectFlagConfidential ) );
       
  1159 				break;
       
  1160 
       
  1161 			case EObjectConditionLevelIgnoreConfidentiality:
       
  1162 				// no implementation needed
       
  1163 				break;
       
  1164 
       
  1165 		    default:
       
  1166 		    	{
       
  1167 #ifdef _DEBUG
       
  1168 				User::Panic( _L("MdSFCAO1") , KErrMdEUnknownConfidentialityLevel );
       
  1169 #endif
       
  1170 		    	User::Leave( KErrMdEUnknownConfidentialityLevel );
       
  1171 		    	}
       
  1172 			}
       
  1173 		}
       
  1174 	else
       
  1175 		{
       
  1176 		// If client application doesn't have device capability limit to
       
  1177 		// only non-confidential (confidential flag is not set)
       
  1178 		// "(NOT Flags&?)AND "
       
  1179 		iQueryBuf->AppendL( KGetNotFlagAnd );
       
  1180 		iVariables.AppendL( TColumn( EMdEObjectFlagConfidential ) );
       
  1181 		}
       
  1182 
       
  1183 	if( ( condition.iFlags & EMdEObjectFlagNotPresent ) == EFalse )
       
  1184 		{
       
  1185 		// Not present flag is not set
       
  1186 		// "NOT(Flags&?)AND "
       
  1187 		iQueryBuf->AppendL( KGetNotFlagAnd );
       
  1188 		iVariables.AppendL( TColumn( EMdEObjectFlagNotPresent ) );
       
  1189 		}
       
  1190 
       
  1191 	if( ( condition.iFlags & EMdEObjectFlagPlaceholder ) )
       
  1192 		{
       
  1193 		// Placeholder flag is not set
       
  1194 		// "NOT(Flags&?)AND "
       
  1195 		iQueryBuf->AppendL( KGetNotFlagAnd );
       
  1196 		iVariables.AppendL( TColumn( EMdEObjectFlagPlaceholder ) );
       
  1197 		}
       
  1198 
       
  1199 	if( aNegated )
       
  1200 		{
       
  1201 		// "NOT "
       
  1202 		iQueryBuf->AppendL( KNotWithSpace );
       
  1203 		}
       
  1204 
       
  1205 	// move position to begin of compare condition
       
  1206 	iSerializedBuffer->PositionL( condition.iCondition );
       
  1207 
       
  1208 	switch( condition.iCompareMethod )
       
  1209 		{
       
  1210 		case EObjectConditionCompareNone:
       
  1211 			break;
       
  1212 		case EObjectConditionCompareId:
       
  1213 			{
       
  1214 			TItemId objectId;
       
  1215 			iSerializedBuffer->ReceiveL( objectId );
       
  1216 
       
  1217 			iQueryBuf->AppendL( KObjectIdEqual );
       
  1218 			iVariables.AppendL( TColumn( objectId ) );
       
  1219 			}
       
  1220 			break;
       
  1221 		case EObjectConditionCompareIds:
       
  1222 			{
       
  1223 			TInt32 objectIdCount;
       
  1224 			iSerializedBuffer->ReceiveL( objectIdCount );
       
  1225 
       
  1226 			if( objectIdCount == 1 )
       
  1227 				{
       
  1228 				TItemId objectId;
       
  1229 				iSerializedBuffer->ReceiveL( objectId );
       
  1230 
       
  1231 				iQueryBuf->AppendL( KObjectIdEqual );
       
  1232 				iVariables.AppendL( TColumn( objectId ) );
       
  1233 				}
       
  1234 			else if( objectIdCount > 1 )
       
  1235 				{
       
  1236 				iQueryBuf->AppendL( KObjectIdIn );
       
  1237 				iQueryBuf->AppendL( KLeftBracket );
       
  1238 
       
  1239 				TItemId objectId;
       
  1240 				iSerializedBuffer->ReceiveL( objectId );
       
  1241 
       
  1242 				iQueryBuf->AppendL( KVariable );
       
  1243 				iVariables.AppendL( TColumn( objectId ) );
       
  1244 
       
  1245 				for( TInt32 i = 1; i < objectIdCount; i++ )
       
  1246 					{
       
  1247 					iSerializedBuffer->ReceiveL( objectId );
       
  1248 
       
  1249 					iQueryBuf->AppendL( KCommaVariable );
       
  1250 					iVariables.AppendL( TColumn( objectId ) );
       
  1251 					}
       
  1252 
       
  1253 				iQueryBuf->AppendL( KRightBracket );
       
  1254 				}
       
  1255 			}
       
  1256 			break;
       
  1257 		case EObjectConditionCompareGuid:
       
  1258 			{
       
  1259 			TInt64 guidHigh;
       
  1260 			iSerializedBuffer->ReceiveL( guidHigh );			
       
  1261 			TInt64 guidLow;
       
  1262 			iSerializedBuffer->ReceiveL( guidLow );			
       
  1263 
       
  1264 			iQueryBuf->AppendL( KObjectGuidEqual );
       
  1265 			iVariables.AppendL( TColumn( guidHigh ) );
       
  1266 			iVariables.AppendL( TColumn( guidLow ) );
       
  1267 			}
       
  1268 			break;
       
  1269 		case EObjectConditionCompareObjectDef:
       
  1270 			{
       
  1271 			TDefId objectDefId;
       
  1272 			iSerializedBuffer->ReceiveL( objectDefId );
       
  1273 
       
  1274 			iQueryBuf->AppendL( KObjectDefIdEqual );
       
  1275 			iVariables.AppendL( TColumn( objectDefId ) );
       
  1276 			}
       
  1277 			break;
       
  1278 		case EObjectConditionCompareUsageCount:
       
  1279 			{
       
  1280 			iQueryBuf->AppendL( KObjectUsageCount );
       
  1281 			AppendRangeL( EUint32RangeValue );
       
  1282 			}
       
  1283 			break;
       
  1284 		case EObjectConditionCompareUri:
       
  1285 			{
       
  1286 			TPtrC16 uriPtr = iSerializedBuffer->ReceivePtr16L();
       
  1287 
       
  1288 			iQueryBuf->AppendL( KObjectUriEqual );
       
  1289 			iVariables.AppendL( TColumn( uriPtr ) );
       
  1290 			}
       
  1291 			break;
       
  1292 		case EObjectConditionCompareUriBeginsWith:
       
  1293 			{
       
  1294 			TPtrC16 uriPtr = iSerializedBuffer->ReceivePtr16L();
       
  1295 
       
  1296 			HBufC16* pattern = HBufC16::NewLC( uriPtr.Length() + 1 );  // prepare for uri%
       
  1297 
       
  1298 	        TPtr16 modPattern = pattern->Des();
       
  1299 
       
  1300 			// add wildcard after URI "uri%"
       
  1301 			modPattern.Append( uriPtr );
       
  1302 			modPattern.Append( KWildcard );
       
  1303 
       
  1304 			iVariables.AppendL( TColumn( pattern ) );
       
  1305 
       
  1306 			CleanupStack::Pop( pattern );
       
  1307 
       
  1308 			iQueryBuf->AppendL( KObjectUriBeginsWith );			
       
  1309 			}
       
  1310 			break;
       
  1311 		case EObjectConditionCompareFreeText:
       
  1312 			{
       
  1313 			TPtrC16 freetextPtr = iSerializedBuffer->ReceivePtr16L();
       
  1314 
       
  1315 			HBufC* freetext = freetextPtr.AllocLC();
       
  1316 			iFreeText.AppendL( freetext );
       
  1317 			CleanupStack::Pop( freetext );
       
  1318 
       
  1319 			iQueryBuf->AppendL( KObjectFreeTextSearchStart );
       
  1320 			iQueryBuf->AppendL( iNamespaceIdDes );
       
  1321 			iQueryBuf->AppendL( KObjectFreeTextSearchWhere );
       
  1322 			iQueryBuf->AppendL( iNamespaceIdDes );
       
  1323 			iQueryBuf->AppendL( KObjectFreeTextSearchDictionaryWhere );
       
  1324 			iQueryBuf->AppendL( KEqual );
       
  1325 			iQueryBuf->AppendL( KObjectFreeTextSearchEnd );
       
  1326 			iVariables.AppendL( TColumn( freetextPtr ) );
       
  1327 			}
       
  1328 			break;
       
  1329 		case EObjectConditionCompareFreeTextContains:
       
  1330 			{
       
  1331 			TPtrC16 freetextPtr = iSerializedBuffer->ReceivePtr16L();
       
  1332 
       
  1333 			HBufC* freetext = freetextPtr.AllocLC();
       
  1334 			iFreeText.AppendL( freetext );
       
  1335 			CleanupStack::Pop( freetext );
       
  1336 
       
  1337 			iQueryBuf->AppendL( KObjectFreeTextSearchStart );
       
  1338 			iQueryBuf->AppendL( iNamespaceIdDes );
       
  1339 			iQueryBuf->AppendL( KObjectFreeTextSearchWhere );
       
  1340 			iQueryBuf->AppendL( iNamespaceIdDes );
       
  1341 			iQueryBuf->AppendL( KObjectFreeTextSearchDictionaryWhere );
       
  1342 			
       
  1343 			HBufC16* pattern = HBufC16::NewLC( freetextPtr.Length() + 2 );  // prepare for %freetext%
       
  1344 
       
  1345 	        TPtr16 modPattern = pattern->Des();
       
  1346 
       
  1347 			// add wildcard before and after freetext "%freetext%"
       
  1348 			modPattern.Append( KWildcard );
       
  1349 			modPattern.Append( freetextPtr );
       
  1350 			modPattern.Append( KWildcard );
       
  1351 
       
  1352 			iVariables.AppendL( TColumn( pattern ) );
       
  1353 
       
  1354 			CleanupStack::Pop( pattern );
       
  1355 
       
  1356 			iQueryBuf->AppendL( KLike );
       
  1357 
       
  1358 			iQueryBuf->AppendL( KObjectFreeTextSearchEnd );
       
  1359 			}
       
  1360 			break;
       
  1361 		case EObjectConditionCompareFreeTextBeginsWith:
       
  1362 			{
       
  1363 			TPtrC16 freetextPtr = iSerializedBuffer->ReceivePtr16L();
       
  1364 
       
  1365 			HBufC* freetext = freetextPtr.AllocLC();
       
  1366 			iFreeText.AppendL( freetext );
       
  1367 			CleanupStack::Pop( freetext );
       
  1368 
       
  1369 			iQueryBuf->AppendL( KObjectFreeTextSearchStart );
       
  1370 			iQueryBuf->AppendL( iNamespaceIdDes );
       
  1371 			iQueryBuf->AppendL( KObjectFreeTextSearchWhere );
       
  1372 			iQueryBuf->AppendL( iNamespaceIdDes );
       
  1373 			iQueryBuf->AppendL( KObjectFreeTextSearchDictionaryWhere );
       
  1374 
       
  1375 			HBufC16* pattern = HBufC16::NewLC( freetextPtr.Length() + 1 );  // prepare for freetext%
       
  1376 
       
  1377 	        TPtr16 modPattern = pattern->Des();
       
  1378 
       
  1379 			// add wildcard after freetext "freetext%"
       
  1380 			modPattern.Append( freetextPtr );
       
  1381 			modPattern.Append( KWildcard );
       
  1382 
       
  1383 			iVariables.AppendL( TColumn( pattern ) );
       
  1384 
       
  1385 			CleanupStack::Pop( pattern );
       
  1386 
       
  1387 			iQueryBuf->AppendL( KLike );
       
  1388 
       
  1389 			iQueryBuf->AppendL( KObjectFreeTextSearchEnd );
       
  1390 			}
       
  1391 			break;
       
  1392 		case EObjectConditionCompareFreeTextEndsWith:
       
  1393 			{
       
  1394 			TPtrC16 freetextPtr = iSerializedBuffer->ReceivePtr16L();
       
  1395 
       
  1396 			HBufC* freetext = freetextPtr.AllocLC();
       
  1397 			iFreeText.AppendL( freetext );
       
  1398 			CleanupStack::Pop( freetext );
       
  1399 
       
  1400 			iQueryBuf->AppendL( KObjectFreeTextSearchStart );
       
  1401 			iQueryBuf->AppendL( iNamespaceIdDes );
       
  1402 			iQueryBuf->AppendL( KObjectFreeTextSearchWhere );
       
  1403 			iQueryBuf->AppendL( iNamespaceIdDes );
       
  1404 			iQueryBuf->AppendL( KObjectFreeTextSearchDictionaryWhere );
       
  1405 
       
  1406 			HBufC16* pattern = HBufC16::NewLC( freetextPtr.Length() + 1 );  // prepare for %freetext
       
  1407 
       
  1408 	        TPtr16 modPattern = pattern->Des();
       
  1409 
       
  1410 			// add wildcard before freetext "%freetext"
       
  1411 			modPattern.Append( KWildcard );
       
  1412 			modPattern.Append( freetextPtr );
       
  1413 
       
  1414 			iVariables.AppendL( TColumn( pattern ) );
       
  1415 
       
  1416 			CleanupStack::Pop( pattern );
       
  1417 
       
  1418 			iQueryBuf->AppendL( KLike );
       
  1419 
       
  1420 			iQueryBuf->AppendL( KObjectFreeTextSearchEnd );
       
  1421 			}
       
  1422 			break;
       
  1423 		default:
       
  1424 #ifdef _DEBUG
       
  1425 			User::Panic( _L("MdSFCAO2") , KErrMdEUnknownConditionCompareMethod );
       
  1426 #endif
       
  1427 			User::Leave( KErrMdEUnknownConditionCompareMethod );
       
  1428 		}
       
  1429 
       
  1430 	iQueryBuf->AppendL( KRightBracket );
       
  1431 	}
       
  1432 
       
  1433 // ---------------------------------------------------------------------------
       
  1434 // AppendRangeL                    Help method for appending range.
       
  1435 // ---------------------------------------------------------------------------
       
  1436 //
       
  1437 void CMdSFindSqlClause::AppendRangeL(TRangeValueType aRangeValueType)
       
  1438 	{
       
  1439 	TInt32 rangeTypeValue;
       
  1440 	iSerializedBuffer->ReceiveL( rangeTypeValue );
       
  1441 	const TMdERangeType rangeType = (TMdERangeType)rangeTypeValue;
       
  1442 
       
  1443 	TMdCValueUnion minValue;
       
  1444 	iSerializedBuffer->ReceiveL( minValue );
       
  1445 
       
  1446 	TMdCValueUnion maxValue;
       
  1447 	iSerializedBuffer->ReceiveL( maxValue );
       
  1448 
       
  1449 	TInt64 minIntegerValue = KMaxTInt64;
       
  1450 	TInt64 maxIntegerValue = 0;
       
  1451 
       
  1452 	switch( aRangeValueType )
       
  1453 		{
       
  1454 		case EInt32RangeValue:
       
  1455 			minIntegerValue = maxValue.iInt32;
       
  1456 			maxIntegerValue = maxValue.iInt32;
       
  1457 			break;
       
  1458 		case EUint32RangeValue:
       
  1459 			minIntegerValue = maxValue.iUint32;
       
  1460 			maxIntegerValue = maxValue.iUint32;
       
  1461 			break;
       
  1462 		case EInt64RangeValue:
       
  1463 			minIntegerValue = maxValue.iInt64;
       
  1464 			maxIntegerValue = maxValue.iInt64;
       
  1465 			break;
       
  1466 		default:		
       
  1467 		    break;
       
  1468 		}
       
  1469 	
       
  1470 	switch( rangeType )
       
  1471 		{
       
  1472 		case EMdERangeTypeAny:
       
  1473 			break;
       
  1474 		case EMdERangeTypeEqual:
       
  1475 			iQueryBuf->AppendL( KEqual );
       
  1476 			iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1477 			break;
       
  1478 		case EMdERangeTypeNotEqual:
       
  1479 			iQueryBuf->AppendL( KNotEqual );
       
  1480 			iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1481 			break;
       
  1482 		case EMdERangeTypeLess:
       
  1483 			iQueryBuf->AppendL( KLess );
       
  1484 			iVariables.AppendL( TColumn( maxIntegerValue ) );
       
  1485 			break;
       
  1486 		case EMdERangeTypeLessOrEqual:
       
  1487 			iQueryBuf->AppendL( KLessOrEqual );
       
  1488 			iVariables.AppendL( TColumn( maxIntegerValue ) );
       
  1489 			break;
       
  1490 		case EMdERangeTypeGreater:
       
  1491 			iQueryBuf->AppendL( KGreater );
       
  1492 			iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1493 			break;
       
  1494 		case EMdERangeTypeGreaterOrEqual:
       
  1495 			iQueryBuf->AppendL( KGreaterOrEqual );
       
  1496 			iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1497 			break;
       
  1498 		case EMdERangeTypeBetween:
       
  1499 			iQueryBuf->AppendL( KBetween );
       
  1500 			iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1501 			iVariables.AppendL( TColumn( maxIntegerValue ) );
       
  1502 			break;
       
  1503 		case EMdERangeTypeNotBetween:
       
  1504 			iQueryBuf->AppendL( KNotBetween );
       
  1505 			iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1506 			iVariables.AppendL( TColumn( maxIntegerValue ) );
       
  1507 			break;
       
  1508 		default:
       
  1509 #ifdef _DEBUG
       
  1510 			User::Panic( _L("MdSFCARa") , KErrMdEUnknownRangeType );
       
  1511 #endif
       
  1512 			User::Leave( KErrMdEUnknownRangeType );
       
  1513 		}
       
  1514 	}
       
  1515 
       
  1516 // ---------------------------------------------------------------------------
       
  1517 // AppendPropertyConditionL      Help method for appending property condition.
       
  1518 // ---------------------------------------------------------------------------
       
  1519 //
       
  1520 void CMdSFindSqlClause::AppendPropertyConditionL(TBool aNegated)
       
  1521 	{
       
  1522 	const TMdCPropertyCondition& condition = 
       
  1523 		TMdCPropertyCondition::GetFromBufferL( *iSerializedBuffer );
       
  1524 	
       
  1525 	// "SELECT ObjectId FROM "
       
  1526 	iQueryBuf->AppendL( KSelectObjectIdFrom );
       
  1527 
       
  1528 	TDefId objectDefId = condition.iObjectDefId;
       
  1529 
       
  1530 	// get object def ID for source object def if 
       
  1531 	// condition is not from "Object" def and 
       
  1532 	// (not multi object query or (multi object query and WHERE to search from 
       
  1533 	// doesn't match with WHAT to search))
       
  1534 	if( KBaseObjectDefId != objectDefId && ( !iSourceObjectDefs || 
       
  1535 			( iSourceObjectDefs && iObjectDef != iSourceObjectDef ) ) )
       
  1536 		{
       
  1537 		objectDefId = iSourceObjectDef->GetId();
       
  1538 		}
       
  1539 	AppendTableByObjectDefIdL( objectDefId );
       
  1540 
       
  1541 	// "WHERE(...)"
       
  1542 	iQueryBuf->AppendL( KSpace );
       
  1543 	iQueryBuf->AppendL( KWhere );
       
  1544 
       
  1545 	iQueryBuf->AppendL( KLeftBracket );
       
  1546 
       
  1547 	AppendColumnByPropertyDefIdL( condition.iPropertyDefId );
       
  1548 
       
  1549 	if( aNegated )
       
  1550 		{
       
  1551 		// If negated search objects without specified property
       
  1552 
       
  1553 		// " ISNULL"
       
  1554 		iQueryBuf->AppendL( KIsNull );
       
  1555 		}
       
  1556 	else
       
  1557 		{
       
  1558 		// " NOTNULL"
       
  1559 		iQueryBuf->AppendL( KNotNull );
       
  1560 		}
       
  1561 
       
  1562 	iQueryBuf->AppendL( KRightBracket );
       
  1563 	}
       
  1564 
       
  1565 // ---------------------------------------------------------------------------
       
  1566 // ObjectDefForPropertyCondition        Helper method for getting object 
       
  1567 //                                      definition for property condition.
       
  1568 // ---------------------------------------------------------------------------
       
  1569 //
       
  1570 TDefId CMdSFindSqlClause::ObjectDefForPropertyCondition(TDefId aObjectDefId)
       
  1571 	{
       
  1572 	// if object def is not base object def
       
  1573 	if( KBaseObjectDefId != aObjectDefId )
       
  1574 		{
       
  1575 		const CMdsObjectDef* parent = iSourceObjectDef->GetParent();
       
  1576 
       
  1577 		// check if property's object def belongs to 
       
  1578 		// any of source object def's parents
       
  1579 		while( parent )
       
  1580 			{
       
  1581 			// if property's object def belongs to 
       
  1582 			// any parent, use source object def's ID
       
  1583 			if( aObjectDefId == parent->GetId() )
       
  1584 				{
       
  1585 				return iSourceObjectDef->GetId();
       
  1586 				}
       
  1587 
       
  1588 			// get parent's parent
       
  1589 			parent = parent->GetParent();
       
  1590 			}
       
  1591 		}
       
  1592 
       
  1593 	return aObjectDefId;
       
  1594 	}
       
  1595 
       
  1596 // ---------------------------------------------------------------------------
       
  1597 // AppendPropertyRangeConditionL        Help method for appending property 
       
  1598 //                                      range condition.
       
  1599 // ---------------------------------------------------------------------------
       
  1600 //
       
  1601 void CMdSFindSqlClause::AppendPropertyRangeConditionL(TBool aNegated)
       
  1602 	{
       
  1603 	const TMdCPropertyCondition& condition = 
       
  1604 		TMdCPropertyCondition::GetFromBufferL( *iSerializedBuffer );
       
  1605 
       
  1606 	// "SELECT ObjectId FROM "
       
  1607 	iQueryBuf->AppendL( KSelectObjectIdFrom );
       
  1608 
       
  1609 	TDefId objectDefId = ObjectDefForPropertyCondition( 
       
  1610 			condition.iObjectDefId );
       
  1611 
       
  1612 	AppendTableByObjectDefIdL( objectDefId );
       
  1613 
       
  1614 	// "WHERE(...)"
       
  1615 	iQueryBuf->AppendL( KSpace );
       
  1616 	iQueryBuf->AppendL( KWhere );
       
  1617 
       
  1618 	iQueryBuf->AppendL( KLeftBracket );
       
  1619 
       
  1620 	if( aNegated )
       
  1621 		{
       
  1622 		// "NOT "
       
  1623 		iQueryBuf->AppendL( KNotWithSpace );
       
  1624 		}
       
  1625 
       
  1626 	AppendColumnByPropertyDefIdL( condition.iPropertyDefId, objectDefId );
       
  1627 
       
  1628 	// move position to begin of compare condition
       
  1629 	iSerializedBuffer->PositionL( condition.iCondition );
       
  1630 	
       
  1631 	TInt32 rangeTypeValue;
       
  1632 	iSerializedBuffer->ReceiveL( rangeTypeValue );
       
  1633 	const TMdERangeType rangeType = (TMdERangeType)rangeTypeValue;
       
  1634 
       
  1635 	TMdCValueUnion minValue;
       
  1636 	iSerializedBuffer->ReceiveL( minValue );
       
  1637 
       
  1638 	TMdCValueUnion maxValue;
       
  1639 	iSerializedBuffer->ReceiveL( maxValue );
       
  1640 
       
  1641 	TBool integerValue = ETrue;
       
  1642 	TInt64 minIntegerValue = 0;
       
  1643 	TInt64 maxIntegerValue = 0;
       
  1644 
       
  1645 	// get correct value and value's type
       
  1646 	switch( condition.iConditionType )
       
  1647 	{
       
  1648 		case EConditionTypePropertyIntRange:
       
  1649 			minIntegerValue = minValue.iInt32;
       
  1650 			maxIntegerValue = maxValue.iInt32;
       
  1651 			break;
       
  1652 		case EConditionTypePropertyUintRange:
       
  1653 			minIntegerValue = minValue.iUint32;
       
  1654 			maxIntegerValue = maxValue.iUint32;
       
  1655 			break;
       
  1656 		case EConditionTypePropertyInt64Range:
       
  1657 		case EConditionTypePropertyTimeRange:
       
  1658 			minIntegerValue = minValue.iInt64;
       
  1659 			maxIntegerValue = maxValue.iInt64;
       
  1660 			break;
       
  1661 		case EConditionTypePropertyRealRange:
       
  1662 			integerValue = EFalse;
       
  1663 			break;
       
  1664 		default:
       
  1665 #ifdef _DEBUG
       
  1666 			User::Panic( _L( "MdSFCAR1" ), KErrMdEUnknownConditionType );
       
  1667 #endif
       
  1668 			User::Leave( KErrMdEUnknownConditionType );
       
  1669 			break;
       
  1670 	};
       
  1671 	
       
  1672 	switch( rangeType )
       
  1673 		{
       
  1674 		case EMdERangeTypeAny:
       
  1675 			break;
       
  1676 		case EMdERangeTypeEqual:
       
  1677 			iQueryBuf->AppendL( KEqual );
       
  1678 			if( integerValue )
       
  1679 				{
       
  1680 				iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1681 				}
       
  1682 			else
       
  1683 				{
       
  1684 				iVariables.AppendL( TColumn( minValue.iReal ) );
       
  1685 				}
       
  1686 			break;
       
  1687 		case EMdERangeTypeNotEqual:
       
  1688 			iQueryBuf->AppendL( KNotEqual );
       
  1689 			if( integerValue )
       
  1690 				{
       
  1691 				iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1692 				}
       
  1693 			else
       
  1694 				{
       
  1695 				iVariables.AppendL( TColumn( minValue.iReal ) );
       
  1696 				}
       
  1697 
       
  1698 			break;
       
  1699 		case EMdERangeTypeLess:
       
  1700 			iQueryBuf->AppendL( KLess );
       
  1701 			if( integerValue )
       
  1702 				{
       
  1703 				iVariables.AppendL( TColumn( maxIntegerValue ) );
       
  1704 				}
       
  1705 			else
       
  1706 				{
       
  1707 				iVariables.AppendL( TColumn( maxValue.iReal ) );
       
  1708 				}
       
  1709 			break;
       
  1710 		case EMdERangeTypeLessOrEqual:
       
  1711 			iQueryBuf->AppendL( KLessOrEqual );
       
  1712 			if( integerValue )
       
  1713 				{
       
  1714 				iVariables.AppendL( TColumn( maxIntegerValue ) );
       
  1715 				}
       
  1716 			else
       
  1717 				{
       
  1718 				iVariables.AppendL( TColumn( maxValue.iReal ) );
       
  1719 				}
       
  1720 			break;
       
  1721 		case EMdERangeTypeGreater:
       
  1722 			iQueryBuf->AppendL( KGreater );
       
  1723 			if( integerValue )
       
  1724 				{
       
  1725 				iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1726 				}
       
  1727 			else
       
  1728 				{
       
  1729 				iVariables.AppendL( TColumn( minValue.iReal ) );
       
  1730 				}
       
  1731 			break;
       
  1732 		case EMdERangeTypeGreaterOrEqual:
       
  1733 			iQueryBuf->AppendL( KGreaterOrEqual );
       
  1734 			if( integerValue )
       
  1735 				{
       
  1736 				iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1737 				}
       
  1738 			else
       
  1739 				{
       
  1740 				iVariables.AppendL( TColumn( minValue.iReal ) );
       
  1741 				}
       
  1742 			break;
       
  1743 		case EMdERangeTypeBetween:
       
  1744 			iQueryBuf->AppendL( KBetween );
       
  1745 			if( integerValue )
       
  1746 				{
       
  1747 				iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1748 				iVariables.AppendL( TColumn( maxIntegerValue ) );
       
  1749 				}
       
  1750 			else
       
  1751 				{
       
  1752 				iVariables.AppendL( TColumn( minValue.iReal ) );
       
  1753 				iVariables.AppendL( TColumn( maxValue.iReal ) );
       
  1754 				}
       
  1755 			break;
       
  1756 		case EMdERangeTypeNotBetween:
       
  1757 			iQueryBuf->AppendL( KNotBetween );
       
  1758 			if( integerValue )
       
  1759 				{
       
  1760 				iVariables.AppendL( TColumn( minIntegerValue ) );
       
  1761 				iVariables.AppendL( TColumn( maxIntegerValue ) );
       
  1762 				}
       
  1763 			else
       
  1764 				{
       
  1765 				iVariables.AppendL( TColumn( minValue.iReal ) );
       
  1766 				iVariables.AppendL( TColumn( maxValue.iReal ) );
       
  1767 				}
       
  1768 			break;
       
  1769 		default:
       
  1770 #ifdef _DEBUG
       
  1771 			User::Panic( _L( "MdSFCAR2" ), KErrMdEUnknownRangeType );
       
  1772 #endif
       
  1773 			User::Leave( KErrMdEUnknownRangeType );
       
  1774 		}
       
  1775 
       
  1776 	iQueryBuf->AppendL( KRightBracket );
       
  1777 	}
       
  1778 
       
  1779 // ---------------------------------------------------------------------------
       
  1780 // AppendPropertyTextConditionL            Help method for appending property 
       
  1781 //                                         text condition.
       
  1782 // ---------------------------------------------------------------------------
       
  1783 //
       
  1784 void CMdSFindSqlClause::AppendPropertyTextConditionL(TBool aNegated)
       
  1785 	{
       
  1786 	const TMdCPropertyCondition& condition = 
       
  1787 		TMdCPropertyCondition::GetFromBufferL( *iSerializedBuffer );
       
  1788 
       
  1789 	// "SELECT ObjectId FROM "
       
  1790 	iQueryBuf->AppendL( KSelectObjectIdFrom );
       
  1791 
       
  1792 	TDefId objectDefId = ObjectDefForPropertyCondition( 
       
  1793 			condition.iObjectDefId );
       
  1794 
       
  1795 	AppendTableByObjectDefIdL( objectDefId );
       
  1796 
       
  1797 	// "WHERE(...)"
       
  1798 	iQueryBuf->AppendL( KSpace );
       
  1799 	iQueryBuf->AppendL( KWhere );
       
  1800 
       
  1801 	iQueryBuf->AppendL( KLeftBracket );
       
  1802 
       
  1803 	if( aNegated )
       
  1804 		{
       
  1805 		// "NOT "
       
  1806 		iQueryBuf->AppendL( KNotWithSpace );
       
  1807 		}
       
  1808 
       
  1809 	AppendColumnByPropertyDefIdL( condition.iPropertyDefId );
       
  1810 
       
  1811 	// move position to begin of compare condition
       
  1812 	iSerializedBuffer->PositionL( condition.iCondition );
       
  1813 
       
  1814 	TUint32 compareMethodValue;
       
  1815 	iSerializedBuffer->ReceiveL( compareMethodValue );
       
  1816 	const TTextPropertyConditionCompareMethod compareMethod = 
       
  1817 		(TTextPropertyConditionCompareMethod)compareMethodValue;
       
  1818 
       
  1819 	TPtrC16 textPtr = iSerializedBuffer->ReceivePtr16L();
       
  1820 
       
  1821 	switch( compareMethod )
       
  1822 		{
       
  1823 		case ETextPropertyConditionCompareEquals:
       
  1824 			iQueryBuf->AppendL( KEqual );
       
  1825 			iVariables.AppendL( TColumn( textPtr ) );
       
  1826 			break;
       
  1827 		case ETextPropertyConditionCompareContains:
       
  1828 			{
       
  1829 			HBufC16* pattern = HBufC16::NewLC( textPtr.Length() + 2 );  // prepare for %text%
       
  1830 
       
  1831 	        TPtr16 modPattern = pattern->Des();
       
  1832 
       
  1833 			// add wildcard before and after text "%text%"
       
  1834 			modPattern.Append( KWildcard );
       
  1835 			modPattern.Append( textPtr );
       
  1836 			modPattern.Append( KWildcard );
       
  1837 
       
  1838 			iVariables.AppendL( TColumn( pattern ) );
       
  1839 
       
  1840 			CleanupStack::Pop( pattern );
       
  1841 
       
  1842 			iQueryBuf->AppendL( KLike );
       
  1843 			}
       
  1844 			break;
       
  1845 		case ETextPropertyConditionCompareBeginsWith:
       
  1846 			{
       
  1847 			HBufC16* pattern = HBufC16::NewLC( textPtr.Length() + 1 );  // prepare for text%
       
  1848 
       
  1849 	        TPtr16 modPattern = pattern->Des();
       
  1850 
       
  1851 			// add wildcard after text "text%"
       
  1852 			modPattern.Append( textPtr );
       
  1853 			modPattern.Append( KWildcard );
       
  1854 
       
  1855 			iVariables.AppendL( TColumn( pattern ) );
       
  1856 
       
  1857 			CleanupStack::Pop( pattern );
       
  1858 
       
  1859 			iQueryBuf->AppendL( KLike );
       
  1860 			}
       
  1861 			break;
       
  1862 		case ETextPropertyConditionCompareEndsWith:
       
  1863 			{
       
  1864 			HBufC16* pattern = HBufC16::NewLC( textPtr.Length() + 1 );  // prepare for %text
       
  1865 
       
  1866 	        TPtr16 modPattern = pattern->Des();
       
  1867 
       
  1868 			// add wildcard before text "%text"
       
  1869 			modPattern.Append( KWildcard );
       
  1870 			modPattern.Append( textPtr );
       
  1871 
       
  1872 			iVariables.AppendL( TColumn( pattern ) );
       
  1873 
       
  1874 			CleanupStack::Pop( pattern );
       
  1875 
       
  1876 			iQueryBuf->AppendL( KLike );
       
  1877 			}
       
  1878 			break;
       
  1879 		default:
       
  1880 #ifdef _DEBUG
       
  1881 			User::Panic( _L( "MdSFCATC" ), KErrMdEUnknownConditionCompareMethod );
       
  1882 #endif
       
  1883 			User::Leave( KErrMdEUnknownConditionCompareMethod );
       
  1884 		}
       
  1885 
       
  1886 	iQueryBuf->AppendL( KRightBracket );
       
  1887 	}
       
  1888 
       
  1889 // ---------------------------------------------------------------------------
       
  1890 // AppendPropertyBoolConditionL            Help method for appending property 
       
  1891 //                                         bool condition.
       
  1892 // ---------------------------------------------------------------------------
       
  1893 //
       
  1894 void CMdSFindSqlClause::AppendPropertyBoolConditionL(TBool aNegated)
       
  1895 	{
       
  1896 	const TMdCPropertyCondition& condition = 
       
  1897 		TMdCPropertyCondition::GetFromBufferL( *iSerializedBuffer );
       
  1898 
       
  1899 	// "SELECT ObjectId FROM "
       
  1900 	iQueryBuf->AppendL( KSelectObjectIdFrom );
       
  1901 
       
  1902 	TDefId objectDefId = ObjectDefForPropertyCondition( 
       
  1903 			condition.iObjectDefId );
       
  1904 
       
  1905 	AppendTableByObjectDefIdL( objectDefId );
       
  1906 
       
  1907 	// "WHERE(...)"
       
  1908 	iQueryBuf->AppendL( KSpace );
       
  1909 	iQueryBuf->AppendL( KWhere );
       
  1910 
       
  1911 	iQueryBuf->AppendL( KLeftBracket );
       
  1912 
       
  1913 	if( aNegated )
       
  1914 		{
       
  1915 		// "NOT "
       
  1916 		iQueryBuf->AppendL( KNotWithSpace );
       
  1917 		}
       
  1918 
       
  1919 	AppendColumnByPropertyDefIdL( condition.iPropertyDefId );
       
  1920 
       
  1921 	// move position to begin of compare condition
       
  1922 	iSerializedBuffer->PositionL( condition.iCondition );
       
  1923 	
       
  1924 	TBool boolValue;
       
  1925 	iSerializedBuffer->ReceiveL( boolValue );
       
  1926 
       
  1927 	iQueryBuf->AppendL( KEqual );
       
  1928 	iVariables.AppendL( TColumn( boolValue ) );
       
  1929 
       
  1930 	iQueryBuf->AppendL( KRightBracket );
       
  1931 	}
       
  1932 
       
  1933 // ---------------------------------------------------------------------------
       
  1934 // AppendEventConditionL                   Help method for appending event 
       
  1935 //                                         condition for object query.
       
  1936 // ---------------------------------------------------------------------------
       
  1937 //
       
  1938 void CMdSFindSqlClause::AppendEventConditionL(TBool aNegated)
       
  1939 	{
       
  1940 	const TMdCEventCondition& condition = TMdCEventCondition::GetFromBufferL( 
       
  1941 			*iSerializedBuffer );
       
  1942 
       
  1943 	if( EQueryTypeEvent == iSearchCriteria->iQueryType )
       
  1944 		{
       
  1945 		// "SELECT EventId FROM Event"
       
  1946 		iQueryBuf->AppendL( KSelectEventIdFromEvent );
       
  1947 		}
       
  1948 	else
       
  1949 		{
       
  1950 		// "SELECT ObjectId FROM Event"
       
  1951 		iQueryBuf->AppendL( KSelectObjectIdFromEvent );
       
  1952 		}
       
  1953 
       
  1954 	// ""namespace def ID" WHERE"
       
  1955 	iQueryBuf->AppendL( iNamespaceIdDes );
       
  1956 	iQueryBuf->AppendL( KSpace ); 
       
  1957 	iQueryBuf->AppendL( KWhere );
       
  1958 
       
  1959 	// " NOT"
       
  1960 	if( aNegated )
       
  1961 		{
       
  1962 		iQueryBuf->AppendL( KSpaceWithNot );
       
  1963 		}
       
  1964 
       
  1965 	iQueryBuf->AppendL( KLeftBracket );
       
  1966 
       
  1967 	TBool firstEventCondition = ETrue;
       
  1968 
       
  1969 	if( condition.iEventId != KNoId )
       
  1970 		{
       
  1971 		firstEventCondition = EFalse;
       
  1972 
       
  1973 		// "EventId="eventId""
       
  1974 		iQueryBuf->AppendL( KEventIdEqual );
       
  1975 		iVariables.AppendL( TColumn( condition.iEventId ) );
       
  1976 		}
       
  1977 
       
  1978 	if( condition.iEventDefId != KNoDefId )
       
  1979 		{
       
  1980 		if( firstEventCondition == EFalse )
       
  1981 			{
       
  1982 			iQueryBuf->AppendL( KSpace );
       
  1983 			iQueryBuf->AppendL( KAnd );
       
  1984 			}
       
  1985 		firstEventCondition = EFalse;
       
  1986 
       
  1987 		// "EventDefId="eventDefId""
       
  1988 		iQueryBuf->AppendL( KEventDefIdEqual );
       
  1989 		iVariables.AppendL( TColumn( condition.iEventDefId ) );
       
  1990 		}
       
  1991 
       
  1992 	if( condition.iCreationTimeRange != KNoOffset )
       
  1993 		{
       
  1994 		if( firstEventCondition == EFalse )
       
  1995 			{
       
  1996 			iQueryBuf->AppendL( KSpace );
       
  1997 			iQueryBuf->AppendL( KAnd );
       
  1998 			}
       
  1999 		firstEventCondition = EFalse;
       
  2000 
       
  2001 		// "TimeStamp"
       
  2002 		iQueryBuf->AppendL( KEventTimeStamp );
       
  2003 		iSerializedBuffer->PositionL( condition.iCreationTimeRange );		
       
  2004 		AppendRangeL( EInt64RangeValue );
       
  2005 		}
       
  2006 
       
  2007 	if( condition.iObjectCondition != KNoOffset )
       
  2008 		{
       
  2009 		if( firstEventCondition == EFalse )
       
  2010 			{
       
  2011 			iQueryBuf->AppendL( KSpace );
       
  2012 			iQueryBuf->AppendL( KAnd );
       
  2013 			}
       
  2014 		firstEventCondition = EFalse;
       
  2015 
       
  2016 		iQueryBuf->AppendL( KEventObjectIdIn );
       
  2017 		iSerializedBuffer->PositionL( condition.iObjectCondition );		
       
  2018 		iQueryBuf->AppendL( KLeftBracket );
       
  2019 		ConditionStatementL( EFalse, EAppendInFalse );
       
  2020 		iQueryBuf->AppendL( KRightBracket );
       
  2021 		}
       
  2022 
       
  2023 	if( condition.iSourceCondition != KNoOffset )
       
  2024 		{
       
  2025 		if( firstEventCondition == EFalse )
       
  2026 			{
       
  2027 			iQueryBuf->AppendL( KSpace );
       
  2028 			iQueryBuf->AppendL( KAnd );
       
  2029 			}
       
  2030 		firstEventCondition = EFalse;
       
  2031 
       
  2032 		iSerializedBuffer->PositionL( condition.iSourceCondition );		
       
  2033 		ConditionStatementL( EFalse, EAppendInFalse );
       
  2034 		}
       
  2035 
       
  2036 	if( condition.iParticipantCondition != KNoOffset )
       
  2037 		{
       
  2038 		if( firstEventCondition == EFalse )
       
  2039 			{
       
  2040 			iQueryBuf->AppendL( KSpace );
       
  2041 			iQueryBuf->AppendL( KAnd );
       
  2042 			}
       
  2043 		firstEventCondition = EFalse;
       
  2044 
       
  2045 		iSerializedBuffer->PositionL( condition.iParticipantCondition );		
       
  2046 		ConditionStatementL( EFalse, EAppendInFalse );
       
  2047 		}
       
  2048 
       
  2049 	// check event condition has URI comparsion
       
  2050 	if( EEventConditionCompareNone != condition.iCompareMethod
       
  2051 			&& KNoOffset != condition.iUriCondition )
       
  2052 		{
       
  2053 		if( firstEventCondition == EFalse )
       
  2054 			{
       
  2055 			iQueryBuf->AppendL( KSpace );
       
  2056 			iQueryBuf->AppendL( KAnd );
       
  2057 			}
       
  2058 		firstEventCondition = EFalse;
       
  2059 
       
  2060 		switch( condition.iCompareMethod )
       
  2061 			{
       
  2062 			case EEventConditionCompareSourceURI:
       
  2063 	        	// "Source="URI""
       
  2064 	        	iQueryBuf->AppendL( KEventSourceEqual );
       
  2065 	        	break;
       
  2066 
       
  2067 	        case EEventConditionCompareParticipantURI:
       
  2068 	        	// "Participant="URI""
       
  2069 		        iQueryBuf->AppendL( KEventParticipantEqual );
       
  2070 				break;
       
  2071 
       
  2072 	        default:
       
  2073 #ifdef _DEBUG
       
  2074 	        	User::Panic( _L( "MdSFCAEC" ), KErrMdEUnknownConditionCompareMethod );
       
  2075 #endif
       
  2076 	        	User::Leave( KErrMdEUnknownConditionCompareMethod );
       
  2077 			}
       
  2078 
       
  2079 		iSerializedBuffer->PositionL( condition.iUriCondition );
       
  2080 		TPtrC16 eventUri = iSerializedBuffer->ReceivePtr16L();
       
  2081 		iVariables.AppendL( TColumn( eventUri ) );		
       
  2082 		}
       
  2083 
       
  2084 	iQueryBuf->AppendL( KRightBracket );
       
  2085 	}
       
  2086 
       
  2087 // ---------------------------------------------------------------------------
       
  2088 // AppendRelationConditionL                Help method for appending relation 
       
  2089 //                                         condition for object query.
       
  2090 // ---------------------------------------------------------------------------
       
  2091 //
       
  2092 void CMdSFindSqlClause::AppendRelationConditionL(TBool aNegated)
       
  2093 	{
       
  2094 	const TMdCRelationCondition& condition = 
       
  2095 			TMdCRelationCondition::GetFromBufferL( *iSerializedBuffer );
       
  2096 
       
  2097 	if( ( ERelationConditionSideLeft == condition.iObjectSide ) || 
       
  2098 		( ERelationConditionSideRight == condition.iObjectSide ) )
       
  2099 		{
       
  2100 		if( EQueryTypeRelation == iSearchCriteria->iQueryType )
       
  2101 			{
       
  2102 			// "SELECT RelationId FROM Relations"
       
  2103 			iQueryBuf->AppendL( KSelectRelationIdFromRelation );
       
  2104 			}
       
  2105 		else
       
  2106 			{
       
  2107 			if( ERelationConditionSideLeft == condition.iObjectSide )
       
  2108 				{
       
  2109 				// "SELECT LeftObjectId FROM Relations"
       
  2110 				iQueryBuf->AppendL( KSelectLeftObjectIdFromRelation );
       
  2111 				}
       
  2112 			else
       
  2113 				{
       
  2114 				// "SELECT RightObjectId FROM Relations"
       
  2115 				iQueryBuf->AppendL( KSelectRightObjectIdFromRelation );
       
  2116 				}
       
  2117 			}
       
  2118 
       
  2119 		// ""namespace def ID" "
       
  2120 		iQueryBuf->AppendL( iNamespaceIdDes );
       
  2121 		iQueryBuf->AppendL( KSpace );
       
  2122 		
       
  2123 		// "WHERE(NOT Flags&?)"
       
  2124 		iQueryBuf->AppendL( KWhereNotFlag );
       
  2125 		iVariables.AppendL( TColumn( 
       
  2126 				EMdERelationFlagNotPresent | 
       
  2127 				EMdERelationFlagDeleted | 
       
  2128 				EMdERelationFlagGarbageDeleted ) );
       
  2129 
       
  2130 		if( condition.iLeftObjectCondition != KNoOffset )
       
  2131 			{
       
  2132 			// "AND "
       
  2133 			iQueryBuf->AppendL( KAnd );
       
  2134 
       
  2135 			// "NOT"
       
  2136 			if( aNegated )
       
  2137 			{
       
  2138 				iQueryBuf->AppendL( KNot );
       
  2139 			}
       
  2140 
       
  2141 			iQueryBuf->AppendL( KLeftBracket );
       
  2142 
       
  2143 			iQueryBuf->AppendL( KLeftBracket );
       
  2144 			// append relation condition and left object condition
       
  2145 			AppendRelationSideConditionL( ETrue, condition, 
       
  2146 					ETrue, condition.iLeftObjectCondition );
       
  2147 			iQueryBuf->AppendL( KRightBracket );
       
  2148 
       
  2149 			// append right object condition if it exists
       
  2150 			if( condition.iRightObjectCondition != KNoOffset )
       
  2151 				{
       
  2152 				// "AND "
       
  2153 				iQueryBuf->AppendL( KAnd );
       
  2154 
       
  2155 				iQueryBuf->AppendL( KLeftBracket );
       
  2156 				// append only right object condition
       
  2157 				AppendRelationSideConditionL( EFalse, condition, 
       
  2158 						EFalse, condition.iRightObjectCondition );
       
  2159 				iQueryBuf->AppendL( KRightBracket );
       
  2160 				}
       
  2161 
       
  2162 			iQueryBuf->AppendL( KRightBracket );
       
  2163 			}
       
  2164 		else
       
  2165 			{
       
  2166 			// append right object condition if it exists
       
  2167 			if( condition.iRightObjectCondition != KNoOffset )
       
  2168 				{
       
  2169 				// "AND "
       
  2170 				iQueryBuf->AppendL( KAnd );
       
  2171 				
       
  2172 				// "NOT"
       
  2173 				if( aNegated )
       
  2174 				{
       
  2175 					iQueryBuf->AppendL( KNot );
       
  2176 				}
       
  2177 
       
  2178 				iQueryBuf->AppendL( KLeftBracket );
       
  2179 				// append relation condition and right object condition
       
  2180 				AppendRelationSideConditionL( ETrue, condition, 
       
  2181 						EFalse, condition.iRightObjectCondition );
       
  2182 				iQueryBuf->AppendL( KRightBracket );
       
  2183 				}
       
  2184 			}
       
  2185 		}
       
  2186 	else if( ERelationConditionSideEither == condition.iObjectSide )
       
  2187 		{
       
  2188 		// relation query
       
  2189 		if( EQueryTypeRelation == iSearchCriteria->iQueryType )
       
  2190 			{
       
  2191 			// "SELECT RelationId FROM Relations"namespace def ID" "
       
  2192 			iQueryBuf->AppendL( KSelectRelationIdFromRelation );
       
  2193 			iQueryBuf->AppendL( iNamespaceIdDes );
       
  2194 			iQueryBuf->AppendL( KSpace );
       
  2195 		
       
  2196 			// "WHERE(NOT Flags&?)"
       
  2197 			iQueryBuf->AppendL( KWhereNotFlag );
       
  2198 			iVariables.AppendL( TColumn( 
       
  2199 					EMdERelationFlagNotPresent | 
       
  2200 					EMdERelationFlagDeleted | 
       
  2201 					EMdERelationFlagGarbageDeleted ) );
       
  2202 			
       
  2203 			// append left object condition if it exists
       
  2204 			if( condition.iLeftObjectCondition != KNoOffset )
       
  2205 				{
       
  2206 				// "AND "
       
  2207 				iQueryBuf->AppendL( KAnd );
       
  2208 
       
  2209 				// "NOT"
       
  2210 				if( aNegated )
       
  2211 					{
       
  2212 					iQueryBuf->AppendL( KNot );
       
  2213 					}
       
  2214 
       
  2215 				iQueryBuf->AppendL( KLeftBracket );
       
  2216 
       
  2217 				iQueryBuf->AppendL( KLeftBracket );
       
  2218 				// append relation condition and left object condition
       
  2219 				AppendRelationSideConditionL( ETrue, condition, 
       
  2220 						ETrue, condition.iLeftObjectCondition );
       
  2221 				iQueryBuf->AppendL( KRightBracket );
       
  2222 
       
  2223 				// append right object condition if it exists
       
  2224 				if( condition.iRightObjectCondition != KNoOffset )
       
  2225 					{
       
  2226 					// "OR "
       
  2227 					iQueryBuf->AppendL( KOr );
       
  2228 
       
  2229 					iQueryBuf->AppendL( KLeftBracket );
       
  2230 					// append only right object condition
       
  2231 					AppendRelationSideConditionL( EFalse, condition, 
       
  2232 							EFalse, condition.iRightObjectCondition );
       
  2233 					iQueryBuf->AppendL( KRightBracket );
       
  2234 					}
       
  2235 
       
  2236 				iQueryBuf->AppendL( KRightBracket );
       
  2237 				}
       
  2238 			// append right object condition if it exists
       
  2239 			else if( condition.iRightObjectCondition != KNoOffset )
       
  2240 				{
       
  2241 				// "AND "
       
  2242 				iQueryBuf->AppendL( KAnd );
       
  2243 
       
  2244 				// "NOT"
       
  2245 				if( aNegated )
       
  2246 					{
       
  2247 					iQueryBuf->AppendL( KNot );
       
  2248 					}
       
  2249 
       
  2250 				iQueryBuf->AppendL( KLeftBracket );
       
  2251 				// append relation condition and right object condition
       
  2252 				AppendRelationSideConditionL( ETrue, condition, 
       
  2253 						EFalse, condition.iRightObjectCondition );
       
  2254 				iQueryBuf->AppendL( KRightBracket );
       
  2255 				}
       
  2256 			// append only relation conditions
       
  2257 			else 
       
  2258 				{
       
  2259 				// "AND "
       
  2260 				iQueryBuf->AppendL( KAnd );
       
  2261 
       
  2262 				// "NOT"
       
  2263 				if( aNegated )
       
  2264 					{
       
  2265 					iQueryBuf->AppendL( KNot );
       
  2266 					}
       
  2267 
       
  2268 				iQueryBuf->AppendL( KLeftBracket );
       
  2269 				// append relation conditions
       
  2270 				AppendRelationSideConditionL( ETrue, condition, 
       
  2271 						EFalse, KNoOffset ); // no object condition
       
  2272 				iQueryBuf->AppendL( KRightBracket );
       
  2273 				}
       
  2274 			}
       
  2275 		// object or event query
       
  2276 		else
       
  2277 			{
       
  2278 			if( condition.iLeftObjectCondition != KNoOffset )
       
  2279 				{
       
  2280 				// "SELECT LeftObjectId FROM Relations"namespace def ID" "
       
  2281 				iQueryBuf->AppendL( KSelectLeftObjectIdFromRelation );
       
  2282 				iQueryBuf->AppendL( iNamespaceIdDes );
       
  2283 				iQueryBuf->AppendL( KSpace );
       
  2284 
       
  2285 				// "WHERE(NOT Flags&?)"
       
  2286 				iQueryBuf->AppendL( KWhereNotFlag );
       
  2287 				iVariables.AppendL( TColumn( 
       
  2288 						EMdERelationFlagNotPresent | 
       
  2289 						EMdERelationFlagDeleted | 
       
  2290 						EMdERelationFlagGarbageDeleted ) );
       
  2291 
       
  2292 				// "AND "
       
  2293 				iQueryBuf->AppendL( KAnd );
       
  2294 				
       
  2295 				// "NOT"
       
  2296 				if( aNegated )
       
  2297 					{
       
  2298 					iQueryBuf->AppendL( KNot );
       
  2299 					}
       
  2300 
       
  2301 				iQueryBuf->AppendL( KLeftBracket );
       
  2302 				AppendRelationSideConditionL( ETrue, condition, 
       
  2303 						ETrue, condition.iLeftObjectCondition );
       
  2304 				iQueryBuf->AppendL( KRightBracket );
       
  2305 
       
  2306 				if( condition.iRightObjectCondition != KNoOffset )
       
  2307 					{
       
  2308 					// " UNION "
       
  2309 					iQueryBuf->AppendL( KUnion );
       
  2310 
       
  2311 					// "SELECT RightObjectId FROM Relations"namespace def ID" "
       
  2312 					iQueryBuf->AppendL( KSelectRightObjectIdFromRelation );
       
  2313 					iQueryBuf->AppendL( iNamespaceIdDes );
       
  2314 					iQueryBuf->AppendL( KSpace );
       
  2315 					
       
  2316 					// "WHERE(NOT Flags&?)"
       
  2317 					iQueryBuf->AppendL( KWhereNotFlag );
       
  2318 					iVariables.AppendL( TColumn( 
       
  2319 							EMdERelationFlagNotPresent | 
       
  2320 							EMdERelationFlagDeleted | 
       
  2321 							EMdERelationFlagGarbageDeleted ) );
       
  2322 
       
  2323 					// "AND "
       
  2324 					iQueryBuf->AppendL( KAnd );
       
  2325 					
       
  2326 					// "NOT"
       
  2327 					if( aNegated )
       
  2328 						{
       
  2329 						iQueryBuf->AppendL( KNot );
       
  2330 						}
       
  2331 
       
  2332 					iQueryBuf->AppendL( KLeftBracket );
       
  2333 					AppendRelationSideConditionL( ETrue, condition, 
       
  2334 							EFalse, condition.iRightObjectCondition );
       
  2335 					iQueryBuf->AppendL( KRightBracket );
       
  2336 					}
       
  2337 				}
       
  2338 			else
       
  2339 				{
       
  2340 				// "SELECT RightObjectId FROM Relations"namespace def ID" "
       
  2341 				iQueryBuf->AppendL( KSelectRightObjectIdFromRelation );
       
  2342 				iQueryBuf->AppendL( iNamespaceIdDes );
       
  2343 				iQueryBuf->AppendL( KSpace );
       
  2344 
       
  2345 				// "WHERE(NOT Flags&?)"
       
  2346 				iQueryBuf->AppendL( KWhereNotFlag );
       
  2347 				iVariables.AppendL( TColumn( 
       
  2348 						EMdERelationFlagNotPresent | 
       
  2349 						EMdERelationFlagDeleted | 
       
  2350 						EMdERelationFlagGarbageDeleted ) );
       
  2351 
       
  2352 				// "AND "
       
  2353 				iQueryBuf->AppendL( KAnd );
       
  2354 				
       
  2355 				// "NOT"
       
  2356 				if( aNegated )
       
  2357 					{
       
  2358 					iQueryBuf->AppendL( KNot );
       
  2359 					}
       
  2360 
       
  2361 				iQueryBuf->AppendL( KLeftBracket );
       
  2362 				AppendRelationSideConditionL( ETrue, condition, 
       
  2363 						EFalse, condition.iRightObjectCondition );
       
  2364 				iQueryBuf->AppendL( KRightBracket );
       
  2365 				}
       
  2366 			}
       
  2367 		}
       
  2368 	else
       
  2369 		{
       
  2370 		// unknown relation condition side
       
  2371 #ifdef _DEBUG
       
  2372 		User::Panic( _L("MdSFCARC") , KErrCorrupt );
       
  2373 #endif
       
  2374 		User::Leave( KErrCorrupt );
       
  2375 		}
       
  2376 	}
       
  2377 
       
  2378 void CMdSFindSqlClause::AppendRelationSideConditionL(
       
  2379 		TBool aRelationConditions, 
       
  2380 		const TMdCRelationCondition& aRelationCondition, 
       
  2381 		TBool aLeftSide, TUint32 aRelationObjectConditionOffset)
       
  2382 	{
       
  2383 	TBool firstEventCondition = ETrue;
       
  2384 
       
  2385 	if( aRelationConditions )
       
  2386 		{
       
  2387 		if( aRelationCondition.iRelationId != KNoId )
       
  2388 			{
       
  2389 			firstEventCondition = EFalse;
       
  2390 	
       
  2391 			// "RelationId="relationID""
       
  2392 			iQueryBuf->AppendL( KRelationIdEqual );
       
  2393 			iVariables.AppendL( TColumn( aRelationCondition.iRelationId ) );
       
  2394 			}
       
  2395 	
       
  2396 		if( aRelationCondition.iRelationIds.iPtr.iCount > 0 && 
       
  2397 				aRelationCondition.iRelationIds.iPtr.iOffset != KNoOffset )
       
  2398 			{
       
  2399 			if( firstEventCondition == EFalse )
       
  2400 				{
       
  2401 				iQueryBuf->AppendL( KSpace );
       
  2402 				iQueryBuf->AppendL( KAnd );
       
  2403 				}
       
  2404 			firstEventCondition = EFalse;
       
  2405 	
       
  2406 			// "RelationId IN("relationIDs")"
       
  2407 			iQueryBuf->AppendL( KRelationIdIn );
       
  2408 	
       
  2409 			iQueryBuf->AppendL( KLeftBracket );
       
  2410 	
       
  2411 			iSerializedBuffer->PositionL( 
       
  2412 					aRelationCondition.iRelationIds.iPtr.iOffset );
       
  2413 	
       
  2414 			TItemId relationId;
       
  2415 			iSerializedBuffer->ReceiveL( relationId );
       
  2416 			iQueryBuf->AppendL( KVariable );
       
  2417 			iVariables.AppendL( TColumn( relationId ) );
       
  2418 	
       
  2419 			for( TInt32 i = 1; i < aRelationCondition.iRelationIds.iPtr.iCount; i++ )
       
  2420 				{
       
  2421 				iSerializedBuffer->ReceiveL( relationId );
       
  2422 				iQueryBuf->AppendL( KCommaVariable );
       
  2423 				iVariables.AppendL( TColumn( relationId ) );
       
  2424 				}
       
  2425 	
       
  2426 			iQueryBuf->AppendL( KRightBracket );
       
  2427 			}
       
  2428 		
       
  2429 		if( aRelationCondition.iRelationDefId != KNoDefId )
       
  2430 			{
       
  2431 			if( firstEventCondition == EFalse )
       
  2432 				{
       
  2433 				iQueryBuf->AppendL( KSpace );
       
  2434 				iQueryBuf->AppendL( KAnd );
       
  2435 				}
       
  2436 			firstEventCondition = EFalse;
       
  2437 	
       
  2438 			// "RelationDefId="relationDefId""
       
  2439 			iQueryBuf->AppendL( KRelationDefIdEqual );
       
  2440 			iVariables.AppendL( TColumn( aRelationCondition.iRelationDefId ) );
       
  2441 			}
       
  2442 	
       
  2443 		if( aRelationCondition.iGuid != KNoOffset )
       
  2444 			{
       
  2445 			if( firstEventCondition == EFalse )
       
  2446 				{
       
  2447 				iQueryBuf->AppendL( KSpace );
       
  2448 				iQueryBuf->AppendL( KAnd );
       
  2449 				}
       
  2450 			firstEventCondition = EFalse;
       
  2451 	
       
  2452 			iSerializedBuffer->PositionL( aRelationCondition.iGuid );
       
  2453 			
       
  2454 			TInt64 guidHigh;
       
  2455 			iSerializedBuffer->ReceiveL( guidHigh );
       
  2456 			TInt64 guidLow;
       
  2457 			iSerializedBuffer->ReceiveL( guidLow );
       
  2458 			
       
  2459 			// "RelationGuidHigh="relationGuidHigh" AND 
       
  2460 			// RelationGuidLow="relationGuidLow""
       
  2461 			iQueryBuf->AppendL( KRelationGuidEqual );
       
  2462 			iVariables.AppendL( TColumn( guidHigh ) );
       
  2463 			iVariables.AppendL( TColumn( guidLow ) );
       
  2464 			}
       
  2465 	
       
  2466 		if( aRelationCondition.iParameterRange != KNoOffset )
       
  2467 			{
       
  2468 			if( firstEventCondition == EFalse )
       
  2469 				{
       
  2470 				iQueryBuf->AppendL( KSpace );
       
  2471 				iQueryBuf->AppendL( KAnd );
       
  2472 				}
       
  2473 			firstEventCondition = EFalse;
       
  2474 	
       
  2475 			// "Parameter"
       
  2476 			iQueryBuf->AppendL( KRelationParameter );
       
  2477 			iSerializedBuffer->PositionL( aRelationCondition.iParameterRange );
       
  2478 			AppendRangeL( EInt32RangeValue );
       
  2479 			}
       
  2480 	
       
  2481 		if( aRelationCondition.iLastModifiedDateRange != KNoOffset )
       
  2482 			{
       
  2483 			if( firstEventCondition == EFalse )
       
  2484 				{
       
  2485 				iQueryBuf->AppendL( KSpace );
       
  2486 				iQueryBuf->AppendL( KAnd );
       
  2487 				}
       
  2488 			firstEventCondition = EFalse;
       
  2489 	
       
  2490 			// "LastModifiedDate"
       
  2491 			iQueryBuf->AppendL( KRelationLastModifiedDate );
       
  2492 			iSerializedBuffer->PositionL( aRelationCondition.iLastModifiedDateRange );		
       
  2493 			AppendRangeL( EInt64RangeValue );
       
  2494 			}
       
  2495 		}
       
  2496 
       
  2497 	// object condition on left or right side
       
  2498 	if( aRelationObjectConditionOffset != KNoOffset )
       
  2499 		{
       
  2500 		if( firstEventCondition == EFalse )
       
  2501 			{
       
  2502 			iQueryBuf->AppendL( KSpace );
       
  2503 			iQueryBuf->AppendL( KAnd );
       
  2504 			}
       
  2505 		firstEventCondition = EFalse;
       
  2506 
       
  2507 		iSerializedBuffer->PositionL( aRelationObjectConditionOffset );
       
  2508 
       
  2509 		if ( aLeftSide )
       
  2510 			{
       
  2511 			ConditionStatementL( EFalse, EAppendInRelationLeft );
       
  2512 			}
       
  2513 		else
       
  2514 			{
       
  2515 			ConditionStatementL( EFalse, EAppendInRelationRight );
       
  2516 			}
       
  2517 		}
       
  2518 
       
  2519 	// if there was no any condition add "empty" true condition
       
  2520 	if( firstEventCondition )
       
  2521 		{
       
  2522 		iQueryBuf->AppendL( KEmptyTrue );
       
  2523 		}
       
  2524 	}
       
  2525 
       
  2526 // ---------------------------------------------------------------------------
       
  2527 // AppendTableByObjectDefIdL               Help method for searching object 
       
  2528 //                                         definition's table, based on 
       
  2529 //                                         object definition's ID, and 
       
  2530 //                                         appending those to buffer.
       
  2531 // ---------------------------------------------------------------------------
       
  2532 //
       
  2533 void CMdSFindSqlClause::AppendTableByObjectDefIdL(TDefId aObjectDefId)
       
  2534 	{
       
  2535 	const CMdsObjectDef* objectDef = iNamespaceDef->GetObjectByIdL( aObjectDefId );
       
  2536 
       
  2537 	if ( !objectDef )
       
  2538 		{
       
  2539 #ifdef _DEBUG
       
  2540 		User::Panic( _L("MdSFCAT1") , KErrMdEUnknownObjectDef );
       
  2541 #endif
       
  2542 		User::Leave( KErrMdEUnknownObjectDef );
       
  2543 		}
       
  2544 
       
  2545 	iQueryBuf->AppendL( objectDef->GetName() );
       
  2546 	iQueryBuf->AppendL( iNamespaceIdDes );
       
  2547 	}
       
  2548 
       
  2549 // ---------------------------------------------------------------------------
       
  2550 // AppendColumnByPropertyDefIdL            Help method for searching property 
       
  2551 //                                         definition's column, based on 
       
  2552 //                                         property definition's ID, and 
       
  2553 //                                         appending those to buffer.
       
  2554 // ---------------------------------------------------------------------------
       
  2555 //
       
  2556 void CMdSFindSqlClause::AppendColumnByPropertyDefIdL(TDefId aPropertyDefId, 
       
  2557 		TDefId aObjectDefId, TBool aResult)
       
  2558 	{
       
  2559 	CMdsPropertyDef* propDef = NULL;
       
  2560 
       
  2561 	if( aObjectDefId == KNoDefId )
       
  2562 		{
       
  2563 		if ( !iObjectDef )
       
  2564 			{
       
  2565 			const CMdsObjectDef* baseObjectDef = iNamespaceDef->GetObjectByIdL(
       
  2566 					KBaseObjectDefId );
       
  2567 	
       
  2568 			// base object definition must always exist
       
  2569 			if ( !baseObjectDef )
       
  2570 				{
       
  2571 #ifdef _DEBUG
       
  2572 				User::Panic( _L("MdSFCAC1") , KErrMdEUnknownObjectDef );
       
  2573 #endif				
       
  2574 				User::Leave( KErrMdEUnknownObjectDef );
       
  2575 				}
       
  2576 
       
  2577 			propDef = CONST_CAST( CMdsPropertyDef*, 
       
  2578 					baseObjectDef->GetPropertyByIdL( aPropertyDefId ) );
       
  2579 
       
  2580 			// if property definition doesn't exist in base object definition 
       
  2581 			// search from whole namespace
       
  2582 			if ( !propDef )
       
  2583 				{
       
  2584 				propDef = CONST_CAST( CMdsPropertyDef*, 
       
  2585 						iNamespaceDef->GetPropertyL( aPropertyDefId ) );
       
  2586 				}
       
  2587 			}
       
  2588 		else
       
  2589 			{
       
  2590 			propDef = CONST_CAST( CMdsPropertyDef*, 
       
  2591 					iObjectDef->GetPropertyByIdL( aPropertyDefId ) );
       
  2592 			}
       
  2593 		}
       
  2594 	else
       
  2595 		{
       
  2596 		const CMdsObjectDef* objDef = iNamespaceDef->GetObjectByIdL( 
       
  2597 				aObjectDefId );
       
  2598 		propDef = CONST_CAST( CMdsPropertyDef*, 
       
  2599 				objDef->GetPropertyByIdL( aPropertyDefId ) );
       
  2600 		}
       
  2601 
       
  2602 	if( propDef )
       
  2603 		{
       
  2604 		iQueryBuf->AppendL( propDef->GetName() );
       
  2605 
       
  2606 		if( aResult )
       
  2607 			{
       
  2608 			if( iAppendToResultRow )
       
  2609 				{
       
  2610 				// expected result for property's type
       
  2611 			    iResultRow.AppendL( TColumn( propDef->GetSqlType() ) );
       
  2612 
       
  2613 			    iPropertyFilters.Append( propDef );
       
  2614 				}
       
  2615 			}
       
  2616 		}
       
  2617 	// object definition must always exist for given property definition ID
       
  2618 	else
       
  2619 		{
       
  2620 #ifdef _DEBUG
       
  2621 		User::Panic( _L("MdSFCAC2") , KErrMdEUnknownObjectDef );
       
  2622 #endif
       
  2623 		User::Leave( KErrMdEUnknownObjectDef );
       
  2624 		}
       
  2625 	}
       
  2626 
       
  2627 // ---------------------------------------------------------------------------
       
  2628 // AppendGroupByL           Appends Group BY statements
       
  2629 // ---------------------------------------------------------------------------
       
  2630 //
       
  2631 void CMdSFindSqlClause::AppendGroupByL()
       
  2632 	{
       
  2633 	// if query type is object query
       
  2634 	if ( EQueryTypeObject == iSearchCriteria->iQueryType )
       
  2635 		{
       
  2636 		switch( iSearchCriteria->iQueryResultType )
       
  2637 			{
       
  2638 			case EQueryResultModeItem:
       
  2639 			case EQueryResultModeId:
       
  2640 			case EQueryResultModeCount:
       
  2641 				{
       
  2642 				// "GROUP BY BO.ObjectId "
       
  2643 				iQueryBuf->AppendL( KGroupByObjectId );
       
  2644 				}
       
  2645 				break;
       
  2646 
       
  2647 			default:
       
  2648 #ifdef _DEBUG
       
  2649 				User::Panic( _L("MdSFCAGB") , KErrMdEUnknownQueryResultMode );
       
  2650 #endif
       
  2651 				User::Leave( KErrMdEUnknownQueryResultMode );
       
  2652 			}
       
  2653 		}
       
  2654 	}
       
  2655 
       
  2656 // ---------------------------------------------------------------------------
       
  2657 // AppendOrderByL           Appends ORDER BY statements
       
  2658 // ---------------------------------------------------------------------------
       
  2659 //
       
  2660 void CMdSFindSqlClause::AppendOrderByL()
       
  2661     {
       
  2662 	if( iSearchCriteria->iOrderRules.iPtr.iCount > 0 )
       
  2663 		{
       
  2664 		iQueryBuf->AppendL( KOrderBy );
       
  2665 
       
  2666 		iSerializedBuffer->PositionL( iSearchCriteria->iOrderRules.iPtr.iOffset );
       
  2667 
       
  2668 		// Add recursive all children.
       
  2669 	    for( TUint32 i = 0; i < iSearchCriteria->iOrderRules.iPtr.iCount; i++)
       
  2670 			{
       
  2671 			if( i > 0 )
       
  2672 				{
       
  2673 				iQueryBuf->AppendL( KComma );
       
  2674 				}
       
  2675 
       
  2676 			const TMdCOrderRule& orderRule = TMdCOrderRule::GetFromBufferL( 
       
  2677 					*iSerializedBuffer );
       
  2678 
       
  2679 			switch( iSearchCriteria->iQueryType )
       
  2680 				{
       
  2681 				case EQueryTypeObject:
       
  2682 					switch( orderRule.iOrderRule )
       
  2683 						{
       
  2684 						case EOrderRuleTypeProperty:
       
  2685 							AppendColumnByPropertyDefIdL( orderRule.iPropertyDefId );
       
  2686 							break;
       
  2687 
       
  2688 						case EOrderRuleTypeItemID:
       
  2689 							iQueryBuf->AppendL( KBaseObjectObjectId );
       
  2690 							break;
       
  2691 
       
  2692 						case EOrderRuleTypeObjectURI:
       
  2693 							_LIT( KQueryBaseObjectURI, "BO.URI");
       
  2694 							iQueryBuf->AppendL( KQueryBaseObjectURI );
       
  2695 							break;
       
  2696 
       
  2697 						case EOrderRuleTypeObjectDef:
       
  2698 							_LIT( KQueryBaseObjectDefId, "BO.ObjectDefId");
       
  2699 							iQueryBuf->AppendL( KQueryBaseObjectDefId );
       
  2700 							break;
       
  2701 
       
  2702 						case EOrderRuleTypeUsageCount:
       
  2703 							_LIT( KQueryBaseObjectUsageCount, "BO.UsageCount");
       
  2704 							iQueryBuf->AppendL( KQueryBaseObjectUsageCount );
       
  2705 							break;
       
  2706 
       
  2707 						default:
       
  2708 							User::Leave( KErrNotSupported );
       
  2709 						}
       
  2710 					break;
       
  2711 
       
  2712 				case EQueryTypeRelation:
       
  2713 					switch( orderRule.iOrderRule )
       
  2714 						{
       
  2715 						case EOrderRuleTypeItemID:
       
  2716 							iQueryBuf->AppendL( KRelationId );
       
  2717 							break;
       
  2718 
       
  2719 						case EOrderRuleTypeLastModifiedDate:
       
  2720 							iQueryBuf->AppendL( KRelationLastModifiedDate );
       
  2721 							break;
       
  2722 
       
  2723 						case EOrderRuleTypeParameterValue:
       
  2724 							iQueryBuf->AppendL( KRelationParameter );
       
  2725 							break;
       
  2726 
       
  2727 						case EOrderRuleTypeLeftObjectID:
       
  2728 							_LIT( KQueryRelationLeftObjectId, "LeftObjectId");
       
  2729 							iQueryBuf->AppendL( KQueryRelationLeftObjectId );
       
  2730 							break;
       
  2731 
       
  2732 						case EOrderRuleTypeRightObjectID:
       
  2733 							_LIT( KQueryRelationRightObjectId, "RightObjectId");
       
  2734 							iQueryBuf->AppendL( KQueryRelationRightObjectId );
       
  2735 							break;
       
  2736 
       
  2737 						case EOrderRuleTypeRelationDef:
       
  2738 							_LIT( KQueryRelationDef, "RelationDefId");
       
  2739 							iQueryBuf->AppendL( KQueryRelationDef );
       
  2740 							break;
       
  2741 
       
  2742 						default:
       
  2743 							User::Leave( KErrNotSupported );
       
  2744 						}
       
  2745 					break;
       
  2746 
       
  2747 				case EQueryTypeEvent:
       
  2748 					switch( orderRule.iOrderRule )
       
  2749 						{
       
  2750 						case EOrderRuleTypeItemID:
       
  2751 							iQueryBuf->AppendL( KEventId );
       
  2752 							break;
       
  2753 
       
  2754 						case EOrderRuleTypeCreationTime:
       
  2755 							iQueryBuf->AppendL( KEventTimeStamp );
       
  2756 							break;
       
  2757 
       
  2758 						case EOrderRuleTypeSourceURI:
       
  2759 							_LIT( KQueryEventSource, "Source");
       
  2760 							iQueryBuf->AppendL( KQueryEventSource );
       
  2761 							break;
       
  2762 
       
  2763 						case EOrderRuleTypeParticipantURI:
       
  2764 							_LIT( KQueryEventParticipant, "Participant");
       
  2765 							iQueryBuf->AppendL( KQueryEventParticipant );
       
  2766 							break;
       
  2767 
       
  2768 						case EOrderRuleTypeTargetObjectID:
       
  2769 							_LIT( KQueryEventObjectId, "ObjectId");
       
  2770 							iQueryBuf->AppendL( KQueryEventObjectId );
       
  2771 							break;
       
  2772 
       
  2773 						case EOrderRuleTypeEventDef:
       
  2774 							_LIT( KQueryEventDef, "EventDefId");
       
  2775 							iQueryBuf->AppendL( KQueryEventDef );
       
  2776 							break;
       
  2777 
       
  2778 						default:
       
  2779 							User::Leave( KErrNotSupported );
       
  2780 						}
       
  2781 					break;
       
  2782 
       
  2783 				default:
       
  2784 #ifdef _DEBUG
       
  2785 					User::Panic( _L("MdSFCAOB") , KErrMdEUnknownQueryType );
       
  2786 #endif
       
  2787 					User::Leave( KErrMdEUnknownQueryType );
       
  2788 				}
       
  2789 
       
  2790 			if ( !orderRule.iCaseSensitive )
       
  2791 				{
       
  2792 				iQueryBuf->AppendL( KNoCaseSensitive );
       
  2793 				}
       
  2794 
       
  2795 			if( orderRule.iAscending )
       
  2796 				{
       
  2797 				iQueryBuf->AppendL( KAsc );
       
  2798 				}
       
  2799 			else
       
  2800 				{
       
  2801 				iQueryBuf->AppendL( KDesc );
       
  2802 				}
       
  2803 			}
       
  2804 		
       
  2805 		iQueryBuf->AppendL( KSpace );				
       
  2806 		}
       
  2807     }
       
  2808 
       
  2809 // ---------------------------------------------------------------------------
       
  2810 // AppendLimitAndOffsetL            LIMIT and OFFSET statements
       
  2811 // ---------------------------------------------------------------------------
       
  2812 //
       
  2813 void CMdSFindSqlClause::AppendLimitAndOffsetL()
       
  2814     {
       
  2815     if ( iSearchCriteria->iOffset > 0 )
       
  2816     	{
       
  2817     	// 10 is maximum length of text format TUint32
       
  2818     	iQueryBuf->ReserveSpaceL( iQueryBuf->ConstBufferL().Length() + 
       
  2819     			2 * 10 );
       
  2820 
       
  2821     	iQueryBuf->AppendL( KLimit );
       
  2822     	iQueryBuf->BufferL().AppendNum( iSearchCriteria->iLimit );
       
  2823     	iQueryBuf->AppendL( KComma );
       
  2824     	iQueryBuf->BufferL().AppendNum( iSearchCriteria->iOffset );
       
  2825     	}
       
  2826     else if ( iSearchCriteria->iLimit != KMaxTUint32 )
       
  2827     	{
       
  2828     	// 10 is maximum length of text format TUint32
       
  2829     	iQueryBuf->ReserveSpaceL( iQueryBuf->ConstBufferL().Length() + 
       
  2830     			10 );
       
  2831     	
       
  2832     	iQueryBuf->AppendL( KLimit );
       
  2833     	iQueryBuf->BufferL().AppendNum( iSearchCriteria->iLimit );
       
  2834     	}
       
  2835     }
       
  2836 
       
  2837 // ---------------------------------------------------------------------------
       
  2838 // CopyVariablesL   Copies number of variables from variable table to the
       
  2839 //                  end of the variable table
       
  2840 // ---------------------------------------------------------------------------
       
  2841 //
       
  2842 void CMdSFindSqlClause::CopyVariablesL( TInt aStart, TInt aEnd )
       
  2843     {
       
  2844     TColumn column;
       
  2845     
       
  2846     for ( TInt i(aStart); i<aEnd; ++i )
       
  2847         {
       
  2848         column = iVariables.Column(i);
       
  2849         
       
  2850         switch( column.Type() )
       
  2851             {
       
  2852             case EColumnBool:
       
  2853                 {
       
  2854                 TBool v=0;
       
  2855                 column.Get( v );
       
  2856                 iVariables.AppendL( TColumn( v ) );
       
  2857                 break;
       
  2858                 }
       
  2859             case EColumnInt32:
       
  2860                 {
       
  2861                 TInt32 v=0;
       
  2862                 column.Get( v );
       
  2863                 iVariables.AppendL( TColumn( v ) );
       
  2864                 break;
       
  2865                 }
       
  2866             case EColumnUint32:
       
  2867                 {
       
  2868                 TUint32 v=0;
       
  2869                 column.Get( v );
       
  2870                 iVariables.AppendL( TColumn( v ) );
       
  2871                 break;
       
  2872                 }
       
  2873             case EColumnTime:
       
  2874                 {
       
  2875                 TTime v(0);
       
  2876                 column.Get( v );
       
  2877                 iVariables.AppendL( TColumn( v ) );
       
  2878                 break;
       
  2879                 }
       
  2880             case EColumnInt64:
       
  2881                 {
       
  2882                 TInt64 v=0;
       
  2883                 column.Get( v );
       
  2884                 iVariables.AppendL( TColumn( v ) );
       
  2885                 break;
       
  2886                 }
       
  2887             case EColumnReal32:
       
  2888                 {
       
  2889                 TReal32 v=0;
       
  2890                 column.Get( v );
       
  2891                 iVariables.AppendL( TColumn( v ) );
       
  2892                 break;
       
  2893                 }
       
  2894             case EColumnReal64:
       
  2895                 {
       
  2896                 TReal64 v=0;
       
  2897                 column.Get( v );
       
  2898                 iVariables.AppendL( TColumn( v ) );
       
  2899                 break;
       
  2900                 }
       
  2901             case EColumnDes16:
       
  2902                 {
       
  2903                 TPtrC16 v = TPtr16((TUint16*)0, 0); //KNullPtr16;
       
  2904                 column.Get( v );
       
  2905                 iVariables.AppendL( TColumn( v ) );
       
  2906                 break;
       
  2907                 }
       
  2908             default:
       
  2909 #ifdef _DEBUG
       
  2910 				User::Panic( _L("MdSFCCoV") , KErrMdEUnknownPropertyType );
       
  2911 #endif            	
       
  2912                 User::Leave( KErrMdEUnknownPropertyType );
       
  2913             }
       
  2914         }
       
  2915     }