Skip to content

Commit 35e43df

Browse files
committed
fix: ineffective marking index as invisible or visible, must be done in a separate ALTER TABLE query
Refs #1388
1 parent 130c26e commit 35e43df

File tree

5 files changed

+55
-21
lines changed

5 files changed

+55
-21
lines changed

source/dbconnection.pas

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,7 @@ TDBLogItem = class(TObject)
431431
frCreateTrigger,
432432
frCreateEvent,
433433
frInvisibleColumns,
434-
frCompressedColumns,
435-
frInvisibleIndexes
434+
frCompressedColumns
436435
);
437436

438437
TDBConnection = class(TComponent)
@@ -6728,8 +6727,6 @@ function TDBConnection.Has(Item: TFeatureOrRequirement): Boolean;
67286727
frCreateEvent: Result := ServerVersionInt >= 50100;
67296728
frInvisibleColumns: Result := (FParameters.IsMariaDB and (ServerVersionInt >= 100303)) or
67306729
(FParameters.IsMySQL(True) and (ServerVersionInt >= 80023));
6731-
frInvisibleIndexes: Result := (FParameters.IsMariaDB and (ServerVersionInt >= 100600)) or
6732-
(FParameters.IsMySQL(True) and (ServerVersionInt >= 80000));
67336730
frCompressedColumns: Result := (FParameters.IsMariaDB and (ServerVersionInt >= 100301));
67346731
end;
67356732
else Result := False;
@@ -11151,13 +11148,6 @@ function TTableKey.SQLCode(TableName: String=''): String;
1115111148
if not Comment.IsEmpty then
1115211149
Result := Result + ' COMMENT ' + FConnection.EscapeString(Comment);
1115311150

11154-
if FConnection.Has(frInvisibleIndexes) then begin
11155-
if FConnection.Parameters.IsMySQL(True) then
11156-
Result := Result + ' ' + IfThen(Visible, 'VISIBLE', 'INVISIBLE')
11157-
else
11158-
Result := Result + ' ' + IfThen(Visible, 'NOT IGNORED', 'IGNORED');
11159-
end;
11160-
1116111151
end
1116211152
else begin
1116311153
// SQLite syntax:

source/dbstructures.mysql.pas

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3396,6 +3396,24 @@ function TMySqlProvider.GetSql(AId: TQueryId): string;
33963396
qGrantRole: Result := 'GRANT %s TO %s%s';
33973397
qRevokeRole: Result := 'REVOKE %s FROM %s';
33983398
qSetDefaultRole: Result := 'SET DEFAULT ROLE %s FOR %s';
3399+
qIndexVisible: Result := IfThen(
3400+
FServerVersion >= 100600, // mariadb
3401+
'NOT IGNORED',
3402+
IfThen(
3403+
FServerVersion >= 80000, // mysql
3404+
'VISIBLE',
3405+
''
3406+
)
3407+
);
3408+
qIndexInvisible: Result := IfThen(
3409+
FServerVersion >= 100600, // mariadb
3410+
'IGNORED',
3411+
IfThen(
3412+
FServerVersion >= 80000, // mysql
3413+
'INVISIBLE',
3414+
''
3415+
)
3416+
);
33993417
else Result := inherited;
34003418
end;
34013419
end;

source/dbstructures.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ interface
5252
qShowFunctionStatus, qShowProcedureStatus, qShowTriggers, qShowEvents, qShowCreateTrigger,
5353
qHelpKeyword, qShowWarnings, qGetEnumTypes,
5454
qDropUser, qCreateRole, qDropRole, qReloadPrivileges, qGrantRole, qRevokeRole, qSetDefaultRole,
55-
qAutoInc);
55+
qAutoInc, qIndexVisible, qIndexInvisible);
5656
TSqlProvider = class
5757
strict protected
5858
FNetType: TNetType;

source/table_editor.dfm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,15 @@ object frmTableEditor: TfrmTableEditor
245245
DefaultNodeHeight = 19
246246
DragMode = dmAutomatic
247247
EditDelay = 0
248-
Header.AutoSizeIndex = 0
248+
Header.AutoSizeIndex = -1
249249
Header.Options = [hoAutoResize, hoColumnResize, hoDrag, hoShowSortGlyphs, hoVisible, hoDisableAnimatedResize, hoAutoResizeInclCaption]
250250
Header.PopupMenu = MainForm.popupListHeader
251251
Images = MainForm.VirtualImageListMain
252252
PopupMenu = popupProperties
253253
TabOrder = 1
254254
TreeOptions.AutoOptions = [toAutoDropExpand, toAutoScrollOnExpand, toAutoTristateTracking, toAutoChangeScale]
255255
TreeOptions.MiscOptions = [toAcceptOLEDrop, toEditable, toFullRepaintOnResize, toGridExtensions, toInitOnSave, toToggleOnDblClick, toWheelPanning, toEditOnClick]
256-
TreeOptions.PaintOptions = [toHotTrack, toShowButtons, toShowDropmark, toShowRoot, toShowTreeLines, toShowVertGridLines, toThemeAware, toUseBlendedImages, toFullVertGridLines, toUseExplorerTheme, toHideTreeLinesIfThemed]
256+
TreeOptions.PaintOptions = [toHotTrack, toShowButtons, toShowDropmark, toShowRoot, toShowTreeLines, toShowVertGridLines, toThemeAware, toUseBlendedImages, toGhostedIfUnfocused, toFullVertGridLines, toUseExplorerTheme, toHideTreeLinesIfThemed]
257257
TreeOptions.SelectionOptions = [toExtendedFocus, toRightClickSelect]
258258
OnBeforePaint = treeIndexesBeforePaint
259259
OnClick = AnyTreeClick

source/table_editor.pas

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -914,10 +914,27 @@ function TfrmTableEditor.ComposeAlterStatement: TSQLBatch;
914914
Specs.Add('ADD ' + Constraint.SQLCode);
915915
end;
916916

917-
918917
FinishSpecs;
919918

920-
// Separate queries from here on
919+
// Separate ALTER TABLE .. ALTER INDEX query for visible/invisible indexes features, which gets otherwise
920+
// ignored in MySQL and MariaDB when done by a drop + add combined query. See issue #1388
921+
if Conn.SqlProvider.Has(qIndexInvisible) then begin
922+
for i:=0 to FKeys.Count-1 do begin
923+
if FKeys[i].Modified then begin
924+
Specs.Add('ALTER INDEX ' + Conn.QuoteIdent(FKeys[i].Name) + ' ' +
925+
IfThen(
926+
FKeys[i].Visible,
927+
Conn.SqlProvider.GetSql(qIndexVisible),
928+
Conn.SqlProvider.GetSql(qIndexInvisible)
929+
)
930+
);
931+
end;
932+
end;
933+
FinishSpecs;
934+
end;
935+
936+
937+
// *** Separate queries from here on
921938

922939
// Drop indexes, also changed indexes, which will be readded below
923940
for i:=0 to FDeletedKeys.Count-1 do begin
@@ -2091,7 +2108,10 @@ procedure TfrmTableEditor.treeIndexesGetImageIndex(Sender: TBaseVirtualTree;
20912108
if not (Kind in [ikNormal, ikSelected]) then
20922109
Exit;
20932110
case VT.GetNodeLevel(Node) of
2094-
0: ImageIndex := FKeys[Node.Index].ImageIndex;
2111+
0: begin
2112+
ImageIndex := FKeys[Node.Index].ImageIndex;
2113+
Ghosted := not FKeys[Node.Index].Visible;
2114+
end;
20952115
1: begin
20962116
TblKey := FKeys[Node.Parent.Index];
20972117
if TblKey.IsExpression(Node.Index) then
@@ -2122,7 +2142,11 @@ procedure TfrmTableEditor.treeIndexesGetText(Sender: TBaseVirtualTree;
21222142
IndexColNumAlgorithm: CellText := TblKey.Algorithm;
21232143
IndexColNumComment: CellText := TblKey.Comment;
21242144
IndexColNumDirection: CellText := ''; // Column collation
2125-
IndexColNumVisibility: CellText := IfThen(TblKey.Visible, 'Visible', 'Invisible');
2145+
IndexColNumVisibility: CellText := IfThen(
2146+
TblKey.Visible,
2147+
DBObject.Connection.SqlProvider.GetSql(qIndexVisible),
2148+
DBObject.Connection.SqlProvider.GetSql(qIndexInvisible)
2149+
);
21262150
end;
21272151
end;
21282152
1: begin
@@ -2360,7 +2384,7 @@ procedure TfrmTableEditor.treeIndexesEditing(Sender: TBaseVirtualTree;
23602384
IndexColNumType: Allowed := True;
23612385
IndexColNumAlgorithm: Allowed := True;
23622386
IndexColNumComment: Allowed := True;
2363-
IndexColNumVisibility: Allowed := DBObject.Connection.Has(frInvisibleIndexes);
2387+
IndexColNumVisibility: Allowed := DBObject.Connection.SqlProvider.Has(qIndexInvisible);
23642388
end;
23652389
end
23662390
else case Column of
@@ -2406,7 +2430,9 @@ procedure TfrmTableEditor.treeIndexesCreateEditor(Sender: TBaseVirtualTree;
24062430
end else if (Level = 0) and (Column = IndexColNumVisibility) then begin
24072431
// Visibility pulldown
24082432
EnumEditor := TEnumEditorLink.Create(VT, True, nil);
2409-
EnumEditor.ValueList := Explode(',', 'Visible,Invisible');
2433+
EnumEditor.ValueList.Add('');
2434+
EnumEditor.ValueList.Add(DBObject.Connection.SqlProvider.GetSql(qIndexVisible));
2435+
EnumEditor.ValueList.Add(DBObject.Connection.SqlProvider.GetSql(qIndexInvisible));
24102436
EditLink := EnumEditor;
24112437
end else if (Level = 1) and (Column = IndexColNumName) then begin
24122438
// Column names pulldown
@@ -2449,7 +2475,7 @@ procedure TfrmTableEditor.treeIndexesNewText(Sender: TBaseVirtualTree;
24492475
end;
24502476
IndexColNumAlgorithm: TblKey.Algorithm := NewText;
24512477
IndexColNumComment: TblKey.Comment := NewText;
2452-
IndexColNumVisibility: TblKey.Visible := SameText(NewText, 'Visible');
2478+
IndexColNumVisibility: TblKey.Visible := SameText(NewText, DBObject.Connection.SqlProvider.GetSql(qIndexVisible));
24532479
end;
24542480
// Needs to be called manually for Name and IndexType properties:
24552481
TblKey.Modification(Sender);

0 commit comments

Comments
 (0)