Skip to content

Commit

Permalink
Merge pull request #171 from Jacyking/master
Browse files Browse the repository at this point in the history
add blob uint8 uint16 uint32 uint64 CString
  • Loading branch information
Jacyking authored Aug 8, 2024
2 parents cc58493 + 9d8d7c0 commit a57db8b
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 85 deletions.
24 changes: 22 additions & 2 deletions ormpp/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

namespace ormpp {

using blob = ormpp_mysql::blob;

class mysql {
public:
~mysql() { disconnect(); }
Expand Down Expand Up @@ -193,6 +191,13 @@ class mysql {
param.buffer = (void *)(value.data());
param.buffer_length = (unsigned long)value.size();
}
#ifdef ORMPP_WITH_CSTRING
else if constexpr (std::is_same_v<CString, U>) {
param.buffer_type = MYSQL_TYPE_STRING;
param.buffer = (void *)(value.GetString());
param.buffer_length = (unsigned long)value.GetLength();
}
#endif
else {
static_assert(!sizeof(U), "this type has not supported yet");
}
Expand Down Expand Up @@ -241,6 +246,15 @@ class mysql {
param_bind.buffer = &(mp.rbegin()->second[0]);
param_bind.buffer_length = 65536;
}
#ifdef ORMPP_WITH_CSTRING
else if constexpr (std::is_same_v<CString, U>) {
param_bind.buffer_type = MYSQL_TYPE_STRING;
std::vector<char> tmp(65536, 0);
mp.emplace(i, std::move(tmp));
param_bind.buffer = &(mp.rbegin()->second[0]);
param_bind.buffer_length = 65536;
}
#endif
else {
static_assert(!sizeof(U), "this type has not supported yet");
}
Expand Down Expand Up @@ -276,6 +290,12 @@ class mysql {
auto &vec = mp[i];
value = blob(vec.data(), vec.data() + get_blob_len(i));
}
#ifdef ORMPP_WITH_CSTRING
else if constexpr (std::is_same_v<CString, U>) {
auto &vec = mp[i];
value.SetString(std::string(&vec[0], strlen(vec.data()).c_str()));
}
#endif
}

template <typename T, typename... Args>
Expand Down
27 changes: 25 additions & 2 deletions ormpp/postgresql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,20 @@ class postgresql {
std::copy(value, value + sizeof(U), std::back_inserter(temp));
param_values.push_back(std::move(temp));
}
else if constexpr (std::is_same_v<blob, U>) {
std::vector<char> temp = {};
std::copy(value.data(), value.data() + value.size(),
std::back_inserter(temp));
param_values.push_back(std::move(temp));
}
#ifdef ORMPP_WITH_CSTRING
else if constexpr (std::is_same_v<CString, U>) {
std::vector<char> temp = {};
std::copy(value.GetString(), value.GetString() + value.GetLength() + 1,
std::back_inserter(temp));
param_values.push_back(std::move(temp));
}
#endif
else {
static_assert(!sizeof(U), "this type has not supported yet");
}
Expand Down Expand Up @@ -752,14 +766,23 @@ class postgresql {
else if constexpr (std::is_same_v<std::string, U>) {
value = PQgetvalue(res_, row, i);
}
else if constexpr (iguana::array_v<U>) {
auto p = PQgetvalue(res_, row, i);
memcpy(value.data(), p, value.size());
}
else if constexpr (iguana::c_array_v<U>) {
auto p = PQgetvalue(res_, row, i);
memcpy(value, p, sizeof(U));
}
else if constexpr (iguana::array_v<U>) {
else if constexpr (std::is_same_v<blob, U>) {
auto p = PQgetvalue(res_, row, i);
memcpy(value.data(), p, value.size());
value = blob(p, p + PQgetlength(res_, row, i));
}
#ifdef ORMPP_WITH_CSTRING
else if constexpr (std::is_same_v<CString, U>) {
value.SetString(PQgetvalue(res_, row, i));
}
#endif
else {
static_assert(!sizeof(U), "this type has not supported yet");
}
Expand Down
14 changes: 12 additions & 2 deletions ormpp/sqlite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,16 @@ class sqlite {
return SQLITE_OK ==
sqlite3_bind_text(stmt_, i, value, strlen(value), nullptr);
}
else if constexpr (std::is_same_v<blob, U>) {
return SQLITE_OK == sqlite3_bind_blob(stmt_, i, value.data(),
static_cast<size_t>(value.size()),
nullptr);
}
#ifdef ORMPP_WITH_CSTRING
else if constexpr (std::is_same_v<CString, U>) {
return SQLITE_OK == sqlite3_bind_text(stmt_, i, value.GetString(),
(int)value.GetLength(), nullptr);
return SQLITE_OK ==
sqlite3_bind_text(stmt_, i, value.GetString(),
static_cast<size_t>(value.GetLength()), nullptr);
}
#endif
else {
Expand Down Expand Up @@ -624,6 +630,10 @@ class sqlite {
else if constexpr (iguana::c_array_v<U>) {
memcpy(value, sqlite3_column_text(stmt_, i), sizeof(U));
}
else if constexpr (std::is_same_v<blob, U>) {
auto p = (const char *)sqlite3_column_blob(stmt_, i);
value = blob(p, p + sqlite3_column_bytes(stmt_, i));
}
#ifdef ORMPP_WITH_CSTRING
else if constexpr (std::is_same_v<CString, U>) {
value.SetString((const char *)sqlite3_column_text(stmt_, i));
Expand Down
124 changes: 73 additions & 51 deletions ormpp/type_mapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,34 @@ using namespace std::string_view_literals;
#define EXAMPLE1_TYPE_MAPPING_HPP

namespace ormpp {

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)

using blob = std::vector<char>;

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 @@ -68,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 @@ -82,16 +95,12 @@ 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;
}
inline constexpr auto type_to_name(identity<double>) noexcept {
return "DOUBLE"sv;
}

inline constexpr auto type_to_name(identity<bool>) noexcept {
return "INTEGER"sv;
}
Expand All @@ -104,15 +113,25 @@ 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<float>) noexcept {
return "FLOAT"sv;
}
inline constexpr auto type_to_name(identity<double>) noexcept {
return "DOUBLE"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;
}
inline constexpr auto type_to_name(identity<uint8_t>) noexcept {
return "INTEGER"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 "INTEGER"sv;
}
inline constexpr auto type_to_name(identity<uint64_t>) noexcept {
return "INTEGER"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>
inline auto type_to_name(identity<std::array<char, N>>) noexcept {
Expand All @@ -123,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 @@ -154,9 +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<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
10 changes: 10 additions & 0 deletions ormpp/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ inline constexpr auto get_type_names(DBType type) {
else if constexpr (is_optional_v<U>::value) {
s = ormpp_mysql::type_to_name(identity<typename U::value_type>{});
}
#ifdef ORMPP_WITH_CSTRING
else if constexpr (std::is_same_v<CString, U>) {
s = "TEXT"sv;
}
#endif
else {
s = ormpp_mysql::type_to_name(identity<U>{});
}
Expand Down Expand Up @@ -181,6 +186,11 @@ inline constexpr auto get_type_names(DBType type) {
else if constexpr (is_optional_v<U>::value) {
s = ormpp_postgresql::type_to_name(identity<typename U::value_type>{});
}
#ifdef ORMPP_WITH_CSTRING
else if constexpr (std::is_same_v<CString, U>) {
s = "TEXT"sv;
}
#endif
else {
s = ormpp_postgresql::type_to_name(identity<U>{});
}
Expand Down
Loading

0 comments on commit a57db8b

Please sign in to comment.