#include "stdafx.h" #include "OtlConn.h" COtlConn::COtlConn() :m_pStream(nullptr), m_nField(0) { otl_connect::otl_initialize(1); } COtlConn::~COtlConn() { try { if (this->m_pStream) { delete this->m_pStream; this->m_pStream = nullptr; } if (this->m_OtlConn.connected) { this->m_OtlConn.logoff(); } } catch (const otl_exception&) { } } bool COtlConn::ConnectDataBase(const CString & connStr, CString & ErrMessage) { try { m_OtlConn.rlogon(connStr, 1); return true; } catch (const otl_exception& e) { ErrMessage = e.msg; } return false; } //bool COtlConn::GetRecordSet(const CString & SQL, CString & ErrMessage) //{ // try // { // if (this->m_pStream) { // delete this->m_pStream; // this->m_pStream = nullptr; // } // this->m_pStream = new otl_stream(1, SQL, m_OtlConn, otl_implicit_select); // if (!this->m_pStream->eof()) // rs.attach(*this->m_pStream); // return true; // } // catch (const otl_exception& e) // { // ErrMessage = e.msg; // try // { // if (ErrMessage.Find("无效的游标状态") >= 0) { // if (this->m_pStream) { // delete this->m_pStream; // this->m_pStream = nullptr; // } // this->m_pStream = new otl_stream(1, SQL, m_OtlConn); // if (!this->m_pStream->eof()) // rs.attach(*this->m_pStream); // return true; // } // } // catch (const otl_exception& e) // { // ErrMessage = e.msg; // } // } // // return false; //} bool COtlConn::GetRecordSet(const CString & SQL, CString & ErrMessage, const bool & IsSaveRs) { try { if (this->m_pStream) { delete this->m_pStream; this->m_pStream = nullptr; } if (!IsSaveRs) { this->m_pStream = new otl_stream(1, SQL, m_OtlConn); } else { this->m_pStream = new otl_stream(1, SQL, m_OtlConn, otl_implicit_select); } return true; } catch (const otl_exception& e) { ErrMessage = e.msg; } return false; } int COtlConn::GetFields() { try { //int nField = 1; //rs.describe(nField); return this->m_nField; } catch (const otl_exception&) { return 0; } } std::pair>, bool> COtlConn::GetAllValues(CString & ErrMessage) { std::list> lisRows; try { std::vector vecRow; int nField = 1; this->m_pStream->describe_select(nField); this->m_nField = nField; std::string strTemp = ""; CString strTmp; while (!m_pStream->eof()) { vecRow.clear(); for (int i = 0; i < nField; ++i) { otl_var_desc* desck = m_pStream->describe_next_out_var(); switch (desck->ftype) { case otl_var_char: { *m_pStream >> strTemp; } break; case otl_var_double: case otl_var_float: { double dVal; *m_pStream >> dVal; strTemp = std::to_string(dVal); } break; case otl_var_int: case otl_var_unsigned_int: case otl_var_short: case otl_var_long_int: { std::int32_t nVal; *m_pStream >> nVal; strTemp = std::to_string(nVal); } break; case otl_var_timestamp: { otl_datetime dtTime; *m_pStream >> dtTime; char szTime[128]; snprintf(szTime, 128, "%d-%02d-%02d %02d:%02d:%02d", dtTime.year, dtTime.month, dtTime.day, dtTime.hour, dtTime.minute, dtTime.second); strTemp = (szTime); } break; default: *m_pStream >> strTemp; break; } strTmp = strTemp.data(); vecRow.emplace_back(strTmp); } lisRows.emplace_back(vecRow); } return std::make_pair(lisRows, true); } catch (const otl_exception& e) { ErrMessage = (char*)e.msg; return std::make_pair(lisRows, false); } } //std::pair>, bool> COtlConn::GetAllValuesProc(CString & ErrMessage) //{ // return std::pair>, bool>(); //} // //void COtlConn::close() //{ // try // { // m_OtlConn.logoff(); // } // catch (const otl_exception&) // { // // } //} // //bool COtlConn::GetValueStr(const int & nPos, CString & strValue, CString & ErrMessage) //{ // try // { // int pos = nPos; // std::string strVal; // rs.get(nPos, strVal); // strValue = strVal.data(); // return true; // } // catch (const otl_exception& e) // { // ErrMessage = e.msg; // } // return false; //}