First public contribution.
1 // Copyright (c) 1998-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // SQL Literal value and node classes
20 // internalising literal strings, overload selectors
22 inline HBufC8* Alloc8L(const TDesC8& aDes)
23 {return aDes.AllocL();}
24 inline HBufC8* Alloc8L(const TDesC16& aDes)
25 {HBufC8* cell=HBufC8::NewL(aDes.Length());cell->Des().Copy(aDes);return cell;}
26 inline HBufC16* Alloc16L(const TDesC16& aDes)
27 {return aDes.AllocL();}
28 inline HBufC16* Alloc16L(const TDesC8& aDes)
29 {HBufC16* cell=HBufC16::NewL(aDes.Length());cell->Des().Copy(aDes);return cell;}
31 template <class B,class T>
32 inline B* DoInternString(B* aBuf,T*)
34 const T* base=aBuf->Ptr();
36 const T* end=base+aBuf->Length();
53 aBuf->Des().SetLength(to-base);
58 inline B* InternString(B* aBuf)
59 {return DoInternString(aBuf,PtrType(*aBuf));}
63 void RSqlLiteral::Close()
65 // Free any allocated cell
71 User::Free(iVal.iAlloc);
75 void RSqlLiteral::ToInt32L()
77 // Convert a numeric type to Int32
81 const TInt64& v=iVal.iInt64;
83 TInt32 h = I64HIGH(v);
84 if (h==(l>>31)) // domain check, in signed 32-bit range
90 __LEAVE(KErrOverflow); // cannot convert
93 void RSqlLiteral::ToUint32L()
95 // Convert a numeric type to Uint32
99 const TInt64& v=iVal.iInt64;
100 if (I64HIGH(v)==0) // domain check, in unsigned 32-bit range
102 iVal.iUint32 = I64LOW(v);
106 __LEAVE(KErrOverflow); // cannot convert
109 void RSqlLiteral::ToInt64L()
111 // Convert a numeric type to int64
114 __ASSERT(IsBasic()); // only apply conversions once
118 iVal.iInt64() = static_cast<TInt64>(Real64());
122 __LEAVE(KErrGeneral);
125 void RSqlLiteral::ToReal64L()
127 // Convert a numeric type to real64
130 __ASSERT(IsBasic()); // only apply conversions once
134 iVal.iReal64=I64REAL(Int64());
138 __LEAVE(KErrGeneral);
141 void RSqlLiteral::ToReal32L()
143 // Convert a numeric type to real32
147 __LEAVE_IF_ERROR(TRealX(iVal.iReal64).GetTReal(iVal.iReal32));
151 void RSqlLiteral::ToTimeL()
153 // Ensure we are a time
156 __ASSERT(IsBasic()); // only apply conversions once
158 __LEAVE(KErrGeneral); // type mismatch
161 void RSqlLiteral::ToText8L()
163 // Convert a ptr to a text8
166 __ASSERT(IsBasic()); // only apply conversions once
169 iVal.iAlloc=InternString(Alloc8L(DesC()));
173 __LEAVE(KErrGeneral); // type mismatch
176 void RSqlLiteral::ToText16L()
178 // Convert a ptr to a text8
181 __ASSERT(IsBasic()); // only apply conversions once
184 iVal.iAlloc=InternString(Alloc16L(DesC()));
188 __LEAVE(KErrGeneral); // type mismatch
191 void RSqlLiteral::ToBlobL()
193 // Convert a hex encoded ptr to a blob
196 __ASSERT(IsBasic()); // only apply conversions once
200 HBufC* buf = Alloc16L(DesC());
201 CleanupStack::PushL(buf);
202 HexDecodeL(*buf, blobBuf);
203 CleanupStack::PopAndDestroy(buf);
204 iVal.iAlloc=Alloc8L(blobBuf);
209 __LEAVE(KErrGeneral); // type mismatch
214 // class CSqlLiteralNode
216 CSqlLiteralNode::CSqlLiteralNode(TType aType,const TDesC& aColumn,const RSqlLiteral& aLiteral)
217 :CSqlBoundNode(aType,aColumn),iLiteral(aLiteral)
220 CSqlLiteralNode::~CSqlLiteralNode()
225 void CSqlLiteralNode::BindL(const RDbTableRow& aSource)
227 CSqlBoundNode::BindL(aSource);
230 default: // type not allowed in evaluation expressions
231 __LEAVE(KErrGeneral);
245 iLiteral.ToReal64L();
251 case EDbColLongText8:
255 case EDbColLongText16:
256 iLiteral.ToText16L();
259 case EDbColLongBinary:
264 // copy text to buffer for LIKE Escape support
265 TInt RSqlLiteral::CopyText()
269 TInt length = iVal.iPtr.iEnd - iVal.iPtr.iPtr;
272 if((iBuffer=HBufC::New(length+1)) == NULL)
276 iBuffer->Des().Copy(iVal.iPtr.iPtr,length);
277 iVal.iPtr.iPtr= &iBuffer->Des()[0];
278 iBuffer->Des().Append(iVal.iPtr.iEnd,1);
279 iVal.iPtr.iEnd=&iBuffer->Des()[length];
282 return KErrGeneral; // type mismatch