~drizzle-trunk/drizzle/development

1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
/**
1052.2.7 by Nathan Williams
Merged trunk and resolved conflicts.
21
 * @file Implementation of CreateField class
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
22
 */
23
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
24
#include <config.h>
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
25
#include <errno.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
26
#include <float.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
27
#include <drizzled/sql_select.h>
28
#include <drizzled/error.h>
29
#include <drizzled/field.h>
30
#include <drizzled/create_field.h>
31
#include <drizzled/field/str.h>
32
#include <drizzled/field/num.h>
33
#include <drizzled/field/blob.h>
34
#include <drizzled/field/boolean.h>
35
#include <drizzled/field/enum.h>
36
#include <drizzled/field/null.h>
37
#include <drizzled/field/date.h>
38
#include <drizzled/field/decimal.h>
39
#include <drizzled/field/real.h>
40
#include <drizzled/field/double.h>
41
#include <drizzled/field/int32.h>
42
#include <drizzled/field/int64.h>
43
#include <drizzled/field/num.h>
44
#include <drizzled/field/epoch.h>
45
#include <drizzled/field/datetime.h>
46
#include <drizzled/field/varstring.h>
47
#include <drizzled/field/uuid.h>
2398.1.1 by Muhammad Umair
merge lp:~mumair/drizzle/drizzle-IPv6Address
48
#include <drizzled/field/ipv6.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
49
#include <drizzled/temporal.h>
50
#include <drizzled/item/string.h>
2154.2.18 by Brian Aker
Merge in all changes for include files.
51
#include <drizzled/table.h>
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
52
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
53
#include <drizzled/display.h>
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
54
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
55
#include <algorithm>
56
57
using namespace std;
58
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
59
namespace drizzled
60
{
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
61
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
62
/** Create a field suitable for create of table. */
1052.2.7 by Nathan Williams
Merged trunk and resolved conflicts.
63
CreateField::CreateField(Field *old_field, Field *orig_field)
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
64
{
65
  field= old_field;
66
  field_name= change= old_field->field_name;
67
  length= old_field->field_length;
68
  flags= old_field->flags;
69
  unireg_check= old_field->unireg_check;
70
  pack_length= old_field->pack_length();
71
  key_length= old_field->key_length();
72
  sql_type= old_field->real_type();
73
  charset= old_field->charset(); // May be NULL ptr
74
  comment= old_field->comment;
75
  decimals= old_field->decimals();
76
77
  /* Fix if the original table had 4 byte pointer blobs */
78
  if (flags & BLOB_FLAG)
2134.1.4 by Brian Aker
Simple encapsulation for table.
79
  {
80
    pack_length= (pack_length - old_field->getTable()->getShare()->sizeBlobPtr() + portable_sizeof_char_ptr);
81
  }
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
82
83
  switch (sql_type) 
84
  {
85
    case DRIZZLE_TYPE_BLOB:
86
      sql_type= DRIZZLE_TYPE_BLOB;
87
      length/= charset->mbmaxlen;
88
      key_length/= charset->mbmaxlen;
89
      break;
90
    case DRIZZLE_TYPE_ENUM:
91
    case DRIZZLE_TYPE_VARCHAR:
92
      /* This is corrected in create_length_to_internal_length */
93
      length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
94
      break;
95
    default:
96
      break;
97
  }
98
1315.2.15 by Stewart Smith
remove the now unused SET_FLAG define. We don't have the SET field type, and this flag was never set.
99
  if (flags & ENUM_FLAG)
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
100
    interval= ((Field_enum*) old_field)->typelib;
101
  else
102
    interval= 0;
103
  def= 0;
104
  char_length= length;
105
1638.10.114 by Stewart Smith
when creating a CreateField from a Field, a auto_incremen column should not have a default value of 0 as that's not magic.
106
  if (!(flags & (NO_DEFAULT_VALUE_FLAG)) &&
107
      !(flags & AUTO_INCREMENT_FLAG) &&
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
108
      old_field->ptr && orig_field &&
2046.2.1 by Brian Aker
First pass on micro timestamp.
109
      (not old_field->is_timestamp() ||                /* set def only if */
1660.1.3 by Brian Aker
Encapsulate Table in field
110
       old_field->getTable()->timestamp_field != old_field ||  /* timestamp field */
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
111
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
112
  {
113
    /* Get the value from default_values */
2318.6.29 by Olaf van der Spek
Refactor
114
    ptrdiff_t diff= (ptrdiff_t) (orig_field->getTable()->getDefaultValues() - orig_field->getTable()->getInsertRecord());
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
115
    orig_field->move_field_offset(diff);	// Points now at default_values
116
    if (! orig_field->is_real_null())
117
    {
2318.6.29 by Olaf van der Spek
Refactor
118
      char buff[MAX_FIELD_WIDTH];
119
      String tmp(buff, sizeof(buff), charset);
120
      String* res= orig_field->val_str_internal(&tmp);
2318.9.11 by Olaf van der Spek
Use sql_strdup(str_ref)
121
      char* pos= memory::sql_strdup(*res);
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
122
      def= new Item_string(pos, res->length(), charset);
123
    }
1672.3.6 by Brian Aker
First pass in encapsulating row
124
    orig_field->move_field_offset(-diff);	// Back to getInsertRecord()
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
125
  }
126
}
127
128
/**
1052.2.7 by Nathan Williams
Merged trunk and resolved conflicts.
129
  Convert CreateField::length from number of characters to number of bytes.
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
130
*/
1052.2.7 by Nathan Williams
Merged trunk and resolved conflicts.
131
void CreateField::create_length_to_internal_length(void)
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
132
{
133
  switch (sql_type) 
134
  {
135
    case DRIZZLE_TYPE_BLOB:
136
    case DRIZZLE_TYPE_VARCHAR:
137
      length*= charset->mbmaxlen;
138
      key_length= length;
139
      pack_length= calc_pack_length(sql_type, length);
140
      break;
141
    case DRIZZLE_TYPE_ENUM:
142
      /* Pack_length already calculated in ::init() */
143
      length*= charset->mbmaxlen;
144
      key_length= pack_length;
145
      break;
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
146
    case DRIZZLE_TYPE_DECIMAL:
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
147
      key_length= pack_length=
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
148
        class_decimal_get_binary_size(class_decimal_length_to_precision(length,
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
149
                  decimals,
150
                  flags &
151
                  UNSIGNED_FLAG),
152
          decimals);
153
      break;
154
    default:
155
      key_length= pack_length= calc_pack_length(sql_type, length);
156
      break;
157
  }
158
}
159
160
/**
161
  Init for a tmp table field. To be extended if need be.
162
*/
1052.2.7 by Nathan Williams
Merged trunk and resolved conflicts.
163
void CreateField::init_for_tmp_table(enum_field_types sql_type_arg,
1119.9.10 by Jay Pipes
Removes FIELDFLAG_MAYBE_NULL and f_maybe_null() macro.
164
                                     uint32_t length_arg,
165
                                     uint32_t decimals_arg,
1119.9.18 by Stewart Smith
Fix maybe_null for CreateField created for temp table in item/sum.cc (Item_sum_distinct)
166
                                     bool maybe_null)
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
167
{
168
  field_name= "";
169
  sql_type= sql_type_arg;
170
  char_length= length= length_arg;;
171
  unireg_check= Field::NONE;
172
  interval= 0;
173
  charset= &my_charset_bin;
1119.9.16 by Stewart Smith
don't store decimal/double scale in pack_flag, instead use the numeric option scale field in the table proto. This removes f_decimals() macro and the bits in the pack_flag for scale.
174
  decimals= decimals_arg & FIELDFLAG_MAX_DEC;
1119.9.18 by Stewart Smith
Fix maybe_null for CreateField created for temp table in item/sum.cc (Item_sum_distinct)
175
2318.8.3 by Olaf van der Spek
CreateField includes
176
  flags= maybe_null ? 0 : NOT_NULL_FLAG;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
177
}
178
1052.2.7 by Nathan Williams
Merged trunk and resolved conflicts.
179
bool CreateField::init(Session *,
2318.8.3 by Olaf van der Spek
CreateField includes
180
                        const char *fld_name,
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
181
                        enum_field_types fld_type,
2318.8.3 by Olaf van der Spek
CreateField includes
182
                        const char *fld_length,
183
                        const char *fld_decimals,
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
184
                        uint32_t fld_type_modifier,
2371.1.2 by Brian Aker
Remove the typedef on lexkey
185
                        lex_string_t *fld_comment,
2318.8.3 by Olaf van der Spek
CreateField includes
186
                        const char *fld_change,
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
187
                        List<String> *fld_interval_list,
2318.8.3 by Olaf van der Spek
CreateField includes
188
                        const charset_info_st* fld_charset,
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
189
                        uint32_t,
2318.8.3 by Olaf van der Spek
CreateField includes
190
                        column_format_type column_format_in)
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
191
{
192
  uint32_t sign_len= 0;
193
  uint32_t allowed_type_modifier= 0;
194
  uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
195
196
  field= 0;
197
  field_name= fld_name;
198
  flags= fld_type_modifier;
199
  flags|= (((uint32_t)column_format_in & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
200
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
201
                 Field::NEXT_NUMBER : Field::NONE);
202
  decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
203
  if (decimals >= NOT_FIXED_DEC)
204
  {
205
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
206
             NOT_FIXED_DEC-1);
2318.6.55 by Olaf van der Spek
Refactor
207
    return true;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
208
  }
209
210
  sql_type= fld_type;
211
  length= 0;
212
  change= fld_change;
213
  interval= 0;
214
  pack_length= key_length= 0;
215
  charset= fld_charset;
2179.2.1 by Olaf van der Spek
Rename List::empty to clear (std::list uses clear)
216
  interval_list.clear();
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
217
218
  comment= *fld_comment;
219
220
  if (fld_length && !(length= (uint32_t) atoi(fld_length)))
971.6.11 by Eric Day
Removed purecov messages.
221
    fld_length= 0;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
222
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
223
224
  switch (fld_type) 
225
  {
226
    case DRIZZLE_TYPE_LONG:
227
      if (!fld_length)
228
        length= MAX_INT_WIDTH+sign_len;
229
      allowed_type_modifier= AUTO_INCREMENT_FLAG;
230
      break;
231
    case DRIZZLE_TYPE_LONGLONG:
232
      if (!fld_length)
233
        length= MAX_BIGINT_WIDTH;
234
      allowed_type_modifier= AUTO_INCREMENT_FLAG;
235
      break;
236
    case DRIZZLE_TYPE_NULL:
237
      break;
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
238
    case DRIZZLE_TYPE_DECIMAL:
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
239
      class_decimal_trim(&length, &decimals);
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
240
      if (length > DECIMAL_MAX_PRECISION)
241
      {
242
        my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
243
                DECIMAL_MAX_PRECISION);
2318.6.55 by Olaf van der Spek
Refactor
244
        return true;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
245
      }
246
      if (length < decimals)
247
      {
248
        my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
2318.6.55 by Olaf van der Spek
Refactor
249
        return true;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
250
      }
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
251
      length= class_decimal_precision_to_length(length, decimals, fld_type_modifier & UNSIGNED_FLAG);
252
      pack_length= class_decimal_get_binary_size(length, decimals);
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
253
      break;
254
    case DRIZZLE_TYPE_VARCHAR:
255
      /*
256
        Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
257
        if they don't have a default value
258
      */
259
      max_field_charlength= MAX_FIELD_VARCHARLENGTH;
260
      break;
261
    case DRIZZLE_TYPE_BLOB:
262
      flags|= BLOB_FLAG;
263
      break;
264
    case DRIZZLE_TYPE_DOUBLE:
265
      allowed_type_modifier= AUTO_INCREMENT_FLAG;
266
      if (!fld_length && !fld_decimals)
267
      {
268
        length= DBL_DIG+7;
269
        decimals= NOT_FIXED_DEC;
270
      }
271
      if (length < decimals &&
272
          decimals != NOT_FIXED_DEC)
273
      {
274
        my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
2318.6.55 by Olaf van der Spek
Refactor
275
        return true;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
276
      }
277
      break;
2046.2.1 by Brian Aker
First pass on micro timestamp.
278
    case DRIZZLE_TYPE_MICROTIME:
2057.2.3 by Brian Aker
Move the syntax for creation to what the standard has.
279
      /* 
280
        This assert() should be correct due to absence of length
281
        specifiers for timestamp. Previous manipulation also wasn't
282
        ever called (from examining lcov)
283
      */
284
      assert(fld_type);
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
285
    case DRIZZLE_TYPE_TIMESTAMP:
2057.2.3 by Brian Aker
Move the syntax for creation to what the standard has.
286
      length= MicroTimestamp::MAX_STRING_LENGTH;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
287
      break;
288
    case DRIZZLE_TYPE_DATE:
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
289
      length= Date::MAX_STRING_LENGTH;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
290
      break;
1996.2.1 by Brian Aker
uuid type code.
291
    case DRIZZLE_TYPE_UUID:
292
      length= field::Uuid::max_string_length();
293
      break;
2398.1.1 by Muhammad Umair
merge lp:~mumair/drizzle/drizzle-IPv6Address
294
    case DRIZZLE_TYPE_IPV6:
295
      length= field::IPv6::max_string_length();
296
      break;
2023.2.2 by Brian Aker
Merge in BOOL change to BOOLEAN.
297
    case DRIZZLE_TYPE_BOOLEAN:
298
      length= field::Boolean::max_string_length();
2023.2.1 by Brian Aker
Merge in BOOL type.
299
      break;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
300
    case DRIZZLE_TYPE_DATETIME:
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
301
      length= DateTime::MAX_STRING_LENGTH;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
302
      break;
1999.4.7 by Brian Aker
Merge in first pass of TIME type (closer to EPOCH time).
303
    case DRIZZLE_TYPE_TIME:
304
      length= DateTime::MAX_STRING_LENGTH;
305
      break;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
306
    case DRIZZLE_TYPE_ENUM:
307
      {
308
        /* Should be safe. */
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
309
        pack_length= 4;
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
310
2183.2.10 by Olaf van der Spek
Use List::begin()
311
        List<String>::iterator it(fld_interval_list->begin());
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
312
        String *tmp;
313
        while ((tmp= it++))
1101.1.24 by Monty Taylor
Reverted my change to interval_list
314
          interval_list.push_back(tmp);
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
315
        length= 1;
316
        break;
317
    }
318
  }
319
  /* Remember the value of length */
320
  char_length= length;
321
322
  if (!(flags & BLOB_FLAG) &&
323
      ((length > max_field_charlength &&
2221.12.5 by Stewart Smith
factor out setting default value out from CreatField::init and into new CreateField::setDefaultValue. This also means we fix up a error message to be correct in type_blob.
324
        fld_type != DRIZZLE_TYPE_ENUM  &&
325
        (fld_type != DRIZZLE_TYPE_VARCHAR)) ||
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
326
       (!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
327
  {
328
    my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ?  ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
329
              MYF(0),
2221.12.4 by Stewart Smith
fix ER_TOO_BIG_FIELDLENGTH error message to always be number of characters, not sometimes number of bytes
330
             fld_name, max_field_charlength / (charset? charset->mbmaxlen : 1));
1055.2.10 by Jay Pipes
Moves Create_field implementation out into its own implementation file.
331
    return true;
332
  }
333
  fld_type_modifier&= AUTO_INCREMENT_FLAG;
334
  if ((~allowed_type_modifier) & fld_type_modifier)
335
  {
336
    my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
337
    return true;
338
  }
339
340
  return false; /* success */
341
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
342
2221.12.5 by Stewart Smith
factor out setting default value out from CreatField::init and into new CreateField::setDefaultValue. This also means we fix up a error message to be correct in type_blob.
343
bool CreateField::setDefaultValue(Item *default_value_item,
344
                                  Item *on_update_item)
345
{
346
  def= default_value_item;
347
348
  /*
349
    Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
350
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
351
  */
352
  if (! default_value_item
353
      && ! (flags & AUTO_INCREMENT_FLAG)
354
      && (flags & NOT_NULL_FLAG)
355
      && (sql_type != DRIZZLE_TYPE_TIMESTAMP
356
          and sql_type != DRIZZLE_TYPE_MICROTIME))
357
  {
358
    flags|= NO_DEFAULT_VALUE_FLAG;
359
  }
2221.12.7 by Stewart Smith
in ALTER TABLE SET DEFAULT code path, use the new CreateField::setDefaultValue method - this means we have the same logic for setting default values for a CreateField as in CREATE TABLE
360
  else
361
  {
362
    flags&= ~NO_DEFAULT_VALUE_FLAG;
363
  }
2221.12.5 by Stewart Smith
factor out setting default value out from CreatField::init and into new CreateField::setDefaultValue. This also means we fix up a error message to be correct in type_blob.
364
365
  if (sql_type == DRIZZLE_TYPE_BLOB && default_value_item)
366
  {
367
    /* Allow empty as default value. */
368
    String str,*res;
369
    res= default_value_item->val_str(&str);
370
    if (res->length())
371
    {
372
      my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0), field_name);
2318.6.55 by Olaf van der Spek
Refactor
373
      return true;
2221.12.5 by Stewart Smith
factor out setting default value out from CreatField::init and into new CreateField::setDefaultValue. This also means we fix up a error message to be correct in type_blob.
374
    }
375
  }
376
377
  if (sql_type == DRIZZLE_TYPE_TIMESTAMP
378
      || sql_type == DRIZZLE_TYPE_MICROTIME)
379
  {
2221.12.8 by Stewart Smith
fix logic for ON UPDATE for TEMPSTAMP columns in CreateField::setDefaultValue in ALTER TABLE SET DEFAULT code path (as we're modifying existing CreateField, not just creating one
380
    bool on_update_now= on_update_item
381
      || (unireg_check == Field::TIMESTAMP_DNUN_FIELD
382
          || unireg_check == Field::TIMESTAMP_UN_FIELD);
383
2221.12.5 by Stewart Smith
factor out setting default value out from CreatField::init and into new CreateField::setDefaultValue. This also means we fix up a error message to be correct in type_blob.
384
    if (default_value_item)
385
    {
386
      /* Grammar allows only NOW() value for ON UPDATE clause */
387
      if (default_value_item->type() == Item::FUNC_ITEM &&
388
          ((Item_func*)default_value_item)->functype() == Item_func::NOW_FUNC)
389
      {
2221.12.8 by Stewart Smith
fix logic for ON UPDATE for TEMPSTAMP columns in CreateField::setDefaultValue in ALTER TABLE SET DEFAULT code path (as we're modifying existing CreateField, not just creating one
390
        unireg_check= (on_update_now ? Field::TIMESTAMP_DNUN_FIELD:
2221.12.5 by Stewart Smith
factor out setting default value out from CreatField::init and into new CreateField::setDefaultValue. This also means we fix up a error message to be correct in type_blob.
391
                       Field::TIMESTAMP_DN_FIELD);
392
        /*
393
          We don't need default value any longer moreover it is dangerous.
394
          Everything handled by unireg_check further.
395
        */
396
        def= 0;
397
      }
398
      else
399
      {
2221.12.8 by Stewart Smith
fix logic for ON UPDATE for TEMPSTAMP columns in CreateField::setDefaultValue in ALTER TABLE SET DEFAULT code path (as we're modifying existing CreateField, not just creating one
400
        unireg_check= (on_update_now ? Field::TIMESTAMP_UN_FIELD:
2221.12.5 by Stewart Smith
factor out setting default value out from CreatField::init and into new CreateField::setDefaultValue. This also means we fix up a error message to be correct in type_blob.
401
                       Field::NONE);
402
      }
403
    }
404
    else
405
    {
406
      /*
407
        If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
408
        or ON UPDATE values then for the sake of compatiblity we should treat
409
        this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
410
        have another TIMESTAMP column with auto-set option before this one)
411
        or DEFAULT 0 (in other cases).
412
        So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
413
        replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
414
        information about all TIMESTAMP fields in table will be availiable.
415
416
        If we have TIMESTAMP NULL column without explicit DEFAULT value
417
        we treat it as having DEFAULT NULL attribute.
418
      */
2221.12.8 by Stewart Smith
fix logic for ON UPDATE for TEMPSTAMP columns in CreateField::setDefaultValue in ALTER TABLE SET DEFAULT code path (as we're modifying existing CreateField, not just creating one
419
      unireg_check= (on_update_now ? Field::TIMESTAMP_UN_FIELD :
2221.12.5 by Stewart Smith
factor out setting default value out from CreatField::init and into new CreateField::setDefaultValue. This also means we fix up a error message to be correct in type_blob.
420
                     (flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
421
                      Field::NONE));
422
    }
423
  }
424
425
  return false;
426
}
427
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
428
std::ostream& operator<<(std::ostream& output, const CreateField &field)
429
{
430
  output << "CreateField:(";
431
  output <<  field.field_name;
432
  output << ", ";
2246.4.6 by Olaf van der Spek
Remove some unnecessary drizzled::
433
  output << display::type(field.type());
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
434
  output << ", { ";
435
436
  if (field.flags & NOT_NULL_FLAG)
437
    output << " NOT_NULL";
438
439
  if (field.flags & PRI_KEY_FLAG)
440
    output << ", PRIMARY KEY";
441
442
  if (field.flags & UNIQUE_KEY_FLAG)
443
    output << ", UNIQUE KEY";
444
445
  if (field.flags & MULTIPLE_KEY_FLAG)
446
    output << ", MULTIPLE KEY";
447
448
  if (field.flags & BLOB_FLAG)
449
    output << ", BLOB";
450
451
  if (field.flags & UNSIGNED_FLAG)
452
    output << ", UNSIGNED";
453
454
  if (field.flags & BINARY_FLAG)
455
    output << ", BINARY";
456
  output << "}, ";
457
  if (field.field)
458
    output << *field.field;
459
  output << ")";
460
461
  return output;  // for multiple << operators.
462
}
463
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
464
} /* namespace drizzled */