Skip to content

Commit e582a8b

Browse files
authored
fix(datagrid): keep query result columns in SELECT order (#1565) (#1570)
Signed-off-by: Ngô Quốc Đạt <datlechin@gmail.com>
1 parent 0414c0b commit e582a8b

4 files changed

Lines changed: 39 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
- Query result columns now follow the order in the SELECT. Adding or removing a column no longer leaves new columns stuck at the end of the grid. (#1565)
1213
- JSON file import works again. It failed to load in 0.48.0.
1314
- SQL export quotes empty or malformed values in numeric columns instead of writing them unquoted, which could produce invalid INSERT statements.
15+
1416
### Added
1517

1618
- Each filter row has a checkbox to turn it on or off and an Apply button to filter by just that row. The main Apply runs every active filter, and disabled filters stay in the panel for later. (#1561)

TablePro/Views/Results/DataGridColumnPool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class DataGridColumnPool {
4646
if slot < visibleCount {
4747
let columnName = schema.columnNames[slot]
4848
let resolvedWidth = willRestoreWidths
49-
? (savedLayout?.columnWidths[columnName] ?? 100)
49+
? (savedLayout?.columnWidths[columnName] ?? widthCalculator(columnName, slot))
5050
: widthCalculator(columnName, slot)
5151
configureColumn(
5252
column,

TablePro/Views/Results/DataGridCoordinator.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ final class TableViewCoordinator: NSObject, NSTableViewDelegate, NSTableViewData
5151
}
5252

5353
func savedColumnLayout(binding: ColumnLayoutState) -> ColumnLayoutState? {
54-
if tabType == .table,
55-
let connectionId,
54+
guard tabType == .table else {
55+
guard !binding.columnWidths.isEmpty else { return nil }
56+
var layout = binding
57+
layout.columnOrder = nil
58+
return layout
59+
}
60+
61+
if let connectionId,
5662
let tableName,
5763
!tableName.isEmpty,
5864
let stored = layoutPersister.load(for: tableName, connectionId: connectionId) {

TableProTests/Views/Results/TableViewCoordinatorLayoutTests.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//
55

66
import Foundation
7-
import TableProPluginKit
87
import SwiftUI
8+
import TableProPluginKit
99
import Testing
1010

1111
@testable import TablePro
@@ -97,20 +97,41 @@ struct TableViewCoordinatorLayoutTests {
9797
#expect(coordinator.savedColumnLayout(binding: ColumnLayoutState()) == nil)
9898
}
9999

100-
@Test("Non-table tab uses the binding directly")
101-
func nonTableTabUsesBinding() {
100+
@Test("Query tab drops a stale saved column order so new columns keep their query position")
101+
func queryTabDropsStaleColumnOrder() {
102102
let coordinator = makeCoordinator(
103103
tabType: .query,
104104
connectionId: nil,
105105
tableName: nil,
106106
persister: FakeColumnLayoutPersister()
107107
)
108-
let resolved = coordinator.savedColumnLayout(binding: nonEmptyLayout())
109-
#expect(resolved?.columnWidths == ["id": 60])
108+
var binding = ColumnLayoutState()
109+
binding.columnWidths = ["id": 60, "business_model": 120]
110+
binding.columnOrder = ["id", "business_model"]
111+
112+
var expected = ColumnLayoutState()
113+
expected.columnWidths = ["id": 60, "business_model": 120]
114+
115+
#expect(coordinator.savedColumnLayout(binding: binding) == expected)
116+
}
117+
118+
@Test("Query tab keeps remembered widths when there is no saved order")
119+
func queryTabKeepsWidths() {
120+
let coordinator = makeCoordinator(
121+
tabType: .query,
122+
connectionId: nil,
123+
tableName: nil,
124+
persister: FakeColumnLayoutPersister()
125+
)
126+
127+
var expected = ColumnLayoutState()
128+
expected.columnWidths = ["id": 60]
129+
130+
#expect(coordinator.savedColumnLayout(binding: nonEmptyLayout()) == expected)
110131
}
111132

112-
@Test("Non-table tab returns nil when binding is empty")
113-
func nonTableTabEmptyReturnsNil() {
133+
@Test("Query tab returns nil when binding is empty")
134+
func queryTabEmptyReturnsNil() {
114135
let coordinator = makeCoordinator(
115136
tabType: .query,
116137
connectionId: nil,

0 commit comments

Comments
 (0)