TMSConnection.OpenDatasets & macros

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Ludek
Posts: 296
Joined: Thu 12 Oct 2006 09:34

TMSConnection.OpenDatasets & macros

Post by Ludek » Fri 17 Apr 2015 08:03

Hi,
I have currently found out, that the TMSConnection.OpenDatasets method does not correctly replace macros - all macros are replaced only with empty string!
Is there some simple workaround?
Thanks, Ludek.

Ludek
Posts: 296
Joined: Thu 12 Oct 2006 09:34

Re: TMSConnection.OpenDatasets & macros

Post by Ludek » Fri 17 Apr 2015 09:01

would you mind using such helper method instead?

Code: Select all

type
  TCustomMSDatasetHelper = class helper for TCustomMSDataset
  private
    procedure SetBulkExecuting(value: boolean);
    function GetFIRecordSet: TOLEDBRecordSet;
  public
    procedure _DoBeforeOpenCursor;
    procedure _CloseCursor;
    property _FBulkExecuting: boolean write SetBulkExecuting;
    property _FIRecordset: TOLEDBRecordSet read GetFIRecordset;
  end;

  TCustomMSConnectionHelper = class helper for TCustomMSConnection
  private
    function GetFDataset: TCustomMSDataset;
    procedure SetFDataset(const Value: TCustomMSDataset);
  public
    property _FDataset: TCustomMSDataset read GetFDataset write SetFDataset;
  end;

{ TCustomMSDatasetHelper }

procedure TCustomMSDatasetHelper.SetBulkExecuting(value: boolean);
begin
  FBulkExecuting := value;
end;

procedure TCustomMSDatasetHelper._DoBeforeOpenCursor;
begin
  DoBeforeOpenCursor;
end;

function TCustomMSDatasetHelper.GetFIRecordSet: TOLEDBRecordSet;
begin
  result := FIRecordSet;
end;

procedure TCustomMSDatasetHelper._CloseCursor;
begin
  CloseCursor;
end;

{ TCustomMSConnectionHelper }

function TCustomMSConnectionHelper.GetFDataset: TCustomMSDataset;
begin
  result := FDataset;
end;

procedure TCustomMSConnectionHelper.SetFDataset(const Value: TCustomMSDataset);
begin
  FDataset := value;
end;

procedure OpenDatasets(c: TCustomMSConnection; const ds: array of TCustomMSDataset);
var
  OldAutoPrepare: boolean;
  i, p: Integer;
begin
  if c._FDataSet = nil then
    c._FDataSet := c.CreateDataSet as TCustomMSDataSet;

  c._FDataSet._FBulkExecuting := True;
  c._FDataSet.SQL.Text := '';
  c._FDataSet.ParamCheck := False;
  c._FDataSet.Params.Clear;

  for i := Low(ds) to High(ds) do begin
    ds[i]._DoBeforeOpenCursor;
    if ds[i].CursorType <> ctDefaultResultSet then
      DatabaseError(SUnallowableCursorType, ds[i]);

    c._FDataSet.SQL.Add(ds[i].FinalSQL); // finalsql insted of simple sql.text
    for p := 0 to ds[i].Params.Count - 1 do
      c._FDataSet.Params.Add.Assign(ds[i].Params[p]);
  end;

  try
    if c._FDataSet._FIRecordSet <> nil then
      c._FDataSet._FIRecordSet.SetProp(prBulkExecuting, True);

    c._FDataSet.Execute;
    Assert(c._FDataSet._FIRecordSet <> nil);

    for i := Low(ds) to High(ds) do begin
      c._FDataSet._FIRecordSet.AssignToNextResult(ds[i]._FIRecordSet);

      OldAutoPrepare := ds[i].Options.AutoPrepare;
      try
        ds[i].Options.AutoPrepare := False;
        ds[i].Open;
      finally
        ds[i].Options.AutoPrepare := OldAutoPrepare;
      end;
    end;
  finally
    c._FDataSet._FIRecordSet.Active := True;
    c._FDataSet._CloseCursor;
  end;
end;
I replaced adding of the .sql.text with adding of .finalsql... and some underscores to access protected elements...

azyk
Devart Team
Posts: 1057
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: TMSConnection.OpenDatasets & macros

Post by azyk » Mon 20 Apr 2015 12:07

Thank you for the information. We will change this behavior in one of the next versions.

Post Reply