diff -r 000000000000 -r f4f9cdf7ed59 src/ASN1Handler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ASN1Handler.h Sat Mar 30 01:12:42 2019 +0100 @@ -0,0 +1,138 @@ +/** + * 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 +#include +#include + +#include +#include +#include +#include + +#include "ASN1Writer.h" + +namespace relpipe { +namespace out { +namespace asn1 { + +using namespace relpipe::reader; + +class ASN1Handler : public handlers::RelationalReaderValueHadler { +private: + shared_ptr asn1Writer; + std::vector columnTypes; + std::vector columnTypeCodes; + std::vector 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 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 (value); + asn1Writer->writeBoolean(*typedValue); + break; + } + case TypeId::INTEGER: + { + assert(type == typeid (integer_t)); + auto* typedValue = static_cast (value); + asn1Writer->writeInteger(*typedValue); + break; + } + case TypeId::STRING: + { + assert(type == typeid (string_t)); + auto* typedValue = static_cast (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(); + } + +}; + +} +} +}