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

📄 sybclient.cpp

📁 通用的数据库中间库
💻 CPP
📖 第 1 页 / 共 4 页
字号:

/*virtual */
void IsybConnection::Commit()
{
	// commit
	SACommand cmd(
		m_pSAConnection,
		"commit tran", SA_CmdSQLStmt);
	cmd.Execute();

	// and start new transaction if not in autocommit mode
	if(m_pSAConnection->AutoCommit() == SA_AutoCommitOff)
	{
		cmd.setCommandText("begin tran", SA_CmdSQLStmt);
		cmd.Execute();
	}

	cmd.Close();
}

/*virtual */
void IsybConnection::Rollback()
{
	// rollback
	SACommand cmd(
		m_pSAConnection,
		"rollback tran", SA_CmdSQLStmt);
	cmd.Execute();

	// and start new transaction if not in autocommit mode
	if(m_pSAConnection->AutoCommit() == SA_AutoCommitOff)
	{
		cmd.setCommandText("begin tran", SA_CmdSQLStmt);
		cmd.Execute();
	}

	cmd.Close();
}

//////////////////////////////////////////////////////////////////////
// IsybClient Class
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IsybClient::IsybClient()
{

}

IsybClient::~IsybClient()
{

}

ISAConnection *IsybClient::QueryConnectionInterface(
	SAConnection *pSAConnection)
{
	return new IsybConnection(pSAConnection);
}

//////////////////////////////////////////////////////////////////////
// IsybCursor Class
//////////////////////////////////////////////////////////////////////

class IsybCursor : public ISACursor
{
	sybCommandHandles	m_handles;

	bool m_bResultsPending;
	void CheckAndCancelPendingResults();
	void CancelPendingResults();
	void ProcessBatchUntilEndOrResultSet();
	CS_INT	m_nRowsAffected;

	int m_cRowsToPrefetch;	// defined in SetSelectBuffers
	CS_INT m_cRowsObtained;
	CS_INT m_cRowCurrent;

	void BindImage(SAParam &Param, SAString *pValue);
	void BindText(SAParam &Param, SAString *pValue);

	void FetchStatusResult();
	void FetchParamResult();

protected:
	virtual unsigned int InputBufferSize(
		const SAParam &Param) const;
	virtual unsigned int OutputBufferSize(
		SADataType_t eDataType,
		unsigned int nDataSize) const;

	virtual SADataType_t CnvtNativeToStd(
		int nNativeType,
		int nNativeSubType,
		int nSize,
		int nPrec,
		int nScale) const;
	virtual int CnvtStdToNative(SADataType_t eDataType) const;

	SADataType_t CnvtNativeTypeFromASESysColumnsToStd(
		int dbtype, int/* dbsubtype*/, int/* dbsize*/, int prec, int scale) const;
	SADataType_t CnvtNativeTypeFromASADomainIDToStd(
		int dbtype, int/* dbsubtype*/, int/* dbsize*/, int prec, int scale) const;

protected:
	void ct_bind_Buffer(	// used for fields and output(return) params
		int nPos,	// 1-based
		void *pInd,
		unsigned int nIndSize,
		void *pSize,
		unsigned int nSizeSize,
		void *pValue,
		unsigned int nValueSize,
		SADataType_t eDataType,
		SAString sName,
		CS_INT count);

	virtual void SetFieldBuffer(
		int nCol,	// 1-based
		void *pInd,
		unsigned int nIndSize,
		void *pSize,
		unsigned int nSizeSize,
		void *pValue,
		unsigned int nValueSize);
	virtual bool IndicatorIsNull(
		int nPos,	// 1-based
		SAValueRead &vr,
		ValueType_t eValueType,
		void *pInd, unsigned int nIndSize,
		void *pSize, unsigned int nSizeSize,
		unsigned int &nRealSize,
		int nBulkReadingBufPos) const;

public:
	IsybCursor(
		IsybConnection *pIsybConnection,
		SACommand *pCommand);
	virtual ~IsybCursor();

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

	// prepare statement (also convert to native format if needed)
	virtual void Prepare(
		const SAString &sStmt,
		SACommandType_t eCmdType,
		int nPlaceHolderCount,
		saPlaceHolder **ppPlaceHolders);
	// binds parameters
	void Bind(
		int nPlaceHolderCount,
		saPlaceHolder **ppPlaceHolders);
	// executes statement
	virtual void Execute(
		int nPlaceHolderCount,
		saPlaceHolder **ppPlaceHolders);
	// cleans up after execute if needed, so the statement can be reexecuted
	virtual void UnExecute();
	virtual void Cancel();

	virtual bool ResultSetExists();
	virtual void DescribeFields(
		DescribeFields_cb_t fn);
	virtual void SetSelectBuffers();
	virtual bool FetchNext();

	virtual long GetRowsAffected();

	virtual void ReadLongOrLOB(
		ValueType_t eValueType,
		SAValueRead &vr,
		void *pValue,
		unsigned int nFieldBufSize,
		saLongOrLobReader_t fnReader,
		unsigned int nReaderWantedPieceSize,
		void *pAddlData);

	virtual void DescribeParamSP();

	virtual saCommandHandles *NativeHandles();
};

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IsybCursor::IsybCursor(
	IsybConnection *pIsybConnection,
	SACommand *pCommand) :
	ISACursor(pIsybConnection, pCommand)
{
	m_bResultsPending = false;
	m_nRowsAffected = -1;
}

/*virtual */
IsybCursor::~IsybCursor()
{
}

/*virtual */
unsigned int IsybCursor::InputBufferSize(
	const SAParam &Param) const
{
	switch(Param.DataType())
	{
	case SA_dtBool:
		return sizeof(CS_BIT);
	case SA_dtDateTime:
		return sizeof(CS_DATETIME);
	case SA_dtLongBinary:
	case SA_dtLongChar:
	case SA_dtBLob:
	case SA_dtCLob:
		return 0;
	default:
		break;
	}
	
	return ISACursor::InputBufferSize(Param);
}

/*virtual */
unsigned int IsybCursor::OutputBufferSize(
	SADataType_t eDataType,
	unsigned int nDataSize) const
{
	switch(eDataType)
	{
	case SA_dtBool:
		return sizeof(CS_BIT);
	case SA_dtDateTime:
		return sizeof(CS_DATETIME);
	case SA_dtLongBinary:
	case SA_dtLongChar:
		return 0;
	default:
		break;
	}
	
	return ISACursor::OutputBufferSize(eDataType, nDataSize);
}

SADataType_t IsybCursor::CnvtNativeTypeFromASADomainIDToStd(
	int dbtype, int/* dbsubtype*/, int/* dbsize*/, int prec, int scale) const
{
	SADataType_t eDataType = SA_dtUnknown;

	switch(dbtype)
	{
	case 11:	//binary
	case 28:	//varbinary
		eDataType = SA_dtBytes;
		break;
	case 7:		//char
	case 8:		//char
	case 9:		//varchar
		eDataType = SA_dtString;
		break;
	case 1:		//smallint
	case 19:	//tinyint
	case 22:	//unsigned smallint
		eDataType = SA_dtShort;
		break;
	case 24:	//bit
		eDataType = SA_dtBool;
		break;
	case 2:		//integer
	case 21:	//unsigned int
		eDataType = SA_dtLong;
		break;
	case 4:		//float
	case 5:		//double
	case 20:	//bigint
	case 23:	//unsigned bigint
		eDataType = SA_dtDouble;
		break;
	case 3:		//numeric
	case 27:	//decimal
		if(scale <= 0)
		{	// check for exact type
			if(prec <= 5)
				eDataType = SA_dtShort;
			else if(prec <= 10)
				eDataType = SA_dtLong;
			else
				eDataType = SA_dtDouble;
		}
		else
			eDataType = SA_dtDouble;
		break;
	case 6:		//date
	case 13:	// timestamp
	case 14:	//time
		eDataType = SA_dtDateTime;
		break;
	case 12:	// long binary
		eDataType = SA_dtLongBinary;
		break;
	case 10:	//long varchar
		eDataType = SA_dtLongChar;
		break;
	default:
		assert(false);
	}

	return eDataType;
}

SADataType_t IsybCursor::CnvtNativeTypeFromASESysColumnsToStd(
	int dbtype, int/* dbsubtype*/, int/* dbsize*/, int prec, int scale) const
{
	SADataType_t eDataType = SA_dtUnknown;

	switch(dbtype)
	{
	case 0x25:	//SQLVARBINARY
		eDataType = SA_dtBytes;
		break;
	case 0x27:	//SQLVARCHAR
		eDataType = SA_dtString;
		break;
	case 0x2f:	//SQLCHAR:	// char, varchar
		eDataType = SA_dtString;

		break;
	case 0x2d:	//SQLBINARY:	// binary, varbinary
		eDataType = SA_dtBytes;
		break;
	case 0x30:	//SQLINT1:	// tinyint
		eDataType = SA_dtShort;
		break;
	case 0x34:	//SQLINT2:	// smallint
		eDataType = SA_dtShort;
		break;
	case 0x38:	//SQLINT4:	// int
		eDataType = SA_dtLong;
		break;
	case 0x3b:	//SQLFLT4:	// real
		eDataType = SA_dtDouble;
		break;
	case 0x3e:	//SQLFLT8:	// float
		eDataType = SA_dtDouble;
		break;
	case 0x7a:	//SQLMONEY4:	// smallmoney
		eDataType = SA_dtDouble;
		break;
	case 0x3c:	//SQLMONEY:	// money
		eDataType = SA_dtDouble;
		break;
	case 0x37:	// decimal
	case 0x3f:	// numeric
		if(scale <= 0)
		{	// check for exact type
			if(prec <= 5)
				eDataType = SA_dtShort;
			else if(prec <= 10)
				eDataType = SA_dtLong;
			else
				eDataType = SA_dtDouble;
		}
		else
			eDataType = SA_dtDouble;
		break;
	case 0x3a:	//SQLDATETIM4:	// smalldatetime
		eDataType = SA_dtDateTime;
		break;
	case 0x3d:	//SQLDATETIME:	// datetime
		eDataType = SA_dtDateTime;
		break;
	case 0x22:	//SQLIMAGE:	// image
		eDataType = SA_dtLongBinary;
		break;
	case 0x23:	//SQLTEXT:	// text
		eDataType = SA_dtLongChar;
		break;
	case 0x32:	//SQLBIT
		eDataType = SA_dtBool;
		break;
	default:
		assert(false);
	}

	return eDataType;
}

/*virtual */
SADataType_t IsybCursor::CnvtNativeToStd(
	int dbtype, int/* dbsubtype*/, int/* dbsize*/, int prec, int scale) const
{
	SADataType_t eDataType = SA_dtUnknown;

	switch(dbtype)
	{
	case CS_CHAR_TYPE:
		eDataType = SA_dtString;
		break;
	case CS_BINARY_TYPE:
		eDataType = SA_dtBytes;
		break;
	case CS_LONGCHAR_TYPE:
		eDataType = SA_dtLongChar;
		break;
	case CS_LONGBINARY_TYPE:
		eDataType = SA_dtLongBinary;
		break;
	case CS_TEXT_TYPE:
		eDataType = SA_dtLongChar;
		break;
	case CS_IMAGE_TYPE:
		eDataType = SA_dtLongBinary;
		break;
	case CS_TINYINT_TYPE:
		eDataType = SA_dtShort;
		break;
	case CS_SMALLINT_TYPE:
		eDataType = SA_dtShort;
		break;
	case CS_INT_TYPE:
		eDataType = SA_dtLong;
		break;
	case CS_REAL_TYPE:
		eDataType = SA_dtDouble;
		break;
	case CS_FLOAT_TYPE:
		eDataType = SA_dtDouble;
		break;
	case CS_BIT_TYPE:
		eDataType = SA_dtBool;
		break;
	case CS_DATETIME_TYPE:
		eDataType = SA_dtDateTime;
		break;
	case CS_DATETIME4_TYPE:
		eDataType = SA_dtDateTime;
		break;
	case CS_MONEY_TYPE:
		eDataType = SA_dtDouble;
		break;
	case CS_MONEY4_TYPE:
		eDataType = SA_dtDouble;
		break;
	case CS_NUMERIC_TYPE:
	case CS_DECIMAL_TYPE:
		if(scale > 0)
			eDataType = SA_dtDouble;
		else	// check for exact type
		{	
			if(prec <= 5)
				eDataType = SA_dtShort;
			else if(prec <= 10)
				eDataType = SA_dtLong;
			else
				eDataType = SA_dtDouble;
		}
		break;
	case CS_VARCHAR_TYPE:
		eDataType = SA_dtString;
		break;
	case CS_VARBINARY_TYPE:
		eDataType = SA_dtBytes;
		break;
	case CS_LONG_TYPE:
		assert(false);
		break;
	case CS_SENSITIVITY_TYPE:
		assert(false);
		break;
	case CS_BOUNDARY_TYPE:
		assert(false);
		break;
	case CS_VOID_TYPE:
		assert(false);
		break;
	case CS_USHORT_TYPE:
		assert(false);
		break;
	default:
		assert(false);
		break;
	}

	return eDataType;
}

/*virtual */
int IsybCursor::CnvtStdToNative(SADataType_t eDataType) const
{
	CS_INT type;

	switch(eDataType)
	{
	case SA_dtUnknown:
		throw SAException(SA_Library_Error, -1, -1, IDS_UNKNOWN_DATA_TYPE);
	case SA_dtBool:
		type = CS_BIT_TYPE;	// single bit type
		break;
	case SA_dtShort:
		type = CS_SMALLINT_TYPE;	// 2-byte integer type
		break;
	case SA_dtLong:
		type = CS_INT_TYPE;	// 4-byte integer type
		break;
	case SA_dtDouble:
		type = CS_FLOAT_TYPE;		// 8-byte float type
		break;
	case SA_dtDateTime:
		type = CS_DATETIME_TYPE;	// 8-byte datetime type
		break;
	case SA_dtString:
		type = CS_CHAR_TYPE;		// character type
		break;
	case SA_dtBytes:
		type = CS_BINARY_TYPE;		// binary type
		break;
	case SA_dtLongBinary:
	case SA_dtBLob:
		type = CS_IMAGE_TYPE;		// image type
		break;
	case SA_dtLongChar:
	case SA_dtCLob:
		type = CS_TEXT_TYPE;		// text type
		break;
	default:
		type = 0;
		assert(false);
	}

	return type;
}

/*virtual */
bool IsybCursor::IsOpened()
{
	return m_handles.m_command != NULL;
}

/*virtual */
void IsybCursor::Open()
{
	assert(m_handles.m_command == NULL);

	((IsybConnection*)m_pISAConnection)->Check(g_sybAPI.ct_cmd_alloc(
		((IsybConnection*)m_pISAConnection)->m_handles.m_connection,
		&m_handles.m_command));
}

void IsybCursor::CancelPendingResults()
{
	// cancel any results pending
	((IsybConnection*)m_pISAConnection)->Check(g_sybAPI.ct_cancel(
		NULL, m_handles.m_command, CS_CANCEL_ALL));

	m_bResultsPending = false;
}

void IsybCursor::CheckAndCancelPendingResults()
{
	// cancel results pending if any
	if(m_bResultsPending)
		CancelPendingResults();
}

/*virtual */
void IsybCursor::Close()
{
	assert(m_handles.m_command != NULL);

	CancelPendingResults();

	((IsybConnection*)m_pISAConnection)->Check(g_sybAPI.ct_cmd_drop(m_handles.m_command));
	m_handles.m_command = NULL;
}

/*virtual */
ISACursor *IsybConnection::NewCursor(SACommand *m_pCommand)
{
	return new IsybCursor(this, m_pCommand);
}

/*virtual */
void IsybCursor::Prepare(
	const SAString &/*sStmt*/,
	SACommandType_t/* eCmdType*/,
	int/* nPlaceHolderCount*/,
	saPlaceHolder ** /*ppPlaceHolders*/)

⌨️ 快捷键说明

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