aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libpqxx/src/row.cxx
blob: 3ae9a19a81c30766778170e0ff6380004ea9fecf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/** Implementation of the pqxx::result class and support classes.
 *
 * pqxx::result represents the set of result rows from a database query.
 *
 * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
 *
 * See COPYING for copyright license.  If you did not receive a file called
 * COPYING with this source code, please notify the distributor of this mistake,
 * or contact the author.
 */
#include "pqxx/compiler-internal.hxx"

#include <cstdlib>
#include <cstring>

extern "C"
{
#include "libpq-fe.h"
}

#include "pqxx/except"
#include "pqxx/result"

#include "pqxx/internal/gates/result-row.hxx"


pqxx::row::row(result r, size_t i) noexcept :
  m_result{r},
  m_index{long(i)},
  m_end{internal::gate::result_row(r) ? r.columns() : 0}
{
}


pqxx::row::const_iterator pqxx::row::begin() const noexcept
{
  return const_iterator{*this, m_begin};
}


pqxx::row::const_iterator pqxx::row::cbegin() const noexcept
{
  return begin();
}


pqxx::row::const_iterator pqxx::row::end() const noexcept
{
  return const_iterator{*this, m_end};
}


pqxx::row::const_iterator pqxx::row::cend() const noexcept
{
  return end();
}


pqxx::row::reference pqxx::row::front() const noexcept
{
  return field{*this, m_begin};
}


pqxx::row::reference pqxx::row::back() const noexcept
{
  return field{*this, m_end - 1};
}


pqxx::row::const_reverse_iterator pqxx::row::rbegin() const
{
  return const_reverse_row_iterator{end()};
}


pqxx::row::const_reverse_iterator pqxx::row::crbegin() const
{
  return rbegin();
}


pqxx::row::const_reverse_iterator pqxx::row::rend() const
{
  return const_reverse_row_iterator{begin()};
}


pqxx::row::const_reverse_iterator pqxx::row::crend() const
{
  return rend();
}


bool pqxx::row::operator==(const row &rhs) const noexcept
{
  if (&rhs == this) return true;
  const auto s = size();
  if (rhs.size() != s) return false;
  // TODO: Depends on how null is handled!
  for (size_type i=0; i<s; ++i) if ((*this)[i] != rhs[i]) return false;
  return true;
}


pqxx::row::reference pqxx::row::operator[](size_type i) const noexcept
{
  return field{*this, m_begin + i};
}


pqxx::row::reference pqxx::row::operator[](int i) const noexcept
{
  return operator[](size_type(i));
}


pqxx::row::reference pqxx::row::operator[](const char f[]) const
{
  return at(f);
}


pqxx::row::reference pqxx::row::operator[](const std::string &s) const
{
  return operator[](s.c_str());
}


pqxx::row::reference pqxx::row::at(int i) const
{
  return at(size_type(i));
}


pqxx::row::reference pqxx::row::at(const std::string &s) const
{
  return at(s.c_str());
}


void pqxx::row::swap(row &rhs) noexcept
{
  const auto i = m_index;
  const auto b= m_begin;
  const auto e = m_end;
  m_result.swap(rhs.m_result);
  m_index = rhs.m_index;
  m_begin = rhs.m_begin;
  m_end = rhs.m_end;
  rhs.m_index = i;
  rhs.m_begin = b;
  rhs.m_end = e;
}


pqxx::field pqxx::row::at(const char f[]) const
{
  return field{*this, m_begin + column_number(f)};
}


pqxx::field pqxx::row::at(pqxx::row::size_type i) const
{
  if (i >= size())
    throw range_error{"Invalid field number."};

  return operator[](i);
}


pqxx::oid pqxx::row::column_type(size_type ColNum) const
{
  return m_result.column_type(m_begin + ColNum);
}


pqxx::oid pqxx::row::column_table(size_type ColNum) const
{
  return m_result.column_table(m_begin + ColNum);
}


pqxx::row::size_type pqxx::row::table_column(size_type ColNum) const
{
  return m_result.table_column(m_begin + ColNum);
}


pqxx::row::size_type pqxx::row::column_number(const char ColName[]) const
{
  const auto n = m_result.column_number(ColName);
  if (n >= m_end)
    return result{}.column_number(ColName);
  if (n >= m_begin)
    return n - m_begin;

  const char *const AdaptedColName = m_result.column_name(n);
  for (auto i = m_begin; i < m_end; ++i)
    if (strcmp(AdaptedColName, m_result.column_name(i)) == 0)
      return i - m_begin;

  return result{}.column_number(ColName);
}


pqxx::row::size_type pqxx::result::column_number(const char ColName[]) const
{
  const int N = PQfnumber(
	const_cast<internal::pq::PGresult *>(m_data.get()), ColName);
  if (N == -1)
    throw argument_error{
	"Unknown column name: '" + std::string{ColName} + "'."};

  return row::size_type(N);
}


pqxx::row pqxx::row::slice(size_type Begin, size_type End) const
{
  if (Begin > End or End > size())
    throw range_error{"Invalid field range."};

  row result{*this};
  result.m_begin = m_begin + Begin;
  result.m_end = m_begin + End;
  return result;
}


bool pqxx::row::empty() const noexcept
{
  return m_begin == m_end;
}


pqxx::const_row_iterator pqxx::const_row_iterator::operator++(int)
{
  const_row_iterator old{*this};
  m_col++;
  return old;
}


pqxx::const_row_iterator pqxx::const_row_iterator::operator--(int)
{
  const_row_iterator old{*this};
  m_col--;
  return old;
}


pqxx::const_row_iterator
pqxx::const_reverse_row_iterator::base() const noexcept
{
  iterator_type tmp{*this};
  return ++tmp;
}


pqxx::const_reverse_row_iterator
pqxx::const_reverse_row_iterator::operator++(int)
{
  const_reverse_row_iterator tmp{*this};
  operator++();
  return tmp;
}


pqxx::const_reverse_row_iterator
pqxx::const_reverse_row_iterator::operator--(int)
{
  const_reverse_row_iterator tmp{*this};
  operator--();
  return tmp;
}