~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field_conv.cc

  • Committer: Monty Taylor
  • Date: 2009-03-04 02:48:12 UTC
  • mto: (917.1.2 mordred)
  • mto: This revision was merged to the branch mainline in revision 918.
  • Revision ID: mordred@inaugust.com-20090304024812-5wb6wpye5c1iitbq
Applied atomic patch to current tree.

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
/**
24
24
    gives much more speed.
25
25
*/
26
26
 
27
 
#include "config.h"
 
27
#include <drizzled/server_includes.h>
28
28
#include <drizzled/error.h>
29
29
#include <drizzled/table.h>
30
30
#include <drizzled/session.h>
31
31
 
32
32
#include <drizzled/field/str.h>
 
33
#include <drizzled/field/longstr.h>
33
34
#include <drizzled/field/num.h>
34
35
#include <drizzled/field/blob.h>
35
36
#include <drizzled/field/enum.h>
38
39
#include <drizzled/field/decimal.h>
39
40
#include <drizzled/field/real.h>
40
41
#include <drizzled/field/double.h>
41
 
#include <drizzled/field/int32.h>
42
 
#include <drizzled/field/int64.h>
 
42
#include <drizzled/field/long.h>
 
43
#include <drizzled/field/int64_t.h>
43
44
#include <drizzled/field/num.h>
44
45
#include <drizzled/field/timestamp.h>
45
46
#include <drizzled/field/datetime.h>
46
47
#include <drizzled/field/varstring.h>
47
48
 
48
 
namespace drizzled
49
 
{
50
 
 
51
 
static void do_field_eq(CopyField *copy)
52
 
{
53
 
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
54
 
}
55
 
 
56
 
static void do_field_1(CopyField *copy)
57
 
{
58
 
  copy->to_ptr[0]= copy->from_ptr[0];
59
 
}
60
 
 
61
 
static void do_field_2(CopyField *copy)
62
 
{
63
 
  copy->to_ptr[0]= copy->from_ptr[0];
64
 
  copy->to_ptr[1]= copy->from_ptr[1];
65
 
}
66
 
 
67
 
static void do_field_3(CopyField *copy)
68
 
{
69
 
  copy->to_ptr[0]= copy->from_ptr[0];
70
 
  copy->to_ptr[1]= copy->from_ptr[1];
71
 
  copy->to_ptr[2]= copy->from_ptr[2];
72
 
}
73
 
 
74
 
static void do_field_4(CopyField *copy)
75
 
{
76
 
  copy->to_ptr[0]= copy->from_ptr[0];
77
 
  copy->to_ptr[1]= copy->from_ptr[1];
78
 
  copy->to_ptr[2]= copy->from_ptr[2];
79
 
  copy->to_ptr[3]= copy->from_ptr[3];
80
 
}
81
 
 
82
 
static void do_field_6(CopyField *copy)
83
 
{                  // For blob field
84
 
  copy->to_ptr[0]= copy->from_ptr[0];
85
 
  copy->to_ptr[1]= copy->from_ptr[1];
86
 
  copy->to_ptr[2]= copy->from_ptr[2];
87
 
  copy->to_ptr[3]= copy->from_ptr[3];
88
 
  copy->to_ptr[4]= copy->from_ptr[4];
89
 
  copy->to_ptr[5]= copy->from_ptr[5];
90
 
}
91
 
 
92
 
static void do_field_8(CopyField *copy)
93
 
{
94
 
  copy->to_ptr[0]= copy->from_ptr[0];
95
 
  copy->to_ptr[1]= copy->from_ptr[1];
96
 
  copy->to_ptr[2]= copy->from_ptr[2];
97
 
  copy->to_ptr[3]= copy->from_ptr[3];
98
 
  copy->to_ptr[4]= copy->from_ptr[4];
99
 
  copy->to_ptr[5]= copy->from_ptr[5];
100
 
  copy->to_ptr[6]= copy->from_ptr[6];
101
 
  copy->to_ptr[7]= copy->from_ptr[7];
102
 
}
103
 
 
104
 
 
105
 
static void do_field_to_null_str(CopyField *copy)
 
49
 
 
50
static void do_field_eq(Copy_field *copy)
 
51
{
 
52
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
 
53
}
 
54
 
 
55
static void do_field_1(Copy_field *copy)
 
56
{
 
57
  copy->to_ptr[0]=copy->from_ptr[0];
 
58
}
 
59
 
 
60
static void do_field_2(Copy_field *copy)
 
61
{
 
62
  copy->to_ptr[0]=copy->from_ptr[0];
 
63
  copy->to_ptr[1]=copy->from_ptr[1];
 
64
}
 
65
 
 
66
static void do_field_3(Copy_field *copy)
 
67
{
 
68
  copy->to_ptr[0]=copy->from_ptr[0];
 
69
  copy->to_ptr[1]=copy->from_ptr[1];
 
70
  copy->to_ptr[2]=copy->from_ptr[2];
 
71
}
 
72
 
 
73
static void do_field_4(Copy_field *copy)
 
74
{
 
75
  copy->to_ptr[0]=copy->from_ptr[0];
 
76
  copy->to_ptr[1]=copy->from_ptr[1];
 
77
  copy->to_ptr[2]=copy->from_ptr[2];
 
78
  copy->to_ptr[3]=copy->from_ptr[3];
 
79
}
 
80
 
 
81
static void do_field_6(Copy_field *copy)
 
82
{                                               // For blob field
 
83
  copy->to_ptr[0]=copy->from_ptr[0];
 
84
  copy->to_ptr[1]=copy->from_ptr[1];
 
85
  copy->to_ptr[2]=copy->from_ptr[2];
 
86
  copy->to_ptr[3]=copy->from_ptr[3];
 
87
  copy->to_ptr[4]=copy->from_ptr[4];
 
88
  copy->to_ptr[5]=copy->from_ptr[5];
 
89
}
 
90
 
 
91
static void do_field_8(Copy_field *copy)
 
92
{
 
93
  copy->to_ptr[0]=copy->from_ptr[0];
 
94
  copy->to_ptr[1]=copy->from_ptr[1];
 
95
  copy->to_ptr[2]=copy->from_ptr[2];
 
96
  copy->to_ptr[3]=copy->from_ptr[3];
 
97
  copy->to_ptr[4]=copy->from_ptr[4];
 
98
  copy->to_ptr[5]=copy->from_ptr[5];
 
99
  copy->to_ptr[6]=copy->from_ptr[6];
 
100
  copy->to_ptr[7]=copy->from_ptr[7];
 
101
}
 
102
 
 
103
 
 
104
static void do_field_to_null_str(Copy_field *copy)
106
105
{
107
106
  if (*copy->from_null_ptr & copy->from_bit)
108
107
  {
109
108
    memset(copy->to_ptr, 0, copy->from_length);
110
 
    copy->to_null_ptr[0]= 1;  // Always bit 1
 
109
    copy->to_null_ptr[0]=1;                     // Always bit 1
111
110
  }
112
111
  else
113
112
  {
114
 
    copy->to_null_ptr[0]= 0;
115
 
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
 
113
    copy->to_null_ptr[0]=0;
 
114
    memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
116
115
  }
117
116
}
118
117
 
119
118
 
120
 
static void do_outer_field_to_null_str(CopyField *copy)
 
119
static void do_outer_field_to_null_str(Copy_field *copy)
121
120
{
122
121
  if (*copy->null_row ||
123
122
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
124
123
  {
125
124
    memset(copy->to_ptr, 0, copy->from_length);
126
 
    copy->to_null_ptr[0]= 1;  // Always bit 1
 
125
    copy->to_null_ptr[0]=1;                     // Always bit 1
127
126
  }
128
127
  else
129
128
  {
130
 
    copy->to_null_ptr[0]= 0;
131
 
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
 
129
    copy->to_null_ptr[0]=0;
 
130
    memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
132
131
  }
133
132
}
134
133
 
143
142
    return 0;
144
143
  }
145
144
  field->reset();
146
 
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
 
145
  if (field->table->in_use->count_cuted_fields == CHECK_FIELD_WARN)
147
146
  {
148
147
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
149
148
    return 0;
150
149
  }
151
 
  if (!field->getTable()->in_use->no_errors)
 
150
  if (!field->table->in_use->no_errors)
152
151
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
153
152
  return -1;
154
153
}
194
193
    return 0;                                   // Ok to set time to NULL
195
194
  }
196
195
  field->reset();
197
 
  if (field == field->getTable()->next_number_field)
 
196
  if (field == field->table->next_number_field)
198
197
  {
199
 
    field->getTable()->auto_increment_field_not_null= false;
 
198
    field->table->auto_increment_field_not_null= false;
200
199
    return 0;                             // field is set in fill_record()
201
200
  }
202
 
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
 
201
  if (field->table->in_use->count_cuted_fields == CHECK_FIELD_WARN)
203
202
  {
204
203
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
205
204
    return 0;
206
205
  }
207
 
  if (!field->getTable()->in_use->no_errors)
 
206
  if (!field->table->in_use->no_errors)
208
207
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
209
208
  return -1;
210
209
}
211
210
 
212
211
 
213
 
static void do_skip(CopyField *)
 
212
static void do_skip(Copy_field *)
214
213
{
215
214
}
216
215
 
217
216
 
218
 
static void do_copy_null(CopyField *copy)
 
217
static void do_copy_null(Copy_field *copy)
219
218
{
220
219
  if (*copy->from_null_ptr & copy->from_bit)
221
220
  {
222
 
    *copy->to_null_ptr|= copy->to_bit;
 
221
    *copy->to_null_ptr|=copy->to_bit;
223
222
    copy->to_field->reset();
224
223
  }
225
224
  else
230
229
}
231
230
 
232
231
 
233
 
static void do_outer_field_null(CopyField *copy)
 
232
static void do_outer_field_null(Copy_field *copy)
234
233
{
235
234
  if (*copy->null_row ||
236
235
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
246
245
}
247
246
 
248
247
 
249
 
static void do_copy_not_null(CopyField *copy)
 
248
static void do_copy_not_null(Copy_field *copy)
250
249
{
251
 
  if (copy->to_field->hasDefault() and *copy->from_null_ptr & copy->from_bit)
252
 
  {
253
 
    copy->to_field->set_default();
254
 
  }
255
 
  else if (*copy->from_null_ptr & copy->from_bit)
 
250
  if (*copy->from_null_ptr & copy->from_bit)
256
251
  {
257
252
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
258
253
                                ER_WARN_DATA_TRUNCATED, 1);
259
254
    copy->to_field->reset();
260
255
  }
261
256
  else
262
 
  {
263
257
    (copy->do_copy2)(copy);
264
 
  }
265
258
}
266
259
 
267
260
 
268
 
static void do_copy_maybe_null(CopyField *copy)
 
261
static void do_copy_maybe_null(Copy_field *copy)
269
262
{
270
263
  *copy->to_null_ptr&= ~copy->to_bit;
271
264
  (copy->do_copy2)(copy);
273
266
 
274
267
/* timestamp and next_number has special handling in case of NULL values */
275
268
 
276
 
static void do_copy_timestamp(CopyField *copy)
 
269
static void do_copy_timestamp(Copy_field *copy)
277
270
{
278
271
  if (*copy->from_null_ptr & copy->from_bit)
279
272
  {
285
278
}
286
279
 
287
280
 
288
 
static void do_copy_next_number(CopyField *copy)
 
281
static void do_copy_next_number(Copy_field *copy)
289
282
{
290
283
  if (*copy->from_null_ptr & copy->from_bit)
291
284
  {
292
285
    /* Same as in set_field_to_null_with_conversions() */
293
 
    copy->to_field->getTable()->auto_increment_field_not_null= false;
 
286
    copy->to_field->table->auto_increment_field_not_null= false;
294
287
    copy->to_field->reset();
295
288
  }
296
289
  else
298
291
}
299
292
 
300
293
 
301
 
static void do_copy_blob(CopyField *copy)
 
294
static void do_copy_blob(Copy_field *copy)
302
295
{
303
 
  ulong length= ((Field_blob*) copy->from_field)->get_length();
 
296
  ulong length=((Field_blob*) copy->from_field)->get_length();
304
297
  ((Field_blob*) copy->to_field)->store_length(length);
305
 
  memcpy(copy->to_ptr, copy->from_ptr, sizeof(char*));
 
298
  memcpy(copy->to_ptr,copy->from_ptr,sizeof(char*));
306
299
}
307
300
 
308
 
static void do_conv_blob(CopyField *copy)
 
301
static void do_conv_blob(Copy_field *copy)
309
302
{
310
 
  copy->from_field->val_str_internal(&copy->tmp);
 
303
  copy->from_field->val_str(&copy->tmp);
311
304
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
312
 
                                         copy->tmp.length(),
313
 
                                         copy->tmp.charset());
 
305
                                         copy->tmp.length(),
 
306
                                         copy->tmp.charset());
314
307
}
315
308
 
316
309
/** Save blob in copy->tmp for GROUP BY. */
317
310
 
318
 
static void do_save_blob(CopyField *copy)
 
311
static void do_save_blob(Copy_field *copy)
319
312
{
320
313
  char buff[MAX_FIELD_WIDTH];
321
 
  String res(buff, sizeof(buff), copy->tmp.charset());
322
 
  copy->from_field->val_str_internal(&res);
 
314
  String res(buff,sizeof(buff),copy->tmp.charset());
 
315
  copy->from_field->val_str(&res);
323
316
  copy->tmp.copy(res);
324
317
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
325
 
                                         copy->tmp.length(),
326
 
                                         copy->tmp.charset());
 
318
                                         copy->tmp.length(),
 
319
                                         copy->tmp.charset());
327
320
}
328
321
 
329
322
 
330
 
static void do_field_string(CopyField *copy)
 
323
static void do_field_string(Copy_field *copy)
331
324
{
332
325
  char buff[MAX_FIELD_WIDTH];
333
326
  copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
334
 
  copy->from_field->val_str_internal(&copy->tmp);
 
327
  copy->from_field->val_str(&copy->tmp);
335
328
  copy->to_field->store(copy->tmp.c_ptr_quick(),copy->tmp.length(),
336
329
                        copy->tmp.charset());
337
330
}
338
331
 
339
332
 
340
 
static void do_field_enum(CopyField *copy)
 
333
static void do_field_enum(Copy_field *copy)
341
334
{
342
335
  if (copy->from_field->val_int() == 0)
343
336
    ((Field_enum *) copy->to_field)->store_type((uint64_t) 0);
346
339
}
347
340
 
348
341
 
349
 
static void do_field_int(CopyField *copy)
 
342
static void do_field_int(Copy_field *copy)
350
343
{
351
344
  int64_t value= copy->from_field->val_int();
352
345
  copy->to_field->store(value,
353
346
                        test(copy->from_field->flags & UNSIGNED_FLAG));
354
347
}
355
348
 
356
 
static void do_field_real(CopyField *copy)
 
349
static void do_field_real(Copy_field *copy)
357
350
{
358
351
  double value=copy->from_field->val_real();
359
352
  copy->to_field->store(value);
360
353
}
361
354
 
362
355
 
363
 
static void do_field_decimal(CopyField *copy)
 
356
static void do_field_decimal(Copy_field *copy)
364
357
{
365
358
  my_decimal value;
366
359
  copy->to_field->store_decimal(copy->from_field->val_decimal(&value));
372
365
  from string.
373
366
*/
374
367
 
375
 
static void do_cut_string(CopyField *copy)
 
368
static void do_cut_string(Copy_field *copy)
376
369
{
377
370
  const CHARSET_INFO * const cs= copy->from_field->charset();
378
371
  memcpy(copy->to_ptr,copy->from_ptr,copy->to_length);
394
387
  from string.
395
388
*/
396
389
 
397
 
static void do_cut_string_complex(CopyField *copy)
 
390
static void do_cut_string_complex(Copy_field *copy)
398
391
{                                               // Shorter string field
399
392
  int well_formed_error;
400
393
  const CHARSET_INFO * const cs= copy->from_field->charset();
426
419
 
427
420
 
428
421
 
429
 
static void do_expand_binary(CopyField *copy)
430
 
{
431
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
432
 
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
433
 
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
434
 
                 copy->to_length-copy->from_length, '\0');
435
 
}
436
 
 
437
 
 
438
 
 
439
 
static void do_expand_string(CopyField *copy)
440
 
{
441
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
442
 
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
443
 
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
444
 
                 copy->to_length-copy->from_length, ' ');
445
 
}
446
 
 
447
 
 
448
 
static void do_varstring1(CopyField *copy)
 
422
static void do_expand_binary(Copy_field *copy)
 
423
{
 
424
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
425
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
 
426
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
 
427
                     copy->to_length-copy->from_length, '\0');
 
428
}
 
429
 
 
430
 
 
431
 
 
432
static void do_expand_string(Copy_field *copy)
 
433
{
 
434
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
435
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
 
436
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
 
437
                     copy->to_length-copy->from_length, ' ');
 
438
}
 
439
 
 
440
 
 
441
static void do_varstring1(Copy_field *copy)
449
442
{
450
443
  uint32_t length= (uint32_t) *(unsigned char*) copy->from_ptr;
451
444
  if (length > copy->to_length- 1)
452
445
  {
453
 
    length= copy->to_length - 1;
454
 
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
 
446
    length=copy->to_length - 1;
 
447
    if (copy->from_field->table->in_use->count_cuted_fields)
455
448
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
456
449
                                  ER_WARN_DATA_TRUNCATED, 1);
457
450
  }
460
453
}
461
454
 
462
455
 
463
 
static void do_varstring1_mb(CopyField *copy)
 
456
static void do_varstring1_mb(Copy_field *copy)
464
457
{
465
458
  int well_formed_error;
466
459
  const CHARSET_INFO * const cs= copy->from_field->charset();
481
474
}
482
475
 
483
476
 
484
 
static void do_varstring2(CopyField *copy)
 
477
static void do_varstring2(Copy_field *copy)
485
478
{
486
 
  uint32_t length= uint2korr(copy->from_ptr);
 
479
  uint32_t length=uint2korr(copy->from_ptr);
487
480
  if (length > copy->to_length- HA_KEY_BLOB_LENGTH)
488
481
  {
489
482
    length=copy->to_length-HA_KEY_BLOB_LENGTH;
490
 
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
 
483
    if (copy->from_field->table->in_use->count_cuted_fields)
491
484
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
492
485
                                  ER_WARN_DATA_TRUNCATED, 1);
493
486
  }
497
490
}
498
491
 
499
492
 
500
 
static void do_varstring2_mb(CopyField *copy)
 
493
static void do_varstring2_mb(Copy_field *copy)
501
494
{
502
495
  int well_formed_error;
503
496
  const CHARSET_INFO * const cs= copy->from_field->charset();
505
498
  uint32_t from_length= uint2korr(copy->from_ptr);
506
499
  const unsigned char *from_beg= copy->from_ptr + HA_KEY_BLOB_LENGTH;
507
500
  uint32_t length= cs->cset->well_formed_len(cs, (char*) from_beg,
508
 
                                             (char*) from_beg + from_length,
509
 
                                             char_length, &well_formed_error);
 
501
                                         (char*) from_beg + from_length,
 
502
                                         char_length, &well_formed_error);
510
503
  if (length < from_length)
511
504
  {
512
505
    if (current_session->count_cuted_fields)
519
512
 
520
513
 
521
514
/***************************************************************************
522
 
** The different functions that fills in a CopyField class
 
515
** The different functions that fills in a Copy_field class
523
516
***************************************************************************/
524
517
 
525
518
/**
530
523
  The 'to' buffer should have a size of field->pack_length()+1
531
524
*/
532
525
 
533
 
void CopyField::set(unsigned char *to,Field *from)
 
526
void Copy_field::set(unsigned char *to,Field *from)
534
527
{
535
 
  from_ptr= from->ptr;
536
 
  to_ptr= to;
537
 
  from_length= from->pack_length();
 
528
  from_ptr=from->ptr;
 
529
  to_ptr=to;
 
530
  from_length=from->pack_length();
538
531
  if (from->maybe_null())
539
532
  {
540
 
    from_null_ptr= from->null_ptr;
541
 
    from_bit= from->null_bit;
542
 
    to_ptr[0]= 1;                             // Null as default value
543
 
    to_null_ptr= (unsigned char*) to_ptr++;
544
 
    to_bit= 1;
545
 
    if (from->getTable()->maybe_null)
 
533
    from_null_ptr=from->null_ptr;
 
534
    from_bit=     from->null_bit;
 
535
    to_ptr[0]=    1;                            // Null as default value
 
536
    to_null_ptr=  (unsigned char*) to_ptr++;
 
537
    to_bit=       1;
 
538
    if (from->table->maybe_null)
546
539
    {
547
 
      null_row= &from->getTable()->null_row;
548
 
      do_copy= do_outer_field_to_null_str;
 
540
      null_row=   &from->table->null_row;
 
541
      do_copy=    do_outer_field_to_null_str;
549
542
    }
550
543
    else
551
 
      do_copy= do_field_to_null_str;
 
544
      do_copy=    do_field_to_null_str;
552
545
  }
553
546
  else
554
547
  {
555
 
    to_null_ptr= 0;                           // For easy debugging
556
 
    do_copy= do_field_eq;
 
548
    to_null_ptr=  0;                            // For easy debugging
 
549
    do_copy=      do_field_eq;
557
550
  }
558
551
}
559
552
 
573
566
  - The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended
574
567
    effect? Truncation is handled by well_formed_copy_nchars anyway.
575
568
 */
576
 
void CopyField::set(Field *to,Field *from,bool save)
 
569
void Copy_field::set(Field *to,Field *from,bool save)
577
570
{
578
571
  if (to->type() == DRIZZLE_TYPE_NULL)
579
572
  {
580
 
    to_null_ptr= 0;           // For easy debugging
581
 
    to_ptr= 0;
582
 
    do_copy= do_skip;
 
573
    to_null_ptr=0;                              // For easy debugging
 
574
    to_ptr=0;
 
575
    do_copy=do_skip;
583
576
    return;
584
577
  }
585
 
  from_field= from;
586
 
  to_field= to;
587
 
  from_ptr= from->ptr;
588
 
  from_length= from->pack_length();
589
 
  to_ptr= to->ptr;
590
 
  to_length= to_field->pack_length();
 
578
  from_field=from;
 
579
  to_field=to;
 
580
  from_ptr=from->ptr;
 
581
  from_length=from->pack_length();
 
582
  to_ptr=  to->ptr;
 
583
  to_length=to_field->pack_length();
591
584
 
592
585
  // set up null handling
593
 
  from_null_ptr= to_null_ptr= 0;
 
586
  from_null_ptr=to_null_ptr=0;
594
587
  if (from->maybe_null())
595
588
  {
596
 
    from_null_ptr= from->null_ptr;
597
 
    from_bit= from->null_bit;
 
589
    from_null_ptr=      from->null_ptr;
 
590
    from_bit=           from->null_bit;
598
591
    if (to_field->real_maybe_null())
599
592
    {
600
 
      to_null_ptr= to->null_ptr;
601
 
      to_bit= to->null_bit;
 
593
      to_null_ptr=      to->null_ptr;
 
594
      to_bit=           to->null_bit;
602
595
      if (from_null_ptr)
603
 
      {
604
 
        do_copy= do_copy_null;
605
 
      }
 
596
        do_copy=        do_copy_null;
606
597
      else
607
598
      {
608
 
        null_row= &from->getTable()->null_row;
609
 
        do_copy= do_outer_field_null;
 
599
        null_row=       &from->table->null_row;
 
600
        do_copy=        do_outer_field_null;
610
601
      }
611
602
    }
612
603
    else
613
604
    {
614
605
      if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
615
 
      {
616
606
        do_copy= do_copy_timestamp;               // Automatic timestamp
617
 
      }
618
 
      else if (to_field == to_field->getTable()->next_number_field)
619
 
      {
 
607
      else if (to_field == to_field->table->next_number_field)
620
608
        do_copy= do_copy_next_number;
621
 
      }
622
609
      else
623
 
      {
624
610
        do_copy= do_copy_not_null;
625
 
      }
626
611
    }
627
612
  }
628
613
  else if (to_field->real_maybe_null())
629
614
  {
630
 
    to_null_ptr= to->null_ptr;
631
 
    to_bit= to->null_bit;
 
615
    to_null_ptr=        to->null_ptr;
 
616
    to_bit=             to->null_bit;
632
617
    do_copy= do_copy_maybe_null;
633
618
  }
634
619
  else
635
 
   do_copy= 0;
 
620
   do_copy=0;
636
621
 
637
622
  if ((to->flags & BLOB_FLAG) && save)
638
623
    do_copy2= do_save_blob;
639
624
  else
640
625
    do_copy2= get_copy_func(to,from);
641
 
  if (!do_copy)         // Not null
642
 
    do_copy= do_copy2;
 
626
  if (!do_copy)                                 // Not null
 
627
    do_copy=do_copy2;
643
628
}
644
629
 
645
630
 
646
 
CopyField::Copy_func *
647
 
CopyField::get_copy_func(Field *to,Field *from)
 
631
Copy_field::Copy_func *
 
632
Copy_field::get_copy_func(Field *to,Field *from)
648
633
{
649
 
  bool compatible_db_low_byte_first= (to->getTable()->getShare()->db_low_byte_first ==
650
 
                                     from->getTable()->getShare()->db_low_byte_first);
 
634
  bool compatible_db_low_byte_first= (to->table->s->db_low_byte_first ==
 
635
                                     from->table->s->db_low_byte_first);
651
636
  if (to->flags & BLOB_FLAG)
652
637
  {
653
638
    if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
655
640
    if (from_length != to_length || !compatible_db_low_byte_first)
656
641
    {
657
642
      // Correct pointer to point at char pointer
658
 
      to_ptr+= to_length - to->getTable()->getShare()->blob_ptr_size;
659
 
      from_ptr+= from_length- from->getTable()->getShare()->blob_ptr_size;
 
643
      to_ptr+=   to_length - to->table->s->blob_ptr_size;
 
644
      from_ptr+= from_length- from->table->s->blob_ptr_size;
660
645
      return do_copy_blob;
661
646
    }
662
647
  }
664
649
  {
665
650
    if (to->result_type() == DECIMAL_RESULT)
666
651
      return do_field_decimal;
667
 
      
668
652
    // Check if identical fields
669
653
    if (from->result_type() == STRING_RESULT)
670
654
    {
674
658
      */
675
659
      if (to->real_type() != from->real_type() ||
676
660
          !compatible_db_low_byte_first ||
677
 
          (((to->getTable()->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) && to->type() == DRIZZLE_TYPE_DATE) || to->type() == DRIZZLE_TYPE_DATETIME))
 
661
          (((to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) && to->type() == DRIZZLE_TYPE_DATE) || to->type() == DRIZZLE_TYPE_DATETIME))
678
662
      {
679
 
        if (from->real_type() == DRIZZLE_TYPE_ENUM)
680
 
        {
681
 
          if (to->result_type() != STRING_RESULT)
682
 
          {
683
 
            return do_field_int;  // Convert SET to number
684
 
          }
685
 
 
686
 
          return do_field_string;
687
 
        }
 
663
        if (from->real_type() == DRIZZLE_TYPE_ENUM)
 
664
          if (to->result_type() != STRING_RESULT)
 
665
            return do_field_int;                // Convert SET to number
 
666
        return do_field_string;
688
667
      }
689
 
 
690
668
      if (to->real_type() == DRIZZLE_TYPE_ENUM)
691
669
      {
692
 
        if (!to->eq_def(from))
 
670
        if (!to->eq_def(from))
693
671
        {
694
672
          if (from->real_type() == DRIZZLE_TYPE_ENUM &&
695
673
              to->real_type() == DRIZZLE_TYPE_ENUM)
699
677
        }
700
678
      }
701
679
      else if (to->charset() != from->charset())
702
 
        return do_field_string;
 
680
        return do_field_string;
703
681
      else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
704
682
      {
705
 
        /* Field_blob is not part of the Field_varstring hierarchy,
706
 
          and casting to varstring for calling pack_length_no_ptr()
707
 
          is always incorrect. Previously the below comparison has
708
 
          always evaluated to false as pack_length_no_ptr() for BLOB
709
 
          will return 4 and varstring can only be <= 2.
710
 
          If your brain slightly bleeds as to why this worked for
711
 
          so many years, you are in no way alone.
712
 
        */
713
 
        if (from->flags & BLOB_FLAG)
714
 
          return do_field_string;
715
 
 
716
 
        if ((static_cast<Field_varstring*>(to))->pack_length_no_ptr() !=
717
 
            (static_cast<Field_varstring*>(from))->pack_length_no_ptr())
718
 
        {
719
 
          return do_field_string;
720
 
        }
721
 
 
 
683
        if (((Field_varstring*) to)->length_bytes !=
 
684
            ((Field_varstring*) from)->length_bytes)
 
685
          return do_field_string;
722
686
        if (to_length != from_length)
723
 
        {
724
 
          return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
 
687
          return (((Field_varstring*) to)->length_bytes == 1 ?
725
688
                  (from->charset()->mbmaxlen == 1 ? do_varstring1 :
726
 
                   do_varstring1_mb) :
 
689
                                                    do_varstring1_mb) :
727
690
                  (from->charset()->mbmaxlen == 1 ? do_varstring2 :
728
 
                   do_varstring2_mb));
729
 
        }
 
691
                                                    do_varstring2_mb));
730
692
      }
731
693
      else if (to_length < from_length)
732
 
      {
733
 
        return (from->charset()->mbmaxlen == 1 ?
 
694
        return (from->charset()->mbmaxlen == 1 ?
734
695
                do_cut_string : do_cut_string_complex);
735
 
      }
736
696
      else if (to_length > from_length)
737
697
      {
738
698
        if (to->charset() == &my_charset_bin)
740
700
        else
741
701
          return do_expand_string;
742
702
      }
 
703
 
743
704
    }
744
705
    else if (to->real_type() != from->real_type() ||
745
 
             to_length != from_length ||
 
706
             to_length != from_length ||
746
707
             !compatible_db_low_byte_first)
747
708
    {
748
709
      if (to->result_type() == STRING_RESULT)
749
 
        return do_field_string;
 
710
        return do_field_string;
750
711
      if (to->result_type() == INT_RESULT)
751
 
        return do_field_int;
752
 
 
 
712
        return do_field_int;
753
713
      return do_field_real;
754
714
    }
755
715
    else
756
716
    {
757
717
      if (!to->eq_def(from) || !compatible_db_low_byte_first)
758
718
      {
759
 
        if (to->result_type() == INT_RESULT)
760
 
          return do_field_int;
761
 
        else
762
 
          return do_field_real;
 
719
        if (to->result_type() == INT_RESULT)
 
720
          return do_field_int;
 
721
        else
 
722
          return do_field_real;
763
723
      }
764
724
    }
765
725
  }
766
 
  
767
726
    /* Eq fields */
768
 
  switch (to_length)
769
 
  {
 
727
  switch (to_length) {
770
728
  case 1: return do_field_1;
771
729
  case 2: return do_field_2;
772
730
  case 3: return do_field_3;
774
732
  case 6: return do_field_6;
775
733
  case 8: return do_field_8;
776
734
  }
777
 
  
778
735
  return do_field_eq;
779
736
}
780
737
 
784
741
int field_conv(Field *to,Field *from)
785
742
{
786
743
  if (to->real_type() == from->real_type() &&
787
 
      !(to->type() == DRIZZLE_TYPE_BLOB && to->getTable()->copy_blobs))
 
744
      !(to->type() == DRIZZLE_TYPE_BLOB && to->table->copy_blobs))
788
745
  {
789
746
    /* Please god, will someone rewrite this to be readable :( */
790
747
    if (to->pack_length() == from->pack_length() &&
791
748
        !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
792
749
        to->real_type() != DRIZZLE_TYPE_ENUM &&
793
 
        (to->real_type() != DRIZZLE_TYPE_DECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
 
750
        (to->real_type() != DRIZZLE_TYPE_NEWDECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
794
751
        from->charset() == to->charset() &&
795
 
        to->getTable()->getShare()->db_low_byte_first == from->getTable()->getShare()->db_low_byte_first &&
796
 
        (!(to->getTable()->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
797
 
        (from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->pack_length_no_ptr() == ((Field_varstring*)to)->pack_length_no_ptr()))
 
752
        to->table->s->db_low_byte_first == from->table->s->db_low_byte_first &&
 
753
        (!(to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
 
754
        (from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->length_bytes == ((Field_varstring*)to)->length_bytes))
798
755
    {                                           // Identical fields
799
756
      /* This may happen if one does 'UPDATE ... SET x=x' */
800
757
      if (to->ptr != from->ptr)
805
762
  if (to->type() == DRIZZLE_TYPE_BLOB)
806
763
  {                                             // Be sure the value is stored
807
764
    Field_blob *blob=(Field_blob*) to;
808
 
    from->val_str_internal(&blob->value);
 
765
    from->val_str(&blob->value);
809
766
    /*
810
767
      Copy value if copy_blobs is set, or source is not a string and
811
768
      we have a pointer to its internal string conversion buffer.
812
769
    */
813
 
    if (to->getTable()->copy_blobs ||
 
770
    if (to->table->copy_blobs ||
814
771
        (!blob->value.is_alloced() &&
815
772
         from->real_type() != DRIZZLE_TYPE_VARCHAR))
816
773
      blob->value.copy();
829
786
  {
830
787
    char buff[MAX_FIELD_WIDTH];
831
788
    String result(buff,sizeof(buff),from->charset());
832
 
    from->val_str_internal(&result);
 
789
    from->val_str(&result);
833
790
    /*
834
791
      We use c_ptr_quick() here to make it easier if to is a float/double
835
792
      as the conversion routines will do a copy of the result doesn't
848
805
  else
849
806
    return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG));
850
807
}
851
 
 
852
 
} /* namespace drizzled */