The correct way to check if a blob field has already been retrieved using poFetchBlobsOnDemand

I have a TClientDataSet with multiple records, and I want o to load all the records, but load the blob field on demand, one at a time.

I noticed that calling FetchBlobs twice twice fetches a blob, and also that checking the IsNull property of a field always returns False.

Thus, the only solution I have found so far is to access a property, such as Value or BlobSize, and if blob has not been selected, the exception throws an EDBClient with the message "Blob was not retrieved", so if this exception is raised I call FetchBlobs

Is there a better way to do this?

try cdsIMG.BlobSize; except on E: EDBClient do cds.FetchBlobs; end; 
+6
source share
2 answers

If you needed to know if blob data was retrieved, I believe TOndrej's answer is the way to go. But you do not need ...

When poFetchBlobsOnDemand is set in the DataSetProvider and FetchOnDemand set to "ClientDataSet", the behavior is already described by you. That is, the client dataset calls FetchBlobs only if blob data has not yet been received, and only when necessary.

From " Provider.TProviderOption Enumeration ":

poFetchBlobsOnDemand BLOB fields: not included in data packets. [...] If the client dataset of the FetchOnDemand property is true, the client queries these values ​​automatically. [...]

+1
source

I'm not sure if this is 100% correct, but this is the best I could do. See for yourself.

 type THackCustomClientDataSet = class(TCustomClientDataSet); function IsBlobFetched(DataSet: TCustomClientDataSet; BlobField: TField): Boolean; var I: Integer; Status: DBResult; BlobLen: Cardinal; begin Result := False; BlobLen := 0; with THackCustomClientDataSet(DataSet) do if Assigned(DSCursor) and (ActiveBuffer <> nil) then begin Status := DSCursor.GetBlobLen(ActiveBuffer, BlobField.FieldNo, BlobLen); case Status of DBERR_NONE: Result := True; DBERR_BLOBNOTFETCHED: ; else Check(Status); end; end; end; 

It seems that DBERR_BLOBNOTFETCHED defined in the DSIntf units returned by GetBlobLen if blob has not been selected yet. So the return code means “blob not fetched”, the success return code means “blob fetched already”, and any other error code probably indicates some other error. Inspired by TCustomClientDataSet.CreateBlobStream .

+3
source

Source: https://habr.com/ru/post/890082/


All Articles