git.iko.soy/parseable-macos
on master help sign in
Auto-run query when selecting a dataset (#9)
8e59dd6iko
ParseableViewer/ViewModels/QueryViewModel.swift+5−1
@@ -284,7 +284,10 @@ final class QueryViewModel {
284 resultsTruncated = false
284 resultsTruncated = false
285 }
285 }
286
286
287+ /// Sets the default query for the given stream, returning `true` if the
288+ /// query text was replaced (i.e. the user hadn't customized it).
289+ @discardableResult
287− func setDefaultQuery(stream: String, previousStream: String? = nil) {
290+ func setDefaultQuery(stream: String, previousStream: String? = nil) -> Bool {
288 // Replace the query if it's empty or still matches the auto-generated
291 // Replace the query if it's empty or still matches the auto-generated
289 // default for the previous stream. If the user edited the SQL, keep it.
292 // default for the previous stream. If the user edited the SQL, keep it.
290 let shouldReplace: Bool
293 let shouldReplace: Bool
@@ -300,6 +303,7 @@ final class QueryViewModel {
300 if shouldReplace {
303 if shouldReplace {
301 sqlQuery = "SELECT * FROM \(Self.escapeSQLIdentifier(stream)) ORDER BY p_timestamp DESC LIMIT \(queryLimit)"
304 sqlQuery = "SELECT * FROM \(Self.escapeSQLIdentifier(stream)) ORDER BY p_timestamp DESC LIMIT \(queryLimit)"
302 }
305 }
306+ return shouldReplace
303 }
307 }
304
308
305 /// Escapes a SQL identifier by doubling internal double-quotes, then wrapping in double-quotes.
309 /// Escapes a SQL identifier by doubling internal double-quotes, then wrapping in double-quotes.
ParseableViewer/Views/QueryView.swift+18−2
@@ -197,12 +197,28 @@ struct QueryView: View {
197 .onChange(of: appState.selectedStream) { oldValue, newValue in
197 .onChange(of: appState.selectedStream) { oldValue, newValue in
198 viewModel.clearResults()
198 viewModel.clearResults()
199 if let stream = newValue {
199 if let stream = newValue {
200− viewModel.setDefaultQuery(stream: stream, previousStream: oldValue)
200+ let didSetDefault = viewModel.setDefaultQuery(stream: stream, previousStream: oldValue)
201+ if didSetDefault {
202+ Task {
203+ await viewModel.executeQuery(
204+ client: appState.client,
205+ stream: stream
206+ )
207+ }
208+ }
201 }
209 }
202 }
210 }
203 .onAppear {
211 .onAppear {
204 if let stream = appState.selectedStream {
212 if let stream = appState.selectedStream {
205− viewModel.setDefaultQuery(stream: stream)
213+ let didSetDefault = viewModel.setDefaultQuery(stream: stream)
214+ if didSetDefault && viewModel.results.isEmpty {
215+ Task {
216+ await viewModel.executeQuery(
217+ client: appState.client,
218+ stream: stream
219+ )
220+ }
221+ }
206 }
222 }
207 }
223 }
208 .onDisappear {
224 .onDisappear {