用戶端遠程方法調用

來源:互聯網
上載者:User

unit uDM;

interface
{$WARN SYMBOL_PLATFORM OFF}
uses
  SysUtils, Classes, Controls,DB, DBClient, MConnect, SConnect, Dialogs,
  Variants, ADODB, IniFiles, Forms, MidServer_TLB, uFun;

type
  TSvrRec = record       // socketConnection's property
    Address: string;
    Port: Integer;
    ServerName: string;
  end;

  Tdm = class(TDataModule)
    Conn: TSocketConnection;
    ParamsADO: TADOQuery;
    cdsCaption: TClientDataSet;
    cdsRights: TClientDataSet;
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    svrRec: TSvrRec;
    procedure GetConfig;
    function tryConnect:Boolean;
    procedure DisConn;
    function Loader: ITestDisp;
  public
    { Public declarations }
    function GetData(cds: TClientDataSet; const ModuleId: String; SqlId: integer; haveParams: Boolean = False):Boolean;
    function ExecSQL(const ModuleId: string; SqlId: integer;haveParams:Boolean=False):Integer;
    function GetStoredData(cds: TClientDataSet; const ModuleId: string; SqlId: integer;haveParams:Boolean=False):Boolean;
    function ExecStored(const ModuleId: string; SqlId: integer;haveParams:Boolean=False):integer;
    procedure ApplyUpdate(Const ModuleId: String; SqlId: integer; Cds: TClientDataSet);
    procedure ClearParameters;
    procedure ApplyUpdates(const moduleId:string;sqlId:Integer;delta0,delta1,delta2,delta3:OleVariant);
    procedure AddParameter(const ParamName: string;
      DataType: TFieldType; Value: OleVariant);
    procedure SetFieldsDef(const ModuleId: string; SqlId: Integer; Cds: TClientDataSet);
    procedure SetCaptions(form: TForm; const ModuleId: string);
    procedure GetRightsList(const UserId,ModuleId:string;RightsList:TStringList);
    function CheckUser(const UserId,Password:string):Integer;
  end;

var
  dm: Tdm;

implementation

{$R *.dfm}

uses ZLibEx;

procedure tdm.AddParameter(const ParamName: string;
  DataType: TFieldType; Value: OleVariant);
begin
  try
    ParamsADO.Parameters.CreateParameter(ParamName, DataType, pdInput, SizeOf(Value), Value);
  except
    exit;
  end;
end;
{
procedure AddParam(Params: TParams; const ParamName: string;
  DataType: TFieldType; Value: OleVariant);
var
  p: TParam;
begin
  try
    p := Params.CreateParam(DataType, ParamName, ptInput);
    p.Value := Value;
    p.Size := SizeOf(Value);
  except
    exit;
  end;
end;    }

procedure Tdm.ApplyUpdate(const ModuleId: String; SqlId: integer;
  Cds: TClientDataSet);
var
  r:Shortint;
begin
  tryConnect;
  if Cds.State in [dsEdit, dsInsert] then cds.Post;
  if Cds.ChangeCount=0 then exit;
  r :=loader.ApplyUpdate(ModuleId, SqlId, CompressData(Cds.Delta));
  if r=1 then
    Cds.MergeChangeLog
  else raise Exception.Create('post data fail');
end;

function Tdm.ExecSQL(const ModuleId: string; SqlId: integer; haveParams: Boolean=False):Integer;
begin
  tryConnect;
  if haveParams then
    Result := loader.ExecSQL(ModuleId, SqlId, ParametersToVariant(ParamsADO.Parameters))
  else
    Result := loader.ExecSQL(ModuleId, SqlId, Null);
end;

function Tdm.GetData(cds: TClientDataSet; const ModuleId: String;
  SqlId: integer; haveParams: Boolean = False):Boolean;
begin
  tryConnect;
  if haveParams then
    cds.Data := DeCompressData(loader.QryData(ModuleId, SqlId, ParametersToVariant(ParamsADO.Parameters)))
  else
    cds.Data := DeCompressData(loader.QryData(ModuleId, SqlId, null));
  Result :=not cds.IsEmpty;
end;

function Tdm.TryConnect:Boolean;
begin
  Result := False;
  if not self.Conn.Connected then
  begin
    try
      self.Conn.Address:=svrRec.Address;  
      self.Conn.Port:=svrRec.Port;
      Conn.ServerName := svrRec.ServerName;
      self.Conn.Connected:=True;
      Result:=True;
    Except
      on E:Exception do
        raise Exception.Create('串連伺服器失敗'+e.Message);
    end;
  end;
end;

procedure Tdm.GetConfig;
var
  ini: TIniFile;
begin
  ini := TIniFile.Create(ExtractFilePath(Application.ExeName)+'client.ini');
  svrRec.Address := ini.ReadString('server', 'address', '');
  svrRec.Port := ini.ReadInteger('server', 'port', 211);
  svrRec.ServerName := ini.ReadString('server', 'servername', '');
  ini.Free;
end;

procedure Tdm.DataModuleCreate(Sender: TObject);
begin
  GetConfig;
  tryConnect;
end;

function Tdm.Loader: ITestDisp;
begin
  Result := ITestDISP(IDispatch(Conn.Appserver));
end;

function Tdm.GetStoredData(cds: TClientDataSet; const ModuleId: string;
  SqlId: integer;haveParams:Boolean=False):Boolean;
begin
  tryConnect;
  if haveParams then
    cds.Data := DeCompressData(Loader.GetStoredData(ModuleId, SqlId, ParametersToVariant(ParamsADO.Parameters)))
  else
    cds.Data := DeCompressData(Loader.GetStoredData(ModuleId, SqlId, Null));
  Result :=not cds.IsEmpty;
end;

function Tdm.ExecStored(const ModuleId: string; SqlId: integer;haveParams:Boolean=False):Integer;
begin
  tryConnect;
  if haveParams then
    Result := Loader.ExecStored(ModuleId, SqlId, ParametersToVariant(ParamsADO.Parameters))
  else
    Result := Loader.ExecStored(ModuleId, SqlId, Null);
end;

procedure Tdm.DisConn;
begin
  Conn.Close;
end;

procedure Tdm.ClearParameters;
begin
  ParamsADO.Parameters.Clear;
end;

procedure Tdm.SetFieldsDef(const ModuleId: string; SqlId: Integer;
  Cds: TClientDataSet);
var
  tmpCDS: TClientDataSet;
  Field: TField;
  sIndexFieldsName: string;
begin
  tmpCDS := TClientDataSet.Create(self);
  try
    tmpCDS.Data := Loader.GetFieldsDef(ModuleId, SqlId);
    if not tmpCDS.IsEmpty then
    begin
      sIndexFieldsName := '';
      tmpCDS.First;
      while tmpCDS.Eof do
      begin
        Field := Cds.FindField(tmpCDS.Fieldbyname('fieldName').AsString);
        if Assigned(Field) then
        begin
          Field.DisplayLabel := tmpCDS.fieldbyname('cnName').AsString;
          Field.Index := tmpCDS.fieldbyname('index').AsInteger;
          Field.DisplayWidth := tmpCDS.fieldbyname('width').AsInteger;
          Field.ReadOnly := tmpCDS.FieldByName('readOnly').AsBoolean;
          Field.Visible := tmpCDS.FieldByName('visible').AsBoolean;
          Field.Required := tmpCDS.FieldByName('isSave').AsBoolean;
          if tmpCDS.FieldByName('isKey').AsBoolean then
            sIndexFieldsName := sIndexFieldsName + ';' + Field.FieldName;
        end;
        tmpCDS.Next;
      end;
      if Length(sIndexFieldsName) > 1 then
      begin
        sIndexFieldsName := Copy(sIndexFieldsName, 2, Length(sIndexFieldsName));
        Cds.IndexFieldNames := sIndexFieldsName;
      end;
    end;
  finally
    tmpCDS.Free;
  end;
end;

procedure Tdm.DataModuleDestroy(Sender: TObject);
begin
  DisConn;
end;

procedure Tdm.SetCaptions(form: TForm; const ModuleId: string);
begin
  cdsCaption.Data := Loader.GetCaptions(ModuleId);
  if (cdsCaption.Active) and (not cdsCaption.IsEmpty) then
  begin
    cdsCaption.First;
    while not cdsCaption.Eof do
    begin
      TForm(form.FindComponent(cdsCaption.fieldbyname('controlName').AsString)).Caption := cdsCaption.fieldbyname('cnName').AsString;
      cdsCaption.Next;
    end;
  end;
end;

procedure Tdm.ApplyUpdates(const moduleId: string; sqlId: Integer; delta0,
  delta1, delta2, delta3: OleVariant);
begin
  if Loader.ApplyUpdates(moduleId,sqlId,delta0,delta1,delta2,delta3) = 0 then
    raise Exception.Create('post data fail');
end;

procedure Tdm.GetRightsList(const UserId,ModuleId:string;RightsList:TStringList);
var
  i:Integer;
begin
  if UserId ='' then Exit;
  if ModuleId ='' then Exit;
  if not Assigned(RightsList) then Exit;
  cdsRights.Data :=Loader.GetRights(UserId,ModuleId);
  if (cdsRights.IsEmpty) or (cdsRights.FieldCount =0) then exit;
  RightsList.Clear;
  for i :=0 to cdsRights.FieldCount - 1 do
  begin
    if cdsRights.Fields[i].AsBoolean then
      RightsList.Add(cdsRights.Fields[i].FieldName);
  end;
end;

function Tdm.CheckUser(const UserId, Password: string):Integer;
begin
  Result := Loader.CheckUser(UserId,Password);
end;

end.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.