首页 > 其他 > 详细

通用的更新方法

时间:2020-09-18 00:42:15      阅读:107      评论:0      收藏:0      [点我收藏+]
function THlsImplBase.UpdateTableData(ATableName, AKeyField: AnsiString; var AData: Variant): Boolean;
var
  LSql: string;
  I: Integer;
  
  {$IFDEF MSSQL}
  LProvider:  TProvider;
  LDataSet: TDataSet;
  {$ENDIF}

  {$IFDEF ORACLE}
  LOraQuery:      TOraQuery;
  LDataProvider:  TOraProvider;
  {$ENDIF}
  LErrCount: Integer;
begin
  Result := False;
  if ATableName = ‘‘ then Exit;
  
  if AData = null then
  begin
    Result := True;
    Exit;
  end;  
  
  {$IFDEF MSSQL}
  if AKeyField = ‘‘ then Exit;

  LSql := ‘select * from ‘ + ATableName + ‘ where 1 = 2‘;
  LDataSet := CreateDataSet(LSql);
  if not Assigned(LDataSet) then Exit;

  LProvider := TProvider.Create(nil);
  LProvider.UpdateMode := upWhereKeyOnly;
  LProvider.DataSet := LDataSet;
  AKeyField := LowerCase(AKeyField);
  for I := 0 to LDataSet.FieldCount - 1 do
  begin
    if (LowerCase(LDataSet.Fields[I].FieldName) = AKeyField) or
        (Pos(‘;‘ + LowerCase(LDataSet.Fields[I].FieldName) + ‘;‘, AKeyField) > 0) then
      LDataSet.Fields[I].ProviderFlags := LDataSet.Fields[I].ProviderFlags + [pfInKey]
    else
      LDataSet.Fields[I].ProviderFlags := [];
  end;
  try
    try
      LProvider.ApplyUpdates(AData, -1, LErrCount);
    except
    end;
    Result := LErrCount = 0;
  finally
    LDataSet.Free;
    LProvider.Free;
  end;
  {$ENDIF}

  {$IFDEF ORACLE}
  LSql := ‘select * from ‘ + ATableName + ‘ where 1=2‘;
  LSql  := StringReplace(LSql, ‘(nolock)‘, ‘‘, [rfReplaceAll, rfIgnoreCase]);
  LOraQuery           := TOraQuery.Create(nil);
  LOraQuery.Session   := FConnection;
  LOraQuery.SQL.Text  := LSql;
  LOraQuery.Open();

  LDataProvider         := TOraProvider.Create(nil);
  LDataProvider.DataSet := LOraQuery;
  LDataProvider.UpdateMode  := upWhereKeyOnly;

  AKeyField := LowerCase(AKeyField);
  for I := 0 to LOraQuery.FieldCount - 1 do
  begin
    if (LowerCase(LOraQuery.Fields[I].FieldName) = AKeyField) or
        (Pos(‘;‘ + LowerCase(LOraQuery.Fields[I].FieldName) + ‘;‘, AKeyField) > 0) then
      LOraQuery.Fields[I].ProviderFlags := LOraQuery.Fields[I].ProviderFlags + [pfInKey]
    else
      LOraQuery.Fields[I].ProviderFlags := [];
  end;

  try
    try
      LDataProvider.ApplyUpdates(AData, -1, LErrCount);
    except
    end;
    Result := LErrCount = 0;
  finally
    LDataProvider.Free;
    LOraQuery.Close;
    LOraQuery.Free;
  end;
  {$ENDIF}

end;
//Provider 用来更新数据

 

 

代码\Source\Core\HlsImplBase.pas

function THisIntfService.UpdateTable(ADatas: TUpdateTableDatas): Boolean;
var
  I:      Integer;
  LData:  Variant;
  LTableData: TUpdateTableData;
begin
  Result      := False;
  if Length(ADatas) = 0 then Exit;
  
  StartTrans();

  try
    for I := Low(ADatas) to High(ADatas) do
    begin
      LTableData  := ADatas[I];
      LData       := LTableData.DataValue;
      HisMzFuncs.UnCompressVariants(LData);
      if not Self.UpdateTableData(LTableData.TableName, LTableData.KeyFields, LData) then//里面利用了Provider 进行提交数据
      begin
        Self.RollBack;
        Exit;
      end;
    end;

  except
    RollBack();
    Exit;
  end;

  Commit();
  Result  := True;

end;

通用的更新方法

原文:https://www.cnblogs.com/jijm123/p/13688552.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!