Drizzled Public API Documentation

ident.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 #include <drizzled/show.h>
00022 #include <drizzled/table.h>
00023 #include <drizzled/item/ident.h>
00024 
00025 #include <cstdio>
00026 
00027 using namespace std;
00028 
00029 namespace drizzled {
00030 
00031 const uint32_t NO_CACHED_FIELD_INDEX= UINT32_MAX;
00032 
00033 Item_ident::Item_ident(Name_resolution_context *context_arg,
00034                        const char *db_name_arg,const char *table_name_arg,
00035                        const char *field_name_arg)
00036   :orig_db_name(db_name_arg), orig_table_name(table_name_arg),
00037    orig_field_name(field_name_arg), context(context_arg),
00038    db_name(db_name_arg), table_name(table_name_arg),
00039    field_name(field_name_arg),
00040    alias_name_used(false), cached_field_index(NO_CACHED_FIELD_INDEX),
00041    cached_table(0), depended_from(0)
00042 {
00043   name = field_name_arg;
00044 }
00045 
00050 Item_ident::Item_ident(Session *session, Item_ident *item)
00051   :Item(session, item),
00052    orig_db_name(item->orig_db_name),
00053    orig_table_name(item->orig_table_name),
00054    orig_field_name(item->orig_field_name),
00055    context(item->context),
00056    db_name(item->db_name),
00057    table_name(item->table_name),
00058    field_name(item->field_name),
00059    alias_name_used(item->alias_name_used),
00060    cached_field_index(item->cached_field_index),
00061    cached_table(item->cached_table),
00062    depended_from(item->depended_from)
00063 {}
00064 
00065 void Item_ident::cleanup()
00066 {
00067   Item::cleanup();
00068   db_name= orig_db_name;
00069   table_name= orig_table_name;
00070   field_name= orig_field_name;
00071   depended_from= 0;
00072 }
00073 
00074 bool Item_ident::remove_dependence_processor(unsigned char * arg)
00075 {
00076   if (depended_from == (Select_Lex *) arg)
00077     depended_from= 0;
00078   return 0;
00079 }
00080 
00081 const char *Item_ident::full_name() const
00082 {
00083   if (!table_name || !field_name)
00084     return field_name ? field_name : name ? name : "tmp_field";
00085   if (db_name && db_name[0])
00086   {
00087     size_t tmp_len= strlen(db_name)+strlen(table_name)+strlen(field_name)+3;
00088     char* tmp= (char*) memory::sql_alloc(tmp_len);
00089     snprintf(tmp, tmp_len, "%s.%s.%s",db_name,table_name,field_name);
00090     return tmp;
00091   }
00092   if (table_name[0])
00093   {
00094     size_t tmp_len= strlen(table_name)+strlen(field_name)+2;
00095     char* tmp= (char*) memory::sql_alloc(tmp_len);
00096     snprintf(tmp, tmp_len, "%s.%s", table_name, field_name);
00097     return tmp;
00098   }
00099   return field_name;
00100 }
00101 
00102 
00103 void Item_ident::print(String *str)
00104 {
00105   string d_name, t_name;
00106 
00107   if (table_name && table_name[0])
00108   {
00109     t_name= table_name;
00110     boost::to_lower(t_name);
00111   }
00112  
00113   if (db_name && db_name[0])
00114   {
00115     d_name= db_name;
00116     boost::to_lower(d_name);
00117   }
00118 
00119   if (!table_name || !field_name || !field_name[0])
00120   {
00121     str->append_identifier(str_ref((field_name && field_name[0]) ? field_name : name ? name : "tmp_field"));
00122     return;
00123   }
00124   if (db_name && db_name[0] && !alias_name_used)
00125   {
00126     str->append_identifier(d_name);
00127     str->append('.');
00128     str->append_identifier(t_name);
00129     str->append('.');
00130     str->append_identifier(str_ref(field_name));
00131   }
00132   else
00133   {
00134     if (table_name[0])
00135     {
00136       str->append_identifier(t_name);
00137       str->append('.');
00138       str->append_identifier(str_ref(field_name));
00139     }
00140     else
00141       str->append_identifier(str_ref(field_name));
00142   }
00143 }
00144 
00145 double Item_ident_for_show::val_real()
00146 {
00147   return field->val_real();
00148 }
00149 
00150 
00151 int64_t Item_ident_for_show::val_int()
00152 {
00153   return field->val_int();
00154 }
00155 
00156 
00157 String *Item_ident_for_show::val_str(String *str)
00158 {
00159   return field->val_str_internal(str);
00160 }
00161 
00162 
00163 type::Decimal *Item_ident_for_show::val_decimal(type::Decimal *dec)
00164 {
00165   return field->val_decimal(dec);
00166 }
00167 
00168 void Item_ident_for_show::make_field(SendField *tmp_field)
00169 {
00170   tmp_field->table_name= tmp_field->org_table_name= table_name;
00171   tmp_field->db_name= db_name;
00172   tmp_field->col_name= tmp_field->org_col_name= field->field_name;
00173   tmp_field->charsetnr= field->charset()->number;
00174   tmp_field->length=field->field_length;
00175   tmp_field->type=field->type();
00176   tmp_field->flags= field->getTable()->maybe_null ? (field->flags & ~NOT_NULL_FLAG) : field->flags;
00177   tmp_field->decimals= field->decimals();
00178 }
00179 
00180 } /* namespace drizzled */