1 /*
2    Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
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 "show_variable_query_extractor.h"
26 #include "instance_callback.h"
27 
28 using namespace Mysql::Tools::Base;
29 using std::string;
30 using std::vector;
31 
Show_variable_query_extractor()32 Show_variable_query_extractor::Show_variable_query_extractor()
33   : m_exists(false)
34 {}
35 
extract_variable(const Mysql_query_runner::Row & result_row)36 int64 Show_variable_query_extractor::extract_variable(
37   const Mysql_query_runner::Row& result_row)
38 {
39   this->m_extracted_variable= result_row[0];
40   this->m_exists= true;
41   Mysql_query_runner::cleanup_result(result_row);
42   return 0;
43 }
44 
get_variable_value(Mysql_query_runner * query_runner_to_copy,string variable,string & value,bool & exists)45 int64 Show_variable_query_extractor::get_variable_value(
46   Mysql_query_runner* query_runner_to_copy,
47   string variable, string& value, bool& exists)
48 {
49   Show_variable_query_extractor extractor;
50   Mysql_query_runner query_runner_to_use(*query_runner_to_copy);
51 
52   Instance_callback<int64, const Mysql_query_runner::Row&,
53                     Show_variable_query_extractor>
54     result_cb(&extractor, &Show_variable_query_extractor::extract_variable);
55 
56   query_runner_to_use.add_result_callback(&result_cb);
57 
58   /*
59     Note: Because MySQL uses the C escape syntax in strings (for example, '\n'
60     to represent newline), you must double any '\' that you use in your LIKE
61     strings. For example, to search for '\n', specify it as '\\n'.To search for
62     '\', specify it as '\\\\' (the backslashes are stripped once by the parser
63     and another time when the pattern match is done, leaving a single backslash
64     to be matched).
65 
66     Example: "t\1" = > "t\\\\1"
67   */
68   string quoted_variable;
69   for (size_t i= 0; i < variable.size(); i++)
70   {
71     if (variable[i] == '\\')
72     {
73       quoted_variable.append(3, '\\');
74     }
75     else if (variable[i] == '\'' || variable[i] == '_' || variable[i] == '%')
76     {
77       quoted_variable+= '\\';
78     }
79     quoted_variable+= variable[i];
80   }
81 
82   query_runner_to_use.run_query("SELECT @@global." + quoted_variable);
83 
84   value= extractor.m_extracted_variable;
85   exists= extractor.m_exists;
86   return 0;
87 }
88