5151from mycli .packages .hybrid_redirection import get_redirect_components , is_redirect_command
5252from mycli .packages .parseutils import is_destructive , is_dropping_database
5353from mycli .packages .prompt_utils import confirm , confirm_destructive_query
54- from mycli .packages .special .favoritequeries import FavoriteQueries
5554from mycli .packages .special .main import ArgType
5655from mycli .packages .tabular_output import sql_format
5756from mycli .packages .toolkit .history import FileHistoryWithTimestamp
@@ -128,8 +127,6 @@ def __init__(
128127 special .set_timing_enabled (c ["main" ].as_bool ("timing" ))
129128 self .beep_after_seconds = float (c ["main" ]["beep_after_seconds" ] or 0 )
130129
131- FavoriteQueries .instance = FavoriteQueries .from_config (self .config )
132-
133130 self .dsn_alias = None
134131 self .main_formatter = TabularOutputFormatter (format_name = c ["main" ]["table_format" ])
135132 self .redirect_formatter = TabularOutputFormatter (format_name = c ["main" ].get ("redirect_format" , "csv" ))
@@ -681,6 +678,47 @@ def get_continuation(width, *_):
681678 def show_suggestion_tip ():
682679 return iterations < 2
683680
681+ def output_res (res , start ):
682+ result_count = 0
683+ mutating = False
684+ for title , cur , headers , status in res :
685+ logger .debug ("headers: %r" , headers )
686+ logger .debug ("rows: %r" , cur )
687+ logger .debug ("status: %r" , status )
688+ threshold = 1000
689+ if is_select (status ) and cur and cur .rowcount > threshold :
690+ self .echo (
691+ "The result set has more than {} rows." .format (threshold ),
692+ fg = "red" ,
693+ )
694+ if not confirm ("Do you want to continue?" ):
695+ self .echo ("Aborted!" , err = True , fg = "red" )
696+ break
697+
698+ if self .auto_vertical_output :
699+ max_width = self .prompt_app .output .get_size ().columns
700+ else :
701+ max_width = None
702+
703+ formatted = self .format_output (title , cur , headers , special .is_expanded_output (), max_width )
704+
705+ t = time () - start
706+ try :
707+ if result_count > 0 :
708+ self .echo ("" )
709+ try :
710+ self .output (formatted , status )
711+ except KeyboardInterrupt :
712+ pass
713+ self .echo ("Time: %0.03fs" % t )
714+ except KeyboardInterrupt :
715+ pass
716+
717+ start = time ()
718+ result_count += 1
719+ mutating = mutating or is_mutating (status )
720+ return mutating
721+
684722 def one_iteration (text = None ):
685723 if text is None :
686724 try :
@@ -707,6 +745,27 @@ def one_iteration(text=None):
707745 logger .error ("traceback: %r" , traceback .format_exc ())
708746 self .echo (str (e ), err = True , fg = "red" )
709747 return
748+ # LLM command support
749+ while special .is_llm_command (text ):
750+ try :
751+ start = time ()
752+ cur = sqlexecute .conn .cursor ()
753+ context , sql , duration = special .handle_llm (text , cur )
754+ if context :
755+ click .echo ("LLM Response:" )
756+ click .echo (context )
757+ click .echo ("---" )
758+ click .echo (f"Time: { duration :.2f} seconds" )
759+ text = self .prompt_app .prompt (default = sql )
760+ except KeyboardInterrupt :
761+ return
762+ except special .FinishIteration as e :
763+ return output_res (e .results , start ) if e .results else None
764+ except RuntimeError as e :
765+ logger .error ("sql: %r, error: %r" , text , e )
766+ logger .error ("traceback: %r" , traceback .format_exc ())
767+ self .echo (str (e ), err = True , fg = "red" )
768+ return
710769
711770 if not text .strip ():
712771 return
0 commit comments