~drizzle-trunk/drizzle/development

642.1.1 by Lee
move functions from item.cc/item.h to item directory
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
#include <drizzled/server_includes.h>
21
#include CSTDINT_H
22
#include <drizzled/show.h>
23
#include <drizzled/table.h>
670.2.1 by Monty Taylor
Moved pthread keys
24
#include <drizzled/current_session.h>
642.1.1 by Lee
move functions from item.cc/item.h to item directory
25
#include <drizzled/item/ident.h>
26
820.3.25 by Padraig
Updated the fix for Bug#319796.
27
using namespace std;
28
642.1.1 by Lee
move functions from item.cc/item.h to item directory
29
const uint32_t NO_CACHED_FIELD_INDEX= UINT32_MAX;
30
31
Item_ident::Item_ident(Name_resolution_context *context_arg,
32
                       const char *db_name_arg,const char *table_name_arg,
33
                       const char *field_name_arg)
34
  :orig_db_name(db_name_arg), orig_table_name(table_name_arg),
35
   orig_field_name(field_name_arg), context(context_arg),
36
   db_name(db_name_arg), table_name(table_name_arg),
37
   field_name(field_name_arg),
38
   alias_name_used(false), cached_field_index(NO_CACHED_FIELD_INDEX),
39
   cached_table(0), depended_from(0)
40
{
41
  name = (char*) field_name_arg;
42
}
43
44
/**
45
  Constructor used by Item_field & Item_*_ref (see Item comment)
46
*/
47
48
Item_ident::Item_ident(Session *session, Item_ident *item)
49
  :Item(session, item),
50
   orig_db_name(item->orig_db_name),
51
   orig_table_name(item->orig_table_name),
52
   orig_field_name(item->orig_field_name),
53
   context(item->context),
54
   db_name(item->db_name),
55
   table_name(item->table_name),
56
   field_name(item->field_name),
57
   alias_name_used(item->alias_name_used),
58
   cached_field_index(item->cached_field_index),
59
   cached_table(item->cached_table),
60
   depended_from(item->depended_from)
61
{}
62
63
void Item_ident::cleanup()
64
{
65
#ifdef CANT_BE_USED_AS_MEMORY_IS_FREED
66
                       db_name ? db_name : "(null)",
67
                       orig_db_name ? orig_db_name : "(null)",
68
                       table_name ? table_name : "(null)",
69
                       orig_table_name ? orig_table_name : "(null)",
70
                       field_name ? field_name : "(null)",
71
                       orig_field_name ? orig_field_name : "(null)"));
72
#endif
73
  Item::cleanup();
74
  db_name= orig_db_name;
75
  table_name= orig_table_name;
76
  field_name= orig_field_name;
77
  depended_from= 0;
78
  return;
79
}
80
81
bool Item_ident::remove_dependence_processor(unsigned char * arg)
82
{
846 by Brian Aker
Removing on typedeffed class.
83
  if (depended_from == (Select_Lex *) arg)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
84
    depended_from= 0;
85
  return(0);
86
}
87
88
const char *Item_ident::full_name() const
89
{
90
  char *tmp;
91
  if (!table_name || !field_name)
92
    return field_name ? field_name : name ? name : "tmp_field";
93
  if (db_name && db_name[0])
94
  {
895 by Brian Aker
Completion (?) of uint conversion.
95
    tmp=(char*) sql_alloc((uint32_t) strlen(db_name)+(uint32_t) strlen(table_name)+
96
                          (uint32_t) strlen(field_name)+3);
673.2.2 by Toru Maesaka
Final pass of replacing MySQL's strxmov with libc's alternatives
97
    sprintf(tmp,"%s.%s.%s",db_name,table_name,field_name);
642.1.1 by Lee
move functions from item.cc/item.h to item directory
98
  }
99
  else
100
  {
101
    if (table_name[0])
102
    {
895 by Brian Aker
Completion (?) of uint conversion.
103
      tmp= (char*) sql_alloc((uint32_t) strlen(table_name) +
104
                             (uint32_t) strlen(field_name) + 2);
673.2.2 by Toru Maesaka
Final pass of replacing MySQL's strxmov with libc's alternatives
105
      sprintf(tmp, "%s.%s", table_name, field_name);
642.1.1 by Lee
move functions from item.cc/item.h to item directory
106
    }
107
    else
108
      tmp= (char*) field_name;
109
  }
110
  return tmp;
111
}
112
113
114
void Item_ident::print(String *str,
115
                       enum_query_type)
116
{
873.2.29 by Monty Taylor
Shortened a small chunk of code.
117
  string d_name, t_name;
820.3.26 by Padraig
Added extra checks when creating strings. Need to make sure that the content
118
873.2.29 by Monty Taylor
Shortened a small chunk of code.
119
  if (table_name && table_name[0])
120
  {
121
    t_name.assign(table_name);
1039.1.5 by Brian Aker
Remove lower case filename bits (aka we just lock into the most compatible
122
    std::transform(t_name.begin(), t_name.end(),
123
                   t_name.begin(), ::tolower);
873.2.29 by Monty Taylor
Shortened a small chunk of code.
124
  }
125
 
126
  if (db_name && db_name[0])
127
  {
128
    d_name.assign(db_name);
1039.1.5 by Brian Aker
Remove lower case filename bits (aka we just lock into the most compatible
129
    // Keeping the std:: prefix here, since Item_ident has a transform
130
    // method
873.2.29 by Monty Taylor
Shortened a small chunk of code.
131
      std::transform(d_name.begin(), d_name.end(),
132
                     d_name.begin(), ::tolower);
642.1.1 by Lee
move functions from item.cc/item.h to item directory
133
  }
134
135
  if (!table_name || !field_name || !field_name[0])
136
  {
137
    const char *nm= (field_name && field_name[0]) ?
138
                      field_name : name ? name : "tmp_field";
895 by Brian Aker
Completion (?) of uint conversion.
139
    str->append_identifier(nm, (uint32_t) strlen(nm));
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
140
642.1.1 by Lee
move functions from item.cc/item.h to item directory
141
    return;
142
  }
143
  if (db_name && db_name[0] && !alias_name_used)
144
  {
145
    {
873.2.29 by Monty Taylor
Shortened a small chunk of code.
146
      str->append_identifier(d_name.c_str(), d_name.length());
642.1.1 by Lee
move functions from item.cc/item.h to item directory
147
      str->append('.');
148
    }
873.2.29 by Monty Taylor
Shortened a small chunk of code.
149
    str->append_identifier(t_name.c_str(), t_name.length());
642.1.1 by Lee
move functions from item.cc/item.h to item directory
150
    str->append('.');
895 by Brian Aker
Completion (?) of uint conversion.
151
    str->append_identifier(field_name, (uint32_t)strlen(field_name));
642.1.1 by Lee
move functions from item.cc/item.h to item directory
152
  }
153
  else
154
  {
155
    if (table_name[0])
156
    {
873.2.29 by Monty Taylor
Shortened a small chunk of code.
157
      str->append_identifier(t_name.c_str(), t_name.length());
642.1.1 by Lee
move functions from item.cc/item.h to item directory
158
      str->append('.');
895 by Brian Aker
Completion (?) of uint conversion.
159
      str->append_identifier(field_name, (uint32_t) strlen(field_name));
642.1.1 by Lee
move functions from item.cc/item.h to item directory
160
    }
161
    else
895 by Brian Aker
Completion (?) of uint conversion.
162
      str->append_identifier(field_name, (uint32_t) strlen(field_name));
642.1.1 by Lee
move functions from item.cc/item.h to item directory
163
  }
164
}
165
166
double Item_ident_for_show::val_real()
167
{
168
  return field->val_real();
169
}
170
171
172
int64_t Item_ident_for_show::val_int()
173
{
174
  return field->val_int();
175
}
176
177
178
String *Item_ident_for_show::val_str(String *str)
179
{
180
  return field->val_str(str);
181
}
182
183
184
my_decimal *Item_ident_for_show::val_decimal(my_decimal *dec)
185
{
186
  return field->val_decimal(dec);
187
}
188
1052.2.4 by Nathan Williams
No actual code changes. Changed Send_field to SendField to be consistent with coding standards.
189
void Item_ident_for_show::make_field(SendField *tmp_field)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
190
{
191
  tmp_field->table_name= tmp_field->org_table_name= table_name;
192
  tmp_field->db_name= db_name;
193
  tmp_field->col_name= tmp_field->org_col_name= field->field_name;
194
  tmp_field->charsetnr= field->charset()->number;
195
  tmp_field->length=field->field_length;
196
  tmp_field->type=field->type();
197
  tmp_field->flags= field->table->maybe_null ?
198
    (field->flags & ~NOT_NULL_FLAG) : field->flags;
199
  tmp_field->decimals= field->decimals();
200
}
201