Result Query::store_next() { #if MYSQL_VERSION_ID > 41000 // only in MySQL v4.1 + if (lock()) { if (throw_exceptions()) { throw LockFailed(); } else { return Result(); } } int ret; if ((ret = mysql_next_result(&conn_->mysql_)) == 0) { // There are more results, so return next result set. MYSQL_RES* res = mysql_store_result(&conn_->mysql_); unlock(); if (res) { return Result(res, throw_exceptions()); } else { // Result set is null, but throw an exception only i it is // null because of some error. If not, it's just an empty // result set, which is harmless. We return an empty result // set if exceptions are disabled, as well. if (conn_->errnum() && throw_exceptions()) { throw BadQuery(error()); } else { return Result(); } } } else { // No more results, or some other error occurred. unlock(); if (throw_exceptions()) { if (ret > 0) { throw BadQuery(error()); } else { throw EndOfResultSets(); } } else { return Result(); } } #else return store(); #endif // MySQL v4.1+ }
//-------------------------------------------------------------------------- ResUse Connection::use(const string &str, bool throw_excptns) { Success = false; if (lock()) if (throw_excptns) throw BadQuery(error()); else return ResUse(); Success = !mysql_query(&mysql, str.c_str()); if (!Success) if (throw_excptns) throw BadQuery(error()); else return ResUse(); return ResUse(mysql_use_result(&mysql), this); }
//-------------------------------------------------------------------------- Result Connection::store(const string &str, bool throw_excptns) { Success = false; if (lock()) if (throw_excptns) throw BadQuery(error()); else return Result(); Success = !mysql_query(&mysql, str.c_str()); unlock(); if (!Success) if (throw_excptns) throw BadQuery(error()); else return Result(); return Result(mysql_store_result(&mysql)); }
//-------------------------------------------------------------------------- ResNSel Connection::execute(const string &str, bool throw_excptns) { Success = false; if (lock()) if (throw_excptns) throw BadQuery(error()); else return ResNSel(); Success = !mysql_query(&mysql, str.c_str()); unlock(); if (!Success) if (throw_excptns) throw BadQuery(error()); else return ResNSel(); return ResNSel(this); }
//-------------------------------------------------------------------------- bool Connection::real_connect (cchar *db, cchar *host, cchar *user, cchar *passwd, uint port, my_bool compress, unsigned int connect_timeout, const char *socket_name) { if (socket_name && socket_name[0]) mysql.options.unix_socket = (char *)socket_name; else mysql.options.unix_socket=NULL; mysql.options.port = port; mysql.options.compress = compress; mysql.options.connect_timeout=connect_timeout; locked = true; if (mysql_connect(&mysql, host, user, passwd)) { locked = false; Success = is_connected = true; } else { locked = false; Success = is_connected = false; if (throw_exceptions) throw BadQuery(error()); } if (!Success) return Success; if (db[0]) // if db is not empty Success = select_db(db); return Success; }
ResNSel Query::execute(const char* str, size_t len) { if (lock()) { success_ = false; if (throw_exceptions()) { throw LockFailed(); } else { return ResNSel(); } } success_ = !mysql_real_query(&conn_->mysql_, str, len); unlock(); if (success_) { return ResNSel(conn_); } else if (throw_exceptions()) { throw BadQuery(error()); } else { return ResNSel(); } }
bool Query::exec(const std::string& str) { success_ = !mysql_real_query(&conn_->mysql_, str.data(), static_cast<unsigned long>(str.length())); if (!success_ && throw_exceptions()) { throw BadQuery(error()); } else { return success_; } }
//-------------------------------------------------------------------------- bool Connection::connect (cchar *db, cchar *host, cchar *user, cchar *passwd) { locked = true; if (mysql_connect(&mysql, host, user, passwd)) { locked = false; Success = is_connected = true; } else { locked = false; if (throw_exceptions) throw BadQuery(error()); Success = is_connected = false; } if (!Success) return Success; if (db[0]) // if db is not empty Success = select_db(db); return Success; }
//-------------------------------------------------------------------------- Connection::Connection (const char *db, const char *host, const char *user, const char *passwd, bool te) : throw_exceptions(te), locked(false) { mysql_init(&mysql); if (connect (db, host, user, passwd)) { locked = false; Success = is_connected = true; } else { locked = false; Success = is_connected = false; if (throw_exceptions) throw BadQuery(error()); } }
//-------------------------------------------------------------------------- Connection::Connection (const char *db, const char *host, const char *user, const char *passwd, uint port, my_bool compress, unsigned int connect_timeout, bool te, const char *socket_name) : throw_exceptions(te), locked(false) { mysql_init(&mysql); if (real_connect (db, host, user, passwd, port, compress, connect_timeout,socket_name)) { locked = false; Success = is_connected = true; } else { locked = false; Success = is_connected = false; if (throw_exceptions) throw BadQuery(error()); } }
Result Query::store(const char* str, size_t len) { if (lock()) { success_ = false; if (throw_exceptions()) { throw LockFailed(); } else { return Result(); } } if (success_ = !mysql_real_query(&conn_->mysql_, str, len)) { MYSQL_RES* res = mysql_store_result(&conn_->mysql_); if (res) { unlock(); return Result(res, throw_exceptions()); } else { success_ = false; } } unlock(); // One of the MySQL API calls failed, but it's not an error if we // just get an empty result set. It happens when store()ing a query // that doesn't always return results. While it's better to use // exec*() in that situation, it's legal to call store() instead, // and sometimes you have no choice. For example, if the SQL comes // from outside the program so you can't predict whether there will // be results. if (conn_->errnum() && throw_exceptions()) { throw BadQuery(error()); } else { return Result(); } }
//-------------------------------------------------------------------------- bool Connection::exec(const string &str) { Success = !mysql_query(&mysql,str.c_str()); if (!Success && throw_exceptions) throw BadQuery(error()); return Success; }
void Query::executeImpl() { std::string query_string = query_buf.str(); if (mysql_real_query(conn->getDriver(), query_string.data(), query_string.size())) throw BadQuery(errorMessage(conn->getDriver()), mysql_errno(conn->getDriver())); }