libpqxx
tablewriter.hxx
1 
13 #ifndef PQXX_H_TABLEWRITER
14 #define PQXX_H_TABLEWRITER
15 
16 #include <iterator>
17 
18 #include "pqxx/compiler-public.hxx"
19 #include "pqxx/compiler-internal-pre.hxx"
20 
21 #include "pqxx/tablestream.hxx"
22 
23 
24 namespace pqxx
25 {
27 
31 class PQXX_LIBEXPORT tablewriter : public tablestream
32 {
33 public:
34  PQXX_DEPRECATED tablewriter(
36  const std::string &WName,
37  const std::string &Null=std::string{});
38  template<typename ITER>
39  PQXX_DEPRECATED tablewriter(
41  const std::string &WName,
42  ITER begincolumns,
43  ITER endcolumns);
44  template<typename ITER>
45  PQXX_DEPRECATED tablewriter(
47  const std::string &WName,
48  ITER begincolumns,
49  ITER endcolumns,
50  const std::string &Null);
51  ~tablewriter() noexcept;
52  template<typename IT> void insert(IT Begin, IT End);
53  template<typename TUPLE> void insert(const TUPLE &);
54  template<typename IT> void push_back(IT Begin, IT End);
55  template<typename TUPLE> void push_back(const TUPLE &);
56  template<typename SIZE> void reserve(SIZE) {}
57  template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
59  template<typename IT> std::string generate(IT Begin, IT End) const;
60  template<typename TUPLE> std::string generate(const TUPLE &) const;
61  virtual void complete() override;
62  void write_raw_line(const std::string &);
63 private:
64  void set_up(
66  const std::string &WName,
67  const std::string &Columns = std::string{});
68  PQXX_PRIVATE void writer_close();
69 };
70 } // namespace pqxx
71 
72 
73 namespace std
74 {
75 template<>
76  class back_insert_iterator<pqxx::tablewriter>
77 {
78 public:
79  using iterator_category = output_iterator_tag;
80 
81  explicit back_insert_iterator(pqxx::tablewriter &W) noexcept :
82  m_writer{&W} {}
83 
84  back_insert_iterator &
85  operator=(const back_insert_iterator &rhs) noexcept
86  {
87  m_writer = rhs.m_writer;
88  return *this;
89  }
90 
91  template<typename TUPLE>
92  back_insert_iterator &operator=(const TUPLE &T)
93  {
94  m_writer->insert(T);
95  return *this;
96  }
97 
98  back_insert_iterator &operator++() { return *this; }
99  back_insert_iterator &operator++(int) { return *this; }
100  back_insert_iterator &operator*() { return *this; }
101 
102 private:
103  pqxx::tablewriter *m_writer;
104 };
105 } // namespace std
106 
107 
108 namespace pqxx
109 {
110 template<typename ITER> inline tablewriter::tablewriter(
111  transaction_base &T,
112  const std::string &WName,
113  ITER begincolumns,
114  ITER endcolumns) :
115  namedclass{"tablewriter", WName},
116  tablestream{T, std::string{}}
117 {
118  set_up(T, WName, columnlist(begincolumns, endcolumns));
119 }
120 
121 
122 template<typename ITER> inline tablewriter::tablewriter(
123  transaction_base &T,
124  const std::string &WName,
125  ITER begincolumns,
126  ITER endcolumns,
127  const std::string &Null) :
128  namedclass{"tablewriter", WName},
129  tablestream{T, Null}
130 {
131  set_up(T, WName, columnlist(begincolumns, endcolumns));
132 }
133 
134 
135 namespace internal
136 {
137 PQXX_LIBEXPORT std::string escape(
138  const std::string &s,
139  const std::string &null);
140 
141 inline std::string escape_any(
142  const std::string &s,
143  const std::string &null)
144 { return escape(s, null); }
145 
146 inline std::string escape_any(
147  const char s[],
148  const std::string &null)
149 { return s ? escape(std::string{s}, null) : "\\N"; }
150 
151 template<typename T> inline std::string escape_any(
152  const T &t,
153  const std::string &null)
154 { return escape(to_string(t), null); }
155 
156 
157 template<typename IT> class Escaper
158 {
159  const std::string &m_null;
160 public:
161  explicit Escaper(const std::string &null) : m_null{null} {}
162  std::string operator()(IT i) const { return escape_any(*i, m_null); }
163 };
164 }
165 
166 
167 template<typename IT>
168 inline std::string tablewriter::generate(IT Begin, IT End) const
169 {
170  return separated_list("\t", Begin, End, internal::Escaper<IT>{NullStr()});
171 }
172 template<typename TUPLE>
173 inline std::string tablewriter::generate(const TUPLE &T) const
174 {
175  return generate(std::begin(T), std::end(T));
176 }
177 
178 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
179 {
180  write_raw_line(generate(Begin, End));
181 }
182 
183 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
184 {
185  insert(std::begin(T), std::end(T));
186 }
187 
188 template<typename IT>
189 inline void tablewriter::push_back(IT Begin, IT End)
190 {
191  insert(Begin, End);
192 }
193 
194 template<typename TUPLE>
195 inline void tablewriter::push_back(const TUPLE &T)
196 {
197  insert(std::begin(T), std::end(T));
198 }
199 
200 template<typename TUPLE>
201 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
202 {
203  insert(T);
204  return *this;
205 }
206 
207 } // namespace pqxx
208 #include "pqxx/compiler-internal-post.hxx"
209 #endif
pqxx::internal::Escaper
Definition: tablewriter.hxx:157
pqxx::internal::Escaper::operator()
std::string operator()(IT i) const
Definition: tablewriter.hxx:162
pqxx::tablestream::NullStr
const std::string & NullStr() const
Definition: tablestream.hxx:36
std::back_insert_iterator< pqxx::tablewriter >::operator=
back_insert_iterator & operator=(const back_insert_iterator &rhs) noexcept
Definition: tablewriter.hxx:85
pqxx::operator<<
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
pqxx::tablereader::get_raw_line
bool get_raw_line(std::string &Line)
Definition: tablereader.cxx:55
pqxx::internal::Escaper::Escaper
Escaper(const std::string &null)
Definition: tablewriter.hxx:161
pqxx::internal
Private namespace for libpqxx's internal use; do not access.
Definition: connection_base.hxx:43
pqxx::tablewriter
Definition: tablewriter.hxx:31
pqxx::tablewriter::generate
std::string generate(IT Begin, IT End) const
Definition: tablewriter.hxx:168
pqxx::tablewriter::reserve
void reserve(SIZE)
Definition: tablewriter.hxx:56
pqxx::tablewriter::~tablewriter
~tablewriter() noexcept
Definition: tablewriter.cxx:33
pqxx::tablestream
Base class for obsolete tablereader/tablewriter classes.
Definition: tablestream.hxx:26
std::back_insert_iterator< pqxx::tablewriter >::back_insert_iterator
back_insert_iterator(pqxx::tablewriter &W) noexcept
Definition: tablewriter.hxx:81
pqxx::tablewriter::complete
virtual void complete() override
Definition: tablewriter.cxx:75
std::back_insert_iterator< pqxx::tablewriter >::operator=
back_insert_iterator & operator=(const TUPLE &T)
Definition: tablewriter.hxx:92
pqxx::separated_list
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::back_insert_iterator< pqxx::tablewriter >::operator++
back_insert_iterator & operator++(int)
Definition: tablewriter.hxx:99
pqxx::tablewriter::operator<<
tablewriter & operator<<(const TUPLE &)
Definition: tablewriter.hxx:201
pqxx::internal::escape_any
std::string escape_any(const std::string &s, const std::string &null)
Definition: tablewriter.hxx:141
pqxx::internal::number_to_digit
constexpr char number_to_digit(int i) noexcept
Definition: strconv.hxx:324
pqxx::internal::escape
std::string escape(const std::string &s, const std::string &null)
Definition: tablewriter.cxx:131
std::back_insert_iterator< pqxx::tablewriter >::operator*
back_insert_iterator & operator*()
Definition: tablewriter.hxx:100
std::back_insert_iterator< pqxx::tablewriter >::operator++
back_insert_iterator & operator++()
Definition: tablewriter.hxx:98
pqxx::tablewriter::insert
void insert(IT Begin, IT End)
Definition: tablewriter.hxx:178
pqxx::internal::namedclass
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:233
std::back_insert_iterator< pqxx::tablewriter >::iterator_category
output_iterator_tag iterator_category
Definition: tablewriter.hxx:79
pqxx::tablewriter::push_back
void push_back(IT Begin, IT End)
Definition: tablewriter.hxx:189
pqxx::to_string
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:451
pqxx::tablewriter::write_raw_line
void write_raw_line(const std::string &)
Definition: tablewriter.cxx:65
pqxx::tablereader
Definition: tablereader.hxx:29
pqxx::transaction_base
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:136
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
pqxx::tablewriter::tablewriter
tablewriter(transaction_base &, const std::string &WName, const std::string &Null=std::string{})
Definition: tablewriter.cxx:22