Skip to content

Commit

Permalink
add get insert id after insert
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacyking committed Dec 14, 2023
1 parent 71f0946 commit 4ec114d
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 147 deletions.
31 changes: 20 additions & 11 deletions include/dbng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@ class dbng {
}

template <typename T, typename... Args>
int insert(const T &t, bool get_insert_id = false, Args &&...args) {
return db_.insert(t, get_insert_id, std::forward<Args>(args)...);
int insert(const T &t, Args &&...args) {
return db_.insert(t, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
int insert(const std::vector<T> &t, bool get_insert_id = false,
Args &&...args) {
return db_.insert(t, get_insert_id, std::forward<Args>(args)...);
int insert(const std::vector<T> &v, Args &&...args) {
return db_.insert(v, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
int replace(const T &t, Args &&...args) {
return db_.replace(t, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
int replace(const std::vector<T> &v, Args &&...args) {
return db_.replace(v, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
Expand All @@ -50,18 +59,18 @@ class dbng {
}

template <typename T, typename... Args>
int update(const std::vector<T> &t, Args &&...args) {
return db_.update(t, std::forward<Args>(args)...);
int update(const std::vector<T> &v, Args &&...args) {
return db_.update(v, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
int replace(const T &t, Args &&...args) {
return db_.replace(t, std::forward<Args>(args)...);
uint64_t get_insert_id_after_insert(const T &t, Args &&...args) {
return db_.get_insert_id_after_insert(t, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
int replace(const std::vector<T> &t, Args &&...args) {
return db_.replace(t, std::forward<Args>(args)...);
uint64_t get_insert_id_after_insert(const std::vector<T> &v, Args &&...args) {
return db_.get_insert_id_after_insert(v, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
Expand Down
101 changes: 61 additions & 40 deletions include/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,49 @@ class mysql {
}

template <typename T, typename... Args>
constexpr int insert(const T &t, bool get_insert_id, Args &&...args) {
return insert_impl(OptType::insert, t, get_insert_id,
std::forward<Args>(args)...);
int insert(const T &t, Args &&...args) {
return insert_impl(OptType::insert, t, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
constexpr int insert(const std::vector<T> &t, bool get_insert_id,
Args &&...args) {
return insert_impl(OptType::insert, t, get_insert_id,
std::forward<Args>(args)...);
int insert(const std::vector<T> &v, Args &&...args) {
return insert_impl(OptType::insert, v, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
constexpr int replace(const T &t, Args &&...args) {
return insert_impl(OptType::replace, t, false, std::forward<Args>(args)...);
int replace(const T &t, Args &&...args) {
return insert_impl(OptType::replace, t, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
constexpr int replace(const std::vector<T> &t, Args &&...args) {
return insert_impl(OptType::replace, t, false, std::forward<Args>(args)...);
int replace(const std::vector<T> &v, Args &&...args) {
return insert_impl(OptType::replace, v, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
constexpr int update(const T &t, Args &&...args) {
int update(const T &t, Args &&...args) {
return update_impl(t, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
constexpr int update(const std::vector<T> &t, Args &&...args) {
return update_impl(t, std::forward<Args>(args)...);
int update(const std::vector<T> &v, Args &&...args) {
return update_impl(v, std::forward<Args>(args)...);
}

template <typename T, typename... Args>
uint64_t get_insert_id_after_insert(const T &t, Args &&...args) {
std::optional<uint64_t> insert_id = {0};
insert_or_update_impl(t, generate_insert_sql<T>(true), OptType::insert,
std::move(insert_id));
return insert_id.value();
}

template <typename T, typename... Args>
uint64_t get_insert_id_after_insert(const std::vector<T> &v, Args &&...args) {
std::optional<uint64_t> insert_id = {0};
insert_or_update_impl(v, generate_insert_sql<T>(true), OptType::insert,
std::move(insert_id));
return insert_id.value();
}

template <typename T, typename... Args>
Expand Down Expand Up @@ -669,39 +682,37 @@ class mysql {
}

template <typename T, typename... Args>
constexpr int insert_impl(OptType type, const T &t, bool get_insert_id,
Args &&...args) {
std::string sql = generate_insert_sql<T>(type == OptType::insert);
return insert_or_update_impl(t, sql, type, get_insert_id);
int insert_impl(OptType type, const T &t, Args &&...args) {
return insert_or_update_impl(
t, generate_insert_sql<T>(type == OptType::insert), type, std::nullopt);
}

template <typename T, typename... Args>
constexpr 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);
return insert_or_update_impl(v, sql, type, get_insert_id);
int insert_impl(OptType type, const std::vector<T> &v, Args &&...args) {
return insert_or_update_impl(
v, generate_insert_sql<T>(type == OptType::insert), type, std::nullopt);
}

template <typename T, typename... Args>
constexpr int update_impl(const T &t, Args &&...args) {
int update_impl(const T &t, Args &&...args) {
bool condition = true;
std::string sql =
generate_update_sql<T>(condition, std::forward<Args>(args)...);
return insert_or_update_impl(t, sql, OptType::update, false, condition);
auto sql = generate_update_sql<T>(condition, std::forward<Args>(args)...);
return insert_or_update_impl(t, sql, OptType::update, std::nullopt,
condition);
}

template <typename T, typename... Args>
constexpr int update_impl(const std::vector<T> &v, Args &&...args) {
int update_impl(const std::vector<T> &v, Args &&...args) {
bool condition = true;
std::string sql =
generate_update_sql<T>(condition, std::forward<Args>(args)...);
return insert_or_update_impl(v, sql, OptType::update, false, condition);
auto sql = generate_update_sql<T>(condition, std::forward<Args>(args)...);
return insert_or_update_impl(v, sql, OptType::update, std::nullopt,
condition);
}

template <typename T>
constexpr int insert_or_update_impl(const T &t, const std::string &sql,
OptType type, bool get_insert_id = false,
bool condition = true) {
int insert_or_update_impl(const T &t, const std::string &sql, OptType type,
std::optional<uint64_t> &&insert_id,
bool condition = true) {
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
Expand All @@ -722,14 +733,17 @@ class mysql {
return INT_MIN;
}

return get_insert_id ? stmt_->mysql->insert_id : 1;
if (insert_id.has_value()) {
insert_id = {stmt_->mysql->insert_id};
}

return 1;
}

template <typename T>
constexpr int insert_or_update_impl(const std::vector<T> &v,
const std::string &sql, OptType type,
bool get_insert_id = false,
bool condition = true) {
int insert_or_update_impl(const std::vector<T> &v, const std::string &sql,
OptType type, std::optional<uint64_t> &&insert_id,
bool condition = true) {
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
Expand Down Expand Up @@ -757,8 +771,15 @@ class mysql {
}
}

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

if (insert_id.has_value()) {
insert_id = {stmt_->mysql->insert_id};
}

return (int)v.size();
}

template <typename... Args>
Expand Down
Loading

0 comments on commit 4ec114d

Please sign in to comment.