~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>
#include <drizzled/show.h>
#include <drizzled/table.h>
#include <drizzled/item/ident.h>

#include <cstdio>

using namespace std;

namespace drizzled
{

const uint32_t NO_CACHED_FIELD_INDEX= UINT32_MAX;

Item_ident::Item_ident(Name_resolution_context *context_arg,
                       const char *db_name_arg,const char *table_name_arg,
                       const char *field_name_arg)
  :orig_db_name(db_name_arg), orig_table_name(table_name_arg),
   orig_field_name(field_name_arg), context(context_arg),
   db_name(db_name_arg), table_name(table_name_arg),
   field_name(field_name_arg),
   alias_name_used(false), cached_field_index(NO_CACHED_FIELD_INDEX),
   cached_table(0), depended_from(0)
{
  name = (char*) field_name_arg;
}

/**
  Constructor used by Item_field & Item_*_ref (see Item comment)
*/

Item_ident::Item_ident(Session *session, Item_ident *item)
  :Item(session, item),
   orig_db_name(item->orig_db_name),
   orig_table_name(item->orig_table_name),
   orig_field_name(item->orig_field_name),
   context(item->context),
   db_name(item->db_name),
   table_name(item->table_name),
   field_name(item->field_name),
   alias_name_used(item->alias_name_used),
   cached_field_index(item->cached_field_index),
   cached_table(item->cached_table),
   depended_from(item->depended_from)
{}

void Item_ident::cleanup()
{
  Item::cleanup();
  db_name= orig_db_name;
  table_name= orig_table_name;
  field_name= orig_field_name;
  depended_from= 0;
  return;
}

bool Item_ident::remove_dependence_processor(unsigned char * arg)
{
  if (depended_from == (Select_Lex *) arg)
    depended_from= 0;
  return(0);
}

const char *Item_ident::full_name() const
{
  char *tmp;
	size_t tmp_len;
  if (!table_name || !field_name)
    return field_name ? field_name : name ? name : "tmp_field";
  if (db_name && db_name[0])
  {
    tmp_len= strlen(db_name)+strlen(table_name)+strlen(field_name)+3;
    tmp= (char*) memory::sql_alloc(tmp_len);
    snprintf(tmp, tmp_len, "%s.%s.%s",db_name,table_name,field_name);
  }
  else
  {
    if (table_name[0])
    {
      tmp_len=strlen(table_name)+strlen(field_name)+2;
      tmp= (char*) memory::sql_alloc(tmp_len);
      snprintf(tmp, tmp_len, "%s.%s", table_name, field_name);
    }
    else
      tmp= (char*) field_name;
  }
  return tmp;
}


void Item_ident::print(String *str)
{
  string d_name, t_name;

  if (table_name && table_name[0])
  {
    t_name.assign(table_name);
    std::transform(t_name.begin(), t_name.end(),
                   t_name.begin(), ::tolower);
  }
 
  if (db_name && db_name[0])
  {
    d_name.assign(db_name);
    // Keeping the std:: prefix here, since Item_ident has a transform
    // method
      std::transform(d_name.begin(), d_name.end(),
                     d_name.begin(), ::tolower);
  }

  if (!table_name || !field_name || !field_name[0])
  {
    const char *nm= (field_name && field_name[0]) ?
                      field_name : name ? name : "tmp_field";
    str->append_identifier(nm, (uint32_t) strlen(nm));

    return;
  }
  if (db_name && db_name[0] && !alias_name_used)
  {
    {
      str->append_identifier(d_name.c_str(), d_name.length());
      str->append('.');
    }
    str->append_identifier(t_name.c_str(), t_name.length());
    str->append('.');
    str->append_identifier(field_name, (uint32_t)strlen(field_name));
  }
  else
  {
    if (table_name[0])
    {
      str->append_identifier(t_name.c_str(), t_name.length());
      str->append('.');
      str->append_identifier(field_name, (uint32_t) strlen(field_name));
    }
    else
      str->append_identifier(field_name, (uint32_t) strlen(field_name));
  }
}

double Item_ident_for_show::val_real()
{
  return field->val_real();
}


int64_t Item_ident_for_show::val_int()
{
  return field->val_int();
}


String *Item_ident_for_show::val_str(String *str)
{
  return field->val_str_internal(str);
}


type::Decimal *Item_ident_for_show::val_decimal(type::Decimal *dec)
{
  return field->val_decimal(dec);
}

void Item_ident_for_show::make_field(SendField *tmp_field)
{
  tmp_field->table_name= tmp_field->org_table_name= table_name;
  tmp_field->db_name= db_name;
  tmp_field->col_name= tmp_field->org_col_name= field->field_name;
  tmp_field->charsetnr= field->charset()->number;
  tmp_field->length=field->field_length;
  tmp_field->type=field->type();
  tmp_field->flags= field->getTable()->maybe_null ?
    (field->flags & ~NOT_NULL_FLAG) : field->flags;
  tmp_field->decimals= field->decimals();
}

} /* namespace drizzled */