Leaked source code of windows server 2003
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.

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