Given that I do not want to use exceptions in this code, how can I remove the duplicated:
if (rc != SQLITE_OK) {
return rc;
}
checks after most statements in this function below?
int sqlite::update(const std::string& table_name, const std::vector<column_values>& fields, const std::vector<column_values>& where) {
if (db_ == nullptr) {
return SQLITE_ERROR;
}
const std::string sql = update_helper(table_name, fields, where);
sqlite3_stmt* stmt = NULL;
int rc = sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt, NULL);
if(rc != SQLITE_OK) {
return rc;
}
// loop thru each parameter, calling bind for each one
rc = bind_fields(stmt, fields);
if(rc != SQLITE_OK) {
return rc;
}
// loop thru each where parameter, calling bind for each one
rc = bind_where(stmt, where);
if (rc != SQLITE_OK) {
return rc;
}
return step_and_finalise(stmt);
}
First off, you are leaking the
sqlite3_stmtif something goes wrong. You need to callsqlite3_finalize()beforereturn'ing anything.As for your question, you can wrap the duplicate
if..returns in a preprocessor macro, eg:Alternatively:
Or, you can re-write the code to not use multiple
returns, eg:Or, you can throw an exception, eg: