src/RecfileHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 30 Mar 2019 01:12:42 +0100
branchv_0
changeset 0 9005fdd81bca
child 1 1b4ca23e5d04
permissions -rw-r--r--
project and code skeleton: text output

/**
 * 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 <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <regex>
#include <cassert>

#include <relpipe/reader/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderValueHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>

#include "RecfileWriter.h"

namespace relpipe {
namespace out {
namespace recfile {

using namespace relpipe::reader;

class RecfileHandler : public handlers::RelationalReaderValueHadler {
private:
	shared_ptr<RecfileWriter> recfileWriter;
	std::vector<TypeId> columnTypes;
	std::vector<string_t> columnTypeCodes;
	std::vector<string_t> columnNames;
	integer_t valueCount = 0;
	integer_t columnCount = 0;
	integer_t relationCount = 0;

public:

	RecfileHandler(std::ostream& output) : recfileWriter(new RecfileWriter(output)) {
	}

	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
		valueCount = 0;
		columnCount = 0;

		if (relationCount == 0) {
			recfileWriter->writeStartSequence(); // root
		} else {
			recfileWriter->writeEndSequence();
			recfileWriter->writeEndSequence();
		}
		relationCount++;
		recfileWriter->writeStartSequence(); // relation

		recfileWriter->writeString(name);


		recfileWriter->writeStartSequence(); // relation metadata
		columnCount = attributes.size();
		columnTypes.resize(columnCount);
		columnTypeCodes.resize(columnCount);
		columnNames.resize(columnCount);
		for (int i = 0; i < attributes.size(); i++) {
			columnNames[i] = attributes[i].getAttributeName();
			columnTypes[i] = attributes[i].getTypeId();
			columnTypeCodes[i] = attributes[i].getTypeName();
			
			recfileWriter->writeStartSequence(); // attribute-metadata
			recfileWriter->writeString(columnNames[i]);
			recfileWriter->writeString(columnTypeCodes[i]);
			recfileWriter->writeEndSequence();
		}
		recfileWriter->writeEndSequence();

	}

	void attribute(const void* value, const std::type_info& type) override {
		integer_t i = valueCount % columnCount;

		if (i == 0 && valueCount) recfileWriter->writeEndSequence();
		if (i == 0) recfileWriter->writeStartSequence();

		valueCount++;
		
		switch (columnTypes[i]) {
			case TypeId::BOOLEAN:
			{
				assert(type == typeid (boolean_t));
				auto* typedValue = static_cast<const boolean_t*> (value);
				recfileWriter->writeBoolean(*typedValue);
				break;
			}
			case TypeId::INTEGER:
			{
				assert(type == typeid (integer_t));
				auto* typedValue = static_cast<const integer_t*> (value);
				recfileWriter->writeInteger(*typedValue);
				break;
			}
			case TypeId::STRING:
			{
				assert(type == typeid (string_t));
				auto* typedValue = static_cast<const string_t*> (value);
				recfileWriter->writeString(*typedValue);
				break;
			}
			default:
				throw cli::RelpipeCLIException(L"Unsupported type in RecfileHandler.attribute()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
		}
		
	}

	void endOfPipe() {
		if (valueCount) recfileWriter->writeEndSequence();
		if (relationCount) recfileWriter->writeEndSequence();
		recfileWriter->writeEndSequence();
	}

};

}
}
}