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 #include <drizzled/item.h>
00025 
00026 namespace drizzled {
00027 
00029 class StoredKey :public memory::SqlAlloc
00030 {
00031 public:
00032   bool null_key; 
00033   enum store_key_result 
00034   { 
00035     STORE_KEY_OK,
00036     STORE_KEY_FATAL, 
00037     STORE_KEY_CONV 
00038   };
00039 
00040 protected:
00041   Field *to_field;        // Store data here
00042   unsigned char *null_ptr;
00043   unsigned char err;
00044   virtual enum store_key_result copy_inner()=0;
00045 
00046 public:
00047   StoredKey(Session *session,
00048             Field *field_arg, 
00049             unsigned char *ptr,
00050             unsigned char *null, 
00051             uint32_t length);
00052 
00053   virtual ~StoredKey() {}     
00054   virtual const char *name() const=0;
00055 
00062   enum store_key_result copy();
00063 
00064 };
00065 
00066 class store_key_field: public StoredKey
00067 {
00068   CopyField copy_field;
00069   const char *field_name;
00070 
00071 public:
00072   store_key_field(Session *session, Field *to_field_arg, unsigned char *ptr,
00073                   unsigned char *null_ptr_arg,
00074                   uint32_t length, Field *from_field, const char *name_arg) :
00075     StoredKey(session, to_field_arg,ptr,
00076               null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
00077               : (unsigned char*) 0, length), field_name(name_arg)
00078     {
00079     if (to_field)
00080     {
00081       copy_field.set(to_field,from_field,0);
00082     }
00083   }
00084   const char *name() const { return field_name; }
00085 
00086 protected:
00087   enum store_key_result copy_inner()
00088   {
00089     copy_field.do_copy(&copy_field);
00090     null_key= to_field->is_null();
00091     return err != 0 ? STORE_KEY_FATAL : STORE_KEY_OK;
00092   }
00093 };
00094 
00095 class store_key_item :public StoredKey
00096 {
00097 protected:
00098   Item *item;
00099 
00100 public:
00101   store_key_item(Session *session, Field *to_field_arg, unsigned char *ptr,
00102                  unsigned char *null_ptr_arg, uint32_t length, Item *item_arg) :
00103     StoredKey(session, to_field_arg, ptr,
00104          null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
00105          &err : (unsigned char*) 0, length), item(item_arg)
00106   {}
00107   const char *name() const { return "func"; }
00108 
00109  protected:
00110   enum store_key_result copy_inner()
00111   {
00112     int res= item->save_in_field(to_field, 1);
00113     null_key= to_field->is_null() || item->null_value;
00114     return (err != 0 || res > 2 ? STORE_KEY_FATAL : (store_key_result) res);
00115   }
00116 };
00117 
00118 class store_key_const_item :public store_key_item
00119 {
00120   bool inited;
00121 
00122 public:
00123   store_key_const_item(Session *session, Field *to_field_arg, unsigned char *ptr,
00124                        unsigned char *null_ptr_arg, uint32_t length,
00125                        Item *item_arg) :
00126     store_key_item(session, to_field_arg,ptr,
00127                    null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
00128                    &err : (unsigned char*) 0, length, item_arg), inited(0)
00129   {
00130   }
00131   const char *name() const { return "const"; }
00132 
00133 protected:
00134   enum store_key_result copy_inner()
00135   {
00136     int res;
00137     if (!inited)
00138     {
00139       inited=1;
00140       if ((res= item->save_in_field(to_field, 1)))
00141       {
00142         if (!err)
00143           err= res;
00144       }
00145     }
00146     null_key= to_field->is_null() || item->null_value;
00147     return (err > 2 ?  STORE_KEY_FATAL : (store_key_result) err);
00148   }
00149 };
00150 
00151 } /* namespace drizzled */
00152