Drizzled Public API Documentation

boolean.cc

00001 /* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Brian Aker
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 
00022 #include <config.h>
00023 
00024 #include <algorithm>
00025 
00026 #include <drizzled/field/boolean.h>
00027 #include <drizzled/type/boolean.h>
00028 
00029 #include <drizzled/error.h>
00030 #include <drizzled/internal/my_sys.h>
00031 #include <drizzled/session.h>
00032 #include <drizzled/table.h>
00033 #include <drizzled/temporal.h>
00034 
00035 union set_true_t {
00036   unsigned char byte;
00037   bool is_true:1;
00038 
00039   set_true_t()
00040   {
00041     is_true= true;
00042   }
00043 } set_true;
00044 
00045 namespace drizzled
00046 {
00047 namespace field
00048 {
00049 
00050 Boolean::Boolean(unsigned char *ptr_arg,
00051                  uint32_t len_arg,
00052                  unsigned char *null_ptr_arg,
00053                  unsigned char null_bit_arg,
00054                  const char *field_name_arg,
00055                  bool ansi_display_arg) :
00056   Field(ptr_arg, len_arg,
00057         null_ptr_arg,
00058         null_bit_arg,
00059         Field::NONE,
00060         field_name_arg),
00061   ansi_display(ansi_display_arg)
00062   {
00063     if (ansi_display)
00064       flags|= UNSIGNED_FLAG;
00065   }
00066 
00067 int Boolean::cmp(const unsigned char *a, const unsigned char *b)
00068 { 
00069   return memcmp(a, b, sizeof(unsigned char));
00070 }
00071 
00072 int Boolean::store(const char *from, uint32_t length, const CHARSET_INFO * const )
00073 {
00074   ASSERT_COLUMN_MARKED_FOR_WRITE;
00075 
00076   bool result;
00077   if (not type::convert(result, from, length))
00078   {
00079     my_error(ER_INVALID_BOOLEAN_VALUE, MYF(0), from);
00080     return 1;
00081   }
00082 
00083   if (result)
00084   {
00085     setTrue();
00086   }
00087   else
00088   {
00089     setFalse();
00090   }
00091 
00092   return 0;
00093 }
00094 
00095 int Boolean::store(int64_t nr, bool )
00096 {
00097   ASSERT_COLUMN_MARKED_FOR_WRITE;
00098   nr == 0 ? setFalse() : setTrue();
00099   return 0;
00100 }
00101 
00102 int  Boolean::store(double nr)
00103 {
00104   ASSERT_COLUMN_MARKED_FOR_WRITE;
00105   nr == 0 ? setFalse() : setTrue();
00106   return 0;
00107 }
00108 
00109 int Boolean::store_decimal(const drizzled::type::Decimal *dec)
00110 {
00111   ASSERT_COLUMN_MARKED_FOR_WRITE;
00112   if (dec->isZero())
00113   {
00114     setFalse();
00115     return 0;
00116   }
00117 
00118   setTrue();
00119 
00120   return 0;
00121 }
00122 
00123 void Boolean::sql_type(String &res) const
00124 {
00125   res.set_ascii(STRING_WITH_LEN("boolean"));
00126 }
00127 
00128 double Boolean::val_real() const
00129 {
00130   ASSERT_COLUMN_MARKED_FOR_READ;
00131   return isTrue();
00132 }
00133 
00134 int64_t Boolean::val_int() const
00135 {
00136   ASSERT_COLUMN_MARKED_FOR_READ;
00137   return isTrue();
00138 }
00139 
00140 String *Boolean::val_str(String *val_buffer, String *) const
00141 {
00142   ASSERT_COLUMN_MARKED_FOR_READ;
00143 
00144   (void)type::convert(*val_buffer, isTrue(), ansi_display);
00145 
00146   return val_buffer;
00147 }
00148 
00149 type::Decimal *Boolean::val_decimal(type::Decimal *dec) const
00150 {
00151   if (isTrue())
00152   {
00153     int2_class_decimal(E_DEC_OK, 1, false, dec);
00154     return dec;
00155   }
00156 
00157   dec->set_zero();
00158 
00159   return dec;
00160 }
00161 
00162 void Boolean::sort_string(unsigned char *to, uint32_t length_arg)
00163 {
00164   memcpy(to, ptr, length_arg);
00165 }
00166 
00167 void Boolean::setTrue()
00168 {
00169   ptr[0]= set_true.byte;
00170 }
00171 
00172 } /* namespace field */
00173 } /* namespace drizzled */