os/persistentdata/persistentstorage/dbms/pcdbms/usql/UQ_LIT.CPP
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) 1998-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
//
sl@0
    13
// Description:
sl@0
    14
// SQL Literal value and node classes
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "UQ_STD.H"
sl@0
    19
sl@0
    20
// internalising literal strings, overload selectors
sl@0
    21
sl@0
    22
inline HBufC8* Alloc8L(const TDesC8& aDes)
sl@0
    23
	{return aDes.AllocL();}
sl@0
    24
inline HBufC8* Alloc8L(const TDesC16& aDes)
sl@0
    25
	{HBufC8* cell=HBufC8::NewL(aDes.Length());cell->Des().Copy(aDes);return cell;}
sl@0
    26
inline HBufC16* Alloc16L(const TDesC16& aDes)
sl@0
    27
	{return aDes.AllocL();}
sl@0
    28
inline HBufC16* Alloc16L(const TDesC8& aDes)
sl@0
    29
	{HBufC16* cell=HBufC16::NewL(aDes.Length());cell->Des().Copy(aDes);return cell;}
sl@0
    30
sl@0
    31
template <class B,class T>
sl@0
    32
inline B* DoInternString(B* aBuf,T*)
sl@0
    33
	{
sl@0
    34
	const T* base=aBuf->Ptr();
sl@0
    35
	const T* from=base;
sl@0
    36
	const T* end=base+aBuf->Length();
sl@0
    37
	for (;;)
sl@0
    38
		{
sl@0
    39
		if (from==end)
sl@0
    40
			return aBuf;
sl@0
    41
		if (*from++=='\'')
sl@0
    42
			break;
sl@0
    43
		}
sl@0
    44
	T* to=(T*)from;
sl@0
    45
	++from;
sl@0
    46
	while (from<end)
sl@0
    47
		{
sl@0
    48
		T c=*from++;
sl@0
    49
		if (c=='\'')
sl@0
    50
			++from;
sl@0
    51
		*to++=c;
sl@0
    52
		}
sl@0
    53
	aBuf->Des().SetLength(to-base);
sl@0
    54
	return aBuf;
sl@0
    55
	}
sl@0
    56
sl@0
    57
template <class B>
sl@0
    58
inline B* InternString(B* aBuf)
sl@0
    59
	{return DoInternString(aBuf,PtrType(*aBuf));}
sl@0
    60
sl@0
    61
// Class RSqlLiteral
sl@0
    62
sl@0
    63
void RSqlLiteral::Close()
sl@0
    64
//
sl@0
    65
// Free any allocated cell
sl@0
    66
//
sl@0
    67
	{
sl@0
    68
	if(iBuffer)
sl@0
    69
		delete iBuffer;
sl@0
    70
	if (IsAlloc())
sl@0
    71
		User::Free(iVal.iAlloc);
sl@0
    72
		
sl@0
    73
	}
sl@0
    74
sl@0
    75
void RSqlLiteral::ToInt32L()
sl@0
    76
//
sl@0
    77
// Convert a numeric type to Int32
sl@0
    78
//
sl@0
    79
	{
sl@0
    80
	ToInt64L();
sl@0
    81
	const TInt64& v=iVal.iInt64;
sl@0
    82
	TInt32 l = I64LOW(v);
sl@0
    83
	TInt32 h = I64HIGH(v);
sl@0
    84
	if (h==(l>>31))	// domain check, in signed 32-bit range
sl@0
    85
		{
sl@0
    86
		iVal.iInt32=l;
sl@0
    87
		iType=EInt32;
sl@0
    88
		}
sl@0
    89
	else
sl@0
    90
		__LEAVE(KErrOverflow);	// cannot convert
sl@0
    91
	}
sl@0
    92
sl@0
    93
void RSqlLiteral::ToUint32L()
sl@0
    94
//
sl@0
    95
// Convert a numeric type to Uint32
sl@0
    96
//
sl@0
    97
	{
sl@0
    98
	ToInt64L();
sl@0
    99
	const TInt64& v=iVal.iInt64;
sl@0
   100
	if (I64HIGH(v)==0)		// domain check, in unsigned 32-bit range
sl@0
   101
		{
sl@0
   102
		iVal.iUint32 = I64LOW(v);
sl@0
   103
		iType=EUint32;
sl@0
   104
		}
sl@0
   105
	else
sl@0
   106
		__LEAVE(KErrOverflow);	// cannot convert
sl@0
   107
	}
sl@0
   108
sl@0
   109
void RSqlLiteral::ToInt64L()
sl@0
   110
//
sl@0
   111
// Convert a numeric type to int64
sl@0
   112
//
sl@0
   113
	{
sl@0
   114
	__ASSERT(IsBasic());	// only apply conversions once
sl@0
   115
	TType t=iType;
sl@0
   116
	if (t==EReal64)
sl@0
   117
		{
sl@0
   118
		iVal.iInt64() = static_cast<TInt64>(Real64());
sl@0
   119
		iType=EInt64;
sl@0
   120
		}
sl@0
   121
	else if (t!=EInt64)
sl@0
   122
		__LEAVE(KErrGeneral);
sl@0
   123
	}
sl@0
   124
sl@0
   125
void RSqlLiteral::ToReal64L()
sl@0
   126
//
sl@0
   127
// Convert a numeric type to real64
sl@0
   128
//
sl@0
   129
	{
sl@0
   130
	__ASSERT(IsBasic());	// only apply conversions once
sl@0
   131
	TType t=iType;
sl@0
   132
	if (t==EInt64)
sl@0
   133
		{
sl@0
   134
		iVal.iReal64=I64REAL(Int64());
sl@0
   135
		iType=EReal64;
sl@0
   136
		}
sl@0
   137
	else if (t!=EReal64)
sl@0
   138
		__LEAVE(KErrGeneral);
sl@0
   139
	}
sl@0
   140
sl@0
   141
void RSqlLiteral::ToReal32L()
sl@0
   142
//
sl@0
   143
// Convert a numeric type to real32
sl@0
   144
//
sl@0
   145
	{
sl@0
   146
	ToReal64L();
sl@0
   147
	__LEAVE_IF_ERROR(TRealX(iVal.iReal64).GetTReal(iVal.iReal32));
sl@0
   148
	iType=EReal32;
sl@0
   149
	}
sl@0
   150
sl@0
   151
void RSqlLiteral::ToTimeL()
sl@0
   152
//
sl@0
   153
// Ensure we are a time
sl@0
   154
//
sl@0
   155
	{
sl@0
   156
	__ASSERT(IsBasic());	// only apply conversions once
sl@0
   157
	if (iType!=ETime)
sl@0
   158
		__LEAVE(KErrGeneral);	// type mismatch
sl@0
   159
	}
sl@0
   160
sl@0
   161
void RSqlLiteral::ToText8L()
sl@0
   162
//
sl@0
   163
// Convert a ptr to a text8
sl@0
   164
//
sl@0
   165
	{
sl@0
   166
	__ASSERT(IsBasic());	// only apply conversions once
sl@0
   167
	if (iType==EPtr)
sl@0
   168
		{
sl@0
   169
		iVal.iAlloc=InternString(Alloc8L(DesC()));
sl@0
   170
		iType=EBuf8;
sl@0
   171
		}
sl@0
   172
	else
sl@0
   173
		__LEAVE(KErrGeneral);	// type mismatch
sl@0
   174
	}
sl@0
   175
sl@0
   176
void RSqlLiteral::ToText16L()
sl@0
   177
//
sl@0
   178
// Convert a ptr to a text8
sl@0
   179
//
sl@0
   180
	{
sl@0
   181
	__ASSERT(IsBasic());	// only apply conversions once
sl@0
   182
	if (iType==EPtr)
sl@0
   183
		{
sl@0
   184
		iVal.iAlloc=InternString(Alloc16L(DesC()));
sl@0
   185
		iType=EBuf16;
sl@0
   186
		}
sl@0
   187
	else
sl@0
   188
		__LEAVE(KErrGeneral);	// type mismatch
sl@0
   189
	}
sl@0
   190
sl@0
   191
void RSqlLiteral::ToBlobL()
sl@0
   192
//
sl@0
   193
// Convert a hex encoded ptr to a blob
sl@0
   194
//
sl@0
   195
	{
sl@0
   196
	__ASSERT(IsBasic());	// only apply conversions once
sl@0
   197
	if (iType==EBlob)
sl@0
   198
		{
sl@0
   199
		RBuf8 blobBuf;
sl@0
   200
		HBufC* buf = Alloc16L(DesC());
sl@0
   201
		CleanupStack::PushL(buf);
sl@0
   202
		HexDecodeL(*buf, blobBuf);
sl@0
   203
		CleanupStack::PopAndDestroy(buf);
sl@0
   204
		iVal.iAlloc=Alloc8L(blobBuf);
sl@0
   205
		blobBuf.Close();
sl@0
   206
		iType=EBlob;
sl@0
   207
		}
sl@0
   208
	else
sl@0
   209
		__LEAVE(KErrGeneral);	// type mismatch
sl@0
   210
	}
sl@0
   211
sl@0
   212
sl@0
   213
sl@0
   214
// class CSqlLiteralNode
sl@0
   215
sl@0
   216
CSqlLiteralNode::CSqlLiteralNode(TType aType,const TDesC& aColumn,const RSqlLiteral& aLiteral)
sl@0
   217
	:CSqlBoundNode(aType,aColumn),iLiteral(aLiteral)
sl@0
   218
	{}
sl@0
   219
sl@0
   220
CSqlLiteralNode::~CSqlLiteralNode()
sl@0
   221
	{
sl@0
   222
	iLiteral.Close();
sl@0
   223
	}
sl@0
   224
sl@0
   225
void CSqlLiteralNode::BindL(const RDbTableRow& aSource)
sl@0
   226
	{
sl@0
   227
	CSqlBoundNode::BindL(aSource);
sl@0
   228
	switch (ColType())
sl@0
   229
		{
sl@0
   230
	default:	// type not allowed in evaluation expressions
sl@0
   231
		__LEAVE(KErrGeneral);
sl@0
   232
		break;
sl@0
   233
	case EDbColBit:
sl@0
   234
	case EDbColInt8:
sl@0
   235
	case EDbColUint8:
sl@0
   236
	case EDbColInt16:
sl@0
   237
	case EDbColUint16:
sl@0
   238
	case EDbColInt32:
sl@0
   239
	case EDbColUint32:
sl@0
   240
	case EDbColInt64:
sl@0
   241
		iLiteral.ToInt64L();
sl@0
   242
		break;
sl@0
   243
	case EDbColReal32:
sl@0
   244
	case EDbColReal64:
sl@0
   245
		iLiteral.ToReal64L();
sl@0
   246
		break;
sl@0
   247
	case EDbColDateTime:
sl@0
   248
		iLiteral.ToTimeL();
sl@0
   249
		break;
sl@0
   250
	case EDbColText8:
sl@0
   251
	case EDbColLongText8:
sl@0
   252
		iLiteral.ToText8L();
sl@0
   253
		break;
sl@0
   254
	case EDbColText16:
sl@0
   255
	case EDbColLongText16:
sl@0
   256
		iLiteral.ToText16L();
sl@0
   257
		break;
sl@0
   258
	case EDbColBinary:
sl@0
   259
	case EDbColLongBinary:
sl@0
   260
		iLiteral.ToBlobL();
sl@0
   261
		break;
sl@0
   262
		}
sl@0
   263
	}
sl@0
   264
// copy text to buffer for LIKE Escape support
sl@0
   265
TInt RSqlLiteral::CopyText()
sl@0
   266
	{
sl@0
   267
	if (iType==EPtr)
sl@0
   268
		{
sl@0
   269
		TInt length = iVal.iPtr.iEnd - iVal.iPtr.iPtr;
sl@0
   270
		if(iBuffer)
sl@0
   271
			delete iBuffer;
sl@0
   272
		if((iBuffer=HBufC::New(length+1)) == NULL)
sl@0
   273
	        	{
sl@0
   274
			return KErrNoMemory;
sl@0
   275
			}
sl@0
   276
		iBuffer->Des().Copy(iVal.iPtr.iPtr,length);
sl@0
   277
		iVal.iPtr.iPtr= &iBuffer->Des()[0];
sl@0
   278
		iBuffer->Des().Append(iVal.iPtr.iEnd,1);
sl@0
   279
		iVal.iPtr.iEnd=&iBuffer->Des()[length]; 
sl@0
   280
		}
sl@0
   281
	else
sl@0
   282
		return KErrGeneral;	// type mismatch
sl@0
   283
	return KErrNone;
sl@0
   284
	}
sl@0
   285