MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
table_setup_objects.cc
Go to the documentation of this file.
1 /* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
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 Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
21 #include "my_global.h"
22 #include "my_pthread.h"
23 #include "pfs_instr.h"
24 #include "pfs_column_types.h"
25 #include "pfs_column_values.h"
26 #include "pfs_setup_object.h"
27 #include "table_setup_objects.h"
28 #include "table_helper.h"
29 #include "pfs_global.h"
30 
31 THR_LOCK table_setup_objects::m_table_lock;
32 
33 static const TABLE_FIELD_TYPE field_types[]=
34 {
35  {
36  { C_STRING_WITH_LEN("OBJECT_TYPE") },
37  { C_STRING_WITH_LEN("enum(\'TABLE\')") },
38  { NULL, 0}
39  },
40  {
41  { C_STRING_WITH_LEN("OBJECT_SCHEMA") },
42  { C_STRING_WITH_LEN("varchar(64)") },
43  { NULL, 0}
44  },
45  {
46  { C_STRING_WITH_LEN("OBJECT_NAME") },
47  { C_STRING_WITH_LEN("varchar(64)") },
48  { NULL, 0}
49  },
50  {
51  { C_STRING_WITH_LEN("ENABLED") },
52  { C_STRING_WITH_LEN("enum(\'YES\',\'NO\')") },
53  { NULL, 0}
54  },
55  {
56  { C_STRING_WITH_LEN("TIMED") },
57  { C_STRING_WITH_LEN("enum(\'YES\',\'NO\')") },
58  { NULL, 0}
59  }
60 };
61 
63 table_setup_objects::m_field_def=
64 { 5, field_types };
65 
68 {
69  { C_STRING_WITH_LEN("setup_objects") },
72  table_setup_objects::write_row,
73  table_setup_objects::delete_all_rows,
74  table_setup_objects::get_row_count,
75  1000, /* records */
76  sizeof(PFS_simple_index),
77  &m_table_lock,
78  &m_field_def,
79  false /* checked */
80 };
81 
82 int update_derived_flags()
83 {
84  PFS_thread *thread= PFS_thread::get_current_thread();
85  if (unlikely(thread == NULL))
86  return HA_ERR_OUT_OF_MEM;
87 
90  return 0;
91 }
92 
94 {
95  return new table_setup_objects();
96 }
97 
98 int table_setup_objects::write_row(TABLE *table, unsigned char *buf,
99  Field **fields)
100 {
101  int result;
102  Field *f;
103  enum_object_type object_type= OBJECT_TYPE_TABLE;
104  String object_schema_data("%", 1, &my_charset_utf8_bin);
105  String object_name_data("%", 1, &my_charset_utf8_bin);
106  String *object_schema= &object_schema_data;
107  String *object_name= &object_name_data;
108  enum_yes_no enabled_value= ENUM_YES;
109  enum_yes_no timed_value= ENUM_YES;
110  bool enabled= true;
111  bool timed= true;
112 
113  for (; (f= *fields) ; fields++)
114  {
115  if (bitmap_is_set(table->write_set, f->field_index))
116  {
117  switch(f->field_index)
118  {
119  case 0: /* OBJECT_TYPE */
120  object_type= (enum_object_type) get_field_enum(f);
121  break;
122  case 1: /* OBJECT_SCHEMA */
123  object_schema= get_field_varchar_utf8(f, &object_schema_data);
124  break;
125  case 2: /* OBJECT_NAME */
126  object_name= get_field_varchar_utf8(f, &object_name_data);
127  break;
128  case 3: /* ENABLED */
129  enabled_value= (enum_yes_no) get_field_enum(f);
130  break;
131  case 4: /* TIMED */
132  timed_value= (enum_yes_no) get_field_enum(f);
133  break;
134  default:
135  DBUG_ASSERT(false);
136  }
137  }
138  }
139 
140  /* Reject illegal enum values in OBJECT_TYPE */
141  if (object_type != OBJECT_TYPE_TABLE)
142  return HA_ERR_NO_REFERENCED_ROW;
143 
144  /* Reject illegal enum values in ENABLED */
145  if ((enabled_value != ENUM_YES) && (enabled_value != ENUM_NO))
146  return HA_ERR_NO_REFERENCED_ROW;
147 
148  /* Reject illegal enum values in TIMED */
149  if ((timed_value != ENUM_YES) && (timed_value != ENUM_NO))
150  return HA_ERR_NO_REFERENCED_ROW;
151 
152  enabled= (enabled_value == ENUM_YES) ? true : false;
153  timed= (timed_value == ENUM_YES) ? true : false;
154 
155  result= insert_setup_object(object_type, object_schema, object_name,
156  enabled, timed);
157  if (result == 0)
158  result= update_derived_flags();
159  return result;
160 }
161 
162 int table_setup_objects::delete_all_rows(void)
163 {
164  int result= reset_setup_object();
165  if (result == 0)
166  result= update_derived_flags();
167  return result;
168 }
169 
170 ha_rows table_setup_objects::get_row_count(void)
171 {
172  return setup_object_count();
173 }
174 
175 table_setup_objects::table_setup_objects()
176  : PFS_engine_table(&m_share, &m_pos),
177  m_row_exists(false), m_pos(0), m_next_pos(0)
178 {}
179 
181 {
182  m_pos.m_index= 0;
183  m_next_pos.m_index= 0;
184 }
185 
187 {
188  PFS_setup_object *pfs;
189 
190  for (m_pos.set_at(&m_next_pos);
191  m_pos.m_index < setup_object_max;
192  m_pos.next())
193  {
194  pfs= &setup_object_array[m_pos.m_index];
195  if (pfs->m_lock.is_populated())
196  {
197  make_row(pfs);
198  m_next_pos.set_after(&m_pos);
199  return 0;
200  }
201  }
202 
203  return HA_ERR_END_OF_FILE;
204 }
205 
206 int table_setup_objects::rnd_pos(const void *pos)
207 {
208  PFS_setup_object *pfs;
209 
210  set_position(pos);
211 
212  DBUG_ASSERT(m_pos.m_index < setup_object_max);
213  pfs= &setup_object_array[m_pos.m_index];
214  if (pfs->m_lock.is_populated())
215  {
216  make_row(pfs);
217  return 0;
218  }
219 
220  return HA_ERR_RECORD_DELETED;
221 }
222 
223 void table_setup_objects::make_row(PFS_setup_object *pfs)
224 {
225  pfs_lock lock;
226 
227  m_row_exists= false;
228 
229  pfs->m_lock.begin_optimistic_lock(&lock);
230 
231  m_row.m_object_type= pfs->get_object_type();
232  memcpy(m_row.m_schema_name, pfs->m_schema_name, pfs->m_schema_name_length);
234  memcpy(m_row.m_object_name, pfs->m_object_name, pfs->m_object_name_length);
236  m_row.m_enabled_ptr= &pfs->m_enabled;
237  m_row.m_timed_ptr= &pfs->m_timed;
238 
239  if (pfs->m_lock.end_optimistic_lock(&lock))
240  m_row_exists= true;
241 }
242 
244  unsigned char *buf,
245  Field **fields,
246  bool read_all)
247 {
248  Field *f;
249 
250  if (unlikely(! m_row_exists))
251  return HA_ERR_RECORD_DELETED;
252 
253  /* Set the null bits */
254  DBUG_ASSERT(table->s->null_bytes == 1);
255  buf[0]= 0;
256 
257  for (; (f= *fields) ; fields++)
258  {
259  if (read_all || bitmap_is_set(table->read_set, f->field_index))
260  {
261  switch(f->field_index)
262  {
263  case 0: /* OBJECT_TYPE */
264  set_field_enum(f, m_row.m_object_type);
265  break;
266  case 1: /* OBJECT_SCHEMA */
267  if (m_row.m_schema_name_length)
269  m_row.m_schema_name_length);
270  else
271  f->set_null();
272  break;
273  case 2: /* OBJECT_NAME */
274  if (m_row.m_object_name_length)
276  m_row.m_object_name_length);
277  else
278  f->set_null();
279  break;
280  case 3: /* ENABLED */
281  set_field_enum(f, (*m_row.m_enabled_ptr) ? ENUM_YES : ENUM_NO);
282  break;
283  case 4: /* TIMED */
284  set_field_enum(f, (*m_row.m_timed_ptr) ? ENUM_YES : ENUM_NO);
285  break;
286  default:
287  DBUG_ASSERT(false);
288  }
289  }
290  }
291 
292  return 0;
293 }
294 
296  const unsigned char *,
297  unsigned char *,
298  Field **fields)
299 {
300  int result;
301  Field *f;
302  enum_yes_no value;
303 
304  for (; (f= *fields) ; fields++)
305  {
306  if (bitmap_is_set(table->write_set, f->field_index))
307  {
308  switch(f->field_index)
309  {
310  case 0: /* OBJECT_TYPE */
311  case 1: /* OBJECT_SCHEMA */
312  case 2: /* OBJECT_NAME */
313  return HA_ERR_WRONG_COMMAND;
314  case 3: /* ENABLED */
315  value= (enum_yes_no) get_field_enum(f);
316  /* Reject illegal enum values in ENABLED */
317  if ((value != ENUM_YES) && (value != ENUM_NO))
318  return HA_ERR_NO_REFERENCED_ROW;
319  *m_row.m_enabled_ptr= (value == ENUM_YES) ? true : false;
320  break;
321  case 4: /* TIMED */
322  value= (enum_yes_no) get_field_enum(f);
323  /* Reject illegal enum values in TIMED */
324  if ((value != ENUM_YES) && (value != ENUM_NO))
325  return HA_ERR_NO_REFERENCED_ROW;
326  *m_row.m_timed_ptr= (value == ENUM_YES) ? true : false;
327  break;
328  default:
329  DBUG_ASSERT(false);
330  }
331  }
332  }
333 
334  result= update_derived_flags();
335  return result;
336 }
337 
339  const unsigned char *buf,
340  Field **fields)
341 {
342  DBUG_ASSERT(m_row_exists);
343 
344  CHARSET_INFO *cs= &my_charset_utf8_bin;
345  enum_object_type object_type= OBJECT_TYPE_TABLE;
346  String object_schema(m_row.m_schema_name, m_row.m_schema_name_length, cs);
347  String object_name(m_row.m_object_name, m_row.m_object_name_length, cs);
348 
349  int result= delete_setup_object(object_type, &object_schema, &object_name);
350 
351  if (result == 0)
352  result= update_derived_flags();
353  return result;
354 }
355