delphi orm

delphi orm

以计量单位为例。

1)定义单位data-model,实际项目,为了加快开发速度,会使用工具从数据表自动生成data-model。

 生成的计量单位data-model

unit danwei.model;
/// <author>cxg 2023-8-30</author>
interface

type      //定义 数据模型(data-model)
  Tdanwei = record
    unitid: string;
    unitname: string;
  end;

implementation

end.

2)以服务端查询为例

procedure TRESTdanwei.select(const req: TRequest; const res: TResponse);
begin
  var pool: TDBPool := GetDBPool('1');
  var db: TDB := pool.Lock;
  try
    try
      var where: string;
      if req.Body <> nil then
        where := TEncoding.UTF8.GetString(TBytesStream(req.Body).Bytes);
      var sql: string;
      if where = '' then
        sql := 'select * from tunit'
      else
        sql := 'select * from tunit where ' + where;
      db.select(sql);
      var data: TData<Tdanwei>; //泛型data-model
      SetLength(data.rows, db.qry.RecordCount);
      var i: Integer := 0;
      db.qry.First;
      while not db.qry.Eof do //将dataset数据填充进泛型data-model
      begin
        data.rows[i].unitid := db.qry.FieldByName('unitid').AsString;
        data.rows[i].unitname := db.qry.FieldByName('unitname').AsString;
        db.qry.Next;
        Inc(i);
      end;
      res.Send(TSerial<TData<Tdanwei>>.json(data)); //序列data-model为json
    except
      on E: Exception do
      begin
        res.Send(error(E.Message));
        writelog('TRESTdanwei.select()' + E.Message);
      end;
    end;
  finally
    pool.Unlock(db);
  end;
end;

3)以delphi客户端查询为例

var data: TData<Tdanwei>;  //泛型data-model
var crud: TRest<TData<Tdanwei>>; //泛型rest CRUD接口

procedure TForm1.Button5Click(Sender: TObject);
//查询
begin
  crud.resource := 'danwei';  //资源=计量单位
  crud.where := 'unitid like ''1%''';
  data := crud.select;
  FDMemTable1.DisableControls;
  FDMemTable1.EmptyDataSet;
  for var row: Tdanwei in data.rows do //将泛型data-model数据填充进内存表dataset
    FDMemTable1.AppendRecord([row.Unitid, row.Unitname]);
  FDMemTable1.First;
  FDMemTable1.EnableControls;
end;

4)跨平台、语言的客户端以apipost工具查询为例

 

  

 

posted @ 2023-09-10 08:09  delphi中间件  阅读(168)  评论(0编辑  收藏  举报