25 #include "sql_cache.h"
29 #include "sql_insert.h"
40 #include "sql_trigger.h"
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;
81 bool error,line_cuted,found_null,enclosed;
91 int escape,
bool get_it_from_net,
bool is_fifo);
94 int read_fixed_length(
void);
96 char unescape(
char chr);
97 int terminator(
const char *ptr,uint length);
98 bool find_start_of_fields();
101 int read_value(
int delim,
String *val);
103 int clear_level(
int level);
111 ::end_io_cache(&cache);
112 need_end_io_cache = 0;
120 void set_io_cache_arg(
void* arg) { cache.arg = arg; }
127 bool ignore_check_option_errors);
131 const String &enclosed, ulong skip_lines,
132 bool ignore_check_option_errors);
138 bool ignore_check_option_errors);
140 #ifndef EMBEDDED_LIBRARY
141 static bool write_execute_load_query_log_event(THD *thd, sql_exchange* ex,
143 const char* table_name_arg,
145 enum enum_duplicates duplicates,
147 bool transactional_table,
172 int mysql_load(THD *thd,sql_exchange *ex,
TABLE_LIST *table_list,
175 enum enum_duplicates handle_duplicates,
bool ignore,
176 bool read_file_from_client)
178 char name[FN_REFLEN];
182 const String *field_term= ex->field_term;
183 const String *escaped= ex->escaped;
184 const String *enclosed= ex->enclosed;
186 #ifndef EMBEDDED_LIBRARY
188 THD::killed_state killed_status= THD::NOT_KILLED;
191 char *db = table_list->db;
197 char *tdb= thd->db ? thd->db : db;
198 ulong skip_lines= ex->skip_lines;
199 bool transactional_table;
200 DBUG_ENTER(
"mysql_load");
208 thd->set_current_stmt_binlog_format_row_if_mixed();
210 #ifdef EMBEDDED_LIBRARY
211 read_file_from_client = 0;
214 if (escaped->length() > 1 || enclosed->length() > 1)
216 my_message(ER_WRONG_FIELD_TERMINATORS,ER(ER_WRONG_FIELD_TERMINATORS),
222 if (!escaped->is_ascii() || !enclosed->is_ascii() ||
223 !field_term->is_ascii() ||
224 !ex->line_term->is_ascii() || !ex->line_start->is_ascii())
226 push_warning(thd, Sql_condition::WARN_LEVEL_WARN,
227 WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED,
228 ER(WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED));
234 &thd->lex->select_lex.top_join_list,
236 &thd->lex->select_lex.leaf_tables, FALSE,
237 INSERT_ACL | UPDATE_ACL,
238 INSERT_ACL | UPDATE_ACL))
240 if (!table_list->table ||
241 !table_list->updatable ||
242 check_key_in_view(thd, table_list))
244 my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias,
"LOAD");
247 if (table_list->prepare_where(thd, 0, TRUE) ||
248 table_list->prepare_check_option(thd))
260 if (
unique_table(thd, table_list, table_list->next_global, 0))
262 my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name);
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);
272 if (!fields_vars.elements)
275 for (field=table->field; *field ; field++)
276 fields_vars.push_back(
new Item_field(*field));
277 bitmap_set_all(table->write_set);
283 set_fields, MARK_COLUMNS_WRITE, 0, 0) ||
284 setup_fields(thd,
Ref_ptr_array(), set_values, MARK_COLUMNS_READ, 0, 0))
291 fields_vars, MARK_COLUMNS_WRITE, 0, 0) ||
293 set_fields, MARK_COLUMNS_WRITE, 0, 0) ||
294 check_that_all_fields_are_given_values(thd, table, table_list))
297 if (setup_fields(thd,
Ref_ptr_array(), set_values, MARK_COLUMNS_READ, 0, 0))
301 const int escape_char= (escaped->length() && (ex->escaped_given() ||
302 !(thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)))
303 ? (*escaped)[0] : INT_MAX;
306 if (read_file_from_client && handle_duplicates == DUP_ERROR)
317 const bool manage_defaults= fields_vars.elements != 0;
318 COPY_INFO info(COPY_INFO::INSERT_OPERATION,
319 &fields_vars, &set_fields,
321 handle_duplicates, ignore, escape_char);
323 if (info.add_function_default_columns(table, table->write_set))
326 prepare_triggers_for_insert_stmt(table);
329 bool use_blobs= 0, use_vars= 0;
335 Item *real_item= item->real_item();
337 if (real_item->type() == Item::FIELD_ITEM)
340 if (field->flags & BLOB_FLAG)
346 tot_length+= field->field_length;
348 else if (item->type() == Item::STRING_ITEM)
351 if (use_blobs && !ex->line_term->length() && !field_term->length())
353 my_message(ER_BLOBS_AND_NO_TERMINATED,ER(ER_BLOBS_AND_NO_TERMINATED),
357 if (use_vars && !field_term->length() && !enclosed->length())
359 my_error(ER_LOAD_FROM_FIXED_SIZE_ROWS_TO_VAR, MYF(0));
363 #ifndef EMBEDDED_LIBRARY
364 if (read_file_from_client)
366 (void)net_request_file(&thd->net,ex->file_name);
372 #ifdef DONT_ALLOW_FULL_LOAD_DATA_PATHS
373 ex->file_name+=dirname_length(ex->file_name);
375 if (!dirname_length(ex->file_name))
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);
383 (void) fn_format(name, ex->file_name, mysql_real_data_home,
"",
384 MY_RELATIVE_PATH | MY_UNPACK_FILENAME |
385 MY_RETURN_REAL_PATH);
388 if (thd->slave_thread)
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))
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");
412 else if (!is_secure_file_path(name))
415 my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
"--secure-file-priv");
419 #if !defined(__WIN__) && ! defined(__NETWARE__)
421 if (!my_stat(name, &stat_info, MYF(MY_WME)))
425 if (!thd->slave_thread &&
426 !((stat_info.st_mode & S_IFLNK) != S_IFLNK &&
427 ((stat_info.st_mode & S_IFREG) == S_IFREG ||
428 (stat_info.st_mode & S_IFIFO) == S_IFIFO)))
430 my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), name);
433 if ((stat_info.st_mode & S_IFIFO) == S_IFIFO)
437 name, O_RDONLY, MYF(MY_WME))) < 0)
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);
453 #ifndef EMBEDDED_LIBRARY
454 if (mysql_bin_log.is_open())
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);
464 thd->count_cuted_fields= CHECK_FIELD_WARN;
465 thd->cuted_fields=0L;
467 if (ex->line_term->length() && ex->filetype != FILETYPE_XML)
470 while (skip_lines > 0)
473 if (read_info.next_line())
478 if (!(error=
test(read_info.error)))
481 table->next_number_field=table->found_next_number_field;
483 handle_duplicates == DUP_REPLACE)
484 table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
485 if (handle_duplicates == DUP_REPLACE &&
487 !table->triggers->has_delete_triggers()))
488 table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
489 if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
493 thd->abort_on_warning= (!ignore && thd->is_strict_mode());
495 if (ex->filetype == FILETYPE_XML)
496 error= read_xml_field(thd, info, table_list, fields_vars,
497 set_fields, set_values, read_info,
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,
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 &&
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;
521 thd->count_cuted_fields= CHECK_FIELD_IGNORE;
526 DBUG_EXECUTE_IF(
"simulate_kill_bug27571",
529 thd->killed= THD::KILL_QUERY;
532 #ifndef EMBEDDED_LIBRARY
533 killed_status= (error == 0) ? THD::NOT_KILLED : thd->killed;
540 query_cache_invalidate3(thd, table_list, 0);
543 if (read_file_from_client)
544 while (!read_info.next_line())
547 #ifndef EMBEDDED_LIBRARY
548 if (mysql_bin_log.is_open())
573 read_info.end_io_cache();
575 if (lf_info.wrote_create_file)
577 int errcode= query_error_code(thd, killed_status == THD::NOT_KILLED);
581 if (thd->transaction.stmt.cannot_safely_rollback())
582 (
void) write_execute_load_query_log_event(thd, ex,
584 table_list->table_name,
586 handle_duplicates, ignore,
598 error= -1; // Error on read
602 my_snprintf(name,
sizeof(name),
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());
608 #ifndef EMBEDDED_LIBRARY
609 if (mysql_bin_log.is_open())
619 if (thd->is_current_stmt_binlog_format_row())
620 error= thd->binlog_flush_pending_rows_event(TRUE, transactional_table);
628 read_info.end_io_cache();
629 if (lf_info.wrote_create_file)
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,
635 handle_duplicates, ignore,
645 error= error || mysql_bin_log.get_log_file()->error;
653 my_ok(thd, info.stats.copied + info.stats.deleted, 0L, name);
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;
665 #ifndef EMBEDDED_LIBRARY
668 static bool write_execute_load_query_log_event(THD *thd, sql_exchange* ex,
670 const char* table_name_arg,
672 enum enum_duplicates duplicates,
674 bool transactional_table,
677 char *load_data_query,
688 const char *tbl= table_name_arg;
689 const char *tdb= (thd->db != NULL ? thd->db : db_arg);
691 if (!thd->db || strcmp(db_arg, thd->db))
698 string_buf.set_charset(system_charset_info);
699 append_identifier(thd, &string_buf, db_arg, strlen(db_arg));
700 string_buf.append(
".");
702 append_identifier(thd, &string_buf, table_name_arg,
703 strlen(table_name_arg));
704 tbl= string_buf.c_ptr_safe();
706 duplicates, ignore, transactional_table);
711 if (thd->lex->local_file)
712 lle.set_fname_outside_temp_buf(ex->file_name, strlen(ex->file_name));
717 if (!thd->lex->field_list.is_empty())
721 pfields.append(
" (");
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()));
732 item->
print(&pfields, QT_ORDINARY);
737 if (!thd->lex->update_list.is_empty())
742 pfields.append(
" SET ");
749 pfields.append(
", ");
750 append_identifier(thd, &pfields, item->item_name.
ptr(),
751 strlen(item->item_name.
ptr()));
752 pfields.append((
char *)str->ptr());
756 p= pfields.c_ptr_safe();
759 if (!(load_data_query= (
char *)thd->alloc(lle.get_query_buffer_length() + 1 + pl)))
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);
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);
789 ulong skip_lines,
bool ignore_check_option_errors)
793 TABLE *table= table_list->table;
795 DBUG_ENTER(
"read_fixed_length");
797 while (!read_info.read_fixed_length())
801 thd->send_kill_message();
816 uchar *pos=read_info.row_start;
818 read_info.row_end[0]=0;
821 restore_record(table, s->default_values);
826 if (validate_default_values_of_unset_fields(thd, table))
838 Field *field= sql_field->field;
839 if (field == table->next_number_field)
840 table->auto_increment_field_not_null= TRUE;
846 field->set_notnull();
848 if (pos == read_info.row_end)
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())
865 if ((length=(uint) (read_info.row_end-pos)) >
867 length=field->field_length;
868 save_chr=pos[length]; pos[length]=
'\0';
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;
875 if (pos != read_info.row_end)
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());
885 fill_record_n_invoke_before_triggers(thd, set_fields, set_values,
886 ignore_check_option_errors,
891 switch (table_list->view_check_option(thd,
892 ignore_check_option_errors)) {
893 case VIEW_CHECK_SKIP:
894 read_info.next_line();
896 case VIEW_CHECK_ERROR:
900 err= write_record(thd, table, &info, NULL);
901 table->auto_increment_field_not_null= FALSE;
909 if (read_info.next_line())
911 if (read_info.line_cuted)
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());
919 thd->get_stmt_da()->inc_current_row_for_warning();
922 DBUG_RETURN(
test(read_info.error));
931 const String &enclosed, ulong skip_lines,
932 bool ignore_check_option_errors)
936 TABLE *table= table_list->table;
937 uint enclosed_length;
939 DBUG_ENTER(
"read_sep_field");
941 enclosed_length=enclosed.length();
947 thd->send_kill_message();
951 restore_record(table, s->default_values);
956 if (validate_default_values_of_unset_fields(thd, table))
968 if (read_info.read_field())
975 pos=read_info.row_start;
976 length=(uint) (read_info.row_end-pos);
978 real_item= item->real_item();
980 if ((!read_info.enclosed &&
981 (enclosed_length && length == 4 &&
982 !memcmp(pos, STRING_WITH_LEN(
"NULL")))) ||
983 (length == 1 && read_info.found_null))
986 if (real_item->type() == Item::FIELD_ITEM)
991 my_error(ER_WARN_NULL_TO_NOTNULL, MYF(0), field->field_name,
992 thd->get_stmt_da()->current_row_for_warning());
997 if (!field->maybe_null())
999 if (field->type() == FIELD_TYPE_TIMESTAMP)
1004 else if (field != table->next_number_field)
1005 field->
set_warning(Sql_condition::WARN_LEVEL_WARN,
1006 ER_WARN_NULL_TO_NOTNULL, 1);
1009 else if (item->type() == Item::STRING_ITEM)
1012 read_info.read_charset);
1016 my_error(ER_LOAD_DATA_INVALID_COLUMN, MYF(0), item->full_name());
1023 if (real_item->type() == Item::FIELD_ITEM)
1026 field->set_notnull();
1027 read_info.row_end[0]=0;
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);
1032 else if (item->type() == Item::STRING_ITEM)
1035 read_info.read_charset);
1039 my_error(ER_LOAD_DATA_INVALID_COLUMN, MYF(0), item->full_name());
1044 if (thd->is_error())
1047 if (read_info.error)
1057 if (item == fields_vars.head())
1059 for (; item ; item= it++)
1061 Item *real_item= item->real_item();
1062 if (real_item->type() == Item::FIELD_ITEM)
1071 my_error(ER_WARN_NULL_TO_NOTNULL, MYF(0),field->field_name,
1072 thd->get_stmt_da()->current_row_for_warning());
1075 if (field->type() == FIELD_TYPE_TIMESTAMP && !field->maybe_null())
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());
1090 else if (item->type() == Item::STRING_ITEM)
1093 read_info.read_charset);
1097 my_error(ER_LOAD_DATA_INVALID_COLUMN, MYF(0), item->full_name());
1104 fill_record_n_invoke_before_triggers(thd, set_fields, set_values,
1105 ignore_check_option_errors,
1110 switch (table_list->view_check_option(thd,
1111 ignore_check_option_errors)) {
1112 case VIEW_CHECK_SKIP:
1113 read_info.next_line();
1115 case VIEW_CHECK_ERROR:
1119 err= write_record(thd, table, &info, NULL);
1120 table->auto_increment_field_not_null= FALSE;
1127 if (read_info.next_line())
1129 if (read_info.line_cuted)
1131 thd->cuted_fields++;
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());
1138 thd->get_stmt_da()->inc_current_row_for_warning();
1141 DBUG_RETURN(
test(read_info.error));
1153 bool ignore_check_option_errors)
1157 TABLE *table= table_list->table;
1159 DBUG_ENTER(
"read_xml_field");
1161 for ( ; ; it.rewind())
1165 thd->send_kill_message();
1170 if (read_info.read_xml())
1178 DBUG_PRINT(
"read_xml_field", (
"skip_lines=%d", (
int) skip_lines));
1179 while ((tag= xmlit++))
1181 DBUG_PRINT(
"read_xml_field", (
"got tag:%i '%s' '%s'",
1182 tag->level, tag->field.c_ptr(),
1183 tag->value.c_ptr()));
1187 restore_record(table, s->default_values);
1192 if (validate_default_values_of_unset_fields(thd, table))
1198 while ((item= it++))
1208 while(tag && strcmp(tag->field.c_ptr(), item->item_name.
ptr()) != 0)
1213 if (item->type() == Item::FIELD_ITEM)
1218 if (field == table->next_number_field)
1219 table->auto_increment_field_not_null= TRUE;
1220 if (!field->maybe_null())
1222 if (field->type() == FIELD_TYPE_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);
1235 if (item->type() == Item::FIELD_ITEM)
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);
1246 (
char *) tag->value.ptr(),
1247 tag->value.length(), cs);
1250 if (read_info.error)
1262 if (item == fields_vars.head())
1265 for ( ; item; item= it++)
1267 if (item->type() == Item::FIELD_ITEM)
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());
1287 fill_record_n_invoke_before_triggers(thd, set_fields, set_values,
1288 ignore_check_option_errors,
1293 switch (table_list->view_check_option(thd,
1294 ignore_check_option_errors)) {
1295 case VIEW_CHECK_SKIP:
1296 read_info.next_line();
1298 case VIEW_CHECK_ERROR:
1302 if (write_record(thd, table, &info, NULL))
1309 thd->get_stmt_da()->inc_current_row_for_warning();
1312 DBUG_RETURN(
test(read_info.error) || thd->is_error());
1319 READ_INFO::unescape(
char chr)
1323 case 'n':
return '\n';
1324 case 't':
return '\t';
1325 case 'r':
return '\r';
1326 case 'b':
return '\b';
1328 case 'Z':
return '\032';
1329 case 'N': found_null=1;
1332 default:
return chr;
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,
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)
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();
1358 if (line_start.length() == 0)
1365 line_start_ptr=(
char*) line_start.ptr();
1366 line_start_end=line_start_ptr+line_start.length();
1370 if (field_term_length == line_term_length &&
1371 !memcmp(field_term_ptr,line_term_ptr,field_term_length))
1374 line_term_ptr=(
char*)
"";
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;
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);
1387 if (!(buffer=(uchar*) my_malloc(buff_length+1,MYF(0))))
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,
1408 need_end_io_cache = 1;
1410 #ifndef EMBEDDED_LIBRARY
1411 if (get_it_from_net)
1412 cache.read_function = _my_b_net_read;
1414 if (mysql_bin_log.is_open())
1415 cache.pre_read = cache.pre_close =
1416 (IO_CACHE_CALLBACK) log_loaded_block;
1423 READ_INFO::~READ_INFO()
1425 if (need_end_io_cache)
1426 ::end_io_cache(&cache);
1432 while ((t= xmlit++))
1437 #define GET (stack_pos != stack ? *--stack_pos : my_b_get(&cache))
1438 #define PUSH(A) *(stack_pos++)=(A)
1441 inline int READ_INFO::terminator(
const char *ptr,uint length)
1445 for (i=1 ; i < length ; i++)
1447 if ((chr=GET) != *++ptr)
1456 PUSH((uchar) *--ptr);
1461 int READ_INFO::read_field()
1463 int chr,found_enclosed_char;
1464 uchar *
to,*new_buffer;
1467 if (found_end_of_line)
1475 if (find_start_of_fields())
1478 if ((chr=GET) == my_b_EOF)
1480 found_end_of_line=eof=1;
1484 if (chr == enclosed_char)
1486 found_enclosed_char=enclosed_char;
1491 found_enclosed_char= INT_MAX;
1497 while ( to < end_of_buff)
1500 if (chr == my_b_EOF)
1502 if (chr == escape_char)
1504 if ((chr=GET) == my_b_EOF)
1506 *to++= (uchar) escape_char;
1516 if (escape_char != enclosed_char || chr == escape_char)
1518 *to++ = (uchar) unescape((
char) chr);
1524 #ifdef ALLOW_LINESEPARATOR_IN_STRINGS
1525 if (chr == line_term_char)
1527 if (chr == line_term_char && found_enclosed_char == INT_MAX)
1530 if (terminator(line_term_ptr,line_term_length))
1533 found_end_of_line=1;
1539 if (chr == found_enclosed_char)
1541 if ((chr=GET) == found_enclosed_char)
1543 *to++ = (uchar) chr;
1547 if (chr == my_b_EOF ||
1548 (chr == line_term_char && terminator(line_term_ptr,
1552 found_end_of_line=1;
1557 if (chr == field_term_char &&
1558 terminator(field_term_ptr,field_term_length))
1571 chr= found_enclosed_char;
1573 else if (chr == field_term_char && found_enclosed_char == INT_MAX)
1575 if (terminator(field_term_ptr,field_term_length))
1584 if (my_mbcharlen(read_charset, chr) > 1 &&
1585 to + my_mbcharlen(read_charset, chr) <= end_of_buff)
1587 uchar* p= (uchar*) to;
1591 ml= my_mbcharlen(read_charset, chr);
1593 for (i= 1; i < ml; i++)
1596 if (chr == my_b_EOF)
1607 if (my_ismbchar(read_charset,
1611 for (i= 0; i < ml; i++)
1612 PUSH((uchar) *--to);
1616 *to++ = (uchar) chr;
1621 if (!(new_buffer=(uchar*) my_realloc((
char*) buffer,buff_length+1+IO_SIZE,
1624 to=new_buffer + (to-buffer);
1626 buff_length+=IO_SIZE;
1627 end_of_buff=buffer+buff_length;
1632 found_end_of_line=eof=1;
1653 int READ_INFO::read_fixed_length()
1657 if (found_end_of_line)
1663 if (find_start_of_fields())
1667 to=row_start=buffer;
1668 while (to < end_of_buff)
1670 if ((chr=GET) == my_b_EOF)
1672 if (chr == escape_char)
1674 if ((chr=GET) == my_b_EOF)
1676 *to++= (uchar) escape_char;
1679 *to++ =(uchar) unescape((
char) chr);
1682 if (chr == line_term_char)
1684 if (terminator(line_term_ptr,line_term_length))
1686 found_end_of_line=1;
1691 *to++ = (uchar) chr;
1697 found_end_of_line=eof=1;
1700 return to == buffer ? 1 : 0;
1704 int READ_INFO::next_line()
1707 start_of_line= line_start_ptr != 0;
1708 if (found_end_of_line || eof)
1710 found_end_of_line=0;
1713 found_end_of_line=0;
1714 if (!line_term_length)
1720 if (chr == my_b_EOF)
1725 if (my_mbcharlen(read_charset, chr) > 1)
1728 chr != my_b_EOF && i<my_mbcharlen(read_charset, chr);
1731 if (chr == escape_char)
1735 if (chr == my_b_EOF)
1740 if (chr == escape_char)
1743 if (GET == my_b_EOF)
1747 if (chr == line_term_char && terminator(line_term_ptr,line_term_length))
1754 bool READ_INFO::find_start_of_fields()
1760 if ((chr=GET) == my_b_EOF)
1762 found_end_of_line=eof=1;
1765 }
while ((
char) chr != line_start_ptr[0]);
1766 for (
const char *ptr=line_start_ptr+1 ; ptr != line_start_end ; ptr++)
1769 if ((
char) chr != *ptr)
1772 while (--ptr != line_start_ptr)
1786 int READ_INFO::clear_level(
int level_arg)
1788 DBUG_ENTER(
"READ_INFO::read_xml clear_level");
1793 while ((tag= xmlit++))
1795 if(tag->level >= level_arg)
1810 my_xml_entity_to_char(
const char *name, uint length)
1814 if (!memcmp(name,
"gt", length))
1816 if (!memcmp(name,
"lt", length))
1819 else if (length == 3)
1821 if (!memcmp(name,
"amp", length))
1824 else if (length == 4)
1826 if (!memcmp(name,
"quot", length))
1828 if (!memcmp(name,
"apos", length))
1848 return (chr ==
'\t' || chr ==
'\r' || chr ==
'\n') ?
' ' : chr;
1855 int READ_INFO::read_value(
int delim,
String *val)
1860 for (chr= GET; my_tospace(chr) != delim && chr != my_b_EOF;)
1863 if (my_mbcharlen(read_charset, chr) > 1)
1865 DBUG_PRINT(
"read_xml",(
"multi byte"));
1866 int i, ml= my_mbcharlen(read_charset, chr);
1867 for (i= 1; i < ml; i++)
1875 if (chr == my_b_EOF)
1883 for (chr= my_tospace(GET) ; chr !=
';' ; chr= my_tospace(GET))
1885 if (chr == my_b_EOF)
1889 if ((chr= my_xml_entity_to_char(tmp.ptr(), tmp.length())) >= 0)
1902 return my_tospace(chr);
1911 int READ_INFO::read_xml()
1913 DBUG_ENTER(
"READ_INFO::read_xml");
1914 int chr, chr2, chr3;
1916 String tag, attribute, value;
1920 attribute.length(0);
1923 for (chr= my_tospace(GET); chr != my_b_EOF ; )
1928 chr= my_tospace(GET);
1934 if(chr2 ==
'-' && chr3 ==
'-')
1938 chr= my_tospace(GET);
1940 while(chr !=
'>' || chr2 !=
'-' || chr3 !=
'-')
1947 else if (chr2 ==
'-')
1952 chr= my_tospace(GET);
1953 if (chr == my_b_EOF)
1961 while(chr !=
'>' && chr !=
' ' && chr !=
'/' && chr != my_b_EOF)
1965 chr= my_tospace(GET);
1969 if((tag.length() == line_term_length -2) &&
1970 (strncmp(tag.c_ptr_safe(), line_term_ptr + 1, tag.length()) == 0))
1972 DBUG_PRINT(
"read_xml", (
"start-of-row: %i %s %s",
1973 level,tag.c_ptr_safe(), line_term_ptr));
1976 if(chr ==
' ' || chr ==
'>')
1979 clear_level(level + 1);
1990 chr= my_tospace(GET);
1995 while(chr !=
'=' && chr !=
'/' && chr !=
'>' && chr != my_b_EOF)
1997 attribute.append(chr);
1998 chr= my_tospace(GET);
2004 chr= read_value(
'<', &value);
2009 if(tag.length() > 0 && value.length() > 0)
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));
2017 attribute.length(0);
2022 chr= my_tospace(GET);
2025 while(chr !=
'>' && chr != my_b_EOF)
2028 chr= my_tospace(GET);
2031 if((tag.length() == line_term_length -2) &&
2032 (strncmp(tag.c_ptr_safe(), line_term_ptr + 1, tag.length()) == 0))
2034 DBUG_PRINT(
"read_xml", (
"found end-of-row %i %s",
2035 level, tag.c_ptr_safe()));
2038 chr= my_tospace(GET);
2043 if(!memcmp(tag.c_ptr_safe(), STRING_WITH_LEN(
"field")) &&
2044 !memcmp(attribute.c_ptr_safe(), STRING_WITH_LEN(
"name")))
2050 delim= my_tospace(GET);
2052 attribute.length(0);
2060 if (chr == my_b_EOF)
2062 if(chr ==
'"' || chr ==
'\'')
2072 chr= read_value(delim, &value);
2073 if(attribute.length() > 0 && value.length() > 0)
2075 DBUG_PRINT(
"read_xml", (
"lev:%i att:%s val:%s\n",
2077 attribute.c_ptr_safe(),
2078 value.c_ptr_safe()));
2079 taglist.push_front(
new XML_TAG(level + 1, attribute, value));
2081 attribute.length(0);
2084 chr= my_tospace(GET);
2088 chr= my_tospace(GET);
2093 DBUG_PRINT(
"read_xml",(
"Found eof"));