os/persistentdata/persistentstorage/sql/SRC/Common/SqlUtil.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sql/SRC/Common/SqlUtil.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,371 @@
     1.4 +// Copyright (c) 2005-2010 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 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <e32svr.h>
    1.20 +#include "SqlAssert.h"
    1.21 +#include <sqldb.h>		//ESqlAtRow, ESqlAtEnd, ESqlErrGeneral
    1.22 +#include "sqlite3.h"	//SQLITE_OK, SQLITE_ROW, SQLITE_DONE
    1.23 +#include "OstTraceDefinitions.h"
    1.24 +#ifdef OST_TRACE_COMPILER_IN_USE
    1.25 +#include "SqlUtilTraces.h"
    1.26 +#endif
    1.27 +#include "SqlTraceDef.h"
    1.28 +
    1.29 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    1.30 +
    1.31 +const TInt KSqlLeavePanic = -359;//The (last-1) error code from the reserved area for the SQL component.
    1.32 +
    1.33 +#define UNUSED_ARG(arg) arg = arg
    1.34 +#define UNUSED_DES(arg) arg
    1.35 +
    1.36 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    1.37 +
    1.38 +#if defined OST_TRACE_COMPILER_IN_USE &&  defined _SQL_RDEBUG_PRINT
    1.39 +
    1.40 +/**
    1.41 +This class has been added here to avoid the crashes when _SQL_RDEBUG_PRINT macro is defined but the
    1.42 +data to be printed out is too big and cannot fit into the buffer with size KSqlMaxPrnStrLen.
    1.43 +@internalComponent   
    1.44 +*/
    1.45 +class TSqlDes16Overflow : public TDes16Overflow
    1.46 +    {
    1.47 +public:    
    1.48 +    virtual void Overflow(TDes16& /*aDes*/)
    1.49 +        {
    1.50 +        }
    1.51 +    };
    1.52 +
    1.53 +//Replaces:
    1.54 +// 1) "%lld" with "%ld"
    1.55 +// 2) "%s" with "%S"
    1.56 +//These are the differences in format specification between RDebig::Print and OST functions.
    1.57 +//The new format spec length should be less or equal than the old format spec length.
    1.58 +static void ReplaceFmtSpec(TDes& aFormat, const TDesC& aFmtSpec, const TDesC& aNewFmtSpec)
    1.59 +	{
    1.60 +	TInt fmtLength = aFormat.Length();
    1.61 +	const TInt KDiff = aFmtSpec.Length() - aNewFmtSpec.Length();
    1.62 +    TPtr ptr((TText*)aFormat.Ptr(), fmtLength, fmtLength);
    1.63 +    TInt pos;
    1.64 +    while((pos = ptr.Find(aFmtSpec)) >= 0)
    1.65 +    	{
    1.66 +		ptr.Replace(pos, aFmtSpec.Length(), aNewFmtSpec);
    1.67 +		fmtLength -= KDiff;
    1.68 +		ptr.Set(ptr.MidTPtr(pos));
    1.69 +    	}
    1.70 +    aFormat.SetLength(fmtLength);
    1.71 +	}
    1.72 +
    1.73 +void SqlPrintf(TInt /*aGroupName*/, TInt /*aTraceName*/, const char* aFormat, ...)
    1.74 +    {
    1.75 +    VA_LIST list;
    1.76 +    VA_START(list, aFormat);
    1.77 +    TBuf<128> format;
    1.78 +    _LIT(KTraceIdent, "SQL;");
    1.79 +    format.Copy(TPtrC8((const TUint8*)aFormat));
    1.80 +    format.Insert(0, KTraceIdent);
    1.81 +    format.Append(_L("\r\n"));
    1.82 +    _LIT(KOstI64Fmt, "%lld");
    1.83 +    _LIT(KDbgPrnI64Fmt, "%ld");
    1.84 +    ReplaceFmtSpec(format, KOstI64Fmt, KDbgPrnI64Fmt);
    1.85 +    _LIT(KOstDes8Fmt, "%s");
    1.86 +    _LIT(KDbgPrnDesFmt, "%S");
    1.87 +    ReplaceFmtSpec(format, KOstDes8Fmt, KDbgPrnDesFmt);
    1.88 +    TBuf<KSqlMaxPrnStrLen> buf;
    1.89 +    TSqlDes16Overflow overflowHandler;
    1.90 +    buf.AppendFormatList(format, list, &overflowHandler);
    1.91 +#ifdef _SQL_RDEBUG_PRINT    
    1.92 +    RDebug::RawPrint(buf);
    1.93 +#endif
    1.94 +    }
    1.95 +
    1.96 +const TDesC* SqlDes8to16Ptr(const TDesC8& aDes, TDes& aOut)
    1.97 +	{
    1.98 +	TPtrC8 ptr(aDes.Ptr(), Min(aDes.Length(), aOut.MaxLength()));
    1.99 +	aOut.Copy(ptr);
   1.100 +	return &aOut;
   1.101 +	}
   1.102 +
   1.103 +#endif//defined OST_TRACE_COMPILER_IN_USE &&  defined _SQL_RDEBUG_PRINT 
   1.104 +
   1.105 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.106 +
   1.107 +/**
   1.108 +SQL panic category.
   1.109 +
   1.110 +@internalComponent
   1.111 +*/
   1.112 +_LIT(KPanicCategory, "SqlDb");
   1.113 +
   1.114 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.115 +
   1.116 +/**
   1.117 +Panics the caller with aPanicCode panic code.
   1.118 +The call will terminate the thread where it is called from.
   1.119 +
   1.120 +@param aPanicCode Panic code.
   1.121 +
   1.122 +@internalComponent
   1.123 +*/
   1.124 +static void SqlPanic(TSqlPanic aPanicCode)
   1.125 +	{
   1.126 +	User::Panic(KPanicCategory, aPanicCode);
   1.127 +	}
   1.128 +	
   1.129 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.130 +
   1.131 +/**
   1.132 +Panics the client with aPanicCode panic code.
   1.133 +This function is used by the SQL server to panic the caller (the client).
   1.134 +
   1.135 +@param aMessage Client's message
   1.136 +@param aPanicCode Panic code.
   1.137 +
   1.138 +@leave KSqlLeavePanic
   1.139 +
   1.140 +@return KErrNone
   1.141 +
   1.142 +@internalComponent
   1.143 +*/
   1.144 +static TInt SqlPanicClientL(const RMessage2& aMessage, TSqlPanic aPanicCode)
   1.145 +	{
   1.146 +	aMessage.Panic(KPanicCategory, aPanicCode);
   1.147 +	__SQLLEAVE2(KSqlLeavePanic);
   1.148 +	return KErrNone;
   1.149 +	}	
   1.150 +
   1.151 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.152 +
   1.153 +/**
   1.154 +The function prints out a "SQL panic" message to the console and panics the thread where it is called from.
   1.155 +It gives a useful information about the found error together with the source file name and line number where
   1.156 +it occurred.
   1.157 +
   1.158 +Note: this function  will output information regarding the panic only if _SQL_PANIC_TRACE_ENABLED macro is defined  
   1.159 +
   1.160 +@param aFile Source file name
   1.161 +@param aLine Source line number
   1.162 +@param aPanicCode Panic code
   1.163 +@param aHandle Numeric value, uniquely identfying the leaving location (the "this" pointer for example)
   1.164 +
   1.165 +@return KErrNone
   1.166 +
   1.167 +@internalComponent
   1.168 +*/  
   1.169 +TInt TSqlUtil::Panic(const TText* aFile, TInt aLine, TInt aPanicCode, TUint aHandle)
   1.170 +    {
   1.171 +#if defined OST_TRACE_COMPILER_IN_USE && defined _SQL_PANIC_TRACE_ENABLED
   1.172 +    TPtrC fname(FileName(aFile));
   1.173 +    OstTraceExt5(TRACE_FATAL, TSQLUTIL_PANIC, "Panic;0x%X;%S;%d;%S;%d", aHandle, __SQLPRNSTR(fname), aLine, __SQLPRNSTR(KPanicCategory), aPanicCode);
   1.174 +#else
   1.175 +    UNUSED_ARG(aFile);
   1.176 +    UNUSED_ARG(aLine);
   1.177 +    UNUSED_ARG(aHandle);
   1.178 +#endif      
   1.179 +    ::SqlPanic(static_cast <TSqlPanic> (aPanicCode));
   1.180 +    return KErrNone;
   1.181 +    }
   1.182 +
   1.183 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.184 +
   1.185 +/**
   1.186 +The function prints out a "SQL leave" message to the console and leaves with aError error code.
   1.187 +It gives a usefull information about the found error together with the source file name and line number where
   1.188 +it occured.
   1.189 +
   1.190 +Note: this function  will output information regarding the panic only if _SQL_LEAVE_TRACE_ENABLED macro is defined  
   1.191 +
   1.192 +@param aFile Source file name
   1.193 +@param aLine Source line number
   1.194 +@param aError Error code
   1.195 +@param aHandle Numeric value, uniquely identfying the leaving location (the "this" pointer for example)
   1.196 +
   1.197 +@internalComponent
   1.198 +*/  
   1.199 +void TSqlUtil::Leave(const TText* aFile, TInt aLine, TInt aError, TUint aHandle)
   1.200 +    {
   1.201 +#if defined OST_TRACE_COMPILER_IN_USE && defined _SQL_LEAVE_TRACE_ENABLED     
   1.202 +    TPtrC fname(FileName(aFile));
   1.203 +    OstTraceExt4(TRACE_ERROR, TSQLUTIL_LEAVE, "Leave;0x%X;%S;%d;Error=%d", aHandle, __SQLPRNSTR(fname), aLine, aError);
   1.204 +#else
   1.205 +    UNUSED_ARG(aFile);
   1.206 +    UNUSED_ARG(aLine);
   1.207 +    UNUSED_ARG(aHandle);
   1.208 +#endif
   1.209 +    User::Leave(aError);
   1.210 +    }
   1.211 +
   1.212 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.213 +
   1.214 +/**
   1.215 +The function prints out a "SQL leave" message to the console and leaves with aError error code, if it is 
   1.216 +negative.
   1.217 +It gives a usefull information about the found error together with the source file name and line number where
   1.218 +it occured.
   1.219 +
   1.220 +Note: this function  will output information regarding the panic only if _SQL_LEAVE_TRACE_ENABLED macro is defined  
   1.221 +
   1.222 +@param aFile Source file name
   1.223 +@param aLine Source line number
   1.224 +@param aError Error code
   1.225 +
   1.226 +@internalComponent
   1.227 +*/  
   1.228 +TInt TSqlUtil::LeaveIfError(const TText* aFile, TInt aLine, TInt aError, TUint aHandle)
   1.229 +    {
   1.230 +    if(aError < 0)
   1.231 +        {
   1.232 +        TSqlUtil::Leave(aFile, aLine, aError, aHandle);
   1.233 +        }
   1.234 +    return aError;
   1.235 +    }
   1.236 +
   1.237 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.238 +
   1.239 +/**
   1.240 +The function prints out a "SQL leave" message to the console and leaves with KErrNoMemory if 
   1.241 +aPtr parameter is NULL.
   1.242 +
   1.243 +Note: this function  will output information regarding the panic only if _SQL_LEAVE_TRACE_ENABLED macro is defined  
   1.244 +
   1.245 +@param aFile Source file name
   1.246 +@param aLine Source line number
   1.247 +@param aPtr The pointer to be tested against NULL value.
   1.248 +
   1.249 +@internalComponent
   1.250 +*/  
   1.251 +void* TSqlUtil::LeaveIfNull(const TText* aFile, TInt aLine, void* aPtr, TUint aHandle)
   1.252 +    {
   1.253 +    if(!aPtr)
   1.254 +        {
   1.255 +        TSqlUtil::Leave(aFile, aLine, KErrNoMemory, aHandle);
   1.256 +        }
   1.257 +    return aPtr;
   1.258 +    }
   1.259 +
   1.260 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.261 +
   1.262 +/**
   1.263 +The function is used by the SQL server.
   1.264 +It prints out a "SQL panic" message to the console and panic the client.
   1.265 +It gives a usefull information about the found error together with the source file name and line number where
   1.266 +it occured.
   1.267 +
   1.268 +Note: this function  will output information regarding the panic only if _SQL_PANIC_TRACE_ENABLED macro is defined  
   1.269 +
   1.270 +@param aFile Source file name
   1.271 +@param aLine Source line number
   1.272 +@param aMessage The client message, which processing caused the panic.
   1.273 +@param aPanicCode Error code
   1.274 +
   1.275 +@leave KSqlLeavePanic
   1.276 +
   1.277 +@return KErrNone;
   1.278 +
   1.279 +@internalComponent
   1.280 +*/  
   1.281 +TInt TSqlUtil::PanicClientL(const TText* aFile, TInt aLine, const RMessage2& aMessage, TInt aPanicCode, TUint aHandle)
   1.282 +    {
   1.283 +#if defined OST_TRACE_COMPILER_IN_USE && defined  _SQL_PANIC_TRACE_ENABLED
   1.284 +    TPtrC fname(FileName(aFile));
   1.285 +    OstTraceExt5(TRACE_FATAL, TSQLUTIL_PANICCLIENTL, "Panic;%X;%S;%d;%S;%d", aHandle, __SQLPRNSTR(fname), aLine, __SQLPRNSTR(KPanicCategory), aPanicCode);
   1.286 +#else
   1.287 +    UNUSED_ARG(aFile);
   1.288 +    UNUSED_ARG(aLine);
   1.289 +    UNUSED_ARG(aHandle);
   1.290 +#endif      
   1.291 +    return ::SqlPanicClientL(aMessage, static_cast <TSqlPanic> (aPanicCode));
   1.292 +    }
   1.293 +
   1.294 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.295 +
   1.296 +/**
   1.297 +Processes SQL database error code and OS error code and returns unified error code.
   1.298 +If aSqlError == SQLITE_ROW then the function returns KSqlAtRow.
   1.299 +If aSqlError == SQLITE_DONE then the function returns KSqlAtEnd.
   1.300 +If aSqlError == SQLITE_NOMEM then the function returns KErrNoMemory.
   1.301 +If aOsError != KErrNone then the function returns aOsError.
   1.302 +Otherwise the function converts aSqlError to one of error codes in [KSqlErrGeneral..KSqlErrStmtExpired] range.
   1.303 +
   1.304 +@param aSqlError SQL database error code.
   1.305 +@param aOsError OS error code.
   1.306 +
   1.307 +@return Database specific error code.
   1.308 +
   1.309 +@panic SqlDb 4 in debug mode - if aSqlError < 0
   1.310 +@panic SqlDb 4 in debug mode - if aOsError > 0
   1.311 +
   1.312 +@internalComponent
   1.313 +*/
   1.314 +TInt Sql2OsErrCode(TInt aSqlError, TInt aOsError)
   1.315 +	{
   1.316 +
   1.317 +	__ASSERT_DEBUG(aSqlError >= SQLITE_OK && aOsError <= KErrNone, __SQLPANIC2(ESqlPanicBadArgument));
   1.318 +	TInt err = KErrNone;
   1.319 +	if(aOsError == KErrDiskFull)
   1.320 +		{//Whatever is the aSqlError value, even SQLITE_OK, never ignore KErrDiskFull errors
   1.321 +		 //(For example: ROLLBACK statement execution, when the disk is full).
   1.322 +		err = aOsError;
   1.323 +		}
   1.324 +	else if(aSqlError == SQLITE_ROW)
   1.325 +		{
   1.326 +		err = KSqlAtRow;
   1.327 +		}
   1.328 +	else if(aSqlError == SQLITE_DONE)
   1.329 +		{
   1.330 +		err = KSqlAtEnd;
   1.331 +		}
   1.332 +	else if(aSqlError == SQLITE_NOMEM)
   1.333 +		{
   1.334 +		err = KErrNoMemory;
   1.335 +		}
   1.336 +	else if(aSqlError == SQLITE_AUTH)
   1.337 +		{
   1.338 +		err = KErrPermissionDenied;
   1.339 +		}
   1.340 +	else if(aSqlError == SQLITE_NOTADB)
   1.341 +		{
   1.342 +		err = KSqlErrNotDb;	
   1.343 +		}
   1.344 +	else if(aSqlError > SQLITE_OK)
   1.345 +		{
   1.346 +		err = aOsError != KErrNone ? aOsError : KSqlErrGeneral - aSqlError + 1;
   1.347 +		}
   1.348 +	return err;
   1.349 +	}
   1.350 +
   1.351 +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.352 +
   1.353 +#if defined OST_TRACE_COMPILER_IN_USE && (defined _SQL_PANIC_TRACE_ENABLED || defined _SQL_LEAVE_TRACE_ENABLED) 
   1.354 +
   1.355 +/**
   1.356 +The function creates and returns TPtrC object which points to aFile parameter.
   1.357 +
   1.358 +@param aFile File name
   1.359 +@return TPtrC object pointing to aFile parameter.
   1.360 +
   1.361 +@internalComponent
   1.362 +*/	
   1.363 +TPtrC TSqlUtil::FileName(const TText* aFile)
   1.364 +	{
   1.365 +	TPtrC p(aFile);
   1.366 +	TInt ix = p.LocateReverse('\\');
   1.367 +	if(ix<0)
   1.368 +		ix=p.LocateReverse('/');
   1.369 +	if(ix>=0)
   1.370 +		p.Set(p.Mid(1+ix));
   1.371 +	return p;
   1.372 +	}
   1.373 +
   1.374 +#endif //defined OST_TRACE_COMPILER_IN_USE && (defined _SQL_PANIC_TRACE_ENABLED || defined _SQL_LEAVE_TRACE_ENABLED)