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 
00032 const uint32_t NO_CACHED_FIELD_INDEX= UINT32_MAX;
00033 
00034 Item_ident::Item_ident(Name_resolution_context *context_arg,
00035                        const char *db_name_arg,const char *table_name_arg,
00036                        const char *field_name_arg)
00037   :orig_db_name(db_name_arg), orig_table_name(table_name_arg),
00038    orig_field_name(field_name_arg), context(context_arg),
00039    db_name(db_name_arg), table_name(table_name_arg),
00040    field_name(field_name_arg),
00041    alias_name_used(false), cached_field_index(NO_CACHED_FIELD_INDEX),
00042    cached_table(0), depended_from(0)
00043 {
00044   name = (char*) field_name_arg;
00045 }
00046 
00051 Item_ident::Item_ident(Session *session, Item_ident *item)
00052   :Item(session, item),
00053    orig_db_name(item->orig_db_name),
00054    orig_table_name(item->orig_table_name),
00055    orig_field_name(item->orig_field_name),
00056    context(item->context),
00057    db_name(item->db_name),
00058    table_name(item->table_name),
00059    field_name(item->field_name),
00060    alias_name_used(item->alias_name_used),
00061    cached_field_index(item->cached_field_index),
00062    cached_table(item->cached_table),
00063    depended_from(item->depended_from)
00064 {}
00065 
00066 void Item_ident::cleanup()
00067 {
00068   Item::cleanup();
00069   db_name= orig_db_name;
00070   table_name= orig_table_name;
00071   field_name= orig_field_name;
00072   depended_from= 0;
00073   return;
00074 }
00075 
00076 bool Item_ident::remove_dependence_processor(unsigned char * arg)
00077 {
00078   if (depended_from == (Select_Lex *) arg)
00079     depended_from= 0;
00080   return(0);
00081 }
00082 
00083 const char *Item_ident::full_name() const
00084 {
00085   char *tmp;
00086   size_t tmp_len;
00087   if (!table_name || !field_name)
00088     return field_name ? field_name : name ? name : "tmp_field";
00089   if (db_name && db_name[0])
00090   {
00091     tmp_len= strlen(db_name)+strlen(table_name)+strlen(field_name)+3;
00092     tmp= (char*) memory::sql_alloc(tmp_len);
00093     snprintf(tmp, tmp_len, "%s.%s.%s",db_name,table_name,field_name);
00094   }
00095   else
00096   {
00097     if (table_name[0])
00098     {
00099       tmp_len=strlen(table_name)+strlen(field_name)+2;
00100       tmp= (char*) memory::sql_alloc(tmp_len);
00101       snprintf(tmp, tmp_len, "%s.%s", table_name, field_name);
00102     }
00103     else
00104       tmp= (char*) field_name;
00105   }
00106   return tmp;
00107 }
00108 
00109 
00110 void Item_ident::print(String *str)
00111 {
00112   string d_name, t_name;
00113 
00114   if (table_name && table_name[0])
00115   {
00116     t_name.assign(table_name);
00117     std::transform(t_name.begin(), t_name.end(),
00118                    t_name.begin(), ::tolower);
00119   }
00120  
00121   if (db_name && db_name[0])
00122   {
00123     d_name.assign(db_name);
00124     // Keeping the std:: prefix here, since Item_ident has a transform
00125     // method
00126       std::transform(d_name.begin(), d_name.end(),
00127                      d_name.begin(), ::tolower);
00128   }
00129 
00130   if (!table_name || !field_name || !field_name[0])
00131   {
00132     const char *nm= (field_name && field_name[0]) ?
00133                       field_name : name ? name : "tmp_field";
00134     str->append_identifier(nm, (uint32_t) strlen(nm));
00135 
00136     return;
00137   }
00138   if (db_name && db_name[0] && !alias_name_used)
00139   {
00140     {
00141       str->append_identifier(d_name.c_str(), d_name.length());
00142       str->append('.');
00143     }
00144     str->append_identifier(t_name.c_str(), t_name.length());
00145     str->append('.');
00146     str->append_identifier(field_name, (uint32_t)strlen(field_name));
00147   }
00148   else
00149   {
00150     if (table_name[0])
00151     {
00152       str->append_identifier(t_name.c_str(), t_name.length());
00153       str->append('.');
00154       str->append_identifier(field_name, (uint32_t) strlen(field_name));
00155     }
00156     else
00157       str->append_identifier(field_name, (uint32_t) strlen(field_name));
00158   }
00159 }
00160 
00161 double Item_ident_for_show::val_real()
00162 {
00163   return field->val_real();
00164 }
00165 
00166 
00167 int64_t Item_ident_for_show::val_int()
00168 {
00169   return field->val_int();
00170 }
00171 
00172 
00173 String *Item_ident_for_show::val_str(String *str)
00174 {
00175   return field->val_str_internal(str);
00176 }
00177 
00178 
00179 type::Decimal *Item_ident_for_show::val_decimal(type::Decimal *dec)
00180 {
00181   return field->val_decimal(dec);
00182 }
00183 
00184 void Item_ident_for_show::make_field(SendField *tmp_field)
00185 {
00186   tmp_field->table_name= tmp_field->org_table_name= table_name;
00187   tmp_field->db_name= db_name;
00188   tmp_field->col_name= tmp_field->org_col_name= field->field_name;
00189   tmp_field->charsetnr= field->charset()->number;
00190   tmp_field->length=field->field_length;
00191   tmp_field->type=field->type();
00192   tmp_field->flags= field->getTable()->maybe_null ?
00193     (field->flags & ~NOT_NULL_FLAG) : field->flags;
00194   tmp_field->decimals= field->decimals();
00195 }
00196 
00197 } /* namespace drizzled */