#include "mysql_types.h" #include "helpers.h" #include "rawDataReader.h" #include #include namespace MyGrate::MySQL { typename Type::C Type::read(RawDataReader &, RawDataReader &) { return nullptr; } #define INTEGER_TYPE(ET) \ typename Type::C Type::read(RawDataReader &, RawDataReader & data) \ { \ return data.readValue::C>(); \ } \ typename Type::C Type::read(RawDataReader &, RawDataReader & data) \ { \ return data.readValue::C>(); \ } INTEGER_TYPE(MYSQL_TYPE_TINY); INTEGER_TYPE(MYSQL_TYPE_SHORT); INTEGER_TYPE(MYSQL_TYPE_LONG); INTEGER_TYPE(MYSQL_TYPE_LONGLONG); #undef INTEGER_TYPE #define FLOAT_TYPE(ET) \ typename Type::C Type::read(RawDataReader & md, RawDataReader & data) \ { \ verify(sizeof(typename Type::C) == md.readValue(), "Invalid " #ET " size"); \ return data.readValue::C>(); \ } FLOAT_TYPE(MYSQL_TYPE_FLOAT); FLOAT_TYPE(MYSQL_TYPE_DOUBLE); #undef FLOAT_TYPE typename Type::C Type::read(RawDataReader &, RawDataReader &) { throw std::logic_error("Not implemented"); } typename Type::C Type::read(RawDataReader &, RawDataReader &) { throw std::logic_error("Not implemented"); } #define INTEGER_TYPE(ET, s, l) \ typename Type::C Type::read(RawDataReader &, RawDataReader & data) \ { \ return data.readValue::C, l>(); \ } INTEGER_TYPE(MYSQL_TYPE_INT24, false, 3); INTEGER_TYPE(MYSQL_TYPE_INT24, true, 3); INTEGER_TYPE(MYSQL_TYPE_YEAR, false, 2); #undef INTEGER24_TYPE static Blob readBlob(RawDataReader & md, RawDataReader & data) { const auto lenlen {md.readValue()}; const auto len {data.readValue(lenlen)}; return data.viewValue(len); } #define BLOB_TYPE(ET) \ typename Type::C Type::read(RawDataReader & md, RawDataReader & data) \ { \ return readBlob(md, data); \ } BLOB_TYPE(MYSQL_TYPE_TINY_BLOB); BLOB_TYPE(MYSQL_TYPE_MEDIUM_BLOB); BLOB_TYPE(MYSQL_TYPE_LONG_BLOB); BLOB_TYPE(MYSQL_TYPE_GEOMETRY); // Ummm, pass this to the target to handle? #undef BLOB_TYPE typename Type::C Type::read(RawDataReader & md, RawDataReader & data) { const auto lenlen {md.readValue()}; const auto len {data.readValue(lenlen)}; return data.viewValue(len); } typename Type::C Type::read(RawDataReader &, RawDataReader &) { throw std::logic_error("Not implemented"); } #define STRING_TYPE(ET) \ typename Type::C Type::read(RawDataReader & md, RawDataReader & data) \ { \ const auto realtype {md.readValue()}; \ const auto lenlen {md.readValue()}; \ switch (realtype) { \ case MYSQL_TYPE_ENUM: { \ return data.viewValue(lenlen); \ break; \ } \ default: \ throw std::logic_error("Not implemented: sub-type: " + std::to_string(realtype)); \ break; \ } \ throw std::logic_error("Didn't return a value: " + std::to_string(realtype)); \ } STRING_TYPE(MYSQL_TYPE_STRING); STRING_TYPE(MYSQL_TYPE_JSON); STRING_TYPE(MYSQL_TYPE_SET); STRING_TYPE(MYSQL_TYPE_ENUM); #undef STRING_TYPE typename Type::C Type::read(RawDataReader & md, RawDataReader & data) { const auto fieldlen {md.readValue()}; const auto len {data.readValue(fieldlen > 255 ? 2 : 1)}; return data.viewValue(len); } typename Type::C Type::read(RawDataReader & md, RawDataReader & data) { const auto fieldlen {md.readValue()}; const auto len {md.readValue(fieldlen > 255 ? 2 : 1)}; return data.viewValue(len); } typename Type::C Type::read(RawDataReader &, RawDataReader & data) { auto dtint {data.readValue()}; DateTime dt; dt.second = mod100_extract(dtint); dt.minute = mod100_extract(dtint); dt.hour = mod100_extract(dtint); dt.day = mod100_extract(dtint); dt.month = mod100_extract(dtint); dt.year = dtint; return dt; } typename Type::C Type::read(RawDataReader &, RawDataReader & data) { auto tint {data.readValue()}; Time t; t.second = mod100_extract(tint); t.minute = mod100_extract(tint); t.hour = tint; return t; } typename Type::C Type::read(RawDataReader &, RawDataReader &) { throw std::logic_error("Not implemented"); } typename Type::C Type::read(RawDataReader &, RawDataReader & data) { auto dint {data.readValue()}; Date d; d.day = bitslice(dint, 0, 6); d.month = bitslice(dint, 6, 4); d.year = bitslice(dint, 10, 14); return d; } typename Type::C Type::read(RawDataReader &, RawDataReader &) { throw std::logic_error("Not implemented"); } typename Type::C Type::read(RawDataReader &, RawDataReader & data) { return {data.readValue(), 0}; } typename Type::C Type::read(RawDataReader & md, RawDataReader & data) { const auto decimals {md.readValue()}; return {data.readValue(), data.readValue(decimals)}; } typename Type::C Type::read(RawDataReader & md, RawDataReader & data) { md.discard(1); union { uint64_t i; std::array b; } r {}; r.b = data.readValue(); std::reverse(r.b.begin(), r.b.end()); DateTime dt; dt.year = (bitslice(r.i, 22, 17) / 13); dt.month = (bitslice(r.i, 22, 17) % 13); dt.day = bitslice(r.i, 17, 5); dt.hour = bitslice(r.i, 12, 5); dt.minute = bitslice(r.i, 6, 6); dt.second = bitslice(r.i, 0, 6); return dt; } }