~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/* Insert of records */
18
19
/*
20
  INSERT DELAYED
21
22
  Drizzle has a different form of DELAYED then MySQL. DELAYED is just
23
  a hint to the the sorage engine (which can then do whatever it likes.
24
*/
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
25
#include <drizzled/server_includes.h>
26
#include <drizzled/sql_select.h>
27
#include <drizzled/sql_show.h>
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
28
#include <drizzled/rpl_mi.h>
549 by Monty Taylor
Took gettext.h out of header files.
29
#include <drizzled/error.h>
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
30
#include <drizzled/slave.h>
520.8.2 by Monty Taylor
Moved sql_parse.h and sql_error.h out of common_includes.
31
#include <drizzled/sql_parse.h>
520.7.1 by Monty Taylor
Moved hash.c to drizzled.
32
#include <drizzled/probes.h>
575.1.3 by Monty Taylor
Moved some stuff out of handler.h.
33
#include <drizzled/tableop_hooks.h>
1 by brian
clean slate
34
35
/* Define to force use of my_malloc() if the allocated memory block is big */
36
37
#ifndef HAVE_ALLOCA
38
#define my_safe_alloca(size, min_length) my_alloca(size)
39
#define my_safe_afree(ptr, size, min_length) my_afree(ptr)
40
#else
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
41
#define my_safe_alloca(size, min_length) ((size <= min_length) ? my_alloca(size) : malloc(size))
42
#define my_safe_afree(ptr, size, min_length) if (size > min_length) free(ptr)
1 by brian
clean slate
43
#endif
44
45
46
47
/*
48
  Check if insert fields are correct.
49
50
  SYNOPSIS
51
    check_insert_fields()
520.1.22 by Brian Aker
Second pass of thd cleanup
52
    session                         The current thread.
1 by brian
clean slate
53
    table                       The table for insert.
54
    fields                      The insert fields.
55
    values                      The insert values.
56
    check_unique                If duplicate values should be rejected.
57
58
  NOTE
59
    Clears TIMESTAMP_AUTO_SET_ON_INSERT from table->timestamp_field_type
60
    or leaves it as is, depending on if timestamp should be updated or
61
    not.
62
63
  RETURN
64
    0           OK
65
    -1          Error
66
*/
67
520.1.22 by Brian Aker
Second pass of thd cleanup
68
static int check_insert_fields(Session *session, TableList *table_list,
1 by brian
clean slate
69
                               List<Item> &fields, List<Item> &values,
77.1.45 by Monty Taylor
Warning fixes.
70
                               bool check_unique,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
71
                               table_map *map __attribute__((unused)))
1 by brian
clean slate
72
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
73
  Table *table= table_list->table;
1 by brian
clean slate
74
75
  if (fields.elements == 0 && values.elements != 0)
76
  {
77
    if (values.elements != table->s->fields)
78
    {
79
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
80
      return -1;
81
    }
82
    clear_timestamp_auto_bits(table->timestamp_field_type,
83
                              TIMESTAMP_AUTO_SET_ON_INSERT);
84
    /*
85
      No fields are provided so all fields must be provided in the values.
86
      Thus we set all bits in the write set.
87
    */
88
    bitmap_set_all(table->write_set);
89
  }
90
  else
91
  {						// Part field list
520.1.22 by Brian Aker
Second pass of thd cleanup
92
    SELECT_LEX *select_lex= &session->lex->select_lex;
1 by brian
clean slate
93
    Name_resolution_context *context= &select_lex->context;
94
    Name_resolution_context_state ctx_state;
95
    int res;
96
97
    if (fields.elements != values.elements)
98
    {
99
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
100
      return -1;
101
    }
102
520.1.22 by Brian Aker
Second pass of thd cleanup
103
    session->dup_field= 0;
1 by brian
clean slate
104
105
    /* Save the state of the current name resolution context. */
106
    ctx_state.save_state(context, table_list);
107
108
    /*
109
      Perform name resolution only in the first table - 'table_list',
110
      which is the table that is inserted into.
111
    */
112
    table_list->next_local= 0;
113
    context->resolve_in_table_list_only(table_list);
520.1.22 by Brian Aker
Second pass of thd cleanup
114
    res= setup_fields(session, 0, fields, MARK_COLUMNS_WRITE, 0, 0);
1 by brian
clean slate
115
116
    /* Restore the current context. */
117
    ctx_state.restore_state(context, table_list);
118
119
    if (res)
120
      return -1;
121
520.1.22 by Brian Aker
Second pass of thd cleanup
122
    if (check_unique && session->dup_field)
1 by brian
clean slate
123
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
124
      my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), session->dup_field->field_name);
1 by brian
clean slate
125
      return -1;
126
    }
127
    if (table->timestamp_field)	// Don't automaticly set timestamp if used
128
    {
129
      if (bitmap_is_set(table->write_set,
130
                        table->timestamp_field->field_index))
131
        clear_timestamp_auto_bits(table->timestamp_field_type,
132
                                  TIMESTAMP_AUTO_SET_ON_INSERT);
133
      else
134
      {
135
        bitmap_set_bit(table->write_set,
136
                       table->timestamp_field->field_index);
137
      }
138
    }
383.7.1 by Andrey Zhakov
Initial submit of code and tests
139
    /* Mark all virtual columns for write*/
140
    if (table->vfield)
141
      table->mark_virtual_columns();
1 by brian
clean slate
142
  }
143
144
  return 0;
145
}
146
147
148
/*
149
  Check update fields for the timestamp field.
150
151
  SYNOPSIS
152
    check_update_fields()
520.1.22 by Brian Aker
Second pass of thd cleanup
153
    session                         The current thread.
1 by brian
clean slate
154
    insert_table_list           The insert table list.
155
    table                       The table for update.
156
    update_fields               The update fields.
157
158
  NOTE
159
    If the update fields include the timestamp field,
160
    remove TIMESTAMP_AUTO_SET_ON_UPDATE from table->timestamp_field_type.
161
162
  RETURN
163
    0           OK
164
    -1          Error
165
*/
166
520.1.22 by Brian Aker
Second pass of thd cleanup
167
static int check_update_fields(Session *session, TableList *insert_table_list,
77.1.45 by Monty Taylor
Warning fixes.
168
                               List<Item> &update_fields,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
169
                               table_map *map __attribute__((unused)))
1 by brian
clean slate
170
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
171
  Table *table= insert_table_list->table;
199 by Brian Aker
my_bool...
172
  bool timestamp_mark= false;
1 by brian
clean slate
173
174
  if (table->timestamp_field)
175
  {
176
    /*
177
      Unmark the timestamp field so that we can check if this is modified
178
      by update_fields
179
    */
180
    timestamp_mark= bitmap_test_and_clear(table->write_set,
181
                                          table->timestamp_field->field_index);
182
  }
183
184
  /* Check the fields we are going to modify */
520.1.22 by Brian Aker
Second pass of thd cleanup
185
  if (setup_fields(session, 0, update_fields, MARK_COLUMNS_WRITE, 0, 0))
1 by brian
clean slate
186
    return -1;
187
188
  if (table->timestamp_field)
189
  {
190
    /* Don't set timestamp column if this is modified. */
191
    if (bitmap_is_set(table->write_set,
192
                      table->timestamp_field->field_index))
193
      clear_timestamp_auto_bits(table->timestamp_field_type,
194
                                TIMESTAMP_AUTO_SET_ON_UPDATE);
195
    if (timestamp_mark)
196
      bitmap_set_bit(table->write_set,
197
                     table->timestamp_field->field_index);
198
  }
199
  return 0;
200
}
201
202
203
/**
204
  Upgrade table-level lock of INSERT statement to TL_WRITE if
205
  a more concurrent lock is infeasible for some reason. This is
206
  necessary for engines without internal locking support (MyISAM).
207
  An engine with internal locking implementation might later
208
  downgrade the lock in handler::store_lock() method.
209
*/
210
211
static
520.1.22 by Brian Aker
Second pass of thd cleanup
212
void upgrade_lock_type(Session *session __attribute__((unused)),
77.1.45 by Monty Taylor
Warning fixes.
213
                       thr_lock_type *lock_type,
1 by brian
clean slate
214
                       enum_duplicates duplic,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
215
                       bool is_multi_insert __attribute__((unused)))
1 by brian
clean slate
216
{
217
  if (duplic == DUP_UPDATE ||
218
      (duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT))
219
  {
220
    *lock_type= TL_WRITE_DEFAULT;
221
    return;
222
  }
223
}
224
225
226
/**
227
  INSERT statement implementation
228
229
  @note Like implementations of other DDL/DML in MySQL, this function
230
  relies on the caller to close the thread tables. This is done in the
231
  end of dispatch_command().
232
*/
233
520.1.22 by Brian Aker
Second pass of thd cleanup
234
bool mysql_insert(Session *session,TableList *table_list,
1 by brian
clean slate
235
                  List<Item> &fields,
236
                  List<List_item> &values_list,
237
                  List<Item> &update_fields,
238
                  List<Item> &update_values,
239
                  enum_duplicates duplic,
240
		  bool ignore)
241
{
242
  int error;
163 by Brian Aker
Merge Monty's code.
243
  bool transactional_table, joins_freed= false;
1 by brian
clean slate
244
  bool changed;
245
  bool was_insert_delayed= (table_list->lock_type ==  TL_WRITE_DELAYED);
482 by Brian Aker
Remove uint.
246
  uint32_t value_count;
1 by brian
clean slate
247
  ulong counter = 1;
151 by Brian Aker
Ulonglong to uint64_t
248
  uint64_t id;
1 by brian
clean slate
249
  COPY_INFO info;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
250
  Table *table= 0;
1 by brian
clean slate
251
  List_iterator_fast<List_item> its(values_list);
252
  List_item *values;
253
  Name_resolution_context *context;
254
  Name_resolution_context_state ctx_state;
255
  thr_lock_type lock_type;
256
  Item *unused_conds= 0;
51.2.2 by Patrick Galbraith
Removed DBUGs from
257
  
1 by brian
clean slate
258
259
  /*
260
    Upgrade lock type if the requested lock is incompatible with
261
    the current connection mode or table operation.
262
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
263
  upgrade_lock_type(session, &table_list->lock_type, duplic,
1 by brian
clean slate
264
                    values_list.elements > 1);
265
266
  /*
267
    We can't write-delayed into a table locked with LOCK TABLES:
268
    this will lead to a deadlock, since the delayed thread will
269
    never be able to get a lock on the table. QQQ: why not
270
    upgrade the lock here instead?
271
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
272
  if (table_list->lock_type == TL_WRITE_DELAYED && session->locked_tables &&
273
      find_locked_table(session, table_list->db, table_list->table_name))
1 by brian
clean slate
274
  {
275
    my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
276
             table_list->table_name);
163 by Brian Aker
Merge Monty's code.
277
    return(true);
1 by brian
clean slate
278
  }
279
280
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
281
    if (open_and_lock_tables(session, table_list))
163 by Brian Aker
Merge Monty's code.
282
      return(true);
1 by brian
clean slate
283
  }
284
  lock_type= table_list->lock_type;
285
520.1.22 by Brian Aker
Second pass of thd cleanup
286
  session->set_proc_info("init");
287
  session->used_tables=0;
1 by brian
clean slate
288
  values= its++;
289
  value_count= values->elements;
290
520.1.22 by Brian Aker
Second pass of thd cleanup
291
  if (mysql_prepare_insert(session, table_list, table, fields, values,
1 by brian
clean slate
292
			   update_fields, update_values, duplic, &unused_conds,
163 by Brian Aker
Merge Monty's code.
293
                           false,
1 by brian
clean slate
294
                           (fields.elements || !value_count ||
295
                            (0) != 0), !ignore))
296
    goto abort;
297
298
  /* mysql_prepare_insert set table_list->table if it was not set */
299
  table= table_list->table;
300
520.1.22 by Brian Aker
Second pass of thd cleanup
301
  context= &session->lex->select_lex.context;
1 by brian
clean slate
302
  /*
303
    These three asserts test the hypothesis that the resetting of the name
304
    resolution context below is not necessary at all since the list of local
305
    tables for INSERT always consists of one table.
306
  */
51.2.2 by Patrick Galbraith
Removed DBUGs from
307
  assert(!table_list->next_local);
308
  assert(!context->table_list->next_local);
309
  assert(!context->first_name_resolution_table->next_name_resolution_table);
1 by brian
clean slate
310
311
  /* Save the state of the current name resolution context. */
312
  ctx_state.save_state(context, table_list);
313
314
  /*
315
    Perform name resolution only in the first table - 'table_list',
316
    which is the table that is inserted into.
317
  */
318
  table_list->next_local= 0;
319
  context->resolve_in_table_list_only(table_list);
320
321
  while ((values= its++))
322
  {
323
    counter++;
324
    if (values->elements != value_count)
325
    {
326
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
327
      goto abort;
328
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
329
    if (setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0))
1 by brian
clean slate
330
      goto abort;
331
  }
332
  its.rewind ();
333
 
334
  /* Restore the current context. */
335
  ctx_state.restore_state(context, table_list);
336
337
  /*
338
    Fill in the given fields and dump it to the table file
339
  */
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
340
  memset(&info, 0, sizeof(info));
1 by brian
clean slate
341
  info.ignore= ignore;
342
  info.handle_duplicates=duplic;
343
  info.update_fields= &update_fields;
344
  info.update_values= &update_values;
345
346
  /*
347
    Count warnings for all inserts.
348
    For single line insert, generate an error if try to set a NOT NULL field
349
    to NULL.
350
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
351
  session->count_cuted_fields= ((values_list.elements == 1 &&
1 by brian
clean slate
352
                             !ignore) ?
353
			    CHECK_FIELD_ERROR_FOR_NULL :
354
			    CHECK_FIELD_WARN);
520.1.22 by Brian Aker
Second pass of thd cleanup
355
  session->cuted_fields = 0L;
1 by brian
clean slate
356
  table->next_number_field=table->found_next_number_field;
357
520.1.22 by Brian Aker
Second pass of thd cleanup
358
  if (session->slave_thread &&
1 by brian
clean slate
359
      (info.handle_duplicates == DUP_UPDATE) &&
360
      (table->next_number_field != NULL) &&
361
      rpl_master_has_bug(&active_mi->rli, 24432))
362
    goto abort;
363
364
  error=0;
520.1.22 by Brian Aker
Second pass of thd cleanup
365
  session->set_proc_info("update");
1 by brian
clean slate
366
  if (duplic == DUP_REPLACE)
367
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
368
  if (duplic == DUP_UPDATE)
369
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
370
  {
371
    if (duplic != DUP_ERROR || ignore)
372
      table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
373
    table->file->ha_start_bulk_insert(values_list.elements);
374
  }
375
376
520.1.22 by Brian Aker
Second pass of thd cleanup
377
  session->abort_on_warning= !ignore;
1 by brian
clean slate
378
379
  table->mark_columns_needed_for_insert();
380
381
  while ((values= its++))
382
  {
383
    if (fields.elements || !value_count)
384
    {
385
      restore_record(table,s->default_values);	// Get empty record
520.1.22 by Brian Aker
Second pass of thd cleanup
386
      if (fill_record(session, fields, *values, 0))
1 by brian
clean slate
387
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
388
	if (values_list.elements != 1 && ! session->is_error())
1 by brian
clean slate
389
	{
390
	  info.records++;
391
	  continue;
392
	}
393
	/*
520.1.22 by Brian Aker
Second pass of thd cleanup
394
	  TODO: set session->abort_on_warning if values_list.elements == 1
1 by brian
clean slate
395
	  and check that all items return warning in case of problem with
396
	  storing field.
397
        */
398
	error=1;
399
	break;
400
      }
401
    }
402
    else
403
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
404
      if (session->used_tables)			// Column used in values()
1 by brian
clean slate
405
	restore_record(table,s->default_values);	// Get empty record
406
      else
407
      {
408
        /*
409
          Fix delete marker. No need to restore rest of record since it will
410
          be overwritten by fill_record() anyway (and fill_record() does not
411
          use default values in this case).
412
        */
413
	table->record[0][0]= table->s->default_values[0];
414
      }
520.1.22 by Brian Aker
Second pass of thd cleanup
415
      if (fill_record(session, table->field, *values, 0))
1 by brian
clean slate
416
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
417
	if (values_list.elements != 1 && ! session->is_error())
1 by brian
clean slate
418
	{
419
	  info.records++;
420
	  continue;
421
	}
422
	error=1;
423
	break;
424
      }
425
    }
426
520.1.22 by Brian Aker
Second pass of thd cleanup
427
    error=write_record(session, table ,&info);
1 by brian
clean slate
428
    if (error)
429
      break;
520.1.22 by Brian Aker
Second pass of thd cleanup
430
    session->row_count++;
1 by brian
clean slate
431
  }
432
520.1.22 by Brian Aker
Second pass of thd cleanup
433
  free_underlaid_joins(session, &session->lex->select_lex);
163 by Brian Aker
Merge Monty's code.
434
  joins_freed= true;
1 by brian
clean slate
435
436
  /*
437
    Now all rows are inserted.  Time to update logs and sends response to
438
    user
439
  */
440
  {
441
    /*
442
      Do not do this release if this is a delayed insert, it would steal
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
443
      auto_inc values from the delayed_insert thread as they share Table.
1 by brian
clean slate
444
    */
445
    table->file->ha_release_auto_increment();
446
    if (table->file->ha_end_bulk_insert() && !error)
447
    {
448
      table->file->print_error(my_errno,MYF(0));
449
      error=1;
450
    }
451
    if (duplic != DUP_ERROR || ignore)
452
      table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
453
454
    transactional_table= table->file->has_transactions();
455
456
    if ((changed= (info.copied || info.deleted || info.updated)))
457
    {
458
      /*
459
        Invalidate the table in the query cache if something changed.
460
        For the transactional algorithm to work the invalidation must be
461
        before binlog writing and ha_autocommit_or_rollback
462
      */
463
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
464
    if ((changed && error <= 0) || session->transaction.stmt.modified_non_trans_table || was_insert_delayed)
1 by brian
clean slate
465
    {
466
      if (mysql_bin_log.is_open())
467
      {
468
	if (error <= 0)
469
        {
470
	  /*
471
	    [Guilhem wrote] Temporary errors may have filled
520.1.22 by Brian Aker
Second pass of thd cleanup
472
	    session->net.last_error/errno.  For example if there has
1 by brian
clean slate
473
	    been a disk full error when writing the row, and it was
520.1.22 by Brian Aker
Second pass of thd cleanup
474
	    MyISAM, then session->net.last_error/errno will be set to
1 by brian
clean slate
475
	    "disk full"... and the my_pwrite() will wait until free
476
	    space appears, and so when it finishes then the
477
	    write_row() was entirely successful
478
	  */
479
	  /* todo: consider removing */
520.1.22 by Brian Aker
Second pass of thd cleanup
480
	  session->clear_error();
1 by brian
clean slate
481
	}
482
	/* bug#22725:
483
484
	A query which per-row-loop can not be interrupted with
485
	KILLED, like INSERT, and that does not invoke stored
486
	routines can be binlogged with neglecting the KILLED error.
487
        
488
	If there was no error (error == zero) until after the end of
489
	inserting loop the KILLED flag that appeared later can be
490
	disregarded since previously possible invocation of stored
491
	routines did not result in any error due to the KILLED.  In
492
	such case the flag is ignored for constructing binlog event.
493
	*/
520.1.22 by Brian Aker
Second pass of thd cleanup
494
	assert(session->killed != Session::KILL_BAD_DATA || error > 0);
495
	if (session->binlog_query(Session::ROW_QUERY_TYPE,
496
			      session->query, session->query_length,
163 by Brian Aker
Merge Monty's code.
497
			      transactional_table, false,
520.1.22 by Brian Aker
Second pass of thd cleanup
498
			      (error>0) ? session->killed : Session::NOT_KILLED) &&
1 by brian
clean slate
499
	    transactional_table)
500
        {
501
	  error=1;
502
	}
503
      }
520.1.22 by Brian Aker
Second pass of thd cleanup
504
      if (session->transaction.stmt.modified_non_trans_table)
505
	session->transaction.all.modified_non_trans_table= true;
1 by brian
clean slate
506
    }
51.2.2 by Patrick Galbraith
Removed DBUGs from
507
    assert(transactional_table || !changed || 
520.1.22 by Brian Aker
Second pass of thd cleanup
508
                session->transaction.stmt.modified_non_trans_table);
1 by brian
clean slate
509
510
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
511
  session->set_proc_info("end");
1 by brian
clean slate
512
  /*
513
    We'll report to the client this id:
514
    - if the table contains an autoincrement column and we successfully
515
    inserted an autogenerated value, the autogenerated value.
516
    - if the table contains no autoincrement column and LAST_INSERT_ID(X) was
517
    called, X.
518
    - if the table contains an autoincrement column, and some rows were
519
    inserted, the id of the last "inserted" row (if IGNORE, that value may not
520
    have been really inserted but ignored).
521
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
522
  id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
523
    session->first_successful_insert_id_in_cur_stmt :
524
    (session->arg_of_last_insert_id_function ?
525
     session->first_successful_insert_id_in_prev_stmt :
1 by brian
clean slate
526
     ((table->next_number_field && info.copied) ?
527
     table->next_number_field->val_int() : 0));
528
  table->next_number_field=0;
520.1.22 by Brian Aker
Second pass of thd cleanup
529
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
163 by Brian Aker
Merge Monty's code.
530
  table->auto_increment_field_not_null= false;
1 by brian
clean slate
531
  if (duplic == DUP_REPLACE)
532
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
533
534
  if (error)
535
    goto abort;
520.1.22 by Brian Aker
Second pass of thd cleanup
536
  if (values_list.elements == 1 && (!(session->options & OPTION_WARNINGS) ||
537
				    !session->cuted_fields))
1 by brian
clean slate
538
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
539
    session->row_count_func= info.copied + info.deleted +
540
                         ((session->client_capabilities & CLIENT_FOUND_ROWS) ?
1 by brian
clean slate
541
                          info.touched : info.updated);
520.1.22 by Brian Aker
Second pass of thd cleanup
542
    my_ok(session, (ulong) session->row_count_func, id);
1 by brian
clean slate
543
  }
544
  else
545
  {
546
    char buff[160];
520.1.22 by Brian Aker
Second pass of thd cleanup
547
    ha_rows updated=((session->client_capabilities & CLIENT_FOUND_ROWS) ?
1 by brian
clean slate
548
                     info.touched : info.updated);
549
    if (ignore)
550
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
520.1.22 by Brian Aker
Second pass of thd cleanup
551
              (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1 by brian
clean slate
552
    else
553
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
520.1.22 by Brian Aker
Second pass of thd cleanup
554
	      (ulong) (info.deleted + updated), (ulong) session->cuted_fields);
555
    session->row_count_func= info.copied + info.deleted + updated;
556
    ::my_ok(session, (ulong) session->row_count_func, id, buff);
1 by brian
clean slate
557
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
558
  session->abort_on_warning= 0;
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
559
  DRIZZLE_INSERT_END();
163 by Brian Aker
Merge Monty's code.
560
  return(false);
1 by brian
clean slate
561
562
abort:
563
  if (table != NULL)
564
    table->file->ha_release_auto_increment();
565
  if (!joins_freed)
520.1.22 by Brian Aker
Second pass of thd cleanup
566
    free_underlaid_joins(session, &session->lex->select_lex);
567
  session->abort_on_warning= 0;
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
568
  DRIZZLE_INSERT_END();
163 by Brian Aker
Merge Monty's code.
569
  return(true);
1 by brian
clean slate
570
}
571
572
573
/*
574
  Check if table can be updated
575
576
  SYNOPSIS
577
     mysql_prepare_insert_check_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
578
     session		Thread handle
1 by brian
clean slate
579
     table_list		Table list
580
     fields		List of fields to be updated
581
     where		Pointer to where clause
582
     select_insert      Check is making for SELECT ... INSERT
583
584
   RETURN
163 by Brian Aker
Merge Monty's code.
585
     false ok
586
     true  ERROR
1 by brian
clean slate
587
*/
588
520.1.22 by Brian Aker
Second pass of thd cleanup
589
static bool mysql_prepare_insert_check_table(Session *session, TableList *table_list,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
590
                                             List<Item> &fields __attribute__((unused)),
1 by brian
clean slate
591
                                             bool select_insert)
592
{
51.2.2 by Patrick Galbraith
Removed DBUGs from
593
  
1 by brian
clean slate
594
595
  /*
596
     first table in list is the one we'll INSERT into, requires INSERT_ACL.
597
     all others require SELECT_ACL only. the ACL requirement below is for
598
     new leaves only anyway (view-constituents), so check for SELECT rather
599
     than INSERT.
600
  */
601
520.1.22 by Brian Aker
Second pass of thd cleanup
602
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
603
                                    &session->lex->select_lex.top_join_list,
1 by brian
clean slate
604
                                    table_list,
520.1.22 by Brian Aker
Second pass of thd cleanup
605
                                    &session->lex->select_lex.leaf_tables,
1 by brian
clean slate
606
                                    select_insert))
163 by Brian Aker
Merge Monty's code.
607
    return(true);
1 by brian
clean slate
608
163 by Brian Aker
Merge Monty's code.
609
  return(false);
1 by brian
clean slate
610
}
611
612
613
/*
614
  Prepare items in INSERT statement
615
616
  SYNOPSIS
617
    mysql_prepare_insert()
520.1.22 by Brian Aker
Second pass of thd cleanup
618
    session			Thread handler
1 by brian
clean slate
619
    table_list	        Global/local table list
620
    table		Table to insert into (can be NULL if table should
621
			be taken from table_list->table)    
622
    where		Where clause (for insert ... select)
163 by Brian Aker
Merge Monty's code.
623
    select_insert	true if INSERT ... SELECT statement
624
    check_fields        true if need to check that all INSERT fields are 
1 by brian
clean slate
625
                        given values.
626
    abort_on_warning    whether to report if some INSERT field is not 
163 by Brian Aker
Merge Monty's code.
627
                        assigned as an error (true) or as a warning (false).
1 by brian
clean slate
628
629
  TODO (in far future)
630
    In cases of:
631
    INSERT INTO t1 SELECT a, sum(a) as sum1 from t2 GROUP BY a
632
    ON DUPLICATE KEY ...
633
    we should be able to refer to sum1 in the ON DUPLICATE KEY part
634
635
  WARNING
636
    You MUST set table->insert_values to 0 after calling this function
637
    before releasing the table object.
638
  
639
  RETURN VALUE
163 by Brian Aker
Merge Monty's code.
640
    false OK
641
    true  error
1 by brian
clean slate
642
*/
643
520.1.22 by Brian Aker
Second pass of thd cleanup
644
bool mysql_prepare_insert(Session *session, TableList *table_list,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
645
                          Table *table, List<Item> &fields, List_item *values,
1 by brian
clean slate
646
                          List<Item> &update_fields, List<Item> &update_values,
647
                          enum_duplicates duplic,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
648
                          COND **where __attribute__((unused)),
77.1.45 by Monty Taylor
Warning fixes.
649
                          bool select_insert,
1 by brian
clean slate
650
                          bool check_fields, bool abort_on_warning)
651
{
520.1.22 by Brian Aker
Second pass of thd cleanup
652
  SELECT_LEX *select_lex= &session->lex->select_lex;
1 by brian
clean slate
653
  Name_resolution_context *context= &select_lex->context;
654
  Name_resolution_context_state ctx_state;
655
  bool insert_into_view= (0 != 0);
656
  bool res= 0;
657
  table_map map= 0;
51.2.2 by Patrick Galbraith
Removed DBUGs from
658
  
1 by brian
clean slate
659
  /* INSERT should have a SELECT or VALUES clause */
51.2.2 by Patrick Galbraith
Removed DBUGs from
660
  assert (!select_insert || !values);
1 by brian
clean slate
661
662
  /*
663
    For subqueries in VALUES() we should not see the table in which we are
664
    inserting (for INSERT ... SELECT this is done by changing table_list,
665
    because INSERT ... SELECT share SELECT_LEX it with SELECT.
666
  */
667
  if (!select_insert)
668
  {
669
    for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit();
670
         un;
671
         un= un->next_unit())
672
    {
673
      for (SELECT_LEX *sl= un->first_select();
674
           sl;
675
           sl= sl->next_select())
676
      {
677
        sl->context.outer_context= 0;
678
      }
679
    }
680
  }
681
682
  if (duplic == DUP_UPDATE)
683
  {
684
    /* it should be allocated before Item::fix_fields() */
520.1.22 by Brian Aker
Second pass of thd cleanup
685
    if (table_list->set_insert_values(session->mem_root))
163 by Brian Aker
Merge Monty's code.
686
      return(true);
1 by brian
clean slate
687
  }
688
520.1.22 by Brian Aker
Second pass of thd cleanup
689
  if (mysql_prepare_insert_check_table(session, table_list, fields, select_insert))
163 by Brian Aker
Merge Monty's code.
690
    return(true);
1 by brian
clean slate
691
692
693
  /* Prepare the fields in the statement. */
694
  if (values)
695
  {
696
    /* if we have INSERT ... VALUES () we cannot have a GROUP BY clause */
51.2.2 by Patrick Galbraith
Removed DBUGs from
697
    assert (!select_lex->group_list.elements);
1 by brian
clean slate
698
699
    /* Save the state of the current name resolution context. */
700
    ctx_state.save_state(context, table_list);
701
702
    /*
703
      Perform name resolution only in the first table - 'table_list',
704
      which is the table that is inserted into.
705
     */
706
    table_list->next_local= 0;
707
    context->resolve_in_table_list_only(table_list);
708
520.1.22 by Brian Aker
Second pass of thd cleanup
709
    res= check_insert_fields(session, context->table_list, fields, *values,
1 by brian
clean slate
710
                             !insert_into_view, &map) ||
520.1.22 by Brian Aker
Second pass of thd cleanup
711
      setup_fields(session, 0, *values, MARK_COLUMNS_READ, 0, 0);
1 by brian
clean slate
712
713
    if (!res && check_fields)
714
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
715
      bool saved_abort_on_warning= session->abort_on_warning;
716
      session->abort_on_warning= abort_on_warning;
717
      res= check_that_all_fields_are_given_values(session, 
1 by brian
clean slate
718
                                                  table ? table : 
719
                                                  context->table_list->table,
720
                                                  context->table_list);
520.1.22 by Brian Aker
Second pass of thd cleanup
721
      session->abort_on_warning= saved_abort_on_warning;
1 by brian
clean slate
722
    }
723
724
    if (!res && duplic == DUP_UPDATE)
725
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
726
      res= check_update_fields(session, context->table_list, update_fields, &map);
1 by brian
clean slate
727
    }
728
729
    /* Restore the current context. */
730
    ctx_state.restore_state(context, table_list);
731
732
    if (!res)
520.1.22 by Brian Aker
Second pass of thd cleanup
733
      res= setup_fields(session, 0, update_values, MARK_COLUMNS_READ, 0, 0);
1 by brian
clean slate
734
  }
735
736
  if (res)
51.2.2 by Patrick Galbraith
Removed DBUGs from
737
    return(res);
1 by brian
clean slate
738
739
  if (!table)
740
    table= table_list->table;
741
742
  if (!select_insert)
743
  {
327.2.4 by Brian Aker
Refactoring table.h
744
    TableList *duplicate;
520.1.22 by Brian Aker
Second pass of thd cleanup
745
    if ((duplicate= unique_table(session, table_list, table_list->next_global, 1)))
1 by brian
clean slate
746
    {
747
      update_non_unique_table_error(table_list, "INSERT", duplicate);
163 by Brian Aker
Merge Monty's code.
748
      return(true);
1 by brian
clean slate
749
    }
750
  }
751
  if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
752
    table->prepare_for_position();
163 by Brian Aker
Merge Monty's code.
753
  return(false);
1 by brian
clean slate
754
}
755
756
757
	/* Check if there is more uniq keys after field */
758
482 by Brian Aker
Remove uint.
759
static int last_uniq_key(Table *table,uint32_t keynr)
1 by brian
clean slate
760
{
761
  while (++keynr < table->s->keys)
762
    if (table->key_info[keynr].flags & HA_NOSAME)
763
      return 0;
764
  return 1;
765
}
766
767
768
/*
769
  Write a record to table with optional deleting of conflicting records,
770
  invoke proper triggers if needed.
771
772
  SYNOPSIS
773
     write_record()
520.1.22 by Brian Aker
Second pass of thd cleanup
774
      session   - thread context
1 by brian
clean slate
775
      table - table to which record should be written
776
      info  - COPY_INFO structure describing handling of duplicates
777
              and which is used for counting number of records inserted
778
              and deleted.
779
780
  NOTE
781
    Once this record will be written to table after insert trigger will
782
    be invoked. If instead of inserting new record we will update old one
783
    then both on update triggers will work instead. Similarly both on
784
    delete triggers will be invoked if we will delete conflicting records.
785
520.1.22 by Brian Aker
Second pass of thd cleanup
786
    Sets session->transaction.stmt.modified_non_trans_table to true if table which is updated didn't have
1 by brian
clean slate
787
    transactions.
788
789
  RETURN VALUE
790
    0     - success
791
    non-0 - error
792
*/
793
794
520.1.22 by Brian Aker
Second pass of thd cleanup
795
int write_record(Session *session, Table *table,COPY_INFO *info)
1 by brian
clean slate
796
{
797
  int error;
798
  char *key=0;
799
  MY_BITMAP *save_read_set, *save_write_set;
151 by Brian Aker
Ulonglong to uint64_t
800
  uint64_t prev_insert_id= table->file->next_insert_id;
801
  uint64_t insert_id_for_cur_row= 0;
51.2.2 by Patrick Galbraith
Removed DBUGs from
802
  
1 by brian
clean slate
803
804
  info->records++;
805
  save_read_set=  table->read_set;
806
  save_write_set= table->write_set;
807
808
  if (info->handle_duplicates == DUP_REPLACE ||
809
      info->handle_duplicates == DUP_UPDATE)
810
  {
811
    while ((error=table->file->ha_write_row(table->record[0])))
812
    {
482 by Brian Aker
Remove uint.
813
      uint32_t key_nr;
1 by brian
clean slate
814
      /*
815
        If we do more than one iteration of this loop, from the second one the
816
        row will have an explicit value in the autoinc field, which was set at
817
        the first call of handler::update_auto_increment(). So we must save
520.1.22 by Brian Aker
Second pass of thd cleanup
818
        the autogenerated value to avoid session->insert_id_for_cur_row to become
1 by brian
clean slate
819
        0.
820
      */
821
      if (table->file->insert_id_for_cur_row > 0)
822
        insert_id_for_cur_row= table->file->insert_id_for_cur_row;
823
      else
824
        table->file->insert_id_for_cur_row= insert_id_for_cur_row;
825
      bool is_duplicate_key_error;
826
      if (table->file->is_fatal_error(error, HA_CHECK_DUP))
827
	goto err;
828
      is_duplicate_key_error= table->file->is_fatal_error(error, 0);
829
      if (!is_duplicate_key_error)
830
      {
831
        /*
832
          We come here when we had an ignorable error which is not a duplicate
833
          key error. In this we ignore error if ignore flag is set, otherwise
834
          report error as usual. We will not do any duplicate key processing.
835
        */
836
        if (info->ignore)
837
          goto gok_or_after_err; /* Ignoring a not fatal error, return 0 */
838
        goto err;
839
      }
840
      if ((int) (key_nr = table->file->get_dup_key(error)) < 0)
841
      {
842
	error= HA_ERR_FOUND_DUPP_KEY;         /* Database can't find key */
843
	goto err;
844
      }
845
      /* Read all columns for the row we are going to replace */
846
      table->use_all_columns();
847
      /*
848
	Don't allow REPLACE to replace a row when a auto_increment column
849
	was used.  This ensures that we don't get a problem when the
850
	whole range of the key has been used.
851
      */
852
      if (info->handle_duplicates == DUP_REPLACE &&
853
          table->next_number_field &&
854
          key_nr == table->s->next_number_index &&
855
	  (insert_id_for_cur_row > 0))
856
	goto err;
857
      if (table->file->ha_table_flags() & HA_DUPLICATE_POS)
858
      {
859
	if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
860
	  goto err;
861
      }
862
      else
863
      {
864
	if (table->file->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */
865
	{
866
	  error=my_errno;
867
	  goto err;
868
	}
869
870
	if (!key)
871
	{
872
	  if (!(key=(char*) my_safe_alloca(table->s->max_unique_length,
873
					   MAX_KEY_LENGTH)))
874
	  {
875
	    error=ENOMEM;
876
	    goto err;
877
	  }
878
	}
481 by Brian Aker
Remove all of uchar.
879
	key_copy((unsigned char*) key,table->record[0],table->key_info+key_nr,0);
1 by brian
clean slate
880
	if ((error=(table->file->index_read_idx_map(table->record[1],key_nr,
481 by Brian Aker
Remove all of uchar.
881
                                                    (unsigned char*) key, HA_WHOLE_KEY,
1 by brian
clean slate
882
                                                    HA_READ_KEY_EXACT))))
883
	  goto err;
884
      }
885
      if (info->handle_duplicates == DUP_UPDATE)
886
      {
887
        /*
888
          We don't check for other UNIQUE keys - the first row
889
          that matches, is updated. If update causes a conflict again,
890
          an error is returned
891
        */
51.2.2 by Patrick Galbraith
Removed DBUGs from
892
	assert(table->insert_values != NULL);
1 by brian
clean slate
893
        store_record(table,insert_values);
894
        restore_record(table,record[1]);
51.2.2 by Patrick Galbraith
Removed DBUGs from
895
        assert(info->update_fields->elements ==
1 by brian
clean slate
896
                    info->update_values->elements);
520.1.22 by Brian Aker
Second pass of thd cleanup
897
        if (fill_record(session, *info->update_fields,
1 by brian
clean slate
898
                                                 *info->update_values,
899
                                                 info->ignore))
900
          goto before_err;
901
902
        table->file->restore_auto_increment(prev_insert_id);
903
        if (table->next_number_field)
904
          table->file->adjust_next_insert_id_after_explicit_value(
905
            table->next_number_field->val_int());
906
        info->touched++;
907
        if ((table->file->ha_table_flags() & HA_PARTIAL_COLUMN_READ &&
908
             !bitmap_is_subset(table->write_set, table->read_set)) ||
355 by Brian Aker
More Table cleanup
909
            table->compare_record())
1 by brian
clean slate
910
        {
911
          if ((error=table->file->ha_update_row(table->record[1],
912
                                                table->record[0])) &&
913
              error != HA_ERR_RECORD_IS_THE_SAME)
914
          {
915
            if (info->ignore &&
916
                !table->file->is_fatal_error(error, HA_CHECK_DUP_KEY))
917
            {
918
              goto gok_or_after_err;
919
            }
920
            goto err;
921
          }
922
923
          if (error != HA_ERR_RECORD_IS_THE_SAME)
924
            info->updated++;
925
          else
926
            error= 0;
927
          /*
928
            If ON DUP KEY UPDATE updates a row instead of inserting one, it's
929
            like a regular UPDATE statement: it should not affect the value of a
930
            next SELECT LAST_INSERT_ID() or mysql_insert_id().
931
            Except if LAST_INSERT_ID(#) was in the INSERT query, which is
520.1.21 by Brian Aker
THD -> Session rename
932
            handled separately by Session::arg_of_last_insert_id_function.
1 by brian
clean slate
933
          */
934
          insert_id_for_cur_row= table->file->insert_id_for_cur_row= 0;
935
          info->copied++;
936
        }
937
938
        if (table->next_number_field)
939
          table->file->adjust_next_insert_id_after_explicit_value(
940
            table->next_number_field->val_int());
941
        info->touched++;
942
943
        goto gok_or_after_err;
944
      }
945
      else /* DUP_REPLACE */
946
      {
947
	/*
948
	  The manual defines the REPLACE semantics that it is either
949
	  an INSERT or DELETE(s) + INSERT; FOREIGN KEY checks in
950
	  InnoDB do not function in the defined way if we allow MySQL
951
	  to convert the latter operation internally to an UPDATE.
952
          We also should not perform this conversion if we have 
953
          timestamp field with ON UPDATE which is different from DEFAULT.
954
          Another case when conversion should not be performed is when
955
          we have ON DELETE trigger on table so user may notice that
956
          we cheat here. Note that it is ok to do such conversion for
957
          tables which have ON UPDATE but have no ON DELETE triggers,
958
          we just should not expose this fact to users by invoking
959
          ON UPDATE triggers.
960
	*/
961
	if (last_uniq_key(table,key_nr) &&
962
	    !table->file->referenced_by_foreign_key() &&
963
            (table->timestamp_field_type == TIMESTAMP_NO_AUTO_SET ||
964
             table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
965
        {
966
          if ((error=table->file->ha_update_row(table->record[1],
967
					        table->record[0])) &&
968
              error != HA_ERR_RECORD_IS_THE_SAME)
969
            goto err;
970
          if (error != HA_ERR_RECORD_IS_THE_SAME)
971
            info->deleted++;
972
          else
973
            error= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
974
          session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
1 by brian
clean slate
975
          /*
976
            Since we pretend that we have done insert we should call
977
            its after triggers.
978
          */
979
          goto after_n_copied_inc;
980
        }
981
        else
982
        {
983
          if ((error=table->file->ha_delete_row(table->record[1])))
984
            goto err;
985
          info->deleted++;
986
          if (!table->file->has_transactions())
520.1.22 by Brian Aker
Second pass of thd cleanup
987
            session->transaction.stmt.modified_non_trans_table= true;
1 by brian
clean slate
988
          /* Let us attempt do write_row() once more */
989
        }
990
      }
991
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
992
    session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
1 by brian
clean slate
993
    /*
994
      Restore column maps if they where replaced during an duplicate key
995
      problem.
996
    */
997
    if (table->read_set != save_read_set ||
998
        table->write_set != save_write_set)
999
      table->column_bitmaps_set(save_read_set, save_write_set);
1000
  }
1001
  else if ((error=table->file->ha_write_row(table->record[0])))
1002
  {
1003
    if (!info->ignore ||
1004
        table->file->is_fatal_error(error, HA_CHECK_DUP))
1005
      goto err;
1006
    table->file->restore_auto_increment(prev_insert_id);
1007
    goto gok_or_after_err;
1008
  }
1009
1010
after_n_copied_inc:
1011
  info->copied++;
520.1.22 by Brian Aker
Second pass of thd cleanup
1012
  session->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
1 by brian
clean slate
1013
1014
gok_or_after_err:
1015
  if (key)
1016
    my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
1017
  if (!table->file->has_transactions())
520.1.22 by Brian Aker
Second pass of thd cleanup
1018
    session->transaction.stmt.modified_non_trans_table= true;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1019
  return(0);
1 by brian
clean slate
1020
1021
err:
1022
  info->last_errno= error;
1023
  /* current_select is NULL if this is a delayed insert */
520.1.22 by Brian Aker
Second pass of thd cleanup
1024
  if (session->lex->current_select)
1025
    session->lex->current_select->no_error= 0;        // Give error
1 by brian
clean slate
1026
  table->file->print_error(error,MYF(0));
1027
  
1028
before_err:
1029
  table->file->restore_auto_increment(prev_insert_id);
1030
  if (key)
1031
    my_safe_afree(key, table->s->max_unique_length, MAX_KEY_LENGTH);
1032
  table->column_bitmaps_set(save_read_set, save_write_set);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1033
  return(1);
1 by brian
clean slate
1034
}
1035
1036
1037
/******************************************************************************
1038
  Check that all fields with arn't null_fields are used
1039
******************************************************************************/
1040
520.1.22 by Brian Aker
Second pass of thd cleanup
1041
int check_that_all_fields_are_given_values(Session *session, Table *entry,
327.2.4 by Brian Aker
Refactoring table.h
1042
                                           TableList *table_list)
1 by brian
clean slate
1043
{
1044
  int err= 0;
1045
  MY_BITMAP *write_set= entry->write_set;
1046
1047
  for (Field **field=entry->field ; *field ; field++)
1048
  {
1049
    if (!bitmap_is_set(write_set, (*field)->field_index) &&
1050
        ((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1051
        ((*field)->real_type() != DRIZZLE_TYPE_ENUM))
1 by brian
clean slate
1052
    {
163 by Brian Aker
Merge Monty's code.
1053
      bool view= false;
1 by brian
clean slate
1054
      if (table_list)
1055
      {
1056
        table_list= table_list->top_table();
1057
        view= test(0);
1058
      }
1059
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
1060
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1 by brian
clean slate
1061
                            ER_NO_DEFAULT_FOR_FIELD,
1062
                            ER(ER_NO_DEFAULT_FOR_FIELD),
1063
                            (*field)->field_name);
1064
      }
1065
      err= 1;
1066
    }
1067
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1068
  return session->abort_on_warning ? err : 0;
1 by brian
clean slate
1069
}
1070
1071
/***************************************************************************
1072
  Store records in INSERT ... SELECT *
1073
***************************************************************************/
1074
1075
1076
/*
1077
  make insert specific preparation and checks after opening tables
1078
1079
  SYNOPSIS
1080
    mysql_insert_select_prepare()
520.1.22 by Brian Aker
Second pass of thd cleanup
1081
    session         thread handler
1 by brian
clean slate
1082
1083
  RETURN
163 by Brian Aker
Merge Monty's code.
1084
    false OK
1085
    true  Error
1 by brian
clean slate
1086
*/
1087
520.1.22 by Brian Aker
Second pass of thd cleanup
1088
bool mysql_insert_select_prepare(Session *session)
1 by brian
clean slate
1089
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1090
  LEX *lex= session->lex;
1 by brian
clean slate
1091
  SELECT_LEX *select_lex= &lex->select_lex;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1092
  
1 by brian
clean slate
1093
1094
  /*
1095
    Statement-based replication of INSERT ... SELECT ... LIMIT is not safe
1096
    as order of rows is not defined, so in mixed mode we go to row-based.
1097
1098
    Note that we may consider a statement as safe if ORDER BY primary_key
1099
    is present or we SELECT a constant. However it may confuse users to
1100
    see very similiar statements replicated differently.
1101
  */
1102
  if (lex->current_select->select_limit)
1103
  {
1104
    lex->set_stmt_unsafe();
520.1.22 by Brian Aker
Second pass of thd cleanup
1105
    session->set_current_stmt_binlog_row_based_if_mixed();
1 by brian
clean slate
1106
  }
1107
  /*
1108
    SELECT_LEX do not belong to INSERT statement, so we can't add WHERE
1109
    clause if table is VIEW
1110
  */
1111
  
520.1.22 by Brian Aker
Second pass of thd cleanup
1112
  if (mysql_prepare_insert(session, lex->query_tables,
1 by brian
clean slate
1113
                           lex->query_tables->table, lex->field_list, 0,
1114
                           lex->update_list, lex->value_list,
1115
                           lex->duplicates,
163 by Brian Aker
Merge Monty's code.
1116
                           &select_lex->where, true, false, false))
1117
    return(true);
1 by brian
clean slate
1118
1119
  /*
1120
    exclude first table from leaf tables list, because it belong to
1121
    INSERT
1122
  */
51.2.2 by Patrick Galbraith
Removed DBUGs from
1123
  assert(select_lex->leaf_tables != 0);
1 by brian
clean slate
1124
  lex->leaf_tables_insert= select_lex->leaf_tables;
1125
  /* skip all leaf tables belonged to view where we are insert */
327.1.7 by Brian Aker
Removed belong_to_view variable
1126
  select_lex->leaf_tables= select_lex->leaf_tables->next_leaf;
163 by Brian Aker
Merge Monty's code.
1127
  return(false);
1 by brian
clean slate
1128
}
1129
1130
327.2.4 by Brian Aker
Refactoring table.h
1131
select_insert::select_insert(TableList *table_list_par, Table *table_par,
1 by brian
clean slate
1132
                             List<Item> *fields_par,
1133
                             List<Item> *update_fields,
1134
                             List<Item> *update_values,
1135
                             enum_duplicates duplic,
1136
                             bool ignore_check_option_errors)
1137
  :table_list(table_list_par), table(table_par), fields(fields_par),
1138
   autoinc_value_of_last_inserted_row(0),
1139
   insert_into_view(table_list_par && 0 != 0)
1140
{
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
1141
  memset(&info, 0, sizeof(info));
1 by brian
clean slate
1142
  info.handle_duplicates= duplic;
1143
  info.ignore= ignore_check_option_errors;
1144
  info.update_fields= update_fields;
1145
  info.update_values= update_values;
1146
}
1147
1148
1149
int
1150
select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
1151
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1152
  LEX *lex= session->lex;
1 by brian
clean slate
1153
  int res;
1154
  table_map map= 0;
1155
  SELECT_LEX *lex_current_select_save= lex->current_select;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1156
  
1 by brian
clean slate
1157
1158
  unit= u;
1159
1160
  /*
1161
    Since table in which we are going to insert is added to the first
1162
    select, LEX::current_select should point to the first select while
1163
    we are fixing fields from insert list.
1164
  */
1165
  lex->current_select= &lex->select_lex;
520.1.22 by Brian Aker
Second pass of thd cleanup
1166
  res= check_insert_fields(session, table_list, *fields, values,
1 by brian
clean slate
1167
                           !insert_into_view, &map) ||
520.1.22 by Brian Aker
Second pass of thd cleanup
1168
       setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0);
1 by brian
clean slate
1169
1170
  if (!res && fields->elements)
1171
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
1172
    bool saved_abort_on_warning= session->abort_on_warning;
1173
    session->abort_on_warning= !info.ignore;
1174
    res= check_that_all_fields_are_given_values(session, table_list->table, 
1 by brian
clean slate
1175
                                                table_list);
520.1.22 by Brian Aker
Second pass of thd cleanup
1176
    session->abort_on_warning= saved_abort_on_warning;
1 by brian
clean slate
1177
  }
1178
1179
  if (info.handle_duplicates == DUP_UPDATE && !res)
1180
  {
1181
    Name_resolution_context *context= &lex->select_lex.context;
1182
    Name_resolution_context_state ctx_state;
1183
1184
    /* Save the state of the current name resolution context. */
1185
    ctx_state.save_state(context, table_list);
1186
1187
    /* Perform name resolution only in the first table - 'table_list'. */
1188
    table_list->next_local= 0;
1189
    context->resolve_in_table_list_only(table_list);
1190
520.1.22 by Brian Aker
Second pass of thd cleanup
1191
    res= res || check_update_fields(session, context->table_list,
1 by brian
clean slate
1192
                                    *info.update_fields, &map);
1193
    /*
1194
      When we are not using GROUP BY and there are no ungrouped aggregate functions 
1195
      we can refer to other tables in the ON DUPLICATE KEY part.
1196
      We use next_name_resolution_table descructively, so check it first (views?)
1197
    */
51.2.2 by Patrick Galbraith
Removed DBUGs from
1198
    assert (!table_list->next_name_resolution_table);
1 by brian
clean slate
1199
    if (lex->select_lex.group_list.elements == 0 &&
1200
        !lex->select_lex.with_sum_func)
1201
      /*
1202
        We must make a single context out of the two separate name resolution contexts :
1203
        the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
1204
        To do that we must concatenate the two lists
1205
      */  
1206
      table_list->next_name_resolution_table= 
1207
        ctx_state.get_first_name_resolution_table();
1208
520.1.22 by Brian Aker
Second pass of thd cleanup
1209
    res= res || setup_fields(session, 0, *info.update_values,
1 by brian
clean slate
1210
                             MARK_COLUMNS_READ, 0, 0);
1211
    if (!res)
1212
    {
1213
      /*
1214
        Traverse the update values list and substitute fields from the
1215
        select for references (Item_ref objects) to them. This is done in
1216
        order to get correct values from those fields when the select
1217
        employs a temporary table.
1218
      */
1219
      List_iterator<Item> li(*info.update_values);
1220
      Item *item;
1221
1222
      while ((item= li++))
1223
      {
1224
        item->transform(&Item::update_value_transformer,
481 by Brian Aker
Remove all of uchar.
1225
                        (unsigned char*)lex->current_select);
1 by brian
clean slate
1226
      }
1227
    }
1228
1229
    /* Restore the current context. */
1230
    ctx_state.restore_state(context, table_list);
1231
  }
1232
1233
  lex->current_select= lex_current_select_save;
1234
  if (res)
51.2.2 by Patrick Galbraith
Removed DBUGs from
1235
    return(1);
1 by brian
clean slate
1236
  /*
1237
    if it is INSERT into join view then check_insert_fields already found
1238
    real table for insert
1239
  */
1240
  table= table_list->table;
1241
1242
  /*
1243
    Is table which we are changing used somewhere in other parts of
1244
    query
1245
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
1246
  if (unique_table(session, table_list, table_list->next_global, 0))
1 by brian
clean slate
1247
  {
1248
    /* Using same table for INSERT and SELECT */
1249
    lex->current_select->options|= OPTION_BUFFER_RESULT;
1250
    lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
1251
  }
1252
  else if (!(lex->current_select->options & OPTION_BUFFER_RESULT))
1253
  {
1254
    /*
1255
      We must not yet prepare the result table if it is the same as one of the 
1256
      source tables (INSERT SELECT). The preparation may disable 
1257
      indexes on the result table, which may be used during the select, if it
1258
      is the same table (Bug #6034). Do the preparation after the select phase
1259
      in select_insert::prepare2().
1260
      We won't start bulk inserts at all if this statement uses functions or
1261
      should invoke triggers since they may access to the same table too.
1262
    */
1263
    table->file->ha_start_bulk_insert((ha_rows) 0);
1264
  }
1265
  restore_record(table,s->default_values);		// Get empty record
1266
  table->next_number_field=table->found_next_number_field;
1267
520.1.22 by Brian Aker
Second pass of thd cleanup
1268
  if (session->slave_thread &&
1 by brian
clean slate
1269
      (info.handle_duplicates == DUP_UPDATE) &&
1270
      (table->next_number_field != NULL) &&
1271
      rpl_master_has_bug(&active_mi->rli, 24432))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1272
    return(1);
1 by brian
clean slate
1273
520.1.22 by Brian Aker
Second pass of thd cleanup
1274
  session->cuted_fields=0;
1 by brian
clean slate
1275
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1276
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1277
  if (info.handle_duplicates == DUP_REPLACE)
1278
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1279
  if (info.handle_duplicates == DUP_UPDATE)
1280
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
520.1.22 by Brian Aker
Second pass of thd cleanup
1281
  session->abort_on_warning= !info.ignore;
1 by brian
clean slate
1282
  table->mark_columns_needed_for_insert();
1283
1284
51.2.2 by Patrick Galbraith
Removed DBUGs from
1285
  return(res);
1 by brian
clean slate
1286
}
1287
1288
1289
/*
1290
  Finish the preparation of the result table.
1291
1292
  SYNOPSIS
1293
    select_insert::prepare2()
1294
    void
1295
1296
  DESCRIPTION
1297
    If the result table is the same as one of the source tables (INSERT SELECT),
1298
    the result table is not finally prepared at the join prepair phase.
1299
    Do the final preparation now.
1300
		       
1301
  RETURN
1302
    0   OK
1303
*/
1304
1305
int select_insert::prepare2(void)
1306
{
51.2.2 by Patrick Galbraith
Removed DBUGs from
1307
  
520.1.22 by Brian Aker
Second pass of thd cleanup
1308
  if (session->lex->current_select->options & OPTION_BUFFER_RESULT)
1 by brian
clean slate
1309
    table->file->ha_start_bulk_insert((ha_rows) 0);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1310
  return(0);
1 by brian
clean slate
1311
}
1312
1313
1314
void select_insert::cleanup()
1315
{
1316
  /* select_insert/select_create are never re-used in prepared statement */
51.2.2 by Patrick Galbraith
Removed DBUGs from
1317
  assert(0);
1 by brian
clean slate
1318
}
1319
1320
select_insert::~select_insert()
1321
{
51.2.2 by Patrick Galbraith
Removed DBUGs from
1322
  
1 by brian
clean slate
1323
  if (table)
1324
  {
1325
    table->next_number_field=0;
163 by Brian Aker
Merge Monty's code.
1326
    table->auto_increment_field_not_null= false;
1 by brian
clean slate
1327
    table->file->ha_reset();
1328
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1329
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1330
  session->abort_on_warning= 0;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1331
  return;
1 by brian
clean slate
1332
}
1333
1334
1335
bool select_insert::send_data(List<Item> &values)
1336
{
51.2.2 by Patrick Galbraith
Removed DBUGs from
1337
  
1 by brian
clean slate
1338
  bool error=0;
1339
1340
  if (unit->offset_limit_cnt)
1341
  {						// using limit offset,count
1342
    unit->offset_limit_cnt--;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1343
    return(0);
1 by brian
clean slate
1344
  }
1345
520.1.22 by Brian Aker
Second pass of thd cleanup
1346
  session->count_cuted_fields= CHECK_FIELD_WARN;	// Calculate cuted fields
1 by brian
clean slate
1347
  store_values(values);
520.1.22 by Brian Aker
Second pass of thd cleanup
1348
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1349
  if (session->is_error())
51.2.2 by Patrick Galbraith
Removed DBUGs from
1350
    return(1);
1 by brian
clean slate
1351
520.1.22 by Brian Aker
Second pass of thd cleanup
1352
  error= write_record(session, table, &info);
1 by brian
clean slate
1353
    
1354
  if (!error)
1355
  {
1356
    if (info.handle_duplicates == DUP_UPDATE)
1357
    {
1358
      /*
1359
        Restore fields of the record since it is possible that they were
1360
        changed by ON DUPLICATE KEY UPDATE clause.
1361
    
1362
        If triggers exist then whey can modify some fields which were not
1363
        originally touched by INSERT ... SELECT, so we have to restore
1364
        their original values for the next row.
1365
      */
1366
      restore_record(table, s->default_values);
1367
    }
1368
    if (table->next_number_field)
1369
    {
1370
      /*
1371
        If no value has been autogenerated so far, we need to remember the
1372
        value we just saw, we may need to send it to client in the end.
1373
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
1374
      if (session->first_successful_insert_id_in_cur_stmt == 0) // optimization
1 by brian
clean slate
1375
        autoinc_value_of_last_inserted_row= 
1376
          table->next_number_field->val_int();
1377
      /*
1378
        Clear auto-increment field for the next record, if triggers are used
1379
        we will clear it twice, but this should be cheap.
1380
      */
1381
      table->next_number_field->reset();
1382
    }
1383
  }
51.2.2 by Patrick Galbraith
Removed DBUGs from
1384
  return(error);
1 by brian
clean slate
1385
}
1386
1387
1388
void select_insert::store_values(List<Item> &values)
1389
{
1390
  if (fields->elements)
520.1.22 by Brian Aker
Second pass of thd cleanup
1391
    fill_record(session, *fields, values, 1);
1 by brian
clean slate
1392
  else
520.1.22 by Brian Aker
Second pass of thd cleanup
1393
    fill_record(session, table->field, values, 1);
1 by brian
clean slate
1394
}
1395
482 by Brian Aker
Remove uint.
1396
void select_insert::send_error(uint32_t errcode,const char *err)
1 by brian
clean slate
1397
{
51.2.2 by Patrick Galbraith
Removed DBUGs from
1398
  
1 by brian
clean slate
1399
1400
  my_message(errcode, err, MYF(0));
1401
51.2.2 by Patrick Galbraith
Removed DBUGs from
1402
  return;
1 by brian
clean slate
1403
}
1404
1405
1406
bool select_insert::send_eof()
1407
{
1408
  int error;
1409
  bool const trans_table= table->file->has_transactions();
151 by Brian Aker
Ulonglong to uint64_t
1410
  uint64_t id;
1 by brian
clean slate
1411
  bool changed;
520.1.22 by Brian Aker
Second pass of thd cleanup
1412
  Session::killed_state killed_status= session->killed;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1413
  
1 by brian
clean slate
1414
  error= table->file->ha_end_bulk_insert();
1415
  table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1416
  table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1417
1418
  if ((changed= (info.copied || info.deleted || info.updated)))
1419
  {
1420
    /*
1421
      We must invalidate the table in the query cache before binlog writing
1422
      and ha_autocommit_or_rollback.
1423
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
1424
    if (session->transaction.stmt.modified_non_trans_table)
1425
      session->transaction.all.modified_non_trans_table= true;
1 by brian
clean slate
1426
  }
51.2.2 by Patrick Galbraith
Removed DBUGs from
1427
  assert(trans_table || !changed || 
520.1.22 by Brian Aker
Second pass of thd cleanup
1428
              session->transaction.stmt.modified_non_trans_table);
1 by brian
clean slate
1429
1430
  /*
1431
    Write to binlog before commiting transaction.  No statement will
1432
    be written by the binlog_query() below in RBR mode.  All the
1433
    events are in the transaction cache and will be written when
1434
    ha_autocommit_or_rollback() is issued below.
1435
  */
1436
  if (mysql_bin_log.is_open())
1437
  {
1438
    if (!error)
520.1.22 by Brian Aker
Second pass of thd cleanup
1439
      session->clear_error();
1440
    session->binlog_query(Session::ROW_QUERY_TYPE,
1441
                      session->query, session->query_length,
163 by Brian Aker
Merge Monty's code.
1442
                      trans_table, false, killed_status);
1 by brian
clean slate
1443
  }
1444
  table->file->ha_release_auto_increment();
1445
1446
  if (error)
1447
  {
1448
    table->file->print_error(error,MYF(0));
51.2.2 by Patrick Galbraith
Removed DBUGs from
1449
    return(1);
1 by brian
clean slate
1450
  }
1451
  char buff[160];
1452
  if (info.ignore)
1453
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
520.1.22 by Brian Aker
Second pass of thd cleanup
1454
	    (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1 by brian
clean slate
1455
  else
1456
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
520.1.22 by Brian Aker
Second pass of thd cleanup
1457
	    (ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1458
  session->row_count_func= info.copied + info.deleted +
1459
                       ((session->client_capabilities & CLIENT_FOUND_ROWS) ?
1 by brian
clean slate
1460
                        info.touched : info.updated);
1461
520.1.22 by Brian Aker
Second pass of thd cleanup
1462
  id= (session->first_successful_insert_id_in_cur_stmt > 0) ?
1463
    session->first_successful_insert_id_in_cur_stmt :
1464
    (session->arg_of_last_insert_id_function ?
1465
     session->first_successful_insert_id_in_prev_stmt :
1 by brian
clean slate
1466
     (info.copied ? autoinc_value_of_last_inserted_row : 0));
520.1.22 by Brian Aker
Second pass of thd cleanup
1467
  ::my_ok(session, (ulong) session->row_count_func, id, buff);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1468
  return(0);
1 by brian
clean slate
1469
}
1470
1471
void select_insert::abort() {
1472
51.2.2 by Patrick Galbraith
Removed DBUGs from
1473
  
1 by brian
clean slate
1474
  /*
1475
    If the creation of the table failed (due to a syntax error, for
1476
    example), no table will have been opened and therefore 'table'
1477
    will be NULL. In that case, we still need to execute the rollback
1478
    and the end of the function.
1479
   */
1480
  if (table)
1481
  {
1482
    bool changed, transactional_table;
1483
1484
    table->file->ha_end_bulk_insert();
1485
1486
    /*
1487
      If at least one row has been inserted/modified and will stay in
1488
      the table (the table doesn't have transactions) we must write to
1489
      the binlog (and the error code will make the slave stop).
1490
1491
      For many errors (example: we got a duplicate key error while
1492
      inserting into a MyISAM table), no row will be added to the table,
1493
      so passing the error to the slave will not help since there will
1494
      be an error code mismatch (the inserts will succeed on the slave
1495
      with no error).
1496
1497
      If table creation failed, the number of rows modified will also be
1498
      zero, so no check for that is made.
1499
    */
1500
    changed= (info.copied || info.deleted || info.updated);
1501
    transactional_table= table->file->has_transactions();
520.1.22 by Brian Aker
Second pass of thd cleanup
1502
    if (session->transaction.stmt.modified_non_trans_table)
1 by brian
clean slate
1503
    {
1504
        if (mysql_bin_log.is_open())
520.1.22 by Brian Aker
Second pass of thd cleanup
1505
          session->binlog_query(Session::ROW_QUERY_TYPE, session->query, session->query_length,
163 by Brian Aker
Merge Monty's code.
1506
                            transactional_table, false);
520.1.22 by Brian Aker
Second pass of thd cleanup
1507
        if (!session->current_stmt_binlog_row_based && !can_rollback_data())
1508
          session->transaction.all.modified_non_trans_table= true;
1 by brian
clean slate
1509
    }
51.2.2 by Patrick Galbraith
Removed DBUGs from
1510
    assert(transactional_table || !changed ||
520.1.22 by Brian Aker
Second pass of thd cleanup
1511
		session->transaction.stmt.modified_non_trans_table);
1 by brian
clean slate
1512
    table->file->ha_release_auto_increment();
1513
  }
1514
51.2.2 by Patrick Galbraith
Removed DBUGs from
1515
  return;
1 by brian
clean slate
1516
}
1517
1518
1519
/***************************************************************************
1520
  CREATE TABLE (SELECT) ...
1521
***************************************************************************/
1522
1523
/*
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1524
  Create table from lists of fields and items (or just return Table
1 by brian
clean slate
1525
  object for pre-opened existing table).
1526
1527
  SYNOPSIS
1528
    create_table_from_items()
520.1.22 by Brian Aker
Second pass of thd cleanup
1529
      session          in     Thread object
1 by brian
clean slate
1530
      create_info  in     Create information (like MAX_ROWS, ENGINE or
1531
                          temporary table flag)
327.2.4 by Brian Aker
Refactoring table.h
1532
      create_table in     Pointer to TableList object providing database
1 by brian
clean slate
1533
                          and name for table to be created or to be open
1534
      alter_info   in/out Initial list of columns and indexes for the table
1535
                          to be created
1536
      items        in     List of items which should be used to produce rest
1537
                          of fields for the table (corresponding fields will
1538
                          be added to the end of alter_info->create_list)
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1539
      lock         out    Pointer to the DRIZZLE_LOCK object for table created
1 by brian
clean slate
1540
                          (or open temporary table) will be returned in this
1541
                          parameter. Since this table is not included in
520.1.21 by Brian Aker
THD -> Session rename
1542
                          Session::lock caller is responsible for explicitly
1 by brian
clean slate
1543
                          unlocking this table.
1544
      hooks
1545
1546
  NOTES
1547
    This function behaves differently for base and temporary tables:
1548
    - For base table we assume that either table exists and was pre-opened
1549
      and locked at open_and_lock_tables() stage (and in this case we just
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1550
      emit error or warning and return pre-opened Table object) or special
1 by brian
clean slate
1551
      placeholder was put in table cache that guarantees that this table
1552
      won't be created or opened until the placeholder will be removed
1553
      (so there is an exclusive lock on this table).
1554
    - We don't pre-open existing temporary table, instead we either open
1555
      or create and then open table in this function.
1556
1557
    Since this function contains some logic specific to CREATE TABLE ...
1558
    SELECT it should be changed before it can be used in other contexts.
1559
1560
  RETURN VALUES
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1561
    non-zero  Pointer to Table object for table created or opened
1 by brian
clean slate
1562
    0         Error
1563
*/
1564
520.1.22 by Brian Aker
Second pass of thd cleanup
1565
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
327.2.4 by Brian Aker
Refactoring table.h
1566
                                      TableList *create_table,
1 by brian
clean slate
1567
                                      Alter_info *alter_info,
1568
                                      List<Item> *items,
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1569
                                      DRIZZLE_LOCK **lock,
575.1.3 by Monty Taylor
Moved some stuff out of handler.h.
1570
                                      Tableop_hooks *hooks)
1 by brian
clean slate
1571
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1572
  Table tmp_table;		// Used during 'Create_field()'
1 by brian
clean slate
1573
  TABLE_SHARE share;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1574
  Table *table= 0;
482 by Brian Aker
Remove uint.
1575
  uint32_t select_field_count= items->elements;
1 by brian
clean slate
1576
  /* Add selected items to field list */
1577
  List_iterator_fast<Item> it(*items);
1578
  Item *item;
1579
  Field *tmp_field;
1580
  bool not_used;
1581
1582
  if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
1583
      create_table->table->db_stat)
1584
  {
1585
    /* Table already exists and was open at open_and_lock_tables() stage. */
1586
    if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1587
    {
1588
      create_info->table_existed= 1;		// Mark that table existed
520.1.22 by Brian Aker
Second pass of thd cleanup
1589
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
1590
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1591
                          create_table->table_name);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1592
      return(create_table->table);
1 by brian
clean slate
1593
    }
1594
1595
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1596
    return(0);
1 by brian
clean slate
1597
  }
1598
1599
  tmp_table.alias= 0;
1600
  tmp_table.timestamp_field= 0;
1601
  tmp_table.s= &share;
520.1.22 by Brian Aker
Second pass of thd cleanup
1602
  init_tmp_table_share(session, &share, "", 0, "", "");
1 by brian
clean slate
1603
1604
  tmp_table.s->db_create_options=0;
1605
  tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
1606
  tmp_table.s->db_low_byte_first= 
1607
        test(create_info->db_type == myisam_hton ||
1608
             create_info->db_type == heap_hton);
274 by Brian Aker
my_bool conversion in Table
1609
  tmp_table.null_row= false;
1610
  tmp_table.maybe_null= false;
1 by brian
clean slate
1611
1612
  while ((item=it++))
1613
  {
1614
    Create_field *cr_field;
1615
    Field *field, *def_field;
1616
    if (item->type() == Item::FUNC_ITEM)
1617
      if (item->result_type() != STRING_RESULT)
1618
        field= item->tmp_table_field(&tmp_table);
1619
      else
1620
        field= item->tmp_table_field_from_field_type(&tmp_table, 0);
1621
    else
520.1.22 by Brian Aker
Second pass of thd cleanup
1622
      field= create_tmp_field(session, &tmp_table, item, item->type(),
1 by brian
clean slate
1623
                              (Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0,
1624
                              0);
1625
    if (!field ||
1626
	!(cr_field=new Create_field(field,(item->type() == Item::FIELD_ITEM ?
1627
					   ((Item_field *)item)->field :
1628
					   (Field*) 0))))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1629
      return(0);
1 by brian
clean slate
1630
    if (item->maybe_null)
1631
      cr_field->flags &= ~NOT_NULL_FLAG;
1632
    alter_info->create_list.push_back(cr_field);
1633
  }
1634
1635
  /*
1636
    Create and lock table.
1637
1638
    Note that we either creating (or opening existing) temporary table or
1639
    creating base table on which name we have exclusive lock. So code below
1640
    should not cause deadlocks or races.
1641
1642
    We don't log the statement, it will be logged later.
1643
1644
    If this is a HEAP table, the automatic DELETE FROM which is written to the
1645
    binlog when a HEAP table is opened for the first time since startup, must
1646
    not be written: 1) it would be wrong (imagine we're in CREATE SELECT: we
1647
    don't want to delete from it) 2) it would be written before the CREATE
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1648
    Table, which is a wrong order. So we keep binary logging disabled when we
1 by brian
clean slate
1649
    open_table().
1650
  */
1651
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
1652
    tmp_disable_binlog(session);
1653
    if (!mysql_create_table_no_lock(session, create_table->db,
1 by brian
clean slate
1654
                                    create_table->table_name,
1655
                                    create_info, alter_info, 0,
496.1.5 by Paul McCullagh
PBXT needs to call mysql_create_table() to create a .frm file, but the LOCK_open is already held when the call is made
1656
                                    select_field_count, true))
1 by brian
clean slate
1657
    {
1658
      if (create_info->table_existed &&
1659
          !(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1660
      {
1661
        /*
1662
          This means that someone created table underneath server
1663
          or it was created via different mysqld front-end to the
1664
          cluster. We don't have much options but throw an error.
1665
        */
1666
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1667
        return(0);
1 by brian
clean slate
1668
      }
1669
1670
      if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1671
      {
398.1.10 by Monty Taylor
Actually removed VOID() this time.
1672
        pthread_mutex_lock(&LOCK_open);
520.1.22 by Brian Aker
Second pass of thd cleanup
1673
        if (reopen_name_locked_table(session, create_table, false))
1 by brian
clean slate
1674
        {
1675
          quick_rm_table(create_info->db_type, create_table->db,
1676
                         table_case_name(create_info, create_table->table_name),
1677
                         0);
1678
        }
1679
        else
1680
          table= create_table->table;
398.1.10 by Monty Taylor
Actually removed VOID() this time.
1681
        pthread_mutex_unlock(&LOCK_open);
1 by brian
clean slate
1682
      }
1683
      else
1684
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
1685
        if (!(table= open_table(session, create_table, (bool*) 0,
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1686
                                DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1 by brian
clean slate
1687
            !create_info->table_existed)
1688
        {
1689
          /*
1690
            This shouldn't happen as creation of temporary table should make
1691
            it preparable for open. But let us do close_temporary_table() here
1692
            just in case.
1693
          */
520.1.22 by Brian Aker
Second pass of thd cleanup
1694
          drop_temporary_table(session, create_table);
1 by brian
clean slate
1695
        }
1696
      }
1697
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1698
    reenable_binlog(session);
1 by brian
clean slate
1699
    if (!table)                                   // open failed
51.2.2 by Patrick Galbraith
Removed DBUGs from
1700
      return(0);
1 by brian
clean slate
1701
  }
1702
1703
  table->reginfo.lock_type=TL_WRITE;
1704
  hooks->prelock(&table, 1);                    // Call prelock hooks
520.1.22 by Brian Aker
Second pass of thd cleanup
1705
  if (! ((*lock)= mysql_lock_tables(session, &table, 1,
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1706
                                    DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)) ||
1 by brian
clean slate
1707
        hooks->postlock(&table, 1))
1708
  {
1709
    if (*lock)
1710
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1711
      mysql_unlock_tables(session, *lock);
1 by brian
clean slate
1712
      *lock= 0;
1713
    }
1714
1715
    if (!create_info->table_existed)
520.1.22 by Brian Aker
Second pass of thd cleanup
1716
      drop_open_table(session, table, create_table->db, create_table->table_name);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1717
    return(0);
1 by brian
clean slate
1718
  }
51.2.2 by Patrick Galbraith
Removed DBUGs from
1719
  return(table);
1 by brian
clean slate
1720
}
1721
1722
1723
int
1724
select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
1725
{
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
1726
  DRIZZLE_LOCK *extra_lock= NULL;
51.2.2 by Patrick Galbraith
Removed DBUGs from
1727
  
1 by brian
clean slate
1728
575.1.3 by Monty Taylor
Moved some stuff out of handler.h.
1729
  Tableop_hooks *hook_ptr= NULL;
1 by brian
clean slate
1730
  /*
1731
    For row-based replication, the CREATE-SELECT statement is written
1732
    in two pieces: the first one contain the CREATE TABLE statement
1733
    necessary to create the table and the second part contain the rows
1734
    that should go into the table.
1735
1736
    For non-temporary tables, the start of the CREATE-SELECT
1737
    implicitly commits the previous transaction, and all events
1738
    forming the statement will be stored the transaction cache. At end
1739
    of the statement, the entire statement is committed as a
1740
    transaction, and all events are written to the binary log.
1741
1742
    On the master, the table is locked for the duration of the
1743
    statement, but since the CREATE part is replicated as a simple
1744
    statement, there is no way to lock the table for accesses on the
1745
    slave.  Hence, we have to hold on to the CREATE part of the
1746
    statement until the statement has finished.
1747
   */
575.1.3 by Monty Taylor
Moved some stuff out of handler.h.
1748
  class MY_HOOKS : public Tableop_hooks {
1 by brian
clean slate
1749
  public:
327.2.4 by Brian Aker
Refactoring table.h
1750
    MY_HOOKS(select_create *x, TableList *create_table,
1751
             TableList *select_tables)
1 by brian
clean slate
1752
      : ptr(x), all_tables(*create_table)
1753
      {
1754
        all_tables.next_global= select_tables;
1755
      }
1756
1757
  private:
482 by Brian Aker
Remove uint.
1758
    virtual int do_postlock(Table **tables, uint32_t count)
1 by brian
clean slate
1759
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1760
      Session *session= const_cast<Session*>(ptr->get_session());
1761
      if (int error= decide_logging_format(session, &all_tables))
1 by brian
clean slate
1762
        return error;
1763
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1764
      Table const *const table = *tables;
520.1.22 by Brian Aker
Second pass of thd cleanup
1765
      if (session->current_stmt_binlog_row_based  &&
1 by brian
clean slate
1766
          !table->s->tmp_table &&
1767
          !ptr->get_create_info()->table_existed)
1768
      {
1769
        ptr->binlog_show_create_table(tables, count);
1770
      }
1771
      return 0;
1772
    }
1773
1774
    select_create *ptr;
327.2.4 by Brian Aker
Refactoring table.h
1775
    TableList all_tables;
1 by brian
clean slate
1776
  };
1777
1778
  MY_HOOKS hooks(this, create_table, select_tables);
1779
  hook_ptr= &hooks;
1780
1781
  unit= u;
1782
1783
  /*
1784
    Start a statement transaction before the create if we are using
1785
    row-based replication for the statement.  If we are creating a
1786
    temporary table, we need to start a statement transaction.
1787
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
1788
  if ((session->lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) == 0 &&
1789
      session->current_stmt_binlog_row_based)
1 by brian
clean slate
1790
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
1791
    session->binlog_start_trans_and_stmt();
1 by brian
clean slate
1792
  }
1793
520.1.22 by Brian Aker
Second pass of thd cleanup
1794
  if (!(table= create_table_from_items(session, create_info, create_table,
1 by brian
clean slate
1795
                                       alter_info, &values,
1796
                                       &extra_lock, hook_ptr)))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1797
    return(-1);				// abort() deletes table
1 by brian
clean slate
1798
1799
  if (extra_lock)
1800
  {
51.2.2 by Patrick Galbraith
Removed DBUGs from
1801
    assert(m_plock == NULL);
1 by brian
clean slate
1802
1803
    if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1804
      m_plock= &m_lock;
1805
    else
520.1.22 by Brian Aker
Second pass of thd cleanup
1806
      m_plock= &session->extra_lock;
1 by brian
clean slate
1807
1808
    *m_plock= extra_lock;
1809
  }
1810
1811
  if (table->s->fields < values.elements)
1812
  {
1813
    my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1814
    return(-1);
1 by brian
clean slate
1815
  }
1816
1817
 /* First field to copy */
1818
  field= table->field+table->s->fields - values.elements;
1819
1820
  /* Mark all fields that are given values */
1821
  for (Field **f= field ; *f ; f++)
1822
    bitmap_set_bit(table->write_set, (*f)->field_index);
1823
1824
  /* Don't set timestamp if used */
1825
  table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1826
  table->next_number_field=table->found_next_number_field;
1827
1828
  restore_record(table,s->default_values);      // Get empty record
520.1.22 by Brian Aker
Second pass of thd cleanup
1829
  session->cuted_fields=0;
1 by brian
clean slate
1830
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1831
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1832
  if (info.handle_duplicates == DUP_REPLACE)
1833
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
1834
  if (info.handle_duplicates == DUP_UPDATE)
1835
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
1836
  table->file->ha_start_bulk_insert((ha_rows) 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
1837
  session->abort_on_warning= !info.ignore;
1838
  if (check_that_all_fields_are_given_values(session, table, table_list))
51.2.2 by Patrick Galbraith
Removed DBUGs from
1839
    return(1);
1 by brian
clean slate
1840
  table->mark_columns_needed_for_insert();
1841
  table->file->extra(HA_EXTRA_WRITE_CACHE);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1842
  return(0);
1 by brian
clean slate
1843
}
1844
1845
void
482 by Brian Aker
Remove uint.
1846
select_create::binlog_show_create_table(Table **tables, uint32_t count)
1 by brian
clean slate
1847
{
1848
  /*
1849
    Note 1: In RBR mode, we generate a CREATE TABLE statement for the
1850
    created table by calling store_create_info() (behaves as SHOW
1851
    CREATE TABLE).  In the event of an error, nothing should be
1852
    written to the binary log, even if the table is non-transactional;
1853
    therefore we pretend that the generated CREATE TABLE statement is
1854
    for a transactional table.  The event will then be put in the
1855
    transaction cache, and any subsequent events (e.g., table-map
1856
    events and binrow events) will also be put there.  We can then use
1857
    ha_autocommit_or_rollback() to either throw away the entire
1858
    kaboodle of events, or write them to the binary log.
1859
1860
    We write the CREATE TABLE statement here and not in prepare()
1861
    since there potentially are sub-selects or accesses to information
1862
    schema that will do a close_thread_tables(), destroying the
1863
    statement transaction cache.
1864
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
1865
  assert(session->current_stmt_binlog_row_based);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1866
  assert(tables && *tables && count > 0);
1 by brian
clean slate
1867
1868
  char buf[2048];
1869
  String query(buf, sizeof(buf), system_charset_info);
1870
  int result;
327.2.4 by Brian Aker
Refactoring table.h
1871
  TableList tmp_table_list;
1 by brian
clean slate
1872
1873
  memset(&tmp_table_list, 0, sizeof(tmp_table_list));
1874
  tmp_table_list.table = *tables;
1875
  query.length(0);      // Have to zero it since constructor doesn't
1876
520.1.22 by Brian Aker
Second pass of thd cleanup
1877
  result= store_create_info(session, &tmp_table_list, &query, create_info);
51.2.2 by Patrick Galbraith
Removed DBUGs from
1878
  assert(result == 0); /* store_create_info() always return 0 */
1 by brian
clean slate
1879
520.1.22 by Brian Aker
Second pass of thd cleanup
1880
  session->binlog_query(Session::STMT_QUERY_TYPE,
1 by brian
clean slate
1881
                    query.ptr(), query.length(),
163 by Brian Aker
Merge Monty's code.
1882
                    /* is_trans */ true,
1883
                    /* suppress_use */ false);
1 by brian
clean slate
1884
}
1885
1886
void select_create::store_values(List<Item> &values)
1887
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1888
  fill_record(session, field, values, 1);
1 by brian
clean slate
1889
}
1890
1891
482 by Brian Aker
Remove uint.
1892
void select_create::send_error(uint32_t errcode,const char *err)
1 by brian
clean slate
1893
{
51.2.2 by Patrick Galbraith
Removed DBUGs from
1894
  
1 by brian
clean slate
1895
1896
  /*
1897
    This will execute any rollbacks that are necessary before writing
1898
    the transcation cache.
1899
1900
    We disable the binary log since nothing should be written to the
1901
    binary log.  This disabling is important, since we potentially do
1902
    a "roll back" of non-transactional tables by removing the table,
1903
    and the actual rollback might generate events that should not be
1904
    written to the binary log.
1905
1906
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
1907
  tmp_disable_binlog(session);
1 by brian
clean slate
1908
  select_insert::send_error(errcode, err);
520.1.22 by Brian Aker
Second pass of thd cleanup
1909
  reenable_binlog(session);
1 by brian
clean slate
1910
51.2.2 by Patrick Galbraith
Removed DBUGs from
1911
  return;
1 by brian
clean slate
1912
}
1913
1914
1915
bool select_create::send_eof()
1916
{
1917
  bool tmp=select_insert::send_eof();
1918
  if (tmp)
1919
    abort();
1920
  else
1921
  {
1922
    /*
1923
      Do an implicit commit at end of statement for non-temporary
1924
      tables.  This can fail, but we should unlock the table
1925
      nevertheless.
1926
    */
1927
    if (!table->s->tmp_table)
1928
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1929
      ha_autocommit_or_rollback(session, 0);
1930
      end_active_trans(session);
1 by brian
clean slate
1931
    }
1932
1933
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1934
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1935
    if (m_plock)
1936
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1937
      mysql_unlock_tables(session, *m_plock);
1 by brian
clean slate
1938
      *m_plock= NULL;
1939
      m_plock= NULL;
1940
    }
1941
  }
1942
  return tmp;
1943
}
1944
1945
1946
void select_create::abort()
1947
{
51.2.2 by Patrick Galbraith
Removed DBUGs from
1948
  
1 by brian
clean slate
1949
1950
  /*
1951
    In select_insert::abort() we roll back the statement, including
1952
    truncating the transaction cache of the binary log. To do this, we
1953
    pretend that the statement is transactional, even though it might
1954
    be the case that it was not.
1955
1956
    We roll back the statement prior to deleting the table and prior
1957
    to releasing the lock on the table, since there might be potential
1958
    for failure if the rollback is executed after the drop or after
1959
    unlocking the table.
1960
1961
    We also roll back the statement regardless of whether the creation
1962
    of the table succeeded or not, since we need to reset the binary
1963
    log state.
1964
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
1965
  tmp_disable_binlog(session);
1 by brian
clean slate
1966
  select_insert::abort();
520.1.22 by Brian Aker
Second pass of thd cleanup
1967
  session->transaction.stmt.modified_non_trans_table= false;
1968
  reenable_binlog(session);
1 by brian
clean slate
1969
1970
1971
  if (m_plock)
1972
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
1973
    mysql_unlock_tables(session, *m_plock);
1 by brian
clean slate
1974
    *m_plock= NULL;
1975
    m_plock= NULL;
1976
  }
1977
1978
  if (table)
1979
  {
1980
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1981
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1982
    if (!create_info->table_existed)
520.1.22 by Brian Aker
Second pass of thd cleanup
1983
      drop_open_table(session, table, create_table->db, create_table->table_name);
1 by brian
clean slate
1984
    table=0;                                    // Safety
1985
  }
51.2.2 by Patrick Galbraith
Removed DBUGs from
1986
  return;
1 by brian
clean slate
1987
}
1988
1989
1990
/*****************************************************************************
1991
  Instansiate templates
1992
*****************************************************************************/
1993
1994
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
1995
template class List_iterator_fast<List_item>;
1996
#endif /* HAVE_EXPLICIT_TEMPLATE_INSTANTIATION */