DataBaseOpt.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "stdafx.h"
  2. #include "DataBaseOpt.h"
  3. #include "Global.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. DataBaseOpt::DataBaseOpt()
  10. {
  11. m_bOpen = FALSE;
  12. m_pdb = NULL;
  13. memset(m_szConnect, 0, sizeof(TCHAR)*MAX_PATH);
  14. }
  15. DataBaseOpt::~DataBaseOpt()
  16. {
  17. CloseDB();
  18. }
  19. BOOL DataBaseOpt::IsOpenDB()
  20. {
  21. BOOL bOpen = FALSE;
  22. m_lock.lock();
  23. bOpen = m_bOpen;
  24. m_lock.unlock();
  25. return bOpen;
  26. }
  27. /************************************************************************/
  28. /*
  29. 函数: OpenDB
  30. 描述: 打开数据库
  31. 参数:
  32. LPCTSTR lpConnect 路径
  33. 返回: 1成功,0失败
  34. */
  35. /************************************************************************/
  36. int DataBaseOpt::OpenDB(LPCTSTR lpConnect)
  37. {
  38. #ifdef _UNICODE
  39. if(lpConnect == NULL || wcscmp(lpConnect, _T("")) == 0)
  40. return 0;
  41. wcscpy(m_szConnect, lpConnect);
  42. #else
  43. if(lpConnect == NULL || strcmp(lpConnect, _T("")) == 0)
  44. return 0;
  45. strcpy(m_szConnect, lpConnect);
  46. #endif
  47. m_lock.lock();
  48. if(m_pdb)
  49. {
  50. m_bOpen = TRUE;
  51. m_lock.unlock();
  52. return 1;
  53. }
  54. #ifdef _UNICODE
  55. wcscpy(m_szConnect, lpConnect);
  56. #else
  57. strcpy(m_szConnect, lpConnect);
  58. #endif //#ifdef _UNICODE
  59. if (m_pdb == NULL)
  60. {
  61. try
  62. {
  63. m_pdb = new CDatabase;
  64. m_pdb->OpenEx(m_szConnect, CDatabase::noOdbcDialog);
  65. m_bOpen = TRUE;
  66. }
  67. catch (CDBException* e)
  68. {
  69. //m_strConnectErrorDescriptor = e->m_strError;
  70. m_lock.unlock();
  71. delete m_pdb;
  72. m_pdb = NULL;
  73. m_bOpen = FALSE;
  74. //OutputDebugString(e->m_strError);
  75. //LOG4C_NO_FILENUM((LOG_NOTICE,"打开数据库失败:%s",CW2A(e->m_strError)));
  76. #ifdef _DEBUG
  77. e->ReportError();
  78. #endif //#ifdef _DEBUG
  79. e->Delete();
  80. return FALSE;
  81. }
  82. }
  83. m_lock.unlock();
  84. return 1;
  85. }
  86. void DataBaseOpt::CloseDB()
  87. {
  88. m_lock.lock();
  89. m_bOpen = FALSE;
  90. if(m_pdb)
  91. delete m_pdb;
  92. m_pdb = NULL;
  93. m_lock.unlock();
  94. }
  95. /************************************************************************/
  96. /*
  97. 函数: SelectCount
  98. 描述: 查询记录数量
  99. 参数:
  100. OUT std:vector<TCHAR*>& vValues //返回值集合
  101. IN LPCTSTR lpTabName, //表名
  102. IN std::vector<TCHAR*>& vField, //字段集
  103. IN LPCTSTR lpFilter = NULL //
  104. 返回: >1查询后的数量,0失败
  105. */
  106. /************************************************************************/
  107. DWORD DataBaseOpt::SelectCount(IN LPCTSTR lpTabName, IN LPCTSTR lpFilter)
  108. {
  109. DWORD nCount = 0;
  110. m_lock.lock();
  111. if(m_pdb == NULL)
  112. {
  113. m_lock.unlock();
  114. return 0;
  115. }
  116. CString strSQL = _T("");
  117. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  118. strSQL.Format(_T("select count(*) as cot from %s where %s"), lpTabName, lpFilter);
  119. else
  120. strSQL.Format(_T("select count(*) as cot from %s"), lpTabName);
  121. try
  122. {
  123. CRecordset tagRecordset(m_pdb);
  124. tagRecordset.Open(CRecordset::forwardOnly, strSQL);
  125. tagRecordset.GetFieldValue(_T("cot"), strSQL);
  126. tagRecordset.Close();
  127. nCount = _ttoi(strSQL);
  128. }
  129. catch (CDBException *e)
  130. {
  131. m_lock.unlock();
  132. WriteLogin(e->m_strError);
  133. #ifdef _DEBUG
  134. e->ReportError();
  135. #endif //#ifdef _DEBUG
  136. e->Delete();
  137. return 0;
  138. }
  139. m_lock.unlock();
  140. return nCount;
  141. }
  142. /************************************************************************/
  143. /*
  144. 函数: Select
  145. 描述: 查询
  146. 参数:
  147. OUT CArray<CStringArray,CStringArray> &AryOfValues //返回值集合
  148. IN LPCTSTR lpTabName, //表名
  149. IN std::vector<CString>& vField, //字段集
  150. IN LPCTSTR lpFilter = NULL //
  151. 返回: >1成功,0失败
  152. */
  153. /************************************************************************/
  154. int DataBaseOpt::Select(OUT CArray<CStringArray,CStringArray>& AryOfValues, IN LPCTSTR lpTabName, IN std::vector<CString>& vFields, IN LPCTSTR lpFilter)
  155. {
  156. DWORD dwCount = SelectCount(lpTabName, lpFilter);
  157. m_lock.lock();
  158. if(m_pdb == NULL)
  159. {
  160. m_lock.unlock();
  161. return 0;
  162. }
  163. CString strSQL = _T("select ");
  164. for(int i = 0; i<vFields.size(); i++)
  165. {
  166. strSQL += vFields[i];
  167. strSQL += _T(",");
  168. }
  169. strSQL.TrimRight(_T(","));
  170. strSQL += _T(" from ");
  171. strSQL += lpTabName;
  172. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  173. {
  174. strSQL += _T(" where ");
  175. strSQL += lpFilter;
  176. }
  177. // 关键字要去除[]后才能取值;
  178. for ( int i = 0; i < vFields.size(); i++)
  179. {
  180. vFields[i].TrimLeft(_T('['));
  181. vFields[i].TrimRight(_T(']'));
  182. }
  183. DWORD dwIndex = 0;
  184. AryOfValues.SetSize(dwCount);
  185. CRecordset tagRet(m_pdb);
  186. tagRet.Open(CRecordset::forwardOnly,strSQL);
  187. try
  188. {
  189. while(!tagRet.IsEOF())
  190. {
  191. for ( int i = 0; i < vFields.size(); i++)
  192. {
  193. CString str = _T("");
  194. CString strKey = vFields[i];
  195. tagRet.GetFieldValue(strKey,str);
  196. WriteLogin(str);
  197. AryOfValues.ElementAt(dwIndex).Add(str);
  198. }
  199. dwIndex++;
  200. if ( dwIndex >= dwCount ) break;
  201. tagRet.MoveNext();
  202. }
  203. }
  204. catch (CDBException *e)
  205. {
  206. m_lock.unlock();
  207. WriteLogin(e->m_strError);
  208. #ifdef _DEBUG
  209. e->ReportError();
  210. #endif //#ifdef _DEBUG
  211. e->Delete();
  212. return 0;
  213. }
  214. m_lock.unlock();
  215. return 1;
  216. }
  217. /************************************************************************/
  218. /*
  219. 函数:Execute
  220. 描述:执行指定的SQL语句;
  221. 参数:
  222. IN: lpSQL 要执行SQL语句;
  223. 返回:成功查询返回TRUE,否则返回FALSE;
  224. */
  225. /************************************************************************/
  226. BOOL DataBaseOpt::Execute(IN LPCTSTR lpSQL)
  227. {
  228. try
  229. {
  230. m_lock.lock();
  231. if (!m_bOpen)
  232. {
  233. m_lock.unlock();
  234. return 0;
  235. }
  236. m_pdb->ExecuteSQL(lpSQL);
  237. m_lock.unlock();
  238. }
  239. catch (CDBException* e)
  240. {
  241. m_lock.unlock();
  242. WriteLogin(e->m_strError);
  243. #ifdef _DEBUG
  244. e->ReportError();
  245. #endif
  246. e->Delete();
  247. return 0;
  248. }
  249. return 1;
  250. }