/****************************************************************************/
/*!

  @file        SQLMan_SessionIterator.cpp
  @author      MartinKi

  @ingroup
  @brief       Implementation of class SQLMan_SessionIterator.

  @see

*/
/*-------------------------------------------------------------------------

  copyright:    Copyright (c) 2005 SAP AG


    ========== licence begin  GPL
    Copyright (c) 2002-2005 SAP AG

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end




*****************************************************************************/

/*===========================================================================*
 *  INCLUDES                                                                 *
 *===========================================================================*/

#include "SQLManager/SQLMan_SessionIterator.hpp"
#include "SQLManager/SQLMan_SessionInfo.hpp"
#include "SQLManager/SQLMan_SessionList.hpp"
#include "SQLManager/SQLMan_ContextSessionMembers.hpp"
#include "SQLManager/SQLMan_Context.hpp"
#include "SQLManager/SQLMan_Types.hpp"

#include "hak10.h"

/*===========================================================================*
 *  DEFINES                                                                  *
 *===========================================================================*/

/*===========================================================================*
 *  MACROS                                                                   *
 *===========================================================================*/

/*===========================================================================*
 *  LOCAL CLASSES, STRUCTURES, TYPES, UNIONS ...                             *
 *===========================================================================*/

/*===========================================================================*
 *  STATIC/INLINE FUNCTIONS  (PROTOTYPES)                                     *
 *===========================================================================*/

/*===========================================================================*
 *  METHODS                                                                  *
 *===========================================================================*/

SQLMan_SessionIterator::SQLMan_SessionIterator()
    : m_pCurContext(0)
    , m_sessionIt(SQLMan_SessionList::Instance())
    , m_isValid(false)
{
}

/**************************************************************************/

SQLMan_SessionIterator::~SQLMan_SessionIterator()
{
}

/**************************************************************************/

SAPDB_Bool SQLMan_SessionIterator::Next()
{
    m_isValid = m_sessionIt.Next( m_pCurContext );
    return m_isValid;
}

/**************************************************************************/

void SQLMan_SessionIterator::Detach()
{
    while ( m_isValid ) {
        m_isValid = m_sessionIt.Next( m_pCurContext );
    }
}

/**************************************************************************/

bool SQLMan_SessionIterator::Unlock()
{
    // TODO: unlock is currently not supported
    return false;
}

/**************************************************************************/

SAPDB_Int4 SQLMan_SessionIterator::GetStartDate() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
        return 0;

    return m_pCurContext->GetSessionInfo().GetStartDate();
}

/**************************************************************************/

SAPDB_Int4 SQLMan_SessionIterator::GetStartTime() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
        return 0;

    return m_pCurContext->GetSessionInfo().GetStartTime();;
}

/**************************************************************************/

SAPDB_Bool
SQLMan_SessionIterator::GetSessionId(tgg91_SessionNo &sessionId) const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return false;
    }

    m_pCurContext->GetSessionInfo().GetSessionId( sessionId );

    return true;
}

/**************************************************************************/

SAPDB_Int4
SQLMan_SessionIterator::GetCatalogCacheSize() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return -1;
    }

    SAPDBERR_ASSERT_STATE( m_pCurContext->a_size_ptr != 0 );
    return m_pCurContext->a_size_ptr->csz_ak_cache;
}

/**************************************************************************/

RTE_TaskId SQLMan_SessionIterator::GetTaskId() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return 0;
    }

    return m_pCurContext->GetSessionInfo().GetTaskId();
}

/**************************************************************************/

SAPDB_Bool SQLMan_SessionIterator::GetApplicationInfo(
    tsp00_NodeId& applNode,
    SAPDB_Int4&   applPid) const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return false;
    }

    return
        m_pCurContext->GetSessionInfo().GetApplicationInfo(
            applNode,
            applPid);
}

/**************************************************************************/

SAPDB_Bool SQLMan_SessionIterator::GetCatalogCacheStatistics(
    SAPDB_Int& accessCount,
    SAPDB_Int& successful,
    SAPDB_Int& hitRate) const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return false;
    }

    const pasbool pNoReset = false;
    a10GetStatistics( *m_pCurContext, pNoReset, accessCount, successful, hitRate );

    return true;
}

/**************************************************************************/

SAPDB_Bool SQLMan_SessionIterator::IsSameSession(SQLMan_Context& context)
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return false;
    }

    return &context == m_pCurContext;
}

/**************************************************************************/

tak_appl_type SQLMan_SessionIterator::GetApplicationType() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return SQLMan_ApplType::fromConst( at_unknown );
    }

    return m_pCurContext->GetSessionInfo().GetApplicationType();
}

/**************************************************************************/

SAPDB_Bool SQLMan_SessionIterator::GetSessionUserName(SQLMan_Identifier& userName) const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return false;
    }

    m_pCurContext->GetSessionInfo().GetUserName( userName );
    return true;
}

/**************************************************************************/

SAPDB_Int4 SQLMan_SessionIterator::GetParseFileSize() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return -1;
    }

    return m_pCurContext->a_pars_curr_size;
}

/**************************************************************************/

SAPDB_UInt SQLMan_SessionIterator::GetIsolationLevel() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return 0;
    }

    return m_pCurContext->GetSessionInfo().GetIsolationLevel();
}

/**************************************************************************/

SAPDB_Int2 SQLMan_SessionIterator::GetUserTimeout() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return 0;
    }

    return m_pCurContext->GetSessionInfo().GetUserTimeout();
}
/**************************************************************************/

SQLMan_SqlMode SQLMan_SessionIterator::GetSqlMode() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return SQLMan_SqlMode::fromConst( sqlm_internal );
    }

    return m_pCurContext->GetSessionInfo().GetSqlMode();
}

/**************************************************************************/

SAPDB_Bool SQLMan_SessionIterator::GetNlsParams(
    tak_nls_param_rec& nlsParams) const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return 0;
    }

    m_pCurContext->GetSessionInfo().GetNlsParams( nlsParams );
    return true;
}

/**************************************************************************/

SAPDB_Int4 SQLMan_SessionIterator::GetTempPageUsage() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return 0;
    }

    return m_pCurContext->GetSessionInfo().GetTempPageUsage();
}

/**************************************************************************/

SQLMan_DateTimeFormat SQLMan_SessionIterator::GetDateTimeFormat() const
{
    SAPDBERR_ASSERT_STATE( m_isValid );
    if ( ! m_isValid )
    {
        return SQLMan_DateTimeFormat::fromConst( dtf_none );
    }

    return m_pCurContext->GetSessionInfo().GetDateTimeFormat();
}

/**************************************************************************/
