You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
7.7 KiB

23 years ago
  1. /* Copyright (C) 1995-2002 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. /**********************************************************************
  14. This file contains the implementation of error and warnings related
  15. - Whenever an error or warning occurred, it pushes it to a warning list
  16. that the user can retrieve with SHOW WARNINGS or SHOW ERRORS.
  17. - For each statement, we return the number of warnings generated from this
  18. command. Note that this can be different from @@warning_count as
  19. we reset the warning list only for questions that uses a table.
  20. This is done to allow on to do:
  21. INSERT ...;
  22. SELECT @@warning_count;
  23. SHOW WARNINGS;
  24. (If we would reset after each command, we could not retrieve the number
  25. of warnings)
  26. - When client requests the information using SHOW command, then
  27. server processes from this list and returns back in the form of
  28. resultset.
  29. Supported syntaxes:
  30. SHOW [COUNT(*)] ERRORS [LIMIT [offset,] rows]
  31. SHOW [COUNT(*)] WARNINGS [LIMIT [offset,] rows]
  32. SELECT @@warning_count, @@error_count;
  33. ***********************************************************************/
  34. #include "mysql_priv.h"
  35. #include "sp_rcontext.h"
  36. /*
  37. Store a new message in an error object
  38. This is used to in group_concat() to register how many warnings we actually
  39. got after the query has been executed.
  40. */
  41. void MYSQL_ERROR::set_msg(THD *thd, const char *msg_arg)
  42. {
  43. msg= strdup_root(&thd->warn_root, msg_arg);
  44. }
  45. /*
  46. Reset all warnings for the thread
  47. SYNOPSIS
  48. mysql_reset_errors()
  49. thd Thread handle
  50. force Reset warnings even if it has been done before
  51. IMPLEMENTATION
  52. Don't reset warnings if this has already been called for this query.
  53. This may happen if one gets a warning during the parsing stage,
  54. in which case push_warnings() has already called this function.
  55. */
  56. void mysql_reset_errors(THD *thd, bool force)
  57. {
  58. DBUG_ENTER("mysql_reset_errors");
  59. if (thd->query_id != thd->warn_id || force)
  60. {
  61. thd->warn_id= thd->query_id;
  62. free_root(&thd->warn_root,MYF(0));
  63. bzero((char*) thd->warn_count, sizeof(thd->warn_count));
  64. if (force)
  65. thd->total_warn_count= 0;
  66. thd->warn_list.empty();
  67. thd->row_count= 1; // by default point to row 1
  68. }
  69. DBUG_VOID_RETURN;
  70. }
  71. /*
  72. Push the warning/error to error list if there is still room in the list
  73. SYNOPSIS
  74. push_warning()
  75. thd Thread handle
  76. level Severity of warning (note, warning, error ...)
  77. code Error number
  78. msg Clear error message
  79. RETURN
  80. pointer on MYSQL_ERROR object
  81. */
  82. MYSQL_ERROR *push_warning(THD *thd, MYSQL_ERROR::enum_warning_level level,
  83. uint code, const char *msg)
  84. {
  85. MYSQL_ERROR *err= 0;
  86. DBUG_ENTER("push_warning");
  87. DBUG_PRINT("enter", ("code: %d, msg: %s", code, msg));
  88. if (level == MYSQL_ERROR::WARN_LEVEL_NOTE &&
  89. !(thd->options & OPTION_SQL_NOTES))
  90. DBUG_RETURN(0);
  91. if (thd->query_id != thd->warn_id && !thd->spcont)
  92. mysql_reset_errors(thd, 0);
  93. thd->got_warning= 1;
  94. /* Abort if we are using strict mode and we are not using IGNORE */
  95. if ((int) level >= (int) MYSQL_ERROR::WARN_LEVEL_WARN &&
  96. thd->really_abort_on_warning())
  97. {
  98. /* Avoid my_message() calling push_warning */
  99. bool no_warnings_for_error= thd->no_warnings_for_error;
  100. sp_rcontext *spcont= thd->spcont;
  101. thd->no_warnings_for_error= 1;
  102. thd->spcont= 0;
  103. thd->killed= THD::KILL_BAD_DATA;
  104. my_message(code, msg, MYF(0));
  105. thd->spcont= spcont;
  106. thd->no_warnings_for_error= no_warnings_for_error;
  107. /* Store error in error list (as my_message() didn't do it) */
  108. level= MYSQL_ERROR::WARN_LEVEL_ERROR;
  109. }
  110. if (thd->spcont &&
  111. thd->spcont->find_handler(code,
  112. ((int) level >=
  113. (int) MYSQL_ERROR::WARN_LEVEL_WARN &&
  114. thd->really_abort_on_warning()) ?
  115. MYSQL_ERROR::WARN_LEVEL_ERROR : level))
  116. {
  117. if (! thd->spcont->found_handler_here())
  118. thd->net.report_error= 1; /* Make "select" abort correctly */
  119. DBUG_RETURN(NULL);
  120. }
  121. query_cache_abort(&thd->net);
  122. if (thd->warn_list.elements < thd->variables.max_error_count)
  123. {
  124. /*
  125. The following code is here to change the allocation to not
  126. use the thd->mem_root, which is freed after each query
  127. */
  128. MEM_ROOT *old_root= thd->mem_root;
  129. thd->mem_root= &thd->warn_root;
  130. if ((err= new MYSQL_ERROR(thd, code, level, msg)))
  131. thd->warn_list.push_back(err);
  132. thd->mem_root= old_root;
  133. }
  134. thd->warn_count[(uint) level]++;
  135. thd->total_warn_count++;
  136. DBUG_RETURN(err);
  137. }
  138. /*
  139. Push the warning/error to error list if there is still room in the list
  140. SYNOPSIS
  141. push_warning_printf()
  142. thd Thread handle
  143. level Severity of warning (note, warning, error ...)
  144. code Error number
  145. msg Clear error message
  146. */
  147. void push_warning_printf(THD *thd, MYSQL_ERROR::enum_warning_level level,
  148. uint code, const char *format, ...)
  149. {
  150. va_list args;
  151. char warning[ERRMSGSIZE+20];
  152. DBUG_ENTER("push_warning_printf");
  153. DBUG_PRINT("enter",("warning: %u", code));
  154. va_start(args,format);
  155. my_vsnprintf(warning, sizeof(warning), format, args);
  156. va_end(args);
  157. push_warning(thd, level, code, warning);
  158. DBUG_VOID_RETURN;
  159. }
  160. /*
  161. Send all notes, errors or warnings to the client in a result set
  162. SYNOPSIS
  163. mysqld_show_warnings()
  164. thd Thread handler
  165. levels_to_show Bitmap for which levels to show
  166. DESCRIPTION
  167. Takes into account the current LIMIT
  168. RETURN VALUES
  169. FALSE ok
  170. TRUE Error sending data to client
  171. */
  172. LEX_STRING warning_level_names[]=
  173. {
  174. {(char*) STRING_WITH_LEN("Note")},
  175. {(char*) STRING_WITH_LEN("Warning")},
  176. {(char*) STRING_WITH_LEN("Error")},
  177. {(char*) STRING_WITH_LEN("?")}
  178. };
  179. bool mysqld_show_warnings(THD *thd, ulong levels_to_show)
  180. {
  181. List<Item> field_list;
  182. DBUG_ENTER("mysqld_show_warnings");
  183. field_list.push_back(new Item_empty_string("Level", 7));
  184. field_list.push_back(new Item_return_int("Code",4, MYSQL_TYPE_LONG));
  185. field_list.push_back(new Item_empty_string("Message",MYSQL_ERRMSG_SIZE));
  186. if (thd->protocol->send_fields(&field_list,
  187. Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
  188. DBUG_RETURN(TRUE);
  189. MYSQL_ERROR *err;
  190. SELECT_LEX *sel= &thd->lex->select_lex;
  191. SELECT_LEX_UNIT *unit= &thd->lex->unit;
  192. ha_rows idx= 0;
  193. Protocol *protocol=thd->protocol;
  194. unit->set_limit(sel);
  195. List_iterator_fast<MYSQL_ERROR> it(thd->warn_list);
  196. while ((err= it++))
  197. {
  198. /* Skip levels that the user is not interested in */
  199. if (!(levels_to_show & ((ulong) 1 << err->level)))
  200. continue;
  201. if (++idx <= unit->offset_limit_cnt)
  202. continue;
  203. if (idx > unit->select_limit_cnt)
  204. break;
  205. protocol->prepare_for_resend();
  206. protocol->store(warning_level_names[err->level].str,
  207. warning_level_names[err->level].length, system_charset_info);
  208. protocol->store((uint32) err->code);
  209. protocol->store(err->msg, strlen(err->msg), system_charset_info);
  210. if (protocol->write())
  211. DBUG_RETURN(TRUE);
  212. }
  213. send_eof(thd);
  214. DBUG_RETURN(FALSE);
  215. }