~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/cast/boolean.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
28
namespace drizzled {
 
29
namespace function {
 
30
namespace cast {
 
31
 
 
32
void Boolean::print(String *str, enum_query_type query_type)
 
33
{
 
34
  str->append(STRING_WITH_LEN("cast("));
 
35
  args[0]->print(str, query_type);
 
36
  str->append(STRING_WITH_LEN(" as boolean)"));
 
37
}
 
38
 
 
39
drizzled::String *Boolean::val_str(drizzled::String *value)
 
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
 
 
79
String *Boolean::evaluate(const bool &result, String *val_buffer)
 
80
{
 
81
  const CHARSET_INFO * const cs= &my_charset_bin;
 
82
  uint32_t mlength= (5) * cs->mbmaxlen;
 
83
 
 
84
  val_buffer->alloc(mlength);
 
85
  char *buffer=(char*) val_buffer->c_ptr();
 
86
 
 
87
  if (result)
 
88
  {
 
89
    memcpy(buffer, "TRUE", 4);
 
90
    val_buffer->length(4);
 
91
  }
 
92
  else
 
93
  {
 
94
    memcpy(buffer, "FALSE", 5);
 
95
    val_buffer->length(5);
 
96
  }
 
97
 
 
98
  return val_buffer;
 
99
}
 
100
 
 
101
} // namespace cast
 
102
} // namespace function
 
103
} // namespace drizzled