add option: --write-header
we can omit the relation's header and generate just the records
and append them to an existing relational stream:
(relpipe-in-cli --relation rrr --attribute a integer; relpipe-in-cli --write-header false --relation rrr --attribute XXX integer --records 1 2 3) | relpipe-out-tabular
/**
* Relational pipes
* Copyright © 2020 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 <codecvt>
#include <memory>
#include <iostream>
#include "Configuration.h"
namespace relpipe {
namespace in {
namespace cli {
class CLICommand {
private:
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default
void processRelation(RelationConfiguration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
// Write header / metadata:
std::vector<relpipe::writer::AttributeMetadata> attributesMetadata;
for (AttributeRecipe ar : configuration.attributes) attributesMetadata.push_back({ar.name, ar.type});
writer->startRelation(configuration.relation, attributesMetadata, configuration.writeHeader);
// Write records from CLI:
for (auto value : configuration.values) writer->writeAttribute(value);
// Write records from STDIN:
if (configuration.valueStream) {
std::stringstream rawValue;
while (true) {
char ch = 0;
configuration.valueStream->get(ch);
if (ch == 0 && configuration.valueStream->good()) {
writer->writeAttribute(convertor.from_bytes(rawValue.str()));
rawValue.str("");
rawValue.clear();
} else if (configuration.valueStream->good()) {
rawValue << ch;
} else if (configuration.valueStream->eof()) {
return;
} else {
throw relpipe::cli::RelpipeCLIException(L"Error while reading values from the input stream.", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception
}
}
}
}
public:
void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
for (RelationConfiguration& relationConfiguration : configuration.relationConfigurations) processRelation(relationConfiguration, writer);
}
};
}
}
}