使用二进制序列

使用二进制序列

作者将二进制序列类设计成为一个键-值对。

引用二进制序列单元:

uses core.binary;

二进制序列类的接口:

  TData = class
  private
    FKey: str; // The key must be unique
    FValue: TBytes; // value
  private
    FList: TList;
  private
    function Path(const AKey: str): TData;
    function GetByteCount: int;
  private
    function GetByte(const AKey: str): Byte;
    procedure SetByte(const AKey: str; const AValue: Byte);
    function GetWord(const AKey: str): Word;
    procedure SetWord(const AKey: str; const AValue: Word);
    function GetCardinal(const AKey: str): Cardinal;
    procedure SetCardinal(const AKey: str; const AValue: Cardinal);
    function GetI(const AKey: str): int;
    procedure SetI(const AKey: str; const AValue: int);
    function GetI64(const AKey: str): Int64;
    procedure SetI64(const AKey: str; const AValue: Int64);
    function GetB(const AKey: str): bool;
    procedure SetB(const AKey: str; const AValue: bool);
    function GetD(const AKey: str): Double;
    procedure SetD(const AKey: str; const AValue: Double);
    function GetDT(const AKey: str): TDateTime;
    procedure SetDT(const AKey: str; const AValue: TDateTime);
    function GetC(const AKey: str): Currency;
    procedure SetC(const AKey: str; const AValue: Currency);
    function GetS(const AKey: str): str;
    procedure SetS(const AKey, AValue: str);
    function GetV(const AKey: str): Variant;
    procedure SetV(const AKey: str; const AValue: Variant);
    function GetST(const AKey: str): TStream;
    procedure SetST(const AKey: str; const AValue: TStream);
    function GetBT(const AKey: str): TBytes;
    procedure SetBT(const AKey: str; const AValue: TBytes);
  public
    property Byte[const key: str]: Byte read GetByte write SetByte;
    property Word[const key: str]: Word read GetWord write SetWord;
    property Cardinal[const key: str]: Cardinal read GetCardinal write SetCardinal;
    property I[const key: str]: int read GetI write SetI;
    property I64[const key: str]: Int64 read GetI64 write SetI64;
    property B[const key: str]: bool read GetB write SetB;
    property C[const key: str]: Currency read GetC write SetC;
    property D[const key: str]: Double read GetD write SetD;
    property DateTime[const key: str]: TDateTime read GetDT write SetDT;
    property S[const key: str]: str read GetS write SetS;
    property V[const key: str]: Variant read GetV write SetV;
    // TClientDataset's data and delta
    property Stream[const key: str]: TStream read GetST write SetST;
    property Bytes[const key: str]: TBytes read GetBT write SetBT;
  public // marshal
    procedure ToStream(AStream: TStream);
    function ToRaw: str;
    function ToBytes: TBytes;
  public // unmarshal
    procedure FromStream(AStream: TStream);
    procedure FromRaw(const AValue: str);
    procedure FromBytes(const AValue: TBytes);
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear; // clear list;
  end;

使用方法,请看代码注释:

class function TRpc.Query(const ADbId: string; const ASqls: TArray<string>;
  const ADataSets: TArray<TDataSet>): Boolean;
var
  LData: TData;//二进制序列类
  LRequest, LResponse: TStream;
  LHttp: THttpClient;
  I: Integer;
begin
  LData := TData.Create;//创建二进制序列类
  LRequest := TMemoryStream.Create;
  LResponse := TMemoryStream.Create;
  LHttp := NewHttp;
  try
    LData.I['count'] := High(ASqls);//给二进制序列类赋值
    LData.s['dbid'] := ADbId;
    for I := 0 to High(ASqls) do
      LData.s['sql' + I.ToString] := ASqls[I];
    LData.ToStream(LRequest);//序列为流
    LHttp.Post(GUrl + '/cds/select', LRequest, LResponse);
    LData.Clear;
    LData.FromStream(LResponse);//从流还原
    Result := LData.B['success'];
    if Result then
    begin
      for I := 0 to High(ADataSets) do
      begin
        ADataSets[I].DisableControls;
        TClientDataSet(ADataSets[I]).Data := LData.V['dataset' + I.ToString];
        ADataSets[I].EnableControls;
      end;
    end;
  finally
    LData.Free;//释放
    LRequest.Free;
    LResponse.Free;
    LHttp.Free;
  end;
end;

 

posted @ 2025-04-29 11:53  delphi中间件  阅读(37)  评论(0)    收藏  举报