From d88662dd332af8a37e93abeb4d27db1e48fe3a12 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 06:58:05 +0700 Subject: [PATCH] fix(windows): show the newest kept result after a run drops earlier ones --- windows/docs/decisions/0004-app-shell.md | 2 +- windows/src/TablePro.App.Model/RunResults.cs | 7 ++++ .../TablePro.App/Views/ResultsPane.xaml.cs | 13 +++---- .../RunResultsTests.cs | 37 +++++++++++++++++++ .../TablePro.App.UITests/RunStatementTests.cs | 6 ++- 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/windows/docs/decisions/0004-app-shell.md b/windows/docs/decisions/0004-app-shell.md index ea007553f5..43b7250c11 100644 --- a/windows/docs/decisions/0004-app-shell.md +++ b/windows/docs/decisions/0004-app-shell.md @@ -113,7 +113,7 @@ On 2026-11-05 the maintainer had the app show what the server prints, PostgreSQL - Invoking a table or view opens its table tab, or brings the open one forward. The core reads the page (`DatabaseSession.ReadTablePageAsync`, over the core's `read_page`), quoted, ordered, filtered and limited as the engine writes it, on one of the session's pooled connections (amended by 0007). - A query tab is the SQL editor over the run's results, on its own editor connection opened on first use; a table tab is the results alone, read on the session's pooled connections (0007). - Run statement (Query menu, Ctrl+Shift+Enter, as on Linux): the core picks the statement the cursor is in, or the last one before it, with the session's own grammar (`DatabaseSession.statement_at`, over `ScriptPlan::statement_at`, in UTF-16 offsets). The editor selects it, so the tab shows which statement ran, and Safe Mode judges that text alone. -- Results: the run's first result set in `DataGridView` (ADR 0003), given to the grid once it has its first rows, or once it ends empty, and grown on each `RowsAppended`. The rows shown stay until then, so a sort or a refresh never shows an empty grid between the two results. A run with more than one set shows a `ComboBox` over the grid, "Result 1 (3)", "Result 2 (50)", as the Linux app labels them, with the count the engine gave or a plus when the row limit cut a set short; picking one shows its rows in arrival order and its count in the status. The pane holds every set of the run until the next run's first set replaces them, unless Settings, Queries, Results kept per run sets a limit (10 to 1,000, as macOS): past it the run keeps its newest sets, lets go of the rest in the core (`QueryRun::release_set`), and its status ends with "N earlier results were dropped.". A dropped set the grid still shows stays until another is picked or the run ends, when the first set kept takes its place. Columns are sized to their content, and a sort or a refresh keeps their widths. Copy and Select All in the Edit menu reach the grid when it had focus last. +- Results: the run's first result set in `DataGridView` (ADR 0003), given to the grid once it has its first rows, or once it ends empty, and grown on each `RowsAppended`. The rows shown stay until then, so a sort or a refresh never shows an empty grid between the two results. A run with more than one set shows a `ComboBox` over the grid, "Result 1 (3)", "Result 2 (50)", as the Linux app labels them, with the count the engine gave or a plus when the row limit cut a set short; picking one shows its rows in arrival order and its count in the status. The pane holds every set of the run until the next run's first set replaces them, unless Settings, Queries, Results kept per run sets a limit (10 to 1,000, as macOS): past it the run keeps its newest sets, lets go of the rest in the core (`QueryRun::release_set`), and its status ends with "N earlier results were dropped.". A dropped set the grid still shows stays until another is picked or the run ends, when the newest set kept takes its place, as macOS shows a run's last result. Columns are sized to their content, and a sort or a refresh keeps their widths. Copy and Select All in the Edit menu reach the grid when it had focus last. - Messages: a run the server printed something in gets a `SelectorBar`, Results | Messages, over its results. Messages is a read-only monospace `TextBox` with one line per notice, `{severity}: {message}`, as the Linux app prints the same core's notices, set once per batch of events rather than per notice. A run with no rows opens on Messages, one with rows on Results unless Messages was picked while it ran. Past the 10,000 notices the core keeps per run (`NoticeLimitReached`), the text ends with "The output was cut short." - A header click on a table tab runs the table's query again with the sort: `build_page` writes the ORDER BY, with the row key as the tiebreak, so it orders the whole table, not the rows already read. A row address the engine adds for edits (SQLite's rowid, PostgreSQL's ctid) follows the visible columns and is not shown. An engine that pages in its own order refuses the sort, and the grid keeps the shown one. - A header click on a query result does what the macOS app does: the result's statement runs again with the column's ORDER BY in place of its own and before its row limit (`DatabaseSession.SortedQuery`, the core's `with_order_by`). The ORDER BY names the column, or gives its position where a name is not unique or not a plain column (core ADR 0007). Only one statement that reads runs again. For a script, a write, SHOW or EXPLAIN, and where the engine takes no position, the core sorts the rows the result holds (`ResultSet.SortedAsync`), as macOS sorts them where they are, and the status says so. diff --git a/windows/src/TablePro.App.Model/RunResults.cs b/windows/src/TablePro.App.Model/RunResults.cs index af7cc9bc23..48e00cc26d 100644 --- a/windows/src/TablePro.App.Model/RunResults.cs +++ b/windows/src/TablePro.App.Model/RunResults.cs @@ -60,6 +60,13 @@ public RunResult Add(int setIndex, IReadOnlyList columns, out RunR return result; } + // The set shown stays after the run unless the limit dropped it, when the newest kept takes its place, as on macOS. + public RunResult ShownAtEnd(RunResult shown) + { + ArgumentNullException.ThrowIfNull(shown); + return Dropped == 0 || _results.Contains(shown) ? shown : _results[^1]; + } + // The listed set the event changed, or null. public RunResult? Apply(QueryEvent queryEvent) { diff --git a/windows/src/TablePro.App/Views/ResultsPane.xaml.cs b/windows/src/TablePro.App/Views/ResultsPane.xaml.cs index a8757523a1..0b3ef3ebbf 100644 --- a/windows/src/TablePro.App/Views/ResultsPane.xaml.cs +++ b/windows/src/TablePro.App/Views/ResultsPane.xaml.cs @@ -223,7 +223,7 @@ or QueryEvent.ByteLimitReached break; case QueryEvent.Finished finished: Adopt(); - ShowFirstKept(); + ShowAtEnd(results, heldBy); LetGoOfLeaving(); _droppedSets = results.Dropped; RowCount = adopted ? (_picked?.Result.Rows ?? 0) : 0; @@ -322,17 +322,16 @@ private void DropSet(List incoming, HeldSet gone) gone.Set.Dispose(); } - // At the run's end the first set kept takes the place of a dropped one still shown, as a run shows its first set. - private void ShowFirstKept() + private void ShowAtEnd(RunResults results, Dictionary heldBy) { - if (_leaving is not { } leaving - || !ReferenceEquals(leaving, _picked) - || _held.Find(entry => !ReferenceEquals(entry, leaving)) is not { } first) + if (_picked is not { } picked + || heldBy.GetValueOrDefault(results.ShownAtEnd(picked.Result)) is not { } shown + || ReferenceEquals(shown, picked)) { return; } - Pick(first); + Pick(shown); ShowPickedLabel(); } diff --git a/windows/tests/TablePro.App.Model.Tests/RunResultsTests.cs b/windows/tests/TablePro.App.Model.Tests/RunResultsTests.cs index 8cfe53e610..131fbc7a9c 100644 --- a/windows/tests/TablePro.App.Model.Tests/RunResultsTests.cs +++ b/windows/tests/TablePro.App.Model.Tests/RunResultsTests.cs @@ -160,6 +160,43 @@ public void ExactlyTheLimitDropsNothing() Assert.Equal(0, results.Dropped); } + [Fact] + public void PastTheLimitADroppedSetShownGivesWayToTheNewestAndAKeptOneStays() + { + var results = New(kept: 2); + var sets = Enumerable.Range(0, 4).Select(index => results.Add(index, Columns, out _)).ToList(); + + Assert.Same(sets[3], results.ShownAtEnd(sets[0])); + Assert.Same(sets[3], results.ShownAtEnd(sets[1])); + Assert.Same(sets[2], results.ShownAtEnd(sets[2])); + } + + [Fact] + public void ARunWithinTheLimitEndsOnTheSetShown() + { + var unlimited = New(); + var first = unlimited.Add(0, Columns, out _); + unlimited.Add(1, Columns, out _); + var limited = New(kept: 2); + var kept = limited.Add(0, Columns, out _); + limited.Add(1, Columns, out _); + + Assert.Same(first, unlimited.ShownAtEnd(first)); + Assert.Same(kept, limited.ShownAtEnd(kept)); + } + + // A run with no set, such as an UPDATE, asks while the grid still shows the last run's set. + [Fact] + public void ARunThatDroppedNothingGivesBackASetOfAnotherRun() + { + var earlier = New().Add(0, Columns, out _); + var limited = New(kept: 2); + limited.Add(0, Columns, out _); + + Assert.Same(earlier, New(kept: 2).ShownAtEnd(earlier)); + Assert.Same(earlier, limited.ShownAtEnd(earlier)); + } + // The sets a 10,000-set script leaves held, with the limit at 100. [Fact] public void TenThousandSetsWithAHundredKeptHoldAHundred() diff --git a/windows/tests/TablePro.App.UITests/RunStatementTests.cs b/windows/tests/TablePro.App.UITests/RunStatementTests.cs index 8ad6807026..c53739120a 100644 --- a/windows/tests/TablePro.App.UITests/RunStatementTests.cs +++ b/windows/tests/TablePro.App.UITests/RunStatementTests.cs @@ -51,7 +51,7 @@ public void EveryResultSetOfARunCanBePicked(string theme) app.Screenshot("42-result-picker"); } - // Settings keeps two results per run, so the first two go and the status line says so. + // Settings keeps two results per run, so the first two go, the status line says so and the grid shows the newest. [Theory] [MemberData(nameof(Themes))] public void ARunPastTheResultLimitKeepsTheNewestAndSaysHowManyWent(string theme) @@ -70,12 +70,14 @@ public void ARunPastTheResultLimitKeepsTheNewestAndSaysHowManyWent(string theme) VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER); - Assert.Equal("3", app.WaitForCell(app.Find("ResultsGrid"), 0, 0, value => value == "3")); + Assert.Equal("4", app.WaitForCell(app.Find("ResultsGrid"), 0, 0, value => value == "4")); var picker = app.Find("ResultPicker").AsComboBox(); picker.Expand(); var listed = picker.Items.Select(item => item.Name).ToArray(); + var selected = picker.SelectedItem?.Name; picker.Collapse(); Assert.Equal(["Result 3 (1)", "Result 4 (1)"], listed); + Assert.Equal("Result 4 (1)", selected); app.Screenshot("43-result-limit"); } }