1 /* Copyright (C) 2008-2018 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 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 "key.h"
28 #include "sql_base.h"
29 #include "tztime.h"
30 #endif
31 #include "sql_select.h"
32 #include "spd_err.h"
33 #include "spd_param.h"
34 #include "spd_db_include.h"
35 #include "spd_include.h"
36 #include "spd_sys_table.h"
37 #include "spd_malloc.h"
38 
39 extern handlerton *spider_hton_ptr;
40 extern Time_zone *spd_tz_system;
41 
42 /**
43   Insert a Spider system table row.
44 
45   @param  table             The spider system table.
46   @param  do_handle_error   TRUE if an error message should be printed
47                             before returning.
48 
49   @return                   Error code returned by the write.
50 */
51 
spider_write_sys_table_row(TABLE * table,bool do_handle_error=TRUE)52 inline int spider_write_sys_table_row(TABLE *table, bool do_handle_error = TRUE)
53 {
54   int error_num;
55   THD *thd = table->in_use;
56 
57   tmp_disable_binlog(thd); /* Do not replicate the low-level changes. */
58   error_num = table->file->ha_write_row(table->record[0]);
59   reenable_binlog(thd);
60 
61   if (error_num && do_handle_error)
62     table->file->print_error(error_num, MYF(0));
63 
64   return error_num;
65 }
66 
67 /**
68   Update a Spider system table row.
69 
70   @param  table             The spider system table.
71   @param  do_handle_error   TRUE if an error message should be printed
72                             before returning.
73 
74   @return                   Error code returned by the update.
75 */
76 
spider_update_sys_table_row(TABLE * table,bool do_handle_error=TRUE)77 inline int spider_update_sys_table_row(TABLE *table, bool do_handle_error = TRUE)
78 {
79   int error_num;
80   THD *thd = table->in_use;
81 
82   tmp_disable_binlog(thd); /* Do not replicate the low-level changes. */
83   error_num = table->file->ha_update_row(table->record[1], table->record[0]);
84   reenable_binlog(thd);
85 
86   if (error_num && do_handle_error)
87   {
88     if (error_num == HA_ERR_RECORD_IS_THE_SAME)
89       error_num = 0;
90     else
91       table->file->print_error(error_num, MYF(0));
92   }
93 
94   return error_num;
95 }
96 
97 /**
98   Delete a Spider system table row.
99 
100   @param  table             The spider system table.
101   @param  record_number     Location of the record: 0 or 1.
102   @param  do_handle_error   TRUE if an error message should be printed
103                             before returning.
104 
105   @return                   Error code returned by the delete.
106 */
107 
spider_delete_sys_table_row(TABLE * table,int record_number=0,bool do_handle_error=TRUE)108 inline int spider_delete_sys_table_row(TABLE *table, int record_number = 0,
109                                        bool do_handle_error = TRUE)
110 {
111   int error_num;
112   THD *thd = table->in_use;
113 
114   tmp_disable_binlog(thd); /* Do not replicate the low-level changes. */
115   error_num = table->file->ha_delete_row(table->record[record_number]);
116   reenable_binlog(thd);
117 
118   if (error_num && do_handle_error)
119     table->file->print_error(error_num, MYF(0));
120 
121   return error_num;
122 }
123 
124 #if MYSQL_VERSION_ID < 50500
spider_open_sys_table(THD * thd,const char * table_name,int table_name_length,bool write,Open_tables_state * open_tables_backup,bool need_lock,int * error_num)125 TABLE *spider_open_sys_table(
126   THD *thd,
127   const char *table_name,
128   int table_name_length,
129   bool write,
130   Open_tables_state *open_tables_backup,
131   bool need_lock,
132   int *error_num
133 )
134 #else
135 TABLE *spider_open_sys_table(
136   THD *thd,
137   const char *table_name,
138   int table_name_length,
139   bool write,
140   Open_tables_backup *open_tables_backup,
141   bool need_lock,
142   int *error_num
143 )
144 #endif
145 {
146   TABLE *table;
147   TABLE_LIST tables;
148 #if MYSQL_VERSION_ID < 50500
149   TABLE_SHARE *table_share;
150   char table_key[MAX_DBKEY_LENGTH];
151   uint table_key_length;
152 #endif
153   DBUG_ENTER("spider_open_sys_table");
154 
155 #if MYSQL_VERSION_ID < 50500
156   memset(&tables, 0, sizeof(TABLE_LIST));
157   SPIDER_TABLE_LIST_db_str(&tables) = (char*)"mysql";
158   SPIDER_TABLE_LIST_db_length(&tables) = sizeof("mysql") - 1;
159   SPIDER_TABLE_LIST_alias_str(&tables) =
160     SPIDER_TABLE_LIST_table_name_str(&tables) = (char *) table_name;
161   SPIDER_TABLE_LIST_table_name_length(&tables) = table_name_length;
162   tables.lock_type = (write ? TL_WRITE : TL_READ);
163 #else
164 #ifdef SPIDER_use_LEX_CSTRING_for_database_tablename_alias
165   LEX_CSTRING db_name =
166   {
167     "mysql",
168     sizeof("mysql") - 1
169   };
170   LEX_CSTRING tbl_name =
171   {
172     table_name,
173     (size_t) table_name_length
174   };
175   tables.init_one_table(&db_name, &tbl_name, 0, (write ? TL_WRITE : TL_READ));
176 #else
177   tables.init_one_table(
178     "mysql", sizeof("mysql") - 1, table_name, table_name_length, table_name,
179     (write ? TL_WRITE : TL_READ));
180 #endif
181 #endif
182 
183 #if MYSQL_VERSION_ID < 50500
184   if (need_lock)
185   {
186 #endif
187 #if MYSQL_VERSION_ID < 50500
188     if (!(table = open_performance_schema_table(thd, &tables,
189       open_tables_backup)))
190 #else
191     if (!(table = spider_sys_open_table(thd, &tables, open_tables_backup)))
192 #endif
193     {
194       my_printf_error(ER_SPIDER_CANT_OPEN_SYS_TABLE_NUM,
195         ER_SPIDER_CANT_OPEN_SYS_TABLE_STR, MYF(0),
196         "mysql", table_name);
197       *error_num = ER_SPIDER_CANT_OPEN_SYS_TABLE_NUM;
198       DBUG_RETURN(NULL);
199     }
200 #if MYSQL_VERSION_ID < 50500
201   } else {
202     thd->reset_n_backup_open_tables_state(open_tables_backup);
203 
204     if (!(table = (TABLE*) spider_malloc(spider_current_trx, 12,
205       sizeof(*table), MYF(MY_WME))))
206     {
207       *error_num = HA_ERR_OUT_OF_MEM;
208       goto error_malloc;
209     }
210 
211     table_key_length =
212       create_table_def_key(thd, table_key, &tables, FALSE);
213 
214     if (!(table_share = get_table_share(thd,
215       &tables, table_key, table_key_length, 0, error_num)))
216       goto error;
217     if (open_table_from_share(thd, table_share, tables.alias,
218       (uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE | HA_GET_INDEX),
219       READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD,
220       (uint) HA_OPEN_IGNORE_IF_LOCKED | HA_OPEN_FROM_SQL_LAYER,
221       table, FALSE)
222     ) {
223       release_table_share(table_share, RELEASE_NORMAL);
224       my_printf_error(ER_SPIDER_CANT_OPEN_SYS_TABLE_NUM,
225         ER_SPIDER_CANT_OPEN_SYS_TABLE_STR, MYF(0),
226         "mysql", table_name);
227       *error_num = ER_SPIDER_CANT_OPEN_SYS_TABLE_NUM;
228       goto error;
229     }
230   }
231 #endif
232   if (table_name_length == SPIDER_SYS_XA_TABLE_NAME_LEN)
233   {
234     if (
235       !memcmp(table_name,
236         SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN) &&
237       table->s->fields != SPIDER_SYS_XA_COL_CNT
238     ) {
239       spider_close_sys_table(thd, table, open_tables_backup, need_lock);
240       table = NULL;
241       my_printf_error(ER_SPIDER_SYS_TABLE_VERSION_NUM,
242         ER_SPIDER_SYS_TABLE_VERSION_STR, MYF(0),
243         SPIDER_SYS_XA_TABLE_NAME_STR);
244       *error_num = ER_SPIDER_SYS_TABLE_VERSION_NUM;
245       goto error_col_num_chk;
246     }
247   } else if (table_name_length == SPIDER_SYS_XA_MEMBER_TABLE_NAME_LEN)
248   {
249     if (
250       !memcmp(table_name,
251         SPIDER_SYS_XA_MEMBER_TABLE_NAME_STR,
252         SPIDER_SYS_XA_MEMBER_TABLE_NAME_LEN) &&
253       table->s->fields != SPIDER_SYS_XA_MEMBER_COL_CNT
254     ) {
255       spider_close_sys_table(thd, table, open_tables_backup, need_lock);
256       table = NULL;
257       my_printf_error(ER_SPIDER_SYS_TABLE_VERSION_NUM,
258         ER_SPIDER_SYS_TABLE_VERSION_STR, MYF(0),
259         SPIDER_SYS_XA_MEMBER_TABLE_NAME_STR);
260       *error_num = ER_SPIDER_SYS_TABLE_VERSION_NUM;
261       goto error_col_num_chk;
262     }
263   } else if (table_name_length == SPIDER_SYS_TABLES_TABLE_NAME_LEN)
264   {
265     if (
266       !memcmp(table_name,
267         SPIDER_SYS_TABLES_TABLE_NAME_STR,
268         SPIDER_SYS_TABLES_TABLE_NAME_LEN) &&
269       table->s->fields != SPIDER_SYS_TABLES_COL_CNT
270     ) {
271       spider_close_sys_table(thd, table, open_tables_backup, need_lock);
272       table = NULL;
273       my_printf_error(ER_SPIDER_SYS_TABLE_VERSION_NUM,
274         ER_SPIDER_SYS_TABLE_VERSION_STR, MYF(0),
275         SPIDER_SYS_TABLES_TABLE_NAME_STR);
276       *error_num = ER_SPIDER_SYS_TABLE_VERSION_NUM;
277       goto error_col_num_chk;
278     }
279   } else if (table_name_length == SPIDER_SYS_LINK_MON_TABLE_NAME_LEN)
280   {
281     if (
282       !memcmp(table_name,
283         SPIDER_SYS_LINK_MON_TABLE_NAME_STR,
284         SPIDER_SYS_LINK_MON_TABLE_NAME_LEN) &&
285       table->s->fields != SPIDER_SYS_LINK_MON_TABLE_COL_CNT
286     ) {
287       spider_close_sys_table(thd, table, open_tables_backup, need_lock);
288       table = NULL;
289       my_printf_error(ER_SPIDER_SYS_TABLE_VERSION_NUM,
290         ER_SPIDER_SYS_TABLE_VERSION_STR, MYF(0),
291         SPIDER_SYS_LINK_MON_TABLE_NAME_STR);
292       *error_num = ER_SPIDER_SYS_TABLE_VERSION_NUM;
293       goto error_col_num_chk;
294     }
295   } else if (table_name_length == SPIDER_SYS_POS_FOR_RECOVERY_TABLE_NAME_LEN)
296   {
297     if (
298       !memcmp(table_name,
299         SPIDER_SYS_POS_FOR_RECOVERY_TABLE_NAME_STR,
300         SPIDER_SYS_POS_FOR_RECOVERY_TABLE_NAME_LEN) &&
301       table->s->fields != SPIDER_SYS_POS_FOR_RECOVERY_TABLE_COL_CNT
302     ) {
303       spider_close_sys_table(thd, table, open_tables_backup, need_lock);
304       table = NULL;
305       my_printf_error(ER_SPIDER_SYS_TABLE_VERSION_NUM,
306         ER_SPIDER_SYS_TABLE_VERSION_STR, MYF(0),
307         SPIDER_SYS_POS_FOR_RECOVERY_TABLE_NAME_STR);
308       *error_num = ER_SPIDER_SYS_TABLE_VERSION_NUM;
309       goto error_col_num_chk;
310     }
311   }
312   DBUG_RETURN(table);
313 
314 #if MYSQL_VERSION_ID < 50500
315 error:
316   spider_free(spider_current_trx, table, MYF(0));
317 error_malloc:
318   thd->restore_backup_open_tables_state(open_tables_backup);
319 #endif
320 error_col_num_chk:
321   DBUG_RETURN(NULL);
322 }
323 
324 #if MYSQL_VERSION_ID < 50500
spider_close_sys_table(THD * thd,TABLE * table,Open_tables_state * open_tables_backup,bool need_lock)325 void spider_close_sys_table(
326   THD *thd,
327   TABLE *table,
328   Open_tables_state *open_tables_backup,
329   bool need_lock
330 )
331 #else
332 void spider_close_sys_table(
333   THD *thd,
334   TABLE *table,
335   Open_tables_backup *open_tables_backup,
336   bool need_lock
337 )
338 #endif
339 {
340   DBUG_ENTER("spider_close_sys_table");
341 #if MYSQL_VERSION_ID < 50500
342   if (need_lock)
343   {
344     close_performance_schema_table(thd, open_tables_backup);
345   } else {
346     table->file->ha_reset();
347     closefrm(table, TRUE);
348     spider_free(spider_current_trx, table, MYF(0));
349     thd->restore_backup_open_tables_state(open_tables_backup);
350   }
351 #else
352   spider_sys_close_table(thd, open_tables_backup);
353 #endif
354   DBUG_VOID_RETURN;
355 }
356 
357 #if MYSQL_VERSION_ID < 50500
358 #else
spider_sys_open_tables(THD * thd,TABLE_LIST ** tables,Open_tables_backup * open_tables_backup)359 bool spider_sys_open_tables(
360   THD *thd,
361   TABLE_LIST **tables,
362   Open_tables_backup *open_tables_backup
363 ) {
364   uint counter;
365   ulonglong utime_after_lock_backup = thd->utime_after_lock;
366   DBUG_ENTER("spider_sys_open_tables");
367   thd->reset_n_backup_open_tables_state(open_tables_backup);
368   if (open_tables(thd, tables, &counter,
369     MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK | MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY |
370     MYSQL_OPEN_IGNORE_FLUSH | MYSQL_LOCK_IGNORE_TIMEOUT | MYSQL_LOCK_LOG_TABLE
371   )) {
372     thd->restore_backup_open_tables_state(open_tables_backup);
373     thd->utime_after_lock = utime_after_lock_backup;
374     DBUG_RETURN(TRUE);
375   }
376   thd->utime_after_lock = utime_after_lock_backup;
377   DBUG_RETURN(FALSE);
378 }
379 
spider_sys_open_table(THD * thd,TABLE_LIST * tables,Open_tables_backup * open_tables_backup)380 TABLE *spider_sys_open_table(
381   THD *thd,
382   TABLE_LIST *tables,
383   Open_tables_backup *open_tables_backup
384 ) {
385   TABLE *table;
386   ulonglong utime_after_lock_backup = thd->utime_after_lock;
387   DBUG_ENTER("spider_sys_open_table");
388   if (open_tables_backup)
389     thd->reset_n_backup_open_tables_state(open_tables_backup);
390   if ((table = open_ltable(thd, tables, tables->lock_type,
391     MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK | MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY |
392     MYSQL_OPEN_IGNORE_FLUSH | MYSQL_LOCK_IGNORE_TIMEOUT | MYSQL_LOCK_LOG_TABLE
393   ))) {
394     table->use_all_columns();
395     table->s->no_replicate = 1;
396   } else if (open_tables_backup)
397     thd->restore_backup_open_tables_state(open_tables_backup);
398   thd->utime_after_lock = utime_after_lock_backup;
399   DBUG_RETURN(table);
400 }
401 
spider_sys_close_table(THD * thd,Open_tables_backup * open_tables_backup)402 void spider_sys_close_table(
403   THD *thd,
404   Open_tables_backup *open_tables_backup
405 ) {
406   DBUG_ENTER("spider_sys_close_table");
407   close_thread_tables(thd);
408   thd->restore_backup_open_tables_state(open_tables_backup);
409   DBUG_VOID_RETURN;
410 }
411 #endif
412 
spider_sys_index_init(TABLE * table,uint idx,bool sorted)413 int spider_sys_index_init(
414   TABLE *table,
415   uint idx,
416   bool sorted
417 ) {
418   DBUG_ENTER("spider_sys_index_init");
419   DBUG_RETURN(table->file->ha_index_init(idx, sorted));
420 }
421 
spider_sys_index_end(TABLE * table)422 int spider_sys_index_end(
423   TABLE *table
424 ) {
425   DBUG_ENTER("spider_sys_index_end");
426   DBUG_RETURN(table->file->ha_index_end());
427 }
428 
spider_sys_rnd_init(TABLE * table,bool scan)429 int spider_sys_rnd_init(
430   TABLE *table,
431   bool scan
432 ) {
433   DBUG_ENTER("spider_sys_rnd_init");
434   DBUG_RETURN(table->file->ha_rnd_init(scan));
435 }
436 
spider_sys_rnd_end(TABLE * table)437 int spider_sys_rnd_end(
438   TABLE *table
439 ) {
440   DBUG_ENTER("spider_sys_rnd_end");
441   DBUG_RETURN(table->file->ha_rnd_end());
442 }
443 
spider_check_sys_table(TABLE * table,char * table_key)444 int spider_check_sys_table(
445   TABLE *table,
446   char *table_key
447 ) {
448   DBUG_ENTER("spider_check_sys_table");
449 
450   key_copy(
451     (uchar *) table_key,
452     table->record[0],
453     table->key_info,
454     table->key_info->key_length);
455 
456 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
457   DBUG_RETURN(table->file->ha_index_read_idx_map(
458     table->record[0], 0, (uchar *) table_key,
459     HA_WHOLE_KEY, HA_READ_KEY_EXACT));
460 #else
461   DBUG_RETURN(table->file->index_read_idx_map(
462     table->record[0], 0, (uchar *) table_key,
463     HA_WHOLE_KEY, HA_READ_KEY_EXACT));
464 #endif
465 }
466 
spider_check_sys_table_with_find_flag(TABLE * table,char * table_key,enum ha_rkey_function find_flag)467 int spider_check_sys_table_with_find_flag(
468   TABLE *table,
469   char *table_key,
470   enum ha_rkey_function find_flag
471 ) {
472   DBUG_ENTER("spider_check_sys_table");
473 
474   key_copy(
475     (uchar *) table_key,
476     table->record[0],
477     table->key_info,
478     table->key_info->key_length);
479 
480 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
481   DBUG_RETURN(table->file->ha_index_read_idx_map(
482     table->record[0], 0, (uchar *) table_key,
483     HA_WHOLE_KEY, find_flag));
484 #else
485   DBUG_RETURN(table->file->index_read_idx_map(
486     table->record[0], 0, (uchar *) table_key,
487     HA_WHOLE_KEY, find_flag));
488 #endif
489 }
490 
spider_check_sys_table_for_update_all_columns(TABLE * table,char * table_key)491 int spider_check_sys_table_for_update_all_columns(
492   TABLE *table,
493   char *table_key
494 ) {
495   DBUG_ENTER("spider_check_sys_table_for_update_all_columns");
496 
497   key_copy(
498     (uchar *) table_key,
499     table->record[0],
500     table->key_info,
501     table->key_info->key_length);
502 
503 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
504   DBUG_RETURN(table->file->ha_index_read_idx_map(
505     table->record[1], 0, (uchar *) table_key,
506     HA_WHOLE_KEY, HA_READ_KEY_EXACT));
507 #else
508   DBUG_RETURN(table->file->index_read_idx_map(
509     table->record[1], 0, (uchar *) table_key,
510     HA_WHOLE_KEY, HA_READ_KEY_EXACT));
511 #endif
512 }
513 
spider_get_sys_table_by_idx(TABLE * table,char * table_key,const int idx,const int col_count)514 int spider_get_sys_table_by_idx(
515   TABLE *table,
516   char *table_key,
517   const int idx,
518   const int col_count
519 ) {
520   int error_num;
521   uint key_length;
522   KEY *key_info = table->key_info + idx;
523   DBUG_ENTER("spider_get_sys_table_by_idx");
524   if ((error_num = spider_sys_index_init(table, idx, FALSE)))
525     DBUG_RETURN(error_num);
526 
527   if ((int) spider_user_defined_key_parts(key_info) == col_count)
528   {
529     key_length = key_info->key_length;
530   } else {
531     int roop_count;
532     key_length = 0;
533     for (roop_count = 0; roop_count < col_count; ++roop_count)
534     {
535       key_length += key_info->key_part[roop_count].store_length;
536     }
537   }
538 
539   key_copy(
540     (uchar *) table_key,
541     table->record[0],
542     key_info,
543     key_length);
544 
545   if (
546 /*
547 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
548     (error_num = table->file->ha_index_read_idx_map(
549       table->record[0], idx, (uchar *) table_key,
550       make_prev_keypart_map(col_count), HA_READ_KEY_EXACT))
551 #else
552     (error_num = table->file->index_read_idx_map(
553       table->record[0], idx, (uchar *) table_key,
554       make_prev_keypart_map(col_count), HA_READ_KEY_EXACT))
555 #endif
556 */
557 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
558     (error_num = table->file->ha_index_read_map(
559       table->record[0], (uchar *) table_key,
560       make_prev_keypart_map(col_count), HA_READ_KEY_EXACT))
561 #else
562     (error_num = table->file->index_read_map(
563       table->record[0], (uchar *) table_key,
564       make_prev_keypart_map(col_count), HA_READ_KEY_EXACT))
565 #endif
566   ) {
567     spider_sys_index_end(table);
568     DBUG_RETURN(error_num);
569   }
570   DBUG_RETURN(0);
571 }
572 
spider_sys_index_next_same(TABLE * table,char * table_key)573 int spider_sys_index_next_same(
574   TABLE *table,
575   char *table_key
576 ) {
577   DBUG_ENTER("spider_sys_index_next_same");
578 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
579   DBUG_RETURN(table->file->ha_index_next_same(
580     table->record[0],
581     (const uchar*) table_key,
582     table->key_info->key_length));
583 #else
584   DBUG_RETURN(table->file->index_next_same(
585     table->record[0],
586     (const uchar*) table_key,
587     table->key_info->key_length));
588 #endif
589 }
590 
spider_sys_index_first(TABLE * table,const int idx)591 int spider_sys_index_first(
592   TABLE *table,
593   const int idx
594 ) {
595   int error_num;
596   DBUG_ENTER("spider_sys_index_first");
597   if ((error_num = spider_sys_index_init(table, idx, FALSE)))
598     DBUG_RETURN(error_num);
599 
600   if (
601 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
602     (error_num = table->file->ha_index_first(table->record[0]))
603 #else
604     (error_num = table->file->index_first(table->record[0]))
605 #endif
606   ) {
607     spider_sys_index_end(table);
608     DBUG_RETURN(error_num);
609   }
610   DBUG_RETURN(0);
611 }
612 
spider_sys_index_last(TABLE * table,const int idx)613 int spider_sys_index_last(
614   TABLE *table,
615   const int idx
616 ) {
617   int error_num;
618   DBUG_ENTER("spider_sys_index_last");
619   if ((error_num = spider_sys_index_init(table, idx, FALSE)))
620     DBUG_RETURN(error_num);
621 
622   if (
623 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
624     (error_num = table->file->ha_index_last(table->record[0]))
625 #else
626     (error_num = table->file->index_last(table->record[0]))
627 #endif
628   ) {
629     spider_sys_index_end(table);
630     DBUG_RETURN(error_num);
631   }
632   DBUG_RETURN(0);
633 }
634 
spider_sys_index_next(TABLE * table)635 int spider_sys_index_next(
636   TABLE *table
637 ) {
638   DBUG_ENTER("spider_sys_index_next");
639 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
640   DBUG_RETURN(table->file->ha_index_next(table->record[0]));
641 #else
642   DBUG_RETURN(table->file->index_next(table->record[0]));
643 #endif
644 }
645 
spider_store_xa_pk(TABLE * table,XID * xid)646 void spider_store_xa_pk(
647   TABLE *table,
648   XID *xid
649 ) {
650   DBUG_ENTER("spider_store_xa_pk");
651   table->field[0]->store(xid->formatID);
652   table->field[1]->store(xid->gtrid_length);
653   table->field[3]->store(
654     xid->data,
655     (uint) xid->gtrid_length + xid->bqual_length,
656     system_charset_info);
657   DBUG_VOID_RETURN;
658 }
659 
spider_store_xa_bqual_length(TABLE * table,XID * xid)660 void spider_store_xa_bqual_length(
661   TABLE *table,
662   XID *xid
663 ) {
664   DBUG_ENTER("spider_store_xa_bqual_length");
665   table->field[2]->store(xid->bqual_length);
666   DBUG_VOID_RETURN;
667 }
668 
spider_store_xa_status(TABLE * table,const char * status)669 void spider_store_xa_status(
670   TABLE *table,
671   const char *status
672 ) {
673   DBUG_ENTER("spider_store_xa_status");
674   table->field[4]->store(
675     status,
676     (uint) strlen(status),
677     system_charset_info);
678   DBUG_VOID_RETURN;
679 }
680 
spider_store_xa_member_pk(TABLE * table,XID * xid,SPIDER_CONN * conn)681 void spider_store_xa_member_pk(
682   TABLE *table,
683   XID *xid,
684   SPIDER_CONN *conn
685 ) {
686   DBUG_ENTER("spider_store_xa_member_pk");
687   table->field[0]->store(xid->formatID);
688   table->field[1]->store(xid->gtrid_length);
689   table->field[3]->store(
690     xid->data,
691     (uint) xid->gtrid_length + xid->bqual_length,
692     system_charset_info);
693   table->field[5]->store(
694     conn->tgt_host,
695     (uint) conn->tgt_host_length,
696     system_charset_info);
697   table->field[6]->store(
698     conn->tgt_port);
699   table->field[7]->store(
700     conn->tgt_socket,
701     (uint) conn->tgt_socket_length,
702     system_charset_info);
703   DBUG_VOID_RETURN;
704 }
705 
spider_store_xa_member_info(TABLE * table,XID * xid,SPIDER_CONN * conn)706 void spider_store_xa_member_info(
707   TABLE *table,
708   XID *xid,
709   SPIDER_CONN *conn
710 ) {
711   DBUG_ENTER("spider_store_xa_member_info");
712   table->field[2]->store(xid->bqual_length);
713   table->field[4]->store(
714     conn->tgt_wrapper,
715     (uint) conn->tgt_wrapper_length,
716     system_charset_info);
717   table->field[8]->store(
718     conn->tgt_username,
719     (uint) conn->tgt_username_length,
720     system_charset_info);
721   table->field[9]->store(
722     conn->tgt_password,
723     (uint) conn->tgt_password_length,
724     system_charset_info);
725   if (conn->tgt_ssl_ca)
726   {
727     table->field[10]->set_notnull();
728     table->field[10]->store(
729       conn->tgt_ssl_ca,
730       (uint) conn->tgt_ssl_ca_length,
731       system_charset_info);
732   } else {
733     table->field[10]->set_null();
734     table->field[10]->reset();
735   }
736   if (conn->tgt_ssl_capath)
737   {
738     table->field[11]->set_notnull();
739     table->field[11]->store(
740       conn->tgt_ssl_capath,
741       (uint) conn->tgt_ssl_capath_length,
742       system_charset_info);
743   } else {
744     table->field[11]->set_null();
745     table->field[11]->reset();
746   }
747   if (conn->tgt_ssl_cert)
748   {
749     table->field[12]->set_notnull();
750     table->field[12]->store(
751       conn->tgt_ssl_cert,
752       (uint) conn->tgt_ssl_cert_length,
753       system_charset_info);
754   } else {
755     table->field[12]->set_null();
756     table->field[12]->reset();
757   }
758   if (conn->tgt_ssl_cipher)
759   {
760     table->field[13]->set_notnull();
761     table->field[13]->store(
762       conn->tgt_ssl_cipher,
763       (uint) conn->tgt_ssl_cipher_length,
764       system_charset_info);
765   } else {
766     table->field[13]->set_null();
767     table->field[13]->reset();
768   }
769   if (conn->tgt_ssl_key)
770   {
771     table->field[14]->set_notnull();
772     table->field[14]->store(
773       conn->tgt_ssl_key,
774       (uint) conn->tgt_ssl_key_length,
775       system_charset_info);
776   } else {
777     table->field[14]->set_null();
778     table->field[14]->reset();
779   }
780   if (conn->tgt_ssl_vsc >= 0)
781   {
782     table->field[15]->set_notnull();
783     table->field[15]->store(
784       conn->tgt_ssl_vsc);
785   } else {
786     table->field[15]->set_null();
787     table->field[15]->reset();
788   }
789   if (conn->tgt_default_file)
790   {
791     table->field[16]->set_notnull();
792     table->field[16]->store(
793       conn->tgt_default_file,
794       (uint) conn->tgt_default_file_length,
795       system_charset_info);
796   } else {
797     table->field[16]->set_null();
798     table->field[16]->reset();
799   }
800   if (conn->tgt_default_group)
801   {
802     table->field[17]->set_notnull();
803     table->field[17]->store(
804       conn->tgt_default_group,
805       (uint) conn->tgt_default_group_length,
806       system_charset_info);
807   } else {
808     table->field[17]->set_null();
809     table->field[17]->reset();
810   }
811   DBUG_VOID_RETURN;
812 }
813 
spider_store_tables_name(TABLE * table,const char * name,const uint name_length)814 void spider_store_tables_name(
815   TABLE *table,
816   const char *name,
817   const uint name_length
818 ) {
819   const char *ptr_db, *ptr_table;
820   my_ptrdiff_t ptr_diff_db, ptr_diff_table;
821   DBUG_ENTER("spider_store_tables_name");
822   if (name[0] == FN_CURLIB && name[1] == FN_LIBCHAR)
823   {
824     ptr_db = strchr(name, FN_LIBCHAR);
825     ptr_db++;
826     ptr_diff_db = PTR_BYTE_DIFF(ptr_db, name);
827     DBUG_PRINT("info",("spider ptr_diff_db = %lld", (longlong) ptr_diff_db));
828     ptr_table = strchr(ptr_db, FN_LIBCHAR);
829     ptr_table++;
830     ptr_diff_table = PTR_BYTE_DIFF(ptr_table, ptr_db);
831     DBUG_PRINT("info",("spider ptr_diff_table = %lld",
832       (longlong) ptr_diff_table));
833   } else {
834     DBUG_PRINT("info",("spider temporary table"));
835     ptr_db = "";
836     ptr_diff_db = 1;
837     ptr_table = "";
838     ptr_diff_table = 1;
839   }
840   table->field[0]->store(
841     ptr_db,
842     (uint)(ptr_diff_table - 1),
843     system_charset_info);
844   DBUG_PRINT("info",("spider field[0]->null_bit = %d",
845     table->field[0]->null_bit));
846   table->field[1]->store(
847     ptr_table,
848     (uint) ((my_ptrdiff_t) name_length - ptr_diff_db - ptr_diff_table),
849     system_charset_info);
850   DBUG_PRINT("info",("spider field[1]->null_bit = %d",
851     table->field[1]->null_bit));
852   DBUG_VOID_RETURN;
853 }
854 
spider_store_db_and_table_name(TABLE * table,const char * db_name,const uint db_name_length,const char * table_name,const uint table_name_length)855 void spider_store_db_and_table_name(
856   TABLE *table,
857   const char *db_name,
858   const uint db_name_length,
859   const char *table_name,
860   const uint table_name_length
861 ) {
862   DBUG_ENTER("spider_store_db_and_table_name");
863   table->field[0]->store(
864     db_name,
865     db_name_length,
866     system_charset_info);
867   DBUG_PRINT("info",("spider field[0]->null_bit = %d",
868     table->field[0]->null_bit));
869   table->field[1]->store(
870     table_name,
871     table_name_length,
872     system_charset_info);
873   DBUG_PRINT("info",("spider field[1]->null_bit = %d",
874     table->field[1]->null_bit));
875   DBUG_VOID_RETURN;
876 }
877 
spider_store_tables_link_idx(TABLE * table,int link_idx)878 void spider_store_tables_link_idx(
879   TABLE *table,
880   int link_idx
881 ) {
882   DBUG_ENTER("spider_store_tables_link_idx");
883   table->field[2]->set_notnull();
884   table->field[2]->store(link_idx);
885   DBUG_VOID_RETURN;
886 }
887 
spider_store_tables_link_idx_str(TABLE * table,const char * link_idx,const uint link_idx_length)888 void spider_store_tables_link_idx_str(
889   TABLE *table,
890   const char *link_idx,
891   const uint link_idx_length
892 ) {
893   DBUG_ENTER("spider_store_tables_link_idx_str");
894   table->field[2]->store(
895     link_idx,
896     link_idx_length,
897     system_charset_info);
898   DBUG_PRINT("info",("spider field[2]->null_bit = %d",
899     table->field[2]->null_bit));
900   DBUG_VOID_RETURN;
901 }
902 
spider_store_tables_static_link_id(TABLE * table,const char * static_link_id,const uint static_link_id_length)903 void spider_store_tables_static_link_id(
904   TABLE *table,
905   const char *static_link_id,
906   const uint static_link_id_length
907 ) {
908   DBUG_ENTER("spider_store_tables_static_link_id");
909   if (static_link_id)
910   {
911     table->field[24]->set_notnull();
912     table->field[24]->store(
913       static_link_id,
914       static_link_id_length,
915       system_charset_info);
916   } else {
917     table->field[24]->set_null();
918     table->field[24]->reset();
919   }
920   DBUG_VOID_RETURN;
921 }
922 
spider_store_tables_priority(TABLE * table,longlong priority)923 void spider_store_tables_priority(
924   TABLE *table,
925   longlong priority
926 ) {
927   DBUG_ENTER("spider_store_tables_priority");
928   DBUG_PRINT("info",("spider priority = %lld", priority));
929   table->field[3]->store(priority, FALSE);
930   DBUG_VOID_RETURN;
931 }
932 
spider_store_tables_connect_info(TABLE * table,SPIDER_ALTER_TABLE * alter_table,int link_idx)933 void spider_store_tables_connect_info(
934   TABLE *table,
935   SPIDER_ALTER_TABLE *alter_table,
936   int link_idx
937 ) {
938   DBUG_ENTER("spider_store_tables_connect_info");
939   if (alter_table->tmp_server_names[link_idx])
940   {
941     table->field[4]->set_notnull();
942     table->field[4]->store(
943       alter_table->tmp_server_names[link_idx],
944       (uint) alter_table->tmp_server_names_lengths[link_idx],
945       system_charset_info);
946   } else {
947     table->field[4]->set_null();
948     table->field[4]->reset();
949   }
950   if (alter_table->tmp_tgt_wrappers[link_idx])
951   {
952     table->field[5]->set_notnull();
953     table->field[5]->store(
954       alter_table->tmp_tgt_wrappers[link_idx],
955       (uint) alter_table->tmp_tgt_wrappers_lengths[link_idx],
956       system_charset_info);
957   } else {
958     table->field[5]->set_null();
959     table->field[5]->reset();
960   }
961   if (alter_table->tmp_tgt_hosts[link_idx])
962   {
963     table->field[6]->set_notnull();
964     table->field[6]->store(
965       alter_table->tmp_tgt_hosts[link_idx],
966       (uint) alter_table->tmp_tgt_hosts_lengths[link_idx],
967       system_charset_info);
968   } else {
969     table->field[6]->set_null();
970     table->field[6]->reset();
971   }
972   if (alter_table->tmp_tgt_ports[link_idx] >= 0)
973   {
974     table->field[7]->set_notnull();
975     table->field[7]->store(
976       alter_table->tmp_tgt_ports[link_idx]);
977   } else {
978     table->field[7]->set_null();
979     table->field[7]->reset();
980   }
981   if (alter_table->tmp_tgt_sockets[link_idx])
982   {
983     table->field[8]->set_notnull();
984     table->field[8]->store(
985       alter_table->tmp_tgt_sockets[link_idx],
986       (uint) alter_table->tmp_tgt_sockets_lengths[link_idx],
987       system_charset_info);
988   } else {
989     table->field[8]->set_null();
990     table->field[8]->reset();
991   }
992   if (alter_table->tmp_tgt_usernames[link_idx])
993   {
994     table->field[9]->set_notnull();
995     table->field[9]->store(
996       alter_table->tmp_tgt_usernames[link_idx],
997       (uint) alter_table->tmp_tgt_usernames_lengths[link_idx],
998       system_charset_info);
999   } else {
1000     table->field[9]->set_null();
1001     table->field[9]->reset();
1002   }
1003   if (alter_table->tmp_tgt_passwords[link_idx])
1004   {
1005     table->field[10]->set_notnull();
1006     table->field[10]->store(
1007       alter_table->tmp_tgt_passwords[link_idx],
1008       (uint) alter_table->tmp_tgt_passwords_lengths[link_idx],
1009       system_charset_info);
1010   } else {
1011     table->field[10]->set_null();
1012     table->field[10]->reset();
1013   }
1014   if (alter_table->tmp_tgt_ssl_cas[link_idx])
1015   {
1016     table->field[11]->set_notnull();
1017     table->field[11]->store(
1018       alter_table->tmp_tgt_ssl_cas[link_idx],
1019       (uint) alter_table->tmp_tgt_ssl_cas_lengths[link_idx],
1020       system_charset_info);
1021   } else {
1022     table->field[11]->set_null();
1023     table->field[11]->reset();
1024   }
1025   if (alter_table->tmp_tgt_ssl_capaths[link_idx])
1026   {
1027     table->field[12]->set_notnull();
1028     table->field[12]->store(
1029       alter_table->tmp_tgt_ssl_capaths[link_idx],
1030       (uint) alter_table->tmp_tgt_ssl_capaths_lengths[link_idx],
1031       system_charset_info);
1032   } else {
1033     table->field[12]->set_null();
1034     table->field[12]->reset();
1035   }
1036   if (alter_table->tmp_tgt_ssl_certs[link_idx])
1037   {
1038     table->field[13]->set_notnull();
1039     table->field[13]->store(
1040       alter_table->tmp_tgt_ssl_certs[link_idx],
1041       (uint) alter_table->tmp_tgt_ssl_certs_lengths[link_idx],
1042       system_charset_info);
1043   } else {
1044     table->field[13]->set_null();
1045     table->field[13]->reset();
1046   }
1047   if (alter_table->tmp_tgt_ssl_ciphers[link_idx])
1048   {
1049     table->field[14]->set_notnull();
1050     table->field[14]->store(
1051       alter_table->tmp_tgt_ssl_ciphers[link_idx],
1052       (uint) alter_table->tmp_tgt_ssl_ciphers_lengths[link_idx],
1053       system_charset_info);
1054   } else {
1055     table->field[14]->set_null();
1056     table->field[14]->reset();
1057   }
1058   if (alter_table->tmp_tgt_ssl_keys[link_idx])
1059   {
1060     table->field[15]->set_notnull();
1061     table->field[15]->store(
1062       alter_table->tmp_tgt_ssl_keys[link_idx],
1063       (uint) alter_table->tmp_tgt_ssl_keys_lengths[link_idx],
1064       system_charset_info);
1065   } else {
1066     table->field[15]->set_null();
1067     table->field[15]->reset();
1068   }
1069   if (alter_table->tmp_tgt_ssl_vscs[link_idx] >= 0)
1070   {
1071     table->field[16]->set_notnull();
1072     table->field[16]->store(
1073       alter_table->tmp_tgt_ssl_vscs[link_idx]);
1074   } else {
1075     table->field[16]->set_null();
1076     table->field[16]->reset();
1077   }
1078   table->field[17]->set_notnull();
1079   if (alter_table->tmp_monitoring_binlog_pos_at_failing[link_idx] >= 0)
1080   {
1081     table->field[17]->store(
1082       alter_table->tmp_monitoring_binlog_pos_at_failing[link_idx]);
1083   } else {
1084     table->field[17]->store(0);
1085   }
1086   if (alter_table->tmp_tgt_default_files[link_idx])
1087   {
1088     table->field[18]->set_notnull();
1089     table->field[18]->store(
1090       alter_table->tmp_tgt_default_files[link_idx],
1091       (uint) alter_table->tmp_tgt_default_files_lengths[link_idx],
1092       system_charset_info);
1093   } else {
1094     table->field[18]->set_null();
1095     table->field[18]->reset();
1096   }
1097   if (alter_table->tmp_tgt_default_groups[link_idx])
1098   {
1099     table->field[19]->set_notnull();
1100     table->field[19]->store(
1101       alter_table->tmp_tgt_default_groups[link_idx],
1102       (uint) alter_table->tmp_tgt_default_groups_lengths[link_idx],
1103       system_charset_info);
1104   } else {
1105     table->field[19]->set_null();
1106     table->field[19]->reset();
1107   }
1108   if (alter_table->tmp_tgt_dbs[link_idx])
1109   {
1110     table->field[20]->set_notnull();
1111     table->field[20]->store(
1112       alter_table->tmp_tgt_dbs[link_idx],
1113       (uint) alter_table->tmp_tgt_dbs_lengths[link_idx],
1114       system_charset_info);
1115   } else {
1116     table->field[20]->set_null();
1117     table->field[20]->reset();
1118   }
1119   if (alter_table->tmp_tgt_table_names[link_idx])
1120   {
1121     table->field[21]->set_notnull();
1122     table->field[21]->store(
1123       alter_table->tmp_tgt_table_names[link_idx],
1124       (uint) alter_table->tmp_tgt_table_names_lengths[link_idx],
1125       system_charset_info);
1126   } else {
1127     table->field[21]->set_null();
1128     table->field[21]->reset();
1129   }
1130   table->field[23]->store((longlong) 0, FALSE);
1131   if (alter_table->tmp_static_link_ids[link_idx])
1132   {
1133     DBUG_PRINT("info",("spider static_link_id[%d] = %s",
1134       link_idx, alter_table->tmp_static_link_ids[link_idx]));
1135     table->field[24]->set_notnull();
1136     table->field[24]->store(
1137       alter_table->tmp_static_link_ids[link_idx],
1138       (uint) alter_table->tmp_static_link_ids_lengths[link_idx],
1139       system_charset_info);
1140   } else {
1141     DBUG_PRINT("info",("spider static_link_id[%d] = NULL", link_idx));
1142     table->field[24]->set_null();
1143     table->field[24]->reset();
1144   }
1145   DBUG_VOID_RETURN;
1146 }
1147 
spider_store_tables_link_status(TABLE * table,long link_status)1148 void spider_store_tables_link_status(
1149   TABLE *table,
1150   long link_status
1151 ) {
1152   DBUG_ENTER("spider_store_tables_link_status");
1153   DBUG_PRINT("info",("spider link_status = %ld", link_status));
1154   if (link_status > SPIDER_LINK_STATUS_NO_CHANGE)
1155     table->field[22]->store(link_status, FALSE);
1156   DBUG_VOID_RETURN;
1157 }
1158 
spider_store_link_chk_server_id(TABLE * table,uint32 server_id)1159 void spider_store_link_chk_server_id(
1160   TABLE *table,
1161   uint32 server_id
1162 ) {
1163   DBUG_ENTER("spider_store_link_chk_server_id");
1164   table->field[3]->set_notnull();
1165   table->field[3]->store(server_id);
1166   DBUG_VOID_RETURN;
1167 }
1168 
spider_store_binlog_pos_failed_link_idx(TABLE * table,int failed_link_idx)1169 void spider_store_binlog_pos_failed_link_idx(
1170   TABLE *table,
1171   int failed_link_idx
1172 ) {
1173   DBUG_ENTER("spider_store_binlog_pos_failed_link_idx");
1174   table->field[2]->set_notnull();
1175   table->field[2]->store(failed_link_idx);
1176   DBUG_VOID_RETURN;
1177 }
1178 
spider_store_binlog_pos_source_link_idx(TABLE * table,int source_link_idx)1179 void spider_store_binlog_pos_source_link_idx(
1180   TABLE *table,
1181   int source_link_idx
1182 ) {
1183   DBUG_ENTER("spider_store_binlog_pos_source_link_idx");
1184   table->field[3]->set_notnull();
1185   table->field[3]->store(source_link_idx);
1186   DBUG_VOID_RETURN;
1187 }
1188 
spider_store_binlog_pos_binlog_file(TABLE * table,const char * file_name,int file_name_length,const char * position,int position_length,CHARSET_INFO * binlog_pos_cs)1189 void spider_store_binlog_pos_binlog_file(
1190   TABLE *table,
1191   const char *file_name,
1192   int file_name_length,
1193   const char *position,
1194   int position_length,
1195   CHARSET_INFO *binlog_pos_cs
1196 ) {
1197   DBUG_ENTER("spider_store_binlog_pos_binlog_file");
1198   if (!file_name)
1199   {
1200     DBUG_PRINT("info",("spider file_name is NULL"));
1201     table->field[4]->set_null();
1202     table->field[4]->reset();
1203   } else {
1204     DBUG_PRINT("info",("spider file_name = %s", file_name));
1205     table->field[4]->set_notnull();
1206     table->field[4]->store(file_name, file_name_length, binlog_pos_cs);
1207   }
1208   if (!position)
1209   {
1210     DBUG_PRINT("info",("spider position is NULL"));
1211     table->field[5]->set_null();
1212     table->field[5]->reset();
1213   } else {
1214     DBUG_PRINT("info",("spider position = %s", position));
1215     table->field[5]->set_notnull();
1216     table->field[5]->store(position, position_length, binlog_pos_cs);
1217   }
1218   DBUG_VOID_RETURN;
1219 }
1220 
spider_store_binlog_pos_gtid(TABLE * table,const char * gtid,int gtid_length,CHARSET_INFO * binlog_pos_cs)1221 void spider_store_binlog_pos_gtid(
1222   TABLE *table,
1223   const char *gtid,
1224   int gtid_length,
1225   CHARSET_INFO *binlog_pos_cs
1226 ) {
1227   DBUG_ENTER("spider_store_binlog_pos_gtid");
1228   if (!gtid)
1229   {
1230     DBUG_PRINT("info",("spider gtid is NULL"));
1231     table->field[6]->set_null();
1232     table->field[6]->reset();
1233   } else {
1234     DBUG_PRINT("info",("spider gtid = %s", gtid));
1235     table->field[6]->set_notnull();
1236     table->field[6]->store(gtid, gtid_length, binlog_pos_cs);
1237   }
1238   DBUG_VOID_RETURN;
1239 }
1240 
spider_store_table_sts_info(TABLE * table,ulonglong * data_file_length,ulonglong * max_data_file_length,ulonglong * index_file_length,ha_rows * records,ulong * mean_rec_length,time_t * check_time,time_t * create_time,time_t * update_time)1241 void spider_store_table_sts_info(
1242   TABLE *table,
1243   ulonglong *data_file_length,
1244   ulonglong *max_data_file_length,
1245   ulonglong *index_file_length,
1246   ha_rows *records,
1247   ulong *mean_rec_length,
1248   time_t *check_time,
1249   time_t *create_time,
1250   time_t *update_time
1251 ) {
1252   MYSQL_TIME mysql_time;
1253   DBUG_ENTER("spider_store_table_sts_info");
1254   table->field[2]->store((longlong) *data_file_length, TRUE);
1255   table->field[3]->store((longlong) *max_data_file_length, TRUE);
1256   table->field[4]->store((longlong) *index_file_length, TRUE);
1257   table->field[5]->store((longlong) *records, TRUE);
1258   table->field[6]->store((longlong) *mean_rec_length, TRUE);
1259   spd_tz_system->gmt_sec_to_TIME(&mysql_time, (my_time_t) *check_time);
1260   table->field[7]->store_time(&mysql_time);
1261   spd_tz_system->gmt_sec_to_TIME(&mysql_time, (my_time_t) *create_time);
1262   table->field[8]->store_time(&mysql_time);
1263   spd_tz_system->gmt_sec_to_TIME(&mysql_time, (my_time_t) *update_time);
1264   table->field[9]->store_time(&mysql_time);
1265   DBUG_VOID_RETURN;
1266 }
1267 
spider_store_table_crd_info(TABLE * table,uint * seq,longlong * cardinality)1268 void spider_store_table_crd_info(
1269   TABLE *table,
1270   uint *seq,
1271   longlong *cardinality
1272 ) {
1273   DBUG_ENTER("spider_store_table_crd_info");
1274   table->field[2]->store((longlong) *seq, TRUE);
1275   table->field[3]->store((longlong) *cardinality, FALSE);
1276   DBUG_VOID_RETURN;
1277 }
1278 
spider_insert_xa(TABLE * table,XID * xid,const char * status)1279 int spider_insert_xa(
1280   TABLE *table,
1281   XID *xid,
1282   const char *status
1283 ) {
1284   int error_num;
1285   char table_key[MAX_KEY_LENGTH];
1286   DBUG_ENTER("spider_insert_xa");
1287   table->use_all_columns();
1288   empty_record(table);
1289   spider_store_xa_pk(table, xid);
1290 
1291   if ((error_num = spider_check_sys_table(table, table_key)))
1292   {
1293     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
1294     {
1295       table->file->print_error(error_num, MYF(0));
1296       DBUG_RETURN(error_num);
1297     }
1298     table->use_all_columns();
1299     spider_store_xa_bqual_length(table, xid);
1300     spider_store_xa_status(table, status);
1301     if ((error_num = spider_write_sys_table_row(table)))
1302     {
1303       DBUG_RETURN(error_num);
1304     }
1305   } else {
1306     my_message(ER_SPIDER_XA_EXISTS_NUM, ER_SPIDER_XA_EXISTS_STR, MYF(0));
1307     DBUG_RETURN(ER_SPIDER_XA_EXISTS_NUM);
1308   }
1309 
1310   DBUG_RETURN(0);
1311 }
1312 
spider_insert_xa_member(TABLE * table,XID * xid,SPIDER_CONN * conn)1313 int spider_insert_xa_member(
1314   TABLE *table,
1315   XID *xid,
1316   SPIDER_CONN *conn
1317 ) {
1318   int error_num;
1319   char table_key[MAX_KEY_LENGTH];
1320   DBUG_ENTER("spider_insert_xa_member");
1321   table->use_all_columns();
1322   empty_record(table);
1323   spider_store_xa_member_pk(table, xid, conn);
1324 
1325   if ((error_num = spider_check_sys_table(table, table_key)))
1326   {
1327     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
1328     {
1329       table->file->print_error(error_num, MYF(0));
1330       DBUG_RETURN(error_num);
1331     }
1332     table->use_all_columns();
1333     spider_store_xa_member_info(table, xid, conn);
1334     if ((error_num = spider_write_sys_table_row(table)))
1335     {
1336       DBUG_RETURN(error_num);
1337     }
1338   } else {
1339     my_message(ER_SPIDER_XA_MEMBER_EXISTS_NUM, ER_SPIDER_XA_MEMBER_EXISTS_STR,
1340       MYF(0));
1341     DBUG_RETURN(ER_SPIDER_XA_MEMBER_EXISTS_NUM);
1342   }
1343 
1344   DBUG_RETURN(0);
1345 }
1346 
spider_insert_tables(TABLE * table,SPIDER_SHARE * share)1347 int spider_insert_tables(
1348   TABLE *table,
1349   SPIDER_SHARE *share
1350 ) {
1351   int error_num, roop_count;
1352   DBUG_ENTER("spider_insert_tables");
1353   table->use_all_columns();
1354   empty_record(table);
1355 
1356   spider_store_tables_name(table, share->table_name, share->table_name_length);
1357   spider_store_tables_priority(table, share->priority);
1358   for (roop_count = 0; roop_count < (int) share->all_link_count; roop_count++)
1359   {
1360     spider_store_tables_link_idx(table, roop_count);
1361     spider_store_tables_connect_info(table, &share->alter_table, roop_count);
1362     spider_store_tables_link_status(table,
1363       share->alter_table.tmp_link_statuses[roop_count] >
1364       SPIDER_LINK_STATUS_NO_CHANGE ?
1365       share->alter_table.tmp_link_statuses[roop_count] :
1366       SPIDER_LINK_STATUS_OK);
1367     if ((error_num = spider_write_sys_table_row(table)))
1368     {
1369       DBUG_RETURN(error_num);
1370     }
1371   }
1372 
1373   DBUG_RETURN(0);
1374 }
1375 
spider_insert_sys_table(TABLE * table)1376 int spider_insert_sys_table(
1377   TABLE *table
1378 ) {
1379   int error_num;
1380   DBUG_ENTER("spider_insert_sys_table");
1381   error_num = spider_write_sys_table_row(table);
1382   DBUG_RETURN(error_num);
1383 }
1384 
spider_insert_or_update_table_sts(TABLE * table,const char * name,uint name_length,ulonglong * data_file_length,ulonglong * max_data_file_length,ulonglong * index_file_length,ha_rows * records,ulong * mean_rec_length,time_t * check_time,time_t * create_time,time_t * update_time)1385 int spider_insert_or_update_table_sts(
1386   TABLE *table,
1387   const char *name,
1388   uint name_length,
1389   ulonglong *data_file_length,
1390   ulonglong *max_data_file_length,
1391   ulonglong *index_file_length,
1392   ha_rows *records,
1393   ulong *mean_rec_length,
1394   time_t *check_time,
1395   time_t *create_time,
1396   time_t *update_time
1397 ) {
1398   int error_num;
1399   char table_key[MAX_KEY_LENGTH];
1400   DBUG_ENTER("spider_insert_or_update_table_sts");
1401   table->use_all_columns();
1402   spider_store_tables_name(table, name, name_length);
1403   spider_store_table_sts_info(
1404     table,
1405     data_file_length,
1406     max_data_file_length,
1407     index_file_length,
1408     records,
1409     mean_rec_length,
1410     check_time,
1411     create_time,
1412     update_time
1413   );
1414 
1415   if ((error_num = spider_check_sys_table_for_update_all_columns(table, table_key)))
1416   {
1417     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
1418     {
1419       table->file->print_error(error_num, MYF(0));
1420       DBUG_RETURN(error_num);
1421     }
1422     if ((error_num = spider_write_sys_table_row(table)))
1423     {
1424       DBUG_RETURN(error_num);
1425     }
1426   } else {
1427     if ((error_num = spider_update_sys_table_row(table, FALSE)))
1428     {
1429       table->file->print_error(error_num, MYF(0));
1430       DBUG_RETURN(error_num);
1431     }
1432   }
1433 
1434   DBUG_RETURN(0);
1435 }
1436 
spider_insert_or_update_table_crd(TABLE * table,const char * name,uint name_length,longlong * cardinality,uint number_of_keys)1437 int spider_insert_or_update_table_crd(
1438   TABLE *table,
1439   const char *name,
1440   uint name_length,
1441   longlong *cardinality,
1442   uint number_of_keys
1443 ) {
1444   int error_num;
1445   uint roop_count;
1446   char table_key[MAX_KEY_LENGTH];
1447   DBUG_ENTER("spider_insert_or_update_table_crd");
1448   table->use_all_columns();
1449   spider_store_tables_name(table, name, name_length);
1450 
1451   for (roop_count = 0; roop_count < number_of_keys; ++roop_count)
1452   {
1453     spider_store_table_crd_info(table, &roop_count, &cardinality[roop_count]);
1454     if ((error_num = spider_check_sys_table_for_update_all_columns(table, table_key)))
1455     {
1456       if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
1457       {
1458         table->file->print_error(error_num, MYF(0));
1459         DBUG_RETURN(error_num);
1460       }
1461       if ((error_num = spider_write_sys_table_row(table)))
1462       {
1463         DBUG_RETURN(error_num);
1464       }
1465     } else {
1466       if ((error_num = spider_update_sys_table_row(table, FALSE)))
1467       {
1468         table->file->print_error(error_num, MYF(0));
1469         DBUG_RETURN(error_num);
1470       }
1471     }
1472   }
1473   DBUG_RETURN(0);
1474 }
1475 
spider_log_tables_link_failed(TABLE * table,char * name,uint name_length,int link_idx)1476 int spider_log_tables_link_failed(
1477   TABLE *table,
1478   char *name,
1479   uint name_length,
1480   int link_idx
1481 ) {
1482   int error_num;
1483   DBUG_ENTER("spider_log_tables_link_failed");
1484   table->use_all_columns();
1485   spider_store_tables_name(table, name, name_length);
1486   spider_store_tables_link_idx(table, link_idx);
1487 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000
1488 #else
1489   if (table->field[3] == table->timestamp_field)
1490     table->timestamp_field->set_time();
1491 #endif
1492   if ((error_num = spider_write_sys_table_row(table)))
1493   {
1494     DBUG_RETURN(error_num);
1495   }
1496   DBUG_RETURN(0);
1497 }
1498 
spider_log_xa_failed(THD * thd,TABLE * table,XID * xid,SPIDER_CONN * conn,const char * status)1499 int spider_log_xa_failed(
1500   THD *thd,
1501   TABLE *table,
1502   XID *xid,
1503   SPIDER_CONN *conn,
1504   const char *status
1505 ) {
1506   int error_num;
1507   DBUG_ENTER("spider_log_xa_failed");
1508   table->use_all_columns();
1509   spider_store_xa_member_pk(table, xid, conn);
1510   spider_store_xa_member_info(table, xid, conn);
1511   if (thd)
1512   {
1513     table->field[18]->set_notnull();
1514     table->field[18]->store(thd->thread_id, TRUE);
1515   } else {
1516     table->field[18]->set_null();
1517     table->field[18]->reset();
1518   }
1519   table->field[19]->store(
1520     status,
1521     (uint) strlen(status),
1522     system_charset_info);
1523 
1524 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000
1525 #else
1526   if (table->field[20] == table->timestamp_field)
1527     table->timestamp_field->set_time();
1528 #endif
1529   if ((error_num = spider_write_sys_table_row(table)))
1530   {
1531     DBUG_RETURN(error_num);
1532   }
1533   DBUG_RETURN(0);
1534 }
1535 
spider_update_xa(TABLE * table,XID * xid,const char * status)1536 int spider_update_xa(
1537   TABLE *table,
1538   XID *xid,
1539   const char *status
1540 ) {
1541   int error_num;
1542   char table_key[MAX_KEY_LENGTH];
1543   DBUG_ENTER("spider_update_xa");
1544   table->use_all_columns();
1545   spider_store_xa_pk(table, xid);
1546 
1547   if ((error_num = spider_check_sys_table(table, table_key)))
1548   {
1549     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
1550     {
1551       table->file->print_error(error_num, MYF(0));
1552       DBUG_RETURN(error_num);
1553     }
1554     my_message(ER_SPIDER_XA_NOT_EXISTS_NUM, ER_SPIDER_XA_NOT_EXISTS_STR,
1555       MYF(0));
1556     DBUG_RETURN(ER_SPIDER_XA_NOT_EXISTS_NUM);
1557   } else {
1558     store_record(table, record[1]);
1559     table->use_all_columns();
1560     spider_store_xa_status(table, status);
1561     if ((error_num = spider_update_sys_table_row(table)))
1562     {
1563       DBUG_RETURN(error_num);
1564     }
1565   }
1566 
1567   DBUG_RETURN(0);
1568 }
1569 
spider_update_tables_name(TABLE * table,const char * from,const char * to,int * old_link_count)1570 int spider_update_tables_name(
1571   TABLE *table,
1572   const char *from,
1573   const char *to,
1574   int *old_link_count
1575 ) {
1576   int error_num, roop_count = 0;
1577   char table_key[MAX_KEY_LENGTH];
1578   DBUG_ENTER("spider_update_tables_name");
1579   table->use_all_columns();
1580   while (TRUE)
1581   {
1582     spider_store_tables_name(table, from, strlen(from));
1583     spider_store_tables_link_idx(table, roop_count);
1584     if ((error_num = spider_check_sys_table(table, table_key)))
1585     {
1586       if (
1587         roop_count &&
1588         (error_num == HA_ERR_KEY_NOT_FOUND || error_num == HA_ERR_END_OF_FILE)
1589       )
1590         break;
1591       table->file->print_error(error_num, MYF(0));
1592       DBUG_RETURN(error_num);
1593     } else {
1594       store_record(table, record[1]);
1595       table->use_all_columns();
1596       spider_store_tables_name(table, to, strlen(to));
1597       if ((error_num = spider_update_sys_table_row(table)))
1598       {
1599         DBUG_RETURN(error_num);
1600       }
1601     }
1602     roop_count++;
1603   }
1604 
1605   *old_link_count = roop_count;
1606   DBUG_RETURN(0);
1607 }
1608 
spider_update_tables_priority(TABLE * table,SPIDER_ALTER_TABLE * alter_table,const char * name,int * old_link_count)1609 int spider_update_tables_priority(
1610   TABLE *table,
1611   SPIDER_ALTER_TABLE *alter_table,
1612   const char *name,
1613   int *old_link_count
1614 ) {
1615   int error_num, roop_count;
1616   char table_key[MAX_KEY_LENGTH];
1617   DBUG_ENTER("spider_update_tables_priority");
1618   table->use_all_columns();
1619   for (roop_count = 0; roop_count < (int) alter_table->all_link_count;
1620     roop_count++)
1621   {
1622     spider_store_tables_name(table, alter_table->table_name,
1623       alter_table->table_name_length);
1624     spider_store_tables_link_idx(table, roop_count);
1625     if ((error_num = spider_check_sys_table(table, table_key)))
1626     {
1627       if (
1628         roop_count &&
1629         (error_num == HA_ERR_KEY_NOT_FOUND || error_num == HA_ERR_END_OF_FILE)
1630       ) {
1631         *old_link_count = roop_count;
1632 
1633         /* insert for adding link */
1634         spider_store_tables_name(table, name, strlen(name));
1635         spider_store_tables_priority(table, alter_table->tmp_priority);
1636         do {
1637           spider_store_tables_link_idx(table, roop_count);
1638           spider_store_tables_connect_info(table, alter_table, roop_count);
1639           spider_store_tables_link_status(table,
1640             alter_table->tmp_link_statuses[roop_count] !=
1641             SPIDER_LINK_STATUS_NO_CHANGE ?
1642             alter_table->tmp_link_statuses[roop_count] :
1643             SPIDER_LINK_STATUS_OK);
1644           if ((error_num = spider_write_sys_table_row(table)))
1645           {
1646             DBUG_RETURN(error_num);
1647           }
1648           roop_count++;
1649         } while (roop_count < (int) alter_table->all_link_count);
1650         DBUG_RETURN(0);
1651       } else {
1652         table->file->print_error(error_num, MYF(0));
1653         DBUG_RETURN(error_num);
1654       }
1655     } else {
1656       store_record(table, record[1]);
1657       table->use_all_columns();
1658       spider_store_tables_name(table, name, strlen(name));
1659       spider_store_tables_priority(table, alter_table->tmp_priority);
1660       spider_store_tables_connect_info(table, alter_table, roop_count);
1661       spider_store_tables_link_status(table,
1662         alter_table->tmp_link_statuses[roop_count]);
1663       if ((error_num = spider_update_sys_table_row(table)))
1664       {
1665         DBUG_RETURN(error_num);
1666       }
1667     }
1668   }
1669   while (TRUE)
1670   {
1671     /* delete for subtracting link */
1672     spider_store_tables_link_idx(table, roop_count);
1673     if ((error_num = spider_check_sys_table(table, table_key)))
1674     {
1675       if (
1676         roop_count &&
1677         (error_num == HA_ERR_KEY_NOT_FOUND || error_num == HA_ERR_END_OF_FILE)
1678       )
1679         break;
1680       else {
1681         table->file->print_error(error_num, MYF(0));
1682         DBUG_RETURN(error_num);
1683       }
1684       if ((error_num = spider_delete_sys_table_row(table)))
1685       {
1686         DBUG_RETURN(error_num);
1687       }
1688     }
1689     roop_count++;
1690   }
1691 
1692   *old_link_count = roop_count;
1693   DBUG_RETURN(0);
1694 }
1695 
spider_update_tables_link_status(TABLE * table,char * name,uint name_length,int link_idx,long link_status)1696 int spider_update_tables_link_status(
1697   TABLE *table,
1698   char *name,
1699   uint name_length,
1700   int link_idx,
1701   long link_status
1702 ) {
1703   int error_num;
1704   char table_key[MAX_KEY_LENGTH];
1705   DBUG_ENTER("spider_update_tables_link_status");
1706   table->use_all_columns();
1707   spider_store_tables_name(table, name, name_length);
1708   spider_store_tables_link_idx(table, link_idx);
1709   if ((error_num = spider_check_sys_table(table, table_key)))
1710   {
1711     if (
1712       (error_num == HA_ERR_KEY_NOT_FOUND || error_num == HA_ERR_END_OF_FILE)
1713     )
1714       DBUG_RETURN(0);
1715     else {
1716       table->file->print_error(error_num, MYF(0));
1717       DBUG_RETURN(error_num);
1718     }
1719   } else {
1720     store_record(table, record[1]);
1721     table->use_all_columns();
1722     spider_store_tables_link_status(table, link_status);
1723     if ((error_num = spider_update_sys_table_row(table)))
1724     {
1725       DBUG_RETURN(error_num);
1726     }
1727   }
1728 
1729   DBUG_RETURN(0);
1730 }
1731 
spider_update_sys_table(TABLE * table)1732 int spider_update_sys_table(
1733   TABLE *table
1734 ) {
1735   int error_num;
1736   DBUG_ENTER("spider_update_sys_table");
1737   error_num = spider_update_sys_table_row(table);
1738   DBUG_RETURN(error_num);
1739 }
1740 
spider_delete_xa(TABLE * table,XID * xid)1741 int spider_delete_xa(
1742   TABLE *table,
1743   XID *xid
1744 ) {
1745   int error_num;
1746   char table_key[MAX_KEY_LENGTH];
1747   DBUG_ENTER("spider_delete_xa");
1748   table->use_all_columns();
1749   spider_store_xa_pk(table, xid);
1750 
1751   if ((error_num = spider_check_sys_table(table, table_key)))
1752   {
1753     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
1754     {
1755       table->file->print_error(error_num, MYF(0));
1756       DBUG_RETURN(error_num);
1757     }
1758     my_message(ER_SPIDER_XA_NOT_EXISTS_NUM, ER_SPIDER_XA_NOT_EXISTS_STR,
1759       MYF(0));
1760     DBUG_RETURN(ER_SPIDER_XA_NOT_EXISTS_NUM);
1761   } else {
1762     if ((error_num = spider_delete_sys_table_row(table)))
1763     {
1764       DBUG_RETURN(error_num);
1765     }
1766   }
1767 
1768   DBUG_RETURN(0);
1769 }
1770 
spider_delete_xa_member(TABLE * table,XID * xid)1771 int spider_delete_xa_member(
1772   TABLE *table,
1773   XID *xid
1774 ) {
1775   int error_num;
1776   char table_key[MAX_KEY_LENGTH];
1777   DBUG_ENTER("spider_delete_xa_member");
1778   table->use_all_columns();
1779   spider_store_xa_pk(table, xid);
1780 
1781   if ((error_num = spider_get_sys_table_by_idx(table, table_key, 0,
1782     SPIDER_SYS_XA_PK_COL_CNT)))
1783   {
1784     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
1785     {
1786       table->file->print_error(error_num, MYF(0));
1787       DBUG_RETURN(error_num);
1788     }
1789     DBUG_RETURN(0);
1790   } else {
1791     do {
1792       if ((error_num = spider_delete_sys_table_row(table, 0, FALSE)))
1793       {
1794         spider_sys_index_end(table);
1795         table->file->print_error(error_num, MYF(0));
1796         DBUG_RETURN(error_num);
1797       }
1798       error_num = spider_sys_index_next_same(table, table_key);
1799     } while (error_num == 0);
1800   }
1801   if ((error_num = spider_sys_index_end(table)))
1802   {
1803     table->file->print_error(error_num, MYF(0));
1804     DBUG_RETURN(error_num);
1805   }
1806 
1807   DBUG_RETURN(0);
1808 }
1809 
spider_delete_tables(TABLE * table,const char * name,int * old_link_count)1810 int spider_delete_tables(
1811   TABLE *table,
1812   const char *name,
1813   int *old_link_count
1814 ) {
1815   int error_num, roop_count = 0;
1816   char table_key[MAX_KEY_LENGTH];
1817   DBUG_ENTER("spider_delete_tables");
1818   table->use_all_columns();
1819   spider_store_tables_name(table, name, strlen(name));
1820 
1821   while (TRUE)
1822   {
1823     spider_store_tables_link_idx(table, roop_count);
1824     if ((error_num = spider_check_sys_table(table, table_key)))
1825       break;
1826     else {
1827       if ((error_num = spider_delete_sys_table_row(table)))
1828       {
1829         DBUG_RETURN(error_num);
1830       }
1831     }
1832     roop_count++;
1833   }
1834 
1835   *old_link_count = roop_count;
1836   DBUG_RETURN(0);
1837 }
1838 
spider_delete_table_sts(TABLE * table,const char * name,uint name_length)1839 int spider_delete_table_sts(
1840   TABLE *table,
1841   const char *name,
1842   uint name_length
1843 ) {
1844   int error_num;
1845   char table_key[MAX_KEY_LENGTH];
1846   DBUG_ENTER("spider_delete_table_sts");
1847   table->use_all_columns();
1848   spider_store_tables_name(table, name, name_length);
1849 
1850   if ((error_num = spider_check_sys_table(table, table_key)))
1851   {
1852     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
1853     {
1854       table->file->print_error(error_num, MYF(0));
1855       DBUG_RETURN(error_num);
1856     }
1857     /* no record is ok */
1858     DBUG_RETURN(0);
1859   } else {
1860     if ((error_num = spider_delete_sys_table_row(table)))
1861     {
1862       DBUG_RETURN(error_num);
1863     }
1864   }
1865 
1866   DBUG_RETURN(0);
1867 }
1868 
spider_delete_table_crd(TABLE * table,const char * name,uint name_length)1869 int spider_delete_table_crd(
1870   TABLE *table,
1871   const char *name,
1872   uint name_length
1873 ) {
1874   int error_num;
1875   char table_key[MAX_KEY_LENGTH];
1876   DBUG_ENTER("spider_delete_table_crd");
1877   table->use_all_columns();
1878   spider_store_tables_name(table, name, name_length);
1879 
1880   if ((error_num = spider_get_sys_table_by_idx(table, table_key, 0,
1881     SPIDER_SYS_TABLE_CRD_PK_COL_CNT - 1)))
1882   {
1883     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
1884     {
1885       table->file->print_error(error_num, MYF(0));
1886       DBUG_RETURN(error_num);
1887     }
1888     /* no record is ok */
1889     DBUG_RETURN(0);
1890   } else {
1891     do {
1892       if ((error_num = spider_delete_sys_table_row(table)))
1893       {
1894         spider_sys_index_end(table);
1895         DBUG_RETURN(error_num);
1896       }
1897       error_num = spider_sys_index_next_same(table, table_key);
1898     } while (error_num == 0);
1899   }
1900   if ((error_num = spider_sys_index_end(table)))
1901   {
1902     table->file->print_error(error_num, MYF(0));
1903     DBUG_RETURN(error_num);
1904   }
1905 
1906   DBUG_RETURN(0);
1907 }
1908 
spider_get_sys_xid(TABLE * table,XID * xid,MEM_ROOT * mem_root)1909 int spider_get_sys_xid(
1910   TABLE *table,
1911   XID *xid,
1912   MEM_ROOT *mem_root
1913 ) {
1914   char *ptr;
1915   DBUG_ENTER("spider_get_sys_xid");
1916   ptr = get_field(mem_root, table->field[0]);
1917   if (ptr)
1918   {
1919     xid->formatID = atoi(ptr);
1920   } else
1921     xid->formatID = 0;
1922   ptr = get_field(mem_root, table->field[1]);
1923   if (ptr)
1924   {
1925     xid->gtrid_length = atoi(ptr);
1926   } else
1927     xid->gtrid_length = 0;
1928   ptr = get_field(mem_root, table->field[2]);
1929   if (ptr)
1930   {
1931     xid->bqual_length = atoi(ptr);
1932   } else
1933     xid->bqual_length = 0;
1934   ptr = get_field(mem_root, table->field[3]);
1935   if (ptr)
1936   {
1937     strmov(xid->data, ptr);
1938   }
1939   DBUG_RETURN(0);
1940 }
1941 
spider_get_sys_server_info(TABLE * table,SPIDER_SHARE * share,int link_idx,MEM_ROOT * mem_root)1942 int spider_get_sys_server_info(
1943   TABLE *table,
1944   SPIDER_SHARE *share,
1945   int link_idx,
1946   MEM_ROOT *mem_root
1947 ) {
1948   char *ptr;
1949   DBUG_ENTER("spider_get_sys_server_info");
1950   if ((ptr = get_field(mem_root, table->field[4])))
1951   {
1952     share->tgt_wrappers_lengths[link_idx] = strlen(ptr);
1953     share->tgt_wrappers[link_idx] = spider_create_string(ptr,
1954       share->tgt_wrappers_lengths[link_idx]);
1955   } else {
1956     share->tgt_wrappers_lengths[link_idx] = 0;
1957     share->tgt_wrappers[link_idx] = NULL;
1958   }
1959   if ((ptr = get_field(mem_root, table->field[5])))
1960   {
1961     share->tgt_hosts_lengths[link_idx] = strlen(ptr);
1962     share->tgt_hosts[link_idx] = spider_create_string(ptr,
1963       share->tgt_hosts_lengths[link_idx]);
1964   } else {
1965     share->tgt_hosts_lengths[link_idx] = 0;
1966     share->tgt_hosts[link_idx] = NULL;
1967   }
1968   if ((ptr = get_field(mem_root, table->field[6])))
1969   {
1970     share->tgt_ports[link_idx] = atol(ptr);
1971   } else
1972     share->tgt_ports[link_idx] = MYSQL_PORT;
1973   if ((ptr = get_field(mem_root, table->field[7])))
1974   {
1975     share->tgt_sockets_lengths[link_idx] = strlen(ptr);
1976     share->tgt_sockets[link_idx] = spider_create_string(ptr,
1977       share->tgt_sockets_lengths[link_idx]);
1978   } else {
1979     share->tgt_sockets_lengths[link_idx] = 0;
1980     share->tgt_sockets[link_idx] = NULL;
1981   }
1982   if ((ptr = get_field(mem_root, table->field[8])))
1983   {
1984     share->tgt_usernames_lengths[link_idx] = strlen(ptr);
1985     share->tgt_usernames[link_idx] =
1986       spider_create_string(ptr, share->tgt_usernames_lengths[link_idx]);
1987   } else {
1988     share->tgt_usernames_lengths[link_idx] = 0;
1989     share->tgt_usernames[link_idx] = NULL;
1990   }
1991   if ((ptr = get_field(mem_root, table->field[9])))
1992   {
1993     share->tgt_passwords_lengths[link_idx] = strlen(ptr);
1994     share->tgt_passwords[link_idx] =
1995       spider_create_string(ptr, share->tgt_passwords_lengths[link_idx]);
1996   } else {
1997     share->tgt_passwords_lengths[link_idx] = 0;
1998     share->tgt_passwords[link_idx] = NULL;
1999   }
2000   if (
2001     !table->field[10]->is_null() &&
2002     (ptr = get_field(mem_root, table->field[10]))
2003   ) {
2004     share->tgt_ssl_cas_lengths[link_idx] = strlen(ptr);
2005     share->tgt_ssl_cas[link_idx] =
2006       spider_create_string(ptr, share->tgt_ssl_cas_lengths[link_idx]);
2007   } else {
2008     share->tgt_ssl_cas_lengths[link_idx] = 0;
2009     share->tgt_ssl_cas[link_idx] = NULL;
2010   }
2011   if (
2012     !table->field[11]->is_null() &&
2013     (ptr = get_field(mem_root, table->field[11]))
2014   ) {
2015     share->tgt_ssl_capaths_lengths[link_idx] = strlen(ptr);
2016     share->tgt_ssl_capaths[link_idx] =
2017       spider_create_string(ptr, share->tgt_ssl_capaths_lengths[link_idx]);
2018   } else {
2019     share->tgt_ssl_capaths_lengths[link_idx] = 0;
2020     share->tgt_ssl_capaths[link_idx] = NULL;
2021   }
2022   if (
2023     !table->field[12]->is_null() &&
2024     (ptr = get_field(mem_root, table->field[12]))
2025   ) {
2026     share->tgt_ssl_certs_lengths[link_idx] = strlen(ptr);
2027     share->tgt_ssl_certs[link_idx] =
2028       spider_create_string(ptr, share->tgt_ssl_certs_lengths[link_idx]);
2029   } else {
2030     share->tgt_ssl_certs_lengths[link_idx] = 0;
2031     share->tgt_ssl_certs[link_idx] = NULL;
2032   }
2033   if (
2034     !table->field[13]->is_null() &&
2035     (ptr = get_field(mem_root, table->field[13]))
2036   ) {
2037     share->tgt_ssl_ciphers_lengths[link_idx] = strlen(ptr);
2038     share->tgt_ssl_ciphers[link_idx] =
2039       spider_create_string(ptr, share->tgt_ssl_ciphers_lengths[link_idx]);
2040   } else {
2041     share->tgt_ssl_ciphers_lengths[link_idx] = 0;
2042     share->tgt_ssl_ciphers[link_idx] = NULL;
2043   }
2044   if (
2045     !table->field[14]->is_null() &&
2046     (ptr = get_field(mem_root, table->field[14]))
2047   ) {
2048     share->tgt_ssl_keys_lengths[link_idx] = strlen(ptr);
2049     share->tgt_ssl_keys[link_idx] =
2050       spider_create_string(ptr, share->tgt_ssl_keys_lengths[link_idx]);
2051   } else {
2052     share->tgt_ssl_keys_lengths[link_idx] = 0;
2053     share->tgt_ssl_keys[link_idx] = NULL;
2054   }
2055   if (
2056     !table->field[15]->is_null() &&
2057     (ptr = get_field(mem_root, table->field[15]))
2058   ) {
2059     share->tgt_ssl_vscs[link_idx] = atol(ptr);
2060   } else
2061     share->tgt_ssl_vscs[link_idx] = 0;
2062   if (
2063     !table->field[16]->is_null() &&
2064     (ptr = get_field(mem_root, table->field[16]))
2065   ) {
2066     share->tgt_default_files_lengths[link_idx] = strlen(ptr);
2067     share->tgt_default_files[link_idx] =
2068       spider_create_string(ptr, share->tgt_default_files_lengths[link_idx]);
2069   } else {
2070     share->tgt_default_files_lengths[link_idx] = 0;
2071     share->tgt_default_files[link_idx] = NULL;
2072   }
2073   if (
2074     !table->field[17]->is_null() &&
2075     (ptr = get_field(mem_root, table->field[17]))
2076   ) {
2077     share->tgt_default_groups_lengths[link_idx] = strlen(ptr);
2078     share->tgt_default_groups[link_idx] =
2079       spider_create_string(ptr, share->tgt_default_groups_lengths[link_idx]);
2080   } else {
2081     share->tgt_default_groups_lengths[link_idx] = 0;
2082     share->tgt_default_groups[link_idx] = NULL;
2083   }
2084   DBUG_RETURN(0);
2085 }
2086 
spider_check_sys_xa_status(TABLE * table,const char * status1,const char * status2,const char * status3,const int check_error_num,MEM_ROOT * mem_root)2087 int spider_check_sys_xa_status(
2088   TABLE *table,
2089   const char *status1,
2090   const char *status2,
2091   const char *status3,
2092   const int check_error_num,
2093   MEM_ROOT *mem_root
2094 ) {
2095   char *ptr;
2096   int error_num;
2097   DBUG_ENTER("spider_check_sys_xa_status");
2098   ptr = get_field(mem_root, table->field[4]);
2099   if (ptr)
2100   {
2101     if (
2102       strcmp(ptr, status1) &&
2103       (status2 == NULL || strcmp(ptr, status2)) &&
2104       (status3 == NULL || strcmp(ptr, status3))
2105     )
2106       error_num = check_error_num;
2107     else
2108       error_num = 0;
2109   } else
2110     error_num = check_error_num;
2111   DBUG_RETURN(error_num);
2112 }
2113 
spider_get_sys_tables(TABLE * table,char ** db_name,char ** table_name,MEM_ROOT * mem_root)2114 int spider_get_sys_tables(
2115   TABLE *table,
2116   char **db_name,
2117   char **table_name,
2118   MEM_ROOT *mem_root
2119 ) {
2120   char *ptr;
2121   DBUG_ENTER("spider_get_sys_tables");
2122   if ((ptr = get_field(mem_root, table->field[0])))
2123   {
2124     *db_name = spider_create_string(ptr, strlen(ptr));
2125   } else {
2126     *db_name = NULL;
2127   }
2128   if ((ptr = get_field(mem_root, table->field[1])))
2129   {
2130     *table_name = spider_create_string(ptr, strlen(ptr));
2131   } else {
2132     *table_name = NULL;
2133   }
2134   DBUG_RETURN(0);
2135 }
2136 
spider_get_sys_tables_connect_info(TABLE * table,SPIDER_SHARE * share,int link_idx,MEM_ROOT * mem_root)2137 int spider_get_sys_tables_connect_info(
2138   TABLE *table,
2139   SPIDER_SHARE *share,
2140   int link_idx,
2141   MEM_ROOT *mem_root
2142 ) {
2143   char *ptr;
2144   int error_num = 0;
2145   DBUG_ENTER("spider_get_sys_tables_connect_info");
2146   if ((ptr = get_field(mem_root, table->field[3])))
2147   {
2148     share->priority = my_strtoll10(ptr, (char**) NULL, &error_num);
2149   } else
2150     share->priority = 1000000;
2151   if (
2152     !table->field[4]->is_null() &&
2153     (ptr = get_field(mem_root, table->field[4]))
2154   ) {
2155     share->server_names_lengths[link_idx] = strlen(ptr);
2156     share->server_names[link_idx] =
2157       spider_create_string(ptr, share->server_names_lengths[link_idx]);
2158   } else {
2159     share->server_names_lengths[link_idx] = 0;
2160     share->server_names[link_idx] = NULL;
2161   }
2162   if (
2163     !table->field[5]->is_null() &&
2164     (ptr = get_field(mem_root, table->field[5]))
2165   ) {
2166     share->tgt_wrappers_lengths[link_idx] = strlen(ptr);
2167     share->tgt_wrappers[link_idx] =
2168       spider_create_string(ptr, share->tgt_wrappers_lengths[link_idx]);
2169   } else {
2170     share->tgt_wrappers_lengths[link_idx] = 0;
2171     share->tgt_wrappers[link_idx] = NULL;
2172   }
2173   if (
2174     !table->field[6]->is_null() &&
2175     (ptr = get_field(mem_root, table->field[6]))
2176   ) {
2177     share->tgt_hosts_lengths[link_idx] = strlen(ptr);
2178     share->tgt_hosts[link_idx] =
2179       spider_create_string(ptr, share->tgt_hosts_lengths[link_idx]);
2180   } else {
2181     share->tgt_hosts_lengths[link_idx] = 0;
2182     share->tgt_hosts[link_idx] = NULL;
2183   }
2184   if (
2185     !table->field[7]->is_null() &&
2186     (ptr = get_field(mem_root, table->field[7]))
2187   ) {
2188     share->tgt_ports[link_idx] = atol(ptr);
2189   } else {
2190     share->tgt_ports[link_idx] = -1;
2191   }
2192   if (
2193     !table->field[8]->is_null() &&
2194     (ptr = get_field(mem_root, table->field[8]))
2195   ) {
2196     share->tgt_sockets_lengths[link_idx] = strlen(ptr);
2197     share->tgt_sockets[link_idx] =
2198       spider_create_string(ptr, share->tgt_sockets_lengths[link_idx]);
2199   } else {
2200     share->tgt_sockets_lengths[link_idx] = 0;
2201     share->tgt_sockets[link_idx] = NULL;
2202   }
2203   if (
2204     !table->field[9]->is_null() &&
2205     (ptr = get_field(mem_root, table->field[9]))
2206   ) {
2207     share->tgt_usernames_lengths[link_idx] = strlen(ptr);
2208     share->tgt_usernames[link_idx] =
2209       spider_create_string(ptr, share->tgt_usernames_lengths[link_idx]);
2210   } else {
2211     share->tgt_usernames_lengths[link_idx] = 0;
2212     share->tgt_usernames[link_idx] = NULL;
2213   }
2214   if (
2215     !table->field[10]->is_null() &&
2216     (ptr = get_field(mem_root, table->field[10]))
2217   ) {
2218     share->tgt_passwords_lengths[link_idx] = strlen(ptr);
2219     share->tgt_passwords[link_idx] =
2220       spider_create_string(ptr, share->tgt_passwords_lengths[link_idx]);
2221   } else {
2222     share->tgt_passwords_lengths[link_idx] = 0;
2223     share->tgt_passwords[link_idx] = NULL;
2224   }
2225   if (
2226     !table->field[11]->is_null() &&
2227     (ptr = get_field(mem_root, table->field[11]))
2228   ) {
2229     share->tgt_ssl_cas_lengths[link_idx] = strlen(ptr);
2230     share->tgt_ssl_cas[link_idx] =
2231       spider_create_string(ptr, share->tgt_ssl_cas_lengths[link_idx]);
2232   } else {
2233     share->tgt_ssl_cas_lengths[link_idx] = 0;
2234     share->tgt_ssl_cas[link_idx] = NULL;
2235   }
2236   if (
2237     !table->field[12]->is_null() &&
2238     (ptr = get_field(mem_root, table->field[12]))
2239   ) {
2240     share->tgt_ssl_capaths_lengths[link_idx] = strlen(ptr);
2241     share->tgt_ssl_capaths[link_idx] =
2242       spider_create_string(ptr, share->tgt_ssl_capaths_lengths[link_idx]);
2243   } else {
2244     share->tgt_ssl_capaths_lengths[link_idx] = 0;
2245     share->tgt_ssl_capaths[link_idx] = NULL;
2246   }
2247   if (
2248     !table->field[13]->is_null() &&
2249     (ptr = get_field(mem_root, table->field[13]))
2250   ) {
2251     share->tgt_ssl_certs_lengths[link_idx] = strlen(ptr);
2252     share->tgt_ssl_certs[link_idx] =
2253       spider_create_string(ptr, share->tgt_ssl_certs_lengths[link_idx]);
2254   } else {
2255     share->tgt_ssl_certs_lengths[link_idx] = 0;
2256     share->tgt_ssl_certs[link_idx] = NULL;
2257   }
2258   if (
2259     !table->field[14]->is_null() &&
2260     (ptr = get_field(mem_root, table->field[14]))
2261   ) {
2262     share->tgt_ssl_ciphers_lengths[link_idx] = strlen(ptr);
2263     share->tgt_ssl_ciphers[link_idx] =
2264       spider_create_string(ptr, share->tgt_ssl_ciphers_lengths[link_idx]);
2265   } else {
2266     share->tgt_ssl_ciphers_lengths[link_idx] = 0;
2267     share->tgt_ssl_ciphers[link_idx] = NULL;
2268   }
2269   if (
2270     !table->field[15]->is_null() &&
2271     (ptr = get_field(mem_root, table->field[15]))
2272   ) {
2273     share->tgt_ssl_keys_lengths[link_idx] = strlen(ptr);
2274     share->tgt_ssl_keys[link_idx] =
2275       spider_create_string(ptr, share->tgt_ssl_keys_lengths[link_idx]);
2276   } else {
2277     share->tgt_ssl_keys_lengths[link_idx] = 0;
2278     share->tgt_ssl_keys[link_idx] = NULL;
2279   }
2280   if (
2281     !table->field[16]->is_null() &&
2282     (ptr = get_field(mem_root, table->field[16]))
2283   ) {
2284     share->tgt_ssl_vscs[link_idx] = atol(ptr);
2285   } else
2286     share->tgt_ssl_vscs[link_idx] = -1;
2287   if (
2288     !table->field[17]->is_null() &&
2289     (ptr = get_field(mem_root, table->field[17]))
2290   ) {
2291     share->monitoring_binlog_pos_at_failing[link_idx] = atol(ptr);
2292   } else
2293     share->monitoring_binlog_pos_at_failing[link_idx] = 0;
2294   if (
2295     !table->field[18]->is_null() &&
2296     (ptr = get_field(mem_root, table->field[18]))
2297   ) {
2298     share->tgt_default_files_lengths[link_idx] = strlen(ptr);
2299     share->tgt_default_files[link_idx] =
2300       spider_create_string(ptr, share->tgt_default_files_lengths[link_idx]);
2301   } else {
2302     share->tgt_default_files_lengths[link_idx] = 0;
2303     share->tgt_default_files[link_idx] = NULL;
2304   }
2305   if (
2306     !table->field[19]->is_null() &&
2307     (ptr = get_field(mem_root, table->field[19]))
2308   ) {
2309     share->tgt_default_groups_lengths[link_idx] = strlen(ptr);
2310     share->tgt_default_groups[link_idx] =
2311       spider_create_string(ptr, share->tgt_default_groups_lengths[link_idx]);
2312   } else {
2313     share->tgt_default_groups_lengths[link_idx] = 0;
2314     share->tgt_default_groups[link_idx] = NULL;
2315   }
2316   if (
2317     !table->field[20]->is_null() &&
2318     (ptr = get_field(mem_root, table->field[20]))
2319   ) {
2320     share->tgt_dbs_lengths[link_idx] = strlen(ptr);
2321     share->tgt_dbs[link_idx] =
2322       spider_create_string(ptr, share->tgt_dbs_lengths[link_idx]);
2323   } else {
2324     share->tgt_dbs_lengths[link_idx] = 0;
2325     share->tgt_dbs[link_idx] = NULL;
2326   }
2327   if (
2328     !table->field[21]->is_null() &&
2329     (ptr = get_field(mem_root, table->field[21]))
2330   ) {
2331     share->tgt_table_names_lengths[link_idx] = strlen(ptr);
2332     share->tgt_table_names[link_idx] =
2333       spider_create_string(ptr, share->tgt_table_names_lengths[link_idx]);
2334   } else {
2335     share->tgt_table_names_lengths[link_idx] = 0;
2336     share->tgt_table_names[link_idx] = NULL;
2337   }
2338   if (
2339     !table->field[24]->is_null() &&
2340     (ptr = get_field(mem_root, table->field[24]))
2341   ) {
2342     share->static_link_ids_lengths[link_idx] = strlen(ptr);
2343     share->static_link_ids[link_idx] =
2344       spider_create_string(ptr, share->static_link_ids_lengths[link_idx]);
2345   } else {
2346     share->static_link_ids_lengths[link_idx] = 0;
2347     share->static_link_ids[link_idx] = NULL;
2348   }
2349   DBUG_RETURN(error_num);
2350 }
2351 
spider_get_sys_tables_monitoring_binlog_pos_at_failing(TABLE * table,long * monitoring_binlog_pos_at_failing,MEM_ROOT * mem_root)2352 int spider_get_sys_tables_monitoring_binlog_pos_at_failing(
2353   TABLE *table,
2354   long *monitoring_binlog_pos_at_failing,
2355   MEM_ROOT *mem_root
2356 ) {
2357   char *ptr;
2358   int error_num = 0;
2359   DBUG_ENTER("spider_get_sys_tables_monitoring_binlog_pos_at_failing");
2360   if ((ptr = get_field(mem_root, table->field[17])))
2361     *monitoring_binlog_pos_at_failing = (long) my_strtoll10(ptr, (char**) NULL,
2362       &error_num);
2363   else
2364     *monitoring_binlog_pos_at_failing = 1;
2365   DBUG_PRINT("info",("spider monitoring_binlog_pos_at_failing=%ld",
2366     *monitoring_binlog_pos_at_failing));
2367   DBUG_RETURN(error_num);
2368 }
2369 
spider_get_sys_tables_link_status(TABLE * table,SPIDER_SHARE * share,int link_idx,MEM_ROOT * mem_root)2370 int spider_get_sys_tables_link_status(
2371   TABLE *table,
2372   SPIDER_SHARE *share,
2373   int link_idx,
2374   MEM_ROOT *mem_root
2375 ) {
2376   char *ptr;
2377   int error_num = 0;
2378   DBUG_ENTER("spider_get_sys_tables_link_status");
2379   if ((ptr = get_field(mem_root, table->field[22])))
2380   {
2381     share->link_statuses[link_idx] =
2382       (long) my_strtoll10(ptr, (char**) NULL, &error_num);
2383   } else
2384     share->link_statuses[link_idx] = 1;
2385   DBUG_PRINT("info",("spider link_statuses[%d]=%ld",
2386     link_idx, share->link_statuses[link_idx]));
2387   DBUG_RETURN(error_num);
2388 }
2389 
spider_get_sys_tables_link_status(TABLE * table,long * link_status,MEM_ROOT * mem_root)2390 int spider_get_sys_tables_link_status(
2391   TABLE *table,
2392   long *link_status,
2393   MEM_ROOT *mem_root
2394 ) {
2395   char *ptr;
2396   int error_num = 0;
2397   DBUG_ENTER("spider_get_sys_tables_link_status");
2398   if ((ptr = get_field(mem_root, table->field[22])))
2399     *link_status = (long) my_strtoll10(ptr, (char**) NULL, &error_num);
2400   else
2401     *link_status = 1;
2402   DBUG_PRINT("info",("spider link_statuses=%ld", *link_status));
2403   DBUG_RETURN(error_num);
2404 }
2405 
spider_get_sys_tables_link_idx(TABLE * table,int * link_idx,MEM_ROOT * mem_root)2406 int spider_get_sys_tables_link_idx(
2407   TABLE *table,
2408   int *link_idx,
2409   MEM_ROOT *mem_root
2410 ) {
2411   char *ptr;
2412   int error_num = 0;
2413   DBUG_ENTER("spider_get_sys_tables_link_idx");
2414   if ((ptr = get_field(mem_root, table->field[2])))
2415     *link_idx = (int) my_strtoll10(ptr, (char**) NULL, &error_num);
2416   else
2417     *link_idx = 1;
2418   DBUG_PRINT("info",("spider link_idx=%d", *link_idx));
2419   DBUG_RETURN(error_num);
2420 }
2421 
spider_get_sys_tables_static_link_id(TABLE * table,char ** static_link_id,uint * static_link_id_length,MEM_ROOT * mem_root)2422 int spider_get_sys_tables_static_link_id(
2423   TABLE *table,
2424   char **static_link_id,
2425   uint *static_link_id_length,
2426   MEM_ROOT *mem_root
2427 ) {
2428   int error_num = 0;
2429   DBUG_ENTER("spider_get_sys_tables_static_link_id");
2430   if (
2431     !table->field[24]->is_null() &&
2432     (*static_link_id = get_field(mem_root, table->field[24]))
2433   ) {
2434     *static_link_id_length = strlen(*static_link_id);
2435   } else {
2436     *static_link_id_length = 0;
2437   }
2438   DBUG_PRINT("info",("spider static_link_id=%s", *static_link_id ? *static_link_id : "NULL"));
2439   DBUG_RETURN(error_num);
2440 }
2441 
spider_get_sys_table_sts_info(TABLE * table,ulonglong * data_file_length,ulonglong * max_data_file_length,ulonglong * index_file_length,ha_rows * records,ulong * mean_rec_length,time_t * check_time,time_t * create_time,time_t * update_time)2442 void spider_get_sys_table_sts_info(
2443   TABLE *table,
2444   ulonglong *data_file_length,
2445   ulonglong *max_data_file_length,
2446   ulonglong *index_file_length,
2447   ha_rows *records,
2448   ulong *mean_rec_length,
2449   time_t *check_time,
2450   time_t *create_time,
2451   time_t *update_time
2452 ) {
2453   MYSQL_TIME mysql_time;
2454 #ifdef MARIADB_BASE_VERSION
2455   uint not_used_uint;
2456 #else
2457   my_bool not_used_my_bool;
2458 #endif
2459   long not_used_long;
2460   DBUG_ENTER("spider_get_sys_table_sts_info");
2461   *data_file_length = (ulonglong) table->field[2]->val_int();
2462   *max_data_file_length = (ulonglong) table->field[3]->val_int();
2463   *index_file_length = (ulonglong) table->field[4]->val_int();
2464   *records = (ha_rows) table->field[5]->val_int();
2465   *mean_rec_length = (ulong) table->field[6]->val_int();
2466   table->field[7]->get_date(&mysql_time, SPIDER_date_mode_t(0));
2467 #ifdef MARIADB_BASE_VERSION
2468   *check_time = (time_t) my_system_gmt_sec(&mysql_time,
2469     &not_used_long, &not_used_uint);
2470 #else
2471   *check_time = (time_t) my_system_gmt_sec(&mysql_time,
2472     &not_used_long, &not_used_my_bool);
2473 #endif
2474   table->field[8]->get_date(&mysql_time, SPIDER_date_mode_t(0));
2475 #ifdef MARIADB_BASE_VERSION
2476   *create_time = (time_t) my_system_gmt_sec(&mysql_time,
2477     &not_used_long, &not_used_uint);
2478 #else
2479   *create_time = (time_t) my_system_gmt_sec(&mysql_time,
2480     &not_used_long, &not_used_my_bool);
2481 #endif
2482   table->field[9]->get_date(&mysql_time, SPIDER_date_mode_t(0));
2483 #ifdef MARIADB_BASE_VERSION
2484   *update_time = (time_t) my_system_gmt_sec(&mysql_time,
2485     &not_used_long, &not_used_uint);
2486 #else
2487   *update_time = (time_t) my_system_gmt_sec(&mysql_time,
2488     &not_used_long, &not_used_my_bool);
2489 #endif
2490   DBUG_VOID_RETURN;
2491 }
2492 
spider_get_sys_table_crd_info(TABLE * table,longlong * cardinality,uint number_of_keys)2493 void spider_get_sys_table_crd_info(
2494   TABLE *table,
2495   longlong *cardinality,
2496   uint number_of_keys
2497 ) {
2498   uint seq;
2499   DBUG_ENTER("spider_get_sys_table_crd_info");
2500   seq = (uint) table->field[2]->val_int();
2501   if (seq < number_of_keys)
2502   {
2503     cardinality[seq] = (longlong) table->field[3]->val_int();
2504   }
2505   DBUG_VOID_RETURN;
2506 }
2507 
spider_sys_update_tables_link_status(THD * thd,char * name,uint name_length,int link_idx,long link_status,bool need_lock)2508 int spider_sys_update_tables_link_status(
2509   THD *thd,
2510   char *name,
2511   uint name_length,
2512   int link_idx,
2513   long link_status,
2514   bool need_lock
2515 ) {
2516   int error_num;
2517   TABLE *table_tables = NULL;
2518 #if MYSQL_VERSION_ID < 50500
2519   Open_tables_state open_tables_backup;
2520 #else
2521   Open_tables_backup open_tables_backup;
2522 #endif
2523   DBUG_ENTER("spider_sys_update_tables_link_status");
2524   if (
2525     !(table_tables = spider_open_sys_table(
2526       thd, SPIDER_SYS_TABLES_TABLE_NAME_STR,
2527       SPIDER_SYS_TABLES_TABLE_NAME_LEN, TRUE, &open_tables_backup, need_lock,
2528       &error_num))
2529   ) {
2530     goto error;
2531   }
2532   if ((error_num = spider_update_tables_link_status(table_tables,
2533     name, name_length, link_idx, link_status)))
2534     goto error;
2535   spider_close_sys_table(thd, table_tables,
2536     &open_tables_backup, need_lock);
2537   table_tables = NULL;
2538   DBUG_RETURN(0);
2539 
2540 error:
2541   if (table_tables)
2542     spider_close_sys_table(thd, table_tables,
2543       &open_tables_backup, need_lock);
2544   DBUG_RETURN(error_num);
2545 }
2546 
spider_sys_log_tables_link_failed(THD * thd,char * name,uint name_length,int link_idx,bool need_lock)2547 int spider_sys_log_tables_link_failed(
2548   THD *thd,
2549   char *name,
2550   uint name_length,
2551   int link_idx,
2552   bool need_lock
2553 ) {
2554   int error_num;
2555   TABLE *table_tables = NULL;
2556 #if MYSQL_VERSION_ID < 50500
2557   Open_tables_state open_tables_backup;
2558 #else
2559   Open_tables_backup open_tables_backup;
2560 #endif
2561   DBUG_ENTER("spider_sys_log_tables_link_failed");
2562   if (
2563     !(table_tables = spider_open_sys_table(
2564       thd, SPIDER_SYS_LINK_FAILED_TABLE_NAME_STR,
2565       SPIDER_SYS_LINK_FAILED_TABLE_NAME_LEN, TRUE, &open_tables_backup,
2566       need_lock, &error_num))
2567   ) {
2568     goto error;
2569   }
2570   empty_record(table_tables);
2571   if ((error_num = spider_log_tables_link_failed(table_tables,
2572     name, name_length, link_idx)))
2573     goto error;
2574   spider_close_sys_table(thd, table_tables,
2575     &open_tables_backup, need_lock);
2576   table_tables = NULL;
2577   DBUG_RETURN(0);
2578 
2579 error:
2580   if (table_tables)
2581     spider_close_sys_table(thd, table_tables,
2582       &open_tables_backup, need_lock);
2583   DBUG_RETURN(error_num);
2584 }
2585 
spider_sys_log_xa_failed(THD * thd,XID * xid,SPIDER_CONN * conn,const char * status,bool need_lock)2586 int spider_sys_log_xa_failed(
2587   THD *thd,
2588   XID *xid,
2589   SPIDER_CONN *conn,
2590   const char *status,
2591   bool need_lock
2592 ) {
2593   int error_num;
2594   TABLE *table_tables = NULL;
2595 #if MYSQL_VERSION_ID < 50500
2596   Open_tables_state open_tables_backup;
2597 #else
2598   Open_tables_backup open_tables_backup;
2599 #endif
2600   DBUG_ENTER("spider_sys_log_xa_failed");
2601   if (
2602     !(table_tables = spider_open_sys_table(
2603       thd, SPIDER_SYS_XA_FAILED_TABLE_NAME_STR,
2604       SPIDER_SYS_XA_FAILED_TABLE_NAME_LEN, TRUE, &open_tables_backup,
2605       need_lock, &error_num))
2606   ) {
2607     goto error;
2608   }
2609   empty_record(table_tables);
2610   if ((error_num = spider_log_xa_failed(thd, table_tables, xid, conn, status)))
2611     goto error;
2612   spider_close_sys_table(thd, table_tables, &open_tables_backup, need_lock);
2613   table_tables = NULL;
2614   DBUG_RETURN(0);
2615 
2616 error:
2617   if (table_tables)
2618     spider_close_sys_table(thd, table_tables, &open_tables_backup, need_lock);
2619   DBUG_RETURN(error_num);
2620 }
2621 
spider_get_sys_link_mon_key(TABLE * table,SPIDER_MON_KEY * mon_key,MEM_ROOT * mem_root,int * same)2622 int spider_get_sys_link_mon_key(
2623   TABLE *table,
2624   SPIDER_MON_KEY *mon_key,
2625   MEM_ROOT *mem_root,
2626   int *same
2627 ) {
2628   char *db_name, *table_name, *link_id;
2629   uint db_name_length, table_name_length, link_id_length;
2630   DBUG_ENTER("spider_get_sys_link_mon_key");
2631   if (
2632     table->field[0]->is_null() ||
2633     table->field[1]->is_null() ||
2634     table->field[2]->is_null()
2635   ) {
2636     my_printf_error(ER_SPIDER_SYS_TABLE_VERSION_NUM,
2637       ER_SPIDER_SYS_TABLE_VERSION_STR, MYF(0),
2638       SPIDER_SYS_LINK_MON_TABLE_NAME_STR);
2639     DBUG_RETURN(ER_SPIDER_SYS_TABLE_VERSION_NUM);
2640   }
2641 
2642   if (
2643     !(db_name = get_field(mem_root, table->field[0])) ||
2644     !(table_name = get_field(mem_root, table->field[1])) ||
2645     !(link_id = get_field(mem_root, table->field[2]))
2646   )
2647     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2648 
2649   db_name_length = strlen(db_name);
2650   table_name_length = strlen(table_name);
2651   link_id_length = strlen(link_id);
2652 
2653   if (
2654     db_name_length > SPIDER_SYS_LINK_MON_TABLE_DB_NAME_SIZE ||
2655     table_name_length > SPIDER_SYS_LINK_MON_TABLE_TABLE_NAME_SIZE ||
2656     link_id_length > SPIDER_SYS_LINK_MON_TABLE_LINK_ID_SIZE
2657   ) {
2658     my_printf_error(ER_SPIDER_SYS_TABLE_VERSION_NUM,
2659       ER_SPIDER_SYS_TABLE_VERSION_STR, MYF(0),
2660       SPIDER_SYS_LINK_MON_TABLE_NAME_STR);
2661     DBUG_RETURN(ER_SPIDER_SYS_TABLE_VERSION_NUM);
2662   }
2663 
2664   if (
2665     db_name_length == mon_key->db_name_length &&
2666     table_name_length == mon_key->table_name_length &&
2667     link_id_length == mon_key->link_id_length &&
2668     !memcmp(db_name, mon_key->db_name, db_name_length) &&
2669     !memcmp(table_name, mon_key->table_name, table_name_length) &&
2670     !memcmp(link_id, mon_key->link_id, link_id_length)
2671   ) {
2672     /* same key */
2673     *same = 1;
2674     DBUG_RETURN(0);
2675   }
2676 
2677   *same = 0;
2678   mon_key->db_name_length = db_name_length;
2679   memcpy(mon_key->db_name, db_name, db_name_length + 1);
2680   mon_key->table_name_length = table_name_length;
2681   memcpy(mon_key->table_name, table_name, table_name_length + 1);
2682   mon_key->link_id_length = link_id_length;
2683   memcpy(mon_key->link_id, link_id, link_id_length + 1);
2684   DBUG_RETURN(0);
2685 }
2686 
spider_get_sys_link_mon_server_id(TABLE * table,uint32 * server_id,MEM_ROOT * mem_root)2687 int spider_get_sys_link_mon_server_id(
2688   TABLE *table,
2689   uint32 *server_id,
2690   MEM_ROOT *mem_root
2691 ) {
2692   char *ptr;
2693   int error_num = 0;
2694   DBUG_ENTER("spider_get_sys_link_mon_server_id");
2695   if ((ptr = get_field(mem_root, table->field[3])))
2696     *server_id = (uint32) my_strtoll10(ptr, (char**) NULL, &error_num);
2697   else
2698     *server_id = ~(uint32) 0;
2699   DBUG_RETURN(error_num);
2700 }
2701 
spider_get_sys_link_mon_connect_info(TABLE * table,SPIDER_SHARE * share,int link_idx,MEM_ROOT * mem_root)2702 int spider_get_sys_link_mon_connect_info(
2703   TABLE *table,
2704   SPIDER_SHARE *share,
2705   int link_idx,
2706   MEM_ROOT *mem_root
2707 ) {
2708   char *ptr;
2709   int error_num = 0;
2710   DBUG_ENTER("spider_get_sys_link_mon_connect_info");
2711   if (
2712     !table->field[4]->is_null() &&
2713     (ptr = get_field(mem_root, table->field[4]))
2714   ) {
2715     share->server_names_lengths[link_idx] = strlen(ptr);
2716     share->server_names[link_idx] =
2717       spider_create_string(ptr, share->server_names_lengths[link_idx]);
2718   } else {
2719     share->server_names_lengths[link_idx] = 0;
2720     share->server_names[link_idx] = NULL;
2721   }
2722   if (
2723     !table->field[5]->is_null() &&
2724     (ptr = get_field(mem_root, table->field[5]))
2725   ) {
2726     share->tgt_wrappers_lengths[link_idx] = strlen(ptr);
2727     share->tgt_wrappers[link_idx] =
2728       spider_create_string(ptr, share->tgt_wrappers_lengths[link_idx]);
2729   } else {
2730     share->tgt_wrappers_lengths[link_idx] = 0;
2731     share->tgt_wrappers[link_idx] = NULL;
2732   }
2733   if (
2734     !table->field[6]->is_null() &&
2735     (ptr = get_field(mem_root, table->field[6]))
2736   ) {
2737     share->tgt_hosts_lengths[link_idx] = strlen(ptr);
2738     share->tgt_hosts[link_idx] =
2739       spider_create_string(ptr, share->tgt_hosts_lengths[link_idx]);
2740   } else {
2741     share->tgt_hosts_lengths[link_idx] = 0;
2742     share->tgt_hosts[link_idx] = NULL;
2743   }
2744   if (
2745     !table->field[7]->is_null() &&
2746     (ptr = get_field(mem_root, table->field[7]))
2747   ) {
2748     share->tgt_ports[link_idx] = atol(ptr);
2749   } else {
2750     share->tgt_ports[link_idx] = -1;
2751   }
2752   if (
2753     !table->field[8]->is_null() &&
2754     (ptr = get_field(mem_root, table->field[8]))
2755   ) {
2756     share->tgt_sockets_lengths[link_idx] = strlen(ptr);
2757     share->tgt_sockets[link_idx] =
2758       spider_create_string(ptr, share->tgt_sockets_lengths[link_idx]);
2759   } else {
2760     share->tgt_sockets_lengths[link_idx] = 0;
2761     share->tgt_sockets[link_idx] = NULL;
2762   }
2763   if (
2764     !table->field[9]->is_null() &&
2765     (ptr = get_field(mem_root, table->field[9]))
2766   ) {
2767     share->tgt_usernames_lengths[link_idx] = strlen(ptr);
2768     share->tgt_usernames[link_idx] =
2769       spider_create_string(ptr, share->tgt_usernames_lengths[link_idx]);
2770   } else {
2771     share->tgt_usernames_lengths[link_idx] = 0;
2772     share->tgt_usernames[link_idx] = NULL;
2773   }
2774   if (
2775     !table->field[10]->is_null() &&
2776     (ptr = get_field(mem_root, table->field[10]))
2777   ) {
2778     share->tgt_passwords_lengths[link_idx] = strlen(ptr);
2779     share->tgt_passwords[link_idx] =
2780       spider_create_string(ptr, share->tgt_passwords_lengths[link_idx]);
2781   } else {
2782     share->tgt_passwords_lengths[link_idx] = 0;
2783     share->tgt_passwords[link_idx] = NULL;
2784   }
2785   if (
2786     !table->field[11]->is_null() &&
2787     (ptr = get_field(mem_root, table->field[11]))
2788   ) {
2789     share->tgt_ssl_cas_lengths[link_idx] = strlen(ptr);
2790     share->tgt_ssl_cas[link_idx] =
2791       spider_create_string(ptr, share->tgt_ssl_cas_lengths[link_idx]);
2792   } else {
2793     share->tgt_ssl_cas_lengths[link_idx] = 0;
2794     share->tgt_ssl_cas[link_idx] = NULL;
2795   }
2796   if (
2797     !table->field[12]->is_null() &&
2798     (ptr = get_field(mem_root, table->field[12]))
2799   ) {
2800     share->tgt_ssl_capaths_lengths[link_idx] = strlen(ptr);
2801     share->tgt_ssl_capaths[link_idx] =
2802       spider_create_string(ptr, share->tgt_ssl_capaths_lengths[link_idx]);
2803   } else {
2804     share->tgt_ssl_capaths_lengths[link_idx] = 0;
2805     share->tgt_ssl_capaths[link_idx] = NULL;
2806   }
2807   if (
2808     !table->field[13]->is_null() &&
2809     (ptr = get_field(mem_root, table->field[13]))
2810   ) {
2811     share->tgt_ssl_certs_lengths[link_idx] = strlen(ptr);
2812     share->tgt_ssl_certs[link_idx] =
2813       spider_create_string(ptr, share->tgt_ssl_certs_lengths[link_idx]);
2814   } else {
2815     share->tgt_ssl_certs_lengths[link_idx] = 0;
2816     share->tgt_ssl_certs[link_idx] = NULL;
2817   }
2818   if (
2819     !table->field[14]->is_null() &&
2820     (ptr = get_field(mem_root, table->field[14]))
2821   ) {
2822     share->tgt_ssl_ciphers_lengths[link_idx] = strlen(ptr);
2823     share->tgt_ssl_ciphers[link_idx] =
2824       spider_create_string(ptr, share->tgt_ssl_ciphers_lengths[link_idx]);
2825   } else {
2826     share->tgt_ssl_ciphers_lengths[link_idx] = 0;
2827     share->tgt_ssl_ciphers[link_idx] = NULL;
2828   }
2829   if (
2830     !table->field[15]->is_null() &&
2831     (ptr = get_field(mem_root, table->field[15]))
2832   ) {
2833     share->tgt_ssl_keys_lengths[link_idx] = strlen(ptr);
2834     share->tgt_ssl_keys[link_idx] =
2835       spider_create_string(ptr, share->tgt_ssl_keys_lengths[link_idx]);
2836   } else {
2837     share->tgt_ssl_keys_lengths[link_idx] = 0;
2838     share->tgt_ssl_keys[link_idx] = NULL;
2839   }
2840   if (
2841     !table->field[16]->is_null() &&
2842     (ptr = get_field(mem_root, table->field[16]))
2843   ) {
2844     share->tgt_ssl_vscs[link_idx] = atol(ptr);
2845   } else
2846     share->tgt_ssl_vscs[link_idx] = -1;
2847   if (
2848     !table->field[17]->is_null() &&
2849     (ptr = get_field(mem_root, table->field[17]))
2850   ) {
2851     share->tgt_default_files_lengths[link_idx] = strlen(ptr);
2852     share->tgt_default_files[link_idx] =
2853       spider_create_string(ptr, share->tgt_default_files_lengths[link_idx]);
2854   } else {
2855     share->tgt_default_files_lengths[link_idx] = 0;
2856     share->tgt_default_files[link_idx] = NULL;
2857   }
2858   if (
2859     !table->field[18]->is_null() &&
2860     (ptr = get_field(mem_root, table->field[18]))
2861   ) {
2862     share->tgt_default_groups_lengths[link_idx] = strlen(ptr);
2863     share->tgt_default_groups[link_idx] =
2864       spider_create_string(ptr, share->tgt_default_groups_lengths[link_idx]);
2865   } else {
2866     share->tgt_default_groups_lengths[link_idx] = 0;
2867     share->tgt_default_groups[link_idx] = NULL;
2868   }
2869   DBUG_RETURN(error_num);
2870 }
2871 
spider_get_link_statuses(TABLE * table,SPIDER_SHARE * share,MEM_ROOT * mem_root)2872 int spider_get_link_statuses(
2873   TABLE *table,
2874   SPIDER_SHARE *share,
2875   MEM_ROOT *mem_root
2876 ) {
2877   int error_num, roop_count;
2878   char table_key[MAX_KEY_LENGTH];
2879   DBUG_ENTER("spider_get_link_statuses");
2880   table->use_all_columns();
2881   spider_store_tables_name(table, share->table_name,
2882     share->table_name_length);
2883   for (roop_count = 0; roop_count < (int) share->link_count; roop_count++)
2884   {
2885     spider_store_tables_link_idx(table, roop_count);
2886     if ((error_num = spider_check_sys_table(table, table_key)))
2887     {
2888       if (
2889         (error_num == HA_ERR_KEY_NOT_FOUND || error_num == HA_ERR_END_OF_FILE)
2890       ) {
2891 /*
2892         table->file->print_error(error_num, MYF(0));
2893 */
2894         DBUG_RETURN(error_num);
2895       }
2896     } else if ((error_num =
2897       spider_get_sys_tables_link_status(table, share, roop_count, mem_root)))
2898     {
2899       table->file->print_error(error_num, MYF(0));
2900       DBUG_RETURN(error_num);
2901     }
2902   }
2903   DBUG_RETURN(0);
2904 }
2905 
spider_sys_insert_or_update_table_sts(THD * thd,const char * name,uint name_length,ulonglong * data_file_length,ulonglong * max_data_file_length,ulonglong * index_file_length,ha_rows * records,ulong * mean_rec_length,time_t * check_time,time_t * create_time,time_t * update_time,bool need_lock)2906 int spider_sys_insert_or_update_table_sts(
2907   THD *thd,
2908   const char *name,
2909   uint name_length,
2910   ulonglong *data_file_length,
2911   ulonglong *max_data_file_length,
2912   ulonglong *index_file_length,
2913   ha_rows *records,
2914   ulong *mean_rec_length,
2915   time_t *check_time,
2916   time_t *create_time,
2917   time_t *update_time,
2918   bool need_lock
2919 ) {
2920   int error_num;
2921   TABLE *table_sts = NULL;
2922 #if MYSQL_VERSION_ID < 50500
2923   Open_tables_state open_tables_backup;
2924 #else
2925   Open_tables_backup open_tables_backup;
2926 #endif
2927   DBUG_ENTER("spider_sys_insert_or_update_table_sts");
2928   if (
2929     !(table_sts = spider_open_sys_table(
2930       thd, SPIDER_SYS_TABLE_STS_TABLE_NAME_STR,
2931       SPIDER_SYS_TABLE_STS_TABLE_NAME_LEN, TRUE,
2932       &open_tables_backup, need_lock, &error_num))
2933   ) {
2934     goto error;
2935   }
2936   if ((error_num = spider_insert_or_update_table_sts(
2937     table_sts,
2938     name,
2939     name_length,
2940     data_file_length,
2941     max_data_file_length,
2942     index_file_length,
2943     records,
2944     mean_rec_length,
2945     check_time,
2946     create_time,
2947     update_time
2948   )))
2949     goto error;
2950   spider_close_sys_table(thd, table_sts, &open_tables_backup, need_lock);
2951   table_sts = NULL;
2952   DBUG_RETURN(0);
2953 
2954 error:
2955   if (table_sts)
2956     spider_close_sys_table(thd, table_sts, &open_tables_backup, need_lock);
2957   DBUG_RETURN(error_num);
2958 }
2959 
spider_sys_insert_or_update_table_crd(THD * thd,const char * name,uint name_length,longlong * cardinality,uint number_of_keys,bool need_lock)2960 int spider_sys_insert_or_update_table_crd(
2961   THD *thd,
2962   const char *name,
2963   uint name_length,
2964   longlong *cardinality,
2965   uint number_of_keys,
2966   bool need_lock
2967 ) {
2968   int error_num;
2969   TABLE *table_crd = NULL;
2970 #if MYSQL_VERSION_ID < 50500
2971   Open_tables_state open_tables_backup;
2972 #else
2973   Open_tables_backup open_tables_backup;
2974 #endif
2975   DBUG_ENTER("spider_sys_insert_or_update_table_crd");
2976   if (
2977     !(table_crd = spider_open_sys_table(
2978       thd, SPIDER_SYS_TABLE_CRD_TABLE_NAME_STR,
2979       SPIDER_SYS_TABLE_CRD_TABLE_NAME_LEN, TRUE,
2980       &open_tables_backup, need_lock, &error_num))
2981   ) {
2982     goto error;
2983   }
2984   if ((error_num = spider_insert_or_update_table_crd(
2985     table_crd,
2986     name,
2987     name_length,
2988     cardinality,
2989     number_of_keys
2990   )))
2991     goto error;
2992   spider_close_sys_table(thd, table_crd, &open_tables_backup, need_lock);
2993   table_crd = NULL;
2994   DBUG_RETURN(0);
2995 
2996 error:
2997   if (table_crd)
2998     spider_close_sys_table(thd, table_crd, &open_tables_backup, need_lock);
2999   DBUG_RETURN(error_num);
3000 }
3001 
spider_sys_delete_table_sts(THD * thd,const char * name,uint name_length,bool need_lock)3002 int spider_sys_delete_table_sts(
3003   THD *thd,
3004   const char *name,
3005   uint name_length,
3006   bool need_lock
3007 ) {
3008   int error_num;
3009   TABLE *table_sts = NULL;
3010 #if MYSQL_VERSION_ID < 50500
3011   Open_tables_state open_tables_backup;
3012 #else
3013   Open_tables_backup open_tables_backup;
3014 #endif
3015   DBUG_ENTER("spider_sys_delete_table_sts");
3016   if (
3017     !(table_sts = spider_open_sys_table(
3018       thd, SPIDER_SYS_TABLE_STS_TABLE_NAME_STR,
3019       SPIDER_SYS_TABLE_STS_TABLE_NAME_LEN, TRUE,
3020       &open_tables_backup, need_lock, &error_num))
3021   ) {
3022     goto error;
3023   }
3024   if ((error_num = spider_delete_table_sts(
3025     table_sts,
3026     name,
3027     name_length
3028   )))
3029     goto error;
3030   spider_close_sys_table(thd, table_sts, &open_tables_backup, need_lock);
3031   table_sts = NULL;
3032   DBUG_RETURN(0);
3033 
3034 error:
3035   if (table_sts)
3036     spider_close_sys_table(thd, table_sts, &open_tables_backup, need_lock);
3037   DBUG_RETURN(error_num);
3038 }
3039 
spider_sys_delete_table_crd(THD * thd,const char * name,uint name_length,bool need_lock)3040 int spider_sys_delete_table_crd(
3041   THD *thd,
3042   const char *name,
3043   uint name_length,
3044   bool need_lock
3045 ) {
3046   int error_num;
3047   TABLE *table_crd = NULL;
3048 #if MYSQL_VERSION_ID < 50500
3049   Open_tables_state open_tables_backup;
3050 #else
3051   Open_tables_backup open_tables_backup;
3052 #endif
3053   DBUG_ENTER("spider_sys_delete_table_crd");
3054   if (
3055     !(table_crd = spider_open_sys_table(
3056       thd, SPIDER_SYS_TABLE_CRD_TABLE_NAME_STR,
3057       SPIDER_SYS_TABLE_CRD_TABLE_NAME_LEN, TRUE,
3058       &open_tables_backup, need_lock, &error_num))
3059   ) {
3060     goto error;
3061   }
3062   if ((error_num = spider_delete_table_crd(
3063     table_crd,
3064     name,
3065     name_length
3066   )))
3067     goto error;
3068   spider_close_sys_table(thd, table_crd, &open_tables_backup, need_lock);
3069   table_crd = NULL;
3070   DBUG_RETURN(0);
3071 
3072 error:
3073   if (table_crd)
3074     spider_close_sys_table(thd, table_crd, &open_tables_backup, need_lock);
3075   DBUG_RETURN(error_num);
3076 }
3077 
spider_sys_get_table_sts(THD * thd,const char * name,uint name_length,ulonglong * data_file_length,ulonglong * max_data_file_length,ulonglong * index_file_length,ha_rows * records,ulong * mean_rec_length,time_t * check_time,time_t * create_time,time_t * update_time,bool need_lock)3078 int spider_sys_get_table_sts(
3079   THD *thd,
3080   const char *name,
3081   uint name_length,
3082   ulonglong *data_file_length,
3083   ulonglong *max_data_file_length,
3084   ulonglong *index_file_length,
3085   ha_rows *records,
3086   ulong *mean_rec_length,
3087   time_t *check_time,
3088   time_t *create_time,
3089   time_t *update_time,
3090   bool need_lock
3091 ) {
3092   int error_num;
3093   char table_key[MAX_KEY_LENGTH];
3094   TABLE *table_sts = NULL;
3095 #if MYSQL_VERSION_ID < 50500
3096   Open_tables_state open_tables_backup;
3097 #else
3098   Open_tables_backup open_tables_backup;
3099 #endif
3100   DBUG_ENTER("spider_sys_get_table_sts");
3101   if (
3102     !(table_sts = spider_open_sys_table(
3103       thd, SPIDER_SYS_TABLE_STS_TABLE_NAME_STR,
3104       SPIDER_SYS_TABLE_STS_TABLE_NAME_LEN, TRUE,
3105       &open_tables_backup, need_lock, &error_num))
3106   ) {
3107     goto error;
3108   }
3109 
3110   table_sts->use_all_columns();
3111   spider_store_tables_name(table_sts, name, name_length);
3112   if ((error_num = spider_check_sys_table(table_sts, table_key)))
3113   {
3114     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
3115     {
3116       table_sts->file->print_error(error_num, MYF(0));
3117     }
3118     goto error;
3119   } else {
3120     spider_get_sys_table_sts_info(
3121       table_sts,
3122       data_file_length,
3123       max_data_file_length,
3124       index_file_length,
3125       records,
3126       mean_rec_length,
3127       check_time,
3128       create_time,
3129       update_time
3130     );
3131   }
3132 
3133   spider_close_sys_table(thd, table_sts, &open_tables_backup, need_lock);
3134   table_sts = NULL;
3135   DBUG_RETURN(0);
3136 
3137 error:
3138   if (table_sts)
3139     spider_close_sys_table(thd, table_sts, &open_tables_backup, need_lock);
3140   DBUG_RETURN(error_num);
3141 }
3142 
spider_sys_get_table_crd(THD * thd,const char * name,uint name_length,longlong * cardinality,uint number_of_keys,bool need_lock)3143 int spider_sys_get_table_crd(
3144   THD *thd,
3145   const char *name,
3146   uint name_length,
3147   longlong *cardinality,
3148   uint number_of_keys,
3149   bool need_lock
3150 ) {
3151   int error_num;
3152   char table_key[MAX_KEY_LENGTH];
3153   bool index_inited = FALSE;
3154   TABLE *table_crd = NULL;
3155 #if MYSQL_VERSION_ID < 50500
3156   Open_tables_state open_tables_backup;
3157 #else
3158   Open_tables_backup open_tables_backup;
3159 #endif
3160   DBUG_ENTER("spider_sys_get_table_crd");
3161   if (
3162     !(table_crd = spider_open_sys_table(
3163       thd, SPIDER_SYS_TABLE_CRD_TABLE_NAME_STR,
3164       SPIDER_SYS_TABLE_CRD_TABLE_NAME_LEN, TRUE,
3165       &open_tables_backup, need_lock, &error_num))
3166   ) {
3167     goto error;
3168   }
3169 
3170   table_crd->use_all_columns();
3171   spider_store_tables_name(table_crd, name, name_length);
3172   if ((error_num = spider_get_sys_table_by_idx(table_crd, table_key, 0,
3173     SPIDER_SYS_TABLE_CRD_PK_COL_CNT - 1)))
3174   {
3175     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
3176     {
3177       table_crd->file->print_error(error_num, MYF(0));
3178     }
3179     goto error;
3180   } else {
3181     index_inited = TRUE;
3182     do {
3183       spider_get_sys_table_crd_info(
3184         table_crd,
3185         cardinality,
3186         number_of_keys
3187       );
3188       error_num = spider_sys_index_next_same(table_crd, table_key);
3189     } while (error_num == 0);
3190   }
3191   index_inited = FALSE;
3192   if ((error_num = spider_sys_index_end(table_crd)))
3193   {
3194     table_crd->file->print_error(error_num, MYF(0));
3195     goto error;
3196   }
3197 
3198   spider_close_sys_table(thd, table_crd, &open_tables_backup, need_lock);
3199   table_crd = NULL;
3200   DBUG_RETURN(0);
3201 
3202 error:
3203   if (index_inited)
3204     spider_sys_index_end(table_crd);
3205   if (table_crd)
3206     spider_close_sys_table(thd, table_crd, &open_tables_backup, need_lock);
3207   DBUG_RETURN(error_num);
3208 }
3209 
spider_sys_replace(TABLE * table,bool * modified_non_trans_table)3210 int spider_sys_replace(
3211   TABLE *table,
3212   bool *modified_non_trans_table
3213 ) {
3214   int error_num, key_num;
3215   bool last_uniq_key;
3216   char table_key[MAX_KEY_LENGTH];
3217   DBUG_ENTER("spider_sys_replace");
3218 
3219   while ((error_num = spider_write_sys_table_row(table, FALSE)))
3220   {
3221     if (
3222       table->file->is_fatal_error(error_num, HA_CHECK_DUP) ||
3223       (key_num = table->file->get_dup_key(error_num)) < 0
3224     )
3225       goto error;
3226 
3227     if (table->file->ha_table_flags() & HA_DUPLICATE_POS)
3228     {
3229 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
3230       error_num = table->file->ha_rnd_pos(table->record[1],
3231         table->file->dup_ref);
3232 #else
3233       error_num = table->file->rnd_pos(table->record[1], table->file->dup_ref);
3234 #endif
3235       if (error_num)
3236       {
3237         if (error_num == HA_ERR_RECORD_DELETED)
3238           error_num = HA_ERR_KEY_NOT_FOUND;
3239         goto error;
3240       }
3241     } else {
3242       if ((error_num = table->file->extra(HA_EXTRA_FLUSH_CACHE)))
3243         goto error;
3244 
3245       key_copy((uchar*)table_key, table->record[0],
3246         table->key_info + key_num, 0);
3247 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
3248       error_num = table->file->ha_index_read_idx_map(table->record[1], key_num,
3249         (const uchar*)table_key, HA_WHOLE_KEY, HA_READ_KEY_EXACT);
3250 #else
3251       error_num = table->file->index_read_idx_map(table->record[1], key_num,
3252         (const uchar*)table_key, HA_WHOLE_KEY, HA_READ_KEY_EXACT);
3253 #endif
3254       if (error_num)
3255       {
3256         if (error_num == HA_ERR_RECORD_DELETED)
3257           error_num = HA_ERR_KEY_NOT_FOUND;
3258         goto error;
3259       }
3260     }
3261 
3262     last_uniq_key = TRUE;
3263     while (++key_num < (int) table->s->keys)
3264       if (table->key_info[key_num].flags & HA_NOSAME)
3265         last_uniq_key = FALSE;
3266 
3267     if (
3268       last_uniq_key &&
3269       !table->file->referenced_by_foreign_key()
3270     ) {
3271       if ((error_num = spider_update_sys_table_row(table)))
3272         goto error;
3273       DBUG_RETURN(0);
3274     } else {
3275       if ((error_num = spider_delete_sys_table_row(table, 1, FALSE)))
3276         goto error;
3277       *modified_non_trans_table = TRUE;
3278     }
3279   }
3280 
3281   DBUG_RETURN(0);
3282 
3283 error:
3284   DBUG_RETURN(error_num);
3285 }
3286 
3287 #ifdef SPIDER_use_LEX_CSTRING_for_Field_blob_constructor
spider_mk_sys_tmp_table(THD * thd,TABLE * table,TMP_TABLE_PARAM * tmp_tbl_prm,const LEX_CSTRING * field_name,CHARSET_INFO * cs)3288 TABLE *spider_mk_sys_tmp_table(
3289   THD *thd,
3290   TABLE *table,
3291   TMP_TABLE_PARAM *tmp_tbl_prm,
3292   const LEX_CSTRING *field_name,
3293   CHARSET_INFO *cs
3294 )
3295 #else
3296 TABLE *spider_mk_sys_tmp_table(
3297   THD *thd,
3298   TABLE *table,
3299   TMP_TABLE_PARAM *tmp_tbl_prm,
3300   const char *field_name,
3301   CHARSET_INFO *cs
3302 )
3303 #endif
3304 {
3305   Field_blob *field;
3306   Item_field *i_field;
3307   List<Item> i_list;
3308   TABLE *tmp_table;
3309   DBUG_ENTER("spider_mk_sys_tmp_table");
3310 
3311 #ifdef SPIDER_FIELD_FIELDPTR_REQUIRES_THDPTR
3312   if (!(field = new (thd->mem_root) Field_blob(
3313     4294967295U, FALSE, field_name, cs, TRUE)))
3314     goto error_alloc_field;
3315 #else
3316   if (!(field = new Field_blob(
3317     4294967295U, FALSE, field_name, cs, TRUE)))
3318     goto error_alloc_field;
3319 #endif
3320   field->init(table);
3321 
3322 #ifdef SPIDER_FIELD_FIELDPTR_REQUIRES_THDPTR
3323   if (!(i_field = new (thd->mem_root) Item_field(thd, (Field *) field)))
3324     goto error_alloc_item_field;
3325 #else
3326   if (!(i_field = new Item_field((Field *) field)))
3327     goto error_alloc_item_field;
3328 #endif
3329 
3330   if (i_list.push_back(i_field))
3331     goto error_push_item;
3332 
3333   if (!(tmp_table = create_tmp_table(thd, tmp_tbl_prm,
3334     i_list, (ORDER*) NULL, FALSE, FALSE,
3335     (TMP_TABLE_FORCE_MYISAM | TMP_TABLE_ALL_COLUMNS),
3336     HA_POS_ERROR, &SPIDER_empty_string)))
3337     goto error_create_tmp_table;
3338   DBUG_RETURN(tmp_table);
3339 
3340 error_create_tmp_table:
3341 error_push_item:
3342   delete i_field;
3343 error_alloc_item_field:
3344   delete field;
3345 error_alloc_field:
3346   DBUG_RETURN(NULL);
3347 }
3348 
spider_rm_sys_tmp_table(THD * thd,TABLE * tmp_table,TMP_TABLE_PARAM * tmp_tbl_prm)3349 void spider_rm_sys_tmp_table(
3350   THD *thd,
3351   TABLE *tmp_table,
3352   TMP_TABLE_PARAM *tmp_tbl_prm
3353 ) {
3354   DBUG_ENTER("spider_rm_sys_tmp_table");
3355   free_tmp_table(thd, tmp_table);
3356   tmp_tbl_prm->cleanup();
3357   tmp_tbl_prm->field_count = 1;
3358   DBUG_VOID_RETURN;
3359 }
3360 
3361 #ifdef SPIDER_use_LEX_CSTRING_for_Field_blob_constructor
spider_mk_sys_tmp_table_for_result(THD * thd,TABLE * table,TMP_TABLE_PARAM * tmp_tbl_prm,const LEX_CSTRING * field_name1,const LEX_CSTRING * field_name2,const LEX_CSTRING * field_name3,CHARSET_INFO * cs)3362 TABLE *spider_mk_sys_tmp_table_for_result(
3363   THD *thd,
3364   TABLE *table,
3365   TMP_TABLE_PARAM *tmp_tbl_prm,
3366   const LEX_CSTRING *field_name1,
3367   const LEX_CSTRING *field_name2,
3368   const LEX_CSTRING *field_name3,
3369   CHARSET_INFO *cs
3370 )
3371 #else
3372 TABLE *spider_mk_sys_tmp_table_for_result(
3373   THD *thd,
3374   TABLE *table,
3375   TMP_TABLE_PARAM *tmp_tbl_prm,
3376   const char *field_name1,
3377   const char *field_name2,
3378   const char *field_name3,
3379   CHARSET_INFO *cs
3380 )
3381 #endif
3382 {
3383   Field_blob *field1, *field2, *field3;
3384   Item_field *i_field1, *i_field2, *i_field3;
3385   List<Item> i_list;
3386   TABLE *tmp_table;
3387   DBUG_ENTER("spider_mk_sys_tmp_table_for_result");
3388 
3389 #ifdef SPIDER_FIELD_FIELDPTR_REQUIRES_THDPTR
3390   if (!(field1 = new (thd->mem_root) Field_blob(
3391     4294967295U, FALSE, field_name1, cs, TRUE)))
3392     goto error_alloc_field1;
3393 #else
3394   if (!(field1 = new Field_blob(
3395     4294967295U, FALSE, field_name1, cs, TRUE)))
3396     goto error_alloc_field1;
3397 #endif
3398   field1->init(table);
3399 
3400 #ifdef SPIDER_FIELD_FIELDPTR_REQUIRES_THDPTR
3401   if (!(i_field1 = new (thd->mem_root) Item_field(thd, (Field *) field1)))
3402     goto error_alloc_item_field1;
3403 #else
3404   if (!(i_field1 = new Item_field((Field *) field1)))
3405     goto error_alloc_item_field1;
3406 #endif
3407 
3408   if (i_list.push_back(i_field1))
3409     goto error_push_item1;
3410 
3411 #ifdef SPIDER_FIELD_FIELDPTR_REQUIRES_THDPTR
3412   if (!(field2 = new (thd->mem_root) Field_blob(
3413     4294967295U, FALSE, field_name2, cs, TRUE)))
3414     goto error_alloc_field2;
3415 #else
3416   if (!(field2 = new Field_blob(
3417     4294967295U, FALSE, field_name2, cs, TRUE)))
3418     goto error_alloc_field2;
3419 #endif
3420   field2->init(table);
3421 
3422 #ifdef SPIDER_FIELD_FIELDPTR_REQUIRES_THDPTR
3423   if (!(i_field2 = new (thd->mem_root) Item_field(thd, (Field *) field2)))
3424     goto error_alloc_item_field2;
3425 #else
3426   if (!(i_field2 = new Item_field((Field *) field2)))
3427     goto error_alloc_item_field2;
3428 #endif
3429 
3430   if (i_list.push_back(i_field2))
3431     goto error_push_item2;
3432 
3433 #ifdef SPIDER_FIELD_FIELDPTR_REQUIRES_THDPTR
3434   if (!(field3 = new (thd->mem_root) Field_blob(
3435     4294967295U, FALSE, field_name3, cs, TRUE)))
3436     goto error_alloc_field3;
3437 #else
3438   if (!(field3 = new Field_blob(
3439     4294967295U, FALSE, field_name3, cs, TRUE)))
3440     goto error_alloc_field3;
3441 #endif
3442   field3->init(table);
3443 
3444 #ifdef SPIDER_FIELD_FIELDPTR_REQUIRES_THDPTR
3445   if (!(i_field3 = new (thd->mem_root) Item_field(thd, (Field *) field3)))
3446     goto error_alloc_item_field3;
3447 #else
3448   if (!(i_field3 = new Item_field((Field *) field3)))
3449     goto error_alloc_item_field3;
3450 #endif
3451 
3452   if (i_list.push_back(i_field3))
3453     goto error_push_item3;
3454 
3455   if (!(tmp_table = create_tmp_table(thd, tmp_tbl_prm,
3456     i_list, (ORDER*) NULL, FALSE, FALSE,
3457     (TMP_TABLE_FORCE_MYISAM | TMP_TABLE_ALL_COLUMNS),
3458     HA_POS_ERROR, &SPIDER_empty_string)))
3459     goto error_create_tmp_table;
3460   DBUG_RETURN(tmp_table);
3461 
3462 error_create_tmp_table:
3463 error_push_item3:
3464   delete i_field3;
3465 error_alloc_item_field3:
3466   delete field3;
3467 error_alloc_field3:
3468 error_push_item2:
3469   delete i_field2;
3470 error_alloc_item_field2:
3471   delete field2;
3472 error_alloc_field2:
3473 error_push_item1:
3474   delete i_field1;
3475 error_alloc_item_field1:
3476   delete field1;
3477 error_alloc_field1:
3478   DBUG_RETURN(NULL);
3479 }
3480 
spider_rm_sys_tmp_table_for_result(THD * thd,TABLE * tmp_table,TMP_TABLE_PARAM * tmp_tbl_prm)3481 void spider_rm_sys_tmp_table_for_result(
3482   THD *thd,
3483   TABLE *tmp_table,
3484   TMP_TABLE_PARAM *tmp_tbl_prm
3485 ) {
3486   DBUG_ENTER("spider_rm_sys_tmp_table_for_result");
3487   free_tmp_table(thd, tmp_table);
3488   tmp_tbl_prm->cleanup();
3489   tmp_tbl_prm->field_count = 3;
3490   DBUG_VOID_RETURN;
3491 }
3492 
spider_find_temporary_table(THD * thd,TABLE_LIST * table_list)3493 TABLE *spider_find_temporary_table(
3494   THD *thd,
3495   TABLE_LIST *table_list
3496 ) {
3497   DBUG_ENTER("spider_find_temporary_table");
3498 #ifdef SPIDER_open_temporary_table
3499   if (thd->open_temporary_table(table_list))
3500   {
3501     DBUG_RETURN(NULL);
3502   } else {
3503     DBUG_RETURN(table_list->table);
3504   }
3505 #else
3506   DBUG_RETURN(find_temporary_table(A,B));
3507 #endif
3508 }
3509