Author: wyoung
Date: Tue Feb 19 20:31:02 2008
New Revision: 2208
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2208&view=rev
Log:
- Added several comparison operators to Null<T>
- Added sql_cmp() overload for Null<T>, needed to allow Null<T> in key
position in SSQLS
- Added new unit test to check these
Added:
trunk/test/null_comparison.cpp
Modified:
trunk/lib/null.h
trunk/lib/ssqls.pl
trunk/mysql++.bkl
Modified: trunk/lib/null.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/null.h?rev=2208&r1=2207&r2=2208&view=diff
==============================================================================
--- trunk/lib/null.h (original)
+++ trunk/lib/null.h Tue Feb 19 20:31:02 2008
@@ -250,6 +250,61 @@
is_null = true;
return *this;
}
+
+ /// \brief Do equality comparison of two nullable values
+ ///
+ /// Two null objects are equal, and null is not equal to not-null.
+ /// If neither is null, we delegate to operator == for the base
+ /// data type.
+ bool operator ==(const Null<Type>& rhs) const
+ {
+ if (is_null && rhs.is_null) {
+ return true;
+ }
+ else if (is_null != rhs.is_null) {
+ return false; // one null, the other not
+ }
+ else {
+ return data == rhs.data;
+ }
+ }
+
+ /// \brief Do equality comparison against hard-coded SQL null
+ ///
+ /// This tells you the same thing as testing is_null member.
+ bool operator ==(const null_type&) const { return is_null; }
+
+ /// \brief Do inequality comparison of two nullable values
+ bool operator !=(const Null<Type>& rhs) const
+ { return !(*this == rhs); }
+
+ /// \brief Do inequality comparison against hard-coded SQL null
+ bool operator !=(const null_type& rhs) const
+ { return !(*this == rhs); }
+
+ /// \brief Do less-than comparison of two nullable values
+ ///
+ /// Two null objects are equal to each other, and null is less
+ /// than not-null. If neither is null, we delegate to operator <
+ /// for the base data type.
+ bool operator <(const Null<Type>& rhs) const
+ {
+ if (is_null && rhs.is_null) {
+ return false;
+ }
+ else if (is_null && !rhs.is_null) {
+ return true;
+ }
+ else {
+ return data < rhs.data;
+ }
+ }
+
+ /// \brief Do less-than comparison against hard-coded SQL null
+ ///
+ /// Always returns false because we can only be greater than or
+ /// equal to a SQL null.
+ bool operator <(const null_type&) const { return false; }
};
Modified: trunk/lib/ssqls.pl
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/ssqls.pl?rev=2208&r1=2207&r2=2208&view=diff
==============================================================================
--- trunk/lib/ssqls.pl (original)
+++ trunk/lib/ssqls.pl Tue Feb 19 20:31:02 2008
@@ -147,6 +147,15 @@
print OUT << "---";
+template <typename T>
+inline int sql_cmp(const mysqlpp::Null<T>& a, const mysqlpp::Null<T>& b)
+{
+ if (a == b) return 0;
+ if (a < b) return -1;
+ return 1;
+}
+
+
// ---------------------------------------------------
// Begin Mandatory Compare
// ---------------------------------------------------
Modified: trunk/mysql++.bkl
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/mysql%2B%2B.bkl?rev=2208&r1=2207&r2=2208&view=diff
==============================================================================
--- trunk/mysql++.bkl (original)
+++ trunk/mysql++.bkl Tue Feb 19 20:31:02 2008
@@ -222,6 +222,9 @@
<exe id="test_manip" template="programs">
<sources>test/manip.cpp</sources>
</exe>
+ <exe id="test_null_comparison" template="programs">
+ <sources>test/null_comparison.cpp</sources>
+ </exe>
<exe id="test_qstream" template="programs">
<sources>test/qstream.cpp</sources>
</exe>
Added: trunk/test/null_comparison.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/test/null_comparison.cpp?rev=2208&view=auto
==============================================================================
--- trunk/test/null_comparison.cpp (added)
+++ trunk/test/null_comparison.cpp Tue Feb 19 20:31:02 2008
@@ -1,0 +1,57 @@
+/***********************************************************************
+ test/null_comparison.cpp - Tests that Null<T> and null_type comparison
+ operators and SSQLS comparison functions work correctly.
+
+ Copyright (c) 2008 by Educational Technology Resources, Inc.
+ Others may also hold copyrights on code in this file. See the
+ CREDITS file in the top directory of the distribution for details.
+
+ This file is part of MySQL++.
+
+ MySQL++ is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ MySQL++ is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with MySQL++; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA
+***********************************************************************/
+
+#include <mysql++.h>
+#include <ssqls.h>
+
+#include <iostream>
+
+int
+main()
+{
+ mysqlpp::Null<int> null_int = mysqlpp::null;
+ mysqlpp::Null<int> non_null_int = 42;
+ if ( !(null_int == non_null_int) &&
+ (null_int != non_null_int) &&
+ (null_int < non_null_int) &&
+ !(non_null_int == null_int) &&
+ (non_null_int != null_int) &&
+ !(non_null_int < null_int) &&
+ (null_int == mysqlpp::null) &&
+ !(null_int != mysqlpp::null) &&
+ (non_null_int != mysqlpp::null) &&
+ !(non_null_int == mysqlpp::null) &&
+ (mysqlpp::sql_cmp(null_int, null_int) == 0) &&
+ (mysqlpp::sql_cmp(null_int, non_null_int) < 0) &&
+ (mysqlpp::sql_cmp(non_null_int, null_int) > 0)) {
+ return 0;
+ }
+ else {
+ std::cerr << "Null comparison gave unexpected result" <<
std::endl;
+ return 1;
+ }
+}
+
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits