Author: wyoung
Date: Fri Jul 13 06:52:55 2007
New Revision: 1691
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1691&view=rev
Log:
Connection now has its own internal error message, used for problems
detected up at the MySQL++ level. We clear this out just before any
MySQL C API call, to allow mysql_error() messages to shine through
via the Connection::error() interface.
Modified:
trunk/Wishlist
trunk/lib/connection.cpp
trunk/lib/connection.h
Modified: trunk/Wishlist
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=1691&r1=1690&r2=1691&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Fri Jul 13 06:52:55 2007
@@ -59,10 +59,6 @@
made into a base class that all SSQLSes derive from. Some
template functions like Query::insert<T> might become regular
member functions, taking a reference to the SSQLS base class.
-
- o Save error message built in Connection::bad_option() in an
- instance variable, used by Connection::error() when set.
- Useful when exceptions are disabled.
o Remove MutableColData. It's only used within myset.h, and that
usage is inefficient to the point of wanting replacement anyway.
Modified: trunk/lib/connection.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.cpp?rev=1691&r1=1690&r2=1691&view=diff
==============================================================================
--- trunk/lib/connection.cpp (original)
+++ trunk/lib/connection.cpp Fri Jul 13 06:52:55 2007
@@ -163,6 +163,15 @@
}
+void
+Connection::build_error_message(const char* core)
+{
+ error_message_ = "Can't ";
+ error_message_ += core;
+ error_message_ += " while disconnected";
+}
+
+
bool
Connection::connect(cchar* db, cchar* password, cchar* user,
cchar* server, unsigned long client_flag)
@@ -189,6 +198,7 @@
// Figure out what the server parameter means, then establish
// the connection.
+ error_message_.clear();
unsigned int port = 0;
string host, socket_name;
scoped_var_set<bool> sb(connecting_, true);
@@ -246,6 +256,7 @@
disconnect();
}
+ error_message_.clear();
mysql_init(&mysql_);
set_exceptions(other.throw_exceptions());
@@ -264,6 +275,7 @@
void
Connection::disconnect()
{
+ error_message_.clear();
mysql_close(&mysql_);
is_connected_ = false;
}
@@ -288,6 +300,7 @@
bool
Connection::select_db(const char *db)
{
+ error_message_.clear();
if (connected()) {
bool suc = !(mysql_select_db(&mysql_, db));
if (throw_exceptions() && !suc) {
@@ -302,6 +315,7 @@
throw DBSelectionFailed("MySQL++ connection not
established");
}
else {
+ build_error_message("select a database");
return false;
}
}
@@ -311,6 +325,7 @@
bool
Connection::reload()
{
+ error_message_.clear();
if (connected()) {
bool suc = !mysql_reload(&mysql_);
if (throw_exceptions() && !suc) {
@@ -329,6 +344,7 @@
throw BadQuery("MySQL++ connection not established");
}
else {
+ build_error_message("reload grant tables");
return false;
}
}
@@ -338,6 +354,7 @@
bool
Connection::shutdown()
{
+ error_message_.clear();
if (connected()) {
bool suc = !(mysql_shutdown(&mysql_ SHUTDOWN_ARG));
if (throw_exceptions() && !suc) {
@@ -352,6 +369,7 @@
throw ConnectionFailed("MySQL++ connection not
established");
}
else {
+ build_error_message("shutdown database server");
return false;
}
}
@@ -361,6 +379,7 @@
string
Connection::info()
{
+ error_message_.clear();
const char* i = mysql_info(&mysql_);
if (!i) {
return string();
@@ -615,6 +634,7 @@
bool
Connection::set_option_impl(mysql_option moption, const void* arg)
{
+ error_message_.clear();
return !mysql_options(&mysql_, moption,
static_cast<const char*>(arg));
}
@@ -624,6 +644,7 @@
bool
Connection::set_option_impl(enum_mysql_set_option msoption)
{
+ error_message_.clear();
return !mysql_set_server_option(&mysql_, msoption);
}
#endif
@@ -632,38 +653,41 @@
bool
Connection::bad_option(Option option, OptionError error)
{
+ ostringstream os;
+
+ switch (error) {
+ case opt_err_type: {
+ // Option was set using wrong argument type
+ OptionArgType type = option_arg_type(option);
+ os << "option " << option;
+ if (type == opt_type_none) {
+ os << " does not take an argument";
+ }
+ else {
+ os << " requires an argument of type " << type;
+ }
+ break;
+ }
+
+ case opt_err_value:
+ // C API rejected option, which probably indicates that
+ // you passed a option that it doesn't understand.
+ os << "option " << option << " not supported in MySQL C
"
+ "API v";
+ api_version(os);
+ break;
+
+ case opt_err_conn:
+ os << "option " << option << " can only be set before "
+ "connection is established";
+ break;
+ }
+
if (throw_exceptions()) {
- ostringstream os;
-
- switch (error) {
- case opt_err_type: {
- // Option was set using wrong argument type
- OptionArgType type = option_arg_type(option);
- os << "option " << option;
- if (type == opt_type_none) {
- os << " does not take an argument";
- }
- else {
- os << " requires an argument of type "
<< type;
- }
- break;
- }
-
- case opt_err_value:
- // C API rejected option, which probably
indicates that
- // you passed a option that it doesn't
understand.
- os << "option " << option << " not supported in
MySQL "
- "C API v";
- api_version(os);
- break;
-
- case opt_err_conn:
- os << "option " << option << " can only be set "
- "before connection is
established";
- break;
- }
-
throw BadOption(os.str(), option);
+ }
+ else {
+ error_message_ = os.str();
}
return false;
@@ -704,7 +728,10 @@
const char* ca, const char* capath, const char* cipher)
{
#if defined(HAVE_MYSQL_SSL_SET)
+ error_message_.clear();
mysql_ssl_set(&mysql_, key, cert, ca, capath, cipher);
+#else
+ error_message_ = "SSL not enabled in MySQL++";
#endif
}
@@ -726,11 +753,13 @@
Connection::ping()
{
if (connected()) {
+ error_message_.clear();
return mysql_ping(&mysql_);
}
else {
// Not connected, and we've forgotten everything we need in
// order to re-connect, if we once were connected.
+ build_error_message("ping database server");
return 1;
}
}
@@ -774,9 +803,9 @@
if (isdigit(service[0])) {
port = atoi(service);
if ((port < 1) || (port > USHRT_MAX)) {
+ error_message_ = "Invalid TCP port
number ";
+ error_message_ += service;
return false;
- }
- else {
}
}
else {
@@ -785,6 +814,8 @@
port = ntohs(pse->s_port);
}
else {
+ error_message_ = "Failed to look up TCP
service ";
+ error_message_ += service;
return false;
}
}
Modified: trunk/lib/connection.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.h?rev=1691&r1=1690&r2=1691&view=diff
==============================================================================
--- trunk/lib/connection.h (original)
+++ trunk/lib/connection.h Fri Jul 13 06:52:55 2007
@@ -240,8 +240,14 @@
/// \brief Return error message for last MySQL error associated with
/// this connection.
///
- /// Simply wraps \c mysql_error() in the C API.
- const char* error() { return mysql_error(&mysql_); }
+ /// Can return a MySQL++ Connection-specific error message if there
+ /// is one. If not, it simply wraps \c mysql_error() in the C API.
+ const char* error()
+ {
+ return error_message_.empty() ?
+ mysql_error(&mysql_) :
+ error_message_.c_str();
+ }
/// \brief Return last MySQL error number associated with this
/// connection
@@ -519,6 +525,11 @@
bool parse_ipc_method(const char* server, std::string& host,
unsigned int& port, std::string& socket_name);
+ /// \brief Build an error message in a standard form.
+ ///
+ /// This is used when MySQL++ itself encounters an error.
+ void build_error_message(const char* core);
+
private:
friend class ResNSel;
friend class ResUse;
@@ -571,6 +582,7 @@
bool is_connected_;
bool connecting_;
bool copacetic_;
+ std::string error_message_;
OptionList applied_options_;
static OptionArgType legal_opt_arg_types_[];
};
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits