Using OTL to monitor Oracle and DB2 code encapsulation

Source: Internet
Author: User
Tags mutex sprintf

#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 &AMP;SQL); Row GetRow (const std::string &sql); Table GetTable (const std::string &sql);//Get data for a field in a row static inline string Getvaluebyindex (const row &row, Unsig Ned 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 &Amp;& col_index >= 0 && Row_index < table.size ()) {if (Col_index < Table[row_index].size ()) {return tab Le[row_index][col_index];}} Return "";} Static inline Row getrowbyindex (const Table &table, unsigned int index) {Row row;if (index >= 0 && index &lt ; 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_ a 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::D isconnect () {if (m_db.connected) {M_db.logoff ();//Disconnect from Oracle}}bool otl_oracle::D Oqueryfirst (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 EXceptionsreturn false;} return true;} BOOL Otl_oracle::D oquerynext (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::D oqueryend () {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]);D oqueryend (); return true;}}} return false;} String otl_oracle::getvaluestr (const std::string &sql) {Row row=getrow (SQL), if (Row.size ()) {return row[0];} Elsereturn "";} Otl_oracle::row otl_oracle::getrow (const std::string &sql) {row row;if (Doqueryfirst (SQL)) Doquerynext (row);D Oqueryend (); 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); Case Otl_var_float:{float F;o >> f;sprintf (outbuf, "%f", f); Case Otl_var_int:case otl_var_short:case otl_var_long_int:{int i;o >> i;sprintf (outbuf, "%d", I); Case otl_var_unsigned_int:{unsigned int I;o >> i;sprintf (outbuf, "%u", I); Case 8://! Otl_var_timestampcase 16://! Otl_var_db2timecase 17://! Otl_var_db2datecase 18://! Otl_var_tz_timestampcase 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;}

Using OTL to monitor Oracle and DB2 code encapsulation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.