1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sql/SRC/Server/SqlSrvStatement.inl Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,298 @@
1.4 +// Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// /////////////// HSqlSrvStmtParamBuf ////////////////////////////
1.18 +//
1.19 +//
1.20 +
1.21 +/**
1.22 +
1.23 + Creates a new HSqlSrvStmtParamBuf instance.
1.24 +
1.25 + @param aStatement A reference to the CSqlSrvStatement object, which needs the parameter data.
1.26 + @param aParamIndex Parameter index, zero based.
1.27 + @param aDataType Parameter value type - binary, text8 or text16.
1.28 + @param aBufType IPC stream buffer or a simple "bind param" buffer
1.29 +
1.30 + @return A pointer to the created HSqlSrvStmtParamBuf instance.
1.31 +
1.32 + @leave KErrNoMemory, an out of memory condition has occurred;
1.33 +
1.34 + @panic SqlDb 4 In _DEBUG mode. Parameter index negative.
1.35 +*/
1.36 +inline HSqlSrvStmtParamBuf* HSqlSrvStmtParamBuf::NewL(CSqlSrvStatement& aStatement, TInt aParamIndex,
1.37 + HSqlSrvStmtParamBuf::TDataType aDataType, HSqlSrvStmtParamBuf::TBufType aBufType)
1.38 + {
1.39 + __ASSERT_DEBUG(aParamIndex >= 0, __SQLPANIC2(ESqlPanicBadArgument));
1.40 + HSqlSrvStmtParamBuf* self = new (ELeave) HSqlSrvStmtParamBuf(aStatement, aParamIndex, aDataType, aBufType);
1.41 + self->PushL();
1.42 + self->ConstructL();
1.43 + CleanupStack::Pop();
1.44 + return self;
1.45 + }
1.46 +
1.47 +/**
1.48 +Resets the current HSqlSrvStmtParamBuf instance.
1.49 +
1.50 +The internal buffer will be resized, if its capacity is bigger than HSqlSrvStmtParamBuf::EExpandSize * 2 bytes.
1.51 +
1.52 +@param aDataType Parameter value type - binary, text8 or text16.
1.53 +@param aBufType IPC stream buffer or a simple "bind param" buffer
1.54 +
1.55 +@panic SqlDb 2 In _DEBUG mode. Invalid HSqlSrvStmtParamBuf object.
1.56 +*/
1.57 +inline void HSqlSrvStmtParamBuf::Reset(HSqlSrvStmtParamBuf::TDataType aDataType, HSqlSrvStmtParamBuf::TBufType aBufType)
1.58 + {
1.59 + __ASSERT_DEBUG(iBuf != NULL, __SQLPANIC(ESqlPanicInvalidObj));
1.60 +
1.61 + iStatementFinalized = EFalse;
1.62 + iAlive = ETrue;
1.63 + iDataType = aDataType;
1.64 + iBufType = aBufType;
1.65 + iSynchDone = EFalse;
1.66 +
1.67 + iBuf->Delete(0, iBuf->Size());
1.68 + if(iBuf->Capacity() > (HSqlSrvStmtParamBuf::EExpandSize * 2))
1.69 + {
1.70 + TRAPD(err, iBuf->SetReserveL(HSqlSrvStmtParamBuf::EExpandSize * 2));//the buffer size is minimized, the call can't fail
1.71 + __ASSERT_ALWAYS(err == KErrNone, __SQLPANIC(ESqlPanicInternalError));
1.72 + }
1.73 + Set(*iBuf, 0, MStreamBuf::ERead | MStreamBuf::EWrite);
1.74 + }
1.75 +
1.76 +/**
1.77 +Stores the aData into the buffer, from position 0.
1.78 +This function is used when the buffer type is not IPC.
1.79 +
1.80 +@param aData The data to be stored into the buffer
1.81 +
1.82 +@leave KErrNoMemory, an out of memory condition has occurred;
1.83 +
1.84 +@panic SqlDb 2 In _DEBUG mode. Invalid HSqlSrvStmtParamBuf object.
1.85 +@panic SqlDb 7 In _DEBUG mode. The buffer type is not HSqlSrvStmtParamBuf::EBufSimpleBind
1.86 +*/
1.87 +inline const TPtrC8 HSqlSrvStmtParamBuf::SetDataL(const TDesC8& aData)
1.88 + {
1.89 + __ASSERT_DEBUG(iBuf != NULL, __SQLPANIC(ESqlPanicInvalidObj));
1.90 + __ASSERT_DEBUG(iBufType == HSqlSrvStmtParamBuf::EBufSimpleBind, __SQLPANIC(ESqlPanicInternalError));
1.91 + iBuf->ResizeL(aData.Length());
1.92 + iBuf->Write(0, aData);
1.93 + //If the size is 0, then return KNullDesC8, where an empty string is hold, not a null one ("" instead of NULL)
1.94 + // was return iBuf->Size() == 0 ? KNullDesC8() : iBuf->Ptr(0);
1.95 + if (iBuf->Size() == 0)
1.96 + {
1.97 + return KNullDesC8();
1.98 + }
1.99 + return iBuf->Ptr(0);
1.100 + }
1.101 +
1.102 +/**
1.103 +Returns a 8-bit pointer to the parameter data.
1.104 +
1.105 +@return 8-bit pointer to the parameter data.
1.106 +
1.107 +@panic SqlDb 2 In _DEBUG mode. Invalid HSqlSrvStmtParamBuf object.
1.108 +*/
1.109 +inline const TPtrC8 HSqlSrvStmtParamBuf::Data() const
1.110 + {
1.111 + __ASSERT_DEBUG(iBuf != NULL, __SQLPANIC(ESqlPanicInvalidObj));
1.112 + //If the size is 0, then return KNullDesC8, where an empty string is hold, not a null one ("" instead of NULL)
1.113 + // was return iBuf->Size() == 0 ? KNullDesC8() : iBuf->Ptr(0);
1.114 + if (iBuf->Size() == 0)
1.115 + {
1.116 + return KNullDesC8();
1.117 + }
1.118 + return iBuf->Ptr(0);
1.119 + }
1.120 +
1.121 +/**
1.122 +@return Buffered parameter value type - binary, text8 or text16.
1.123 +*/
1.124 +inline HSqlSrvStmtParamBuf::TDataType HSqlSrvStmtParamBuf::DataType() const
1.125 + {
1.126 + return iDataType;
1.127 + }
1.128 +
1.129 +/**
1.130 +Returns the parameter index.
1.131 +
1.132 +@panic SqlDb 7 In _DEBUG mode. Negative parameter index.
1.133 +
1.134 +@return Parameter index, zero based.
1.135 +*/
1.136 +inline TInt HSqlSrvStmtParamBuf::ParamIndex() const
1.137 + {
1.138 + __ASSERT_DEBUG(iParamIndex >= 0, __SQLPANIC(ESqlPanicInternalError));
1.139 + return iParamIndex;
1.140 + }
1.141 +
1.142 +/**
1.143 +Initializes the HSqlSrvStmtParamBuf instance data members.
1.144 +
1.145 +@param aStatement A reference to the CSqlSrvStatement object, which needs the parameter data.
1.146 +@param aParameterIndex Parameter index, zero based.
1.147 +@param aDataType Parameter value type - binary, text8 or text16.
1.148 +@param aBufType IPC stream buffer or a simple "bind param" buffer
1.149 +*/
1.150 +inline HSqlSrvStmtParamBuf::HSqlSrvStmtParamBuf(CSqlSrvStatement& aStatement, TInt aParamIndex,
1.151 + HSqlSrvStmtParamBuf::TDataType aDataType, HSqlSrvStmtParamBuf::TBufType aBufType) :
1.152 + iStatement(aStatement),
1.153 + iBuf(NULL),
1.154 + iParamIndex(aParamIndex),
1.155 + iStatementFinalized(EFalse),
1.156 + iAlive(EFalse),
1.157 + iDataType(aDataType),
1.158 + iBufType(aBufType),
1.159 + iSynchDone(EFalse)
1.160 + {
1.161 + }
1.162 +
1.163 +/**
1.164 +HSqlSrvStmtParamBuf - second phase construction.
1.165 +Constructs the internal CFlatBuf object.
1.166 +
1.167 +@leave KErrNoMemory, an out of memory condition has occurred;
1.168 +*/
1.169 +inline void HSqlSrvStmtParamBuf::ConstructL()
1.170 + {
1.171 + iBuf = CBufFlat::NewL(EExpandSize);
1.172 + Set(*iBuf, 0, MStreamBuf::ERead | MStreamBuf::EWrite);
1.173 + iAlive = ETrue;
1.174 + }
1.175 +
1.176 +//////////////////////////////////////////////////////////////////////////////////////////////////////
1.177 +///////////////////////////// CSqlSrvStatement class ////////////////////////////////////////////
1.178 +//////////////////////////////////////////////////////////////////////////////////////////////////////
1.179 +
1.180 +/**
1.181 +Executes the SQL statement moving the cursor to the next row if there is a row available.
1.182 +
1.183 +@return KSqlErrStmtExpired, statement expired (if new functions or collating sequences are
1.184 + registered or if an authorizer function is added or changed);
1.185 + KErrNone, the operation completed successfully;
1.186 + KSqlAtRow, the next record data is ready for processing by the caller;
1.187 + KSqlAtEnd, no more records;
1.188 + KSqlErrBusy, database file is locked;
1.189 + KSqlErrGeneral, run-time error. Next() should not be called anymore;
1.190 + KSqlErrMisuse, Next() called after KSqlAtEnd or KSqlErrGeneral returned by the previous Next() call;
1.191 + KErrNoMemory, an out of memory condition has occurred. The statement will be reset.
1.192 +
1.193 +@panic SqlDb 2 In _DEBUG mode. Invalid (not created) CSqlSrvStatement object.
1.194 +@panic SqlDb 7 In _DEBUG mode. SQLITE internal error. (SQLITE_ERROR, followed by a sqlite3_reset(), which returns SQLITE_OK)
1.195 +*/
1.196 +inline TInt CSqlSrvStatement::Next()
1.197 + {
1.198 + __ASSERT_DEBUG(iStmtHandle != NULL, __SQLPANIC(ESqlPanicInvalidObj));
1.199 + TInt err = ::StmtNext(iStmtHandle);
1.200 + iBufFlatType = static_cast <TSqlBufFlatType> (-1);
1.201 + iBufFlat.ResetAndMinimize();
1.202 + return err;
1.203 + }
1.204 +
1.205 +/**
1.206 +Resets the prepared SQL statement to its initial state and makes it ready to be executed again.
1.207 +
1.208 +Any SQL statement parameters that had values bound to them, retain their values.
1.209 +
1.210 +@return KErrNone, the operation completed successfully;
1.211 + KSqlErrStmtExpired, Statement expired (if new functions or collating sequences are
1.212 + registered or if an authorizer function is added or changed)
1.213 +
1.214 +@panic SqlDb 2 In _DEBUG mode. Invalid (not created) CSqlSrvStatement object.
1.215 +*/
1.216 +inline TInt CSqlSrvStatement::Reset()
1.217 + {
1.218 + __ASSERT_DEBUG(iStmtHandle != NULL, __SQLPANIC(ESqlPanicInvalidObj));
1.219 + iBufFlatType = static_cast <TSqlBufFlatType> (-1);
1.220 + iBufFlat.ResetAndMinimize();
1.221 + return ::StmtReset(iStmtHandle);
1.222 + }
1.223 +
1.224 +/**
1.225 +Executes the prepared SQL statement.
1.226 +
1.227 +@return KErrNone, the operation completed successfully;
1.228 + KSqlErrStmtExpired, statement expired (if new functions or collating sequences are
1.229 + registered or if an authorizer function is added or changed);
1.230 + KErrNoMemory, an out of memory condition has occurred. The statement will be reset.
1.231 +
1.232 +@panic SqlDb 2 In _DEBUG mode. Invalid (not created) CSqlSrvStatement object.
1.233 +@panic SqlDb 7 In _DEBUG mode. SQLITE internal error. (SQLITE_ERROR, followed by a sqlite3_reset(), which returns SQLITE_OK)
1.234 +*/
1.235 +inline TInt CSqlSrvStatement::Exec()
1.236 + {
1.237 + __ASSERT_DEBUG(iStmtHandle != NULL, __SQLPANIC(ESqlPanicInvalidObj));
1.238 + TInt err = ::StmtExec(iStmtHandle);
1.239 + iBufFlatType = static_cast <TSqlBufFlatType> (-1);
1.240 + iBufFlat.ResetAndMinimize();
1.241 + return err;
1.242 + }
1.243 +
1.244 +/**
1.245 +@panic SqlDb 2 In _DEBUG mode. Invalid (not created) CSqlSrvStatement object.
1.246 +*/
1.247 +inline const RSqlBufFlat& CSqlSrvStatement::BufFlatL(TSqlBufFlatType aWhat) const
1.248 + {
1.249 + __ASSERT_DEBUG(iStmtHandle != NULL, __SQLPANIC(ESqlPanicInvalidObj));
1.250 + if(aWhat != iBufFlatType)
1.251 + {
1.252 + __SQLLEAVE(KErrArgument);
1.253 + }
1.254 + return iBufFlat;
1.255 + }
1.256 +
1.257 +/**
1.258 +*/
1.259 +inline CSqlSrvStatement::CSqlSrvStatement()
1.260 + {
1.261 + }
1.262 +
1.263 +/**
1.264 +Initializes CSqlSrvStatement instance.
1.265 +
1.266 +@param aDbHandle The database handle
1.267 +@param aSqlStmt 16-bit SQL statement, zero-terminated string
1.268 +
1.269 +@leave KErrNoMemory, an out of memory condition has occurred;
1.270 + KErrArgument, bad argument, for example - the SQL string contains more than one SQL statements;
1.271 + Note that the function may also leave with some other database specific
1.272 + errors categorised as ESqlDbError.
1.273 +
1.274 +@panic SqlDb 4 In _DEBUG mode. aDbHandle is NULL.
1.275 +*/
1.276 +inline void CSqlSrvStatement::ConstructL(sqlite3* aDbHandle, const TDesC16& aSqlStmt)
1.277 + {
1.278 + __ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC(ESqlPanicBadArgument));
1.279 + iStmtHandle = ::StmtPrepare16L(aDbHandle, aSqlStmt);
1.280 + DoCommonConstructL();
1.281 + }
1.282 +
1.283 +/**
1.284 +Initializes CSqlSrvStatement instance.
1.285 +
1.286 +@param aDbHandle The database handle
1.287 +@param aSqlStmt 8-bit SQL statement, zero-terminated string
1.288 +
1.289 +@leave KErrNoMemory, an out of memory condition has occurred;
1.290 + KErrArgument, bad argument, for example - the SQL string contains more than one SQL statements;
1.291 + Note that the function may also leave with some other database specific
1.292 + errors categorised as ESqlDbError.
1.293 +
1.294 +@panic SqlDb 4 In _DEBUG mode. aDbHandle is NULL.
1.295 +*/
1.296 +inline void CSqlSrvStatement::ConstructL(sqlite3* aDbHandle, const TDesC8& aSqlStmt)
1.297 + {
1.298 + __ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC(ESqlPanicBadArgument));
1.299 + iStmtHandle = ::StmtPrepare8L(aDbHandle, aSqlStmt);
1.300 + DoCommonConstructL();
1.301 + }