I have a TClientDataSet with several records, and I want o load all the records, but load the blob field on Demand, one at a time.
I noticed that calling FetchBlobs twice fetches the blob twice and also that checking the field's IsNull property always returns False.
So the only solution I found so far is to access a property like Value or BlobSize and if the blob has not been fetched an exception is raised EDBClient with message "Blob has not been fetched", so if this exception is raised I call the FetchBlobs.
Is there some better way of doing this?
try
cdsIMG.BlobSize;
except
on E: EDBClient do
cds.FetchBlobs;
end;
I'm not sure if this is 100% correct but it's 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;
There seems to be DBERR_BLOBNOTFETCHED
defined in DSIntf
unit to be returned by GetBlobLen
in case the blob has not been fetched yet. So that return code means 'blob not fetched', success return code means 'blob fetched already', and any other error code probably indicates some other error.
Inspired by TCustomClientDataSet.CreateBlobStream
.
If you had to know if a blob's data had been retrieved, I believe, TOndrej's answer would be the way to go. But you don't have to..
When poFetchBlobsOnDemand
is set in 'DataSetProvider's options, and FetchOnDemand
is set on the 'ClientDataSet', the behavior is already as you describe. I.e. the client data set calls FetchBlobs
only if the blob data has not already been retrieved and only when it is needed.
From "Provider.TProviderOption Enumeration":
poFetchBlobsOnDemand BLOB fields are not included in data packets. [...] If the client dataset's FetchOnDemand property is true, the client requests these values automatically. [...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With