MSGPACK序列和还原TFDParams
MSGPACK序列和还原TFDParams
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, qmsgpack, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client, Datasnap.DBClient, Vcl.StdCtrls;
type
TForm1 = class(TForm)
FDQuery1: TFDQuery;
cds1: TClientDataSet;
btn1: TButton;
mmo1: TMemo;
mmo2: TMemo;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function ParamsToStream(aParams: TFDParams): TStream;
function StreamTOParams(Stream: TStream): TFDParams;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
TMS: TStream;
i: Integer;
str: AnsiString;
begin
with FDQuery1.Params do //创建参数
begin
Clear;
CreateParam(ftString, 'ColStr', ptInput).AsString := '字符串';
CreateParam(ftFloat, 'ColFloat', ptInput).AsFloat := 168.168;
CreateParam(ftInteger, 'ColSmallint', ptInput).AsInteger := 1234;
CreateParam(ftBoolean, 'ColBool', ptInput).AsBoolean := true;
CreateParam(ftDateTime, 'ColDatTime', ptInput).AsDateTime := now;
end;
mmo1.Clear;
mmo1.Lines.Add('序列化之前的Params');
for I := 0 to FDQuery1.Params.Count - 1 do //输出
begin
with FDQuery1 do
begin
str := Format('Name:%s Value:%s DataType:%d ParamType:%d Size:%d Precision:%d NumericScale:%d', [Params[I].Name, Params[I].Value, Ord(Params[I].DataType), Ord(Params[I].ParamType), Params[I].Size, Params[I].Precision, Params[I].NumericScale]);
mmo1.Lines.Add(str)
end;
end;
TMS := ParamsToStream(FDQuery1.Params);//序列化参数
FDQuery1.Params.Clear;
TMS.Position := 0;
FDQuery1.Params.Assign(StreamToParams(TMS));//还原参数
mmo2.Clear;
mmo2.Lines.Add('序列化还原后的Params');
for I := 0 to FDQuery1.Params.Count - 1 do //输出
begin
with FDQuery1 do
begin
str := Format('Name:%s Value:%s DataType:%d ParamType:%d Size:%d Precision:%d NumericScale:%d', [Params[I].Name, Params[I].Value, Ord(Params[I].DataType), Ord(Params[I].ParamType), Params[I].Size, Params[I].Precision, Params[I].NumericScale]);
mmo2.Lines.Add(str);
end;
end;
end;
/// <summary>
/// 参数序列化流
/// </summary>
/// <param name="aParams"></param>
/// <returns>TStream</returns>
function TForm1.ParamsToStream(aParams: TFDParams): TStream;
var
QMP: TQMsgPack;
I, Idx, Count: Integer;
begin
QMP := TQMsgPack.Create;
Result := TMemoryStream.Create;
try
Count := 0;
for I := 0 to aParams.Count - 1 do
if aParams[I].ParamType in AllParamTypes then
Inc(Count);
if Count > 0 then
begin
Idx := 0;
for I := 0 to aParams.Count - 1 do
if aParams[I].ParamType in AllParamTypes then
begin
QMP.ForcePath(idx.ToString).ForcePath('Name').AsString := aParams[I].Name;
QMP.ForcePath(idx.ToString).ForcePath('Value').AsVariant := aParams[I].Value;
QMP.ForcePath(idx.ToString).ForcePath('DataType').AsInteger := Ord(aParams[I].DataType);
QMP.ForcePath(idx.ToString).ForcePath('ParamType').AsInteger := Ord(aParams[I].ParamType);
QMP.ForcePath(idx.ToString).ForcePath('Size').AsInteger := aParams[I].Size;
QMP.ForcePath(idx.ToString).ForcePath('Precision').AsInteger := aParams[I].Precision;
QMP.ForcePath(idx.ToString).ForcePath('NumericScale').AsInteger := aParams[I].NumericScale;
Inc(Idx);
end;
end;
QMP.SaveToStream(Result);
finally
QMP.Free;
end;
end;
/// <summary>
/// 流还原成 TFDParams
/// </summary>
/// <param name="Stream"></param>
/// <returns>TFDParams</returns>
function TForm1.StreamTOParams(Stream: TStream): TFDParams;
var
QMP: TQMsgPack;
LParam: TFDParam;
I: Integer;
begin
QMP := TQMsgPack.Create;
Result := TFDParams.Create;
try
Stream.Position := 0;
QMP.LoadFromStream(Stream);
for I := 0 to QMP.Count - 1 do
begin
LParam := TFDParam(Result.Add);
LParam.Name := QMP.ForcePath(i.ToString).ForcePath('Name').AsString;
LParam.DataType := TFieldType(QMP.ForcePath(i.ToString).ForcePath('DataType').AsInteger);
LParam.ParamType := TParamType(QMP.ForcePath(i.ToString).ForcePath('ParamType').AsInteger);
LParam.Size := QMP.ForcePath(i.ToString).ForcePath('Size').AsInteger;
LParam.Precision := QMP.ForcePath(i.ToString).ForcePath('Precision').AsInteger;
LParam.NumericScale := QMP.ForcePath(i.ToString).ForcePath('NumericScale').AsInteger;
LParam.Value := QMP.ForcePath(i.ToString).ForcePath('Value').AsVariant;
end;
finally
QMP.Free;
end;
end;
end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/7680635.html