drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
field/boolean.cc
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#include <algorithm>
24#include <drizzled/field/boolean.h>
25#include <drizzled/type/boolean.h>
26#include <drizzled/error.h>
27#include <drizzled/internal/my_sys.h>
28#include <drizzled/session.h>
29#include <drizzled/table.h>
30#include <drizzled/temporal.h>
31
32namespace drizzled {
33namespace field {
34
35Boolean::Boolean(unsigned char *ptr_arg,
36 uint32_t len_arg,
37 unsigned char *null_ptr_arg,
38 unsigned char null_bit_arg,
39 const char *field_name_arg,
40 bool ansi_display_arg) :
41 Field(ptr_arg, len_arg,
42 null_ptr_arg,
43 null_bit_arg,
44 Field::NONE,
45 field_name_arg),
46 ansi_display(ansi_display_arg)
47 {
48 if (ansi_display)
49 flags|= UNSIGNED_FLAG;
50 }
51
52int Boolean::cmp(const unsigned char *a, const unsigned char *b)
53{
54 return memcmp(a, b, sizeof(unsigned char));
55}
56
57int Boolean::store(const char *from, uint32_t length, const charset_info_st * const )
58{
59 ASSERT_COLUMN_MARKED_FOR_WRITE;
60
61 bool result;
62 if (not type::convert(result, from, length))
63 {
64 my_error(ER_INVALID_BOOLEAN_VALUE, MYF(0), from);
65 return 1;
66 }
67 set(result);
68 return 0;
69}
70
71int Boolean::store(int64_t nr, bool)
72{
73 ASSERT_COLUMN_MARKED_FOR_WRITE;
74 set(nr != 0);
75 return 0;
76}
77
78int Boolean::store(double nr)
79{
80 ASSERT_COLUMN_MARKED_FOR_WRITE;
81 set(nr);
82 return 0;
83}
84
85int Boolean::store_decimal(const drizzled::type::Decimal *dec)
86{
87 ASSERT_COLUMN_MARKED_FOR_WRITE;
88 set(not dec->isZero());
89 return 0;
90}
91
92double Boolean::val_real() const
93{
94 ASSERT_COLUMN_MARKED_FOR_READ;
95 return isTrue();
96}
97
98int64_t Boolean::val_int() const
99{
100 ASSERT_COLUMN_MARKED_FOR_READ;
101 return isTrue();
102}
103
104String *Boolean::val_str(String *val_buffer, String *) const
105{
106 ASSERT_COLUMN_MARKED_FOR_READ;
107 type::convert(*val_buffer, isTrue(), ansi_display);
108 return val_buffer;
109}
110
111type::Decimal *Boolean::val_decimal(type::Decimal *dec) const
112{
113 if (isTrue())
114 {
115 int2_class_decimal(E_DEC_OK, 1, false, dec);
116 return dec;
117 }
118 dec->set_zero();
119 return dec;
120}
121
122void Boolean::sort_string(unsigned char *to, uint32_t length_arg)
123{
124 memcpy(to, ptr, length_arg);
125}
126
127} /* namespace field */
128} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.