src/relpipe-in-csv.cpp
branchv_0
changeset 3 d7907be4cc40
parent 2 e83895da3e8f
child 10 1ae185cac1f3
--- a/src/relpipe-in-csv.cpp	Wed Jan 09 23:18:16 2019 +0100
+++ b/src/relpipe-in-csv.cpp	Thu Jan 10 00:10:19 2019 +0100
@@ -60,7 +60,7 @@
 		lastInRecord = true;
 		return true;
 	} else if (ch == '\r') {
-		currentValue << ch;
+		input.get(ch);
 		if (ch == '\n') {
 			lastInRecord = true;
 			return true;
@@ -97,11 +97,37 @@
 		} else {
 			AttributeMetadata am;
 			am.attributeName = convertor.from_bytes(currentValue.str());
-			am.typeId = TypeId::STRING; // TODO: support also other types passed as CLI parameters
+			am.typeId = TypeId::STRING;
 			metadata.push_back(am);
 			if (lastInRecord) {
+
+				/*
+				 * Usage (simple syntax):
+				 * relpipe-in-csv → default relation name, attribute names on the first line, all types are string
+				 * relpipe-in-csv my_relation → custom relation name
+				 * relpipe-in-csv my_relation a b c → custom relation name, custom attribute names (a,b,c), first line contains data
+				 * relpipe-in-csv my_relation a integer b string c boolean → custom relation name, custom attribute names (a,b,c), custom types (integer,string,boolean), first line contains data
+				 */
+
+				vector<string_t> firstLine;
+				if (args.size() == (1 + metadata.size())) {
+					for (int i = 0; i < metadata.size(); i++) {
+						firstLine.push_back(metadata[i].attributeName);
+						metadata[i].attributeName = args[1 + i];
+					}
+				} else if (args.size() == (1 + 2 * metadata.size())) {
+					for (int i = 0; i < metadata.size(); i++) {
+						firstLine.push_back(metadata[i].attributeName);
+						metadata[i].attributeName = args[1 + i * 2];
+						metadata[i].typeId = writer->toTypeId(args[1 + i * 2 + 1]);
+					}
+				}
+
 				headerDone = true;
 				writer->startRelation(args.size() > 0 ? args[0] : L"csv", metadata, true);
+				if (firstLine.size()) {
+					for (string_t value : firstLine) writer->writeAttribute(value);
+				}
 			}
 		}