Пример #1
0
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+
}
Пример #2
0
//--------------------------------------------------------------------------
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);
}
Пример #3
0
//--------------------------------------------------------------------------
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));
}
Пример #4
0
//--------------------------------------------------------------------------
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);
}
Пример #5
0
//--------------------------------------------------------------------------
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;
}
Пример #6
0
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();
	}
}
Пример #7
0
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_;
	}
}
Пример #8
0
//--------------------------------------------------------------------------
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;
}
Пример #9
0
//--------------------------------------------------------------------------
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());
  }
}
Пример #10
0
//--------------------------------------------------------------------------
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());
  }
}
Пример #11
0
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();
	}
}
Пример #12
0
//--------------------------------------------------------------------------
bool Connection::exec(const string &str) 
{
	Success = !mysql_query(&mysql,str.c_str());
	if (!Success && throw_exceptions) throw BadQuery(error());
	return Success;
}
Пример #13
0
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()));
}