@@ -208,6 +208,7 @@ def __init__(
208208 self .output_file = None
209209 self .pgspecial = PGSpecial ()
210210
211+ self .hide_named_query_text = "hide_named_query_text" in c ["main" ] and c ["main" ].as_bool ("hide_named_query_text" )
211212 self .explain_mode = False
212213 self .multi_line = c ["main" ].as_bool ("multi_line" )
213214 self .multiline_mode = c ["main" ].get ("multi_line_mode" , "psql" )
@@ -307,7 +308,28 @@ def __init__(
307308 def quit (self ):
308309 raise PgCliQuitError
309310
311+ def toggle_named_query_quiet (self ):
312+ """Toggle hiding of named query text"""
313+ self .hide_named_query_text = not self .hide_named_query_text
314+ status = "ON" if self .hide_named_query_text else "OFF"
315+ message = f"Named query quiet mode: { status } "
316+ return [(None , None , None , message )]
317+
318+ def _is_named_query_execution (self , text ):
319+ """Check if the command is a named query execution (\n <name>)."""
320+ text = text .strip ()
321+ return text .startswith ("\\ n " ) and not text .startswith ("\\ ns " ) and not text .startswith ("\\ nd " )
322+
310323 def register_special_commands (self ):
324+ self .pgspecial .register (
325+ self .toggle_named_query_quiet ,
326+ "\\ nq" ,
327+ "\\ nq" ,
328+ "Toggle named query quiet mode (hide query text)" ,
329+ arg_type = NO_QUERY ,
330+ case_sensitive = True ,
331+ )
332+
311333 self .pgspecial .register (
312334 self .change_db ,
313335 "\\ c" ,
@@ -828,7 +850,14 @@ def execute_command(self, text, handle_closed_connection=True):
828850 if self .output_file and not text .startswith (("\\ o " , "\\ log-file" , "\\ ? " , "\\ echo " )):
829851 try :
830852 with open (self .output_file , "a" , encoding = "utf-8" ) as f :
831- click .echo (text , file = f )
853+ should_hide = (
854+ self .hide_named_query_text
855+ and query .is_special
856+ and query .successful
857+ and self ._is_named_query_execution (text )
858+ )
859+ if not should_hide :
860+ click .echo (text , file = f )
832861 click .echo ("\n " .join (output ), file = f )
833862 click .echo ("" , file = f ) # extra newline
834863 except OSError as e :
@@ -842,7 +871,14 @@ def execute_command(self, text, handle_closed_connection=True):
842871 try :
843872 with open (self .log_file , "a" , encoding = "utf-8" ) as f :
844873 click .echo (dt .datetime .now ().isoformat (), file = f ) # timestamp log
845- click .echo (text , file = f )
874+ should_hide = (
875+ self .hide_named_query_text
876+ and query .is_special
877+ and query .successful
878+ and self ._is_named_query_execution (text )
879+ )
880+ if not should_hide :
881+ click .echo (text , file = f )
846882 click .echo ("\n " .join (output ), file = f )
847883 click .echo ("" , file = f ) # extra newline
848884 except OSError as e :
@@ -1136,6 +1172,18 @@ def _evaluate_command(self, text):
11361172 style_output = self .style_output ,
11371173 max_field_width = self .max_field_width ,
11381174 )
1175+
1176+ # Hide query text for named queries in quiet mode
1177+ if (
1178+ self .hide_named_query_text
1179+ and is_special
1180+ and success
1181+ and self ._is_named_query_execution (text )
1182+ and title
1183+ and title .startswith ("> " )
1184+ ):
1185+ title = None
1186+
11391187 execution = time () - start
11401188 formatted = format_output (title , cur , headers , status , settings , self .explain_mode )
11411189
0 commit comments