zoukankan      html  css  js  c++  java
  • 通用的更新方法 THlsImplBase

    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 用来更新数据

    代码SourceCoreHlsImplBase.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;
    好的代码像粥一样,都是用时间熬出来的
  • 相关阅读:
    LeetCode 227. Basic Calculator II
    LeetCode 224. Basic Calculator
    LeetCode 103. Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 169. Majority Element
    LeetCode 145. Binary Tree Postorder Traversal
    LeetCode 94. Binary Tree Inorder Traversal
    LeetCode 144. Binary Tree Preorder Traversal
  • 原文地址:https://www.cnblogs.com/jijm123/p/13688552.html
Copyright © 2011-2022 走看看