aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Data/src/Row.cpp
blob: d078536bd5f052bbecee9a6757439eca5440c49e (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//
// Row.cpp
//
// Library: Data
// Package: DataCore
// Module:  Row
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/Data/Row.h"
#include "Poco/Data/SimpleRowFormatter.h"
#include "Poco/String.h"
#include "Poco/Exception.h"


namespace Poco {
namespace Data {


std::ostream& operator << (std::ostream &os, const Row& row)
{
	os << row.valuesToString();
	return os;
}


Row::Row(): 
	_pNames(0),
	_pSortMap(new SortMap),
	_pFormatter(new SimpleRowFormatter)
{
}


Row::Row(NameVecPtr pNames,
	const RowFormatter::Ptr& pFormatter): _pNames(pNames)
{
	if (!_pNames) throw NullPointerException();
	init(0, pFormatter);
}


Row::Row(NameVecPtr pNames,
	const SortMapPtr& pSortMap,
	const RowFormatter::Ptr& pFormatter): _pNames(pNames)
{
	if (!_pNames) throw NullPointerException();
	init(pSortMap, pFormatter);
}


void Row::init(const SortMapPtr& pSortMap, const RowFormatter::Ptr& pFormatter)
{
	setFormatter(pFormatter);
	setSortMap(pSortMap);

	NameVec::size_type sz = _pNames->size();
	if (sz)
	{
		_values.resize(sz);
		// Row sortability in the strict weak ordering sense is 
		// an invariant, hence we must start with a zero here.
		// If null value is later retrieved from DB, the 
		// Var::empty() call should be used to empty
		// the corresponding Row value.
		_values[0] = 0;
		addSortField(0);
	}
}


Row::~Row()
{
}


Poco::Dynamic::Var& Row::get(std::size_t col)
{
	try
	{
		return _values.at(col);
	}
	catch (std::out_of_range& re)
	{
		throw RangeException(re.what());
	}
}


std::size_t Row::getPosition(const std::string& name)
{
	if (!_pNames)
		throw NullPointerException();

	NameVec::const_iterator it = _pNames->begin();
	NameVec::const_iterator end = _pNames->end();
	std::size_t col = 0;
	for (; it != end; ++it, ++col)
		if (0 == icompare(name, *it)) return col;
	
	throw NotFoundException(name);
}


void Row::addSortField(std::size_t pos)
{
	poco_assert (pos <= _values.size());

	SortMap::iterator it = _pSortMap->begin();
	SortMap::iterator end = _pSortMap->end();
	for (; it != end; ++it)
	{
		if (it->get<0>() == pos) return; 
	}

	ComparisonType ct;
	if (_values[pos].isEmpty())
	{
		ct = COMPARE_AS_EMPTY;
	}
	else if ((_values[pos].type() == typeid(Poco::Int8))   ||
		(_values[pos].type() == typeid(Poco::UInt8))  ||
		(_values[pos].type() == typeid(Poco::Int16))  ||
		(_values[pos].type() == typeid(Poco::UInt16)) ||
		(_values[pos].type() == typeid(Poco::Int32))  ||
		(_values[pos].type() == typeid(Poco::UInt32)) ||
		(_values[pos].type() == typeid(Poco::Int64))  ||
		(_values[pos].type() == typeid(Poco::UInt64)) ||
		(_values[pos].type() == typeid(bool)))
	{
		ct = COMPARE_AS_INTEGER;
	}
	else if ((_values[pos].type() == typeid(float)) ||
		(_values[pos].type() == typeid(double)))
	{
		ct = COMPARE_AS_FLOAT;
	}
	else
	{
		ct = COMPARE_AS_STRING;
	}

	_pSortMap->push_back(SortTuple(pos, ct));
}


void Row::addSortField(const std::string& name)
{
	addSortField(getPosition(name));
}


void Row::removeSortField(std::size_t pos)
{
	SortMap::iterator it = _pSortMap->begin();
	SortMap::iterator end = _pSortMap->end();
	for (; it != end; ++it)
	{
		if (it->get<0>() == pos)
		{
			_pSortMap->erase(it);
			return;
		}
	}
}


void Row::removeSortField(const std::string& name)
{
	removeSortField(getPosition(name));
}


void Row::replaceSortField(std::size_t oldPos, std::size_t newPos)
{
	poco_assert (oldPos <= _values.size());
	poco_assert (newPos <= _values.size());

	ComparisonType ct;

	if (_values[newPos].isEmpty())
	{
		ct = COMPARE_AS_EMPTY;
	}
	else if ((_values[newPos].type() == typeid(Poco::Int8)) ||
		(_values[newPos].type() == typeid(Poco::UInt8))     ||
		(_values[newPos].type() == typeid(Poco::Int16))     ||
		(_values[newPos].type() == typeid(Poco::UInt16))    ||
		(_values[newPos].type() == typeid(Poco::Int32))     ||
		(_values[newPos].type() == typeid(Poco::UInt32))    ||
		(_values[newPos].type() == typeid(Poco::Int64))     ||
		(_values[newPos].type() == typeid(Poco::UInt64))    ||
		(_values[newPos].type() == typeid(bool)))
	{
		ct = COMPARE_AS_INTEGER;
	}
	else if ((_values[newPos].type() == typeid(float)) ||
		(_values[newPos].type() == typeid(double)))
	{
		ct = COMPARE_AS_FLOAT;
	}
	else
	{
		ct = COMPARE_AS_STRING;
	}

	SortMap::iterator it = _pSortMap->begin();
	SortMap::iterator end = _pSortMap->end();
	for (; it != end; ++it)
	{
		if (it->get<0>() == oldPos)
		{
			*it = SortTuple(newPos, ct);
			return;
		}
	}

	throw NotFoundException("Field not found");
}


void Row::replaceSortField(const std::string& oldName, const std::string& newName)
{
	replaceSortField(getPosition(oldName), getPosition(newName));
}


void Row::resetSort()
{
	_pSortMap->clear();
	if (_values.size())	addSortField(0);
}


bool Row::isEqualSize(const Row& other) const
{
	return (other._values.size() == _values.size());
}


bool Row::isEqualType(const Row& other) const
{
	std::vector<Poco::Dynamic::Var>::const_iterator it = _values.begin();
	std::vector<Poco::Dynamic::Var>::const_iterator end = _values.end();
	for (int i = 0; it != end; ++it, ++i)
	{
		if (it->type() != other._values[i].type())
			return false;
	}

	return true;
}


bool Row::operator == (const Row& other) const
{
	if (!isEqualSize(other)) return false;
	if (!isEqualType(other)) return false;

	std::vector<Poco::Dynamic::Var>::const_iterator it = _values.begin();
	std::vector<Poco::Dynamic::Var>::const_iterator end = _values.end();
	for (int i = 0; it != end; ++it, ++i)
	{
		if ((*it).convert<std::string>() != other._values[i].convert<std::string>())
			return false;
	}

	return true;
}


bool Row::operator != (const Row& other) const
{
	return !(*this == other);
}


bool Row::operator < (const Row& other) const
{
	if (*_pSortMap != *other._pSortMap)
		throw InvalidAccessException("Rows compared have different sorting criteria.");

	SortMap::const_iterator it = _pSortMap->begin();
	SortMap::const_iterator end = _pSortMap->end();
	for (; it != end; ++it)
	{
		switch (it->get<1>())
		{
		case COMPARE_AS_EMPTY:
			return false;

		case COMPARE_AS_INTEGER:
			if (_values[it->get<0>()].convert<Poco::Int64>() < 
				other._values[it->get<0>()].convert<Poco::Int64>())
				return true;
			else if (_values[it->get<0>()].convert<Poco::Int64>() != 
				other._values[it->get<0>()].convert<Poco::Int64>())
				return false;
			break;

		case COMPARE_AS_FLOAT:
			if (_values[it->get<0>()].convert<double>() < 
				other._values[it->get<0>()].convert<double>())
				return true;
			else if (_values[it->get<0>()].convert<double>() !=
				other._values[it->get<0>()].convert<double>())
				return false;
			break;

		case COMPARE_AS_STRING:
			if (_values[it->get<0>()].convert<std::string>() < 
				other._values[it->get<0>()].convert<std::string>())
				return true;
			else if (_values[it->get<0>()].convert<std::string>() !=
				other._values[it->get<0>()].convert<std::string>())
				return false;
			break;

		default:
			throw IllegalStateException("Unknown comparison criteria.");
		}
	}

	return false;
}


void Row::setFormatter(const RowFormatter::Ptr& pFormatter)
{
	if (pFormatter.get())
		_pFormatter = pFormatter;
	else 
		_pFormatter = new SimpleRowFormatter;
}


void Row::setSortMap(const SortMapPtr& pSortMap)
{
	if (pSortMap.get())
		_pSortMap = pSortMap;
	else 
		_pSortMap = new SortMap;
}


const std::string& Row::namesToString() const
{
	if (!_pNames)
		throw NullPointerException();

	return _pFormatter->formatNames(names(), _nameStr);
}


void Row::formatNames() const
{
	if (!_pNames)
		throw NullPointerException();

	return _pFormatter->formatNames(names());
}


} } // namespace Poco::Data