~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/stored_key.h

  • Committer: Lee
  • Date: 2008-10-30 22:02:01 UTC
  • mto: (572.1.2 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: lbieber@lbieber-desktop-20081030220201-elb6qprbzpn7c5a4
add my name to the AUTHORS file

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