persistentstorage/sql/SRC/Server/SqlSrvStatement.inl
changeset 0 08ec8eefde2f
child 9 667e88a979d7
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 // ///////////////                HSqlSrvStmtParamBuf                        ////////////////////////////
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  
       
    20  Creates a new HSqlSrvStmtParamBuf instance.
       
    21  
       
    22  @param aStatement A reference to the CSqlSrvStatement object, which needs the parameter data.
       
    23  @param aParameterIndex Parameter index, zero based.
       
    24  @param aDataType Parameter value type - binary, text8 or text16.
       
    25  @param aBufType IPC stream buffer or a simple "bind param" buffer
       
    26  
       
    27  @return A pointer to the created HSqlSrvStmtParamBuf instance.
       
    28  
       
    29  @leave KErrNoMemory, an out of memory condition has occurred;
       
    30  
       
    31  @panic SqlDb 4 In _DEBUG mode. Parameter index negative.
       
    32 */
       
    33 inline HSqlSrvStmtParamBuf* HSqlSrvStmtParamBuf::NewL(CSqlSrvStatement& aStatement, TInt aParamIndex, 
       
    34 													  HSqlSrvStmtParamBuf::TDataType aDataType, HSqlSrvStmtParamBuf::TBufType aBufType)
       
    35 	{
       
    36 	__SQLASSERT(aParamIndex >= 0, ESqlPanicBadArgument);
       
    37 	HSqlSrvStmtParamBuf* self = new (ELeave) HSqlSrvStmtParamBuf(aStatement, aParamIndex, aDataType, aBufType);
       
    38 	self->PushL();
       
    39 	self->ConstructL();
       
    40 	CleanupStack::Pop();
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 /**
       
    45 Resets the current HSqlSrvStmtParamBuf instance.
       
    46 
       
    47 The internal buffer will be resized, if its capacity is bigger than HSqlSrvStmtParamBuf::EExpandSize * 2 bytes.
       
    48 
       
    49 @param aDataType Parameter value type - binary, text8 or text16.
       
    50 @param aBufType IPC stream buffer or a simple "bind param" buffer
       
    51 
       
    52 @panic SqlDb 2 In _DEBUG mode. Invalid HSqlSrvStmtParamBuf object.
       
    53 */
       
    54 inline void HSqlSrvStmtParamBuf::Reset(HSqlSrvStmtParamBuf::TDataType aDataType, HSqlSrvStmtParamBuf::TBufType aBufType)
       
    55 	{
       
    56 	__SQLASSERT(iBuf != NULL, ESqlPanicInvalidObj);
       
    57 	
       
    58 	iStatementFinalized = EFalse;
       
    59 	iAlive = ETrue;
       
    60 	iDataType = aDataType;
       
    61 	iBufType = aBufType;
       
    62 	iSynchDone = EFalse;
       
    63 	
       
    64 	iBuf->Delete(0, iBuf->Size());
       
    65 	if(iBuf->Capacity() > (HSqlSrvStmtParamBuf::EExpandSize * 2))
       
    66 		{
       
    67 		TRAPD(err, iBuf->SetReserveL(HSqlSrvStmtParamBuf::EExpandSize * 2));//the buffer size is minimized, the call can't fail
       
    68 		__SQLASSERT_ALWAYS(err == KErrNone, ESqlPanicInternalError);
       
    69 		}
       
    70 	Set(*iBuf, 0, MStreamBuf::ERead | MStreamBuf::EWrite);
       
    71 	}
       
    72 
       
    73 /**
       
    74 Stores the aData into the buffer, from position 0.
       
    75 This function is used when the buffer type is not IPC.
       
    76 
       
    77 @param aData The data to be stored into the buffer
       
    78 
       
    79 @leave KErrNoMemory, an out of memory condition has occurred;
       
    80 
       
    81 @panic SqlDb 2 In _DEBUG mode. Invalid HSqlSrvStmtParamBuf object.
       
    82 @panic SqlDb 7 In _DEBUG mode. The buffer type is not HSqlSrvStmtParamBuf::EBufSimpleBind
       
    83 */
       
    84 inline const TPtrC8 HSqlSrvStmtParamBuf::SetDataL(const TDesC8& aData)
       
    85 	{
       
    86 	__SQLASSERT(iBuf != NULL, ESqlPanicInvalidObj);
       
    87 	__SQLASSERT(iBufType == HSqlSrvStmtParamBuf::EBufSimpleBind, ESqlPanicInternalError);
       
    88 	iBuf->ResizeL(aData.Length());
       
    89 	iBuf->Write(0, aData);
       
    90 	return iBuf->Ptr(0);
       
    91 	}
       
    92 
       
    93 /**
       
    94 Returns a 8-bit pointer to the parameter data.
       
    95 
       
    96 @return 8-bit pointer to the parameter data.
       
    97 
       
    98 @panic SqlDb 2 In _DEBUG mode. Invalid HSqlSrvStmtParamBuf object.
       
    99 */
       
   100 inline const TPtrC8 HSqlSrvStmtParamBuf::Data() const
       
   101 	{
       
   102 	__SQLASSERT(iBuf != NULL, ESqlPanicInvalidObj);
       
   103 	return iBuf->Ptr(0);
       
   104 	}
       
   105 
       
   106 /**
       
   107 @return Buffered parameter value type - binary, text8 or text16.
       
   108 */
       
   109 inline HSqlSrvStmtParamBuf::TDataType HSqlSrvStmtParamBuf::DataType() const
       
   110 	{
       
   111 	return iDataType;		
       
   112 	}
       
   113 
       
   114 /**
       
   115 Returns the parameter index.
       
   116 
       
   117 @panic SqlDb 7 In _DEBUG mode. Negative parameter index.
       
   118 
       
   119 @return Parameter index, zero based.
       
   120 */
       
   121 inline TInt HSqlSrvStmtParamBuf::ParamIndex() const
       
   122 	{
       
   123 	__SQLASSERT(iParamIndex >= 0, ESqlPanicInternalError);
       
   124 	return iParamIndex;
       
   125 	}
       
   126 
       
   127 /**
       
   128 Initializes the HSqlSrvStmtParamBuf instance data members.
       
   129 
       
   130 @param aStatement A reference to the CSqlSrvStatement object, which needs the parameter data.
       
   131 @param aParameterIndex Parameter index, zero based.
       
   132 @param aDataType Parameter value type - binary, text8 or text16.
       
   133 @param aBufType IPC stream buffer or a simple "bind param" buffer
       
   134 */
       
   135 inline HSqlSrvStmtParamBuf::HSqlSrvStmtParamBuf(CSqlSrvStatement& aStatement, TInt aParamIndex, 
       
   136 												HSqlSrvStmtParamBuf::TDataType aDataType, HSqlSrvStmtParamBuf::TBufType aBufType) :
       
   137 	iStatement(aStatement),
       
   138 	iBuf(NULL),
       
   139 	iParamIndex(aParamIndex),
       
   140 	iStatementFinalized(EFalse),
       
   141 	iAlive(EFalse),
       
   142 	iDataType(aDataType),
       
   143 	iBufType(aBufType),
       
   144 	iSynchDone(EFalse)
       
   145 	{
       
   146 	}
       
   147 
       
   148 /**
       
   149 HSqlSrvStmtParamBuf - second phase construction.
       
   150 Constructs the internal CFlatBuf object.
       
   151 
       
   152 @leave KErrNoMemory, an out of memory condition has occurred;
       
   153 */
       
   154 inline void HSqlSrvStmtParamBuf::ConstructL()
       
   155 	{
       
   156 	iBuf = CBufFlat::NewL(EExpandSize);
       
   157 	Set(*iBuf, 0, MStreamBuf::ERead | MStreamBuf::EWrite);
       
   158 	iAlive = ETrue;
       
   159 	}
       
   160 
       
   161 //////////////////////////////////////////////////////////////////////////////////////////////////////
       
   162 /////////////////////////////   CSqlSrvStatement class    ////////////////////////////////////////////
       
   163 //////////////////////////////////////////////////////////////////////////////////////////////////////
       
   164 
       
   165 /**
       
   166 Executes the SQL statement moving the cursor to the next row if there is a row available.
       
   167 
       
   168 @return KSqlErrStmtExpired, statement expired (if new functions or collating sequences are 
       
   169 							registered or if an authorizer function is added or changed);
       
   170 		KErrNone, the operation completed successfully;
       
   171 		KSqlAtRow, the next record data is ready for processing by the caller;
       
   172 		KSqlAtEnd, no more records;
       
   173 		KSqlErrBusy, database file is locked;
       
   174 		KSqlErrGeneral, run-time error. Next() should not be called anymore;
       
   175 		KSqlErrMisuse, Next() called after KSqlAtEnd or KSqlErrGeneral returned by the previous Next() call;
       
   176 		KErrNoMemory, an out of memory condition has occurred. The statement will be reset.
       
   177 
       
   178 @panic SqlDb 2 In _DEBUG mode. Invalid (not created) CSqlSrvStatement object.
       
   179 @panic SqlDb 7 In _DEBUG mode. SQLITE internal error. (SQLITE_ERROR, followed by a sqlite3_reset(), which returns SQLITE_OK)
       
   180 */	
       
   181 inline TInt CSqlSrvStatement::Next()
       
   182 	{
       
   183 	__SQLASSERT(iStmtHandle != NULL, ESqlPanicInvalidObj);
       
   184 	TInt err = ::StmtNext(iStmtHandle);
       
   185 	iBufFlatType = static_cast <TSqlBufFlatType> (-1);
       
   186 	iBufFlat.ResetAndMinimize();
       
   187 	return err;
       
   188 	}
       
   189 
       
   190 /**
       
   191 Resets the prepared SQL statement to its initial state and makes it ready to be executed again.
       
   192 
       
   193 Any SQL statement parameters that had values bound to them, retain their values.
       
   194 
       
   195 @return KErrNone,  the operation completed successfully;
       
   196 		KSqlErrStmtExpired, Statement expired (if new functions or collating sequences are 
       
   197 							registered or if an authorizer function is added or changed)
       
   198 
       
   199 @panic SqlDb 2 In _DEBUG mode. Invalid (not created) CSqlSrvStatement object.
       
   200 */	
       
   201 inline TInt CSqlSrvStatement::Reset()
       
   202 	{
       
   203 	__SQLASSERT(iStmtHandle != NULL, ESqlPanicInvalidObj);
       
   204 	iBufFlatType = static_cast <TSqlBufFlatType> (-1);
       
   205 	iBufFlat.ResetAndMinimize();
       
   206 	return ::StmtReset(iStmtHandle);
       
   207 	}
       
   208 
       
   209 /**
       
   210 Executes the prepared SQL statement.
       
   211 
       
   212 @return KErrNone, the operation completed successfully;
       
   213 		KSqlErrStmtExpired, statement expired (if new functions or collating sequences are 
       
   214 							registered or if an authorizer function is added or changed);
       
   215 		KErrNoMemory, an out of memory condition has occurred. The statement will be reset.
       
   216 
       
   217 @panic SqlDb 2 In _DEBUG mode. Invalid (not created) CSqlSrvStatement object.
       
   218 @panic SqlDb 7 In _DEBUG mode. SQLITE internal error. (SQLITE_ERROR, followed by a sqlite3_reset(), which returns SQLITE_OK)
       
   219 */	
       
   220 inline TInt CSqlSrvStatement::Exec()
       
   221 	{
       
   222 	__SQLASSERT(iStmtHandle != NULL, ESqlPanicInvalidObj);
       
   223 	TInt err = ::StmtExec(iStmtHandle);
       
   224 	iBufFlatType = static_cast <TSqlBufFlatType> (-1);
       
   225 	iBufFlat.ResetAndMinimize();
       
   226 	return err;
       
   227 	}
       
   228 
       
   229 /**
       
   230 @panic SqlDb 2 In _DEBUG mode. Invalid (not created) CSqlSrvStatement object.
       
   231 */
       
   232 inline const RSqlBufFlat& CSqlSrvStatement::BufFlatL(TSqlBufFlatType aWhat) const
       
   233 	{
       
   234 	__SQLASSERT(iStmtHandle != NULL, ESqlPanicInvalidObj);
       
   235 	if(aWhat != iBufFlatType)
       
   236 		{
       
   237 		__SQLLEAVE(KErrArgument);
       
   238 		}
       
   239 	return iBufFlat;
       
   240 	}
       
   241 			
       
   242 /**
       
   243 */	
       
   244 inline CSqlSrvStatement::CSqlSrvStatement()
       
   245 	{
       
   246 	}
       
   247 
       
   248 /**
       
   249 Initializes CSqlSrvStatement instance.
       
   250 
       
   251 @param aDbHandle The database handle
       
   252 @param aSqlStmt 16-bit SQL statement, zero-terminated string
       
   253 
       
   254 @leave KErrNoMemory, an out of memory condition has occurred;
       
   255 	   KErrArgument, bad argument, for example - the SQL string contains more than one SQL statements;
       
   256                   Note that the function may also leave with some other database specific 
       
   257                   errors categorised as ESqlDbError.
       
   258 
       
   259 @panic SqlDb 4 In _DEBUG mode. aDbHandle is NULL.
       
   260 */	
       
   261 inline void CSqlSrvStatement::ConstructL(sqlite3* aDbHandle, const TDesC16& aSqlStmt)
       
   262 	{
       
   263 	__SQLASSERT(aDbHandle != NULL, ESqlPanicBadArgument);
       
   264 	iStmtHandle = ::StmtPrepare16L(aDbHandle, aSqlStmt);
       
   265 	DoCommonConstructL();
       
   266 	}
       
   267 	
       
   268 /**
       
   269 Initializes CSqlSrvStatement instance.
       
   270 
       
   271 @param aDbHandle The database handle
       
   272 @param aSqlStmt 8-bit SQL statement, zero-terminated string
       
   273 
       
   274 @leave KErrNoMemory, an out of memory condition has occurred;
       
   275 	   KErrArgument, bad argument, for example - the SQL string contains more than one SQL statements;
       
   276                   Note that the function may also leave with some other database specific 
       
   277                   errors categorised as ESqlDbError.
       
   278 
       
   279 @panic SqlDb 4 In _DEBUG mode. aDbHandle is NULL.
       
   280 */	
       
   281 inline void CSqlSrvStatement::ConstructL(sqlite3* aDbHandle, const TDesC8& aSqlStmt)
       
   282 	{
       
   283 	__SQLASSERT(aDbHandle != NULL, ESqlPanicBadArgument);
       
   284 	iStmtHandle = ::StmtPrepare8L(aDbHandle, aSqlStmt);
       
   285 	DoCommonConstructL();
       
   286 	}