delphi模板开发
delphi模板开发
用模板开发,写代码就是填空题。
让开发变为填空题,这也是AI开发的指导思想。
1)制作模板
/// <author>cxg 2023-10-9</author> unit db.crud; interface uses System.Classes, System.SysUtils, serialize, yn.log, db.unidacpool, db.unidac, global; type TCRUD<T: record> = record dbid: string; func: string; sql: string; db: TDB; table: TTable<T>; req: TRequest; res: TResponse; type TTableModel = reference to procedure(db: TDB; tbl: TTable<T>; i: integer); procedure select(OnTableModel: TTableModel); //查询 procedure delete(OnTableModel: TTableModel); //删除 procedure insert(OnTableModel: TTableModel); //新增 procedure update(OnTableModel: TTableModel); //修改 end; implementation { TCRUD<T> } procedure TCRUD<T>.delete(OnTableModel: TTableModel); begin if req.Body = nil then Exit; var pool: TDBPool := GetDBPool(dbid); db := pool.Lock; try try table := TSerial<TTable<T>>.unjson(TStream(req.Body)); db.startTrans; for var i: Integer := 0 to High(table.rows) do begin db.qry.Close; db.qry.SQL.Clear; db.qry.sql.add(sql); OnTableModel(db, table, i); db.qry.ExecSQL; end; db.commitTrans; res.Send(success); except on E: Exception do begin db.rollbackTrans; res.Send(error(E.Message)); WriteLog(func + E.Message); end; end; finally pool.Unlock(db); end; end; procedure TCRUD<T>.insert(OnTableModel: TTableModel); begin if req.Body = nil then Exit; var pool: TDBPool := GetDBPool(dbid); db := pool.Lock; try try table := TSerial<TTable<T>>.unjson(TStream(req.Body)); db.startTrans; for var i: Integer := 0 to High(table.rows) do begin db.qry.Close; db.qry.SQL.Clear; db.qry.sql.add(sql); OnTableModel(db, table, i); db.qry.ExecSQL; end; db.commitTrans; res.Send(success); except on E: Exception do begin db.rollbackTrans; res.Send(error(E.Message)); WriteLog(func + E.Message); end; end; finally pool.Unlock(db); end; end; procedure TCRUD<T>.select(OnTableModel: TTableModel); begin var pool: TDBPool := GetDBPool(dbid); db := pool.Lock; try try var where: string; if req.Body <> nil then where := TEncoding.UTF8.GetString(TBytesStream(req.Body).Bytes); var lsql: string; if where = '' then lsql := sql else lsql := sql + ' where ' + where; db.select(lsql); SetLength(table.rows, db.qry.RecordCount); var i: Integer := 0; db.qry.First; while not db.qry.Eof do begin OnTableModel(db, table, i); db.qry.Next; Inc(i); end; res.Send(TSerial<TTable<T>>.json(table)); except on E: Exception do begin res.Send(error(E.Message)); writelog(func + E.Message); end; end; finally pool.Unlock(db); end; end; procedure TCRUD<T>.update(OnTableModel: TTableModel); begin if req.Body = nil then Exit; var pool: TDBPool := GetDBPool(dbid); db := pool.Lock; try try table := TSerial<TTable<T>>.unjson(TStream(req.Body)); db.startTrans; for var i: Integer := 0 to High(table.rows) do begin db.qry.Close; db.qry.SQL.Clear; db.qry.sql.add(sql); OnTableModel(db, table, i); db.qry.ExecSQL; end; db.commitTrans; res.Send(success); except on E: Exception do begin db.rollbackTrans; res.Send(error(E.Message)); WriteLog(func + E.Message); end; end; finally pool.Unlock(db); end; end; end.
2)模板填空
unit danwei; /// <author>cxg 2023-10-8</author> interface uses {product.model,} db.crud, yn.log, danwei.model, global, Net.CrossHttpServer, db.unidac, db.unidacpool, system.Classes, serialize, System.SysUtils; type TRESTdanwei = class(TPersistent) //单位的远程方法类 procedure select(const req: TRequest; const res: TResponse); //查询 procedure insert(const req: TRequest; const res: TResponse); //新增 procedure update(const req: TRequest; const res: TResponse); //修改 procedure delete(const req: TRequest; const res: TResponse); //删除 end; implementation procedure TRESTdanwei.select(const req: TRequest; const res: TResponse); //查询 begin var crud: TCRUD<Tdanwei>; crud.dbid := '1'; crud.func := 'TRESTdanwei.select()'; crud.sql := 'select * from tunit'; crud.req := req; crud.res := res; crud.select( procedure(db: TDB; tbl: TTable<Tdanwei>; i: Integer) begin tbl.rows[i].unitid := db.qry.FieldByName('unitid').AsString; tbl.rows[i].unitname := db.qry.FieldByName('unitname').AsString; end); end; procedure TRESTdanwei.delete(const req: TRequest; const res: TResponse); //删除 begin var crud: TCRUD<Tdanwei>; crud.dbid := '1'; crud.func := 'TRESTdanwei.delete()'; crud.sql := 'delete from tunit where unitid=:unitid'; crud.req := req; crud.res := res; crud.delete( procedure(db: TDB; tbl: TTable<Tdanwei>; i: Integer) begin db.qry.ParamByName('unitid').AsString := tbl.rows[i].unitid; end); end; procedure TRESTdanwei.insert(const req: TRequest; const res: TResponse); //新增 begin var crud: TCRUD<Tdanwei>; crud.dbid := '1'; crud.func := 'TRESTdanwei.insert()'; crud.sql := 'insert into tunit(unitid,unitname) values (:unitid,:unitname)'; crud.req := req; crud.res := res; crud.insert( procedure(db: TDB; tbl: TTable<Tdanwei>; i: Integer) begin db.qry.ParamByName('unitid').AsString := tbl.rows[i].unitid; db.qry.ParamByName('unitname').AsString := tbl.rows[i].unitname; end); end; procedure TRESTdanwei.update(const req: TRequest; const res: TResponse); //修改 begin var crud: TCRUD<Tdanwei>; crud.dbid := '1'; crud.func := 'TRESTdanwei.update()'; crud.sql := 'update tunit set unitid=:unitid,unitname=:unitname where unitid=:key'; crud.req := req; crud.res := res; crud.update( procedure(db: TDB; tbl: TTable<Tdanwei>; i: Integer) begin db.qry.ParamByName('unitid').AsString := tbl.rows[i].unitid; db.qry.ParamByName('unitname').AsString := tbl.rows[i].unitname; db.qry.ParamByName('key').AsString := tbl.rows[i].unitid; end); end; initialization RegisterClass(TRESTdanwei); finalization UnRegisterClass(TRESTdanwei); end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/17751243.html