-
Notifications
You must be signed in to change notification settings - Fork 289
add update&replace api #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
96af55d
add update&replace api
Jacyking 84cee7e
Merge branch 'master' of https://github.com/Jacyking/ormpp
Jacyking 49d752c
add update&replace api
Jacyking 67b726f
add update&replace api
Jacyking a48743b
add update&replace api
Jacyking 05638ce
add update replace ut
Jacyking 81f77c9
add update&replace ut
Jacyking 1466d33
add update replace ut
Jacyking b281b05
add update&replace ut
Jacyking e140556
add update&replace ut
Jacyking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,23 +100,35 @@ class mysql { | |
|
||
template <typename T, typename... Args> | ||
int insert(const T &t, bool get_insert_id = false, Args &&...args) { | ||
return insert_impl(false, t, get_insert_id, std::forward<Args>(args)...); | ||
return insert_impl(OptType::insert, t, get_insert_id, | ||
std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename T, typename... Args> | ||
int insert(const std::vector<T> &t, bool get_insert_id = false, | ||
Args &&...args) { | ||
return insert_impl(false, t, get_insert_id, std::forward<Args>(args)...); | ||
return insert_impl(OptType::insert, t, get_insert_id, | ||
std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename T, typename... Args> | ||
int replace(const T &t, Args &&...args) { | ||
return insert_impl(OptType::replace, t, false, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename T, typename... Args> | ||
int replace(const std::vector<T> &t, Args &&...args) { | ||
return insert_impl(OptType::replace, t, false, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename T, typename... Args> | ||
int update(const T &t, Args &&...args) { | ||
return insert_impl(true, t, false, std::forward<Args>(args)...); | ||
return update_impl(t, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename T, typename... Args> | ||
int update(const std::vector<T> &t, Args &&...args) { | ||
return insert_impl(true, t, false, std::forward<Args>(args)...); | ||
return update_impl(t, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename T, typename... Args> | ||
|
@@ -617,18 +629,25 @@ class mysql { | |
} | ||
|
||
template <typename T> | ||
int stmt_execute(bool update, const T &t) { | ||
reset_error(); | ||
int stmt_execute(const T &t, OptType type, bool condition) { | ||
std::vector<MYSQL_BIND> param_binds; | ||
|
||
iguana::for_each(t, [&t, ¶m_binds, update, this](auto item, auto i) { | ||
if (is_auto_key(iguana::get_name<T>(), iguana::get_name<T>(i).data()) && | ||
!update) { | ||
iguana::for_each(t, [&t, ¶m_binds, type, this](auto item, auto i) { | ||
if (type == OptType::insert && | ||
is_auto_key<T>(iguana::get_name<T>(i).data())) { | ||
return; | ||
} | ||
set_param_bind(param_binds, t.*item); | ||
}); | ||
|
||
if (condition && type == OptType::update) { | ||
iguana::for_each(t, [&t, ¶m_binds, this](auto item, auto i) { | ||
if (is_conflict_key<T>( | ||
"`" + std::string(iguana::get_name<T>(i).data()) + "`")) { | ||
set_param_bind(param_binds, t.*item); | ||
} | ||
}); | ||
} | ||
|
||
if (mysql_stmt_bind_param(stmt_, ¶m_binds[0])) { | ||
set_last_error(mysql_stmt_error(stmt_)); | ||
return INT_MIN; | ||
|
@@ -648,9 +667,38 @@ class mysql { | |
} | ||
|
||
template <typename T, typename... Args> | ||
int insert_impl(bool update, const T &t, bool get_insert_id = false, | ||
int insert_impl(OptType type, const T &t, bool get_insert_id, | ||
Args &&...args) { | ||
std::string sql = generate_insert_sql<T>(update); | ||
std::string sql = generate_insert_sql<T>(type == OptType::insert); | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. args是多余的吗,没看到哪里有用到? |
||
std::string sql = generate_insert_sql<T>(type == OptType::insert); | ||
return insert_or_update_impl(v, sql, type, get_insert_id); | ||
} | ||
|
||
template <typename T, typename... 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); | ||
} | ||
|
||
template <typename T, typename... 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); | ||
} | ||
|
||
template <typename T> | ||
int insert_or_update_impl(const T &t, const std::string &sql, OptType type, | ||
bool get_insert_id = false, bool condition = true) { | ||
#ifdef ORMPP_ENABLE_LOG | ||
std::cout << sql << std::endl; | ||
#endif | ||
|
@@ -667,18 +715,18 @@ class mysql { | |
|
||
auto guard = guard_statment(stmt_); | ||
|
||
if (stmt_execute(update, t) == INT_MIN) { | ||
if (stmt_execute(t, type, condition) == INT_MIN) { | ||
set_last_error(mysql_stmt_error(stmt_)); | ||
return INT_MIN; | ||
} | ||
|
||
return get_insert_id ? stmt_->mysql->insert_id : 1; | ||
} | ||
|
||
template <typename T, typename... Args> | ||
int insert_impl(bool update, const std::vector<T> &t, | ||
bool get_insert_id = false, Args &&...args) { | ||
std::string sql = generate_insert_sql<T>(update); | ||
template <typename T> | ||
int insert_or_update_impl(const std::vector<T> &v, const std::string &sql, | ||
OptType type, bool get_insert_id = false, | ||
bool condition = true) { | ||
#ifdef ORMPP_ENABLE_LOG | ||
std::cout << sql << std::endl; | ||
#endif | ||
|
@@ -699,14 +747,14 @@ class mysql { | |
return INT_MIN; | ||
} | ||
|
||
for (auto &item : t) { | ||
if (stmt_execute(update, item) == INT_MIN) { | ||
for (auto &item : v) { | ||
if (stmt_execute(item, type, condition) == INT_MIN) { | ||
rollback(); | ||
return INT_MIN; | ||
} | ||
} | ||
|
||
return commit() ? (get_insert_id ? stmt_->mysql->insert_id : (int)t.size()) | ||
return commit() ? get_insert_id ? stmt_->mysql->insert_id : (int)v.size() | ||
: INT_MIN; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要用连加,效率低。