Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

fix(redis): own query editor command context

Tobiee264c38d63e
diff --git a/macos/App/State/AppState.swift b/macos/App/State/AppState.swift
index 8a6fef9..edbd5d0 100644
--- a/macos/App/State/AppState.swift
+++ b/macos/App/State/AppState.swift
@@ -656,6 +656,76 @@ final class AppState: ObservableObject {
)
}
+ func performRedisCLIStatement(
+ _ statement: String
+ ) async -> Result<QueryResult, Error>? {
+ await performRedisCLIStatement(statement) { adapter, command in
+ try await adapter.executeRaw(sql: command)
+ }
+ }
+
+ func performRedisCLIStatement(
+ _ statement: String,
+ execute: (RedisAdapter, String) async throws -> QueryResult
+ ) async -> Result<QueryResult, Error>? {
+ let command = statement.trimmingCharacters(in: .whitespacesAndNewlines)
+ guard !command.isEmpty else { return nil }
+
+ guard let databaseName = Self.redisDatabaseNameSelected(by: command) else {
+ guard let context = currentRedisContext else { return nil }
+ return await performRedisOperation(for: context) { adapter in
+ try await execute(adapter, command)
+ }
+ }
+
+ guard let token = beginRedisDatabaseTransition() else { return nil }
+ let leaseRun = await withRedisOperationLease {
+ guard isPendingRedisDatabaseTransition(token),
+ !Task.isCancelled,
+ let adapter = activeAdapter as? RedisAdapter else {
+ discardRedisDatabaseTransition(token)
+ return Optional<Result<QueryResult, Error>>.none
+ }
+
+ do {
+ let result = try await execute(adapter, command)
+ let ownsPublication = finishRedisDatabaseTransition(
+ token,
+ outcome: .success(databaseName: databaseName)
+ )
+ guard ownsPublication, !Task.isCancelled else { return nil }
+ return Result<QueryResult, Error>.success(result)
+ } catch {
+ let ownsPublication = finishRedisDatabaseTransition(
+ token,
+ outcome: .failure
+ )
+ guard ownsPublication, !Task.isCancelled else { return nil }
+ return Result<QueryResult, Error>.failure(error)
+ }
+ }
+
+ switch leaseRun {
+ case .completed(let result):
+ return result
+ case .cancelled:
+ _ = finishRedisDatabaseTransition(token, outcome: .failure)
+ return nil
+ }
+ }
+
+ static func redisDatabaseNameSelected(by statement: String) -> String? {
+ let components = statement.split(whereSeparator: { $0.isWhitespace })
+ guard components.count == 2,
+ components[0].lowercased() == "select",
+ !components[1].isEmpty,
+ components[1].unicodeScalars.allSatisfy({
+ $0.value >= 48 && $0.value <= 57
+ }),
+ let index = Int(components[1]) else { return nil }
+ return "db\(index)"
+ }
+
@discardableResult
func loadRedisConnectionMetadata(
for token: RedisSessionRevisionToken,
diff --git a/macos/Presentation/Views/QueryEditor/QueryEditorView.swift b/macos/Presentation/Views/QueryEditor/QueryEditorView.swift
index 18f3cb1..069d996 100644
--- a/macos/Presentation/Views/QueryEditor/QueryEditorView.swift
+++ b/macos/Presentation/Views/QueryEditor/QueryEditorView.swift
@@ -281,7 +281,16 @@ struct QueryEditorView: View {
for stmt in toRun {
let start = Date()
do {
- let result = try await adapter.executeRaw(sql: stmt)
+ let result: QueryResult
+ if adapter.databaseType == .redis {
+ guard let ownedResult = await appState.performRedisCLIStatement(stmt) else {
+ isExecuting = false
+ return
+ }
+ result = try ownedResult.get()
+ } else {
+ result = try await adapter.executeRaw(sql: stmt)
+ }
let duration = Date().timeIntervalSince(start)
appState.logQuery(sql: stmt, duration: duration)
appState.recordQueryHistory(sql: stmt, duration: duration, rowCount: result.rowCount)