src/AttributeFinder.h
branchv_0
changeset 4 d44ed75822e7
child 8 eb1ecb37c6b7
equal deleted inserted replaced
3:62eac7ab4cf4 4:d44ed75822e7
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2019 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, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 #pragma once
       
    19 
       
    20 #include <vector>
       
    21 #include <filesystem>
       
    22 
       
    23 #include <relpipe/writer/typedefs.h>
       
    24 #include <relpipe/writer/AttributeMetadata.h>
       
    25 #include <relpipe/writer/RelationalWriter.h>
       
    26 
       
    27 #include "RequestedField.h"
       
    28 
       
    29 namespace relpipe {
       
    30 namespace in {
       
    31 namespace filesystem {
       
    32 
       
    33 namespace fs = std::filesystem;
       
    34 using namespace relpipe::writer;
       
    35 
       
    36 class AttributeFinder {
       
    37 public:
       
    38 
       
    39 	/**
       
    40 	 * Single requested fields might generate multiple attributes in the relation.
       
    41 	 * But usually it is 1:1.
       
    42 	 * @param field requested field from the user (usually from CLI arguments)
       
    43 	 * @return attribute metadata to be used in the RelationalWriter.startRelation()
       
    44 	 */
       
    45 	virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) = 0;
       
    46 
       
    47 	/**
       
    48 	 * Writing of the record for current file is starting.
       
    49 	 * Following writeField() calls are related to this file.
       
    50 	 * @param file
       
    51 	 */
       
    52 	virtual void startFile(const fs::path& file) = 0;
       
    53 
       
    54 	/**
       
    55 	 * Writing of the record for current file is finished. All resources linked to this file should be released.
       
    56 	 */
       
    57 	virtual void endFile() = 0;
       
    58 
       
    59 	/**
       
    60 	 * Writes field attribute(s). The attribute count must match with count of AttributeMetadata returned in toMetadata().
       
    61 	 * @param writer
       
    62 	 * @param field
       
    63 	 */
       
    64 	virtual void writeField(RelationalWriter* writer, const RequestedField& field) = 0;
       
    65 
       
    66 	/**
       
    67 	 * Writes empty attribute(s) in case of non-existent file or an error. 
       
    68 	 * The attribute count must match with count of AttributeMetadata returned in toMetadata().
       
    69 	 * @param writer
       
    70 	 * @param field
       
    71 	 */
       
    72 	virtual void writeEmptyField(RelationalWriter* writer, const RequestedField& field) {
       
    73 		// TODO: better handling of null values (when null values are supported by the format specification)
       
    74 		for (AttributeMetadata m : toMetadata(field)) {
       
    75 			switch (m.typeId) {
       
    76 				case TypeId::BOOLEAN:
       
    77 					writer->writeAttribute(L"false");
       
    78 					break;
       
    79 				case TypeId::INTEGER:
       
    80 					writer->writeAttribute(L"0");
       
    81 					break;
       
    82 				case TypeId::STRING:
       
    83 					writer->writeAttribute(L"");
       
    84 					break;
       
    85 				default:
       
    86 					throw RelpipeWriterException(L"Unsupported attribute type in writeEmptyField().");
       
    87 			}
       
    88 		}
       
    89 	}
       
    90 
       
    91 	virtual ~AttributeFinder() {
       
    92 	}
       
    93 };
       
    94 
       
    95 }
       
    96 }
       
    97 }