@@ -242,6 +242,13 @@ TfrmTableEditor = class(TFrame)
242242 const ColNumInvisible = 13 ;
243243 const ColNumCompressed = 14 ;
244244 const ColNumsCheckboxes = [ColNumUnsigned, ColNumAllownull, ColNumZerofill, ColNumInvisible, ColNumCompressed];
245+ // Columns in index tree
246+ const IndexColNumName = 0 ;
247+ const IndexColNumType = 1 ;
248+ const IndexColNumAlgorithm = 2 ;
249+ const IndexColNumComment = 3 ;
250+ const IndexColNumDirection = 4 ;
251+ const IndexColNumVisibility = 5 ;
245252 procedure ValidateColumnControls ;
246253 procedure ValidateIndexControls ;
247254 procedure MoveFocusedIndexPart (NewIdx: Cardinal);
@@ -911,10 +918,27 @@ function TfrmTableEditor.ComposeAlterStatement: TSQLBatch;
911918 Specs.Add(' ADD ' + Constraint.SQLCode);
912919 end ;
913920
914-
915921 FinishSpecs;
916922
917- // Separate queries from here on
923+ // Separate ALTER TABLE .. ALTER INDEX query for visible/invisible indexes features, which gets otherwise
924+ // ignored in MySQL and MariaDB when done by a drop + add combined query. See issue #1388
925+ if Conn.SqlProvider.Has(qIndexInvisible) then begin
926+ for i:=0 to FKeys.Count-1 do begin
927+ if FKeys[i].Modified then begin
928+ Specs.Add(' ALTER INDEX ' + Conn.QuoteIdent(FKeys[i].Name ) + ' ' +
929+ IfThen(
930+ FKeys[i].Visible,
931+ Conn.SqlProvider.GetSql(qIndexVisible),
932+ Conn.SqlProvider.GetSql(qIndexInvisible)
933+ )
934+ );
935+ end ;
936+ end ;
937+ FinishSpecs;
938+ end ;
939+
940+
941+ // *** Separate queries from here on
918942
919943 // Drop indexes, also changed indexes, which will be readded below
920944 for i:=0 to FDeletedKeys.Count-1 do begin
@@ -2081,10 +2105,15 @@ procedure TfrmTableEditor.treeIndexesGetImageIndex(Sender: TBaseVirtualTree;
20812105begin
20822106 // Icon image showing type of index
20832107 VT := Sender as TLazVirtualStringTree;
2084- if Column <> 0 then Exit;
2085- if not (Kind in [ikNormal, ikSelected]) then Exit;
2108+ if Column <> IndexColNumName then
2109+ Exit;
2110+ if not (Kind in [ikNormal, ikSelected]) then
2111+ Exit;
20862112 case VT.GetNodeLevel(Node) of
2087- 0 : ImageIndex := FKeys[Node.Index].ImageIndex;
2113+ 0 : begin
2114+ ImageIndex := FKeys[Node.Index].ImageIndex;
2115+ Ghosted := not FKeys[Node.Index].Visible;
2116+ end ;
20882117 1 : begin
20892118 TblKey := FKeys[Node.Parent.Index];
20902119 if TblKey.IsExpression(Node.Index) then
@@ -2107,27 +2136,33 @@ procedure TfrmTableEditor.treeIndexesGetText(Sender: TBaseVirtualTree;
21072136 0 : begin
21082137 TblKey := FKeys[Node.Index];
21092138 case Column of
2110- 0 : if TblKey.IsPrimary then
2139+ IndexColNumName : if TblKey.IsPrimary then
21112140 CellText := TblKey.IndexType + ' KEY' // Fixed name "PRIMARY KEY", cannot be changed
21122141 else
21132142 CellText := TblKey.Name ;
2114- 1 : CellText := TblKey.IndexType;
2115- 2 : CellText := TblKey.Algorithm;
2116- 3 : CellText := TblKey.Comment;
2117- 4 : CellText := ' ' ; // Column collation
2143+ IndexColNumType: CellText := TblKey.IndexType;
2144+ IndexColNumAlgorithm: CellText := TblKey.Algorithm;
2145+ IndexColNumComment: CellText := TblKey.Comment;
2146+ IndexColNumDirection: CellText := ' ' ; // Column collation
2147+ IndexColNumVisibility: CellText := IfThen(
2148+ TblKey.Visible,
2149+ DBObject.Connection.SqlProvider.GetSql(qIndexVisible),
2150+ DBObject.Connection.SqlProvider.GetSql(qIndexInvisible)
2151+ );
21182152 end ;
21192153 end ;
21202154 1 : begin
21212155 TblKey := FKeys[Node.Parent.Index];
21222156 case Column of
2123- 0 : CellText := TblKey.Columns[Node.Index];
2124- 1 : CellText := TblKey.SubParts[Node.Index];
2125- 2 : CellText := ' ' ; // Index algorithm
2126- 3 : CellText := ' ' ; // Index comment
2127- 4 : begin
2157+ IndexColNumName : CellText := TblKey.Columns[Node.Index];
2158+ IndexColNumType : CellText := TblKey.SubParts[Node.Index];
2159+ IndexColNumAlgorithm : CellText := ' ' ; // Index algorithm
2160+ IndexColNumComment : CellText := ' ' ; // Index comment
2161+ IndexColNumDirection : begin
21282162 CellText := TblKey.Collations[Node.Index];
21292163 CellText := IfThen(CellText.ToLower = ' a' , ' ASC' , ' DESC' );
21302164 end ;
2165+ IndexColNumVisibility: CellText := ' ' ;
21312166 end ;
21322167 end ;
21332168 end ;
@@ -2346,13 +2381,17 @@ procedure TfrmTableEditor.treeIndexesEditing(Sender: TBaseVirtualTree;
23462381 Allowed := False;
23472382 if VT.GetNodeLevel(Node) = 0 then begin
23482383 // Disallow renaming primary key, and direction/collation of key node level
2349- if (Column = 0 ) and (VT.Text[Node, 1 ] <> TTableKey.PRIMARY) then
2350- Allowed := True
2351- else
2352- Allowed := Column in [1 ,2 ,3 ];
2353- end else case Column of
2354- 0 : Allowed := True;
2355- 1 : begin
2384+ case Column of
2385+ IndexColNumName: Allowed := (VT.Text[Node, 1 ] <> TTableKey.PRIMARY);
2386+ IndexColNumType: Allowed := True;
2387+ IndexColNumAlgorithm: Allowed := True;
2388+ IndexColNumComment: Allowed := True;
2389+ IndexColNumVisibility: Allowed := DBObject.Connection.SqlProvider.Has(qIndexInvisible);
2390+ end ;
2391+ end
2392+ else case Column of
2393+ IndexColNumName: Allowed := True;
2394+ IndexColNumType: begin
23562395 // Column length is allowed for (var)char/text types only, even mandantory for text and blobs
23572396 IndexedColName := VT.Text[Node, 0 ];
23582397 for i:=0 to FColumns.Count-1 do begin
@@ -2362,7 +2401,7 @@ procedure TfrmTableEditor.treeIndexesEditing(Sender: TBaseVirtualTree;
23622401 end ;
23632402 end ;
23642403 end ;
2365- 4 : Allowed := True; // Collation
2404+ IndexColNumDirection : Allowed := True; // Collation
23662405 end ;
23672406end ;
23682407
@@ -2379,18 +2418,25 @@ procedure TfrmTableEditor.treeIndexesCreateEditor(Sender: TBaseVirtualTree;
23792418 // Start cell editor
23802419 VT := Sender as TLazVirtualStringTree;
23812420 Level := (Sender as TLazVirtualStringTree).GetNodeLevel(Node);
2382- if (Level = 0 ) and (Column = 1 ) then begin
2421+ if (Level = 0 ) and (Column = IndexColNumType ) then begin
23832422 // Index type pulldown
23842423 EnumEditor := TEnumEditorLink.Create(VT, True, nil );
23852424 EnumEditor.ValueList := TStringList.Create;
23862425 EnumEditor.ValueList.CommaText := TTableKey.PRIMARY +' ,' + TTableKey.KEY +' ,' + TTableKey.UNIQUE +' ,' + TTableKey.FULLTEXT +' ,' + TTableKey.SPATIAL;
23872426 EditLink := EnumEditor;
2388- end else if (Level = 0 ) and (Column = 2 ) then begin
2427+ end else if (Level = 0 ) and (Column = IndexColNumAlgorithm ) then begin
23892428 // Algorithm pulldown
23902429 EnumEditor := TEnumEditorLink.Create(VT, True, nil );
23912430 EnumEditor.ValueList := Explode(' ,' , ' ,BTREE,HASH,RTREE' );
23922431 EditLink := EnumEditor;
2393- end else if (Level = 1 ) and (Column = 0 ) then begin
2432+ end else if (Level = 0 ) and (Column = IndexColNumVisibility) then begin
2433+ // Visibility pulldown
2434+ EnumEditor := TEnumEditorLink.Create(VT, True, nil );
2435+ EnumEditor.ValueList.Add(' ' );
2436+ EnumEditor.ValueList.Add(DBObject.Connection.SqlProvider.GetSql(qIndexVisible));
2437+ EnumEditor.ValueList.Add(DBObject.Connection.SqlProvider.GetSql(qIndexInvisible));
2438+ EditLink := EnumEditor;
2439+ end else if (Level = 1 ) and (Column = IndexColNumName) then begin
23942440 // Column names pulldown
23952441 EnumEditor := TEnumEditorLink.Create(VT, True, nil );
23962442 ColNode := listColumns.GetFirst;
@@ -2401,7 +2447,7 @@ procedure TfrmTableEditor.treeIndexesCreateEditor(Sender: TBaseVirtualTree;
24012447 end ;
24022448 EnumEditor.AllowCustomText := True; // Allows adding a subpart in index parts: "TextCol(20)"
24032449 EditLink := EnumEditor;
2404- end else if (Level = 1 ) and (Column = 4 ) then begin
2450+ end else if (Level = 1 ) and (Column = IndexColNumDirection ) then begin
24052451 EnumEditor := TEnumEditorLink.Create(VT, True, nil );
24062452 EnumEditor.ValueList := Explode(' ,' , ' ,ASC,DESC' );
24072453 EditLink := EnumEditor;
@@ -2423,22 +2469,23 @@ procedure TfrmTableEditor.treeIndexesNewText(Sender: TBaseVirtualTree;
24232469 0 : begin
24242470 TblKey := FKeys[Node.Index];
24252471 case Column of
2426- 0 : TblKey.Name := NewText;
2427- 1 : begin
2472+ IndexColNumName : TblKey.Name := NewText;
2473+ IndexColNumType : begin
24282474 TblKey.IndexType := NewText;
24292475 if NewText = TTableKey.PRIMARY then
24302476 TblKey.Name := TTableKey.PRIMARY;
24312477 end ;
2432- 2 : TblKey.Algorithm := NewText;
2433- 3 : TblKey.Comment := NewText;
2478+ IndexColNumAlgorithm: TblKey.Algorithm := NewText;
2479+ IndexColNumComment: TblKey.Comment := NewText;
2480+ IndexColNumVisibility: TblKey.Visible := SameText(NewText, DBObject.Connection.SqlProvider.GetSql(qIndexVisible));
24342481 end ;
24352482 // Needs to be called manually for Name and IndexType properties:
24362483 TblKey.Modification(Sender);
24372484 end ;
24382485 1 : begin
24392486 TblKey := FKeys[Node.Parent.Index];
24402487 case Column of
2441- 0 : begin
2488+ IndexColNumName : begin
24422489 // Detect input of "col(123)" and move "123" into subpart
24432490 rx := TRegExpr.Create;
24442491 rx.Expression := ' .+\((\d+)\)' ;
@@ -2448,8 +2495,8 @@ procedure TfrmTableEditor.treeIndexesNewText(Sender: TBaseVirtualTree;
24482495 end else
24492496 TblKey.Columns[Node.Index] := NewText;
24502497 end ;
2451- 1 : TblKey.SubParts[Node.Index] := NewText;
2452- 4 : begin
2498+ IndexColNumType : TblKey.SubParts[Node.Index] := NewText;
2499+ IndexColNumDirection : begin
24532500 if NewText.ToLower = ' asc' then
24542501 TblKey.Collations[Node.Index] := ' A'
24552502 else
0 commit comments