123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- #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<TCHAR*>& vValues //返回值集合
- IN LPCTSTR lpTabName, //表名
- IN std::vector<TCHAR*>& 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<CStringArray,CStringArray> &AryOfValues //返回值集合
- IN LPCTSTR lpTabName, //表名
- IN std::vector<CString>& vField, //字段集
- IN LPCTSTR lpFilter = NULL //
- 返回: >1成功,0失败
- */
- /************************************************************************/
- int DataBaseOpt::Select(OUT CArray<CStringArray,CStringArray>& AryOfValues, IN LPCTSTR lpTabName, IN std::vector<CString>& 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<vFields.size(); i++)
- {
- strSQL += vFields[i];
- strSQL += _T(",");
- }
- strSQL.TrimRight(_T(","));
- strSQL += _T(" from ");
- strSQL += lpTabName;
- if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
- {
- strSQL += _T(" where ");
- strSQL += lpFilter;
- }
- // 关键字要去除[]后才能取值;
- for ( int i = 0; i < vFields.size(); i++)
- {
- vFields[i].TrimLeft(_T('['));
- vFields[i].TrimRight(_T(']'));
- }
- DWORD dwIndex = 0;
- AryOfValues.SetSize(dwCount);
- CRecordset tagRet(m_pdb);
- tagRet.Open(CRecordset::forwardOnly,strSQL);
- try
- {
- while(!tagRet.IsEOF())
- {
- for ( int i = 0; i < vFields.size(); i++)
- {
- CString str = _T("");
- CString strKey = vFields[i];
- tagRet.GetFieldValue(strKey,str);
- WriteLogin(str);
- AryOfValues.ElementAt(dwIndex).Add(str);
- }
- dwIndex++;
- if ( dwIndex >= 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;
- }
|