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