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 #include <algorithm>
00024 #include <drizzled/field/boolean.h>
00025 #include <drizzled/type/boolean.h>
00026 #include <drizzled/error.h>
00027 #include <drizzled/internal/my_sys.h>
00028 #include <drizzled/session.h>
00029 #include <drizzled/table.h>
00030 #include <drizzled/temporal.h>
00031 
00032 namespace drizzled {
00033 namespace field {
00034 
00035 Boolean::Boolean(unsigned char *ptr_arg,
00036                  uint32_t len_arg,
00037                  unsigned char *null_ptr_arg,
00038                  unsigned char null_bit_arg,
00039                  const char *field_name_arg,
00040                  bool ansi_display_arg) :
00041   Field(ptr_arg, len_arg,
00042         null_ptr_arg,
00043         null_bit_arg,
00044         Field::NONE,
00045         field_name_arg),
00046   ansi_display(ansi_display_arg)
00047   {
00048     if (ansi_display)
00049       flags|= UNSIGNED_FLAG;
00050   }
00051 
00052 int Boolean::cmp(const unsigned char *a, const unsigned char *b)
00053 { 
00054   return memcmp(a, b, sizeof(unsigned char));
00055 }
00056 
00057 int Boolean::store(const char *from, uint32_t length, const charset_info_st * const )
00058 {
00059   ASSERT_COLUMN_MARKED_FOR_WRITE;
00060 
00061   bool result;
00062   if (not type::convert(result, from, length))
00063   {
00064     my_error(ER_INVALID_BOOLEAN_VALUE, MYF(0), from);
00065     return 1;
00066   }
00067   set(result);
00068   return 0;
00069 }
00070 
00071 int Boolean::store(int64_t nr, bool)
00072 {
00073   ASSERT_COLUMN_MARKED_FOR_WRITE;
00074   set(nr != 0);
00075   return 0;
00076 }
00077 
00078 int  Boolean::store(double nr)
00079 {
00080   ASSERT_COLUMN_MARKED_FOR_WRITE;
00081   set(nr);
00082   return 0;
00083 }
00084 
00085 int Boolean::store_decimal(const drizzled::type::Decimal *dec)
00086 {
00087   ASSERT_COLUMN_MARKED_FOR_WRITE;
00088   set(not dec->isZero());
00089   return 0;
00090 }
00091 
00092 double Boolean::val_real() const
00093 {
00094   ASSERT_COLUMN_MARKED_FOR_READ;
00095   return isTrue();
00096 }
00097 
00098 int64_t Boolean::val_int() const
00099 {
00100   ASSERT_COLUMN_MARKED_FOR_READ;
00101   return isTrue();
00102 }
00103 
00104 String *Boolean::val_str(String *val_buffer, String *) const
00105 {
00106   ASSERT_COLUMN_MARKED_FOR_READ;
00107   type::convert(*val_buffer, isTrue(), ansi_display);
00108   return val_buffer;
00109 }
00110 
00111 type::Decimal *Boolean::val_decimal(type::Decimal *dec) const
00112 {
00113   if (isTrue())
00114   {
00115     int2_class_decimal(E_DEC_OK, 1, false, dec);
00116     return dec;
00117   }
00118   dec->set_zero();
00119   return dec;
00120 }
00121 
00122 void Boolean::sort_string(unsigned char *to, uint32_t length_arg)
00123 {
00124   memcpy(to, ptr, length_arg);
00125 }
00126 
00127 } /* namespace field */
00128 } /* namespace drizzled */