1/* Copyright (C) 2009-2017 Kentoku Shiba
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
15
16#define MYSQL_SERVER 1
17#include <my_global.h>
18#include "mysql_version.h"
19#include "spd_environ.h"
20#if MYSQL_VERSION_ID < 50500
21#include "mysql_priv.h"
22#include <mysql/plugin.h>
23#else
24#include "sql_priv.h"
25#include "probes_mysql.h"
26#include "sql_class.h"
27#include "sql_partition.h"
28#include "sql_base.h"
29#include "sql_servers.h"
30#endif
31#include "spd_err.h"
32#include "spd_param.h"
33#include "spd_db_include.h"
34#include "spd_include.h"
35#include "spd_sys_table.h"
36#include "ha_spider.h"
37#include "spd_db_conn.h"
38#include "spd_trx.h"
39#include "spd_conn.h"
40#include "spd_table.h"
41#include "spd_direct_sql.h"
42#include "spd_udf.h"
43#include "spd_malloc.h"
44
45#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100004
46#define SPIDER_NEED_INIT_ONE_TABLE_FOR_FIND_TEMPORARY_TABLE
47#endif
48
49extern const char **spd_defaults_extra_file;
50extern const char **spd_defaults_file;
51
52extern handlerton *spider_hton_ptr;
53extern SPIDER_DBTON spider_dbton[SPIDER_DBTON_SIZE];
54
55#ifdef HAVE_PSI_INTERFACE
56extern PSI_mutex_key spd_key_mutex_mta_conn;
57extern PSI_mutex_key spd_key_mutex_bg_direct_sql;
58extern PSI_cond_key spd_key_cond_bg_direct_sql;
59#endif
60
61extern HASH spider_open_connections;
62extern HASH spider_ipport_conns;
63extern pthread_mutex_t spider_conn_mutex;
64extern pthread_mutex_t spider_conn_id_mutex;
65extern pthread_mutex_t spider_ipport_conn_mutex;
66extern ulonglong spider_conn_id;
67
68uint spider_udf_calc_hash(
69 char *key,
70 uint mod
71) {
72 uint sum = 0;
73 DBUG_ENTER("spider_udf_calc_hash");
74 while (*key != '\0')
75 {
76 sum += *key;
77 key++;
78 }
79 DBUG_PRINT("info",("spider calc hash = %u", sum % mod));
80 DBUG_RETURN(sum % mod);
81}
82
83int spider_udf_direct_sql_create_table_list(
84 SPIDER_DIRECT_SQL *direct_sql,
85 char *table_name_list,
86 uint table_name_list_length
87) {
88 int table_count, roop_count, length;
89 char *tmp_ptr, *tmp_ptr2, *tmp_ptr3, *tmp_name_ptr;
90 THD *thd = direct_sql->trx->thd;
91 DBUG_ENTER("spider_udf_direct_sql_create_table_list");
92 tmp_ptr = table_name_list;
93 while (*tmp_ptr == ' ')
94 tmp_ptr++;
95 if (*tmp_ptr)
96 table_count = 1;
97 else {
98 direct_sql->table_count = 0;
99 DBUG_RETURN(0);
100 }
101
102 while (TRUE)
103 {
104 if ((tmp_ptr2 = strchr(tmp_ptr, ' ')))
105 {
106 table_count++;
107 tmp_ptr = tmp_ptr2 + 1;
108 while (*tmp_ptr == ' ')
109 tmp_ptr++;
110 } else
111 break;
112 }
113#if MYSQL_VERSION_ID < 50500
114 if (!(direct_sql->db_names = (char**)
115 spider_bulk_malloc(spider_current_trx, 31, MYF(MY_WME | MY_ZEROFILL),
116 &direct_sql->db_names, sizeof(char*) * table_count,
117 &direct_sql->table_names, sizeof(char*) * table_count,
118 &direct_sql->tables, sizeof(TABLE*) * table_count,
119 &tmp_name_ptr, sizeof(char) * (
120 table_name_list_length +
121 thd->db_length * table_count +
122 2 * table_count
123 ),
124 &direct_sql->iop, sizeof(int) * table_count,
125 NullS))
126 )
127#else
128 if (!(direct_sql->db_names = (char**)
129 spider_bulk_malloc(spider_current_trx, 31, MYF(MY_WME | MY_ZEROFILL),
130 &direct_sql->db_names, sizeof(char*) * table_count,
131 &direct_sql->table_names, sizeof(char*) * table_count,
132 &direct_sql->tables, sizeof(TABLE*) * table_count,
133 &tmp_name_ptr, sizeof(char) * (
134 table_name_list_length +
135 thd->db.length * table_count +
136 2 * table_count
137 ),
138 &direct_sql->iop, sizeof(int) * table_count,
139 &direct_sql->table_list, sizeof(TABLE_LIST) * table_count,
140 &direct_sql->real_table_bitmap, sizeof(uchar) * ((table_count + 7) / 8),
141 NullS))
142 )
143#endif
144 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
145
146 tmp_ptr = table_name_list;
147 while (*tmp_ptr == ' ')
148 tmp_ptr++;
149 roop_count = 0;
150 while (TRUE)
151 {
152 if ((tmp_ptr2 = strchr(tmp_ptr, ' ')))
153 *tmp_ptr2 = '\0';
154
155 direct_sql->db_names[roop_count] = tmp_name_ptr;
156
157 if ((tmp_ptr3 = strchr(tmp_ptr, '.')))
158 {
159 /* exist database name */
160 *tmp_ptr3 = '\0';
161 length = strlen(tmp_ptr);
162 memcpy(tmp_name_ptr, tmp_ptr, length + 1);
163 tmp_name_ptr += length + 1;
164 tmp_ptr = tmp_ptr3 + 1;
165 } else {
166 if (thd->db.str)
167 {
168 memcpy(tmp_name_ptr, thd->db.str,
169 thd->db.length + 1);
170 tmp_name_ptr += thd->db.length + 1;
171 } else {
172 direct_sql->db_names[roop_count] = (char *) "";
173 }
174 }
175
176 direct_sql->table_names[roop_count] = tmp_name_ptr;
177 length = strlen(tmp_ptr);
178 memcpy(tmp_name_ptr, tmp_ptr, length + 1);
179 tmp_name_ptr += length + 1;
180
181 DBUG_PRINT("info",("spider db=%s",
182 direct_sql->db_names[roop_count]));
183 DBUG_PRINT("info",("spider table_name=%s",
184 direct_sql->table_names[roop_count]));
185
186 if (!tmp_ptr2)
187 break;
188 tmp_ptr = tmp_ptr2 + 1;
189 while (*tmp_ptr == ' ')
190 tmp_ptr++;
191 roop_count++;
192 }
193 direct_sql->table_count = table_count;
194 DBUG_RETURN(0);
195}
196
197int spider_udf_direct_sql_create_conn_key(
198 SPIDER_DIRECT_SQL *direct_sql
199) {
200 char *tmp_name, port_str[6];
201 DBUG_ENTER("spider_udf_direct_sql_create_conn_key");
202
203 /* tgt_db not use */
204#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
205 if (direct_sql->access_mode == 0)
206 {
207#endif
208 direct_sql->conn_key_length
209 = 1
210 + direct_sql->tgt_wrapper_length + 1
211 + direct_sql->tgt_host_length + 1
212 + 5 + 1
213 + direct_sql->tgt_socket_length + 1
214 + direct_sql->tgt_username_length + 1
215 + direct_sql->tgt_password_length + 1
216 + direct_sql->tgt_ssl_ca_length + 1
217 + direct_sql->tgt_ssl_capath_length + 1
218 + direct_sql->tgt_ssl_cert_length + 1
219 + direct_sql->tgt_ssl_cipher_length + 1
220 + direct_sql->tgt_ssl_key_length + 1
221 + 1 + 1
222 + direct_sql->tgt_default_file_length + 1
223 + direct_sql->tgt_default_group_length;
224#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
225 } else {
226 direct_sql->conn_key_length
227 = 1
228 + direct_sql->tgt_wrapper_length + 1
229 + direct_sql->tgt_host_length + 1
230 + 5 + 1
231 + direct_sql->tgt_socket_length;
232 }
233#endif
234 if (!(direct_sql->conn_key = (char *)
235 spider_malloc(spider_current_trx, 9, direct_sql->conn_key_length + 1,
236 MYF(MY_WME | MY_ZEROFILL)))
237 )
238 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
239 if (direct_sql->connection_channel > 48)
240 *direct_sql->conn_key = '0' + 48 - direct_sql->connection_channel;
241 else
242 *direct_sql->conn_key = '0' + direct_sql->connection_channel;
243 DBUG_PRINT("info",("spider tgt_wrapper=%s", direct_sql->tgt_wrapper));
244 tmp_name = strmov(direct_sql->conn_key + 1, direct_sql->tgt_wrapper);
245 DBUG_PRINT("info",("spider tgt_host=%s", direct_sql->tgt_host));
246 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_host);
247 my_sprintf(port_str, (port_str, "%05ld", direct_sql->tgt_port));
248 DBUG_PRINT("info",("spider port_str=%s", port_str));
249 tmp_name = strmov(tmp_name + 1, port_str);
250 if (direct_sql->tgt_socket)
251 {
252 DBUG_PRINT("info",("spider tgt_socket=%s", direct_sql->tgt_socket));
253 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_socket);
254 } else
255 tmp_name++;
256#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
257 if (direct_sql->access_mode == 0)
258 {
259#endif
260 if (direct_sql->tgt_username)
261 {
262 DBUG_PRINT("info",("spider tgt_username=%s", direct_sql->tgt_username));
263 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_username);
264 } else
265 tmp_name++;
266 if (direct_sql->tgt_password)
267 {
268 DBUG_PRINT("info",("spider tgt_password=%s", direct_sql->tgt_password));
269 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_password);
270 } else
271 tmp_name++;
272 if (direct_sql->tgt_ssl_ca)
273 {
274 DBUG_PRINT("info",("spider tgt_ssl_ca=%s", direct_sql->tgt_ssl_ca));
275 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_ssl_ca);
276 } else
277 tmp_name++;
278 if (direct_sql->tgt_ssl_capath)
279 {
280 DBUG_PRINT("info",("spider tgt_ssl_capath=%s",
281 direct_sql->tgt_ssl_capath));
282 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_ssl_capath);
283 } else
284 tmp_name++;
285 if (direct_sql->tgt_ssl_cert)
286 {
287 DBUG_PRINT("info",("spider tgt_ssl_cert=%s", direct_sql->tgt_ssl_cert));
288 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_ssl_cert);
289 } else
290 tmp_name++;
291 if (direct_sql->tgt_ssl_cipher)
292 {
293 DBUG_PRINT("info",("spider tgt_ssl_cipher=%s",
294 direct_sql->tgt_ssl_cipher));
295 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_ssl_cipher);
296 } else
297 tmp_name++;
298 if (direct_sql->tgt_ssl_key)
299 {
300 DBUG_PRINT("info",("spider tgt_ssl_key=%s", direct_sql->tgt_ssl_key));
301 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_ssl_key);
302 } else
303 tmp_name++;
304 tmp_name++;
305 *tmp_name = '0' + ((char) direct_sql->tgt_ssl_vsc);
306 if (direct_sql->tgt_default_file)
307 {
308 DBUG_PRINT("info",("spider tgt_default_file=%s",
309 direct_sql->tgt_default_file));
310 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_default_file);
311 } else
312 tmp_name++;
313 if (direct_sql->tgt_default_group)
314 {
315 DBUG_PRINT("info",("spider tgt_default_group=%s",
316 direct_sql->tgt_default_group));
317 tmp_name = strmov(tmp_name + 1, direct_sql->tgt_default_group);
318 } else
319 tmp_name++;
320#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
321 }
322#endif
323 uint roop_count2;
324 direct_sql->dbton_id = SPIDER_DBTON_SIZE;
325 DBUG_PRINT("info",("spider direct_sql->tgt_wrapper=%s",
326 direct_sql->tgt_wrapper));
327 for (roop_count2 = 0; roop_count2 < SPIDER_DBTON_SIZE; roop_count2++)
328 {
329 DBUG_PRINT("info",("spider spider_dbton[%d].wrapper=%s", roop_count2,
330 spider_dbton[roop_count2].wrapper ?
331 spider_dbton[roop_count2].wrapper : "NULL"));
332 if (
333 spider_dbton[roop_count2].wrapper &&
334 !strcmp(direct_sql->tgt_wrapper, spider_dbton[roop_count2].wrapper)
335 ) {
336#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
337 if (direct_sql->access_mode == 0)
338 {
339#endif
340 if (spider_dbton[roop_count2].db_access_type ==
341 SPIDER_DB_ACCESS_TYPE_SQL)
342 {
343 direct_sql->dbton_id = roop_count2;
344 break;
345 }
346#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
347 } else {
348 if (spider_dbton[roop_count2].db_access_type ==
349 SPIDER_DB_ACCESS_TYPE_NOSQL)
350 {
351 direct_sql->dbton_id = roop_count2;
352 break;
353 }
354 }
355#endif
356 }
357 }
358 if (direct_sql->dbton_id == SPIDER_DBTON_SIZE)
359 {
360#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
361 if (direct_sql->access_mode == 0)
362 {
363#endif
364 my_printf_error(
365 ER_SPIDER_SQL_WRAPPER_IS_INVALID_NUM,
366 ER_SPIDER_SQL_WRAPPER_IS_INVALID_STR,
367 MYF(0), direct_sql->tgt_wrapper);
368 DBUG_RETURN(ER_SPIDER_SQL_WRAPPER_IS_INVALID_NUM);
369#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
370 } else {
371 my_printf_error(
372 ER_SPIDER_NOSQL_WRAPPER_IS_INVALID_NUM,
373 ER_SPIDER_NOSQL_WRAPPER_IS_INVALID_STR,
374 MYF(0), direct_sql->tgt_wrapper);
375 DBUG_RETURN(ER_SPIDER_NOSQL_WRAPPER_IS_INVALID_NUM);
376 }
377#endif
378 }
379#ifdef SPIDER_HAS_HASH_VALUE_TYPE
380 direct_sql->conn_key_hash_value = my_calc_hash(&spider_open_connections,
381 (uchar*) direct_sql->conn_key, direct_sql->conn_key_length);
382#endif
383 DBUG_RETURN(0);
384}
385
386SPIDER_CONN *spider_udf_direct_sql_create_conn(
387 const SPIDER_DIRECT_SQL *direct_sql,
388 int *error_num
389) {
390 SPIDER_CONN *conn;
391 SPIDER_IP_PORT_CONN *ip_port_conn;
392 char *tmp_name, *tmp_host, *tmp_username, *tmp_password, *tmp_socket;
393 char *tmp_wrapper, *tmp_ssl_ca, *tmp_ssl_capath, *tmp_ssl_cert;
394 char *tmp_ssl_cipher, *tmp_ssl_key, *tmp_default_file, *tmp_default_group;
395 int *need_mon;
396 DBUG_ENTER("spider_udf_direct_sql_create_conn");
397
398#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
399 if (direct_sql->access_mode == 0)
400 {
401#endif
402 if (!(conn = (SPIDER_CONN *)
403 spider_bulk_malloc(spider_current_trx, 32, MYF(MY_WME | MY_ZEROFILL),
404 &conn, sizeof(*conn),
405 &tmp_name, direct_sql->conn_key_length + 1,
406 &tmp_host, direct_sql->tgt_host_length + 1,
407 &tmp_username, direct_sql->tgt_username_length + 1,
408 &tmp_password, direct_sql->tgt_password_length + 1,
409 &tmp_socket, direct_sql->tgt_socket_length + 1,
410 &tmp_wrapper, direct_sql->tgt_wrapper_length + 1,
411 &tmp_ssl_ca, direct_sql->tgt_ssl_ca_length + 1,
412 &tmp_ssl_capath, direct_sql->tgt_ssl_capath_length + 1,
413 &tmp_ssl_cert, direct_sql->tgt_ssl_cert_length + 1,
414 &tmp_ssl_cipher, direct_sql->tgt_ssl_cipher_length + 1,
415 &tmp_ssl_key, direct_sql->tgt_ssl_key_length + 1,
416 &tmp_default_file,
417 direct_sql->tgt_default_file_length + 1,
418 &tmp_default_group,
419 direct_sql->tgt_default_group_length + 1,
420 &need_mon, sizeof(int),
421 NullS))
422 ) {
423 *error_num = HA_ERR_OUT_OF_MEM;
424 goto error_alloc_conn;
425 }
426 conn->default_database.init_calc_mem(138);
427#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
428 } else {
429 if (!(conn = (SPIDER_CONN *)
430 spider_bulk_malloc(spider_current_trx, 33, MYF(MY_WME | MY_ZEROFILL),
431 &conn, sizeof(*conn),
432 &tmp_name, direct_sql->conn_key_length + 1,
433 &tmp_host, direct_sql->tgt_host_length + 1,
434 &tmp_socket, direct_sql->tgt_socket_length + 1,
435 &tmp_wrapper, direct_sql->tgt_wrapper_length + 1,
436 &need_mon, sizeof(int),
437 NullS))
438 ) {
439 *error_num = HA_ERR_OUT_OF_MEM;
440 goto error_alloc_conn;
441 }
442 conn->default_database.init_calc_mem(103);
443 }
444#endif
445
446 conn->conn_key_length = direct_sql->conn_key_length;
447 conn->conn_key = tmp_name;
448 memcpy(conn->conn_key, direct_sql->conn_key, direct_sql->conn_key_length);
449 conn->tgt_wrapper_length = direct_sql->tgt_wrapper_length;
450 conn->tgt_wrapper = tmp_wrapper;
451 memcpy(conn->tgt_wrapper, direct_sql->tgt_wrapper,
452 direct_sql->tgt_wrapper_length);
453 conn->tgt_host_length = direct_sql->tgt_host_length;
454 conn->tgt_host = tmp_host;
455 memcpy(conn->tgt_host, direct_sql->tgt_host, direct_sql->tgt_host_length);
456#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
457 if (direct_sql->access_mode == 0)
458 {
459#endif
460 conn->tgt_port = direct_sql->tgt_port;
461 conn->tgt_socket_length = direct_sql->tgt_socket_length;
462 conn->tgt_socket = tmp_socket;
463 memcpy(conn->tgt_socket, direct_sql->tgt_socket,
464 direct_sql->tgt_socket_length);
465 conn->tgt_username_length = direct_sql->tgt_username_length;
466 conn->tgt_username = tmp_username;
467 memcpy(conn->tgt_username, direct_sql->tgt_username,
468 direct_sql->tgt_username_length);
469 conn->tgt_password_length = direct_sql->tgt_password_length;
470 conn->tgt_password = tmp_password;
471 memcpy(conn->tgt_password, direct_sql->tgt_password,
472 direct_sql->tgt_password_length);
473 conn->tgt_ssl_ca_length = direct_sql->tgt_ssl_ca_length;
474 if (conn->tgt_ssl_ca_length)
475 {
476 conn->tgt_ssl_ca = tmp_ssl_ca;
477 memcpy(conn->tgt_ssl_ca, direct_sql->tgt_ssl_ca,
478 direct_sql->tgt_ssl_ca_length);
479 } else
480 conn->tgt_ssl_ca = NULL;
481 conn->tgt_ssl_capath_length = direct_sql->tgt_ssl_capath_length;
482 if (conn->tgt_ssl_capath_length)
483 {
484 conn->tgt_ssl_capath = tmp_ssl_capath;
485 memcpy(conn->tgt_ssl_capath, direct_sql->tgt_ssl_capath,
486 direct_sql->tgt_ssl_capath_length);
487 } else
488 conn->tgt_ssl_capath = NULL;
489 conn->tgt_ssl_cert_length = direct_sql->tgt_ssl_cert_length;
490 if (conn->tgt_ssl_cert_length)
491 {
492 conn->tgt_ssl_cert = tmp_ssl_cert;
493 memcpy(conn->tgt_ssl_cert, direct_sql->tgt_ssl_cert,
494 direct_sql->tgt_ssl_cert_length);
495 } else
496 conn->tgt_ssl_cert = NULL;
497 conn->tgt_ssl_cipher_length = direct_sql->tgt_ssl_cipher_length;
498 if (conn->tgt_ssl_cipher_length)
499 {
500 conn->tgt_ssl_cipher = tmp_ssl_cipher;
501 memcpy(conn->tgt_ssl_cipher, direct_sql->tgt_ssl_cipher,
502 direct_sql->tgt_ssl_cipher_length);
503 } else
504 conn->tgt_ssl_cipher = NULL;
505 conn->tgt_ssl_key_length = direct_sql->tgt_ssl_key_length;
506 if (conn->tgt_ssl_key_length)
507 {
508 conn->tgt_ssl_key = tmp_ssl_key;
509 memcpy(conn->tgt_ssl_key, direct_sql->tgt_ssl_key,
510 direct_sql->tgt_ssl_key_length);
511 } else
512 conn->tgt_ssl_key = NULL;
513 conn->tgt_default_file_length = direct_sql->tgt_default_file_length;
514 if (conn->tgt_default_file_length)
515 {
516 conn->tgt_default_file = tmp_default_file;
517 memcpy(conn->tgt_default_file, direct_sql->tgt_default_file,
518 direct_sql->tgt_default_file_length);
519 } else
520 conn->tgt_default_file = NULL;
521 conn->tgt_default_group_length = direct_sql->tgt_default_group_length;
522 if (conn->tgt_default_group_length)
523 {
524 conn->tgt_default_group = tmp_default_group;
525 memcpy(conn->tgt_default_group, direct_sql->tgt_default_group,
526 direct_sql->tgt_default_group_length);
527 } else
528 conn->tgt_default_group = NULL;
529 conn->tgt_ssl_vsc = direct_sql->tgt_ssl_vsc;
530#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
531 } else {
532 conn->hs_port = direct_sql->tgt_port;
533 if (direct_sql->tgt_socket)
534 {
535 conn->hs_sock_length = direct_sql->tgt_socket_length;
536 conn->hs_sock = tmp_socket;
537 memcpy(conn->hs_sock, direct_sql->tgt_socket,
538 direct_sql->tgt_socket_length);
539 }
540 }
541#endif
542 conn->dbton_id = direct_sql->dbton_id;
543 conn->conn_need_mon = need_mon;
544 conn->need_mon = need_mon;
545 if (!(conn->db_conn = spider_dbton[conn->dbton_id].create_db_conn(conn)))
546 {
547 *error_num = HA_ERR_OUT_OF_MEM;
548 goto error_db_conn_create;
549 }
550 if ((*error_num = conn->db_conn->init()))
551 {
552 goto error_db_conn_init;
553 }
554 conn->join_trx = 0;
555 conn->thd = NULL;
556 conn->table_lock = 0;
557 conn->semi_trx_isolation = -2;
558 conn->semi_trx_isolation_chk = FALSE;
559 conn->semi_trx_chk = FALSE;
560#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
561 if (direct_sql->access_mode == 0)
562 {
563#endif
564 conn->conn_kind = SPIDER_CONN_KIND_MYSQL;
565#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
566 } else if (direct_sql->access_mode == 1)
567 {
568 conn->conn_kind = SPIDER_CONN_KIND_HS_READ;
569 } else {
570 conn->conn_kind = SPIDER_CONN_KIND_HS_WRITE;
571 }
572#endif
573
574#if MYSQL_VERSION_ID < 50500
575 if (pthread_mutex_init(&conn->mta_conn_mutex, MY_MUTEX_INIT_FAST))
576#else
577 if (mysql_mutex_init(spd_key_mutex_mta_conn, &conn->mta_conn_mutex,
578 MY_MUTEX_INIT_FAST))
579#endif
580 {
581 *error_num = HA_ERR_OUT_OF_MEM;
582 goto error_mta_conn_mutex_init;
583 }
584
585 if ((*error_num = spider_db_udf_direct_sql_connect(direct_sql, conn)))
586 goto error;
587 conn->ping_time = (time_t) time((time_t*) 0);
588 conn->connect_error_time = conn->ping_time;
589 pthread_mutex_lock(&spider_conn_id_mutex);
590 conn->conn_id = spider_conn_id;
591 ++spider_conn_id;
592 pthread_mutex_unlock(&spider_conn_id_mutex);
593
594 pthread_mutex_lock(&spider_ipport_conn_mutex);
595#ifdef SPIDER_HAS_HASH_VALUE_TYPE
596 if ((ip_port_conn = (SPIDER_IP_PORT_CONN*) my_hash_search_using_hash_value(
597 &spider_ipport_conns, conn->conn_key_hash_value,
598 (uchar*)conn->conn_key, conn->conn_key_length)))
599#else
600 if ((ip_port_conn = (SPIDER_IP_PORT_CONN*) my_hash_search(
601 &spider_ipport_conns, (uchar*)conn->conn_key, conn->conn_key_length)))
602#endif
603 { /* exists, +1 */
604 pthread_mutex_unlock(&spider_ipport_conn_mutex);
605 pthread_mutex_lock(&ip_port_conn->mutex);
606 if (spider_param_max_connections())
607 { /* enable conncetion pool */
608 if (ip_port_conn->ip_port_count >= spider_param_max_connections())
609 { /* bigger than the max num of connections, free conn and return NULL */
610 pthread_mutex_unlock(&ip_port_conn->mutex);
611 goto error_too_many_ipport_count;
612 }
613 }
614 ip_port_conn->ip_port_count++;
615 pthread_mutex_unlock(&ip_port_conn->mutex);
616 }
617 else
618 {// do not exist
619 ip_port_conn = spider_create_ipport_conn(conn);
620 if (!ip_port_conn) {
621 /* failed, always do not effect 'create conn' */
622 pthread_mutex_unlock(&spider_ipport_conn_mutex);
623 DBUG_RETURN(conn);
624 }
625 if (my_hash_insert(&spider_ipport_conns, (uchar *)ip_port_conn)) {
626 /* insert failed, always do not effect 'create conn' */
627 pthread_mutex_unlock(&spider_ipport_conn_mutex);
628 DBUG_RETURN(conn);
629 }
630 pthread_mutex_unlock(&spider_ipport_conn_mutex);
631 }
632 conn->ip_port_conn = ip_port_conn;
633
634 DBUG_RETURN(conn);
635
636error:
637 DBUG_ASSERT(!conn->mta_conn_mutex_file_pos.file_name);
638 pthread_mutex_destroy(&conn->mta_conn_mutex);
639error_too_many_ipport_count:
640error_mta_conn_mutex_init:
641error_db_conn_init:
642 delete conn->db_conn;
643error_db_conn_create:
644 spider_free(spider_current_trx, conn, MYF(0));
645error_alloc_conn:
646 DBUG_RETURN(NULL);
647}
648
649SPIDER_CONN *spider_udf_direct_sql_get_conn(
650 const SPIDER_DIRECT_SQL *direct_sql,
651 SPIDER_TRX *trx,
652 int *error_num
653) {
654 SPIDER_CONN *conn = NULL;
655 DBUG_ENTER("spider_udf_direct_sql_get_conn");
656#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
657 DBUG_PRINT("info",("spider direct_sql->access_mode=%d",
658 direct_sql->access_mode));
659#endif
660
661#ifdef SPIDER_HAS_HASH_VALUE_TYPE
662 if (
663#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
664 (direct_sql->access_mode == 0 &&
665#endif
666 !(conn = (SPIDER_CONN*) my_hash_search_using_hash_value(
667 &trx->trx_conn_hash, direct_sql->conn_key_hash_value,
668 (uchar*) direct_sql->conn_key, direct_sql->conn_key_length))
669#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
670 ) ||
671 (direct_sql->access_mode == 1 &&
672 !(conn = (SPIDER_CONN*) my_hash_search_using_hash_value(
673 &trx->trx_direct_hs_r_conn_hash, direct_sql->conn_key_hash_value,
674 (uchar*) direct_sql->conn_key, direct_sql->conn_key_length))
675 ) ||
676 (direct_sql->access_mode == 2 &&
677 !(conn = (SPIDER_CONN*) my_hash_search_using_hash_value(
678 &trx->trx_direct_hs_w_conn_hash, direct_sql->conn_key_hash_value,
679 (uchar*) direct_sql->conn_key, direct_sql->conn_key_length))
680 )
681#endif
682 )
683#else
684 if (
685#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
686 (direct_sql->access_mode == 0 &&
687#endif
688 !(conn = (SPIDER_CONN*) my_hash_search(&trx->trx_conn_hash,
689 (uchar*) direct_sql->conn_key, direct_sql->conn_key_length))
690#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
691 ) ||
692 (direct_sql->access_mode == 1 &&
693 !(conn = (SPIDER_CONN*) my_hash_search(&trx->trx_direct_hs_r_conn_hash,
694 (uchar*) direct_sql->conn_key, direct_sql->conn_key_length))
695 ) ||
696 (direct_sql->access_mode == 2 &&
697 !(conn = (SPIDER_CONN*) my_hash_search(&trx->trx_direct_hs_w_conn_hash,
698 (uchar*) direct_sql->conn_key, direct_sql->conn_key_length))
699 )
700#endif
701 )
702#endif
703 {
704 if (
705#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
706 (direct_sql->access_mode == 0 &&
707#endif
708 (
709 (spider_param_conn_recycle_mode(trx->thd) & 1) ||
710 spider_param_conn_recycle_strict(trx->thd)
711 )
712#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
713 ) ||
714 (direct_sql->access_mode == 1 &&
715 (
716 (spider_param_hs_r_conn_recycle_mode(trx->thd) & 1) ||
717 spider_param_hs_r_conn_recycle_strict(trx->thd)
718 )
719 ) ||
720 (direct_sql->access_mode == 2 &&
721 (
722 (spider_param_hs_w_conn_recycle_mode(trx->thd) & 1) ||
723 spider_param_hs_w_conn_recycle_strict(trx->thd)
724 )
725 )
726#endif
727 ) {
728#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
729 if (direct_sql->access_mode == 0)
730 {
731#endif
732 pthread_mutex_lock(&spider_conn_mutex);
733#ifdef SPIDER_HAS_HASH_VALUE_TYPE
734 if (!(conn = (SPIDER_CONN*) my_hash_search_using_hash_value(
735 &spider_open_connections, direct_sql->conn_key_hash_value,
736 (uchar*) direct_sql->conn_key, direct_sql->conn_key_length)))
737#else
738 if (!(conn = (SPIDER_CONN*) my_hash_search(&spider_open_connections,
739 (uchar*) direct_sql->conn_key, direct_sql->conn_key_length)))
740#endif
741 {
742 pthread_mutex_unlock(&spider_conn_mutex);
743 DBUG_PRINT("info",("spider create new conn"));
744 if(!(conn = spider_udf_direct_sql_create_conn(direct_sql,
745 error_num)))
746 goto error;
747 } else {
748#ifdef HASH_UPDATE_WITH_HASH_VALUE
749 my_hash_delete_with_hash_value(&spider_open_connections,
750 conn->conn_key_hash_value, (uchar*) conn);
751#else
752 my_hash_delete(&spider_open_connections, (uchar*) conn);
753#endif
754 pthread_mutex_unlock(&spider_conn_mutex);
755 DBUG_PRINT("info",("spider get global conn"));
756 }
757#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
758 }
759#endif
760 } else {
761 DBUG_PRINT("info",("spider create new conn"));
762 /* conn_recycle_strict = 0 and conn_recycle_mode = 0 or 2 */
763 if(!(conn = spider_udf_direct_sql_create_conn(direct_sql, error_num)))
764 goto error;
765 }
766 conn->thd = trx->thd;
767 conn->priority = direct_sql->priority;
768
769#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
770 if (direct_sql->access_mode == 0)
771 {
772#endif
773 uint old_elements = trx->trx_conn_hash.array.max_element;
774#ifdef HASH_UPDATE_WITH_HASH_VALUE
775 if (my_hash_insert_with_hash_value(&trx->trx_conn_hash,
776 direct_sql->conn_key_hash_value, (uchar*) conn))
777#else
778 if (my_hash_insert(&trx->trx_conn_hash, (uchar*) conn))
779#endif
780 {
781 spider_free_conn(conn);
782 *error_num = HA_ERR_OUT_OF_MEM;
783 goto error;
784 }
785 if (trx->trx_conn_hash.array.max_element > old_elements)
786 {
787 spider_alloc_calc_mem(spider_current_trx,
788 trx->trx_conn_hash,
789 (trx->trx_conn_hash.array.max_element - old_elements) *
790 trx->trx_conn_hash.array.size_of_element);
791 }
792#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
793 } else if (direct_sql->access_mode == 1)
794 {
795 uint old_elements = trx->trx_direct_hs_r_conn_hash.array.max_element;
796#ifdef HASH_UPDATE_WITH_HASH_VALUE
797 if (my_hash_insert_with_hash_value(&trx->trx_direct_hs_r_conn_hash,
798 direct_sql->conn_key_hash_value, (uchar*) conn))
799#else
800 if (my_hash_insert(&trx->trx_direct_hs_r_conn_hash, (uchar*) conn))
801#endif
802 {
803 spider_free_conn(conn);
804 *error_num = HA_ERR_OUT_OF_MEM;
805 goto error;
806 }
807 if (trx->trx_direct_hs_r_conn_hash.array.max_element > old_elements)
808 {
809 spider_alloc_calc_mem(spider_current_trx,
810 trx->trx_direct_hs_r_conn_hash,
811 (trx->trx_direct_hs_r_conn_hash.array.max_element - old_elements) *
812 trx->trx_direct_hs_r_conn_hash.array.size_of_element);
813 }
814 } else {
815 uint old_elements = trx->trx_direct_hs_w_conn_hash.array.max_element;
816#ifdef HASH_UPDATE_WITH_HASH_VALUE
817 if (my_hash_insert_with_hash_value(&trx->trx_direct_hs_w_conn_hash,
818 direct_sql->conn_key_hash_value, (uchar*) conn))
819#else
820 if (my_hash_insert(&trx->trx_direct_hs_w_conn_hash, (uchar*) conn))
821#endif
822 {
823 spider_free_conn(conn);
824 *error_num = HA_ERR_OUT_OF_MEM;
825 goto error;
826 }
827 if (trx->trx_direct_hs_w_conn_hash.array.max_element > old_elements)
828 {
829 spider_alloc_calc_mem(spider_current_trx,
830 trx->trx_direct_hs_w_conn_hash,
831 (trx->trx_direct_hs_w_conn_hash.array.max_element - old_elements) *
832 trx->trx_direct_hs_w_conn_hash.array.size_of_element);
833 }
834 }
835#endif
836 }
837
838 if (conn->queued_connect)
839 {
840 if ((*error_num = spider_db_udf_direct_sql_connect(direct_sql, conn)))
841 goto error;
842 conn->queued_connect = FALSE;
843 }
844
845 if (conn->queued_ping)
846 conn->queued_ping = FALSE;
847
848 DBUG_PRINT("info",("spider conn=%p", conn));
849 DBUG_PRINT("info",("spider conn->conn_kind=%u", conn->conn_kind));
850 DBUG_RETURN(conn);
851
852error:
853 DBUG_RETURN(NULL);
854}
855
856int spider_udf_direct_sql_get_server(
857 SPIDER_DIRECT_SQL *direct_sql
858) {
859 MEM_ROOT mem_root;
860 int error_num, length;
861 FOREIGN_SERVER *server, server_buf;
862 DBUG_ENTER("spider_udf_direct_sql_get_server");
863 SPD_INIT_ALLOC_ROOT(&mem_root, 65, 0, MYF(MY_WME));
864
865 if (!(server
866 = get_server_by_name(&mem_root, direct_sql->server_name, &server_buf)))
867 {
868 error_num = ER_FOREIGN_SERVER_DOESNT_EXIST;
869 goto error_get_server;
870 }
871
872 if (!direct_sql->tgt_wrapper && server->scheme)
873 {
874 direct_sql->tgt_wrapper_length = strlen(server->scheme);
875 if (!(direct_sql->tgt_wrapper =
876 spider_create_string(server->scheme, direct_sql->tgt_wrapper_length)))
877 {
878 error_num = HA_ERR_OUT_OF_MEM;
879 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
880 goto error;
881 }
882 DBUG_PRINT("info",("spider tgt_wrapper=%s", direct_sql->tgt_wrapper));
883 }
884
885 if (!direct_sql->tgt_host && server->host)
886 {
887 direct_sql->tgt_host_length = strlen(server->host);
888 if (!(direct_sql->tgt_host =
889 spider_create_string(server->host, direct_sql->tgt_host_length)))
890 {
891 error_num = HA_ERR_OUT_OF_MEM;
892 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
893 goto error;
894 }
895 DBUG_PRINT("info",("spider tgt_host=%s", direct_sql->tgt_host));
896 }
897
898 if (direct_sql->tgt_port == -1)
899 {
900 direct_sql->tgt_port = server->port;
901 DBUG_PRINT("info",("spider tgt_port=%ld", direct_sql->tgt_port));
902 }
903
904 if (!direct_sql->tgt_socket && server->socket)
905 {
906 direct_sql->tgt_socket_length = strlen(server->socket);
907 if (!(direct_sql->tgt_socket =
908 spider_create_string(server->socket, direct_sql->tgt_socket_length)))
909 {
910 error_num = HA_ERR_OUT_OF_MEM;
911 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
912 goto error;
913 }
914 DBUG_PRINT("info",("spider tgt_socket=%s", direct_sql->tgt_socket));
915 }
916
917 if (!direct_sql->tgt_default_db_name && server->db &&
918 (length = strlen(server->db)))
919 {
920 direct_sql->tgt_default_db_name_length = length;
921 if (!(direct_sql->tgt_default_db_name =
922 spider_create_string(server->db, length)))
923 {
924 error_num = HA_ERR_OUT_OF_MEM;
925 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
926 goto error;
927 }
928 DBUG_PRINT("info",("spider tgt_default_db_name=%s",
929 direct_sql->tgt_default_db_name));
930 }
931
932 if (!direct_sql->tgt_username && server->username)
933 {
934 direct_sql->tgt_username_length = strlen(server->username);
935 if (!(direct_sql->tgt_username =
936 spider_create_string(server->username, direct_sql->tgt_username_length)))
937 {
938 error_num = HA_ERR_OUT_OF_MEM;
939 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
940 goto error;
941 }
942 DBUG_PRINT("info",("spider tgt_username=%s", direct_sql->tgt_username));
943 }
944
945 if (!direct_sql->tgt_password && server->password)
946 {
947 direct_sql->tgt_password_length = strlen(server->password);
948 if (!(direct_sql->tgt_password =
949 spider_create_string(server->password, direct_sql->tgt_password_length)))
950 {
951 error_num = HA_ERR_OUT_OF_MEM;
952 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
953 goto error;
954 }
955 DBUG_PRINT("info",("spider tgt_password=%s", direct_sql->tgt_password));
956 }
957
958 free_root(&mem_root, MYF(0));
959 DBUG_RETURN(0);
960
961error_get_server:
962 my_error(error_num, MYF(0), direct_sql->server_name);
963error:
964 free_root(&mem_root, MYF(0));
965 DBUG_RETURN(error_num);
966}
967
968#define SPIDER_PARAM_STR_LEN(name) name ## _length
969#define SPIDER_PARAM_STR(title_name, param_name) \
970 if (!strncasecmp(tmp_ptr, title_name, title_length)) \
971 { \
972 DBUG_PRINT("info",("spider " title_name " start")); \
973 if (!direct_sql->param_name) \
974 { \
975 if ((direct_sql->param_name = spider_get_string_between_quote( \
976 start_ptr, TRUE, &param_string_parse))) \
977 direct_sql->SPIDER_PARAM_STR_LEN(param_name) = \
978 strlen(direct_sql->param_name); \
979 else \
980 { \
981 error_num = param_string_parse.print_param_error(); \
982 goto error; \
983 } \
984 DBUG_PRINT("info",("spider " title_name "=%s", direct_sql->param_name)); \
985 } \
986 break; \
987 }
988#define SPIDER_PARAM_HINT_WITH_MAX(title_name, param_name, check_length, max_size, min_val, max_val) \
989 if (!strncasecmp(tmp_ptr, title_name, check_length)) \
990 { \
991 DBUG_PRINT("info",("spider " title_name " start")); \
992 DBUG_PRINT("info",("spider max_size=%d", max_size)); \
993 int hint_num = atoi(tmp_ptr + check_length) - 1; \
994 DBUG_PRINT("info",("spider hint_num=%d", hint_num)); \
995 DBUG_PRINT("info",("spider direct_sql->param_name=%p", \
996 direct_sql->param_name)); \
997 if (direct_sql->param_name) \
998 { \
999 if (hint_num < 0 || hint_num >= max_size) \
1000 { \
1001 error_num = param_string_parse.print_param_error(); \
1002 goto error; \
1003 } else if (direct_sql->param_name[hint_num] != -1) \
1004 break; \
1005 char *hint_str = spider_get_string_between_quote(start_ptr, FALSE); \
1006 if (hint_str) \
1007 { \
1008 direct_sql->param_name[hint_num] = atoi(hint_str); \
1009 if (direct_sql->param_name[hint_num] < min_val) \
1010 direct_sql->param_name[hint_num] = min_val; \
1011 else if (direct_sql->param_name[hint_num] > max_val) \
1012 direct_sql->param_name[hint_num] = max_val; \
1013 } else { \
1014 error_num = param_string_parse.print_param_error(); \
1015 goto error; \
1016 } \
1017 DBUG_PRINT("info",("spider " title_name "[%d]=%d", hint_num, \
1018 direct_sql->param_name[hint_num])); \
1019 } else { \
1020 error_num = param_string_parse.print_param_error(); \
1021 goto error; \
1022 } \
1023 break; \
1024 }
1025#define SPIDER_PARAM_INT_WITH_MAX(title_name, param_name, min_val, max_val) \
1026 if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1027 { \
1028 DBUG_PRINT("info",("spider " title_name " start")); \
1029 if (direct_sql->param_name == -1) \
1030 { \
1031 if ((tmp_ptr2 = spider_get_string_between_quote( \
1032 start_ptr, FALSE))) \
1033 { \
1034 direct_sql->param_name = atoi(tmp_ptr2); \
1035 if (direct_sql->param_name < min_val) \
1036 direct_sql->param_name = min_val; \
1037 else if (direct_sql->param_name > max_val) \
1038 direct_sql->param_name = max_val; \
1039 param_string_parse.set_param_value(tmp_ptr2, \
1040 tmp_ptr2 + \
1041 strlen(tmp_ptr2) + 1); \
1042 } else { \
1043 error_num = param_string_parse.print_param_error(); \
1044 goto error; \
1045 } \
1046 DBUG_PRINT("info",("spider " title_name "=%d", \
1047 (int) direct_sql->param_name)); \
1048 } \
1049 break; \
1050 }
1051#define SPIDER_PARAM_INT(title_name, param_name, min_val) \
1052 if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1053 { \
1054 DBUG_PRINT("info",("spider " title_name " start")); \
1055 if (direct_sql->param_name == -1) \
1056 { \
1057 if ((tmp_ptr2 = spider_get_string_between_quote( \
1058 start_ptr, FALSE))) \
1059 { \
1060 direct_sql->param_name = atoi(tmp_ptr2); \
1061 if (direct_sql->param_name < min_val) \
1062 direct_sql->param_name = min_val; \
1063 param_string_parse.set_param_value(tmp_ptr2, \
1064 tmp_ptr2 + \
1065 strlen(tmp_ptr2) + 1); \
1066 } else { \
1067 error_num = param_string_parse.print_param_error(); \
1068 goto error; \
1069 } \
1070 DBUG_PRINT("info",("spider " title_name "=%d", direct_sql->param_name)); \
1071 } \
1072 break; \
1073 }
1074#define SPIDER_PARAM_LONGLONG(title_name, param_name, min_val) \
1075 if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1076 { \
1077 DBUG_PRINT("info",("spider " title_name " start")); \
1078 if (direct_sql->param_name == -1) \
1079 { \
1080 if ((tmp_ptr2 = spider_get_string_between_quote( \
1081 start_ptr, FALSE))) \
1082 { \
1083 direct_sql->param_name = \
1084 my_strtoll10(tmp_ptr2, (char**) NULL, &error_num); \
1085 if (direct_sql->param_name < min_val) \
1086 direct_sql->param_name = min_val; \
1087 param_string_parse.set_param_value(tmp_ptr2, \
1088 tmp_ptr2 + \
1089 strlen(tmp_ptr2) + 1); \
1090 } else { \
1091 error_num = param_string_parse.print_param_error(); \
1092 goto error; \
1093 } \
1094 DBUG_PRINT("info",("spider " title_name "=%lld", \
1095 direct_sql->param_name)); \
1096 } \
1097 break; \
1098 }
1099
1100int spider_udf_parse_direct_sql_param(
1101 SPIDER_TRX *trx,
1102 SPIDER_DIRECT_SQL *direct_sql,
1103 const char *param,
1104 int param_length
1105) {
1106 int error_num = 0, roop_count;
1107 char *param_string = NULL;
1108 char *sprit_ptr[2];
1109 char *tmp_ptr, *tmp_ptr2, *start_ptr;
1110 int title_length;
1111 SPIDER_PARAM_STRING_PARSE param_string_parse;
1112 DBUG_ENTER("spider_udf_parse_direct_sql_param");
1113 direct_sql->tgt_port = -1;
1114 direct_sql->tgt_ssl_vsc = -1;
1115 direct_sql->table_loop_mode = -1;
1116 direct_sql->priority = -1;
1117 direct_sql->connect_timeout = -1;
1118 direct_sql->net_read_timeout = -1;
1119 direct_sql->net_write_timeout = -1;
1120 direct_sql->bulk_insert_rows = -1;
1121 direct_sql->connection_channel = -1;
1122#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1123 direct_sql->access_mode = -1;
1124#endif
1125#if MYSQL_VERSION_ID < 50500
1126#else
1127 direct_sql->use_real_table = -1;
1128#endif
1129 direct_sql->error_rw_mode = -1;
1130 for (roop_count = 0; roop_count < direct_sql->table_count; roop_count++)
1131 direct_sql->iop[roop_count] = -1;
1132
1133 if (param_length == 0)
1134 goto set_default;
1135 DBUG_PRINT("info",("spider create param_string string"));
1136 if (
1137 !(param_string = spider_create_string(
1138 param,
1139 param_length))
1140 ) {
1141 error_num = HA_ERR_OUT_OF_MEM;
1142 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1143 goto error_alloc_param_string;
1144 }
1145 DBUG_PRINT("info",("spider param_string=%s", param_string));
1146
1147 sprit_ptr[0] = param_string;
1148 param_string_parse.init(param_string, ER_SPIDER_INVALID_UDF_PARAM_NUM);
1149 while (sprit_ptr[0])
1150 {
1151 if ((sprit_ptr[1] = strchr(sprit_ptr[0], ',')))
1152 {
1153 *sprit_ptr[1] = '\0';
1154 sprit_ptr[1]++;
1155 }
1156 tmp_ptr = sprit_ptr[0];
1157 sprit_ptr[0] = sprit_ptr[1];
1158 while (*tmp_ptr == ' ' || *tmp_ptr == '\r' ||
1159 *tmp_ptr == '\n' || *tmp_ptr == '\t')
1160 tmp_ptr++;
1161
1162 if (*tmp_ptr == '\0')
1163 continue;
1164
1165 title_length = 0;
1166 start_ptr = tmp_ptr;
1167 while (*start_ptr != ' ' && *start_ptr != '\'' &&
1168 *start_ptr != '"' && *start_ptr != '\0' &&
1169 *start_ptr != '\r' && *start_ptr != '\n' &&
1170 *start_ptr != '\t')
1171 {
1172 title_length++;
1173 start_ptr++;
1174 }
1175 param_string_parse.set_param_title(tmp_ptr, tmp_ptr + title_length);
1176
1177 switch (title_length)
1178 {
1179 case 0:
1180 error_num = param_string_parse.print_param_error();
1181 if (error_num)
1182 goto error;
1183 continue;
1184 case 3:
1185#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1186 SPIDER_PARAM_INT_WITH_MAX("acm", access_mode, 0, 2);
1187#endif
1188 SPIDER_PARAM_LONGLONG("bir", bulk_insert_rows, 0);
1189 SPIDER_PARAM_INT_WITH_MAX("cch", connection_channel, 0, 63);
1190 SPIDER_PARAM_INT("cto", connect_timeout, 0);
1191 SPIDER_PARAM_STR("dff", tgt_default_file);
1192 SPIDER_PARAM_STR("dfg", tgt_default_group);
1193 SPIDER_PARAM_LONGLONG("prt", priority, 0);
1194 SPIDER_PARAM_INT("rto", net_read_timeout, 0);
1195 SPIDER_PARAM_STR("sca", tgt_ssl_ca);
1196 SPIDER_PARAM_STR("sch", tgt_ssl_cipher);
1197 SPIDER_PARAM_STR("scp", tgt_ssl_capath);
1198 SPIDER_PARAM_STR("scr", tgt_ssl_cert);
1199 SPIDER_PARAM_STR("sky", tgt_ssl_key);
1200 SPIDER_PARAM_STR("srv", server_name);
1201 SPIDER_PARAM_INT_WITH_MAX("svc", tgt_ssl_vsc, 0, 1);
1202 SPIDER_PARAM_INT_WITH_MAX("tlm", table_loop_mode, 0, 2);
1203#if MYSQL_VERSION_ID < 50500
1204#else
1205 SPIDER_PARAM_INT_WITH_MAX("urt", use_real_table, 0, 1);
1206#endif
1207 SPIDER_PARAM_INT("wto", net_write_timeout, 0);
1208 error_num = param_string_parse.print_param_error();
1209 goto error;
1210 case 4:
1211 SPIDER_PARAM_INT_WITH_MAX("erwm", error_rw_mode, 0, 1);
1212 SPIDER_PARAM_STR("host", tgt_host);
1213 SPIDER_PARAM_INT_WITH_MAX("port", tgt_port, 0, 65535);
1214 SPIDER_PARAM_STR("user", tgt_username);
1215 error_num = param_string_parse.print_param_error();
1216 goto error;
1217 case 6:
1218 SPIDER_PARAM_STR("server", server_name);
1219 SPIDER_PARAM_STR("socket", tgt_socket);
1220 SPIDER_PARAM_HINT_WITH_MAX("iop", iop, 3, direct_sql->table_count, 0, 2);
1221 SPIDER_PARAM_STR("ssl_ca", tgt_ssl_ca);
1222 error_num = param_string_parse.print_param_error();
1223 goto error;
1224 case 7:
1225 SPIDER_PARAM_STR("wrapper", tgt_wrapper);
1226 SPIDER_PARAM_STR("ssl_key", tgt_ssl_key);
1227 error_num = param_string_parse.print_param_error();
1228 goto error;
1229 case 8:
1230 SPIDER_PARAM_STR("database", tgt_default_db_name);
1231 SPIDER_PARAM_STR("password", tgt_password);
1232 SPIDER_PARAM_LONGLONG("priority", priority, 0);
1233 SPIDER_PARAM_STR("ssl_cert", tgt_ssl_cert);
1234 error_num = param_string_parse.print_param_error();
1235 goto error;
1236 case 10:
1237 SPIDER_PARAM_STR("ssl_cipher", tgt_ssl_cipher);
1238 SPIDER_PARAM_STR("ssl_capath", tgt_ssl_capath);
1239 error_num = param_string_parse.print_param_error();
1240 goto error;
1241 case 11:
1242#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1243 SPIDER_PARAM_INT_WITH_MAX("access_mode", access_mode, 0, 2);
1244#endif
1245 error_num = param_string_parse.print_param_error();
1246 goto error;
1247 case 12:
1248 SPIDER_PARAM_STR("default_file", tgt_default_file);
1249 error_num = param_string_parse.print_param_error();
1250 goto error;
1251 case 13:
1252 SPIDER_PARAM_STR("default_group", tgt_default_group);
1253 SPIDER_PARAM_INT_WITH_MAX("error_rw_mode", error_rw_mode, 0, 1);
1254 error_num = param_string_parse.print_param_error();
1255 goto error;
1256 case 14:
1257#if MYSQL_VERSION_ID < 50500
1258#else
1259 SPIDER_PARAM_INT_WITH_MAX("use_real_table", use_real_table, 0, 1);
1260#endif
1261 error_num = param_string_parse.print_param_error();
1262 goto error;
1263 case 15:
1264 SPIDER_PARAM_INT_WITH_MAX("table_loop_mode", table_loop_mode, 0, 2);
1265 SPIDER_PARAM_INT("connect_timeout", connect_timeout, 0);
1266 error_num = param_string_parse.print_param_error();
1267 goto error;
1268 case 16:
1269 SPIDER_PARAM_LONGLONG("bulk_insert_rows", bulk_insert_rows, 1);
1270 SPIDER_PARAM_INT("net_read_timeout", net_read_timeout, 0);
1271 error_num = param_string_parse.print_param_error();
1272 goto error;
1273 case 17:
1274 SPIDER_PARAM_INT("net_write_timeout", net_write_timeout, 0);
1275 error_num = param_string_parse.print_param_error();
1276 goto error;
1277 case 18:
1278 SPIDER_PARAM_INT_WITH_MAX(
1279 "connection_channel", connection_channel, 0, 63);
1280 error_num = param_string_parse.print_param_error();
1281 goto error;
1282 case 22:
1283 SPIDER_PARAM_INT_WITH_MAX("ssl_verify_server_cert", tgt_ssl_vsc, 0, 1);
1284 error_num = param_string_parse.print_param_error();
1285 goto error;
1286 default:
1287 error_num = param_string_parse.print_param_error();
1288 goto error;
1289 }
1290
1291 /* Verify that the remainder of the parameter value is whitespace */
1292 if ((error_num = param_string_parse.has_extra_parameter_values()))
1293 goto error;
1294 }
1295
1296set_default:
1297 if ((error_num = spider_udf_set_direct_sql_param_default(
1298 trx,
1299 direct_sql
1300 )))
1301 goto error;
1302
1303 if (param_string)
1304 {
1305 spider_free(spider_current_trx, param_string, MYF(0));
1306 }
1307 DBUG_RETURN(0);
1308
1309error:
1310 if (param_string)
1311 {
1312 spider_free(spider_current_trx, param_string, MYF(0));
1313 }
1314error_alloc_param_string:
1315 DBUG_RETURN(error_num);
1316}
1317
1318int spider_udf_set_direct_sql_param_default(
1319 SPIDER_TRX *trx,
1320 SPIDER_DIRECT_SQL *direct_sql
1321) {
1322 int error_num, roop_count;
1323 DBUG_ENTER("spider_udf_set_direct_sql_param_default");
1324 if (direct_sql->server_name)
1325 {
1326 if ((error_num = spider_udf_direct_sql_get_server(direct_sql)))
1327 DBUG_RETURN(error_num);
1328 }
1329
1330 if (!direct_sql->tgt_default_db_name)
1331 {
1332 DBUG_PRINT("info",("spider create default tgt_default_db_name"));
1333 direct_sql->tgt_default_db_name_length = trx->thd->db.length;
1334 if (
1335 !(direct_sql->tgt_default_db_name = spider_create_string(
1336 trx->thd->db.str,
1337 direct_sql->tgt_default_db_name_length))
1338 ) {
1339 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1340 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1341 }
1342 }
1343
1344 if (!direct_sql->tgt_wrapper)
1345 {
1346 DBUG_PRINT("info",("spider create default tgt_wrapper"));
1347 direct_sql->tgt_wrapper_length = SPIDER_DB_WRAPPER_LEN;
1348 if (
1349 !(direct_sql->tgt_wrapper = spider_create_string(
1350 SPIDER_DB_WRAPPER_STR,
1351 direct_sql->tgt_wrapper_length))
1352 ) {
1353 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1354 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1355 }
1356 }
1357
1358 if (!direct_sql->tgt_host)
1359 {
1360 DBUG_PRINT("info",("spider create default tgt_host"));
1361 direct_sql->tgt_host_length = strlen(my_localhost);
1362 if (
1363 !(direct_sql->tgt_host = spider_create_string(
1364 my_localhost,
1365 direct_sql->tgt_host_length))
1366 ) {
1367 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1368 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1369 }
1370 }
1371
1372 if (
1373 !direct_sql->tgt_default_file &&
1374 direct_sql->tgt_default_group &&
1375 (*spd_defaults_file || *spd_defaults_extra_file)
1376 ) {
1377 DBUG_PRINT("info",("spider create default tgt_default_file"));
1378 if (*spd_defaults_extra_file)
1379 {
1380 direct_sql->tgt_default_file_length = strlen(*spd_defaults_extra_file);
1381 if (
1382 !(direct_sql->tgt_default_file = spider_create_string(
1383 *spd_defaults_extra_file,
1384 direct_sql->tgt_default_file_length))
1385 ) {
1386 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1387 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1388 }
1389 } else {
1390 direct_sql->tgt_default_file_length = strlen(*spd_defaults_file);
1391 if (
1392 !(direct_sql->tgt_default_file = spider_create_string(
1393 *spd_defaults_file,
1394 direct_sql->tgt_default_file_length))
1395 ) {
1396 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1397 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1398 }
1399 }
1400 }
1401
1402#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1403 if (direct_sql->access_mode == -1)
1404 direct_sql->access_mode = 0;
1405#endif
1406
1407 if (direct_sql->tgt_port == -1)
1408 {
1409#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1410 if (direct_sql->access_mode == 1)
1411 direct_sql->tgt_port = 9998;
1412 else if (direct_sql->access_mode == 2)
1413 direct_sql->tgt_port = 9999;
1414 else
1415#endif
1416 direct_sql->tgt_port = MYSQL_PORT;
1417 }
1418 else if (direct_sql->tgt_port < 0)
1419 direct_sql->tgt_port = 0;
1420 else if (direct_sql->tgt_port > 65535)
1421 direct_sql->tgt_port = 65535;
1422
1423 if (direct_sql->tgt_ssl_vsc == -1)
1424 direct_sql->tgt_ssl_vsc = 0;
1425
1426 if (
1427#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1428 direct_sql->access_mode == 0 &&
1429#endif
1430 !direct_sql->tgt_socket &&
1431 !strcmp(direct_sql->tgt_host, my_localhost)
1432 ) {
1433 DBUG_PRINT("info",("spider create default tgt_socket"));
1434 direct_sql->tgt_socket_length = strlen((char *) MYSQL_UNIX_ADDR);
1435 if (
1436 !(direct_sql->tgt_socket = spider_create_string(
1437 (char *) MYSQL_UNIX_ADDR,
1438 direct_sql->tgt_socket_length))
1439 ) {
1440 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1441 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1442 }
1443 }
1444
1445 if (direct_sql->table_loop_mode == -1)
1446 direct_sql->table_loop_mode = 0;
1447 if (direct_sql->priority == -1)
1448 direct_sql->priority = 1000000;
1449 if (direct_sql->connect_timeout == -1)
1450 direct_sql->connect_timeout = 6;
1451 if (direct_sql->net_read_timeout == -1)
1452 direct_sql->net_read_timeout = 600;
1453 if (direct_sql->net_write_timeout == -1)
1454 direct_sql->net_write_timeout = 600;
1455 if (direct_sql->bulk_insert_rows == -1)
1456 direct_sql->bulk_insert_rows = 3000;
1457 if (direct_sql->connection_channel == -1)
1458 direct_sql->connection_channel = 0;
1459#if MYSQL_VERSION_ID < 50500
1460#else
1461 if (direct_sql->use_real_table == -1)
1462 direct_sql->use_real_table = 0;
1463#endif
1464 if (direct_sql->error_rw_mode == -1)
1465 direct_sql->error_rw_mode = 0;
1466 for (roop_count = 0; roop_count < direct_sql->table_count; roop_count++)
1467 {
1468 if (direct_sql->iop[roop_count] == -1)
1469 direct_sql->iop[roop_count] = 0;
1470 }
1471 DBUG_RETURN(0);
1472}
1473
1474void spider_udf_free_direct_sql_alloc(
1475 SPIDER_DIRECT_SQL *direct_sql,
1476 my_bool bg
1477) {
1478 SPIDER_BG_DIRECT_SQL *bg_direct_sql;
1479 DBUG_ENTER("spider_udf_free_direct_sql_alloc");
1480#ifndef WITHOUT_SPIDER_BG_SEARCH
1481 if (bg)
1482 {
1483 pthread_mutex_lock(direct_sql->bg_mutex);
1484 bg_direct_sql = (SPIDER_BG_DIRECT_SQL *) direct_sql->parent;
1485 if (bg_direct_sql->direct_sql == direct_sql)
1486 bg_direct_sql->direct_sql = direct_sql->next;
1487 if (direct_sql->next)
1488 direct_sql->next->prev = direct_sql->prev;
1489 if (direct_sql->prev)
1490 direct_sql->prev->next = direct_sql->next;
1491 pthread_cond_signal(direct_sql->bg_cond);
1492 pthread_mutex_unlock(direct_sql->bg_mutex);
1493 }
1494#endif
1495#if MYSQL_VERSION_ID < 50500
1496#else
1497 if (direct_sql->real_table_used && direct_sql->open_tables_thd)
1498 {
1499 spider_sys_close_table(direct_sql->open_tables_thd,
1500 &direct_sql->open_tables_backup);
1501 }
1502#endif
1503 if (direct_sql->server_name)
1504 {
1505 spider_free(spider_current_trx, direct_sql->server_name, MYF(0));
1506 }
1507 if (direct_sql->tgt_default_db_name)
1508 {
1509 spider_free(spider_current_trx, direct_sql->tgt_default_db_name, MYF(0));
1510 }
1511 if (direct_sql->tgt_host)
1512 {
1513 spider_free(spider_current_trx, direct_sql->tgt_host, MYF(0));
1514 }
1515 if (direct_sql->tgt_username)
1516 {
1517 spider_free(spider_current_trx, direct_sql->tgt_username, MYF(0));
1518 }
1519 if (direct_sql->tgt_password)
1520 {
1521 spider_free(spider_current_trx, direct_sql->tgt_password, MYF(0));
1522 }
1523 if (direct_sql->tgt_socket)
1524 {
1525 spider_free(spider_current_trx, direct_sql->tgt_socket, MYF(0));
1526 }
1527 if (direct_sql->tgt_wrapper)
1528 {
1529 spider_free(spider_current_trx, direct_sql->tgt_wrapper, MYF(0));
1530 }
1531 if (direct_sql->tgt_ssl_ca)
1532 {
1533 spider_free(spider_current_trx, direct_sql->tgt_ssl_ca, MYF(0));
1534 }
1535 if (direct_sql->tgt_ssl_capath)
1536 {
1537 spider_free(spider_current_trx, direct_sql->tgt_ssl_capath, MYF(0));
1538 }
1539 if (direct_sql->tgt_ssl_cert)
1540 {
1541 spider_free(spider_current_trx, direct_sql->tgt_ssl_cert, MYF(0));
1542 }
1543 if (direct_sql->tgt_ssl_cipher)
1544 {
1545 spider_free(spider_current_trx, direct_sql->tgt_ssl_cipher, MYF(0));
1546 }
1547 if (direct_sql->tgt_ssl_key)
1548 {
1549 spider_free(spider_current_trx, direct_sql->tgt_ssl_key, MYF(0));
1550 }
1551 if (direct_sql->tgt_default_file)
1552 {
1553 spider_free(spider_current_trx, direct_sql->tgt_default_file, MYF(0));
1554 }
1555 if (direct_sql->tgt_default_group)
1556 {
1557 spider_free(spider_current_trx, direct_sql->tgt_default_group, MYF(0));
1558 }
1559 if (direct_sql->conn_key)
1560 {
1561 spider_free(spider_current_trx, direct_sql->conn_key, MYF(0));
1562 }
1563 if (direct_sql->db_names)
1564 {
1565 spider_free(spider_current_trx, direct_sql->db_names, MYF(0));
1566 }
1567 spider_free(spider_current_trx, direct_sql, MYF(0));
1568 DBUG_VOID_RETURN;
1569}
1570
1571long long spider_direct_sql_body(
1572 UDF_INIT *initid,
1573 UDF_ARGS *args,
1574 char *is_null,
1575 char *error,
1576 my_bool bg
1577) {
1578 int error_num, roop_count;
1579 SPIDER_DIRECT_SQL *direct_sql = NULL, *tmp_direct_sql;
1580 THD *thd = current_thd;
1581 SPIDER_TRX *trx;
1582 SPIDER_CONN *conn;
1583 char *sql;
1584 TABLE_LIST table_list;
1585 SPIDER_BG_DIRECT_SQL *bg_direct_sql;
1586#if MYSQL_VERSION_ID < 50500
1587#else
1588 TABLE_LIST *real_table_list_last = NULL;
1589 uint use_real_table = 0;
1590#endif
1591 DBUG_ENTER("spider_direct_sql_body");
1592 SPIDER_BACKUP_DASTATUS;
1593 if (!(direct_sql = (SPIDER_DIRECT_SQL *)
1594 spider_bulk_malloc(spider_current_trx, 34, MYF(MY_WME | MY_ZEROFILL),
1595 &direct_sql, sizeof(SPIDER_DIRECT_SQL),
1596 &sql, sizeof(char) * args->lengths[0],
1597 NullS))
1598 ) {
1599 error_num = HA_ERR_OUT_OF_MEM;
1600 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1601 goto error;
1602 }
1603#ifndef WITHOUT_SPIDER_BG_SEARCH
1604 if (bg)
1605 {
1606 bg_direct_sql = (SPIDER_BG_DIRECT_SQL *) initid->ptr;
1607 pthread_mutex_lock(&bg_direct_sql->bg_mutex);
1608 tmp_direct_sql = (SPIDER_DIRECT_SQL *) bg_direct_sql->direct_sql;
1609 bg_direct_sql->direct_sql = direct_sql;
1610 if (tmp_direct_sql)
1611 {
1612 tmp_direct_sql->prev = direct_sql;
1613 direct_sql->next = tmp_direct_sql;
1614 }
1615 pthread_mutex_unlock(&bg_direct_sql->bg_mutex);
1616 direct_sql->bg_mutex = &bg_direct_sql->bg_mutex;
1617 direct_sql->bg_cond = &bg_direct_sql->bg_cond;
1618 direct_sql->parent = bg_direct_sql;
1619 bg_direct_sql->called_cnt++;
1620 }
1621#endif
1622 if (!(trx = spider_get_trx(thd, TRUE, &error_num)))
1623 {
1624 if (error_num == HA_ERR_OUT_OF_MEM)
1625 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1626 goto error;
1627 }
1628 direct_sql->trx = trx;
1629
1630 if (args->args[1])
1631 {
1632 if ((error_num = spider_udf_direct_sql_create_table_list(
1633 direct_sql,
1634 args->args[1],
1635 args->lengths[1]
1636 ))) {
1637 if (error_num == HA_ERR_OUT_OF_MEM)
1638 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1639 goto error;
1640 }
1641 } else {
1642 if ((error_num = spider_udf_direct_sql_create_table_list(
1643 direct_sql,
1644 (char *) "",
1645 0
1646 ))) {
1647 if (error_num == HA_ERR_OUT_OF_MEM)
1648 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1649 goto error;
1650 }
1651 }
1652 if (args->args[2])
1653 {
1654 if ((error_num = spider_udf_parse_direct_sql_param(
1655 trx,
1656 direct_sql,
1657 args->args[2],
1658 args->lengths[2]
1659 ))) {
1660 goto error;
1661 }
1662 } else {
1663 if ((error_num = spider_udf_parse_direct_sql_param(
1664 trx,
1665 direct_sql,
1666 "",
1667 0
1668 ))) {
1669 goto error;
1670 }
1671 }
1672#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1673 if (trx->trx_start && direct_sql->access_mode != 1)
1674 {
1675#endif
1676 trx->updated_in_this_trx = TRUE;
1677 DBUG_PRINT("info",("spider trx->updated_in_this_trx=TRUE"));
1678#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1679 }
1680#endif
1681#if MYSQL_VERSION_ID < 50500
1682#else
1683 use_real_table = spider_param_udf_ds_use_real_table(thd,
1684 direct_sql->use_real_table);
1685#endif
1686 for (roop_count = 0; roop_count < direct_sql->table_count; roop_count++)
1687 {
1688#ifdef SPIDER_NEED_INIT_ONE_TABLE_FOR_FIND_TEMPORARY_TABLE
1689 LEX_CSTRING db_name= { direct_sql->db_names[roop_count],
1690 strlen(direct_sql->db_names[roop_count]) };
1691 LEX_CSTRING tbl_name= { direct_sql->table_names[roop_count],
1692 strlen(direct_sql->table_names[roop_count]) };
1693 table_list.init_one_table(&db_name, &tbl_name, 0, TL_WRITE);
1694#else
1695 table_list.db = direct_sql->db_names[roop_count];
1696 table_list.table_name = direct_sql->table_names[roop_count];
1697#endif
1698 if (!(direct_sql->tables[roop_count] =
1699 SPIDER_find_temporary_table(thd, &table_list)))
1700 {
1701#if MYSQL_VERSION_ID < 50500
1702#else
1703 if (!use_real_table)
1704 {
1705#endif
1706 error_num = ER_SPIDER_UDF_TMP_TABLE_NOT_FOUND_NUM;
1707 my_printf_error(ER_SPIDER_UDF_TMP_TABLE_NOT_FOUND_NUM,
1708 ER_SPIDER_UDF_TMP_TABLE_NOT_FOUND_STR,
1709 MYF(0), table_list.db, table_list.table_name);
1710 goto error;
1711#if MYSQL_VERSION_ID < 50500
1712#else
1713 }
1714 TABLE_LIST *tables = &direct_sql->table_list[roop_count];
1715
1716 table_list.init_one_table(&table_list.db, &table_list.table_name, 0, TL_WRITE);
1717 tables->mdl_request.init(MDL_key::TABLE, table_list.db.str,
1718 table_list.table_name.str, MDL_SHARED_WRITE, MDL_TRANSACTION);
1719 if (!direct_sql->table_list_first)
1720 {
1721 direct_sql->table_list_first = tables;
1722 } else {
1723 real_table_list_last->next_global = tables;
1724 }
1725 real_table_list_last = tables;
1726 spider_set_bit(direct_sql->real_table_bitmap, roop_count);
1727 direct_sql->real_table_used = TRUE;
1728#endif
1729 }
1730 }
1731 if ((error_num = spider_udf_direct_sql_create_conn_key(direct_sql)))
1732 {
1733 if (error_num == HA_ERR_OUT_OF_MEM)
1734 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1735 goto error;
1736 }
1737 if (!(conn = spider_udf_direct_sql_get_conn(direct_sql, trx, &error_num)))
1738 {
1739 if (error_num == HA_ERR_OUT_OF_MEM)
1740 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1741 goto error;
1742 }
1743 conn->error_mode = 0;
1744 direct_sql->conn = conn;
1745 if ((error_num = spider_db_udf_check_and_set_set_names(trx)))
1746 {
1747 if (error_num == HA_ERR_OUT_OF_MEM)
1748 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1749 goto error;
1750 }
1751 if (args->args[0])
1752 {
1753 direct_sql->sql_length = args->lengths[0];
1754 memcpy(sql, args->args[0], direct_sql->sql_length);
1755 } else
1756 direct_sql->sql_length = 0;
1757 direct_sql->sql = sql;
1758
1759#ifndef WITHOUT_SPIDER_BG_SEARCH
1760 if (bg)
1761 {
1762 if ((error_num = spider_udf_bg_direct_sql(direct_sql)))
1763 {
1764 if (error_num == HA_ERR_OUT_OF_MEM)
1765 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1766 goto error;
1767 }
1768 } else {
1769#endif
1770 if (conn->bg_init)
1771 pthread_mutex_lock(&conn->bg_conn_mutex);
1772 if ((error_num = spider_db_udf_direct_sql(direct_sql)))
1773 {
1774 if (conn->bg_init)
1775 pthread_mutex_unlock(&conn->bg_conn_mutex);
1776 if (direct_sql->modified_non_trans_table)
1777 thd->transaction.stmt.modified_non_trans_table = TRUE;
1778 if (error_num == HA_ERR_OUT_OF_MEM)
1779 my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1780 goto error;
1781 }
1782 if (conn->bg_init)
1783 pthread_mutex_unlock(&conn->bg_conn_mutex);
1784 if (direct_sql->modified_non_trans_table)
1785 thd->transaction.stmt.modified_non_trans_table = TRUE;
1786#ifndef WITHOUT_SPIDER_BG_SEARCH
1787 }
1788 if (!bg)
1789 {
1790#endif
1791 spider_udf_free_direct_sql_alloc(direct_sql, FALSE);
1792#ifndef WITHOUT_SPIDER_BG_SEARCH
1793 }
1794#endif
1795 DBUG_RETURN(1);
1796
1797error:
1798 if (direct_sql)
1799 {
1800 if (
1801 direct_sql->error_rw_mode &&
1802 spider_db_conn_is_network_error(error_num)
1803 ) {
1804 SPIDER_RESTORE_DASTATUS;
1805 spider_udf_free_direct_sql_alloc(direct_sql, bg);
1806 DBUG_RETURN(1);
1807 }
1808 spider_udf_free_direct_sql_alloc(direct_sql, bg);
1809 }
1810 *error = 1;
1811 DBUG_RETURN(0);
1812}
1813
1814my_bool spider_direct_sql_init_body(
1815 UDF_INIT *initid,
1816 UDF_ARGS *args,
1817 char *message,
1818 my_bool bg
1819) {
1820 SPIDER_BG_DIRECT_SQL *bg_direct_sql;
1821 DBUG_ENTER("spider_direct_sql_init_body");
1822 if (args->arg_count != 3)
1823 {
1824 strcpy(message, "spider_(bg)_direct_sql() requires 3 arguments");
1825 goto error;
1826 }
1827 if (
1828 args->arg_type[0] != STRING_RESULT ||
1829 args->arg_type[1] != STRING_RESULT ||
1830 args->arg_type[2] != STRING_RESULT
1831 ) {
1832 strcpy(message, "spider_(bg)_direct_sql() requires string arguments");
1833 goto error;
1834 }
1835#ifndef WITHOUT_SPIDER_BG_SEARCH
1836 if (bg)
1837 {
1838 if (!(bg_direct_sql = (SPIDER_BG_DIRECT_SQL *)
1839 spider_malloc(spider_current_trx, 10, sizeof(SPIDER_BG_DIRECT_SQL),
1840 MYF(MY_WME | MY_ZEROFILL)))
1841 ) {
1842 strcpy(message, "spider_bg_direct_sql() out of memory");
1843 goto error;
1844 }
1845#if MYSQL_VERSION_ID < 50500
1846 if (pthread_mutex_init(&bg_direct_sql->bg_mutex, MY_MUTEX_INIT_FAST))
1847#else
1848 if (mysql_mutex_init(spd_key_mutex_bg_direct_sql,
1849 &bg_direct_sql->bg_mutex, MY_MUTEX_INIT_FAST))
1850#endif
1851 {
1852 strcpy(message, "spider_bg_direct_sql() out of memory");
1853 goto error_mutex_init;
1854 }
1855#if MYSQL_VERSION_ID < 50500
1856 if (pthread_cond_init(&bg_direct_sql->bg_cond, NULL))
1857#else
1858 if (mysql_cond_init(spd_key_cond_bg_direct_sql,
1859 &bg_direct_sql->bg_cond, NULL))
1860#endif
1861 {
1862 strcpy(message, "spider_bg_direct_sql() out of memory");
1863 goto error_cond_init;
1864 }
1865 initid->ptr = (char *) bg_direct_sql;
1866 }
1867#endif
1868 DBUG_RETURN(FALSE);
1869
1870#ifndef WITHOUT_SPIDER_BG_SEARCH
1871error_cond_init:
1872 pthread_mutex_destroy(&bg_direct_sql->bg_mutex);
1873error_mutex_init:
1874 spider_free(spider_current_trx, bg_direct_sql, MYF(0));
1875#endif
1876error:
1877 DBUG_RETURN(TRUE);
1878}
1879
1880void spider_direct_sql_deinit_body(
1881 UDF_INIT *initid
1882) {
1883 SPIDER_BG_DIRECT_SQL *bg_direct_sql = (SPIDER_BG_DIRECT_SQL *) initid->ptr;
1884 DBUG_ENTER("spider_direct_sql_deinit_body");
1885 if (bg_direct_sql)
1886 {
1887 pthread_mutex_lock(&bg_direct_sql->bg_mutex);
1888 while (bg_direct_sql->direct_sql)
1889 pthread_cond_wait(&bg_direct_sql->bg_cond, &bg_direct_sql->bg_mutex);
1890 pthread_mutex_unlock(&bg_direct_sql->bg_mutex);
1891 if (bg_direct_sql->modified_non_trans_table)
1892 {
1893 THD *thd = current_thd;
1894 thd->transaction.stmt.modified_non_trans_table = TRUE;
1895 }
1896 pthread_cond_destroy(&bg_direct_sql->bg_cond);
1897 pthread_mutex_destroy(&bg_direct_sql->bg_mutex);
1898 spider_free(spider_current_trx, bg_direct_sql, MYF(0));
1899 }
1900 DBUG_VOID_RETURN;
1901}
1902
1903#ifndef WITHOUT_SPIDER_BG_SEARCH
1904void spider_direct_sql_bg_start(
1905 UDF_INIT *initid
1906) {
1907 SPIDER_BG_DIRECT_SQL *bg_direct_sql = (SPIDER_BG_DIRECT_SQL *) initid->ptr;
1908 DBUG_ENTER("spider_direct_sql_bg_start");
1909 bg_direct_sql->called_cnt = 0;
1910 bg_direct_sql->bg_error = 0;
1911 DBUG_VOID_RETURN;
1912}
1913
1914long long spider_direct_sql_bg_end(
1915 UDF_INIT *initid
1916) {
1917 THD *thd = current_thd;
1918 SPIDER_BG_DIRECT_SQL *bg_direct_sql = (SPIDER_BG_DIRECT_SQL *) initid->ptr;
1919 DBUG_ENTER("spider_direct_sql_bg_end");
1920 pthread_mutex_lock(&bg_direct_sql->bg_mutex);
1921 while (bg_direct_sql->direct_sql)
1922 pthread_cond_wait(&bg_direct_sql->bg_cond, &bg_direct_sql->bg_mutex);
1923 pthread_mutex_unlock(&bg_direct_sql->bg_mutex);
1924 if (bg_direct_sql->modified_non_trans_table)
1925 thd->transaction.stmt.modified_non_trans_table = TRUE;
1926 if (bg_direct_sql->bg_error)
1927 {
1928 my_message(bg_direct_sql->bg_error, bg_direct_sql->bg_error_msg, MYF(0));
1929 DBUG_RETURN(0);
1930 }
1931 DBUG_RETURN(bg_direct_sql->called_cnt);
1932}
1933
1934int spider_udf_bg_direct_sql(
1935 SPIDER_DIRECT_SQL *direct_sql
1936) {
1937 int error_num;
1938 SPIDER_CONN *conn = direct_sql->conn;
1939 DBUG_ENTER("spider_udf_bg_direct_sql");
1940 if ((error_num = spider_create_conn_thread(conn)))
1941 DBUG_RETURN(error_num);
1942 if (!pthread_mutex_trylock(&conn->bg_conn_mutex))
1943 {
1944 DBUG_PRINT("info",("spider get bg_conn_mutex"));
1945 conn->bg_target = direct_sql;
1946 conn->bg_direct_sql = TRUE;
1947 conn->bg_caller_sync_wait = TRUE;
1948 pthread_mutex_lock(&conn->bg_conn_sync_mutex);
1949 pthread_cond_signal(&conn->bg_conn_cond);
1950 pthread_mutex_unlock(&conn->bg_conn_mutex);
1951 pthread_cond_wait(&conn->bg_conn_sync_cond, &conn->bg_conn_sync_mutex);
1952 pthread_mutex_unlock(&conn->bg_conn_sync_mutex);
1953 conn->bg_caller_sync_wait = FALSE;
1954 } else {
1955 DBUG_PRINT("info",("spider get put job stack"));
1956 bool bg_get_job_stack = FALSE;
1957 pthread_mutex_lock(&conn->bg_job_stack_mutex);
1958 uint old_elements = conn->bg_job_stack.max_element;
1959 if (insert_dynamic(&conn->bg_job_stack, (uchar *) &direct_sql))
1960 {
1961 pthread_mutex_unlock(&conn->bg_job_stack_mutex);
1962 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1963 }
1964 if (conn->bg_job_stack.max_element > old_elements)
1965 {
1966 spider_alloc_calc_mem(spider_current_trx,
1967 conn->bg_job_stack,
1968 (conn->bg_job_stack.max_element - old_elements) *
1969 conn->bg_job_stack.size_of_element);
1970 }
1971 if (!conn->bg_get_job_stack_off)
1972 bg_get_job_stack = TRUE;
1973 pthread_mutex_unlock(&conn->bg_job_stack_mutex);
1974 if (bg_get_job_stack)
1975 {
1976 DBUG_PRINT("info",("spider get bg_conn_mutex"));
1977 pthread_mutex_lock(&conn->bg_conn_mutex);
1978 conn->bg_target = NULL;
1979 conn->bg_get_job_stack = TRUE;
1980 conn->bg_direct_sql = TRUE;
1981 conn->bg_caller_sync_wait = TRUE;
1982 pthread_mutex_lock(&conn->bg_conn_sync_mutex);
1983 pthread_cond_signal(&conn->bg_conn_cond);
1984 pthread_mutex_unlock(&conn->bg_conn_mutex);
1985 pthread_cond_wait(&conn->bg_conn_sync_cond, &conn->bg_conn_sync_mutex);
1986 pthread_mutex_unlock(&conn->bg_conn_sync_mutex);
1987 conn->bg_caller_sync_wait = FALSE;
1988 }
1989 }
1990 DBUG_RETURN(0);
1991}
1992#endif
1993