// Мирошниченко Кирилл aka spock
// #163860836
// e-mail: spock@km.ru
//
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __oledb_h__
#include <oledb.h>
#endif // __oledb_h__

#include "..\ODBC\MetaNameParser.h"
#include "..\SQL\MetaDataWork.h"
#include "..\GUID.h"
#include "sp_oledb.h"

#define NUMROWS_CHUNK 50
#define BLOCK_SIZE 1024

//_____________________________________________________________________________
//
struct COLEDBFldsInf
{
public:
	COLEDBFldsInf();

	ULONG	 m_Index;
	CString	 m_strName; 
	CType	 m_nType1C;
	DBTYPE	 m_wType;
	bool	 m_bIsType1C;
	bool	 m_bIsLong;
};

//_____________________________________________________________________________
//
class COLEDBCommand : public CBLContext
{
public:
	// конструктор по-умолчанию не используем!
	// только с параметром, в конструкторе член (m_bCreated) инициализируется
	//
	COLEDBCommand(CComPtr<IDBCreateCommand> pIDBCreateCommand);
	virtual ~COLEDBCommand();

	virtual int	 CallAsFunc(int, class CValue&, class CValue**);
	virtual int	 CallAsProc(int, class CValue**);
	virtual int	 FindMethod(char const *) const;
	virtual char const * GetMethodName(int, int) const;
	virtual int	 GetNMethods(void) const;
	virtual int	 GetNParams(int) const;
	virtual int	 GetParamDefValue(int, int, class CValue*) const;
	virtual int	 HasRetVal(int) const;
	virtual char const * GetTypeString(void) const;

	virtual int	 FindProp(char const *) const;
	virtual char const * GetCode(void) const;
	virtual int	 GetDestroyUnRefd(void) const;
	virtual class CObjID GetID(void) const;
	virtual int	 GetNProps(void) const;
	virtual char const * GetPropName(int, int) const;
	virtual int	 GetPropVal(int, class CValue&) const;
	virtual long GetTypeID(void) const;
	virtual class CType GetValueType(void) const;
	virtual int	 IsExactValue(void) const;
	virtual int	 IsOleContext(void) const;
	virtual int	 IsPropReadable(int) const;
	virtual int	 IsPropWritable(int) const;
	virtual int	 IsSerializable(void);
	virtual int	 SaveToString(class CString&);
	virtual int	 SetPropVal(int, class CValue const &);
//trad
	void SetIBProp(CMetaDataCont *pMDC, CIBSet *pIBSet) {m_MetaNameParser.SetIBProp(pMDC, pIBSet);};
	CBLPtr<COLEDBData> m_pOLEDBData;
//
	static class CObjID ObjID;

private:
	void GetErrorDescription(CString& strError) const;
	HRESULT Binding(CComPtr<IRowset>& pIRowset, ULONG* pnCols, DBBINDING** ppDBBindings, COLEDBFldsInf** ppColumnsInfo1C, DBOBJECT** ppDBObject, ULONG* pcMaxRowSize, bool& HasKindField) const;
	CType GetType1C(COLEDBFldsInf* fInfo) const;
	HRESULT ParseQuery(CString& strQuery);
	HRESULT Execute(const CString& strSQL, long* pnRowsAffected, IUnknown** pIRowset, bool quiet=false) const;
	HRESULT CreateAccessor(CComPtr<IAccessor>& pIAccessor, CComPtr<IRowset>& pIRowset, HACCESSOR* hAccessor, ULONG& cCols, DBBINDING* pDBBindings) const;
	HRESULT PutObjectsList(CValue** ppValue);
	HRESULT PutAggregateObjects(CValue* pValue, const CString* pszTableName, const CString* RefName);
	void RemoveTmpTable(CString& strTmpTblName);
	void RemoveAllTmpTables(void);
	ULONG FindKindCol(const COLEDBFldsInf* pColumnsInfo, const ULONG& cMaxCols, const CString& strColName) const;

	static CParamDefs defFnNames;
	
	CMetaNameParser m_MetaNameParser;
	CComPtr<ICommandText> m_pICommandText;
	bool	 m_bCreated;
	bool	 m_IsDebugMode;
	GUID	 m_GUID;
	CString	 m_szTempTablesDir;
	long	 m_ExecTimeout;

	CList<CString, CString&> m_TmpTblsList;
};