delphi自动生成资源的REST CRUD工具

delphi自动生成资源的REST CRUD工具

该工具同样,还可以将资源生成GOOGLE PROTOBUF CRUD。

运行tableTool.exe工具,设置参数,连接数据库。

以“计量单位”资源为例,选择“tunit”数据表,点击“查询”按钮,点击“生成REST CRUD”按钮,点击“保存成文件”按钮,选择中间件源码所在目录,文件名就用自动生成的。

查看生成的rest.tunit.pas

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
unit rest.tunit;
//代码由代码工厂自动生成
//2022-07-10
{$I def.inc}
interface
 
uses
  {$IFDEF firedac}  db.firedac, db.firedacPool, {$ENDIF}
  {$IFDEF unidac}db.unidac, db.unidacpool,  {$ENDIF}
  classes, db,
  system.JSON.Serializers, yn.log, system.JSON, SysUtils, json.help;
 
type
  Ttunit = record
    [Serialize(1)] unitid: string;
    [Serialize(2)] unitname: string;
  end;
 
  TtunitArray = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
    [Serialize(4)] tunits: TArray<Ttunit>;
  end;
 
  TRes = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
  end;
 
function select(url: string; body: TBytes): string;
 
function insert(url: string; body: TBytes): string;
 
function update(url: string; body: TBytes): string;
 
function delete(url: string; body: TBytes): string;
 
implementation
 
function select(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: TArray<string>;
  serial: TJsonSerializer;
  rows: TtunitArray;
  i, h: integer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.qry.Close;
      db.qry.SQL.Clear;
      db.qry.SQL.Text := 'select * from tunit';
      db.qry.Open;
      SetLength(rows.tunits, db.qry.RecordCount);
      db.qry.First;
      i := 0;
      while not db.qry.Eof do
      begin
        rows.tunits[i].unitid := db.qry.fieldbyname('unitid').asstring;
        rows.tunits[i].unitname := db.qry.fieldbyname('unitname').asstring;
        rows.status := 1;
        rows.message := 'success';
        inc(i);
        db.qry.Next;
      end;
      result := serial.Serialize<TtunitArray>(rows);
    except
      on E: Exception do
      begin
        res.status := 0;
        res.exception := E.message;
        result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;
 
function insert(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      var rows: TtunitArray;
      rows := serial.Deserialize<TtunitArray>(TEncoding.UTF8.GetString(body));
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.startTrans;
      for var row: Ttunit in rows.tunits do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'insert into tunit (unitid,unitname) values (:unitid,:unitname)';
        db.qry.ParamByName('unitid').AsString := row.unitid;
        db.qry.ParamByName('unitname').AsString := row.unitname;
        db.qry.ExecSQL;
      end;
      db.commitTrans;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;
 
function update(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      var rows: TtunitArray;
      rows := serial.Deserialize<TtunitArray>(TEncoding.UTF8.GetString(body));
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.startTrans;
      for var row: Ttunit in rows.tunits do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        db.qry.SQL.Text := 'update tunit set unitid=:unitid,unitname=:unitname where unitid=:key0';
        db.qry.ParamByName('unitid').AsString := row.unitid;
        db.qry.ParamByName('key0').AsString := row.unitid;
        db.qry.ParamByName('unitname').AsString := row.unitname;
        db.qry.ExecSQL;
      end;
      db.commitTrans;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;
 
function delete(url: string; body: TBytes): string;
var
  db: tdb;
  pool: tdbpool;
  arr: tarray<string>;
  serial: TJsonSerializer;
  res: TRes;
begin
  serial := TJsonSerializer.Create;
  try
    try
      arr := url.Split(['/']);
      pool := GetDBPool(arr[4]);
      db := pool.Lock;
      db.qry.Close;
      db.qry.SQL.Clear;
      db.qry.SQL.Text := 'delete from tunit where unitid=:key0';
      db.qry.ParamByName('key0').AsString := arr[5];
      db.qry.ExecSQL;
      res.status := 1;
      res.message := 'success';
      Result := serial.Serialize<TRes>(res);
    except
      on E: Exception do
      begin
        res.status := 0;
        res.exception := E.Message;
        Result := serial.Serialize<TRes>(res);
      end;
    end;
  finally
    pool.Unlock(db);
    serial.Free;
  end;
end;
 
end.

  到sock.router.pas增加路由方法。

运行服务端

 

clientRest客户端uses  rest.tunit;

rest.tunit是工具自动生成的。

增加CRUD代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
procedure TForm1.Button1Click(Sender: TObject);
//新增json
begin
  var t: rest.tunit.TtunitArray;
  SetLength(t.tunits, 1);
  t.tunits[0].Unitid := '10';
  t.tunits[0].Unitname := '新增';
  var res: string := TRest.inJson<rest.tunit.TtunitArray>('/rest/unit/insert/1', t);
  var r: TRes := TRest.unmarshal<TRes>(res);
  if r.Status = 0 then
    ShowMessage('err: ' + r.Exception)
  else
    ShowMessage('ok');
end;

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
procedure TForm1.Button3Click(Sender: TObject);
//修改json
begin
  var t: rest.tunit.TtunitArray;
  SetLength(t.tunits, 1);
  t.tunits[0].Unitid := '10';
  t.tunits[0].Unitname := '修改';
  var res: string := TRest.inJson<rest.tunit.TtunitArray>('/rest/unit/update/1', t);
  var r: TRes := TRest.unmarshal<TRes>(res);
  if r.Status = 0 then
    ShowMessage('err: ' + r.Exception)
  else
    ShowMessage('ok');
end;

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
procedure TForm1.Button5Click(Sender: TObject);
//json查询
begin
  var t: rest.tunit.TtunitArray := TRest.outJson<rest.tunit.TtunitArray>('/rest/unit/select/1');
  if t.Status = 0 then
  begin
    ShowMessage(t.Exception);
    Exit;
  end;
  FDMemTable1.EmptyDataSet;
  FDMemTable1.DisableControls;
  for var dw: rest.tunit.Ttunit in t.tunits do
    FDMemTable1.AppendRecord([dw.Unitid, dw.Unitname]);
  FDMemTable1.First;
  FDMemTable1.EnableControls;
end;

  

1
2
3
4
5
6
7
8
9
10
procedure TForm1.Button7Click(Sender: TObject);
//删除json
begin
  var res: string := TRest.inUrl('/rest/unit/delete/1/10');
  var r: TRes := TRest.unmarshal<TRes>(res);
  if r.Status = 0 then
    ShowMessage('err: ' + r.Exception)
  else
    ShowMessage('ok');
end;

  

运行tableTool.exe生成REST接口的在线文档

 

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
unit server.Resources.tunit;
//代码由代码工厂自动生成
//2022-07-10
interface
 
uses
  System.SysUtils, WiRL.Core.Registry, WiRL.Core.Attributes,  WiRL.Core.MessageBody.Default,
  WiRL.http.Accept.MediaType;
 
type
  Ttunit = record
    [Serialize(1)] unitid: string;
    [Serialize(2)] unitname: string;
  end;
 
  TtunitArray = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
    [Serialize(4)] tunits: TArray<Ttunit>;
  end;
 
  TRes = record
    [Serialize(1)] status: integer;
    [Serialize(2)] exception: string;
    [Serialize(3)] message: string;
  end;
 
  [Path('tunit')]
  TtunitAPI = class
    [post, path('/select/{dbid}'), Produces(TMediaType.APPLICATION_JSON)]
    function select([PathParam('dbid')] dbid: string): TtunitArray; virtual; abstract;
    [post, path('/insert/{dbid}'), Consumes(TMediaType.APPLICATION_JSON), Produces(TMediaType.APPLICATION_JSON)]
    function insert([PathParam('dbid')] dbid: string; [BodyParam] body: TtunitArray): TRes; virtual; abstract;
    [post, path('/update/{dbid}'), Consumes(TMediaType.APPLICATION_JSON), Produces(TMediaType.APPLICATION_JSON)]
    function update([PathParam('dbid')] dbid: string; [BodyParam] body: TtunitArray): TRes; virtual; abstract;
    [post, path('/delete/{dbid}/{unitid}'), Produces(TMediaType.APPLICATION_JSON)]
    function delete([PathParam('dbid')] dbid: string; [PathParam('unitid')] unitid: string): TRes; virtual; abstract;
  end;
 
implementation
 
initialization
  TWiRLResourceRegistry.Instance.RegisterResource<TtunitAPI>;
 
end.

  openApi工程中添加上面用工具生成的单元

 

编译,运行openApi.exe

 

posted @   delphi中间件  阅读(472)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示