persistentstorage/sql/SRC/Client/SqlStatementImpl.inl
changeset 0 08ec8eefde2f
child 11 211563e4b919
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // ///////////////////////             RSqlLongColumnColl        ////////////////////////////////////////////
       
    15 // Sets the default granularity of the collection.
       
    16 // 
       
    17 //
       
    18 
       
    19 inline RSqlLongColumnColl::RSqlLongColumnColl() :
       
    20 	iValues(KLongColumnCollGranularity)
       
    21 	{
       
    22 	}
       
    23 
       
    24 /**
       
    25 Releases the allocated memory.
       
    26 */
       
    27 inline void RSqlLongColumnColl::Close()
       
    28 	{
       
    29 	LONGCOL_INVARIANT();
       
    30 	Reset();
       
    31 	iValues.Close();	
       
    32 	}
       
    33 
       
    34 /**
       
    35 Destroys all long column values in the collection, without destroying the collection object.
       
    36 */
       
    37 inline void RSqlLongColumnColl::Reset()
       
    38 	{
       
    39 	LONGCOL_INVARIANT();
       
    40 	for(TInt i=iValues.Count()-1;i>=0;--i)
       
    41 		{
       
    42 		delete iValues[i].iData;
       
    43 		}
       
    44 	iValues.Reset();
       
    45 	}
       
    46 
       
    47 /**
       
    48 Constructs and returns a TPtrC object to the long column value, identified by aColumnIndex argument.
       
    49 @param aColumnIndex Column index
       
    50 @return A TPtrC object to the long column value
       
    51 @panic SqlDb 4 In _DEBUG mode, aColumnIndex value is negative.
       
    52 @panic SqlDb 7 There is no long column value with aColumnIndex index.
       
    53 */
       
    54 inline TPtrC RSqlLongColumnColl::Text(TInt aColumnIndex) const
       
    55 	{
       
    56 	__SQLASSERT(aColumnIndex >= 0, ESqlPanicBadArgument);
       
    57 	LONGCOL_INVARIANT();
       
    58 	TInt rc = FindValue(aColumnIndex);
       
    59 	__SQLASSERT_ALWAYS(rc >= 0, ESqlPanicInternalError);
       
    60 	TPtrC8 ptr(iValues[rc].iData->Des());
       
    61 	return TPtrC(reinterpret_cast <const TUint16*> (ptr.Ptr()), ptr.Length() / sizeof(TUint16));
       
    62 	}
       
    63 
       
    64 /**
       
    65 Constructs and returns a TPtrC8 object to the long column value, identified by aColumnIndex argument.
       
    66 @param aColumnIndex Column index
       
    67 @return A TPtrC8 object to the long column value
       
    68 @panic SqlDb 4 In _DEBUG mode, aColumnIndex value is negative.
       
    69 @panic SqlDb 7 There is no long column value with aColumnIndex index.
       
    70 */
       
    71 inline TPtrC8 RSqlLongColumnColl::Binary(TInt aColumnIndex) const
       
    72 	{
       
    73 	__SQLASSERT(aColumnIndex >= 0, ESqlPanicBadArgument);
       
    74 	LONGCOL_INVARIANT();
       
    75 	TInt rc = FindValue(aColumnIndex);
       
    76 	__SQLASSERT_ALWAYS(rc >= 0, ESqlPanicInternalError);
       
    77 	return iValues[rc].iData->Des();
       
    78 	}
       
    79 
       
    80 /**
       
    81 Returns true if there is a long column value in the collection, which index is aColumnIndex.
       
    82 @param aColumnIndex Column index
       
    83 @return True if there is a long column value in the collection, which index is aColumnIndex.
       
    84 @panic SqlDb 4 In _DEBUG mode, aColumnIndex value is negative.
       
    85 */
       
    86 inline TBool RSqlLongColumnColl::IsPresent(TInt aColumnIndex) const
       
    87 	{
       
    88 	__SQLASSERT(aColumnIndex >= 0, ESqlPanicBadArgument);
       
    89 	LONGCOL_INVARIANT();
       
    90 	return FindValue(aColumnIndex) >= 0;
       
    91 	}
       
    92 
       
    93 /**
       
    94 The method returns the index in the collection of the long column value, identified by aColumnIndex argument.
       
    95 @param aColumnIndex Column index
       
    96 @return The collection index of the long column value, identified by aColumnIndex,
       
    97 		KErrNotFound otherwise.
       
    98 @panic SqlDb 4 In _DEBUG mode, aColumnIndex value is negative.
       
    99 */
       
   100 inline TInt RSqlLongColumnColl::FindValue(TInt aColumnIndex) const
       
   101 	{
       
   102 	__SQLASSERT(aColumnIndex >= 0, ESqlPanicBadArgument);
       
   103 	return iValues.Find(aColumnIndex, &RSqlLongColumnColl::TData::Compare);
       
   104 	}
       
   105 
       
   106 /**
       
   107 Initializes the RSqlLongColumnColl::TData instance with the column index, and a pointer to the column data
       
   108 @param aIndex Column index
       
   109 @param aData A HBufC8 pointer to the column data. Cannot be NULL, this is a long column value
       
   110 @panic SqlDb 4 In _DEBUG mode, aColumnIndex value is negative or aData is NULL.
       
   111 */
       
   112 inline RSqlLongColumnColl::TData::TData(TInt aIndex, HBufC8* aData) :
       
   113 	iIndex(aIndex),
       
   114 	iData(aData)
       
   115 	{
       
   116 	__SQLASSERT(aIndex >= 0, ESqlPanicBadArgument);
       
   117 	__SQLASSERT(aData != NULL, ESqlPanicBadArgument);
       
   118 	}
       
   119 
       
   120 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   121 //////////////////////////           CSqlStatementImpl           ////////////////////////////////////////////
       
   122 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   123 	
       
   124 /**
       
   125 Initializes the created CSqlDatabaseImpl object.
       
   126 Works with 8/16-bit SQL statements.
       
   127 
       
   128 @param aDatabase A reference to CSqlDatabaseImpl object.
       
   129 @param aSqlStmt 8/16-bit string containing the SQL statement.
       
   130 
       
   131 @return KErrNoMemory, an out of memory condition has occurred;
       
   132  	    KErrArgument, bad argument, for example - the SQL string contains more than one SQL statements.
       
   133                       Note that the function may leave with some database specific errors categorised as 
       
   134                       ESqlDbError or other system-wide error codes;
       
   135         KErrNone      Operation has completed successfully.
       
   136 
       
   137 @see CSqlStatementImpl::New()
       
   138 */
       
   139 template <class DES> TInt CSqlStatementImpl::Construct(CSqlDatabaseImpl& aDatabase, const DES& aSqlStmt)
       
   140 	{
       
   141 	TInt err = iSqlStmtSession.Prepare(aDatabase.Session(), aSqlStmt, iColumnCnt, iParamCnt);
       
   142 	if(err != KErrNone)
       
   143 		{
       
   144 		return err;	
       
   145 		}
       
   146 	if(iColumnCnt >= 0)
       
   147 		{
       
   148 		err = iColumnValueBuf.SetCount(iColumnCnt);
       
   149 		if(err != KErrNone)
       
   150 			{
       
   151 			return err;	
       
   152 			}
       
   153 		iColumnValBufIt.Set(iColumnValueBuf);
       
   154 		}
       
   155 	if(iParamCnt > 0)
       
   156 		{
       
   157 		err = iParamValueBuf.SetCount(iParamCnt);
       
   158 		if(err != KErrNone)
       
   159 			{
       
   160 			return err;	
       
   161 			}
       
   162 		iParamValBufIt.Set(iParamValueBuf);
       
   163 		}
       
   164 	return KErrNone;
       
   165 	}
       
   166 
       
   167 /**
       
   168 Template function, friend of CSqlStatementImpl, that is used for creation of CSqlStatementImpl objects.
       
   169 
       
   170 @param aImpl A reference to a CSqlStatementImpl pointer. Will be initialized after a successfull CSqlStatementImpl construction.
       
   171 @param aDatabase A reference to a CSqlDatabaseImpl object.
       
   172 @param aSqlStmt The SQL statement: 8-bit or 16-bit.
       
   173 
       
   174 @return KErrNoMemory, an out of memory condition has occurred;
       
   175  	    KErrArgument, bad argument, for example - the SQL string contains more than one SQL statements.
       
   176                       Note that the function may leave with some database specific errors categorised as 
       
   177                       ESqlDbError or other system-wide error codes;
       
   178         KErrNone      Operation has completed successfully.
       
   179 
       
   180 @internalComponent
       
   181 */
       
   182 template <class DES> TInt SqlCreateStatement(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const DES& aSqlStmt)
       
   183 	{
       
   184 	TInt err = KErrNoMemory;
       
   185 	aImpl = new CSqlStatementImpl;
       
   186 	if(aImpl)
       
   187 		{
       
   188 		err = aImpl->Construct(aDatabase, aSqlStmt);
       
   189 		}
       
   190 	if(err != KErrNone)
       
   191 		{
       
   192 		delete aImpl;
       
   193 		aImpl = NULL;
       
   194 		}
       
   195 	return err;
       
   196 	}
       
   197 
       
   198 /**
       
   199 Factory function for creating CSqlStatementImpl objects.
       
   200 Works with 16-bit SQL statements.
       
   201 
       
   202 Creates CSqlStatementImpl instance and prepares the supplied SQL statement for execution.
       
   203 
       
   204 Note that:
       
   205 - CSqlStatementImpl can prepare and execute both parametrized SQL statements and SQL statements 
       
   206   without parameters;
       
   207 - CSqlStatementImpl cannot prepare and execute SQL strings containing more than one SQL statement;
       
   208 
       
   209 @param aImpl A reference to CSqlStatementImpl pointer which will be set to point to the created instance
       
   210 @param aDatabase A reference to CSqlDatabaseImpl object.
       
   211 @param aSqlStmt 16-bit string containing the SQL statement.
       
   212 
       
   213 @return KErrNoMemory, an out of memory condition has occurred;
       
   214  	    KErrArgument, bad argument, for example - the SQL string contains more than one SQL statements.
       
   215                       Note that the function may leave with some database specific errors categorised as 
       
   216                       ESqlDbError or other system-wide error codes;
       
   217         KErrNone      Operation has completed successfully.
       
   218 
       
   219 @see RSqlStatement
       
   220 @see RSqlStatement::Prepare()
       
   221 */
       
   222 inline TInt CSqlStatementImpl::New(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const TDesC16& aSqlStmt)
       
   223 	{
       
   224 	return SqlCreateStatement(aImpl, aDatabase, aSqlStmt);
       
   225 	}
       
   226 
       
   227 /**
       
   228 Factory function for creating CSqlStatementImpl objects.
       
   229 Works with 8-bit SQL statements.
       
   230 
       
   231 Creates CSqlStatementImpl instance and prepares the supplied SQL statement for execution.
       
   232 
       
   233 Note that:
       
   234 - CSqlStatementImpl can prepare and execute both parametrized SQL statements and SQL statements 
       
   235   without parameters;
       
   236 - CSqlStatementImpl cannot prepare and execute SQL strings containing more than one SQL statement;
       
   237 
       
   238 @param aImpl A reference to CSqlStatementImpl pointer which will be set to point to the created instance
       
   239 @param aDatabase A reference to CSqlDatabaseImpl object.
       
   240 @param aSqlStmt 8-bit string containing the SQL statement.
       
   241 
       
   242 @return KErrNoMemory, an out of memory condition has occurred;
       
   243  	    KErrArgument, bad argument, for example - the SQL string contains more than one SQL statements.
       
   244                       Note that the function may leave with some database specific errors categorised as 
       
   245                       ESqlDbError or other system-wide error codes;
       
   246         KErrNone      Operation has completed successfully.
       
   247 
       
   248 @see RSqlStatement
       
   249 @see RSqlStatement::Prepare()
       
   250 */
       
   251 inline TInt CSqlStatementImpl::New(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const TDesC8& aSqlStmt)
       
   252 	{
       
   253 	return SqlCreateStatement(aImpl, aDatabase, aSqlStmt);
       
   254 	}
       
   255 
       
   256 /**
       
   257 @return Non-zero if CSqlStatementImpl object points at a valid record, zero otherwise.
       
   258 
       
   259 @see RSqlStatement::AtRow()
       
   260 */
       
   261 inline TBool CSqlStatementImpl::AtRow() const
       
   262 	{
       
   263 	return iState == CSqlStatementImpl::EAtRow;
       
   264 	}
       
   265 
       
   266 /**
       
   267 Implements RSqlStatement::ColumnCount().
       
   268 
       
   269 @see RSqlStatement::ColumnCount().
       
   270 
       
   271 @return The column count of the statement
       
   272 */	
       
   273 inline TInt CSqlStatementImpl::ColumnCount() const
       
   274 	{
       
   275 	return iColumnCnt;
       
   276 	}
       
   277 
       
   278 /**
       
   279 Gets the index (starting from 0) of the parameter with the given name.
       
   280 
       
   281 The function does a case insensitive parameter name search.
       
   282 
       
   283 If the parameter name is ":Prm", then the ":" prefix cannot be omitted when you call ParameterIndex().
       
   284 
       
   285 This function can be called at any time after the SQL statement has been prepared.
       
   286 
       
   287 @param aParamName Parameter name
       
   288 
       
   289 @return the parameter index value, if successful - this is a non-negative integer value;
       
   290         KErrNotFound, if no such parameter can be found.
       
   291         One of the other system-wide error codes may also be returned.
       
   292 
       
   293 @see RSqlStatement::ParamIndex()
       
   294 */
       
   295 inline TInt CSqlStatementImpl::ParamIndex(const TDesC& aParamName)
       
   296 	{
       
   297 	return Name2Index(aParamName, iParamNameBuf, iParamCnt, ESqlSrvStmtParamNames, iParamNameBufPresent);
       
   298 	}
       
   299 
       
   300 /**
       
   301 Gets the index (starting from 0) of the column with the given name.
       
   302 
       
   303 The function does a case insensitive column name search.
       
   304 
       
   305 This function can be called at any time after the SQL statement has been prepared.
       
   306 
       
   307 @param aColumnName Column name
       
   308 
       
   309 @return the column index value, if successful - this is a non-negative integer value;
       
   310         KErrNotFound, if no such parameter can be found.
       
   311         One of the other system-wide error codes may also be returned.
       
   312 
       
   313 @see RSqlStatement::ColumnIndex()
       
   314 */
       
   315 inline TInt CSqlStatementImpl::ColumnIndex(const TDesC& aColumnName)
       
   316 	{
       
   317 	return Name2Index(aColumnName, iColumnNameBuf, iColumnCnt, ESqlSrvStmtColumnNames, iColumnNameBufPresent);
       
   318 	}
       
   319 
       
   320 /**
       
   321 Gives an access to the content of the requested column as a stream of bytes or characters.
       
   322 
       
   323 The method creates a read-only MStreamBuf derived object which allows the column with aColumnIndex index 
       
   324 to be accessed as a stream of bytes (if the column is a binary column) or characters 
       
   325 (if the column is a text column) and returns it to the caller.
       
   326 
       
   327 The caller is responsible for the destroying of the read-only MStreamBuf derived object.
       
   328 
       
   329 ColumnSourceL() can be used only after successful Next() call (Next() returned KSqlAtRow),
       
   330 otherwise the method issues panic 11.
       
   331 
       
   332 @param aColumnIndex Column index (starting from 0)
       
   333 @return A pointer to the created read-only memory MStreamBuf derived object.
       
   334 
       
   335 @leave KErrNoMemory, an out of memory condition has occurred;
       
   336 
       
   337 @panic SqlDb  5 Column index out of bounds.
       
   338 @panic SqlDb  7 In _DEBUG mode. aColumnIndex index does not refer to a text or binary column.
       
   339 @panic SqlDb 11 Statement object not positioned at row.
       
   340 
       
   341 @see RSqlColumnReadStream
       
   342 */
       
   343 inline MStreamBuf* CSqlStatementImpl::ColumnSourceL(TInt aColumnIndex)
       
   344 	{
       
   345 	__SQLASSERT_ALWAYS((TUint)aColumnIndex < (TUint)iColumnCnt, ESqlPanicBadColumnIndex);
       
   346 	__SQLASSERT_ALWAYS(iState == CSqlStatementImpl::EAtRow, ESqlPanicInvalidRow);
       
   347 	iColumnValBufIt.MoveTo(aColumnIndex);		
       
   348 	return iColumnValBufIt.IsPresent() ? iColumnValBufIt.StreamL() : iSqlStmtSession.ColumnSourceL(aColumnIndex);
       
   349 	}
       
   350 
       
   351 /**
       
   352 Gives an access to the content of the requested parameter as a stream of bytes or characters.
       
   353 
       
   354 The method creates an IPC object with buffering capabilities, allowing to stream out the data of the 
       
   355 parameter with aParameterIndex index and returns this MStreamBuf derived object to the caller.
       
   356 
       
   357 The caller is responsible for the destroying of the MStreamBuf derived object.
       
   358 
       
   359 @param aFunction Requested operation
       
   360 @param aParamIndex Parameter index (starting from 0)
       
   361 
       
   362 @return A pointer to the created MStreamBuf derived object.
       
   363 
       
   364 @leave KErrNoMemory, an out of memory condition has occurred;
       
   365 
       
   366 @panic SqlDb 5 Parameter index out of bounds.
       
   367 
       
   368 @see RSqlParamWriteStream
       
   369 */
       
   370 inline MStreamBuf* CSqlStatementImpl::ParamSinkL(TSqlSrvFunction aFunction, TInt aParamIndex)
       
   371 	{
       
   372 	__SQLASSERT_ALWAYS((TUint)aParamIndex < (TUint)iParamCnt, ESqlPanicBadColumnIndex);
       
   373 	return iSqlStmtSession.ParamSinkL(aFunction, aParamIndex);
       
   374 	}
       
   375 
       
   376 /**
       
   377 Sets the internal state of CSqlStatementImpl instance to CSqlStatementImpl::EUnknown.
       
   378 */
       
   379 inline CSqlStatementImpl::CSqlStatementImpl() :
       
   380 	iBound(ETrue),
       
   381 	iState(CSqlStatementImpl::EUnknown)
       
   382 	{
       
   383 	}