check total number of values and throw exception if it does not match the number of columns v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 13 Nov 2021 11:52:54 +0100
branchv_0
changeset 21 22eb4838e8d0
parent 20 90ae67de2f68
child 22 3f6488171e34
check total number of values and throw exception if it does not match the number of columns
src/CSVCommand.cpp
--- a/src/CSVCommand.cpp	Sun Apr 18 18:20:09 2021 +0200
+++ b/src/CSVCommand.cpp	Sat Nov 13 11:52:54 2021 +0100
@@ -134,12 +134,14 @@
 	vector<AttributeMetadata> metadata;
 	bool headerDone = false;
 	bool lastInRecord = false;
+	integer_t valueCount = 0;
 	stringstream currentValue;
 
 
 	while (readValue(input, currentValue, lastInRecord) && input.good()) {
 		if (headerDone) {
 			writer->writeAttribute(convertor.from_bytes(currentValue.str()));
+			valueCount++;
 		} else {
 			AttributeMetadata am;
 			am.attributeName = convertor.from_bytes(currentValue.str());
@@ -173,6 +175,13 @@
 		currentValue.str("");
 		currentValue.clear();
 	}
+
+	/**
+	 * RFC 4180:
+	 *  - Each line should contain the same number of fields throughout the file.
+	 *  - The last field in the record must not be followed by a comma.
+	 */
+	if (valueCount % metadata.size()) throw RelpipeWriterException(L"The total number of values " + std::to_wstring(valueCount) + L" does not match the number of declared columns " + std::to_wstring(metadata.size()));
 }
 
 }