Ado2Impl.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #include "StdAfx.h"
  2. #include "Ado2Impl.h"
  3. Ado2Impl::Ado2Impl(void)
  4. {
  5. m_bOpen = FALSE;
  6. m_pADODatabase = NULL;
  7. memset(m_szConnectString,0,1024);
  8. InitializeCriticalSection(&m_csAdo);
  9. }
  10. Ado2Impl::~Ado2Impl(void)
  11. {
  12. CloseCatalog();
  13. DeleteCriticalSection(&m_csAdo);
  14. }
  15. BOOL Ado2Impl::OpenCatalog(IN Catalogkernel::STDatabaseInfo& dbInfo)
  16. {
  17. if ( m_pADODatabase ) return TRUE;
  18. BOOL bRet = FALSE;
  19. if ( !_tcsicmp(dbInfo.szDatabaseTCPPort, _T("0")))
  20. {
  21. _stprintf_s(m_szConnectString, _T("Provider=sqloledb;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; "),
  22. dbInfo.szDataSource, // IP華硊麼督昢靡備;
  23. dbInfo.szDatabaseName,
  24. dbInfo.szDatabaseAccount,
  25. dbInfo.szDatabasePassword);
  26. }
  27. else
  28. {
  29. _stprintf_s(m_szConnectString, _T("Provider=sqloledb;Data Source=%s,%s;Initial Catalog=%s;User Id=%s;Password=%s; "),
  30. dbInfo.szDataSource, // IP華硊麼督昢靡備;
  31. dbInfo.szDatabaseTCPPort,
  32. dbInfo.szDatabaseName,
  33. dbInfo.szDatabaseAccount,
  34. dbInfo.szDatabasePassword);
  35. }
  36. m_pADODatabase = new CADODatabase();
  37. if ( m_pADODatabase )
  38. {
  39. m_pADODatabase->SetConnectionString(m_szConnectString);
  40. if ( m_pADODatabase->Open() )
  41. {
  42. bRet = TRUE;
  43. }
  44. else
  45. {
  46. if ( m_pADODatabase->IsOpen() )
  47. {
  48. m_pADODatabase->Close();
  49. }
  50. delete m_pADODatabase;
  51. m_pADODatabase = NULL;
  52. }
  53. }
  54. return bRet;
  55. }
  56. void Ado2Impl::CloseCatalog()
  57. {
  58. if( NULL != m_pADODatabase )
  59. {
  60. if( m_pADODatabase->IsOpen() )
  61. {
  62. m_pADODatabase->Close();
  63. }
  64. if( m_pADODatabase )
  65. delete m_pADODatabase;
  66. m_pADODatabase = NULL;
  67. }
  68. }
  69. INT Ado2Impl::Execute(IN LPCTSTR lpExcSQL)
  70. {
  71. INT nRetCode = 0;
  72. if ( NULL == m_pADODatabase || NULL == lpExcSQL)
  73. return -1;
  74. try
  75. {
  76. EnterCriticalSection(&m_csAdo);
  77. if ( m_pADODatabase->IsOpen() )
  78. {
  79. nRetCode = m_pADODatabase->Execute(lpExcSQL);
  80. LeaveCriticalSection(&m_csAdo);
  81. }
  82. else
  83. {
  84. LeaveCriticalSection(&m_csAdo);
  85. return -1;
  86. }
  87. }
  88. catch (_com_error* e)
  89. {
  90. }
  91. return nRetCode;
  92. }
  93. INT Ado2Impl::GetTblRecordCount(IN LPCTSTR lpTblName, IN LPCTSTR lpFilters)
  94. {
  95. if ( lpTblName == NULL)
  96. return -1;
  97. TCHAR szSQL[1024] = _T("");
  98. if(lpFilters == NULL || _tcscmp(lpFilters,_T("")) == 0)
  99. _stprintf_s(szSQL,_T("select count(*) as cot from %s"),lpTblName);
  100. else
  101. _stprintf_s(szSQL,_T("select count(*) as cot from %s where %s"),lpTblName,lpFilters);
  102. DWORD nRecordCount = 0;
  103. _variant_t vtFieldValue;
  104. bool bResult = false;
  105. EnterCriticalSection(&m_csAdo);
  106. bResult = GetADODateBaseStatus();
  107. if( false == bResult )
  108. {
  109. LeaveCriticalSection(&m_csAdo);
  110. return -1;
  111. }
  112. CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
  113. if( NULL == pRSet )
  114. {
  115. LeaveCriticalSection(&m_csAdo);
  116. return -1;
  117. }
  118. bResult = OpenADORecordSet(pRSet, szSQL);
  119. if( false == bResult )
  120. {
  121. LeaveCriticalSection(&m_csAdo);
  122. DeleteADORecordSet(pRSet);
  123. return -1;
  124. }
  125. if( !pRSet->IsFieldNull(_T("cot")) )
  126. {
  127. pRSet->GetFieldValue(_T("cot"), nRecordCount);
  128. }
  129. else
  130. {
  131. nRecordCount = -1;
  132. }
  133. DeleteADORecordSet(pRSet);
  134. LeaveCriticalSection(&m_csAdo);
  135. return nRecordCount;
  136. }
  137. BOOL Ado2Impl::IsOpen()
  138. {
  139. return m_pADODatabase == NULL;
  140. }
  141. BOOL Ado2Impl::GetADODateBaseStatus()
  142. {
  143. if( NULL == m_pADODatabase || !m_pADODatabase->IsOpen())
  144. return FALSE;
  145. return TRUE;
  146. }
  147. BOOL Ado2Impl::OpenADORecordSet(IN CADORecordset *pRSet, IN LPCTSTR lpSQL)
  148. {
  149. if( NULL == pRSet || NULL == lpSQL)
  150. return FALSE;
  151. INT nRetCode;
  152. BOOL bResult = TRUE;
  153. try
  154. {
  155. nRetCode = pRSet->Open(lpSQL, CADORecordset::openQuery);
  156. if (nRetCode == 0)
  157. {
  158. DeleteADORecordSet(pRSet);
  159. bResult = FALSE;
  160. }
  161. }
  162. catch (_com_error &e)
  163. {
  164. DeleteADORecordSet(pRSet);
  165. bResult = FALSE;
  166. }
  167. return bResult;
  168. }
  169. void Ado2Impl::CloseADORecordSet(IN CADORecordset* pRSet)
  170. {
  171. if( NULL != pRSet )
  172. {
  173. if( pRSet->IsOpen() )
  174. {
  175. pRSet->Close();
  176. }
  177. }
  178. }
  179. void Ado2Impl::DeleteADORecordSet(IN CADORecordset* pRSet)
  180. {
  181. CloseADORecordSet(pRSet);
  182. if( pRSet )
  183. {
  184. delete pRSet;
  185. pRSet = NULL;
  186. }
  187. }
  188. INT Ado2Impl::GetTbl_NetShareInfoRecordSet(IN CONST TCHAR *pTableName, IN CONST TCHAR *pFilters, IN CONST TCHAR *pSql, INT &nFieldCount, OUT vector<TblNetShareInfo>& vtRSet)
  189. {
  190. TCHAR szSQL[1024] = _T("");
  191. if(pFilters == NULL || _tcscmp(pFilters,_T("")) == 0)
  192. _stprintf_s(szSQL,_T("select count(*) as cot from %s"),pTableName);
  193. else
  194. _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters);
  195. int nRecordCount = 0;
  196. _variant_t vtFieldValue;
  197. bool bResult = false;
  198. EnterCriticalSection( &m_csAdo );
  199. bResult = GetADODateBaseStatus();
  200. if( false == bResult )
  201. {
  202. LeaveCriticalSection( &m_csAdo );
  203. return -1;
  204. }
  205. CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
  206. LeaveCriticalSection( &m_csAdo );
  207. if( NULL == pRSet )
  208. {
  209. return -1;
  210. }
  211. bResult = OpenADORecordSet(pRSet, szSQL);
  212. if( false == bResult )
  213. {
  214. DeleteADORecordSet(pRSet);
  215. return -1;
  216. }
  217. if( !pRSet->IsFieldNull(_T("cot")) )
  218. {
  219. pRSet->GetFieldValue(_T("cot"), nRecordCount);
  220. }
  221. else
  222. {
  223. nRecordCount = -1;
  224. }
  225. DeleteADORecordSet(pRSet);
  226. pRSet = new CADORecordset(m_pADODatabase);
  227. LeaveCriticalSection( &m_csAdo );
  228. if( NULL == pRSet )
  229. {
  230. return -1;
  231. }
  232. bResult = OpenADORecordSet(pRSet, pSql);
  233. if( false == bResult )
  234. {
  235. DeleteADORecordSet(pRSet);
  236. return -1;
  237. }
  238. nFieldCount = pRSet->GetFieldCount();
  239. for ( int i = 0; i < nRecordCount; i++)
  240. {
  241. TblNetShareInfo tagVal;
  242. if( !pRSet->IsFieldNull(_T("enable")) )
  243. {
  244. pRSet->GetFieldValue(_T("enable"), vtFieldValue);
  245. tagVal._enable = vtFieldValue;
  246. }
  247. else
  248. {
  249. tagVal._enable = 0;
  250. }
  251. if( !pRSet->IsFieldNull(_T("branchid")) )
  252. {
  253. pRSet->GetFieldValue(_T("branchid"), vtFieldValue);
  254. tagVal._branchid = TString((TCHAR*)(_bstr_t)vtFieldValue);
  255. }
  256. else
  257. {
  258. tagVal._branchid = _T("");
  259. }
  260. if( !pRSet->IsFieldNull(_T("sharePath")) )
  261. {
  262. pRSet->GetFieldValue(_T("sharePath"), vtFieldValue);
  263. tagVal._sharepath = TString((TCHAR*)(_bstr_t)vtFieldValue);
  264. }
  265. else
  266. {
  267. tagVal._sharepath = _T("");
  268. }
  269. if( !pRSet->IsFieldNull(_T("mincapacity")) )
  270. {
  271. pRSet->GetFieldValue(_T("mincapacity"), vtFieldValue);
  272. tagVal._mincapacity = vtFieldValue;
  273. }
  274. else
  275. {
  276. tagVal._mincapacity = 0;
  277. }
  278. if( !pRSet->IsFieldNull(_T("maxcapacity")) )
  279. {
  280. pRSet->GetFieldValue(_T("maxcapacity"), vtFieldValue);
  281. tagVal._maxcapacity = vtFieldValue;
  282. }
  283. else
  284. {
  285. tagVal._maxcapacity = 0;
  286. }
  287. if( !pRSet->IsFieldNull(_T("photoType")) )
  288. {
  289. pRSet->GetFieldValue(_T("photoType"), vtFieldValue);
  290. tagVal._phototype = vtFieldValue;
  291. }
  292. else
  293. {
  294. tagVal._phototype = 0;
  295. }
  296. if( !pRSet->IsFieldNull(_T("priority")) )
  297. {
  298. pRSet->GetFieldValue(_T("priority"), vtFieldValue);
  299. tagVal._priority = vtFieldValue;
  300. }
  301. else
  302. {
  303. tagVal._priority = 0;
  304. }
  305. vtRSet.push_back(tagVal);
  306. pRSet->MoveNext();
  307. }
  308. DeleteADORecordSet(pRSet);
  309. return nRecordCount;
  310. }
  311. INT Ado2Impl::GetTbl_dindanRecordSet(IN CONST TCHAR *pTableName, IN CONST TCHAR *pFilters, IN CONST TCHAR *pSql, INT &nFieldCount, OUT vector<Tbldindan>& vtRSet)
  312. {
  313. TCHAR szSQL[1024] = _T("");
  314. if(pFilters == NULL || _tcscmp(pFilters,_T("")) == 0)
  315. _stprintf_s(szSQL,_T("select count(*) as cot from %s"),pTableName);
  316. else
  317. _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters);
  318. int nRecordCount = 0;
  319. _variant_t vtFieldValue;
  320. bool bResult = false;
  321. EnterCriticalSection( &m_csAdo );
  322. bResult = GetADODateBaseStatus();
  323. if( false == bResult )
  324. {
  325. LeaveCriticalSection( &m_csAdo );
  326. return -1;
  327. }
  328. CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
  329. LeaveCriticalSection( &m_csAdo );
  330. if( NULL == pRSet )
  331. {
  332. return -1;
  333. }
  334. bResult = OpenADORecordSet(pRSet, szSQL);
  335. if( false == bResult )
  336. {
  337. DeleteADORecordSet(pRSet);
  338. return -1;
  339. }
  340. if( !pRSet->IsFieldNull(_T("cot")) )
  341. {
  342. pRSet->GetFieldValue(_T("cot"), nRecordCount);
  343. }
  344. else
  345. {
  346. nRecordCount = -1;
  347. }
  348. DeleteADORecordSet(pRSet);
  349. pRSet = new CADORecordset(m_pADODatabase);
  350. LeaveCriticalSection( &m_csAdo );
  351. if( NULL == pRSet )
  352. {
  353. return -1;
  354. }
  355. bResult = OpenADORecordSet(pRSet, pSql);
  356. if( false == bResult )
  357. {
  358. DeleteADORecordSet(pRSet);
  359. return -1;
  360. }
  361. nFieldCount = pRSet->GetFieldCount();
  362. for ( int i = 0; i < nRecordCount; i++)
  363. {
  364. Tbldindan tagVal;
  365. if( !pRSet->IsFieldNull(_T("id")) )
  366. {
  367. pRSet->GetFieldValue(_T("id"), vtFieldValue);
  368. tagVal._id = TString((TCHAR*)(_bstr_t)vtFieldValue);
  369. }
  370. else
  371. {
  372. tagVal._id = _T("");
  373. }
  374. if( !pRSet->IsFieldNull(_T("name1")) )
  375. {
  376. pRSet->GetFieldValue(_T("name1"), vtFieldValue);
  377. tagVal._name1 = TString((TCHAR*)(_bstr_t)vtFieldValue);
  378. }
  379. else
  380. {
  381. tagVal._name1 = _T("");
  382. }
  383. if( !pRSet->IsFieldNull(_T("name2")) )
  384. {
  385. pRSet->GetFieldValue(_T("name2"), vtFieldValue);
  386. tagVal._name2 = TString((TCHAR*)(_bstr_t)vtFieldValue);
  387. }
  388. else
  389. {
  390. tagVal._name2 = _T("");
  391. }
  392. if( !pRSet->IsFieldNull(_T("taoxiname")) )
  393. {
  394. pRSet->GetFieldValue(_T("taoxiname"), vtFieldValue);
  395. tagVal._taoxiname = TString((TCHAR*)(_bstr_t)vtFieldValue);
  396. }
  397. else
  398. {
  399. tagVal._taoxiname = _T("");
  400. }
  401. if( !pRSet->IsFieldNull(_T("taoxijiage")) )
  402. {
  403. pRSet->GetFieldValue(_T("taoxijiage"), vtFieldValue);
  404. tagVal._taoxijiage = TString((TCHAR*)(_bstr_t)vtFieldValue);
  405. }
  406. else
  407. {
  408. tagVal._taoxijiage = _T("");
  409. }
  410. if( !pRSet->IsFieldNull(_T("curno")) )
  411. {
  412. pRSet->GetFieldValue(_T("curno"), vtFieldValue);
  413. tagVal._curno = TString((TCHAR*)(_bstr_t)vtFieldValue);
  414. }
  415. else
  416. {
  417. tagVal._curno = _T("");
  418. }
  419. vtRSet.push_back(tagVal);
  420. pRSet->MoveNext();
  421. }
  422. DeleteADORecordSet(pRSet);
  423. return nRecordCount;
  424. }