1 /*
2    Copyright (c) 2013, 2021, Oracle and/or its affiliates.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "my_global.h"
26 #include "trigger_creation_ctx.h"
27 #include "sql_db.h" // get_default_db_collation()
28 #include "log.h"
29 
30 Trigger_creation_ctx *
create(THD * thd,const LEX_CSTRING & db_name,const LEX_CSTRING & table_name,const LEX_STRING & client_cs_name,const LEX_STRING & connection_cl_name,const LEX_STRING & db_cl_name)31 Trigger_creation_ctx::create(THD *thd,
32                              const LEX_CSTRING &db_name,
33                              const LEX_CSTRING &table_name,
34                              const LEX_STRING &client_cs_name,
35                              const LEX_STRING &connection_cl_name,
36                              const LEX_STRING &db_cl_name)
37 {
38   const CHARSET_INFO *client_cs;
39   const CHARSET_INFO *connection_cl;
40   const CHARSET_INFO *db_cl;
41 
42   bool invalid_creation_ctx= FALSE;
43 
44   if (resolve_charset(client_cs_name.str,
45                       thd->variables.character_set_client,
46                       &client_cs))
47   {
48     sql_print_warning("Trigger for table '%s'.'%s': "
49                       "invalid character_set_client value (%s).",
50                       (const char *) db_name.str,
51                       (const char *) table_name.str,
52                       (const char *) client_cs_name.str);
53 
54     invalid_creation_ctx= TRUE;
55   }
56 
57   if (resolve_collation(connection_cl_name.str,
58                         thd->variables.collation_connection,
59                         &connection_cl))
60   {
61     sql_print_warning("Trigger for table '%s'.'%s': "
62                       "invalid collation_connection value (%s).",
63                       (const char *) db_name.str,
64                       (const char *) table_name.str,
65                       (const char *) connection_cl_name.str);
66 
67     invalid_creation_ctx= TRUE;
68   }
69 
70   if (resolve_collation(db_cl_name.str, NULL, &db_cl))
71   {
72     sql_print_warning("Trigger for table '%s'.'%s': "
73                       "invalid database_collation value (%s).",
74                       (const char *) db_name.str,
75                       (const char *) table_name.str,
76                       (const char *) db_cl_name.str);
77 
78     invalid_creation_ctx= TRUE;
79   }
80 
81   if (invalid_creation_ctx)
82   {
83     push_warning_printf(thd,
84                         Sql_condition::SL_WARNING,
85                         ER_TRG_INVALID_CREATION_CTX,
86                         ER(ER_TRG_INVALID_CREATION_CTX),
87                         (const char *) db_name.str,
88                         (const char *) table_name.str);
89   }
90 
91   /*
92     If we failed to resolve the database collation, load the default one
93     from the disk.
94   */
95 
96   if (!db_cl)
97     db_cl= get_default_db_collation(thd, db_name.str);
98 
99   return new Trigger_creation_ctx(client_cs, connection_cl, db_cl);
100 }
101