~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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
27
#include <config.h>
2154.2.11 by Brian Aker
Further strip out includes.
28
550 by Monty Taylor
Moved error.h into just the files that need it.
29
#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.
30
#include <drizzled/table.h>
31
#include <drizzled/session.h>
2154.2.24 by Brian Aker
Merge in all changes for current_session, etc.
32
#include <drizzled/current_session.h>
1 by brian
clean slate
33
2154.2.11 by Brian Aker
Further strip out includes.
34
#include <drizzled/copy_field.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
35
#include <drizzled/field/blob.h>
36
#include <drizzled/field/date.h>
2154.2.11 by Brian Aker
Further strip out includes.
37
#include <drizzled/field/datetime.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/double.h>
2154.2.11 by Brian Aker
Further strip out includes.
40
#include <drizzled/field/enum.h>
41
#include <drizzled/field/epoch.h>
2007 by Brian Aker
Refactor naming for integers.
42
#include <drizzled/field/int32.h>
43
#include <drizzled/field/int64.h>
2154.2.11 by Brian Aker
Further strip out includes.
44
#include <drizzled/field/null.h>
45
#include <drizzled/field/num.h>
46
#include <drizzled/field/num.h>
47
#include <drizzled/field/real.h>
48
#include <drizzled/field/str.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
49
#include <drizzled/field/varstring.h>
2239.1.9 by Olaf van der Spek
Refactor includes
50
#include <drizzled/util/test.h>
2241.3.2 by Olaf van der Spek
Refactor Session::variables
51
#include <drizzled/system_variables.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
52
2239.1.9 by Olaf van der Spek
Refactor includes
53
namespace drizzled {
584.5.1 by Monty Taylor
Removed field includes from field.h.
54
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
55
static void do_field_eq(CopyField *copy)
1 by brian
clean slate
56
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
57
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
58
}
59
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
60
static void do_field_1(CopyField *copy)
1 by brian
clean slate
61
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
62
  copy->to_ptr[0]= copy->from_ptr[0];
1 by brian
clean slate
63
}
64
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
65
static void do_field_2(CopyField *copy)
1 by brian
clean slate
66
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
67
  copy->to_ptr[0]= copy->from_ptr[0];
68
  copy->to_ptr[1]= copy->from_ptr[1];
1 by brian
clean slate
69
}
70
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
71
static void do_field_3(CopyField *copy)
1 by brian
clean slate
72
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
73
  copy->to_ptr[0]= copy->from_ptr[0];
74
  copy->to_ptr[1]= copy->from_ptr[1];
75
  copy->to_ptr[2]= copy->from_ptr[2];
1 by brian
clean slate
76
}
77
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
78
static void do_field_4(CopyField *copy)
1 by brian
clean slate
79
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
80
  copy->to_ptr[0]= copy->from_ptr[0];
81
  copy->to_ptr[1]= copy->from_ptr[1];
82
  copy->to_ptr[2]= copy->from_ptr[2];
83
  copy->to_ptr[3]= copy->from_ptr[3];
1 by brian
clean slate
84
}
85
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
86
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.
87
{                  // For blob field
88
  copy->to_ptr[0]= copy->from_ptr[0];
89
  copy->to_ptr[1]= copy->from_ptr[1];
90
  copy->to_ptr[2]= copy->from_ptr[2];
91
  copy->to_ptr[3]= copy->from_ptr[3];
92
  copy->to_ptr[4]= copy->from_ptr[4];
93
  copy->to_ptr[5]= copy->from_ptr[5];
1 by brian
clean slate
94
}
95
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
96
static void do_field_8(CopyField *copy)
1 by brian
clean slate
97
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
98
  copy->to_ptr[0]= copy->from_ptr[0];
99
  copy->to_ptr[1]= copy->from_ptr[1];
100
  copy->to_ptr[2]= copy->from_ptr[2];
101
  copy->to_ptr[3]= copy->from_ptr[3];
102
  copy->to_ptr[4]= copy->from_ptr[4];
103
  copy->to_ptr[5]= copy->from_ptr[5];
104
  copy->to_ptr[6]= copy->from_ptr[6];
105
  copy->to_ptr[7]= copy->from_ptr[7];
1 by brian
clean slate
106
}
107
108
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
109
static void do_field_to_null_str(CopyField *copy)
1 by brian
clean slate
110
{
111
  if (*copy->from_null_ptr & copy->from_bit)
112
  {
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
113
    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.
114
    copy->to_null_ptr[0]= 1;  // Always bit 1
1 by brian
clean slate
115
  }
116
  else
117
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
118
    copy->to_null_ptr[0]= 0;
119
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
120
  }
121
}
122
123
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
124
static void do_outer_field_to_null_str(CopyField *copy)
1 by brian
clean slate
125
{
126
  if (*copy->null_row ||
127
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
128
  {
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
129
    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.
130
    copy->to_null_ptr[0]= 1;  // Always bit 1
1 by brian
clean slate
131
  }
132
  else
133
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
134
    copy->to_null_ptr[0]= 0;
135
    memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
136
  }
137
}
138
139
140
int
141
set_field_to_null(Field *field)
142
{
143
  if (field->real_maybe_null())
144
  {
145
    field->set_null();
146
    field->reset();
147
    return 0;
148
  }
149
  field->reset();
1660.1.3 by Brian Aker
Encapsulate Table in field
150
  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
151
  {
152
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
153
    return 0;
154
  }
1660.1.3 by Brian Aker
Encapsulate Table in field
155
  if (!field->getTable()->in_use->no_errors)
1 by brian
clean slate
156
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
157
  return -1;
158
}
159
160
161
/**
162
  Set field to NULL or TIMESTAMP or to next auto_increment number.
163
164
  @param field           Field to update
165
  @param no_conversions  Set to 1 if we should return 1 if field can't
166
                         take null values.
167
                         If set to 0 we will do store the 'default value'
168
                         if the field is a special field. If not we will
169
                         give an error.
170
171
  @retval
172
    0    Field could take 0 or an automatic conversion was used
173
  @retval
174
    -1   Field could not take NULL and no conversion was used.
175
    If no_conversion was not set, an error message is printed
176
*/
177
178
int
179
set_field_to_null_with_conversions(Field *field, bool no_conversions)
180
{
181
  if (field->real_maybe_null())
182
  {
183
    field->set_null();
184
    field->reset();
185
    return 0;
186
  }
1999.4.9 by Brian Aker
Created EPOCH
187
1 by brian
clean slate
188
  if (no_conversions)
189
    return -1;
190
191
  /*
192
    Check if this is a special type, which will get a special walue
193
    when set to NULL (TIMESTAMP fields which allow setting to NULL
194
    are handled by first check).
195
  */
2046.2.1 by Brian Aker
First pass on micro timestamp.
196
  if (field->is_timestamp())
1 by brian
clean slate
197
  {
1999.4.9 by Brian Aker
Created EPOCH
198
    ((field::Epoch::pointer) field)->set_time();
1 by brian
clean slate
199
    return 0;					// Ok to set time to NULL
200
  }
1999.4.9 by Brian Aker
Created EPOCH
201
1 by brian
clean slate
202
  field->reset();
1660.1.3 by Brian Aker
Encapsulate Table in field
203
  if (field == field->getTable()->next_number_field)
1 by brian
clean slate
204
  {
1660.1.3 by Brian Aker
Encapsulate Table in field
205
    field->getTable()->auto_increment_field_not_null= false;
1 by brian
clean slate
206
    return 0;				  // field is set in fill_record()
207
  }
1999.4.9 by Brian Aker
Created EPOCH
208
1660.1.3 by Brian Aker
Encapsulate Table in field
209
  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
210
  {
211
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
212
    return 0;
213
  }
1999.4.9 by Brian Aker
Created EPOCH
214
1660.1.3 by Brian Aker
Encapsulate Table in field
215
  if (!field->getTable()->in_use->no_errors)
1 by brian
clean slate
216
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
1999.4.9 by Brian Aker
Created EPOCH
217
1 by brian
clean slate
218
  return -1;
219
}
220
221
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
222
static void do_skip(CopyField *)
1 by brian
clean slate
223
{
224
}
225
226
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
227
static void do_copy_null(CopyField *copy)
1 by brian
clean slate
228
{
229
  if (*copy->from_null_ptr & copy->from_bit)
230
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
231
    *copy->to_null_ptr|= copy->to_bit;
1 by brian
clean slate
232
    copy->to_field->reset();
233
  }
234
  else
235
  {
236
    *copy->to_null_ptr&= ~copy->to_bit;
237
    (copy->do_copy2)(copy);
238
  }
239
}
240
241
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
242
static void do_outer_field_null(CopyField *copy)
1 by brian
clean slate
243
{
244
  if (*copy->null_row ||
245
      (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
246
  {
247
    *copy->to_null_ptr|=copy->to_bit;
248
    copy->to_field->reset();
249
  }
250
  else
251
  {
252
    *copy->to_null_ptr&= ~copy->to_bit;
253
    (copy->do_copy2)(copy);
254
  }
255
}
256
257
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
258
static void do_copy_not_null(CopyField *copy)
1 by brian
clean slate
259
{
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
260
  if (copy->to_field->hasDefault() and *copy->from_null_ptr & copy->from_bit)
261
  {
262
    copy->to_field->set_default();
263
  }
264
  else if (*copy->from_null_ptr & copy->from_bit)
1 by brian
clean slate
265
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
266
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
267
                                ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
268
    copy->to_field->reset();
269
  }
270
  else
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
271
  {
1 by brian
clean slate
272
    (copy->do_copy2)(copy);
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
273
  }
1 by brian
clean slate
274
}
275
276
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
277
static void do_copy_maybe_null(CopyField *copy)
1 by brian
clean slate
278
{
279
  *copy->to_null_ptr&= ~copy->to_bit;
280
  (copy->do_copy2)(copy);
281
}
282
283
/* timestamp and next_number has special handling in case of NULL values */
284
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
285
static void do_copy_timestamp(CopyField *copy)
1 by brian
clean slate
286
{
287
  if (*copy->from_null_ptr & copy->from_bit)
288
  {
289
    /* Same as in set_field_to_null_with_conversions() */
1999.4.9 by Brian Aker
Created EPOCH
290
    ((field::Epoch::pointer) copy->to_field)->set_time();
1 by brian
clean slate
291
  }
292
  else
1999.4.9 by Brian Aker
Created EPOCH
293
  {
1 by brian
clean slate
294
    (copy->do_copy2)(copy);
1999.4.9 by Brian Aker
Created EPOCH
295
  }
1 by brian
clean slate
296
}
297
298
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
299
static void do_copy_next_number(CopyField *copy)
1 by brian
clean slate
300
{
301
  if (*copy->from_null_ptr & copy->from_bit)
302
  {
303
    /* Same as in set_field_to_null_with_conversions() */
1660.1.3 by Brian Aker
Encapsulate Table in field
304
    copy->to_field->getTable()->auto_increment_field_not_null= false;
1 by brian
clean slate
305
    copy->to_field->reset();
306
  }
307
  else
1999.4.9 by Brian Aker
Created EPOCH
308
  {
1 by brian
clean slate
309
    (copy->do_copy2)(copy);
1999.4.9 by Brian Aker
Created EPOCH
310
  }
1 by brian
clean slate
311
}
312
313
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
314
static void do_copy_blob(CopyField *copy)
1 by brian
clean slate
315
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
316
  ulong length= ((Field_blob*) copy->from_field)->get_length();
1 by brian
clean slate
317
  ((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.
318
  memcpy(copy->to_ptr, copy->from_ptr, sizeof(char*));
1 by brian
clean slate
319
}
320
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
321
static void do_conv_blob(CopyField *copy)
1 by brian
clean slate
322
{
1996.2.1 by Brian Aker
uuid type code.
323
  copy->from_field->val_str_internal(&copy->tmp);
1 by brian
clean slate
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
/** Save blob in copy->tmp for GROUP BY. */
330
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
331
static void do_save_blob(CopyField *copy)
1 by brian
clean slate
332
{
333
  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.
334
  String res(buff, sizeof(buff), copy->tmp.charset());
1996.2.1 by Brian Aker
uuid type code.
335
  copy->from_field->val_str_internal(&res);
1 by brian
clean slate
336
  copy->tmp.copy(res);
337
  ((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.
338
                                         copy->tmp.length(),
339
                                         copy->tmp.charset());
1 by brian
clean slate
340
}
341
342
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
343
static void do_field_string(CopyField *copy)
1 by brian
clean slate
344
{
345
  char buff[MAX_FIELD_WIDTH];
346
  copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
1996.2.1 by Brian Aker
uuid type code.
347
  copy->from_field->val_str_internal(&copy->tmp);
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
348
  copy->to_field->store(copy->tmp.data(),copy->tmp.length(), copy->tmp.charset());
1 by brian
clean slate
349
}
350
351
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
352
static void do_field_enum(CopyField *copy)
1 by brian
clean slate
353
{
354
  if (copy->from_field->val_int() == 0)
151 by Brian Aker
Ulonglong to uint64_t
355
    ((Field_enum *) copy->to_field)->store_type((uint64_t) 0);
1 by brian
clean slate
356
  else
357
    do_field_string(copy);
358
}
359
360
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
361
static void do_field_int(CopyField *copy)
1 by brian
clean slate
362
{
152 by Brian Aker
longlong replacement
363
  int64_t value= copy->from_field->val_int();
1 by brian
clean slate
364
  copy->to_field->store(value,
365
                        test(copy->from_field->flags & UNSIGNED_FLAG));
366
}
367
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
368
static void do_field_real(CopyField *copy)
1 by brian
clean slate
369
{
370
  double value=copy->from_field->val_real();
371
  copy->to_field->store(value);
372
}
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_field_decimal(CopyField *copy)
1 by brian
clean slate
376
{
2030.1.4 by Brian Aker
Change my_decimal to Decimal
377
  type::Decimal value;
1 by brian
clean slate
378
  copy->to_field->store_decimal(copy->from_field->val_decimal(&value));
379
}
380
381
382
/**
383
  string copy for single byte characters set when to string is shorter than
384
  from string.
385
*/
386
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
387
static void do_cut_string(CopyField *copy)
1 by brian
clean slate
388
{
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
389
  const charset_info_st * const cs= copy->from_field->charset();
2046.2.3 by Brian Aker
Add basic tests for microtime.
390
  memcpy(copy->to_ptr, copy->from_ptr, copy->to_length);
1 by brian
clean slate
391
392
  /* Check if we loosed any important characters */
393
  if (cs->cset->scan(cs,
394
                     (char*) copy->from_ptr + copy->to_length,
395
                     (char*) copy->from_ptr + copy->from_length,
396
                     MY_SEQ_SPACES) < copy->from_length - copy->to_length)
397
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
398
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
399
                                ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
400
  }
401
}
402
403
404
/**
405
  string copy for multi byte characters set when to string is shorter than
406
  from string.
407
*/
408
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
409
static void do_cut_string_complex(CopyField *copy)
1 by brian
clean slate
410
{						// Shorter string field
411
  int well_formed_error;
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
412
  const charset_info_st * const cs= copy->from_field->charset();
481 by Brian Aker
Remove all of uchar.
413
  const unsigned char *from_end= copy->from_ptr + copy->from_length;
2440.2.14 by Olaf van der Spek
Refactor
414
  uint32_t copy_length= cs->cset->well_formed_len(*cs, str_ref(copy->from_ptr, from_end), copy->to_length / cs->mbmaxlen, &well_formed_error);
1 by brian
clean slate
415
  if (copy->to_length < copy_length)
416
    copy_length= copy->to_length;
417
  memcpy(copy->to_ptr, copy->from_ptr, copy_length);
418
419
  /* Check if we lost any important characters */
420
  if (well_formed_error ||
421
      cs->cset->scan(cs, (char*) copy->from_ptr + copy_length,
422
                     (char*) from_end,
423
                     MY_SEQ_SPACES) < (copy->from_length - copy_length))
424
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
425
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
426
                                ER_WARN_DATA_TRUNCATED, 1);
1 by brian
clean slate
427
  }
428
429
  if (copy_length < copy->to_length)
430
    cs->cset->fill(cs, (char*) copy->to_ptr + copy_length,
431
                   copy->to_length - copy_length, ' ');
432
}
433
434
435
436
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
437
static void do_expand_binary(CopyField *copy)
1 by brian
clean slate
438
{
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
439
  const charset_info_st * 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.
440
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
1 by brian
clean slate
441
  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.
442
                 copy->to_length-copy->from_length, '\0');
1 by brian
clean slate
443
}
444
445
446
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
447
static void do_expand_string(CopyField *copy)
1 by brian
clean slate
448
{
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
449
  const charset_info_st * const cs= copy->from_field->charset();
1 by brian
clean slate
450
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
451
  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.
452
                 copy->to_length-copy->from_length, ' ');
1 by brian
clean slate
453
}
454
455
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
456
static void do_varstring1(CopyField *copy)
1 by brian
clean slate
457
{
895 by Brian Aker
Completion (?) of uint conversion.
458
  uint32_t length= (uint32_t) *(unsigned char*) copy->from_ptr;
1 by brian
clean slate
459
  if (length > copy->to_length- 1)
460
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
461
    length= copy->to_length - 1;
1660.1.3 by Brian Aker
Encapsulate Table in field
462
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
2046.2.3 by Brian Aker
Add basic tests for microtime.
463
    {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
464
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
465
                                  ER_WARN_DATA_TRUNCATED, 1);
2046.2.3 by Brian Aker
Add basic tests for microtime.
466
    }
1 by brian
clean slate
467
  }
481 by Brian Aker
Remove all of uchar.
468
  *(unsigned char*) copy->to_ptr= (unsigned char) length;
1 by brian
clean slate
469
  memcpy(copy->to_ptr+1, copy->from_ptr + 1, length);
470
}
471
472
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
473
static void do_varstring1_mb(CopyField *copy)
1 by brian
clean slate
474
{
475
  int well_formed_error;
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
476
  const charset_info_st * const cs= copy->from_field->charset();
895 by Brian Aker
Completion (?) of uint conversion.
477
  uint32_t from_length= (uint32_t) *(unsigned char*) copy->from_ptr;
481 by Brian Aker
Remove all of uchar.
478
  const unsigned char *from_ptr= copy->from_ptr + 1;
482 by Brian Aker
Remove uint.
479
  uint32_t to_char_length= (copy->to_length - 1) / cs->mbmaxlen;
2440.2.14 by Olaf van der Spek
Refactor
480
  uint32_t length= cs->cset->well_formed_len(*cs, str_ref(from_ptr, from_length), to_char_length, &well_formed_error);
1 by brian
clean slate
481
  if (length < from_length)
482
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
483
    if (current_session->count_cuted_fields)
2046.2.3 by Brian Aker
Add basic tests for microtime.
484
    {
2440.2.14 by Olaf van der Spek
Refactor
485
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
2046.2.3 by Brian Aker
Add basic tests for microtime.
486
    }
1 by brian
clean slate
487
  }
481 by Brian Aker
Remove all of uchar.
488
  *copy->to_ptr= (unsigned char) length;
1 by brian
clean slate
489
  memcpy(copy->to_ptr + 1, from_ptr, length);
490
}
491
492
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
493
static void do_varstring2(CopyField *copy)
1 by brian
clean slate
494
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
495
  uint32_t length= uint2korr(copy->from_ptr);
1 by brian
clean slate
496
  if (length > copy->to_length- HA_KEY_BLOB_LENGTH)
497
  {
498
    length=copy->to_length-HA_KEY_BLOB_LENGTH;
1660.1.3 by Brian Aker
Encapsulate Table in field
499
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
2046.2.3 by Brian Aker
Add basic tests for microtime.
500
    {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
501
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
502
                                  ER_WARN_DATA_TRUNCATED, 1);
2046.2.3 by Brian Aker
Add basic tests for microtime.
503
    }
1 by brian
clean slate
504
  }
505
  int2store(copy->to_ptr,length);
506
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, copy->from_ptr + HA_KEY_BLOB_LENGTH,
507
         length);
508
}
509
510
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
511
static void do_varstring2_mb(CopyField *copy)
1 by brian
clean slate
512
{
513
  int well_formed_error;
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
514
  const charset_info_st * const cs= copy->from_field->charset();
482 by Brian Aker
Remove uint.
515
  uint32_t char_length= (copy->to_length - HA_KEY_BLOB_LENGTH) / cs->mbmaxlen;
516
  uint32_t from_length= uint2korr(copy->from_ptr);
481 by Brian Aker
Remove all of uchar.
517
  const unsigned char *from_beg= copy->from_ptr + HA_KEY_BLOB_LENGTH;
2440.2.14 by Olaf van der Spek
Refactor
518
  uint32_t length= cs->cset->well_formed_len(*cs, str_ref(from_beg, from_length), char_length, &well_formed_error);
1 by brian
clean slate
519
  if (length < from_length)
520
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
521
    if (current_session->count_cuted_fields)
2046.2.3 by Brian Aker
Add basic tests for microtime.
522
    {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
523
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
524
                                  ER_WARN_DATA_TRUNCATED, 1);
2046.2.3 by Brian Aker
Add basic tests for microtime.
525
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
526
  }
1 by brian
clean slate
527
  int2store(copy->to_ptr, length);
528
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, from_beg, length);
529
}
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
530
1 by brian
clean slate
531
532
/***************************************************************************
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
533
** The different functions that fills in a CopyField class
1 by brian
clean slate
534
***************************************************************************/
535
536
/**
537
  copy of field to maybe null string.
538
  If field is null then the all bytes are set to 0.
539
  if field is not null then the first byte is set to 1 and the rest of the
540
  string is the field value.
541
  The 'to' buffer should have a size of field->pack_length()+1
542
*/
543
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
544
void CopyField::set(unsigned char *to,Field *from)
1 by brian
clean slate
545
{
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
546
  from_ptr= from->ptr;
547
  to_ptr= to;
548
  from_length= from->pack_length();
1 by brian
clean slate
549
  if (from->maybe_null())
550
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
551
    from_null_ptr= from->null_ptr;
552
    from_bit= from->null_bit;
553
    to_ptr[0]= 1;                             // Null as default value
554
    to_null_ptr= (unsigned char*) to_ptr++;
555
    to_bit= 1;
1660.1.3 by Brian Aker
Encapsulate Table in field
556
    if (from->getTable()->maybe_null)
1 by brian
clean slate
557
    {
1660.1.3 by Brian Aker
Encapsulate Table in field
558
      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.
559
      do_copy= do_outer_field_to_null_str;
1 by brian
clean slate
560
    }
561
    else
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
562
      do_copy= do_field_to_null_str;
1 by brian
clean slate
563
  }
564
  else
565
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
566
    to_null_ptr= 0;                           // For easy debugging
567
    do_copy= do_field_eq;
1 by brian
clean slate
568
  }
569
}
570
571
572
/*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
573
  To do:
1 by brian
clean slate
574
575
  If 'save\ is set to true and the 'from' is a blob field, do_copy is set to
576
  do_save_blob rather than do_conv_blob.  The only differences between them
577
  appears to be:
578
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
579
  - do_save_blob allocates and uses an intermediate buffer before calling
580
    Field_blob::store. Is this in order to trigger the call to
1 by brian
clean slate
581
    well_formed_copy_nchars, by changing the pointer copy->tmp.ptr()?
582
    That call will take place anyway in all known cases.
583
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
584
  - The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended
1 by brian
clean slate
585
    effect? Truncation is handled by well_formed_copy_nchars anyway.
586
 */
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
587
void CopyField::set(Field *to,Field *from,bool save)
1 by brian
clean slate
588
{
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
589
  if (to->type() == DRIZZLE_TYPE_NULL)
1 by brian
clean slate
590
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
591
    to_null_ptr= 0;           // For easy debugging
592
    to_ptr= 0;
593
    do_copy= do_skip;
1 by brian
clean slate
594
    return;
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_field= from;
597
  to_field= to;
598
  from_ptr= from->ptr;
599
  from_length= from->pack_length();
600
  to_ptr= to->ptr;
601
  to_length= to_field->pack_length();
1 by brian
clean slate
602
603
  // 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.
604
  from_null_ptr= to_null_ptr= 0;
1 by brian
clean slate
605
  if (from->maybe_null())
606
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
607
    from_null_ptr= from->null_ptr;
608
    from_bit= from->null_bit;
1 by brian
clean slate
609
    if (to_field->real_maybe_null())
610
    {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
611
      to_null_ptr= to->null_ptr;
612
      to_bit= to->null_bit;
1 by brian
clean slate
613
      if (from_null_ptr)
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
614
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
615
        do_copy= do_copy_null;
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
616
      }
1 by brian
clean slate
617
      else
618
      {
1660.1.3 by Brian Aker
Encapsulate Table in field
619
        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.
620
        do_copy= do_outer_field_null;
1 by brian
clean slate
621
      }
622
    }
623
    else
624
    {
2046.2.1 by Brian Aker
First pass on micro timestamp.
625
      if (to_field->is_timestamp())
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
626
      {
1 by brian
clean slate
627
        do_copy= do_copy_timestamp;               // Automatic timestamp
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
628
      }
1660.1.3 by Brian Aker
Encapsulate Table in field
629
      else if (to_field == to_field->getTable()->next_number_field)
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
630
      {
1 by brian
clean slate
631
        do_copy= do_copy_next_number;
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
632
      }
1 by brian
clean slate
633
      else
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
634
      {
1 by brian
clean slate
635
        do_copy= do_copy_not_null;
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
636
      }
1 by brian
clean slate
637
    }
638
  }
639
  else if (to_field->real_maybe_null())
640
  {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
641
    to_null_ptr= to->null_ptr;
642
    to_bit= to->null_bit;
1 by brian
clean slate
643
    do_copy= do_copy_maybe_null;
644
  }
645
  else
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
646
   do_copy= 0;
1 by brian
clean slate
647
648
  if ((to->flags & BLOB_FLAG) && save)
649
    do_copy2= do_save_blob;
650
  else
651
    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.
652
  if (!do_copy)         // Not null
653
    do_copy= do_copy2;
1 by brian
clean slate
654
}
655
656
1052.2.2 by Nathan Williams
No actual code changes. Changed Copy_field to CopyField, to reflect the coding standards.
657
CopyField::Copy_func *
658
CopyField::get_copy_func(Field *to,Field *from)
1 by brian
clean slate
659
{
1660.1.3 by Brian Aker
Encapsulate Table in field
660
  bool compatible_db_low_byte_first= (to->getTable()->getShare()->db_low_byte_first ==
661
                                     from->getTable()->getShare()->db_low_byte_first);
1 by brian
clean slate
662
  if (to->flags & BLOB_FLAG)
663
  {
664
    if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
665
      return do_conv_blob;
666
    if (from_length != to_length || !compatible_db_low_byte_first)
667
    {
668
      // Correct pointer to point at char pointer
2134.1.4 by Brian Aker
Simple encapsulation for table.
669
      to_ptr+= to_length - to->getTable()->getShare()->sizeBlobPtr();
670
      from_ptr+= from_length- from->getTable()->getShare()->sizeBlobPtr();
1 by brian
clean slate
671
      return do_copy_blob;
672
    }
673
  }
674
  else
675
  {
676
    if (to->result_type() == DECIMAL_RESULT)
677
      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.
678
      
1 by brian
clean slate
679
    // Check if identical fields
680
    if (from->result_type() == STRING_RESULT)
681
    {
682
      /*
683
        If we are copying date or datetime's we have to check the dates
684
        if we don't allow 'all' dates.
685
      */
686
      if (to->real_type() != from->real_type() ||
687
          !compatible_db_low_byte_first ||
1660.1.3 by Brian Aker
Encapsulate Table in field
688
          (((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
689
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
690
        if (from->real_type() == DRIZZLE_TYPE_ENUM)
691
        {
692
          if (to->result_type() != STRING_RESULT)
693
          {
694
            return do_field_int;  // Convert SET to number
695
          }
2008 by Brian Aker
Formatting + remove default from switch/case.
696
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
697
          return do_field_string;
698
        }
1 by brian
clean slate
699
      }
2008 by Brian Aker
Formatting + remove default from switch/case.
700
325 by Brian Aker
Remove SET
701
      if (to->real_type() == DRIZZLE_TYPE_ENUM)
1 by brian
clean slate
702
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
703
        if (!to->eq_def(from))
1 by brian
clean slate
704
        {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
705
          if (from->real_type() == DRIZZLE_TYPE_ENUM &&
706
              to->real_type() == DRIZZLE_TYPE_ENUM)
1 by brian
clean slate
707
            return do_field_enum;
708
          else
709
            return do_field_string;
710
        }
711
      }
712
      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.
713
        return do_field_string;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
714
      else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
1 by brian
clean slate
715
      {
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.
716
        /* Field_blob is not part of the Field_varstring hierarchy,
2008 by Brian Aker
Formatting + remove default from switch/case.
717
          and casting to varstring for calling pack_length_no_ptr()
718
          is always incorrect. Previously the below comparison has
719
          always evaluated to false as pack_length_no_ptr() for BLOB
720
          will return 4 and varstring can only be <= 2.
721
          If your brain slightly bleeds as to why this worked for
722
          so many years, you are in no way alone.
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.
723
        */
724
        if (from->flags & BLOB_FLAG)
725
          return do_field_string;
726
1267.1.8 by Stewart Smith
change dynamic_cast in field_conv.cc back to static_cast for runtime performance
727
        if ((static_cast<Field_varstring*>(to))->pack_length_no_ptr() !=
728
            (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.
729
        {
1 by brian
clean slate
730
          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.
731
        }
2008 by Brian Aker
Formatting + remove default from switch/case.
732
1 by brian
clean slate
733
        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.
734
        {
1782.4.5 by Brian Aker
Encapsulate the length of the bytes in the varchar header.
735
          return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
1 by brian
clean slate
736
                  (from->charset()->mbmaxlen == 1 ? do_varstring1 :
2008 by Brian Aker
Formatting + remove default from switch/case.
737
                   do_varstring1_mb) :
1 by brian
clean slate
738
                  (from->charset()->mbmaxlen == 1 ? do_varstring2 :
2008 by Brian Aker
Formatting + remove default from switch/case.
739
                   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.
740
        }
1 by brian
clean slate
741
      }
742
      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.
743
      {
744
        return (from->charset()->mbmaxlen == 1 ?
1 by brian
clean slate
745
                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.
746
      }
1 by brian
clean slate
747
      else if (to_length > from_length)
748
      {
749
        if (to->charset() == &my_charset_bin)
750
          return do_expand_binary;
751
        else
752
          return do_expand_string;
753
      }
754
    }
755
    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.
756
             to_length != from_length ||
1 by brian
clean slate
757
             !compatible_db_low_byte_first)
758
    {
80 by Brian Aker
Most of the removal of dead DECIMAL type
759
      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.
760
        return do_field_string;
1 by brian
clean slate
761
      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.
762
        return do_field_int;
763
1 by brian
clean slate
764
      return do_field_real;
765
    }
766
    else
767
    {
768
      if (!to->eq_def(from) || !compatible_db_low_byte_first)
769
      {
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
770
        if (to->result_type() == INT_RESULT)
771
          return do_field_int;
772
        else
773
          return do_field_real;
1 by brian
clean slate
774
      }
775
    }
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
    /* Eq fields */
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
779
  switch (to_length)
780
  {
1 by brian
clean slate
781
  case 1: return do_field_1;
782
  case 2: return do_field_2;
783
  case 3: return do_field_3;
784
  case 4: return do_field_4;
785
  case 6: return do_field_6;
786
  case 8: return do_field_8;
787
  }
1052.2.6 by Nathan Williams
More style cleanup in files associated with Copy_field to CopyField change. Primarily spacing after = operator.
788
  
1 by brian
clean slate
789
  return do_field_eq;
790
}
791
792
793
/** Simple quick field convert that is called on insert. */
794
795
int field_conv(Field *to,Field *from)
796
{
797
  if (to->real_type() == from->real_type() &&
1660.1.3 by Brian Aker
Encapsulate Table in field
798
      !(to->type() == DRIZZLE_TYPE_BLOB && to->getTable()->copy_blobs))
1 by brian
clean slate
799
  {
800
    /* Please god, will someone rewrite this to be readable :( */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
801
    if (to->pack_length() == from->pack_length() &&
802
        !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
803
        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
804
        (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
805
        from->charset() == to->charset() &&
1660.1.3 by Brian Aker
Encapsulate Table in field
806
	to->getTable()->getShare()->db_low_byte_first == from->getTable()->getShare()->db_low_byte_first &&
807
        (!(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.
808
        (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
809
    {						// Identical fields
810
      /* This may happen if one does 'UPDATE ... SET x=x' */
811
      if (to->ptr != from->ptr)
812
        memcpy(to->ptr,from->ptr,to->pack_length());
813
      return 0;
814
    }
815
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
816
  if (to->type() == DRIZZLE_TYPE_BLOB)
1 by brian
clean slate
817
  {						// Be sure the value is stored
818
    Field_blob *blob=(Field_blob*) to;
1996.2.1 by Brian Aker
uuid type code.
819
    from->val_str_internal(&blob->value);
1 by brian
clean slate
820
    /*
821
      Copy value if copy_blobs is set, or source is not a string and
822
      we have a pointer to its internal string conversion buffer.
823
    */
1660.1.3 by Brian Aker
Encapsulate Table in field
824
    if (to->getTable()->copy_blobs ||
1 by brian
clean slate
825
        (!blob->value.is_alloced() &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
826
         from->real_type() != DRIZZLE_TYPE_VARCHAR))
1 by brian
clean slate
827
      blob->value.copy();
828
    return blob->store(blob->value.ptr(),blob->value.length(),from->charset());
829
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
830
  if (from->real_type() == DRIZZLE_TYPE_ENUM &&
831
      to->real_type() == DRIZZLE_TYPE_ENUM &&
1 by brian
clean slate
832
      from->val_int() == 0)
833
  {
834
    ((Field_enum *)(to))->store_type(0);
835
    return 0;
836
  }
837
  else if ((from->result_type() == STRING_RESULT &&
838
            (to->result_type() == STRING_RESULT ||
325 by Brian Aker
Remove SET
839
             (from->real_type() != DRIZZLE_TYPE_ENUM))))
1 by brian
clean slate
840
  {
841
    char buff[MAX_FIELD_WIDTH];
842
    String result(buff,sizeof(buff),from->charset());
1996.2.1 by Brian Aker
uuid type code.
843
    from->val_str_internal(&result);
1 by brian
clean slate
844
    /*
845
      We use c_ptr_quick() here to make it easier if to is a float/double
846
      as the conversion routines will do a copy of the result doesn't
847
      end with \0. Can be replaced with .ptr() when we have our own
848
      string->double conversion.
849
    */
2318.7.12 by Olaf van der Spek
Remove String::c_ptr_quick()
850
    return to->store(result.c_str(),result.length(),from->charset());
1 by brian
clean slate
851
  }
852
  else if (from->result_type() == REAL_RESULT)
2057.2.4 by Brian Aker
We no longer look at the sign of a timestamp.
853
  {
1 by brian
clean slate
854
    return to->store(from->val_real());
2057.2.4 by Brian Aker
We no longer look at the sign of a timestamp.
855
  }
1 by brian
clean slate
856
  else if (from->result_type() == DECIMAL_RESULT)
857
  {
2030.1.4 by Brian Aker
Change my_decimal to Decimal
858
    type::Decimal buff;
1 by brian
clean slate
859
    return to->store_decimal(from->val_decimal(&buff));
860
  }
861
  else
2057.2.4 by Brian Aker
We no longer look at the sign of a timestamp.
862
  {
1 by brian
clean slate
863
    return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG));
2057.2.4 by Brian Aker
We no longer look at the sign of a timestamp.
864
  }
1 by brian
clean slate
865
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
866
867
} /* namespace drizzled */