2000-07-31 21:29:14 +02:00
|
|
|
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
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 */
|
|
|
|
|
|
|
|
#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);
|
2002-11-07 18:25:27 +01:00
|
|
|
extern "C" pthread_handler_decl(handle_delayed_insert,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
|
2004-09-15 22:42:56 +02:00
|
|
|
static bool check_view_insertability(TABLE_LIST *view, ulong query_id);
|
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
|
|
|
|
|
|
|
|
/*
|
2004-04-02 08:12:53 +02:00
|
|
|
Check if insert fields are correct.
|
2004-10-01 16:54:06 +02:00
|
|
|
Sets table->timestamp_field_type to TIMESTAMP_NO_AUTO_SET or leaves it
|
|
|
|
as is, depending on if timestamp should be updated or not.
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
static int
|
|
|
|
check_insert_fields(THD *thd, TABLE_LIST *table_list, List<Item> &fields,
|
|
|
|
List<Item> &values, ulong counter, bool check_unique)
|
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)
|
|
|
|
{
|
|
|
|
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias, "INSERT");
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(table_list->view &&
|
|
|
|
table_list->ancestor && table_list->ancestor->next_local);
|
|
|
|
my_error(ER_VIEW_NO_INSERT_FIELD_LIST, MYF(0),
|
|
|
|
table_list->view_db.str, table_list->view_name.str);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
if (values.elements != table->fields)
|
|
|
|
{
|
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
|
|
|
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,
|
2004-07-16 00:15:55 +02:00
|
|
|
table->table_cache_key, table->real_name,
|
|
|
|
&fields))
|
|
|
|
return -1;
|
|
|
|
}
|
2003-09-26 12:33:13 +02:00
|
|
|
#endif
|
2004-10-01 16:54:06 +02:00
|
|
|
table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Part field list
|
2004-09-03 20:43:04 +02:00
|
|
|
TABLE_LIST *save_next;
|
2004-07-23 08:20:58 +02:00
|
|
|
int res;
|
2000-07-31 21:29:14 +02:00
|
|
|
if (fields.elements != values.elements)
|
|
|
|
{
|
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
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
thd->dupp_field=0;
|
2004-07-23 08:20:58 +02:00
|
|
|
thd->lex->select_lex.no_wrap_view_item= 1;
|
2004-09-04 14:02:57 +02:00
|
|
|
save_next= table_list->next_local; // fields only from first table
|
|
|
|
table_list->next_local= 0;
|
2004-07-23 08:20:58 +02:00
|
|
|
res= setup_fields(thd, 0, table_list, fields, 1, 0, 0);
|
|
|
|
table_list->next_local= save_next;
|
2004-09-04 14:02:57 +02:00
|
|
|
thd->lex->select_lex.no_wrap_view_item= 0;
|
2004-07-23 08:20:58 +02:00
|
|
|
if (res)
|
2000-07-31 21:29:14 +02:00
|
|
|
return -1;
|
2004-09-15 22:42:56 +02:00
|
|
|
if (table == 0)
|
|
|
|
{
|
|
|
|
/* it is join view => we need to find table for update */
|
|
|
|
List_iterator_fast<Item> it(fields);
|
|
|
|
Item *item;
|
|
|
|
TABLE_LIST *tbl= 0;
|
|
|
|
table_map map= 0;
|
2004-12-06 16:15:54 +01:00
|
|
|
|
|
|
|
while ((item= it++))
|
2004-09-15 22:42:56 +02:00
|
|
|
map|= item->used_tables();
|
|
|
|
if (table_list->check_single_table(&tbl, map) || tbl == 0)
|
|
|
|
{
|
|
|
|
my_error(ER_VIEW_MULTIUPDATE, MYF(0),
|
|
|
|
table_list->view_db.str, table_list->view_name.str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
table_list->table= table= tbl->table;
|
|
|
|
}
|
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)
|
2004-10-01 16:54:06 +02:00
|
|
|
table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
|
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
|
2000-07-31 21:29:14 +02: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 &&
|
|
|
|
check_view_insertability(table_list, thd->query_id)))
|
|
|
|
{
|
|
|
|
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias, "INSERT");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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).
|
|
|
|
*/
|
2003-04-02 10:59:21 +02:00
|
|
|
bool log_on= (thd->options & OPTION_BIN_LOG) || (!(thd->master_access & SUPER_ACL));
|
2004-05-05 07:59:17 +02:00
|
|
|
bool transactional_table, log_delayed;
|
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;
|
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)
|
|
|
|
{
|
|
|
|
if (find_locked_table(thd,
|
|
|
|
table_list->db ? table_list->db : thd->db,
|
|
|
|
table_list->real_name))
|
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
|
|
|
|
table_list->real_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
|
|
|
{
|
|
|
|
res= 0;
|
2004-07-16 00:15:55 +02:00
|
|
|
if (table_list->next_global) /* if sub select */
|
|
|
|
res= open_and_lock_tables(thd, table_list->next_global);
|
|
|
|
/*
|
|
|
|
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
|
|
|
}
|
2002-11-26 00:00:05 +01:00
|
|
|
else
|
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
|
|
|
|
2004-07-16 00:15:55 +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;
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
// is table which we are changing used somewhere in other parts of query
|
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;
|
|
|
|
}
|
2004-07-16 00:15:55 +02:00
|
|
|
if (setup_fields(thd, 0, table_list, *values, 0, 0, 0))
|
2000-07-31 21:29:14 +02:00
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
its.rewind ();
|
|
|
|
/*
|
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";
|
2002-12-02 20:38:00 +01:00
|
|
|
if (duplic != DUP_ERROR)
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
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.
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
if (lock_type != TL_WRITE_DELAYED)
|
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)));
|
|
|
|
|
2004-10-02 21:20:08 +02:00
|
|
|
if (fields.elements && check_that_all_fields_are_given_values(thd, table))
|
2004-09-28 19:08:00 +02:00
|
|
|
{
|
|
|
|
/* thd->net.report_error is now set, which will abort the next loop */
|
|
|
|
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)
|
|
|
|
{
|
2003-05-03 01:16:56 +02:00
|
|
|
restore_record(table,default_values); // Get empty record
|
2004-11-12 13:34:00 +01:00
|
|
|
if (fill_record(thd, fields, *values, 0))
|
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()
|
2003-05-03 01:16:56 +02:00
|
|
|
restore_record(table,default_values); // Get empty record
|
2000-10-03 13:18:03 +02:00
|
|
|
else
|
2003-05-03 01:16:56 +02:00
|
|
|
table->record[0][0]=table->default_values[0]; // Fix delete marker
|
2004-11-12 13:34:00 +01:00
|
|
|
if (fill_record(thd, table->field, *values, 0))
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
FIXME: Actually we should do this before
|
|
|
|
check_that_all_fields_are_given_values Or even go into write_record ?
|
|
|
|
*/
|
|
|
|
if (table->triggers)
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_INSERT,
|
|
|
|
TRG_ACTION_BEFORE);
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
If auto_increment values are used, save the first one
|
|
|
|
for LAST_INSERT_ID() and for the update log.
|
|
|
|
We can't use insert_id() as we don't want to touch the
|
|
|
|
last_insert_id_used flag.
|
|
|
|
*/
|
|
|
|
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++;
|
2004-09-07 14:29:46 +02:00
|
|
|
|
|
|
|
if (table->triggers)
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_INSERT, TRG_ACTION_AFTER);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
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
|
|
|
{
|
2004-04-07 16:04:28 +02:00
|
|
|
if (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
|
|
|
|
2003-05-26 18:10:43 +02: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_...
|
|
|
|
*/
|
2004-03-15 18:36:16 +01:00
|
|
|
if (info.copied || info.deleted || info.updated)
|
2003-12-21 01:07:45 +01:00
|
|
|
{
|
2003-05-26 18:10:43 +02:00
|
|
|
query_cache_invalidate3(thd, table_list, 1);
|
2003-12-21 01:07:45 +01:00
|
|
|
}
|
2003-05-26 18:10:43 +02:00
|
|
|
|
2002-11-07 03:02:37 +01:00
|
|
|
transactional_table= table->file->has_transactions();
|
2003-05-26 18:10:43 +02:00
|
|
|
|
2002-11-07 03:02:37 +01:00
|
|
|
log_delayed= (transactional_table || table->tmp_table);
|
2004-04-07 16:04:28 +02:00
|
|
|
if ((info.copied || info.deleted || info.updated) &&
|
2004-03-15 18:36:16 +01:00
|
|
|
(error <= 0 || !transactional_table))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2000-09-16 03:27:21 +02:00
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
2003-12-16 11:10:50 +01:00
|
|
|
if (error <= 0)
|
|
|
|
thd->clear_error();
|
2002-08-08 02:12:02 +02:00
|
|
|
Query_log_event qinfo(thd, thd->query, thd->query_length,
|
2004-12-03 12:13:51 +01:00
|
|
|
log_delayed, FALSE);
|
2002-11-07 03:02:37 +01:00
|
|
|
if (mysql_bin_log.write(&qinfo) && transactional_table)
|
2000-12-07 13:08:48 +01:00
|
|
|
error=1;
|
2000-09-16 03:27:21 +02:00
|
|
|
}
|
2002-11-07 03:02:37 +01:00
|
|
|
if (!log_delayed)
|
2000-11-24 00:51:18 +01:00
|
|
|
thd->options|=OPTION_STATUS_NO_TRANS_UPDATE;
|
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);
|
|
|
|
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
|
2002-12-02 20:38:00 +01:00
|
|
|
if (duplic != DUP_ERROR)
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
|
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
|
|
|
}
|
2003-05-05 20:54:37 +02:00
|
|
|
free_underlaid_joins(thd, &thd->lex->select_lex);
|
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
|
2003-05-05 20:54:37 +02:00
|
|
|
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()
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool check_view_insertability(TABLE_LIST *view, ulong query_id)
|
|
|
|
{
|
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;
|
|
|
|
ulong other_query_id= query_id - 1;
|
2004-09-03 20:43:04 +02:00
|
|
|
DBUG_ENTER("check_key_in_view");
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
DBUG_ASSERT(view->table != 0 && view->field_translation != 0);
|
|
|
|
|
|
|
|
view->contain_auto_increment= 0;
|
|
|
|
/* 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
|
|
|
{
|
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()))
|
2004-07-16 00:15:55 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
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-09-03 20:43:04 +02:00
|
|
|
field->field->query_id= other_query_id;
|
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
|
|
|
}
|
|
|
|
/* 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;
|
2004-07-16 00:15:55 +02:00
|
|
|
if (field->field->query_id == query_id)
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
field->field->query_id= query_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* VIEW contain all fields without default value */
|
2004-09-03 20:43:04 +02:00
|
|
|
for (; *field_ptr; field_ptr++)
|
2004-07-16 00:15:55 +02:00
|
|
|
{
|
|
|
|
Field *field= *field_ptr;
|
|
|
|
/* field have not default value */
|
|
|
|
if ((field->type() == FIELD_TYPE_BLOB) &&
|
|
|
|
(table->timestamp_field != field ||
|
|
|
|
field->unireg_check == Field::TIMESTAMP_UN_FIELD))
|
|
|
|
{
|
2004-09-03 20:43:04 +02:00
|
|
|
for (trans= trans_start; ; trans++)
|
2004-07-16 00:15:55 +02:00
|
|
|
{
|
2004-09-03 20:43:04 +02:00
|
|
|
if (trans == trans_end)
|
|
|
|
DBUG_RETURN(TRUE); // Field was not part of view
|
2004-09-14 18:28:29 +02:00
|
|
|
if (((Item_field *)trans->item)->field == *field_ptr)
|
2004-09-03 20:43:04 +02:00
|
|
|
break; // ok
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2004-09-14 18:28:29 +02:00
|
|
|
if (setup_tables(thd, table_list, where, &thd->lex->select_lex.leaf_tables,
|
2004-11-25 01:23:13 +01:00
|
|
|
FALSE, select_insert))
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(table_list->view &&
|
|
|
|
table_list->ancestor && table_list->ancestor->next_local);
|
|
|
|
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
|
|
|
}
|
|
|
|
DBUG_RETURN(insert_view_fields(&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
|
2004-12-30 23:44:00 +01:00
|
|
|
table Table to insert into (can be NULL if table should be taken from
|
|
|
|
table_list->table)
|
|
|
|
where Where clause (for insert ... select)
|
|
|
|
select_insert TRUE if INSERT ... SELECT statement
|
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
|
|
|
*/
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list, TABLE *table,
|
2004-12-22 12:54:39 +01:00
|
|
|
List<Item> &fields, List_item *values,
|
|
|
|
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
|
|
|
{
|
|
|
|
bool insert_into_view= (table_list->view != 0);
|
|
|
|
/* TODO: use this condition for 'WITH CHECK OPTION' */
|
2004-10-20 03:04:37 +02:00
|
|
|
bool res;
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2004-12-22 12:54:39 +01:00
|
|
|
if ((values && check_insert_fields(thd, table_list, fields, *values, 1,
|
|
|
|
!insert_into_view)) ||
|
|
|
|
(values && setup_fields(thd, 0, table_list, *values, 0, 0, 0)) ||
|
2004-04-10 00:14:32 +02:00
|
|
|
(duplic == DUP_UPDATE &&
|
2004-09-06 13:37:10 +02:00
|
|
|
((thd->lex->select_lex.no_wrap_view_item= 1,
|
2004-11-20 18:36:41 +01:00
|
|
|
(res= setup_fields(thd, 0, table_list, update_fields, 1, 0, 0)),
|
2004-09-06 13:37:10 +02:00
|
|
|
thd->lex->select_lex.no_wrap_view_item= 0,
|
|
|
|
res) ||
|
2004-11-20 18:36:41 +01:00
|
|
|
setup_fields(thd, 0, table_list, update_values, 1, 0, 0))))
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-07-16 00:15:55 +02:00
|
|
|
|
2004-11-25 01:23:13 +01:00
|
|
|
if (!table)
|
|
|
|
table= table_list->table;
|
|
|
|
|
2004-12-31 15:26:24 +01:00
|
|
|
if (!select_insert && unique_table(table_list, table_list->next_global))
|
2004-04-10 00:14:32 +02:00
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->real_name);
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
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-05-20 01:02:49 +02:00
|
|
|
thd->lex->select_lex.first_execution= 0;
|
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)
|
|
|
|
{
|
|
|
|
while (++keynr < table->keys)
|
|
|
|
if (table->key_info[keynr].flags & HA_NOSAME)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2002-07-25 00:00:56 +02:00
|
|
|
Write a record to table with optional deleting of conflicting records
|
2004-09-28 19:08:00 +02:00
|
|
|
|
|
|
|
Sets thd->no_trans_update if table which is updated didn't have transactions
|
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
|
|
|
{
|
|
|
|
int error;
|
|
|
|
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;
|
2004-09-15 21:10:31 +02:00
|
|
|
table->file->restore_auto_increment();
|
2000-07-31 21:29:14 +02:00
|
|
|
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 &&
|
|
|
|
key_nr == table->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)
|
|
|
|
{
|
|
|
|
if (!(key=(char*) my_safe_alloca(table->max_unique_length,
|
|
|
|
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;
|
2002-12-02 20:38:00 +01: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
|
|
|
|
*/
|
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);
|
2004-11-12 13:34:00 +01:00
|
|
|
if (fill_record(thd, *info->update_fields, *info->update_values, 0))
|
2002-12-02 20:38:00 +01:00
|
|
|
goto 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)
|
|
|
|
break;
|
2004-12-30 23:44:00 +01:00
|
|
|
if (res == VIEW_CHECK_ERROR)
|
2004-09-29 15:35:01 +02:00
|
|
|
goto 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])))
|
|
|
|
goto err;
|
2004-03-15 18:36:16 +01:00
|
|
|
info->updated++;
|
2002-12-02 20:38:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
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.
|
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 ||
|
|
|
|
table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
|
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++;
|
2003-05-21 20:39:58 +02:00
|
|
|
break; /* Update logfile and count */
|
2002-12-02 20:38:00 +01:00
|
|
|
}
|
|
|
|
else if ((error=table->file->delete_row(table->record[1])))
|
|
|
|
goto err;
|
|
|
|
info->deleted++;
|
2004-09-28 19:08:00 +02:00
|
|
|
if (!table->file->has_transactions())
|
|
|
|
thd->no_trans_update= 1;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
info->copied++;
|
|
|
|
}
|
|
|
|
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();
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
info->copied++;
|
|
|
|
if (key)
|
|
|
|
my_safe_afree(key,table->max_unique_length,MAX_KEY_LENGTH);
|
2004-09-28 19:08:00 +02:00
|
|
|
if (!table->file->has_transactions())
|
|
|
|
thd->no_trans_update= 1;
|
2004-04-28 02:37:45 +02:00
|
|
|
DBUG_RETURN(0);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
err:
|
|
|
|
if (key)
|
|
|
|
my_afree(key);
|
2002-06-11 10:20:31 +02:00
|
|
|
info->last_errno= error;
|
2000-07-31 21:29:14 +02:00
|
|
|
table->file->print_error(error,MYF(0));
|
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
|
|
|
******************************************************************************/
|
|
|
|
|
2004-09-28 19:08:00 +02:00
|
|
|
int check_that_all_fields_are_given_values(THD *thd, TABLE *entry)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-10-02 21:20:08 +02:00
|
|
|
if (!thd->abort_on_warning) // No check if not strict mode
|
2004-09-28 19:08:00 +02:00
|
|
|
return 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 &&
|
|
|
|
((*field)->flags & NO_DEFAULT_VALUE_FLAG))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), (*field)->field_name);
|
2000-07-31 21:29:14 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
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;
|
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;
|
|
|
|
uint 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)
|
|
|
|
{
|
2001-03-09 02:38:18 +01:00
|
|
|
thd.user=thd.priv_user=(char*) delayed_user;
|
2003-09-26 12:33:13 +02:00
|
|
|
thd.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;
|
2003-09-24 15:26:20 +02:00
|
|
|
thd.lex->current_select= 0; /* for my_message_sql */
|
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;
|
2003-02-27 16:35:51 +01:00
|
|
|
thd.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);
|
|
|
|
thd.user=thd.host=0;
|
|
|
|
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) &&
|
|
|
|
!strcmp(table_list->real_name,tmp->table->real_name))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
DBUG_ENTER("delayed_get_table");
|
|
|
|
|
|
|
|
if (!table_list->db)
|
|
|
|
table_list->db=thd->db;
|
|
|
|
|
|
|
|
/* no match; create a new thread to handle the table */
|
|
|
|
if (!(tmp=find_handler(thd,table_list)))
|
|
|
|
{
|
2003-04-26 19:43:28 +02:00
|
|
|
/* Don't create 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);
|
|
|
|
if (!(tmp=find_handler(thd,table_list))) // Was just created
|
|
|
|
{
|
|
|
|
if (!(tmp=new delayed_insert()))
|
|
|
|
{
|
2003-01-30 21:15:44 +01:00
|
|
|
thd->fatal_error();
|
2000-07-31 21:29:14 +02:00
|
|
|
my_error(ER_OUTOFMEMORY,MYF(0),sizeof(delayed_insert));
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_create);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
2000-10-23 14:35:42 +02:00
|
|
|
pthread_mutex_lock(&LOCK_thread_count);
|
|
|
|
thread_count++;
|
|
|
|
pthread_mutex_unlock(&LOCK_thread_count);
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!(tmp->thd.db=my_strdup(table_list->db,MYF(MY_WME))) ||
|
2000-11-13 22:55:10 +01:00
|
|
|
!(tmp->thd.query=my_strdup(table_list->real_name,MYF(MY_WME))))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
delete tmp;
|
2003-01-30 21:15:44 +01:00
|
|
|
thd->fatal_error();
|
2004-11-12 13:34:00 +01:00
|
|
|
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_unlock(&LOCK_delayed_create);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
2001-09-01 09:38:16 +02:00
|
|
|
tmp->table_list= *table_list; // Needed to open table
|
|
|
|
tmp->table_list.db= tmp->thd.db;
|
2002-09-20 13:05:18 +02:00
|
|
|
tmp->table_list.alias= tmp->table_list.real_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;
|
2003-01-30 21:15:44 +01:00
|
|
|
thd->fatal_error();
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_unlock(&LOCK_delayed_create);
|
2004-10-20 03:04:37 +02:00
|
|
|
my_error(ER_CANT_CREATE_THREAD, MYF(0), error);
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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();
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_create);
|
|
|
|
DBUG_RETURN(0); // Continue with normal insert
|
|
|
|
}
|
|
|
|
if (thd->killed)
|
|
|
|
{
|
|
|
|
tmp->unlock();
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_create);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&LOCK_delayed_create);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&tmp->mutex);
|
|
|
|
TABLE *table=tmp->get_local_table(thd);
|
|
|
|
pthread_mutex_unlock(&tmp->mutex);
|
|
|
|
tmp->unlock();
|
|
|
|
if (table)
|
|
|
|
thd->di=tmp;
|
2003-01-30 21:15:44 +01:00
|
|
|
else if (tmp->thd.is_fatal_error)
|
|
|
|
thd->fatal_error();
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_RETURN((table_list->table=table));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client_thd->proc_info="allocating local table";
|
2001-12-05 12:03:00 +01:00
|
|
|
copy= (TABLE*) client_thd->alloc(sizeof(*copy)+
|
|
|
|
(table->fields+1)*sizeof(Field**)+
|
|
|
|
table->reclength);
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!copy)
|
|
|
|
goto error;
|
|
|
|
*copy= *table;
|
|
|
|
bzero((char*) ©->name_hash,sizeof(copy->name_hash)); // No name hashing
|
|
|
|
/* We don't need to change the file handler here */
|
|
|
|
|
|
|
|
field=copy->field=(Field**) (copy+1);
|
|
|
|
copy->record[0]=(byte*) (field+table->fields+1);
|
|
|
|
memcpy((char*) copy->record[0],(char*) table->record[0],table->reclength);
|
|
|
|
|
|
|
|
/* Make a copy of all fields */
|
|
|
|
|
|
|
|
adjust_ptrs=PTR_BYTE_DIFF(copy->record[0],table->record[0]);
|
|
|
|
|
2001-03-08 20:49:15 +01:00
|
|
|
found_next_number_field=table->found_next_number_field;
|
2000-07-31 21:29:14 +02:00
|
|
|
for (org_field=table->field ; *org_field ; org_field++,field++)
|
|
|
|
{
|
2004-11-08 00:13:54 +01:00
|
|
|
if (!(*field= (*org_field)->new_field(client_thd->mem_root,copy)))
|
2000-07-31 21:29:14 +02:00
|
|
|
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=
|
|
|
|
(Field_timestamp*) copy->field[table->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;
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
return copy;
|
|
|
|
|
|
|
|
/* Got fatal error */
|
|
|
|
error:
|
|
|
|
tables_in_use--;
|
|
|
|
status=1;
|
|
|
|
pthread_cond_signal(&cond); // Inform thread about abort
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
if (!(row->record= (char*) my_malloc(table->reclength+query_length+1,
|
|
|
|
MYF(MY_WME))))
|
|
|
|
goto err;
|
|
|
|
memcpy(row->record,table->record[0],table->reclength);
|
|
|
|
if (query_length)
|
|
|
|
{
|
|
|
|
row->query=row->record+table->reclength;
|
|
|
|
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
|
|
|
|
|
|
|
di->rows.push_back(row);
|
|
|
|
di->stacked_inserts++;
|
|
|
|
di->status=1;
|
|
|
|
if (table->blob_fields)
|
|
|
|
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++))
|
|
|
|
{
|
2001-10-02 20:08:08 +02:00
|
|
|
/* Ensure that the thread doesn't kill itself while we are looking at it */
|
2000-07-31 21:29:14 +02:00
|
|
|
pthread_mutex_lock(&tmp->mutex);
|
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);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&tmp->mutex);
|
|
|
|
}
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_delayed_insert)); // For unlink from list
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new delayed insert thread
|
|
|
|
*/
|
|
|
|
|
2002-11-07 11:49:02 +01:00
|
|
|
extern "C" pthread_handler_decl(handle_delayed_insert,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);
|
|
|
|
|
|
|
|
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");
|
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();
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_ILLEGAL_HA, MYF(0), di->table_list.real_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;
|
|
|
|
if (error == ETIME || error == ETIMEDOUT)
|
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
/* request for new delayed insert */
|
|
|
|
if (!(thd->lock=mysql_lock_tables(thd,&di->table,1)))
|
|
|
|
{
|
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;
|
|
|
|
uint max_rows;
|
2001-11-27 01:50:20 +01:00
|
|
|
bool using_ignore=0, 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 */
|
|
|
|
sql_print_error(ER(ER_DELAYED_CANT_CHANGE_LOCK),table->real_name);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
thd.proc_info="insert";
|
|
|
|
max_rows=delayed_insert_limit;
|
|
|
|
if (thd.killed || table->version != refresh_version)
|
|
|
|
{
|
2003-03-31 10:39:46 +02:00
|
|
|
thd.killed= THD::KILL_CONNECTION;
|
2000-07-31 21:29:14 +02:00
|
|
|
max_rows= ~0; // Do as much as possible
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
while ((row=rows.get()))
|
|
|
|
{
|
|
|
|
stacked_inserts--;
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
memcpy(table->record[0],row->record,table->reclength);
|
|
|
|
|
|
|
|
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
|
|
|
|
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 ||
|
2000-12-24 14:19:00 +01:00
|
|
|
info.handle_duplicates == DUP_REPLACE)
|
|
|
|
{
|
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
|
|
|
using_ignore=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;
|
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);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
if (table->blob_fields)
|
|
|
|
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);
|
|
|
|
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 */
|
|
|
|
sql_print_error(ER(ER_DELAYED_CANT_CHANGE_LOCK),table->real_name);
|
|
|
|
}
|
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);
|
|
|
|
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:
|
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--;
|
|
|
|
}
|
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;
|
2004-12-30 23:44:00 +01:00
|
|
|
TABLE_LIST *first_select_table=
|
|
|
|
(TABLE_LIST*) lex->select_lex.table_list.first;
|
|
|
|
TABLE_LIST *first_select_leaf_table;
|
2004-09-15 22:42:56 +02:00
|
|
|
int res;
|
2004-07-16 00:15:55 +02:00
|
|
|
DBUG_ENTER("mysql_insert_select_prepare");
|
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
|
|
|
*/
|
2004-09-06 07:27:18 +02:00
|
|
|
lex->query_tables->no_where_clause= 1;
|
2004-12-30 23:44:00 +01:00
|
|
|
if (mysql_prepare_insert(thd, lex->query_tables,
|
|
|
|
lex->query_tables->table, lex->field_list, 0,
|
|
|
|
lex->update_list, lex->value_list,
|
|
|
|
lex->duplicates,
|
|
|
|
&lex->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
|
|
|
|
*/
|
2004-09-14 18:28:29 +02:00
|
|
|
DBUG_ASSERT(lex->select_lex.leaf_tables);
|
|
|
|
lex->leaf_tables_insert= lex->select_lex.leaf_tables;
|
2004-09-15 22:42:56 +02:00
|
|
|
/* skip all leaf tables belonged to view where we are insert */
|
|
|
|
for (first_select_leaf_table= lex->select_lex.leaf_tables->next_leaf;
|
|
|
|
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)
|
|
|
|
{}
|
|
|
|
lex->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
|
|
|
{
|
|
|
|
DBUG_ENTER("select_insert::prepare");
|
|
|
|
|
2002-05-08 22:14:40 +02:00
|
|
|
unit= u;
|
2004-07-16 00:15:55 +02:00
|
|
|
if (check_insert_fields(thd, table_list, *fields, values, 1,
|
|
|
|
!insert_into_view))
|
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
|
|
|
|
*/
|
|
|
|
if (!(thd->lex->current_select->options & OPTION_BUFFER_RESULT) &&
|
2004-11-21 18:33:49 +01:00
|
|
|
unique_table(table_list, table_list->next_global))
|
2004-09-15 22:42:56 +02:00
|
|
|
{
|
|
|
|
/* Using same table for INSERT and SELECT */
|
|
|
|
thd->lex->current_select->options|= OPTION_BUFFER_RESULT;
|
|
|
|
thd->lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2003-05-03 01:16:56 +02:00
|
|
|
restore_record(table,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-01-04 12:46:53 +01:00
|
|
|
if (info.ignore || info.handle_duplicates == DUP_REPLACE)
|
2001-08-02 05:29:50 +02:00
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
2004-04-06 21:35:26 +02:00
|
|
|
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)));
|
2004-10-02 21:20:08 +02:00
|
|
|
DBUG_RETURN(fields->elements &&
|
|
|
|
check_that_all_fields_are_given_values(thd, table));
|
2000-07-31 21:29:14 +02: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
|
|
|
}
|
2004-12-06 16:15:54 +01:00
|
|
|
if (!(error= write_record(thd, table,&info)) && table->next_number_field)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-12-06 10:38:56 +01:00
|
|
|
/* Clear for next record */
|
2000-07-31 21:29:14 +02:00
|
|
|
table->next_number_field->reset();
|
|
|
|
if (! last_insert_id && thd->insert_id_used)
|
|
|
|
last_insert_id=thd->insert_id();
|
|
|
|
}
|
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)
|
2004-12-06 16:15:54 +01:00
|
|
|
fill_record(thd, *fields, values, 1);
|
2004-12-03 15:02:29 +01:00
|
|
|
else
|
2004-12-06 16:15:54 +01:00
|
|
|
fill_record(thd, table->field, values, 1);
|
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;
|
|
|
|
}
|
2004-04-06 21:35:26 +02:00
|
|
|
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);
|
|
|
|
}
|
2 minor edits, plus
fix for BUG#1113 "INSERT into non-trans table SELECT ; ROLLBACK" does not send warning"
and
fix for BUG#873 "In transaction, INSERT to non-trans table is written too early to binlog".
Now we don't always write the non-trans update immediately to the binlog;
if there is something in the binlog cache we write it to the binlog cache
(because the non-trans update could depend on a trans table which was modified
earlier in the transaction); then in case of ROLLBACK, we write the binlog
cache to the binlog, wrapped with BEGIN/ROLLBACK.
This guarantees that the slave does the same updates.
For ROLLBACK TO SAVEPOINT: when we execute a SAVEPOINT command we write it
to the binlog cache. At ROLLBACK TO SAVEPOINT, if some non-trans table was updated,
we write ROLLBACK TO SAVEPOINT to the binlog cache; when the transaction
terminates (COMMIT/ROLLBACK), the binlog cache will be flushed to the binlog
(because of the non-trans update) so we'll have SAVEPOINT and ROLLBACK TO
SAVEPOINT in the binlog.
Apart from this rare case of updates of mixed table types in transaction, the
usual way is still clear the binlog cache at ROLLBACK, or chop it at
ROLLBACK TO SAVEPOINT (meaning the SAVEPOINT command is also chopped, which
is fine).
Note that BUG#873 encompasses subbugs 1) and 2) of BUG#333 "3 binlogging bugs when doing INSERT with mixed InnoDB/MyISAM".
2003-08-22 15:39:24 +02:00
|
|
|
if (!table->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");
|
|
|
|
|
2004-04-07 16:04:28 +02:00
|
|
|
error=table->file->end_bulk_insert();
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
|
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
|
|
|
|
and ha_autocommit_...
|
|
|
|
*/
|
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);
|
2003-08-29 12:44:35 +02:00
|
|
|
if (!(table->file->has_transactions() || table->tmp_table))
|
|
|
|
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
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
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,
|
2003-12-30 12:14:21 +01:00
|
|
|
extra_fields, keys, &values, &lock);
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!table)
|
|
|
|
DBUG_RETURN(-1); // abort() deletes table
|
|
|
|
|
2003-10-06 20:02:27 +02:00
|
|
|
if (table->fields < values.elements)
|
|
|
|
{
|
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 */
|
|
|
|
field=table->field+table->fields - values.elements;
|
|
|
|
|
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;
|
|
|
|
|
2003-05-03 01:16:56 +02:00
|
|
|
restore_record(table,default_values); // Get empty record
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->cuted_fields=0;
|
2005-01-04 12:46:53 +01:00
|
|
|
if (info.ignore || info.handle_duplicates == DUP_REPLACE)
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
2004-04-06 21:35:26 +02:00
|
|
|
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)));
|
|
|
|
DBUG_RETURN(check_that_all_fields_are_given_values(thd, table));
|
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
|
|
|
{
|
2004-11-12 13:34:00 +01:00
|
|
|
fill_record(thd, field, values, 1);
|
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);
|
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.
|
|
|
|
*/
|
2000-11-13 22:55:10 +01:00
|
|
|
if (!table->tmp_table)
|
2004-10-26 18:30:01 +02:00
|
|
|
{
|
2004-10-27 20:56:17 +02:00
|
|
|
ulong version= table->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)
|
2004-10-26 18:30:01 +02:00
|
|
|
VOID(pthread_cond_broadcast(&COND_refresh));
|
|
|
|
}
|
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);
|
2000-07-31 21:29:14 +02:00
|
|
|
enum db_type table_type=table->db_type;
|
2000-11-13 22:55:10 +01:00
|
|
|
if (!table->tmp_table)
|
2004-07-18 14:34:35 +02:00
|
|
|
{
|
2004-12-03 15:02:29 +01:00
|
|
|
ulong version= table->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)
|
2004-07-20 15:34:57 +02:00
|
|
|
quick_rm_table(table_type, create_table->db, create_table->real_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)
|
2004-10-26 18:30:01 +02:00
|
|
|
VOID(pthread_cond_broadcast(&COND_refresh));
|
2004-07-18 14:34:35 +02:00
|
|
|
}
|
|
|
|
else if (!create_info->table_existed)
|
2004-07-20 15:34:57 +02:00
|
|
|
close_temporary_table(thd, create_table->db, create_table->real_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
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
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 */
|
|
|
|
#endif /* __GNUC__ */
|