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