1 #ifndef SQL_CREATE_FIELD_INCLUDED 2 #define SQL_CREATE_FIELD_INCLUDED 3 4 /* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. 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, version 2.0, 8 as published by the Free Software Foundation. 9 10 This program is also distributed with certain software (including 11 but not limited to OpenSSL) that is licensed under separate terms, 12 as designated in a particular file or component or in included license 13 documentation. The authors of MySQL hereby grant you an additional 14 permission to link the program and your derivative works with the 15 separately licensed software that they have included with MySQL. 16 17 This program is distributed in the hope that it will be useful, 18 but WITHOUT ANY WARRANTY; without even the implied warranty of 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 GNU General Public License, version 2.0, for more details. 21 22 You should have received a copy of the GNU General Public License 23 along with this program; if not, write to the Free Software 24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 25 26 #include "field_types.h" 27 #include "lex_string.h" 28 #include "m_ctype.h" 29 #include "my_alloc.h" 30 #include "my_base.h" 31 #include "nullable.h" 32 #include "sql/dd/types/column.h" 33 #include "sql/field.h" 34 #include "sql/gis/srid.h" 35 #include "sql/sql_list.h" 36 #include "typelib.h" 37 38 class Item; 39 class String; 40 class Value_generator; 41 42 /// Create_field is a description a field/column that may or may not exists in 43 /// a table. 44 /// 45 /// The main usage of Create_field is to contain the description of a column 46 /// given by the user (usually given with CREATE TABLE). It is also used to 47 /// describe changes to be carried out on a column (usually given with ALTER 48 /// TABLE ... CHANGE COLUMN). 49 class Create_field { 50 public: 51 /// @returns the maximum display width of this column, in number of. 52 /// code points. See m_max_display_width_in_codepoints for an 53 /// explanation of "display width" and "code point". 54 size_t max_display_width_in_codepoints() const; 55 56 /// @returns the maximum display width of this column, in number of bytes. For 57 /// numeric types, temporal types, YEAR and BIT, this method returns 58 /// the same as max_display_width_in_codepoints(). For character 59 /// types (CHAR, VARCHAR, TEXT etc) the returned value depends on 60 /// max_display_width_in_codepoints() and the character set of this 61 /// column. 62 size_t max_display_width_in_bytes() const; 63 64 /// @returns the pack length for this column, which is the number of bytes 65 /// needed to store this column in memory. Note that blob returns 66 /// a length variable + the size of a pointer to an external memory 67 /// location where the actual data is stored. So LONGBLOB would 68 /// return 4 bytes for the length variable + 8 bytes for the pointer 69 /// to the data (12 bytes in total). 70 /// @param dont_override Don't use pack_length_override even if non-zero 71 /// Used by multi-valued index, where pack_length 72 /// and key_length aren't the same. 73 size_t pack_length(bool dont_override = false) const; 74 75 /// @returns the key length for this column. 76 size_t key_length() const; 77 78 /// @retval true if the maximum column length was given explicitly by the 79 /// user. 80 /// @retval false if the user didn't specify any maximum length. explicit_display_width()81 bool explicit_display_width() const { return m_explicit_display_width; } 82 83 /// Set the maximum display width based on another Create_field. set_max_display_width_from_create_field(const Create_field & create_field)84 void set_max_display_width_from_create_field( 85 const Create_field &create_field) { 86 m_max_display_width_in_codepoints = 87 create_field.max_display_width_in_codepoints(); 88 } 89 90 dd::Column::enum_hidden_type hidden; 91 92 const char *field_name; 93 /** 94 Name of column modified by ALTER TABLE's CHANGE/MODIFY COLUMN clauses, 95 NULL for columns added. 96 */ 97 const char *change; 98 const char *after; // Put column after this one 99 LEX_CSTRING comment; // Comment for field 100 101 /** 102 The declared default value, if any, otherwise NULL. Note that this member 103 is NULL if the default is a function. If the column definition has a 104 function declared as the default, the information is found in 105 Create_field::auto_flags. 106 107 @see Create_field::auto_flags 108 */ 109 Item *constant_default; 110 enum_field_types sql_type; 111 uint decimals; 112 uint flags{0}; 113 /** 114 Bitmap of flags indicating if field value should be auto-generated 115 by default and/or on update, and in which way. 116 117 @sa Field::enum_auto_flags for possible options. 118 */ 119 uchar auto_flags{Field::NONE}; 120 TYPELIB *interval; // Which interval to use 121 // Used only for UCS2 intervals 122 List<String> interval_list; 123 const CHARSET_INFO *charset; 124 bool is_explicit_collation; // User exeplicitly provided charset ? 125 Field::geometry_type geom_type; 126 Field *field; // For alter table 127 128 uint offset; 129 130 /** 131 Indicate whether column is nullable, zerofill or unsigned. 132 133 Initialized based on flags and other members at prepare_create_field()/ 134 init_for_tmp_table() stage. 135 */ 136 bool is_nullable; 137 bool is_zerofill; 138 bool is_unsigned; 139 140 /** 141 Indicates that storage engine doesn't support optimized BIT field 142 storage. 143 144 @note We also use safe/non-optimized version of BIT field for 145 special cases like virtual temporary tables. 146 147 Initialized at mysql_prepare_create_table()/sp_prepare_create_field()/ 148 init_for_tmp_table() stage. 149 */ 150 bool treat_bit_as_char; 151 152 /** 153 Row based replication code sometimes needs to create ENUM and SET 154 fields with pack length which doesn't correspond to number of 155 elements in interval TYPELIB. 156 157 When this member is non-zero ENUM/SET field to be created will use 158 its value as pack length instead of one calculated from number 159 elements in its interval. 160 161 Initialized at prepare_create_field()/init_for_tmp_table() stage. 162 */ 163 uint pack_length_override{0}; 164 165 /* Generated column expression information */ 166 Value_generator *gcol_info{nullptr}; 167 /* 168 Indication that the field is phycically stored in tables 169 rather than just generated on SQL queries. 170 As of now, false can only be set for virtual generated columns. 171 */ 172 bool stored_in_db; 173 174 /// Holds the expression to be used to generate default values. 175 Value_generator *m_default_val_expr{nullptr}; 176 Nullable<gis::srid_t> m_srid; 177 178 // Whether the field is actually an array of the field's type; 179 bool is_array{false}; 180 181 LEX_CSTRING m_engine_attribute = EMPTY_CSTR; 182 LEX_CSTRING m_secondary_engine_attribute = EMPTY_CSTR; 183 Create_field()184 Create_field() 185 : after(nullptr), 186 is_explicit_collation(false), 187 geom_type(Field::GEOM_GEOMETRY), 188 is_nullable(false), 189 is_zerofill(false), 190 is_unsigned(false), 191 /* 192 Initialize treat_bit_as_char for all field types even if 193 it is only used for MYSQL_TYPE_BIT. This avoids bogus 194 valgrind warnings in optimized builds. 195 */ 196 treat_bit_as_char(false), 197 pack_length_override(0), 198 stored_in_db(false), 199 m_default_val_expr(nullptr) {} 200 Create_field(Field *field, Field *orig_field); 201 202 /* Used to make a clone of this object for ALTER/CREATE TABLE */ clone(MEM_ROOT * mem_root)203 Create_field *clone(MEM_ROOT *mem_root) const { 204 return new (mem_root) Create_field(*this); 205 } is_gcol()206 bool is_gcol() const { return gcol_info; } is_virtual_gcol()207 bool is_virtual_gcol() const { 208 return gcol_info && !gcol_info->get_field_stored(); 209 } 210 211 /* Init for a tmp table field. To be extended if need be. */ 212 void init_for_tmp_table(enum_field_types sql_type_arg, uint32 max_length, 213 uint32 decimals, bool is_nullable, bool is_unsigned, 214 uint pack_length_override, 215 const char *field_name = ""); 216 217 bool init(THD *thd, const char *field_name, enum_field_types type, 218 const char *length, const char *decimals, uint type_modifier, 219 Item *default_value, Item *on_update_value, 220 const LEX_CSTRING *comment, const char *change, 221 List<String> *interval_list, const CHARSET_INFO *cs, 222 bool has_explicit_collation, uint uint_geom_type, 223 Value_generator *gcol_info, Value_generator *default_val_expr, 224 Nullable<gis::srid_t> srid, dd::Column::enum_hidden_type hidden, 225 bool is_array = false); 226 field_storage_type()227 ha_storage_media field_storage_type() const { 228 return (ha_storage_media)((flags >> FIELD_FLAGS_STORAGE_MEDIA) & 3); 229 } 230 column_format()231 column_format_type column_format() const { 232 return (column_format_type)((flags >> FIELD_FLAGS_COLUMN_FORMAT) & 3); 233 } 234 235 private: 236 /// The maximum display width of this column. 237 /// 238 /// The "display width" is the number of code points that is needed to print 239 /// out the string represenation of a value. It can be given by the user 240 /// both explicitly and implicitly. If a user creates a table with the columns 241 /// "a VARCHAR(3), b INT(3)", both columns are given an explicit display width 242 /// of 3 code points. But if a user creates a table with the columns 243 /// "a INT, b TINYINT UNSIGNED", the first column has an implicit display 244 /// width of 11 (-2147483648 is the longest value for a signed int) and the 245 /// second column has an implicit display width of 3 (255 is the longest value 246 /// for an unsigned tinyint). 247 /// This is related to storage size for some types (VARCHAR, BLOB etc), but 248 /// not for all types (an INT is four bytes regardless of the display width). 249 /// 250 /// A "code point" is bascially a numeric value. For instance, ASCII 251 /// compromises of 128 code points (0x00 to 0x7F), while unicode contains way 252 /// more. In most cases a code point represents a single graphical unit (aka 253 /// grapheme), but not always. For instance, É may consists of two code points 254 /// where one is the letter E and the other one is the quotation mark above 255 /// the letter. 256 size_t m_max_display_width_in_codepoints{0}; 257 258 /// Whether or not the display width was given explicitly by the user. 259 bool m_explicit_display_width{false}; 260 261 /// The maximum number of bytes a TINYBLOB can hold. 262 static constexpr size_t TINYBLOB_MAX_SIZE_IN_BYTES{255}; 263 264 /// The maximum number of bytes a BLOB can hold. 265 static constexpr size_t BLOB_MAX_SIZE_IN_BYTES{65535}; 266 267 /// The maximum number of bytes a MEDIUMBLOB can hold. 268 static constexpr size_t MEDIUMBLOB_MAX_SIZE_IN_BYTES{16777215}; 269 270 /// The maximum number of bytes a LONGBLOB can hold. 271 static constexpr size_t LONGBLOB_MAX_SIZE_IN_BYTES{4294967295}; 272 }; 273 274 /// @returns whether or not this field is a hidden column that represents a 275 /// functional index. 276 bool is_field_for_functional_index(const Create_field *create_field); 277 278 #endif 279