Author: wyoung
Date: Fri Jul 13 02:24:44 2007
New Revision: 1684
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1684&view=rev
Log:
- Renamed "success" data members in Connection, Query and ResNSel to
"copacetic_", making them private if need be. This better reflects
their actual use, which isn't to say that there has necessarily been
actual success, but rather that everything's okay with the object.
- Removed success() member functions; access copacetic_ via operator
bool in all cases.
- Decoupled Connection::copacetic_ from Connection::is_connected_. It
is now possible for the object to be copacetic without being
connected. However, if it tries to connect and fails, then it is not
copacetic. If it is copacetic and not connected, it means we haven't
even tried to connect yet, a useful distinction.
- While playing with ResNSel, made all of its data members private, and
added const accessor functions for all of them.
Modified:
trunk/Wishlist
trunk/examples/load_jpeg.cpp
trunk/lib/connection.cpp
trunk/lib/connection.h
trunk/lib/query.cpp
trunk/lib/query.h
trunk/lib/result.cpp
trunk/lib/result.h
Modified: trunk/Wishlist
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=1684&r1=1683&r2=1684&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Fri Jul 13 02:24:44 2007
@@ -67,11 +67,6 @@
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 Rename Query::success() and Query::success_. The names imply
- that they only have meaning once a query is tried, but that
- isn't true. They're an indicator of whether the object has
- had any problems at all, not just problems in query execution.
o Connection::error() returns const char*, while Query::error()
returns std::string. Pick one.
Modified: trunk/examples/load_jpeg.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/load_jpeg.cpp?rev=1684&r1=1683&r2=1684&view=diff
==============================================================================
--- trunk/examples/load_jpeg.cpp (original)
+++ trunk/examples/load_jpeg.cpp Fri Jul 13 02:24:44 2007
@@ -97,7 +97,7 @@
// If we get here, insertion succeeded
cout << "Inserted \"" << argv[argc] <<
"\" into images table, " << img_data.size() <<
- " bytes, ID " << res.insert_id << endl;
+ " bytes, ID " << res.insert_id() << endl;
}
catch (const BadQuery& er) {
// Handle any query errors
Modified: trunk/lib/connection.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.cpp?rev=1684&r1=1683&r2=1684&view=diff
==============================================================================
--- trunk/lib/connection.cpp (original)
+++ trunk/lib/connection.cpp Fri Jul 13 02:24:44 2007
@@ -107,7 +107,7 @@
Lockable(false),
is_connected_(false),
connecting_(false),
-success_(false)
+copacetic_(true)
{
mysql_init(&mysql_);
}
@@ -125,11 +125,11 @@
if (connect(db, host, user, passwd, port, compress,
connect_timeout, socket_name, client_flag)) {
unlock();
- success_ = is_connected_ = true;
+ copacetic_ = is_connected_ = true;
}
else {
unlock();
- success_ = is_connected_ = false;
+ copacetic_ = is_connected_ = false;
if (throw_exceptions()) {
throw ConnectionFailed(error());
}
@@ -195,22 +195,25 @@
if (mysql_real_connect(&mysql_, host, user, passwd, db, port,
socket_name, client_flag)) {
unlock();
- success_ = is_connected_ = true;
+ is_connected_ = true;
if (db && db[0]) {
// Also attach to given database
- success_ = select_db(db);
+ copacetic_ = select_db(db);
+ }
+ else {
+ copacetic_ = true;
}
}
else {
unlock();
- success_ = is_connected_ = false;
+ copacetic_ = is_connected_ = false;
if (throw_exceptions()) {
throw ConnectionFailed(error());
}
}
- return success_;
+ return is_connected_;
}
@@ -241,7 +244,7 @@
else {
is_connected_ = false;
connecting_ = false;
- success_ = false;
+ copacetic_ = other.copacetic_;
}
}
Modified: trunk/lib/connection.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.h?rev=1684&r1=1683&r2=1684&view=diff
==============================================================================
--- trunk/lib/connection.h (original)
+++ trunk/lib/connection.h Fri Jul 13 02:24:44 2007
@@ -195,12 +195,6 @@
return is_connected_;
}
- /// \brief Return true if the last query was successful
- bool success() const
- {
- return success_;
- }
-
/// \brief Alias for close()
void purge() { close(); }
@@ -213,22 +207,27 @@
/// is connected to.
Query query();
- /// \brief Alias for success()
- ///
- /// Alias for success() member function. Allows you to have code
- /// constructs like this:
+ /// \brief Test whether the connection has experienced an error
+ /// condition.
+ ///
+ /// Allows for code constructs like this:
///
/// \code
- /// Connection conn;
- /// .... use conn
- /// if (conn) {
- /// ... last SQL query was successful
- /// }
- /// else {
- /// ... error occurred in SQL query
- /// }
+ /// Connection conn;
+ /// .... use conn
+ /// if (conn) {
+ /// ... last SQL query was successful
+ /// }
+ /// else {
+ /// ... error occurred in SQL query
+ /// }
/// \endcode
- operator bool() { return success(); }
+ ///
+ /// Prior to version 3, this function could never return true if
+ /// we weren't connected. As of version 3, a true return simply
+ /// indicates a lack of errors; call connected() to test whether
+ /// the connection is established.
+ operator bool() const { return copacetic_; }
/// \brief Copy an existing Connection object's state into this
/// object.
@@ -567,7 +566,7 @@
MYSQL mysql_;
bool is_connected_;
bool connecting_;
- bool success_;
+ bool copacetic_;
OptionList applied_options_;
static OptionArgType legal_opt_arg_types_[];
};
Modified: trunk/lib/query.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/query.cpp?rev=1684&r1=1683&r2=1684&view=diff
==============================================================================
--- trunk/lib/query.cpp (original)
+++ trunk/lib/query.cpp Fri Jul 13 02:24:44 2007
@@ -42,10 +42,9 @@
Lockable(false),
template_defaults(this),
conn_(c),
-success_(false)
+copacetic_(true)
{
init(&sbuffer_);
- success_ = true;
}
Query::Query(const Query& q) :
@@ -59,7 +58,7 @@
Lockable(q.locked()),
template_defaults(q.template_defaults),
conn_(q.conn_),
-success_(q.success_)
+copacetic_(q.copacetic_)
{
init(&sbuffer_);
}
@@ -72,7 +71,7 @@
set_lock(rhs.locked());
template_defaults = rhs.template_defaults;
conn_ = rhs.conn_;
- success_ = rhs.success_;
+ copacetic_ = rhs.copacetic_;
return *this;
}
@@ -95,13 +94,13 @@
bool
Query::exec(const std::string& str)
{
- success_ = !mysql_real_query(&conn_->mysql_, str.data(),
+ copacetic_ = !mysql_real_query(&conn_->mysql_, str.data(),
static_cast<unsigned long>(str.length()));
- if (!success_ && throw_exceptions()) {
+ if (!copacetic_ && throw_exceptions()) {
throw BadQuery(error());
}
else {
- return success_;
+ return copacetic_;
}
}
@@ -136,7 +135,7 @@
Query::execute(const char* str, size_t len)
{
if (lock()) {
- success_ = false;
+ copacetic_ = false;
if (throw_exceptions()) {
throw LockFailed();
}
@@ -145,10 +144,10 @@
}
}
- success_ = !mysql_real_query(&conn_->mysql_, str, len);
+ copacetic_ = !mysql_real_query(&conn_->mysql_, str, len);
unlock();
- if (success_) {
+ if (copacetic_) {
return ResNSel(conn_);
}
else if (throw_exceptions()) {
@@ -424,7 +423,7 @@
Query::store(const char* str, size_t len)
{
if (lock()) {
- success_ = false;
+ copacetic_ = false;
if (throw_exceptions()) {
throw LockFailed();
}
@@ -434,14 +433,14 @@
}
- if (success_ = !mysql_real_query(&conn_->mysql_, str, len)) {
+ if (copacetic_ = !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;
+ copacetic_ = false;
}
}
unlock();
@@ -528,13 +527,6 @@
}
-bool
-Query::success()
-{
- return success_ && conn_->success();
-}
-
-
void
Query::unlock()
{
@@ -572,7 +564,7 @@
Query::use(const char* str, size_t len)
{
if (lock()) {
- success_ = false;
+ copacetic_ = false;
if (throw_exceptions()) {
throw LockFailed();
}
@@ -581,11 +573,14 @@
}
}
- if (success_ = !mysql_real_query(&conn_->mysql_, str, len)) {
+ if (copacetic_ = !mysql_real_query(&conn_->mysql_, str, len)) {
MYSQL_RES* res = mysql_use_result(&conn_->mysql_);
if (res) {
unlock();
return ResUse(res, conn_, throw_exceptions());
+ }
+ else {
+ copacetic_ = false;
}
}
unlock();
Modified: trunk/lib/query.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/query.h?rev=1684&r1=1683&r2=1684&view=diff
==============================================================================
--- trunk/lib/query.h (original)
+++ trunk/lib/query.h Fri Jul 13 02:24:44 2007
@@ -150,19 +150,35 @@
/// ctor.
Query& operator=(const Query& rhs);
+ /// \brief Test whether the object has experienced an error condition
+ ///
+ /// Allows for code constructs like this:
+ ///
+ /// \code
+ /// Query q = conn.query();
+ /// .... use query object
+ /// if (q) {
+ /// ... no problems in using query object
+ /// }
+ /// else {
+ /// ... an error has occurred
+ /// }
+ /// \endcode
+ ///
+ /// This method returns false if either the Query object or its
+ /// associated Connection object has seen an error condition since
+ /// the last operation.
+ operator bool() const { return *conn_ && copacetic_; }
+
+ /// \brief Return true if the object has experienced an error
+ bool operator !() { return !copacetic_; }
+
/// \brief Get the last error message that was set.
///
/// This class has an internal error message string, but if it
/// isn't set, we return the last error message that happened
/// on the connection we're bound to instead.
std::string error();
-
- /// \brief Returns true if the last operation succeeded
- ///
- /// Returns true if the last query succeeded, and the associated
- /// Connection object's success() method also returns true. If
- /// either object is unhappy, this method returns false.
- bool success();
/// \brief Treat the contents of the query string as a template
/// query.
@@ -779,12 +795,6 @@
return *this;
}
- /// \brief Return true if the last query was successful
- operator bool() { return success(); }
-
- /// \brief Return true if the last query failed
- bool operator !() { return !success(); }
-
#if !defined(DOXYGEN_IGNORE)
// Declare the remaining overloads. These are hidden down here partly
// to keep the above code clear, but also so that we may hide them
@@ -812,7 +822,7 @@
Connection* conn_;
/// \brief If true, last query succeeded
- bool success_;
+ bool copacetic_;
/// \brief List of template query parameters
std::vector<SQLParseElement> parse_elems_;
Modified: trunk/lib/result.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/result.cpp?rev=1684&r1=1683&r2=1684&view=diff
==============================================================================
--- trunk/lib/result.cpp (original)
+++ trunk/lib/result.cpp Fri Jul 13 02:24:44 2007
@@ -30,11 +30,11 @@
namespace mysqlpp {
-ResNSel::ResNSel(Connection* q) :
-success(q->success()),
-insert_id(q->insert_id()),
-rows(q->affected_rows()),
-info(q->info())
+ResNSel::ResNSel(Connection* c) :
+copacetic_(c->copacetic_),
+insert_id_(c->insert_id()),
+rows_(c->affected_rows()),
+info_(c->info())
{
}
Modified: trunk/lib/result.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/result.h?rev=1684&r1=1683&r2=1684&view=diff
==============================================================================
--- trunk/lib/result.h (original)
+++ trunk/lib/result.h Fri Jul 13 02:24:44 2007
@@ -444,26 +444,39 @@
y = tmp;
}
-/// \brief Holds the information on the success of queries that
-/// don't return any results.
+/// \brief Holds information on queries that don't return data.
class MYSQLPP_EXPORT ResNSel
{
public:
- bool success; ///< if true, query was successful
- my_ulonglong insert_id; ///< last value used for AUTO_INCREMENT field
- my_ulonglong rows; ///< number of rows affected
- std::string info; ///< additional info about query result
-
+ /// \brief Default ctor
ResNSel() :
- success(false)
+ copacetic_(false),
+ insert_id_(0),
+ rows_(0)
{
}
/// \brief Initialize object
- ResNSel(Connection* q);
-
- /// \brief Returns true if the query was successful
- operator bool() { return success; }
+ ResNSel(Connection* c);
+
+ /// \brief Test whether the query was successful
+ operator bool() const { return copacetic_; }
+
+ /// \brief Get the last value used for an AUTO_INCREMENT field
+ my_ulonglong insert_id() const { return insert_id_; }
+
+ /// \brief Get the number of rows affected by the query
+ my_ulonglong rows() const { return rows_; }
+
+ /// \brief Get any additional information about the query returned
+ /// by the server.
+ const char* info() const { return info_.c_str(); }
+
+private:
+ bool copacetic_;
+ my_ulonglong insert_id_;
+ my_ulonglong rows_;
+ std::string info_;
};
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits