unit uDARemoteCommand; {----------------------------------------------------------------------------} { Data Abstract Library - Core Library } { } { compiler: Delphi 6 and up } { platform: Win32, Linux } { } { (c)opyright RemObjects Software. all rights reserved. } { } { Using this code requires a valid license of the Data Abstract } { which can be obtained at http://www.remobjects.com. } {----------------------------------------------------------------------------} {$I DataAbstract.inc} interface uses Classes, uRORemoteService, DataAbstract4_Intf, uDAInterfaces, uRODynamicRequest, uROTypes; type { TRORemoteCommandRequest } TRORemoteCommandRequest = class(TRODynamicRequest) private fOutgoingCommandNameParameter: string; fOutgoingParametersParameter: string; fIncomingAffectedRowsParameter: string; fIncomingParametersParameter: string; public constructor Create(aOwner : TComponent); override; procedure SetupDefaultRequest; virtual; published property OutgoingCommandNameParameter: string read fOutgoingCommandNameParameter write fOutgoingCommandNameParameter; property OutgoingParametersParameter: string read fOutgoingParametersParameter write fOutgoingParametersParameter; property IncomingAffectedRowsParameter: string read fIncomingAffectedRowsParameter write fIncomingAffectedRowsParameter; property IncomingParametersParameter: string read fIncomingParametersParameter write fIncomingParametersParameter; end; { TDARemoteCommand } TDARemoteCommand = class(TComponent) private fRemoteService: TRORemoteService; fExecuteCall: TRORemoteCommandRequest; procedure SetRemoteService(const Value: TRORemoteService); procedure FillCommandParams(aDataParameterArray: DataParameterArray; aCommandParam: TRORequestParam); public constructor Create(aOwner: TComponent); override; destructor Destroy; override; function Execute(aCommandName: string; aInputParameters: DataParameterArray): integer; overload; function Execute(aCommandName: string; aParamNames: array of string; aParamValues: array of variant): integer; overload; published property RemoteService: TRORemoteService read fRemoteService write SetRemoteService; property ExecuteCall: TRORemoteCommandRequest read fExecuteCall write fExecuteCall; end; implementation uses SysUtils, Dialogs, uRODL; { TRORemoteCommandRequest } constructor TRORemoteCommandRequest.Create(aOwner : TComponent); begin inherited; Self.SetupDefaultRequest(); end; procedure TRORemoteCommandRequest.SetupDefaultRequest; begin Params.Clear(); OutgoingCommandNameParameter := Params.Add('aCommandName', rtString, fIn).Name; OutgoingParametersParameter := Params.Add('aParameterArray', rtUserDefined, fIn, 'DataParameterArray').Name; IncomingAffectedRowsParameter := Params.Add('Result', rtInteger, fResult).Name; IncomingParametersParameter := ''; MethodName := 'ExecuteCommand'; end; { TDARemoteCommand } constructor TDARemoteCommand.Create(aOwner: TComponent); begin inherited; fExecuteCall := TRORemoteCommandRequest.Create(Self); fExecuteCall.Name := 'RemoteCommandRequest'; end; destructor TDARemoteCommand.Destroy; begin if Assigned(fExecuteCall) then fExecuteCall.Free(); inherited; end; procedure TDARemoteCommand.SetRemoteService(const Value: TRORemoteService); begin if Value <> fRemoteService then begin fRemoteService := Value; if assigned(fRemoteService) then fRemoteService.FreeNotification(self); fExecuteCall.RemoteService := fRemoteService; end; end; function TDARemoteCommand.Execute(aCommandName: string; aInputParameters: DataParameterArray): integer; var lParam : TRORequestParam; begin result := 0; if not Assigned(fExecuteCall) then raise Exception.Create('ExecuteCall is unassigned'); if (Length(fExecuteCall.OutgoingCommandNameParameter)>0) then fExecuteCall.ParamByName(fExecuteCall.OutgoingCommandNameParameter).Value := aCommandName; if (Length(fExecuteCall.OutgoingParametersParameter)>0) then begin lParam := fExecuteCall.Params.FindParam(fExecuteCall.OutgoingParametersParameter); if Assigned(lParam) then FillCommandParams(aInputParameters, lparam); end; fExecuteCall.Execute(); if (Length(fExecuteCall.IncomingAffectedRowsParameter)>0) then result := Integer(fExecuteCall.ParamByName(fExecuteCall.IncomingAffectedRowsParameter).Value); end; function TDARemoteCommand.Execute(aCommandName: string; aParamNames: array of string; aParamValues: array of variant): integer; var i, idx: Integer; lInputParameters: DataParameterArray; begin lInputParameters := DataParameterArray.Create(); if (High(aParamNames) <> High(aParamValues)) then raise Exception.Create('Error in parameters. Count of parameter values doesn''t correspond to count of parameters'); for i := Low(aParamNames) to High(aParamNames) do begin idx := lInputParameters.Add(DataParameter.Create()); lInputParameters[idx].Name := aParamNames[i]; lInputParameters[idx].Value := aParamValues[i]; end; result := Self.Execute(aCommandName, lInputParameters); end; procedure TDARemoteCommand.FillCommandParams(aDataParameterArray: DataParameterArray; aCommandParam: TRORequestParam); var lArray: DataParameterArray; lParam: DataParameter; i: integer; begin if (aCommandParam.DataType <> rtUserDefined) or (aCommandParam.TypeName <> 'DataParameterArray') then exit; lArray := DataParameterArray.Create(); for i := 0 to aDataParameterArray.Count - 1 do begin lParam := lArray.Add(); lParam.Name := aDataParameterArray[i].Name; lParam.Value := aDataParameterArray[i].Value; end; aCommandParam.AsComplexType := lArray; aCommandParam.OwnsComplexType := true; end; end.