libpqxx
stream_to.hxx
1
13#ifndef PQXX_H_STREAM_TO
14#define PQXX_H_STREAM_TO
15
16#include "pqxx/compiler-public.hxx"
17#include "pqxx/compiler-internal-pre.hxx"
18#include "pqxx/transaction_base.hxx"
19#include "pqxx/stream_base.hxx"
20#include "pqxx/stream_from.hxx"
21#include "pqxx/internal/type_utils.hxx"
22
23#include <string>
24
25
26namespace pqxx
27{
28
30
59class PQXX_LIBEXPORT stream_to : public stream_base
60{
61public:
63
70 stream_to(transaction_base &, const std::string &table_name);
71
73 template<typename Columns> stream_to(
75 const std::string &table_name,
76 const Columns& columns
77 );
78
80 template<typename Iter> stream_to(
82 const std::string &table_name,
83 Iter columns_begin,
84 Iter columns_end
85 );
86
87 ~stream_to() noexcept;
88
90
96 void complete() override;
97
99
106 template<typename Tuple> stream_to & operator<<(const Tuple &);
107
109
114
115private:
117 void write_raw_line(const std::string &);
118
119 void set_up(transaction_base &, const std::string &table_name);
120 void set_up(
122 const std::string &table_name,
123 const std::string &columns
124 );
125
126 void close() override;
127};
128
129
130template<typename Columns> inline stream_to::stream_to(
132 const std::string &table_name,
133 const Columns& columns
134) : stream_to{
135 tb,
136 table_name,
137 std::begin(columns),
138 std::end(columns)
139}
140{}
141
142
143template<typename Iter> inline stream_to::stream_to(
145 const std::string &table_name,
146 Iter columns_begin,
147 Iter columns_end
148) :
149 namedclass{"stream_from", table_name},
150 stream_base{tb}
151{
152 set_up(
153 tb,
154 table_name,
155 columnlist(columns_begin, columns_end)
156 );
157}
158
159
160namespace internal
161{
162
163class PQXX_LIBEXPORT TypedCopyEscaper
164{
165 static std::string escape(const std::string &);
166public:
167 template<typename T> std::string operator()(const T* t) const
168 {
169 return string_traits<T>::is_null(*t) ? "\\N" : escape(to_string(*t));
170 }
171};
172
173// Explicit specialization so we don't need a string_traits<> for nullptr_t
174template<> inline std::string TypedCopyEscaper::operator()<std::nullptr_t>(
175 const std::nullptr_t*
176) const
177{ return "\\N"; }
178
179} // namespace pqxx::internal
180
181
182template<typename Tuple> stream_to & stream_to::operator<<(const Tuple &t)
183{
184 write_raw_line(separated_list("\t", t, internal::TypedCopyEscaper()));
185 return *this;
186}
187
188} // namespace pqxx
189
190
191#include "pqxx/compiler-internal-post.hxx"
192#endif
STL namespace.
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
std::basic_ostream< CHAR > & operator<<(std::basic_ostream< CHAR > &S, const field &F)
Write a result field to any type of stream.
Definition: field.hxx:355
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:451
std::string separated_list(const std::string &sep, ITER begin, ITER end, ACCESS access)
Represent sequence of values as a string, joined by a given separator.
Definition: util.hxx:95
std::string escape(const std::string &s, const std::string &null)
Definition: tablewriter.cxx:131
Traits class for use in string conversions.
Definition: strconv.hxx:51
Definition: stream_base.hxx:29
static std::string columnlist(const C &)
Definition: stream_base.hxx:48
Efficiently pull data directly out of a table.
Definition: stream_from.hxx:30
Efficiently write data directly to a database table.
Definition: stream_to.hxx:60
stream_to(transaction_base &, const std::string &table_name)
Create a stream, without specifying columns.
Definition: stream_to.cxx:18
stream_to & operator<<(const Tuple &)
Insert a row of data.
Definition: stream_to.hxx:182
Definition: stream_to.hxx:164
std::string operator()(const T *t) const
Definition: stream_to.hxx:167
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:138