1 /* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #include "plugin/group_replication/include/sql_service/sql_service_context.h"
24 
25 #include "my_dbug.h"
26 
start_result_metadata(uint ncols,uint,const CHARSET_INFO * resultcs)27 int Sql_service_context::start_result_metadata(uint ncols, uint,
28                                                const CHARSET_INFO *resultcs) {
29   DBUG_TRACE;
30   DBUG_PRINT("info", ("resultcs->name: %s", resultcs->name));
31   if (resultset) {
32     resultset->set_cols(ncols);
33     resultset->set_charset(resultcs);
34   }
35   return 0;
36 }
37 
field_metadata(struct st_send_field * field,const CHARSET_INFO *)38 int Sql_service_context::field_metadata(struct st_send_field *field,
39                                         const CHARSET_INFO *) {
40   DBUG_TRACE;
41   DBUG_PRINT("info", ("field->flags: %d", (int)field->flags));
42   DBUG_PRINT("info", ("field->type: %d", (int)field->type));
43 
44   if (resultset) {
45     Field_type ftype = {field->db_name,        field->table_name,
46                         field->org_table_name, field->col_name,
47                         field->org_col_name,   field->length,
48                         field->charsetnr,      field->flags,
49                         field->decimals,       field->type};
50     resultset->set_metadata(ftype);
51   }
52   return 0;
53 }
54 
end_result_metadata(uint,uint)55 int Sql_service_context::end_result_metadata(uint, uint) {
56   DBUG_TRACE;
57   return 0;
58 }
59 
start_row()60 int Sql_service_context::start_row() {
61   DBUG_TRACE;
62   if (resultset) resultset->new_row();
63   return 0;
64 }
65 
end_row()66 int Sql_service_context::end_row() {
67   DBUG_TRACE;
68   if (resultset) resultset->increment_rows();
69   return 0;
70 }
71 
abort_row()72 void Sql_service_context::abort_row() { DBUG_TRACE; }
73 
get_client_capabilities()74 ulong Sql_service_context::get_client_capabilities() {
75   DBUG_TRACE;
76   return 0;
77 }
78 
get_null()79 int Sql_service_context::get_null() {
80   DBUG_TRACE;
81   if (resultset) resultset->new_field(nullptr);
82   return 0;
83 }
84 
get_integer(longlong value)85 int Sql_service_context::get_integer(longlong value) {
86   DBUG_TRACE;
87   if (resultset) resultset->new_field(new Field_value(value));
88   return 0;
89 }
90 
get_longlong(longlong value,uint is_unsigned)91 int Sql_service_context::get_longlong(longlong value, uint is_unsigned) {
92   DBUG_TRACE;
93   if (resultset) resultset->new_field(new Field_value(value, is_unsigned));
94   return 0;
95 }
96 
get_decimal(const decimal_t * value)97 int Sql_service_context::get_decimal(const decimal_t *value) {
98   DBUG_TRACE;
99   if (resultset) resultset->new_field(new Field_value(*value));
100   return 0;
101 }
102 
get_double(double value,uint32)103 int Sql_service_context::get_double(double value, uint32) {
104   DBUG_TRACE;
105   if (resultset) resultset->new_field(new Field_value(value));
106   return 0;
107 }
108 
get_date(const MYSQL_TIME * value)109 int Sql_service_context::get_date(const MYSQL_TIME *value) {
110   DBUG_TRACE;
111   if (resultset) resultset->new_field(new Field_value(*value));
112   return 0;
113 }
114 
get_time(const MYSQL_TIME * value,uint)115 int Sql_service_context::get_time(const MYSQL_TIME *value, uint) {
116   DBUG_TRACE;
117   if (resultset) resultset->new_field(new Field_value(*value));
118   return 0;
119 }
120 
get_datetime(const MYSQL_TIME * value,uint)121 int Sql_service_context::get_datetime(const MYSQL_TIME *value, uint) {
122   DBUG_TRACE;
123   if (resultset) resultset->new_field(new Field_value(*value));
124   return 0;
125 }
126 
get_string(const char * const value,size_t length,const CHARSET_INFO * const)127 int Sql_service_context::get_string(const char *const value, size_t length,
128                                     const CHARSET_INFO *const) {
129   DBUG_TRACE;
130   DBUG_PRINT("info", ("value: %s", value));
131   if (resultset) resultset->new_field(new Field_value(value, length));
132   return 0;
133 }
134 
handle_ok(uint server_status,uint statement_warn_count,ulonglong affected_rows,ulonglong last_insert_id,const char * const message)135 void Sql_service_context::handle_ok(uint server_status,
136                                     uint statement_warn_count,
137                                     ulonglong affected_rows,
138                                     ulonglong last_insert_id,
139                                     const char *const message) {
140   DBUG_TRACE;
141 
142   if (resultset) {
143     resultset->set_server_status(server_status);
144     resultset->set_warn_count(statement_warn_count);
145     resultset->set_affected_rows(affected_rows);
146     resultset->set_last_insert_id(last_insert_id);
147     resultset->set_message(message ? message : "");
148   }
149 }
150 
handle_error(uint sql_errno,const char * const err_msg,const char * const sqlstate)151 void Sql_service_context::handle_error(uint sql_errno,
152                                        const char *const err_msg,
153                                        const char *const sqlstate) {
154   DBUG_TRACE;
155   DBUG_PRINT("info", ("sql_errno: %d", (int)sql_errno));
156   DBUG_PRINT("info", ("err_msg: %s", err_msg));
157   DBUG_PRINT("info", ("sqlstate: %s", sqlstate));
158 
159   if (resultset) {
160     resultset->set_rows(0);
161     resultset->set_sql_errno(sql_errno);
162     resultset->set_err_msg(err_msg ? err_msg : "");
163     resultset->set_sqlstate(sqlstate ? sqlstate : "");
164   }
165 }
166 
shutdown(int)167 void Sql_service_context::shutdown(int) {
168   DBUG_TRACE;
169   if (resultset) resultset->set_killed();
170 }
171