~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/create_field.cc

Merged in latest plugin-slot-reorg.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
21
21
 * @file Implementation of CreateField class
22
22
 */
23
23
 
24
 
#include "config.h"
 
24
#include "drizzled/server_includes.h"
25
25
#include <errno.h>
26
 
#include <float.h>
27
26
#include "drizzled/sql_select.h"
28
27
#include "drizzled/error.h"
29
28
#include "drizzled/field.h"
31
30
#include "drizzled/field/str.h"
32
31
#include "drizzled/field/num.h"
33
32
#include "drizzled/field/blob.h"
34
 
#include "drizzled/field/boolean.h"
35
33
#include "drizzled/field/enum.h"
36
34
#include "drizzled/field/null.h"
37
35
#include "drizzled/field/date.h"
38
36
#include "drizzled/field/decimal.h"
39
37
#include "drizzled/field/real.h"
40
38
#include "drizzled/field/double.h"
41
 
#include "drizzled/field/int32.h"
42
 
#include "drizzled/field/int64.h"
 
39
#include "drizzled/field/long.h"
 
40
#include "drizzled/field/int64_t.h"
43
41
#include "drizzled/field/num.h"
44
 
#include "drizzled/field/epoch.h"
 
42
#include "drizzled/field/timestamp.h"
45
43
#include "drizzled/field/datetime.h"
46
44
#include "drizzled/field/varstring.h"
47
 
#include "drizzled/field/uuid.h"
48
45
#include "drizzled/temporal.h"
49
 
#include "drizzled/item/string.h"
50
 
 
51
 
#include "drizzled/display.h"
52
46
 
53
47
#include <algorithm>
54
48
 
55
49
using namespace std;
56
50
 
57
 
namespace drizzled
58
 
{
59
51
 
60
52
/** Create a field suitable for create of table. */
61
53
CreateField::CreateField(Field *old_field, Field *orig_field)
74
66
 
75
67
  /* Fix if the original table had 4 byte pointer blobs */
76
68
  if (flags & BLOB_FLAG)
77
 
  {
78
 
    pack_length= (pack_length - old_field->getTable()->getShare()->sizeBlobPtr() + portable_sizeof_char_ptr);
79
 
  }
 
69
    pack_length= (pack_length - old_field->table->s->blob_ptr_size + portable_sizeof_char_ptr);
80
70
 
81
71
  switch (sql_type) 
82
72
  {
94
84
      break;
95
85
  }
96
86
 
97
 
  if (flags & ENUM_FLAG)
 
87
  if (flags & (ENUM_FLAG | SET_FLAG))
98
88
    interval= ((Field_enum*) old_field)->typelib;
99
89
  else
100
90
    interval= 0;
101
91
  def= 0;
102
92
  char_length= length;
103
93
 
104
 
  if (!(flags & (NO_DEFAULT_VALUE_FLAG)) &&
105
 
      !(flags & AUTO_INCREMENT_FLAG) &&
 
94
  if (!(flags & (NO_DEFAULT_VALUE_FLAG )) &&
106
95
      old_field->ptr && orig_field &&
107
 
      (not old_field->is_timestamp() ||                /* set def only if */
108
 
       old_field->getTable()->timestamp_field != old_field ||  /* timestamp field */
 
96
      (sql_type != DRIZZLE_TYPE_TIMESTAMP ||                /* set def only if */
 
97
       old_field->table->timestamp_field != old_field ||  /* timestamp field */
109
98
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
110
99
  {
111
100
    ptrdiff_t diff;
112
101
 
113
102
    /* Get the value from default_values */
114
 
    diff= (ptrdiff_t) (orig_field->getTable()->getDefaultValues() - orig_field->getTable()->getInsertRecord());
 
103
    diff= (ptrdiff_t) (orig_field->table->s->default_values - orig_field->table->record[0]);
115
104
    orig_field->move_field_offset(diff);        // Points now at default_values
116
105
    if (! orig_field->is_real_null())
117
106
    {
118
107
      char buff[MAX_FIELD_WIDTH], *pos;
119
108
      String tmp(buff, sizeof(buff), charset), *res;
120
 
      res= orig_field->val_str_internal(&tmp);
121
 
      pos= (char*) memory::sql_strmake(res->ptr(), res->length());
 
109
      res= orig_field->val_str(&tmp);
 
110
      pos= (char*) sql_strmake(res->ptr(), res->length());
122
111
      def= new Item_string(pos, res->length(), charset);
123
112
    }
124
 
    orig_field->move_field_offset(-diff);       // Back to getInsertRecord()
 
113
    orig_field->move_field_offset(-diff);       // Back to record[0]
125
114
  }
126
115
}
127
116
 
143
132
      length*= charset->mbmaxlen;
144
133
      key_length= pack_length;
145
134
      break;
146
 
    case DRIZZLE_TYPE_DECIMAL:
 
135
    case DRIZZLE_TYPE_NEWDECIMAL:
147
136
      key_length= pack_length=
148
 
        class_decimal_get_binary_size(class_decimal_length_to_precision(length,
 
137
        my_decimal_get_binary_size(my_decimal_length_to_precision(length,
149
138
                  decimals,
150
139
                  flags &
151
140
                  UNSIGNED_FLAG),
161
150
  Init for a tmp table field. To be extended if need be.
162
151
*/
163
152
void CreateField::init_for_tmp_table(enum_field_types sql_type_arg,
164
 
                                     uint32_t length_arg,
165
 
                                     uint32_t decimals_arg,
166
 
                                     bool maybe_null)
 
153
                                      uint32_t length_arg, uint32_t decimals_arg,
 
154
                                      bool maybe_null, bool is_unsigned)
167
155
{
168
156
  field_name= "";
169
157
  sql_type= sql_type_arg;
171
159
  unireg_check= Field::NONE;
172
160
  interval= 0;
173
161
  charset= &my_charset_bin;
174
 
  decimals= decimals_arg & FIELDFLAG_MAX_DEC;
175
 
 
176
 
  if (! maybe_null)
177
 
    flags= NOT_NULL_FLAG;
178
 
  else
179
 
    flags= 0;
 
162
  pack_flag= (FIELDFLAG_NUMBER |
 
163
              ((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
 
164
              (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
 
165
              (is_unsigned ? 0 : FIELDFLAG_DECIMAL));
180
166
}
181
167
 
182
168
bool CreateField::init(Session *,
229
215
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
230
216
  */
231
217
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
232
 
      (fld_type_modifier & NOT_NULL_FLAG) && (fld_type != DRIZZLE_TYPE_TIMESTAMP and fld_type != DRIZZLE_TYPE_MICROTIME))
233
 
  {
 
218
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
234
219
    flags|= NO_DEFAULT_VALUE_FLAG;
235
 
  }
236
220
 
237
221
  if (fld_length && !(length= (uint32_t) atoi(fld_length)))
238
222
    fld_length= 0;
240
224
 
241
225
  switch (fld_type) 
242
226
  {
 
227
    case DRIZZLE_TYPE_TINY:
 
228
      if (!fld_length)
 
229
        length= MAX_TINYINT_WIDTH+sign_len;
 
230
      allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
231
      break;
243
232
    case DRIZZLE_TYPE_LONG:
244
233
      if (!fld_length)
245
234
        length= MAX_INT_WIDTH+sign_len;
252
241
      break;
253
242
    case DRIZZLE_TYPE_NULL:
254
243
      break;
255
 
    case DRIZZLE_TYPE_DECIMAL:
256
 
      class_decimal_trim(&length, &decimals);
 
244
    case DRIZZLE_TYPE_NEWDECIMAL:
 
245
      my_decimal_trim(&length, &decimals);
257
246
      if (length > DECIMAL_MAX_PRECISION)
258
247
      {
259
248
        my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
265
254
        my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
266
255
        return(true);
267
256
      }
268
 
      length= class_decimal_precision_to_length(length, decimals, fld_type_modifier & UNSIGNED_FLAG);
269
 
      pack_length= class_decimal_get_binary_size(length, decimals);
 
257
      length= my_decimal_precision_to_length(length, decimals, fld_type_modifier & UNSIGNED_FLAG);
 
258
      pack_length= my_decimal_get_binary_size(length, decimals);
270
259
      break;
271
260
    case DRIZZLE_TYPE_VARCHAR:
272
261
      /*
304
293
        return(true);
305
294
      }
306
295
      break;
307
 
    case DRIZZLE_TYPE_MICROTIME:
308
 
      /* 
309
 
        This assert() should be correct due to absence of length
310
 
        specifiers for timestamp. Previous manipulation also wasn't
311
 
        ever called (from examining lcov)
312
 
      */
313
 
      assert(fld_type);
314
296
    case DRIZZLE_TYPE_TIMESTAMP:
315
 
      length= MicroTimestamp::MAX_STRING_LENGTH;
316
 
 
 
297
      if (!fld_length)
 
298
      {
 
299
        length= drizzled::DateTime::MAX_STRING_LENGTH;
 
300
      }
 
301
 
 
302
      /* This assert() should be correct due to absence of length
 
303
         specifiers for timestamp. Previous manipulation also wasn't
 
304
         ever called (from examining lcov)
 
305
      */
 
306
      assert(length == (uint32_t)drizzled::DateTime::MAX_STRING_LENGTH);
 
307
 
 
308
      flags|= UNSIGNED_FLAG;
317
309
      if (fld_default_value)
318
310
      {
319
311
        /* Grammar allows only NOW() value for ON UPDATE clause */
329
321
          def= 0;
330
322
        }
331
323
        else
332
 
        {
333
324
          unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
334
 
                         Field::NONE);
335
 
        }
 
325
                                              Field::NONE);
336
326
      }
337
327
      else
338
328
      {
355
345
      }
356
346
      break;
357
347
    case DRIZZLE_TYPE_DATE:
358
 
      length= Date::MAX_STRING_LENGTH;
359
 
      break;
360
 
    case DRIZZLE_TYPE_UUID:
361
 
      length= field::Uuid::max_string_length();
362
 
      break;
363
 
    case DRIZZLE_TYPE_BOOLEAN:
364
 
      length= field::Boolean::max_string_length();
 
348
      length= drizzled::Date::MAX_STRING_LENGTH;
365
349
      break;
366
350
    case DRIZZLE_TYPE_DATETIME:
367
 
      length= DateTime::MAX_STRING_LENGTH;
368
 
      break;
369
 
    case DRIZZLE_TYPE_TIME:
370
 
      length= DateTime::MAX_STRING_LENGTH;
 
351
      length= drizzled::DateTime::MAX_STRING_LENGTH;
371
352
      break;
372
353
    case DRIZZLE_TYPE_ENUM:
373
354
      {
374
355
        /* Should be safe. */
375
 
        pack_length= 4;
 
356
        pack_length= get_enum_pack_length(fld_interval_list->elements);
376
357
 
377
358
        List_iterator<String> it(*fld_interval_list);
378
359
        String *tmp;
405
386
 
406
387
  return false; /* success */
407
388
}
408
 
 
409
 
std::ostream& operator<<(std::ostream& output, const CreateField &field)
410
 
{
411
 
  output << "CreateField:(";
412
 
  output <<  field.field_name;
413
 
  output << ", ";
414
 
  output << drizzled::display::type(field.type());
415
 
  output << ", { ";
416
 
 
417
 
  if (field.flags & NOT_NULL_FLAG)
418
 
    output << " NOT_NULL";
419
 
 
420
 
  if (field.flags & PRI_KEY_FLAG)
421
 
    output << ", PRIMARY KEY";
422
 
 
423
 
  if (field.flags & UNIQUE_KEY_FLAG)
424
 
    output << ", UNIQUE KEY";
425
 
 
426
 
  if (field.flags & MULTIPLE_KEY_FLAG)
427
 
    output << ", MULTIPLE KEY";
428
 
 
429
 
  if (field.flags & BLOB_FLAG)
430
 
    output << ", BLOB";
431
 
 
432
 
  if (field.flags & UNSIGNED_FLAG)
433
 
    output << ", UNSIGNED";
434
 
 
435
 
  if (field.flags & BINARY_FLAG)
436
 
    output << ", BINARY";
437
 
  output << "}, ";
438
 
  if (field.field)
439
 
    output << *field.field;
440
 
  output << ")";
441
 
 
442
 
  return output;  // for multiple << operators.
443
 
}
444
 
 
445
 
} /* namespace drizzled */