CatalogObj.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. //LOG4C_NO_FILENUM((LOG_NOTICE,"数据库连接串为空"));
  231. return FALSE;
  232. }
  233. }
  234. else
  235. {
  236. _stprintf_s(m_szConnectString,MAX_PATH,_T("%s"),lpSQLConnectString);
  237. }
  238. if (m_pdbInstance == NULL)
  239. {
  240. try
  241. {
  242. m_pdbInstance = new CDatabase;
  243. m_pdbInstance->OpenEx(m_szConnectString, CDatabase::noOdbcDialog);
  244. }
  245. catch (CDBException* e)
  246. {
  247. //m_strConnectErrorDescriptor = e->m_strError;
  248. LeaveCriticalSection(&m_CriticalSection);
  249. delete m_pdbInstance;
  250. m_pdbInstance = NULL;
  251. //OutputDebugString(e->m_strError);
  252. //LOG4C_NO_FILENUM((LOG_NOTICE,"打开数据库失败:%s",CW2A(e->m_strError)));
  253. #ifdef _DEBUG
  254. e->ReportError();
  255. #endif
  256. e->Delete();
  257. return FALSE;
  258. }
  259. m_bOpen = TRUE;
  260. }
  261. LeaveCriticalSection(&m_CriticalSection);
  262. return m_bOpen;
  263. }
  264. /************************************************************************/
  265. /*
  266. 函数:CloseCatalog
  267. 描述:关闭数据库连接对象实例;
  268. 参数:
  269. 返回:
  270. 要求:
  271. 注意:
  272. */
  273. /************************************************************************/
  274. void CCatalogObj::CloseCatalog()
  275. {
  276. EnterCriticalSection(&m_CriticalSection);
  277. //if (!m_bOpen) return;
  278. m_bOpen = FALSE;
  279. if (m_pdbInstance)
  280. delete m_pdbInstance;
  281. m_pdbInstance = NULL;
  282. LeaveCriticalSection(&m_CriticalSection);
  283. }
  284. /************************************************************************/
  285. /*
  286. 函数:Execute
  287. 描述:执行指定的SQL语句;
  288. 参数:
  289. IN: lpSQL 要执行SQL语句;
  290. 返回:成功查询返回TRUE,否则返回FALSE;
  291. 要求:
  292. 注意:
  293. */
  294. /************************************************************************/
  295. BOOL CCatalogObj::Execute(IN LPCTSTR lpSQL)
  296. {
  297. try
  298. {
  299. EnterCriticalSection(&m_CriticalSection);
  300. if (!m_bOpen)
  301. {
  302. LeaveCriticalSection(&m_CriticalSection);
  303. return FALSE;
  304. }
  305. //m_pdbInstance->BeginTrans();
  306. m_pdbInstance->ExecuteSQL(lpSQL);
  307. //m_pdbInstance->CommitTrans();
  308. LeaveCriticalSection(&m_CriticalSection);
  309. }
  310. catch (CDBException* e)
  311. {
  312. //m_pdbInstance->Rollback();
  313. LeaveCriticalSection(&m_CriticalSection);
  314. SolveDBError(GetLastError(),e->m_strError);
  315. WriteTextLog(_T("%s,%s"),e->m_strError,lpSQL);
  316. #ifdef _DEBUG
  317. e->ReportError();
  318. #endif
  319. e->Delete();
  320. return FALSE;
  321. }
  322. return TRUE;
  323. }
  324. /************************************************************************/
  325. /*
  326. 函数:GetSelectCount
  327. 描述:获取指定表的查询记录数;
  328. 参数:
  329. IN: lpTableName 要查询的表;
  330. IN: lpFilter 查询条件;
  331. 返回:成功查询返回记录条数,否则返回-1;
  332. 要求:
  333. 注意:
  334. */
  335. /************************************************************************/
  336. int CCatalogObj::GetSelectCount(IN LPCTSTR lpTableName, IN LPCTSTR lpFilter)
  337. {
  338. try
  339. {
  340. EnterCriticalSection(&m_CriticalSection);
  341. if (!m_bOpen)
  342. {
  343. LeaveCriticalSection(&m_CriticalSection);
  344. return -1;
  345. }
  346. CString strSQL;
  347. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  348. strSQL.Format(_T("select count(*) as cot from %s where %s"), lpTableName, lpFilter);
  349. else
  350. strSQL.Format(_T("select count(*) as cot from %s"), lpTableName);
  351. CRecordset tagRecordset(m_pdbInstance);
  352. tagRecordset.Open(CRecordset::forwardOnly, strSQL);
  353. tagRecordset.GetFieldValue(_T("cot"), strSQL);
  354. tagRecordset.Close();
  355. LeaveCriticalSection(&m_CriticalSection);
  356. return _ttoi(strSQL);
  357. }
  358. catch (CDBException* e)
  359. {
  360. LeaveCriticalSection(&m_CriticalSection);
  361. SolveDBError(GetLastError(),e->m_strError);
  362. //OutputDebugString(e->m_strError);
  363. WriteTextLog(e->m_strError);
  364. #ifdef _DEBUG
  365. e->ReportError();
  366. #endif
  367. e->Delete();
  368. return -1;
  369. }
  370. }
  371. /************************************************************************/
  372. /*
  373. 函数:GetTableValues
  374. 描述:获取查询结果;
  375. 参数:
  376. IN: lpTableName 要查询的表;
  377. IN: lpFilter 查询条件;
  378. IN: AryOfFields 要查询的字段;
  379. OUT: AryOfValues 记录集结果返回;
  380. 返回:查询成功且记录数大于0返回TRUE;
  381. 要求:
  382. 注意:
  383. */
  384. /************************************************************************/
  385. BOOL CCatalogObj::GetTableValues(IN LPCTSTR lpTableName, IN LPCTSTR lpFilter, IN CStringArray &AryOfFields, OUT CArray<CStringArray,CStringArray> &AryOfValues)
  386. {
  387. try
  388. {
  389. EnterCriticalSection(&m_CriticalSection);
  390. if ( !m_bOpen )
  391. {
  392. //OutputDebugString(_T("数据库未打开\n"));
  393. //LOG4C_NO_FILENUM((LOG_NOTICE,"数据库未打开"));
  394. LeaveCriticalSection(&m_CriticalSection);
  395. return FALSE;
  396. }
  397. // 获取记录数;
  398. DWORD dwCount = 0;
  399. CString strSQL = _T("");
  400. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  401. strSQL.Format(_T("select count(*) as cot from %s where %s"), lpTableName, lpFilter);
  402. else
  403. strSQL.Format(_T("select count(*) as cot from %s"), lpTableName);
  404. CRecordset tagRet(m_pdbInstance);
  405. tagRet.Open(CRecordset::forwardOnly, strSQL);
  406. tagRet.GetFieldValue(_T("cot"), strSQL);
  407. tagRet.Close();
  408. if( (dwCount = _ttol(strSQL)) == 0 )
  409. {
  410. //OutputDebugString(_T("查询无记录\n"));
  411. //LOG4C_NO_FILENUM((LOG_NOTICE,"查询无记录"));
  412. LeaveCriticalSection(&m_CriticalSection);
  413. return TRUE;
  414. }
  415. // 查询;
  416. strSQL = _T("select ");
  417. for ( int i = 0; i < AryOfFields.GetSize(); i++)
  418. {
  419. strSQL += AryOfFields.ElementAt(i);
  420. strSQL += _T(",");
  421. }
  422. strSQL.TrimRight(_T(","));
  423. strSQL += _T(" from ");
  424. strSQL += lpTableName;
  425. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  426. {
  427. strSQL += _T(" where ");
  428. strSQL += lpFilter;
  429. }
  430. #if 1 // 关键字要去除[]后才能取值;
  431. for ( int i = 0; i < AryOfFields.GetSize(); i++)
  432. {
  433. AryOfFields.ElementAt(i).TrimLeft(_T('['));
  434. AryOfFields.ElementAt(i).TrimRight(_T(']'));
  435. }
  436. #endif
  437. DWORD dwIndex = 0;
  438. AryOfValues.SetSize(dwCount);
  439. tagRet.Open(CRecordset::forwardOnly,strSQL);
  440. while(!tagRet.IsEOF())
  441. {
  442. for ( int i = 0; i < AryOfFields.GetSize(); i++)
  443. {
  444. #if 0
  445. CString str;
  446. CDBVariant dbv;
  447. tagRet.GetFieldValue(AryOfFields.ElementAt(i),dbv);
  448. switch(dbv.m_dwType)
  449. {
  450. case DBVT_NULL:
  451. AryOfValues.ElementAt(dwIndex).Add("");
  452. break;
  453. case DBVT_BOOL:
  454. str.Format(_T("%d"),dbv.m_boolVal);
  455. AryOfValues.ElementAt(dwIndex).Add(str);
  456. break;
  457. case DBVT_UCHAR:
  458. str.Format(_T("%d"),dbv.m_chVal);
  459. AryOfValues.ElementAt(dwIndex).Add(str);
  460. break;
  461. case DBVT_SHORT:
  462. str.Format(_T("%d"),dbv.m_iVal);
  463. AryOfValues.ElementAt(dwIndex).Add(str);
  464. break;
  465. case DBVT_LONG:
  466. str.Format(_T("%ld"),dbv.m_lVal);
  467. AryOfValues.ElementAt(dwIndex).Add(str);
  468. break;
  469. case DBVT_SINGLE:
  470. str.Format(_T("%lf"),dbv.m_fltVal);
  471. AryOfValues.ElementAt(dwIndex).Add(str);
  472. break;
  473. case DBVT_DOUBLE:
  474. str.Format(_T("%lf"),dbv.m_dblVal);
  475. AryOfValues.ElementAt(dwIndex).Add(str);
  476. break;
  477. case DBVT_DATE:
  478. break;
  479. case DBVT_STRING:
  480. break;
  481. case DBVT_BINARY:
  482. break;
  483. case DBVT_ASTRING:
  484. str = *dbv.m_pstringA;
  485. AryOfValues.ElementAt(dwIndex).Add(str);
  486. break;
  487. case DBVT_WSTRING:
  488. if ( AryOfFields.ElementAt(i) == _T("content"))
  489. {
  490. str = *dbv.m_pstringA;
  491. AryOfValues.ElementAt(dwIndex).Add(str);
  492. }
  493. else
  494. {
  495. str = CW2A(*dbv.m_pstringW);
  496. AryOfValues.ElementAt(dwIndex).Add(str);
  497. }
  498. break;
  499. default:
  500. break;
  501. }
  502. #else
  503. CString str = _T("");
  504. CString strKey = AryOfFields.ElementAt(i);
  505. tagRet.GetFieldValue(strKey,str);
  506. OutputDebugString(str);
  507. OutputDebugString("\n");
  508. AryOfValues.ElementAt(dwIndex).Add(str);
  509. #endif
  510. }
  511. dwIndex++;
  512. #if 1 // 防止查询过程中有新记录添加导致错误;
  513. if ( dwIndex >= dwCount ) break;
  514. #endif
  515. tagRet.MoveNext();
  516. }
  517. tagRet.Close();
  518. //AryOfValues.FreeExtra();
  519. LeaveCriticalSection(&m_CriticalSection);
  520. }
  521. catch (CDBException *e)
  522. {
  523. LeaveCriticalSection(&m_CriticalSection);
  524. SolveDBError(GetLastError(),e->m_strError);
  525. //OutputDebugString(e->m_strError);
  526. //AfxMessageBox(e->m_strError);
  527. WriteTextLog(e->m_strError);
  528. #ifdef _DEBUG
  529. e->ReportError();
  530. #endif
  531. e->Delete();
  532. return FALSE;
  533. }
  534. return TRUE;
  535. }