os/persistentdata/persistentstorage/sql/SRC/Client/SqlStatementImpl.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
// NTT DOCOMO, INC - Fix for Bug 1915 "SQL server panics when using long column type strings"
sl@0
    13
//
sl@0
    14
// Description:
sl@0
    15
//
sl@0
    16
sl@0
    17
#ifndef __SQLSTATEMENTIMPL_H__
sl@0
    18
#define __SQLSTATEMENTIMPL_H__
sl@0
    19
sl@0
    20
#include "SqlBufIterator.h"	//TSqlBufFlat, TSqlBufRIterator, TSqlBufWIterator
sl@0
    21
#include "SqlDatabaseImpl.h"//CSqlDatabaseImpl
sl@0
    22
#include "SqlStmtSession.h"	//RSqlStatementSession
sl@0
    23
sl@0
    24
//Forward declarations
sl@0
    25
class MStreamBuf;
sl@0
    26
class CSqlStatementImpl;
sl@0
    27
sl@0
    28
#ifdef _DEBUG
sl@0
    29
#define LONGCOL_INVARIANT()	Invariant()
sl@0
    30
#else
sl@0
    31
#define LONGCOL_INVARIANT()
sl@0
    32
#endif
sl@0
    33
sl@0
    34
/**
sl@0
    35
RSqlLongColumnColl class represents a collection of long text/binary column values.
sl@0
    36
Once a column value is added to the collection, it is guaranteed that the memory address of the 
sl@0
    37
column value will never change.
sl@0
    38
The class was designed to solve one specific problem - to exclude the possibility that SQL clients
sl@0
    39
may get a dangling pointer to a block of memory which already has been deleted.
sl@0
    40
Without RSqlLongColumnColl class, it may happen for example, when client prepares a SELECT sql
sl@0
    41
statement, where the record consists of two long binary/text column values.
sl@0
    42
The client calls RSqlStatement::Next() to move to a record. Since the column values are long, they will
sl@0
    43
not be transferred on the client side by the Next() call, so the client side row buffer (CSqlStatementImpl::iColumnValueBuf)
sl@0
    44
will mark them as "not present". 
sl@0
    45
Then the client calls RSqlStatement::ColumnBinary(0, TPtrC8&) to get a pointer to the first column value.
sl@0
    46
The row buffer is reallocated, and the column value is retrieved from the server and copied into the buffer, the client's pointer
sl@0
    47
is set to point to the place in the row buffer, where the column value is.
sl@0
    48
While keeping the pointer to the first column value, the client calls again RSqlStatement::ColumnBinary(1, TPtrC8&) to retrieve
sl@0
    49
the second column value. In which case, the row buffer may get reallocated again, so the first pointer will point to a 
sl@0
    50
block of memory, which has been deleted.
sl@0
    51
With the implementation and use of RSqlLongColumnColl class, the long column values will be kept in a different place,
sl@0
    52
RSqlLongColumnColl's collection, where it is guaranteed that the memory address of the column value is constant.
sl@0
    53
sl@0
    54
@internalComponent
sl@0
    55
*/
sl@0
    56
NONSHARABLE_CLASS(RSqlLongColumnColl)
sl@0
    57
	{
sl@0
    58
public:	
sl@0
    59
	struct TColumnReader
sl@0
    60
		{
sl@0
    61
		virtual TInt Read(TInt aColumnIndex, TDes8& aBuf) = 0;
sl@0
    62
		};
sl@0
    63
		
sl@0
    64
public:	
sl@0
    65
	inline RSqlLongColumnColl();
sl@0
    66
	inline void Close();
sl@0
    67
	inline void Reset();
sl@0
    68
	TInt Append(TColumnReader& aReader, TInt aColumnIndex, TInt aColumnSize);
sl@0
    69
	inline TPtrC Text(TInt aColumnIndex) const;
sl@0
    70
	inline TPtrC8 Binary(TInt aColumnIndex) const;
sl@0
    71
	inline TBool IsPresent(TInt aColumnIndex) const;
sl@0
    72
sl@0
    73
private:
sl@0
    74
	inline TInt FindValue(TInt aColumnIndex) const;
sl@0
    75
#ifdef _DEBUG
sl@0
    76
	void Invariant() const;	
sl@0
    77
#endif
sl@0
    78
	
sl@0
    79
private:
sl@0
    80
	struct TData
sl@0
    81
		{
sl@0
    82
		static TBool Compare(const TInt* aIndex, const TData& aData);
sl@0
    83
		inline TData(TInt aIndex, HBufC8* aData);
sl@0
    84
		TInt	iIndex;
sl@0
    85
		HBufC8*	iData;
sl@0
    86
		};
sl@0
    87
	enum {KLongColumnCollGranularity = 8};
sl@0
    88
	RArray<TData>	iValues;
sl@0
    89
	
sl@0
    90
	};
sl@0
    91
sl@0
    92
template <class DES> TInt SqlCreateStatement(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const DES& aSqlStmt);
sl@0
    93
sl@0
    94
/**
sl@0
    95
CSqlStatementImpl implements RSqlStatement functionality.
sl@0
    96
CSqlStatementImpl can prepare and execute parametrized SQL statements and SQL statements without parameters.
sl@0
    97
CSqlStatementImpl cannot prepare and execute SQL strings containing more than one SQL statement.
sl@0
    98
@internalComponent
sl@0
    99
*/
sl@0
   100
NONSHARABLE_CLASS(CSqlStatementImpl) : public CBase
sl@0
   101
	{
sl@0
   102
	template <class DES> friend TInt SqlCreateStatement(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const DES& aSqlStmt);
sl@0
   103
	
sl@0
   104
public:
sl@0
   105
	static inline TInt New(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const TDesC16& aSqlStmt);
sl@0
   106
	static inline TInt New(CSqlStatementImpl*& aImpl, CSqlDatabaseImpl& aDatabase, const TDesC8& aSqlStmt);
sl@0
   107
	virtual ~CSqlStatementImpl();
sl@0
   108
	
sl@0
   109
	inline TBool AtRow() const;
sl@0
   110
	
sl@0
   111
	TInt Reset();
sl@0
   112
	TInt Exec();
sl@0
   113
	void Exec(TRequestStatus& aStatus);
sl@0
   114
	TInt Next();
sl@0
   115
	
sl@0
   116
	TInt BindNull(TInt aParamIndex);
sl@0
   117
	TInt BindInt(TInt aParamIndex, TInt aParamValue);
sl@0
   118
	TInt BindInt64(TInt aParamIndex, TInt64 aParamValue);
sl@0
   119
	TInt BindReal(TInt aParamIndex, TReal aParamValue);
sl@0
   120
	TInt BindText(TInt aParamIndex, const TDesC& aParamText);
sl@0
   121
	TInt BindBinary(TInt aParamIndex, const TDesC8& aParamData);
sl@0
   122
	TInt BindZeroBlob(TInt aParamIndex, TInt aBlobSize);
sl@0
   123
sl@0
   124
	inline TInt ColumnCount() const;
sl@0
   125
	TSqlColumnType ColumnType(TInt aColumnIndex);
sl@0
   126
	TInt DeclaredColumnType(TInt aColumnIndex, TSqlColumnType& aColumnType);
sl@0
   127
	TInt ColumnSize(TInt aColumnIndex);
sl@0
   128
	TInt ColumnInt(TInt aColumnIndex);
sl@0
   129
	TInt64 ColumnInt64(TInt aColumnIndex);
sl@0
   130
	TReal ColumnReal(TInt aColumnIndex);
sl@0
   131
	TInt ColumnText(TInt aColumnIndex, TPtrC& aPtr);
sl@0
   132
	TInt ColumnText(TInt aColumnIndex, TDes& aDest);
sl@0
   133
	TInt ColumnBinary(TInt aColumnIndex, TPtrC8& aPtr);
sl@0
   134
	TInt ColumnBinary(TInt aColumnIndex, TDes8& aDest);
sl@0
   135
sl@0
   136
	TInt ColumnName(TInt aColumnIndex, TPtrC& aNameDest);
sl@0
   137
	TInt ParameterName(TInt aParamIndex, TPtrC& aNameDest);
sl@0
   138
sl@0
   139
	inline TInt ParamIndex(const TDesC& aParamName);
sl@0
   140
	inline TInt ColumnIndex(const TDesC& aColumnName);
sl@0
   141
		
sl@0
   142
	inline MStreamBuf* ColumnSourceL(TInt aColumnIndex);
sl@0
   143
	inline MStreamBuf* ParamSinkL(TSqlSrvFunction aFunction, TInt aParamIndex);
sl@0
   144
sl@0
   145
private:
sl@0
   146
	inline CSqlStatementImpl();
sl@0
   147
	template <class DES> TInt Construct(CSqlDatabaseImpl& aDatabase, const DES& aSqlStmt);
sl@0
   148
	TInt Name2Index(const TDesC& aName, RSqlBufFlat& aNameBufFlat, TInt aCount, TSqlSrvFunction aFunction, TBool& aPresent);
sl@0
   149
	TInt Index2Name(TInt aIndex, RSqlBufFlat& aNameBufFlat, TInt aCount, TSqlSrvFunction aFunction, TBool& aPresent, TPtrC& aColName);
sl@0
   150
	TInt CheckNameBufPresent(TBool& aPresent, RSqlBufFlat& aNameBufFlat, TInt aCount, TSqlSrvFunction aFunction);
sl@0
   151
	
sl@0
   152
private:
sl@0
   153
	RSqlStatementSession	iSqlStmtSession;
sl@0
   154
	TBool					iBound;
sl@0
   155
	enum TState				{EUnknown, EAtRow};
sl@0
   156
	TState					iState;
sl@0
   157
	
sl@0
   158
	TInt 					iColumnCnt;
sl@0
   159
	TBool					iColumnNameBufPresent;
sl@0
   160
	RSqlBufFlat				iColumnNameBuf;
sl@0
   161
	RSqlBufFlat				iColumnValueBuf;
sl@0
   162
	TSqlBufRIterator		iColumnValBufIt;
sl@0
   163
	
sl@0
   164
	TInt 					iParamCnt;
sl@0
   165
	TBool					iParamNameBufPresent;
sl@0
   166
	RSqlBufFlat				iParamNameBuf;
sl@0
   167
	RSqlBufFlat				iParamValueBuf;
sl@0
   168
	TSqlBufWIterator		iParamValBufIt;
sl@0
   169
	
sl@0
   170
	RSqlLongColumnColl		iLongColumnColl;
sl@0
   171
	
sl@0
   172
	RArray<TSqlColumnType>	iDeclaredColumnTypes;	//Array of declared column types for current statement.
sl@0
   173
	};
sl@0
   174
sl@0
   175
#include "SqlStatementImpl.inl"
sl@0
   176
sl@0
   177
#endif //__SQLSTATEMENTIMPL_H__