~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
32
  - When client requests the information using SHOW command, then
33
    server processes from this list and returns back in the form of
1 by brian
clean slate
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
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
44
#include <drizzled/server_includes.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
45
#include <drizzled/session.h>
46
#include <drizzled/sql_base.h>
675 by Brian Aker
Cleanup around item includes.
47
#include <drizzled/item/empty_string.h>
642.1.17 by Lee
header file clean up
48
#include <drizzled/item/return_int.h>
1 by brian
clean slate
49
971.3.65 by Eric Day
Namespace cleanup for Protocol and Listen.
50
using namespace drizzled;
51
1 by brian
clean slate
52
/*
53
  Store a new message in an error object
54
55
  This is used to in group_concat() to register how many warnings we actually
56
  got after the query has been executed.
57
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
58
void DRIZZLE_ERROR::set_msg(Session *session, const char *msg_arg)
1 by brian
clean slate
59
{
520.1.22 by Brian Aker
Second pass of thd cleanup
60
  msg= strdup_root(&session->warn_root, msg_arg);
1 by brian
clean slate
61
}
62
63
/*
64
  Reset all warnings for the thread
65
66
  SYNOPSIS
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
67
    drizzle_reset_errors()
520.1.22 by Brian Aker
Second pass of thd cleanup
68
    session			Thread handle
1 by brian
clean slate
69
    force               Reset warnings even if it has been done before
70
71
  IMPLEMENTATION
72
    Don't reset warnings if this has already been called for this query.
73
    This may happen if one gets a warning during the parsing stage,
74
    in which case push_warnings() has already called this function.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
75
*/
1 by brian
clean slate
76
520.1.22 by Brian Aker
Second pass of thd cleanup
77
void drizzle_reset_errors(Session *session, bool force)
1 by brian
clean slate
78
{
520.1.22 by Brian Aker
Second pass of thd cleanup
79
  if (session->query_id != session->warn_id || force)
1 by brian
clean slate
80
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
81
    session->warn_id= session->query_id;
82
    free_root(&session->warn_root,MYF(0));
83
    memset(session->warn_count, 0, sizeof(session->warn_count));
1 by brian
clean slate
84
    if (force)
520.1.22 by Brian Aker
Second pass of thd cleanup
85
      session->total_warn_count= 0;
86
    session->warn_list.empty();
87
    session->row_count= 1; // by default point to row 1
1 by brian
clean slate
88
  }
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
89
  return;
1 by brian
clean slate
90
}
91
92
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
93
/*
1 by brian
clean slate
94
  Push the warning/error to error list if there is still room in the list
95
96
  SYNOPSIS
97
    push_warning()
520.1.22 by Brian Aker
Second pass of thd cleanup
98
    session			Thread handle
1 by brian
clean slate
99
    level		Severity of warning (note, warning, error ...)
100
    code		Error number
101
    msg			Clear error message
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
102
1 by brian
clean slate
103
  RETURN
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
104
    pointer on DRIZZLE_ERROR object
1 by brian
clean slate
105
*/
106
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
107
DRIZZLE_ERROR *push_warning(Session *session, DRIZZLE_ERROR::enum_warning_level level,
482 by Brian Aker
Remove uint.
108
                          uint32_t code, const char *msg)
1 by brian
clean slate
109
{
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
110
  DRIZZLE_ERROR *err= 0;
1 by brian
clean slate
111
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
112
  if (level == DRIZZLE_ERROR::WARN_LEVEL_NOTE &&
520.1.22 by Brian Aker
Second pass of thd cleanup
113
      !(session->options & OPTION_SQL_NOTES))
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
114
    return(0);
1 by brian
clean slate
115
520.1.22 by Brian Aker
Second pass of thd cleanup
116
  if (session->query_id != session->warn_id)
117
    drizzle_reset_errors(session, 0);
118
  session->got_warning= 1;
1 by brian
clean slate
119
120
  /* Abort if we are using strict mode and we are not using IGNORE */
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
121
  if ((int) level >= (int) DRIZZLE_ERROR::WARN_LEVEL_WARN &&
520.1.22 by Brian Aker
Second pass of thd cleanup
122
      session->really_abort_on_warning())
1 by brian
clean slate
123
  {
124
    /* Avoid my_message() calling push_warning */
520.1.22 by Brian Aker
Second pass of thd cleanup
125
    bool no_warnings_for_error= session->no_warnings_for_error;
126
127
    session->no_warnings_for_error= 1;
128
129
    session->killed= Session::KILL_BAD_DATA;
1 by brian
clean slate
130
    my_message(code, msg, MYF(0));
131
520.1.22 by Brian Aker
Second pass of thd cleanup
132
    session->no_warnings_for_error= no_warnings_for_error;
1 by brian
clean slate
133
    /* Store error in error list (as my_message() didn't do it) */
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
134
    level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
1 by brian
clean slate
135
  }
136
520.1.22 by Brian Aker
Second pass of thd cleanup
137
  if (session->handle_error(code, msg, level))
1019.1.6 by Brian Aker
A number of random cleanups.
138
    return NULL;
1 by brian
clean slate
139
520.1.22 by Brian Aker
Second pass of thd cleanup
140
  if (session->warn_list.elements < session->variables.max_error_count)
1 by brian
clean slate
141
  {
142
    /* We have to use warn_root, as mem_root is freed after each query */
520.1.22 by Brian Aker
Second pass of thd cleanup
143
    if ((err= new (&session->warn_root) DRIZZLE_ERROR(session, code, level, msg)))
144
      session->warn_list.push_back(err, &session->warn_root);
1 by brian
clean slate
145
  }
895 by Brian Aker
Completion (?) of uint conversion.
146
  session->warn_count[(uint32_t) level]++;
520.1.22 by Brian Aker
Second pass of thd cleanup
147
  session->total_warn_count++;
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
148
  return(err);
1 by brian
clean slate
149
}
150
151
/*
152
  Push the warning/error to error list if there is still room in the list
153
154
  SYNOPSIS
155
    push_warning_printf()
520.1.22 by Brian Aker
Second pass of thd cleanup
156
    session			Thread handle
1 by brian
clean slate
157
    level		Severity of warning (note, warning, error ...)
158
    code		Error number
159
    msg			Clear error message
160
*/
161
520.1.22 by Brian Aker
Second pass of thd cleanup
162
void push_warning_printf(Session *session, DRIZZLE_ERROR::enum_warning_level level,
482 by Brian Aker
Remove uint.
163
			 uint32_t code, const char *format, ...)
1 by brian
clean slate
164
{
165
  va_list args;
166
  char    warning[ERRMSGSIZE+20];
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
167
1 by brian
clean slate
168
  va_start(args,format);
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
169
  vsnprintf(warning, sizeof(warning), format, args);
1 by brian
clean slate
170
  va_end(args);
520.1.22 by Brian Aker
Second pass of thd cleanup
171
  push_warning(session, level, code, warning);
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
172
  return;
1 by brian
clean slate
173
}
174
175
176
/*
177
  Send all notes, errors or warnings to the client in a result set
178
179
  SYNOPSIS
180
    mysqld_show_warnings()
520.1.22 by Brian Aker
Second pass of thd cleanup
181
    session			Thread handler
1 by brian
clean slate
182
    levels_to_show	Bitmap for which levels to show
183
184
  DESCRIPTION
185
    Takes into account the current LIMIT
186
187
  RETURN VALUES
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
188
    false ok
189
    true  Error sending data to client
1 by brian
clean slate
190
*/
191
192
const LEX_STRING warning_level_names[]=
193
{
194
  { C_STRING_WITH_LEN("Note") },
195
  { C_STRING_WITH_LEN("Warning") },
196
  { C_STRING_WITH_LEN("Error") },
197
  { C_STRING_WITH_LEN("?") }
198
};
199
1100.3.35 by Padraig O'Sullivan
Updated mysql_show_warnings to take a std::bitset instead of a uint32_t to
200
bool mysqld_show_warnings(Session *session,
201
                          bitset<DRIZZLE_ERROR::NUM_ERRORS> &levels_to_show)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
202
{
1 by brian
clean slate
203
  List<Item> field_list;
204
205
  field_list.push_back(new Item_empty_string("Level", 7));
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
206
  field_list.push_back(new Item_return_int("Code",4, DRIZZLE_TYPE_LONG));
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
207
  field_list.push_back(new Item_empty_string("Message",DRIZZLE_ERRMSG_SIZE));
1 by brian
clean slate
208
971.3.63 by Eric Day
Removed protocol field flags.
209
  if (session->protocol->sendFields(&field_list))
210
    return true;
1 by brian
clean slate
211
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
212
  DRIZZLE_ERROR *err;
846 by Brian Aker
Removing on typedeffed class.
213
  Select_Lex *sel= &session->lex->select_lex;
848 by Brian Aker
typdef class removal (just... use the name of the class).
214
  Select_Lex_Unit *unit= &session->lex->unit;
1 by brian
clean slate
215
  ha_rows idx= 0;
971.3.70 by Eric Day
Fixed style issues found by Jay.
216
  plugin::Protocol *protocol= session->protocol;
1 by brian
clean slate
217
218
  unit->set_limit(sel);
219
520.1.22 by Brian Aker
Second pass of thd cleanup
220
  List_iterator_fast<DRIZZLE_ERROR> it(session->warn_list);
1 by brian
clean slate
221
  while ((err= it++))
222
  {
223
    /* Skip levels that the user is not interested in */
1100.3.35 by Padraig O'Sullivan
Updated mysql_show_warnings to take a std::bitset instead of a uint32_t to
224
    if (! levels_to_show.test(err->level))
1 by brian
clean slate
225
      continue;
226
    if (++idx <= unit->offset_limit_cnt)
227
      continue;
228
    if (idx > unit->select_limit_cnt)
229
      break;
971.3.19 by Eric Day
Finished first pass at Protocol cleanup, still some things to remove but they are a bit more involved.
230
    protocol->prepareForResend();
1 by brian
clean slate
231
    protocol->store(warning_level_names[err->level].str,
1054.2.9 by Monty Taylor
Removed CHARSET_INFO stuff from protocol plugin interface - it makes no sense.
232
		    warning_level_names[err->level].length);
205 by Brian Aker
uint32 -> uin32_t
233
    protocol->store((uint32_t) err->code);
1054.2.9 by Monty Taylor
Removed CHARSET_INFO stuff from protocol plugin interface - it makes no sense.
234
    protocol->store(err->msg, strlen(err->msg));
1 by brian
clean slate
235
    if (protocol->write())
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
236
      return(true);
1 by brian
clean slate
237
  }
836 by Brian Aker
Fixed session call from function to method.
238
  session->my_eof();
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
239
  return(false);
1 by brian
clean slate
240
}