~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/ident.cc

Cleanup around SAFEMALLOC

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
 
#include "config.h"
21
 
#include <drizzled/show.h>
22
 
#include <drizzled/table.h>
23
 
#include <drizzled/current_session.h>
24
 
#include <drizzled/item/ident.h>
25
 
 
26
 
using namespace std;
27
 
 
28
 
namespace drizzled
29
 
{
30
 
 
31
 
const uint32_t NO_CACHED_FIELD_INDEX= UINT32_MAX;
32
 
 
33
 
Item_ident::Item_ident(Name_resolution_context *context_arg,
34
 
                       const char *db_name_arg,const char *table_name_arg,
35
 
                       const char *field_name_arg)
36
 
  :orig_db_name(db_name_arg), orig_table_name(table_name_arg),
37
 
   orig_field_name(field_name_arg), context(context_arg),
38
 
   db_name(db_name_arg), table_name(table_name_arg),
39
 
   field_name(field_name_arg),
40
 
   alias_name_used(false), cached_field_index(NO_CACHED_FIELD_INDEX),
41
 
   cached_table(0), depended_from(0)
42
 
{
43
 
  name = (char*) field_name_arg;
44
 
}
45
 
 
46
 
/**
47
 
  Constructor used by Item_field & Item_*_ref (see Item comment)
48
 
*/
49
 
 
50
 
Item_ident::Item_ident(Session *session, Item_ident *item)
51
 
  :Item(session, item),
52
 
   orig_db_name(item->orig_db_name),
53
 
   orig_table_name(item->orig_table_name),
54
 
   orig_field_name(item->orig_field_name),
55
 
   context(item->context),
56
 
   db_name(item->db_name),
57
 
   table_name(item->table_name),
58
 
   field_name(item->field_name),
59
 
   alias_name_used(item->alias_name_used),
60
 
   cached_field_index(item->cached_field_index),
61
 
   cached_table(item->cached_table),
62
 
   depended_from(item->depended_from)
63
 
{}
64
 
 
65
 
void Item_ident::cleanup()
66
 
{
67
 
  Item::cleanup();
68
 
  db_name= orig_db_name;
69
 
  table_name= orig_table_name;
70
 
  field_name= orig_field_name;
71
 
  depended_from= 0;
72
 
  return;
73
 
}
74
 
 
75
 
bool Item_ident::remove_dependence_processor(unsigned char * arg)
76
 
{
77
 
  if (depended_from == (Select_Lex *) arg)
78
 
    depended_from= 0;
79
 
  return(0);
80
 
}
81
 
 
82
 
const char *Item_ident::full_name() const
83
 
{
84
 
  char *tmp;
85
 
        size_t tmp_len;
86
 
  if (!table_name || !field_name)
87
 
    return field_name ? field_name : name ? name : "tmp_field";
88
 
  if (db_name && db_name[0])
89
 
  {
90
 
    tmp_len= strlen(db_name)+strlen(table_name)+strlen(field_name)+3;
91
 
    tmp= (char*) memory::sql_alloc(tmp_len);
92
 
    snprintf(tmp, tmp_len, "%s.%s.%s",db_name,table_name,field_name);
93
 
  }
94
 
  else
95
 
  {
96
 
    if (table_name[0])
97
 
    {
98
 
      tmp_len=strlen(table_name)+strlen(field_name)+2;
99
 
      tmp= (char*) memory::sql_alloc(tmp_len);
100
 
      snprintf(tmp, tmp_len, "%s.%s", table_name, field_name);
101
 
    }
102
 
    else
103
 
      tmp= (char*) field_name;
104
 
  }
105
 
  return tmp;
106
 
}
107
 
 
108
 
 
109
 
void Item_ident::print(String *str,
110
 
                       enum_query_type)
111
 
{
112
 
  string d_name, t_name;
113
 
 
114
 
  if (table_name && table_name[0])
115
 
  {
116
 
    t_name.assign(table_name);
117
 
    std::transform(t_name.begin(), t_name.end(),
118
 
                   t_name.begin(), ::tolower);
119
 
  }
120
 
 
121
 
  if (db_name && db_name[0])
122
 
  {
123
 
    d_name.assign(db_name);
124
 
    // Keeping the std:: prefix here, since Item_ident has a transform
125
 
    // method
126
 
      std::transform(d_name.begin(), d_name.end(),
127
 
                     d_name.begin(), ::tolower);
128
 
  }
129
 
 
130
 
  if (!table_name || !field_name || !field_name[0])
131
 
  {
132
 
    const char *nm= (field_name && field_name[0]) ?
133
 
                      field_name : name ? name : "tmp_field";
134
 
    str->append_identifier(nm, (uint32_t) strlen(nm));
135
 
 
136
 
    return;
137
 
  }
138
 
  if (db_name && db_name[0] && !alias_name_used)
139
 
  {
140
 
    {
141
 
      str->append_identifier(d_name.c_str(), d_name.length());
142
 
      str->append('.');
143
 
    }
144
 
    str->append_identifier(t_name.c_str(), t_name.length());
145
 
    str->append('.');
146
 
    str->append_identifier(field_name, (uint32_t)strlen(field_name));
147
 
  }
148
 
  else
149
 
  {
150
 
    if (table_name[0])
151
 
    {
152
 
      str->append_identifier(t_name.c_str(), t_name.length());
153
 
      str->append('.');
154
 
      str->append_identifier(field_name, (uint32_t) strlen(field_name));
155
 
    }
156
 
    else
157
 
      str->append_identifier(field_name, (uint32_t) strlen(field_name));
158
 
  }
159
 
}
160
 
 
161
 
double Item_ident_for_show::val_real()
162
 
{
163
 
  return field->val_real();
164
 
}
165
 
 
166
 
 
167
 
int64_t Item_ident_for_show::val_int()
168
 
{
169
 
  return field->val_int();
170
 
}
171
 
 
172
 
 
173
 
String *Item_ident_for_show::val_str(String *str)
174
 
{
175
 
  return field->val_str(str);
176
 
}
177
 
 
178
 
 
179
 
my_decimal *Item_ident_for_show::val_decimal(my_decimal *dec)
180
 
{
181
 
  return field->val_decimal(dec);
182
 
}
183
 
 
184
 
void Item_ident_for_show::make_field(SendField *tmp_field)
185
 
{
186
 
  tmp_field->table_name= tmp_field->org_table_name= table_name;
187
 
  tmp_field->db_name= db_name;
188
 
  tmp_field->col_name= tmp_field->org_col_name= field->field_name;
189
 
  tmp_field->charsetnr= field->charset()->number;
190
 
  tmp_field->length=field->field_length;
191
 
  tmp_field->type=field->type();
192
 
  tmp_field->flags= field->table->maybe_null ?
193
 
    (field->flags & ~NOT_NULL_FLAG) : field->flags;
194
 
  tmp_field->decimals= field->decimals();
195
 
}
196
 
 
197
 
} /* namespace drizzled */