Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.

Public Report
Report From: Delphi-BCB/Database/DBExpress    [ Add a report in this area ]  
Report #:  103567   Status: Reported
Automatic parameter creation..
Project:  Delphi Build #:  16.0.4358.45540
Version:    16.3 Submitted By:   Tugrul HELVACI
Report Type:  Suggestion / Enhancement Request Date Reported:  2/21/2012 12:43:42 AM
Severity:    Commonly encountered problem Last Updated:
Platform:    All platforms Internal Tracking #:  
Resolution: None  Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: (1 Total Rating)
5.00 out of 5
Total Votes: None
Description
When i want to use SQL Server stored procedure via DBX framework i have to create many TDBXParameter objects and process that parameters. It is not an easy.. I think we can call some method to create automatic parameters like TADOStoredProc.Parameters.Refresh.

I think you can add some helper function to creating parameters automatically. My solution like this;

  TDBXCommandEx = class(TDBXMorphicCommand)
  protected
    procedure SetText(const Value: UnicodeString); override;
  end;

procedure TDBXCommandEx.SetText(const Value: UnicodeString);
  procedure DeleteListObjects(const AList : TList);
  begin
    while AList.Count > 0 do
    begin
      TObject(AList.Items[0]).Free;
      AList.Delete(0);
    end;
  end;
var
  ParentConnection : TPersistent; // TSQLConnection
  ParamList        : TList;
  Param            : SPParamDesc;
  iCounter         : Integer;
  DBXParam         : TDBXParameter;
begin
  inherited;

  if Self.CommandType <> TDBXCommandTypes.DbxStoredProcedure then Exit;
  if Trim(Self.Text) = '' then Exit;

  ParentConnection := nil;

  if FConnection <> nil then
    if FConnection.ConnectionProperties <> nil then
    begin
      ParentConnection := FConnection.ConnectionProperties.GetOwner;
      if ParentConnection <> nil then
        if ParentConnection is TSQLConnection then
        begin
          ParamList := TList.Create;
          try
            TSQLConnection(ParentConnection).GetProcedureParams( Self.Text, ParamList );

            for iCounter := 0 to ParamList.Count - 1 do
            begin
              if ParamList.Items[iCounter] <> nil then
                if TObject(ParamList.Items[iCounter]) is SPParamDesc then
                begin
                  Param := SPParamDesc(ParamList.Items[iCounter]);

                  DBXParam := Self.CreateParameter;

                  // Direction
                  case Param.iArgType of
                    ptUnknown         : DBXParam.ParameterDirection := TDBXParameterDirections.Unknown;
                    ptInput           : DBXParam.ParameterDirection := TDBXParameterDirections.InParameter;
                    ptOutput          : DBXParam.ParameterDirection := TDBXParameterDirections.OutParameter;
                    ptInputOutput     : DBXParam.ParameterDirection := TDBXParameterDirections.InOutParameter;
                    ptResult          : DBXParam.ParameterDirection := TDBXParameterDirections.ReturnParameter;
                  end;

                  // Data type
                  DBXParam.DataType := FldTypeMap[Param.iDataType];

                  // Name
                  DBXParam.Name := Param.szName;

                  if Param.iArgType <> ptResult then
                    DBXParam.Value.SetNull;

                  Parameters.AddParameter( DBXParam );
                end; // if TObject(ParamList.Items[iCounter]) is SPParamDesc then
            end; // for iCounter := 0 to ParamList.Count - 1 do
          finally
            DeleteListObjects(ParamList);
            ParamList.Free;
          end; // try/finally
        end; // if ParentConnection is TSQLConnection then
    end; // if FConnection.ConnectionProperties <> nil then
end;
Steps to Reproduce:
None
Workarounds
None
Attachment
None
Comments

None

Server Response from: ETNACODE01