Skip to content

Commit

Permalink
Issue #1512: add basic support for indexes with descending column dir…
Browse files Browse the repository at this point in the history
…ection
  • Loading branch information
ansgarbecker committed Feb 2, 2024
1 parent 74f872d commit 95151d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
10 changes: 9 additions & 1 deletion source/dbconnection.pas
Expand Up @@ -83,7 +83,7 @@ TTableKey = class(TPersistent)
public
Name, OldName: String;
IndexType, OldIndexType, Algorithm, Comment: String;
Columns, SubParts: TStringList;
Columns, SubParts, Collations: TStringList;
Modified, Added: Boolean;
constructor Create(AOwner: TDBConnection);
destructor Destroy; override;
Expand Down Expand Up @@ -6032,6 +6032,7 @@ function TMySQLConnection.GetTableKeys(Table: TDBObject): TTableKeyList;
NewKey.Comment := KeyQuery.Col('Index_comment', True);
end;
NewKey.Columns.Add(KeyQuery.Col('Column_name'));
NewKey.Collations.Add(KeyQuery.Col('Collation', True));
if NewKey.IndexType = TTableKey.SPATIAL then
NewKey.SubParts.Add('') // Prevent "Incorrect prefix key"
else
Expand Down Expand Up @@ -10646,14 +10647,17 @@ constructor TTableKey.Create(AOwner: TDBConnection);
FConnection := AOwner;
Columns := TStringList.Create;
SubParts := TStringList.Create;
Collations := TStringList.Create;
Columns.OnChange := Modification;
Subparts.OnChange := Modification;
Collations.OnChange := Modification;
end;

destructor TTableKey.Destroy;
begin
FreeAndNil(Columns);
FreeAndNil(SubParts);
FreeAndNil(Collations);
inherited Destroy;
end;

Expand All @@ -10671,6 +10675,7 @@ procedure TTableKey.Assign(Source: TPersistent);
Comment := s.Comment;
Columns.Assign(s.Columns);
SubParts.Assign(s.SubParts);
Collations.Assign(s.Collations);
Modified := s.Modified;
Added := s.Added;
end else
Expand Down Expand Up @@ -10714,6 +10719,9 @@ function TTableKey.SQLCode: String;
Result := Result + FConnection.QuoteIdent(Columns[i]);
if SubParts[i] <> '' then
Result := Result + '(' + SubParts[i] + ')';
// Collation / sort order, see issue #1512
if Collations[i].ToLower = 'd' then
Result := Result + ' DESC';
Result := Result + ', ';
end;
if Columns.Count > 0 then
Expand Down
5 changes: 5 additions & 0 deletions source/table_editor.dfm
Expand Up @@ -299,6 +299,11 @@ object frmTableEditor: TfrmTableEditor
Position = 3
Text = 'Comment'
Width = 120
end
item
Position = 4
Text = 'Direction'
Width = 80
end>
end
object tlbIndexes: TToolBar
Expand Down
31 changes: 28 additions & 3 deletions source/table_editor.pas
Expand Up @@ -6,7 +6,8 @@ interface
Winapi.Windows, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
Vcl.ComCtrls, Vcl.ToolWin, VirtualTrees, VirtualTrees.Types, SynRegExpr, Winapi.ActiveX, Vcl.ExtCtrls, SynEdit,
SynMemo, Vcl.Menus, Vcl.Clipbrd, System.Math, System.UITypes, System.Generics.Collections,
grideditlinks, dbstructures, dbstructures.mysql, dbconnection, apphelpers, gnugettext, System.StrUtils, extra_controls;
grideditlinks, dbstructures, dbstructures.mysql, dbconnection, apphelpers, gnugettext, System.StrUtils, extra_controls,
VirtualTrees.BaseAncestorVCL, VirtualTrees.BaseTree, VirtualTrees.AncestorVCL;

type
TFrame = TDBObjectEditor;
Expand Down Expand Up @@ -1705,6 +1706,7 @@ procedure TfrmTableEditor.menuAddIndexColumnClick(Sender: TObject);
treeIndexes.AddChild(Node);
TblKey.Columns.Add(NewCol);
TblKey.SubParts.Add(PartLength);
TblKey.Collations.Add('A');
Modification(Sender);
treeIndexes.Invalidate;
SelectNode(treeIndexes, FKeys.Count-1, Node);
Expand Down Expand Up @@ -1733,6 +1735,7 @@ procedure TfrmTableEditor.btnRemoveIndexClick(Sender: TObject);
idx := treeIndexes.FocusedNode.Parent.Index;
FKeys[idx].Columns.Delete(treeIndexes.FocusedNode.Index);
FKeys[idx].SubParts.Delete(treeIndexes.FocusedNode.Index);
FKeys[idx].Collations.Delete(treeIndexes.FocusedNode.Index);
treeIndexes.DeleteNode(treeIndexes.FocusedNode);
end;
end;
Expand Down Expand Up @@ -1800,14 +1803,20 @@ procedure TfrmTableEditor.treeIndexesGetText(Sender: TBaseVirtualTree;
1: CellText := TblKey.IndexType;
2: CellText := TblKey.Algorithm;
3: CellText := TblKey.Comment;
4: CellText := ''; // Column collation
end;
end;
1: begin
TblKey := FKeys[Node.Parent.Index];
case Column of
0: CellText := TblKey.Columns[Node.Index];
1: CellText := TblKey.SubParts[Node.Index];
2: CellText := '';
2: CellText := ''; // Index algorithm
3: CellText := ''; // Index comment
4: begin
CellText := TblKey.Collations[Node.Index];
CellText := IfThen(CellText.ToLower = 'a', 'ASC', 'DESC');
end;
end;
end;
end;
Expand Down Expand Up @@ -2040,6 +2049,7 @@ procedure TfrmTableEditor.treeIndexesEditing(Sender: TBaseVirtualTree;
end;
end;
end;
4: Allowed := True; // Collation
end;
end;

Expand Down Expand Up @@ -2078,6 +2088,10 @@ procedure TfrmTableEditor.treeIndexesCreateEditor(Sender: TBaseVirtualTree;
end;
EnumEditor.AllowCustomText := True; // Allows adding a subpart in index parts: "TextCol(20)"
EditLink := EnumEditor;
end else if (Level = 1) and (Column = 4) then begin
EnumEditor := TEnumEditorLink.Create(VT, True, nil);
EnumEditor.ValueList := Explode(',', ',ASC,DESC');
EditLink := EnumEditor;
end else
EditLink := TInplaceEditorLink.Create(VT, True, nil);
end;
Expand Down Expand Up @@ -2122,6 +2136,12 @@ procedure TfrmTableEditor.treeIndexesNewText(Sender: TBaseVirtualTree;
TblKey.Columns[Node.Index] := NewText;
end;
1: TblKey.SubParts[Node.Index] := NewText;
4: begin
if NewText.ToLower = 'asc' then
TblKey.Collations[Node.Index] := 'A'
else
TblKey.Collations[Node.Index] := 'D';
end;
end;
TblKey.Modified := True;
end;
Expand Down Expand Up @@ -2232,6 +2252,7 @@ procedure TfrmTableEditor.treeIndexesDragDrop(Sender: TBaseVirtualTree;
if (TblKey.IndexType <> TTableKey.FULLTEXT) and (Col.DataType.Index in [dbdtTinyText, dbdtText, dbdtMediumText, dbdtLongText, dbdtTinyBlob, dbdtBlob, dbdtMediumBlob, dbdtLongBlob]) then
PartLength := '100';
TblKey.Subparts.Insert(ColPos, PartLength);
TblKey.Collations.Insert(ColPos, 'A');
IndexNode.States := IndexNode.States + [vsHasChildren, vsExpanded];
end;
Modification(Sender);
Expand Down Expand Up @@ -2269,6 +2290,7 @@ procedure TfrmTableEditor.MoveFocusedIndexPart(NewIdx: Cardinal);
end;
TblKey.Columns.Move(treeIndexes.FocusedNode.Index, NewIdx);
TblKey.SubParts.Move(treeIndexes.FocusedNode.Index, NewIdx);
TblKey.Collations.Move(treeIndexes.FocusedNode.Index, NewIdx);
Modification(treeIndexes);
SelectNode(treeIndexes, NewIdx, treeIndexes.FocusedNode.Parent);
end;
Expand Down Expand Up @@ -2486,8 +2508,10 @@ procedure TfrmTableEditor.AddIndexByColumn(Sender: TObject);
TblKey.IndexType := NewType;
TblKey.Added := True;
TblKey.Columns := NewParts;
for i:=0 to TblKey.Columns.Count do
for i:=0 to TblKey.Columns.Count do begin
TblKey.SubParts.Add('');
TblKey.Collations.Add('A');
end;
FKeys.Add(TblKey);
PageControlMain.ActivePage := tabIndexes;
treeIndexes.Repaint;
Expand All @@ -2500,6 +2524,7 @@ procedure TfrmTableEditor.AddIndexByColumn(Sender: TObject);
if TblKey.Columns.IndexOf(NewParts[i]) = -1 then begin
TblKey.Columns.Add(NewParts[i]);
TblKey.Subparts.Add('');
TblKey.Collations.Add('A');
end;
end;
SelectNode(treeIndexes, Item.MenuIndex);
Expand Down

0 comments on commit 95151d6

Please sign in to comment.