1 /* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef DERROR_INCLUDED
24 #define DERROR_INCLUDED
25 
26 #include <stddef.h>
27 #include <sys/types.h>
28 
29 #include "my_inttypes.h"
30 
31 #ifdef EXTRA_CODE_FOR_UNIT_TESTING
32 #include "mysqld_error.h"
33 #endif
34 
35 /**
36   A record describing an error message.
37 */
38 struct {
39   const char *name;        ///< MySQL error symbol ("ER_STARTUP")
40   uint mysql_errno;        ///< MySQL error code (consecutive within sections)
41   const char *text;        ///< MySQL error message
42   const char *odbc_state;  ///< SQL state
43   const char *jdbc_state;  ///< JBDC state
44   uint error_index;        ///< consecutive. 0 for obsolete.
45 } typedef server_error;
46 
47 class THD;
48 struct CHARSET_INFO;
49 
50 /**
51   Character set of the buildin error messages loaded from errmsg.sys.
52 */
53 extern CHARSET_INFO *error_message_charset_info;
54 
55 class MY_LOCALE_ERRMSGS {
56   const char *language;
57 
58  public:
59   const char **errmsgs;
60 
MY_LOCALE_ERRMSGS(const char * lang_par)61   MY_LOCALE_ERRMSGS(const char *lang_par)
62       : language(lang_par), errmsgs(nullptr) {}
63 
64   /** Return error message string for a given error number. */
65   const char *lookup(int mysql_errno);
66 
67 #ifdef EXTRA_CODE_FOR_UNIT_TESTING
replace_msg(int mysql_errno,const char * new_msg)68   bool replace_msg(int mysql_errno, const char *new_msg) {
69     int offset = 0;  // Position where the current section starts in the array.
70     int num_sections =
71         sizeof(errmsg_section_start) / sizeof(errmsg_section_start[0]);
72     for (int i = 0; i < num_sections; i++) {
73       if (mysql_errno < (errmsg_section_start[i] + errmsg_section_size[i])) {
74         errmsgs[mysql_errno - errmsg_section_start[i] + offset] = new_msg;
75         return false;
76       }
77       offset += errmsg_section_size[i];
78     }
79     return true;
80   }
81 #endif
82 
83   /** Has the error message file been sucessfully loaded? */
is_loaded()84   bool is_loaded() const { return errmsgs != nullptr; }
85 
86   /** Deallocate error message strings. */
87   void destroy();
88 
89   /** Read the error message file and initialize strings. */
90   bool read_texts();
91 
92   /** What language is this error message set for? */
get_language()93   const char *get_language() const { return language; }
94 };
95 
96 #ifdef CHECK_ERRMSG_FORMAT
97 // The number and type of arguments to error messages is
98 // now checked at compile time.
99 #include "mysqld_errmsg.h"
100 #define ER_DEFAULT(X) X##_MSG
101 #define ER_THD(T, X) X##_MSG
102 #else
103 const char *ER_DEFAULT(int mysql_errno);
104 const char *ER_THD(const THD *thd, int mysql_errno);
105 #endif  // CHECK_ERRMSG_FORMAT
106 
107 // Use these in place of ER_DEFAULT/ER_THD when the error number is not known at
108 // compile time. Avoid using these if at all possible.
109 const char *ER_DEFAULT_NONCONST(int mysql_errno);
110 const char *ER_THD_NONCONST(const THD *thd, int mysql_errno);
111 
112 const char *error_message_for_error_log(int mysql_errno);
113 const char *error_message_for_client(int mysql_errno);
114 
115 const char *mysql_errno_to_symbol(int mysql_errno);
116 int mysql_symbol_to_errno(const char *error_symbol);
117 int mysql_errno_to_builtin(uint mysql_errno);
118 
119 int errmsgs_reload(THD *thd);
120 
121 /**
122   Read the error message file, initialize and register error messages
123   for all languages.
124 
125   @retval true if initialization failed, false otherwise.
126 */
127 bool init_errmessage();
128 
129 /**
130   Unregister error messages for all languages.
131 */
132 void deinit_errmessage();
133 
134 #endif /* DERROR_INCLUDED */
135