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 "ngs/protocol/notice_builder.h"
27 #include "ngs_common/protocol_protobuf.h"
28 
29 using namespace ngs;
30 
encode_frame(Output_buffer * out_buffer,uint32 type,const std::string & data,int scope)31 void Notice_builder::encode_frame(
32   Output_buffer* out_buffer,
33   uint32 type,
34   const std::string &data,
35   int scope)
36 {
37   start_message(out_buffer, Mysqlx::ServerMessages::NOTICE);
38 
39   // 1) Type
40   encode_uint32(type);
41   // 2) Scope
42   encode_int32(scope);
43   // 3) Payload
44   encode_string(data.c_str(), data.length());
45 
46   end_message();
47 }
48 
encode_rows_affected(Output_buffer * out_buffer,uint64 value)49 void Notice_builder::encode_rows_affected(
50   Output_buffer* out_buffer,
51   uint64 value)
52 {
53   int32 param = Mysqlx::Notice::SessionStateChanged::ROWS_AFFECTED;
54   int32 type = Mysqlx::Datatypes::Scalar_Type_V_UINT;
55 
56   start_message(out_buffer, Mysqlx::ServerMessages::NOTICE);
57 
58   // 1) Type
59   encode_uint32(3);
60   // 2) Scope
61   encode_int32(static_cast<int>(Mysqlx::Notice::Frame_Scope_LOCAL));
62   // 3) Payload
63   google::protobuf::internal::WireFormatLite::WriteTag(3, google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, m_out_stream.get());
64   uint32 size_scalar = CodedOutputStream::VarintSize32SignExtended(type) + CodedOutputStream::VarintSize64(value) + 2 /*tags*/;
65   uint32 size_payload = 1 /* param tag */ + CodedOutputStream::VarintSize32SignExtended(param)
66     + 1 /* scalar tag */ + CodedOutputStream::VarintSize32(size_scalar) + size_scalar;
67   m_out_stream->WriteVarint32(size_payload);
68   {
69     m_field_number = 0;
70     // 1) Param
71     encode_int32(param);
72     // 2) Scalar
73     google::protobuf::internal::WireFormatLite::WriteTag(2, google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, m_out_stream.get());
74     m_out_stream->WriteVarint32(size_scalar);
75     {
76       m_field_number = 0;
77       // 1) Type
78       encode_int32(type);
79       // 3!) V_unisgned_int
80       m_field_number = 2; /*Need to skip one field tag here*/
81       encode_uint64(value);
82     }
83   }
84 
85   end_message();
86 }
87