Skip to content

change is_char_array_v to iguana::array_v #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions ormpp/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ class mysql {
param.buffer = (void *)(value.c_str());
param.buffer_length = (unsigned long)value.size();
}
else if constexpr (std::is_same_v<const char *, U> || is_char_array_v<U>) {
else if constexpr (iguana::array_v<U>) {
param.buffer_type = MYSQL_TYPE_STRING;
param.buffer = (void *)(value.data());
param.buffer_length = (unsigned long)value.size();
}
else if constexpr (iguana::c_array_v<U> ||
std::is_same_v<const char *, U>) {
param.buffer_type = MYSQL_TYPE_STRING;
param.buffer = (void *)(value);
param.buffer_length = (unsigned long)strlen(value);
Expand Down Expand Up @@ -221,7 +227,7 @@ class mysql {
param_bind.buffer = &(mp.rbegin()->second[0]);
param_bind.buffer_length = 65536;
}
else if constexpr (is_char_array_v<U>) {
else if constexpr (iguana::array_v<U>) {
param_bind.buffer_type = MYSQL_TYPE_VAR_STRING;
std::vector<char> tmp(sizeof(U), 0);
mp.emplace(i, std::move(tmp));
Expand Down Expand Up @@ -262,9 +268,9 @@ class mysql {
auto &vec = mp[i];
value = std::string(&vec[0], strlen(vec.data()));
}
else if constexpr (is_char_array_v<U>) {
else if constexpr (iguana::array_v<U>) {
auto &vec = mp[i];
memcpy(value, vec.data(), vec.size());
memcpy(value.data(), vec.data(), value.size());
}
else if constexpr (std::is_same_v<blob, U>) {
auto &vec = mp[i];
Expand Down
10 changes: 7 additions & 3 deletions ormpp/postgresql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,13 +708,13 @@ class postgresql {
sprintf(temp.data(), "%f", value);
param_values.push_back(std::move(temp));
}
else if constexpr (std::is_same_v<std::string, U>) {
else if constexpr (iguana::array_v<U> || std::is_same_v<std::string, U>) {
std::vector<char> temp = {};
std::copy(value.data(), value.data() + value.size() + 1,
std::back_inserter(temp));
param_values.push_back(std::move(temp));
}
else if constexpr (is_char_array_v<U>) {
else if constexpr (iguana::c_array_v<U>) {
std::vector<char> temp = {};
std::copy(value, value + sizeof(U), std::back_inserter(temp));
param_values.push_back(std::move(temp));
Expand Down Expand Up @@ -752,10 +752,14 @@ class postgresql {
else if constexpr (std::is_same_v<std::string, U>) {
value = PQgetvalue(res_, row, i);
}
else if constexpr (is_char_array_v<U>) {
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>) {
auto p = PQgetvalue(res_, row, i);
memcpy(value.data(), p, value.size());
}
else {
static_assert(!sizeof(U), "this type has not supported yet");
}
Expand Down
14 changes: 7 additions & 7 deletions ormpp/sqlite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,19 +562,16 @@ class sqlite {
else if constexpr (std::is_floating_point_v<U>) {
return SQLITE_OK == sqlite3_bind_double(stmt_, i, value);
}
else if constexpr (std::is_same_v<std::string, U>) {
else if constexpr (iguana::array_v<U> || std::is_same_v<std::string, U>) {
return SQLITE_OK ==
sqlite3_bind_text(stmt_, i, value.data(), value.size(), nullptr);
}
else if constexpr (std::is_same_v<char,
else if constexpr (iguana::c_array_v<U> ||
std::is_same_v<char,
std::remove_pointer_t<std::decay_t<U>>>) {
return SQLITE_OK ==
sqlite3_bind_text(stmt_, i, value, strlen(value), nullptr);
}
else if constexpr (is_char_array_v<U>) {
return SQLITE_OK ==
sqlite3_bind_text(stmt_, i, value, sizeof(U), nullptr);
}
#ifdef ORMPP_WITH_CSTRING
else if constexpr (std::is_same_v<CString, U>) {
return SQLITE_OK == sqlite3_bind_text(stmt_, i, value.GetString(),
Expand Down Expand Up @@ -621,7 +618,10 @@ class sqlite {
value.assign((const char *)sqlite3_column_text(stmt_, i),
(size_t)sqlite3_column_bytes(stmt_, i));
}
else if constexpr (is_char_array_v<U>) {
else if constexpr (iguana::array_v<U>) {
memcpy(value.data(), sqlite3_column_text(stmt_, i), sizeof(U));
}
else if constexpr (iguana::c_array_v<U>) {
memcpy(value, sqlite3_column_text(stmt_, i), sizeof(U));
}
#ifdef ORMPP_WITH_CSTRING
Expand Down
10 changes: 1 addition & 9 deletions ormpp/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "entity.hpp"
#include "iguana/reflection.hpp"
#include "iguana/util.hpp"
#include "type_mapping.hpp"

namespace ormpp {
Expand Down Expand Up @@ -404,15 +405,6 @@ inline std::string generate_update_sql(Args &&...args) {

inline bool is_empty(const std::string &t) { return t.empty(); }

template <class T>
constexpr bool is_char_array_v = std::is_array_v<T>
&&std::is_same_v<char, std::remove_pointer_t<std::decay_t<T>>>;

template <size_t N>
inline constexpr size_t char_array_size(char (&)[N]) {
return N;
}

template <typename T, typename... Args>
inline std::string generate_delete_sql(Args &&...where_conditon) {
std::string sql = "delete from ";
Expand Down
15 changes: 8 additions & 7 deletions tests/test_ormpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ struct simple {
int id;
double code;
int age;
std::array<char, 128> arr;
};
REFLECTION(simple, id, code, age)
REFLECTION(simple, id, code, age, arr)

// TEST_CASE(mysql performance){
// dbng<mysql> mysql;
Expand Down Expand Up @@ -760,16 +761,16 @@ TEST_CASE("delete") {

TEST_CASE("query") {
ormpp_key key{"id"};
simple s1 = {1, 2.5, 3};
simple s2 = {2, 3.5, 4};
simple s3 = {3, 4.5, 5};
simple s1 = {1, 2.5, 3, {"s1"}};
simple s2 = {2, 3.5, 4, {"s2"}};
simple s3 = {3, 4.5, 5, {"s3"}};
std::vector<simple> v{s1, s2, s3};

#ifdef ORMPP_ENABLE_MYSQL
dbng<mysql> mysql;
if (mysql.connect(ip, username, password, db)) {
mysql.execute("drop table if exists simple");
mysql.create_datatable<simple>(key);
mysql.delete_records<simple>();
CHECK(mysql.insert(v) == 3);
auto vec1 = mysql.query<simple>();
CHECK(vec1.size() == 3);
Expand All @@ -781,8 +782,8 @@ TEST_CASE("query") {
#ifdef ORMPP_ENABLE_PG
dbng<postgresql> postgres;
if (postgres.connect(ip, username, password, db)) {
postgres.execute("drop table if exists simple");
postgres.create_datatable<simple>(key);
postgres.delete_records<simple>();
CHECK(postgres.insert(v) == 3);
auto vec1 = postgres.query<simple>();
CHECK(vec1.size() == 3);
Expand All @@ -794,8 +795,8 @@ TEST_CASE("query") {
#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
if (sqlite.connect(db)) {
sqlite.execute("drop table if exists simple");
sqlite.create_datatable<simple>(key);
sqlite.delete_records<simple>();
CHECK(sqlite.insert(v) == 3);
auto vec1 = sqlite.query<simple>();
CHECK(vec1.size() == 3);
Expand Down