CatalogObj.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. //OutputDebugString(e->m_strError);
  316. //LOG4C_NO_FILENUM((LOG_NOTICE,"执行SQL语句失败:%s",CW2A(e->m_strError)));
  317. CString str;
  318. str.Format(_T("%s,%s"),e->m_strError,lpSQL);
  319. WriteLogin(str);
  320. #ifdef _DEBUG
  321. e->ReportError();
  322. #endif
  323. e->Delete();
  324. return FALSE;
  325. }
  326. return TRUE;
  327. }
  328. /************************************************************************/
  329. /*
  330. 函数:GetSelectCount
  331. 描述:获取指定表的查询记录数;
  332. 参数:
  333. IN: lpTableName 要查询的表;
  334. IN: lpFilter 查询条件;
  335. 返回:成功查询返回记录条数,否则返回-1;
  336. 要求:
  337. 注意:
  338. */
  339. /************************************************************************/
  340. int CCatalogObj::GetSelectCount(IN LPCTSTR lpTableName, IN LPCTSTR lpFilter)
  341. {
  342. try
  343. {
  344. EnterCriticalSection(&m_CriticalSection);
  345. if (!m_bOpen)
  346. {
  347. LeaveCriticalSection(&m_CriticalSection);
  348. return -1;
  349. }
  350. CString strSQL;
  351. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  352. strSQL.Format(_T("select count(*) as cot from %s where %s"), lpTableName, lpFilter);
  353. else
  354. strSQL.Format(_T("select count(*) as cot from %s"), lpTableName);
  355. CRecordset tagRecordset(m_pdbInstance);
  356. tagRecordset.Open(CRecordset::forwardOnly, strSQL);
  357. tagRecordset.GetFieldValue(_T("cot"), strSQL);
  358. tagRecordset.Close();
  359. LeaveCriticalSection(&m_CriticalSection);
  360. return _ttoi(strSQL);
  361. }
  362. catch (CDBException* e)
  363. {
  364. LeaveCriticalSection(&m_CriticalSection);
  365. SolveDBError(GetLastError(),e->m_strError);
  366. //OutputDebugString(e->m_strError);
  367. WriteLogin(e->m_strError);
  368. #ifdef _DEBUG
  369. e->ReportError();
  370. #endif
  371. e->Delete();
  372. return -1;
  373. }
  374. }
  375. /************************************************************************/
  376. /*
  377. 函数:GetTableValues
  378. 描述:获取查询结果;
  379. 参数:
  380. IN: lpTableName 要查询的表;
  381. IN: lpFilter 查询条件;
  382. IN: AryOfFields 要查询的字段;
  383. OUT: AryOfValues 记录集结果返回;
  384. 返回:查询成功且记录数大于0返回TRUE;
  385. 要求:
  386. 注意:
  387. */
  388. /************************************************************************/
  389. BOOL CCatalogObj::GetTableValues(IN LPCTSTR lpTableName, IN LPCTSTR lpFilter, IN CStringArray &AryOfFields, OUT CArray<CStringArray,CStringArray> &AryOfValues)
  390. {
  391. try
  392. {
  393. EnterCriticalSection(&m_CriticalSection);
  394. if ( !m_bOpen )
  395. {
  396. //OutputDebugString(_T("数据库未打开\n"));
  397. //LOG4C_NO_FILENUM((LOG_NOTICE,"数据库未打开"));
  398. LeaveCriticalSection(&m_CriticalSection);
  399. return FALSE;
  400. }
  401. // 获取记录数;
  402. DWORD dwCount = 0;
  403. CString strSQL = _T("");
  404. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  405. strSQL.Format(_T("select count(*) as cot from %s where %s"), lpTableName, lpFilter);
  406. else
  407. strSQL.Format(_T("select count(*) as cot from %s"), lpTableName);
  408. CRecordset tagRet(m_pdbInstance);
  409. tagRet.Open(CRecordset::forwardOnly, strSQL);
  410. tagRet.GetFieldValue(_T("cot"), strSQL);
  411. tagRet.Close();
  412. if( (dwCount = _ttol(strSQL)) == 0 )
  413. {
  414. //OutputDebugString(_T("查询无记录\n"));
  415. //LOG4C_NO_FILENUM((LOG_NOTICE,"查询无记录"));
  416. LeaveCriticalSection(&m_CriticalSection);
  417. return TRUE;
  418. }
  419. // 查询;
  420. strSQL = _T("select ");
  421. for ( int i = 0; i < AryOfFields.GetSize(); i++)
  422. {
  423. strSQL += AryOfFields.ElementAt(i);
  424. strSQL += _T(",");
  425. }
  426. strSQL.TrimRight(_T(","));
  427. strSQL += _T(" from ");
  428. strSQL += lpTableName;
  429. if (lpFilter && _tcsicmp(lpFilter, _T("")) != 0)
  430. {
  431. strSQL += _T(" where ");
  432. strSQL += lpFilter;
  433. }
  434. #if 1 // 关键字要去除[]后才能取值;
  435. for ( int i = 0; i < AryOfFields.GetSize(); i++)
  436. {
  437. AryOfFields.ElementAt(i).TrimLeft(_T('['));
  438. AryOfFields.ElementAt(i).TrimRight(_T(']'));
  439. }
  440. #endif
  441. DWORD dwIndex = 0;
  442. AryOfValues.SetSize(dwCount);
  443. tagRet.Open(CRecordset::forwardOnly,strSQL);
  444. while(!tagRet.IsEOF())
  445. {
  446. for ( int i = 0; i < AryOfFields.GetSize(); i++)
  447. {
  448. #if 0
  449. CString str;
  450. CDBVariant dbv;
  451. tagRet.GetFieldValue(AryOfFields.ElementAt(i),dbv);
  452. switch(dbv.m_dwType)
  453. {
  454. case DBVT_NULL:
  455. AryOfValues.ElementAt(dwIndex).Add("");
  456. break;
  457. case DBVT_BOOL:
  458. str.Format(_T("%d"),dbv.m_boolVal);
  459. AryOfValues.ElementAt(dwIndex).Add(str);
  460. break;
  461. case DBVT_UCHAR:
  462. str.Format(_T("%d"),dbv.m_chVal);
  463. AryOfValues.ElementAt(dwIndex).Add(str);
  464. break;
  465. case DBVT_SHORT:
  466. str.Format(_T("%d"),dbv.m_iVal);
  467. AryOfValues.ElementAt(dwIndex).Add(str);
  468. break;
  469. case DBVT_LONG:
  470. str.Format(_T("%ld"),dbv.m_lVal);
  471. AryOfValues.ElementAt(dwIndex).Add(str);
  472. break;
  473. case DBVT_SINGLE:
  474. str.Format(_T("%lf"),dbv.m_fltVal);
  475. AryOfValues.ElementAt(dwIndex).Add(str);
  476. break;
  477. case DBVT_DOUBLE:
  478. str.Format(_T("%lf"),dbv.m_dblVal);
  479. AryOfValues.ElementAt(dwIndex).Add(str);
  480. break;
  481. case DBVT_DATE:
  482. break;
  483. case DBVT_STRING:
  484. break;
  485. case DBVT_BINARY:
  486. break;
  487. case DBVT_ASTRING:
  488. str = *dbv.m_pstringA;
  489. AryOfValues.ElementAt(dwIndex).Add(str);
  490. break;
  491. case DBVT_WSTRING:
  492. if ( AryOfFields.ElementAt(i) == _T("content"))
  493. {
  494. str = *dbv.m_pstringA;
  495. AryOfValues.ElementAt(dwIndex).Add(str);
  496. }
  497. else
  498. {
  499. str = CW2A(*dbv.m_pstringW);
  500. AryOfValues.ElementAt(dwIndex).Add(str);
  501. }
  502. break;
  503. default:
  504. break;
  505. }
  506. #else
  507. CString str = _T("");
  508. CString strKey = AryOfFields.ElementAt(i);
  509. tagRet.GetFieldValue(strKey,str);
  510. OutputDebugString(str);
  511. OutputDebugString("\n");
  512. AryOfValues.ElementAt(dwIndex).Add(str);
  513. #endif
  514. }
  515. dwIndex++;
  516. #if 1 // 防止查询过程中有新记录添加导致错误;
  517. if ( dwIndex >= dwCount ) break;
  518. #endif
  519. tagRet.MoveNext();
  520. }
  521. tagRet.Close();
  522. //AryOfValues.FreeExtra();
  523. LeaveCriticalSection(&m_CriticalSection);
  524. }
  525. catch (CDBException *e)
  526. {
  527. LeaveCriticalSection(&m_CriticalSection);
  528. SolveDBError(GetLastError(),e->m_strError);
  529. //OutputDebugString(e->m_strError);
  530. //AfxMessageBox(e->m_strError);
  531. WriteLogin(e->m_strError);
  532. #ifdef _DEBUG
  533. e->ReportError();
  534. #endif
  535. e->Delete();
  536. return FALSE;
  537. }
  538. return TRUE;
  539. }