~drizzle-pbxt/drizzle/drizzle-pbxt-2

« back to all changes in this revision

Viewing changes to drizzled/stored_key.h

  • Committer: Paul McCullagh
  • Date: 2009-11-10 14:18:39 UTC
  • mfrom: (1038.1.7 drizzle-pbxt-pre-merge)
  • Revision ID: paul.mccullagh@primebase.org-20091110141839-2j3k43b17ag6f605
Merged Drizzle trunk and PBXT 1.0.09

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
/** class to store an field/item as a key struct */
 
24
class StoredKey :public Sql_alloc
 
25
{
 
26
public:
 
27
  bool null_key; /**< If true, the value of the key has a null part */
 
28
  enum store_key_result 
 
29
  { 
 
30
    STORE_KEY_OK,
 
31
    STORE_KEY_FATAL, 
 
32
    STORE_KEY_CONV 
 
33
  };
 
34
protected:
 
35
  Field *to_field;                              // Store data here
 
36
  unsigned char *null_ptr;
 
37
  unsigned char err;
 
38
  virtual enum store_key_result copy_inner()=0;
 
39
public:
 
40
  StoredKey(Session *session,
 
41
            Field *field_arg, 
 
42
            unsigned char *ptr,
 
43
            unsigned char *null, 
 
44
            uint32_t length)
 
45
    :
 
46
      null_key(0), 
 
47
      null_ptr(null), 
 
48
      err(0)
 
49
  {
 
50
    if (field_arg->type() == DRIZZLE_TYPE_BLOB)
 
51
    {
 
52
      /*
 
53
        Key segments are always packed with a 2 byte length prefix.
 
54
        See mi_rkey for details.
 
55
      */
 
56
      to_field= new Field_varstring(ptr,
 
57
                                    length,
 
58
                                    2,
 
59
                                    null,
 
60
                                    1,
 
61
                                    field_arg->field_name,
 
62
                                    field_arg->table->s,
 
63
                                    field_arg->charset());
 
64
      to_field->init(field_arg->table);
 
65
    }
 
66
    else
 
67
      to_field= field_arg->new_key_field(session->mem_root, field_arg->table,
 
68
                                        ptr, null, 1);
 
69
 
 
70
    to_field->setWriteSet();
 
71
  }
 
72
  virtual ~StoredKey() {}                       /** Not actually needed */
 
73
  virtual const char *name() const=0;
 
74
 
 
75
  /**
 
76
    @brief sets ignore truncation warnings mode and calls the real copy method
 
77
 
 
78
    @details this function makes sure truncation warnings when preparing the
 
79
    key buffers don't end up as errors (because of an enclosing INSERT/UPDATE).
 
80
  */
 
81
  enum store_key_result copy()
 
82
  {
 
83
    enum store_key_result result;
 
84
    Session *session= to_field->table->in_use;
 
85
    enum_check_fields saved_count_cuted_fields= session->count_cuted_fields;
 
86
    session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
87
    result= copy_inner();
 
88
    session->count_cuted_fields= saved_count_cuted_fields;
 
89
 
 
90
    return result;
 
91
  }
 
92
};
 
93
 
 
94
class store_key_field: public StoredKey
 
95
{
 
96
  CopyField copy_field;
 
97
  const char *field_name;
 
98
public:
 
99
  store_key_field(Session *session, Field *to_field_arg, unsigned char *ptr,
 
100
                  unsigned char *null_ptr_arg,
 
101
                  uint32_t length, Field *from_field, const char *name_arg)
 
102
    :StoredKey(session, to_field_arg,ptr,
 
103
               null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
 
104
               : (unsigned char*) 0, length), field_name(name_arg)
 
105
  {
 
106
    if (to_field)
 
107
    {
 
108
      copy_field.set(to_field,from_field,0);
 
109
    }
 
110
  }
 
111
  const char *name() const { return field_name; }
 
112
 
 
113
protected:
 
114
  enum store_key_result copy_inner()
 
115
  {
 
116
    copy_field.do_copy(&copy_field);
 
117
    null_key= to_field->is_null();
 
118
    return err != 0 ? STORE_KEY_FATAL : STORE_KEY_OK;
 
119
  }
 
120
};
 
121
 
 
122
class store_key_item :public StoredKey
 
123
{
 
124
 protected:
 
125
  Item *item;
 
126
public:
 
127
  store_key_item(Session *session, Field *to_field_arg, unsigned char *ptr,
 
128
                 unsigned char *null_ptr_arg, uint32_t length, Item *item_arg)
 
129
    :StoredKey(session, to_field_arg, ptr,
 
130
               null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
 
131
               &err : (unsigned char*) 0, length), item(item_arg)
 
132
  {}
 
133
  const char *name() const { return "func"; }
 
134
 
 
135
 protected:
 
136
  enum store_key_result copy_inner()
 
137
  {
 
138
    int res= item->save_in_field(to_field, 1);
 
139
    null_key= to_field->is_null() || item->null_value;
 
140
    return (err != 0 || res > 2 ? STORE_KEY_FATAL : (store_key_result) res);
 
141
  }
 
142
};
 
143
 
 
144
class store_key_const_item :public store_key_item
 
145
{
 
146
  bool inited;
 
147
public:
 
148
  store_key_const_item(Session *session, Field *to_field_arg, unsigned char *ptr,
 
149
                       unsigned char *null_ptr_arg, uint32_t length,
 
150
                       Item *item_arg)
 
151
    :store_key_item(session, to_field_arg,ptr,
 
152
                    null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
 
153
                    &err : (unsigned char*) 0, length, item_arg), inited(0)
 
154
  {
 
155
  }
 
156
  const char *name() const { return "const"; }
 
157
 
 
158
protected:
 
159
  enum store_key_result copy_inner()
 
160
  {
 
161
    int res;
 
162
    if (!inited)
 
163
    {
 
164
      inited=1;
 
165
      if ((res= item->save_in_field(to_field, 1)))
 
166
      {
 
167
        if (!err)
 
168
          err= res;
 
169
      }
 
170
    }
 
171
    null_key= to_field->is_null() || item->null_value;
 
172
    return (err > 2 ?  STORE_KEY_FATAL : (store_key_result) err);
 
173
  }
 
174
};
 
175
 
 
176
#endif /* DRIZZLED_STORED_KEY_H */