1/* Copyright (C) 2009-2017 Kentoku Shiba
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
15
16#define MYSQL_SERVER 1
17#include <my_global.h>
18#include "mysql_version.h"
19#include "spd_environ.h"
20#if MYSQL_VERSION_ID < 50500
21#include "mysql_priv.h"
22#include <mysql/plugin.h>
23#else
24#include "sql_priv.h"
25#include "probes_mysql.h"
26#include "sql_class.h"
27#include "sql_base.h"
28#include "sql_partition.h"
29#include "transaction.h"
30#endif
31#include "spd_err.h"
32#include "spd_param.h"
33#include "spd_db_include.h"
34#include "spd_include.h"
35#include "ha_spider.h"
36#include "spd_db_conn.h"
37#include "spd_trx.h"
38#include "spd_conn.h"
39#include "spd_sys_table.h"
40#include "spd_table.h"
41#include "spd_copy_tables.h"
42#include "spd_udf.h"
43#include "spd_malloc.h"
44
45extern handlerton *spider_hton_ptr;
46extern SPIDER_DBTON spider_dbton[SPIDER_DBTON_SIZE];
47
48int spider_udf_set_copy_tables_param_default(
49 SPIDER_COPY_TABLES *copy_tables
50) {
51 DBUG_ENTER("spider_udf_set_copy_tables_param_default");
52
53 if (!copy_tables->database)
54 {
55 DBUG_PRINT("info",("spider create default database"));
56 copy_tables->database_length = copy_tables->trx->thd->db.length;
57 if (
58 !(copy_tables->database = spider_create_string(
59 copy_tables->trx->thd->db.str,
60 copy_tables->database_length))
61 ) {
62 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
63 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
64 }
65 }
66
67 if (copy_tables->bulk_insert_interval == -1)
68 copy_tables->bulk_insert_interval = 10;
69 if (copy_tables->bulk_insert_rows == -1)
70 copy_tables->bulk_insert_rows = 100;
71 if (copy_tables->use_table_charset == -1)
72 copy_tables->use_table_charset = 1;
73 if (copy_tables->use_transaction == -1)
74 copy_tables->use_transaction = 1;
75#ifndef WITHOUT_SPIDER_BG_SEARCH
76 if (copy_tables->bg_mode == -1)
77 copy_tables->bg_mode = 0;
78#endif
79 DBUG_RETURN(0);
80}
81
82#define SPIDER_PARAM_STR_LEN(name) name ## _length
83#define SPIDER_PARAM_STR(title_name, param_name) \
84 if (!strncasecmp(tmp_ptr, title_name, title_length)) \
85 { \
86 DBUG_PRINT("info",("spider " title_name " start")); \
87 if (!copy_tables->param_name) \
88 { \
89 if ((copy_tables->param_name = spider_get_string_between_quote( \
90 start_ptr, TRUE, &param_string_parse))) \
91 copy_tables->SPIDER_PARAM_STR_LEN(param_name) = \
92 strlen(copy_tables->param_name); \
93 else \
94 { \
95 error_num = param_string_parse.print_param_error(); \
96 goto error; \
97 } \
98 DBUG_PRINT("info",("spider " title_name "=%s", copy_tables->param_name)); \
99 } \
100 break; \
101 }
102#define SPIDER_PARAM_HINT_WITH_MAX(title_name, param_name, check_length, max_size, min_val, max_val) \
103 if (!strncasecmp(tmp_ptr, title_name, check_length)) \
104 { \
105 DBUG_PRINT("info",("spider " title_name " start")); \
106 DBUG_PRINT("info",("spider max_size=%d", max_size)); \
107 int hint_num = atoi(tmp_ptr + check_length) - 1; \
108 DBUG_PRINT("info",("spider hint_num=%d", hint_num)); \
109 DBUG_PRINT("info",("spider copy_tables->param_name=%x", \
110 copy_tables->param_name)); \
111 if (copy_tables->param_name) \
112 { \
113 if (hint_num < 0 || hint_num >= max_size) \
114 { \
115 error_num = param_string_parse.print_param_error(); \
116 goto error; \
117 } else if (copy_tables->param_name[hint_num] != -1) \
118 break; \
119 char *hint_str = spider_get_string_between_quote(start_ptr, FALSE); \
120 if (hint_str) \
121 { \
122 copy_tables->param_name[hint_num] = atoi(hint_str); \
123 if (copy_tables->param_name[hint_num] < min_val) \
124 copy_tables->param_name[hint_num] = min_val; \
125 else if (copy_tables->param_name[hint_num] > max_val) \
126 copy_tables->param_name[hint_num] = max_val; \
127 } else { \
128 error_num = param_string_parse.print_param_error(); \
129 goto error; \
130 } \
131 DBUG_PRINT("info",("spider " title_name "[%d]=%d", hint_num, \
132 copy_tables->param_name[hint_num])); \
133 } else { \
134 error_num = param_string_parse.print_param_error(); \
135 goto error; \
136 } \
137 break; \
138 }
139#define SPIDER_PARAM_INT_WITH_MAX(title_name, param_name, min_val, max_val) \
140 if (!strncasecmp(tmp_ptr, title_name, title_length)) \
141 { \
142 DBUG_PRINT("info",("spider " title_name " start")); \
143 if (copy_tables->param_name == -1) \
144 { \
145 if ((tmp_ptr2 = spider_get_string_between_quote( \
146 start_ptr, FALSE))) \
147 { \
148 copy_tables->param_name = atoi(tmp_ptr2); \
149 if (copy_tables->param_name < min_val) \
150 copy_tables->param_name = min_val; \
151 else if (copy_tables->param_name > max_val) \
152 copy_tables->param_name = max_val; \
153 param_string_parse.set_param_value(tmp_ptr2, \
154 tmp_ptr2 + \
155 strlen(tmp_ptr2) + 1); \
156 } else { \
157 error_num = param_string_parse.print_param_error(); \
158 goto error; \
159 } \
160 DBUG_PRINT("info",("spider " title_name "=%d", copy_tables->param_name)); \
161 } \
162 break; \
163 }
164#define SPIDER_PARAM_INT(title_name, param_name, min_val) \
165 if (!strncasecmp(tmp_ptr, title_name, title_length)) \
166 { \
167 DBUG_PRINT("info",("spider " title_name " start")); \
168 if (copy_tables->param_name == -1) \
169 { \
170 if ((tmp_ptr2 = spider_get_string_between_quote( \
171 start_ptr, FALSE))) \
172 { \
173 copy_tables->param_name = atoi(tmp_ptr2); \
174 if (copy_tables->param_name < min_val) \
175 copy_tables->param_name = min_val; \
176 param_string_parse.set_param_value(tmp_ptr2, \
177 tmp_ptr2 + \
178 strlen(tmp_ptr2) + 1); \
179 } else { \
180 error_num = param_string_parse.print_param_error(); \
181 goto error; \
182 } \
183 DBUG_PRINT("info",("spider " title_name "=%d", copy_tables->param_name)); \
184 } \
185 break; \
186 }
187#define SPIDER_PARAM_LONGLONG(title_name, param_name, min_val) \
188 if (!strncasecmp(tmp_ptr, title_name, title_length)) \
189 { \
190 DBUG_PRINT("info",("spider " title_name " start")); \
191 if (copy_tables->param_name == -1) \
192 { \
193 if ((tmp_ptr2 = spider_get_string_between_quote( \
194 start_ptr, FALSE))) \
195 { \
196 copy_tables->param_name = \
197 my_strtoll10(tmp_ptr2, (char**) NULL, &error_num); \
198 if (copy_tables->param_name < min_val) \
199 copy_tables->param_name = min_val; \
200 param_string_parse.set_param_value(tmp_ptr2, \
201 tmp_ptr2 + \
202 strlen(tmp_ptr2) + 1); \
203 } else { \
204 error_num = param_string_parse.print_param_error(); \
205 goto error; \
206 } \
207 DBUG_PRINT("info",("spider " title_name "=%lld", \
208 copy_tables->param_name)); \
209 } \
210 break; \
211 }
212
213int spider_udf_parse_copy_tables_param(
214 SPIDER_COPY_TABLES *copy_tables,
215 char *param,
216 int param_length
217) {
218 int error_num = 0;
219 char *param_string = NULL;
220 char *sprit_ptr[2];
221 char *tmp_ptr, *tmp_ptr2, *start_ptr;
222 int title_length;
223 SPIDER_PARAM_STRING_PARSE param_string_parse;
224 DBUG_ENTER("spider_udf_parse_copy_tables_param");
225 copy_tables->bulk_insert_interval = -1;
226 copy_tables->bulk_insert_rows = -1;
227 copy_tables->use_table_charset = -1;
228 copy_tables->use_transaction = -1;
229#ifndef WITHOUT_SPIDER_BG_SEARCH
230 copy_tables->bg_mode = -1;
231#endif
232
233 if (param_length == 0)
234 goto set_default;
235 DBUG_PRINT("info",("spider create param_string string"));
236 if (
237 !(param_string = spider_create_string(
238 param,
239 param_length))
240 ) {
241 error_num = HA_ERR_OUT_OF_MEM;
242 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
243 goto error_alloc_param_string;
244 }
245 DBUG_PRINT("info",("spider param_string=%s", param_string));
246
247 sprit_ptr[0] = param_string;
248 param_string_parse.init(param_string, ER_SPIDER_INVALID_UDF_PARAM_NUM);
249 while (sprit_ptr[0])
250 {
251 if ((sprit_ptr[1] = strchr(sprit_ptr[0], ',')))
252 {
253 *sprit_ptr[1] = '\0';
254 sprit_ptr[1]++;
255 }
256 tmp_ptr = sprit_ptr[0];
257 sprit_ptr[0] = sprit_ptr[1];
258 while (*tmp_ptr == ' ' || *tmp_ptr == '\r' ||
259 *tmp_ptr == '\n' || *tmp_ptr == '\t')
260 tmp_ptr++;
261
262 if (*tmp_ptr == '\0')
263 continue;
264
265 title_length = 0;
266 start_ptr = tmp_ptr;
267 while (*start_ptr != ' ' && *start_ptr != '\'' &&
268 *start_ptr != '"' && *start_ptr != '\0' &&
269 *start_ptr != '\r' && *start_ptr != '\n' &&
270 *start_ptr != '\t')
271 {
272 title_length++;
273 start_ptr++;
274 }
275 param_string_parse.set_param_title(tmp_ptr, tmp_ptr + title_length);
276
277 switch (title_length)
278 {
279 case 0:
280 error_num = param_string_parse.print_param_error();
281 if (error_num)
282 goto error;
283 continue;
284 case 3:
285#ifndef WITHOUT_SPIDER_BG_SEARCH
286 SPIDER_PARAM_INT_WITH_MAX("bgm", bg_mode, 0, 1);
287#endif
288 SPIDER_PARAM_INT("bii", bulk_insert_interval, 0);
289 SPIDER_PARAM_LONGLONG("bir", bulk_insert_rows, 1);
290 SPIDER_PARAM_STR("dtb", database);
291 SPIDER_PARAM_INT_WITH_MAX("utc", use_table_charset, 0, 1);
292 SPIDER_PARAM_INT_WITH_MAX("utr", use_transaction, 0, 1);
293 error_num = param_string_parse.print_param_error();
294 goto error;
295#ifndef WITHOUT_SPIDER_BG_SEARCH
296 case 7:
297 SPIDER_PARAM_INT_WITH_MAX("bg_mode", bg_mode, 0, 1);
298 error_num = param_string_parse.print_param_error();
299 goto error;
300#endif
301 case 8:
302 SPIDER_PARAM_STR("database", database);
303 error_num = param_string_parse.print_param_error();
304 goto error;
305 case 15:
306 SPIDER_PARAM_INT_WITH_MAX("use_transaction", use_transaction, 0, 1);
307 error_num = param_string_parse.print_param_error();
308 goto error;
309 case 16:
310 SPIDER_PARAM_LONGLONG("bulk_insert_rows", bulk_insert_rows, 1);
311 error_num = param_string_parse.print_param_error();
312 goto error;
313 case 17:
314 SPIDER_PARAM_INT_WITH_MAX(
315 "use_table_charset", use_table_charset, 0, 1);
316 error_num = param_string_parse.print_param_error();
317 goto error;
318 case 20:
319 SPIDER_PARAM_INT("bulk_insert_interval", bulk_insert_interval, 0);
320 error_num = param_string_parse.print_param_error();
321 goto error;
322 default:
323 error_num = param_string_parse.print_param_error();
324 goto error;
325 }
326
327 /* Verify that the remainder of the parameter value is whitespace */
328 if ((error_num = param_string_parse.has_extra_parameter_values()))
329 goto error;
330 }
331
332set_default:
333 if ((error_num = spider_udf_set_copy_tables_param_default(
334 copy_tables
335 )))
336 goto error;
337
338 if (param_string)
339 spider_free(spider_current_trx, param_string, MYF(0));
340 DBUG_RETURN(0);
341
342error:
343 if (param_string)
344 spider_free(spider_current_trx, param_string, MYF(0));
345error_alloc_param_string:
346 DBUG_RETURN(error_num);
347}
348
349int spider_udf_get_copy_tgt_tables(
350 THD *thd,
351 SPIDER_COPY_TABLES *copy_tables,
352 MEM_ROOT *mem_root,
353 bool need_lock
354) {
355 int error_num, roop_count;
356 TABLE *table_tables = NULL;
357#if MYSQL_VERSION_ID < 50500
358 Open_tables_state open_tables_backup;
359#else
360 Open_tables_backup open_tables_backup;
361#endif
362 char table_key[MAX_KEY_LENGTH];
363 SPIDER_COPY_TABLE_CONN *table_conn = NULL, *src_table_conn_prev = NULL,
364 *dst_table_conn_prev = NULL;
365 SPIDER_SHARE *tmp_share;
366 char **tmp_connect_info;
367 uint *tmp_connect_info_length;
368 long *tmp_long;
369 longlong *tmp_longlong;
370 DBUG_ENTER("spider_udf_get_copy_tgt_tables");
371
372 if (
373 !(table_tables = spider_open_sys_table(
374 thd, SPIDER_SYS_TABLES_TABLE_NAME_STR,
375 SPIDER_SYS_TABLES_TABLE_NAME_LEN, FALSE, &open_tables_backup,
376 need_lock, &error_num))
377 ) {
378 my_error(error_num, MYF(0));
379 goto error;
380 }
381 spider_store_db_and_table_name(table_tables,
382 copy_tables->spider_db_name, copy_tables->spider_db_name_length,
383 copy_tables->spider_table_name, copy_tables->spider_table_name_length
384 );
385 if ((error_num = spider_get_sys_table_by_idx(table_tables, table_key,
386 table_tables->s->primary_key, 2)))
387 {
388 table_tables->file->print_error(error_num, MYF(0));
389 goto error;
390 }
391 do {
392 if (!(table_conn = (SPIDER_COPY_TABLE_CONN *)
393 spider_bulk_malloc(spider_current_trx, 25, MYF(MY_WME | MY_ZEROFILL),
394 &table_conn, sizeof(SPIDER_COPY_TABLE_CONN),
395 &tmp_share, sizeof(SPIDER_SHARE),
396 &tmp_connect_info, sizeof(char *) * SPIDER_TMP_SHARE_CHAR_PTR_COUNT,
397 &tmp_connect_info_length, sizeof(uint) * SPIDER_TMP_SHARE_UINT_COUNT,
398 &tmp_long, sizeof(long) * SPIDER_TMP_SHARE_LONG_COUNT,
399 &tmp_longlong, sizeof(longlong) * SPIDER_TMP_SHARE_LONGLONG_COUNT,
400 NullS))
401 ) {
402 spider_sys_index_end(table_tables);
403 error_num = HA_ERR_OUT_OF_MEM;
404 my_error(HA_ERR_OUT_OF_MEM, MYF(0));
405 goto error;
406 }
407 spider_set_tmp_share_pointer(tmp_share, tmp_connect_info,
408 tmp_connect_info_length, tmp_long, tmp_longlong);
409 tmp_share->link_statuses[0] = -1;
410 table_conn->share = tmp_share;
411
412 if (
413 (error_num = spider_get_sys_tables_connect_info(
414 table_tables, tmp_share, 0, mem_root)) ||
415 (error_num = spider_get_sys_tables_link_status(
416 table_tables, tmp_share, 0, mem_root)) ||
417 (error_num = spider_get_sys_tables_link_idx(
418 table_tables, &table_conn->link_idx, mem_root))
419 ) {
420 table_tables->file->print_error(error_num, MYF(0));
421 spider_sys_index_end(table_tables);
422 goto error;
423 }
424 if (
425 (error_num = spider_set_connect_info_default(
426 tmp_share,
427#ifdef WITH_PARTITION_STORAGE_ENGINE
428 NULL,
429 NULL,
430#endif
431 NULL
432 )) ||
433 (error_num = spider_set_connect_info_default_db_table(
434 tmp_share,
435 copy_tables->spider_db_name, copy_tables->spider_db_name_length,
436 copy_tables->spider_table_name, copy_tables->spider_table_name_length
437 )) ||
438 (error_num = spider_create_conn_keys(tmp_share)) ||
439 (error_num = spider_create_tmp_dbton_share(tmp_share))
440 ) {
441 spider_sys_index_end(table_tables);
442 goto error;
443 }
444
445/*
446 if (spider_db_create_table_names_str(tmp_share))
447 {
448 spider_sys_index_end(table_tables);
449 error_num = HA_ERR_OUT_OF_MEM;
450 my_error(HA_ERR_OUT_OF_MEM, MYF(0));
451 goto error;
452 }
453*/
454
455 for (roop_count = 0; roop_count < (int) tmp_share->use_dbton_count;
456 roop_count++)
457 {
458 uint dbton_id = tmp_share->use_dbton_ids[roop_count];
459
460 if (!spider_dbton[dbton_id].create_db_copy_table)
461 continue;
462
463 if (!(table_conn->copy_table =
464 spider_dbton[dbton_id].create_db_copy_table(
465 tmp_share->dbton_share[dbton_id])))
466 {
467 spider_sys_index_end(table_tables);
468 error_num = HA_ERR_OUT_OF_MEM;
469 my_error(HA_ERR_OUT_OF_MEM, MYF(0));
470 goto error;
471 }
472 if ((error_num = table_conn->copy_table->init()))
473 goto error;
474 break;
475 }
476
477 if (
478 !copy_tables->use_auto_mode[0]
479 ) {
480 for (roop_count = 0; roop_count < copy_tables->link_idx_count[0];
481 roop_count++)
482 {
483 if (table_conn->link_idx == copy_tables->link_idxs[0][roop_count])
484 {
485 if (tmp_share->link_statuses[0] == SPIDER_LINK_STATUS_NG)
486 {
487 spider_sys_index_end(table_tables);
488 error_num = ER_SPIDER_UDF_COPY_TABLE_SRC_NG_STATUS_NUM;
489 my_printf_error(ER_SPIDER_UDF_COPY_TABLE_SRC_NG_STATUS_NUM,
490 ER_SPIDER_UDF_COPY_TABLE_SRC_NG_STATUS_STR, MYF(0));
491 goto error;
492 }
493 if (src_table_conn_prev)
494 src_table_conn_prev->next = table_conn;
495 else
496 copy_tables->table_conn[0] = table_conn;
497 src_table_conn_prev = table_conn;
498 table_conn = NULL;
499 break;
500 }
501 }
502 }
503 if (table_conn && !copy_tables->use_auto_mode[1])
504 {
505 for (roop_count = 0; roop_count < copy_tables->link_idx_count[1];
506 roop_count++)
507 {
508 if (table_conn->link_idx == copy_tables->link_idxs[1][roop_count])
509 {
510 if (tmp_share->link_statuses[0] == SPIDER_LINK_STATUS_NG)
511 {
512 spider_sys_index_end(table_tables);
513 error_num = ER_SPIDER_UDF_COPY_TABLE_SRC_NG_STATUS_NUM;
514 my_printf_error(ER_SPIDER_UDF_COPY_TABLE_SRC_NG_STATUS_NUM,
515 ER_SPIDER_UDF_COPY_TABLE_SRC_NG_STATUS_STR, MYF(0));
516 goto error;
517 }
518 if (dst_table_conn_prev)
519 dst_table_conn_prev->next = table_conn;
520 else
521 copy_tables->table_conn[1] = table_conn;
522 dst_table_conn_prev = table_conn;
523 table_conn = NULL;
524 break;
525 }
526 }
527 }
528 if (table_conn && copy_tables->use_auto_mode[0] &&
529 tmp_share->link_statuses[0] == SPIDER_LINK_STATUS_OK)
530 {
531 if (src_table_conn_prev)
532 src_table_conn_prev->next = table_conn;
533 else
534 copy_tables->table_conn[0] = table_conn;
535 src_table_conn_prev = table_conn;
536 copy_tables->link_idx_count[0]++;
537 table_conn = NULL;
538 }
539 if (table_conn && copy_tables->use_auto_mode[1] &&
540 tmp_share->link_statuses[0] == SPIDER_LINK_STATUS_RECOVERY)
541 {
542 if (dst_table_conn_prev)
543 dst_table_conn_prev->next = table_conn;
544 else
545 copy_tables->table_conn[1] = table_conn;
546 dst_table_conn_prev = table_conn;
547 copy_tables->link_idx_count[1]++;
548 table_conn = NULL;
549 }
550 if (table_conn)
551 {
552 spider_free_tmp_dbton_share(tmp_share);
553 spider_free_tmp_share_alloc(tmp_share);
554 if (table_conn->copy_table)
555 delete table_conn->copy_table;
556 spider_free(spider_current_trx, table_conn, MYF(0));
557 table_conn = NULL;
558 }
559
560 error_num = spider_sys_index_next_same(table_tables, table_key);
561 } while (error_num == 0);
562 spider_sys_index_end(table_tables);
563 spider_close_sys_table(thd, table_tables,
564 &open_tables_backup, need_lock);
565 table_tables = NULL;
566
567 if (!copy_tables->table_conn[0])
568 {
569 error_num = ER_SPIDER_UDF_COPY_TABLE_SRC_NOT_FOUND_NUM;
570 my_printf_error(ER_SPIDER_UDF_COPY_TABLE_SRC_NOT_FOUND_NUM,
571 ER_SPIDER_UDF_COPY_TABLE_SRC_NOT_FOUND_STR, MYF(0));
572 goto error;
573 }
574 if (!copy_tables->table_conn[1])
575 {
576 error_num = ER_SPIDER_UDF_COPY_TABLE_DST_NOT_FOUND_NUM;
577 my_printf_error(ER_SPIDER_UDF_COPY_TABLE_DST_NOT_FOUND_NUM,
578 ER_SPIDER_UDF_COPY_TABLE_DST_NOT_FOUND_STR, MYF(0));
579 goto error;
580 }
581
582 DBUG_RETURN(0);
583
584error:
585 if (table_tables)
586 spider_close_sys_table(thd, table_tables,
587 &open_tables_backup, need_lock);
588 if (table_conn)
589 {
590 spider_free_tmp_dbton_share(tmp_share);
591 spider_free_tmp_share_alloc(tmp_share);
592 if (table_conn->copy_table)
593 delete table_conn->copy_table;
594 spider_free(spider_current_trx, table_conn, MYF(0));
595 }
596 DBUG_RETURN(error_num);
597}
598
599int spider_udf_get_copy_tgt_conns(
600 SPIDER_COPY_TABLES *copy_tables
601) {
602 int error_num, roop_count;
603 SPIDER_TRX *trx = copy_tables->trx;
604 SPIDER_SHARE *share;
605 SPIDER_COPY_TABLE_CONN *table_conn;
606 DBUG_ENTER("spider_udf_get_copy_tgt_conns");
607 for (roop_count = 0; roop_count < 2; roop_count++)
608 {
609 table_conn = copy_tables->table_conn[roop_count];
610 while (table_conn)
611 {
612 share = table_conn->share;
613 if (
614 !(table_conn->conn = spider_get_conn(
615 share, 0, share->conn_keys[0], trx, NULL, FALSE, FALSE,
616 SPIDER_CONN_KIND_MYSQL, &error_num))
617 ) {
618 my_error(ER_CONNECT_TO_FOREIGN_DATA_SOURCE, MYF(0), share->server_names[0]);
619 DBUG_RETURN(ER_CONNECT_TO_FOREIGN_DATA_SOURCE);
620 }
621 table_conn->conn->error_mode = 0;
622 table_conn = table_conn->next;
623 }
624 }
625 DBUG_RETURN(0);
626}
627
628void spider_udf_free_copy_tables_alloc(
629 SPIDER_COPY_TABLES *copy_tables
630) {
631 int roop_count;
632 SPIDER_COPY_TABLE_CONN *table_conn, *table_conn_next;
633 DBUG_ENTER("spider_udf_free_copy_tables_alloc");
634 for (roop_count = 0; roop_count < 2; roop_count++)
635 {
636 table_conn = copy_tables->table_conn[roop_count];
637 while (table_conn)
638 {
639 table_conn_next = table_conn->next;
640 spider_free_tmp_dbton_share(table_conn->share);
641 spider_free_tmp_share_alloc(table_conn->share);
642 if (table_conn->copy_table)
643 delete table_conn->copy_table;
644 spider_free(spider_current_trx, table_conn, MYF(0));
645 table_conn = table_conn_next;
646 }
647 }
648 if (copy_tables->link_idxs[0])
649 spider_free(spider_current_trx, copy_tables->link_idxs[0], MYF(0));
650 if (copy_tables->database)
651 spider_free(spider_current_trx, copy_tables->database, MYF(0));
652 spider_free(spider_current_trx, copy_tables, MYF(0));
653 DBUG_VOID_RETURN;
654}
655
656int spider_udf_copy_tables_create_table_list(
657 SPIDER_COPY_TABLES *copy_tables,
658 char *spider_table_name,
659 uint spider_table_name_length,
660 char *src_link_idx_list,
661 uint src_link_idx_list_length,
662 char *dst_link_idx_list,
663 uint dst_link_idx_list_length
664) {
665 int roop_count, roop_count2, length;
666 char *tmp_ptr, *tmp_ptr2, *tmp_ptr3, *tmp_name_ptr;
667 DBUG_ENTER("spider_udf_copy_tables_create_table_list");
668
669 if (!spider_table_name_length)
670 {
671 my_printf_error(ER_SPIDER_BLANK_UDF_ARGUMENT_NUM,
672 ER_SPIDER_BLANK_UDF_ARGUMENT_STR, MYF(0), 1);
673 DBUG_RETURN(ER_SPIDER_BLANK_UDF_ARGUMENT_NUM);
674 }
675
676 for (roop_count2 = 0; roop_count2 < 2; roop_count2++)
677 {
678 if (roop_count2 == 0)
679 tmp_ptr = src_link_idx_list;
680 else
681 tmp_ptr = dst_link_idx_list;
682
683 while (*tmp_ptr == ' ')
684 tmp_ptr++;
685 if (*tmp_ptr)
686 copy_tables->link_idx_count[roop_count2] = 1;
687 else {
688 /* use auto detect */
689 copy_tables->use_auto_mode[roop_count2] = TRUE;
690 copy_tables->link_idx_count[roop_count2] = 0;
691 continue;
692 }
693
694 while (TRUE)
695 {
696 if ((tmp_ptr2 = strchr(tmp_ptr, ' ')))
697 {
698 copy_tables->link_idx_count[roop_count2]++;
699 tmp_ptr = tmp_ptr2 + 1;
700 while (*tmp_ptr == ' ')
701 tmp_ptr++;
702 } else
703 break;
704 }
705 }
706
707 if (!(copy_tables->link_idxs[0] = (int *)
708 spider_bulk_malloc(spider_current_trx, 26, MYF(MY_WME | MY_ZEROFILL),
709 &copy_tables->link_idxs[0],
710 sizeof(int) * copy_tables->link_idx_count[0],
711 &copy_tables->link_idxs[1],
712 sizeof(int) * copy_tables->link_idx_count[1],
713 &tmp_name_ptr, sizeof(char) * (
714 spider_table_name_length * 2 + copy_tables->database_length + 3
715 ),
716 NullS))
717 ) {
718 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
719 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
720 }
721
722 copy_tables->spider_db_name = tmp_name_ptr;
723 if ((tmp_ptr3 = strchr(spider_table_name, '.')))
724 {
725 /* exist database name */
726 *tmp_ptr3 = '\0';
727 length = strlen(spider_table_name);
728 memcpy(tmp_name_ptr, spider_table_name, length + 1);
729 copy_tables->spider_db_name_length = length;
730 tmp_name_ptr += length + 1;
731 tmp_ptr3++;
732 } else {
733 memcpy(tmp_name_ptr, copy_tables->database,
734 copy_tables->database_length + 1);
735 copy_tables->spider_db_name_length = copy_tables->database_length;
736 tmp_name_ptr += copy_tables->database_length + 1;
737 tmp_ptr3 = spider_table_name;
738 length = -1;
739 }
740 copy_tables->spider_table_name = tmp_name_ptr;
741 length = spider_table_name_length - length - 1;
742 memcpy(tmp_name_ptr, tmp_ptr3, length + 1);
743 copy_tables->spider_table_name_length = length;
744 tmp_name_ptr += length + 1;
745 memcpy(tmp_name_ptr, tmp_ptr3, length + 1);
746 copy_tables->spider_real_table_name = tmp_name_ptr;
747 if ((tmp_ptr2 = strstr(tmp_name_ptr, "#P#")))
748 {
749 *tmp_ptr2 = '\0';
750 copy_tables->spider_real_table_name_length = strlen(tmp_name_ptr);
751 } else
752 copy_tables->spider_real_table_name_length = length;
753
754 DBUG_PRINT("info",("spider spider_db=%s", copy_tables->spider_db_name));
755 DBUG_PRINT("info",("spider spider_table_name=%s",
756 copy_tables->spider_table_name));
757 DBUG_PRINT("info",("spider spider_real_table_name=%s",
758 copy_tables->spider_real_table_name));
759
760 for (roop_count2 = 0; roop_count2 < 2; roop_count2++)
761 {
762 if (roop_count2 == 0)
763 tmp_ptr = src_link_idx_list;
764 else
765 tmp_ptr = dst_link_idx_list;
766
767 while (*tmp_ptr == ' ')
768 tmp_ptr++;
769 roop_count = 0;
770 while (*tmp_ptr)
771 {
772 if ((tmp_ptr2 = strchr(tmp_ptr, ' ')))
773 *tmp_ptr2 = '\0';
774
775 copy_tables->link_idxs[roop_count2][roop_count] = atoi(tmp_ptr);
776
777 DBUG_PRINT("info",("spider link_idx[%d][%d]=%d",
778 roop_count2, roop_count,
779 copy_tables->link_idxs[roop_count2][roop_count]));
780 if (!tmp_ptr2)
781 break;
782
783 tmp_ptr = tmp_ptr2 + 1;
784 while (*tmp_ptr == ' ')
785 tmp_ptr++;
786 roop_count++;
787 }
788 }
789 DBUG_RETURN(0);
790}
791
792#ifndef WITHOUT_SPIDER_BG_SEARCH
793int spider_udf_bg_copy_exec_sql(
794 SPIDER_COPY_TABLE_CONN *table_conn
795) {
796 int error_num;
797 SPIDER_CONN *conn = table_conn->conn;
798 ha_spider *spider = table_conn->spider;
799 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
800 DBUG_ENTER("spider_udf_bg_copy_exec_sql");
801 if ((error_num = spider_create_conn_thread(conn)))
802 DBUG_RETURN(error_num);
803 if ((error_num = dbton_hdl->set_sql_for_exec(table_conn->copy_table,
804 SPIDER_SQL_TYPE_INSERT_SQL)))
805 DBUG_RETURN(error_num);
806 pthread_mutex_lock(&conn->bg_conn_mutex);
807 conn->bg_target = spider;
808 conn->bg_error_num = &table_conn->bg_error_num;
809 conn->bg_sql_type = SPIDER_SQL_TYPE_INSERT_SQL;
810 conn->link_idx = 0;
811 conn->bg_exec_sql = TRUE;
812 conn->bg_caller_sync_wait = TRUE;
813 pthread_mutex_lock(&conn->bg_conn_sync_mutex);
814 pthread_cond_signal(&conn->bg_conn_cond);
815 pthread_mutex_unlock(&conn->bg_conn_mutex);
816 pthread_cond_wait(&conn->bg_conn_sync_cond, &conn->bg_conn_sync_mutex);
817 pthread_mutex_unlock(&conn->bg_conn_sync_mutex);
818 conn->bg_caller_sync_wait = FALSE;
819 DBUG_RETURN(0);
820}
821#endif
822
823long long spider_copy_tables_body(
824 UDF_INIT *initid,
825 UDF_ARGS *args,
826 char *is_null,
827 char *error
828) {
829 int error_num, roop_count, all_link_cnt = 0, use_table_charset;
830 SPIDER_COPY_TABLES *copy_tables = NULL;
831 THD *thd = current_thd;
832 TABLE_LIST *table_list = NULL;
833 TABLE *table;
834 TABLE_SHARE *table_share;
835 KEY *key_info;
836 ha_spider *spider = NULL, *tmp_spider;
837 spider_string *tmp_sql = NULL;
838 SPIDER_COPY_TABLE_CONN *table_conn, *src_tbl_conn, *dst_tbl_conn;
839 SPIDER_CONN *tmp_conn;
840 spider_db_copy_table *select_ct, *insert_ct;
841 MEM_ROOT mem_root;
842 longlong bulk_insert_rows;
843 Reprepare_observer *reprepare_observer_backup;
844 uint tmp_conn_link_idx = 0;
845 DBUG_ENTER("spider_copy_tables_body");
846 if (
847 thd->open_tables != 0 ||
848 thd->handler_tables_hash.records != 0 ||
849 thd->derived_tables != 0 ||
850 thd->lock != 0 ||
851#if MYSQL_VERSION_ID < 50500
852 thd->locked_tables != 0 ||
853 thd->prelocked_mode != NON_PRELOCKED
854#else
855 thd->locked_tables_list.locked_tables() ||
856 thd->locked_tables_mode != LTM_NONE
857#endif
858 ) {
859 if (thd->open_tables != 0)
860 {
861 my_printf_error(ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_NUM,
862 ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_STR_WITH_PTR, MYF(0),
863 "thd->open_tables", thd->open_tables);
864 } else if (thd->handler_tables_hash.records != 0)
865 {
866 my_printf_error(ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_NUM,
867 ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_STR_WITH_NUM, MYF(0),
868 "thd->handler_tables_hash.records",
869 (longlong) thd->handler_tables_hash.records);
870 } else if (thd->derived_tables != 0)
871 {
872 my_printf_error(ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_NUM,
873 ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_STR_WITH_PTR, MYF(0),
874 "thd->derived_tables", thd->derived_tables);
875 } else if (thd->lock != 0)
876 {
877 my_printf_error(ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_NUM,
878 ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_STR_WITH_PTR, MYF(0),
879 "thd->lock", thd->lock);
880#if MYSQL_VERSION_ID < 50500
881 } else if (thd->locked_tables != 0)
882 {
883 my_printf_error(ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_NUM,
884 ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_STR_WITH_PTR, MYF(0),
885 "thd->locked_tables", thd->locked_tables);
886 } else if (thd->prelocked_mode != NON_PRELOCKED)
887 {
888 my_printf_error(ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_NUM,
889 ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_STR_WITH_NUM, MYF(0),
890 "thd->prelocked_mode", (longlong) thd->prelocked_mode);
891#else
892 } else if (thd->locked_tables_list.locked_tables())
893 {
894 my_printf_error(ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_NUM,
895 ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_STR_WITH_PTR, MYF(0),
896 "thd->locked_tables_list.locked_tables()",
897 thd->locked_tables_list.locked_tables());
898 } else if (thd->locked_tables_mode != LTM_NONE)
899 {
900 my_printf_error(ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_NUM,
901 ER_SPIDER_UDF_CANT_USE_IF_OPEN_TABLE_STR_WITH_NUM, MYF(0),
902 "thd->locked_tables_mode", (longlong) thd->locked_tables_mode);
903#endif
904 }
905 goto error;
906 }
907
908 if (!(copy_tables = (SPIDER_COPY_TABLES *)
909 spider_bulk_malloc(spider_current_trx, 27, MYF(MY_WME | MY_ZEROFILL),
910 &copy_tables, sizeof(SPIDER_COPY_TABLES),
911 NullS))
912 ) {
913 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
914 goto error;
915 }
916 if (!(copy_tables->trx = spider_get_trx(thd, TRUE, &error_num)))
917 {
918 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
919 goto error;
920 }
921
922 if (args->arg_count == 4)
923 {
924 if (spider_udf_parse_copy_tables_param(
925 copy_tables,
926 args->args[3] ? args->args[3] : (char *) "",
927 args->args[3] ? args->lengths[3] : 0
928 ))
929 goto error;
930 } else {
931 if (spider_udf_parse_copy_tables_param(
932 copy_tables,
933 (char *) "",
934 0
935 ))
936 goto error;
937 }
938 if (
939 spider_udf_copy_tables_create_table_list(
940 copy_tables,
941 args->args[0],
942 args->lengths[0],
943 args->args[1] ? args->args[1] : (char *) "",
944 args->args[1] ? args->lengths[1] : 0,
945 args->args[2] ? args->args[2] : (char *) "",
946 args->args[2] ? args->lengths[2] : 0
947 )
948 )
949 goto error;
950
951 SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME));
952 if (
953 spider_udf_get_copy_tgt_tables(
954 thd,
955 copy_tables,
956 &mem_root,
957 TRUE
958 )
959 ) {
960 free_root(&mem_root, MYF(0));
961 goto error;
962 }
963 free_root(&mem_root, MYF(0));
964
965 if (
966 spider_udf_get_copy_tgt_conns(copy_tables)
967 )
968 goto error;
969
970 table_list = &copy_tables->spider_table_list;
971 table_list->db.str = copy_tables->spider_db_name;
972 table_list->db.length = copy_tables->spider_db_name_length;
973 table_list->alias.str = table_list->table_name.str =
974 copy_tables->spider_real_table_name;
975 table_list->table_name.length = copy_tables->spider_real_table_name_length;
976 table_list->alias.length= table_list->table_name.length;
977 table_list->lock_type = TL_READ;
978
979 DBUG_PRINT("info",("spider db=%s", table_list->db.str));
980 DBUG_PRINT("info",("spider db_length=%zd", table_list->db.length));
981 DBUG_PRINT("info",("spider table_name=%s", table_list->table_name.str));
982 DBUG_PRINT("info",("spider table_name_length=%zd",
983 table_list->table_name.length));
984 reprepare_observer_backup = thd->m_reprepare_observer;
985 thd->m_reprepare_observer = NULL;
986 copy_tables->trx->trx_start = TRUE;
987 copy_tables->trx->updated_in_this_trx = FALSE;
988 DBUG_PRINT("info",("spider trx->updated_in_this_trx=FALSE"));
989#if MYSQL_VERSION_ID < 50500
990 if (open_and_lock_tables(thd, table_list))
991#else
992 table_list->mdl_request.init(
993 MDL_key::TABLE,
994 table_list->db.str,
995 table_list->table_name.str,
996 MDL_SHARED_READ,
997 MDL_TRANSACTION
998 );
999 if (open_and_lock_tables(thd, table_list, FALSE, 0))
1000#endif
1001 {
1002 thd->m_reprepare_observer = reprepare_observer_backup;
1003 copy_tables->trx->trx_start = FALSE;
1004 copy_tables->trx->updated_in_this_trx = FALSE;
1005 DBUG_PRINT("info",("spider trx->updated_in_this_trx=FALSE"));
1006 my_printf_error(ER_SPIDER_UDF_CANT_OPEN_TABLE_NUM,
1007 ER_SPIDER_UDF_CANT_OPEN_TABLE_STR, MYF(0), table_list->db,
1008 table_list->table_name);
1009 goto error;
1010 }
1011 thd->m_reprepare_observer = reprepare_observer_backup;
1012 copy_tables->trx->trx_start = FALSE;
1013 copy_tables->trx->updated_in_this_trx = FALSE;
1014 DBUG_PRINT("info",("spider trx->updated_in_this_trx=FALSE"));
1015
1016 table = table_list->table;
1017 table_share = table->s;
1018 if (table_share->primary_key == MAX_KEY)
1019 {
1020 my_printf_error(ER_SPIDER_UDF_COPY_TABLE_NEED_PK_NUM,
1021 ER_SPIDER_UDF_COPY_TABLE_NEED_PK_STR, MYF(0),
1022 table_list->db, table_list->table_name);
1023 goto error;
1024 }
1025 key_info = &table->key_info[table_share->primary_key];
1026
1027 use_table_charset = spider_param_use_table_charset(
1028 copy_tables->use_table_charset);
1029 if (use_table_charset)
1030 copy_tables->access_charset = table_share->table_charset;
1031 else
1032 copy_tables->access_charset = system_charset_info;
1033
1034 bulk_insert_rows = spider_param_udf_ct_bulk_insert_rows(
1035 copy_tables->bulk_insert_rows);
1036 for (src_tbl_conn = copy_tables->table_conn[0]; src_tbl_conn;
1037 src_tbl_conn = src_tbl_conn->next)
1038 {
1039 select_ct = src_tbl_conn->copy_table;
1040 src_tbl_conn->share->access_charset = copy_tables->access_charset;
1041 select_ct->set_sql_charset(copy_tables->access_charset);
1042 if (
1043 select_ct->append_select_str() ||
1044 select_ct->append_table_columns(table_share)
1045 ) {
1046 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1047 goto error;
1048 }
1049
1050 if (
1051 select_ct->append_from_str() ||
1052 select_ct->append_table_name(0)
1053 ) {
1054 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1055 goto error;
1056 }
1057
1058 select_ct->set_sql_pos();
1059
1060 if (
1061 select_ct->append_key_order_str(key_info, 0, FALSE) ||
1062 select_ct->append_limit(0, bulk_insert_rows)
1063 ) {
1064 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1065 goto error;
1066 }
1067
1068 if (
1069 copy_tables->use_transaction &&
1070 select_ct->append_select_lock_str(SPIDER_LOCK_MODE_SHARED)
1071 ) {
1072 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1073 goto error;
1074 }
1075 }
1076
1077 for (dst_tbl_conn = copy_tables->table_conn[1]; dst_tbl_conn;
1078 dst_tbl_conn = dst_tbl_conn->next)
1079 {
1080 insert_ct = dst_tbl_conn->copy_table;
1081 dst_tbl_conn->share->access_charset = copy_tables->access_charset;
1082 insert_ct->set_sql_charset(copy_tables->access_charset);
1083 if (
1084 insert_ct->append_insert_str(SPIDER_DB_INSERT_IGNORE) ||
1085 insert_ct->append_into_str() ||
1086 insert_ct->append_table_name(0) ||
1087 insert_ct->append_open_paren_str() ||
1088 insert_ct->append_table_columns(table_share) ||
1089 insert_ct->append_values_str()
1090 ) {
1091 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1092 goto error;
1093 }
1094
1095 insert_ct->set_sql_pos();
1096 }
1097
1098 all_link_cnt =
1099 copy_tables->link_idx_count[0] + copy_tables->link_idx_count[1];
1100 if (
1101 !(tmp_sql = new spider_string[all_link_cnt]) ||
1102 !(spider = new ha_spider[all_link_cnt])
1103 ) {
1104 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1105 goto error;
1106 }
1107 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
1108 {
1109 spider[roop_count].conns = NULL;
1110 spider[roop_count].change_table_ptr(table, table_share);
1111 }
1112 for (roop_count = 0, table_conn = copy_tables->table_conn[0];
1113 table_conn; roop_count++, table_conn = table_conn->next)
1114 {
1115 tmp_spider = &spider[roop_count];
1116 if (!(tmp_spider->dbton_handler = (spider_db_handler **)
1117 spider_bulk_alloc_mem(spider_current_trx, 205,
1118 __func__, __FILE__, __LINE__, MYF(MY_WME | MY_ZEROFILL),
1119 &tmp_spider->dbton_handler,
1120 sizeof(spider_db_handler *) * SPIDER_DBTON_SIZE,
1121 NullS))
1122 ) {
1123 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1124 goto error;
1125 }
1126 tmp_spider->share = table_conn->share;
1127 tmp_spider->trx = copy_tables->trx;
1128/*
1129 if (spider_db_append_set_names(table_conn->share))
1130 {
1131 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1132 goto error_append_set_names;
1133 }
1134*/
1135 tmp_spider->conns = &table_conn->conn;
1136 tmp_sql[roop_count].init_calc_mem(122);
1137 tmp_sql[roop_count].set_charset(copy_tables->access_charset);
1138 tmp_spider->result_list.sqls = &tmp_sql[roop_count];
1139 tmp_spider->need_mons = &table_conn->need_mon;
1140 tmp_spider->lock_type = TL_READ;
1141 tmp_spider->conn_link_idx = &tmp_conn_link_idx;
1142 uint dbton_id = tmp_spider->share->use_dbton_ids[0];
1143 if (!(tmp_spider->dbton_handler[dbton_id] =
1144 spider_dbton[dbton_id].create_db_handler(tmp_spider,
1145 tmp_spider->share->dbton_share[dbton_id])))
1146 {
1147 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1148 goto error_create_dbton_handler;
1149 }
1150 if ((error_num = tmp_spider->dbton_handler[dbton_id]->init()))
1151 {
1152 goto error_init_dbton_handler;
1153 }
1154 table_conn->spider = tmp_spider;
1155 }
1156 for (table_conn = copy_tables->table_conn[1];
1157 table_conn; roop_count++, table_conn = table_conn->next)
1158 {
1159 tmp_spider = &spider[roop_count];
1160 if (!(tmp_spider->dbton_handler = (spider_db_handler **)
1161 spider_bulk_alloc_mem(spider_current_trx, 206,
1162 __func__, __FILE__, __LINE__, MYF(MY_WME | MY_ZEROFILL),
1163 &tmp_spider->dbton_handler,
1164 sizeof(spider_db_handler *) * SPIDER_DBTON_SIZE,
1165 NullS))
1166 ) {
1167 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1168 goto error;
1169 }
1170 tmp_spider->share = table_conn->share;
1171 tmp_spider->trx = copy_tables->trx;
1172/*
1173 if (spider_db_append_set_names(table_conn->share))
1174 {
1175 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1176 goto error_append_set_names;
1177 }
1178*/
1179 tmp_spider->conns = &table_conn->conn;
1180 tmp_sql[roop_count].init_calc_mem(201);
1181 tmp_sql[roop_count].set_charset(copy_tables->access_charset);
1182 tmp_spider->result_list.sqls = &tmp_sql[roop_count];
1183 tmp_spider->need_mons = &table_conn->need_mon;
1184 tmp_spider->lock_type = TL_WRITE;
1185 tmp_spider->conn_link_idx = &tmp_conn_link_idx;
1186 uint dbton_id = tmp_spider->share->use_dbton_ids[0];
1187 if (!(tmp_spider->dbton_handler[dbton_id] =
1188 spider_dbton[dbton_id].create_db_handler(tmp_spider,
1189 tmp_spider->share->dbton_share[dbton_id])))
1190 {
1191 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1192 goto error_create_dbton_handler;
1193 }
1194 if ((error_num = tmp_spider->dbton_handler[dbton_id]->init()))
1195 {
1196 goto error_init_dbton_handler;
1197 }
1198 table_conn->spider = tmp_spider;
1199 }
1200
1201 if ((error_num = spider_db_udf_copy_tables(copy_tables, spider, table,
1202 bulk_insert_rows)))
1203 goto error_db_udf_copy_tables;
1204
1205/*
1206 for (table_conn = copy_tables->table_conn[0];
1207 table_conn; table_conn = table_conn->next)
1208 spider_db_free_set_names(table_conn->share);
1209 for (table_conn = copy_tables->table_conn[1];
1210 table_conn; table_conn = table_conn->next)
1211 spider_db_free_set_names(table_conn->share);
1212*/
1213 if (table_list->table)
1214 {
1215#if MYSQL_VERSION_ID < 50500
1216 ha_autocommit_or_rollback(thd, 0);
1217#else
1218 (thd->is_error() ? trans_rollback_stmt(thd) : trans_commit_stmt(thd));
1219#endif
1220 close_thread_tables(thd);
1221 }
1222 if (spider)
1223 {
1224 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
1225 {
1226 if (spider[roop_count].share && spider[roop_count].dbton_handler)
1227 {
1228 uint dbton_id = spider[roop_count].share->use_dbton_ids[0];
1229 if (spider[roop_count].dbton_handler[dbton_id])
1230 delete spider[roop_count].dbton_handler[dbton_id];
1231 spider_free(spider_current_trx, spider[roop_count].dbton_handler,
1232 MYF(0));
1233 }
1234 }
1235 delete [] spider;
1236 }
1237 if (tmp_sql)
1238 delete [] tmp_sql;
1239 spider_udf_free_copy_tables_alloc(copy_tables);
1240
1241 DBUG_RETURN(1);
1242
1243error_db_udf_copy_tables:
1244error_create_dbton_handler:
1245error_init_dbton_handler:
1246/*
1247error_append_set_names:
1248*/
1249/*
1250 for (table_conn = copy_tables->table_conn[0];
1251 table_conn; table_conn = table_conn->next)
1252 spider_db_free_set_names(table_conn->share);
1253 for (table_conn = copy_tables->table_conn[1];
1254 table_conn; table_conn = table_conn->next)
1255 spider_db_free_set_names(table_conn->share);
1256*/
1257error:
1258 if (spider)
1259 {
1260 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
1261 {
1262 tmp_spider = &spider[roop_count];
1263 if (tmp_spider->conns)
1264 {
1265 tmp_conn = tmp_spider->conns[0];
1266 if (tmp_conn && tmp_conn->db_conn &&
1267 tmp_conn->db_conn->get_lock_table_hash_count()
1268 ) {
1269 tmp_conn->db_conn->reset_lock_table_hash();
1270 tmp_conn->table_lock = 0;
1271 }
1272 }
1273 }
1274 }
1275 if (table_list && table_list->table)
1276 {
1277#if MYSQL_VERSION_ID < 50500
1278 ha_autocommit_or_rollback(thd, 0);
1279#else
1280 (thd->is_error() ? trans_rollback_stmt(thd) : trans_commit_stmt(thd));
1281#endif
1282 close_thread_tables(thd);
1283 }
1284 if (spider)
1285 {
1286 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
1287 {
1288 tmp_spider = &spider[roop_count];
1289 if (tmp_spider->share && spider[roop_count].dbton_handler)
1290 {
1291 uint dbton_id = tmp_spider->share->use_dbton_ids[0];
1292 if (tmp_spider->dbton_handler[dbton_id])
1293 delete tmp_spider->dbton_handler[dbton_id];
1294 spider_free(spider_current_trx, spider[roop_count].dbton_handler,
1295 MYF(0));
1296 }
1297 }
1298 delete [] spider;
1299 }
1300 if (tmp_sql)
1301 {
1302 delete [] tmp_sql;
1303 }
1304 if (copy_tables)
1305 {
1306 spider_udf_free_copy_tables_alloc(copy_tables);
1307 }
1308 *error = 1;
1309 DBUG_RETURN(0);
1310}
1311
1312my_bool spider_copy_tables_init_body(
1313 UDF_INIT *initid,
1314 UDF_ARGS *args,
1315 char *message
1316) {
1317 DBUG_ENTER("spider_copy_tables_init_body");
1318 if (args->arg_count != 3 && args->arg_count != 4)
1319 {
1320 strcpy(message, "spider_copy_tables() requires 3 or 4 arguments");
1321 goto error;
1322 }
1323 if (
1324 args->arg_type[0] != STRING_RESULT ||
1325 args->arg_type[1] != STRING_RESULT ||
1326 args->arg_type[2] != STRING_RESULT ||
1327 (
1328 args->arg_count == 4 &&
1329 args->arg_type[3] != STRING_RESULT
1330 )
1331 ) {
1332 strcpy(message, "spider_copy_tables() requires string arguments");
1333 goto error;
1334 }
1335 DBUG_RETURN(FALSE);
1336
1337error:
1338 DBUG_RETURN(TRUE);
1339}
1340
1341void spider_copy_tables_deinit_body(
1342 UDF_INIT *initid
1343) {
1344 int error_num;
1345 THD *thd = current_thd;
1346 SPIDER_TRX *trx;
1347 DBUG_ENTER("spider_copy_tables_deinit_body");
1348 if (
1349 !thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) &&
1350 (trx = spider_get_trx(thd, TRUE, &error_num))
1351 )
1352 spider_copy_table_free_trx_conn(trx);
1353 DBUG_VOID_RETURN;
1354}
1355