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;
    好的代码像粥一样,都是用时间熬出来的
  • 相关阅读:
    <form:select>的使用
    存储过程-删除、新建索引
    java 反射常用总结
    java判断是否是数字
    jquery遍历数组添加行删除行
    oracle常用sql
    cxf (zhuan)
    linux 常用命令--个人小结一
    java发送邮件
    socket和webservice特点
  • 原文地址:https://www.cnblogs.com/jijm123/p/13688552.html
Copyright © 2011-2022 走看看