List:MySQL++« Previous MessageNext Message »
From:Jim Wallace Date:February 3 2009 5:38pm
Subject:Patch for query string in BadQuery
View as plain text  
This patch saves off the string passed into the Query object, and then
if it throws a BadQueryException, it passes the string to it so the
handler of the exception can access it.  Note if you pass in multiple
statements to Query, the classes cannot tell which one caused the error.
This changes the ABI since two objects change size, so Warren probably
won't roll it in until the next major release.

Index: exceptions.h
===================================================================
--- exceptions.h	(revision 2439)
+++ exceptions.h	(working copy)
@@ -288,20 +288,30 @@
 	///
 	/// \param w explanation for why the exception was thrown
 	/// \param e the error number from the underlying database API
-	explicit BadQuery(const char* w = "", int e = 0) :
+	/// \param s the SQL string that caused this exception
+	explicit BadQuery(const char* w = "", int e = 0, const
std::string *s = 0) :
 	Exception(w),
 	errnum_(e)
 	{
+		if ( s )
+		{
+			query_string_ = *s;				
+		}
 	}
 
 	/// \brief Create exception object
 	///
 	/// \param w explanation for why the exception was thrown
 	/// \param e the error number from the underlying database API
-	explicit BadQuery(const std::string& w, int e = 0) :
+	/// \param s the SQL string that caused this exception
+	explicit BadQuery(const std::string& w, int e = 0, const
std::string *s = 0) :
 	Exception(w),
 	errnum_(e)
 	{
+		if ( s )
+		{
+			query_string_ = *s;				
+		}
 	}
 
 	/// \brief Return the error number corresponding to the error
@@ -311,9 +321,19 @@
 	/// always.  See the overview documentation for this class for
the
 	/// reason for the difference.
 	int errnum() const { return errnum_; }
+
+	/// \brief Return the SQL string that caused the error
+	///
+	/// This is the entire string passed to MySQL.  If multiple
+	/// statements are passed in the string, the entire string
+	/// is returned since the exception does not know which
+	/// statement caused the exception.
+	const std::string &query_string() const { return query_string_;
}
+
 	
 private:	
 	int	errnum_;	///< error number associated with
execption
+	std::string query_string_;	///< SQL string associated with
execption
 };
 
 
Index: query.cpp
===================================================================
--- query.cpp	(revision 2439)
+++ query.cpp	(working copy)
@@ -151,6 +151,7 @@
 bool
 Query::exec(const std::string& str)
 {
+	query_string_ = str;
 	if ((copacetic_ = conn_->driver()->execute(str.data(),
 			static_cast<unsigned long>(str.length()))) ==
true) {
 		if (parse_elems_.size() == 0) {
@@ -160,7 +161,7 @@
 		return true;
 	}
 	else if (throw_exceptions()) {
-		throw BadQuery(error(), errnum());
+		throw BadQuery(error(), errnum(), &query_string_);
 	}
 	else {
 		return false;
@@ -197,6 +198,7 @@
 SimpleResult
 Query::execute(const char* str, size_t len)
 {
+	query_string_ = std::string(str, len);
 	if ((copacetic_ = conn_->driver()->execute(str, len)) == true) {
 		if (parse_elems_.size() == 0) {
 			// Not a template query, so auto-reset
@@ -205,7 +207,7 @@
 		return SimpleResult(conn_, insert_id(), affected_rows(),
info());
 	}
 	else if (throw_exceptions()) {
-		throw BadQuery(error(), errnum());
+		throw BadQuery(error(), errnum(), &query_string_);
 	}
 	else {
 		return SimpleResult();
@@ -488,6 +490,7 @@
 StoreQueryResult
 Query::store(const char* str, size_t len)
 {
+	query_string_ = std::string(str, len);
 	MYSQL_RES* res = 0;
 	if ((copacetic_ = conn_->driver()->execute(str, len)) == true) {
 		res = conn_->driver()->store_result();
@@ -515,7 +518,7 @@
 			return StoreQueryResult();
 		}
 		else if (throw_exceptions()) {
-			throw BadQuery(error(), errnum());
+			throw BadQuery(error(), errnum(),
&query_string_);
 		}
 		else {
 			return StoreQueryResult();
@@ -542,7 +545,7 @@
 			// 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(), errnum());
+				throw BadQuery(error(), errnum(),
&query_string_);
 			}
 			else {
 				return StoreQueryResult();
@@ -551,10 +554,10 @@
 	}
 	else if (throw_exceptions()) {
 		if (rc == DBDriver::nr_error) {
-			throw BadQuery(error(), errnum());
+			throw BadQuery(error(), errnum(),
&query_string_);
 		}
 		else if (conn_->errnum()) {
-			throw BadQuery(error(), errnum());
+			throw BadQuery(error(), errnum(),
&query_string_);
 		}
 		else {
 			return StoreQueryResult();	// normal
end-of-result-sets case
@@ -609,6 +612,7 @@
 UseQueryResult
 Query::use(const char* str, size_t len)
 {
+	query_string_ = std::string(str, len);
 	MYSQL_RES* res = 0;
 	if ((copacetic_ = conn_->driver()->execute(str, len)) == true) {
 		res = conn_->driver()->use_result();
@@ -632,7 +636,7 @@
 			return UseQueryResult();
 		}
 		else if (throw_exceptions()) {
-			throw BadQuery(error(), errnum());
+			throw BadQuery(error(), errnum(),
&query_string_);
 		}
 		else {
 			return UseQueryResult();
Index: query.h
===================================================================
--- query.h	(revision 2439)
+++ query.h	(working copy)
@@ -1076,6 +1076,9 @@
 	/// \brief String buffer for storing assembled query
 	std::stringbuf sbuffer_;
 
+	/// \brief String of the query passed to MySQL API for error
reporting
+	std::string query_string_;
+	
 	/// \brief Process a parameterized query list.
 	void proc(SQLQueryParms& p);

Thread
Patch for query string in BadQueryJim Wallace3 Feb