iOS/Sources/App/Settings/DatabaseExplorer/DatabaseExplorerView.swift
Copilot 77fa34669c
Add Database Explorer to debugging menu (#4167)
## Summary

Adds a "Database Explorer" feature to the debugging settings. When
tapped, displays a list of GRDB database tables. Selecting a table shows
all entries (up to 1000 rows) with:
- Text search across all columns
- Server ID filter (when table has `serverId` column and multiple
servers configured)
- Tap-through to view all fields for any row

### Changes
- **DatabaseExplorerView.swift** - Lists all tables from `sqlite_master`
- **DatabaseTableDetailView.swift** - Table entry viewer with filtering,
uses `@StateObject` ViewModel pattern
- **DebugView.swift** - Added navigation link in the existing section
with Event Log and Location History
- Localization strings added for new UI elements

### Security
- Table names validated via parameterized query before use
- SQL identifier quoting applied
- Row limit (1000) prevents memory issues on large tables

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#

## Any other notes

Uses existing patterns from `ClientEventsLogView` and
`ServersPickerPillList`. Follows `DesignSystem.Spaces` convention.

<!-- START COPILOT CODING AGENT SUFFIX -->



<!-- START COPILOT ORIGINAL PROMPT -->



<details>

<summary>Original prompt</summary>

> Inside app settings, inside debugging, let's add a "Database explorer"
feature, which when you tap opens a list of GRDB database tables, then
on tap on it it shows all entries for that table with filter by text and
serverId.
> 
> Check GRDB+Initialization so understand the database


</details>



<!-- START COPILOT CODING AGENT TIPS -->
---

 Let Copilot coding agent [set things up for
you](https://github.com/home-assistant/iOS/issues/new?title=+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bgoncal <5808343+bgoncal@users.noreply.github.com>
2026-01-06 22:13:23 +00:00

46 lines
1.2 KiB
Swift

import GRDB
import SFSafeSymbols
import Shared
import SwiftUI
struct DatabaseExplorerView: View {
@State private var tables: [String] = []
var body: some View {
List {
ForEach(tables, id: \.self) { table in
NavigationLink {
DatabaseTableDetailView(tableName: table)
} label: {
HStack(spacing: DesignSystem.Spaces.two) {
Image(systemSymbol: .tablecells)
.foregroundStyle(Color.haPrimary)
Text(table)
}
}
}
}
.navigationTitle(L10n.Settings.DatabaseExplorer.title)
.navigationBarTitleDisplayMode(.large)
.onAppear {
loadTables()
}
}
private func loadTables() {
do {
tables = try Current.database().read { db in
try String.fetchAll(db, sql: "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
}
} catch {
Current.Log.error("Failed to load database tables: \(error)")
}
}
}
#Preview {
NavigationView {
DatabaseExplorerView()
}
}