persistentstorage/sql/SRC/Common/SqlBufIterator.cpp
changeset 0 08ec8eefde2f
child 15 fcc16690f446
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 //
       
    15 
       
    16 #include <e32math.h>		//Math class
       
    17 #include <s32mem.h>			//TMemBuf
       
    18 #include "SqlBufIterator.h"
       
    19 
       
    20 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    21 ////////////////////////          TSqlBufIterator class               /////////////////////////////////////
       
    22 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    23 
       
    24 /**
       
    25 @return Represents the content of the current flat buffer field as integer value.
       
    26 */
       
    27 inline TInt TSqlBufRIterator::AsInt() const
       
    28 	{
       
    29 	__SQLASSERT(iCurrent >= iBegin && iCurrent < iEnd, ESqlPanicInternalError);
       
    30 	return *reinterpret_cast <const TInt32*> (reinterpret_cast <const TUint8*> (iBegin) + iCurrent->iPos);
       
    31 	}
       
    32 
       
    33 /**
       
    34 @return Represents the content of the current flat buffer field as 64 bit integer value.
       
    35 */
       
    36 inline TInt64 TSqlBufRIterator::AsInt64() const
       
    37 	{
       
    38 	__SQLASSERT(iCurrent >= iBegin && iCurrent < iEnd, ESqlPanicInternalError);
       
    39 	return *reinterpret_cast <const TInt64*> (reinterpret_cast <const TUint8*> (iBegin) + iCurrent->iPos);
       
    40 	}
       
    41 
       
    42 /**
       
    43 @return Represents the content of the current flat buffer field as real value.
       
    44 */
       
    45 inline TReal TSqlBufRIterator::AsReal() const
       
    46 	{
       
    47 	__SQLASSERT(iCurrent >= iBegin && iCurrent < iEnd, ESqlPanicInternalError);
       
    48 	return *reinterpret_cast <const TReal*> (reinterpret_cast <const TUint8*> (iBegin) + iCurrent->iPos);
       
    49 	}
       
    50 
       
    51 /**
       
    52 @return Represents the content of the current flat buffer field as integer value.
       
    53 		If the current flat buffer field type does not refer to an integer, then 
       
    54 		the function will do a data conversion as described in the table which can be found
       
    55 		in SqlBufIterator.h file.
       
    56 */
       
    57 TInt TSqlBufRIterator::Int() const
       
    58 	{
       
    59 	__SQLASSERT(iCurrent >= iBegin && iCurrent < iEnd, ESqlPanicInternalError);
       
    60 	switch(Type())
       
    61 		{
       
    62 		case ESqlInt64:
       
    63 			{
       
    64 			TInt64 val = AsInt64();
       
    65 			return val == (TInt)val ? (TInt)val : (val < KMinTInt ? KMinTInt : KMaxTInt);
       
    66 			}
       
    67 		case ESqlReal:
       
    68 			{
       
    69 			TReal roundVal;
       
    70 			TInt err = Math::Round(roundVal, AsReal(), 0);
       
    71 			if(err != KErrNone)
       
    72 				{
       
    73 				return KMinTInt;
       
    74 				}
       
    75 			TRealX val(roundVal);
       
    76 			return static_cast <TInt> (val);
       
    77 			}
       
    78 		case ESqlNull:
       
    79 		case ESqlText:
       
    80 		case ESqlBinary:
       
    81 			return 0;
       
    82 		case ESqlZeroBlob:
       
    83 			return AsInt();
       
    84 		default:
       
    85 			return AsInt();
       
    86 		}
       
    87 	}
       
    88 
       
    89 /**
       
    90 @return Represents the content of the current flat buffer field as 64 bit integer value.
       
    91 		If the current flat buffer field type does not refer to a 64 bit integer, then 
       
    92 		the function will do a data conversion as described in the table which can be found
       
    93 		in SqlBufIterator.h file.
       
    94 */
       
    95 TInt64 TSqlBufRIterator::Int64() const
       
    96 	{
       
    97 	__SQLASSERT(iCurrent >= iBegin && iCurrent < iEnd, ESqlPanicInternalError);
       
    98 	switch(Type())
       
    99 		{
       
   100 		case ESqlInt:
       
   101 		case ESqlZeroBlob:
       
   102 			return AsInt();
       
   103 		case ESqlReal:
       
   104 			{
       
   105 			TReal roundVal;
       
   106 			TInt err = Math::Round(roundVal, AsReal(), 0);
       
   107 			if(err != KErrNone)
       
   108 				{
       
   109 				return KMinTInt64;
       
   110 				}
       
   111 			TRealX val(roundVal);
       
   112 			return static_cast <TInt64> (val);
       
   113 			}
       
   114 		case ESqlNull:
       
   115 		case ESqlText:
       
   116 		case ESqlBinary:
       
   117 			return 0;
       
   118 		default:
       
   119 			return AsInt64();
       
   120 		}
       
   121 	}
       
   122 
       
   123 /**
       
   124 @return Represents the content of the current flat buffer field as real value.
       
   125 		If the current flat buffer field type does not refer to a real, then 
       
   126 		the function will do a data conversion as described in the table which can be found
       
   127 		in SqlBufIterator.h file.
       
   128 */
       
   129 TReal TSqlBufRIterator::Real() const
       
   130 	{
       
   131 	__SQLASSERT(iCurrent >= iBegin && iCurrent < iEnd, ESqlPanicInternalError);
       
   132 	switch(Type())
       
   133 		{
       
   134 		case ESqlInt:
       
   135 		case ESqlZeroBlob:
       
   136 			{
       
   137 			TRealX val(AsInt());	
       
   138 			return static_cast <TReal> (val);
       
   139 			}
       
   140 		case ESqlInt64:
       
   141 			{
       
   142 			TRealX val(AsInt64());	
       
   143 			return static_cast <TReal> (val);
       
   144 			}
       
   145 		case ESqlNull:
       
   146 		case ESqlText:
       
   147 		case ESqlBinary:
       
   148 			return 0.0;
       
   149 		default:
       
   150 			return AsReal();
       
   151 		}
       
   152 	}
       
   153 
       
   154 /**
       
   155 @return Represents the content of the current flat buffer field as binary (8 bit) descriptor.
       
   156 		If the current flat buffer field type does not refer to a binary block of data, then 
       
   157 		the function will do a data conversion as described in the table which can be found
       
   158 		in SqlBufIterator.h file.
       
   159 */
       
   160 TPtrC8 TSqlBufRIterator::Binary() const
       
   161 	{
       
   162 	__SQLASSERT(iCurrent >= iBegin && iCurrent < iEnd, ESqlPanicInternalError);
       
   163 	TInt size = Size();
       
   164 	if(Type() != ESqlBinary || size == 0)
       
   165 		{
       
   166 		return 	TPtrC8();
       
   167 		}
       
   168 	return TPtrC8(reinterpret_cast <const TUint8*> (iBegin) + iCurrent->iPos, size);
       
   169 	}
       
   170 
       
   171 /**
       
   172 @return Represents the content of the current flat buffer field as text (16 bit) descriptor.
       
   173 		If the current flat buffer field type does not refer to a text block of data, then 
       
   174 		the function will do a data conversion as described in the table which can be found
       
   175 		in SqlBufIterator.h file.
       
   176 */
       
   177 TPtrC16 TSqlBufRIterator::Text() const
       
   178 	{
       
   179 	__SQLASSERT(iCurrent >= iBegin && iCurrent < iEnd, ESqlPanicInternalError);
       
   180 	TInt size = Size();
       
   181 	if(Type() != ESqlText || size == 0)
       
   182 		{
       
   183 		return 	TPtrC16();
       
   184 		}
       
   185 	return TPtrC16(reinterpret_cast <const TUint16*> (reinterpret_cast <const TUint8*> (iBegin) + iCurrent->iPos), size);
       
   186 	}
       
   187 
       
   188 /**
       
   189 An instance of the class is used to get a read-only access to the content of a text or binary column
       
   190 via a stream object.
       
   191 
       
   192 @internalComponent
       
   193 */
       
   194 class HReadOnlyBuf : public TMemBuf
       
   195 	{
       
   196 public:
       
   197 	static HReadOnlyBuf* NewL(const TUint8* aFrom, TInt aLen)
       
   198 		{
       
   199 		__SQLASSERT(aLen >= 0, ESqlPanicBadArgument);
       
   200    		HReadOnlyBuf* self = new (ELeave) HReadOnlyBuf;
       
   201 		TUint8* p = const_cast <TUint8*> (aFrom);
       
   202 		self->Set(p, p + aLen, MStreamBuf::ERead);
       
   203 		return self;
       
   204 		}
       
   205 		
       
   206 private:
       
   207 	virtual void DoRelease()
       
   208 		{
       
   209 		delete this;	
       
   210 		}
       
   211 	
       
   212 };
       
   213 
       
   214 /**
       
   215 @return Represents the content of a text or a binary field as a stream of bytes.
       
   216 
       
   217 @leave KErrNoMemory, out of memory condition has occured;
       
   218 */	
       
   219 MStreamBuf* TSqlBufRIterator::StreamL() const
       
   220 	{
       
   221 	__SQLASSERT(iCurrent >= iBegin && iCurrent < iEnd, ESqlPanicInternalError);
       
   222 	__SQLASSERT(::IsSequenceSqlType(Type()), ESqlPanicInternalError);
       
   223 	return HReadOnlyBuf::NewL(reinterpret_cast <const TUint8*> (iBegin) + iCurrent->iPos, iCurrent->Size());
       
   224 	}