libpqxx  7.0.6
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>
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>
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
pqxx::ignore_unused
void ignore_unused(T &&)
Suppress compiler warning about an unused item.
Definition: util.hxx:44
pqxx::stream_to::complete
void complete()
Complete the operation, and check for errors.
Definition: stream_to.cxx:99
pqxx::is_null
bool is_null(TYPE const &value)
Is value null?
Definition: strconv.hxx:286
pqxx::internal::copy_string_escape
std::string copy_string_escape(std::string_view)
Definition: stream_to.cxx:110
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
pqxx::stream_to::~stream_to
~stream_to() noexcept
Definition: stream_to.cxx:53
pqxx::stream_from::get_raw_line
bool get_raw_line(std::string &)
Definition: stream_from.cxx:80
pqxx::to_string
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
pqxx::internal
Private namespace for libpqxx's internal use; do not access.
Definition: connection.hxx:59
pqxx::stream_to::stream_to
stream_to(transaction_base &, std::string_view table_name)
Create a stream, without specifying columns.
Definition: stream_to.cxx:45
pqxx::transaction_base::exec0
result exec0(std::string const &query, std::string const &desc=std::string{})
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:251
pqxx::transaction_base
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:97
pqxx::stream_to::operator!
bool operator!() const noexcept
Definition: stream_to.hxx:102
pqxx::stream_from
Efficiently pull data directly out of a table.
Definition: stream_from.hxx:30
pqxx::internal::TypedCopyEscaper::operator()
std::string operator()(T const *t) const
Definition: stream_to.hxx:29
pqxx::separated_list
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
pqxx::stream_to::operator<<
stream_to & operator<<(Tuple const &)
Insert a row of data.
Definition: stream_to.hxx:161
pqxx::operator<<
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:349
pqxx::stream_to
Efficiently write data directly to a database table.
Definition: stream_to.hxx:75
pqxx::internal::transactionfocus
Definition: transaction_base.hxx:42
pqxx::internal::TypedCopyEscaper
Definition: stream_to.hxx:27