1 /*
2  * Copyright (c) 2015, 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
23  * 02110-1301  USA
24  */
25 
26 #include "sql_data_result.h"
27 #include "ngs/memory.h"
28 
Sql_data_result(Sql_data_context & context)29 xpl::Sql_data_result::Sql_data_result(Sql_data_context &context)
30 : m_field_index(0), m_context(context)
31 {
32 }
33 
34 
disable_binlog()35 void xpl::Sql_data_result::disable_binlog()
36 {
37   // save original value of binary logging
38   query("SET @MYSQLX_OLD_LOG_BIN=@@SQL_LOG_BIN");
39   // disable binary logging
40   query("SET SESSION SQL_LOG_BIN=0;");
41 }
42 
restore_binlog()43 void xpl::Sql_data_result::restore_binlog()
44 {
45   query("SET SESSION SQL_LOG_BIN=@MYSQLX_OLD_LOG_BIN;");
46 }
47 
query(const ngs::PFS_string & query)48 void xpl::Sql_data_result::query(const ngs::PFS_string &query)
49 {
50   m_result_set.clear();
51 
52   m_field_index = 0;
53 
54   ngs::Error_code error = m_context.execute_sql_and_collect_results(query.data(), query.length(), m_field_types, m_result_set, m_result_info);
55 
56   if (error)
57   {
58     throw error;
59   }
60 
61   m_row_index = m_result_set.begin();
62 }
63 
64 
get_next_field(long & value)65 void xpl::Sql_data_result::get_next_field(long &value)
66 {
67   Field_value &field_value = validate_field_index_no_null(MYSQL_TYPE_LONGLONG);
68 
69   value = static_cast<long>(field_value.value.v_long);
70 }
71 
72 
get_next_field(bool & value)73 void xpl::Sql_data_result::get_next_field(bool &value)
74 {
75   Field_value &field_value = validate_field_index_no_null(MYSQL_TYPE_LONGLONG);
76 
77   value = field_value.value.v_long;
78 }
79 
80 
get_next_field(std::string & value)81 void xpl::Sql_data_result::get_next_field(std::string &value)
82 {
83   validate_field_index(MYSQL_TYPE_VARCHAR, MYSQL_TYPE_STRING);
84 
85   Field_value *field_value = get_value();
86 
87   value = "";
88   if (field_value && field_value->is_string)
89     value = *field_value->value.v_string;
90 }
91 
92 
93 /*
94 NOTE: Commented for coverage. Uncomment when needed.
95 
96 void xpl::Sql_data_result::get_next_field(const char * &value)
97 {
98   validate_field_index(MYSQL_TYPE_VARCHAR);
99 
100   Field_value *field_value = get_value();
101 
102   if (field_value && field_value->is_string)
103     value = field_value->value.v_string->c_str();
104   else
105     value = NULL;
106 }
107 */
108 
109 
get_next_field(char * & value)110 void xpl::Sql_data_result::get_next_field(char * &value)
111 {
112   validate_field_index(MYSQL_TYPE_VARCHAR);
113 
114   Field_value *field_value = get_value();
115 
116   if (field_value && field_value->is_string)
117     value = &(*field_value->value.v_string)[0];
118   else
119     value = NULL;
120 }
121 
122 
statement_warn_count()123 long xpl::Sql_data_result::statement_warn_count()
124 {
125   return m_result_info.num_warnings;
126 }
127 
128 
get_value()129 xpl::Sql_data_result::Field_value *xpl::Sql_data_result::get_value()
130 {
131   Callback_command_delegate::Field_value *result = (*m_row_index).fields[m_field_index++];
132 
133   return result;
134 }
135 
136 
next_row()137 bool xpl::Sql_data_result::next_row()
138 {
139   ++m_row_index;
140   m_field_index = 0;
141 
142   return m_result_set.end() != m_row_index;
143 }
144 
145 
validate_field_index_no_null(const enum_field_types field_type)146 xpl::Sql_data_result::Field_value &xpl::Sql_data_result::validate_field_index_no_null(const enum_field_types field_type)
147 {
148   validate_field_index(field_type);
149 
150   Callback_command_delegate::Field_value *result = get_value();
151 
152   if (NULL == result)
153   {
154     throw ngs::Error(ER_DATA_OUT_OF_RANGE, "Null values received");
155   }
156 
157   return *result;
158 }
159 
160 
validate_field_index_common() const161 void xpl::Sql_data_result::validate_field_index_common() const
162 {
163   if (0 == m_result_set.size())
164   {
165     throw ngs::Error(ER_DATA_OUT_OF_RANGE, "Resultset doesn't contain data");
166   }
167 
168   if (m_row_index == m_result_set.end())
169   {
170     throw ngs::Error(ER_DATA_OUT_OF_RANGE, "No more rows in resultset");
171   }
172 
173   if (m_field_index >= (*m_row_index).fields.size())
174   {
175     throw ngs::Error(ER_DATA_OUT_OF_RANGE, "Field index of of range. Request index: %u, last index: %u", (unsigned int)m_field_index, (unsigned int)(*m_row_index).fields.size()- 1);
176   }
177 
178   if (m_field_index >= m_field_types.size())
179   {
180     throw ngs::Error(ER_DATA_OUT_OF_RANGE, "Type field index of of range. Request index: %u, last index: %u", (unsigned int)m_field_index, (unsigned int)m_field_types.size() - 1);
181   }
182 }
183 
184 
validate_field_index(const enum_field_types field_type) const185 void xpl::Sql_data_result::validate_field_index(const enum_field_types field_type) const
186 {
187   validate_field_index_common();
188 
189   if (m_field_types[m_field_index].type != field_type)
190   {
191     throw ngs::Error(ER_DATA_OUT_OF_RANGE, "Invalid column type. Request type: %u, last type: %u", (unsigned int)field_type, (unsigned int)m_field_types[m_field_index].type);
192   }
193 }
194 
195 
validate_field_index(const enum_field_types field_type1,const enum_field_types field_type2) const196 void xpl::Sql_data_result::validate_field_index(const enum_field_types field_type1, const enum_field_types field_type2) const
197 {
198   validate_field_index_common();
199 
200   if (m_field_types[m_field_index].type != field_type1 && m_field_types[m_field_index].type != field_type2)
201   {
202     throw ngs::Error(ER_DATA_OUT_OF_RANGE, "Invalid column type. Request types: %u and %u, last type: %u",
203                      (unsigned int)field_type1, (unsigned int)field_type2, (unsigned int)m_field_types[m_field_index].type);
204   }
205 }
206