00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef MYSQLPP_NULL_H
00033 #define MYSQLPP_NULL_H
00034
00035 #include "exceptions.h"
00036
00037 #include <iostream>
00038
00039 namespace mysqlpp {
00040
00041
00046 class null_type
00047 {
00048 public:
00049 #if !defined(DOXYGEN_IGNORE)
00050
00051 template <class Type> operator Type()
00052 {
00053 throw BadNullConversion();
00054 }
00055 #endif // !defined(DOXYGEN_IGNORE)
00056 };
00057
00060 const null_type null = null_type();
00061
00062
00070 struct NullisNull
00071 {
00072 #if !defined(DOXYGEN_IGNORE)
00073
00074 static null_type null_is() { return null_type(); }
00075
00076 static std::ostream& null_ostr(std::ostream& o)
00077 {
00078 o << "(NULL)";
00079 return o;
00080 }
00081 #endif // !defined(DOXYGEN_IGNORE)
00082 };
00083
00084
00091 struct NullisZero
00092 {
00093 #if !defined(DOXYGEN_IGNORE)
00094
00095 static int null_is() { return 0; }
00096
00097 static std::ostream& null_ostr(std::ostream& o)
00098 {
00099 o << 0;
00100 return o;
00101 }
00102 #endif // !defined(DOXYGEN_IGNORE)
00103 };
00104
00111 struct NullisBlank
00112 {
00113 #if !defined(DOXYGEN_IGNORE)
00114
00115 static const char *null_is() { return ""; }
00116
00117 static std::ostream& null_ostr(std::ostream& o)
00118 {
00119 o << "";
00120 return o;
00121 }
00122 #endif // !defined(DOXYGEN_IGNORE)
00123 };
00124
00125
00145 template <class Type, class Behavior = NullisNull> class Null
00146 {
00147 public:
00149 Type data;
00150
00154 bool is_null;
00155
00158 typedef Type value_type;
00159
00163 Null()
00164 {
00165 }
00166
00174 Null(Type x) :
00175 data(x),
00176 is_null(false)
00177 {
00178 }
00179
00188 Null(const null_type& n) :
00189 is_null(true)
00190 {
00191 }
00192
00200 operator Type&()
00201 {
00202 if (is_null)
00203 return data = Behavior::null_is();
00204 else
00205 return data;
00206 }
00207
00211 Null& operator =(const Type& x)
00212 {
00213 data = x;
00214 is_null = false;
00215 return *this;
00216 }
00217
00222 Null& operator =(const null_type& n)
00223 {
00224 is_null = true;
00225 return *this;
00226 }
00227 };
00228
00229
00230 #if !defined(DOXYGEN_IGNORE)
00231
00232
00233
00234 template <> class Null<void>
00235 {
00236 public:
00237 bool is_null;
00238 typedef void value_type;
00239
00240 Null() :
00241 is_null(false)
00242 {
00243 }
00244
00245 Null(const null_type&) :
00246 is_null(true)
00247 {
00248 }
00249
00250 Null& operator =(const null_type&)
00251 {
00252 is_null = true;
00253 return *this;
00254 }
00255 };
00256
00257 #endif // !defined(DOXYGEN_IGNORE)
00258
00259
00263 template <class Type, class Behavior>
00264 inline std::ostream& operator <<(std::ostream& o,
00265 const Null<Type, Behavior>& n)
00266 {
00267 if (n.is_null)
00268 return Behavior::null_ostr(o);
00269 else
00270 return o << n.data;
00271 }
00272
00273 }
00274
00275 #endif