delphi数据表自动生成rest CRUD和rest api在线文档

delphi数据表自动生成rest CRUD和rest api在线文档

1)设置数据库连接参数

2)代码工厂自动生成REST CRUD方法代码

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在线接口文档

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.

  查看在线接口文档

 

posted @ 2022-07-07 17:27  delphi中间件  阅读(306)  评论(0编辑  收藏  举报