src/ArgumentsCommand.h
branchv_0
changeset 43 3c8ea5dcf793
parent 42 09cd32a65709
child 44 dd7094457e44
equal deleted inserted replaced
42:09cd32a65709 43:3c8ea5dcf793
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #pragma once
       
    18 
       
    19 #include <cstdlib>
       
    20 #include <iostream>
       
    21 #include <string>
       
    22 #include <vector>
       
    23 #include <algorithm>
       
    24 
       
    25 #include <relpipe/writer/typedefs.h>
       
    26 #include <relpipe/writer/AttributeMetadata.h>
       
    27 
       
    28 #include "Command.h"
       
    29 
       
    30 namespace relpipe {
       
    31 namespace in {
       
    32 namespace cli {
       
    33 
       
    34 class ArgumentsCommand : public Command {
       
    35 public:
       
    36 
       
    37 	void process(std::istream& input, std::ostream& output, const relpipe::writer::string_t& command, const std::vector<relpipe::writer::string_t>& arguments) override {
       
    38 		using namespace relpipe::writer;
       
    39 
       
    40 		size_t i = 0;
       
    41 		string_t relationName = arguments[i++];
       
    42 		integer_t attributeCount = std::stol(arguments[i++]); // TODO: use integer data type's method? + unsigned type
       
    43 		boolean_t writeHeader = true; // TODO: add option for header omitting
       
    44 
       
    45 		// TODO: check argument count
       
    46 
       
    47 		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
       
    48 
       
    49 		std::vector<AttributeMetadata> attributes(attributeCount);
       
    50 
       
    51 		for (size_t j = 0; j < attributeCount; j++) {
       
    52 			string_t attributeName = arguments[i++];
       
    53 			TypeId attributeType = writer->toTypeId(arguments[i++]);
       
    54 			attributes[j] = {attributeName, attributeType};
       
    55 		}
       
    56 
       
    57 		writer->startRelation(relationName, attributes, writeHeader);
       
    58 
       
    59 		for (; i < arguments.size(); i++) {
       
    60 			writer->writeAttribute(arguments[i]);
       
    61 		}
       
    62 	}
       
    63 };
       
    64 
       
    65 }
       
    66 }
       
    67 }