diff -r 62eac7ab4cf4 -r d44ed75822e7 src/AttributeFinder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/AttributeFinder.h Wed Jan 16 17:23:05 2019 +0100 @@ -0,0 +1,97 @@ +/** + * Relational pipes + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include + +#include +#include +#include + +#include "RequestedField.h" + +namespace relpipe { +namespace in { +namespace filesystem { + +namespace fs = std::filesystem; +using namespace relpipe::writer; + +class AttributeFinder { +public: + + /** + * Single requested fields might generate multiple attributes in the relation. + * But usually it is 1:1. + * @param field requested field from the user (usually from CLI arguments) + * @return attribute metadata to be used in the RelationalWriter.startRelation() + */ + virtual vector toMetadata(const RequestedField& field) = 0; + + /** + * Writing of the record for current file is starting. + * Following writeField() calls are related to this file. + * @param file + */ + virtual void startFile(const fs::path& file) = 0; + + /** + * Writing of the record for current file is finished. All resources linked to this file should be released. + */ + virtual void endFile() = 0; + + /** + * Writes field attribute(s). The attribute count must match with count of AttributeMetadata returned in toMetadata(). + * @param writer + * @param field + */ + virtual void writeField(RelationalWriter* writer, const RequestedField& field) = 0; + + /** + * Writes empty attribute(s) in case of non-existent file or an error. + * The attribute count must match with count of AttributeMetadata returned in toMetadata(). + * @param writer + * @param field + */ + virtual void writeEmptyField(RelationalWriter* writer, const RequestedField& field) { + // TODO: better handling of null values (when null values are supported by the format specification) + for (AttributeMetadata m : toMetadata(field)) { + switch (m.typeId) { + case TypeId::BOOLEAN: + writer->writeAttribute(L"false"); + break; + case TypeId::INTEGER: + writer->writeAttribute(L"0"); + break; + case TypeId::STRING: + writer->writeAttribute(L""); + break; + default: + throw RelpipeWriterException(L"Unsupported attribute type in writeEmptyField()."); + } + } + } + + virtual ~AttributeFinder() { + } +}; + +} +} +} \ No newline at end of file