1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Brian Aker
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.
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.
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
23
#include "drizzled/function/cast/boolean.h"
24
#include "drizzled/error.h"
30
void Boolean::print(String *str, enum_query_type query_type)
32
str->append(STRING_WITH_LEN("cast("));
33
args[0]->print(str, query_type);
34
str->append(STRING_WITH_LEN(" as boolean)"));
37
drizzled::String *Boolean::val_str(drizzled::String *value)
39
switch (args[0]->result_type())
43
drizzled::String _res, *res;
45
if (not (res= args[0]->val_str(&_res)))
53
if (res->length() == 1)
55
switch (res->ptr()[0])
58
case 't': case 'T': // PG compatibility
59
return evaluate(true, value);
62
case 'f': case 'F': // PG compatibility
63
return evaluate(false, value);
69
else if ((res->length() == 5) and (strcasecmp(res->ptr(), "FALSE") == 0))
71
return evaluate(false, value);
73
if ((res->length() == 4) and (strcasecmp(res->ptr(), "TRUE") == 0))
75
return evaluate(true, value);
77
else if ((res->length() == 5) and (strcasecmp(res->ptr(), "FALSE") == 0))
79
return evaluate(false, value);
81
else if ((res->length() == 3) and (strcasecmp(res->ptr(), "YES") == 0))
83
return evaluate(true, value);
85
else if ((res->length() == 2) and (strcasecmp(res->ptr(), "NO") == 0))
87
return evaluate(false, value);
90
my_error(ER_INVALID_CAST_TO_BOOLEAN, MYF(0), res->ptr());
91
return evaluate(false, value);
99
bool tmp= args[0]->val_bool();
100
null_value=args[0]->null_value;
102
return evaluate(tmp, value);
106
// We should never reach this
107
return evaluate(false, value);
110
String *Boolean::evaluate(const bool &result, String *val_buffer)
112
const CHARSET_INFO * const cs= &my_charset_bin;
113
uint32_t mlength= (5) * cs->mbmaxlen;
115
val_buffer->alloc(mlength);
116
char *buffer=(char*) val_buffer->ptr();
120
memcpy(buffer, "TRUE", 4);
121
val_buffer->length(4);
125
memcpy(buffer, "FALSE", 5);
126
val_buffer->length(5);
133
bool Boolean::val_bool()
137
switch (args[0]->result_type())
141
drizzled::String _res, *res;
143
if (not (res= args[0]->val_str(&_res)))
151
if (res->length() == 1)
153
switch (res->ptr()[0])
156
case 't': case 'T': // PG compatibility
160
case 'f': case 'F': // PG compatibility
167
else if ((res->length() == 5) and (strcasecmp(res->ptr(), "FALSE") == 0))
171
if ((res->length() == 4) and (strcasecmp(res->ptr(), "TRUE") == 0))
175
else if ((res->length() == 5) and (strcasecmp(res->ptr(), "FALSE") == 0))
179
else if ((res->length() == 3) and (strcasecmp(res->ptr(), "YES") == 0))
183
else if ((res->length() == 2) and (strcasecmp(res->ptr(), "NO") == 0))
192
tmp= args[0]->val_bool();
193
null_value=args[0]->null_value;
202
} // namespace function
203
} // namespace drizzled