1/* Copyright (C) 2008-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_partition.h"
28#include "sql_analyse.h"
29#include "sql_base.h"
30#include "tztime.h"
31#include "errmsg.h"
32#ifdef HANDLER_HAS_DIRECT_AGGREGATE
33#include "sql_select.h"
34#endif
35#endif
36#include "sql_common.h"
37#include <errmsg.h>
38#include "spd_err.h"
39#include "spd_param.h"
40#include "spd_db_include.h"
41#include "spd_include.h"
42#include "spd_sys_table.h"
43#include "ha_spider.h"
44#include "spd_db_conn.h"
45#include "spd_table.h"
46#include "spd_trx.h"
47#include "spd_conn.h"
48#include "spd_direct_sql.h"
49#include "spd_ping_table.h"
50#include "spd_copy_tables.h"
51#include "spd_malloc.h"
52
53extern handlerton *spider_hton_ptr;
54extern SPIDER_DBTON spider_dbton[SPIDER_DBTON_SIZE];
55
56#define SPIDER_SQL_COALESCE_STR "coalesce("
57#define SPIDER_SQL_COALESCE_LEN (sizeof(SPIDER_SQL_COALESCE_STR) - 1)
58#define SPIDER_SQL_HEX_STR "0x"
59#define SPIDER_SQL_HEX_LEN (sizeof(SPIDER_SQL_HEX_STR) - 1)
60#define SPIDER_SQL_SQL_FORCE_IDX_STR " force index("
61#define SPIDER_SQL_SQL_FORCE_IDX_LEN (sizeof(SPIDER_SQL_SQL_FORCE_IDX_STR) - 1)
62#define SPIDER_SQL_SQL_USE_IDX_STR " use index("
63#define SPIDER_SQL_SQL_USE_IDX_LEN (sizeof(SPIDER_SQL_SQL_USE_IDX_STR) - 1)
64#define SPIDER_SQL_SQL_IGNORE_IDX_STR " ignore index("
65#define SPIDER_SQL_SQL_IGNORE_IDX_LEN (sizeof(SPIDER_SQL_SQL_IGNORE_IDX_STR) - 1)
66
67#define SPIDER_SQL_SET_NAMES_STR "set names "
68#define SPIDER_SQL_SET_NAMES_LEN sizeof(SPIDER_SQL_SET_NAMES_STR) - 1
69
70#define SPIDER_SQL_PING_TABLE_STR "spider_ping_table("
71#define SPIDER_SQL_PING_TABLE_LEN (sizeof(SPIDER_SQL_PING_TABLE_STR) - 1)
72
73#ifdef SPIDER_HAS_HASH_VALUE_TYPE
74extern HASH spider_open_connections;
75#endif
76pthread_mutex_t spider_open_conn_mutex;
77const char spider_dig_upper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
78
79int spider_db_connect(
80 const SPIDER_SHARE *share,
81 SPIDER_CONN *conn,
82 int link_idx
83) {
84 int error_num, connect_retry_count;
85 THD* thd = current_thd;
86 longlong connect_retry_interval;
87 DBUG_ENTER("spider_db_connect");
88 DBUG_ASSERT(conn->conn_kind != SPIDER_CONN_KIND_MYSQL || conn->need_mon);
89 DBUG_PRINT("info",("spider link_idx=%d", link_idx));
90 DBUG_PRINT("info",("spider conn=%p", conn));
91
92 if (conn->connect_error)
93 {
94 time_t tmp_time = (time_t) time((time_t*) 0);
95 DBUG_PRINT("info",("spider diff=%f",
96 difftime(tmp_time, conn->connect_error_time)));
97 if (
98 (
99 conn->thd &&
100 conn->thd == conn->connect_error_thd &&
101 conn->thd->query_id == conn->connect_error_query_id
102 ) ||
103 (
104 difftime(tmp_time, conn->connect_error_time) <
105 spider_param_connect_error_interval()
106 )
107 ) {
108 DBUG_PRINT("info",("spider set same error"));
109 if (conn->connect_error_with_message)
110 my_message(conn->connect_error, conn->connect_error_msg, MYF(0));
111 DBUG_RETURN(conn->connect_error);
112 }
113 }
114
115 if (thd)
116 {
117 conn->connect_timeout = spider_param_connect_timeout(thd,
118 share->connect_timeouts[link_idx]);
119 conn->net_read_timeout = spider_param_net_read_timeout(thd,
120 share->net_read_timeouts[link_idx]);
121 conn->net_write_timeout = spider_param_net_write_timeout(thd,
122 share->net_write_timeouts[link_idx]);
123 connect_retry_interval = spider_param_connect_retry_interval(thd);
124 if (conn->disable_connect_retry)
125 connect_retry_count = 0;
126 else
127 connect_retry_count = spider_param_connect_retry_count(thd);
128 } else {
129 conn->connect_timeout = spider_param_connect_timeout(NULL,
130 share->connect_timeouts[link_idx]);
131 conn->net_read_timeout = spider_param_net_read_timeout(NULL,
132 share->net_read_timeouts[link_idx]);
133 conn->net_write_timeout = spider_param_net_write_timeout(NULL,
134 share->net_write_timeouts[link_idx]);
135 connect_retry_interval = spider_param_connect_retry_interval(NULL);
136 connect_retry_count = spider_param_connect_retry_count(NULL);
137 }
138 DBUG_PRINT("info",("spider connect_timeout=%u", conn->connect_timeout));
139 DBUG_PRINT("info",("spider net_read_timeout=%u", conn->net_read_timeout));
140 DBUG_PRINT("info",("spider net_write_timeout=%u", conn->net_write_timeout));
141
142#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
143 if (conn->conn_kind == SPIDER_CONN_KIND_MYSQL)
144 {
145#endif
146 if ((error_num = spider_reset_conn_setted_parameter(conn, thd)))
147 DBUG_RETURN(error_num);
148#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
149 }
150#endif
151
152 if (conn->dbton_id == SPIDER_DBTON_SIZE)
153 {
154#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
155 if (conn->conn_kind == SPIDER_CONN_KIND_MYSQL)
156 {
157#endif
158 my_printf_error(
159 ER_SPIDER_SQL_WRAPPER_IS_INVALID_NUM,
160 ER_SPIDER_SQL_WRAPPER_IS_INVALID_STR,
161 MYF(0), conn->tgt_wrapper);
162 DBUG_RETURN(ER_SPIDER_SQL_WRAPPER_IS_INVALID_NUM);
163#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
164 } else {
165 my_printf_error(
166 ER_SPIDER_NOSQL_WRAPPER_IS_INVALID_NUM,
167 ER_SPIDER_NOSQL_WRAPPER_IS_INVALID_STR,
168 MYF(0), conn->tgt_wrapper);
169 DBUG_RETURN(ER_SPIDER_NOSQL_WRAPPER_IS_INVALID_NUM);
170 }
171#endif
172 }
173
174 if ((error_num = conn->db_conn->connect(
175 share->tgt_hosts[link_idx],
176 share->tgt_usernames[link_idx],
177 share->tgt_passwords[link_idx],
178 share->tgt_ports[link_idx],
179 share->tgt_sockets[link_idx],
180 share->server_names[link_idx],
181 connect_retry_count, connect_retry_interval)))
182 {
183 if (conn->thd)
184 {
185 conn->connect_error_thd = conn->thd;
186 conn->connect_error_query_id = conn->thd->query_id;
187 conn->connect_error_time = (time_t) time((time_t*) 0);
188 conn->connect_error = error_num;
189 if ((conn->connect_error_with_message = thd->is_error()))
190 strmov(conn->connect_error_msg, spider_stmt_da_message(thd));
191 }
192 DBUG_RETURN(error_num);
193 }
194 conn->connect_error = 0;
195 conn->opened_handlers = 0;
196 conn->db_conn->reset_opened_handler();
197 ++conn->connection_id;
198 DBUG_RETURN(0);
199}
200
201int spider_db_ping_internal(
202 SPIDER_SHARE *share,
203 SPIDER_CONN *conn,
204 int all_link_idx,
205 int *need_mon
206) {
207 int error_num;
208 DBUG_ENTER("spider_db_ping_internal");
209 if (!conn->mta_conn_mutex_lock_already)
210 {
211 pthread_mutex_lock(&conn->mta_conn_mutex);
212 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
213 conn->need_mon = need_mon;
214 }
215 DBUG_ASSERT(conn->mta_conn_mutex_file_pos.file_name);
216 if (conn->server_lost || conn->queued_connect)
217 {
218 if ((error_num = spider_db_connect(share, conn, all_link_idx)))
219 {
220 if (!conn->mta_conn_mutex_unlock_later)
221 {
222 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
223 pthread_mutex_unlock(&conn->mta_conn_mutex);
224 }
225 DBUG_RETURN(error_num);
226 }
227 conn->server_lost = FALSE;
228 conn->queued_connect = FALSE;
229 }
230 if ((error_num = conn->db_conn->ping()))
231 {
232 spider_db_disconnect(conn);
233 if ((error_num = spider_db_connect(share, conn, all_link_idx)))
234 {
235 DBUG_PRINT("info", ("spider conn=%p SERVER_LOST", conn));
236 conn->server_lost = TRUE;
237 if (!conn->mta_conn_mutex_unlock_later)
238 {
239 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
240 pthread_mutex_unlock(&conn->mta_conn_mutex);
241 }
242 DBUG_RETURN(error_num);
243 }
244 if((error_num = conn->db_conn->ping()))
245 {
246 spider_db_disconnect(conn);
247 DBUG_PRINT("info", ("spider conn=%p SERVER_LOST", conn));
248 conn->server_lost = TRUE;
249 if (!conn->mta_conn_mutex_unlock_later)
250 {
251 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
252 pthread_mutex_unlock(&conn->mta_conn_mutex);
253 }
254 DBUG_RETURN(error_num);
255 }
256 }
257 conn->ping_time = (time_t) time((time_t*) 0);
258 if (!conn->mta_conn_mutex_unlock_later)
259 {
260 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
261 pthread_mutex_unlock(&conn->mta_conn_mutex);
262 }
263 DBUG_RETURN(0);
264}
265
266int spider_db_ping(
267 ha_spider *spider,
268 SPIDER_CONN *conn,
269 int link_idx
270) {
271 DBUG_ENTER("spider_db_ping");
272#ifndef DBUG_OFF
273 if (spider->trx->thd)
274 DBUG_PRINT("info", ("spider thd->query_id is %lld",
275 spider->trx->thd->query_id));
276#endif
277 DBUG_RETURN(spider_db_ping_internal(spider->share, conn,
278 spider->conn_link_idx[link_idx], &spider->need_mons[link_idx]));
279}
280
281void spider_db_disconnect(
282 SPIDER_CONN *conn
283) {
284 DBUG_ENTER("spider_db_disconnect");
285 DBUG_PRINT("info",("spider conn=%p", conn));
286 DBUG_PRINT("info",("spider conn->conn_kind=%u", conn->conn_kind));
287 if (conn->db_conn->is_connected())
288 {
289 conn->db_conn->disconnect();
290 }
291 DBUG_VOID_RETURN;
292}
293
294int spider_db_conn_queue_action(
295 SPIDER_CONN *conn
296) {
297 int error_num;
298 char sql_buf[MAX_FIELD_WIDTH * 2];
299 spider_string sql_str(sql_buf, sizeof(sql_buf), system_charset_info);
300 DBUG_ENTER("spider_db_conn_queue_action");
301 DBUG_PRINT("info", ("spider conn=%p", conn));
302 sql_str.init_calc_mem(106);
303 sql_str.length(0);
304 if (conn->queued_connect)
305 {
306 if ((error_num = spider_db_connect(conn->queued_connect_share, conn,
307 conn->queued_connect_link_idx)))
308 {
309 DBUG_PRINT("info", ("spider conn=%p SERVER_LOST", conn));
310 conn->server_lost = TRUE;
311 DBUG_RETURN(error_num);
312 }
313 conn->server_lost = FALSE;
314 conn->queued_connect = FALSE;
315 }
316
317#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
318 if (conn->conn_kind == SPIDER_CONN_KIND_MYSQL)
319 {
320#endif
321 if (conn->queued_ping)
322 {
323 if ((error_num = spider_db_ping(conn->queued_ping_spider, conn,
324 conn->queued_ping_link_idx)))
325 DBUG_RETURN(error_num);
326 conn->queued_ping = FALSE;
327 }
328
329 if (conn->server_lost)
330 {
331 DBUG_PRINT("info", ("spider no reconnect queue"));
332 DBUG_RETURN(CR_SERVER_GONE_ERROR);
333 }
334
335 if (conn->queued_net_timeout)
336 {
337 conn->db_conn->set_net_timeout();
338 conn->queued_net_timeout = FALSE;
339 }
340 if (
341 (
342 conn->queued_trx_isolation &&
343 !conn->queued_semi_trx_isolation &&
344 conn->queued_trx_isolation_val != conn->trx_isolation &&
345 conn->db_conn->set_trx_isolation_in_bulk_sql() &&
346 (error_num = spider_dbton[conn->dbton_id].db_util->
347 append_trx_isolation(&sql_str, conn->queued_trx_isolation_val))
348 ) ||
349 (
350 conn->queued_semi_trx_isolation &&
351 conn->queued_semi_trx_isolation_val != conn->trx_isolation &&
352 conn->db_conn->set_trx_isolation_in_bulk_sql() &&
353 (error_num = spider_dbton[conn->dbton_id].db_util->
354 append_trx_isolation(&sql_str, conn->queued_semi_trx_isolation_val))
355 ) ||
356 (
357 conn->queued_autocommit &&
358 (
359 (conn->queued_autocommit_val && conn->autocommit != 1) ||
360 (!conn->queued_autocommit_val && conn->autocommit != 0)
361 ) &&
362 conn->db_conn->set_autocommit_in_bulk_sql() &&
363 (error_num = spider_dbton[conn->dbton_id].db_util->
364 append_autocommit(&sql_str, conn->queued_autocommit_val))
365 ) ||
366 (
367 conn->queued_sql_log_off &&
368 (
369 (conn->queued_sql_log_off_val && conn->sql_log_off != 1) ||
370 (!conn->queued_sql_log_off_val && conn->sql_log_off != 0)
371 ) &&
372 conn->db_conn->set_sql_log_off_in_bulk_sql() &&
373 (error_num = spider_dbton[conn->dbton_id].db_util->
374 append_sql_log_off(&sql_str, conn->queued_sql_log_off_val))
375 ) ||
376 (
377 conn->queued_time_zone &&
378 conn->queued_time_zone_val != conn->time_zone &&
379 conn->db_conn->set_time_zone_in_bulk_sql() &&
380 (error_num = spider_dbton[conn->dbton_id].db_util->
381 append_time_zone(&sql_str, conn->queued_time_zone_val))
382 ) ||
383 (
384 conn->queued_trx_start &&
385 conn->db_conn->trx_start_in_bulk_sql() &&
386 (error_num = spider_dbton[conn->dbton_id].db_util->
387 append_start_transaction(&sql_str))
388 ) ||
389 (
390 conn->queued_xa_start &&
391 conn->db_conn->xa_start_in_bulk_sql() &&
392 (error_num = spider_dbton[conn->dbton_id].db_util->
393 append_xa_start(&sql_str, conn->queued_xa_start_xid))
394 )
395 )
396 DBUG_RETURN(error_num);
397 if (sql_str.length())
398 {
399 if ((error_num = conn->db_conn->exec_query(sql_str.ptr(),
400 sql_str.length(), -1)))
401 DBUG_RETURN(error_num);
402 spider_db_result *result;
403 do {
404 st_spider_db_request_key request_key;
405 request_key.spider_thread_id = 1;
406 request_key.query_id = 1;
407 request_key.handler = NULL;
408 request_key.request_id = 1;
409 request_key.next = NULL;
410 if ((result = conn->db_conn->store_result(NULL, &request_key,
411 &error_num)))
412 {
413 result->free_result();
414 delete result;
415 } else if ((error_num = conn->db_conn->get_errno()))
416 {
417 break;
418 }
419 } while (!(error_num = conn->db_conn->next_result()));
420 if (error_num > 0)
421 DBUG_RETURN(error_num);
422 }
423
424 if (
425 conn->queued_autocommit &&
426 (
427 (conn->queued_autocommit_val && conn->autocommit != 1) ||
428 (!conn->queued_autocommit_val && conn->autocommit != 0)
429 ) &&
430 !conn->db_conn->set_autocommit_in_bulk_sql() &&
431 (error_num = spider_dbton[conn->dbton_id].db_util->
432 append_autocommit(&sql_str, conn->queued_autocommit_val))
433 ) {
434 DBUG_RETURN(error_num);
435 }
436 if (
437 conn->queued_sql_log_off &&
438 (
439 (conn->queued_sql_log_off_val && conn->sql_log_off != 1) ||
440 (!conn->queued_sql_log_off_val && conn->sql_log_off != 0)
441 ) &&
442 !conn->db_conn->set_sql_log_off_in_bulk_sql() &&
443 (error_num = spider_dbton[conn->dbton_id].db_util->
444 append_sql_log_off(&sql_str, conn->queued_sql_log_off_val))
445 ) {
446 DBUG_RETURN(error_num);
447 }
448 if (
449 conn->queued_time_zone &&
450 conn->queued_time_zone_val != conn->time_zone &&
451 !conn->db_conn->set_time_zone_in_bulk_sql() &&
452 (error_num = spider_dbton[conn->dbton_id].db_util->
453 append_time_zone(&sql_str, conn->queued_time_zone_val))
454 ) {
455 DBUG_RETURN(error_num);
456 }
457 if (
458 conn->queued_trx_isolation &&
459 !conn->queued_semi_trx_isolation &&
460 conn->queued_trx_isolation_val != conn->trx_isolation &&
461 !conn->db_conn->set_trx_isolation_in_bulk_sql() &&
462 (error_num = conn->db_conn->set_trx_isolation(
463 conn->queued_trx_isolation_val, (int *) conn->need_mon))
464 ) {
465 DBUG_RETURN(error_num);
466 }
467 if (
468 conn->queued_semi_trx_isolation &&
469 conn->queued_semi_trx_isolation_val != conn->trx_isolation &&
470 !conn->db_conn->set_trx_isolation_in_bulk_sql() &&
471 (error_num = conn->db_conn->set_trx_isolation(
472 conn->queued_semi_trx_isolation_val, (int *) conn->need_mon))
473 ) {
474 DBUG_RETURN(error_num);
475 }
476 if (
477 conn->queued_trx_start &&
478 !conn->db_conn->trx_start_in_bulk_sql() &&
479 (error_num = conn->db_conn->
480 start_transaction((int *) conn->need_mon))
481 ) {
482 DBUG_RETURN(error_num);
483 }
484 if (
485 conn->queued_xa_start &&
486 !conn->db_conn->xa_start_in_bulk_sql() &&
487 (error_num = conn->db_conn->
488 xa_start(conn->queued_xa_start_xid, (int *) conn->need_mon))
489 ) {
490 DBUG_RETURN(error_num);
491 }
492
493 if (
494 conn->queued_trx_isolation &&
495 !conn->queued_semi_trx_isolation &&
496 conn->queued_trx_isolation_val != conn->trx_isolation
497 ) {
498 conn->trx_isolation = conn->queued_trx_isolation_val;
499 DBUG_PRINT("info", ("spider conn->trx_isolation=%d",
500 conn->trx_isolation));
501 }
502
503 if (
504 conn->queued_semi_trx_isolation &&
505 conn->queued_semi_trx_isolation_val != conn->trx_isolation
506 ) {
507 conn->semi_trx_isolation = conn->queued_semi_trx_isolation_val;
508 DBUG_PRINT("info", ("spider conn->semi_trx_isolation=%d",
509 conn->semi_trx_isolation));
510 conn->trx_isolation = thd_tx_isolation(conn->thd);
511 DBUG_PRINT("info", ("spider conn->trx_isolation=%d",
512 conn->trx_isolation));
513 }
514
515 if (conn->queued_autocommit)
516 {
517 if (conn->queued_autocommit_val && conn->autocommit != 1)
518 {
519 conn->autocommit = 1;
520 } else if (!conn->queued_autocommit_val && conn->autocommit != 0)
521 {
522 conn->autocommit = 0;
523 }
524 DBUG_PRINT("info", ("spider conn->autocommit=%d",
525 conn->autocommit));
526 }
527
528 if (conn->queued_sql_log_off)
529 {
530 if (conn->queued_sql_log_off_val && conn->sql_log_off != 1)
531 {
532 conn->sql_log_off = 1;
533 } else if (!conn->queued_sql_log_off_val && conn->sql_log_off != 0)
534 {
535 conn->sql_log_off = 0;
536 }
537 DBUG_PRINT("info", ("spider conn->sql_log_off=%d",
538 conn->sql_log_off));
539 }
540
541 if (
542 conn->queued_time_zone &&
543 conn->queued_time_zone_val != conn->time_zone
544 ) {
545 conn->time_zone = conn->queued_time_zone_val;
546 DBUG_PRINT("info", ("spider conn->time_zone=%p",
547 conn->time_zone));
548 }
549 spider_conn_clear_queue(conn);
550#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
551 } else if (conn->server_lost)
552 {
553 DBUG_PRINT("info", ("spider no connect queue"));
554 DBUG_RETURN(CR_SERVER_GONE_ERROR);
555 }
556#endif
557 DBUG_RETURN(0);
558}
559
560int spider_db_before_query(
561 SPIDER_CONN *conn,
562 int *need_mon
563) {
564 int error_num;
565 bool tmp_mta_conn_mutex_lock_already;
566 DBUG_ENTER("spider_db_before_query");
567 DBUG_ASSERT(need_mon);
568#ifndef WITHOUT_SPIDER_BG_SEARCH
569 if (conn->bg_search)
570 spider_bg_conn_break(conn, NULL);
571#endif
572 conn->in_before_query = TRUE;
573 if (!conn->mta_conn_mutex_lock_already)
574 {
575 pthread_mutex_lock(&conn->mta_conn_mutex);
576 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
577 conn->need_mon = need_mon;
578 }
579 DBUG_ASSERT(conn->mta_conn_mutex_file_pos.file_name);
580 tmp_mta_conn_mutex_lock_already = conn->mta_conn_mutex_lock_already;
581 conn->mta_conn_mutex_lock_already = TRUE;
582 if ((error_num = spider_db_conn_queue_action(conn)))
583 {
584 conn->in_before_query = FALSE;
585 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
586 DBUG_RETURN(error_num);
587 }
588 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
589 if (conn->server_lost)
590 {
591 conn->in_before_query = FALSE;
592 DBUG_RETURN(CR_SERVER_GONE_ERROR);
593 }
594 DBUG_PRINT("info", ("spider conn[%p]->quick_target=%p",
595 conn, conn->quick_target));
596 if (conn->quick_target)
597 {
598 bool tmp_mta_conn_mutex_unlock_later;
599 ha_spider *spider = (ha_spider*) conn->quick_target;
600 SPIDER_RESULT_LIST *result_list = &spider->result_list;
601 DBUG_PRINT("info", ("spider result_list->quick_mode=%d",
602 result_list->quick_mode));
603 if (result_list->quick_mode == 2)
604 {
605 result_list->quick_phase = 1;
606 spider->connection_ids[conn->link_idx] = conn->connection_id;
607 tmp_mta_conn_mutex_unlock_later = conn->mta_conn_mutex_unlock_later;
608 conn->mta_conn_mutex_unlock_later = TRUE;
609 while (conn->quick_target)
610 {
611 if (
612 (error_num = spider_db_store_result(spider, conn->link_idx,
613 result_list->table)) &&
614 error_num != HA_ERR_END_OF_FILE
615 ) {
616 conn->mta_conn_mutex_unlock_later = tmp_mta_conn_mutex_unlock_later;
617 conn->in_before_query = FALSE;
618 DBUG_RETURN(error_num);
619 }
620 }
621 conn->mta_conn_mutex_unlock_later = tmp_mta_conn_mutex_unlock_later;
622 result_list->quick_phase = 2;
623 } else {
624 result_list->bgs_current->result->free_result();
625 delete result_list->bgs_current->result;
626 result_list->bgs_current->result = NULL;
627 DBUG_PRINT("info", ("spider conn[%p]->quick_target=NULL", conn));
628 conn->quick_target = NULL;
629 spider->quick_targets[conn->link_idx] = NULL;
630 }
631 }
632 conn->in_before_query = FALSE;
633 DBUG_RETURN(0);
634}
635
636int spider_db_query(
637 SPIDER_CONN *conn,
638 const char *query,
639 uint length,
640 int quick_mode,
641 int *need_mon
642) {
643 int error_num;
644 DBUG_ENTER("spider_db_query");
645#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
646 if (conn->conn_kind == SPIDER_CONN_KIND_MYSQL)
647 {
648#endif
649 DBUG_PRINT("info", ("spider conn->db_conn %p", conn->db_conn));
650 if (
651 !conn->in_before_query &&
652 (error_num = spider_db_before_query(conn, need_mon))
653 )
654 DBUG_RETURN(error_num);
655#ifndef DBUG_OFF
656 spider_string tmp_query_str(sizeof(char) * (length + 1));
657 tmp_query_str.init_calc_mem(107);
658 char *tmp_query = (char *) tmp_query_str.c_ptr_safe();
659 memcpy(tmp_query, query, length);
660 tmp_query[length] = '\0';
661 query = (const char *) tmp_query;
662 DBUG_PRINT("info", ("spider query=%s", query));
663 DBUG_PRINT("info", ("spider length=%u", length));
664#endif
665 if ((error_num = conn->db_conn->exec_query(query, length, quick_mode)))
666 DBUG_RETURN(error_num);
667 DBUG_RETURN(0);
668#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
669 } else {
670 if (conn->queued_net_timeout)
671 {
672 if (conn->db_conn->set_net_timeout())
673 DBUG_RETURN(ER_SPIDER_HS_NUM);
674 conn->queued_net_timeout = FALSE;
675 }
676 DBUG_RETURN(conn->db_conn->exec_query(NULL, 0, quick_mode));
677 }
678#endif
679}
680
681int spider_db_errorno(
682 SPIDER_CONN *conn
683) {
684 int error_num;
685 DBUG_ENTER("spider_db_errorno");
686 DBUG_ASSERT(conn->need_mon);
687#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
688 if (conn->conn_kind == SPIDER_CONN_KIND_MYSQL)
689 {
690#endif
691 if (conn->server_lost)
692 {
693 *conn->need_mon = ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM;
694 if (!current_thd->is_error())
695 {
696 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
697 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
698 }
699 if (!conn->mta_conn_mutex_unlock_later)
700 {
701 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
702 pthread_mutex_unlock(&conn->mta_conn_mutex);
703 }
704 DBUG_RETURN(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM);
705 }
706 if ((error_num = conn->db_conn->get_errno()))
707 {
708 DBUG_PRINT("info",("spider error_num = %d", error_num));
709 if (conn->db_conn->is_server_gone_error(error_num))
710 {
711 spider_db_disconnect(conn);
712 DBUG_PRINT("info", ("spider conn=%p SERVER_LOST", conn));
713 conn->server_lost = TRUE;
714 if (conn->disable_reconnect)
715 {
716 *conn->need_mon = ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM;
717 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
718 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
719 }
720 if (!conn->mta_conn_mutex_unlock_later)
721 {
722 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
723 pthread_mutex_unlock(&conn->mta_conn_mutex);
724 }
725 DBUG_RETURN(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM);
726 } else if (
727 conn->ignore_dup_key &&
728 conn->db_conn->is_dup_entry_error(error_num)
729 ) {
730 conn->error_str = (char*) conn->db_conn->get_error();
731 conn->error_length = strlen(conn->error_str);
732 if (!conn->mta_conn_mutex_unlock_later)
733 {
734 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
735 pthread_mutex_unlock(&conn->mta_conn_mutex);
736 }
737 DBUG_RETURN(HA_ERR_FOUND_DUPP_KEY);
738 } else if (
739 conn->db_conn->is_xa_nota_error(error_num) &&
740 current_thd &&
741 spider_param_force_commit(current_thd) == 1
742 ) {
743 push_warning(current_thd, SPIDER_WARN_LEVEL_WARN,
744 error_num, conn->db_conn->get_error());
745 if (spider_param_log_result_errors() >= 3)
746 {
747 time_t cur_time = (time_t) time((time_t*) 0);
748 struct tm lt;
749 struct tm *l_time = localtime_r(&cur_time, &lt);
750 fprintf(stderr, "%04d%02d%02d %02d:%02d:%02d [WARN SPIDER RESULT] "
751 "to %lld: %d %s\n",
752 l_time->tm_year + 1900, l_time->tm_mon + 1, l_time->tm_mday,
753 l_time->tm_hour, l_time->tm_min, l_time->tm_sec,
754 (long long int) current_thd->thread_id, error_num,
755 conn->db_conn->get_error());
756 }
757 if (!conn->mta_conn_mutex_unlock_later)
758 {
759 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
760 pthread_mutex_unlock(&conn->mta_conn_mutex);
761 }
762 DBUG_RETURN(error_num);
763 }
764 *conn->need_mon = error_num;
765 my_message(error_num, conn->db_conn->get_error(), MYF(0));
766 if (spider_param_log_result_errors() >= 1)
767 {
768 time_t cur_time = (time_t) time((time_t*) 0);
769 struct tm lt;
770 struct tm *l_time = localtime_r(&cur_time, &lt);
771 fprintf(stderr, "%04d%02d%02d %02d:%02d:%02d [ERROR SPIDER RESULT] "
772 "to %lld: %d %s\n",
773 l_time->tm_year + 1900, l_time->tm_mon + 1, l_time->tm_mday,
774 l_time->tm_hour, l_time->tm_min, l_time->tm_sec,
775 (long long int) current_thd->thread_id, error_num,
776 conn->db_conn->get_error());
777 }
778 if (!conn->mta_conn_mutex_unlock_later)
779 {
780 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
781 pthread_mutex_unlock(&conn->mta_conn_mutex);
782 }
783 DBUG_RETURN(error_num);
784 }
785#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
786 } else {
787 if (conn->db_conn->is_server_gone_error(0))
788 {
789 my_printf_error(ER_SPIDER_HS_NUM, ER_SPIDER_HS_STR, MYF(0),
790 conn->db_conn->get_errno(), conn->db_conn->get_error());
791 *conn->need_mon = ER_SPIDER_HS_NUM;
792 DBUG_PRINT("info", ("spider conn=%p SERVER_LOST", conn));
793 conn->server_lost = TRUE;
794 if (!conn->mta_conn_mutex_unlock_later)
795 {
796 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
797 pthread_mutex_unlock(&conn->mta_conn_mutex);
798 }
799 DBUG_RETURN(ER_SPIDER_HS_NUM);
800 } else if (conn->db_conn->is_dup_entry_error(0))
801 {
802 *conn->need_mon = 0;
803 if (!conn->mta_conn_mutex_unlock_later)
804 {
805 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
806 pthread_mutex_unlock(&conn->mta_conn_mutex);
807 }
808 DBUG_RETURN(HA_ERR_FOUND_DUPP_KEY);
809 }
810 my_printf_error(ER_SPIDER_HS_NUM, ER_SPIDER_HS_STR, MYF(0),
811 conn->db_conn->get_errno(), conn->db_conn->get_error());
812 if (spider_param_log_result_errors() >= 1)
813 {
814 time_t cur_time = (time_t) time((time_t*) 0);
815 struct tm lt;
816 struct tm *l_time = localtime_r(&cur_time, &lt);
817 fprintf(stderr, "%04d%02d%02d %02d:%02d:%02d [ERROR SPIDER RESULT] "
818 "to %ld: %d %s\n",
819 l_time->tm_year + 1900, l_time->tm_mon + 1, l_time->tm_mday,
820 l_time->tm_hour, l_time->tm_min, l_time->tm_sec,
821 (ulong) current_thd->thread_id, conn->db_conn->get_errno(),
822 conn->db_conn->get_error());
823 }
824 *conn->need_mon = ER_SPIDER_HS_NUM;
825 if (!conn->mta_conn_mutex_unlock_later)
826 {
827 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
828 pthread_mutex_unlock(&conn->mta_conn_mutex);
829 }
830 DBUG_RETURN(ER_SPIDER_HS_NUM);
831 }
832#endif
833 if (!conn->mta_conn_mutex_unlock_later)
834 {
835 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
836 pthread_mutex_unlock(&conn->mta_conn_mutex);
837 }
838 DBUG_RETURN(0);
839}
840
841int spider_db_set_trx_isolation(
842 SPIDER_CONN *conn,
843 int trx_isolation,
844 int *need_mon
845) {
846 DBUG_ENTER("spider_db_set_trx_isolation");
847 DBUG_RETURN(conn->db_conn->set_trx_isolation(trx_isolation, need_mon));
848}
849
850int spider_db_set_names_internal(
851 SPIDER_TRX *trx,
852 SPIDER_SHARE *share,
853 SPIDER_CONN *conn,
854 int all_link_idx,
855 int *need_mon
856) {
857 bool tmp_mta_conn_mutex_lock_already;
858 DBUG_ENTER("spider_db_set_names_internal");
859#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
860 if (conn->conn_kind == SPIDER_CONN_KIND_MYSQL)
861 {
862#endif
863 if (!conn->mta_conn_mutex_lock_already)
864 {
865 pthread_mutex_lock(&conn->mta_conn_mutex);
866 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
867 conn->need_mon = need_mon;
868 }
869 DBUG_ASSERT(conn->mta_conn_mutex_file_pos.file_name);
870 if (
871 !conn->access_charset ||
872 share->access_charset->cset != conn->access_charset->cset
873 ) {
874 tmp_mta_conn_mutex_lock_already = conn->mta_conn_mutex_lock_already;
875 conn->mta_conn_mutex_lock_already = TRUE;
876 if (
877 spider_db_before_query(conn, need_mon) ||
878 conn->db_conn->set_character_set(share->access_charset->csname)
879 ) {
880 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
881 DBUG_RETURN(spider_db_errorno(conn));
882 }
883 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
884 conn->access_charset = share->access_charset;
885 }
886 if (
887 spider_param_use_default_database(trx->thd) &&
888 (
889 !conn->default_database.length() ||
890 conn->default_database.length() !=
891 share->tgt_dbs_lengths[all_link_idx] ||
892 memcmp(share->tgt_dbs[all_link_idx], conn->default_database.ptr(),
893 share->tgt_dbs_lengths[all_link_idx])
894 )
895 ) {
896 DBUG_PRINT("info",("spider all_link_idx=%d db=%s", all_link_idx,
897 share->tgt_dbs[all_link_idx]));
898 tmp_mta_conn_mutex_lock_already = conn->mta_conn_mutex_lock_already;
899 conn->mta_conn_mutex_lock_already = TRUE;
900 if (
901 spider_db_before_query(conn, need_mon) ||
902 conn->db_conn->select_db(share->tgt_dbs[all_link_idx])
903 ) {
904 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
905 DBUG_RETURN(spider_db_errorno(conn));
906 }
907 conn->default_database.length(0);
908 if (conn->default_database.reserve(
909 share->tgt_dbs_lengths[all_link_idx] + 1))
910 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
911 conn->default_database.q_append(share->tgt_dbs[all_link_idx],
912 share->tgt_dbs_lengths[all_link_idx] + 1);
913 conn->default_database.length(share->tgt_dbs_lengths[all_link_idx]);
914 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
915 }
916 if (!conn->mta_conn_mutex_unlock_later)
917 {
918 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
919 pthread_mutex_unlock(&conn->mta_conn_mutex);
920 }
921#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
922 }
923#endif
924 DBUG_RETURN(0);
925}
926
927int spider_db_set_names(
928 ha_spider *spider,
929 SPIDER_CONN *conn,
930 int link_idx
931) {
932 DBUG_ENTER("spider_db_set_names");
933 DBUG_RETURN(spider_db_set_names_internal(spider->trx, spider->share, conn,
934 spider->conn_link_idx[link_idx], &spider->need_mons[link_idx]));
935}
936
937int spider_db_query_with_set_names(
938 ulong sql_type,
939 ha_spider *spider,
940 SPIDER_CONN *conn,
941 int link_idx
942) {
943 int error_num;
944 SPIDER_SHARE *share = spider->share;
945 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
946 DBUG_ENTER("spider_db_query_with_set_names");
947
948/*
949 pthread_mutex_lock(&conn->mta_conn_mutex);
950 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
951*/
952 conn->need_mon = &spider->need_mons[link_idx];
953 conn->mta_conn_mutex_lock_already = TRUE;
954 conn->mta_conn_mutex_unlock_later = TRUE;
955 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
956 {
957 conn->mta_conn_mutex_lock_already = FALSE;
958 conn->mta_conn_mutex_unlock_later = FALSE;
959 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
960 pthread_mutex_unlock(&conn->mta_conn_mutex);
961 if (
962 share->monitoring_kind[link_idx] &&
963 spider->need_mons[link_idx]
964 ) {
965 error_num = spider_ping_table_mon_from_table(
966 spider->trx,
967 spider->trx->thd,
968 share,
969 link_idx,
970 (uint32) share->monitoring_sid[link_idx],
971 share->table_name,
972 share->table_name_length,
973 spider->conn_link_idx[link_idx],
974 NULL,
975 0,
976 share->monitoring_kind[link_idx],
977 share->monitoring_limit[link_idx],
978 share->monitoring_flag[link_idx],
979 TRUE
980 );
981 }
982 DBUG_RETURN(error_num);
983 }
984 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
985 share);
986 if (dbton_hdl->execute_sql(
987 sql_type,
988 conn,
989 -1,
990 &spider->need_mons[link_idx])
991 ) {
992 conn->mta_conn_mutex_lock_already = FALSE;
993 conn->mta_conn_mutex_unlock_later = FALSE;
994 error_num = spider_db_errorno(conn);
995 if (
996 share->monitoring_kind[link_idx] &&
997 spider->need_mons[link_idx]
998 ) {
999 error_num = spider_ping_table_mon_from_table(
1000 spider->trx,
1001 spider->trx->thd,
1002 share,
1003 link_idx,
1004 (uint32) share->monitoring_sid[link_idx],
1005 share->table_name,
1006 share->table_name_length,
1007 spider->conn_link_idx[link_idx],
1008 NULL,
1009 0,
1010 share->monitoring_kind[link_idx],
1011 share->monitoring_limit[link_idx],
1012 share->monitoring_flag[link_idx],
1013 TRUE
1014 );
1015 }
1016 DBUG_RETURN(error_num);
1017 }
1018 conn->mta_conn_mutex_lock_already = FALSE;
1019 conn->mta_conn_mutex_unlock_later = FALSE;
1020 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
1021 pthread_mutex_unlock(&conn->mta_conn_mutex);
1022 DBUG_RETURN(0);
1023}
1024
1025int spider_db_query_for_bulk_update(
1026 ha_spider *spider,
1027 SPIDER_CONN *conn,
1028 int link_idx,
1029 ha_rows *dup_key_found
1030) {
1031 int error_num;
1032 SPIDER_SHARE *share = spider->share;
1033 DBUG_ENTER("spider_db_query_for_bulk_update");
1034
1035/*
1036 pthread_mutex_lock(&conn->mta_conn_mutex);
1037 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
1038*/
1039 conn->need_mon = &spider->need_mons[link_idx];
1040 conn->mta_conn_mutex_lock_already = TRUE;
1041 conn->mta_conn_mutex_unlock_later = TRUE;
1042 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
1043 {
1044 conn->mta_conn_mutex_lock_already = FALSE;
1045 conn->mta_conn_mutex_unlock_later = FALSE;
1046 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
1047 pthread_mutex_unlock(&conn->mta_conn_mutex);
1048 if (
1049 share->monitoring_kind[link_idx] &&
1050 spider->need_mons[link_idx]
1051 ) {
1052 error_num = spider_ping_table_mon_from_table(
1053 spider->trx,
1054 spider->trx->thd,
1055 share,
1056 link_idx,
1057 (uint32) share->monitoring_sid[link_idx],
1058 share->table_name,
1059 share->table_name_length,
1060 spider->conn_link_idx[link_idx],
1061 NULL,
1062 0,
1063 share->monitoring_kind[link_idx],
1064 share->monitoring_limit[link_idx],
1065 share->monitoring_flag[link_idx],
1066 TRUE
1067 );
1068 }
1069 DBUG_RETURN(error_num);
1070 }
1071 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
1072 share);
1073 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
1074 if (dbton_hdl->execute_sql(
1075 SPIDER_SQL_TYPE_BULK_UPDATE_SQL,
1076 conn,
1077 -1,
1078 &spider->need_mons[link_idx])
1079 ) {
1080 conn->mta_conn_mutex_lock_already = FALSE;
1081 conn->mta_conn_mutex_unlock_later = FALSE;
1082 error_num = spider_db_errorno(conn);
1083 if (
1084 error_num != ER_DUP_ENTRY &&
1085 error_num != ER_DUP_KEY &&
1086 error_num != HA_ERR_FOUND_DUPP_KEY &&
1087 share->monitoring_kind[link_idx] &&
1088 spider->need_mons[link_idx]
1089 ) {
1090 error_num = spider_ping_table_mon_from_table(
1091 spider->trx,
1092 spider->trx->thd,
1093 share,
1094 link_idx,
1095 (uint32) share->monitoring_sid[link_idx],
1096 share->table_name,
1097 share->table_name_length,
1098 spider->conn_link_idx[link_idx],
1099 NULL,
1100 0,
1101 share->monitoring_kind[link_idx],
1102 share->monitoring_limit[link_idx],
1103 share->monitoring_flag[link_idx],
1104 TRUE
1105 );
1106 }
1107 if (
1108 spider->ignore_dup_key &&
1109 (
1110 error_num == ER_DUP_ENTRY ||
1111 error_num == ER_DUP_KEY ||
1112 error_num == HA_ERR_FOUND_DUPP_KEY
1113 )
1114 ) {
1115 ++(*dup_key_found);
1116 spider->trx->thd->clear_error();
1117 DBUG_RETURN(0);
1118 }
1119 DBUG_RETURN(error_num);
1120 }
1121 while (!(error_num = conn->db_conn->next_result()))
1122 {
1123 ;
1124 }
1125 if (error_num > 0 && !conn->db_conn->is_dup_entry_error(error_num))
1126 {
1127 conn->mta_conn_mutex_lock_already = FALSE;
1128 conn->mta_conn_mutex_unlock_later = FALSE;
1129 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
1130 pthread_mutex_unlock(&conn->mta_conn_mutex);
1131 if (
1132 share->monitoring_kind[link_idx] &&
1133 spider->need_mons[link_idx]
1134 ) {
1135 error_num = spider_ping_table_mon_from_table(
1136 spider->trx,
1137 spider->trx->thd,
1138 share,
1139 link_idx,
1140 (uint32) share->monitoring_sid[link_idx],
1141 share->table_name,
1142 share->table_name_length,
1143 spider->conn_link_idx[link_idx],
1144 NULL,
1145 0,
1146 share->monitoring_kind[link_idx],
1147 share->monitoring_limit[link_idx],
1148 share->monitoring_flag[link_idx],
1149 TRUE
1150 );
1151 }
1152 DBUG_RETURN(error_num);
1153 }
1154 conn->mta_conn_mutex_lock_already = FALSE;
1155 conn->mta_conn_mutex_unlock_later = FALSE;
1156 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
1157 pthread_mutex_unlock(&conn->mta_conn_mutex);
1158 DBUG_RETURN(0);
1159}
1160
1161size_t spider_db_real_escape_string(
1162 SPIDER_CONN *conn,
1163 char *to,
1164 const char *from,
1165 size_t from_length
1166) {
1167 DBUG_ENTER("spider_db_real_escape_string");
1168 DBUG_RETURN(conn->db_conn->escape_string(to, from, from_length));
1169}
1170
1171int spider_db_consistent_snapshot(
1172 SPIDER_CONN *conn,
1173 int *need_mon
1174) {
1175 int error_num;
1176 DBUG_ENTER("spider_db_consistent_snapshot");
1177 if ((error_num = conn->db_conn->consistent_snapshot(need_mon)))
1178 {
1179 DBUG_RETURN(error_num);
1180 }
1181 conn->trx_start = TRUE;
1182 DBUG_RETURN(0);
1183}
1184
1185int spider_db_start_transaction(
1186 SPIDER_CONN *conn,
1187 int *need_mon
1188) {
1189 int error_num;
1190 DBUG_ENTER("spider_db_start_transaction");
1191 if ((error_num = conn->db_conn->start_transaction(need_mon)))
1192 {
1193 DBUG_RETURN(error_num);
1194 }
1195 conn->trx_start = TRUE;
1196 DBUG_RETURN(0);
1197}
1198
1199int spider_db_commit(
1200 SPIDER_CONN *conn
1201) {
1202 int need_mon = 0, error_num;
1203 DBUG_ENTER("spider_db_commit");
1204 if (!conn->queued_connect && !conn->queued_trx_start)
1205 {
1206 if (conn->use_for_active_standby && conn->server_lost)
1207 {
1208 my_message(ER_SPIDER_LINK_IS_FAILOVER_NUM,
1209 ER_SPIDER_LINK_IS_FAILOVER_STR, MYF(0));
1210 DBUG_RETURN(ER_SPIDER_LINK_IS_FAILOVER_NUM);
1211 }
1212 if ((error_num = conn->db_conn->commit(&need_mon)))
1213 {
1214 DBUG_RETURN(error_num);
1215 }
1216 conn->trx_start = FALSE;
1217 } else
1218 conn->trx_start = FALSE;
1219 DBUG_RETURN(0);
1220}
1221
1222int spider_db_rollback(
1223 SPIDER_CONN *conn
1224) {
1225 int error_num, need_mon = 0;
1226 DBUG_ENTER("spider_db_rollback");
1227 if (!conn->queued_connect && !conn->queued_trx_start)
1228 {
1229 if ((error_num = conn->db_conn->rollback(&need_mon)))
1230 {
1231 DBUG_RETURN(error_num);
1232 }
1233 conn->trx_start = FALSE;
1234 } else
1235 conn->trx_start = FALSE;
1236 DBUG_RETURN(0);
1237}
1238
1239int spider_db_append_hex_string(
1240 spider_string *str,
1241 uchar *hex_ptr,
1242 int hex_ptr_length
1243) {
1244 uchar *end_ptr;
1245 char *str_ptr;
1246 DBUG_ENTER("spider_db_append_hex_string");
1247 if (hex_ptr_length)
1248 {
1249 if (str->reserve(SPIDER_SQL_HEX_LEN + hex_ptr_length * 2))
1250 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1251 str->q_append(SPIDER_SQL_HEX_STR, SPIDER_SQL_HEX_LEN);
1252 str_ptr = (char *) str->ptr() + str->length();
1253 for (end_ptr = hex_ptr + hex_ptr_length; hex_ptr < end_ptr; hex_ptr++)
1254 {
1255 *str_ptr++ = spider_dig_upper[(*hex_ptr) >> 4];
1256 *str_ptr++ = spider_dig_upper[(*hex_ptr) & 0x0F];
1257 }
1258 str->length(str->length() + hex_ptr_length * 2);
1259 } else {
1260 if (str->reserve((SPIDER_SQL_VALUE_QUOTE_LEN) * 2))
1261 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1262 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
1263 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
1264 }
1265 DBUG_RETURN(0);
1266}
1267
1268void spider_db_append_xid_str(
1269 spider_string *tmp_str,
1270 XID *xid
1271) {
1272 char format_id[sizeof(long) + 3];
1273 uint format_id_length;
1274 DBUG_ENTER("spider_db_append_xid_str");
1275
1276 format_id_length =
1277 my_sprintf(format_id, (format_id, "%lu", xid->formatID));
1278 spider_db_append_hex_string(tmp_str, (uchar *) xid->data, xid->gtrid_length);
1279/*
1280 tmp_str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
1281 tmp_str->q_append(xid->data, xid->gtrid_length);
1282 tmp_str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
1283*/
1284 tmp_str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
1285 spider_db_append_hex_string(tmp_str,
1286 (uchar *) xid->data + xid->gtrid_length, xid->bqual_length);
1287/*
1288 tmp_str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
1289 tmp_str->q_append(xid->data + xid->gtrid_length, xid->bqual_length);
1290 tmp_str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
1291*/
1292 tmp_str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
1293 tmp_str->q_append(format_id, format_id_length);
1294#ifndef DBUG_OFF
1295 ((char *) tmp_str->ptr())[tmp_str->length()] = '\0';
1296#endif
1297
1298 DBUG_VOID_RETURN;
1299}
1300
1301int spider_db_xa_end(
1302 SPIDER_CONN *conn,
1303 XID *xid
1304) {
1305 int need_mon = 0;
1306 DBUG_ENTER("spider_db_xa_end");
1307 if (!conn->queued_connect && !conn->queued_xa_start)
1308 {
1309 DBUG_RETURN(conn->db_conn->xa_end(xid, &need_mon));
1310 }
1311 DBUG_RETURN(0);
1312}
1313
1314int spider_db_xa_prepare(
1315 SPIDER_CONN *conn,
1316 XID *xid
1317) {
1318 int need_mon = 0;
1319 DBUG_ENTER("spider_db_xa_prepare");
1320 if (!conn->queued_connect && !conn->queued_xa_start)
1321 {
1322 if (conn->use_for_active_standby && conn->server_lost)
1323 {
1324 my_message(ER_SPIDER_LINK_IS_FAILOVER_NUM,
1325 ER_SPIDER_LINK_IS_FAILOVER_STR, MYF(0));
1326 DBUG_RETURN(ER_SPIDER_LINK_IS_FAILOVER_NUM);
1327 }
1328 DBUG_RETURN(conn->db_conn->xa_prepare(xid, &need_mon));
1329 }
1330 DBUG_RETURN(0);
1331}
1332
1333int spider_db_xa_commit(
1334 SPIDER_CONN *conn,
1335 XID *xid
1336) {
1337 int need_mon = 0;
1338 DBUG_ENTER("spider_db_xa_commit");
1339 if (!conn->queued_connect && !conn->queued_xa_start)
1340 {
1341 DBUG_RETURN(conn->db_conn->xa_commit(xid, &need_mon));
1342 }
1343 DBUG_RETURN(0);
1344}
1345
1346int spider_db_xa_rollback(
1347 SPIDER_CONN *conn,
1348 XID *xid
1349) {
1350 int need_mon = 0;
1351 DBUG_ENTER("spider_db_xa_rollback");
1352 if (!conn->queued_connect && !conn->queued_xa_start)
1353 {
1354 DBUG_RETURN(conn->db_conn->xa_rollback(xid, &need_mon));
1355 }
1356 DBUG_RETURN(0);
1357}
1358
1359int spider_db_lock_tables(
1360 ha_spider *spider,
1361 int link_idx
1362) {
1363 int error_num;
1364 SPIDER_CONN *conn = spider->conns[link_idx];
1365 DBUG_ENTER("spider_db_lock_tables");
1366 error_num = spider->dbton_handler[conn->dbton_id]->lock_tables(link_idx);
1367 DBUG_RETURN(error_num);
1368}
1369
1370int spider_db_unlock_tables(
1371 ha_spider *spider,
1372 int link_idx
1373) {
1374 int error_num;
1375 SPIDER_CONN *conn = spider->conns[link_idx];
1376 DBUG_ENTER("spider_db_unlock_tables");
1377 error_num = spider->dbton_handler[conn->dbton_id]->unlock_tables(link_idx);
1378 DBUG_RETURN(error_num);
1379}
1380
1381int spider_db_append_name_with_quote_str(
1382 spider_string *str,
1383 char *name,
1384 uint dbton_id
1385) {
1386 int error_num, length = strlen(name);
1387 char *name_end, head_code;
1388 DBUG_ENTER("spider_db_append_name_with_quote_str");
1389 for (name_end = name + length; name < name_end; name += length)
1390 {
1391 head_code = *name;
1392#ifdef SPIDER_HAS_MY_CHARLEN
1393 if ((length = my_charlen(system_charset_info, name, name_end)) < 1)
1394#else
1395 if (!(length = my_mbcharlen(system_charset_info, (uchar) head_code)))
1396#endif
1397 {
1398 my_message(ER_SPIDER_WRONG_CHARACTER_IN_NAME_NUM,
1399 ER_SPIDER_WRONG_CHARACTER_IN_NAME_STR, MYF(0));
1400 DBUG_RETURN(ER_SPIDER_WRONG_CHARACTER_IN_NAME_NUM);
1401 }
1402 if (
1403 length == 1 &&
1404 spider_dbton[dbton_id].db_util->is_name_quote(head_code)
1405 ) {
1406 if ((error_num = spider_dbton[dbton_id].db_util->
1407 append_escaped_name_quote(str)))
1408 {
1409 DBUG_RETURN(error_num);
1410 }
1411 } else {
1412 if (str->append(name, length, system_charset_info))
1413 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1414 }
1415 }
1416 DBUG_RETURN(0);
1417}
1418
1419int spider_db_append_select(
1420 ha_spider *spider
1421) {
1422 int error_num;
1423 DBUG_ENTER("spider_db_append_select");
1424
1425 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
1426 {
1427 if ((error_num = spider->append_select_sql_part(
1428 SPIDER_SQL_TYPE_SELECT_SQL)))
1429 DBUG_RETURN(error_num);
1430 }
1431 if (spider->sql_kinds & SPIDER_SQL_KIND_HANDLER)
1432 {
1433 if ((error_num = spider->append_select_sql_part(
1434 SPIDER_SQL_TYPE_HANDLER)))
1435 DBUG_RETURN(error_num);
1436 }
1437 DBUG_RETURN(0);
1438}
1439
1440int spider_db_append_select_columns(
1441 ha_spider *spider
1442) {
1443 int error_num;
1444 SPIDER_RESULT_LIST *result_list = &spider->result_list;
1445 DBUG_ENTER("spider_db_append_select_columns");
1446 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
1447 {
1448#ifdef HANDLER_HAS_DIRECT_AGGREGATE
1449 if (
1450 result_list->direct_aggregate &&
1451 (error_num = spider->append_sum_select_sql_part(
1452 SPIDER_SQL_TYPE_SELECT_SQL, NULL, 0))
1453 )
1454 DBUG_RETURN(error_num);
1455#endif
1456 if ((error_num = spider->append_match_select_sql_part(
1457 SPIDER_SQL_TYPE_SELECT_SQL, NULL, 0)))
1458 DBUG_RETURN(error_num);
1459 if (!spider->select_column_mode)
1460 {
1461 if (result_list->keyread)
1462 {
1463 if ((error_num = spider->append_key_select_sql_part(
1464 SPIDER_SQL_TYPE_SELECT_SQL, spider->active_index)))
1465 DBUG_RETURN(error_num);
1466 } else {
1467 if ((error_num = spider->append_table_select_sql_part(
1468 SPIDER_SQL_TYPE_SELECT_SQL)))
1469 DBUG_RETURN(error_num);
1470 }
1471 } else {
1472 if ((error_num = spider->append_minimum_select_sql_part(
1473 SPIDER_SQL_TYPE_SELECT_SQL)))
1474 DBUG_RETURN(error_num);
1475 }
1476 }
1477 if (spider->sql_kinds & SPIDER_SQL_KIND_HANDLER)
1478 {
1479 if ((error_num = spider->append_from_sql_part(SPIDER_SQL_TYPE_HANDLER)))
1480 DBUG_RETURN(error_num);
1481 }
1482 DBUG_RETURN(0);
1483}
1484
1485int spider_db_append_null_value(
1486 spider_string *str,
1487 KEY_PART_INFO *key_part,
1488 const uchar **ptr
1489) {
1490 DBUG_ENTER("spider_db_append_null_value");
1491 if (key_part->null_bit)
1492 {
1493 if (*(*ptr)++)
1494 {
1495 if (str->reserve(SPIDER_SQL_NULL_LEN))
1496 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1497 str->q_append(SPIDER_SQL_NULL_STR, SPIDER_SQL_NULL_LEN);
1498 DBUG_RETURN(-1);
1499 }
1500 }
1501 DBUG_RETURN(0);
1502}
1503
1504int spider_db_append_key_columns(
1505 const key_range *start_key,
1506 ha_spider *spider,
1507 spider_string *str
1508) {
1509 SPIDER_RESULT_LIST *result_list = &spider->result_list;
1510 KEY *key_info = result_list->key_info;
1511 uint key_name_length, key_count;
1512 key_part_map full_key_part_map =
1513 make_prev_keypart_map(spider_user_defined_key_parts(key_info));
1514 key_part_map start_key_part_map;
1515 char tmp_buf[MAX_FIELD_WIDTH];
1516 DBUG_ENTER("spider_db_append_key_columns");
1517
1518 start_key_part_map = start_key->keypart_map & full_key_part_map;
1519 DBUG_PRINT("info", ("spider spider_user_defined_key_parts=%u",
1520 spider_user_defined_key_parts(key_info)));
1521 DBUG_PRINT("info", ("spider full_key_part_map=%lu", full_key_part_map));
1522 DBUG_PRINT("info", ("spider start_key_part_map=%lu", start_key_part_map));
1523
1524 if (!start_key_part_map)
1525 DBUG_RETURN(0);
1526
1527 for (
1528 key_count = 0;
1529 start_key_part_map;
1530 start_key_part_map >>= 1,
1531 key_count++
1532 ) {
1533 key_name_length = my_sprintf(tmp_buf, (tmp_buf, "c%u", key_count));
1534 if (str->reserve(key_name_length + SPIDER_SQL_COMMA_LEN))
1535 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1536 str->q_append(tmp_buf, key_name_length);
1537 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
1538 }
1539 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
1540
1541 DBUG_RETURN(0);
1542}
1543
1544int spider_db_append_key_hint(
1545 spider_string *str,
1546 char *hint_str
1547) {
1548 int hint_str_len = strlen(hint_str);
1549 DBUG_ENTER("spider_db_append_key_hint");
1550 if (hint_str_len >= 2 &&
1551 (hint_str[0] == 'f' || hint_str[0] == 'F') && hint_str[1] == ' '
1552 ) {
1553 if (str->reserve(hint_str_len - 2 +
1554 SPIDER_SQL_SQL_FORCE_IDX_LEN + SPIDER_SQL_CLOSE_PAREN_LEN))
1555 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1556 hint_str += 2;
1557 str->q_append(SPIDER_SQL_SQL_FORCE_IDX_STR, SPIDER_SQL_SQL_FORCE_IDX_LEN);
1558 str->q_append(hint_str, hint_str_len - 2);
1559 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
1560 } else if (hint_str_len >= 2 &&
1561 (hint_str[0] == 'u' || hint_str[0] == 'U') && hint_str[1] == ' '
1562 ) {
1563 if (str->reserve(hint_str_len - 2 +
1564 SPIDER_SQL_SQL_USE_IDX_LEN + SPIDER_SQL_CLOSE_PAREN_LEN))
1565 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1566 hint_str += 2;
1567 str->q_append(SPIDER_SQL_SQL_USE_IDX_STR, SPIDER_SQL_SQL_USE_IDX_LEN);
1568 str->q_append(hint_str, hint_str_len - 2);
1569 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
1570 } else if (hint_str_len >= 3 &&
1571 (hint_str[0] == 'i' || hint_str[0] == 'I') &&
1572 (hint_str[1] == 'g' || hint_str[1] == 'G') && hint_str[2] == ' '
1573 ) {
1574 if (str->reserve(hint_str_len - 3 +
1575 SPIDER_SQL_SQL_IGNORE_IDX_LEN + SPIDER_SQL_CLOSE_PAREN_LEN))
1576 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1577 hint_str += 3;
1578 str->q_append(
1579 SPIDER_SQL_SQL_IGNORE_IDX_STR, SPIDER_SQL_SQL_IGNORE_IDX_LEN);
1580 str->q_append(hint_str, hint_str_len - 3);
1581 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
1582 } else if (str->reserve(hint_str_len + SPIDER_SQL_SPACE_LEN))
1583 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1584 else
1585 {
1586 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
1587 str->q_append(hint_str, hint_str_len);
1588 }
1589 DBUG_RETURN(0);
1590}
1591
1592int spider_db_append_hint_after_table(
1593 ha_spider *spider,
1594 spider_string *str,
1595 spider_string *hint
1596) {
1597 DBUG_ENTER("spider_db_append_hint_after_table");
1598 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
1599 {
1600 if (str->append(*hint))
1601 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1602 }
1603 DBUG_RETURN(0);
1604}
1605
1606int spider_db_append_key_where_internal(
1607 spider_string *str,
1608 spider_string *str_part,
1609 spider_string *str_part2,
1610 const key_range *start_key,
1611 const key_range *end_key,
1612 ha_spider *spider,
1613 bool set_order,
1614 ulong sql_type,
1615 uint dbton_id
1616) {
1617 SPIDER_RESULT_LIST *result_list = &spider->result_list;
1618 SPIDER_SHARE *share = spider->share;
1619#ifndef DBUG_OFF
1620 TABLE *table = spider->get_table();
1621#endif
1622 KEY *key_info = result_list->key_info;
1623 int error_num;
1624 uint key_name_length;
1625 key_part_map full_key_part_map;
1626 key_part_map start_key_part_map;
1627 key_part_map end_key_part_map;
1628 key_part_map tgt_key_part_map;
1629 int key_count;
1630 uint length;
1631 uint store_length;
1632 const uchar *ptr, *another_ptr;
1633 const key_range *use_key, *another_key;
1634 KEY_PART_INFO *key_part;
1635 Field *field;
1636 bool use_both = TRUE, key_eq;
1637 uint sql_kind;
1638 spider_db_handler *dbton_hdl = spider->dbton_handler[dbton_id];
1639 spider_db_share *dbton_share = share->dbton_share[dbton_id];
1640 DBUG_ENTER("spider_db_append_key_where_internal");
1641 switch (sql_type)
1642 {
1643 case SPIDER_SQL_TYPE_HANDLER:
1644 sql_kind = SPIDER_SQL_KIND_HANDLER;
1645 break;
1646#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1647 case SPIDER_SQL_TYPE_SELECT_HS:
1648 case SPIDER_SQL_TYPE_INSERT_HS:
1649 case SPIDER_SQL_TYPE_UPDATE_HS:
1650 case SPIDER_SQL_TYPE_DELETE_HS:
1651 sql_kind = SPIDER_SQL_KIND_HS;
1652 break;
1653#endif
1654 default:
1655 sql_kind = SPIDER_SQL_KIND_SQL;
1656 break;
1657 }
1658
1659 if (key_info)
1660 full_key_part_map =
1661 make_prev_keypart_map(spider_user_defined_key_parts(key_info));
1662 else
1663 full_key_part_map = 0;
1664
1665 if (start_key)
1666 {
1667 start_key_part_map = start_key->keypart_map & full_key_part_map;
1668 } else {
1669 start_key_part_map = 0;
1670 use_both = FALSE;
1671 }
1672 if (end_key) {
1673 end_key_part_map = end_key->keypart_map & full_key_part_map;
1674 result_list->end_key = end_key;
1675 } else {
1676 end_key_part_map = 0;
1677 use_both = FALSE;
1678 }
1679 DBUG_PRINT("info", ("spider spider_user_defined_key_parts=%u", key_info ?
1680 spider_user_defined_key_parts(key_info) : 0));
1681 DBUG_PRINT("info", ("spider full_key_part_map=%lu", full_key_part_map));
1682 DBUG_PRINT("info", ("spider start_key_part_map=%lu", start_key_part_map));
1683 DBUG_PRINT("info", ("spider end_key_part_map=%lu", end_key_part_map));
1684
1685#ifndef DBUG_OFF
1686 my_bitmap_map *tmp_map = dbug_tmp_use_all_columns(table, table->read_set);
1687#endif
1688
1689 if (sql_kind == SPIDER_SQL_KIND_HANDLER)
1690 {
1691 const char *key_name = key_info->name.str;
1692 key_name_length = key_info->name.length;
1693 if (str->reserve(SPIDER_SQL_READ_LEN +
1694 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + key_name_length))
1695 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1696 str->q_append(SPIDER_SQL_READ_STR, SPIDER_SQL_READ_LEN);
1697 if ((error_num = spider_dbton[dbton_id].db_util->
1698 append_name(str, key_name, key_name_length)))
1699 {
1700 DBUG_RETURN(error_num);
1701 }
1702 dbton_hdl->set_order_pos(SPIDER_SQL_TYPE_HANDLER);
1703 if (
1704 (start_key_part_map || end_key_part_map) &&
1705 !(use_both && (!start_key_part_map || !end_key_part_map))
1706 ) {
1707 if (str_part->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
1708 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1709 str_part->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
1710 result_list->ha_read_kind = 0;
1711 } else if (!result_list->desc_flg)
1712 {
1713 if (str->reserve(SPIDER_SQL_FIRST_LEN))
1714 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1715 str->q_append(SPIDER_SQL_FIRST_STR, SPIDER_SQL_FIRST_LEN);
1716 result_list->ha_read_kind = 1;
1717 } else {
1718 if (str->reserve(SPIDER_SQL_LAST_LEN))
1719 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1720 str->q_append(SPIDER_SQL_LAST_STR, SPIDER_SQL_LAST_LEN);
1721 result_list->ha_read_kind = 2;
1722 }
1723 }
1724 if (!start_key_part_map && !end_key_part_map)
1725 {
1726 result_list->key_order = 0;
1727 goto end;
1728 } else if (use_both && (!start_key_part_map || !end_key_part_map))
1729 {
1730 result_list->key_order = 0;
1731 goto end;
1732 } else if (start_key_part_map >= end_key_part_map)
1733 {
1734 use_key = start_key;
1735 another_key = end_key;
1736 tgt_key_part_map = start_key_part_map;
1737 } else {
1738 use_key = end_key;
1739 another_key = start_key;
1740 tgt_key_part_map = end_key_part_map;
1741 }
1742 DBUG_PRINT("info", ("spider tgt_key_part_map=%lu", tgt_key_part_map));
1743 if (start_key_part_map == end_key_part_map)
1744 result_list->use_both_key = TRUE;
1745
1746 if (sql_kind == SPIDER_SQL_KIND_SQL)
1747 {
1748 if (str->reserve(SPIDER_SQL_WHERE_LEN))
1749 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1750 str->q_append(SPIDER_SQL_WHERE_STR, SPIDER_SQL_WHERE_LEN);
1751 } else if (sql_kind == SPIDER_SQL_KIND_HANDLER)
1752 {
1753 if (str_part2->reserve(SPIDER_SQL_WHERE_LEN))
1754 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1755 str_part2->q_append(SPIDER_SQL_WHERE_STR, SPIDER_SQL_WHERE_LEN);
1756 }
1757
1758 for (
1759 key_part = key_info->key_part,
1760 length = 0,
1761 key_count = 0;
1762 tgt_key_part_map;
1763 length += store_length,
1764 tgt_key_part_map >>= 1,
1765 start_key_part_map >>= 1,
1766 end_key_part_map >>= 1,
1767 key_part++,
1768 key_count++
1769 ) {
1770 store_length = key_part->store_length;
1771 field = key_part->field;
1772 key_name_length = dbton_share->get_column_name_length(field->field_index);
1773 ptr = use_key->key + length;
1774 if (use_both)
1775 {
1776 another_ptr = another_key->key + length;
1777 if (
1778 start_key_part_map &&
1779 end_key_part_map &&
1780 !memcmp(ptr, another_ptr, store_length)
1781 )
1782 key_eq = TRUE;
1783 else {
1784 key_eq = FALSE;
1785#ifndef DBUG_OFF
1786 if (
1787 start_key_part_map &&
1788 end_key_part_map
1789 )
1790 DBUG_PRINT("info", ("spider memcmp=%d",
1791 memcmp(ptr, another_ptr, store_length)));
1792#endif
1793 }
1794 } else {
1795 DBUG_PRINT("info", ("spider tgt_key_part_map=%lu", tgt_key_part_map));
1796 if (tgt_key_part_map > 1)
1797 key_eq = TRUE;
1798 else
1799 key_eq = FALSE;
1800 }
1801 if (
1802 (key_eq && use_key == start_key) ||
1803 (!key_eq && start_key_part_map)
1804 ) {
1805 bool tgt_final = (use_key == start_key && tgt_key_part_map == 1);
1806 ptr = start_key->key + length;
1807 if (
1808 (error_num = dbton_hdl->append_is_null_part(sql_type, key_part,
1809 start_key, &ptr, key_eq, tgt_final))
1810 ) {
1811 if (error_num > 0)
1812 DBUG_RETURN(error_num);
1813 if (
1814 !set_order &&
1815 start_key->flag != HA_READ_KEY_EXACT &&
1816 sql_kind == SPIDER_SQL_KIND_SQL
1817 ) {
1818 result_list->key_order = key_count;
1819 set_order = TRUE;
1820 }
1821 } else if (key_eq)
1822 {
1823 DBUG_PRINT("info", ("spider key_eq"));
1824 if (sql_kind == SPIDER_SQL_KIND_SQL)
1825 {
1826 if (str->reserve(store_length + key_name_length +
1827 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
1828 SPIDER_SQL_EQUAL_LEN + SPIDER_SQL_AND_LEN))
1829 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1830 dbton_share->append_column_name(str, field->field_index);
1831 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
1832 if (spider_dbton[dbton_id].db_util->
1833 append_column_value(spider, str, field, ptr,
1834 share->access_charset))
1835 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1836 } else if (sql_kind == SPIDER_SQL_KIND_HANDLER)
1837 {
1838 if (str_part2->reserve(store_length + key_name_length +
1839 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
1840 SPIDER_SQL_EQUAL_LEN + SPIDER_SQL_AND_LEN))
1841 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1842 dbton_share->append_column_name(str_part2, field->field_index);
1843 str_part2->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
1844 if (spider_dbton[dbton_id].db_util->
1845 append_column_value(spider, str_part2, field, ptr,
1846 share->access_charset))
1847 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1848
1849 if (use_key == start_key)
1850 {
1851 if (spider_dbton[dbton_id].db_util->
1852 append_column_value(spider, str_part, field, ptr,
1853 share->access_charset))
1854 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1855 }
1856 }
1857#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1858 else {
1859 if (spider_dbton[share->use_hs_dbton_ids[0]].db_util->
1860 append_column_value(spider, NULL, field, ptr,
1861 share->access_charset))
1862 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1863 }
1864#endif
1865 } else {
1866 DBUG_PRINT("info", ("spider start_key->flag=%d", start_key->flag));
1867 switch (start_key->flag)
1868 {
1869#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000
1870 case HA_READ_PREFIX_LAST:
1871 result_list->desc_flg = TRUE;
1872#endif
1873 /* fall through */
1874 case HA_READ_KEY_EXACT:
1875 if (sql_kind == SPIDER_SQL_KIND_SQL)
1876 {
1877 if (str->reserve(store_length + key_name_length +
1878 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
1879 SPIDER_SQL_EQUAL_LEN))
1880 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1881 dbton_share->append_column_name(str, field->field_index);
1882 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
1883 if (spider_dbton[dbton_id].db_util->
1884 append_column_value(spider, str, field, ptr,
1885 share->access_charset))
1886 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1887 } else if (sql_kind == SPIDER_SQL_KIND_HANDLER)
1888 {
1889 if (str_part2->reserve(store_length + key_name_length +
1890 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
1891 SPIDER_SQL_EQUAL_LEN))
1892 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1893 dbton_share->append_column_name(str_part2, field->field_index);
1894 str_part2->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
1895 if (spider_dbton[dbton_id].db_util->
1896 append_column_value(spider, str_part2, field, ptr,
1897 share->access_charset))
1898 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1899
1900 if (use_key == start_key)
1901 {
1902 if (tgt_key_part_map == 1)
1903 {
1904 if (str->reserve(SPIDER_SQL_EQUAL_LEN))
1905 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1906 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
1907 }
1908 if (spider_dbton[dbton_id].db_util->
1909 append_column_value(spider, str_part, field, ptr,
1910 share->access_charset))
1911 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1912 }
1913 }
1914#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1915 else {
1916 if (spider_dbton[share->use_hs_dbton_ids[0]].db_util->
1917 append_column_value(spider, NULL, field, ptr,
1918 share->access_charset))
1919 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1920 if (str->reserve(SPIDER_SQL_HS_EQUAL_LEN))
1921 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1922 str->q_append(SPIDER_SQL_HS_EQUAL_STR, SPIDER_SQL_HS_EQUAL_LEN);
1923 }
1924#endif
1925 break;
1926 case HA_READ_AFTER_KEY:
1927 if (sql_kind == SPIDER_SQL_KIND_SQL)
1928 {
1929 if (str->reserve(store_length + key_name_length +
1930 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
1931 SPIDER_SQL_GT_LEN))
1932 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1933 dbton_share->append_column_name(str, field->field_index);
1934 str->q_append(SPIDER_SQL_GT_STR, SPIDER_SQL_GT_LEN);
1935 if (spider_dbton[dbton_id].db_util->
1936 append_column_value(spider, str, field, ptr,
1937 share->access_charset))
1938 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1939 if (use_both)
1940 start_key_part_map = 0;
1941 if (!set_order)
1942 {
1943 result_list->key_order = key_count;
1944 set_order = TRUE;
1945 }
1946 } else if (sql_kind == SPIDER_SQL_KIND_HANDLER)
1947 {
1948 if (str_part2->reserve(store_length + key_name_length +
1949 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
1950 SPIDER_SQL_GT_LEN))
1951 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1952 dbton_share->append_column_name(str_part2, field->field_index);
1953 str_part2->q_append(SPIDER_SQL_GT_STR, SPIDER_SQL_GT_LEN);
1954 if (spider_dbton[dbton_id].db_util->
1955 append_column_value(spider, str_part2, field, ptr,
1956 share->access_charset))
1957 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1958
1959 if (use_key == start_key)
1960 {
1961 if (tgt_key_part_map == 1)
1962 {
1963 if (str->reserve(SPIDER_SQL_GT_LEN))
1964 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1965 str->q_append(SPIDER_SQL_GT_STR, SPIDER_SQL_GT_LEN);
1966 }
1967 if (spider_dbton[dbton_id].db_util->
1968 append_column_value(spider, str_part, field, ptr,
1969 share->access_charset))
1970 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1971 }
1972 }
1973#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1974 else {
1975 if (spider_dbton[share->use_hs_dbton_ids[0]].db_util->
1976 append_column_value(spider, NULL, field, ptr,
1977 share->access_charset))
1978 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1979 if (str->reserve(SPIDER_SQL_HS_GT_LEN))
1980 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1981 str->q_append(SPIDER_SQL_HS_GT_STR, SPIDER_SQL_HS_GT_LEN);
1982 }
1983#endif
1984 break;
1985 case HA_READ_BEFORE_KEY:
1986 result_list->desc_flg = TRUE;
1987 if (sql_kind == SPIDER_SQL_KIND_SQL)
1988 {
1989 if (str->reserve(store_length + key_name_length +
1990 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
1991 SPIDER_SQL_LT_LEN))
1992 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1993 dbton_share->append_column_name(str, field->field_index);
1994 str->q_append(SPIDER_SQL_LT_STR, SPIDER_SQL_LT_LEN);
1995 if (spider_dbton[dbton_id].db_util->
1996 append_column_value(spider, str, field, ptr,
1997 share->access_charset))
1998 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1999 if (use_both)
2000 start_key_part_map = 0;
2001 if (!set_order)
2002 {
2003 result_list->key_order = key_count;
2004 set_order = TRUE;
2005 }
2006 } else if (sql_kind == SPIDER_SQL_KIND_HANDLER)
2007 {
2008 if (str_part2->reserve(store_length + key_name_length +
2009 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2010 SPIDER_SQL_LT_LEN))
2011 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2012 dbton_share->append_column_name(str_part2, field->field_index);
2013 str_part2->q_append(SPIDER_SQL_LT_STR, SPIDER_SQL_LT_LEN);
2014 if (spider_dbton[dbton_id].db_util->
2015 append_column_value(spider, str_part2, field, ptr,
2016 share->access_charset))
2017 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2018
2019 if (use_key == start_key)
2020 {
2021 if (tgt_key_part_map == 1)
2022 {
2023 if (str->reserve(SPIDER_SQL_LT_LEN))
2024 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2025 str->q_append(SPIDER_SQL_LT_STR, SPIDER_SQL_LT_LEN);
2026 }
2027 if (spider_dbton[dbton_id].db_util->
2028 append_column_value(spider, str_part, field, ptr,
2029 share->access_charset))
2030 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2031 }
2032 }
2033#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2034 else {
2035 if (spider_dbton[share->use_hs_dbton_ids[0]].db_util->
2036 append_column_value(spider, NULL, field, ptr,
2037 share->access_charset))
2038 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2039 if (str->reserve(SPIDER_SQL_HS_LT_LEN))
2040 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2041 str->q_append(SPIDER_SQL_HS_LT_STR, SPIDER_SQL_HS_LT_LEN);
2042 }
2043#endif
2044 break;
2045#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000
2046#else
2047 case HA_READ_PREFIX_LAST:
2048 result_list->limit_num = 1;
2049 /* fall through */
2050#endif
2051 case HA_READ_KEY_OR_PREV:
2052 case HA_READ_PREFIX_LAST_OR_PREV:
2053 result_list->desc_flg = TRUE;
2054 if (sql_kind == SPIDER_SQL_KIND_SQL)
2055 {
2056 if (str->reserve(store_length + key_name_length +
2057 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2058 SPIDER_SQL_LTEQUAL_LEN))
2059 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2060 dbton_share->append_column_name(str, field->field_index);
2061 str->q_append(SPIDER_SQL_LTEQUAL_STR, SPIDER_SQL_LTEQUAL_LEN);
2062 if (spider_dbton[dbton_id].db_util->
2063 append_column_value(spider, str, field, ptr,
2064 share->access_charset))
2065 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2066 if (!set_order)
2067 {
2068 result_list->key_order = key_count;
2069 set_order = TRUE;
2070 }
2071 } else if (sql_kind == SPIDER_SQL_KIND_HANDLER)
2072 {
2073 if (str_part2->reserve(store_length + key_name_length +
2074 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2075 SPIDER_SQL_LTEQUAL_LEN))
2076 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2077 dbton_share->append_column_name(str_part2, field->field_index);
2078 str_part2->q_append(SPIDER_SQL_LTEQUAL_STR,
2079 SPIDER_SQL_LTEQUAL_LEN);
2080 if (spider_dbton[dbton_id].db_util->
2081 append_column_value(spider, str_part2, field, ptr,
2082 share->access_charset))
2083 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2084
2085 if (use_key == start_key)
2086 {
2087 if (tgt_key_part_map == 1)
2088 {
2089 if (str->reserve(SPIDER_SQL_LTEQUAL_LEN))
2090 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2091 str->q_append(SPIDER_SQL_LTEQUAL_STR,
2092 SPIDER_SQL_LTEQUAL_LEN);
2093 }
2094 if (spider_dbton[dbton_id].db_util->
2095 append_column_value(spider, str_part, field, ptr,
2096 share->access_charset))
2097 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2098 }
2099 }
2100#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2101 else {
2102 if (spider_dbton[share->use_hs_dbton_ids[0]].db_util->
2103 append_column_value(spider, NULL, field, ptr,
2104 share->access_charset))
2105 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2106 if (str->reserve(SPIDER_SQL_HS_LTEQUAL_LEN))
2107 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2108 str->q_append(SPIDER_SQL_HS_LTEQUAL_STR,
2109 SPIDER_SQL_HS_LTEQUAL_LEN);
2110 }
2111#endif
2112 break;
2113 case HA_READ_MBR_CONTAIN:
2114 if (str->reserve(SPIDER_SQL_MBR_CONTAIN_LEN))
2115 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2116 str->q_append(SPIDER_SQL_MBR_CONTAIN_STR,
2117 SPIDER_SQL_MBR_CONTAIN_LEN);
2118 if (
2119 spider_dbton[dbton_id].db_util->
2120 append_column_value(spider, str, field, ptr,
2121 share->access_charset) ||
2122 str->reserve(SPIDER_SQL_COMMA_LEN + key_name_length +
2123 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_CLOSE_PAREN_LEN)
2124 )
2125 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2126 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
2127 dbton_share->append_column_name(str, field->field_index);
2128 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
2129 SPIDER_SQL_CLOSE_PAREN_LEN);
2130 break;
2131 case HA_READ_MBR_INTERSECT:
2132 if (str->reserve(SPIDER_SQL_MBR_INTERSECT_LEN))
2133 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2134 str->q_append(SPIDER_SQL_MBR_INTERSECT_STR,
2135 SPIDER_SQL_MBR_INTERSECT_LEN);
2136 if (
2137 spider_dbton[dbton_id].db_util->
2138 append_column_value(spider, str, field, ptr,
2139 share->access_charset) ||
2140 str->reserve(SPIDER_SQL_COMMA_LEN + key_name_length +
2141 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_CLOSE_PAREN_LEN)
2142 )
2143 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2144 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
2145 dbton_share->append_column_name(str, field->field_index);
2146 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
2147 SPIDER_SQL_CLOSE_PAREN_LEN);
2148 break;
2149 case HA_READ_MBR_WITHIN:
2150 if (str->reserve(SPIDER_SQL_MBR_WITHIN_LEN))
2151 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2152 str->q_append(SPIDER_SQL_MBR_WITHIN_STR,
2153 SPIDER_SQL_MBR_WITHIN_LEN);
2154 if (
2155 spider_dbton[dbton_id].db_util->
2156 append_column_value(spider, str, field, ptr,
2157 share->access_charset) ||
2158 str->reserve(SPIDER_SQL_COMMA_LEN + key_name_length +
2159 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_CLOSE_PAREN_LEN)
2160 )
2161 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2162 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
2163 dbton_share->append_column_name(str, field->field_index);
2164 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
2165 SPIDER_SQL_CLOSE_PAREN_LEN);
2166 break;
2167 case HA_READ_MBR_DISJOINT:
2168 if (str->reserve(SPIDER_SQL_MBR_DISJOINT_LEN))
2169 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2170 str->q_append(SPIDER_SQL_MBR_DISJOINT_STR,
2171 SPIDER_SQL_MBR_DISJOINT_LEN);
2172 if (
2173 spider_dbton[dbton_id].db_util->
2174 append_column_value(spider, str, field, ptr,
2175 share->access_charset) ||
2176 str->reserve(SPIDER_SQL_COMMA_LEN + key_name_length +
2177 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_CLOSE_PAREN_LEN)
2178 )
2179 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2180 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
2181 dbton_share->append_column_name(str, field->field_index);
2182 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
2183 SPIDER_SQL_CLOSE_PAREN_LEN);
2184 break;
2185 case HA_READ_MBR_EQUAL:
2186 if (str->reserve(SPIDER_SQL_MBR_EQUAL_LEN))
2187 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2188 str->q_append(SPIDER_SQL_MBR_EQUAL_STR, SPIDER_SQL_MBR_EQUAL_LEN);
2189 if (
2190 spider_dbton[dbton_id].db_util->
2191 append_column_value(spider, str, field, ptr,
2192 share->access_charset) ||
2193 str->reserve(SPIDER_SQL_COMMA_LEN + key_name_length +
2194 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_CLOSE_PAREN_LEN)
2195 )
2196 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2197 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
2198 dbton_share->append_column_name(str, field->field_index);
2199 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
2200 SPIDER_SQL_CLOSE_PAREN_LEN);
2201 break;
2202 default:
2203 if (sql_kind == SPIDER_SQL_KIND_SQL)
2204 {
2205 if (str->reserve(store_length + key_name_length +
2206 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2207 SPIDER_SQL_GTEQUAL_LEN))
2208 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2209 dbton_share->append_column_name(str, field->field_index);
2210 str->q_append(SPIDER_SQL_GTEQUAL_STR, SPIDER_SQL_GTEQUAL_LEN);
2211 if (spider_dbton[dbton_id].db_util->
2212 append_column_value(spider, str, field, ptr,
2213 share->access_charset))
2214 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2215 if (!set_order)
2216 {
2217 result_list->key_order = key_count;
2218 set_order = TRUE;
2219 }
2220 } else if (sql_kind == SPIDER_SQL_KIND_HANDLER)
2221 {
2222 if (str_part2->reserve(store_length + key_name_length +
2223 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2224 SPIDER_SQL_GTEQUAL_LEN))
2225 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2226 dbton_share->append_column_name(str_part2, field->field_index);
2227 str_part2->q_append(SPIDER_SQL_GTEQUAL_STR,
2228 SPIDER_SQL_GTEQUAL_LEN);
2229 if (spider_dbton[dbton_id].db_util->
2230 append_column_value(spider, str_part2, field, ptr,
2231 share->access_charset))
2232 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2233
2234 if (use_key == start_key)
2235 {
2236 if (tgt_key_part_map == 1)
2237 {
2238 if (str->reserve(SPIDER_SQL_GTEQUAL_LEN))
2239 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2240 str->q_append(SPIDER_SQL_GTEQUAL_STR,
2241 SPIDER_SQL_GTEQUAL_LEN);
2242 }
2243 if (spider_dbton[dbton_id].db_util->
2244 append_column_value(spider, str_part, field, ptr,
2245 share->access_charset))
2246 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2247 }
2248 }
2249#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2250 else {
2251 if (spider_dbton[share->use_hs_dbton_ids[0]].db_util->
2252 append_column_value(spider, NULL, field, ptr,
2253 share->access_charset))
2254 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2255 if (str->reserve(SPIDER_SQL_HS_GTEQUAL_LEN))
2256 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2257 str->q_append(SPIDER_SQL_HS_GTEQUAL_STR,
2258 SPIDER_SQL_HS_GTEQUAL_LEN);
2259 }
2260#endif
2261 break;
2262 }
2263 }
2264 if (sql_kind == SPIDER_SQL_KIND_SQL)
2265 {
2266 if (str->reserve(SPIDER_SQL_AND_LEN))
2267 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2268 str->q_append(SPIDER_SQL_AND_STR,
2269 SPIDER_SQL_AND_LEN);
2270 } else if (sql_kind == SPIDER_SQL_KIND_HANDLER)
2271 {
2272 if (str_part2->reserve(SPIDER_SQL_AND_LEN))
2273 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2274 str_part2->q_append(SPIDER_SQL_AND_STR,
2275 SPIDER_SQL_AND_LEN);
2276
2277 if (use_key == start_key)
2278 {
2279 if (str_part->reserve(SPIDER_SQL_COMMA_LEN))
2280 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2281 str_part->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
2282 }
2283 }
2284 }
2285
2286#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2287 if (sql_kind != SPIDER_SQL_KIND_HS)
2288 {
2289#endif
2290 if (
2291 (key_eq && use_key == end_key) ||
2292 (!key_eq && end_key_part_map)
2293 ) {
2294 bool tgt_final = (use_key == end_key && tgt_key_part_map == 1);
2295 ptr = end_key->key + length;
2296 if ((error_num = dbton_hdl->append_is_null_part(sql_type, key_part,
2297 end_key, &ptr, key_eq, tgt_final)))
2298 {
2299 if (error_num > 0)
2300 DBUG_RETURN(error_num);
2301 if (
2302 !set_order &&
2303 end_key->flag != HA_READ_KEY_EXACT &&
2304 sql_kind == SPIDER_SQL_KIND_SQL
2305 ) {
2306 result_list->key_order = key_count;
2307 set_order = TRUE;
2308 }
2309 } else if (key_eq)
2310 {
2311 DBUG_PRINT("info", ("spider key_eq"));
2312 if (sql_kind == SPIDER_SQL_KIND_SQL)
2313 {
2314 if (str->reserve(store_length + key_name_length +
2315 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2316 SPIDER_SQL_EQUAL_LEN + SPIDER_SQL_AND_LEN))
2317 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2318 dbton_share->append_column_name(str, field->field_index);
2319 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
2320 if (spider_dbton[dbton_id].db_util->
2321 append_column_value(spider, str, field, ptr,
2322 share->access_charset))
2323 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2324 } else {
2325 if (str_part2->reserve(store_length + key_name_length +
2326 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2327 SPIDER_SQL_EQUAL_LEN + SPIDER_SQL_AND_LEN))
2328 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2329 dbton_share->append_column_name(str_part2, field->field_index);
2330 str_part2->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
2331 if (spider_dbton[dbton_id].db_util->
2332 append_column_value(spider, str_part2, field, ptr,
2333 share->access_charset))
2334 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2335
2336 if (use_key == end_key)
2337 {
2338/*
2339 if (tgt_key_part_map == 1)
2340 {
2341 if (str->reserve(SPIDER_SQL_EQUAL_LEN))
2342 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2343 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
2344 }
2345*/
2346 if (spider_dbton[dbton_id].db_util->
2347 append_column_value(spider, str_part, field, ptr,
2348 share->access_charset))
2349 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2350 }
2351 }
2352 } else {
2353 DBUG_PRINT("info", ("spider end_key->flag=%d", end_key->flag));
2354 switch (end_key->flag)
2355 {
2356 case HA_READ_BEFORE_KEY:
2357 if (sql_kind == SPIDER_SQL_KIND_SQL)
2358 {
2359 if (str->reserve(store_length + key_name_length +
2360 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2361 SPIDER_SQL_LT_LEN))
2362 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2363 dbton_share->append_column_name(str, field->field_index);
2364 str->q_append(SPIDER_SQL_LT_STR, SPIDER_SQL_LT_LEN);
2365 if (spider_dbton[dbton_id].db_util->
2366 append_column_value(spider, str, field, ptr,
2367 share->access_charset))
2368 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2369 if (use_both)
2370 end_key_part_map = 0;
2371 if (!set_order)
2372 {
2373 result_list->key_order = key_count;
2374 set_order = TRUE;
2375 }
2376 } else {
2377 if (str_part2->reserve(store_length + key_name_length +
2378 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2379 SPIDER_SQL_LT_LEN))
2380 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2381 dbton_share->append_column_name(str_part2, field->field_index);
2382 str_part2->q_append(SPIDER_SQL_LT_STR, SPIDER_SQL_LT_LEN);
2383 if (spider_dbton[dbton_id].db_util->
2384 append_column_value(spider, str_part2, field, ptr,
2385 share->access_charset))
2386 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2387
2388 if (use_key == end_key)
2389 {
2390 if (tgt_key_part_map == 1)
2391 {
2392 if (str->reserve(SPIDER_SQL_LT_LEN))
2393 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2394 str->q_append(SPIDER_SQL_LT_STR, SPIDER_SQL_LT_LEN);
2395 }
2396 if (spider_dbton[dbton_id].db_util->
2397 append_column_value(spider, str_part, field, ptr,
2398 share->access_charset))
2399 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2400 }
2401 }
2402 break;
2403 default:
2404 if (sql_kind == SPIDER_SQL_KIND_SQL)
2405 {
2406 if (str->reserve(store_length + key_name_length +
2407 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2408 SPIDER_SQL_LTEQUAL_LEN))
2409 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2410 dbton_share->append_column_name(str, field->field_index);
2411 str->q_append(SPIDER_SQL_LTEQUAL_STR, SPIDER_SQL_LTEQUAL_LEN);
2412 if (spider_dbton[dbton_id].db_util->
2413 append_column_value(spider, str, field, ptr,
2414 share->access_charset))
2415 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2416 if (!set_order)
2417 {
2418 result_list->key_order = key_count;
2419 set_order = TRUE;
2420 }
2421 } else {
2422 if (str_part2->reserve(store_length + key_name_length +
2423 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
2424 SPIDER_SQL_LTEQUAL_LEN))
2425 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2426 dbton_share->append_column_name(str_part2, field->field_index);
2427 str_part2->q_append(SPIDER_SQL_LTEQUAL_STR,
2428 SPIDER_SQL_LTEQUAL_LEN);
2429 if (spider_dbton[dbton_id].db_util->
2430 append_column_value(spider, str_part2, field, ptr,
2431 share->access_charset))
2432 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2433
2434 if (use_key == end_key)
2435 {
2436 if (tgt_key_part_map == 1)
2437 {
2438 if (str->reserve(SPIDER_SQL_LTEQUAL_LEN))
2439 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2440 str->q_append(SPIDER_SQL_LTEQUAL_STR,
2441 SPIDER_SQL_LTEQUAL_LEN);
2442 }
2443 if (spider_dbton[dbton_id].db_util->
2444 append_column_value(spider, str_part, field, ptr,
2445 share->access_charset))
2446 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2447 }
2448 }
2449 break;
2450 }
2451 }
2452 if (sql_kind == SPIDER_SQL_KIND_SQL)
2453 {
2454 if (str->reserve(SPIDER_SQL_AND_LEN))
2455 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2456 str->q_append(SPIDER_SQL_AND_STR,
2457 SPIDER_SQL_AND_LEN);
2458 } else {
2459 if (str_part2->reserve(SPIDER_SQL_AND_LEN))
2460 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2461 str_part2->q_append(SPIDER_SQL_AND_STR,
2462 SPIDER_SQL_AND_LEN);
2463
2464 if (use_key == end_key)
2465 {
2466 if (str_part->reserve(SPIDER_SQL_COMMA_LEN))
2467 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2468 str_part->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
2469 }
2470 }
2471 }
2472 if (use_both && (!start_key_part_map || !end_key_part_map))
2473 break;
2474#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2475 }
2476#endif
2477 }
2478 if ((error_num = dbton_hdl->append_where_terminator_part(sql_type,
2479 set_order, key_count)))
2480 DBUG_RETURN(error_num);
2481
2482end:
2483 /* use condition */
2484 if (dbton_hdl->append_condition_part(NULL, 0, sql_type, FALSE))
2485 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2486 if (sql_kind == SPIDER_SQL_KIND_SQL)
2487 dbton_hdl->set_order_pos(sql_type);
2488#ifndef DBUG_OFF
2489 dbug_tmp_restore_column_map(table->read_set, tmp_map);
2490#endif
2491 DBUG_RETURN(0);
2492}
2493
2494int spider_db_append_key_where(
2495 const key_range *start_key,
2496 const key_range *end_key,
2497 ha_spider *spider
2498) {
2499 int error_num;
2500 DBUG_ENTER("spider_db_append_key_where");
2501 if ((spider->sql_kinds & SPIDER_SQL_KIND_SQL))
2502 {
2503 DBUG_PRINT("info",("spider call internal by SPIDER_SQL_KIND_SQL"));
2504 if ((error_num = spider->append_key_where_sql_part(start_key, end_key,
2505 SPIDER_SQL_TYPE_SELECT_SQL)))
2506 DBUG_RETURN(error_num);
2507 }
2508 if (spider->sql_kinds & SPIDER_SQL_KIND_HANDLER)
2509 {
2510 DBUG_PRINT("info",("spider call internal by SPIDER_SQL_KIND_HANDLER"));
2511 if ((error_num = spider->append_key_where_sql_part(start_key, end_key,
2512 SPIDER_SQL_TYPE_HANDLER)))
2513 DBUG_RETURN(error_num);
2514 }
2515#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2516 if ((spider->sql_kinds & SPIDER_SQL_KIND_HS))
2517 {
2518 DBUG_PRINT("info",("spider call internal by SPIDER_SQL_KIND_HS"));
2519 if ((error_num = spider->append_key_where_hs_part(start_key, end_key,
2520 SPIDER_SQL_TYPE_SELECT_HS)))
2521 DBUG_RETURN(error_num);
2522 }
2523#endif
2524 DBUG_RETURN(0);
2525}
2526
2527#ifdef HANDLER_HAS_DIRECT_AGGREGATE
2528int spider_db_refetch_for_item_sum_funcs(
2529 ha_spider *spider
2530) {
2531 int error_num;
2532 SPIDER_RESULT_LIST *result_list = &spider->result_list;
2533 DBUG_ENTER("spider_db_refetch_for_item_sum_funcs");
2534 if (result_list->snap_direct_aggregate)
2535 {
2536 SPIDER_DB_ROW *row = result_list->snap_row;
2537 row->first();
2538 if (result_list->snap_mrr_with_cnt)
2539 {
2540 row->next();
2541 }
2542 if ((error_num = spider_db_fetch_for_item_sum_funcs(row, spider)))
2543 DBUG_RETURN(error_num);
2544 }
2545 DBUG_RETURN(0);
2546}
2547
2548int spider_db_fetch_for_item_sum_funcs(
2549 SPIDER_DB_ROW *row,
2550 ha_spider *spider
2551) {
2552 int error_num;
2553 st_select_lex *select_lex;
2554 DBUG_ENTER("spider_db_fetch_for_item_sum_funcs");
2555 select_lex = spider_get_select_lex(spider);
2556 JOIN *join = select_lex->join;
2557 Item_sum **item_sum_ptr;
2558 spider->direct_aggregate_item_current = NULL;
2559 for (item_sum_ptr = join->sum_funcs; *item_sum_ptr; ++item_sum_ptr)
2560 {
2561 if ((error_num = spider_db_fetch_for_item_sum_func(row, *item_sum_ptr,
2562 spider)))
2563 DBUG_RETURN(error_num);
2564 }
2565 DBUG_RETURN(0);
2566}
2567
2568int spider_db_fetch_for_item_sum_func(
2569 SPIDER_DB_ROW *row,
2570 Item_sum *item_sum,
2571 ha_spider *spider
2572) {
2573 int error_num;
2574 SPIDER_SHARE *share = spider->share;
2575 THD *thd = spider->trx->thd;
2576 DBUG_ENTER("spider_db_fetch_for_item_sum_func");
2577 DBUG_PRINT("info",("spider Sumfunctype = %d", item_sum->sum_func()));
2578 switch (item_sum->sum_func())
2579 {
2580 case Item_sum::COUNT_FUNC:
2581 {
2582 Item_sum_count *item_sum_count = (Item_sum_count *) item_sum;
2583 if (!row->is_null())
2584 item_sum_count->direct_add(row->val_int());
2585 else
2586 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
2587 row->next();
2588 }
2589 break;
2590 case Item_sum::SUM_FUNC:
2591 {
2592 Item_sum_sum *item_sum_sum = (Item_sum_sum *) item_sum;
2593 if (item_sum_sum->result_type() == DECIMAL_RESULT)
2594 {
2595 my_decimal decimal_value;
2596 item_sum_sum->direct_add(row->val_decimal(&decimal_value,
2597 share->access_charset));
2598 } else {
2599 item_sum_sum->direct_add(row->val_real(), row->is_null());
2600 }
2601 row->next();
2602 }
2603 break;
2604 case Item_sum::MIN_FUNC:
2605 case Item_sum::MAX_FUNC:
2606 {
2607 if (!spider->direct_aggregate_item_current)
2608 {
2609 if (!spider->direct_aggregate_item_first)
2610 {
2611 if (!spider_bulk_malloc(spider_current_trx, 240, MYF(MY_WME),
2612 &spider->direct_aggregate_item_first, sizeof(SPIDER_ITEM_HLD),
2613 NullS)
2614 ) {
2615 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2616 }
2617 spider->direct_aggregate_item_first->next = NULL;
2618 spider->direct_aggregate_item_first->item = NULL;
2619 spider->direct_aggregate_item_first->tgt_num = 0;
2620#ifdef SPIDER_ITEM_STRING_WITHOUT_SET_STR_WITH_COPY_AND_THDPTR
2621 spider->direct_aggregate_item_first->init_mem_root = FALSE;
2622#endif
2623 }
2624 spider->direct_aggregate_item_current =
2625 spider->direct_aggregate_item_first;
2626 } else {
2627 if (!spider->direct_aggregate_item_current->next)
2628 {
2629 if (!spider_bulk_malloc(spider_current_trx, 241, MYF(MY_WME),
2630 &spider->direct_aggregate_item_current->next,
2631 sizeof(SPIDER_ITEM_HLD), NullS)
2632 ) {
2633 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2634 }
2635 spider->direct_aggregate_item_current->next->next = NULL;
2636 spider->direct_aggregate_item_current->next->item = NULL;
2637 spider->direct_aggregate_item_current->next->tgt_num =
2638 spider->direct_aggregate_item_current->tgt_num + 1;
2639#ifdef SPIDER_ITEM_STRING_WITHOUT_SET_STR_WITH_COPY_AND_THDPTR
2640 spider->direct_aggregate_item_current->next->init_mem_root = FALSE;
2641#endif
2642 }
2643 spider->direct_aggregate_item_current =
2644 spider->direct_aggregate_item_current->next;
2645 }
2646 if (!spider->direct_aggregate_item_current->item)
2647 {
2648#ifdef SPIDER_ITEM_STRING_WITHOUT_SET_STR_WITH_COPY_AND_THDPTR
2649 if (!spider->direct_aggregate_item_current->init_mem_root)
2650 {
2651 SPD_INIT_ALLOC_ROOT(
2652 &spider->direct_aggregate_item_current->mem_root,
2653 4096, 0, MYF(MY_WME));
2654 spider->direct_aggregate_item_current->init_mem_root = TRUE;
2655 }
2656#endif
2657 Item *free_list = thd->free_list;
2658 spider->direct_aggregate_item_current->item =
2659#ifdef SPIDER_ITEM_STRING_WITHOUT_SET_STR_WITH_COPY
2660#ifdef SPIDER_ITEM_STRING_WITHOUT_SET_STR_WITH_COPY_AND_THDPTR
2661 new (&spider->direct_aggregate_item_current->mem_root)
2662 Item_string(thd, "", 0, share->access_charset);
2663#else
2664 new Item_string("", 0, share->access_charset);
2665#endif
2666#else
2667 new Item_string(share->access_charset);
2668#endif
2669 if (!spider->direct_aggregate_item_current->item)
2670 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2671 thd->free_list = free_list;
2672 }
2673
2674 Item_sum_hybrid *item_hybrid = (Item_sum_hybrid *) item_sum;
2675 Item_string *item =
2676 (Item_string *) spider->direct_aggregate_item_current->item;
2677 if (row->is_null())
2678 {
2679#ifdef SPIDER_ITEM_STRING_WITHOUT_SET_STR_WITH_COPY
2680 item->val_str(NULL)->length(0);
2681 item->append(NULL, 0);
2682#else
2683 item->set_str_with_copy(NULL, 0);
2684#endif
2685 item->null_value = TRUE;
2686 } else {
2687 char buf[MAX_FIELD_WIDTH];
2688 spider_string tmp_str(buf, MAX_FIELD_WIDTH, share->access_charset);
2689 tmp_str.init_calc_mem(242);
2690 tmp_str.length(0);
2691 if ((error_num = row->append_to_str(&tmp_str)))
2692 DBUG_RETURN(error_num);
2693#ifdef SPIDER_ITEM_STRING_WITHOUT_SET_STR_WITH_COPY
2694 item->val_str(NULL)->length(0);
2695 item->append((char *) tmp_str.ptr(), tmp_str.length());
2696#else
2697 item->set_str_with_copy(tmp_str.ptr(), tmp_str.length());
2698#endif
2699 item->null_value = FALSE;
2700 }
2701 item_hybrid->direct_add(item);
2702 row->next();
2703 }
2704 break;
2705 case Item_sum::COUNT_DISTINCT_FUNC:
2706 case Item_sum::SUM_DISTINCT_FUNC:
2707 case Item_sum::AVG_FUNC:
2708 case Item_sum::AVG_DISTINCT_FUNC:
2709 case Item_sum::STD_FUNC:
2710 case Item_sum::VARIANCE_FUNC:
2711 case Item_sum::SUM_BIT_FUNC:
2712 case Item_sum::UDF_SUM_FUNC:
2713 case Item_sum::GROUP_CONCAT_FUNC:
2714 default:
2715 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
2716 }
2717 DBUG_RETURN(0);
2718}
2719#endif
2720
2721int spider_db_append_match_fetch(
2722 ha_spider *spider,
2723 st_spider_ft_info *ft_first,
2724 st_spider_ft_info *ft_current,
2725 SPIDER_DB_ROW *row
2726) {
2727 DBUG_ENTER("spider_db_append_match_fetch");
2728 if (ft_current)
2729 {
2730 st_spider_ft_info *ft_info = ft_first;
2731 while (TRUE)
2732 {
2733 DBUG_PRINT("info",("spider ft_info=%p", ft_info));
2734 if (!row->is_null())
2735 ft_info->score = (float) row->val_real();
2736 else
2737 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
2738 row->next();
2739 if (ft_info == ft_current)
2740 break;
2741 ft_info = ft_info->next;
2742 }
2743 }
2744 DBUG_RETURN(0);
2745}
2746
2747int spider_db_append_match_where(
2748 ha_spider *spider
2749) {
2750 int error_num;
2751 DBUG_ENTER("spider_db_append_match_where");
2752 if ((error_num = spider->append_match_where_sql_part(
2753 SPIDER_SQL_TYPE_SELECT_SQL)))
2754 DBUG_RETURN(error_num);
2755
2756 /* use condition */
2757 if ((error_num = spider->append_condition_sql_part(
2758 NULL, 0, SPIDER_SQL_TYPE_SELECT_SQL, FALSE)))
2759 DBUG_RETURN(error_num);
2760
2761 spider->set_order_pos_sql(SPIDER_SQL_TYPE_SELECT_SQL);
2762 DBUG_RETURN(0);
2763}
2764
2765void spider_db_append_handler_next(
2766 ha_spider *spider
2767) {
2768 const char *alias;
2769 uint alias_length;
2770 SPIDER_RESULT_LIST *result_list = &spider->result_list;
2771 DBUG_ENTER("spider_db_append_handler_next");
2772 if (result_list->sorted && result_list->desc_flg)
2773 {
2774 alias = SPIDER_SQL_PREV_STR;
2775 alias_length = SPIDER_SQL_PREV_LEN;
2776 } else {
2777 alias = SPIDER_SQL_NEXT_STR;
2778 alias_length = SPIDER_SQL_NEXT_LEN;
2779 }
2780 spider->set_order_to_pos_sql(SPIDER_SQL_TYPE_HANDLER);
2781 spider->append_key_order_with_alias_sql_part(alias, alias_length,
2782 SPIDER_SQL_TYPE_HANDLER);
2783 DBUG_VOID_RETURN;
2784}
2785
2786void spider_db_get_row_from_tmp_tbl_rec(
2787 SPIDER_RESULT *current,
2788 SPIDER_DB_ROW **row
2789) {
2790 DBUG_ENTER("spider_db_get_row_from_tmp_tbl_rec");
2791 *row = current->result->fetch_row_from_tmp_table(current->result_tmp_tbl);
2792 DBUG_VOID_RETURN;
2793}
2794
2795int spider_db_get_row_from_tmp_tbl(
2796 SPIDER_RESULT *current,
2797 SPIDER_DB_ROW **row
2798) {
2799 int error_num;
2800 DBUG_ENTER("spider_db_get_row_from_tmp_tbl");
2801 if (current->result_tmp_tbl_inited == 2)
2802 {
2803 current->result_tmp_tbl->file->ha_rnd_end();
2804 current->result_tmp_tbl_inited = 0;
2805 }
2806 if (current->result_tmp_tbl_inited == 0)
2807 {
2808 current->result_tmp_tbl->file->extra(HA_EXTRA_CACHE);
2809 if ((error_num = current->result_tmp_tbl->file->ha_rnd_init(TRUE)))
2810 DBUG_RETURN(error_num);
2811 current->result_tmp_tbl_inited = 1;
2812 }
2813 if (
2814#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
2815 (error_num = current->result_tmp_tbl->file->ha_rnd_next(
2816 current->result_tmp_tbl->record[0]))
2817#else
2818 (error_num = current->result_tmp_tbl->file->rnd_next(
2819 current->result_tmp_tbl->record[0]))
2820#endif
2821 ) {
2822 DBUG_RETURN(error_num);
2823 }
2824 spider_db_get_row_from_tmp_tbl_rec(current, row);
2825 DBUG_RETURN(0);
2826}
2827
2828int spider_db_get_row_from_tmp_tbl_pos(
2829 SPIDER_POSITION *pos,
2830 SPIDER_DB_ROW **row
2831) {
2832 int error_num;
2833 SPIDER_RESULT *result = pos->result;
2834 TABLE *tmp_tbl = result->result_tmp_tbl;
2835 DBUG_ENTER("spider_db_get_row_from_tmp_tbl_pos");
2836 if (result->result_tmp_tbl_inited == 1)
2837 {
2838 tmp_tbl->file->ha_rnd_end();
2839 result->result_tmp_tbl_inited = 0;
2840 }
2841 if (result->result_tmp_tbl_inited == 0)
2842 {
2843 if ((error_num = tmp_tbl->file->ha_rnd_init(FALSE)))
2844 DBUG_RETURN(error_num);
2845 result->result_tmp_tbl_inited = 2;
2846 }
2847 if (
2848#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
2849 (error_num = tmp_tbl->file->ha_rnd_pos(tmp_tbl->record[0],
2850 (uchar *) &pos->tmp_tbl_pos))
2851#else
2852 (error_num = tmp_tbl->file->rnd_pos(tmp_tbl->record[0],
2853 (uchar *) &pos->tmp_tbl_pos))
2854#endif
2855 ) {
2856 DBUG_RETURN(error_num);
2857 }
2858 spider_db_get_row_from_tmp_tbl_rec(result, row);
2859 DBUG_RETURN(0);
2860}
2861
2862int spider_db_fetch_row(
2863 SPIDER_SHARE *share,
2864 Field *field,
2865 SPIDER_DB_ROW *row,
2866 my_ptrdiff_t ptr_diff
2867) {
2868 int error_num;
2869 DBUG_ENTER("spider_db_fetch_row");
2870 DBUG_PRINT("info", ("spider field_name %s", field->field_name.str));
2871 DBUG_PRINT("info", ("spider fieldcharset %s", field->charset()->csname));
2872 field->move_field_offset(ptr_diff);
2873 error_num = row->store_to_field(field, share->access_charset);
2874 field->move_field_offset(-ptr_diff);
2875 DBUG_RETURN(error_num);
2876}
2877
2878int spider_db_fetch_table(
2879 ha_spider *spider,
2880 uchar *buf,
2881 TABLE *table,
2882 SPIDER_RESULT_LIST *result_list
2883) {
2884 int error_num;
2885 SPIDER_SHARE *share = spider->share;
2886 my_ptrdiff_t ptr_diff = PTR_BYTE_DIFF(buf, table->record[0]);
2887 SPIDER_RESULT *current = (SPIDER_RESULT*) result_list->current;
2888 SPIDER_DB_ROW *row;
2889 Field **field;
2890 DBUG_ENTER("spider_db_fetch_table");
2891#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2892 if (spider->conn_kind[spider->result_link_idx] == SPIDER_CONN_KIND_MYSQL)
2893 {
2894#endif
2895 if (result_list->quick_mode == 0)
2896 {
2897 SPIDER_DB_RESULT *result = current->result;
2898 if (!(row = result->fetch_row()))
2899 {
2900 table->status = STATUS_NOT_FOUND;
2901 DBUG_RETURN(HA_ERR_END_OF_FILE);
2902 }
2903 } else {
2904 if (result_list->current_row_num < result_list->quick_page_size)
2905 {
2906 row = current->first_position[result_list->current_row_num].row;
2907 } else {
2908 if ((error_num = spider_db_get_row_from_tmp_tbl(
2909 current, &row)))
2910 {
2911 if (error_num == HA_ERR_END_OF_FILE)
2912 table->status = STATUS_NOT_FOUND;
2913 DBUG_RETURN(error_num);
2914 }
2915 }
2916 }
2917
2918 DBUG_PRINT("info", ("spider row=%p", row));
2919#ifdef HANDLER_HAS_DIRECT_AGGREGATE
2920 DBUG_PRINT("info", ("spider direct_aggregate=%s",
2921 result_list->direct_aggregate ? "TRUE" : "FALSE"));
2922 result_list->snap_mrr_with_cnt = spider->mrr_with_cnt;
2923 result_list->snap_direct_aggregate = result_list->direct_aggregate;
2924 result_list->snap_row = row;
2925#endif
2926
2927 /* for mrr */
2928 if (spider->mrr_with_cnt)
2929 {
2930 DBUG_PRINT("info", ("spider mrr_with_cnt"));
2931 if (spider->sql_kind[spider->result_link_idx] == SPIDER_SQL_KIND_SQL)
2932 {
2933 if (!row->is_null())
2934 spider->multi_range_hit_point = row->val_int();
2935#ifdef HANDLER_HAS_DIRECT_AGGREGATE
2936 else if (result_list->direct_aggregate)
2937 {
2938 table->status = STATUS_NOT_FOUND;
2939 DBUG_RETURN(HA_ERR_END_OF_FILE);
2940 }
2941#endif
2942 else
2943 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
2944 row->next();
2945 } else {
2946 spider->multi_range_hit_point = 0;
2947#ifdef HANDLER_HAS_DIRECT_AGGREGATE
2948 result_list->snap_mrr_with_cnt = FALSE;
2949#endif
2950 }
2951 }
2952
2953#ifdef HANDLER_HAS_DIRECT_AGGREGATE
2954 /* for direct_aggregate */
2955 if (result_list->direct_aggregate)
2956 {
2957 if ((error_num = spider_db_fetch_for_item_sum_funcs(row, spider)))
2958 DBUG_RETURN(error_num);
2959 }
2960#endif
2961
2962#ifdef SPIDER_HAS_GROUP_BY_HANDLER
2963 if (!spider->use_fields)
2964 {
2965#endif
2966 if ((error_num = spider_db_append_match_fetch(spider,
2967 spider->ft_first, spider->ft_current, row)))
2968 DBUG_RETURN(error_num);
2969#ifdef SPIDER_HAS_GROUP_BY_HANDLER
2970 }
2971#endif
2972#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2973 } else {
2974 if (!(row = result_list->hs_result->fetch_row_from_result_buffer(
2975 result_list->hs_result_buf)))
2976 {
2977 table->status = STATUS_NOT_FOUND;
2978 DBUG_RETURN(HA_ERR_END_OF_FILE);
2979 }
2980 }
2981#endif
2982
2983#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2984 if (spider->conn_kind[spider->result_link_idx] == SPIDER_CONN_KIND_MYSQL)
2985 {
2986#endif
2987 for (
2988 field = table->field;
2989 *field;
2990 field++
2991 ) {
2992 if ((
2993 bitmap_is_set(table->read_set, (*field)->field_index) |
2994 bitmap_is_set(table->write_set, (*field)->field_index)
2995 )) {
2996#ifndef DBUG_OFF
2997 my_bitmap_map *tmp_map =
2998 dbug_tmp_use_all_columns(table, table->write_set);
2999#endif
3000 DBUG_PRINT("info", ("spider bitmap is set %s",
3001 (*field)->field_name.str));
3002 if ((error_num =
3003 spider_db_fetch_row(share, *field, row, ptr_diff)))
3004 DBUG_RETURN(error_num);
3005#ifndef DBUG_OFF
3006 dbug_tmp_restore_column_map(table->write_set, tmp_map);
3007#endif
3008 }
3009 row->next();
3010 }
3011#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
3012 } else {
3013#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
3014 if (spider->hs_pushed_ret_fields_num == MAX_FIELDS)
3015 {
3016#endif
3017 spider_db_handler *dbton_hdl = spider->dbton_handler[row->dbton_id];
3018 for (
3019 field = table->field;
3020 *field;
3021 field++
3022 ) {
3023 if (dbton_hdl->minimum_select_bit_is_set((*field)->field_index))
3024 {
3025#ifndef DBUG_OFF
3026 my_bitmap_map *tmp_map =
3027 dbug_tmp_use_all_columns(table, table->write_set);
3028#endif
3029 if ((error_num = spider_db_fetch_row(share, *field, row, ptr_diff)))
3030 DBUG_RETURN(error_num);
3031#ifndef DBUG_OFF
3032 dbug_tmp_restore_column_map(table->write_set, tmp_map);
3033#endif
3034 row->next();
3035 }
3036 }
3037#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
3038 } else {
3039 uint32 *field_idxs = spider->hs_pushed_ret_fields;
3040 size_t field_idxs_num = spider->hs_pushed_ret_fields_num;
3041 Field *tf;
3042 int roop_count;
3043 if (spider->hs_pushed_lcl_fields_num !=
3044 result_list->hs_result->num_fields())
3045 {
3046 DBUG_PRINT("info", ("spider different field_num %zu %u",
3047 spider->hs_pushed_lcl_fields_num,
3048 result_list->hs_result->num_fields()));
3049 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
3050 }
3051 for (roop_count = 0; roop_count < (int) field_idxs_num;
3052 roop_count++)
3053 {
3054 tf = spider->get_top_table_field(field_idxs[roop_count]);
3055 if ((tf = spider->field_exchange(tf)))
3056 {
3057#ifndef DBUG_OFF
3058 my_bitmap_map *tmp_map =
3059 dbug_tmp_use_all_columns(table, table->write_set);
3060#endif
3061 if ((error_num = spider_db_fetch_row(share, tf, row, ptr_diff)))
3062 DBUG_RETURN(error_num);
3063#ifndef DBUG_OFF
3064 dbug_tmp_restore_column_map(table->write_set, tmp_map);
3065#endif
3066 row->next();
3067 }
3068 }
3069 }
3070#endif
3071 }
3072#endif
3073 table->status = 0;
3074 DBUG_RETURN(0);
3075}
3076
3077int spider_db_fetch_key(
3078 ha_spider *spider,
3079 uchar *buf,
3080 TABLE *table,
3081 const KEY *key_info,
3082 SPIDER_RESULT_LIST *result_list
3083) {
3084 int error_num;
3085 SPIDER_SHARE *share = spider->share;
3086 my_ptrdiff_t ptr_diff = PTR_BYTE_DIFF(buf, table->record[0]);
3087 SPIDER_RESULT *current = (SPIDER_RESULT*) result_list->current;
3088 KEY_PART_INFO *key_part;
3089 uint part_num;
3090 SPIDER_DB_ROW *row;
3091 Field *field;
3092 DBUG_ENTER("spider_db_fetch_key");
3093 if (result_list->quick_mode == 0)
3094 {
3095 SPIDER_DB_RESULT *result = current->result;
3096 if (!(row = result->fetch_row()))
3097 {
3098 table->status = STATUS_NOT_FOUND;
3099 DBUG_RETURN(HA_ERR_END_OF_FILE);
3100 }
3101 } else {
3102 if (result_list->current_row_num < result_list->quick_page_size)
3103 {
3104 row = current->first_position[result_list->current_row_num].row;
3105 } else {
3106 if ((error_num = spider_db_get_row_from_tmp_tbl(
3107 current, &row)))
3108 {
3109 if (error_num == HA_ERR_END_OF_FILE)
3110 table->status = STATUS_NOT_FOUND;
3111 DBUG_RETURN(error_num);
3112 }
3113 }
3114 }
3115
3116 DBUG_PRINT("info", ("spider row=%p", row));
3117#ifdef HANDLER_HAS_DIRECT_AGGREGATE
3118 DBUG_PRINT("info", ("spider direct_aggregate=%s",
3119 result_list->direct_aggregate ? "TRUE" : "FALSE"));
3120 result_list->snap_mrr_with_cnt = spider->mrr_with_cnt;
3121 result_list->snap_direct_aggregate = result_list->direct_aggregate;
3122 result_list->snap_row = row;
3123#endif
3124
3125 /* for mrr */
3126 if (spider->mrr_with_cnt)
3127 {
3128 DBUG_PRINT("info", ("spider mrr_with_cnt"));
3129 if (!row->is_null())
3130 spider->multi_range_hit_point = row->val_int();
3131#ifdef HANDLER_HAS_DIRECT_AGGREGATE
3132 else if (result_list->direct_aggregate)
3133 {
3134 table->status = STATUS_NOT_FOUND;
3135 DBUG_RETURN(HA_ERR_END_OF_FILE);
3136 }
3137#endif
3138 else
3139 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
3140 row->next();
3141 }
3142
3143#ifdef HANDLER_HAS_DIRECT_AGGREGATE
3144 /* for direct_aggregate */
3145 if (result_list->direct_aggregate)
3146 {
3147 if ((error_num = spider_db_fetch_for_item_sum_funcs(row, spider)))
3148 DBUG_RETURN(error_num);
3149 }
3150#endif
3151
3152 if ((error_num = spider_db_append_match_fetch(spider,
3153 spider->ft_first, spider->ft_current, row)))
3154 DBUG_RETURN(error_num);
3155
3156 for (
3157 key_part = key_info->key_part,
3158 part_num = 0;
3159 part_num < spider_user_defined_key_parts(key_info);
3160 key_part++,
3161 part_num++
3162 ) {
3163 field = key_part->field;
3164 if ((
3165 bitmap_is_set(table->read_set, field->field_index) |
3166 bitmap_is_set(table->write_set, field->field_index)
3167 )) {
3168#ifndef DBUG_OFF
3169 my_bitmap_map *tmp_map =
3170 dbug_tmp_use_all_columns(table, table->write_set);
3171#endif
3172 DBUG_PRINT("info", ("spider bitmap is set %s", field->field_name.str));
3173 if ((error_num =
3174 spider_db_fetch_row(share, field, row, ptr_diff)))
3175 DBUG_RETURN(error_num);
3176#ifndef DBUG_OFF
3177 dbug_tmp_restore_column_map(table->write_set, tmp_map);
3178#endif
3179 }
3180 row->next();
3181 }
3182 table->status = 0;
3183 DBUG_RETURN(0);
3184}
3185
3186int spider_db_fetch_minimum_columns(
3187 ha_spider *spider,
3188 uchar *buf,
3189 TABLE *table,
3190 SPIDER_RESULT_LIST *result_list
3191) {
3192 int error_num;
3193 my_ptrdiff_t ptr_diff = PTR_BYTE_DIFF(buf, table->record[0]);
3194 SPIDER_SHARE *share = spider->share;
3195 SPIDER_RESULT *current = (SPIDER_RESULT*) result_list->current;
3196 SPIDER_DB_ROW *row;
3197 Field **field;
3198 spider_db_handler *dbton_hdl;
3199 DBUG_ENTER("spider_db_fetch_minimum_columns");
3200 if (result_list->quick_mode == 0)
3201 {
3202 SPIDER_DB_RESULT *result = current->result;
3203 if (!(row = result->fetch_row()))
3204 {
3205 table->status = STATUS_NOT_FOUND;
3206 DBUG_RETURN(HA_ERR_END_OF_FILE);
3207 }
3208 } else {
3209 if (result_list->current_row_num < result_list->quick_page_size)
3210 {
3211 DBUG_PRINT("info", ("spider current=%p", current));
3212 DBUG_PRINT("info", ("spider first_position=%p", current->first_position));
3213 DBUG_PRINT("info", ("spider current_row_num=%lld", result_list->current_row_num));
3214 DBUG_PRINT("info", ("spider first_position[]=%p", &current->first_position[result_list->current_row_num]));
3215 row = current->first_position[result_list->current_row_num].row;
3216 } else {
3217 if ((error_num = spider_db_get_row_from_tmp_tbl(
3218 current, &row)))
3219 {
3220 if (error_num == HA_ERR_END_OF_FILE)
3221 table->status = STATUS_NOT_FOUND;
3222 DBUG_RETURN(error_num);
3223 }
3224 }
3225 }
3226
3227 DBUG_PRINT("info", ("spider row=%p", row));
3228#ifdef HANDLER_HAS_DIRECT_AGGREGATE
3229 DBUG_PRINT("info", ("spider direct_aggregate=%s",
3230 result_list->direct_aggregate ? "TRUE" : "FALSE"));
3231 result_list->snap_mrr_with_cnt = spider->mrr_with_cnt;
3232 result_list->snap_direct_aggregate = result_list->direct_aggregate;
3233 result_list->snap_row = row;
3234#endif
3235
3236 /* for mrr */
3237 if (spider->mrr_with_cnt)
3238 {
3239 DBUG_PRINT("info", ("spider mrr_with_cnt"));
3240 if (!row->is_null())
3241 spider->multi_range_hit_point = row->val_int();
3242#ifdef HANDLER_HAS_DIRECT_AGGREGATE
3243 else if (result_list->direct_aggregate)
3244 {
3245 table->status = STATUS_NOT_FOUND;
3246 DBUG_RETURN(HA_ERR_END_OF_FILE);
3247 }
3248#endif
3249 else
3250 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
3251 row->next();
3252 }
3253
3254#ifdef HANDLER_HAS_DIRECT_AGGREGATE
3255 /* for direct_aggregate */
3256 if (result_list->direct_aggregate)
3257 {
3258 if ((error_num = spider_db_fetch_for_item_sum_funcs(row, spider)))
3259 DBUG_RETURN(error_num);
3260 }
3261#endif
3262
3263 if ((error_num = spider_db_append_match_fetch(spider,
3264 spider->ft_first, spider->ft_current, row)))
3265 DBUG_RETURN(error_num);
3266
3267 dbton_hdl = spider->dbton_handler[row->dbton_id];
3268 for (
3269 field = table->field;
3270 *field;
3271 field++
3272 ) {
3273 DBUG_PRINT("info", ("spider field_index %u", (*field)->field_index));
3274 DBUG_PRINT("info", ("spider searched_bitmap %u",
3275 spider_bit_is_set(spider->searched_bitmap, (*field)->field_index)));
3276 DBUG_PRINT("info", ("spider read_set %u",
3277 bitmap_is_set(table->read_set, (*field)->field_index)));
3278 DBUG_PRINT("info", ("spider write_set %u",
3279 bitmap_is_set(table->write_set, (*field)->field_index)));
3280 if (dbton_hdl->minimum_select_bit_is_set((*field)->field_index))
3281 {
3282 if ((
3283 bitmap_is_set(table->read_set, (*field)->field_index) |
3284 bitmap_is_set(table->write_set, (*field)->field_index)
3285 )) {
3286#ifndef DBUG_OFF
3287 my_bitmap_map *tmp_map =
3288 dbug_tmp_use_all_columns(table, table->write_set);
3289#endif
3290 DBUG_PRINT("info", ("spider bitmap is set %s",
3291 (*field)->field_name.str));
3292 if ((error_num = spider_db_fetch_row(share, *field, row, ptr_diff)))
3293 DBUG_RETURN(error_num);
3294#ifndef DBUG_OFF
3295 dbug_tmp_restore_column_map(table->write_set, tmp_map);
3296#endif
3297 }
3298 row->next();
3299 }
3300 }
3301 table->status = 0;
3302 DBUG_RETURN(0);
3303}
3304
3305void spider_db_free_one_result_for_start_next(
3306 ha_spider *spider
3307) {
3308 SPIDER_RESULT_LIST *result_list = &spider->result_list;
3309 SPIDER_RESULT *result = (SPIDER_RESULT *) result_list->current;
3310 DBUG_ENTER("spider_db_free_one_result_for_start_next");
3311 spider_bg_all_conn_break(spider);
3312
3313#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
3314 if (spider->conn_kind[spider->result_link_idx] == SPIDER_CONN_KIND_MYSQL)
3315 {
3316#endif
3317 if (result_list->low_mem_read)
3318 {
3319 if (result)
3320 {
3321 do {
3322 spider_db_free_one_result(result_list, result);
3323 DBUG_PRINT("info",("spider result=%p", result));
3324 DBUG_PRINT("info",("spider result->finish_flg = FALSE"));
3325 result->finish_flg = FALSE;
3326 result = (SPIDER_RESULT *) result->next;
3327 } while (result && (result->result || result->first_position));
3328 result = (SPIDER_RESULT *) result_list->current;
3329 if (
3330 !result->result &&
3331 !result->first_position &&
3332 !result->tmp_tbl_use_position
3333 )
3334 result_list->current = result->prev;
3335 }
3336 } else {
3337 while (
3338 result && result->next &&
3339 (result->next->result || result->next->first_position)
3340 ) {
3341 result_list->current = result->next;
3342 result = (SPIDER_RESULT *) result->next;
3343 }
3344 }
3345#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
3346 } else {
3347 if (result_list->hs_has_result)
3348 {
3349 if (result_list->hs_result)
3350 {
3351 result_list->hs_result->free_result();
3352 delete result_list->hs_result;
3353 result_list->hs_result = NULL;
3354 }
3355 if (result_list->hs_result_buf)
3356 {
3357 result_list->hs_result_buf->clear();
3358 }
3359 result_list->hs_has_result = FALSE;
3360 }
3361 }
3362#endif
3363 DBUG_VOID_RETURN;
3364}
3365
3366void spider_db_free_one_result(
3367 SPIDER_RESULT_LIST *result_list,
3368 SPIDER_RESULT *result
3369) {
3370 DBUG_ENTER("spider_db_free_one_result");
3371 if (result_list->quick_mode == 0)
3372 {
3373 if (
3374 !result->use_position &&
3375 result->result
3376 ) {
3377 result->result->free_result();
3378 delete result->result;
3379 result->result = NULL;
3380 }
3381 } else {
3382 int roop_count;
3383 SPIDER_POSITION *position = result->first_position;
3384 if (position)
3385 {
3386 for (roop_count = 0; roop_count < result->pos_page_size; roop_count++)
3387 {
3388 if (
3389 position[roop_count].row &&
3390 !position[roop_count].use_position
3391 ) {
3392 delete position[roop_count].row;
3393 position[roop_count].row = NULL;
3394 }
3395 }
3396 if (result_list->quick_mode == 3)
3397 {
3398 if (!result->first_pos_use_position)
3399 {
3400 spider_free(spider_current_trx, position, MYF(0));
3401 result->first_position = NULL;
3402 }
3403 if (result->result)
3404 {
3405 result->result->free_result();
3406 if (!result->tmp_tbl_use_position)
3407 {
3408 delete result->result;
3409 result->result = NULL;
3410 }
3411 }
3412 if (!result->tmp_tbl_use_position)
3413 {
3414 if (result->result_tmp_tbl)
3415 {
3416 if (result->result_tmp_tbl_inited)
3417 {
3418 result->result_tmp_tbl->file->ha_rnd_end();
3419 result->result_tmp_tbl_inited = 0;
3420 }
3421 spider_rm_sys_tmp_table_for_result(result->result_tmp_tbl_thd,
3422 result->result_tmp_tbl, &result->result_tmp_tbl_prm);
3423 result->result_tmp_tbl = NULL;
3424 result->result_tmp_tbl_thd = NULL;
3425 }
3426 }
3427 }
3428 }
3429 }
3430 DBUG_VOID_RETURN;
3431}
3432
3433int spider_db_free_result(
3434 ha_spider *spider,
3435 bool final
3436) {
3437 SPIDER_RESULT_LIST *result_list = &spider->result_list;
3438 SPIDER_RESULT *result;
3439 SPIDER_RESULT *prev;
3440 SPIDER_SHARE *share = spider->share;
3441 SPIDER_TRX *trx = spider->trx;
3442 SPIDER_POSITION *position;
3443 int roop_count, error_num;
3444 DBUG_ENTER("spider_db_free_result");
3445 spider_bg_all_conn_break(spider);
3446 result = (SPIDER_RESULT*) result_list->first;
3447
3448 while (result_list->tmp_pos_row_first)
3449 {
3450 SPIDER_DB_ROW *tmp_pos_row = result_list->tmp_pos_row_first;
3451 result_list->tmp_pos_row_first = tmp_pos_row->next_pos;
3452 delete tmp_pos_row;
3453 }
3454
3455#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
3456 if (result_list->hs_has_result)
3457 {
3458 if (result_list->hs_result)
3459 {
3460 result_list->hs_result->free_result();
3461 delete result_list->hs_result;
3462 result_list->hs_result = NULL;
3463 }
3464 if (result_list->hs_result_buf)
3465 {
3466 if (result_list->hs_result_buf->check_size(
3467 spider_param_hs_result_free_size(trx->thd, share->hs_result_free_size))
3468 ) {
3469 trx->hs_result_free_count++;
3470 }
3471 result_list->hs_result_buf->clear();
3472 }
3473 result_list->hs_has_result = FALSE;
3474 }
3475#endif
3476
3477 if (
3478 final ||
3479 spider_param_reset_sql_alloc(trx->thd, share->reset_sql_alloc) == 1
3480 ) {
3481 int alloc_size = final ? 0 :
3482 (spider_param_init_sql_alloc_size(trx->thd, share->init_sql_alloc_size));
3483 while (result)
3484 {
3485 position = result->first_position;
3486 if (position)
3487 {
3488 for (roop_count = 0; roop_count < result->pos_page_size; roop_count++)
3489 {
3490 if (position[roop_count].row)
3491 {
3492 delete position[roop_count].row;
3493 }
3494 }
3495 spider_free(spider_current_trx, position, MYF(0));
3496 }
3497 if (result->result)
3498 {
3499 result->result->free_result();
3500 delete result->result;
3501 result->result = NULL;
3502 }
3503 if (result->result_tmp_tbl)
3504 {
3505 if (result->result_tmp_tbl_inited)
3506 {
3507 result->result_tmp_tbl->file->ha_rnd_end();
3508 result->result_tmp_tbl_inited = 0;
3509 }
3510 spider_rm_sys_tmp_table_for_result(result->result_tmp_tbl_thd,
3511 result->result_tmp_tbl, &result->result_tmp_tbl_prm);
3512 result->result_tmp_tbl = NULL;
3513 result->result_tmp_tbl_thd = NULL;
3514 }
3515 prev = result;
3516 result = (SPIDER_RESULT*) result->next;
3517 spider_free(spider_current_trx, prev, MYF(0));
3518 }
3519 result_list->first = NULL;
3520 result_list->last = NULL;
3521 if (!final)
3522 {
3523 ulong realloced = 0;
3524 int init_sql_alloc_size =
3525 spider_param_init_sql_alloc_size(trx->thd, share->init_sql_alloc_size);
3526 for (roop_count = 0; roop_count < (int) share->use_dbton_count;
3527 roop_count++)
3528 {
3529 uint dbton_id = share->use_dbton_ids[roop_count];
3530 if ((error_num = spider->dbton_handler[dbton_id]->
3531 realloc_sql(&realloced)))
3532 {
3533 DBUG_RETURN(error_num);
3534 }
3535 }
3536 if (realloced & (SPIDER_SQL_TYPE_SELECT_SQL | SPIDER_SQL_TYPE_HANDLER))
3537 {
3538 for (roop_count = 0; roop_count < (int) share->link_count;
3539 roop_count++)
3540 {
3541 if ((int) result_list->sqls[roop_count].alloced_length() >
3542 alloc_size * 2)
3543 {
3544 result_list->sqls[roop_count].free();
3545 if (result_list->sqls[roop_count].real_alloc(
3546 init_sql_alloc_size))
3547 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3548 }
3549 }
3550 }
3551 if (realloced & SPIDER_SQL_TYPE_INSERT_SQL)
3552 {
3553 for (roop_count = 0; roop_count < (int) share->link_count;
3554 roop_count++)
3555 {
3556 if ((int) result_list->insert_sqls[roop_count].alloced_length() >
3557 alloc_size * 2)
3558 {
3559 result_list->insert_sqls[roop_count].free();
3560 if (result_list->insert_sqls[roop_count].real_alloc(
3561 init_sql_alloc_size))
3562 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3563 }
3564 }
3565 }
3566 if (realloced & SPIDER_SQL_TYPE_UPDATE_SQL)
3567 {
3568 for (roop_count = 0; roop_count < (int) share->link_count;
3569 roop_count++)
3570 {
3571 if ((int) result_list->update_sqls[roop_count].alloced_length() >
3572 alloc_size * 2)
3573 {
3574 result_list->update_sqls[roop_count].free();
3575 if (result_list->update_sqls[roop_count].real_alloc(
3576 init_sql_alloc_size))
3577 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3578 }
3579 }
3580 }
3581 if ((error_num = spider->reset_sql_sql(SPIDER_SQL_TYPE_BULK_UPDATE_SQL)))
3582 DBUG_RETURN(error_num);
3583
3584 if (realloced & SPIDER_SQL_TYPE_TMP_SQL)
3585 {
3586 for (roop_count = 0; roop_count < (int) share->link_count;
3587 roop_count++)
3588 {
3589 if ((int) result_list->tmp_sqls[roop_count].alloced_length() >
3590 alloc_size * 2)
3591 {
3592 result_list->tmp_sqls[roop_count].free();
3593 if (result_list->tmp_sqls[roop_count].real_alloc(
3594 init_sql_alloc_size))
3595 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3596 }
3597 }
3598 }
3599 }
3600 } else {
3601 while (result)
3602 {
3603 position = result->first_position;
3604 if (position)
3605 {
3606 for (roop_count = 0; roop_count < result->pos_page_size; roop_count++)
3607 {
3608 if (position[roop_count].row)
3609 {
3610 delete position[roop_count].row;
3611 }
3612 }
3613 spider_free(spider_current_trx, position, MYF(0));
3614 }
3615 result->first_position = NULL;
3616 if (result->result)
3617 {
3618 result->result->free_result();
3619 delete result->result;
3620 result->result = NULL;
3621 }
3622 if (result->result_tmp_tbl)
3623 {
3624 if (result->result_tmp_tbl_inited)
3625 {
3626 result->result_tmp_tbl->file->ha_rnd_end();
3627 result->result_tmp_tbl_inited = 0;
3628 }
3629 spider_rm_sys_tmp_table_for_result(result->result_tmp_tbl_thd,
3630 result->result_tmp_tbl, &result->result_tmp_tbl_prm);
3631 result->result_tmp_tbl = NULL;
3632 result->result_tmp_tbl_thd = NULL;
3633 }
3634 result->record_num = 0;
3635 DBUG_PRINT("info",("spider result->finish_flg = FALSE"));
3636 result->finish_flg = FALSE;
3637 result->first_pos_use_position = FALSE;
3638 result->tmp_tbl_use_position = FALSE;
3639 result->use_position = FALSE;
3640 result = (SPIDER_RESULT*) result->next;
3641 }
3642 }
3643 result_list->current = NULL;
3644 result_list->record_num = 0;
3645 DBUG_PRINT("info",("spider result_list->finish_flg = FALSE"));
3646 result_list->finish_flg = FALSE;
3647 result_list->quick_phase = 0;
3648#ifndef WITHOUT_SPIDER_BG_SEARCH
3649 result_list->bgs_phase = 0;
3650#endif
3651 DBUG_RETURN(0);
3652}
3653
3654int spider_db_store_result(
3655 ha_spider *spider,
3656 int link_idx,
3657 TABLE *table
3658) {
3659 int error_num;
3660 SPIDER_CONN *conn;
3661 SPIDER_DB_CONN *db_conn;
3662 SPIDER_RESULT_LIST *result_list = &spider->result_list;
3663 SPIDER_RESULT *current;
3664 DBUG_ENTER("spider_db_store_result");
3665#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
3666 if (spider->conn_kind[link_idx] == SPIDER_CONN_KIND_MYSQL)
3667 {
3668#endif
3669 conn = spider->conns[link_idx];
3670 DBUG_PRINT("info",("spider conn->connection_id=%llu",
3671 conn->connection_id));
3672 DBUG_PRINT("info",("spider spider->connection_ids[%d]=%llu",
3673 link_idx, spider->connection_ids[link_idx]));
3674 if (conn->connection_id != spider->connection_ids[link_idx])
3675 {
3676 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
3677 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
3678 if (!conn->mta_conn_mutex_unlock_later)
3679 {
3680 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
3681 pthread_mutex_unlock(&conn->mta_conn_mutex);
3682 }
3683 DBUG_RETURN(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM);
3684 }
3685 db_conn = conn->db_conn;
3686 if (!result_list->current)
3687 {
3688 if (!result_list->first)
3689 {
3690 if (!(result_list->first = (SPIDER_RESULT *)
3691 spider_malloc(spider_current_trx, 4, sizeof(*result_list->first),
3692 MYF(MY_WME | MY_ZEROFILL)))
3693 ) {
3694 if (!conn->mta_conn_mutex_unlock_later)
3695 {
3696 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
3697 pthread_mutex_unlock(&conn->mta_conn_mutex);
3698 }
3699 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3700 }
3701 TMP_TABLE_PARAM *tmp_tbl_prm = (TMP_TABLE_PARAM *)
3702 &result_list->first->result_tmp_tbl_prm;
3703 tmp_tbl_prm->init();
3704 tmp_tbl_prm->field_count = 3;
3705 result_list->last = result_list->first;
3706 result_list->current = result_list->first;
3707 } else {
3708 result_list->current = result_list->first;
3709 }
3710 result_list->bgs_current = result_list->current;
3711 current = (SPIDER_RESULT*) result_list->current;
3712 } else {
3713 if (
3714#ifndef WITHOUT_SPIDER_BG_SEARCH
3715 result_list->bgs_phase > 0 ||
3716#endif
3717 result_list->quick_phase > 0
3718 ) {
3719 if (result_list->bgs_current == result_list->last)
3720 {
3721 if (!(result_list->last = (SPIDER_RESULT *)
3722 spider_malloc(spider_current_trx, 5, sizeof(*result_list->last),
3723 MYF(MY_WME | MY_ZEROFILL)))
3724 ) {
3725 if (!conn->mta_conn_mutex_unlock_later)
3726 {
3727 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
3728 pthread_mutex_unlock(&conn->mta_conn_mutex);
3729 }
3730 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3731 }
3732 TMP_TABLE_PARAM *tmp_tbl_prm = (TMP_TABLE_PARAM *)
3733 &result_list->last->result_tmp_tbl_prm;
3734 tmp_tbl_prm->init();
3735 tmp_tbl_prm->field_count = 3;
3736 result_list->bgs_current->next = result_list->last;
3737 result_list->last->prev = result_list->bgs_current;
3738 result_list->bgs_current = result_list->last;
3739 } else {
3740 result_list->bgs_current = result_list->bgs_current->next;
3741 }
3742 if (
3743#ifndef WITHOUT_SPIDER_BG_SEARCH
3744 result_list->bgs_phase == 1 ||
3745#endif
3746 result_list->quick_phase == 2
3747 ) {
3748 if (result_list->low_mem_read)
3749 {
3750 do {
3751 spider_db_free_one_result(result_list,
3752 (SPIDER_RESULT*) result_list->current);
3753 result_list->current = result_list->current->next;
3754 } while (result_list->current != result_list->bgs_current);
3755 } else {
3756 result_list->current = result_list->bgs_current;
3757 }
3758 result_list->quick_phase = 0;
3759 }
3760 current = (SPIDER_RESULT*) result_list->bgs_current;
3761 } else {
3762 if (result_list->current == result_list->last)
3763 {
3764 if (!(result_list->last = (SPIDER_RESULT *)
3765 spider_malloc(spider_current_trx, 6, sizeof(*result_list->last),
3766 MYF(MY_WME | MY_ZEROFILL)))
3767 ) {
3768 if (!conn->mta_conn_mutex_unlock_later)
3769 {
3770 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
3771 pthread_mutex_unlock(&conn->mta_conn_mutex);
3772 }
3773 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3774 }
3775 TMP_TABLE_PARAM *tmp_tbl_prm = (TMP_TABLE_PARAM *)
3776 &result_list->last->result_tmp_tbl_prm;
3777 tmp_tbl_prm->init();
3778 tmp_tbl_prm->field_count = 3;
3779 result_list->current->next = result_list->last;
3780 result_list->last->prev = result_list->current;
3781 result_list->current = result_list->last;
3782 } else {
3783 result_list->current = result_list->current->next;
3784 }
3785 result_list->bgs_current = result_list->current;
3786 current = (SPIDER_RESULT*) result_list->current;
3787 }
3788 }
3789
3790 if (result_list->quick_mode == 0)
3791 {
3792 if (spider_bit_is_set(spider->db_request_phase, link_idx))
3793 {
3794 spider_clear_bit(spider->db_request_phase, link_idx);
3795 }
3796 st_spider_db_request_key request_key;
3797 request_key.spider_thread_id = spider->trx->spider_thread_id;
3798 request_key.query_id = spider->trx->thd->query_id;
3799 request_key.handler = spider;
3800 request_key.request_id = spider->db_request_id[link_idx];
3801 request_key.next = NULL;
3802 if (!(current->result = db_conn->store_result(NULL, &request_key,
3803 &error_num)))
3804 {
3805 if (error_num && error_num != HA_ERR_END_OF_FILE)
3806 {
3807 if (!conn->mta_conn_mutex_unlock_later)
3808 {
3809 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
3810 pthread_mutex_unlock(&conn->mta_conn_mutex);
3811 }
3812 DBUG_RETURN(error_num);
3813 }
3814 bool call_db_errorno = FALSE;
3815 if (error_num != HA_ERR_END_OF_FILE)
3816 {
3817 call_db_errorno = TRUE;
3818 if ((error_num = spider_db_errorno(conn)))
3819 DBUG_RETURN(error_num);
3820 }
3821 DBUG_PRINT("info",("spider set finish_flg point 1"));
3822 DBUG_PRINT("info",("spider current->finish_flg = TRUE"));
3823 DBUG_PRINT("info",("spider result_list->finish_flg = TRUE"));
3824 current->finish_flg = TRUE;
3825 result_list->finish_flg = TRUE;
3826#ifndef WITHOUT_SPIDER_BG_SEARCH
3827 if (result_list->bgs_phase <= 1)
3828 {
3829#endif
3830 result_list->current_row_num = 0;
3831 table->status = STATUS_NOT_FOUND;
3832#ifndef WITHOUT_SPIDER_BG_SEARCH
3833 }
3834#endif
3835 if (!conn->mta_conn_mutex_unlock_later && !call_db_errorno)
3836 {
3837 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
3838 pthread_mutex_unlock(&conn->mta_conn_mutex);
3839 }
3840 DBUG_RETURN(HA_ERR_END_OF_FILE);
3841 } else {
3842 if (!conn->mta_conn_mutex_unlock_later)
3843 {
3844 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
3845 pthread_mutex_unlock(&conn->mta_conn_mutex);
3846 }
3847 current->record_num = current->result->num_rows();
3848 current->dbton_id = current->result->dbton_id;
3849 result_list->record_num += current->record_num;
3850 DBUG_PRINT("info",("spider current->record_num=%lld",
3851 current->record_num));
3852 DBUG_PRINT("info",("spider result_list->record_num=%lld",
3853 result_list->record_num));
3854 DBUG_PRINT("info",("spider result_list->internal_limit=%lld",
3855 result_list->internal_limit));
3856 DBUG_PRINT("info",("spider result_list->split_read=%lld",
3857 result_list->split_read));
3858 if (
3859 result_list->internal_limit <= result_list->record_num ||
3860 result_list->split_read > current->record_num
3861 ) {
3862 DBUG_PRINT("info",("spider set finish_flg point 2"));
3863 DBUG_PRINT("info",("spider current->finish_flg = TRUE"));
3864 DBUG_PRINT("info",("spider result_list->finish_flg = TRUE"));
3865 current->finish_flg = TRUE;
3866 result_list->finish_flg = TRUE;
3867 }
3868#ifndef WITHOUT_SPIDER_BG_SEARCH
3869 if (result_list->bgs_phase <= 1)
3870 {
3871#endif
3872 result_list->current_row_num = 0;
3873#ifndef WITHOUT_SPIDER_BG_SEARCH
3874 }
3875#endif
3876 }
3877 } else {
3878 /* has_result() for case of result with result_tmp_tbl */
3879 if (current->prev && current->prev->result &&
3880 current->prev->result->has_result())
3881 {
3882 current->result = current->prev->result;
3883 current->prev->result = NULL;
3884 result_list->limit_num -= current->prev->record_num;
3885 if (!conn->mta_conn_mutex_unlock_later)
3886 {
3887 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
3888 pthread_mutex_unlock(&conn->mta_conn_mutex);
3889 }
3890 } else {
3891 if (spider_bit_is_set(spider->db_request_phase, link_idx))
3892 {
3893 spider_clear_bit(spider->db_request_phase, link_idx);
3894 }
3895 st_spider_db_request_key request_key;
3896 request_key.spider_thread_id = spider->trx->spider_thread_id;
3897 request_key.query_id = spider->trx->thd->query_id;
3898 request_key.handler = spider;
3899 request_key.request_id = spider->db_request_id[link_idx];
3900 request_key.next = NULL;
3901 if (!(current->result = conn->db_conn->use_result(&request_key,
3902 &error_num)))
3903 {
3904 if (!error_num)
3905 {
3906 error_num = spider_db_errorno(conn);
3907 } else {
3908 if (!conn->mta_conn_mutex_unlock_later)
3909 {
3910 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
3911 pthread_mutex_unlock(&conn->mta_conn_mutex);
3912 }
3913 }
3914 DBUG_RETURN(error_num);
3915 }
3916 DBUG_PRINT("info", ("spider conn[%p]->quick_target=%p", conn, spider));
3917 conn->quick_target = spider;
3918 spider->quick_targets[link_idx] = spider;
3919 if (!conn->mta_conn_mutex_unlock_later)
3920 {
3921 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
3922 pthread_mutex_unlock(&conn->mta_conn_mutex);
3923 }
3924 }
3925 current->dbton_id = current->result->dbton_id;
3926 SPIDER_DB_ROW *row;
3927 if (!(row = current->result->fetch_row()))
3928 {
3929 error_num = current->result->get_errno();
3930 DBUG_PRINT("info",("spider set finish_flg point 3"));
3931 DBUG_PRINT("info",("spider current->finish_flg = TRUE"));
3932 DBUG_PRINT("info",("spider result_list->finish_flg = TRUE"));
3933 current->finish_flg = TRUE;
3934 result_list->finish_flg = TRUE;
3935 current->result->free_result();
3936 delete current->result;
3937 current->result = NULL;
3938 DBUG_PRINT("info", ("spider conn[%p]->quick_target=NULL", conn));
3939 conn->quick_target = NULL;
3940 spider->quick_targets[link_idx] = NULL;
3941 if (
3942#ifndef WITHOUT_SPIDER_BG_SEARCH
3943 result_list->bgs_phase <= 1 &&
3944#endif
3945 result_list->quick_phase == 0
3946 ) {
3947 result_list->current_row_num = 0;
3948 table->status = STATUS_NOT_FOUND;
3949 }
3950 if (error_num)
3951 DBUG_RETURN(error_num);
3952 else if (result_list->quick_phase > 0)
3953 DBUG_RETURN(0);
3954 DBUG_RETURN(HA_ERR_END_OF_FILE);
3955 }
3956 SPIDER_DB_ROW *tmp_row;
3957 uint field_count = current->result->num_fields();
3958 SPIDER_POSITION *position;
3959 longlong page_size =
3960 !result_list->quick_page_size ||
3961 result_list->limit_num < result_list->quick_page_size ?
3962 result_list->limit_num : result_list->quick_page_size;
3963 int roop_count = 0;
3964 current->field_count = field_count;
3965 if (!(position = (SPIDER_POSITION *)
3966 spider_bulk_malloc(spider_current_trx, 7, MYF(MY_WME | MY_ZEROFILL),
3967 &position, sizeof(SPIDER_POSITION) * page_size,
3968 &tmp_row, sizeof(char*) * field_count,
3969 NullS))
3970 )
3971 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3972 current->pos_page_size = (int) page_size;
3973 current->first_position = position;
3974 current->tmp_tbl_row = tmp_row;
3975 do {
3976 if (!(position->row = row->clone()))
3977 {
3978 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3979 }
3980 position++;
3981 roop_count++;
3982 } while (
3983 page_size > roop_count &&
3984 (row = current->result->fetch_row())
3985 );
3986 if (
3987 result_list->quick_mode == 3 &&
3988 page_size == roop_count &&
3989 result_list->limit_num > roop_count &&
3990 (row = current->result->fetch_row())
3991 ) {
3992 THD *thd = current_thd;
3993 char buf[MAX_FIELD_WIDTH];
3994 spider_string tmp_str(buf, MAX_FIELD_WIDTH, &my_charset_bin);
3995 tmp_str.init_calc_mem(120);
3996
3997 DBUG_PRINT("info",("spider store result to temporary table"));
3998 DBUG_ASSERT(!current->result_tmp_tbl);
3999 if (!(current->result_tmp_tbl = spider_mk_sys_tmp_table_for_result(
4000 thd, table, &current->result_tmp_tbl_prm, "a", "b", "c",
4001 &my_charset_bin)))
4002 {
4003 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4004 }
4005 current->result_tmp_tbl_thd = thd;
4006 TABLE *tmp_tbl = current->result_tmp_tbl;
4007 tmp_tbl->file->extra(HA_EXTRA_WRITE_CACHE);
4008 tmp_tbl->file->ha_start_bulk_insert((ha_rows) 0);
4009 do {
4010 if ((error_num = row->store_to_tmp_table(tmp_tbl, &tmp_str)))
4011 {
4012 tmp_tbl->file->ha_end_bulk_insert();
4013 DBUG_RETURN(error_num);
4014 }
4015 roop_count++;
4016 } while (
4017 result_list->limit_num > roop_count &&
4018 (row = current->result->fetch_row())
4019 );
4020 tmp_tbl->file->ha_end_bulk_insert();
4021 page_size = result_list->limit_num;
4022 }
4023 current->record_num = roop_count;
4024 result_list->record_num += roop_count;
4025 if (
4026 result_list->internal_limit <= result_list->record_num ||
4027 page_size > roop_count
4028 ) {
4029 DBUG_PRINT("info",("spider set finish_flg point 4"));
4030 DBUG_PRINT("info",("spider current->finish_flg = TRUE"));
4031 DBUG_PRINT("info",("spider result_list->finish_flg = TRUE"));
4032 current->finish_flg = TRUE;
4033 result_list->finish_flg = TRUE;
4034 current->result->free_result();
4035 if (!current->result_tmp_tbl)
4036 {
4037 delete current->result;
4038 current->result = NULL;
4039 }
4040 DBUG_PRINT("info", ("spider conn[%p]->quick_target=NULL", conn));
4041 conn->quick_target = NULL;
4042 spider->quick_targets[link_idx] = NULL;
4043 } else if (
4044 result_list->quick_mode == 3 ||
4045 result_list->limit_num == roop_count
4046 ) {
4047 current->result->free_result();
4048 if (!current->result_tmp_tbl)
4049 {
4050 delete current->result;
4051 current->result = NULL;
4052 }
4053 DBUG_PRINT("info", ("spider conn[%p]->quick_target=NULL", conn));
4054 conn->quick_target = NULL;
4055 spider->quick_targets[link_idx] = NULL;
4056 }
4057#ifndef WITHOUT_SPIDER_BG_SEARCH
4058 DBUG_PRINT("info", ("spider bgs_phase=%d", result_list->bgs_phase));
4059#endif
4060 DBUG_PRINT("info", ("spider quick_phase=%d", result_list->quick_phase));
4061 if (
4062#ifndef WITHOUT_SPIDER_BG_SEARCH
4063 result_list->bgs_phase <= 1 &&
4064#endif
4065 result_list->quick_phase == 0
4066 ) {
4067 result_list->current_row_num = 0;
4068 }
4069 DBUG_PRINT("info", ("spider result_list->current=%p", result_list->current));
4070 DBUG_PRINT("info", ("spider current=%p", current));
4071 DBUG_PRINT("info", ("spider first_position=%p", current->first_position));
4072 DBUG_PRINT("info", ("spider current_row_num=%lld", result_list->current_row_num));
4073 DBUG_PRINT("info", ("spider first_position[]=%p", &current->first_position[result_list->current_row_num]));
4074 DBUG_PRINT("info", ("spider row=%p", current->first_position[result_list->current_row_num].row));
4075 }
4076#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4077 } else {
4078 if (spider->conn_kind[link_idx] == SPIDER_CONN_KIND_HS_READ)
4079 conn = spider->hs_r_conns[link_idx];
4080 else
4081 conn = spider->hs_w_conns[link_idx];
4082 DBUG_PRINT("info",("spider conn=%p", conn));
4083 DBUG_PRINT("info",("spider conn->connection_id=%llu",
4084 conn->connection_id));
4085 DBUG_PRINT("info",("spider spider->connection_ids[%d]=%llu",
4086 link_idx, spider->connection_ids[link_idx]));
4087 if (conn->connection_id != spider->connection_ids[link_idx])
4088 {
4089 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
4090 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
4091 if (!conn->mta_conn_mutex_unlock_later)
4092 {
4093 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
4094 pthread_mutex_unlock(&conn->mta_conn_mutex);
4095 }
4096 DBUG_RETURN(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM);
4097 }
4098 if (spider_bit_is_set(spider->db_request_phase, link_idx))
4099 {
4100 spider_clear_bit(spider->db_request_phase, link_idx);
4101 }
4102 st_spider_db_request_key request_key;
4103 request_key.spider_thread_id = spider->trx->spider_thread_id;
4104 request_key.query_id = spider->trx->thd->query_id;
4105 request_key.handler = spider;
4106 request_key.request_id = spider->db_request_id[link_idx];
4107 request_key.next = NULL;
4108 if (!(result_list->hs_result = conn->db_conn->store_result(
4109 &result_list->hs_result_buf, &request_key, &error_num)))
4110 {
4111 if (!error_num)
4112 {
4113 spider_db_errorno(conn);
4114 DBUG_RETURN(ER_SPIDER_HS_NUM);
4115 } else {
4116 if (!conn->mta_conn_mutex_unlock_later)
4117 {
4118 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
4119 pthread_mutex_unlock(&conn->mta_conn_mutex);
4120 }
4121 }
4122 DBUG_RETURN(error_num);
4123 }
4124 result_list->hs_conn = conn->db_conn;
4125 result_list->hs_has_result = TRUE;
4126 if (!conn->mta_conn_mutex_unlock_later)
4127 {
4128 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
4129 pthread_mutex_unlock(&conn->mta_conn_mutex);
4130 }
4131 }
4132#endif
4133 DBUG_RETURN(0);
4134}
4135
4136void spider_db_discard_result(
4137 ha_spider *spider,
4138 int link_idx,
4139 SPIDER_CONN *conn
4140) {
4141 int error_num;
4142 SPIDER_DB_RESULT *result;
4143 DBUG_ENTER("spider_db_discard_result");
4144 if (spider_bit_is_set(spider->db_request_phase, link_idx))
4145 {
4146 spider_clear_bit(spider->db_request_phase, link_idx);
4147 }
4148 st_spider_db_request_key request_key;
4149 request_key.spider_thread_id = spider->trx->spider_thread_id;
4150 request_key.query_id = spider->trx->thd->query_id;
4151 request_key.handler = spider;
4152 request_key.request_id = spider->db_request_id[link_idx];
4153 request_key.next = NULL;
4154 if ((result = conn->db_conn->use_result(&request_key, &error_num)))
4155 {
4156 result->free_result();
4157 delete result;
4158 }
4159 DBUG_VOID_RETURN;
4160}
4161
4162void spider_db_discard_multiple_result(
4163 ha_spider *spider,
4164 int link_idx,
4165 SPIDER_CONN *conn
4166) {
4167 int error_num;
4168 SPIDER_DB_RESULT *result;
4169 st_spider_db_request_key request_key;
4170 DBUG_ENTER("spider_db_discard_multiple_result");
4171 if (spider_bit_is_set(spider->db_request_phase, link_idx))
4172 {
4173 spider_clear_bit(spider->db_request_phase, link_idx);
4174 }
4175 request_key.spider_thread_id = spider->trx->spider_thread_id;
4176 request_key.query_id = spider->trx->thd->query_id;
4177 request_key.handler = spider;
4178 request_key.request_id = spider->db_request_id[link_idx];
4179 request_key.next = NULL;
4180 do
4181 {
4182 if (!conn->db_conn->cmp_request_key_to_snd(&request_key))
4183 break;
4184 if ((result = conn->db_conn->use_result(&request_key, &error_num)))
4185 {
4186 result->free_result();
4187 delete result;
4188 }
4189 } while (!conn->db_conn->next_result());
4190 DBUG_VOID_RETURN;
4191}
4192
4193#ifdef HA_CAN_BULK_ACCESS
4194int spider_db_bulk_store_result(
4195 ha_spider *spider,
4196 SPIDER_CONN *conn,
4197 int link_idx,
4198 bool discard_result
4199) {
4200 int error_num, tmp_error_num;
4201 DBUG_ENTER("spider_db_bulk_store_result");
4202 DBUG_PRINT("info",("spider spider=%p", spider));
4203 DBUG_PRINT("info",("spider conn=%p", conn));
4204 DBUG_PRINT("info",("spider link_idx=%d", link_idx));
4205 if (conn->conn_kind == SPIDER_CONN_KIND_MYSQL)
4206 {
4207 /* already stored */
4208 DBUG_RETURN(0);
4209 }
4210 error_num = spider_db_bulk_open_handler(spider, conn, link_idx);
4211 if (!discard_result)
4212 {
4213 bool tmp_mta_conn_mutex_unlock_later;
4214 tmp_mta_conn_mutex_unlock_later = conn->mta_conn_mutex_unlock_later;
4215 conn->mta_conn_mutex_unlock_later = TRUE;
4216 if ((tmp_error_num = spider_db_store_result(spider, link_idx,
4217 spider->get_table())))
4218 {
4219 error_num = tmp_error_num;
4220 }
4221 conn->mta_conn_mutex_unlock_later = tmp_mta_conn_mutex_unlock_later;
4222 } else {
4223 if (spider->connection_ids[link_idx] == conn->connection_id)
4224 spider_db_discard_result(spider, link_idx, conn);
4225 }
4226 DBUG_RETURN(error_num);
4227}
4228#endif
4229
4230int spider_db_fetch(
4231 uchar *buf,
4232 ha_spider *spider,
4233 TABLE *table
4234) {
4235 int error_num;
4236 SPIDER_RESULT_LIST *result_list = &spider->result_list;
4237 DBUG_ENTER("spider_db_fetch");
4238 if (spider->sql_kind[spider->result_link_idx] == SPIDER_SQL_KIND_SQL)
4239 {
4240 if (!spider->select_column_mode) {
4241 if (result_list->keyread)
4242 error_num = spider_db_fetch_key(spider, buf, table,
4243 result_list->key_info, result_list);
4244 else
4245 error_num = spider_db_fetch_table(spider, buf, table,
4246 result_list);
4247 } else
4248 error_num = spider_db_fetch_minimum_columns(spider, buf, table,
4249 result_list);
4250 } else {
4251 error_num = spider_db_fetch_table(spider, buf, table,
4252 result_list);
4253 }
4254 result_list->current_row_num++;
4255 DBUG_PRINT("info",("spider error_num=%d", error_num));
4256 spider->pushed_pos = NULL;
4257 DBUG_RETURN(error_num);
4258}
4259
4260int spider_db_seek_prev(
4261 uchar *buf,
4262 ha_spider *spider,
4263 TABLE *table
4264) {
4265 SPIDER_RESULT_LIST *result_list = &spider->result_list;
4266 DBUG_ENTER("spider_db_seek_prev");
4267 if (result_list->current_row_num <= 1)
4268 {
4269 if (result_list->current == result_list->first)
4270 {
4271 table->status = STATUS_NOT_FOUND;
4272 DBUG_RETURN(HA_ERR_END_OF_FILE);
4273 }
4274 if (result_list->low_mem_read == 1)
4275 {
4276 my_message(ER_SPIDER_LOW_MEM_READ_PREV_NUM,
4277 ER_SPIDER_LOW_MEM_READ_PREV_STR, MYF(0));
4278 DBUG_RETURN(ER_SPIDER_LOW_MEM_READ_PREV_NUM);
4279 }
4280 result_list->current = result_list->current->prev;
4281 result_list->current_row_num = result_list->current->record_num - 1;
4282 } else {
4283 result_list->current_row_num -= 2;
4284 }
4285 if (result_list->quick_mode == 0)
4286 result_list->current->result->move_to_pos(result_list->current_row_num);
4287 DBUG_RETURN(spider_db_fetch(buf, spider, table));
4288}
4289
4290int spider_db_seek_next(
4291 uchar *buf,
4292 ha_spider *spider,
4293 int link_idx,
4294 TABLE *table
4295) {
4296 int error_num;
4297 SPIDER_SHARE *share = spider->share;
4298 SPIDER_CONN *conn = spider->conns[link_idx];
4299 SPIDER_RESULT_LIST *result_list = &spider->result_list;
4300 DBUG_ENTER("spider_db_seek_next");
4301 if (
4302#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4303 spider->conn_kind[spider->result_link_idx] == SPIDER_CONN_KIND_MYSQL &&
4304#endif
4305 result_list->current_row_num >= result_list->current->record_num
4306 ) {
4307 DBUG_PRINT("info",("spider result_list->current_row_num=%lld",
4308 result_list->current_row_num));
4309 DBUG_PRINT("info",("spider result_list->current->record_num=%lld",
4310 result_list->current->record_num));
4311 if (result_list->low_mem_read)
4312 spider_db_free_one_result(result_list,
4313 (SPIDER_RESULT*) result_list->current);
4314
4315 int roop_start = 0, roop_end = 1, roop_count, lock_mode, link_ok = 0;
4316#ifdef SPIDER_HAS_GROUP_BY_HANDLER
4317 if (!spider->use_fields)
4318 {
4319#endif
4320 lock_mode = spider_conn_lock_mode(spider);
4321 if (lock_mode)
4322 {
4323 /* "for update" or "lock in share mode" */
4324 link_ok = spider_conn_link_idx_next(share->link_statuses,
4325 spider->conn_link_idx, -1, share->link_count,
4326 SPIDER_LINK_STATUS_OK);
4327 roop_start = spider_conn_link_idx_next(share->link_statuses,
4328 spider->conn_link_idx, -1, share->link_count,
4329 SPIDER_LINK_STATUS_RECOVERY);
4330 roop_end = spider->share->link_count;
4331 } else {
4332 link_ok = link_idx;
4333 roop_start = link_idx;
4334 roop_end = link_idx + 1;
4335 }
4336#ifdef SPIDER_HAS_GROUP_BY_HANDLER
4337 }
4338#endif
4339
4340#ifndef WITHOUT_SPIDER_BG_SEARCH
4341 if (result_list->bgs_phase > 0)
4342 {
4343#ifdef SPIDER_HAS_GROUP_BY_HANDLER
4344 if (spider->use_fields)
4345 {
4346 SPIDER_LINK_IDX_CHAIN *link_idx_chain;
4347 SPIDER_LINK_IDX_HOLDER *link_idx_holder;
4348 spider_fields *fields = spider->fields;
4349 fields->set_pos_to_first_link_idx_chain();
4350 while ((link_idx_chain = fields->get_next_link_idx_chain()))
4351 {
4352 conn = link_idx_chain->conn;
4353 link_idx_holder = link_idx_chain->link_idx_holder;
4354 spider_db_handler *dbton_hdl =
4355 spider->dbton_handler[conn->dbton_id];
4356 spider->link_idx_chain = link_idx_chain;
4357 if ((error_num = spider_bg_conn_search(spider,
4358 link_idx_holder->link_idx, dbton_hdl->first_link_idx,
4359 FALSE, FALSE,
4360 !fields->is_first_link_ok_chain(link_idx_chain))))
4361 {
4362 DBUG_PRINT("info",("spider error_num 1=%d", error_num));
4363 DBUG_RETURN(error_num);
4364 }
4365 }
4366 } else {
4367#endif
4368 for (roop_count = roop_start; roop_count < roop_end;
4369 roop_count = spider_conn_link_idx_next(share->link_statuses,
4370 spider->conn_link_idx, roop_count, share->link_count,
4371 SPIDER_LINK_STATUS_RECOVERY)
4372 ) {
4373 if ((error_num = spider_bg_conn_search(spider, roop_count, roop_start,
4374 FALSE, FALSE, (roop_count != link_ok))))
4375 {
4376 DBUG_PRINT("info",("spider error_num 1=%d", error_num));
4377 DBUG_RETURN(error_num);
4378 }
4379 }
4380#ifdef SPIDER_HAS_GROUP_BY_HANDLER
4381 }
4382#endif
4383 } else {
4384#endif
4385 if (result_list->current == result_list->bgs_current)
4386 {
4387 if (result_list->finish_flg)
4388 {
4389 table->status = STATUS_NOT_FOUND;
4390 DBUG_PRINT("info",("spider error_num 2=%d", HA_ERR_END_OF_FILE));
4391 DBUG_RETURN(HA_ERR_END_OF_FILE);
4392 }
4393 spider_next_split_read_param(spider);
4394 if (
4395 result_list->quick_mode == 0 ||
4396 result_list->quick_mode == 3 ||
4397 !result_list->current->result
4398 ) {
4399 result_list->limit_num =
4400 result_list->internal_limit - result_list->record_num >=
4401 result_list->split_read ?
4402 result_list->split_read :
4403 result_list->internal_limit - result_list->record_num;
4404 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
4405 {
4406 if ((error_num = spider->reappend_limit_sql_part(
4407 result_list->record_num, result_list->limit_num,
4408 SPIDER_SQL_TYPE_SELECT_SQL)))
4409 {
4410 DBUG_PRINT("info",("spider error_num 3=%d", error_num));
4411 DBUG_RETURN(error_num);
4412 }
4413 if (
4414 !result_list->use_union &&
4415 (error_num = spider->append_select_lock_sql_part(
4416 SPIDER_SQL_TYPE_SELECT_SQL))
4417 ) {
4418 DBUG_PRINT("info",("spider error_num 4=%d", error_num));
4419 DBUG_RETURN(error_num);
4420 }
4421 }
4422 if (spider->sql_kinds & SPIDER_SQL_KIND_HANDLER)
4423 {
4424 spider_db_append_handler_next(spider);
4425 if ((error_num = spider->reappend_limit_sql_part(
4426 0, result_list->limit_num,
4427 SPIDER_SQL_TYPE_HANDLER)))
4428 {
4429 DBUG_PRINT("info",("spider error_num 5=%d", error_num));
4430 DBUG_RETURN(error_num);
4431 }
4432 }
4433
4434#ifdef SPIDER_HAS_GROUP_BY_HANDLER
4435 if (spider->use_fields)
4436 {
4437 SPIDER_LINK_IDX_CHAIN *link_idx_chain;
4438 SPIDER_LINK_IDX_HOLDER *link_idx_holder;
4439 spider_fields *fields = spider->fields;
4440 fields->set_pos_to_first_link_idx_chain();
4441 while ((link_idx_chain = fields->get_next_link_idx_chain()))
4442 {
4443 ulong sql_type;
4444 conn = link_idx_chain->conn;
4445 sql_type = SPIDER_SQL_TYPE_SELECT_SQL;
4446 link_idx_holder = link_idx_chain->link_idx_holder;
4447 link_idx = link_idx_holder->link_idx;
4448 spider_db_handler *dbton_handler =
4449 spider->dbton_handler[conn->dbton_id];
4450 if (dbton_handler->need_lock_before_set_sql_for_exec(sql_type))
4451 {
4452 pthread_mutex_lock(&conn->mta_conn_mutex);
4453 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
4454 }
4455 if ((error_num = dbton_handler->set_sql_for_exec(sql_type,
4456 link_idx)))
4457 {
4458 DBUG_PRINT("info",("spider error_num 6=%d", error_num));
4459 DBUG_RETURN(error_num);
4460 }
4461 if (!dbton_handler->need_lock_before_set_sql_for_exec(sql_type))
4462 {
4463 pthread_mutex_lock(&conn->mta_conn_mutex);
4464 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
4465 }
4466 conn->need_mon = &spider->need_mons[link_idx];
4467 conn->mta_conn_mutex_lock_already = TRUE;
4468 conn->mta_conn_mutex_unlock_later = TRUE;
4469 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
4470 {
4471 conn->mta_conn_mutex_lock_already = FALSE;
4472 conn->mta_conn_mutex_unlock_later = FALSE;
4473 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
4474 pthread_mutex_unlock(&conn->mta_conn_mutex);
4475 if (
4476 spider->need_mons[link_idx]
4477 ) {
4478 error_num = fields->ping_table_mon_from_table(link_idx_chain);
4479 }
4480 DBUG_PRINT("info",("spider error_num 7a=%d", error_num));
4481 DBUG_RETURN(error_num);
4482 }
4483 spider_conn_set_timeout_from_share(conn, link_idx,
4484 spider->trx->thd, share);
4485 if (dbton_handler->execute_sql(
4486 sql_type,
4487 conn,
4488 result_list->quick_mode,
4489 &spider->need_mons[link_idx])
4490 ) {
4491 conn->mta_conn_mutex_lock_already = FALSE;
4492 conn->mta_conn_mutex_unlock_later = FALSE;
4493 error_num = spider_db_errorno(conn);
4494 if (
4495 spider->need_mons[link_idx]
4496 ) {
4497 error_num = fields->ping_table_mon_from_table(link_idx_chain);
4498 }
4499 DBUG_PRINT("info",("spider error_num 8a=%d", error_num));
4500 DBUG_RETURN(error_num);
4501 }
4502 spider->connection_ids[link_idx] = conn->connection_id;
4503 conn->mta_conn_mutex_lock_already = FALSE;
4504 conn->mta_conn_mutex_unlock_later = FALSE;
4505 if (fields->is_first_link_ok_chain(link_idx_chain))
4506 {
4507 if ((error_num = spider_db_store_result(spider, link_idx,
4508 table)))
4509 {
4510 if (
4511 error_num != HA_ERR_END_OF_FILE &&
4512 spider->need_mons[link_idx]
4513 ) {
4514 error_num =
4515 fields->ping_table_mon_from_table(link_idx_chain);
4516 }
4517 DBUG_PRINT("info",("spider error_num 9a=%d", error_num));
4518 DBUG_RETURN(error_num);
4519 }
4520 spider->result_link_idx = link_ok;
4521 } else {
4522 spider_db_discard_result(spider, link_idx, conn);
4523 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
4524 pthread_mutex_unlock(&conn->mta_conn_mutex);
4525 }
4526 }
4527 } else {
4528#endif
4529 for (roop_count = roop_start; roop_count < roop_end;
4530 roop_count = spider_conn_link_idx_next(share->link_statuses,
4531 spider->conn_link_idx, roop_count, share->link_count,
4532 SPIDER_LINK_STATUS_RECOVERY)
4533 ) {
4534 ulong sql_type;
4535 conn = spider->conns[roop_count];
4536 if (spider->sql_kind[roop_count] == SPIDER_SQL_KIND_SQL)
4537 {
4538 sql_type = SPIDER_SQL_TYPE_SELECT_SQL;
4539 } else {
4540 sql_type = SPIDER_SQL_TYPE_HANDLER;
4541 }
4542 spider_db_handler *dbton_handler =
4543 spider->dbton_handler[conn->dbton_id];
4544 if (dbton_handler->need_lock_before_set_sql_for_exec(sql_type))
4545 {
4546 pthread_mutex_lock(&conn->mta_conn_mutex);
4547 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
4548 }
4549 if ((error_num = dbton_handler->set_sql_for_exec(sql_type,
4550 roop_count)))
4551 {
4552 DBUG_PRINT("info",("spider error_num 6=%d", error_num));
4553 DBUG_RETURN(error_num);
4554 }
4555 if (!dbton_handler->need_lock_before_set_sql_for_exec(sql_type))
4556 {
4557 pthread_mutex_lock(&conn->mta_conn_mutex);
4558 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
4559 }
4560 conn->need_mon = &spider->need_mons[roop_count];
4561 conn->mta_conn_mutex_lock_already = TRUE;
4562 conn->mta_conn_mutex_unlock_later = TRUE;
4563 if ((error_num = spider_db_set_names(spider, conn, roop_count)))
4564 {
4565 conn->mta_conn_mutex_lock_already = FALSE;
4566 conn->mta_conn_mutex_unlock_later = FALSE;
4567 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
4568 pthread_mutex_unlock(&conn->mta_conn_mutex);
4569 if (
4570 share->monitoring_kind[roop_count] &&
4571 spider->need_mons[roop_count]
4572 ) {
4573 error_num = spider_ping_table_mon_from_table(
4574 spider->trx,
4575 spider->trx->thd,
4576 share,
4577 roop_count,
4578 (uint32) share->monitoring_sid[roop_count],
4579 share->table_name,
4580 share->table_name_length,
4581 spider->conn_link_idx[roop_count],
4582 NULL,
4583 0,
4584 share->monitoring_kind[roop_count],
4585 share->monitoring_limit[roop_count],
4586 share->monitoring_flag[roop_count],
4587 TRUE
4588 );
4589 }
4590 DBUG_PRINT("info",("spider error_num 7=%d", error_num));
4591 DBUG_RETURN(error_num);
4592 }
4593 spider_conn_set_timeout_from_share(conn, roop_count,
4594 spider->trx->thd, share);
4595 if (dbton_handler->execute_sql(
4596 sql_type,
4597 conn,
4598 result_list->quick_mode,
4599 &spider->need_mons[roop_count])
4600 ) {
4601 conn->mta_conn_mutex_lock_already = FALSE;
4602 conn->mta_conn_mutex_unlock_later = FALSE;
4603 error_num = spider_db_errorno(conn);
4604 if (
4605 share->monitoring_kind[roop_count] &&
4606 spider->need_mons[roop_count]
4607 ) {
4608 error_num = spider_ping_table_mon_from_table(
4609 spider->trx,
4610 spider->trx->thd,
4611 share,
4612 roop_count,
4613 (uint32) share->monitoring_sid[roop_count],
4614 share->table_name,
4615 share->table_name_length,
4616 spider->conn_link_idx[roop_count],
4617 NULL,
4618 0,
4619 share->monitoring_kind[roop_count],
4620 share->monitoring_limit[roop_count],
4621 share->monitoring_flag[roop_count],
4622 TRUE
4623 );
4624 }
4625 DBUG_PRINT("info",("spider error_num 8=%d", error_num));
4626 DBUG_RETURN(error_num);
4627 }
4628 spider->connection_ids[roop_count] = conn->connection_id;
4629 conn->mta_conn_mutex_lock_already = FALSE;
4630 conn->mta_conn_mutex_unlock_later = FALSE;
4631 if (roop_count == link_ok)
4632 {
4633 if ((error_num = spider_db_store_result(spider, roop_count,
4634 table)))
4635 {
4636 if (
4637 error_num != HA_ERR_END_OF_FILE &&
4638 share->monitoring_kind[roop_count] &&
4639 spider->need_mons[roop_count]
4640 ) {
4641 error_num = spider_ping_table_mon_from_table(
4642 spider->trx,
4643 spider->trx->thd,
4644 share,
4645 roop_count,
4646 (uint32) share->monitoring_sid[roop_count],
4647 share->table_name,
4648 share->table_name_length,
4649 spider->conn_link_idx[roop_count],
4650 NULL,
4651 0,
4652 share->monitoring_kind[roop_count],
4653 share->monitoring_limit[roop_count],
4654 share->monitoring_flag[roop_count],
4655 TRUE
4656 );
4657 }
4658 DBUG_PRINT("info",("spider error_num 9=%d", error_num));
4659 DBUG_RETURN(error_num);
4660 }
4661 spider->result_link_idx = link_ok;
4662 } else {
4663 spider_db_discard_result(spider, roop_count, conn);
4664 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
4665 pthread_mutex_unlock(&conn->mta_conn_mutex);
4666 }
4667 }
4668#ifdef SPIDER_HAS_GROUP_BY_HANDLER
4669 }
4670#endif
4671 } else {
4672 spider->connection_ids[link_idx] = conn->connection_id;
4673 conn->mta_conn_mutex_unlock_later = TRUE;
4674 if ((error_num = spider_db_store_result(spider, link_idx, table)))
4675 {
4676 conn->mta_conn_mutex_unlock_later = FALSE;
4677 DBUG_PRINT("info",("spider error_num 10=%d", error_num));
4678 DBUG_RETURN(error_num);
4679 }
4680 conn->mta_conn_mutex_unlock_later = FALSE;
4681 }
4682 } else {
4683 result_list->current = result_list->current->next;
4684 result_list->current_row_num = 0;
4685 if (
4686 result_list->current == result_list->bgs_current &&
4687 result_list->finish_flg
4688 ) {
4689 table->status = STATUS_NOT_FOUND;
4690 DBUG_PRINT("info",("spider error_num 11=%d", HA_ERR_END_OF_FILE));
4691 DBUG_RETURN(HA_ERR_END_OF_FILE);
4692 }
4693 }
4694#ifndef WITHOUT_SPIDER_BG_SEARCH
4695 }
4696#endif
4697 DBUG_RETURN(spider_db_fetch(buf, spider, table));
4698 } else
4699 DBUG_RETURN(spider_db_fetch(buf, spider, table));
4700}
4701
4702int spider_db_seek_last(
4703 uchar *buf,
4704 ha_spider *spider,
4705 int link_idx,
4706 TABLE *table
4707) {
4708 int error_num;
4709 SPIDER_SHARE *share = spider->share;
4710 SPIDER_CONN *conn;
4711 SPIDER_RESULT_LIST *result_list = &spider->result_list;
4712 DBUG_ENTER("spider_db_seek_last");
4713 if (result_list->finish_flg)
4714 {
4715 if (result_list->low_mem_read == 1)
4716 {
4717 my_message(ER_SPIDER_LOW_MEM_READ_PREV_NUM,
4718 ER_SPIDER_LOW_MEM_READ_PREV_STR, MYF(0));
4719 DBUG_RETURN(ER_SPIDER_LOW_MEM_READ_PREV_NUM);
4720 }
4721 result_list->current = result_list->last;
4722 result_list->current_row_num = result_list->current->record_num - 1;
4723 if (result_list->quick_mode == 0)
4724 result_list->current->result->move_to_pos(result_list->current_row_num);
4725 DBUG_RETURN(spider_db_fetch(buf, spider, table));
4726 } else if (!result_list->sorted ||
4727 result_list->internal_limit <= result_list->record_num * 2)
4728 {
4729 if (result_list->low_mem_read == 1)
4730 {
4731 my_message(ER_SPIDER_LOW_MEM_READ_PREV_NUM,
4732 ER_SPIDER_LOW_MEM_READ_PREV_STR, MYF(0));
4733 DBUG_RETURN(ER_SPIDER_LOW_MEM_READ_PREV_NUM);
4734 }
4735 spider_next_split_read_param(spider);
4736 result_list->limit_num =
4737 result_list->internal_limit - result_list->record_num;
4738 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
4739 {
4740 if ((error_num = spider->reappend_limit_sql_part(
4741 result_list->internal_offset + result_list->record_num,
4742 result_list->limit_num,
4743 SPIDER_SQL_TYPE_SELECT_SQL)))
4744 DBUG_RETURN(error_num);
4745 if (
4746 !result_list->use_union &&
4747 (error_num = spider->append_select_lock_sql_part(
4748 SPIDER_SQL_TYPE_SELECT_SQL))
4749 )
4750 DBUG_RETURN(error_num);
4751 }
4752 if (spider->sql_kinds & SPIDER_SQL_KIND_HANDLER)
4753 {
4754 spider_db_append_handler_next(spider);
4755 if ((error_num = spider->reappend_limit_sql_part(
4756 result_list->internal_offset + result_list->record_num,
4757 result_list->limit_num,
4758 SPIDER_SQL_TYPE_HANDLER)))
4759 DBUG_RETURN(error_num);
4760 if (
4761 !result_list->use_union &&
4762 (error_num = spider->append_select_lock_sql_part(
4763 SPIDER_SQL_TYPE_HANDLER))
4764 )
4765 DBUG_RETURN(error_num);
4766 }
4767
4768 int roop_start, roop_end, roop_count, lock_mode, link_ok;
4769 lock_mode = spider_conn_lock_mode(spider);
4770 if (lock_mode)
4771 {
4772 /* "for update" or "lock in share mode" */
4773 link_ok = spider_conn_link_idx_next(share->link_statuses,
4774 spider->conn_link_idx, -1, share->link_count,
4775 SPIDER_LINK_STATUS_OK);
4776 roop_start = spider_conn_link_idx_next(share->link_statuses,
4777 spider->conn_link_idx, -1, share->link_count,
4778 SPIDER_LINK_STATUS_RECOVERY);
4779 roop_end = spider->share->link_count;
4780 } else {
4781 link_ok = link_idx;
4782 roop_start = link_idx;
4783 roop_end = link_idx + 1;
4784 }
4785 for (roop_count = roop_start; roop_count < roop_end;
4786 roop_count = spider_conn_link_idx_next(share->link_statuses,
4787 spider->conn_link_idx, roop_count, share->link_count,
4788 SPIDER_LINK_STATUS_RECOVERY)
4789 ) {
4790 ulong sql_type;
4791 if (spider->sql_kind[roop_count] == SPIDER_SQL_KIND_SQL)
4792 {
4793 sql_type = SPIDER_SQL_TYPE_SELECT_SQL;
4794 } else {
4795 sql_type = SPIDER_SQL_TYPE_HANDLER;
4796 }
4797 conn = spider->conns[roop_count];
4798 spider_db_handler *dbton_handler = spider->dbton_handler[conn->dbton_id];
4799 if (dbton_handler->need_lock_before_set_sql_for_exec(sql_type))
4800 {
4801 pthread_mutex_lock(&conn->mta_conn_mutex);
4802 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
4803 }
4804 if ((error_num = dbton_handler->set_sql_for_exec(sql_type, roop_count)))
4805 {
4806 DBUG_RETURN(error_num);
4807 }
4808 if (!dbton_handler->need_lock_before_set_sql_for_exec(sql_type))
4809 {
4810 pthread_mutex_lock(&conn->mta_conn_mutex);
4811 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
4812 }
4813 DBUG_PRINT("info",("spider sql_type=%lu", sql_type));
4814 conn->need_mon = &spider->need_mons[roop_count];
4815 conn->mta_conn_mutex_lock_already = TRUE;
4816 conn->mta_conn_mutex_unlock_later = TRUE;
4817 if ((error_num = spider_db_set_names(spider, conn, roop_count)))
4818 {
4819 conn->mta_conn_mutex_lock_already = FALSE;
4820 conn->mta_conn_mutex_unlock_later = FALSE;
4821 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
4822 pthread_mutex_unlock(&conn->mta_conn_mutex);
4823 if (
4824 share->monitoring_kind[roop_count] &&
4825 spider->need_mons[roop_count]
4826 ) {
4827 error_num = spider_ping_table_mon_from_table(
4828 spider->trx,
4829 spider->trx->thd,
4830 share,
4831 roop_count,
4832 (uint32) share->monitoring_sid[roop_count],
4833 share->table_name,
4834 share->table_name_length,
4835 spider->conn_link_idx[roop_count],
4836 NULL,
4837 0,
4838 share->monitoring_kind[roop_count],
4839 share->monitoring_limit[roop_count],
4840 share->monitoring_flag[roop_count],
4841 TRUE
4842 );
4843 }
4844 DBUG_RETURN(error_num);
4845 }
4846 spider_conn_set_timeout_from_share(conn, roop_count, spider->trx->thd,
4847 share);
4848 if (dbton_handler->execute_sql(
4849 sql_type,
4850 conn,
4851 result_list->quick_mode,
4852 &spider->need_mons[roop_count])
4853 ) {
4854 conn->mta_conn_mutex_lock_already = FALSE;
4855 conn->mta_conn_mutex_unlock_later = FALSE;
4856 error_num = spider_db_errorno(conn);
4857 if (
4858 share->monitoring_kind[roop_count] &&
4859 spider->need_mons[roop_count]
4860 ) {
4861 error_num = spider_ping_table_mon_from_table(
4862 spider->trx,
4863 spider->trx->thd,
4864 share,
4865 roop_count,
4866 (uint32) share->monitoring_sid[roop_count],
4867 share->table_name,
4868 share->table_name_length,
4869 spider->conn_link_idx[roop_count],
4870 NULL,
4871 0,
4872 share->monitoring_kind[roop_count],
4873 share->monitoring_limit[roop_count],
4874 share->monitoring_flag[roop_count],
4875 TRUE
4876 );
4877 }
4878 DBUG_RETURN(error_num);
4879 }
4880 spider->connection_ids[roop_count] = conn->connection_id;
4881 conn->mta_conn_mutex_lock_already = FALSE;
4882 conn->mta_conn_mutex_unlock_later = FALSE;
4883 if (roop_count == link_ok)
4884 {
4885 if ((error_num = spider_db_store_result(spider, roop_count, table)))
4886 {
4887 if (
4888 error_num != HA_ERR_END_OF_FILE &&
4889 share->monitoring_kind[roop_count] &&
4890 spider->need_mons[roop_count]
4891 ) {
4892 error_num = spider_ping_table_mon_from_table(
4893 spider->trx,
4894 spider->trx->thd,
4895 share,
4896 roop_count,
4897 (uint32) share->monitoring_sid[roop_count],
4898 share->table_name,
4899 share->table_name_length,
4900 spider->conn_link_idx[roop_count],
4901 NULL,
4902 0,
4903 share->monitoring_kind[roop_count],
4904 share->monitoring_limit[roop_count],
4905 share->monitoring_flag[roop_count],
4906 TRUE
4907 );
4908 }
4909 DBUG_RETURN(error_num);
4910 }
4911 spider->result_link_idx = link_ok;
4912 } else {
4913 spider_db_discard_result(spider, roop_count, conn);
4914 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
4915 pthread_mutex_unlock(&conn->mta_conn_mutex);
4916 }
4917 }
4918 result_list->current_row_num = result_list->current->record_num - 1;
4919 if (result_list->quick_mode == 0)
4920 result_list->current->result->move_to_pos(result_list->current_row_num);
4921 DBUG_RETURN(spider_db_fetch(buf, spider, table));
4922 }
4923 if ((error_num = spider_db_free_result(spider, FALSE)))
4924 DBUG_RETURN(error_num);
4925 spider_first_split_read_param(spider);
4926 result_list->desc_flg = !(result_list->desc_flg);
4927 result_list->limit_num =
4928 result_list->internal_limit >= result_list->split_read ?
4929 result_list->split_read : result_list->internal_limit;
4930 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
4931 {
4932 spider->set_order_to_pos_sql(SPIDER_SQL_TYPE_SELECT_SQL);
4933 if (
4934 (error_num = spider->append_key_order_with_alias_sql_part(
4935 NULL, 0, SPIDER_SQL_TYPE_SELECT_SQL)) ||
4936 (error_num = spider->append_limit_sql_part(
4937 result_list->internal_offset,
4938 result_list->limit_num, SPIDER_SQL_TYPE_SELECT_SQL)) ||
4939 (
4940 !result_list->use_union &&
4941 (spider->sql_kinds & SPIDER_SQL_KIND_SQL) &&
4942 (error_num = spider->append_select_lock_sql_part(
4943 SPIDER_SQL_TYPE_SELECT_SQL))
4944 )
4945 )
4946 DBUG_RETURN(error_num);
4947 }
4948 if (spider->sql_kinds & SPIDER_SQL_KIND_HANDLER)
4949 {
4950 const char *alias;
4951 uint alias_length;
4952 if (result_list->sorted && result_list->desc_flg)
4953 {
4954 alias = SPIDER_SQL_LAST_STR;
4955 alias_length = SPIDER_SQL_LAST_LEN;
4956 } else {
4957 alias = SPIDER_SQL_FIRST_STR;
4958 alias_length = SPIDER_SQL_FIRST_LEN;
4959 }
4960 spider->set_order_to_pos_sql(SPIDER_SQL_TYPE_HANDLER);
4961 if (
4962 (error_num = spider->append_key_order_with_alias_sql_part(
4963 alias, alias_length, SPIDER_SQL_TYPE_HANDLER)) ||
4964 (error_num = spider->reappend_limit_sql_part(
4965 result_list->internal_offset,
4966 result_list->limit_num, SPIDER_SQL_TYPE_HANDLER))
4967 )
4968 DBUG_RETURN(error_num);
4969 }
4970
4971 int roop_start, roop_end, roop_count, lock_mode, link_ok;
4972 lock_mode = spider_conn_lock_mode(spider);
4973 if (lock_mode)
4974 {
4975 /* "for update" or "lock in share mode" */
4976 link_ok = spider_conn_link_idx_next(share->link_statuses,
4977 spider->conn_link_idx, -1, share->link_count,
4978 SPIDER_LINK_STATUS_OK);
4979 roop_start = spider_conn_link_idx_next(share->link_statuses,
4980 spider->conn_link_idx, -1, share->link_count,
4981 SPIDER_LINK_STATUS_RECOVERY);
4982 roop_end = spider->share->link_count;
4983 } else {
4984 link_ok = link_idx;
4985 roop_start = link_idx;
4986 roop_end = link_idx + 1;
4987 }
4988 for (roop_count = roop_start; roop_count < roop_end;
4989 roop_count = spider_conn_link_idx_next(share->link_statuses,
4990 spider->conn_link_idx, roop_count, share->link_count,
4991 SPIDER_LINK_STATUS_RECOVERY)
4992 ) {
4993 ulong sql_type;
4994 if (spider->sql_kind[roop_count] == SPIDER_SQL_KIND_SQL)
4995 {
4996 sql_type = SPIDER_SQL_TYPE_SELECT_SQL;
4997 } else {
4998 sql_type = SPIDER_SQL_TYPE_HANDLER;
4999 }
5000 conn = spider->conns[roop_count];
5001 spider_db_handler *dbton_handler = spider->dbton_handler[conn->dbton_id];
5002 if (dbton_handler->need_lock_before_set_sql_for_exec(sql_type))
5003 {
5004 pthread_mutex_lock(&conn->mta_conn_mutex);
5005 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
5006 }
5007 if ((error_num = dbton_handler->set_sql_for_exec(sql_type, roop_count)))
5008 {
5009 DBUG_RETURN(error_num);
5010 }
5011 if (!dbton_handler->need_lock_before_set_sql_for_exec(sql_type))
5012 {
5013 pthread_mutex_lock(&conn->mta_conn_mutex);
5014 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
5015 }
5016 DBUG_PRINT("info",("spider sql_type=%lu", sql_type));
5017 conn->need_mon = &spider->need_mons[roop_count];
5018 conn->mta_conn_mutex_lock_already = TRUE;
5019 conn->mta_conn_mutex_unlock_later = TRUE;
5020 if ((error_num = spider_db_set_names(spider, conn, roop_count)))
5021 {
5022 conn->mta_conn_mutex_lock_already = FALSE;
5023 conn->mta_conn_mutex_unlock_later = FALSE;
5024 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5025 pthread_mutex_unlock(&conn->mta_conn_mutex);
5026 if (
5027 share->monitoring_kind[roop_count] &&
5028 spider->need_mons[roop_count]
5029 ) {
5030 error_num = spider_ping_table_mon_from_table(
5031 spider->trx,
5032 spider->trx->thd,
5033 share,
5034 roop_count,
5035 (uint32) share->monitoring_sid[roop_count],
5036 share->table_name,
5037 share->table_name_length,
5038 spider->conn_link_idx[roop_count],
5039 NULL,
5040 0,
5041 share->monitoring_kind[roop_count],
5042 share->monitoring_limit[roop_count],
5043 share->monitoring_flag[roop_count],
5044 TRUE
5045 );
5046 }
5047 DBUG_RETURN(error_num);
5048 }
5049 spider_conn_set_timeout_from_share(conn, roop_count, spider->trx->thd,
5050 share);
5051 if (dbton_handler->execute_sql(
5052 sql_type,
5053 conn,
5054 result_list->quick_mode,
5055 &spider->need_mons[roop_count])
5056 ) {
5057 conn->mta_conn_mutex_lock_already = FALSE;
5058 conn->mta_conn_mutex_unlock_later = FALSE;
5059 error_num = spider_db_errorno(conn);
5060 if (
5061 share->monitoring_kind[roop_count] &&
5062 spider->need_mons[roop_count]
5063 ) {
5064 error_num = spider_ping_table_mon_from_table(
5065 spider->trx,
5066 spider->trx->thd,
5067 share,
5068 roop_count,
5069 (uint32) share->monitoring_sid[roop_count],
5070 share->table_name,
5071 share->table_name_length,
5072 spider->conn_link_idx[roop_count],
5073 NULL,
5074 0,
5075 share->monitoring_kind[roop_count],
5076 share->monitoring_limit[roop_count],
5077 share->monitoring_flag[roop_count],
5078 TRUE
5079 );
5080 }
5081 DBUG_RETURN(error_num);
5082 }
5083 spider->connection_ids[roop_count] = conn->connection_id;
5084 conn->mta_conn_mutex_lock_already = FALSE;
5085 conn->mta_conn_mutex_unlock_later = FALSE;
5086 if (roop_count == link_ok)
5087 {
5088 if ((error_num = spider_db_store_result(spider, roop_count, table)))
5089 {
5090 if (
5091 error_num != HA_ERR_END_OF_FILE &&
5092 share->monitoring_kind[roop_count] &&
5093 spider->need_mons[roop_count]
5094 ) {
5095 error_num = spider_ping_table_mon_from_table(
5096 spider->trx,
5097 spider->trx->thd,
5098 share,
5099 roop_count,
5100 (uint32) share->monitoring_sid[roop_count],
5101 share->table_name,
5102 share->table_name_length,
5103 spider->conn_link_idx[roop_count],
5104 NULL,
5105 0,
5106 share->monitoring_kind[roop_count],
5107 share->monitoring_limit[roop_count],
5108 share->monitoring_flag[roop_count],
5109 TRUE
5110 );
5111 }
5112 DBUG_RETURN(error_num);
5113 }
5114 spider->result_link_idx = link_ok;
5115 } else {
5116 spider_db_discard_result(spider, roop_count, conn);
5117 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5118 pthread_mutex_unlock(&conn->mta_conn_mutex);
5119 }
5120 }
5121 DBUG_RETURN(spider_db_fetch(buf, spider, table));
5122}
5123
5124int spider_db_seek_first(
5125 uchar *buf,
5126 ha_spider *spider,
5127 TABLE *table
5128) {
5129 SPIDER_RESULT_LIST *result_list = &spider->result_list;
5130 DBUG_ENTER("spider_db_seek_first");
5131 if (
5132 result_list->current != result_list->first &&
5133 result_list->low_mem_read == 1
5134 ) {
5135 my_message(ER_SPIDER_LOW_MEM_READ_PREV_NUM, ER_SPIDER_LOW_MEM_READ_PREV_STR, MYF(0));
5136 DBUG_RETURN(ER_SPIDER_LOW_MEM_READ_PREV_NUM);
5137 }
5138 result_list->current = result_list->first;
5139 spider_db_set_pos_to_first_row(result_list);
5140 DBUG_RETURN(spider_db_fetch(buf, spider, table));
5141}
5142
5143void spider_db_set_pos_to_first_row(
5144 SPIDER_RESULT_LIST *result_list
5145) {
5146 DBUG_ENTER("spider_db_set_pos_to_first_row");
5147 result_list->current_row_num = 0;
5148 if (result_list->quick_mode == 0)
5149 result_list->current->result->move_to_pos(0);
5150 DBUG_VOID_RETURN;
5151}
5152
5153void spider_db_create_position(
5154 ha_spider *spider,
5155 SPIDER_POSITION *pos
5156) {
5157 SPIDER_RESULT_LIST *result_list = &spider->result_list;
5158 SPIDER_RESULT *current = (SPIDER_RESULT*) result_list->current;
5159 DBUG_ENTER("spider_db_create_position");
5160 if (result_list->quick_mode == 0)
5161 {
5162 SPIDER_DB_RESULT *result = current->result;
5163 pos->row = result->current_row();
5164 pos->pos_mode = 2;
5165 pos->row->next_pos = result_list->tmp_pos_row_first;
5166 result_list->tmp_pos_row_first = pos->row;
5167 } else {
5168 if (result_list->current_row_num <= result_list->quick_page_size)
5169 {
5170 SPIDER_POSITION *tmp_pos =
5171 &current->first_position[result_list->current_row_num - 1];
5172 memcpy(pos, tmp_pos, sizeof(SPIDER_POSITION));
5173 tmp_pos->use_position = TRUE;
5174 tmp_pos->pos_mode = 0;
5175 pos->pos_mode = 0;
5176 current->first_pos_use_position = TRUE;
5177 } else {
5178 TABLE *tmp_tbl = current->result_tmp_tbl;
5179 pos->row = NULL;
5180 pos->pos_mode = 1;
5181 DBUG_PRINT("info",("spider tmp_tbl=%p", tmp_tbl));
5182 DBUG_PRINT("info",("spider tmp_tbl->file=%p", tmp_tbl->file));
5183 DBUG_PRINT("info",("spider tmp_tbl->file->ref=%p", tmp_tbl->file->ref));
5184 tmp_tbl->file->ref = (uchar *) &pos->tmp_tbl_pos;
5185 tmp_tbl->file->position(tmp_tbl->record[0]);
5186 current->tmp_tbl_use_position = TRUE;
5187 }
5188 }
5189 current->use_position = TRUE;
5190 pos->use_position = TRUE;
5191 pos->mrr_with_cnt = spider->mrr_with_cnt;
5192#ifdef HANDLER_HAS_DIRECT_AGGREGATE
5193 pos->direct_aggregate = result_list->direct_aggregate;
5194#endif
5195 pos->sql_kind = spider->sql_kind[spider->result_link_idx];
5196 pos->position_bitmap = spider->position_bitmap;
5197 pos->ft_first = spider->ft_first;
5198 pos->ft_current = spider->ft_current;
5199 pos->result = current;
5200 DBUG_VOID_RETURN;
5201}
5202
5203int spider_db_seek_tmp(
5204 uchar *buf,
5205 SPIDER_POSITION *pos,
5206 ha_spider *spider,
5207 TABLE *table
5208) {
5209 int error_num;
5210 SPIDER_RESULT_LIST *result_list = &spider->result_list;
5211 DBUG_ENTER("spider_db_seek_tmp");
5212 if (pos->pos_mode != 1)
5213 {
5214 if (!pos->row)
5215 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5216 pos->row->first();
5217 }
5218 if (pos->sql_kind == SPIDER_SQL_KIND_SQL)
5219 {
5220 if (!spider->select_column_mode)
5221 {
5222 if (result_list->keyread)
5223 error_num = spider_db_seek_tmp_key(buf, pos, spider, table,
5224 result_list->key_info);
5225 else
5226 error_num = spider_db_seek_tmp_table(buf, pos, spider, table);
5227 } else
5228 error_num = spider_db_seek_tmp_minimum_columns(buf, pos, spider, table);
5229 } else
5230 error_num = spider_db_seek_tmp_table(buf, pos, spider, table);
5231
5232 DBUG_PRINT("info",("spider error_num=%d", error_num));
5233 DBUG_RETURN(error_num);
5234}
5235
5236int spider_db_seek_tmp_table(
5237 uchar *buf,
5238 SPIDER_POSITION *pos,
5239 ha_spider *spider,
5240 TABLE *table
5241) {
5242 int error_num;
5243 Field **field;
5244 SPIDER_DB_ROW *row = pos->row;
5245 my_ptrdiff_t ptr_diff = PTR_BYTE_DIFF(buf, table->record[0]);
5246 DBUG_ENTER("spider_db_seek_tmp_table");
5247 if (pos->pos_mode == 1)
5248 {
5249 if ((error_num = spider_db_get_row_from_tmp_tbl_pos(pos, &row)))
5250 DBUG_RETURN(error_num);
5251 } else if (pos->pos_mode == 2)
5252 {
5253/*
5254 SPIDER_DB_RESULT *result = pos->result->result;
5255 result->current_row = row;
5256*/
5257 }
5258
5259 DBUG_PRINT("info", ("spider row=%p", row));
5260#ifdef HANDLER_HAS_DIRECT_AGGREGATE
5261 if (!spider->result_list.in_cmp_ref)
5262 {
5263 DBUG_PRINT("info", ("spider direct_aggregate=%s",
5264 pos->direct_aggregate ? "TRUE" : "FALSE"));
5265 spider->result_list.snap_mrr_with_cnt = pos->mrr_with_cnt;
5266 spider->result_list.snap_direct_aggregate = pos->direct_aggregate;
5267 spider->result_list.snap_row = row;
5268 }
5269#endif
5270
5271 /* for mrr */
5272 if (pos->mrr_with_cnt)
5273 {
5274 DBUG_PRINT("info", ("spider mrr_with_cnt"));
5275 if (pos->sql_kind == SPIDER_SQL_KIND_SQL)
5276 {
5277 row->next();
5278 } else {
5279#ifdef HANDLER_HAS_DIRECT_AGGREGATE
5280 spider->result_list.snap_mrr_with_cnt = FALSE;
5281#endif
5282 }
5283 }
5284
5285#ifdef HANDLER_HAS_DIRECT_AGGREGATE
5286 /* for direct_aggregate */
5287 if (pos->direct_aggregate)
5288 {
5289 if ((error_num = spider_db_fetch_for_item_sum_funcs(row, spider)))
5290 DBUG_RETURN(error_num);
5291 }
5292#endif
5293
5294 if ((error_num = spider_db_append_match_fetch(spider,
5295 pos->ft_first, pos->ft_current, row)))
5296 DBUG_RETURN(error_num);
5297
5298 for (
5299 field = table->field;
5300 *field;
5301 field++
5302 ) {
5303 if ((
5304 bitmap_is_set(table->read_set, (*field)->field_index) |
5305 bitmap_is_set(table->write_set, (*field)->field_index)
5306 )) {
5307#ifndef DBUG_OFF
5308 my_bitmap_map *tmp_map =
5309 dbug_tmp_use_all_columns(table, table->write_set);
5310#endif
5311 DBUG_PRINT("info", ("spider bitmap is set %s",
5312 (*field)->field_name.str));
5313 if ((error_num =
5314 spider_db_fetch_row(spider->share, *field, row, ptr_diff)))
5315 DBUG_RETURN(error_num);
5316#ifndef DBUG_OFF
5317 dbug_tmp_restore_column_map(table->write_set, tmp_map);
5318#endif
5319 }
5320 row->next();
5321 }
5322 DBUG_RETURN(0);
5323}
5324
5325int spider_db_seek_tmp_key(
5326 uchar *buf,
5327 SPIDER_POSITION *pos,
5328 ha_spider *spider,
5329 TABLE *table,
5330 const KEY *key_info
5331) {
5332 int error_num;
5333 KEY_PART_INFO *key_part;
5334 uint part_num;
5335 SPIDER_DB_ROW *row = pos->row;
5336 Field *field;
5337 my_ptrdiff_t ptr_diff = PTR_BYTE_DIFF(buf, table->record[0]);
5338 DBUG_ENTER("spider_db_seek_tmp_key");
5339 if (pos->pos_mode == 1)
5340 {
5341 if ((error_num = spider_db_get_row_from_tmp_tbl_pos(pos, &row)))
5342 DBUG_RETURN(error_num);
5343 } else if (pos->pos_mode == 2)
5344 {
5345/*
5346 SPIDER_DB_RESULT *result = pos->result->result;
5347 result->current_row = row;
5348*/
5349 }
5350
5351 DBUG_PRINT("info", ("spider row=%p", row));
5352#ifdef HANDLER_HAS_DIRECT_AGGREGATE
5353 if (!spider->result_list.in_cmp_ref)
5354 {
5355 DBUG_PRINT("info", ("spider direct_aggregate=%s",
5356 pos->direct_aggregate ? "TRUE" : "FALSE"));
5357 spider->result_list.snap_mrr_with_cnt = pos->mrr_with_cnt;
5358 spider->result_list.snap_direct_aggregate = pos->direct_aggregate;
5359 spider->result_list.snap_row = row;
5360 }
5361#endif
5362
5363 /* for mrr */
5364 if (pos->mrr_with_cnt)
5365 {
5366 DBUG_PRINT("info", ("spider mrr_with_cnt"));
5367 row->next();
5368 }
5369
5370#ifdef HANDLER_HAS_DIRECT_AGGREGATE
5371 /* for direct_aggregate */
5372 if (pos->direct_aggregate)
5373 {
5374 if ((error_num = spider_db_fetch_for_item_sum_funcs(row, spider)))
5375 DBUG_RETURN(error_num);
5376 }
5377#endif
5378
5379 if ((error_num = spider_db_append_match_fetch(spider,
5380 pos->ft_first, pos->ft_current, row)))
5381 DBUG_RETURN(error_num);
5382
5383 for (
5384 key_part = key_info->key_part,
5385 part_num = 0;
5386 part_num < spider_user_defined_key_parts(key_info);
5387 key_part++,
5388 part_num++
5389 ) {
5390 field = key_part->field;
5391 if ((
5392 bitmap_is_set(table->read_set, field->field_index) |
5393 bitmap_is_set(table->write_set, field->field_index)
5394 )) {
5395#ifndef DBUG_OFF
5396 my_bitmap_map *tmp_map =
5397 dbug_tmp_use_all_columns(table, table->write_set);
5398#endif
5399 DBUG_PRINT("info", ("spider bitmap is set %s", field->field_name.str));
5400 if ((error_num =
5401 spider_db_fetch_row(spider->share, field, row, ptr_diff)))
5402 DBUG_RETURN(error_num);
5403#ifndef DBUG_OFF
5404 dbug_tmp_restore_column_map(table->write_set, tmp_map);
5405#endif
5406 }
5407 row->next();
5408 }
5409 DBUG_RETURN(0);
5410}
5411
5412int spider_db_seek_tmp_minimum_columns(
5413 uchar *buf,
5414 SPIDER_POSITION *pos,
5415 ha_spider *spider,
5416 TABLE *table
5417) {
5418 int error_num;
5419 Field **field;
5420 SPIDER_DB_ROW *row = pos->row;
5421 my_ptrdiff_t ptr_diff = PTR_BYTE_DIFF(buf, table->record[0]);
5422 DBUG_ENTER("spider_db_seek_tmp_minimum_columns");
5423 if (pos->pos_mode == 1)
5424 {
5425 if ((error_num = spider_db_get_row_from_tmp_tbl_pos(pos, &row)))
5426 DBUG_RETURN(error_num);
5427 } else if (pos->pos_mode == 2)
5428 {
5429/*
5430 SPIDER_DB_RESULT *result = pos->result->result;
5431 result->current_row = row;
5432*/
5433 }
5434
5435 DBUG_PRINT("info", ("spider row=%p", row));
5436#ifdef HANDLER_HAS_DIRECT_AGGREGATE
5437 if (!spider->result_list.in_cmp_ref)
5438 {
5439 DBUG_PRINT("info", ("spider direct_aggregate=%s",
5440 pos->direct_aggregate ? "TRUE" : "FALSE"));
5441 spider->result_list.snap_mrr_with_cnt = pos->mrr_with_cnt;
5442 spider->result_list.snap_direct_aggregate = pos->direct_aggregate;
5443 spider->result_list.snap_row = row;
5444 }
5445#endif
5446
5447 /* for mrr */
5448 if (pos->mrr_with_cnt)
5449 {
5450 DBUG_PRINT("info", ("spider mrr_with_cnt"));
5451 row->next();
5452 }
5453
5454#ifdef HANDLER_HAS_DIRECT_AGGREGATE
5455 /* for direct_aggregate */
5456 if (pos->direct_aggregate)
5457 {
5458 if ((error_num = spider_db_fetch_for_item_sum_funcs(row, spider)))
5459 DBUG_RETURN(error_num);
5460 }
5461#endif
5462
5463 if ((error_num = spider_db_append_match_fetch(spider,
5464 pos->ft_first, pos->ft_current, row)))
5465 DBUG_RETURN(error_num);
5466
5467 for (
5468 field = table->field;
5469 *field;
5470 field++
5471 ) {
5472 DBUG_PRINT("info", ("spider field_index %u", (*field)->field_index));
5473 if (spider_bit_is_set(pos->position_bitmap, (*field)->field_index))
5474 {
5475/*
5476 if ((
5477 bitmap_is_set(table->read_set, (*field)->field_index) |
5478 bitmap_is_set(table->write_set, (*field)->field_index)
5479 )) {
5480 DBUG_PRINT("info", ("spider read_set %u",
5481 bitmap_is_set(table->read_set, (*field)->field_index)));
5482 DBUG_PRINT("info", ("spider write_set %u",
5483 bitmap_is_set(table->write_set, (*field)->field_index)));
5484*/
5485#ifndef DBUG_OFF
5486 my_bitmap_map *tmp_map =
5487 dbug_tmp_use_all_columns(table, table->write_set);
5488#endif
5489 DBUG_PRINT("info", ("spider bitmap is set %s",
5490 (*field)->field_name.str));
5491 if ((error_num =
5492 spider_db_fetch_row(spider->share, *field, row, ptr_diff)))
5493 DBUG_RETURN(error_num);
5494 row->next();
5495#ifndef DBUG_OFF
5496 dbug_tmp_restore_column_map(table->write_set, tmp_map);
5497#endif
5498 }
5499 else if (bitmap_is_set(table->read_set, (*field)->field_index))
5500 {
5501 DBUG_PRINT("info", ("spider bitmap is cleared %s",
5502 (*field)->field_name.str));
5503 bitmap_clear_bit(table->read_set, (*field)->field_index);
5504 }
5505 }
5506 DBUG_RETURN(0);
5507}
5508
5509int spider_db_show_table_status(
5510 ha_spider *spider,
5511 int link_idx,
5512 int sts_mode,
5513 uint flag
5514) {
5515 int error_num;
5516 SPIDER_CONN *conn = spider->conns[link_idx];
5517 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
5518 DBUG_ENTER("spider_db_show_table_status");
5519 DBUG_PRINT("info",("spider sts_mode=%d", sts_mode));
5520 sts_mode = dbton_hdl->sts_mode_exchange(sts_mode);
5521 error_num = dbton_hdl->show_table_status(
5522 link_idx,
5523 sts_mode,
5524 flag
5525 );
5526 DBUG_RETURN(error_num);
5527}
5528
5529int spider_db_show_records(
5530 ha_spider *spider,
5531 int link_idx,
5532 bool pre_call
5533) {
5534 int error_num;
5535 THD *thd = spider->trx->thd;
5536 SPIDER_CONN *conn;
5537 DBUG_ENTER("spider_db_show_records");
5538 if (pre_call)
5539 {
5540 if (spider_param_bgs_mode(thd, spider->share->bgs_mode))
5541 {
5542 if ((error_num = spider_check_and_get_casual_read_conn(thd, spider,
5543 link_idx)))
5544 {
5545 DBUG_RETURN(error_num);
5546 }
5547 conn = spider->conns[link_idx];
5548 if (!(error_num = spider_create_conn_thread(conn)))
5549 {
5550 spider_bg_conn_simple_action(conn, SPIDER_BG_SIMPLE_RECORDS, FALSE,
5551 spider, link_idx, (int *) &spider->result_list.bgs_error);
5552 }
5553 } else {
5554 conn = spider->conns[link_idx];
5555 error_num = spider->dbton_handler[conn->dbton_id]->show_records(
5556 link_idx
5557 );
5558 }
5559 } else {
5560 conn = spider->conns[link_idx];
5561 if (spider->use_pre_records)
5562 {
5563 if (spider_param_bgs_mode(thd, spider->share->bgs_mode))
5564 {
5565 spider_bg_conn_wait(conn);
5566 error_num = spider->result_list.bgs_error;
5567 if (conn->casual_read_base_conn)
5568 {
5569 spider->conns[link_idx] = conn->casual_read_base_conn;
5570 }
5571 } else {
5572 error_num = 0;
5573 }
5574 } else {
5575 error_num = spider->dbton_handler[conn->dbton_id]->show_records(
5576 link_idx
5577 );
5578 }
5579 }
5580 DBUG_RETURN(error_num);
5581}
5582
5583void spider_db_set_cardinarity(
5584 ha_spider *spider,
5585 TABLE *table
5586) {
5587 int roop_count, roop_count2;
5588 SPIDER_SHARE *share = spider->share;
5589 KEY *key_info;
5590 KEY_PART_INFO *key_part;
5591 Field *field;
5592 ha_rows rec_per_key;
5593 DBUG_ENTER("spider_db_set_cardinarity");
5594 for (roop_count = 0; roop_count < (int) table->s->keys; roop_count++)
5595 {
5596 key_info = &table->key_info[roop_count];
5597 for (roop_count2 = 0;
5598 roop_count2 < (int) spider_user_defined_key_parts(key_info);
5599 roop_count2++)
5600 {
5601 key_part = &key_info->key_part[roop_count2];
5602 field = key_part->field;
5603 rec_per_key = (ha_rows) share->records /
5604 share->cardinality[field->field_index];
5605 if (rec_per_key > ~(ulong) 0)
5606 key_info->rec_per_key[roop_count2] = ~(ulong) 0;
5607 else if (rec_per_key == 0)
5608 key_info->rec_per_key[roop_count2] = 1;
5609 else
5610 key_info->rec_per_key[roop_count2] = (ulong) rec_per_key;
5611 DBUG_PRINT("info",
5612 ("spider column id=%d", field->field_index));
5613 DBUG_PRINT("info",
5614 ("spider cardinality=%lld",
5615 share->cardinality[field->field_index]));
5616 DBUG_PRINT("info",
5617 ("spider rec_per_key=%lu",
5618 key_info->rec_per_key[roop_count2]));
5619 }
5620 }
5621 DBUG_VOID_RETURN;
5622}
5623
5624int spider_db_show_index(
5625 ha_spider *spider,
5626 int link_idx,
5627 TABLE *table,
5628 int crd_mode
5629) {
5630 int error_num;
5631 SPIDER_CONN *conn = spider->conns[link_idx];
5632 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
5633 DBUG_ENTER("spider_db_show_index");
5634 crd_mode = dbton_hdl->crd_mode_exchange(crd_mode);
5635 error_num = spider->dbton_handler[conn->dbton_id]->show_index(
5636 link_idx,
5637 crd_mode
5638 );
5639 DBUG_RETURN(error_num);
5640}
5641
5642ha_rows spider_db_explain_select(
5643 key_range *start_key,
5644 key_range *end_key,
5645 ha_spider *spider,
5646 int link_idx
5647) {
5648 SPIDER_CONN *conn = spider->conns[link_idx];
5649 ha_rows rows;
5650 DBUG_ENTER("spider_db_explain_select");
5651 rows = spider->dbton_handler[conn->dbton_id]->explain_select(
5652 start_key,
5653 end_key,
5654 link_idx
5655 );
5656 DBUG_RETURN(rows);
5657}
5658
5659int spider_db_bulk_insert_init(
5660 ha_spider *spider,
5661 const TABLE *table
5662) {
5663 int error_num, roop_count;
5664 SPIDER_SHARE *share = spider->share;
5665 DBUG_ENTER("spider_db_bulk_insert_init");
5666 spider->sql_kinds = 0;
5667 spider->reset_sql_sql(SPIDER_SQL_TYPE_INSERT_SQL);
5668#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5669 spider->reset_hs_sql(SPIDER_SQL_TYPE_OTHER_HS);
5670#endif
5671 for (
5672 roop_count = spider_conn_link_idx_next(share->link_statuses,
5673 spider->conn_link_idx, -1, share->link_count,
5674 SPIDER_LINK_STATUS_RECOVERY);
5675 roop_count < (int) share->link_count;
5676 roop_count = spider_conn_link_idx_next(share->link_statuses,
5677 spider->conn_link_idx, roop_count, share->link_count,
5678 SPIDER_LINK_STATUS_RECOVERY)
5679 ) {
5680 if (spider->conns[roop_count])
5681 spider->conns[roop_count]->ignore_dup_key = spider->ignore_dup_key;
5682#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5683 if (
5684 spider_conn_use_handler(spider, spider->lock_mode, roop_count) &&
5685 (
5686 !spider->handler_opened(roop_count, SPIDER_CONN_KIND_HS_WRITE) ||
5687#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
5688 spider->hs_w_ret_fields_num[roop_count] < MAX_FIELDS ||
5689#endif
5690 spider->hs_w_conns[roop_count]->server_lost
5691 )
5692 ) {
5693 if ((error_num = spider_db_open_handler(spider,
5694 spider->hs_w_conns[roop_count], roop_count)))
5695 {
5696 if (
5697 share->monitoring_kind[roop_count] &&
5698 spider->need_mons[roop_count]
5699 ) {
5700 error_num = spider_ping_table_mon_from_table(
5701 spider->trx,
5702 spider->trx->thd,
5703 share,
5704 roop_count,
5705 (uint32) share->monitoring_sid[roop_count],
5706 share->table_name,
5707 share->table_name_length,
5708 spider->conn_link_idx[roop_count],
5709 NULL,
5710 0,
5711 share->monitoring_kind[roop_count],
5712 share->monitoring_limit[roop_count],
5713 share->monitoring_flag[roop_count],
5714 TRUE
5715 );
5716 }
5717 DBUG_RETURN(error_num);
5718 }
5719 spider->set_handler_opened(roop_count);
5720 }
5721#else
5722 spider_conn_use_handler(spider, spider->lock_mode, roop_count);
5723#endif
5724 }
5725#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5726 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
5727 {
5728#endif
5729 if (
5730 (error_num = spider->append_insert_sql_part()) ||
5731 (error_num = spider->append_into_sql_part(
5732 SPIDER_SQL_TYPE_INSERT_SQL))
5733 )
5734 DBUG_RETURN(error_num);
5735#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5736 }
5737 if (spider->sql_kinds & SPIDER_SQL_KIND_HS)
5738 {
5739 spider->result_list.hs_upd_rows = 0;
5740 }
5741#endif
5742 DBUG_RETURN(0);
5743}
5744
5745int spider_db_bulk_insert(
5746 ha_spider *spider,
5747 TABLE *table,
5748 bool bulk_end
5749) {
5750 int error_num, first_insert_link_idx = -1;
5751#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5752 SPIDER_RESULT_LIST *result_list = &spider->result_list;
5753#endif
5754 SPIDER_SHARE *share = spider->share;
5755 THD *thd = spider->trx->thd;
5756 bool mta_conn_mutex_lock_already_backup;
5757 bool mta_conn_mutex_unlock_later_backup;
5758 DBUG_ENTER("spider_db_bulk_insert");
5759
5760 if (!bulk_end)
5761 {
5762#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5763 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
5764 {
5765#endif
5766 if ((error_num = spider->append_insert_values_sql_part(
5767 SPIDER_SQL_TYPE_INSERT_SQL)))
5768 DBUG_RETURN(error_num);
5769#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5770 }
5771 if (spider->sql_kinds & SPIDER_SQL_KIND_HS)
5772 {
5773 if ((error_num = spider->append_insert_values_hs_part(
5774 SPIDER_SQL_TYPE_INSERT_HS)))
5775 DBUG_RETURN(error_num);
5776 result_list->hs_upd_rows++;
5777 }
5778#endif
5779 }
5780
5781 if (spider->is_bulk_insert_exec_period(bulk_end))
5782 {
5783 int roop_count2;
5784 SPIDER_CONN *conn, *first_insert_conn = NULL;
5785 if ((error_num = spider->append_insert_terminator_sql_part(
5786 SPIDER_SQL_TYPE_INSERT_SQL)))
5787 {
5788 DBUG_RETURN(error_num);
5789 }
5790#ifdef HA_CAN_BULK_ACCESS
5791 if (!spider->is_bulk_access_clone)
5792 {
5793#endif
5794 for (
5795 roop_count2 = spider_conn_link_idx_next(share->link_statuses,
5796 spider->conn_link_idx, -1, share->link_count,
5797 SPIDER_LINK_STATUS_RECOVERY);
5798 roop_count2 < (int) share->link_count;
5799 roop_count2 = spider_conn_link_idx_next(share->link_statuses,
5800 spider->conn_link_idx, roop_count2, share->link_count,
5801 SPIDER_LINK_STATUS_RECOVERY)
5802 ) {
5803 ulong sql_type;
5804 spider_db_handler *dbton_handler;
5805#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5806 if (spider->conn_kind[roop_count2] == SPIDER_CONN_KIND_MYSQL)
5807 {
5808#endif
5809 sql_type = SPIDER_SQL_TYPE_INSERT_SQL;
5810 conn = spider->conns[roop_count2];
5811 dbton_handler = spider->dbton_handler[conn->dbton_id];
5812 mta_conn_mutex_lock_already_backup =
5813 conn->mta_conn_mutex_lock_already;
5814 mta_conn_mutex_unlock_later_backup =
5815 conn->mta_conn_mutex_unlock_later;
5816 if (dbton_handler->need_lock_before_set_sql_for_exec(sql_type))
5817 {
5818 if (!mta_conn_mutex_lock_already_backup)
5819 {
5820 pthread_mutex_lock(&conn->mta_conn_mutex);
5821 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
5822 }
5823 }
5824 if ((error_num = dbton_handler->set_sql_for_exec(sql_type,
5825 roop_count2)))
5826 {
5827 DBUG_RETURN(error_num);
5828 }
5829 if (!dbton_handler->need_lock_before_set_sql_for_exec(sql_type))
5830 {
5831 if (!mta_conn_mutex_lock_already_backup)
5832 {
5833 pthread_mutex_lock(&conn->mta_conn_mutex);
5834 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
5835 }
5836 }
5837#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5838 } else {
5839 sql_type = SPIDER_SQL_TYPE_INSERT_HS;
5840 conn = spider->hs_w_conns[roop_count2];
5841 dbton_handler = spider->dbton_handler[conn->dbton_id];
5842 mta_conn_mutex_lock_already_backup =
5843 conn->mta_conn_mutex_lock_already;
5844 mta_conn_mutex_unlock_later_backup =
5845 conn->mta_conn_mutex_unlock_later;
5846 if (!mta_conn_mutex_lock_already_backup)
5847 {
5848 pthread_mutex_lock(&conn->mta_conn_mutex);
5849 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
5850 }
5851 }
5852#endif
5853 conn->need_mon = &spider->need_mons[roop_count2];
5854 conn->mta_conn_mutex_lock_already = TRUE;
5855 conn->mta_conn_mutex_unlock_later = TRUE;
5856 if ((error_num = spider_db_set_names(spider, conn, roop_count2)))
5857 {
5858 conn->mta_conn_mutex_lock_already =
5859 mta_conn_mutex_lock_already_backup;
5860 conn->mta_conn_mutex_unlock_later =
5861 mta_conn_mutex_unlock_later_backup;
5862 if (!mta_conn_mutex_unlock_later_backup)
5863 {
5864 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5865 pthread_mutex_unlock(&conn->mta_conn_mutex);
5866 }
5867 if (
5868 share->monitoring_kind[roop_count2] &&
5869 spider->need_mons[roop_count2]
5870 ) {
5871 error_num = spider_ping_table_mon_from_table(
5872 spider->trx,
5873 spider->trx->thd,
5874 share,
5875 roop_count2,
5876 (uint32) share->monitoring_sid[roop_count2],
5877 share->table_name,
5878 share->table_name_length,
5879 spider->conn_link_idx[roop_count2],
5880 NULL,
5881 0,
5882 share->monitoring_kind[roop_count2],
5883 share->monitoring_limit[roop_count2],
5884 share->monitoring_flag[roop_count2],
5885 TRUE
5886 );
5887 }
5888 DBUG_RETURN(error_num);
5889 }
5890 spider_conn_set_timeout_from_share(conn, roop_count2, spider->trx->thd,
5891 share);
5892 if (dbton_handler->execute_sql(
5893 sql_type,
5894 conn,
5895 -1,
5896 &spider->need_mons[roop_count2])
5897 ) {
5898 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
5899 spider->set_insert_to_pos_sql(SPIDER_SQL_TYPE_INSERT_SQL);
5900 error_num = spider_db_errorno(conn);
5901 if (error_num == HA_ERR_FOUND_DUPP_KEY)
5902 {
5903 conn->db_conn->set_dup_key_idx(spider, roop_count2);
5904 }
5905 conn->mta_conn_mutex_lock_already =
5906 mta_conn_mutex_lock_already_backup;
5907 conn->mta_conn_mutex_unlock_later =
5908 mta_conn_mutex_unlock_later_backup;
5909 if (!mta_conn_mutex_unlock_later_backup)
5910 {
5911 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5912 pthread_mutex_unlock(&conn->mta_conn_mutex);
5913 }
5914 if (
5915 error_num != ER_DUP_ENTRY &&
5916 error_num != ER_DUP_KEY &&
5917 error_num != HA_ERR_FOUND_DUPP_KEY &&
5918 share->monitoring_kind[roop_count2] &&
5919 spider->need_mons[roop_count2]
5920 ) {
5921 error_num = spider_ping_table_mon_from_table(
5922 spider->trx,
5923 spider->trx->thd,
5924 share,
5925 roop_count2,
5926 (uint32) share->monitoring_sid[roop_count2],
5927 share->table_name,
5928 share->table_name_length,
5929 spider->conn_link_idx[roop_count2],
5930 NULL,
5931 0,
5932 share->monitoring_kind[roop_count2],
5933 share->monitoring_limit[roop_count2],
5934 share->monitoring_flag[roop_count2],
5935 TRUE
5936 );
5937 }
5938 DBUG_RETURN(error_num);
5939 }
5940 conn->mta_conn_mutex_lock_already = mta_conn_mutex_lock_already_backup;
5941 conn->mta_conn_mutex_unlock_later = mta_conn_mutex_unlock_later_backup;
5942#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5943 if (conn->conn_kind != SPIDER_CONN_KIND_MYSQL)
5944 {
5945 uint roop_count;
5946 DBUG_PRINT("info",("spider conn=%p", conn));
5947 DBUG_PRINT("info",("spider result_list->hs_upd_rows=%llu",
5948 result_list->hs_upd_rows));
5949 for (roop_count = 0; roop_count < result_list->hs_upd_rows;
5950 roop_count++)
5951 {
5952 SPIDER_DB_RESULT *result;
5953 if (spider_bit_is_set(spider->db_request_phase, roop_count2))
5954 {
5955 spider_clear_bit(spider->db_request_phase, roop_count2);
5956 }
5957 st_spider_db_request_key request_key;
5958 request_key.spider_thread_id = spider->trx->spider_thread_id;
5959 request_key.query_id = spider->trx->thd->query_id;
5960 request_key.handler = spider;
5961 request_key.request_id = spider->db_request_id[roop_count2];
5962 request_key.next = NULL;
5963 if ((result = conn->db_conn->use_result(&request_key, &error_num)))
5964 {
5965 result->free_result();
5966 delete result;
5967 } else {
5968 if (!error_num)
5969 {
5970 error_num = spider_db_errorno(conn);
5971 }
5972 DBUG_RETURN(error_num);
5973 }
5974 }
5975 }
5976#endif
5977 if (!mta_conn_mutex_unlock_later_backup)
5978 {
5979 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5980 pthread_mutex_unlock(&conn->mta_conn_mutex);
5981 }
5982 if (first_insert_link_idx == -1)
5983 {
5984 first_insert_link_idx = roop_count2;
5985 first_insert_conn = conn;
5986 }
5987 }
5988
5989 conn = first_insert_conn;
5990 mta_conn_mutex_lock_already_backup = conn->mta_conn_mutex_lock_already;
5991 mta_conn_mutex_unlock_later_backup = conn->mta_conn_mutex_unlock_later;
5992 if (!mta_conn_mutex_lock_already_backup)
5993 {
5994 pthread_mutex_lock(&conn->mta_conn_mutex);
5995 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
5996 }
5997 conn->need_mon = &spider->need_mons[first_insert_link_idx];
5998 conn->mta_conn_mutex_lock_already = TRUE;
5999 conn->mta_conn_mutex_unlock_later = TRUE;
6000 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
6001 spider->set_insert_to_pos_sql(SPIDER_SQL_TYPE_INSERT_SQL);
6002 if (table->next_number_field &&
6003 (
6004 !table->auto_increment_field_not_null ||
6005 (
6006 !table->next_number_field->val_int() &&
6007 !(thd->variables.sql_mode & MODE_NO_AUTO_VALUE_ON_ZERO)
6008 )
6009 )
6010 ) {
6011 ulonglong last_insert_id;
6012 spider_db_handler *dbton_handler =
6013 spider->dbton_handler[conn->dbton_id];
6014 if (spider->store_last_insert_id)
6015 last_insert_id = spider->store_last_insert_id;
6016 else if ((error_num = dbton_handler->
6017 show_last_insert_id(first_insert_link_idx, last_insert_id)))
6018 {
6019 conn->mta_conn_mutex_lock_already =
6020 mta_conn_mutex_lock_already_backup;
6021 conn->mta_conn_mutex_unlock_later =
6022 mta_conn_mutex_unlock_later_backup;
6023 if (!mta_conn_mutex_unlock_later_backup)
6024 {
6025 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6026 pthread_mutex_unlock(&conn->mta_conn_mutex);
6027 }
6028 DBUG_RETURN(error_num);
6029 }
6030 table->next_number_field->set_notnull();
6031 if (
6032 (error_num = spider_db_update_auto_increment(spider,
6033 first_insert_link_idx)) ||
6034 (error_num = table->next_number_field->store(
6035 last_insert_id, TRUE))
6036 ) {
6037 conn->mta_conn_mutex_lock_already =
6038 mta_conn_mutex_lock_already_backup;
6039 conn->mta_conn_mutex_unlock_later =
6040 mta_conn_mutex_unlock_later_backup;
6041 if (!mta_conn_mutex_unlock_later_backup)
6042 {
6043 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6044 pthread_mutex_unlock(&conn->mta_conn_mutex);
6045 }
6046 DBUG_RETURN(error_num);
6047 }
6048 }
6049 conn->mta_conn_mutex_lock_already = mta_conn_mutex_lock_already_backup;
6050 conn->mta_conn_mutex_unlock_later = mta_conn_mutex_unlock_later_backup;
6051 if (!mta_conn_mutex_unlock_later_backup)
6052 {
6053 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6054 pthread_mutex_unlock(&conn->mta_conn_mutex);
6055 }
6056 spider->store_last_insert_id = 0;
6057#ifdef HA_CAN_BULK_ACCESS
6058 }
6059#endif
6060 }
6061 if (
6062 (bulk_end || !spider->bulk_insert) &&
6063 (error_num = spider_trx_check_link_idx_failed(spider))
6064 )
6065 DBUG_RETURN(error_num);
6066 DBUG_RETURN(0);
6067}
6068
6069#ifdef HA_CAN_BULK_ACCESS
6070int spider_db_bulk_bulk_insert(
6071 ha_spider *spider
6072) {
6073 int error_num = 0, first_insert_link_idx = -1, tmp_error_num;
6074 int roop_count2;
6075 SPIDER_SHARE *share = spider->share;
6076 SPIDER_CONN *conn, *first_insert_conn = NULL;
6077 TABLE *table = spider->get_table();
6078 THD *thd = spider->trx->thd;
6079 DBUG_ENTER("spider_db_bulk_bulk_insert");
6080 for (
6081 roop_count2 = spider_conn_link_idx_next(share->link_statuses,
6082 spider->conn_link_idx, -1, share->link_count,
6083 SPIDER_LINK_STATUS_RECOVERY);
6084 roop_count2 < (int) share->link_count;
6085 roop_count2 = spider_conn_link_idx_next(share->link_statuses,
6086 spider->conn_link_idx, roop_count2, share->link_count,
6087 SPIDER_LINK_STATUS_RECOVERY)
6088 ) {
6089#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6090 if (spider->conn_kind[roop_count2] == SPIDER_CONN_KIND_MYSQL)
6091 {
6092#endif
6093 conn = spider->conns[roop_count2];
6094#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6095 } else {
6096 conn = spider->hs_w_conns[roop_count2];
6097 }
6098#endif
6099 pthread_mutex_lock(&conn->mta_conn_mutex);
6100 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6101 conn->mta_conn_mutex_lock_already = TRUE;
6102 conn->mta_conn_mutex_unlock_later = TRUE;
6103 if ((tmp_error_num = spider_db_bulk_open_handler(spider, conn,
6104 roop_count2)))
6105 {
6106 error_num = tmp_error_num;
6107 }
6108 conn->mta_conn_mutex_lock_already = FALSE;
6109 conn->mta_conn_mutex_unlock_later = FALSE;
6110#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6111 if (conn->conn_kind != SPIDER_CONN_KIND_MYSQL)
6112 {
6113 uint roop_count;
6114 SPIDER_RESULT_LIST *result_list = &spider->result_list;
6115 DBUG_PRINT("info",("spider conn=%p", conn));
6116 DBUG_PRINT("info",("spider result_list->hs_upd_rows=%llu",
6117 result_list->hs_upd_rows));
6118 for (roop_count = 0; roop_count < result_list->hs_upd_rows;
6119 roop_count++)
6120 {
6121 SPIDER_DB_RESULT *result;
6122 if (spider_bit_is_set(spider->db_request_phase, roop_count2))
6123 {
6124 spider_clear_bit(spider->db_request_phase, roop_count2);
6125 }
6126 st_spider_db_request_key request_key;
6127 request_key.spider_thread_id = spider->trx->spider_thread_id;
6128 request_key.query_id = spider->trx->thd->query_id;
6129 request_key.handler = spider;
6130 request_key.request_id = spider->db_request_id[roop_count2];
6131 request_key.next = NULL;
6132 if ((result = conn->db_conn->use_result(&request_key, &error_num)))
6133 {
6134 result->free_result();
6135 delete result;
6136 } else {
6137 if (!error_num)
6138 {
6139 error_num = spider_db_errorno(conn);
6140 }
6141 DBUG_RETURN(error_num);
6142 }
6143 }
6144 }
6145#endif
6146 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6147 pthread_mutex_unlock(&conn->mta_conn_mutex);
6148 if (first_insert_link_idx == -1)
6149 {
6150 first_insert_link_idx = roop_count2;
6151 first_insert_conn = conn;
6152 }
6153 }
6154
6155 conn = first_insert_conn;
6156 pthread_mutex_lock(&conn->mta_conn_mutex);
6157 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6158 conn->need_mon = &spider->need_mons[first_insert_link_idx];
6159 conn->mta_conn_mutex_lock_already = TRUE;
6160 conn->mta_conn_mutex_unlock_later = TRUE;
6161 if (table->next_number_field &&
6162 (
6163 !table->auto_increment_field_not_null ||
6164 (
6165 !table->next_number_field->val_int() &&
6166 !(thd->variables.sql_mode & MODE_NO_AUTO_VALUE_ON_ZERO)
6167 )
6168 )
6169 ) {
6170 ulonglong last_insert_id;
6171 if (spider->store_last_insert_id)
6172 last_insert_id = spider->store_last_insert_id;
6173 else
6174 last_insert_id = conn->db_conn->last_insert_id();
6175 table->next_number_field->set_notnull();
6176 if (
6177 (tmp_error_num = spider_db_update_auto_increment(spider,
6178 first_insert_link_idx)) ||
6179 (tmp_error_num = table->next_number_field->store(
6180 last_insert_id, TRUE))
6181 ) {
6182 error_num = tmp_error_num;
6183 }
6184 }
6185 conn->mta_conn_mutex_lock_already = FALSE;
6186 conn->mta_conn_mutex_unlock_later = FALSE;
6187 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6188 pthread_mutex_unlock(&conn->mta_conn_mutex);
6189 spider->store_last_insert_id = 0;
6190 DBUG_RETURN(error_num);
6191}
6192#endif
6193
6194int spider_db_update_auto_increment(
6195 ha_spider *spider,
6196 int link_idx
6197) {
6198 int roop_count;
6199 THD *thd = spider->trx->thd;
6200 ulonglong last_insert_id, affected_rows;
6201 SPIDER_SHARE *share = spider->share;
6202 TABLE *table = spider->get_table();
6203 int auto_increment_mode = spider_param_auto_increment_mode(thd,
6204 share->auto_increment_mode);
6205 DBUG_ENTER("spider_db_update_auto_increment");
6206 if (
6207 auto_increment_mode == 2 ||
6208 (auto_increment_mode == 3 && !table->auto_increment_field_not_null)
6209 ) {
6210 last_insert_id = spider->conns[link_idx]->db_conn->last_insert_id();
6211#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6212 if (spider->conn_kind[link_idx] == SPIDER_CONN_KIND_MYSQL)
6213 {
6214#endif
6215 affected_rows = spider->conns[link_idx]->db_conn->affected_rows();
6216#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6217 } else {
6218 affected_rows = spider->result_list.hs_upd_rows;
6219 }
6220#endif
6221 DBUG_PRINT("info",("spider last_insert_id=%llu", last_insert_id));
6222 share->lgtm_tblhnd_share->auto_increment_value =
6223 last_insert_id + affected_rows;
6224 DBUG_PRINT("info",("spider auto_increment_value=%llu",
6225 share->lgtm_tblhnd_share->auto_increment_value));
6226/*
6227 thd->record_first_successful_insert_id_in_cur_stmt(last_insert_id);
6228*/
6229 if (
6230 thd->first_successful_insert_id_in_cur_stmt == 0 ||
6231 thd->first_successful_insert_id_in_cur_stmt > last_insert_id
6232 ) {
6233 bool first_set = (thd->first_successful_insert_id_in_cur_stmt == 0);
6234 thd->first_successful_insert_id_in_cur_stmt = last_insert_id;
6235 if (
6236 table->s->next_number_keypart == 0 &&
6237 mysql_bin_log.is_open() &&
6238#if MYSQL_VERSION_ID < 50500
6239 !thd->current_stmt_binlog_row_based
6240#else
6241 !thd->is_current_stmt_binlog_format_row()
6242#endif
6243 ) {
6244 if (
6245 spider->check_partitioned() &&
6246 thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0
6247 ) {
6248 DBUG_PRINT("info",("spider table partitioning"));
6249 Discrete_interval *current =
6250 thd->auto_inc_intervals_in_cur_stmt_for_binlog.get_current();
6251 current->replace(last_insert_id, affected_rows, 1);
6252 } else {
6253 DBUG_PRINT("info",("spider table"));
6254 thd->auto_inc_intervals_in_cur_stmt_for_binlog.append(
6255 last_insert_id, affected_rows, 1);
6256 }
6257 if (affected_rows > 1 || !first_set)
6258 {
6259 for (roop_count = first_set ? 1 : 0;
6260 roop_count < (int) affected_rows;
6261 roop_count++)
6262 push_warning_printf(thd, SPIDER_WARN_LEVEL_NOTE,
6263 ER_SPIDER_AUTOINC_VAL_IS_DIFFERENT_NUM,
6264 ER_SPIDER_AUTOINC_VAL_IS_DIFFERENT_STR);
6265 }
6266 }
6267 } else {
6268 if (
6269 table->s->next_number_keypart == 0 &&
6270 mysql_bin_log.is_open() &&
6271#if MYSQL_VERSION_ID < 50500
6272 !thd->current_stmt_binlog_row_based
6273#else
6274 !thd->is_current_stmt_binlog_format_row()
6275#endif
6276 ) {
6277 for (roop_count = 0; roop_count < (int) affected_rows; roop_count++)
6278 push_warning_printf(thd, SPIDER_WARN_LEVEL_NOTE,
6279 ER_SPIDER_AUTOINC_VAL_IS_DIFFERENT_NUM,
6280 ER_SPIDER_AUTOINC_VAL_IS_DIFFERENT_STR);
6281 }
6282 }
6283 }
6284 DBUG_RETURN(0);
6285}
6286
6287int spider_db_bulk_update_size_limit(
6288 ha_spider *spider,
6289 TABLE *table
6290) {
6291 int error_num, roop_count;
6292 SPIDER_SHARE *share = spider->share;
6293 SPIDER_RESULT_LIST *result_list = &spider->result_list;
6294 SPIDER_CONN *conn;
6295 ha_rows dup_key_found = 0;
6296 DBUG_ENTER("spider_db_bulk_update_size_limit");
6297
6298 if (result_list->bulk_update_mode == 1)
6299 {
6300 /* execute bulk updating */
6301 for (
6302 roop_count = spider_conn_link_idx_next(share->link_statuses,
6303 spider->conn_link_idx, -1, share->link_count,
6304 SPIDER_LINK_STATUS_RECOVERY);
6305 roop_count < (int) share->link_count;
6306 roop_count = spider_conn_link_idx_next(share->link_statuses,
6307 spider->conn_link_idx, roop_count, share->link_count,
6308 SPIDER_LINK_STATUS_RECOVERY)
6309 ) {
6310 conn = spider->conns[roop_count];
6311 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
6312 if (dbton_hdl->need_lock_before_set_sql_for_exec(
6313 SPIDER_SQL_TYPE_BULK_UPDATE_SQL))
6314 {
6315 pthread_mutex_lock(&conn->mta_conn_mutex);
6316 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6317 }
6318 if ((error_num = dbton_hdl->set_sql_for_exec(
6319 SPIDER_SQL_TYPE_BULK_UPDATE_SQL, roop_count)))
6320 {
6321 DBUG_RETURN(error_num);
6322 }
6323 if (!dbton_hdl->need_lock_before_set_sql_for_exec(
6324 SPIDER_SQL_TYPE_BULK_UPDATE_SQL))
6325 {
6326 pthread_mutex_lock(&conn->mta_conn_mutex);
6327 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6328 }
6329 if ((error_num = spider_db_query_for_bulk_update(
6330 spider, conn, roop_count, &dup_key_found)))
6331 DBUG_RETURN(error_num);
6332 }
6333 spider->reset_sql_sql(SPIDER_SQL_TYPE_BULK_UPDATE_SQL);
6334 } else {
6335 /* store query to temporary tables */
6336 if ((error_num = spider->mk_bulk_tmp_table_and_bulk_start()))
6337 {
6338 goto error_mk_table;
6339 }
6340 if ((error_num = spider->bulk_tmp_table_insert()))
6341 {
6342 goto error_write_row;
6343 }
6344 spider->reset_sql_sql(SPIDER_SQL_TYPE_BULK_UPDATE_SQL);
6345 }
6346 DBUG_RETURN(0);
6347
6348error_write_row:
6349 spider->bulk_tmp_table_end_bulk_insert();
6350 spider->rm_bulk_tmp_table();
6351 spider->reset_sql_sql(SPIDER_SQL_TYPE_BULK_UPDATE_SQL);
6352error_mk_table:
6353 DBUG_RETURN(error_num);
6354}
6355
6356int spider_db_bulk_update_end(
6357 ha_spider *spider,
6358 ha_rows *dup_key_found
6359) {
6360 int error_num = 0, error_num2, roop_count;
6361 THD *thd = spider->trx->thd;
6362 SPIDER_SHARE *share = spider->share;
6363 SPIDER_CONN *conn;
6364 bool is_error = thd->is_error();
6365 DBUG_ENTER("spider_db_bulk_update_end");
6366
6367 if (spider->bulk_tmp_table_created())
6368 {
6369 if ((error_num2 = spider->bulk_tmp_table_end_bulk_insert()))
6370 {
6371 error_num = error_num2;
6372 }
6373
6374 if (!is_error)
6375 {
6376 if (error_num)
6377 goto error_last_query;
6378
6379 if ((error_num = spider->bulk_tmp_table_rnd_init()))
6380 {
6381 goto error_rnd_init;
6382 }
6383
6384 while (!(error_num = spider->bulk_tmp_table_rnd_next()))
6385 {
6386 for (
6387 roop_count = spider_conn_link_idx_next(share->link_statuses,
6388 spider->conn_link_idx, -1, share->link_count,
6389 SPIDER_LINK_STATUS_RECOVERY);
6390 roop_count < (int) share->link_count;
6391 roop_count = spider_conn_link_idx_next(share->link_statuses,
6392 spider->conn_link_idx, roop_count, share->link_count,
6393 SPIDER_LINK_STATUS_RECOVERY)
6394 ) {
6395 conn = spider->conns[roop_count];
6396 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
6397 if (dbton_hdl->need_lock_before_set_sql_for_exec(
6398 SPIDER_SQL_TYPE_BULK_UPDATE_SQL))
6399 {
6400 pthread_mutex_lock(&conn->mta_conn_mutex);
6401 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6402 }
6403 if ((error_num = dbton_hdl->set_sql_for_exec(
6404 SPIDER_SQL_TYPE_BULK_UPDATE_SQL, roop_count)))
6405 {
6406 if (error_num == ER_SPIDER_COND_SKIP_NUM)
6407 {
6408 if (dbton_hdl->need_lock_before_set_sql_for_exec(
6409 SPIDER_SQL_TYPE_BULK_UPDATE_SQL))
6410 {
6411 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6412 pthread_mutex_unlock(&conn->mta_conn_mutex);
6413 }
6414 continue;
6415 }
6416 DBUG_RETURN(error_num);
6417 }
6418 if (!dbton_hdl->need_lock_before_set_sql_for_exec(
6419 SPIDER_SQL_TYPE_BULK_UPDATE_SQL))
6420 {
6421 pthread_mutex_lock(&conn->mta_conn_mutex);
6422 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6423 }
6424 if ((error_num = spider_db_query_for_bulk_update(
6425 spider, conn, roop_count, dup_key_found)))
6426 goto error_query;
6427 }
6428 }
6429 if (error_num != HA_ERR_END_OF_FILE)
6430 goto error_rnd_next;
6431
6432 spider->bulk_tmp_table_rnd_end();
6433 }
6434 }
6435
6436 if (!is_error)
6437 {
6438 if (!spider->sql_is_empty(SPIDER_SQL_TYPE_BULK_UPDATE_SQL))
6439 {
6440 for (
6441 roop_count = spider_conn_link_idx_next(share->link_statuses,
6442 spider->conn_link_idx, -1, share->link_count,
6443 SPIDER_LINK_STATUS_RECOVERY);
6444 roop_count < (int) share->link_count;
6445 roop_count = spider_conn_link_idx_next(share->link_statuses,
6446 spider->conn_link_idx, roop_count, share->link_count,
6447 SPIDER_LINK_STATUS_RECOVERY)
6448 ) {
6449 conn = spider->conns[roop_count];
6450 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
6451 if (dbton_hdl->need_lock_before_set_sql_for_exec(
6452 SPIDER_SQL_TYPE_BULK_UPDATE_SQL))
6453 {
6454 pthread_mutex_lock(&conn->mta_conn_mutex);
6455 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6456 }
6457 if ((error_num = dbton_hdl->set_sql_for_exec(
6458 SPIDER_SQL_TYPE_BULK_UPDATE_SQL, roop_count)))
6459 {
6460 DBUG_RETURN(error_num);
6461 }
6462 if (!dbton_hdl->need_lock_before_set_sql_for_exec(
6463 SPIDER_SQL_TYPE_BULK_UPDATE_SQL))
6464 {
6465 pthread_mutex_lock(&conn->mta_conn_mutex);
6466 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6467 }
6468 if ((error_num = spider_db_query_for_bulk_update(
6469 spider, conn, roop_count, dup_key_found)))
6470 goto error_last_query;
6471 }
6472 }
6473 }
6474 spider->rm_bulk_tmp_table();
6475 spider->reset_sql_sql(SPIDER_SQL_TYPE_BULK_UPDATE_SQL);
6476 DBUG_RETURN(0);
6477
6478error_query:
6479error_rnd_next:
6480 spider->bulk_tmp_table_rnd_end();
6481error_rnd_init:
6482error_last_query:
6483 spider->rm_bulk_tmp_table();
6484 spider->reset_sql_sql(SPIDER_SQL_TYPE_BULK_UPDATE_SQL);
6485 DBUG_RETURN(error_num);
6486}
6487
6488int spider_db_bulk_update(
6489 ha_spider *spider,
6490 TABLE *table,
6491 my_ptrdiff_t ptr_diff
6492) {
6493 int error_num;
6494 DBUG_ENTER("spider_db_bulk_update");
6495
6496 if ((error_num = spider->append_update_sql(table, ptr_diff, TRUE)))
6497 DBUG_RETURN(error_num);
6498
6499 if (
6500 spider->sql_is_filled_up(SPIDER_SQL_TYPE_BULK_UPDATE_SQL) &&
6501 (error_num = spider_db_bulk_update_size_limit(spider, table))
6502 )
6503 DBUG_RETURN(error_num);
6504 DBUG_RETURN(0);
6505}
6506
6507int spider_db_update(
6508 ha_spider *spider,
6509 TABLE *table,
6510 const uchar *old_data
6511) {
6512 int error_num, roop_count;
6513 SPIDER_SHARE *share = spider->share;
6514 SPIDER_CONN *conn;
6515 SPIDER_RESULT_LIST *result_list = &spider->result_list;
6516 my_ptrdiff_t ptr_diff = PTR_BYTE_DIFF(old_data, table->record[0]);
6517 DBUG_ENTER("spider_db_update");
6518 if (result_list->bulk_update_mode)
6519 DBUG_RETURN(spider_db_bulk_update(spider, table, ptr_diff));
6520
6521 if ((error_num = spider->append_update_sql(table, ptr_diff, FALSE)))
6522 DBUG_RETURN(error_num);
6523
6524 for (
6525 roop_count = spider_conn_link_idx_next(share->link_statuses,
6526 spider->conn_link_idx, -1, share->link_count,
6527 SPIDER_LINK_STATUS_RECOVERY);
6528 roop_count < (int) share->link_count;
6529 roop_count = spider_conn_link_idx_next(share->link_statuses,
6530 spider->conn_link_idx, roop_count, share->link_count,
6531 SPIDER_LINK_STATUS_RECOVERY)
6532 ) {
6533 conn = spider->conns[roop_count];
6534 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
6535#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000
6536 conn->ignore_dup_key = spider->ignore_dup_key;
6537#endif
6538 if (dbton_hdl->need_lock_before_set_sql_for_exec(
6539 SPIDER_SQL_TYPE_UPDATE_SQL))
6540 {
6541 pthread_mutex_lock(&conn->mta_conn_mutex);
6542 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6543 }
6544 if ((error_num = dbton_hdl->set_sql_for_exec(
6545 SPIDER_SQL_TYPE_UPDATE_SQL, roop_count)))
6546 {
6547 DBUG_RETURN(error_num);
6548 }
6549 if (!dbton_hdl->need_lock_before_set_sql_for_exec(
6550 SPIDER_SQL_TYPE_UPDATE_SQL))
6551 {
6552 pthread_mutex_lock(&conn->mta_conn_mutex);
6553 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6554 }
6555 conn->need_mon = &spider->need_mons[roop_count];
6556 conn->mta_conn_mutex_lock_already = TRUE;
6557 conn->mta_conn_mutex_unlock_later = TRUE;
6558 if ((error_num = spider_db_set_names(spider, conn, roop_count)))
6559 {
6560 conn->mta_conn_mutex_lock_already = FALSE;
6561 conn->mta_conn_mutex_unlock_later = FALSE;
6562 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6563 pthread_mutex_unlock(&conn->mta_conn_mutex);
6564 if (
6565 share->monitoring_kind[roop_count] &&
6566 spider->need_mons[roop_count]
6567 ) {
6568 error_num = spider_ping_table_mon_from_table(
6569 spider->trx,
6570 spider->trx->thd,
6571 share,
6572 roop_count,
6573 (uint32) share->monitoring_sid[roop_count],
6574 share->table_name,
6575 share->table_name_length,
6576 spider->conn_link_idx[roop_count],
6577 NULL,
6578 0,
6579 share->monitoring_kind[roop_count],
6580 share->monitoring_limit[roop_count],
6581 share->monitoring_flag[roop_count],
6582 TRUE
6583 );
6584 }
6585 DBUG_RETURN(error_num);
6586 }
6587 spider_conn_set_timeout_from_share(conn, roop_count, spider->trx->thd,
6588 share);
6589 if (dbton_hdl->execute_sql(
6590 SPIDER_SQL_TYPE_UPDATE_SQL,
6591 conn,
6592 -1,
6593 &spider->need_mons[roop_count])
6594 ) {
6595 conn->mta_conn_mutex_lock_already = FALSE;
6596 conn->mta_conn_mutex_unlock_later = FALSE;
6597 error_num = spider_db_errorno(conn);
6598 if (
6599 error_num != ER_DUP_ENTRY &&
6600 error_num != ER_DUP_KEY &&
6601 error_num != HA_ERR_FOUND_DUPP_KEY &&
6602 share->monitoring_kind[roop_count] &&
6603 spider->need_mons[roop_count]
6604 ) {
6605 error_num = spider_ping_table_mon_from_table(
6606 spider->trx,
6607 spider->trx->thd,
6608 share,
6609 roop_count,
6610 (uint32) share->monitoring_sid[roop_count],
6611 share->table_name,
6612 share->table_name_length,
6613 spider->conn_link_idx[roop_count],
6614 NULL,
6615 0,
6616 share->monitoring_kind[roop_count],
6617 share->monitoring_limit[roop_count],
6618 share->monitoring_flag[roop_count],
6619 TRUE
6620 );
6621 }
6622 DBUG_RETURN(error_num);
6623 }
6624
6625 if (
6626 !conn->db_conn->affected_rows() &&
6627 share->link_statuses[roop_count] == SPIDER_LINK_STATUS_RECOVERY &&
6628 spider->pk_update
6629 ) {
6630 /* insert */
6631 if ((error_num = dbton_hdl->append_insert_for_recovery(
6632 SPIDER_SQL_TYPE_INSERT_SQL, roop_count)))
6633 {
6634 conn->mta_conn_mutex_lock_already = FALSE;
6635 conn->mta_conn_mutex_unlock_later = FALSE;
6636 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6637 pthread_mutex_unlock(&conn->mta_conn_mutex);
6638 DBUG_RETURN(error_num);
6639 }
6640 spider_conn_set_timeout_from_share(conn, roop_count, spider->trx->thd,
6641 share);
6642 if (dbton_hdl->execute_sql(
6643 SPIDER_SQL_TYPE_INSERT_SQL,
6644 conn,
6645 -1,
6646 &spider->need_mons[roop_count])
6647 ) {
6648 conn->mta_conn_mutex_lock_already = FALSE;
6649 conn->mta_conn_mutex_unlock_later = FALSE;
6650 error_num = spider_db_errorno(conn);
6651 if (
6652 error_num != ER_DUP_ENTRY &&
6653 error_num != ER_DUP_KEY &&
6654 error_num != HA_ERR_FOUND_DUPP_KEY &&
6655 share->monitoring_kind[roop_count] &&
6656 spider->need_mons[roop_count]
6657 ) {
6658 error_num = spider_ping_table_mon_from_table(
6659 spider->trx,
6660 spider->trx->thd,
6661 share,
6662 roop_count,
6663 (uint32) share->monitoring_sid[roop_count],
6664 share->table_name,
6665 share->table_name_length,
6666 spider->conn_link_idx[roop_count],
6667 NULL,
6668 0,
6669 share->monitoring_kind[roop_count],
6670 share->monitoring_limit[roop_count],
6671 share->monitoring_flag[roop_count],
6672 TRUE
6673 );
6674 }
6675 DBUG_RETURN(error_num);
6676 }
6677 }
6678 conn->mta_conn_mutex_lock_already = FALSE;
6679 conn->mta_conn_mutex_unlock_later = FALSE;
6680 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6681 pthread_mutex_unlock(&conn->mta_conn_mutex);
6682 result_list->update_sqls[roop_count].length(0);
6683 }
6684 spider->reset_sql_sql(SPIDER_SQL_TYPE_UPDATE_SQL);
6685 DBUG_RETURN(0);
6686}
6687
6688#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
6689#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS_WITH_HS
6690int spider_db_direct_update(
6691 ha_spider *spider,
6692 TABLE *table,
6693 KEY_MULTI_RANGE *ranges,
6694 uint range_count,
6695 ha_rows *update_rows
6696) {
6697 int error_num, roop_count;
6698 SPIDER_SHARE *share = spider->share;
6699 SPIDER_CONN *conn;
6700 SPIDER_RESULT_LIST *result_list = &spider->result_list;
6701 bool counted = FALSE;
6702 st_select_lex *select_lex;
6703 longlong select_limit;
6704 longlong offset_limit;
6705 DBUG_ENTER("spider_db_direct_update");
6706
6707 spider_set_result_list_param(spider);
6708 result_list->finish_flg = FALSE;
6709 DBUG_PRINT("info", ("spider do_direct_update=%s",
6710 spider->do_direct_update ? "TRUE" : "FALSE"));
6711 DBUG_PRINT("info", ("spider direct_update_kinds=%u",
6712 spider->direct_update_kinds));
6713#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6714 if (
6715#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
6716 (
6717 spider->do_direct_update &&
6718 (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL)
6719 ) ||
6720 (
6721 !spider->do_direct_update &&
6722#endif
6723 (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
6724#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
6725 )
6726#endif
6727 ) {
6728#endif
6729 if ((error_num = spider->append_update_sql_part()))
6730 DBUG_RETURN(error_num);
6731#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6732 }
6733#endif
6734
6735/*
6736 SQL access -> SQL remote access
6737 !spider->do_direct_update &&
6738 (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
6739
6740 SQL access -> SQL remote access with dirct_update
6741 spider->do_direct_update &&
6742 spider->direct_update_kinds == SPIDER_SQL_KIND_SQL &&
6743 spider->direct_update_fields
6744
6745 Handlersocket access -> SQL remote access with dirct_update
6746 spider->do_direct_update &&
6747 (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL)
6748
6749 Handlersocket access -> Handlersocket access
6750 spider->do_direct_update &&
6751 (spider->direct_update_kinds & SPIDER_SQL_KIND_HS)
6752*/
6753
6754#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6755 if (spider->hs_increment || spider->hs_decrement)
6756 {
6757 if (
6758 (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL) &&
6759 (error_num = spider->append_increment_update_set_sql_part())
6760 ) {
6761 DBUG_RETURN(error_num);
6762 }
6763 } else {
6764#endif
6765#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
6766 if (!spider->do_direct_update)
6767 {
6768#endif
6769 if (
6770 (spider->sql_kinds & SPIDER_SQL_KIND_SQL) &&
6771 (error_num = spider->append_update_set_sql_part())
6772 ) {
6773 DBUG_RETURN(error_num);
6774 }
6775#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
6776 } else {
6777 if (
6778 (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL) &&
6779 (error_num = spider->append_direct_update_set_sql_part())
6780 ) {
6781 DBUG_RETURN(error_num);
6782 }
6783#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6784 if (
6785 (spider->direct_update_kinds & SPIDER_SQL_KIND_HS) &&
6786 (error_num = spider->append_direct_update_set_hs_part())
6787 ) {
6788 DBUG_RETURN(error_num);
6789 }
6790#endif
6791 }
6792#endif
6793#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6794 }
6795#endif
6796
6797 result_list->desc_flg = FALSE;
6798 result_list->sorted = TRUE;
6799 if (spider->active_index == MAX_KEY)
6800 result_list->key_info = NULL;
6801 else
6802 result_list->key_info = &table->key_info[spider->active_index];
6803 spider_get_select_limit(spider, &select_lex, &select_limit, &offset_limit);
6804 result_list->limit_num =
6805 result_list->internal_limit >= select_limit ?
6806 select_limit : result_list->internal_limit;
6807 result_list->internal_offset += offset_limit;
6808 if (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL)
6809 {
6810 if (
6811 (error_num = spider->append_key_where_sql_part(
6812 (ranges && ranges->start_key.key) ? &ranges->start_key : NULL,
6813 (ranges && ranges->end_key.key) ? &ranges->end_key : NULL,
6814 SPIDER_SQL_TYPE_UPDATE_SQL)) ||
6815 (error_num = spider->
6816 append_key_order_for_direct_order_limit_with_alias_sql_part(
6817 NULL, 0, SPIDER_SQL_TYPE_UPDATE_SQL)) ||
6818 (error_num = spider->append_limit_sql_part(
6819 result_list->internal_offset, result_list->limit_num,
6820 SPIDER_SQL_TYPE_UPDATE_SQL))
6821 ) {
6822 DBUG_RETURN(error_num);
6823 }
6824 }
6825#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6826 if (spider->direct_update_kinds & SPIDER_SQL_KIND_HS)
6827 {
6828 if (
6829 (error_num = spider->append_key_where_hs_part(
6830 (ranges && ranges->start_key.key) ? &ranges->start_key : NULL,
6831 (ranges && ranges->end_key.key) ? &ranges->end_key : NULL,
6832 SPIDER_SQL_TYPE_UPDATE_HS)) ||
6833 (error_num = spider->append_limit_hs_part(
6834 result_list->internal_offset, result_list->limit_num,
6835 SPIDER_SQL_TYPE_UPDATE_HS))
6836 ) {
6837 DBUG_RETURN(error_num);
6838 }
6839 }
6840#endif
6841
6842 for (
6843 roop_count = spider_conn_link_idx_next(share->link_statuses,
6844 spider->conn_link_idx, -1, share->link_count,
6845 SPIDER_LINK_STATUS_RECOVERY);
6846 roop_count < (int) share->link_count;
6847 roop_count = spider_conn_link_idx_next(share->link_statuses,
6848 spider->conn_link_idx, roop_count, share->link_count,
6849 SPIDER_LINK_STATUS_RECOVERY)
6850 ) {
6851 ulong sql_type;
6852#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6853 if (!spider_bit_is_set(spider->do_hs_direct_update, roop_count))
6854 {
6855#endif
6856 DBUG_PRINT("info", ("spider exec sql"));
6857 conn = spider->conns[roop_count];
6858 sql_type = SPIDER_SQL_TYPE_UPDATE_SQL;
6859#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6860 } else {
6861 DBUG_PRINT("info", ("spider exec hs"));
6862 conn = spider->hs_w_conns[roop_count];
6863 sql_type = SPIDER_SQL_TYPE_UPDATE_HS;
6864 }
6865#endif
6866 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
6867 if (dbton_hdl->need_lock_before_set_sql_for_exec(sql_type))
6868 {
6869 pthread_mutex_lock(&conn->mta_conn_mutex);
6870 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6871 }
6872 if ((error_num = dbton_hdl->set_sql_for_exec(sql_type, roop_count)))
6873 {
6874 DBUG_RETURN(error_num);
6875 }
6876 if (!dbton_hdl->need_lock_before_set_sql_for_exec(sql_type))
6877 {
6878 pthread_mutex_lock(&conn->mta_conn_mutex);
6879 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6880 }
6881#ifdef HA_CAN_BULK_ACCESS
6882 if (spider->is_bulk_access_clone)
6883 {
6884 spider->connection_ids[roop_count] = conn->connection_id;
6885 spider_trx_add_bulk_access_conn(spider->trx, conn);
6886 } else {
6887#endif
6888 conn->need_mon = &spider->need_mons[roop_count];
6889 conn->mta_conn_mutex_lock_already = TRUE;
6890 conn->mta_conn_mutex_unlock_later = TRUE;
6891 if ((error_num = spider_db_set_names(spider, conn, roop_count)))
6892 {
6893 conn->mta_conn_mutex_lock_already = FALSE;
6894 conn->mta_conn_mutex_unlock_later = FALSE;
6895 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6896 pthread_mutex_unlock(&conn->mta_conn_mutex);
6897 if (
6898 share->monitoring_kind[roop_count] &&
6899 spider->need_mons[roop_count]
6900 ) {
6901 error_num = spider_ping_table_mon_from_table(
6902 spider->trx,
6903 spider->trx->thd,
6904 share,
6905 roop_count,
6906 (uint32) share->monitoring_sid[roop_count],
6907 share->table_name,
6908 share->table_name_length,
6909 spider->conn_link_idx[roop_count],
6910 NULL,
6911 0,
6912 share->monitoring_kind[roop_count],
6913 share->monitoring_limit[roop_count],
6914 share->monitoring_flag[roop_count],
6915 TRUE
6916 );
6917 }
6918 DBUG_RETURN(error_num);
6919 }
6920 spider_conn_set_timeout_from_share(conn, roop_count, spider->trx->thd,
6921 share);
6922 if (
6923 (error_num = dbton_hdl->execute_sql(
6924 sql_type,
6925 conn,
6926 -1,
6927 &spider->need_mons[roop_count])
6928 ) &&
6929 (error_num != HA_ERR_FOUND_DUPP_KEY || !spider->ignore_dup_key)
6930 ) {
6931 conn->mta_conn_mutex_lock_already = FALSE;
6932 conn->mta_conn_mutex_unlock_later = FALSE;
6933 error_num = spider_db_errorno(conn);
6934 if (
6935 error_num != ER_DUP_ENTRY &&
6936 error_num != ER_DUP_KEY &&
6937 error_num != HA_ERR_FOUND_DUPP_KEY &&
6938 share->monitoring_kind[roop_count] &&
6939 spider->need_mons[roop_count]
6940 ) {
6941 error_num = spider_ping_table_mon_from_table(
6942 spider->trx,
6943 spider->trx->thd,
6944 share,
6945 roop_count,
6946 (uint32) share->monitoring_sid[roop_count],
6947 share->table_name,
6948 share->table_name_length,
6949 spider->conn_link_idx[roop_count],
6950 NULL,
6951 0,
6952 share->monitoring_kind[roop_count],
6953 share->monitoring_limit[roop_count],
6954 share->monitoring_flag[roop_count],
6955 TRUE
6956 );
6957 }
6958 DBUG_RETURN(error_num);
6959 }
6960#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6961 if (!spider_bit_is_set(spider->do_hs_direct_update, roop_count))
6962 {
6963#endif
6964 if (!counted)
6965 {
6966 *update_rows = spider->conns[roop_count]->db_conn->affected_rows();
6967 DBUG_PRINT("info", ("spider update_rows = %llu", *update_rows));
6968 counted = TRUE;
6969 }
6970#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6971 } else {
6972 SPIDER_DB_RESULT *result;
6973 if (spider_bit_is_set(spider->db_request_phase, roop_count))
6974 {
6975 spider_clear_bit(spider->db_request_phase, roop_count);
6976 }
6977 st_spider_db_request_key request_key;
6978 request_key.spider_thread_id = spider->trx->spider_thread_id;
6979 request_key.query_id = spider->trx->thd->query_id;
6980 request_key.handler = spider;
6981 request_key.request_id = spider->db_request_id[roop_count];
6982 request_key.next = NULL;
6983 if ((result = conn->db_conn->use_result(&request_key, &error_num)))
6984 {
6985 if (!counted)
6986 {
6987 *update_rows = conn->db_conn->affected_rows();
6988 DBUG_PRINT("info", ("spider update_rows = %llu", *update_rows));
6989 counted = TRUE;
6990 }
6991 result->free_result();
6992 delete result;
6993 } else {
6994 if (!error_num)
6995 {
6996 error_num = spider_db_errorno(conn);
6997 }
6998 conn->mta_conn_mutex_lock_already = FALSE;
6999 conn->mta_conn_mutex_unlock_later = FALSE;
7000 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7001 pthread_mutex_unlock(&conn->mta_conn_mutex);
7002 DBUG_RETURN(error_num);
7003 }
7004 }
7005#endif
7006#ifdef HA_CAN_BULK_ACCESS
7007 }
7008#endif
7009 conn->mta_conn_mutex_lock_already = FALSE;
7010 conn->mta_conn_mutex_unlock_later = FALSE;
7011 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7012 pthread_mutex_unlock(&conn->mta_conn_mutex);
7013 }
7014 spider->reset_sql_sql(SPIDER_SQL_TYPE_UPDATE_SQL);
7015#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7016 spider->reset_hs_sql(SPIDER_SQL_TYPE_UPDATE_HS);
7017 spider->reset_hs_keys(SPIDER_SQL_TYPE_UPDATE_HS);
7018 spider->reset_hs_upds(SPIDER_SQL_TYPE_UPDATE_HS);
7019#endif
7020 DBUG_RETURN(0);
7021}
7022#else
7023int spider_db_direct_update(
7024 ha_spider *spider,
7025 TABLE *table,
7026 ha_rows *update_rows
7027) {
7028 int error_num, roop_count;
7029 SPIDER_SHARE *share = spider->share;
7030 SPIDER_CONN *conn;
7031 SPIDER_RESULT_LIST *result_list = &spider->result_list;
7032 bool counted = FALSE;
7033 st_select_lex *select_lex;
7034 longlong select_limit;
7035 longlong offset_limit;
7036 DBUG_ENTER("spider_db_direct_update");
7037
7038 spider_set_result_list_param(spider);
7039 result_list->finish_flg = FALSE;
7040 DBUG_PRINT("info", ("spider do_direct_update=%s",
7041 spider->do_direct_update ? "TRUE" : "FALSE"));
7042 DBUG_PRINT("info", ("spider direct_update_kinds=%u",
7043 spider->direct_update_kinds));
7044 if ((error_num = spider->append_update_sql_part()))
7045 DBUG_RETURN(error_num);
7046
7047/*
7048 SQL access -> SQL remote access
7049 !spider->do_direct_update &&
7050 (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
7051
7052 SQL access -> SQL remote access with dirct_update
7053 spider->do_direct_update &&
7054 spider->direct_update_kinds == SPIDER_SQL_KIND_SQL &&
7055 spider->direct_update_fields
7056*/
7057
7058#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
7059 if (!spider->do_direct_update)
7060 {
7061#endif
7062 if (
7063 (spider->sql_kinds & SPIDER_SQL_KIND_SQL) &&
7064 (error_num = spider->append_update_set_sql_part())
7065 ) {
7066 DBUG_RETURN(error_num);
7067 }
7068#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
7069 } else {
7070 if (
7071 (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL) &&
7072 (error_num = spider->append_direct_update_set_sql_part())
7073 ) {
7074 DBUG_RETURN(error_num);
7075 }
7076 }
7077#endif
7078
7079 result_list->desc_flg = FALSE;
7080 result_list->sorted = TRUE;
7081 if (spider->active_index == MAX_KEY)
7082 result_list->key_info = NULL;
7083 else
7084 result_list->key_info = &table->key_info[spider->active_index];
7085 spider_get_select_limit(spider, &select_lex, &select_limit, &offset_limit);
7086 result_list->limit_num =
7087 result_list->internal_limit >= select_limit ?
7088 select_limit : result_list->internal_limit;
7089 result_list->internal_offset += offset_limit;
7090 if (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL)
7091 {
7092 if (
7093 (error_num = spider->append_key_where_sql_part(
7094 NULL,
7095 NULL,
7096 SPIDER_SQL_TYPE_UPDATE_SQL)) ||
7097 (error_num = spider->
7098 append_key_order_for_direct_order_limit_with_alias_sql_part(
7099 NULL, 0, SPIDER_SQL_TYPE_UPDATE_SQL)) ||
7100 (error_num = spider->append_limit_sql_part(
7101 result_list->internal_offset, result_list->limit_num,
7102 SPIDER_SQL_TYPE_UPDATE_SQL))
7103 ) {
7104 DBUG_RETURN(error_num);
7105 }
7106 }
7107
7108 for (
7109 roop_count = spider_conn_link_idx_next(share->link_statuses,
7110 spider->conn_link_idx, -1, share->link_count,
7111 SPIDER_LINK_STATUS_RECOVERY);
7112 roop_count < (int) share->link_count;
7113 roop_count = spider_conn_link_idx_next(share->link_statuses,
7114 spider->conn_link_idx, roop_count, share->link_count,
7115 SPIDER_LINK_STATUS_RECOVERY)
7116 ) {
7117 ulong sql_type;
7118 DBUG_PRINT("info", ("spider exec sql"));
7119 conn = spider->conns[roop_count];
7120 sql_type = SPIDER_SQL_TYPE_UPDATE_SQL;
7121 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
7122 if (dbton_hdl->need_lock_before_set_sql_for_exec(sql_type))
7123 {
7124 pthread_mutex_lock(&conn->mta_conn_mutex);
7125 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7126 }
7127 if ((error_num = dbton_hdl->set_sql_for_exec(sql_type, roop_count)))
7128 {
7129 DBUG_RETURN(error_num);
7130 }
7131 if (!dbton_hdl->need_lock_before_set_sql_for_exec(sql_type))
7132 {
7133 pthread_mutex_lock(&conn->mta_conn_mutex);
7134 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7135 }
7136#ifdef HA_CAN_BULK_ACCESS
7137 if (spider->is_bulk_access_clone)
7138 {
7139 spider->connection_ids[roop_count] = conn->connection_id;
7140 spider_trx_add_bulk_access_conn(spider->trx, conn);
7141 } else {
7142#endif
7143 conn->need_mon = &spider->need_mons[roop_count];
7144 conn->mta_conn_mutex_lock_already = TRUE;
7145 conn->mta_conn_mutex_unlock_later = TRUE;
7146 if ((error_num = spider_db_set_names(spider, conn, roop_count)))
7147 {
7148 conn->mta_conn_mutex_lock_already = FALSE;
7149 conn->mta_conn_mutex_unlock_later = FALSE;
7150 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7151 pthread_mutex_unlock(&conn->mta_conn_mutex);
7152 if (
7153 share->monitoring_kind[roop_count] &&
7154 spider->need_mons[roop_count]
7155 ) {
7156 error_num = spider_ping_table_mon_from_table(
7157 spider->trx,
7158 spider->trx->thd,
7159 share,
7160 roop_count,
7161 (uint32) share->monitoring_sid[roop_count],
7162 share->table_name,
7163 share->table_name_length,
7164 spider->conn_link_idx[roop_count],
7165 NULL,
7166 0,
7167 share->monitoring_kind[roop_count],
7168 share->monitoring_limit[roop_count],
7169 share->monitoring_flag[roop_count],
7170 TRUE
7171 );
7172 }
7173 DBUG_RETURN(error_num);
7174 }
7175 spider_conn_set_timeout_from_share(conn, roop_count, spider->trx->thd,
7176 share);
7177 if (
7178 (error_num = dbton_hdl->execute_sql(
7179 sql_type,
7180 conn,
7181 -1,
7182 &spider->need_mons[roop_count])
7183 ) &&
7184 (error_num != HA_ERR_FOUND_DUPP_KEY || !spider->ignore_dup_key)
7185 ) {
7186 conn->mta_conn_mutex_lock_already = FALSE;
7187 conn->mta_conn_mutex_unlock_later = FALSE;
7188 error_num = spider_db_errorno(conn);
7189 if (
7190 error_num != ER_DUP_ENTRY &&
7191 error_num != ER_DUP_KEY &&
7192 error_num != HA_ERR_FOUND_DUPP_KEY &&
7193 share->monitoring_kind[roop_count] &&
7194 spider->need_mons[roop_count]
7195 ) {
7196 error_num = spider_ping_table_mon_from_table(
7197 spider->trx,
7198 spider->trx->thd,
7199 share,
7200 roop_count,
7201 (uint32) share->monitoring_sid[roop_count],
7202 share->table_name,
7203 share->table_name_length,
7204 spider->conn_link_idx[roop_count],
7205 NULL,
7206 0,
7207 share->monitoring_kind[roop_count],
7208 share->monitoring_limit[roop_count],
7209 share->monitoring_flag[roop_count],
7210 TRUE
7211 );
7212 }
7213 DBUG_RETURN(error_num);
7214 }
7215 if (!counted)
7216 {
7217 *update_rows = spider->conns[roop_count]->db_conn->affected_rows();
7218 DBUG_PRINT("info", ("spider update_rows = %llu", *update_rows));
7219 counted = TRUE;
7220 }
7221#ifdef HA_CAN_BULK_ACCESS
7222 }
7223#endif
7224 conn->mta_conn_mutex_lock_already = FALSE;
7225 conn->mta_conn_mutex_unlock_later = FALSE;
7226 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7227 pthread_mutex_unlock(&conn->mta_conn_mutex);
7228 }
7229 spider->reset_sql_sql(SPIDER_SQL_TYPE_UPDATE_SQL);
7230 DBUG_RETURN(0);
7231}
7232#endif
7233#endif
7234
7235#ifdef HA_CAN_BULK_ACCESS
7236int spider_db_bulk_direct_update(
7237 ha_spider *spider,
7238 ha_rows *update_rows
7239) {
7240 int error_num = 0, roop_count, tmp_error_num;
7241 SPIDER_SHARE *share = spider->share;
7242 SPIDER_CONN *conn;
7243 bool counted = FALSE;
7244 DBUG_ENTER("spider_db_bulk_direct_update");
7245 for (
7246 roop_count = spider_conn_link_idx_next(share->link_statuses,
7247 spider->conn_link_idx, -1, share->link_count,
7248 SPIDER_LINK_STATUS_RECOVERY);
7249 roop_count < (int) share->link_count;
7250 roop_count = spider_conn_link_idx_next(share->link_statuses,
7251 spider->conn_link_idx, roop_count, share->link_count,
7252 SPIDER_LINK_STATUS_RECOVERY)
7253 ) {
7254#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7255 if (!spider_bit_is_set(spider->do_hs_direct_update, roop_count))
7256 {
7257#endif
7258 DBUG_PRINT("info", ("spider exec sql"));
7259 conn = spider->conns[roop_count];
7260#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7261 } else {
7262 DBUG_PRINT("info", ("spider exec hs"));
7263 conn = spider->hs_w_conns[roop_count];
7264 }
7265#endif
7266 pthread_mutex_lock(&conn->mta_conn_mutex);
7267 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7268 conn->mta_conn_mutex_lock_already = TRUE;
7269 conn->mta_conn_mutex_unlock_later = TRUE;
7270 if ((tmp_error_num = spider_db_bulk_open_handler(spider, conn,
7271 roop_count)))
7272 {
7273 error_num = tmp_error_num;
7274 }
7275#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7276 if (!spider_bit_is_set(spider->do_hs_direct_update, roop_count))
7277 {
7278#endif
7279 if (!counted)
7280 {
7281 *update_rows = spider->conns[roop_count]->db_conn->affected_rows();
7282 DBUG_PRINT("info", ("spider update_rows = %llu", *update_rows));
7283 counted = TRUE;
7284 }
7285#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7286 } else {
7287 SPIDER_DB_RESULT *result;
7288 if (spider_bit_is_set(spider->db_request_phase, roop_count))
7289 {
7290 spider_clear_bit(spider->db_request_phase, roop_count);
7291 }
7292 st_spider_db_request_key request_key;
7293 request_key.spider_thread_id = spider->trx->spider_thread_id;
7294 request_key.query_id = spider->trx->thd->query_id;
7295 request_key.handler = spider;
7296 request_key.request_id = spider->db_request_id[roop_count];
7297 request_key.next = NULL;
7298 if ((result = conn->db_conn->use_result(&request_key, &error_num)))
7299 {
7300 if (!counted)
7301 {
7302 *update_rows = conn->db_conn->affected_rows();
7303 DBUG_PRINT("info", ("spider update_rows = %llu", *update_rows));
7304 counted = TRUE;
7305 }
7306 result->free_result();
7307 delete result;
7308 } else {
7309 if (!error_num)
7310 {
7311 error_num = spider_db_errorno(conn);
7312 }
7313 conn->mta_conn_mutex_lock_already = FALSE;
7314 conn->mta_conn_mutex_unlock_later = FALSE;
7315 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7316 pthread_mutex_unlock(&conn->mta_conn_mutex);
7317 DBUG_RETURN(error_num);
7318 }
7319 }
7320#endif
7321 conn->mta_conn_mutex_lock_already = FALSE;
7322 conn->mta_conn_mutex_unlock_later = FALSE;
7323 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7324 pthread_mutex_unlock(&conn->mta_conn_mutex);
7325 }
7326 DBUG_RETURN(error_num);
7327}
7328#endif
7329
7330int spider_db_bulk_delete(
7331 ha_spider *spider,
7332 TABLE *table,
7333 my_ptrdiff_t ptr_diff
7334) {
7335 int error_num;
7336 DBUG_ENTER("spider_db_bulk_delete");
7337
7338 if ((error_num = spider->append_delete_sql(table, ptr_diff, TRUE)))
7339 DBUG_RETURN(error_num);
7340
7341 if (
7342 spider->sql_is_filled_up(SPIDER_SQL_TYPE_BULK_UPDATE_SQL) &&
7343 (error_num = spider_db_bulk_update_size_limit(spider, table))
7344 )
7345 DBUG_RETURN(error_num);
7346 DBUG_RETURN(0);
7347}
7348
7349int spider_db_delete(
7350 ha_spider *spider,
7351 TABLE *table,
7352 const uchar *buf
7353) {
7354 int error_num, roop_count;
7355 SPIDER_SHARE *share = spider->share;
7356 SPIDER_CONN *conn;
7357 SPIDER_RESULT_LIST *result_list = &spider->result_list;
7358 my_ptrdiff_t ptr_diff = PTR_BYTE_DIFF(buf, table->record[0]);
7359 DBUG_ENTER("spider_db_delete");
7360 if (result_list->bulk_update_mode)
7361 DBUG_RETURN(spider_db_bulk_delete(spider, table, ptr_diff));
7362
7363 if ((error_num = spider->append_delete_sql(table, ptr_diff, FALSE)))
7364 DBUG_RETURN(error_num);
7365
7366 for (
7367 roop_count = spider_conn_link_idx_next(share->link_statuses,
7368 spider->conn_link_idx, -1, share->link_count,
7369 SPIDER_LINK_STATUS_RECOVERY);
7370 roop_count < (int) share->link_count;
7371 roop_count = spider_conn_link_idx_next(share->link_statuses,
7372 spider->conn_link_idx, roop_count, share->link_count,
7373 SPIDER_LINK_STATUS_RECOVERY)
7374 ) {
7375 conn = spider->conns[roop_count];
7376 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
7377 if (dbton_hdl->need_lock_before_set_sql_for_exec(
7378 SPIDER_SQL_TYPE_DELETE_SQL))
7379 {
7380 pthread_mutex_lock(&conn->mta_conn_mutex);
7381 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7382 }
7383 if ((error_num = dbton_hdl->set_sql_for_exec(
7384 SPIDER_SQL_TYPE_DELETE_SQL, roop_count)))
7385 {
7386 DBUG_RETURN(error_num);
7387 }
7388 if (!dbton_hdl->need_lock_before_set_sql_for_exec(
7389 SPIDER_SQL_TYPE_DELETE_SQL))
7390 {
7391 pthread_mutex_lock(&conn->mta_conn_mutex);
7392 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7393 }
7394 if ((error_num = spider_db_query_with_set_names(
7395 SPIDER_SQL_TYPE_DELETE_SQL, spider, conn, roop_count)))
7396 DBUG_RETURN(error_num);
7397 result_list->update_sqls[roop_count].length(0);
7398 }
7399 if ((error_num = spider->reset_sql_sql(SPIDER_SQL_TYPE_DELETE_SQL)))
7400 {
7401 DBUG_RETURN(error_num);
7402 }
7403 DBUG_RETURN(0);
7404}
7405
7406#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
7407#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS_WITH_HS
7408int spider_db_direct_delete(
7409 ha_spider *spider,
7410 TABLE *table,
7411 KEY_MULTI_RANGE *ranges,
7412 uint range_count,
7413 ha_rows *delete_rows
7414) {
7415 int error_num, roop_count;
7416 SPIDER_SHARE *share = spider->share;
7417 SPIDER_CONN *conn;
7418 SPIDER_RESULT_LIST *result_list = &spider->result_list;
7419 bool counted = FALSE;
7420 st_select_lex *select_lex;
7421 longlong select_limit;
7422 longlong offset_limit;
7423 DBUG_ENTER("spider_db_direct_delete");
7424
7425 spider_set_result_list_param(spider);
7426 result_list->finish_flg = FALSE;
7427 result_list->desc_flg = FALSE;
7428 result_list->sorted = TRUE;
7429 if (spider->active_index == MAX_KEY)
7430 result_list->key_info = NULL;
7431 else
7432 result_list->key_info = &table->key_info[spider->active_index];
7433 spider_get_select_limit(spider, &select_lex, &select_limit, &offset_limit);
7434 result_list->limit_num =
7435 result_list->internal_limit >= select_limit ?
7436 select_limit : result_list->internal_limit;
7437 result_list->internal_offset += offset_limit;
7438/*
7439 result_list->limit_num =
7440 result_list->internal_limit >= result_list->split_read ?
7441 result_list->split_read : result_list->internal_limit;
7442*/
7443 if (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL)
7444 {
7445 if (
7446 (error_num = spider->append_delete_sql_part()) ||
7447 (error_num = spider->append_from_sql_part(SPIDER_SQL_TYPE_DELETE_SQL))
7448 ) {
7449 DBUG_RETURN(error_num);
7450 }
7451 spider->set_where_pos_sql(SPIDER_SQL_TYPE_DELETE_SQL);
7452 if (
7453 (error_num = spider->append_key_where_sql_part(
7454 (ranges && ranges->start_key.key) ? &ranges->start_key : NULL,
7455 (ranges && ranges->end_key.key) ? &ranges->end_key : NULL,
7456 SPIDER_SQL_TYPE_DELETE_SQL)) ||
7457 (error_num = spider->
7458 append_key_order_for_direct_order_limit_with_alias_sql_part(
7459 NULL, 0, SPIDER_SQL_TYPE_DELETE_SQL)) ||
7460 (error_num = spider->append_limit_sql_part(
7461 result_list->internal_offset, result_list->limit_num,
7462 SPIDER_SQL_TYPE_DELETE_SQL))
7463 ) {
7464 DBUG_RETURN(error_num);
7465 }
7466 }
7467#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7468 if (spider->direct_update_kinds & SPIDER_SQL_KIND_HS)
7469 {
7470 if (
7471 (error_num = spider->append_key_where_hs_part(
7472 (ranges && ranges->start_key.key) ? &ranges->start_key : NULL,
7473 (ranges && ranges->end_key.key) ? &ranges->end_key : NULL,
7474 SPIDER_SQL_TYPE_DELETE_HS)) ||
7475 (error_num = spider->append_limit_hs_part(
7476 result_list->internal_offset, result_list->limit_num,
7477 SPIDER_SQL_TYPE_DELETE_HS))
7478 ) {
7479 DBUG_RETURN(error_num);
7480 }
7481 }
7482#endif
7483
7484 for (
7485 roop_count = spider_conn_link_idx_next(share->link_statuses,
7486 spider->conn_link_idx, -1, share->link_count,
7487 SPIDER_LINK_STATUS_RECOVERY);
7488 roop_count < (int) share->link_count;
7489 roop_count = spider_conn_link_idx_next(share->link_statuses,
7490 spider->conn_link_idx, roop_count, share->link_count,
7491 SPIDER_LINK_STATUS_RECOVERY)
7492 ) {
7493 ulong sql_type;
7494#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7495 if (!spider_bit_is_set(spider->do_hs_direct_update, roop_count))
7496 {
7497#endif
7498 DBUG_PRINT("info", ("spider exec sql"));
7499 conn = spider->conns[roop_count];
7500 sql_type = SPIDER_SQL_TYPE_DELETE_SQL;
7501#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7502 } else {
7503 DBUG_PRINT("info", ("spider exec hs"));
7504 conn = spider->hs_w_conns[roop_count];
7505 sql_type = SPIDER_SQL_TYPE_DELETE_HS;
7506 }
7507#endif
7508 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
7509 if (dbton_hdl->need_lock_before_set_sql_for_exec(sql_type))
7510 {
7511 pthread_mutex_lock(&conn->mta_conn_mutex);
7512 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7513 }
7514 if ((error_num = dbton_hdl->set_sql_for_exec(sql_type, roop_count)))
7515 {
7516 DBUG_RETURN(error_num);
7517 }
7518 if (!dbton_hdl->need_lock_before_set_sql_for_exec(sql_type))
7519 {
7520 pthread_mutex_lock(&conn->mta_conn_mutex);
7521 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7522 }
7523#ifdef HA_CAN_BULK_ACCESS
7524 if (spider->is_bulk_access_clone)
7525 {
7526 spider->connection_ids[roop_count] = conn->connection_id;
7527 spider_trx_add_bulk_access_conn(spider->trx, conn);
7528 } else {
7529#endif
7530 conn->need_mon = &spider->need_mons[roop_count];
7531 conn->mta_conn_mutex_lock_already = TRUE;
7532 conn->mta_conn_mutex_unlock_later = TRUE;
7533 if ((error_num = spider_db_set_names(spider, conn, roop_count)))
7534 {
7535 conn->mta_conn_mutex_lock_already = FALSE;
7536 conn->mta_conn_mutex_unlock_later = FALSE;
7537 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7538 pthread_mutex_unlock(&conn->mta_conn_mutex);
7539 if (
7540 share->monitoring_kind[roop_count] &&
7541 spider->need_mons[roop_count]
7542 ) {
7543 error_num = spider_ping_table_mon_from_table(
7544 spider->trx,
7545 spider->trx->thd,
7546 share,
7547 roop_count,
7548 (uint32) share->monitoring_sid[roop_count],
7549 share->table_name,
7550 share->table_name_length,
7551 spider->conn_link_idx[roop_count],
7552 NULL,
7553 0,
7554 share->monitoring_kind[roop_count],
7555 share->monitoring_limit[roop_count],
7556 share->monitoring_flag[roop_count],
7557 TRUE
7558 );
7559 }
7560 DBUG_RETURN(error_num);
7561 }
7562 spider_conn_set_timeout_from_share(conn, roop_count, spider->trx->thd,
7563 share);
7564 if (dbton_hdl->execute_sql(
7565 sql_type,
7566 conn,
7567 -1,
7568 &spider->need_mons[roop_count])
7569 ) {
7570 conn->mta_conn_mutex_lock_already = FALSE;
7571 conn->mta_conn_mutex_unlock_later = FALSE;
7572 error_num = spider_db_errorno(conn);
7573 if (
7574 share->monitoring_kind[roop_count] &&
7575 spider->need_mons[roop_count]
7576 ) {
7577 error_num = spider_ping_table_mon_from_table(
7578 spider->trx,
7579 spider->trx->thd,
7580 share,
7581 roop_count,
7582 (uint32) share->monitoring_sid[roop_count],
7583 share->table_name,
7584 share->table_name_length,
7585 spider->conn_link_idx[roop_count],
7586 NULL,
7587 0,
7588 share->monitoring_kind[roop_count],
7589 share->monitoring_limit[roop_count],
7590 share->monitoring_flag[roop_count],
7591 TRUE
7592 );
7593 }
7594 DBUG_RETURN(error_num);
7595 }
7596 conn->mta_conn_mutex_lock_already = FALSE;
7597 conn->mta_conn_mutex_unlock_later = FALSE;
7598#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7599 if (!spider_bit_is_set(spider->do_hs_direct_update, roop_count))
7600 {
7601#endif
7602 if (!counted)
7603 {
7604 *delete_rows = spider->conns[roop_count]->db_conn->affected_rows();
7605 DBUG_PRINT("info", ("spider delete_rows = %llu", *delete_rows));
7606 counted = TRUE;
7607 }
7608#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7609 } else {
7610 SPIDER_DB_RESULT *result;
7611 if (spider_bit_is_set(spider->db_request_phase, roop_count))
7612 {
7613 spider_clear_bit(spider->db_request_phase, roop_count);
7614 }
7615 st_spider_db_request_key request_key;
7616 request_key.spider_thread_id = spider->trx->spider_thread_id;
7617 request_key.query_id = spider->trx->thd->query_id;
7618 request_key.handler = spider;
7619 request_key.request_id = spider->db_request_id[roop_count];
7620 request_key.next = NULL;
7621 if ((result = conn->db_conn->use_result(&request_key, &error_num)))
7622 {
7623 if (!counted)
7624 {
7625 *delete_rows = conn->db_conn->affected_rows();
7626 DBUG_PRINT("info", ("spider delete_rows = %llu", *delete_rows));
7627 counted = TRUE;
7628 }
7629 result->free_result();
7630 delete result;
7631 } else {
7632 if (!error_num)
7633 {
7634 error_num = spider_db_errorno(conn);
7635 }
7636 DBUG_RETURN(error_num);
7637 }
7638 }
7639#endif
7640#ifdef HA_CAN_BULK_ACCESS
7641 }
7642#endif
7643 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7644 pthread_mutex_unlock(&conn->mta_conn_mutex);
7645 }
7646 int error_num2 = 0;
7647 if (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL)
7648 {
7649 if ((error_num = spider->reset_sql_sql(SPIDER_SQL_TYPE_DELETE_SQL)))
7650 error_num2 = error_num;
7651 }
7652#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7653 if (spider->direct_update_kinds & SPIDER_SQL_KIND_HS)
7654 {
7655 if ((error_num = spider->reset_hs_sql(SPIDER_SQL_TYPE_DELETE_HS)))
7656 error_num2 = error_num;
7657 if ((error_num = spider->reset_hs_keys(SPIDER_SQL_TYPE_DELETE_HS)))
7658 error_num2 = error_num;
7659 }
7660#endif
7661 DBUG_RETURN(error_num2);
7662}
7663#else
7664int spider_db_direct_delete(
7665 ha_spider *spider,
7666 TABLE *table,
7667 ha_rows *delete_rows
7668) {
7669 int error_num, roop_count;
7670 SPIDER_SHARE *share = spider->share;
7671 SPIDER_CONN *conn;
7672 SPIDER_RESULT_LIST *result_list = &spider->result_list;
7673 bool counted = FALSE;
7674 st_select_lex *select_lex;
7675 longlong select_limit;
7676 longlong offset_limit;
7677 DBUG_ENTER("spider_db_direct_delete");
7678
7679 spider_set_result_list_param(spider);
7680 result_list->finish_flg = FALSE;
7681 result_list->desc_flg = FALSE;
7682 result_list->sorted = TRUE;
7683 if (spider->active_index == MAX_KEY)
7684 result_list->key_info = NULL;
7685 else
7686 result_list->key_info = &table->key_info[spider->active_index];
7687 spider_get_select_limit(spider, &select_lex, &select_limit, &offset_limit);
7688 result_list->limit_num =
7689 result_list->internal_limit >= select_limit ?
7690 select_limit : result_list->internal_limit;
7691 result_list->internal_offset += offset_limit;
7692 if (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL)
7693 {
7694 if (
7695 (error_num = spider->append_delete_sql_part()) ||
7696 (error_num = spider->append_from_sql_part(SPIDER_SQL_TYPE_DELETE_SQL))
7697 ) {
7698 DBUG_RETURN(error_num);
7699 }
7700 spider->set_where_pos_sql(SPIDER_SQL_TYPE_DELETE_SQL);
7701 if (
7702 (error_num = spider->append_key_where_sql_part(
7703 NULL,
7704 NULL,
7705 SPIDER_SQL_TYPE_DELETE_SQL)) ||
7706 (error_num = spider->
7707 append_key_order_for_direct_order_limit_with_alias_sql_part(
7708 NULL, 0, SPIDER_SQL_TYPE_DELETE_SQL)) ||
7709 (error_num = spider->append_limit_sql_part(
7710 result_list->internal_offset, result_list->limit_num,
7711 SPIDER_SQL_TYPE_DELETE_SQL))
7712 ) {
7713 DBUG_RETURN(error_num);
7714 }
7715 }
7716
7717 for (
7718 roop_count = spider_conn_link_idx_next(share->link_statuses,
7719 spider->conn_link_idx, -1, share->link_count,
7720 SPIDER_LINK_STATUS_RECOVERY);
7721 roop_count < (int) share->link_count;
7722 roop_count = spider_conn_link_idx_next(share->link_statuses,
7723 spider->conn_link_idx, roop_count, share->link_count,
7724 SPIDER_LINK_STATUS_RECOVERY)
7725 ) {
7726 ulong sql_type;
7727 DBUG_PRINT("info", ("spider exec sql"));
7728 conn = spider->conns[roop_count];
7729 sql_type = SPIDER_SQL_TYPE_DELETE_SQL;
7730 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
7731 if (dbton_hdl->need_lock_before_set_sql_for_exec(sql_type))
7732 {
7733 pthread_mutex_lock(&conn->mta_conn_mutex);
7734 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7735 }
7736 if ((error_num = dbton_hdl->set_sql_for_exec(sql_type, roop_count)))
7737 {
7738 DBUG_RETURN(error_num);
7739 }
7740 if (!dbton_hdl->need_lock_before_set_sql_for_exec(sql_type))
7741 {
7742 pthread_mutex_lock(&conn->mta_conn_mutex);
7743 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7744 }
7745#ifdef HA_CAN_BULK_ACCESS
7746 if (spider->is_bulk_access_clone)
7747 {
7748 spider->connection_ids[roop_count] = conn->connection_id;
7749 spider_trx_add_bulk_access_conn(spider->trx, conn);
7750 } else {
7751#endif
7752 conn->need_mon = &spider->need_mons[roop_count];
7753 conn->mta_conn_mutex_lock_already = TRUE;
7754 conn->mta_conn_mutex_unlock_later = TRUE;
7755 if ((error_num = spider_db_set_names(spider, conn, roop_count)))
7756 {
7757 conn->mta_conn_mutex_lock_already = FALSE;
7758 conn->mta_conn_mutex_unlock_later = FALSE;
7759 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7760 pthread_mutex_unlock(&conn->mta_conn_mutex);
7761 if (
7762 share->monitoring_kind[roop_count] &&
7763 spider->need_mons[roop_count]
7764 ) {
7765 error_num = spider_ping_table_mon_from_table(
7766 spider->trx,
7767 spider->trx->thd,
7768 share,
7769 roop_count,
7770 (uint32) share->monitoring_sid[roop_count],
7771 share->table_name,
7772 share->table_name_length,
7773 spider->conn_link_idx[roop_count],
7774 NULL,
7775 0,
7776 share->monitoring_kind[roop_count],
7777 share->monitoring_limit[roop_count],
7778 share->monitoring_flag[roop_count],
7779 TRUE
7780 );
7781 }
7782 DBUG_RETURN(error_num);
7783 }
7784 spider_conn_set_timeout_from_share(conn, roop_count, spider->trx->thd,
7785 share);
7786 if (dbton_hdl->execute_sql(
7787 sql_type,
7788 conn,
7789 -1,
7790 &spider->need_mons[roop_count])
7791 ) {
7792 conn->mta_conn_mutex_lock_already = FALSE;
7793 conn->mta_conn_mutex_unlock_later = FALSE;
7794 error_num = spider_db_errorno(conn);
7795 if (
7796 share->monitoring_kind[roop_count] &&
7797 spider->need_mons[roop_count]
7798 ) {
7799 error_num = spider_ping_table_mon_from_table(
7800 spider->trx,
7801 spider->trx->thd,
7802 share,
7803 roop_count,
7804 (uint32) share->monitoring_sid[roop_count],
7805 share->table_name,
7806 share->table_name_length,
7807 spider->conn_link_idx[roop_count],
7808 NULL,
7809 0,
7810 share->monitoring_kind[roop_count],
7811 share->monitoring_limit[roop_count],
7812 share->monitoring_flag[roop_count],
7813 TRUE
7814 );
7815 }
7816 DBUG_RETURN(error_num);
7817 }
7818 conn->mta_conn_mutex_lock_already = FALSE;
7819 conn->mta_conn_mutex_unlock_later = FALSE;
7820 if (!counted)
7821 {
7822 *delete_rows = spider->conns[roop_count]->db_conn->affected_rows();
7823 DBUG_PRINT("info", ("spider delete_rows = %llu", *delete_rows));
7824 counted = TRUE;
7825 }
7826#ifdef HA_CAN_BULK_ACCESS
7827 }
7828#endif
7829 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7830 pthread_mutex_unlock(&conn->mta_conn_mutex);
7831 }
7832 int error_num2 = 0;
7833 if (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL)
7834 {
7835 if ((error_num = spider->reset_sql_sql(SPIDER_SQL_TYPE_DELETE_SQL)))
7836 error_num2 = error_num;
7837 }
7838 DBUG_RETURN(error_num2);
7839}
7840#endif
7841#endif
7842
7843int spider_db_delete_all_rows(
7844 ha_spider *spider
7845) {
7846 int error_num, roop_count;
7847 SPIDER_SHARE *share = spider->share;
7848 SPIDER_CONN *conn;
7849 DBUG_ENTER("spider_db_delete_all_rows");
7850 if ((error_num = spider->append_delete_all_rows_sql_part(
7851 SPIDER_SQL_TYPE_DELETE_SQL)))
7852 DBUG_RETURN(error_num);
7853
7854 for (
7855 roop_count = spider_conn_link_idx_next(share->link_statuses,
7856 spider->conn_link_idx, -1, share->link_count,
7857 SPIDER_LINK_STATUS_RECOVERY);
7858 roop_count < (int) share->link_count;
7859 roop_count = spider_conn_link_idx_next(share->link_statuses,
7860 spider->conn_link_idx, roop_count, share->link_count,
7861 SPIDER_LINK_STATUS_RECOVERY)
7862 ) {
7863 uint dbton_id = share->use_sql_dbton_ids[roop_count];
7864 spider_db_handler *dbton_hdl = spider->dbton_handler[dbton_id];
7865 conn = spider->conns[roop_count];
7866 if (dbton_hdl->need_lock_before_set_sql_for_exec(
7867 SPIDER_SQL_TYPE_DELETE_SQL))
7868 {
7869 pthread_mutex_lock(&conn->mta_conn_mutex);
7870 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7871 }
7872 if ((error_num = dbton_hdl->set_sql_for_exec(
7873 SPIDER_SQL_TYPE_DELETE_SQL, roop_count)))
7874 {
7875 DBUG_RETURN(error_num);
7876 }
7877 if (!dbton_hdl->need_lock_before_set_sql_for_exec(
7878 SPIDER_SQL_TYPE_DELETE_SQL))
7879 {
7880 pthread_mutex_lock(&conn->mta_conn_mutex);
7881 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
7882 }
7883 conn->need_mon = &spider->need_mons[roop_count];
7884 conn->mta_conn_mutex_lock_already = TRUE;
7885 conn->mta_conn_mutex_unlock_later = TRUE;
7886 spider_conn_set_timeout_from_share(conn, roop_count, spider->trx->thd,
7887 share);
7888 if (
7889 (error_num = spider_db_set_names(spider, conn, roop_count)) ||
7890 (
7891 dbton_hdl->execute_sql(
7892 SPIDER_SQL_TYPE_DELETE_SQL,
7893 conn,
7894 -1,
7895 &spider->need_mons[roop_count]) &&
7896 (error_num = spider_db_errorno(conn))
7897 )
7898 ) {
7899 if (
7900 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
7901 !conn->disable_reconnect
7902 ) {
7903 /* retry */
7904 if ((error_num = spider_db_ping(spider, conn, roop_count)))
7905 {
7906 conn->mta_conn_mutex_lock_already = FALSE;
7907 conn->mta_conn_mutex_unlock_later = FALSE;
7908 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7909 pthread_mutex_unlock(&conn->mta_conn_mutex);
7910 if (
7911 share->monitoring_kind[roop_count] &&
7912 spider->need_mons[roop_count]
7913 ) {
7914 error_num = spider_ping_table_mon_from_table(
7915 spider->trx,
7916 spider->trx->thd,
7917 share,
7918 roop_count,
7919 (uint32) share->monitoring_sid[roop_count],
7920 share->table_name,
7921 share->table_name_length,
7922 spider->conn_link_idx[roop_count],
7923 NULL,
7924 0,
7925 share->monitoring_kind[roop_count],
7926 share->monitoring_limit[roop_count],
7927 share->monitoring_flag[roop_count],
7928 TRUE
7929 );
7930 }
7931 DBUG_RETURN(error_num);
7932 }
7933 if ((error_num = spider_db_set_names(spider, conn, roop_count)))
7934 {
7935 conn->mta_conn_mutex_lock_already = FALSE;
7936 conn->mta_conn_mutex_unlock_later = FALSE;
7937 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
7938 pthread_mutex_unlock(&conn->mta_conn_mutex);
7939 if (
7940 share->monitoring_kind[roop_count] &&
7941 spider->need_mons[roop_count]
7942 ) {
7943 error_num = spider_ping_table_mon_from_table(
7944 spider->trx,
7945 spider->trx->thd,
7946 share,
7947 roop_count,
7948 (uint32) share->monitoring_sid[roop_count],
7949 share->table_name,
7950 share->table_name_length,
7951 spider->conn_link_idx[roop_count],
7952 NULL,
7953 0,
7954 share->monitoring_kind[roop_count],
7955 share->monitoring_limit[roop_count],
7956 share->monitoring_flag[roop_count],
7957 TRUE
7958 );
7959 }
7960 DBUG_RETURN(error_num);
7961 }
7962 spider_conn_set_timeout_from_share(conn, roop_count, spider->trx->thd,
7963 share);
7964 if (dbton_hdl->execute_sql(
7965 SPIDER_SQL_TYPE_DELETE_SQL,
7966 conn,
7967 -1,
7968 &spider->need_mons[roop_count])
7969 ) {
7970 conn->mta_conn_mutex_lock_already = FALSE;
7971 conn->mta_conn_mutex_unlock_later = FALSE;
7972 error_num = spider_db_errorno(conn);
7973 if (
7974 share->monitoring_kind[roop_count] &&
7975 spider->need_mons[roop_count]
7976 ) {
7977 error_num = spider_ping_table_mon_from_table(
7978 spider->trx,
7979 spider->trx->thd,
7980 share,
7981 roop_count,
7982 (uint32) share->monitoring_sid[roop_count],
7983 share->table_name,
7984 share->table_name_length,
7985 spider->conn_link_idx[roop_count],
7986 NULL,
7987 0,
7988 share->monitoring_kind[roop_count],
7989 share->monitoring_limit[roop_count],
7990 share->monitoring_flag[roop_count],
7991 TRUE
7992 );
7993 }
7994 DBUG_RETURN(error_num);
7995 }
7996 } else {
7997 conn->mta_conn_mutex_lock_already = FALSE;
7998 conn->mta_conn_mutex_unlock_later = FALSE;
7999 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
8000 pthread_mutex_unlock(&conn->mta_conn_mutex);
8001 if (
8002 share->monitoring_kind[roop_count] &&
8003 spider->need_mons[roop_count]
8004 ) {
8005 error_num = spider_ping_table_mon_from_table(
8006 spider->trx,
8007 spider->trx->thd,
8008 share,
8009 roop_count,
8010 (uint32) share->monitoring_sid[roop_count],
8011 share->table_name,
8012 share->table_name_length,
8013 spider->conn_link_idx[roop_count],
8014 NULL,
8015 0,
8016 share->monitoring_kind[roop_count],
8017 share->monitoring_limit[roop_count],
8018 share->monitoring_flag[roop_count],
8019 TRUE
8020 );
8021 }
8022 DBUG_RETURN(error_num);
8023 }
8024 }
8025 conn->mta_conn_mutex_lock_already = FALSE;
8026 conn->mta_conn_mutex_unlock_later = FALSE;
8027 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
8028 pthread_mutex_unlock(&conn->mta_conn_mutex);
8029 }
8030 if ((error_num = spider->reset_sql_sql(SPIDER_SQL_TYPE_DELETE_SQL)))
8031 DBUG_RETURN(error_num);
8032 DBUG_RETURN(0);
8033}
8034
8035int spider_db_disable_keys(
8036 ha_spider *spider
8037) {
8038 int error_num, roop_count;
8039 SPIDER_SHARE *share = spider->share;
8040 SPIDER_CONN *conn;
8041 spider_db_handler *dbton_hdl;
8042 DBUG_ENTER("spider_db_disable_keys");
8043 if (
8044 spider_param_internal_optimize(spider->trx->thd,
8045 share->internal_optimize) == 1
8046 ) {
8047 for (
8048 roop_count = spider_conn_link_idx_next(share->link_statuses,
8049 spider->conn_link_idx, -1, share->link_count,
8050 SPIDER_LINK_STATUS_RECOVERY);
8051 roop_count < (int) share->link_count;
8052 roop_count = spider_conn_link_idx_next(share->link_statuses,
8053 spider->conn_link_idx, roop_count, share->link_count,
8054 SPIDER_LINK_STATUS_RECOVERY)
8055 ) {
8056 conn = spider->conns[roop_count];
8057 dbton_hdl = spider->dbton_handler[conn->dbton_id];
8058 if ((error_num = dbton_hdl->disable_keys(conn, roop_count)))
8059 {
8060 if (
8061 share->monitoring_kind[roop_count] &&
8062 spider->need_mons[roop_count]
8063 ) {
8064 error_num = spider_ping_table_mon_from_table(
8065 spider->trx,
8066 spider->trx->thd,
8067 share,
8068 roop_count,
8069 (uint32) share->monitoring_sid[roop_count],
8070 share->table_name,
8071 share->table_name_length,
8072 spider->conn_link_idx[roop_count],
8073 NULL,
8074 0,
8075 share->monitoring_kind[roop_count],
8076 share->monitoring_limit[roop_count],
8077 share->monitoring_flag[roop_count],
8078 TRUE
8079 );
8080 }
8081 DBUG_RETURN(error_num);
8082 }
8083 }
8084 }
8085 DBUG_RETURN(0);
8086}
8087
8088int spider_db_enable_keys(
8089 ha_spider *spider
8090) {
8091 int error_num, roop_count;
8092 SPIDER_SHARE *share = spider->share;
8093 SPIDER_CONN *conn;
8094 spider_db_handler *dbton_hdl;
8095 DBUG_ENTER("spider_db_enable_keys");
8096 if (
8097 spider_param_internal_optimize(spider->trx->thd,
8098 share->internal_optimize) == 1
8099 ) {
8100 for (
8101 roop_count = spider_conn_link_idx_next(share->link_statuses,
8102 spider->conn_link_idx, -1, share->link_count,
8103 SPIDER_LINK_STATUS_RECOVERY);
8104 roop_count < (int) share->link_count;
8105 roop_count = spider_conn_link_idx_next(share->link_statuses,
8106 spider->conn_link_idx, roop_count, share->link_count,
8107 SPIDER_LINK_STATUS_RECOVERY)
8108 ) {
8109 conn = spider->conns[roop_count];
8110 dbton_hdl = spider->dbton_handler[conn->dbton_id];
8111 if ((error_num = dbton_hdl->enable_keys(conn, roop_count)))
8112 {
8113 if (
8114 share->monitoring_kind[roop_count] &&
8115 spider->need_mons[roop_count]
8116 ) {
8117 error_num = spider_ping_table_mon_from_table(
8118 spider->trx,
8119 spider->trx->thd,
8120 share,
8121 roop_count,
8122 (uint32) share->monitoring_sid[roop_count],
8123 share->table_name,
8124 share->table_name_length,
8125 spider->conn_link_idx[roop_count],
8126 NULL,
8127 0,
8128 share->monitoring_kind[roop_count],
8129 share->monitoring_limit[roop_count],
8130 share->monitoring_flag[roop_count],
8131 TRUE
8132 );
8133 }
8134 DBUG_RETURN(error_num);
8135 }
8136 }
8137 }
8138 DBUG_RETURN(0);
8139}
8140
8141int spider_db_check_table(
8142 ha_spider *spider,
8143 HA_CHECK_OPT* check_opt
8144) {
8145 int error_num, roop_count;
8146 SPIDER_SHARE *share = spider->share;
8147 SPIDER_CONN *conn;
8148 spider_db_handler *dbton_hdl;
8149 DBUG_ENTER("spider_db_check_table");
8150 if (
8151 spider_param_internal_optimize(spider->trx->thd,
8152 share->internal_optimize) == 1
8153 ) {
8154 for (
8155 roop_count = spider_conn_link_idx_next(share->link_statuses,
8156 spider->conn_link_idx, -1, share->link_count,
8157 SPIDER_LINK_STATUS_RECOVERY);
8158 roop_count < (int) share->link_count;
8159 roop_count = spider_conn_link_idx_next(share->link_statuses,
8160 spider->conn_link_idx, roop_count, share->link_count,
8161 SPIDER_LINK_STATUS_RECOVERY)
8162 ) {
8163 conn = spider->conns[roop_count];
8164 dbton_hdl = spider->dbton_handler[conn->dbton_id];
8165 if ((error_num = dbton_hdl->check_table(conn, roop_count, check_opt)))
8166 {
8167 if (
8168 share->monitoring_kind[roop_count] &&
8169 spider->need_mons[roop_count]
8170 ) {
8171 error_num = spider_ping_table_mon_from_table(
8172 spider->trx,
8173 spider->trx->thd,
8174 share,
8175 roop_count,
8176 (uint32) share->monitoring_sid[roop_count],
8177 share->table_name,
8178 share->table_name_length,
8179 spider->conn_link_idx[roop_count],
8180 NULL,
8181 0,
8182 share->monitoring_kind[roop_count],
8183 share->monitoring_limit[roop_count],
8184 share->monitoring_flag[roop_count],
8185 TRUE
8186 );
8187 }
8188 DBUG_RETURN(error_num);
8189 }
8190 }
8191 }
8192 DBUG_RETURN(0);
8193}
8194
8195int spider_db_repair_table(
8196 ha_spider *spider,
8197 HA_CHECK_OPT* check_opt
8198) {
8199 int error_num, roop_count;
8200 SPIDER_SHARE *share = spider->share;
8201 SPIDER_CONN *conn;
8202 spider_db_handler *dbton_hdl;
8203 DBUG_ENTER("spider_db_repair_table");
8204 if (
8205 spider_param_internal_optimize(spider->trx->thd,
8206 share->internal_optimize) == 1
8207 ) {
8208 for (
8209 roop_count = spider_conn_link_idx_next(share->link_statuses,
8210 spider->conn_link_idx, -1, share->link_count,
8211 SPIDER_LINK_STATUS_RECOVERY);
8212 roop_count < (int) share->link_count;
8213 roop_count = spider_conn_link_idx_next(share->link_statuses,
8214 spider->conn_link_idx, roop_count, share->link_count,
8215 SPIDER_LINK_STATUS_RECOVERY)
8216 ) {
8217 conn = spider->conns[roop_count];
8218 dbton_hdl = spider->dbton_handler[conn->dbton_id];
8219 if ((error_num = dbton_hdl->repair_table(conn, roop_count, check_opt)))
8220 {
8221 if (
8222 share->monitoring_kind[roop_count] &&
8223 spider->need_mons[roop_count]
8224 ) {
8225 error_num = spider_ping_table_mon_from_table(
8226 spider->trx,
8227 spider->trx->thd,
8228 share,
8229 roop_count,
8230 (uint32) share->monitoring_sid[roop_count],
8231 share->table_name,
8232 share->table_name_length,
8233 spider->conn_link_idx[roop_count],
8234 NULL,
8235 0,
8236 share->monitoring_kind[roop_count],
8237 share->monitoring_limit[roop_count],
8238 share->monitoring_flag[roop_count],
8239 TRUE
8240 );
8241 }
8242 DBUG_RETURN(error_num);
8243 }
8244 }
8245 }
8246 DBUG_RETURN(0);
8247}
8248
8249int spider_db_analyze_table(
8250 ha_spider *spider
8251) {
8252 int error_num, roop_count;
8253 SPIDER_SHARE *share = spider->share;
8254 SPIDER_CONN *conn;
8255 spider_db_handler *dbton_hdl;
8256 DBUG_ENTER("spider_db_analyze_table");
8257 if (
8258 spider_param_internal_optimize(spider->trx->thd,
8259 share->internal_optimize) == 1
8260 ) {
8261 for (
8262 roop_count = spider_conn_link_idx_next(share->link_statuses,
8263 spider->conn_link_idx, -1, share->link_count,
8264 SPIDER_LINK_STATUS_RECOVERY);
8265 roop_count < (int) share->link_count;
8266 roop_count = spider_conn_link_idx_next(share->link_statuses,
8267 spider->conn_link_idx, roop_count, share->link_count,
8268 SPIDER_LINK_STATUS_RECOVERY)
8269 ) {
8270 conn = spider->conns[roop_count];
8271 dbton_hdl = spider->dbton_handler[conn->dbton_id];
8272 if ((error_num = dbton_hdl->analyze_table(conn, roop_count)))
8273 {
8274 if (
8275 share->monitoring_kind[roop_count] &&
8276 spider->need_mons[roop_count]
8277 ) {
8278 error_num = spider_ping_table_mon_from_table(
8279 spider->trx,
8280 spider->trx->thd,
8281 share,
8282 roop_count,
8283 (uint32) share->monitoring_sid[roop_count],
8284 share->table_name,
8285 share->table_name_length,
8286 spider->conn_link_idx[roop_count],
8287 NULL,
8288 0,
8289 share->monitoring_kind[roop_count],
8290 share->monitoring_limit[roop_count],
8291 share->monitoring_flag[roop_count],
8292 TRUE
8293 );
8294 }
8295 DBUG_RETURN(error_num);
8296 }
8297 }
8298 }
8299 DBUG_RETURN(0);
8300}
8301
8302int spider_db_optimize_table(
8303 ha_spider *spider
8304) {
8305 int error_num, roop_count;
8306 SPIDER_SHARE *share = spider->share;
8307 SPIDER_CONN *conn;
8308 spider_db_handler *dbton_hdl;
8309 DBUG_ENTER("spider_db_optimize_table");
8310 if (
8311 spider_param_internal_optimize(spider->trx->thd,
8312 share->internal_optimize) == 1
8313 ) {
8314 for (
8315 roop_count = spider_conn_link_idx_next(share->link_statuses,
8316 spider->conn_link_idx, -1, share->link_count,
8317 SPIDER_LINK_STATUS_RECOVERY);
8318 roop_count < (int) share->link_count;
8319 roop_count = spider_conn_link_idx_next(share->link_statuses,
8320 spider->conn_link_idx, roop_count, share->link_count,
8321 SPIDER_LINK_STATUS_RECOVERY)
8322 ) {
8323 conn = spider->conns[roop_count];
8324 dbton_hdl = spider->dbton_handler[conn->dbton_id];
8325 if ((error_num = dbton_hdl->optimize_table(conn, roop_count)))
8326 {
8327 if (
8328 share->monitoring_kind[roop_count] &&
8329 spider->need_mons[roop_count]
8330 ) {
8331 error_num = spider_ping_table_mon_from_table(
8332 spider->trx,
8333 spider->trx->thd,
8334 share,
8335 roop_count,
8336 (uint32) share->monitoring_sid[roop_count],
8337 share->table_name,
8338 share->table_name_length,
8339 spider->conn_link_idx[roop_count],
8340 NULL,
8341 0,
8342 share->monitoring_kind[roop_count],
8343 share->monitoring_limit[roop_count],
8344 share->monitoring_flag[roop_count],
8345 TRUE
8346 );
8347 }
8348 DBUG_RETURN(error_num);
8349 }
8350 }
8351 }
8352 DBUG_RETURN(0);
8353}
8354
8355int spider_db_flush_tables(
8356 ha_spider *spider,
8357 bool lock
8358) {
8359 int error_num, roop_count;
8360 SPIDER_SHARE *share = spider->share;
8361 SPIDER_CONN *conn;
8362 spider_db_handler *dbton_hdl;
8363 DBUG_ENTER("spider_db_flush_tables");
8364 for (
8365 roop_count = spider_conn_link_idx_next(share->link_statuses,
8366 spider->conn_link_idx, -1, share->link_count,
8367 SPIDER_LINK_STATUS_RECOVERY);
8368 roop_count < (int) share->link_count;
8369 roop_count = spider_conn_link_idx_next(share->link_statuses,
8370 spider->conn_link_idx, roop_count, share->link_count,
8371 SPIDER_LINK_STATUS_RECOVERY)
8372 ) {
8373 conn = spider->conns[roop_count];
8374 dbton_hdl = spider->dbton_handler[conn->dbton_id];
8375 if ((error_num = dbton_hdl->flush_tables(conn, roop_count, lock)))
8376 {
8377 if (
8378 share->monitoring_kind[roop_count] &&
8379 spider->need_mons[roop_count]
8380 ) {
8381 error_num = spider_ping_table_mon_from_table(
8382 spider->trx,
8383 spider->trx->thd,
8384 share,
8385 roop_count,
8386 (uint32) share->monitoring_sid[roop_count],
8387 share->table_name,
8388 share->table_name_length,
8389 spider->conn_link_idx[roop_count],
8390 NULL,
8391 0,
8392 share->monitoring_kind[roop_count],
8393 share->monitoring_limit[roop_count],
8394 share->monitoring_flag[roop_count],
8395 TRUE
8396 );
8397 }
8398 DBUG_RETURN(error_num);
8399 }
8400 }
8401 DBUG_RETURN(0);
8402}
8403
8404int spider_db_flush_logs(
8405 ha_spider *spider
8406) {
8407 int roop_count, error_num;
8408 SPIDER_SHARE *share = spider->share;
8409 SPIDER_CONN *conn;
8410 spider_db_handler *dbton_hdl;
8411 DBUG_ENTER("spider_db_flush_logs");
8412 for (
8413 roop_count = spider_conn_link_idx_next(share->link_statuses,
8414 spider->conn_link_idx, -1, share->link_count,
8415 SPIDER_LINK_STATUS_RECOVERY);
8416 roop_count < (int) share->link_count;
8417 roop_count = spider_conn_link_idx_next(share->link_statuses,
8418 spider->conn_link_idx, roop_count, share->link_count,
8419 SPIDER_LINK_STATUS_RECOVERY)
8420 ) {
8421 conn = spider->conns[roop_count];
8422 dbton_hdl = spider->dbton_handler[conn->dbton_id];
8423 if ((error_num = dbton_hdl->flush_logs(conn, roop_count)))
8424 {
8425 if (
8426 share->monitoring_kind[roop_count] &&
8427 spider->need_mons[roop_count]
8428 ) {
8429 error_num = spider_ping_table_mon_from_table(
8430 spider->trx,
8431 spider->trx->thd,
8432 share,
8433 roop_count,
8434 (uint32) share->monitoring_sid[roop_count],
8435 share->table_name,
8436 share->table_name_length,
8437 spider->conn_link_idx[roop_count],
8438 NULL,
8439 0,
8440 share->monitoring_kind[roop_count],
8441 share->monitoring_limit[roop_count],
8442 share->monitoring_flag[roop_count],
8443 TRUE
8444 );
8445 }
8446 DBUG_RETURN(error_num);
8447 }
8448 }
8449 DBUG_RETURN(0);
8450}
8451
8452int spider_db_print_item_type(
8453 Item *item,
8454 ha_spider *spider,
8455 spider_string *str,
8456 const char *alias,
8457 uint alias_length,
8458 uint dbton_id,
8459 bool use_fields,
8460 spider_fields *fields
8461) {
8462 DBUG_ENTER("spider_db_print_item_type");
8463 DBUG_PRINT("info",("spider COND type=%d", item->type()));
8464 switch (item->type())
8465 {
8466 case Item::FUNC_ITEM:
8467 DBUG_RETURN(spider_db_open_item_func((Item_func *) item, spider, str,
8468 alias, alias_length, dbton_id, use_fields, fields));
8469#ifdef HANDLER_HAS_DIRECT_AGGREGATE
8470 case Item::SUM_FUNC_ITEM:
8471 DBUG_RETURN(spider_db_open_item_sum_func((Item_sum *)item, spider, str,
8472 alias, alias_length, dbton_id, use_fields, fields));
8473#endif
8474 case Item::COND_ITEM:
8475 DBUG_RETURN(spider_db_open_item_cond((Item_cond *) item, spider, str,
8476 alias, alias_length, dbton_id, use_fields, fields));
8477 case Item::FIELD_ITEM:
8478 DBUG_RETURN(spider_db_open_item_field((Item_field *) item, spider, str,
8479 alias, alias_length, dbton_id, use_fields, fields));
8480 case Item::REF_ITEM:
8481 DBUG_RETURN(spider_db_open_item_ref((Item_ref *) item, spider, str,
8482 alias, alias_length, dbton_id, use_fields, fields));
8483 case Item::ROW_ITEM:
8484 DBUG_RETURN(spider_db_open_item_row((Item_row *) item, spider, str,
8485 alias, alias_length, dbton_id, use_fields, fields));
8486 case Item::STRING_ITEM:
8487 DBUG_RETURN(spider_db_open_item_string(item, spider, str,
8488 alias, alias_length, dbton_id, use_fields, fields));
8489 case Item::INT_ITEM:
8490 case Item::REAL_ITEM:
8491 case Item::DECIMAL_ITEM:
8492 DBUG_RETURN(spider_db_open_item_int(item, spider, str,
8493 alias, alias_length, dbton_id, use_fields, fields));
8494 case Item::CACHE_ITEM:
8495 DBUG_RETURN(spider_db_open_item_cache((Item_cache *)item, spider, str,
8496 alias, alias_length, dbton_id, use_fields, fields));
8497 case Item::INSERT_VALUE_ITEM:
8498 DBUG_RETURN(spider_db_open_item_insert_value((Item_insert_value *)item,
8499 spider, str, alias, alias_length, dbton_id, use_fields, fields));
8500 case Item::SUBSELECT_ITEM:
8501 case Item::TRIGGER_FIELD_ITEM:
8502#ifdef SPIDER_HAS_EXPR_CACHE_ITEM
8503 case Item::EXPR_CACHE_ITEM:
8504#endif
8505 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
8506 default:
8507 THD *thd = spider->trx->thd;
8508 SPIDER_SHARE *share = spider->share;
8509 if (spider_param_skip_default_condition(thd,
8510 share->skip_default_condition))
8511 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
8512 if (str)
8513 {
8514 if (spider->share->access_charset->cset == system_charset_info->cset)
8515 {
8516#if MYSQL_VERSION_ID < 50500
8517 item->print(str->get_str(), QT_IS);
8518#else
8519 item->print(str->get_str(), QT_TO_SYSTEM_CHARSET);
8520#endif
8521 } else {
8522 item->print(str->get_str(), QT_ORDINARY);
8523 }
8524 str->mem_calc();
8525 }
8526 break;
8527 }
8528 DBUG_RETURN(0);
8529}
8530
8531int spider_db_open_item_cond(
8532 Item_cond *item_cond,
8533 ha_spider *spider,
8534 spider_string *str,
8535 const char *alias,
8536 uint alias_length,
8537 uint dbton_id,
8538 bool use_fields,
8539 spider_fields *fields
8540) {
8541 int error_num = 0;
8542 List_iterator_fast<Item> lif(*(item_cond->argument_list()));
8543 Item *item;
8544 char *func_name = NULL;
8545 int func_name_length = 0, restart_pos = 0;
8546 DBUG_ENTER("spider_db_open_item_cond");
8547 if (str)
8548 {
8549 if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
8550 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8551 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
8552 }
8553
8554restart_first:
8555 if ((item = lif++))
8556 {
8557 if (str)
8558 restart_pos = str->length();
8559 if ((error_num = spider_db_print_item_type(item, spider, str,
8560 alias, alias_length, dbton_id, use_fields, fields)))
8561 {
8562 if (
8563 str &&
8564 error_num == ER_SPIDER_COND_SKIP_NUM &&
8565 item_cond->functype() == Item_func::COND_AND_FUNC
8566 ) {
8567 DBUG_PRINT("info",("spider COND skip"));
8568 str->length(restart_pos);
8569 goto restart_first;
8570 }
8571 DBUG_RETURN(error_num);
8572 }
8573 }
8574 if (error_num)
8575 DBUG_RETURN(error_num);
8576 while ((item = lif++))
8577 {
8578 if (str)
8579 {
8580 restart_pos = str->length();
8581 if (!func_name)
8582 {
8583 func_name = (char*) item_cond->func_name();
8584 func_name_length = strlen(func_name);
8585 }
8586 if (str->reserve(func_name_length + SPIDER_SQL_SPACE_LEN * 2))
8587 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8588 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
8589 str->q_append(func_name, func_name_length);
8590 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
8591 }
8592
8593 if ((error_num = spider_db_print_item_type(item, spider, str,
8594 alias, alias_length, dbton_id, use_fields, fields)))
8595 {
8596 if (
8597 str &&
8598 error_num == ER_SPIDER_COND_SKIP_NUM &&
8599 item_cond->functype() == Item_func::COND_AND_FUNC
8600 ) {
8601 DBUG_PRINT("info",("spider COND skip"));
8602 str->length(restart_pos);
8603 } else
8604 DBUG_RETURN(error_num);
8605 }
8606 }
8607 if (str)
8608 {
8609 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN))
8610 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8611 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
8612 }
8613 DBUG_RETURN(0);
8614}
8615
8616int spider_db_open_item_func(
8617 Item_func *item_func,
8618 ha_spider *spider,
8619 spider_string *str,
8620 const char *alias,
8621 uint alias_length,
8622 uint dbton_id,
8623 bool use_fields,
8624 spider_fields *fields
8625) {
8626 DBUG_ENTER("spider_db_open_item_func");
8627 DBUG_RETURN(spider_dbton[dbton_id].db_util->open_item_func(
8628 item_func, spider, str, alias, alias_length, use_fields, fields));
8629}
8630
8631#ifdef HANDLER_HAS_DIRECT_AGGREGATE
8632int spider_db_open_item_sum_func(
8633 Item_sum *item_sum,
8634 ha_spider *spider,
8635 spider_string *str,
8636 const char *alias,
8637 uint alias_length,
8638 uint dbton_id,
8639 bool use_fields,
8640 spider_fields *fields
8641) {
8642 DBUG_ENTER("spider_db_open_item_func");
8643 DBUG_RETURN(spider_dbton[dbton_id].db_util->open_item_sum_func(
8644 item_sum, spider, str, alias, alias_length, use_fields, fields));
8645}
8646#endif
8647
8648int spider_db_open_item_ident(
8649 Item_ident *item_ident,
8650 ha_spider *spider,
8651 spider_string *str,
8652 const char *alias,
8653 uint alias_length,
8654 uint dbton_id,
8655 bool use_fields,
8656 spider_fields *fields
8657) {
8658 int error_num, field_name_length;
8659 SPIDER_SHARE *share = spider->share;
8660 DBUG_ENTER("spider_db_open_item_ident");
8661 if (
8662 item_ident->cached_field_index != NO_CACHED_FIELD_INDEX &&
8663 item_ident->cached_table
8664 ) {
8665 Field *field = item_ident->cached_table->table->field[
8666 item_ident->cached_field_index];
8667 DBUG_PRINT("info",("spider use cached_field_index"));
8668 if (!use_fields)
8669 {
8670 if (!(field = spider->field_exchange(field)))
8671 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
8672 if (str)
8673 {
8674 if ((error_num = share->dbton_share[dbton_id]->
8675 append_column_name_with_alias(str, field->field_index,
8676 alias, alias_length)))
8677 DBUG_RETURN(error_num);
8678 }
8679 } else {
8680 if (str)
8681 {
8682 SPIDER_FIELD_CHAIN *field_chain = fields->get_next_field_chain();
8683 SPIDER_FIELD_HOLDER *field_holder = field_chain->field_holder;
8684 spider = field_holder->spider;
8685 share = spider->share;
8686 field = spider->field_exchange(field);
8687 DBUG_ASSERT(field);
8688 if ((error_num = share->dbton_share[dbton_id]->
8689 append_column_name_with_alias(str, field->field_index,
8690 field_holder->alias->ptr(), field_holder->alias->length())))
8691 DBUG_RETURN(error_num);
8692 } else {
8693 if ((error_num = fields->add_field(field)))
8694 {
8695 DBUG_RETURN(error_num);
8696 }
8697 }
8698 }
8699 DBUG_RETURN(0);
8700 }
8701 if (str)
8702 {
8703 field_name_length = item_ident->field_name.length;
8704 if (share->access_charset->cset == system_charset_info->cset)
8705 {
8706 if (str->reserve(alias_length +
8707 field_name_length + /* SPIDER_SQL_NAME_QUOTE_LEN */ 2))
8708 {
8709 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8710 }
8711 str->q_append(alias, alias_length);
8712 if ((error_num = spider_dbton[dbton_id].db_util->
8713 append_name(str, item_ident->field_name.str, field_name_length)))
8714 {
8715 DBUG_RETURN(error_num);
8716 }
8717 } else {
8718 if (str->reserve(alias_length))
8719 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8720 str->q_append(alias, alias_length);
8721 if ((error_num = spider_dbton[dbton_id].db_util->
8722 append_name_with_charset(str, item_ident->field_name.str,
8723 field_name_length, system_charset_info)))
8724 {
8725 DBUG_RETURN(error_num);
8726 }
8727 }
8728 }
8729 DBUG_RETURN(0);
8730}
8731
8732int spider_db_open_item_field(
8733 Item_field *item_field,
8734 ha_spider *spider,
8735 spider_string *str,
8736 const char *alias,
8737 uint alias_length,
8738 uint dbton_id,
8739 bool use_fields,
8740 spider_fields *fields
8741) {
8742 int error_num;
8743 Field *field = item_field->field;
8744 SPIDER_SHARE *share = spider->share;
8745 DBUG_ENTER("spider_db_open_item_field");
8746 if (field && !field->table->const_table)
8747 {
8748 DBUG_PRINT("info",("spider field=%p", field));
8749 DBUG_PRINT("info",("spider db=%s", field->table->s->db.str));
8750 DBUG_PRINT("info",("spider table_name=%s", field->table->s->table_name.str));
8751 DBUG_PRINT("info",("spider tmp_table=%u", field->table->s->tmp_table));
8752 if (field->table->s->tmp_table != INTERNAL_TMP_TABLE)
8753 {
8754 if (!use_fields)
8755 {
8756 if (!(field = spider->field_exchange(field)))
8757 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
8758 if (str)
8759 {
8760 if ((error_num = share->dbton_share[dbton_id]->
8761 append_column_name_with_alias(str, field->field_index,
8762 alias, alias_length)))
8763 DBUG_RETURN(error_num);
8764 }
8765 DBUG_RETURN(0);
8766 } else {
8767 if (str)
8768 {
8769 SPIDER_FIELD_CHAIN *field_chain = fields->get_next_field_chain();
8770 SPIDER_FIELD_HOLDER *field_holder = field_chain->field_holder;
8771 spider = field_holder->spider;
8772 share = spider->share;
8773 field = spider->field_exchange(field);
8774 DBUG_ASSERT(field);
8775 if ((error_num = share->dbton_share[dbton_id]->
8776 append_column_name_with_alias(str, field->field_index,
8777 field_holder->alias->ptr(), field_holder->alias->length())))
8778 DBUG_RETURN(error_num);
8779 } else {
8780 if ((error_num = fields->add_field(field)))
8781 {
8782 DBUG_RETURN(error_num);
8783 }
8784 }
8785 DBUG_RETURN(0);
8786 }
8787 }
8788 }
8789 DBUG_RETURN(spider_db_open_item_ident(
8790 (Item_ident *) item_field, spider, str, alias, alias_length, dbton_id,
8791 use_fields, fields));
8792}
8793
8794int spider_db_open_item_ref(
8795 Item_ref *item_ref,
8796 ha_spider *spider,
8797 spider_string *str,
8798 const char *alias,
8799 uint alias_length,
8800 uint dbton_id,
8801 bool use_fields,
8802 spider_fields *fields
8803) {
8804 int error_num;
8805 DBUG_ENTER("spider_db_open_item_ref");
8806 if (item_ref->ref)
8807 {
8808 if (
8809 (*(item_ref->ref))->type() != Item::CACHE_ITEM &&
8810 item_ref->ref_type() != Item_ref::VIEW_REF &&
8811 !item_ref->table_name &&
8812 item_ref->name.str &&
8813 item_ref->alias_name_used
8814 ) {
8815 if (str)
8816 {
8817 uint length = item_ref->name.length;
8818 if (str->reserve(length + /* SPIDER_SQL_NAME_QUOTE_LEN */ 2))
8819 {
8820 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8821 }
8822 if ((error_num = spider_dbton[dbton_id].db_util->
8823 append_name(str, item_ref->name.str, length)))
8824 {
8825 DBUG_RETURN(error_num);
8826 }
8827 }
8828 DBUG_RETURN(0);
8829 }
8830 DBUG_RETURN(spider_db_print_item_type(*(item_ref->ref), spider, str,
8831 alias, alias_length, dbton_id, use_fields, fields));
8832 }
8833 DBUG_RETURN(spider_db_open_item_ident((Item_ident *) item_ref, spider, str,
8834 alias, alias_length, dbton_id, use_fields, fields));
8835}
8836
8837int spider_db_open_item_row(
8838 Item_row *item_row,
8839 ha_spider *spider,
8840 spider_string *str,
8841 const char *alias,
8842 uint alias_length,
8843 uint dbton_id,
8844 bool use_fields,
8845 spider_fields *fields
8846) {
8847 int error_num;
8848 uint roop_count, cols = item_row->cols() - 1;
8849 Item *item;
8850 DBUG_ENTER("spider_db_open_item_row");
8851 if (str)
8852 {
8853 if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
8854 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8855 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
8856 }
8857 for (roop_count = 0; roop_count < cols; roop_count++)
8858 {
8859 item = item_row->element_index(roop_count);
8860 if ((error_num = spider_db_print_item_type(item, spider, str,
8861 alias, alias_length, dbton_id, use_fields, fields)))
8862 DBUG_RETURN(error_num);
8863 if (str)
8864 {
8865 if (str->reserve(SPIDER_SQL_COMMA_LEN))
8866 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8867 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
8868 }
8869 }
8870 item = item_row->element_index(roop_count);
8871 if ((error_num = spider_db_print_item_type(item, spider, str,
8872 alias, alias_length, dbton_id, use_fields, fields)))
8873 DBUG_RETURN(error_num);
8874 if (str)
8875 {
8876 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN))
8877 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8878 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
8879 SPIDER_SQL_CLOSE_PAREN_LEN);
8880 }
8881 DBUG_RETURN(0);
8882}
8883
8884int spider_db_open_item_string(
8885 Item *item,
8886 ha_spider *spider,
8887 spider_string *str,
8888 const char *alias,
8889 uint alias_length,
8890 uint dbton_id,
8891 bool use_fields,
8892 spider_fields *fields
8893) {
8894 DBUG_ENTER("spider_db_open_item_string");
8895 if (str)
8896 {
8897 char tmp_buf[MAX_FIELD_WIDTH];
8898 spider_string tmp_str(tmp_buf, MAX_FIELD_WIDTH, str->charset());
8899 String *tmp_str2;
8900 tmp_str.init_calc_mem(126);
8901 if (!(tmp_str2 = item->val_str(tmp_str.get_str())))
8902 {
8903 if (str->reserve(SPIDER_SQL_NULL_LEN))
8904 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8905 str->q_append(SPIDER_SQL_NULL_STR, SPIDER_SQL_NULL_LEN);
8906 } else {
8907 if (str->reserve(SPIDER_SQL_VALUE_QUOTE_LEN * 2 +
8908 tmp_str2->length() * 2))
8909 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8910 tmp_str.mem_calc();
8911 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
8912 str->append_escape_string(tmp_str2->ptr(), tmp_str2->length());
8913 if (
8914 str->reserve(SPIDER_SQL_VALUE_QUOTE_LEN)
8915 )
8916 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8917 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
8918 }
8919 }
8920 DBUG_RETURN(0);
8921}
8922
8923int spider_db_open_item_int(
8924 Item *item,
8925 ha_spider *spider,
8926 spider_string *str,
8927 const char *alias,
8928 uint alias_length,
8929 uint dbton_id,
8930 bool use_fields,
8931 spider_fields *fields
8932) {
8933 DBUG_ENTER("spider_db_open_item_int");
8934 if (str)
8935 {
8936 char tmp_buf[MAX_FIELD_WIDTH];
8937 spider_string tmp_str(tmp_buf, MAX_FIELD_WIDTH, str->charset());
8938 String *tmp_str2;
8939 tmp_str.init_calc_mem(127);
8940 if (!(tmp_str2 = item->val_str(tmp_str.get_str())))
8941 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8942 tmp_str.mem_calc();
8943#ifdef SPIDER_ITEM_HAS_CMP_TYPE
8944 DBUG_PRINT("info",("spider cmp_type=%u", item->cmp_type()));
8945 if (item->cmp_type() == TIME_RESULT)
8946 {
8947 if (str->reserve(SPIDER_SQL_VALUE_QUOTE_LEN * 2 + tmp_str2->length()))
8948 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8949 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
8950 str->append(*tmp_str2);
8951 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
8952 } else {
8953#endif
8954 if (str->append(*tmp_str2))
8955 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8956#ifdef SPIDER_ITEM_HAS_CMP_TYPE
8957 }
8958#endif
8959 }
8960 DBUG_RETURN(0);
8961}
8962
8963int spider_db_open_item_cache(
8964 Item_cache *item_cache,
8965 ha_spider *spider,
8966 spider_string *str,
8967 const char *alias,
8968 uint alias_length,
8969 uint dbton_id,
8970 bool use_fields,
8971 spider_fields *fields
8972) {
8973 DBUG_ENTER("spider_db_open_item_cache");
8974 if (!item_cache->const_item())
8975 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
8976 DBUG_PRINT("info",("spider result_type=%u", item_cache->result_type()));
8977 switch (item_cache->result_type())
8978 {
8979 case STRING_RESULT:
8980 DBUG_RETURN(spider_db_open_item_string(item_cache, spider, str,
8981 alias, alias_length, dbton_id, use_fields, fields));
8982 case ROW_RESULT:
8983 {
8984 int error_num;
8985 Item_cache_row *item_cache_row = (Item_cache_row *) item_cache;
8986 uint item_count = item_cache_row->cols() - 1, roop_count;
8987 if (str)
8988 {
8989 if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
8990 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8991 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
8992 }
8993 for (roop_count = 0; roop_count < item_count; ++roop_count)
8994 {
8995 if ((error_num = spider_db_open_item_cache(
8996 (Item_cache *) item_cache_row->element_index(roop_count),
8997 spider, str, alias, alias_length, dbton_id, use_fields, fields
8998 ))) {
8999 DBUG_RETURN(error_num);
9000 }
9001 if (str)
9002 {
9003 if (str->reserve(SPIDER_SQL_COMMA_LEN))
9004 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9005 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9006 }
9007 }
9008 if ((error_num = spider_db_open_item_cache(
9009 (Item_cache *) item_cache_row->element_index(roop_count),
9010 spider, str, alias, alias_length, dbton_id, use_fields, fields
9011 ))) {
9012 DBUG_RETURN(error_num);
9013 }
9014 if (str)
9015 {
9016 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN))
9017 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9018 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
9019 SPIDER_SQL_CLOSE_PAREN_LEN);
9020 }
9021 }
9022 DBUG_RETURN(0);
9023 case REAL_RESULT:
9024 case INT_RESULT:
9025 case DECIMAL_RESULT:
9026 default:
9027 break;
9028 }
9029 DBUG_RETURN(spider_db_open_item_int(item_cache, spider, str,
9030 alias, alias_length, dbton_id, use_fields, fields));
9031}
9032
9033int spider_db_open_item_insert_value(
9034 Item_insert_value *item_insert_value,
9035 ha_spider *spider,
9036 spider_string *str,
9037 const char *alias,
9038 uint alias_length,
9039 uint dbton_id,
9040 bool use_fields,
9041 spider_fields *fields
9042) {
9043 int error_num;
9044 DBUG_ENTER("spider_db_open_item_insert_value");
9045 if (item_insert_value->arg)
9046 {
9047 if (str)
9048 {
9049 if (str->reserve(SPIDER_SQL_VALUES_LEN + SPIDER_SQL_OPEN_PAREN_LEN))
9050 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9051 str->q_append(SPIDER_SQL_VALUES_STR, SPIDER_SQL_VALUES_LEN);
9052 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
9053 }
9054 if ((error_num = spider_db_print_item_type(item_insert_value->arg, spider,
9055 str, alias, alias_length, dbton_id, use_fields, fields)))
9056 DBUG_RETURN(error_num);
9057 if (str)
9058 {
9059 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN))
9060 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9061 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
9062 }
9063 }
9064 DBUG_RETURN(0);
9065}
9066
9067int spider_db_append_condition(
9068 ha_spider *spider,
9069 const char *alias,
9070 uint alias_length,
9071 bool test_flg
9072) {
9073 int error_num;
9074 DBUG_ENTER("spider_db_append_condition");
9075 if (!test_flg)
9076 {
9077 if (spider->sql_kinds & SPIDER_SQL_KIND_SQL)
9078 {
9079 if ((error_num = spider->append_condition_sql_part(
9080 alias, alias_length, SPIDER_SQL_TYPE_SELECT_SQL, FALSE)))
9081 DBUG_RETURN(error_num);
9082 }
9083 if (spider->sql_kinds & SPIDER_SQL_KIND_HANDLER)
9084 {
9085 if ((error_num = spider->append_condition_sql_part(
9086 alias, alias_length, SPIDER_SQL_TYPE_HANDLER, FALSE)))
9087 DBUG_RETURN(error_num);
9088 }
9089 } else {
9090 if (spider->cond_check)
9091 DBUG_RETURN(spider->cond_check_error);
9092 spider->cond_check = TRUE;
9093 if ((spider->cond_check_error = spider->append_condition_sql_part(
9094 NULL, 0, SPIDER_SQL_TYPE_SELECT_SQL, TRUE)))
9095 DBUG_RETURN(spider->cond_check_error);
9096 }
9097 DBUG_RETURN(0);
9098}
9099
9100#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
9101int spider_db_append_update_columns(
9102 ha_spider *spider,
9103 spider_string *str,
9104 const char *alias,
9105 uint alias_length,
9106 uint dbton_id,
9107 bool use_fields,
9108 spider_fields *fields
9109) {
9110 int error_num;
9111 bool add_comma = FALSE;
9112 List_iterator_fast<Item> fi(*spider->direct_update_fields),
9113 vi(*spider->direct_update_values);
9114 Item *field, *value;
9115 DBUG_ENTER("spider_db_append_update_columns");
9116 while ((field = fi++))
9117 {
9118 value = vi++;
9119 if ((error_num = spider_db_print_item_type(
9120 (Item *) field, spider, str, alias, alias_length, dbton_id,
9121 use_fields, fields)))
9122 {
9123 if (
9124 error_num == ER_SPIDER_COND_SKIP_NUM &&
9125 field->type() == Item::FIELD_ITEM &&
9126 ((Item_field *) field)->field
9127 ) {
9128 DBUG_PRINT("info",("spider no match field(ex. vp child table)"));
9129 continue;
9130 }
9131 DBUG_RETURN(error_num);
9132 }
9133 if (str)
9134 {
9135 if (str->reserve(SPIDER_SQL_EQUAL_LEN))
9136 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9137 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
9138 }
9139 if ((error_num = spider_db_print_item_type(
9140 (Item *) value, spider, str, alias, alias_length, dbton_id,
9141 use_fields, fields)))
9142 DBUG_RETURN(error_num);
9143 if (str)
9144 {
9145 if (str->reserve(SPIDER_SQL_COMMA_LEN))
9146 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9147 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9148 add_comma = TRUE;
9149 }
9150 }
9151 if (str && add_comma)
9152 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
9153 DBUG_RETURN(0);
9154}
9155#endif
9156
9157uint spider_db_check_ft_idx(
9158 Item_func *item_func,
9159 ha_spider *spider
9160) {
9161 uint roop_count, roop_count2, part_num;
9162 uint item_count = item_func->argument_count();
9163 Item **item_list = item_func->arguments();
9164 Item_field *item_field;
9165 Field *field;
9166 TABLE *table = spider->get_table();
9167 TABLE_SHARE *table_share = table->s;
9168 KEY *key_info;
9169 KEY_PART_INFO *key_part;
9170 bool match1, match2;
9171 DBUG_ENTER("spider_db_check_ft_idx");
9172
9173 for (roop_count = 0; roop_count < table_share->keys; roop_count++)
9174 {
9175 key_info = &table->key_info[roop_count];
9176 if (
9177 key_info->algorithm == HA_KEY_ALG_FULLTEXT &&
9178 item_count - 1 == spider_user_defined_key_parts(key_info)
9179 ) {
9180 match1 = TRUE;
9181 for (roop_count2 = 1; roop_count2 < item_count; roop_count2++)
9182 {
9183 item_field = (Item_field *) item_list[roop_count2];
9184 field = item_field->field;
9185 if (!(field = spider->field_exchange(field)))
9186 DBUG_RETURN(MAX_KEY);
9187 match2 = FALSE;
9188 for (key_part = key_info->key_part, part_num = 0;
9189 part_num < spider_user_defined_key_parts(key_info);
9190 key_part++, part_num++)
9191 {
9192 if (key_part->field == field)
9193 {
9194 match2 = TRUE;
9195 break;
9196 }
9197 }
9198 if (!match2)
9199 {
9200 match1 = FALSE;
9201 break;
9202 }
9203 }
9204 if (match1)
9205 DBUG_RETURN(roop_count);
9206 }
9207 }
9208 DBUG_RETURN(MAX_KEY);
9209}
9210
9211int spider_db_udf_fetch_row(
9212 SPIDER_TRX *trx,
9213 Field *field,
9214 SPIDER_DB_ROW *row
9215) {
9216 DBUG_ENTER("spider_db_udf_fetch_row");
9217 DBUG_RETURN(row->store_to_field(field, trx->udf_access_charset));
9218 DBUG_RETURN(0);
9219}
9220
9221int spider_db_udf_fetch_table(
9222 SPIDER_TRX *trx,
9223 SPIDER_CONN *conn,
9224 TABLE *table,
9225 SPIDER_DB_RESULT *result,
9226 uint set_on,
9227 uint set_off
9228) {
9229 int error_num;
9230 SPIDER_DB_ROW *row = NULL;
9231 Field **field;
9232 uint roop_count;
9233 DBUG_ENTER("spider_db_udf_fetch_table");
9234 if (!(row = result->fetch_row()))
9235 DBUG_RETURN(HA_ERR_END_OF_FILE);
9236
9237#ifndef DBUG_OFF
9238 my_bitmap_map *tmp_map =
9239 dbug_tmp_use_all_columns(table, table->write_set);
9240#endif
9241 for (
9242 roop_count = 0,
9243 field = table->field;
9244 roop_count < set_on;
9245 roop_count++,
9246 field++
9247 ) {
9248 if ((error_num =
9249 spider_db_udf_fetch_row(trx, *field, row)))
9250 {
9251#ifndef DBUG_OFF
9252 dbug_tmp_restore_column_map(table->write_set, tmp_map);
9253#endif
9254 DBUG_RETURN(error_num);
9255 }
9256 row->next();
9257 }
9258
9259 for (; roop_count < set_off; roop_count++, field++)
9260 (*field)->set_default();
9261#ifndef DBUG_OFF
9262 dbug_tmp_restore_column_map(table->write_set, tmp_map);
9263#endif
9264 table->status = 0;
9265 DBUG_RETURN(0);
9266}
9267
9268int spider_db_udf_direct_sql_connect(
9269 const SPIDER_DIRECT_SQL *direct_sql,
9270 SPIDER_CONN *conn
9271) {
9272 int error_num, connect_retry_count;
9273 THD* thd = current_thd;
9274 longlong connect_retry_interval;
9275 DBUG_ENTER("spider_db_udf_direct_sql_connect");
9276
9277 if (thd)
9278 {
9279 conn->connect_timeout = spider_param_connect_timeout(thd,
9280 direct_sql->connect_timeout);
9281 conn->net_read_timeout = spider_param_net_read_timeout(thd,
9282 direct_sql->net_read_timeout);
9283 conn->net_write_timeout = spider_param_net_write_timeout(thd,
9284 direct_sql->net_write_timeout);
9285 connect_retry_interval = spider_param_connect_retry_interval(thd);
9286 connect_retry_count = spider_param_connect_retry_count(thd);
9287 } else {
9288 conn->connect_timeout = spider_param_connect_timeout(NULL,
9289 direct_sql->connect_timeout);
9290 conn->net_read_timeout = spider_param_net_read_timeout(NULL,
9291 direct_sql->net_read_timeout);
9292 conn->net_write_timeout = spider_param_net_write_timeout(NULL,
9293 direct_sql->net_write_timeout);
9294 connect_retry_interval = spider_param_connect_retry_interval(NULL);
9295 connect_retry_count = spider_param_connect_retry_count(NULL);
9296 }
9297 DBUG_PRINT("info",("spider connect_timeout=%u", conn->connect_timeout));
9298 DBUG_PRINT("info",("spider net_read_timeout=%u", conn->net_read_timeout));
9299 DBUG_PRINT("info",("spider net_write_timeout=%u", conn->net_write_timeout));
9300
9301#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9302 if (direct_sql->access_mode == 0)
9303 {
9304#endif
9305 if ((error_num = spider_reset_conn_setted_parameter(conn, thd)))
9306 DBUG_RETURN(error_num);
9307#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9308 }
9309#endif
9310
9311 if (conn->dbton_id == SPIDER_DBTON_SIZE)
9312 {
9313#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9314 if (conn->conn_kind == SPIDER_CONN_KIND_MYSQL)
9315 {
9316#endif
9317 my_printf_error(
9318 ER_SPIDER_SQL_WRAPPER_IS_INVALID_NUM,
9319 ER_SPIDER_SQL_WRAPPER_IS_INVALID_STR,
9320 MYF(0), conn->tgt_wrapper);
9321 DBUG_RETURN(ER_SPIDER_SQL_WRAPPER_IS_INVALID_NUM);
9322#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9323 } else {
9324 my_printf_error(
9325 ER_SPIDER_NOSQL_WRAPPER_IS_INVALID_NUM,
9326 ER_SPIDER_NOSQL_WRAPPER_IS_INVALID_STR,
9327 MYF(0), conn->tgt_wrapper);
9328 DBUG_RETURN(ER_SPIDER_NOSQL_WRAPPER_IS_INVALID_NUM);
9329 }
9330#endif
9331 }
9332
9333/*
9334 if (!(conn->db_conn = spider_dbton[conn->dbton_id].create_db_conn(conn)))
9335 {
9336 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9337 }
9338*/
9339
9340 if ((error_num = conn->db_conn->connect(
9341 direct_sql->tgt_host,
9342 direct_sql->tgt_username,
9343 direct_sql->tgt_password,
9344 direct_sql->tgt_port,
9345 direct_sql->tgt_socket,
9346 direct_sql->server_name,
9347 connect_retry_count, connect_retry_interval)))
9348 {
9349 DBUG_RETURN(error_num);
9350 }
9351 ++conn->connection_id;
9352 DBUG_RETURN(0);
9353}
9354
9355int spider_db_udf_direct_sql_ping(
9356 SPIDER_DIRECT_SQL *direct_sql
9357) {
9358 int error_num;
9359 SPIDER_CONN *conn = direct_sql->conn;
9360 DBUG_ENTER("spider_db_udf_direct_sql_ping");
9361 if (conn->server_lost)
9362 {
9363 if ((error_num = spider_db_udf_direct_sql_connect(direct_sql, conn)))
9364 DBUG_RETURN(error_num);
9365 conn->server_lost = FALSE;
9366 }
9367#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9368 if (direct_sql->access_mode == 0)
9369 {
9370#endif
9371 if ((error_num = conn->db_conn->ping()))
9372 {
9373 spider_db_disconnect(conn);
9374 if ((error_num = spider_db_udf_direct_sql_connect(direct_sql, conn)))
9375 {
9376 DBUG_PRINT("info", ("spider conn=%p SERVER_LOST", conn));
9377 conn->server_lost = TRUE;
9378 DBUG_RETURN(error_num);
9379 }
9380 if((error_num = conn->db_conn->ping()))
9381 {
9382 spider_db_disconnect(conn);
9383 DBUG_PRINT("info", ("spider conn=%p SERVER_LOST", conn));
9384 conn->server_lost = TRUE;
9385 DBUG_RETURN(error_num);
9386 }
9387 }
9388#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9389 }
9390#endif
9391 conn->ping_time = (time_t) time((time_t*) 0);
9392 DBUG_RETURN(0);
9393}
9394
9395int spider_db_udf_direct_sql(
9396 SPIDER_DIRECT_SQL *direct_sql
9397) {
9398 int error_num = 0, status = 0, roop_count = 0, need_mon = 0;
9399 uint udf_table_mutex_index, field_num, set_on, set_off;
9400 long long roop_count2;
9401 bool end_of_file;
9402 SPIDER_TRX *trx = direct_sql->trx;
9403 THD *thd = trx->thd, *c_thd = current_thd;
9404 SPIDER_CONN *conn = direct_sql->conn;
9405 SPIDER_DB_RESULT *result = NULL;
9406 TABLE *table;
9407 int bulk_insert_rows = (int) spider_param_udf_ds_bulk_insert_rows(thd,
9408 direct_sql->bulk_insert_rows);
9409 int table_loop_mode = spider_param_udf_ds_table_loop_mode(thd,
9410 direct_sql->table_loop_mode);
9411 double ping_interval_at_trx_start =
9412 spider_param_ping_interval_at_trx_start(thd);
9413 time_t tmp_time = (time_t) time((time_t*) 0);
9414 bool need_trx_end, need_all_commit, insert_start = FALSE;
9415#if MYSQL_VERSION_ID < 50500
9416#else
9417 enum_sql_command sql_command_backup;
9418#endif
9419 DBUG_ENTER("spider_db_udf_direct_sql");
9420#if MYSQL_VERSION_ID < 50500
9421#else
9422 if (direct_sql->real_table_used)
9423 {
9424 if (spider_sys_open_tables(c_thd, &direct_sql->table_list_first,
9425 &direct_sql->open_tables_backup))
9426 {
9427 direct_sql->real_table_used = FALSE;
9428 DBUG_RETURN(my_errno);
9429 }
9430 for (roop_count = 0; roop_count < direct_sql->table_count; roop_count++)
9431 {
9432 if (!spider_bit_is_set(direct_sql->real_table_bitmap, roop_count))
9433 continue;
9434 direct_sql->tables[roop_count] =
9435 direct_sql->table_list[roop_count].table;
9436 }
9437 direct_sql->open_tables_thd = c_thd;
9438 roop_count = 0;
9439 }
9440#endif
9441
9442 if (c_thd != thd)
9443 {
9444 need_all_commit = TRUE;
9445 need_trx_end = TRUE;
9446 } else {
9447 need_all_commit = FALSE;
9448#if MYSQL_VERSION_ID < 50500
9449#else
9450 if (direct_sql->real_table_used)
9451 {
9452 need_trx_end = TRUE;
9453 } else {
9454#endif
9455 if (c_thd->transaction.stmt.ha_list)
9456 need_trx_end = FALSE;
9457 else
9458 need_trx_end = TRUE;
9459#if MYSQL_VERSION_ID < 50500
9460#else
9461 }
9462#endif
9463 }
9464
9465 if (!conn->disable_reconnect)
9466 {
9467 if (
9468 (
9469 conn->server_lost ||
9470 difftime(tmp_time, conn->ping_time) >= ping_interval_at_trx_start
9471 ) &&
9472 (error_num = spider_db_udf_direct_sql_ping(direct_sql))
9473 )
9474 DBUG_RETURN(error_num);
9475 } else if (conn->server_lost)
9476 {
9477 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
9478 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
9479 DBUG_RETURN(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM);
9480 }
9481
9482#if MYSQL_VERSION_ID < 50500
9483#else
9484 sql_command_backup = c_thd->lex->sql_command;
9485 c_thd->lex->sql_command = SQLCOM_INSERT;
9486#endif
9487
9488 conn->need_mon = &need_mon;
9489 if (
9490 !(error_num = spider_db_udf_direct_sql_set_names(direct_sql, trx, conn)) &&
9491 !(error_num = spider_db_udf_direct_sql_select_db(direct_sql, conn))
9492 ) {
9493#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9494 if (direct_sql->access_mode != 0)
9495 {
9496 st_spider_db_request_key request_key;
9497 request_key.spider_thread_id = direct_sql->trx->spider_thread_id;
9498 request_key.query_id = direct_sql->trx->thd->query_id;
9499 request_key.handler = direct_sql;
9500 request_key.request_id = 1;
9501 request_key.next = NULL;
9502 if ((error_num = conn->db_conn->append_sql(
9503 direct_sql->sql, direct_sql->sql_length, &request_key)))
9504 {
9505#if MYSQL_VERSION_ID < 50500
9506#else
9507 c_thd->lex->sql_command = sql_command_backup;
9508#endif
9509 DBUG_RETURN(error_num);
9510 }
9511 pthread_mutex_lock(&conn->mta_conn_mutex);
9512 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
9513 DBUG_ASSERT(conn->mta_conn_mutex_file_pos.file_name);
9514 }
9515#endif
9516 spider_conn_set_timeout_from_direct_sql(conn, thd, direct_sql);
9517 if (spider_db_query(
9518 conn,
9519 direct_sql->sql,
9520 direct_sql->sql_length,
9521 -1,
9522 &need_mon)
9523 ) {
9524 error_num = spider_db_errorno(conn);
9525 if (error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM)
9526 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
9527 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
9528 } else {
9529 DBUG_PRINT("info",("spider conn=%p", conn));
9530 if (!direct_sql->table_count)
9531 roop_count = -1;
9532 conn->mta_conn_mutex_lock_already = TRUE;
9533 conn->mta_conn_mutex_unlock_later = TRUE;
9534 do {
9535 if (roop_count == direct_sql->table_count)
9536 {
9537 if (table_loop_mode == 1)
9538 roop_count--;
9539 else if (table_loop_mode == 2)
9540 roop_count = 0;
9541 else
9542 roop_count = -1;
9543 }
9544 st_spider_db_request_key request_key;
9545 request_key.spider_thread_id = direct_sql->trx->spider_thread_id;
9546 request_key.query_id = direct_sql->trx->thd->query_id;
9547 request_key.handler = direct_sql;
9548 request_key.request_id = 1;
9549 request_key.next = NULL;
9550 if ((result = conn->db_conn->use_result(&request_key, &error_num)))
9551 {
9552 end_of_file = FALSE;
9553 if (roop_count >= 0)
9554 {
9555 while (!error_num && !end_of_file)
9556 {
9557 udf_table_mutex_index = spider_udf_calc_hash(
9558 direct_sql->db_names[roop_count],
9559 spider_param_udf_table_lock_mutex_count());
9560 udf_table_mutex_index += spider_udf_calc_hash(
9561 direct_sql->table_names[roop_count],
9562 spider_param_udf_table_lock_mutex_count());
9563 udf_table_mutex_index %=
9564 spider_param_udf_table_lock_mutex_count();
9565 pthread_mutex_lock(
9566 &trx->udf_table_mutexes[udf_table_mutex_index]);
9567 table = direct_sql->tables[roop_count];
9568 table->in_use = c_thd;
9569 memset((uchar *) table->null_flags, ~(uchar) 0,
9570 sizeof(uchar) * table->s->null_bytes);
9571 insert_start = TRUE;
9572
9573 field_num = result->num_fields();
9574 if (field_num > table->s->fields)
9575 {
9576 set_on = table->s->fields;
9577 set_off = table->s->fields;
9578 } else {
9579 set_on = field_num;
9580 set_off = table->s->fields;
9581 }
9582 for (roop_count2 = 0; roop_count2 < set_on; roop_count2++)
9583 bitmap_set_bit(table->write_set, (uint) roop_count2);
9584 for (; roop_count2 < set_off; roop_count2++)
9585 bitmap_clear_bit(table->write_set, (uint) roop_count2);
9586
9587#if MYSQL_VERSION_ID < 50500
9588 if (table->file->has_transactions())
9589#endif
9590 {
9591 THR_LOCK_DATA *to[2];
9592 table->file->store_lock(table->in_use, to,
9593 TL_WRITE_CONCURRENT_INSERT);
9594 if ((error_num = table->file->ha_external_lock(table->in_use,
9595 F_WRLCK)))
9596 {
9597 table->file->print_error(error_num, MYF(0));
9598 break;
9599 }
9600#if MYSQL_VERSION_ID < 50500
9601#else
9602 if (
9603 table->s->tmp_table == NO_TMP_TABLE &&
9604 table->pos_in_table_list
9605 ) {
9606 TABLE_LIST *next_tables =
9607 table->pos_in_table_list->next_global;
9608 while (next_tables && next_tables->parent_l)
9609 {
9610 DBUG_PRINT("info",("spider call child lock"));
9611 TABLE *child_table = next_tables->table;
9612 child_table->file->store_lock(child_table->in_use, to,
9613 TL_WRITE_CONCURRENT_INSERT);
9614 if ((error_num = child_table->file->ha_external_lock(
9615 child_table->in_use, F_WRLCK)))
9616 {
9617 table->file->print_error(error_num, MYF(0));
9618 break;
9619 }
9620 next_tables = next_tables->next_global;
9621 }
9622 }
9623#endif
9624 }
9625
9626 if (direct_sql->iop)
9627 {
9628 if (direct_sql->iop[roop_count] == 1)
9629 table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
9630 else if (direct_sql->iop[roop_count] == 2)
9631 table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
9632 }
9633 table->file->ha_start_bulk_insert(
9634 (ha_rows) bulk_insert_rows);
9635
9636 for (roop_count2 = 0;
9637 roop_count2 < bulk_insert_rows;
9638 roop_count2++)
9639 {
9640 if ((error_num = spider_db_udf_fetch_table(
9641 trx, conn, table, result, set_on, set_off)))
9642 {
9643 if (error_num == HA_ERR_END_OF_FILE)
9644 {
9645 end_of_file = TRUE;
9646 error_num = 0;
9647 }
9648 break;
9649 }
9650 if (direct_sql->iop && direct_sql->iop[roop_count] == 2)
9651 {
9652 if ((error_num = spider_sys_replace(table,
9653 &direct_sql->modified_non_trans_table)))
9654 {
9655 table->file->print_error(error_num, MYF(0));
9656 break;
9657 }
9658 } else if ((error_num =
9659 table->file->ha_write_row(table->record[0])))
9660 {
9661 /* insert */
9662 if (
9663 !direct_sql->iop || direct_sql->iop[roop_count] != 1 ||
9664 table->file->is_fatal_error(error_num, HA_CHECK_DUP)
9665 ) {
9666 DBUG_PRINT("info",("spider error_num=%d", error_num));
9667 table->file->print_error(error_num, MYF(0));
9668 break;
9669 } else
9670 error_num = 0;
9671 }
9672 }
9673
9674 if (error_num)
9675 table->file->ha_end_bulk_insert();
9676 else
9677 error_num = table->file->ha_end_bulk_insert();
9678 if (direct_sql->iop)
9679 {
9680 if (direct_sql->iop[roop_count] == 1)
9681 table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
9682 else if (direct_sql->iop[roop_count] == 2)
9683 table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
9684 }
9685#if MYSQL_VERSION_ID < 50500
9686 if (table->file->has_transactions())
9687#endif
9688 {
9689 table->file->ha_external_lock(table->in_use, F_UNLCK);
9690#if MYSQL_VERSION_ID < 50500
9691#else
9692 if (
9693 table->s->tmp_table == NO_TMP_TABLE &&
9694 table->pos_in_table_list
9695 ) {
9696 TABLE_LIST *next_tables =
9697 table->pos_in_table_list->next_global;
9698 while (next_tables && next_tables->parent_l)
9699 {
9700 DBUG_PRINT("info",("spider call child lock"));
9701 TABLE *child_table = next_tables->table;
9702 child_table->file->ha_external_lock(child_table->in_use,
9703 F_UNLCK);
9704 next_tables = next_tables->next_global;
9705 }
9706 }
9707#endif
9708 }
9709 table->file->ha_reset();
9710 table->in_use = thd;
9711 pthread_mutex_unlock(
9712 &trx->udf_table_mutexes[udf_table_mutex_index]);
9713 }
9714 if (error_num)
9715 roop_count = -1;
9716 }
9717 result->free_result();
9718 delete result;
9719 } else {
9720 if (!error_num)
9721 {
9722 error_num = spider_db_errorno(conn);
9723 }
9724 if (error_num)
9725 {
9726 if (error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM)
9727 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
9728 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
9729 else if (error_num == HA_ERR_FOUND_DUPP_KEY)
9730 {
9731 my_printf_error(ER_SPIDER_HS_NUM, ER_SPIDER_HS_STR, MYF(0),
9732 conn->db_conn->get_errno(), conn->db_conn->get_error());
9733 }
9734 break;
9735 }
9736 }
9737 if ((status = conn->db_conn->next_result()) > 0)
9738 {
9739 error_num = status;
9740 break;
9741 }
9742 if (roop_count >= 0)
9743 roop_count++;
9744 } while (status == 0);
9745 conn->mta_conn_mutex_lock_already = FALSE;
9746 conn->mta_conn_mutex_unlock_later = FALSE;
9747 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
9748 pthread_mutex_unlock(&conn->mta_conn_mutex);
9749 }
9750 }
9751 if (need_trx_end && insert_start)
9752 {
9753 if (error_num)
9754 {
9755 (void) ha_rollback_trans(c_thd, FALSE);
9756 if (need_all_commit)
9757 (void) ha_rollback_trans(c_thd, TRUE);
9758 } else {
9759 if ((error_num = ha_commit_trans(c_thd, FALSE)))
9760 my_error(error_num, MYF(0));
9761 if (need_all_commit)
9762 {
9763 if ((error_num = ha_commit_trans(c_thd, TRUE)))
9764 my_error(error_num, MYF(0));
9765 }
9766 }
9767 }
9768#if MYSQL_VERSION_ID < 50500
9769#else
9770 c_thd->lex->sql_command = sql_command_backup;
9771#endif
9772 DBUG_RETURN(error_num);
9773}
9774
9775int spider_db_udf_direct_sql_select_db(
9776 SPIDER_DIRECT_SQL *direct_sql,
9777 SPIDER_CONN *conn
9778) {
9779 int error_num, need_mon = 0;
9780 bool tmp_mta_conn_mutex_lock_already;
9781 SPIDER_DB_CONN *db_conn = conn->db_conn;
9782 DBUG_ENTER("spider_db_udf_direct_sql_select_db");
9783#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9784 if (direct_sql->access_mode == 0)
9785 {
9786#endif
9787 if (!conn->mta_conn_mutex_lock_already)
9788 {
9789 pthread_mutex_lock(&conn->mta_conn_mutex);
9790 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
9791 conn->need_mon = &need_mon;
9792 }
9793 DBUG_ASSERT(conn->mta_conn_mutex_file_pos.file_name);
9794 if (
9795 !conn->default_database.length() ||
9796 conn->default_database.length() !=
9797 direct_sql->tgt_default_db_name_length ||
9798 memcmp(direct_sql->tgt_default_db_name, conn->default_database.ptr(),
9799 direct_sql->tgt_default_db_name_length)
9800 ) {
9801 tmp_mta_conn_mutex_lock_already = conn->mta_conn_mutex_lock_already;
9802 conn->mta_conn_mutex_lock_already = TRUE;
9803 if (
9804 (
9805 spider_db_before_query(conn, &need_mon) ||
9806 db_conn->select_db(direct_sql->tgt_default_db_name)
9807 ) &&
9808 (error_num = spider_db_errorno(conn))
9809 ) {
9810 if (
9811 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
9812 !conn->disable_reconnect
9813 )
9814 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
9815 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
9816 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
9817 DBUG_RETURN(error_num);
9818 }
9819 conn->default_database.length(0);
9820 if (conn->default_database.reserve(
9821 direct_sql->tgt_default_db_name_length + 1))
9822 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9823 conn->default_database.q_append(direct_sql->tgt_default_db_name,
9824 direct_sql->tgt_default_db_name_length + 1);
9825 conn->default_database.length(direct_sql->tgt_default_db_name_length);
9826 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
9827 }
9828 if (!conn->mta_conn_mutex_unlock_later)
9829 {
9830 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
9831 pthread_mutex_unlock(&conn->mta_conn_mutex);
9832 }
9833#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9834 }
9835#endif
9836 DBUG_RETURN(0);
9837}
9838
9839int spider_db_udf_direct_sql_set_names(
9840 SPIDER_DIRECT_SQL *direct_sql,
9841 SPIDER_TRX *trx,
9842 SPIDER_CONN *conn
9843) {
9844 int error_num, need_mon = 0;
9845 bool tmp_mta_conn_mutex_lock_already;
9846 DBUG_ENTER("spider_db_udf_direct_sql_set_names");
9847#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9848 if (direct_sql->access_mode == 0)
9849 {
9850#endif
9851 if (!conn->mta_conn_mutex_lock_already)
9852 {
9853 pthread_mutex_lock(&conn->mta_conn_mutex);
9854 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
9855 conn->need_mon = &need_mon;
9856 }
9857 DBUG_ASSERT(conn->mta_conn_mutex_file_pos.file_name);
9858 if (
9859 !conn->access_charset ||
9860 trx->udf_access_charset->cset != conn->access_charset->cset
9861 ) {
9862 tmp_mta_conn_mutex_lock_already = conn->mta_conn_mutex_lock_already;
9863 conn->mta_conn_mutex_lock_already = TRUE;
9864 if (
9865 (
9866 spider_db_before_query(conn, &need_mon) ||
9867 conn->db_conn->set_character_set(trx->udf_access_charset->csname)
9868 ) &&
9869 (error_num = spider_db_errorno(conn))
9870 ) {
9871 if (
9872 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
9873 !conn->disable_reconnect
9874 ) {
9875 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
9876 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
9877 }
9878 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
9879 DBUG_RETURN(error_num);
9880 }
9881 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
9882 conn->access_charset = trx->udf_access_charset;
9883 }
9884 if (!conn->mta_conn_mutex_unlock_later)
9885 {
9886 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
9887 pthread_mutex_unlock(&conn->mta_conn_mutex);
9888 }
9889#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9890 }
9891#endif
9892 DBUG_RETURN(0);
9893}
9894
9895int spider_db_udf_check_and_set_set_names(
9896 SPIDER_TRX *trx
9897) {
9898 int error_num;
9899 DBUG_ENTER("spider_db_udf_check_and_set_set_names");
9900 if (
9901 !trx->udf_access_charset ||
9902 trx->udf_access_charset->cset !=
9903 trx->thd->variables.character_set_client->cset)
9904 {
9905 trx->udf_access_charset = trx->thd->variables.character_set_client;
9906 if ((error_num = spider_db_udf_append_set_names(trx)))
9907 DBUG_RETURN(error_num);
9908 }
9909 DBUG_RETURN(0);
9910}
9911
9912int spider_db_udf_append_set_names(
9913 SPIDER_TRX *trx
9914) {
9915 DBUG_ENTER("spider_db_udf_append_set_names");
9916 DBUG_RETURN(0);
9917}
9918
9919void spider_db_udf_free_set_names(
9920 SPIDER_TRX *trx
9921) {
9922 DBUG_ENTER("spider_db_udf_free_set_names");
9923 DBUG_VOID_RETURN;
9924}
9925
9926int spider_db_udf_ping_table(
9927 SPIDER_TABLE_MON_LIST *table_mon_list,
9928 SPIDER_SHARE *share,
9929 SPIDER_TRX *trx,
9930 SPIDER_CONN *conn,
9931 char *where_clause,
9932 uint where_clause_length,
9933 bool ping_only,
9934 bool use_where,
9935 longlong limit
9936) {
9937 int error_num;
9938 DBUG_ENTER("spider_db_udf_ping_table");
9939 if (!pthread_mutex_trylock(&table_mon_list->monitor_mutex))
9940 {
9941 int need_mon = 0;
9942 uint tmp_conn_link_idx = 0;
9943 ha_spider spider;
9944 uchar db_request_phase = 0;
9945 ulonglong db_request_id = 0;
9946 spider.share = share;
9947 spider.trx = trx;
9948 spider.need_mons = &need_mon;
9949 spider.conn_link_idx = &tmp_conn_link_idx;
9950 spider.db_request_phase = &db_request_phase;
9951 spider.db_request_id = &db_request_id;
9952 pthread_mutex_lock(&conn->mta_conn_mutex);
9953 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
9954 conn->need_mon = &need_mon;
9955 conn->mta_conn_mutex_lock_already = TRUE;
9956 conn->mta_conn_mutex_unlock_later = TRUE;
9957 if ((error_num = spider_db_ping(&spider, conn, 0)))
9958 {
9959 conn->mta_conn_mutex_lock_already = FALSE;
9960 conn->mta_conn_mutex_unlock_later = FALSE;
9961 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
9962 pthread_mutex_unlock(&conn->mta_conn_mutex);
9963 table_mon_list->last_mon_result = error_num;
9964 pthread_mutex_unlock(&table_mon_list->monitor_mutex);
9965 if (error_num == ER_CON_COUNT_ERROR)
9966 {
9967 my_error(ER_CON_COUNT_ERROR, MYF(0));
9968 DBUG_RETURN(ER_CON_COUNT_ERROR);
9969 }
9970 my_error(ER_CONNECT_TO_FOREIGN_DATA_SOURCE, MYF(0),
9971 share->server_names[0]);
9972 DBUG_RETURN(ER_CONNECT_TO_FOREIGN_DATA_SOURCE);
9973 }
9974 conn->mta_conn_mutex_lock_already = FALSE;
9975 conn->mta_conn_mutex_unlock_later = FALSE;
9976 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
9977 pthread_mutex_unlock(&conn->mta_conn_mutex);
9978 if (!ping_only)
9979 {
9980 int init_sql_alloc_size =
9981 spider_param_init_sql_alloc_size(trx->thd, share->init_sql_alloc_size);
9982 char *sql_buf = (char *) my_alloca(init_sql_alloc_size * 2);
9983 if (!sql_buf)
9984 {
9985 table_mon_list->last_mon_result = HA_ERR_OUT_OF_MEM;
9986 pthread_mutex_unlock(&table_mon_list->monitor_mutex);
9987 my_error(HA_ERR_OUT_OF_MEM, MYF(0));
9988 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9989 }
9990 char *where_buf = sql_buf + init_sql_alloc_size;
9991 spider_string sql_str(sql_buf, sizeof(sql_buf),
9992 system_charset_info);
9993 spider_string where_str(where_buf, sizeof(where_buf),
9994 system_charset_info);
9995 sql_str.init_calc_mem(128);
9996 where_str.init_calc_mem(129);
9997 sql_str.length(0);
9998 where_str.length(0);
9999 if (
10000 use_where &&
10001 where_str.append(where_clause, where_clause_length,
10002 trx->thd->variables.character_set_client)
10003 ) {
10004 table_mon_list->last_mon_result = HA_ERR_OUT_OF_MEM;
10005 pthread_mutex_unlock(&table_mon_list->monitor_mutex);
10006 my_error(HA_ERR_OUT_OF_MEM, MYF(0));
10007 my_afree(sql_buf);
10008 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10009 }
10010 share->access_charset = system_charset_info;
10011 if ((error_num = spider_db_udf_ping_table_append_select(&sql_str,
10012 share, trx, &where_str, use_where, limit, conn->dbton_id)))
10013 {
10014 table_mon_list->last_mon_result = error_num;
10015 pthread_mutex_unlock(&table_mon_list->monitor_mutex);
10016 my_error(error_num, MYF(0));
10017 my_afree(sql_buf);
10018 DBUG_RETURN(error_num);
10019 }
10020 pthread_mutex_lock(&conn->mta_conn_mutex);
10021 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
10022 conn->need_mon = &need_mon;
10023 conn->mta_conn_mutex_lock_already = TRUE;
10024 conn->mta_conn_mutex_unlock_later = TRUE;
10025 if ((error_num = spider_db_set_names(&spider, conn, 0)))
10026 {
10027 conn->mta_conn_mutex_lock_already = FALSE;
10028 conn->mta_conn_mutex_unlock_later = FALSE;
10029 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
10030 pthread_mutex_unlock(&conn->mta_conn_mutex);
10031 table_mon_list->last_mon_result = error_num;
10032 pthread_mutex_unlock(&table_mon_list->monitor_mutex);
10033 DBUG_PRINT("info",("spider error_num=%d", error_num));
10034 my_afree(sql_buf);
10035 DBUG_RETURN(error_num);
10036 }
10037 spider_conn_set_timeout_from_share(conn, 0, trx->thd, share);
10038 if (spider_db_query(
10039 conn,
10040 sql_str.ptr(),
10041 sql_str.length(),
10042 -1,
10043 &need_mon)
10044 ) {
10045 conn->mta_conn_mutex_lock_already = FALSE;
10046 conn->mta_conn_mutex_unlock_later = FALSE;
10047 error_num = spider_db_errorno(conn);
10048 table_mon_list->last_mon_result = error_num;
10049 pthread_mutex_unlock(&table_mon_list->monitor_mutex);
10050 DBUG_PRINT("info",("spider error_num=%d", error_num));
10051 my_afree(sql_buf);
10052 DBUG_RETURN(error_num);
10053 }
10054 conn->mta_conn_mutex_lock_already = FALSE;
10055 conn->mta_conn_mutex_unlock_later = FALSE;
10056 spider_db_discard_result(&spider, 0, conn);
10057 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
10058 pthread_mutex_unlock(&conn->mta_conn_mutex);
10059 my_afree(sql_buf);
10060 }
10061 table_mon_list->last_mon_result = 0;
10062 pthread_mutex_unlock(&table_mon_list->monitor_mutex);
10063 } else {
10064 pthread_mutex_lock(&table_mon_list->monitor_mutex);
10065 error_num = table_mon_list->last_mon_result;
10066 pthread_mutex_unlock(&table_mon_list->monitor_mutex);
10067 DBUG_RETURN(error_num);
10068 }
10069
10070 DBUG_RETURN(0);
10071}
10072
10073int spider_db_udf_ping_table_append_mon_next(
10074 spider_string *str,
10075 char *child_table_name,
10076 uint child_table_name_length,
10077 int link_id,
10078 char *static_link_id,
10079 uint static_link_id_length,
10080 char *where_clause,
10081 uint where_clause_length,
10082 longlong first_sid,
10083 int full_mon_count,
10084 int current_mon_count,
10085 int success_count,
10086 int fault_count,
10087 int flags,
10088 longlong limit
10089) {
10090 char limit_str[SPIDER_SQL_INT_LEN], sid_str[SPIDER_SQL_INT_LEN];
10091 int limit_str_length, sid_str_length;
10092 spider_string child_table_name_str(child_table_name,
10093 child_table_name_length + 1, str->charset());
10094 spider_string where_clause_str(where_clause ? where_clause : "",
10095 where_clause_length + 1, str->charset());
10096 DBUG_ENTER("spider_db_udf_ping_table_append_mon_next");
10097 child_table_name_str.init_calc_mem(130);
10098 where_clause_str.init_calc_mem(131);
10099 child_table_name_str.length(child_table_name_length);
10100 where_clause_str.length(where_clause_length);
10101 limit_str_length = my_sprintf(limit_str, (limit_str, "%lld", limit));
10102 sid_str_length = my_sprintf(sid_str, (sid_str, "%lld", first_sid));
10103 if (str->reserve(
10104 SPIDER_SQL_SELECT_LEN +
10105 SPIDER_SQL_PING_TABLE_LEN +
10106 (child_table_name_length * 2) +
10107 (
10108 static_link_id ?
10109 (SPIDER_SQL_INT_LEN * 5) +
10110 (SPIDER_SQL_VALUE_QUOTE_LEN * 2) +
10111 (static_link_id_length * 2) :
10112 (SPIDER_SQL_INT_LEN * 6)
10113 ) +
10114 sid_str_length +
10115 limit_str_length +
10116 (where_clause_length * 2) +
10117 (SPIDER_SQL_VALUE_QUOTE_LEN * 4) +
10118 (SPIDER_SQL_COMMA_LEN * 9) +
10119 SPIDER_SQL_CLOSE_PAREN_LEN
10120 ))
10121 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10122 str->q_append(SPIDER_SQL_SELECT_STR, SPIDER_SQL_SELECT_LEN);
10123 str->q_append(SPIDER_SQL_PING_TABLE_STR, SPIDER_SQL_PING_TABLE_LEN);
10124 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
10125 str->append_escape_string(child_table_name_str.ptr(), child_table_name_str.length());
10126 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
10127 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
10128 if (static_link_id)
10129 {
10130 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
10131 str->append_for_single_quote(static_link_id, static_link_id_length);
10132 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
10133 } else {
10134 str->qs_append(link_id);
10135 }
10136 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
10137 str->qs_append(flags);
10138 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
10139 str->q_append(limit_str, limit_str_length);
10140 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
10141 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
10142 str->append_escape_string(where_clause_str.ptr(), where_clause_str.length());
10143 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
10144 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
10145 str->q_append(sid_str, sid_str_length);
10146 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
10147 str->qs_append(full_mon_count);
10148 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
10149 str->qs_append(current_mon_count);
10150 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
10151 str->qs_append(success_count);
10152 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
10153 str->qs_append(fault_count);
10154 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
10155 DBUG_RETURN(0);
10156}
10157
10158int spider_db_udf_ping_table_append_select(
10159 spider_string *str,
10160 SPIDER_SHARE *share,
10161 SPIDER_TRX *trx,
10162 spider_string *where_str,
10163 bool use_where,
10164 longlong limit,
10165 uint dbton_id
10166) {
10167 int error_num;
10168 char limit_str[SPIDER_SQL_INT_LEN];
10169 int limit_str_length;
10170 DBUG_ENTER("spider_db_udf_ping_table_append_select");
10171 if (str->reserve(SPIDER_SQL_SELECT_LEN + SPIDER_SQL_ONE_LEN +
10172 SPIDER_SQL_FROM_LEN))
10173 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10174 str->q_append(SPIDER_SQL_SELECT_STR, SPIDER_SQL_SELECT_LEN);
10175 str->q_append(SPIDER_SQL_ONE_STR, SPIDER_SQL_ONE_LEN);
10176 str->q_append(SPIDER_SQL_FROM_STR, SPIDER_SQL_FROM_LEN);
10177 if ((error_num = spider_db_append_name_with_quote_str(str,
10178 share->tgt_dbs[0], dbton_id)))
10179 DBUG_RETURN(error_num);
10180 if (str->reserve(SPIDER_SQL_DOT_LEN))
10181 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10182 str->q_append(SPIDER_SQL_DOT_STR, SPIDER_SQL_DOT_LEN);
10183 if ((error_num = spider_db_append_name_with_quote_str(str,
10184 share->tgt_table_names[0], share->sql_dbton_ids[0])))
10185 DBUG_RETURN(error_num);
10186
10187 limit_str_length = my_sprintf(limit_str, (limit_str, "%lld", limit));
10188 if (str->reserve(
10189 (use_where ? (where_str->length() * 2) : 0) +
10190 SPIDER_SQL_LIMIT_LEN + limit_str_length
10191 ))
10192 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10193 if (use_where)
10194 {
10195 str->append_escape_string(where_str->ptr(), where_str->length());
10196 }
10197 str->q_append(SPIDER_SQL_LIMIT_STR, SPIDER_SQL_LIMIT_LEN);
10198 str->q_append(limit_str, limit_str_length);
10199 DBUG_RETURN(0);
10200}
10201
10202int spider_db_udf_ping_table_mon_next(
10203 THD *thd,
10204 SPIDER_TABLE_MON *table_mon,
10205 SPIDER_CONN *conn,
10206 SPIDER_MON_TABLE_RESULT *mon_table_result,
10207 char *child_table_name,
10208 uint child_table_name_length,
10209 int link_id,
10210 char *where_clause,
10211 uint where_clause_length,
10212 longlong first_sid,
10213 int full_mon_count,
10214 int current_mon_count,
10215 int success_count,
10216 int fault_count,
10217 int flags,
10218 longlong limit
10219) {
10220 int error_num, need_mon = 0;
10221 uint tmp_conn_link_idx = 0;
10222 SPIDER_DB_RESULT *res;
10223 SPIDER_SHARE *share = table_mon->share;
10224 int init_sql_alloc_size =
10225 spider_param_init_sql_alloc_size(thd, share->init_sql_alloc_size);
10226 ha_spider spider;
10227 SPIDER_TRX trx;
10228 DBUG_ENTER("spider_db_udf_ping_table_mon_next");
10229 char *sql_buf = (char *) my_alloca(init_sql_alloc_size);
10230 if (!sql_buf)
10231 {
10232 my_error(HA_ERR_OUT_OF_MEM, MYF(0));
10233 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10234 }
10235 spider_string sql_str(sql_buf, sizeof(sql_buf),
10236 thd->variables.character_set_client);
10237 sql_str.init_calc_mem(132);
10238 sql_str.length(0);
10239 trx.thd = thd;
10240 spider.share = share;
10241 spider.trx = &trx;
10242 spider.need_mons = &need_mon;
10243 spider.conn_link_idx = &tmp_conn_link_idx;
10244
10245 share->access_charset = thd->variables.character_set_client;
10246 if ((error_num = spider_db_udf_ping_table_append_mon_next(&sql_str,
10247 child_table_name, child_table_name_length, link_id,
10248 table_mon->parent->share->static_link_ids[0],
10249 table_mon->parent->share->static_link_ids_lengths[0],
10250 where_clause,
10251 where_clause_length, first_sid, full_mon_count, current_mon_count,
10252 success_count, fault_count, flags, limit)))
10253 {
10254 my_error(error_num, MYF(0));
10255 my_afree(sql_buf);
10256 DBUG_RETURN(error_num);
10257 }
10258
10259 pthread_mutex_lock(&conn->mta_conn_mutex);
10260 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
10261 conn->need_mon = &need_mon;
10262 conn->mta_conn_mutex_lock_already = TRUE;
10263 conn->mta_conn_mutex_unlock_later = TRUE;
10264 if ((error_num = spider_db_ping(&spider, conn, 0)))
10265 {
10266 conn->mta_conn_mutex_lock_already = FALSE;
10267 conn->mta_conn_mutex_unlock_later = FALSE;
10268 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
10269 pthread_mutex_unlock(&conn->mta_conn_mutex);
10270 my_error(ER_CONNECT_TO_FOREIGN_DATA_SOURCE, MYF(0),
10271 share->server_names[0]);
10272 my_afree(sql_buf);
10273 DBUG_RETURN(ER_CONNECT_TO_FOREIGN_DATA_SOURCE);
10274 }
10275 if ((error_num = spider_db_set_names(&spider, conn, 0)))
10276 {
10277 conn->mta_conn_mutex_lock_already = FALSE;
10278 conn->mta_conn_mutex_unlock_later = FALSE;
10279 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
10280 pthread_mutex_unlock(&conn->mta_conn_mutex);
10281 my_afree(sql_buf);
10282 DBUG_RETURN(error_num);
10283 }
10284 spider_conn_set_timeout_from_share(conn, 0, thd, share);
10285 if (spider_db_query(
10286 conn,
10287 sql_str.ptr(),
10288 sql_str.length(),
10289 -1,
10290 &need_mon)
10291 ) {
10292 conn->mta_conn_mutex_lock_already = FALSE;
10293 conn->mta_conn_mutex_unlock_later = FALSE;
10294 my_afree(sql_buf);
10295 DBUG_RETURN(spider_db_errorno(conn));
10296 }
10297 st_spider_db_request_key request_key;
10298 request_key.spider_thread_id = trx.spider_thread_id;
10299 request_key.query_id = trx.thd->query_id;
10300 request_key.handler = table_mon;
10301 request_key.request_id = 1;
10302 request_key.next = NULL;
10303 if (!(res = conn->db_conn->store_result(NULL, &request_key, &error_num)))
10304 {
10305 conn->mta_conn_mutex_lock_already = FALSE;
10306 conn->mta_conn_mutex_unlock_later = FALSE;
10307 if (error_num || (error_num = spider_db_errorno(conn)))
10308 {
10309 my_afree(sql_buf);
10310 DBUG_RETURN(error_num);
10311 }
10312 my_error(HA_ERR_OUT_OF_MEM, MYF(0));
10313 my_afree(sql_buf);
10314 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10315 }
10316 conn->mta_conn_mutex_lock_already = FALSE;
10317 conn->mta_conn_mutex_unlock_later = FALSE;
10318 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
10319 pthread_mutex_unlock(&conn->mta_conn_mutex);
10320 my_afree(sql_buf);
10321 error_num = res->fetch_table_mon_status(mon_table_result->result_status);
10322 res->free_result();
10323 delete res;
10324 DBUG_RETURN(error_num);
10325}
10326
10327int spider_db_udf_copy_key_row(
10328 spider_string *str,
10329 spider_string *source_str,
10330 Field *field,
10331 ulong *row_pos,
10332 ulong *length,
10333 const char *joint_str,
10334 const int joint_length,
10335 uint dbton_id
10336) {
10337 int error_num;
10338 DBUG_ENTER("spider_db_udf_copy_key_row");
10339 if ((error_num = spider_db_append_name_with_quote_str(str,
10340 (char *) field->field_name.str, dbton_id)))
10341 DBUG_RETURN(error_num);
10342 if (str->reserve(joint_length + *length + SPIDER_SQL_AND_LEN))
10343 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10344 str->q_append(joint_str, joint_length);
10345 str->q_append(source_str->ptr() + *row_pos, *length);
10346 str->q_append(SPIDER_SQL_AND_STR, SPIDER_SQL_AND_LEN);
10347 DBUG_RETURN(0);
10348}
10349
10350int spider_db_udf_copy_tables(
10351 SPIDER_COPY_TABLES *copy_tables,
10352 ha_spider *spider,
10353 TABLE *table,
10354 longlong bulk_insert_rows
10355) {
10356 int error_num = 0, roop_count;
10357 bool end_of_file = FALSE;
10358 ulong *last_lengths, *last_row_pos = NULL;
10359 ha_spider *tmp_spider;
10360 SPIDER_CONN *tmp_conn;
10361 int all_link_cnt =
10362 copy_tables->link_idx_count[0] + copy_tables->link_idx_count[1];
10363 SPIDER_COPY_TABLE_CONN *src_tbl_conn = copy_tables->table_conn[0];
10364 SPIDER_COPY_TABLE_CONN *dst_tbl_conn;
10365 spider_db_copy_table *select_ct = src_tbl_conn->copy_table;
10366 spider_db_copy_table *insert_ct = NULL;
10367 KEY *key_info = &table->key_info[table->s->primary_key];
10368 int bulk_insert_interval;
10369 DBUG_ENTER("spider_db_udf_copy_tables");
10370 if (!(last_row_pos = (ulong *)
10371 spider_bulk_malloc(spider_current_trx, 30, MYF(MY_WME),
10372 &last_row_pos, sizeof(ulong) * table->s->fields,
10373 &last_lengths, sizeof(ulong) * table->s->fields,
10374 NullS))
10375 ) {
10376 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
10377 goto error;
10378 }
10379 while (!end_of_file)
10380 {
10381 if (copy_tables->trx->thd->killed)
10382 {
10383 my_error(ER_QUERY_INTERRUPTED, MYF(0));
10384 error_num = ER_QUERY_INTERRUPTED;
10385 goto error_killed;
10386 }
10387 if (copy_tables->use_transaction)
10388 {
10389 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
10390 {
10391 tmp_spider = &spider[roop_count];
10392 tmp_conn = tmp_spider->conns[0];
10393 /* disable transaction */
10394 spider_conn_clear_queue_at_commit(tmp_conn);
10395 if (!tmp_conn->trx_start)
10396 {
10397 if (spider_db_ping(tmp_spider, tmp_conn, 0))
10398 {
10399 my_error(ER_CONNECT_TO_FOREIGN_DATA_SOURCE, MYF(0),
10400 tmp_spider->share->server_names[0]);
10401 error_num = ER_CONNECT_TO_FOREIGN_DATA_SOURCE;
10402 goto error_db_ping;
10403 }
10404 if (
10405 (error_num = spider_db_set_names(tmp_spider, tmp_conn, 0)) ||
10406 (error_num = spider_db_start_transaction(tmp_conn,
10407 tmp_spider->need_mons))
10408 )
10409 goto error_start_transaction;
10410 }
10411 }
10412 } else {
10413 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
10414 {
10415 tmp_spider = &spider[roop_count];
10416 tmp_conn = tmp_spider->conns[0];
10417 /* disable transaction */
10418 spider_conn_clear_queue_at_commit(tmp_conn);
10419 spider_db_handler *tmp_dbton_hdl =
10420 tmp_spider->dbton_handler[tmp_conn->dbton_id];
10421 if ((error_num = tmp_dbton_hdl->insert_lock_tables_list(tmp_conn, 0)))
10422 goto error_lock_table_hash;
10423 tmp_conn->table_lock = 2;
10424 }
10425 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
10426 {
10427 tmp_spider = &spider[roop_count];
10428 tmp_conn = tmp_spider->conns[0];
10429 if (spider_db_ping(tmp_spider, tmp_conn, 0))
10430 {
10431 my_error(ER_CONNECT_TO_FOREIGN_DATA_SOURCE, MYF(0),
10432 tmp_spider->share->server_names[0]);
10433 error_num = ER_CONNECT_TO_FOREIGN_DATA_SOURCE;
10434 goto error_db_ping;
10435 }
10436 if (
10437 tmp_conn->db_conn->have_lock_table_list() &&
10438 (
10439 (error_num = spider_db_set_names(tmp_spider, tmp_conn, 0)) ||
10440 (error_num = spider_db_lock_tables(tmp_spider, 0))
10441 )
10442 ) {
10443 tmp_conn->table_lock = 0;
10444 if (error_num == HA_ERR_OUT_OF_MEM)
10445 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
10446 goto error_lock_tables;
10447 }
10448 tmp_conn->table_lock = 1;
10449 }
10450 }
10451
10452 tmp_conn = src_tbl_conn->conn;
10453 spider_conn_set_timeout_from_share(tmp_conn, 0,
10454 copy_tables->trx->thd, src_tbl_conn->share);
10455 if (select_ct->exec_query(
10456 tmp_conn,
10457 -1,
10458 &src_tbl_conn->need_mon)
10459 ) {
10460 error_num = spider_db_errorno(tmp_conn);
10461 if (error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM)
10462 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
10463 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
10464 goto error_db_query;
10465 } else {
10466 SPIDER_DB_RESULT *result;
10467 st_spider_db_request_key request_key;
10468 request_key.spider_thread_id = copy_tables->trx->spider_thread_id;
10469 request_key.query_id = copy_tables->trx->thd->query_id;
10470 request_key.handler = copy_tables;
10471 request_key.request_id = 1;
10472 request_key.next = NULL;
10473 if ((result = tmp_conn->db_conn->use_result(&request_key, &error_num)))
10474 {
10475 SPIDER_DB_ROW *row;
10476 roop_count = 0;
10477 while ((row = result->fetch_row()))
10478 {
10479 dst_tbl_conn = copy_tables->table_conn[1];
10480 insert_ct = dst_tbl_conn->copy_table;
10481 if ((error_num = insert_ct->copy_rows(table, row,
10482 &last_row_pos, &last_lengths)))
10483 {
10484 if (error_num == HA_ERR_OUT_OF_MEM)
10485 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
10486 result->free_result();
10487 delete result;
10488 SPIDER_CLEAR_FILE_POS(&tmp_conn->mta_conn_mutex_file_pos);
10489 pthread_mutex_unlock(&tmp_conn->mta_conn_mutex);
10490 goto error_db_query;
10491 }
10492 for (dst_tbl_conn = dst_tbl_conn->next; dst_tbl_conn;
10493 dst_tbl_conn = dst_tbl_conn->next)
10494 {
10495 row->first();
10496 insert_ct = dst_tbl_conn->copy_table;
10497 if ((error_num = insert_ct->copy_rows(table, row)))
10498 {
10499 if (error_num == HA_ERR_OUT_OF_MEM)
10500 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
10501 result->free_result();
10502 delete result;
10503 SPIDER_CLEAR_FILE_POS(&tmp_conn->mta_conn_mutex_file_pos);
10504 pthread_mutex_unlock(&tmp_conn->mta_conn_mutex);
10505 goto error_db_query;
10506 }
10507 }
10508 ++roop_count;
10509 }
10510 error_num = result->get_errno();
10511 if (error_num == HA_ERR_END_OF_FILE)
10512 {
10513 if (roop_count < copy_tables->bulk_insert_rows)
10514 {
10515 end_of_file = TRUE;
10516 if (roop_count)
10517 error_num = 0;
10518 } else {
10519 /* add next where clause */
10520 select_ct->set_sql_to_pos();
10521 error_num = select_ct->append_copy_where(insert_ct, key_info,
10522 last_row_pos, last_lengths);
10523 if (error_num)
10524 {
10525 if (error_num == HA_ERR_OUT_OF_MEM)
10526 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
10527 result->free_result();
10528 delete result;
10529 SPIDER_CLEAR_FILE_POS(&tmp_conn->mta_conn_mutex_file_pos);
10530 pthread_mutex_unlock(&tmp_conn->mta_conn_mutex);
10531 goto error_db_query;
10532 }
10533 bulk_insert_rows = spider_param_udf_ct_bulk_insert_rows(
10534 copy_tables->bulk_insert_rows);
10535 if (
10536 select_ct->append_key_order_str(key_info, 0, FALSE) ||
10537 select_ct->append_limit(0, bulk_insert_rows) ||
10538 (
10539 copy_tables->use_transaction &&
10540 select_ct->append_select_lock_str(SPIDER_LOCK_MODE_SHARED)
10541 )
10542 ) {
10543 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
10544 result->free_result();
10545 delete result;
10546 SPIDER_CLEAR_FILE_POS(&tmp_conn->mta_conn_mutex_file_pos);
10547 pthread_mutex_unlock(&tmp_conn->mta_conn_mutex);
10548 error_num = ER_OUT_OF_RESOURCES;
10549 goto error_db_query;
10550 }
10551 error_num = 0;
10552 }
10553 } else {
10554 if (error_num == HA_ERR_OUT_OF_MEM)
10555 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
10556 result->free_result();
10557 delete result;
10558 SPIDER_CLEAR_FILE_POS(&tmp_conn->mta_conn_mutex_file_pos);
10559 pthread_mutex_unlock(&tmp_conn->mta_conn_mutex);
10560 goto error_db_query;
10561 }
10562 result->free_result();
10563 delete result;
10564 SPIDER_CLEAR_FILE_POS(&tmp_conn->mta_conn_mutex_file_pos);
10565 pthread_mutex_unlock(&tmp_conn->mta_conn_mutex);
10566 for (dst_tbl_conn = copy_tables->table_conn[1]; dst_tbl_conn;
10567 dst_tbl_conn = dst_tbl_conn->next)
10568 {
10569 insert_ct = dst_tbl_conn->copy_table;
10570 if ((error_num = insert_ct->append_insert_terminator()))
10571 {
10572 if (error_num == HA_ERR_OUT_OF_MEM)
10573 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
10574 goto error_db_query;
10575 }
10576 }
10577 } else {
10578 if (!error_num)
10579 {
10580 error_num = spider_db_errorno(tmp_conn);
10581 }
10582 if (error_num)
10583 {
10584 if (error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM)
10585 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
10586 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
10587 goto error_db_query;
10588 }
10589 error_num = HA_ERR_END_OF_FILE;
10590 end_of_file = TRUE;
10591 SPIDER_CLEAR_FILE_POS(&tmp_conn->mta_conn_mutex_file_pos);
10592 pthread_mutex_unlock(&tmp_conn->mta_conn_mutex);
10593 }
10594 }
10595
10596 if (!error_num && roop_count)
10597 {
10598/*
10599 dst_tbl_conn = copy_tables->table_conn[1];
10600 spider_db_copy_table *source_ct = dst_tbl_conn->copy_table;
10601 for (dst_tbl_conn = dst_tbl_conn->next; dst_tbl_conn;
10602 dst_tbl_conn = dst_tbl_conn->next)
10603 {
10604 insert_ct = dst_tbl_conn->copy_table;
10605 if (insert_ct->copy_insert_values(source_ct))
10606 {
10607 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
10608 error_num = ER_OUT_OF_RESOURCES;
10609 goto error_db_query;
10610 }
10611 }
10612*/
10613#ifndef WITHOUT_SPIDER_BG_SEARCH
10614 if (copy_tables->bg_mode)
10615 {
10616 for (dst_tbl_conn = copy_tables->table_conn[1]; dst_tbl_conn;
10617 dst_tbl_conn = dst_tbl_conn->next)
10618 {
10619 if (spider_udf_bg_copy_exec_sql(dst_tbl_conn))
10620 {
10621 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
10622 error_num = ER_OUT_OF_RESOURCES;
10623 goto error_db_query;
10624 }
10625 }
10626 } else {
10627#endif
10628 for (dst_tbl_conn = copy_tables->table_conn[1]; dst_tbl_conn;
10629 dst_tbl_conn = dst_tbl_conn->next)
10630 {
10631 tmp_conn = dst_tbl_conn->conn;
10632 insert_ct = dst_tbl_conn->copy_table;
10633 pthread_mutex_lock(&tmp_conn->mta_conn_mutex);
10634 SPIDER_SET_FILE_POS(&tmp_conn->mta_conn_mutex_file_pos);
10635 tmp_conn->need_mon = &dst_tbl_conn->need_mon;
10636 tmp_conn->mta_conn_mutex_lock_already = TRUE;
10637 tmp_conn->mta_conn_mutex_unlock_later = TRUE;
10638 spider_conn_set_timeout_from_share(tmp_conn, 0,
10639 copy_tables->trx->thd, dst_tbl_conn->share);
10640 if (insert_ct->exec_query(
10641 tmp_conn,
10642 -1,
10643 &dst_tbl_conn->need_mon)
10644 ) {
10645 tmp_conn->mta_conn_mutex_lock_already = FALSE;
10646 tmp_conn->mta_conn_mutex_unlock_later = FALSE;
10647 error_num = spider_db_errorno(tmp_conn);
10648 if (error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM)
10649 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
10650 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
10651 goto error_db_query;
10652 } else {
10653 tmp_conn->mta_conn_mutex_lock_already = FALSE;
10654 tmp_conn->mta_conn_mutex_unlock_later = FALSE;
10655 SPIDER_CLEAR_FILE_POS(&tmp_conn->mta_conn_mutex_file_pos);
10656 pthread_mutex_unlock(&tmp_conn->mta_conn_mutex);
10657 }
10658 }
10659#ifndef WITHOUT_SPIDER_BG_SEARCH
10660 }
10661#endif
10662
10663#ifndef WITHOUT_SPIDER_BG_SEARCH
10664 if (copy_tables->bg_mode)
10665 {
10666 for (dst_tbl_conn = copy_tables->table_conn[1]; dst_tbl_conn;
10667 dst_tbl_conn = dst_tbl_conn->next)
10668 {
10669 tmp_conn = dst_tbl_conn->conn;
10670 if (tmp_conn->bg_exec_sql)
10671 {
10672 /* wait */
10673 pthread_mutex_lock(&tmp_conn->bg_conn_mutex);
10674 pthread_mutex_unlock(&tmp_conn->bg_conn_mutex);
10675 }
10676
10677 if (dst_tbl_conn->bg_error_num)
10678 {
10679 if (error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM)
10680 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
10681 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
10682 goto error_db_query;
10683 }
10684 }
10685 }
10686#endif
10687 }
10688
10689 if (copy_tables->use_transaction)
10690 {
10691 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
10692 {
10693 tmp_spider = &spider[roop_count];
10694 tmp_conn = tmp_spider->conns[0];
10695 if (tmp_conn->trx_start)
10696 {
10697 if ((error_num = spider_db_commit(tmp_conn)))
10698 goto error_commit;
10699 }
10700 }
10701 } else {
10702 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
10703 {
10704 tmp_spider = &spider[roop_count];
10705 tmp_conn = tmp_spider->conns[0];
10706 if (tmp_conn->table_lock == 1)
10707 {
10708 tmp_conn->table_lock = 0;
10709 if ((error_num = spider_db_unlock_tables(tmp_spider, 0)))
10710 goto error_unlock_tables;
10711 }
10712 }
10713 }
10714 if (!end_of_file)
10715 {
10716 for (dst_tbl_conn = copy_tables->table_conn[1]; dst_tbl_conn;
10717 dst_tbl_conn = dst_tbl_conn->next)
10718 {
10719 insert_ct = dst_tbl_conn->copy_table;
10720 insert_ct->set_sql_to_pos();
10721 }
10722 DBUG_PRINT("info",("spider sleep"));
10723 bulk_insert_interval = spider_param_udf_ct_bulk_insert_interval(
10724 copy_tables->bulk_insert_interval);
10725 my_sleep(bulk_insert_interval);
10726 }
10727 }
10728 spider_free(spider_current_trx, last_row_pos, MYF(0));
10729 DBUG_RETURN(0);
10730
10731error_db_query:
10732#ifndef WITHOUT_SPIDER_BG_SEARCH
10733 if (copy_tables->bg_mode)
10734 {
10735 for (dst_tbl_conn = copy_tables->table_conn[1]; dst_tbl_conn;
10736 dst_tbl_conn = dst_tbl_conn->next)
10737 {
10738 tmp_conn = dst_tbl_conn->conn;
10739 if (tmp_conn->bg_exec_sql)
10740 {
10741 /* wait */
10742 pthread_mutex_lock(&tmp_conn->bg_conn_mutex);
10743 pthread_mutex_unlock(&tmp_conn->bg_conn_mutex);
10744 }
10745 }
10746 }
10747#endif
10748error_unlock_tables:
10749error_commit:
10750error_lock_tables:
10751error_lock_table_hash:
10752error_start_transaction:
10753error_db_ping:
10754error_killed:
10755 if (copy_tables->use_transaction)
10756 {
10757 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
10758 {
10759 tmp_spider = &spider[roop_count];
10760 tmp_conn = tmp_spider->conns[0];
10761 if (tmp_conn->trx_start)
10762 spider_db_rollback(tmp_conn);
10763 }
10764 } else {
10765 if (copy_tables->trx->locked_connections)
10766 {
10767 for (roop_count = 0; roop_count < all_link_cnt; roop_count++)
10768 {
10769 tmp_spider = &spider[roop_count];
10770 tmp_conn = tmp_spider->conns[0];
10771 if (tmp_conn->table_lock == 1)
10772 {
10773 tmp_conn->table_lock = 0;
10774 spider_db_unlock_tables(tmp_spider, 0);
10775 }
10776 }
10777 }
10778 }
10779error:
10780 if (last_row_pos)
10781 {
10782 spider_free(spider_current_trx, last_row_pos, MYF(0));
10783 }
10784 DBUG_RETURN(error_num);
10785}
10786
10787int spider_db_open_handler(
10788 ha_spider *spider,
10789 SPIDER_CONN *conn,
10790 int link_idx
10791) {
10792 int error_num;
10793 bool tmp_mta_conn_mutex_lock_already;
10794 bool tmp_mta_conn_mutex_unlock_later;
10795 SPIDER_SHARE *share = spider->share;
10796 uint *handler_id_ptr =
10797#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
10798 conn->conn_kind == SPIDER_CONN_KIND_MYSQL ?
10799#endif
10800 &spider->m_handler_id[link_idx]
10801#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
10802 : conn->conn_kind == SPIDER_CONN_KIND_HS_READ ?
10803 &spider->r_handler_id[link_idx] :
10804 &spider->w_handler_id[link_idx]
10805#endif
10806 ;
10807 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
10808 DBUG_ENTER("spider_db_open_handler");
10809 if (!conn->mta_conn_mutex_lock_already)
10810 {
10811 pthread_mutex_lock(&conn->mta_conn_mutex);
10812 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
10813 conn->need_mon = &spider->need_mons[link_idx];
10814 }
10815 DBUG_ASSERT(conn->mta_conn_mutex_file_pos.file_name);
10816 tmp_mta_conn_mutex_lock_already = conn->mta_conn_mutex_lock_already;
10817 conn->mta_conn_mutex_lock_already = TRUE;
10818 tmp_mta_conn_mutex_unlock_later = conn->mta_conn_mutex_unlock_later;
10819 conn->mta_conn_mutex_unlock_later = TRUE;
10820 if (!spider->handler_opened(link_idx, conn->conn_kind))
10821 *handler_id_ptr = conn->opened_handlers;
10822#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
10823 if (conn->conn_kind == SPIDER_CONN_KIND_MYSQL)
10824 {
10825#endif
10826 if (!spider->handler_opened(link_idx, conn->conn_kind))
10827 my_sprintf(spider->m_handler_cid[link_idx],
10828 (spider->m_handler_cid[link_idx], SPIDER_SQL_HANDLER_CID_FORMAT,
10829 *handler_id_ptr));
10830
10831 if ((error_num = dbton_hdl->append_open_handler_part(
10832 SPIDER_SQL_TYPE_HANDLER, *handler_id_ptr, conn, link_idx)))
10833 {
10834 goto error;
10835 }
10836
10837 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
10838 share);
10839 if (dbton_hdl->execute_sql(
10840 SPIDER_SQL_TYPE_HANDLER,
10841 conn,
10842 -1,
10843 &spider->need_mons[link_idx])
10844 ) {
10845 error_num = spider_db_errorno(conn);
10846 goto error;
10847 }
10848 dbton_hdl->reset_sql(SPIDER_SQL_TYPE_HANDLER);
10849#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
10850 } else {
10851 uint reconnect = 0;
10852 if (conn->hs_pre_age != conn->hs_age && conn->server_lost)
10853 {
10854 spider_conn_queue_connect(share, conn, link_idx);
10855 reconnect |= 1;
10856 }
10857 if ((error_num = spider_db_conn_queue_action(conn)))
10858 {
10859 goto error;
10860 }
10861 if (conn->hs_pre_age != conn->hs_age)
10862 {
10863 if (conn->db_conn->ping())
10864 {
10865 my_printf_error(ER_SPIDER_HS_NUM, ER_SPIDER_HS_STR, MYF(0),
10866 conn->db_conn->get_errno(), conn->db_conn->get_error());
10867 if (!conn->mta_conn_mutex_unlock_later)
10868 {
10869 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
10870 pthread_mutex_unlock(&conn->mta_conn_mutex);
10871 }
10872 spider->need_mons[link_idx] = ER_SPIDER_HS_NUM;
10873 error_num = ER_SPIDER_HS_NUM;
10874 goto error;
10875 }
10876 conn->opened_handlers = 0;
10877 conn->db_conn->reset_opened_handler();
10878 conn->hs_age = conn->hs_pre_age;
10879 reconnect |= 2;
10880 }
10881 if (conn->conn_kind == SPIDER_CONN_KIND_HS_READ)
10882 {
10883 if (spider->hs_r_conn_ages[link_idx] != conn->hs_age)
10884 {
10885 spider->clear_handler_opened(link_idx, SPIDER_CONN_KIND_HS_READ);
10886 *handler_id_ptr = conn->opened_handlers;
10887 }
10888 } else {
10889 if (spider->hs_w_conn_ages[link_idx] != conn->hs_age)
10890 {
10891 spider->clear_handler_opened(link_idx, SPIDER_CONN_KIND_HS_WRITE);
10892 *handler_id_ptr = conn->opened_handlers;
10893 }
10894 }
10895
10896#ifdef HA_CAN_BULK_ACCESS
10897 if (!spider->is_bulk_access_clone)
10898 {
10899#endif
10900 conn->db_conn->reset_request_queue();
10901#ifdef HA_CAN_BULK_ACCESS
10902 } else if (!spider->bulk_access_executing)
10903 {
10904 if (conn->conn_kind == SPIDER_CONN_KIND_HS_READ)
10905 {
10906 spider_set_bit(spider->result_list.hs_r_bulk_open_index, link_idx);
10907 } else {
10908 spider_set_bit(spider->result_list.hs_w_bulk_open_index, link_idx);
10909 }
10910 }
10911#endif
10912 if ((error_num = dbton_hdl->append_open_handler_part(
10913 SPIDER_SQL_TYPE_OTHER_HS, *handler_id_ptr, conn, link_idx)))
10914 {
10915 goto error;
10916 }
10917#ifdef HA_CAN_BULK_ACCESS
10918 if (spider->is_bulk_access_clone && !spider->bulk_access_executing)
10919 {
10920 spider->connection_ids[link_idx] = conn->connection_id;
10921 spider_trx_add_bulk_access_conn(spider->trx, conn);
10922 } else {
10923#endif
10924 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
10925 share);
10926 if (dbton_hdl->execute_sql(
10927 SPIDER_SQL_TYPE_SELECT_HS,
10928 conn,
10929 -1,
10930 &spider->need_mons[link_idx])
10931 ) {
10932 error_num = spider_db_errorno(conn);
10933 goto error;
10934 }
10935
10936 SPIDER_DB_RESULT *result;
10937 if (spider_bit_is_set(spider->db_request_phase, link_idx))
10938 {
10939 spider_clear_bit(spider->db_request_phase, link_idx);
10940 }
10941 st_spider_db_request_key request_key;
10942 request_key.spider_thread_id = spider->trx->spider_thread_id;
10943 request_key.query_id = spider->trx->thd->query_id;
10944 request_key.handler = spider;
10945 request_key.request_id = spider->db_request_id[link_idx];
10946 request_key.next = NULL;
10947 if (!(result = conn->db_conn->use_result(&request_key, &error_num)))
10948 {
10949 if (!error_num)
10950 {
10951 spider_db_errorno(conn);
10952 error_num = ER_SPIDER_HS_NUM;
10953 }
10954 goto error;
10955 } else {
10956 conn->ping_time = (time_t) time((time_t*) 0);
10957 }
10958 result->free_result();
10959 delete result;
10960#ifdef HA_CAN_BULK_ACCESS
10961 }
10962#endif
10963 if (conn->conn_kind == SPIDER_CONN_KIND_HS_READ)
10964 {
10965 spider->r_handler_index[link_idx] = spider->active_index;
10966 spider->hs_r_conn_ages[link_idx] = conn->hs_age;
10967#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
10968 if (
10969 spider->sql_command != SQLCOM_HS_INSERT &&
10970 spider->hs_pushed_ret_fields_num < MAX_FIELDS
10971 ) {
10972 spider->hs_r_ret_fields_num[link_idx] =
10973 spider->hs_pushed_ret_fields_num;
10974 memcpy(spider->hs_r_ret_fields[link_idx], spider->hs_pushed_ret_fields,
10975 sizeof(uint32) * spider->hs_pushed_ret_fields_num);
10976 } else {
10977 spider->hs_r_ret_fields_num[link_idx] = MAX_FIELDS;
10978 }
10979#endif
10980 } else {
10981 spider->w_handler_index[link_idx] = spider->active_index;
10982 spider->hs_w_conn_ages[link_idx] = conn->hs_age;
10983#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
10984 if (
10985 spider->sql_command != SQLCOM_HS_INSERT &&
10986 spider->hs_pushed_ret_fields_num < MAX_FIELDS
10987 ) {
10988 spider->hs_w_ret_fields_num[link_idx] =
10989 spider->hs_pushed_ret_fields_num;
10990 memcpy(spider->hs_w_ret_fields[link_idx], spider->hs_pushed_ret_fields,
10991 sizeof(uint32) * spider->hs_pushed_ret_fields_num);
10992 } else {
10993 spider->hs_w_ret_fields_num[link_idx] = MAX_FIELDS;
10994 }
10995#endif
10996 }
10997 }
10998#endif
10999 if (!spider->handler_opened(link_idx, conn->conn_kind))
11000 {
11001 if ((error_num = dbton_hdl->insert_opened_handler(conn, link_idx)))
11002 goto error;
11003 conn->opened_handlers++;
11004 }
11005 DBUG_PRINT("info",("spider conn=%p", conn));
11006 DBUG_PRINT("info",("spider opened_handlers=%u", conn->opened_handlers));
11007 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
11008 conn->mta_conn_mutex_unlock_later = tmp_mta_conn_mutex_unlock_later;
11009 if (!tmp_mta_conn_mutex_unlock_later)
11010 {
11011 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11012 pthread_mutex_unlock(&conn->mta_conn_mutex);
11013 }
11014 DBUG_RETURN(0);
11015
11016error:
11017 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
11018 conn->mta_conn_mutex_unlock_later = tmp_mta_conn_mutex_unlock_later;
11019 if (!tmp_mta_conn_mutex_unlock_later)
11020 {
11021 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11022 pthread_mutex_unlock(&conn->mta_conn_mutex);
11023 }
11024 DBUG_RETURN(error_num);
11025}
11026
11027#ifdef HA_CAN_BULK_ACCESS
11028int spider_db_bulk_open_handler(
11029 ha_spider *spider,
11030 SPIDER_CONN *conn,
11031 int link_idx
11032) {
11033 int error_num = 0;
11034#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
11035 bool opening_index = FALSE;
11036#endif
11037 DBUG_ENTER("spider_db_bulk_open_handler");
11038 DBUG_PRINT("info",("spider spider=%p", spider));
11039 DBUG_PRINT("info",("spider conn=%p", conn));
11040 DBUG_PRINT("info",("spider link_idx=%d", link_idx));
11041#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
11042 DBUG_ASSERT(conn->conn_kind != SPIDER_CONN_KIND_MYSQL);
11043 if (conn->conn_kind == SPIDER_CONN_KIND_HS_READ)
11044 {
11045 if (spider_bit_is_set(spider->result_list.hs_r_bulk_open_index, link_idx))
11046 {
11047 DBUG_PRINT("info",("spider SPIDER_CONN_KIND_HS_READ"));
11048 spider_clear_bit(spider->result_list.hs_r_bulk_open_index, link_idx);
11049 opening_index = TRUE;
11050 }
11051 } else {
11052 if (spider_bit_is_set(spider->result_list.hs_w_bulk_open_index, link_idx))
11053 {
11054 DBUG_PRINT("info",("spider SPIDER_CONN_KIND_HS_WRITE"));
11055 spider_clear_bit(spider->result_list.hs_w_bulk_open_index, link_idx);
11056 opening_index = TRUE;
11057 }
11058 }
11059 if (opening_index)
11060 {
11061 DBUG_PRINT("info",("spider conn->connection_id=%llu",
11062 conn->connection_id));
11063 DBUG_PRINT("info",("spider spider->connection_ids[%d]=%llu",
11064 link_idx, spider->connection_ids[link_idx]));
11065 if (conn->connection_id != spider->connection_ids[link_idx])
11066 {
11067 my_message(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM,
11068 ER_SPIDER_REMOTE_SERVER_GONE_AWAY_STR, MYF(0));
11069 DBUG_RETURN(ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM);
11070 }
11071
11072 bool tmp_mta_conn_mutex_unlock_later;
11073 tmp_mta_conn_mutex_unlock_later = conn->mta_conn_mutex_unlock_later;
11074 conn->mta_conn_mutex_unlock_later = TRUE;
11075 SPIDER_DB_RESULT *result;
11076 if (spider_bit_is_set(spider->db_request_phase, link_idx))
11077 {
11078 spider_clear_bit(spider->db_request_phase, link_idx);
11079 }
11080 st_spider_db_request_key request_key;
11081 request_key.spider_thread_id = spider->trx->spider_thread_id;
11082 request_key.query_id = spider->trx->thd->query_id;
11083 request_key.handler = spider;
11084 request_key.request_id = spider->db_request_id[link_idx];
11085 request_key.next = NULL;
11086 if (!(result = conn->db_conn->use_result(&request_key, &error_num)))
11087 {
11088 if (!error_num)
11089 {
11090 spider_db_errorno(conn);
11091 error_num = ER_SPIDER_HS_NUM;
11092 }
11093 } else {
11094 result->free_result();
11095 delete result;
11096 }
11097 conn->mta_conn_mutex_unlock_later = tmp_mta_conn_mutex_unlock_later;
11098 }
11099#endif
11100 DBUG_RETURN(error_num);
11101}
11102#endif
11103
11104int spider_db_close_handler(
11105 ha_spider *spider,
11106 SPIDER_CONN *conn,
11107 int link_idx,
11108 uint tgt_conn_kind
11109) {
11110 int error_num;
11111 bool tmp_mta_conn_mutex_lock_already;
11112 bool tmp_mta_conn_mutex_unlock_later;
11113 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
11114 DBUG_ENTER("spider_db_close_handler");
11115 DBUG_PRINT("info",("spider conn=%p", conn));
11116 if (spider->handler_opened(link_idx, tgt_conn_kind))
11117 {
11118 if (!conn->mta_conn_mutex_lock_already)
11119 {
11120 pthread_mutex_lock(&conn->mta_conn_mutex);
11121 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
11122 conn->need_mon = &spider->need_mons[link_idx];
11123 }
11124 DBUG_ASSERT(conn->mta_conn_mutex_file_pos.file_name);
11125 tmp_mta_conn_mutex_lock_already = conn->mta_conn_mutex_lock_already;
11126 conn->mta_conn_mutex_lock_already = TRUE;
11127 tmp_mta_conn_mutex_unlock_later = conn->mta_conn_mutex_unlock_later;
11128 conn->mta_conn_mutex_unlock_later = TRUE;
11129#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
11130 if (conn->conn_kind == SPIDER_CONN_KIND_MYSQL)
11131 {
11132#endif
11133 dbton_hdl->reset_sql(SPIDER_SQL_TYPE_HANDLER);
11134 if ((error_num = dbton_hdl->append_close_handler_part(
11135 SPIDER_SQL_TYPE_HANDLER, link_idx)))
11136 DBUG_RETURN(error_num);
11137
11138 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11139 spider->share);
11140 if (dbton_hdl->execute_sql(
11141 SPIDER_SQL_TYPE_HANDLER,
11142 conn,
11143 -1,
11144 &spider->need_mons[link_idx])
11145 ) {
11146 error_num = spider_db_errorno(conn);
11147 goto error;
11148 }
11149 dbton_hdl->reset_sql(SPIDER_SQL_TYPE_HANDLER);
11150#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
11151 } else {
11152/*
11153 conn->hs_conn->close();
11154 conn->server_lost = TRUE;
11155*/
11156 }
11157#endif
11158 if ((error_num = dbton_hdl->delete_opened_handler(conn, link_idx)))
11159 goto error;
11160 conn->opened_handlers--;
11161 DBUG_PRINT("info",("spider opened_handlers=%u", conn->opened_handlers));
11162 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
11163 conn->mta_conn_mutex_unlock_later = tmp_mta_conn_mutex_unlock_later;
11164 if (!tmp_mta_conn_mutex_unlock_later)
11165 {
11166 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11167 pthread_mutex_unlock(&conn->mta_conn_mutex);
11168 }
11169 }
11170 DBUG_RETURN(0);
11171
11172error:
11173 conn->mta_conn_mutex_lock_already = tmp_mta_conn_mutex_lock_already;
11174 conn->mta_conn_mutex_unlock_later = tmp_mta_conn_mutex_unlock_later;
11175 if (!tmp_mta_conn_mutex_unlock_later)
11176 {
11177 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11178 pthread_mutex_unlock(&conn->mta_conn_mutex);
11179 }
11180 DBUG_RETURN(error_num);
11181}
11182
11183#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
11184void spider_db_hs_request_buf_reset(
11185 SPIDER_CONN *conn
11186) {
11187 DBUG_ENTER("spider_db_hs_request_buf_reset");
11188 if (conn->bulk_access_requests)
11189 {
11190 if (conn->db_conn->is_connected())
11191 {
11192 conn->db_conn->reset_request_queue();
11193 }
11194 conn->bulk_access_requests = 0;
11195 }
11196 DBUG_VOID_RETURN;
11197}
11198#endif
11199
11200bool spider_db_conn_is_network_error(
11201 int error_num
11202) {
11203 DBUG_ENTER("spider_db_conn_is_network_error");
11204 if (
11205 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM ||
11206 error_num == ER_CONNECT_TO_FOREIGN_DATA_SOURCE ||
11207 (
11208 error_num >= CR_MIN_ERROR &&
11209 error_num <= CR_MAX_ERROR
11210 )
11211 ) {
11212 DBUG_RETURN(TRUE);
11213 }
11214 DBUG_RETURN(FALSE);
11215}
11216