~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to sql/sql_error.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 1995-2002 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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 */
 
15
 
 
16
/**********************************************************************
 
17
This file contains the implementation of error and warnings related
 
18
 
 
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.
 
21
 
 
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:
 
26
    INSERT ...;
 
27
    SELECT @@warning_count;
 
28
    SHOW WARNINGS;
 
29
    (If we would reset after each command, we could not retrieve the number
 
30
     of warnings)
 
31
 
 
32
  - When client requests the information using SHOW command, then 
 
33
    server processes from this list and returns back in the form of 
 
34
    resultset.
 
35
 
 
36
    Supported syntaxes:
 
37
 
 
38
    SHOW [COUNT(*)] ERRORS [LIMIT [offset,] rows]
 
39
    SHOW [COUNT(*)] WARNINGS [LIMIT [offset,] rows]
 
40
    SELECT @@warning_count, @@error_count;
 
41
 
 
42
***********************************************************************/
 
43
 
 
44
#include "mysql_priv.h"
 
45
 
 
46
/*
 
47
  Store a new message in an error object
 
48
 
 
49
  This is used to in group_concat() to register how many warnings we actually
 
50
  got after the query has been executed.
 
51
*/
 
52
 
 
53
void MYSQL_ERROR::set_msg(THD *thd, const char *msg_arg)
 
54
{
 
55
  msg= strdup_root(&thd->warn_root, msg_arg);
 
56
}
 
57
 
 
58
 
 
59
/*
 
60
  Reset all warnings for the thread
 
61
 
 
62
  SYNOPSIS
 
63
    mysql_reset_errors()
 
64
    thd                 Thread handle
 
65
    force               Reset warnings even if it has been done before
 
66
 
 
67
  IMPLEMENTATION
 
68
    Don't reset warnings if this has already been called for this query.
 
69
    This may happen if one gets a warning during the parsing stage,
 
70
    in which case push_warnings() has already called this function.
 
71
*/  
 
72
 
 
73
void mysql_reset_errors(THD *thd, bool force)
 
74
{
 
75
  DBUG_ENTER("mysql_reset_errors");
 
76
  if (thd->query_id != thd->warn_id || force)
 
77
  {
 
78
    thd->warn_id= thd->query_id;
 
79
    free_root(&thd->warn_root,MYF(0));
 
80
    bzero((char*) thd->warn_count, sizeof(thd->warn_count));
 
81
    if (force)
 
82
      thd->total_warn_count= 0;
 
83
    thd->warn_list.empty();
 
84
    thd->row_count= 1; // by default point to row 1
 
85
  }
 
86
  DBUG_VOID_RETURN;
 
87
}
 
88
 
 
89
 
 
90
/* 
 
91
  Push the warning/error to error list if there is still room in the list
 
92
 
 
93
  SYNOPSIS
 
94
    push_warning()
 
95
    thd                 Thread handle
 
96
    level               Severity of warning (note, warning, error ...)
 
97
    code                Error number
 
98
    msg                 Clear error message
 
99
    
 
100
  RETURN
 
101
    pointer on MYSQL_ERROR object
 
102
*/
 
103
 
 
104
MYSQL_ERROR *push_warning(THD *thd, MYSQL_ERROR::enum_warning_level level, 
 
105
                          uint code, const char *msg)
 
106
{
 
107
  MYSQL_ERROR *err= 0;
 
108
  DBUG_ENTER("push_warning");
 
109
  DBUG_PRINT("enter", ("code: %d, msg: %s", code, msg));
 
110
 
 
111
  if (level == MYSQL_ERROR::WARN_LEVEL_NOTE &&
 
112
      !(thd->options & OPTION_SQL_NOTES))
 
113
    DBUG_RETURN(0);
 
114
 
 
115
  if (thd->query_id != thd->warn_id)
 
116
    mysql_reset_errors(thd, 0);
 
117
  thd->got_warning= 1;
 
118
 
 
119
  /* Abort if we are using strict mode and we are not using IGNORE */
 
120
  if ((int) level >= (int) MYSQL_ERROR::WARN_LEVEL_WARN &&
 
121
      thd->really_abort_on_warning())
 
122
  {
 
123
    /* Avoid my_message() calling push_warning */
 
124
    bool no_warnings_for_error= thd->no_warnings_for_error;
 
125
 
 
126
    thd->no_warnings_for_error= 1;
 
127
 
 
128
    thd->killed= THD::KILL_BAD_DATA;
 
129
    my_message(code, msg, MYF(0));
 
130
 
 
131
    thd->no_warnings_for_error= no_warnings_for_error;
 
132
    /* Store error in error list (as my_message() didn't do it) */
 
133
    level= MYSQL_ERROR::WARN_LEVEL_ERROR;
 
134
  }
 
135
 
 
136
  if (thd->handle_error(code, msg, level))
 
137
    DBUG_RETURN(NULL);
 
138
 
 
139
  if (thd->warn_list.elements < thd->variables.max_error_count)
 
140
  {
 
141
    /* We have to use warn_root, as mem_root is freed after each query */
 
142
    if ((err= new (&thd->warn_root) MYSQL_ERROR(thd, code, level, msg)))
 
143
      thd->warn_list.push_back(err, &thd->warn_root);
 
144
  }
 
145
  thd->warn_count[(uint) level]++;
 
146
  thd->total_warn_count++;
 
147
  DBUG_RETURN(err);
 
148
}
 
149
 
 
150
/*
 
151
  Push the warning/error to error list if there is still room in the list
 
152
 
 
153
  SYNOPSIS
 
154
    push_warning_printf()
 
155
    thd                 Thread handle
 
156
    level               Severity of warning (note, warning, error ...)
 
157
    code                Error number
 
158
    msg                 Clear error message
 
159
*/
 
160
 
 
161
void push_warning_printf(THD *thd, MYSQL_ERROR::enum_warning_level level,
 
162
                         uint code, const char *format, ...)
 
163
{
 
164
  va_list args;
 
165
  char    warning[ERRMSGSIZE+20];
 
166
  DBUG_ENTER("push_warning_printf");
 
167
  DBUG_PRINT("enter",("warning: %u", code));
 
168
  
 
169
  va_start(args,format);
 
170
  my_vsnprintf(warning, sizeof(warning), format, args);
 
171
  va_end(args);
 
172
  push_warning(thd, level, code, warning);
 
173
  DBUG_VOID_RETURN;
 
174
}
 
175
 
 
176
 
 
177
/*
 
178
  Send all notes, errors or warnings to the client in a result set
 
179
 
 
180
  SYNOPSIS
 
181
    mysqld_show_warnings()
 
182
    thd                 Thread handler
 
183
    levels_to_show      Bitmap for which levels to show
 
184
 
 
185
  DESCRIPTION
 
186
    Takes into account the current LIMIT
 
187
 
 
188
  RETURN VALUES
 
189
    FALSE ok
 
190
    TRUE  Error sending data to client
 
191
*/
 
192
 
 
193
const LEX_STRING warning_level_names[]=
 
194
{
 
195
  { C_STRING_WITH_LEN("Note") },
 
196
  { C_STRING_WITH_LEN("Warning") },
 
197
  { C_STRING_WITH_LEN("Error") },
 
198
  { C_STRING_WITH_LEN("?") }
 
199
};
 
200
 
 
201
bool mysqld_show_warnings(THD *thd, ulong levels_to_show)
 
202
{  
 
203
  List<Item> field_list;
 
204
  DBUG_ENTER("mysqld_show_warnings");
 
205
 
 
206
  field_list.push_back(new Item_empty_string("Level", 7));
 
207
  field_list.push_back(new Item_return_int("Code",4, MYSQL_TYPE_LONG));
 
208
  field_list.push_back(new Item_empty_string("Message",MYSQL_ERRMSG_SIZE));
 
209
 
 
210
  if (thd->protocol->send_fields(&field_list,
 
211
                                 Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
 
212
    DBUG_RETURN(TRUE);
 
213
 
 
214
  MYSQL_ERROR *err;
 
215
  SELECT_LEX *sel= &thd->lex->select_lex;
 
216
  SELECT_LEX_UNIT *unit= &thd->lex->unit;
 
217
  ha_rows idx= 0;
 
218
  Protocol *protocol=thd->protocol;
 
219
 
 
220
  unit->set_limit(sel);
 
221
 
 
222
  List_iterator_fast<MYSQL_ERROR> it(thd->warn_list);
 
223
  while ((err= it++))
 
224
  {
 
225
    /* Skip levels that the user is not interested in */
 
226
    if (!(levels_to_show & ((ulong) 1 << err->level)))
 
227
      continue;
 
228
    if (++idx <= unit->offset_limit_cnt)
 
229
      continue;
 
230
    if (idx > unit->select_limit_cnt)
 
231
      break;
 
232
    protocol->prepare_for_resend();
 
233
    protocol->store(warning_level_names[err->level].str,
 
234
                    warning_level_names[err->level].length, system_charset_info);
 
235
    protocol->store((uint32) err->code);
 
236
    protocol->store(err->msg, strlen(err->msg), system_charset_info);
 
237
    if (protocol->write())
 
238
      DBUG_RETURN(TRUE);
 
239
  }
 
240
  my_eof(thd);
 
241
  DBUG_RETURN(FALSE);
 
242
}