persistentstorage/sql/SRC/Server/SqlSrvBlob.cpp
branchRCL_3
changeset 23 26645d81f48d
parent 0 08ec8eefde2f
child 24 cc28652e0254
equal deleted inserted replaced
21:28839de615b4 23:26645d81f48d
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    12 //
    12 //
    13 // Description:
    13 // Description:
    14 //
    14 //
    15 
    15 
    16 #include "sqlite3.h"
    16 #include "sqlite3.h"
    17 #include "SqlPanic.h"
    17 #include "SqlAssert.h"
    18 #include "SqlSrvBlob.h"
    18 #include "SqlSrvBlob.h"
    19 #include "SqliteSymbian.h"
    19 #include "SqliteSymbian.h"
    20 #include "SqlSrvUtil.h"
    20 #include "SqlSrvUtil.h"
    21 #include "SqlSrvStrings.h"
    21 #include "SqlSrvStrings.h"
    22 #include "SqlDb.h"
    22 #include "SqlDb.h"
       
    23 #include "OstTraceDefinitions.h"
       
    24 #ifdef OST_TRACE_COMPILER_IN_USE
       
    25 #include "SqlSrvBlobTraces.h"
       
    26 #endif
       
    27 #include "SqlTraceDef.h"
       
    28 
       
    29 #define UNUSED_VAR(var) var = var
    23 
    30 
    24 /**
    31 /**
    25 Creates a new HBlobBuf instance.
    32 Creates a new HBlobBuf instance.
    26 
    33 
    27 @param aDb 			 The database handle
    34 @param aDb 			 The database handle
    43 @panic SqlDb 4 In _DEBUG mode. Negative or zero aRowId.
    50 @panic SqlDb 4 In _DEBUG mode. Negative or zero aRowId.
    44 @panic SqlDb 4 In _DEBUG mode. aMode parameter is not HBlobBuf::EReadOnly or HBlobBuf::EReadWrite.
    51 @panic SqlDb 4 In _DEBUG mode. aMode parameter is not HBlobBuf::EReadOnly or HBlobBuf::EReadWrite.
    45 */
    52 */
    46 HBlobBuf* HBlobBuf::NewL(sqlite3* aDb, const TDesC8& aDbName, const TDesC8& aTableName, const TDesC8& aColumnName, TInt64 aRowId, TMode aMode)
    53 HBlobBuf* HBlobBuf::NewL(sqlite3* aDb, const TDesC8& aDbName, const TDesC8& aTableName, const TDesC8& aColumnName, TInt64 aRowId, TMode aMode)
    47 	{
    54 	{
    48 	__SQLASSERT(aDb != NULL, ESqlPanicBadArgument);
    55 	__SQLTRACE_BLOBVAR(TBuf<100> des16prnbuf);
    49 	__SQLASSERT(aRowId > 0, ESqlPanicBadArgument);
    56 	SQL_TRACE_BLOB(OstTraceExt4(TRACE_INTERNALS, HBLOBBUF_NEWL_ENTRY1, "Entry;0;HBlobBuf::NewL;sqlite3*=0x%X;aDbName=%s;aRowId=%lld;aMode=%d", (TUint)aDb, __SQLPRNSTR8(aDbName, des16prnbuf), aRowId, (TInt)aMode));
    50 	__SQLASSERT(aMode == HBlobBuf::EReadOnly || aMode == HBlobBuf::EReadWrite, ESqlPanicBadArgument);
    57 	SQL_TRACE_BLOB(OstTraceExt1(TRACE_INTERNALS, HBLOBBUF_NEWL_ENTRY2, "Entry;0;HBlobBuf::NewL;aTableName=%s", __SQLPRNSTR8(aTableName, des16prnbuf)));
       
    58 	SQL_TRACE_BLOB(OstTraceExt1(TRACE_INTERNALS, HBLOBBUF_NEWL_ENTRY3, "Entry;0;HBlobBuf::NewL;aColumnName=%s", __SQLPRNSTR8(aColumnName, des16prnbuf)));
       
    59 	__ASSERT_DEBUG(aDb != NULL, __SQLPANIC2(ESqlPanicBadArgument));
       
    60 	__ASSERT_DEBUG(aRowId > 0, __SQLPANIC2(ESqlPanicBadArgument));
       
    61 	__ASSERT_DEBUG(aMode == HBlobBuf::EReadOnly || aMode == HBlobBuf::EReadWrite, __SQLPANIC2(ESqlPanicBadArgument));
    51 	
    62 	
    52 	HBlobBuf* self = new (ELeave) HBlobBuf;
    63 	HBlobBuf* self = new (ELeave) HBlobBuf;
    53 	CleanupStack::PushL(self);
    64 	CleanupStack::PushL(self);
    54 	self->ConstructL(aDb, aDbName, aTableName, aColumnName, aRowId, aMode);
    65 	self->ConstructL(aDb, aDbName, aTableName, aColumnName, aRowId, aMode);
    55 	CleanupStack::Pop(self);
    66 	CleanupStack::Pop(self);
       
    67 	SQL_TRACE_BLOB(OstTraceExt2(TRACE_INTERNALS, HBLOBBUF_NEWL_EXIT, "Exit;0x%X;HBlobBuf::NewL;sqlite3_blob*=0x%X", (TUint)self, (TUint)self->iBlobHandle));
    56 	return self;
    68 	return self;
    57 	}
    69 	}
    58 
    70 
    59 /**
    71 /**
    60 Initializes HBlobBuf data members with their default values.
    72 Initializes HBlobBuf data members with their default values.
    87 @panic SqlDb 4 In _DEBUG mode. aMode parameter is not HBlobBuf::EReadOnly or HBlobBuf::EReadWrite.
    99 @panic SqlDb 4 In _DEBUG mode. aMode parameter is not HBlobBuf::EReadOnly or HBlobBuf::EReadWrite.
    88 @panic SqlDb 7 In _DEBUG mode. NULL blob handle.
   100 @panic SqlDb 7 In _DEBUG mode. NULL blob handle.
    89 */
   101 */
    90 void HBlobBuf::ConstructL(sqlite3* aDb, const TDesC8& aDbName, const TDesC8& aTableName, const TDesC8& aColumnName, TInt64 aRowId, TMode aMode)
   102 void HBlobBuf::ConstructL(sqlite3* aDb, const TDesC8& aDbName, const TDesC8& aTableName, const TDesC8& aColumnName, TInt64 aRowId, TMode aMode)
    91 	{
   103 	{
    92 	__SQLASSERT(aDb != NULL, ESqlPanicBadArgument);
   104 	__ASSERT_DEBUG(aDb != NULL, __SQLPANIC(ESqlPanicBadArgument));
    93 	__SQLASSERT(aRowId > 0, ESqlPanicBadArgument);
   105 	__ASSERT_DEBUG(aRowId > 0, __SQLPANIC(ESqlPanicBadArgument));
    94 	__SQLASSERT(aMode == HBlobBuf::EReadOnly || aMode == HBlobBuf::EReadWrite, ESqlPanicBadArgument);
   106 	__ASSERT_DEBUG(aMode == HBlobBuf::EReadOnly || aMode == HBlobBuf::EReadWrite, __SQLPANIC(ESqlPanicBadArgument));
    95 	
   107 	
    96 	(void)sqlite3SymbianLastOsError();//clear last OS error
   108 	(void)sqlite3SymbianLastOsError();//clear last OS error
    97 			
   109 			
    98 	TInt err = sqlite3_blob_open(aDb, (const char*)aDbName.Ptr(), (const char*)aTableName.Ptr(), (const char*)aColumnName.Ptr(), 
   110 	TInt err = sqlite3_blob_open(aDb, (const char*)aDbName.Ptr(), (const char*)aTableName.Ptr(), (const char*)aColumnName.Ptr(), 
    99 								 aRowId, aMode & HBlobBuf::EReadWrite, &iBlobHandle);
   111 								 aRowId, aMode & HBlobBuf::EReadWrite, &iBlobHandle);
   100 	__SQLLEAVE_IF_ERROR(::Sql2OsErrCode(err, sqlite3SymbianLastOsError()));
   112 	__SQLLEAVE_IF_ERROR(::Sql2OsErrCode(err, sqlite3SymbianLastOsError()));
   101 	__SQLASSERT(iBlobHandle != NULL, ESqlPanicInternalError);
   113 	__ASSERT_DEBUG(iBlobHandle != NULL, __SQLPANIC(ESqlPanicInternalError));
   102 	iBlobSize = sqlite3_blob_bytes(iBlobHandle);
   114 	iBlobSize = sqlite3_blob_bytes(iBlobHandle);
   103 	iWrPos = iRdPos = 0;
   115 	iWrPos = iRdPos = 0;
   104 	}
   116 	}
   105 
   117 
   106 /**
   118 /**
   107 Closes the blob handle as part of the HBlobBuf object being destroyed.
   119 Closes the blob handle as part of the HBlobBuf object being destroyed.
   108 */
   120 */
   109 void HBlobBuf::DoRelease()
   121 void HBlobBuf::DoRelease()
   110 	{
   122 	{
   111 	TRAP_IGNORE(DoSynchL());
   123 	SQL_TRACE_BLOB(OstTraceExt2(TRACE_INTERNALS, HBLOBBUF_DORELEASE_ENTRY, "Entry;0x%X;HBlobBuf::DoRelease;sqlite3_blob*=0x%X", (TUint)this, (TUint)iBlobHandle));
   112 	}
   124 	TRAPD(err, DoSynchL());
       
   125 	SQL_TRACE_BLOB(OstTraceExt2(TRACE_INTERNALS, HBLOBBUF_DORELEASE_EXIT, "Exit;0x%X;HBlobBuf::DoRelease;err=%d", (TUint)this, err));
       
   126     UNUSED_VAR(err);
       
   127     }
   113 	
   128 	
   114 /**
   129 /**
   115 Closes the blob handle.
   130 Closes the blob handle.
   116 Any buffered data is delivered to the stream.
   131 Any buffered data is delivered to the stream.
   117 */	
   132 */	
   118 void HBlobBuf::DoSynchL()
   133 void HBlobBuf::DoSynchL()
   119 	{
   134 	{
   120 	if(iBlobHandle)
   135 	if(iBlobHandle)
   121 		{
   136 		{
   122 		TInt err = sqlite3_blob_close(iBlobHandle);
   137 		TInt err = sqlite3_blob_close(iBlobHandle);
       
   138 		SQL_TRACE_BLOB(OstTraceExt2(TRACE_INTERNALS, HBLOBBUF_DOSYNCHL, "0x%X;HBlobBuf::DoSynchL;err=%d", (TUint)this, err));
   123 		iBlobHandle = NULL; // the close is unconditional, even if an error occurs
   139 		iBlobHandle = NULL; // the close is unconditional, even if an error occurs
   124 		__SQLLEAVE_IF_ERROR(::Sql2OsErrCode(err, sqlite3SymbianLastOsError()));
   140 		__SQLLEAVE_IF_ERROR(::Sql2OsErrCode(err, sqlite3SymbianLastOsError()));
   125 		}	
   141 		}	
   126 	}
   142 	}
   127 
   143 
   143 @panic SqlDb 4 In _DEBUG mode. Negative aMaxLength parameter.
   159 @panic SqlDb 4 In _DEBUG mode. Negative aMaxLength parameter.
   144 @panic SqlDb 2 In _DEBUG mode. NULL iBlobHandle.
   160 @panic SqlDb 2 In _DEBUG mode. NULL iBlobHandle.
   145 */
   161 */
   146 TInt HBlobBuf::DoReadL(TAny* aPtr, TInt aMaxLength)
   162 TInt HBlobBuf::DoReadL(TAny* aPtr, TInt aMaxLength)
   147 	{
   163 	{
   148 	__SQLASSERT(aPtr != NULL, ESqlPanicBadArgument);
   164 	__ASSERT_DEBUG(aPtr != NULL, __SQLPANIC(ESqlPanicBadArgument));
   149 	__SQLASSERT(aMaxLength >= 0, ESqlPanicBadArgument);
   165 	__ASSERT_DEBUG(aMaxLength >= 0, __SQLPANIC(ESqlPanicBadArgument));
   150 	__SQLASSERT(iBlobHandle != NULL, ESqlPanicInvalidObj);
   166 	__ASSERT_DEBUG(iBlobHandle != NULL, __SQLPANIC(ESqlPanicInvalidObj));
       
   167 	
       
   168 	SQL_TRACE_BLOB(OstTraceExt5(TRACE_INTERNALS, HBLOBBUF_DOREADL, "0x%X;HBlobBuf::DoReadL;aMaxLength=%d;iBlobSize=%d;iWrPos=%d;iRdPos=%d", (TUint)this, aMaxLength, iBlobSize, iWrPos, iRdPos));
   151 	
   169 	
   152 	if(aMaxLength <= 0)
   170 	if(aMaxLength <= 0)
   153 		{
   171 		{
   154 		return 0;	
   172 		return 0;	
   155 		}
   173 		}
   186 @panic SqlDb 4 In _DEBUG mode. Negative aLength parameter.
   204 @panic SqlDb 4 In _DEBUG mode. Negative aLength parameter.
   187 @panic SqlDb 2 In _DEBUG mode. NULL iBlobHandle.
   205 @panic SqlDb 2 In _DEBUG mode. NULL iBlobHandle.
   188 */
   206 */
   189 void HBlobBuf::DoWriteL(const TAny* aPtr, TInt aLength)
   207 void HBlobBuf::DoWriteL(const TAny* aPtr, TInt aLength)
   190 	{
   208 	{
   191 	__SQLASSERT(aPtr != NULL, ESqlPanicBadArgument);
   209 	__ASSERT_DEBUG(aPtr != NULL, __SQLPANIC(ESqlPanicBadArgument));
   192 	__SQLASSERT(aLength >= 0, ESqlPanicBadArgument);
   210 	__ASSERT_DEBUG(aLength >= 0, __SQLPANIC(ESqlPanicBadArgument));
   193 	__SQLASSERT(iBlobHandle != NULL, ESqlPanicInvalidObj);
   211 	__ASSERT_DEBUG(iBlobHandle != NULL, __SQLPANIC(ESqlPanicInvalidObj));
       
   212 	
       
   213 	SQL_TRACE_BLOB(OstTraceExt5(TRACE_INTERNALS, HBLOBBUF_DOWRITEL, "0x%X;HBlobBuf::DoWriteL;aLength=%d;iBlobSize=%d;iWrPos=%d;iRdPos=%d", (TUint)this, aLength, iBlobSize, iWrPos, iRdPos));
   194 	
   214 	
   195 	if(aLength <= 0)
   215 	if(aLength <= 0)
   196 		{
   216 		{
   197 		return;
   217 		return;
   198 		}
   218 		}
   225 @panic SqlDb 8 In _DEBUG mode. Invalid aMark parameter.
   245 @panic SqlDb 8 In _DEBUG mode. Invalid aMark parameter.
   226 @panic SqlDb 9 In _DEBUG mode. Invalid aLocation parameter.
   246 @panic SqlDb 9 In _DEBUG mode. Invalid aLocation parameter.
   227 */
   247 */
   228 TStreamPos HBlobBuf::DoSeekL(MStreamBuf::TMark aMark, TStreamLocation aLocation, TInt aOffset)
   248 TStreamPos HBlobBuf::DoSeekL(MStreamBuf::TMark aMark, TStreamLocation aLocation, TInt aOffset)
   229 	{
   249 	{
   230 	__SQLASSERT_ALWAYS(!(aMark & ~(ERead | EWrite)), ESqlPanicStreamMarkInvalid);
   250 	SQL_TRACE_BLOB(OstTraceExt5(TRACE_INTERNALS, HBLOBBUF_DOSEEKL, "0x%X;HBlobBuf::DoSeekL;aMark=%d;aLocation=%d;aOffset=%d;iBlobSize=%d", (TUint)this, (TInt)aMark, (TInt)aLocation, aOffset, iBlobSize));
   231 	__SQLASSERT(aOffset >= 0, ESqlPanicBadArgument);
   251 	__ASSERT_ALWAYS(!(aMark & ~(ERead | EWrite)), __SQLPANIC(ESqlPanicStreamMarkInvalid));
   232 	__SQLASSERT(iBlobHandle != NULL, ESqlPanicInvalidObj);
   252 	__ASSERT_DEBUG(aOffset >= 0, __SQLPANIC(ESqlPanicBadArgument));
       
   253 	__ASSERT_DEBUG(iBlobHandle != NULL, __SQLPANIC(ESqlPanicInvalidObj));
   233 	
   254 	
   234 	TInt newPos = 0;
   255 	TInt newPos = 0;
   235 	switch(aLocation)
   256 	switch(aLocation)
   236 		{
   257 		{
   237 	case EStreamBeginning:
   258 	case EStreamBeginning:
   242 		break;
   263 		break;
   243 	case EStreamEnd:
   264 	case EStreamEnd:
   244 		newPos = iBlobSize + aOffset;
   265 		newPos = iBlobSize + aOffset;
   245 		break;
   266 		break;
   246 	default:
   267 	default:
   247 		__SQLASSERT(0, ESqlPanicStreamLocationInvalid);
   268 		__ASSERT_DEBUG(0, __SQLPANIC(ESqlPanicStreamLocationInvalid));
   248 		newPos = -1;
   269 		newPos = -1;
   249 		break;
   270 		break;
   250 		}
   271 		}
   251 	if(newPos < 0 || newPos > iBlobSize)
   272 	if(newPos < 0 || newPos > iBlobSize)
   252 		{
   273 		{