persistentstorage/sql/SRC/Server/SqlSrvStatementUtil.cpp
changeset 17 55f2396f6d25
parent 0 08ec8eefde2f
child 16 6b6fd149daa2
child 22 a7ba600cb39d
equal deleted inserted replaced
15:3eacc0623088 17:55f2396f6d25
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
   203 static TInt DoSingleStmtExec16(sqlite3 *aDbHandle, const TDesC16& aSql)
   203 static TInt DoSingleStmtExec16(sqlite3 *aDbHandle, const TDesC16& aSql)
   204 	{
   204 	{
   205 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInvalidObj);
   205 	__SQLASSERT(aDbHandle != NULL, ESqlPanicInvalidObj);
   206 	sqlite3_stmt* stmtHandle = NULL;
   206 	sqlite3_stmt* stmtHandle = NULL;
   207 	const void* stmtTail = NULL;
   207 	const void* stmtTail = NULL;
   208 	//sqlite3_prepare16_v2() expects parameter #3 to be one of the following:
   208     //sqlite3_prepare16_v2() expects parameter #3 to be one of the following:
   209 	// - byte length of the sql statement, excluding terminating zero;
   209     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   210 	// - negative value - zero-terminated sql statement;
   210     // - negative value - the sql statement (parameter #2) is zero-terminated;
   211 	TInt err = sqlite3_prepare16_v2(aDbHandle, aSql.Ptr(), aSql.Length() * sizeof(TUint16) - sizeof(TUint16), &stmtHandle, &stmtTail);
   211 	TInt err = sqlite3_prepare16_v2(aDbHandle, aSql.Ptr(), aSql.Length() * sizeof(TUint16) - sizeof(TUint16), &stmtHandle, &stmtTail);
   212 	__SQLASSERT(err == SQLITE_OK ? !stmtTail || User::StringLength((const TUint16*)stmtTail) == 0 : !stmtHandle, ESqlPanicInternalError);
   212 	__SQLASSERT(err == SQLITE_OK ? !stmtTail || User::StringLength((const TUint16*)stmtTail) == 0 : !stmtHandle, ESqlPanicInternalError);
   213 	if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
   213 	if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
   214 		{
   214 		{
   215 		if(err == SQLITE_OK)
   215 		if(err == SQLITE_OK)
   381 //aStmt - zero-terminated string.
   381 //aStmt - zero-terminated string.
   382 //Returns one of SQLITE error codes.
   382 //Returns one of SQLITE error codes.
   383 static TInt DoPrepareStmt16(sqlite3* aDbHandle, const TDesC& aStmt, sqlite3_stmt** aStmtHandle, TBool& aHasTail)
   383 static TInt DoPrepareStmt16(sqlite3* aDbHandle, const TDesC& aStmt, sqlite3_stmt** aStmtHandle, TBool& aHasTail)
   384 	{
   384 	{
   385 	const void* stmtTail = NULL;
   385 	const void* stmtTail = NULL;
   386 	//sqlite3_prepare16_v2() expects parameter #3 to be one of the following:
   386     //sqlite3_prepare16_v2() expects parameter #3 to be one of the following:
   387 	// - byte length of the sql statement, excluding terminating zero;
   387     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   388 	// - negative value - zero-terminated sql statement;
   388     // - negative value - the sql statement (parameter #2) is zero-terminated;
   389 	TInt err = sqlite3_prepare16_v2(aDbHandle, aStmt.Ptr(), aStmt.Length() * sizeof(TUint16) - sizeof(TUint16), aStmtHandle, &stmtTail);
   389 	TInt err = sqlite3_prepare16_v2(aDbHandle, aStmt.Ptr(), aStmt.Length() * sizeof(TUint16) - sizeof(TUint16), aStmtHandle, &stmtTail);
   390 	aHasTail = stmtTail && static_cast <const TUint16*> (stmtTail)[0] != 0;
   390 	aHasTail = stmtTail && static_cast <const TUint16*> (stmtTail)[0] != 0;
   391 	PRINT_ERRMSG16(err != SQLITE_OK, aDbHandle, "DoPrepareStmt16()", aStmt);
   391 	PRINT_ERRMSG16(err != SQLITE_OK, aDbHandle, "DoPrepareStmt16()", aStmt);
   392 	__SQLASSERT(err != SQLITE_OK ? !(*aStmtHandle) : ETrue, ESqlPanicInternalError);
   392 	__SQLASSERT(err != SQLITE_OK ? !(*aStmtHandle) : ETrue, ESqlPanicInternalError);
   393 	//(*aStmtHandle) is NULL for ";" statements, when err == SQLITE_OK. Since the server should not panic
   393 	//(*aStmtHandle) is NULL for ";" statements, when err == SQLITE_OK. Since the server should not panic
   400 //aStmt - zero-terminated string.
   400 //aStmt - zero-terminated string.
   401 //Returns one of SQLITE error codes.
   401 //Returns one of SQLITE error codes.
   402 static TInt DoPrepareStmt8(sqlite3* aDbHandle, const TUint8* aStmt, sqlite3_stmt** aStmtHandle, TBool& aHasTail)
   402 static TInt DoPrepareStmt8(sqlite3* aDbHandle, const TUint8* aStmt, sqlite3_stmt** aStmtHandle, TBool& aHasTail)
   403 	{
   403 	{
   404 	const char* stmtTail = NULL;
   404 	const char* stmtTail = NULL;
   405 	//sqlite3_prepare_v2() expects parameter #3 to be one of the following:
   405     //sqlite3_prepare_v2() expects parameter #3 to be one of the following:
   406 	// - byte length of the sql statement;
   406     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   407 	// - negative value - zero-terminated sql statement;
   407     // - negative value - the sql statement (parameter #2) is zero-terminated;
   408 	TInt err = sqlite3_prepare_v2(aDbHandle, reinterpret_cast <const char*> (aStmt), -1, aStmtHandle, &stmtTail);
   408 	TInt err = sqlite3_prepare_v2(aDbHandle, reinterpret_cast <const char*> (aStmt), -1, aStmtHandle, &stmtTail);
   409 	aHasTail = stmtTail && stmtTail[0] != 0;
   409 	aHasTail = stmtTail && stmtTail[0] != 0;
   410 	PRINT_ERRMSG8(err != SQLITE_OK, aDbHandle, "DoPrepareStmt8()", TPtrC8(aStmt, aStmt ? User::StringLength(aStmt) : 0));
   410 	PRINT_ERRMSG8(err != SQLITE_OK, aDbHandle, "DoPrepareStmt8()", TPtrC8(aStmt, aStmt ? User::StringLength(aStmt) : 0));
   411 	__SQLASSERT(err != SQLITE_OK ? !(*aStmtHandle) : ETrue, ESqlPanicInternalError);
   411 	__SQLASSERT(err != SQLITE_OK ? !(*aStmtHandle) : ETrue, ESqlPanicInternalError);
   412 	//(*aStmtHandle) is NULL for ";" statements, when err == SQLITE_OK. Since the server should not panic
   412 	//(*aStmtHandle) is NULL for ";" statements, when err == SQLITE_OK. Since the server should not panic
   413 	//that situation is handled later (not inside the assert above)
   413 	//that situation is handled later (not inside the assert above)
   414 	return err;
   414 	return err;
   415 	}
   415 	}
   416 
   416 
   417 //This function accepts as arguments the SQLITE error, the length of non-parsed part of the SQL statement
   417 //This function accepts as arguments the SQLITE error code, 
       
   418 //"aHasTail" boolean flag set to true if the SQL string contains more than one SQL statement 
   418 //and the statement handle.
   419 //and the statement handle.
   419 //
   420 //
   420 //It checks the arguments and returns an error if:
   421 //It checks the arguments and returns an error if:
   421 // - aSqliteError != SQLITE_OK;
   422 // - aSqliteError != SQLITE_OK;
   422 // - aHasTail is true (possibly more than one SQL statement, separated with ";");
   423 // - aHasTail is true (possibly more than one SQL statement, separated with ";");
   434 		return KErrArgument;
   435 		return KErrArgument;
   435 		}
   436 		}
   436 	return KErrNone;
   437 	return KErrNone;
   437 	}
   438 	}
   438 
   439 
   439 //This function accepts as arguments the SQLITE error, the length of non-parsed part of the SQL statement
   440 //This function accepts as arguments the SQLITE error code, 
       
   441 //"aHasTail" boolean flag set to true if the SQL string contains more than one SQL statement 
   440 //and the statement handle.
   442 //and the statement handle.
   441 //
   443 //
   442 //It checks the arguments and leaves if:
   444 //It checks the arguments and leaves if:
   443 // - aSqliteError != SQLITE_OK;
   445 // - aSqliteError != SQLITE_OK;
   444 // - aHasTail is true (possibly more than one SQL statement, separated with ";");
   446 // - aHasTail is true (possibly more than one SQL statement, separated with ";");
   954 	//statement using sqlite3_step() and counts the steps, because each step compacts one page.
   956 	//statement using sqlite3_step() and counts the steps, because each step compacts one page.
   955 	(void)sqlite3SymbianLastOsError();//clear last OS error
   957 	(void)sqlite3SymbianLastOsError();//clear last OS error
   956 	sqlite3_stmt* stmtHandle = NULL;
   958 	sqlite3_stmt* stmtHandle = NULL;
   957 	const char* stmtTail = NULL;
   959 	const char* stmtTail = NULL;
   958 	aProcessedPageCount = 0;
   960 	aProcessedPageCount = 0;
   959 	//sqlite3_prepare16() expects parameter #3 to be one of the following:
   961 	//sqlite3_prepare_v2() expects parameter #3 to be one of the following:
   960 	// - byte length of the sql statement, excluding terminating zero;
   962 	// - byte length of the sql statement (parameter #2), excluding terminating zero;
   961 	// - negative value - zero-terminated sql statement;
   963 	// - negative value - the sql statement (parameter #2) is zero-terminated;
   962 	TInt err = sqlite3_prepare_v2(aDbHandle, (const char*)sql.Ptr(), sql.Length() - sizeof(TUint8), &stmtHandle, &stmtTail);
   964 	TInt err = sqlite3_prepare_v2(aDbHandle, (const char*)sql.Ptr(), sql.Length() - sizeof(TUint8), &stmtHandle, &stmtTail);
   963 	__SQLASSERT(err == SQLITE_OK ? !stmtTail || User::StringLength((const TUint8*)stmtTail) == 0 : !stmtHandle, ESqlPanicInternalError);
   965 	__SQLASSERT(err == SQLITE_OK ? !stmtTail || User::StringLength((const TUint8*)stmtTail) == 0 : !stmtHandle, ESqlPanicInternalError);
   964 	if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
   966 	if(stmtHandle)	//stmtHandle can be NULL for statements like this: ";".
   965 		{
   967 		{
   966 		if(err == SQLITE_OK)
   968 		if(err == SQLITE_OK)