diff --git a/Data/Makefile b/Data/Makefile index 308786ab0..f61d2111a 100644 --- a/Data/Makefile +++ b/Data/Makefile @@ -10,7 +10,7 @@ include $(POCO_BASE)/build/rules/global objects = AbstractBinder AbstractBinding AbstractExtraction AbstractExtractor \ AbstractPreparation AbstractPreparator ArchiveStrategy Transaction \ - Bulk Connector DataException Date DynamicLOB Limit JSONFormatter \ + Bulk Connector DataException Date DynamicLOB Limit JSONRowFormatter \ MetaColumn PooledSessionHolder PooledSessionImpl Position \ Range RecordSet Row RowFilter RowFormatter RowIterator \ SimpleRowFormatter Session SessionFactory SessionImpl \ diff --git a/Data/include/Poco/Data/JSONRowFormatter.h b/Data/include/Poco/Data/JSONRowFormatter.h index c25426498..fb0df9d3b 100644 --- a/Data/include/Poco/Data/JSONRowFormatter.h +++ b/Data/include/Poco/Data/JSONRowFormatter.h @@ -29,6 +29,51 @@ namespace Data { class Data_API JSONRowFormatter: public Poco::Data::RowFormatter /// Class for JSON formatting of data rows. + /// + /// Formatter can be configured to operate in four modes (and + /// certain combinations thereof) : + /// + /// - small (condensed mode, only array of values) + /// + /// Example: + /// { + /// [["Simpson", "Bart", "Springfield", 12], + /// ["Simpson", "Lisa", "Springfield", 10]] + /// } + /// + /// - row count (total row count provided) + /// + /// Example: + /// { + /// "count":2, + /// [["Simpson", "Bart", "Springfield", 12], + /// ["Simpson", "Lisa", "Springfield", 10]] + /// } + /// + /// - column names (column names provided as a string array) + /// + /// Example: + /// { + /// "names":["LastName", "FirstName", "Address", "Age"], + /// [["Simpson", "Bart", "Springfield", 12], + /// ["Simpson", "Lisa", "Springfield", 10]] + /// } + /// + /// - full (total row count, column names provided in every row of data) + /// + /// Example: + /// { + /// "count":2, + /// [ + /// {"LastName": "Simpson", "FirstName": "Bart", "Address": "Springfield", "Age": 12}, + /// {"LastName": "Simpson", "FirstName": "Lisa", "Address": "Springfield", "Age": 10} + /// ] + /// } + /// + /// Total row count will be specified by the Poco::DataRecordSet. Note, however, that this is + /// not possible to do accurately in case of result set paging. For those cases, there is + /// setTotalRowCount() member function, which allows to explicitly set the total row count. + /// If the total row count is preset on the formatter, the Data framework shall not interfere. { public: static const int JSON_FMT_MODE_SMALL = 1; diff --git a/Data/include/Poco/Data/SimpleRowFormatter.h b/Data/include/Poco/Data/SimpleRowFormatter.h index 9b9d10715..aa928f6e0 100644 --- a/Data/include/Poco/Data/SimpleRowFormatter.h +++ b/Data/include/Poco/Data/SimpleRowFormatter.h @@ -32,9 +32,6 @@ class Data_API SimpleRowFormatter: public RowFormatter /// A simple row formatting class. { public: - //typedef RowFormatter::NameVec NameVec; - //typedef RowFormatter::NameVecPtr NameVecPtr; - //typedef RowFormatter::ValueVec ValueVec; static const int DEFAULT_COLUMN_WIDTH = 16; static const int DEFAULT_SPACING = 1; diff --git a/Data/src/JSONRowFormatter.cpp b/Data/src/JSONRowFormatter.cpp index edf826833..ab90cc513 100644 --- a/Data/src/JSONRowFormatter.cpp +++ b/Data/src/JSONRowFormatter.cpp @@ -29,6 +29,12 @@ namespace Poco { namespace Data { +const int JSONRowFormatter::JSON_FMT_MODE_SMALL; +const int JSONRowFormatter::JSON_FMT_MODE_ROW_COUNT; +const int JSONRowFormatter::JSON_FMT_MODE_COLUMN_NAMES; +const int JSONRowFormatter::JSON_FMT_MODE_FULL; + + JSONRowFormatter::JSONRowFormatter(int mode) : RowFormatter("{", "]}"), _firstTime(true) { diff --git a/Data/src/RecordSet.cpp b/Data/src/RecordSet.cpp index c9bf671a5..503595698 100644 --- a/Data/src/RecordSet.cpp +++ b/Data/src/RecordSet.cpp @@ -301,11 +301,18 @@ bool RecordSet::moveLast() void RecordSet::setRowFormatter(RowFormatter::Ptr pRowFormatter) { - pRowFormatter->setTotalRowCount(static_cast(getTotalRowCount())); - Statement::setRowFormatter(pRowFormatter); - RowMap::iterator it = _rowMap.begin(); - RowMap::iterator end = _rowMap.end(); - for (; it != end; ++it) it->second->setFormatter(getRowFormatter()); + if (pRowFormatter) + { + if (pRowFormatter->getTotalRowCount() == RowFormatter::INVALID_ROW_COUNT) + pRowFormatter->setTotalRowCount(static_cast(getTotalRowCount())); + + Statement::setRowFormatter(pRowFormatter); + RowMap::iterator it = _rowMap.begin(); + RowMap::iterator end = _rowMap.end(); + for (; it != end; ++it) it->second->setFormatter(getRowFormatter()); + } + else + throw NullPointerException("Null RowFormatter in RecordSet."); } diff --git a/Data/testsuite/src/DataTest.cpp b/Data/testsuite/src/DataTest.cpp index ed68c4c2a..bec8145fc 100644 --- a/Data/testsuite/src/DataTest.cpp +++ b/Data/testsuite/src/DataTest.cpp @@ -1232,7 +1232,6 @@ void DataTest::testJSONRowFormatter() row1.setFormatter(new JSONRowFormatter(JSONRowFormatter::JSON_FMT_MODE_SMALL)); assert(row1.getFormatter().getMode() == RowFormatter::FORMAT_PROGRESSIVE); assert(row1.namesToString() == ""); - //std::cout << row1.valuesToString() << std::endl; assert(row1.valuesToString() == "[[0,\"1\",\"2007-03-13T08:12:15Z\",null,4]"); assert(row1.valuesToString() == ",[0,\"1\",\"2007-03-13T08:12:15Z\",null,4]"); @@ -1240,7 +1239,6 @@ void DataTest::testJSONRowFormatter() assert(row1.getFormatter().prefix() == "{\"count\":0,["); assert(row1.getFormatter().postfix() == "]}"); assert(row1.getFormatter().getMode() == RowFormatter::FORMAT_PROGRESSIVE); - std::cout << row1.namesToString() << std::endl; assert(row1.namesToString() == ""); assert(row1.valuesToString() == "{\"field0\":0,\"field1\":\"1\",\"field2\":\"2007-03-13T08:12:15Z\",\"field3\":null,\"field4\":4}"); assert(row1.valuesToString() == ",{\"field0\":0,\"field1\":\"1\",\"field2\":\"2007-03-13T08:12:15Z\",\"field3\":null,\"field4\":4}");