delphi基于泛型结构的数据序列
delphi基于泛型结构的数据序列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-07-13 SOCKET缓存
2020-07-13 异步任务调度
2016-07-13 三层数据库设计注意事项
2013-07-13 关于序列化
2013-07-13 SOA技术的进化史