~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/boolean.cc

few updates and modifications to admin commands

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
 
    if (ansi_display)
64
 
      flags|= UNSIGNED_FLAG;
65
 
  }
66
 
 
67
 
int Boolean::cmp(const unsigned char *a, const unsigned char *b)
68
 
69
 
  return memcmp(a, b, sizeof(unsigned char));
70
 
}
71
 
 
72
 
int Boolean::store(const char *from, uint32_t length, const CHARSET_INFO * const )
73
 
{
74
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
75
 
 
76
 
  bool result;
77
 
  if (not type::convert(result, from, length))
78
 
  {
79
 
    my_error(ER_INVALID_BOOLEAN_VALUE, MYF(0), from);
80
 
    return 1;
81
 
  }
82
 
 
83
 
  if (result)
84
 
  {
85
 
    setTrue();
86
 
  }
87
 
  else
88
 
  {
89
 
    setFalse();
90
 
  }
91
 
 
92
 
  return 0;
93
 
}
94
 
 
95
 
int Boolean::store(int64_t nr, bool )
96
 
{
97
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
98
 
  nr == 0 ? setFalse() : setTrue();
99
 
  return 0;
100
 
}
101
 
 
102
 
int  Boolean::store(double nr)
103
 
{
104
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
105
 
  nr == 0 ? setFalse() : setTrue();
106
 
  return 0;
107
 
}
108
 
 
109
 
int Boolean::store_decimal(const drizzled::type::Decimal *dec)
110
 
{
111
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
112
 
  if (dec->isZero())
113
 
  {
114
 
    setFalse();
115
 
    return 0;
116
 
  }
117
 
 
118
 
  setTrue();
119
 
 
120
 
  return 0;
121
 
}
122
 
 
123
 
void Boolean::sql_type(String &res) const
124
 
{
125
 
  res.set_ascii(STRING_WITH_LEN("boolean"));
126
 
}
127
 
 
128
 
double Boolean::val_real() const
129
 
{
130
 
  ASSERT_COLUMN_MARKED_FOR_READ;
131
 
  return isTrue();
132
 
}
133
 
 
134
 
int64_t Boolean::val_int() const
135
 
{
136
 
  ASSERT_COLUMN_MARKED_FOR_READ;
137
 
  return isTrue();
138
 
}
139
 
 
140
 
String *Boolean::val_str(String *val_buffer, String *) const
141
 
{
142
 
  ASSERT_COLUMN_MARKED_FOR_READ;
143
 
 
144
 
  (void)type::convert(*val_buffer, isTrue(), ansi_display);
145
 
 
146
 
  return val_buffer;
147
 
}
148
 
 
149
 
type::Decimal *Boolean::val_decimal(type::Decimal *dec) const
150
 
{
151
 
  if (isTrue())
152
 
  {
153
 
    int2_class_decimal(E_DEC_OK, 1, false, dec);
154
 
    return dec;
155
 
  }
156
 
 
157
 
  dec->set_zero();
158
 
 
159
 
  return dec;
160
 
}
161
 
 
162
 
void Boolean::sort_string(unsigned char *to, uint32_t length_arg)
163
 
{
164
 
  memcpy(to, ptr, length_arg);
165
 
}
166
 
 
167
 
void Boolean::setTrue()
168
 
{
169
 
  ptr[0]= set_true.byte;
170
 
}
171
 
 
172
 
} /* namespace field */
173
 
} /* namespace drizzled */