00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00026 #include <config.h>
00027 #include <cstdio>
00028 #include <errno.h>
00029 #include <float.h>
00030 #include <drizzled/sql_select.h>
00031 #include <drizzled/error.h>
00032 #include <drizzled/field/str.h>
00033 #include <drizzled/field/num.h>
00034 #include <drizzled/field/blob.h>
00035 #include <drizzled/field/boolean.h>
00036 #include <drizzled/field/enum.h>
00037 #include <drizzled/field/null.h>
00038 #include <drizzled/field/date.h>
00039 #include <drizzled/field/decimal.h>
00040 #include <drizzled/field/real.h>
00041 #include <drizzled/field/double.h>
00042 #include <drizzled/field/int32.h>
00043 #include <drizzled/field/int64.h>
00044 #include <drizzled/field/num.h>
00045 #include <drizzled/field/time.h>
00046 #include <drizzled/field/epoch.h>
00047 #include <drizzled/field/datetime.h>
00048 #include <drizzled/field/microtime.h>
00049 #include <drizzled/field/varstring.h>
00050 #include <drizzled/field/uuid.h>
00051 #include <drizzled/time_functions.h>
00052 #include <drizzled/internal/m_string.h>
00053 #include <drizzled/table.h>
00054 #include <drizzled/util/test.h>
00055 #include <drizzled/session.h>
00056 #include <drizzled/current_session.h>
00057 #include <drizzled/display.h>
00058 #include <drizzled/typelib.h>
00059
00060 namespace drizzled
00061 {
00062
00063
00064
00065
00066
00067 static enum_field_types
00068 field_types_merge_rules [enum_field_types_size][enum_field_types_size]=
00069 {
00070
00071 {
00072
00073 DRIZZLE_TYPE_LONG,
00074
00075 DRIZZLE_TYPE_DOUBLE,
00076
00077 DRIZZLE_TYPE_LONG,
00078
00079 DRIZZLE_TYPE_VARCHAR,
00080
00081 DRIZZLE_TYPE_LONGLONG,
00082
00083 DRIZZLE_TYPE_VARCHAR,
00084
00085 DRIZZLE_TYPE_VARCHAR,
00086
00087 DRIZZLE_TYPE_VARCHAR,
00088
00089 DRIZZLE_TYPE_DECIMAL,
00090
00091 DRIZZLE_TYPE_VARCHAR,
00092
00093 DRIZZLE_TYPE_BLOB,
00094
00095 DRIZZLE_TYPE_TIME,
00096
00097 DRIZZLE_TYPE_VARCHAR,
00098
00099 DRIZZLE_TYPE_VARCHAR,
00100
00101 DRIZZLE_TYPE_VARCHAR,
00102 },
00103
00104 {
00105
00106 DRIZZLE_TYPE_DOUBLE,
00107
00108 DRIZZLE_TYPE_DOUBLE,
00109
00110 DRIZZLE_TYPE_DOUBLE,
00111
00112 DRIZZLE_TYPE_VARCHAR,
00113
00114 DRIZZLE_TYPE_DOUBLE,
00115
00116 DRIZZLE_TYPE_VARCHAR,
00117
00118 DRIZZLE_TYPE_VARCHAR,
00119
00120 DRIZZLE_TYPE_VARCHAR,
00121
00122 DRIZZLE_TYPE_DOUBLE,
00123
00124 DRIZZLE_TYPE_VARCHAR,
00125
00126 DRIZZLE_TYPE_BLOB,
00127
00128 DRIZZLE_TYPE_TIME,
00129
00130 DRIZZLE_TYPE_VARCHAR,
00131
00132 DRIZZLE_TYPE_VARCHAR,
00133
00134 DRIZZLE_TYPE_VARCHAR,
00135 },
00136
00137 {
00138
00139 DRIZZLE_TYPE_LONG,
00140
00141 DRIZZLE_TYPE_DOUBLE,
00142
00143 DRIZZLE_TYPE_NULL,
00144
00145 DRIZZLE_TYPE_TIMESTAMP,
00146
00147 DRIZZLE_TYPE_LONGLONG,
00148
00149 DRIZZLE_TYPE_DATETIME,
00150
00151 DRIZZLE_TYPE_DATE,
00152
00153 DRIZZLE_TYPE_VARCHAR,
00154
00155 DRIZZLE_TYPE_DECIMAL,
00156
00157 DRIZZLE_TYPE_ENUM,
00158
00159 DRIZZLE_TYPE_BLOB,
00160
00161 DRIZZLE_TYPE_TIME,
00162
00163 DRIZZLE_TYPE_BOOLEAN,
00164
00165 DRIZZLE_TYPE_UUID,
00166
00167 DRIZZLE_TYPE_MICROTIME,
00168 },
00169
00170 {
00171
00172 DRIZZLE_TYPE_VARCHAR,
00173
00174 DRIZZLE_TYPE_VARCHAR,
00175
00176 DRIZZLE_TYPE_TIMESTAMP,
00177
00178 DRIZZLE_TYPE_TIMESTAMP,
00179
00180 DRIZZLE_TYPE_VARCHAR,
00181
00182 DRIZZLE_TYPE_DATETIME,
00183
00184 DRIZZLE_TYPE_DATE,
00185
00186 DRIZZLE_TYPE_VARCHAR,
00187
00188 DRIZZLE_TYPE_VARCHAR,
00189
00190 DRIZZLE_TYPE_VARCHAR,
00191
00192 DRIZZLE_TYPE_BLOB,
00193
00194 DRIZZLE_TYPE_TIME,
00195
00196 DRIZZLE_TYPE_VARCHAR,
00197
00198 DRIZZLE_TYPE_VARCHAR,
00199
00200 DRIZZLE_TYPE_VARCHAR,
00201 },
00202
00203 {
00204
00205 DRIZZLE_TYPE_LONGLONG,
00206
00207 DRIZZLE_TYPE_DOUBLE,
00208
00209 DRIZZLE_TYPE_LONGLONG,
00210
00211 DRIZZLE_TYPE_VARCHAR,
00212
00213 DRIZZLE_TYPE_LONGLONG,
00214
00215 DRIZZLE_TYPE_VARCHAR,
00216
00217 DRIZZLE_TYPE_DATE,
00218
00219 DRIZZLE_TYPE_VARCHAR,
00220
00221 DRIZZLE_TYPE_DECIMAL,
00222 DRIZZLE_TYPE_VARCHAR,
00223
00224 DRIZZLE_TYPE_BLOB,
00225
00226 DRIZZLE_TYPE_TIME,
00227
00228 DRIZZLE_TYPE_VARCHAR,
00229
00230 DRIZZLE_TYPE_VARCHAR,
00231
00232 DRIZZLE_TYPE_VARCHAR,
00233 },
00234
00235 {
00236
00237 DRIZZLE_TYPE_VARCHAR,
00238
00239 DRIZZLE_TYPE_VARCHAR,
00240
00241 DRIZZLE_TYPE_DATETIME,
00242
00243 DRIZZLE_TYPE_DATETIME,
00244
00245 DRIZZLE_TYPE_VARCHAR,
00246
00247 DRIZZLE_TYPE_DATETIME,
00248
00249 DRIZZLE_TYPE_DATE,
00250
00251 DRIZZLE_TYPE_VARCHAR,
00252
00253 DRIZZLE_TYPE_VARCHAR,
00254
00255 DRIZZLE_TYPE_VARCHAR,
00256
00257 DRIZZLE_TYPE_BLOB,
00258
00259 DRIZZLE_TYPE_TIME,
00260
00261 DRIZZLE_TYPE_VARCHAR,
00262
00263 DRIZZLE_TYPE_VARCHAR,
00264
00265 DRIZZLE_TYPE_VARCHAR,
00266 },
00267
00268 {
00269
00270 DRIZZLE_TYPE_VARCHAR,
00271
00272 DRIZZLE_TYPE_VARCHAR,
00273
00274 DRIZZLE_TYPE_DATE,
00275
00276 DRIZZLE_TYPE_DATETIME,
00277
00278 DRIZZLE_TYPE_VARCHAR,
00279
00280 DRIZZLE_TYPE_DATETIME,
00281
00282 DRIZZLE_TYPE_DATE,
00283
00284 DRIZZLE_TYPE_VARCHAR,
00285
00286 DRIZZLE_TYPE_VARCHAR,
00287
00288 DRIZZLE_TYPE_VARCHAR,
00289
00290 DRIZZLE_TYPE_BLOB,
00291
00292 DRIZZLE_TYPE_TIME,
00293
00294 DRIZZLE_TYPE_VARCHAR,
00295
00296 DRIZZLE_TYPE_VARCHAR,
00297
00298 DRIZZLE_TYPE_VARCHAR,
00299 },
00300
00301 {
00302
00303 DRIZZLE_TYPE_VARCHAR,
00304
00305 DRIZZLE_TYPE_VARCHAR,
00306
00307 DRIZZLE_TYPE_VARCHAR,
00308
00309 DRIZZLE_TYPE_VARCHAR,
00310
00311 DRIZZLE_TYPE_VARCHAR,
00312
00313 DRIZZLE_TYPE_VARCHAR,
00314
00315 DRIZZLE_TYPE_VARCHAR,
00316
00317 DRIZZLE_TYPE_VARCHAR,
00318
00319 DRIZZLE_TYPE_VARCHAR,
00320
00321 DRIZZLE_TYPE_VARCHAR,
00322
00323 DRIZZLE_TYPE_BLOB,
00324
00325 DRIZZLE_TYPE_TIME,
00326
00327 DRIZZLE_TYPE_VARCHAR,
00328
00329 DRIZZLE_TYPE_VARCHAR,
00330
00331 DRIZZLE_TYPE_VARCHAR,
00332 },
00333
00334 {
00335
00336 DRIZZLE_TYPE_DECIMAL,
00337
00338 DRIZZLE_TYPE_DOUBLE,
00339
00340 DRIZZLE_TYPE_DECIMAL,
00341
00342 DRIZZLE_TYPE_VARCHAR,
00343
00344 DRIZZLE_TYPE_DECIMAL,
00345
00346 DRIZZLE_TYPE_VARCHAR,
00347
00348 DRIZZLE_TYPE_VARCHAR,
00349
00350 DRIZZLE_TYPE_VARCHAR,
00351
00352 DRIZZLE_TYPE_DECIMAL,
00353
00354 DRIZZLE_TYPE_VARCHAR,
00355
00356 DRIZZLE_TYPE_BLOB,
00357
00358 DRIZZLE_TYPE_TIME,
00359
00360 DRIZZLE_TYPE_VARCHAR,
00361
00362 DRIZZLE_TYPE_VARCHAR,
00363
00364 DRIZZLE_TYPE_VARCHAR,
00365 },
00366
00367 {
00368
00369 DRIZZLE_TYPE_VARCHAR,
00370
00371 DRIZZLE_TYPE_VARCHAR,
00372
00373 DRIZZLE_TYPE_ENUM,
00374
00375 DRIZZLE_TYPE_VARCHAR,
00376
00377 DRIZZLE_TYPE_VARCHAR,
00378
00379 DRIZZLE_TYPE_VARCHAR,
00380
00381 DRIZZLE_TYPE_VARCHAR,
00382
00383 DRIZZLE_TYPE_VARCHAR,
00384
00385 DRIZZLE_TYPE_VARCHAR,
00386
00387 DRIZZLE_TYPE_VARCHAR,
00388
00389 DRIZZLE_TYPE_BLOB,
00390
00391 DRIZZLE_TYPE_TIME,
00392
00393 DRIZZLE_TYPE_VARCHAR,
00394
00395 DRIZZLE_TYPE_VARCHAR,
00396
00397 DRIZZLE_TYPE_VARCHAR,
00398 },
00399
00400 {
00401
00402 DRIZZLE_TYPE_BLOB,
00403
00404 DRIZZLE_TYPE_BLOB,
00405
00406 DRIZZLE_TYPE_BLOB,
00407
00408 DRIZZLE_TYPE_BLOB,
00409
00410 DRIZZLE_TYPE_BLOB,
00411
00412 DRIZZLE_TYPE_BLOB,
00413
00414 DRIZZLE_TYPE_BLOB,
00415
00416 DRIZZLE_TYPE_BLOB,
00417
00418 DRIZZLE_TYPE_BLOB,
00419
00420 DRIZZLE_TYPE_BLOB,
00421
00422 DRIZZLE_TYPE_BLOB,
00423
00424 DRIZZLE_TYPE_TIME,
00425
00426 DRIZZLE_TYPE_VARCHAR,
00427
00428 DRIZZLE_TYPE_VARCHAR,
00429
00430 DRIZZLE_TYPE_VARCHAR,
00431 },
00432
00433 {
00434
00435 DRIZZLE_TYPE_VARCHAR,
00436
00437 DRIZZLE_TYPE_VARCHAR,
00438
00439 DRIZZLE_TYPE_TIME,
00440
00441 DRIZZLE_TYPE_VARCHAR,
00442
00443 DRIZZLE_TYPE_VARCHAR,
00444
00445 DRIZZLE_TYPE_VARCHAR,
00446
00447 DRIZZLE_TYPE_VARCHAR,
00448
00449 DRIZZLE_TYPE_VARCHAR,
00450
00451 DRIZZLE_TYPE_VARCHAR,
00452
00453 DRIZZLE_TYPE_VARCHAR,
00454
00455 DRIZZLE_TYPE_BLOB,
00456
00457 DRIZZLE_TYPE_TIME,
00458
00459 DRIZZLE_TYPE_VARCHAR,
00460
00461 DRIZZLE_TYPE_UUID,
00462
00463 DRIZZLE_TYPE_VARCHAR,
00464 },
00465
00466 {
00467
00468 DRIZZLE_TYPE_VARCHAR,
00469
00470 DRIZZLE_TYPE_VARCHAR,
00471
00472 DRIZZLE_TYPE_BOOLEAN,
00473
00474 DRIZZLE_TYPE_VARCHAR,
00475
00476 DRIZZLE_TYPE_VARCHAR,
00477
00478 DRIZZLE_TYPE_VARCHAR,
00479
00480 DRIZZLE_TYPE_VARCHAR,
00481
00482 DRIZZLE_TYPE_VARCHAR,
00483
00484 DRIZZLE_TYPE_VARCHAR,
00485
00486 DRIZZLE_TYPE_VARCHAR,
00487
00488 DRIZZLE_TYPE_BLOB,
00489
00490 DRIZZLE_TYPE_TIME,
00491
00492 DRIZZLE_TYPE_BOOLEAN,
00493
00494 DRIZZLE_TYPE_VARCHAR,
00495
00496 DRIZZLE_TYPE_VARCHAR,
00497 },
00498
00499 {
00500
00501 DRIZZLE_TYPE_VARCHAR,
00502
00503 DRIZZLE_TYPE_VARCHAR,
00504
00505 DRIZZLE_TYPE_UUID,
00506
00507 DRIZZLE_TYPE_VARCHAR,
00508
00509 DRIZZLE_TYPE_VARCHAR,
00510
00511 DRIZZLE_TYPE_VARCHAR,
00512
00513 DRIZZLE_TYPE_VARCHAR,
00514
00515 DRIZZLE_TYPE_VARCHAR,
00516
00517 DRIZZLE_TYPE_VARCHAR,
00518
00519 DRIZZLE_TYPE_VARCHAR,
00520
00521 DRIZZLE_TYPE_BLOB,
00522
00523 DRIZZLE_TYPE_TIME,
00524
00525 DRIZZLE_TYPE_VARCHAR,
00526
00527 DRIZZLE_TYPE_UUID,
00528
00529 DRIZZLE_TYPE_VARCHAR,
00530 },
00531
00532 {
00533
00534 DRIZZLE_TYPE_VARCHAR,
00535
00536 DRIZZLE_TYPE_VARCHAR,
00537
00538 DRIZZLE_TYPE_MICROTIME,
00539
00540 DRIZZLE_TYPE_VARCHAR,
00541
00542 DRIZZLE_TYPE_VARCHAR,
00543
00544 DRIZZLE_TYPE_VARCHAR,
00545
00546 DRIZZLE_TYPE_VARCHAR,
00547
00548 DRIZZLE_TYPE_VARCHAR,
00549
00550 DRIZZLE_TYPE_VARCHAR,
00551
00552 DRIZZLE_TYPE_VARCHAR,
00553
00554 DRIZZLE_TYPE_BLOB,
00555
00556 DRIZZLE_TYPE_TIME,
00557
00558 DRIZZLE_TYPE_VARCHAR,
00559
00560 DRIZZLE_TYPE_UUID,
00561
00562 DRIZZLE_TYPE_MICROTIME,
00563 },
00564 };
00565
00566 static Item_result field_types_result_type [enum_field_types_size]=
00567 {
00568
00569 INT_RESULT,
00570
00571 REAL_RESULT,
00572
00573 STRING_RESULT,
00574
00575 STRING_RESULT,
00576
00577 INT_RESULT,
00578
00579 STRING_RESULT,
00580
00581 STRING_RESULT,
00582
00583 STRING_RESULT,
00584
00585 DECIMAL_RESULT,
00586
00587 STRING_RESULT,
00588
00589 STRING_RESULT,
00590
00591 STRING_RESULT,
00592
00593 STRING_RESULT,
00594
00595 STRING_RESULT,
00596
00597 STRING_RESULT,
00598 };
00599
00600 bool test_if_important_data(const CHARSET_INFO * const cs,
00601 const char *str,
00602 const char *strend)
00603 {
00604 if (cs != &my_charset_bin)
00605 str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
00606 return (str < strend);
00607 }
00608
00609 void *Field::operator new(size_t size)
00610 {
00611 return memory::sql_alloc(size);
00612 }
00613
00614 void *Field::operator new(size_t size, memory::Root *mem_root)
00615 {
00616 return mem_root->alloc_root(static_cast<uint32_t>(size));
00617 }
00618
00619 enum_field_types Field::field_type_merge(enum_field_types a,
00620 enum_field_types b)
00621 {
00622 assert(a < enum_field_types_size);
00623 assert(b < enum_field_types_size);
00624 return field_types_merge_rules[a][b];
00625 }
00626
00627 Item_result Field::result_merge_type(enum_field_types field_type)
00628 {
00629 assert(field_type < enum_field_types_size);
00630 return field_types_result_type[field_type];
00631 }
00632
00633 bool Field::eq(Field *field)
00634 {
00635 return (ptr == field->ptr && null_ptr == field->null_ptr &&
00636 null_bit == field->null_bit);
00637 }
00638
00639 uint32_t Field::pack_length() const
00640 {
00641 return field_length;
00642 }
00643
00644 uint32_t Field::pack_length_in_rec() const
00645 {
00646 return pack_length();
00647 }
00648
00649 uint32_t Field::data_length()
00650 {
00651 return pack_length();
00652 }
00653
00654 uint32_t Field::used_length()
00655 {
00656 return pack_length();
00657 }
00658
00659 uint32_t Field::sort_length() const
00660 {
00661 return pack_length();
00662 }
00663
00664 uint32_t Field::max_data_length() const
00665 {
00666 return pack_length();
00667 }
00668
00669 int Field::reset(void)
00670 {
00671 memset(ptr, 0, pack_length());
00672 return 0;
00673 }
00674
00675 void Field::reset_fields()
00676 {}
00677
00678 void Field::set_default()
00679 {
00680 ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->getInsertRecord());
00681 memcpy(ptr, ptr + l_offset, pack_length());
00682 if (null_ptr)
00683 *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
00684
00685 if (this == table->next_number_field)
00686 table->auto_increment_field_not_null= false;
00687 }
00688
00689 bool Field::binary() const
00690 {
00691 return true;
00692 }
00693
00694 bool Field::zero_pack() const
00695 {
00696 return true;
00697 }
00698
00699 enum ha_base_keytype Field::key_type() const
00700 {
00701 return HA_KEYTYPE_BINARY;
00702 }
00703
00704 uint32_t Field::key_length() const
00705 {
00706 return pack_length();
00707 }
00708
00709 enum_field_types Field::real_type() const
00710 {
00711 return type();
00712 }
00713
00714 int Field::cmp_max(const unsigned char *a, const unsigned char *b, uint32_t)
00715 {
00716 return cmp(a, b);
00717 }
00718
00719 int Field::cmp_binary(const unsigned char *a,const unsigned char *b, uint32_t)
00720 {
00721 return memcmp(a,b,pack_length());
00722 }
00723
00724 int Field::cmp_offset(uint32_t row_offset)
00725 {
00726 return cmp(ptr,ptr+row_offset);
00727 }
00728
00729 int Field::cmp_binary_offset(uint32_t row_offset)
00730 {
00731 return cmp_binary(ptr, ptr+row_offset);
00732 }
00733
00734 int Field::key_cmp(const unsigned char *a,const unsigned char *b)
00735 {
00736 return cmp(a, b);
00737 }
00738
00739 int Field::key_cmp(const unsigned char *str, uint32_t)
00740 {
00741 return cmp(ptr,str);
00742 }
00743
00744 uint32_t Field::decimals() const
00745 {
00746 return 0;
00747 }
00748
00749 bool Field::is_null(ptrdiff_t row_offset) const
00750 {
00751 return null_ptr ?
00752 (null_ptr[row_offset] & null_bit ? true : false) :
00753 table->null_row;
00754 }
00755
00756 bool Field::is_real_null(ptrdiff_t row_offset) const
00757 {
00758 return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
00759 }
00760
00761 bool Field::is_null_in_record(const unsigned char *record) const
00762 {
00763 if (! null_ptr)
00764 return false;
00765 return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
00766 }
00767
00768 bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset) const
00769 {
00770 if (! null_ptr)
00771 return false;
00772 return test(null_ptr[with_offset] & null_bit);
00773 }
00774
00775 void Field::set_null(ptrdiff_t row_offset)
00776 {
00777 if (null_ptr)
00778 null_ptr[row_offset]|= null_bit;
00779 }
00780
00781 void Field::set_notnull(ptrdiff_t row_offset)
00782 {
00783 if (null_ptr)
00784 null_ptr[row_offset]&= (unsigned char) ~null_bit;
00785 }
00786
00787 bool Field::maybe_null(void) const
00788 {
00789 return null_ptr != 0 || table->maybe_null;
00790 }
00791
00792 bool Field::real_maybe_null(void) const
00793 {
00794 return null_ptr != 0;
00795 }
00796
00797 bool Field::type_can_have_key_part(enum enum_field_types type)
00798 {
00799 switch (type) {
00800 case DRIZZLE_TYPE_VARCHAR:
00801 case DRIZZLE_TYPE_BLOB:
00802 return true;
00803 default:
00804 return false;
00805 }
00806 }
00807
00808 int Field::warn_if_overflow(int op_result)
00809 {
00810 if (op_result == E_DEC_OVERFLOW)
00811 {
00812 set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
00813 return E_DEC_OVERFLOW;
00814 }
00815 if (op_result == E_DEC_TRUNCATED)
00816 {
00817 set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
00818 return E_DEC_TRUNCATED;
00819 }
00820 return 0;
00821 }
00822
00823 void Field::init(Table *table_arg)
00824 {
00825 orig_table= table= table_arg;
00826 }
00827
00829 Field::Field(unsigned char *ptr_arg,
00830 uint32_t length_arg,
00831 unsigned char *null_ptr_arg,
00832 unsigned char null_bit_arg,
00833 utype unireg_check_arg,
00834 const char *field_name_arg) :
00835 ptr(ptr_arg),
00836 null_ptr(null_ptr_arg),
00837 table(NULL),
00838 orig_table(NULL),
00839 field_name(field_name_arg),
00840 comment(NULL_LEX_STRING),
00841 key_start(0),
00842 part_of_key(0),
00843 part_of_key_not_clustered(0),
00844 part_of_sortkey(0),
00845 unireg_check(unireg_check_arg),
00846 field_length(length_arg),
00847 flags(null_ptr ? 0: NOT_NULL_FLAG),
00848 field_index(0),
00849 null_bit(null_bit_arg),
00850 is_created_from_null_item(false)
00851 {
00852 }
00853
00854 void Field::hash(uint32_t *nr, uint32_t *nr2) const
00855 {
00856 if (is_null())
00857 {
00858 *nr^= (*nr << 1) | 1;
00859 }
00860 else
00861 {
00862 uint32_t len= pack_length();
00863 const CHARSET_INFO * const cs= charset();
00864 cs->coll->hash_sort(cs, ptr, len, nr, nr2);
00865 }
00866 }
00867
00868 void Field::copy_from_tmp(int row_offset)
00869 {
00870 memcpy(ptr,ptr+row_offset,pack_length());
00871 if (null_ptr)
00872 {
00873 *null_ptr= (unsigned char) ((null_ptr[0] &
00874 (unsigned char) ~(uint32_t) null_bit) |
00875 (null_ptr[row_offset] &
00876 (unsigned char) null_bit));
00877 }
00878 }
00879
00880 int Field::store_and_check(enum_check_fields check_level,
00881 const char *to,
00882 uint32_t length,
00883 const CHARSET_INFO * const cs)
00884
00885 {
00886 int res;
00887 enum_check_fields old_check_level= table->in_use->count_cuted_fields;
00888 table->in_use->count_cuted_fields= check_level;
00889 res= store(to, length, cs);
00890 table->in_use->count_cuted_fields= old_check_level;
00891 return res;
00892 }
00893
00894 unsigned char *Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length, bool)
00895 {
00896 uint32_t length= pack_length();
00897 set_if_smaller(length, max_length);
00898 memcpy(to, from, length);
00899 return to+length;
00900 }
00901
00902 unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
00903 {
00904 unsigned char *result= this->pack(to, from, UINT32_MAX, table->getShare()->db_low_byte_first);
00905 return(result);
00906 }
00907
00908 const unsigned char *Field::unpack(unsigned char* to,
00909 const unsigned char *from,
00910 uint32_t param_data,
00911 bool)
00912 {
00913 uint32_t length=pack_length();
00914 int from_type= 0;
00915
00916
00917
00918
00919 if (param_data > 255)
00920 {
00921 from_type= (param_data & 0xff00) >> 8U;
00922 param_data= param_data & 0x00ff;
00923 }
00924
00925 if ((param_data == 0) ||
00926 (length == param_data) ||
00927 (from_type != real_type()))
00928 {
00929 memcpy(to, from, length);
00930 return from+length;
00931 }
00932
00933 uint32_t len= (param_data && (param_data < length)) ?
00934 param_data : length;
00935
00936 memcpy(to, from, param_data > length ? length : len);
00937 return (from + len);
00938 }
00939
00940 const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
00941 {
00942 const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
00943 return(result);
00944 }
00945
00946 type::Decimal *Field::val_decimal(type::Decimal *) const
00947 {
00948
00949 assert(0);
00950 return 0;
00951 }
00952
00953
00954 void Field::make_field(SendField *field)
00955 {
00956 if (orig_table && orig_table->getShare()->getSchemaName() && *orig_table->getShare()->getSchemaName())
00957 {
00958 field->db_name= orig_table->getShare()->getSchemaName();
00959 field->org_table_name= orig_table->getShare()->getTableName();
00960 }
00961 else
00962 field->org_table_name= field->db_name= "";
00963 if (orig_table)
00964 {
00965 field->table_name= orig_table->getAlias();
00966 field->org_col_name= field_name;
00967 }
00968 else
00969 {
00970 field->table_name= "";
00971 field->org_col_name= "";
00972 }
00973 field->col_name= field_name;
00974 field->charsetnr= charset()->number;
00975 field->length= field_length;
00976 field->type= type();
00977 field->flags= table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
00978 field->decimals= 0;
00979 }
00980
00981 int64_t Field::convert_decimal2int64_t(const type::Decimal *val, bool, int *err)
00982 {
00983 int64_t i;
00984 if (warn_if_overflow(val->val_int32(E_DEC_ERROR &
00985 ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
00986 false, &i)))
00987 {
00988 i= (val->sign() ? INT64_MIN : INT64_MAX);
00989 *err= 1;
00990 }
00991 return i;
00992 }
00993
00994 uint32_t Field::fill_cache_field(CacheField *copy)
00995 {
00996 uint32_t store_length;
00997 copy->str=ptr;
00998 copy->length=pack_length();
00999 copy->blob_field=0;
01000 if (flags & BLOB_FLAG)
01001 {
01002 copy->blob_field=(Field_blob*) this;
01003 copy->strip=0;
01004 copy->length-= table->getShare()->sizeBlobPtr();
01005 return copy->length;
01006 }
01007 else
01008 {
01009 copy->strip=0;
01010 store_length= 0;
01011 }
01012 return copy->length+ store_length;
01013 }
01014
01015 bool Field::get_date(type::Time <ime, uint32_t fuzzydate) const
01016 {
01017 char buff[type::Time::MAX_STRING_LENGTH];
01018 String tmp(buff,sizeof(buff),&my_charset_bin),*res;
01019
01020 assert(getTable() and getTable()->getSession());
01021
01022 if (not (res= val_str_internal(&tmp)) or
01023 str_to_datetime_with_warn(getTable()->getSession(),
01024 res->ptr(), res->length(),
01025 <ime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
01026 {
01027 return true;
01028 }
01029
01030 return false;
01031 }
01032
01033 bool Field::get_time(type::Time <ime) const
01034 {
01035 char buff[type::Time::MAX_STRING_LENGTH];
01036 String tmp(buff,sizeof(buff),&my_charset_bin),*res;
01037
01038 if (not (res= val_str_internal(&tmp)) or
01039 str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), <ime))
01040 {
01041 return true;
01042 }
01043
01044 return false;
01045 }
01046
01047 int Field::store_time(type::Time <ime, type::timestamp_t)
01048 {
01049 String tmp;
01050
01051 ltime.convert(tmp);
01052
01053 return store(tmp.ptr(), tmp.length(), &my_charset_bin);
01054 }
01055
01056 bool Field::optimize_range(uint32_t idx, uint32_t)
01057 {
01058 return test(table->index_flags(idx) & HA_READ_RANGE);
01059 }
01060
01061 Field *Field::new_field(memory::Root *root, Table *new_table, bool)
01062 {
01063 Field *tmp;
01064 if (!(tmp= (Field*) root->memdup_root((char*) this,size_of())))
01065 return 0;
01066
01067 if (tmp->table->maybe_null)
01068 tmp->flags&= ~NOT_NULL_FLAG;
01069 tmp->table= new_table;
01070 tmp->key_start.reset();
01071 tmp->part_of_key.reset();
01072 tmp->part_of_sortkey.reset();
01073 tmp->unireg_check= Field::NONE;
01074 tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG);
01075 tmp->reset_fields();
01076 return tmp;
01077 }
01078
01079 Field *Field::new_key_field(memory::Root *root, Table *new_table,
01080 unsigned char *new_ptr,
01081 unsigned char *new_null_ptr,
01082 uint32_t new_null_bit)
01083 {
01084 Field *tmp;
01085 if ((tmp= new_field(root, new_table, table == new_table)))
01086 {
01087 tmp->ptr= new_ptr;
01088 tmp->null_ptr= new_null_ptr;
01089 tmp->null_bit= new_null_bit;
01090 }
01091 return tmp;
01092 }
01093
01094 Field *Field::clone(memory::Root *root, Table *new_table)
01095 {
01096 Field *tmp;
01097 if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
01098 {
01099 tmp->init(new_table);
01100 tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
01101 new_table->getDefaultValues()));
01102 }
01103 return tmp;
01104 }
01105
01106
01107 uint32_t Field::is_equal(CreateField *new_field_ptr)
01108 {
01109 return (new_field_ptr->sql_type == real_type());
01110 }
01111
01112 bool Field::eq_def(Field *field)
01113 {
01114 if (real_type() != field->real_type() || charset() != field->charset() ||
01115 pack_length() != field->pack_length())
01116 return 0;
01117 return 1;
01118 }
01119
01120 bool Field_enum::eq_def(Field *field)
01121 {
01122 if (!Field::eq_def(field))
01123 return 0;
01124
01125 TYPELIB *from_lib=((Field_enum*) field)->typelib;
01126
01127 if (typelib->count < from_lib->count)
01128 return 0;
01129
01130 for (uint32_t i=0 ; i < from_lib->count ; i++)
01131 {
01132 if (my_strnncoll(field_charset,
01133 (const unsigned char*)typelib->type_names[i],
01134 strlen(typelib->type_names[i]),
01135 (const unsigned char*)from_lib->type_names[i],
01136 strlen(from_lib->type_names[i])))
01137 return 0;
01138 }
01139
01140 return 1;
01141 }
01142
01143 uint32_t calc_pack_length(enum_field_types type,uint32_t length)
01144 {
01145 switch (type) {
01146 case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
01147 case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
01148 case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
01149 case DRIZZLE_TYPE_TIMESTAMP: return field::Epoch::max_string_length();
01150 case DRIZZLE_TYPE_BOOLEAN: return field::Boolean::max_string_length();
01151 case DRIZZLE_TYPE_DATE:
01152 case DRIZZLE_TYPE_ENUM:
01153 case DRIZZLE_TYPE_LONG: return 4;
01154 case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
01155 case DRIZZLE_TYPE_TIME:
01156 case DRIZZLE_TYPE_DATETIME:
01157 case DRIZZLE_TYPE_LONGLONG: return 8;
01158 case DRIZZLE_TYPE_NULL: return 0;
01159 case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
01160 case DRIZZLE_TYPE_DECIMAL:
01161 break;
01162 }
01163
01164 assert(0);
01165 abort();
01166 }
01167
01168 uint32_t pack_length_to_packflag(uint32_t type)
01169 {
01170 switch (type) {
01171 case 1: return 1 << FIELDFLAG_PACK_SHIFT;
01172 case 2: assert(1);
01173 case 3: assert(1);
01174 case 4: return f_settype(DRIZZLE_TYPE_LONG);
01175 case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
01176 }
01177 return 0;
01178 }
01179
01180
01181
01182
01183
01184 bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
01185 drizzled::error_t code,
01186 int cuted_increment)
01187 {
01188
01189
01190
01191
01192 Session *session= table ? table->in_use : current_session;
01193 if (session->count_cuted_fields)
01194 {
01195 session->cuted_fields+= cuted_increment;
01196 push_warning_printf(session, level, code, ER(code), field_name,
01197 session->row_count);
01198 return 0;
01199 }
01200 return level >= DRIZZLE_ERROR::WARN_LEVEL_WARN;
01201 }
01202
01203
01204 void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
01205 drizzled::error_t code,
01206 const char *str,
01207 uint32_t str_length,
01208 type::timestamp_t ts_type,
01209 int cuted_increment)
01210 {
01211 Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
01212
01213 if ((session->abortOnWarning() and
01214 level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
01215 set_warning(level, code, cuted_increment))
01216 make_truncated_value_warning(session, level, str, str_length, ts_type,
01217 field_name);
01218 }
01219
01220 void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
01221 drizzled::error_t code,
01222 int64_t nr,
01223 type::timestamp_t ts_type,
01224 int cuted_increment)
01225 {
01226 Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
01227
01228 if (session->abortOnWarning() or
01229 set_warning(level, code, cuted_increment))
01230 {
01231 char str_nr[DECIMAL_LONGLONG_DIGITS];
01232 char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
01233 make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
01234 ts_type, field_name);
01235 }
01236 }
01237
01238 void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
01239 const drizzled::error_t code,
01240 double nr,
01241 type::timestamp_t ts_type)
01242 {
01243 Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
01244
01245 if (session->abortOnWarning() or
01246 set_warning(level, code, 1))
01247 {
01248
01249 char str_nr[DBL_DIG + 8];
01250 uint32_t str_len= snprintf(str_nr, sizeof(str_nr), "%g", nr);
01251 make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
01252 field_name);
01253 }
01254 }
01255
01256 bool Field::isReadSet() const
01257 {
01258 return table->isReadSet(field_index);
01259 }
01260
01261 bool Field::isWriteSet()
01262 {
01263 return table->isWriteSet(field_index);
01264 }
01265
01266 void Field::setReadSet(bool arg)
01267 {
01268 if (arg)
01269 table->setReadSet(field_index);
01270 else
01271 table->clearReadSet(field_index);
01272 }
01273
01274 void Field::setWriteSet(bool arg)
01275 {
01276 if (arg)
01277 table->setWriteSet(field_index);
01278 else
01279 table->clearWriteSet(field_index);
01280 }
01281
01282 void Field::pack_num(uint64_t arg, unsigned char *destination)
01283 {
01284 if (not destination)
01285 destination= ptr;
01286
01287 int64_tstore(destination, arg);
01288 }
01289
01290 void Field::pack_num(uint32_t arg, unsigned char *destination)
01291 {
01292 if (not destination)
01293 destination= ptr;
01294
01295 longstore(destination, arg);
01296 }
01297
01298 uint64_t Field::unpack_num(uint64_t &destination, const unsigned char *arg) const
01299 {
01300 if (not arg)
01301 arg= ptr;
01302
01303 int64_tget(destination, arg);
01304
01305 return destination;
01306 }
01307
01308 uint32_t Field::unpack_num(uint32_t &destination, const unsigned char *arg) const
01309 {
01310 if (not arg)
01311 arg= ptr;
01312
01313 longget(destination, arg);
01314
01315 return destination;
01316 }
01317
01318 std::ostream& operator<<(std::ostream& output, const Field &field)
01319 {
01320 output << "Field:(";
01321 output << field.field_name;
01322 output << ", ";
01323 output << drizzled::display::type(field.real_type());
01324 output << ", { ";
01325
01326 if (field.flags & NOT_NULL_FLAG)
01327 output << " NOT_NULL";
01328
01329 if (field.flags & PRI_KEY_FLAG)
01330 output << ", PRIMARY KEY";
01331
01332 if (field.flags & UNIQUE_KEY_FLAG)
01333 output << ", UNIQUE KEY";
01334
01335 if (field.flags & MULTIPLE_KEY_FLAG)
01336 output << ", MULTIPLE KEY";
01337
01338 if (field.flags & BLOB_FLAG)
01339 output << ", BLOB";
01340
01341 if (field.flags & UNSIGNED_FLAG)
01342 output << ", UNSIGNED";
01343
01344 if (field.flags & BINARY_FLAG)
01345 output << ", BINARY";
01346 output << "}, ";
01347 output << ")";
01348
01349 return output;
01350 }
01351
01352 }