//-------------------- CSPODBCRecordset.h ---------------------

//*************************************************************
//*****       Copyright ©2002 - 2004 Micronovae Ltd       *****
//*****                All rights reserved                *****
//*************************************************************


#ifndef _CSPODBCRECORDSET
#define _CSPODBCRECORDSET

#include "CSPConf.h"
#include "CSPLib.h"
#include "CSPODBCField.h"
#include "CSPODBCConnection.h"


#ifdef EOF
#undef EOF
#endif

#ifdef BOF
#undef BOF
#endif

class CSPODBC_CLASS_EXPORT CSPODBCRecordset : public CSPObject
{
public:
	enum { 
		CURSOR_DYNAMIC = SQL_CURSOR_DYNAMIC,
		CURSOR_STATIC = SQL_CURSOR_STATIC,
		CURSOR_KEYSET = SQL_CURSOR_KEYSET_DRIVEN,
		CURSOR_FORWARD = SQL_CURSOR_FORWARD_ONLY
		};

	enum {
		CONCUR_LOCK = SQL_CONCUR_LOCK,
		CONCUR_READ_ONLY = SQL_CONCUR_READ_ONLY,
		CONCUR_ROW_VERSION = SQL_CONCUR_ROWVER,
		CONCUR_VALUES = SQL_CONCUR_VALUES
		};
public:
	const CSPString& GetLastErrorStr();
	int GetColCount() const;
	bool IsEOF() const;
	bool IsBOF() const;
	bool EOF() const;
	bool BOF() const;
	bool GetRow( unsigned long ulIndex );
	bool LastRow();
	bool FirstRow();
	bool PrevRow();
	bool NextRow();
	bool ExecuteSQL( const CSPString& strSQL );
	bool ExecuteSQL();
	void Close();
	bool Disconnect();
	bool PrepareSQL( const CSPString& strSQL );
	bool Connect( const CSPODBCConnection& Connection );
	bool Connect( const CSPString& strDSN, const CSPString& strUser = CSPString(), const CSPString& strPass = CSPString() );
	CSPODBCRecordset( unsigned long ulRowsetSize = 1 );
	CSPODBCRecordset( const CSPODBCConnection& Conn, unsigned long ulRowsetSize = 1 );
	virtual ~CSPODBCRecordset();
	int GetRowPos() const;
	int GetRowCount() const;

	CSPODBCField& operator[]( unsigned long ulColumn ) const;
	CSPODBCField& operator()( unsigned long ulColumn ) const;
	CSPODBCField& operator()( const char* pszColumnName ) const;
	CSPODBCField& operator()( const CSPString& strColumnName ) const;
	
	bool AutoCommit( bool bAuto = true );
	bool Commit();
	bool Rollback();

	bool Update();
	bool Delete();
	bool Refresh();
	bool Insert();

	void ClearFields();

	bool Lock();
	bool Unlock();

	bool UseBookmarks( bool bUse = true );
	CSPVariant GetBookmark() const;
	bool SetBookmark( const CSPVariant& varBookmark );

	bool SetConcurrency( long lConcur );
	bool SetCursorType( long lCurType );

protected:
	int iRows;
	int iRowsFetched;
	CSPODBCConnection* pConn;
	bool bOwnConn;
	SQLHANDLE hStmt;
	SQLSMALLINT nColumns;
	int iCurRowInRowset;
	int iCurRow;
	int iRowCount;
	bool bEOF;
	bool bBOF;
	CSPODBCField* FieldList;
	mutable CSPODBCField strNullField;
	bool bAutoCommit;
	bool bScrollable;
	unsigned long ulBookmarkOffset;
	bool bUseBookmarks;
	long lCursorType;
	long lConcurrency;
	mutable CSPString strDiagCode;
	mutable int iNativeErrorCode;
	mutable CSPString strLastError;

	bool PrepareSQLInternal( const CSPString& strSQL, bool bExecDirect );
	bool BindColumns();
	bool UnbindColumns();
	SQLRETURN GetRowSet( SQLSMALLINT Type, int index );
	void SetError( int iIndex = 1 ) const;
	void SetError( const CSPString& strErrorMsg ) const;
	void ThrowError() const;
	void DestroyFields();
	bool Check() const;
	bool CheckConnected() const;
	bool CheckColumns() const;

private:
	CSPODBCRecordset( const CSPODBCRecordset& );
	CSPODBCRecordset& operator = ( const CSPODBCRecordset& );

};

typedef CSPODBCRecordset CSPODBCAccess;

#endif
