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