persistentstorage/sql/SRC/Server/SqlSrvStatementUtil.cpp
branchRCL_3
changeset 15 fcc16690f446
parent 12 6b6fd149daa2
child 23 26645d81f48d
equal deleted inserted replaced
14:04ec7606545c 15:fcc16690f446
   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 /**
   548 TInt StmtExec(sqlite3_stmt* aStmtHandle)
   545 TInt StmtExec(sqlite3_stmt* aStmtHandle)
   549 	{
   546 	{
   550 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   547 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   551 	
   548 	
   552 	(void)sqlite3SymbianLastOsError();//clear last OS error
   549 	(void)sqlite3SymbianLastOsError();//clear last OS error
   553 	
       
   554 	if(sqlite3_expired(aStmtHandle))
       
   555 		{
       
   556 		return KSqlErrStmtExpired;
       
   557 		}
       
   558 		
   550 		
   559 	TInt err;
   551 	TInt err;
   560 	while((err = sqlite3_step(aStmtHandle)) == SQLITE_ROW)
   552 	while((err = sqlite3_step(aStmtHandle)) == SQLITE_ROW)
   561 		{
   553 		{
   562 		}
   554 		}
   598 	{
   590 	{
   599 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   591 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   600 	
   592 	
   601 	(void)sqlite3SymbianLastOsError();//clear last OS error
   593 	(void)sqlite3SymbianLastOsError();//clear last OS error
   602 	
   594 	
   603 	if(sqlite3_expired(aStmtHandle))
       
   604 		{
       
   605 		return KSqlErrStmtExpired;
       
   606 		}
       
   607 		
       
   608 	TInt err = sqlite3_step(aStmtHandle);
   595 	TInt err = sqlite3_step(aStmtHandle);
   609 	if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   596 	if(err == SQLITE_ERROR)	//It may be "out of memory" problem
   610 		{
   597 		{
   611 		err = sqlite3_reset(aStmtHandle);
   598 		err = sqlite3_reset(aStmtHandle);
   612 		__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   599 		__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   632 	{
   619 	{
   633 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   620 	__SQLASSERT(aStmtHandle != NULL, ESqlPanicInvalidObj);
   634 	
   621 	
   635 	(void)sqlite3SymbianLastOsError();//clear last OS error
   622 	(void)sqlite3SymbianLastOsError();//clear last OS error
   636 	
   623 	
   637 	if(sqlite3_expired(aStmtHandle))
       
   638 		{
       
   639 		return KSqlErrStmtExpired;
       
   640 		}
       
   641 		
       
   642 	TInt err = sqlite3_reset(aStmtHandle);
   624 	TInt err = sqlite3_reset(aStmtHandle);
   643 	return ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   625 	return ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   644 	}
   626 	}
   645 
   627 
   646 /**
   628 /**
   715 	sqlite3_stmt* stmtHandle = NULL;
   697 	sqlite3_stmt* stmtHandle = NULL;
   716 	TInt err = PreRetrievePragmaValue(aDbHandle, aDbName, aPragmaSql, stmtHandle);
   698 	TInt err = PreRetrievePragmaValue(aDbHandle, aDbName, aPragmaSql, stmtHandle);
   717 	if(err == KSqlAtRow)
   699 	if(err == KSqlAtRow)
   718 		{
   700 		{
   719 		aPragmaValue = sqlite3_column_int(stmtHandle, 0);
   701 		aPragmaValue = sqlite3_column_int(stmtHandle, 0);
   720 		__SQLASSERT(aPragmaValue >= 0, ESqlPanicInternalError);
   702 		err = aPragmaValue >= 0 ? KErrNone : KErrCorrupt;
   721 		err = KErrNone;
   703 		}
   722 		}
   704 	(void)FinalizeStmtHandle(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   723 	TInt err2 = FinalizeStmtHandle(stmtHandle);
       
   724 	if(err == KErrNone && err2 != KErrNone)
       
   725 		{//FinalizeStmtHandle() has failed
       
   726 		err = err2;
       
   727 		}
       
   728 	return err;
   705 	return err;
   729 	}
   706 	}
   730 
   707 
   731 /**
   708 /**
   732 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.
   756 		{
   733 		{
   757 		TPtrC8 ptr(sqlite3_column_text(stmtHandle, 0));
   734 		TPtrC8 ptr(sqlite3_column_text(stmtHandle, 0));
   758 		aPragmaValue.Copy(ptr);
   735 		aPragmaValue.Copy(ptr);
   759 		err = KErrNone;
   736 		err = KErrNone;
   760 		}
   737 		}
   761 	TInt err2 = FinalizeStmtHandle(stmtHandle);
   738 	(void)FinalizeStmtHandle(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   762 	if(err == KErrNone && err2 != KErrNone)
       
   763 		{//::FinalizeStmtHandle() has failed
       
   764 		err = err2;
       
   765 		}
       
   766 	return err;
   739 	return err;
   767 	}
   740 	}
   768 
   741 
   769 /**
   742 /**
   770 Retrieves the database pages count.
   743 Retrieves the database pages count.
   991 				{
   964 				{
   992 				err = sqlite3_reset(stmtHandle);
   965 				err = sqlite3_reset(stmtHandle);
   993 				__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   966 				__SQLASSERT(err != SQLITE_OK, ESqlPanicInternalError);
   994 				}
   967 				}
   995 			}
   968 			}
   996 		TInt err2 = sqlite3_finalize(stmtHandle);
   969 		(void)sqlite3_finalize(stmtHandle);//sqlite3_finalize() fails only if an invalid statement handle is passed.
   997 		if(err == SQLITE_DONE && err2 != SQLITE_OK)
       
   998 			{//use the "sqlite3_finalize" error
       
   999 			err = err2;				
       
  1000 			}
       
  1001 		}
   970 		}
  1002 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
   971 	err = ::Sql2OsErrCode(err, sqlite3SymbianLastOsError());
  1003 	if(err == KSqlAtEnd)
   972 	if(err == KSqlAtEnd)
  1004 		{
   973 		{
  1005 		err = KErrNone;	
   974 		err = KErrNone;	
  1008 	return err;
   977 	return err;
  1009 	}
   978 	}
  1010 
   979 
  1011 /**
   980 /**
  1012 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.
  1013 
   987 
  1014 @internalComponent
   988 @internalComponent
  1015 */
   989 */
  1016 TInt FinalizeStmtHandle(sqlite3_stmt* aStmtHandle)
   990 TInt FinalizeStmtHandle(sqlite3_stmt* aStmtHandle)
  1017 	{
   991 	{