Skip to content

Commit 48a93d2

Browse files
committed
feat: display size of each index and paint a color bar behind it
Refs #1500
1 parent 1c46db7 commit 48a93d2

5 files changed

Lines changed: 63 additions & 1 deletion

File tree

source/dbconnection.pas

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ TTableKey = class(TPersistent)
9191
public
9292
Name, OldName: String;
9393
IndexType, OldIndexType, Algorithm, Comment: String;
94+
Size: Int64;
9495
Columns, SubParts, Collations: TStringList;
9596
Modified, Added, Visible: Boolean;
9697
constructor Create(AOwner: TDBConnection);
@@ -112,6 +113,7 @@ TTableKey = class(TPersistent)
112113
TTableKeyList = class(TObjectList<TTableKey>)
113114
public
114115
procedure Assign(Source: TTableKeyList);
116+
function MaxSize: Int64;
115117
end;
116118
TKeyCache = TDictionary<String,TTableKeyList>;
117119

@@ -6016,6 +6018,7 @@ function TMySQLConnection.GetTableKeys(Table: TDBObject): TTableKeyList;
60166018
var
60176019
KeyQuery, ColQuery: TDBQuery;
60186020
NewKey: TTableKey;
6021+
SizeQuery: String;
60196022
begin
60206023
Result := TTableKeyList.Create(True);
60216024

@@ -6094,7 +6097,20 @@ function TMySQLConnection.GetTableKeys(Table: TDBObject): TTableKeyList;
60946097
NewKey.Visible := SameText(KeyQuery.Col('Visible'), 'yes')
60956098
else if KeyQuery.ColumnExists('Ignored') then // mariadb 10.6
60966099
NewKey.Visible := SameText(KeyQuery.Col('Ignored'), 'NO');
6100+
6101+
if FSqlProvider.Has(qIndexSize) then begin
6102+
try
6103+
SizeQuery := FSqlProvider.GetSql(qIndexSize, [
6104+
EscapeString(Table.Database),
6105+
EscapeString(Table.Name),
6106+
EscapeString(NewKey.Name)
6107+
]);
6108+
NewKey.Size := StrToInt64Def(GetVar(SizeQuery), NewKey.Size);
6109+
except
6110+
end;
6111+
end;
60976112
end;
6113+
60986114
if KeyQuery.ColumnExists('Expression') and (not KeyQuery.IsNull('Expression')) then begin
60996115
// Functional key part: enclose expression within parentheses to distinguish them from columns (issue #1777)
61006116
NewKey.Columns.Add('('+KeyQuery.Col('Expression')+')');
@@ -10966,6 +10982,7 @@ constructor TTableKey.Create(AOwner: TDBConnection);
1096610982
Subparts.OnChange := Modification;
1096710983
Collations.OnChange := Modification;
1096810984
Visible := True;
10985+
Size := -1
1096910986
end;
1097010987

1097110988
destructor TTableKey.Destroy;
@@ -10989,6 +11006,7 @@ procedure TTableKey.Assign(Source: TPersistent);
1098911006
Algorithm := s.Algorithm;
1099011007
Comment := s.Comment;
1099111008
Visible := s.Visible;
11009+
Size := s.Size;
1099211010
Columns.Assign(s.Columns);
1099311011
SubParts.Assign(s.SubParts);
1099411012
Collations.Assign(s.Collations);
@@ -11138,6 +11156,15 @@ procedure TTableKeyList.Assign(Source: TTableKeyList);
1113811156
end;
1113911157
end;
1114011158

11159+
function TTableKeyList.MaxSize: Int64;
11160+
var
11161+
Item: TTableKey;
11162+
begin
11163+
Result := -1;
11164+
for Item in Self do begin
11165+
Result := Max(Result, Item.Size);
11166+
end;
11167+
end;
1114111168

1114211169

1114311170

source/dbstructures.mysql.pas

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3434,6 +3434,13 @@ function TMySqlProvider.GetSql(AId: TQueryId): string;
34343434
'CAST(%s AS CHAR)',
34353435
''
34363436
);
3437+
qIndexSize: Result := IfThen(
3438+
(IsMySQL and (FServerVersion >= 50600)) or IsMariaDB,
3439+
'SELECT stat_value * @@innodb_page_size' +
3440+
' FROM mysql.innodb_index_stats'+
3441+
' WHERE database_name=%s AND table_name=%s AND index_name=%s AND stat_name=''size''',
3442+
''
3443+
);
34373444
else Result := inherited;
34383445
end;
34393446
end;

source/dbstructures.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ interface
5050
qShowFunctionStatus, qShowProcedureStatus, qShowTriggers, qShowEvents, qShowCreateTrigger,
5151
qHelpKeyword, qShowWarnings, qGetEnumTypes,
5252
qDropUser, qCreateRole, qDropRole, qReloadPrivileges, qGrantRole, qRevokeRole, qSetDefaultRole,
53-
qAutoInc, qIndexVisible, qIndexInvisible, qGetAuthPlugins, qCastAsText);
53+
qAutoInc, qIndexVisible, qIndexInvisible, qGetAuthPlugins, qCastAsText, qIndexSize);
5454
TSqlProvider = class
5555
strict protected
5656
FNetType: TNetType;

source/table_editor.lfm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,12 @@ object frmTableEditor: TfrmTableEditor
368368
item
369369
Position = 5
370370
Text = 'Visibility'
371+
end
372+
item
373+
Alignment = taRightJustify
374+
Position = 6
375+
Text = 'Size'
376+
Width = 10
371377
end>
372378
Header.Options = [hoAutoResize, hoColumnResize, hoDrag, hoShowSortGlyphs, hoVisible, hoDisableAnimatedResize]
373379
Header.PopupMenu = MainForm.popupListHeader
@@ -378,6 +384,7 @@ object frmTableEditor: TfrmTableEditor
378384
TreeOptions.MiscOptions = [toAcceptOLEDrop, toEditable, toFullRepaintOnResize, toGridExtensions, toInitOnSave, toToggleOnDblClick, toWheelPanning, toEditOnClick]
379385
TreeOptions.PaintOptions = [toHotTrack, toShowButtons, toShowDropmark, toShowRoot, toShowTreeLines, toThemeAware, toUseBlendedImages, toGhostedIfUnfocused, toFullVertGridLines, toUseExplorerTheme, toHideTreeLinesIfThemed]
380386
TreeOptions.SelectionOptions = [toExtendedFocus, toRightClickSelect]
387+
OnBeforeCellPaint = treeIndexesBeforeCellPaint
381388
OnBeforePaint = treeIndexesBeforePaint
382389
OnClick = AnyTreeClick
383390
OnCreateEditor = treeIndexesCreateEditor

source/table_editor.pas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ TfrmTableEditor = class(TFrame)
123123
procedure listColumnsPaintText(Sender: TBaseVirtualTree; const TargetCanvas: TCanvas; Node: PVirtualNode;
124124
Column: TColumnIndex; TextType: TVSTTextType);
125125
procedure listColumnsCreateEditor(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; out EditLink: IVTEditLink);
126+
procedure treeIndexesBeforeCellPaint(Sender: TBaseVirtualTree;
127+
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
128+
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
126129
procedure treeIndexesInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
127130
procedure treeIndexesGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
128131
var CellText: String);
@@ -249,6 +252,7 @@ TfrmTableEditor = class(TFrame)
249252
const IndexColNumComment = 3;
250253
const IndexColNumDirection = 4;
251254
const IndexColNumVisibility = 5;
255+
const IndexColNumSize = 6;
252256
procedure ValidateColumnControls;
253257
procedure ValidateIndexControls;
254258
procedure MoveFocusedIndexPart(NewIdx: Cardinal);
@@ -1937,6 +1941,19 @@ procedure TfrmTableEditor.listColumnsCreateEditor(Sender: TBaseVirtualTree;
19371941
end;
19381942
end;
19391943

1944+
procedure TfrmTableEditor.treeIndexesBeforeCellPaint(Sender: TBaseVirtualTree;
1945+
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
1946+
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
1947+
var
1948+
Key: TTableKey;
1949+
begin
1950+
// Only paint bar in size column
1951+
if Column in [IndexColNumSize] then begin
1952+
Key := FKeys[Node.Index];
1953+
MainForm.PaintColorBar(Key.Size, FKeys.MaxSize, TargetCanvas, CellRect);
1954+
end;
1955+
end;
1956+
19401957

19411958
procedure TfrmTableEditor.editNumEditChange(Sender: TObject);
19421959
var
@@ -2149,6 +2166,10 @@ procedure TfrmTableEditor.treeIndexesGetText(Sender: TBaseVirtualTree;
21492166
DBObject.Connection.SqlProvider.GetSql(qIndexVisible),
21502167
DBObject.Connection.SqlProvider.GetSql(qIndexInvisible)
21512168
);
2169+
IndexColNumSize: if TblKey.Size >= 0 then
2170+
CellText := FormatByteNumber(TblKey.Size)
2171+
else
2172+
CellText := '';
21522173
end;
21532174
end;
21542175
1: begin

0 commit comments

Comments
 (0)