~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/stored_key.h

  • Committer: Monty Taylor
  • Date: 2008-11-16 06:29:53 UTC
  • mto: (584.1.9 devel)
  • mto: This revision was merged to the branch mainline in revision 589.
  • Revision ID: monty@inaugust.com-20081116062953-ivdltjmfe009b5fr
Moved stuff into item/

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
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; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#ifndef DRIZZLED_STORED_KEY_H
21
 
#define DRIZZLED_STORED_KEY_H
22
 
 
23
 
namespace drizzled
24
 
{
25
 
 
26
 
/** class to store an field/item as a key struct */
27
 
class StoredKey :public memory::SqlAlloc
28
 
{
29
 
public:
30
 
  bool null_key; /**< If true, the value of the key has a null part */
31
 
  enum store_key_result 
32
 
  { 
33
 
    STORE_KEY_OK,
34
 
    STORE_KEY_FATAL, 
35
 
    STORE_KEY_CONV 
36
 
  };
37
 
protected:
38
 
  Field *to_field;                              // Store data here
39
 
  unsigned char *null_ptr;
40
 
  unsigned char err;
41
 
  virtual enum store_key_result copy_inner()=0;
42
 
public:
43
 
  StoredKey(Session *session,
44
 
            Field *field_arg, 
45
 
            unsigned char *ptr,
46
 
            unsigned char *null, 
47
 
            uint32_t length)
48
 
    :
49
 
      null_key(0), 
50
 
      null_ptr(null), 
51
 
      err(0)
52
 
  {
53
 
    if (field_arg->type() == DRIZZLE_TYPE_BLOB)
54
 
    {
55
 
      /*
56
 
        Key segments are always packed with a 2 byte length prefix.
57
 
        See mi_rkey for details.
58
 
      */
59
 
      to_field= new Field_varstring(ptr,
60
 
                                    length,
61
 
                                    2,
62
 
                                    null,
63
 
                                    1,
64
 
                                    field_arg->field_name,
65
 
                                    field_arg->getTable()->getMutableShare(),
66
 
                                    field_arg->charset());
67
 
      to_field->init(field_arg->getTable());
68
 
    }
69
 
    else
70
 
      to_field= field_arg->new_key_field(session->mem_root, field_arg->getTable(),
71
 
                                        ptr, null, 1);
72
 
 
73
 
    to_field->setWriteSet();
74
 
  }
75
 
  virtual ~StoredKey() {}                       /** Not actually needed */
76
 
  virtual const char *name() const=0;
77
 
 
78
 
  /**
79
 
    @brief sets ignore truncation warnings mode and calls the real copy method
80
 
 
81
 
    @details this function makes sure truncation warnings when preparing the
82
 
    key buffers don't end up as errors (because of an enclosing INSERT/UPDATE).
83
 
  */
84
 
  enum store_key_result copy()
85
 
  {
86
 
    enum store_key_result result;
87
 
    Session *session= to_field->getTable()->in_use;
88
 
    enum_check_fields saved_count_cuted_fields= session->count_cuted_fields;
89
 
    session->count_cuted_fields= CHECK_FIELD_IGNORE;
90
 
    result= copy_inner();
91
 
    session->count_cuted_fields= saved_count_cuted_fields;
92
 
 
93
 
    return result;
94
 
  }
95
 
};
96
 
 
97
 
class store_key_field: public StoredKey
98
 
{
99
 
  CopyField copy_field;
100
 
  const char *field_name;
101
 
public:
102
 
  store_key_field(Session *session, Field *to_field_arg, unsigned char *ptr,
103
 
                  unsigned char *null_ptr_arg,
104
 
                  uint32_t length, Field *from_field, const char *name_arg)
105
 
    :StoredKey(session, to_field_arg,ptr,
106
 
               null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
107
 
               : (unsigned char*) 0, length), field_name(name_arg)
108
 
  {
109
 
    if (to_field)
110
 
    {
111
 
      copy_field.set(to_field,from_field,0);
112
 
    }
113
 
  }
114
 
  const char *name() const { return field_name; }
115
 
 
116
 
protected:
117
 
  enum store_key_result copy_inner()
118
 
  {
119
 
    copy_field.do_copy(&copy_field);
120
 
    null_key= to_field->is_null();
121
 
    return err != 0 ? STORE_KEY_FATAL : STORE_KEY_OK;
122
 
  }
123
 
};
124
 
 
125
 
class store_key_item :public StoredKey
126
 
{
127
 
 protected:
128
 
  Item *item;
129
 
public:
130
 
  store_key_item(Session *session, Field *to_field_arg, unsigned char *ptr,
131
 
                 unsigned char *null_ptr_arg, uint32_t length, Item *item_arg)
132
 
    :StoredKey(session, to_field_arg, ptr,
133
 
               null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
134
 
               &err : (unsigned char*) 0, length), item(item_arg)
135
 
  {}
136
 
  const char *name() const { return "func"; }
137
 
 
138
 
 protected:
139
 
  enum store_key_result copy_inner()
140
 
  {
141
 
    int res= item->save_in_field(to_field, 1);
142
 
    null_key= to_field->is_null() || item->null_value;
143
 
    return (err != 0 || res > 2 ? STORE_KEY_FATAL : (store_key_result) res);
144
 
  }
145
 
};
146
 
 
147
 
class store_key_const_item :public store_key_item
148
 
{
149
 
  bool inited;
150
 
public:
151
 
  store_key_const_item(Session *session, Field *to_field_arg, unsigned char *ptr,
152
 
                       unsigned char *null_ptr_arg, uint32_t length,
153
 
                       Item *item_arg)
154
 
    :store_key_item(session, to_field_arg,ptr,
155
 
                    null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
156
 
                    &err : (unsigned char*) 0, length, item_arg), inited(0)
157
 
  {
158
 
  }
159
 
  const char *name() const { return "const"; }
160
 
 
161
 
protected:
162
 
  enum store_key_result copy_inner()
163
 
  {
164
 
    int res;
165
 
    if (!inited)
166
 
    {
167
 
      inited=1;
168
 
      if ((res= item->save_in_field(to_field, 1)))
169
 
      {
170
 
        if (!err)
171
 
          err= res;
172
 
      }
173
 
    }
174
 
    null_key= to_field->is_null() || item->null_value;
175
 
    return (err > 2 ?  STORE_KEY_FATAL : (store_key_result) err);
176
 
  }
177
 
};
178
 
 
179
 
} /* namespace drizzled */
180
 
 
181
 
#endif /* DRIZZLED_STORED_KEY_H */