MySQL  8.0.24
Source Code Documentation
create_field.h
Go to the documentation of this file.
1 #ifndef SQL_CREATE_FIELD_INCLUDED
2 #define SQL_CREATE_FIELD_INCLUDED
3 
4 /* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
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.
82 
83  /// Set the maximum display width based on another Create_field.
85  const Create_field &create_field) {
87  create_field.max_display_width_in_codepoints();
88  }
89 
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  */
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  */
120  TYPELIB *interval; // Which interval to use
121  // Used only for UCS2 intervals
124  bool is_explicit_collation; // User exeplicitly provided charset ?
126  Field *field; // For alter table
127 
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  */
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  */
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  */
164 
165  /* Generated column expression information */
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  */
173 
174  /// Holds the expression to be used to generate default values.
177 
178  // Whether the field is actually an array of the field's type;
179  bool is_array{false};
180 
183 
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),
198  stored_in_db(false),
200  Create_field(Field *field, Field *orig_field);
201 
202  /* Used to make a clone of this object for ALTER/CREATE TABLE */
204  return new (mem_root) Create_field(*this);
205  }
206  bool is_gcol() const { return gcol_info; }
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,
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,
222  bool has_explicit_collation, uint uint_geom_type,
223  Value_generator *gcol_info, Value_generator *default_val_expr,
225  bool is_array = false);
226 
229  }
230 
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.
257 
258  /// Whether or not the display width was given explicitly by the user.
260 };
261 
262 /// @returns whether or not this field is a hidden column that represents a
263 /// functional index.
264 bool is_field_for_functional_index(const Create_field *create_field);
265 
266 /**
267  @retval true If this column is hidden either in the storage engine
268  or SQL layer. Either way, it is completely hidden from
269  the user.
270  @retval false Otherwise.
271 */
272 bool is_hidden_by_system(const Create_field *create_field);
273 
274 /**
275  @retval true If this column is hidden by the user.
276  @retval false otherwise.
277 */
278 bool is_hidden_by_user(const Create_field *create_field);
279 #endif
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:49
const char * change
Name of column modified by ALTER TABLE's CHANGE/MODIFY COLUMN clauses, NULL for columns added.
Definition: create_field.h:97
bool treat_bit_as_char
Indicates that storage engine doesn't support optimized BIT field storage.
Definition: create_field.h:150
bool stored_in_db
Definition: create_field.h:172
size_t max_display_width_in_codepoints() const
Definition: create_field.cc:627
TYPELIB * interval
Definition: create_field.h:120
ha_storage_media field_storage_type() const
Definition: create_field.h:227
uint decimals
Definition: create_field.h:111
Value_generator * gcol_info
Definition: create_field.h:166
Value_generator * m_default_val_expr
Holds the expression to be used to generate default values.
Definition: create_field.h:175
bool is_zerofill
Definition: create_field.h:137
bool init(THD *thd, const char *field_name, enum_field_types type, const char *length, const char *decimals, uint type_modifier, Item *default_value, Item *on_update_value, const LEX_CSTRING *comment, const char *change, List< String > *interval_list, const CHARSET_INFO *cs, bool has_explicit_collation, uint uint_geom_type, Value_generator *gcol_info, Value_generator *default_val_expr, Nullable< gis::srid_t > srid, dd::Column::enum_hidden_type hidden, bool is_array=false)
Initialize a column definition object.
Definition: create_field.cc:184
dd::Column::enum_hidden_type hidden
Definition: create_field.h:90
uint pack_length_override
Row based replication code sometimes needs to create ENUM and SET fields with pack length which doesn...
Definition: create_field.h:163
uint flags
Definition: create_field.h:112
Nullable< gis::srid_t > m_srid
Definition: create_field.h:176
bool is_explicit_collation
Definition: create_field.h:124
size_t key_length() const
Definition: create_field.cc:746
const CHARSET_INFO * charset
Definition: create_field.h:123
const char * field_name
Definition: create_field.h:92
bool is_virtual_gcol() const
Definition: create_field.h:207
List< String > interval_list
Definition: create_field.h:122
LEX_CSTRING m_engine_attribute
Definition: create_field.h:181
bool is_unsigned
Definition: create_field.h:138
bool explicit_display_width() const
Definition: create_field.h:81
Create_field()
Definition: create_field.h:184
uint offset
Definition: create_field.h:128
bool is_gcol() const
Definition: create_field.h:206
const char * after
Definition: create_field.h:98
void init_for_tmp_table(enum_field_types sql_type_arg, uint32 max_length, uint32 decimals, bool is_nullable, bool is_unsigned, uint pack_length_override, const char *field_name="")
Init for a tmp table field.
Definition: create_field.cc:570
Field::geometry_type geom_type
Definition: create_field.h:125
uchar auto_flags
Bitmap of flags indicating if field value should be auto-generated by default and/or on update,...
Definition: create_field.h:119
size_t pack_length(bool dont_override=false) const
Definition: create_field.cc:714
column_format_type column_format() const
Definition: create_field.h:231
Create_field * clone(MEM_ROOT *mem_root) const
Definition: create_field.h:203
size_t max_display_width_in_bytes() const
Definition: create_field.cc:674
bool is_array
Definition: create_field.h:179
bool m_explicit_display_width
Whether or not the display width was given explicitly by the user.
Definition: create_field.h:259
size_t m_max_display_width_in_codepoints
The maximum display width of this column.
Definition: create_field.h:256
Field * field
Definition: create_field.h:126
bool is_nullable
Indicate whether column is nullable, zerofill or unsigned.
Definition: create_field.h:136
Item * constant_default
The declared default value, if any, otherwise NULL.
Definition: create_field.h:109
LEX_CSTRING comment
Definition: create_field.h:99
enum_field_types sql_type
Definition: create_field.h:110
LEX_CSTRING m_secondary_engine_attribute
Definition: create_field.h:182
void set_max_display_width_from_create_field(const Create_field &create_field)
Set the maximum display width based on another Create_field.
Definition: create_field.h:84
Definition: field.h:568
@ NONE
Definition: field.h:704
geometry_type
Definition: field.h:711
Definition: item.h:776
Class for storing value or NULL value.
Definition: nullable.h:40
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:165
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:813
Used for storing information associated with generated column, default values generated from expressi...
Definition: field.h:459
bool get_field_stored() const
Definition: field.h:522
enum_hidden_type
Definition: column.h:95
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
bool is_field_for_functional_index(const Create_field *create_field)
Definition: create_field.cc:776
bool is_hidden_by_user(const Create_field *create_field)
Definition: create_field.cc:785
bool is_hidden_by_system(const Create_field *create_field)
Definition: create_field.cc:780
Dialog Client Authentication nullptr
Definition: dialog.cc:353
This file contains the field type.
enum_field_types
Column types for MySQL.
Definition: field_types.h:57
#define FIELD_FLAGS_COLUMN_FORMAT
Field column format, bit 24-25.
Definition: mysql_com.h:175
#define FIELD_FLAGS_STORAGE_MEDIA
Field storage media, bit 22-23.
Definition: mysql_com.h:173
constexpr const LEX_CSTRING EMPTY_CSTR
Definition: lex_string.h:46
A better implementation of the UNIX ctype(3) library.
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
This file includes constants used by all storage engines.
ha_storage_media
Definition: my_base.h:114
unsigned char uchar
Definition: my_inttypes.h:51
uint32_t uint32
Definition: my_inttypes.h:66
Definition: commit_order_queue.h:33
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
column_format_type
Definition: field.h:189
Definition: m_ctype.h:354
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:78
Definition: mysql_lex_string.h:39
Definition: typelib.h:34
unsigned int uint
Definition: uca-dump.cc:29