@@ -158,7 +158,8 @@ static my_bool ignore_errors=0,wait_flag=0,quick=0,
158158 default_pager_set= 0 , opt_sigint_ignore= 0 ,
159159 auto_vertical_output= 0 ,
160160 show_warnings= 0 , executing_query= 0 ,
161- ignore_spaces= 0 , opt_binhex= 0 , opt_progress_reports;
161+ ignore_spaces= 0 , opt_binhex= 0 , opt_progress_reports,
162+ opt_print_query_on_error;
162163static my_bool debug_info_flag, debug_check_flag, batch_abort_on_error;
163164static my_bool column_types_flag;
164165static my_bool preserve_comments= 0 ;
@@ -237,6 +238,7 @@ static int com_quit(String *str,char*),
237238 com_prompt(String *str, char *), com_delimiter(String *str, char *),
238239 com_warnings(String *str, char *), com_nowarnings(String *str, char *),
239240 com_sandbox(String *str, char *);
241+ static void print_query_to_stderr (String *buffer);
240242
241243#ifdef USE_POPEN
242244static int com_nopager (String *str, char *), com_pager(String *str, char *),
@@ -1659,6 +1661,10 @@ static struct my_option my_long_options[] =
16591661#endif
16601662 " built-in default (" STRINGIFY_ARG (MYSQL_PORT) " )." , &opt_mysql_port,
16611663 &opt_mysql_port, 0 , GET_UINT, REQUIRED_ARG, 0 , 0 , 0 , 0 , 0 , 0 },
1664+ {" print-query-on-error" , 0 ,
1665+ " Print the query if there was an error. Is only enabled in --batch mode if verbose is not set (as then the query would be printed anyway)" ,
1666+ &opt_print_query_on_error, &opt_print_query_on_error, 0 , GET_BOOL, NO_ARG,
1667+ 1 , 0 , 0 , 0 , 0 , 0 },
16621668 {" progress-reports" , 0 ,
16631669 " Get progress reports for long running commands (like ALTER TABLE)" ,
16641670 &opt_progress_reports, &opt_progress_reports, 0 , GET_BOOL, NO_ARG, 1 , 0 ,
@@ -3086,6 +3092,11 @@ int mysql_real_query_for_lazy(const char *buf, size_t length)
30863092 int error;
30873093 if (!mysql_real_query (&mysql,buf,(ulong)length))
30883094 return 0 ;
3095+ if (opt_print_query_on_error)
3096+ {
3097+ String query (buf, length, charset_info);
3098+ (void ) print_query_to_stderr (&query);
3099+ }
30893100 error= put_error (&mysql);
30903101 if (mysql_errno (&mysql) != CR_SERVER_GONE_ERROR || retry > 1 ||
30913102 !opt_reconnect)
@@ -3291,7 +3302,6 @@ static int com_charset(String *, char *line)
32913302 1 if fatal error
32923303*/
32933304
3294-
32953305static int com_go (String *buffer, char *)
32963306{
32973307 char buff[200 ]; /* about 110 chars used so far */
@@ -3363,6 +3373,8 @@ static int com_go(String *buffer, char *)
33633373 {
33643374 if (!(result=mysql_use_result (&mysql)) && mysql_field_count (&mysql))
33653375 {
3376+ if (opt_print_query_on_error)
3377+ print_query_to_stderr (buffer);
33663378 error= put_error (&mysql);
33673379 goto end;
33683380 }
@@ -3416,7 +3428,11 @@ static int com_go(String *buffer, char *)
34163428 (long ) mysql_num_rows (result) == 1 ? " row" : " rows" );
34173429 end_pager ();
34183430 if (mysql_errno (&mysql))
3431+ {
3432+ if (opt_print_query_on_error)
3433+ print_query_to_stderr (buffer);
34193434 error= put_error (&mysql);
3435+ }
34203436 }
34213437 }
34223438 else if (mysql_affected_rows (&mysql) == ~(ulonglong) 0 )
@@ -3443,13 +3459,21 @@ static int com_go(String *buffer, char *)
34433459 put_info (" " ,INFO_RESULT); // Empty row
34443460
34453461 if (result && !mysql_eof (result)) /* Something wrong when using quick */
3462+ {
3463+ if (opt_print_query_on_error)
3464+ print_query_to_stderr (buffer);
34463465 error= put_error (&mysql);
3466+ }
34473467 else if (unbuffered)
34483468 fflush (stdout);
34493469 mysql_free_result (result);
34503470 } while (!(err= mysql_next_result (&mysql)));
34513471 if (err >= 1 )
3472+ {
3473+ if (opt_print_query_on_error)
3474+ print_query_to_stderr (buffer);
34523475 error= put_error (&mysql);
3476+ }
34533477
34543478end:
34553479
@@ -4375,14 +4399,35 @@ static int com_shell(String *, char *line)
43754399#endif
43764400
43774401
4378- static int com_print (String *buffer,char * )
4402+ static void print_query (String *buffer, FILE *file )
43794403{
4380- tee_puts (" --------------" , stdout );
4381- (void ) tee_fputs (buffer->c_ptr (), stdout );
4404+ tee_puts (" --------------" , file );
4405+ (void ) tee_fputs (buffer->c_ptr (), file );
43824406 if (!buffer->length () || (*buffer)[buffer->length ()-1 ] != ' \n ' )
4383- tee_putc (' \n ' , stdout);
4384- tee_puts (" --------------\n " , stdout);
4385- return 0 ; /* If empty buffer */
4407+ tee_putc (' \n ' , file);
4408+ tee_puts (" --------------\n " , file);
4409+ }
4410+
4411+
4412+ /*
4413+ Print query to stderr in batch mode if verbose is not set
4414+ */
4415+
4416+ static void print_query_to_stderr (String *buffer)
4417+ {
4418+ if ((status.batch || in_com_source) && !verbose)
4419+ {
4420+ fflush (stdout);
4421+ print_query (buffer, stderr);
4422+ fflush (stderr);
4423+ }
4424+ }
4425+
4426+
4427+ static int com_print (String *buffer,char *)
4428+ {
4429+ print_query (buffer, stdout);
4430+ return 0 ;
43864431}
43874432
43884433
@@ -5117,8 +5162,9 @@ put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
51175162
51185163static int put_error (MYSQL *con)
51195164{
5120- return put_info (mysql_error (con), INFO_ERROR, mysql_errno (con),
5121- mysql_sqlstate (con));
5165+ DBUG_ENTER (" put_error" );
5166+ DBUG_RETURN (put_info (mysql_error (con), INFO_ERROR,
5167+ mysql_errno (con), mysql_sqlstate (con)));
51225168}
51235169
51245170
0 commit comments