2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2022-05-08 17:01:47 +00:00
|
|
|
#include <cinttypes>
|
2022-11-14 14:17:17 +00:00
|
|
|
#include <utility>
|
2022-11-15 00:46:15 +00:00
|
|
|
#include <Common/formatIPv6.h>
|
2023-03-06 14:53:58 +00:00
|
|
|
#include <base/hex.h>
|
2017-09-07 21:04:48 +00:00
|
|
|
|
2010-06-04 18:25:25 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-07-14 15:08:01 +00:00
|
|
|
template <typename IteratorSrc, typename IteratorDst>
|
2022-03-13 11:59:20 +00:00
|
|
|
void formatHex(IteratorSrc src, IteratorDst dst, size_t num_bytes)
|
2017-06-15 09:12:32 +00:00
|
|
|
{
|
|
|
|
size_t src_pos = 0;
|
|
|
|
size_t dst_pos = 0;
|
|
|
|
for (; src_pos < num_bytes; ++src_pos)
|
|
|
|
{
|
2017-09-07 21:04:48 +00:00
|
|
|
writeHexByteLowercase(src[src_pos], &dst[dst_pos]);
|
2017-06-15 09:12:32 +00:00
|
|
|
dst_pos += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 19:36:37 +00:00
|
|
|
std::array<char, 36> formatUUID(const UUID & uuid)
|
2017-06-30 20:30:13 +00:00
|
|
|
{
|
2023-06-02 19:36:37 +00:00
|
|
|
std::array<char, 36> dst;
|
|
|
|
auto * dst_ptr = dst.data();
|
2023-08-16 20:12:31 +00:00
|
|
|
|
2023-06-05 15:18:03 +00:00
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2023-08-16 20:12:31 +00:00
|
|
|
const auto * src_ptr = reinterpret_cast<const UInt8 *>(&uuid);
|
|
|
|
const std::reverse_iterator src(src_ptr + 16);
|
2023-06-05 15:18:03 +00:00
|
|
|
#else
|
2023-08-16 20:12:31 +00:00
|
|
|
const auto * src = reinterpret_cast<const UInt8 *>(&uuid);
|
|
|
|
#endif
|
|
|
|
formatHex(src + 8, dst_ptr, 4);
|
2023-06-05 15:18:03 +00:00
|
|
|
dst[8] = '-';
|
2023-08-16 20:12:31 +00:00
|
|
|
formatHex(src + 12, dst_ptr + 9, 2);
|
2023-06-05 15:18:03 +00:00
|
|
|
dst[13] = '-';
|
2023-08-16 20:12:31 +00:00
|
|
|
formatHex(src + 14, dst_ptr + 14, 2);
|
2023-06-05 15:18:03 +00:00
|
|
|
dst[18] = '-';
|
2023-08-16 20:12:31 +00:00
|
|
|
formatHex(src, dst_ptr + 19, 2);
|
2023-06-05 15:18:03 +00:00
|
|
|
dst[23] = '-';
|
2023-08-16 20:12:31 +00:00
|
|
|
formatHex(src + 2, dst_ptr + 24, 6);
|
2023-06-02 19:36:37 +00:00
|
|
|
|
|
|
|
return dst;
|
2017-06-30 20:30:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 14:17:17 +00:00
|
|
|
void writeIPv4Text(const IPv4 & ip, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
size_t idx = (ip >> 24);
|
2022-12-07 21:29:17 +00:00
|
|
|
buf.write(one_byte_to_string_lookup_table[idx].first, one_byte_to_string_lookup_table[idx].second);
|
2022-11-14 14:17:17 +00:00
|
|
|
buf.write('.');
|
|
|
|
idx = (ip >> 16) & 0xFF;
|
2022-12-07 21:29:17 +00:00
|
|
|
buf.write(one_byte_to_string_lookup_table[idx].first, one_byte_to_string_lookup_table[idx].second);
|
2022-11-14 14:17:17 +00:00
|
|
|
buf.write('.');
|
|
|
|
idx = (ip >> 8) & 0xFF;
|
2022-12-07 21:29:17 +00:00
|
|
|
buf.write(one_byte_to_string_lookup_table[idx].first, one_byte_to_string_lookup_table[idx].second);
|
2022-11-14 14:17:17 +00:00
|
|
|
buf.write('.');
|
|
|
|
idx = ip & 0xFF;
|
2022-12-07 21:29:17 +00:00
|
|
|
buf.write(one_byte_to_string_lookup_table[idx].first, one_byte_to_string_lookup_table[idx].second);
|
2022-11-14 14:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeIPv6Text(const IPv6 & ip, WriteBuffer & buf)
|
|
|
|
{
|
2022-11-15 00:46:15 +00:00
|
|
|
char addr[IPV6_MAX_TEXT_LENGTH + 1] {};
|
|
|
|
char * paddr = addr;
|
|
|
|
|
|
|
|
formatIPv6(reinterpret_cast<const unsigned char *>(&ip), paddr);
|
2022-11-18 20:10:27 +00:00
|
|
|
buf.write(addr, paddr - addr - 1);
|
2022-11-14 14:17:17 +00:00
|
|
|
}
|
2017-06-30 20:30:13 +00:00
|
|
|
|
2018-08-24 07:30:53 +00:00
|
|
|
void writeException(const Exception & e, WriteBuffer & buf, bool with_stack_trace)
|
2012-05-08 05:42:05 +00:00
|
|
|
{
|
2023-05-26 14:04:44 +00:00
|
|
|
writeBinaryLittleEndian(e.code(), buf);
|
2012-05-08 05:42:05 +00:00
|
|
|
writeBinary(String(e.name()), buf);
|
2022-05-03 13:13:47 +00:00
|
|
|
writeBinary(e.displayText() + getExtraExceptionInfo(e), buf);
|
2018-08-24 07:30:53 +00:00
|
|
|
|
|
|
|
if (with_stack_trace)
|
2020-01-02 06:56:53 +00:00
|
|
|
writeBinary(e.getStackTraceString(), buf);
|
2018-08-24 07:30:53 +00:00
|
|
|
else
|
|
|
|
writeBinary(String(), buf);
|
2012-05-08 05:42:05 +00:00
|
|
|
|
2020-01-02 06:56:53 +00:00
|
|
|
bool has_nested = false;
|
2012-05-08 05:42:05 +00:00
|
|
|
writeBinary(has_nested, buf);
|
|
|
|
}
|
2020-03-05 14:55:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// The same, but quotes apply only if there are characters that do not match the identifier without quotes
|
|
|
|
template <typename F>
|
2022-07-15 11:15:46 +00:00
|
|
|
static inline void writeProbablyQuotedStringImpl(StringRef s, WriteBuffer & buf, F && write_quoted_string)
|
2020-03-05 14:55:53 +00:00
|
|
|
{
|
2022-07-17 15:22:12 +00:00
|
|
|
if (isValidIdentifier(s.toView())
|
2021-12-09 20:51:52 +00:00
|
|
|
/// This are valid identifiers but are problematic if present unquoted in SQL query.
|
|
|
|
&& !(s.size == strlen("distinct") && 0 == strncasecmp(s.data, "distinct", strlen("distinct")))
|
2024-02-17 09:11:59 +00:00
|
|
|
&& !(s.size == strlen("all") && 0 == strncasecmp(s.data, "all", strlen("all")))
|
|
|
|
&& !(s.size == strlen("table") && 0 == strncasecmp(s.data, "table", strlen("table"))))
|
2021-12-09 20:51:52 +00:00
|
|
|
{
|
2020-08-08 16:34:35 +00:00
|
|
|
writeString(s, buf);
|
2021-12-09 20:51:52 +00:00
|
|
|
}
|
2020-03-05 14:55:53 +00:00
|
|
|
else
|
2020-08-08 16:34:35 +00:00
|
|
|
write_quoted_string(s, buf);
|
2020-03-05 14:55:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 11:15:46 +00:00
|
|
|
void writeProbablyBackQuotedString(StringRef s, WriteBuffer & buf)
|
2020-03-05 14:55:53 +00:00
|
|
|
{
|
2024-05-09 01:58:34 +00:00
|
|
|
writeProbablyQuotedStringImpl(s, buf, [](StringRef s_, WriteBuffer & buf_) { writeBackQuotedString(s_, buf_); });
|
2020-03-05 14:55:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 11:15:46 +00:00
|
|
|
void writeProbablyDoubleQuotedString(StringRef s, WriteBuffer & buf)
|
2020-03-05 14:55:53 +00:00
|
|
|
{
|
2024-05-09 01:58:34 +00:00
|
|
|
writeProbablyQuotedStringImpl(s, buf, [](StringRef s_, WriteBuffer & buf_) { writeDoubleQuotedString(s_, buf_); });
|
2020-03-05 14:55:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 11:15:46 +00:00
|
|
|
void writeProbablyBackQuotedStringMySQL(StringRef s, WriteBuffer & buf)
|
2020-03-05 14:55:53 +00:00
|
|
|
{
|
2024-05-09 01:58:34 +00:00
|
|
|
writeProbablyQuotedStringImpl(s, buf, [](StringRef s_, WriteBuffer & buf_) { writeBackQuotedStringMySQL(s_, buf_); });
|
2020-03-05 14:55:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 19:07:38 +00:00
|
|
|
void writePointerHex(const void * ptr, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
writeString("0x", buf);
|
|
|
|
char hex_str[2 * sizeof(ptr)];
|
|
|
|
writeHexUIntLowercase(reinterpret_cast<uintptr_t>(ptr), hex_str);
|
|
|
|
buf.write(hex_str, 2 * sizeof(ptr));
|
|
|
|
}
|
|
|
|
|
2023-09-28 12:23:13 +00:00
|
|
|
String fourSpaceIndent(size_t indent)
|
|
|
|
{
|
|
|
|
return std::string(indent * 4, ' ');
|
|
|
|
}
|
2010-06-04 18:25:25 +00:00
|
|
|
}
|