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

📄 sqlapi.h

📁 能够连接各种数据库的API
💻 H
📖 第 1 页 / 共 3 页
字号:
	const wchar_t *GetWideChars() const;
	// get string length (in Unicode characters)
	int GetWideCharsLength() const;
#endif	// !SA_UNICODE

// Implementation
public:
	~SAString();

protected:
	SAChar *m_pchData;	// pointer to ref counted string data

	// implementation helpers
	SAStringData *GetData() const;
	void Init();
	void AllocBuffer(int nLen);
#ifdef SA_UNICODE
	void AssignBinaryCopy(int nSrcLenInBytes, const void *pSrcData);
	void ConcatBinaryCopy(int nSrc1LenInBytes, const void *pSrc1Data, int nSrc2LenInBytes, const void *pSrc2Data);
	void ConcatBinaryInPlace(int nSrcLen, const void *pData);
#endif	// SA_UNICODE
	void AssignCopy(int nSrcLen, const SAChar *lpszSrcData);
	void ConcatCopy(int nSrc1Len, const SAChar *lpszSrc1Data, int nSrc2Len, const SAChar *lpszSrc2Data);
	void ConcatInPlace(int nSrcLen, const SAChar *lpszSrcData);

	void CopyBeforeWrite();
	void AllocBeforeWrite(int nLen);
	void Release();
	static void Release(SAStringData *pData);
	static int SafeStrlen(const SAChar *lpsz);
	static void FreeData(SAStringData *pData);
};

// Compare helpers
bool SQLAPI_API operator==(const SAString &s1, const SAString &s2);
bool SQLAPI_API operator==(const SAString &s1, const SAChar *s2);
bool SQLAPI_API operator==(const SAChar *s1, const SAString &s2);
bool SQLAPI_API operator!=(const SAString &s1, const SAString &s2);
bool SQLAPI_API operator!=(const SAString &s1, const SAChar *s2);
bool SQLAPI_API operator!=(const SAChar *s1, const SAString &s2);
bool SQLAPI_API operator<(const SAString &s1, const SAString &s2);
bool SQLAPI_API operator<(const SAString &s1, const SAChar *s2);
bool SQLAPI_API operator<(const SAChar *s1, const SAString &s2);
bool SQLAPI_API operator>(const SAString &s1, const SAString &s2);
bool SQLAPI_API operator>(const SAString &s1, const SAChar *s2);
bool SQLAPI_API operator>(const SAChar *s1, const SAString &s2);
bool SQLAPI_API operator<=(const SAString &s1, const SAString &s2);
bool SQLAPI_API operator<=(const SAString &s1, const SAChar *s2);
bool SQLAPI_API operator<=(const SAChar *s1, const SAString &s2);
bool SQLAPI_API operator>=(const SAString &s1, const SAString &s2);
bool SQLAPI_API operator>=(const SAString &s1, const SAChar *s2);
bool SQLAPI_API operator>=(const SAChar *s1, const SAString &s2);


class SQLAPI_API SANull
{
};

#define SA_NUMERIC_MANTISSA_SIZE 32
class SQLAPI_API SANumeric
{
	void InitZero();
	void setFromPlainString(const SAChar *s);
	void setFromExpString(const SAString &s);

public:
	SANumeric();	// default constructor, initializes to zero

	SANumeric(double d);			// initializes from double
	SANumeric &operator=(double);	// reinitializes from double
	operator double() const;		// converts to double

	SANumeric &operator=(const SAChar *s);	// reinitializes from string
	operator SAString() const;	// converts to string

public:
	unsigned char precision;	// the maximum number of digits in base 10
	unsigned char scale;		// the number of digits to the right of the decimal point
	unsigned char sign;			// the sign: 1 for positive numbers, 0 for negative numbers

	// a number stored as SA_NUMERIC_MANTISSA_SIZE-byte scaled integer, with the least-significant byte on the left
	unsigned char val[SA_NUMERIC_MANTISSA_SIZE];
};

class SQLAPI_API SADateTime
{
	static int m_saMonthDays[13];

protected:
	static bool DateFromTm(
		unsigned short wYear, unsigned short wMonth, unsigned short wDay,
		unsigned short wHour, unsigned short wMinute, unsigned short wSecond,
		unsigned long nNanoSecond,
		double &dtDest);
	static bool TmFromDate(
		double dtSrc,
		struct tm &tmDest, unsigned long &nNanoSecond);

protected:
	void Init_Tm();
	struct tm m_tm;
	unsigned long m_nFraction;	// 0..999999999

	int	nReserved;

public:
	SADateTime();
	SADateTime(const SADateTime &other);

	SADateTime(const struct tm &tmValue);
	operator struct tm &();
	operator struct tm() const;

	SADateTime(double dt);
	operator double() const;

	SADateTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec);

	int GetYear() const;		// year, f.ex., 1999, 2000
	int GetMonth() const;		// 1..12
	int GetDay() const;			// 1..31
	int GetHour() const;		// 0..23
	int GetMinute() const;		// 0..59
	int GetSecond() const;		// 0..59
	int GetDayOfWeek() const;	// 1..7, 1=Sunday, 2=Monday, and so on
	int GetDayOfYear() const;	// 1..366, where January 1 = 1

	unsigned long &Fraction();
	unsigned long Fraction() const;

	// return the current date/time value
	static SADateTime currentDateTime();
};

class SQLAPI_API SAPos
{
	friend class SACommand;

	SAString m_sName;

public:
	SAPos(int nByID);
	SAPos(const SAString& sByName);
};

class SQLAPI_API saOptions
{
	int		m_nOptionCount;
	SAParam	**m_ppOptions;

private:
	// disable copy constructor
	saOptions(const saOptions &);
	// disable assignment operator
	saOptions &operator = (const saOptions &);

public:
	saOptions();
	virtual ~saOptions();

	SAString &operator[](const SAString &sOptionName);
	SAString operator[](const SAString &sOptionName) const;
};

typedef void (*EnumCursors_t)(ISACursor *, void *);

class SQLAPI_API SAConnection
{
	friend class SACommand;
	friend class SAField;
	friend class SAParam;
	friend class ISAConnection;

private:
	// disable copy constructor
	SAConnection(const SAConnection &);
	// disable assignment operator
	SAConnection &operator = (const SAConnection &);

	SAClient_t m_eSAClient;
	ISAConnection *m_pISAConnection;
	SAMutex *m_pCommandsMutex;
	sa_Commands *m_pCommands;

	SAIsolationLevel_t m_eIsolationLevel;
	SAAutoCommit_t m_eAutoCommit;

	saOptions m_Options;

	int	nReserved;

protected:
	void EnumCursors(EnumCursors_t fn, void *pAddlData);
	void RegisterCommand(SACommand *pCommand);
	void UnRegisterCommand(SACommand *pCommand);
	ISACursor *GetISACursor(SACommand *pCommand);

public:
	SAConnection();
	virtual ~SAConnection();

	void setClient(SAClient_t eSAClient);
	SAClient_t Client() const;
	long ClientVersion() const;
	long ServerVersion() const;
	SAString ServerVersionString() const;

	bool isConnected() const;
	void Connect(
		const SAString &sDBString,
		const SAString &sUserID,
		const SAString &sPassword,
		SAClient_t eSAClient = SA_Client_NotSpecified);
	void Disconnect();

	void setIsolationLevel(SAIsolationLevel_t eIsolationLevel);
	SAIsolationLevel_t IsolationLevel() const;
	void setAutoCommit(SAAutoCommit_t eAutoCommit);
	SAAutoCommit_t AutoCommit() const;
	void Commit();
	void Rollback();

	SAString &setOption(const SAString &sOptionName);
	SAString Option(const SAString &sOptionName) const;

	saAPI *NativeAPI() const;
	saConnectionHandles *NativeHandles();
};

class SQLAPI_API SACommand
{
	friend class SAConnection;
	friend class IibCursor;
	friend class IsybCursor;
	friend class IssDBLibCursor;
	friend class IsbCursor;

private:
	// disable copy constructor
	SACommand(const SACommand &);
	// disable assignment operator
	SACommand &operator = (const SACommand &);

	SAConnection	*m_pConnection;

	SACommandType_t	m_eCmdType;
	SAString		m_sCmd;
	bool			m_bPrepared;
	bool			m_bExecuted;
	bool			m_bFieldsDescribed;
	bool			m_bSelectBuffersSet;

	bool			m_bParamsKnown;
	int				m_nPlaceHolderCount;
	saPlaceHolder	**m_ppPlaceHolders;
	int				m_nParamCount;
	SAParam			**m_ppParams;
	int				m_nCurParamID;
	SAString		m_sCurParamName;
	
	int				m_nFieldCount;
	SAField			**m_ppFields;

	saOptions		m_Options;

	int	nReserved;

	void Init();

	static int CompareIdentifier(
		const SAString &sIdentifier1,
		const SAString &sIdentifier2);
	SAParam &CreateParam(
		const SAString &sName,
		SADataType_t eParamType,
		int nNativeType,
		int nParamSize,
		int	nParamPrecision,
		int	nParamScale,
		SAParamDirType_t eDirType,
		const SAString &sFullName,
		int nStart,	// param position in SQL statement
		int nEnd);	// param end position in SQL statemen
	void GetParamsSP();
	void UnDescribeParams();
	void ParseInputMarkers(
		const SAString &sCmd,
		bool *pbSpacesInCmd);

	void DescribeFields();
	void CreateField(
		const SAString &sName,
		SADataType_t eFieldType,
		int nNativeType,
		int nFieldSize,
		int nFieldPrecision,
		int nFieldScale,
		bool bFieldRequired);
	void DestroyFields();

	// parses sql statement and create bind parameters array if any (In)
	// also cancels previous stsement if any
	void ParseCmd(
		const SAString &sSQL,
		SACommandType_t eCmdType);

	void UnSetCommandText();
	void UnPrepare();
	void UnExecute();

public:
	SACommand();	// construct command with no associated connection and SQL
	SACommand(		// construct command based on the given connection and SQL
		SAConnection *pConnection,
		const SAString &sCmd = SAString(),
		SACommandType_t eCmdType = SA_CmdUnknown);
	
	virtual ~SACommand();

	SAConnection *Connection() const;
	void setConnection(SAConnection *pConnection);

	virtual void Open();
	virtual bool isOpened();
	virtual void Close();

	void setCommandText(
		const SAString &sSQL,
		SACommandType_t eCmdType = SA_CmdUnknown);
	SAString CommandText() const;
	SACommandType_t CommandType() const;
	virtual void Prepare();
	virtual void Execute();
	bool isResultSet();
	long RowsAffected();	// returns number of rows affected by last DML operation
	bool FetchNext();	// returns true if new row is fetched
	void Cancel();

	SAParam &CreateParam(
		const SAString &sName,
		SADataType_t eParamType,
		int nNativeType,
		int nParamSize,
		int	nParamPrecision,
		int	nParamScale,
		SAParamDirType_t eDirType);
	void DestroyParams();
	int ParamCount();
	SAParam &ParamByIndex(int i);	// zero based index of C array
	SAParam &Param(int nParamByID);	// id in SQL statement, not in C array
	SAParam &Param(const SAString& sParamByName);
	SACommand &operator << (const SAPos &pos);
	SACommand &operator << (const SANull &null);
	SACommand &operator << (bool Value);
	SACommand &operator << (short Value);
	SACommand &operator << (long Value);
	SACommand &operator << (double Value);
	SACommand &operator << (const SANumeric &Value);
	SACommand &operator << (const SADateTime &Value);
	SACommand &operator << (const SAChar *Value);	// special overload for string constants
	SACommand &operator << (const SAString &Value);
	SACommand &operator << (const SABytes &Value);
	SACommand &operator << (const SALongBinary &Value);
	SACommand &operator << (const SALongChar &Value);
	SACommand &operator << (const SABLob &Value);
	SACommand &operator << (const SACLob &Value);
	SACommand &operator << (const SAValueRead &Value);

	int FieldCount();
	SAField &Field(int nField);	// 1-based field number in result set
	SAField &Field(const SAString &sField);
	SAField &operator[](int nField);	// 1-based field number in result set
	SAField &operator[](const SAString &sField);

	SAString &setOption(const SAString &sOptionName);
	SAString Option(const SAString &sOptionName) const;

	saCommandHandles *NativeHandles();
};

class SQLAPI_API SAValueRead
{
	friend class ISACursor;
	friend class IibCursor;
	friend class Iora7Cursor;
	friend class Iora8Cursor;
	friend class IsbCursor;
	friend class IodbcCursor;
	friend class IssDBLibCursor;
	friend class IssOleDbCursor;
	friend class Idb2Cursor;
	friend class IinfCursor;
	friend class IsybCursor;
	friend class ImyCursor;
	friend class IpgCursor;

protected:
	SALongOrLobReaderModes_t m_eReaderMode;
	
	saLongOrLobReader_t	m_fnReader;
	unsigned int	m_nReaderWantedPieceSize;
	void			*m_pReaderAddlData;

	unsigned char	*m_pReaderBuf;
	unsigned int	m_nReaderAlloc;
	unsigned int	m_nExpectedSizeMax;
	unsigned int	m_nReaderRead;
	unsigned int	m_nPieceSize;

	unsigned int PrepareReader(
		unsigned int nExpectedSizeMax,	// to optimaze buf allocation for internal buffer, 0 = unknown
		unsigned int nCallerMaxSize,	// max Piece that can be proceeced by API
		unsigned char *&pBuf,
		saLongOrLobReader_t fnReader,
		unsigned int nReaderWantedPieceSize,
		void *pReaderAddlData,
		bool bAddSpaceForNull = false);
	void InvokeReader(
		SAPieceType_t ePieceType,
		unsigned char *&pBuf,
		unsigned int nPieceLen);
	SAString asLongOrLob() const;

protected:
	SADataType_t	m_eDataType;

	// null indicator
	bool	*m_pbNull;
	// scalar types
	void	*m_pScalar;
	// an exact numeric value with a fixed precision and scale
	SANumeric	*m_pNumeric;
	// Date time
	SADateTime	*m_pDateTime;
	// variable length types (string, bytes, Longs and Lobs)
	SAString	*m_pString;

⌨️ 快捷键说明

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