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#include "hs_compat.h"
17#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
18#include "hstcpcli.hpp"
19#endif
20
21#define SPIDER_DBTON_SIZE 15
22
23#define SPIDER_DB_WRAPPER_MYSQL "mysql"
24
25#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100204
26#define PLUGIN_VAR_CAN_MEMALLOC
27/*
28#define ITEM_FUNC_CASE_PARAMS_ARE_PUBLIC
29#define HASH_UPDATE_WITH_HASH_VALUE
30*/
31#else
32#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
33#define HANDLER_HAS_DIRECT_UPDATE_ROWS_WITH_HS
34#endif
35#endif
36
37#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100002
38#define SPIDER_HAS_DISCOVER_TABLE_STRUCTURE
39#define SPIDER_HAS_APPEND_FOR_SINGLE_QUOTE
40#define SPIDER_HAS_SHOW_SIMPLE_FUNC
41#define SPIDER_HAS_JT_HASH_INDEX_MERGE
42#define SPIDER_HAS_EXPR_CACHE_ITEM
43#else
44#define SPIDER_NEED_CHECK_CONDITION_AT_CHECKING_DIRECT_ORDER_LIMIT
45#endif
46
47#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100007
48#define SPIDER_ITEM_HAS_CMP_TYPE
49#endif
50
51#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100004
52#define SPIDER_HAS_TIME_STATUS
53#define SPIDER_HAS_DECIMAL_OPERATION_RESULTS_VALUE_TYPE
54#endif
55
56#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100014
57#define SPIDER_ITEM_STRING_WITHOUT_SET_STR_WITH_COPY
58#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100100
59#define SPIDER_ITEM_STRING_WITHOUT_SET_STR_WITH_COPY_AND_THDPTR
60#endif
61#endif
62
63#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100108
64#define SPIDER_HAS_GROUP_BY_HANDLER
65#endif
66
67#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100200
68#define SPIDER_ORDER_HAS_ENUM_ORDER
69#endif
70
71#if defined(MARIADB_BASE_VERSION)
72#define SPIDER_ITEM_GEOFUNC_NAME_HAS_MBR
73#define SPIDER_HANDLER_AUTO_REPAIR_HAS_ERROR
74#endif
75
76class spider_db_conn;
77typedef spider_db_conn SPIDER_DB_CONN;
78class spider_db_result;
79typedef spider_db_result SPIDER_DB_RESULT;
80class spider_db_row;
81typedef spider_db_row SPIDER_DB_ROW;
82class spider_db_result_buffer;
83typedef spider_db_result_buffer SPIDER_DB_RESULT_BUFFER;
84struct st_spider_conn;
85typedef st_spider_conn SPIDER_CONN;
86struct st_spider_result;
87typedef st_spider_result SPIDER_RESULT;
88
89#define SPIDER_SQL_SEMICOLON_STR ";"
90#define SPIDER_SQL_SEMICOLON_LEN sizeof(SPIDER_SQL_SEMICOLON_STR) - 1
91#define SPIDER_SQL_VALUE_QUOTE_STR "'"
92#define SPIDER_SQL_VALUE_QUOTE_LEN (sizeof(SPIDER_SQL_VALUE_QUOTE_STR) - 1)
93
94#define SPIDER_SQL_DOT_STR "."
95#define SPIDER_SQL_DOT_LEN (sizeof(SPIDER_SQL_DOT_STR) - 1)
96
97#define SPIDER_SQL_EQUAL_STR " = "
98#define SPIDER_SQL_EQUAL_LEN (sizeof(SPIDER_SQL_EQUAL_STR) - 1)
99#define SPIDER_SQL_AND_STR " and "
100#define SPIDER_SQL_AND_LEN (sizeof(SPIDER_SQL_AND_STR) - 1)
101#define SPIDER_SQL_BETWEEN_STR " between "
102#define SPIDER_SQL_BETWEEN_LEN (sizeof(SPIDER_SQL_BETWEEN_STR) - 1)
103
104#define SPIDER_SQL_TABLE_NAME_STR "`table_name`"
105#define SPIDER_SQL_TABLE_NAME_LEN sizeof(SPIDER_SQL_TABLE_NAME_STR) - 1
106
107#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
108#define SPIDER_SQL_HS_EQUAL_STR "="
109#define SPIDER_SQL_HS_EQUAL_LEN (sizeof(SPIDER_SQL_HS_EQUAL_STR) - 1)
110#define SPIDER_SQL_HS_GT_STR ">"
111#define SPIDER_SQL_HS_GT_LEN (sizeof(SPIDER_SQL_HS_GT_STR) - 1)
112#define SPIDER_SQL_HS_GTEQUAL_STR ">="
113#define SPIDER_SQL_HS_GTEQUAL_LEN (sizeof(SPIDER_SQL_HS_GTEQUAL_STR) - 1)
114#define SPIDER_SQL_HS_LT_STR "<"
115#define SPIDER_SQL_HS_LT_LEN (sizeof(SPIDER_SQL_HS_LT_STR) - 1)
116#define SPIDER_SQL_HS_INSERT_STR "+"
117#define SPIDER_SQL_HS_INSERT_LEN (sizeof(SPIDER_SQL_HS_INSERT_STR) - 1)
118#define SPIDER_SQL_HS_UPDATE_STR "U"
119#define SPIDER_SQL_HS_UPDATE_LEN (sizeof(SPIDER_SQL_HS_UPDATE_STR) - 1)
120#define SPIDER_SQL_HS_DELETE_STR "D"
121#define SPIDER_SQL_HS_DELETE_LEN (sizeof(SPIDER_SQL_HS_DELETE_STR) - 1)
122#define SPIDER_SQL_HS_INCREMENT_STR "+"
123#define SPIDER_SQL_HS_INCREMENT_LEN (sizeof(SPIDER_SQL_HS_INCREMENT_STR) - 1)
124#define SPIDER_SQL_HS_DECREMENT_STR "-"
125#define SPIDER_SQL_HS_DECREMENT_LEN (sizeof(SPIDER_SQL_HS_DECREMENT_STR) - 1)
126#endif
127#define SPIDER_SQL_HS_LTEQUAL_STR "<="
128#define SPIDER_SQL_HS_LTEQUAL_LEN (sizeof(SPIDER_SQL_HS_LTEQUAL_STR) - 1)
129
130#ifdef ITEM_FUNC_CASE_PARAMS_ARE_PUBLIC
131#define SPIDER_SQL_CASE_STR "case "
132#define SPIDER_SQL_CASE_LEN (sizeof(SPIDER_SQL_CASE_STR) - 1)
133#define SPIDER_SQL_WHEN_STR " when "
134#define SPIDER_SQL_WHEN_LEN (sizeof(SPIDER_SQL_WHEN_STR) - 1)
135#define SPIDER_SQL_THEN_STR " then "
136#define SPIDER_SQL_THEN_LEN (sizeof(SPIDER_SQL_THEN_STR) - 1)
137#define SPIDER_SQL_ELSE_STR " else "
138#define SPIDER_SQL_ELSE_LEN (sizeof(SPIDER_SQL_ELSE_STR) - 1)
139#define SPIDER_SQL_END_STR " end"
140#define SPIDER_SQL_END_LEN (sizeof(SPIDER_SQL_END_STR) - 1)
141#endif
142
143#define SPIDER_SQL_USING_STR " using "
144#define SPIDER_SQL_USING_LEN (sizeof(SPIDER_SQL_USING_STR) - 1)
145#define SPIDER_SQL_MBR_STR "mbr"
146#define SPIDER_SQL_MBR_LEN (sizeof(SPIDER_SQL_MBR_STR) - 1)
147#define SPIDER_SQL_MBR_EQUAL_STR "mbrequal("
148#define SPIDER_SQL_MBR_EQUAL_LEN (sizeof(SPIDER_SQL_MBR_EQUAL_STR) - 1)
149#define SPIDER_SQL_MBR_CONTAIN_STR "mbrcontains("
150#define SPIDER_SQL_MBR_CONTAIN_LEN (sizeof(SPIDER_SQL_MBR_CONTAIN_STR) - 1)
151#define SPIDER_SQL_MBR_INTERSECT_STR "mbrintersects("
152#define SPIDER_SQL_MBR_INTERSECT_LEN (sizeof(SPIDER_SQL_MBR_INTERSECT_STR) - 1)
153#define SPIDER_SQL_MBR_WITHIN_STR "mbrwithin("
154#define SPIDER_SQL_MBR_WITHIN_LEN (sizeof(SPIDER_SQL_MBR_WITHIN_STR) - 1)
155#define SPIDER_SQL_MBR_DISJOINT_STR "mbrdisjoint("
156#define SPIDER_SQL_MBR_DISJOINT_LEN (sizeof(SPIDER_SQL_MBR_DISJOINT_STR) - 1)
157#define SPIDER_SQL_NOT_BETWEEN_STR "not between"
158#define SPIDER_SQL_NOT_BETWEEN_LEN (sizeof(SPIDER_SQL_NOT_BETWEEN_STR) - 1)
159#define SPIDER_SQL_IN_STR "in("
160#define SPIDER_SQL_IN_LEN (sizeof(SPIDER_SQL_IN_STR) - 1)
161#define SPIDER_SQL_NOT_IN_STR "not in("
162#define SPIDER_SQL_NOT_IN_LEN (sizeof(SPIDER_SQL_NOT_IN_STR) - 1)
163#define SPIDER_SQL_AS_CHAR_STR " as char"
164#define SPIDER_SQL_AS_CHAR_LEN (sizeof(SPIDER_SQL_AS_CHAR_STR) - 1)
165#define SPIDER_SQL_CAST_STR "cast("
166#define SPIDER_SQL_CAST_LEN (sizeof(SPIDER_SQL_CAST_STR) - 1)
167#define SPIDER_SQL_AS_DATETIME_STR " as datetime"
168#define SPIDER_SQL_AS_DATETIME_LEN (sizeof(SPIDER_SQL_AS_DATETIME_STR) - 1)
169#define SPIDER_SQL_AS_DECIMAL_STR " as decimal"
170#define SPIDER_SQL_AS_DECIMAL_LEN (sizeof(SPIDER_SQL_AS_DECIMAL_STR) - 1)
171#define SPIDER_SQL_AS_SIGNED_STR " as signed"
172#define SPIDER_SQL_AS_SIGNED_LEN (sizeof(SPIDER_SQL_AS_SIGNED_STR) - 1)
173#define SPIDER_SQL_AS_UNSIGNED_STR " as unsigned"
174#define SPIDER_SQL_AS_UNSIGNED_LEN (sizeof(SPIDER_SQL_AS_UNSIGNED_STR) - 1)
175#define SPIDER_SQL_AS_DATE_STR " as date"
176#define SPIDER_SQL_AS_DATE_LEN (sizeof(SPIDER_SQL_AS_DATE_STR) - 1)
177#define SPIDER_SQL_AS_TIME_STR " as time"
178#define SPIDER_SQL_AS_TIME_LEN (sizeof(SPIDER_SQL_AS_TIME_STR) - 1)
179#define SPIDER_SQL_AS_BINARY_STR " as binary"
180#define SPIDER_SQL_AS_BINARY_LEN (sizeof(SPIDER_SQL_AS_BINARY_STR) - 1)
181#define SPIDER_SQL_IS_TRUE_STR " is true"
182#define SPIDER_SQL_IS_TRUE_LEN (sizeof(SPIDER_SQL_IS_TRUE_STR) - 1)
183#define SPIDER_SQL_IS_NOT_TRUE_STR " is not true"
184#define SPIDER_SQL_IS_NOT_TRUE_LEN (sizeof(SPIDER_SQL_IS_NOT_TRUE_STR) - 1)
185#define SPIDER_SQL_IS_FALSE_STR " is false"
186#define SPIDER_SQL_IS_FALSE_LEN (sizeof(SPIDER_SQL_IS_FALSE_STR) - 1)
187#define SPIDER_SQL_IS_NOT_FALSE_STR " is not false"
188#define SPIDER_SQL_IS_NOT_FALSE_LEN (sizeof(SPIDER_SQL_IS_NOT_FALSE_STR) - 1)
189#define SPIDER_SQL_NULL_CHAR_STR ""
190#define SPIDER_SQL_NULL_CHAR_LEN (sizeof(SPIDER_SQL_NULL_CHAR_STR) - 1)
191#define SPIDER_SQL_CREATE_TABLE_STR "create table "
192#define SPIDER_SQL_CREATE_TABLE_LEN (sizeof(SPIDER_SQL_CREATE_TABLE_STR) - 1)
193#define SPIDER_SQL_DEFAULT_CHARSET_STR " default charset "
194#define SPIDER_SQL_DEFAULT_CHARSET_LEN (sizeof(SPIDER_SQL_DEFAULT_CHARSET_STR) - 1)
195#define SPIDER_SQL_CHARACTER_SET_STR " character set "
196#define SPIDER_SQL_CHARACTER_SET_LEN (sizeof(SPIDER_SQL_CHARACTER_SET_STR) - 1)
197#define SPIDER_SQL_COLLATE_STR " collate "
198#define SPIDER_SQL_COLLATE_LEN (sizeof(SPIDER_SQL_COLLATE_STR) - 1)
199#define SPIDER_SQL_COMMENT_STR " comment "
200#define SPIDER_SQL_COMMENT_LEN (sizeof(SPIDER_SQL_COMMENT_STR) - 1)
201#define SPIDER_SQL_CONNECTION_STR " connection "
202#define SPIDER_SQL_CONNECTION_LEN (sizeof(SPIDER_SQL_CONNECTION_STR) - 1)
203#define SPIDER_SQL_LCL_NAME_QUOTE_STR "`"
204#define SPIDER_SQL_LCL_NAME_QUOTE_LEN (sizeof(SPIDER_SQL_LCL_NAME_QUOTE_STR) - 1)
205
206#define SPIDER_CONN_KIND_MYSQL (1 << 0)
207#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
208#define SPIDER_CONN_KIND_HS_READ (1 << 2)
209#define SPIDER_CONN_KIND_HS_WRITE (1 << 3)
210#endif
211
212#define SPIDER_SQL_KIND_SQL (1 << 0)
213#define SPIDER_SQL_KIND_HANDLER (1 << 1)
214#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
215#define SPIDER_SQL_KIND_HS (1 << 2)
216#endif
217
218#define SPIDER_SQL_TYPE_SELECT_SQL (1 << 0)
219#define SPIDER_SQL_TYPE_INSERT_SQL (1 << 1)
220#define SPIDER_SQL_TYPE_UPDATE_SQL (1 << 2)
221#define SPIDER_SQL_TYPE_DELETE_SQL (1 << 3)
222#define SPIDER_SQL_TYPE_BULK_UPDATE_SQL (1 << 4)
223#define SPIDER_SQL_TYPE_TMP_SQL (1 << 5)
224#define SPIDER_SQL_TYPE_DROP_TMP_TABLE_SQL (1 << 6)
225#define SPIDER_SQL_TYPE_OTHER_SQL (1 << 7)
226#define SPIDER_SQL_TYPE_HANDLER (1 << 8)
227#define SPIDER_SQL_TYPE_SELECT_HS (1 << 9)
228#define SPIDER_SQL_TYPE_INSERT_HS (1 << 10)
229#define SPIDER_SQL_TYPE_UPDATE_HS (1 << 11)
230#define SPIDER_SQL_TYPE_DELETE_HS (1 << 12)
231#define SPIDER_SQL_TYPE_OTHER_HS (1 << 13)
232
233enum spider_bulk_upd_start {
234 SPD_BU_NOT_START,
235 SPD_BU_START_BY_INDEX_OR_RND_INIT,
236 SPD_BU_START_BY_BULK_INIT
237};
238
239enum spider_index_rnd_init {
240 SPD_NONE,
241 SPD_INDEX,
242 SPD_RND
243};
244
245struct st_spider_ft_info;
246struct st_spider_result;
247typedef struct st_spider_transaction SPIDER_TRX;
248typedef struct st_spider_share SPIDER_SHARE;
249class ha_spider;
250class spider_db_copy_table;
251
252class spider_string
253{
254public:
255 bool mem_calc_inited;
256 String str;
257 uint id;
258 const char *func_name;
259 const char *file_name;
260 ulong line_no;
261 uint32 current_alloc_mem;
262 spider_string *next;
263
264 spider_string();
265 spider_string(
266 uint32 length_arg
267 );
268 spider_string(
269 const char *str,
270 CHARSET_INFO *cs
271 );
272 spider_string(
273 const char *str,
274 uint32 len,
275 CHARSET_INFO *cs
276 );
277 spider_string(
278 char *str,
279 uint32 len,
280 CHARSET_INFO *cs
281 );
282 spider_string(
283 const String &str
284 );
285 ~spider_string();
286 void init_mem_calc(
287 uint id,
288 const char *func_name,
289 const char *file_name,
290 ulong line_no
291 );
292 void mem_calc();
293 String *get_str();
294 void set_charset(
295 CHARSET_INFO *charset_arg
296 );
297 CHARSET_INFO *charset() const;
298 uint32 length() const;
299 uint32 alloced_length() const;
300 char &operator [] (
301 uint32 i
302 ) const;
303 void length(
304 uint32 len
305 );
306 bool is_empty() const;
307 const char *ptr() const;
308 char *c_ptr();
309 char *c_ptr_quick();
310 char *c_ptr_safe();
311 LEX_STRING lex_string() const;
312 void set(
313 String &str,
314 uint32 offset,
315 uint32 arg_length
316 );
317 void set(
318 char *str,
319 uint32 arg_length,
320 CHARSET_INFO *cs
321 );
322 void set(
323 const char *str,
324 uint32 arg_length,
325 CHARSET_INFO *cs
326 );
327 bool set_ascii(
328 const char *str,
329 uint32 arg_length
330 );
331 void set_quick(
332 char *str,
333 uint32 arg_length,
334 CHARSET_INFO *cs
335 );
336 bool set_int(
337 longlong num,
338 bool unsigned_flag,
339 CHARSET_INFO *cs
340 );
341 bool set(
342 longlong num,
343 CHARSET_INFO *cs
344 );
345 bool set(
346 ulonglong num,
347 CHARSET_INFO *cs
348 );
349 bool set_real(
350 double num,
351 uint decimals,
352 CHARSET_INFO *cs
353 );
354 void chop();
355 void free();
356 bool alloc(
357 uint32 arg_length
358 );
359 bool real_alloc(
360 uint32 arg_length
361 );
362 bool realloc(
363 uint32 arg_length
364 );
365 void shrink(
366 uint32 arg_length
367 );
368 bool is_alloced();
369 spider_string& operator = (
370 const String &s
371 );
372 bool copy();
373 bool copy(
374 const spider_string &s
375 );
376 bool copy(
377 const String &s
378 );
379 bool copy(
380 const char *s,
381 uint32 arg_length,
382 CHARSET_INFO *cs
383 );
384 bool needs_conversion(
385 uint32 arg_length,
386 CHARSET_INFO *cs_from,
387 CHARSET_INFO *cs_to,
388 uint32 *offset
389 );
390 bool copy_aligned(
391 const char *s,
392 uint32 arg_length,
393 uint32 offset,
394 CHARSET_INFO *cs
395 );
396 bool set_or_copy_aligned(
397 const char *s,
398 uint32 arg_length,
399 CHARSET_INFO *cs
400 );
401 bool copy(
402 const char *s,
403 uint32 arg_length,
404 CHARSET_INFO *csfrom,
405 CHARSET_INFO *csto,
406 uint *errors
407 );
408 bool append(
409 const spider_string &s
410 );
411 bool append(
412 const String &s
413 );
414 bool append(
415 const char *s
416 );
417 bool append(
418 LEX_STRING *ls
419 );
420 bool append(
421 const char *s,
422 uint32 arg_length
423 );
424 bool append(
425 const char *s,
426 uint32 arg_length,
427 CHARSET_INFO *cs
428 );
429 bool append_ulonglong(
430 ulonglong val
431 );
432 bool append(
433 IO_CACHE *file,
434 uint32 arg_length
435 );
436 bool append_with_prefill(
437 const char *s,
438 uint32 arg_length,
439 uint32 full_length,
440 char fill_char
441 );
442 int strstr(
443 const String &search,
444 uint32 offset = 0
445 );
446 int strrstr(
447 const String &search,
448 uint32 offset = 0
449 );
450 bool replace(
451 uint32 offset,
452 uint32 arg_length,
453 const char *to,
454 uint32 length
455 );
456 bool replace(
457 uint32 offset,
458 uint32 arg_length,
459 const String &to
460 );
461 inline bool append(
462 char chr
463 );
464 bool fill(
465 uint32 max_length,
466 char fill
467 );
468 void strip_sp();
469 uint32 numchars();
470 int charpos(
471 int i,
472 uint32 offset=0
473 );
474 int reserve(
475 uint32 space_needed
476 );
477 int reserve(
478 uint32 space_needed,
479 uint32 grow_by
480 );
481 void q_append(
482 const char c
483 );
484 void q_append(
485 const uint32 n
486 );
487 void q_append(
488 double d
489 );
490 void q_append(
491 double *d
492 );
493 void q_append(
494 const char *data,
495 uint32 data_len
496 );
497 void write_at_position(
498 int position,
499 uint32 value
500 );
501 void qs_append(
502 const char *str,
503 uint32 len
504 );
505 void qs_append(
506 double d
507 );
508 void qs_append(
509 double *d
510 );
511 void qs_append(
512 const char c
513 );
514 void qs_append(
515 int i
516 );
517 void qs_append(
518 uint i
519 );
520 char *prep_append(
521 uint32 arg_length,
522 uint32 step_alloc
523 );
524 bool append(
525 const char *s,
526 uint32 arg_length,
527 uint32 step_alloc
528 );
529 void append_escape_string(
530 const char *st,
531 uint len
532 );
533 bool append_for_single_quote(
534 const char *st,
535 uint len
536 );
537 bool append_for_single_quote(
538 const String *s
539 );
540 bool append_for_single_quote(
541 const char *st
542 );
543 void print(
544 String *print
545 );
546 void swap(
547 spider_string &s
548 );
549 bool uses_buffer_owned_by(
550 const String *s
551 ) const;
552 bool is_ascii() const;
553};
554
555typedef struct spider_table_link_idx_holder SPIDER_TABLE_LINK_IDX_HOLDER;
556typedef struct spider_table_holder SPIDER_TABLE_HOLDER;
557
558typedef struct spider_link_idx_holder
559{
560 spider_table_link_idx_holder *table_link_idx_holder;
561 int link_idx;
562 int link_status;
563 spider_link_idx_holder *next_table;
564 spider_link_idx_holder *next;
565} SPIDER_LINK_IDX_HOLDER;
566
567typedef struct spider_link_idx_chain
568{
569 SPIDER_CONN *conn;
570 spider_link_idx_holder *link_idx_holder;
571 spider_link_idx_holder *current_link_idx_holder;
572 int link_status;
573 spider_link_idx_chain *next;
574} SPIDER_LINK_IDX_CHAIN;
575
576typedef struct spider_table_link_idx_holder
577{
578 spider_table_holder *table_holder;
579 spider_link_idx_holder *first_link_idx_holder;
580 spider_link_idx_holder *last_link_idx_holder;
581 spider_link_idx_holder *current_link_idx_holder;
582 uint link_idx_holder_count;
583} SPIDER_TABLE_LINK_IDX_HOLDER;
584
585typedef struct spider_conn_holder
586{
587 SPIDER_CONN *conn;
588 spider_table_link_idx_holder *table_link_idx_holder;
589 uint link_idx_holder_count_max;
590 bool checked_for_same_conn;
591 long access_balance;
592 spider_conn_holder *prev;
593 spider_conn_holder *next;
594} SPIDER_CONN_HOLDER;
595
596typedef struct spider_table_holder
597{
598 TABLE *table;
599 ha_spider *spider;
600 spider_string *alias;
601} SPIDER_TABLE_HOLDER;
602
603typedef struct spider_field_holder
604{
605 Field *field;
606 ha_spider *spider;
607 spider_string *alias;
608 spider_field_holder *next;
609} SPIDER_FIELD_HOLDER;
610
611typedef struct spider_field_chain
612{
613 spider_field_holder *field_holder;
614 spider_field_chain *next;
615} SPIDER_FIELD_CHAIN;
616
617class spider_fields
618{
619 uint dbton_count;
620 uint current_dbton_num;
621 uint dbton_ids[SPIDER_DBTON_SIZE];
622 uint table_count;
623 uint current_table_num;
624 SPIDER_TABLE_HOLDER *table_holder;
625 SPIDER_LINK_IDX_CHAIN *first_link_idx_chain;
626 SPIDER_LINK_IDX_CHAIN *last_link_idx_chain;
627 SPIDER_LINK_IDX_CHAIN *current_link_idx_chain;
628 SPIDER_LINK_IDX_CHAIN *first_ok_link_idx_chain;
629 SPIDER_CONN_HOLDER *first_conn_holder;
630 SPIDER_CONN_HOLDER *last_conn_holder;
631 SPIDER_CONN_HOLDER *current_conn_holder;
632 SPIDER_FIELD_HOLDER *first_field_holder;
633 SPIDER_FIELD_HOLDER *last_field_holder;
634 SPIDER_FIELD_HOLDER *current_field_holder;
635 SPIDER_FIELD_CHAIN *first_field_chain;
636 SPIDER_FIELD_CHAIN *last_field_chain;
637 SPIDER_FIELD_CHAIN *current_field_chain;
638 Field **first_field_ptr;
639 Field **current_field_ptr;
640public:
641 spider_fields();
642 virtual ~spider_fields();
643 void add_dbton_id(
644 uint dbton_id_arg
645 );
646 void set_pos_to_first_dbton_id();
647 uint get_next_dbton_id();
648 int make_link_idx_chain(
649 int link_status
650 );
651 SPIDER_LINK_IDX_CHAIN *create_link_idx_chain();
652 void set_pos_to_first_link_idx_chain();
653 SPIDER_LINK_IDX_CHAIN *get_next_link_idx_chain();
654 SPIDER_LINK_IDX_HOLDER *get_dup_link_idx_holder(
655 SPIDER_TABLE_LINK_IDX_HOLDER *table_link_idx_holder,
656 SPIDER_LINK_IDX_HOLDER *current
657 );
658 bool check_link_ok_chain();
659 bool is_first_link_ok_chain(
660 SPIDER_LINK_IDX_CHAIN *link_idx_chain_arg
661 );
662 int get_ok_link_idx();
663 void set_first_link_idx();
664 int add_link_idx(
665 SPIDER_CONN_HOLDER *conn_holder_arg,
666 ha_spider *spider_arg,
667 int link_idx
668 );
669 SPIDER_LINK_IDX_HOLDER *create_link_idx_holder();
670 void set_pos_to_first_table_on_link_idx_chain(
671 SPIDER_LINK_IDX_CHAIN *link_idx_chain_arg
672 );
673 SPIDER_LINK_IDX_HOLDER *get_next_table_on_link_idx_chain(
674 SPIDER_LINK_IDX_CHAIN *link_idx_chain_arg
675 );
676 SPIDER_CONN_HOLDER *add_conn(
677 SPIDER_CONN *conn_arg,
678 long access_balance
679 );
680 SPIDER_CONN_HOLDER *create_conn_holder();
681 void set_pos_to_first_conn_holder();
682 SPIDER_CONN_HOLDER *get_next_conn_holder();
683 bool has_conn_holder();
684 void clear_conn_holder_from_conn();
685 bool check_conn_same_conn(
686 SPIDER_CONN *conn_arg
687 );
688 bool remove_conn_if_not_checked();
689 void check_support_dbton(
690 uchar *dbton_bitmap
691 );
692 void choose_a_conn();
693 void free_conn_holder(
694 SPIDER_CONN_HOLDER *conn_holder_arg
695 );
696 SPIDER_TABLE_HOLDER *add_table(
697 ha_spider *spider_arg
698 );
699 int create_table_holder(
700 uint table_count_arg
701 );
702 void set_pos_to_first_table_holder();
703 SPIDER_TABLE_HOLDER *get_next_table_holder();
704 int add_field(Field *field_arg);
705 SPIDER_FIELD_HOLDER *create_field_holder();
706 void set_pos_to_first_field_holder();
707 SPIDER_FIELD_HOLDER *get_next_field_holder();
708 SPIDER_FIELD_CHAIN *create_field_chain();
709 void set_pos_to_first_field_chain();
710 SPIDER_FIELD_CHAIN *get_next_field_chain();
711 void set_field_ptr(Field **field_arg);
712 Field **get_next_field_ptr();
713 int ping_table_mon_from_table(
714 SPIDER_LINK_IDX_CHAIN *link_idx_chain
715 );
716};
717
718#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
719#define SPIDER_HS_UINT32_INFO dena::uint32_info
720#define SPIDER_HS_STRING_REF dena::string_ref
721#ifndef HANDLERSOCKET_MYSQL_UTIL
722#define SPIDER_HS_VECTOR std::vector
723class spider_db_hs_string_ref_buffer
724{
725 SPIDER_HS_VECTOR<SPIDER_HS_STRING_REF> hs_conds;
726public:
727 spider_db_hs_string_ref_buffer();
728 ~spider_db_hs_string_ref_buffer();
729 int init();
730 void clear();
731 int push_back(
732 SPIDER_HS_STRING_REF &cond
733 );
734 SPIDER_HS_STRING_REF *ptr();
735 uint size();
736};
737#else
738class spider_db_hs_string_ref_buffer
739{
740 bool hs_da_init;
741 DYNAMIC_ARRAY hs_conds;
742 uint hs_conds_id;
743 const char *hs_conds_func_name;
744 const char *hs_conds_file_name;
745 ulong hs_conds_line_no;
746public:
747 spider_db_hs_string_ref_buffer();
748 ~spider_db_hs_string_ref_buffer();
749 int init();
750 void clear();
751 int push_back(
752 SPIDER_HS_STRING_REF &cond
753 );
754 SPIDER_HS_STRING_REF *ptr();
755 uint size();
756};
757#endif
758
759class spider_db_hs_str_buffer
760{
761 bool hs_da_init;
762 DYNAMIC_ARRAY hs_conds;
763 uint hs_conds_id;
764 const char *hs_conds_func_name;
765 const char *hs_conds_file_name;
766 ulong hs_conds_line_no;
767public:
768 spider_db_hs_str_buffer();
769 ~spider_db_hs_str_buffer();
770 int init();
771 void clear();
772 spider_string *add(
773 uint *strs_pos,
774 const char *str,
775 uint str_len
776 );
777};
778
779#define SPIDER_DB_HS_STRING_REF_BUFFER spider_db_hs_string_ref_buffer
780#define SPIDER_DB_HS_STR_BUFFER spider_db_hs_str_buffer
781#endif
782
783struct st_spider_db_request_key
784{
785 ulonglong spider_thread_id;
786 query_id_t query_id;
787 void *handler;
788 ulonglong request_id;
789 st_spider_db_request_key *next;
790};
791
792class spider_db_util
793{
794public:
795 spider_db_util() {}
796 virtual ~spider_db_util() {}
797 virtual int append_name(
798 spider_string *str,
799 const char *name,
800 uint name_length
801 ) = 0;
802 virtual int append_name_with_charset(
803 spider_string *str,
804 const char *name,
805 uint name_length,
806 CHARSET_INFO *name_charset
807 ) = 0;
808 virtual bool is_name_quote(
809 const char head_code
810 ) = 0;
811 virtual int append_escaped_name_quote(
812 spider_string *str
813 ) = 0;
814 virtual int append_column_value(
815 ha_spider *spider,
816 spider_string *str,
817 Field *field,
818 const uchar *new_ptr,
819 CHARSET_INFO *access_charset
820 ) = 0;
821 virtual int append_trx_isolation(
822 spider_string *str,
823 int trx_isolation
824 ) = 0;
825 virtual int append_autocommit(
826 spider_string *str,
827 bool autocommit
828 ) = 0;
829 virtual int append_sql_log_off(
830 spider_string *str,
831 bool sql_log_off
832 ) = 0;
833 virtual int append_time_zone(
834 spider_string *str,
835 Time_zone *time_zone
836 ) = 0;
837 virtual int append_start_transaction(
838 spider_string *str
839 ) = 0;
840 virtual int append_xa_start(
841 spider_string *str,
842 XID *xid
843 ) = 0;
844 virtual int append_lock_table_head(
845 spider_string *str
846 ) = 0;
847 virtual int append_lock_table_body(
848 spider_string *str,
849 const char *db_name,
850 uint db_name_length,
851 CHARSET_INFO *db_name_charset,
852 const char *table_name,
853 uint table_name_length,
854 CHARSET_INFO *table_name_charset,
855 int lock_type
856 ) = 0;
857 virtual int append_lock_table_tail(
858 spider_string *str
859 ) = 0;
860 virtual int append_unlock_table(
861 spider_string *str
862 ) = 0;
863 virtual int open_item_func(
864 Item_func *item_func,
865 ha_spider *spider,
866 spider_string *str,
867 const char *alias,
868 uint alias_length,
869 bool use_fields,
870 spider_fields *fields
871 ) = 0;
872#ifdef HANDLER_HAS_DIRECT_AGGREGATE
873 virtual int open_item_sum_func(
874 Item_sum *item_sum,
875 ha_spider *spider,
876 spider_string *str,
877 const char *alias,
878 uint alias_length,
879 bool use_fields,
880 spider_fields *fields
881 ) = 0;
882#endif
883 virtual int append_escaped_util(
884 spider_string *to,
885 String *from
886 ) = 0;
887#ifdef SPIDER_HAS_GROUP_BY_HANDLER
888 virtual int append_from_and_tables(
889 spider_fields *fields,
890 spider_string *str
891 ) = 0;
892 virtual int reappend_tables(
893 spider_fields *fields,
894 SPIDER_LINK_IDX_CHAIN *link_idx_chain,
895 spider_string *str
896 ) = 0;
897 virtual int append_where(
898 spider_string *str
899 ) = 0;
900 virtual int append_having(
901 spider_string *str
902 ) = 0;
903#endif
904};
905
906class spider_db_row
907{
908public:
909 uint dbton_id;
910 SPIDER_DB_ROW *next_pos;
911 spider_db_row(uint in_dbton_id) : dbton_id(in_dbton_id), next_pos(NULL) {}
912 virtual ~spider_db_row() {}
913 virtual int store_to_field(
914 Field *field,
915 CHARSET_INFO *access_charset
916 ) = 0;
917 virtual int append_to_str(
918 spider_string *str
919 ) = 0;
920 virtual int append_escaped_to_str(
921 spider_string *str,
922 uint dbton_id
923 ) = 0;
924 virtual void first() = 0;
925 virtual void next() = 0;
926 virtual bool is_null() = 0;
927 virtual int val_int() = 0;
928 virtual double val_real() = 0;
929 virtual my_decimal *val_decimal(
930 my_decimal *decimal_value,
931 CHARSET_INFO *access_charset
932 ) = 0;
933 virtual SPIDER_DB_ROW *clone() = 0;
934 virtual int store_to_tmp_table(
935 TABLE *tmp_table,
936 spider_string *str
937 ) = 0;
938};
939
940class spider_db_result_buffer
941{
942public:
943 spider_db_result_buffer() {}
944 virtual ~spider_db_result_buffer() {}
945 virtual void clear() = 0;
946 virtual bool check_size(
947 longlong size
948 ) = 0;
949};
950
951class spider_db_result
952{
953protected:
954 SPIDER_DB_CONN *db_conn;
955public:
956 uint dbton_id;
957 spider_db_result(SPIDER_DB_CONN *in_db_conn, uint in_dbton_id) :
958 db_conn(in_db_conn), dbton_id(in_dbton_id) {}
959 virtual ~spider_db_result() {}
960 virtual bool has_result() = 0;
961 virtual void free_result() = 0;
962 virtual SPIDER_DB_ROW *current_row() = 0;
963 virtual SPIDER_DB_ROW *fetch_row() = 0;
964 virtual SPIDER_DB_ROW *fetch_row_from_result_buffer(
965 spider_db_result_buffer *spider_res_buf
966 ) = 0;
967 virtual SPIDER_DB_ROW *fetch_row_from_tmp_table(
968 TABLE *tmp_table
969 ) = 0;
970 virtual int fetch_table_status(
971 int mode,
972 ha_rows &records,
973 ulong &mean_rec_length,
974 ulonglong &data_file_length,
975 ulonglong &max_data_file_length,
976 ulonglong &index_file_length,
977 ulonglong &auto_increment_value,
978 time_t &create_time,
979 time_t &update_time,
980 time_t &check_time
981 ) = 0;
982 virtual int fetch_table_records(
983 int mode,
984 ha_rows &records
985 ) = 0;
986 virtual int fetch_table_cardinality(
987 int mode,
988 TABLE *table,
989 longlong *cardinality,
990 uchar *cardinality_upd,
991 int bitmap_size
992 ) = 0;
993 virtual int fetch_table_mon_status(
994 int &status
995 ) = 0;
996 virtual longlong num_rows() = 0;
997 virtual uint num_fields() = 0;
998 virtual void move_to_pos(
999 longlong pos
1000 ) = 0;
1001 virtual int get_errno() = 0;
1002#ifdef SPIDER_HAS_DISCOVER_TABLE_STRUCTURE
1003 virtual int fetch_columns_for_discover_table_structure(
1004 spider_string *str,
1005 CHARSET_INFO *access_charset
1006 ) = 0;
1007 virtual int fetch_index_for_discover_table_structure(
1008 spider_string *str,
1009 CHARSET_INFO *access_charset
1010 ) = 0;
1011 virtual int fetch_table_for_discover_table_structure(
1012 spider_string *str,
1013 SPIDER_SHARE *spider_share,
1014 CHARSET_INFO *access_charset
1015 ) = 0;
1016#endif
1017};
1018
1019class spider_db_conn
1020{
1021protected:
1022 SPIDER_CONN *conn;
1023public:
1024 spider_db_conn(
1025 SPIDER_CONN *conn
1026 ) : conn(conn) {}
1027 virtual ~spider_db_conn() {}
1028 virtual int init() = 0;
1029 virtual bool is_connected() = 0;
1030 virtual void bg_connect() = 0;
1031 virtual int connect(
1032 char *tgt_host,
1033 char *tgt_username,
1034 char *tgt_password,
1035 long tgt_port,
1036 char *tgt_socket,
1037 char *server_name,
1038 int connect_retry_count,
1039 longlong connect_retry_interval
1040 ) = 0;
1041 virtual int ping() = 0;
1042 virtual void bg_disconnect() = 0;
1043 virtual void disconnect() = 0;
1044 virtual int set_net_timeout() = 0;
1045 virtual int exec_query(
1046 const char *query,
1047 uint length,
1048 int quick_mode
1049 ) = 0;
1050 virtual int get_errno() = 0;
1051 virtual const char *get_error() = 0;
1052 virtual bool is_server_gone_error(
1053 int error_num
1054 ) = 0;
1055 virtual bool is_dup_entry_error(
1056 int error_num
1057 ) = 0;
1058 virtual bool is_xa_nota_error(
1059 int error_num
1060 ) = 0;
1061 virtual spider_db_result *store_result(
1062 spider_db_result_buffer **spider_res_buf,
1063 st_spider_db_request_key *request_key,
1064 int *error_num
1065 ) = 0;
1066 virtual spider_db_result *use_result(
1067 st_spider_db_request_key *request_key,
1068 int *error_num
1069 ) = 0;
1070 virtual int next_result() = 0;
1071 virtual uint affected_rows() = 0;
1072 virtual ulonglong last_insert_id() = 0;
1073 virtual int set_character_set(
1074 const char *csname
1075 ) = 0;
1076 virtual int select_db(
1077 const char *dbname
1078 ) = 0;
1079 virtual int consistent_snapshot(
1080 int *need_mon
1081 ) = 0;
1082 virtual bool trx_start_in_bulk_sql() = 0;
1083 virtual int start_transaction(
1084 int *need_mon
1085 ) = 0;
1086 virtual int commit(
1087 int *need_mon
1088 ) = 0;
1089 virtual int rollback(
1090 int *need_mon
1091 ) = 0;
1092 virtual bool xa_start_in_bulk_sql() = 0;
1093 virtual int xa_start(
1094 XID *xid,
1095 int *need_mon
1096 ) = 0;
1097 virtual int xa_end(
1098 XID *xid,
1099 int *need_mon
1100 ) = 0;
1101 virtual int xa_prepare(
1102 XID *xid,
1103 int *need_mon
1104 ) = 0;
1105 virtual int xa_commit(
1106 XID *xid,
1107 int *need_mon
1108 ) = 0;
1109 virtual int xa_rollback(
1110 XID *xid,
1111 int *need_mon
1112 ) = 0;
1113 virtual bool set_trx_isolation_in_bulk_sql() = 0;
1114 virtual int set_trx_isolation(
1115 int trx_isolation,
1116 int *need_mon
1117 ) = 0;
1118 virtual bool set_autocommit_in_bulk_sql() = 0;
1119 virtual int set_autocommit(
1120 bool autocommit,
1121 int *need_mon
1122 ) = 0;
1123 virtual bool set_sql_log_off_in_bulk_sql() = 0;
1124 virtual int set_sql_log_off(
1125 bool sql_log_off,
1126 int *need_mon
1127 ) = 0;
1128 virtual bool set_time_zone_in_bulk_sql() = 0;
1129 virtual int set_time_zone(
1130 Time_zone *time_zone,
1131 int *need_mon
1132 ) = 0;
1133 virtual int show_master_status(
1134 SPIDER_TRX *trx,
1135 SPIDER_SHARE *share,
1136 int all_link_idx,
1137 int *need_mon,
1138 TABLE *table,
1139 spider_string *str,
1140 int mode,
1141 SPIDER_DB_RESULT **res1,
1142 SPIDER_DB_RESULT **res2
1143 ) = 0;
1144#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1145 virtual int append_sql(
1146 char *sql,
1147 ulong sql_length,
1148 st_spider_db_request_key *request_key
1149 ) = 0;
1150 virtual int append_open_handler(
1151 uint handler_id,
1152 const char *db_name,
1153 const char *table_name,
1154 const char *index_name,
1155 const char *sql,
1156 st_spider_db_request_key *request_key
1157 ) = 0;
1158 virtual int append_select(
1159 uint handler_id,
1160 spider_string *sql,
1161 SPIDER_DB_HS_STRING_REF_BUFFER *keys,
1162 int limit,
1163 int skip,
1164 st_spider_db_request_key *request_key
1165 ) = 0;
1166 virtual int append_insert(
1167 uint handler_id,
1168 SPIDER_DB_HS_STRING_REF_BUFFER *upds,
1169 st_spider_db_request_key *request_key
1170 ) = 0;
1171 virtual int append_update(
1172 uint handler_id,
1173 spider_string *sql,
1174 SPIDER_DB_HS_STRING_REF_BUFFER *keys,
1175 SPIDER_DB_HS_STRING_REF_BUFFER *upds,
1176 int limit,
1177 int skip,
1178 bool increment,
1179 bool decrement,
1180 st_spider_db_request_key *request_key
1181 ) = 0;
1182 virtual int append_delete(
1183 uint handler_id,
1184 spider_string *sql,
1185 SPIDER_DB_HS_STRING_REF_BUFFER *keys,
1186 int limit,
1187 int skip,
1188 st_spider_db_request_key *request_key
1189 ) = 0;
1190 virtual void reset_request_queue() = 0;
1191#endif
1192 virtual size_t escape_string(
1193 char *to,
1194 const char *from,
1195 size_t from_length
1196 ) = 0;
1197 virtual bool have_lock_table_list() = 0;
1198 virtual int append_lock_tables(
1199 spider_string *str
1200 ) = 0;
1201 virtual int append_unlock_tables(
1202 spider_string *str
1203 ) = 0;
1204 virtual uint get_lock_table_hash_count() = 0;
1205 virtual void reset_lock_table_hash() = 0;
1206 virtual uint get_opened_handler_count() = 0;
1207 virtual void reset_opened_handler() = 0;
1208 virtual void set_dup_key_idx(
1209 ha_spider *spider,
1210 int link_idx
1211 ) = 0;
1212 virtual bool cmp_request_key_to_snd(
1213 st_spider_db_request_key *request_key
1214 ) = 0;
1215};
1216
1217class spider_db_share
1218{
1219protected:
1220 uint mem_calc_id;
1221 const char *mem_calc_func_name;
1222 const char *mem_calc_file_name;
1223 ulong mem_calc_line_no;
1224public:
1225 st_spider_share *spider_share;
1226 spider_db_share(st_spider_share *share) : spider_share(share) {}
1227 virtual ~spider_db_share() {}
1228 virtual int init() = 0;
1229 virtual uint get_column_name_length(
1230 uint field_index
1231 ) = 0;
1232 virtual int append_column_name(
1233 spider_string *str,
1234 uint field_index
1235 ) = 0;
1236 virtual int append_column_name_with_alias(
1237 spider_string *str,
1238 uint field_index,
1239 const char *alias,
1240 uint alias_length
1241 ) = 0;
1242 virtual bool need_change_db_table_name() = 0;
1243#ifdef SPIDER_HAS_DISCOVER_TABLE_STRUCTURE
1244 virtual int discover_table_structure(
1245 SPIDER_TRX *trx,
1246 SPIDER_SHARE *spider_share,
1247 spider_string *str
1248 ) = 0;
1249#endif
1250};
1251
1252class spider_db_handler
1253{
1254protected:
1255 uint mem_calc_id;
1256 const char *mem_calc_func_name;
1257 const char *mem_calc_file_name;
1258 ulong mem_calc_line_no;
1259public:
1260 ha_spider *spider;
1261 spider_db_share *db_share;
1262 int first_link_idx;
1263#ifdef SPIDER_HAS_GROUP_BY_HANDLER
1264 SPIDER_LINK_IDX_CHAIN *link_idx_chain;
1265#endif
1266 spider_db_handler(ha_spider *spider, spider_db_share *db_share) :
1267 spider(spider), db_share(db_share), first_link_idx(-1) {}
1268 virtual ~spider_db_handler() {}
1269 virtual int init() = 0;
1270 virtual int append_index_hint(
1271 spider_string *str,
1272 int link_idx,
1273 ulong sql_type
1274 ) = 0;
1275 virtual int append_table_name_with_adjusting(
1276 spider_string *str,
1277 int link_idx,
1278 ulong sql_type
1279 ) = 0;
1280 virtual int append_tmp_table_and_sql_for_bka(
1281 const key_range *start_key
1282 ) = 0;
1283 virtual int reuse_tmp_table_and_sql_for_bka() = 0;
1284 virtual int append_union_table_and_sql_for_bka(
1285 const key_range *start_key
1286 ) = 0;
1287 virtual int reuse_union_table_and_sql_for_bka() = 0;
1288 virtual int append_insert_for_recovery(
1289 ulong sql_type,
1290 int link_idx
1291 ) = 0;
1292 virtual int append_update(
1293 const TABLE *table,
1294 my_ptrdiff_t ptr_diff
1295 ) = 0;
1296 virtual int append_update(
1297 const TABLE *table,
1298 my_ptrdiff_t ptr_diff,
1299 int link_idx
1300 ) = 0;
1301 virtual int append_delete(
1302 const TABLE *table,
1303 my_ptrdiff_t ptr_diff
1304 ) = 0;
1305 virtual int append_delete(
1306 const TABLE *table,
1307 my_ptrdiff_t ptr_diff,
1308 int link_idx
1309 ) = 0;
1310 virtual int append_insert_part() = 0;
1311 virtual int append_update_part() = 0;
1312 virtual int append_delete_part() = 0;
1313#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1314#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
1315 virtual int append_increment_update_set_part() = 0;
1316#endif
1317#endif
1318 virtual int append_update_set_part() = 0;
1319#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
1320 virtual int append_direct_update_set_part() = 0;
1321 virtual int append_dup_update_pushdown_part(
1322 const char *alias,
1323 uint alias_length
1324 ) = 0;
1325 virtual int append_update_columns_part(
1326 const char *alias,
1327 uint alias_length
1328 ) = 0;
1329 virtual int check_update_columns_part() = 0;
1330#endif
1331 virtual int append_select_part(
1332 ulong sql_type
1333 ) = 0;
1334 virtual int append_select(
1335 spider_string *str,
1336 ulong sql_type
1337 ) = 0;
1338 virtual int append_table_select_part(
1339 ulong sql_type
1340 ) = 0;
1341 virtual int append_key_select_part(
1342 ulong sql_type,
1343 uint idx
1344 ) = 0;
1345 virtual int append_minimum_select_part(
1346 ulong sql_type
1347 ) = 0;
1348 virtual int append_hint_after_table_part(
1349 ulong sql_type
1350 ) = 0;
1351 virtual void set_where_pos(
1352 ulong sql_type
1353 ) = 0;
1354 virtual void set_where_to_pos(
1355 ulong sql_type
1356 ) = 0;
1357 virtual int check_item_type(
1358 Item *item
1359 ) = 0;
1360 virtual int append_values_connector_part(
1361 ulong sql_type
1362 ) = 0;
1363 virtual int append_values_terminator_part(
1364 ulong sql_type
1365 ) = 0;
1366 virtual int append_union_table_connector_part(
1367 ulong sql_type
1368 ) = 0;
1369 virtual int append_union_table_terminator_part(
1370 ulong sql_type
1371 ) = 0;
1372 virtual int append_key_column_values_part(
1373 const key_range *start_key,
1374 ulong sql_type
1375 ) = 0;
1376 virtual int append_key_column_values_with_name_part(
1377 const key_range *start_key,
1378 ulong sql_type
1379 ) = 0;
1380 virtual int append_key_where_part(
1381 const key_range *start_key,
1382 const key_range *end_key,
1383 ulong sql_type
1384 ) = 0;
1385 virtual int append_is_null_part(
1386 ulong sql_type,
1387 KEY_PART_INFO *key_part,
1388 const key_range *key,
1389 const uchar **ptr,
1390 bool key_eq,
1391 bool tgt_final
1392 ) = 0;
1393 virtual int append_where_terminator_part(
1394 ulong sql_type,
1395 bool set_order,
1396 int key_count
1397 ) = 0;
1398 virtual int append_match_where_part(
1399 ulong sql_type
1400 ) = 0;
1401 virtual int append_condition_part(
1402 const char *alias,
1403 uint alias_length,
1404 ulong sql_type,
1405 bool test_flg
1406 ) = 0;
1407 virtual int append_match_select_part(
1408 ulong sql_type,
1409 const char *alias,
1410 uint alias_length
1411 ) = 0;
1412#ifdef HANDLER_HAS_DIRECT_AGGREGATE
1413 virtual int append_sum_select_part(
1414 ulong sql_type,
1415 const char *alias,
1416 uint alias_length
1417 ) = 0;
1418#endif
1419 virtual void set_order_pos(
1420 ulong sql_type
1421 ) = 0;
1422 virtual void set_order_to_pos(
1423 ulong sql_type
1424 ) = 0;
1425#ifdef HANDLER_HAS_DIRECT_AGGREGATE
1426 virtual int append_group_by_part(
1427 const char *alias,
1428 uint alias_length,
1429 ulong sql_type
1430 ) = 0;
1431#endif
1432 virtual int append_key_order_for_merge_with_alias_part(
1433 const char *alias,
1434 uint alias_length,
1435 ulong sql_type
1436 ) = 0;
1437 virtual int append_key_order_for_direct_order_limit_with_alias_part(
1438 const char *alias,
1439 uint alias_length,
1440 ulong sql_type
1441 ) = 0;
1442 virtual int append_key_order_with_alias_part(
1443 const char *alias,
1444 uint alias_length,
1445 ulong sql_type
1446 ) = 0;
1447 virtual int append_limit_part(
1448 longlong offset,
1449 longlong limit,
1450 ulong sql_type
1451 ) = 0;
1452 virtual int reappend_limit_part(
1453 longlong offset,
1454 longlong limit,
1455 ulong sql_type
1456 ) = 0;
1457 virtual int append_select_lock_part(
1458 ulong sql_type
1459 ) = 0;
1460 virtual int append_union_all_start_part(
1461 ulong sql_type
1462 ) = 0;
1463 virtual int append_union_all_part(
1464 ulong sql_type
1465 ) = 0;
1466 virtual int append_union_all_end_part(
1467 ulong sql_type
1468 ) = 0;
1469 virtual int append_multi_range_cnt_part(
1470 ulong sql_type,
1471 uint multi_range_cnt,
1472 bool with_comma
1473 ) = 0;
1474 virtual int append_multi_range_cnt_with_name_part(
1475 ulong sql_type,
1476 uint multi_range_cnt
1477 ) = 0;
1478 virtual int append_open_handler_part(
1479 ulong sql_type,
1480 uint handler_id,
1481 SPIDER_CONN *conn,
1482 int link_idx
1483 ) = 0;
1484 virtual int append_close_handler_part(
1485 ulong sql_type,
1486 int link_idx
1487 ) = 0;
1488 virtual int append_insert_terminator_part(
1489 ulong sql_type
1490 ) = 0;
1491 virtual int append_insert_values_part(
1492 ulong sql_type
1493 ) = 0;
1494 virtual int append_into_part(
1495 ulong sql_type
1496 ) = 0;
1497 virtual void set_insert_to_pos(
1498 ulong sql_type
1499 ) = 0;
1500 virtual int append_from_part(
1501 ulong sql_type,
1502 int link_idx
1503 ) = 0;
1504 virtual int append_delete_all_rows_part(
1505 ulong sql_type
1506 ) = 0;
1507 virtual int append_explain_select_part(
1508 key_range *start_key,
1509 key_range *end_key,
1510 ulong sql_type,
1511 int link_idx
1512 ) = 0;
1513 virtual bool is_sole_projection_field(
1514 uint16 field_index
1515 ) = 0;
1516 virtual bool is_bulk_insert_exec_period(
1517 bool bulk_end
1518 ) = 0;
1519 virtual bool sql_is_filled_up(
1520 ulong sql_type
1521 ) = 0;
1522 virtual bool sql_is_empty(
1523 ulong sql_type
1524 ) = 0;
1525 virtual bool support_multi_split_read() = 0;
1526 virtual bool support_bulk_update() = 0;
1527 virtual int bulk_tmp_table_insert() = 0;
1528 virtual int bulk_tmp_table_insert(
1529 int link_idx
1530 ) = 0;
1531 virtual int bulk_tmp_table_end_bulk_insert() = 0;
1532 virtual int bulk_tmp_table_rnd_init() = 0;
1533 virtual int bulk_tmp_table_rnd_next() = 0;
1534 virtual int bulk_tmp_table_rnd_end() = 0;
1535 virtual bool need_copy_for_update(
1536 int link_idx
1537 ) = 0;
1538 virtual bool bulk_tmp_table_created() = 0;
1539 virtual int mk_bulk_tmp_table_and_bulk_start() = 0;
1540 virtual void rm_bulk_tmp_table() = 0;
1541 virtual int insert_lock_tables_list(
1542 SPIDER_CONN *conn,
1543 int link_idx
1544 ) = 0;
1545 virtual int append_lock_tables_list(
1546 SPIDER_CONN *conn,
1547 int link_idx,
1548 int *appended
1549 ) = 0;
1550 virtual int realloc_sql(
1551 ulong *realloced
1552 ) = 0;
1553 virtual int reset_sql(
1554 ulong sql_type
1555 ) = 0;
1556#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1557 virtual int reset_keys(
1558 ulong sql_type
1559 ) = 0;
1560 virtual int reset_upds(
1561 ulong sql_type
1562 ) = 0;
1563 virtual int reset_strs(
1564 ulong sql_type
1565 ) = 0;
1566 virtual int reset_strs_pos(
1567 ulong sql_type
1568 ) = 0;
1569 virtual int push_back_upds(
1570 SPIDER_HS_STRING_REF &info
1571 ) = 0;
1572#endif
1573 virtual bool need_lock_before_set_sql_for_exec(
1574 ulong sql_type
1575 ) = 0;
1576#ifdef SPIDER_HAS_GROUP_BY_HANDLER
1577 virtual int set_sql_for_exec(
1578 ulong sql_type,
1579 int link_idx,
1580 SPIDER_LINK_IDX_CHAIN *link_idx_chain
1581 ) = 0;
1582#endif
1583 virtual int set_sql_for_exec(
1584 ulong sql_type,
1585 int link_idx
1586 ) = 0;
1587 virtual int set_sql_for_exec(
1588 spider_db_copy_table *tgt_ct,
1589 ulong sql_type
1590 ) = 0;
1591 virtual int execute_sql(
1592 ulong sql_type,
1593 SPIDER_CONN *conn,
1594 int quick_mode,
1595 int *need_mon
1596 ) = 0;
1597 virtual int reset() = 0;
1598 virtual int sts_mode_exchange(
1599 int sts_mode
1600 ) = 0;
1601 virtual int show_table_status(
1602 int link_idx,
1603 int sts_mode,
1604 uint flag
1605 ) = 0;
1606 virtual int crd_mode_exchange(
1607 int crd_mode
1608 ) = 0;
1609 virtual int show_index(
1610 int link_idx,
1611 int crd_mode
1612 ) = 0;
1613 virtual int show_records(
1614 int link_idx
1615 ) = 0;
1616 virtual int show_last_insert_id(
1617 int link_idx,
1618 ulonglong &last_insert_id
1619 ) = 0;
1620 virtual ha_rows explain_select(
1621 key_range *start_key,
1622 key_range *end_key,
1623 int link_idx
1624 ) = 0;
1625 virtual int lock_tables(
1626 int link_idx
1627 ) = 0;
1628 virtual int unlock_tables(
1629 int link_idx
1630 ) = 0;
1631 virtual int disable_keys(
1632 SPIDER_CONN *conn,
1633 int link_idx
1634 ) = 0;
1635 virtual int enable_keys(
1636 SPIDER_CONN *conn,
1637 int link_idx
1638 ) = 0;
1639 virtual int check_table(
1640 SPIDER_CONN *conn,
1641 int link_idx,
1642 HA_CHECK_OPT* check_opt
1643 ) = 0;
1644 virtual int repair_table(
1645 SPIDER_CONN *conn,
1646 int link_idx,
1647 HA_CHECK_OPT* check_opt
1648 ) = 0;
1649 virtual int analyze_table(
1650 SPIDER_CONN *conn,
1651 int link_idx
1652 ) = 0;
1653 virtual int optimize_table(
1654 SPIDER_CONN *conn,
1655 int link_idx
1656 ) = 0;
1657 virtual int flush_tables(
1658 SPIDER_CONN *conn,
1659 int link_idx,
1660 bool lock
1661 ) = 0;
1662 virtual int flush_logs(
1663 SPIDER_CONN *conn,
1664 int link_idx
1665 ) = 0;
1666 virtual int insert_opened_handler(
1667 SPIDER_CONN *conn,
1668 int link_idx
1669 ) = 0;
1670 virtual int delete_opened_handler(
1671 SPIDER_CONN *conn,
1672 int link_idx
1673 ) = 0;
1674 virtual int sync_from_clone_source(
1675 spider_db_handler *dbton_hdl
1676 ) = 0;
1677 virtual bool support_use_handler(
1678 int use_handler
1679 ) = 0;
1680 virtual bool minimum_select_bit_is_set(
1681 uint field_index
1682 ) = 0;
1683 virtual void copy_minimum_select_bitmap(
1684 uchar *bitmap
1685 ) = 0;
1686 virtual int init_union_table_name_pos() = 0;
1687 virtual int set_union_table_name_pos() = 0;
1688 virtual int reset_union_table_name(
1689 spider_string *str,
1690 int link_idx,
1691 ulong sql_type
1692 ) = 0;
1693#ifdef SPIDER_HAS_GROUP_BY_HANDLER
1694 virtual int append_from_and_tables_part(
1695 spider_fields *fields,
1696 ulong sql_type
1697 ) = 0;
1698 virtual int reappend_tables_part(
1699 spider_fields *fields,
1700 ulong sql_type
1701 ) = 0;
1702 virtual int append_where_part(
1703 ulong sql_type
1704 ) = 0;
1705 virtual int append_having_part(
1706 ulong sql_type
1707 ) = 0;
1708 virtual int append_item_type_part(
1709 Item *item,
1710 const char *alias,
1711 uint alias_length,
1712 bool use_fields,
1713 spider_fields *fields,
1714 ulong sql_type
1715 ) = 0;
1716 virtual int append_list_item_select_part(
1717 List<Item> *select,
1718 const char *alias,
1719 uint alias_length,
1720 bool use_fields,
1721 spider_fields *fields,
1722 ulong sql_type
1723 ) = 0;
1724 virtual int append_group_by_part(
1725 ORDER *order,
1726 const char *alias,
1727 uint alias_length,
1728 bool use_fields,
1729 spider_fields *fields,
1730 ulong sql_type
1731 ) = 0;
1732 virtual int append_order_by_part(
1733 ORDER *order,
1734 const char *alias,
1735 uint alias_length,
1736 bool use_fields,
1737 spider_fields *fields,
1738 ulong sql_type
1739 ) = 0;
1740#endif
1741};
1742
1743class spider_db_copy_table
1744{
1745public:
1746 spider_db_share *db_share;
1747 spider_db_copy_table(spider_db_share *db_share) :
1748 db_share(db_share) {}
1749 virtual ~spider_db_copy_table() {}
1750 virtual int init() = 0;
1751 virtual void set_sql_charset(
1752 CHARSET_INFO *cs
1753 ) = 0;
1754 virtual int append_select_str() = 0;
1755 virtual int append_insert_str(
1756 int insert_flg
1757 ) = 0;
1758 virtual int append_table_columns(
1759 TABLE_SHARE *table_share
1760 ) = 0;
1761 virtual int append_from_str() = 0;
1762 virtual int append_table_name(
1763 int link_idx
1764 ) = 0;
1765 virtual void set_sql_pos() = 0;
1766 virtual void set_sql_to_pos() = 0;
1767 virtual int append_copy_where(
1768 spider_db_copy_table *source_ct,
1769 KEY *key_info,
1770 ulong *last_row_pos,
1771 ulong *last_lengths
1772 ) = 0;
1773 virtual int append_key_order_str(
1774 KEY *key_info,
1775 int start_pos,
1776 bool desc_flg
1777 ) = 0;
1778 virtual int append_limit(
1779 longlong offset,
1780 longlong limit
1781 ) = 0;
1782 virtual int append_into_str() = 0;
1783 virtual int append_open_paren_str() = 0;
1784 virtual int append_values_str() = 0;
1785 virtual int append_select_lock_str(
1786 int lock_mode
1787 ) = 0;
1788 virtual int exec_query(
1789 SPIDER_CONN *conn,
1790 int quick_mode,
1791 int *need_mon
1792 ) = 0;
1793 virtual int copy_rows(
1794 TABLE *table,
1795 SPIDER_DB_ROW *row,
1796 ulong **last_row_pos,
1797 ulong **last_lengths
1798 ) = 0;
1799 virtual int copy_rows(
1800 TABLE *table,
1801 SPIDER_DB_ROW *row
1802 ) = 0;
1803 virtual int append_insert_terminator() = 0;
1804 virtual int copy_insert_values(
1805 spider_db_copy_table *source_ct
1806 ) = 0;
1807};
1808
1809enum spider_db_access_type
1810{
1811 SPIDER_DB_ACCESS_TYPE_SQL,
1812 SPIDER_DB_ACCESS_TYPE_NOSQL
1813};
1814
1815typedef struct st_spider_dbton
1816{
1817 uint dbton_id;
1818 const char *wrapper;
1819 enum spider_db_access_type db_access_type;
1820 int (*init)();
1821 int (*deinit)();
1822 spider_db_share *(*create_db_share)(st_spider_share *share);
1823 spider_db_handler *(*create_db_handler)(ha_spider *spider,
1824 spider_db_share *db_share);
1825 spider_db_copy_table *(*create_db_copy_table)(
1826 spider_db_share *db_share);
1827 SPIDER_DB_CONN *(*create_db_conn)(SPIDER_CONN *conn);
1828 bool (*support_direct_join)();
1829 spider_db_util *db_util;
1830} SPIDER_DBTON;
1831
1832typedef struct st_spider_position
1833{
1834 SPIDER_DB_ROW *row;
1835 uint pos_mode;
1836 bool use_position;
1837 bool mrr_with_cnt;
1838#ifdef HANDLER_HAS_DIRECT_AGGREGATE
1839 bool direct_aggregate;
1840#endif
1841 uint sql_kind;
1842 uchar *position_bitmap;
1843 st_spider_ft_info *ft_first;
1844 st_spider_ft_info *ft_current;
1845 my_off_t tmp_tbl_pos;
1846 SPIDER_RESULT *result;
1847} SPIDER_POSITION;
1848
1849typedef struct st_spider_condition
1850{
1851 COND *cond;
1852 st_spider_condition *next;
1853} SPIDER_CONDITION;
1854
1855typedef struct st_spider_result
1856{
1857 uint dbton_id;
1858 SPIDER_DB_RESULT *result;
1859#ifndef WITHOUT_SPIDER_BG_SEARCH
1860 volatile
1861#endif
1862 st_spider_result *prev;
1863#ifndef WITHOUT_SPIDER_BG_SEARCH
1864 volatile
1865#endif
1866 st_spider_result *next;
1867 SPIDER_POSITION *first_position; /* for quick mode */
1868 int pos_page_size; /* for quick mode */
1869 longlong record_num;
1870 bool finish_flg;
1871 bool use_position;
1872 bool first_pos_use_position;
1873 bool tmp_tbl_use_position;
1874 uint field_count; /* for quick mode */
1875 TABLE *result_tmp_tbl;
1876 TMP_TABLE_PARAM result_tmp_tbl_prm;
1877 THD *result_tmp_tbl_thd;
1878 uint result_tmp_tbl_inited;
1879 SPIDER_DB_ROW *tmp_tbl_row;
1880} SPIDER_RESULT;
1881
1882typedef struct st_spider_result_list
1883{
1884#ifndef WITHOUT_SPIDER_BG_SEARCH
1885 volatile
1886#endif
1887 SPIDER_RESULT *first;
1888#ifndef WITHOUT_SPIDER_BG_SEARCH
1889 volatile
1890#endif
1891 SPIDER_RESULT *last;
1892#ifndef WITHOUT_SPIDER_BG_SEARCH
1893 volatile
1894#endif
1895 SPIDER_RESULT *current;
1896 KEY *key_info;
1897 int key_order;
1898#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1899 ulonglong hs_upd_rows;
1900 SPIDER_DB_RESULT *hs_result;
1901 SPIDER_DB_RESULT_BUFFER *hs_result_buf;
1902 bool hs_has_result;
1903 SPIDER_DB_CONN *hs_conn;
1904#endif
1905#ifdef HA_CAN_BULK_ACCESS
1906#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1907 uchar *hs_r_bulk_open_index;
1908 uchar *hs_w_bulk_open_index;
1909#endif
1910#endif
1911 spider_string *sqls;
1912 int ha_read_kind;
1913 bool have_sql_kind_backup;
1914 uint *sql_kind_backup;
1915 uint sql_kinds_backup;
1916 bool use_union;
1917 bool use_both_key;
1918 const key_range *end_key;
1919 spider_string *insert_sqls;
1920 spider_string *update_sqls;
1921 TABLE **upd_tmp_tbls;
1922 TMP_TABLE_PARAM *upd_tmp_tbl_prms;
1923 bool tmp_table_join;
1924 uchar *tmp_table_join_first;
1925 bool tmp_tables_created;
1926 uchar *tmp_table_created;
1927 bool tmp_table_join_break_after_get_next;
1928 key_part_map tmp_table_join_key_part_map;
1929 spider_string *tmp_sqls;
1930 bool tmp_reuse_sql;
1931 bool sorted;
1932 bool desc_flg;
1933 longlong current_row_num;
1934 longlong record_num;
1935 bool finish_flg;
1936 longlong limit_num;
1937 longlong internal_offset;
1938 longlong internal_limit;
1939 longlong split_read;
1940 int multi_split_read;
1941 int max_order;
1942 int quick_mode;
1943 longlong quick_page_size;
1944 int low_mem_read;
1945 int bulk_update_mode;
1946 int bulk_update_size;
1947 spider_bulk_upd_start bulk_update_start;
1948 bool check_direct_order_limit;
1949 bool direct_order_limit;
1950 /* the limit_offeset, without where condition */
1951 bool direct_limit_offset;
1952 bool direct_distinct;
1953#ifdef HANDLER_HAS_DIRECT_AGGREGATE
1954 bool direct_aggregate;
1955 bool snap_mrr_with_cnt;
1956 bool snap_direct_aggregate;
1957 SPIDER_DB_ROW *snap_row;
1958#endif
1959 bool in_cmp_ref;
1960 bool set_split_read;
1961 bool insert_dup_update_pushdown;
1962 longlong split_read_base;
1963 double semi_split_read;
1964 longlong semi_split_read_limit;
1965 longlong semi_split_read_base;
1966 longlong first_read;
1967 longlong second_read;
1968 int set_split_read_count;
1969 int *casual_read;
1970#ifndef WITHOUT_SPIDER_BG_SEARCH
1971 /* 0:nomal 1:store 2:store end */
1972 volatile
1973#endif
1974 int quick_phase;
1975 bool keyread;
1976 int lock_type;
1977 TABLE *table;
1978#ifndef WITHOUT_SPIDER_BG_SEARCH
1979 volatile int bgs_error;
1980 bool bgs_error_with_message;
1981 char bgs_error_msg[MYSQL_ERRMSG_SIZE];
1982 volatile bool bgs_working;
1983 /* 0:not use bg 1:first read 2:second read 3:after second read */
1984 volatile int bgs_phase;
1985 volatile longlong bgs_first_read;
1986 volatile longlong bgs_second_read;
1987 volatile longlong bgs_split_read;
1988 volatile
1989#endif
1990 SPIDER_RESULT *bgs_current;
1991 SPIDER_DB_ROW *tmp_pos_row_first;
1992} SPIDER_RESULT_LIST;
1993