src/AttributeFinder.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 25 Apr 2021 18:47:57 +0200
branchv_0
changeset 89 25a11859975b
parent 32 bccda5688d71
permissions -rw-r--r--
streamlet examples: QR: rename qr to qr-decode + simplify Makefile

/**
 * 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 string_t& relationName, 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 string_t& relationName, const RequestedField& field) {
		// TODO: better handling of null values (when null values are supported by the format specification)
		for (AttributeMetadata m : toMetadata(writer, relationName, 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 relationName default one or set by the user
	 * @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 string_t& relationName, 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 string_t& relationName, const RequestedField& field) {
		if (currentFileExists) writeFieldOfExistingFile(writer, relationName, field);
		else writeEmptyField(writer, relationName, 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() {
	}
};

}
}
}