src/MetadataMode.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 04 Dec 2021 21:14:53 +0100
branchv_0
changeset 11 0d49d5041a84
parent 5 9ddc9462e25b
permissions -rw-r--r--
Added tag v0.18 for changeset 840db0029136
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <regex>
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <locale>
2
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    21
#include <stdexcept>
1
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/common/type/typedefs.h>
2
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    24
#include <relpipe/writer/RelpipeWriterException.h>
1
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Mode.h"
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace tr {
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace infertypes {
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class MetadataMode : public Mode {
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
5
9ddc9462e25b uppercase constant name
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    34
	static const std::wregex PATTERN;
1
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
public:
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	MetadataMode(shared_ptr<writer::RelationalWriter> relationalWriter) : Mode(relationalWriter) {
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	static bool willInfer(std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) {
5
9ddc9462e25b uppercase constant name
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    41
		for (relpipe::reader::handlers::AttributeMetadata a : attributes) if (!std::regex_match(a.getAttributeName(), PATTERN)) return false;
1
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		return true;
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	}
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
2
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    46
		// TODO: this logic should be same as in relpipe-in-csv and should be shared through relpipe-lib-infertypes later
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    47
		vector<writer::AttributeMetadata> writerMetadata;
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    48
		std::wsmatch match;
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    49
		for (relpipe::reader::handlers::AttributeMetadata& am : attributes) {
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    50
			relpipe::common::type::StringX nameAndType = am.getAttributeName();
5
9ddc9462e25b uppercase constant name
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    51
			if (std::regex_match(nameAndType, match, PATTERN)) writerMetadata.push_back({match[1], relationalWriter->toTypeId(match[2])});
2
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    52
			else throw relpipe::writer::RelpipeWriterException(L"Unable to find type in the attribute name: " + am.getAttributeName());
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    53
		}
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    54
eddead33f633 implement MetadataMode – like in relpipe-in-csv --read-types true
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    55
		relationalWriter->startRelation(name, writerMetadata, true);
1
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	}
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	void attribute(const relpipe::common::type::StringX& value) override {
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		relationalWriter->writeAttribute(value);
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	}
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
};
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
5
9ddc9462e25b uppercase constant name
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    63
const std::wregex MetadataMode::PATTERN = std::wregex(L"(.*)::(.*)"); // name::type
1
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
}
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
}
da114916734b multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
}