~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/boolean.cc

  • Committer: Brian Aker
  • Date: 2011-02-21 17:37:00 UTC
  • mto: (2192.1.1 drizzle-staging)
  • mto: This revision was merged to the branch mainline in revision 2193.
  • Revision ID: brian@tangent.org-20110221173700-6wdfbxm60zq9q9se
Merge in change from REPLICATION to REPLICATE as keyword.

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/boolean.h>
 
27
#include <drizzled/type/boolean.h>
 
28
 
 
29
#include <drizzled/error.h>
 
30
#include <drizzled/internal/my_sys.h>
 
31
#include <drizzled/session.h>
 
32
#include <drizzled/table.h>
 
33
#include <drizzled/temporal.h>
 
34
 
 
35
union set_true_t {
 
36
  unsigned char byte;
 
37
  bool is_true:1;
 
38
 
 
39
  set_true_t()
 
40
  {
 
41
    is_true= true;
 
42
  }
 
43
} set_true;
 
44
 
 
45
namespace drizzled
 
46
{
 
47
namespace field
 
48
{
 
49
 
 
50
Boolean::Boolean(unsigned char *ptr_arg,
 
51
                 uint32_t len_arg,
 
52
                 unsigned char *null_ptr_arg,
 
53
                 unsigned char null_bit_arg,
 
54
                 const char *field_name_arg,
 
55
                 bool ansi_display_arg) :
 
56
  Field(ptr_arg, len_arg,
 
57
        null_ptr_arg,
 
58
        null_bit_arg,
 
59
        Field::NONE,
 
60
        field_name_arg),
 
61
  ansi_display(ansi_display_arg)
 
62
  {
 
63
  }
 
64
 
 
65
int Boolean::cmp(const unsigned char *a, const unsigned char *b)
 
66
 
67
  return memcmp(a, b, sizeof(unsigned char));
 
68
}
 
69
 
 
70
int Boolean::store(const char *from, uint32_t length, const CHARSET_INFO * const )
 
71
{
 
72
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
73
 
 
74
  bool result;
 
75
  if (not type::convert(result, from, length))
 
76
  {
 
77
    my_error(ER_INVALID_BOOLEAN_VALUE, MYF(0), from);
 
78
    return 1;
 
79
  }
 
80
 
 
81
  if (result)
 
82
  {
 
83
    setTrue();
 
84
  }
 
85
  else
 
86
  {
 
87
    setFalse();
 
88
  }
 
89
 
 
90
  return 0;
 
91
}
 
92
 
 
93
int Boolean::store(int64_t nr, bool )
 
94
{
 
95
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
96
  nr == 0 ? setFalse() : setTrue();
 
97
  return 0;
 
98
}
 
99
 
 
100
int  Boolean::store(double nr)
 
101
{
 
102
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
103
  nr == 0 ? setFalse() : setTrue();
 
104
  return 0;
 
105
}
 
106
 
 
107
int Boolean::store_decimal(const drizzled::type::Decimal *dec)
 
108
{
 
109
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
110
  if (dec->isZero())
 
111
  {
 
112
    setFalse();
 
113
    return 0;
 
114
  }
 
115
 
 
116
  setTrue();
 
117
 
 
118
  return 0;
 
119
}
 
120
 
 
121
void Boolean::sql_type(String &res) const
 
122
{
 
123
  res.set_ascii(STRING_WITH_LEN("boolean"));
 
124
}
 
125
 
 
126
double Boolean::val_real() const
 
127
{
 
128
  ASSERT_COLUMN_MARKED_FOR_READ;
 
129
  return isTrue();
 
130
}
 
131
 
 
132
int64_t Boolean::val_int() const
 
133
{
 
134
  ASSERT_COLUMN_MARKED_FOR_READ;
 
135
  return isTrue();
 
136
}
 
137
 
 
138
String *Boolean::val_str(String *val_buffer, String *) const
 
139
{
 
140
  ASSERT_COLUMN_MARKED_FOR_READ;
 
141
 
 
142
  (void)type::convert(*val_buffer, isTrue(), ansi_display);
 
143
 
 
144
  return val_buffer;
 
145
}
 
146
 
 
147
type::Decimal *Boolean::val_decimal(type::Decimal *dec) const
 
148
{
 
149
  if (isTrue())
 
150
  {
 
151
    int2_class_decimal(E_DEC_OK, 1, false, dec);
 
152
    return dec;
 
153
  }
 
154
 
 
155
  dec->set_zero();
 
156
 
 
157
  return dec;
 
158
}
 
159
 
 
160
void Boolean::sort_string(unsigned char *to, uint32_t length_arg)
 
161
{
 
162
  memcpy(to, ptr, length_arg);
 
163
}
 
164
 
 
165
void Boolean::setTrue()
 
166
{
 
167
  ptr[0]= set_true.byte;
 
168
}
 
169
 
 
170
} /* namespace field */
 
171
} /* namespace drizzled */