From 01ccd4e00d0990bd191ccc07a22895a68df65634 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 27 Jun 2021 18:51:59 +0100 Subject: Basic type mapper --- lib/output/pq/typeMapper.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++ lib/output/pq/typeMapper.h | 48 +++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 lib/output/pq/typeMapper.cpp create mode 100644 lib/output/pq/typeMapper.h (limited to 'lib/output') diff --git a/lib/output/pq/typeMapper.cpp b/lib/output/pq/typeMapper.cpp new file mode 100644 index 0000000..5583319 --- /dev/null +++ b/lib/output/pq/typeMapper.cpp @@ -0,0 +1,72 @@ +#include "typeMapper.h" +#include + +namespace MyGrate::Output::Pq { + TypeMapper::RegexMapper::RegexMapper(std::optional t, std::optional c, std::string tt) : + typeMatch {std::move(t)}, columnMatch {std::move(c)}, targetType {std::move(tt)} + { + } + + std::optional + TypeMapper::RegexMapper::map(std::string_view typeName, std::string_view columnName) const + { + if ((!typeMatch || std::regex_match(typeName.begin(), typeName.end(), *typeMatch)) + && (!columnMatch || std::regex_match(columnName.begin(), columnName.end(), *columnMatch))) { + if (targetType.empty()) { + return {typeName}; + } + return {targetType}; + } + return {}; + } + + TypeMapper::TypeMapper() + { + const auto add = [this](auto r, auto t) { + mappings.emplace_back(std::make_unique(r, std::nullopt, t)); + }; + // https://dev.mysql.com/doc/refman/8.0/en/numeric-types.html + add(R"(bit\(\d+\))"_r, ""); + add(R"(tinyint\(1\)( (un)?signed)?)"_r, "boolean"); + add(R"((tiny|small)int(\(\d+\))?( (un)?signed)?)"_r, "smallint"); + add(R"((medium)?int(\(\d+\))?( (un)?signed)?)"_r, "int"); + add(R"(bigint(\(\d+\))?( (un)?signed)?)"_r, "bigint"); + add(R"((numeric|decimal)(\(\d+(,\d+)?\))?)"_r, ""); + add(R"(float(\(\d+,\d+\))?)"_r, "float"); + add(R"((real|double( precision)?)(\(\d+,\d+\))?)"_r, "double"); + // https://dev.mysql.com/doc/refman/8.0/en/date-and-time-types.html + add(R"(date)"_r, "date"); + add(R"(datetime)"_r, "timestamp without time zone"); + add(R"(datetime(\(\d+\))?)"_r, "timestamp without time zone"); // drops precision + add(R"(timestamp)"_r, "timestamp without time zone"); + add(R"(timestamp(\(\d+\))?)"_r, "timestamp without time zone"); // drops precision + add(R"(time)"_r, "time without time zone"); + add(R"(time(\(\d+\))?)"_r, "time without time zone"); // drops precision + add(R"(year)"_r, "smallint"); + // https://dev.mysql.com/doc/refman/8.0/en/string-types.html + add(R"((var)?binary(\(\d+\))?)"_r, "bytea"); + add(R"((var)?char(\(\d+\))?)"_r, ""); + add(R"((tiny||medium|long)blob(\(\d+\))?)"_r, "bytea"); + add(R"((tiny||medium|long)text(\(\d+\))?)"_r, "text"); + add(R"(enum(.*))"_r, "text"); // not really + // set + // https://dev.mysql.com/doc/refman/8.0/en/json.html + add(R"(json)"_r, "json"); + } + + std::string_view + TypeMapper::map(std::string_view t, std::string_view n) const + { + for (const auto & m : mappings) { + if (const auto rt {m->map(t, n)}) { + return *rt; + } + } + throw std::runtime_error(scprintf<"No mapping for %? (%?)">(n, t)); + } + + TypeMapper::ObsRegex operator""_r(const char * input, std::size_t) + { + return MyGrate::Output::Pq::TypeMapper::ObsRegex {input, std::regex::icase}; + } +} diff --git a/lib/output/pq/typeMapper.h b/lib/output/pq/typeMapper.h new file mode 100644 index 0000000..7005e4f --- /dev/null +++ b/lib/output/pq/typeMapper.h @@ -0,0 +1,48 @@ +#ifndef MYGRATE_OUTPUT_PQ_TYPEMAPPER_H +#define MYGRATE_OUTPUT_PQ_TYPEMAPPER_H + +#include +#include +#include +#include +#include + +namespace MyGrate::Output::Pq { + class TypeMapper { + public: + class ObsRegex : public std::regex { + public: + explicit ObsRegex(const std::string & src, std::regex_constants::syntax_option_type sot = {}) : + std::regex {src, sot}, src {src} + { + } + const std::string src; + }; + + struct Mapping { + virtual std::optional map(std::string_view t, std::string_view n) const = 0; + }; + using MappingPtr = std::unique_ptr; + + struct RegexMapper : public Mapping { + RegexMapper(std::optional, std::optional, std::string); + + std::optional map(std::string_view t, std::string_view n) const override; + + std::optional typeMatch; + std::optional columnMatch; + std::string targetType; + }; + + TypeMapper(); + + std::string_view map(std::string_view t, std::string_view n) const; + + private: + std::vector mappings; + }; + + TypeMapper::ObsRegex operator""_r(const char * input, std::size_t); +} + +#endif -- cgit v1.2.3