drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
function/cast/boolean.cc
1/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Brian Aker
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <config.h>
22
23#include <drizzled/error.h>
24#include <drizzled/function/cast/boolean.h>
25#include <drizzled/lex_string.h>
26#include <drizzled/type/boolean.h>
27
28namespace drizzled {
29namespace function {
30namespace cast {
31
33{
34 str->append(STRING_WITH_LEN("cast("));
35 args[0]->print(str);
36 str->append(STRING_WITH_LEN(" as boolean)"));
37}
38
40{
41 switch (args[0]->result_type())
42 {
43 case STRING_RESULT:
44 {
45 drizzled::String _res, *res;
46
47 if (not (res= args[0]->val_str(&_res)))
48 {
49 null_value= true;
50 break;
51 }
52 null_value= false;
53
54 bool result;
55 if (not type::convert(result, *res))
56 {
57 my_error(ER_INVALID_CAST_TO_BOOLEAN, MYF(0), res->c_ptr());
58 }
59
60 return evaluate(result, value);
61 }
62
63 case REAL_RESULT:
64 case ROW_RESULT:
65 case DECIMAL_RESULT:
66 case INT_RESULT:
67 {
68 bool tmp= args[0]->val_bool();
69 null_value=args[0]->null_value;
70
71 return evaluate(tmp, value);
72 }
73 }
74
75 // We should never reach this
76 return evaluate(false, value);
77}
78
79String *Boolean::evaluate(const bool &result, String *val_buffer)
80{
81 val_buffer->alloc(5 * my_charset_bin.mbmaxlen);
82 char *buffer= val_buffer->c_ptr();
83
84 if (result)
85 {
86 memcpy(buffer, "TRUE", 4);
87 val_buffer->length(4);
88 }
89 else
90 {
91 memcpy(buffer, "FALSE", 5);
92 val_buffer->length(5);
93 }
94
95 return val_buffer;
96}
97
98} // namespace cast
99} // namespace function
100} // namespace drizzled
bool null_value
Definition item.h:124
drizzled::String * val_str(drizzled::String *value)
TODO: Rename this file - func.h is stupid.