~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2003 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
16
17
/**
18
  @file
19
20
  @brief
21
  Functions to copy data to or from fields
22
23
    This could be done with a single short function but opencoding this
24
    gives much more speed.
25
*/
26
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
27
#include "config.h"
550 by Monty Taylor
Moved error.h into just the files that need it.
28
#include <drizzled/error.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
29
#include <drizzled/table.h>
30
#include <drizzled/session.h>
1 by brian
clean slate
31
584.5.1 by Monty Taylor
Removed field includes from field.h.
32
#include <drizzled/field/str.h>
33
#include <drizzled/field/num.h>
34
#include <drizzled/field/blob.h>
35
#include <drizzled/field/enum.h>
36
#include <drizzled/field/null.h>
37
#include <drizzled/field/date.h>
670.1.1 by Monty Taylor
Renamed fdecimal.* to decimal.*. Let's see how many things we can break!
38
#include <drizzled/field/decimal.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
39
#include <drizzled/field/real.h>
40
#include <drizzled/field/double.h>
41
#include <drizzled/field/long.h>
42
#include <drizzled/field/int64_t.h>
43
#include <drizzled/field/num.h>
44
#include <drizzled/field/timestamp.h>
45
#include <drizzled/field/datetime.h>
46
#include <drizzled/field/varstring.h>
47
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
48
namespace drizzled
49
{
584.5.1 by Monty Taylor
Removed field includes from field.h.
50
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
51
static void do_field_eq(CopyField *copy)
1 by brian
clean slate
52
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
53
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
54
}
55
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
56
static void do_field_1(CopyField *copy)
1 by brian
clean slate
57
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
58
  copy->to_ptr[0]= copy->from_ptr[0];
1 by brian
clean slate
59
}
60
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
61
static void do_field_2(CopyField *copy)
1 by brian
clean slate
62
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
63
  copy->to_ptr[0]= copy->from_ptr[0];
64
  copy->to_ptr[1]= copy->from_ptr[1];
1 by brian
clean slate
65
}
66
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
67
static void do_field_3(CopyField *copy)
1 by brian
clean slate
68
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
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];
1 by brian
clean slate
72
}
73
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
74
static void do_field_4(CopyField *copy)
1 by brian
clean slate
75
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
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];
1 by brian
clean slate
80
}
81
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
82
static void do_field_6(CopyField *copy)
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
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];
1 by brian
clean slate
90
}
91
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
92
static void do_field_8(CopyField *copy)
1 by brian
clean slate
93
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
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];
1 by brian
clean slate
102
}
103
104
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
105
static void do_field_to_null_str(CopyField *copy)
1 by brian
clean slate
106
{
107
  if (*copy->from_null_ptr & copy->from_bit)
108
  {
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
109
    memset(copy->to_ptr, 0, copy->from_length);
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
110
    copy->to_null_ptr[0]= 1;  // Always bit 1
1 by brian
clean slate
111
  }
112
  else
113
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
114
    copy->to_null_ptr[0]= 0;
115
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
116
  }
117
}
118
119
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
120
static void do_outer_field_to_null_str(CopyField *copy)
1 by brian
clean slate
121
{
122
  if (*copy->null_row ||
123
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
124
  {
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
125
    memset(copy->to_ptr, 0, copy->from_length);
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
126
    copy->to_null_ptr[0]= 1;  // Always bit 1
1 by brian
clean slate
127
  }
128
  else
129
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
130
    copy->to_null_ptr[0]= 0;
131
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
132
  }
133
}
134
135
136
int
137
set_field_to_null(Field *field)
138
{
139
  if (field->real_maybe_null())
140
  {
141
    field->set_null();
142
    field->reset();
143
    return 0;
144
  }
145
  field->reset();
1660.1.3 by Brian Aker
Encapsulate Table in field
146
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
685.4.2 by Jay Pipes
Added back truncation warnings. The NULL error is triggered from: session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL, not CHECK_FIELD_WARN
147
  {
148
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
149
    return 0;
150
  }
1660.1.3 by Brian Aker
Encapsulate Table in field
151
  if (!field->getTable()->in_use->no_errors)
1 by brian
clean slate
152
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
153
  return -1;
154
}
155
156
157
/**
158
  Set field to NULL or TIMESTAMP or to next auto_increment number.
159
160
  @param field           Field to update
161
  @param no_conversions  Set to 1 if we should return 1 if field can't
162
                         take null values.
163
                         If set to 0 we will do store the 'default value'
164
                         if the field is a special field. If not we will
165
                         give an error.
166
167
  @retval
168
    0    Field could take 0 or an automatic conversion was used
169
  @retval
170
    -1   Field could not take NULL and no conversion was used.
171
    If no_conversion was not set, an error message is printed
172
*/
173
174
int
175
set_field_to_null_with_conversions(Field *field, bool no_conversions)
176
{
177
  if (field->real_maybe_null())
178
  {
179
    field->set_null();
180
    field->reset();
181
    return 0;
182
  }
183
  if (no_conversions)
184
    return -1;
185
186
  /*
187
    Check if this is a special type, which will get a special walue
188
    when set to NULL (TIMESTAMP fields which allow setting to NULL
189
    are handled by first check).
190
  */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
191
  if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
1 by brian
clean slate
192
  {
193
    ((Field_timestamp*) field)->set_time();
194
    return 0;					// Ok to set time to NULL
195
  }
196
  field->reset();
1660.1.3 by Brian Aker
Encapsulate Table in field
197
  if (field == field->getTable()->next_number_field)
1 by brian
clean slate
198
  {
1660.1.3 by Brian Aker
Encapsulate Table in field
199
    field->getTable()->auto_increment_field_not_null= false;
1 by brian
clean slate
200
    return 0;				  // field is set in fill_record()
201
  }
1660.1.3 by Brian Aker
Encapsulate Table in field
202
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
685.4.2 by Jay Pipes
Added back truncation warnings. The NULL error is triggered from: session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL, not CHECK_FIELD_WARN
203
  {
204
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
205
    return 0;
206
  }
1660.1.3 by Brian Aker
Encapsulate Table in field
207
  if (!field->getTable()->in_use->no_errors)
1 by brian
clean slate
208
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
209
  return -1;
210
}
211
212
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
213
static void do_skip(CopyField *)
1 by brian
clean slate
214
{
215
}
216
217
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
218
static void do_copy_null(CopyField *copy)
1 by brian
clean slate
219
{
220
  if (*copy->from_null_ptr & copy->from_bit)
221
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
222
    *copy->to_null_ptr|= copy->to_bit;
1 by brian
clean slate
223
    copy->to_field->reset();
224
  }
225
  else
226
  {
227
    *copy->to_null_ptr&= ~copy->to_bit;
228
    (copy->do_copy2)(copy);
229
  }
230
}
231
232
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
233
static void do_outer_field_null(CopyField *copy)
1 by brian
clean slate
234
{
235
  if (*copy->null_row ||
236
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
237
  {
238
    *copy->to_null_ptr|=copy->to_bit;
239
    copy->to_field->reset();
240
  }
241
  else
242
  {
243
    *copy->to_null_ptr&= ~copy->to_bit;
244
    (copy->do_copy2)(copy);
245
  }
246
}
247
248
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
249
static void do_copy_not_null(CopyField *copy)
1 by brian
clean slate
250
{
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
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)
1 by brian
clean slate
256
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
257
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
258
                                ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
259
    copy->to_field->reset();
260
  }
261
  else
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
262
  {
1 by brian
clean slate
263
    (copy->do_copy2)(copy);
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
264
  }
1 by brian
clean slate
265
}
266
267
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
268
static void do_copy_maybe_null(CopyField *copy)
1 by brian
clean slate
269
{
270
  *copy->to_null_ptr&= ~copy->to_bit;
271
  (copy->do_copy2)(copy);
272
}
273
274
/* timestamp and next_number has special handling in case of NULL values */
275
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
276
static void do_copy_timestamp(CopyField *copy)
1 by brian
clean slate
277
{
278
  if (*copy->from_null_ptr & copy->from_bit)
279
  {
280
    /* Same as in set_field_to_null_with_conversions() */
281
    ((Field_timestamp*) copy->to_field)->set_time();
282
  }
283
  else
284
    (copy->do_copy2)(copy);
285
}
286
287
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
288
static void do_copy_next_number(CopyField *copy)
1 by brian
clean slate
289
{
290
  if (*copy->from_null_ptr & copy->from_bit)
291
  {
292
    /* Same as in set_field_to_null_with_conversions() */
1660.1.3 by Brian Aker
Encapsulate Table in field
293
    copy->to_field->getTable()->auto_increment_field_not_null= false;
1 by brian
clean slate
294
    copy->to_field->reset();
295
  }
296
  else
297
    (copy->do_copy2)(copy);
298
}
299
300
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
301
static void do_copy_blob(CopyField *copy)
1 by brian
clean slate
302
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
303
  ulong length= ((Field_blob*) copy->from_field)->get_length();
1 by brian
clean slate
304
  ((Field_blob*) copy->to_field)->store_length(length);
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
305
  memcpy(copy->to_ptr, copy->from_ptr, sizeof(char*));
1 by brian
clean slate
306
}
307
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
308
static void do_conv_blob(CopyField *copy)
1 by brian
clean slate
309
{
310
  copy->from_field->val_str(&copy->tmp);
311
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
312
                                         copy->tmp.length(),
313
                                         copy->tmp.charset());
1 by brian
clean slate
314
}
315
316
/** Save blob in copy->tmp for GROUP BY. */
317
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
318
static void do_save_blob(CopyField *copy)
1 by brian
clean slate
319
{
320
  char buff[MAX_FIELD_WIDTH];
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
321
  String res(buff, sizeof(buff), copy->tmp.charset());
1 by brian
clean slate
322
  copy->from_field->val_str(&res);
323
  copy->tmp.copy(res);
324
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
325
                                         copy->tmp.length(),
326
                                         copy->tmp.charset());
1 by brian
clean slate
327
}
328
329
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
330
static void do_field_string(CopyField *copy)
1 by brian
clean slate
331
{
332
  char buff[MAX_FIELD_WIDTH];
333
  copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
334
  copy->from_field->val_str(&copy->tmp);
335
  copy->to_field->store(copy->tmp.c_ptr_quick(),copy->tmp.length(),
336
                        copy->tmp.charset());
337
}
338
339
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
340
static void do_field_enum(CopyField *copy)
1 by brian
clean slate
341
{
342
  if (copy->from_field->val_int() == 0)
151 by Brian Aker
Ulonglong to uint64_t
343
    ((Field_enum *) copy->to_field)->store_type((uint64_t) 0);
1 by brian
clean slate
344
  else
345
    do_field_string(copy);
346
}
347
348
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
349
static void do_field_int(CopyField *copy)
1 by brian
clean slate
350
{
152 by Brian Aker
longlong replacement
351
  int64_t value= copy->from_field->val_int();
1 by brian
clean slate
352
  copy->to_field->store(value,
353
                        test(copy->from_field->flags & UNSIGNED_FLAG));
354
}
355
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
356
static void do_field_real(CopyField *copy)
1 by brian
clean slate
357
{
358
  double value=copy->from_field->val_real();
359
  copy->to_field->store(value);
360
}
361
362
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
363
static void do_field_decimal(CopyField *copy)
1 by brian
clean slate
364
{
365
  my_decimal value;
366
  copy->to_field->store_decimal(copy->from_field->val_decimal(&value));
367
}
368
369
370
/**
371
  string copy for single byte characters set when to string is shorter than
372
  from string.
373
*/
374
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
375
static void do_cut_string(CopyField *copy)
1 by brian
clean slate
376
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
377
  const CHARSET_INFO * const cs= copy->from_field->charset();
1 by brian
clean slate
378
  memcpy(copy->to_ptr,copy->from_ptr,copy->to_length);
379
380
  /* Check if we loosed any important characters */
381
  if (cs->cset->scan(cs,
382
                     (char*) copy->from_ptr + copy->to_length,
383
                     (char*) copy->from_ptr + copy->from_length,
384
                     MY_SEQ_SPACES) < copy->from_length - copy->to_length)
385
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
386
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
387
                                ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
388
  }
389
}
390
391
392
/**
393
  string copy for multi byte characters set when to string is shorter than
394
  from string.
395
*/
396
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
397
static void do_cut_string_complex(CopyField *copy)
1 by brian
clean slate
398
{						// Shorter string field
399
  int well_formed_error;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
400
  const CHARSET_INFO * const cs= copy->from_field->charset();
481 by Brian Aker
Remove all of uchar.
401
  const unsigned char *from_end= copy->from_ptr + copy->from_length;
482 by Brian Aker
Remove uint.
402
  uint32_t copy_length= cs->cset->well_formed_len(cs,
1 by brian
clean slate
403
                                              (char*) copy->from_ptr,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
404
                                              (char*) from_end,
1 by brian
clean slate
405
                                              copy->to_length / cs->mbmaxlen,
406
                                              &well_formed_error);
407
  if (copy->to_length < copy_length)
408
    copy_length= copy->to_length;
409
  memcpy(copy->to_ptr, copy->from_ptr, copy_length);
410
411
  /* Check if we lost any important characters */
412
  if (well_formed_error ||
413
      cs->cset->scan(cs, (char*) copy->from_ptr + copy_length,
414
                     (char*) from_end,
415
                     MY_SEQ_SPACES) < (copy->from_length - copy_length))
416
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
417
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
418
                                ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
419
  }
420
421
  if (copy_length < copy->to_length)
422
    cs->cset->fill(cs, (char*) copy->to_ptr + copy_length,
423
                   copy->to_length - copy_length, ' ');
424
}
425
426
427
428
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
429
static void do_expand_binary(CopyField *copy)
1 by brian
clean slate
430
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
431
  const CHARSET_INFO * const cs= copy->from_field->charset();
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
432
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
433
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
434
                 copy->to_length-copy->from_length, '\0');
1 by brian
clean slate
435
}
436
437
438
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
439
static void do_expand_string(CopyField *copy)
1 by brian
clean slate
440
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
441
  const CHARSET_INFO * const cs= copy->from_field->charset();
1 by brian
clean slate
442
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
443
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
444
                 copy->to_length-copy->from_length, ' ');
1 by brian
clean slate
445
}
446
447
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
448
static void do_varstring1(CopyField *copy)
1 by brian
clean slate
449
{
895 by Brian Aker
Completion (?) of uint conversion.
450
  uint32_t length= (uint32_t) *(unsigned char*) copy->from_ptr;
1 by brian
clean slate
451
  if (length > copy->to_length- 1)
452
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
453
    length= copy->to_length - 1;
1660.1.3 by Brian Aker
Encapsulate Table in field
454
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
455
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
456
                                  ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
457
  }
481 by Brian Aker
Remove all of uchar.
458
  *(unsigned char*) copy->to_ptr= (unsigned char) length;
1 by brian
clean slate
459
  memcpy(copy->to_ptr+1, copy->from_ptr + 1, length);
460
}
461
462
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
463
static void do_varstring1_mb(CopyField *copy)
1 by brian
clean slate
464
{
465
  int well_formed_error;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
466
  const CHARSET_INFO * const cs= copy->from_field->charset();
895 by Brian Aker
Completion (?) of uint conversion.
467
  uint32_t from_length= (uint32_t) *(unsigned char*) copy->from_ptr;
481 by Brian Aker
Remove all of uchar.
468
  const unsigned char *from_ptr= copy->from_ptr + 1;
482 by Brian Aker
Remove uint.
469
  uint32_t to_char_length= (copy->to_length - 1) / cs->mbmaxlen;
470
  uint32_t length= cs->cset->well_formed_len(cs, (char*) from_ptr,
1 by brian
clean slate
471
                                         (char*) from_ptr + from_length,
472
                                         to_char_length, &well_formed_error);
473
  if (length < from_length)
474
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
475
    if (current_session->count_cuted_fields)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
476
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
477
                                  ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
478
  }
481 by Brian Aker
Remove all of uchar.
479
  *copy->to_ptr= (unsigned char) length;
1 by brian
clean slate
480
  memcpy(copy->to_ptr + 1, from_ptr, length);
481
}
482
483
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
484
static void do_varstring2(CopyField *copy)
1 by brian
clean slate
485
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
486
  uint32_t length= uint2korr(copy->from_ptr);
1 by brian
clean slate
487
  if (length > copy->to_length- HA_KEY_BLOB_LENGTH)
488
  {
489
    length=copy->to_length-HA_KEY_BLOB_LENGTH;
1660.1.3 by Brian Aker
Encapsulate Table in field
490
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
491
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
492
                                  ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
493
  }
494
  int2store(copy->to_ptr,length);
495
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, copy->from_ptr + HA_KEY_BLOB_LENGTH,
496
         length);
497
}
498
499
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
500
static void do_varstring2_mb(CopyField *copy)
1 by brian
clean slate
501
{
502
  int well_formed_error;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
503
  const CHARSET_INFO * const cs= copy->from_field->charset();
482 by Brian Aker
Remove uint.
504
  uint32_t char_length= (copy->to_length - HA_KEY_BLOB_LENGTH) / cs->mbmaxlen;
505
  uint32_t from_length= uint2korr(copy->from_ptr);
481 by Brian Aker
Remove all of uchar.
506
  const unsigned char *from_beg= copy->from_ptr + HA_KEY_BLOB_LENGTH;
482 by Brian Aker
Remove uint.
507
  uint32_t length= cs->cset->well_formed_len(cs, (char*) from_beg,
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
508
                                             (char*) from_beg + from_length,
509
                                             char_length, &well_formed_error);
1 by brian
clean slate
510
  if (length < from_length)
511
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
512
    if (current_session->count_cuted_fields)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
513
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
514
                                  ER_WARN_DATA_TRUNCATED, 1);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
515
  }
1 by brian
clean slate
516
  int2store(copy->to_ptr, length);
517
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, from_beg, length);
518
}
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
519
1 by brian
clean slate
520
521
/***************************************************************************
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
522
** The different functions that fills in a CopyField class
1 by brian
clean slate
523
***************************************************************************/
524
525
/**
526
  copy of field to maybe null string.
527
  If field is null then the all bytes are set to 0.
528
  if field is not null then the first byte is set to 1 and the rest of the
529
  string is the field value.
530
  The 'to' buffer should have a size of field->pack_length()+1
531
*/
532
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
533
void CopyField::set(unsigned char *to,Field *from)
1 by brian
clean slate
534
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
535
  from_ptr= from->ptr;
536
  to_ptr= to;
537
  from_length= from->pack_length();
1 by brian
clean slate
538
  if (from->maybe_null())
539
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
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;
1660.1.3 by Brian Aker
Encapsulate Table in field
545
    if (from->getTable()->maybe_null)
1 by brian
clean slate
546
    {
1660.1.3 by Brian Aker
Encapsulate Table in field
547
      null_row= &from->getTable()->null_row;
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
548
      do_copy= do_outer_field_to_null_str;
1 by brian
clean slate
549
    }
550
    else
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
551
      do_copy= do_field_to_null_str;
1 by brian
clean slate
552
  }
553
  else
554
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
555
    to_null_ptr= 0;                           // For easy debugging
556
    do_copy= do_field_eq;
1 by brian
clean slate
557
  }
558
}
559
560
561
/*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
562
  To do:
1 by brian
clean slate
563
564
  If 'save\ is set to true and the 'from' is a blob field, do_copy is set to
565
  do_save_blob rather than do_conv_blob.  The only differences between them
566
  appears to be:
567
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
568
  - do_save_blob allocates and uses an intermediate buffer before calling
569
    Field_blob::store. Is this in order to trigger the call to
1 by brian
clean slate
570
    well_formed_copy_nchars, by changing the pointer copy->tmp.ptr()?
571
    That call will take place anyway in all known cases.
572
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
573
  - The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended
1 by brian
clean slate
574
    effect? Truncation is handled by well_formed_copy_nchars anyway.
575
 */
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
576
void CopyField::set(Field *to,Field *from,bool save)
1 by brian
clean slate
577
{
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
578
  if (to->type() == DRIZZLE_TYPE_NULL)
1 by brian
clean slate
579
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
580
    to_null_ptr= 0;           // For easy debugging
581
    to_ptr= 0;
582
    do_copy= do_skip;
1 by brian
clean slate
583
    return;
584
  }
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
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();
1 by brian
clean slate
591
592
  // set up null handling
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
593
  from_null_ptr= to_null_ptr= 0;
1 by brian
clean slate
594
  if (from->maybe_null())
595
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
596
    from_null_ptr= from->null_ptr;
597
    from_bit= from->null_bit;
1 by brian
clean slate
598
    if (to_field->real_maybe_null())
599
    {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
600
      to_null_ptr= to->null_ptr;
601
      to_bit= to->null_bit;
1 by brian
clean slate
602
      if (from_null_ptr)
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
603
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
604
        do_copy= do_copy_null;
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
605
      }
1 by brian
clean slate
606
      else
607
      {
1660.1.3 by Brian Aker
Encapsulate Table in field
608
        null_row= &from->getTable()->null_row;
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
609
        do_copy= do_outer_field_null;
1 by brian
clean slate
610
      }
611
    }
612
    else
613
    {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
614
      if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
615
      {
1 by brian
clean slate
616
        do_copy= do_copy_timestamp;               // Automatic timestamp
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
617
      }
1660.1.3 by Brian Aker
Encapsulate Table in field
618
      else if (to_field == to_field->getTable()->next_number_field)
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
619
      {
1 by brian
clean slate
620
        do_copy= do_copy_next_number;
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
621
      }
1 by brian
clean slate
622
      else
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
623
      {
1 by brian
clean slate
624
        do_copy= do_copy_not_null;
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
625
      }
1 by brian
clean slate
626
    }
627
  }
628
  else if (to_field->real_maybe_null())
629
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
630
    to_null_ptr= to->null_ptr;
631
    to_bit= to->null_bit;
1 by brian
clean slate
632
    do_copy= do_copy_maybe_null;
633
  }
634
  else
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
635
   do_copy= 0;
1 by brian
clean slate
636
637
  if ((to->flags & BLOB_FLAG) && save)
638
    do_copy2= do_save_blob;
639
  else
640
    do_copy2= get_copy_func(to,from);
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
641
  if (!do_copy)         // Not null
642
    do_copy= do_copy2;
1 by brian
clean slate
643
}
644
645
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
646
CopyField::Copy_func *
647
CopyField::get_copy_func(Field *to,Field *from)
1 by brian
clean slate
648
{
1660.1.3 by Brian Aker
Encapsulate Table in field
649
  bool compatible_db_low_byte_first= (to->getTable()->getShare()->db_low_byte_first ==
650
                                     from->getTable()->getShare()->db_low_byte_first);
1 by brian
clean slate
651
  if (to->flags & BLOB_FLAG)
652
  {
653
    if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
654
      return do_conv_blob;
655
    if (from_length != to_length || !compatible_db_low_byte_first)
656
    {
657
      // Correct pointer to point at char pointer
1660.1.3 by Brian Aker
Encapsulate Table in field
658
      to_ptr+= to_length - to->getTable()->getShare()->blob_ptr_size;
659
      from_ptr+= from_length- from->getTable()->getShare()->blob_ptr_size;
1 by brian
clean slate
660
      return do_copy_blob;
661
    }
662
  }
663
  else
664
  {
665
    if (to->result_type() == DECIMAL_RESULT)
666
      return do_field_decimal;
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
667
      
1 by brian
clean slate
668
    // Check if identical fields
669
    if (from->result_type() == STRING_RESULT)
670
    {
671
      /*
672
        If we are copying date or datetime's we have to check the dates
673
        if we don't allow 'all' dates.
674
      */
675
      if (to->real_type() != from->real_type() ||
676
          !compatible_db_low_byte_first ||
1660.1.3 by Brian Aker
Encapsulate Table in field
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))
1 by brian
clean slate
678
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
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
        }
1 by brian
clean slate
688
      }
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
689
      
325 by Brian Aker
Remove SET
690
      if (to->real_type() == DRIZZLE_TYPE_ENUM)
1 by brian
clean slate
691
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
692
        if (!to->eq_def(from))
1 by brian
clean slate
693
        {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
694
          if (from->real_type() == DRIZZLE_TYPE_ENUM &&
695
              to->real_type() == DRIZZLE_TYPE_ENUM)
1 by brian
clean slate
696
            return do_field_enum;
697
          else
698
            return do_field_string;
699
        }
700
      }
701
      else if (to->charset() != from->charset())
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
702
        return do_field_string;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
703
      else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
1 by brian
clean slate
704
      {
1267.1.7 by Stewart Smith
in field_conv.cc, get_copy_func should not try to cast Field_blob to Field_varstring as Field_ blob does not inherit from field_varstring. Previously this kinda worked out due to the first member of field_blob kinda making sense in this comparison. Instead, we just shortcut to that decision if we are converting from BLOB.
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
1267.1.8 by Stewart Smith
change dynamic_cast in field_conv.cc back to static_cast for runtime performance
716
        if ((static_cast<Field_varstring*>(to))->pack_length_no_ptr() !=
717
            (static_cast<Field_varstring*>(from))->pack_length_no_ptr())
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
718
        {
1 by brian
clean slate
719
          return do_field_string;
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
720
        }
721
        
1 by brian
clean slate
722
        if (to_length != from_length)
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
723
        {
1782.4.5 by Brian Aker
Encapsulate the length of the bytes in the varchar header.
724
          return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
1 by brian
clean slate
725
                  (from->charset()->mbmaxlen == 1 ? do_varstring1 :
726
                                                    do_varstring1_mb) :
727
                  (from->charset()->mbmaxlen == 1 ? do_varstring2 :
728
                                                    do_varstring2_mb));
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
729
        }
1 by brian
clean slate
730
      }
731
      else if (to_length < from_length)
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
732
      {
733
        return (from->charset()->mbmaxlen == 1 ?
1 by brian
clean slate
734
                do_cut_string : do_cut_string_complex);
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
735
      }
1 by brian
clean slate
736
      else if (to_length > from_length)
737
      {
738
        if (to->charset() == &my_charset_bin)
739
          return do_expand_binary;
740
        else
741
          return do_expand_string;
742
      }
743
    }
744
    else if (to->real_type() != from->real_type() ||
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
745
             to_length != from_length ||
1 by brian
clean slate
746
             !compatible_db_low_byte_first)
747
    {
80 by Brian Aker
Most of the removal of dead DECIMAL type
748
      if (to->result_type() == STRING_RESULT)
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
749
        return do_field_string;
1 by brian
clean slate
750
      if (to->result_type() == INT_RESULT)
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
751
        return do_field_int;
752
1 by brian
clean slate
753
      return do_field_real;
754
    }
755
    else
756
    {
757
      if (!to->eq_def(from) || !compatible_db_low_byte_first)
758
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
759
        if (to->result_type() == INT_RESULT)
760
          return do_field_int;
761
        else
762
          return do_field_real;
1 by brian
clean slate
763
      }
764
    }
765
  }
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
766
  
1 by brian
clean slate
767
    /* Eq fields */
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
768
  switch (to_length)
769
  {
1 by brian
clean slate
770
  case 1: return do_field_1;
771
  case 2: return do_field_2;
772
  case 3: return do_field_3;
773
  case 4: return do_field_4;
774
  case 6: return do_field_6;
775
  case 8: return do_field_8;
776
  }
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
777
  
1 by brian
clean slate
778
  return do_field_eq;
779
}
780
781
782
/** Simple quick field convert that is called on insert. */
783
784
int field_conv(Field *to,Field *from)
785
{
786
  if (to->real_type() == from->real_type() &&
1660.1.3 by Brian Aker
Encapsulate Table in field
787
      !(to->type() == DRIZZLE_TYPE_BLOB && to->getTable()->copy_blobs))
1 by brian
clean slate
788
  {
789
    /* Please god, will someone rewrite this to be readable :( */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
790
    if (to->pack_length() == from->pack_length() &&
791
        !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
792
        to->real_type() != DRIZZLE_TYPE_ENUM &&
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
793
        (to->real_type() != DRIZZLE_TYPE_DECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
1 by brian
clean slate
794
        from->charset() == to->charset() &&
1660.1.3 by Brian Aker
Encapsulate Table in field
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)) &&
1782.4.5 by Brian Aker
Encapsulate the length of the bytes in the varchar header.
797
        (from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->pack_length_no_ptr() == ((Field_varstring*)to)->pack_length_no_ptr()))
1 by brian
clean slate
798
    {						// Identical fields
799
      /* This may happen if one does 'UPDATE ... SET x=x' */
800
      if (to->ptr != from->ptr)
801
        memcpy(to->ptr,from->ptr,to->pack_length());
802
      return 0;
803
    }
804
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
805
  if (to->type() == DRIZZLE_TYPE_BLOB)
1 by brian
clean slate
806
  {						// Be sure the value is stored
807
    Field_blob *blob=(Field_blob*) to;
808
    from->val_str(&blob->value);
809
    /*
810
      Copy value if copy_blobs is set, or source is not a string and
811
      we have a pointer to its internal string conversion buffer.
812
    */
1660.1.3 by Brian Aker
Encapsulate Table in field
813
    if (to->getTable()->copy_blobs ||
1 by brian
clean slate
814
        (!blob->value.is_alloced() &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
815
         from->real_type() != DRIZZLE_TYPE_VARCHAR))
1 by brian
clean slate
816
      blob->value.copy();
817
    return blob->store(blob->value.ptr(),blob->value.length(),from->charset());
818
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
819
  if (from->real_type() == DRIZZLE_TYPE_ENUM &&
820
      to->real_type() == DRIZZLE_TYPE_ENUM &&
1 by brian
clean slate
821
      from->val_int() == 0)
822
  {
823
    ((Field_enum *)(to))->store_type(0);
824
    return 0;
825
  }
826
  else if ((from->result_type() == STRING_RESULT &&
827
            (to->result_type() == STRING_RESULT ||
325 by Brian Aker
Remove SET
828
             (from->real_type() != DRIZZLE_TYPE_ENUM))))
1 by brian
clean slate
829
  {
830
    char buff[MAX_FIELD_WIDTH];
831
    String result(buff,sizeof(buff),from->charset());
832
    from->val_str(&result);
833
    /*
834
      We use c_ptr_quick() here to make it easier if to is a float/double
835
      as the conversion routines will do a copy of the result doesn't
836
      end with \0. Can be replaced with .ptr() when we have our own
837
      string->double conversion.
838
    */
839
    return to->store(result.c_ptr_quick(),result.length(),from->charset());
840
  }
841
  else if (from->result_type() == REAL_RESULT)
842
    return to->store(from->val_real());
843
  else if (from->result_type() == DECIMAL_RESULT)
844
  {
845
    my_decimal buff;
846
    return to->store_decimal(from->val_decimal(&buff));
847
  }
848
  else
849
    return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG));
850
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
851
852
} /* namespace drizzled */