null.h
Go to the documentation of this file.
/***********************************************************************
Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
(c) 2004-2008 by Educational Technology Resources, Inc. Others may
also hold copyrights on code in this file. See the CREDITS.txt 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
***********************************************************************/
#ifndef MYSQLPP_NULL_H
#define MYSQLPP_NULL_H
#include "exceptions.h"
#include <iostream>
#include <string>
namespace mysqlpp {
extern const std::string null_str;
class MYSQLPP_EXPORT null_type
{
protected:
#if !defined(DOXYGEN_IGNORE)
// Doxygen will not generate documentation for this section.
template <typename CannotConvertNullToAnyOtherDataType>
operator CannotConvertNullToAnyOtherDataType() const
{
return CannotConvertNullToAnyOtherDataType();
}
#endif // !defined(DOXYGEN_IGNORE)
};
const null_type null = null_type();
struct NullIsNull
{
#if !defined(DOXYGEN_IGNORE)
// Doxygen will not generate documentation for this section.
static null_type null_is() { return null; }
static std::ostream& null_ostr(std::ostream& o)
{
o << "(NULL)";
return o;
}
#endif // !defined(DOXYGEN_IGNORE)
};
struct NullIsZero
{
#if !defined(DOXYGEN_IGNORE)
// Doxygen will not generate documentation for this section.
static int null_is() { return 0; }
static std::ostream& null_ostr(std::ostream& o)
{
o << 0;
return o;
}
#endif // !defined(DOXYGEN_IGNORE)
};
struct NullIsBlank
{
#if !defined(DOXYGEN_IGNORE)
// Doxygen will not generate documentation for this section.
static const char *null_is() { return ""; }
static std::ostream& null_ostr(std::ostream& o)
{
o << "";
return o;
}
#endif // !defined(DOXYGEN_IGNORE)
};
template <class Type, class Behavior = NullIsNull>
class Null
{
public:
Type data;
bool is_null;
typedef Type value_type;
Null() :
data(),
is_null(false)
{
}
Null(const Type& x) :
data(x),
is_null(false)
{
}
Null(const null_type&) :
data(),
is_null(true)
{
}
operator Type() const
{
if (is_null) {
return Behavior::null_is();
}
else {
return data;
}
}
Null& operator =(const Type& x)
{
data = x;
is_null = false;
return *this;
}
Null& operator =(const null_type&)
{
is_null = true;
return *this;
}
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;
}
}
bool operator ==(const null_type&) const { return is_null; }
bool operator !=(const Null<Type>& rhs) const
{ return !(*this == rhs); }
bool operator !=(const null_type& rhs) const
{ return !(*this == rhs); }
bool operator <(const Null<Type>& rhs) const
{
if (is_null) {
return !rhs.is_null; // less than only if RHS not null
}
else if (rhs.is_null) {
return false; // non-null always greater than null
}
else {
return data < rhs.data; // neither is null, so compare data
}
}
bool operator <(const null_type&) const { return false; }
};
#if !defined(DOXYGEN_IGNORE)
// Doxygen will not generate documentation for this section.
// Specialization the Null template for \c void
template <> class Null<void>
{
public:
bool is_null;
typedef void value_type;
Null() :
is_null(false)
{
}
Null(const null_type&) :
is_null(true)
{
}
Null& operator =(const null_type&)
{
is_null = true;
return *this;
}
};
#endif // !defined(DOXYGEN_IGNORE)
template <class Type, class Behavior>
inline std::ostream& operator <<(std::ostream& o,
const Null<Type, Behavior>& n)
{
if (n.is_null)
return Behavior::null_ostr(o);
else
return o << n.data;
}
} // end namespace mysqlpp
#endif
mysqlpp::Null::operator==
bool operator==(const Null< Type > &rhs) const
Do equality comparison of two nullable values.
Definition: null.h:264
mysqlpp::Null::value_type
Type value_type
Type of the data stored in this object, when it is not equal to SQL null.
Definition: null.h:183
mysqlpp::Null::operator<
bool operator<(const Null< Type > &rhs) const
Do less-than comparison of two nullable values.
Definition: null.h:295
mysqlpp::Null::Null
Null(const null_type &)
Construct a Null equal to SQL null.
Definition: null.h:216
mysqlpp::Null::Null
Null(const Type &x)
Initialize the object with a particular value.
Definition: null.h:202
mysqlpp::NullIsZero
Class for objects that define SQL null as 0.
Definition: null.h:115
mysqlpp::NullIsBlank
Class for objects that define SQL null as a blank C string.
Definition: null.h:135
mysqlpp::null_type
The type of the global mysqlpp::null object.
Definition: null.h:49
exceptions.h
Declares the MySQL++-specific exception classes.
mysqlpp::Null::data
Type data
The object&#39;s value, when it is not SQL null.
Definition: null.h:174
mysqlpp::Null::Null
Null()
Default constructor.
Definition: null.h:189
mysqlpp::Null::operator=
Null & operator=(const Type &x)
Assign a value to the object.
Definition: null.h:242
mysqlpp::Null::operator!=
bool operator!=(const Null< Type > &rhs) const
Do inequality comparison of two nullable values.
Definition: null.h:283
mysqlpp::Null::is_null
bool is_null
If set, this object is considered equal to SQL null.
Definition: null.h:179
mysqlpp::Null
Class for holding data from a SQL column with the NULL attribute.
Definition: null.h:170
mysqlpp::NullIsNull
Class for objects that define SQL null in terms of MySQL++&#39;s null_type.
Definition: null.h:94