CatalogObj.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #include "StdAfx.h"
  2. #include "CatalogObj.h"
  3. //#include "SQLStatementImpl.h"
  4. //////////////////////////////////////////////////////////////////////////
  5. BOOL CCatalogObj::m_bEndofThread = FALSE;
  6. CCatalogObj::CCatalogObj(void)
  7. {
  8. m_bOpen = FALSE;
  9. m_bSolve = FALSE;
  10. m_pdbInstance = NULL;
  11. m_hReConnectEvent = NULL;
  12. m_hReConnectThread = NULL;
  13. m_hWaitableTimer = NULL;
  14. memset(m_szConnectString,0,sizeof(TCHAR)*MAX_PATH);
  15. InitializeCriticalSection(&m_CriticalSection);
  16. }
  17. CCatalogObj::~CCatalogObj(void)
  18. {
  19. CloseCatalog();
  20. EndofThread();
  21. DeleteCriticalSection(&m_CriticalSection);
  22. }
  23. /************************************************************************/
  24. /*
  25. 函数:SolveDBError
  26. 描述:处理数据库实例操作过程中出现的错误,当前只处理:命名管道连接错误,网络连接错误,网络中断错误;
  27. 参数:
  28. IN: dwError 操作SQL数据库实例中产生的错误码;
  29. IN: lpErrorString 错误码对应的描述;
  30. 返回:已处理中返回TRUE,否则返回FALSE;
  31. 要求:
  32. 注意:
  33. */
  34. /************************************************************************/
  35. BOOL CCatalogObj::SolveDBError(IN CONST DWORD &dwError, IN LPCTSTR lpErrorString)
  36. {
  37. EnterCriticalSection(&m_CriticalSection);
  38. if(m_bSolve)
  39. {
  40. LeaveCriticalSection(&m_CriticalSection);
  41. return TRUE;
  42. }
  43. LeaveCriticalSection(&m_CriticalSection);
  44. switch(dwError)
  45. {
  46. case ERROR_PIPE_NOT_CONNECTED:
  47. case WSAECONNRESET:
  48. case WSAECONNABORTED:
  49. {
  50. EnterCriticalSection(&m_CriticalSection);
  51. m_bSolve = TRUE;
  52. LeaveCriticalSection(&m_CriticalSection);
  53. m_strConnectErrorDescriptor = lpErrorString;
  54. // 启用重连线程;
  55. CloseCatalog();
  56. StartThread();
  57. // LOG4C((LOG_NOTICE,"数据库连接失败,请检查配置信息或数据库服务是否启动!"));
  58. }
  59. break;
  60. default:
  61. // 其他错误,输出日志;
  62. // LOG4C((LOG_NOTICE,"数据库错误信息:%s",CW2A(lpErrorString)));
  63. break;
  64. }
  65. return FALSE;
  66. }
  67. /************************************************************************/
  68. /*
  69. 函数:StartThread
  70. 描述:启动重连线程;
  71. 参数:
  72. 返回:
  73. 要求:
  74. 注意:
  75. */
  76. /************************************************************************/
  77. int CCatalogObj::StartThread()
  78. {
  79. #if JEFF_TEST_OFF
  80. // 计时器;
  81. SYSTEMTIME st;
  82. FILETIME ftLocal, ftUTC;
  83. LARGE_INTEGER liUTC;
  84. st.wYear = 2014; // Year
  85. st.wMonth = 12; // January
  86. st.wDayOfWeek = 0; // Ignored
  87. st.wDay = 1; // The first of the month
  88. st.wHour = 13; // 1PM
  89. st.wMinute = 0; // 0 minutes into the hour
  90. st.wSecond = 0; // 0 seconds into the minute
  91. st.wMilliseconds = 0; // 0 milliseconds into the second
  92. SystemTimeToFileTime(&st, &ftLocal);
  93. // Convert local time to UTC time.
  94. LocalFileTimeToFileTime(&ftLocal, &ftUTC);
  95. // Convert FILETIME to LARGE_INTEGER because of different alignment.
  96. liUTC.LowPart = ftUTC.dwLowDateTime;
  97. liUTC.HighPart = ftUTC.dwHighDateTime;
  98. //liUTC.QuadPart = -(2*10000000);
  99. m_bEndofThread = FALSE;
  100. m_hWaitableTimer = CreateWaitableTimer(NULL,FALSE,NULL);
  101. const int nTimerUnitsPerSecond = 10000000;
  102. SetWaitableTimer(m_hWaitableTimer,&liUTC,3*60*60*1000,NULL,NULL,FALSE); // 3小时执行一次;
  103. #else
  104. if(m_hReConnectEvent == NULL)
  105. m_hReConnectEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // 无信号事件;
  106. if (m_hReConnectEvent == NULL)
  107. {
  108. m_bSolve = FALSE;
  109. return -1;
  110. }
  111. if(m_hReConnectThread == NULL)
  112. m_hReConnectThread = CreateThread(NULL, 0, ReConnectDatabaseThread, this, 0, NULL);
  113. if (m_hReConnectThread == NULL)
  114. {
  115. m_bSolve = FALSE;
  116. return -1;
  117. }
  118. else
  119. {
  120. CloseHandle(m_hReConnectThread);
  121. m_hReConnectThread = NULL;
  122. }
  123. #endif
  124. return 0;
  125. }
  126. /************************************************************************/
  127. /*
  128. 函数:EndofThread
  129. 描述:结束重连线程;
  130. 参数:
  131. 返回:
  132. 要求:
  133. 注意:
  134. */
  135. /************************************************************************/
  136. int CCatalogObj::EndofThread()
  137. {
  138. #if JEFF_TEST_OFF
  139. // 重置等待时间,立即返回;
  140. LARGE_INTEGER li;
  141. const int nTimerUnitsPerSecond = 10000000;
  142. li.QuadPart = 1*nTimerUnitsPerSecond;
  143. SetWaitableTimer(m_hWaitableTimer,&li,NULL,NULL,NULL,FALSE);
  144. m_bEndofThread = TRUE;
  145. #else
  146. if (m_hReConnectEvent)
  147. SetEvent(m_hReConnectEvent);
  148. if (m_hReConnectThread)
  149. {
  150. if (WaitForSingleObject(m_hReConnectThread, INFINITE) != WAIT_TIMEOUT)
  151. CloseHandle(m_hReConnectThread);
  152. m_hReConnectThread = NULL;
  153. }
  154. if (m_hReConnectEvent)
  155. CloseHandle(m_hReConnectEvent);
  156. m_hReConnectEvent = NULL;
  157. #endif
  158. return 0L;
  159. }
  160. /************************************************************************/
  161. /*
  162. 函数:ReConnectDatabaseThread
  163. 描述:重连数据库实例连接的线程;
  164. 参数:
  165. IN: lpPara CCatalogObj自身;
  166. 返回:
  167. 要求:
  168. 注意:
  169. */
  170. /************************************************************************/
  171. DWORD CCatalogObj::ReConnectDatabaseThread(IN LPVOID lpPara)
  172. {
  173. CCatalogObj *pInstance = (CCatalogObj*)lpPara;
  174. #if JEFF_TEST_OFF
  175. while ( !m_bEndofThread )
  176. {
  177. WaitForSingleObject(pInstance->m_hWaitableTimer,INFINITE);
  178. if ( !m_bEndofThread)
  179. {
  180. if( pInstance->OpenDatabase())
  181. {
  182. pInstance->m_bSolve = FALSE;
  183. break;
  184. }
  185. }
  186. }
  187. if (pInstance->m_hReConnectEvent)
  188. CloseHandle(pInstance->m_hReConnectEvent);
  189. pInstance->m_hReConnectEvent = NULL;
  190. #else
  191. do
  192. {
  193. if( pInstance->OpenCatalog())
  194. {
  195. pInstance->m_bSolve = FALSE;
  196. break;
  197. }
  198. } while (WaitForSingleObject(pInstance->m_hReConnectEvent,5000L) == WAIT_TIMEOUT);
  199. if (pInstance->m_hReConnectEvent)
  200. CloseHandle(pInstance->m_hReConnectEvent);
  201. pInstance->m_hReConnectEvent = NULL;
  202. #endif
  203. return 0L;
  204. }
  205. /************************************************************************/
  206. /*
  207. 函数:OpenCatalog
  208. 描述:创建并打开数据库连接对象实例;
  209. 参数:
  210. IN: lpSQLConnectString 连接数据库实例的连接串;
  211. 返回:连接数据库实例成功返回TRUE,否则返回FALSE;
  212. 要求:
  213. 注意:
  214. */
  215. /************************************************************************/
  216. BOOL CCatalogObj::OpenCatalog(LPCTSTR lpSQLConnectString)
  217. {
  218. EnterCriticalSection(&m_CriticalSection);
  219. if(m_bOpen)
  220. {
  221. LeaveCriticalSection(&m_CriticalSection);
  222. return TRUE;
  223. }
  224. if (lpSQLConnectString == NULL || _tcscmp(lpSQLConnectString,_T("")) == 0)
  225. {
  226. if (_tcscmp(m_szConnectString, _T("")) == 0)
  227. {
  228. LeaveCriticalSection(&m_CriticalSection);
  229. OutputDebugString(_T("数据库连接串为空!\n"));
  230. return FALSE;
  231. }
  232. }
  233. else
  234. {
  235. _stprintf_s(m_szConnectString,MAX_PATH,_T("%s"),lpSQLConnectString);
  236. }
  237. if (m_pdbInstance == NULL)
  238. {
  239. try
  240. {
  241. m_pdbInstance = new CDatabase;
  242. m_pdbInstance->OpenEx(m_szConnectString, CDatabase::noOdbcDialog);
  243. }
  244. catch (CDBException* e)
  245. {
  246. //m_strConnectErrorDescriptor = e->m_strError;
  247. LeaveCriticalSection(&m_CriticalSection);
  248. delete m_pdbInstance;
  249. m_pdbInstance = NULL;
  250. OutputDebugString(e->m_strError);
  251. e->Delete();
  252. return FALSE;
  253. }
  254. m_bOpen = TRUE;
  255. }
  256. LeaveCriticalSection(&m_CriticalSection);
  257. return m_bOpen;
  258. }
  259. /************************************************************************/
  260. /*
  261. 函数:CloseCatalog
  262. 描述:关闭数据库连接对象实例;
  263. 参数:
  264. 返回:
  265. 要求:
  266. 注意:
  267. */
  268. /************************************************************************/
  269. void CCatalogObj::CloseCatalog()
  270. {
  271. EnterCriticalSection(&m_CriticalSection);
  272. //if (!m_bOpen) return;
  273. m_bOpen = FALSE;
  274. if (m_pdbInstance)
  275. delete m_pdbInstance;
  276. m_pdbInstance = NULL;
  277. LeaveCriticalSection(&m_CriticalSection);
  278. }
  279. /************************************************************************/
  280. /*
  281. 函数:Execute
  282. 描述:执行指定的SQL语句;
  283. 参数:
  284. IN: lpSQL 要执行SQL语句;
  285. 返回:成功查询返回TRUE,否则返回FALSE;
  286. 要求:
  287. 注意:
  288. */
  289. /************************************************************************/
  290. BOOL CCatalogObj::Execute(IN LPCTSTR lpSQL)
  291. {
  292. try
  293. {
  294. EnterCriticalSection(&m_CriticalSection);
  295. if (!m_bOpen)
  296. {
  297. LeaveCriticalSection(&m_CriticalSection);
  298. return FALSE;
  299. }
  300. //m_pdbInstance->BeginTrans();
  301. m_pdbInstance->ExecuteSQL(lpSQL);
  302. //m_pdbInstance->CommitTrans();
  303. LeaveCriticalSection(&m_CriticalSection);
  304. }
  305. catch (CDBException* e)
  306. {
  307. //m_pdbInstance->Rollback();
  308. LeaveCriticalSection(&m_CriticalSection);
  309. SolveDBError(GetLastError(),e->m_strError);
  310. OutputDebugString(e->m_strError);
  311. e->Delete();
  312. return FALSE;
  313. }
  314. return TRUE;
  315. }
  316. /************************************************************************/
  317. /*
  318. 函数:GetSelectCount
  319. 描述:获取指定表的查询记录数;
  320. 参数:
  321. IN: lpTableName 要查询的表;
  322. IN: lpFilter 查询条件;
  323. 返回:成功查询返回记录条数,否则返回-1;
  324. 要求:
  325. 注意:
  326. */
  327. /************************************************************************/
  328. int CCatalogObj::GetSelectCount(IN LPCTSTR lpTableName, IN LPCTSTR lpFilter)
  329. {
  330. try
  331. {
  332. EnterCriticalSection(&m_CriticalSection);
  333. if (!m_bOpen)
  334. {
  335. LeaveCriticalSection(&m_CriticalSection);
  336. return -1;
  337. }
  338. CString strSQL;
  339. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  340. strSQL.Format(_T("select count(*) as cot from %s where %s"), lpTableName, lpFilter);
  341. else
  342. strSQL.Format(_T("select count(*) as cot from %s"), lpTableName);
  343. CRecordset tagRecordset(m_pdbInstance);
  344. tagRecordset.Open(CRecordset::forwardOnly, strSQL);
  345. tagRecordset.GetFieldValue(_T("cot"), strSQL);
  346. tagRecordset.Close();
  347. LeaveCriticalSection(&m_CriticalSection);
  348. return _ttoi(strSQL);
  349. }
  350. catch (CDBException* e)
  351. {
  352. LeaveCriticalSection(&m_CriticalSection);
  353. SolveDBError(GetLastError(),e->m_strError);
  354. OutputDebugString(e->m_strError);
  355. e->Delete();
  356. return -1;
  357. }
  358. }
  359. /************************************************************************/
  360. /*
  361. 函数:GetTableValues
  362. 描述:获取查询结果;
  363. 参数:
  364. IN: lpTableName 要查询的表;
  365. IN: lpFilter 查询条件;
  366. IN: AryOfFields 要查询的字段;
  367. OUT: AryOfValues 记录集结果返回;
  368. 返回:查询成功且记录数大于0返回TRUE;
  369. 要求:
  370. 注意:
  371. */
  372. /************************************************************************/
  373. BOOL CCatalogObj::GetTableValues(IN LPCTSTR lpTableName, IN LPCTSTR lpFilter, IN CStringArray &AryOfFields, OUT CArray<CStringArray,CStringArray> &AryOfValues)
  374. {
  375. try
  376. {
  377. EnterCriticalSection(&m_CriticalSection);
  378. if ( !m_bOpen )
  379. {
  380. OutputDebugString(_T("数据库未打开\n"));
  381. LeaveCriticalSection(&m_CriticalSection);
  382. return FALSE;
  383. }
  384. // 获取记录数;
  385. DWORD dwCount = 0;
  386. CString strSQL = _T("");
  387. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  388. strSQL.Format(_T("select count(*) as cot from %s where %s"), lpTableName, lpFilter);
  389. else
  390. strSQL.Format(_T("select count(*) as cot from %s"), lpTableName);
  391. CRecordset tagRet(m_pdbInstance);
  392. tagRet.Open(CRecordset::forwardOnly, strSQL);
  393. tagRet.GetFieldValue(_T("cot"), strSQL);
  394. tagRet.Close();
  395. if( (dwCount = _ttol(strSQL)) == 0 )
  396. {
  397. OutputDebugString(_T("查询无记录\n"));
  398. LeaveCriticalSection(&m_CriticalSection);
  399. return FALSE;
  400. }
  401. // 查询;
  402. strSQL = _T("select ");
  403. for ( int i = 0; i < AryOfFields.GetSize(); i++)
  404. {
  405. strSQL += AryOfFields.ElementAt(i);
  406. strSQL += _T(",");
  407. }
  408. strSQL.TrimRight(_T(","));
  409. strSQL += _T(" from ");
  410. strSQL += lpTableName;
  411. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  412. {
  413. strSQL += _T(" where ");
  414. strSQL += lpFilter;
  415. }
  416. #if 1 // 关键字要去除[]后才能取值;
  417. for ( int i = 0; i < AryOfFields.GetSize(); i++)
  418. {
  419. AryOfFields.ElementAt(i).TrimLeft(_T('['));
  420. AryOfFields.ElementAt(i).TrimRight(_T(']'));
  421. }
  422. #endif
  423. DWORD dwIndex = 0;
  424. AryOfValues.SetSize(dwCount);
  425. tagRet.Open(CRecordset::forwardOnly,strSQL);
  426. while(!tagRet.IsEOF())
  427. {
  428. for ( int i = 0; i < AryOfFields.GetSize(); i++)
  429. {
  430. CString str;
  431. tagRet.GetFieldValue(AryOfFields.ElementAt(i),str);
  432. AryOfValues.ElementAt(dwIndex).Add(str);
  433. }
  434. dwIndex++;
  435. #if 1 // 防止查询过程中有新记录添加导致错误;
  436. if ( dwIndex >= dwCount ) break;
  437. #endif
  438. tagRet.MoveNext();
  439. }
  440. tagRet.Close();
  441. //AryOfValues.FreeExtra();
  442. LeaveCriticalSection(&m_CriticalSection);
  443. }
  444. catch (CDBException *e)
  445. {
  446. LeaveCriticalSection(&m_CriticalSection);
  447. SolveDBError(GetLastError(),e->m_strError);
  448. OutputDebugString(e->m_strError);
  449. e->Delete();
  450. return FALSE;
  451. }
  452. return TRUE;
  453. }