SQLWrap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #ifndef _SQLWRAP_H_
  2. #define _SQLWRAP_H_
  3. //
  4. // Includes needed for SQL.
  5. //
  6. #include <sql.h>
  7. #include <sqlext.h>
  8. //
  9. // The parameter list structure. Arrays of this structure are passed to
  10. // CSQLWrap::Execute and CSQLWrap::BindReturnParameters.
  11. //
  12. typedef struct _SQL_PARAMETER_LIST
  13. {
  14. SQLSMALLINT InputOutputType;
  15. SQLSMALLINT ValueType;
  16. SQLSMALLINT ParameterType;
  17. SQLUINTEGER ColumnDigits;
  18. SQLPOINTER ParameterValuePtr;
  19. SQLINTEGER BufferLength;
  20. SQLINTEGER * StrLen_or_IndPtr;
  21. } SQL_PARAMETER_LIST, *PSQL_PARAMETER_LIST;
  22. //
  23. // A NULL SQL parameter (indicates end of parameter list).
  24. //
  25. #define SQL_PNULL \
  26. { \
  27. 0, \
  28. 0, \
  29. 0, \
  30. 0, \
  31. NULL, \
  32. 0, \
  33. NULL \
  34. }
  35. //
  36. // Generic macros used by the parameters.
  37. //
  38. #define SQL_P_INT4(ParamInOut, Buffer, pSQLIntLen) \
  39. { \
  40. ParamInOut, \
  41. SQL_C_SLONG, \
  42. SQL_INTEGER, \
  43. 0, \
  44. &(Buffer), \
  45. 4, \
  46. pSQLIntLen \
  47. }
  48. #define SQL_P_INT2(ParamInOut, Buffer, pSQLIntLen) \
  49. { \
  50. ParamInOut, \
  51. SQL_C_SSHORT, \
  52. SQL_SMALLINT, \
  53. 0, \
  54. &(Buffer), \
  55. 2, \
  56. pSQLIntLen \
  57. }
  58. #define SQL_P_INT1(ParamInOut, Buffer, pSQLIntLen) \
  59. { \
  60. ParamInOut, \
  61. SQL_C_STINYINT, \
  62. SQL_TINYINT, \
  63. 0, \
  64. &(Buffer), \
  65. 1, \
  66. pSQLIntLen \
  67. }
  68. #define SQL_P_FLOAT4(ParamInOut, Buffer, pSQLIntLen) \
  69. { \
  70. ParamInOut, \
  71. SQL_C_FLOAT, \
  72. SQL_FLOAT, \
  73. 0, \
  74. &(Buffer), \
  75. 4, \
  76. pSQLIntLen \
  77. }
  78. #define SQL_P_CHAR(ParamInOut, Buffer, iLen, pSQLIntLen) \
  79. { \
  80. ParamInOut, \
  81. SQL_C_CHAR, \
  82. SQL_CHAR, \
  83. 0, \
  84. Buffer, \
  85. (iLen), \
  86. pSQLIntLen \
  87. }
  88. #define SQL_P_WCHAR(ParamInOut, Buffer, iLen, pSQLIntLen) \
  89. { \
  90. ParamInOut, \
  91. SQL_C_WCHAR, \
  92. SQL_WCHAR, \
  93. 0, \
  94. Buffer, \
  95. (iLen), \
  96. pSQLIntLen \
  97. }
  98. //
  99. // Macros for converting from SQL types to C types on returned data.
  100. //
  101. #define SQL_PIN_INT4(Buf) SQL_P_INT4(SQL_PARAM_INPUT, Buf, NULL)
  102. #define SQL_POUT_INT4(Buf, Len) SQL_P_INT4(SQL_PARAM_OUTPUT, Buf, &Len)
  103. #define SQL_PIN_INT2(Buf) SQL_P_INT2(SQL_PARAM_INPUT, Buf, NULL)
  104. #define SQL_POUT_INT2(Buf, Len) SQL_P_INT2(SQL_PARAM_OUTPUT, Buf, &Len)
  105. #define SQL_PIN_INT1(Buf) SQL_P_INT1(SQL_PARAM_INPUT, Buf, NULL)
  106. #define SQL_POUT_INT1(Buf, Len) SQL_P_INT1(SQL_PARAM_OUTPUT, Buf, &Len)
  107. #define SQL_PIN_FLOAT4(Buf) SQL_P_FLOAT4(SQL_PARAM_INPUT, Buf, NULL)
  108. #define SQL_POUT_FLOAT4(Buf, Len) SQL_P_FLOAT4(SQL_PARAM_OUTPUT, Buf, &Len)
  109. #define SQL_PIN_CHAR(Buf, iLen) \
  110. SQL_P_CHAR(SQL_PARAM_INPUT, Buf, iLen, NULL)
  111. #define SQL_POUT_CHAR(Buf, iLen, oLen) \
  112. SQL_P_CHAR(SQL_PARAM_OUTPUT, Buf, iLen, &oLen)
  113. #define SQL_PIN_WCHAR(Buf, iLen) \
  114. SQL_P_WCHAR(SQL_PARAM_INPUT, Buf, iLen, NULL)
  115. #define SQL_POUT_WCHAR(Buf, iLen, oLen) \
  116. SQL_P_WCHAR(SQL_PARAM_OUTPUT, Buf, iLen, &oLen)
  117. /*
  118. #define SQL_POUT_INT4(Buffer, SQLIntLen) \
  119. { \
  120. SQL_PARAM_OUTPUT, \
  121. SQL_C_SLONG, \
  122. 0, \
  123. 0, \
  124. &(Buffer), \
  125. 4, \
  126. &(SQLIntLen) \
  127. }
  128. #define SQL_POUT_INT2(Buffer, SQLIntLen) \
  129. { \
  130. SQL_PARAM_OUTPUT, \
  131. SQL_C_SSHORT, \
  132. 0, \
  133. 0, \
  134. &(Buffer), \
  135. 2, \
  136. &(SQLIntLen) \
  137. }
  138. #define SQL_POUT_INT1(Buffer, SQLIntLen) \
  139. { \
  140. SQL_PARAM_OUTPUT, \
  141. SQL_C_STINYINT, \
  142. 0, \
  143. 0, \
  144. &(Buffer), \
  145. 1, \
  146. &(SQLIntLen) \
  147. }
  148. #define SQL_POUT_FLOAT4(Buffer, SQLIntLen) \
  149. { \
  150. SQL_PARAM_OUTPUT, \
  151. SQL_C_FLOAT, \
  152. 0, \
  153. 0, \
  154. &(Buffer), \
  155. 4, \
  156. &(SQLIntLen) \
  157. }
  158. #define SQL_POUT_CHAR(Buffer, iLen, SQLIntLen) \
  159. { \
  160. SQL_PARAM_OUTPUT, \
  161. SQL_C_CHAR, \
  162. 0, \
  163. 0, \
  164. Buffer, \
  165. iLen, \
  166. &(SQLIntLen) \
  167. }
  168. */
  169. #define SQL_OK(SQLRet) (SQL_SUCCEEDED(SQLRet))
  170. #define SQLWRAP_ERROR_MSG_LENGTH 2048
  171. class CSQLWrap
  172. {
  173. private:
  174. SQLHENV mhSQLEnv;
  175. SQLHDBC mhSQLConn;
  176. WCHAR mwszErrorState[SQLWRAP_ERROR_MSG_LENGTH + 2];
  177. WCHAR mwszErrorMessage[SQLWRAP_ERROR_MSG_LENGTH + 2];
  178. SDWORD msdwErrorCode;
  179. SQLRETURN InitializeEnvironment();
  180. SQLRETURN BindOutputParameter(SQLHSTMT hStatement,
  181. SQL_PARAMETER_LIST *pParameter,
  182. SQLUSMALLINT *pusmiColumns);
  183. SQLRETURN FinishExecute(SQLHSTMT hStatement,
  184. SQL_PARAMETER_LIST *pParameter,
  185. BOOL *pfReturnParameters);
  186. VOID TrapError(SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStmt);
  187. public:
  188. CSQLWrap();
  189. virtual ~CSQLWrap();
  190. SQLRETURN Initialize(PCHAR szDSN);
  191. SQLRETURN Initialize(PWCHAR szDSN);
  192. SQLRETURN Terminate();
  193. SQLRETURN Execute(PCHAR szStatement, SQL_PARAMETER_LIST *pParameter,
  194. SQLHSTMT * phStatement);
  195. SQLRETURN Execute(PWCHAR szStatement, SQL_PARAMETER_LIST *pParameter,
  196. SQLHSTMT * phStatement);
  197. SQLRETURN BindReturnParameters(SQLHSTMT hStatement,
  198. SQL_PARAMETER_LIST *pParameter);
  199. SQLRETURN FetchRow(SQLHSTMT hStatement);
  200. SQLRETURN EndStatement(SQLHSTMT hStatement);
  201. VOID GetError(WCHAR * szError, DWORD cbLength);
  202. VOID GetError(CHAR * szError, DWORD cbLength);
  203. INT TerminateString(CHAR * szString, INT iLength);
  204. INT TerminateString(WCHAR * szString, INT iLength);
  205. };
  206. //
  207. // Method documentation follows:
  208. //
  209. //
  210. // SQLRETURN CSQLWrap::Initialize(LPTSTR szDSN)
  211. //
  212. // Description:
  213. // This method initializes the CSQLWrap class.
  214. //
  215. // Parameters:
  216. // szDSN [IN] - Name of the system DSN to connect to (ASCII or Unicode).
  217. //
  218. // Returns:
  219. // SQL_SUCCESS or SQL_SUCCESS_WITH_INFO on success.
  220. // SQL specific error code on failure.
  221. //
  222. //
  223. // SQLRETURN CSQLWrap::Terminate()
  224. //
  225. // Description:
  226. // This method cleans up all internal data being used by the
  227. // CSQLWrap class.
  228. //
  229. // Returns:
  230. // SQL_SUCCESS or SQL_SUCCESS_WITH_INFO on success.
  231. // SQL specific error code on failure.
  232. //
  233. //
  234. // SQLRETURN CSQLWrap::Execute(LPTSTR szStatement,
  235. // SQL_PARAMETER_LIST *pParameter,
  236. // SQLHSTMT * phStatement)
  237. //
  238. // Description:
  239. // This method executes a SQL query or statement using the passed
  240. // parameters and returns a SQLHSTMT for the statement.
  241. //
  242. // Parameters:
  243. // szStatement [IN] - String containing the SQL query of statement
  244. // (ASCII or Unicode).
  245. // pParameter [IN] - Array of parameters to use in statement.
  246. // phStatement [OUT] - SQL HSTMT returned for statement.
  247. //
  248. // Returns:
  249. // SQL_SUCCESS or SQL_SUCCESS_WITH_INFO on success.
  250. // SQL specific error code on failure.
  251. //
  252. // Notes:
  253. // The SQL_PIN_* and SQL_POUT_* macros provided above should be
  254. // used when defining an array of parameters to pass to this
  255. // function. The delcaration should look like:
  256. // INT i, j;
  257. // INT Len[2];
  258. //
  259. // SQL_PARAMETER_LIST List[] =
  260. // {
  261. // SQL_POUT_INT4(i, Len[0]),
  262. // SQL_POUT_INT4(j, Len[1]),
  263. // SQL_PNULL
  264. // } ;
  265. //
  266. //
  267. // SQLRETURN BindReturnParameters(SQLHSTMT hStatement,
  268. // SQL_PARAMETER_LIST *pParameter)
  269. //
  270. // Description:
  271. // This method binds the return parameters for the given SQL statement.
  272. //
  273. // Parameters:
  274. // hStatement [IN] - HSTMT of an existing SQL statement.
  275. // pParameter [IN] - Array of parameters to use in statement.
  276. //
  277. // Returns:
  278. // SQL_SUCCESS or SQL_SUCCESS_WITH_INFO on success.
  279. // SQL specific error code on failure.
  280. //
  281. // Notes:
  282. // This function need not be called if the SQL out parameters have
  283. // been specified in the call to CSQLWrap::Execute. This method
  284. // simply provides a way to change the parameter list between
  285. // row entries (for filling an array for example).
  286. //
  287. //
  288. // SQLRETURN FetchRow(SQLHSTMT hStatement)
  289. //
  290. // Description:
  291. // This method fetches the next row for the given statement,
  292. // automatically putting any bound parameters into their destintation
  293. // buffers.
  294. //
  295. // Parameters:
  296. // hStatement [IN] - HSTMT of an existing SQL statement.
  297. //
  298. // Returns:
  299. // SQL_SUCCESS or SQL_SUCCESS_WITH_INFO on success.
  300. // SQL_NO_DATA if no rows remain.
  301. // SQL specific error code on failure.
  302. //
  303. //
  304. // SQLRETURN EndStatement(SQLHSTMT hStatement)
  305. //
  306. // Description:
  307. // This method frees the SQL HSTMT and ends the statement.
  308. //
  309. // Parameters:
  310. // hStatement [MODIFY] - HSTMT of an existing SQL statement.
  311. //
  312. // Returns:
  313. // SQL_SUCCESS or SQL_SUCCESS_WITH_INFO on success.
  314. // SQL specific error code on failure.
  315. //
  316. //
  317. // VOID GetError(LPTSTR szError, DWORD cbLength)
  318. //
  319. // Description:
  320. // This function puts the last SQL error message into the given
  321. // buffer. If the buffer is not long enough, the message is
  322. // truncated to fit.
  323. //
  324. // Parameters:
  325. // szError [MODIFY] - Buffer to place the error message
  326. // (ASCII or Unicode).
  327. // cbLength [IN] - Size of the buffer in bytes.
  328. //
  329. //
  330. // INT TerminateString(LPTSTR szString, INT iLength)
  331. //
  332. // Description:
  333. // This helper function NULL terminates the given string based on the
  334. // length during the CSQLWrap::FetchRow() method.
  335. //
  336. // Parameters:
  337. // szString [MODIFY] - String to terminate (ASCII or Unicode).
  338. // iLength [IN] - Length of the string returned by SQL.
  339. //
  340. // Returns:
  341. // The length of the string in characters including the trailing NULL.
  342. //
  343. #endif