Skip to content

Commit

Permalink
add uint8 uint16 uint32 uint64
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacyking committed Aug 7, 2024
1 parent 548046e commit 9d8d7c0
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 75 deletions.
2 changes: 1 addition & 1 deletion ormpp/postgresql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ class postgresql {
}
else if constexpr (std::is_same_v<blob, U>) {
std::vector<char> temp = {};
std::copy(value.data(), value.data() + value.size() + 1,
std::copy(value.data(), value.data() + value.size(),
std::back_inserter(temp));
param_values.push_back(std::move(temp));
}
Expand Down
99 changes: 53 additions & 46 deletions ormpp/type_mapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,28 @@ using blob = std::vector<char>;
template <class T>
struct identity {};

#define REGISTER_TYPE(Type, Index) \
inline constexpr int type_to_id(identity<Type>) noexcept { return Index; } \
inline constexpr auto id_to_type( \
std::integral_constant<std::size_t, Index>) noexcept { \
Type res{}; \
return res; \
}

#ifdef ORMPP_ENABLE_MYSQL
namespace ormpp_mysql {
REGISTER_TYPE(char, MYSQL_TYPE_TINY)
REGISTER_TYPE(short, MYSQL_TYPE_SHORT)
REGISTER_TYPE(int, MYSQL_TYPE_LONG)
REGISTER_TYPE(float, MYSQL_TYPE_FLOAT)
REGISTER_TYPE(double, MYSQL_TYPE_DOUBLE)
REGISTER_TYPE(int64_t, MYSQL_TYPE_LONGLONG)

inline int type_to_id(identity<char>) noexcept { return MYSQL_TYPE_TINY; }
inline int type_to_id(identity<short>) noexcept { return MYSQL_TYPE_SHORT; }
inline int type_to_id(identity<int>) noexcept { return MYSQL_TYPE_LONG; }
inline int type_to_id(identity<float>) noexcept { return MYSQL_TYPE_FLOAT; }
inline int type_to_id(identity<double>) noexcept { return MYSQL_TYPE_DOUBLE; }
inline int type_to_id(identity<int8_t>) noexcept { return MYSQL_TYPE_TINY; }
inline int type_to_id(identity<int64_t>) noexcept {
return MYSQL_TYPE_LONGLONG;
}
inline int type_to_id(identity<uint8_t>) noexcept { return MYSQL_TYPE_SHORT; }
inline int type_to_id(identity<uint16_t>) noexcept { return MYSQL_TYPE_LONG; }
inline int type_to_id(identity<uint32_t>) noexcept {
return MYSQL_TYPE_LONGLONG;
}
inline int type_to_id(identity<uint64_t>) noexcept {
return MYSQL_TYPE_LONGLONG;
}
inline int type_to_id(identity<std::string>) noexcept {
return MYSQL_TYPE_VAR_STRING;
}
inline std::string id_to_type(
std::integral_constant<std::size_t, MYSQL_TYPE_VAR_STRING>) noexcept {
std::string res{};
return res;
}

inline constexpr auto type_to_name(identity<bool>) noexcept {
return "BOOLEAN"sv;
Expand All @@ -69,9 +66,24 @@ inline constexpr auto type_to_name(identity<float>) noexcept {
inline constexpr auto type_to_name(identity<double>) noexcept {
return "DOUBLE"sv;
}
inline constexpr auto type_to_name(identity<int8_t>) noexcept {
return "TINYINT"sv;
}
inline constexpr auto type_to_name(identity<int64_t>) noexcept {
return "BIGINT"sv;
}
inline constexpr auto type_to_name(identity<uint8_t>) noexcept {
return "SMALLINT"sv;
}
inline constexpr auto type_to_name(identity<uint16_t>) noexcept {
return "INTEGER"sv;
}
inline constexpr auto type_to_name(identity<uint32_t>) noexcept {
return "BIGINT"sv;
}
inline constexpr auto type_to_name(identity<uint64_t>) noexcept {
return "BIGINT"sv;
}
inline constexpr auto type_to_name(identity<blob>) noexcept { return "BLOB"sv; }
inline auto type_to_name(identity<std::string>) noexcept { return "TEXT"sv; }
template <size_t N>
Expand All @@ -83,16 +95,6 @@ inline auto type_to_name(identity<std::array<char, N>>) noexcept {
#endif
#ifdef ORMPP_ENABLE_SQLITE3
namespace ormpp_sqlite {
REGISTER_TYPE(int, SQLITE_INTEGER)
REGISTER_TYPE(double, SQLITE_FLOAT)

inline int type_to_id(identity<std::string>) noexcept { return SQLITE_TEXT; }
inline std::string id_to_type(
std::integral_constant<std::size_t, SQLITE_TEXT>) noexcept {
std::string res{};
return res;
}

inline constexpr auto type_to_name(identity<float>) noexcept {
return "FLOAT"sv;
}
Expand All @@ -111,6 +113,9 @@ inline constexpr auto type_to_name(identity<short>) noexcept {
inline constexpr auto type_to_name(identity<int>) noexcept {
return "INTEGER"sv;
}
inline constexpr auto type_to_name(identity<int8_t>) noexcept {
return "INTEGER"sv;
}
inline constexpr auto type_to_name(identity<int64_t>) noexcept {
return "INTEGER"sv;
}
Expand All @@ -137,21 +142,6 @@ inline auto type_to_name(identity<std::array<char, N>>) noexcept {
#endif
#ifdef ORMPP_ENABLE_PG
namespace ormpp_postgresql {
REGISTER_TYPE(bool, BOOLOID)
REGISTER_TYPE(char, CHAROID)
REGISTER_TYPE(short, INT2OID)
REGISTER_TYPE(int, INT4OID)
REGISTER_TYPE(float, FLOAT4OID)
REGISTER_TYPE(double, FLOAT8OID)
REGISTER_TYPE(int64_t, INT8OID)

inline int type_to_id(identity<std::string>) noexcept { return TEXTOID; }
inline std::string id_to_type(
std::integral_constant<std::size_t, TEXTOID>) noexcept {
std::string res{};
return res;
}

inline constexpr auto type_to_name(identity<bool>) noexcept {
return "integer"sv;
}
Expand All @@ -168,10 +158,27 @@ inline constexpr auto type_to_name(identity<float>) noexcept {
inline constexpr auto type_to_name(identity<double>) noexcept {
return "double precision"sv;
}
inline constexpr auto type_to_name(identity<int8_t>) noexcept {
return "char"sv;
}
inline constexpr auto type_to_name(identity<int64_t>) noexcept {
return "bigint"sv;
}
inline constexpr auto type_to_name(identity<blob>) noexcept { return "BLOB"sv; }
inline constexpr auto type_to_name(identity<uint8_t>) noexcept {
return "smallint"sv;
}
inline constexpr auto type_to_name(identity<uint16_t>) noexcept {
return "integer"sv;
}
inline constexpr auto type_to_name(identity<uint32_t>) noexcept {
return "bigint"sv;
}
inline constexpr auto type_to_name(identity<uint64_t>) noexcept {
return "bigint"sv;
}
inline constexpr auto type_to_name(identity<blob>) noexcept {
return "bytea"sv;
}
inline auto type_to_name(identity<std::string>) noexcept { return "text"sv; }
template <size_t N>
inline auto type_to_name(identity<std::array<char, N>>) noexcept {
Expand Down
Loading

0 comments on commit 9d8d7c0

Please sign in to comment.