persistentstorage/sql/SRC/Server/SqlSrvStatementUtil.cpp
branchRCL_3
changeset 23 26645d81f48d
parent 15 fcc16690f446
child 24 cc28652e0254
equal deleted inserted replaced
21:28839de615b4 23:26645d81f48d
    16 #include <e32debug.h>
    16 #include <e32debug.h>
    17 #include <hal.h>
    17 #include <hal.h>
    18 #include <sqldb.h>
    18 #include <sqldb.h>
    19 #include "sqlite3.h"
    19 #include "sqlite3.h"
    20 #include "SqlSrvStatementUtil.h"
    20 #include "SqlSrvStatementUtil.h"
    21 #include "SqlPanic.h"
    21 #include "SqlAssert.h"
    22 #include "SqlSrvUtil.h"
    22 #include "SqlSrvUtil.h"
    23 #include "SqlUtil.h"
    23 #include "SqlUtil.h"
    24 #include "SqliteSymbian.h"		//sqlite3SymbianLastOsError()
    24 #include "SqliteSymbian.h"		//sqlite3SymbianLastOsError()
       
    25 #include "OstTraceDefinitions.h"
    25 #include "SqlSrvResourceProfiler.h"
    26 #include "SqlSrvResourceProfiler.h"
       
    27 #ifdef OST_TRACE_COMPILER_IN_USE
       
    28 #include "SqlSrvStatementUtilTraces.h"
       
    29 #endif
       
    30 #include "SqlTraceDef.h"
    26 
    31 
    27 //The database names in all statements are quoted to avoid the "sql injection" threat.
    32 //The database names in all statements are quoted to avoid the "sql injection" threat.
    28 _LIT8(KPageCountPragma, "PRAGMA \"%S\".page_count\x0");
    33 _LIT8(KPageCountPragma, "PRAGMA \"%S\".page_count\x0");
    29 _LIT8(KPageSizePragma,  "PRAGMA \"%S\".page_size\x0");
    34 _LIT8(KPageSizePragma,  "PRAGMA \"%S\".page_size\x0");
    30 _LIT8(KCacheSizePragma,  "PRAGMA \"%S\".cache_size\x0");
    35 _LIT8(KCacheSizePragma,  "PRAGMA \"%S\".cache_size\x0");
    33 _LIT8(KVacuumModePragma, "PRAGMA \"%S\".auto_vacuum\x0");
    38 _LIT8(KVacuumModePragma, "PRAGMA \"%S\".auto_vacuum\x0");
    34 _LIT8(KIncrementalVacuumPragma, "PRAGMA \"%S\".incremental_vacuum(%d)\x0");
    39 _LIT8(KIncrementalVacuumPragma, "PRAGMA \"%S\".incremental_vacuum(%d)\x0");
    35 
    40 
    36 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    41 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    37 
    42 
    38 #ifdef _NOTIFY
       
    39 static void PrintErrMsg8(TBool aCondition, sqlite3* aDbHandle, const char* aFuncNameZ, const TDesC8& aStmt)
       
    40 	{
       
    41 	if(!aCondition)
       
    42 		{
       
    43 		return;	
       
    44 		}
       
    45 	TPtrC8 funcName8(reinterpret_cast <const TUint8*> (aFuncNameZ));
       
    46 	TBuf<32> funcName;
       
    47 	funcName.Copy(funcName8);
       
    48 	RDebug::Print(_L("###%S\r\n"), &funcName);
       
    49 	TInt stmtLen = aStmt.Length();
       
    50 	if(stmtLen > 0)
       
    51 		{
       
    52 		if(aStmt[stmtLen - 1] == 0)
       
    53 			{
       
    54 			--stmtLen;
       
    55 			}
       
    56 		HBufC* buf = HBufC::New(stmtLen);
       
    57 		if(buf)
       
    58 			{
       
    59 			TPtr sqlStmt = buf->Des();
       
    60 			sqlStmt.Copy(aStmt.Left(stmtLen));
       
    61 			if(sqlStmt.Length() > 250)
       
    62 				{
       
    63 				sqlStmt.SetLength(250);		
       
    64 				}
       
    65 			RDebug::Print(_L("###\"%S\"\r\n"), &sqlStmt);
       
    66 			delete buf;
       
    67 			}
       
    68 		}
       
    69 	TBuf<16> tbuf;
       
    70 	Util::GetTimeStr(tbuf);
       
    71 	const void* errMsg = sqlite3_errmsg16(aDbHandle);//"errMsg" - zero terminated string
       
    72 	if(errMsg)
       
    73 		{
       
    74 		TPtrC msg(reinterpret_cast <const TText16*> (errMsg));//terminating zero character excluded.
       
    75 		if(msg.Length() > 230)
       
    76 			{
       
    77 			msg.Set(msg.Left(230));		
       
    78 			}
       
    79 		RDebug::Print(_L("##%S#ErrMsg=%S\r\n"), &tbuf, &msg);
       
    80 		}
       
    81 	else
       
    82 		{
       
    83 		RDebug::Print(_L("##%S#ErrMsg=null\r\n"), &tbuf);
       
    84 		}
       
    85 	}
       
    86 	
       
    87 static void PrintErrMsg16(TBool aCondition, sqlite3* aDbHandle, const char* aFuncNameZ, const TDesC16& aStmt)
       
    88 	{
       
    89 	if(!aCondition)
       
    90 		{
       
    91 		return;	
       
    92 		}
       
    93 	TPtrC8 funcName8(reinterpret_cast <const TUint8*> (aFuncNameZ));
       
    94 	TBuf<32> funcName;
       
    95 	funcName.Copy(funcName8);
       
    96 	RDebug::Print(_L("###%S\r\n"), &funcName);
       
    97 	TInt stmtLen = aStmt.Length();
       
    98 	if(stmtLen > 0)
       
    99 		{
       
   100 		if(aStmt[stmtLen - 1] == 0)
       
   101 			{
       
   102 			--stmtLen;
       
   103 			}
       
   104 		TPtrC sqlStmt(aStmt.Ptr(), stmtLen);
       
   105 		if(sqlStmt.Length() > 250)
       
   106 			{
       
   107 			sqlStmt.Set(sqlStmt.Left(250));		
       
   108 			}
       
   109 		RDebug::Print(_L("###\"%S\"\r\n"), &sqlStmt);
       
   110 		}
       
   111 	TBuf<16> tbuf;
       
   112 	Util::GetTimeStr(tbuf);
       
   113 	const void* errMsg = sqlite3_errmsg16(aDbHandle);//"errMsg" - zero terminated string
       
   114 	if(errMsg)
       
   115 		{
       
   116 		TPtrC msg(reinterpret_cast <const TText16*> (errMsg));//terminating zero character excluded.
       
   117 		if(msg.Length() > 230)
       
   118 			{
       
   119 			msg.Set(msg.Left(230));		
       
   120 			}
       
   121 		RDebug::Print(_L("##%S#ErrMsg=%S\r\n"), &tbuf, &msg);
       
   122 		}
       
   123 	else
       
   124 		{
       
   125 		RDebug::Print(_L("##%S#ErrMsg=null\r\n"), &tbuf);
       
   126 		}
       
   127 	}
       
   128 
       
   129 #define PRINT_ERRMSG8(Condition, DbHandle, FuncNameZ, Stmt) PrintErrMsg8(Condition, DbHandle, FuncNameZ, Stmt)
       
   130 #define PRINT_ERRMSG16(Condition, DbHandle, FuncNameZ, Stmt) PrintErrMsg16(Condition, DbHandle, FuncNameZ, Stmt)
       
   131 
       
   132 #else  //_NOTIFY
       
   133 
       
   134 #define PRINT_ERRMSG8(Condition, DbHandle, FuncNameZ, Stmt) 
       
   135 #define PRINT_ERRMSG16(Condition, DbHandle, FuncNameZ, Stmt) 
       
   136 	
       
   137 #endif //_NOTIFY
       
   138 
       
   139 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   140 
       
   141 //Calls sqlite3_open16() to create or open database file with aFileNameZ.
    43 //Calls sqlite3_open16() to create or open database file with aFileNameZ.
   142 //aFileNameZ is UTF16 encoded, zero-terminated.
    44 //aFileNameZ is UTF16 encoded, zero-terminated.
   143 //The function returns system-wide errors or database specific errors.
    45 //The function returns system-wide errors or database specific errors.
   144 //The function may panic with code 7 in _DEBUG mode - internal error: the created database handle is NULL.
    46 //The function may panic with code 7 in _DEBUG mode - internal error: the created database handle is NULL.
   145 TInt CreateDbHandle16(const TDesC& aFileNameZ, sqlite3*& aDbHandle)
    47 TInt CreateDbHandle16(const TDesC& aFileNameZ, sqlite3*& aDbHandle)
   146 	{
    48 	{
   147 	(void)sqlite3SymbianLastOsError();//clear last OS error
    49 	(void)sqlite3SymbianLastOsError();//clear last OS error
   148 	TInt err = sqlite3_open16(aFileNameZ.Ptr(), &aDbHandle);
    50 	TInt err = sqlite3_open16(aFileNameZ.Ptr(), &aDbHandle);
   149 	__SQLASSERT(err == SQLITE_OK ? aDbHandle != NULL : ETrue, ESqlPanicInternalError);
    51 	__ASSERT_DEBUG(err == SQLITE_OK ? aDbHandle != NULL : ETrue, __SQLPANIC2(ESqlPanicInternalError));
   150 	if(err == SQLITE_OK)
    52 	if(err == SQLITE_OK)
   151 		{
    53 		{
   152 		(void)sqlite3_extended_result_codes(aDbHandle, 0);
    54 		(void)sqlite3_extended_result_codes(aDbHandle, 0);
   153 		}
    55 		}
   154 	//Get the return error code now, because the next "if" may destroy it.
    56 	//Get the return error code now, because the next "if" may destroy it.
   167 //The function may panic with code 7 in _DEBUG mode - internal error: the created database handle is NULL.
    69 //The function may panic with code 7 in _DEBUG mode - internal error: the created database handle is NULL.
   168 TInt CreateDbHandle8(const TDesC8& aFileNameZ, sqlite3*& aDbHandle)
    70 TInt CreateDbHandle8(const TDesC8& aFileNameZ, sqlite3*& aDbHandle)
   169 	{
    71 	{
   170 	(void)sqlite3SymbianLastOsError();//clear last OS error
    72 	(void)sqlite3SymbianLastOsError();//clear last OS error
   171 	TInt err = sqlite3_open((const char *) aFileNameZ.Ptr(), &aDbHandle);
    73 	TInt err = sqlite3_open((const char *) aFileNameZ.Ptr(), &aDbHandle);
   172 	__SQLASSERT(err == SQLITE_OK ? aDbHandle != NULL : ETrue, ESqlPanicInternalError);
    74 	__ASSERT_DEBUG(err == SQLITE_OK ? aDbHandle != NULL : ETrue, __SQLPANIC2(ESqlPanicInternalError));
   173 	if(err == SQLITE_OK)
    75 	if(err == SQLITE_OK)
   174 		{
    76 		{
   175 		(void)sqlite3_extended_result_codes(aDbHandle, 0);
    77 		(void)sqlite3_extended_result_codes(aDbHandle, 0);
   176 		}
    78 		}
   177 	//Get the return error code now, because the next "if" may destroy it.
    79 	//Get the return error code now, because the next "if" may destroy it.
   200 //
   102 //
   201 //If the function completes successfully, it returns SQLITE_ROW or SQLITE_DONE.
   103 //If the function completes successfully, it returns SQLITE_ROW or SQLITE_DONE.
   202 //If the function fails then it returns one of the SQLITE error codes.
   104 //If the function fails then it returns one of the SQLITE error codes.
   203 static TInt DoSingleStmtExec16(sqlite3 *aDbHandle, const TDesC16& aSql)
   105 static TInt DoSingleStmtExec16(sqlite3 *aDbHandle, const TDesC16& aSql)
   204 	{
   106 	{
   205 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInvalidObj);
   107     __ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInvalidObj));
   206 	__SQLASSERT(aSql.Length() > 0 ? (TInt)aSql[aSql.Length() - 1] == 0 : ETrue, ESqlPanicBadArgument);
   108     __ASSERT_DEBUG(aSql.Length() > 0 ? (TInt)aSql[aSql.Length() - 1] == 0 : ETrue, __SQLPANIC2(ESqlPanicBadArgument));
   207 	sqlite3_stmt* stmtHandle = NULL;
   109 	sqlite3_stmt* stmtHandle = NULL;
   208 	const void* stmtTail = NULL;
   110 	const void* stmtTail = NULL;
   209     //sqlite3_prepare16_v2() expects parameter #3 to be one of the following:
   111     //sqlite3_prepare16_v2() expects parameter #3 to be one of the following:
   210     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   112     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   211     // - negative value - the sql statement (parameter #2) is zero-terminated;
   113     // - negative value - the sql statement (parameter #2) is zero-terminated;
   212 	TInt err = sqlite3_prepare16_v2(aDbHandle, aSql.Ptr(), aSql.Length() * sizeof(TUint16) - sizeof(TUint16), &stmtHandle, &stmtTail);
   114 	TInt err = sqlite3_prepare16_v2(aDbHandle, aSql.Ptr(), aSql.Length() * sizeof(TUint16) - sizeof(TUint16), &stmtHandle, &stmtTail);
   213 	__SQLASSERT(err == SQLITE_OK ? !stmtTail || User::StringLength((const TUint16*)stmtTail) == 0 : !stmtHandle, ESqlPanicInternalError);
   115 	__ASSERT_DEBUG(err == SQLITE_OK ? !stmtTail || User::StringLength((const TUint16*)stmtTail) == 0 : !stmtHandle, __SQLPANIC2(ESqlPanicInternalError));
   214 	if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
   116 	if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
   215 		{
   117 		{
   216 		if(err == SQLITE_OK)
   118 		if(err == SQLITE_OK)
   217 			{
   119 			{
   218 			while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
   120 			while((err = sqlite3_step(stmtHandle)) == SQLITE_ROW)
   219 				{
   121 				{
   220 				}
   122 				}
   221 			if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   123 			if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   222 				{
   124 				{
   223 				err = sqlite3_reset(stmtHandle);
   125 				err = sqlite3_reset(stmtHandle);
   224 				__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   126 				__ASSERT_DEBUG(err != SQLITE_OK, __SQLPANIC2(ESqlPanicInternalError));
   225 				}
   127 				}
   226 			}
   128 			}
   227 		(void)sqlite3_finalize(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   129 		(void)sqlite3_finalize(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   228 		}
   130 		}
   229 	return err;
   131 	return err;
   298 @panic SqlDb 4 In _DEBUG mode if aSqlStmt is not zero-terminated string
   200 @panic SqlDb 4 In _DEBUG mode if aSqlStmt is not zero-terminated string
   299 @panic SqlDb 7 In _DEBUG mode if aDbHandle is NULL
   201 @panic SqlDb 7 In _DEBUG mode if aDbHandle is NULL
   300 
   202 
   301 @internalComponent
   203 @internalComponent
   302 */
   204 */
   303 TInt DbExecStmt16(sqlite3 *aDbHandle, TDes16& aSqlStmt)
   205 TInt DbExecStmt16(sqlite3* aDbHandle, TDes16& aSqlStmt)
   304 	{
   206 	{
   305 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   207     __SQLTRACE_INTERNALSEXPR(TPtrC sqlprnptr(aSqlStmt.Left(aSqlStmt.Length() - 1)));
   306 	__SQLASSERT(aSqlStmt.Length() > 0 ? (TInt)aSqlStmt[aSqlStmt.Length() - 1] == 0: ETrue, ESqlPanicBadArgument);
   208     SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, DBEXECSTMT16_ENTRY, "Entry;0x%X;DbExecStmt16;sql=%S", (TUint)aDbHandle, __SQLPRNSTR(sqlprnptr)));
       
   209 	
       
   210 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
       
   211 	__ASSERT_DEBUG(aSqlStmt.Length() > 0 ? (TInt)aSqlStmt[aSqlStmt.Length() - 1] == 0: ETrue, __SQLPANIC2(ESqlPanicBadArgument));
   307 
   212 
   308 	(void)sqlite3SymbianLastOsError();//clear last OS error
   213 	(void)sqlite3SymbianLastOsError();//clear last OS error
   309 	
   214 	
   310 	TInt err = SQLITE_DONE;
   215 	TInt err = SQLITE_DONE;
   311 	//Execute SQL statement(s)
   216 	//Execute SQL statement(s)
   323 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   228 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   324 	if(err == KSqlAtEnd)
   229 	if(err == KSqlAtEnd)
   325 		{
   230 		{
   326 		err = KErrNone;	
   231 		err = KErrNone;	
   327 		}
   232 		}
   328 	PRINT_ERRMSG16(err <= KSqlErrGeneral, aDbHandle, "DbExecStmt16()", aSqlStmt);
   233 	SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, DBEXECSTMT16_EXIT, "Exit;0x%X;DbExecStmt16;err=%d", (TUint)aDbHandle, err));
   329 	return err;
   234 	return err;
   330 	}
   235 	}
   331 
   236 
   332 /**
   237 /**
   333 Executes one or more 8-bit SQL statements. SQL statements of any kind can be executed, but the 
   238 Executes one or more 8-bit SQL statements. SQL statements of any kind can be executed, but the 
   347 @panic SqlDb 4 In _DEBUG mode if aSqlStmt is not zero-terminated string
   252 @panic SqlDb 4 In _DEBUG mode if aSqlStmt is not zero-terminated string
   348 @panic SqlDb 7 In _DEBUG mode if aDbHandle is NULL
   253 @panic SqlDb 7 In _DEBUG mode if aDbHandle is NULL
   349 
   254 
   350 @internalComponent
   255 @internalComponent
   351 */
   256 */
   352 TInt DbExecStmt8(sqlite3 *aDbHandle, const TDesC8& aSqlStmt)
   257 TInt DbExecStmt8(sqlite3* aDbHandle, const TDesC8& aSqlStmt)
   353 	{
   258 	{
   354 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   259     __SQLTRACE_INTERNALSEXPR(TPtrC8 sqlprnptr(aSqlStmt.Left(aSqlStmt.Length() - 1)));
   355 	__SQLASSERT(aSqlStmt.Length() > 0 ? (TInt)aSqlStmt[aSqlStmt.Length() - 1] == 0: ETrue, ESqlPanicBadArgument);
   260 	__SQLTRACE_INTERNALSVAR(TBuf<100> des16prnbuf);
       
   261     SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, DBEXECSTMT8_ENTRY, "Entry;0x%X;DbExecStmt8;sql=%s", (TUint)aDbHandle, __SQLPRNSTR8(sqlprnptr, des16prnbuf)));
       
   262     
       
   263 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
       
   264 	__ASSERT_DEBUG(aSqlStmt.Length() > 0 ? (TInt)aSqlStmt[aSqlStmt.Length() - 1] == 0: ETrue, __SQLPANIC2(ESqlPanicBadArgument));
   356 
   265 
   357     SQLPROFILER_SQL8_PRINT((TUint)aDbHandle, aSqlStmt.Left(aSqlStmt.Length() - 1), EFalse);
   266     SQLPROFILER_SQL8_PRINT((TUint)aDbHandle, aSqlStmt.Left(aSqlStmt.Length() - 1), EFalse);
   358 	
   267 	
   359 	(void)sqlite3SymbianLastOsError();//clear last OS error
   268 	(void)sqlite3SymbianLastOsError();//clear last OS error
   360 	
   269 	
   363 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   272 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   364 	if(err == KSqlAtEnd)
   273 	if(err == KSqlAtEnd)
   365 		{
   274 		{
   366 		err = KErrNone;	
   275 		err = KErrNone;	
   367 		}
   276 		}
   368 	PRINT_ERRMSG8(err <= KSqlErrGeneral, aDbHandle, "DbExecStmt8()", aSqlStmt);
   277 	SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, DBEXECSTMT8_EXIT, "Exit;0x%X;DbExecStmt8;err=%d", (TUint)aDbHandle, err));
   369 	return err;
   278 	return err;
   370 	}
   279 	}
   371 
   280 
   372 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
   281 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
   373 /////                     16-bit and 8-bit SQL statement preparation                                      /////
   282 /////                     16-bit and 8-bit SQL statement preparation                                      /////
   383     //sqlite3_prepare16_v2() expects parameter #3 to be one of the following:
   292     //sqlite3_prepare16_v2() expects parameter #3 to be one of the following:
   384     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   293     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   385     // - negative value - the sql statement (parameter #2) is zero-terminated;
   294     // - negative value - the sql statement (parameter #2) is zero-terminated;
   386 	TInt err = sqlite3_prepare16_v2(aDbHandle, aStmt.Ptr(), aStmt.Length() * sizeof(TUint16) - sizeof(TUint16), aStmtHandle, &stmtTail);
   295 	TInt err = sqlite3_prepare16_v2(aDbHandle, aStmt.Ptr(), aStmt.Length() * sizeof(TUint16) - sizeof(TUint16), aStmtHandle, &stmtTail);
   387 	aHasTail = stmtTail && static_cast <const TUint16*> (stmtTail)[0] != 0;
   296 	aHasTail = stmtTail && static_cast <const TUint16*> (stmtTail)[0] != 0;
   388 	PRINT_ERRMSG16(err != SQLITE_OK, aDbHandle, "DoPrepareStmt16()", aStmt);
   297 	__ASSERT_DEBUG(err != SQLITE_OK ? !(*aStmtHandle) : ETrue, __SQLPANIC2(ESqlPanicInternalError));
   389 	__SQLASSERT(err != SQLITE_OK ? !(*aStmtHandle) : ETrue, ESqlPanicInternalError);
       
   390 	//(*aStmtHandle) is NULL for ";" statements, when err == SQLITE_OK. Since the server should not panic
   298 	//(*aStmtHandle) is NULL for ";" statements, when err == SQLITE_OK. Since the server should not panic
   391 	//that situation is handled later (not inside the assert above)
   299 	//that situation is handled later (not inside the assert above)
   392 	return err;
   300 	return err;
   393 	}
   301 	}
   394 	
   302 	
   402     //sqlite3_prepare_v2() expects parameter #3 to be one of the following:
   310     //sqlite3_prepare_v2() expects parameter #3 to be one of the following:
   403     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   311     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   404     // - negative value - the sql statement (parameter #2) is zero-terminated;
   312     // - negative value - the sql statement (parameter #2) is zero-terminated;
   405 	TInt err = sqlite3_prepare_v2(aDbHandle, reinterpret_cast <const char*> (aStmt), -1, aStmtHandle, &stmtTail);
   313 	TInt err = sqlite3_prepare_v2(aDbHandle, reinterpret_cast <const char*> (aStmt), -1, aStmtHandle, &stmtTail);
   406 	aHasTail = stmtTail && stmtTail[0] != 0;
   314 	aHasTail = stmtTail && stmtTail[0] != 0;
   407 	PRINT_ERRMSG8(err != SQLITE_OK, aDbHandle, "DoPrepareStmt8()", TPtrC8(aStmt, aStmt ? User::StringLength(aStmt) : 0));
   315 	__ASSERT_DEBUG(err != SQLITE_OK ? !(*aStmtHandle) : ETrue, __SQLPANIC2(ESqlPanicInternalError));
   408 	__SQLASSERT(err != SQLITE_OK ? !(*aStmtHandle) : ETrue, ESqlPanicInternalError);
       
   409 	//(*aStmtHandle) is NULL for ";" statements, when err == SQLITE_OK. Since the server should not panic
   316 	//(*aStmtHandle) is NULL for ";" statements, when err == SQLITE_OK. Since the server should not panic
   410 	//that situation is handled later (not inside the assert above)
   317 	//that situation is handled later (not inside the assert above)
   411 	return err;
   318 	return err;
   412 	}
   319 	}
   413 
   320 
   449 // - aHasTail is true (possibly more than one SQL statement, separated with ";");
   356 // - aHasTail is true (possibly more than one SQL statement, separated with ";");
   450 // - aStmtHandle is NULL;
   357 // - aStmtHandle is NULL;
   451 //
   358 //
   452 static void LeaveIfPrepareErrorL(TInt aSqliteError, TBool aHasTail, sqlite3_stmt* aStmtHandle)
   359 static void LeaveIfPrepareErrorL(TInt aSqliteError, TBool aHasTail, sqlite3_stmt* aStmtHandle)
   453 	{
   360 	{
   454 	__SQLLEAVE_IF_ERROR(ProcessPrepareError(aSqliteError, aHasTail, aStmtHandle));
   361 	__SQLLEAVE_IF_ERROR2(ProcessPrepareError(aSqliteError, aHasTail, aStmtHandle));
   455 	}
   362 	}
   456 
   363 
   457 /**
   364 /**
   458 Prepares 16-bit aSqlStmt SQL statement.
   365 Prepares 16-bit aSqlStmt SQL statement.
   459 
   366 
   467 
   374 
   468 @internalComponent
   375 @internalComponent
   469 */
   376 */
   470 sqlite3_stmt* StmtPrepare16L(sqlite3* aDbHandle, const TDesC16& aSqlStmt)
   377 sqlite3_stmt* StmtPrepare16L(sqlite3* aDbHandle, const TDesC16& aSqlStmt)
   471 	{
   378 	{
   472     SQLPROFILER_SQL16_PRINT((TUint)aDbHandle, aSqlStmt.Left(aSqlStmt.Length() - 1), ETrue);
   379     __SQLTRACE_INTERNALSEXPR(TPtrC sqlprnptr(aSqlStmt.Left(aSqlStmt.Length() - 1)));
   473 	
   380     SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, STMTPREPARE16L_ENTRY, "Entry;0x%X;StmtPrepare16L;sql=%S", (TUint)aDbHandle, __SQLPRNSTR(sqlprnptr)));	
   474 	(void)sqlite3SymbianLastOsError();//clear last OS error
   381 	(void)sqlite3SymbianLastOsError();//clear last OS error
   475 	TBool hasTail = EFalse;
   382 	TBool hasTail = EFalse;
   476 	sqlite3_stmt* stmtHandle = NULL;
   383 	sqlite3_stmt* stmtHandle = NULL;
   477 	TInt err = DoPrepareStmt16(aDbHandle, aSqlStmt, &stmtHandle, hasTail);
   384 	TInt err = DoPrepareStmt16(aDbHandle, aSqlStmt, &stmtHandle, hasTail);
   478 	LeaveIfPrepareErrorL(err, hasTail, stmtHandle);
   385 	LeaveIfPrepareErrorL(err, hasTail, stmtHandle);
       
   386 	SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, STMTPREPARE16L_EXIT, "Exit;0x%X;StmtPrepare16L;stmtHandle=0x%X", (TUint)aDbHandle, (TUint)stmtHandle)); 
   479 	return stmtHandle;
   387 	return stmtHandle;
   480 	}
   388 	}
   481 
   389 
   482 /**
   390 /**
   483 Prepares 8-bit aSqlStmt SQL statement.
   391 Prepares 8-bit aSqlStmt SQL statement.
   492 
   400 
   493 @internalComponent
   401 @internalComponent
   494 */
   402 */
   495 TInt StmtPrepare8(sqlite3* aDbHandle, const TDesC8& aSqlStmt, sqlite3_stmt*& aStmtHandle)
   403 TInt StmtPrepare8(sqlite3* aDbHandle, const TDesC8& aSqlStmt, sqlite3_stmt*& aStmtHandle)
   496 	{
   404 	{
   497     SQLPROFILER_SQL8_PRINT((TUint)aDbHandle, aSqlStmt.Left(aSqlStmt.Length() - 1), ETrue);
   405     __SQLTRACE_INTERNALSEXPR(TPtrC8 sqlprnptr(aSqlStmt.Left(aSqlStmt.Length() - 1)));
   498 	
   406 	__SQLTRACE_INTERNALSVAR(TBuf<100> des16prnbuf);
       
   407     SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, STMTPREPARE8_ENTRY, "Entry;0x%X;StmtPrepare8;sql=%s", (TUint)aDbHandle, __SQLPRNSTR8(sqlprnptr, des16prnbuf))); 
   499 	(void)sqlite3SymbianLastOsError();//clear last OS error
   408 	(void)sqlite3SymbianLastOsError();//clear last OS error
   500 	TBool hasTail = EFalse;
   409 	TBool hasTail = EFalse;
   501 	TInt err = DoPrepareStmt8(aDbHandle, aSqlStmt.Ptr(), &aStmtHandle, hasTail);
   410 	TInt err = DoPrepareStmt8(aDbHandle, aSqlStmt.Ptr(), &aStmtHandle, hasTail);
   502 	return ProcessPrepareError(err, hasTail, aStmtHandle);
   411 	err = ProcessPrepareError(err, hasTail, aStmtHandle);
       
   412 	SQL_TRACE_INTERNALS(OstTraceExt3(TRACE_INTERNALS, STMTPREPARE8_EXIT, "Exit;0x%X;StmtPrepare8;aStmtHandle=0x%X;err=%d", (TUint)aDbHandle, (TUint)aStmtHandle, err)); 
       
   413 	return err;
   503 	}
   414 	}
   504 
   415 
   505 /**
   416 /**
   506 Prepares 8-bit aSqlStmt SQL statement.
   417 Prepares 8-bit aSqlStmt SQL statement.
   507 
   418 
   515 
   426 
   516 @internalComponent
   427 @internalComponent
   517 */
   428 */
   518 sqlite3_stmt* StmtPrepare8L(sqlite3* aDbHandle, const TDesC8& aSqlStmt)
   429 sqlite3_stmt* StmtPrepare8L(sqlite3* aDbHandle, const TDesC8& aSqlStmt)
   519 	{
   430 	{
   520     SQLPROFILER_SQL8_PRINT((TUint)aDbHandle, aSqlStmt.Left(aSqlStmt.Length() - 1), ETrue);
   431     __SQLTRACE_INTERNALSEXPR(TPtrC8 sqlprnptr(aSqlStmt.Left(aSqlStmt.Length() - 1)));
   521 	
   432 	__SQLTRACE_INTERNALSVAR(TBuf<100> des16prnbuf);
       
   433     SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, STMTPREPARE8L_ENTRY, "Entry;0x%X;StmtPrepare8L;sql=%s", (TUint)aDbHandle, __SQLPRNSTR8(sqlprnptr, des16prnbuf))); 
   522 	(void)sqlite3SymbianLastOsError();//clear last OS error
   434 	(void)sqlite3SymbianLastOsError();//clear last OS error
   523 	TBool hasTail = EFalse;
   435 	TBool hasTail = EFalse;
   524 	sqlite3_stmt* stmtHandle = NULL;
   436 	sqlite3_stmt* stmtHandle = NULL;
   525 	TInt err = DoPrepareStmt8(aDbHandle, aSqlStmt.Ptr(), &stmtHandle, hasTail);
   437 	TInt err = DoPrepareStmt8(aDbHandle, aSqlStmt.Ptr(), &stmtHandle, hasTail);
   526 	LeaveIfPrepareErrorL(err, hasTail, stmtHandle);
   438 	LeaveIfPrepareErrorL(err, hasTail, stmtHandle);
       
   439 	SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, STMTPREPARE8L_EXIT, "Exit;0x%X;StmtPrepare8L;stmtHandle=0x%X", (TUint)aDbHandle, (TUint)stmtHandle)); 
   527 	return stmtHandle;
   440 	return stmtHandle;
   528 	}
   441 	}
   529 
   442 
   530 /**
   443 /**
   531 Executes upon completion the prepared SQL statement.
   444 Executes upon completion the prepared SQL statement.
   542 
   455 
   543 @internalComponent
   456 @internalComponent
   544 */	
   457 */	
   545 TInt StmtExec(sqlite3_stmt* aStmtHandle)
   458 TInt StmtExec(sqlite3_stmt* aStmtHandle)
   546 	{
   459 	{
   547 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   460 	SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, STMTEXEC_ENTRY, "Entry;0x%X;StmtExec;aStmtHandle=0x%X", (TUint)sqlite3_db_handle(aStmtHandle), (TUint)aStmtHandle));
   548 	
   461 	__ASSERT_DEBUG(aStmtHandle != NULL, __SQLPANIC2(ESqlPanicInvalidObj));
   549 	(void)sqlite3SymbianLastOsError();//clear last OS error
   462 	
   550 		
   463 	(void)sqlite3SymbianLastOsError();//clear last OS error
       
   464 
   551 	TInt err;
   465 	TInt err;
   552 	while((err = sqlite3_step(aStmtHandle)) == SQLITE_ROW)
   466 	while((err = sqlite3_step(aStmtHandle)) == SQLITE_ROW)
   553 		{
   467 		{
   554 		}
   468 		}
   555 		
   469 		
   556 	if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   470 	if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   557 		{
   471 		{
   558 		err = sqlite3_reset(aStmtHandle);
   472 		err = sqlite3_reset(aStmtHandle);
   559 		__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   473 		__ASSERT_DEBUG(err != SQLITE_OK, __SQLPANIC2(ESqlPanicInternalError));
   560 		}
   474 		}
   561 		
   475 		
   562 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   476 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   563 	if(err == KSqlAtEnd)
   477 	if(err == KSqlAtEnd)
   564 		{
   478 		{
   565 		err = KErrNone;	
   479 		err = KErrNone;	
   566 		}
   480 		}
   567 
   481 
   568 	PRINT_ERRMSG16(err <= KSqlErrGeneral, sqlite3_db_handle(aStmtHandle), "StmtExec()", _L(" "));
   482 	SQL_TRACE_INTERNALS(OstTraceExt3(TRACE_INTERNALS, STMTEXEC_EXIT, "Exit;0x%X;StmtExec;aStmtHandle=0x%X;err=%d", (TUint)sqlite3_db_handle(aStmtHandle), (TUint)aStmtHandle, err));
   569 	return err;
   483 	return err;
   570 	}
   484 	}
   571 
   485 
   572 /**
   486 /**
   573 Executes the SQL statement moving it to the next row if available.
   487 Executes the SQL statement moving it to the next row if available.
   586 
   500 
   587 @internalComponent
   501 @internalComponent
   588 */	
   502 */	
   589 TInt StmtNext(sqlite3_stmt* aStmtHandle)
   503 TInt StmtNext(sqlite3_stmt* aStmtHandle)
   590 	{
   504 	{
   591 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   505 	SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, STMTNEXT_ENTRY, "Entry;0x%X;StmtNext;aStmtHandle=0x%X", (TUint)sqlite3_db_handle(aStmtHandle), (TUint)aStmtHandle));
       
   506 	__ASSERT_DEBUG(aStmtHandle != NULL, __SQLPANIC2(ESqlPanicInvalidObj));
   592 	
   507 	
   593 	(void)sqlite3SymbianLastOsError();//clear last OS error
   508 	(void)sqlite3SymbianLastOsError();//clear last OS error
   594 	
   509 	
   595 	TInt err = sqlite3_step(aStmtHandle);
   510 	TInt err = sqlite3_step(aStmtHandle);
   596 	if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   511 	if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   597 		{
   512 		{
   598 		err = sqlite3_reset(aStmtHandle);
   513 		err = sqlite3_reset(aStmtHandle);
   599 		__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   514 		__ASSERT_DEBUG(err != SQLITE_OK, __SQLPANIC2(ESqlPanicInternalError));
   600 		}
   515 		}
   601 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   516 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   602 	PRINT_ERRMSG16(err <= KSqlErrGeneral, sqlite3_db_handle(aStmtHandle), "StmtNext()", _L(" "));
   517 	SQL_TRACE_INTERNALS(OstTraceExt3(TRACE_INTERNALS, STMTNEXT_EXIT, "Exit;0x%X;StmtNext;aStmtHandle=0x%X;err=%d", (TUint)sqlite3_db_handle(aStmtHandle), (TUint)aStmtHandle, err));
   603 	return err;
   518 	return err;
   604 	}
   519 	}
   605 
   520 
   606 /**
   521 /**
   607 Resets the prepared SQL statement to its initial state and makes it ready to be executed again.
   522 Resets the prepared SQL statement to its initial state and makes it ready to be executed again.
   615 
   530 
   616 @internalComponent
   531 @internalComponent
   617 */	
   532 */	
   618 TInt StmtReset(sqlite3_stmt* aStmtHandle)
   533 TInt StmtReset(sqlite3_stmt* aStmtHandle)
   619 	{
   534 	{
   620 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   535 	SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, STMTRESET_ENTRY, "Entry;0x%X;StmtReset;aStmtHandle=0x%X", (TUint)sqlite3_db_handle(aStmtHandle), (TUint)aStmtHandle));
       
   536 	__ASSERT_DEBUG(aStmtHandle != NULL, __SQLPANIC2(ESqlPanicInvalidObj));
   621 	
   537 	
   622 	(void)sqlite3SymbianLastOsError();//clear last OS error
   538 	(void)sqlite3SymbianLastOsError();//clear last OS error
   623 	
   539 	
   624 	TInt err = sqlite3_reset(aStmtHandle);
   540 	TInt err = sqlite3_reset(aStmtHandle);
   625 	return ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   541 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
       
   542 	SQL_TRACE_INTERNALS(OstTraceExt3(TRACE_INTERNALS, STMTRESET_EXIT, "Exit;0x%X;StmtReset;aStmtHandle=0x%X;err=%d", (TUint)sqlite3_db_handle(aStmtHandle), (TUint)aStmtHandle, err));
       
   543 	return err;
   626 	}
   544 	}
   627 
   545 
   628 /**
   546 /**
   629 Prepares and executes PRAGMA statement and moves the statement cursor on the first row.
   547 Prepares and executes PRAGMA statement and moves the statement cursor on the first row.
   630 
   548 
   642 
   560 
   643 @internalComponent
   561 @internalComponent
   644 */
   562 */
   645 static TInt PreRetrievePragmaValue(sqlite3* aDbHandle, const TDesC& aDbName, const TDesC8& aPragmaSql, sqlite3_stmt*& aStmtHandle)
   563 static TInt PreRetrievePragmaValue(sqlite3* aDbHandle, const TDesC& aDbName, const TDesC8& aPragmaSql, sqlite3_stmt*& aStmtHandle)
   646 	{
   564 	{
   647 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   565     __SQLTRACE_INTERNALSEXPR(TPtrC8 sqlprnptr(aPragmaSql.Left(aPragmaSql.Length() - 1)));
   648 	__SQLASSERT(aPragmaSql.Length() > 0, ESqlPanicBadArgument);
   566 	__SQLTRACE_INTERNALSVAR(TBuf<100> des16prnbuf);
   649 	__SQLASSERT(aPragmaSql[aPragmaSql.Length() - 1] == 0, ESqlPanicBadArgument);
   567     SQL_TRACE_INTERNALS(OstTraceExt3(TRACE_INTERNALS, PRERETRIEVEPRAGMAVALUE_ENTRY, "Entry;0x%X;PreRetrievePragmaValue;aDbName=%S;aPragmaSql=%s", (TUint)aDbHandle, __SQLPRNSTR(aDbName), __SQLPRNSTR8(sqlprnptr, des16prnbuf)));
       
   568 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
       
   569 	__ASSERT_DEBUG(aPragmaSql.Length() > 0, __SQLPANIC2(ESqlPanicBadArgument));
       
   570 	__ASSERT_DEBUG(aPragmaSql[aPragmaSql.Length() - 1] == 0, __SQLPANIC2(ESqlPanicBadArgument));
   650 	TBuf8<KMaxFileName> dbName;
   571 	TBuf8<KMaxFileName> dbName;
   651 	if(!UTF16ToUTF8(aDbName, dbName))
   572 	if(!UTF16ToUTF8(aDbName, dbName))
   652 		{
   573 		{
       
   574 		SQL_TRACE_INTERNALS(OstTrace1(TRACE_INTERNALS, PRERETRIEVEPRAGMAVALUE_EXIT1, "Exit;0x%X;PreRetrievePragmaValue;err=KErrGeneral", (TUint)aDbHandle));
   653 		return KErrGeneral;
   575 		return KErrGeneral;
   654 		}
   576 		}
   655 	TBuf8<KMaxFileName + 64> sql;//64 characters is enough for the longest PRAGMA statement
   577 	TBuf8<KMaxFileName + 64> sql;//64 characters is enough for the longest PRAGMA statement
   656 	if(dbName == KNullDesC8)
   578 	if(dbName == KNullDesC8)
   657 		{
   579 		{
   663 		}
   585 		}
   664 	aStmtHandle = NULL;
   586 	aStmtHandle = NULL;
   665 	TInt err = ::StmtPrepare8(aDbHandle, sql, aStmtHandle);
   587 	TInt err = ::StmtPrepare8(aDbHandle, sql, aStmtHandle);
   666 	if(err == KErrNone)
   588 	if(err == KErrNone)
   667 		{
   589 		{
   668 		__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   590 		__ASSERT_DEBUG(aStmtHandle != NULL, __SQLPANIC2(ESqlPanicInvalidObj));
   669 		err = ::StmtNext(aStmtHandle);
   591 		err = ::StmtNext(aStmtHandle);
   670 		}
   592 		}
   671 	PRINT_ERRMSG8(err <= KSqlErrGeneral, aDbHandle, "PreRetrievePragmaValue()", sql);
   593 	SQL_TRACE_INTERNALS(OstTraceExt3(TRACE_INTERNALS, PRERETRIEVEPRAGMAVALUE_EXIT2, "Exit;0x%X;PreRetrievePragmaValue;aStmtHandle=0x%X;err=%d", (TUint)aDbHandle, (TUint)aStmtHandle, err));
   672 	return err;
   594 	return err;
   673 	}
   595 	}
   674 
   596 
   675 /**
   597 /**
   676 Prepares and executes PRAGMA statement and retrieves the value of column 0 (the pragma value).
   598 Prepares and executes PRAGMA statement and retrieves the value of column 0 (the pragma value).
   689 
   611 
   690 @internalComponent
   612 @internalComponent
   691 */
   613 */
   692 static TInt RetrievePragmaValue(sqlite3* aDbHandle, const TDesC& aDbName, const TDesC8& aPragmaSql, TInt& aPragmaValue)
   614 static TInt RetrievePragmaValue(sqlite3* aDbHandle, const TDesC& aDbName, const TDesC8& aPragmaSql, TInt& aPragmaValue)
   693 	{
   615 	{
   694 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   616 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
   695 	__SQLASSERT(aPragmaSql.Length() > 0, ESqlPanicBadArgument);
   617 	__ASSERT_DEBUG(aPragmaSql.Length() > 0, __SQLPANIC2(ESqlPanicBadArgument));
   696 	__SQLASSERT(aPragmaSql[aPragmaSql.Length() - 1] == 0, ESqlPanicBadArgument);
   618 	__ASSERT_DEBUG(aPragmaSql[aPragmaSql.Length() - 1] == 0, __SQLPANIC2(ESqlPanicBadArgument));
   697 	sqlite3_stmt* stmtHandle = NULL;
   619 	sqlite3_stmt* stmtHandle = NULL;
   698 	TInt err = PreRetrievePragmaValue(aDbHandle, aDbName, aPragmaSql, stmtHandle);
   620 	TInt err = PreRetrievePragmaValue(aDbHandle, aDbName, aPragmaSql, stmtHandle);
   699 	if(err == KSqlAtRow)
   621 	if(err == KSqlAtRow)
   700 		{
   622 		{
   701 		aPragmaValue = sqlite3_column_int(stmtHandle, 0);
   623 		aPragmaValue = sqlite3_column_int(stmtHandle, 0);
   722 
   644 
   723 @internalComponent
   645 @internalComponent
   724 */
   646 */
   725 static TInt RetrievePragmaValue(sqlite3* aDbHandle, const TDesC& aDbName, const TDesC8& aPragmaSql, TDes8& aPragmaValue)
   647 static TInt RetrievePragmaValue(sqlite3* aDbHandle, const TDesC& aDbName, const TDesC8& aPragmaSql, TDes8& aPragmaValue)
   726 	{
   648 	{
   727 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   649 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
   728 	__SQLASSERT(aPragmaSql.Length() > 0, ESqlPanicBadArgument);
   650 	__ASSERT_DEBUG(aPragmaSql.Length() > 0, __SQLPANIC2(ESqlPanicBadArgument));
   729 	__SQLASSERT(aPragmaSql[aPragmaSql.Length() - 1] == 0, ESqlPanicBadArgument);
   651 	__ASSERT_DEBUG(aPragmaSql[aPragmaSql.Length() - 1] == 0, __SQLPANIC2(ESqlPanicBadArgument));
   730 	sqlite3_stmt* stmtHandle = NULL;
   652 	sqlite3_stmt* stmtHandle = NULL;
   731 	TInt err = PreRetrievePragmaValue(aDbHandle, aDbName, aPragmaSql, stmtHandle);
   653 	TInt err = PreRetrievePragmaValue(aDbHandle, aDbName, aPragmaSql, stmtHandle);
   732 	if(err == KSqlAtRow)
   654 	if(err == KSqlAtRow)
   733 		{
   655 		{
   734 		TPtrC8 ptr(sqlite3_column_text(stmtHandle, 0));
   656 		TPtrC8 ptr(sqlite3_column_text(stmtHandle, 0));
   754 
   676 
   755 @internalComponent
   677 @internalComponent
   756 */
   678 */
   757 TInt DbPageCount(sqlite3* aDbHandle, const TDesC& aDbName, TInt& aPageCount)
   679 TInt DbPageCount(sqlite3* aDbHandle, const TDesC& aDbName, TInt& aPageCount)
   758 	{
   680 	{
   759 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   681 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
   760 	return RetrievePragmaValue(aDbHandle, aDbName, KPageCountPragma, aPageCount);
   682 	return RetrievePragmaValue(aDbHandle, aDbName, KPageCountPragma, aPageCount);
   761 	}
   683 	}
   762 
   684 
   763 /**
   685 /**
   764 Retrieves the database page size.
   686 Retrieves the database page size.
   775 
   697 
   776 @internalComponent
   698 @internalComponent
   777 */
   699 */
   778 TInt DbPageSize(sqlite3* aDbHandle, const TDesC& aDbName, TInt& aPageSize)
   700 TInt DbPageSize(sqlite3* aDbHandle, const TDesC& aDbName, TInt& aPageSize)
   779 	{
   701 	{
   780 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   702 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
   781 	return RetrievePragmaValue(aDbHandle, aDbName, KPageSizePragma, aPageSize);
   703 	return RetrievePragmaValue(aDbHandle, aDbName, KPageSizePragma, aPageSize);
   782 	}
   704 	}
   783 
   705 
   784 /**
   706 /**
   785 Retrieves the database cache size in pages.
   707 Retrieves the database cache size in pages.
   796 
   718 
   797 @internalComponent
   719 @internalComponent
   798 */
   720 */
   799 TInt DbCacheSize(sqlite3* aDbHandle, const TDesC& aDbName, TInt& aCacheSize)
   721 TInt DbCacheSize(sqlite3* aDbHandle, const TDesC& aDbName, TInt& aCacheSize)
   800 	{
   722 	{
   801 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   723 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
   802 	return RetrievePragmaValue(aDbHandle, aDbName, KCacheSizePragma, aCacheSize);
   724 	return RetrievePragmaValue(aDbHandle, aDbName, KCacheSizePragma, aCacheSize);
   803 	}
   725 	}
   804 
   726 
   805 /**
   727 /**
   806 Retrieves the database encoding.
   728 Retrieves the database encoding.
   817 
   739 
   818 @internalComponent
   740 @internalComponent
   819 */
   741 */
   820 TInt DbEncoding(sqlite3* aDbHandle, const TDesC& aDbName, TDes8& aEncoding)
   742 TInt DbEncoding(sqlite3* aDbHandle, const TDesC& aDbName, TDes8& aEncoding)
   821 	{
   743 	{
   822 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   744 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
   823 	return RetrievePragmaValue(aDbHandle, aDbName, KEncodingPragma, aEncoding);
   745 	return RetrievePragmaValue(aDbHandle, aDbName, KEncodingPragma, aEncoding);
   824 	}
   746 	}
   825 
   747 
   826 /**
   748 /**
   827 Retrieves the database free pages count.
   749 Retrieves the database free pages count.
   838 
   760 
   839 @internalComponent
   761 @internalComponent
   840 */
   762 */
   841 TInt DbFreePageCount(sqlite3* aDbHandle, const TDesC& aDbName, TInt& aPageCount)
   763 TInt DbFreePageCount(sqlite3* aDbHandle, const TDesC& aDbName, TInt& aPageCount)
   842 	{
   764 	{
   843 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   765 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
   844 	return RetrievePragmaValue(aDbHandle, aDbName, KFreePageCountPragma, aPageCount);
   766 	return RetrievePragmaValue(aDbHandle, aDbName, KFreePageCountPragma, aPageCount);
   845 	}
   767 	}
   846 
   768 
   847 /**
   769 /**
   848 Retrieves the current vacuum mode of the database.
   770 Retrieves the current vacuum mode of the database.
   859 
   781 
   860 @internalComponent
   782 @internalComponent
   861 */
   783 */
   862 TInt DbVacuumMode(sqlite3* aDbHandle, const TDesC& aDbName, TInt& aVacuumMode)
   784 TInt DbVacuumMode(sqlite3* aDbHandle, const TDesC& aDbName, TInt& aVacuumMode)
   863 	{
   785 	{
   864 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   786 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
   865 	return RetrievePragmaValue(aDbHandle, aDbName, KVacuumModePragma, aVacuumMode);
   787 	return RetrievePragmaValue(aDbHandle, aDbName, KVacuumModePragma, aVacuumMode);
   866 	}
   788 	}
   867 
   789 
   868 static TBool IsCompactTimeLimitReached(TUint32 aStartTicks, TUint32 aCurrTicks, TInt aMaxTime)
   790 static TBool IsCompactTimeLimitReached(TUint32 aStartTicks, TUint32 aCurrTicks, TInt aMaxTime)
   869 	{
   791 	{
   870 	__SQLASSERT(aMaxTime > 0, ESqlPanicBadArgument);
   792 	__ASSERT_DEBUG(aMaxTime > 0, __SQLPANIC2(ESqlPanicBadArgument));
   871 	TInt64 tickDiff64 = (TInt64)aCurrTicks - (TInt64)aStartTicks;
   793 	TInt64 tickDiff64 = (TInt64)aCurrTicks - (TInt64)aStartTicks;
   872 	if(tickDiff64 < 0)
   794 	if(tickDiff64 < 0)
   873 		{
   795 		{
   874 		tickDiff64 = KMaxTUint32 + tickDiff64 + 1;
   796 		tickDiff64 = KMaxTUint32 + tickDiff64 + 1;
   875 		}
   797 		}
   876 	static TInt freq = 0;
   798 	static TInt freq = 0;
   877 	TInt err = KErrNone;
   799 	TInt err = KErrNone;
   878 	if(freq == 0)
   800 	if(freq == 0)
   879 		{
   801 		{
   880 		err = HAL::Get(HAL::EFastCounterFrequency, freq);
   802 		err = HAL::Get(HAL::EFastCounterFrequency, freq);
       
   803 		SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, ISCOMPACTTIMELIMITREACHED, "0;IsCompactTimeLimitReached;fast counter frequency=%d;err=%d", freq, err));
   881 		}
   804 		}
   882 	if(err == KErrNone && freq > 0)
   805 	if(err == KErrNone && freq > 0)
   883 		{
   806 		{
   884 		const TInt KMicroSecIn1Sec = 1000000;
   807 		const TInt KMicroSecIn1Sec = 1000000;
   885 		const TInt KMicroSecIn1Ms = 1000;
   808 		const TInt KMicroSecIn1Ms = 1000;
   910 
   833 
   911 @internalComponent
   834 @internalComponent
   912 */
   835 */
   913 TInt DbCompact(sqlite3* aDbHandle, const TDesC& aDbName, TInt aPageCount, TInt& aProcessedPageCount, TInt aMaxTime)
   836 TInt DbCompact(sqlite3* aDbHandle, const TDesC& aDbName, TInt aPageCount, TInt& aProcessedPageCount, TInt aMaxTime)
   914 	{
   837 	{
   915 	__SQLASSERT(aPageCount >= 0, ESqlPanicBadArgument);
   838 	SQL_TRACE_INTERNALS(OstTraceExt4(TRACE_INTERNALS, DBCOMPACT_ENTRY, "Entry;0x%X;DbCompact;aDbName=%S;aPageCount=%d;aMaxTime=%d", (TUint)aDbHandle, __SQLPRNSTR(aDbName), aPageCount, aMaxTime));
   916 	__SQLASSERT(aMaxTime >= 0, ESqlPanicBadArgument);
   839 	__ASSERT_DEBUG(aPageCount >= 0, __SQLPANIC2(ESqlPanicBadArgument));
   917 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInternalError);
   840 	__ASSERT_DEBUG(aMaxTime >= 0, __SQLPANIC2(ESqlPanicBadArgument));
       
   841 	__ASSERT_DEBUG(aDbHandle != NULL, __SQLPANIC2(ESqlPanicInternalError));
   918 	TBuf8<KMaxFileName> dbName;
   842 	TBuf8<KMaxFileName> dbName;
   919 	if(!UTF16ToUTF8(aDbName, dbName))
   843 	if(!UTF16ToUTF8(aDbName, dbName))
   920 		{
   844 		{
       
   845 		SQL_TRACE_INTERNALS(OstTrace1(TRACE_INTERNALS, DBCOMPACT_EXIT1, "Exit;0x%X;DbCompact;err=KErrGeneral", (TUint)aDbHandle));
   921 		return KErrGeneral;
   846 		return KErrGeneral;
   922 		}
   847 		}
   923 	TBuf8<KMaxFileName + sizeof(KIncrementalVacuumPragma) + 1> sql;
   848 	TBuf8<KMaxFileName + sizeof(KIncrementalVacuumPragma) + 1> sql;
   924 	if(dbName == KNullDesC8)
   849 	if(dbName == KNullDesC8)
   925 		{
   850 		{
   939 	aProcessedPageCount = 0;
   864 	aProcessedPageCount = 0;
   940 	//sqlite3_prepare_v2() expects parameter #3 to be one of the following:
   865 	//sqlite3_prepare_v2() expects parameter #3 to be one of the following:
   941 	// - byte length of the sql statement (parameter #2), excluding terminating zero;
   866 	// - byte length of the sql statement (parameter #2), excluding terminating zero;
   942 	// - negative value - the sql statement (parameter #2) is zero-terminated;
   867 	// - negative value - the sql statement (parameter #2) is zero-terminated;
   943 	TInt err = sqlite3_prepare_v2(aDbHandle, (const char*)sql.Ptr(), sql.Length() - sizeof(TUint8), &stmtHandle, &stmtTail);
   868 	TInt err = sqlite3_prepare_v2(aDbHandle, (const char*)sql.Ptr(), sql.Length() - sizeof(TUint8), &stmtHandle, &stmtTail);
   944 	__SQLASSERT(err == SQLITE_OK ? !stmtTail || User::StringLength((const TUint8*)stmtTail) == 0 : !stmtHandle, ESqlPanicInternalError);
   869 	__ASSERT_DEBUG(err == SQLITE_OK ? !stmtTail || User::StringLength((const TUint8*)stmtTail) == 0 : !stmtHandle, __SQLPANIC2(ESqlPanicInternalError));
   945 	if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
   870 	if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
   946 		{
   871 		{
   947 		if(err == SQLITE_OK)
   872 		if(err == SQLITE_OK)
   948 			{
   873 			{
   949 			TUint32 startTicks = 0;
   874 			TUint32 startTicks = 0;
   961 					}
   886 					}
   962 				}
   887 				}
   963 			if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   888 			if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   964 				{
   889 				{
   965 				err = sqlite3_reset(stmtHandle);
   890 				err = sqlite3_reset(stmtHandle);
   966 				__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   891 				__ASSERT_DEBUG(err != SQLITE_OK, __SQLPANIC2(ESqlPanicInternalError));
   967 				}
   892 				}
   968 			}
   893 			}
   969 		(void)sqlite3_finalize(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   894 		(void)sqlite3_finalize(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   970 		}
   895 		}
   971 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   896 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   972 	if(err == KSqlAtEnd)
   897 	if(err == KSqlAtEnd)
   973 		{
   898 		{
   974 		err = KErrNone;	
   899 		err = KErrNone;	
   975 		}
   900 		}
   976 	PRINT_ERRMSG8(err <= KSqlErrGeneral, aDbHandle, "DbCompact()", sql);
   901 	SQL_TRACE_INTERNALS(OstTraceExt3(TRACE_INTERNALS, DBCOMPACT_EXIT2, "Exit;0x%X;DbCompact;aProcessedPageCount=%d;err=%d", (TUint)aDbHandle, aProcessedPageCount, err));
   977 	return err;
   902 	return err;
   978 	}
   903 	}
   979 
   904 
   980 /**
   905 /**
   981 Finalizes the statement handle.
   906 Finalizes the statement handle.
   987 
   912 
   988 @internalComponent
   913 @internalComponent
   989 */
   914 */
   990 TInt FinalizeStmtHandle(sqlite3_stmt* aStmtHandle)
   915 TInt FinalizeStmtHandle(sqlite3_stmt* aStmtHandle)
   991 	{
   916 	{
       
   917 	SQL_TRACE_INTERNALS(OstTraceExt2(TRACE_INTERNALS, FINALIZESTMTHANDLE_ENTRY, "Entry;0x%X;FinalizeStmtHandle;aStmtHandle=0x%X", (TUint)sqlite3_db_handle(aStmtHandle), (TUint)aStmtHandle));
   992 	TInt err = KErrNone;
   918 	TInt err = KErrNone;
   993 	if(aStmtHandle)
   919 	if(aStmtHandle)
   994 		{
   920 		{
   995 		(void)sqlite3SymbianLastOsError();//clear last OS error
   921 		(void)sqlite3SymbianLastOsError();//clear last OS error
   996 		err = sqlite3_finalize(aStmtHandle);
   922 		err = sqlite3_finalize(aStmtHandle);
   997 		err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   923 		err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   998 		}
   924 		}
   999 	return err;
   925 	SQL_TRACE_INTERNALS(OstTrace1(TRACE_INTERNALS, FINALIZESTMTHANDLE_EXIT, "Exit;0;FinalizeStmtHandle;err=%d", err));
  1000 	}
   926 	return err;
       
   927 	}