libpqxx  7.0.2
stream_to.hxx
1 /* Definition of the pqxx::stream_to class.
2  *
3  * pqxx::stream_to enables optimized batch updates to a database table.
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/stream_to.hxx instead.
6  *
7  * Copyright (c) 2000-2020, Jeroen T. Vermeulen.
8  *
9  * See COPYING for copyright license. If you did not receive a file called
10  * COPYING with this source code, please notify the distributor of this
11  * mistake, or contact the author.
12  */
13 #ifndef PQXX_H_STREAM_TO
14 #define PQXX_H_STREAM_TO
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
18 
19 #include "pqxx/separated_list.hxx"
20 #include "pqxx/transaction_base.hxx"
21 
22 
23 namespace pqxx::internal
24 {
25 std::string PQXX_LIBEXPORT copy_string_escape(std::string_view);
26 
28 {
29  template<typename T> std::string operator()(T const *t) const
30  {
31  // gcc 9 complains when t is used only in one branch of the "if constexpr".
32  ignore_unused(t);
33  if constexpr (std::is_same_v<T, std::nullptr_t>)
34  return "\\N";
35  else
36  return (t == nullptr or is_null(*t)) ? "\\N" :
38  }
39 };
40 } // namespace pqxx::internal
41 
42 
43 namespace pqxx
44 {
46 
75 class PQXX_LIBEXPORT stream_to : internal::transactionfocus
76 {
77 public:
79 
86  stream_to(transaction_base &, std::string_view table_name);
87 
89  template<typename Columns>
90  stream_to(
91  transaction_base &, std::string_view table_name, Columns const &columns);
92 
94  template<typename Iter>
95  stream_to(
96  transaction_base &, std::string_view table_name, Iter columns_begin,
97  Iter columns_end);
98 
99  ~stream_to() noexcept;
100 
101  [[nodiscard]] operator bool() const noexcept { return not m_finished; }
102  [[nodiscard]] bool operator!() const noexcept { return m_finished; }
103 
105 
111  void complete();
112 
114 
121  template<typename Tuple> stream_to &operator<<(Tuple const &);
122 
124 
129 
130 private:
131  bool m_finished = false;
132 
134  void write_raw_line(std::string_view);
135 
136  void set_up(transaction_base &, std::string_view table_name);
137  void set_up(
138  transaction_base &, std::string_view table_name,
139  std::string const &columns);
140 };
141 
142 
143 template<typename Columns>
144 inline stream_to::stream_to(
145  transaction_base &tb, std::string_view table_name, Columns const &columns) :
146  stream_to{tb, table_name, std::begin(columns), std::end(columns)}
147 {}
148 
149 
150 template<typename Iter>
151 inline stream_to::stream_to(
152  transaction_base &tb, std::string_view table_name, Iter columns_begin,
153  Iter columns_end) :
154  namedclass{"stream_to", table_name},
156 {
157  set_up(tb, table_name, separated_list(",", columns_begin, columns_end));
158 }
159 
160 
161 template<typename Tuple> stream_to &stream_to::operator<<(Tuple const &t)
162 {
163  write_raw_line(separated_list("\t", t, internal::TypedCopyEscaper{}));
164  return *this;
165 }
166 } // namespace pqxx
167 
168 #include "pqxx/internal/compiler-internal-post.hxx"
169 #endif
bool operator!() const noexcept
Definition: stream_to.hxx:102
bool is_null(TYPE const &value)
Is value null?
Definition: strconv.hxx:286
Definition: transaction_base.hxx:42
std::string copy_string_escape(std::string_view)
Definition: stream_to.cxx:110
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
Definition: connection.hxx:59
Definition: stream_to.hxx:27
std::string separated_list(std::string_view sep, ITER begin, ITER end, ACCESS access)
Represent sequence of values as a string, joined by a given separator.
Definition: separated_list.hxx:40
std::string operator()(T const *t) const
Definition: stream_to.hxx:29
void ignore_unused(T &&)
Suppress compiler warning about an unused item.
Definition: util.hxx:43
Dedicated namespace for helper types related to prepared statements.
Definition: array.hxx:25
Efficiently pull data directly out of a table.
Definition: stream_from.hxx:30
std::basic_ostream< CHAR > & operator<<(std::basic_ostream< CHAR > &s, field const &value)
Write a result field to any type of stream.
Definition: field.hxx:347
Efficiently write data directly to a database table.
Definition: stream_to.hxx:75