AdoImpl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include "StdAfx.h"
  2. #include "AdoImpl.h"
  3. AdoImpl::AdoImpl(void)
  4. {
  5. m_pADODatabase = NULL;
  6. memset(m_szConnectString, 0, 1024);
  7. InitializeCriticalSection(&m_csAdo);
  8. }
  9. AdoImpl::~AdoImpl(void)
  10. {
  11. DeleteCriticalSection(&m_csAdo);
  12. }
  13. void AdoImpl::SetConnectString(
  14. IN const int &nType,
  15. IN LPCTSTR lpDataSource,
  16. IN LPCTSTR lpDatabaseTCPPort,
  17. IN LPCTSTR lpDatabaseAccount,
  18. IN LPCTSTR lpDatabasePassWord,
  19. IN LPCTSTR lpDatabaseName,
  20. IN LPCTSTR lpDatabaseFile)
  21. {
  22. switch (nType)
  23. {
  24. case ADO_ACCESS97:
  25. _stprintf_s(m_szConnectString, _T("Provider=Microsoft.Jet.OLEDB.3.51;Data Source=%s"), lpDatabaseFile);
  26. break;
  27. case ADO_ACCESS2000:
  28. _stprintf_s(m_szConnectString, _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s"), lpDatabaseFile);
  29. break;
  30. case ADO_ACCESS2007:
  31. _stprintf_s(m_szConnectString, _T("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%s"), lpDatabaseFile);
  32. break;
  33. case ADO_SQLSERVER:
  34. {
  35. if ( !_tcsicmp(lpDatabaseTCPPort, _T("0")))
  36. {
  37. //sprintf(m_szConnectString, "Provider=sqloledb.1;Persist Security Info=false;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; ",
  38. _stprintf_s(m_szConnectString, _T("Provider=sqloledb;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; "),
  39. lpDataSource, // IP地址或服务名称;
  40. lpDatabaseName,
  41. lpDatabaseAccount,
  42. lpDatabasePassWord);
  43. }
  44. else
  45. {
  46. //sprintf(m_szConnectString, "Provider=sqloledb.1;Persist Security Info=false;Data Source=%s,%s;Initial Catalog=%s;User Id=%s;Password=%s; ",
  47. _stprintf_s(m_szConnectString, _T("Provider=sqloledb;Data Source=%s,%s;Initial Catalog=%s;User Id=%s;Password=%s; "),
  48. lpDataSource, // IP地址或服务名称;
  49. lpDatabaseTCPPort,
  50. lpDatabaseName,
  51. lpDatabaseAccount,
  52. lpDatabasePassWord);
  53. }
  54. }
  55. break;
  56. case ADO_PGSQL:
  57. {
  58. _stprintf_s(m_szConnectString, _T("DRIVER={PostgreSQL ODBC Driver(UNICODE)}; SERVER=%s; port=%s; DATABASE=%s; UID=%s; PWD=%s;"),
  59. lpDataSource, // IP地址或服务名称;
  60. lpDatabaseTCPPort,
  61. lpDatabaseName,
  62. lpDatabaseAccount,
  63. lpDatabasePassWord);
  64. }
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. BOOL AdoImpl::IsOpen()
  71. {
  72. if ( m_pADODatabase == NULL)
  73. return FALSE;
  74. return m_pADODatabase->IsOpen();
  75. }
  76. BOOL AdoImpl::Opendatabase(IN LPCTSTR lpConnectString /* = NULL */)
  77. {
  78. BOOL bResult = FALSE;
  79. if( NULL == m_pADODatabase )
  80. {
  81. m_pADODatabase = new CADODatabase();
  82. if( m_pADODatabase )
  83. {
  84. if( lpConnectString == NULL )
  85. {
  86. m_pADODatabase->SetConnectionString(m_szConnectString);
  87. }
  88. else
  89. {
  90. m_pADODatabase->SetConnectionString(lpConnectString);
  91. _tcscpy_s(m_szConnectString, lpConnectString);
  92. }
  93. if( TRUE == m_pADODatabase->Open() )
  94. {
  95. bResult = TRUE;
  96. }
  97. else
  98. {
  99. if( m_pADODatabase->IsOpen() )
  100. {
  101. m_pADODatabase->Close();
  102. }
  103. delete m_pADODatabase;
  104. m_pADODatabase = NULL;
  105. }
  106. }
  107. }
  108. return bResult;
  109. }
  110. void AdoImpl::Closedatabase()
  111. {
  112. if( NULL != m_pADODatabase )
  113. {
  114. if( m_pADODatabase->IsOpen() )
  115. {
  116. m_pADODatabase->Close();
  117. }
  118. if( m_pADODatabase )
  119. delete m_pADODatabase;
  120. m_pADODatabase = NULL;
  121. }
  122. }
  123. int AdoImpl::Execute(IN LPCTSTR lpSQL)
  124. {
  125. INT nRetCode = 0;
  126. if( NULL == m_pADODatabase )
  127. {
  128. return -1;
  129. }
  130. EnterCriticalSection(&m_csAdo);
  131. if ( m_pADODatabase->IsOpen() )
  132. {
  133. nRetCode = m_pADODatabase->Execute(lpSQL);
  134. LeaveCriticalSection(&m_csAdo);
  135. }
  136. else
  137. {
  138. LeaveCriticalSection(&m_csAdo);
  139. return -1;
  140. }
  141. return nRetCode;
  142. }
  143. void AdoImpl::DeleteADORecordSet(CADORecordset* pRSet)
  144. {
  145. CloseADORecordSet(pRSet);
  146. if( pRSet )
  147. {
  148. delete pRSet;
  149. pRSet = NULL;
  150. }
  151. }
  152. BOOL AdoImpl::OpenADORecordSet(IN CADORecordset *pRSet, IN LPCTSTR lpSQL, IN const int& nCursorLocation)
  153. {
  154. BOOL bResult = TRUE;
  155. if( NULL == pRSet ) return FALSE;
  156. if ( !pRSet->Open(lpSQL, CADORecordset::openQuery))
  157. {
  158. DeleteADORecordSet(pRSet);
  159. bResult = FALSE;
  160. }
  161. return bResult;
  162. }
  163. void AdoImpl::CloseADORecordSet(CADORecordset* pRSet)
  164. {
  165. if( NULL != pRSet )
  166. {
  167. if( pRSet->IsOpen() )
  168. {
  169. pRSet->Close();
  170. }
  171. }
  172. }
  173. bool AdoImpl::GetADODateBaseStatus()
  174. {
  175. if( NULL == m_pADODatabase || !m_pADODatabase->IsOpen())
  176. return false;
  177. return true;
  178. }
  179. DWORD AdoImpl::GetTableRecordCount(IN LPCTSTR lpTableName, IN LPCTSTR lpFilters)
  180. {
  181. if ( lpTableName == NULL)
  182. return -1;
  183. TCHAR szSQL[1024] = _T("");
  184. if(lpFilters == NULL || _tcscmp(lpFilters,_T("")) == 0)
  185. _stprintf_s(szSQL,_T("select count(*) as cot from %s"),lpTableName);
  186. else
  187. _stprintf_s(szSQL,_T("select count(*) as cot from %s where %s"),lpTableName,lpFilters);
  188. DWORD nRecordCount = 0;
  189. _variant_t vtFieldValue;
  190. BOOL bResult = FALSE;
  191. //AutoThreadSection aSection(&s_critSection);
  192. EnterCriticalSection(&m_csAdo);
  193. bResult = GetADODateBaseStatus();
  194. if( FALSE == bResult )
  195. {
  196. LeaveCriticalSection(&m_csAdo);
  197. return -1;
  198. }
  199. CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
  200. if( NULL == pRSet )
  201. {
  202. LeaveCriticalSection(&m_csAdo);
  203. return -1;
  204. }
  205. bResult = OpenADORecordSet(pRSet, szSQL);
  206. if( FALSE == bResult )
  207. {
  208. LeaveCriticalSection(&m_csAdo);
  209. return -1;
  210. }
  211. if( !pRSet->IsFieldNull(_T("cot")) )
  212. {
  213. pRSet->GetFieldValue(_T("cot"), nRecordCount);
  214. }
  215. else
  216. {
  217. nRecordCount = -1;
  218. }// end if( !pRSet->IsFieldNull(0) )
  219. DeleteADORecordSet(pRSet);
  220. LeaveCriticalSection(&m_csAdo);
  221. return nRecordCount;
  222. }
  223. /************************************************************************/
  224. /* 函数:GetFieldValueList[4/12/2016 IT];
  225. /* 描述:获取指定表查询字段的条件记录集;
  226. /* 参数:;
  227. /* [IN] lpTableName:要查询的表;
  228. /* [IN] lpFields:要查询的字段;
  229. /* [IN] lpFilters:查询的条件;
  230. /* [IN] dwCallBackType:回调函数类型;
  231. /* [IN] lpCallBack:回调函数指针;
  232. /* [INOUT] lpParamter:传递给回调函数的参数;
  233. /* 返回:成功查询返回记录数, 参数错误返回-1;
  234. /* 注意:不使用select count(*) as cot from table来获取记录数;
  235. /* 示例:;
  236. /*
  237. /* 修改:;
  238. /* 日期:;
  239. /* 内容:;
  240. /************************************************************************/
  241. int AdoImpl::GetFieldValueList(IN LPCTSTR lpTableName, IN LPCTSTR lpFields, IN LPCTSTR lpFilters, IN const DWORD &dwCallBackType, IN LPVOID lpCallBack, IN OUT LPVOID lpParamter)
  242. {
  243. if ( lpCallBack == NULL || lpParamter == NULL )
  244. return -1;
  245. if ( lpTableName == NULL || lpTableName[0] == _T('\0') || lpFields == NULL || lpFields[0] == _T('\0'))
  246. return -1;
  247. int nRecordCount = 0;
  248. BOOL bResult = FALSE;
  249. TCHAR szSQL[2048] = _T(""); // 长度可能会不够,_stprintf_s结果导致SQL语句不正确;
  250. EnterCriticalSection( &m_csAdo );
  251. bResult = GetADODateBaseStatus();
  252. if( FALSE == bResult ){
  253. LeaveCriticalSection( &m_csAdo );
  254. return -1;
  255. }
  256. // 获取查询记录集;
  257. CADORecordset *pRSet = new CADORecordset(m_pADODatabase);
  258. LeaveCriticalSection( &m_csAdo );
  259. if( NULL == pRSet )
  260. return -1;
  261. if ( lpFilters && lpFilters[0] != _T('\0') )
  262. _stprintf_s(szSQL, _T("select %s from %s where %s"), lpFields, lpTableName, lpFilters);
  263. else
  264. _stprintf_s(szSQL, _T("select %s from %s"), lpFields, lpTableName);
  265. bResult = OpenADORecordSet(pRSet, szSQL);
  266. if( FALSE == bResult )
  267. {
  268. return -1;
  269. }
  270. // 让回调函数处理返回的数据集结果;
  271. nRecordCount = ((RecordsetCallback)lpCallBack)(dwCallBackType, lpParamter, 0, pRSet);
  272. DeleteADORecordSet(pRSet);
  273. return nRecordCount;
  274. }
  275. /************************************************************************/
  276. /* 函数:GetFieldValueList[4/12/2016 IT];
  277. /* 描述:获取指定表查询字段的条件记录集;
  278. /* 参数:;
  279. /* [IN] lpTableName:要查询的表;
  280. /* [IN] lpFields:要查询的字段;
  281. /* [IN] lpFilters:查询的条件;
  282. /* [IN] dwCallBackType:回调函数类型;
  283. /* [IN] lpCallBack:回调函数指针;
  284. /* [INOUT] lpParamter:传递给回调函数的参数;
  285. /* 返回:成功查询返回记录数, 参数错误返回-1;
  286. /* 注意:;
  287. /* 示例:;
  288. /*
  289. /* 修改:;
  290. /* 日期:;
  291. /* 内容:;
  292. /************************************************************************/
  293. int AdoImpl::GetFieldValueListEx(IN LPCTSTR lpTableName, IN LPCTSTR lpFields, IN LPCTSTR lpFilters, IN const DWORD &dwCallBackType, IN LPVOID lpCallBack, IN OUT LPVOID lpParamter)
  294. {
  295. if ( lpCallBack == NULL || lpParamter == NULL )
  296. return -1;
  297. if ( lpTableName == NULL || lpTableName[0] == _T('\0') || lpFields == NULL || lpFields[0] == _T('\0'))
  298. return -1;
  299. int nRecordCount = 0;
  300. BOOL bResult = FALSE;
  301. TCHAR szSQL[2048] = _T(""); // 长度可能会不够,_stprintf_s结果导致SQL语句不正确;
  302. if ( lpFilters && lpFilters[0] != _T('\0') )
  303. {
  304. _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), lpTableName, lpFilters);
  305. }
  306. else
  307. {
  308. _stprintf_s(szSQL, _T("select count(*) as cot from %s"), lpTableName);
  309. }
  310. EnterCriticalSection( &m_csAdo );
  311. bResult = GetADODateBaseStatus();
  312. if( FALSE == bResult ){
  313. LeaveCriticalSection( &m_csAdo );
  314. return -1;
  315. }
  316. // 获取记录数;
  317. CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
  318. LeaveCriticalSection( &m_csAdo );
  319. if( NULL == pRSet )
  320. return -1;
  321. bResult = OpenADORecordSet(pRSet, szSQL);
  322. if( FALSE == bResult ){
  323. return -1;
  324. }
  325. if( !pRSet->IsFieldNull(_T("cot")) )
  326. {
  327. pRSet->GetFieldValue(_T("cot"), nRecordCount);
  328. }
  329. else
  330. {
  331. nRecordCount = -1;
  332. // 没有记录则返回;
  333. DeleteADORecordSet(pRSet);
  334. return -1;
  335. }
  336. DeleteADORecordSet(pRSet);
  337. // 获取查询记录集;
  338. pRSet = new CADORecordset(m_pADODatabase);
  339. LeaveCriticalSection( &m_csAdo );
  340. if( NULL == pRSet )
  341. return -1;
  342. if ( lpFilters && lpFilters[0] != _T('\0') )
  343. _stprintf_s(szSQL, _T("select %s from %s where %s"), lpFields, lpTableName, lpFilters);
  344. else
  345. _stprintf_s(szSQL, _T("select %s from %s"), lpFields, lpTableName);
  346. bResult = OpenADORecordSet(pRSet, szSQL);
  347. if( FALSE == bResult )
  348. {
  349. return -1;
  350. }
  351. // 让回调函数处理返回的数据集结果;
  352. nRecordCount = ((RecordsetCallback)lpCallBack)(dwCallBackType, lpParamter, nRecordCount, pRSet);
  353. DeleteADORecordSet(pRSet);
  354. return nRecordCount;
  355. }
  356. BOOL AdoImpl::AppenChunkFromFile(IN LPCTSTR lpTableName, IN LPCTSTR lpFieldName, IN LPCTSTR lpFileName)
  357. {
  358. if ( lpTableName == NULL ||
  359. lpTableName[0] == _T('\0') ||
  360. lpFieldName == NULL ||
  361. lpFieldName[0] == _T('\0') ||
  362. lpFileName == NULL ||
  363. !PathFileExists(lpFileName) )
  364. return FALSE;
  365. CFile cf;
  366. BYTE* pBuffer = NULL;
  367. if ( !cf.Open(lpFileName, CFile::modeRead) )
  368. return FALSE;
  369. DWORD dwLength = cf.GetLength();
  370. pBuffer = new BYTE[dwLength+1];
  371. memset(pBuffer, 0, dwLength + 1);
  372. cf.Read(pBuffer, dwLength);
  373. cf.Close();
  374. BOOL bResult = AppenChunkFromBuffer(lpTableName, lpFieldName, pBuffer, dwLength);
  375. if ( pBuffer )
  376. {
  377. delete []pBuffer;
  378. pBuffer = NULL;
  379. }
  380. return bResult;
  381. }
  382. /************************************************************************/
  383. /* 函数:AppenChunkFromBuffer[4/13/2016 IT];
  384. /* 描述:从指定的缓冲区里把二进制数据写入字段中;
  385. /* 参数:;
  386. /* [IN] lpTableName:表名;
  387. /* [IN] lpFieldName:字段名;
  388. /* [IN] lpBuffer:缓冲区数据;
  389. /* [IN] nBuflen:缓冲区大小;
  390. /* 返回:void;
  391. /* 注意:;
  392. /* 示例:;
  393. /*
  394. /* 修改:;
  395. /* 日期:;
  396. /* 内容:;
  397. /************************************************************************/
  398. BOOL AdoImpl::AppenChunkFromBuffer(IN LPCTSTR lpTableName, IN LPCTSTR lpFieldName, IN BYTE* lpBuffer, IN const int& nBuflen)
  399. {
  400. if ( lpTableName == NULL ||
  401. lpTableName[0] == _T('\0') ||
  402. lpFieldName == NULL ||
  403. lpFieldName[0] == _T('\0') ||
  404. lpBuffer == NULL )
  405. return FALSE;
  406. EnterCriticalSection( &m_csAdo );
  407. BOOL bResult = GetADODateBaseStatus();
  408. if( FALSE == bResult ){
  409. LeaveCriticalSection( &m_csAdo );
  410. return FALSE;
  411. }
  412. CADORecordset *pRSet = new CADORecordset(m_pADODatabase);
  413. LeaveCriticalSection( &m_csAdo );
  414. if( NULL == pRSet )
  415. return -1;
  416. TCHAR szSQL[2048] = {0};
  417. _stprintf_s(szSQL, _T("select %s from %s"), lpFieldName, lpTableName);
  418. // 写二进制大文件时,_RecordSetPtr必须以adUseSever方式打开,则否更新不成功;
  419. bResult = OpenADORecordSet(pRSet, szSQL, adUseServer);
  420. if( FALSE == bResult )
  421. {
  422. return FALSE;
  423. }
  424. pRSet->Edit();
  425. bResult = pRSet->AppendChunk(lpFieldName, lpBuffer, nBuflen);
  426. if ( bResult )
  427. {
  428. pRSet->Update();
  429. }
  430. DeleteADORecordSet(pRSet);
  431. return bResult;
  432. }