delphi数据表自动生成rest CRUD和rest api在线文档
delphi数据表自动生成rest CRUD和rest api在线文档
1)设置数据库连接参数
2)代码工厂自动生成REST CRUD方法代码
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | unit rest.tgoods; //代码由代码工厂自动生成 //2022-07-07 {$I def.inc} interface uses {$IFDEF firedac} db.firedac, db.firedacPool, {$ENDIF} {$IFDEF unidac}db.unidac, db.unidacpool, {$ENDIF} system.JSON.Serializers, yn.log, system.JSON, SysUtils, json.help; type Ttgoods = record [Serialize(1)] goodsid: string ; [Serialize(2)] barcode: string ; [Serialize(3)] goodsname: string ; [Serialize(4)] pyjm: string ; [Serialize(5)] gg: string ; [Serialize(6)] kindid: string ; [Serialize(7)] jj: double ; [Serialize(8)] lsj: double ; [Serialize(9)] kcxx: double ; [Serialize(10)] kcsx: double ; [Serialize(11)] unitid: string ; [Serialize(12)] unitname: string ; [Serialize(13)] kindname: string ; end; TtgoodsArray = record [Serialize(1)] status: integer; [Serialize(2)] exception: string ; [Serialize(3)] message: string ; [Serialize(4)] tgoodss: TArray<Ttgoods>; 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: TtgoodsArray; 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 tgoods' ; db.qry.Open; SetLength(rows.tgoodss, db.qry.RecordCount); db.qry.First; i := 0; while not db.qry.Eof do begin rows.tgoodss[i].goodsid := db.qry.fieldbyname( 'goodsid' ).asstring; rows.tgoodss[i].barcode := db.qry.fieldbyname( 'barcode' ).asstring; rows.tgoodss[i].goodsname := db.qry.fieldbyname( 'goodsname' ).asstring; rows.tgoodss[i].pyjm := db.qry.fieldbyname( 'pyjm' ).asstring; rows.tgoodss[i].gg := db.qry.fieldbyname( 'gg' ).asstring; rows.tgoodss[i].kindid := db.qry.fieldbyname( 'kindid' ).asstring; rows.tgoodss[i].jj := db.qry.fieldbyname( 'jj' ).AsFloat; rows.tgoodss[i].lsj := db.qry.fieldbyname( 'lsj' ).AsFloat; rows.tgoodss[i].kcxx := db.qry.fieldbyname( 'kcxx' ).AsFloat; rows.tgoodss[i].kcsx := db.qry.fieldbyname( 'kcsx' ).AsFloat; rows.tgoodss[i].unitid := db.qry.fieldbyname( 'unitid' ).asstring; rows.tgoodss[i].unitname := db.qry.fieldbyname( 'unitname' ).asstring; rows.tgoodss[i].kindname := db.qry.fieldbyname( 'kindname' ).asstring; rows.status := 1; rows.message := 'success' ; inc(i); db.qry.Next; end; result := serial.Serialize<TtgoodsArray>(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; req: TJSONObject; begin serial := TJsonSerializer.Create; req := TJSONObject.Create; try try req.Parse(body, 0, True); arr := url.Split([ '/' ]); pool := GetDBPool(arr[4]); db := pool.Lock; db.qry.Close; db.qry.SQL.Clear; db.qry.SQL.Text := 'insert into tgoods (goodsid,barcode,goodsname,pyjm,gg,kindid,jj,lsj,kcxx,kcsx,unitid,unitname,kindname) values (:goodsid,:barcode,:goodsname,:pyjm,:gg,:kindid,:jj,:lsj,:kcxx,:kcsx,:unitid,:unitname,:kindname)' ; db.qry.ParamByName( 'goodsid' ).AsString := req.S[ 'goodsid' ]; db.qry.ParamByName( 'barcode' ).AsString := req.S[ 'barcode' ]; db.qry.ParamByName( 'goodsname' ).AsString := req.S[ 'goodsname' ]; db.qry.ParamByName( 'pyjm' ).AsString := req.S[ 'pyjm' ]; db.qry.ParamByName( 'gg' ).AsString := req.S[ 'gg' ]; db.qry.ParamByName( 'kindid' ).AsString := req.S[ 'kindid' ]; db.qry.ParamByName( 'jj' ).AsFloat := req.F[ 'jj' ]; db.qry.ParamByName( 'lsj' ).AsFloat := req.F[ 'lsj' ]; db.qry.ParamByName( 'kcxx' ).AsFloat := req.F[ 'kcxx' ]; db.qry.ParamByName( 'kcsx' ).AsFloat := req.F[ 'kcsx' ]; db.qry.ParamByName( 'unitid' ).AsString := req.S[ 'unitid' ]; db.qry.ParamByName( 'unitname' ).AsString := req.S[ 'unitname' ]; db.qry.ParamByName( 'kindname' ).AsString := req.S[ 'kindname' ]; 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; req.free; end; end; function update(url: string ; body: TBytes): string ; var db: tdb; pool: tdbpool; arr: tarray< string >; serial: TJsonSerializer; res: TRes; req: TJSONObject; begin serial := TJsonSerializer.Create; req := TJSONObject.Create; try try req.Parse(body, 0, True); arr := url.Split([ '/' ]); pool := GetDBPool(arr[4]); db := pool.Lock; db.qry.Close; db.qry.SQL.Clear; db.qry.SQL.Text := 'update tgoods set goodsid=:goodsid,barcode=:barcode,goodsname=:goodsname,pyjm=:pyjm,gg=:gg,kindid=:kindid,jj=:jj,lsj=:lsj,kcxx=:kcxx,kcsx=:kcsx,unitid=:unitid,unitname=:unitname,kindname=:kindname where goodsid=:key0' ; db.qry.ParamByName( 'goodsid' ).AsString := req.S[ 'goodsid' ]; db.qry.ParamByName( 'barcode' ).AsString := req.S[ 'barcode' ]; db.qry.ParamByName( 'goodsname' ).AsString := req.S[ 'goodsname' ]; db.qry.ParamByName( 'pyjm' ).AsString := req.S[ 'pyjm' ]; db.qry.ParamByName( 'gg' ).AsString := req.S[ 'gg' ]; db.qry.ParamByName( 'kindid' ).AsString := req.S[ 'kindid' ]; db.qry.ParamByName( 'jj' ).AsFloat := req.F[ 'jj' ]; db.qry.ParamByName( 'lsj' ).AsFloat := req.F[ 'lsj' ]; db.qry.ParamByName( 'kcxx' ).AsFloat := req.F[ 'kcxx' ]; db.qry.ParamByName( 'kcsx' ).AsFloat := req.F[ 'kcsx' ]; db.qry.ParamByName( 'unitid' ).AsString := req.S[ 'unitid' ]; db.qry.ParamByName( 'unitname' ).AsString := req.S[ 'unitname' ]; db.qry.ParamByName( 'kindname' ).AsString := req.S[ 'kindname' ]; 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; req.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; req := TJSONObject.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 tgoods where goodsid=: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. |
3)代码工厂自动生成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 47 48 49 50 51 52 53 54 55 56 57 | unit server.Resources.tgoods; //代码由代码工厂自动生成 //2022-07-07 interface uses System.SysUtils, WiRL.Core.Registry, WiRL.Core.Attributes, WiRL.Core.MessageBody.Default, WiRL.http.Accept.MediaType; type Ttgoods = record [Serialize(1)] goodsid: string ; [Serialize(2)] barcode: string ; [Serialize(3)] goodsname: string ; [Serialize(4)] pyjm: string ; [Serialize(5)] gg: string ; [Serialize(6)] kindid: string ; [Serialize(7)] jj: double ; [Serialize(8)] lsj: double ; [Serialize(9)] kcxx: double ; [Serialize(10)] kcsx: double ; [Serialize(11)] unitid: string ; [Serialize(12)] unitname: string ; [Serialize(13)] kindname: string ; end; TtgoodsArray = record [Serialize(1)] status: integer; [Serialize(2)] exception: string ; [Serialize(3)] message: string ; [Serialize(4)] tgoodss: TArray<Ttgoods>; end; TRes = record [Serialize(1)] status: integer; [Serialize(2)] exception: string ; [Serialize(3)] message: string ; end; [Path( 'tgoods' )] TtgoodsAPI = class [post, path( '/select/{dbid}' ), Produces(TMediaType.APPLICATION_JSON)] function select ([PathParam( 'dbid' )] dbid: string ): TtgoodsArray; virtual ; abstract ; [post, path( '/insert/{dbid}' ), Consumes(TMediaType.APPLICATION_JSON), Produces(TMediaType.APPLICATION_JSON)] function insert([PathParam( 'dbid' )] dbid: string ; [BodyParam] body: Ttgoods): TRes; virtual ; abstract ; [post, path( '/update/{dbid}/{goodsid}' ), Consumes(TMediaType.APPLICATION_JSON), Produces(TMediaType.APPLICATION_JSON)] function update([PathParam( 'dbid' )] dbid: string ; [PathParam( 'goodsid' )] goodsid: string ; [BodyParam] body: Ttgoods): TRes; virtual ; abstract ; [post, path( '/delete/{dbid}/{goodsid}' ), Produces(TMediaType.APPLICATION_JSON)] function delete([PathParam( 'dbid' )] dbid: string ; [PathParam( 'goodsid' )] goodsid: string ): TRes; virtual ; abstract ; end; implementation initialization TWiRLResourceRegistry.Instance.RegisterResource<TtgoodsAPI>; end. |
查看在线接口文档
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/16455500.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2019-07-07 位运算