summaryrefslogtreecommitdiff
path: root/sql/sql_view.cc
diff options
context:
space:
mode:
authorunknown <brian@zim.(none)>2005-08-30 10:49:11 -0700
committerunknown <brian@zim.(none)>2005-08-30 10:49:11 -0700
commit172392f272b65ce630e3c56790ed76e666af3903 (patch)
tree55867311c24915cd6461d324ceec28ea9f86bf0c /sql/sql_view.cc
parent9dd4438d00f606bd2a25016c68aaed40b8434bc5 (diff)
parent5dd6e8776be709bbc04a332f450252383d2568d8 (diff)
downloadmariadb-git-172392f272b65ce630e3c56790ed76e666af3903.tar.gz
Merge baker@bk-internal.mysql.com:/home/bk/mysql-5.1
into zim.(none):/home/brian/mysql/mysql-5.1
Diffstat (limited to 'sql/sql_view.cc')
-rw-r--r--sql/sql_view.cc110
1 files changed, 78 insertions, 32 deletions
diff --git a/sql/sql_view.cc b/sql/sql_view.cc
index aedff648e5c..dcada0c0780 100644
--- a/sql/sql_view.cc
+++ b/sql/sql_view.cc
@@ -94,6 +94,67 @@ static void make_unique_view_field_name(Item *target,
target->set_name(buff, name_len, system_charset_info);
}
+
+/*
+ Check if items with same names are present in list and possibly
+ generate unique names for them.
+
+ SYNOPSIS
+ item_list list of Items which should be checked for duplicates
+ gen_unique_view_name flag: generate unique name or return with error when
+ duplicate names are found.
+
+ DESCRIPTION
+ This function is used on view creation and preparation of derived tables.
+ It checks item_list for items with duplicate names. If it founds two
+ items with same name and conversion to unique names isn't allowed, or
+ names for both items are set by user - function fails.
+ Otherwise it generates unique name for one item with autogenerated name
+ using make_unique_view_field_name()
+
+ RETURN VALUE
+ FALSE no duplicate names found, or they are converted to unique ones
+ TRUE duplicate names are found and they can't be converted or conversion
+ isn't allowed
+*/
+
+bool check_duplicate_names(List<Item> &item_list, bool gen_unique_view_name)
+{
+ Item *item;
+ List_iterator_fast<Item> it(item_list);
+ List_iterator_fast<Item> itc(item_list);
+ DBUG_ENTER("check_duplicate_names");
+
+ while ((item= it++))
+ {
+ Item *check;
+ /* treat underlying fields like set by user names */
+ if (item->real_item()->type() == Item::FIELD_ITEM)
+ item->is_autogenerated_name= FALSE;
+ itc.rewind();
+ while ((check= itc++) && check != item)
+ {
+ if (my_strcasecmp(system_charset_info, item->name, check->name) == 0)
+ {
+ if (!gen_unique_view_name)
+ goto err;
+ if (item->is_autogenerated_name)
+ make_unique_view_field_name(item, item_list, item);
+ else if (check->is_autogenerated_name)
+ make_unique_view_field_name(check, item_list, item);
+ else
+ goto err;
+ }
+ }
+ }
+ DBUG_RETURN(FALSE);
+
+err:
+ my_error(ER_DUP_FIELDNAME, MYF(0), item->name);
+ DBUG_RETURN(TRUE);
+}
+
+
/*
Creating/altering VIEW procedure
@@ -245,9 +306,11 @@ bool mysql_create_view(THD *thd,
/*
check that tables are not temporary and this VIEW do not used in query
- (it is possible with ALTERing VIEW)
- */
- for (tbl= tables; tbl; tbl= tbl->next_global)
+ (it is possible with ALTERing VIEW).
+ open_and_lock_tables can change the value of tables,
+ e.g. it may happen if before the function call tables was equal to 0.
+ */
+ for (tbl= lex->query_tables; tbl; tbl= tbl->next_global)
{
/* is this table temporary and is not view? */
if (tbl->table->s->tmp_table != NO_TMP_TABLE && !tbl->view &&
@@ -308,35 +371,8 @@ bool mysql_create_view(THD *thd,
}
}
- /* Test absence of duplicates names */
- {
- Item *item;
- List_iterator_fast<Item> it(select_lex->item_list);
- List_iterator_fast<Item> itc(select_lex->item_list);
- while ((item= it++))
- {
- Item *check;
- /* treat underlying fields like set by user names */
- if (item->real_item()->type() == Item::FIELD_ITEM)
- item->is_autogenerated_name= FALSE;
- itc.rewind();
- while ((check= itc++) && check != item)
- {
- if (my_strcasecmp(system_charset_info, item->name, check->name) == 0)
- {
- if (item->is_autogenerated_name)
- make_unique_view_field_name(item, select_lex->item_list, item);
- else if (check->is_autogenerated_name)
- make_unique_view_field_name(check, select_lex->item_list, item);
- else
- {
- my_error(ER_DUP_FIELDNAME, MYF(0), item->name);
- DBUG_RETURN(TRUE);
- }
- }
- }
- }
- }
+ if (check_duplicate_names(select_lex->item_list, 1))
+ DBUG_RETURN(TRUE);
#ifndef NO_EMBEDDED_ACCESS_CHECKS
/*
@@ -844,6 +880,9 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table)
view_tables);
lex->select_lex.context.outer_context= 0;
lex->select_lex.context.select_lex= table->select_lex;
+ lex->select_lex.select_n_having_items+=
+ table->select_lex->select_n_having_items;
+
/* do not check privileges & hide errors for view underlyings */
for (SELECT_LEX *sl= lex->all_selects_list;
sl;
@@ -864,7 +903,11 @@ mysql_make_view(File_parser *parser, TABLE_LIST *table)
{
if (view_tables->next_local)
+ {
table->multitable_view= TRUE;
+ if (table->belong_to_view)
+ table->belong_to_view->multitable_view= TRUE;
+ }
/* make nested join structure for view tables */
NESTED_JOIN *nested_join;
if (!(nested_join= table->nested_join=
@@ -1110,7 +1153,10 @@ bool check_key_in_view(THD *thd, TABLE_LIST *view)
for (Field_translator *fld= trans; fld < end_of_trans; fld++)
{
if (!fld->item->fixed && fld->item->fix_fields(thd, &fld->item))
+ {
+ thd->set_query_id= save_set_query_id;
return TRUE;
+ }
}
thd->set_query_id= save_set_query_id;
}