2014-11-15 19:00:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2018-12-26 06:03:18 +00:00
|
|
|
#include <functional>
|
2021-09-10 21:28:43 +00:00
|
|
|
|
2021-09-06 14:24:03 +00:00
|
|
|
|
2014-11-15 19:00:55 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2021-09-10 21:28:43 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/** Implements the ability to write and read data in/from WriteBuffer/ReadBuffer
|
|
|
|
* with the help of << and >> operators and also manipulators,
|
|
|
|
* providing a way of using, similar to iostreams.
|
2014-11-15 19:00:55 +00:00
|
|
|
*
|
2017-05-28 14:29:40 +00:00
|
|
|
* It is neither a subset nor an extension of iostreams.
|
2014-11-15 19:00:55 +00:00
|
|
|
*
|
2017-05-28 14:29:40 +00:00
|
|
|
* Example usage:
|
2014-11-15 19:00:55 +00:00
|
|
|
*
|
|
|
|
* DB::WriteBufferFromFileDescriptor buf(STDOUT_FILENO);
|
|
|
|
* buf << DB::double_quote << "Hello, world!" << '\n' << DB::flush;
|
|
|
|
*
|
2017-05-28 14:29:40 +00:00
|
|
|
* Outputs `char` type (usually it's Int8) as a symbol, not as a number.
|
2014-11-15 19:00:55 +00:00
|
|
|
*/
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Manipulators.
|
|
|
|
enum EscapeManip { escape }; /// For strings - escape special characters. In the rest, as usual.
|
|
|
|
enum QuoteManip { quote }; /// For strings, dates, datetimes - enclose in single quotes with escaping. In the rest, as usual.
|
|
|
|
enum DoubleQuoteManip { double_quote }; /// For strings, dates, datetimes - enclose in double quotes with escaping. In the rest, as usual.
|
|
|
|
enum BinaryManip { binary }; /// Output in binary format.
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2018-12-26 06:03:18 +00:00
|
|
|
struct EscapeManipWriteBuffer : std::reference_wrapper<WriteBuffer> { using std::reference_wrapper<WriteBuffer>::reference_wrapper; };
|
|
|
|
struct QuoteManipWriteBuffer : std::reference_wrapper<WriteBuffer> { using std::reference_wrapper<WriteBuffer>::reference_wrapper; };
|
|
|
|
struct DoubleQuoteManipWriteBuffer : std::reference_wrapper<WriteBuffer> { using std::reference_wrapper<WriteBuffer>::reference_wrapper; };
|
|
|
|
struct BinaryManipWriteBuffer : std::reference_wrapper<WriteBuffer> { using std::reference_wrapper<WriteBuffer>::reference_wrapper; };
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2018-12-26 06:03:18 +00:00
|
|
|
struct EscapeManipReadBuffer : std::reference_wrapper<ReadBuffer> { using std::reference_wrapper<ReadBuffer>::reference_wrapper; };
|
|
|
|
struct QuoteManipReadBuffer : std::reference_wrapper<ReadBuffer> { using std::reference_wrapper<ReadBuffer>::reference_wrapper; };
|
|
|
|
struct DoubleQuoteManipReadBuffer : std::reference_wrapper<ReadBuffer> { using std::reference_wrapper<ReadBuffer>::reference_wrapper; };
|
|
|
|
struct BinaryManipReadBuffer : std::reference_wrapper<ReadBuffer> { using std::reference_wrapper<ReadBuffer>::reference_wrapper; };
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2021-09-06 15:59:46 +00:00
|
|
|
inline WriteBuffer & operator<<(WriteBuffer & buf, const auto & x) { writeText(x, buf); return buf; }
|
|
|
|
inline WriteBuffer & operator<<(WriteBuffer & buf, const pcg32_fast & x) { PcgSerializer::serializePcg32(x, buf); return buf; }
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2018-12-26 06:03:18 +00:00
|
|
|
inline EscapeManipWriteBuffer operator<< (WriteBuffer & buf, EscapeManip) { return buf; }
|
|
|
|
inline QuoteManipWriteBuffer operator<< (WriteBuffer & buf, QuoteManip) { return buf; }
|
|
|
|
inline DoubleQuoteManipWriteBuffer operator<< (WriteBuffer & buf, DoubleQuoteManip) { return buf; }
|
|
|
|
inline BinaryManipWriteBuffer operator<< (WriteBuffer & buf, BinaryManip) { return buf; }
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2021-09-10 21:28:43 +00:00
|
|
|
template <typename T> WriteBuffer & operator<< (EscapeManipWriteBuffer buf, const T & x) { writeText(x, buf.get()); return buf; }
|
|
|
|
template <typename T> WriteBuffer & operator<< (QuoteManipWriteBuffer buf, const T & x) { writeQuoted(x, buf.get()); return buf; }
|
|
|
|
template <typename T> WriteBuffer & operator<< (DoubleQuoteManipWriteBuffer buf, const T & x) { writeDoubleQuoted(x, buf.get()); return buf; }
|
|
|
|
template <typename T> WriteBuffer & operator<< (BinaryManipWriteBuffer buf, const T & x) { writeBinary(x, buf.get()); return buf; }
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2021-09-06 15:59:46 +00:00
|
|
|
inline WriteBuffer & operator<< (EscapeManipWriteBuffer buf, const String & x) { writeEscapedString(x, buf); return buf; }
|
|
|
|
inline WriteBuffer & operator<< (EscapeManipWriteBuffer buf, std::string_view x) { writeEscapedString(x, buf); return buf; }
|
|
|
|
inline WriteBuffer & operator<< (EscapeManipWriteBuffer buf, StringRef x) { writeEscapedString(x, buf); return buf; }
|
|
|
|
inline WriteBuffer & operator<< (EscapeManipWriteBuffer buf, const char * x) { writeEscapedString(x, strlen(x), buf); return buf; }
|
2021-01-13 12:47:34 +00:00
|
|
|
|
2018-12-26 06:03:18 +00:00
|
|
|
inline WriteBuffer & operator<< (QuoteManipWriteBuffer buf, const char * x) { writeAnyQuotedString<'\''>(x, x + strlen(x), buf.get()); return buf; }
|
|
|
|
inline WriteBuffer & operator<< (DoubleQuoteManipWriteBuffer buf, const char * x) { writeAnyQuotedString<'"'>(x, x + strlen(x), buf.get()); return buf; }
|
|
|
|
inline WriteBuffer & operator<< (BinaryManipWriteBuffer buf, const char * x) { writeStringBinary(x, buf.get()); return buf; }
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The manipulator calls the WriteBuffer method `next` - this makes the buffer reset. For nested buffers, the reset is not recursive.
|
2014-11-15 19:00:55 +00:00
|
|
|
enum FlushManip { flush };
|
|
|
|
|
2017-12-01 17:49:12 +00:00
|
|
|
inline WriteBuffer & operator<< (WriteBuffer & buf, FlushManip) { buf.next(); return buf; }
|
2014-11-15 19:00:55 +00:00
|
|
|
|
|
|
|
|
2017-04-14 21:08:57 +00:00
|
|
|
template <typename T> ReadBuffer & operator>> (ReadBuffer & buf, T & x) { readText(x, buf); return buf; }
|
|
|
|
template <> inline ReadBuffer & operator>> (ReadBuffer & buf, String & x) { readString(x, buf); return buf; }
|
|
|
|
template <> inline ReadBuffer & operator>> (ReadBuffer & buf, char & x) { readChar(x, buf); return buf; }
|
2020-11-09 13:07:38 +00:00
|
|
|
template <> inline ReadBuffer & operator>> (ReadBuffer & buf, pcg32_fast & x) { PcgDeserializer::deserializePcg32(x, buf); return buf; }
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// If you specify a string literal for reading, this will mean - make sure there is a sequence of bytes and skip it.
|
2017-04-01 07:20:54 +00:00
|
|
|
inline ReadBuffer & operator>> (ReadBuffer & buf, const char * x) { assertString(x, buf); return buf; }
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2018-12-26 06:03:18 +00:00
|
|
|
inline EscapeManipReadBuffer operator>> (ReadBuffer & buf, EscapeManip) { return buf; }
|
|
|
|
inline QuoteManipReadBuffer operator>> (ReadBuffer & buf, QuoteManip) { return buf; }
|
|
|
|
inline DoubleQuoteManipReadBuffer operator>> (ReadBuffer & buf, DoubleQuoteManip) { return buf; }
|
|
|
|
inline BinaryManipReadBuffer operator>> (ReadBuffer & buf, BinaryManip) { return buf; }
|
2014-11-15 19:00:55 +00:00
|
|
|
|
2018-12-26 06:03:18 +00:00
|
|
|
template <typename T> ReadBuffer & operator>> (EscapeManipReadBuffer buf, T & x) { readText(x, buf.get()); return buf; }
|
|
|
|
template <typename T> ReadBuffer & operator>> (QuoteManipReadBuffer buf, T & x) { readQuoted(x, buf.get()); return buf; }
|
|
|
|
template <typename T> ReadBuffer & operator>> (DoubleQuoteManipReadBuffer buf, T & x) { readDoubleQuoted(x, buf.get()); return buf; }
|
|
|
|
template <typename T> ReadBuffer & operator>> (BinaryManipReadBuffer buf, T & x) { readBinary(x, buf.get()); return buf; }
|
2021-09-10 21:28:43 +00:00
|
|
|
|
2014-11-15 19:00:55 +00:00
|
|
|
}
|