【问题标题】:Reverse the order on an index for a ClientDataSet反转 ClientDataSet 索引的顺序
【发布时间】:2017-06-27 03:39:47
【问题描述】:

我想反转 TClientDataSet 中索引的顺序,下面的代码看起来应该可以解决问题,但什么也没做。有没有很好的方法来反转索引的顺序?

procedure TForm8.Button1Click(Sender: TObject);
var
  index: TIndexDef;
begin
  index := ClientDataSet1.IndexDefs.Find('LengthIndex');
  if ixDescending in index.Options then
    index.Options := index.Options - [ixDescending]
  else
    index.Options := index.Options + [ixDescending];
end;

【问题讨论】:

  • 为什么不删除索引并重新创建呢?应该没有更快的方法来做到这一点,因为(CMIIW)如果有一种方法可以非常快速地反转索引,那么就没有理由区分升序和降序索引 - 数据库引擎可以即时排序然后(基于查询)。
  • 也许你会看看 edn.embarcadero.com/article/29056 不仅有关于 CDS 索引的长篇解释,还有一些适合你需要的代码 - 我想是的 :o)

标签: delphi indexing tclientdataset


【解决方案1】:

TIndexDef.Options 在创建索引时使用。它们不能用于尝试和影响现有索引。请参阅documentation强调我的):

创建新索引时,使用 Options 指定索引的属性。选项可以包含零个或多个 TIndexOption 常量 ixPrimary、ixUnique、ixDescending、ixCaseInsensitive 和 ixExpression。

检查现有索引的定义时,请阅读选项以确定用于创建索引的选项。

您需要使用ixDescending 值集创建一个单独的索引。然后,您只需更改 IndexName 属性即可来回切换。

【讨论】:

  • +1,正确的方法是创建两个索引并使用IndexName属性在它们之间切换。
  • (强调我的)是什么意思?
  • @CapeCodGunny:这意味着我是添加粗体来强调这些观点的人。
  • 好的,知道了。因此,您提升了文档的部分内容,并加粗了那些驱动概念的要点。这很不错。谢谢。
  • 很棒的算法建议You'll need to create a separate index with the ixDescending value set. You can then switch back and forth by just changing the IndexName property. 简单,优雅,对我非常有用!
【解决方案2】:

这是我最终确定的双向排序方法。基本上它只是创建和释放索引,不是很漂亮但可以工作。使用TFDMemTable 更容易做到这一点(如果您可以访问 FireDAC)

type
  TSortByFieldOption = (ForceAscending, ForceDescending);
  TSortByFieldOptions = set of TSortByFieldOption;

procedure SortClientDataSetByField(cds : TClientDataSet; FieldName : String; Options : TSortByFieldOptions = []);
const
  IndexName = 'GridSort';
var
  i: integer;
  index: TIndexDef;
  OldOrder: string;
  IndexOptions : TIndexOptions;
begin
  cds.DisableControls;
  try
    i := cds.IndexDefs.IndexOf(IndexName);
    if i <> - 1  then
    begin
      index := cds.IndexDefs.Find(IndexName);
      OldOrder := index.Fields;
      try
        cds.DeleteIndex(IndexName);
      except;
        OutputDebugString('no index?');
        //there seem to be conditions where the index does not exist but
      end;
      index.Free; //delete index for some reason does not free the index
      indexOptions := index.Options;
    end else
      IndexOptions := [ixDescending];

    index := cds.IndexDefs.AddIndexDef;
    index.Name := IndexName;
    index.Fields := FieldName;
    if ForceAscending in Options then
      index.Options := []
    else if ForceDescending in Options then
      index.Options := [ixDescending]
    else if OldOrder = FieldName  then
    begin
      if (IndexOptions = [ixDescending]) then
        index.Options := []
      else
        index.Options := [ixDescending];
    end;
    cds.IndexName := IndexName;
  finally
    cds.EnableControls;
  end;
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多