#include "StdAfx.h" #include "WxAdoImpl.h" #include "WxAdoPool.h" #include "table.pb.h" #ifdef _DEBUG #define new DEBUG_NEW #endif #define GETDBPTR(dwTimeOut) \ pAdoObj pObj = NULL;\ CAdoConnGuard tagConnGuard(pObj, dwTimeOut);\ if ( pObj == NULL)\ return FALSE\ enum UserLogInStatus { USER_LOGIN = 1, // 登录成功; USER_PW_MISTAKE = 2, // 密码错误; USER_NULL = 0 // 没有用户; }; CWxAdoImpl* CWxAdoImpl::m_pInstance = NULL; CWxAdoImpl::CWxAdoImpl(void):m_nRef(0),m_nObjRef(0) { // 全局uid,64位整型,转字符长度17位; m_uniqueid.setWorkerId(10); m_uniqueid.setDatacenterId(20); } CWxAdoImpl::~CWxAdoImpl(void) { } ULONG CWxAdoImpl::AddRef() { InterlockedIncrement( &m_nRef ); //增加引用计数; return m_nRef; } ULONG CWxAdoImpl::Release() { InterlockedDecrement( &m_nRef ); //减少引用计数; //如果为0,删除对象 if( m_nRef == 0 ) { delete this; } return m_nRef; } HRESULT CWxAdoImpl::QueryInterface(REFIID refiid, void **ppvObject) { if (IID_IUnknown == refiid) { *ppvObject = this; } else if (IID_IWxAdoInterface == refiid) { *ppvObject = (IWxAdoInterface*)this; } else { *ppvObject = NULL; return E_NOINTERFACE; } ((IUnknown*)(*ppvObject))->AddRef(); return NOERROR; } /************************************************************************/ /* 函数:[9/25/2016 IT]; /* 描述:; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ void CWxAdoImpl::SolveDBError( IN CONST DWORD &dwError, IN LPVOID pDBConn) { switch(dwError) { //case SQL_ERROR://这个错误比较难精确判断问题; case ERROR_PIPE_NOT_CONNECTED: case WSAECONNRESET: case WSAECONNABORTED: { pAdoObj pObj = (pAdoObj)pDBConn; CWxAdoPool::GetInstance()->CloseBusyConnection(pObj); #if _DEBUG OutputDebugString(_T("关闭数据库连接\n")); #endif } break; default: break; } } //template //BOOL CWxAdoImpl::GetFiedValue(IN _RecordsetPtr rst, IN LPCTSTR lpFiedName, T &value) //{ // typeid(value); // _variant_t var = rst->GetCollect(lpFiedName); //switch(var.vt) //{ //case VT_EMPTY: // break; //case VT_NULL: // break; //case VT_I2://2 byte signed int // value = var.iVal; // break; //case VT_I4://4 byte signed int // value = var.iVal; // break; //case VT_R4://4 byte real // value = var.fltVal; // break; //case VT_R8://8 byte real // value = var.dblVal; // break; //case VT_CY://currency // break; //case VT_DATE: // { // COleDateTime dt(var); // value = dt.Format(_T("%Y-%m-%d %H:%M:%S")); // } // break; //case VT_BSTR://OLE Automation string // value = _bstr_t(var.bstrVal); // break; //case VT_DISPATCH://IDispatch * // break; //case VT_ERROR://SCODE // break; //case VT_BOOL://True=-1, False=0 // value = var.boolVal; // break; //case VT_VARIANT://VARIANT * // break; //case VT_UNKNOWN://IUnknown * // break; //case VT_DECIMAL://16 byte fixed point // break; //case VT_RECORD://user defined type // break; //case VT_I1: // signed char // value = var.pcVal; // break; //case VT_UI1://unsigned char // value = var.bVal; // break; //case VT_UI2://unsigned short // value = var.uiVal; // break; //case VT_UI4://unsigned long // value = var.uiVal; // break; //case VT_I8://signed 64-bit int // value = var.lVal; // break; //case VT_UI8://unsigned 64-bit int // value = var.ulVal; // break; //case VT_INT://signed machine int // value = var.intVal; // break; //case VT_UINT://unsigned machine int // value = var.uintVal; // break; //case VT_INT_PTR://signed machine register size width // break; //case VT_UINT_PTR://unsigned machine register size width // break; //case VT_VOID://C style void // break; //case VT_HRESULT://Standard return type // break; //case VT_PTR://pointer type // break; //case VT_SAFEARRAY://(use VT_ARRAY in VARIANT) // break; //case VT_CARRAY://C style array // break; //case VT_USERDEFINED://user defined type // break; //case VT_LPSTR://null terminated string // break; //case VT_LPWSTR://wide null terminated string // break; //case VT_FILETIME://FILETIME // break; //case VT_BLOB://Length prefixed bytes // break; //case VT_STREAM://Name of the stream follows // break; //case VT_STORAGE://Name of the storage follows // break; //case VT_STREAMED_OBJECT://Stream contains an object // break; //case VT_STORED_OBJECT://Storage contains an object // break; //case VT_VERSIONED_STREAM://Stream with a GUID version // break; //case VT_BLOB_OBJECT://Blob contains an object // break; //case VT_CF://Clipboard format // break; //case VT_CLSID://A Class ID // break; //case VT_VECTOR://simple counted array // break; //case VT_ARRAY://SAFEARRAY* // break; //case VT_BYREF://void* for local use // break; //case VT_BSTR_BLOB://Reserved for system use // break; //default: // value = var.iVal; //} // return TRUE; //} /************************************************************************/ /* 函数:[9/25/2016 IT]; /* 描述:; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ DWORD CWxAdoImpl::InitializePool( IN LPCTSTR lpDBType, IN LPCTSTR lpDBSource, IN CONST DWORD &dwDBPort, IN LPCTSTR lpDBAccount, IN LPCTSTR lpPassWord, IN LPCTSTR lpDBName, IN CONST INT &nMinConn /* = 1 */, IN CONST INT &nMaxConn /* = 5 */ ) { return CWxAdoPool::GetInstance()->InitializePool(lpDBType, lpDBSource, dwDBPort, lpDBAccount, lpPassWord, lpDBName, nMinConn, nMaxConn); } /************************************************************************/ /* 函数:[9/25/2016 IT]; /* 描述:; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ void CWxAdoImpl::ReleasePool() { CWxAdoPool *pObj = CWxAdoPool::GetInstance(); if ( pObj ) { pObj->ReleasePool(); delete pObj; pObj = NULL; } } /************************************************************************/ /* 函数:[9/25/2016 IT]; /* 描述:; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ BOOL CWxAdoImpl::ExecuteSQL( IN LPCTSTR lpExcuteSQL, IN CONST DWORD &dwTimeOut /* = 30000 */ ) { GETDBPTR(dwTimeOut); try { if ( lpExcuteSQL == NULL || pObj == NULL ) return FALSE; pObj->pCommand->CommandText = _bstr_t(lpExcuteSQL); // 执行SQL语句,返回记录集 pObj->pCommand->Execute(NULL, NULL, adCmdText); } catch (_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); SolveDBError(e.Error(), pObj); WriteTextLog(_T("SQL出错:%08lx,%s,%s,%s"), e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription); return FALSE; } return TRUE; } /************************************************************************/ /* 函数:[9/25/2016 IT]; /* 描述:; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ BOOL CWxAdoImpl::IsUserExist( IN LPCTSTR lpPhone, IN LPCTSTR lpPassword, OUT LPVOID lpOutValue,IN CONST DWORD &dwTimeOut /* = 3000 */ ) { GETDBPTR(dwTimeOut); #if 0 try { if ( lpPhone == NULL || lpPassword == NULL || pObj == NULL) return FALSE; CString strSql = _T(""); strSql.Format(_T("select csr_id,csr_name,csr_phone,csr_gender,csr_password,csr_old_phone,csr_vcode,vcode_expiry_time,enable from customer where csr_phone = %s"), lpPhone);//用于是否转相片; pObj->pCommand->CommandText = _bstr_t(strSql); // 执行SQL语句,返回记录集 _RecordsetPtr rst = pObj->pCommand->Execute(NULL, NULL, adCmdText); tb_customer &customer = *(tb_customer*)lpOutValue; if(!rst->adoEOF) { customer.set_customer_id(_bstr_t(rst->GetCollect(_T("csr_id")).bstrVal)); customer.set_customer_name(_bstr_t(rst->GetCollect(_T("csr_name")).bstrVal)); customer.set_customer_gender(rst->GetCollect(_T("csr_gender")).boolVal == VARIANT_TRUE ? TRUE : FALSE); customer.set_customer_password(_bstr_t(rst->GetCollect(_T("csr_password")).bstrVal)); customer.set_customer_phone(_bstr_t(rst->GetCollect(_T("csr_phone")).bstrVal)); customer.set_customer_old_phone(_bstr_t(rst->GetCollect(_T("csr_old_phone")).bstrVal)); customer.set_customer_vcode(_bstr_t(rst->GetCollect(_T("csr_vcode")).bstrVal)); _variant_t var = rst->GetCollect(_T("vcode_expiry_time")); if (var.vt == VT_EMPTY || var.vt == VT_NULL) customer.set_customer_vcode_expiry_time(_T("")); else { COleDateTime dt(var); customer.set_customer_vcode_expiry_time(dt.Format(_T("%Y-%m-%d %H:%M:%S"))); } customer.set_customer_enable(rst->GetCollect(_T("enable")).boolVal == VARIANT_TRUE ? TRUE : FALSE); } rst->Close(); } catch (_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); SolveDBError(e.Error(), pObj); WriteTextLog(_T("SQL出错:%08lx,%s,%s,%s"), e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription); return FALSE; } #else // 参数化查询; try { if (lpPhone == NULL || lpPassword == NULL || pObj == NULL) return FALSE; _ParameterPtr QueryParam; QueryParam.CreateInstance(__uuidof(Parameter)); QueryParam = pObj->pCommand->CreateParameter(_bstr_t(_T("csr_phone")), adBSTR, adParamInput, _tclen(lpPhone), lpPhone); pObj->pCommand->Parameters->Append(QueryParam); pObj->pCommand->CommandText = _bstr_t(_T("select csr_id,csr_name,csr_phone,csr_gender,csr_password,csr_old_phone,csr_vcode,vcode_expiry_time,enable from customer where csr_phone = ?")); // 执行SQL语句,返回记录集 _RecordsetPtr rst = pObj->pCommand->Execute(NULL, NULL, adCmdText); tb_customer &customer = *(tb_customer*)lpOutValue; if (!rst->adoEOF) { customer.set_customer_id(_bstr_t(rst->GetCollect(_T("csr_id")).bstrVal)); customer.set_customer_name(_bstr_t(rst->GetCollect(_T("csr_name")).bstrVal)); customer.set_customer_gender(rst->GetCollect(_T("csr_gender")).boolVal == VARIANT_TRUE ? TRUE : FALSE); customer.set_customer_password(_bstr_t(rst->GetCollect(_T("csr_password")).bstrVal)); customer.set_customer_phone(_bstr_t(rst->GetCollect(_T("csr_phone")).bstrVal)); customer.set_customer_old_phone(_bstr_t(rst->GetCollect(_T("csr_old_phone")).bstrVal)); customer.set_customer_vcode(_bstr_t(rst->GetCollect(_T("csr_vcode")).bstrVal)); _variant_t var = rst->GetCollect(_T("vcode_expiry_time")); if (var.vt == VT_EMPTY || var.vt == VT_NULL) customer.set_customer_vcode_expiry_time(_T("")); else { COleDateTime dt(var); customer.set_customer_vcode_expiry_time(dt.Format(_T("%Y-%m-%d %H:%M:%S"))); } customer.set_customer_enable(rst->GetCollect(_T("enable")).boolVal == VARIANT_TRUE ? TRUE : FALSE); } rst->Close(); pObj->pCommand->Parameters->Release(); } catch (_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); SolveDBError(e.Error(), pObj); WriteTextLog(_T("SQL出错:%08lx,%s,%s,%s"), e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription); return FALSE; } #endif return TRUE; } INT CWxAdoImpl::QueryUserDetail(IN LPCTSTR lpPhone, IN LPVOID lpOutValue, IN const DWORD & dwTimeOut) { GETDBPTR(dwTimeOut); #if 0 try { if (lpPhone == NULL || pObj == NULL) return FALSE; CString strSql = _T(""); strSql.Format(_T("select csr_id,csr_name,csr_phone,csr_gender,csr_password,csr_old_phone,csr_vcode,vcode_expiry_time,enable from customer where csr_phone = %s"), lpPhone);//用于是否转相片; pObj->pCommand->CommandText = _bstr_t(strSql); // 执行SQL语句,返回记录集 _RecordsetPtr rst = pObj->pCommand->Execute(NULL, NULL, adCmdText); tb_customer &customer = *(tb_customer*)lpOutValue; if (!rst->adoEOF) { customer.set_customer_id(_bstr_t(rst->GetCollect(_T("csr_id")).bstrVal)); customer.set_customer_name(_bstr_t(rst->GetCollect(_T("csr_name")).bstrVal)); customer.set_customer_gender(rst->GetCollect(_T("csr_gender")).boolVal == VARIANT_TRUE ? TRUE : FALSE); customer.set_customer_password(_bstr_t(rst->GetCollect(_T("csr_password")).bstrVal)); customer.set_customer_phone(_bstr_t(rst->GetCollect(_T("csr_phone")).bstrVal)); customer.set_customer_old_phone(_bstr_t(rst->GetCollect(_T("csr_old_phone")).bstrVal)); customer.set_customer_vcode(_bstr_t(rst->GetCollect(_T("csr_vcode")).bstrVal)); _variant_t var = rst->GetCollect(_T("vcode_expiry_time")); if (var.vt == VT_EMPTY || var.vt == VT_NULL) customer.set_customer_vcode_expiry_time(_T("")); else { COleDateTime dt(var); customer.set_customer_vcode_expiry_time(dt.Format(_T("%Y-%m-%d %H:%M:%S"))); } customer.set_customer_enable(rst->GetCollect(_T("enable")).boolVal == VARIANT_TRUE ? TRUE : FALSE); } rst->Close(); } catch (_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); SolveDBError(e.Error(), pObj); WriteTextLog(_T("SQL出错:%08lx,%s,%s,%s"), e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription); return FALSE; } #else // 参数化查询; try { if (lpPhone == NULL || pObj == NULL) return FALSE; _ParameterPtr QueryParam; QueryParam.CreateInstance(__uuidof(Parameter)); QueryParam = pObj->pCommand->CreateParameter(_bstr_t(_T("csr_phone")), adBSTR, adParamInput, _tclen(lpPhone), lpPhone); pObj->pCommand->Parameters->Append(QueryParam); pObj->pCommand->CommandText = _bstr_t(_T("select csr_id,csr_name,csr_phone,csr_gender,csr_password,csr_old_phone,csr_vcode,vcode_expiry_time,enable from customer where csr_phone = ?")); // 执行SQL语句,返回记录集 _RecordsetPtr rst = pObj->pCommand->Execute(NULL, NULL, adCmdText); tb_customer &customer = *(tb_customer*)lpOutValue; if (!rst->adoEOF) { customer.set_customer_id(_bstr_t(rst->GetCollect(_T("csr_id")).bstrVal)); customer.set_customer_name(_bstr_t(rst->GetCollect(_T("csr_name")).bstrVal)); customer.set_customer_gender(rst->GetCollect(_T("csr_gender")).boolVal == VARIANT_TRUE ? TRUE : FALSE); customer.set_customer_password(_bstr_t(rst->GetCollect(_T("csr_password")).bstrVal)); customer.set_customer_phone(_bstr_t(rst->GetCollect(_T("csr_phone")).bstrVal)); customer.set_customer_old_phone(_bstr_t(rst->GetCollect(_T("csr_old_phone")).bstrVal)); customer.set_customer_vcode(_bstr_t(rst->GetCollect(_T("csr_vcode")).bstrVal)); _variant_t var = rst->GetCollect(_T("vcode_expiry_time")); if (var.vt == VT_EMPTY || var.vt == VT_NULL) customer.set_customer_vcode_expiry_time(_T("")); else { COleDateTime dt(var); customer.set_customer_vcode_expiry_time(dt.Format(_T("%Y-%m-%d %H:%M:%S"))); } customer.set_customer_enable(rst->GetCollect(_T("enable")).boolVal == VARIANT_TRUE ? TRUE : FALSE); } rst->Close(); pObj->pCommand->Parameters->Release(); } catch (_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); SolveDBError(e.Error(), pObj); WriteTextLog(_T("SQL出错:%08lx,%s,%s,%s"), e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription); return FALSE; } #endif return TRUE; } BOOL CWxAdoImpl::AddCustomer(IN LPVOID lpCustomerInfo, IN const DWORD & dwTimeOut) { if (lpCustomerInfo == NULL) return FALSE; GETDBPTR(dwTimeOut); tb_customer &customer = *(tb_customer*)lpCustomerInfo; try { #if 1 CString strSql = _T(""); strSql.Format(_T("INSERT INTO customer(csr_id,csr_name,csr_phone,csr_gender,csr_password,csr_old_phone,csr_vcode,vcode_expiry_time,enable) " "VALUES('%s','%s','%s',%d,'%s','%s','%s','%s',%d) "), //customer.customer_id().c_str(), //[&]()->TCHAR* { // // 如果存在, 或者异常,新建spid; // TCHAR szUid[50] = { 0 }; // _ui64tot_s(m_uniqueid.get_unique_id(), szUid, 50, 10); // return szUid; //}(), [&]()->TString { // 如果存在, 或者异常,新建spid; TCHAR szUid[50] = { 0 }; _ui64tot_s(m_uniqueid.get_unique_id(), szUid, 50, 10); return TString(szUid); }().c_str(), customer.customer_name().c_str(), customer.customer_phone().c_str(), customer.customer_gender(), customer.customer_password().c_str(), customer.customer_old_phone().c_str(), customer.customer_vcode().c_str(), customer.customer_vcode_expiry_time().c_str(), customer.customer_enable() ); pObj->pCommand->CommandText = _bstr_t(strSql); // 执行SQL语句,返回记录集 _RecordsetPtr rst = pObj->pCommand->Execute(NULL, NULL, adCmdText); if (!rst->adoEOF) { ; } rst->Close(); #else #endif } catch (_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); SolveDBError(e.Error(), pObj); WriteTextLog(_T("SQL出错:%08lx,%s,%s,%s"), e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription); return FALSE; } return TRUE; } BOOL CWxAdoImpl::AddType(IN LPVOID lpStudioInfo, IN const DWORD & dwTimeOut) { return 0; } BOOL CWxAdoImpl::AddReply(IN LPVOID lpStudioInfo, IN const DWORD & dwTimeOut) { return 0; } BOOL CWxAdoImpl::UpdateCustomer(IN LPVOID lpCustomerInfo, IN const DWORD & dwTimeOut) { return 0; }