2006-12-31 01:02:27 +01:00
|
|
|
/* Copyright (C) 2000-2006 MySQL AB
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
|
|
|
|
/* Insert of records */
|
|
|
|
|
2006-06-26 20:57:18 +02:00
|
|
|
/*
|
|
|
|
INSERT DELAYED
|
|
|
|
|
|
|
|
Insert delayed is distinguished from a normal insert by lock_type ==
|
|
|
|
TL_WRITE_DELAYED instead of TL_WRITE. It first tries to open a
|
|
|
|
"delayed" table (delayed_get_table()), but falls back to
|
|
|
|
open_and_lock_tables() on error and proceeds as normal insert then.
|
|
|
|
|
|
|
|
Opening a "delayed" table means to find a delayed insert thread that
|
|
|
|
has the table open already. If this fails, a new thread is created and
|
|
|
|
waited for to open and lock the table.
|
|
|
|
|
|
|
|
If accessing the thread succeeded, in
|
|
|
|
delayed_insert::get_local_table() the table of the thread is copied
|
|
|
|
for local use. A copy is required because the normal insert logic
|
|
|
|
works on a target table, but the other threads table object must not
|
|
|
|
be used. The insert logic uses the record buffer to create a record.
|
|
|
|
And the delayed insert thread uses the record buffer to pass the
|
|
|
|
record to the table handler. So there must be different objects. Also
|
|
|
|
the copied table is not included in the lock, so that the statement
|
|
|
|
can proceed even if the real table cannot be accessed at this moment.
|
|
|
|
|
|
|
|
Copying a table object is not a trivial operation. Besides the TABLE
|
|
|
|
object there are the field pointer array, the field objects and the
|
|
|
|
record buffer. After copying the field objects, their pointers into
|
|
|
|
the record must be "moved" to point to the new record buffer.
|
|
|
|
|
|
|
|
After this setup the normal insert logic is used. Only that for
|
|
|
|
delayed inserts write_delayed() is called instead of write_record().
|
|
|
|
It inserts the rows into a queue and signals the delayed insert thread
|
|
|
|
instead of writing directly to the table.
|
|
|
|
|
|
|
|
The delayed insert thread awakes from the signal. It locks the table,
|
|
|
|
inserts the rows from the queue, unlocks the table, and waits for the
|
|
|
|
next signal. It does normally live until a FLUSH TABLES or SHUTDOWN.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
#include "mysql_priv.h"
|
2004-09-07 14:29:46 +02:00
|
|
|
#include "sp_head.h"
|
|
|
|
#include "sql_trigger.h"
|
2004-09-15 22:42:56 +02:00
|
|
|
#include "sql_select.h"
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
static int check_null_fields(THD *thd,TABLE *entry);
|
2004-04-05 12:56:05 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2000-07-31 21:29:14 +02:00
|
|
|
static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list);
|
2004-12-31 11:04:35 +01:00
|
|
|
static int write_delayed(THD *thd,TABLE *table, enum_duplicates dup, bool ignore,
|
2003-04-02 00:15:20 +02:00
|
|
|
char *query, uint query_length, bool log_on);
|
2000-07-31 21:29:14 +02:00
|
|
|
static void end_delayed_insert(THD *thd);
|
2005-10-08 16:39:55 +02:00
|
|
|
pthread_handler_t handle_delayed_insert(void *arg);
|
2000-07-31 21:29:14 +02:00
|
|
|
static void unlink_blobs(register TABLE *table);
|
2004-04-05 12:56:05 +02:00
|
|
|
#endif
|
2005-07-01 06:05:42 +02:00
|
|
|
static bool check_view_insertability(THD *thd, TABLE_LIST *view);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
/* Define to force use of my_malloc() if the allocated memory block is big */
|
|
|
|
|
|
|
|
#ifndef HAVE_ALLOCA
|
|
|
|
#define my_safe_alloca(size, min_length) my_alloca(size)
|
|
|
|
#define my_safe_afree(ptr, size, min_length) my_afree(ptr)
|
|
|
|
#else
|
|
|
|
#define my_safe_alloca(size, min_length) ((size <= min_length) ? my_alloca(size) : my_malloc(size,MYF(0)))
|
|
|
|
#define my_safe_afree(ptr, size, min_length) if (size > min_length) my_free(ptr,MYF(0))
|
|
|
|
#endif
|
|
|
|
|
2007-01-22 13:14:38 +01:00
|
|
|
/*
|
|
|
|
Check that insert/update fields are from the same single table of a view.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
check_view_single_update()
|
|
|
|
fields The insert/update fields to be checked.
|
|
|
|
view The view for insert.
|
|
|
|
map [in/out] The insert table map.
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
This function is called in 2 cases:
|
|
|
|
1. to check insert fields. In this case *map will be set to 0.
|
|
|
|
Insert fields are checked to be all from the same single underlying
|
|
|
|
table of the given view. Otherwise the error is thrown. Found table
|
|
|
|
map is returned in the map parameter.
|
|
|
|
2. to check update fields of the ON DUPLICATE KEY UPDATE clause.
|
|
|
|
In this case *map contains table_map found on the previous call of
|
|
|
|
the function to check insert fields. Update fields are checked to be
|
|
|
|
from the same table as the insert fields.
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 OK
|
|
|
|
1 Error
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool check_view_single_update(List<Item> &fields, TABLE_LIST *view,
|
|
|
|
table_map *map)
|
|
|
|
{
|
|
|
|
/* it is join view => we need to find the table for update */
|
|
|
|
List_iterator_fast<Item> it(fields);
|
|
|
|
Item *item;
|
|
|
|
TABLE_LIST *tbl= 0; // reset for call to check_single_table()
|
|
|
|
table_map tables= 0;
|
|
|
|
|
|
|
|
while ((item= it++))
|
|
|
|
tables|= item->used_tables();
|
|
|
|
|
|
|
|
/* Check found map against provided map */
|
|
|
|
if (*map)
|
|
|
|
{
|
|
|
|
if (tables != *map)
|
|
|
|
goto error;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view->check_single_table(&tbl, tables, view) || tbl == 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
view->table= tbl->table;
|
|
|
|
*map= tables;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
error:
|
|
|
|
my_error(ER_VIEW_MULTIUPDATE, MYF(0),
|
|
|
|
view->view_db.str, view->view_name.str);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-04-19 15:12:32 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
/*
|
2004-04-02 08:12:53 +02:00
|
|
|
Check if insert fields are correct.
|
2005-04-19 15:12:32 +02:00
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
check_insert_fields()
|
|
|
|
thd The current thread.
|
|
|
|
table The table for insert.
|
|
|
|
fields The insert fields.
|
|
|
|
values The insert values.
|
2005-04-21 10:29:05 +02:00
|
|
|
check_unique If duplicate values should be rejected.
|
2005-04-19 15:12:32 +02:00
|
|
|
|
|
|
|
NOTE
|
|
|
|
Clears TIMESTAMP_AUTO_SET_ON_INSERT from table->timestamp_field_type
|
|
|
|
or leaves it as is, depending on if timestamp should be updated or
|
|
|
|
not.
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 OK
|
|
|
|
-1 Error
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
|
|
|
|
2005-04-21 10:29:05 +02:00
|
|
|
static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
|
|
|
|
List<Item> &fields, List<Item> &values,
|
2007-01-22 13:14:38 +01:00
|
|
|
bool check_unique, table_map *map)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-07-16 00:15:55 +02:00
|
|
|
TABLE *table= table_list->table;
|
2004-09-03 20:43:04 +02:00
|
|
|
|
2004-09-15 22:42:56 +02:00
|
|
|
if (!table_list->updatable)
|
|
|
|
{
|
2006-09-28 23:00:18 +02:00
|
|
|
my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias, "INSERT");
|
2004-09-15 22:42:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (fields.elements == 0 && values.elements != 0)
|
|
|
|
{
|
2004-09-15 22:42:56 +02:00
|
|
|
if (!table)
|
|
|
|
{
|
|
|
|
my_error(ER_VIEW_NO_INSERT_FIELD_LIST, MYF(0),
|
|
|
|
table_list->view_db.str, table_list->view_name.str);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-01-06 12:00:13 +01:00
|
|
|
if (values.elements != table->s->fields)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-04-30 08:46:08 +02:00
|
|
|
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
|
2000-07-31 21:29:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-09-26 12:33:13 +02:00
|
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
2004-09-03 20:43:04 +02:00
|
|
|
if (grant_option)
|
2004-07-16 00:15:55 +02:00
|
|
|
{
|
|
|
|
Field_iterator_table fields;
|
|
|
|
fields.set_table(table);
|
2004-09-03 20:43:04 +02:00
|
|
|
if (check_grant_all_columns(thd, INSERT_ACL, &table->grant,
|
2005-01-06 12:00:13 +01:00
|
|
|
table->s->db, table->s->table_name,
|
2004-07-16 00:15:55 +02:00
|
|
|
&fields))
|
|
|
|
return -1;
|
|
|
|
}
|
2003-09-26 12:33:13 +02:00
|
|
|
#endif
|
2005-04-22 12:30:09 +02:00
|
|
|
clear_timestamp_auto_bits(table->timestamp_field_type,
|
|
|
|
TIMESTAMP_AUTO_SET_ON_INSERT);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Part field list
|
2005-08-12 16:57:19 +02:00
|
|
|
SELECT_LEX *select_lex= &thd->lex->select_lex;
|
|
|
|
Name_resolution_context *context= &select_lex->context;
|
2005-11-28 20:57:50 +01:00
|
|
|
Name_resolution_context_state ctx_state;
|
2004-07-23 08:20:58 +02:00
|
|
|
int res;
|
2005-08-12 16:57:19 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (fields.elements != values.elements)
|
|
|
|
{
|
2005-04-30 08:46:08 +02:00
|
|
|
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
|
2000-07-31 21:29:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
thd->dupp_field=0;
|
2005-08-12 16:57:19 +02:00
|
|
|
select_lex->no_wrap_view_item= TRUE;
|
|
|
|
|
|
|
|
/* Save the state of the current name resolution context. */
|
2005-11-28 20:57:50 +01:00
|
|
|
ctx_state.save_state(context, table_list);
|
2005-08-12 16:57:19 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Perform name resolution only in the first table - 'table_list',
|
|
|
|
which is the table that is inserted into.
|
|
|
|
*/
|
2004-09-04 14:02:57 +02:00
|
|
|
table_list->next_local= 0;
|
2005-07-01 06:05:42 +02:00
|
|
|
context->resolve_in_table_list_only(table_list);
|
|
|
|
res= setup_fields(thd, 0, fields, 1, 0, 0);
|
2005-08-12 16:57:19 +02:00
|
|
|
|
|
|
|
/* Restore the current context. */
|
2005-11-28 20:57:50 +01:00
|
|
|
ctx_state.restore_state(context, table_list);
|
2005-07-01 06:05:42 +02:00
|
|
|
thd->lex->select_lex.no_wrap_view_item= FALSE;
|
2005-08-12 16:57:19 +02:00
|
|
|
|
2004-07-23 08:20:58 +02:00
|
|
|
if (res)
|
2000-07-31 21:29:14 +02:00
|
|
|
return -1;
|
2005-08-12 16:57:19 +02:00
|
|
|
|
2005-05-11 01:31:13 +02:00
|
|
|
if (table_list->effective_algorithm == VIEW_ALGORITHM_MERGE)
|
2004-09-15 22:42:56 +02:00
|
|
|
{
|
2007-01-22 13:14:38 +01:00
|
|
|
if (check_view_single_update(fields, table_list, map))
|
2004-09-15 22:42:56 +02:00
|
|
|
return -1;
|
2007-01-22 13:14:38 +01:00
|
|
|
table= table_list->table;
|
2004-09-15 22:42:56 +02:00
|
|
|
}
|
2004-04-04 02:05:44 +02:00
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
if (check_unique && thd->dupp_field)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), thd->dupp_field->field_name);
|
2000-07-31 21:29:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (table->timestamp_field && // Don't set timestamp if used
|
2004-04-02 08:12:53 +02:00
|
|
|
table->timestamp_field->query_id == thd->query_id)
|
2005-04-22 12:30:09 +02:00
|
|
|
clear_timestamp_auto_bits(table->timestamp_field_type,
|
|
|
|
TIMESTAMP_AUTO_SET_ON_INSERT);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2002-11-29 15:40:18 +01:00
|
|
|
// For the values we need select_priv
|
2003-09-26 12:33:13 +02:00
|
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
2005-01-04 17:04:16 +01:00
|
|
|
table->grant.want_privilege= (SELECT_ACL & ~table->grant.privilege);
|
2003-09-26 12:33:13 +02:00
|
|
|
#endif
|
2004-09-15 22:42:56 +02:00
|
|
|
|
|
|
|
if (check_key_in_view(thd, table_list) ||
|
|
|
|
(table_list->view &&
|
2005-07-01 06:05:42 +02:00
|
|
|
check_view_insertability(thd, table_list)))
|
2004-09-15 22:42:56 +02:00
|
|
|
{
|
2006-09-28 23:00:18 +02:00
|
|
|
my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias, "INSERT");
|
2004-09-15 22:42:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-19 15:12:32 +02:00
|
|
|
/*
|
|
|
|
Check update fields for the timestamp field.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
check_update_fields()
|
|
|
|
thd The current thread.
|
|
|
|
insert_table_list The insert table list.
|
|
|
|
table The table for update.
|
|
|
|
update_fields The update fields.
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
If the update fields include the timestamp field,
|
|
|
|
remove TIMESTAMP_AUTO_SET_ON_UPDATE from table->timestamp_field_type.
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 OK
|
|
|
|
-1 Error
|
|
|
|
*/
|
|
|
|
|
2005-04-21 10:29:05 +02:00
|
|
|
static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
|
2007-01-22 13:14:38 +01:00
|
|
|
List<Item> &update_fields, table_map *map)
|
2005-04-19 15:12:32 +02:00
|
|
|
{
|
2005-04-21 10:29:05 +02:00
|
|
|
TABLE *table= insert_table_list->table;
|
2005-07-07 03:56:10 +02:00
|
|
|
query_id_t timestamp_query_id;
|
2005-04-19 15:12:32 +02:00
|
|
|
LINT_INIT(timestamp_query_id);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Change the query_id for the timestamp column so that we can
|
|
|
|
check if this is modified directly.
|
|
|
|
*/
|
|
|
|
if (table->timestamp_field)
|
|
|
|
{
|
|
|
|
timestamp_query_id= table->timestamp_field->query_id;
|
2005-04-21 10:29:05 +02:00
|
|
|
table->timestamp_field->query_id= thd->query_id - 1;
|
2005-04-19 15:12:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check the fields we are going to modify. This will set the query_id
|
|
|
|
of all used fields to the threads query_id.
|
|
|
|
*/
|
2005-07-01 06:05:42 +02:00
|
|
|
if (setup_fields(thd, 0, update_fields, 1, 0, 0))
|
2005-04-19 15:12:32 +02:00
|
|
|
return -1;
|
|
|
|
|
2007-01-22 13:14:38 +01:00
|
|
|
if (insert_table_list->effective_algorithm == VIEW_ALGORITHM_MERGE &&
|
|
|
|
check_view_single_update(update_fields, insert_table_list, map))
|
|
|
|
return -1;
|
|
|
|
|
2005-04-19 15:12:32 +02:00
|
|
|
if (table->timestamp_field)
|
|
|
|
{
|
|
|
|
/* Don't set timestamp column if this is modified. */
|
|
|
|
if (table->timestamp_field->query_id == thd->query_id)
|
2005-04-22 12:30:09 +02:00
|
|
|
clear_timestamp_auto_bits(table->timestamp_field_type,
|
|
|
|
TIMESTAMP_AUTO_SET_ON_UPDATE);
|
2005-04-19 15:12:32 +02:00
|
|
|
else
|
|
|
|
table->timestamp_field->query_id= timestamp_query_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-01 23:51:10 +02:00
|
|
|
/*
|
|
|
|
Mark fields used by triggers for INSERT-like statement.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mark_fields_used_by_triggers_for_insert_stmt()
|
|
|
|
thd The current thread
|
|
|
|
table Table to which insert will happen
|
|
|
|
duplic Type of duplicate handling for insert which will happen
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
For REPLACE there is no sense in marking particular fields
|
|
|
|
used by ON DELETE trigger as to execute it properly we have
|
|
|
|
to retrieve and store values for all table columns anyway.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void mark_fields_used_by_triggers_for_insert_stmt(THD *thd, TABLE *table,
|
|
|
|
enum_duplicates duplic)
|
|
|
|
{
|
|
|
|
if (table->triggers)
|
|
|
|
{
|
|
|
|
table->triggers->mark_fields_used(thd, TRG_EVENT_INSERT);
|
|
|
|
if (duplic == DUP_UPDATE)
|
|
|
|
table->triggers->mark_fields_used(thd, TRG_EVENT_UPDATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_insert(THD *thd,TABLE_LIST *table_list,
|
|
|
|
List<Item> &fields,
|
|
|
|
List<List_item> &values_list,
|
|
|
|
List<Item> &update_fields,
|
|
|
|
List<Item> &update_values,
|
2005-01-03 22:04:52 +01:00
|
|
|
enum_duplicates duplic,
|
|
|
|
bool ignore)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-03-29 16:57:07 +02:00
|
|
|
int error, res;
|
2003-03-06 22:46:24 +01:00
|
|
|
/*
|
|
|
|
log_on is about delayed inserts only.
|
|
|
|
By default, both logs are enabled (this won't cause problems if the server
|
|
|
|
runs without --log-update or --log-bin).
|
|
|
|
*/
|
2005-09-15 21:29:07 +02:00
|
|
|
bool log_on= (thd->options & OPTION_BIN_LOG) ||
|
|
|
|
(!(thd->security_ctx->master_access & SUPER_ACL));
|
2005-12-12 19:11:56 +01:00
|
|
|
bool transactional_table, joins_freed= FALSE;
|
2006-03-15 18:15:52 +01:00
|
|
|
bool changed;
|
2000-07-31 21:29:14 +02:00
|
|
|
uint value_count;
|
|
|
|
ulong counter = 1;
|
|
|
|
ulonglong id;
|
|
|
|
COPY_INFO info;
|
2004-09-15 22:42:56 +02:00
|
|
|
TABLE *table= 0;
|
2001-08-02 05:29:50 +02:00
|
|
|
List_iterator_fast<List_item> its(values_list);
|
2000-07-31 21:29:14 +02:00
|
|
|
List_item *values;
|
2005-08-12 16:57:19 +02:00
|
|
|
Name_resolution_context *context;
|
2005-11-28 20:57:50 +01:00
|
|
|
Name_resolution_context_state ctx_state;
|
2004-04-05 12:56:05 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
|
|
|
char *query= thd->query;
|
|
|
|
#endif
|
2002-11-16 19:19:10 +01:00
|
|
|
thr_lock_type lock_type = table_list->lock_type;
|
2004-12-30 23:44:00 +01:00
|
|
|
Item *unused_conds= 0;
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_ENTER("mysql_insert");
|
|
|
|
|
2000-11-18 07:35:40 +01:00
|
|
|
/*
|
|
|
|
in safe mode or with skip-new change delayed insert to be regular
|
|
|
|
if we are told to replace duplicates, the insert cannot be concurrent
|
|
|
|
delayed insert changed to regular in slave thread
|
|
|
|
*/
|
2004-03-29 16:57:07 +02:00
|
|
|
#ifdef EMBEDDED_LIBRARY
|
|
|
|
if (lock_type == TL_WRITE_DELAYED)
|
|
|
|
lock_type=TL_WRITE;
|
|
|
|
#else
|
2000-11-18 22:13:48 +01:00
|
|
|
if ((lock_type == TL_WRITE_DELAYED &&
|
|
|
|
((specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE)) ||
|
2004-03-04 18:58:36 +01:00
|
|
|
thd->slave_thread || !thd->variables.max_insert_delayed_threads)) ||
|
2002-12-02 20:38:00 +01:00
|
|
|
(lock_type == TL_WRITE_CONCURRENT_INSERT && duplic == DUP_REPLACE) ||
|
|
|
|
(duplic == DUP_UPDATE))
|
2000-07-31 21:29:14 +02:00
|
|
|
lock_type=TL_WRITE;
|
2004-03-29 16:57:07 +02:00
|
|
|
#endif
|
2002-11-26 00:00:05 +01:00
|
|
|
table_list->lock_type= lock_type;
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2004-03-29 16:57:07 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2000-07-31 21:29:14 +02:00
|
|
|
if (lock_type == TL_WRITE_DELAYED)
|
|
|
|
{
|
|
|
|
if (thd->locked_tables)
|
|
|
|
{
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
DBUG_ASSERT(table_list->db); /* Must be set in the parser */
|
|
|
|
if (find_locked_table(thd, table_list->db, table_list->table_name))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
|
2005-01-06 12:00:13 +01:00
|
|
|
table_list->table_name);
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
2003-01-30 21:15:44 +01:00
|
|
|
if ((table= delayed_get_table(thd,table_list)) && !thd->is_fatal_error)
|
2003-10-06 21:56:34 +02:00
|
|
|
{
|
2005-03-10 14:05:48 +01:00
|
|
|
/*
|
|
|
|
Open tables used for sub-selects or in stored functions, will also
|
|
|
|
cache these functions.
|
|
|
|
*/
|
|
|
|
res= open_and_lock_tables(thd, table_list->next_global);
|
2004-07-16 00:15:55 +02:00
|
|
|
/*
|
|
|
|
First is not processed by open_and_lock_tables() => we need set
|
|
|
|
updateability flags "by hands".
|
|
|
|
*/
|
|
|
|
if (!table_list->derived && !table_list->view)
|
|
|
|
table_list->updatable= 1; // usual table
|
2003-10-06 21:56:34 +02:00
|
|
|
}
|
2006-04-04 23:09:07 +02:00
|
|
|
else if (thd->net.last_errno != ER_WRONG_OBJECT)
|
2003-08-22 18:21:23 +02:00
|
|
|
{
|
2003-10-06 21:56:34 +02:00
|
|
|
/* Too many delayed insert threads; Use a normal insert */
|
|
|
|
table_list->lock_type= lock_type= TL_WRITE;
|
2002-11-26 00:00:05 +01:00
|
|
|
res= open_and_lock_tables(thd, table_list);
|
2003-08-22 18:21:23 +02:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
else
|
2004-03-29 16:57:07 +02:00
|
|
|
#endif /* EMBEDDED_LIBRARY */
|
2002-11-26 00:00:05 +01:00
|
|
|
res= open_and_lock_tables(thd, table_list);
|
2004-05-26 18:12:49 +02:00
|
|
|
if (res || thd->is_fatal_error)
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2002-11-26 00:00:05 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->proc_info="init";
|
2000-10-03 13:18:03 +02:00
|
|
|
thd->used_tables=0;
|
2000-07-31 21:29:14 +02:00
|
|
|
values= its++;
|
2003-05-03 01:16:56 +02:00
|
|
|
|
2005-07-04 02:24:25 +02:00
|
|
|
if (mysql_prepare_insert(thd, table_list, table, fields, values,
|
2004-12-30 23:44:00 +01:00
|
|
|
update_fields, update_values, duplic, &unused_conds,
|
|
|
|
FALSE))
|
2000-07-31 21:29:14 +02:00
|
|
|
goto abort;
|
2002-11-30 18:26:18 +01:00
|
|
|
|
2004-09-15 22:42:56 +02:00
|
|
|
/* mysql_prepare_insert set table_list->table if it was not set */
|
|
|
|
table= table_list->table;
|
|
|
|
|
2005-08-12 16:57:19 +02:00
|
|
|
context= &thd->lex->select_lex.context;
|
2006-09-12 16:50:24 +02:00
|
|
|
/*
|
|
|
|
These three asserts test the hypothesis that the resetting of the name
|
|
|
|
resolution context below is not necessary at all since the list of local
|
|
|
|
tables for INSERT always consists of one table.
|
|
|
|
*/
|
|
|
|
DBUG_ASSERT(!table_list->next_local);
|
|
|
|
DBUG_ASSERT(!context->table_list->next_local);
|
|
|
|
DBUG_ASSERT(!context->first_name_resolution_table->next_name_resolution_table);
|
|
|
|
|
2005-08-12 16:57:19 +02:00
|
|
|
/* Save the state of the current name resolution context. */
|
2005-11-28 20:57:50 +01:00
|
|
|
ctx_state.save_state(context, table_list);
|
2005-08-12 16:57:19 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Perform name resolution only in the first table - 'table_list',
|
|
|
|
which is the table that is inserted into.
|
|
|
|
*/
|
2005-07-01 06:05:42 +02:00
|
|
|
table_list->next_local= 0;
|
2005-08-12 16:57:19 +02:00
|
|
|
context->resolve_in_table_list_only(table_list);
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
value_count= values->elements;
|
2002-11-26 00:00:05 +01:00
|
|
|
while ((values= its++))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
counter++;
|
|
|
|
if (values->elements != value_count)
|
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
|
2000-07-31 21:29:14 +02:00
|
|
|
goto abort;
|
|
|
|
}
|
2005-07-01 06:05:42 +02:00
|
|
|
if (setup_fields(thd, 0, *values, 0, 0, 0))
|
2000-07-31 21:29:14 +02:00
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
its.rewind ();
|
2005-08-12 16:57:19 +02:00
|
|
|
|
|
|
|
/* Restore the current context. */
|
2005-11-28 20:57:50 +01:00
|
|
|
ctx_state.restore_state(context, table_list);
|
2005-08-12 16:57:19 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
/*
|
2002-07-25 00:00:56 +02:00
|
|
|
Fill in the given fields and dump it to the table file
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
2004-03-15 18:36:16 +01:00
|
|
|
info.records= info.deleted= info.copied= info.updated= 0;
|
2004-12-31 11:04:35 +01:00
|
|
|
info.ignore= ignore;
|
2000-07-31 21:29:14 +02:00
|
|
|
info.handle_duplicates=duplic;
|
2004-12-31 11:04:35 +01:00
|
|
|
info.update_fields= &update_fields;
|
|
|
|
info.update_values= &update_values;
|
2004-09-29 15:35:01 +02:00
|
|
|
info.view= (table_list->view ? table_list : 0);
|
2005-01-03 22:04:52 +01:00
|
|
|
|
2003-10-11 22:26:39 +02:00
|
|
|
/*
|
|
|
|
Count warnings for all inserts.
|
|
|
|
For single line insert, generate an error if try to set a NOT NULL field
|
2004-10-02 21:20:08 +02:00
|
|
|
to NULL.
|
2003-10-11 22:26:39 +02:00
|
|
|
*/
|
2004-10-02 21:20:08 +02:00
|
|
|
thd->count_cuted_fields= ((values_list.elements == 1 &&
|
2005-01-04 12:46:53 +01:00
|
|
|
!ignore) ?
|
2003-10-11 22:26:39 +02:00
|
|
|
CHECK_FIELD_ERROR_FOR_NULL :
|
|
|
|
CHECK_FIELD_WARN);
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->cuted_fields = 0L;
|
|
|
|
table->next_number_field=table->found_next_number_field;
|
|
|
|
|
|
|
|
error=0;
|
|
|
|
id=0;
|
|
|
|
thd->proc_info="update";
|
2005-01-10 20:55:05 +01:00
|
|
|
if (duplic != DUP_ERROR || ignore)
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
2006-07-01 23:51:10 +02:00
|
|
|
if (duplic == DUP_REPLACE)
|
|
|
|
{
|
|
|
|
if (!table->triggers || !table->triggers->has_delete_triggers())
|
|
|
|
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
|
|
|
|
/*
|
|
|
|
REPLACE should change values of all columns so we should mark
|
|
|
|
all columns as columns to be set. As nice side effect we will
|
|
|
|
retrieve columns which values are needed for ON DELETE triggers.
|
|
|
|
*/
|
|
|
|
table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
|
|
|
|
}
|
2004-04-08 20:41:00 +02:00
|
|
|
/*
|
|
|
|
let's *try* to start bulk inserts. It won't necessary
|
|
|
|
start them as values_list.elements should be greater than
|
|
|
|
some - handler dependent - threshold.
|
2006-03-29 12:53:00 +02:00
|
|
|
We should not start bulk inserts if this statement uses
|
|
|
|
functions or invokes triggers since they may access
|
|
|
|
to the same table and therefore should not see its
|
|
|
|
inconsistent state created by this optimization.
|
2004-04-08 20:41:00 +02:00
|
|
|
So we call start_bulk_insert to perform nesessary checks on
|
|
|
|
values_list.elements, and - if nothing else - to initialize
|
|
|
|
the code to make the call of end_bulk_insert() below safe.
|
|
|
|
*/
|
2006-03-29 12:53:00 +02:00
|
|
|
if (lock_type != TL_WRITE_DELAYED && !thd->prelocked_mode)
|
2004-04-06 21:35:26 +02:00
|
|
|
table->file->start_bulk_insert(values_list.elements);
|
2001-09-02 12:47:00 +02:00
|
|
|
|
2004-09-28 19:08:00 +02:00
|
|
|
thd->no_trans_update= 0;
|
2005-01-04 12:46:53 +01:00
|
|
|
thd->abort_on_warning= (!ignore &&
|
2004-09-28 19:08:00 +02:00
|
|
|
(thd->variables.sql_mode &
|
|
|
|
(MODE_STRICT_TRANS_TABLES |
|
|
|
|
MODE_STRICT_ALL_TABLES)));
|
|
|
|
|
2005-01-14 03:23:34 +01:00
|
|
|
if ((fields.elements || !value_count) &&
|
2005-07-01 06:05:42 +02:00
|
|
|
check_that_all_fields_are_given_values(thd, table, table_list))
|
2004-09-28 19:08:00 +02:00
|
|
|
{
|
|
|
|
/* thd->net.report_error is now set, which will abort the next loop */
|
|
|
|
error= 1;
|
|
|
|
}
|
|
|
|
|
2006-07-01 23:51:10 +02:00
|
|
|
mark_fields_used_by_triggers_for_insert_stmt(thd, table, duplic);
|
|
|
|
|
2005-07-01 06:05:42 +02:00
|
|
|
if (table_list->prepare_where(thd, 0, TRUE) ||
|
|
|
|
table_list->prepare_check_option(thd))
|
|
|
|
error= 1;
|
|
|
|
|
2002-07-25 00:00:56 +02:00
|
|
|
while ((values= its++))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
if (fields.elements || !value_count)
|
|
|
|
{
|
2005-01-06 12:00:13 +01:00
|
|
|
restore_record(table,s->default_values); // Get empty record
|
2005-05-24 20:19:33 +02:00
|
|
|
if (fill_record_n_invoke_before_triggers(thd, fields, *values, 0,
|
|
|
|
table->triggers,
|
|
|
|
TRG_EVENT_INSERT))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2002-11-30 23:11:22 +01:00
|
|
|
if (values_list.elements != 1 && !thd->net.report_error)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
info.records++;
|
|
|
|
continue;
|
|
|
|
}
|
2004-10-20 03:04:37 +02:00
|
|
|
/*
|
|
|
|
TODO: set thd->abort_on_warning if values_list.elements == 1
|
|
|
|
and check that all items return warning in case of problem with
|
|
|
|
storing field.
|
|
|
|
*/
|
2000-07-31 21:29:14 +02:00
|
|
|
error=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-10-03 13:18:03 +02:00
|
|
|
if (thd->used_tables) // Column used in values()
|
2005-01-06 12:00:13 +01:00
|
|
|
restore_record(table,s->default_values); // Get empty record
|
2000-10-03 13:18:03 +02:00
|
|
|
else
|
2005-05-24 20:19:33 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Fix delete marker. No need to restore rest of record since it will
|
|
|
|
be overwritten by fill_record() anyway (and fill_record() does not
|
|
|
|
use default values in this case).
|
|
|
|
*/
|
|
|
|
table->record[0][0]= table->s->default_values[0];
|
|
|
|
}
|
|
|
|
if (fill_record_n_invoke_before_triggers(thd, table->field, *values, 0,
|
|
|
|
table->triggers,
|
|
|
|
TRG_EVENT_INSERT))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2002-11-30 23:11:22 +01:00
|
|
|
if (values_list.elements != 1 && ! thd->net.report_error)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
info.records++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
error=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-10-21 16:05:45 +02:00
|
|
|
|
2004-10-21 13:32:10 +02:00
|
|
|
if ((res= table_list->view_check_option(thd,
|
|
|
|
(values_list.elements == 1 ?
|
|
|
|
0 :
|
2005-01-03 22:04:52 +01:00
|
|
|
ignore))) ==
|
2004-09-29 15:35:01 +02:00
|
|
|
VIEW_CHECK_SKIP)
|
|
|
|
continue;
|
|
|
|
else if (res == VIEW_CHECK_ERROR)
|
2004-09-03 14:18:40 +02:00
|
|
|
{
|
2004-09-29 15:35:01 +02:00
|
|
|
error= 1;
|
|
|
|
break;
|
2004-09-03 14:18:40 +02:00
|
|
|
}
|
2004-03-29 16:57:07 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2001-04-09 20:08:56 +02:00
|
|
|
if (lock_type == TL_WRITE_DELAYED)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-12-31 11:04:35 +01:00
|
|
|
error=write_delayed(thd, table, duplic, ignore, query, thd->query_length, log_on);
|
2000-07-31 21:29:14 +02:00
|
|
|
query=0;
|
|
|
|
}
|
|
|
|
else
|
2004-03-29 16:57:07 +02:00
|
|
|
#endif
|
2004-09-28 19:08:00 +02:00
|
|
|
error=write_record(thd, table ,&info);
|
2000-07-31 21:29:14 +02:00
|
|
|
/*
|
2006-10-02 12:28:23 +02:00
|
|
|
If auto_increment values are used, save the first one for
|
|
|
|
LAST_INSERT_ID() and for the update log.
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
|
|
|
if (! id && thd->insert_id_used)
|
|
|
|
{ // Get auto increment value
|
|
|
|
id= thd->last_insert_id;
|
|
|
|
}
|
2004-09-15 21:10:31 +02:00
|
|
|
if (error)
|
|
|
|
break;
|
2003-04-30 09:02:28 +02:00
|
|
|
thd->row_count++;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2003-06-15 11:34:04 +02:00
|
|
|
|
2005-12-08 21:33:33 +01:00
|
|
|
free_underlaid_joins(thd, &thd->lex->select_lex);
|
|
|
|
joins_freed= TRUE;
|
|
|
|
|
2003-06-15 11:34:04 +02:00
|
|
|
/*
|
|
|
|
Now all rows are inserted. Time to update logs and sends response to
|
|
|
|
user
|
|
|
|
*/
|
2004-03-29 16:57:07 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2000-07-31 21:29:14 +02:00
|
|
|
if (lock_type == TL_WRITE_DELAYED)
|
|
|
|
{
|
2001-03-08 20:49:15 +01:00
|
|
|
if (!error)
|
|
|
|
{
|
|
|
|
id=0; // No auto_increment id
|
|
|
|
info.copied=values_list.elements;
|
|
|
|
end_delayed_insert(thd);
|
|
|
|
}
|
2002-11-16 19:19:10 +01:00
|
|
|
query_cache_invalidate3(thd, table_list, 1);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
else
|
2004-03-29 16:57:07 +02:00
|
|
|
#endif
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2006-03-29 12:53:00 +02:00
|
|
|
if (!thd->prelocked_mode && table->file->end_bulk_insert() && !error)
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
2004-04-07 16:04:28 +02:00
|
|
|
table->file->print_error(my_errno,MYF(0));
|
|
|
|
error=1;
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
if (id && values_list.elements != 1)
|
|
|
|
thd->insert_id(id); // For update log
|
2003-06-15 11:34:04 +02:00
|
|
|
else if (table->next_number_field && info.copied)
|
2000-07-31 21:29:14 +02:00
|
|
|
id=table->next_number_field->val_int(); // Return auto_increment value
|
2003-05-03 01:16:56 +02:00
|
|
|
|
2002-11-07 03:02:37 +01:00
|
|
|
transactional_table= table->file->has_transactions();
|
2003-05-26 18:10:43 +02:00
|
|
|
|
2006-03-15 18:15:52 +01:00
|
|
|
if ((changed= (info.copied || info.deleted || info.updated)))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2006-03-15 18:15:52 +01:00
|
|
|
/*
|
|
|
|
Invalidate the table in the query cache if something changed.
|
|
|
|
For the transactional algorithm to work the invalidation must be
|
|
|
|
before binlog writing and ha_autocommit_or_rollback
|
|
|
|
*/
|
|
|
|
query_cache_invalidate3(thd, table_list, 1);
|
|
|
|
if (error <= 0 || !transactional_table)
|
2000-09-16 03:27:21 +02:00
|
|
|
{
|
2006-03-15 18:15:52 +01:00
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
|
|
|
if (error <= 0)
|
|
|
|
thd->clear_error();
|
|
|
|
Query_log_event qinfo(thd, thd->query, thd->query_length,
|
|
|
|
transactional_table, FALSE);
|
|
|
|
if (mysql_bin_log.write(&qinfo) && transactional_table)
|
|
|
|
error=1;
|
|
|
|
}
|
|
|
|
if (!transactional_table)
|
|
|
|
thd->options|=OPTION_STATUS_NO_TRANS_UPDATE;
|
2000-09-16 03:27:21 +02:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2002-11-07 03:02:37 +01:00
|
|
|
if (transactional_table)
|
2000-12-07 13:08:48 +01:00
|
|
|
error=ha_autocommit_or_rollback(thd,error);
|
2002-11-16 19:19:10 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (thd->lock)
|
|
|
|
{
|
|
|
|
mysql_unlock_tables(thd, thd->lock);
|
2006-03-15 18:15:52 +01:00
|
|
|
/*
|
|
|
|
Invalidate the table in the query cache if something changed
|
|
|
|
after unlocking when changes become fisible.
|
|
|
|
TODO: this is workaround. right way will be move invalidating in
|
|
|
|
the unlock procedure.
|
|
|
|
*/
|
|
|
|
if (lock_type == TL_WRITE_CONCURRENT_INSERT && changed)
|
|
|
|
{
|
|
|
|
query_cache_invalidate3(thd, table_list, 1);
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->lock=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thd->proc_info="end";
|
|
|
|
table->next_number_field=0;
|
2003-10-11 22:26:39 +02:00
|
|
|
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->next_insert_id=0; // Reset this if wrongly used
|
2005-01-10 20:55:05 +01:00
|
|
|
if (duplic != DUP_ERROR || ignore)
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
|
2006-07-01 23:51:10 +02:00
|
|
|
if (duplic == DUP_REPLACE &&
|
|
|
|
(!table->triggers || !table->triggers->has_delete_triggers()))
|
|
|
|
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
|
2003-06-15 11:34:04 +02:00
|
|
|
|
|
|
|
/* Reset value of LAST_INSERT_ID if no rows where inserted */
|
|
|
|
if (!info.copied && thd->insert_id_used)
|
|
|
|
{
|
|
|
|
thd->insert_id(0);
|
|
|
|
id=0;
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
if (error)
|
|
|
|
goto abort;
|
|
|
|
if (values_list.elements == 1 && (!(thd->options & OPTION_WARNINGS) ||
|
|
|
|
!thd->cuted_fields))
|
2004-05-04 13:45:20 +02:00
|
|
|
{
|
|
|
|
thd->row_count_func= info.copied+info.deleted+info.updated;
|
2004-09-03 20:43:04 +02:00
|
|
|
send_ok(thd, (ulong) thd->row_count_func, id);
|
2004-05-04 13:45:20 +02:00
|
|
|
}
|
2000-11-24 00:51:18 +01:00
|
|
|
else
|
|
|
|
{
|
2000-07-31 21:29:14 +02:00
|
|
|
char buff[160];
|
2004-12-31 11:04:35 +01:00
|
|
|
if (ignore)
|
2003-07-14 12:32:31 +02:00
|
|
|
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
|
|
|
|
(lock_type == TL_WRITE_DELAYED) ? (ulong) 0 :
|
|
|
|
(ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
|
2000-07-31 21:29:14 +02:00
|
|
|
else
|
2003-07-14 12:32:31 +02:00
|
|
|
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
|
2004-05-19 04:09:10 +02:00
|
|
|
(ulong) (info.deleted+info.updated), (ulong) thd->cuted_fields);
|
2004-05-04 13:45:20 +02:00
|
|
|
thd->row_count_func= info.copied+info.deleted+info.updated;
|
2004-09-03 20:43:04 +02:00
|
|
|
::send_ok(thd, (ulong) thd->row_count_func, id, buff);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-10-05 00:05:15 +02:00
|
|
|
thd->abort_on_warning= 0;
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(FALSE);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
abort:
|
2004-03-29 16:57:07 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2000-07-31 21:29:14 +02:00
|
|
|
if (lock_type == TL_WRITE_DELAYED)
|
|
|
|
end_delayed_insert(thd);
|
2004-03-29 16:57:07 +02:00
|
|
|
#endif
|
2005-12-08 21:33:33 +01:00
|
|
|
if (!joins_freed)
|
|
|
|
free_underlaid_joins(thd, &thd->lex->select_lex);
|
2004-09-28 19:08:00 +02:00
|
|
|
thd->abort_on_warning= 0;
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
/*
|
|
|
|
Additional check for insertability for VIEW
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
check_view_insertability()
|
2005-07-01 06:05:42 +02:00
|
|
|
thd - thread handler
|
2004-07-16 00:15:55 +02:00
|
|
|
view - reference on VIEW
|
|
|
|
|
2004-09-03 20:43:04 +02:00
|
|
|
IMPLEMENTATION
|
|
|
|
A view is insertable if the folloings are true:
|
|
|
|
- All columns in the view are columns from a table
|
|
|
|
- All not used columns in table have a default values
|
|
|
|
- All field in view are unique (not referring to the same column)
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
RETURN
|
|
|
|
FALSE - OK
|
2004-09-03 20:43:04 +02:00
|
|
|
view->contain_auto_increment is 1 if and only if the view contains an
|
|
|
|
auto_increment field
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
TRUE - can't be used for insert
|
|
|
|
*/
|
|
|
|
|
2005-07-01 06:05:42 +02:00
|
|
|
static bool check_view_insertability(THD * thd, TABLE_LIST *view)
|
2004-07-16 00:15:55 +02:00
|
|
|
{
|
2004-09-03 20:43:04 +02:00
|
|
|
uint num= view->view->select_lex.item_list.elements;
|
2004-07-16 00:15:55 +02:00
|
|
|
TABLE *table= view->table;
|
2004-09-14 18:28:29 +02:00
|
|
|
Field_translator *trans_start= view->field_translation,
|
|
|
|
*trans_end= trans_start + num;
|
|
|
|
Field_translator *trans;
|
2004-07-16 00:15:55 +02:00
|
|
|
Field **field_ptr= table->field;
|
2005-07-01 06:05:42 +02:00
|
|
|
uint used_fields_buff_size= (table->s->fields + 7) / 8;
|
|
|
|
uchar *used_fields_buff= (uchar*)thd->alloc(used_fields_buff_size);
|
|
|
|
MY_BITMAP used_fields;
|
2006-07-04 11:10:12 +02:00
|
|
|
bool save_set_query_id= thd->set_query_id;
|
2004-09-03 20:43:04 +02:00
|
|
|
DBUG_ENTER("check_key_in_view");
|
|
|
|
|
2005-07-01 06:05:42 +02:00
|
|
|
if (!used_fields_buff)
|
|
|
|
DBUG_RETURN(TRUE); // EOM
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
DBUG_ASSERT(view->table != 0 && view->field_translation != 0);
|
|
|
|
|
2006-01-03 17:54:54 +01:00
|
|
|
VOID(bitmap_init(&used_fields, used_fields_buff, used_fields_buff_size * 8,
|
|
|
|
0));
|
2005-07-01 06:05:42 +02:00
|
|
|
bitmap_clear_all(&used_fields);
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
view->contain_auto_increment= 0;
|
2006-07-04 11:10:12 +02:00
|
|
|
/*
|
|
|
|
we must not set query_id for fields as they're not
|
|
|
|
really used in this context
|
|
|
|
*/
|
|
|
|
thd->set_query_id= 0;
|
2004-07-16 00:15:55 +02:00
|
|
|
/* check simplicity and prepare unique test of view */
|
2004-09-03 20:43:04 +02:00
|
|
|
for (trans= trans_start; trans != trans_end; trans++)
|
2004-07-16 00:15:55 +02:00
|
|
|
{
|
2005-07-01 06:05:42 +02:00
|
|
|
if (!trans->item->fixed && trans->item->fix_fields(thd, &trans->item))
|
2006-07-04 11:10:12 +02:00
|
|
|
{
|
|
|
|
thd->set_query_id= save_set_query_id;
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
2004-09-03 20:43:04 +02:00
|
|
|
Item_field *field;
|
2004-07-16 00:15:55 +02:00
|
|
|
/* simple SELECT list entry (field without expression) */
|
2004-10-05 12:41:51 +02:00
|
|
|
if (!(field= trans->item->filed_for_view_update()))
|
2006-07-04 11:10:12 +02:00
|
|
|
{
|
|
|
|
thd->set_query_id= save_set_query_id;
|
2004-07-16 00:15:55 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2006-07-04 11:10:12 +02:00
|
|
|
}
|
2004-09-03 20:43:04 +02:00
|
|
|
if (field->field->unireg_check == Field::NEXT_NUMBER)
|
2004-07-16 00:15:55 +02:00
|
|
|
view->contain_auto_increment= 1;
|
|
|
|
/* prepare unique test */
|
2004-10-05 14:07:31 +02:00
|
|
|
/*
|
|
|
|
remove collation (or other transparent for update function) if we have
|
|
|
|
it
|
|
|
|
*/
|
|
|
|
trans->item= field;
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
2006-07-04 11:10:12 +02:00
|
|
|
thd->set_query_id= save_set_query_id;
|
2004-07-16 00:15:55 +02:00
|
|
|
/* unique test */
|
2004-09-03 20:43:04 +02:00
|
|
|
for (trans= trans_start; trans != trans_end; trans++)
|
2004-07-16 00:15:55 +02:00
|
|
|
{
|
2004-09-03 20:43:04 +02:00
|
|
|
/* Thanks to test above, we know that all columns are of type Item_field */
|
2004-09-14 18:28:29 +02:00
|
|
|
Item_field *field= (Item_field *)trans->item;
|
2005-07-01 06:05:42 +02:00
|
|
|
/* check fields belong to table in which we are inserting */
|
|
|
|
if (field->field->table == table &&
|
|
|
|
bitmap_fast_test_and_set(&used_fields, field->field->field_index))
|
2004-07-16 00:15:55 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-10 00:14:32 +02:00
|
|
|
/*
|
2004-09-03 20:43:04 +02:00
|
|
|
Check if table can be updated
|
2004-04-10 00:14:32 +02:00
|
|
|
|
|
|
|
SYNOPSIS
|
2004-09-03 20:43:04 +02:00
|
|
|
mysql_prepare_insert_check_table()
|
|
|
|
thd Thread handle
|
2004-11-25 01:23:13 +01:00
|
|
|
table_list Table list
|
2004-09-03 20:43:04 +02:00
|
|
|
fields List of fields to be updated
|
|
|
|
where Pointer to where clause
|
2004-11-25 01:23:13 +01:00
|
|
|
select_insert Check is making for SELECT ... INSERT
|
2004-09-03 20:43:04 +02:00
|
|
|
|
|
|
|
RETURN
|
2004-11-25 01:23:13 +01:00
|
|
|
FALSE ok
|
|
|
|
TRUE ERROR
|
2004-04-10 00:14:32 +02:00
|
|
|
*/
|
2004-09-03 20:43:04 +02:00
|
|
|
|
2004-11-25 01:23:13 +01:00
|
|
|
static bool mysql_prepare_insert_check_table(THD *thd, TABLE_LIST *table_list,
|
|
|
|
List<Item> &fields, COND **where,
|
|
|
|
bool select_insert)
|
2004-04-10 00:14:32 +02:00
|
|
|
{
|
2004-07-16 00:15:55 +02:00
|
|
|
bool insert_into_view= (table_list->view != 0);
|
2004-09-03 20:43:04 +02:00
|
|
|
DBUG_ENTER("mysql_prepare_insert_check_table");
|
2004-07-16 00:15:55 +02:00
|
|
|
|
2006-07-19 11:49:07 +02:00
|
|
|
/*
|
|
|
|
first table in list is the one we'll INSERT into, requires INSERT_ACL.
|
|
|
|
all others require SELECT_ACL only. the ACL requirement below is for
|
|
|
|
new leaves only anyway (view-constituents), so check for SELECT rather
|
|
|
|
than INSERT.
|
|
|
|
*/
|
|
|
|
|
2006-05-26 10:47:53 +02:00
|
|
|
if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
|
|
|
|
&thd->lex->select_lex.top_join_list,
|
|
|
|
table_list, where,
|
|
|
|
&thd->lex->select_lex.leaf_tables,
|
2006-08-15 19:45:24 +02:00
|
|
|
select_insert, INSERT_ACL, SELECT_ACL))
|
2004-11-25 01:23:13 +01:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-07-16 00:15:55 +02:00
|
|
|
|
|
|
|
if (insert_into_view && !fields.elements)
|
|
|
|
{
|
|
|
|
thd->lex->empty_field_list_on_rset= 1;
|
2004-09-15 22:42:56 +02:00
|
|
|
if (!table_list->table)
|
|
|
|
{
|
|
|
|
my_error(ER_VIEW_NO_INSERT_FIELD_LIST, MYF(0),
|
|
|
|
table_list->view_db.str, table_list->view_name.str);
|
2004-11-25 01:23:13 +01:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-09-15 22:42:56 +02:00
|
|
|
}
|
2005-07-01 06:05:42 +02:00
|
|
|
DBUG_RETURN(insert_view_fields(thd, &fields, table_list));
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
|
|
|
|
2004-11-25 01:23:13 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2004-09-03 20:43:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Prepare items in INSERT statement
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_prepare_insert()
|
|
|
|
thd Thread handler
|
|
|
|
table_list Global/local table list
|
2005-07-03 13:17:52 +02:00
|
|
|
table Table to insert into (can be NULL if table should
|
|
|
|
be taken from table_list->table)
|
2004-12-30 23:44:00 +01:00
|
|
|
where Where clause (for insert ... select)
|
|
|
|
select_insert TRUE if INSERT ... SELECT statement
|
2004-09-03 20:43:04 +02:00
|
|
|
|
2005-06-27 15:46:41 +02:00
|
|
|
TODO (in far future)
|
|
|
|
In cases of:
|
|
|
|
INSERT INTO t1 SELECT a, sum(a) as sum1 from t2 GROUP BY a
|
|
|
|
ON DUPLICATE KEY ...
|
|
|
|
we should be able to refer to sum1 in the ON DUPLICATE KEY part
|
2004-04-10 00:14:32 +02:00
|
|
|
|
2005-06-28 14:06:16 +02:00
|
|
|
WARNING
|
|
|
|
You MUST set table->insert_values to 0 after calling this function
|
|
|
|
before releasing the table object.
|
2005-07-03 13:17:52 +02:00
|
|
|
|
2004-09-03 20:43:04 +02:00
|
|
|
RETURN VALUE
|
2004-10-20 03:04:37 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE error
|
2004-09-03 20:43:04 +02:00
|
|
|
*/
|
|
|
|
|
2005-07-03 13:17:52 +02:00
|
|
|
bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
|
2005-07-04 02:24:25 +02:00
|
|
|
TABLE *table, List<Item> &fields, List_item *values,
|
2004-12-22 12:54:39 +01:00
|
|
|
List<Item> &update_fields, List<Item> &update_values,
|
2004-12-30 23:44:00 +01:00
|
|
|
enum_duplicates duplic,
|
|
|
|
COND **where, bool select_insert)
|
2004-09-03 20:43:04 +02:00
|
|
|
{
|
2005-07-04 02:24:25 +02:00
|
|
|
SELECT_LEX *select_lex= &thd->lex->select_lex;
|
2005-08-12 16:57:19 +02:00
|
|
|
Name_resolution_context *context= &select_lex->context;
|
2005-11-28 20:57:50 +01:00
|
|
|
Name_resolution_context_state ctx_state;
|
2004-09-03 20:43:04 +02:00
|
|
|
bool insert_into_view= (table_list->view != 0);
|
2005-07-04 02:24:25 +02:00
|
|
|
bool res= 0;
|
2007-01-22 13:14:38 +01:00
|
|
|
table_map map= 0;
|
2004-09-03 20:43:04 +02:00
|
|
|
DBUG_ENTER("mysql_prepare_insert");
|
2004-11-25 01:23:13 +01:00
|
|
|
DBUG_PRINT("enter", ("table_list 0x%lx, table 0x%lx, view %d",
|
|
|
|
(ulong)table_list, (ulong)table,
|
|
|
|
(int)insert_into_view));
|
2004-12-22 12:54:39 +01:00
|
|
|
|
2005-07-01 06:05:42 +02:00
|
|
|
/*
|
|
|
|
For subqueries in VALUES() we should not see the table in which we are
|
|
|
|
inserting (for INSERT ... SELECT this is done by changing table_list,
|
|
|
|
because INSERT ... SELECT share SELECT_LEX it with SELECT.
|
|
|
|
*/
|
|
|
|
if (!select_insert)
|
|
|
|
{
|
2005-07-03 13:17:52 +02:00
|
|
|
for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit();
|
2005-07-01 06:05:42 +02:00
|
|
|
un;
|
|
|
|
un= un->next_unit())
|
|
|
|
{
|
|
|
|
for (SELECT_LEX *sl= un->first_select();
|
|
|
|
sl;
|
|
|
|
sl= sl->next_select())
|
|
|
|
{
|
|
|
|
sl->context.outer_context= 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-22 12:54:39 +01:00
|
|
|
if (duplic == DUP_UPDATE)
|
2004-12-13 13:26:28 +01:00
|
|
|
{
|
|
|
|
/* it should be allocated before Item::fix_fields() */
|
2004-12-22 12:54:39 +01:00
|
|
|
if (table_list->set_insert_values(thd->mem_root))
|
2004-12-30 23:44:00 +01:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-12-13 13:26:28 +01:00
|
|
|
}
|
2004-12-22 12:54:39 +01:00
|
|
|
|
2004-12-30 23:44:00 +01:00
|
|
|
if (mysql_prepare_insert_check_table(thd, table_list, fields, where,
|
|
|
|
select_insert))
|
2004-11-25 01:23:13 +01:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-07-16 00:15:55 +02:00
|
|
|
|
2005-08-12 16:57:19 +02:00
|
|
|
/* Save the state of the current name resolution context. */
|
2005-11-28 20:57:50 +01:00
|
|
|
ctx_state.save_state(context, table_list);
|
2005-07-03 13:17:52 +02:00
|
|
|
|
2005-08-12 16:57:19 +02:00
|
|
|
/*
|
|
|
|
Perform name resolution only in the first table - 'table_list',
|
|
|
|
which is the table that is inserted into.
|
|
|
|
*/
|
2005-02-17 18:38:13 +01:00
|
|
|
table_list->next_local= 0;
|
2005-08-12 16:57:19 +02:00
|
|
|
context->resolve_in_table_list_only(table_list);
|
|
|
|
|
|
|
|
/* Prepare the fields in the statement. */
|
2005-08-10 23:17:53 +02:00
|
|
|
if (values &&
|
2005-08-12 18:27:54 +02:00
|
|
|
!(res= check_insert_fields(thd, context->table_list, fields, *values,
|
2007-01-22 13:14:38 +01:00
|
|
|
!insert_into_view, &map) ||
|
2005-08-12 18:27:54 +02:00
|
|
|
setup_fields(thd, 0, *values, 0, 0, 0)) &&
|
2005-08-10 23:17:53 +02:00
|
|
|
duplic == DUP_UPDATE)
|
2004-04-10 00:14:32 +02:00
|
|
|
{
|
2005-07-03 13:17:52 +02:00
|
|
|
select_lex->no_wrap_view_item= TRUE;
|
2007-01-22 13:14:38 +01:00
|
|
|
res= check_update_fields(thd, context->table_list, update_fields, &map);
|
2005-07-03 13:17:52 +02:00
|
|
|
select_lex->no_wrap_view_item= FALSE;
|
2005-08-12 18:27:54 +02:00
|
|
|
/*
|
|
|
|
When we are not using GROUP BY we can refer to other tables in the
|
|
|
|
ON DUPLICATE KEY part.
|
|
|
|
*/
|
2005-07-04 02:24:25 +02:00
|
|
|
if (select_lex->group_list.elements == 0)
|
|
|
|
{
|
2005-11-28 20:57:50 +01:00
|
|
|
context->table_list->next_local= ctx_state.save_next_local;
|
2005-08-18 02:12:42 +02:00
|
|
|
/* first_name_resolution_table was set by resolve_in_table_list_only() */
|
2005-08-12 16:57:19 +02:00
|
|
|
context->first_name_resolution_table->
|
2005-11-28 20:57:50 +01:00
|
|
|
next_name_resolution_table= ctx_state.save_next_local;
|
2005-07-04 02:24:25 +02:00
|
|
|
}
|
|
|
|
if (!res)
|
|
|
|
res= setup_fields(thd, 0, update_values, 1, 0, 0);
|
2005-07-03 13:17:52 +02:00
|
|
|
}
|
2005-08-12 16:57:19 +02:00
|
|
|
|
|
|
|
/* Restore the current context. */
|
2005-11-28 20:57:50 +01:00
|
|
|
ctx_state.restore_state(context, table_list);
|
2005-08-12 16:57:19 +02:00
|
|
|
|
2005-07-03 13:17:52 +02:00
|
|
|
if (res)
|
|
|
|
DBUG_RETURN(res);
|
|
|
|
|
2004-11-25 01:23:13 +01:00
|
|
|
if (!table)
|
|
|
|
table= table_list->table;
|
|
|
|
|
2005-07-01 06:05:42 +02:00
|
|
|
if (!select_insert)
|
2004-04-10 00:14:32 +02:00
|
|
|
{
|
2005-07-01 06:05:42 +02:00
|
|
|
Item *fake_conds= 0;
|
2005-08-02 21:54:49 +02:00
|
|
|
TABLE_LIST *duplicate;
|
2005-12-20 16:35:05 +01:00
|
|
|
if ((duplicate= unique_table(thd, table_list, table_list->next_global)))
|
2005-07-01 06:05:42 +02:00
|
|
|
{
|
2005-08-02 21:54:49 +02:00
|
|
|
update_non_unique_table_error(table_list, "INSERT", duplicate);
|
2005-07-01 06:05:42 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
2006-09-16 18:50:48 +02:00
|
|
|
select_lex->fix_prepare_information(thd, &fake_conds, &fake_conds);
|
2005-07-03 13:17:52 +02:00
|
|
|
select_lex->first_execution= 0;
|
2004-04-10 00:14:32 +02:00
|
|
|
}
|
2004-11-18 12:11:56 +01:00
|
|
|
if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
|
|
|
|
table->file->extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY);
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(FALSE);
|
2004-04-10 00:14:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
/* Check if there is more uniq keys after field */
|
|
|
|
|
|
|
|
static int last_uniq_key(TABLE *table,uint keynr)
|
|
|
|
{
|
2005-01-06 12:00:13 +01:00
|
|
|
while (++keynr < table->s->keys)
|
2000-07-31 21:29:14 +02:00
|
|
|
if (table->key_info[keynr].flags & HA_NOSAME)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-05-24 20:19:33 +02:00
|
|
|
Write a record to table with optional deleting of conflicting records,
|
|
|
|
invoke proper triggers if needed.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
write_record()
|
|
|
|
thd - thread context
|
|
|
|
table - table to which record should be written
|
|
|
|
info - COPY_INFO structure describing handling of duplicates
|
|
|
|
and which is used for counting number of records inserted
|
|
|
|
and deleted.
|
2004-09-28 19:08:00 +02:00
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
NOTE
|
|
|
|
Once this record will be written to table after insert trigger will
|
|
|
|
be invoked. If instead of inserting new record we will update old one
|
|
|
|
then both on update triggers will work instead. Similarly both on
|
|
|
|
delete triggers will be invoked if we will delete conflicting records.
|
2004-09-28 19:08:00 +02:00
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
Sets thd->no_trans_update if table which is updated didn't have
|
|
|
|
transactions.
|
|
|
|
|
|
|
|
RETURN VALUE
|
|
|
|
0 - success
|
|
|
|
non-0 - error
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-09-28 19:08:00 +02:00
|
|
|
int write_record(THD *thd, TABLE *table,COPY_INFO *info)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-05-24 20:19:33 +02:00
|
|
|
int error, trg_error= 0;
|
2000-07-31 21:29:14 +02:00
|
|
|
char *key=0;
|
2004-04-28 02:37:45 +02:00
|
|
|
DBUG_ENTER("write_record");
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
info->records++;
|
2002-12-02 20:38:00 +01:00
|
|
|
if (info->handle_duplicates == DUP_REPLACE ||
|
|
|
|
info->handle_duplicates == DUP_UPDATE)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
while ((error=table->file->write_row(table->record[0])))
|
|
|
|
{
|
2004-09-15 21:10:31 +02:00
|
|
|
uint key_nr;
|
2001-11-06 23:13:29 +01:00
|
|
|
if (error != HA_WRITE_SKIP)
|
2000-07-31 21:29:14 +02:00
|
|
|
goto err;
|
|
|
|
if ((int) (key_nr = table->file->get_dup_key(error)) < 0)
|
|
|
|
{
|
2001-11-06 23:13:29 +01:00
|
|
|
error=HA_WRITE_SKIP; /* Database can't find key */
|
2000-07-31 21:29:14 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2001-03-06 14:24:08 +01:00
|
|
|
/*
|
|
|
|
Don't allow REPLACE to replace a row when a auto_increment column
|
|
|
|
was used. This ensures that we don't get a problem when the
|
|
|
|
whole range of the key has been used.
|
|
|
|
*/
|
2002-12-02 20:38:00 +01:00
|
|
|
if (info->handle_duplicates == DUP_REPLACE &&
|
|
|
|
table->next_number_field &&
|
2005-01-06 12:00:13 +01:00
|
|
|
key_nr == table->s->next_number_index &&
|
2001-03-06 14:24:08 +01:00
|
|
|
table->file->auto_increment_column_changed)
|
|
|
|
goto err;
|
2002-04-12 20:35:46 +02:00
|
|
|
if (table->file->table_flags() & HA_DUPP_POS)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
if (table->file->rnd_pos(table->record[1],table->file->dupp_ref))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-27 20:45:48 +02:00
|
|
|
if (table->file->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
error=my_errno;
|
|
|
|
goto err;
|
|
|
|
}
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!key)
|
|
|
|
{
|
2005-01-06 12:00:13 +01:00
|
|
|
if (!(key=(char*) my_safe_alloca(table->s->max_unique_length,
|
2000-07-31 21:29:14 +02:00
|
|
|
MAX_KEY_LENGTH)))
|
|
|
|
{
|
|
|
|
error=ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2004-08-27 15:37:13 +02:00
|
|
|
key_copy((byte*) key,table->record[0],table->key_info+key_nr,0);
|
2000-07-31 21:29:14 +02:00
|
|
|
if ((error=(table->file->index_read_idx(table->record[1],key_nr,
|
2000-12-22 15:19:54 +01:00
|
|
|
(byte*) key,
|
2003-05-21 20:39:58 +02:00
|
|
|
table->key_info[key_nr].
|
|
|
|
key_length,
|
2000-07-31 21:29:14 +02:00
|
|
|
HA_READ_KEY_EXACT))))
|
|
|
|
goto err;
|
|
|
|
}
|
2002-12-02 20:38:00 +01:00
|
|
|
if (info->handle_duplicates == DUP_UPDATE)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-09-29 15:35:01 +02:00
|
|
|
int res= 0;
|
2005-05-25 17:33:32 +02:00
|
|
|
/*
|
|
|
|
We don't check for other UNIQUE keys - the first row
|
|
|
|
that matches, is updated. If update causes a conflict again,
|
|
|
|
an error is returned
|
2002-12-02 20:38:00 +01:00
|
|
|
*/
|
2004-12-13 13:26:28 +01:00
|
|
|
DBUG_ASSERT(table->insert_values != NULL);
|
2003-05-03 01:16:56 +02:00
|
|
|
store_record(table,insert_values);
|
|
|
|
restore_record(table,record[1]);
|
2004-12-30 23:44:00 +01:00
|
|
|
DBUG_ASSERT(info->update_fields->elements ==
|
|
|
|
info->update_values->elements);
|
2005-05-24 20:19:33 +02:00
|
|
|
if (fill_record_n_invoke_before_triggers(thd, *info->update_fields,
|
|
|
|
*info->update_values, 0,
|
|
|
|
table->triggers,
|
|
|
|
TRG_EVENT_UPDATE))
|
|
|
|
goto before_trg_err;
|
2004-09-06 22:55:36 +02:00
|
|
|
|
|
|
|
/* CHECK OPTION for VIEW ... ON DUPLICATE KEY UPDATE ... */
|
2004-09-29 15:35:01 +02:00
|
|
|
if (info->view &&
|
|
|
|
(res= info->view->view_check_option(current_thd, info->ignore)) ==
|
|
|
|
VIEW_CHECK_SKIP)
|
2005-05-24 20:19:33 +02:00
|
|
|
goto ok_or_after_trg_err;
|
2004-12-30 23:44:00 +01:00
|
|
|
if (res == VIEW_CHECK_ERROR)
|
2005-05-24 20:19:33 +02:00
|
|
|
goto before_trg_err;
|
2004-09-06 22:55:36 +02:00
|
|
|
|
2002-12-02 20:38:00 +01:00
|
|
|
if ((error=table->file->update_row(table->record[1],table->record[0])))
|
2005-05-07 14:31:37 +02:00
|
|
|
{
|
|
|
|
if ((error == HA_ERR_FOUND_DUPP_KEY) && info->ignore)
|
2006-07-05 14:41:35 +02:00
|
|
|
{
|
|
|
|
table->file->restore_auto_increment();
|
2005-05-24 20:19:33 +02:00
|
|
|
goto ok_or_after_trg_err;
|
2006-07-05 14:41:35 +02:00
|
|
|
}
|
2002-12-02 20:38:00 +01:00
|
|
|
goto err;
|
2005-05-07 14:31:37 +02:00
|
|
|
}
|
2004-03-15 18:36:16 +01:00
|
|
|
info->updated++;
|
2005-05-24 20:19:33 +02:00
|
|
|
|
2006-07-05 14:41:35 +02:00
|
|
|
if (table->next_number_field)
|
|
|
|
table->file->adjust_next_insert_id_after_explicit_value(table->next_number_field->val_int());
|
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
trg_error= (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_UPDATE,
|
|
|
|
TRG_ACTION_AFTER, TRUE));
|
|
|
|
info->copied++;
|
|
|
|
goto ok_or_after_trg_err;
|
2002-12-02 20:38:00 +01:00
|
|
|
}
|
|
|
|
else /* DUP_REPLACE */
|
|
|
|
{
|
2004-02-11 00:06:46 +01:00
|
|
|
/*
|
|
|
|
The manual defines the REPLACE semantics that it is either
|
|
|
|
an INSERT or DELETE(s) + INSERT; FOREIGN KEY checks in
|
|
|
|
InnoDB do not function in the defined way if we allow MySQL
|
|
|
|
to convert the latter operation internally to an UPDATE.
|
2004-04-02 08:12:53 +02:00
|
|
|
We also should not perform this conversion if we have
|
|
|
|
timestamp field with ON UPDATE which is different from DEFAULT.
|
2006-06-16 18:21:25 +02:00
|
|
|
Another case when conversion should not be performed is when
|
|
|
|
we have ON DELETE trigger on table so user may notice that
|
|
|
|
we cheat here. Note that it is ok to do such conversion for
|
|
|
|
tables which have ON UPDATE but have no ON DELETE triggers,
|
|
|
|
we just should not expose this fact to users by invoking
|
|
|
|
ON UPDATE triggers.
|
2004-02-11 00:06:46 +01:00
|
|
|
*/
|
|
|
|
if (last_uniq_key(table,key_nr) &&
|
2004-04-02 08:12:53 +02:00
|
|
|
!table->file->referenced_by_foreign_key() &&
|
2004-10-01 16:54:06 +02:00
|
|
|
(table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
|
2006-06-16 18:21:25 +02:00
|
|
|
table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH) &&
|
|
|
|
(!table->triggers || !table->triggers->has_delete_triggers()))
|
2002-12-02 20:38:00 +01:00
|
|
|
{
|
2003-05-21 20:39:58 +02:00
|
|
|
if ((error=table->file->update_row(table->record[1],
|
|
|
|
table->record[0])))
|
2002-12-02 20:38:00 +01:00
|
|
|
goto err;
|
|
|
|
info->deleted++;
|
2006-06-16 18:21:25 +02:00
|
|
|
/*
|
|
|
|
Since we pretend that we have done insert we should call
|
|
|
|
its after triggers.
|
|
|
|
*/
|
|
|
|
goto after_trg_n_copied_inc;
|
2005-05-24 20:19:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_BEFORE, TRUE))
|
|
|
|
goto before_trg_err;
|
|
|
|
if ((error=table->file->delete_row(table->record[1])))
|
|
|
|
goto err;
|
|
|
|
info->deleted++;
|
|
|
|
if (!table->file->has_transactions())
|
|
|
|
thd->no_trans_update= 1;
|
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_AFTER, TRUE))
|
|
|
|
{
|
|
|
|
trg_error= 1;
|
|
|
|
goto ok_or_after_trg_err;
|
|
|
|
}
|
|
|
|
/* Let us attempt do write_row() once more */
|
2002-12-02 20:38:00 +01:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((error=table->file->write_row(table->record[0])))
|
|
|
|
{
|
2004-12-31 11:04:35 +01:00
|
|
|
if (!info->ignore ||
|
2000-07-31 21:29:14 +02:00
|
|
|
(error != HA_ERR_FOUND_DUPP_KEY && error != HA_ERR_FOUND_DUPP_UNIQUE))
|
|
|
|
goto err;
|
2004-09-15 21:10:31 +02:00
|
|
|
table->file->restore_auto_increment();
|
2006-06-16 18:21:25 +02:00
|
|
|
goto ok_or_after_trg_err;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2006-06-16 18:21:25 +02:00
|
|
|
|
|
|
|
after_trg_n_copied_inc:
|
|
|
|
info->copied++;
|
|
|
|
trg_error= (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_INSERT,
|
|
|
|
TRG_ACTION_AFTER, TRUE));
|
2005-05-24 20:19:33 +02:00
|
|
|
|
|
|
|
ok_or_after_trg_err:
|
2000-07-31 21:29:14 +02:00
|
|
|
if (key)
|
2005-01-06 12:00:13 +01:00
|
|
|
my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
|
2004-09-28 19:08:00 +02:00
|
|
|
if (!table->file->has_transactions())
|
|
|
|
thd->no_trans_update= 1;
|
2005-05-24 20:19:33 +02:00
|
|
|
DBUG_RETURN(trg_error);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
err:
|
2002-06-11 10:20:31 +02:00
|
|
|
info->last_errno= error;
|
2005-08-02 02:00:03 +02:00
|
|
|
/* current_select is NULL if this is a delayed insert */
|
|
|
|
if (thd->lex->current_select)
|
|
|
|
thd->lex->current_select->no_error= 0; // Give error
|
2000-07-31 21:29:14 +02:00
|
|
|
table->file->print_error(error,MYF(0));
|
2005-05-24 20:19:33 +02:00
|
|
|
|
|
|
|
before_trg_err:
|
2006-07-05 14:41:35 +02:00
|
|
|
table->file->restore_auto_increment();
|
2005-05-24 20:19:33 +02:00
|
|
|
if (key)
|
|
|
|
my_safe_afree(key, table->s->max_unique_length, MAX_KEY_LENGTH);
|
2004-04-28 02:37:45 +02:00
|
|
|
DBUG_RETURN(1);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2002-07-25 00:00:56 +02:00
|
|
|
Check that all fields with arn't null_fields are used
|
2000-07-31 21:29:14 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2005-07-01 06:05:42 +02:00
|
|
|
int check_that_all_fields_are_given_values(THD *thd, TABLE *entry,
|
|
|
|
TABLE_LIST *table_list)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-01-15 02:09:35 +01:00
|
|
|
int err= 0;
|
2000-07-31 21:29:14 +02:00
|
|
|
for (Field **field=entry->field ; *field ; field++)
|
|
|
|
{
|
2004-09-28 19:08:00 +02:00
|
|
|
if ((*field)->query_id != thd->query_id &&
|
2005-05-13 13:44:14 +02:00
|
|
|
((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
|
|
|
|
((*field)->real_type() != FIELD_TYPE_ENUM))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-07-01 06:05:42 +02:00
|
|
|
bool view= FALSE;
|
|
|
|
if (table_list)
|
|
|
|
{
|
2005-09-02 08:50:17 +02:00
|
|
|
table_list= table_list->top_table();
|
2005-07-08 14:59:02 +02:00
|
|
|
view= test(table_list->view);
|
2005-07-01 06:05:42 +02:00
|
|
|
}
|
|
|
|
if (view)
|
|
|
|
{
|
|
|
|
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
|
|
|
|
ER_NO_DEFAULT_FOR_VIEW_FIELD,
|
|
|
|
ER(ER_NO_DEFAULT_FOR_VIEW_FIELD),
|
|
|
|
table_list->view_db.str,
|
|
|
|
table_list->view_name.str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
|
|
|
|
ER_NO_DEFAULT_FOR_FIELD,
|
|
|
|
ER(ER_NO_DEFAULT_FOR_FIELD),
|
|
|
|
(*field)->field_name);
|
|
|
|
}
|
2005-01-15 02:09:35 +01:00
|
|
|
err= 1;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
2005-01-15 02:09:35 +01:00
|
|
|
return thd->abort_on_warning ? err : 0;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2002-07-25 00:00:56 +02:00
|
|
|
Handling of delayed inserts
|
|
|
|
A thread is created for each table that one uses with the DELAYED attribute.
|
2000-07-31 21:29:14 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2004-03-29 16:57:07 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
class delayed_row :public ilink {
|
|
|
|
public:
|
|
|
|
char *record,*query;
|
|
|
|
enum_duplicates dup;
|
|
|
|
time_t start_time;
|
2005-01-03 22:04:52 +01:00
|
|
|
bool query_start_used,last_insert_id_used,insert_id_used, ignore, log_query;
|
2000-07-31 21:29:14 +02:00
|
|
|
ulonglong last_insert_id;
|
2006-09-20 11:05:11 +02:00
|
|
|
ulonglong next_insert_id;
|
|
|
|
ulong auto_increment_increment;
|
|
|
|
ulong auto_increment_offset;
|
2004-10-01 16:54:06 +02:00
|
|
|
timestamp_auto_set_type timestamp_field_type;
|
2000-07-31 21:29:14 +02:00
|
|
|
uint query_length;
|
|
|
|
|
2005-01-03 22:04:52 +01:00
|
|
|
delayed_row(enum_duplicates dup_arg, bool ignore_arg, bool log_query_arg)
|
|
|
|
:record(0), query(0), dup(dup_arg), ignore(ignore_arg), log_query(log_query_arg) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
~delayed_row()
|
|
|
|
{
|
|
|
|
x_free(record);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class delayed_insert :public ilink {
|
|
|
|
uint locks_in_memory;
|
|
|
|
public:
|
|
|
|
THD thd;
|
|
|
|
TABLE *table;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t cond,cond_client;
|
|
|
|
volatile uint tables_in_use,stacked_inserts;
|
|
|
|
volatile bool status,dead;
|
|
|
|
COPY_INFO info;
|
|
|
|
I_List<delayed_row> rows;
|
2005-06-03 22:46:03 +02:00
|
|
|
ulong group_count;
|
2001-09-01 09:38:16 +02:00
|
|
|
TABLE_LIST table_list; // Argument
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
delayed_insert()
|
|
|
|
:locks_in_memory(0),
|
|
|
|
table(0),tables_in_use(0),stacked_inserts(0), status(0), dead(0),
|
|
|
|
group_count(0)
|
|
|
|
{
|
2005-09-15 21:29:07 +02:00
|
|
|
thd.security_ctx->user=thd.security_ctx->priv_user=(char*) delayed_user;
|
|
|
|
thd.security_ctx->host=(char*) my_localhost;
|
2000-07-31 21:29:14 +02:00
|
|
|
thd.current_tablenr=0;
|
|
|
|
thd.version=refresh_version;
|
|
|
|
thd.command=COM_DELAYED_INSERT;
|
2005-02-28 10:59:46 +01:00
|
|
|
thd.lex->current_select= 0; // for my_message_sql
|
|
|
|
thd.lex->sql_command= SQLCOM_INSERT; // For innodb::store_lock()
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2004-02-01 14:30:32 +01:00
|
|
|
bzero((char*) &thd.net, sizeof(thd.net)); // Safety
|
|
|
|
bzero((char*) &table_list, sizeof(table_list)); // Safety
|
2003-12-04 22:42:18 +01:00
|
|
|
thd.system_thread= SYSTEM_THREAD_DELAYED_INSERT;
|
2005-09-15 21:29:07 +02:00
|
|
|
thd.security_ctx->host_or_ip= "";
|
2000-07-31 21:29:14 +02:00
|
|
|
bzero((char*) &info,sizeof(info));
|
2001-03-26 00:05:04 +02:00
|
|
|
pthread_mutex_init(&mutex,MY_MUTEX_INIT_FAST);
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_cond_init(&cond,NULL);
|
|
|
|
pthread_cond_init(&cond_client,NULL);
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_thread_count));
|
|
|
|
delayed_insert_threads++;
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_thread_count));
|
|
|
|
}
|
|
|
|
~delayed_insert()
|
|
|
|
{
|
2001-09-27 20:45:48 +02:00
|
|
|
/* The following is not really needed, but just for safety */
|
2000-07-31 21:29:14 +02:00
|
|
|
delayed_row *row;
|
|
|
|
while ((row=rows.get()))
|
|
|
|
delete row;
|
|
|
|
if (table)
|
|
|
|
close_thread_tables(&thd);
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_thread_count));
|
2001-08-31 22:02:09 +02:00
|
|
|
pthread_mutex_destroy(&mutex);
|
|
|
|
pthread_cond_destroy(&cond);
|
|
|
|
pthread_cond_destroy(&cond_client);
|
2000-07-31 21:29:14 +02:00
|
|
|
thd.unlink(); // Must be unlinked under lock
|
|
|
|
x_free(thd.query);
|
2005-09-15 21:29:07 +02:00
|
|
|
thd.security_ctx->user= thd.security_ctx->host=0;
|
2000-07-31 21:29:14 +02:00
|
|
|
thread_count--;
|
|
|
|
delayed_insert_threads--;
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_thread_count));
|
2002-08-22 15:50:58 +02:00
|
|
|
VOID(pthread_cond_broadcast(&COND_thread_count)); /* Tell main we are ready */
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The following is for checking when we can delete ourselves */
|
|
|
|
inline void lock()
|
|
|
|
{
|
|
|
|
locks_in_memory++; // Assume LOCK_delay_insert
|
|
|
|
}
|
|
|
|
void unlock()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&LOCK_delayed_insert);
|
|
|
|
if (!--locks_in_memory)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
if (thd.killed && ! stacked_inserts && ! tables_in_use)
|
|
|
|
{
|
|
|
|
pthread_cond_signal(&cond);
|
|
|
|
status=1;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_insert);
|
|
|
|
}
|
|
|
|
inline uint lock_count() { return locks_in_memory; }
|
|
|
|
|
|
|
|
TABLE* get_local_table(THD* client_thd);
|
|
|
|
bool handle_inserts(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
I_List<delayed_insert> delayed_threads;
|
|
|
|
|
|
|
|
|
|
|
|
delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list)
|
|
|
|
{
|
|
|
|
thd->proc_info="waiting for delay_list";
|
|
|
|
pthread_mutex_lock(&LOCK_delayed_insert); // Protect master list
|
|
|
|
I_List_iterator<delayed_insert> it(delayed_threads);
|
|
|
|
delayed_insert *tmp;
|
|
|
|
while ((tmp=it++))
|
|
|
|
{
|
|
|
|
if (!strcmp(tmp->thd.db,table_list->db) &&
|
2005-01-06 12:00:13 +01:00
|
|
|
!strcmp(table_list->table_name,tmp->table->s->table_name))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
tmp->lock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_insert); // For unlink from list
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static TABLE *delayed_get_table(THD *thd,TABLE_LIST *table_list)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
delayed_insert *tmp;
|
2005-04-27 20:54:19 +02:00
|
|
|
TABLE *table;
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_ENTER("delayed_get_table");
|
|
|
|
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
/* Must be set in the parser */
|
|
|
|
DBUG_ASSERT(table_list->db);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2005-04-27 20:54:19 +02:00
|
|
|
/* Find the thread which handles this table. */
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!(tmp=find_handler(thd,table_list)))
|
|
|
|
{
|
2005-04-27 20:54:19 +02:00
|
|
|
/*
|
|
|
|
No match. Create a new thread to handle the table, but
|
|
|
|
no more than max_insert_delayed_threads.
|
|
|
|
*/
|
2004-03-04 18:58:36 +01:00
|
|
|
if (delayed_insert_threads >= thd->variables.max_insert_delayed_threads)
|
2003-04-26 19:43:28 +02:00
|
|
|
DBUG_RETURN(0);
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->proc_info="Creating delayed handler";
|
|
|
|
pthread_mutex_lock(&LOCK_delayed_create);
|
2005-04-27 20:54:19 +02:00
|
|
|
/*
|
|
|
|
The first search above was done without LOCK_delayed_create.
|
|
|
|
Another thread might have created the handler in between. Search again.
|
|
|
|
*/
|
|
|
|
if (! (tmp= find_handler(thd, table_list)))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
if (!(tmp=new delayed_insert()))
|
|
|
|
{
|
|
|
|
my_error(ER_OUTOFMEMORY,MYF(0),sizeof(delayed_insert));
|
2005-04-27 20:54:19 +02:00
|
|
|
goto err1;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2000-10-23 14:35:42 +02:00
|
|
|
pthread_mutex_lock(&LOCK_thread_count);
|
|
|
|
thread_count++;
|
|
|
|
pthread_mutex_unlock(&LOCK_thread_count);
|
A fix and a test case for
Bug#19022 "Memory bug when switching db during trigger execution"
Bug#17199 "Problem when view calls function from another database."
Bug#18444 "Fully qualified stored function names don't work correctly in
SELECT statements"
Documentation note: this patch introduces a change in behaviour of prepared
statements.
This patch adds a few new invariants with regard to how THD::db should
be used. These invariants should be preserved in future:
- one should never refer to THD::db by pointer and always make a deep copy
(strmake, strdup)
- one should never compare two databases by pointer, but use strncmp or
my_strncasecmp
- TABLE_LIST object table->db should be always initialized in the parser or
by creator of the object.
For prepared statements it means that if the current database is changed
after a statement is prepared, the database that was current at prepare
remains active. This also means that you can not prepare a statement that
implicitly refers to the current database if the latter is not set.
This is not documented, and therefore needs documentation. This is NOT a
change in behavior for almost all SQL statements except:
- ALTER TABLE t1 RENAME t2
- OPTIMIZE TABLE t1
- ANALYZE TABLE t1
- TRUNCATE TABLE t1 --
until this patch t1 or t2 could be evaluated at the first execution of
prepared statement.
CURRENT_DATABASE() still works OK and is evaluated at every execution
of prepared statement.
Note, that in stored routines this is not an issue as the default
database is the database of the stored procedure and "use" statement
is prohibited in stored routines.
This patch makes obsolete the use of check_db_used (it was never used in the
old code too) and all other places that check for table->db and assign it
from THD::db if it's NULL, except the parser.
How this patch was created: THD::{db,db_length} were replaced with a
LEX_STRING, THD::db. All the places that refer to THD::{db,db_length} were
manually checked and:
- if the place uses thd->db by pointer, it was fixed to make a deep copy
- if a place compared two db pointers, it was fixed to compare them by value
(via strcmp/my_strcasecmp, whatever was approproate)
Then this intermediate patch was used to write a smaller patch that does the
same thing but without a rename.
TODO in 5.1:
- remove check_db_used
- deploy THD::set_db in mysql_change_db
See also comments to individual files.
2006-06-26 22:47:52 +02:00
|
|
|
tmp->thd.set_db(table_list->db, strlen(table_list->db));
|
|
|
|
tmp->thd.query= my_strdup(table_list->table_name,MYF(MY_WME));
|
|
|
|
if (tmp->thd.db == NULL || tmp->thd.query == NULL)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
delete tmp;
|
2004-11-12 13:34:00 +01:00
|
|
|
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
|
2005-04-27 20:54:19 +02:00
|
|
|
goto err1;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2001-09-01 09:38:16 +02:00
|
|
|
tmp->table_list= *table_list; // Needed to open table
|
2005-01-06 12:00:13 +01:00
|
|
|
tmp->table_list.alias= tmp->table_list.table_name= tmp->thd.query;
|
2000-07-31 21:29:14 +02:00
|
|
|
tmp->lock();
|
|
|
|
pthread_mutex_lock(&tmp->mutex);
|
|
|
|
if ((error=pthread_create(&tmp->thd.real_id,&connection_attrib,
|
|
|
|
handle_delayed_insert,(void*) tmp)))
|
|
|
|
{
|
|
|
|
DBUG_PRINT("error",
|
|
|
|
("Can't create thread to handle delayed insert (error %d)",
|
|
|
|
error));
|
|
|
|
pthread_mutex_unlock(&tmp->mutex);
|
|
|
|
tmp->unlock();
|
|
|
|
delete tmp;
|
2004-10-20 03:04:37 +02:00
|
|
|
my_error(ER_CANT_CREATE_THREAD, MYF(0), error);
|
2005-04-27 20:54:19 +02:00
|
|
|
goto err1;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until table is open */
|
|
|
|
thd->proc_info="waiting for handler open";
|
|
|
|
while (!tmp->thd.killed && !tmp->table && !thd->killed)
|
|
|
|
{
|
|
|
|
pthread_cond_wait(&tmp->cond_client,&tmp->mutex);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&tmp->mutex);
|
|
|
|
thd->proc_info="got old table";
|
|
|
|
if (tmp->thd.killed)
|
|
|
|
{
|
2003-01-30 21:15:44 +01:00
|
|
|
if (tmp->thd.is_fatal_error)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
/* Copy error message and abort */
|
2003-01-30 21:15:44 +01:00
|
|
|
thd->fatal_error();
|
2000-07-31 21:29:14 +02:00
|
|
|
strmov(thd->net.last_error,tmp->thd.net.last_error);
|
2001-06-05 02:38:10 +02:00
|
|
|
thd->net.last_errno=tmp->thd.net.last_errno;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
tmp->unlock();
|
2005-04-27 20:54:19 +02:00
|
|
|
goto err;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
if (thd->killed)
|
|
|
|
{
|
|
|
|
tmp->unlock();
|
2005-04-27 20:54:19 +02:00
|
|
|
goto err;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_create);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&tmp->mutex);
|
2005-04-27 22:58:11 +02:00
|
|
|
table= tmp->get_local_table(thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_unlock(&tmp->mutex);
|
|
|
|
if (table)
|
|
|
|
thd->di=tmp;
|
2003-01-30 21:15:44 +01:00
|
|
|
else if (tmp->thd.is_fatal_error)
|
|
|
|
thd->fatal_error();
|
2005-04-27 20:54:19 +02:00
|
|
|
/* Unlock the delayed insert object after its last access. */
|
|
|
|
tmp->unlock();
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_RETURN((table_list->table=table));
|
2005-04-27 20:54:19 +02:00
|
|
|
|
|
|
|
err1:
|
|
|
|
thd->fatal_error();
|
|
|
|
err:
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_create);
|
|
|
|
DBUG_RETURN(0); // Continue with normal insert
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
As we can't let many threads modify the same TABLE structure, we create
|
|
|
|
an own structure for each tread. This includes a row buffer to save the
|
|
|
|
column values and new fields that points to the new row buffer.
|
|
|
|
The memory is allocated in the client thread and is freed automaticly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
TABLE *delayed_insert::get_local_table(THD* client_thd)
|
|
|
|
{
|
|
|
|
my_ptrdiff_t adjust_ptrs;
|
2001-03-08 20:49:15 +01:00
|
|
|
Field **field,**org_field, *found_next_number_field;
|
2000-07-31 21:29:14 +02:00
|
|
|
TABLE *copy;
|
2006-06-26 20:57:18 +02:00
|
|
|
DBUG_ENTER("delayed_insert::get_local_table");
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
/* First request insert thread to get a lock */
|
|
|
|
status=1;
|
|
|
|
tables_in_use++;
|
|
|
|
if (!thd.lock) // Table is not locked
|
|
|
|
{
|
|
|
|
client_thd->proc_info="waiting for handler lock";
|
|
|
|
pthread_cond_signal(&cond); // Tell handler to lock table
|
|
|
|
while (!dead && !thd.lock && ! client_thd->killed)
|
|
|
|
{
|
|
|
|
pthread_cond_wait(&cond_client,&mutex);
|
|
|
|
}
|
|
|
|
client_thd->proc_info="got handler lock";
|
|
|
|
if (client_thd->killed)
|
|
|
|
goto error;
|
|
|
|
if (dead)
|
|
|
|
{
|
|
|
|
strmov(client_thd->net.last_error,thd.net.last_error);
|
|
|
|
client_thd->net.last_errno=thd.net.last_errno;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-26 20:57:18 +02:00
|
|
|
/*
|
|
|
|
Allocate memory for the TABLE object, the field pointers array, and
|
|
|
|
one record buffer of reclength size. Normally a table has three
|
|
|
|
record buffers of rec_buff_length size, which includes alignment
|
|
|
|
bytes. Since the table copy is used for creating one record only,
|
|
|
|
the other record buffers and alignment are unnecessary.
|
|
|
|
*/
|
2000-07-31 21:29:14 +02:00
|
|
|
client_thd->proc_info="allocating local table";
|
2001-12-05 12:03:00 +01:00
|
|
|
copy= (TABLE*) client_thd->alloc(sizeof(*copy)+
|
2005-01-06 12:00:13 +01:00
|
|
|
(table->s->fields+1)*sizeof(Field**)+
|
|
|
|
table->s->reclength);
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!copy)
|
|
|
|
goto error;
|
2006-06-26 20:57:18 +02:00
|
|
|
|
|
|
|
/* Copy the TABLE object. */
|
2000-07-31 21:29:14 +02:00
|
|
|
*copy= *table;
|
2005-01-06 12:00:13 +01:00
|
|
|
copy->s= ©->share_not_to_be_used;
|
|
|
|
// No name hashing
|
|
|
|
bzero((char*) ©->s->name_hash,sizeof(copy->s->name_hash));
|
2000-07-31 21:29:14 +02:00
|
|
|
/* We don't need to change the file handler here */
|
|
|
|
|
2006-06-26 20:57:18 +02:00
|
|
|
/* Assign the pointers for the field pointers array and the record. */
|
|
|
|
field= copy->field= (Field**) (copy + 1);
|
|
|
|
copy->record[0]= (byte*) (field + table->s->fields + 1);
|
|
|
|
memcpy((char*) copy->record[0], (char*) table->record[0],
|
|
|
|
table->s->reclength);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2006-06-26 20:57:18 +02:00
|
|
|
/*
|
|
|
|
Make a copy of all fields.
|
|
|
|
The copied fields need to point into the copied record. This is done
|
|
|
|
by copying the field objects with their old pointer values and then
|
|
|
|
"move" the pointers by the distance between the original and copied
|
|
|
|
records. That way we preserve the relative positions in the records.
|
|
|
|
*/
|
|
|
|
adjust_ptrs= PTR_BYTE_DIFF(copy->record[0], table->record[0]);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2006-06-26 20:57:18 +02:00
|
|
|
found_next_number_field= table->found_next_number_field;
|
|
|
|
for (org_field= table->field; *org_field; org_field++, field++)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2006-06-26 20:57:18 +02:00
|
|
|
if (!(*field= (*org_field)->new_field(client_thd->mem_root, copy, 1)))
|
|
|
|
DBUG_RETURN(0);
|
2004-03-30 18:24:28 +02:00
|
|
|
(*field)->orig_table= copy; // Remove connection
|
2000-07-31 21:29:14 +02:00
|
|
|
(*field)->move_field(adjust_ptrs); // Point at copy->record[0]
|
2001-03-08 20:49:15 +01:00
|
|
|
if (*org_field == found_next_number_field)
|
|
|
|
(*field)->table->found_next_number_field= *field;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
*field=0;
|
|
|
|
|
|
|
|
/* Adjust timestamp */
|
|
|
|
if (table->timestamp_field)
|
|
|
|
{
|
|
|
|
/* Restore offset as this may have been reset in handle_inserts */
|
|
|
|
copy->timestamp_field=
|
2005-01-06 12:00:13 +01:00
|
|
|
(Field_timestamp*) copy->field[table->s->timestamp_field_offset];
|
2004-04-02 08:12:53 +02:00
|
|
|
copy->timestamp_field->unireg_check= table->timestamp_field->unireg_check;
|
2004-10-01 16:54:06 +02:00
|
|
|
copy->timestamp_field_type= copy->timestamp_field->get_auto_set_type();
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* _rowid is not used with delayed insert */
|
|
|
|
copy->rowid_field=0;
|
2004-06-18 08:11:31 +02:00
|
|
|
|
|
|
|
/* Adjust in_use for pointing to client thread */
|
|
|
|
copy->in_use= client_thd;
|
2006-04-05 14:39:20 +02:00
|
|
|
|
|
|
|
/* Adjust lock_count. This table object is not part of a lock. */
|
|
|
|
copy->lock_count= 0;
|
|
|
|
|
2006-06-26 20:57:18 +02:00
|
|
|
DBUG_RETURN(copy);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
/* Got fatal error */
|
|
|
|
error:
|
|
|
|
tables_in_use--;
|
|
|
|
status=1;
|
|
|
|
pthread_cond_signal(&cond); // Inform thread about abort
|
2006-06-26 20:57:18 +02:00
|
|
|
DBUG_RETURN(0);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Put a question in queue */
|
|
|
|
|
2004-12-31 11:04:35 +01:00
|
|
|
static int write_delayed(THD *thd,TABLE *table,enum_duplicates duplic, bool ignore,
|
2003-04-02 00:15:20 +02:00
|
|
|
char *query, uint query_length, bool log_on)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
delayed_row *row=0;
|
|
|
|
delayed_insert *di=thd->di;
|
|
|
|
DBUG_ENTER("write_delayed");
|
|
|
|
|
|
|
|
thd->proc_info="waiting for handler insert";
|
|
|
|
pthread_mutex_lock(&di->mutex);
|
|
|
|
while (di->stacked_inserts >= delayed_queue_size && !thd->killed)
|
|
|
|
pthread_cond_wait(&di->cond_client,&di->mutex);
|
|
|
|
thd->proc_info="storing row into queue";
|
|
|
|
|
2004-12-31 11:04:35 +01:00
|
|
|
if (thd->killed || !(row= new delayed_row(duplic, ignore, log_on)))
|
2000-07-31 21:29:14 +02:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!query)
|
|
|
|
query_length=0;
|
2005-01-06 12:00:13 +01:00
|
|
|
if (!(row->record= (char*) my_malloc(table->s->reclength+query_length+1,
|
2000-07-31 21:29:14 +02:00
|
|
|
MYF(MY_WME))))
|
|
|
|
goto err;
|
2005-01-06 12:00:13 +01:00
|
|
|
memcpy(row->record, table->record[0], table->s->reclength);
|
2000-07-31 21:29:14 +02:00
|
|
|
if (query_length)
|
|
|
|
{
|
2005-01-06 12:00:13 +01:00
|
|
|
row->query= row->record+table->s->reclength;
|
2000-07-31 21:29:14 +02:00
|
|
|
memcpy(row->query,query,query_length+1);
|
|
|
|
}
|
|
|
|
row->query_length= query_length;
|
|
|
|
row->start_time= thd->start_time;
|
|
|
|
row->query_start_used= thd->query_start_used;
|
|
|
|
row->last_insert_id_used= thd->last_insert_id_used;
|
|
|
|
row->insert_id_used= thd->insert_id_used;
|
|
|
|
row->last_insert_id= thd->last_insert_id;
|
2004-10-01 16:54:06 +02:00
|
|
|
row->timestamp_field_type= table->timestamp_field_type;
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2006-09-20 11:05:11 +02:00
|
|
|
/* The session variable settings can always be copied. */
|
|
|
|
row->auto_increment_increment= thd->variables.auto_increment_increment;
|
|
|
|
row->auto_increment_offset= thd->variables.auto_increment_offset;
|
|
|
|
/*
|
|
|
|
Next insert id must be set for the first value in a multi-row insert
|
|
|
|
only. So clear it after the first use. Assume a multi-row insert.
|
|
|
|
Since the user thread doesn't really execute the insert,
|
|
|
|
thd->next_insert_id is left untouched between the rows. If we copy
|
|
|
|
the same insert id to every row of the multi-row insert, the delayed
|
|
|
|
insert thread would copy this before inserting every row. Thus it
|
|
|
|
tries to insert all rows with the same insert id. This fails on the
|
|
|
|
unique constraint. So just the first row would be really inserted.
|
|
|
|
*/
|
|
|
|
row->next_insert_id= thd->next_insert_id;
|
|
|
|
thd->next_insert_id= 0;
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
di->rows.push_back(row);
|
|
|
|
di->stacked_inserts++;
|
|
|
|
di->status=1;
|
2005-01-06 12:00:13 +01:00
|
|
|
if (table->s->blob_fields)
|
2000-07-31 21:29:14 +02:00
|
|
|
unlink_blobs(table);
|
|
|
|
pthread_cond_signal(&di->cond);
|
|
|
|
|
|
|
|
thread_safe_increment(delayed_rows_in_use,&LOCK_delayed_status);
|
|
|
|
pthread_mutex_unlock(&di->mutex);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
|
|
|
|
err:
|
|
|
|
delete row;
|
|
|
|
pthread_mutex_unlock(&di->mutex);
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void end_delayed_insert(THD *thd)
|
|
|
|
{
|
2001-03-08 20:49:15 +01:00
|
|
|
DBUG_ENTER("end_delayed_insert");
|
2000-07-31 21:29:14 +02:00
|
|
|
delayed_insert *di=thd->di;
|
|
|
|
pthread_mutex_lock(&di->mutex);
|
2001-03-08 20:49:15 +01:00
|
|
|
DBUG_PRINT("info",("tables in use: %d",di->tables_in_use));
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!--di->tables_in_use || di->thd.killed)
|
|
|
|
{ // Unlock table
|
|
|
|
di->status=1;
|
|
|
|
pthread_cond_signal(&di->cond);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&di->mutex);
|
2001-03-08 20:49:15 +01:00
|
|
|
DBUG_VOID_RETURN;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* We kill all delayed threads when doing flush-tables */
|
|
|
|
|
|
|
|
void kill_delayed_threads(void)
|
|
|
|
{
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_delayed_insert)); // For unlink from list
|
|
|
|
|
|
|
|
I_List_iterator<delayed_insert> it(delayed_threads);
|
|
|
|
delayed_insert *tmp;
|
|
|
|
while ((tmp=it++))
|
|
|
|
{
|
2003-03-31 10:39:46 +02:00
|
|
|
tmp->thd.killed= THD::KILL_CONNECTION;
|
2000-07-31 21:29:14 +02:00
|
|
|
if (tmp->thd.mysys_var)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&tmp->thd.mysys_var->mutex);
|
2001-08-31 22:02:09 +02:00
|
|
|
if (tmp->thd.mysys_var->current_cond)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2001-10-02 20:08:08 +02:00
|
|
|
/*
|
|
|
|
We need the following test because the main mutex may be locked
|
|
|
|
in handle_delayed_insert()
|
|
|
|
*/
|
|
|
|
if (&tmp->mutex != tmp->thd.mysys_var->current_mutex)
|
|
|
|
pthread_mutex_lock(tmp->thd.mysys_var->current_mutex);
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_cond_broadcast(tmp->thd.mysys_var->current_cond);
|
2001-10-02 20:08:08 +02:00
|
|
|
if (&tmp->mutex != tmp->thd.mysys_var->current_mutex)
|
|
|
|
pthread_mutex_unlock(tmp->thd.mysys_var->current_mutex);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&tmp->thd.mysys_var->mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_delayed_insert)); // For unlink from list
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new delayed insert thread
|
|
|
|
*/
|
|
|
|
|
2005-10-08 16:39:55 +02:00
|
|
|
pthread_handler_t handle_delayed_insert(void *arg)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
delayed_insert *di=(delayed_insert*) arg;
|
|
|
|
THD *thd= &di->thd;
|
|
|
|
|
|
|
|
pthread_detach_this_thread();
|
|
|
|
/* Add thread to THD list so that's it's visible in 'show processlist' */
|
|
|
|
pthread_mutex_lock(&LOCK_thread_count);
|
|
|
|
thd->thread_id=thread_id++;
|
2001-07-18 22:34:04 +02:00
|
|
|
thd->end_time();
|
2000-07-31 21:29:14 +02:00
|
|
|
threads.append(thd);
|
2003-03-31 10:39:46 +02:00
|
|
|
thd->killed=abort_loop ? THD::KILL_CONNECTION : THD::NOT_KILLED;
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_unlock(&LOCK_thread_count);
|
|
|
|
|
2005-04-27 20:54:19 +02:00
|
|
|
/*
|
|
|
|
Wait until the client runs into pthread_cond_wait(),
|
|
|
|
where we free it after the table is opened and di linked in the list.
|
|
|
|
If we did not wait here, the client might detect the opened table
|
|
|
|
before it is linked to the list. It would release LOCK_delayed_create
|
|
|
|
and allow another thread to create another handler for the same table,
|
|
|
|
since it does not find one in the list.
|
|
|
|
*/
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_lock(&di->mutex);
|
2001-08-22 00:45:07 +02:00
|
|
|
#if !defined( __WIN__) && !defined(OS2) /* Win32 calls this in pthread_create */
|
2000-07-31 21:29:14 +02:00
|
|
|
if (my_thread_init())
|
|
|
|
{
|
|
|
|
strmov(thd->net.last_error,ER(thd->net.last_errno=ER_OUT_OF_RESOURCES));
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DBUG_ENTER("handle_delayed_insert");
|
2005-11-23 19:18:10 +01:00
|
|
|
thd->thread_stack= (char*) &thd;
|
2002-07-23 17:31:22 +02:00
|
|
|
if (init_thr_lock() || thd->store_globals())
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-01-30 21:15:44 +01:00
|
|
|
thd->fatal_error();
|
2000-07-31 21:29:14 +02:00
|
|
|
strmov(thd->net.last_error,ER(thd->net.last_errno=ER_OUT_OF_RESOURCES));
|
|
|
|
goto end;
|
|
|
|
}
|
2003-01-28 07:38:28 +01:00
|
|
|
#if !defined(__WIN__) && !defined(OS2) && !defined(__NETWARE__)
|
2000-07-31 21:29:14 +02:00
|
|
|
sigset_t set;
|
|
|
|
VOID(sigemptyset(&set)); // Get mask in use
|
|
|
|
VOID(pthread_sigmask(SIG_UNBLOCK,&set,&thd->block_signals));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* open table */
|
|
|
|
|
2001-09-01 09:38:16 +02:00
|
|
|
if (!(di->table=open_ltable(thd,&di->table_list,TL_WRITE_DELAYED)))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-01-30 21:15:44 +01:00
|
|
|
thd->fatal_error(); // Abort waiting inserts
|
2000-07-31 21:29:14 +02:00
|
|
|
goto end;
|
|
|
|
}
|
2004-06-23 12:29:05 +02:00
|
|
|
if (!(di->table->file->table_flags() & HA_CAN_INSERT_DELAYED))
|
2001-04-09 20:08:56 +02:00
|
|
|
{
|
2003-01-30 21:15:44 +01:00
|
|
|
thd->fatal_error();
|
2005-01-06 12:00:13 +01:00
|
|
|
my_error(ER_ILLEGAL_HA, MYF(0), di->table_list.table_name);
|
2001-04-09 20:08:56 +02:00
|
|
|
goto end;
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
di->table->copy_blobs=1;
|
|
|
|
|
|
|
|
/* One can now use this */
|
|
|
|
pthread_mutex_lock(&LOCK_delayed_insert);
|
|
|
|
delayed_threads.append(di);
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_insert);
|
|
|
|
|
|
|
|
/* Tell client that the thread is initialized */
|
|
|
|
pthread_cond_signal(&di->cond_client);
|
|
|
|
|
|
|
|
/* Now wait until we get an insert or lock to handle */
|
|
|
|
/* We will not abort as long as a client thread uses this thread */
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2003-03-31 10:39:46 +02:00
|
|
|
if (thd->killed == THD::KILL_CONNECTION)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
uint lock_count;
|
|
|
|
/*
|
|
|
|
Remove this from delay insert list so that no one can request a
|
|
|
|
table from this
|
|
|
|
*/
|
|
|
|
pthread_mutex_unlock(&di->mutex);
|
|
|
|
pthread_mutex_lock(&LOCK_delayed_insert);
|
|
|
|
di->unlink();
|
|
|
|
lock_count=di->lock_count();
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_insert);
|
|
|
|
pthread_mutex_lock(&di->mutex);
|
|
|
|
if (!lock_count && !di->tables_in_use && !di->stacked_inserts)
|
|
|
|
break; // Time to die
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!di->status && !di->stacked_inserts)
|
|
|
|
{
|
|
|
|
struct timespec abstime;
|
2002-01-02 20:29:41 +01:00
|
|
|
set_timespec(abstime, delayed_insert_timeout);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
/* Information for pthread_kill */
|
|
|
|
di->thd.mysys_var->current_mutex= &di->mutex;
|
|
|
|
di->thd.mysys_var->current_cond= &di->cond;
|
2002-01-02 20:29:41 +01:00
|
|
|
di->thd.proc_info="Waiting for INSERT";
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2001-03-08 20:49:15 +01:00
|
|
|
DBUG_PRINT("info",("Waiting for someone to insert rows"));
|
2001-08-31 22:02:09 +02:00
|
|
|
while (!thd->killed)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
int error;
|
2003-04-26 19:43:28 +02:00
|
|
|
#if defined(HAVE_BROKEN_COND_TIMEDWAIT)
|
2000-07-31 21:29:14 +02:00
|
|
|
error=pthread_cond_wait(&di->cond,&di->mutex);
|
|
|
|
#else
|
|
|
|
error=pthread_cond_timedwait(&di->cond,&di->mutex,&abstime);
|
|
|
|
#ifdef EXTRA_DEBUG
|
2003-04-27 21:12:08 +02:00
|
|
|
if (error && error != EINTR && error != ETIMEDOUT)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Got error %d from pthread_cond_timedwait\n",error);
|
|
|
|
DBUG_PRINT("error",("Got error %d from pthread_cond_timedwait",
|
|
|
|
error));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if (thd->killed || di->status)
|
|
|
|
break;
|
2005-10-11 23:58:22 +02:00
|
|
|
if (error == ETIMEDOUT || error == ETIME)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-03-31 10:39:46 +02:00
|
|
|
thd->killed= THD::KILL_CONNECTION;
|
2000-07-31 21:29:14 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-09-01 09:38:16 +02:00
|
|
|
/* We can't lock di->mutex and mysys_var->mutex at the same time */
|
|
|
|
pthread_mutex_unlock(&di->mutex);
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_lock(&di->thd.mysys_var->mutex);
|
|
|
|
di->thd.mysys_var->current_mutex= 0;
|
|
|
|
di->thd.mysys_var->current_cond= 0;
|
|
|
|
pthread_mutex_unlock(&di->thd.mysys_var->mutex);
|
2001-09-01 09:38:16 +02:00
|
|
|
pthread_mutex_lock(&di->mutex);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2002-01-02 20:29:41 +01:00
|
|
|
di->thd.proc_info=0;
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
if (di->tables_in_use && ! thd->lock)
|
|
|
|
{
|
2005-09-15 01:56:09 +02:00
|
|
|
bool not_used;
|
2005-04-27 20:54:19 +02:00
|
|
|
/*
|
|
|
|
Request for new delayed insert.
|
|
|
|
Lock the table, but avoid to be blocked by a global read lock.
|
|
|
|
If we got here while a global read lock exists, then one or more
|
|
|
|
inserts started before the lock was requested. These are allowed
|
|
|
|
to complete their work before the server returns control to the
|
|
|
|
client which requested the global read lock. The delayed insert
|
|
|
|
handler will close the table and finish when the outstanding
|
|
|
|
inserts are done.
|
|
|
|
*/
|
2005-05-31 11:08:14 +02:00
|
|
|
if (! (thd->lock= mysql_lock_tables(thd, &di->table, 1,
|
2005-09-15 01:56:09 +02:00
|
|
|
MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK,
|
|
|
|
¬_used)))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-12-17 20:18:01 +01:00
|
|
|
/* Fatal error */
|
|
|
|
di->dead= 1;
|
|
|
|
thd->killed= THD::KILL_CONNECTION;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
pthread_cond_broadcast(&di->cond_client);
|
|
|
|
}
|
|
|
|
if (di->stacked_inserts)
|
|
|
|
{
|
|
|
|
if (di->handle_inserts())
|
|
|
|
{
|
2003-12-17 20:18:01 +01:00
|
|
|
/* Some fatal error */
|
|
|
|
di->dead= 1;
|
|
|
|
thd->killed= THD::KILL_CONNECTION;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
di->status=0;
|
|
|
|
if (!di->stacked_inserts && !di->tables_in_use && thd->lock)
|
|
|
|
{
|
2004-06-25 20:56:23 +02:00
|
|
|
/*
|
|
|
|
No one is doing a insert delayed
|
|
|
|
Unlock table so that other threads can use it
|
|
|
|
*/
|
2000-07-31 21:29:14 +02:00
|
|
|
MYSQL_LOCK *lock=thd->lock;
|
|
|
|
thd->lock=0;
|
|
|
|
pthread_mutex_unlock(&di->mutex);
|
|
|
|
mysql_unlock_tables(thd, lock);
|
|
|
|
di->group_count=0;
|
|
|
|
pthread_mutex_lock(&di->mutex);
|
|
|
|
}
|
|
|
|
if (di->tables_in_use)
|
|
|
|
pthread_cond_broadcast(&di->cond_client); // If waiting clients
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
/*
|
|
|
|
di should be unlinked from the thread handler list and have no active
|
|
|
|
clients
|
|
|
|
*/
|
|
|
|
|
|
|
|
close_thread_tables(thd); // Free the table
|
|
|
|
di->table=0;
|
2003-12-17 20:18:01 +01:00
|
|
|
di->dead= 1; // If error
|
|
|
|
thd->killed= THD::KILL_CONNECTION; // If error
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_cond_broadcast(&di->cond_client); // Safety
|
|
|
|
pthread_mutex_unlock(&di->mutex);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&LOCK_delayed_create); // Because of delayed_get_table
|
|
|
|
pthread_mutex_lock(&LOCK_delayed_insert);
|
|
|
|
delete di;
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_insert);
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_create);
|
|
|
|
|
|
|
|
my_thread_end();
|
|
|
|
pthread_exit(0);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Remove pointers from temporary fields to allocated values */
|
|
|
|
|
|
|
|
static void unlink_blobs(register TABLE *table)
|
|
|
|
{
|
|
|
|
for (Field **ptr=table->field ; *ptr ; ptr++)
|
|
|
|
{
|
|
|
|
if ((*ptr)->flags & BLOB_FLAG)
|
|
|
|
((Field_blob *) (*ptr))->clear_temporary();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free blobs stored in current row */
|
|
|
|
|
|
|
|
static void free_delayed_insert_blobs(register TABLE *table)
|
|
|
|
{
|
|
|
|
for (Field **ptr=table->field ; *ptr ; ptr++)
|
|
|
|
{
|
|
|
|
if ((*ptr)->flags & BLOB_FLAG)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
((Field_blob *) (*ptr))->get_ptr(&str);
|
|
|
|
my_free(str,MYF(MY_ALLOW_ZERO_PTR));
|
|
|
|
((Field_blob *) (*ptr))->reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool delayed_insert::handle_inserts(void)
|
|
|
|
{
|
|
|
|
int error;
|
2005-06-03 22:46:03 +02:00
|
|
|
ulong max_rows;
|
2006-07-01 23:51:10 +02:00
|
|
|
bool using_ignore= 0, using_opt_replace= 0;
|
|
|
|
bool using_bin_log= mysql_bin_log.is_open();
|
2001-05-16 23:46:50 +02:00
|
|
|
delayed_row *row;
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_ENTER("handle_inserts");
|
|
|
|
|
|
|
|
/* Allow client to insert new rows */
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
|
|
|
|
table->next_number_field=table->found_next_number_field;
|
|
|
|
|
|
|
|
thd.proc_info="upgrading lock";
|
|
|
|
if (thr_upgrade_write_delay_lock(*thd.lock->locks))
|
|
|
|
{
|
|
|
|
/* This can only happen if thread is killed by shutdown */
|
2005-01-06 12:00:13 +01:00
|
|
|
sql_print_error(ER(ER_DELAYED_CANT_CHANGE_LOCK),table->s->table_name);
|
2000-07-31 21:29:14 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
thd.proc_info="insert";
|
2005-06-03 22:46:03 +02:00
|
|
|
max_rows= delayed_insert_limit;
|
2005-01-06 12:00:13 +01:00
|
|
|
if (thd.killed || table->s->version != refresh_version)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-03-31 10:39:46 +02:00
|
|
|
thd.killed= THD::KILL_CONNECTION;
|
2005-06-03 22:46:03 +02:00
|
|
|
max_rows= ~(ulong)0; // Do as much as possible
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2001-11-27 01:50:20 +01:00
|
|
|
/*
|
|
|
|
We can't use row caching when using the binary log because if
|
|
|
|
we get a crash, then binary log will contain rows that are not yet
|
|
|
|
written to disk, which will cause problems in replication.
|
|
|
|
*/
|
|
|
|
if (!using_bin_log)
|
|
|
|
table->file->extra(HA_EXTRA_WRITE_CACHE);
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_lock(&mutex);
|
2006-06-13 17:18:32 +02:00
|
|
|
|
|
|
|
/* Reset auto-increment cacheing */
|
|
|
|
if (thd.clear_next_insert_id)
|
|
|
|
{
|
|
|
|
thd.next_insert_id= 0;
|
|
|
|
thd.clear_next_insert_id= 0;
|
|
|
|
}
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
while ((row=rows.get()))
|
|
|
|
{
|
|
|
|
stacked_inserts--;
|
|
|
|
pthread_mutex_unlock(&mutex);
|
2005-01-06 12:00:13 +01:00
|
|
|
memcpy(table->record[0],row->record,table->s->reclength);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
thd.start_time=row->start_time;
|
|
|
|
thd.query_start_used=row->query_start_used;
|
|
|
|
thd.last_insert_id=row->last_insert_id;
|
|
|
|
thd.last_insert_id_used=row->last_insert_id_used;
|
|
|
|
thd.insert_id_used=row->insert_id_used;
|
2004-10-01 16:54:06 +02:00
|
|
|
table->timestamp_field_type= row->timestamp_field_type;
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2006-09-20 11:05:11 +02:00
|
|
|
/* The session variable settings can always be copied. */
|
|
|
|
thd.variables.auto_increment_increment= row->auto_increment_increment;
|
|
|
|
thd.variables.auto_increment_offset= row->auto_increment_offset;
|
|
|
|
/* Next insert id must be used only if non-zero. */
|
|
|
|
if (row->next_insert_id)
|
|
|
|
thd.next_insert_id= row->next_insert_id;
|
|
|
|
DBUG_PRINT("loop", ("next_insert_id: %lu", (ulong) thd.next_insert_id));
|
|
|
|
|
2004-12-31 11:04:35 +01:00
|
|
|
info.ignore= row->ignore;
|
2000-07-31 21:29:14 +02:00
|
|
|
info.handle_duplicates= row->dup;
|
2004-12-31 11:04:35 +01:00
|
|
|
if (info.ignore ||
|
2005-01-10 20:55:05 +01:00
|
|
|
info.handle_duplicates != DUP_ERROR)
|
2000-12-24 14:19:00 +01:00
|
|
|
{
|
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
|
|
|
using_ignore=1;
|
|
|
|
}
|
2006-07-01 23:51:10 +02:00
|
|
|
if (info.handle_duplicates == DUP_REPLACE &&
|
|
|
|
(!table->triggers ||
|
|
|
|
!table->triggers->has_delete_triggers()))
|
|
|
|
{
|
|
|
|
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
|
|
|
|
using_opt_replace= 1;
|
|
|
|
}
|
2002-11-03 23:56:25 +01:00
|
|
|
thd.clear_error(); // reset error for binlog
|
2004-09-28 19:08:00 +02:00
|
|
|
if (write_record(&thd, table, &info))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2002-06-11 10:20:31 +02:00
|
|
|
info.error_count++; // Ignore errors
|
2001-05-16 23:46:50 +02:00
|
|
|
thread_safe_increment(delayed_insert_errors,&LOCK_delayed_status);
|
2000-11-11 22:57:35 +01:00
|
|
|
row->log_query = 0;
|
2006-09-20 11:05:11 +02:00
|
|
|
/*
|
|
|
|
We must reset next_insert_id. Otherwise all following rows may
|
|
|
|
become duplicates. If write_record() failed on a duplicate and
|
|
|
|
next_insert_id would be left unchanged, the next rows would also
|
|
|
|
be tried with the same insert id and would fail. Since the end
|
|
|
|
of a multi-row statement is unknown here, all following rows in
|
|
|
|
the queue would be dropped, regardless which thread added them.
|
|
|
|
After the queue is used up, next_insert_id is cleared and the
|
|
|
|
next run will succeed. This could even happen if these come from
|
|
|
|
the same multi-row statement as the current queue contents. That
|
|
|
|
way it would look somewhat random which rows are rejected after
|
|
|
|
a duplicate.
|
|
|
|
*/
|
|
|
|
thd.next_insert_id= 0;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2000-12-24 14:19:00 +01:00
|
|
|
if (using_ignore)
|
|
|
|
{
|
|
|
|
using_ignore=0;
|
|
|
|
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
|
|
|
|
}
|
2006-07-01 23:51:10 +02:00
|
|
|
if (using_opt_replace)
|
|
|
|
{
|
|
|
|
using_opt_replace= 0;
|
|
|
|
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
|
|
|
|
}
|
2003-04-02 00:15:20 +02:00
|
|
|
if (row->query && row->log_query && using_bin_log)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-12-06 10:38:56 +01:00
|
|
|
Query_log_event qinfo(&thd, row->query, row->query_length, 0, FALSE);
|
2003-04-02 00:15:20 +02:00
|
|
|
mysql_bin_log.write(&qinfo);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2005-01-06 12:00:13 +01:00
|
|
|
if (table->s->blob_fields)
|
2000-07-31 21:29:14 +02:00
|
|
|
free_delayed_insert_blobs(table);
|
|
|
|
thread_safe_sub(delayed_rows_in_use,1,&LOCK_delayed_status);
|
|
|
|
thread_safe_increment(delayed_insert_writes,&LOCK_delayed_status);
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
|
|
|
|
delete row;
|
2004-03-04 17:16:10 +01:00
|
|
|
/*
|
|
|
|
Let READ clients do something once in a while
|
|
|
|
We should however not break in the middle of a multi-line insert
|
|
|
|
if we have binary logging enabled as we don't want other commands
|
|
|
|
on this table until all entries has been processed
|
|
|
|
*/
|
|
|
|
if (group_count++ >= max_rows && (row= rows.head()) &&
|
2004-04-28 12:08:54 +02:00
|
|
|
(!(row->log_query & using_bin_log) ||
|
2004-03-04 17:16:10 +01:00
|
|
|
row->query))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
group_count=0;
|
|
|
|
if (stacked_inserts || tables_in_use) // Let these wait a while
|
|
|
|
{
|
|
|
|
if (tables_in_use)
|
|
|
|
pthread_cond_broadcast(&cond_client); // If waiting clients
|
|
|
|
thd.proc_info="reschedule";
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
|
|
|
|
{
|
|
|
|
/* This should never happen */
|
|
|
|
table->file->print_error(error,MYF(0));
|
|
|
|
sql_print_error("%s",thd.net.last_error);
|
2006-09-20 11:05:11 +02:00
|
|
|
DBUG_PRINT("error", ("HA_EXTRA_NO_CACHE failed in loop"));
|
2000-07-31 21:29:14 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2002-03-22 21:55:08 +01:00
|
|
|
query_cache_invalidate3(&thd, table, 1);
|
2000-07-31 21:29:14 +02:00
|
|
|
if (thr_reschedule_write_lock(*thd.lock->locks))
|
|
|
|
{
|
|
|
|
/* This should never happen */
|
2005-01-06 12:00:13 +01:00
|
|
|
sql_print_error(ER(ER_DELAYED_CANT_CHANGE_LOCK),table->s->table_name);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2001-11-27 01:50:20 +01:00
|
|
|
if (!using_bin_log)
|
|
|
|
table->file->extra(HA_EXTRA_WRITE_CACHE);
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
thd.proc_info="insert";
|
|
|
|
}
|
|
|
|
if (tables_in_use)
|
|
|
|
pthread_cond_broadcast(&cond_client); // If waiting clients
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
thd.proc_info=0;
|
|
|
|
table->next_number_field=0;
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
|
|
|
|
{ // This shouldn't happen
|
|
|
|
table->file->print_error(error,MYF(0));
|
|
|
|
sql_print_error("%s",thd.net.last_error);
|
2006-09-20 11:05:11 +02:00
|
|
|
DBUG_PRINT("error", ("HA_EXTRA_NO_CACHE failed after loop"));
|
2000-07-31 21:29:14 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2002-03-22 21:55:08 +01:00
|
|
|
query_cache_invalidate3(&thd, table, 1);
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
|
|
|
|
err:
|
2006-09-20 11:05:11 +02:00
|
|
|
DBUG_EXECUTE("error", max_rows= 0;);
|
2001-05-16 23:46:50 +02:00
|
|
|
/* Remove all not used rows */
|
|
|
|
while ((row=rows.get()))
|
|
|
|
{
|
|
|
|
delete row;
|
|
|
|
thread_safe_increment(delayed_insert_errors,&LOCK_delayed_status);
|
|
|
|
stacked_inserts--;
|
2006-09-20 11:05:11 +02:00
|
|
|
DBUG_EXECUTE("error", max_rows++;);
|
2001-05-16 23:46:50 +02:00
|
|
|
}
|
2006-09-20 11:05:11 +02:00
|
|
|
DBUG_PRINT("error", ("dropped %lu rows after an error", max_rows));
|
2000-07-31 21:29:14 +02:00
|
|
|
thread_safe_increment(delayed_insert_errors, &LOCK_delayed_status);
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2004-03-29 16:57:07 +02:00
|
|
|
#endif /* EMBEDDED_LIBRARY */
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
/***************************************************************************
|
2002-07-25 00:00:56 +02:00
|
|
|
Store records in INSERT ... SELECT *
|
2000-07-31 21:29:14 +02:00
|
|
|
***************************************************************************/
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
make insert specific preparation and checks after opening tables
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_insert_select_prepare()
|
|
|
|
thd thread handler
|
|
|
|
|
|
|
|
RETURN
|
2004-10-20 03:04:37 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE Error
|
2004-07-16 00:15:55 +02:00
|
|
|
*/
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_insert_select_prepare(THD *thd)
|
2004-07-16 00:15:55 +02:00
|
|
|
{
|
|
|
|
LEX *lex= thd->lex;
|
2005-07-04 02:24:25 +02:00
|
|
|
SELECT_LEX *select_lex= &lex->select_lex;
|
2004-12-30 23:44:00 +01:00
|
|
|
TABLE_LIST *first_select_leaf_table;
|
2004-07-16 00:15:55 +02:00
|
|
|
DBUG_ENTER("mysql_insert_select_prepare");
|
2005-07-03 13:17:52 +02:00
|
|
|
|
2004-09-03 14:18:40 +02:00
|
|
|
/*
|
|
|
|
SELECT_LEX do not belong to INSERT statement, so we can't add WHERE
|
2004-12-30 23:44:00 +01:00
|
|
|
clause if table is VIEW
|
2004-09-03 14:18:40 +02:00
|
|
|
*/
|
2005-07-03 13:17:52 +02:00
|
|
|
|
2005-07-04 02:24:25 +02:00
|
|
|
if (mysql_prepare_insert(thd, lex->query_tables,
|
2004-12-30 23:44:00 +01:00
|
|
|
lex->query_tables->table, lex->field_list, 0,
|
|
|
|
lex->update_list, lex->value_list,
|
|
|
|
lex->duplicates,
|
2005-07-04 02:24:25 +02:00
|
|
|
&select_lex->where, TRUE))
|
2004-11-25 01:23:13 +01:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-12-30 23:44:00 +01:00
|
|
|
|
2004-09-15 22:42:56 +02:00
|
|
|
/*
|
|
|
|
exclude first table from leaf tables list, because it belong to
|
|
|
|
INSERT
|
|
|
|
*/
|
2005-07-04 02:24:25 +02:00
|
|
|
DBUG_ASSERT(select_lex->leaf_tables != 0);
|
|
|
|
lex->leaf_tables_insert= select_lex->leaf_tables;
|
2004-09-15 22:42:56 +02:00
|
|
|
/* skip all leaf tables belonged to view where we are insert */
|
2005-07-04 02:24:25 +02:00
|
|
|
for (first_select_leaf_table= select_lex->leaf_tables->next_leaf;
|
2004-09-15 22:42:56 +02:00
|
|
|
first_select_leaf_table &&
|
|
|
|
first_select_leaf_table->belong_to_view &&
|
|
|
|
first_select_leaf_table->belong_to_view ==
|
|
|
|
lex->leaf_tables_insert->belong_to_view;
|
|
|
|
first_select_leaf_table= first_select_leaf_table->next_leaf)
|
|
|
|
{}
|
2005-07-04 02:24:25 +02:00
|
|
|
select_lex->leaf_tables= first_select_leaf_table;
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(FALSE);
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-06 22:55:36 +02:00
|
|
|
select_insert::select_insert(TABLE_LIST *table_list_par, TABLE *table_par,
|
2004-12-22 12:54:39 +01:00
|
|
|
List<Item> *fields_par,
|
2005-01-04 12:46:53 +01:00
|
|
|
List<Item> *update_fields,
|
|
|
|
List<Item> *update_values,
|
2004-12-22 12:54:39 +01:00
|
|
|
enum_duplicates duplic,
|
2004-09-06 22:55:36 +02:00
|
|
|
bool ignore_check_option_errors)
|
|
|
|
:table_list(table_list_par), table(table_par), fields(fields_par),
|
|
|
|
last_insert_id(0),
|
|
|
|
insert_into_view(table_list_par && table_list_par->view != 0)
|
|
|
|
{
|
|
|
|
bzero((char*) &info,sizeof(info));
|
2004-12-22 12:54:39 +01:00
|
|
|
info.handle_duplicates= duplic;
|
|
|
|
info.ignore= ignore_check_option_errors;
|
|
|
|
info.update_fields= update_fields;
|
|
|
|
info.update_values= update_values;
|
2004-09-06 22:55:36 +02:00
|
|
|
if (table_list_par)
|
2004-09-29 15:35:01 +02:00
|
|
|
info.view= (table_list_par->view ? table_list_par : 0);
|
2004-09-06 22:55:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
int
|
2002-05-08 22:14:40 +02:00
|
|
|
select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-06-20 14:07:00 +02:00
|
|
|
LEX *lex= thd->lex;
|
2005-07-01 06:05:42 +02:00
|
|
|
int res;
|
2007-01-22 13:14:38 +01:00
|
|
|
table_map map= 0;
|
2005-06-20 14:07:00 +02:00
|
|
|
SELECT_LEX *lex_current_select_save= lex->current_select;
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_ENTER("select_insert::prepare");
|
|
|
|
|
2002-05-08 22:14:40 +02:00
|
|
|
unit= u;
|
2005-06-20 14:07:00 +02:00
|
|
|
/*
|
|
|
|
Since table in which we are going to insert is added to the first
|
|
|
|
select, LEX::current_select should point to the first select while
|
|
|
|
we are fixing fields from insert list.
|
|
|
|
*/
|
|
|
|
lex->current_select= &lex->select_lex;
|
2005-06-20 14:58:02 +02:00
|
|
|
res= check_insert_fields(thd, table_list, *fields, values,
|
2007-01-22 13:14:38 +01:00
|
|
|
!insert_into_view, &map) ||
|
2005-08-10 23:17:53 +02:00
|
|
|
setup_fields(thd, 0, values, 0, 0, 0);
|
2005-08-12 18:27:54 +02:00
|
|
|
|
2005-08-10 23:17:53 +02:00
|
|
|
if (info.handle_duplicates == DUP_UPDATE)
|
|
|
|
{
|
2005-08-12 18:27:54 +02:00
|
|
|
/* Save the state of the current name resolution context. */
|
|
|
|
Name_resolution_context *context= &lex->select_lex.context;
|
2005-11-28 20:57:50 +01:00
|
|
|
Name_resolution_context_state ctx_state;
|
|
|
|
|
|
|
|
/* Save the state of the current name resolution context. */
|
|
|
|
ctx_state.save_state(context, table_list);
|
2005-08-12 18:27:54 +02:00
|
|
|
|
|
|
|
/* Perform name resolution only in the first table - 'table_list'. */
|
2005-08-10 23:17:53 +02:00
|
|
|
table_list->next_local= 0;
|
2005-08-12 18:27:54 +02:00
|
|
|
context->resolve_in_table_list_only(table_list);
|
|
|
|
|
2005-08-10 23:17:53 +02:00
|
|
|
lex->select_lex.no_wrap_view_item= TRUE;
|
2005-08-12 18:27:54 +02:00
|
|
|
res= res || check_update_fields(thd, context->table_list,
|
2007-01-22 13:14:38 +01:00
|
|
|
*info.update_fields, &map);
|
2005-08-10 23:17:53 +02:00
|
|
|
lex->select_lex.no_wrap_view_item= FALSE;
|
|
|
|
/*
|
|
|
|
When we are not using GROUP BY we can refer to other tables in the
|
|
|
|
ON DUPLICATE KEY part
|
|
|
|
*/
|
2005-08-12 18:27:54 +02:00
|
|
|
if (lex->select_lex.group_list.elements == 0)
|
|
|
|
{
|
2005-11-28 20:57:50 +01:00
|
|
|
context->table_list->next_local= ctx_state.save_next_local;
|
2005-08-18 02:12:42 +02:00
|
|
|
/* first_name_resolution_table was set by resolve_in_table_list_only() */
|
2005-08-12 18:27:54 +02:00
|
|
|
context->first_name_resolution_table->
|
2005-11-28 20:57:50 +01:00
|
|
|
next_name_resolution_table= ctx_state.save_next_local;
|
2005-08-12 18:27:54 +02:00
|
|
|
}
|
2005-08-10 23:17:53 +02:00
|
|
|
res= res || setup_fields(thd, 0, *info.update_values, 1, 0, 0);
|
2005-08-12 18:27:54 +02:00
|
|
|
|
|
|
|
/* Restore the current context. */
|
2005-11-28 20:57:50 +01:00
|
|
|
ctx_state.restore_state(context, table_list);
|
2005-08-10 23:17:53 +02:00
|
|
|
}
|
2005-08-12 18:27:54 +02:00
|
|
|
|
2005-06-20 14:07:00 +02:00
|
|
|
lex->current_select= lex_current_select_save;
|
|
|
|
if (res)
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_RETURN(1);
|
2004-09-15 22:42:56 +02:00
|
|
|
/*
|
|
|
|
if it is INSERT into join view then check_insert_fields already found
|
|
|
|
real table for insert
|
|
|
|
*/
|
|
|
|
table= table_list->table;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Is table which we are changing used somewhere in other parts of
|
|
|
|
query
|
|
|
|
*/
|
2005-06-20 14:58:02 +02:00
|
|
|
if (!(lex->current_select->options & OPTION_BUFFER_RESULT) &&
|
2005-12-20 16:35:05 +01:00
|
|
|
unique_table(thd, table_list, table_list->next_global))
|
2004-09-15 22:42:56 +02:00
|
|
|
{
|
|
|
|
/* Using same table for INSERT and SELECT */
|
2005-06-20 14:58:02 +02:00
|
|
|
lex->current_select->options|= OPTION_BUFFER_RESULT;
|
|
|
|
lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
|
2004-09-15 22:42:56 +02:00
|
|
|
}
|
2006-03-29 12:53:00 +02:00
|
|
|
else if (!thd->prelocked_mode)
|
2005-01-19 21:20:55 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
We must not yet prepare the result table if it is the same as one of the
|
|
|
|
source tables (INSERT SELECT). The preparation may disable
|
|
|
|
indexes on the result table, which may be used during the select, if it
|
|
|
|
is the same table (Bug #6034). Do the preparation after the select phase
|
|
|
|
in select_insert::prepare2().
|
2006-03-29 12:53:00 +02:00
|
|
|
We won't start bulk inserts at all if this statement uses functions or
|
|
|
|
should invoke triggers since they may access to the same table too.
|
2005-01-19 21:20:55 +01:00
|
|
|
*/
|
|
|
|
table->file->start_bulk_insert((ha_rows) 0);
|
|
|
|
}
|
2005-01-06 12:00:13 +01:00
|
|
|
restore_record(table,s->default_values); // Get empty record
|
2000-08-16 04:14:02 +02:00
|
|
|
table->next_number_field=table->found_next_number_field;
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->cuted_fields=0;
|
2005-02-02 21:39:21 +01:00
|
|
|
if (info.ignore || info.handle_duplicates != DUP_ERROR)
|
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
2006-07-01 23:51:10 +02:00
|
|
|
if (info.handle_duplicates == DUP_REPLACE)
|
|
|
|
{
|
|
|
|
if (!table->triggers || !table->triggers->has_delete_triggers())
|
|
|
|
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
|
|
|
|
table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
|
|
|
|
}
|
2004-09-28 19:08:00 +02:00
|
|
|
thd->no_trans_update= 0;
|
2005-01-04 12:46:53 +01:00
|
|
|
thd->abort_on_warning= (!info.ignore &&
|
2004-09-28 19:08:00 +02:00
|
|
|
(thd->variables.sql_mode &
|
|
|
|
(MODE_STRICT_TRANS_TABLES |
|
|
|
|
MODE_STRICT_ALL_TABLES)));
|
2005-07-01 06:05:42 +02:00
|
|
|
res= ((fields->elements &&
|
|
|
|
check_that_all_fields_are_given_values(thd, table, table_list)) ||
|
|
|
|
table_list->prepare_where(thd, 0, TRUE) ||
|
|
|
|
table_list->prepare_check_option(thd));
|
2006-07-01 23:51:10 +02:00
|
|
|
|
|
|
|
if (!res)
|
|
|
|
mark_fields_used_by_triggers_for_insert_stmt(thd, table,
|
|
|
|
info.handle_duplicates);
|
2005-07-01 06:05:42 +02:00
|
|
|
DBUG_RETURN(res);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2004-08-24 18:17:11 +02:00
|
|
|
|
2005-01-19 21:20:55 +01:00
|
|
|
/*
|
|
|
|
Finish the preparation of the result table.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
select_insert::prepare2()
|
|
|
|
void
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
If the result table is the same as one of the source tables (INSERT SELECT),
|
|
|
|
the result table is not finally prepared at the join prepair phase.
|
|
|
|
Do the final preparation now.
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 OK
|
|
|
|
*/
|
|
|
|
|
|
|
|
int select_insert::prepare2(void)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("select_insert::prepare2");
|
2006-03-29 12:53:00 +02:00
|
|
|
if (thd->lex->current_select->options & OPTION_BUFFER_RESULT &&
|
|
|
|
!thd->prelocked_mode)
|
2005-01-19 21:20:55 +01:00
|
|
|
table->file->start_bulk_insert((ha_rows) 0);
|
2005-02-02 21:39:21 +01:00
|
|
|
DBUG_RETURN(0);
|
2005-01-19 21:20:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-24 18:17:11 +02:00
|
|
|
void select_insert::cleanup()
|
|
|
|
{
|
|
|
|
/* select_insert/select_create are never re-used in prepared statement */
|
|
|
|
DBUG_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
select_insert::~select_insert()
|
|
|
|
{
|
2004-09-17 02:08:23 +02:00
|
|
|
DBUG_ENTER("~select_insert");
|
2000-07-31 21:29:14 +02:00
|
|
|
if (table)
|
|
|
|
{
|
|
|
|
table->next_number_field=0;
|
2004-04-06 21:35:26 +02:00
|
|
|
table->file->reset();
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2003-10-11 22:26:39 +02:00
|
|
|
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
|
2004-09-28 19:08:00 +02:00
|
|
|
thd->abort_on_warning= 0;
|
2004-09-17 02:08:23 +02:00
|
|
|
DBUG_VOID_RETURN;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool select_insert::send_data(List<Item> &values)
|
|
|
|
{
|
2004-04-28 02:37:45 +02:00
|
|
|
DBUG_ENTER("select_insert::send_data");
|
2004-12-03 15:02:29 +01:00
|
|
|
bool error=0;
|
2002-05-08 22:14:40 +02:00
|
|
|
if (unit->offset_limit_cnt)
|
2000-07-31 21:29:14 +02:00
|
|
|
{ // using limit offset,count
|
2002-05-08 22:14:40 +02:00
|
|
|
unit->offset_limit_cnt--;
|
2004-04-28 02:37:45 +02:00
|
|
|
DBUG_RETURN(0);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-12-06 10:38:56 +01:00
|
|
|
|
2004-12-06 16:15:54 +01:00
|
|
|
thd->count_cuted_fields= CHECK_FIELD_WARN; // Calculate cuted fields
|
2004-12-03 15:02:29 +01:00
|
|
|
store_values(values);
|
|
|
|
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
|
2004-12-06 10:38:56 +01:00
|
|
|
if (thd->net.report_error)
|
|
|
|
DBUG_RETURN(1);
|
2004-12-06 16:15:54 +01:00
|
|
|
if (table_list) // Not CREATE ... SELECT
|
|
|
|
{
|
2005-01-04 12:46:53 +01:00
|
|
|
switch (table_list->view_check_option(thd, info.ignore)) {
|
2004-12-06 16:15:54 +01:00
|
|
|
case VIEW_CHECK_SKIP:
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
case VIEW_CHECK_ERROR:
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2004-09-03 14:18:40 +02:00
|
|
|
}
|
2005-05-24 20:19:33 +02:00
|
|
|
if (!(error= write_record(thd, table, &info)))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-06-22 19:07:28 +02:00
|
|
|
if (table->triggers || info.handle_duplicates == DUP_UPDATE)
|
2005-05-24 20:19:33 +02:00
|
|
|
{
|
|
|
|
/*
|
2005-06-22 19:07:28 +02:00
|
|
|
Restore fields of the record since it is possible that they were
|
|
|
|
changed by ON DUPLICATE KEY UPDATE clause.
|
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
If triggers exist then whey can modify some fields which were not
|
|
|
|
originally touched by INSERT ... SELECT, so we have to restore
|
|
|
|
their original values for the next row.
|
|
|
|
*/
|
|
|
|
restore_record(table, s->default_values);
|
|
|
|
}
|
|
|
|
if (table->next_number_field)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Clear auto-increment field for the next record, if triggers are used
|
|
|
|
we will clear it twice, but this should be cheap.
|
|
|
|
*/
|
|
|
|
table->next_number_field->reset();
|
|
|
|
if (!last_insert_id && thd->insert_id_used)
|
2006-10-02 12:28:23 +02:00
|
|
|
last_insert_id= thd->last_insert_id;
|
2005-05-24 20:19:33 +02:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-12-03 15:02:29 +01:00
|
|
|
DBUG_RETURN(error);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-03 15:02:29 +01:00
|
|
|
void select_insert::store_values(List<Item> &values)
|
|
|
|
{
|
|
|
|
if (fields->elements)
|
2005-05-24 20:19:33 +02:00
|
|
|
fill_record_n_invoke_before_triggers(thd, *fields, values, 1,
|
|
|
|
table->triggers, TRG_EVENT_INSERT);
|
2004-12-03 15:02:29 +01:00
|
|
|
else
|
2005-05-24 20:19:33 +02:00
|
|
|
fill_record_n_invoke_before_triggers(thd, table->field, values, 1,
|
|
|
|
table->triggers, TRG_EVENT_INSERT);
|
2004-12-03 15:02:29 +01:00
|
|
|
}
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
void select_insert::send_error(uint errcode,const char *err)
|
|
|
|
{
|
2003-10-08 11:01:58 +02:00
|
|
|
DBUG_ENTER("select_insert::send_error");
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
my_message(errcode, err, MYF(0));
|
2003-10-08 17:53:31 +02:00
|
|
|
|
|
|
|
if (!table)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This can only happen when using CREATE ... SELECT and the table was not
|
|
|
|
created becasue of an syntax error
|
|
|
|
*/
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
2006-03-29 12:53:00 +02:00
|
|
|
if (!thd->prelocked_mode)
|
|
|
|
table->file->end_bulk_insert();
|
2003-10-08 11:01:58 +02:00
|
|
|
/*
|
|
|
|
If at least one row has been inserted/modified and will stay in the table
|
|
|
|
(the table doesn't have transactions) (example: we got a duplicate key
|
|
|
|
error while inserting into a MyISAM table) we must write to the binlog (and
|
|
|
|
the error code will make the slave stop).
|
2003-05-24 16:43:53 +02:00
|
|
|
*/
|
2004-04-07 16:04:28 +02:00
|
|
|
if ((info.copied || info.deleted || info.updated) &&
|
2004-03-15 18:36:16 +01:00
|
|
|
!table->file->has_transactions())
|
2003-05-24 16:43:53 +02:00
|
|
|
{
|
|
|
|
if (last_insert_id)
|
|
|
|
thd->insert_id(last_insert_id); // For binary log
|
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
|
|
|
Query_log_event qinfo(thd, thd->query, thd->query_length,
|
2004-12-03 12:13:51 +01:00
|
|
|
table->file->has_transactions(), FALSE);
|
2003-05-24 16:43:53 +02:00
|
|
|
mysql_bin_log.write(&qinfo);
|
|
|
|
}
|
2005-01-06 12:00:13 +01:00
|
|
|
if (!table->s->tmp_table)
|
2004-04-07 16:04:28 +02:00
|
|
|
thd->options|=OPTION_STATUS_NO_TRANS_UPDATE;
|
2003-05-24 16:43:53 +02:00
|
|
|
}
|
2004-03-15 18:36:16 +01:00
|
|
|
if (info.copied || info.deleted || info.updated)
|
2003-12-21 01:07:45 +01:00
|
|
|
{
|
2002-03-22 21:55:08 +01:00
|
|
|
query_cache_invalidate3(thd, table, 1);
|
2003-12-21 01:07:45 +01:00
|
|
|
}
|
2003-05-26 18:10:43 +02:00
|
|
|
ha_rollback_stmt(thd);
|
2003-10-08 11:01:58 +02:00
|
|
|
DBUG_VOID_RETURN;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool select_insert::send_eof()
|
|
|
|
{
|
2000-08-21 02:00:52 +02:00
|
|
|
int error,error2;
|
2003-11-20 21:06:25 +01:00
|
|
|
DBUG_ENTER("select_insert::send_eof");
|
|
|
|
|
2006-03-29 12:53:00 +02:00
|
|
|
error= (!thd->prelocked_mode) ? table->file->end_bulk_insert():0;
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
|
2006-07-01 23:51:10 +02:00
|
|
|
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
|
2003-03-16 15:28:30 +01:00
|
|
|
|
2003-05-26 19:48:40 +02:00
|
|
|
/*
|
|
|
|
We must invalidate the table in the query cache before binlog writing
|
2005-01-16 13:16:23 +01:00
|
|
|
and ha_autocommit_or_rollback
|
2003-05-26 19:48:40 +02:00
|
|
|
*/
|
2003-05-26 18:10:43 +02:00
|
|
|
|
2004-03-15 18:36:16 +01:00
|
|
|
if (info.copied || info.deleted || info.updated)
|
2003-08-29 12:44:35 +02:00
|
|
|
{
|
2003-05-26 18:10:43 +02:00
|
|
|
query_cache_invalidate3(thd, table, 1);
|
2005-01-06 12:00:13 +01:00
|
|
|
if (!(table->file->has_transactions() || table->s->tmp_table))
|
2003-08-29 12:44:35 +02:00
|
|
|
thd->options|=OPTION_STATUS_NO_TRANS_UPDATE;
|
|
|
|
}
|
2003-05-26 18:10:43 +02:00
|
|
|
|
2003-05-24 16:43:53 +02:00
|
|
|
if (last_insert_id)
|
|
|
|
thd->insert_id(last_insert_id); // For binary log
|
2003-03-16 15:28:30 +01:00
|
|
|
/* Write to binlog before commiting transaction */
|
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
2003-12-16 11:10:50 +01:00
|
|
|
if (!error)
|
|
|
|
thd->clear_error();
|
2003-03-16 15:28:30 +01:00
|
|
|
Query_log_event qinfo(thd, thd->query, thd->query_length,
|
2004-12-03 12:13:51 +01:00
|
|
|
table->file->has_transactions(), FALSE);
|
2003-03-16 15:28:30 +01:00
|
|
|
mysql_bin_log.write(&qinfo);
|
|
|
|
}
|
2000-08-21 02:00:52 +02:00
|
|
|
if ((error2=ha_autocommit_or_rollback(thd,error)) && ! error)
|
|
|
|
error=error2;
|
|
|
|
if (error)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
table->file->print_error(error,MYF(0));
|
2003-11-20 21:06:25 +01:00
|
|
|
DBUG_RETURN(1);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2003-11-20 21:06:25 +01:00
|
|
|
char buff[160];
|
2004-12-31 11:04:35 +01:00
|
|
|
if (info.ignore)
|
2003-11-20 21:06:25 +01:00
|
|
|
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
|
|
|
|
(ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
|
2000-07-31 21:29:14 +02:00
|
|
|
else
|
2003-11-20 21:06:25 +01:00
|
|
|
sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
|
2004-05-19 04:09:10 +02:00
|
|
|
(ulong) (info.deleted+info.updated), (ulong) thd->cuted_fields);
|
2004-05-04 13:45:20 +02:00
|
|
|
thd->row_count_func= info.copied+info.deleted+info.updated;
|
2004-07-16 08:47:17 +02:00
|
|
|
::send_ok(thd, (ulong) thd->row_count_func, last_insert_id, buff);
|
2003-11-20 21:06:25 +01:00
|
|
|
DBUG_RETURN(0);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
2002-07-25 00:00:56 +02:00
|
|
|
CREATE TABLE (SELECT) ...
|
2000-07-31 21:29:14 +02:00
|
|
|
***************************************************************************/
|
|
|
|
|
2006-05-09 14:39:11 +02:00
|
|
|
/*
|
|
|
|
Create table from lists of fields and items (or open existing table
|
|
|
|
with same name).
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
create_table_from_items()
|
|
|
|
thd in Thread object
|
|
|
|
create_info in Create information (like MAX_ROWS, ENGINE or
|
|
|
|
temporary table flag)
|
|
|
|
create_table in Pointer to TABLE_LIST object providing database
|
|
|
|
and name for table to be created or to be open
|
2006-12-11 23:50:12 +01:00
|
|
|
alter_info in/out Initial list of columns and indexes for the table
|
|
|
|
to be created
|
2006-05-09 14:39:11 +02:00
|
|
|
items in List of items which should be used to produce rest
|
|
|
|
of fields for the table (corresponding fields will
|
2006-12-11 23:50:12 +01:00
|
|
|
be added to the end of alter_info->create_list)
|
2006-05-09 14:39:11 +02:00
|
|
|
lock out Pointer to the MYSQL_LOCK object for table created
|
|
|
|
(open) will be returned in this parameter. Since
|
|
|
|
this table is not included in THD::lock caller is
|
|
|
|
responsible for explicitly unlocking this table.
|
|
|
|
|
|
|
|
NOTES
|
|
|
|
If 'create_info->options' bitmask has HA_LEX_CREATE_IF_NOT_EXISTS
|
|
|
|
flag and table with name provided already exists then this function will
|
|
|
|
simply open existing table.
|
|
|
|
Also note that create, open and lock sequence in this function is not
|
|
|
|
atomic and thus contains gap for deadlock and can cause other troubles.
|
|
|
|
Since this function contains some logic specific to CREATE TABLE ... SELECT
|
|
|
|
it should be changed before it can be used in other contexts.
|
|
|
|
|
|
|
|
RETURN VALUES
|
|
|
|
non-zero Pointer to TABLE object for table created or opened
|
|
|
|
0 Error
|
|
|
|
*/
|
|
|
|
|
|
|
|
static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
|
|
|
|
TABLE_LIST *create_table,
|
2006-12-11 23:50:12 +01:00
|
|
|
Alter_info *alter_info,
|
|
|
|
List<Item> *items,
|
2006-05-09 14:39:11 +02:00
|
|
|
MYSQL_LOCK **lock)
|
|
|
|
{
|
|
|
|
TABLE tmp_table; // Used during 'create_field()'
|
|
|
|
TABLE *table= 0;
|
|
|
|
uint select_field_count= items->elements;
|
|
|
|
/* Add selected items to field list */
|
|
|
|
List_iterator_fast<Item> it(*items);
|
|
|
|
Item *item;
|
|
|
|
Field *tmp_field;
|
|
|
|
bool not_used;
|
|
|
|
DBUG_ENTER("create_table_from_items");
|
|
|
|
|
|
|
|
tmp_table.alias= 0;
|
|
|
|
tmp_table.timestamp_field= 0;
|
|
|
|
tmp_table.s= &tmp_table.share_not_to_be_used;
|
|
|
|
tmp_table.s->db_create_options=0;
|
|
|
|
tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
|
|
|
|
tmp_table.s->db_low_byte_first= test(create_info->db_type == DB_TYPE_MYISAM ||
|
|
|
|
create_info->db_type == DB_TYPE_HEAP);
|
|
|
|
tmp_table.null_row=tmp_table.maybe_null=0;
|
|
|
|
|
|
|
|
while ((item=it++))
|
|
|
|
{
|
|
|
|
create_field *cr_field;
|
2006-05-24 10:56:59 +02:00
|
|
|
Field *field, *def_field;
|
2006-05-09 14:39:11 +02:00
|
|
|
if (item->type() == Item::FUNC_ITEM)
|
2006-05-24 10:56:59 +02:00
|
|
|
field= item->tmp_table_field(&tmp_table);
|
2006-05-09 14:39:11 +02:00
|
|
|
else
|
2006-05-24 10:56:59 +02:00
|
|
|
field= create_tmp_field(thd, &tmp_table, item, item->type(),
|
|
|
|
(Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0,
|
|
|
|
0);
|
2006-05-09 14:39:11 +02:00
|
|
|
if (!field ||
|
|
|
|
!(cr_field=new create_field(field,(item->type() == Item::FIELD_ITEM ?
|
|
|
|
((Item_field *)item)->field :
|
|
|
|
(Field*) 0))))
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
if (item->maybe_null)
|
|
|
|
cr_field->flags &= ~NOT_NULL_FLAG;
|
2006-12-11 23:50:12 +01:00
|
|
|
alter_info->create_list.push_back(cr_field);
|
2006-05-09 14:39:11 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
create and lock table
|
|
|
|
|
|
|
|
We don't log the statement, it will be logged later.
|
|
|
|
|
|
|
|
If this is a HEAP table, the automatic DELETE FROM which is written to the
|
|
|
|
binlog when a HEAP table is opened for the first time since startup, must
|
|
|
|
not be written: 1) it would be wrong (imagine we're in CREATE SELECT: we
|
|
|
|
don't want to delete from it) 2) it would be written before the CREATE
|
|
|
|
TABLE, which is a wrong order. So we keep binary logging disabled when we
|
|
|
|
open_table().
|
|
|
|
NOTE: By locking table which we just have created (or for which we just have
|
|
|
|
have found that it already exists) separately from other tables used by the
|
|
|
|
statement we create potential window for deadlock.
|
|
|
|
TODO: create and open should be done atomic !
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
tmp_disable_binlog(thd);
|
|
|
|
if (!mysql_create_table(thd, create_table->db, create_table->table_name,
|
2006-12-11 23:50:12 +01:00
|
|
|
create_info, alter_info, 0, select_field_count))
|
2006-05-09 14:39:11 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
If we are here in prelocked mode we either create temporary table
|
|
|
|
or prelocked mode is caused by the SELECT part of this statement.
|
|
|
|
*/
|
|
|
|
DBUG_ASSERT(!thd->prelocked_mode ||
|
|
|
|
create_info->options & HA_LEX_CREATE_TMP_TABLE ||
|
|
|
|
thd->lex->requires_prelocking());
|
|
|
|
|
|
|
|
/*
|
|
|
|
NOTE: We don't want to ignore set of locked tables here if we are
|
|
|
|
under explicit LOCK TABLES since it will open gap for deadlock
|
|
|
|
too wide (and also is not backward compatible).
|
|
|
|
*/
|
|
|
|
if (! (table= open_table(thd, create_table, thd->mem_root, (bool*) 0,
|
|
|
|
(MYSQL_LOCK_IGNORE_FLUSH |
|
|
|
|
((thd->prelocked_mode == PRELOCKED) ?
|
|
|
|
MYSQL_OPEN_IGNORE_LOCKED_TABLES:0)))))
|
|
|
|
quick_rm_table(create_info->db_type, create_table->db,
|
|
|
|
table_case_name(create_info, create_table->table_name));
|
|
|
|
}
|
|
|
|
reenable_binlog(thd);
|
|
|
|
if (!table) // open failed
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
FIXME: What happens if trigger manages to be created while we are
|
|
|
|
obtaining this lock ? May be it is sensible just to disable
|
|
|
|
trigger execution in this case ? Or will MYSQL_LOCK_IGNORE_FLUSH
|
|
|
|
save us from that ?
|
|
|
|
*/
|
|
|
|
table->reginfo.lock_type=TL_WRITE;
|
|
|
|
if (! ((*lock)= mysql_lock_tables(thd, &table, 1,
|
|
|
|
MYSQL_LOCK_IGNORE_FLUSH, ¬_used)))
|
|
|
|
{
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_open));
|
|
|
|
hash_delete(&open_cache,(byte*) table);
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_open));
|
|
|
|
quick_rm_table(create_info->db_type, create_table->db,
|
|
|
|
table_case_name(create_info, create_table->table_name));
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
table->file->extra(HA_EXTRA_WRITE_CACHE);
|
|
|
|
DBUG_RETURN(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
int
|
2002-05-08 22:14:40 +02:00
|
|
|
select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
DBUG_ENTER("select_create::prepare");
|
|
|
|
|
2002-05-08 22:14:40 +02:00
|
|
|
unit= u;
|
2004-07-16 00:15:55 +02:00
|
|
|
table= create_table_from_items(thd, create_info, create_table,
|
A fix and test cases for
Bug#4968 "Stored procedure crash if cursor opened on altered table"
Bug#19733 "Repeated alter, or repeated create/drop, fails"
Bug#19182 "CREATE TABLE bar (m INT) SELECT n FROM foo; doesn't work from
stored procedure."
Bug#6895 "Prepared Statements: ALTER TABLE DROP COLUMN does nothing"
Bug#22060 "ALTER TABLE x AUTO_INCREMENT=y in SP crashes server"
Test cases for bugs 4968, 19733, 6895 will be added in 5.0.
Re-execution of CREATE DATABASE, CREATE TABLE and ALTER TABLE
statements in stored routines or as prepared statements caused
incorrect results (and crashes in versions prior to 5.0.25).
In 5.1 the problem occured only for CREATE DATABASE, CREATE TABLE
SELECT and CREATE TABLE with INDEX/DATA DIRECTOY options).
The problem of bugs 4968, 19733, 19282 and 6895 was that functions
mysql_prepare_table, mysql_create_table and mysql_alter_table were not
re-execution friendly: during their operation they used to modify contents
of LEX (members create_info, alter_info, key_list, create_list),
thus making the LEX unusable for the next execution.
In particular, these functions removed processed columns and keys from
create_list, key_list and drop_list. Search the code in sql_table.cc
for drop_it.remove() and similar patterns to find evidence.
The fix is to supply to these functions a usable copy of each of the
above structures at every re-execution of an SQL statement.
To simplify memory management, LEX::key_list and LEX::create_list
were added to LEX::alter_info, a fresh copy of which is created for
every execution.
The problem of crashing bug 22060 stemmed from the fact that the above
metnioned functions were not only modifying HA_CREATE_INFO structure in
LEX, but also were changing it to point to areas in volatile memory of
the execution memory root.
The patch solves this problem by creating and using an on-stack
copy of HA_CREATE_INFO (note that code in 5.1 already creates and
uses a copy of this structure in mysql_create_table()/alter_table(),
but this approach didn't work well for CREATE TABLE SELECT statement).
2006-12-08 00:20:09 +01:00
|
|
|
alter_info, &values, &lock);
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!table)
|
|
|
|
DBUG_RETURN(-1); // abort() deletes table
|
|
|
|
|
2005-01-06 12:00:13 +01:00
|
|
|
if (table->s->fields < values.elements)
|
2003-10-06 20:02:27 +02:00
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
|
2003-10-06 20:02:27 +02:00
|
|
|
DBUG_RETURN(-1);
|
|
|
|
}
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
/* First field to copy */
|
2006-01-05 23:47:49 +01:00
|
|
|
field= table->field+table->s->fields - values.elements;
|
|
|
|
|
|
|
|
/* Mark all fields that are given values */
|
|
|
|
for (Field **f= field ; *f ; f++)
|
|
|
|
(*f)->query_id= thd->query_id;
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2004-04-02 08:12:53 +02:00
|
|
|
/* Don't set timestamp if used */
|
2004-10-01 16:54:06 +02:00
|
|
|
table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
|
2004-04-07 16:04:28 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
table->next_number_field=table->found_next_number_field;
|
|
|
|
|
2005-01-06 12:00:13 +01:00
|
|
|
restore_record(table,s->default_values); // Get empty record
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->cuted_fields=0;
|
2005-01-10 20:55:05 +01:00
|
|
|
if (info.ignore || info.handle_duplicates != DUP_ERROR)
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
2006-07-01 23:51:10 +02:00
|
|
|
if (info.handle_duplicates == DUP_REPLACE)
|
|
|
|
{
|
|
|
|
if (!table->triggers || !table->triggers->has_delete_triggers())
|
|
|
|
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
|
|
|
|
table->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);
|
|
|
|
}
|
2006-03-29 12:53:00 +02:00
|
|
|
if (!thd->prelocked_mode)
|
|
|
|
table->file->start_bulk_insert((ha_rows) 0);
|
2004-09-28 19:08:00 +02:00
|
|
|
thd->no_trans_update= 0;
|
2005-01-04 12:46:53 +01:00
|
|
|
thd->abort_on_warning= (!info.ignore &&
|
2004-09-28 19:08:00 +02:00
|
|
|
(thd->variables.sql_mode &
|
|
|
|
(MODE_STRICT_TRANS_TABLES |
|
|
|
|
MODE_STRICT_ALL_TABLES)));
|
2005-07-01 06:05:42 +02:00
|
|
|
DBUG_RETURN(check_that_all_fields_are_given_values(thd, table,
|
|
|
|
table_list));
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-03 15:02:29 +01:00
|
|
|
void select_create::store_values(List<Item> &values)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-05-24 20:19:33 +02:00
|
|
|
fill_record_n_invoke_before_triggers(thd, field, values, 1,
|
|
|
|
table->triggers, TRG_EVENT_INSERT);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2001-08-02 05:29:50 +02:00
|
|
|
|
2004-12-03 00:05:11 +01:00
|
|
|
void select_create::send_error(uint errcode,const char *err)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Disable binlog, because we "roll back" partial inserts in ::abort
|
|
|
|
by removing the table, even for non-transactional tables.
|
|
|
|
*/
|
|
|
|
tmp_disable_binlog(thd);
|
|
|
|
select_insert::send_error(errcode, err);
|
|
|
|
reenable_binlog(thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2001-08-02 05:29:50 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
bool select_create::send_eof()
|
|
|
|
{
|
|
|
|
bool tmp=select_insert::send_eof();
|
|
|
|
if (tmp)
|
|
|
|
abort();
|
|
|
|
else
|
|
|
|
{
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
|
2006-07-01 23:51:10 +02:00
|
|
|
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
|
2000-07-31 21:29:14 +02:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_open));
|
|
|
|
mysql_unlock_tables(thd, lock);
|
2002-08-05 17:50:38 +02:00
|
|
|
/*
|
|
|
|
TODO:
|
|
|
|
Check if we can remove the following two rows.
|
|
|
|
We should be able to just keep the table in the table cache.
|
|
|
|
*/
|
2005-01-06 12:00:13 +01:00
|
|
|
if (!table->s->tmp_table)
|
2004-10-26 18:30:01 +02:00
|
|
|
{
|
2005-01-06 12:00:13 +01:00
|
|
|
ulong version= table->s->version;
|
2000-11-13 22:55:10 +01:00
|
|
|
hash_delete(&open_cache,(byte*) table);
|
2004-10-26 18:30:01 +02:00
|
|
|
/* Tell threads waiting for refresh that something has happened */
|
2004-10-27 20:56:17 +02:00
|
|
|
if (version != refresh_version)
|
2006-06-26 19:14:35 +02:00
|
|
|
broadcast_refresh();
|
2004-10-26 18:30:01 +02:00
|
|
|
}
|
2003-10-06 20:02:27 +02:00
|
|
|
lock=0;
|
2001-08-02 05:29:50 +02:00
|
|
|
table=0;
|
2000-07-31 21:29:14 +02:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_open));
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void select_create::abort()
|
|
|
|
{
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_open));
|
|
|
|
if (lock)
|
|
|
|
{
|
|
|
|
mysql_unlock_tables(thd, lock);
|
|
|
|
lock=0;
|
|
|
|
}
|
|
|
|
if (table)
|
|
|
|
{
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
|
2006-07-01 23:51:10 +02:00
|
|
|
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
|
2005-01-06 12:00:13 +01:00
|
|
|
enum db_type table_type=table->s->db_type;
|
|
|
|
if (!table->s->tmp_table)
|
2004-07-18 14:34:35 +02:00
|
|
|
{
|
2005-01-06 12:00:13 +01:00
|
|
|
ulong version= table->s->version;
|
2000-11-13 22:55:10 +01:00
|
|
|
hash_delete(&open_cache,(byte*) table);
|
2004-07-18 14:34:35 +02:00
|
|
|
if (!create_info->table_existed)
|
2005-01-06 12:00:13 +01:00
|
|
|
quick_rm_table(table_type, create_table->db, create_table->table_name);
|
2004-10-26 18:30:01 +02:00
|
|
|
/* Tell threads waiting for refresh that something has happened */
|
2004-10-27 20:56:17 +02:00
|
|
|
if (version != refresh_version)
|
2006-06-26 19:14:35 +02:00
|
|
|
broadcast_refresh();
|
2004-07-18 14:34:35 +02:00
|
|
|
}
|
|
|
|
else if (!create_info->table_existed)
|
2005-01-06 12:00:13 +01:00
|
|
|
close_temporary_table(thd, create_table->db, create_table->table_name);
|
2000-07-31 21:29:14 +02:00
|
|
|
table=0;
|
|
|
|
}
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_open));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2002-07-25 00:00:56 +02:00
|
|
|
Instansiate templates
|
2000-07-31 21:29:14 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2005-06-22 11:08:28 +02:00
|
|
|
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
|
2001-08-02 05:29:50 +02:00
|
|
|
template class List_iterator_fast<List_item>;
|
2004-03-29 16:57:07 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2000-07-31 21:29:14 +02:00
|
|
|
template class I_List<delayed_insert>;
|
|
|
|
template class I_List_iterator<delayed_insert>;
|
|
|
|
template class I_List<delayed_row>;
|
2004-03-29 16:57:07 +02:00
|
|
|
#endif /* EMBEDDED_LIBRARY */
|
2005-06-22 11:08:28 +02:00
|
|
|
#endif /* HAVE_EXPLICIT_TEMPLATE_INSTANTIATION */
|