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 "crud_cmd_handler.h"
27 #include "admin_cmd_handler.h"
28 
29 #include "xpl_log.h"
30 #include "ngs/mysqlx/getter_any.h"
31 #include "expr_generator.h"
32 #include "sql_data_context.h"
33 #include "xpl_server.h"
34 #include "xpl_session.h"
35 #include "xpl_dispatcher.h"
36 #include "xpl_error.h"
37 #include "ngs_common/protocol_protobuf.h"
38 #include "notices.h"
39 #include "expect.h"
40 
41 
42 namespace
43 {
44 
45 class Stmt
46 {
47 public:
execute(xpl::Sql_data_context & da,ngs::Protocol_encoder & proto,const bool show_warnings,const bool compact_metadata,const std::string & query,const::google::protobuf::RepeatedPtrField<::Mysqlx::Datatypes::Any> & args)48   ngs::Error_code execute(xpl::Sql_data_context &da, ngs::Protocol_encoder &proto, const bool show_warnings,
49                           const bool compact_metadata, const std::string &query,
50                           const ::google::protobuf::RepeatedPtrField< ::Mysqlx::Datatypes::Any > &args)
51   {
52     const int args_size = args.size();
53 
54     if (0 == args_size)
55       return execute(da, proto, show_warnings, compact_metadata, query.data(), query.length());
56 
57     m_qb.clear();
58     m_qb.put(query);
59 
60     try
61     {
62       for (int i = 0; i < args_size; ++i)
63       {
64         ngs::Getter_any::put_scalar_value_to_functor(args.Get(i), *this);
65       }
66     }
67     catch (const ngs::Error_code &error)
68     {
69       return error;
70     }
71 
72     return execute(da, proto, show_warnings, compact_metadata, m_qb.get().data(), m_qb.get().length());
73   }
74 
execute(xpl::Sql_data_context & da,ngs::Protocol_encoder & proto,const bool show_warnings,const bool compact_metadata,const char * query,std::size_t query_len)75   ngs::Error_code execute(xpl::Sql_data_context &da, ngs::Protocol_encoder &proto, const bool show_warnings, const bool compact_metadata,
76       const char* query, std::size_t query_len)
77   {
78     xpl::Sql_data_context::Result_info info;
79     ngs::Error_code error = da.execute_sql_and_stream_results(query, query_len, compact_metadata, info);
80 
81     if (!error)
82     {
83       if (info.num_warnings > 0 && show_warnings)
84         xpl::notices::send_warnings(da, proto);
85       xpl::notices::send_rows_affected(proto, info.affected_rows);
86       if (info.last_insert_id > 0)
87         xpl::notices::send_generated_insert_id(proto, info.last_insert_id);
88       if (!info.message.empty())
89         xpl::notices::send_message(proto, info.message);
90       proto.send_exec_ok();
91     }
92     else
93     {
94       if (show_warnings)
95         xpl::notices::send_warnings(da, proto, true);
96     }
97 
98     return error;
99   }
100 
operator ()()101   void operator() ()
102   {
103     static const char *value_null = "NULL";
104 
105     m_qb.format() % xpl::Query_formatter::No_escape<const char*>(value_null);
106   }
107 
108   template <typename Value_type>
operator ()(const Value_type & value)109   void operator() (const Value_type &value)
110   {
111     m_qb.format() % value;
112   }
113 
114 private:
115   xpl::Query_string_builder m_qb;
116 };
117 
118 
on_stmt_execute(xpl::Session & session,const Mysqlx::Sql::StmtExecute & msg)119 ngs::Error_code on_stmt_execute(xpl::Session &session, const Mysqlx::Sql::StmtExecute &msg)
120 {
121   log_debug("%s: %s", session.client().client_id(), msg.stmt().c_str());
122 
123   if (msg.namespace_() == "sql" || !msg.has_namespace_())
124   {
125     session.update_status<&xpl::Common_status_variables::m_stmt_execute_sql>();
126     return Stmt().execute(session.data_context(), session.proto(), session.options().get_send_warnings(),
127                           msg.compact_metadata(), msg.stmt(), msg.args());
128   }
129 
130   if (msg.namespace_() == "xplugin")
131   {
132     session.update_status<&xpl::Common_status_variables::m_stmt_execute_xplugin>();
133     if (session.options().get_send_xplugin_deprecation())
134     {
135       xpl::notices::send_message(session.proto(), "Namespace 'xplugin' is deprecated, please use 'mysqlx' instead");
136       session.options().set_send_xplugin_deprecation(false);
137     }
138     xpl::Admin_command_arguments_list args(msg.args());
139     return xpl::Admin_command_handler(session).execute(msg.namespace_(), msg.stmt(), args);
140   }
141 
142   if (msg.namespace_() == "mysqlx")
143   {
144     session.update_status<&xpl::Common_status_variables::m_stmt_execute_mysqlx>();
145     xpl::Admin_command_arguments_object args(msg.args());
146     return xpl::Admin_command_handler(session).execute(msg.namespace_(), msg.stmt(), args);
147   }
148 
149   return ngs::Error(ER_X_INVALID_NAMESPACE, "Unknown namespace %s", msg.namespace_().c_str());
150 }
151 
152 
on_expect_open(xpl::Session & session,xpl::Expectation_stack & expect,const Mysqlx::Expect::Open & msg)153 ngs::Error_code on_expect_open(xpl::Session &session, xpl::Expectation_stack &expect, const Mysqlx::Expect::Open &msg)
154 {
155   session.update_status<&xpl::Common_status_variables::m_expect_open>();
156 
157   ngs::Error_code error = expect.open(msg);
158   if (!error)
159     session.proto().send_ok();
160   return error;
161 }
162 
163 
on_expect_close(xpl::Session & session,xpl::Expectation_stack & expect,const Mysqlx::Expect::Close & msg)164 ngs::Error_code on_expect_close(xpl::Session &session, xpl::Expectation_stack &expect, const Mysqlx::Expect::Close &msg)
165 {
166   session.update_status<&xpl::Common_status_variables::m_expect_close>();
167 
168   ngs::Error_code error = expect.close();
169   if (!error)
170     session.proto().send_ok();
171   return error;
172 }
173 
174 
do_dispatch_command(xpl::Session & session,xpl::Crud_command_handler & crudh,xpl::Expectation_stack & expect,ngs::Request & command)175 ngs::Error_code do_dispatch_command(xpl::Session &session, xpl::Crud_command_handler &crudh,
176                                     xpl::Expectation_stack &expect, ngs::Request &command)
177 {
178   switch (command.get_type())
179   {
180     case Mysqlx::ClientMessages::SQL_STMT_EXECUTE:
181       return on_stmt_execute(session, static_cast<const Mysqlx::Sql::StmtExecute&>(*command.message()));
182 
183     case Mysqlx::ClientMessages::CRUD_FIND:
184       return crudh.execute_crud_find(session, static_cast<const Mysqlx::Crud::Find&>(*command.message()));
185 
186     case Mysqlx::ClientMessages::CRUD_INSERT:
187       return crudh.execute_crud_insert(session, static_cast<const Mysqlx::Crud::Insert&>(*command.message()));
188 
189     case Mysqlx::ClientMessages::CRUD_UPDATE:
190       return crudh.execute_crud_update(session, static_cast<const Mysqlx::Crud::Update&>(*command.message()));
191 
192     case Mysqlx::ClientMessages::CRUD_DELETE:
193       return crudh.execute_crud_delete(session, static_cast<const Mysqlx::Crud::Delete&>(*command.message()));
194 
195     case Mysqlx::ClientMessages::CRUD_CREATE_VIEW:
196       return crudh.execute_create_view(session, static_cast<const Mysqlx::Crud::CreateView&>(*command.message()));
197 
198     case Mysqlx::ClientMessages::CRUD_MODIFY_VIEW:
199       return crudh.execute_modify_view(session, static_cast<const Mysqlx::Crud::ModifyView&>(*command.message()));
200 
201     case Mysqlx::ClientMessages::CRUD_DROP_VIEW:
202       return crudh.execute_drop_view(session, static_cast<const Mysqlx::Crud::DropView&>(*command.message()));
203 
204     case Mysqlx::ClientMessages::EXPECT_OPEN:
205       return on_expect_open(session, expect, static_cast<const Mysqlx::Expect::Open&>(*command.message()));
206 
207     case Mysqlx::ClientMessages::EXPECT_CLOSE:
208       return on_expect_close(session, expect, static_cast<const Mysqlx::Expect::Close&>(*command.message()));
209   }
210 
211   session.proto().get_protocol_monitor().on_error_unknown_msg_type();
212   return ngs::Error(ER_UNKNOWN_COM_ERROR, "Unexpected message received");
213 }
214 
215 } // namespace
216 
217 
dispatch_command(Session & session,Crud_command_handler & crudh,Expectation_stack & expect,ngs::Request & command)218 bool xpl::dispatcher::dispatch_command(Session &session, Crud_command_handler &crudh,
219                                        Expectation_stack &expect, ngs::Request &command)
220 {
221   ngs::Error_code error = expect.pre_client_stmt(command.get_type());
222   if (!error)
223   {
224     error = do_dispatch_command(session, crudh, expect, command);
225     if (error)
226       session.proto().send_result(error);
227     expect.post_client_stmt(command.get_type(), error);
228   }
229   else
230     session.proto().send_result(error);
231   return error.error != ER_UNKNOWN_COM_ERROR;
232 }
233