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.

2555 lines
76 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: crowset.cxx
  7. //
  8. // Contents: IRowset implementation for ADSI OLEDB provider
  9. //
  10. //--------------------------------------------------------------------------
  11. #include "oleds.hxx"
  12. //-----------------------------------------------------------------------------
  13. // Note on nomenclature
  14. //
  15. // Index - index within the rowset cache.
  16. // 0 <= (Index <= m_cNumRowsCached - 1)
  17. //
  18. // Row - index within the entire rowset (not just the cache).
  19. // 0 <= Row <= #rows in rowset - 1
  20. //
  21. // Bookmark - a unique value associated with each row. Currently, this is the
  22. // same as the row value.
  23. //
  24. // Row handle - handle to a row in the rowset cache that is returned to the
  25. // user. This value has to be unique across all rows currently in the cache
  26. // (rows that the user has a reference on). Although we could use the index
  27. // as this unique value, we will use the row (+1, since 0 == DB_NULL_HROW and
  28. // hence row 0 cannot have a handle of 0). Using the row has the advantage
  29. // that we will be able to detect stale row handles passed in by the user in
  30. // most cases. This would be more difficult with the index as the index can
  31. // be reused.
  32. //
  33. // The rowset cache contains a fixed number of rows, specified by the
  34. // DBPROP_MAXOPENROWS property (if this property is set to 0, the rowset may
  35. // contain as many rows as possble). A row remains in the cache as long as the
  36. // consumer has a reference to the row. A row is removed from the cache when
  37. // its reference count goes to 0, as a result of a call to ReleaseRows. If the
  38. // cache becomes full, subsequent calls to fetch more rows in to the cache will
  39. // return DB_S_ROWLIMITEXCEEDED. The rows in the cache need not be contiguous.
  40. // For example, the consumer might fetch rows 1,2,3 and then call ReleaseRows
  41. // only on 2. In this case, 1 and 3 would still be in the cache. Each row in
  42. // the cache contains its "row" value. Currently, this value is also used as
  43. // the bookmark to the row.
  44. //-----------------------------------------------------------------------------
  45. //-----------------------------------------------------------------------------
  46. // CreateRowset
  47. //
  48. // Creates and initializes a rowset object. Called by a command or session
  49. // object. The rows are brought in only on demand.
  50. //
  51. //-----------------------------------------------------------------------------
  52. HRESULT
  53. CRowset::CreateRowset(
  54. CRowProvider *pIRowProvider, // The Row provider
  55. IUnknown *pParentObj, // parent object, a command or a session
  56. CSessionObject *pCSession, // Session Object
  57. CCommandObject *pCCommand, // Command Object
  58. ULONG cPropertySets, // # property sets in rowset property group
  59. DBPROPSET rgPropertySets[],// properties in rowset property group
  60. ULONG cAccessors, // accessor count
  61. HACCESSOR rgAccessors[], // accessors on command object
  62. BOOL fadsPathPresent, // Is ADsPath present in query
  63. BOOL fAllAttrs, // Return all attrs from row object?
  64. REFIID riid, // Interface desired
  65. IUnknown **ppIRowset // pointer to desired interface
  66. )
  67. {
  68. HRESULT hr;
  69. CRowset *pCRowset = NULL;
  70. BOOL fGotWarning = FALSE, fRowObjRequested;
  71. DBCOUNTITEM cRowsObtained;
  72. HROW *phRow = NULL;
  73. if( ppIRowset != NULL)
  74. *ppIRowset = NULL;
  75. pCRowset = new CRowset();
  76. if( NULL == pCRowset )
  77. BAIL_ON_FAILURE( hr = E_OUTOFMEMORY );
  78. hr = pCRowset->FInit(
  79. pIRowProvider,
  80. pParentObj,
  81. pCSession,
  82. pCCommand,
  83. cPropertySets,
  84. rgPropertySets,
  85. cAccessors,
  86. rgAccessors,
  87. fadsPathPresent,
  88. fAllAttrs
  89. );
  90. BAIL_ON_FAILURE(hr);
  91. if( hr != S_OK )
  92. fGotWarning = TRUE;
  93. if( ppIRowset != NULL )
  94. {
  95. #if (!defined(BUILD_FOR_NT40))
  96. // If the client requested IRow, create a rowset and get a row object
  97. // off the rowset. Once we make the provider read/write, we should add
  98. // IRowChange and IRowSchmaChange here in addition to IRow. If the
  99. // client set the DBPROP_IRow property, then we always create a row
  100. // object by default.
  101. hr = pCRowset->IsRowObjRequested(&fRowObjRequested);
  102. BAIL_ON_FAILURE( hr );
  103. if( IsEqualIID(riid, IID_IRow) || (fRowObjRequested) )
  104. {
  105. hr = pCRowset->GetNextRows(NULL, 0, 1, &cRowsObtained, &phRow);
  106. BAIL_ON_FAILURE(hr);
  107. if( DB_S_ENDOFROWSET == hr )
  108. BAIL_ON_FAILURE( hr = DB_E_NOTFOUND );
  109. if( hr != S_OK )
  110. BAIL_ON_FAILURE( E_FAIL );
  111. ADsAssert( (1 == cRowsObtained ) && (phRow != NULL) &&
  112. (*phRow != DB_NULL_HROW) );
  113. // Bump up reference count of rowset. This is to avoid the
  114. // destruction of the rowset if the call below fails. Artificially
  115. // incrementing the reference count by one will ensure that any
  116. // call to Release() on the rowset (say, from the destructor of
  117. // the row object) will not end up in the rowset object being
  118. // deleted.
  119. pCRowset->m_cRef++;
  120. hr = pCRowset->m_pCRowsetInfo->GetRowFromHROW(
  121. NULL,
  122. *phRow,
  123. riid,
  124. ppIRowset,
  125. FALSE, // this is not a tear-off
  126. pCRowset->m_fAllAttrs
  127. );
  128. // Restore reference count
  129. pCRowset->m_cRef--;
  130. BAIL_ON_FAILURE(hr);
  131. // Release the row handle since the row object now has a reference
  132. // to it. The reference count of the rowset is at 1 now since the
  133. // row object stored off a pointer to the rowset. The rowset will
  134. // be freed when the row is released.
  135. hr = pCRowset->ReleaseRows(1, phRow, NULL, NULL, NULL);
  136. BAIL_ON_FAILURE(hr);
  137. // we won't bother returning DB_S_NOTSINGLETON if there are more
  138. // rows in the rowset since the spec doesn't require it.
  139. }
  140. else
  141. #endif
  142. {
  143. hr = pCRowset->QueryInterface(riid, (void **)ppIRowset);
  144. BAIL_ON_FAILURE(hr);
  145. }
  146. }
  147. else // OpenRowset may pass in NULL as ppIRowset
  148. delete pCRowset;
  149. if( fGotWarning )
  150. RRETURN( DB_S_ERRORSOCCURRED );
  151. else
  152. RRETURN( S_OK );
  153. error:
  154. if( pCRowset )
  155. delete pCRowset;
  156. RRETURN(hr);
  157. }
  158. //-----------------------------------------------------------------------------
  159. // IsRowObjRequested
  160. //
  161. // From the properties specified by the client, check if DBPROP_IRow is set
  162. //
  163. //-----------------------------------------------------------------------------
  164. HRESULT
  165. CRowset::IsRowObjRequested(BOOL *pfRowObjRequested)
  166. {
  167. #if (!defined(BUILD_FOR_NT40))
  168. DBPROPIDSET rgPropertyIDSets;
  169. DBPROPID dbPropId;
  170. ULONG cPropSets;
  171. DBPROPSET *prgPropertySets;
  172. VARIANT *pVariant;
  173. HRESULT hr;
  174. int i, j;
  175. if( NULL == pfRowObjRequested )
  176. BAIL_ON_FAILURE( hr = E_INVALIDARG );
  177. dbPropId = DBPROP_IRow;
  178. cPropSets = 0;
  179. prgPropertySets = NULL;
  180. rgPropertyIDSets.cPropertyIDs = 1;
  181. rgPropertyIDSets.rgPropertyIDs = &dbPropId;
  182. rgPropertyIDSets.guidPropertySet = DBPROPSET_ROWSET;
  183. hr = m_pCUtilProp->GetProperties(1,
  184. &rgPropertyIDSets,
  185. &cPropSets,
  186. &prgPropertySets,
  187. PROPSET_COMMAND
  188. );
  189. BAIL_ON_FAILURE( hr );
  190. ADsAssert( (1 == cPropSets) && (prgPropertySets != NULL) );
  191. ADsAssert( (1 == prgPropertySets->cProperties) &&
  192. (prgPropertySets->rgProperties != NULL) );
  193. pVariant = &(prgPropertySets->rgProperties[0].vValue);
  194. *pfRowObjRequested = V_BOOL( pVariant );
  195. // Free memory allocated by GetProperties
  196. for (i = 0; i < cPropSets; i++)
  197. {
  198. for (j = 0; j < prgPropertySets[i].cProperties; j++)
  199. {
  200. DBPROP *pProp = &(prgPropertySets[i].rgProperties[j]);
  201. ADsAssert(pProp);
  202. // We should free the DBID in pProp, but we know that
  203. // GetProperties always returns DB_NULLID and FreeDBID doesn't
  204. // handle DB_NULLID. So, DBID is not freed here.
  205. VariantClear(&pProp->vValue);
  206. }
  207. CoTaskMemFree(prgPropertySets[i].rgProperties);
  208. }
  209. CoTaskMemFree(prgPropertySets);
  210. RRETURN( S_OK );
  211. error:
  212. RRETURN( hr );
  213. #else
  214. RRETURN(E_FAIL);
  215. #endif
  216. }
  217. //-----------------------------------------------------------------------------
  218. // CRowset
  219. //
  220. // Constructor initializes all fields to NULL
  221. //
  222. //-----------------------------------------------------------------------------
  223. CRowset::CRowset(void)
  224. {
  225. m_pCRowsetInfo = NULL;
  226. m_pCAccessor = NULL;
  227. m_pCRowProvider = NULL;
  228. m_pCUtilProp = NULL;
  229. m_pIDataConvert = NULL;
  230. m_pIColumnsInfo = NULL;
  231. m_pIRowProvider = NULL;
  232. m_pIMalloc = NULL;
  233. m_lLastFetchRow = RESET_ROW;
  234. m_cRowBytes = 0;
  235. m_cCol = 0;
  236. m_pColData = NULL;
  237. m_fCritSectionInitialized = FALSE;
  238. m_fEndOfRowset = FALSE;
  239. m_fadsPathPresent = TRUE;
  240. m_fAllAttrs = FALSE;
  241. m_cNumRowsCached = 0;
  242. m_LastFetchDir = FORWARD;
  243. m_pRowsPtr = NULL;
  244. m_dwRowCacheSize = 0;
  245. m_lCurrentRow = 0;
  246. m_cRef = 0;
  247. }
  248. //-----------------------------------------------------------------------------
  249. // ~Crowset
  250. //
  251. // Destructor releases all resources
  252. //
  253. //-----------------------------------------------------------------------------
  254. CRowset::~CRowset(void)
  255. {
  256. if( m_pRowsPtr )
  257. {
  258. int iRow;
  259. for(iRow = 0; iRow < m_cNumRowsCached; iRow++)
  260. {
  261. ADsAssert(m_pRowsPtr[iRow] != NULL);
  262. FreeRow(m_pRowsPtr[iRow]);
  263. }
  264. FreeADsMem(m_pRowsPtr);
  265. }
  266. if( m_pCRowsetInfo )
  267. delete m_pCRowsetInfo;
  268. if( m_pCAccessor )
  269. delete m_pCAccessor;
  270. if( m_pCUtilProp )
  271. delete m_pCUtilProp;
  272. // Shouldn't delete m_pCRowProvider since we didn't allocate it
  273. if( m_pIDataConvert )
  274. m_pIDataConvert->Release();
  275. if( m_pIColumnsInfo )
  276. m_pIColumnsInfo->Release();
  277. if( m_pIMalloc )
  278. m_pIMalloc->Release();
  279. // Shouldn't release m_pIRowProvider since we didn't AddRef it
  280. if( m_pColData )
  281. FreeADsMem(m_pColData);
  282. if( m_fCritSectionInitialized )
  283. DeleteCriticalSection(&m_RowsetCritSection);
  284. }
  285. //-----------------------------------------------------------------------------
  286. // FInit
  287. //
  288. // Initializes rowset object
  289. //
  290. //-----------------------------------------------------------------------------
  291. STDMETHODIMP
  292. CRowset::FInit(
  293. CRowProvider *pIRowProvider, // The Row provider
  294. IUnknown *pParentObj, // parent object, a command or a session
  295. CSessionObject *pCSession, // Session Object
  296. CCommandObject *pCCommand, // Command Object
  297. ULONG cPropertySets, // # property sets in rowset property group
  298. DBPROPSET rgPropertySets[],// properties in rowset property group
  299. ULONG cAccessors, // accessor count
  300. HACCESSOR rgAccessors[], // accessors on command object
  301. BOOL fadsPathPresent, // Is ADsPath present in query
  302. BOOL fAllAttrs // Return all attrs from row object?
  303. )
  304. {
  305. HRESULT hr;
  306. BOOL fGotWarning = FALSE;
  307. InitializeCriticalSection(&m_RowsetCritSection);
  308. m_fCritSectionInitialized = TRUE;
  309. m_pCRowsetInfo = new CRowsetInfo(NULL, pParentObj, pCSession, pCCommand,
  310. pIRowProvider);
  311. if( NULL == m_pCRowsetInfo )
  312. BAIL_ON_FAILURE( hr = E_OUTOFMEMORY );
  313. hr = m_pCRowsetInfo->FInit( (IUnknown *) ((IAccessor *)this) );
  314. BAIL_ON_FAILURE(hr);
  315. m_pCAccessor = new CImpIAccessor(this, NULL);
  316. if( NULL == m_pCAccessor )
  317. BAIL_ON_FAILURE( hr = E_OUTOFMEMORY );
  318. hr = m_pCAccessor->FInit();
  319. BAIL_ON_FAILURE(hr);
  320. m_pCRowProvider = pIRowProvider;
  321. m_pCUtilProp = new CUtilProp();
  322. if( NULL == m_pCUtilProp )
  323. BAIL_ON_FAILURE( hr = E_OUTOFMEMORY );
  324. hr = m_pCUtilProp->FInit(NULL);
  325. BAIL_ON_FAILURE(hr);
  326. // Set the properties. Use PROPSET_COMMAND as we only want to set
  327. // properties in the rowset property group. Any other properties
  328. // should return error.
  329. hr = m_pCUtilProp->SetProperties(cPropertySets,
  330. rgPropertySets,
  331. PROPSET_COMMAND
  332. );
  333. // On session object, we need to check DBPROPOPTIONS to really decide if
  334. // there was a serious error. See comment before SetSearchPrefs in
  335. // csession.cxx
  336. if( pCSession &&
  337. ((DB_E_ERRORSOCCURRED == hr) || (DB_S_ERRORSOCCURRED == hr)) )
  338. {
  339. ULONG i, j;
  340. for(i = 0; i < cPropertySets; i++)
  341. for(j = 0; j < rgPropertySets[i].cProperties; j++)
  342. if( rgPropertySets[i].rgProperties[j].dwStatus !=
  343. DBPROPSTATUS_OK )
  344. if( rgPropertySets[i].rgProperties[j].dwOptions !=
  345. DBPROPOPTIONS_OPTIONAL )
  346. {
  347. BAIL_ON_FAILURE( hr = DB_E_ERRORSOCCURRED );
  348. }
  349. else
  350. fGotWarning = TRUE;
  351. // if we get here, then there was all required properties were set
  352. // successfully. However, hr could still be DB_ERRORSOCCURRED if all
  353. // properties were optional and all of them could not be set. This
  354. // condition is not an error for OpenRowset as noted in the comment
  355. // in csession.cxx. Hence reset hr to S_OK.
  356. hr = S_OK;
  357. }
  358. BAIL_ON_FAILURE(hr);
  359. if( hr != S_OK ) // warning needs to be returned to user
  360. fGotWarning = TRUE;
  361. // Get the maximum number of rows the rowset cache should support
  362. hr = GetMaxCacheSize();
  363. BAIL_ON_FAILURE(hr);
  364. // Get IDataConvert interface for later use by IRowset->GetData
  365. hr = CoCreateInstance(
  366. CLSID_OLEDB_CONVERSIONLIBRARY,
  367. NULL,
  368. CLSCTX_INPROC_SERVER,
  369. IID_IDataConvert,
  370. (void **)&m_pIDataConvert
  371. );
  372. BAIL_ON_FAILURE(hr);
  373. // Get IColumnsInfo interface pointer from row provider
  374. hr = pIRowProvider->QueryInterface(
  375. IID_IColumnsInfo,
  376. (void **)&m_pIColumnsInfo
  377. );
  378. BAIL_ON_FAILURE(hr);
  379. // No need to AddRef below since row provider is contained in CRowsetInfo
  380. // and CRowsetInfo's lifetime is within CRowset.
  381. m_pIRowProvider = pIRowProvider;
  382. // Copy inherited accessors from the command object
  383. hr = CopyAccessors(cAccessors, pCCommand, rgAccessors);
  384. BAIL_ON_FAILURE(hr);
  385. hr = CoGetMalloc(MEMCTX_TASK, &m_pIMalloc);
  386. BAIL_ON_FAILURE(hr);
  387. // get the column offsets from the row provider
  388. hr = GetColOffsets();
  389. BAIL_ON_FAILURE(hr);
  390. m_fadsPathPresent = fadsPathPresent;
  391. m_fAllAttrs = fAllAttrs;
  392. if( fGotWarning )
  393. RRETURN( DB_S_ERRORSOCCURRED );
  394. else
  395. RRETURN( S_OK );
  396. error:
  397. if( m_pCRowsetInfo != NULL )
  398. delete m_pCRowsetInfo; // Releases pParentObj, pCCommand, pCSession and
  399. // pIRowProvider
  400. if( m_pCAccessor != NULL )
  401. delete m_pCAccessor;
  402. if( m_pCUtilProp != NULL )
  403. delete m_pCUtilProp;
  404. if( m_pIDataConvert != NULL )
  405. m_pIDataConvert->Release();
  406. if( m_pIColumnsInfo != NULL )
  407. m_pIColumnsInfo->Release();
  408. if( m_pIMalloc )
  409. m_pIMalloc->Release();
  410. if( m_fCritSectionInitialized )
  411. DeleteCriticalSection(&m_RowsetCritSection);
  412. m_pCRowsetInfo = NULL;
  413. m_pCAccessor = NULL;
  414. m_pCRowProvider = NULL;
  415. m_pCUtilProp = NULL;
  416. m_pIDataConvert = NULL;
  417. m_pIColumnsInfo = NULL;
  418. m_pIRowProvider = NULL;
  419. m_pIMalloc = NULL;
  420. m_fCritSectionInitialized = FALSE;
  421. RRETURN( hr );
  422. }
  423. //-----------------------------------------------------------------------------
  424. // GetMaxCacheSize
  425. //
  426. // From the properties specified by the client, get the maximum number of rows
  427. // that the rowset cache should support.
  428. //
  429. //-----------------------------------------------------------------------------
  430. HRESULT
  431. CRowset::GetMaxCacheSize(void)
  432. {
  433. DBPROPIDSET rgPropertyIDSets;
  434. DBPROPID dbPropId;
  435. ULONG cPropSets;
  436. DBPROPSET *prgPropertySets;
  437. VARIANT *pVariant;
  438. HRESULT hr;
  439. ULONG i, j;
  440. dbPropId = DBPROP_MAXOPENROWS;
  441. cPropSets = 0;
  442. prgPropertySets = NULL;
  443. rgPropertyIDSets.cPropertyIDs = 1;
  444. rgPropertyIDSets.rgPropertyIDs = &dbPropId;
  445. rgPropertyIDSets.guidPropertySet = DBPROPSET_ROWSET;
  446. hr = m_pCUtilProp->GetProperties(1,
  447. &rgPropertyIDSets,
  448. &cPropSets,
  449. &prgPropertySets,
  450. PROPSET_COMMAND
  451. );
  452. BAIL_ON_FAILURE( hr );
  453. ADsAssert( (1 == cPropSets) && (prgPropertySets != NULL) );
  454. ADsAssert( (1 == prgPropertySets->cProperties) &&
  455. (prgPropertySets->rgProperties != NULL) );
  456. pVariant = &(prgPropertySets->rgProperties[0].vValue);
  457. m_dwMaxCacheSize = V_I4( pVariant );
  458. // Free memory allocated by GetProperties
  459. for (i = 0; i < cPropSets; i++)
  460. {
  461. for (j = 0; j < prgPropertySets[i].cProperties; j++)
  462. {
  463. DBPROP *pProp = &(prgPropertySets[i].rgProperties[j]);
  464. ADsAssert(pProp);
  465. // We should free the DBID in pProp, but we know that
  466. // GetProperties always returns DB_NULLID and FreeDBID doesn't
  467. // handle DB_NULLID. So, DBID is not freed here.
  468. VariantClear(&pProp->vValue);
  469. }
  470. CoTaskMemFree(prgPropertySets[i].rgProperties);
  471. }
  472. CoTaskMemFree(prgPropertySets);
  473. RRETURN( S_OK );
  474. error:
  475. RRETURN( hr );
  476. }
  477. //-----------------------------------------------------------------------------
  478. // CopyAccessors
  479. //
  480. // Copies inherited accessors from the command object to the rowset object. If
  481. // the rowset is being created by a session object, nothing has to be done.
  482. //
  483. //-----------------------------------------------------------------------------
  484. HRESULT
  485. CRowset::CopyAccessors(
  486. ULONG cAccessors, // accessor count
  487. CCommandObject *pCCommand, // Command Object
  488. HACCESSOR rgAccessors[] // accessors on command object
  489. )
  490. {
  491. HRESULT hr = S_OK;
  492. IAccessor *pIAccessorCommand = NULL; // Command's IAccessor
  493. IAccessor *pIAccessorRowset = NULL; // Rowset's IAccessor
  494. // Bump up reference count so that call to Release at the end of this
  495. // function doesn't delete rowset object.
  496. CAutoBlock cab(&m_RowsetCritSection);
  497. ++m_cRef;
  498. if( (cAccessors > 0) && (pCCommand != NULL) )
  499. {
  500. hr = pCCommand->QueryInterface(
  501. IID_IAccessor,
  502. (void **)&pIAccessorCommand
  503. );
  504. BAIL_ON_FAILURE(hr);
  505. hr = this->QueryInterface(
  506. IID_IAccessor,
  507. (void **)&pIAccessorRowset
  508. );
  509. BAIL_ON_FAILURE(hr);
  510. hr = CpAccessors2Rowset(
  511. pIAccessorCommand,
  512. pIAccessorRowset,
  513. cAccessors,
  514. rgAccessors,
  515. m_pCAccessor);
  516. BAIL_ON_FAILURE(hr);
  517. }
  518. error:
  519. if( pIAccessorCommand )
  520. pIAccessorCommand->Release();
  521. if( pIAccessorRowset )
  522. pIAccessorRowset->Release();
  523. --m_cRef;
  524. RRETURN( hr );
  525. }
  526. //-----------------------------------------------------------------------------
  527. // GetColOffsets
  528. //
  529. // Decides the offets of the columns in thr row buffer based on the column
  530. // info from the row provider
  531. //
  532. //-----------------------------------------------------------------------------
  533. HRESULT
  534. CRowset::GetColOffsets(void)
  535. {
  536. HRESULT hr;
  537. DBCOLUMNINFO *pColInfo = NULL;
  538. OLECHAR *pColNames = NULL;
  539. DWORD dwOffset, i;
  540. hr = m_pIColumnsInfo->GetColumnInfo(&m_cCol, &pColInfo, &pColNames);
  541. BAIL_ON_FAILURE( hr );
  542. // we don't need the column names
  543. m_pIMalloc->Free(pColNames);
  544. m_pColData = (COLDATA *) AllocADsMem(sizeof(COLDATA) * m_cCol);
  545. if( NULL == m_pColData )
  546. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  547. // Account for fields such as the row reference count that occur
  548. // before all columns in the row buffer
  549. dwOffset = sizeof(ROWBUFFER);
  550. for(i = 0; i < m_cCol; i++)
  551. {
  552. dwOffset = ColumnAlign(dwOffset);
  553. m_pColData[i].dwColOffset = dwOffset;
  554. // Account for the length and status fields of OLEDB data
  555. dwOffset += FIELD_OFFSET(OLEDBDATA, OledbValue);
  556. // Row provider returns all variable-length data types as BYREF
  557. if( pColInfo[i].wType & DBTYPE_BYREF )
  558. dwOffset += sizeof(char *);
  559. else
  560. dwOffset += (DWORD)pColInfo[i].ulColumnSize;
  561. m_pColData[i].wType = pColInfo[i].wType;
  562. }
  563. m_cRowBytes = dwOffset;
  564. m_pIMalloc->Free(pColInfo);
  565. RRETURN( S_OK );
  566. error:
  567. if( pColInfo != NULL )
  568. m_pIMalloc->Free(pColInfo);
  569. if( m_pColData != NULL )
  570. {
  571. FreeADsMem(m_pColData);
  572. m_pColData = NULL;
  573. }
  574. m_cCol = m_cRowBytes = 0;
  575. RRETURN( hr );
  576. }
  577. //-----------------------------------------------------------------------------
  578. // GetNextRows
  579. //
  580. // Gets rows using the row provider into the rowset's cache. Handles for these
  581. // rows are returned to the caller. The caller then retrieves the rows using
  582. // GetData.
  583. //
  584. //-----------------------------------------------------------------------------
  585. STDMETHODIMP
  586. CRowset::GetNextRows(
  587. HCHAPTER hChapter, // ignored since this is not a chaptered rowset
  588. DBROWOFFSET lRowsOffset,
  589. DBROWCOUNT cRows,
  590. DBCOUNTITEM *pcRowsObtained,
  591. HROW **prghRows
  592. )
  593. {
  594. HRESULT hr;
  595. RBOOKMARK StartRowBmk; // bookmark of first row to fetch
  596. ULONG cbBookmark = ADSI_BMK_SIZE; // size in bytes of the bookmark
  597. if( (NULL == pcRowsObtained) || (NULL == prghRows) )
  598. {
  599. if( pcRowsObtained != NULL )
  600. *pcRowsObtained = 0;
  601. RRETURN( E_INVALIDARG );
  602. }
  603. *pcRowsObtained = 0;
  604. if( 0 == cRows )
  605. RRETURN( S_OK );
  606. CAutoBlock cab(&m_RowsetCritSection);
  607. if( RESET_ROW == m_lLastFetchRow )
  608. // this is the first fetch OR RestartPosition was called prior to this
  609. {
  610. if( (lRowsOffset > 0) || ((0 == lRowsOffset) && (cRows > 0)) )
  611. {
  612. StartRowBmk = DBBMK_FIRST;
  613. cbBookmark = STD_BMK_SIZE;
  614. }
  615. else if( (lRowsOffset < 0) || ((0 == lRowsOffset) && (cRows < 0)) )
  616. {
  617. // can't set StartRowBmk to DBBMK_LAST as we want to set it one
  618. // beyond the end of the rowset
  619. // Seek to end of rowset
  620. hr = SeekToEnd();
  621. if( FAILED(hr) )
  622. RRETURN( hr );
  623. // m_lCurrentRow is now 2 off the last row of the rowset
  624. StartRowBmk = RowToBmk(m_lCurrentRow - 1);
  625. }
  626. if( cRows < 0 )
  627. // the first row we want to fetch is one before that specified by
  628. // the combination of StartRowBmk and lRowsOffset
  629. lRowsOffset--;
  630. }
  631. else // we have fetched rows before OR RestartPosition was not called
  632. {
  633. // except in the 3rd case below, it is possible that StartRowBmk
  634. // will end up pointing to a row that is outside the rowset
  635. if( (FORWARD == m_LastFetchDir) && (cRows > 0) )
  636. StartRowBmk = RowToBmk(m_lLastFetchRow + 1);
  637. else if( (BACKWARD == m_LastFetchDir) && (cRows < 0) )
  638. StartRowBmk = RowToBmk(m_lLastFetchRow - 1);
  639. else // first row returned will be same as last row returned previously
  640. StartRowBmk = RowToBmk(m_lLastFetchRow);
  641. }
  642. hr = GetRowsAt(NULL, hChapter, cbBookmark, (BYTE *) &StartRowBmk,
  643. lRowsOffset, cRows, pcRowsObtained, prghRows);
  644. if( SUCCEEDED(hr) )
  645. {
  646. // If we return DB_S_ENDOFROWSET because lRowsOffset indicated a
  647. // position outside the rowset, then we do not modify m_lLastFetchRow.
  648. // Only if we walked off either end of the rowset AND fetched at least
  649. // a row is m_lLastFetchRow updated. If no rows are returned due to
  650. // lack of space in the rowset cache, we do not modify m_lLastFetchRow.
  651. if( *pcRowsObtained )
  652. {
  653. m_LastFetchDir = (cRows > 0) ? FORWARD:BACKWARD;
  654. m_lLastFetchRow = HROWToRow( (*prghRows)[*pcRowsObtained - 1] );
  655. }
  656. }
  657. RRETURN( hr );
  658. }
  659. //-----------------------------------------------------------------------------
  660. // GetRowsAt
  661. //
  662. // Fetches rows starting from a given offset from a bookmark. Handles for
  663. // rows fetched are returned to the caller. The caller may send in an invalid
  664. // bookmark i.e, a bookmark that was not returned by a previous invocation of
  665. // GetData. Although, this is an error, the spec does not require the provider
  666. // to detect this condition. So, the user can send in a random bookmark and the
  667. // provider will return the correct row if there is a row that corresponds to
  668. // the bookmark and DB_S_ENDOFROWSET otherwise. We make use of this fact when
  669. // GetNextRows calls GetRowsAt. The bookmark passed in by GetNextRows may be
  670. // for a row that is not yet in the provider's cache.
  671. //
  672. //-----------------------------------------------------------------------------
  673. STDMETHODIMP
  674. CRowset::GetRowsAt(
  675. HWATCHREGION hReserved, // Reserved for future use. Ignored.
  676. HCHAPTER hChapter, // ignored as this is not a chaptered rowset
  677. DBBKMARK cbBookmark,
  678. const BYTE *pBookmark,
  679. DBROWOFFSET lRowsOffset,
  680. DBROWCOUNT cRows,
  681. DBCOUNTITEM *pcRowsObtained,
  682. HROW **prghRows
  683. )
  684. {
  685. LONG lStartRow, // first row returned
  686. lBmkRow, // row corresponding to bookmark
  687. lNextRow,
  688. lRowIndex;
  689. int iStep;
  690. HROW *phRow;
  691. ROWBUFFER *pRowBuffer;
  692. HRESULT hr;
  693. BOOL fMemAllocated = FALSE;
  694. DBCOUNTITEM iRow;
  695. ULONG_PTR ulRowsOffset;
  696. if( (0 == cbBookmark) || (NULL == pBookmark) ||
  697. (NULL == pcRowsObtained) || (NULL == prghRows) )
  698. {
  699. if( pcRowsObtained != NULL )
  700. *pcRowsObtained = 0;
  701. RRETURN( E_INVALIDARG );
  702. }
  703. *pcRowsObtained = 0;
  704. if( (cbBookmark != STD_BMK_SIZE) && (cbBookmark != ADSI_BMK_SIZE) )
  705. RRETURN( DB_E_BADBOOKMARK );
  706. if( 0 == cRows )
  707. RRETURN( S_OK ); // don't have to check for any errors
  708. CAutoBlock cab(&m_RowsetCritSection);
  709. // rowset can't have more than 2^32 rows. So, if offset points to beyond
  710. // that return eof.
  711. ulRowsOffset = (lRowsOffset > 0) ? lRowsOffset : -lRowsOffset;
  712. if( ulRowsOffset & 0xffffffff00000000 )
  713. RRETURN( DB_S_ENDOFROWSET );
  714. // Get first row to fetch
  715. hr = BmkToRow(cbBookmark, pBookmark, &lBmkRow);
  716. if( FAILED(hr) )
  717. RRETURN( hr );
  718. lStartRow = lBmkRow + lRowsOffset;
  719. if( lStartRow < FIRST_ROW )
  720. RRETURN( DB_S_ENDOFROWSET );
  721. lNextRow = lStartRow;
  722. if( NULL == *prghRows ) // provider has to allocate memory for handles
  723. {
  724. ULONG_PTR cNumHandles, cRowsAbs;
  725. // not sure if there is an abs64
  726. if(cRows < 0)
  727. cRowsAbs = -cRows;
  728. else
  729. cRowsAbs = cRows;
  730. // Guard against user requesting too many rows - maximum we can
  731. // return is the size of the rowset cache
  732. if( m_dwMaxCacheSize > 0 ) // user specified some max value
  733. cNumHandles = Min(cRowsAbs, (ULONG_PTR) m_dwMaxCacheSize);
  734. else
  735. cNumHandles = cRowsAbs;
  736. if( 0 == (cNumHandles * sizeof(HROW)) ) // numeric overflow
  737. *prghRows = NULL;
  738. else
  739. *prghRows = (HROW *) m_pIMalloc->Alloc(cNumHandles * sizeof(HROW));
  740. if( NULL == *prghRows )
  741. RRETURN( E_OUTOFMEMORY );
  742. fMemAllocated = TRUE;
  743. }
  744. iStep = (cRows > 0) ? 1 : -1;
  745. phRow = *prghRows;
  746. // fetch rows
  747. while( cRows )
  748. {
  749. hr = SeekToRow(lNextRow);
  750. BAIL_ON_FAILURE(hr);
  751. if( DB_S_ENDOFROWSET == hr )
  752. // we reached the end of the rowset OR we have reached the end of
  753. // whatever portion of the results set that ADSI has cached (depends
  754. // on the ADSIPROP_CACHE_RESULTS property)
  755. {
  756. if( (0 == *pcRowsObtained) && fMemAllocated )
  757. {
  758. m_pIMalloc->Free(*prghRows);
  759. *prghRows = NULL;
  760. }
  761. RRETURN( DB_S_ENDOFROWSET );
  762. }
  763. // Bring in the row that we just seeked to
  764. hr = BringInRow();
  765. BAIL_ON_FAILURE(hr);
  766. if( (DB_S_ROWLIMITEXCEEDED == hr) || (DB_S_ENDOFROWSET == hr) )
  767. // no more space in rowset cache OR end of rowset. We will hit end
  768. // of rowset only if m_lCurrentRow was one/two beyond the end of the
  769. // rowset when SeekToRow was called above AND lNextRow == m_lCurrentRow
  770. // (in which case, SeekToRow would just return without doing anything).
  771. {
  772. if( (0 == *pcRowsObtained) && fMemAllocated )
  773. {
  774. m_pIMalloc->Free(*prghRows);
  775. *prghRows = NULL;
  776. }
  777. RRETURN( hr );
  778. }
  779. // Get the index of the row within the rowset cache
  780. *phRow = RowToHROW(lNextRow);
  781. lRowIndex = HROWToIndex(*phRow);
  782. // Increment reference count of row
  783. pRowBuffer = m_pRowsPtr[lRowIndex];
  784. ADsAssert(pRowBuffer != NULL);
  785. IncrRefCount(pRowBuffer);
  786. phRow++;
  787. (*pcRowsObtained)++;
  788. lNextRow += iStep;
  789. cRows += (-iStep);
  790. } // while (cRows)
  791. RRETURN( S_OK );
  792. error:
  793. // release any rows that were brought into the cache
  794. phRow = *prghRows;
  795. for(iRow = 0; iRow < *pcRowsObtained; iRow++)
  796. {
  797. ULONG ulRefCount;
  798. ReleaseRows(1, phRow, NULL, &ulRefCount, NULL); //ignore ret value
  799. phRow++;
  800. }
  801. if( fMemAllocated )
  802. {
  803. m_pIMalloc->Free(*prghRows);
  804. *prghRows = NULL;
  805. }
  806. *pcRowsObtained = 0;
  807. RRETURN(hr);
  808. }
  809. //-----------------------------------------------------------------------------
  810. // BmkToRow
  811. //
  812. // Converts a bookmark to a row within the rowset.
  813. //
  814. //-----------------------------------------------------------------------------
  815. HRESULT
  816. CRowset::BmkToRow(
  817. ULONG cbBookmark, // number of bytes in the bookmark
  818. const BYTE *pBookmark, // pointer to bookmark
  819. LONG *plRow
  820. )
  821. {
  822. HRESULT hr;
  823. ADsAssert( plRow != NULL );
  824. if( STD_BMK_SIZE == cbBookmark )
  825. {
  826. // If pBookmark is a pointer passed in from GetNextRows, then it is
  827. // actually a LONG *. Accessing *pBookmark to get the LSByte of the
  828. // bookmark below assumes LITTLE ENDIAN format.
  829. if( DBBMK_FIRST == *pBookmark )
  830. *plRow = FIRST_ROW;
  831. else if( DBBMK_LAST == *pBookmark )
  832. {
  833. CAutoBlock cab(&m_RowsetCritSection); // protect access to m_ fields
  834. // Seek to end of rowset
  835. hr = SeekToEnd();
  836. if( FAILED(hr) )
  837. RRETURN( hr );
  838. // should have reached end of rowset
  839. ADsAssert( DB_S_ENDOFROWSET == hr );
  840. // Seeking to end of rowset sets current row to MAX + 2, if there
  841. // is at least one row in the result. If the result is empty, we
  842. // will return -1 in *plRow below, but that's OK as -1 implies
  843. // DB_S_ENDOFROWSET in GetRowsAt()
  844. *plRow = m_lCurrentRow - 2;
  845. }
  846. else
  847. RRETURN( DB_E_BADBOOKMARK );
  848. }
  849. else if( ADSI_BMK_SIZE == cbBookmark )
  850. *plRow = *((LONG *) pBookmark);
  851. else // should never get in here
  852. ADsAssert( FALSE );
  853. RRETURN( S_OK );
  854. }
  855. //-----------------------------------------------------------------------------
  856. // SeekToRow
  857. //
  858. // Positions the IDirectorySearch cursor such that the next call to GetNextRow
  859. // will fetch the row lTargetRow. If we hit the end of the rowset before
  860. // seeking to lTargetRow, returns DB_S_ENDOFROWSET.
  861. //
  862. //-----------------------------------------------------------------------------
  863. HRESULT
  864. CRowset::SeekToRow(LONG lTargetRow)
  865. {
  866. HRESULT hr;
  867. CAutoBlock cab(&m_RowsetCritSection);
  868. if( lTargetRow == m_lCurrentRow ) // already at the right row
  869. RRETURN( S_OK );
  870. if( lTargetRow < m_lCurrentRow )
  871. {
  872. while( lTargetRow != m_lCurrentRow )
  873. {
  874. hr = m_pCRowProvider->SeekToPreviousRow();
  875. BAIL_ON_FAILURE(hr);
  876. // m_fEndOfRowset is set to TRUE only if we hit the end of the
  877. // rowset while moving forward
  878. m_fEndOfRowset = FALSE;
  879. if( m_lCurrentRow > 0 )
  880. m_lCurrentRow--;
  881. if( S_ADS_NOMORE_ROWS == hr )
  882. if( m_lCurrentRow != lTargetRow )
  883. RRETURN( DB_S_ENDOFROWSET );
  884. }
  885. }
  886. else
  887. {
  888. while( lTargetRow != m_lCurrentRow )
  889. {
  890. hr = m_pCRowProvider->SeekToNextRow();
  891. BAIL_ON_FAILURE(hr);
  892. m_lCurrentRow++;
  893. if( S_ADS_NOMORE_ROWS == hr )
  894. {
  895. // if we were already at the end of the rowset, then reset
  896. // m_lCurrentRow to its original value
  897. if( m_fEndOfRowset)
  898. m_lCurrentRow--;
  899. else
  900. m_fEndOfRowset = TRUE;
  901. RRETURN( DB_S_ENDOFROWSET );
  902. }
  903. }
  904. m_fEndOfRowset = FALSE;
  905. }
  906. RRETURN( S_OK );
  907. error:
  908. RRETURN( hr );
  909. }
  910. //-----------------------------------------------------------------------------
  911. // SeekToEnd
  912. //
  913. // Moves the IDirectorySearch cursor forward till it hits the end of the
  914. // rowset.
  915. //
  916. //-----------------------------------------------------------------------------
  917. HRESULT
  918. CRowset::SeekToEnd(void)
  919. {
  920. HRESULT hr = S_OK;
  921. CAutoBlock cab(&m_RowsetCritSection);
  922. while(1) // till we reach end of rowset
  923. {
  924. hr = m_pCRowProvider->SeekToNextRow();
  925. BAIL_ON_FAILURE(hr);
  926. m_lCurrentRow++;
  927. if( S_ADS_NOMORE_ROWS == hr )
  928. {
  929. // if we were already at the end of the rowset, then reset
  930. // m_lCurrentRow to its original value
  931. if( m_fEndOfRowset)
  932. m_lCurrentRow--;
  933. else
  934. m_fEndOfRowset = TRUE;
  935. RRETURN( DB_S_ENDOFROWSET );
  936. }
  937. }
  938. error:
  939. RRETURN( hr );
  940. }
  941. //-----------------------------------------------------------------------------
  942. // BringInRow
  943. //
  944. // Brings in the row that we seeked to last. Only one row is brought in.
  945. //
  946. //-----------------------------------------------------------------------------
  947. HRESULT
  948. CRowset::BringInRow(void)
  949. {
  950. HRESULT hr;
  951. ROWBUFFER *pRowBuffer = NULL;
  952. DWORD cColErrors; // #columns in a row that had an error when retrieved
  953. int iCol, iRow;
  954. HROW hRow;
  955. LONG lIndex;
  956. DBSTATUS *pdbStatus;
  957. CAutoBlock cab(&m_RowsetCritSection);
  958. // check if the row is already in the cache
  959. hRow = RowToHROW(m_lCurrentRow);
  960. lIndex = HROWToIndex(hRow);
  961. if(lIndex != -1)
  962. // row is already in cache
  963. {
  964. pRowBuffer = m_pRowsPtr[lIndex];
  965. ADsAssert(pRowBuffer != NULL);
  966. if( pRowBuffer->cColErrors )
  967. RRETURN( DB_S_ERRORSOCCURRED );
  968. else
  969. RRETURN( S_OK );
  970. }
  971. // check if we have room in the cache
  972. if( (m_dwMaxCacheSize > 0) && ((DWORD)m_cNumRowsCached == m_dwMaxCacheSize) )
  973. RRETURN( DB_S_ROWLIMITEXCEEDED );
  974. // Allocate more space in the cache, if we have run out
  975. if( (DWORD)m_cNumRowsCached == m_dwRowCacheSize ) // no more space for rows
  976. {
  977. DWORD dwTmpSize;
  978. ROWBUFFER **pTmpRowsPtr;
  979. dwTmpSize = (0 == m_dwRowCacheSize) ? 1 : (m_dwRowCacheSize*2);
  980. // make sure we don't overflow the cache
  981. if( (m_dwMaxCacheSize > 0) && (dwTmpSize > m_dwMaxCacheSize) )
  982. dwTmpSize = m_dwMaxCacheSize;
  983. pTmpRowsPtr = (ROWBUFFER **) ReallocADsMem(m_pRowsPtr,
  984. m_dwRowCacheSize*sizeof(ROWBUFFER *),
  985. dwTmpSize*sizeof(ROWBUFFER *));
  986. if( NULL == pTmpRowsPtr )
  987. BAIL_ON_FAILURE( hr = E_OUTOFMEMORY );
  988. m_dwRowCacheSize = dwTmpSize;
  989. m_pRowsPtr = pTmpRowsPtr;
  990. }
  991. // Allocate memory for a new row
  992. pRowBuffer = (ROWBUFFER *) AllocADsMem(m_cRowBytes);
  993. if( NULL == pRowBuffer )
  994. BAIL_ON_FAILURE( hr = E_OUTOFMEMORY );
  995. // initialize row to 0 (reference count initialized to 0 here)
  996. memset((char *) pRowBuffer, 0, m_cRowBytes);
  997. pRowBuffer->lRow = m_lCurrentRow;
  998. // use row provider to get the next row
  999. hr = m_pIRowProvider->NextRow();
  1000. BAIL_ON_FAILURE(hr);
  1001. m_lCurrentRow++;
  1002. if( DB_S_ENDOFROWSET == hr )
  1003. {
  1004. if( m_fEndOfRowset )
  1005. m_lCurrentRow--;
  1006. else
  1007. m_fEndOfRowset = TRUE;
  1008. FreeADsMem(pRowBuffer);
  1009. RRETURN( DB_S_ENDOFROWSET );
  1010. }
  1011. else
  1012. m_fEndOfRowset = FALSE;
  1013. cColErrors = 0;
  1014. for(iCol = 1; (DBORDINAL)iCol < m_cCol; iCol++)
  1015. {
  1016. hr = m_pIRowProvider->GetColumn(
  1017. iCol,
  1018. (DBSTATUS *) ( m_pColData[iCol].dwColOffset +
  1019. FIELD_OFFSET(OLEDBDATA, dbStatus) + (char *)pRowBuffer ),
  1020. (ULONG *) ( m_pColData[iCol].dwColOffset +
  1021. FIELD_OFFSET(OLEDBDATA, dwLength) + (char *)pRowBuffer ),
  1022. (BYTE *) ( m_pColData[iCol].dwColOffset +
  1023. FIELD_OFFSET(OLEDBDATA, OledbValue) + (char *)pRowBuffer )
  1024. );
  1025. if( FAILED(hr) )
  1026. cColErrors++;
  1027. // store the ADS_SEARCH_COLUMN if the DS returned any data. Even if
  1028. // GetColumn fails, this has to be done to ensure that the
  1029. // ADS_SEARCH_COLUMN structure is freed later.
  1030. pdbStatus = (DBSTATUS *) ( m_pColData[iCol].dwColOffset +
  1031. FIELD_OFFSET(OLEDBDATA, dbStatus) + (char *)pRowBuffer );
  1032. if( *pdbStatus != DBSTATUS_S_ISNULL )
  1033. memcpy((void *) ((char *)pRowBuffer + m_pColData[iCol].dwColOffset
  1034. + FIELD_OFFSET(OLEDBDATA, adsSearchCol)),
  1035. (void *) (&(m_pCRowProvider->_pdbSearchCol[iCol].adsColumn)),
  1036. sizeof(ADS_SEARCH_COLUMN) );
  1037. }
  1038. // Copy over the ADsPath search column, if required
  1039. if( FALSE == m_fadsPathPresent )
  1040. memcpy( (void *) (&(pRowBuffer->adsSearchCol)),
  1041. (void *) (&(m_pCRowProvider->_pdbSearchCol[iCol].adsColumn)),
  1042. sizeof(ADS_SEARCH_COLUMN) );
  1043. if( cColErrors == (m_cCol - 1) ) // all columns were in error
  1044. {
  1045. // any failure after this point should do this
  1046. FreeRow(pRowBuffer);
  1047. pRowBuffer = NULL; // so that we don't try to free again later
  1048. BAIL_ON_FAILURE( hr = DB_E_ERRORSOCCURRED );
  1049. }
  1050. // fill in the bookmark column
  1051. *( (DBSTATUS *) (m_pColData[0].dwColOffset +
  1052. FIELD_OFFSET(OLEDBDATA, dbStatus)+(char *)pRowBuffer) ) = DBSTATUS_S_OK;
  1053. *( (ULONG *) (m_pColData[0].dwColOffset +
  1054. FIELD_OFFSET(OLEDBDATA, dwLength)+(char *)pRowBuffer) ) = ADSI_BMK_SIZE;
  1055. // bookmark value is same as row
  1056. *( (ULONG *) (m_pColData[0].dwColOffset +
  1057. FIELD_OFFSET(OLEDBDATA, OledbValue)+(char *)pRowBuffer) ) =
  1058. m_lCurrentRow - 1;
  1059. // Store pointer to new row
  1060. m_pRowsPtr[m_cNumRowsCached] = pRowBuffer;
  1061. m_cNumRowsCached++;
  1062. if( cColErrors )
  1063. {
  1064. pRowBuffer->cColErrors = cColErrors;
  1065. RRETURN( DB_S_ERRORSOCCURRED );
  1066. }
  1067. RRETURN( S_OK );
  1068. error:
  1069. if( pRowBuffer )
  1070. FreeADsMem(pRowBuffer);
  1071. RRETURN( hr );
  1072. }
  1073. //----------------------------------------------------------------------------- // GetData
  1074. //
  1075. // Returns the data from the rowset cache in the format requested by the
  1076. // client. This routine performs deferred accessor validation i.e, checks that
  1077. // could not be done when the accessor was created due to lack of info. about
  1078. // the rowset.
  1079. //
  1080. //-----------------------------------------------------------------------------
  1081. STDMETHODIMP
  1082. CRowset::GetData(
  1083. HROW hRow,
  1084. HACCESSOR hAccessor,
  1085. void *pData
  1086. )
  1087. {
  1088. HRESULT hr;
  1089. LONG lRowIndex;
  1090. DBACCESSORFLAGS dwAccessorFlags;
  1091. DBCOUNTITEM cBindings = 0, cErrors = 0;
  1092. DBBINDING *prgBindings = NULL;
  1093. ROWBUFFER *pRowBuffer;
  1094. int iBind;
  1095. ULONG ulCol;
  1096. ULONG *pulProvLength;
  1097. DBLENGTH *pulConsLength;
  1098. DBSTATUS *pdbConsStatus, *pdbProvStatus;
  1099. void *pConsValue, *pProvValue;
  1100. OLEDBDATA *pProvOledbData;
  1101. if( NULL == pData ) // we don't supprt NULL accessor. So, this is an error
  1102. RRETURN( E_INVALIDARG );
  1103. CAutoBlock cab(&m_RowsetCritSection);
  1104. lRowIndex = HROWToIndex(hRow);
  1105. if( (lRowIndex < FIRST_ROW) || (lRowIndex >= m_cNumRowsCached) )
  1106. RRETURN( DB_E_BADROWHANDLE );
  1107. // Get pointer to the specified row
  1108. pRowBuffer = m_pRowsPtr[lRowIndex];
  1109. ADsAssert( pRowBuffer != NULL );
  1110. if( RefCount(pRowBuffer) <= 0 )
  1111. RRETURN( DB_E_BADROWHANDLE );
  1112. ADsAssert( m_pCAccessor );
  1113. hr = m_pCAccessor->GetBindings( // this call validates hAccessor
  1114. hAccessor,
  1115. &dwAccessorFlags,
  1116. &cBindings,
  1117. &prgBindings
  1118. );
  1119. if( FAILED(hr) )
  1120. RRETURN( hr );
  1121. ADsAssert( cBindings ); // NULL accessor is disallowed
  1122. for(iBind = 0; (DBCOUNTITEM)iBind < cBindings; iBind++)
  1123. {
  1124. // Free pObject in the binding, if any
  1125. if( prgBindings[iBind].pObject )
  1126. m_pIMalloc->Free(prgBindings[iBind].pObject);
  1127. // these types are disallowed
  1128. ADsAssert( !((prgBindings[iBind].wType & DBTYPE_VECTOR) ||
  1129. (prgBindings[iBind].wType & DBTYPE_ARRAY)) );
  1130. // Get pointers to consumer's OLEDB data using the bindings
  1131. if( prgBindings[iBind].dwPart & DBPART_STATUS )
  1132. pdbConsStatus = (DBSTATUS *) ( (char *)pData +
  1133. prgBindings[iBind].obStatus );
  1134. else
  1135. pdbConsStatus = NULL;
  1136. if( prgBindings[iBind].dwPart & DBPART_LENGTH )
  1137. pulConsLength = (DBLENGTH *) ( (char *)pData +
  1138. prgBindings[iBind].obLength );
  1139. else
  1140. pulConsLength = NULL;
  1141. if( prgBindings[iBind].dwPart & DBPART_VALUE )
  1142. pConsValue = (void *) ( (char *)pData +
  1143. prgBindings[iBind].obValue );
  1144. else
  1145. pConsValue = NULL;
  1146. // Check if accessor points to a valid column
  1147. ulCol = prgBindings[iBind].iOrdinal;
  1148. if( ulCol >= m_cCol )
  1149. {
  1150. if( pdbConsStatus )
  1151. *pdbConsStatus = DBSTATUS_E_BADACCESSOR;
  1152. cErrors++;
  1153. continue;
  1154. }
  1155. // Get pointers to providers OLEDB data from the row buffer cache
  1156. pProvOledbData = (OLEDBDATA *) ((char *)pRowBuffer +
  1157. m_pColData[ulCol].dwColOffset);
  1158. pdbProvStatus = &(pProvOledbData->dbStatus);
  1159. pulProvLength = &(pProvOledbData->dwLength);
  1160. pProvValue = &(pProvOledbData->OledbValue);
  1161. if( DBMEMOWNER_PROVIDEROWNED == prgBindings[iBind].dwMemOwner )
  1162. {
  1163. if( pdbConsStatus )
  1164. *pdbConsStatus = *pdbProvStatus;
  1165. if( pulConsLength )
  1166. *pulConsLength = *pulProvLength;
  1167. if( prgBindings[iBind].wType & DBTYPE_BYREF )
  1168. {
  1169. // If a binding specifies provider owned memory, and specifies
  1170. // type X | BYREF, and the provider's copy is not X or
  1171. // X | BYREF, return error
  1172. if( (prgBindings[iBind].wType & (~DBTYPE_BYREF)) !=
  1173. (m_pColData[ulCol].wType & (~DBTYPE_BYREF)) )
  1174. {
  1175. if( pdbConsStatus )
  1176. *pdbConsStatus = DBSTATUS_E_BADACCESSOR;
  1177. cErrors++;
  1178. continue;
  1179. }
  1180. if( m_pColData[ulCol].wType & DBTYPE_BYREF )
  1181. // provider's type exactly matches consumer's type
  1182. {
  1183. if( pConsValue )
  1184. *(void **) pConsValue = *(void **) pProvValue;
  1185. }
  1186. else
  1187. // provider actually has the data, not a pointer to the data
  1188. {
  1189. if( pConsValue )
  1190. *(void **) pConsValue = pProvValue;
  1191. }
  1192. }
  1193. else if( DBTYPE_BSTR == prgBindings[iBind].wType )
  1194. {
  1195. if( DBTYPE_BSTR != m_pColData[ulCol].wType )
  1196. {
  1197. if( pdbConsStatus )
  1198. *pdbConsStatus = DBSTATUS_E_BADACCESSOR;
  1199. cErrors++;
  1200. continue;
  1201. }
  1202. if( pConsValue )
  1203. *(void **) pConsValue = *(void **) pProvValue;
  1204. }
  1205. else // we should never get here
  1206. ADsAssert( FALSE );
  1207. }
  1208. else // binding specified client-owned memory
  1209. {
  1210. // workaround for bug in IDataConvert. Variant to Variant
  1211. // conversions may not always work (depending on the type in the
  1212. // variant). So, handle that case separately.
  1213. DBTYPE dbSrcType, dbDstType;
  1214. dbSrcType = m_pColData[ulCol].wType & (~DBTYPE_BYREF);
  1215. dbDstType = prgBindings[iBind].wType & (~DBTYPE_BYREF);
  1216. if( (DBTYPE_VARIANT == dbSrcType) &&
  1217. (DBTYPE_VARIANT == dbDstType) )
  1218. {
  1219. PVARIANT pSrcVariant, pDstVariant = NULL;
  1220. if( (*pdbProvStatus != DBSTATUS_S_OK) &&
  1221. (*pdbProvStatus != DBSTATUS_S_ISNULL) )
  1222. // provider wasn't able to get value from DS. Return bad
  1223. // status to consumer
  1224. {
  1225. if( pdbConsStatus )
  1226. *pdbConsStatus = *pdbProvStatus;
  1227. if( pulConsLength )
  1228. *pulConsLength = 0;
  1229. // value will not be set
  1230. cErrors++;
  1231. continue; // to next binding
  1232. }
  1233. if( m_pColData[ulCol].wType & DBTYPE_BYREF )
  1234. pSrcVariant = *(PVARIANT *)pProvValue;
  1235. else
  1236. pSrcVariant = (PVARIANT)pProvValue;
  1237. if( (prgBindings[iBind].wType & DBTYPE_BYREF) && pConsValue )
  1238. {
  1239. pDstVariant = (PVARIANT)m_pIMalloc->Alloc(sizeof(VARIANT));
  1240. if( NULL == pDstVariant )
  1241. hr = E_OUTOFMEMORY;
  1242. }
  1243. else
  1244. pDstVariant = (PVARIANT)pConsValue;
  1245. if( pdbConsStatus )
  1246. *pdbConsStatus = *pdbProvStatus;
  1247. if( pulConsLength )
  1248. *pulConsLength = *pulProvLength;
  1249. if( pDstVariant )
  1250. {
  1251. VariantInit(pDstVariant);
  1252. if( DBSTATUS_S_ISNULL == *pdbProvStatus )
  1253. // provider couldn't get this column from DS (probably
  1254. // because there was no attribute with this name)
  1255. {
  1256. if( prgBindings[iBind].wType & DBTYPE_BYREF )
  1257. // don't allocate anything if returning NULL status
  1258. m_pIMalloc->Free(pDstVariant);
  1259. else
  1260. V_VT(pDstVariant) = VT_EMPTY;
  1261. hr = S_OK;
  1262. }
  1263. else
  1264. {
  1265. hr = VariantCopy(pDstVariant, pSrcVariant);
  1266. if( SUCCEEDED(hr) )
  1267. {
  1268. if( pConsValue &&
  1269. (prgBindings[iBind].wType & DBTYPE_BYREF) )
  1270. *(void **) pConsValue = pDstVariant;
  1271. }
  1272. else if( prgBindings[iBind].wType & DBTYPE_BYREF )
  1273. m_pIMalloc->Free(pDstVariant);
  1274. } // else
  1275. } // if( pDstvariant)
  1276. } // if( DBTYPE_VARIANT == ...)
  1277. else
  1278. {
  1279. DBLENGTH dbTmpLen = 0;
  1280. hr = m_pIDataConvert->DataConvert(
  1281. m_pColData[ulCol].wType,
  1282. prgBindings[iBind].wType,
  1283. *pulProvLength,
  1284. &dbTmpLen,
  1285. pProvValue,
  1286. pConsValue,
  1287. prgBindings[iBind].cbMaxLen,
  1288. *pdbProvStatus,
  1289. pdbConsStatus,
  1290. prgBindings[iBind].bPrecision,
  1291. prgBindings[iBind].bScale,
  1292. DBDATACONVERT_DEFAULT
  1293. );
  1294. if( pulConsLength )
  1295. *pulConsLength = dbTmpLen;
  1296. // if the binding specified DBTYPE_VARIANT | DBTYPE_BYREF, then
  1297. // IDataConvert does not allocate a VT_NULL variant. Instead it
  1298. // returns nothing in pConsValue. If it is not BYREF, then
  1299. // return VT_EMPTY instead of VT_NULL.
  1300. if( SUCCEEDED(hr) && (DBSTATUS_S_ISNULL == *pdbProvStatus) &&
  1301. (prgBindings[iBind].wType == DBTYPE_VARIANT) &&
  1302. pConsValue )
  1303. {
  1304. PVARIANT pVariant;
  1305. pVariant = (PVARIANT)pConsValue;
  1306. V_VT(pVariant) = VT_EMPTY;
  1307. }
  1308. }
  1309. if( FAILED(hr) )
  1310. {
  1311. if( pdbConsStatus )
  1312. *pdbConsStatus = DBSTATUS_E_CANTCONVERTVALUE;
  1313. cErrors++;
  1314. continue;
  1315. }
  1316. } // client-owned memory
  1317. }
  1318. m_pIMalloc->Free(prgBindings);
  1319. if( cErrors )
  1320. if( cErrors != cBindings ) // not all columns had error
  1321. RRETURN( DB_S_ERRORSOCCURRED );
  1322. else
  1323. RRETURN( DB_E_ERRORSOCCURRED );
  1324. else
  1325. RRETURN( S_OK );
  1326. }
  1327. //-----------------------------------------------------------------------------
  1328. // AddRefRows
  1329. //
  1330. // Increments reference count of specified rows
  1331. //
  1332. //-----------------------------------------------------------------------------
  1333. STDMETHODIMP
  1334. CRowset::AddRefRows(
  1335. DBCOUNTITEM cRows,
  1336. const HROW rghRows[],
  1337. ULONG rgRefCounts[],
  1338. DBROWSTATUS rgRowStatus[]
  1339. )
  1340. {
  1341. int iRow;
  1342. LONG lRowIndex;
  1343. ROWBUFFER *pRowBuffer;
  1344. DBCOUNTITEM cErrors = 0;
  1345. if( (NULL == rghRows) && (cRows != 0) )
  1346. RRETURN( E_INVALIDARG );
  1347. CAutoBlock cab(&m_RowsetCritSection);
  1348. for(iRow = 0; (DBCOUNTITEM)iRow < cRows; iRow++)
  1349. {
  1350. lRowIndex = HROWToIndex(rghRows[iRow]);
  1351. if( (lRowIndex < 0) || (lRowIndex >= m_cNumRowsCached) )
  1352. {
  1353. if( rgRowStatus )
  1354. rgRowStatus[iRow] = DBROWSTATUS_E_INVALID;
  1355. if( rgRefCounts )
  1356. rgRefCounts[iRow] = 0;
  1357. cErrors++;
  1358. continue;
  1359. }
  1360. // Get pointer to the specified row
  1361. pRowBuffer = m_pRowsPtr[lRowIndex];
  1362. ADsAssert( pRowBuffer != NULL );
  1363. if( RefCount(pRowBuffer) <= 0 )
  1364. {
  1365. if( rgRowStatus )
  1366. rgRowStatus[iRow] = DBROWSTATUS_E_INVALID;
  1367. if( rgRefCounts )
  1368. rgRefCounts[iRow] = 0;
  1369. cErrors++;
  1370. continue;
  1371. }
  1372. IncrRefCount(pRowBuffer);
  1373. if( rgRefCounts )
  1374. rgRefCounts[iRow] = RefCount(pRowBuffer);
  1375. if( rgRowStatus )
  1376. rgRowStatus[iRow] = DBROWSTATUS_S_OK;
  1377. }
  1378. if( cErrors )
  1379. if( cErrors == cRows )
  1380. RRETURN( DB_E_ERRORSOCCURRED );
  1381. else
  1382. RRETURN( DB_S_ERRORSOCCURRED );
  1383. RRETURN( S_OK );
  1384. }
  1385. //----------------------------------------------------------------------------
  1386. // ReleaseRows
  1387. //
  1388. // Decrements reference count of specified rows. The rows are not freed even
  1389. // if the reference count goes down to 0.
  1390. //
  1391. //----------------------------------------------------------------------------
  1392. STDMETHODIMP
  1393. CRowset::ReleaseRows(
  1394. DBCOUNTITEM cRows,
  1395. const HROW rghRows[],
  1396. DBROWOPTIONS rgRowOptions[], // ignored
  1397. ULONG rgRefCounts[],
  1398. DBROWSTATUS rgRowStatus[]
  1399. )
  1400. {
  1401. int iRow;
  1402. LONG lRowIndex;
  1403. ROWBUFFER *pRowBuffer;
  1404. DBCOUNTITEM cErrors = 0;
  1405. if( (NULL == rghRows) && (cRows != 0) )
  1406. RRETURN( E_INVALIDARG );
  1407. CAutoBlock cab(&m_RowsetCritSection);
  1408. for(iRow = 0; (DBCOUNTITEM)iRow < cRows; iRow++)
  1409. {
  1410. lRowIndex = HROWToIndex(rghRows[iRow]);
  1411. if( (lRowIndex < 0) || (lRowIndex >= m_cNumRowsCached) )
  1412. {
  1413. if( rgRowStatus )
  1414. rgRowStatus[iRow] = DBROWSTATUS_E_INVALID;
  1415. if( rgRefCounts )
  1416. rgRefCounts[iRow] = 0;
  1417. cErrors++;
  1418. continue;
  1419. }
  1420. // Get pointer to the specified row
  1421. pRowBuffer = m_pRowsPtr[lRowIndex];
  1422. ADsAssert( pRowBuffer != NULL );
  1423. if( RefCount(pRowBuffer) <= 0 )
  1424. {
  1425. if( rgRowStatus )
  1426. rgRowStatus[iRow] = DBROWSTATUS_E_INVALID;
  1427. if( rgRefCounts )
  1428. rgRefCounts[iRow] = 0;
  1429. cErrors++;
  1430. continue;
  1431. }
  1432. DecrRefCount(pRowBuffer);
  1433. if( rgRefCounts )
  1434. rgRefCounts[iRow] = RefCount(pRowBuffer);
  1435. if( rgRowStatus )
  1436. rgRowStatus[iRow] = DBROWSTATUS_S_OK;
  1437. // Free the row's memory
  1438. if( 0 == RefCount(pRowBuffer) )
  1439. {
  1440. int i;
  1441. FreeRow(pRowBuffer);
  1442. m_cNumRowsCached--;
  1443. // compact the cache
  1444. for(i = lRowIndex; i < m_cNumRowsCached; i++)
  1445. m_pRowsPtr[i] = m_pRowsPtr[i+1];
  1446. }
  1447. }
  1448. if( cErrors )
  1449. if( cErrors == cRows )
  1450. RRETURN( DB_E_ERRORSOCCURRED );
  1451. else
  1452. RRETURN( DB_S_ERRORSOCCURRED );
  1453. RRETURN( S_OK );
  1454. }
  1455. //----------------------------------------------------------------------------
  1456. // RestartPosition
  1457. //
  1458. // Repositions the next fetch poition to the initial position
  1459. //
  1460. //----------------------------------------------------------------------------
  1461. STDMETHODIMP
  1462. CRowset::RestartPosition(
  1463. HCHAPTER hChapter // ignored
  1464. )
  1465. {
  1466. CAutoBlock cab(&m_RowsetCritSection);
  1467. m_lLastFetchRow = RESET_ROW;
  1468. RRETURN( S_OK );
  1469. }
  1470. //----------------------------------------------------------------------------
  1471. // FreeRow
  1472. //
  1473. // Frees the memory used by a row in the rowset cache. Also frees the ADsColumn
  1474. // structures contained within the row.
  1475. //
  1476. //----------------------------------------------------------------------------
  1477. void
  1478. CRowset::FreeRow(
  1479. ROWBUFFER *pRowBuffer
  1480. )
  1481. {
  1482. int iCol;
  1483. ADS_SEARCH_COLUMN *pADsCol;
  1484. DBSTATUS *pdbStatus;
  1485. PVARIANT pVariant;
  1486. CAutoBlock cab(&m_RowsetCritSection);
  1487. // start with column 1. Bookmark column is ignored.
  1488. for(iCol = 1; (DBORDINAL)iCol < m_cCol; iCol++)
  1489. {
  1490. pADsCol = (ADS_SEARCH_COLUMN *) ((char *)pRowBuffer +
  1491. m_pColData[iCol].dwColOffset +
  1492. FIELD_OFFSET(OLEDBDATA, ,adsSearchCol));
  1493. pdbStatus = (DBSTATUS *) ( m_pColData[iCol].dwColOffset +
  1494. FIELD_OFFSET(OLEDBDATA, dbStatus) +
  1495. (char *)pRowBuffer );
  1496. if( *pdbStatus != DBSTATUS_S_ISNULL )
  1497. {
  1498. if( (DBSTATUS_S_OK == *pdbStatus) &&
  1499. ((m_pColData[iCol].wType & (~DBTYPE_BYREF)) == DBTYPE_VARIANT) )
  1500. // variant (or variant array) needs to be freed
  1501. {
  1502. pVariant = *(PVARIANT *) ( (m_pColData[iCol].dwColOffset +
  1503. FIELD_OFFSET(OLEDBDATA, OledbValue) +
  1504. (char *)pRowBuffer) );
  1505. if( V_VT(pVariant) & VT_ARRAY )
  1506. SafeArrayDestroy(V_ARRAY(pVariant));
  1507. else
  1508. VariantClear(pVariant);
  1509. FreeADsMem(pVariant);
  1510. }
  1511. // ignore error return
  1512. m_pCRowProvider->_pDSSearch->FreeColumn(pADsCol);
  1513. }
  1514. }
  1515. if( FALSE == m_fadsPathPresent )
  1516. // ignore error return
  1517. m_pCRowProvider->_pDSSearch->FreeColumn(&(pRowBuffer->adsSearchCol));
  1518. FreeADsMem(pRowBuffer);
  1519. return;
  1520. }
  1521. //-----------------------------------------------------------------------------
  1522. // GetADsPathFromHROW
  1523. //
  1524. // Gets the ADsPath corresponding to a HROW. This function is called by
  1525. // GetURLFromHROW, only if m_fadsPathPresent is FALSE.
  1526. //
  1527. //-----------------------------------------------------------------------------
  1528. HRESULT
  1529. CRowset::GetADsPathFromHROW(
  1530. HROW hRow,
  1531. ADS_CASE_IGNORE_STRING *padsPath
  1532. )
  1533. {
  1534. LONG lRowIndex;
  1535. ROWBUFFER *pRowBuffer;
  1536. ADsAssert(FALSE == m_fadsPathPresent);
  1537. CAutoBlock cab(&m_RowsetCritSection);
  1538. lRowIndex = HROWToIndex(hRow);
  1539. if( (lRowIndex < 0) || (lRowIndex >= m_cNumRowsCached) )
  1540. RRETURN(E_HANDLE);
  1541. // Get pointer to the specified row
  1542. pRowBuffer = m_pRowsPtr[lRowIndex];
  1543. ADsAssert( pRowBuffer != NULL );
  1544. if( RefCount(pRowBuffer) <= 0 )
  1545. RRETURN(E_HANDLE);
  1546. if( NULL == padsPath )
  1547. RRETURN(E_INVALIDARG);
  1548. *padsPath =
  1549. pRowBuffer->adsSearchCol.pADsValues[0].CaseIgnoreString;
  1550. RRETURN( S_OK );
  1551. }
  1552. //-----------------------------------------------------------------------------
  1553. // IUnknown methods
  1554. //-----------------------------------------------------------------------------
  1555. STDMETHODIMP
  1556. CRowset::QueryInterface(
  1557. THIS_ REFIID riid,
  1558. LPVOID FAR* ppvObj
  1559. )
  1560. {
  1561. if( NULL == ppvObj )
  1562. RRETURN( E_INVALIDARG );
  1563. *ppvObj = NULL;
  1564. if( IsEqualIID(riid, IID_IUnknown) )
  1565. *ppvObj = (IUnknown FAR *) ((IAccessor *) this);
  1566. else if( IsEqualIID(riid, IID_IAccessor) )
  1567. *ppvObj = (IAccessor FAR *) this;
  1568. else if( IsEqualIID(riid, IID_IColumnsInfo) )
  1569. *ppvObj = (IColumnsInfo FAR *) this;
  1570. else if( IsEqualIID(riid, IID_IConvertType) )
  1571. *ppvObj = (IConvertType FAR *) this;
  1572. #if (!defined(BUILD_FOR_NT40))
  1573. else if( IsEqualIID(riid, IID_IGetRow) )
  1574. *ppvObj = (IGetRow FAR *) this;
  1575. #endif
  1576. else if( IsEqualIID(riid, IID_IRowset) )
  1577. *ppvObj = (IRowset FAR *) this;
  1578. else if( IsEqualIID(riid, IID_IRowsetIdentity) )
  1579. *ppvObj = (IRowsetIdentity FAR *) this;
  1580. else if( IsEqualIID(riid, IID_IRowsetInfo) )
  1581. *ppvObj = (IRowsetInfo FAR *) this;
  1582. else if( IsEqualIID(riid, IID_IRowsetLocate) )
  1583. *ppvObj = (IRowsetLocate FAR *) this;
  1584. else if( IsEqualIID(riid, IID_IRowsetScroll) )
  1585. *ppvObj = (IRowsetScroll FAR *) this;
  1586. else
  1587. RRETURN( E_NOINTERFACE );
  1588. AddRef();
  1589. RRETURN( S_OK );
  1590. }
  1591. //-----------------------------------------------------------------------------
  1592. // AddRef
  1593. //
  1594. // Increments reference count of this object
  1595. //
  1596. //-----------------------------------------------------------------------------
  1597. STDMETHODIMP_(ULONG)
  1598. CRowset::AddRef(
  1599. void
  1600. )
  1601. {
  1602. CAutoBlock cab(&m_RowsetCritSection);
  1603. ADsAssert(((LONG)m_cRef) >= 0);
  1604. return ++m_cRef;
  1605. }
  1606. //-----------------------------------------------------------------------------
  1607. // Release
  1608. //
  1609. // Decrements reference count of this object
  1610. //
  1611. //-----------------------------------------------------------------------------
  1612. STDMETHODIMP_(ULONG)
  1613. CRowset::Release(
  1614. void
  1615. )
  1616. {
  1617. CAutoBlock cab(&m_RowsetCritSection);
  1618. ADsAssert(m_cRef > 0);
  1619. m_cRef--;
  1620. if( 0 == m_cRef )
  1621. {
  1622. cab.UnBlock();
  1623. delete this;
  1624. return 0;
  1625. }
  1626. return m_cRef;
  1627. }
  1628. //-----------------------------------------------------------------------------
  1629. // IAccessor methods
  1630. //-----------------------------------------------------------------------------
  1631. STDMETHODIMP
  1632. CRowset::AddRefAccessor(
  1633. HACCESSOR hAccessor,
  1634. DBREFCOUNT *pcRefCount
  1635. )
  1636. {
  1637. ADsAssert(m_pCAccessor);
  1638. RRETURN( m_pCAccessor->AddRefAccessor(hAccessor, pcRefCount) );
  1639. }
  1640. STDMETHODIMP
  1641. CRowset::CreateAccessor(
  1642. DBACCESSORFLAGS dwAccessorFlags,
  1643. DBCOUNTITEM cBindings,
  1644. const DBBINDING rgBindings[],
  1645. DBLENGTH cbRowSize,
  1646. HACCESSOR * phAccessor,
  1647. DBBINDSTATUS rgStatus[]
  1648. )
  1649. {
  1650. ADsAssert(m_pCAccessor);
  1651. RRETURN(m_pCAccessor->CreateAccessor(
  1652. dwAccessorFlags,
  1653. cBindings,
  1654. rgBindings,
  1655. cbRowSize,
  1656. phAccessor,
  1657. rgStatus)
  1658. );
  1659. }
  1660. STDMETHODIMP
  1661. CRowset::ReleaseAccessor(
  1662. HACCESSOR hAccessor,
  1663. DBREFCOUNT *pcRefCount
  1664. )
  1665. {
  1666. ADsAssert(m_pCAccessor);
  1667. RRETURN( m_pCAccessor->ReleaseAccessor(
  1668. hAccessor,
  1669. pcRefCount)
  1670. );
  1671. }
  1672. STDMETHODIMP
  1673. CRowset::GetBindings(
  1674. HACCESSOR hAccessor,
  1675. DBACCESSORFLAGS * pdwAccessorFlags,
  1676. DBCOUNTITEM * pcBindings,
  1677. DBBINDING ** prgBindings
  1678. )
  1679. {
  1680. ADsAssert(m_pCAccessor);
  1681. RRETURN( m_pCAccessor->GetBindings(
  1682. hAccessor,
  1683. pdwAccessorFlags,
  1684. pcBindings,
  1685. prgBindings)
  1686. );
  1687. }
  1688. //-----------------------------------------------------------------------------
  1689. // IColumnsInfo methods
  1690. //-----------------------------------------------------------------------------
  1691. STDMETHODIMP
  1692. CRowset::GetColumnInfo(
  1693. DBORDINAL *pcColumns,
  1694. DBCOLUMNINFO **pprgInfo,
  1695. WCHAR ** ppStringBuffer
  1696. )
  1697. {
  1698. ADsAssert(m_pIColumnsInfo);
  1699. RRETURN( m_pIColumnsInfo->GetColumnInfo(
  1700. pcColumns,
  1701. pprgInfo,
  1702. ppStringBuffer)
  1703. );
  1704. }
  1705. STDMETHODIMP
  1706. CRowset::MapColumnIDs(
  1707. DBORDINAL cColumnIDs,
  1708. const DBID rgColumnIDs[],
  1709. DBORDINAL rgColumns[]
  1710. )
  1711. {
  1712. ADsAssert(m_pIColumnsInfo);
  1713. RRETURN( m_pIColumnsInfo->MapColumnIDs(
  1714. cColumnIDs,
  1715. rgColumnIDs,
  1716. rgColumns)
  1717. );
  1718. }
  1719. //-----------------------------------------------------------------------------
  1720. // IConvertType methods (mandatory for IRowset)
  1721. //-----------------------------------------------------------------------------
  1722. STDMETHODIMP
  1723. CRowset::CanConvert(
  1724. DBTYPE wFromType,
  1725. DBTYPE wToType,
  1726. DBCONVERTFLAGS dwConvertFlags
  1727. )
  1728. {
  1729. HRESULT hr;
  1730. if( dwConvertFlags & DBCONVERTFLAGS_PARAMETER ) // not allowed on rowset
  1731. RRETURN( DB_E_BADCONVERTFLAG );
  1732. if( (dwConvertFlags & (~(DBCONVERTFLAGS_ISLONG |
  1733. DBCONVERTFLAGS_ISFIXEDLENGTH |
  1734. DBCONVERTFLAGS_FROMVARIANT))) !=
  1735. DBCONVERTFLAGS_COLUMN )
  1736. RRETURN( DB_E_BADCONVERTFLAG );
  1737. if( dwConvertFlags & DBCONVERTFLAGS_ISLONG )
  1738. {
  1739. DBTYPE wType;
  1740. wType = wFromType & (~(DBTYPE_BYREF | DBTYPE_ARRAY | DBTYPE_VECTOR));
  1741. // wType has to be variable-length DBTYPE
  1742. if( (wType != DBTYPE_STR) && (wType != DBTYPE_WSTR) &&
  1743. (wType != DBTYPE_BYTES) && (wType != DBTYPE_VARNUMERIC) )
  1744. RRETURN( DB_E_BADCONVERTFLAG );
  1745. }
  1746. // CreateAccessor returns error for ARRAY and VECTOR types. So we cannot
  1747. // convert to these types.
  1748. if( (wToType & DBTYPE_ARRAY) || (wToType & DBTYPE_VECTOR) )
  1749. RRETURN( S_FALSE );
  1750. if( dwConvertFlags & DBCONVERTFLAGS_FROMVARIANT )
  1751. {
  1752. DBTYPE dbTmpType, wVtType;
  1753. wVtType = wFromType & VT_TYPEMASK;
  1754. // Take out all of the Valid VT_TYPES (36 is VT_RECORD in VC 6)
  1755. if( (wVtType > VT_DECIMAL && wVtType < VT_I1) ||
  1756. ((wVtType > VT_LPWSTR && wVtType < VT_FILETIME) && wVtType !=36) ||
  1757. (wVtType > VT_CLSID) )
  1758. RRETURN( DB_E_BADTYPE );
  1759. dbTmpType = wToType & (~DBTYPE_BYREF);
  1760. if( DBTYPE_VARIANT == dbTmpType )
  1761. // GetData will do the right thing, so return TRUE.
  1762. RRETURN( S_OK );
  1763. }
  1764. // GetData handles VARIANT to VARIANT conversions, so special check here
  1765. if( (DBTYPE_VARIANT == (wFromType & (~DBTYPE_BYREF))) &&
  1766. (DBTYPE_VARIANT == (wToType & (~DBTYPE_BYREF))) )
  1767. RRETURN( S_OK );
  1768. ADsAssert( m_pIDataConvert != NULL );
  1769. hr = m_pIDataConvert->CanConvert(wFromType, wToType);
  1770. RRETURN( (E_INVALIDARG == hr)? S_FALSE : hr );
  1771. }
  1772. //-----------------------------------------------------------------------------
  1773. // IGetRow methods
  1774. //-----------------------------------------------------------------------------
  1775. STDMETHODIMP
  1776. CRowset::GetRowFromHROW(
  1777. IUnknown *pUnkOuter,
  1778. HROW hRow,
  1779. REFIID riid,
  1780. IUnknown **ppUnk
  1781. )
  1782. {
  1783. ADsAssert(m_pCRowsetInfo);
  1784. RRETURN( m_pCRowsetInfo->GetRowFromHROW(
  1785. pUnkOuter,
  1786. hRow,
  1787. riid,
  1788. ppUnk,
  1789. TRUE, // this ia tear-off row
  1790. m_fAllAttrs)
  1791. );
  1792. }
  1793. STDMETHODIMP
  1794. CRowset::GetURLFromHROW(
  1795. HROW hRow,
  1796. LPOLESTR *ppwszURL
  1797. )
  1798. {
  1799. ADsAssert(m_pCRowsetInfo);
  1800. RRETURN( m_pCRowsetInfo->GetURLFromHROW(hRow, ppwszURL) );
  1801. }
  1802. //-----------------------------------------------------------------------------
  1803. // IRowsetIdentity methods
  1804. //-----------------------------------------------------------------------------
  1805. STDMETHODIMP
  1806. CRowset::IsSameRow(
  1807. HROW hRow1,
  1808. HROW hRow2
  1809. )
  1810. {
  1811. LONG lIndex1, lIndex2;
  1812. ROWBUFFER *pRowBuffer1, *pRowBuffer2;
  1813. lIndex1 = HROWToIndex(hRow1);
  1814. lIndex2 = HROWToIndex(hRow2);
  1815. if( (lIndex1 < 0) || (lIndex1 >= m_cNumRowsCached) ||
  1816. (lIndex2 < 0) || (lIndex2 >= m_cNumRowsCached) )
  1817. RRETURN( DB_E_BADROWHANDLE );
  1818. // Get pointer to the specified row
  1819. pRowBuffer1 = m_pRowsPtr[lIndex1];
  1820. pRowBuffer2 = m_pRowsPtr[lIndex2];
  1821. ADsAssert( (pRowBuffer1 != NULL) && (pRowBuffer2 != NULL) );
  1822. if( (RefCount(pRowBuffer1) <= 0) || (RefCount(pRowBuffer2) <= 0) )
  1823. RRETURN( DB_E_BADROWHANDLE );
  1824. if( lIndex1 == lIndex2 )
  1825. RRETURN( S_OK );
  1826. else
  1827. RRETURN( S_FALSE );
  1828. }
  1829. //-----------------------------------------------------------------------------
  1830. // IRowsetInfo methods
  1831. //-----------------------------------------------------------------------------
  1832. STDMETHODIMP
  1833. CRowset::GetProperties(
  1834. const ULONG cPropertyIDSets,
  1835. const DBPROPIDSET rgPropertyIDSets[],
  1836. ULONG *pcPropertySets,
  1837. DBPROPSET **pprgPropertySets
  1838. )
  1839. {
  1840. ADsAssert( m_pCUtilProp );
  1841. // Check arguments for error
  1842. HRESULT hr = m_pCUtilProp->GetPropertiesArgChk(
  1843. cPropertyIDSets,
  1844. rgPropertyIDSets,
  1845. pcPropertySets,
  1846. pprgPropertySets,
  1847. PROPSET_COMMAND);
  1848. if( FAILED(hr) )
  1849. RRETURN( hr );
  1850. RRETURN( m_pCUtilProp->GetProperties(
  1851. cPropertyIDSets,
  1852. rgPropertyIDSets,
  1853. pcPropertySets,
  1854. pprgPropertySets,
  1855. PROPSET_COMMAND) );
  1856. }
  1857. STDMETHODIMP
  1858. CRowset::GetReferencedRowset(
  1859. DBORDINAL iOrdinal,
  1860. REFIID riid,
  1861. IUnknown **ppReferencedRowset
  1862. )
  1863. {
  1864. ADsAssert(m_pCRowsetInfo);
  1865. RRETURN( m_pCRowsetInfo->GetReferencedRowset(
  1866. iOrdinal,
  1867. riid,
  1868. ppReferencedRowset)
  1869. );
  1870. }
  1871. STDMETHODIMP
  1872. CRowset::GetSpecification(
  1873. REFIID riid,
  1874. IUnknown **ppSpecification
  1875. )
  1876. {
  1877. ADsAssert(m_pCRowsetInfo);
  1878. RRETURN( m_pCRowsetInfo->GetSpecification(
  1879. riid,
  1880. ppSpecification)
  1881. );
  1882. }
  1883. //----------------------------------------------------------------------------- // IRowsetLocate methods
  1884. //-----------------------------------------------------------------------------
  1885. STDMETHODIMP
  1886. CRowset::Compare(
  1887. HCHAPTER hChapter,
  1888. DBBKMARK cbBookmark1,
  1889. const BYTE *pBookmark1,
  1890. DBBKMARK cbBookmark2,
  1891. const BYTE *pBookmark2,
  1892. DBCOMPARE *pComparison
  1893. )
  1894. {
  1895. if( (0 == cbBookmark1) || (0 == cbBookmark2) || (NULL == pComparison) ||
  1896. (NULL == pBookmark1) || (NULL == pBookmark2) )
  1897. RRETURN( E_INVALIDARG );
  1898. if( STD_BMK_SIZE == cbBookmark1 )
  1899. {
  1900. if( (*pBookmark1 != DBBMK_FIRST) && (*pBookmark1 != DBBMK_LAST) )
  1901. RRETURN( DB_E_BADBOOKMARK );
  1902. }
  1903. else
  1904. if( ADSI_BMK_SIZE != cbBookmark1 )
  1905. RRETURN( DB_E_BADBOOKMARK );
  1906. if( STD_BMK_SIZE == cbBookmark2 )
  1907. {
  1908. if( (*pBookmark2 != DBBMK_FIRST) && (*pBookmark2 != DBBMK_LAST) )
  1909. RRETURN( DB_E_BADBOOKMARK );
  1910. }
  1911. else
  1912. if( ADSI_BMK_SIZE != cbBookmark2 )
  1913. RRETURN( DB_E_BADBOOKMARK );
  1914. if( (STD_BMK_SIZE == cbBookmark1) || (STD_BMK_SIZE == cbBookmark2) )
  1915. // standard bookmarks can only be compared for equality (not for <, >)
  1916. {
  1917. if( cbBookmark1 != cbBookmark2 )
  1918. *pComparison = DBCOMPARE_NE;
  1919. else if( *pBookmark1 == *pBookmark2 )
  1920. *pComparison = DBCOMPARE_EQ;
  1921. else
  1922. *pComparison = DBCOMPARE_NE;
  1923. }
  1924. else
  1925. {
  1926. if( (*((RBOOKMARK *)pBookmark1)) < (*((RBOOKMARK *)pBookmark2)) )
  1927. *pComparison = DBCOMPARE_LT;
  1928. else if( (*((RBOOKMARK *)pBookmark1)) > (*((RBOOKMARK *)pBookmark2)) )
  1929. *pComparison = DBCOMPARE_GT;
  1930. else
  1931. *pComparison = DBCOMPARE_EQ;
  1932. }
  1933. RRETURN( S_OK );
  1934. }
  1935. STDMETHODIMP
  1936. CRowset::Hash(
  1937. HCHAPTER hChapter,
  1938. DBBKMARK cBookmarks,
  1939. const DBBKMARK rgcbBookmarks[],
  1940. const BYTE *rgpBookmarks[],
  1941. DBHASHVALUE rgHashedValues[],
  1942. DBROWSTATUS rgBookmarkStatus[]
  1943. )
  1944. {
  1945. int i;
  1946. HRESULT hr;
  1947. DBBKMARK cErrors = 0;
  1948. if( (NULL == rgHashedValues) || ((cBookmarks != 0) &&
  1949. ((NULL == rgpBookmarks) || (NULL == rgcbBookmarks))) )
  1950. RRETURN( E_INVALIDARG );
  1951. if( 0 == cBookmarks )
  1952. RRETURN( S_OK );
  1953. for(i = 0; (DBBKMARK)i < cBookmarks; i++)
  1954. {
  1955. if( (rgcbBookmarks[i] != ADSI_BMK_SIZE) || (NULL == rgpBookmarks[i]) )
  1956. {
  1957. cErrors++;
  1958. if( rgBookmarkStatus )
  1959. rgBookmarkStatus[i] = DBROWSTATUS_E_INVALID;
  1960. }
  1961. else
  1962. {
  1963. LONG lRow;
  1964. hr = BmkToRow(rgcbBookmarks[i], rgpBookmarks[i], &lRow);
  1965. if( FAILED(hr) )
  1966. {
  1967. cErrors++;
  1968. if( rgBookmarkStatus )
  1969. rgBookmarkStatus[i] = DBROWSTATUS_E_INVALID;
  1970. continue;
  1971. }
  1972. rgHashedValues[i] = (DWORD) lRow;
  1973. if( rgBookmarkStatus )
  1974. rgBookmarkStatus[i] = DBROWSTATUS_S_OK;
  1975. }
  1976. }
  1977. if( cErrors )
  1978. {
  1979. if( cErrors == cBookmarks )
  1980. RRETURN( DB_E_ERRORSOCCURRED );
  1981. else
  1982. RRETURN( DB_S_ERRORSOCCURRED );
  1983. }
  1984. else
  1985. RRETURN( S_OK );
  1986. }
  1987. STDMETHODIMP
  1988. CRowset::GetRowsByBookmark(
  1989. HCHAPTER hChapter,
  1990. DBCOUNTITEM cRows,
  1991. const DBBKMARK rgcbBookmarks[],
  1992. const BYTE *rgpBookmarks[],
  1993. HROW rghRows[],
  1994. DBROWSTATUS rgRowStatus[]
  1995. )
  1996. {
  1997. int i;
  1998. HRESULT hr;
  1999. DBCOUNTITEM cRowsObtained = 0, cErrors = 0;
  2000. if( (NULL == rghRows) || (NULL == rgcbBookmarks) || (NULL== rgpBookmarks) )
  2001. RRETURN( E_INVALIDARG );
  2002. if( 0 == cRows )
  2003. RRETURN( S_OK );
  2004. for(i = 0; (DBCOUNTITEM)i < cRows; i++)
  2005. {
  2006. if( (rgcbBookmarks[i] != ADSI_BMK_SIZE) || (NULL == rgpBookmarks[i]) )
  2007. {
  2008. cErrors++;
  2009. if( rgRowStatus )
  2010. rgRowStatus[i] = DBROWSTATUS_E_INVALID;
  2011. rghRows[i] = DB_NULL_HROW;
  2012. }
  2013. else
  2014. {
  2015. HROW *phRow;
  2016. phRow = &rghRows[i];
  2017. hr = GetRowsAt(NULL, hChapter, rgcbBookmarks[i],
  2018. (BYTE *) rgpBookmarks[i], 0, 1, &cRowsObtained,
  2019. &phRow
  2020. );
  2021. if( 1 == cRowsObtained )
  2022. {
  2023. if( rgRowStatus )
  2024. rgRowStatus[i] = DBROWSTATUS_S_OK;
  2025. }
  2026. else
  2027. {
  2028. cErrors++;
  2029. rghRows[i] = DB_NULL_HROW;
  2030. if( rgRowStatus )
  2031. {
  2032. if( DB_S_ROWLIMITEXCEEDED == hr )
  2033. rgRowStatus[i] = DBROWSTATUS_E_LIMITREACHED;
  2034. else if ( E_OUTOFMEMORY == hr )
  2035. rgRowStatus[i] = DBROWSTATUS_E_OUTOFMEMORY;
  2036. else
  2037. rgRowStatus[i] = DBROWSTATUS_E_INVALID;
  2038. }
  2039. } // else
  2040. } // else
  2041. } // for
  2042. if( cErrors )
  2043. {
  2044. if( cErrors == cRows )
  2045. RRETURN( DB_E_ERRORSOCCURRED );
  2046. else
  2047. RRETURN( DB_S_ERRORSOCCURRED );
  2048. }
  2049. else
  2050. RRETURN( S_OK );
  2051. }
  2052. //----------------------------------------------------------------------------
  2053. // IRowsetScroll methods
  2054. //----------------------------------------------------------------------------
  2055. STDMETHODIMP
  2056. CRowset::GetApproximatePosition(
  2057. HCHAPTER hChapter,
  2058. DBBKMARK cbBookmark,
  2059. const BYTE *pBookmark,
  2060. DBCOUNTITEM *pulPosition,
  2061. DBCOUNTITEM *pcRows
  2062. )
  2063. {
  2064. LONG cRows = 0;
  2065. HRESULT hr;
  2066. if( (cbBookmark != 0) && ( NULL == pBookmark) )
  2067. RRETURN( E_INVALIDARG );
  2068. CAutoBlock cab(&m_RowsetCritSection);
  2069. if( pcRows )
  2070. // get the total number of rows in the rowset
  2071. {
  2072. hr = SeekToEnd();
  2073. if( FAILED(hr) )
  2074. RRETURN( E_FAIL );
  2075. cRows = m_lCurrentRow - 1;
  2076. }
  2077. if( 0 == cbBookmark )
  2078. {
  2079. if( pcRows )
  2080. *pcRows = cRows;
  2081. RRETURN( S_OK );
  2082. }
  2083. else if( (cbBookmark != ADSI_BMK_SIZE) && (cbBookmark != STD_BMK_SIZE) )
  2084. RRETURN( DB_E_BADBOOKMARK );
  2085. else
  2086. {
  2087. LONG lRow;
  2088. hr = BmkToRow(cbBookmark, pBookmark, &lRow);
  2089. if( FAILED(hr) )
  2090. {
  2091. if( DB_E_BADBOOKMARK == hr )
  2092. RRETURN( DB_E_BADBOOKMARK );
  2093. else
  2094. RRETURN( E_FAIL );
  2095. }
  2096. if( pulPosition )
  2097. {
  2098. *pulPosition = lRow + 1; // this number is 1-based
  2099. if( pcRows && (*pulPosition > (DBCOUNTITEM)cRows) ) // bookmark was bad
  2100. *pulPosition = cRows; // make sure *pulPosition <= *pcRows
  2101. }
  2102. if( pcRows )
  2103. *pcRows = cRows;
  2104. }
  2105. RRETURN( S_OK );
  2106. }
  2107. STDMETHODIMP
  2108. CRowset::GetRowsAtRatio(
  2109. HWATCHREGION hReserved1,
  2110. HCHAPTER hChapter,
  2111. DBCOUNTITEM ulNumerator,
  2112. DBCOUNTITEM ulDenominator,
  2113. DBROWCOUNT cRows,
  2114. DBCOUNTITEM *pcRowsObtained,
  2115. HROW **prghRows
  2116. )
  2117. {
  2118. HRESULT hr;
  2119. LONG lStartRow, cTotalRows;
  2120. RBOOKMARK StartRowBmk;
  2121. if( (NULL == pcRowsObtained) || (NULL == prghRows) )
  2122. {
  2123. if( pcRowsObtained != NULL )
  2124. *pcRowsObtained = 0;
  2125. RRETURN( E_INVALIDARG );
  2126. }
  2127. *pcRowsObtained = 0;
  2128. if( (ulNumerator > ulDenominator) || (0 == ulDenominator) )
  2129. RRETURN( DB_E_BADRATIO );
  2130. if( ((ulNumerator == ulDenominator) && (cRows > 0)) ||
  2131. ((0 == ulNumerator) && (cRows < 0)) )
  2132. RRETURN( DB_S_ENDOFROWSET );
  2133. if( 0 == cRows )
  2134. RRETURN( S_OK );
  2135. CAutoBlock cab(&m_RowsetCritSection);
  2136. // get total number of rows
  2137. hr = SeekToEnd();
  2138. if( FAILED(hr) )
  2139. RRETURN( E_FAIL );
  2140. cTotalRows = m_lCurrentRow - 1;
  2141. // Make sure ratio of 1 sets lStartRow to cTotalRows -1 (last row)
  2142. lStartRow = (long)((((double) ulNumerator)/ulDenominator) *
  2143. (cTotalRows - 1));
  2144. StartRowBmk = RowToBmk(lStartRow);
  2145. hr = GetRowsAt(NULL, hChapter, ADSI_BMK_SIZE, (BYTE *) &StartRowBmk,
  2146. 0, cRows, pcRowsObtained, prghRows);
  2147. RRETURN( hr );
  2148. }
  2149. //----------------------------------------------------------------------------