1
/* Copyright (C) 1995-2002 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/**********************************************************************
17
This file contains the implementation of error and warnings related
19
- Whenever an error or warning occurred, it pushes it to a warning list
20
that the user can retrieve with SHOW WARNINGS or SHOW ERRORS.
22
- For each statement, we return the number of warnings generated from this
23
command. Note that this can be different from @@warning_count as
24
we reset the warning list only for questions that uses a table.
25
This is done to allow on to do:
27
SELECT @@warning_count;
29
(If we would reset after each command, we could not retrieve the number
32
- When client requests the information using SHOW command, then
33
server processes from this list and returns back in the form of
38
SHOW [COUNT(*)] ERRORS [LIMIT [offset,] rows]
39
SHOW [COUNT(*)] WARNINGS [LIMIT [offset,] rows]
40
SELECT @@warning_count, @@error_count;
42
***********************************************************************/
44
#include "mysql_priv.h"
45
#include "sp_rcontext.h"
48
Store a new message in an error object
50
This is used to in group_concat() to register how many warnings we actually
51
got after the query has been executed.
54
void MYSQL_ERROR::set_msg(THD *thd, const char *msg_arg)
56
msg= strdup_root(&thd->warn_root, msg_arg);
61
Reset all warnings for the thread
66
force Reset warnings even if it has been done before
69
Don't reset warnings if this has already been called for this query.
70
This may happen if one gets a warning during the parsing stage,
71
in which case push_warnings() has already called this function.
74
void mysql_reset_errors(THD *thd, bool force)
76
DBUG_ENTER("mysql_reset_errors");
77
if (thd->query_id != thd->warn_id || force)
79
thd->warn_id= thd->query_id;
80
free_root(&thd->warn_root,MYF(0));
81
bzero((char*) thd->warn_count, sizeof(thd->warn_count));
83
thd->total_warn_count= 0;
84
thd->warn_list.empty();
85
thd->row_count= 1; // by default point to row 1
92
Push the warning/error to error list if there is still room in the list
97
level Severity of warning (note, warning, error ...)
99
msg Clear error message
102
pointer on MYSQL_ERROR object
105
MYSQL_ERROR *push_warning(THD *thd, MYSQL_ERROR::enum_warning_level level,
106
uint code, const char *msg)
109
DBUG_ENTER("push_warning");
110
DBUG_PRINT("enter", ("code: %d, msg: %s", code, msg));
112
DBUG_ASSERT(code != 0);
113
DBUG_ASSERT(msg != NULL);
115
if (level == MYSQL_ERROR::WARN_LEVEL_NOTE &&
116
!(thd->options & OPTION_SQL_NOTES))
119
if (thd->query_id != thd->warn_id && !thd->spcont)
120
mysql_reset_errors(thd, 0);
123
/* Abort if we are using strict mode and we are not using IGNORE */
124
if ((int) level >= (int) MYSQL_ERROR::WARN_LEVEL_WARN &&
125
thd->really_abort_on_warning())
127
/* Avoid my_message() calling push_warning */
128
bool no_warnings_for_error= thd->no_warnings_for_error;
129
sp_rcontext *spcont= thd->spcont;
131
thd->no_warnings_for_error= 1;
134
thd->killed= THD::KILL_BAD_DATA;
135
my_message(code, msg, MYF(0));
138
thd->no_warnings_for_error= no_warnings_for_error;
139
/* Store error in error list (as my_message() didn't do it) */
140
level= MYSQL_ERROR::WARN_LEVEL_ERROR;
143
if (thd->handle_error(code, msg, level))
147
thd->spcont->handle_error(code, level, thd))
151
query_cache_abort(&thd->net);
154
if (thd->warn_list.elements < thd->variables.max_error_count)
156
/* We have to use warn_root, as mem_root is freed after each query */
157
if ((err= new (&thd->warn_root) MYSQL_ERROR(thd, code, level, msg)))
158
thd->warn_list.push_back(err, &thd->warn_root);
160
thd->warn_count[(uint) level]++;
161
thd->total_warn_count++;
166
Push the warning/error to error list if there is still room in the list
169
push_warning_printf()
171
level Severity of warning (note, warning, error ...)
173
msg Clear error message
176
void push_warning_printf(THD *thd, MYSQL_ERROR::enum_warning_level level,
177
uint code, const char *format, ...)
180
char warning[MYSQL_ERRMSG_SIZE];
181
DBUG_ENTER("push_warning_printf");
182
DBUG_PRINT("enter",("warning: %u", code));
184
DBUG_ASSERT(code != 0);
185
DBUG_ASSERT(format != NULL);
187
va_start(args,format);
188
my_vsnprintf(warning, sizeof(warning), format, args);
190
push_warning(thd, level, code, warning);
196
Send all notes, errors or warnings to the client in a result set
199
mysqld_show_warnings()
201
levels_to_show Bitmap for which levels to show
204
Takes into account the current LIMIT
208
TRUE Error sending data to client
211
const LEX_STRING warning_level_names[]=
213
{ C_STRING_WITH_LEN("Note") },
214
{ C_STRING_WITH_LEN("Warning") },
215
{ C_STRING_WITH_LEN("Error") },
216
{ C_STRING_WITH_LEN("?") }
219
bool mysqld_show_warnings(THD *thd, ulong levels_to_show)
221
List<Item> field_list;
222
DBUG_ENTER("mysqld_show_warnings");
224
field_list.push_back(new Item_empty_string("Level", 7));
225
field_list.push_back(new Item_return_int("Code",4, MYSQL_TYPE_LONG));
226
field_list.push_back(new Item_empty_string("Message",MYSQL_ERRMSG_SIZE));
228
if (thd->protocol->send_fields(&field_list,
229
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
233
SELECT_LEX *sel= &thd->lex->select_lex;
234
SELECT_LEX_UNIT *unit= &thd->lex->unit;
236
Protocol *protocol=thd->protocol;
238
unit->set_limit(sel);
240
List_iterator_fast<MYSQL_ERROR> it(thd->warn_list);
243
/* Skip levels that the user is not interested in */
244
if (!(levels_to_show & ((ulong) 1 << err->level)))
246
if (++idx <= unit->offset_limit_cnt)
248
if (idx > unit->select_limit_cnt)
250
protocol->prepare_for_resend();
251
protocol->store(warning_level_names[err->level].str,
252
warning_level_names[err->level].length, system_charset_info);
253
protocol->store((uint32) err->code);
254
protocol->store(err->msg, (uint) strlen(err->msg), system_charset_info);
255
if (protocol->write())