1607
/****************************************************************************
1609
** This is a string which only can have a selection of different values.
1610
** If one uses this string in a number context one gets the type number.
1611
****************************************************************************/
1613
enum ha_base_keytype Field_enum::key_type() const
1615
switch (packlength) {
1616
default: return HA_KEYTYPE_BINARY;
1619
case 4: return HA_KEYTYPE_ULONG_INT;
1620
case 8: return HA_KEYTYPE_ULONGLONG;
1624
void Field_enum::store_type(uint64_t value)
1626
switch (packlength) {
1627
case 1: ptr[0]= (uchar) value; break;
1629
#ifdef WORDS_BIGENDIAN
1630
if (table->s->db_low_byte_first)
1632
int2store(ptr,(unsigned short) value);
1636
shortstore(ptr,(unsigned short) value);
1638
case 3: int3store(ptr,(long) value); break;
1640
#ifdef WORDS_BIGENDIAN
1641
if (table->s->db_low_byte_first)
1643
int4store(ptr,value);
1647
longstore(ptr,(long) value);
1650
#ifdef WORDS_BIGENDIAN
1651
if (table->s->db_low_byte_first)
1653
int8store(ptr,value);
1657
int64_tstore(ptr,value); break;
1664
Storing a empty string in a enum field gives a warning
1665
(if there isn't a empty value in the enum)
1668
int Field_enum::store(const char *from, uint length, const CHARSET_INFO * const cs)
1672
char buff[STRING_BUFFER_USUAL_SIZE];
1673
String tmpstr(buff,sizeof(buff), &my_charset_bin);
1675
/* Convert character set if necessary */
1676
if (String::needs_conversion(length, cs, field_charset, ¬_used))
1679
tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
1681
length= tmpstr.length();
1684
/* Remove end space */
1685
length= field_charset->cset->lengthsp(field_charset, from, length);
1686
uint tmp=find_type2(typelib, from, length, field_charset);
1689
if (length < 6) // Can't be more than 99999 enums
1691
/* This is for reading numbers with LOAD DATA INFILE */
1693
tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
1694
if (err || end != from+length || tmp > typelib->count)
1697
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1699
if (!table->in_use->count_cuted_fields)
1703
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1705
store_type((uint64_t) tmp);
1710
int Field_enum::store(double nr)
1712
return Field_enum::store((int64_t) nr, false);
1716
int Field_enum::store(int64_t nr,
1717
bool unsigned_val __attribute__((unused)))
1720
if ((uint64_t) nr > typelib->count || nr == 0)
1722
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1723
if (nr != 0 || table->in_use->count_cuted_fields)
1729
store_type((uint64_t) (uint) nr);
1734
double Field_enum::val_real(void)
1736
return (double) Field_enum::val_int();
1740
int64_t Field_enum::val_int(void)
1742
switch (packlength) {
1744
return (int64_t) ptr[0];
1748
#ifdef WORDS_BIGENDIAN
1749
if (table->s->db_low_byte_first)
1754
return (int64_t) tmp;
1757
return (int64_t) uint3korr(ptr);
1761
#ifdef WORDS_BIGENDIAN
1762
if (table->s->db_low_byte_first)
1767
return (int64_t) tmp;
1772
#ifdef WORDS_BIGENDIAN
1773
if (table->s->db_low_byte_first)
1777
int64_tget(tmp,ptr);
1781
return 0; // impossible
1786
Save the field metadata for enum fields.
1788
Saves the real type in the first byte and the pack length in the
1789
second byte of the field metadata array at index of *metadata_ptr and
1790
*(metadata_ptr + 1).
1792
@param metadata_ptr First byte of field metadata
1794
@returns number of bytes written to metadata_ptr
1796
int Field_enum::do_save_field_metadata(uchar *metadata_ptr)
1798
*metadata_ptr= real_type();
1799
*(metadata_ptr + 1)= pack_length();
1804
String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
1807
uint tmp=(uint) Field_enum::val_int();
1808
if (!tmp || tmp > typelib->count)
1809
val_ptr->set("", 0, field_charset);
1811
val_ptr->set((const char*) typelib->type_names[tmp-1],
1812
typelib->type_lengths[tmp-1],
1817
int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
1820
ptr= (uchar*) a_ptr;
1821
uint64_t a=Field_enum::val_int();
1822
ptr= (uchar*) b_ptr;
1823
uint64_t b=Field_enum::val_int();
1825
return (a < b) ? -1 : (a > b) ? 1 : 0;
1828
void Field_enum::sort_string(uchar *to,uint length __attribute__((unused)))
1830
uint64_t value=Field_enum::val_int();
1832
for (uint i=0 ; i < packlength ; i++)
1834
*to-- = (uchar) (value & 255);
1840
void Field_enum::sql_type(String &res) const
1843
String enum_item(buffer, sizeof(buffer), res.charset());
1846
res.append(STRING_WITH_LEN("enum("));
1849
uint *len= typelib->type_lengths;
1850
for (const char **pos= typelib->type_names; *pos; pos++, len++)
1855
/* convert to res.charset() == utf8, then quote */
1856
enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
1857
append_unescaped(&res, enum_item.ptr(), enum_item.length());
1864
Field *Field_enum::new_field(MEM_ROOT *root, Table *new_table,
1867
Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
1869
res->typelib= copy_typelib(root, typelib);
1876
1609
1 if the fields are equally defined