Пример #1
0
Row& RecordSet::row(std::size_t pos)
{
	std::size_t rowCnt = rowCount();
	if (0 == rowCnt || pos > rowCnt - 1)
		throw RangeException("Invalid recordset row requested.");

	RowMap::const_iterator it = _rowMap.find(pos);
	Row* pRow = 0;
	std::size_t columns = columnCount();
	if (it == _rowMap.end())
	{
		if (_rowMap.size())
		{
			//reuse first row column names and sorting fields to save some memory 
			pRow = new Row(_rowMap.begin()->second->names(),
				_rowMap.begin()->second->getSortMap(),
				getRowFormatter());

			for (std::size_t col = 0; col < columns; ++col)
				pRow->set(col, value(col, pos));
		}
		else 
		{
			pRow = new Row;
			pRow->setFormatter(getRowFormatter());
			for (std::size_t col = 0; col < columns; ++col)
				pRow->append(metaColumn(static_cast<UInt32>(col)).name(), value(col, pos));
		}

		_rowMap.insert(RowMap::value_type(pos, pRow));
	}
	else 
	{
		pRow = it->second;
		poco_check_ptr (pRow);
	}

	return *pRow;
}
Пример #2
0
void DataTest::testRow()
{
	Row row;
	row.append("field0", 0);
	row.append("field1", 1);
	row.append("field2", 2);
	row.append("field3", 3);
	row.append("field4", 4);

	assert (row["field0"] == 0);
	assert (row["field1"] == 1);
	assert (row["field2"] == 2);
	assert (row["field3"] == 3);
	assert (row["field4"] == 4);

	assert (row[0] == 0);
	assert (row[1] == 1);
	assert (row[2] == 2);
	assert (row[3] == 3);
	assert (row[4] == 4);

	try
	{
		int i; i = row[5]; // to silence gcc
		fail ("must fail");
	}catch (RangeException&) {}

	try
	{
		int i; i = row["a bad name"]; // to silence gcc
		fail ("must fail");
	}catch (NotFoundException&) {}

	assert (5 == row.fieldCount());
	assert (row[0] == 0);
	assert (row["field0"] == 0);
	assert (row[1] == 1);
	assert (row["field1"] == 1);
	assert (row[2] == 2);
	assert (row["field2"] == 2);
	assert (row[3] == 3);
	assert (row["field3"] == 3);
	assert (row[4] == 4);
	assert (row["field4"] == 4);

	Row row2;

	row2.append("field0", 5);
	row2.append("field1", 4);
	row2.append("field2", 3);
	row2.append("field3", 2);
	row2.append("field4", 1);

	assert (row != row2);

	Row row3;

	row3.append("field0", 0);
	row3.append("field1", 1);
	row3.append("field2", 2);
	row3.append("field3", 3);
	row3.append("field4", 4);

	assert (row3 == row);
	assert (!(row < row3 | row3 < row));

	Row row4(row3.names());
	try
	{
		row4.set("badfieldname", 0);
		fail ("must fail");
	}catch (NotFoundException&) {}

	row4.set("field0", 0);
	row4.set("field1", 1);
	row4.set("field2", 2);
	row4.set("field3", 3);
	row4.set("field4", 4);
	assert (row3 == row4);
	try
	{
		row4.set(5, 0);
		fail ("must fail");
	}catch (RangeException&) {}
	row4.set("field0", 1);
	assert (row3 != row4);
	assert (row3 < row4);
}