void Item_ident::print(String *str, enum_query_type query_type)
{
  THD *thd= current_thd;
  char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
  const char *d_name= db_name, *t_name= table_name;
  bool use_table_name= table_name && table_name[0];
  bool use_db_name= use_table_name && db_name && db_name[0] && !alias_name_used;

  if (use_db_name && (query_type & QT_ITEM_IDENT_SKIP_DB_NAMES))
    use_db_name= !thd->db.str || strcmp(thd->db.str, db_name);

  if (use_db_name)
    use_db_name= !(cached_table && cached_table->belong_to_view &&
                   cached_table->belong_to_view->compact_view_format);

  if (use_table_name && (query_type & QT_ITEM_IDENT_SKIP_TABLE_NAMES))
  {
    /*
      Don't print the table name if it's the only table in the context
      XXX technically, that's a sufficient, but too strong condition
    */
    if (!context)
      use_db_name= use_table_name= false;
    else if (context->outer_context)
      use_table_name= true;
    else if (context->last_name_resolution_table == context->first_name_resolution_table)
      use_db_name= use_table_name= false;
    else if (!context->last_name_resolution_table &&
             !context->first_name_resolution_table->next_name_resolution_table)
      use_db_name= use_table_name= false;
  }

  if (!field_name.str || !field_name.str[0])
  {
    append_identifier(thd, str, STRING_WITH_LEN("tmp_field"));
    return;
  }

  if (lower_case_table_names== 1 ||
      (lower_case_table_names == 2 && !alias_name_used))
  {
    if (use_table_name)
    {
      strmov(t_name_buff, table_name);
      my_casedn_str(files_charset_info, t_name_buff);
      t_name= t_name_buff;
    }
    if (use_db_name)
    {
      strmov(d_name_buff, db_name);
      my_casedn_str(files_charset_info, d_name_buff);
      d_name= d_name_buff;
    }
  }

  if (use_db_name)
  {
    append_identifier(thd, str, d_name, (uint)strlen(d_name));
    str->append('.');
    DBUG_ASSERT(use_table_name);
  }
  if (use_table_name)
  {
    append_identifier(thd, str, t_name, (uint) strlen(t_name));
    str->append('.');
  }
  append_identifier(thd, str, &field_name);
}
void Item_ident::print(String *str, enum_query_type query_type)
{
  THD *thd= current_thd;
  char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
  const char *d_name= db_name, *t_name= table_name;
  bool use_table_name= table_name && table_name[0];
  bool use_db_name= use_table_name && db_name && db_name[0] && !alias_name_used;

  if (use_db_name && (query_type & QT_ITEM_IDENT_SKIP_DB_NAMES))
    use_db_name= !thd->db.str || strcmp(thd->db.str, db_name);

  if (use_db_name)
    use_db_name= !(cached_table && cached_table->belong_to_view &&
                   cached_table->belong_to_view->compact_view_format);

  if (use_table_name && (query_type & QT_ITEM_IDENT_SKIP_TABLE_NAMES))
  {
     
    if (!context)
      use_db_name= use_table_name= false;
    else if (context->outer_context)
      use_table_name= true;
    else if (context->last_name_resolution_table == context->first_name_resolution_table)
      use_db_name= use_table_name= false;
    else if (!context->last_name_resolution_table &&
             !context->first_name_resolution_table->next_name_resolution_table)
      use_db_name= use_table_name= false;
  }

  if (!field_name.str || !field_name.str[0])
  {
    append_identifier(thd, str, STRING_WITH_LEN("tmp_field"));
    return;
  }

  if (lower_case_table_names== 1 ||
      (lower_case_table_names == 2 && !alias_name_used))
  {
    if (use_table_name)
    {
      strmov(t_name_buff, table_name);
      my_casedn_str(files_charset_info, t_name_buff);
      t_name= t_name_buff;
    }
    if (use_db_name)
    {
      strmov(d_name_buff, db_name);
      my_casedn_str(files_charset_info, d_name_buff);
      d_name= d_name_buff;
    }
  }

  if (use_db_name)
  {
    append_identifier(thd, str, d_name, (uint)strlen(d_name));
    str->append('.');
    DBUG_ASSERT(use_table_name);
  }
  if (use_table_name)
  {
    append_identifier(thd, str, t_name, (uint) strlen(t_name));
    str->append('.');
  }
  append_identifier(thd, str, &field_name);
}