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 "notices.h"
27 
28 #include <vector>
29 
30 #include "callback_command_delegate.h"
31 #include "ngs_common/bind.h"
32 #include "ngs_common/protocol_protobuf.h"
33 #include "ngs/protocol_monitor.h"
34 #include "protocol.h"
35 #include "sql_data_context.h"
36 
37 namespace xpl {
38 
39 namespace notices {
40 
41 namespace {
42 
start_warning_row(Callback_command_delegate::Row_data * row_data)43 Callback_command_delegate::Row_data *start_warning_row(
44     Callback_command_delegate::Row_data *row_data) {
45   row_data->clear();
46   return row_data;
47 }
48 
get_warning_level(const std::string & level)49 inline Mysqlx::Notice::Warning::Level get_warning_level(
50     const std::string &level) {
51   static const char *const ERROR_STRING = "Error";
52   static const char *const WARNING_STRING = "Warning";
53   if (level == WARNING_STRING) return Mysqlx::Notice::Warning::WARNING;
54   if (level == ERROR_STRING) return Mysqlx::Notice::Warning::ERROR;
55   return Mysqlx::Notice::Warning::NOTE;
56 }
57 
end_warning_row(Callback_command_delegate::Row_data * row,ngs::Protocol_encoder & proto,bool skip_single_error,std::string & last_error,unsigned int & num_errors)58 bool end_warning_row(Callback_command_delegate::Row_data *row,
59                      ngs::Protocol_encoder &proto, bool skip_single_error,
60                      std::string &last_error, unsigned int &num_errors) {
61   typedef Mysqlx::Notice::Warning Warning;
62 
63   if (!last_error.empty()) {
64     proto.send_local_warning(last_error);
65     last_error.clear();
66   }
67 
68   std::vector<Callback_command_delegate::Field_value *> &fields = row->fields;
69   if (fields.size() != 3) return false;
70 
71   const Warning::Level level = get_warning_level(*fields[0]->value.v_string);
72 
73   Warning warning;
74   warning.set_level(level);
75   warning.set_code(
76       static_cast<google::protobuf::uint32>(fields[1]->value.v_long));
77   warning.set_msg(*fields[2]->value.v_string);
78 
79   std::string data;
80   warning.SerializeToString(&data);
81 
82   if (level == Warning::ERROR) {
83     ++num_errors;
84     if (skip_single_error && (num_errors <= 1)) {
85       last_error = data;
86       return true;
87     }
88   }
89 
90   proto.send_local_warning(data);
91   return true;
92 }
93 
send_local_notice(const Mysqlx::Notice::SessionStateChanged & notice,ngs::Protocol_encoder * proto)94 inline void send_local_notice(const Mysqlx::Notice::SessionStateChanged &notice,
95                               ngs::Protocol_encoder *proto) {
96   std::string data;
97   notice.SerializeToString(&data);
98   proto->send_local_notice(
99       ngs::Protocol_encoder::k_notice_session_state_changed, data);
100 }
101 }  // namespace
102 
send_warnings(Sql_data_context & da,ngs::Protocol_encoder & proto,bool skip_single_error)103 ngs::Error_code send_warnings(Sql_data_context &da,
104                               ngs::Protocol_encoder &proto,
105                               bool skip_single_error) {
106   Callback_command_delegate::Row_data row_data;
107   Sql_data_context::Result_info winfo;
108   static std::string q = "SHOW WARNINGS";
109   std::string last_error;
110   unsigned int num_errors = 0u;
111 
112   // send warnings as notices
113   return da.execute_sql_and_process_results(
114       q.data(), q.length(), ngs::bind(start_warning_row, &row_data),
115       ngs::bind(end_warning_row, ngs::placeholders::_1, ngs::ref(proto),
116                 skip_single_error, last_error, num_errors),
117       winfo);
118 }
119 
send_account_expired(ngs::Protocol_encoder & proto)120 ngs::Error_code send_account_expired(ngs::Protocol_encoder &proto) {
121   Mysqlx::Notice::SessionStateChanged change;
122   change.set_param(Mysqlx::Notice::SessionStateChanged::ACCOUNT_EXPIRED);
123   send_local_notice(change, &proto);
124   return ngs::Success();
125 }
126 
send_generated_insert_id(ngs::Protocol_encoder & proto,uint64_t i)127 ngs::Error_code send_generated_insert_id(ngs::Protocol_encoder &proto,
128                                          uint64_t i) {
129   Mysqlx::Notice::SessionStateChanged change;
130   change.set_param(Mysqlx::Notice::SessionStateChanged::GENERATED_INSERT_ID);
131   change.mutable_value()->set_type(Mysqlx::Datatypes::Scalar::V_UINT);
132   change.mutable_value()->set_v_unsigned_int(i);
133   send_local_notice(change, &proto);
134   return ngs::Success();
135 }
136 
send_rows_affected(ngs::Protocol_encoder & proto,uint64_t i)137 ngs::Error_code send_rows_affected(ngs::Protocol_encoder &proto, uint64_t i) {
138   proto.send_rows_affected(i);
139   return ngs::Success();
140 }
141 
send_client_id(ngs::Protocol_encoder & proto,uint64_t i)142 ngs::Error_code send_client_id(ngs::Protocol_encoder &proto, uint64_t i) {
143   Mysqlx::Notice::SessionStateChanged change;
144   change.set_param(Mysqlx::Notice::SessionStateChanged::CLIENT_ID_ASSIGNED);
145   change.mutable_value()->set_type(Mysqlx::Datatypes::Scalar::V_UINT);
146   change.mutable_value()->set_v_unsigned_int(i);
147   send_local_notice(change, &proto);
148   return ngs::Success();
149 }
150 
send_message(ngs::Protocol_encoder & proto,const std::string & message)151 ngs::Error_code send_message(ngs::Protocol_encoder &proto,
152                              const std::string &message) {
153   Mysqlx::Notice::SessionStateChanged change;
154   change.set_param(Mysqlx::Notice::SessionStateChanged::PRODUCED_MESSAGE);
155   change.mutable_value()->set_type(Mysqlx::Datatypes::Scalar::V_STRING);
156   change.mutable_value()->mutable_v_string()->set_value(message);
157   send_local_notice(change, &proto);
158   return ngs::Success();
159 }
160 
161 }  // namespace notices
162 }  // namespace xpl
163