~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/bool.cc

  • Committer: Brian Aker
  • Date: 2010-12-24 05:50:22 UTC
  • mto: (2035.1.1 clean)
  • mto: This revision was merged to the branch mainline in revision 2030.
  • Revision ID: brian@tangent.org-20101224055022-kcdt5djgx3o2z4sx
Merge in BOOL type. 

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
 
 
22
#include "config.h"
 
23
 
 
24
#include <algorithm>
 
25
 
 
26
#include "drizzled/field/bool.h"
 
27
 
 
28
#include "drizzled/error.h"
 
29
#include "drizzled/internal/my_sys.h"
 
30
#include "drizzled/session.h"
 
31
#include "drizzled/table.h"
 
32
#include "drizzled/temporal.h"
 
33
 
 
34
union set_true_t {
 
35
  unsigned char byte;
 
36
  bool is_true:1;
 
37
 
 
38
  set_true_t()
 
39
  {
 
40
    is_true= true;
 
41
  }
 
42
} set_true;
 
43
 
 
44
namespace drizzled
 
45
{
 
46
namespace field
 
47
{
 
48
 
 
49
Bool::Bool(unsigned char *ptr_arg,
 
50
           uint32_t len_arg,
 
51
           unsigned char *null_ptr_arg,
 
52
           unsigned char null_bit_arg,
 
53
           const char *field_name_arg,
 
54
           bool ansi_display_arg) :
 
55
  Field(ptr_arg, len_arg,
 
56
        null_ptr_arg,
 
57
        null_bit_arg,
 
58
        Field::NONE,
 
59
        field_name_arg),
 
60
  ansi_display(ansi_display_arg)
 
61
{
 
62
}
 
63
 
 
64
int Bool::cmp(const unsigned char *a, const unsigned char *b)
 
65
 
66
  return memcmp(a, b, sizeof(unsigned char));
 
67
}
 
68
 
 
69
int Bool::store(const char *from, uint32_t length, const CHARSET_INFO * const )
 
70
{
 
71
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
72
 
 
73
  if (length == 1)
 
74
  {
 
75
    switch (from[0])
 
76
    {
 
77
    case 'y': case 'Y':
 
78
    case 't': case 'T': // PG compatibility
 
79
      setTrue();
 
80
      return 0;
 
81
 
 
82
    case 'n': case 'N':
 
83
    case 'f': case 'F': // PG compatibility
 
84
      setFalse();
 
85
      return 0;
 
86
 
 
87
    default:
 
88
      my_error(ER_INVALID_BOOL_VALUE, MYF(0), from);
 
89
      return 1; // invalid
 
90
    }
 
91
  }
 
92
  else if ((length == 5) and (strcasecmp(from, "FALSE") == 0))
 
93
  {
 
94
    setFalse();
 
95
  }
 
96
  if ((length == 4) and (strcasecmp(from, "TRUE") == 0))
 
97
  {
 
98
    setTrue();
 
99
  }
 
100
  else if ((length == 5) and (strcasecmp(from, "FALSE") == 0))
 
101
  {
 
102
    setFalse();
 
103
  }
 
104
  else if ((length == 3) and (strcasecmp(from, "YES") == 0))
 
105
  {
 
106
    setTrue();
 
107
  }
 
108
  else if ((length == 2) and (strcasecmp(from, "NO") == 0))
 
109
  {
 
110
    setFalse();
 
111
  }
 
112
  else
 
113
  {
 
114
    my_error(ER_INVALID_BOOL_VALUE, MYF(0), from);
 
115
    return 1; // invalid
 
116
  }
 
117
 
 
118
  return 0;
 
119
}
 
120
 
 
121
int Bool::store(int64_t nr, bool )
 
122
{
 
123
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
124
  nr == 0 ? setFalse() : setTrue();
 
125
  return 0;
 
126
}
 
127
 
 
128
int  Bool::store(double nr)
 
129
{
 
130
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
131
  nr == 0 ? setFalse() : setTrue();
 
132
  return 0;
 
133
}
 
134
 
 
135
int Bool::store_decimal(const drizzled::my_decimal*)
 
136
{
 
137
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
138
  my_error(ER_INVALID_BOOL_VALUE, MYF(ME_FATALERROR), " ");
 
139
  return 1;
 
140
}
 
141
 
 
142
void Bool::sql_type(String &res) const
 
143
{
 
144
  res.set_ascii(STRING_WITH_LEN("bool"));
 
145
}
 
146
 
 
147
double Bool::val_real()
 
148
{
 
149
  ASSERT_COLUMN_MARKED_FOR_READ;
 
150
  return isTrue();
 
151
}
 
152
 
 
153
int64_t Bool::val_int()
 
154
{
 
155
  ASSERT_COLUMN_MARKED_FOR_READ;
 
156
  return isTrue();
 
157
}
 
158
 
 
159
String *Bool::val_str(String *val_buffer, String *)
 
160
{
 
161
  ASSERT_COLUMN_MARKED_FOR_READ;
 
162
  const CHARSET_INFO * const cs= &my_charset_bin;
 
163
  uint32_t mlength= (5) * cs->mbmaxlen;
 
164
 
 
165
  val_buffer->alloc(mlength);
 
166
  char *buffer=(char*) val_buffer->ptr();
 
167
 
 
168
  if (isTrue())
 
169
  {
 
170
    if (ansi_display)
 
171
    {
 
172
      memcpy(buffer, "YES", 3);
 
173
      val_buffer->length(3);
 
174
    }
 
175
    else
 
176
    {
 
177
      memcpy(buffer, "TRUE", 4);
 
178
      val_buffer->length(4);
 
179
    }
 
180
  }
 
181
  else
 
182
  {
 
183
    if (ansi_display)
 
184
    {
 
185
      memcpy(buffer, "NO", 2);
 
186
      val_buffer->length(2);
 
187
    }
 
188
    else
 
189
    {
 
190
      memcpy(buffer, "FALSE", 5);
 
191
      val_buffer->length(5);
 
192
    }
 
193
  }
 
194
 
 
195
  return val_buffer;
 
196
}
 
197
 
 
198
void Bool::sort_string(unsigned char *to, uint32_t length_arg)
 
199
{
 
200
  memcpy(to, ptr, length_arg);
 
201
}
 
202
 
 
203
void Bool::setTrue()
 
204
{
 
205
  ptr[0]= set_true.byte;
 
206
}
 
207
 
 
208
} /* namespace field */
 
209
} /* namespace drizzled */