通用的更新方法

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;

 

posted on 2016-02-28 21:36  Morlin  阅读(316)  评论(0编辑  收藏  举报

导航