⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqldb2.h

📁 以OLE DB风格访问DB2数据库的C++类源码
💻 H
字号:
//////////////////////////////////////////////////////////////////////
//
// DB2 Access Object Version 1.0
//
// Developer: Jeff Lee
// Jan 10, 2003
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SQLDB2_H__2D7B3202_7F78_419E_9C60_59D0B985F8C9__INCLUDED_)
#define AFX_SQLDB2_H__2D7B3202_7F78_419E_9C60_59D0B985F8C9__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <string>
#include <list>
#include <set>

#include "SqlData.h"
#include "SqlTemp.h"

// Enable this compile switch because SQL_ATTR_ROW_NUMBER doesn't work correctly
#define TRACE_ROW_NUMBER

//////////////////////////////////////////////////////////////////////
// class CSqlErrorInfo - Represents a DB2 diagnostic record info

class CSqlErrorInfo
{
public:
	CSqlErrorInfo();
	CSqlErrorInfo(const CSqlErrorInfo& rEI);
	~CSqlErrorInfo() {}

public:
	enum ErrorType { sqlInfo, sqlError };
	short m_nErrorType;			// error type: information or error
	std::string m_strDesc;		// description of the error
	SQLCHAR m_szSqlState[SQL_SQLSTATE_SIZE+1];	// 5-character SQLSTATE code
	SQLINTEGER m_nCode;			// native error code

	const char* GetErrorText() const { return m_strDesc.data(); }
	CSqlErrorInfo& operator=(const CSqlErrorInfo& rEI);
};

//////////////////////////////////////////////////////////////////////
// class CSqlObject - base class to wrap DB2 CLI handles

class CSqlDatabase;
class CSqlObject
{
public:
	CSqlObject();
	virtual ~CSqlObject();

protected:
	SQLSMALLINT m_nHandleType;	// handle type: SQL_HANDLE_DBC or SQL_HANDLE_STMT
	SQLHANDLE m_hSql;			// SQL handle
	int m_nRefs;				// reference count of object
	CSqlDatabase* m_pDB;		// the relative database connection object

	std::list<CSqlErrorInfo*> m_listErrors;	// all the errors occured on this object

protected:
	void GetErrorInfo();
	virtual void OnSqlError(CSqlErrorInfo* pErrorInfo);

public:
	// Attributes
	SQLHANDLE GetHandle() const;
	BOOL IsOpen() const;
	CSqlDatabase* GetDatabase() const;

	int AddRef() { return ++m_nRefs; }
	int Release();

	// Operations
	void AttachDatabase(CSqlDatabase* pDB);
	void DetachDatabase();
	virtual void Close();

	BOOL SqlCheck(SQLRETURN nSqlRet);
	BOOL SetAttribute(SQLINTEGER nAttr, SQLPOINTER pValue,
		SQLINTEGER nValueSize=0);
	BOOL GetAttribute(SQLINTEGER nAttr, SQLPOINTER pValue,
		SQLINTEGER nBuffSize=0, SQLINTEGER* pnValueSize=NULL);

	// Error handling:
	BOOL GetLastError(CSqlErrorInfo& rErrorInfo);
};

inline SQLHANDLE CSqlObject::GetHandle() const
	{ return m_hSql; }

inline BOOL CSqlObject::IsOpen() const
	{ return m_hSql != SQL_NULL_HANDLE; }

inline CSqlDatabase* CSqlObject::GetDatabase() const
	{ return m_pDB; }

//////////////////////////////////////////////////////////////////////
// class CSqlDatabase - Represents a database connection

class CSqlDatabase : public CSqlObject
{
	friend class CSqlObject;
public:
	CSqlDatabase();
	virtual ~CSqlDatabase();

protected:
	static SQLHANDLE m_henv;	// global SQL environment handle

	DWORD m_dwOption;			// attributes of the connection
	// all the command objects attached to this connection
	std::set<CSqlObject*, less<CSqlObject*> > m_listObjects;

public:
	enum DatabaseOption
	{
		defaultOption = 0,		// use default option
		readOnly = 0x0001,		// the connection is read only
		manualCommit = 0x0002,	// manual-commit transaction
		autoUnlock = 0x0004		// automatically release read-lock when cursor is closed
	};

public:
	// Environment operations:
	static BOOL Initialize(BOOL bMultiThread=TRUE);
	static void Uninitialize();
	static SQLHANDLE GetEnvHandle() { return m_henv; }
	static BOOL SetEnvAttr(SQLINTEGER nAttr, SQLINTEGER nValue);
	static BOOL GetEnvAttr(SQLINTEGER nAttr, SQLINTEGER& nValue);

	// Operations:
	BOOL Connect(PCSTR pszDB, PCSTR pszUser, PCSTR pszPwd,
		DWORD dwOption=defaultOption,
		DWORD dwTxnIsolation=SQL_TXN_READ_COMMITTED);
	virtual void Close();

	// Transaction control:
	BOOL BeginTrans();
	BOOL CommitTrans();
	BOOL RollbackTrans();
};

//////////////////////////////////////////////////////////////////////
// class CSqlCommand - Defines a specific command (SQL statement)

class CSqlCommand : public CSqlObject
{
public:
	CSqlCommand();
	virtual ~CSqlCommand();

protected:
	DWORD m_dwOption;			// options of the stmt handle
	std::string m_strSQL;		// the SQL statement to be executed

	// all bound parameters:
	CVaryArray<CSqlParameter> m_listParams;

protected:
	virtual BOOL BindParameters();

public:
	enum CommandOption
	{
		defaultOption = 0,
		execDirect = 0x0001,	// execute command directly without preparation
		autoCloseCursor = 0x0002,	// auto-close cursor while a new cursor is opened
		nonScanEscape = 0x0004,	// SQL statements are not scanned for vendor escape clauses.
		preFetch = 0x0008		// server pre-fetch next block of data
	};

public:
	// Operations:
	BOOL Create(CSqlDatabase* pDB, DWORD dwOption=defaultOption);
	virtual void Close();
	virtual void Reset();

	// Execute SQL:
	BOOL SetCommand(PCSTR lpszSQL);
	BOOL Execute();
	SQLINTEGER GetRowCount();

	// Parameter collection:
	typedef CVaryArray<CSqlParameter> CSqlParameters;
	CSqlParameters& Parameters() { return m_listParams; }
};

//////////////////////////////////////////////////////////////////////
// class CSqlLocator - Represents a locator to manipulate BLOB data

class CSqlLocator
{
public:
	CSqlLocator();
	~CSqlLocator() { Close(); }

protected:
	SQLSMALLINT m_nType;		// SQL data type of the locator
	SQLINTEGER m_nLocator;		// the identifier of the locator
	CSqlCommand m_cmd;			// command object to run the locator

public:
	BOOL Open(CSqlDatabase* pDB, const CSqlVariant& rVariant);
	void Close() { m_cmd.Close(); }	// or Free()

	SQLINTEGER GetLength();
	SQLINTEGER GetSubString(SQLPOINTER pBuff, SQLINTEGER nStartPos, SQLINTEGER nLength);
	BOOL Free();
};

//////////////////////////////////////////////////////////////////////
// class CSqlRecordset - Represents a set of records selected from
// table(s) or the results of an executed command

class CSqlFieldCache;			// defined in Sqldb2.cpp
class CSqlRecordset : public CSqlCommand
{
public:
	CSqlRecordset();
	virtual ~CSqlRecordset();

protected:
	// Attributes:
	int m_nCmdType;				// command type. see enum CommandType
	BOOL m_bUseBookmarks;		// specifies whether use bookmark on this recordset
	int m_nCursorType;			// cursor type: forward-only, static, or keyset
	int m_nMaxRows;				// the maximum number of rows to be returned for a query
	int m_nCacheSize;			// number of rows to cache

	// Runtime status:
	BOOL m_bBOF;				// record position is before the first record
	BOOL m_bEOF;				// record position is after the last record

	// Use these variables to keep track of the current row number
	// because SQL_ATTR_ROW_NUMBER doesn't work correctly.
#ifdef TRACE_ROW_NUMBER
	int m_nRowNumber;			// the row number of the first row in current rowset
	int m_nRowCount;			// count of rows of the entire recordset
#endif

	// Rowset caching:
	int m_nRowIndex;			// the ordinal number of the current row in rowset (cache)
	int m_nRowsetSize;			// the count of rows in rowset (cache)
	SQLUSMALLINT *m_pRowStatus;	// array to store status of each row in rowset
	CSqlFieldCache* m_pRowset;	// rowset cache buffer (array)

	// all bound fields:
	CVaryArray<CSqlField> m_listFields;

protected:
	virtual BOOL BindParameters();
	BOOL BuildSQL(PCSTR pszCommand);
	void InitResultset();
	BOOL BindColumns();
	void UnbindColumns();
	BOOL ExecuteSetPos(SQLUSMALLINT nOperation, SQLUSMALLINT nLockType);

	void ResetCache();
	BOOL CacheRowset() const;
	BOOL TestCache(SQLINTEGER nRowIndex) const;
	void ReadCache();
	BOOL WriteCache();
	BOOL FetchRowset(SQLSMALLINT nOrientation, SQLINTEGER nRowPos);
	BOOL FetchCache(SQLINTEGER nRowIndex);

public:
	std::string m_strFilter;	// WHERE clause of the SELECT statement
	std::string m_strSort;		// GROUP BY clause of the SELECT statement

	enum CommandType			// command type enumeration
	{
		sqlCmdSQL,				// evaluates command as a SQL statement
		sqlCmdTable,			// evaluates command as a table name
		sqlCmdStoreProc			// evaluates command as a store procedure name
	};

public:
	// Initialization:
	BOOL Open(PCSTR pszCommand, int nCmdType,
		DWORD nCursorType=SQL_CURSOR_STATIC,
		BOOL bUseBookmarks=FALSE);
	BOOL Requery();
	BOOL NextRecordset();
	virtual void Close();
	virtual void Reset();
	BOOL AutoBindProcParams(PCSTR pszProcName,
		PCSTR pszSchemaName=NULL);

	// Attributes:
	BOOL IsScrollable() const;
	BOOL IsUpdatable() const;
	BOOL SetMaxRows(DWORD nMaxRows=0);
	void SetCacheSize(int nCacheSize=0);

	int GetCacheSize() const;
	int GetFields() const;
	int GetRetStatus() const;
	SQLUSMALLINT GetRowStatus() const;
	BOOL IsDeleted() const;
	BOOL IsBOF() const;
	BOOL IsEOF() const;

	// Operations:
	BOOL Move(SQLINTEGER nOffset, BOOL bRefresh=FALSE);
	BOOL MoveFirst();
	BOOL MoveLast();
	BOOL MovePrevious();
	BOOL MoveNext();
	BOOL Refresh();

#ifndef TRACE_ROW_NUMBER
	typedef SQLUINTEGER SqlBookmark;
#else
	struct SqlBookmark
	{
		SQLUINTEGER nBookmark;
		SQLINTEGER nRowNumber;	// corresponding row number
	};
#endif
	BOOL GetBookmark(SqlBookmark& rBookmark);
	BOOL SetBookmark(const SqlBookmark& rBookmark);

	BOOL Update();
	BOOL Delete();

	// Field collection:
	typedef CVaryArray<CSqlField> CSqlFields;
	CSqlFields& Fields() { return m_listFields; }
};

inline void CSqlRecordset::ResetCache()
	{ m_nRowIndex = m_nRowsetSize = 0; }
inline BOOL CSqlRecordset::CacheRowset() const
	{ return m_pRowset != NULL; }
inline BOOL CSqlRecordset::TestCache(SQLINTEGER nRowIndex) const
	{ return (nRowIndex >= 0 && nRowIndex < m_nRowsetSize); }

inline BOOL CSqlRecordset::IsScrollable() const
	{ return m_nCursorType != SQL_CURSOR_FORWARD_ONLY; }
inline BOOL CSqlRecordset::IsUpdatable() const
	{ return m_nCursorType == SQL_CURSOR_KEYSET_DRIVEN; }

inline int CSqlRecordset::GetCacheSize() const
	{ return m_nCacheSize; }
inline int CSqlRecordset::GetFields() const
	{ return (int)m_listFields.GetSize(); }
inline SQLUSMALLINT CSqlRecordset::GetRowStatus() const
	{ return m_pRowStatus != NULL ? m_pRowStatus[m_nRowIndex] : SQL_ROW_NOROW; }
inline BOOL CSqlRecordset::IsDeleted() const
	{ return GetRowStatus() == SQL_ROW_DELETED; }
inline BOOL CSqlRecordset::IsBOF() const
	{ return m_bBOF; }
inline BOOL CSqlRecordset::IsEOF() const
	{ return m_bEOF; }

#endif // !defined(AFX_SQLDB2_H__2D7B3202_7F78_419E_9C60_59D0B985F8C9__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -