@@ -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" )
@@ -342,7 +343,28 @@ def __init__(
342343 def quit (self ):
343344 raise PgCliQuitError
344345
346+ def toggle_named_query_quiet (self ):
347+ """Toggle hiding of named query text"""
348+ self .hide_named_query_text = not self .hide_named_query_text
349+ status = "ON" if self .hide_named_query_text else "OFF"
350+ message = f"Named query quiet mode: { status } "
351+ return [(None , None , None , message )]
352+
353+ def _is_named_query_execution (self , text ):
354+ """Check if the command is a named query execution (\n <name>)."""
355+ text = text .strip ()
356+ return text .startswith ("\\ n " ) and not text .startswith ("\\ ns " ) and not text .startswith ("\\ nd " )
357+
345358 def register_special_commands (self ):
359+ self .pgspecial .register (
360+ self .toggle_named_query_quiet ,
361+ "\\ nq" ,
362+ "\\ nq" ,
363+ "Toggle named query quiet mode (hide query text)" ,
364+ arg_type = NO_QUERY ,
365+ case_sensitive = True ,
366+ )
367+
346368 self .pgspecial .register (
347369 self .change_db ,
348370 "\\ c" ,
@@ -972,7 +994,14 @@ def execute_command(self, text, handle_closed_connection=True):
972994 if self .output_file and not text .startswith (("\\ o " , "\\ log-file" , "\\ ? " , "\\ echo " )):
973995 try :
974996 with open (self .output_file , "a" , encoding = "utf-8" ) as f :
975- click .echo (text , file = f )
997+ should_hide = (
998+ self .hide_named_query_text
999+ and query .is_special
1000+ and query .successful
1001+ and self ._is_named_query_execution (text )
1002+ )
1003+ if not should_hide :
1004+ click .echo (text , file = f )
9761005 click .echo ("\n " .join (output ), file = f )
9771006 click .echo ("" , file = f ) # extra newline
9781007 except OSError as e :
@@ -986,7 +1015,14 @@ def execute_command(self, text, handle_closed_connection=True):
9861015 try :
9871016 with open (self .log_file , "a" , encoding = "utf-8" ) as f :
9881017 click .echo (dt .datetime .now ().isoformat (), file = f ) # timestamp log
989- click .echo (text , file = f )
1018+ should_hide = (
1019+ self .hide_named_query_text
1020+ and query .is_special
1021+ and query .successful
1022+ and self ._is_named_query_execution (text )
1023+ )
1024+ if not should_hide :
1025+ click .echo (text , file = f )
9901026 click .echo ("\n " .join (output ), file = f )
9911027 click .echo ("" , file = f ) # extra newline
9921028 except OSError as e :
@@ -1324,6 +1360,18 @@ def _evaluate_command(self, text):
13241360 tuples_only = self .tuples_only ,
13251361 show_status = self .show_status ,
13261362 )
1363+
1364+ # Hide query text for named queries in quiet mode
1365+ if (
1366+ self .hide_named_query_text
1367+ and is_special
1368+ and success
1369+ and self ._is_named_query_execution (text )
1370+ and title
1371+ and title .startswith ("> " )
1372+ ):
1373+ title = None
1374+
13271375 execution = time () - start
13281376 formatted = format_output (title , cur , headers , status , settings , self .explain_mode )
13291377
@@ -1447,6 +1495,7 @@ def get_prompt(self, string):
14471495 string = string .replace ("\\ i" , str (self .pgexecute .pid ) or "(none)" )
14481496 string = string .replace ("\\ #" , "#" if self .pgexecute .superuser else ">" )
14491497 string = string .replace ("\\ n" , "\n " )
1498+ string = string .replace ("\\ T" , self .pgexecute .transaction_indicator )
14501499 return string
14511500
14521501 def get_last_query (self ):
0 commit comments