1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-05 13:16:09 +03:00

MDEV-34406 Enhance mariadb_upgrade to print failing query in case of error

To make this possible, it was also necessary to enhance the mariadb
client with the option --print-query-on-error.
This option can also be very useful when running a batch of queries
through the mariadb client and one wants to find out where things goes
wrong.

TODO: It would be good to enhance mariadb_upgrade to not call the mariadb
client for executing queries but instead do this internally.  This
would have made this patch much easier!

Reviewed by: Sergei Golubchik <serg@mariadb.com>
This commit is contained in:
Monty
2024-06-15 14:26:07 +03:00
parent 4b4c371fe7
commit fef32fd9ad
3 changed files with 101 additions and 33 deletions

View File

@@ -158,7 +158,8 @@ static my_bool ignore_errors=0,wait_flag=0,quick=0,
default_pager_set= 0, opt_sigint_ignore= 0,
auto_vertical_output= 0,
show_warnings= 0, executing_query= 0,
ignore_spaces= 0, opt_binhex= 0, opt_progress_reports;
ignore_spaces= 0, opt_binhex= 0, opt_progress_reports,
opt_print_query_on_error;
static my_bool debug_info_flag, debug_check_flag, batch_abort_on_error;
static my_bool column_types_flag;
static my_bool preserve_comments= 0;
@@ -237,6 +238,7 @@ static int com_quit(String *str,char*),
com_prompt(String *str, char*), com_delimiter(String *str, char*),
com_warnings(String *str, char*), com_nowarnings(String *str, char*),
com_sandbox(String *str, char*);
static void print_query_to_stderr(String *buffer);
#ifdef USE_POPEN
static int com_nopager(String *str, char*), com_pager(String *str, char*),
@@ -1659,6 +1661,10 @@ static struct my_option my_long_options[] =
#endif
"built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").", &opt_mysql_port,
&opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"print-query-on-error", 0,
"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)",
&opt_print_query_on_error, &opt_print_query_on_error, 0, GET_BOOL, NO_ARG,
1, 0, 0, 0, 0, 0},
{"progress-reports", 0,
"Get progress reports for long running commands (like ALTER TABLE)",
&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)
int error;
if (!mysql_real_query(&mysql,buf,(ulong)length))
return 0;
if (opt_print_query_on_error)
{
String query(buf, length, charset_info);
(void) print_query_to_stderr(&query);
}
error= put_error(&mysql);
if (mysql_errno(&mysql) != CR_SERVER_GONE_ERROR || retry > 1 ||
!opt_reconnect)
@@ -3291,7 +3302,6 @@ static int com_charset(String *, char *line)
1 if fatal error
*/
static int com_go(String *buffer, char *)
{
char buff[200]; /* about 110 chars used so far */
@@ -3363,6 +3373,8 @@ static int com_go(String *buffer, char *)
{
if (!(result=mysql_use_result(&mysql)) && mysql_field_count(&mysql))
{
if (opt_print_query_on_error)
print_query_to_stderr(buffer);
error= put_error(&mysql);
goto end;
}
@@ -3416,7 +3428,11 @@ static int com_go(String *buffer, char *)
(long) mysql_num_rows(result) == 1 ? "row" : "rows");
end_pager();
if (mysql_errno(&mysql))
{
if (opt_print_query_on_error)
print_query_to_stderr(buffer);
error= put_error(&mysql);
}
}
}
else if (mysql_affected_rows(&mysql) == ~(ulonglong) 0)
@@ -3443,13 +3459,21 @@ static int com_go(String *buffer, char *)
put_info("",INFO_RESULT); // Empty row
if (result && !mysql_eof(result)) /* Something wrong when using quick */
{
if (opt_print_query_on_error)
print_query_to_stderr(buffer);
error= put_error(&mysql);
}
else if (unbuffered)
fflush(stdout);
mysql_free_result(result);
} while (!(err= mysql_next_result(&mysql)));
if (err >= 1)
{
if (opt_print_query_on_error)
print_query_to_stderr(buffer);
error= put_error(&mysql);
}
end:
@@ -4375,14 +4399,35 @@ static int com_shell(String *, char *line)
#endif
static void print_query(String *buffer, FILE *file)
{
tee_puts("--------------", file);
(void) tee_fputs(buffer->c_ptr(), file);
if (!buffer->length() || (*buffer)[buffer->length()-1] != '\n')
tee_putc('\n', file);
tee_puts("--------------\n", file);
}
/*
Print query to stderr in batch mode if verbose is not set
*/
static void print_query_to_stderr(String *buffer)
{
if ((status.batch || in_com_source) && !verbose)
{
fflush(stdout);
print_query(buffer, stderr);
fflush(stderr);
}
}
static int com_print(String *buffer,char *)
{
tee_puts("--------------", stdout);
(void) tee_fputs(buffer->c_ptr(), stdout);
if (!buffer->length() || (*buffer)[buffer->length()-1] != '\n')
tee_putc('\n', stdout);
tee_puts("--------------\n", stdout);
return 0; /* If empty buffer */
print_query(buffer, stdout);
return 0;
}
@@ -5117,8 +5162,9 @@ put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
static int put_error(MYSQL *con)
{
return put_info(mysql_error(con), INFO_ERROR, mysql_errno(con),
mysql_sqlstate(con));
DBUG_ENTER("put_error");
DBUG_RETURN(put_info(mysql_error(con), INFO_ERROR,
mysql_errno(con), mysql_sqlstate(con)));
}