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.

753 lines
13 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. //
  24. // SQL-ODBC interface headers
  25. //
  26. # include "sql.h"
  27. # include "sqlext.h"
  28. /************************************************************
  29. * Macros Definitions
  30. ************************************************************/
  31. //
  32. // Default connection pool timeout
  33. //
  34. #define IDC_POOL_TIMEOUT 30
  35. //
  36. // Support Old users of LoadODBC
  37. //
  38. inline
  39. BOOL
  40. LoadODBC(
  41. VOID
  42. )
  43. {
  44. return TRUE;
  45. } // LoadODBC()
  46. /************************************************************
  47. * Type Definitions
  48. ************************************************************/
  49. /*++
  50. class ODBC_PARAMETER
  51. This class encapsulates data related to ODBC parameter markers
  52. to be used for binding purposes.
  53. Information about a parameter include:
  54. Parameter Number - Index of the parameter in the columns of table.
  55. Parameter Type - Indicates direction of data tfr(In, Out, In/Out)
  56. C Type - Specifies the C Equivalent data struct.
  57. Sql Type - Specifies the SQL Equivalent data struct.
  58. Precision - Gives the precision of the column
  59. Scale - Gives the scale for display of parameter values
  60. Value - Specifies the pointer to memory containing
  61. the value of the parameter
  62. MaxCbValue - Specifies the maximum bytes of data that can
  63. be stored in the value buffer
  64. CbValue - Provides a counter for bytes of data used
  65. up in the binding process.
  66. --*/
  67. class ODBC_PARAMETER {
  68. public:
  69. ODBC_PARAMETER(
  70. IN WORD iParameter,
  71. IN SWORD fParamType,
  72. IN SWORD CType,
  73. IN SWORD sqlType,
  74. IN UDWORD cbPrecision = 0
  75. ) : m_iParameter ( iParameter ),
  76. m_paramType ( fParamType ),
  77. m_CType ( CType ),
  78. m_SqlType ( sqlType ),
  79. m_cbColPrecision( cbPrecision ),
  80. m_ibScale ( 0 ),
  81. m_pValue ( NULL ),
  82. m_cbValue ( 0 ),
  83. m_cbValueMax ( 0 )
  84. {}
  85. ~ODBC_PARAMETER(
  86. )
  87. {
  88. if ( m_pValue != NULL )
  89. {
  90. delete[] m_pValue;
  91. m_pValue = NULL;
  92. }
  93. }
  94. HRESULT
  95. SetValueBuffer(
  96. IN SDWORD cbMaxSize,
  97. IN SDWORD cbValue
  98. )
  99. {
  100. m_pValue = ( PTR ) new CHAR[ cbMaxSize ];
  101. if ( m_pValue != NULL)
  102. {
  103. memset( m_pValue, 0, cbMaxSize);
  104. m_cbValueMax = cbMaxSize;
  105. m_cbValue = cbValue;
  106. return S_OK;
  107. }
  108. return HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
  109. } // SetValueBuffer()
  110. HRESULT
  111. CopyValue(
  112. IN PVOID pvValue,
  113. IN SDWORD cbValue
  114. )
  115. {
  116. if ( cbValue <= m_cbValueMax )
  117. {
  118. memcpy( m_pValue, pvValue, cbValue );
  119. }
  120. else
  121. {
  122. return HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
  123. }
  124. return S_OK;
  125. } // CopyValue()
  126. HRESULT
  127. CopyValue(
  128. IN LPCSTR pszValue
  129. )
  130. {
  131. //
  132. // always copy including the null character
  133. //
  134. return ( CopyValue( ( PVOID ) pszValue,
  135. strlen( pszValue ) + 1 ) );
  136. }
  137. HRESULT
  138. CopyValue(
  139. IN LPCWSTR pwszValue
  140. );
  141. HRESULT
  142. CopyValue(
  143. IN LPSYSTEMTIME lpSystemTime
  144. );
  145. HRESULT
  146. CopyValue(
  147. IN DWORD dwValue
  148. )
  149. {
  150. return ( CopyValue( ( PVOID ) &dwValue,
  151. sizeof( DWORD ) ) );
  152. }
  153. WORD
  154. QueryParamNumber(
  155. VOID
  156. ) const
  157. {
  158. return ( m_iParameter );
  159. }
  160. SWORD
  161. QueryParamType(
  162. VOID
  163. ) const
  164. {
  165. return ( m_paramType );
  166. }
  167. SWORD
  168. QueryCType(
  169. VOID
  170. ) const
  171. {
  172. return ( m_CType );
  173. }
  174. SWORD
  175. QuerySqlType(
  176. VOID
  177. ) const
  178. {
  179. return ( m_SqlType);
  180. }
  181. UDWORD
  182. QueryPrecision(
  183. VOID
  184. ) const
  185. {
  186. return ( m_cbColPrecision );
  187. }
  188. SWORD
  189. QueryScale(
  190. VOID
  191. ) const
  192. {
  193. return ( m_ibScale );
  194. }
  195. PTR
  196. QueryValue(
  197. VOID
  198. ) const
  199. {
  200. return ( m_pValue );
  201. }
  202. SQLLEN
  203. QueryMaxCbValue(
  204. VOID
  205. ) const
  206. {
  207. return ( m_cbValueMax );
  208. }
  209. SQLLEN
  210. QueryCbValue(
  211. VOID
  212. ) const
  213. {
  214. return ( m_cbValue );
  215. }
  216. SQLLEN &
  217. QueryCbValueRef(
  218. VOID
  219. )
  220. {
  221. //
  222. // return a reference to count of bytes rx
  223. //
  224. return ( m_cbValue );
  225. }
  226. RETCODE
  227. Bind(
  228. IN HSTMT hStmt
  229. );
  230. #if DBG
  231. VOID
  232. Print(
  233. VOID
  234. ) const;
  235. #endif // DBG
  236. private:
  237. //
  238. // index or the parameter number
  239. //
  240. WORD m_iParameter;
  241. //
  242. // type of the parameter
  243. //
  244. SWORD m_paramType;
  245. //
  246. // the C data type for this parameter
  247. //
  248. SWORD m_CType;
  249. //
  250. // the SQL data type for this parameter
  251. //
  252. SWORD m_SqlType;
  253. //
  254. // precision of the column
  255. //
  256. UDWORD m_cbColPrecision;
  257. //
  258. // scale of the column
  259. //
  260. SWORD m_ibScale;
  261. //
  262. // pointer to the value
  263. //
  264. PTR m_pValue;
  265. //
  266. // max bytes allowed in pValue
  267. //
  268. SQLLEN m_cbValueMax;
  269. //
  270. // count of bytes of value
  271. //
  272. SQLLEN m_cbValue;
  273. }; // class ODBC_PARAMETER
  274. typedef ODBC_PARAMETER * PODBC_PARAMETER;
  275. //
  276. // Forwards Declaration
  277. //
  278. class ODBC_CONNECTION;
  279. /*++
  280. class ODBC_STATEMENT:
  281. This class declares an interface for statements using ODBC
  282. connection.
  283. m_hstmt Statement used for execution
  284. m_rc Return code for last ODBC call
  285. m_fPreparedStmt is the statement prepared
  286. --*/
  287. #define ODBC_STATEMENT_SIGNATURE 'TSDO'
  288. #define ODBC_STATEMENT_FREE_SIGNATURE 'fSDO'
  289. class ODBC_STATEMENT {
  290. public:
  291. ODBC_STATEMENT(
  292. IN ODBC_CONNECTION * pOdbcConnection,
  293. IN HSTMT hStmt
  294. ) : m_dwSignature ( ODBC_STATEMENT_SIGNATURE ),
  295. m_hStmt ( hStmt ),
  296. m_pOdbcConnection( pOdbcConnection ),
  297. m_fPreparedStmt ( FALSE ),
  298. m_rc ( SQL_SUCCESS ),
  299. m_astrColNames ( NULL ),
  300. m_astrValues ( NULL ),
  301. m_acbValue ( NULL ),
  302. m_cCols ( 0 )
  303. {}
  304. ~ODBC_STATEMENT();
  305. BOOL
  306. CheckSignature(
  307. VOID
  308. ) const
  309. {
  310. return m_dwSignature == ODBC_STATEMENT_SIGNATURE;
  311. }
  312. RETCODE
  313. QueryErrorCode(
  314. VOID
  315. ) const
  316. {
  317. return ( m_rc );
  318. }
  319. BOOL
  320. IsValid(
  321. VOID
  322. ) const
  323. {
  324. return ( m_fPreparedStmt );
  325. }
  326. HRESULT
  327. PrepareStatement(
  328. IN LPCSTR pszStatement
  329. );
  330. HRESULT
  331. PrepareStatement(
  332. IN LPCWSTR pwszStatement
  333. );
  334. HRESULT
  335. BindParameter(
  336. IN PODBC_PARAMETER pOdbcParam
  337. );
  338. //
  339. // Executes the prepared statement
  340. //
  341. HRESULT
  342. ExecuteStatement(
  343. VOID
  344. );
  345. HRESULT
  346. ExecDirect(
  347. IN LPCSTR pszSqlCommand,
  348. IN DWORD cchSqlCommand
  349. );
  350. HRESULT
  351. ExecDirect(
  352. IN LPCWSTR pszSqlCommand,
  353. IN DWORD cchSqlCommand
  354. );
  355. HRESULT
  356. QueryRowCount(
  357. OUT SQLLEN *pRows
  358. );
  359. inline
  360. BOOL
  361. GetLastErrorText(
  362. OUT STRA * pstrError
  363. );
  364. inline
  365. BOOL
  366. GetLastErrorTextAsHtml(
  367. OUT STRA * pstrError
  368. );
  369. //
  370. // These allocate an array (0 to cCols-1) containing the row
  371. // name of the result column and the string value of the current
  372. // position in the result set. Each successive call to
  373. // QueryValuesAsStr gets the next row in the result set.
  374. //
  375. HRESULT
  376. QueryColNames(
  377. OUT STRA * * apstrCols,
  378. OUT DWORD * cCols,
  379. IN DWORD cchMaxFieldSize,
  380. OUT BOOL * pfIsSelect
  381. );
  382. HRESULT
  383. QueryValuesAsStr(
  384. OUT STRA * * apstrValues,
  385. OUT DWORD * * apcbValues,
  386. OUT BOOL * pfLast
  387. );
  388. HRESULT
  389. MoreResults(
  390. BOOL * pfMoreResults
  391. );
  392. # if DBG
  393. VOID
  394. Print(
  395. VOID
  396. ) const;
  397. # endif // DBG
  398. VOID FreeColumnMemory( VOID );
  399. VOID *
  400. operator new(
  401. size_t size
  402. )
  403. {
  404. DBG_ASSERT( size == sizeof( ODBC_STATEMENT ) );
  405. DBG_ASSERT( sm_pachOdbcStatements != NULL );
  406. return sm_pachOdbcStatements->Alloc();
  407. }
  408. VOID
  409. operator delete(
  410. VOID * pOdbcStatement
  411. )
  412. {
  413. DBG_ASSERT( pOdbcStatement != NULL );
  414. DBG_ASSERT( sm_pachOdbcStatements != NULL );
  415. DBG_REQUIRE( sm_pachOdbcStatements->Free( pOdbcStatement ) );
  416. }
  417. static
  418. HRESULT
  419. Initialize(
  420. VOID
  421. );
  422. static
  423. VOID
  424. Terminate(
  425. VOID
  426. );
  427. private:
  428. //
  429. // Signature of the class
  430. //
  431. DWORD m_dwSignature;
  432. //
  433. // back pointer to connection
  434. //
  435. ODBC_CONNECTION * m_pOdbcConnection;
  436. //
  437. // set after stmt is prepared
  438. //
  439. BOOL m_fPreparedStmt;
  440. HSTMT m_hStmt;
  441. RETCODE m_rc;
  442. //
  443. // Contains buffers used by QueryColNames and QueryValuesAsStr
  444. //
  445. WORD m_cCols;
  446. STRA * m_astrColNames;
  447. //
  448. // Memory for destination of fetched data
  449. //
  450. STRA * m_astrValues;
  451. //
  452. // Array of byte counts of data placed in m_astrValues
  453. //
  454. SQLLEN * m_acbValue;
  455. //
  456. // Lookaside
  457. //
  458. static ALLOC_CACHE_HANDLER * sm_pachOdbcStatements;
  459. }; // ODBC_STATEMENT()
  460. typedef ODBC_STATEMENT * PODBC_STATEMENT;
  461. /*++
  462. class ODBC_CONNECTION:
  463. This class specifies a logical class to contain the ODBC nuances
  464. and encapsulates relevant data for using ODBC to talk to a
  465. database system.
  466. Data encapsulated includes:
  467. m_henv Environment handle for ODBC connection.
  468. m_hdbc Database connection handle.
  469. m_rc Return code for last ODBC call.
  470. --*/
  471. class ODBC_CONNECTION {
  472. public:
  473. ODBC_CONNECTION()
  474. : m_henv ( SQL_NULL_HENV ),
  475. m_hdbc ( SQL_NULL_HDBC ),
  476. m_fValid ( FALSE ),
  477. m_rc ( SQL_SUCCESS )
  478. {}
  479. ~ODBC_CONNECTION();
  480. //
  481. // Returns the text for the last error that occurred
  482. //
  483. BOOL
  484. GetLastErrorText(
  485. OUT STRA * pstrError,
  486. IN HSTMT hstmt = SQL_NULL_HSTMT,
  487. IN RETCODE rc = SQL_SUCCESS
  488. ) const;
  489. BOOL
  490. GetLastErrorTextAsHtml(
  491. OUT STRA * pstrError,
  492. IN HSTMT hstmt = SQL_NULL_HSTMT,
  493. IN RETCODE rc = SQL_SUCCESS
  494. ) const;
  495. static
  496. BOOL
  497. Success(
  498. IN RETCODE rc
  499. )
  500. {
  501. return ( ( rc == SQL_SUCCESS ) ||
  502. ( rc == SQL_SUCCESS_WITH_INFO ) );
  503. }
  504. BOOL
  505. IsValid(
  506. VOID
  507. ) const
  508. {
  509. return ( m_fValid );
  510. }
  511. RETCODE
  512. QueryErrorCode(
  513. VOID
  514. ) const
  515. {
  516. return ( m_rc );
  517. }
  518. HRESULT
  519. Open(
  520. IN LPCSTR pszDataSource,
  521. IN LPCSTR pszUserName,
  522. IN LPCSTR pszPassword
  523. );
  524. HRESULT
  525. Open(
  526. IN LPCWSTR pwszDataSource,
  527. IN LPCWSTR pwszUserName,
  528. IN LPCWSTR pwszPassword
  529. );
  530. HRESULT
  531. Close(
  532. VOID
  533. );
  534. PODBC_STATEMENT
  535. AllocStatement(
  536. VOID
  537. );
  538. HRESULT
  539. SetConnectOption(
  540. IN UWORD Option,
  541. IN SQLULEN Param
  542. );
  543. BOOL
  544. GetInfo(
  545. IN DWORD fInfoType,
  546. IN PVOID rgbInfoValue,
  547. IN DWORD cbInfoValueMax,
  548. IN OUT DWORD * pcbInfoValue
  549. );
  550. static
  551. DWORD
  552. DisplaySize(
  553. SWORD ColType,
  554. DWORD cchColLength
  555. );
  556. #if DBG
  557. VOID
  558. Print(
  559. VOID
  560. ) const;
  561. #endif // DBG
  562. private:
  563. //
  564. // ODBC specific data members.
  565. //
  566. HENV m_henv;
  567. HDBC m_hdbc;
  568. RETCODE m_rc;
  569. BOOL m_fValid;
  570. }; // ODBC_CONNECTION
  571. typedef ODBC_CONNECTION * PODBC_CONNECTION;
  572. /************************************************************
  573. * Inline Functions
  574. ************************************************************/
  575. inline
  576. BOOL
  577. ODBC_STATEMENT::GetLastErrorText(
  578. OUT STRA * pstrError
  579. )
  580. {
  581. return ( m_pOdbcConnection->GetLastErrorText( pstrError,
  582. m_hStmt,
  583. m_rc ) );
  584. } // ODBC_STATEMENT::GetLastErrorText()
  585. inline
  586. BOOL
  587. ODBC_STATEMENT::GetLastErrorTextAsHtml(
  588. OUT STRA * pstrError
  589. )
  590. {
  591. return ( m_pOdbcConnection->GetLastErrorTextAsHtml( pstrError,
  592. m_hStmt,
  593. m_rc ) );
  594. } // ODBC_STATEMENT::GetLastErrorText()
  595. # endif // _ODBCCONN_HXX_