summaryrefslogtreecommitdiff
path: root/sql/datadict.h
blob: 9c1c151af1e1d9318672970dbc4f3af7b6c88caa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifndef DATADICT_INCLUDED
#define DATADICT_INCLUDED
/* Copyright (c) 2010, Oracle and/or its affiliates.
   Copyright (c) 2017 MariaDB corporation.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */

#include "handler.h"

/*
  Data dictionary API.
*/

enum Table_type
{
  TABLE_TYPE_UNKNOWN,
  TABLE_TYPE_NORMAL,                            /* Normal table */
  TABLE_TYPE_SEQUENCE,
  TABLE_TYPE_VIEW
};


#define INVISIBLE_MAX_BITS              3

/*
  Types of values in the MariaDB extra2 frm segment.
  Each value is written as
    type:       1 byte
    length:     1 byte  (1..255) or \0 and 2 bytes.
    binary value of the 'length' bytes.

  Older MariaDB servers can ignore values of unknown types if
  the type code is less than 128 (EXTRA2_ENGINE_IMPORTANT).
  Otherwise older (but newer than 10.0.1) servers are required
  to report an error.
*/
enum extra2_frm_value_type
{
  EXTRA2_TABLEDEF_VERSION=0,
  EXTRA2_DEFAULT_PART_ENGINE=1,
  EXTRA2_GIS=2,
  EXTRA2_APPLICATION_TIME_PERIOD=3,
  EXTRA2_PERIOD_FOR_SYSTEM_TIME=4,
  EXTRA2_INDEX_FLAGS=5,

#define EXTRA2_ENGINE_IMPORTANT 128

  EXTRA2_ENGINE_TABLEOPTS=128,
  EXTRA2_FIELD_FLAGS=129,
  EXTRA2_FIELD_DATA_TYPE_INFO=130,
  EXTRA2_PERIOD_WITHOUT_OVERLAPS=131,
};

enum extra2_field_flags
{
  VERS_OPTIMIZED_UPDATE= 1 << INVISIBLE_MAX_BITS,
};

enum extra2_index_flags
{
  EXTRA2_DEFAULT_INDEX_FLAGS,
  EXTRA2_IGNORED_KEY
};

#define FRM_HEADER_SIZE 64
#define FRM_FORMINFO_SIZE 288
#define FRM_MAX_SIZE (1024*1024)


inline size_t extra2_read_len(const uchar **pos, const uchar *end)
{
  size_t length= *(*pos)++;
  if (length)
    return length;

  if ((*pos) + 2 >= end)
    return 0;
  length= uint2korr(*pos);
  (*pos)+= 2;
  if (length < 256 || *pos + length > end)
    return 0;
  return length;
}

/*
  write the length as
  if (  0 < length <= 255)      one byte
  if (256 < length < ~65535)    zero byte, then two bytes, low-endian
*/
inline uchar *
extra2_write_len(uchar *pos, size_t len)
{
  DBUG_ASSERT(len);
  if (len <= 255)
    *pos++= (uchar)len;
  else
  {
    /*
      At the moment we support options_len up to 64K.
      We can easily extend it in the future, if the need arises.

      See build_frm_image():

        int2store(frm_header + 6, frm.length);

      frm.length includes FRM_HEADER_SIZE + extra2_size + 4
      and it must be 2 bytes, therefore extra2_size cannot be more than
      0xFFFF - FRM_HEADER_SIZE - 4.
    */
    DBUG_ASSERT(len <= 0xffff - FRM_HEADER_SIZE - 4);
    *pos++= 0;
    int2store(pos, len);
    pos+= 2;
  }
  return pos;
}

inline
uchar* extra2_write_str(uchar *pos, const LEX_CSTRING str)
{
  pos= extra2_write_len(pos, str.length);
  memcpy(pos, str.str, str.length);
  return pos + str.length;
}

inline uchar *
extra2_write(uchar *pos, enum extra2_frm_value_type type,
             const LEX_CSTRING &str)
{
  *pos++ = type;
  return extra2_write_str(pos, str);
}

inline uchar *
extra2_write(uchar *pos, enum extra2_frm_value_type type,
             const LEX_CUSTRING &str)
{
  return extra2_write(pos, type, *reinterpret_cast<const LEX_CSTRING*>(&str));
}

uchar *
extra2_write_field_properties(uchar *pos, List<Create_field> &create_fields);


struct Extra2_info
{
  LEX_CUSTRING version;
  LEX_CUSTRING options;
  Lex_ident engine;
  LEX_CUSTRING gis;
  LEX_CUSTRING field_flags;
  LEX_CUSTRING system_period;
  LEX_CUSTRING application_period;
  LEX_CUSTRING field_data_type_info;
  LEX_CUSTRING without_overlaps;
  LEX_CUSTRING index_flags;

  uint read_size;
  size_t write_size;

  Extra2_info()
  {
    bzero((void*)this, sizeof(*this));
  }

  template <class S>
  size_t store_size(const S &f) const
  {
    if (f.length == 0)
      return 0;
    DBUG_ASSERT(f.length <= 65535);
    // 1 byte is for type; 1 or 3 bytes for length
    return f.length + (f.length <= 255 ? 2 : 4);
  }
  size_t store_size() const
  {
    return
      store_size(version) +
      store_size(options) +
      store_size(engine) +
      store_size(gis) +
      store_size(field_flags) +
      store_size(system_period) +
      store_size(application_period) +
      store_size(field_data_type_info) +
      store_size(without_overlaps) +
      store_size(index_flags);
  }

  bool read_once(LEX_CUSTRING *section, const uchar *pos, size_t len)
  {
    if (section->str)
      return true;

    *section= { pos, len };
    return false;
  }

  bool read(const uchar* frm_image, size_t frm_size);
  uchar * write(uchar* frm_image, size_t frm_size);
};


/*
  Take extra care when using dd_frm_type() - it only checks the .frm file,
  and it won't work for any engine that supports discovery.

  Prefer to use ha_table_exists() instead.
  To check whether it's an frm of a view, use dd_frm_is_view().
*/

enum Table_type dd_frm_type(THD *thd, char *path, LEX_CSTRING *engine_name,
                            LEX_CSTRING *partition_engine_name,
                            LEX_CUSTRING *table_version);

static inline bool dd_frm_is_view(THD *thd, char *path)
{
  return dd_frm_type(thd, path, NULL, NULL, NULL) == TABLE_TYPE_VIEW;
}

bool dd_recreate_table(THD *thd, const char *db, const char *table_name);

#endif // DATADICT_INCLUDED