wwbmmm commented on code in PR #2093: URL: https://github.com/apache/brpc/pull/2093#discussion_r1084936553
########## src/brpc/mysql_common.h: ########## @@ -0,0 +1,419 @@ +// Copyright (c) 2019 Baidu, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Authors: Yang,Liming (yanglimin...@baidu.com) + +#ifndef BRPC_MYSQL_COMMON_H +#define BRPC_MYSQL_COMMON_H + +#include <sstream> +#include <map> +#include "butil/logging.h" // LOG() + +namespace brpc { +// Msql Collation +extern const char* MysqlDefaultCollation; +extern const char* MysqlBinaryCollation; +const std::map<std::string, uint8_t> MysqlCollations = { + {"big5_chinese_ci", 1}, + {"latin2_czech_cs", 2}, + {"dec8_swedish_ci", 3}, + {"cp850_general_ci", 4}, + {"latin1_german1_ci", 5}, + {"hp8_english_ci", 6}, + {"koi8r_general_ci", 7}, + {"latin1_swedish_ci", 8}, + {"latin2_general_ci", 9}, + {"swe7_swedish_ci", 10}, + {"ascii_general_ci", 11}, + {"ujis_japanese_ci", 12}, + {"sjis_japanese_ci", 13}, + {"cp1251_bulgarian_ci", 14}, + {"latin1_danish_ci", 15}, + {"hebrew_general_ci", 16}, + {"tis620_thai_ci", 18}, + {"euckr_korean_ci", 19}, + {"latin7_estonian_cs", 20}, + {"latin2_hungarian_ci", 21}, + {"koi8u_general_ci", 22}, + {"cp1251_ukrainian_ci", 23}, + {"gb2312_chinese_ci", 24}, + {"greek_general_ci", 25}, + {"cp1250_general_ci", 26}, + {"latin2_croatian_ci", 27}, + {"gbk_chinese_ci", 28}, + {"cp1257_lithuanian_ci", 29}, + {"latin5_turkish_ci", 30}, + {"latin1_german2_ci", 31}, + {"armscii8_general_ci", 32}, + {"utf8_general_ci", 33}, + {"cp1250_czech_cs", 34}, + //{"ucs2_general_ci", 35}, + {"cp866_general_ci", 36}, + {"keybcs2_general_ci", 37}, + {"macce_general_ci", 38}, + {"macroman_general_ci", 39}, + {"cp852_general_ci", 40}, + {"latin7_general_ci", 41}, + {"latin7_general_cs", 42}, + {"macce_bin", 43}, + {"cp1250_croatian_ci", 44}, + {"utf8mb4_general_ci", 45}, + {"utf8mb4_bin", 46}, + {"latin1_bin", 47}, + {"latin1_general_ci", 48}, + {"latin1_general_cs", 49}, + {"cp1251_bin", 50}, + {"cp1251_general_ci", 51}, + {"cp1251_general_cs", 52}, + {"macroman_bin", 53}, + //{"utf16_general_ci", 54}, + //{"utf16_bin", 55}, + //{"utf16le_general_ci", 56}, + {"cp1256_general_ci", 57}, + {"cp1257_bin", 58}, + {"cp1257_general_ci", 59}, + //{"utf32_general_ci", 60}, + //{"utf32_bin", 61}, + //{"utf16le_bin", 62}, + {"binary", 63}, + {"armscii8_bin", 64}, + {"ascii_bin", 65}, + {"cp1250_bin", 66}, + {"cp1256_bin", 67}, + {"cp866_bin", 68}, + {"dec8_bin", 69}, + {"greek_bin", 70}, + {"hebrew_bin", 71}, + {"hp8_bin", 72}, + {"keybcs2_bin", 73}, + {"koi8r_bin", 74}, + {"koi8u_bin", 75}, + {"utf8_tolower_ci", 76}, + {"latin2_bin", 77}, + {"latin5_bin", 78}, + {"latin7_bin", 79}, + {"cp850_bin", 80}, + {"cp852_bin", 81}, + {"swe7_bin", 82}, + {"utf8_bin", 83}, + {"big5_bin", 84}, + {"euckr_bin", 85}, + {"gb2312_bin", 86}, + {"gbk_bin", 87}, + {"sjis_bin", 88}, + {"tis620_bin", 89}, + //"{ucs2_bin", 90}, + {"ujis_bin", 91}, + {"geostd8_general_ci", 92}, + {"geostd8_bin", 93}, + {"latin1_spanish_ci", 94}, + {"cp932_japanese_ci", 95}, + {"cp932_bin", 96}, + {"eucjpms_japanese_ci", 97}, + {"eucjpms_bin", 98}, + {"cp1250_polish_ci", 99}, + // {"utf16_unicode_ci", 101}, + // {"utf16_icelandic_ci", 102}, + // {"utf16_latvian_ci", 103}, + // {"utf16_romanian_ci", 104}, + // {"utf16_slovenian_ci", 105}, + // {"utf16_polish_ci", 106}, + // {"utf16_estonian_ci", 107}, + // {"utf16_spanish_ci", 108}, + // {"utf16_swedish_ci", 109}, + // {"utf16_turkish_ci", 110}, + // {"utf16_czech_ci", 111}, + // {"utf16_danish_ci", 112}, + // {"utf16_lithuanian_ci", 113}, + // {"utf16_slovak_ci", 114}, + // {"utf16_spanish2_ci", 115}, + // {"utf16_roman_ci", 116}, + // {"utf16_persian_ci", 117}, + // {"utf16_esperanto_ci", 118}, + // {"utf16_hungarian_ci", 119}, + // {"utf16_sinhala_ci", 120}, + // {"utf16_german2_ci", 121}, + // {"utf16_croatian_ci", 122}, + // {"utf16_unicode_520_ci", 123}, + // {"utf16_vietnamese_ci", 124}, + // {"ucs2_unicode_ci", 128}, + // {"ucs2_icelandic_ci", 129}, + // {"ucs2_latvian_ci", 130}, + // {"ucs2_romanian_ci", 131}, + // {"ucs2_slovenian_ci", 132}, + // {"ucs2_polish_ci", 133}, + // {"ucs2_estonian_ci", 134}, + // {"ucs2_spanish_ci", 135}, + // {"ucs2_swedish_ci", 136}, + // {"ucs2_turkish_ci", 137}, + // {"ucs2_czech_ci", 138}, + // {"ucs2_danish_ci", 139}, + // {"ucs2_lithuanian_ci", 140}, + // {"ucs2_slovak_ci", 141}, + // {"ucs2_spanish2_ci", 142}, + // {"ucs2_roman_ci", 143}, + // {"ucs2_persian_ci", 144}, + // {"ucs2_esperanto_ci", 145}, + // {"ucs2_hungarian_ci", 146}, + // {"ucs2_sinhala_ci", 147}, + // {"ucs2_german2_ci", 148}, + // {"ucs2_croatian_ci", 149}, + // {"ucs2_unicode_520_ci", 150}, + // {"ucs2_vietnamese_ci", 151}, + // {"ucs2_general_mysql500_ci", 159}, + // {"utf32_unicode_ci", 160}, + // {"utf32_icelandic_ci", 161}, + // {"utf32_latvian_ci", 162}, + // {"utf32_romanian_ci", 163}, + // {"utf32_slovenian_ci", 164}, + // {"utf32_polish_ci", 165}, + // {"utf32_estonian_ci", 166}, + // {"utf32_spanish_ci", 167}, + // {"utf32_swedish_ci", 168}, + // {"utf32_turkish_ci", 169}, + // {"utf32_czech_ci", 170}, + // {"utf32_danish_ci", 171}, + // {"utf32_lithuanian_ci", 172}, + // {"utf32_slovak_ci", 173}, + // {"utf32_spanish2_ci", 174}, + // {"utf32_roman_ci", 175}, + // {"utf32_persian_ci", 176}, + // {"utf32_esperanto_ci", 177}, + // {"utf32_hungarian_ci", 178}, + // {"utf32_sinhala_ci", 179}, + // {"utf32_german2_ci", 180}, + // {"utf32_croatian_ci", 181}, + // {"utf32_unicode_520_ci", 182}, + // {"utf32_vietnamese_ci", 183}, + {"utf8_unicode_ci", 192}, + {"utf8_icelandic_ci", 193}, + {"utf8_latvian_ci", 194}, + {"utf8_romanian_ci", 195}, + {"utf8_slovenian_ci", 196}, + {"utf8_polish_ci", 197}, + {"utf8_estonian_ci", 198}, + {"utf8_spanish_ci", 199}, + {"utf8_swedish_ci", 200}, + {"utf8_turkish_ci", 201}, + {"utf8_czech_ci", 202}, + {"utf8_danish_ci", 203}, + {"utf8_lithuanian_ci", 204}, + {"utf8_slovak_ci", 205}, + {"utf8_spanish2_ci", 206}, + {"utf8_roman_ci", 207}, + {"utf8_persian_ci", 208}, + {"utf8_esperanto_ci", 209}, + {"utf8_hungarian_ci", 210}, + {"utf8_sinhala_ci", 211}, + {"utf8_german2_ci", 212}, + {"utf8_croatian_ci", 213}, + {"utf8_unicode_520_ci", 214}, + {"utf8_vietnamese_ci", 215}, + {"utf8_general_mysql500_ci", 223}, + {"utf8mb4_unicode_ci", 224}, + {"utf8mb4_icelandic_ci", 225}, + {"utf8mb4_latvian_ci", 226}, + {"utf8mb4_romanian_ci", 227}, + {"utf8mb4_slovenian_ci", 228}, + {"utf8mb4_polish_ci", 229}, + {"utf8mb4_estonian_ci", 230}, + {"utf8mb4_spanish_ci", 231}, + {"utf8mb4_swedish_ci", 232}, + {"utf8mb4_turkish_ci", 233}, + {"utf8mb4_czech_ci", 234}, + {"utf8mb4_danish_ci", 235}, + {"utf8mb4_lithuanian_ci", 236}, + {"utf8mb4_slovak_ci", 237}, + {"utf8mb4_spanish2_ci", 238}, + {"utf8mb4_roman_ci", 239}, + {"utf8mb4_persian_ci", 240}, + {"utf8mb4_esperanto_ci", 241}, + {"utf8mb4_hungarian_ci", 242}, + {"utf8mb4_sinhala_ci", 243}, + {"utf8mb4_german2_ci", 244}, + {"utf8mb4_croatian_ci", 245}, + {"utf8mb4_unicode_520_ci", 246}, + {"utf8mb4_vietnamese_ci", 247}, + {"gb18030_chinese_ci", 248}, + {"gb18030_bin", 249}, + {"gb18030_unicode_520_ci", 250}, + {"utf8mb4_0900_ai_ci", 255}, +}; + +enum MysqlFieldType : uint8_t { + MYSQL_FIELD_TYPE_DECIMAL = 0x00, + MYSQL_FIELD_TYPE_TINY = 0x01, + MYSQL_FIELD_TYPE_SHORT = 0x02, + MYSQL_FIELD_TYPE_LONG = 0x03, + MYSQL_FIELD_TYPE_FLOAT = 0x04, + MYSQL_FIELD_TYPE_DOUBLE = 0x05, + MYSQL_FIELD_TYPE_NULL = 0x06, + MYSQL_FIELD_TYPE_TIMESTAMP = 0x07, + MYSQL_FIELD_TYPE_LONGLONG = 0x08, + MYSQL_FIELD_TYPE_INT24 = 0x09, + MYSQL_FIELD_TYPE_DATE = 0x0A, + MYSQL_FIELD_TYPE_TIME = 0x0B, + MYSQL_FIELD_TYPE_DATETIME = 0x0C, + MYSQL_FIELD_TYPE_YEAR = 0x0D, + MYSQL_FIELD_TYPE_NEWDATE = 0x0E, + MYSQL_FIELD_TYPE_VARCHAR = 0x0F, + MYSQL_FIELD_TYPE_BIT = 0x10, + MYSQL_FIELD_TYPE_JSON = 0xF5, + MYSQL_FIELD_TYPE_NEWDECIMAL = 0xF6, + MYSQL_FIELD_TYPE_ENUM = 0xF7, + MYSQL_FIELD_TYPE_SET = 0xF8, + MYSQL_FIELD_TYPE_TINY_BLOB = 0xF9, + MYSQL_FIELD_TYPE_MEDIUM_BLOB = 0xFA, + MYSQL_FIELD_TYPE_LONG_BLOB = 0xFB, + MYSQL_FIELD_TYPE_BLOB = 0xFC, + MYSQL_FIELD_TYPE_VAR_STRING = 0xFD, + MYSQL_FIELD_TYPE_STRING = 0xFE, + MYSQL_FIELD_TYPE_GEOMETRY = 0xFF, +}; + +enum MysqlFieldFlag : uint16_t { + MYSQL_NOT_NULL_FLAG = 0x0001, + MYSQL_PRI_KEY_FLAG = 0x0002, + MYSQL_UNIQUE_KEY_FLAG = 0x0004, + MYSQL_MULTIPLE_KEY_FLAG = 0x0008, + MYSQL_BLOB_FLAG = 0x0010, + MYSQL_UNSIGNED_FLAG = 0x0020, + MYSQL_ZEROFILL_FLAG = 0x0040, + MYSQL_BINARY_FLAG = 0x0080, + MYSQL_ENUM_FLAG = 0x0100, + MYSQL_AUTO_INCREMENT_FLAG = 0x0200, + MYSQL_TIMESTAMP_FLAG = 0x0400, + MYSQL_SET_FLAG = 0x0800, +}; + +enum MysqlServerStatus : uint16_t { Review Comment: 这个哪里用了 ########## src/brpc/controller.h: ########## @@ -104,6 +104,14 @@ enum StopStyle { const int32_t UNSET_MAGIC_NUM = -123456789; +// if controller want to reserve a sock after RPC, set BIND_SOCK_ACTIVE Review Comment: 这个ACTIVE不是很好理解,是不是可以叫BIND_SOCK_RESERVE ########## src/brpc/mysql.h: ########## @@ -0,0 +1,286 @@ +// Copyright (c) 2019 Baidu, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Authors: Yang,Liming (yanglimin...@baidu.com) + +#ifndef BRPC_MYSQL_H +#define BRPC_MYSQL_H + +#include <string> +#include <vector> +#include <google/protobuf/stubs/common.h> + +#include <google/protobuf/generated_message_util.h> +#include <google/protobuf/repeated_field.h> +#include <google/protobuf/extension_set.h> +#include <google/protobuf/generated_message_reflection.h> +#include "google/protobuf/descriptor.pb.h" + +#include "butil/iobuf.h" +#include "butil/strings/string_piece.h" +#include "butil/arena.h" +#include "parse_result.h" +#include "mysql_command.h" +#include "mysql_reply.h" +#include "mysql_transaction.h" +#include "mysql_statement.h" + +namespace brpc { +// Request to mysql. +// Notice that you can pipeline multiple commands in one request and sent +// them to ONE mysql-server together. +// Example: +// MysqlRequest request; +// request.Query("select * from table"); +// MysqlResponse response; +// channel.CallMethod(NULL, &controller, &request, &response, NULL/*done*/); +// if (!cntl.Failed()) { +// LOG(INFO) << response.reply(0); +// } + +class MysqlStatementStub { Review Comment: 这个类是不是放在mysql_statment.h里更合适 ########## src/brpc/mysql_command.cpp: ########## @@ -0,0 +1,260 @@ +// Copyright (c) 2015 Baidu, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Authors: Yang,Liming (yanglimin...@baidu.com) + +#include "butil/sys_byteorder.h" +#include "butil/logging.h" // LOG() +#include "brpc/mysql_command.h" +#include "brpc/mysql_common.h" +#include "brpc/mysql.h" + +namespace brpc { + +namespace { +const uint32_t max_allowed_packet = 67108864; Review Comment: 用全大写命名吧 ########## src/brpc/mysql_command.cpp: ########## @@ -0,0 +1,260 @@ +// Copyright (c) 2015 Baidu, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Authors: Yang,Liming (yanglimin...@baidu.com) + +#include "butil/sys_byteorder.h" +#include "butil/logging.h" // LOG() +#include "brpc/mysql_command.h" +#include "brpc/mysql_common.h" +#include "brpc/mysql.h" + +namespace brpc { + +namespace { +const uint32_t max_allowed_packet = 67108864; +const uint32_t max_packet_size = 16777215; + +template <class H, class F, class D> +butil::Status MakePacket(butil::IOBuf* outbuf, const H& head, const F& func, const D& data) { + long pkg_len = head.size() + data.size(); + if (pkg_len > max_allowed_packet) { + return butil::Status( + EINVAL, + "[MakePacket] statement size is too big, maxAllowedPacket = %d, pkg_len = %ld", + max_allowed_packet, + pkg_len); + } + uint32_t size, header; + uint8_t seq = 0; + size_t offset = 0; + for (; pkg_len > 0; pkg_len -= max_packet_size, ++seq) { + if (pkg_len > max_packet_size) { + size = max_packet_size; + } else { + size = pkg_len; + } + header = butil::ByteSwapToLE32(size); + ((uint8_t*)&header)[3] = seq; + outbuf->append(&header, 4); + if (seq == 0) { + const uint32_t old_size = outbuf->size(); + outbuf->append(head); + size -= outbuf->size() - old_size; Review Comment: 为什么不直接用size -= head.size()呢 ########## src/brpc/mysql_command.cpp: ########## @@ -0,0 +1,260 @@ +// Copyright (c) 2015 Baidu, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Authors: Yang,Liming (yanglimin...@baidu.com) + +#include "butil/sys_byteorder.h" +#include "butil/logging.h" // LOG() +#include "brpc/mysql_command.h" +#include "brpc/mysql_common.h" +#include "brpc/mysql.h" + +namespace brpc { + +namespace { +const uint32_t max_allowed_packet = 67108864; +const uint32_t max_packet_size = 16777215; + +template <class H, class F, class D> +butil::Status MakePacket(butil::IOBuf* outbuf, const H& head, const F& func, const D& data) { + long pkg_len = head.size() + data.size(); + if (pkg_len > max_allowed_packet) { + return butil::Status( + EINVAL, + "[MakePacket] statement size is too big, maxAllowedPacket = %d, pkg_len = %ld", + max_allowed_packet, + pkg_len); + } + uint32_t size, header; + uint8_t seq = 0; + size_t offset = 0; + for (; pkg_len > 0; pkg_len -= max_packet_size, ++seq) { + if (pkg_len > max_packet_size) { + size = max_packet_size; + } else { + size = pkg_len; + } + header = butil::ByteSwapToLE32(size); + ((uint8_t*)&header)[3] = seq; + outbuf->append(&header, 4); + if (seq == 0) { + const uint32_t old_size = outbuf->size(); + outbuf->append(head); + size -= outbuf->size() - old_size; + } + func(outbuf, data, size, offset); + offset += size; + } + + return butil::Status::OK(); +} + +} // namespace + +butil::Status MysqlMakeCommand(butil::IOBuf* outbuf, Review Comment: 这个叫MysqlMakeCommandPacket会不会更合适 ########## src/brpc/mysql_command.cpp: ########## @@ -0,0 +1,260 @@ +// Copyright (c) 2015 Baidu, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Authors: Yang,Liming (yanglimin...@baidu.com) + +#include "butil/sys_byteorder.h" +#include "butil/logging.h" // LOG() +#include "brpc/mysql_command.h" +#include "brpc/mysql_common.h" +#include "brpc/mysql.h" + +namespace brpc { + +namespace { +const uint32_t max_allowed_packet = 67108864; +const uint32_t max_packet_size = 16777215; + +template <class H, class F, class D> +butil::Status MakePacket(butil::IOBuf* outbuf, const H& head, const F& func, const D& data) { + long pkg_len = head.size() + data.size(); + if (pkg_len > max_allowed_packet) { + return butil::Status( + EINVAL, + "[MakePacket] statement size is too big, maxAllowedPacket = %d, pkg_len = %ld", + max_allowed_packet, + pkg_len); + } + uint32_t size, header; + uint8_t seq = 0; + size_t offset = 0; + for (; pkg_len > 0; pkg_len -= max_packet_size, ++seq) { + if (pkg_len > max_packet_size) { + size = max_packet_size; + } else { + size = pkg_len; + } + header = butil::ByteSwapToLE32(size); + ((uint8_t*)&header)[3] = seq; + outbuf->append(&header, 4); + if (seq == 0) { + const uint32_t old_size = outbuf->size(); + outbuf->append(head); + size -= outbuf->size() - old_size; + } + func(outbuf, data, size, offset); + offset += size; + } + + return butil::Status::OK(); +} + +} // namespace + +butil::Status MysqlMakeCommand(butil::IOBuf* outbuf, + const MysqlCommandType type, + const butil::StringPiece& command) { + if (outbuf == NULL || command.size() == 0) { + return butil::Status(EINVAL, "[MysqlMakeCommand] Param[outbuf] or [stmt] is NULL"); + } + auto func = + [](butil::IOBuf* outbuf, const butil::StringPiece& command, size_t size, size_t offset) { + outbuf->append(command.data() + offset, size); + }; + butil::IOBuf head; + head.push_back(type); + return MakePacket(outbuf, head, func, command); +} + +butil::Status MysqlMakeExecutePacket(butil::IOBuf* outbuf, + uint32_t stmt_id, + const butil::IOBuf& edata) { + butil::IOBuf head; // cmd_type + stmt_id + flag + reserved + body_size + head.push_back(MYSQL_COM_STMT_EXECUTE); + const uint32_t si = butil::ByteSwapToLE32(stmt_id); + head.append(&si, 4); + head.push_back('\0'); + head.push_back((char)0x01); + head.push_back('\0'); + head.push_back('\0'); + head.push_back('\0'); + auto func = [](butil::IOBuf* outbuf, const butil::IOBuf& data, size_t size, size_t offset) { + data.append_to(outbuf, size, offset); + }; + return MakePacket(outbuf, head, func, edata); +} + +butil::Status MysqlMakeExecuteData(MysqlStatementStub* stmt, Review Comment: 这个作为MysqlStatementStub的成员函数会不会更合适 ########## src/brpc/mysql_statement_inl.h: ########## @@ -0,0 +1,58 @@ +// Copyright (c) 2019 Baidu, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Authors: Yang,Liming (yanglimin...@baidu.com) + +#ifndef BRPC_MYSQL_STATEMENT_INL_H +#define BRPC_MYSQL_STATEMENT_INL_H +#include <gflags/gflags.h> +#include "butil/containers/flat_map.h" // FlatMap +#include "butil/containers/doubly_buffered_data.h" +#include "brpc/socket_id.h" + +namespace brpc { +DECLARE_int32(mysql_statment_map_size); + +struct MysqlStatementId { + uint32_t stmt_id; // statement id + uint64_t version; // socket's fd version +}; + +typedef butil::FlatMap<SocketId, MysqlStatementId> MysqlStatementKVMap; +typedef butil::DoublyBufferedData<MysqlStatementKVMap> MysqlStatementDBD; + +inline size_t my_init_kv(MysqlStatementKVMap& m) { Review Comment: 这个不用放在头文件里吧,放在mysql_statement.cpp里就行了 ########## src/brpc/mysql_reply.cpp: ########## @@ -0,0 +1,1189 @@ +// Copyright (c) 2019 Baidu, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Authors: Yang,Liming (yanglimin...@baidu.com) + +#include "brpc/mysql_common.h" +#include "brpc/mysql_reply.h" + +namespace brpc { + +#define MY_ALLOC_CHECK(expr) \ + do { \ + if ((expr) == false) { \ + return PARSE_ERROR_ABSOLUTELY_WRONG; \ + } \ + } while (0) + +#define MY_PARSE_CHECK(expr) \ + do { \ + ParseError rc = (expr); \ + if (rc != PARSE_OK) { \ + return rc; \ + } \ + } while (0) + +template <class Type> +inline bool my_alloc_check(butil::Arena* arena, const size_t n, Type*& pointer) { + if (pointer == NULL) { + pointer = (Type*)arena->allocate(sizeof(Type) * n); + if (pointer == NULL) { + return false; + } + for (size_t i = 0; i < n; ++i) { + new (pointer + i) Type; + } + } + return true; +} + +template <> +inline bool my_alloc_check(butil::Arena* arena, const size_t n, char*& pointer) { + if (pointer == NULL) { + pointer = (char*)arena->allocate(sizeof(char) * n); + if (pointer == NULL) { + return false; + } + } + return true; +} + +namespace { +struct MysqlHeader { + uint32_t payload_size; + uint32_t seq; +}; +const char* digits01 = + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123" + "456789"; +const char* digits10 = + "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999" + "999999"; +} // namespace + +const char* MysqlRspTypeToString(MysqlRspType type) { + switch (type) { + case MYSQL_RSP_OK: + return "ok"; + case MYSQL_RSP_ERROR: + return "error"; + case MYSQL_RSP_RESULTSET: + return "resultset"; + case MYSQL_RSP_EOF: + return "eof"; + case MYSQL_RSP_AUTH: + return "auth"; + case MYSQL_RSP_PREPARE_OK: + return "prepare_ok"; + default: + return "Unknown Response Type"; + } +} + +// check if the buf is contain a full package +inline bool is_full_package(const butil::IOBuf& buf) { + uint8_t header[4]; + const uint8_t* p = (const uint8_t*)buf.fetch(header, sizeof(header)); + if (p == NULL) { + return false; + } + uint32_t payload_size = mysql_uint3korr(p); + if (buf.size() < payload_size + 4) { + return false; + } + return true; +} +// if is eof package +inline bool is_an_eof(const butil::IOBuf& buf) { + uint8_t tmp[5]; + const uint8_t* p = (const uint8_t*)buf.fetch(tmp, sizeof(tmp)); + if (p == NULL) { + return false; + } + uint8_t type = p[4]; + if (type == MYSQL_RSP_EOF) { + return true; + } else { + return false; + } +} +// parse header +inline bool parse_header(butil::IOBuf& buf, MysqlHeader* value) { + if (!is_full_package(buf)) { + return false; + } + { + uint8_t tmp[3]; + buf.cutn(tmp, sizeof(tmp)); + value->payload_size = mysql_uint3korr(tmp); + } + { + uint8_t tmp; + buf.cut1((char*)&tmp); + value->seq = tmp; + } + return true; +} +// use this carefully, we depending on parse_header for checking IOBuf contain full package +inline uint64_t parse_encode_length(butil::IOBuf& buf) { + if (buf.size() == 0) { + return 0; + } + + uint64_t value = 0; + uint8_t f = 0; + buf.cut1((char*)&f); + if (f <= 250) { + value = f; + } else if (f == 251) { + value = 0; + } else if (f == 252) { + uint8_t tmp[2]; + buf.cutn(tmp, sizeof(tmp)); + value = mysql_uint2korr(tmp); + } else if (f == 253) { + uint8_t tmp[3]; + buf.cutn(tmp, sizeof(tmp)); + value = mysql_uint3korr(tmp); + } else if (f == 254) { + uint8_t tmp[8]; + buf.cutn(tmp, sizeof(tmp)); + value = mysql_uint8korr(tmp); + } + return value; +} + +ParseError MysqlReply::ConsumePartialIOBuf(butil::IOBuf& buf, + butil::Arena* arena, + bool is_auth, + MysqlStmtType stmt_type, + bool* more_results) { + *more_results = false; + if (!is_full_package(buf)) { + return PARSE_ERROR_NOT_ENOUGH_DATA; + } + uint8_t header[4 + 1]; // use the extra byte to judge message type + const uint8_t* p = (const uint8_t*)buf.fetch(header, sizeof(header)); + uint8_t type = (_type == MYSQL_RSP_UNKNOWN) ? p[4] : (uint8_t)_type; + if (is_auth && type != 0x00 && type != 0xFF) { + _type = MYSQL_RSP_AUTH; + MY_ALLOC_CHECK(my_alloc_check(arena, 1, _data.auth)); + MY_PARSE_CHECK(_data.auth->Parse(buf, arena)); + return PARSE_OK; + } + if (type == 0x00 && (is_auth || stmt_type != MYSQL_NEED_PREPARE)) { + _type = MYSQL_RSP_OK; + MY_ALLOC_CHECK(my_alloc_check(arena, 1, _data.ok)); + MY_PARSE_CHECK(_data.ok->Parse(buf, arena)); + *more_results = _data.ok->status() & MYSQL_SERVER_MORE_RESULTS_EXISTS; + } else if ((type == 0x00 && stmt_type == MYSQL_NEED_PREPARE) || type == MYSQL_RSP_PREPARE_OK) { + _type = MYSQL_RSP_PREPARE_OK; + MY_ALLOC_CHECK(my_alloc_check(arena, 1, _data.prepare_ok)); + MY_PARSE_CHECK(_data.prepare_ok->Parse(buf, arena)); + } else if (type == 0xFF) { + _type = MYSQL_RSP_ERROR; + MY_ALLOC_CHECK(my_alloc_check(arena, 1, _data.error)); + MY_PARSE_CHECK(_data.error->Parse(buf, arena)); + } else if (type == 0xFE) { + _type = MYSQL_RSP_EOF; + MY_ALLOC_CHECK(my_alloc_check(arena, 1, _data.eof)); + MY_PARSE_CHECK(_data.eof->Parse(buf)); + *more_results = _data.eof->status() & MYSQL_SERVER_MORE_RESULTS_EXISTS; + } else if (type >= 0x01 && type <= 0xFA) { + _type = MYSQL_RSP_RESULTSET; + MY_ALLOC_CHECK(my_alloc_check(arena, 1, _data.result_set)); + MY_PARSE_CHECK(_data.result_set->Parse(buf, arena, !(stmt_type == MYSQL_NORMAL_STATEMENT))); + *more_results = _data.result_set->_eof2.status() & MYSQL_SERVER_MORE_RESULTS_EXISTS; + } else { + LOG(ERROR) << "Unknown Response Type " + << "type=" << unsigned(type) << " buf_size=" << buf.size(); + return PARSE_ERROR_ABSOLUTELY_WRONG; + } + return PARSE_OK; +} + +void MysqlReply::Print(std::ostream& os) const { + if (_type == MYSQL_RSP_AUTH) { + const Auth& auth = *_data.auth; + os << "\nprotocol:" << (unsigned)auth._protocol << "\nversion:" << auth._version + << "\nthread_id:" << auth._thread_id << "\nsalt:" << auth._salt + << "\ncapacity:" << auth._capability << "\nlanguage:" << (unsigned)auth._collation + << "\nstatus:" << auth._status << "\nextended_capacity:" << auth._extended_capability + << "\nauth_plugin_length:" << auth._auth_plugin_length << "\nsalt2:" << auth._salt2 + << "\nauth_plugin:" << auth._auth_plugin; + } else if (_type == MYSQL_RSP_OK) { + const Ok& ok = *_data.ok; + os << "\naffect_row:" << ok._affect_row << "\nindex:" << ok._index + << "\nstatus:" << ok._status << "\nwarning:" << ok._warning << "\nmessage:" << ok._msg; + } else if (_type == MYSQL_RSP_ERROR) { + const Error& err = *_data.error; + os << "\nerrcode:" << err._errcode << "\nstatus:" << err._status + << "\nmessage:" << err._msg; + } else if (_type == MYSQL_RSP_RESULTSET) { + const ResultSet& r = *_data.result_set; + os << "\nheader.column_count:" << r._header._column_count; + for (uint64_t i = 0; i < r._header._column_count; ++i) { + os << "\ncolumn[" << i << "].catalog:" << r._columns[i]._catalog << "\ncolumn[" << i + << "].database:" << r._columns[i]._database << "\ncolumn[" << i + << "].table:" << r._columns[i]._table << "\ncolumn[" << i + << "].origin_table:" << r._columns[i]._origin_table << "\ncolumn[" << i + << "].name:" << r._columns[i]._name << "\ncolumn[" << i + << "].origin_name:" << r._columns[i]._origin_name << "\ncolumn[" << i + << "].charset:" << (uint16_t)r._columns[i]._charset << "\ncolumn[" << i + << "].length:" << r._columns[i]._length << "\ncolumn[" << i + << "].type:" << (unsigned)r._columns[i]._type << "\ncolumn[" << i + << "].flag:" << (unsigned)r._columns[i]._flag << "\ncolumn[" << i + << "].decimal:" << (unsigned)r._columns[i]._decimal; + } + os << "\neof1.warning:" << r._eof1._warning; + os << "\neof1.status:" << r._eof1._status; + int n = 0; + for (const Row* row = r._first->_next; row != r._last->_next; row = row->_next) { + os << "\nrow(" << n++ << "):"; + for (uint64_t j = 0; j < r._header._column_count; ++j) { + if (row->field(j).is_nil()) { + os << "NULL\t"; + continue; + } + switch (row->field(j)._type) { + case MYSQL_FIELD_TYPE_NULL: + os << "NULL"; + break; + case MYSQL_FIELD_TYPE_TINY: + if (r._columns[j]._flag & MYSQL_UNSIGNED_FLAG) { + os << unsigned(row->field(j).tiny()); + } else { + os << signed(row->field(j).stiny()); + } + break; + case MYSQL_FIELD_TYPE_SHORT: + case MYSQL_FIELD_TYPE_YEAR: + if (r._columns[j]._flag & MYSQL_UNSIGNED_FLAG) { + os << unsigned(row->field(j).small()); + } else { + os << signed(row->field(j).ssmall()); + } + break; + case MYSQL_FIELD_TYPE_INT24: + case MYSQL_FIELD_TYPE_LONG: + if (r._columns[j]._flag & MYSQL_UNSIGNED_FLAG) { + os << row->field(j).integer(); + } else { + os << row->field(j).sinteger(); + } + break; + case MYSQL_FIELD_TYPE_LONGLONG: + if (r._columns[j]._flag & MYSQL_UNSIGNED_FLAG) { + os << row->field(j).bigint(); + } else { + os << row->field(j).sbigint(); + } + break; + case MYSQL_FIELD_TYPE_FLOAT: + os << row->field(j).float32(); + break; + case MYSQL_FIELD_TYPE_DOUBLE: + os << row->field(j).float64(); + break; + case MYSQL_FIELD_TYPE_DECIMAL: + case MYSQL_FIELD_TYPE_NEWDECIMAL: + case MYSQL_FIELD_TYPE_VARCHAR: + case MYSQL_FIELD_TYPE_BIT: + case MYSQL_FIELD_TYPE_ENUM: + case MYSQL_FIELD_TYPE_SET: + case MYSQL_FIELD_TYPE_TINY_BLOB: + case MYSQL_FIELD_TYPE_MEDIUM_BLOB: + case MYSQL_FIELD_TYPE_LONG_BLOB: + case MYSQL_FIELD_TYPE_BLOB: + case MYSQL_FIELD_TYPE_VAR_STRING: + case MYSQL_FIELD_TYPE_STRING: + case MYSQL_FIELD_TYPE_GEOMETRY: + case MYSQL_FIELD_TYPE_JSON: + case MYSQL_FIELD_TYPE_TIME: + case MYSQL_FIELD_TYPE_DATE: + case MYSQL_FIELD_TYPE_NEWDATE: + case MYSQL_FIELD_TYPE_TIMESTAMP: + case MYSQL_FIELD_TYPE_DATETIME: + os << row->field(j).string(); + break; + default: + os << "Unknown field type"; + } + os << "\t"; + } + } + os << "\neof2.warning:" << r._eof2._warning; + os << "\neof2.status:" << r._eof2._status; + } else if (_type == MYSQL_RSP_EOF) { + const Eof& e = *_data.eof; + os << "\nwarning:" << e._warning << "\nstatus:" << e._status; + } else if (_type == MYSQL_RSP_PREPARE_OK) { + const PrepareOk& prep = *_data.prepare_ok; + os << "\nstmt_id:" << prep._header._stmt_id + << "\ncolumn_count:" << prep._header._column_count + << "\nparam_count:" << prep._header._param_count; + for (uint16_t i = 0; i < prep._header._param_count; ++i) { + os << "\nparam[" << i << "].catalog:" << prep._params[i]._catalog << "\nparam[" << i + << "].database:" << prep._params[i]._database << "\nparam[" << i + << "].table:" << prep._params[i]._table << "\nparam[" << i + << "].origin_table:" << prep._params[i]._origin_table << "\nparam[" << i + << "].name:" << prep._params[i]._name << "\nparam[" << i + << "].origin_name:" << prep._params[i]._origin_name << "\nparam[" << i + << "].charset:" << (uint16_t)prep._params[i]._charset << "\nparam[" << i + << "].length:" << prep._params[i]._length << "\nparam[" << i + << "].type:" << (unsigned)prep._params[i]._type << "\nparam[" << i + << "].flag:" << (unsigned)prep._params[i]._flag << "\nparam[" << i + << "].decimal:" << (unsigned)prep._params[i]._decimal; + } + for (uint16_t i = 0; i < prep._header._column_count; ++i) { + os << "\ncolumn[" << i << "].catalog:" << prep._columns[i]._catalog << "\ncolumn[" << i + << "].database:" << prep._columns[i]._database << "\ncolumn[" << i + << "].table:" << prep._columns[i]._table << "\ncolumn[" << i + << "].origin_table:" << prep._columns[i]._origin_table << "\ncolumn[" << i + << "].name:" << prep._columns[i]._name << "\ncolumn[" << i + << "].origin_name:" << prep._columns[i]._origin_name << "\ncolumn[" << i + << "].charset:" << (uint16_t)prep._columns[i]._charset << "\ncolumn[" << i + << "].length:" << prep._columns[i]._length << "\ncolumn[" << i + << "].type:" << (unsigned)prep._columns[i]._type << "\ncolumn[" << i + << "].flag:" << (unsigned)prep._columns[i]._flag << "\ncolumn[" << i + << "].decimal:" << (unsigned)prep._columns[i]._decimal; + } + } else { + os << "Unknown response type"; + } +} + +ParseError MysqlReply::Auth::Parse(butil::IOBuf& buf, butil::Arena* arena) { + if (is_parsed()) { + return PARSE_OK; + } + const std::string delim(1, 0x00); + MysqlHeader header; + if (!parse_header(buf, &header)) { + return PARSE_ERROR_NOT_ENOUGH_DATA; + } + buf.cut1((char*)&_protocol); + { + butil::IOBuf version; + buf.cut_until(&version, delim); + char* d = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, version.size(), d)); + version.copy_to(d); + _version.set(d, version.size()); + } + { + uint8_t tmp[4]; + buf.cutn(tmp, sizeof(tmp)); + _thread_id = mysql_uint4korr(tmp); + } + { + butil::IOBuf salt; + buf.cut_until(&salt, delim); + char* d = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, salt.size(), d)); + salt.copy_to(d); + _salt.set(d, salt.size()); + } + { + uint8_t tmp[2]; + buf.cutn(&tmp, sizeof(tmp)); + _capability = mysql_uint2korr(tmp); + } + buf.cut1((char*)&_collation); + { + uint8_t tmp[2]; + buf.cutn(tmp, sizeof(tmp)); + _status = mysql_uint2korr(tmp); + } + { + uint8_t tmp[2]; + buf.cutn(tmp, sizeof(tmp)); + _extended_capability = mysql_uint2korr(tmp); + } + buf.cut1((char*)&_auth_plugin_length); + buf.pop_front(10); + { + butil::IOBuf salt2; + buf.cut_until(&salt2, delim); + char* d = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, salt2.size(), d)); + salt2.copy_to(d); + _salt2.set(d, salt2.size()); + } + { + char* d = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, _auth_plugin_length, d)); + buf.cutn(d, _auth_plugin_length); + _auth_plugin.set(d, _auth_plugin_length); + } + buf.clear(); // consume all buf + set_parsed(); + return PARSE_OK; +} + +ParseError MysqlReply::ResultSetHeader::Parse(butil::IOBuf& buf) { + if (is_parsed()) { + return PARSE_OK; + } + MysqlHeader header; + if (!parse_header(buf, &header)) { + return PARSE_ERROR_NOT_ENOUGH_DATA; + } + uint64_t old_size, new_size; + old_size = buf.size(); + _column_count = parse_encode_length(buf); + new_size = buf.size(); + if (old_size - new_size < header.payload_size) { + _extra_msg = parse_encode_length(buf); + } else { + _extra_msg = 0; + } + set_parsed(); + return PARSE_OK; +} + +ParseError MysqlReply::Column::Parse(butil::IOBuf& buf, butil::Arena* arena) { + if (is_parsed()) { + return PARSE_OK; + } + MysqlHeader header; + if (!parse_header(buf, &header)) { + return PARSE_ERROR_NOT_ENOUGH_DATA; + } + + uint64_t len = parse_encode_length(buf); + char* catalog = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, len, catalog)); + buf.cutn(catalog, len); + _catalog.set(catalog, len); + + len = parse_encode_length(buf); + char* database = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, len, database)); + buf.cutn(database, len); + _database.set(database, len); + + len = parse_encode_length(buf); + char* table = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, len, table)); + buf.cutn(table, len); + _table.set(table, len); + + len = parse_encode_length(buf); + char* origin_table = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, len, origin_table)); + buf.cutn(origin_table, len); + _origin_table.set(origin_table, len); + + len = parse_encode_length(buf); + char* name = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, len, name)); + buf.cutn(name, len); + _name.set(name, len); + + len = parse_encode_length(buf); + char* origin_name = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, len, origin_name)); + buf.cutn(origin_name, len); + _origin_name.set(origin_name, len); + buf.pop_front(1); + { + uint8_t tmp[2]; + buf.cutn(tmp, sizeof(tmp)); + _charset = mysql_uint2korr(tmp); + } + { + uint8_t tmp[4]; + buf.cutn(tmp, sizeof(tmp)); + _length = mysql_uint4korr(tmp); + } + buf.cut1((char*)&_type); + { + uint8_t tmp[2]; + buf.cutn(tmp, sizeof(tmp)); + _flag = (MysqlFieldFlag)mysql_uint2korr(tmp); + } + buf.cut1((char*)&_decimal); + buf.pop_front(2); + set_parsed(); + return PARSE_OK; +} + +ParseError MysqlReply::Ok::Parse(butil::IOBuf& buf, butil::Arena* arena) { + if (is_parsed()) { + return PARSE_OK; + } + MysqlHeader header; + if (!parse_header(buf, &header)) { + return PARSE_ERROR_NOT_ENOUGH_DATA; + } + + uint64_t old_size, new_size; + old_size = buf.size(); + buf.pop_front(1); + + _affect_row = parse_encode_length(buf); + _index = parse_encode_length(buf); + buf.cutn(&_status, 2); + buf.cutn(&_warning, 2); + + new_size = buf.size(); + if (old_size - new_size < header.payload_size) { + const int64_t len = header.payload_size - (old_size - new_size); + char* msg = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, len, msg)); + buf.cutn(msg, len); + _msg.set(msg, len); + // buf.pop_front(1); // Null + } + set_parsed(); + return PARSE_OK; +} + +ParseError MysqlReply::Eof::Parse(butil::IOBuf& buf) { + if (is_parsed()) { + return PARSE_OK; + } + MysqlHeader header; + if (!parse_header(buf, &header)) { + return PARSE_ERROR_NOT_ENOUGH_DATA; + } + buf.pop_front(1); + buf.cutn(&_warning, 2); + buf.cutn(&_status, 2); + set_parsed(); + return PARSE_OK; +} + +ParseError MysqlReply::Error::Parse(butil::IOBuf& buf, butil::Arena* arena) { + if (is_parsed()) { + return PARSE_OK; + } + MysqlHeader header; + if (!parse_header(buf, &header)) { + return PARSE_ERROR_NOT_ENOUGH_DATA; + } + buf.pop_front(1); // 0xFF + { + uint8_t tmp[2]; + buf.cutn(tmp, sizeof(tmp)); + _errcode = mysql_uint2korr(tmp); + } + buf.pop_front(1); // '#' + // 5 byte server status + char* status = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, 5, status)); + buf.cutn(status, 5); + _status.set(status, 5); + // error message, Null-Terminated string + uint64_t len = header.payload_size - 9; + char* msg = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, len, msg)); + buf.cutn(msg, len); + _msg.set(msg, len); + // buf.pop_front(1); // Null + set_parsed(); + return PARSE_OK; +} + +ParseError MysqlReply::Row::Parse(butil::IOBuf& buf, + const MysqlReply::Column* columns, + uint64_t column_count, + MysqlReply::Field* fields, + bool binary, + butil::Arena* arena) { + if (is_parsed()) { + return PARSE_OK; + } + MysqlHeader header; + if (!parse_header(buf, &header)) { + return PARSE_ERROR_NOT_ENOUGH_DATA; + } + if (!binary) { // mysql text protocol + for (uint64_t i = 0; i < column_count; ++i) { + MY_PARSE_CHECK(fields[i].Parse(buf, columns + i, arena)); + } + } else { // mysql binary protocol + uint8_t hdr = 0; + buf.cut1((char*)&hdr); + if (hdr != 0x00) { + return PARSE_ERROR_ABSOLUTELY_WRONG; + } + // NULL-bitmap, [(column-count + 7 + 2) / 8 bytes] + const uint64_t size = ((column_count + 7 + 2) >> 3); + uint8_t null_mask[size]; + for (size_t i = 0; i < sizeof(null_mask); ++i) { + null_mask[i] = 0; + } + buf.cutn(null_mask, size); + for (uint64_t i = 0; i < column_count; ++i) { + MY_PARSE_CHECK(fields[i].Parse(buf, columns + i, i, column_count, null_mask, arena)); + } + } + set_parsed(); + return PARSE_OK; +} + +ParseError MysqlReply::Field::Parse(butil::IOBuf& buf, + const MysqlReply::Column* column, + butil::Arena* arena) { + if (is_parsed()) { + return PARSE_OK; + } + // field type + _type = column->_type; + // is unsigned flag set + _unsigned = column->_flag & MYSQL_UNSIGNED_FLAG; + // parse encode length + const uint64_t len = parse_encode_length(buf); + // is it null? + if (len == 0 && !(column->_flag & MYSQL_NOT_NULL_FLAG)) { + _is_nil = true; + set_parsed(); + return PARSE_OK; + } + // field is not null + butil::IOBuf str; + buf.cutn(&str, len); + switch (_type) { + case MYSQL_FIELD_TYPE_NULL: + _is_nil = true; + break; + case MYSQL_FIELD_TYPE_TINY: + if (column->_flag & MYSQL_UNSIGNED_FLAG) { + _data.tiny = strtoul(str.to_string().c_str(), NULL, 10); + } else { + _data.stiny = strtol(str.to_string().c_str(), NULL, 10); + } + break; + case MYSQL_FIELD_TYPE_SHORT: + case MYSQL_FIELD_TYPE_YEAR: + if (column->_flag & MYSQL_UNSIGNED_FLAG) { + _data.small = strtoul(str.to_string().c_str(), NULL, 10); + } else { + _data.ssmall = strtol(str.to_string().c_str(), NULL, 10); + } + break; + case MYSQL_FIELD_TYPE_INT24: + case MYSQL_FIELD_TYPE_LONG: + if (column->_flag & MYSQL_UNSIGNED_FLAG) { + _data.integer = strtoul(str.to_string().c_str(), NULL, 10); + } else { + _data.sinteger = strtol(str.to_string().c_str(), NULL, 10); + } + break; + case MYSQL_FIELD_TYPE_LONGLONG: + if (column->_flag & MYSQL_UNSIGNED_FLAG) { + _data.bigint = strtoul(str.to_string().c_str(), NULL, 10); + } else { + _data.sbigint = strtol(str.to_string().c_str(), NULL, 10); + } + break; + case MYSQL_FIELD_TYPE_FLOAT: + _data.float32 = strtof(str.to_string().c_str(), NULL); + break; + case MYSQL_FIELD_TYPE_DOUBLE: + _data.float64 = strtod(str.to_string().c_str(), NULL); + break; + case MYSQL_FIELD_TYPE_DECIMAL: + case MYSQL_FIELD_TYPE_NEWDECIMAL: + case MYSQL_FIELD_TYPE_VARCHAR: + case MYSQL_FIELD_TYPE_BIT: + case MYSQL_FIELD_TYPE_ENUM: + case MYSQL_FIELD_TYPE_SET: + case MYSQL_FIELD_TYPE_TINY_BLOB: + case MYSQL_FIELD_TYPE_MEDIUM_BLOB: + case MYSQL_FIELD_TYPE_LONG_BLOB: + case MYSQL_FIELD_TYPE_BLOB: + case MYSQL_FIELD_TYPE_VAR_STRING: + case MYSQL_FIELD_TYPE_STRING: + case MYSQL_FIELD_TYPE_GEOMETRY: + case MYSQL_FIELD_TYPE_JSON: + case MYSQL_FIELD_TYPE_TIME: + case MYSQL_FIELD_TYPE_DATE: + case MYSQL_FIELD_TYPE_NEWDATE: + case MYSQL_FIELD_TYPE_TIMESTAMP: + case MYSQL_FIELD_TYPE_DATETIME: { + char* d = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, len, d)); + str.copy_to(d); + _data.str.set(d, len); + } break; + default: + LOG(ERROR) << "Unknown field type"; + set_parsed(); + return PARSE_ERROR_ABSOLUTELY_WRONG; + } + set_parsed(); + return PARSE_OK; +} + +ParseError MysqlReply::Field::Parse(butil::IOBuf& buf, + const MysqlReply::Column* column, + uint64_t column_index, + uint64_t column_count, + const uint8_t* null_mask, + butil::Arena* arena) { + if (is_parsed()) { + return PARSE_OK; + } + // field type + _type = column->_type; + // is unsigned flag set + _unsigned = column->_flag & MYSQL_UNSIGNED_FLAG; + // (byte >> bit-pos) % 2 == 1 + if (((null_mask[(column_index + 2) >> 3] >> ((column_index + 2) & 7)) & 1) == 1) { + _is_nil = true; + set_parsed(); + return PARSE_OK; + } + + switch (_type) { + case MYSQL_FIELD_TYPE_NULL: + _is_nil = true; + break; + case MYSQL_FIELD_TYPE_TINY: + if (column->_flag & MYSQL_UNSIGNED_FLAG) { + buf.cut1((char*)&_data.tiny); + } else { + buf.cut1((char*)&_data.stiny); + } + break; + case MYSQL_FIELD_TYPE_SHORT: + case MYSQL_FIELD_TYPE_YEAR: + if (column->_flag & MYSQL_UNSIGNED_FLAG) { + uint8_t* p = (uint8_t*)&_data.small; + buf.cutn(p, 2); + _data.small = mysql_uint2korr(p); + } else { + uint8_t* p = (uint8_t*)&_data.ssmall; + buf.cutn(p, 2); + _data.ssmall = (int16_t)mysql_uint2korr(p); + } + break; + case MYSQL_FIELD_TYPE_INT24: + case MYSQL_FIELD_TYPE_LONG: + if (column->_flag & MYSQL_UNSIGNED_FLAG) { + uint8_t* p = (uint8_t*)&_data.integer; + buf.cutn(p, 4); + _data.integer = mysql_uint4korr(p); + } else { + uint8_t* p = (uint8_t*)&_data.sinteger; + buf.cutn(p, 4); + _data.sinteger = (int32_t)mysql_uint4korr(p); + } + break; + case MYSQL_FIELD_TYPE_LONGLONG: + if (column->_flag & MYSQL_UNSIGNED_FLAG) { + uint8_t* p = (uint8_t*)&_data.bigint; + buf.cutn(p, 8); + _data.bigint = mysql_uint8korr(p); + } else { + uint8_t* p = (uint8_t*)&_data.sbigint; + buf.cutn(p, 8); + _data.sbigint = (int64_t)mysql_uint8korr(p); + } + break; + case MYSQL_FIELD_TYPE_FLOAT: { + uint8_t* p = (uint8_t*)&_data.float32; + buf.cutn(p, 4); + } break; + case MYSQL_FIELD_TYPE_DOUBLE: { + uint8_t* p = (uint8_t*)&_data.float64; + buf.cutn(p, 8); + } break; + case MYSQL_FIELD_TYPE_DECIMAL: + case MYSQL_FIELD_TYPE_NEWDECIMAL: + case MYSQL_FIELD_TYPE_VARCHAR: + case MYSQL_FIELD_TYPE_BIT: + case MYSQL_FIELD_TYPE_ENUM: + case MYSQL_FIELD_TYPE_SET: + case MYSQL_FIELD_TYPE_TINY_BLOB: + case MYSQL_FIELD_TYPE_MEDIUM_BLOB: + case MYSQL_FIELD_TYPE_LONG_BLOB: + case MYSQL_FIELD_TYPE_BLOB: + case MYSQL_FIELD_TYPE_VAR_STRING: + case MYSQL_FIELD_TYPE_STRING: + case MYSQL_FIELD_TYPE_GEOMETRY: + case MYSQL_FIELD_TYPE_JSON: { + const uint64_t len = parse_encode_length(buf); + // is it null? + if (len == 0 && !(column->_flag & MYSQL_NOT_NULL_FLAG)) { + _is_nil = true; + set_parsed(); + return PARSE_OK; + } + // field is not null + char* d = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, len, d)); + buf.cutn(d, len); + _data.str.set(d, len); + } break; + case MYSQL_FIELD_TYPE_NEWDATE: // Date YYYY-MM-DD + case MYSQL_FIELD_TYPE_DATE: // Date YYYY-MM-DD + case MYSQL_FIELD_TYPE_DATETIME: // Timestamp YYYY-MM-DD HH:MM:SS[.fractal] + case MYSQL_FIELD_TYPE_TIMESTAMP: { // Timestamp YYYY-MM-DD HH:MM:SS[.fractal] + ParseError rc = ParseBinaryDataTime(buf, column, _data.str, arena); + if (rc != PARSE_OK) { + return rc; + } + } break; + case MYSQL_FIELD_TYPE_TIME: { // Time [-][H]HH:MM:SS[.fractal] + ParseError rc = ParseBinaryTime(buf, column, _data.str, arena); + if (rc != PARSE_OK) { + return rc; + } + } break; + default: + LOG(ERROR) << "Unknown field type"; + return PARSE_ERROR_ABSOLUTELY_WRONG; + } + set_parsed(); + return PARSE_OK; +} + +ParseError MysqlReply::Field::ParseBinaryTime(butil::IOBuf& buf, + const MysqlReply::Column* column, + butil::StringPiece& str, + butil::Arena* arena) { + + const uint64_t len = parse_encode_length(buf); + if (len == 0) { + _is_nil = true; + return PARSE_OK; + } + + if (len != 8 && len != 12) { + LOG(ERROR) << "invalid TIME packet length " << len; + return PARSE_ERROR_ABSOLUTELY_WRONG; + } + + uint8_t dstlen; + switch (column->_decimal) { + case 0x00: + case 0x1f: + dstlen = 8; + break; + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + dstlen = 8 + 1 + column->_decimal; + break; + default: + LOG(ERROR) << "protocol error, illegal decimals value " << column->_decimal; + return PARSE_ERROR_ABSOLUTELY_WRONG; + } + + size_t i = 0; + char* d = NULL; + MY_ALLOC_CHECK(my_alloc_check(arena, dstlen + 2, d)); + d[dstlen] = '\0'; + d[dstlen + 1] = '\0'; + uint32_t day; + uint8_t neg, hour, min, sec; + + buf.cut1((char*)&neg); + if (neg == 1) { + d[i++] = '-'; + } + + buf.cutn(&day, 4); + day = mysql_uint4korr((uint8_t*)&day); + buf.cut1((char*)&hour); + hour += day * 24; + if (hour >= 100) { + std::ostringstream os; + os << hour; + std::string s = os.str(); + for (const auto& v : s) { + d[i++] = v; + } + } else { + d[i++] = digits10[hour]; + d[i++] = digits01[hour]; + } + + buf.cut1((char*)&min); + buf.cut1((char*)&sec); + + d[i++] = ':'; + d[i++] = digits10[min]; + d[i++] = digits01[min]; + d[i++] = ':'; + d[i++] = digits10[sec]; + d[i++] = digits01[sec]; + + ParseError rc = ParseMicrosecs(buf, column->_decimal, d + i); + if (rc == PARSE_OK) { + str.set(d, dstlen + 2); + } + return rc; +} + +ParseError MysqlReply::Field::ParseBinaryDataTime(butil::IOBuf& buf, Review Comment: DataTime -> DateTime -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org