使用otl监控oracle和DB2代码封装

#ifndef _OTL_ORACLE_H_
#define _OTL_ORACLE_H_

#ifdef WIN32
#include <windows.h>
#endif
#include <vector>
#include <base/common.h>
#include <base/string_util.h>
#include <boost/thread/mutex.hpp>
#define OTL_DB2_CLI //OTL_ORA9I
#include "../otlv4.h"
namespace thefox{
	namespace db{

		class otl_oracle{

		public:
			typedef std::vector<string> Row;
			typedef std::vector<Row> Table;
			otl_oracle();
			~otl_oracle();

			void SetConnStr(const string &connStr);

			bool Connect();
			void Disconnect();

			bool GetValueInt(const std::string &sql,int64_t &value);
			string GetValueStr(const std::string &sql);
			Row GetRow(const std::string &sql);
			Table  GetTable(const std::string &sql);
			
			// 得到一行中某个字段的数据
			static inline string GetValueByIndex(const Row &row, unsigned int index)
			{
				if (index >= 0 && index < row.size()) {
					return row[index];
				}
				return "";
			}
			static inline string GetValueByIndex(const Table &table, unsigned int row_index,unsigned int col_index)
			{
				if (row_index >= 0 && col_index >= 0 && row_index < table.size()) {
					if (col_index < table[row_index].size())
					{
						return table[row_index][col_index];
					}
					
				}
				return "";
			}
			static inline Row GetRowByIndex(const Table &table, unsigned int index)
			{
				Row row;
				if (index >= 0 && index < table.size()) {
					row = table[index];
				}
				
				return row;
			}
		private:
			bool DoQueryFirst(const string &ssql);
			bool DoQueryNext(Row &row);
			void DoQueryEnd();
			char* getNextVariable(otl_stream &o, char *outBuf);
			string m_connStr;
			otl_connect m_db;
			otl_stream m_o;
			boost::mutex  m_mutex;
		};



	}//end namespace db
}//end namespace thefox


#endif//END DEFINE _OTL_ORACLE_H_

#include "otl_oracle.h"
#include <log/Logging.h>

using namespace std;
using namespace thefox;
using namespace db;


otl_oracle::otl_oracle()
	:m_connStr("")
{}

otl_oracle::~otl_oracle()
{
	Disconnect();
}

void otl_oracle::SetConnStr(const string &connStr){
	m_connStr = connStr;
}

bool otl_oracle::Connect(){
	if (m_connStr.empty())
	{
		THEFOX_LOG(WARN) << "oracle conn str is empty";
		return false;
	}

	if (m_db.connected) {
		Disconnect();
	}

	try {
		m_db.rlogon(m_connStr.c_str());// connect to Oracle
	} catch(otl_exception& p){
		THEFOX_LOG(ERROR) << "otl error status:" << p.code <<"sql:"<<(char*)(p.msg);
		return false;
	}
	//THEFOX_LOG(TRACE) << "connect success connstr:" << m_connStr.c_str() ;
	return true;

}

void otl_oracle::Disconnect(){

	if(m_db.connected){
		m_db.logoff(); // disconnect from Oracle
	}
}

bool otl_oracle::DoQueryFirst(const string &ssql){
	m_mutex.lock();
	if (!m_db.connected) {
		return false;
	}
	try{
		m_o.open(1,ssql.c_str(),m_db);
	} catch(otl_exception& p) { // intercept OTL exceptions
		return false;
	}
	return true;
}

bool otl_oracle::DoQueryNext(Row &row){
	char buf[65535];
	row.clear();
	while (!m_o.eof())
	{
		getNextVariable(m_o,buf);
		row.push_back(string(buf));
		try
		{
			m_o.check_end_of_row();
		}
		catch (otl_exception&)
		{
			continue;
		}
		return true;
	}

	return false;
}

void otl_oracle::DoQueryEnd(){
	if (m_o.good())
		m_o.close();
	m_mutex.unlock();
}

bool otl_oracle::GetValueInt(const std::string &sql, int64_t &value)
{
	value = 0;
	Row row;
	if (DoQueryFirst(sql))
	{
		if (DoQueryNext(row)) {
			if (!row.empty()) {
				value = StringUtil::stoi64(row[0]);
				DoQueryEnd();
				return true;
			}
		}
	}
	
	return false;
}
string otl_oracle::GetValueStr(const std::string &sql){
	Row row=GetRow(sql);
	if (row.size())
	{
		return row[0];
	}else
		return "";
}

otl_oracle::Row otl_oracle::GetRow(const std::string &sql)
{
	Row row;
	if(DoQueryFirst(sql))
		DoQueryNext(row);
	DoQueryEnd();
	return row;
}

otl_oracle::Table otl_oracle::GetTable(const std::string &sql)
{
	Table table;
	Row row;
	if (DoQueryFirst(sql))
	{
		while (DoQueryNext(row)) {
			if (!row.empty()) {
				table.push_back(row);
			}
		}
	}
	DoQueryEnd();
	return table;
}

char* otl_oracle::getNextVariable(otl_stream &o, char *outBuf)
{
	strcpy(outBuf,"");
	int type = o.describe_next_out_var()->ftype;
	switch (type){
	case otl_var_char:
		{
			o >> outBuf;
			break;
		}
	case otl_var_double:
		{
			double d;
			o >> d;
			sprintf(outBuf, "%lf", d);
			break;
		}
	case otl_var_float:
		{
			float f;
			o >> f;
			sprintf(outBuf, "%f", f);
			break;
		}
	case otl_var_int:
	case otl_var_short:
	case otl_var_long_int:
		{
			int i;
			o >> i;
			sprintf(outBuf, "%d", i);
			break;
		}
	case otl_var_unsigned_int:
		{
			unsigned int i;
			o >> i;
			sprintf(outBuf, "%u", i);
			break;
		}
	case 8://! otl_var_timestamp
	case 16://! otl_var_db2time
	case 17://! otl_var_db2date
	case 18://! otl_var_tz_timestamp
	case 19://! otl_var_ltz_timestamp
		{
			otl_datetime f1;
			o>>f1;
			sprintf(outBuf, "%4d-%2d-%2d %2d:%2d:%2d",
				f1.year, f1.month, f1.day, f1.hour, f1.minute, f1.second);  
			break;
		}
	default :
		strcpy(outBuf, "");
		break;
	}

	return outBuf;
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。