delphi基于泛型结构的数据序列
delphi基于泛型结构的数据序列
unit server.rest.api;
/// <author>cxg 2022-7-13</author>
interface
uses
IdHTTP, System.Classes, Grijjy.ProtocolBuffers, System.NetEncoding,
System.JSON.Serializers, System.SysUtils;
type
TRest = class
public
/// <summary>
/// 查询
/// </summary>
/// <param name="resource">资源</param>
/// <param name="dbid">数据库id</param>
/// <param name="where">查询条件,例如:unitname like '%'</param>
/// <returns>记录数组</returns>
class function select<T: record>(const resource: string; dbid: string = '1'; where: string = ''): T;
class function insert<T: record>(const resource: string; const aRecord: T; dbid: string = '1'): string;
class function update<T: record>(const resource: string; const aRecord: T; dbid: string = '1'): string;
/// <summary>
/// 删除
/// </summary>
/// <param name="resource">资源</param>
/// <param name="where">删除条件,例如:unitid='10'</param>
/// <param name="dbid">数据库id</param>
/// <returns>json</returns>
class function delete(const resource: string; where: string; dbid: string = '1'): string;
//protobuf CRUD
class function select2<T: record>(const resource: string; dbid: string = '1'; where: string = ''): T;
class function delete2(const resource: string; where: string; dbid: string = '1'): string;
class function insert2<T: record>(const resource: string; const aRecord: T; dbid: string = '1'): tbytes;
class function update2<T: record>(const resource: string; const aRecord: T; dbid: string = '1'): tbytes;
//unmarshal
class function unmarshal<T: record>(const value: string): T; overload;
class function unmarshal<T: record>(const value: tbytes): T; overload;
end;
var
ipport: string = 'http://127.0.0.1:1234';
http: TIdHTTP;
implementation
{ TRest }
class function TRest.unmarshal<T>(const value: string): T;
begin
var serial: TJsonSerializer := TJsonSerializer.Create;
Result := serial.Deserialize<T>(value);
serial.free;
end;
class function TRest.unmarshal<T>(const value: tbytes): T;
begin
var serial: TgoProtocolBuffer := TgoProtocolBuffer.Create;
serial.Deserialize<T>(result, value);
serial.Free;
end;
class function TRest.update<T>(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<T>(aRecord)));
req.Position := 0;
Result := http.post(ipport + '/rest/' + resource + '/update/' + dbid, req);
req.Free;
serial.Free;
end;
class function TRest.update2<T>(const resource: string; const aRecord: T;
dbid: string): tbytes;
begin
var serial: TgoProtocolBuffer := TgoProtocolBuffer.Create;
var req: TBytesStream := TBytesStream.Create(serial.Serialize<T>(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<T>(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<T>(http.post(ipport + '/rest/' + resource, req))
else
begin
var s: string := ipport + '/rest/' + resource + '/select/' + dbid + '/' + TNetEncoding.URL.Encode(where);
result := serial.Deserialize<T>(http.post(s, req));
end;
serial.Free;
req.Free;
end;
class function TRest.select2<T>(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<T>(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<T>(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<T>(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<T>(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<T>(const resource: string; const aRecord: T; dbid: string = '1'): tbytes;
begin
var serial: TgoProtocolBuffer := TgoProtocolBuffer.Create;
var req: TBytesStream := TBytesStream.Create(serial.Serialize<T>(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.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/16472532.html

浙公网安备 33010602011771号