diff --git a/include/mysql.hpp b/include/mysql.hpp index 7f800bad..403ba31f 100644 --- a/include/mysql.hpp +++ b/include/mysql.hpp @@ -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(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 int insert_impl(OptType type, const std::vector &v, bool get_insert_id, Args &&...args) { std::string sql = generate_insert_sql(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 @@ -699,7 +693,8 @@ class mysql { } template - 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 @@ -721,12 +716,12 @@ class mysql { return INT_MIN; } - return 1; + return get_insert_id ? stmt_->mysql->insert_id : 1; } template int insert_or_update_impl(const std::vector &v, const std::string &sql, - OptType type) { + OptType type, bool get_insert_id = false) { #ifdef ORMPP_ENABLE_LOG std::cout << sql << std::endl; #endif @@ -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 diff --git a/include/sqlite.hpp b/include/sqlite.hpp index 1ca43627..ce35781f 100644 --- a/include/sqlite.hpp +++ b/include/sqlite.hpp @@ -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(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 int insert_impl(OptType type, const std::vector &v, bool get_insert_id, Args &&...args) { std::string sql = generate_insert_sql(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 @@ -518,7 +512,8 @@ class sqlite { } template - 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 @@ -535,12 +530,12 @@ class sqlite { return INT_MIN; } - return 1; + return get_insert_id ? sqlite3_last_insert_rowid(handle_) : 1; } template int insert_or_update_impl(const std::vector &v, const std::string &sql, - OptType type) { + OptType type, bool get_insert_id = false) { #ifdef ORMPP_ENABLE_LOG std::cout << sql << std::endl; #endif @@ -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: diff --git a/include/utility.hpp b/include/utility.hpp index 51b8495e..28a4c87b 100644 --- a/include/utility.hpp +++ b/include/utility.hpp @@ -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 @@ -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()) { - 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);