MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_load.cc
1 /*
2  Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 
18 /* Copy data from a textfile to table */
19 /* 2006-12 Erik Wetterberg : LOAD XML added */
20 
21 #include "sql_priv.h"
22 #include "unireg.h"
23 #include "sql_load.h"
24 #include "sql_load.h"
25 #include "sql_cache.h" // query_cache_*
26 #include "sql_base.h" // fill_record_n_invoke_before_triggers
27 #include <my_dir.h>
28 #include "sql_view.h" // check_key_in_view
29 #include "sql_insert.h" // check_that_all_fields_are_given_values,
30  // prepare_triggers_for_insert_stmt,
31  // write_record
32 #include "sql_acl.h" // INSERT_ACL, UPDATE_ACL
33 #include "log_event.h" // Delete_file_log_event,
34  // Execute_load_query_log_event,
35  // LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F
36 #include <m_ctype.h>
37 #include "rpl_mi.h"
38 #include "rpl_slave.h"
39 #include "sp_head.h"
40 #include "sql_trigger.h"
41 #include "sql_show.h"
42 #include <algorithm>
43 
44 using std::min;
45 using std::max;
46 
47 class XML_TAG {
48 public:
49  int level;
50  String field;
51  String value;
52  XML_TAG(int l, String f, String v);
53 };
54 
55 
56 XML_TAG::XML_TAG(int l, String f, String v)
57 {
58  level= l;
59  field.append(f);
60  value.append(v);
61 }
62 
63 
64 class READ_INFO {
65  File file;
66  uchar *buffer, /* Buffer for read text */
67  *end_of_buff; /* Data in bufferts ends here */
68  uint buff_length, /* Length of buffert */
69  max_length; /* Max length of row */
70  const char *field_term_ptr, *line_term_ptr, *line_start_ptr, *line_start_end;
71  uint field_term_length,line_term_length,enclosed_length;
72  int field_term_char,line_term_char,enclosed_char,escape_char;
73  int *stack,*stack_pos;
74  bool found_end_of_line,start_of_line,eof;
75  bool need_end_io_cache;
76  IO_CACHE cache;
77  NET *io_net;
78  int level; /* for load xml */
79 
80 public:
81  bool error,line_cuted,found_null,enclosed;
82  uchar *row_start, /* Found row starts here */
83  *row_end; /* Found row ends here */
84  const CHARSET_INFO *read_charset;
85 
86  READ_INFO(File file,uint tot_length,const CHARSET_INFO *cs,
87  const String &field_term,
88  const String &line_start,
89  const String &line_term,
90  const String &enclosed,
91  int escape,bool get_it_from_net, bool is_fifo);
92  ~READ_INFO();
93  int read_field();
94  int read_fixed_length(void);
95  int next_line(void);
96  char unescape(char chr);
97  int terminator(const char *ptr,uint length);
98  bool find_start_of_fields();
99  /* load xml */
100  List<XML_TAG> taglist;
101  int read_value(int delim, String *val);
102  int read_xml();
103  int clear_level(int level);
104 
105  /*
106  We need to force cache close before destructor is invoked to log
107  the last read block
108  */
109  void end_io_cache()
110  {
111  ::end_io_cache(&cache);
112  need_end_io_cache = 0;
113  }
114 
115  /*
116  Either this method, or we need to make cache public
117  Arg must be set from mysql_load() since constructor does not see
118  either the table or THD value
119  */
120  void set_io_cache_arg(void* arg) { cache.arg = arg; }
121 };
122 
123 static int read_fixed_length(THD *thd, COPY_INFO &info, TABLE_LIST *table_list,
124  List<Item> &fields_vars, List<Item> &set_fields,
125  List<Item> &set_values, READ_INFO &read_info,
126  ulong skip_lines,
127  bool ignore_check_option_errors);
128 static int read_sep_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list,
129  List<Item> &fields_vars, List<Item> &set_fields,
130  List<Item> &set_values, READ_INFO &read_info,
131  const String &enclosed, ulong skip_lines,
132  bool ignore_check_option_errors);
133 
134 static int read_xml_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list,
135  List<Item> &fields_vars, List<Item> &set_fields,
136  List<Item> &set_values, READ_INFO &read_info,
137  ulong skip_lines,
138  bool ignore_check_option_errors);
139 
140 #ifndef EMBEDDED_LIBRARY
141 static bool write_execute_load_query_log_event(THD *thd, sql_exchange* ex,
142  const char* db_arg, /* table's database */
143  const char* table_name_arg,
144  bool is_concurrent,
145  enum enum_duplicates duplicates,
146  bool ignore,
147  bool transactional_table,
148  int errocode);
149 #endif /* EMBEDDED_LIBRARY */
150 
151 /*
152  Execute LOAD DATA query
153 
154  SYNOPSYS
155  mysql_load()
156  thd - current thread
157  ex - sql_exchange object representing source file and its parsing rules
158  table_list - list of tables to which we are loading data
159  fields_vars - list of fields and variables to which we read
160  data from file
161  set_fields - list of fields mentioned in set clause
162  set_values - expressions to assign to fields in previous list
163  handle_duplicates - indicates whenever we should emit error or
164  replace row if we will meet duplicates.
165  ignore - - indicates whenever we should ignore duplicates
166  read_file_from_client - is this LOAD DATA LOCAL ?
167 
168  RETURN VALUES
169  TRUE - error / FALSE - success
170 */
171 
172 int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list,
173  List<Item> &fields_vars, List<Item> &set_fields,
174  List<Item> &set_values,
175  enum enum_duplicates handle_duplicates, bool ignore,
176  bool read_file_from_client)
177 {
178  char name[FN_REFLEN];
179  File file;
180  TABLE *table= NULL;
181  int error= 0;
182  const String *field_term= ex->field_term;
183  const String *escaped= ex->escaped;
184  const String *enclosed= ex->enclosed;
185  bool is_fifo=0;
186 #ifndef EMBEDDED_LIBRARY
187  LOAD_FILE_INFO lf_info;
188  THD::killed_state killed_status= THD::NOT_KILLED;
189  bool is_concurrent;
190 #endif
191  char *db = table_list->db; // This is never null
192  /*
193  If path for file is not defined, we will use the current database.
194  If this is not set, we will use the directory where the table to be
195  loaded is located
196  */
197  char *tdb= thd->db ? thd->db : db; // Result is never null
198  ulong skip_lines= ex->skip_lines;
199  bool transactional_table;
200  DBUG_ENTER("mysql_load");
201 
202  /*
203  Bug #34283
204  mysqlbinlog leaves tmpfile after termination if binlog contains
205  load data infile, so in mixed mode we go to row-based for
206  avoiding the problem.
207  */
208  thd->set_current_stmt_binlog_format_row_if_mixed();
209 
210 #ifdef EMBEDDED_LIBRARY
211  read_file_from_client = 0; //server is always in the same process
212 #endif
213 
214  if (escaped->length() > 1 || enclosed->length() > 1)
215  {
216  my_message(ER_WRONG_FIELD_TERMINATORS,ER(ER_WRONG_FIELD_TERMINATORS),
217  MYF(0));
218  DBUG_RETURN(TRUE);
219  }
220 
221  /* Report problems with non-ascii separators */
222  if (!escaped->is_ascii() || !enclosed->is_ascii() ||
223  !field_term->is_ascii() ||
224  !ex->line_term->is_ascii() || !ex->line_start->is_ascii())
225  {
226  push_warning(thd, Sql_condition::WARN_LEVEL_WARN,
227  WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED,
228  ER(WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED));
229  }
230 
231  if (open_and_lock_tables(thd, table_list, TRUE, 0))
232  DBUG_RETURN(TRUE);
233  if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
234  &thd->lex->select_lex.top_join_list,
235  table_list,
236  &thd->lex->select_lex.leaf_tables, FALSE,
237  INSERT_ACL | UPDATE_ACL,
238  INSERT_ACL | UPDATE_ACL))
239  DBUG_RETURN(-1);
240  if (!table_list->table || // do not suport join view
241  !table_list->updatable || // and derived tables
242  check_key_in_view(thd, table_list))
243  {
244  my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias, "LOAD");
245  DBUG_RETURN(TRUE);
246  }
247  if (table_list->prepare_where(thd, 0, TRUE) ||
248  table_list->prepare_check_option(thd))
249  {
250  DBUG_RETURN(TRUE);
251  }
252  /*
253  Let us emit an error if we are loading data to table which is used
254  in subselect in SET clause like we do it for INSERT.
255 
256  The main thing to fix to remove this restriction is to ensure that the
257  table is marked to be 'used for insert' in which case we should never
258  mark this table as 'const table' (ie, one that has only one row).
259  */
260  if (unique_table(thd, table_list, table_list->next_global, 0))
261  {
262  my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name);
263  DBUG_RETURN(TRUE);
264  }
265 
266  table= table_list->table;
267  transactional_table= table->file->has_transactions();
268 #ifndef EMBEDDED_LIBRARY
269  is_concurrent= (table_list->lock_type == TL_WRITE_CONCURRENT_INSERT);
270 #endif
271 
272  if (!fields_vars.elements)
273  {
274  Field **field;
275  for (field=table->field; *field ; field++)
276  fields_vars.push_back(new Item_field(*field));
277  bitmap_set_all(table->write_set);
278  /*
279  Let us also prepare SET clause, altough it is probably empty
280  in this case.
281  */
282  if (setup_fields(thd, Ref_ptr_array(),
283  set_fields, MARK_COLUMNS_WRITE, 0, 0) ||
284  setup_fields(thd, Ref_ptr_array(), set_values, MARK_COLUMNS_READ, 0, 0))
285  DBUG_RETURN(TRUE);
286  }
287  else
288  { // Part field list
289  /* TODO: use this conds for 'WITH CHECK OPTIONS' */
290  if (setup_fields(thd, Ref_ptr_array(),
291  fields_vars, MARK_COLUMNS_WRITE, 0, 0) ||
292  setup_fields(thd, Ref_ptr_array(),
293  set_fields, MARK_COLUMNS_WRITE, 0, 0) ||
294  check_that_all_fields_are_given_values(thd, table, table_list))
295  DBUG_RETURN(TRUE);
296  /* Fix the expressions in SET clause */
297  if (setup_fields(thd, Ref_ptr_array(), set_values, MARK_COLUMNS_READ, 0, 0))
298  DBUG_RETURN(TRUE);
299  }
300 
301  const int escape_char= (escaped->length() && (ex->escaped_given() ||
302  !(thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)))
303  ? (*escaped)[0] : INT_MAX;
304 
305  /* We can't give an error in the middle when using LOCAL files */
306  if (read_file_from_client && handle_duplicates == DUP_ERROR)
307  ignore= 1;
308 
309  /*
310  * LOAD DATA INFILE fff INTO TABLE xxx SET columns2
311  sets all columns, except if file's row lacks some: in that case,
312  defaults are set by read_fixed_length() and read_sep_field(),
313  not by COPY_INFO.
314  * LOAD DATA INFILE fff INTO TABLE xxx (columns1) SET columns2=
315  may need a default for columns other than columns1 and columns2.
316  */
317  const bool manage_defaults= fields_vars.elements != 0;
318  COPY_INFO info(COPY_INFO::INSERT_OPERATION,
319  &fields_vars, &set_fields,
320  manage_defaults,
321  handle_duplicates, ignore, escape_char);
322 
323  if (info.add_function_default_columns(table, table->write_set))
324  DBUG_RETURN(TRUE);
325 
326  prepare_triggers_for_insert_stmt(table);
327 
328  uint tot_length=0;
329  bool use_blobs= 0, use_vars= 0;
330  List_iterator_fast<Item> it(fields_vars);
331  Item *item;
332 
333  while ((item= it++))
334  {
335  Item *real_item= item->real_item();
336 
337  if (real_item->type() == Item::FIELD_ITEM)
338  {
339  Field *field= ((Item_field*)real_item)->field;
340  if (field->flags & BLOB_FLAG)
341  {
342  use_blobs= 1;
343  tot_length+= 256; // Will be extended if needed
344  }
345  else
346  tot_length+= field->field_length;
347  }
348  else if (item->type() == Item::STRING_ITEM)
349  use_vars= 1;
350  }
351  if (use_blobs && !ex->line_term->length() && !field_term->length())
352  {
353  my_message(ER_BLOBS_AND_NO_TERMINATED,ER(ER_BLOBS_AND_NO_TERMINATED),
354  MYF(0));
355  DBUG_RETURN(TRUE);
356  }
357  if (use_vars && !field_term->length() && !enclosed->length())
358  {
359  my_error(ER_LOAD_FROM_FIXED_SIZE_ROWS_TO_VAR, MYF(0));
360  DBUG_RETURN(TRUE);
361  }
362 
363 #ifndef EMBEDDED_LIBRARY
364  if (read_file_from_client)
365  {
366  (void)net_request_file(&thd->net,ex->file_name);
367  file = -1;
368  }
369  else
370 #endif
371  {
372 #ifdef DONT_ALLOW_FULL_LOAD_DATA_PATHS
373  ex->file_name+=dirname_length(ex->file_name);
374 #endif
375  if (!dirname_length(ex->file_name))
376  {
377  strxnmov(name, FN_REFLEN-1, mysql_real_data_home, tdb, NullS);
378  (void) fn_format(name, ex->file_name, name, "",
379  MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
380  }
381  else
382  {
383  (void) fn_format(name, ex->file_name, mysql_real_data_home, "",
384  MY_RELATIVE_PATH | MY_UNPACK_FILENAME |
385  MY_RETURN_REAL_PATH);
386  }
387 
388  if (thd->slave_thread)
389  {
390 #if defined(HAVE_REPLICATION) && !defined(MYSQL_CLIENT)
391  DBUG_ASSERT(active_mi != NULL);
392  if (strncmp(active_mi->rli->slave_patternload_file, name,
393  active_mi->rli->slave_patternload_file_size))
394  {
395  /*
396  LOAD DATA INFILE in the slave SQL Thread can only read from
397  --slave-load-tmpdir". This should never happen. Please, report a bug.
398  */
399 
400  sql_print_error("LOAD DATA INFILE in the slave SQL Thread can only read from --slave-load-tmpdir. " \
401  "Please, report a bug.");
402  my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--slave-load-tmpdir");
403  DBUG_RETURN(TRUE);
404  }
405 #else
406  /*
407  This is impossible and should never happen.
408  */
409  DBUG_ASSERT(FALSE);
410 #endif
411  }
412  else if (!is_secure_file_path(name))
413  {
414  /* Read only allowed from within dir specified by secure_file_priv */
415  my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
416  DBUG_RETURN(TRUE);
417  }
418 
419 #if !defined(__WIN__) && ! defined(__NETWARE__)
420  MY_STAT stat_info;
421  if (!my_stat(name, &stat_info, MYF(MY_WME)))
422  DBUG_RETURN(TRUE);
423 
424  // if we are not in slave thread, the file must be:
425  if (!thd->slave_thread &&
426  !((stat_info.st_mode & S_IFLNK) != S_IFLNK && // symlink
427  ((stat_info.st_mode & S_IFREG) == S_IFREG || // regular file
428  (stat_info.st_mode & S_IFIFO) == S_IFIFO))) // named pipe
429  {
430  my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), name);
431  DBUG_RETURN(TRUE);
432  }
433  if ((stat_info.st_mode & S_IFIFO) == S_IFIFO)
434  is_fifo= 1;
435 #endif
436  if ((file= mysql_file_open(key_file_load,
437  name, O_RDONLY, MYF(MY_WME))) < 0)
438 
439  DBUG_RETURN(TRUE);
440  }
441 
442  READ_INFO read_info(file,tot_length,
443  ex->cs ? ex->cs : thd->variables.collation_database,
444  *field_term,*ex->line_start, *ex->line_term, *enclosed,
445  info.escape_char, read_file_from_client, is_fifo);
446  if (read_info.error)
447  {
448  if (file >= 0)
449  mysql_file_close(file, MYF(0)); // no files in net reading
450  DBUG_RETURN(TRUE); // Can't allocate buffers
451  }
452 
453 #ifndef EMBEDDED_LIBRARY
454  if (mysql_bin_log.is_open())
455  {
456  lf_info.thd = thd;
457  lf_info.wrote_create_file = 0;
458  lf_info.last_pos_in_file = HA_POS_ERROR;
459  lf_info.log_delayed= transactional_table;
460  read_info.set_io_cache_arg((void*) &lf_info);
461  }
462 #endif
464  thd->count_cuted_fields= CHECK_FIELD_WARN; /* calc cuted fields */
465  thd->cuted_fields=0L;
466  /* Skip lines if there is a line terminator */
467  if (ex->line_term->length() && ex->filetype != FILETYPE_XML)
468  {
469  /* ex->skip_lines needs to be preserved for logging */
470  while (skip_lines > 0)
471  {
472  skip_lines--;
473  if (read_info.next_line())
474  break;
475  }
476  }
477 
478  if (!(error=test(read_info.error)))
479  {
480 
481  table->next_number_field=table->found_next_number_field;
482  if (ignore ||
483  handle_duplicates == DUP_REPLACE)
484  table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
485  if (handle_duplicates == DUP_REPLACE &&
486  (!table->triggers ||
487  !table->triggers->has_delete_triggers()))
488  table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
489  if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
490  table->file->ha_start_bulk_insert((ha_rows) 0);
491  table->copy_blobs=1;
492 
493  thd->abort_on_warning= (!ignore && thd->is_strict_mode());
494 
495  if (ex->filetype == FILETYPE_XML) /* load xml */
496  error= read_xml_field(thd, info, table_list, fields_vars,
497  set_fields, set_values, read_info,
498  skip_lines, ignore);
499  else if (!field_term->length() && !enclosed->length())
500  error= read_fixed_length(thd, info, table_list, fields_vars,
501  set_fields, set_values, read_info,
502  skip_lines, ignore);
503  else
504  error= read_sep_field(thd, info, table_list, fields_vars,
505  set_fields, set_values, read_info,
506  *enclosed, skip_lines, ignore);
507  if (thd->locked_tables_mode <= LTM_LOCK_TABLES &&
508  table->file->ha_end_bulk_insert() && !error)
509  {
510  table->file->print_error(my_errno, MYF(0));
511  error= 1;
512  }
513  table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
514  table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
515  table->next_number_field=0;
516  }
517  if (file >= 0)
518  mysql_file_close(file, MYF(0));
519  free_blobs(table); /* if pack_blob was used */
520  table->copy_blobs=0;
521  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
522  /*
523  simulated killing in the middle of per-row loop
524  must be effective for binlogging
525  */
526  DBUG_EXECUTE_IF("simulate_kill_bug27571",
527  {
528  error=1;
529  thd->killed= THD::KILL_QUERY;
530  };);
531 
532 #ifndef EMBEDDED_LIBRARY
533  killed_status= (error == 0) ? THD::NOT_KILLED : thd->killed;
534 #endif
535 
536  /*
537  We must invalidate the table in query cache before binlog writing and
538  ha_autocommit_...
539  */
540  query_cache_invalidate3(thd, table_list, 0);
541  if (error)
542  {
543  if (read_file_from_client)
544  while (!read_info.next_line())
545  ;
546 
547 #ifndef EMBEDDED_LIBRARY
548  if (mysql_bin_log.is_open())
549  {
550  {
551  /*
552  Make sure last block (the one which caused the error) gets
553  logged. This is needed because otherwise after write of (to
554  the binlog, not to read_info (which is a cache))
555  Delete_file_log_event the bad block will remain in read_info
556  (because pre_read is not called at the end of the last
557  block; remember pre_read is called whenever a new block is
558  read from disk). At the end of mysql_load(), the destructor
559  of read_info will call end_io_cache() which will flush
560  read_info, so we will finally have this in the binlog:
561 
562  Append_block # The last successfull block
563  Delete_file
564  Append_block # The failing block
565  which is nonsense.
566  Or could also be (for a small file)
567  Create_file # The failing block
568  which is nonsense (Delete_file is not written in this case, because:
569  Create_file has not been written, so Delete_file is not written, then
570  when read_info is destroyed end_io_cache() is called which writes
571  Create_file.
572  */
573  read_info.end_io_cache();
574  /* If the file was not empty, wrote_create_file is true */
575  if (lf_info.wrote_create_file)
576  {
577  int errcode= query_error_code(thd, killed_status == THD::NOT_KILLED);
578 
579  /* since there is already an error, the possible error of
580  writing binary log will be ignored */
581  if (thd->transaction.stmt.cannot_safely_rollback())
582  (void) write_execute_load_query_log_event(thd, ex,
583  table_list->db,
584  table_list->table_name,
585  is_concurrent,
586  handle_duplicates, ignore,
587  transactional_table,
588  errcode);
589  else
590  {
591  Delete_file_log_event d(thd, db, transactional_table);
592  (void) mysql_bin_log.write_event(&d);
593  }
594  }
595  }
596  }
597 #endif
598  error= -1; // Error on read
599  goto err;
600  }
601 
602  my_snprintf(name, sizeof(name),
603  ER(ER_LOAD_INFO),
604  (long) info.stats.records, (long) info.stats.deleted,
605  (long) (info.stats.records - info.stats.copied),
606  (long) thd->get_stmt_da()->current_statement_warn_count());
607 
608 #ifndef EMBEDDED_LIBRARY
609  if (mysql_bin_log.is_open())
610  {
611  /*
612  We need to do the job that is normally done inside
613  binlog_query() here, which is to ensure that the pending event
614  is written before tables are unlocked and before any other
615  events are written. We also need to update the table map
616  version for the binary log to mark that table maps are invalid
617  after this point.
618  */
619  if (thd->is_current_stmt_binlog_format_row())
620  error= thd->binlog_flush_pending_rows_event(TRUE, transactional_table);
621  else
622  {
623  /*
624  As already explained above, we need to call end_io_cache() or the last
625  block will be logged only after Execute_load_query_log_event (which is
626  wrong), when read_info is destroyed.
627  */
628  read_info.end_io_cache();
629  if (lf_info.wrote_create_file)
630  {
631  int errcode= query_error_code(thd, killed_status == THD::NOT_KILLED);
632  error= write_execute_load_query_log_event(thd, ex,
633  table_list->db, table_list->table_name,
634  is_concurrent,
635  handle_duplicates, ignore,
636  transactional_table,
637  errcode);
638  }
639 
640  /*
641  Flushing the IO CACHE while writing the execute load query log event
642  may result in error (for instance, because the max_binlog_size has been
643  reached, and rotation of the binary log failed).
644  */
645  error= error || mysql_bin_log.get_log_file()->error;
646  }
647  if (error)
648  goto err;
649  }
650 #endif
652  /* ok to client sent only after binlog write and engine commit */
653  my_ok(thd, info.stats.copied + info.stats.deleted, 0L, name);
654 err:
655  DBUG_ASSERT(transactional_table ||
656  !(info.stats.copied || info.stats.deleted) ||
657  thd->transaction.stmt.cannot_safely_rollback());
658  table->file->ha_release_auto_increment();
659  table->auto_increment_field_not_null= FALSE;
660  thd->abort_on_warning= 0;
661  DBUG_RETURN(error);
662 }
663 
664 
665 #ifndef EMBEDDED_LIBRARY
666 
667 /* Not a very useful function; just to avoid duplication of code */
668 static bool write_execute_load_query_log_event(THD *thd, sql_exchange* ex,
669  const char* db_arg, /* table's database */
670  const char* table_name_arg,
671  bool is_concurrent,
672  enum enum_duplicates duplicates,
673  bool ignore,
674  bool transactional_table,
675  int errcode)
676 {
677  char *load_data_query,
678  *end,
679  *fname_start,
680  *fname_end,
681  *p= NULL;
682  size_t pl= 0;
683  List<Item> fv;
684  Item *item;
685  String *str;
686  String pfield, pfields;
687  int n;
688  const char *tbl= table_name_arg;
689  const char *tdb= (thd->db != NULL ? thd->db : db_arg);
690  String string_buf;
691  if (!thd->db || strcmp(db_arg, thd->db))
692  {
693  /*
694  If used database differs from table's database,
695  prefix table name with database name so that it
696  becomes a FQ name.
697  */
698  string_buf.set_charset(system_charset_info);
699  append_identifier(thd, &string_buf, db_arg, strlen(db_arg));
700  string_buf.append(".");
701  }
702  append_identifier(thd, &string_buf, table_name_arg,
703  strlen(table_name_arg));
704  tbl= string_buf.c_ptr_safe();
705  Load_log_event lle(thd, ex, tdb, tbl, fv, is_concurrent,
706  duplicates, ignore, transactional_table);
707 
708  /*
709  force in a LOCAL if there was one in the original.
710  */
711  if (thd->lex->local_file)
712  lle.set_fname_outside_temp_buf(ex->file_name, strlen(ex->file_name));
713 
714  /*
715  prepare fields-list and SET if needed; print_query won't do that for us.
716  */
717  if (!thd->lex->field_list.is_empty())
718  {
719  List_iterator<Item> li(thd->lex->field_list);
720 
721  pfields.append(" (");
722  n= 0;
723 
724  while ((item= li++))
725  {
726  if (n++)
727  pfields.append(", ");
728  if (item->type() == Item::FIELD_ITEM)
729  append_identifier(thd, &pfields, item->item_name.ptr(),
730  strlen(item->item_name.ptr()));
731  else
732  item->print(&pfields, QT_ORDINARY);
733  }
734  pfields.append(")");
735  }
736 
737  if (!thd->lex->update_list.is_empty())
738  {
739  List_iterator<Item> lu(thd->lex->update_list);
740  List_iterator<String> ls(thd->lex->load_set_str_list);
741 
742  pfields.append(" SET ");
743  n= 0;
744 
745  while ((item= lu++))
746  {
747  str= ls++;
748  if (n++)
749  pfields.append(", ");
750  append_identifier(thd, &pfields, item->item_name.ptr(),
751  strlen(item->item_name.ptr()));
752  pfields.append((char *)str->ptr());
753  }
754  }
755 
756  p= pfields.c_ptr_safe();
757  pl= strlen(p);
758 
759  if (!(load_data_query= (char *)thd->alloc(lle.get_query_buffer_length() + 1 + pl)))
760  return TRUE;
761 
762  lle.print_query(FALSE, (const char *) ex->cs?ex->cs->csname:NULL,
763  load_data_query, &end,
764  (char **)&fname_start, (char **)&fname_end);
765 
766  strcpy(end, p);
767  end += pl;
768 
770  e(thd, load_data_query, end-load_data_query,
771  (uint) ((char*) fname_start - load_data_query - 1),
772  (uint) ((char*) fname_end - load_data_query),
773  (duplicates == DUP_REPLACE) ? LOAD_DUP_REPLACE :
774  (ignore ? LOAD_DUP_IGNORE : LOAD_DUP_ERROR),
775  transactional_table, FALSE, FALSE, errcode);
776  return mysql_bin_log.write_event(&e);
777 }
778 
779 #endif
780 
781 /****************************************************************************
782 ** Read of rows of fixed size + optional garage + optonal newline
783 ****************************************************************************/
784 
785 static int
786 read_fixed_length(THD *thd, COPY_INFO &info, TABLE_LIST *table_list,
787  List<Item> &fields_vars, List<Item> &set_fields,
788  List<Item> &set_values, READ_INFO &read_info,
789  ulong skip_lines, bool ignore_check_option_errors)
790 {
791  List_iterator_fast<Item> it(fields_vars);
792  Item_field *sql_field;
793  TABLE *table= table_list->table;
794  bool err;
795  DBUG_ENTER("read_fixed_length");
796 
797  while (!read_info.read_fixed_length())
798  {
799  if (thd->killed)
800  {
801  thd->send_kill_message();
802  DBUG_RETURN(1);
803  }
804  if (skip_lines)
805  {
806  /*
807  We could implement this with a simple seek if:
808  - We are not using DATA INFILE LOCAL
809  - escape character is ""
810  - line starting prefix is ""
811  */
812  skip_lines--;
813  continue;
814  }
815  it.rewind();
816  uchar *pos=read_info.row_start;
817 #ifdef HAVE_purify
818  read_info.row_end[0]=0;
819 #endif
820 
821  restore_record(table, s->default_values);
822  /*
823  Check whether default values of the fields not specified in column list
824  are correct or not.
825  */
826  if (validate_default_values_of_unset_fields(thd, table))
827  {
828  read_info.error= 1;
829  break;
830  }
831 
832  /*
833  There is no variables in fields_vars list in this format so
834  this conversion is safe.
835  */
836  while ((sql_field= (Item_field*) it++))
837  {
838  Field *field= sql_field->field;
839  if (field == table->next_number_field)
840  table->auto_increment_field_not_null= TRUE;
841  /*
842  No fields specified in fields_vars list can be null in this format.
843  Mark field as not null, we should do this for each row because of
844  restore_record...
845  */
846  field->set_notnull();
847 
848  if (pos == read_info.row_end)
849  {
850  thd->cuted_fields++; /* Not enough fields */
851  push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
852  ER_WARN_TOO_FEW_RECORDS,
853  ER(ER_WARN_TOO_FEW_RECORDS),
854  thd->get_stmt_da()->current_row_for_warning());
855  if (field->type() == FIELD_TYPE_TIMESTAMP && !field->maybe_null())
856  {
857  // Specific of TIMESTAMP NOT NULL: set to CURRENT_TIMESTAMP.
859  }
860  }
861  else
862  {
863  uint length;
864  uchar save_chr;
865  if ((length=(uint) (read_info.row_end-pos)) >
866  field->field_length)
867  length=field->field_length;
868  save_chr=pos[length]; pos[length]='\0'; // Safeguard aganst malloc
869  field->store((char*) pos,length,read_info.read_charset);
870  pos[length]=save_chr;
871  if ((pos+=length) > read_info.row_end)
872  pos= read_info.row_end; /* Fills rest with space */
873  }
874  }
875  if (pos != read_info.row_end)
876  {
877  thd->cuted_fields++; /* To long row */
878  push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
879  ER_WARN_TOO_MANY_RECORDS,
880  ER(ER_WARN_TOO_MANY_RECORDS),
881  thd->get_stmt_da()->current_row_for_warning());
882  }
883 
884  if (thd->killed ||
885  fill_record_n_invoke_before_triggers(thd, set_fields, set_values,
886  ignore_check_option_errors,
887  table->triggers,
888  TRG_EVENT_INSERT))
889  DBUG_RETURN(1);
890 
891  switch (table_list->view_check_option(thd,
892  ignore_check_option_errors)) {
893  case VIEW_CHECK_SKIP:
894  read_info.next_line();
895  goto continue_loop;
896  case VIEW_CHECK_ERROR:
897  DBUG_RETURN(-1);
898  }
899 
900  err= write_record(thd, table, &info, NULL);
901  table->auto_increment_field_not_null= FALSE;
902  if (err)
903  DBUG_RETURN(1);
904 
905  /*
906  We don't need to reset auto-increment field since we are restoring
907  its default value at the beginning of each loop iteration.
908  */
909  if (read_info.next_line()) // Skip to next line
910  break;
911  if (read_info.line_cuted)
912  {
913  thd->cuted_fields++; /* To long row */
914  push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
915  ER_WARN_TOO_MANY_RECORDS,
916  ER(ER_WARN_TOO_MANY_RECORDS),
917  thd->get_stmt_da()->current_row_for_warning());
918  }
919  thd->get_stmt_da()->inc_current_row_for_warning();
920 continue_loop:;
921  }
922  DBUG_RETURN(test(read_info.error));
923 }
924 
925 
926 
927 static int
928 read_sep_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list,
929  List<Item> &fields_vars, List<Item> &set_fields,
930  List<Item> &set_values, READ_INFO &read_info,
931  const String &enclosed, ulong skip_lines,
932  bool ignore_check_option_errors)
933 {
934  List_iterator_fast<Item> it(fields_vars);
935  Item *item;
936  TABLE *table= table_list->table;
937  uint enclosed_length;
938  bool err;
939  DBUG_ENTER("read_sep_field");
940 
941  enclosed_length=enclosed.length();
942 
943  for (;;it.rewind())
944  {
945  if (thd->killed)
946  {
947  thd->send_kill_message();
948  DBUG_RETURN(1);
949  }
950 
951  restore_record(table, s->default_values);
952  /*
953  Check whether default values of the fields not specified in column list
954  are correct or not.
955  */
956  if (validate_default_values_of_unset_fields(thd, table))
957  {
958  read_info.error= 1;
959  break;
960  }
961 
962  while ((item= it++))
963  {
964  uint length;
965  uchar *pos;
966  Item *real_item;
967 
968  if (read_info.read_field())
969  break;
970 
971  /* If this line is to be skipped we don't want to fill field or var */
972  if (skip_lines)
973  continue;
974 
975  pos=read_info.row_start;
976  length=(uint) (read_info.row_end-pos);
977 
978  real_item= item->real_item();
979 
980  if ((!read_info.enclosed &&
981  (enclosed_length && length == 4 &&
982  !memcmp(pos, STRING_WITH_LEN("NULL")))) ||
983  (length == 1 && read_info.found_null))
984  {
985 
986  if (real_item->type() == Item::FIELD_ITEM)
987  {
988  Field *field= ((Item_field *)real_item)->field;
989  if (field->reset()) // Set to 0
990  {
991  my_error(ER_WARN_NULL_TO_NOTNULL, MYF(0), field->field_name,
992  thd->get_stmt_da()->current_row_for_warning());
993  DBUG_RETURN(1);
994  }
995  // Try to set to NULL; if it fails, field remains at 0.
996  field->set_null();
997  if (!field->maybe_null())
998  {
999  if (field->type() == FIELD_TYPE_TIMESTAMP)
1000  {
1001  // Specific of TIMESTAMP NOT NULL: set to CURRENT_TIMESTAMP.
1003  }
1004  else if (field != table->next_number_field)
1005  field->set_warning(Sql_condition::WARN_LEVEL_WARN,
1006  ER_WARN_NULL_TO_NOTNULL, 1);
1007  }
1008  }
1009  else if (item->type() == Item::STRING_ITEM)
1010  {
1011  ((Item_user_var_as_out_param *)item)->set_null_value(
1012  read_info.read_charset);
1013  }
1014  else
1015  {
1016  my_error(ER_LOAD_DATA_INVALID_COLUMN, MYF(0), item->full_name());
1017  DBUG_RETURN(1);
1018  }
1019 
1020  continue;
1021  }
1022 
1023  if (real_item->type() == Item::FIELD_ITEM)
1024  {
1025  Field *field= ((Item_field *)real_item)->field;
1026  field->set_notnull();
1027  read_info.row_end[0]=0; // Safe to change end marker
1028  if (field == table->next_number_field)
1029  table->auto_increment_field_not_null= TRUE;
1030  field->store((char*) pos, length, read_info.read_charset);
1031  }
1032  else if (item->type() == Item::STRING_ITEM)
1033  {
1034  ((Item_user_var_as_out_param *)item)->set_value((char*) pos, length,
1035  read_info.read_charset);
1036  }
1037  else
1038  {
1039  my_error(ER_LOAD_DATA_INVALID_COLUMN, MYF(0), item->full_name());
1040  DBUG_RETURN(1);
1041  }
1042  }
1043 
1044  if (thd->is_error())
1045  read_info.error= 1;
1046 
1047  if (read_info.error)
1048  break;
1049  if (skip_lines)
1050  {
1051  skip_lines--;
1052  continue;
1053  }
1054  if (item)
1055  {
1056  /* Have not read any field, thus input file is simply ended */
1057  if (item == fields_vars.head())
1058  break;
1059  for (; item ; item= it++)
1060  {
1061  Item *real_item= item->real_item();
1062  if (real_item->type() == Item::FIELD_ITEM)
1063  {
1064  Field *field= ((Item_field *)real_item)->field;
1065  /*
1066  We set to 0. But if the field is DEFAULT NULL, the "null bit"
1067  turned on by restore_record() above remains so field will be NULL.
1068  */
1069  if (field->reset())
1070  {
1071  my_error(ER_WARN_NULL_TO_NOTNULL, MYF(0),field->field_name,
1072  thd->get_stmt_da()->current_row_for_warning());
1073  DBUG_RETURN(1);
1074  }
1075  if (field->type() == FIELD_TYPE_TIMESTAMP && !field->maybe_null())
1076  // Specific of TIMESTAMP NOT NULL: set to CURRENT_TIMESTAMP.
1078  /*
1079  QQ: We probably should not throw warning for each field.
1080  But how about intention to always have the same number
1081  of warnings in THD::cuted_fields (and get rid of cuted_fields
1082  in the end ?)
1083  */
1084  thd->cuted_fields++;
1085  push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1086  ER_WARN_TOO_FEW_RECORDS,
1087  ER(ER_WARN_TOO_FEW_RECORDS),
1088  thd->get_stmt_da()->current_row_for_warning());
1089  }
1090  else if (item->type() == Item::STRING_ITEM)
1091  {
1092  ((Item_user_var_as_out_param *)item)->set_null_value(
1093  read_info.read_charset);
1094  }
1095  else
1096  {
1097  my_error(ER_LOAD_DATA_INVALID_COLUMN, MYF(0), item->full_name());
1098  DBUG_RETURN(1);
1099  }
1100  }
1101  }
1102 
1103  if (thd->killed ||
1104  fill_record_n_invoke_before_triggers(thd, set_fields, set_values,
1105  ignore_check_option_errors,
1106  table->triggers,
1107  TRG_EVENT_INSERT))
1108  DBUG_RETURN(1);
1109 
1110  switch (table_list->view_check_option(thd,
1111  ignore_check_option_errors)) {
1112  case VIEW_CHECK_SKIP:
1113  read_info.next_line();
1114  goto continue_loop;
1115  case VIEW_CHECK_ERROR:
1116  DBUG_RETURN(-1);
1117  }
1118 
1119  err= write_record(thd, table, &info, NULL);
1120  table->auto_increment_field_not_null= FALSE;
1121  if (err)
1122  DBUG_RETURN(1);
1123  /*
1124  We don't need to reset auto-increment field since we are restoring
1125  its default value at the beginning of each loop iteration.
1126  */
1127  if (read_info.next_line()) // Skip to next line
1128  break;
1129  if (read_info.line_cuted)
1130  {
1131  thd->cuted_fields++; /* To long row */
1132  push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1133  ER_WARN_TOO_MANY_RECORDS, ER(ER_WARN_TOO_MANY_RECORDS),
1134  thd->get_stmt_da()->current_row_for_warning());
1135  if (thd->killed)
1136  DBUG_RETURN(1);
1137  }
1138  thd->get_stmt_da()->inc_current_row_for_warning();
1139 continue_loop:;
1140  }
1141  DBUG_RETURN(test(read_info.error));
1142 }
1143 
1144 
1145 /****************************************************************************
1146 ** Read rows in xml format
1147 ****************************************************************************/
1148 static int
1149 read_xml_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list,
1150  List<Item> &fields_vars, List<Item> &set_fields,
1151  List<Item> &set_values, READ_INFO &read_info,
1152  ulong skip_lines,
1153  bool ignore_check_option_errors)
1154 {
1155  List_iterator_fast<Item> it(fields_vars);
1156  Item *item;
1157  TABLE *table= table_list->table;
1158  const CHARSET_INFO *cs= read_info.read_charset;
1159  DBUG_ENTER("read_xml_field");
1160 
1161  for ( ; ; it.rewind())
1162  {
1163  if (thd->killed)
1164  {
1165  thd->send_kill_message();
1166  DBUG_RETURN(1);
1167  }
1168 
1169  // read row tag and save values into tag list
1170  if (read_info.read_xml())
1171  break;
1172 
1173  List_iterator_fast<XML_TAG> xmlit(read_info.taglist);
1174  xmlit.rewind();
1175  XML_TAG *tag= NULL;
1176 
1177 #ifndef DBUG_OFF
1178  DBUG_PRINT("read_xml_field", ("skip_lines=%d", (int) skip_lines));
1179  while ((tag= xmlit++))
1180  {
1181  DBUG_PRINT("read_xml_field", ("got tag:%i '%s' '%s'",
1182  tag->level, tag->field.c_ptr(),
1183  tag->value.c_ptr()));
1184  }
1185 #endif
1186 
1187  restore_record(table, s->default_values);
1188  /*
1189  Check whether default values of the fields not specified in column list
1190  are correct or not.
1191  */
1192  if (validate_default_values_of_unset_fields(thd, table))
1193  {
1194  read_info.error= 1;
1195  break;
1196  }
1197 
1198  while ((item= it++))
1199  {
1200  /* If this line is to be skipped we don't want to fill field or var */
1201  if (skip_lines)
1202  continue;
1203 
1204  /* find field in tag list */
1205  xmlit.rewind();
1206  tag= xmlit++;
1207 
1208  while(tag && strcmp(tag->field.c_ptr(), item->item_name.ptr()) != 0)
1209  tag= xmlit++;
1210 
1211  if (!tag) // found null
1212  {
1213  if (item->type() == Item::FIELD_ITEM)
1214  {
1215  Field *field= ((Item_field *) item)->field;
1216  field->reset();
1217  field->set_null();
1218  if (field == table->next_number_field)
1219  table->auto_increment_field_not_null= TRUE;
1220  if (!field->maybe_null())
1221  {
1222  if (field->type() == FIELD_TYPE_TIMESTAMP)
1223  // Specific of TIMESTAMP NOT NULL: set to CURRENT_TIMESTAMP.
1225  else if (field != table->next_number_field)
1226  field->set_warning(Sql_condition::WARN_LEVEL_WARN,
1227  ER_WARN_NULL_TO_NOTNULL, 1);
1228  }
1229  }
1230  else
1231  ((Item_user_var_as_out_param *) item)->set_null_value(cs);
1232  continue;
1233  }
1234 
1235  if (item->type() == Item::FIELD_ITEM)
1236  {
1237 
1238  Field *field= ((Item_field *)item)->field;
1239  field->set_notnull();
1240  if (field == table->next_number_field)
1241  table->auto_increment_field_not_null= TRUE;
1242  field->store((char *) tag->value.ptr(), tag->value.length(), cs);
1243  }
1244  else
1245  ((Item_user_var_as_out_param *) item)->set_value(
1246  (char *) tag->value.ptr(),
1247  tag->value.length(), cs);
1248  }
1249 
1250  if (read_info.error)
1251  break;
1252 
1253  if (skip_lines)
1254  {
1255  skip_lines--;
1256  continue;
1257  }
1258 
1259  if (item)
1260  {
1261  /* Have not read any field, thus input file is simply ended */
1262  if (item == fields_vars.head())
1263  break;
1264 
1265  for ( ; item; item= it++)
1266  {
1267  if (item->type() == Item::FIELD_ITEM)
1268  {
1269  /*
1270  QQ: We probably should not throw warning for each field.
1271  But how about intention to always have the same number
1272  of warnings in THD::cuted_fields (and get rid of cuted_fields
1273  in the end ?)
1274  */
1275  thd->cuted_fields++;
1276  push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1277  ER_WARN_TOO_FEW_RECORDS,
1278  ER(ER_WARN_TOO_FEW_RECORDS),
1279  thd->get_stmt_da()->current_row_for_warning());
1280  }
1281  else
1282  ((Item_user_var_as_out_param *)item)->set_null_value(cs);
1283  }
1284  }
1285 
1286  if (thd->killed ||
1287  fill_record_n_invoke_before_triggers(thd, set_fields, set_values,
1288  ignore_check_option_errors,
1289  table->triggers,
1290  TRG_EVENT_INSERT))
1291  DBUG_RETURN(1);
1292 
1293  switch (table_list->view_check_option(thd,
1294  ignore_check_option_errors)) {
1295  case VIEW_CHECK_SKIP:
1296  read_info.next_line();
1297  goto continue_loop;
1298  case VIEW_CHECK_ERROR:
1299  DBUG_RETURN(-1);
1300  }
1301 
1302  if (write_record(thd, table, &info, NULL))
1303  DBUG_RETURN(1);
1304 
1305  /*
1306  We don't need to reset auto-increment field since we are restoring
1307  its default value at the beginning of each loop iteration.
1308  */
1309  thd->get_stmt_da()->inc_current_row_for_warning();
1310  continue_loop:;
1311  }
1312  DBUG_RETURN(test(read_info.error) || thd->is_error());
1313 } /* load xml end */
1314 
1315 
1316 /* Unescape all escape characters, mark \N as null */
1317 
1318 char
1319 READ_INFO::unescape(char chr)
1320 {
1321  /* keep this switch synchornous with the ESCAPE_CHARS macro */
1322  switch(chr) {
1323  case 'n': return '\n';
1324  case 't': return '\t';
1325  case 'r': return '\r';
1326  case 'b': return '\b';
1327  case '0': return 0; // Ascii null
1328  case 'Z': return '\032'; // Win32 end of file
1329  case 'N': found_null=1;
1330 
1331  /* fall through */
1332  default: return chr;
1333  }
1334 }
1335 
1336 
1337 /*
1338  Read a line using buffering
1339  If last line is empty (in line mode) then it isn't outputed
1340 */
1341 
1342 
1343 READ_INFO::READ_INFO(File file_par, uint tot_length, const CHARSET_INFO *cs,
1344  const String &field_term,
1345  const String &line_start,
1346  const String &line_term,
1347  const String &enclosed_par,
1348  int escape, bool get_it_from_net, bool is_fifo)
1349  :file(file_par), buff_length(tot_length), escape_char(escape),
1350  found_end_of_line(false), eof(false), need_end_io_cache(false),
1351  error(false), line_cuted(false), found_null(false), read_charset(cs)
1352 {
1353  field_term_ptr= field_term.ptr();
1354  field_term_length= field_term.length();
1355  line_term_ptr= line_term.ptr();
1356  line_term_length= line_term.length();
1357  level= 0; /* for load xml */
1358  if (line_start.length() == 0)
1359  {
1360  line_start_ptr=0;
1361  start_of_line= 0;
1362  }
1363  else
1364  {
1365  line_start_ptr=(char*) line_start.ptr();
1366  line_start_end=line_start_ptr+line_start.length();
1367  start_of_line= 1;
1368  }
1369  /* If field_terminator == line_terminator, don't use line_terminator */
1370  if (field_term_length == line_term_length &&
1371  !memcmp(field_term_ptr,line_term_ptr,field_term_length))
1372  {
1373  line_term_length=0;
1374  line_term_ptr=(char*) "";
1375  }
1376  enclosed_char= (enclosed_length=enclosed_par.length()) ?
1377  (uchar) enclosed_par[0] : INT_MAX;
1378  field_term_char= field_term_length ? (uchar) field_term_ptr[0] : INT_MAX;
1379  line_term_char= line_term_length ? (uchar) line_term_ptr[0] : INT_MAX;
1380 
1381 
1382  /* Set of a stack for unget if long terminators */
1383  uint length= max(cs->mbmaxlen, max(field_term_length, line_term_length)) + 1;
1384  set_if_bigger(length,line_start.length());
1385  stack=stack_pos=(int*) sql_alloc(sizeof(int)*length);
1386 
1387  if (!(buffer=(uchar*) my_malloc(buff_length+1,MYF(0))))
1388  error=1; /* purecov: inspected */
1389  else
1390  {
1391  end_of_buff=buffer+buff_length;
1392  if (init_io_cache(&cache,(get_it_from_net) ? -1 : file, 0,
1393  (get_it_from_net) ? READ_NET :
1394  (is_fifo ? READ_FIFO : READ_CACHE),0L,1,
1395  MYF(MY_WME)))
1396  {
1397  my_free(buffer); /* purecov: inspected */
1398  buffer= NULL;
1399  error=1;
1400  }
1401  else
1402  {
1403  /*
1404  init_io_cache() will not initialize read_function member
1405  if the cache is READ_NET. So we work around the problem with a
1406  manual assignment
1407  */
1408  need_end_io_cache = 1;
1409 
1410 #ifndef EMBEDDED_LIBRARY
1411  if (get_it_from_net)
1412  cache.read_function = _my_b_net_read;
1413 
1414  if (mysql_bin_log.is_open())
1415  cache.pre_read = cache.pre_close =
1416  (IO_CACHE_CALLBACK) log_loaded_block;
1417 #endif
1418  }
1419  }
1420 }
1421 
1422 
1423 READ_INFO::~READ_INFO()
1424 {
1425  if (need_end_io_cache)
1426  ::end_io_cache(&cache);
1427 
1428  if (buffer != NULL)
1429  my_free(buffer);
1430  List_iterator<XML_TAG> xmlit(taglist);
1431  XML_TAG *t;
1432  while ((t= xmlit++))
1433  delete(t);
1434 }
1435 
1436 
1437 #define GET (stack_pos != stack ? *--stack_pos : my_b_get(&cache))
1438 #define PUSH(A) *(stack_pos++)=(A)
1439 
1440 
1441 inline int READ_INFO::terminator(const char *ptr,uint length)
1442 {
1443  int chr=0; // Keep gcc happy
1444  uint i;
1445  for (i=1 ; i < length ; i++)
1446  {
1447  if ((chr=GET) != *++ptr)
1448  {
1449  break;
1450  }
1451  }
1452  if (i == length)
1453  return 1;
1454  PUSH(chr);
1455  while (i-- > 1)
1456  PUSH((uchar) *--ptr);
1457  return 0;
1458 }
1459 
1460 
1461 int READ_INFO::read_field()
1462 {
1463  int chr,found_enclosed_char;
1464  uchar *to,*new_buffer;
1465 
1466  found_null=0;
1467  if (found_end_of_line)
1468  return 1; // One have to call next_line
1469 
1470  /* Skip until we find 'line_start' */
1471 
1472  if (start_of_line)
1473  { // Skip until line_start
1474  start_of_line=0;
1475  if (find_start_of_fields())
1476  return 1;
1477  }
1478  if ((chr=GET) == my_b_EOF)
1479  {
1480  found_end_of_line=eof=1;
1481  return 1;
1482  }
1483  to=buffer;
1484  if (chr == enclosed_char)
1485  {
1486  found_enclosed_char=enclosed_char;
1487  *to++=(uchar) chr; // If error
1488  }
1489  else
1490  {
1491  found_enclosed_char= INT_MAX;
1492  PUSH(chr);
1493  }
1494 
1495  for (;;)
1496  {
1497  while ( to < end_of_buff)
1498  {
1499  chr = GET;
1500  if (chr == my_b_EOF)
1501  goto found_eof;
1502  if (chr == escape_char)
1503  {
1504  if ((chr=GET) == my_b_EOF)
1505  {
1506  *to++= (uchar) escape_char;
1507  goto found_eof;
1508  }
1509  /*
1510  When escape_char == enclosed_char, we treat it like we do for
1511  handling quotes in SQL parsing -- you can double-up the
1512  escape_char to include it literally, but it doesn't do escapes
1513  like \n. This allows: LOAD DATA ... ENCLOSED BY '"' ESCAPED BY '"'
1514  with data like: "fie""ld1", "field2"
1515  */
1516  if (escape_char != enclosed_char || chr == escape_char)
1517  {
1518  *to++ = (uchar) unescape((char) chr);
1519  continue;
1520  }
1521  PUSH(chr);
1522  chr= escape_char;
1523  }
1524 #ifdef ALLOW_LINESEPARATOR_IN_STRINGS
1525  if (chr == line_term_char)
1526 #else
1527  if (chr == line_term_char && found_enclosed_char == INT_MAX)
1528 #endif
1529  {
1530  if (terminator(line_term_ptr,line_term_length))
1531  { // Maybe unexpected linefeed
1532  enclosed=0;
1533  found_end_of_line=1;
1534  row_start=buffer;
1535  row_end= to;
1536  return 0;
1537  }
1538  }
1539  if (chr == found_enclosed_char)
1540  {
1541  if ((chr=GET) == found_enclosed_char)
1542  { // Remove dupplicated
1543  *to++ = (uchar) chr;
1544  continue;
1545  }
1546  // End of enclosed field if followed by field_term or line_term
1547  if (chr == my_b_EOF ||
1548  (chr == line_term_char && terminator(line_term_ptr,
1549  line_term_length)))
1550  { // Maybe unexpected linefeed
1551  enclosed=1;
1552  found_end_of_line=1;
1553  row_start=buffer+1;
1554  row_end= to;
1555  return 0;
1556  }
1557  if (chr == field_term_char &&
1558  terminator(field_term_ptr,field_term_length))
1559  {
1560  enclosed=1;
1561  row_start=buffer+1;
1562  row_end= to;
1563  return 0;
1564  }
1565  /*
1566  The string didn't terminate yet.
1567  Store back next character for the loop
1568  */
1569  PUSH(chr);
1570  /* copy the found term character to 'to' */
1571  chr= found_enclosed_char;
1572  }
1573  else if (chr == field_term_char && found_enclosed_char == INT_MAX)
1574  {
1575  if (terminator(field_term_ptr,field_term_length))
1576  {
1577  enclosed=0;
1578  row_start=buffer;
1579  row_end= to;
1580  return 0;
1581  }
1582  }
1583 #ifdef USE_MB
1584  if (my_mbcharlen(read_charset, chr) > 1 &&
1585  to + my_mbcharlen(read_charset, chr) <= end_of_buff)
1586  {
1587  uchar* p= (uchar*) to;
1588  int ml, i;
1589  *to++ = chr;
1590 
1591  ml= my_mbcharlen(read_charset, chr);
1592 
1593  for (i= 1; i < ml; i++)
1594  {
1595  chr= GET;
1596  if (chr == my_b_EOF)
1597  {
1598  /*
1599  Need to back up the bytes already ready from illformed
1600  multi-byte char
1601  */
1602  to-= i;
1603  goto found_eof;
1604  }
1605  *to++ = chr;
1606  }
1607  if (my_ismbchar(read_charset,
1608  (const char *)p,
1609  (const char *)to))
1610  continue;
1611  for (i= 0; i < ml; i++)
1612  PUSH((uchar) *--to);
1613  chr= GET;
1614  }
1615 #endif
1616  *to++ = (uchar) chr;
1617  }
1618  /*
1619  ** We come here if buffer is too small. Enlarge it and continue
1620  */
1621  if (!(new_buffer=(uchar*) my_realloc((char*) buffer,buff_length+1+IO_SIZE,
1622  MYF(MY_WME))))
1623  return (error=1);
1624  to=new_buffer + (to-buffer);
1625  buffer=new_buffer;
1626  buff_length+=IO_SIZE;
1627  end_of_buff=buffer+buff_length;
1628  }
1629 
1630 found_eof:
1631  enclosed=0;
1632  found_end_of_line=eof=1;
1633  row_start=buffer;
1634  row_end=to;
1635  return 0;
1636 }
1637 
1638 /*
1639  Read a row with fixed length.
1640 
1641  NOTES
1642  The row may not be fixed size on disk if there are escape
1643  characters in the file.
1644 
1645  IMPLEMENTATION NOTE
1646  One can't use fixed length with multi-byte charset **
1647 
1648  RETURN
1649  0 ok
1650  1 error
1651 */
1652 
1653 int READ_INFO::read_fixed_length()
1654 {
1655  int chr;
1656  uchar *to;
1657  if (found_end_of_line)
1658  return 1; // One have to call next_line
1659 
1660  if (start_of_line)
1661  { // Skip until line_start
1662  start_of_line=0;
1663  if (find_start_of_fields())
1664  return 1;
1665  }
1666 
1667  to=row_start=buffer;
1668  while (to < end_of_buff)
1669  {
1670  if ((chr=GET) == my_b_EOF)
1671  goto found_eof;
1672  if (chr == escape_char)
1673  {
1674  if ((chr=GET) == my_b_EOF)
1675  {
1676  *to++= (uchar) escape_char;
1677  goto found_eof;
1678  }
1679  *to++ =(uchar) unescape((char) chr);
1680  continue;
1681  }
1682  if (chr == line_term_char)
1683  {
1684  if (terminator(line_term_ptr,line_term_length))
1685  { // Maybe unexpected linefeed
1686  found_end_of_line=1;
1687  row_end= to;
1688  return 0;
1689  }
1690  }
1691  *to++ = (uchar) chr;
1692  }
1693  row_end=to; // Found full line
1694  return 0;
1695 
1696 found_eof:
1697  found_end_of_line=eof=1;
1698  row_start=buffer;
1699  row_end=to;
1700  return to == buffer ? 1 : 0;
1701 }
1702 
1703 
1704 int READ_INFO::next_line()
1705 {
1706  line_cuted=0;
1707  start_of_line= line_start_ptr != 0;
1708  if (found_end_of_line || eof)
1709  {
1710  found_end_of_line=0;
1711  return eof;
1712  }
1713  found_end_of_line=0;
1714  if (!line_term_length)
1715  return 0; // No lines
1716  for (;;)
1717  {
1718  int chr = GET;
1719 #ifdef USE_MB
1720  if (chr == my_b_EOF)
1721  {
1722  eof= 1;
1723  return 1;
1724  }
1725  if (my_mbcharlen(read_charset, chr) > 1)
1726  {
1727  for (uint i=1;
1728  chr != my_b_EOF && i<my_mbcharlen(read_charset, chr);
1729  i++)
1730  chr = GET;
1731  if (chr == escape_char)
1732  continue;
1733  }
1734 #endif
1735  if (chr == my_b_EOF)
1736  {
1737  eof=1;
1738  return 1;
1739  }
1740  if (chr == escape_char)
1741  {
1742  line_cuted=1;
1743  if (GET == my_b_EOF)
1744  return 1;
1745  continue;
1746  }
1747  if (chr == line_term_char && terminator(line_term_ptr,line_term_length))
1748  return 0;
1749  line_cuted=1;
1750  }
1751 }
1752 
1753 
1754 bool READ_INFO::find_start_of_fields()
1755 {
1756  int chr;
1757  try_again:
1758  do
1759  {
1760  if ((chr=GET) == my_b_EOF)
1761  {
1762  found_end_of_line=eof=1;
1763  return 1;
1764  }
1765  } while ((char) chr != line_start_ptr[0]);
1766  for (const char *ptr=line_start_ptr+1 ; ptr != line_start_end ; ptr++)
1767  {
1768  chr=GET; // Eof will be checked later
1769  if ((char) chr != *ptr)
1770  { // Can't be line_start
1771  PUSH(chr);
1772  while (--ptr != line_start_ptr)
1773  { // Restart with next char
1774  PUSH((uchar) *ptr);
1775  }
1776  goto try_again;
1777  }
1778  }
1779  return 0;
1780 }
1781 
1782 
1783 /*
1784  Clear taglist from tags with a specified level
1785 */
1786 int READ_INFO::clear_level(int level_arg)
1787 {
1788  DBUG_ENTER("READ_INFO::read_xml clear_level");
1789  List_iterator<XML_TAG> xmlit(taglist);
1790  xmlit.rewind();
1791  XML_TAG *tag;
1792 
1793  while ((tag= xmlit++))
1794  {
1795  if(tag->level >= level_arg)
1796  {
1797  xmlit.remove();
1798  delete tag;
1799  }
1800  }
1801  DBUG_RETURN(0);
1802 }
1803 
1804 
1805 /*
1806  Convert an XML entity to Unicode value.
1807  Return -1 on error;
1808 */
1809 static int
1810 my_xml_entity_to_char(const char *name, uint length)
1811 {
1812  if (length == 2)
1813  {
1814  if (!memcmp(name, "gt", length))
1815  return '>';
1816  if (!memcmp(name, "lt", length))
1817  return '<';
1818  }
1819  else if (length == 3)
1820  {
1821  if (!memcmp(name, "amp", length))
1822  return '&';
1823  }
1824  else if (length == 4)
1825  {
1826  if (!memcmp(name, "quot", length))
1827  return '"';
1828  if (!memcmp(name, "apos", length))
1829  return '\'';
1830  }
1831  return -1;
1832 }
1833 
1834 
1845 static int
1846 my_tospace(int chr)
1847 {
1848  return (chr == '\t' || chr == '\r' || chr == '\n') ? ' ' : chr;
1849 }
1850 
1851 
1852 /*
1853  Read an xml value: handle multibyte and xml escape
1854 */
1855 int READ_INFO::read_value(int delim, String *val)
1856 {
1857  int chr;
1858  String tmp;
1859 
1860  for (chr= GET; my_tospace(chr) != delim && chr != my_b_EOF;)
1861  {
1862 #ifdef USE_MB
1863  if (my_mbcharlen(read_charset, chr) > 1)
1864  {
1865  DBUG_PRINT("read_xml",("multi byte"));
1866  int i, ml= my_mbcharlen(read_charset, chr);
1867  for (i= 1; i < ml; i++)
1868  {
1869  val->append(chr);
1870  /*
1871  Don't use my_tospace() in the middle of a multi-byte character
1872  TODO: check that the multi-byte sequence is valid.
1873  */
1874  chr= GET;
1875  if (chr == my_b_EOF)
1876  return chr;
1877  }
1878  }
1879 #endif
1880  if(chr == '&')
1881  {
1882  tmp.length(0);
1883  for (chr= my_tospace(GET) ; chr != ';' ; chr= my_tospace(GET))
1884  {
1885  if (chr == my_b_EOF)
1886  return chr;
1887  tmp.append(chr);
1888  }
1889  if ((chr= my_xml_entity_to_char(tmp.ptr(), tmp.length())) >= 0)
1890  val->append(chr);
1891  else
1892  {
1893  val->append('&');
1894  val->append(tmp);
1895  val->append(';');
1896  }
1897  }
1898  else
1899  val->append(chr);
1900  chr= GET;
1901  }
1902  return my_tospace(chr);
1903 }
1904 
1905 
1906 /*
1907  Read a record in xml format
1908  tags and attributes are stored in taglist
1909  when tag set in ROWS IDENTIFIED BY is closed, we are ready and return
1910 */
1911 int READ_INFO::read_xml()
1912 {
1913  DBUG_ENTER("READ_INFO::read_xml");
1914  int chr, chr2, chr3;
1915  int delim= 0;
1916  String tag, attribute, value;
1917  bool in_tag= false;
1918 
1919  tag.length(0);
1920  attribute.length(0);
1921  value.length(0);
1922 
1923  for (chr= my_tospace(GET); chr != my_b_EOF ; )
1924  {
1925  switch(chr){
1926  case '<': /* read tag */
1927  /* TODO: check if this is a comment <!-- comment --> */
1928  chr= my_tospace(GET);
1929  if(chr == '!')
1930  {
1931  chr2= GET;
1932  chr3= GET;
1933 
1934  if(chr2 == '-' && chr3 == '-')
1935  {
1936  chr2= 0;
1937  chr3= 0;
1938  chr= my_tospace(GET);
1939 
1940  while(chr != '>' || chr2 != '-' || chr3 != '-')
1941  {
1942  if(chr == '-')
1943  {
1944  chr3= chr2;
1945  chr2= chr;
1946  }
1947  else if (chr2 == '-')
1948  {
1949  chr2= 0;
1950  chr3= 0;
1951  }
1952  chr= my_tospace(GET);
1953  if (chr == my_b_EOF)
1954  goto found_eof;
1955  }
1956  break;
1957  }
1958  }
1959 
1960  tag.length(0);
1961  while(chr != '>' && chr != ' ' && chr != '/' && chr != my_b_EOF)
1962  {
1963  if(chr != delim) /* fix for the '<field name =' format */
1964  tag.append(chr);
1965  chr= my_tospace(GET);
1966  }
1967 
1968  // row tag should be in ROWS IDENTIFIED BY '<row>' - stored in line_term
1969  if((tag.length() == line_term_length -2) &&
1970  (strncmp(tag.c_ptr_safe(), line_term_ptr + 1, tag.length()) == 0))
1971  {
1972  DBUG_PRINT("read_xml", ("start-of-row: %i %s %s",
1973  level,tag.c_ptr_safe(), line_term_ptr));
1974  }
1975 
1976  if(chr == ' ' || chr == '>')
1977  {
1978  level++;
1979  clear_level(level + 1);
1980  }
1981 
1982  if (chr == ' ')
1983  in_tag= true;
1984  else
1985  in_tag= false;
1986  break;
1987 
1988  case ' ': /* read attribute */
1989  while(chr == ' ') /* skip blanks */
1990  chr= my_tospace(GET);
1991 
1992  if(!in_tag)
1993  break;
1994 
1995  while(chr != '=' && chr != '/' && chr != '>' && chr != my_b_EOF)
1996  {
1997  attribute.append(chr);
1998  chr= my_tospace(GET);
1999  }
2000  break;
2001 
2002  case '>': /* end tag - read tag value */
2003  in_tag= false;
2004  chr= read_value('<', &value);
2005  if(chr == my_b_EOF)
2006  goto found_eof;
2007 
2008  /* save value to list */
2009  if(tag.length() > 0 && value.length() > 0)
2010  {
2011  DBUG_PRINT("read_xml", ("lev:%i tag:%s val:%s",
2012  level,tag.c_ptr_safe(), value.c_ptr_safe()));
2013  taglist.push_front( new XML_TAG(level, tag, value));
2014  }
2015  tag.length(0);
2016  value.length(0);
2017  attribute.length(0);
2018  break;
2019 
2020  case '/': /* close tag */
2021  level--;
2022  chr= my_tospace(GET);
2023  if(chr != '>') /* if this is an empty tag <tag /> */
2024  tag.length(0); /* we should keep tag value */
2025  while(chr != '>' && chr != my_b_EOF)
2026  {
2027  tag.append(chr);
2028  chr= my_tospace(GET);
2029  }
2030 
2031  if((tag.length() == line_term_length -2) &&
2032  (strncmp(tag.c_ptr_safe(), line_term_ptr + 1, tag.length()) == 0))
2033  {
2034  DBUG_PRINT("read_xml", ("found end-of-row %i %s",
2035  level, tag.c_ptr_safe()));
2036  DBUG_RETURN(0); //normal return
2037  }
2038  chr= my_tospace(GET);
2039  break;
2040 
2041  case '=': /* attribute name end - read the value */
2042  //check for tag field and attribute name
2043  if(!memcmp(tag.c_ptr_safe(), STRING_WITH_LEN("field")) &&
2044  !memcmp(attribute.c_ptr_safe(), STRING_WITH_LEN("name")))
2045  {
2046  /*
2047  this is format <field name="xx">xx</field>
2048  where actual fieldname is in attribute
2049  */
2050  delim= my_tospace(GET);
2051  tag.length(0);
2052  attribute.length(0);
2053  chr= '<'; /* we pretend that it is a tag */
2054  level--;
2055  break;
2056  }
2057 
2058  //check for " or '
2059  chr= GET;
2060  if (chr == my_b_EOF)
2061  goto found_eof;
2062  if(chr == '"' || chr == '\'')
2063  {
2064  delim= chr;
2065  }
2066  else
2067  {
2068  delim= ' '; /* no delimiter, use space */
2069  PUSH(chr);
2070  }
2071 
2072  chr= read_value(delim, &value);
2073  if(attribute.length() > 0 && value.length() > 0)
2074  {
2075  DBUG_PRINT("read_xml", ("lev:%i att:%s val:%s\n",
2076  level + 1,
2077  attribute.c_ptr_safe(),
2078  value.c_ptr_safe()));
2079  taglist.push_front(new XML_TAG(level + 1, attribute, value));
2080  }
2081  attribute.length(0);
2082  value.length(0);
2083  if (chr != ' ')
2084  chr= my_tospace(GET);
2085  break;
2086 
2087  default:
2088  chr= my_tospace(GET);
2089  } /* end switch */
2090  } /* end while */
2091 
2092 found_eof:
2093  DBUG_PRINT("read_xml",("Found eof"));
2094  eof= 1;
2095  DBUG_RETURN(1);
2096 }