2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2017-06-30 20:30:13 +00:00
|
|
|
#include <inttypes.h>
|
2017-09-07 21:04:48 +00:00
|
|
|
#include <Common/hex.h>
|
|
|
|
|
2010-06-04 18:25:25 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-07-14 15:08:01 +00:00
|
|
|
template <typename IteratorSrc, typename IteratorDst>
|
2017-07-21 06:55:33 +00:00
|
|
|
void formatHex(IteratorSrc src, IteratorDst dst, const 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void formatUUID(const UInt8 * src16, UInt8 * dst36)
|
|
|
|
{
|
|
|
|
formatHex(&src16[0], &dst36[0], 4);
|
|
|
|
dst36[8] = '-';
|
|
|
|
formatHex(&src16[4], &dst36[9], 2);
|
|
|
|
dst36[13] = '-';
|
|
|
|
formatHex(&src16[6], &dst36[14], 2);
|
|
|
|
dst36[18] = '-';
|
|
|
|
formatHex(&src16[8], &dst36[19], 2);
|
|
|
|
dst36[23] = '-';
|
|
|
|
formatHex(&src16[10], &dst36[24], 6);
|
|
|
|
}
|
|
|
|
|
2017-07-14 15:08:01 +00:00
|
|
|
/** Function used when byte ordering is important when parsing uuid
|
|
|
|
* ex: When we create an UUID type
|
|
|
|
*/
|
|
|
|
void formatUUID(std::reverse_iterator<const UInt8 *> src16, UInt8 * dst36)
|
2017-06-30 20:30:13 +00:00
|
|
|
{
|
2017-07-14 15:08:01 +00:00
|
|
|
formatHex(src16 + 8, &dst36[0], 4);
|
2017-06-30 20:30:13 +00:00
|
|
|
dst36[8] = '-';
|
2017-07-14 15:08:01 +00:00
|
|
|
formatHex(src16 + 12, &dst36[9], 2);
|
2017-06-30 20:30:13 +00:00
|
|
|
dst36[13] = '-';
|
2017-07-14 15:08:01 +00:00
|
|
|
formatHex(src16 + 14, &dst36[14], 2);
|
2017-06-30 20:30:13 +00:00
|
|
|
dst36[18] = '-';
|
2017-07-14 15:08:01 +00:00
|
|
|
formatHex(src16, &dst36[19], 2);
|
2017-06-30 20:30:13 +00:00
|
|
|
dst36[23] = '-';
|
2017-07-14 15:08:01 +00:00
|
|
|
formatHex(src16 + 2, &dst36[24], 6);
|
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
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeBinary(e.code(), buf);
|
|
|
|
writeBinary(String(e.name()), buf);
|
|
|
|
writeBinary(e.displayText(), 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;
|
2017-04-01 07:20:54 +00:00
|
|
|
writeBinary(has_nested, buf);
|
2012-05-08 05:42:05 +00:00
|
|
|
}
|
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>
|
|
|
|
static inline void writeProbablyQuotedStringImpl(const StringRef & s, WriteBuffer & buf, F && write_quoted_string)
|
|
|
|
{
|
2020-08-08 16:34:35 +00:00
|
|
|
if (isValidIdentifier(std::string_view{s}))
|
|
|
|
writeString(s, buf);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void writeProbablyBackQuotedString(const StringRef & s, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
writeProbablyQuotedStringImpl(s, buf, [](const StringRef & s_, WriteBuffer & buf_) { return writeBackQuotedString(s_, buf_); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeProbablyDoubleQuotedString(const StringRef & s, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
writeProbablyQuotedStringImpl(s, buf, [](const StringRef & s_, WriteBuffer & buf_) { return writeDoubleQuotedString(s_, buf_); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeProbablyBackQuotedStringMySQL(const StringRef & s, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
writeProbablyQuotedStringImpl(s, buf, [](const StringRef & s_, WriteBuffer & buf_) { return writeBackQuotedStringMySQL(s_, buf_); });
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2010-06-04 18:25:25 +00:00
|
|
|
}
|