#include "stdafx.h" #include "DataBaseOpt.h" #include "Global.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif DataBaseOpt::DataBaseOpt() { m_bOpen = FALSE; m_pdb = NULL; memset(m_szConnect, 0, sizeof(TCHAR)*MAX_PATH); } DataBaseOpt::~DataBaseOpt() { CloseDB(); } BOOL DataBaseOpt::IsOpenDB() { BOOL bOpen = FALSE; m_lock.lock(); bOpen = m_bOpen; m_lock.unlock(); return bOpen; } /************************************************************************/ /* 函数: OpenDB 描述: 打开数据库 参数: LPCTSTR lpConnect 路径 返回: 1成功,0失败 */ /************************************************************************/ int DataBaseOpt::OpenDB(LPCTSTR lpConnect) { #ifdef _UNICODE if(lpConnect == NULL || wcscmp(lpConnect, _T("")) == 0) return 0; wcscpy(m_szConnect, lpConnect); #else if(lpConnect == NULL || strcmp(lpConnect, _T("")) == 0) return 0; strcpy(m_szConnect, lpConnect); #endif m_lock.lock(); if(m_pdb) { m_bOpen = TRUE; m_lock.unlock(); return 1; } #ifdef _UNICODE wcscpy(m_szConnect, lpConnect); #else strcpy(m_szConnect, lpConnect); #endif //#ifdef _UNICODE if (m_pdb == NULL) { try { m_pdb = new CDatabase; m_pdb->OpenEx(m_szConnect, CDatabase::noOdbcDialog); m_bOpen = TRUE; } catch (CDBException* e) { //m_strConnectErrorDescriptor = e->m_strError; m_lock.unlock(); delete m_pdb; m_pdb = NULL; m_bOpen = FALSE; //OutputDebugString(e->m_strError); //LOG4C_NO_FILENUM((LOG_NOTICE,"打开数据库失败:%s",CW2A(e->m_strError))); #ifdef _DEBUG e->ReportError(); #endif //#ifdef _DEBUG e->Delete(); return FALSE; } } m_lock.unlock(); return 1; } void DataBaseOpt::CloseDB() { m_lock.lock(); m_bOpen = FALSE; if(m_pdb) delete m_pdb; m_pdb = NULL; m_lock.unlock(); } /************************************************************************/ /* 函数: SelectCount 描述: 查询记录数量 参数: OUT std:vector& vValues //返回值集合 IN LPCTSTR lpTabName, //表名 IN std::vector& vField, //字段集 IN LPCTSTR lpFilter = NULL // 返回: >1查询后的数量,0失败 */ /************************************************************************/ DWORD DataBaseOpt::SelectCount(IN LPCTSTR lpTabName, IN LPCTSTR lpFilter) { DWORD nCount = 0; m_lock.lock(); if(m_pdb == NULL) { m_lock.unlock(); return 0; } CString strSQL = _T(""); if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0) strSQL.Format(_T("select count(*) as cot from %s where %s"), lpTabName, lpFilter); else strSQL.Format(_T("select count(*) as cot from %s"), lpTabName); try { CRecordset tagRecordset(m_pdb); tagRecordset.Open(CRecordset::forwardOnly, strSQL); tagRecordset.GetFieldValue(_T("cot"), strSQL); tagRecordset.Close(); nCount = _ttoi(strSQL); } catch (CDBException *e) { m_lock.unlock(); WriteLogin(e->m_strError); #ifdef _DEBUG e->ReportError(); #endif //#ifdef _DEBUG e->Delete(); return 0; } m_lock.unlock(); return nCount; } /************************************************************************/ /* 函数: Select 描述: 查询 参数: OUT CArray &AryOfValues //返回值集合 IN LPCTSTR lpTabName, //表名 IN std::vector& vField, //字段集 IN LPCTSTR lpFilter = NULL // 返回: >1成功,0失败 */ /************************************************************************/ int DataBaseOpt::Select(OUT CArray& AryOfValues, IN LPCTSTR lpTabName, IN std::vector& vFields, IN LPCTSTR lpFilter) { DWORD dwCount = SelectCount(lpTabName, lpFilter); m_lock.lock(); if(m_pdb == NULL) { m_lock.unlock(); return 0; } CString strSQL = _T("select "); for(int i = 0; i= dwCount ) break; tagRet.MoveNext(); } } catch (CDBException *e) { m_lock.unlock(); WriteLogin(e->m_strError); #ifdef _DEBUG e->ReportError(); #endif //#ifdef _DEBUG e->Delete(); return 0; } m_lock.unlock(); return 1; } /************************************************************************/ /* 函数:Execute 描述:执行指定的SQL语句; 参数: IN: lpSQL 要执行SQL语句; 返回:成功查询返回TRUE,否则返回FALSE; */ /************************************************************************/ BOOL DataBaseOpt::Execute(IN LPCTSTR lpSQL) { try { m_lock.lock(); if (!m_bOpen) { m_lock.unlock(); return 0; } m_pdb->ExecuteSQL(lpSQL); m_lock.unlock(); } catch (CDBException* e) { m_lock.unlock(); WriteLogin(e->m_strError); #ifdef _DEBUG e->ReportError(); #endif e->Delete(); return 0; } return 1; }