api.fdmemtable.pas

api.fdmemtable.pas

unit api.fdmemtable;
// fdmemtable CRUD
// cxg 2024-12-6

interface

uses
  //firedac---
  FireDAC.stan.StorageBin, FireDAC.Stan.Intf,
  // mormot------
  mormot.core.variants,
  // system---------
  Classes, SysUtils,
  // my--------
  db.firedac, db.firedacpool,
  keyValue.serialize, sys.global;

type
  Tfdm = record
    // 查询
    function select(ctxt: Tcontext): cardinal;
    // 保存
    function save(ctxt: Tcontext): cardinal;
    function execsql(ctxt: Tcontext): cardinal;
  end;

implementation

function Tfdm.execsql(ctxt: Tcontext): cardinal;
var
  ser, ser2: TSerialize;
  pool: TDBPool;
  db: TDB;
begin
  ser := TSerialize.Create;
  ser2 := TSerialize.Create;
  try
    try
      ser.unMarshal(ctxt.InContent);
      pool := GetDBPool(ser.asStr['dbid']);
      db := pool.Lock;
      db.execsql(ser.asStr['sql']);
      ser2.asBool['ok'] := true;
      ctxt.OutContent := ser2.marshal3;
    except
      on E: Exception do
      begin
        ser2.asBool['ok'] := false;
        ser2.asStr['message'] := E.Message;
        ctxt.OutContent := ser2.marshal3;
        writelog('api.fdmemtable.execsql()' + E.Message);
      end;
    end;
  finally
    pool.Unlock(db);
    ser.free;
    ser2.free;
  end;
end;

function Tfdm.save(ctxt: Tcontext): cardinal;
var
  ser, ser2: TSerialize;
  pool: TDBPool;
  db: TDB;
  i, err: Integer;
  needFlds: string;
begin
  ser := TSerialize.Create;
  ser2 := TSerialize.Create;
  try
    try
      ser.unMarshal(ctxt.InContent);
      pool := GetDBPool(ser.asStr['dbid']);
      db := pool.Lock;
      db.startTrans;
      for i := 1 to ser.asByte['count'] do
      begin
        db.qry.Close;
        db.qry.SQL.Clear;
        //有的字段不要保存
        needFlds := db.getNeedFields(ser.asStr['tablename' + i.ToString], ser.asStr['nonfields' + i.ToString]);
        db.qry.SQL.Text := 'select ' + needFlds + ' from ' + ser.asStr['tablename' + i.ToString] + ' where 1=2';
        db.qry.LoadFromStream(ser.asStream['delta' + i.ToString], sfBinary);
        err := db.qry.ApplyUpdates;
        if err > 0 then
        begin
          db.rollbackTrans;
          ser2.asBool['ok'] := false;
          ctxt.OutContent := ser2.marshal3;
        end;
      end;
      db.commitTrans;
      ctxt.OutContent := ser2.marshal3;
      Result := 200;
    except
      on E: Exception do
      begin
        db.rollbackTrans;
        ser2.asBool['ok'] := false;
        ser2.asStr['message'] := E.Message;
        ctxt.OutContent := ser2.marshal3;
        writelog('api.fdmemtable.save()' + E.Message);
      end;
    end;
  finally
    pool.Unlock(db);
    ser.free;
    ser2.free;
  end;
end;

function Tfdm.select(ctxt: Tcontext): cardinal;
var
  ser, ser2: TSerialize;
  pool: TDBPool;
  db: TDB;
  i: Integer;
  ms: TMemoryStream;
begin
  ser := TSerialize.Create;
  ser2 := TSerialize.Create;
  ms := TMemoryStream.Create;
  try
    try
      ser.unMarshal(ctxt.InContent);
      pool := GetDBPool(ser.asStr['dbid']);
      db := pool.Lock;
      for i := 1 to ser.asByte['count'] do
      begin
        db.select(ser.asStr['sql' + i.ToString]);
        TMemoryStream(ms).Clear;
        db.qry.SaveToStream(ms, sfBinary);
        ser2.asStream['ds' + i.ToString] := ms;
      end;
      ser2.asBool['ok'] := True;
      ctxt.OutContent := ser2.marshal3;
      Result := 200;
    except
      on E: Exception do
      begin
        ser2.asBool['ok'] := false;
        ser2.asStr['message'] := E.Message;
        ctxt.OutContent := ser2.marshal3;
        writelog('api.fdmemtable.select()' + E.Message);
      end;
    end;
  finally
    pool.Unlock(db);
    ser.free;
    ser2.free;
    ms.Free;
  end;
end;

var
  _: Tfdm;

initialization

_router.Post('/fdm/select', _.select);
_router.Post('/fdm/save', _.save);
_router.Post('/fdm/execsql', _.execsql);

end.

 

posted @ 2024-12-07 09:37  delphi中间件  阅读(26)  评论(0)    收藏  举报