~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_load.cc

  • Committer: Brian Aker
  • Date: 2009-06-03 19:30:45 UTC
  • mfrom: (1046.1.6 merge)
  • Revision ID: brian@gaz-20090603193045-4xgeczyfixh07beg
MergeĀ forĀ Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/* Copy data from a textfile to table */
18
18
 
19
 
#include "config.h"
 
19
#include <drizzled/server_includes.h>
20
20
#include <drizzled/sql_load.h>
21
21
#include <drizzled/error.h>
22
22
#include <drizzled/data_home.h>
23
23
#include <drizzled/session.h>
24
24
#include <drizzled/sql_base.h>
25
25
#include <drizzled/field/timestamp.h>
26
 
#include "drizzled/internal/my_sys.h"
27
 
#include "drizzled/internal/iocache.h"
28
 
#include <drizzled/db.h>
29
 
 
30
 
#include <sys/stat.h>
31
 
#include <fcntl.h>
32
 
#include <algorithm>
33
 
#include <climits>
34
 
#include <boost/filesystem.hpp>
35
 
 
36
 
namespace fs=boost::filesystem;
37
 
using namespace std;
38
 
namespace drizzled
39
 
{
40
26
 
41
27
class READ_INFO {
42
 
  int   cursor;
 
28
  File  file;
43
29
  unsigned char *buffer;                /* Buffer for read text */
44
30
  unsigned char *end_of_buff;           /* Data in bufferts ends here */
45
31
  size_t buff_length;                   /* Length of buffert */
50
36
  int   *stack,*stack_pos;
51
37
  bool  found_end_of_line,start_of_line,eof;
52
38
  bool  need_end_io_cache;
53
 
  internal::IO_CACHE cache;
 
39
  IO_CACHE cache;
54
40
 
55
41
public:
56
42
  bool error,line_cuted,found_null,enclosed;
58
44
        *row_end;                       /* Found row ends here */
59
45
  const CHARSET_INFO *read_charset;
60
46
 
61
 
  READ_INFO(int cursor, size_t tot_length, const CHARSET_INFO * const cs,
 
47
  READ_INFO(File file, size_t tot_length, const CHARSET_INFO * const cs,
62
48
            String &field_term,String &line_start,String &line_term,
63
49
            String &enclosed,int escape, bool is_fifo);
64
50
  ~READ_INFO();
75
61
  */
76
62
  void end_io_cache()
77
63
  {
78
 
    cache.end_io_cache();
 
64
    ::end_io_cache(&cache);
79
65
    need_end_io_cache = 0;
80
66
  }
81
67
 
87
73
  void set_io_cache_arg(void* arg) { cache.arg = arg; }
88
74
};
89
75
 
90
 
static int read_fixed_length(Session *session, CopyInfo &info, TableList *table_list,
 
76
static int read_fixed_length(Session *session, COPY_INFO &info, TableList *table_list,
91
77
                             List<Item> &fields_vars, List<Item> &set_fields,
92
78
                             List<Item> &set_values, READ_INFO &read_info,
93
79
                             uint32_t skip_lines,
94
80
                             bool ignore_check_option_errors);
95
 
static int read_sep_field(Session *session, CopyInfo &info, TableList *table_list,
 
81
static int read_sep_field(Session *session, COPY_INFO &info, TableList *table_list,
96
82
                          List<Item> &fields_vars, List<Item> &set_fields,
97
83
                          List<Item> &set_values, READ_INFO &read_info,
98
84
                          String &enclosed, uint32_t skip_lines,
105
91
  SYNOPSYS
106
92
    mysql_load()
107
93
      session - current thread
108
 
      ex  - file_exchange object representing source cursor and its parsing rules
 
94
      ex  - file_exchange object representing source file and its parsing rules
109
95
      table_list  - list of tables to which we are loading data
110
96
      fields_vars - list of fields and variables to which we read
111
 
                    data from cursor
 
97
                    data from file
112
98
      set_fields  - list of fields mentioned in set clause
113
99
      set_values  - expressions to assign to fields in previous list
114
100
      handle_duplicates - indicates whenever we should emit error or
124
110
                List<Item> &set_values,
125
111
                enum enum_duplicates handle_duplicates, bool ignore)
126
112
{
127
 
  int file;
 
113
  char name[FN_REFLEN];
 
114
  File file;
128
115
  Table *table= NULL;
129
116
  int error;
130
117
  String *field_term=ex->field_term,*escaped=ex->escaped;
131
118
  String *enclosed=ex->enclosed;
132
119
  bool is_fifo=0;
133
 
 
134
 
  assert(table_list->getSchemaName()); // This should never be null
135
 
 
 
120
  char *db= table_list->db;                     // This is never null
 
121
  assert(db);
136
122
  /*
137
 
    If path for cursor is not defined, we will use the current database.
 
123
    If path for file is not defined, we will use the current database.
138
124
    If this is not set, we will use the directory where the table to be
139
125
    loaded is located
140
126
  */
141
 
  util::string::const_shared_ptr schema(session->schema());
142
 
  const char *tdb= (schema and not schema->empty()) ? schema->c_str() : table_list->getSchemaName(); // Result should never be null
 
127
  char *tdb= session->db ? session->db : db;            // Result is never null
143
128
  assert(tdb);
144
129
  uint32_t skip_lines= ex->skip_lines;
145
130
  bool transactional_table;
146
 
  Session::killed_state_t killed_status= Session::NOT_KILLED;
 
131
  Session::killed_state killed_status= Session::NOT_KILLED;
147
132
 
148
133
  /* Escape and enclosed character may be a utf8 4-byte character */
149
134
  if (escaped->length() > 4 || enclosed->length() > 4)
151
136
    my_error(ER_WRONG_FIELD_TERMINATORS,MYF(0),enclosed->c_ptr(), enclosed->length());
152
137
    return(true);
153
138
  }
154
 
 
155
 
  if (session->openTablesLock(table_list))
 
139
  if (open_and_lock_tables(session, table_list))
156
140
    return(true);
157
 
 
158
141
  if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
159
142
                                    &session->lex->select_lex.top_join_list,
160
143
                                    table_list,
169
152
    table is marked to be 'used for insert' in which case we should never
170
153
    mark this table as 'const table' (ie, one that has only one row).
171
154
  */
172
 
  if (unique_table(table_list, table_list->next_global))
 
155
  if (unique_table(session, table_list, table_list->next_global, 0))
173
156
  {
174
 
    my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->getTableName());
 
157
    my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name);
175
158
    return(true);
176
159
  }
177
160
 
178
161
  table= table_list->table;
179
 
  transactional_table= table->cursor->has_transactions();
 
162
  transactional_table= table->file->has_transactions();
180
163
 
181
164
  if (!fields_vars.elements)
182
165
  {
183
166
    Field **field;
184
 
    for (field= table->getFields(); *field ; field++)
 
167
    for (field=table->field; *field ; field++)
185
168
      fields_vars.push_back(new Item_field(*field));
186
169
    table->setWriteSet();
187
170
    table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
206
189
    */
207
190
    if (table->timestamp_field)
208
191
    {
209
 
      if (table->isWriteSet(table->timestamp_field->position()))
210
 
      {
 
192
      if (table->isWriteSet(table->timestamp_field->field_index))
211
193
        table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
212
 
      }
213
194
      else
214
195
      {
215
 
        table->setWriteSet(table->timestamp_field->position());
 
196
        table->setWriteSet(table->timestamp_field->field_index);
216
197
      }
217
198
    }
218
199
    /* Fix the expressions in SET clause */
257
238
    return(true);
258
239
  }
259
240
 
260
 
  fs::path to_file(ex->file_name);
261
 
  fs::path target_path(fs::system_complete(getDataHomeCatalog()));
262
 
  if (not to_file.has_root_directory())
263
 
  {
264
 
    int count_elements= 0;
265
 
    for (fs::path::iterator iter= to_file.begin();
266
 
         iter != to_file.end();
267
 
         ++iter, ++count_elements)
268
 
    { }
269
 
 
270
 
    if (count_elements == 1)
271
 
    {
272
 
      target_path /= tdb;
273
 
    }
274
 
    target_path /= to_file;
275
 
  }
276
 
  else
277
 
  {
278
 
    target_path= to_file;
279
 
  }
280
 
 
281
 
  if (not secure_file_priv.string().empty())
282
 
  {
283
 
    if (target_path.file_string().substr(0, secure_file_priv.file_string().size()) != secure_file_priv.file_string())
284
 
    {
285
 
      /* Read only allowed from within dir specified by secure_file_priv */
286
 
      my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
 
241
  {
 
242
#ifdef DONT_ALLOW_FULL_LOAD_DATA_PATHS
 
243
    ex->file_name+=dirname_length(ex->file_name);
 
244
#endif
 
245
    if (!dirname_length(ex->file_name))
 
246
    {
 
247
      strcpy(name, drizzle_real_data_home);
 
248
      strncat(name, tdb, FN_REFLEN-strlen(drizzle_real_data_home)-1);
 
249
      (void) fn_format(name, ex->file_name, name, "",
 
250
                       MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
 
251
    }
 
252
    else
 
253
    {
 
254
      (void) fn_format(name, ex->file_name, drizzle_real_data_home, "",
 
255
                       MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
 
256
 
 
257
      if (opt_secure_file_priv &&
 
258
          strncmp(opt_secure_file_priv, name, strlen(opt_secure_file_priv)))
 
259
      {
 
260
        /* Read only allowed from within dir specified by secure_file_priv */
 
261
        my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
 
262
        return(true);
 
263
      }
 
264
 
 
265
      struct stat stat_info;
 
266
      if (stat(name,&stat_info))
 
267
      {
 
268
        my_error(ER_FILE_NOT_FOUND, MYF(0), name, errno);
 
269
        return(true);
 
270
      }
 
271
 
 
272
      // if we are not in slave thread, the file must be:
 
273
      if (!((stat_info.st_mode & S_IROTH) == S_IROTH &&  // readable by others
 
274
            (stat_info.st_mode & S_IFLNK) != S_IFLNK && // and not a symlink
 
275
            ((stat_info.st_mode & S_IFREG) == S_IFREG ||
 
276
             (stat_info.st_mode & S_IFIFO) == S_IFIFO)))
 
277
      {
 
278
        my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), name);
 
279
        return(true);
 
280
      }
 
281
      if ((stat_info.st_mode & S_IFIFO) == S_IFIFO)
 
282
        is_fifo = 1;
 
283
    }
 
284
    if ((file=my_open(name,O_RDONLY,MYF(MY_WME))) < 0)
 
285
    {
 
286
      my_error(ER_CANT_OPEN_FILE, MYF(0), name, my_errno);
287
287
      return(true);
288
288
    }
289
289
  }
290
290
 
291
 
  struct stat stat_info;
292
 
  if (stat(target_path.file_string().c_str(), &stat_info))
293
 
  {
294
 
    my_error(ER_FILE_NOT_FOUND, MYF(0), target_path.file_string().c_str(), errno);
295
 
    return(true);
296
 
  }
297
 
 
298
 
  // if we are not in slave thread, the cursor must be:
299
 
  if (!((stat_info.st_mode & S_IROTH) == S_IROTH &&  // readable by others
300
 
        (stat_info.st_mode & S_IFLNK) != S_IFLNK && // and not a symlink
301
 
        ((stat_info.st_mode & S_IFREG) == S_IFREG ||
302
 
         (stat_info.st_mode & S_IFIFO) == S_IFIFO)))
303
 
  {
304
 
    my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), target_path.file_string().c_str());
305
 
    return(true);
306
 
  }
307
 
  if ((stat_info.st_mode & S_IFIFO) == S_IFIFO)
308
 
    is_fifo = 1;
309
 
 
310
 
 
311
 
  if ((file=internal::my_open(target_path.file_string().c_str(), O_RDONLY,MYF(MY_WME))) < 0)
312
 
  {
313
 
    my_error(ER_CANT_OPEN_FILE, MYF(0), target_path.file_string().c_str(), errno);
314
 
    return(true);
315
 
  }
316
 
  CopyInfo info;
 
291
  COPY_INFO info;
317
292
  memset(&info, 0, sizeof(info));
318
293
  info.ignore= ignore;
319
294
  info.handle_duplicates=handle_duplicates;
320
295
  info.escape_char=escaped->length() ? (*escaped)[0] : INT_MAX;
321
296
 
322
 
  SchemaIdentifier identifier(*schema);
323
297
  READ_INFO read_info(file, tot_length,
324
 
                      ex->cs ? ex->cs : plugin::StorageEngine::getSchemaCollation(identifier),
325
 
                      *field_term, *ex->line_start, *ex->line_term, *enclosed,
 
298
                      ex->cs ? ex->cs : get_default_db_collation(session->db),
 
299
                      *field_term,*ex->line_start, *ex->line_term, *enclosed,
326
300
                      info.escape_char, is_fifo);
327
301
  if (read_info.error)
328
302
  {
329
303
    if  (file >= 0)
330
 
      internal::my_close(file,MYF(0));                  // no files in net reading
 
304
      my_close(file,MYF(0));                    // no files in net reading
331
305
    return(true);                               // Can't allocate buffers
332
306
  }
333
307
 
359
333
    table->next_number_field=table->found_next_number_field;
360
334
    if (ignore ||
361
335
        handle_duplicates == DUP_REPLACE)
362
 
      table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
 
336
      table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
363
337
    if (handle_duplicates == DUP_REPLACE)
364
 
        table->cursor->extra(HA_EXTRA_WRITE_CAN_REPLACE);
365
 
    table->cursor->ha_start_bulk_insert((ha_rows) 0);
 
338
        table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
 
339
    table->file->ha_start_bulk_insert((ha_rows) 0);
366
340
    table->copy_blobs=1;
367
341
 
368
342
    session->abort_on_warning= true;
375
349
      error= read_sep_field(session, info, table_list, fields_vars,
376
350
                            set_fields, set_values, read_info,
377
351
                            *enclosed, skip_lines, ignore);
378
 
    if (table->cursor->ha_end_bulk_insert() && !error)
 
352
    if (table->file->ha_end_bulk_insert() && !error)
379
353
    {
380
 
      table->print_error(errno, MYF(0));
 
354
      table->file->print_error(my_errno, MYF(0));
381
355
      error= 1;
382
356
    }
383
 
    table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
384
 
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
 
357
    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
358
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
385
359
    table->next_number_field=0;
386
360
  }
387
361
  if (file >= 0)
388
 
    internal::my_close(file,MYF(0));
 
362
    my_close(file,MYF(0));
389
363
  free_blobs(table);                            /* if pack_blob was used */
390
364
  table->copy_blobs=0;
391
 
  session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
 
365
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
392
366
  /*
393
367
     simulated killing in the middle of per-row loop
394
368
     must be effective for binlogging
395
369
  */
396
 
  killed_status= (error == 0)? Session::NOT_KILLED : session->getKilled();
 
370
  killed_status= (error == 0)? Session::NOT_KILLED : session->killed;
397
371
  if (error)
398
372
  {
399
373
    error= -1;                          // Error on read
400
374
    goto err;
401
375
  }
402
 
 
403
 
  char msg[FN_REFLEN];
404
 
  snprintf(msg, sizeof(msg), ER(ER_LOAD_INFO), info.records, info.deleted,
405
 
           (info.records - info.copied), session->cuted_fields);
406
 
 
407
 
  if (session->transaction.stmt.hasModifiedNonTransData())
408
 
    session->transaction.all.markModifiedNonTransData();
 
376
  sprintf(name, ER(ER_LOAD_INFO), (uint32_t) info.records, (uint32_t) info.deleted,
 
377
          (uint32_t) (info.records - info.copied), (uint32_t) session->cuted_fields);
 
378
 
 
379
  if (session->transaction.stmt.modified_non_trans_table)
 
380
    session->transaction.all.modified_non_trans_table= true;
409
381
 
410
382
  /* ok to client sent only after binlog write and engine commit */
411
 
  session->my_ok(info.copied + info.deleted, 0, 0L, msg);
 
383
  session->my_ok(info.copied + info.deleted, 0L, name);
412
384
err:
413
385
  assert(transactional_table || !(info.copied || info.deleted) ||
414
 
              session->transaction.stmt.hasModifiedNonTransData());
415
 
  table->cursor->ha_release_auto_increment();
 
386
              session->transaction.stmt.modified_non_trans_table);
 
387
  table->file->ha_release_auto_increment();
416
388
  table->auto_increment_field_not_null= false;
417
389
  session->abort_on_warning= 0;
418
390
  return(error);
424
396
****************************************************************************/
425
397
 
426
398
static int
427
 
read_fixed_length(Session *session, CopyInfo &info, TableList *table_list,
 
399
read_fixed_length(Session *session, COPY_INFO &info, TableList *table_list,
428
400
                  List<Item> &fields_vars, List<Item> &set_fields,
429
401
                  List<Item> &set_values, READ_INFO &read_info,
430
402
                  uint32_t skip_lines, bool ignore_check_option_errors)
439
411
 
440
412
  while (!read_info.read_fixed_length())
441
413
  {
442
 
    if (session->getKilled())
 
414
    if (session->killed)
443
415
    {
444
416
      session->send_kill_message();
445
417
      return(1);
457
429
    }
458
430
    it.rewind();
459
431
    unsigned char *pos=read_info.row_start;
460
 
#ifdef HAVE_VALGRIND
 
432
#ifdef HAVE_purify
461
433
    read_info.row_end[0]=0;
462
434
#endif
463
435
 
512
484
                          ER(ER_WARN_TOO_MANY_RECORDS), session->row_count);
513
485
    }
514
486
 
515
 
    if (session->getKilled() ||
 
487
    if (session->killed ||
516
488
        fill_record(session, set_fields, set_values,
517
489
                    ignore_check_option_errors))
518
490
      return(1);
543
515
 
544
516
 
545
517
static int
546
 
read_sep_field(Session *session, CopyInfo &info, TableList *table_list,
 
518
read_sep_field(Session *session, COPY_INFO &info, TableList *table_list,
547
519
               List<Item> &fields_vars, List<Item> &set_fields,
548
520
               List<Item> &set_values, READ_INFO &read_info,
549
521
               String &enclosed, uint32_t skip_lines,
561
533
 
562
534
  for (;;it.rewind())
563
535
  {
564
 
    if (session->getKilled())
 
536
    if (session->killed)
565
537
    {
566
538
      session->send_kill_message();
567
539
      return(1);
653
625
    }
654
626
    if (item)
655
627
    {
656
 
      /* Have not read any field, thus input cursor is simply ended */
 
628
      /* Have not read any field, thus input file is simply ended */
657
629
      if (item == fields_vars.head())
658
630
        break;
659
631
      for (; item ; item= it++)
694
666
      }
695
667
    }
696
668
 
697
 
    if (session->getKilled() ||
 
669
    if (session->killed ||
698
670
        fill_record(session, set_fields, set_values,
699
671
                    ignore_check_option_errors))
700
672
      return(1);
715
687
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
716
688
                          ER_WARN_TOO_MANY_RECORDS, ER(ER_WARN_TOO_MANY_RECORDS),
717
689
                          session->row_count);
718
 
      if (session->getKilled())
 
690
      if (session->killed)
719
691
        return(1);
720
692
    }
721
693
    session->row_count++;
736
708
  case 'r': return '\r';
737
709
  case 'b': return '\b';
738
710
  case '0': return 0;                           // Ascii null
739
 
  case 'Z': return '\032';                      // Win32 end of cursor
 
711
  case 'Z': return '\032';                      // Win32 end of file
740
712
  case 'N': found_null=1;
741
713
 
742
714
    /* fall through */
751
723
*/
752
724
 
753
725
 
754
 
READ_INFO::READ_INFO(int file_par, size_t tot_length,
 
726
READ_INFO::READ_INFO(File file_par, size_t tot_length,
755
727
                     const CHARSET_INFO * const cs,
756
728
                     String &field_term, String &line_start, String &line_term,
757
729
                     String &enclosed_par, int escape, bool is_fifo)
758
 
  :cursor(file_par),escape_char(escape)
 
730
  :file(file_par),escape_char(escape)
759
731
{
760
732
  read_charset= cs;
761
733
  field_term_ptr=(char*) field_term.ptr();
789
761
 
790
762
 
791
763
  /* Set of a stack for unget if long terminators */
792
 
  size_t length= max(field_term_length,line_term_length)+1;
793
 
  set_if_bigger(length, line_start.length());
794
 
  stack= stack_pos= (int*) memory::sql_alloc(sizeof(int)*length);
 
764
  uint32_t length=cmax(field_term_length,line_term_length)+1;
 
765
  set_if_bigger(length,line_start.length());
 
766
  stack=stack_pos=(int*) sql_alloc(sizeof(int)*length);
795
767
 
796
768
  if (!(buffer=(unsigned char*) calloc(1, buff_length+1)))
797
 
    error=1;
 
769
    error=1; /* purecov: inspected */
798
770
  else
799
771
  {
800
772
    end_of_buff=buffer+buff_length;
801
 
    if (cache.init_io_cache((false) ? -1 : cursor, 0,
802
 
                            (false) ? internal::READ_NET :
803
 
                            (is_fifo ? internal::READ_FIFO : internal::READ_CACHE),0L,1,
804
 
                            MYF(MY_WME)))
 
773
    if (init_io_cache(&cache,(false) ? -1 : file, 0,
 
774
                      (false) ? READ_NET :
 
775
                      (is_fifo ? READ_FIFO : READ_CACHE),0L,1,
 
776
                      MYF(MY_WME)))
805
777
    {
806
 
      free((unsigned char*) buffer);
 
778
      free((unsigned char*) buffer); /* purecov: inspected */
807
779
      error=1;
808
780
    }
809
781
    else
824
796
  if (!error)
825
797
  {
826
798
    if (need_end_io_cache)
827
 
      cache.end_io_cache();
 
799
      ::end_io_cache(&cache);
828
800
    free(buffer);
829
801
    error=1;
830
802
  }
894
866
    while ( to < end_of_buff)
895
867
    {
896
868
      chr = GET;
 
869
#ifdef USE_MB
897
870
      if ((my_mbcharlen(read_charset, chr) > 1) &&
898
871
          to+my_mbcharlen(read_charset, chr) <= end_of_buff)
899
872
      {
900
 
        unsigned char* p = (unsigned char*)to;
901
 
        *to++ = chr;
902
 
        int ml = my_mbcharlen(read_charset, chr);
903
 
        int i;
904
 
        for (i=1; i<ml; i++) {
905
 
          chr = GET;
906
 
          if (chr == my_b_EOF)
907
 
            goto found_eof;
908
 
          *to++ = chr;
909
 
        }
910
 
        if (my_ismbchar(read_charset,
911
 
              (const char *)p,
912
 
              (const char *)to))
913
 
          continue;
914
 
        for (i=0; i<ml; i++)
915
 
          PUSH((unsigned char) *--to);
916
 
        chr = GET;
 
873
          unsigned char* p = (unsigned char*)to;
 
874
          *to++ = chr;
 
875
          int ml = my_mbcharlen(read_charset, chr);
 
876
          int i;
 
877
          for (i=1; i<ml; i++) {
 
878
              chr = GET;
 
879
              if (chr == my_b_EOF)
 
880
                  goto found_eof;
 
881
              *to++ = chr;
 
882
          }
 
883
          if (my_ismbchar(read_charset,
 
884
                          (const char *)p,
 
885
                          (const char *)to))
 
886
            continue;
 
887
          for (i=0; i<ml; i++)
 
888
            PUSH((unsigned char) *--to);
 
889
          chr = GET;
917
890
      }
 
891
#endif
918
892
      if (chr == my_b_EOF)
919
 
        goto found_eof;
 
893
        goto found_eof;
920
894
      if (chr == escape_char)
921
895
      {
922
 
        if ((chr=GET) == my_b_EOF)
923
 
        {
924
 
          *to++= (unsigned char) escape_char;
925
 
          goto found_eof;
926
 
        }
 
896
        if ((chr=GET) == my_b_EOF)
 
897
        {
 
898
          *to++= (unsigned char) escape_char;
 
899
          goto found_eof;
 
900
        }
927
901
        /*
928
902
          When escape_char == enclosed_char, we treat it like we do for
929
903
          handling quotes in SQL parsing -- you can double-up the
942
916
#ifdef ALLOW_LINESEPARATOR_IN_STRINGS
943
917
      if (chr == line_term_char)
944
918
#else
945
 
        if (chr == line_term_char && found_enclosed_char == INT_MAX)
 
919
      if (chr == line_term_char && found_enclosed_char == INT_MAX)
946
920
#endif
947
 
        {
948
 
          if (terminator(line_term_ptr,line_term_length))
949
 
          {                                     // Maybe unexpected linefeed
950
 
            enclosed=0;
951
 
            found_end_of_line=1;
952
 
            row_start=buffer;
953
 
            row_end=  to;
954
 
            return 0;
955
 
          }
956
 
        }
 
921
      {
 
922
        if (terminator(line_term_ptr,line_term_length))
 
923
        {                                       // Maybe unexpected linefeed
 
924
          enclosed=0;
 
925
          found_end_of_line=1;
 
926
          row_start=buffer;
 
927
          row_end=  to;
 
928
          return 0;
 
929
        }
 
930
      }
957
931
      if (chr == found_enclosed_char)
958
932
      {
959
 
        if ((chr=GET) == found_enclosed_char)
960
 
        {                                       // Remove dupplicated
961
 
          *to++ = (unsigned char) chr;
962
 
          continue;
963
 
        }
964
 
        // End of enclosed field if followed by field_term or line_term
965
 
        if (chr == my_b_EOF ||
966
 
            (chr == line_term_char && terminator(line_term_ptr, line_term_length)))
967
 
        {                                       // Maybe unexpected linefeed
968
 
          enclosed=1;
969
 
          found_end_of_line=1;
970
 
          row_start=buffer+1;
971
 
          row_end=  to;
972
 
          return 0;
973
 
        }
974
 
        if (chr == field_term_char &&
975
 
            terminator(field_term_ptr,field_term_length))
976
 
        {
977
 
          enclosed=1;
978
 
          row_start=buffer+1;
979
 
          row_end=  to;
980
 
          return 0;
981
 
        }
982
 
        /*
983
 
           The string didn't terminate yet.
984
 
           Store back next character for the loop
985
 
         */
986
 
        PUSH(chr);
987
 
        /* copy the found term character to 'to' */
988
 
        chr= found_enclosed_char;
 
933
        if ((chr=GET) == found_enclosed_char)
 
934
        {                                       // Remove dupplicated
 
935
          *to++ = (unsigned char) chr;
 
936
          continue;
 
937
        }
 
938
        // End of enclosed field if followed by field_term or line_term
 
939
        if (chr == my_b_EOF ||
 
940
            (chr == line_term_char && terminator(line_term_ptr, line_term_length)))
 
941
        {                                       // Maybe unexpected linefeed
 
942
          enclosed=1;
 
943
          found_end_of_line=1;
 
944
          row_start=buffer+1;
 
945
          row_end=  to;
 
946
          return 0;
 
947
        }
 
948
        if (chr == field_term_char &&
 
949
            terminator(field_term_ptr,field_term_length))
 
950
        {
 
951
          enclosed=1;
 
952
          row_start=buffer+1;
 
953
          row_end=  to;
 
954
          return 0;
 
955
        }
 
956
        /*
 
957
          The string didn't terminate yet.
 
958
          Store back next character for the loop
 
959
        */
 
960
        PUSH(chr);
 
961
        /* copy the found term character to 'to' */
 
962
        chr= found_enclosed_char;
989
963
      }
990
964
      else if (chr == field_term_char && found_enclosed_char == INT_MAX)
991
965
      {
992
 
        if (terminator(field_term_ptr,field_term_length))
993
 
        {
994
 
          enclosed=0;
995
 
          row_start=buffer;
996
 
          row_end=  to;
997
 
          return 0;
998
 
        }
 
966
        if (terminator(field_term_ptr,field_term_length))
 
967
        {
 
968
          enclosed=0;
 
969
          row_start=buffer;
 
970
          row_end=  to;
 
971
          return 0;
 
972
        }
999
973
      }
1000
974
      *to++ = (unsigned char) chr;
1001
975
    }
1002
976
    /*
1003
 
     ** We come here if buffer is too small. Enlarge it and continue
1004
 
     */
 
977
    ** We come here if buffer is too small. Enlarge it and continue
 
978
    */
1005
979
    if (!(new_buffer=(unsigned char*) realloc(buffer, buff_length+1+IO_SIZE)))
1006
980
      return (error=1);
1007
981
    to=new_buffer + (to-buffer);
1023
997
 
1024
998
  NOTES
1025
999
    The row may not be fixed size on disk if there are escape
1026
 
    characters in the cursor.
 
1000
    characters in the file.
1027
1001
 
1028
1002
  IMPLEMENTATION NOTE
1029
1003
    One can't use fixed length with multi-byte charset **
1099
1073
  for (;;)
1100
1074
  {
1101
1075
    int chr = GET;
1102
 
    if (my_mbcharlen(read_charset, chr) > 1)
1103
 
    {
1104
 
      for (uint32_t i=1;
1105
 
          chr != my_b_EOF && i<my_mbcharlen(read_charset, chr);
1106
 
          i++)
1107
 
        chr = GET;
1108
 
      if (chr == escape_char)
1109
 
        continue;
1110
 
    }
1111
 
    if (chr == my_b_EOF)
1112
 
    {
 
1076
#ifdef USE_MB
 
1077
   if (my_mbcharlen(read_charset, chr) > 1)
 
1078
   {
 
1079
       for (uint32_t i=1;
 
1080
            chr != my_b_EOF && i<my_mbcharlen(read_charset, chr);
 
1081
            i++)
 
1082
           chr = GET;
 
1083
       if (chr == escape_char)
 
1084
           continue;
 
1085
   }
 
1086
#endif
 
1087
   if (chr == my_b_EOF)
 
1088
   {
1113
1089
      eof=1;
1114
1090
      return 1;
1115
1091
    }
1117
1093
    {
1118
1094
      line_cuted=1;
1119
1095
      if (GET == my_b_EOF)
1120
 
        return 1;
 
1096
        return 1;
1121
1097
      continue;
1122
1098
    }
1123
1099
    if (chr == line_term_char && terminator(line_term_ptr,line_term_length))
1156
1132
}
1157
1133
 
1158
1134
 
1159
 
} /* namespace drizzled */