| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- #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<std::list<std::vector<CString>>, bool> COtlConn::GetAllValues(CString & ErrMessage)
- {
- std::list<std::vector<CString>> lisRows;
- try {
- std::vector<CString> 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<std::list<std::vector<CString>>, bool> COtlConn::GetAllValuesProc(CString & ErrMessage)
- //{
- // return std::pair<std::list<std::vector<CString>>, 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;
- //}
|