~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2006 MySQL AB
2
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.
6
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.
11
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 */
15
16
#include "rpl_utility.h"
17
#include "rpl_rli.h"
18
19
/*********************************************************************
20
 *                   table_def member definitions                    *
21
 *********************************************************************/
22
23
/*
24
  This function returns the field size in raw bytes based on the type
25
  and the encoded field data from the master's raw data.
26
*/
27
uint32 table_def::calc_field_size(uint col, uchar *master_data) const
28
{
29
  uint32 length= 0;
30
31
  switch (type(col)) {
32
  case MYSQL_TYPE_NEWDECIMAL:
33
    length= my_decimal_get_binary_size(m_field_metadata[col] >> 8, 
34
                                       m_field_metadata[col] & 0xff);
35
    break;
36
  case MYSQL_TYPE_DOUBLE:
37
    length= m_field_metadata[col];
38
    break;
39
  /*
40
    The cases for SET and ENUM are include for completeness, however
41
    both are mapped to type MYSQL_TYPE_STRING and their real types
42
    are encoded in the field metadata.
43
  */
44
  case MYSQL_TYPE_SET:
45
  case MYSQL_TYPE_ENUM:
46
  case MYSQL_TYPE_STRING:
47
  {
48
    uchar type= m_field_metadata[col] >> 8U;
49
    if ((type == MYSQL_TYPE_SET) || (type == MYSQL_TYPE_ENUM))
50
      length= m_field_metadata[col] & 0x00ff;
51
    else
52
    {
53
      /*
54
        We are reading the actual size from the master_data record
55
        because this field has the actual lengh stored in the first
56
        byte.
57
      */
58
      length= (uint) *master_data + 1;
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
59
      assert(length != 0);
1 by brian
clean slate
60
    }
61
    break;
62
  }
63
  case MYSQL_TYPE_YEAR:
64
  case MYSQL_TYPE_TINY:
65
    length= 1;
66
    break;
67
  case MYSQL_TYPE_SHORT:
68
    length= 2;
69
    break;
70
  case MYSQL_TYPE_LONG:
71
    length= 4;
72
    break;
73
  case MYSQL_TYPE_LONGLONG:
74
    length= 8;
75
    break;
76
  case MYSQL_TYPE_NULL:
77
    length= 0;
78
    break;
79
  case MYSQL_TYPE_NEWDATE:
80
    length= 3;
81
    break;
82
  case MYSQL_TYPE_TIME:
83
    length= 3;
84
    break;
85
  case MYSQL_TYPE_TIMESTAMP:
86
    length= 4;
87
    break;
88
  case MYSQL_TYPE_DATETIME:
89
    length= 8;
90
    break;
91
  case MYSQL_TYPE_VARCHAR:
92
  {
93
    length= m_field_metadata[col] > 255 ? 2 : 1; // c&p of Field_varstring::data_length()
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
94
    assert(uint2korr(master_data) > 0);
1 by brian
clean slate
95
    length+= length == 1 ? (uint32) *master_data : uint2korr(master_data);
96
    break;
97
  }
98
  case MYSQL_TYPE_BLOB:
99
  {
113 by Brian Aker
First pass on removing blob internals.
100
    length= uint4korr(master_data);
1 by brian
clean slate
101
    length+= m_field_metadata[col];
102
    break;
103
  }
104
  default:
105
    length= ~(uint32) 0;
106
  }
107
  return length;
108
}
109
110
/*
111
  Is the definition compatible with a table?
112
113
*/
114
int
115
table_def::compatible_with(Relay_log_info const *rli_arg, TABLE *table)
116
  const
117
{
118
  /*
119
    We only check the initial columns for the tables.
120
  */
121
  uint const cols_to_check= min(table->s->fields, size());
122
  int error= 0;
123
  Relay_log_info const *rli= const_cast<Relay_log_info*>(rli_arg);
124
125
  TABLE_SHARE const *const tsh= table->s;
126
127
  for (uint col= 0 ; col < cols_to_check ; ++col)
128
  {
129
    if (table->field[col]->type() != type(col))
130
    {
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
131
      assert(col < size() && col < tsh->fields);
132
      assert(tsh->db.str && tsh->table_name.str);
1 by brian
clean slate
133
      error= 1;
134
      char buf[256];
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
135
      snprintf(buf, sizeof(buf), "Column %d type mismatch - "
136
                "received type %d, %s.%s has type %d",
137
                col, type(col), tsh->db.str, tsh->table_name.str,
138
                table->field[col]->type());
1 by brian
clean slate
139
      rli->report(ERROR_LEVEL, ER_BINLOG_ROW_WRONG_TABLE_DEF,
140
                  ER(ER_BINLOG_ROW_WRONG_TABLE_DEF), buf);
141
    }
142
    /*
143
      Check the slave's field size against that of the master.
144
    */
145
    if (!error && 
146
        !table->field[col]->compatible_field_size(field_metadata(col)))
147
    {
148
      error= 1;
149
      char buf[256];
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
150
      snprintf(buf, sizeof(buf), "Column %d size mismatch - "
151
               "master has size %d, %s.%s on slave has size %d."
152
               " Master's column size should be <= the slave's "
153
               "column size.", col,
154
               table->field[col]->pack_length_from_metadata(
155
                                    m_field_metadata[col]),
156
               tsh->db.str, tsh->table_name.str, 
157
               table->field[col]->row_pack_length());
1 by brian
clean slate
158
      rli->report(ERROR_LEVEL, ER_BINLOG_ROW_WRONG_TABLE_DEF,
159
                  ER(ER_BINLOG_ROW_WRONG_TABLE_DEF), buf);
160
    }
161
  }
162
163
  return error;
164
}