do UNION ALL if multiple relations have same number of attributes of same types v_0 tip
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 27 Aug 2022 21:56:52 +0200
branchv_0
changeset 21 af4cb72127c1
parent 20 56aeec532d4f
do UNION ALL if multiple relations have same number of attributes of same types
src/CSVHandler.h
--- a/src/CSVHandler.h	Sat Dec 04 21:14:51 2021 +0100
+++ b/src/CSVHandler.h	Sat Aug 27 21:56:52 2022 +0200
@@ -48,6 +48,17 @@
 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding or generate CSV always in UTF-8 like XML?
 	std::vector<AttributeMetadata> firstAttributes;
 	integer_t valueCount = 0;
+
+	/**
+	 * @param a
+	 * @param b
+	 * @return true if relations have same number and types of attributes (names may differ)
+	 */
+	bool matches(const std::vector<AttributeMetadata>& a, const std::vector<AttributeMetadata>& b) {
+		if (a.size() != b.size()) return false;
+		for (int i = 0, limit = a.size(); i < limit; i++) if (a[i].getTypeId() != b[i].getTypeId()) return false;
+		return true;
+	}
 public:
 
 	CSVHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
@@ -57,9 +68,10 @@
 		if (firstAttributes.empty()) {
 			firstAttributes = attributes;
 			if (configuration.writeHeader) for (auto attr : attributes) attribute(configuration.writeTypes ? attr.getAttributeName() + L"::" + attr.getTypeName() : attr.getAttributeName());
+		} else if (matches(firstAttributes, attributes)) {
+			// do UNION ALL – just append the records
 		} else {
-			// TODO: UNION ALL if data types and attribute count matches
-			throw RelpipeCSVWriterException(L"Only a single relation can be converted to the CSV format.");
+			throw RelpipeCSVWriterException(L"To the CSV format we can convert only one relation or multiple relations that have same number of attributes of same types (relation and attribute names may differ – result is named after the first one).");
 		}
 	}