summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorunknown <ingo@mysql.com>2005-12-06 17:48:31 +0100
committerunknown <ingo@mysql.com>2005-12-06 17:48:31 +0100
commita73c263386d5982bcdec70ea407013a3f4c91ea7 (patch)
treef731241c8b5e21c507287765a07eca69dad51d27 /sql
parent60f2e7a9a4518c0ff271ccb2153060bb68f658a7 (diff)
parent38b7ede9c6146884c17fe9290a0913d2a95fc548 (diff)
downloadmariadb-git-a73c263386d5982bcdec70ea407013a3f4c91ea7.tar.gz
Merge mysql.com:/home/mydev/mysql-4.0
into mysql.com:/home/mydev/mysql-4.0-bug5390 sql/sql_parse.cc: Auto merged
Diffstat (limited to 'sql')
-rw-r--r--sql/lock.cc89
-rw-r--r--sql/mysql_priv.h1
-rw-r--r--sql/sql_parse.cc11
-rw-r--r--sql/sql_update.cc2
4 files changed, 94 insertions, 9 deletions
diff --git a/sql/lock.cc b/sql/lock.cc
index 47ccc44952d..8944f23977f 100644
--- a/sql/lock.cc
+++ b/sql/lock.cc
@@ -390,6 +390,88 @@ MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b)
}
+/*
+ Find duplicate lock in tables.
+
+ SYNOPSIS
+ mysql_lock_have_duplicate()
+ thd The current thread.
+ table The table to check for duplicate lock.
+ tables The list of tables to search for the dup lock.
+
+ NOTE
+ This is mainly meant for MERGE tables in INSERT ... SELECT
+ situations. The 'real', underlying tables can be found only after
+ the table is opened. The easier way is to check this after the
+ tables are locked.
+
+ RETURN
+ 1 A table from 'tables' matches a lock on 'table'.
+ 0 No duplicate lock is present.
+ -1 Error.
+*/
+
+int mysql_lock_have_duplicate(THD *thd, TABLE *table, TABLE_LIST *tables)
+{
+ uint count;
+ MYSQL_LOCK *sql_lock1;
+ MYSQL_LOCK *sql_lock2;
+ TABLE **tables1= &table;
+ TABLE **tables2;
+ TABLE **table_ptr;
+ TABLE_LIST *tablist2;
+ TABLE *write_lock_used;
+ THR_LOCK_DATA **lock_data1;
+ THR_LOCK_DATA **end_data1;
+ THR_LOCK_DATA **lock_data2;
+ THR_LOCK_DATA **end_data2;
+ THR_LOCK *lock1;
+ DBUG_ENTER("mysql_lock_have_duplicate");
+
+ if (! (sql_lock1= get_lock_data(thd, tables1, 1, 1, &write_lock_used)))
+ goto err0;
+
+ count=0;
+ for (tablist2 = tables; tablist2; tablist2= tablist2->next)
+ count++;
+ if (! (tables2= (TABLE**) sql_alloc(sizeof(TABLE*) * count)))
+ goto err1;
+ table_ptr= tables2;
+ for (tablist2 = tables; tablist2; tablist2= tablist2->next)
+ *(table_ptr++)= tablist2->table;
+ if (! (sql_lock2= get_lock_data(thd, tables2, count, 1, &write_lock_used)))
+ goto err1;
+
+ count= 1;
+ for (lock_data1= sql_lock1->locks,
+ end_data1= lock_data1 + sql_lock1->lock_count;
+ lock_data1 < end_data1;
+ lock_data1++)
+ {
+ lock1= (*lock_data1)->lock;
+ for (lock_data2= sql_lock2->locks,
+ end_data2= lock_data2 + sql_lock2->lock_count;
+ lock_data2 < end_data2;
+ lock_data2++)
+ {
+ if ((*lock_data2)->lock == lock1)
+ goto end;
+ }
+ }
+ count= 0;
+
+ end:
+ my_free((gptr) sql_lock2, MYF(0));
+ my_free((gptr) sql_lock1, MYF(0));
+ DBUG_RETURN(count);
+
+ err1:
+ my_free((gptr) sql_lock1, MYF(0));
+ err0:
+ DBUG_RETURN(-1);
+}
+
+
/* unlock a set of external */
static int unlock_external(THD *thd, TABLE **table,uint count)
@@ -426,6 +508,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count,
MYSQL_LOCK *sql_lock;
THR_LOCK_DATA **locks;
TABLE **to;
+ DBUG_ENTER("get_lock_data");
*write_lock_used=0;
for (i=tables=lock_count=0 ; i < count ; i++)
@@ -441,7 +524,7 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count,
my_malloc(sizeof(*sql_lock)+
sizeof(THR_LOCK_DATA*)*tables+sizeof(table_ptr)*lock_count,
MYF(0))))
- return 0;
+ DBUG_RETURN(0);
locks=sql_lock->locks=(THR_LOCK_DATA**) (sql_lock+1);
to=sql_lock->table=(TABLE**) (locks+tables);
sql_lock->table_count=lock_count;
@@ -461,13 +544,13 @@ static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table_ptr, uint count,
{
my_error(ER_OPEN_AS_READONLY,MYF(0),table->table_name);
my_free((gptr) sql_lock,MYF(0));
- return 0;
+ DBUG_RETURN(0);
}
}
locks=table->file->store_lock(thd, locks, get_old_locks ? TL_IGNORE :
lock_type);
}
- return sql_lock;
+ DBUG_RETURN(sql_lock);
}
/*****************************************************************************
diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h
index 62e65ea95da..3972a01f7ba 100644
--- a/sql/mysql_priv.h
+++ b/sql/mysql_priv.h
@@ -788,6 +788,7 @@ void mysql_lock_remove(THD *thd, MYSQL_LOCK *locked,TABLE *table);
void mysql_lock_abort(THD *thd, TABLE *table);
bool mysql_lock_abort_for_thread(THD *thd, TABLE *table);
MYSQL_LOCK *mysql_lock_merge(MYSQL_LOCK *a,MYSQL_LOCK *b);
+int mysql_lock_have_duplicate(THD *thd, TABLE *table, TABLE_LIST *tables);
bool lock_global_read_lock(THD *thd);
void unlock_global_read_lock(THD *thd);
bool wait_if_global_read_lock(THD *thd, bool abort_on_refresh, bool is_not_commit);
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 15974fc1d63..4b32703c1ee 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -2081,11 +2081,6 @@ mysql_execute_command(void)
if (thd->select_limit < select_lex->select_limit)
thd->select_limit= HA_POS_ERROR; // No limit
- if (check_dup(tables->db, tables->real_name, tables->next))
- {
- /* Using same table for INSERT and SELECT */
- select_lex->options |= OPTION_BUFFER_RESULT;
- }
{
/* TODO: Delete the following loop when locks is set by sql_yacc */
TABLE_LIST *table;
@@ -2098,6 +2093,12 @@ mysql_execute_command(void)
(byte*) (((TABLE_LIST *) lex->select_lex.table_list.first)->next);
if (!(res=open_and_lock_tables(thd, tables)))
{
+ /* MERGE sub-tables can only be detected after open. */
+ if (mysql_lock_have_duplicate(thd, tables->table, tables->next))
+ {
+ /* Using same table for INSERT and SELECT */
+ select_lex->options |= OPTION_BUFFER_RESULT;
+ }
if ((result=new select_insert(tables->table,&lex->field_list,
lex->duplicates)))
res=handle_select(thd,lex,result);
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index b2b10a9b59a..888b475ffce 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -712,7 +712,7 @@ int multi_update::prepare(List<Item> &not_used_values)
{
TABLE *table=table_ref->table;
if (!(tables_to_update & table->map) &&
- check_dup(table_ref->db, table_ref->real_name, update_tables))
+ mysql_lock_have_duplicate(thd, table, update_tables))
table->no_cache= 1; // Disable row cache
}
DBUG_RETURN(thd->fatal_error != 0);