1
/* Copyright (C) 2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
20
#error "Don't include this C++ header file from a non-C++ file!"
23
#include "mysql_priv.h"
29
A table definition from the master.
31
The responsibilities of this class is:
32
- Extract and decode table definition data from the table map event
33
- Check if table definition in table map is compatible with table
36
Currently, the only field type data available is an array of the
37
type operators that are present in the table map event.
39
@todo Add type operands to this structure to allow detection of
40
difference between, e.g., BIT(5) and BIT(10).
47
Convenience declaration of the type of the field type data in a
50
typedef unsigned char field_type;
55
@param types Array of types
56
@param size Number of elements in array 'types'
57
@param field_metadata Array of extra information about fields
58
@param metadata_size Size of the field_metadata array
59
@param null_bitmap The bitmap of fields that can be null
61
table_def(field_type *types, ulong size, uchar *field_metadata,
62
int metadata_size, uchar *null_bitmap)
63
: m_size(size), m_type(0), m_field_metadata_size(metadata_size),
64
m_field_metadata(0), m_null_bits(0), m_memory(NULL)
66
m_memory= (uchar *)my_multi_malloc(MYF(MY_WME),
69
size * sizeof(uint16),
70
&m_null_bits, (size + 7) / 8,
73
bzero(m_field_metadata, size * sizeof(uint16));
76
memcpy(m_type, types, size);
80
Extract the data from the table map into the field metadata array
81
iff there is field metadata. The variable metadata_size will be
82
0 if we are replicating from an older version server since no field
83
metadata was written to the table map. This can also happen if
84
there were no fields in the master that needed extra metadata.
86
if (m_size && metadata_size)
89
for (unsigned int i= 0; i < m_size; i++)
92
case MYSQL_TYPE_TINY_BLOB:
94
case MYSQL_TYPE_MEDIUM_BLOB:
95
case MYSQL_TYPE_LONG_BLOB:
96
case MYSQL_TYPE_DOUBLE:
97
case MYSQL_TYPE_FLOAT:
100
These types store a single byte.
102
m_field_metadata[i]= field_metadata[index];
107
case MYSQL_TYPE_ENUM:
108
case MYSQL_TYPE_STRING:
110
uint16 x= field_metadata[index++] << 8U; // real_type
111
x+= field_metadata[index++]; // pack or field length
112
m_field_metadata[i]= x;
115
case MYSQL_TYPE_VARCHAR:
118
These types store two bytes.
120
char *ptr= (char *)&field_metadata[index];
121
m_field_metadata[i]= uint2korr(ptr);
125
case MYSQL_TYPE_NEWDECIMAL:
127
uint16 x= field_metadata[index++] << 8U; // precision
128
x+= field_metadata[index++]; // decimals
129
m_field_metadata[i]= x;
133
m_field_metadata[i]= 0;
138
if (m_size && null_bitmap)
139
memcpy(m_null_bits, null_bitmap, (m_size + 7) / 8);
143
my_free(m_memory, MYF(0));
151
Return the number of fields there is type data for.
153
@return The number of fields that there is type data for.
155
ulong size() const { return m_size; }
159
Return a representation of the type data for one field.
161
@param index Field index to return data for
163
@return Will return a representation of the type data for field
164
<code>index</code>. Currently, only the type identifier is
167
field_type type(ulong index) const
169
DBUG_ASSERT(index < m_size);
170
return m_type[index];
175
This function allows callers to get the extra field data from the
176
table map for a given field. If there is no metadata for that field
177
or there is no extra metadata at all, the function returns 0.
179
The function returns the value for the field metadata for column at
180
position indicated by index. As mentioned, if the field was a type
181
that stores field metadata, that value is returned else zero (0) is
182
returned. This method is used in the unpack() methods of the
183
corresponding fields to properly extract the data from the binary log
184
in the event that the master's field is smaller than the slave.
186
uint16 field_metadata(uint index) const
188
DBUG_ASSERT(index < m_size);
189
if (m_field_metadata_size)
190
return m_field_metadata[index];
196
This function returns whether the field on the master can be null.
197
This value is derived from field->maybe_null().
199
my_bool maybe_null(uint index) const
201
DBUG_ASSERT(index < m_size);
202
return ((m_null_bits[(index / 8)] &
203
(1 << (index % 8))) == (1 << (index %8)));
207
This function returns the field size in raw bytes based on the type
208
and the encoded field data from the master's raw data. This method can
209
be used for situations where the slave needs to skip a column (e.g.,
210
WL#3915) or needs to advance the pointer for the fields in the raw
211
data from the master to a specific column.
213
uint32 calc_field_size(uint col, uchar *master_data) const;
216
Decide if the table definition is compatible with a table.
218
Compare the definition with a table to see if it is compatible
221
A table definition is compatible with a table if:
222
- the columns types of the table definition is a (not
223
necessarily proper) prefix of the column type of the table, or
224
- the other way around
226
@param rli Pointer to relay log info
227
@param table Pointer to table to compare with.
229
@retval 1 if the table definition is not compatible with @c table
230
@retval 0 if the table definition is compatible with @c table
232
int compatible_with(Relay_log_info const *rli, TABLE *table) const;
235
ulong m_size; // Number of elements in the types array
236
field_type *m_type; // Array of type descriptors
237
uint m_field_metadata_size;
238
uint16 *m_field_metadata;
244
Extend the normal table list with a few new fields needed by the
245
slave thread, but nowhere else.
247
struct RPL_TABLE_LIST
250
bool m_tabledef_valid;
251
table_def m_tabledef;
255
/* Anonymous namespace for template functions/classes */
259
Smart pointer that will automatically call my_afree (a macro) when
260
the pointer goes out of scope. This is used so that I do not have
261
to remember to call my_afree() before each return. There is no
262
overhead associated with this, since all functions are inline.
264
I (Matz) would prefer to use the free function as a template
265
parameter, but that is not possible when the "function" is a
273
auto_afree_ptr(Obj* ptr) : m_ptr(ptr) { }
274
~auto_afree_ptr() { if (m_ptr) my_afree(m_ptr); }
275
void assign(Obj* ptr) {
276
/* Only to be called if it hasn't been given a value before. */
277
DBUG_ASSERT(m_ptr == NULL);
280
Obj* get() { return m_ptr; }
286
One test in mysqldump.test has 330 columns!
287
So have a sufficient buffer and check its limit anyway.
289
#define DBUG_PRINT_BITSET(N,FRM,BS) \
291
char buf[MAX_FIELDS+1]; \
293
for (i = 0 ; i < (BS)->n_bits && i < MAX_FIELDS ; ++i) \
294
buf[i] = bitmap_is_set((BS), i) ? '1' : '0'; \
296
DBUG_PRINT((N), ((FRM), buf)); \
299
#endif /* RPL_UTILITY_H */