MySQL++ Reference Manual

MySQL++: null.h Source File

MySQL++ 3.3.0

null.h
Go to the documentation of this file.
1
8 /***********************************************************************
9 Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
10 (c) 2004-2008 by Educational Technology Resources, Inc. Others may
11 also hold copyrights on code in this file. See the CREDITS.txt file
12 in the top directory of the distribution for details.
13
14 This file is part of MySQL++.
15
16 MySQL++ is free software; you can redistribute it and/or modify it
17 under the terms of the GNU Lesser General Public License as published
18 by the Free Software Foundation; either version 2.1 of the License, or
19 (at your option) any later version.
20
21 MySQL++ is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public
27 License along with MySQL++; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
29 USA
30 ***********************************************************************/
31
32 #ifndef MYSQLPP_NULL_H
33 #define MYSQLPP_NULL_H
34
35 #include "exceptions.h"
36
37 #include <iostream>
38 #include <string>
39
40 namespace mysqlpp {
41
42 extern const std::string null_str;
43
44
49 class MYSQLPP_EXPORT null_type
50 {
51 protected:
52 #if !defined(DOXYGEN_IGNORE)
53 // Doxygen will not generate documentation for this section.
54 template <typename CannotConvertNullToAnyOtherDataType>
55 operator CannotConvertNullToAnyOtherDataType() const
56 {
57 return CannotConvertNullToAnyOtherDataType();
58 }
59 #endif // !defined(DOXYGEN_IGNORE)
60 };
61
84 const null_type null = null_type();
85
86
94 struct NullIsNull
95 {
96 #if !defined(DOXYGEN_IGNORE)
97 // Doxygen will not generate documentation for this section.
98 static null_type null_is() { return null; }
99
100 static std::ostream& null_ostr(std::ostream& o)
101 {
102 o << "(NULL)";
103 return o;
104 }
105 #endif // !defined(DOXYGEN_IGNORE)
106 };
107
108
115 struct NullIsZero
116 {
117 #if !defined(DOXYGEN_IGNORE)
118 // Doxygen will not generate documentation for this section.
119 static int null_is() { return 0; }
120
121 static std::ostream& null_ostr(std::ostream& o)
122 {
123 o << 0;
124 return o;
125 }
126 #endif // !defined(DOXYGEN_IGNORE)
127 };
128
135 struct NullIsBlank
136 {
137 #if !defined(DOXYGEN_IGNORE)
138 // Doxygen will not generate documentation for this section.
139 static const char *null_is() { return ""; }
140
141 static std::ostream& null_ostr(std::ostream& o)
142 {
143 o << "";
144 return o;
145 }
146 #endif // !defined(DOXYGEN_IGNORE)
147 };
148
149
169 template <class Type, class Behavior = NullIsNull>
170 class Null
171 {
172 public:
174 Type data;
175
179 bool is_null;
180
183 typedef Type value_type;
184
189 Null() :
190 data(),
191 is_null(false)
192 {
193 }
194
202 Null(const Type& x) :
203 data(x),
204 is_null(false)
205 {
206 }
207
216 Null(const null_type&) :
217 data(),
218 is_null(true)
219 {
220 }
221
229 operator Type() const
230 {
231 if (is_null) {
232 return Behavior::null_is();
233 }
234 else {
235 return data;
236 }
237 }
238
242 Null& operator =(const Type& x)
243 {
244 data = x;
245 is_null = false;
246 return *this;
247 }
248
253 Null& operator =(const null_type&)
254 {
255 is_null = true;
256 return *this;
257 }
258
264 bool operator ==(const Null<Type>& rhs) const
265 {
266 if (is_null && rhs.is_null) {
267 return true;
268 }
269 else if (is_null != rhs.is_null) {
270 return false; // one null, the other not
271 }
272 else {
273 return data == rhs.data;
274 }
275 }
276
280 bool operator ==(const null_type&) const { return is_null; }
281
283 bool operator !=(const Null<Type>& rhs) const
284 { return !(*this == rhs); }
285
287 bool operator !=(const null_type& rhs) const
288 { return !(*this == rhs); }
289
295 bool operator <(const Null<Type>& rhs) const
296 {
297 if (is_null) {
298 return !rhs.is_null; // less than only if RHS not null
299 }
300 else if (rhs.is_null) {
301 return false; // non-null always greater than null
302 }
303 else {
304 return data < rhs.data; // neither is null, so compare data
305 }
306 }
307
312 bool operator <(const null_type&) const { return false; }
313 };
314
315
316 #if !defined(DOXYGEN_IGNORE)
317 // Doxygen will not generate documentation for this section.
318
319 // Specialization the Null template for \c void
320 template <> class Null<void>
321 {
322 public:
323 bool is_null;
324 typedef void value_type;
325
326 Null() :
327 is_null(false)
328 {
329 }
330
331 Null(const null_type&) :
332 is_null(true)
333 {
334 }
335
336 Null& operator =(const null_type&)
337 {
338 is_null = true;
339 return *this;
340 }
341 };
342
343 #endif // !defined(DOXYGEN_IGNORE)
344
345
349 template <class Type, class Behavior>
350 inline std::ostream& operator <<(std::ostream& o,
351 const Null<Type, Behavior>& n)
352 {
353 if (n.is_null)
354 return Behavior::null_ostr(o);
355 else
356 return o << n.data;
357 }
358
359 } // end namespace mysqlpp
360
361 #endif
mysqlpp::Null
Class for holding data from a SQL column with the NULL attribute.
Definition: null.h:171
mysqlpp::Null::data
Type data
The object's value, when it is not SQL null.
Definition: null.h:174
mysqlpp::Null::operator=
Null & operator=(const Type &x)
Assign a value to the object.
Definition: null.h:242
mysqlpp::Null::is_null
bool is_null
If set, this object is considered equal to SQL null.
Definition: null.h:179
mysqlpp::Null::operator!=
bool operator!=(const Null< Type > &rhs) const
Do inequality comparison of two nullable values.
Definition: null.h:283
mysqlpp::Null::Null
Null(const Type &x)
Initialize the object with a particular value.
Definition: null.h:202
mysqlpp::Null::Null
Null()
Default constructor.
Definition: null.h:189
mysqlpp::Null::Null
Null(const null_type &)
Construct a Null equal to SQL null.
Definition: null.h:216
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 equality comparison of two nullable values.
Definition: null.h:264
mysqlpp::Null::operator<
bool operator<(const Null< Type > &rhs) const
Do less-than comparison of two nullable values.
Definition: null.h:295
mysqlpp::null_type
The type of the global mysqlpp::null object.
Definition: null.h:50
exceptions.h
Declares the MySQL++-specific exception classes.
mysqlpp::null_str
const std::string null_str
"NULL" string constant
mysqlpp::NullIsBlank
Class for objects that define SQL null as a blank C string.
Definition: null.h:136
mysqlpp::NullIsNull
Class for objects that define SQL null in terms of MySQL++'s null_type.
Definition: null.h:95
mysqlpp::NullIsZero
Class for objects that define SQL null as 0.
Definition: null.h:116