MySQL  8.0.19
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, 2019, 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.
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 
182  : after(NULL),
184  geom_type(Field::GEOM_GEOMETRY),
185  maybe_null(false),
188  /*
189  Initialize treat_bit_as_char for all field types even if
190  it is only used for MYSQL_TYPE_BIT. This avoids bogus
191  valgrind warnings in optimized builds.
192  */
196  m_default_val_expr(nullptr) {}
197  Create_field(Field *field, Field *orig_field);
198 
199  /* Used to make a clone of this object for ALTER/CREATE TABLE */
201  return new (mem_root) Create_field(*this);
202  }
203  bool is_gcol() const { return gcol_info; }
204  bool is_virtual_gcol() const {
205  return gcol_info && !gcol_info->get_field_stored();
206  }
207 
208  /* Init for a tmp table field. To be extended if need be. */
209  void init_for_tmp_table(enum_field_types sql_type_arg, uint32 max_length,
212  const char *field_name = "");
213 
214  bool init(THD *thd, const char *field_name, enum_field_types type,
215  const char *length, const char *decimals, uint type_modifier,
216  Item *default_value, Item *on_update_value, LEX_CSTRING *comment,
217  const char *change, List<String> *interval_list,
218  const CHARSET_INFO *cs, bool has_explicit_collation,
219  uint uint_geom_type, Value_generator *gcol_info,
220  Value_generator *default_val_expr, Nullable<gis::srid_t> srid,
222 
225  }
226 
229  }
230 
231  private:
232  /// The maximum display width of this column.
233  ///
234  /// The "display width" is the number of code points that is needed to print
235  /// out the string represenation of a value. It can be given by the user
236  /// both explicitly and implicitly. If a user creates a table with the columns
237  /// "a VARCHAR(3), b INT(3)", both columns are given an explicit display width
238  /// of 3 code points. But if a user creates a table with the columns
239  /// "a INT, b TINYINT UNSIGNED", the first column has an implicit display
240  /// width of 11 (-2147483648 is the longest value for a signed int) and the
241  /// second column has an implicit display width of 3 (255 is the longest value
242  /// for an unsigned tinyint).
243  /// This is related to storage size for some types (VARCHAR, BLOB etc), but
244  /// not for all types (an INT is four bytes regardless of the display width).
245  ///
246  /// A "code point" is bascially a numeric value. For instance, ASCII
247  /// compromises of 128 code points (0x00 to 0x7F), while unicode contains way
248  /// more. In most cases a code point represents a single graphical unit (aka
249  /// grapheme), but not always. For instance, É may consists of two code points
250  /// where one is the letter E and the other one is the quotation mark above
251  /// the letter.
253 
254  /// Whether or not the display width was given explicitly by the user.
256 
257  /// The maximum number of bytes a TINYBLOB can hold.
258  static constexpr size_t TINYBLOB_MAX_SIZE_IN_BYTES{255};
259 
260  /// The maximum number of bytes a BLOB can hold.
261  static constexpr size_t BLOB_MAX_SIZE_IN_BYTES{65535};
262 
263  /// The maximum number of bytes a MEDIUMBLOB can hold.
264  static constexpr size_t MEDIUMBLOB_MAX_SIZE_IN_BYTES{16777215};
265 
266  /// The maximum number of bytes a LONGBLOB can hold.
267  static constexpr size_t LONGBLOB_MAX_SIZE_IN_BYTES{4294967295};
268 };
269 
270 /// @returns whether or not this field is a hidden column that represents a
271 /// functional index.
272 bool is_field_for_functional_index(const Create_field *create_field);
273 
274 #endif
Create_field::pack_length
size_t pack_length(bool dont_override=false) const
Definition: create_field.cc:715
Field::NONE
@ NONE
Definition: field.h:816
MYSQL_LEX_CSTRING
Definition: mysql_lex_string.h:39
Item
Definition: item.h:665
Create_field::TINYBLOB_MAX_SIZE_IN_BYTES
static constexpr size_t TINYBLOB_MAX_SIZE_IN_BYTES
The maximum number of bytes a TINYBLOB can hold.
Definition: create_field.h:258
THD
Definition: sql_class.h:764
Create_field::maybe_null
bool maybe_null
Indicate whether column is nullable, zerofill or unsigned.
Definition: create_field.h:136
field_types.h
This file contains the field type.
Create_field::comment
LEX_CSTRING comment
Definition: create_field.h:99
my_base.h
NULL
#define NULL
Definition: types.h:55
Value_generator::get_field_stored
bool get_field_stored() const
Definition: field.h:649
Create_field::gcol_info
Value_generator * gcol_info
Definition: create_field.h:166
Create_field::hidden
dd::Column::enum_hidden_type hidden
Definition: create_field.h:90
Create_field::BLOB_MAX_SIZE_IN_BYTES
static constexpr size_t BLOB_MAX_SIZE_IN_BYTES
The maximum number of bytes a BLOB can hold.
Definition: create_field.h:261
CHARSET_INFO
Definition: m_ctype.h:354
Create_field::init
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, 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:188
Create_field::m_explicit_display_width
bool m_explicit_display_width
Whether or not the display width was given explicitly by the user.
Definition: create_field.h:255
Create_field::decimals
uint decimals
Definition: create_field.h:111
Create_field
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:49
Create_field::init_for_tmp_table
void init_for_tmp_table(enum_field_types sql_type_arg, uint32 max_length, uint32 decimals, bool maybe_null, bool is_unsigned, uint pack_length_override, const char *field_name="")
Init for a tmp table field.
Definition: create_field.cc:572
Create_field::is_explicit_collation
bool is_explicit_collation
Definition: create_field.h:124
String
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:164
mem_root
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
Create_field::m_default_val_expr
Value_generator * m_default_val_expr
Holds the expression to be used to generate default values.
Definition: create_field.h:175
Create_field::interval
TYPELIB * interval
Definition: create_field.h:120
Create_field::is_unsigned
bool is_unsigned
Definition: create_field.h:138
my_alloc.h
Create_field::flags
uint flags
Definition: create_field.h:112
Mysql::Nullable
Class for storing value or NULL value.
Definition: nullable.h:40
Create_field::Create_field
Create_field()
Definition: create_field.h:181
field.h
is_field_for_functional_index
bool is_field_for_functional_index(const Create_field *create_field)
Definition: create_field.cc:780
m_ctype.h
column_format_type
column_format_type
Definition: field.h:186
Create_field::is_zerofill
bool is_zerofill
Definition: create_field.h:137
Create_field::interval_list
List< String > interval_list
Definition: create_field.h:122
MEM_ROOT
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
Create_field::treat_bit_as_char
bool treat_bit_as_char
Indicates that storage engine doesn't support optimized BIT field storage.
Definition: create_field.h:150
lex_string.h
Create_field::key_length
size_t key_length() const
Definition: create_field.cc:743
Create_field::pack_length_override
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
enum_field_types
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
dd::Column::enum_hidden_type
enum_hidden_type
Definition: column.h:94
uint
unsigned int uint
Definition: uca-dump.cc:29
Create_field::charset
const CHARSET_INFO * charset
Definition: create_field.h:123
Create_field::explicit_display_width
bool explicit_display_width() const
Definition: create_field.h:81
column.h
Create_field::MEDIUMBLOB_MAX_SIZE_IN_BYTES
static constexpr size_t MEDIUMBLOB_MAX_SIZE_IN_BYTES
The maximum number of bytes a MEDIUMBLOB can hold.
Definition: create_field.h:264
Create_field::set_max_display_width_from_create_field
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
Field::geometry_type
geometry_type
Definition: field.h:823
Value_generator
Used for storing information associated with generated column, default values generated from expressi...
Definition: field.h:586
Create_field::is_array
bool is_array
Definition: create_field.h:179
uchar
unsigned char uchar
Definition: my_inttypes.h:51
Create_field::sql_type
enum_field_types sql_type
Definition: create_field.h:110
TYPELIB
Definition: typelib.h:34
nullable.h
uint32
uint32_t uint32
Definition: my_inttypes.h:66
Create_field::offset
uint offset
Definition: create_field.h:128
ha_storage_media
ha_storage_media
Definition: my_base.h:114
Create_field::constant_default
Item * constant_default
The declared default value, if any, otherwise NULL.
Definition: create_field.h:109
sql_list.h
Create_field::max_display_width_in_bytes
size_t max_display_width_in_bytes() const
Definition: create_field.cc:675
srid.h
Create_field::m_srid
Nullable< gis::srid_t > m_srid
Definition: create_field.h:176
Create_field::max_display_width_in_codepoints
size_t max_display_width_in_codepoints() const
Definition: create_field.cc:628
Create_field::change
const char * change
Name of column modified by ALTER TABLE's CHANGE/MODIFY COLUMN clauses, NULL for columns added.
Definition: create_field.h:97
Create_field::clone
Create_field * clone(MEM_ROOT *mem_root) const
Definition: create_field.h:200
HttpMethod::type
int type
Definition: http_common.h:411
FIELD_FLAGS_STORAGE_MEDIA
#define FIELD_FLAGS_STORAGE_MEDIA
Field storage media, bit 22-23.
Definition: mysql_com.h:173
Create_field::auto_flags
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
gis::length
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
Create_field::is_gcol
bool is_gcol() const
Definition: create_field.h:203
FIELD_FLAGS_COLUMN_FORMAT
#define FIELD_FLAGS_COLUMN_FORMAT
Field column format, bit 24-25.
Definition: mysql_com.h:175
Create_field::field_name
const char * field_name
Definition: create_field.h:92
Create_field::column_format
column_format_type column_format() const
Definition: create_field.h:227
Create_field::after
const char * after
Definition: create_field.h:98
Create_field::geom_type
Field::geometry_type geom_type
Definition: create_field.h:125
List< String >
Create_field::m_max_display_width_in_codepoints
size_t m_max_display_width_in_codepoints
The maximum display width of this column.
Definition: create_field.h:252
Field
Definition: field.h:695
Create_field::is_virtual_gcol
bool is_virtual_gcol() const
Definition: create_field.h:204
Create_field::field
Field * field
Definition: create_field.h:126
typelib.h
Create_field::LONGBLOB_MAX_SIZE_IN_BYTES
static constexpr size_t LONGBLOB_MAX_SIZE_IN_BYTES
The maximum number of bytes a LONGBLOB can hold.
Definition: create_field.h:267
Create_field::field_storage_type
ha_storage_media field_storage_type() const
Definition: create_field.h:223
Create_field::stored_in_db
bool stored_in_db
Definition: create_field.h:172
false
#define false
Definition: config_static.h:43