~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
  Item::cleanup();
66
  db_name= orig_db_name;
67
  table_name= orig_table_name;
68
  field_name= orig_field_name;
69
  depended_from= 0;
70
  return;
71
}
72
73
bool Item_ident::remove_dependence_processor(unsigned char * arg)
74
{
846 by Brian Aker
Removing on typedeffed class.
75
  if (depended_from == (Select_Lex *) arg)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
76
    depended_from= 0;
77
  return(0);
78
}
79
80
const char *Item_ident::full_name() const
81
{
82
  char *tmp;
83
  if (!table_name || !field_name)
84
    return field_name ? field_name : name ? name : "tmp_field";
85
  if (db_name && db_name[0])
86
  {
895 by Brian Aker
Completion (?) of uint conversion.
87
    tmp=(char*) sql_alloc((uint32_t) strlen(db_name)+(uint32_t) strlen(table_name)+
88
                          (uint32_t) strlen(field_name)+3);
673.2.2 by Toru Maesaka
Final pass of replacing MySQL's strxmov with libc's alternatives
89
    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
90
  }
91
  else
92
  {
93
    if (table_name[0])
94
    {
895 by Brian Aker
Completion (?) of uint conversion.
95
      tmp= (char*) sql_alloc((uint32_t) strlen(table_name) +
96
                             (uint32_t) strlen(field_name) + 2);
673.2.2 by Toru Maesaka
Final pass of replacing MySQL's strxmov with libc's alternatives
97
      sprintf(tmp, "%s.%s", table_name, field_name);
642.1.1 by Lee
move functions from item.cc/item.h to item directory
98
    }
99
    else
100
      tmp= (char*) field_name;
101
  }
102
  return tmp;
103
}
104
105
106
void Item_ident::print(String *str,
107
                       enum_query_type)
108
{
873.2.29 by Monty Taylor
Shortened a small chunk of code.
109
  string d_name, t_name;
820.3.26 by Padraig
Added extra checks when creating strings. Need to make sure that the content
110
873.2.29 by Monty Taylor
Shortened a small chunk of code.
111
  if (table_name && table_name[0])
112
  {
113
    t_name.assign(table_name);
1039.1.5 by Brian Aker
Remove lower case filename bits (aka we just lock into the most compatible
114
    std::transform(t_name.begin(), t_name.end(),
115
                   t_name.begin(), ::tolower);
873.2.29 by Monty Taylor
Shortened a small chunk of code.
116
  }
117
 
118
  if (db_name && db_name[0])
119
  {
120
    d_name.assign(db_name);
1039.1.5 by Brian Aker
Remove lower case filename bits (aka we just lock into the most compatible
121
    // Keeping the std:: prefix here, since Item_ident has a transform
122
    // method
873.2.29 by Monty Taylor
Shortened a small chunk of code.
123
      std::transform(d_name.begin(), d_name.end(),
124
                     d_name.begin(), ::tolower);
642.1.1 by Lee
move functions from item.cc/item.h to item directory
125
  }
126
127
  if (!table_name || !field_name || !field_name[0])
128
  {
129
    const char *nm= (field_name && field_name[0]) ?
130
                      field_name : name ? name : "tmp_field";
895 by Brian Aker
Completion (?) of uint conversion.
131
    str->append_identifier(nm, (uint32_t) strlen(nm));
794 by Brian Aker
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
132
642.1.1 by Lee
move functions from item.cc/item.h to item directory
133
    return;
134
  }
135
  if (db_name && db_name[0] && !alias_name_used)
136
  {
137
    {
873.2.29 by Monty Taylor
Shortened a small chunk of code.
138
      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
139
      str->append('.');
140
    }
873.2.29 by Monty Taylor
Shortened a small chunk of code.
141
    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
142
    str->append('.');
895 by Brian Aker
Completion (?) of uint conversion.
143
    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
144
  }
145
  else
146
  {
147
    if (table_name[0])
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
895 by Brian Aker
Completion (?) of uint conversion.
154
      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
155
  }
156
}
157
158
double Item_ident_for_show::val_real()
159
{
160
  return field->val_real();
161
}
162
163
164
int64_t Item_ident_for_show::val_int()
165
{
166
  return field->val_int();
167
}
168
169
170
String *Item_ident_for_show::val_str(String *str)
171
{
172
  return field->val_str(str);
173
}
174
175
176
my_decimal *Item_ident_for_show::val_decimal(my_decimal *dec)
177
{
178
  return field->val_decimal(dec);
179
}
180
1052.2.4 by Nathan Williams
No actual code changes. Changed Send_field to SendField to be consistent with coding standards.
181
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
182
{
183
  tmp_field->table_name= tmp_field->org_table_name= table_name;
184
  tmp_field->db_name= db_name;
185
  tmp_field->col_name= tmp_field->org_col_name= field->field_name;
186
  tmp_field->charsetnr= field->charset()->number;
187
  tmp_field->length=field->field_length;
188
  tmp_field->type=field->type();
189
  tmp_field->flags= field->table->maybe_null ?
190
    (field->flags & ~NOT_NULL_FLAG) : field->flags;
191
  tmp_field->decimals= field->decimals();
192
}
193