Author: wyoung
Date: Thu Nov 15 21:21:54 2007
New Revision: 1861
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1861&view=rev
Log:
- Moved operator<<(ostream&, String&) from manip module to mystring.
It has nothing to do with the quoting and escaping mechanism.
- Removed all String stuff from manip module, since the only good reason
to use them in building queries is the rare case of turning result set
data back around as part of a new query.
- Added conversion ctor and assignment operator to STA for String to
to cope with that rare case: an efficient refcounted shared buffer
conversion lets you use Strings to build queries.
Modified:
trunk/Wishlist
trunk/lib/manip.cpp
trunk/lib/manip.h
trunk/lib/mystring.cpp
trunk/lib/mystring.h
trunk/lib/stadapter.cpp
trunk/lib/stadapter.h
Modified: trunk/Wishlist
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=1861&r1=1860&r2=1861&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Thu Nov 15 21:21:54 2007
@@ -16,10 +16,6 @@
o The manipulator changes sometime during 2.3 broke the column
formatting of the examples that dump tables. A setw(x) call
results in x spaces, not x - strlen(whatevercamebefore).
-
- o Add conversion ctor to SQLTypeAdapter for String that can do
- buffer sharing, then remove String specializations for
- manipulators.
o Add String::compare(const char*, int), and anything else that
looks useful from the std::string interface.
Modified: trunk/lib/manip.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/manip.cpp?rev=1861&r1=1860&r2=1861&view=diff
==============================================================================
--- trunk/lib/manip.cpp (original)
+++ trunk/lib/manip.cpp Thu Nov 15 21:21:54 2007
@@ -33,13 +33,6 @@
// Manipulator stuff is _always_ in namespace mysqlpp.
namespace mysqlpp {
-
-/// \brief Set to true if you want to suppress automatic quoting
-///
-/// Works only for String inserted into C++ streams.
-
-bool dont_quote_auto = false;
-
/// \brief Inserts a SQLTypeAdapter into a stream, quoted and escaped
/// as appropriate to the data type the object was initialized from.
@@ -86,34 +79,6 @@
}
-/// \brief Inserts a String into a stream, quoted and escaped as needed
-///
-/// Because String was designed to contain MySQL type data, we may
-/// choose not to actually quote or escape the data, if it is not
-/// needed.
-
-ostream& operator <<(quote_type1 o, const String& in)
-{
- if (in.escape_q()) {
- char* escaped = new char[in.length() * 2 + 1];
- size_t len = mysql_escape_string(escaped, in.data(),
in.length());
-
- if (in.quote_q()) o.ostr->write("'", 1);
- o.ostr->write(escaped, len);
- if (in.quote_q()) o.ostr->write("'", 1);
-
- delete[] escaped;
- }
- else {
- if (in.quote_q()) o.ostr->write("'", 1);
- o.ostr->write(in.data(), in.length());
- if (in.quote_q()) o.ostr->write("'", 1);
- }
-
- return *o.ostr;
-}
-
-
/// \brief Inserts a SQLTypeAdapter into a non-Query stream.
///
/// Although we know how to quote and escape SQLTypeAdapter objects, we
@@ -125,57 +90,6 @@
operator <<(ostream& o, const SQLTypeAdapter& in)
{
o.write(in.data(), in.length());
- return o;
-}
-
-
-/// \brief Inserts a String into a non-Query stream.
-///
-/// Although we know how to automatically quote and escape String
-/// objects, we only do that when inserting them into Query streams
-/// because this feature is only intended to make it easier to build
-/// syntactically-correct SQL queries. You can force the library to
-/// give you quoting and escaping with the quote manipulator:
-///
-/// \code
-/// mysqlpp::String cd("...");
-/// cout << mysqlpp::quote << cd << endl;
-/// \endcode
-
-ostream& operator <<(ostream& o, const String& in)
-{
- o.write(in.data(), in.length());
- return o;
-}
-
-
-/// \brief Insert a String into a Query stream
-///
-/// This operator appears to be a workaround for a weakness in one
-/// compiler's implementation of the C++ type system. See Wishlist for
-/// current plan on what to do about this.
-
-Query& operator <<(Query& o, const String& in)
-{
- if (dont_quote_auto) {
- o.write(in.data(), in.length());
- }
- else if (in.escape_q()) {
- char* escaped = new char[in.length() * 2 + 1];
- size_t len = mysql_escape_string(escaped, in.data(),
in.length());
-
- if (in.quote_q()) o.write("'", 1);
- o.write(escaped, len);
- if (in.quote_q()) o.write("'", 1);
-
- delete[] escaped;
- }
- else {
- if (in.quote_q()) o.write("'", 1);
- o.write(in.data(), in.length());
- if (in.quote_q()) o.write("'", 1);
- }
-
return o;
}
@@ -202,21 +116,6 @@
}
-/// \brief Inserts a String into a stream, quoted as needed
-///
-/// Because String was designed to contain MySQL type data, we may
-/// choose not to actually quote the data, if it is not needed.
-
-ostream& operator <<(quote_only_type1 o, const String& in)
-{
- if (in.quote_q()) o.ostr->write("'", 1);
- o.ostr->write(in.data(), in.length());
- if (in.quote_q()) o.ostr->write("'", 1);
-
- return *o.ostr;
-}
-
-
/// \brief Inserts a SQLTypeAdapter into a stream, double-quoting it (")
/// unless it's data that needs no quoting.
///
@@ -236,21 +135,6 @@
in.set_processed();
return *p.qparms << in;
}
-}
-
-
-/// \brief Inserts a String into a stream, double-quoted (") as needed
-///
-/// Because String was designed to contain MySQL type data, we may
-/// choose not to actually quote the data, if it is not needed.
-
-ostream& operator <<(quote_double_only_type1 o, const String& in)
-{
- if (in.quote_q()) o.ostr->write("\"", 1);
- o.ostr->write(in.data(), in.length());
- if (in.quote_q()) o.ostr->write("\"", 1);
-
- return *o.ostr;
}
@@ -291,27 +175,6 @@
}
-/// \brief Inserts a String into a stream, escaping special SQL characters
-///
-/// Because String was designed to contain MySQL type data, we may
-/// choose not to escape the data, if it is not needed.
-
-std::ostream& operator <<(escape_type1 o, const String& in)
-{
- if (in.escape_q()) {
- char* escaped = new char[in.length() * 2 + 1];
- size_t len = mysql_escape_string(escaped, in.data(),
in.length());
- o.ostr->write(escaped, len);
- delete[] escaped;
- }
- else {
- o.ostr->write(in.data(), in.length());
- }
-
- return *o.ostr;
-}
-
-
/// \brief Inserts a SQLTypeAdapter into a stream, with no escaping or
/// quoting.
Modified: trunk/lib/manip.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/manip.h?rev=1861&r1=1860&r2=1861&view=diff
==============================================================================
--- trunk/lib/manip.h (original)
+++ trunk/lib/manip.h Thu Nov 15 21:21:54 2007
@@ -54,10 +54,7 @@
/// so there is a serious danger of conflicts.
namespace mysqlpp {
-class Query;
class SQLQueryParms;
-
-extern bool dont_quote_auto;
/// \enum quote_type0
@@ -121,20 +118,8 @@
const SQLTypeAdapter& in);
-MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& o,
- const String& in);
-
-
-MYSQLPP_EXPORT Query& operator <<(Query& o,
- const String& in);
-
-
MYSQLPP_EXPORT std::ostream& operator <<(quote_type1 o,
const SQLTypeAdapter& in);
-
-
-MYSQLPP_EXPORT std::ostream& operator <<(quote_type1 o,
- const String& in);
template <class ST>
@@ -214,10 +199,6 @@
}
-MYSQLPP_EXPORT std::ostream& operator <<(quote_only_type1 o,
- const String& in);
-
-
template <class ST>
inline std::ostream&
operator <<(quote_only_type1 o, const Set<ST>& in)
@@ -295,10 +276,6 @@
}
-MYSQLPP_EXPORT std::ostream&
-operator <<(quote_double_only_type1 o, const String& in);
-
-
template <class ST>
inline std::ostream&
operator <<(quote_double_only_type1 o, const Set<ST>& in)
@@ -371,10 +348,6 @@
MYSQLPP_EXPORT std::ostream& operator <<(escape_type1 o,
const SQLTypeAdapter& in);
-
-
-MYSQLPP_EXPORT std::ostream& operator <<(escape_type1 o,
- const String& in);
/// \enum do_nothing_type0
Modified: trunk/lib/mystring.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/mystring.cpp?rev=1861&r1=1860&r2=1861&view=diff
==============================================================================
--- trunk/lib/mystring.cpp (original)
+++ trunk/lib/mystring.cpp Thu Nov 15 21:21:54 2007
@@ -194,5 +194,25 @@
return buffer_ ? buffer_->data()[pos] : 0;
}
+/// \brief Stream insertion operator for String objects
+///
+/// This doesn't have anything to do with the automatic quoting and
+/// escaping you get when using SQLTypeAdapter with Query. The need to
+/// use String with Query should be rare, since String generally comes
+/// in result sets; it should only go back out as queries when using
+/// result data in a new query. Since SQLTypeAdapter has a conversion
+/// ctor for String, this shouldn't be a problem. It's just trading
+/// simplicity for a tiny bit of inefficiency in a rare case. And
+/// since String and SQLTypeAdapter can share a buffer, it's not all
+/// that inefficient anyway.
+
+std::ostream&
+operator <<(std::ostream& o, const String& in)
+{
+ o.write(in.data(), in.length());
+ return o;
+}
+
+
} // end namespace mysqlpp
Modified: trunk/lib/mystring.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/mystring.h?rev=1861&r1=1860&r2=1861&view=diff
==============================================================================
--- trunk/lib/mystring.h (original)
+++ trunk/lib/mystring.h Thu Nov 15 21:21:54 2007
@@ -41,6 +41,10 @@
#include <stdlib.h>
namespace mysqlpp {
+
+#if !defined(DOXYGEN_IGNORE)
+class SQLTypeAdapter;
+#endif
/// \brief A std::string work-alike that can convert itself from SQL
/// text data formats to C++ data types.
@@ -471,7 +475,13 @@
}
RefCountedBuffer* buffer_; ///< pointer to data buffer manager
object
+
+ friend class SQLTypeAdapter;
};
+
+MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& o,
+ const String& in);
+
#if !defined(DOXYGEN_IGNORE)
Modified: trunk/lib/stadapter.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/stadapter.cpp?rev=1861&r1=1860&r2=1861&view=diff
==============================================================================
--- trunk/lib/stadapter.cpp (original)
+++ trunk/lib/stadapter.cpp Thu Nov 15 21:21:54 2007
@@ -26,6 +26,7 @@
#include "stadapter.h"
+#include "mystring.h"
#include "refcounted.h"
#include "stream2string.h"
@@ -44,6 +45,13 @@
SQLTypeAdapter::SQLTypeAdapter(const SQLTypeAdapter& other) :
buffer_(other.buffer_),
is_processed_(false)
+{
+ buffer_->attach();
+}
+
+SQLTypeAdapter::SQLTypeAdapter(const String& other, bool processed) :
+buffer_(other.buffer_),
+is_processed_(processed)
{
buffer_->attach();
}
@@ -411,6 +419,16 @@
}
SQLTypeAdapter&
+SQLTypeAdapter::operator =(const String& rhs)
+{
+ dec_ref_count();
+ buffer_ = rhs.buffer_;
+ buffer_->attach();
+ is_processed_ = false;
+ return *this;
+}
+
+SQLTypeAdapter&
SQLTypeAdapter::operator =(const char* str)
{
dec_ref_count();
Modified: trunk/lib/stadapter.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/stadapter.h?rev=1861&r1=1860&r2=1861&view=diff
==============================================================================
--- trunk/lib/stadapter.h (original)
+++ trunk/lib/stadapter.h Thu Nov 15 21:21:54 2007
@@ -41,6 +41,7 @@
#if !defined(DOXYGEN_IGNORE)
class RefCountedBuffer;
+class String;
#endif
/// \brief Converts many different data types to strings suitable for
@@ -86,6 +87,14 @@
/// deep copy, use one of the ctors that takes a string.
SQLTypeAdapter(const SQLTypeAdapter& other);
+ /// \brief Create a copy of a MySQL++ string
+ ///
+ /// This does reference-counted buffer sharing with the other
+ /// object. If you need a deep copy, pass the result of
+ /// either String::c_str() or String::conv() instead, which will
+ /// call one of the other string ctors.
+ SQLTypeAdapter(const String& str, bool processed = false);
+
/// \brief Create a copy of a C++ string
SQLTypeAdapter(const std::string& str, bool processed = false);
@@ -155,6 +164,13 @@
/// itself to the other object's buffer, with reference counting
/// on each side. If you need a deep copy, assign a string instead.
SQLTypeAdapter& operator =(const SQLTypeAdapter& rhs);
+
+ /// \brief Share a buffer with a String object
+ ///
+ /// Detaches this object from its internal buffer and attaches
+ /// itself to the other object's buffer, with reference counting
+ /// on each side. If you need a deep copy, assign a string instead.
+ SQLTypeAdapter& operator =(const String& rhs);
/// \brief Copy a C string into this object
SQLTypeAdapter& operator =(const char* str);
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits