mormot接口封装

2020/02/12 09:36
阅读数 216

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;

  

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部