Skip to content

Commit

Permalink
update for iguana
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacyking committed Dec 23, 2024
1 parent c2f5660 commit 21cbaf1
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmake/build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ if(BUILD_WITH_LIBCXX AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
message(STATUS "Build with libc++")
else()
message(STATUS "Build with libstdc++")
endif()
endif()
14 changes: 13 additions & 1 deletion cmake/mariadb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ ELSEIF (LINUX)
/usr/include/mariadb)
ELSEIF (APPLE)
FIND_PATH(MARIADB_INCLUDE_DIR mysql.h
/opt/homebrew/include/mariadb)
/opt/homebrew/include/mariadb)
ELSE (WIN32)
FIND_PATH(MARIADB_INCLUDE_DIR mysql.h
/opt/homebrew/include/mariadb
/usr/local/include/mariadb
/usr/include/mariadb)
ENDIF()

SET(MARIADB_NAMES mariadb)
Expand All @@ -41,6 +46,13 @@ ELSEIF (APPLE)
NAMES ${MARIADB_NAMES}
PATHS /opt/homebrew/lib
PATH_SUFFIXES mariadb)
ELSE (WIN32)
FIND_LIBRARY(MARIADB_LIBRARY
NAMES ${MARIADB_NAMES}
PATHS /usr/lib
/usr/local/lib
/opt/homebrew/lib
PATH_SUFFIXES mariadb)
ENDIF()

IF (MARIADB_INCLUDE_DIR AND MARIADB_LIBRARY)
Expand Down
16 changes: 16 additions & 0 deletions cmake/mysql.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ ELSEIF (APPLE)
/opt/homebrew/include/mysql
/opt/homebrew/opt/[email protected]/include
/opt/homebrew/Cellar/[email protected]/*/include/mysql)
ELSE (WIN32)
FIND_PATH(MYSQL_INCLUDE_DIR mysql.h
/opt/homebrew/Cellar/[email protected]/*/include/mysql
/opt/homebrew/opt/[email protected]/include
/opt/homebrew/include/mysql
/usr/local/include/mysql
/usr/include/mysql)
ENDIF()

SET(MYSQL_NAMES mysqlclient)
Expand All @@ -45,6 +52,15 @@ ELSEIF (APPLE)
/opt/homebrew/opt/[email protected]/lib
/opt/homebrew/Cellar/[email protected]/*/lib
PATH_SUFFIXES mysql)
ELSE (WIN32)
FIND_LIBRARY(MYSQL_LIBRARY
NAMES ${MYSQL_NAMES}
PATHS /usr/lib
/usr/local/lib
/opt/homebrew/lib
/opt/homebrew/opt/[email protected]/lib
/opt/homebrew/Cellar/[email protected]/*/lib
PATH_SUFFIXES mysql)
ENDIF()

IF (MYSQL_INCLUDE_DIR AND MYSQL_LIBRARY)
Expand Down
12 changes: 12 additions & 0 deletions cmake/pgsql.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ ELSEIF (LINUX)
ELSEIF (APPLE)
FIND_PATH(PGSQL_INCLUDE_DIR libpq-fe.h
/opt/homebrew/include/postgresql)
ELSE (WIN32)
FIND_PATH(PGSQL_INCLUDE_DIR libpq-fe.h
/opt/homebrew/include/postgresql
/usr/local/include/postgresql
/usr/include/postgresql)
ENDIF()

IF (WIN32)
Expand All @@ -40,6 +45,13 @@ ELSEIF (APPLE)
FIND_LIBRARY(PGSQL_LIBRARY
NAMES ${PGSQL_NAMES}
PATHS /opt/homebrew/lib)
ELSE (WIN32)
SET(PGSQL_NAMES pq)
FIND_LIBRARY(PGSQL_LIBRARY
NAMES ${PGSQL_NAMES}
PATHS /usr/lib
/usr/local/lib
/opt/homebrew/lib)
ENDIF()

IF (PGSQL_INCLUDE_DIR AND PGSQL_LIBRARY)
Expand Down
16 changes: 8 additions & 8 deletions ormpp/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,11 @@ class mysql {
template <typename T, typename... Args>
std::enable_if_t<iguana::ylt_refletable_v<T>, std::vector<T>> query_s(
const std::string &str, Args &&...args) {
constexpr auto SIZE = ylt::reflection::members_count_v<T>;
std::string sql = generate_query_sql<T>(str);
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
constexpr auto SIZE = iguana::get_value<T>();

stmt_ = mysql_stmt_init(con_);
if (!stmt_) {
Expand Down Expand Up @@ -482,7 +482,7 @@ class mysql {
index++;
}
},
std::make_index_sequence<std::tuple_size_v<T>>{});
std::make_index_sequence<SIZE>{});

if (index == 0) {
return {};
Expand All @@ -506,7 +506,7 @@ class mysql {
using U = ylt::reflection::remove_cvref_t<decltype(item)>;
if constexpr (iguana::ylt_refletable_v<U>) {
ylt::reflection::for_each(
item, [&param_binds, &index, &mp, &t, this](
item, [&param_binds, &index, &mp, this](
auto &field, auto /*name*/, auto /*index*/) {
set_value(param_binds.at(index), field, index, mp);
index++;
Expand All @@ -529,9 +529,9 @@ class mysql {
[&index, nulls](auto &item, auto /*index*/) {
using U = ylt::reflection::remove_cvref_t<decltype(item)>;
if constexpr (iguana::ylt_refletable_v<U>) {
ylt::reflection::for_each(item, [&index, nulls, &t](
auto &field, auto /*name*/,
auto /*index*/) {
ylt::reflection::for_each(item, [&index, nulls](auto &field,
auto /*name*/,
auto /*index*/) {
if (nulls.at(index++)) {
using W = ylt::reflection::remove_cvref_t<decltype(field)>;
if constexpr (is_optional_v<W>::value ||
Expand Down Expand Up @@ -562,11 +562,11 @@ class mysql {
template <typename T, typename... Args>
std::enable_if_t<iguana::ylt_refletable_v<T>, std::vector<T>> query(
Args &&...args) {
constexpr auto SIZE = ylt::reflection::members_count_v<T>;
std::string sql = generate_query_sql<T>(args...);
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
constexpr auto SIZE = iguana::get_value<T>();

stmt_ = mysql_stmt_init(con_);
if (!stmt_) {
Expand Down Expand Up @@ -945,7 +945,7 @@ class mysql {
template <auto... members, typename T, typename... Args>
int stmt_execute(const T &t, OptType type, Args &&...args) {
std::vector<MYSQL_BIND> param_binds;
constexpr auto arr = iguana::indexs_of<members...>();
constexpr auto arr = indexs_of<members...>();
if constexpr (sizeof...(members) > 0) {
(set_param_bind(
param_binds,
Expand Down
11 changes: 5 additions & 6 deletions ormpp/postgresql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ class postgresql {
const Arg &s, Args &&...args) {
static_assert(iguana::is_tuple<T>::value);
constexpr auto SIZE = std::tuple_size_v<T>;

std::string sql = s;
constexpr auto Args_Size = sizeof...(Args);
if (Args_Size != 0) {
Expand Down Expand Up @@ -449,8 +448,8 @@ class postgresql {
bool has_add_field = false;
for_each0(
tp,
[&sql, &i, &has_add_field, &unique_fields, field_name, name, this](
auto item, auto I) {
[&sql, &i, &has_add_field, &unique_fields, field_name, name,
this](auto item) {
if constexpr (std::is_same_v<decltype(item), ormpp_not_null> ||
std::is_same_v<decltype(item), ormpp_unique>) {
if (item.fields.find(field_name.data()) == item.fields.end())
Expand Down Expand Up @@ -522,8 +521,8 @@ class postgresql {
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
res_ =
PQprepare(con_, "", sql.data(), (int)iguana::get_value<T>(), nullptr);
res_ = PQprepare(con_, "", sql.data(), ylt::reflection::members_count_v<T>,
nullptr);
auto guard = guard_statment(res_);
return PQresultStatus(res_) == PGRES_COMMAND_OK;
}
Expand All @@ -532,7 +531,7 @@ class postgresql {
std::optional<uint64_t> stmt_execute(const T &t, OptType type,
Args &&...args) {
std::vector<std::vector<char>> param_values;
constexpr auto arr = iguana::indexs_of<members...>();
constexpr auto arr = indexs_of<members...>();
if constexpr (sizeof...(members) > 0) {
(set_param_values(
param_values,
Expand Down
1 change: 0 additions & 1 deletion ormpp/sqlite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ class sqlite {
const Arg &s, Args &&...args) {
static_assert(iguana::is_tuple<T>::value);
constexpr auto SIZE = std::tuple_size_v<T>;

std::string sql = s;
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
Expand Down
43 changes: 41 additions & 2 deletions ormpp/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ namespace ormpp {

//-------------------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------------------//
template <auto... members>
constexpr std::array<size_t, sizeof...(members)> indexs_of() {
return std::array<size_t, sizeof...(members)>{
ylt::reflection::index_of<members>()...};
}

template <typename... Args, typename F, std::size_t... Idx>
constexpr void for_each(std::tuple<Args...> &t, F &&f,
std::index_sequence<Idx...>) {
Expand Down Expand Up @@ -94,6 +100,39 @@ struct is_optional_v : std::false_type {};
template <typename T>
struct is_optional_v<std::optional<T>> : std::true_type {};

template <typename T>
constexpr std::enable_if_t<iguana::ylt_refletable_v<T>, size_t> get_value() {
return ylt::reflection::members_count_v<T>;
}

template <typename T>
constexpr std::enable_if_t<iguana::non_ylt_refletable_v<T>, size_t>
get_value() {
return 1;
}

template <typename... Args>
struct value_of;

template <typename T>
struct value_of<T> {
static const auto value = (get_value<T>());
};

template <typename T, typename... Rest>
struct value_of<T, Rest...> {
static const auto value = (value_of<T>::value + value_of<Rest...>::value);
};

template <typename List>
struct result_size;

template <template <class...> class List, class... T>
struct result_size<List<T...>> {
constexpr static const size_t value =
value_of<T...>::value; // (iguana::get_value<T>() + ...);
};

template <typename T>
inline void append_impl(std::string &sql, const T &str) {
if constexpr (std::is_same_v<std::string, T> ||
Expand Down Expand Up @@ -288,12 +327,12 @@ inline std::string generate_insert_sql(bool insert, Args &&...args) {
std::string fields = "(";
std::string values = "values(";
for (auto i = 0; i < Count; ++i) {
std::string field_name = iguana::get_name<T>(i).data();
std::string field_name = ylt::reflection::name_of<T>(i).data();
std::string value = "$" + std::to_string(++index);
append(set, field_name, "=", value);
fields += field_name;
values += value;
if (i < SIZE - 1) {
if (i < Count - 1) {
fields += ",";
values += ",";
set += ",";
Expand Down

0 comments on commit 21cbaf1

Please sign in to comment.