mormot接口封装
Posted hnxxcxg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mormot接口封装相关的知识,希望对你有一定的参考价值。
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接口封装的主要内容,如果未能解决你的问题,请参考以下文章
Delphi语言最好的JSON代码库 mORMot学习笔记1