field group --exec, replaces --script and --hash, starts reusable sub-program that returns set of attributes for all records during its runtime
(no fork/exec for each record like with --script)
/**
* 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, version 3 of the License.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <filesystem>
#include <relpipe/writer/typedefs.h>
#include <relpipe/writer/AttributeMetadata.h>
#include <relpipe/writer/RelationalWriter.h>
#include "RequestedField.h"
namespace relpipe {
namespace in {
namespace filesystem {
namespace fs = std::filesystem;
using namespace relpipe::writer;
class AttributeFinder {
protected:
fs::path currentFile;
string currentFileRaw;
bool currentFileExists;
/**
* Writes field attribute(s). The attribute count must match with count of AttributeMetadata returned in toMetadata().
* @param writer
* @param field
*/
virtual void writeFieldOfExistingFile(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(writer, 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().");
}
}
}
public:
/**
* Single requested fields might generate multiple attributes in the relation.
* But usually it is 1:1.
* @param writer can be used for TypeId coversion from string_t
* @param field requested field from the user (usually from CLI arguments)
* @return attribute metadata to be used in the RelationalWriter.startRelation()
*/
virtual vector<AttributeMetadata> toMetadata(RelationalWriter* writer, const RequestedField& field) = 0;
/**
* Writing of the record for current file is starting.
* Following writeField() calls are related to this file.
* @param file path to the file
* @param fileRaw raw file name as it was on the input
*/
virtual void startFile(const fs::path& file, const string& fileRaw, bool exists) {
currentFile = file;
currentFileRaw = fileRaw;
currentFileExists = exists;
}
virtual void writeField(RelationalWriter* writer, const RequestedField& field) {
if (currentFileExists) writeFieldOfExistingFile(writer, field);
else writeEmptyField(writer, field);
}
/**
* Writing of the record for current file is finished. All resources linked to this file should be released.
*/
virtual void endFile() {
currentFile.clear();
currentFileRaw.clear();
}
virtual ~AttributeFinder() {
}
};
}
}
}