阿炮 發表於 2022-7-13 09:01:00

delphi基于泛型结构的数据序列

<p>delphi基于泛型结构的数据序列</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">unit server.rest.api;
/// &lt;author&gt;cxg 2022-7-13&lt;/author&gt;

interface

uses
IdHTTP, System.Classes, Grijjy.ProtocolBuffers, System.NetEncoding,
System.JSON.Serializers, System.SysUtils;

type
TRest = class
public
    /// &lt;summary&gt;
    /// 查询
    /// &lt;/summary&gt;
    /// &lt;param name="resource"&gt;资源&lt;/param&gt;
    /// &lt;param name="dbid"&gt;数据库id&lt;/param&gt;
    /// &lt;param name="where"&gt;查询条件,例如:unitname like '%'&lt;/param&gt;
    /// &lt;returns&gt;记录数组&lt;/returns&gt;
    class function select&lt;T: record&gt;(const resource: string; dbid: string = '1'; where: string = ''): T;
    class function insert&lt;T: record&gt;(const resource: string; const aRecord: T; dbid: string = '1'): string;
    class function update&lt;T: record&gt;(const resource: string; const aRecord: T; dbid: string = '1'): string;
    /// &lt;summary&gt;
    /// 删除
    /// &lt;/summary&gt;
    /// &lt;param name="resource"&gt;资源&lt;/param&gt;
    /// &lt;param name="where"&gt;删除条件,例如:unitid='10'&lt;/param&gt;
    /// &lt;param name="dbid"&gt;数据库id&lt;/param&gt;
    /// &lt;returns&gt;json&lt;/returns&gt;
    class function delete(const resource: string; where: string; dbid: string = '1'): string;
    //protobuf CRUD
    class function select2&lt;T: record&gt;(const resource: string; dbid: string = '1'; where: string = ''): T;
    class function delete2(const resource: string; where: string; dbid: string = '1'): string;
    class function insert2&lt;T: record&gt;(const resource: string; const aRecord: T; dbid: string = '1'): tbytes;
    class function update2&lt;T: record&gt;(const resource: string; const aRecord: T; dbid: string = '1'): tbytes;
    //unmarshal
    class function unmarshal&lt;T: record&gt;(const value: string): T; overload;
    class function unmarshal&lt;T: record&gt;(const value: tbytes): T; overload;
end;

var
ipport: string = 'http://127.0.0.1:1234';
http: TIdHTTP;

implementation

{ TRest }

class function TRest.unmarshal&lt;T&gt;(const value: string): T;
begin
var serial: TJsonSerializer := TJsonSerializer.Create;
Result := serial.Deserialize&lt;T&gt;(value);
serial.free;
end;

class function TRest.unmarshal&lt;T&gt;(const value: tbytes): T;
begin
var serial: TgoProtocolBuffer := TgoProtocolBuffer.Create;
serial.Deserialize&lt;T&gt;(result, value);
serial.Free;
end;

class function TRest.update&lt;T&gt;(const resource: string; const aRecord: T;
dbid: string): string;
begin
var serial: TJsonSerializer := TJsonSerializer.Create;
var req: TBytesStream := TBytesStream.Create(TEncoding.UTF8.GetBytes(serial.Serialize&lt;T&gt;(aRecord)));
req.Position := 0;
Result := http.post(ipport + '/rest/' + resource + '/update/' + dbid, req);
req.Free;
serial.Free;
end;

class function TRest.update2&lt;T&gt;(const resource: string; const aRecord: T;
dbid: string): tbytes;
begin
var serial: TgoProtocolBuffer := TgoProtocolBuffer.Create;
var req: TBytesStream := TBytesStream.Create(serial.Serialize&lt;T&gt;(aRecord));
req.Position := 0;
Result := TEncoding.UTF8.GetBytes(http.post(ipport + '/protobuf/' + resource + '/update/' + dbid , req));
req.Free;
serial.Free;
end;

class function TRest.select&lt;T&gt;(const resource: string; dbid: string = '1'; where: string = ''): T;
begin
var req: tmemorystream := tmemorystream.Create;
var serial: TJsonSerializer := TJsonSerializer.Create;
if where = '' then
    result := serial.Deserialize&lt;T&gt;(http.post(ipport + '/rest/' + resource, req))
else
begin
    var s: string := ipport + '/rest/' + resource + '/select/' + dbid + '/' + TNetEncoding.URL.Encode(where);
    result := serial.Deserialize&lt;T&gt;(http.post(s, req));
end;
serial.Free;
req.Free;
end;

class function TRest.select2&lt;T&gt;(const resource: string; dbid: string = '1'; where: string = ''): T;
begin
var req: tmemorystream := tmemorystream.Create;
var serial: TgoProtocolBuffer := TgoProtocolBuffer.Create;
if where = '' then
    serial.Deserialize&lt;T&gt;(result, TEncoding.UTF8.GetBytes(http.post(ipport + resource, req)))
else
begin
    var s: string := ipport + '/protobuf/' + resource + '/select/' + dbid + '/' + TNetEncoding.URL.Encode(where);
    serial.Deserialize&lt;T&gt;(result, TEncoding.UTF8.GetBytes(http.post(s, req)));
end;
req.Free;
serial.Free;
end;

class function TRest.delete2(const resource: string; where, dbid: string): string;
begin
var req: TBytesStream := TBytesStream.Create;
Result := http.post(ipport + '/protobuf/' + resource + '/delete/' + dbid + '/' + TNetEncoding.URL.Encode(where), req);
req.free;
end;

class function TRest.insert&lt;T&gt;(const resource: string; const aRecord: T; dbid: string = '1'): string;
begin
var serial: TJsonSerializer := TJsonSerializer.Create;
var req: TBytesStream := TBytesStream.Create(TEncoding.UTF8.GetBytes(serial.Serialize&lt;T&gt;(aRecord)));
req.Position := 0;
Result := http.post(ipport + '/rest/' + resource + '/insert/' + dbid, req);
req.Free;
serial.Free;
end;

class function TRest.delete(const resource: string; where: string; dbid: string = '1'): string;
begin
var req: TBytesStream := TBytesStream.Create;
Result := http.post(ipport + '/rest/' + resource + '/delete/' + dbid + '/' + TNetEncoding.URL.Encode(where), req);
req.free;
end;

class function TRest.insert2&lt;T&gt;(const resource: string; const aRecord: T; dbid: string = '1'): tbytes;
begin
var serial: TgoProtocolBuffer := TgoProtocolBuffer.Create;
var req: TBytesStream := TBytesStream.Create(serial.Serialize&lt;T&gt;(aRecord));
req.Position := 0;
Result := TEncoding.UTF8.GetBytes(http.post(ipport + '/protobuf/' + resource + '/insert/' + dbid , req));
req.Free;
serial.Free;
end;

initialization
http := TIdHTTP.Create(nil);
finalization
FreeAndNil(http);

end.
</pre>
</div>
<p>  </p>

</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/16472532.html</p><br><br>
来源:https://www.cnblogs.com/hnxxcxg/p/16472532.html
頁: [1]
查看完整版本: delphi基于泛型结构的数据序列