2014-04-08 06:51:53 +00:00
|
|
|
#pragma once
|
2011-03-09 19:18:01 +00:00
|
|
|
|
|
|
|
#include <mysqlxx/Exception.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace mysqlxx
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
struct NullType {};
|
|
|
|
const NullType null = {};
|
2011-03-09 19:18:01 +00:00
|
|
|
|
|
|
|
|
2018-06-04 15:28:25 +00:00
|
|
|
/** Store NULL-able types from MySQL.
|
|
|
|
* Usage example:
|
2017-04-01 07:20:54 +00:00
|
|
|
* mysqlxx::Null<int> x = mysqlxx::null;
|
|
|
|
* std::cout << (x.isNull() ? "Ok." : "Fail.") << std::endl;
|
|
|
|
* x = 10;
|
2011-03-18 20:26:54 +00:00
|
|
|
*/
|
2011-03-09 19:18:01 +00:00
|
|
|
template <typename T>
|
|
|
|
class Null
|
|
|
|
{
|
|
|
|
public:
|
2018-06-04 15:28:25 +00:00
|
|
|
T data {};
|
|
|
|
bool is_null = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
Null() : is_null(true) {}
|
2017-10-17 17:01:01 +00:00
|
|
|
Null(const Null<T> &) = default;
|
2017-10-17 11:58:08 +00:00
|
|
|
Null(Null<T> &&) noexcept = default;
|
2020-05-09 22:59:34 +00:00
|
|
|
Null(NullType) : is_null(true) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
explicit Null(const T & data_) : data(data_), is_null(false) {}
|
|
|
|
|
|
|
|
operator T & ()
|
|
|
|
{
|
|
|
|
if (is_null)
|
|
|
|
throw Exception("Value is NULL");
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const T & () const
|
|
|
|
{
|
|
|
|
if (is_null)
|
|
|
|
throw Exception("Value is NULL");
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-10-17 11:58:08 +00:00
|
|
|
Null<T> & operator= (Null<T> &&) noexcept = default;
|
|
|
|
Null<T> & operator= (const Null<T> &) = default;
|
2017-04-01 07:20:54 +00:00
|
|
|
Null<T> & operator= (const T & data_) { is_null = false; data = data_; return *this; }
|
2020-05-09 22:59:34 +00:00
|
|
|
Null<T> & operator= (const NullType) { is_null = true; data = T(); return *this; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
bool isNull() const { return is_null; }
|
|
|
|
|
|
|
|
bool operator< (const Null<T> & other) const
|
|
|
|
{
|
|
|
|
return is_null < other.is_null
|
|
|
|
|| (is_null == other.is_null && data < other.data);
|
|
|
|
}
|
|
|
|
|
2020-05-09 22:59:34 +00:00
|
|
|
bool operator< (const NullType) const { return false; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
bool operator== (const Null<T> & other) const
|
|
|
|
{
|
|
|
|
return is_null == other.is_null && data == other.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator== (const T & other) const
|
|
|
|
{
|
|
|
|
return !is_null && data == other;
|
|
|
|
}
|
|
|
|
|
2020-05-09 22:59:34 +00:00
|
|
|
bool operator== (const NullType) const { return is_null; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
bool operator!= (const Null<T> & other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2020-05-09 22:59:34 +00:00
|
|
|
bool operator!= (const NullType) const { return !is_null; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
bool operator!= (const T & other) const
|
|
|
|
{
|
|
|
|
return is_null || data != other;
|
|
|
|
}
|
2011-03-09 19:18:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-16 12:26:19 +00:00
|
|
|
template<typename T>
|
|
|
|
T getValueFromNull(const Null<T> & maybe)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (maybe.isNull())
|
|
|
|
return {};
|
|
|
|
return maybe;
|
2015-03-16 12:26:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-09 19:18:01 +00:00
|
|
|
}
|