/root/doris/be/src/util/mysql_global.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <float.h> |
21 | | #include <stdint.h> |
22 | | |
23 | | #include <cstring> |
24 | | #include <string> |
25 | | |
26 | | namespace doris { |
27 | | |
28 | | typedef unsigned char uchar; |
29 | | |
30 | 75 | #define int1store(T, A) *((uint8_t*)(T)) = (uint8_t)(A) |
31 | 0 | #define int2store(T, A) *((uint16_t*)(T)) = (uint16_t)(A) |
32 | | #define int3store(T, A) \ |
33 | 0 | do { \ |
34 | 0 | *(T) = (uchar)((A)); \ |
35 | 0 | *(T + 1) = (uchar)(((uint32_t)(A) >> 8)); \ |
36 | 0 | *(T + 2) = (uchar)(((A) >> 16)); \ |
37 | 0 | } while (0) |
38 | 13 | #define int4store(T, A) *((uint32_t*)(T)) = (uint32_t)(A) |
39 | 0 | #define int8store(T, A) *((int64_t*)(T)) = (uint64_t)(A) |
40 | 0 | #define float4store(T, A) *((float*)(T)) = (float)(A) |
41 | 0 | #define float8store(T, A) *((double*)(T)) = (double)(A) |
42 | | |
43 | | #define MY_ALIGN(A, L) (((A) + (L)-1) & ~((L)-1)) |
44 | | #define SIZEOF_CHARP 8 |
45 | | |
46 | | #define MAX_TINYINT_WIDTH 3 /* Max width for a TINY w.o. sign */ |
47 | | #define MAX_SMALLINT_WIDTH 5 /* Max width for a SHORT w.o. sign */ |
48 | | #define MAX_MEDIUMINT_WIDTH 8 /* Max width for a INT24 w.o. sign */ |
49 | | #define MAX_INT_WIDTH 10 /* Max width for a LONG w.o. sign */ |
50 | | #define MAX_BIGINT_WIDTH 20 /* Max width for a LONGLONG */ |
51 | | #define MAX_LARGEINT_WIDTH 39 /* Max width for a LARGEINT */ |
52 | | #define MAX_CHAR_WIDTH 255 /* Max length for a CHAR column */ |
53 | | #define MAX_BLOB_WIDTH 16777216 /* Default width for blob */ |
54 | | #define MAX_TIME_WIDTH 10 /* Max width for a TIME HH:MM:SS*/ |
55 | | #define MAX_DECPT_FOR_F_FORMAT DBL_DIG |
56 | | #define MAX_DATETIME_WIDTH 27 /* YYYY-MM-DD HH:MM:SS.ssssss */ |
57 | 0 | #define MAX_DECIMAL_WIDTH 29 /* Max width for a DECIMAL */ |
58 | | |
59 | | /* -[digits].E+## */ |
60 | | #define MAX_FLOAT_STR_LENGTH 24 // see gutil/strings/numbers.h kFloatToBufferSize |
61 | | /* -[digits].E+### */ |
62 | | #define MAX_DOUBLE_STR_LENGTH 32 // see gutil/strings/numbers.h kDoubleToBufferSize |
63 | | |
64 | | /* -[digits].[frac] */ |
65 | | #define MAX_DECIMAL_STR_LENGTH 29 |
66 | | |
67 | | inline void direct_write_to_mysql_result_string(std::string& mysql_rows, const char* str, |
68 | 56 | size_t size) { |
69 | | // MySQL protocol length encoding: |
70 | | // <= 250: 1 byte for length |
71 | | // < 65536: 1 byte (252) + 2 bytes for length |
72 | | // < 16777216: 1 byte (253) + 3 bytes for length |
73 | | // >= 16777216: 1 byte (254) + 8 bytes for length |
74 | | |
75 | 56 | char buf[16]; |
76 | 56 | if (size < 251ULL) { |
77 | 56 | int1store(buf, size); |
78 | 56 | mysql_rows.append(buf, 1); |
79 | 56 | } else if (size < 65536ULL) { |
80 | 0 | buf[0] = static_cast<char>(252); |
81 | 0 | uint16_t temp16 = static_cast<uint16_t>(size); |
82 | 0 | memcpy(buf + 1, &temp16, sizeof(temp16)); |
83 | 0 | mysql_rows.append(buf, 3); |
84 | 0 | } else if (size < 16777216ULL) { |
85 | 0 | buf[0] = static_cast<char>(253); |
86 | 0 | int3store(buf + 1, size); |
87 | 0 | mysql_rows.append(buf, 4); |
88 | 0 | } else { |
89 | 0 | buf[0] = static_cast<char>(254); |
90 | 0 | uint64_t temp64 = static_cast<uint64_t>(size); |
91 | 0 | memcpy(buf + 1, &temp64, sizeof(temp64)); |
92 | 0 | mysql_rows.append(buf, 9); |
93 | 0 | } |
94 | | |
95 | | // Append string content |
96 | 56 | mysql_rows.append(str, size); |
97 | 56 | } |
98 | | |
99 | 0 | inline void direct_write_to_mysql_result_null(std::string& mysql_rows) { |
100 | | // MySQL protocol for NULL value is a single byte with value 251 |
101 | 0 | mysql_rows.push_back(static_cast<char>(251)); |
102 | 0 | } |
103 | | |
104 | | } // namespace doris |