Skip to content

Commit

Permalink
add update&replace api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacyking committed Dec 13, 2023
1 parent 49d752c commit 67b726f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
20 changes: 8 additions & 12 deletions include/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,20 +670,14 @@ class mysql {
int insert_impl(OptType type, const T &t, bool get_insert_id,
Args &&...args) {
std::string sql = generate_insert_sql<T>(type == OptType::insert);
if (insert_or_update_impl(t, sql, type) == INT_MIN) {
return INT_MIN;
}
return get_insert_id ? stmt_->mysql->insert_id : 1;
return insert_or_update_impl(t, sql, type, get_insert_id);
}

template <typename T, typename... Args>
int insert_impl(OptType type, const std::vector<T> &v, bool get_insert_id,
Args &&...args) {
std::string sql = generate_insert_sql<T>(type == OptType::insert);
if (insert_or_update_impl(v, sql, type) == INT_MIN) {
return INT_MIN;
}
return get_insert_id ? stmt_->mysql->insert_id : (int)v.size();
return insert_or_update_impl(v, sql, type, get_insert_id);
}

template <typename T, typename... Args>
Expand All @@ -699,7 +693,8 @@ class mysql {
}

template <typename T>
int insert_or_update_impl(const T &t, const std::string &sql, OptType type) {
int insert_or_update_impl(const T &t, const std::string &sql, OptType type,
bool get_insert_id = false) {
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
Expand All @@ -721,12 +716,12 @@ class mysql {
return INT_MIN;
}

return 1;
return get_insert_id ? stmt_->mysql->insert_id : 1;
}

template <typename T>
int insert_or_update_impl(const std::vector<T> &v, const std::string &sql,
OptType type) {
OptType type, bool get_insert_id = false) {
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
Expand Down Expand Up @@ -754,7 +749,8 @@ class mysql {
}
}

return commit() ? (int)v.size() : INT_MIN;
return commit() ? get_insert_id ? stmt_->mysql->insert_id : (int)v.size()
: INT_MIN;
}

template <typename... Args>
Expand Down
21 changes: 9 additions & 12 deletions include/sqlite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,20 +489,14 @@ class sqlite {
int insert_impl(OptType type, const T &t, bool get_insert_id,
Args &&...args) {
std::string sql = generate_insert_sql<T>(type == OptType::insert);
if (insert_or_update_impl(t, sql, type) == INT_MIN) {
return INT_MIN;
}
return get_insert_id ? sqlite3_last_insert_rowid(handle_) : 1;
return insert_or_update_impl(t, sql, type, get_insert_id);
}

template <typename T, typename... Args>
int insert_impl(OptType type, const std::vector<T> &v, bool get_insert_id,
Args &&...args) {
std::string sql = generate_insert_sql<T>(type == OptType::insert);
if (insert_or_update_impl(v, sql, type) == INT_MIN) {
return INT_MIN;
}
return get_insert_id ? sqlite3_last_insert_rowid(handle_) : (int)v.size();
return insert_or_update_impl(v, sql, type, get_insert_id);
}

template <typename T, typename... Args>
Expand All @@ -518,7 +512,8 @@ class sqlite {
}

template <typename T>
int insert_or_update_impl(const T &t, const std::string &sql, OptType type) {
int insert_or_update_impl(const T &t, const std::string &sql, OptType type,
bool get_insert_id = false) {
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
Expand All @@ -535,12 +530,12 @@ class sqlite {
return INT_MIN;
}

return 1;
return get_insert_id ? sqlite3_last_insert_rowid(handle_) : 1;
}

template <typename T>
int insert_or_update_impl(const std::vector<T> &v, const std::string &sql,
OptType type) {
OptType type, bool get_insert_id = false) {
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
Expand Down Expand Up @@ -569,7 +564,9 @@ class sqlite {
}
}

return commit() ? (int)v.size() : INT_MIN;
return commit() ? get_insert_id ? sqlite3_last_insert_rowid(handle_)
: (int)v.size()
: INT_MIN;
}

private:
Expand Down
10 changes: 7 additions & 3 deletions include/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ inline std::string generate_update_sql(Args &&...args) {
fields += field_name;
#endif
#ifdef ORMPP_ENABLE_PG
append(fields, "=", "$" + std::to_string(++index));
append(fields, " =", "$" + std::to_string(++index));
#else
fields += " = ?";
#endif
Expand All @@ -349,11 +349,15 @@ inline std::string generate_update_sql(Args &&...args) {
}
std::string conflict = "where 1=1";
if constexpr (sizeof...(Args) > 0) {
append(conflict, " and ", args...);
append(conflict, " and", args...);
}
else {
for (const auto &it : get_conflict_keys<T>()) {
append(conflict, " and ", it, " = ?");
#ifdef ORMPP_ENABLE_PG
append(conflict, " and", it, "=", "$" + std::to_string(++index));
#else
append(conflict, " and", it, "= ?");
#endif
}
}
append(sql, fields, conflict);
Expand Down

0 comments on commit 67b726f

Please sign in to comment.