21
21
* @file Implementation of CreateField class
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/enum.h"
35
#include "drizzled/field/null.h"
36
#include "drizzled/field/date.h"
37
#include "drizzled/field/decimal.h"
38
#include "drizzled/field/real.h"
39
#include "drizzled/field/double.h"
40
#include "drizzled/field/int32.h"
41
#include "drizzled/field/int64.h"
42
#include "drizzled/field/num.h"
43
#include "drizzled/field/timestamp.h"
44
#include "drizzled/field/datetime.h"
45
#include "drizzled/field/varstring.h"
46
#include "drizzled/field/uuid.h"
47
#include "drizzled/temporal.h"
48
#include "drizzled/item/string.h"
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>
48
#include <drizzled/temporal.h>
49
#include <drizzled/item/string.h>
50
#include <drizzled/table.h>
52
#include <drizzled/display.h>
50
54
#include <algorithm>
180
186
char *fld_length,
181
187
char *fld_decimals,
182
188
uint32_t fld_type_modifier,
183
Item *fld_default_value,
184
Item *fld_on_update_value,
185
189
LEX_STRING *fld_comment,
186
190
char *fld_change,
187
191
List<String> *fld_interval_list,
188
const CHARSET_INFO * const fld_charset,
192
const charset_info_st * const fld_charset,
190
194
enum column_format_type column_format_in)
193
196
uint32_t sign_len= 0;
194
197
uint32_t allowed_type_modifier= 0;
216
218
pack_length= key_length= 0;
217
219
charset= fld_charset;
218
interval_list.empty();
220
interval_list.clear();
220
222
comment= *fld_comment;
223
Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
224
it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
226
if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
227
(fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
228
flags|= NO_DEFAULT_VALUE_FLAG;
230
224
if (fld_length && !(length= (uint32_t) atoi(fld_length)))
232
226
sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
258
252
my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
261
length= my_decimal_precision_to_length(length, decimals, fld_type_modifier & UNSIGNED_FLAG);
262
pack_length= my_decimal_get_binary_size(length, decimals);
255
length= class_decimal_precision_to_length(length, decimals, fld_type_modifier & UNSIGNED_FLAG);
256
pack_length= class_decimal_get_binary_size(length, decimals);
264
258
case DRIZZLE_TYPE_VARCHAR:
282
case DRIZZLE_TYPE_MICROTIME:
284
This assert() should be correct due to absence of length
285
specifiers for timestamp. Previous manipulation also wasn't
286
ever called (from examining lcov)
300
289
case DRIZZLE_TYPE_TIMESTAMP:
303
length= DateTime::MAX_STRING_LENGTH;
306
/* This assert() should be correct due to absence of length
307
specifiers for timestamp. Previous manipulation also wasn't
308
ever called (from examining lcov)
310
assert(length == (uint32_t)DateTime::MAX_STRING_LENGTH);
312
flags|= UNSIGNED_FLAG;
313
if (fld_default_value)
315
/* Grammar allows only NOW() value for ON UPDATE clause */
316
if (fld_default_value->type() == Item::FUNC_ITEM &&
317
((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
319
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
320
Field::TIMESTAMP_DN_FIELD);
322
We don't need default value any longer moreover it is dangerous.
323
Everything handled by unireg_check further.
328
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
334
If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
335
or ON UPDATE values then for the sake of compatiblity we should treat
336
this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
337
have another TIMESTAMP column with auto-set option before this one)
338
or DEFAULT 0 (in other cases).
339
So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
340
replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
341
information about all TIMESTAMP fields in table will be availiable.
343
If we have TIMESTAMP NULL column without explicit DEFAULT value
344
we treat it as having DEFAULT NULL attribute.
346
unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
347
(flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
290
length= MicroTimestamp::MAX_STRING_LENGTH;
351
292
case DRIZZLE_TYPE_DATE:
352
293
length= Date::MAX_STRING_LENGTH;
354
295
case DRIZZLE_TYPE_UUID:
355
296
length= field::Uuid::max_string_length();
298
case DRIZZLE_TYPE_BOOLEAN:
299
length= field::Boolean::max_string_length();
357
301
case DRIZZLE_TYPE_DATETIME:
358
302
length= DateTime::MAX_STRING_LENGTH;
304
case DRIZZLE_TYPE_TIME:
305
length= DateTime::MAX_STRING_LENGTH;
360
307
case DRIZZLE_TYPE_ENUM:
362
309
/* Should be safe. */
365
List_iterator<String> it(*fld_interval_list);
312
List<String>::iterator it(fld_interval_list->begin());
367
314
while ((tmp= it++))
368
315
interval_list.push_back(tmp);
376
323
if (!(flags & BLOB_FLAG) &&
377
324
((length > max_field_charlength &&
378
fld_type != DRIZZLE_TYPE_ENUM &&
379
(fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
325
fld_type != DRIZZLE_TYPE_ENUM &&
326
(fld_type != DRIZZLE_TYPE_VARCHAR)) ||
380
327
(!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
382
329
my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ? ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
384
fld_name, max_field_charlength);
331
fld_name, max_field_charlength / (charset? charset->mbmaxlen : 1));
387
334
fld_type_modifier&= AUTO_INCREMENT_FLAG;
394
341
return false; /* success */
344
bool CreateField::setDefaultValue(Item *default_value_item,
345
Item *on_update_item)
347
def= default_value_item;
350
Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
351
it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
353
if (! default_value_item
354
&& ! (flags & AUTO_INCREMENT_FLAG)
355
&& (flags & NOT_NULL_FLAG)
356
&& (sql_type != DRIZZLE_TYPE_TIMESTAMP
357
and sql_type != DRIZZLE_TYPE_MICROTIME))
359
flags|= NO_DEFAULT_VALUE_FLAG;
363
flags&= ~NO_DEFAULT_VALUE_FLAG;
366
if (sql_type == DRIZZLE_TYPE_BLOB && default_value_item)
368
/* Allow empty as default value. */
370
res= default_value_item->val_str(&str);
373
my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0), field_name);
378
if (sql_type == DRIZZLE_TYPE_TIMESTAMP
379
|| sql_type == DRIZZLE_TYPE_MICROTIME)
381
bool on_update_now= on_update_item
382
|| (unireg_check == Field::TIMESTAMP_DNUN_FIELD
383
|| unireg_check == Field::TIMESTAMP_UN_FIELD);
385
if (default_value_item)
387
/* Grammar allows only NOW() value for ON UPDATE clause */
388
if (default_value_item->type() == Item::FUNC_ITEM &&
389
((Item_func*)default_value_item)->functype() == Item_func::NOW_FUNC)
391
unireg_check= (on_update_now ? Field::TIMESTAMP_DNUN_FIELD:
392
Field::TIMESTAMP_DN_FIELD);
394
We don't need default value any longer moreover it is dangerous.
395
Everything handled by unireg_check further.
401
unireg_check= (on_update_now ? Field::TIMESTAMP_UN_FIELD:
408
If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
409
or ON UPDATE values then for the sake of compatiblity we should treat
410
this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
411
have another TIMESTAMP column with auto-set option before this one)
412
or DEFAULT 0 (in other cases).
413
So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
414
replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
415
information about all TIMESTAMP fields in table will be availiable.
417
If we have TIMESTAMP NULL column without explicit DEFAULT value
418
we treat it as having DEFAULT NULL attribute.
420
unireg_check= (on_update_now ? Field::TIMESTAMP_UN_FIELD :
421
(flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
429
std::ostream& operator<<(std::ostream& output, const CreateField &field)
431
output << "CreateField:(";
432
output << field.field_name;
434
output << display::type(field.type());
437
if (field.flags & NOT_NULL_FLAG)
438
output << " NOT_NULL";
440
if (field.flags & PRI_KEY_FLAG)
441
output << ", PRIMARY KEY";
443
if (field.flags & UNIQUE_KEY_FLAG)
444
output << ", UNIQUE KEY";
446
if (field.flags & MULTIPLE_KEY_FLAG)
447
output << ", MULTIPLE KEY";
449
if (field.flags & BLOB_FLAG)
452
if (field.flags & UNSIGNED_FLAG)
453
output << ", UNSIGNED";
455
if (field.flags & BINARY_FLAG)
456
output << ", BINARY";
459
output << *field.field;
462
return output; // for multiple << operators.
397
465
} /* namespace drizzled */