22#include <drizzled/sql_load.h>
23#include <drizzled/error.h>
24#include <drizzled/catalog/local.h>
25#include <drizzled/session.h>
26#include <drizzled/sql_base.h>
27#include <drizzled/field/epoch.h>
28#include <drizzled/internal/my_sys.h>
29#include <drizzled/internal/iocache.h>
30#include <drizzled/plugin/storage_engine.h>
31#include <drizzled/sql_lex.h>
32#include <drizzled/copy_info.h>
33#include <drizzled/file_exchange.h>
34#include <drizzled/util/test.h>
35#include <drizzled/session/transactions.h>
41#include <boost/filesystem.hpp>
43namespace fs=boost::filesystem;
50 unsigned char *buffer;
51 unsigned char *end_of_buff;
54 char *field_term_ptr,*line_term_ptr,*line_start_ptr,*line_start_end;
55 uint field_term_length,line_term_length,enclosed_length;
56 int field_term_char,line_term_char,enclosed_char,escape_char;
57 int *stack,*stack_pos;
58 bool found_end_of_line,start_of_line,eof;
59 bool need_end_io_cache;
63 bool error,line_cuted,found_null,enclosed;
64 unsigned char *row_start,
68 READ_INFO(
int cursor,
size_t tot_length,
const charset_info_st *
const cs,
70 String &enclosed,
int escape,
bool is_fifo);
73 int read_fixed_length(
void);
75 char unescape(
char chr);
76 int terminator(
char *ptr,uint32_t length);
77 bool find_start_of_fields();
86 need_end_io_cache = 0;
94 void set_io_cache_arg(
void* arg) { cache.arg = arg; }
101 bool ignore_check_option_errors);
105 String &enclosed, uint32_t skip_lines,
106 bool ignore_check_option_errors);
132 enum enum_duplicates handle_duplicates,
bool ignore)
137 String *field_term=ex->field_term,*escaped=ex->escaped;
138 String *enclosed=ex->enclosed;
141 assert(table_list->getSchemaName());
148 util::string::ptr schema(session->schema());
149 const char *tdb= (schema and not schema->empty()) ? schema->c_str() : table_list->getSchemaName();
151 uint32_t skip_lines= ex->skip_lines;
152 bool transactional_table;
155 if (escaped->length() > 4 || enclosed->length() > 4)
157 my_error(ER_WRONG_FIELD_TERMINATORS,MYF(0),enclosed->c_ptr(), enclosed->length());
164 if (setup_tables_and_check_access(session, &session->lex().select_lex.context,
165 &session->lex().select_lex.top_join_list,
167 &session->lex().select_lex.leaf_tables,
true))
178 if (unique_table(table_list, table_list->
next_global))
180 my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->getTableName());
184 table= table_list->
table;
185 transactional_table= table->
cursor->has_transactions();
187 if (!fields_vars.size())
190 for (field= table->getFields(); *field ; field++)
191 fields_vars.push_back(
new Item_field(*field));
192 table->setWriteSet();
193 table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
198 if (setup_fields(session, 0, set_fields, MARK_COLUMNS_WRITE, 0, 0) ||
199 setup_fields(session, 0, set_values, MARK_COLUMNS_READ, 0, 0))
205 if (setup_fields(session, 0, fields_vars, MARK_COLUMNS_WRITE, 0, 0) ||
206 setup_fields(session, 0, set_fields, MARK_COLUMNS_WRITE, 0, 0) ||
207 check_that_all_fields_are_given_values(session, table, table_list))
213 if (table->timestamp_field)
215 if (table->isWriteSet(table->timestamp_field->position()))
217 table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
221 table->setWriteSet(table->timestamp_field->position());
225 if (setup_fields(session, 0, set_values, MARK_COLUMNS_READ, 0, 0))
229 table->mark_columns_needed_for_insert();
232 bool use_blobs= 0, use_vars= 0;
233 List<Item>::iterator it(fields_vars.begin());
238 Item *real_item= item->real_item();
240 if (real_item->type() == Item::FIELD_ITEM)
243 if (field->flags & BLOB_FLAG)
251 else if (item->type() == Item::STRING_ITEM)
254 if (use_blobs && !ex->line_term->length() && !field_term->length())
256 my_message(ER_BLOBS_AND_NO_TERMINATED,ER(ER_BLOBS_AND_NO_TERMINATED),
260 if (use_vars && !field_term->length() && !enclosed->length())
262 my_error(ER_LOAD_FROM_FIXED_SIZE_ROWS_TO_VAR, MYF(0));
266 fs::path to_file(ex->file_name);
267 fs::path target_path(fs::system_complete(catalog::local_identifier().getPath()));
268 if (not to_file.has_root_directory())
270 int count_elements= 0;
271 for (fs::path::iterator iter= to_file.begin();
272 iter != to_file.end();
273 ++iter, ++count_elements)
276 if (count_elements == 1)
280 target_path /= to_file;
284 target_path= to_file;
287 if (not secure_file_priv.string().empty())
289 if (target_path.string().substr(0, secure_file_priv.string().size()) != secure_file_priv.string())
292 my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
"--secure-file-priv");
297 struct stat stat_info;
298 if (stat(target_path.string().c_str(), &stat_info))
300 my_error(ER_FILE_NOT_FOUND, MYF(0), target_path.string().c_str(), errno);
305 if (!((stat_info.st_mode & S_IROTH) == S_IROTH &&
306 (stat_info.st_mode & S_IFLNK) != S_IFLNK &&
307 ((stat_info.st_mode & S_IFREG) == S_IFREG ||
308 (stat_info.st_mode & S_IFIFO) == S_IFIFO)))
310 my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), target_path.string().c_str());
313 if ((stat_info.st_mode & S_IFIFO) == S_IFIFO)
317 if ((file=internal::my_open(target_path.string().c_str(), O_RDONLY,MYF(MY_WME))) < 0)
319 my_error(ER_CANT_OPEN_FILE, MYF(0), target_path.string().c_str(), errno);
324 info.handle_duplicates=handle_duplicates;
325 info.escape_char=escaped->length() ? (*escaped)[0] : INT_MAX;
330 ex->cs ? ex->cs : plugin::StorageEngine::getSchemaCollation(identifier),
331 *field_term, *ex->line_start, *ex->line_term, *enclosed,
332 info.escape_char, is_fifo);
336 internal::my_close(file,MYF(0));
348 session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
349 session->cuted_fields=0L;
351 if (ex->line_term->length())
354 while (skip_lines > 0)
357 if (read_info.next_line())
362 if (!(error=test(read_info.error)))
365 table->next_number_field=table->found_next_number_field;
367 handle_duplicates == DUP_REPLACE)
368 table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
369 if (handle_duplicates == DUP_REPLACE)
370 table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
371 table->cursor->ha_start_bulk_insert((ha_rows) 0);
374 session->setAbortOnWarning(
true);
376 if (!field_term->length() && !enclosed->length())
377 error= read_fixed_length(session, info, table_list, fields_vars,
378 set_fields, set_values, read_info,
381 error= read_sep_field(session, info, table_list, fields_vars,
382 set_fields, set_values, read_info,
383 *enclosed, skip_lines, ignore);
384 if (table->cursor->ha_end_bulk_insert() && !error)
386 table->print_error(errno, MYF(0));
389 table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
390 table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
391 table->next_number_field=0;
394 internal::my_close(file,MYF(0));
397 session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
406 snprintf(msg,
sizeof(msg), ER(ER_LOAD_INFO), info.records, info.deleted,
407 (info.records - info.copied), session->cuted_fields);
409 if (session->transaction.stmt.hasModifiedNonTransData())
410 session->transaction.all.markModifiedNonTransData();
412 session->my_ok(info.copied + info.deleted, 0, 0L, msg);
414 (void)(transactional_table);
415 assert(transactional_table || !(info.copied || info.deleted) ||
416 session->transaction.stmt.hasModifiedNonTransData());
417 table->cursor->ha_release_auto_increment();
418 table->auto_increment_field_not_null=
false;
419 session->setAbortOnWarning(
false);
433 uint32_t skip_lines,
bool ignore_check_option_errors)
435 List<Item>::iterator it(fields_vars.begin());
437 Table *table= table_list->table;
440 while (!read_info.read_fixed_length())
442 if (session->getKilled())
444 session->send_kill_message();
458 it= fields_vars.begin();
459 unsigned char *pos=read_info.row_start;
461 read_info.row_end[0]=0;
464 table->restoreRecordAsDefault();
471 Field *field= sql_field->field;
472 if (field == table->next_number_field)
473 table->auto_increment_field_not_null=
true;
479 field->set_notnull();
481 if (pos == read_info.row_end)
483 session->cuted_fields++;
484 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
485 ER_WARN_TOO_FEW_RECORDS,
486 ER(ER_WARN_TOO_FEW_RECORDS), session->row_count);
488 if (not field->maybe_null() and field->is_timestamp())
489 ((field::Epoch::pointer) field)->set_time();
494 unsigned char save_chr;
495 if ((length=(uint32_t) (read_info.row_end-pos)) >
498 length=field->field_length;
500 save_chr=pos[length];
502 field->store((
char*) pos,length,read_info.read_charset);
503 pos[length]=save_chr;
504 if ((pos+=length) > read_info.row_end)
505 pos= read_info.row_end;
508 if (pos != read_info.row_end)
510 session->cuted_fields++;
511 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
512 ER_WARN_TOO_MANY_RECORDS,
513 ER(ER_WARN_TOO_MANY_RECORDS), session->row_count);
516 if (session->getKilled() ||
517 fill_record(session, set_fields, set_values,
518 ignore_check_option_errors))
521 err= write_record(session, table, &info);
522 table->auto_increment_field_not_null=
false;
530 if (read_info.next_line())
532 if (read_info.line_cuted)
534 session->cuted_fields++;
535 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
536 ER_WARN_TOO_MANY_RECORDS,
537 ER(ER_WARN_TOO_MANY_RECORDS), session->row_count);
539 session->row_count++;
541 return(test(read_info.error));
550 String &enclosed, uint32_t skip_lines,
551 bool ignore_check_option_errors)
553 List<Item>::iterator it(fields_vars.begin());
555 Table *table= table_list->table;
556 uint32_t enclosed_length;
559 enclosed_length=enclosed.length();
561 for (;;it= fields_vars.begin())
563 if (session->getKilled())
565 session->send_kill_message();
569 table->restoreRecordAsDefault();
577 if (read_info.read_field())
584 pos=read_info.row_start;
585 length=(uint32_t) (read_info.row_end-pos);
587 real_item= item->real_item();
589 if ((!read_info.enclosed && (enclosed_length && length == 4 && !memcmp(pos, STRING_WITH_LEN(
"NULL")))) ||
590 (length == 1 && read_info.found_null))
593 if (real_item->type() == Item::FIELD_ITEM)
598 my_error(ER_WARN_NULL_TO_NOTNULL, MYF(0), field->field_name,
603 if (not field->maybe_null())
605 if (field->is_timestamp())
607 ((field::Epoch::pointer) field)->set_time();
609 else if (field != table->next_number_field)
611 field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_NULL_TO_NOTNULL, 1);
615 else if (item->type() == Item::STRING_ITEM)
618 read_info.read_charset);
622 my_error(ER_LOAD_DATA_INVALID_COLUMN, MYF(0), item->full_name());
629 if (real_item->type() == Item::FIELD_ITEM)
632 field->set_notnull();
633 read_info.row_end[0]=0;
634 if (field == table->next_number_field)
635 table->auto_increment_field_not_null=
true;
636 field->store((
char*) pos, length, read_info.read_charset);
638 else if (item->type() == Item::STRING_ITEM)
644 my_error(ER_LOAD_DATA_INVALID_COLUMN, MYF(0), item->full_name());
658 if (item == &fields_vars.front())
660 for (; item ; item= it++)
662 Item *real_item= item->real_item();
663 if (real_item->type() == Item::FIELD_ITEM)
668 my_error(ER_WARN_NULL_TO_NOTNULL, MYF(0),field->field_name,
672 if (not field->maybe_null() and field->is_timestamp())
673 ((field::Epoch::pointer) field)->set_time();
680 session->cuted_fields++;
681 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
682 ER_WARN_TOO_FEW_RECORDS,
683 ER(ER_WARN_TOO_FEW_RECORDS), session->row_count);
685 else if (item->type() == Item::STRING_ITEM)
688 read_info.read_charset);
692 my_error(ER_LOAD_DATA_INVALID_COLUMN, MYF(0), item->full_name());
698 if (session->getKilled() ||
699 fill_record(session, set_fields, set_values,
700 ignore_check_option_errors))
703 err= write_record(session, table, &info);
704 table->auto_increment_field_not_null=
false;
711 if (read_info.next_line())
713 if (read_info.line_cuted)
715 session->cuted_fields++;
716 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
717 ER_WARN_TOO_MANY_RECORDS, ER(ER_WARN_TOO_MANY_RECORDS),
719 if (session->getKilled())
722 session->row_count++;
724 return(test(read_info.error));
731READ_INFO::unescape(
char chr)
735 case 'n':
return '\n';
736 case 't':
return '\t';
737 case 'r':
return '\r';
738 case 'b':
return '\b';
740 case 'Z':
return '\032';
741 case 'N': found_null=1;
755READ_INFO::READ_INFO(
int file_par,
size_t tot_length,
758 String &enclosed_par,
int escape,
bool is_fifo)
759 :cursor(file_par),escape_char(escape)
762 field_term_ptr=(
char*) field_term.ptr();
763 field_term_length= field_term.length();
764 line_term_ptr=(
char*) line_term.ptr();
765 line_term_length= line_term.length();
766 if (line_start.length() == 0)
773 line_start_ptr=(char*) line_start.ptr();
774 line_start_end=line_start_ptr+line_start.length();
778 if (field_term_length == line_term_length &&
779 !memcmp(field_term_ptr,line_term_ptr,field_term_length))
782 line_term_ptr=(
char*)
"";
784 enclosed_char= (enclosed_length=enclosed_par.length()) ?
785 (
unsigned char) enclosed_par[0] : INT_MAX;
786 field_term_char= field_term_length ? (
unsigned char) field_term_ptr[0] : INT_MAX;
787 line_term_char= line_term_length ? (
unsigned char) line_term_ptr[0] : INT_MAX;
788 error=eof=found_end_of_line=found_null=line_cuted=0;
789 buff_length=tot_length;
793 size_t length= max(field_term_length,line_term_length)+1;
794 set_if_bigger(length, line_start.length());
795 stack= stack_pos= (
int*) memory::sql_alloc(
sizeof(
int)*length);
797 if (!(buffer=(
unsigned char*) calloc(1, buff_length+1)))
801 end_of_buff=buffer+buff_length;
802 if (cache.init_io_cache((
false) ? -1 : cursor, 0,
803 (
false) ? internal::READ_NET :
804 (is_fifo ? internal::READ_FIFO : internal::READ_CACHE),0L,1,
807 free((
unsigned char*) buffer);
817 need_end_io_cache = 1;
823READ_INFO::~READ_INFO()
827 if (need_end_io_cache)
828 cache.end_io_cache();
835#define GET (stack_pos != stack ? *--stack_pos : cache.get())
836#define PUSH(A) *(stack_pos++)=(A)
839inline int READ_INFO::terminator(
char *ptr,uint32_t length)
843 for (i=1 ; i < length ; i++)
845 if ((chr=GET) != *++ptr)
854 PUSH((
unsigned char) *--ptr);
859int READ_INFO::read_field()
861 int chr,found_enclosed_char;
862 unsigned char *to,*new_buffer;
865 if (found_end_of_line)
873 if (find_start_of_fields())
876 if ((chr=GET) == my_b_EOF)
878 found_end_of_line=eof=1;
882 if (chr == enclosed_char)
884 found_enclosed_char=enclosed_char;
885 *to++=(
unsigned char) chr;
889 found_enclosed_char= INT_MAX;
895 while ( to < end_of_buff)
898 if ((my_mbcharlen(read_charset, chr) > 1) &&
899 to+my_mbcharlen(read_charset, chr) <= end_of_buff)
901 unsigned char* p = (
unsigned char*)to;
903 int ml = my_mbcharlen(read_charset, chr);
905 for (i=1; i<ml; i++) {
911 if (my_ismbchar(read_charset,
916 PUSH((
unsigned char) *--to);
921 if (chr == escape_char)
923 if ((chr=GET) == my_b_EOF)
925 *to++= (
unsigned char) escape_char;
935 if (escape_char != enclosed_char || chr == escape_char)
937 *to++ = (
unsigned char) unescape((
char) chr);
943#ifdef ALLOW_LINESEPARATOR_IN_STRINGS
944 if (chr == line_term_char)
946 if (chr == line_term_char && found_enclosed_char == INT_MAX)
949 if (terminator(line_term_ptr,line_term_length))
958 if (chr == found_enclosed_char)
960 if ((chr=GET) == found_enclosed_char)
962 *to++ = (
unsigned char) chr;
966 if (chr == my_b_EOF ||
967 (chr == line_term_char && terminator(line_term_ptr, line_term_length)))
975 if (chr == field_term_char &&
976 terminator(field_term_ptr,field_term_length))
989 chr= found_enclosed_char;
991 else if (chr == field_term_char && found_enclosed_char == INT_MAX)
993 if (terminator(field_term_ptr,field_term_length))
1001 *to++ = (
unsigned char) chr;
1006 new_buffer=(
unsigned char*) realloc(buffer, buff_length+1+IO_SIZE);
1007 to=new_buffer + (to-buffer);
1009 buff_length+=IO_SIZE;
1010 end_of_buff=buffer+buff_length;
1015 found_end_of_line=eof=1;
1036int READ_INFO::read_fixed_length()
1040 if (found_end_of_line)
1046 if (find_start_of_fields())
1050 to=row_start=buffer;
1051 while (to < end_of_buff)
1053 if ((chr=GET) == my_b_EOF)
1055 if (chr == escape_char)
1057 if ((chr=GET) == my_b_EOF)
1059 *to++= (
unsigned char) escape_char;
1062 *to++ =(
unsigned char) unescape((
char) chr);
1065 if (chr == line_term_char)
1067 if (terminator(line_term_ptr,line_term_length))
1069 found_end_of_line=1;
1074 *to++ = (
unsigned char) chr;
1080 found_end_of_line=eof=1;
1083 return to == buffer ? 1 : 0;
1087int READ_INFO::next_line()
1090 start_of_line= line_start_ptr != 0;
1091 if (found_end_of_line || eof)
1093 found_end_of_line=0;
1096 found_end_of_line=0;
1097 if (!line_term_length)
1102 if (my_mbcharlen(read_charset, chr) > 1)
1105 chr != my_b_EOF && i<my_mbcharlen(read_charset, chr);
1108 if (chr == escape_char)
1111 if (chr == my_b_EOF)
1116 if (chr == escape_char)
1119 if (GET == my_b_EOF)
1123 if (chr == line_term_char && terminator(line_term_ptr,line_term_length))
1130bool READ_INFO::find_start_of_fields()
1136 if ((chr=GET) == my_b_EOF)
1138 found_end_of_line=eof=1;
1141 }
while ((
char) chr != line_start_ptr[0]);
1142 for (
char *ptr=line_start_ptr+1 ; ptr != line_start_end ; ptr++)
1145 if ((
char) chr != *ptr)
1148 while (--ptr != line_start_ptr)
1150 PUSH((
unsigned char) *ptr);
bool openTablesLock(TableList *)
Table * table
opened table
if(!referred_tab||referred_tab->join !=join)
TODO: Rename this file - func.h is stupid.