/**
* 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 <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 "ASN1Writer.h"
namespace relpipe {
namespace out {
namespace asn1 {
using namespace relpipe::reader;
class ASN1Handler : public handlers::RelationalReaderValueHandler {
private:
shared_ptr<ASN1Writer> asn1Writer;
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:
ASN1Handler(std::ostream& output) : asn1Writer(new ASN1Writer(output)) {
}
void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
valueCount = 0;
columnCount = 0;
if (relationCount == 0) {
asn1Writer->writeStartSequence(); // root
} else {
asn1Writer->writeEndSequence();
asn1Writer->writeEndSequence();
}
relationCount++;
asn1Writer->writeStartSequence(); // relation
asn1Writer->writeString(name);
asn1Writer->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();
asn1Writer->writeStartSequence(); // attribute-metadata
asn1Writer->writeString(columnNames[i]);
asn1Writer->writeString(columnTypeCodes[i]);
asn1Writer->writeEndSequence();
}
asn1Writer->writeEndSequence();
}
void attribute(const void* value, const std::type_info& type) override {
integer_t i = valueCount % columnCount;
if (i == 0 && valueCount) asn1Writer->writeEndSequence();
if (i == 0) asn1Writer->writeStartSequence();
valueCount++;
switch (columnTypes[i]) {
case TypeId::BOOLEAN:
{
assert(type == typeid (boolean_t));
auto* typedValue = static_cast<const boolean_t*> (value);
asn1Writer->writeBoolean(*typedValue);
break;
}
case TypeId::INTEGER:
{
assert(type == typeid (integer_t));
auto* typedValue = static_cast<const integer_t*> (value);
asn1Writer->writeInteger(*typedValue);
break;
}
case TypeId::STRING:
{
assert(type == typeid (string_t));
auto* typedValue = static_cast<const string_t*> (value);
asn1Writer->writeString(*typedValue);
break;
}
default:
throw cli::RelpipeCLIException(L"Unsupported type in ASN1Handler.attribute()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
}
}
void endOfPipe() {
if (valueCount) asn1Writer->writeEndSequence();
if (relationCount) asn1Writer->writeEndSequence();
asn1Writer->writeEndSequence();
asn1Writer->end();
}
};
}
}
}