@@ -28,8 +28,6 @@ use tui_logger::TuiWidgetEvent;
2828use crate :: tui:: state:: tabs:: flightsql:: FlightSQLConnectionStatus ;
2929use crate :: tui:: state:: tabs:: history:: Context ;
3030use crate :: tui:: ExecutionResultsBatch ;
31-
32- #[ cfg( feature = "flightsql" ) ]
3331use std:: sync:: Arc ;
3432
3533use super :: App ;
@@ -222,13 +220,50 @@ pub fn app_event_handler(app: &mut App, event: AppEvent) -> Result<()> {
222220 duration,
223221 batch,
224222 } = r;
223+
224+ let is_first_batch = app. state . sql_tab . current_page ( ) . is_none ( ) ;
225+
225226 app. state . sql_tab . add_batch ( batch) ;
227+
228+ if is_first_batch {
229+ app. state . sql_tab . next_page ( ) ;
230+ app. state . sql_tab . refresh_query_results_state ( ) ;
231+
232+ let history_query =
233+ HistoryQuery :: new ( Context :: Local , query. to_string ( ) , duration, None , None ) ;
234+ app. state . history_tab . add_to_history ( history_query) ;
235+ app. state . history_tab . refresh_history_table_state ( ) ;
236+ } else {
237+ app. state . sql_tab . refresh_query_results_state ( ) ;
238+
239+ // Check if we have enough data for the next page now
240+ // If not, automatically fetch another batch
241+ if let Some ( current_page) = app. state . sql_tab . current_page ( ) {
242+ let next_page = current_page + 1 ;
243+ if app. state . sql_tab . needs_more_batches_for_page ( next_page) {
244+ info ! (
245+ "Still need more batches for page {}, fetching next batch" ,
246+ next_page
247+ ) ;
248+ let execution = Arc :: clone ( & app. execution ) ;
249+ let sql = query. clone ( ) ;
250+ let _event_tx = app. event_tx ( ) ;
251+ tokio:: spawn ( async move {
252+ execution. next_batch ( sql, _event_tx) . await ;
253+ } ) ;
254+ } else {
255+ // We now have enough data, advance to the page
256+ info ! ( "Sufficient data loaded, advancing to page {}" , next_page) ;
257+ if let Err ( e) = app. event_tx ( ) . send ( AppEvent :: ExecutionResultsNextPage ) {
258+ error ! ( "Error advancing to next page: {e}" ) ;
259+ }
260+ }
261+ }
262+ }
263+ }
264+ AppEvent :: ExecutionResultsNextPage => {
226265 app. state . sql_tab . next_page ( ) ;
227266 app. state . sql_tab . refresh_query_results_state ( ) ;
228- let history_query =
229- HistoryQuery :: new ( Context :: Local , query. to_string ( ) , duration, None , None ) ;
230- app. state . history_tab . add_to_history ( history_query) ;
231- app. state . history_tab . refresh_history_table_state ( ) ;
232267 }
233268 #[ cfg( feature = "flightsql" ) ]
234269 AppEvent :: FlightSQLExecutionResultsNextBatch ( r) => {
@@ -237,15 +272,54 @@ pub fn app_event_handler(app: &mut App, event: AppEvent) -> Result<()> {
237272 duration,
238273 batch,
239274 } = r;
240- info ! ( "Adding batch to flightsql tab" ) ;
275+
276+ let is_first_batch = app. state . flightsql_tab . current_page ( ) . is_none ( ) ;
277+
241278 app. state . flightsql_tab . set_in_progress ( false ) ;
242279 app. state . flightsql_tab . add_batch ( batch) ;
243- app. state . flightsql_tab . next_page ( ) ;
244- app. state . flightsql_tab . refresh_query_results_state ( ) ;
245- let history_query =
246- HistoryQuery :: new ( Context :: FlightSQL , query. to_string ( ) , duration, None , None ) ;
247- app. state . history_tab . add_to_history ( history_query) ;
248- app. state . history_tab . refresh_history_table_state ( ) ;
280+
281+ if is_first_batch {
282+ app. state . flightsql_tab . next_page ( ) ;
283+ app. state . flightsql_tab . refresh_query_results_state ( ) ;
284+
285+ let history_query =
286+ HistoryQuery :: new ( Context :: FlightSQL , query. to_string ( ) , duration, None , None ) ;
287+ app. state . history_tab . add_to_history ( history_query) ;
288+ app. state . history_tab . refresh_history_table_state ( ) ;
289+ } else {
290+ app. state . flightsql_tab . refresh_query_results_state ( ) ;
291+
292+ // Check if we have enough data for the next page now
293+ // If not, automatically fetch another batch
294+ if let Some ( current_page) = app. state . flightsql_tab . current_page ( ) {
295+ let next_page = current_page + 1 ;
296+ if app
297+ . state
298+ . flightsql_tab
299+ . needs_more_batches_for_page ( next_page)
300+ {
301+ info ! (
302+ "Still need more batches for page {}, fetching next batch" ,
303+ next_page
304+ ) ;
305+ let execution = Arc :: clone ( & app. execution ) ;
306+ let sql = query. clone ( ) ;
307+ let _event_tx = app. event_tx ( ) ;
308+ tokio:: spawn ( async move {
309+ execution. next_flightsql_batch ( sql, _event_tx) . await ;
310+ } ) ;
311+ } else {
312+ // We now have enough data, advance to the page
313+ info ! ( "Sufficient data loaded, advancing to page {}" , next_page) ;
314+ if let Err ( e) = app
315+ . event_tx ( )
316+ . send ( AppEvent :: FlightSQLExecutionResultsNextPage )
317+ {
318+ error ! ( "Error advancing to next page: {e}" ) ;
319+ }
320+ }
321+ }
322+ }
249323 }
250324 #[ cfg( feature = "flightsql" ) ]
251325 AppEvent :: FlightSQLEstablishConnection => {
0 commit comments