码迷,mamicode.com
首页 > 其他好文 > 详细

mormot接口封装

时间:2020-02-12 10:32:20      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:client   highlight   adb   var   info   img   ted   csharp   ring   

mormot接口封装

mormot基于http.sys通讯,支持海量并发,用mormot开发的服务端,长时间运行非常稳定。

经常有人说mormot晦涩难懂,但经过封装以后的MORMOT接口,调用异常简单。支持DELPHI7及以上版本调用哦!

技术图片

procedure TForm1.DownFile(filename: string);
// 下载文件
var
  url: SockString;
  i: integer;
  serial: TynSerial;
  ms: TMemoryStream;
begin
  serial := TynSerial.Create;
  ms := TMemoryStream.Create;
  try
    url := ‘downfile‘;
    serial.WriteString(‘filename‘, filename);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin
      serial.Text := FHttp.Content;
      serial.ReadFile(‘filecontent‘, ms);
      ms.SaveToFile(‘d:\‘ + filename);
    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
    ms.Free;
  end;
end;

  

procedure TForm1.Qry(accountno, sql: string; DataSet: TFDMemTable);
// TFDMemTable查询
var
  url: SockString;
  i: integer;
  serial: TynSerial;
begin
  serial := TynSerial.Create;
  try
    url := ‘query2‘;
    serial.WriteString(‘accountno‘, accountno);
    serial.WriteString(‘sql‘, sql);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin
      serial.Text := FHttp.Content;
      serial.ReadFDDataSet(‘dataset‘, DataSet);
    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
  end;
end;

  

procedure TForm1.Qry(accountno, sql: string; DataSet: TClientDataSet);
// TClientDataSet查询
var
  url: SockString;
  i: integer;
  serial: TynSerial;
begin
  serial := TynSerial.Create;
  try
    url := ‘query3‘;
    serial.WriteString(‘accountno‘, accountno);
    serial.WriteString(‘sql‘, sql);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin
      serial.Text := FHttp.Content;
      serial.ReadClientDataSet(‘dataset‘, DataSet);
    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
  end;
end;

  

procedure TForm1.redisQry(cacheKey, accountno, sql: string; DataSet: TFDMemTable);
// redis缓存 查询
var
  url: SockString;
  i: integer;
  serial: TynSerial;
begin
  serial := TynSerial.Create;
  try
    url := ‘redis‘;
    serial.WriteString(‘accountno‘, accountno);
    serial.WriteString(‘sql‘, sql);
    serial.WriteString(‘cachekey‘, cacheKey);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin
      serial.Text := FHttp.Content;
      serial.ReadFDDataSet(‘dataset‘, DataSet);
    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
  end;
end;

  

procedure TForm1.Qry(middleMark, accountno, sql: string; DataSet: TFDMemTable);
// 中间件桥接查询
var
  url: SockString;
  i: integer;
  serial: TynSerial;
begin
  serial := TynSerial.Create;
  try
    url := ‘bridgequery‘;
    serial.WriteString(‘accountno‘, accountno);
    serial.WriteString(‘sql‘, sql);
    serial.WriteString(‘middlemark‘, middleMark);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin
      serial.Text := FHttp.Content;
      serial.ReadFDDataSet(‘dataset‘, DataSet);
    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
  end;
end;

  

procedure TForm1.Qrys(accountno, sql, sql2: string; DataSet, DataSet2: TClientDataSet);
// TClientDataSet主从表查询
var
  url: SockString;
  i: integer;
  serial: TynSerial;
begin
  serial := TynSerial.Create;
  try
    url := ‘querys2‘;
    serial.WriteString(‘accountno‘, accountno);
    serial.WriteString(‘sql‘, sql);
    serial.WriteString(‘sql2‘, sql2);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin
      serial.Text := FHttp.Content;
      serial.ReadClientDataSet(‘dataset‘, DataSet);
      serial.ReadClientDataSet(‘dataset2‘, DataSet2);
    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
  end;
end;

  

procedure TForm1.Qrys(accountno, sql, sql2: string; DataSet, DataSet2: TFDMemTable);
// TFDMemTable主从表查询
var
  url: SockString;
  i: integer;
  serial: TynSerial;
begin
  serial := TynSerial.Create;
  try
    url := ‘querys‘;
    serial.WriteString(‘accountno‘, accountno);
    serial.WriteString(‘sql‘, sql);
    serial.WriteString(‘sql2‘, sql2);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin
      serial.Text := FHttp.Content;
      serial.ReadFDDataSet(‘dataset‘, DataSet);
      serial.ReadFDDataSet(‘dataset2‘, DataSet2);
    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
  end;
end;

  

function TForm1.Save(accountno, tablename: string; dataset: TClientDataSet): string;
// clientDataSet提交
var
  url: SockString;
  i: Integer;
  serial: TynSerial;
begin
  serial := TynSerial.Create;
  try
    url := ‘save‘;
    serial.WriteString(‘accountno‘, accountno);
    serial.WriteString(‘tablename‘, tablename);
    serial.WriteDelta(‘delta‘, dataset);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin
      serial.Text := FHttp.Content;
      if serial.ReadBool(‘result‘) then
        dataset.MergeChangeLog;
    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
  end;
end;

  

function TForm1.Save(accountno, tablename: string; dataset: TFDMemTable): string;
// TFDMemTable提交
var
  url: SockString;
  i: Integer;
  serial: TynSerial;
begin
  serial := TynSerial.Create;
  try
    url := ‘save2‘;
    serial.WriteString(‘accountno‘, accountno);
    serial.WriteString(‘tablename‘, tablename);
    serial.WriteFDDelta(‘delta‘, dataset);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin
      serial.Text := FHttp.Content;
      if serial.ReadBool(‘result‘) then
        dataset.MergeChangeLog
      else
        ShowMessage(‘提交失败‘);
    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
  end;
end;

  

procedure TForm1.StoredProc(accountno, spname, params: string; DataSet: TClientDataSet);
// 存储过程
var
  url: SockString;
  i: Integer;
  serial: TynSerial;
begin
  serial := TynSerial.Create;
  try
    url := ‘spopen3‘;
    serial.WriteString(‘accountno‘, accountno);
    serial.WriteString(‘spname‘, spname);
    serial.WriteString(‘params‘, params);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin
      serial.Text := FHttp.Content;
      serial.ReadClientDataSet(‘dataset‘, DataSet);
    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
  end;
end;

  

procedure TForm1.UpFile(filename: string);
// 上传文件
var
  url: SockString;
  i: Integer;
  serial: TynSerial;
begin
  serial := TynSerial.Create;
  try
    url := ‘upfile‘;
    serial.WriteFile(‘filecontent‘, filename);
    serial.WriteString(‘filename‘, filename);
    i := FHttp.Post(url, serial.Text, FDataType);
    if i = 200 then
    begin

    end
    else
      ShowMessage(i.ToString + ‘ error‘);
  finally
    serial.Free;
  end;
end;

  

mormot接口封装

标签:client   highlight   adb   var   info   img   ted   csharp   ring   

原文地址:https://www.cnblogs.com/hnxxcxg/p/12297854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!