diff options
author | bell@sanja.is.com.ua <> | 2002-11-30 19:33:30 +0200 |
---|---|---|
committer | bell@sanja.is.com.ua <> | 2002-11-30 19:33:30 +0200 |
commit | 2ccb0eeb4fe09b4d688a34326411bb53b61f65cf (patch) | |
tree | ed41081be7cf06300b971cb3433b3445dc8977df | |
parent | 6a8e2a9cbc91ff7e9eb9958a0c5e758685f6c3b8 (diff) | |
parent | 9da705ef2ffd2f157565a6fa9522d7de6d1aa95e (diff) | |
download | mariadb-git-2ccb0eeb4fe09b4d688a34326411bb53b61f65cf.tar.gz |
Merge sanja.is.com.ua:/home/bell/mysql/mysql-4.1
into sanja.is.com.ua:/home/bell/mysql/work-crash-4.1
-rw-r--r-- | mysql-test/r/subselect.result | 22 | ||||
-rw-r--r-- | mysql-test/t/subselect.test | 18 | ||||
-rw-r--r-- | sql/mysql_priv.h | 3 | ||||
-rw-r--r-- | sql/sql_base.cc | 27 | ||||
-rw-r--r-- | sql/sql_delete.cc | 6 | ||||
-rw-r--r-- | sql/sql_insert.cc | 7 | ||||
-rw-r--r-- | sql/sql_parse.cc | 26 | ||||
-rw-r--r-- | sql/sql_update.cc | 7 |
8 files changed, 96 insertions, 20 deletions
diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index bbf70150ee0..91a4087378f 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -411,6 +411,8 @@ a b 0 10 1 11 2 12 +update t1 set b= (select b from t1); +INSERT TABLE 't1' isn't allowed in FROM table list update t1 set b= (select b from t2 where t1.a = t2.a); select * from t1; a b @@ -430,6 +432,8 @@ a b select * from t1 where b = (select b from t2 where t1.a = t2.a); a b 2 12 +delete from t1 where b = (select b from t1); +INSERT TABLE 't1' isn't allowed in FROM table list delete from t1 where b = (select b from t2 where t1.a = t2.a); select * from t1; a b @@ -453,6 +457,8 @@ a b 33 10 22 11 2 12 +delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a); +INSERT TABLE 't12' isn't allowed in FROM table list delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a); select * from t11; a b @@ -466,6 +472,8 @@ drop table t11, t12, t2; CREATE TABLE t1 (x int); create table t2 (a int); insert into t2 values (1); +INSERT INTO t1 (x) VALUES ((SELECT x FROM t1)); +INSERT TABLE 't1' isn't allowed in FROM table list INSERT INTO t1 (x) VALUES ((SELECT a FROM t2)); select * from t1; x @@ -485,20 +493,22 @@ x 3 INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2; INSERT TABLE 't1' isn't allowed in FROM table list -INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t1)); +INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2)); select * from t1; x 1 2 3 3 -9 +0 drop table t1, t2; CREATE TABLE t1 (x int not null, y int, primary key (x)); create table t2 (a int); insert into t2 values (1); select * from t1; x y +replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2)); +INSERT TABLE 't1' isn't allowed in FROM table list replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2)); select * from t1; x y @@ -559,4 +569,10 @@ id SELECT * FROM t WHERE id IN (SELECT 5 UNION SELECT 2); id 2 -drop table if exists t; +INSERT INTO t VALUES ((SELECT * FROM t)); +INSERT TABLE 't' isn't allowed in FROM table list +SELECT * FROM t; +id +1 +2 +drop table t; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index c875f8372b3..a82808d3391 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -245,6 +245,8 @@ create table t2 (a int NOT NULL, b int, primary key (a)); insert into t1 values (0, 10),(1, 11),(2, 12); insert into t2 values (1, 21),(2, 22),(3, 23); select * from t1; +-- error 1093 +update t1 set b= (select b from t1); update t1 set b= (select b from t2 where t1.a = t2.a); select * from t1; drop table t1, t2; @@ -256,6 +258,8 @@ insert into t1 values (0, 10),(1, 11),(2, 12); insert into t2 values (1, 21),(2, 12),(3, 23); select * from t1; select * from t1 where b = (select b from t2 where t1.a = t2.a); +-- error 1093 +delete from t1 where b = (select b from t1); delete from t1 where b = (select b from t2 where t1.a = t2.a); select * from t1; drop table t1, t2; @@ -270,6 +274,8 @@ insert into t12 values (33, 10),(22, 11),(2, 12); insert into t2 values (1, 21),(2, 12),(3, 23); select * from t11; select * from t12; +-- error 1093 +delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a); delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a); select * from t11; select * from t12; @@ -279,6 +285,8 @@ drop table t11, t12, t2; CREATE TABLE t1 (x int); create table t2 (a int); insert into t2 values (1); +-- error 1093 +INSERT INTO t1 (x) VALUES ((SELECT x FROM t1)); INSERT INTO t1 (x) VALUES ((SELECT a FROM t2)); select * from t1; insert into t2 values (1); @@ -289,7 +297,7 @@ INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2; select * from t1; -- error 1093 INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2; -INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t1)); +INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2)); -- sleep 1 select * from t1; drop table t1, t2; @@ -299,6 +307,8 @@ CREATE TABLE t1 (x int not null, y int, primary key (x)); create table t2 (a int); insert into t2 values (1); select * from t1; +-- error 1093 +replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2)); replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2)); select * from t1; replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2)); @@ -326,5 +336,7 @@ EXPLAIN SELECT * FROM t WHERE id IN (SELECT 1+(select 1)); EXPLAIN SELECT * FROM t WHERE id IN (SELECT 1 UNION SELECT 3); SELECT * FROM t WHERE id IN (SELECT 5 UNION SELECT 3); SELECT * FROM t WHERE id IN (SELECT 5 UNION SELECT 2); -drop table if exists t; - +-- error 1093 +INSERT INTO t VALUES ((SELECT * FROM t)); +SELECT * FROM t; +drop table t; diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 242f8601b23..61e0f8f7ffa 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -580,6 +580,9 @@ bool close_thread_table(THD *thd, TABLE **table_ptr); void close_temporary_tables(THD *thd); TABLE_LIST * find_table_in_list(TABLE_LIST *table, const char *db_name, const char *table_name); +TABLE_LIST * find_real_table_in_list(TABLE_LIST *table, + const char *db_name, + const char *table_name); TABLE **find_temporary_table(THD *thd, const char *db, const char *table_name); bool close_temporary_table(THD *thd, const char *db, const char *table_name); void close_temporary(TABLE *table, bool delete_table=1); diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 8f74903027e..0e110dca52b 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -744,7 +744,7 @@ void close_temporary_tables(THD *thd) } /* - Find first suitable table in given list. + Find first suitable table by alias in given list. SYNOPSIS find_table_in_list() @@ -767,6 +767,31 @@ TABLE_LIST * find_table_in_list(TABLE_LIST *table, return table; } +/* + Find real table in given list. + + SYNOPSIS + find_table_in_list() + table - pointer to table list + db_name - data base name + table_name - table name + + RETURN VALUES + NULL Table not found + # Pointer to found table. +*/ + +TABLE_LIST * find_real_table_in_list(TABLE_LIST *table, + const char *db_name, + const char *table_name) +{ + for (; table; table= table->next) + if (!strcmp(table->db, db_name) && + !strcmp(table->real_name, table_name)) + break; + return table; +} + TABLE **find_temporary_table(THD *thd, const char *db, const char *table_name) { char key[MAX_DBKEY_LENGTH]; diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 07958ebcfab..fe1a967f936 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -51,6 +51,12 @@ int mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, ORDER *order, if (setup_conds(thd, delete_table_list, &conds) || setup_ftfuncs(&thd->lex.select_lex)) DBUG_RETURN(-1); + if (find_real_table_in_list(table_list->next, + table_list->db, table_list->real_name)) + { + my_error(ER_INSERT_TABLE_USED, MYF(0), table_list->real_name); + DBUG_RETURN(-1); + } const_cond= (!conds || conds->const_item()); safe_update=test(thd->options & OPTION_SAFE_UPDATES); diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 1ca44046997..83b125ee630 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -171,6 +171,13 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list, List<Item> &fields, table->time_stamp= save_time_stamp; goto abort; } + if (find_real_table_in_list(table_list->next, + table_list->db, table_list->real_name)) + { + my_error(ER_INSERT_TABLE_USED, MYF(0), table_list->real_name); + goto abort; + } + value_count= values->elements; while ((values= its++)) { diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 70fb5acdffd..7bdc6873e6f 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -61,7 +61,6 @@ static int check_for_max_user_connections(THD *thd, USER_CONN *uc); static void decrease_user_connections(USER_CONN *uc); static bool check_db_used(THD *thd,TABLE_LIST *tables); static bool check_merge_table_access(THD *thd, char *db, TABLE_LIST *tables); -static bool check_dup(const char *db, const char *name, TABLE_LIST *tables); static void remove_escape(char *name); static void refresh_status(void); static bool append_file_to_dir(THD *thd, char **filename_ptr, @@ -1645,7 +1644,7 @@ mysql_execute_command(THD *thd) select_result *result; if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) && - check_dup(tables->db, tables->real_name, tables->next)) + find_real_table_in_list(tables->next, tables->db, tables->real_name)) { net_printf(thd,ER_INSERT_TABLE_USED,tables->real_name); DBUG_VOID_RETURN; @@ -2007,7 +2006,7 @@ mysql_execute_command(THD *thd) if (unit->select_limit_cnt < select_lex->select_limit) unit->select_limit_cnt= HA_POS_ERROR; // No limit - if (check_dup(tables->db, tables->real_name, tables->next)) + if (find_real_table_in_list(tables->next, tables->db, tables->real_name)) { net_printf(thd,ER_INSERT_TABLE_USED,tables->real_name); DBUG_VOID_RETURN; @@ -2100,6 +2099,17 @@ mysql_execute_command(THD *thd) /* Fix tables-to-be-deleted-from list to point at opened tables */ for (auxi=(TABLE_LIST*) aux_tables ; auxi ; auxi=auxi->next) auxi->table= auxi->table_list->table; + if (&lex->select_lex != lex->all_selects_list) + for (TABLE_LIST *t= select_lex->get_table_list(); + t; t= t->next) + { + if (find_real_table_in_list(t->table_list->next, t->db, t->real_name)) + { + my_error(ER_INSERT_TABLE_USED, MYF(0), t->real_name); + res= -1; + break; + } + } fix_tables_pointers(lex->all_selects_list); if (!thd->fatal_error && (result= new multi_delete(thd,aux_tables, table_count))) @@ -3521,16 +3531,6 @@ void add_join_natural(TABLE_LIST *a,TABLE_LIST *b) b->natural_join=a; } - /* Check if name is used in table list */ - -static bool check_dup(const char *db, const char *name, TABLE_LIST *tables) -{ - for (; tables ; tables=tables->next) - if (!strcmp(name,tables->real_name) && !strcmp(db,tables->db)) - return 1; - return 0; -} - bool reload_acl_and_cache(THD *thd, ulong options, TABLE_LIST *tables) { bool result=0; diff --git a/sql/sql_update.cc b/sql/sql_update.cc index c2ba65248a7..4e15011c254 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -86,6 +86,13 @@ int mysql_update(THD *thd, setup_conds(thd,update_table_list,&conds) || setup_ftfuncs(&thd->lex.select_lex)) DBUG_RETURN(-1); /* purecov: inspected */ + if (find_real_table_in_list(table_list->next, + table_list->db, table_list->real_name)) + { + my_error(ER_INSERT_TABLE_USED, MYF(0), table_list->real_name); + DBUG_RETURN(-1); + } + old_used_keys=table->used_keys; // Keys used in WHERE /* |