src/relpipe-in-csv.cpp
branchv_0
changeset 3 d7907be4cc40
parent 2 e83895da3e8f
child 10 1ae185cac1f3
equal deleted inserted replaced
2:e83895da3e8f 3:d7907be4cc40
    58 		return true;
    58 		return true;
    59 	} else if (ch == '\n') {
    59 	} else if (ch == '\n') {
    60 		lastInRecord = true;
    60 		lastInRecord = true;
    61 		return true;
    61 		return true;
    62 	} else if (ch == '\r') {
    62 	} else if (ch == '\r') {
    63 		currentValue << ch;
    63 		input.get(ch);
    64 		if (ch == '\n') {
    64 		if (ch == '\n') {
    65 			lastInRecord = true;
    65 			lastInRecord = true;
    66 			return true;
    66 			return true;
    67 		} else {
    67 		} else {
    68 			throw RelpipeWriterException(L"Crazy carriage stuck during journey");
    68 			throw RelpipeWriterException(L"Crazy carriage stuck during journey");
    95 		if (headerDone) {
    95 		if (headerDone) {
    96 			writer->writeAttribute(convertor.from_bytes(currentValue.str()));
    96 			writer->writeAttribute(convertor.from_bytes(currentValue.str()));
    97 		} else {
    97 		} else {
    98 			AttributeMetadata am;
    98 			AttributeMetadata am;
    99 			am.attributeName = convertor.from_bytes(currentValue.str());
    99 			am.attributeName = convertor.from_bytes(currentValue.str());
   100 			am.typeId = TypeId::STRING; // TODO: support also other types passed as CLI parameters
   100 			am.typeId = TypeId::STRING;
   101 			metadata.push_back(am);
   101 			metadata.push_back(am);
   102 			if (lastInRecord) {
   102 			if (lastInRecord) {
       
   103 
       
   104 				/*
       
   105 				 * Usage (simple syntax):
       
   106 				 * relpipe-in-csv → default relation name, attribute names on the first line, all types are string
       
   107 				 * relpipe-in-csv my_relation → custom relation name
       
   108 				 * relpipe-in-csv my_relation a b c → custom relation name, custom attribute names (a,b,c), first line contains data
       
   109 				 * 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
       
   110 				 */
       
   111 
       
   112 				vector<string_t> firstLine;
       
   113 				if (args.size() == (1 + metadata.size())) {
       
   114 					for (int i = 0; i < metadata.size(); i++) {
       
   115 						firstLine.push_back(metadata[i].attributeName);
       
   116 						metadata[i].attributeName = args[1 + i];
       
   117 					}
       
   118 				} else if (args.size() == (1 + 2 * metadata.size())) {
       
   119 					for (int i = 0; i < metadata.size(); i++) {
       
   120 						firstLine.push_back(metadata[i].attributeName);
       
   121 						metadata[i].attributeName = args[1 + i * 2];
       
   122 						metadata[i].typeId = writer->toTypeId(args[1 + i * 2 + 1]);
       
   123 					}
       
   124 				}
       
   125 
   103 				headerDone = true;
   126 				headerDone = true;
   104 				writer->startRelation(args.size() > 0 ? args[0] : L"csv", metadata, true);
   127 				writer->startRelation(args.size() > 0 ? args[0] : L"csv", metadata, true);
       
   128 				if (firstLine.size()) {
       
   129 					for (string_t value : firstLine) writer->writeAttribute(value);
       
   130 				}
   105 			}
   131 			}
   106 		}
   132 		}
   107 
   133 
   108 		currentValue.str("");
   134 		currentValue.str("");
   109 		currentValue.clear();
   135 		currentValue.clear();