//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2007  (all rights reserved)
//               MELBOURNE, VICTORIA, AUSTRALIA, 3130
//
//  Provided no fee is charged (either by the distributer or user)
//  permission to use, copy, modify, and distribute this software and
//  its documentation for any purpose and without fee is hereby granted,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//  No waranty or suitablity for purpose is implied.
//
//
//  ODBC extensions
//  NOTES: Exposes definitions for extensions to the ODBC API
//         

#include "StdAfx.h"
#include "ODBC_Ext.h"

//
//
//  Safe SQLHANDLE container
//  NOTES: SQLHANDLE is managed within the life cycle of this object
//
SafeSQLHANDLE::SafeSQLHANDLE ( )
{
    RenderThisSafe ( );
}
SafeSQLHANDLE::SafeSQLHANDLE ( SQLSMALLINT hType, SQLHANDLE hSQL )
{   
    m_hType   = hType;
    m_hHandle = hSQL;
}
SafeSQLHANDLE::~SafeSQLHANDLE ( )
{   
    if ( m_hHandle )
      SQLFreeHandle ( m_hType, m_hHandle );
}
void
SafeSQLHANDLE::RenderThisSafe ( )
{
     m_hType   = NULL;
     m_hHandle = NULL;
}
SafeSQLHANDLE::operator SQLHANDLE ( )
{
     return &m_hHandle;
}
SafeSQLHANDLE::operator SQLHANDLE* ( )
{
     return &m_hHandle;
}
SQLHANDLE
SafeSQLHANDLE::Deref ( )
{
     SQLHANDLE hHandle = m_hHandle;
     m_hHandle = NULL;
     return hHandle;
}

///////////////////////////////////////////////////////////////////////////////
//  ODBC to P2Pmsg translators

//
//  Translates retrieved SQLGetData() item into P3PmsgData() item
P2PmsgODBC_EXT BOOL
SQLGetData_2_P3PmsgData ( P3PmsgData& oData, SQLSMALLINT iDataType
                        , void *pvValue, SQLINTEGER cbValue )
{
    USES_CONVERSION;

    // Make repository
    // NOTES: Map from SQL to P2Peer data types
    if ( iDataType == SQL_CHAR )
    {
      char *pszValue = (char *)pvValue;
      while (  cbValue            >   0  &&
              pszValue[cbValue-1] == ' '    )
        cbValue--;
      if ( cbValue <= 0xFF )
        oData.Recreate ( VBLockData_BSTR08, pvValue, cbValue );
      else if ( cbValue <= 0xFFFF ) 
        oData.Recreate ( VBLockData_BSTR16, pvValue, cbValue );
      else
        oData.Recreate ( VBLockData_BSTR32, pvValue, cbValue );
    }
    else if ( iDataType == SQL_VARCHAR )
    {
      if ( cbValue <= 0xFF )
        oData.Recreate ( VBLockData_INT08, pvValue, cbValue );
      else if ( cbValue <= 0xFFFF )
        oData.Recreate ( VBLockData_INT16, pvValue, cbValue );
      else
        oData.Recreate ( VBLockData_INT32, pvValue, cbValue );
    }
    else if ( iDataType == SQL_WVARCHAR ||
              iDataType == -150            )
    {
      LPCTSTR lpszValue = W2A((SQLWCHAR*)pvValue);
      if ( cbValue <= 0xFF )
        oData.Recreate ( VBLockData_INT08, (void*)lpszValue, cbValue );
      else if ( cbValue <= 0xFFFF )
        oData.Recreate ( VBLockData_INT16, (void *)lpszValue, cbValue );
      else
        oData.Recreate ( VBLockData_INT32, (void *)lpszValue, cbValue );
    }
    else if ( iDataType == SQL_WCHAR )
    {
      char *pszValue = (char *)pvValue;
      while (  cbValue            >   0  &&
              pszValue[cbValue-1] == ' '    )
        cbValue--;
      if ( cbValue <= 0xFF )
        oData.Recreate ( VBLockData_BSTR08, pvValue, cbValue );
      else if ( cbValue <= 0xFFFF ) 
        oData.Recreate ( VBLockData_BSTR16, pvValue, cbValue );
      else
        oData.Recreate ( VBLockData_BSTR32, pvValue, cbValue );
    }
    else if ( iDataType == SQL_INTEGER )
      oData.Recreate ( VBLockData_INT32, pvValue, cbValue );
    else if ( iDataType == SQL_BIGINT )
      oData.Recreate ( VBLockData_INT64, pvValue, cbValue );
    else if ( iDataType == SQL_SMALLINT )
      oData.Recreate ( VBLockData_INT16, pvValue, cbValue );
    else if ( iDataType == SQL_FLOAT )
      oData.Recreate ( VBLockData_FLOAT, pvValue, cbValue );
    else if ( iDataType == SQL_DOUBLE )
      oData.Recreate ( VBLockData_DOUBLE, pvValue, cbValue );
    else if ( iDataType == SQL_DECIMAL )
      oData.Recreate ( VBLockData_DOUBLE, pvValue, cbValue );
    else if ( iDataType == SQL_TINYINT )
      oData.Recreate ( VBLockData_INT16, pvValue, cbValue );
    else if ( iDataType == SQL_TIME )
      oData.Recreate ( VBLockData_INT32, pvValue, cbValue );
    else if ( iDataType == SQL_TIMESTAMP )
      oData.Recreate ( VBLockData_INT32, pvValue, cbValue );
    else if ( iDataType == SQL_TYPE_TIMESTAMP )
    {
      if ( pvValue )
      {
        SQL_TIMESTAMP_STRUCT *pTIMESTAMP = (SQL_TIMESTAMP_STRUCT *)pvValue;
        COleDateTime o;
        SQLSMALLINT nYear = pTIMESTAMP->year;
        if ( nYear < 1970 )
          nYear = 1971;
        CTime oTime (            nYear, pTIMESTAMP->month,  pTIMESTAMP->day
                    , pTIMESTAMP->hour, pTIMESTAMP->minute, pTIMESTAMP->second );
        oData = P3PmsgData ( oTime.GetTime() );
        }
        else
          oData.Recreate ( VBLockData_TIME64, 0, 0 );
    }
    else if ( iDataType == SQL_VARBINARY     ||
              iDataType == SQL_LONGVARBINARY    )
    {
      if ( cbValue <= 0xFF )
        oData.Recreate ( VBLockData_BLOB08, pvValue, cbValue );
      else if ( cbValue <= 0xFFFF )
        oData.Recreate ( VBLockData_BLOB16, pvValue, cbValue );
      else
        oData.Recreate ( VBLockData_BLOB32, pvValue, cbValue );
    }
    else if ( iDataType == SQL_BIT )
      oData.Recreate ( VBLockData_BOOL, pvValue, cbValue );
    else if ( iDataType == SQL_GUID )
      oData.Recreate ( VBLockData_GUID, pvValue, cbValue );
    else
      ASSERT(0);
    return TRUE;
}
