src/GuileHandler.h
branchv_0
changeset 16 2dcc22b7a424
parent 15 051e58022783
child 18 3f46b784b617
equal deleted inserted replaced
15:051e58022783 16:2dcc22b7a424
   185 			default:
   185 			default:
   186 				throw cli::RelpipeCLIException(L"Unsupported type in writeGuileValueToAttribute()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
   186 				throw cli::RelpipeCLIException(L"Unsupported type in writeGuileValueToAttribute()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
   187 		}
   187 		}
   188 	}
   188 	}
   189 
   189 
       
   190 	/**
       
   191 	 * Read from the Guile variables and write to relational output stream.
       
   192 	 */
       
   193 	void writeCurrentRecord() {
       
   194 		for (auto attribute : currentWriterMetadata) writeGuileValueToAttribute(attribute);
       
   195 	}
       
   196 
       
   197 	void writeMoreRecords() {
       
   198 		while (scm_to_bool(evalGuileCode(currentRelationConfiguration->guileHasMoreRecords, SCM_BOOL_F))) writeCurrentRecord();
       
   199 	}
       
   200 
   190 public:
   201 public:
   191 
   202 
   192 	GuileHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration, const vector<string_t>& arguments) : relationalWriter(relationalWriter), configuration(configuration) {
   203 	GuileHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration, const vector<string_t>& arguments) : relationalWriter(relationalWriter), configuration(configuration) {
   193 	}
   204 	}
   194 
   205 
   195 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
   206 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
   196 		if (currentRelationConfiguration) {
   207 		if (currentRelationConfiguration) {
   197 			evalGuileCode(currentRelationConfiguration->guileAfterRecords);
   208 			evalGuileCode(currentRelationConfiguration->guileAfterRecords);
       
   209 			writeMoreRecords();
   198 			for (DefinitionRecipe definition : currentRelationConfiguration->definitions) undefineGuileVariable(definition.name);
   210 			for (DefinitionRecipe definition : currentRelationConfiguration->definitions) undefineGuileVariable(definition.name);
   199 		}
   211 		}
   200 		for (auto attribute : currentReaderMetadata) undefineGuileVariable(attribute.getAttributeName());
   212 		for (auto attribute : currentReaderMetadata) undefineGuileVariable(attribute.getAttributeName());
   201 
   213 
   202 		for (DefinitionRecipe definition : configuration.definitions) defineGuileVariable(definition);
   214 		for (DefinitionRecipe definition : configuration.definitions) defineGuileVariable(definition);
   238 
   250 
   239 			// TODO: > 0 ?:
   251 			// TODO: > 0 ?:
   240 			if (currentAttributeIndex > 0 && currentAttributeIndex % currentReaderMetadata.size() == 0) {
   252 			if (currentAttributeIndex > 0 && currentAttributeIndex % currentReaderMetadata.size() == 0) {
   241 				evalGuileCode(currentRelationConfiguration->guileForEach);
   253 				evalGuileCode(currentRelationConfiguration->guileForEach);
   242 				includeCurrentRecord = scm_to_bool(evalGuileCode(currentRelationConfiguration->guileWhere, SCM_BOOL_T));
   254 				includeCurrentRecord = scm_to_bool(evalGuileCode(currentRelationConfiguration->guileWhere, SCM_BOOL_T));
   243 				if (includeCurrentRecord && !currentRelationConfiguration->drop) for (auto attribute : currentWriterMetadata) writeGuileValueToAttribute(attribute);
   255 				if (includeCurrentRecord && !currentRelationConfiguration->drop) writeCurrentRecord();
   244 				includeCurrentRecord = false;
   256 				includeCurrentRecord = false;
       
   257 				writeMoreRecords();
   245 			}
   258 			}
   246 
   259 
   247 			currentAttributeIndex = currentAttributeIndex % currentReaderMetadata.size();
   260 			currentAttributeIndex = currentAttributeIndex % currentReaderMetadata.size();
   248 		} else {
   261 		} else {
   249 			relationalWriter->writeAttribute(value, type);
   262 			relationalWriter->writeAttribute(value, type);
   250 		}
   263 		}
   251 	}
   264 	}
   252 
   265 
   253 	void endOfPipe() {
   266 	void endOfPipe() {
   254 		if (currentRelationConfiguration) evalGuileCode(currentRelationConfiguration->guileAfterRecords);
   267 		if (currentRelationConfiguration) {
       
   268 			evalGuileCode(currentRelationConfiguration->guileAfterRecords);
       
   269 			writeMoreRecords();
       
   270 		}
   255 	}
   271 	}
   256 
   272 
   257 };
   273 };
   258 
   274 
   259 }
   275 }