persistentstorage/sql/SRC/Server/SqlSrvStatementUtil.cpp
changeset 22 a7ba600cb39d
parent 17 55f2396f6d25
child 31 ba1c4f4a893f
equal deleted inserted replaced
19:d6ef85bc5971 22:a7ba600cb39d
   201 //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.
   202 //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.
   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 	__SQLASSERT(aSql.Length() > 0 ? (TInt)aSql[aSql.Length() - 1] == 0 : ETrue, ESqlPanicBadArgument);
   206 	sqlite3_stmt* stmtHandle = NULL;
   207 	sqlite3_stmt* stmtHandle = NULL;
   207 	const void* stmtTail = NULL;
   208 	const void* stmtTail = NULL;
   208     //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:
   209     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   210     // - byte length of the sql statement (parameter #2), excluding terminating zero;
   210     // - negative value - the sql statement (parameter #2) is zero-terminated;
   211     // - negative value - the sql statement (parameter #2) is zero-terminated;
   221 				{
   222 				{
   222 				err = sqlite3_reset(stmtHandle);
   223 				err = sqlite3_reset(stmtHandle);
   223 				__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   224 				__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   224 				}
   225 				}
   225 			}
   226 			}
   226 		TInt err2 = sqlite3_finalize(stmtHandle);
   227 		(void)sqlite3_finalize(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   227 		if(err == SQLITE_DONE && err2 != SQLITE_OK)
       
   228 			{//return the "sqlite3_finalize" error
       
   229 			err = err2;	
       
   230 			}
       
   231 		}
   228 		}
   232 	return err;
   229 	return err;
   233 	}
   230 	}
   234 
   231 
   235 /**
   232 /**
   421 //It checks the arguments and returns an error if:
   418 //It checks the arguments and returns an error if:
   422 // - aSqliteError != SQLITE_OK;
   419 // - aSqliteError != SQLITE_OK;
   423 // - aHasTail is true (possibly more than one SQL statement, separated with ";");
   420 // - aHasTail is true (possibly more than one SQL statement, separated with ";");
   424 // - aStmtHandle is NULL;
   421 // - aStmtHandle is NULL;
   425 //
   422 //
   426 static TInt ProcessPrepareError(TInt aSqliteError, TBool aHasTail, sqlite3_stmt* aStmtHandle)
   423 static TInt ProcessPrepareError(TInt aSqliteError, TBool aHasTail, sqlite3_stmt*& aStmtHandle)
   427 	{
   424 	{
   428 	if(aSqliteError != SQLITE_OK)
   425 	if(aSqliteError != SQLITE_OK)
   429 		{
   426 		{
   430 		return ::Sql2OsErrCode(aSqliteError, sqlite3SymbianLastOsError());
   427 		return ::Sql2OsErrCode(aSqliteError, sqlite3SymbianLastOsError());
   431 		}
   428 		}
   432 	else if(aHasTail || !aStmtHandle)
   429 	else if(aHasTail || !aStmtHandle)
   433 		{//More than one SQL statement or the SQL string is "" or ";;;" or ";   ;; ;". 
   430 		{//Case 1:
   434 		 //Report it as an error, because there is no statement handle.
   431 		 // More than one SQL statement or the SQL string is "" or ";;;" or ";   ;; ;". 
       
   432 		 // Report it as an error, because there is no statement handle.
       
   433 		 //Case 2:
       
   434 		 // Non-null aHasTail. In this case the SQL string contains more than one SQL statement.
       
   435 		 // The statement handle is not null. The statement has to be finialized before reporting the error.
       
   436 		(void)FinalizeStmtHandle(aStmtHandle);
       
   437 		aStmtHandle = NULL;
   435 		return KErrArgument;
   438 		return KErrArgument;
   436 		}
   439 		}
   437 	return KErrNone;
   440 	return KErrNone;
   438 	}
   441 	}
   439 
   442 
   542 TInt StmtExec(sqlite3_stmt* aStmtHandle)
   545 TInt StmtExec(sqlite3_stmt* aStmtHandle)
   543 	{
   546 	{
   544 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   547 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   545 	
   548 	
   546 	(void)sqlite3SymbianLastOsError();//clear last OS error
   549 	(void)sqlite3SymbianLastOsError();//clear last OS error
   547 	
   550 
   548 	if(sqlite3_expired(aStmtHandle))
       
   549 		{
       
   550 		return KSqlErrStmtExpired;
       
   551 		}
       
   552 		
       
   553 	TInt err;
   551 	TInt err;
   554 	while((err = sqlite3_step(aStmtHandle)) == SQLITE_ROW)
   552 	while((err = sqlite3_step(aStmtHandle)) == SQLITE_ROW)
   555 		{
   553 		{
   556 		}
   554 		}
   557 		
   555 		
   592 	{
   590 	{
   593 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   591 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   594 	
   592 	
   595 	(void)sqlite3SymbianLastOsError();//clear last OS error
   593 	(void)sqlite3SymbianLastOsError();//clear last OS error
   596 	
   594 	
   597 	if(sqlite3_expired(aStmtHandle))
       
   598 		{
       
   599 		return KSqlErrStmtExpired;
       
   600 		}
       
   601 		
       
   602 	TInt err = sqlite3_step(aStmtHandle);
   595 	TInt err = sqlite3_step(aStmtHandle);
   603 	if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   596 	if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   604 		{
   597 		{
   605 		err = sqlite3_reset(aStmtHandle);
   598 		err = sqlite3_reset(aStmtHandle);
   606 		__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   599 		__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   626 	{
   619 	{
   627 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   620 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   628 	
   621 	
   629 	(void)sqlite3SymbianLastOsError();//clear last OS error
   622 	(void)sqlite3SymbianLastOsError();//clear last OS error
   630 	
   623 	
   631 	if(sqlite3_expired(aStmtHandle))
       
   632 		{
       
   633 		return KSqlErrStmtExpired;
       
   634 		}
       
   635 		
       
   636 	TInt err = sqlite3_reset(aStmtHandle);
   624 	TInt err = sqlite3_reset(aStmtHandle);
   637 	return ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   625 	return ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   638 	}
   626 	}
   639 
   627 
   640 /**
   628 /**
   709 	sqlite3_stmt* stmtHandle = NULL;
   697 	sqlite3_stmt* stmtHandle = NULL;
   710 	TInt err = PreRetrievePragmaValue(aDbHandle, aDbName, aPragmaSql, stmtHandle);
   698 	TInt err = PreRetrievePragmaValue(aDbHandle, aDbName, aPragmaSql, stmtHandle);
   711 	if(err == KSqlAtRow)
   699 	if(err == KSqlAtRow)
   712 		{
   700 		{
   713 		aPragmaValue = sqlite3_column_int(stmtHandle, 0);
   701 		aPragmaValue = sqlite3_column_int(stmtHandle, 0);
   714 		__SQLASSERT(aPragmaValue >= 0, ESqlPanicInternalError);
   702 		err = aPragmaValue >= 0 ? KErrNone : KErrCorrupt;
   715 		err = KErrNone;
   703 		}
   716 		}
   704 	(void)FinalizeStmtHandle(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   717 	TInt err2 = FinalizeStmtHandle(stmtHandle);
       
   718 	if(err == KErrNone && err2 != KErrNone)
       
   719 		{//FinalizeStmtHandle() has failed
       
   720 		err = err2;
       
   721 		}
       
   722 	return err;
   705 	return err;
   723 	}
   706 	}
   724 
   707 
   725 /**
   708 /**
   726 Prepares and executes PRAGMA statement and retrieves the value of column 0 (the pragma value) as text.
   709 Prepares and executes PRAGMA statement and retrieves the value of column 0 (the pragma value) as text.
   750 		{
   733 		{
   751 		TPtrC8 ptr(sqlite3_column_text(stmtHandle, 0));
   734 		TPtrC8 ptr(sqlite3_column_text(stmtHandle, 0));
   752 		aPragmaValue.Copy(ptr);
   735 		aPragmaValue.Copy(ptr);
   753 		err = KErrNone;
   736 		err = KErrNone;
   754 		}
   737 		}
   755 	TInt err2 = FinalizeStmtHandle(stmtHandle);
   738 	(void)FinalizeStmtHandle(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   756 	if(err == KErrNone && err2 != KErrNone)
       
   757 		{//::FinalizeStmtHandle() has failed
       
   758 		err = err2;
       
   759 		}
       
   760 	return err;
   739 	return err;
   761 	}
   740 	}
   762 
   741 
   763 /**
   742 /**
   764 Retrieves the database pages count.
   743 Retrieves the database pages count.
   985 				{
   964 				{
   986 				err = sqlite3_reset(stmtHandle);
   965 				err = sqlite3_reset(stmtHandle);
   987 				__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   966 				__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   988 				}
   967 				}
   989 			}
   968 			}
   990 		TInt err2 = sqlite3_finalize(stmtHandle);
   969 		(void)sqlite3_finalize(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   991 		if(err == SQLITE_DONE && err2 != SQLITE_OK)
       
   992 			{//use the "sqlite3_finalize" error
       
   993 			err = err2;				
       
   994 			}
       
   995 		}
   970 		}
   996 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   971 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   997 	if(err == KSqlAtEnd)
   972 	if(err == KSqlAtEnd)
   998 		{
   973 		{
   999 		err = KErrNone;	
   974 		err = KErrNone;	
  1002 	return err;
   977 	return err;
  1003 	}
   978 	}
  1004 
   979 
  1005 /**
   980 /**
  1006 Finalizes the statement handle.
   981 Finalizes the statement handle.
       
   982 Although the function can return an error, it is ok not to check the returned error,
       
   983 because sqlite3_finalize() fails only if invalid statement handle is passed as an argument.
       
   984 
       
   985 @return KErrNone,     Operation completed successfully;
       
   986 					  Other system-wide error codes or SQL errors of ESqlDbError type.
  1007 
   987 
  1008 @internalComponent
   988 @internalComponent
  1009 */
   989 */
  1010 TInt FinalizeStmtHandle(sqlite3_stmt* aStmtHandle)
   990 TInt FinalizeStmtHandle(sqlite3_stmt* aStmtHandle)
  1011 	{
   991 	{