Skip to content

Commit

Permalink
mysql_stmt_prepare replace mysql_query
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacyking committed Jan 31, 2024
1 parent 74d243d commit 97c61af
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
22 changes: 12 additions & 10 deletions include/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,8 @@ class mysql {

template <typename T, typename... Args>
bool delete_records(Args &&...where_conditon) {
reset_error();
auto sql = generate_delete_sql<T>(std::forward<Args>(where_conditon)...);
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
if (mysql_query(con_, sql.data())) {
set_last_error(mysql_error(con_));
return false;
}
return true;
return execute(sql);
}

int get_last_affect_rows() { return (int)mysql_affected_rows(con_); }
Expand Down Expand Up @@ -522,10 +514,20 @@ class mysql {
std::cout << sql << std::endl;
#endif
reset_error();
if (mysql_query(con_, sql.data()) != 0) {
stmt_ = mysql_stmt_init(con_);
if (!stmt_) {
set_last_error(mysql_error(con_));
return false;
}
auto guard = guard_statment(stmt_);
if (mysql_stmt_prepare(stmt_, sql.c_str(), (unsigned long)sql.size())) {
set_last_error(mysql_stmt_error(stmt_));
return false;
}
if (mysql_stmt_execute(stmt_)) {
set_last_error(mysql_stmt_error(stmt_));
return false;
}
return true;
}

Expand Down
17 changes: 1 addition & 16 deletions include/sqlite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,7 @@ class sqlite {
template <typename T, typename... Args>
bool delete_records(Args &&...where_conditon) {
auto sql = generate_delete_sql<T>(std::forward<Args>(where_conditon)...);
#ifdef ORMPP_ENABLE_LOG
std::cout << sql << std::endl;
#endif
int result = sqlite3_prepare_v2(handle_, sql.data(), (int)sql.size(),
&stmt_, nullptr);
if (result != SQLITE_OK) {
set_last_error(sqlite3_errmsg(handle_));
return false;
}

auto guard = guard_statment(stmt_);
if (sqlite3_step(stmt_) != SQLITE_DONE) {
set_last_error(sqlite3_errmsg(handle_));
return false;
}
return true;
return execute(sql);
}

// restriction, all the args are string, the first is the where condition,
Expand Down

0 comments on commit 97c61af

Please sign in to comment.