TJsonObject CRUD

TJsonObject CRUD

  TORM = record
    class function Insert(const AJsonO: TJsonO; const ATableName: string)
      : string; static;
    class function Delete(const AJsonO: TJsonO;
      const ATableName, AKeyFields: string): string; static;
    class function Update(const AJsonO: TJsonO;
      const ATableName, AKeyFields: string): string; static;
  end;
class function TORM.Delete(const AJsonO: TJsonO;
  const ATableName, AKeyFields: string): string;
var
  LPair: TJSONPair;
  LKey, LValue, LWhere: string;
begin
  if (AJsonO = nil) or (ATableName = '') or (AKeyFields = '') then
    Exit;
  LWhere := '';
  LKey := '';
  LValue := '';
  for LPair in AJsonO do
  begin
    LKey := LPair.JsonString.Value;
    LValue := LPair.JsonValue.Value;
    if LPair.JsonValue is TJSONString then
      LValue := QuotedStr(LValue);
    if Pos(LKey, AKeyFields) = 0 then
      Continue; // 非主键不做where条件
    LWhere := LWhere + ' and ' + LKey + '=' + LValue; // 拼where条件
  end;
  System.Delete(LWhere, 1, 5);
  Result := 'delete from ' + ATableName + ' where ' + LWhere;
end;

class function TORM.Insert(const AJsonO: TJsonO;
  const ATableName: string): string;
var
  LPair: TJSONPair;
  LKey, LValue, LFields, LValues: string;
begin
  if (AJsonO = nil) or (ATableName = '') then
    Exit;
  LKey := '';
  LValue := '';
  LFields := '';
  LValues := '';
  for LPair in AJsonO do
  begin
    LKey := LPair.JsonString.Value;
    LValue := LPair.JsonValue.Value;
    if LPair.JsonValue is TJSONString then
      LValue := QuotedStr(LValue);
    LFields := LFields + ',' + LKey;//拼字段
    LValues := LValues + ',' + LValue;//拼值
  end;
  system.Delete(LFields,1,1);
  system.Delete(LValues,1,1);
  Result := 'insert into ' + ATableName + '('+LFields+') values (' + LValues + ')';
end;

class function TORM.Update(const AJsonO: TJsonO;
  const ATableName, AKeyFields: string): string;
var
  LPair: TJSONPair;
  LKey, LValue, LSet, LWhere: string;
begin
  if (AJsonO = nil) or (ATableName = '') or (AKeyFields = '') then
    Exit;
  LWhere := '';
  LKey := '';
  LValue := '';
  LSet := '';
  for LPair in AJsonO do
  begin
    LKey := LPair.JsonString.Value;
    LValue := LPair.JsonValue.Value;
    if LPair.JsonValue is TJSONString then
      LValue := QuotedStr(LValue);
    LSet := LSet + ',' + LKey + '=' + LValue;
    if Pos(LKey, AKeyFields) = 0 then Continue; //非主键不做where条件
    LWhere := LWhere + ' and ' + LKey + '=' + LValue; // 拼where条件
  end;
  system.Delete(LSet,1,1);
  system.Delete(LWhere,1,5);
  Result := 'update ' + ATableName + ' set ' + LSet + ' where ' + LWhere;
end;

 

procedure TForm1.Button4Click(Sender: TObject);
begin
  var
    jo: TJsonO := SO('{"goodsid":"666","goodsname":"品名一"}');
  Memo1.Text := TORM.Delete(jo, 'tgoods', 'goodsid');
  //delete from tgoods where goodsid='666'
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  var
    jo: TJsonO := SO('{"goodsid":"666","goodsname":"新增"}');
  Memo1.Text := TORM.Insert(jo, 'tgoods');
  //insert into tgoods(goodsid,goodsname) values ('666','新增')
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  var
    jo: TJsonO := SO('{"goodsid":"666","goodsname":"修改"}');
  Memo1.Text := TORM.Update(jo, 'tgoods', 'goodsid');
  //update tgoods set goodsid='666',goodsname='修改' where goodsid='666'
end;

 

posted @ 2025-05-22 11:05  delphi中间件  阅读(31)  评论(0)    收藏  举报