persistentstorage/sqlite3api/OsLayer/SqliteUtil.cpp
author hgs
Thu, 12 Aug 2010 11:53:23 +0100
changeset 40 b8bdbc8f59c7
parent 31 ba1c4f4a893f
permissions -rw-r--r--
201031_02
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
// "%lld" with "%ld"
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    47
//These are the differences in format specification between RDebig::Print and OST functions.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    48
//The new format spec length should be less or equal than the old format spec length.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    49
static void ReplaceFmtSpec(TDes& aFormat, const TDesC& aFmtSpec, const TDesC& aNewFmtSpec)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    50
	{
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    51
	TInt fmtLength = aFormat.Length();
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    52
	const TInt KDiff = aFmtSpec.Length() - aNewFmtSpec.Length();
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    53
    TPtr ptr((TText*)aFormat.Ptr(), fmtLength, fmtLength);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    54
    TInt pos;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    55
    while((pos = ptr.Find(aFmtSpec)) >= 0)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    56
    	{
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    57
		ptr.Replace(pos, aFmtSpec.Length(), aNewFmtSpec);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    58
		fmtLength -= KDiff;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    59
		ptr.Set(ptr.MidTPtr(pos));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    60
    	}
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    61
    aFormat.SetLength(fmtLength);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    62
	}
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    63
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    64
void SqlitePrintf(TInt /*aGroupName*/, TInt /*aTraceName*/, const char* aFormat, ...)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    65
    {
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    66
    VA_LIST list;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    67
    VA_START(list, aFormat);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    68
    TBuf<128> format;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    69
    _LIT(KTraceIdent, "Sqlite3;");
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    70
    format.Copy(TPtrC8((const TUint8*)aFormat));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    71
    format.Insert(0, KTraceIdent);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    72
    format.Append(_L("\r\n"));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    73
    _LIT(KOstI64Fmt, "%lld");
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    74
    _LIT(KDbgPrnI64Fmt, "%ld");
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    75
    ReplaceFmtSpec(format, KOstI64Fmt, KDbgPrnI64Fmt);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    76
    TBuf<KSqliteMaxPrnStrLen> buf;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    77
    TSqliteDes16Overflow overflowHandler;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    78
    buf.AppendFormatList(format, list, &overflowHandler);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    79
#ifdef _SQLITE_RDEBUG_PRINT    
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    80
    RDebug::RawPrint(buf);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    81
#endif
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    82
    }
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    83
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    84
#endif//defined OST_TRACE_COMPILER_IN_USE &&  defined _SQLITE_RDEBUG_PRINT 
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    85
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    86
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    87
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    88
/**
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    89
SQLite panic category.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    90
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    91
@internalComponent
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    92
*/
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    93
_LIT(KSqlitePanicCategory, "Sqlite3");
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    94
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    95
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    96
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    97
/**
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    98
Panics the caller with aPanicCode panic code.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
    99
The call will terminate the thread where it is called from.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   100
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   101
@param aPanicCode Panic code.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   102
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   103
@internalComponent
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   104
*/
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   105
static void SqlitePanic(TSqlitePanic aPanicCode)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   106
	{
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   107
	User::Panic(KSqlitePanicCategory, aPanicCode);
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
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   112
/**
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   113
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
   114
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
   115
it occurred.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   116
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   117
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
   118
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   119
@param aFile Source file name
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   120
@param aLine Source line number
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   121
@param aPanicCode Panic code
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   122
@param aHandle Numeric value, uniquely identfying the leaving location (the "this" pointer for example)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   123
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   124
@return KErrNone
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   125
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   126
@internalComponent
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   127
*/  
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   128
TInt TSqliteUtil::Panic(const TText* aFile, TInt aLine, TInt aPanicCode, TUint aHandle)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   129
    {
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   130
#if defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   131
    TPtrC fname(FileName(aFile));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   132
    OstTraceExt5(TRACE_FATAL, TSQLUTIL_PANIC, "Panic;0x%X;%S;%d;%S;%d", aHandle, __SQLITEPRNSTR(fname), aLine, __SQLITEPRNSTR(KSqlitePanicCategory), aPanicCode);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   133
#else
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   134
    UNUSED_ARG(aFile);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   135
    UNUSED_ARG(aLine);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   136
    UNUSED_ARG(aHandle);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   137
#endif      
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   138
    ::SqlitePanic(static_cast <TSqlitePanic> (aPanicCode));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   139
    return KErrNone;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   140
    }
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   141
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   142
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   143
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   144
#if defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   145
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   146
/**
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   147
The function creates and returns TPtrC object which points to aFile parameter.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   148
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   149
@param aFile File name
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   150
@return TPtrC object pointing to aFile parameter.
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   151
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   152
@internalComponent
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   153
*/	
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   154
TPtrC TSqliteUtil::FileName(const TText* aFile)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   155
	{
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   156
	TPtrC p(aFile);
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   157
	TInt ix = p.LocateReverse('\\');
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   158
	if(ix<0)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   159
		ix=p.LocateReverse('/');
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   160
	if(ix>=0)
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   161
		p.Set(p.Mid(1+ix));
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   162
	return p;
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   163
	}
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   164
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   165
#endif //defined OST_TRACE_COMPILER_IN_USE && defined _SQLITE_PANIC_TRACE_ENABLED
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   166
ba1c4f4a893f 201025_02
hgs
parents:
diff changeset
   167
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////