You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1352 lines
42 KiB

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