MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fts0sql.cc
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2007, 2012, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation; version 2 of the License.
8 
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16 
17 *****************************************************************************/
18 
19 /**************************************************/
26 #include "que0que.h"
27 #include "trx0roll.h"
28 #include "pars0pars.h"
29 #include "dict0dict.h"
30 #include "fts0types.h"
31 #include "fts0priv.h"
32 
33 #ifndef UNIV_NONINL
34 #include "fts0types.ic"
35 #include "fts0vlc.ic"
36 #endif
37 
42 static const char* fts_sql_begin=
43  "PROCEDURE P() IS\n";
44 
46 static const char* fts_sql_end=
47  "\n"
48  "END;\n";
49 
50 /******************************************************************/
53 UNIV_INTERN
54 int
56 /*=============*/
57  const fts_table_t*
58  fts_table,
59  char* table_id)
62 {
63  int len;
64 
65  switch (fts_table->type) {
66  case FTS_COMMON_TABLE:
67  len = fts_write_object_id(fts_table->table_id, table_id);
68  break;
69 
70  case FTS_INDEX_TABLE:
71 
72  len = fts_write_object_id(fts_table->table_id, table_id);
73 
74  table_id[len] = '_';
75  ++len;
76  table_id += len;
77 
78  len += fts_write_object_id(fts_table->index_id, table_id);
79  break;
80 
81  default:
82  ut_error;
83  }
84 
85  ut_a(len >= 16);
87 
88  return(len);
89 }
90 
91 /******************************************************************/
94 UNIV_INTERN
95 char*
97 /*======================*/
98  const fts_table_t*
99  fts_table)
100 {
101  int len;
102  const char* slash;
103  char* prefix_name;
104  int dbname_len = 0;
105  int prefix_name_len;
106  char table_id[FTS_AUX_MIN_TABLE_ID_LENGTH];
107 
108  slash = static_cast<const char*>(
109  memchr(fts_table->parent, '/', strlen(fts_table->parent)));
110 
111  if (slash) {
112  /* Print up to and including the separator. */
113  dbname_len = (slash - fts_table->parent) + 1;
114  }
115 
116  len = fts_get_table_id(fts_table, table_id);
117 
118  prefix_name_len = dbname_len + 4 + len + 1;
119 
120  prefix_name = static_cast<char*>(mem_alloc(prefix_name_len));
121 
122  len = sprintf(prefix_name, "%.*sFTS_%s",
123  dbname_len, fts_table->parent, table_id);
124 
125  ut_a(len > 0);
126  ut_a(len == prefix_name_len - 1);
127 
128  return(prefix_name);
129 }
130 
131 /******************************************************************/
134 UNIV_INTERN
135 char*
137 /*===============*/
138  const fts_table_t* fts_table)
140 {
141  int len;
142  char* name;
143  int name_len;
144  char* prefix_name;
145 
146  prefix_name = fts_get_table_name_prefix(fts_table);
147 
148  name_len = strlen(prefix_name) + 1 + strlen(fts_table->suffix) + 1;
149 
150  name = static_cast<char*>(mem_alloc(name_len));
151 
152  len = sprintf(name, "%s_%s", prefix_name, fts_table->suffix);
153 
154  ut_a(len > 0);
155  ut_a(len == name_len - 1);
156 
157  mem_free(prefix_name);
158 
159  return(name);
160 }
161 
162 /******************************************************************/
165 UNIV_INTERN
166 que_t*
168 /*==========*/
170  pars_info_t* info,
171  const char* sql)
172 {
173  char* str;
174  que_t* graph;
175  char* str_tmp;
176  ibool dict_locked;
177 
178  if (fts_table != NULL) {
179  char* table_name;
180 
181  table_name = fts_get_table_name(fts_table);
182  str_tmp = ut_strreplace(sql, "%s", table_name);
183  mem_free(table_name);
184  } else {
185  ulint sql_len = strlen(sql) + 1;
186 
187  str_tmp = static_cast<char*>(mem_alloc(sql_len));
188  strcpy(str_tmp, sql);
189  }
190 
191  str = ut_str3cat(fts_sql_begin, str_tmp, fts_sql_end);
192  mem_free(str_tmp);
193 
194  dict_locked = (fts_table && fts_table->table
195  && (fts_table->table->fts->fts_status
196  & TABLE_DICT_LOCKED));
197 
198  if (!dict_locked) {
199  ut_ad(!mutex_own(&(dict_sys->mutex)));
200 
201  /* The InnoDB SQL parser is not re-entrant. */
202  mutex_enter(&dict_sys->mutex);
203  }
204 
205  graph = pars_sql(info, str);
206  ut_a(graph);
207 
208  if (!dict_locked) {
209  mutex_exit(&dict_sys->mutex);
210  }
211 
212  mem_free(str);
213 
214  return(graph);
215 }
216 
217 /******************************************************************/
220 UNIV_INTERN
221 que_t*
223 /*=======================*/
225  pars_info_t* info,
226  const char* sql)
227 {
228  char* str;
229  que_t* graph;
230  char* str_tmp = NULL;
231 
232 #ifdef UNIV_DEBUG
233  ut_ad(mutex_own(&dict_sys->mutex));
234 #endif
235 
236  if (fts_table != NULL) {
237  char* table_name;
238 
239  table_name = fts_get_table_name(fts_table);
240  str_tmp = ut_strreplace(sql, "%s", table_name);
241  mem_free(table_name);
242  }
243 
244  if (str_tmp != NULL) {
245  str = ut_str3cat(fts_sql_begin, str_tmp, fts_sql_end);
246  mem_free(str_tmp);
247  } else {
248  str = ut_str3cat(fts_sql_begin, sql, fts_sql_end);
249  }
250 
251  //fprintf(stderr, "%s\n", str);
252 
253  graph = pars_sql(info, str);
254  ut_a(graph);
255 
256  mem_free(str);
257 
258  return(graph);
259 }
260 
261 /******************************************************************/
264 UNIV_INTERN
265 dberr_t
267 /*=========*/
268  trx_t* trx,
269  que_t* graph)
270 {
271  que_thr_t* thr;
272 
273  graph->trx = trx;
274  graph->fork_type = QUE_FORK_MYSQL_INTERFACE;
275 
276  ut_a(thr = que_fork_start_command(graph));
277 
278  que_run_threads(thr);
279 
280  return(trx->error_state);
281 }
282 
283 /******************************************************************/
298 UNIV_INTERN
299 const char*
301 /*=======================*/
303  pars_info_t* info,
304  mem_heap_t* heap)
305 {
306  ulint i;
307  const char* str = "";
308 
309  for (i = 0; i < index->n_user_defined_cols; i++) {
310  char* sel_str;
311 
312  dict_field_t* field = dict_index_get_nth_field(index, i);
313 
314  sel_str = mem_heap_printf(heap, "sel%lu", (ulong) i);
315 
316  /* Set copy_name to TRUE since it's dynamic. */
317  pars_info_bind_id(info, TRUE, sel_str, field->name);
318 
319  str = mem_heap_printf(
320  heap, "%s%s$%s", str, (*str) ? ", " : "", sel_str);
321  }
322 
323  return(str);
324 }
325 
326 /******************************************************************/
329 UNIV_INTERN
330 dberr_t
332 /*===========*/
333  trx_t* trx)
334 {
335  dberr_t error;
336 
337  error = trx_commit_for_mysql(trx);
338 
339  /* Commit should always succeed */
340  ut_a(error == DB_SUCCESS);
341 
342  return(DB_SUCCESS);
343 }
344 
345 /******************************************************************/
348 UNIV_INTERN
349 dberr_t
351 /*=============*/
352  trx_t* trx)
353 {
354  return(trx_rollback_to_savepoint(trx, NULL));
355 }