Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

433 lines
12 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name :
  4. odbcconn.hxx
  5. Abstract:
  6. This module declares the class ODBC_CONNECTION used for odbc
  7. connectivity. It also declares the class ODBC_PARAMETER for
  8. parameter markers used for insertion of data.
  9. Author:
  10. Murali R. Krishnan ( MuraliK ) 16-Feb-1995
  11. Environment:
  12. User Mode
  13. Project:
  14. Internet Services Common DLL
  15. Revision History:
  16. MuraliK 08-Jan-1995 Split Dynamic Load ODBC module.
  17. --*/
  18. # ifndef _ODBCCONN_HXX_
  19. # define _ODBCCONN_HXX_
  20. /************************************************************
  21. * Include Headers
  22. ************************************************************/
  23. # include <string.hxx>
  24. //
  25. // SQL-ODBC interface headers
  26. //
  27. # include "sqltypes.h"
  28. # include "sql.h"
  29. # include "sqlext.h"
  30. /************************************************************
  31. * Dynamic Load support
  32. ************************************************************/
  33. extern "C" {
  34. # include "dynodbc.h"
  35. };
  36. //
  37. // Support Old users of LoadODBC
  38. //
  39. inline BOOL
  40. LoadODBC(VOID)
  41. {
  42. return ( DynLoadODBC());
  43. } // LoadODBC()
  44. /************************************************************
  45. * Type Definitions
  46. ************************************************************/
  47. /*++
  48. class ODBC_PARAMETER
  49. This class encapsulates data related to ODBC parameter markers
  50. to be used for binding purposes.
  51. Information about a parameter include:
  52. Parameter Number Index of the parameter in the columns of table.
  53. Parameter Type Indicates direction of data tfr ( In, Out, In/Out)
  54. C Type Specifies the C Equivalent data struct.
  55. Sql Type Specifies the SQL Equivalent data struct.
  56. Precision Gives the precision of the column
  57. Scale Gives the scale for display of parameter values
  58. Value Specifies the pointer to memory containing
  59. the value of the parameter
  60. MaxCbValue Specifies the maximum bytes of data that can
  61. be stored in the value buffer
  62. CbValue Provides a counter for bytes of data used
  63. up in the binding process.
  64. --*/
  65. class ODBC_PARAMETER {
  66. public:
  67. dllexp ODBC_PARAMETER( IN WORD iParameter,
  68. IN SWORD fParamType,
  69. IN SWORD CType,
  70. IN SWORD sqlType,
  71. IN UDWORD cbPrecision = 0)
  72. : m_iParameter ( iParameter),
  73. m_paramType ( fParamType),
  74. m_CType ( CType),
  75. m_SqlType ( sqlType),
  76. m_cbColPrecision( cbPrecision),
  77. m_ibScale ( 0),
  78. m_pValue ( NULL),
  79. m_cbValue ( 0),
  80. m_cbValueMax ( 0)
  81. {}
  82. dllexp ~ODBC_PARAMETER( VOID)
  83. { if ( m_pValue != NULL) { delete m_pValue; } }
  84. dllexp BOOL
  85. SetValueBuffer( IN SDWORD cbMaxSize, IN SDWORD cbValue)
  86. {
  87. m_pValue = (PTR ) new CHAR[ cbMaxSize]; // pointer to value buffer
  88. if ( m_pValue != NULL) {
  89. memset( m_pValue, 0, cbMaxSize);
  90. m_cbValueMax = cbMaxSize; // max size of buffer
  91. m_cbValue = cbValue; // current effective value
  92. }
  93. return ( m_pValue != NULL);
  94. } // SetValue()
  95. dllexp BOOL
  96. CopyValue( IN PVOID pvValue, IN SDWORD cbValue)
  97. {
  98. if ( cbValue <= m_cbValueMax) {
  99. memcpy( m_pValue, pvValue, cbValue);
  100. } else {
  101. SetLastError( ERROR_INSUFFICIENT_BUFFER);
  102. }
  103. return ( cbValue <= m_cbValueMax); // return true if we copied.
  104. } // CopyValue()
  105. dllexp BOOL
  106. CopyValue( IN LPCSTR pszValue)
  107. { // always copy including the null character
  108. return ( CopyValue( (PVOID ) pszValue, strlen( pszValue) + 1));
  109. }
  110. dllexp BOOL
  111. CopyValue( IN LPCWSTR pwszValue);
  112. dllexp BOOL
  113. CopyValue( IN LPSYSTEMTIME lpSystemTime);
  114. dllexp BOOL
  115. CopyValue( IN DWORD dwValue)
  116. { return ( CopyValue( (PVOID ) &dwValue, sizeof( DWORD))); }
  117. dllexp WORD QueryParamNumber( VOID) const { return ( m_iParameter); }
  118. dllexp SWORD QueryParamType ( VOID) const { return ( m_paramType); }
  119. dllexp SWORD QueryCType ( VOID) const { return ( m_CType); }
  120. dllexp SWORD QuerySqlType ( VOID) const { return ( m_SqlType); }
  121. dllexp UDWORD QueryPrecision ( VOID) const { return ( m_cbColPrecision); }
  122. dllexp SWORD QueryScale ( VOID) const { return ( m_ibScale); }
  123. dllexp PTR QueryValue ( VOID) const { return ( m_pValue); }
  124. dllexp SDWORD QueryMaxCbValue ( VOID) const { return ( m_cbValueMax); }
  125. dllexp SDWORD QueryCbValue ( VOID) const { return ( m_cbValue); }
  126. dllexp SDWORD &
  127. QueryCbValueRef( VOID)
  128. { return ( m_cbValue); } // return a reference to count of bytes rx.
  129. dllexp RETCODE Bind( IN HSTMT hStmt);
  130. # if DBG
  131. VOID Print( VOID) const;
  132. # endif // DBG
  133. private:
  134. WORD m_iParameter; // index or the parameter number
  135. SWORD m_paramType; // type of the parameter
  136. SWORD m_CType; // the C data type for this parameter
  137. SWORD m_SqlType; // the SQL data type for this parameter
  138. UDWORD m_cbColPrecision; // precision of the column
  139. SWORD m_ibScale; // scale of the column
  140. PTR m_pValue; // pointer to the value.
  141. SDWORD m_cbValueMax; // max bytes allowed in pValue.
  142. SDWORD m_cbValue; // count of bytes of value
  143. }; // class ODBC_PARAMETER
  144. typedef ODBC_PARAMETER * PODBC_PARAMETER;
  145. //
  146. // Forwards Declaration
  147. //
  148. class ODBC_CONNECTION;
  149. /*++
  150. class ODBC_STATEMENT:
  151. This class declares an interface for statements using ODBC connection.
  152. m_hstmt Statement used for execution.
  153. m_rc Return code for last ODBC call.
  154. m_fPreparedStmt is the statement prepared.
  155. --*/
  156. class ODBC_STATEMENT {
  157. public:
  158. ODBC_STATEMENT( IN ODBC_CONNECTION * pOdbcConnection,
  159. IN HSTMT hStmt)
  160. : m_hStmt ( hStmt),
  161. m_pOdbcConnection( pOdbcConnection),
  162. m_fPreparedStmt ( FALSE),
  163. m_rc ( SQL_SUCCESS),
  164. m_astrColNames ( NULL ),
  165. m_astrValues ( NULL ),
  166. m_acbValue ( NULL ),
  167. m_cCols ( 0 )
  168. {}
  169. dllexp ~ODBC_STATEMENT( VOID);
  170. dllexp RETCODE QueryErrorCode( VOID) const { return ( m_rc); }
  171. dllexp BOOL IsValid( VOID) const { return ( m_fPreparedStmt);}
  172. dllexp BOOL PrepareStatement( IN LPCSTR pszStatement);
  173. dllexp BOOL PrepareStatement( IN LPCWSTR pwszStatement);
  174. dllexp BOOL BindParameter( IN PODBC_PARAMETER pOdbcParam);
  175. dllexp BOOL ExecuteStatement( VOID); // Executes the prepared statement.
  176. dllexp BOOL ExecDirect( IN LPCSTR pszSqlCommand, IN DWORD cchSqlCommand);
  177. dllexp BOOL ExecDirect( IN LPCWSTR pszSqlCommand, IN DWORD cchSqlCommand);
  178. dllexp BOOL QueryRowCount( OUT DWORD * pRows );
  179. inline BOOL GetLastErrorText( OUT STR * pstrError );
  180. inline BOOL GetLastErrorTextAsHtml( OUT STR * pstrError );
  181. //
  182. // These allocate an array (0 to cCols-1) containing the row name of
  183. // the result column and the string value of the current position in
  184. // the result set. Each successive call to QueryValuesAsStr gets the next
  185. // row in the result set.
  186. //
  187. dllexp BOOL QueryColNames( OUT STR * * apstrCols,
  188. OUT DWORD * cCols,
  189. IN DWORD cchMaxFieldSize,
  190. OUT BOOL * pfIsSelect );
  191. dllexp BOOL QueryValuesAsStr( OUT STR * * apstrValues,
  192. OUT DWORD * * apcbValues,
  193. OUT BOOL * pfLast );
  194. dllexp BOOL MoreResults( BOOL * pfMoreResults );
  195. # if DBG
  196. VOID Print( VOID) const;
  197. # endif // DBG
  198. dllexp VOID FreeColumnMemory( VOID );
  199. private:
  200. // DATA
  201. ODBC_CONNECTION * m_pOdbcConnection; // back pointer to connection
  202. BOOL m_fPreparedStmt; // set after stmt is prepared.
  203. HSTMT m_hStmt;
  204. RETCODE m_rc;
  205. //
  206. // Contains buffers used by QueryColNames and QueryValuesAsStr
  207. //
  208. WORD m_cCols;
  209. STR * m_astrColNames;
  210. STR * m_astrValues; // Memory for destination of fetched data
  211. LONG * m_acbValue;
  212. // Array of byte counts of data placed in m_astrValues
  213. }; // ODBC_STATEMENT()
  214. typedef ODBC_STATEMENT * PODBC_STATEMENT;
  215. /*++
  216. class ODBC_CONNECTION:
  217. This class specifies a logical class to contain the ODBC nuances
  218. and encapsulates relevant data for using ODBC to talk to a
  219. database system.
  220. Data encapsulated includes:
  221. m_henv Environment handle for ODBC connection.
  222. m_hdbc Database connection handle.
  223. m_rc Return code for last ODBC call.
  224. --*/
  225. class ODBC_CONNECTION {
  226. public:
  227. dllexp ODBC_CONNECTION( VOID)
  228. : m_henv ( SQL_NULL_HENV),
  229. m_hdbc ( SQL_NULL_HDBC),
  230. m_fValid ( FALSE),
  231. m_rc ( SQL_SUCCESS)
  232. { }
  233. dllexp ~ODBC_CONNECTION( VOID);
  234. //
  235. // Returns the text for the last error that occurred
  236. //
  237. dllexp BOOL GetLastErrorText( OUT STR * pstrError,
  238. IN HSTMT hstmt = SQL_NULL_HSTMT,
  239. IN RETCODE rc = SQL_SUCCESS ) const;
  240. dllexp BOOL GetLastErrorTextAsHtml( OUT STR * pstrError,
  241. IN HSTMT hstmt = SQL_NULL_HSTMT,
  242. IN RETCODE rc = SQL_SUCCESS ) const;
  243. dllexp static BOOL Success( IN RETCODE rc)
  244. { return ( ( rc == SQL_SUCCESS) || ( rc == SQL_SUCCESS_WITH_INFO)); }
  245. dllexp BOOL IsValid( VOID) const { return ( m_fValid); }
  246. dllexp RETCODE QueryErrorCode( VOID) const { return ( m_rc); }
  247. dllexp BOOL Open(
  248. IN LPCSTR pszDataSource,
  249. IN LPCSTR pszUserName,
  250. IN LPCSTR pszPassword);
  251. dllexp BOOL Open(
  252. IN LPCWSTR pwszDataSource,
  253. IN LPCWSTR pwszUserName,
  254. IN LPCWSTR pwszPassword);
  255. dllexp BOOL Close( VOID);
  256. dllexp PODBC_STATEMENT AllocStatement( VOID);
  257. dllexp BOOL SetConnectOption( IN UWORD Option,
  258. IN SQLPOINTER Param);
  259. dllexp BOOL GetInfo( IN DWORD fInfoType,
  260. IN PVOID rgbInfoValue,
  261. IN DWORD cbInfoValueMax,
  262. IN OUT DWORD * pcbInfoValue);
  263. dllexp static DWORD DisplaySize( SWORD ColType, DWORD cchColLength );
  264. # if DBG
  265. VOID Print( VOID) const;
  266. # endif // DBG
  267. private:
  268. //
  269. // ODBC specific data members.
  270. //
  271. HENV m_henv;
  272. HDBC m_hdbc;
  273. RETCODE m_rc;
  274. BOOL m_fValid;
  275. }; // ODBC_CONNECTION
  276. typedef ODBC_CONNECTION * PODBC_CONNECTION;
  277. /************************************************************
  278. * Inline Functions
  279. ************************************************************/
  280. inline BOOL ODBC_STATEMENT::GetLastErrorText( OUT STR * pstrError )
  281. {
  282. return ( m_pOdbcConnection->GetLastErrorText( pstrError,
  283. m_hStmt,
  284. m_rc ));
  285. } // ODBC_STATEMENT::GetLastErrorText()
  286. inline BOOL ODBC_STATEMENT::GetLastErrorTextAsHtml( OUT STR * pstrError )
  287. {
  288. return ( m_pOdbcConnection->GetLastErrorTextAsHtml( pstrError,
  289. m_hStmt,
  290. m_rc ));
  291. } // ODBC_STATEMENT::GetLastErrorText()
  292. # endif // _ODBCCONN_HXX_
  293. /************************ End of File ***********************/
  294.