Drizzled Public API Documentation

ident.cc
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, Inc.
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/item/ident.h>
24 
25 #include <cstdio>
26 
27 using namespace std;
28 
29 namespace drizzled {
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 = field_name_arg;
44 }
45 
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 }
73 
74 bool Item_ident::remove_dependence_processor(unsigned char * arg)
75 {
76  if (depended_from == (Select_Lex *) arg)
77  depended_from= 0;
78  return 0;
79 }
80 
81 const char *Item_ident::full_name() const
82 {
83  if (!table_name || !field_name)
84  return field_name ? field_name : name ? name : "tmp_field";
85  if (db_name && db_name[0])
86  {
87  size_t tmp_len= strlen(db_name)+strlen(table_name)+strlen(field_name)+3;
88  char* tmp= (char*) memory::sql_alloc(tmp_len);
89  snprintf(tmp, tmp_len, "%s.%s.%s",db_name,table_name,field_name);
90  return tmp;
91  }
92  if (table_name[0])
93  {
94  size_t tmp_len= strlen(table_name)+strlen(field_name)+2;
95  char* tmp= (char*) memory::sql_alloc(tmp_len);
96  snprintf(tmp, tmp_len, "%s.%s", table_name, field_name);
97  return tmp;
98  }
99  return field_name;
100 }
101 
102 
104 {
105  string d_name, t_name;
106 
107  if (table_name && table_name[0])
108  {
109  t_name= table_name;
110  boost::to_lower(t_name);
111  }
112 
113  if (db_name && db_name[0])
114  {
115  d_name= db_name;
116  boost::to_lower(d_name);
117  }
118 
119  if (!table_name || !field_name || !field_name[0])
120  {
121  str->append_identifier(str_ref((field_name && field_name[0]) ? field_name : name ? name : "tmp_field"));
122  return;
123  }
124  if (db_name && db_name[0] && !alias_name_used)
125  {
126  str->append_identifier(d_name);
127  str->append('.');
128  str->append_identifier(t_name);
129  str->append('.');
130  str->append_identifier(str_ref(field_name));
131  }
132  else
133  {
134  if (table_name[0])
135  {
136  str->append_identifier(t_name);
137  str->append('.');
138  str->append_identifier(str_ref(field_name));
139  }
140  else
141  str->append_identifier(str_ref(field_name));
142  }
143 }
144 
146 {
147  return field->val_real();
148 }
149 
150 
152 {
153  return field->val_int();
154 }
155 
156 
158 {
159  return field->val_str_internal(str);
160 }
161 
162 
164 {
165  return field->val_decimal(dec);
166 }
167 
168 void Item_ident_for_show::make_field(SendField *tmp_field)
169 {
170  tmp_field->table_name= tmp_field->org_table_name= table_name;
171  tmp_field->db_name= db_name;
172  tmp_field->col_name= tmp_field->org_col_name= field->field_name;
173  tmp_field->charsetnr= field->charset()->number;
174  tmp_field->length=field->field_length;
175  tmp_field->type=field->type();
176  tmp_field->flags= field->getTable()->maybe_null ? (field->flags & ~NOT_NULL_FLAG) : field->flags;
177  tmp_field->decimals= field->decimals();
178 }
179 
180 } /* namespace drizzled */
const char * name
Definition: item.h:110
uint32_t field_length
Definition: field.h:129
virtual void print(String *str)
Definition: ident.cc:103
type::Decimal * val_decimal(type::Decimal *dec)
Definition: ident.cc:163
String * val_str(String *str)
Definition: ident.cc:157
const char * field_name
Definition: field.h:102