Skip to content

Commit

Permalink
add query_s delete_records_s
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacyking committed Feb 20, 2024
1 parent 2b2c473 commit f7f5e23
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 29 deletions.
11 changes: 5 additions & 6 deletions ormpp/dbng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,30 @@ class dbng {
return db_.template query_s<T>(str, std::forward<Args>(args)...);
}

// deprecated
template <typename T, typename... Args>
bool delete_records(Args &&...where_condition) {
[[deprecated]] bool delete_records(Args &&...where_condition) {
return db_.template delete_records<T>(
std::forward<Args>(where_condition)...);
}

// deprecated
// restriction, all the args are string, the first is the where condition,
// rest are append conditions
template <typename T, typename... Args>
std::vector<T> query(Args &&...args) {
[[deprecated]] std::vector<T> query(Args &&...args) {
return db_.template query<T>(std::forward<Args>(args)...);
}

// support member variable, such as: query(FID(simple::id), "<", 5)
template <typename Pair, typename U>
auto query(Pair pair, std::string_view oper, U &&val) {
[[deprecated]] auto query(Pair pair, std::string_view oper, U &&val) {
auto sql = build_condition(pair, oper, std::forward<U>(val));
using T = typename ormpp::field_attribute<decltype(pair.second)>::type;
return query<T>(sql);
}

template <typename Pair, typename U>
bool delete_records(Pair pair, std::string_view oper, U &&val) {
[[deprecated]] bool delete_records(Pair pair, std::string_view oper,
U &&val) {
auto sql = build_condition(pair, oper, std::forward<U>(val));
using T = typename ormpp::field_attribute<decltype(pair.second)>::type;
return delete_records<T>(sql);
Expand Down
9 changes: 3 additions & 6 deletions ormpp/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,8 @@ class mysql {

if constexpr (sizeof...(Args) > 0) {
size_t index = 0;
using expander = int[];
std::vector<MYSQL_BIND> param_binds;
expander{0, (set_param_bind(param_binds, args), 0)...};
(set_param_bind(param_binds, args), 0)...);
if (mysql_stmt_bind_param(stmt_, &param_binds[0])) {
set_last_error(mysql_stmt_error(stmt_));
return false;
Expand Down Expand Up @@ -338,9 +337,8 @@ class mysql {

if constexpr (sizeof...(Args) > 0) {
size_t index = 0;
using expander = int[];
std::vector<MYSQL_BIND> param_binds;
expander{0, (set_param_bind(param_binds, args), 0)...};
(set_param_bind(param_binds, args), ...);
if (mysql_stmt_bind_param(stmt_, &param_binds[0])) {
set_last_error(mysql_stmt_error(stmt_));
return {};
Expand Down Expand Up @@ -424,9 +422,8 @@ class mysql {

if constexpr (sizeof...(Args) > 0) {
size_t index = 0;
using expander = int[];
std::vector<MYSQL_BIND> param_binds;
expander{0, (set_param_bind(param_binds, args), 0)...};
(set_param_bind(param_binds, args), ...);
if (mysql_stmt_bind_param(stmt_, &param_binds[0])) {
set_last_error(mysql_stmt_error(stmt_));
return {};
Expand Down
9 changes: 3 additions & 6 deletions ormpp/postgresql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,9 @@ class postgresql {

if constexpr (sizeof...(Args) > 0) {
size_t index = 0;
using expander = int[];
std::vector<const char *> param_values_buf;
std::vector<std::vector<char>> param_values;
expander{0, (set_param_values(param_values, args), 0)...};
(set_param_values(param_values, args), ...);
for (auto &item : param_values) {
param_values_buf.push_back(item.data());
}
Expand Down Expand Up @@ -168,10 +167,9 @@ class postgresql {

if constexpr (sizeof...(Args) > 0) {
size_t index = 0;
using expander = int[];
std::vector<const char *> param_values_buf;
std::vector<std::vector<char>> param_values;
expander{0, (set_param_values(param_values, args), 0)...};
(set_param_values(param_values, args), ...);
for (auto &item : param_values) {
param_values_buf.push_back(item.data());
}
Expand Down Expand Up @@ -213,10 +211,9 @@ class postgresql {

if constexpr (sizeof...(Args) > 0) {
size_t index = 0;
using expander = int[];
std::vector<const char *> param_values_buf;
std::vector<std::vector<char>> param_values;
expander{0, (set_param_values(param_values, args), 0)...};
(set_param_values(param_values, args), ...);
for (auto &item : param_values) {
param_values_buf.push_back(item.data());
}
Expand Down
9 changes: 3 additions & 6 deletions ormpp/sqlite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ class sqlite {

if constexpr (sizeof...(Args) > 0) {
size_t index = 0;
using expander = int[];
expander{0, (set_param_bind(args, ++index), 0)...};
(set_param_bind(args, ++index), ...);
}

auto guard = guard_statment(stmt_);
Expand All @@ -164,8 +163,7 @@ class sqlite {

if constexpr (sizeof...(Args) > 0) {
size_t index = 0;
using expander = int[];
expander{0, (set_param_bind(args, ++index), 0)...};
(set_param_bind(args, ++index), ...);
}

auto guard = guard_statment(stmt_);
Expand Down Expand Up @@ -206,8 +204,7 @@ class sqlite {

if constexpr (sizeof...(Args) > 0) {
size_t index = 0;
using expander = int[];
expander{0, (set_param_bind(args, ++index), 0)...};
(set_param_bind(args, ++index), ...);
}

auto guard = guard_statment(stmt_);
Expand Down
11 changes: 6 additions & 5 deletions ormpp/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,16 +446,17 @@ inline void get_sql_conditions(std::string &sql, const std::string &arg,

template <typename T, typename... Args>
inline std::string generate_query_sql(Args &&...args) {
bool where = true;
bool where = false;
auto name = get_name<T>();
std::string sql = "select ";
auto fields = get_fields<T>();
append(sql, fields.data(), "from", name.data());
if constexpr (sizeof...(Args) > 0) {
if (where && !is_empty(std::forward<Args>(args)...)) {
append(sql, "where 1=1 and ");
where = false;
}
using expander = int[];
expander{0, (where = where ? where : !is_empty(args), 0)...};
}
if (where) {
append(sql, "where 1=1 and ");
}
get_sql_conditions(sql, std::forward<Args>(args)...);
return sql;
Expand Down

0 comments on commit f7f5e23

Please sign in to comment.