~drizzle-trunk/drizzle/development

1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
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
2234 by Brian Aker
Mass removal of ifdef/endif in favor of pragma once.
20
#pragma once
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
21
2154.2.11 by Brian Aker
Further strip out includes.
22
#include <drizzled/memory/sql_alloc.h>
23
#include <drizzled/copy_field.h>
2241.2.14 by Olaf van der Spek
Refactor
24
#include <drizzled/item.h>
2154.2.11 by Brian Aker
Further strip out includes.
25
2241.2.14 by Olaf van der Spek
Refactor
26
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
27
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
28
/** class to store an field/item as a key struct */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
29
class StoredKey :public memory::SqlAlloc
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
30
{
31
public:
32
  bool null_key; /**< If true, the value of the key has a null part */
33
  enum store_key_result 
34
  { 
35
    STORE_KEY_OK,
36
    STORE_KEY_FATAL, 
37
    STORE_KEY_CONV 
38
  };
2154.2.11 by Brian Aker
Further strip out includes.
39
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
40
protected:
41
  Field *to_field;				// Store data here
42
  unsigned char *null_ptr;
43
  unsigned char err;
44
  virtual enum store_key_result copy_inner()=0;
2154.2.11 by Brian Aker
Further strip out includes.
45
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
46
public:
47
  StoredKey(Session *session,
48
            Field *field_arg, 
49
            unsigned char *ptr,
50
            unsigned char *null, 
2154.2.6 by Brian Aker
Merge in small cleanups.
51
            uint32_t length);
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
52
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
53
  virtual ~StoredKey() {}			/** Not actually needed */
54
  virtual const char *name() const=0;
55
56
  /**
57
    @brief sets ignore truncation warnings mode and calls the real copy method
58
59
    @details this function makes sure truncation warnings when preparing the
60
    key buffers don't end up as errors (because of an enclosing INSERT/UPDATE).
61
  */
2154.2.6 by Brian Aker
Merge in small cleanups.
62
  enum store_key_result copy();
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
63
64
};
65
66
class store_key_field: public StoredKey
67
{
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
68
  CopyField copy_field;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
69
  const char *field_name;
2154.2.6 by Brian Aker
Merge in small cleanups.
70
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
71
public:
72
  store_key_field(Session *session, Field *to_field_arg, unsigned char *ptr,
73
                  unsigned char *null_ptr_arg,
2154.2.11 by Brian Aker
Further strip out includes.
74
                  uint32_t length, Field *from_field, const char *name_arg) :
75
    StoredKey(session, to_field_arg,ptr,
76
              null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
77
              : (unsigned char*) 0, length), field_name(name_arg)
78
    {
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
79
    if (to_field)
80
    {
81
      copy_field.set(to_field,from_field,0);
82
    }
83
  }
84
  const char *name() const { return field_name; }
85
86
protected:
87
  enum store_key_result copy_inner()
88
  {
89
    copy_field.do_copy(&copy_field);
90
    null_key= to_field->is_null();
91
    return err != 0 ? STORE_KEY_FATAL : STORE_KEY_OK;
92
  }
93
};
94
95
class store_key_item :public StoredKey
96
{
2154.2.11 by Brian Aker
Further strip out includes.
97
protected:
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
98
  Item *item;
2154.2.11 by Brian Aker
Further strip out includes.
99
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
100
public:
101
  store_key_item(Session *session, Field *to_field_arg, unsigned char *ptr,
2154.2.6 by Brian Aker
Merge in small cleanups.
102
                 unsigned char *null_ptr_arg, uint32_t length, Item *item_arg) :
103
    StoredKey(session, to_field_arg, ptr,
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
104
	       null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
105
	       &err : (unsigned char*) 0, length), item(item_arg)
106
  {}
107
  const char *name() const { return "func"; }
108
109
 protected:
110
  enum store_key_result copy_inner()
111
  {
112
    int res= item->save_in_field(to_field, 1);
113
    null_key= to_field->is_null() || item->null_value;
114
    return (err != 0 || res > 2 ? STORE_KEY_FATAL : (store_key_result) res);
115
  }
116
};
117
118
class store_key_const_item :public store_key_item
119
{
120
  bool inited;
2154.2.11 by Brian Aker
Further strip out includes.
121
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
122
public:
123
  store_key_const_item(Session *session, Field *to_field_arg, unsigned char *ptr,
2154.2.11 by Brian Aker
Further strip out includes.
124
                       unsigned char *null_ptr_arg, uint32_t length,
125
                       Item *item_arg) :
2154.2.6 by Brian Aker
Merge in small cleanups.
126
    store_key_item(session, to_field_arg,ptr,
127
                   null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
128
                   &err : (unsigned char*) 0, length, item_arg), inited(0)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
129
  {
130
  }
131
  const char *name() const { return "const"; }
132
133
protected:
134
  enum store_key_result copy_inner()
135
  {
136
    int res;
137
    if (!inited)
138
    {
139
      inited=1;
140
      if ((res= item->save_in_field(to_field, 1)))
141
      {
142
        if (!err)
143
          err= res;
144
      }
145
    }
146
    null_key= to_field->is_null() || item->null_value;
147
    return (err > 2 ?  STORE_KEY_FATAL : (store_key_result) err);
148
  }
149
};
150
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
151
} /* namespace drizzled */
152