~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"
202.3.6 by Monty Taylor
First pass at gettexizing the error messages.
18
#include <drizzled/drizzled_error_messages.h>
1 by brian
clean slate
19
20
/*********************************************************************
21
 *                   table_def member definitions                    *
22
 *********************************************************************/
23
24
/*
25
  This function returns the field size in raw bytes based on the type
26
  and the encoded field data from the master's raw data.
27
*/
205 by Brian Aker
uint32 -> uin32_t
28
uint32_t table_def::calc_field_size(uint col, uchar *master_data) const
1 by brian
clean slate
29
{
205 by Brian Aker
uint32 -> uin32_t
30
  uint32_t length= 0;
1 by brian
clean slate
31
32
  switch (type(col)) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
33
  case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
34
    length= my_decimal_get_binary_size(m_field_metadata[col] >> 8, 
35
                                       m_field_metadata[col] & 0xff);
36
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
37
  case DRIZZLE_TYPE_DOUBLE:
1 by brian
clean slate
38
    length= m_field_metadata[col];
39
    break;
40
  /*
41
    The cases for SET and ENUM are include for completeness, however
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
42
    both are mapped to type DRIZZLE_TYPE_STRING and their real types
1 by brian
clean slate
43
    are encoded in the field metadata.
44
  */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
45
  case DRIZZLE_TYPE_SET:
46
  case DRIZZLE_TYPE_ENUM:
1 by brian
clean slate
47
  {
241 by Brian Aker
First pass of CHAR removal.
48
    length= m_field_metadata[col] & 0x00ff;
1 by brian
clean slate
49
    break;
50
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
51
  case DRIZZLE_TYPE_TINY:
1 by brian
clean slate
52
    length= 1;
53
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
54
  case DRIZZLE_TYPE_SHORT:
1 by brian
clean slate
55
    length= 2;
56
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
57
  case DRIZZLE_TYPE_LONG:
1 by brian
clean slate
58
    length= 4;
59
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
60
  case DRIZZLE_TYPE_LONGLONG:
1 by brian
clean slate
61
    length= 8;
62
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
63
  case DRIZZLE_TYPE_NULL:
1 by brian
clean slate
64
    length= 0;
65
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
66
  case DRIZZLE_TYPE_NEWDATE:
67
    length= 3;
68
    break;
69
  case DRIZZLE_TYPE_TIME:
70
    length= 3;
71
    break;
72
  case DRIZZLE_TYPE_TIMESTAMP:
1 by brian
clean slate
73
    length= 4;
74
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
75
  case DRIZZLE_TYPE_DATETIME:
1 by brian
clean slate
76
    length= 8;
77
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
78
  case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
79
  {
80
    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
81
    assert(uint2korr(master_data) > 0);
205 by Brian Aker
uint32 -> uin32_t
82
    length+= length == 1 ? (uint32_t) *master_data : uint2korr(master_data);
1 by brian
clean slate
83
    break;
84
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
85
  case DRIZZLE_TYPE_BLOB:
1 by brian
clean slate
86
  {
113 by Brian Aker
First pass on removing blob internals.
87
    length= uint4korr(master_data);
1 by brian
clean slate
88
    length+= m_field_metadata[col];
89
    break;
90
  }
91
  default:
205 by Brian Aker
uint32 -> uin32_t
92
    length= ~(uint32_t) 0;
1 by brian
clean slate
93
  }
94
  return length;
95
}
96
97
/*
98
  Is the definition compatible with a table?
99
100
*/
101
int
102
table_def::compatible_with(Relay_log_info const *rli_arg, TABLE *table)
103
  const
104
{
105
  /*
106
    We only check the initial columns for the tables.
107
  */
108
  uint const cols_to_check= min(table->s->fields, size());
109
  int error= 0;
110
  Relay_log_info const *rli= const_cast<Relay_log_info*>(rli_arg);
111
112
  TABLE_SHARE const *const tsh= table->s;
113
114
  for (uint col= 0 ; col < cols_to_check ; ++col)
115
  {
116
    if (table->field[col]->type() != type(col))
117
    {
51.1.44 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
118
      assert(col < size() && col < tsh->fields);
119
      assert(tsh->db.str && tsh->table_name.str);
1 by brian
clean slate
120
      error= 1;
121
      char buf[256];
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
122
      snprintf(buf, sizeof(buf), "Column %d type mismatch - "
123
                "received type %d, %s.%s has type %d",
124
                col, type(col), tsh->db.str, tsh->table_name.str,
125
                table->field[col]->type());
1 by brian
clean slate
126
      rli->report(ERROR_LEVEL, ER_BINLOG_ROW_WRONG_TABLE_DEF,
127
                  ER(ER_BINLOG_ROW_WRONG_TABLE_DEF), buf);
128
    }
129
    /*
130
      Check the slave's field size against that of the master.
131
    */
132
    if (!error && 
133
        !table->field[col]->compatible_field_size(field_metadata(col)))
134
    {
135
      error= 1;
136
      char buf[256];
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
137
      snprintf(buf, sizeof(buf), "Column %d size mismatch - "
138
               "master has size %d, %s.%s on slave has size %d."
139
               " Master's column size should be <= the slave's "
140
               "column size.", col,
141
               table->field[col]->pack_length_from_metadata(
142
                                    m_field_metadata[col]),
143
               tsh->db.str, tsh->table_name.str, 
144
               table->field[col]->row_pack_length());
1 by brian
clean slate
145
      rli->report(ERROR_LEVEL, ER_BINLOG_ROW_WRONG_TABLE_DEF,
146
                  ER(ER_BINLOG_ROW_WRONG_TABLE_DEF), buf);
147
    }
148
  }
149
150
  return error;
151
}