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