/**********************************************************************
**
** Filename : OLEDatabase.h
**
** Contents : Include file for OLEDatabase.cpp
**
**********************************************************************/
#pragma once

#include "OLEDBException.h"
#include "DynAccessor.h"

class COLEDatabase
{
public:
    COLEDatabase();
    ~COLEDatabase();

    CString FormatDateTime( COleDateTime &ts ) ;
    const TCHAR *UpperCaseFunction();
   
    // Attributes
    BOOL IsOpen( VOID_ ) const { return m_bOpen ; }
    BOOL IsSQL( VOID_ ) const { return m_bSQL ; }
    BOOL IsReadOnly( VOID_ ) const { return m_bReadOnly ; }
    BOOL IsExclusive( VOID_ ) const { return m_bExclusive ; }

    // Operations
    virtual void Open(LPCTSTR lpszName, BOOL bExclusive = FALSE, BOOL bReadOnly = FALSE, LPCTSTR pszPassword = NULL );
    virtual void Close();

    LONG Execute(LPCTSTR lpszSQL, int nOptions = 0);
    BOOL RepeatExecute(LPCTSTR lpszSQL, CString &csError, long lBackoff, long lMin = 1, long lMax = 1, int nOptions = 0 );

    CSession& GetDataSession( VOID_ ) { return m_Session; }
    const CString& GetServer() const
    { return m_csServerName; }
    const CString& GetDatabase() const
    { return m_csDatabase; }

    CString GetFullPath() ;

protected:
    CString        m_csServerName ;
    CString        m_csDatabase ;
    BOOL        m_bSQL ;
    CSession    m_Session ;
    CDataSource    m_DB ;
    BOOL        m_bOpen ;

    VOID_        OpenSQLConnection( BOOL bExclusive, BOOL bReadOnly ) ;
    VOID_        CloseConnection( VOID_ ) ;

private:
    BOOL        m_bExclusive ;
    BOOL_        m_bReadOnly ;

    VOID_        OpenJETConnection( BOOL bExclusive, BOOL bReadOnly, LPCTSTR lpszConnect ) ;

    //
    // A general SQL command that returns no rows
    //


    class CGeneralSQLCommand : public CCommand<CDynAccessor>
    {
    public:
        void Open(CSession& Session, const _TCHAR* szCommand, LONG *plRowsAffected )
        {
            // Open but do NOT bind

            CDBPropSet     propset(DBPROPSET_ROWSET);
            propset.AddProperty(DBPROP_LOCKMODE, 2L );


            HRESULT hr = CCommand<CDynAccessor>::Open(Session, szCommand, &propset,
                                                        plRowsAffected, DBGUID_DBSQL, false);

            if (FAILED(hr))
                throw (new COLEDBException(Session, hr));
        }
    };

    class CNoRecordSetSQLCommand : public CCommand<CDynAccessor>
    {
    public:
        HRESULT Execute(CSession& Session, const _TCHAR* szCommand, LONG *plRowsAffected )
        {
            // Open but do NOT bind

            CDBPropSet     propset(DBPROPSET_DBINIT);

            return CCommand<CDynAccessor>::Open(Session, szCommand, &propset,
                                                plRowsAffected, DBGUID_DBSQL, false);
        }
    };
};