Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to check if a blob field has already been fetched when using poFetchBlobsOnDemand

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;
like image 320
Fabio Gomes Avatar asked Jun 08 '11 18:06

Fabio Gomes


2 Answers

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.

like image 159
Ondrej Kelle Avatar answered Sep 20 '22 11:09

Ondrej Kelle


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. [...]

like image 41
Sertac Akyuz Avatar answered Sep 19 '22 11:09

Sertac Akyuz