persistentstorage/sql/OsLayer/SqliteUtil.cpp
author Shabe Razvi <shaber@symbian.org>
Tue, 19 Oct 2010 15:57:30 +0100
changeset 54 a0e1d366428c
parent 31 ba1c4f4a893f
permissions -rw-r--r--
Workaround for Bug 3854 - featuremgr bld.inf no longer exports features.dat for emulator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
     1
// Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
     2
// All rights reserved.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
     3
// This component and the accompanying materials are made available
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
     4
// under the terms of "Eclipse Public License v1.0"
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
     5
// which accompanies this distribution, and is available
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
     7
//
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
     8
// Initial Contributors:
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
     9
// Nokia Corporation - initial contribution.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    10
//
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    11
// Contributors:
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    12
//
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    13
// Description:
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    14
//
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    15
#include <e32debug.h>
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    16
#include "SqliteUtil.h"
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    17
#include "OstTraceDefinitions.h"
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    18
#ifdef OST_TRACE_COMPILER_IN_USE
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    19
#include "SqliteUtilTraces.h"
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    20
#endif
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    21
#include "SqliteTraceDef.h"
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    22
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    23
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    24
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    25
#define UNUSED_ARG(arg) arg = arg
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    26
#define UNUSED_DES(arg) arg
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    27
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    28
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    29
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    30
#if defined OST_TRACE_COMPILER_IN_USE &&  defined _SQLITE_RDEBUG_PRINT
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    31
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    32
/**
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    33
This class has been added here to avoid the crashes when _SQLITE_RDEBUG_PRINT macro is defined but the
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    34
data to be printed out is too big and cannot fit into the buffer with size KSqliteMaxPrnStrLen.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    35
@internalComponent   
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    36
*/
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    37
class TSqliteDes16Overflow : public TDes16Overflow
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    38
    {
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    39
public:    
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    40
    virtual void Overflow(TDes16& /*aDes*/)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    41
        {
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    42
        }
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    43
    };
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    44
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    45
//Replaces:
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    46
// 1) "%lld" with "%ld"
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    47
// 2) "%s" with "%S"
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    48
//These are the differences in format specification between RDebig::Print and OST functions.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    49
//The new format spec length should be less or equal than the old format spec length.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    50
static void ReplaceFmtSpec(TDes& aFormat, const TDesC& aFmtSpec, const TDesC& aNewFmtSpec)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    51
	{
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    52
	TInt fmtLength = aFormat.Length();
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    53
	const TInt KDiff = aFmtSpec.Length() - aNewFmtSpec.Length();
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    54
    TPtr ptr((TText*)aFormat.Ptr(), fmtLength, fmtLength);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    55
    TInt pos;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    56
    while((pos = ptr.Find(aFmtSpec)) >= 0)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    57
    	{
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    58
		ptr.Replace(pos, aFmtSpec.Length(), aNewFmtSpec);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    59
		fmtLength -= KDiff;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    60
		ptr.Set(ptr.MidTPtr(pos));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    61
    	}
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    62
    aFormat.SetLength(fmtLength);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    63
	}
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    64
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    65
void SqlitePrintf(TInt /*aGroupName*/, TInt /*aTraceName*/, const char* aFormat, ...)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    66
    {
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    67
    VA_LIST list;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    68
    VA_START(list, aFormat);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    69
    TBuf<128> format;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    70
    _LIT(KTraceIdent, "Sqlite;");
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    71
    format.Copy(TPtrC8((const TUint8*)aFormat));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    72
    format.Insert(0, KTraceIdent);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    73
    format.Append(_L("\r\n"));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    74
    _LIT(KOstI64Fmt, "%lld");
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    75
    _LIT(KDbgPrnI64Fmt, "%ld");
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    76
    ReplaceFmtSpec(format, KOstI64Fmt, KDbgPrnI64Fmt);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    77
    _LIT(KOstDes8Fmt, "%s");
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    78
    _LIT(KDbgPrnDesFmt, "%S");
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    79
    ReplaceFmtSpec(format, KOstDes8Fmt, KDbgPrnDesFmt);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    80
    TBuf<KSqliteMaxPrnStrLen> buf;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    81
    TSqliteDes16Overflow overflowHandler;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    82
    buf.AppendFormatList(format, list, &overflowHandler);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    83
#ifdef _SQLITE_RDEBUG_PRINT    
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    84
    RDebug::RawPrint(buf);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    85
#endif
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    86
    }
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    87
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    88
const TDesC* SqliteDes8to16Ptr(const TDesC8& aDes)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    89
	{
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    90
	const TInt KBufSize = 100;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    91
	static TBuf<KBufSize> buf;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    92
	TPtrC8 ptr(aDes.Ptr(), Min(aDes.Length(), buf.MaxLength()));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    93
	buf.Copy(ptr);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    94
	return &buf;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    95
	}
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    96
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    97
#endif//defined OST_TRACE_COMPILER_IN_USE &&  defined _SQLITE_RDEBUG_PRINT 
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    98
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    99
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   100
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   101
/**
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   102
SQLite panic category.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   103
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   104
@internalComponent
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   105
*/
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   106
_LIT(KSqlitePanicCategory, "Sqlite");
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   107
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   108
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   109
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   110
/**
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   111
Panics the caller with aPanicCode panic code.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   112
The call will terminate the thread where it is called from.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   113
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   114
@param aPanicCode Panic code.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   115
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   116
@internalComponent
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   117
*/
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   118
static void SqlitePanic(TSqlitePanic aPanicCode)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   119
	{
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   120
	User::Panic(KSqlitePanicCategory, aPanicCode);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   121
	}
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   122
	
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   123
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   124
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   125
/**
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   126
The function prints out a "SQLite" panic message to the console and panics the thread where it is called from.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   127
It gives a useful information about the found error together with the source file name and line number where
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   128
it occurred.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   129
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   130
Note: this function  will output information regarding the panic only if _SQLITE_PANIC_TRACE_ENABLED macro is defined  
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   131
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   132
@param aFile Source file name
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   133
@param aLine Source line number
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   134
@param aPanicCode Panic code
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   135
@param aHandle Numeric value, uniquely identfying the leaving location (the "this" pointer for example)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   136
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   137
@return KErrNone
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   138
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   139
@internalComponent
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   140
*/  
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   141
TInt TSqliteUtil::Panic(const TText* aFile, TInt aLine, TInt aPanicCode, TUint aHandle)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   142
    {
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   143
#if defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   144
    TPtrC fname(FileName(aFile));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   145
    OstTraceExt5(TRACE_FATAL, TSQLUTIL_PANIC, "SQLite-Panic;0x%X;%S;%d;%S;%d", aHandle, __SQLITEPRNSTR(fname), aLine, __SQLITEPRNSTR(KSqlitePanicCategory), aPanicCode);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   146
#else
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   147
    UNUSED_ARG(aFile);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   148
    UNUSED_ARG(aLine);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   149
    UNUSED_ARG(aHandle);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   150
#endif      
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   151
    ::SqlitePanic(static_cast <TSqlitePanic> (aPanicCode));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   152
    return KErrNone;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   153
    }
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   154
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   155
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   156
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   157
#if defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   158
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   159
/**
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   160
The function creates and returns TPtrC object which points to aFile parameter.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   161
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   162
@param aFile File name
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   163
@return TPtrC object pointing to aFile parameter.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   164
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   165
@internalComponent
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   166
*/	
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   167
TPtrC TSqliteUtil::FileName(const TText* aFile)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   168
	{
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   169
	TPtrC p(aFile);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   170
	TInt ix = p.LocateReverse('\\');
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   171
	if(ix<0)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   172
		ix=p.LocateReverse('/');
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   173
	if(ix>=0)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   174
		p.Set(p.Mid(1+ix));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   175
	return p;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   176
	}
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   177
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   178
#endif //defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   179
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   180
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////