Drizzled Public API Documentation

stored_key.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
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; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00022 #include <drizzled/memory/sql_alloc.h>
00023 #include <drizzled/copy_field.h>
00024 
00025 namespace drizzled
00026 {
00027 
00028 class Field;
00029 class Session;
00030 class Item;
00031 
00033 class StoredKey :public memory::SqlAlloc
00034 {
00035 public:
00036   bool null_key; 
00037   enum store_key_result 
00038   { 
00039     STORE_KEY_OK,
00040     STORE_KEY_FATAL, 
00041     STORE_KEY_CONV 
00042   };
00043 
00044 protected:
00045   Field *to_field;        // Store data here
00046   unsigned char *null_ptr;
00047   unsigned char err;
00048   virtual enum store_key_result copy_inner()=0;
00049 
00050 public:
00051   StoredKey(Session *session,
00052             Field *field_arg, 
00053             unsigned char *ptr,
00054             unsigned char *null, 
00055             uint32_t length);
00056 
00057   virtual ~StoredKey() {}     
00058   virtual const char *name() const=0;
00059 
00066   enum store_key_result copy();
00067 
00068 };
00069 
00070 class store_key_field: public StoredKey
00071 {
00072   CopyField copy_field;
00073   const char *field_name;
00074 
00075 public:
00076   store_key_field(Session *session, Field *to_field_arg, unsigned char *ptr,
00077                   unsigned char *null_ptr_arg,
00078                   uint32_t length, Field *from_field, const char *name_arg) :
00079     StoredKey(session, to_field_arg,ptr,
00080               null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
00081               : (unsigned char*) 0, length), field_name(name_arg)
00082     {
00083     if (to_field)
00084     {
00085       copy_field.set(to_field,from_field,0);
00086     }
00087   }
00088   const char *name() const { return field_name; }
00089 
00090 protected:
00091   enum store_key_result copy_inner()
00092   {
00093     copy_field.do_copy(&copy_field);
00094     null_key= to_field->is_null();
00095     return err != 0 ? STORE_KEY_FATAL : STORE_KEY_OK;
00096   }
00097 };
00098 
00099 class store_key_item :public StoredKey
00100 {
00101 protected:
00102   Item *item;
00103 
00104 public:
00105   store_key_item(Session *session, Field *to_field_arg, unsigned char *ptr,
00106                  unsigned char *null_ptr_arg, uint32_t length, Item *item_arg) :
00107     StoredKey(session, to_field_arg, ptr,
00108          null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
00109          &err : (unsigned char*) 0, length), item(item_arg)
00110   {}
00111   const char *name() const { return "func"; }
00112 
00113  protected:
00114   enum store_key_result copy_inner()
00115   {
00116     int res= item->save_in_field(to_field, 1);
00117     null_key= to_field->is_null() || item->null_value;
00118     return (err != 0 || res > 2 ? STORE_KEY_FATAL : (store_key_result) res);
00119   }
00120 };
00121 
00122 class store_key_const_item :public store_key_item
00123 {
00124   bool inited;
00125 
00126 public:
00127   store_key_const_item(Session *session, Field *to_field_arg, unsigned char *ptr,
00128                        unsigned char *null_ptr_arg, uint32_t length,
00129                        Item *item_arg) :
00130     store_key_item(session, to_field_arg,ptr,
00131                    null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
00132                    &err : (unsigned char*) 0, length, item_arg), inited(0)
00133   {
00134   }
00135   const char *name() const { return "const"; }
00136 
00137 protected:
00138   enum store_key_result copy_inner()
00139   {
00140     int res;
00141     if (!inited)
00142     {
00143       inited=1;
00144       if ((res= item->save_in_field(to_field, 1)))
00145       {
00146         if (!err)
00147           err= res;
00148       }
00149     }
00150     null_key= to_field->is_null() || item->null_value;
00151     return (err > 2 ?  STORE_KEY_FATAL : (store_key_result) err);
00152   }
00153 };
00154 
00155 } /* namespace drizzled */
00156