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;
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/18890764

浙公网安备 33010602011771号