SQLWrap.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //#include <windows.h>
  2. #include "pch.h"
  3. #include "SQLWrap.h"
  4. CSQLWrap::CSQLWrap()
  5. {
  6. mhSQLEnv = NULL;
  7. mhSQLConn = SQL_NULL_HDBC;
  8. }
  9. CSQLWrap::~CSQLWrap()
  10. {
  11. this->Terminate();
  12. }
  13. SQLRETURN CSQLWrap::InitializeEnvironment()
  14. {
  15. SQLRETURN SQLResult;
  16. if (NULL == mhSQLEnv)
  17. {
  18. SQLResult = SQLAllocHandle(SQL_HANDLE_ENV,
  19. SQL_NULL_HANDLE,
  20. &mhSQLEnv);
  21. if (SQL_SUCCESS == SQLResult)
  22. {
  23. //
  24. // Exhibit ODBC 2.x behavior. Not sure why,
  25. // but this is what dev is doing.
  26. //
  27. SQLResult = SQLSetEnvAttr(mhSQLEnv,
  28. SQL_ATTR_ODBC_VERSION,
  29. (SQLPOINTER) SQL_OV_ODBC2,
  30. 0);
  31. if (SQL_SUCCESS != SQLResult)
  32. {
  33. SQLFreeHandle(SQL_HANDLE_ENV, mhSQLEnv);
  34. mhSQLEnv = NULL;
  35. }
  36. } else
  37. mhSQLEnv = NULL;
  38. }
  39. return(SQLResult);
  40. }
  41. SQLRETURN CSQLWrap::Initialize(PCHAR szDSN)
  42. {
  43. SQLRETURN SQLResult;
  44. SQLResult = this->InitializeEnvironment();
  45. //
  46. // Open a connection to the ODBC DSN.
  47. //
  48. if ((SQL_NULL_HDBC == mhSQLConn) && SQL_OK(SQLResult))
  49. {
  50. SQLResult = SQLAllocHandle(SQL_HANDLE_DBC,
  51. mhSQLEnv,
  52. &mhSQLConn);
  53. if (SQL_OK(SQLResult))
  54. {
  55. SQLResult = SQLConnectA(mhSQLConn,
  56. (SQLCHAR *) szDSN,
  57. SQL_NTS,
  58. (SQLCHAR *) "",
  59. SQL_NTS,
  60. (SQLCHAR *) "",
  61. SQL_NTS);
  62. if (!SQL_OK(SQLResult))
  63. {
  64. this->TrapError(mhSQLEnv, mhSQLConn, SQL_NULL_HSTMT);
  65. SQLFreeHandle(SQL_HANDLE_DBC, mhSQLConn);
  66. mhSQLConn = SQL_NULL_HDBC;
  67. }
  68. } else
  69. mhSQLConn = SQL_NULL_HDBC;
  70. }
  71. return(SQLResult);
  72. }
  73. SQLRETURN CSQLWrap::Initialize(PWCHAR szDSN)
  74. {
  75. SQLRETURN SQLResult;
  76. SQLResult = this->InitializeEnvironment();
  77. //
  78. // Open a connection to the ODBC DSN.
  79. //
  80. if ((SQL_NULL_HDBC == mhSQLConn) && (SQL_SUCCESS == SQLResult))
  81. {
  82. SQLResult = SQLAllocHandle(SQL_HANDLE_DBC,
  83. mhSQLEnv,
  84. &mhSQLConn);
  85. if (SQL_OK(SQLResult))
  86. {
  87. SQLResult = SQLConnectW(mhSQLConn,
  88. (SQLWCHAR *) szDSN,
  89. SQL_NTS,
  90. (SQLWCHAR *) L"",
  91. SQL_NTS,
  92. (SQLWCHAR *) L"",
  93. SQL_NTS);
  94. if (!SQL_OK(SQLResult))
  95. {
  96. this->TrapError(mhSQLEnv, mhSQLConn, SQL_NULL_HSTMT);
  97. SQLFreeHandle(SQL_HANDLE_DBC, mhSQLConn);
  98. mhSQLConn = SQL_NULL_HDBC;
  99. }
  100. } else
  101. mhSQLConn = SQL_NULL_HDBC;
  102. }
  103. return(SQLResult);
  104. }
  105. SQLRETURN CSQLWrap::Terminate()
  106. {
  107. SQLRETURN SQLResult;
  108. SQLResult = SQL_SUCCESS;
  109. if (SQL_NULL_HDBC != mhSQLConn)
  110. {
  111. SQLResult = SQLDisconnect(mhSQLConn);
  112. if (SQL_OK(SQLResult))
  113. {
  114. SQLResult = SQLFreeHandle(SQL_HANDLE_DBC,
  115. mhSQLConn);
  116. if (SQL_OK(SQLResult))
  117. mhSQLConn = SQL_NULL_HDBC;
  118. else
  119. this->TrapError(mhSQLEnv, mhSQLConn, SQL_NULL_HSTMT);
  120. } else
  121. this->TrapError(mhSQLEnv, mhSQLConn, SQL_NULL_HSTMT);
  122. }
  123. if ((NULL != mhSQLEnv) && SQL_OK(SQLResult))
  124. {
  125. SQLResult = SQLFreeHandle(SQL_HANDLE_ENV,
  126. mhSQLEnv);
  127. if (SQL_OK(SQLResult))
  128. mhSQLEnv = NULL;
  129. else
  130. this->TrapError(mhSQLEnv, SQL_NULL_HDBC, SQL_NULL_HSTMT);
  131. }
  132. return(SQLResult);
  133. }
  134. SQLRETURN CSQLWrap::BindOutputParameter(SQLHSTMT hStatement,
  135. SQL_PARAMETER_LIST *pParameter,
  136. SQLUSMALLINT *pusmiColumns)
  137. {
  138. SQLRETURN SQLResult;
  139. SQLResult = SQLBindCol(
  140. hStatement,
  141. ++(*pusmiColumns),
  142. pParameter->ValueType,
  143. pParameter->ParameterValuePtr,
  144. pParameter->BufferLength,
  145. pParameter->StrLen_or_IndPtr);
  146. return(SQLResult);
  147. }
  148. SQLRETURN CSQLWrap::FinishExecute(SQLHSTMT hStatement,
  149. SQL_PARAMETER_LIST *pParameter,
  150. BOOL *pfReturnParameters)
  151. {
  152. SQLUSMALLINT usmiParameters, usmiColumns;
  153. SQLRETURN SQLResult;
  154. DWORD dwIndex;
  155. *pfReturnParameters = FALSE;
  156. if (NULL != pParameter)
  157. {
  158. usmiParameters = usmiColumns = 0;
  159. for(dwIndex = 0;
  160. NULL != pParameter[dwIndex].ParameterValuePtr;
  161. dwIndex++)
  162. {
  163. switch(pParameter[dwIndex].InputOutputType)
  164. {
  165. case SQL_PARAM_INPUT:
  166. SQLResult = SQLBindParameter(
  167. hStatement,
  168. ++usmiParameters,
  169. SQL_PARAM_INPUT,
  170. pParameter[dwIndex].ValueType,
  171. pParameter[dwIndex].ParameterType,
  172. pParameter[dwIndex].ColumnDigits,
  173. (SQLSMALLINT) pParameter[dwIndex].ColumnDigits,
  174. pParameter[dwIndex].ParameterValuePtr,
  175. pParameter[dwIndex].BufferLength,
  176. pParameter[dwIndex].StrLen_or_IndPtr);
  177. break;
  178. case SQL_PARAM_OUTPUT:
  179. SQLResult = this->BindOutputParameter(
  180. hStatement,
  181. &pParameter[dwIndex],
  182. &usmiColumns);
  183. *pfReturnParameters = TRUE;
  184. break;
  185. default:
  186. //
  187. // BUGBUG: Set an error here.
  188. //
  189. break;
  190. }
  191. if (!SQL_OK(SQLResult))
  192. goto END_LABEL;
  193. }
  194. }
  195. //
  196. // Execute the statement.
  197. //
  198. SQLResult = SQLExecute(hStatement);
  199. END_LABEL:
  200. if (!SQL_OK(SQLResult))
  201. this->TrapError(mhSQLEnv, mhSQLConn, hStatement);
  202. return(SQLResult);
  203. }
  204. SQLRETURN CSQLWrap::Execute(PCHAR szStatement, SQL_PARAMETER_LIST *pParameter,
  205. SQLHSTMT * phStatement)
  206. {
  207. SQLRETURN SQLResult;
  208. SQLHSTMT hStatement;
  209. BOOL fReturnParameters;
  210. if (NULL != phStatement)
  211. *phStatement = SQL_NULL_HSTMT;
  212. hStatement = SQL_NULL_HSTMT;
  213. //
  214. // Need to create a statement.
  215. //
  216. SQLResult = SQLAllocHandle(SQL_HANDLE_STMT, mhSQLConn, &hStatement);
  217. if (!SQL_OK(SQLResult))
  218. goto ERROR_LABEL;
  219. //
  220. // Need to prepare the statement.
  221. //
  222. SQLResult = SQLPrepareA(hStatement,
  223. (SQLCHAR *) szStatement,
  224. SQL_NTS);
  225. if (!SQL_OK(SQLResult))
  226. goto ERROR_LABEL;
  227. //
  228. // Time to add parameters to the statement.
  229. //
  230. SQLResult = this->FinishExecute(hStatement,
  231. pParameter,
  232. &fReturnParameters);
  233. if (!SQL_OK(SQLResult))
  234. goto ERROR_LABEL;
  235. goto END_LABEL;
  236. ERROR_LABEL:
  237. this->TrapError(mhSQLEnv, mhSQLConn, hStatement);
  238. if (SQL_NULL_HSTMT != hStatement)
  239. {
  240. SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
  241. hStatement = SQL_NULL_HSTMT;
  242. }
  243. END_LABEL:
  244. if (NULL != phStatement)
  245. *phStatement = hStatement;
  246. else if (SQL_NULL_HSTMT != hStatement)
  247. SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
  248. return(SQLResult);
  249. }
  250. SQLRETURN CSQLWrap::Execute(PWCHAR szStatement, SQL_PARAMETER_LIST *pParameter,
  251. SQLHSTMT * phStatement)
  252. {
  253. SQLRETURN SQLResult;
  254. SQLHSTMT hStatement;
  255. BOOL fReturnParameters;
  256. if (NULL != phStatement)
  257. *phStatement = SQL_NULL_HSTMT;
  258. hStatement = SQL_NULL_HSTMT;
  259. //
  260. // Need to create a statement.
  261. //
  262. SQLResult = SQLAllocHandle(SQL_HANDLE_STMT, mhSQLConn, &hStatement);
  263. if (!SQL_OK(SQLResult))
  264. goto ERROR_LABEL;
  265. //
  266. // Need to prepare the statement.
  267. //
  268. SQLResult = SQLPrepareW(hStatement,
  269. (SQLWCHAR *) szStatement,
  270. SQL_NTS);
  271. if (!SQL_OK(SQLResult))
  272. goto ERROR_LABEL;
  273. //
  274. // Time to add parameters to the statement.
  275. //
  276. SQLResult = this->FinishExecute(hStatement,
  277. pParameter,
  278. &fReturnParameters);
  279. if (!SQL_OK(SQLResult))
  280. goto ERROR_LABEL;
  281. goto END_LABEL;
  282. ERROR_LABEL:
  283. this->TrapError(mhSQLEnv, mhSQLConn, hStatement);
  284. if (SQL_NULL_HSTMT != hStatement)
  285. {
  286. SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
  287. hStatement = SQL_NULL_HSTMT;
  288. }
  289. END_LABEL:
  290. if (NULL != phStatement)
  291. *phStatement = hStatement;
  292. else if (SQL_NULL_HSTMT != hStatement)
  293. SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
  294. return(SQLResult);
  295. }
  296. SQLRETURN CSQLWrap::BindReturnParameters(SQLHSTMT hStatement,
  297. SQL_PARAMETER_LIST *pParameter)
  298. {
  299. SQLRETURN SQLResult;
  300. DWORD dwIndex;
  301. SQLUSMALLINT usmiColumns;
  302. SQLResult = SQL_SUCCESS;
  303. usmiColumns = 0;
  304. for(dwIndex = 0;
  305. NULL != pParameter[dwIndex].ParameterValuePtr;
  306. dwIndex++)
  307. {
  308. if (pParameter[dwIndex].InputOutputType != SQL_PARAM_OUTPUT)
  309. {
  310. SQLResult = SQL_ERROR;
  311. goto END_LABEL;
  312. }
  313. SQLResult = this->BindOutputParameter(hStatement,
  314. &pParameter[dwIndex],
  315. &usmiColumns);
  316. if (!SQL_OK(SQLResult))
  317. goto END_LABEL;
  318. }
  319. END_LABEL:
  320. if (!SQL_OK(SQLResult))
  321. this->TrapError(mhSQLEnv, mhSQLConn, hStatement);
  322. return(SQLResult);
  323. }
  324. SQLRETURN CSQLWrap::FetchRow(SQLHSTMT hStatement)
  325. {
  326. SQLRETURN SQLResult;
  327. SQLResult = SQLFetch(hStatement);
  328. if (!SQL_OK(SQLResult))
  329. this->TrapError(mhSQLEnv, mhSQLConn, hStatement);
  330. return(SQLResult);
  331. }
  332. SQLRETURN CSQLWrap::EndStatement(SQLHSTMT hStatement)
  333. {
  334. SQLRETURN SQLResult;
  335. SQLResult = SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
  336. return(SQLResult);
  337. }
  338. VOID CSQLWrap::TrapError(SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStmt)
  339. {
  340. SWORD swLen;
  341. SQLErrorW(hEnv,
  342. hDbc,
  343. hStmt,
  344. mwszErrorState,
  345. &msdwErrorCode,
  346. mwszErrorMessage,
  347. SQLWRAP_ERROR_MSG_LENGTH,
  348. &swLen);
  349. }
  350. VOID CSQLWrap::GetError(WCHAR * szError, DWORD cbLength)
  351. {
  352. DWORD cchLength;
  353. cchLength = (cbLength / sizeof(WCHAR)) - 1;
  354. wcsncpy(szError, mwszErrorMessage, cchLength);
  355. szError[cchLength] = L'\0';
  356. }
  357. VOID CSQLWrap::GetError(CHAR * szError, DWORD cbLength)
  358. {
  359. INT i;
  360. i = WideCharToMultiByte(CP_ACP,
  361. 0,
  362. mwszErrorMessage,
  363. -1,
  364. szError,
  365. cbLength,
  366. NULL,
  367. NULL);
  368. szError[cbLength - 1] = '\0';
  369. }
  370. INT CSQLWrap::TerminateString(CHAR * szString, INT iLength)
  371. {
  372. if (iLength <= 0)
  373. iLength = 0;
  374. szString[iLength] = '\0';
  375. return(iLength + 1);
  376. }
  377. INT CSQLWrap::TerminateString(WCHAR * szString, INT iLength)
  378. {
  379. if (iLength <= 0)
  380. iLength = 0;
  381. szString[iLength] = L'\0';
  382. return(iLength + 1);
  383. }