Skip to content

Commit

Permalink
add get_fields api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacyking committed Aug 18, 2023
1 parent d2cae80 commit 53ad6ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
18 changes: 0 additions & 18 deletions iguana/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,18 +614,6 @@ constexpr std::array<frozen::string, N> get_alias_arr(Args... pairs) {
constexpr static std::string_view struct_name() { \
return std::string_view(#STRUCT_NAME, sizeof(#STRUCT_NAME) - 1); \
} \
static std::string_view fields() { \
static std::string alisa_fields; \
if (!alisa_fields.empty()) { \
return alisa_fields; \
} \
for (const auto &it : arr()) { \
alisa_fields += it.data(); \
alisa_fields += ","; \
} \
alisa_fields.back() = ' '; \
return alisa_fields; \
} \
constexpr static size_t value() { return size_type::value; } \
constexpr static std::array<frozen::string, size_type::value> arr() { \
return iguana::detail::get_alias_arr<size_type::value>(__VA_ARGS__); \
Expand Down Expand Up @@ -923,12 +911,6 @@ constexpr const std::string_view get_name() {
return M::name();
}

template <typename T>
constexpr const std::string_view get_fields() {
using M = Reflect_members<T>;
return M::fields();
}

template <typename T>
constexpr std::enable_if_t<is_reflection<T>::value, size_t> get_value() {
using M = decltype(iguana_reflect_type(std::declval<T>()));
Expand Down
18 changes: 16 additions & 2 deletions include/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,26 @@ inline std::string get_name() {
return quota_name;
}

template <typename T, typename = std::enable_if_t<iguana::is_reflection_v<T>>>
inline std::string get_fields() {
static std::string alisa_fields;
if (!alisa_fields.empty()) {
return alisa_fields;
}
for (const auto &it : iguana::Reflect_members<T>::arr()) {
alisa_fields += it.data();
alisa_fields += ",";
}
alisa_fields.back() = ' ';
return alisa_fields;
}

template <typename T>
inline std::string generate_insert_sql(bool replace) {
std::string sql = replace ? "replace into " : "insert into ";
constexpr size_t SIZE = iguana::get_value<T>();
auto name = get_name<T>();
auto fields = iguana::get_fields<T>();
auto fields = get_fields<T>();
append(sql, name.data(), "(", fields.data(), ")", "values(");

for (size_t i = 0; i < SIZE; ++i) {
Expand Down Expand Up @@ -257,7 +271,7 @@ inline std::string generate_query_sql(Args &&...args) {
static_assert(param_size == 0 || param_size > 0);
std::string sql = "select ";
auto name = get_name<T>();
auto fields = iguana::get_fields<T>();
auto fields = get_fields<T>();
append(sql, fields.data(), "from", name.data());

std::string where_sql = "";
Expand Down

0 comments on commit 53ad6ef

Please sign in to comment.