Tclientdataset实现反向排序

ADO的sort可以很方便实现正向和方向排序,clt的排序可以用indexfieldnames,和indexDef.但是只支持正向排序.测试很长时间都没有办法利用这两个属性来实现反向排序,查看了clt的代码后,通过修改实现:

procedure TClientDataSet.SortOnFields(Cursor: IDSCursor; const Fields: string;
  CaseInsensitive, Descending: Boolean);
var
  I: Integer;
  FieldList: TList;
  DescFlags, CaseFlags: DSKEYBOOL;
  //shappy
  sFlds:string;

  function GetFlags(Flag: Bool; var FlagArray: DSKEYBOOL): Pointer;
  var
    J: Integer;
    //shappy
    s,s2:string;
    i:Integer;
  begin
{    if not Flag then Result := nil else
    begin
      for J := 0 to FieldList.Count - 1 do
        FlagArray[J] := True;
      Result := @FlagArray;
    end;}
    //shappy 修改成类似ado.sort的效果,可能存在bug,效率无优化
    if not Flag then begin
      s:= sFlds;
      i:=pos(';',s);  j:=0;
      while i>0 do begin
        if Copy(s,i-5,5)=' DESC' then
          FlagArray[j]:=True
        else
          FlagArray[j]:=False;
        inc(j);
        s:=copy(s,i+1,MaxInt);
        i:=pos(';',s);
      end;
      if (s<>'') and (Copy(s,Length(s)-4,5)=' DESC') then
        FlagArray[j]:=True
      else FlagArray[j]:=False;
    end else begin
      for J := 0 to FieldList.Count - 1 do
        FlagArray[J] := True;
      Result := @FlagArray;
    end;
  end;

begin
  FieldList := TList.Create;
  try
    //shappy
    sFlds:=UpperCase(Fields);
    GetFlags(Descending, DescFlags);
    i:=pos(' DESC',sFlds);
    while i>0 do begin
      sFlds:=Copy(sFlds,1,i-1)+copy(sFlds,i+5,MaxInt);
      i:=pos(' DESC',sFlds);
    end;
    GetFieldList(FieldList, sFlds);

    //GetFieldList(FieldList, Fields);
    for I := 0 to FieldList.Count - 1 do
      if TField(FieldList[I]).FieldNo > 0 then
        FieldList[I] := Pointer(TField(FieldList[I]).FieldNo) else
        DatabaseError(SFieldIndexError, Self);
      //shappy
      Check(Cursor.SortOnFields(FieldList.Count, PDWord(FieldList.List),
        @DescFlags, GetFlags(CaseInsensitive, CaseFlags)));
//    Check(Cursor.SortOnFields(FieldList.Count, PDWord(FieldList.List),
//      GetFlags(Descending, DescFlags), GetFlags(CaseInsensitive, CaseFlags)));
    GetIndexInfo('');
  finally
    FieldList.Free;
  end;
end;

 

posted on 2007-05-14 09:39  shappy  阅读(811)  评论(0编辑  收藏  举报

导航