src/CSVCommand.cpp
branchv_0
changeset 21 22eb4838e8d0
parent 20 90ae67de2f68
child 22 3f6488171e34
equal deleted inserted replaced
20:90ae67de2f68 21:22eb4838e8d0
   132 void CSVCommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
   132 void CSVCommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
   133 	wstring_convert < codecvt_utf8<wchar_t>> convertor; // UTF-8 is required for CSV
   133 	wstring_convert < codecvt_utf8<wchar_t>> convertor; // UTF-8 is required for CSV
   134 	vector<AttributeMetadata> metadata;
   134 	vector<AttributeMetadata> metadata;
   135 	bool headerDone = false;
   135 	bool headerDone = false;
   136 	bool lastInRecord = false;
   136 	bool lastInRecord = false;
       
   137 	integer_t valueCount = 0;
   137 	stringstream currentValue;
   138 	stringstream currentValue;
   138 
   139 
   139 
   140 
   140 	while (readValue(input, currentValue, lastInRecord) && input.good()) {
   141 	while (readValue(input, currentValue, lastInRecord) && input.good()) {
   141 		if (headerDone) {
   142 		if (headerDone) {
   142 			writer->writeAttribute(convertor.from_bytes(currentValue.str()));
   143 			writer->writeAttribute(convertor.from_bytes(currentValue.str()));
       
   144 			valueCount++;
   143 		} else {
   145 		} else {
   144 			AttributeMetadata am;
   146 			AttributeMetadata am;
   145 			am.attributeName = convertor.from_bytes(currentValue.str());
   147 			am.attributeName = convertor.from_bytes(currentValue.str());
   146 			am.typeId = TypeId::STRING;
   148 			am.typeId = TypeId::STRING;
   147 			metadata.push_back(am);
   149 			metadata.push_back(am);
   171 		}
   173 		}
   172 
   174 
   173 		currentValue.str("");
   175 		currentValue.str("");
   174 		currentValue.clear();
   176 		currentValue.clear();
   175 	}
   177 	}
       
   178 
       
   179 	/**
       
   180 	 * RFC 4180:
       
   181 	 *  - Each line should contain the same number of fields throughout the file.
       
   182 	 *  - The last field in the record must not be followed by a comma.
       
   183 	 */
       
   184 	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()));
   176 }
   185 }
   177 
   186 
   178 }
   187 }
   179 }
   188 }
   180 }
   189 }