1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sql/SRC/Client/SqlStatementImpl.h Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,177 @@
1.4 +// Copyright (c) 2005-2009 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 +// NTT DOCOMO, INC - Fix for Bug 1915 "SQL server panics when using long column type strings"
1.16 +//
1.17 +// Description:
1.18 +//
1.19 +
1.20 +#ifndef __SQLSTATEMENTIMPL_H__
1.21 +#define __SQLSTATEMENTIMPL_H__
1.22 +
1.23 +#include "SqlBufIterator.h" //TSqlBufFlat, TSqlBufRIterator, TSqlBufWIterator
1.24 +#include "SqlDatabaseImpl.h"//CSqlDatabaseImpl
1.25 +#include "SqlStmtSession.h" //RSqlStatementSession
1.26 +
1.27 +//Forward declarations
1.28 +class MStreamBuf;
1.29 +class CSqlStatementImpl;
1.30 +
1.31 +#ifdef _DEBUG
1.32 +#define LONGCOL_INVARIANT() Invariant()
1.33 +#else
1.34 +#define LONGCOL_INVARIANT()
1.35 +#endif
1.36 +
1.37 +/**
1.38 +RSqlLongColumnColl class represents a collection of long text/binary column values.
1.39 +Once a column value is added to the collection, it is guaranteed that the memory address of the
1.40 +column value will never change.
1.41 +The class was designed to solve one specific problem - to exclude the possibility that SQL clients
1.42 +may get a dangling pointer to a block of memory which already has been deleted.
1.43 +Without RSqlLongColumnColl class, it may happen for example, when client prepares a SELECT sql
1.44 +statement, where the record consists of two long binary/text column values.
1.45 +The client calls RSqlStatement::Next() to move to a record. Since the column values are long, they will
1.46 +not be transferred on the client side by the Next() call, so the client side row buffer (CSqlStatementImpl::iColumnValueBuf)
1.47 +will mark them as "not present".
1.48 +Then the client calls RSqlStatement::ColumnBinary(0, TPtrC8&) to get a pointer to the first column value.
1.49 +The row buffer is reallocated, and the column value is retrieved from the server and copied into the buffer, the client's pointer
1.50 +is set to point to the place in the row buffer, where the column value is.
1.51 +While keeping the pointer to the first column value, the client calls again RSqlStatement::ColumnBinary(1, TPtrC8&) to retrieve
1.52 +the second column value. In which case, the row buffer may get reallocated again, so the first pointer will point to a
1.53 +block of memory, which has been deleted.
1.54 +With the implementation and use of RSqlLongColumnColl class, the long column values will be kept in a different place,
1.55 +RSqlLongColumnColl's collection, where it is guaranteed that the memory address of the column value is constant.
1.56 +
1.57 +@internalComponent
1.58 +*/
1.59 +NONSHARABLE_CLASS(RSqlLongColumnColl)
1.60 + {
1.61 +public:
1.62 + struct TColumnReader
1.63 + {
1.64 + virtual TInt Read(TInt aColumnIndex, TDes8& aBuf) = 0;
1.65 + };
1.66 +
1.67 +public:
1.68 + inline RSqlLongColumnColl();
1.69 + inline void Close();
1.70 + inline void Reset();
1.71 + TInt Append(TColumnReader& aReader, TInt aColumnIndex, TInt aColumnSize);
1.72 + inline TPtrC Text(TInt aColumnIndex) const;
1.73 + inline TPtrC8 Binary(TInt aColumnIndex) const;
1.74 + inline TBool IsPresent(TInt aColumnIndex) const;
1.75 +
1.76 +private:
1.77 + inline TInt FindValue(TInt aColumnIndex) const;
1.78 +#ifdef _DEBUG
1.79 + void Invariant() const;
1.80 +#endif
1.81 +
1.82 +private:
1.83 + struct TData
1.84 + {
1.85 + static TBool Compare(const TInt* aIndex, const TData& aData);
1.86 + inline TData(TInt aIndex, HBufC8* aData);
1.87 + TInt iIndex;
1.88 + HBufC8* iData;
1.89 + };
1.90 + enum {KLongColumnCollGranularity = 8};
1.91 + RArray<TData> iValues;
1.92 +
1.93 + };
1.94 +
1.95 +template <class DES> TInt SqlCreateStatement(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const DES& aSqlStmt);
1.96 +
1.97 +/**
1.98 +CSqlStatementImpl implements RSqlStatement functionality.
1.99 +CSqlStatementImpl can prepare and execute parametrized SQL statements and SQL statements without parameters.
1.100 +CSqlStatementImpl cannot prepare and execute SQL strings containing more than one SQL statement.
1.101 +@internalComponent
1.102 +*/
1.103 +NONSHARABLE_CLASS(CSqlStatementImpl) : public CBase
1.104 + {
1.105 + template <class DES> friend TInt SqlCreateStatement(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const DES& aSqlStmt);
1.106 +
1.107 +public:
1.108 + static inline TInt New(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const TDesC16& aSqlStmt);
1.109 + static inline TInt New(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const TDesC8& aSqlStmt);
1.110 + virtual ~CSqlStatementImpl();
1.111 +
1.112 + inline TBool AtRow() const;
1.113 +
1.114 + TInt Reset();
1.115 + TInt Exec();
1.116 + void Exec(TRequestStatus& aStatus);
1.117 + TInt Next();
1.118 +
1.119 + TInt BindNull(TInt aParamIndex);
1.120 + TInt BindInt(TInt aParamIndex, TInt aParamValue);
1.121 + TInt BindInt64(TInt aParamIndex, TInt64 aParamValue);
1.122 + TInt BindReal(TInt aParamIndex, TReal aParamValue);
1.123 + TInt BindText(TInt aParamIndex, const TDesC& aParamText);
1.124 + TInt BindBinary(TInt aParamIndex, const TDesC8& aParamData);
1.125 + TInt BindZeroBlob(TInt aParamIndex, TInt aBlobSize);
1.126 +
1.127 + inline TInt ColumnCount() const;
1.128 + TSqlColumnType ColumnType(TInt aColumnIndex);
1.129 + TInt DeclaredColumnType(TInt aColumnIndex, TSqlColumnType& aColumnType);
1.130 + TInt ColumnSize(TInt aColumnIndex);
1.131 + TInt ColumnInt(TInt aColumnIndex);
1.132 + TInt64 ColumnInt64(TInt aColumnIndex);
1.133 + TReal ColumnReal(TInt aColumnIndex);
1.134 + TInt ColumnText(TInt aColumnIndex, TPtrC& aPtr);
1.135 + TInt ColumnText(TInt aColumnIndex, TDes& aDest);
1.136 + TInt ColumnBinary(TInt aColumnIndex, TPtrC8& aPtr);
1.137 + TInt ColumnBinary(TInt aColumnIndex, TDes8& aDest);
1.138 +
1.139 + TInt ColumnName(TInt aColumnIndex, TPtrC& aNameDest);
1.140 + TInt ParameterName(TInt aParamIndex, TPtrC& aNameDest);
1.141 +
1.142 + inline TInt ParamIndex(const TDesC& aParamName);
1.143 + inline TInt ColumnIndex(const TDesC& aColumnName);
1.144 +
1.145 + inline MStreamBuf* ColumnSourceL(TInt aColumnIndex);
1.146 + inline MStreamBuf* ParamSinkL(TSqlSrvFunction aFunction, TInt aParamIndex);
1.147 +
1.148 +private:
1.149 + inline CSqlStatementImpl();
1.150 + template <class DES> TInt Construct(CSqlDatabaseImpl& aDatabase, const DES& aSqlStmt);
1.151 + TInt Name2Index(const TDesC& aName, RSqlBufFlat& aNameBufFlat, TInt aCount, TSqlSrvFunction aFunction, TBool& aPresent);
1.152 + TInt Index2Name(TInt aIndex, RSqlBufFlat& aNameBufFlat, TInt aCount, TSqlSrvFunction aFunction, TBool& aPresent, TPtrC& aColName);
1.153 + TInt CheckNameBufPresent(TBool& aPresent, RSqlBufFlat& aNameBufFlat, TInt aCount, TSqlSrvFunction aFunction);
1.154 +
1.155 +private:
1.156 + RSqlStatementSession iSqlStmtSession;
1.157 + TBool iBound;
1.158 + enum TState {EUnknown, EAtRow};
1.159 + TState iState;
1.160 +
1.161 + TInt iColumnCnt;
1.162 + TBool iColumnNameBufPresent;
1.163 + RSqlBufFlat iColumnNameBuf;
1.164 + RSqlBufFlat iColumnValueBuf;
1.165 + TSqlBufRIterator iColumnValBufIt;
1.166 +
1.167 + TInt iParamCnt;
1.168 + TBool iParamNameBufPresent;
1.169 + RSqlBufFlat iParamNameBuf;
1.170 + RSqlBufFlat iParamValueBuf;
1.171 + TSqlBufWIterator iParamValBufIt;
1.172 +
1.173 + RSqlLongColumnColl iLongColumnColl;
1.174 +
1.175 + RArray<TSqlColumnType> iDeclaredColumnTypes; //Array of declared column types for current statement.
1.176 + };
1.177 +
1.178 +#include "SqlStatementImpl.inl"
1.179 +
1.180 +#endif //__SQLSTATEMENTIMPL_H__