List:MySQL++« Previous MessageNext Message »
From:Michael Hanselmann Date:April 15 2007 10:58pm
Subject:NULL in templatized queries
View as plain text  
Hello

Currently, using NULL in templatized queries isn't possible[1]. Since I need
the ability for NULL in templates (user configurable queries which can't be
hand-rolled), I invested some time. A clean solution isn't possible without
breaking compatibility; so here's what I came up with:

--- sql_string.cpp	(revision 1526)
+++ sql_string.cpp	(working copy)
@@ -166,5 +166,19 @@
 	assign(outs.str());
 }
 
+SQLString::SQLString(const SQLNullStringTag& value) :
+string("NULL"),
+is_string(false),
+dont_escape(false),
+processed(false)
+{
+}
+
+static const SQLNullStringTag null_string_tag = SQLNullStringTag();
+
+SQLNullString::SQLNullString() :
+    SQLString(null_string_tag) {
+}
+
 } // end namespace mysqlpp
 
--- sql_string.h	(revision 1526)
+++ sql_string.h	(working copy)
@@ -41,10 +41,17 @@
 
 namespace mysqlpp {
 
+class SQLNullString;
+
+struct SQLNullStringTag {
+};
+
 /// \brief A specialized \c std::string that will convert from any
 /// valid MySQL type.
 
 class MYSQLPP_EXPORT SQLString : public std::string {
+    friend class SQLNullString;
+
 public:
 	/// \brief If true, the object's string data is a copy of another
 	/// string.  Otherwise, it's the string form of an integral type.
@@ -132,8 +139,16 @@
 		processed = false;
 		return *this;
 	}
+
+private:
+	SQLString(const SQLNullStringTag& value);
 };
 
+class SQLNullString : public SQLString {
+public:
+    SQLNullString();
+};
+
 } // end namespace mysqlpp
 
 #endif

When a parameter should be set to NULL, just pass it an instance of
SQLNullString:
  query.def["item"] = mysqlpp::SQLNullString();

Could this be an acceptable solution for the 2.2.x or 2.x branch?

In case it can't be included, due to whatever reasons, that's fine. For those
interested, the following class resembles the same functionality:

struct MySQLNullString : public mysqlpp::SQLString {
    MySQLNullString() :
        mysqlpp::SQLString("NULL") {
        is_string = false;
    }
};

Greets,
Michael

[1] http://lists.mysql.com/plusplus/6310

-- 
Gentoo Linux developer, http://hansmi.ch/, http://forkbomb.ch/

Attachment: [application/pgp-signature]
Thread
NULL in templatized queriesMichael Hanselmann16 Apr
  • Re: NULL in templatized queriesMichael Hanselmann16 Apr
  • Re: NULL in templatized queriesWarren Young17 Apr
    • Re: NULL in templatized queriesMichael Hanselmann22 Apr
      • Re: NULL in templatized queriesWarren Young23 Apr