~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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA */
1 by brian
clean slate
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
44
#include "config.h"
1241.9.62 by Monty Taylor
Removed plugin/myisam/myisam.h from session.h
45
1502.3.1 by iwamatsu at nigauri
Add cstdio include to files needing it. Fixes the build on some debian
46
#include <cstdio>
1241.9.62 by Monty Taylor
Removed plugin/myisam/myisam.h from session.h
47
#include <stdarg.h>
48
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.
49
#include <drizzled/session.h>
50
#include <drizzled/sql_base.h>
675 by Brian Aker
Cleanup around item includes.
51
#include <drizzled/item/empty_string.h>
642.1.17 by Lee
header file clean up
52
#include <drizzled/item/return_int.h>
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
53
#include <drizzled/plugin/client.h>
1 by brian
clean slate
54
1130.3.6 by Monty Taylor
Removed use of using namespace std; from a header.
55
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
56
57
namespace drizzled
58
{
971.3.65 by Eric Day
Namespace cleanup for Protocol and Listen.
59
1 by brian
clean slate
60
/*
61
  Store a new message in an error object
62
63
  This is used to in group_concat() to register how many warnings we actually
64
  got after the query has been executed.
65
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
66
void DRIZZLE_ERROR::set_msg(Session *session, const char *msg_arg)
1 by brian
clean slate
67
{
1487 by Brian Aker
More updates for memory::Root
68
  msg= session->warn_root.strdup_root(msg_arg);
1 by brian
clean slate
69
}
70
71
/*
72
  Reset all warnings for the thread
73
74
  SYNOPSIS
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
75
    drizzle_reset_errors()
520.1.22 by Brian Aker
Second pass of thd cleanup
76
    session			Thread handle
1 by brian
clean slate
77
    force               Reset warnings even if it has been done before
78
79
  IMPLEMENTATION
80
    Don't reset warnings if this has already been called for this query.
81
    This may happen if one gets a warning during the parsing stage,
82
    in which case push_warnings() has already called this function.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
83
*/
1 by brian
clean slate
84
520.1.22 by Brian Aker
Second pass of thd cleanup
85
void drizzle_reset_errors(Session *session, bool force)
1 by brian
clean slate
86
{
1273.1.1 by Jay Pipes
* Changes Session::warn_id to Session::warn_query_id
87
  if (session->getQueryId() != session->getWarningQueryId() || force)
1 by brian
clean slate
88
  {
1273.1.1 by Jay Pipes
* Changes Session::warn_id to Session::warn_query_id
89
    session->setWarningQueryId(session->getQueryId());
1487 by Brian Aker
More updates for memory::Root
90
    session->warn_root.free_root(MYF(0));
520.1.22 by Brian Aker
Second pass of thd cleanup
91
    memset(session->warn_count, 0, sizeof(session->warn_count));
1 by brian
clean slate
92
    if (force)
520.1.22 by Brian Aker
Second pass of thd cleanup
93
      session->total_warn_count= 0;
94
    session->warn_list.empty();
95
    session->row_count= 1; // by default point to row 1
1 by brian
clean slate
96
  }
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
97
  return;
1 by brian
clean slate
98
}
99
100
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
101
/*
1 by brian
clean slate
102
  Push the warning/error to error list if there is still room in the list
103
104
  SYNOPSIS
105
    push_warning()
520.1.22 by Brian Aker
Second pass of thd cleanup
106
    session			Thread handle
1 by brian
clean slate
107
    level		Severity of warning (note, warning, error ...)
108
    code		Error number
109
    msg			Clear error message
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
110
1 by brian
clean slate
111
  RETURN
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
112
    pointer on DRIZZLE_ERROR object
1 by brian
clean slate
113
*/
114
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
115
DRIZZLE_ERROR *push_warning(Session *session, DRIZZLE_ERROR::enum_warning_level level,
2087.3.1 by Brian Aker
Entire convert over to time_t.
116
                            drizzled::error_t code, const char *msg)
1 by brian
clean slate
117
{
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
118
  DRIZZLE_ERROR *err= 0;
1 by brian
clean slate
119
2087.3.1 by Brian Aker
Entire convert over to time_t.
120
  if (level == DRIZZLE_ERROR::WARN_LEVEL_NOTE && !(session->options & OPTION_SQL_NOTES))
121
  {
122
    return NULL;
123
  }
1 by brian
clean slate
124
1273.1.1 by Jay Pipes
* Changes Session::warn_id to Session::warn_query_id
125
  if (session->getQueryId() != session->getWarningQueryId())
520.1.22 by Brian Aker
Second pass of thd cleanup
126
    drizzle_reset_errors(session, 0);
127
  session->got_warning= 1;
1 by brian
clean slate
128
129
  /* Abort if we are using strict mode and we are not using IGNORE */
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
130
  if ((int) level >= (int) DRIZZLE_ERROR::WARN_LEVEL_WARN &&
2114.5.2 by Brian Aker
Just remove second ecapsulated method call.
131
      session->abortOnWarning())
1 by brian
clean slate
132
  {
133
    /* Avoid my_message() calling push_warning */
520.1.22 by Brian Aker
Second pass of thd cleanup
134
    bool no_warnings_for_error= session->no_warnings_for_error;
135
136
    session->no_warnings_for_error= 1;
137
1910.2.8 by Brian Aker
Enapsulate Kill.
138
    session->setKilled(Session::KILL_BAD_DATA);
1 by brian
clean slate
139
    my_message(code, msg, MYF(0));
140
520.1.22 by Brian Aker
Second pass of thd cleanup
141
    session->no_warnings_for_error= no_warnings_for_error;
1 by brian
clean slate
142
    /* Store error in error list (as my_message() didn't do it) */
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
143
    level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
1 by brian
clean slate
144
  }
145
520.1.22 by Brian Aker
Second pass of thd cleanup
146
  if (session->handle_error(code, msg, level))
1019.1.6 by Brian Aker
A number of random cleanups.
147
    return NULL;
1 by brian
clean slate
148
520.1.22 by Brian Aker
Second pass of thd cleanup
149
  if (session->warn_list.elements < session->variables.max_error_count)
1 by brian
clean slate
150
  {
151
    /* 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
152
    if ((err= new (&session->warn_root) DRIZZLE_ERROR(session, code, level, msg)))
2087.3.1 by Brian Aker
Entire convert over to time_t.
153
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
154
      session->warn_list.push_back(err, &session->warn_root);
2087.3.1 by Brian Aker
Entire convert over to time_t.
155
    }
1 by brian
clean slate
156
  }
895 by Brian Aker
Completion (?) of uint conversion.
157
  session->warn_count[(uint32_t) level]++;
520.1.22 by Brian Aker
Second pass of thd cleanup
158
  session->total_warn_count++;
2087.3.1 by Brian Aker
Entire convert over to time_t.
159
160
  return err;
1 by brian
clean slate
161
}
162
163
/*
164
  Push the warning/error to error list if there is still room in the list
165
166
  SYNOPSIS
167
    push_warning_printf()
520.1.22 by Brian Aker
Second pass of thd cleanup
168
    session			Thread handle
1 by brian
clean slate
169
    level		Severity of warning (note, warning, error ...)
170
    code		Error number
171
    msg			Clear error message
172
*/
173
520.1.22 by Brian Aker
Second pass of thd cleanup
174
void push_warning_printf(Session *session, DRIZZLE_ERROR::enum_warning_level level,
2087.3.1 by Brian Aker
Entire convert over to time_t.
175
			 drizzled::error_t code, const char *format, ...)
1 by brian
clean slate
176
{
177
  va_list args;
178
  char    warning[ERRMSGSIZE+20];
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
179
1 by brian
clean slate
180
  va_start(args,format);
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
181
  vsnprintf(warning, sizeof(warning), format, args);
1 by brian
clean slate
182
  va_end(args);
520.1.22 by Brian Aker
Second pass of thd cleanup
183
  push_warning(session, level, code, warning);
1 by brian
clean slate
184
}
185
186
187
/*
188
  Send all notes, errors or warnings to the client in a result set
189
190
  SYNOPSIS
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
191
    show_warnings()
520.1.22 by Brian Aker
Second pass of thd cleanup
192
    session			Thread handler
1 by brian
clean slate
193
    levels_to_show	Bitmap for which levels to show
194
195
  DESCRIPTION
196
    Takes into account the current LIMIT
197
198
  RETURN VALUES
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
199
    false ok
200
    true  Error sending data to client
1 by brian
clean slate
201
*/
202
203
const LEX_STRING warning_level_names[]=
204
{
205
  { C_STRING_WITH_LEN("Note") },
206
  { C_STRING_WITH_LEN("Warning") },
207
  { C_STRING_WITH_LEN("Error") },
208
  { C_STRING_WITH_LEN("?") }
209
};
210
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
211
bool show_warnings(Session *session,
212
                   bitset<DRIZZLE_ERROR::NUM_ERRORS> &levels_to_show)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
213
{
1 by brian
clean slate
214
  List<Item> field_list;
215
216
  field_list.push_back(new Item_empty_string("Level", 7));
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
217
  field_list.push_back(new Item_return_int("Code",4, DRIZZLE_TYPE_LONG));
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
218
  field_list.push_back(new Item_empty_string("Message",DRIZZLE_ERRMSG_SIZE));
1 by brian
clean slate
219
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
220
  if (session->getClient()->sendFields(&field_list))
971.3.63 by Eric Day
Removed protocol field flags.
221
    return true;
1 by brian
clean slate
222
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
223
  DRIZZLE_ERROR *err;
846 by Brian Aker
Removing on typedeffed class.
224
  Select_Lex *sel= &session->lex->select_lex;
848 by Brian Aker
typdef class removal (just... use the name of the class).
225
  Select_Lex_Unit *unit= &session->lex->unit;
1 by brian
clean slate
226
  ha_rows idx= 0;
227
228
  unit->set_limit(sel);
229
520.1.22 by Brian Aker
Second pass of thd cleanup
230
  List_iterator_fast<DRIZZLE_ERROR> it(session->warn_list);
1 by brian
clean slate
231
  while ((err= it++))
232
  {
233
    /* 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
234
    if (! levels_to_show.test(err->level))
1 by brian
clean slate
235
      continue;
236
    if (++idx <= unit->offset_limit_cnt)
237
      continue;
238
    if (idx > unit->select_limit_cnt)
239
      break;
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
240
    session->getClient()->store(warning_level_names[err->level].str,
241
                                warning_level_names[err->level].length);
242
    session->getClient()->store((uint32_t) err->code);
243
    session->getClient()->store(err->msg, strlen(err->msg));
244
    if (session->getClient()->flush())
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
245
      return(true);
1 by brian
clean slate
246
  }
836 by Brian Aker
Fixed session call from function to method.
247
  session->my_eof();
51.1.51 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
248
  return(false);
1 by brian
clean slate
249
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
250
251
} /* namespace drizzled */