1/* Copyright (C) 2012-2017 Kentoku Shiba
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
15
16#define MYSQL_SERVER 1
17#include <my_global.h>
18#include "mysql_version.h"
19#include "spd_environ.h"
20#if MYSQL_VERSION_ID < 50500
21#include "mysql_priv.h"
22#include <mysql/plugin.h>
23#else
24#include "sql_priv.h"
25#include "probes_mysql.h"
26#include "sql_class.h"
27#include "sql_partition.h"
28#include "sql_analyse.h"
29#include "sql_base.h"
30#include "tztime.h"
31#ifdef HANDLER_HAS_DIRECT_AGGREGATE
32#include "sql_select.h"
33#endif
34#endif
35#include "sql_common.h"
36#include <mysql.h>
37#include <errmsg.h>
38#include "spd_err.h"
39#include "spd_param.h"
40#include "spd_db_include.h"
41#include "spd_include.h"
42#include "spd_db_mysql.h"
43#include "ha_spider.h"
44#include "spd_conn.h"
45#include "spd_db_conn.h"
46#include "spd_malloc.h"
47#include "spd_sys_table.h"
48#include "spd_table.h"
49
50extern struct charset_info_st *spd_charset_utf8_bin;
51extern bool volatile *spd_abort_loop;
52
53extern handlerton *spider_hton_ptr;
54extern pthread_mutex_t spider_open_conn_mutex;
55extern HASH spider_open_connections;
56extern HASH spider_ipport_conns;
57extern SPIDER_DBTON spider_dbton[SPIDER_DBTON_SIZE];
58extern const char spider_dig_upper[];
59
60#define SPIDER_SQL_NAME_QUOTE_STR "`"
61#define SPIDER_SQL_NAME_QUOTE_LEN (sizeof(SPIDER_SQL_NAME_QUOTE_STR) - 1)
62static const char *name_quote_str = SPIDER_SQL_NAME_QUOTE_STR;
63
64#define SPIDER_SQL_ISO_READ_UNCOMMITTED_STR "set session transaction isolation level read uncommitted"
65#define SPIDER_SQL_ISO_READ_UNCOMMITTED_LEN sizeof(SPIDER_SQL_ISO_READ_UNCOMMITTED_STR) - 1
66#define SPIDER_SQL_ISO_READ_COMMITTED_STR "set session transaction isolation level read committed"
67#define SPIDER_SQL_ISO_READ_COMMITTED_LEN sizeof(SPIDER_SQL_ISO_READ_COMMITTED_STR) - 1
68#define SPIDER_SQL_ISO_REPEATABLE_READ_STR "set session transaction isolation level repeatable read"
69#define SPIDER_SQL_ISO_REPEATABLE_READ_LEN sizeof(SPIDER_SQL_ISO_REPEATABLE_READ_STR) - 1
70#define SPIDER_SQL_ISO_SERIALIZABLE_STR "set session transaction isolation level serializable"
71#define SPIDER_SQL_ISO_SERIALIZABLE_LEN sizeof(SPIDER_SQL_ISO_SERIALIZABLE_STR) - 1
72
73#define SPIDER_SQL_START_CONSISTENT_SNAPSHOT_STR "start transaction with consistent snapshot"
74#define SPIDER_SQL_START_CONSISTENT_SNAPSHOT_LEN sizeof(SPIDER_SQL_START_CONSISTENT_SNAPSHOT_STR) - 1
75#define SPIDER_SQL_START_TRANSACTION_STR "start transaction"
76#define SPIDER_SQL_START_TRANSACTION_LEN sizeof(SPIDER_SQL_START_TRANSACTION_STR) - 1
77
78#define SPIDER_SQL_AUTOCOMMIT_OFF_STR "set session autocommit = 0"
79#define SPIDER_SQL_AUTOCOMMIT_OFF_LEN sizeof(SPIDER_SQL_AUTOCOMMIT_OFF_STR) - 1
80#define SPIDER_SQL_AUTOCOMMIT_ON_STR "set session autocommit = 1"
81#define SPIDER_SQL_AUTOCOMMIT_ON_LEN sizeof(SPIDER_SQL_AUTOCOMMIT_ON_STR) - 1
82
83#define SPIDER_SQL_SQL_LOG_OFF_STR "set session sql_log_off = 0"
84#define SPIDER_SQL_SQL_LOG_OFF_LEN sizeof(SPIDER_SQL_SQL_LOG_OFF_STR) - 1
85#define SPIDER_SQL_SQL_LOG_ON_STR "set session sql_log_off = 1"
86#define SPIDER_SQL_SQL_LOG_ON_LEN sizeof(SPIDER_SQL_SQL_LOG_ON_STR) - 1
87
88#define SPIDER_SQL_TIME_ZONE_STR "set session time_zone = '"
89#define SPIDER_SQL_TIME_ZONE_LEN sizeof(SPIDER_SQL_TIME_ZONE_STR) - 1
90
91#define SPIDER_SQL_COMMIT_STR "commit"
92#define SPIDER_SQL_COMMIT_LEN sizeof(SPIDER_SQL_COMMIT_STR) - 1
93#define SPIDER_SQL_ROLLBACK_STR "rollback"
94#define SPIDER_SQL_ROLLBACK_LEN sizeof(SPIDER_SQL_ROLLBACK_STR) - 1
95
96#define SPIDER_SQL_XA_START_STR "xa start "
97#define SPIDER_SQL_XA_START_LEN sizeof(SPIDER_SQL_XA_START_STR) - 1
98#define SPIDER_SQL_XA_END_STR "xa end "
99#define SPIDER_SQL_XA_END_LEN sizeof(SPIDER_SQL_XA_END_STR) - 1
100#define SPIDER_SQL_XA_PREPARE_STR "xa prepare "
101#define SPIDER_SQL_XA_PREPARE_LEN sizeof(SPIDER_SQL_XA_PREPARE_STR) - 1
102#define SPIDER_SQL_XA_COMMIT_STR "xa commit "
103#define SPIDER_SQL_XA_COMMIT_LEN sizeof(SPIDER_SQL_XA_COMMIT_STR) - 1
104#define SPIDER_SQL_XA_ROLLBACK_STR "xa rollback "
105#define SPIDER_SQL_XA_ROLLBACK_LEN sizeof(SPIDER_SQL_XA_ROLLBACK_STR) - 1
106
107#define SPIDER_SQL_LOCK_TABLE_STR "lock tables "
108#define SPIDER_SQL_LOCK_TABLE_LEN (sizeof(SPIDER_SQL_LOCK_TABLE_STR) - 1)
109#define SPIDER_SQL_UNLOCK_TABLE_STR "unlock tables"
110#define SPIDER_SQL_UNLOCK_TABLE_LEN (sizeof(SPIDER_SQL_UNLOCK_TABLE_STR) - 1)
111
112#define SPIDER_SQL_SHOW_TABLE_STATUS_STR "show table status from "
113#define SPIDER_SQL_SHOW_TABLE_STATUS_LEN sizeof(SPIDER_SQL_SHOW_TABLE_STATUS_STR) - 1
114#define SPIDER_SQL_SELECT_TABLES_STATUS_STR "select `table_rows`,`avg_row_length`,`data_length`,`max_data_length`,`index_length`,`auto_increment`,`create_time`,`update_time`,`check_time` from `information_schema`.`tables` where `table_schema` = "
115#define SPIDER_SQL_SELECT_TABLES_STATUS_LEN sizeof(SPIDER_SQL_SELECT_TABLES_STATUS_STR) - 1
116#define SPIDER_SQL_SHOW_WARNINGS_STR "show warnings"
117#define SPIDER_SQL_SHOW_WARNINGS_LEN sizeof(SPIDER_SQL_SHOW_WARNINGS_STR) - 1
118
119#define SPIDER_SQL_SHOW_MASTER_STATUS_STR "show master status"
120#define SPIDER_SQL_SHOW_MASTER_STATUS_LEN sizeof(SPIDER_SQL_SHOW_MASTER_STATUS_STR) - 1
121#define SPIDER_SQL_BINLOG_GTID_POS_STR "select binlog_gtid_pos"
122#define SPIDER_SQL_BINLOG_GTID_POS_LEN sizeof(SPIDER_SQL_BINLOG_GTID_POS_STR) - 1
123
124#ifdef SPIDER_HAS_DISCOVER_TABLE_STRUCTURE
125#define SPIDER_SQL_SHOW_COLUMNS_STR "show columns from "
126#define SPIDER_SQL_SHOW_COLUMNS_LEN sizeof(SPIDER_SQL_SHOW_COLUMNS_STR) - 1
127#define SPIDER_SQL_SELECT_COLUMNS_STR "select `column_name`,`column_default`,`is_nullable`,`character_set_name`,`collation_name`,`column_type`,`extra` from `information_schema`.`columns` where `table_schema` = "
128#define SPIDER_SQL_SELECT_COLUMNS_LEN sizeof(SPIDER_SQL_SELECT_COLUMNS_STR) - 1
129
130#define SPIDER_SQL_AUTO_INCREMENT_STR " auto_increment"
131#define SPIDER_SQL_AUTO_INCREMENT_LEN sizeof(SPIDER_SQL_AUTO_INCREMENT_STR) - 1
132#define SPIDER_SQL_ORDINAL_POSITION_STR "ordinal_position"
133#define SPIDER_SQL_ORDINAL_POSITION_LEN sizeof(SPIDER_SQL_ORDINAL_POSITION_STR) - 1
134#define SPIDER_SQL_FULLTEXT_STR "fulltext"
135#define SPIDER_SQL_FULLTEXT_LEN sizeof(SPIDER_SQL_FULLTEXT_STR) - 1
136#define SPIDER_SQL_SPATIAL_STR "spatial"
137#define SPIDER_SQL_SPATIAL_LEN sizeof(SPIDER_SQL_SPATIAL_STR) - 1
138#define SPIDER_SQL_USING_HASH_STR " using hash"
139#define SPIDER_SQL_USING_HASH_LEN sizeof(SPIDER_SQL_USING_HASH_STR) - 1
140#endif
141
142#define SPIDER_SQL_LIKE_STR " like "
143#define SPIDER_SQL_LIKE_LEN (sizeof(SPIDER_SQL_LIKE_STR) - 1)
144#define SPIDER_SQL_LIMIT1_STR " limit 1"
145#define SPIDER_SQL_LIMIT1_LEN (sizeof(SPIDER_SQL_LIMIT1_STR) - 1)
146#define SPIDER_SQL_COLLATE_STR " collate "
147#define SPIDER_SQL_COLLATE_LEN (sizeof(SPIDER_SQL_COLLATE_STR) - 1)
148
149#define SPIDER_SQL_INTERVAL_STR " + interval "
150#define SPIDER_SQL_INTERVAL_LEN (sizeof(SPIDER_SQL_INTERVAL_STR) - 1)
151#define SPIDER_SQL_NEGINTERVAL_STR " - interval "
152#define SPIDER_SQL_NEGINTERVAL_LEN (sizeof(SPIDER_SQL_NEGINTERVAL_STR) - 1)
153
154static uchar SPIDER_SQL_LINESTRING_HEAD_STR[] =
155 {0x00,0x00,0x00,0x00,0x01,0x02,0x00,0x00,0x00,0x02,0x00,0x00,0x00};
156#define SPIDER_SQL_LINESTRING_HEAD_LEN sizeof(SPIDER_SQL_LINESTRING_HEAD_STR)
157
158static const char *spider_db_table_lock_str[] =
159{
160 " read local,",
161 " read,",
162 " low_priority write,",
163 " write,"
164};
165static const int spider_db_table_lock_len[] =
166{
167 sizeof(" read local,") - 1,
168 sizeof(" read,") - 1,
169 sizeof(" low_priority write,") - 1,
170 sizeof(" write,") - 1
171};
172static const char *spider_db_timefunc_interval_str[] =
173{
174 " year", " quarter", " month", " week", " day",
175 " hour", " minute", " second", " microsecond",
176 " year_month", " day_hour", " day_minute",
177 " day_second", " hour_minute", " hour_second",
178 " minute_second", " day_microsecond", " hour_microsecond",
179 " minute_microsecond", " second_microsecond"
180};
181
182int spider_mysql_init()
183{
184 DBUG_ENTER("spider_mysql_init");
185 DBUG_RETURN(0);
186}
187
188int spider_mysql_deinit()
189{
190 DBUG_ENTER("spider_mysql_deinit");
191 DBUG_RETURN(0);
192}
193
194spider_db_share *spider_mysql_create_share(
195 SPIDER_SHARE *share
196) {
197 DBUG_ENTER("spider_mysql_create_share");
198 DBUG_RETURN(new spider_mysql_share(share));
199}
200
201spider_db_handler *spider_mysql_create_handler(
202 ha_spider *spider,
203 spider_db_share *db_share
204) {
205 DBUG_ENTER("spider_mysql_create_handler");
206 DBUG_RETURN(new spider_mysql_handler(spider,
207 (spider_mysql_share *) db_share));
208}
209
210spider_db_copy_table *spider_mysql_create_copy_table(
211 spider_db_share *db_share
212) {
213 DBUG_ENTER("spider_mysql_create_copy_table");
214 DBUG_RETURN(new spider_mysql_copy_table(
215 (spider_mysql_share *) db_share));
216}
217
218SPIDER_DB_CONN *spider_mysql_create_conn(
219 SPIDER_CONN *conn
220) {
221 DBUG_ENTER("spider_mysql_create_conn");
222 DBUG_RETURN(new spider_db_mysql(conn));
223}
224
225bool spider_mysql_support_direct_join(
226) {
227 DBUG_ENTER("spider_mysql_support_direct_join");
228 DBUG_RETURN(TRUE);
229}
230
231spider_db_mysql_util spider_db_mysql_utility;
232
233SPIDER_DBTON spider_dbton_mysql = {
234 0,
235 SPIDER_DB_WRAPPER_MYSQL,
236 SPIDER_DB_ACCESS_TYPE_SQL,
237 spider_mysql_init,
238 spider_mysql_deinit,
239 spider_mysql_create_share,
240 spider_mysql_create_handler,
241 spider_mysql_create_copy_table,
242 spider_mysql_create_conn,
243 spider_mysql_support_direct_join,
244 &spider_db_mysql_utility
245};
246
247spider_db_mysql_row::spider_db_mysql_row() :
248 spider_db_row(spider_dbton_mysql.dbton_id),
249 row(NULL), lengths(NULL), cloned(FALSE)
250{
251 DBUG_ENTER("spider_db_mysql_row::spider_db_mysql_row");
252 DBUG_PRINT("info",("spider this=%p", this));
253 DBUG_VOID_RETURN;
254}
255
256spider_db_mysql_row::~spider_db_mysql_row()
257{
258 DBUG_ENTER("spider_db_mysql_row::~spider_db_mysql_row");
259 DBUG_PRINT("info",("spider this=%p", this));
260 if (cloned)
261 {
262 spider_free(spider_current_trx, row_first, MYF(0));
263 }
264 DBUG_VOID_RETURN;
265}
266
267int spider_db_mysql_row::store_to_field(
268 Field *field,
269 CHARSET_INFO *access_charset
270) {
271 DBUG_ENTER("spider_db_mysql_row::store_to_field");
272 DBUG_PRINT("info",("spider this=%p", this));
273 if (!*row)
274 {
275 DBUG_PRINT("info", ("spider field is null"));
276 field->set_null();
277 field->reset();
278 } else {
279 field->set_notnull();
280 if (field->flags & BLOB_FLAG)
281 {
282 DBUG_PRINT("info", ("spider blob field"));
283 if (
284 field->charset() == &my_charset_bin ||
285 field->charset()->cset == access_charset->cset
286 )
287 ((Field_blob *)field)->set_ptr(*lengths, (uchar *) *row);
288 else {
289 DBUG_PRINT("info", ("spider blob convert"));
290 if (field->table->file->ht == spider_hton_ptr)
291 {
292 ha_spider *spider = (ha_spider *) field->table->file;
293 spider_string *str = &spider->blob_buff[field->field_index];
294 str->length(0);
295 if (str->append(*row, *lengths, access_charset))
296 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
297 ((Field_blob *)field)->set_ptr(str->length(), (uchar *) str->ptr());
298 } else {
299 field->store(*row, *lengths, access_charset);
300 }
301 }
302 } else
303 field->store(*row, *lengths, access_charset);
304 }
305 DBUG_RETURN(0);
306}
307
308int spider_db_mysql_row::append_to_str(
309 spider_string *str
310) {
311 DBUG_ENTER("spider_db_mysql_row::append_to_str");
312 DBUG_PRINT("info",("spider this=%p", this));
313 if (str->reserve(*lengths))
314 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
315 str->q_append(*row, *lengths);
316 DBUG_RETURN(0);
317}
318
319int spider_db_mysql_row::append_escaped_to_str(
320 spider_string *str,
321 uint dbton_id
322) {
323 DBUG_ENTER("spider_db_mysql_row::append_escaped_to_str");
324 DBUG_PRINT("info",("spider this=%p", this));
325 spider_string tmp_str(*row, *lengths + 1, str->charset());
326 tmp_str.init_calc_mem(133);
327 tmp_str.length(*lengths);
328 if (str->reserve(*lengths * 2 + 2))
329 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
330 spider_dbton[dbton_id].db_util->append_escaped_util(str, tmp_str.get_str());
331 DBUG_RETURN(0);
332}
333
334void spider_db_mysql_row::first()
335{
336 DBUG_ENTER("spider_db_mysql_row::first");
337 DBUG_PRINT("info",("spider this=%p", this));
338 row = row_first;
339 lengths = lengths_first;
340 DBUG_VOID_RETURN;
341}
342
343void spider_db_mysql_row::next()
344{
345 DBUG_ENTER("spider_db_mysql_row::next");
346 DBUG_PRINT("info",("spider this=%p", this));
347 row++;
348 lengths++;
349 DBUG_VOID_RETURN;
350}
351
352bool spider_db_mysql_row::is_null()
353{
354 DBUG_ENTER("spider_db_mysql_row::is_null");
355 DBUG_PRINT("info",("spider this=%p", this));
356 DBUG_RETURN(!(*row));
357}
358
359int spider_db_mysql_row::val_int()
360{
361 DBUG_ENTER("spider_db_mysql_row::val_int");
362 DBUG_PRINT("info",("spider this=%p", this));
363 DBUG_RETURN(*row ? atoi(*row) : 0);
364}
365
366double spider_db_mysql_row::val_real()
367{
368 DBUG_ENTER("spider_db_mysql_row::val_real");
369 DBUG_PRINT("info",("spider this=%p", this));
370 DBUG_RETURN(*row ? my_atof(*row) : 0.0);
371}
372
373my_decimal *spider_db_mysql_row::val_decimal(
374 my_decimal *decimal_value,
375 CHARSET_INFO *access_charset
376) {
377 DBUG_ENTER("spider_db_mysql_row::val_decimal");
378 DBUG_PRINT("info",("spider this=%p", this));
379 if (!*row)
380 DBUG_RETURN(NULL);
381
382#ifdef SPIDER_HAS_DECIMAL_OPERATION_RESULTS_VALUE_TYPE
383 decimal_operation_results(str2my_decimal(0, *row, *lengths, access_charset,
384 decimal_value), "", "");
385#else
386 decimal_operation_results(str2my_decimal(0, *row, *lengths, access_charset,
387 decimal_value));
388#endif
389
390 DBUG_RETURN(decimal_value);
391}
392
393SPIDER_DB_ROW *spider_db_mysql_row::clone()
394{
395 spider_db_mysql_row *clone_row;
396 char *tmp_char;
397 MYSQL_ROW tmp_row = row_first, ctmp_row;
398 ulong *tmp_lengths = lengths_first;
399 uint row_size, i;
400 DBUG_ENTER("spider_db_mysql_row::clone");
401 DBUG_PRINT("info",("spider this=%p", this));
402 if (!(clone_row = new spider_db_mysql_row()))
403 {
404 DBUG_RETURN(NULL);
405 }
406 row_size = field_count;
407 for (i = 0; i < field_count; i++)
408 {
409 row_size += *tmp_lengths;
410 tmp_lengths++;
411 }
412 if (!spider_bulk_malloc(spider_current_trx, 29, MYF(MY_WME),
413 &clone_row->row, sizeof(char*) * field_count,
414 &tmp_char, row_size,
415 &clone_row->lengths, sizeof(ulong) * field_count,
416 NullS)
417 ) {
418 delete clone_row;
419 DBUG_RETURN(NULL);
420 }
421 memcpy(clone_row->lengths, lengths_first, sizeof(ulong) * field_count);
422 tmp_lengths = lengths_first;
423 ctmp_row = clone_row->row;
424 for (i = 0; i < field_count; i++)
425 {
426 DBUG_PRINT("info",("spider *lengths=%lu", *tmp_lengths));
427 if (*tmp_row == NULL)
428 {
429 *ctmp_row = NULL;
430 *tmp_char = 0;
431 tmp_char++;
432 } else {
433 *ctmp_row = tmp_char;
434 memcpy(tmp_char, *tmp_row, *tmp_lengths + 1);
435 tmp_char += *tmp_lengths + 1;
436 }
437 ctmp_row++;
438 tmp_lengths++;
439 tmp_row++;
440 }
441 clone_row->field_count = field_count;
442 clone_row->row_first = clone_row->row;
443 clone_row->lengths_first = clone_row->lengths;
444 clone_row->cloned = TRUE;
445 DBUG_RETURN((SPIDER_DB_ROW *) clone_row);
446}
447
448int spider_db_mysql_row::store_to_tmp_table(
449 TABLE *tmp_table,
450 spider_string *str
451) {
452 uint i;
453 MYSQL_ROW tmp_row = row;
454 ulong *tmp_lengths = lengths;
455 DBUG_ENTER("spider_db_mysql_row::store_to_tmp_table");
456 DBUG_PRINT("info",("spider this=%p", this));
457 str->length(0);
458 for (i = 0; i < field_count; i++)
459 {
460 if (*tmp_row)
461 {
462 if (str->reserve(*tmp_lengths + 1))
463 {
464 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
465 }
466 str->q_append(*tmp_row, *tmp_lengths + 1);
467 }
468 tmp_lengths++;
469 tmp_row++;
470 }
471 tmp_table->field[0]->set_notnull();
472 tmp_table->field[0]->store(
473 (const char *) lengths,
474 sizeof(ulong) * field_count, &my_charset_bin);
475 tmp_table->field[1]->set_notnull();
476 tmp_table->field[1]->store(
477 str->ptr(), str->length(), &my_charset_bin);
478 tmp_table->field[2]->set_notnull();
479 tmp_table->field[2]->store(
480 (char *) row, (uint) (sizeof(char *) * field_count), &my_charset_bin);
481 DBUG_RETURN(tmp_table->file->ha_write_row(tmp_table->record[0]));
482}
483
484spider_db_mysql_result::spider_db_mysql_result(SPIDER_DB_CONN *in_db_conn) :
485 spider_db_result(in_db_conn, spider_dbton_mysql.dbton_id),
486 db_result(NULL)
487{
488 DBUG_ENTER("spider_db_mysql_result::spider_db_mysql_result");
489 DBUG_PRINT("info",("spider this=%p", this));
490 DBUG_VOID_RETURN;
491}
492
493spider_db_mysql_result::~spider_db_mysql_result()
494{
495 DBUG_ENTER("spider_db_mysql_result::~spider_db_mysql_result");
496 DBUG_PRINT("info",("spider this=%p", this));
497 if (db_result)
498 {
499 free_result();
500 }
501 DBUG_VOID_RETURN;
502}
503
504bool spider_db_mysql_result::has_result()
505{
506 DBUG_ENTER("spider_db_mysql_result::has_result");
507 DBUG_PRINT("info",("spider this=%p", this));
508 DBUG_RETURN(db_result);
509}
510
511void spider_db_mysql_result::free_result()
512{
513 DBUG_ENTER("spider_db_mysql_result::free_result");
514 DBUG_PRINT("info",("spider this=%p", this));
515 /* need 2 times execution design */
516 if (db_result)
517 {
518 mysql_free_result(db_result);
519 db_result = NULL;
520 }
521 DBUG_VOID_RETURN;
522}
523
524SPIDER_DB_ROW *spider_db_mysql_result::current_row()
525{
526 DBUG_ENTER("spider_db_mysql_result::current_row");
527 DBUG_PRINT("info",("spider this=%p", this));
528 DBUG_RETURN((SPIDER_DB_ROW *) row.clone());
529}
530
531SPIDER_DB_ROW *spider_db_mysql_result::fetch_row()
532{
533 DBUG_ENTER("spider_db_mysql_result::fetch_row");
534 DBUG_PRINT("info",("spider this=%p", this));
535 if (!(row.row = mysql_fetch_row(db_result)))
536 {
537 if (mysql_errno(((spider_db_mysql *) db_conn)->db_conn))
538 {
539 store_error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn);
540 my_message(store_error_num,
541 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
542 } else
543 store_error_num = HA_ERR_END_OF_FILE;
544 DBUG_RETURN(NULL);
545 }
546 row.lengths = mysql_fetch_lengths(db_result);
547 row.field_count = mysql_num_fields(db_result);
548 row.row_first = row.row;
549 row.lengths_first = row.lengths;
550 DBUG_RETURN((SPIDER_DB_ROW *) &row);
551}
552
553SPIDER_DB_ROW *spider_db_mysql_result::fetch_row_from_result_buffer(
554 spider_db_result_buffer *spider_res_buf
555) {
556 DBUG_ENTER("spider_db_mysql_result::fetch_row_from_result_buffer");
557 DBUG_PRINT("info",("spider this=%p", this));
558 if (!(row.row = mysql_fetch_row(db_result)))
559 {
560 if (mysql_errno(((spider_db_mysql *) db_conn)->db_conn))
561 {
562 store_error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn);
563 my_message(store_error_num,
564 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
565 } else
566 store_error_num = HA_ERR_END_OF_FILE;
567 DBUG_RETURN(NULL);
568 }
569 row.lengths = mysql_fetch_lengths(db_result);
570 row.field_count = mysql_num_fields(db_result);
571 row.row_first = row.row;
572 row.lengths_first = row.lengths;
573 DBUG_RETURN((SPIDER_DB_ROW *) &row);
574}
575
576SPIDER_DB_ROW *spider_db_mysql_result::fetch_row_from_tmp_table(
577 TABLE *tmp_table
578) {
579 uint i;
580 spider_string tmp_str1, tmp_str2, tmp_str3;
581 const char *row_ptr;
582 MYSQL_ROW tmp_row;
583 ulong *tmp_lengths;
584 uint field_count;
585 DBUG_ENTER("spider_db_mysql_result::fetch_row_from_tmp_table");
586 DBUG_PRINT("info",("spider this=%p", this));
587 tmp_str1.init_calc_mem(117);
588 tmp_str2.init_calc_mem(118);
589 tmp_str3.init_calc_mem(170);
590 tmp_table->field[0]->val_str(tmp_str1.get_str());
591 tmp_table->field[1]->val_str(tmp_str2.get_str());
592 tmp_table->field[2]->val_str(tmp_str3.get_str());
593 tmp_str1.mem_calc();
594 tmp_str2.mem_calc();
595 tmp_str3.mem_calc();
596 row_ptr = tmp_str2.ptr();
597 tmp_lengths = (ulong *) tmp_str1.ptr();
598 tmp_row = (MYSQL_ROW) tmp_str3.ptr();
599 field_count = tmp_str1.length() / sizeof(ulong);
600 row.row = tmp_row;
601 row.lengths = tmp_lengths;
602 row.field_count = field_count;
603 row.row_first = row.row;
604 row.lengths_first = row.lengths;
605 for (i = 0; i < field_count; i++)
606 {
607 if (*tmp_row)
608 {
609 *tmp_row = (char *) row_ptr;
610 row_ptr += *tmp_lengths + 1;
611 }
612 tmp_row++;
613 tmp_lengths++;
614 }
615 DBUG_RETURN((SPIDER_DB_ROW *) &row);
616}
617
618int spider_db_mysql_result::fetch_table_status(
619 int mode,
620 ha_rows &records,
621 ulong &mean_rec_length,
622 ulonglong &data_file_length,
623 ulonglong &max_data_file_length,
624 ulonglong &index_file_length,
625 ulonglong &auto_increment_value,
626 time_t &create_time,
627 time_t &update_time,
628 time_t &check_time
629) {
630 int error_num;
631 MYSQL_ROW mysql_row;
632 MYSQL_TIME mysql_time;
633#ifdef MARIADB_BASE_VERSION
634 uint not_used_uint;
635#else
636 my_bool not_used_my_bool;
637#endif
638#ifdef SPIDER_HAS_TIME_STATUS
639 MYSQL_TIME_STATUS time_status;
640#else
641 int time_status;
642#endif
643 long not_used_long;
644 DBUG_ENTER("spider_db_mysql_result::fetch_table_status");
645 DBUG_PRINT("info",("spider this=%p", this));
646 if (!(mysql_row = mysql_fetch_row(db_result)))
647 {
648 DBUG_PRINT("info",("spider fetch row is null"));
649 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
650 {
651 my_message(error_num,
652 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
653 DBUG_RETURN(error_num);
654 }
655 DBUG_RETURN(ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM);
656 }
657 if (mode == 1)
658 {
659 /* Ok to test for 18 fields as all new fields are added last */
660 if (num_fields() < 18)
661 {
662 DBUG_PRINT("info",("spider field_count < 18"));
663 DBUG_RETURN(ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM);
664 }
665
666 if (mysql_row[4])
667 records =
668 (ha_rows) my_strtoll10(mysql_row[4], (char**) NULL, &error_num);
669 else
670 records = (ha_rows) 0;
671 DBUG_PRINT("info",
672 ("spider records=%lld", records));
673 if (mysql_row[5])
674 mean_rec_length =
675 (ulong) my_strtoll10(mysql_row[5], (char**) NULL, &error_num);
676 else
677 mean_rec_length = 0;
678 DBUG_PRINT("info",
679 ("spider mean_rec_length=%lu", mean_rec_length));
680 if (mysql_row[6])
681 data_file_length =
682 (ulonglong) my_strtoll10(mysql_row[6], (char**) NULL, &error_num);
683 else
684 data_file_length = 0;
685 DBUG_PRINT("info",
686 ("spider data_file_length=%lld", data_file_length));
687 if (mysql_row[7])
688 max_data_file_length =
689 (ulonglong) my_strtoll10(mysql_row[7], (char**) NULL, &error_num);
690 else
691 max_data_file_length = 0;
692 DBUG_PRINT("info",
693 ("spider max_data_file_length=%lld", max_data_file_length));
694 if (mysql_row[8])
695 index_file_length =
696 (ulonglong) my_strtoll10(mysql_row[8], (char**) NULL, &error_num);
697 else
698 index_file_length = 0;
699 DBUG_PRINT("info",
700 ("spider index_file_length=%lld", index_file_length));
701 if (mysql_row[10])
702 auto_increment_value =
703 (ulonglong) my_strtoll10(mysql_row[10], (char**) NULL, &error_num);
704 else
705 auto_increment_value = 1;
706 DBUG_PRINT("info",
707 ("spider auto_increment_value=%lld", auto_increment_value));
708 if (mysql_row[11])
709 {
710#ifdef SPIDER_HAS_TIME_STATUS
711 my_time_status_init(&time_status);
712#endif
713 str_to_datetime(mysql_row[11], strlen(mysql_row[11]), &mysql_time, 0,
714 &time_status);
715#ifdef MARIADB_BASE_VERSION
716 create_time = (time_t) my_system_gmt_sec(&mysql_time,
717 &not_used_long, &not_used_uint);
718#else
719 create_time = (time_t) my_system_gmt_sec(&mysql_time,
720 &not_used_long, &not_used_my_bool);
721#endif
722 } else
723 create_time = (time_t) 0;
724#ifndef DBUG_OFF
725 {
726 struct tm *ts, tmp_ts;
727 char buf[80];
728 ts = localtime_r(&create_time, &tmp_ts);
729 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ts);
730 DBUG_PRINT("info",("spider create_time=%s", buf));
731 }
732#endif
733 if (mysql_row[12])
734 {
735#ifdef SPIDER_HAS_TIME_STATUS
736 my_time_status_init(&time_status);
737#endif
738 str_to_datetime(mysql_row[12], strlen(mysql_row[12]), &mysql_time, 0,
739 &time_status);
740#ifdef MARIADB_BASE_VERSION
741 update_time = (time_t) my_system_gmt_sec(&mysql_time,
742 &not_used_long, &not_used_uint);
743#else
744 update_time = (time_t) my_system_gmt_sec(&mysql_time,
745 &not_used_long, &not_used_my_bool);
746#endif
747 } else
748 update_time = (time_t) 0;
749#ifndef DBUG_OFF
750 {
751 struct tm *ts, tmp_ts;
752 char buf[80];
753 ts = localtime_r(&update_time, &tmp_ts);
754 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ts);
755 DBUG_PRINT("info",("spider update_time=%s", buf));
756 }
757#endif
758 if (mysql_row[13])
759 {
760#ifdef SPIDER_HAS_TIME_STATUS
761 my_time_status_init(&time_status);
762#endif
763 str_to_datetime(mysql_row[13], strlen(mysql_row[13]), &mysql_time, 0,
764 &time_status);
765#ifdef MARIADB_BASE_VERSION
766 check_time = (time_t) my_system_gmt_sec(&mysql_time,
767 &not_used_long, &not_used_uint);
768#else
769 check_time = (time_t) my_system_gmt_sec(&mysql_time,
770 &not_used_long, &not_used_my_bool);
771#endif
772 } else
773 check_time = (time_t) 0;
774#ifndef DBUG_OFF
775 {
776 struct tm *ts, tmp_ts;
777 char buf[80];
778 ts = localtime_r(&check_time, &tmp_ts);
779 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ts);
780 DBUG_PRINT("info",("spider check_time=%s", buf));
781 }
782#endif
783 } else {
784 if (mysql_row[0])
785 records =
786 (ha_rows) my_strtoll10(mysql_row[0], (char**) NULL, &error_num);
787 else
788 records = (ha_rows) 0;
789 DBUG_PRINT("info",
790 ("spider records=%lld", records));
791 if (mysql_row[1])
792 mean_rec_length =
793 (ulong) my_strtoll10(mysql_row[1], (char**) NULL, &error_num);
794 else
795 mean_rec_length = 0;
796 DBUG_PRINT("info",
797 ("spider mean_rec_length=%lu", mean_rec_length));
798 if (mysql_row[2])
799 data_file_length =
800 (ulonglong) my_strtoll10(mysql_row[2], (char**) NULL, &error_num);
801 else
802 data_file_length = 0;
803 DBUG_PRINT("info",
804 ("spider data_file_length=%lld", data_file_length));
805 if (mysql_row[3])
806 max_data_file_length =
807 (ulonglong) my_strtoll10(mysql_row[3], (char**) NULL, &error_num);
808 else
809 max_data_file_length = 0;
810 DBUG_PRINT("info",
811 ("spider max_data_file_length=%lld", max_data_file_length));
812 if (mysql_row[4])
813 index_file_length =
814 (ulonglong) my_strtoll10(mysql_row[4], (char**) NULL, &error_num);
815 else
816 index_file_length = 0;
817 DBUG_PRINT("info",
818 ("spider index_file_length=%lld", index_file_length));
819 if (mysql_row[5])
820 auto_increment_value =
821 (ulonglong) my_strtoll10(mysql_row[5], (char**) NULL, &error_num);
822 else
823 auto_increment_value = 1;
824 DBUG_PRINT("info",
825 ("spider auto_increment_value=%lld", auto_increment_value));
826 if (mysql_row[6])
827 {
828#ifdef SPIDER_HAS_TIME_STATUS
829 my_time_status_init(&time_status);
830#endif
831 str_to_datetime(mysql_row[6], strlen(mysql_row[6]), &mysql_time, 0,
832 &time_status);
833#ifdef MARIADB_BASE_VERSION
834 create_time = (time_t) my_system_gmt_sec(&mysql_time,
835 &not_used_long, &not_used_uint);
836#else
837 create_time = (time_t) my_system_gmt_sec(&mysql_time,
838 &not_used_long, &not_used_my_bool);
839#endif
840 } else
841 create_time = (time_t) 0;
842#ifndef DBUG_OFF
843 {
844 struct tm *ts, tmp_ts;
845 char buf[80];
846 ts = localtime_r(&create_time, &tmp_ts);
847 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ts);
848 DBUG_PRINT("info",("spider create_time=%s", buf));
849 }
850#endif
851 if (mysql_row[7])
852 {
853#ifdef SPIDER_HAS_TIME_STATUS
854 my_time_status_init(&time_status);
855#endif
856 str_to_datetime(mysql_row[7], strlen(mysql_row[7]), &mysql_time, 0,
857 &time_status);
858#ifdef MARIADB_BASE_VERSION
859 update_time = (time_t) my_system_gmt_sec(&mysql_time,
860 &not_used_long, &not_used_uint);
861#else
862 update_time = (time_t) my_system_gmt_sec(&mysql_time,
863 &not_used_long, &not_used_my_bool);
864#endif
865 } else
866 update_time = (time_t) 0;
867#ifndef DBUG_OFF
868 {
869 struct tm *ts, tmp_ts;
870 char buf[80];
871 ts = localtime_r(&update_time, &tmp_ts);
872 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ts);
873 DBUG_PRINT("info",("spider update_time=%s", buf));
874 }
875#endif
876 if (mysql_row[8])
877 {
878#ifdef SPIDER_HAS_TIME_STATUS
879 my_time_status_init(&time_status);
880#endif
881 str_to_datetime(mysql_row[8], strlen(mysql_row[8]), &mysql_time, 0,
882 &time_status);
883#ifdef MARIADB_BASE_VERSION
884 check_time = (time_t) my_system_gmt_sec(&mysql_time,
885 &not_used_long, &not_used_uint);
886#else
887 check_time = (time_t) my_system_gmt_sec(&mysql_time,
888 &not_used_long, &not_used_my_bool);
889#endif
890 } else
891 check_time = (time_t) 0;
892#ifndef DBUG_OFF
893 {
894 struct tm *ts, tmp_ts;
895 char buf[80];
896 ts = localtime_r(&check_time, &tmp_ts);
897 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ts);
898 DBUG_PRINT("info",("spider check_time=%s", buf));
899 }
900#endif
901 }
902 DBUG_RETURN(0);
903}
904
905int spider_db_mysql_result::fetch_table_records(
906 int mode,
907 ha_rows &records
908) {
909 int error_num;
910 MYSQL_ROW mysql_row;
911 DBUG_ENTER("spider_db_mysql_result::fetch_table_records");
912 DBUG_PRINT("info",("spider this=%p", this));
913 if (!(mysql_row = mysql_fetch_row(db_result)))
914 {
915 DBUG_PRINT("info",("spider fetch row is null"));
916 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
917 {
918 my_message(error_num,
919 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
920 DBUG_RETURN(error_num);
921 }
922 DBUG_RETURN(ER_QUERY_ON_FOREIGN_DATA_SOURCE);
923 }
924 if (mode == 1)
925 {
926 if (mysql_row[0])
927 {
928 records =
929 (ha_rows) my_strtoll10(mysql_row[0], (char**) NULL, &error_num);
930 } else
931 records = (ha_rows) 0;
932 DBUG_PRINT("info",
933 ("spider records=%lld", records));
934 } else {
935 if (num_fields() != 10)
936 {
937 DBUG_RETURN(ER_QUERY_ON_FOREIGN_DATA_SOURCE);
938 }
939
940 if (mysql_row[8])
941 {
942 records =
943 (ha_rows) my_strtoll10(mysql_row[8], (char**) NULL, &error_num);
944 } else
945 records = 0;
946 }
947 DBUG_RETURN(0);
948}
949
950int spider_db_mysql_result::fetch_table_cardinality(
951 int mode,
952 TABLE *table,
953 longlong *cardinality,
954 uchar *cardinality_upd,
955 int bitmap_size
956) {
957 int error_num;
958 MYSQL_ROW mysql_row;
959 Field *field;
960 DBUG_ENTER("spider_db_mysql_result::fetch_table_cardinality");
961 DBUG_PRINT("info",("spider this=%p", this));
962 memset((uchar *) cardinality_upd, 0, sizeof(uchar) * bitmap_size);
963 if (!(mysql_row = mysql_fetch_row(db_result)))
964 {
965 DBUG_PRINT("info",("spider fetch row is null"));
966 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
967 {
968 my_message(error_num,
969 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
970 DBUG_RETURN(error_num);
971 }
972 /* no index */
973 DBUG_RETURN(0);
974 }
975 if (mode == 1)
976 {
977 uint num_fields = this->num_fields();
978 if (num_fields < 12 || num_fields > 13)
979 {
980 DBUG_PRINT("info",("spider num_fields < 12 || num_fields > 13"));
981 DBUG_RETURN(ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM);
982 }
983
984 while (mysql_row)
985 {
986 if (
987 mysql_row[4] &&
988 mysql_row[6] &&
989 (field = find_field_in_table_sef(table, mysql_row[4]))
990 ) {
991 if ((cardinality[field->field_index] =
992 (longlong) my_strtoll10(mysql_row[6], (char**) NULL, &error_num))
993 <= 0)
994 cardinality[field->field_index] = 1;
995 spider_set_bit(cardinality_upd, field->field_index);
996 DBUG_PRINT("info",
997 ("spider col_name=%s", mysql_row[4]));
998 DBUG_PRINT("info",
999 ("spider cardinality=%lld",
1000 cardinality[field->field_index]));
1001 } else if (mysql_row[4])
1002 {
1003 DBUG_PRINT("info",
1004 ("spider skip col_name=%s", mysql_row[4]));
1005 } else {
1006 DBUG_RETURN(ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM);
1007 }
1008 mysql_row = mysql_fetch_row(db_result);
1009 }
1010 } else {
1011 while (mysql_row)
1012 {
1013 if (
1014 mysql_row[0] &&
1015 mysql_row[1] &&
1016 (field = find_field_in_table_sef(table, mysql_row[0]))
1017 ) {
1018 if ((cardinality[field->field_index] =
1019 (longlong) my_strtoll10(mysql_row[1], (char**) NULL, &error_num))
1020 <= 0)
1021 cardinality[field->field_index] = 1;
1022 spider_set_bit(cardinality_upd, field->field_index);
1023 DBUG_PRINT("info",
1024 ("spider col_name=%s", mysql_row[0]));
1025 DBUG_PRINT("info",
1026 ("spider cardinality=%lld",
1027 cardinality[field->field_index]));
1028 } else if (mysql_row[0])
1029 {
1030 DBUG_PRINT("info",
1031 ("spider skip col_name=%s", mysql_row[0]));
1032 } else {
1033 DBUG_RETURN(ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM);
1034 }
1035 mysql_row = mysql_fetch_row(db_result);
1036 }
1037 }
1038 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
1039 {
1040 my_message(error_num,
1041 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
1042 DBUG_RETURN(error_num);
1043 }
1044 DBUG_RETURN(0);
1045}
1046
1047int spider_db_mysql_result::fetch_table_mon_status(
1048 int &status
1049) {
1050 int error_num;
1051 MYSQL_ROW mysql_row;
1052 DBUG_ENTER("spider_db_mysql_result::fetch_table_mon_status");
1053 DBUG_PRINT("info",("spider this=%p", this));
1054 if (!(mysql_row = mysql_fetch_row(db_result)))
1055 {
1056 DBUG_PRINT("info",("spider fetch row is null"));
1057 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
1058 {
1059 my_message(error_num,
1060 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
1061 DBUG_RETURN(error_num);
1062 }
1063 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1064 }
1065 if (num_fields() != 1)
1066 {
1067 DBUG_PRINT("info",("spider num_fields != 1"));
1068 my_printf_error(ER_SPIDER_UNKNOWN_NUM, ER_SPIDER_UNKNOWN_STR, MYF(0));
1069 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
1070 }
1071 if (mysql_row[0])
1072 status = atoi(mysql_row[0]);
1073 else
1074 status = SPIDER_LINK_MON_OK;
1075 DBUG_PRINT("info", ("spider status=%d", status));
1076 DBUG_RETURN(0);
1077}
1078
1079int spider_db_mysql_result::fetch_show_master_status(
1080 const char **binlog_file_name,
1081 const char **binlog_pos
1082) {
1083 int error_num;
1084 MYSQL_ROW mysql_row;
1085 DBUG_ENTER("spider_db_mysql_result::fetch_show_master_status");
1086 DBUG_PRINT("info",("spider this=%p", this));
1087 if (!(mysql_row = mysql_fetch_row(db_result)))
1088 {
1089 DBUG_PRINT("info",("spider fetch row is null"));
1090 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
1091 {
1092 my_message(error_num,
1093 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
1094 DBUG_RETURN(error_num);
1095 }
1096 DBUG_RETURN(ER_QUERY_ON_FOREIGN_DATA_SOURCE);
1097 }
1098 if (num_fields() != 4)
1099 {
1100 DBUG_RETURN(ER_QUERY_ON_FOREIGN_DATA_SOURCE);
1101 }
1102
1103 *binlog_file_name = mysql_row[0];
1104 DBUG_PRINT("info",("spider binlog_file_name=%s", *binlog_file_name));
1105 *binlog_pos = mysql_row[1];
1106 DBUG_PRINT("info",("spider binlog_pos=%s", *binlog_pos));
1107 DBUG_RETURN(0);
1108}
1109
1110int spider_db_mysql_result::fetch_select_binlog_gtid_pos(
1111 const char **gtid_pos
1112) {
1113 int error_num;
1114 MYSQL_ROW mysql_row;
1115 DBUG_ENTER("spider_db_mysql_result::fetch_select_binlog_gtid_pos");
1116 DBUG_PRINT("info",("spider this=%p", this));
1117 if (!(mysql_row = mysql_fetch_row(db_result)))
1118 {
1119 DBUG_PRINT("info",("spider fetch row is null"));
1120 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
1121 {
1122 my_message(error_num,
1123 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
1124 DBUG_RETURN(error_num);
1125 }
1126 DBUG_RETURN(ER_QUERY_ON_FOREIGN_DATA_SOURCE);
1127 }
1128 if (num_fields() != 1)
1129 {
1130 DBUG_RETURN(ER_QUERY_ON_FOREIGN_DATA_SOURCE);
1131 }
1132
1133 *gtid_pos = mysql_row[0];
1134 DBUG_PRINT("info",("spider gtid_pos=%s", *gtid_pos));
1135 DBUG_RETURN(0);
1136}
1137
1138longlong spider_db_mysql_result::num_rows()
1139{
1140 DBUG_ENTER("spider_db_mysql_result::num_rows");
1141 DBUG_PRINT("info",("spider this=%p", this));
1142 DBUG_RETURN((longlong) mysql_num_rows(db_result));
1143}
1144
1145uint spider_db_mysql_result::num_fields()
1146{
1147 DBUG_ENTER("spider_db_mysql_result::num_fields");
1148 DBUG_PRINT("info",("spider this=%p", this));
1149 DBUG_RETURN(mysql_num_fields(db_result));
1150}
1151
1152void spider_db_mysql_result::move_to_pos(
1153 longlong pos
1154) {
1155 DBUG_ENTER("spider_db_mysql_result::move_to_pos");
1156 DBUG_PRINT("info",("spider this=%p", this));
1157 DBUG_PRINT("info",("spider pos=%lld", pos));
1158/*
1159 DBUG_ASSERT(first_row);
1160*/
1161 db_result->data_cursor = first_row + pos;
1162 DBUG_VOID_RETURN;
1163}
1164
1165int spider_db_mysql_result::get_errno()
1166{
1167 DBUG_ENTER("spider_db_mysql_result::get_errno");
1168 DBUG_PRINT("info",("spider this=%p", this));
1169 DBUG_PRINT("info",("spider store_error_num=%d", store_error_num));
1170 DBUG_RETURN(store_error_num);
1171}
1172
1173#ifdef SPIDER_HAS_DISCOVER_TABLE_STRUCTURE
1174int spider_db_mysql_result::fetch_columns_for_discover_table_structure(
1175 spider_string *str,
1176 CHARSET_INFO *access_charset
1177) {
1178 int error_num;
1179 uint length;
1180 MYSQL_ROW mysql_row;
1181 DBUG_ENTER("spider_db_mysql_result::fetch_columns_for_discover_table_structure");
1182 DBUG_PRINT("info",("spider this=%p", this));
1183 if (!(mysql_row = mysql_fetch_row(db_result)))
1184 {
1185 DBUG_PRINT("info",("spider fetch row is null"));
1186 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
1187 {
1188 my_message(error_num,
1189 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
1190 DBUG_RETURN(error_num);
1191 }
1192 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1193 }
1194 if (num_fields() != 7)
1195 {
1196 DBUG_PRINT("info",("spider num_fields != 7"));
1197 my_printf_error(ER_SPIDER_UNKNOWN_NUM, ER_SPIDER_UNKNOWN_STR, MYF(0));
1198 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
1199 }
1200 do {
1201 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN))
1202 {
1203 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1204 }
1205 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
1206 if (str->append(mysql_row[0], strlen(mysql_row[0]), access_charset))
1207 {
1208 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1209 }
1210 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN + SPIDER_SQL_SPACE_LEN))
1211 {
1212 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1213 }
1214 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
1215 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
1216 if (str->append(mysql_row[5], strlen(mysql_row[5]), access_charset))
1217 {
1218 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1219 }
1220 if (mysql_row[3])
1221 {
1222 length = strlen(mysql_row[3]);
1223 if (str->reserve(SPIDER_SQL_CHARACTER_SET_LEN + length))
1224 {
1225 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1226 }
1227 str->q_append(SPIDER_SQL_CHARACTER_SET_STR, SPIDER_SQL_CHARACTER_SET_LEN);
1228 str->q_append(mysql_row[3], length);
1229 }
1230 if (mysql_row[4])
1231 {
1232 length = strlen(mysql_row[4]);
1233 if (str->reserve(SPIDER_SQL_COLLATE_LEN + length))
1234 {
1235 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1236 }
1237 str->q_append(SPIDER_SQL_COLLATE_STR, SPIDER_SQL_COLLATE_LEN);
1238 str->q_append(mysql_row[4], length);
1239 }
1240 if (!strcmp(mysql_row[2], "NO"))
1241 {
1242 if (str->reserve(SPIDER_SQL_NOT_NULL_LEN))
1243 {
1244 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1245 }
1246 str->q_append(SPIDER_SQL_NOT_NULL_STR, SPIDER_SQL_NOT_NULL_LEN);
1247 if (mysql_row[1])
1248 {
1249 if (str->reserve(SPIDER_SQL_DEFAULT_LEN))
1250 {
1251 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1252 }
1253 str->q_append(SPIDER_SQL_DEFAULT_STR, SPIDER_SQL_DEFAULT_LEN);
1254 if (str->append(mysql_row[1], strlen(mysql_row[1]), access_charset))
1255 {
1256 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1257 }
1258 }
1259 } else {
1260 if (str->reserve(SPIDER_SQL_DEFAULT_LEN))
1261 {
1262 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1263 }
1264 str->q_append(SPIDER_SQL_DEFAULT_STR, SPIDER_SQL_DEFAULT_LEN);
1265 if (mysql_row[1])
1266 {
1267 if (str->append(mysql_row[1], strlen(mysql_row[1]), access_charset))
1268 {
1269 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1270 }
1271 } else {
1272 if (str->reserve(SPIDER_SQL_NULL_LEN))
1273 {
1274 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1275 }
1276 str->q_append(SPIDER_SQL_NULL_STR, SPIDER_SQL_NULL_LEN);
1277 }
1278 }
1279 if (mysql_row[6] && !strcmp(mysql_row[6], "auto_increment"))
1280 {
1281 if (str->reserve(SPIDER_SQL_AUTO_INCREMENT_LEN))
1282 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1283 str->q_append(SPIDER_SQL_AUTO_INCREMENT_STR, SPIDER_SQL_AUTO_INCREMENT_LEN);
1284 }
1285 if (str->reserve(SPIDER_SQL_COMMA_LEN))
1286 {
1287 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1288 }
1289 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
1290 } while ((mysql_row = mysql_fetch_row(db_result)));
1291 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
1292 {
1293 my_message(error_num,
1294 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
1295 DBUG_RETURN(error_num);
1296 }
1297 DBUG_RETURN(0);
1298}
1299
1300int spider_db_mysql_result::fetch_index_for_discover_table_structure(
1301 spider_string *str,
1302 CHARSET_INFO *access_charset
1303) {
1304 int error_num;
1305 MYSQL_ROW mysql_row;
1306 DBUG_ENTER("spider_db_mysql_result::fetch_index_for_discover_table_structure");
1307 DBUG_PRINT("info",("spider this=%p", this));
1308 if (!(mysql_row = mysql_fetch_row(db_result)))
1309 {
1310 DBUG_PRINT("info",("spider fetch row is null"));
1311 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
1312 {
1313 my_message(error_num,
1314 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
1315 DBUG_RETURN(error_num);
1316 }
1317 DBUG_RETURN(0);
1318 }
1319 if (num_fields() != 13)
1320 {
1321 DBUG_PRINT("info",("spider num_fields != 13"));
1322 my_printf_error(ER_SPIDER_UNKNOWN_NUM, ER_SPIDER_UNKNOWN_STR, MYF(0));
1323 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
1324 }
1325 bool first = TRUE;
1326 bool without_size = FALSE;
1327 bool using_hash = FALSE;
1328 do {
1329 if (!strcmp(mysql_row[3], "1"))
1330 {
1331 without_size = FALSE;
1332 if (first)
1333 {
1334 first = FALSE;
1335 } else {
1336 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN + SPIDER_SQL_COMMA_LEN +
1337 (using_hash ? SPIDER_SQL_USING_HASH_LEN : 0)))
1338 {
1339 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1340 }
1341 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
1342 if (using_hash)
1343 str->q_append(SPIDER_SQL_USING_HASH_STR, SPIDER_SQL_USING_HASH_LEN);
1344 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
1345 }
1346 /* new index */
1347 if (!strcmp(mysql_row[2], SPIDER_DB_PK_NAME_STR))
1348 {
1349 /* primary key */
1350 if (str->reserve(SPIDER_DB_PK_NAME_LEN + SPIDER_SQL_SPACE_LEN))
1351 {
1352 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1353 }
1354 str->q_append(SPIDER_DB_PK_NAME_STR, SPIDER_DB_PK_NAME_LEN);
1355 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
1356 } else if (!strcmp(mysql_row[1], "0"))
1357 {
1358 /* unique key */
1359 if (str->reserve(SPIDER_DB_UNIQUE_NAME_LEN + SPIDER_SQL_SPACE_LEN))
1360 {
1361 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1362 }
1363 str->q_append(SPIDER_DB_UNIQUE_NAME_STR, SPIDER_DB_UNIQUE_NAME_LEN);
1364 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
1365 } else if (mysql_row[10] && !strcmp(mysql_row[10], "FULLTEXT"))
1366 {
1367 /* fulltext key */
1368 if (str->reserve(SPIDER_SQL_FULLTEXT_LEN + SPIDER_SQL_SPACE_LEN))
1369 {
1370 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1371 }
1372 str->q_append(SPIDER_SQL_FULLTEXT_STR, SPIDER_SQL_FULLTEXT_LEN);
1373 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
1374 } else if (mysql_row[10] && !strcmp(mysql_row[10], "SPATIAL"))
1375 {
1376 /* spatial key */
1377 without_size = TRUE;
1378 if (str->reserve(SPIDER_SQL_SPATIAL_LEN + SPIDER_SQL_SPACE_LEN))
1379 {
1380 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1381 }
1382 str->q_append(SPIDER_SQL_SPATIAL_STR, SPIDER_SQL_SPATIAL_LEN);
1383 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
1384 }
1385 if (str->reserve(SPIDER_DB_KEY_NAME_LEN + SPIDER_SQL_SPACE_LEN))
1386 {
1387 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1388 }
1389 str->q_append(SPIDER_DB_KEY_NAME_STR, SPIDER_DB_KEY_NAME_LEN);
1390 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
1391 if (strcmp(mysql_row[2], SPIDER_DB_PK_NAME_STR))
1392 {
1393 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN))
1394 {
1395 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1396 }
1397 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
1398 if (str->append(mysql_row[2], strlen(mysql_row[2]), access_charset))
1399 {
1400 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1401 }
1402 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN))
1403 {
1404 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1405 }
1406 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
1407 }
1408 if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
1409 {
1410 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1411 }
1412 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
1413 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN))
1414 {
1415 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1416 }
1417 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
1418 if (str->append(mysql_row[4], strlen(mysql_row[4]), access_charset))
1419 {
1420 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1421 }
1422 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN))
1423 {
1424 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1425 }
1426 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
1427 if (mysql_row[7] && !without_size)
1428 {
1429 if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
1430 {
1431 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1432 }
1433 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
1434 if (str->append(mysql_row[7], strlen(mysql_row[7]), access_charset))
1435 {
1436 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1437 }
1438 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN))
1439 {
1440 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1441 }
1442 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
1443 }
1444 } else {
1445 if (str->reserve(SPIDER_SQL_COMMA_LEN + SPIDER_SQL_NAME_QUOTE_LEN))
1446 {
1447 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1448 }
1449 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
1450 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
1451 if (str->append(mysql_row[4], strlen(mysql_row[4]), access_charset))
1452 {
1453 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1454 }
1455 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN))
1456 {
1457 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1458 }
1459 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
1460 if (mysql_row[7] && !without_size)
1461 {
1462 if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
1463 {
1464 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1465 }
1466 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
1467 if (str->append(mysql_row[7], strlen(mysql_row[7]), access_charset))
1468 {
1469 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1470 }
1471 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN))
1472 {
1473 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1474 }
1475 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
1476 }
1477 }
1478 if (mysql_row[10] && !strcmp(mysql_row[10], "HASH"))
1479 using_hash = TRUE;
1480 else
1481 using_hash = FALSE;
1482 } while ((mysql_row = mysql_fetch_row(db_result)));
1483 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
1484 {
1485 my_message(error_num,
1486 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
1487 DBUG_RETURN(error_num);
1488 }
1489 if (!first)
1490 {
1491 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN + SPIDER_SQL_COMMA_LEN +
1492 (using_hash ? SPIDER_SQL_USING_HASH_LEN : 0)))
1493 {
1494 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1495 }
1496 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
1497 if (using_hash)
1498 str->q_append(SPIDER_SQL_USING_HASH_STR, SPIDER_SQL_USING_HASH_LEN);
1499 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
1500 }
1501 DBUG_RETURN(0);
1502}
1503
1504int spider_db_mysql_result::fetch_table_for_discover_table_structure(
1505 spider_string *str,
1506 SPIDER_SHARE *spider_share,
1507 CHARSET_INFO *access_charset
1508) {
1509 int error_num;
1510 MYSQL_ROW mysql_row;
1511 DBUG_ENTER("spider_db_mysql_result::fetch_table_for_discover_table_structure");
1512 DBUG_PRINT("info",("spider this=%p", this));
1513 if (!(mysql_row = mysql_fetch_row(db_result)))
1514 {
1515 DBUG_PRINT("info",("spider fetch row is null"));
1516 if ((error_num = mysql_errno(((spider_db_mysql *) db_conn)->db_conn)))
1517 {
1518 my_message(error_num,
1519 mysql_error(((spider_db_mysql *) db_conn)->db_conn), MYF(0));
1520 DBUG_RETURN(error_num);
1521 }
1522 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1523 }
1524 if (num_fields() != 18)
1525 {
1526 DBUG_PRINT("info",("spider num_fields != 18"));
1527 my_printf_error(ER_SPIDER_UNKNOWN_NUM, ER_SPIDER_UNKNOWN_STR, MYF(0));
1528 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
1529 }
1530 if (!mysql_row[14])
1531 {
1532 DBUG_PRINT("info",("spider mysql_row[14] is null"));
1533 my_printf_error(ER_SPIDER_UNKNOWN_NUM, ER_SPIDER_UNKNOWN_STR, MYF(0));
1534 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
1535 }
1536 DBUG_PRINT("info",("spider mysql_row[14]=%s", mysql_row[14]));
1537 if (!spider_share->table_share->table_charset)
1538 {
1539 spider_share->table_share->table_charset = get_charset_by_name(mysql_row[14], MYF(MY_WME));
1540 }
1541 DBUG_RETURN(0);
1542}
1543#endif
1544
1545spider_db_mysql::spider_db_mysql(
1546 SPIDER_CONN *conn
1547) : spider_db_conn(conn), lock_table_hash_inited(FALSE),
1548 handler_open_array_inited(FALSE)
1549{
1550 DBUG_ENTER("spider_db_mysql::spider_db_mysql");
1551 DBUG_PRINT("info",("spider this=%p", this));
1552 db_conn = NULL;
1553 DBUG_VOID_RETURN;
1554}
1555
1556spider_db_mysql::~spider_db_mysql()
1557{
1558 DBUG_ENTER("spider_db_mysql::~spider_db_mysql");
1559 DBUG_PRINT("info",("spider this=%p", this));
1560 if (handler_open_array_inited)
1561 {
1562 reset_opened_handler();
1563 spider_free_mem_calc(spider_current_trx,
1564 handler_open_array_id,
1565 handler_open_array.max_element *
1566 handler_open_array.size_of_element);
1567 delete_dynamic(&handler_open_array);
1568 }
1569 if (lock_table_hash_inited)
1570 {
1571 spider_free_mem_calc(spider_current_trx,
1572 lock_table_hash_id,
1573 lock_table_hash.array.max_element *
1574 lock_table_hash.array.size_of_element);
1575 my_hash_free(&lock_table_hash);
1576 }
1577 DBUG_VOID_RETURN;
1578}
1579
1580int spider_db_mysql::init()
1581{
1582 DBUG_ENTER("spider_db_mysql::init");
1583 DBUG_PRINT("info",("spider this=%p", this));
1584 if (
1585 my_hash_init(&lock_table_hash, spd_charset_utf8_bin, 32, 0, 0,
1586 (my_hash_get_key) spider_link_get_key, 0, 0)
1587 ) {
1588 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1589 }
1590 spider_alloc_calc_mem_init(lock_table_hash, 140);
1591 spider_alloc_calc_mem(spider_current_trx,
1592 lock_table_hash,
1593 lock_table_hash.array.max_element *
1594 lock_table_hash.array.size_of_element);
1595 lock_table_hash_inited = TRUE;
1596
1597 if (
1598 SPD_INIT_DYNAMIC_ARRAY2(&handler_open_array,
1599 sizeof(SPIDER_LINK_FOR_HASH *), NULL, 16, 16, MYF(MY_WME))
1600 ) {
1601 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1602 }
1603 spider_alloc_calc_mem_init(handler_open_array, 162);
1604 spider_alloc_calc_mem(spider_current_trx,
1605 handler_open_array,
1606 handler_open_array.max_element *
1607 handler_open_array.size_of_element);
1608 handler_open_array_inited = TRUE;
1609 DBUG_RETURN(0);
1610}
1611
1612bool spider_db_mysql::is_connected()
1613{
1614 DBUG_ENTER("spider_db_mysql::is_connected");
1615 DBUG_PRINT("info",("spider this=%p", this));
1616 DBUG_RETURN(db_conn);
1617}
1618
1619void spider_db_mysql::bg_connect()
1620{
1621 DBUG_ENTER("spider_db_mysql::bg_connect");
1622 DBUG_PRINT("info",("spider this=%p", this));
1623 DBUG_VOID_RETURN;
1624}
1625
1626int spider_db_mysql::connect(
1627 char *tgt_host,
1628 char *tgt_username,
1629 char *tgt_password,
1630 long tgt_port,
1631 char *tgt_socket,
1632 char *server_name,
1633 int connect_retry_count,
1634 longlong connect_retry_interval
1635) {
1636 int error_num;
1637 my_bool connect_mutex = spider_param_connect_mutex();
1638 DBUG_ENTER("spider_db_mysql::connect");
1639 DBUG_PRINT("info",("spider this=%p", this));
1640 while (TRUE)
1641 {
1642 THD *thd = current_thd;
1643 DBUG_PRINT("info",("spider thd->killed=%s",
1644 thd ? (thd->killed ? "TRUE" : "FALSE") : "NULL"));
1645 DBUG_PRINT("info",("spider abort_loop=%s",
1646 *spd_abort_loop ? "TRUE" : "FALSE"));
1647 if (
1648 (thd && thd->killed) ||
1649 *spd_abort_loop
1650 ) {
1651 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
1652 }
1653
1654 if (!db_conn)
1655 {
1656 if (!(db_conn = mysql_init(NULL)))
1657 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1658 }
1659
1660 mysql_options(db_conn, MYSQL_OPT_READ_TIMEOUT,
1661 &conn->net_read_timeout);
1662 mysql_options(db_conn, MYSQL_OPT_WRITE_TIMEOUT,
1663 &conn->net_write_timeout);
1664 mysql_options(db_conn, MYSQL_OPT_CONNECT_TIMEOUT,
1665 &conn->connect_timeout);
1666 mysql_options(db_conn, MYSQL_OPT_USE_REMOTE_CONNECTION,
1667 NULL);
1668
1669 if (
1670 conn->tgt_ssl_ca_length |
1671 conn->tgt_ssl_capath_length |
1672 conn->tgt_ssl_cert_length |
1673 conn->tgt_ssl_key_length
1674 ) {
1675 mysql_ssl_set(db_conn, conn->tgt_ssl_key, conn->tgt_ssl_cert,
1676 conn->tgt_ssl_ca, conn->tgt_ssl_capath, conn->tgt_ssl_cipher);
1677 if (conn->tgt_ssl_vsc)
1678 {
1679 my_bool verify_flg = TRUE;
1680 mysql_options(db_conn, MYSQL_OPT_SSL_VERIFY_SERVER_CERT,
1681 &verify_flg);
1682 }
1683 }
1684
1685 if (conn->tgt_default_file)
1686 {
1687 DBUG_PRINT("info",("spider tgt_default_file=%s",
1688 conn->tgt_default_file));
1689 mysql_options(db_conn, MYSQL_READ_DEFAULT_FILE,
1690 conn->tgt_default_file);
1691 }
1692 if (conn->tgt_default_group)
1693 {
1694 DBUG_PRINT("info",("spider tgt_default_group=%s",
1695 conn->tgt_default_group));
1696 mysql_options(db_conn, MYSQL_READ_DEFAULT_GROUP,
1697 conn->tgt_default_group);
1698 }
1699
1700 if (connect_mutex)
1701 pthread_mutex_lock(&spider_open_conn_mutex);
1702 /* tgt_db not use */
1703 if (
1704 !spider_param_dry_access() &&
1705 !mysql_real_connect(
1706 db_conn,
1707 tgt_host,
1708 tgt_username,
1709 tgt_password,
1710 NULL,
1711 tgt_port,
1712 tgt_socket,
1713 CLIENT_MULTI_STATEMENTS
1714 )
1715 ) {
1716 if (connect_mutex)
1717 pthread_mutex_unlock(&spider_open_conn_mutex);
1718 error_num = mysql_errno(db_conn);
1719 disconnect();
1720 DBUG_PRINT("info",("spider thd->killed=%s",
1721 thd ? (thd->killed ? "TRUE" : "FALSE") : "NULL"));
1722 DBUG_PRINT("info",("spider abort_loop=%s",
1723 *spd_abort_loop ? "TRUE" : "FALSE"));
1724 if (
1725 (thd && thd->killed) ||
1726 *spd_abort_loop
1727 ) {
1728 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
1729 }
1730 if (
1731 (
1732 error_num != CR_CONN_HOST_ERROR &&
1733 error_num != CR_CONNECTION_ERROR
1734 ) ||
1735 !connect_retry_count
1736 ) {
1737 if (error_num == ER_CON_COUNT_ERROR)
1738 {
1739 *conn->need_mon = 0;
1740 my_error(ER_CON_COUNT_ERROR, MYF(0));
1741 DBUG_RETURN(ER_CON_COUNT_ERROR);
1742 }
1743 *conn->need_mon = ER_CONNECT_TO_FOREIGN_DATA_SOURCE;
1744 my_error(ER_CONNECT_TO_FOREIGN_DATA_SOURCE, MYF(0),
1745 server_name ? server_name : tgt_host);
1746 DBUG_RETURN(ER_CONNECT_TO_FOREIGN_DATA_SOURCE);
1747 }
1748 connect_retry_count--;
1749 my_sleep((ulong) connect_retry_interval);
1750 } else {
1751 if (connect_mutex)
1752 pthread_mutex_unlock(&spider_open_conn_mutex);
1753 break;
1754 }
1755 }
1756 DBUG_RETURN(0);
1757}
1758
1759int spider_db_mysql::ping(
1760) {
1761 DBUG_ENTER("spider_db_mysql::ping");
1762 DBUG_PRINT("info",("spider this=%p", this));
1763 if (spider_param_dry_access())
1764 DBUG_RETURN(0);
1765 DBUG_RETURN(simple_command(db_conn, COM_PING, 0, 0, 0));
1766}
1767
1768void spider_db_mysql::bg_disconnect()
1769{
1770 DBUG_ENTER("spider_db_mysql::bg_disconnect");
1771 DBUG_PRINT("info",("spider this=%p", this));
1772 DBUG_VOID_RETURN;
1773}
1774
1775void spider_db_mysql::disconnect()
1776{
1777 DBUG_ENTER("spider_db_mysql::disconnect");
1778 DBUG_PRINT("info",("spider this=%p", this));
1779 DBUG_PRINT("info",("spider db_conn=%p", db_conn));
1780 if (db_conn)
1781 {
1782 mysql_close(db_conn);
1783 db_conn = NULL;
1784 }
1785 DBUG_VOID_RETURN;
1786}
1787
1788int spider_db_mysql::set_net_timeout()
1789{
1790 DBUG_ENTER("spider_db_mysql::set_net_timeout");
1791 DBUG_PRINT("info",("spider this=%p", this));
1792 DBUG_PRINT("info",("spider conn=%p", conn));
1793 my_net_set_read_timeout(&db_conn->net, conn->net_read_timeout);
1794 my_net_set_write_timeout(&db_conn->net, conn->net_write_timeout);
1795 DBUG_RETURN(0);
1796}
1797
1798int spider_db_mysql::exec_query(
1799 const char *query,
1800 uint length,
1801 int quick_mode
1802) {
1803 int error_num = 0;
1804 uint log_result_errors = spider_param_log_result_errors();
1805 DBUG_ENTER("spider_db_mysql::exec_query");
1806 DBUG_PRINT("info",("spider this=%p", this));
1807 if (spider_param_general_log())
1808 {
1809 const char *tgt_str = conn->tgt_host;
1810 uint32 tgt_len = conn->tgt_host_length;
1811 spider_string tmp_query_str;
1812 tmp_query_str.init_calc_mem(230);
1813 if (tmp_query_str.reserve(
1814 length + conn->tgt_wrapper_length +
1815 tgt_len + (SPIDER_SQL_SPACE_LEN * 2)))
1816 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1817 tmp_query_str.q_append(conn->tgt_wrapper, conn->tgt_wrapper_length);
1818 tmp_query_str.q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
1819 tmp_query_str.q_append(tgt_str, tgt_len);
1820 tmp_query_str.q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
1821 tmp_query_str.q_append(query, length);
1822 general_log_write(current_thd, COM_QUERY, tmp_query_str.ptr(),
1823 tmp_query_str.length());
1824 }
1825 if (!spider_param_dry_access())
1826 {
1827 error_num = mysql_real_query(db_conn, query, length);
1828 }
1829 if (
1830 (error_num && log_result_errors >= 1) ||
1831 (log_result_errors >= 2 && db_conn->warning_count > 0) ||
1832 (log_result_errors >= 4)
1833 ) {
1834 THD *thd = current_thd;
1835 uint log_result_error_with_sql = spider_param_log_result_error_with_sql();
1836 if (log_result_error_with_sql)
1837 {
1838 time_t cur_time = (time_t) time((time_t*) 0);
1839 struct tm lt;
1840 struct tm *l_time = localtime_r(&cur_time, &lt);
1841 spider_string tmp_query_str;
1842 tmp_query_str.init_calc_mem(243);
1843 uint query_length = thd->query_length();
1844 if ((log_result_error_with_sql & 2) && query_length)
1845 {
1846 Security_context *security_ctx = thd->security_ctx;
1847 tmp_query_str.length(0);
1848 if (tmp_query_str.reserve(query_length + 1))
1849 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1850 tmp_query_str.q_append(thd->query(), query_length);
1851 fprintf(stderr, "%04d%02d%02d %02d:%02d:%02d [RECV SPIDER SQL] "
1852 "from [%s][%s] to %ld: "
1853 "sql: %s\n",
1854 l_time->tm_year + 1900, l_time->tm_mon + 1, l_time->tm_mday,
1855 l_time->tm_hour, l_time->tm_min, l_time->tm_sec,
1856 security_ctx->user ? security_ctx->user : "system user",
1857 security_ctx->host_or_ip,
1858 (ulong) thd->thread_id,
1859 tmp_query_str.c_ptr_safe());
1860 }
1861 if (log_result_error_with_sql & 1)
1862 {
1863 tmp_query_str.length(0);
1864 if (tmp_query_str.reserve(length + 1))
1865 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1866 tmp_query_str.q_append(query, length);
1867 fprintf(stderr, "%04d%02d%02d %02d:%02d:%02d [SEND SPIDER SQL] "
1868 "from %ld to [%s] %ld: "
1869 "sql: %s\n",
1870 l_time->tm_year + 1900, l_time->tm_mon + 1, l_time->tm_mday,
1871 l_time->tm_hour, l_time->tm_min, l_time->tm_sec,
1872 (ulong) thd->thread_id, conn->tgt_host, (ulong) db_conn->thread_id,
1873 tmp_query_str.c_ptr_safe());
1874 }
1875 }
1876 if (log_result_errors >= 2 && db_conn->warning_count > 0)
1877 {
1878 time_t cur_time = (time_t) time((time_t*) 0);
1879 struct tm lt;
1880 struct tm *l_time = localtime_r(&cur_time, &lt);
1881 fprintf(stderr, "%04d%02d%02d %02d:%02d:%02d [WARN SPIDER RESULT] "
1882 "from [%s] %ld to %ld: "
1883 "affected_rows: %llu id: %llu status: %u warning_count: %u\n",
1884 l_time->tm_year + 1900, l_time->tm_mon + 1, l_time->tm_mday,
1885 l_time->tm_hour, l_time->tm_min, l_time->tm_sec,
1886 conn->tgt_host, (ulong) db_conn->thread_id, (ulong) thd->thread_id,
1887 db_conn->affected_rows, db_conn->insert_id,
1888 db_conn->server_status, db_conn->warning_count);
1889 if (spider_param_log_result_errors() >= 3)
1890 print_warnings(l_time);
1891 } else if (log_result_errors >= 4)
1892 {
1893 time_t cur_time = (time_t) time((time_t*) 0);
1894 struct tm lt;
1895 struct tm *l_time = localtime_r(&cur_time, &lt);
1896 fprintf(stderr, "%04d%02d%02d %02d:%02d:%02d [INFO SPIDER RESULT] "
1897 "from [%s] %ld to %ld: "
1898 "affected_rows: %llu id: %llu status: %u warning_count: %u\n",
1899 l_time->tm_year + 1900, l_time->tm_mon + 1, l_time->tm_mday,
1900 l_time->tm_hour, l_time->tm_min, l_time->tm_sec,
1901 conn->tgt_host, (ulong) db_conn->thread_id, (ulong) thd->thread_id,
1902 db_conn->affected_rows, db_conn->insert_id,
1903 db_conn->server_status, db_conn->warning_count);
1904 }
1905 }
1906 DBUG_RETURN(error_num);
1907}
1908
1909int spider_db_mysql::get_errno()
1910{
1911 DBUG_ENTER("spider_db_mysql::get_errno");
1912 DBUG_PRINT("info",("spider this=%p", this));
1913 stored_error = mysql_errno(db_conn);
1914 DBUG_PRINT("info",("spider stored_error=%d", stored_error));
1915 DBUG_RETURN(stored_error);
1916}
1917
1918const char *spider_db_mysql::get_error()
1919{
1920 const char *error_ptr;
1921 DBUG_ENTER("spider_db_mysql::get_error");
1922 DBUG_PRINT("info",("spider this=%p", this));
1923 error_ptr = mysql_error(db_conn);
1924 DBUG_PRINT("info",("spider error=%s", error_ptr));
1925 DBUG_RETURN(error_ptr);
1926}
1927
1928bool spider_db_mysql::is_server_gone_error(
1929 int error_num
1930) {
1931 bool server_gone;
1932 DBUG_ENTER("spider_db_mysql::is_server_gone_error");
1933 DBUG_PRINT("info",("spider this=%p", this));
1934 server_gone =
1935 (error_num == CR_SERVER_GONE_ERROR || error_num == CR_SERVER_LOST);
1936 DBUG_PRINT("info",("spider server_gone=%s", server_gone ? "TRUE" : "FALSE"));
1937 DBUG_RETURN(server_gone);
1938}
1939
1940bool spider_db_mysql::is_dup_entry_error(
1941 int error_num
1942) {
1943 bool dup_entry;
1944 DBUG_ENTER("spider_db_mysql::is_dup_entry_error");
1945 DBUG_PRINT("info",("spider this=%p", this));
1946 dup_entry =
1947 (
1948 error_num == ER_DUP_ENTRY ||
1949 error_num == ER_DUP_KEY ||
1950 error_num == HA_ERR_FOUND_DUPP_KEY
1951 );
1952 DBUG_PRINT("info",("spider dup_entry=%s", dup_entry ? "TRUE" : "FALSE"));
1953 DBUG_RETURN(dup_entry);
1954}
1955
1956bool spider_db_mysql::is_xa_nota_error(
1957 int error_num
1958) {
1959 bool xa_nota;
1960 DBUG_ENTER("spider_db_mysql::is_xa_nota_error");
1961 DBUG_PRINT("info",("spider this=%p", this));
1962 xa_nota =
1963 (
1964 error_num == ER_XAER_NOTA ||
1965 error_num == ER_XA_RBTIMEOUT ||
1966 error_num == ER_XA_RBDEADLOCK
1967 );
1968 DBUG_PRINT("info",("spider xa_nota=%s", xa_nota ? "TRUE" : "FALSE"));
1969 DBUG_RETURN(xa_nota);
1970}
1971
1972void spider_db_mysql::print_warnings(
1973 struct tm *l_time
1974) {
1975 DBUG_ENTER("spider_db_mysql::print_warnings");
1976 DBUG_PRINT("info",("spider this=%p", this));
1977 if (db_conn->status == MYSQL_STATUS_READY)
1978 {
1979#if MYSQL_VERSION_ID < 50500
1980 if (!(db_conn->last_used_con->server_status & SERVER_MORE_RESULTS_EXISTS))
1981#else
1982 if (!(db_conn->server_status & SERVER_MORE_RESULTS_EXISTS))
1983#endif
1984 {
1985/*
1986 pthread_mutex_lock(&conn->mta_conn_mutex);
1987 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
1988*/
1989 if (
1990 spider_param_dry_access() ||
1991 !mysql_real_query(db_conn, SPIDER_SQL_SHOW_WARNINGS_STR,
1992 SPIDER_SQL_SHOW_WARNINGS_LEN)
1993 ) {
1994 MYSQL_RES *res = NULL;
1995 MYSQL_ROW row = NULL;
1996 uint num_fields;
1997 if (
1998 spider_param_dry_access() ||
1999 !(res = mysql_store_result(db_conn)) ||
2000 !(row = mysql_fetch_row(res))
2001 ) {
2002 if (mysql_errno(db_conn))
2003 {
2004 if (res)
2005 mysql_free_result(res);
2006/*
2007 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2008 pthread_mutex_unlock(&conn->mta_conn_mutex);
2009*/
2010 DBUG_VOID_RETURN;
2011 }
2012 /* no record is ok */
2013 }
2014/*
2015 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2016 pthread_mutex_unlock(&conn->mta_conn_mutex);
2017*/
2018 num_fields = mysql_num_fields(res);
2019 if (num_fields != 3)
2020 {
2021 mysql_free_result(res);
2022 DBUG_VOID_RETURN;
2023 }
2024 while (row)
2025 {
2026 fprintf(stderr, "%04d%02d%02d %02d:%02d:%02d [WARN SPIDER RESULT] "
2027 "from [%s] %ld to %ld: %s %s %s\n",
2028 l_time->tm_year + 1900, l_time->tm_mon + 1, l_time->tm_mday,
2029 l_time->tm_hour, l_time->tm_min, l_time->tm_sec,
2030 conn->tgt_host, (ulong) db_conn->thread_id,
2031 (ulong) current_thd->thread_id, row[0], row[1], row[2]);
2032 row = mysql_fetch_row(res);
2033 }
2034 if (res)
2035 mysql_free_result(res);
2036 } else {
2037/*
2038 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2039 pthread_mutex_unlock(&conn->mta_conn_mutex);
2040*/
2041 }
2042 }
2043 }
2044 DBUG_VOID_RETURN;
2045}
2046
2047spider_db_result *spider_db_mysql::store_result(
2048 spider_db_result_buffer **spider_res_buf,
2049 st_spider_db_request_key *request_key,
2050 int *error_num
2051) {
2052 spider_db_mysql_result *result;
2053 DBUG_ENTER("spider_db_mysql::store_result");
2054 DBUG_PRINT("info",("spider this=%p", this));
2055 DBUG_ASSERT(!spider_res_buf);
2056 if ((result = new spider_db_mysql_result(this)))
2057 {
2058 *error_num = 0;
2059 if (
2060 spider_param_dry_access() ||
2061 !(result->db_result = mysql_store_result(db_conn))
2062 ) {
2063 delete result;
2064 result = NULL;
2065 } else {
2066 result->first_row = result->db_result->data_cursor;
2067 DBUG_PRINT("info",("spider result->first_row=%p", result->first_row));
2068 }
2069 } else {
2070 *error_num = HA_ERR_OUT_OF_MEM;
2071 }
2072 DBUG_RETURN(result);
2073}
2074
2075spider_db_result *spider_db_mysql::use_result(
2076 st_spider_db_request_key *request_key,
2077 int *error_num
2078) {
2079 spider_db_mysql_result *result;
2080 DBUG_ENTER("spider_db_mysql::use_result");
2081 DBUG_PRINT("info",("spider this=%p", this));
2082 if ((result = new spider_db_mysql_result(this)))
2083 {
2084 *error_num = 0;
2085 if (
2086 spider_param_dry_access() ||
2087 !(result->db_result = db_conn->methods->use_result(db_conn))
2088 ) {
2089 delete result;
2090 result = NULL;
2091 } else {
2092 result->first_row = NULL;
2093 }
2094 } else {
2095 *error_num = HA_ERR_OUT_OF_MEM;
2096 }
2097 DBUG_RETURN(result);
2098}
2099
2100int spider_db_mysql::next_result()
2101{
2102 int status;
2103 DBUG_ENTER("spider_db_mysql::next_result");
2104 DBUG_PRINT("info",("spider this=%p", this));
2105 if (db_conn->status != MYSQL_STATUS_READY)
2106 {
2107 my_message(ER_SPIDER_UNKNOWN_NUM, ER_SPIDER_UNKNOWN_STR, MYF(0));
2108 DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
2109 }
2110
2111 db_conn->net.last_errno = 0;
2112 db_conn->net.last_error[0] = '\0';
2113 strmov(db_conn->net.sqlstate, "00000");
2114 db_conn->affected_rows = ~(my_ulonglong) 0;
2115
2116#if MYSQL_VERSION_ID < 50500
2117 if (db_conn->last_used_con->server_status & SERVER_MORE_RESULTS_EXISTS)
2118#else
2119 if (db_conn->server_status & SERVER_MORE_RESULTS_EXISTS)
2120#endif
2121 {
2122 if ((status = db_conn->methods->read_query_result(db_conn)) > 0)
2123 DBUG_RETURN(spider_db_errorno(conn));
2124 DBUG_RETURN(status);
2125 }
2126 DBUG_RETURN(-1);
2127}
2128
2129uint spider_db_mysql::affected_rows()
2130{
2131 MYSQL *last_used_con;
2132 DBUG_ENTER("spider_db_mysql::affected_rows");
2133 DBUG_PRINT("info",("spider this=%p", this));
2134#if MYSQL_VERSION_ID < 50500
2135 last_used_con = db_conn->last_used_con;
2136#else
2137 last_used_con = db_conn;
2138#endif
2139 DBUG_RETURN((uint) last_used_con->affected_rows);
2140}
2141
2142ulonglong spider_db_mysql::last_insert_id()
2143{
2144 MYSQL *last_used_con;
2145 DBUG_ENTER("spider_db_mysql::last_insert_id");
2146 DBUG_PRINT("info",("spider this=%p", this));
2147#if MYSQL_VERSION_ID < 50500
2148 last_used_con = db_conn->last_used_con;
2149#else
2150 last_used_con = db_conn;
2151#endif
2152 DBUG_RETURN((uint) last_used_con->insert_id);
2153}
2154
2155int spider_db_mysql::set_character_set(
2156 const char *csname
2157) {
2158 DBUG_ENTER("spider_db_mysql::set_character_set");
2159 DBUG_PRINT("info",("spider this=%p", this));
2160 if (spider_param_dry_access())
2161 DBUG_RETURN(0);
2162 DBUG_RETURN(mysql_set_character_set(db_conn, csname));
2163}
2164
2165int spider_db_mysql::select_db(
2166 const char *dbname
2167) {
2168 DBUG_ENTER("spider_db_mysql::select_db");
2169 DBUG_PRINT("info",("spider this=%p", this));
2170 if (spider_param_dry_access())
2171 DBUG_RETURN(0);
2172 DBUG_RETURN(mysql_select_db(db_conn, dbname));
2173}
2174
2175int spider_db_mysql::consistent_snapshot(
2176 int *need_mon
2177) {
2178 DBUG_ENTER("spider_db_mysql::consistent_snapshot");
2179 DBUG_PRINT("info",("spider this=%p", this));
2180 if (spider_db_query(
2181 conn,
2182 SPIDER_SQL_START_CONSISTENT_SNAPSHOT_STR,
2183 SPIDER_SQL_START_CONSISTENT_SNAPSHOT_LEN,
2184 -1,
2185 need_mon)
2186 )
2187 DBUG_RETURN(spider_db_errorno(conn));
2188 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2189 pthread_mutex_unlock(&conn->mta_conn_mutex);
2190 DBUG_RETURN(0);
2191}
2192
2193bool spider_db_mysql::trx_start_in_bulk_sql()
2194{
2195 DBUG_ENTER("spider_db_mysql::trx_start_in_bulk_sql");
2196 DBUG_PRINT("info",("spider this=%p", this));
2197 DBUG_RETURN(TRUE);
2198}
2199
2200int spider_db_mysql::start_transaction(
2201 int *need_mon
2202) {
2203 DBUG_ENTER("spider_db_mysql::start_transaction");
2204 DBUG_PRINT("info",("spider this=%p", this));
2205 if (spider_db_query(
2206 conn,
2207 SPIDER_SQL_START_TRANSACTION_STR,
2208 SPIDER_SQL_START_TRANSACTION_LEN,
2209 -1,
2210 need_mon)
2211 )
2212 DBUG_RETURN(spider_db_errorno(conn));
2213 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2214 pthread_mutex_unlock(&conn->mta_conn_mutex);
2215 DBUG_RETURN(0);
2216}
2217
2218int spider_db_mysql::commit(
2219 int *need_mon
2220) {
2221 DBUG_ENTER("spider_db_mysql::commit");
2222 DBUG_PRINT("info",("spider this=%p", this));
2223 if (spider_db_query(
2224 conn,
2225 SPIDER_SQL_COMMIT_STR,
2226 SPIDER_SQL_COMMIT_LEN,
2227 -1,
2228 need_mon)
2229 )
2230 DBUG_RETURN(spider_db_errorno(conn));
2231 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2232 pthread_mutex_unlock(&conn->mta_conn_mutex);
2233 DBUG_RETURN(0);
2234}
2235
2236int spider_db_mysql::rollback(
2237 int *need_mon
2238) {
2239 bool is_error;
2240 int error_num;
2241 DBUG_ENTER("spider_db_mysql::rollback");
2242 DBUG_PRINT("info",("spider this=%p", this));
2243 conn->mta_conn_mutex_unlock_later = TRUE;
2244 if (spider_db_query(
2245 conn,
2246 SPIDER_SQL_ROLLBACK_STR,
2247 SPIDER_SQL_ROLLBACK_LEN,
2248 -1,
2249 need_mon)
2250 ) {
2251 is_error = conn->thd->is_error();
2252 error_num = spider_db_errorno(conn);
2253 if (
2254 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
2255 !is_error
2256 )
2257 conn->thd->clear_error();
2258 else {
2259 conn->mta_conn_mutex_unlock_later = FALSE;
2260 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2261 pthread_mutex_unlock(&conn->mta_conn_mutex);
2262 DBUG_RETURN(error_num);
2263 }
2264 }
2265 conn->mta_conn_mutex_unlock_later = FALSE;
2266 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2267 pthread_mutex_unlock(&conn->mta_conn_mutex);
2268 DBUG_RETURN(0);
2269}
2270
2271int spider_db_mysql::xa_start(
2272 XID *xid,
2273 int *need_mon
2274) {
2275 DBUG_ENTER("spider_db_mysql::xa_start");
2276 DBUG_PRINT("info",("spider this=%p", this));
2277 DBUG_ASSERT(0);
2278 DBUG_RETURN(0);
2279}
2280
2281bool spider_db_mysql::xa_start_in_bulk_sql()
2282{
2283 DBUG_ENTER("spider_db_mysql::xa_start_in_bulk_sql");
2284 DBUG_PRINT("info",("spider this=%p", this));
2285 DBUG_RETURN(TRUE);
2286}
2287
2288int spider_db_mysql::xa_end(
2289 XID *xid,
2290 int *need_mon
2291) {
2292 char sql_buf[SPIDER_SQL_XA_END_LEN + XIDDATASIZE + sizeof(long) + 9];
2293 spider_string sql_str(sql_buf, sizeof(sql_buf), &my_charset_bin);
2294 DBUG_ENTER("spider_db_mysql::xa_end");
2295 DBUG_PRINT("info",("spider this=%p", this));
2296 sql_str.init_calc_mem(108);
2297
2298 sql_str.length(0);
2299 sql_str.q_append(SPIDER_SQL_XA_END_STR, SPIDER_SQL_XA_END_LEN);
2300 spider_db_append_xid_str(&sql_str, xid);
2301 if (spider_db_query(
2302 conn,
2303 sql_str.ptr(),
2304 sql_str.length(),
2305 -1,
2306 need_mon)
2307 )
2308 DBUG_RETURN(spider_db_errorno(conn));
2309 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2310 pthread_mutex_unlock(&conn->mta_conn_mutex);
2311 DBUG_RETURN(0);
2312}
2313
2314int spider_db_mysql::xa_prepare(
2315 XID *xid,
2316 int *need_mon
2317) {
2318 char sql_buf[SPIDER_SQL_XA_PREPARE_LEN + XIDDATASIZE + sizeof(long) + 9];
2319 spider_string sql_str(sql_buf, sizeof(sql_buf), &my_charset_bin);
2320 DBUG_ENTER("spider_db_mysql::xa_prepare");
2321 DBUG_PRINT("info",("spider this=%p", this));
2322 sql_str.init_calc_mem(109);
2323
2324 sql_str.length(0);
2325 sql_str.q_append(SPIDER_SQL_XA_PREPARE_STR, SPIDER_SQL_XA_PREPARE_LEN);
2326 spider_db_append_xid_str(&sql_str, xid);
2327 if (spider_db_query(
2328 conn,
2329 sql_str.ptr(),
2330 sql_str.length(),
2331 -1,
2332 need_mon)
2333 )
2334 DBUG_RETURN(spider_db_errorno(conn));
2335 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2336 pthread_mutex_unlock(&conn->mta_conn_mutex);
2337 DBUG_RETURN(0);
2338}
2339
2340int spider_db_mysql::xa_commit(
2341 XID *xid,
2342 int *need_mon
2343) {
2344 char sql_buf[SPIDER_SQL_XA_COMMIT_LEN + XIDDATASIZE + sizeof(long) + 9];
2345 spider_string sql_str(sql_buf, sizeof(sql_buf), &my_charset_bin);
2346 DBUG_ENTER("spider_db_mysql::xa_commit");
2347 DBUG_PRINT("info",("spider this=%p", this));
2348 sql_str.init_calc_mem(110);
2349
2350 sql_str.length(0);
2351 sql_str.q_append(SPIDER_SQL_XA_COMMIT_STR, SPIDER_SQL_XA_COMMIT_LEN);
2352 spider_db_append_xid_str(&sql_str, xid);
2353 if (spider_db_query(
2354 conn,
2355 sql_str.ptr(),
2356 sql_str.length(),
2357 -1,
2358 need_mon)
2359 )
2360 DBUG_RETURN(spider_db_errorno(conn));
2361 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2362 pthread_mutex_unlock(&conn->mta_conn_mutex);
2363 DBUG_RETURN(0);
2364}
2365
2366int spider_db_mysql::xa_rollback(
2367 XID *xid,
2368 int *need_mon
2369) {
2370 char sql_buf[SPIDER_SQL_XA_ROLLBACK_LEN + XIDDATASIZE + sizeof(long) + 9];
2371 spider_string sql_str(sql_buf, sizeof(sql_buf), &my_charset_bin);
2372 DBUG_ENTER("spider_db_mysql::xa_rollback");
2373 DBUG_PRINT("info",("spider this=%p", this));
2374 sql_str.init_calc_mem(111);
2375
2376 sql_str.length(0);
2377 sql_str.q_append(SPIDER_SQL_XA_ROLLBACK_STR, SPIDER_SQL_XA_ROLLBACK_LEN);
2378 spider_db_append_xid_str(&sql_str, xid);
2379 if (spider_db_query(
2380 conn,
2381 sql_str.ptr(),
2382 sql_str.length(),
2383 -1,
2384 need_mon)
2385 )
2386 DBUG_RETURN(spider_db_errorno(conn));
2387 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2388 pthread_mutex_unlock(&conn->mta_conn_mutex);
2389 DBUG_RETURN(0);
2390}
2391
2392bool spider_db_mysql::set_trx_isolation_in_bulk_sql()
2393{
2394 DBUG_ENTER("spider_db_mysql::set_trx_isolation_in_bulk_sql");
2395 DBUG_PRINT("info",("spider this=%p", this));
2396 DBUG_RETURN(TRUE);
2397}
2398
2399int spider_db_mysql::set_trx_isolation(
2400 int trx_isolation,
2401 int *need_mon
2402) {
2403 DBUG_ENTER("spider_db_mysql::set_trx_isolation");
2404 DBUG_PRINT("info",("spider this=%p", this));
2405 switch (trx_isolation)
2406 {
2407 case ISO_READ_UNCOMMITTED:
2408 if (spider_db_query(
2409 conn,
2410 SPIDER_SQL_ISO_READ_UNCOMMITTED_STR,
2411 SPIDER_SQL_ISO_READ_UNCOMMITTED_LEN,
2412 -1,
2413 need_mon)
2414 )
2415 DBUG_RETURN(spider_db_errorno(conn));
2416 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2417 pthread_mutex_unlock(&conn->mta_conn_mutex);
2418 break;
2419 case ISO_READ_COMMITTED:
2420 if (spider_db_query(
2421 conn,
2422 SPIDER_SQL_ISO_READ_COMMITTED_STR,
2423 SPIDER_SQL_ISO_READ_COMMITTED_LEN,
2424 -1,
2425 need_mon)
2426 )
2427 DBUG_RETURN(spider_db_errorno(conn));
2428 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2429 pthread_mutex_unlock(&conn->mta_conn_mutex);
2430 break;
2431 case ISO_REPEATABLE_READ:
2432 if (spider_db_query(
2433 conn,
2434 SPIDER_SQL_ISO_REPEATABLE_READ_STR,
2435 SPIDER_SQL_ISO_REPEATABLE_READ_LEN,
2436 -1,
2437 need_mon)
2438 )
2439 DBUG_RETURN(spider_db_errorno(conn));
2440 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2441 pthread_mutex_unlock(&conn->mta_conn_mutex);
2442 break;
2443 case ISO_SERIALIZABLE:
2444 if (spider_db_query(
2445 conn,
2446 SPIDER_SQL_ISO_SERIALIZABLE_STR,
2447 SPIDER_SQL_ISO_SERIALIZABLE_LEN,
2448 -1,
2449 need_mon)
2450 )
2451 DBUG_RETURN(spider_db_errorno(conn));
2452 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2453 pthread_mutex_unlock(&conn->mta_conn_mutex);
2454 break;
2455 default:
2456 DBUG_RETURN(HA_ERR_UNSUPPORTED);
2457 }
2458 DBUG_RETURN(0);
2459}
2460
2461bool spider_db_mysql::set_autocommit_in_bulk_sql()
2462{
2463 DBUG_ENTER("spider_db_mysql::set_autocommit_in_bulk_sql");
2464 DBUG_PRINT("info",("spider this=%p", this));
2465 DBUG_RETURN(TRUE);
2466}
2467
2468int spider_db_mysql::set_autocommit(
2469 bool autocommit,
2470 int *need_mon
2471) {
2472 DBUG_ENTER("spider_db_mysql::set_autocommit");
2473 DBUG_PRINT("info",("spider this=%p", this));
2474 if (autocommit)
2475 {
2476 if (spider_db_query(
2477 conn,
2478 SPIDER_SQL_AUTOCOMMIT_ON_STR,
2479 SPIDER_SQL_AUTOCOMMIT_ON_LEN,
2480 -1,
2481 need_mon)
2482 )
2483 DBUG_RETURN(spider_db_errorno(conn));
2484 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2485 pthread_mutex_unlock(&conn->mta_conn_mutex);
2486 } else {
2487 if (spider_db_query(
2488 conn,
2489 SPIDER_SQL_AUTOCOMMIT_OFF_STR,
2490 SPIDER_SQL_AUTOCOMMIT_OFF_LEN,
2491 -1,
2492 need_mon)
2493 )
2494 DBUG_RETURN(spider_db_errorno(conn));
2495 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2496 pthread_mutex_unlock(&conn->mta_conn_mutex);
2497 }
2498 DBUG_RETURN(0);
2499}
2500
2501bool spider_db_mysql::set_sql_log_off_in_bulk_sql()
2502{
2503 DBUG_ENTER("spider_db_mysql::set_sql_log_off_in_bulk_sql");
2504 DBUG_PRINT("info",("spider this=%p", this));
2505 DBUG_RETURN(TRUE);
2506}
2507
2508int spider_db_mysql::set_sql_log_off(
2509 bool sql_log_off,
2510 int *need_mon
2511) {
2512 DBUG_ENTER("spider_db_mysql::set_sql_log_off");
2513 DBUG_PRINT("info",("spider this=%p", this));
2514 if (sql_log_off)
2515 {
2516 if (spider_db_query(
2517 conn,
2518 SPIDER_SQL_SQL_LOG_ON_STR,
2519 SPIDER_SQL_SQL_LOG_ON_LEN,
2520 -1,
2521 need_mon)
2522 )
2523 DBUG_RETURN(spider_db_errorno(conn));
2524 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2525 pthread_mutex_unlock(&conn->mta_conn_mutex);
2526 } else {
2527 if (spider_db_query(
2528 conn,
2529 SPIDER_SQL_SQL_LOG_OFF_STR,
2530 SPIDER_SQL_SQL_LOG_OFF_LEN,
2531 -1,
2532 need_mon)
2533 )
2534 DBUG_RETURN(spider_db_errorno(conn));
2535 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2536 pthread_mutex_unlock(&conn->mta_conn_mutex);
2537 }
2538 DBUG_RETURN(0);
2539}
2540
2541bool spider_db_mysql::set_time_zone_in_bulk_sql()
2542{
2543 DBUG_ENTER("spider_db_mysql::set_time_zone_in_bulk_sql");
2544 DBUG_PRINT("info",("spider this=%p", this));
2545 DBUG_RETURN(TRUE);
2546}
2547
2548int spider_db_mysql::set_time_zone(
2549 Time_zone *time_zone,
2550 int *need_mon
2551) {
2552 const String *tz_str = time_zone->get_name();
2553 char sql_buf[MAX_FIELD_WIDTH];
2554 spider_string sql_str(sql_buf, sizeof(sql_buf), &my_charset_bin);
2555 DBUG_ENTER("spider_db_mysql::set_time_zone");
2556 DBUG_PRINT("info",("spider this=%p", this));
2557 sql_str.init_calc_mem(214);
2558 sql_str.length(0);
2559 if (sql_str.reserve(SPIDER_SQL_TIME_ZONE_LEN +
2560 tz_str->length() + SPIDER_SQL_VALUE_QUOTE_LEN))
2561 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2562 sql_str.q_append(SPIDER_SQL_TIME_ZONE_STR, SPIDER_SQL_TIME_ZONE_LEN);
2563 sql_str.q_append(tz_str->ptr(), tz_str->length());
2564 sql_str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
2565 if (spider_db_query(
2566 conn,
2567 sql_str.ptr(),
2568 sql_str.length(),
2569 -1,
2570 need_mon)
2571 )
2572 DBUG_RETURN(spider_db_errorno(conn));
2573 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2574 pthread_mutex_unlock(&conn->mta_conn_mutex);
2575 DBUG_RETURN(0);
2576}
2577
2578int spider_db_mysql::exec_simple_sql_with_result(
2579 SPIDER_TRX *trx,
2580 SPIDER_SHARE *share,
2581 const char *sql,
2582 uint sql_length,
2583 int all_link_idx,
2584 int *need_mon,
2585 SPIDER_DB_RESULT **res
2586) {
2587 int error_num;
2588 DBUG_ENTER("spider_db_mysql::exec_simple_sql_with_result");
2589 pthread_mutex_lock(&conn->mta_conn_mutex);
2590 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
2591 conn->need_mon = need_mon;
2592 conn->mta_conn_mutex_lock_already = TRUE;
2593 conn->mta_conn_mutex_unlock_later = TRUE;
2594 spider_conn_set_timeout_from_share(conn, all_link_idx, trx->thd,
2595 share);
2596 if (
2597 (error_num = spider_db_set_names_internal(trx, share, conn,
2598 all_link_idx, need_mon)) ||
2599 (
2600 spider_db_query(
2601 conn,
2602 sql,
2603 sql_length,
2604 -1,
2605 need_mon) &&
2606 (error_num = spider_db_errorno(conn))
2607 )
2608 ) {
2609 if (
2610 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
2611 !conn->disable_reconnect
2612 ) {
2613 /* retry */
2614 if ((error_num = spider_db_ping_internal(share, conn,
2615 all_link_idx, need_mon)))
2616 {
2617 conn->mta_conn_mutex_lock_already = FALSE;
2618 conn->mta_conn_mutex_unlock_later = FALSE;
2619 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2620 pthread_mutex_unlock(&conn->mta_conn_mutex);
2621 DBUG_PRINT("info", ("spider error_num=%d 1", error_num));
2622 DBUG_RETURN(error_num);
2623 }
2624 if ((error_num = spider_db_set_names_internal(trx, share, conn,
2625 all_link_idx, need_mon)))
2626 {
2627 conn->mta_conn_mutex_lock_already = FALSE;
2628 conn->mta_conn_mutex_unlock_later = FALSE;
2629 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2630 pthread_mutex_unlock(&conn->mta_conn_mutex);
2631 DBUG_PRINT("info", ("spider error_num=%d 2", error_num));
2632 DBUG_RETURN(error_num);
2633 }
2634 spider_conn_set_timeout_from_share(conn, all_link_idx, trx->thd,
2635 share);
2636 if (spider_db_query(
2637 conn,
2638 sql,
2639 sql_length,
2640 -1,
2641 need_mon)
2642 ) {
2643 conn->mta_conn_mutex_lock_already = FALSE;
2644 conn->mta_conn_mutex_unlock_later = FALSE;
2645 DBUG_PRINT("info", ("spider error_num=%d 3", error_num));
2646 DBUG_RETURN(spider_db_errorno(conn));
2647 }
2648 } else {
2649 conn->mta_conn_mutex_lock_already = FALSE;
2650 conn->mta_conn_mutex_unlock_later = FALSE;
2651 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2652 pthread_mutex_unlock(&conn->mta_conn_mutex);
2653 DBUG_PRINT("info", ("spider error_num=%d 4", error_num));
2654 DBUG_RETURN(error_num);
2655 }
2656 }
2657 if (!(*res = store_result(NULL, NULL, &error_num)))
2658 {
2659 conn->mta_conn_mutex_lock_already = FALSE;
2660 conn->mta_conn_mutex_unlock_later = FALSE;
2661 if (error_num || (error_num = spider_db_errorno(conn)))
2662 {
2663 DBUG_PRINT("info", ("spider error_num=%d 5", error_num));
2664 DBUG_RETURN(error_num);
2665 } else {
2666 DBUG_PRINT("info", ("spider error_num=%d 6",
2667 ER_QUERY_ON_FOREIGN_DATA_SOURCE));
2668 DBUG_RETURN(ER_QUERY_ON_FOREIGN_DATA_SOURCE);
2669 }
2670 }
2671 conn->mta_conn_mutex_lock_already = FALSE;
2672 conn->mta_conn_mutex_unlock_later = FALSE;
2673 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
2674 pthread_mutex_unlock(&conn->mta_conn_mutex);
2675 DBUG_RETURN(0);
2676}
2677
2678int spider_db_mysql::show_master_status(
2679 SPIDER_TRX *trx,
2680 SPIDER_SHARE *share,
2681 int all_link_idx,
2682 int *need_mon,
2683 TABLE *table,
2684 spider_string *str,
2685 int mode,
2686 SPIDER_DB_RESULT **res1,
2687 SPIDER_DB_RESULT **res2
2688) {
2689 int error_num;
2690 const char *binlog_file_name, *binlog_pos;
2691 uint binlog_file_name_length, binlog_pos_length;
2692 DBUG_ENTER("spider_db_mysql::show_master_status");
2693 if ((error_num = exec_simple_sql_with_result(trx, share,
2694 SPIDER_SQL_SHOW_MASTER_STATUS_STR, SPIDER_SQL_SHOW_MASTER_STATUS_LEN,
2695 all_link_idx, need_mon, res1))
2696 ) {
2697 DBUG_PRINT("info", ("spider error_num=%d 1", error_num));
2698 DBUG_RETURN(error_num);
2699 }
2700
2701 if (!(error_num = ((spider_db_mysql_result *)*res1)->fetch_show_master_status(
2702 &binlog_file_name, &binlog_pos))
2703 ) {
2704 binlog_file_name_length = strlen(binlog_file_name);
2705 binlog_pos_length = strlen(binlog_pos);
2706 spider_store_binlog_pos_binlog_file(table,
2707 binlog_file_name, binlog_file_name_length,
2708 binlog_pos, binlog_pos_length, conn->access_charset);
2709 if (mode > 0)
2710 {
2711 error_num = select_binlog_gtid_pos(
2712 trx,
2713 share,
2714 all_link_idx,
2715 need_mon,
2716 table,
2717 str,
2718 binlog_file_name,
2719 binlog_file_name_length,
2720 binlog_pos,
2721 binlog_pos_length,
2722 res2
2723 );
2724 } else {
2725 spider_store_binlog_pos_gtid(table, NULL, 0, conn->access_charset);
2726 }
2727 }
2728/*
2729 res->free_result();
2730 delete res;
2731*/
2732 if (error_num)
2733 {
2734 DBUG_PRINT("info", ("spider error_num=%d 2", error_num));
2735 DBUG_RETURN(error_num);
2736 }
2737 DBUG_RETURN(0);
2738}
2739
2740int spider_db_mysql::select_binlog_gtid_pos(
2741 SPIDER_TRX *trx,
2742 SPIDER_SHARE *share,
2743 int all_link_idx,
2744 int *need_mon,
2745 TABLE *table,
2746 spider_string *str,
2747 const char *binlog_file_name,
2748 uint binlog_file_name_length,
2749 const char *binlog_pos,
2750 uint binlog_pos_length,
2751 SPIDER_DB_RESULT **res
2752) {
2753 int error_num;
2754 size_t length;
2755 const char *gtid_pos;
2756 DBUG_ENTER("spider_db_mysql::select_binlog_gtid_pos");
2757 str->length(0);
2758 if (str->reserve(
2759 SPIDER_SQL_BINLOG_GTID_POS_LEN +
2760 SPIDER_SQL_OPEN_PAREN_LEN +
2761 SPIDER_SQL_VALUE_QUOTE_LEN +
2762 binlog_file_name_length * 2 +
2763 SPIDER_SQL_VALUE_QUOTE_LEN +
2764 SPIDER_SQL_COMMA_LEN +
2765 SPIDER_SQL_VALUE_QUOTE_LEN +
2766 binlog_pos_length * 2 +
2767 SPIDER_SQL_VALUE_QUOTE_LEN +
2768 SPIDER_SQL_CLOSE_PAREN_LEN
2769 ))
2770 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
2771 str->q_append(SPIDER_SQL_BINLOG_GTID_POS_STR,
2772 SPIDER_SQL_BINLOG_GTID_POS_LEN);
2773 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
2774 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
2775 length = conn->db_conn->escape_string((char *)str->ptr() + str->length(),
2776 binlog_file_name, binlog_file_name_length);
2777 str->length(str->length() + length);
2778 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
2779 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
2780 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
2781 length = conn->db_conn->escape_string((char *)str->ptr() + str->length(),
2782 binlog_pos, binlog_pos_length);
2783 str->length(str->length() + length);
2784 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
2785 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
2786
2787 if ((error_num = exec_simple_sql_with_result(trx, share,
2788 str->ptr(), str->length(), all_link_idx, need_mon, res)))
2789 {
2790 DBUG_PRINT("info", ("spider error_num=%d 1", error_num));
2791 DBUG_RETURN(error_num);
2792 }
2793 if (!(error_num = ((spider_db_mysql_result *)*res)->fetch_select_binlog_gtid_pos(&gtid_pos)))
2794 {
2795 spider_store_binlog_pos_gtid(table, gtid_pos, strlen(gtid_pos), conn->access_charset);
2796 }
2797/*
2798 res->free_result();
2799 delete res;
2800*/
2801 if (error_num)
2802 {
2803 DBUG_PRINT("info", ("spider error_num=%d 2", error_num));
2804 DBUG_RETURN(error_num);
2805 }
2806 DBUG_RETURN(0);
2807}
2808
2809#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2810int spider_db_mysql::append_sql(
2811 char *sql,
2812 ulong sql_length,
2813 st_spider_db_request_key *request_key
2814) {
2815 DBUG_ENTER("spider_db_mysql::append_sql");
2816 DBUG_PRINT("info",("spider this=%p", this));
2817 DBUG_ASSERT(0);
2818 DBUG_RETURN(0);
2819}
2820
2821int spider_db_mysql::append_open_handler(
2822 uint handler_id,
2823 const char *db_name,
2824 const char *table_name,
2825 const char *index_name,
2826 const char *sql,
2827 st_spider_db_request_key *request_key
2828) {
2829 DBUG_ENTER("spider_db_mysql::append_open_handler");
2830 DBUG_PRINT("info",("spider this=%p", this));
2831 DBUG_ASSERT(0);
2832 DBUG_RETURN(0);
2833}
2834
2835int spider_db_mysql::append_select(
2836 uint handler_id,
2837 spider_string *sql,
2838 SPIDER_DB_HS_STRING_REF_BUFFER *keys,
2839 int limit,
2840 int skip,
2841 st_spider_db_request_key *request_key
2842) {
2843 DBUG_ENTER("spider_db_mysql::append_select");
2844 DBUG_PRINT("info",("spider this=%p", this));
2845 DBUG_ASSERT(0);
2846 DBUG_RETURN(0);
2847}
2848
2849int spider_db_mysql::append_insert(
2850 uint handler_id,
2851 SPIDER_DB_HS_STRING_REF_BUFFER *upds,
2852 st_spider_db_request_key *request_key
2853) {
2854 DBUG_ENTER("spider_db_mysql::append_insert");
2855 DBUG_PRINT("info",("spider this=%p", this));
2856 DBUG_ASSERT(0);
2857 DBUG_RETURN(0);
2858}
2859
2860int spider_db_mysql::append_update(
2861 uint handler_id,
2862 spider_string *sql,
2863 SPIDER_DB_HS_STRING_REF_BUFFER *keys,
2864 SPIDER_DB_HS_STRING_REF_BUFFER *upds,
2865 int limit,
2866 int skip,
2867 bool increment,
2868 bool decrement,
2869 st_spider_db_request_key *request_key
2870) {
2871 DBUG_ENTER("spider_db_mysql::append_update");
2872 DBUG_PRINT("info",("spider this=%p", this));
2873 DBUG_ASSERT(0);
2874 DBUG_RETURN(0);
2875}
2876
2877int spider_db_mysql::append_delete(
2878 uint handler_id,
2879 spider_string *sql,
2880 SPIDER_DB_HS_STRING_REF_BUFFER *keys,
2881 int limit,
2882 int skip,
2883 st_spider_db_request_key *request_key
2884) {
2885 DBUG_ENTER("spider_db_mysql::append_delete");
2886 DBUG_PRINT("info",("spider this=%p", this));
2887 DBUG_ASSERT(0);
2888 DBUG_RETURN(0);
2889}
2890
2891void spider_db_mysql::reset_request_queue()
2892{
2893 DBUG_ENTER("spider_db_mysql::reset_request_queue");
2894 DBUG_PRINT("info",("spider this=%p", this));
2895 DBUG_ASSERT(0);
2896 DBUG_VOID_RETURN;
2897}
2898#endif
2899
2900size_t spider_db_mysql::escape_string(
2901 char *to,
2902 const char *from,
2903 size_t from_length
2904) {
2905 DBUG_ENTER("spider_db_mysql::escape_string");
2906 DBUG_PRINT("info",("spider this=%p", this));
2907 if (db_conn->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
2908 DBUG_RETURN(escape_quotes_for_mysql(db_conn->charset, to, 0,
2909 from, from_length));
2910 DBUG_RETURN(escape_string_for_mysql(db_conn->charset, to, 0,
2911 from, from_length));
2912}
2913
2914bool spider_db_mysql::have_lock_table_list()
2915{
2916 DBUG_ENTER("spider_db_mysql::have_lock_table_list");
2917 DBUG_PRINT("info",("spider this=%p", this));
2918 DBUG_RETURN(lock_table_hash.records);
2919}
2920
2921int spider_db_mysql::append_lock_tables(
2922 spider_string *str
2923) {
2924 int error_num;
2925 ha_spider *tmp_spider;
2926 int lock_type;
2927 uint conn_link_idx;
2928 int tmp_link_idx;
2929 SPIDER_LINK_FOR_HASH *tmp_link_for_hash;
2930 const char *db_name;
2931 uint db_name_length;
2932 CHARSET_INFO *db_name_charset;
2933 const char *table_name;
2934 uint table_name_length;
2935 CHARSET_INFO *table_name_charset;
2936 DBUG_ENTER("spider_db_mysql::lock_tables");
2937 DBUG_PRINT("info",("spider this=%p", this));
2938 if ((error_num = spider_db_mysql_utility.append_lock_table_head(str)))
2939 {
2940 DBUG_RETURN(error_num);
2941 }
2942 while ((tmp_link_for_hash =
2943 (SPIDER_LINK_FOR_HASH *) my_hash_element(&lock_table_hash, 0)))
2944 {
2945 tmp_spider = tmp_link_for_hash->spider;
2946 tmp_link_idx = tmp_link_for_hash->link_idx;
2947 switch (tmp_spider->lock_type)
2948 {
2949 case TL_READ:
2950 lock_type = SPIDER_DB_TABLE_LOCK_READ_LOCAL;
2951 break;
2952 case TL_READ_NO_INSERT:
2953 lock_type = SPIDER_DB_TABLE_LOCK_READ;
2954 break;
2955 case TL_WRITE_LOW_PRIORITY:
2956 lock_type = SPIDER_DB_TABLE_LOCK_LOW_PRIORITY_WRITE;
2957 break;
2958 case TL_WRITE:
2959 lock_type = SPIDER_DB_TABLE_LOCK_WRITE;
2960 break;
2961 default:
2962 // no lock
2963 DBUG_PRINT("info",("spider lock_type=%d", tmp_spider->lock_type));
2964 DBUG_RETURN(0);
2965 }
2966 conn_link_idx = tmp_spider->conn_link_idx[tmp_link_idx];
2967 spider_mysql_share *db_share = (spider_mysql_share *)
2968 tmp_spider->share->dbton_share[conn->dbton_id];
2969 if (&db_share->db_names_str[conn_link_idx])
2970 {
2971 db_name = db_share->db_names_str[conn_link_idx].ptr();
2972 db_name_length = db_share->db_names_str[conn_link_idx].length();
2973 db_name_charset = tmp_spider->share->access_charset;
2974 } else {
2975 db_name = tmp_spider->share->tgt_dbs[conn_link_idx];
2976 db_name_length = tmp_spider->share->tgt_dbs_lengths[conn_link_idx];
2977 db_name_charset = system_charset_info;
2978 }
2979 if (&db_share->table_names_str[conn_link_idx])
2980 {
2981 table_name = db_share->table_names_str[conn_link_idx].ptr();
2982 table_name_length = db_share->table_names_str[conn_link_idx].length();
2983 table_name_charset = tmp_spider->share->access_charset;
2984 } else {
2985 table_name = tmp_spider->share->tgt_table_names[conn_link_idx];
2986 table_name_length =
2987 tmp_spider->share->tgt_table_names_lengths[conn_link_idx];
2988 table_name_charset = system_charset_info;
2989 }
2990 if ((error_num = spider_db_mysql_utility.
2991 append_lock_table_body(
2992 str,
2993 db_name,
2994 db_name_length,
2995 db_name_charset,
2996 table_name,
2997 table_name_length,
2998 table_name_charset,
2999 lock_type
3000 )
3001 )) {
3002 my_hash_reset(&lock_table_hash);
3003 DBUG_RETURN(error_num);
3004 }
3005#ifdef HASH_UPDATE_WITH_HASH_VALUE
3006 my_hash_delete_with_hash_value(&lock_table_hash,
3007 tmp_link_for_hash->db_table_str_hash_value, (uchar*) tmp_link_for_hash);
3008#else
3009 my_hash_delete(&lock_table_hash, (uchar*) tmp_link_for_hash);
3010#endif
3011 }
3012 if ((error_num = spider_db_mysql_utility.append_lock_table_tail(str)))
3013 {
3014 DBUG_RETURN(error_num);
3015 }
3016 DBUG_RETURN(0);
3017}
3018
3019int spider_db_mysql::append_unlock_tables(
3020 spider_string *str
3021) {
3022 int error_num;
3023 DBUG_ENTER("spider_db_mysql::append_unlock_tables");
3024 DBUG_PRINT("info",("spider this=%p", this));
3025 if ((error_num = spider_db_mysql_utility.append_unlock_table(str)))
3026 {
3027 DBUG_RETURN(error_num);
3028 }
3029 DBUG_RETURN(0);
3030}
3031
3032uint spider_db_mysql::get_lock_table_hash_count()
3033{
3034 DBUG_ENTER("spider_db_mysql::get_lock_table_hash_count");
3035 DBUG_PRINT("info",("spider this=%p", this));
3036 DBUG_RETURN(lock_table_hash.records);
3037}
3038
3039void spider_db_mysql::reset_lock_table_hash()
3040{
3041 DBUG_ENTER("spider_db_mysql::reset_lock_table_hash");
3042 DBUG_PRINT("info",("spider this=%p", this));
3043 my_hash_reset(&lock_table_hash);
3044 DBUG_VOID_RETURN;
3045}
3046
3047uint spider_db_mysql::get_opened_handler_count()
3048{
3049 DBUG_ENTER("spider_db_mysql::get_opened_handler_count");
3050 DBUG_PRINT("info",("spider this=%p", this));
3051 DBUG_RETURN(handler_open_array.elements);
3052}
3053
3054void spider_db_mysql::reset_opened_handler()
3055{
3056 ha_spider *tmp_spider;
3057 int tmp_link_idx;
3058 SPIDER_LINK_FOR_HASH **tmp_link_for_hash;
3059 DBUG_ENTER("spider_db_mysql::reset_opened_handler");
3060 DBUG_PRINT("info",("spider this=%p", this));
3061 while ((tmp_link_for_hash =
3062 (SPIDER_LINK_FOR_HASH **) pop_dynamic(&handler_open_array)))
3063 {
3064 tmp_spider = (*tmp_link_for_hash)->spider;
3065 tmp_link_idx = (*tmp_link_for_hash)->link_idx;
3066 tmp_spider->clear_handler_opened(tmp_link_idx, conn->conn_kind);
3067 }
3068 DBUG_VOID_RETURN;
3069}
3070
3071void spider_db_mysql::set_dup_key_idx(
3072 ha_spider *spider,
3073 int link_idx
3074) {
3075 TABLE *table = spider->get_table();
3076 uint roop_count, pk_idx = table->s->primary_key;
3077 int key_name_length;
3078 int max_length = 0;
3079 const char *key_name;
3080 DBUG_ENTER("spider_db_mysql::set_dup_key_idx");
3081 DBUG_PRINT("info",("spider this=%p", this));
3082 DBUG_PRINT("info",("spider error_str=%s", conn->error_str));
3083 for (roop_count = 0; roop_count < table->s->keys; roop_count++)
3084 {
3085 if (roop_count == pk_idx)
3086 {
3087 DBUG_PRINT("info",("spider pk_idx=%u", roop_count));
3088 int all_link_idx = spider->conn_link_idx[link_idx];
3089 key_name = spider->share->tgt_pk_names[all_link_idx];
3090 key_name_length = spider->share->tgt_pk_names_lengths[all_link_idx];
3091 } else {
3092 key_name = table->s->key_info[roop_count].name.str;
3093 key_name_length = table->s->key_info[roop_count].name.length;
3094 }
3095 DBUG_PRINT("info",("spider key_name=%s", key_name));
3096 if (
3097 max_length < key_name_length &&
3098 conn->error_length - 1 >= key_name_length &&
3099 *(conn->error_str + conn->error_length - 2 -
3100 key_name_length) == '\'' &&
3101 !strncasecmp(conn->error_str +
3102 conn->error_length - 1 - key_name_length,
3103 key_name, key_name_length)
3104 ) {
3105 max_length = key_name_length;
3106 spider->dup_key_idx = roop_count;
3107 }
3108 }
3109 if (max_length == 0)
3110 spider->dup_key_idx = (uint) -1;
3111 DBUG_PRINT("info",("spider dup_key_idx=%d", spider->dup_key_idx));
3112 DBUG_VOID_RETURN;
3113}
3114
3115bool spider_db_mysql::cmp_request_key_to_snd(
3116 st_spider_db_request_key *request_key
3117) {
3118 DBUG_ENTER("spider_db_mysql::cmp_request_key_to_snd");
3119 DBUG_PRINT("info",("spider this=%p", this));
3120 DBUG_RETURN(TRUE);
3121}
3122
3123spider_db_mysql_util::spider_db_mysql_util() : spider_db_util()
3124{
3125 DBUG_ENTER("spider_db_mysql_util::spider_db_mysql_util");
3126 DBUG_PRINT("info",("spider this=%p", this));
3127 DBUG_VOID_RETURN;
3128}
3129
3130spider_db_mysql_util::~spider_db_mysql_util()
3131{
3132 DBUG_ENTER("spider_db_mysql_util::~spider_db_mysql_util");
3133 DBUG_PRINT("info",("spider this=%p", this));
3134 DBUG_VOID_RETURN;
3135}
3136
3137int spider_db_mysql_util::append_name(
3138 spider_string *str,
3139 const char *name,
3140 uint name_length
3141) {
3142 DBUG_ENTER("spider_db_mysql_util::append_name");
3143 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
3144 str->q_append(name, name_length);
3145 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
3146 DBUG_RETURN(0);
3147}
3148
3149int spider_db_mysql_util::append_name_with_charset(
3150 spider_string *str,
3151 const char *name,
3152 uint name_length,
3153 CHARSET_INFO *name_charset
3154) {
3155 DBUG_ENTER("spider_db_mysql_util::append_name_with_charset");
3156 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN * 2 + name_length * 2))
3157 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3158 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
3159 str->append(name, name_length, name_charset);
3160 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN))
3161 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3162 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
3163 DBUG_RETURN(0);
3164}
3165
3166bool spider_db_mysql_util::is_name_quote(
3167 const char head_code
3168) {
3169 DBUG_ENTER("spider_db_mysql_util::is_name_quote");
3170 DBUG_RETURN(head_code == *name_quote_str);
3171}
3172
3173int spider_db_mysql_util::append_escaped_name_quote(
3174 spider_string *str
3175) {
3176 DBUG_ENTER("spider_db_mysql_util::append_escaped_name_quote");
3177 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN * 2))
3178 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3179 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
3180 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
3181 DBUG_RETURN(0);
3182}
3183
3184int spider_db_mysql_util::append_column_value(
3185 ha_spider *spider,
3186 spider_string *str,
3187 Field *field,
3188 const uchar *new_ptr,
3189 CHARSET_INFO *access_charset
3190) {
3191 char buf[MAX_FIELD_WIDTH];
3192 spider_string tmp_str(buf, MAX_FIELD_WIDTH, &my_charset_bin);
3193 String *ptr;
3194 uint length;
3195 DBUG_ENTER("spider_db_mysql_util::append_column_value");
3196 tmp_str.init_calc_mem(113);
3197
3198 if (new_ptr)
3199 {
3200 if (
3201 field->type() == MYSQL_TYPE_BLOB ||
3202 field->real_type() == MYSQL_TYPE_VARCHAR
3203 ) {
3204 length = uint2korr(new_ptr);
3205 tmp_str.set_quick((char *) new_ptr + HA_KEY_BLOB_LENGTH, length,
3206 &my_charset_bin);
3207 ptr = tmp_str.get_str();
3208 } else if (field->type() == MYSQL_TYPE_GEOMETRY)
3209 {
3210/*
3211 uint mlength = SIZEOF_STORED_DOUBLE, lcnt;
3212 uchar *dest = (uchar *) buf;
3213 const uchar *source;
3214 for (lcnt = 0; lcnt < 4; lcnt++)
3215 {
3216 mlength = SIZEOF_STORED_DOUBLE;
3217 source = new_ptr + mlength + SIZEOF_STORED_DOUBLE * lcnt;
3218 while (mlength--)
3219 *dest++ = *--source;
3220 }
3221 tmp_str.length(SIZEOF_STORED_DOUBLE * lcnt);
3222*/
3223#ifndef DBUG_OFF
3224 double xmin, xmax, ymin, ymax;
3225/*
3226 float8store(buf,xmin);
3227 float8store(buf+8,xmax);
3228 float8store(buf+16,ymin);
3229 float8store(buf+24,ymax);
3230 memcpy(&xmin,new_ptr,sizeof(xmin));
3231 memcpy(&xmax,new_ptr + 8,sizeof(xmax));
3232 memcpy(&ymin,new_ptr + 16,sizeof(ymin));
3233 memcpy(&ymax,new_ptr + 24,sizeof(ymax));
3234 float8get(xmin, buf);
3235 float8get(xmax, buf + 8);
3236 float8get(ymin, buf + 16);
3237 float8get(ymax, buf + 24);
3238 DBUG_PRINT("info", ("spider geo is %f %f %f %f",
3239 xmin, xmax, ymin, ymax));
3240 DBUG_PRINT("info", ("spider geo is %.14g %.14g %.14g %.14g",
3241 xmin, xmax, ymin, ymax));
3242*/
3243 float8get(xmin, new_ptr);
3244 float8get(xmax, new_ptr + 8);
3245 float8get(ymin, new_ptr + 16);
3246 float8get(ymax, new_ptr + 24);
3247 DBUG_PRINT("info", ("spider geo is %f %f %f %f",
3248 xmin, xmax, ymin, ymax));
3249/*
3250 float8get(xmin, new_ptr + SIZEOF_STORED_DOUBLE * 4);
3251 float8get(xmax, new_ptr + SIZEOF_STORED_DOUBLE * 5);
3252 float8get(ymin, new_ptr + SIZEOF_STORED_DOUBLE * 6);
3253 float8get(ymax, new_ptr + SIZEOF_STORED_DOUBLE * 7);
3254 DBUG_PRINT("info", ("spider geo is %f %f %f %f",
3255 xmin, xmax, ymin, ymax));
3256 float8get(xmin, new_ptr + SIZEOF_STORED_DOUBLE * 8);
3257 float8get(xmax, new_ptr + SIZEOF_STORED_DOUBLE * 9);
3258 float8get(ymin, new_ptr + SIZEOF_STORED_DOUBLE * 10);
3259 float8get(ymax, new_ptr + SIZEOF_STORED_DOUBLE * 11);
3260 DBUG_PRINT("info", ("spider geo is %f %f %f %f",
3261 xmin, xmax, ymin, ymax));
3262 float8get(xmin, new_ptr + SIZEOF_STORED_DOUBLE * 12);
3263 float8get(xmax, new_ptr + SIZEOF_STORED_DOUBLE * 13);
3264 float8get(ymin, new_ptr + SIZEOF_STORED_DOUBLE * 14);
3265 float8get(ymax, new_ptr + SIZEOF_STORED_DOUBLE * 15);
3266 DBUG_PRINT("info", ("spider geo is %f %f %f %f",
3267 xmin, xmax, ymin, ymax));
3268*/
3269#endif
3270/*
3271 tmp_str.set_quick((char *) new_ptr, SIZEOF_STORED_DOUBLE * 4,
3272 &my_charset_bin);
3273*/
3274 tmp_str.length(0);
3275 tmp_str.q_append((char *) SPIDER_SQL_LINESTRING_HEAD_STR,
3276 SPIDER_SQL_LINESTRING_HEAD_LEN);
3277 tmp_str.q_append((char *) new_ptr, SIZEOF_STORED_DOUBLE);
3278 tmp_str.q_append((char *) new_ptr + SIZEOF_STORED_DOUBLE * 2,
3279 SIZEOF_STORED_DOUBLE);
3280 tmp_str.q_append((char *) new_ptr + SIZEOF_STORED_DOUBLE,
3281 SIZEOF_STORED_DOUBLE);
3282 tmp_str.q_append((char *) new_ptr + SIZEOF_STORED_DOUBLE * 3,
3283 SIZEOF_STORED_DOUBLE);
3284 ptr = tmp_str.get_str();
3285 } else {
3286 ptr = field->val_str(tmp_str.get_str(), new_ptr);
3287 tmp_str.mem_calc();
3288 }
3289 } else {
3290 ptr = field->val_str(tmp_str.get_str());
3291 tmp_str.mem_calc();
3292 }
3293 DBUG_PRINT("info", ("spider field->type() is %d", field->type()));
3294 DBUG_PRINT("info", ("spider ptr->length() is %d", ptr->length()));
3295/*
3296 if (
3297 field->type() == MYSQL_TYPE_BIT ||
3298 (field->type() >= MYSQL_TYPE_TINY_BLOB &&
3299 field->type() <= MYSQL_TYPE_BLOB)
3300 ) {
3301 uchar *hex_ptr = (uchar *) ptr->ptr(), *end_ptr;
3302 char *str_ptr;
3303 DBUG_PRINT("info", ("spider HEX"));
3304 if (str->reserve(SPIDER_SQL_HEX_LEN + ptr->length() * 2))
3305 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3306 str->q_append(SPIDER_SQL_HEX_STR, SPIDER_SQL_HEX_LEN);
3307 str_ptr = (char *) str->ptr() + str->length();
3308 for (end_ptr = hex_ptr + ptr->length(); hex_ptr < end_ptr; hex_ptr++)
3309 {
3310 *str_ptr++ = spider_dig_upper[(*hex_ptr) >> 4];
3311 *str_ptr++ = spider_dig_upper[(*hex_ptr) & 0x0F];
3312 }
3313 str->length(str->length() + ptr->length() * 2);
3314 } else
3315*/
3316 if (field->result_type() == STRING_RESULT)
3317 {
3318 DBUG_PRINT("info", ("spider STRING_RESULT"));
3319 if (str->reserve(SPIDER_SQL_VALUE_QUOTE_LEN))
3320 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3321 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
3322 if (
3323 field->type() == MYSQL_TYPE_VARCHAR ||
3324 (field->type() >= MYSQL_TYPE_ENUM &&
3325 field->type() <= MYSQL_TYPE_GEOMETRY)
3326 ) {
3327 DBUG_PRINT("info", ("spider append_escaped"));
3328 char buf2[MAX_FIELD_WIDTH];
3329 spider_string tmp_str2(buf2, MAX_FIELD_WIDTH, access_charset);
3330 tmp_str2.init_calc_mem(114);
3331 tmp_str2.length(0);
3332 if (
3333 tmp_str2.append(ptr->ptr(), ptr->length(), field->charset()) ||
3334 str->reserve(tmp_str2.length() * 2) ||
3335 append_escaped_util(str, tmp_str2.get_str())
3336 )
3337 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3338 } else if (str->append(*ptr))
3339 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3340 if (str->reserve(SPIDER_SQL_VALUE_QUOTE_LEN))
3341 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3342 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
3343 } else if (field->str_needs_quotes())
3344 {
3345 if (str->reserve(SPIDER_SQL_VALUE_QUOTE_LEN * 2 + ptr->length() * 2 + 2))
3346 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3347 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
3348 append_escaped_util(str, ptr);
3349 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
3350 } else if (str->append(*ptr))
3351 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3352 DBUG_RETURN(0);
3353}
3354
3355int spider_db_mysql_util::append_from_with_alias(
3356 spider_string *str,
3357 const char **table_names,
3358 uint *table_name_lengths,
3359 const char **table_aliases,
3360 uint *table_alias_lengths,
3361 uint table_count,
3362 int *table_name_pos,
3363 bool over_write
3364) {
3365 uint roop_count, length = 0;
3366 DBUG_ENTER("spider_db_mysql_util::append_from_with_alias");
3367 DBUG_PRINT("info",("spider this=%p", this));
3368 if (!over_write)
3369 {
3370 for (roop_count = 0; roop_count < table_count; roop_count++)
3371 length += table_name_lengths[roop_count] + SPIDER_SQL_SPACE_LEN +
3372 table_alias_lengths[roop_count] + SPIDER_SQL_COMMA_LEN;
3373 if (str->reserve(SPIDER_SQL_FROM_LEN + length))
3374 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3375 str->q_append(SPIDER_SQL_FROM_STR, SPIDER_SQL_FROM_LEN);
3376 *table_name_pos = str->length();
3377 }
3378 for (roop_count = 0; roop_count < table_count; roop_count++)
3379 {
3380 str->q_append(table_names[roop_count], table_name_lengths[roop_count]);
3381 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
3382 str->q_append(table_aliases[roop_count], table_alias_lengths[roop_count]);
3383 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
3384 }
3385 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
3386 DBUG_RETURN(0);
3387}
3388
3389int spider_db_mysql_util::append_trx_isolation(
3390 spider_string *str,
3391 int trx_isolation
3392) {
3393 DBUG_ENTER("spider_db_mysql_util::append_trx_isolation");
3394 DBUG_PRINT("info",("spider this=%p", this));
3395 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN +
3396 SPIDER_SQL_ISO_READ_UNCOMMITTED_LEN))
3397 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3398 if (str->length())
3399 {
3400 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
3401 }
3402 switch (trx_isolation)
3403 {
3404 case ISO_READ_UNCOMMITTED:
3405 str->q_append(SPIDER_SQL_ISO_READ_UNCOMMITTED_STR,
3406 SPIDER_SQL_ISO_READ_UNCOMMITTED_LEN);
3407 break;
3408 case ISO_READ_COMMITTED:
3409 str->q_append(SPIDER_SQL_ISO_READ_COMMITTED_STR,
3410 SPIDER_SQL_ISO_READ_COMMITTED_LEN);
3411 break;
3412 case ISO_REPEATABLE_READ:
3413 str->q_append(SPIDER_SQL_ISO_REPEATABLE_READ_STR,
3414 SPIDER_SQL_ISO_REPEATABLE_READ_LEN);
3415 break;
3416 case ISO_SERIALIZABLE:
3417 str->q_append(SPIDER_SQL_ISO_SERIALIZABLE_STR,
3418 SPIDER_SQL_ISO_SERIALIZABLE_LEN);
3419 break;
3420 default:
3421 DBUG_RETURN(HA_ERR_UNSUPPORTED);
3422 }
3423 DBUG_RETURN(0);
3424}
3425
3426int spider_db_mysql_util::append_autocommit(
3427 spider_string *str,
3428 bool autocommit
3429) {
3430 DBUG_ENTER("spider_db_mysql_util::append_autocommit");
3431 DBUG_PRINT("info",("spider this=%p", this));
3432 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN + SPIDER_SQL_AUTOCOMMIT_OFF_LEN))
3433 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3434 if (str->length())
3435 {
3436 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
3437 }
3438 if (autocommit)
3439 {
3440 str->q_append(SPIDER_SQL_AUTOCOMMIT_ON_STR,
3441 SPIDER_SQL_AUTOCOMMIT_ON_LEN);
3442 } else {
3443 str->q_append(SPIDER_SQL_AUTOCOMMIT_OFF_STR,
3444 SPIDER_SQL_AUTOCOMMIT_OFF_LEN);
3445 }
3446 DBUG_RETURN(0);
3447}
3448
3449int spider_db_mysql_util::append_sql_log_off(
3450 spider_string *str,
3451 bool sql_log_off
3452) {
3453 DBUG_ENTER("spider_db_mysql_util::append_sql_log_off");
3454 DBUG_PRINT("info",("spider this=%p", this));
3455 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN + SPIDER_SQL_SQL_LOG_OFF_LEN))
3456 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3457 if (str->length())
3458 {
3459 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
3460 }
3461 if (sql_log_off)
3462 {
3463 str->q_append(SPIDER_SQL_SQL_LOG_ON_STR, SPIDER_SQL_SQL_LOG_ON_LEN);
3464 } else {
3465 str->q_append(SPIDER_SQL_SQL_LOG_OFF_STR, SPIDER_SQL_SQL_LOG_OFF_LEN);
3466 }
3467 DBUG_RETURN(0);
3468}
3469
3470int spider_db_mysql_util::append_time_zone(
3471 spider_string *str,
3472 Time_zone *time_zone
3473) {
3474 const String *tz_str = time_zone->get_name();
3475 DBUG_ENTER("spider_db_mysql_util::append_time_zone");
3476 DBUG_PRINT("info",("spider this=%p", this));
3477 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN + SPIDER_SQL_TIME_ZONE_LEN +
3478 tz_str->length() + SPIDER_SQL_VALUE_QUOTE_LEN))
3479 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3480 if (str->length())
3481 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
3482 str->q_append(SPIDER_SQL_TIME_ZONE_STR, SPIDER_SQL_TIME_ZONE_LEN);
3483 str->q_append(tz_str->ptr(), tz_str->length());
3484 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
3485 DBUG_RETURN(0);
3486}
3487
3488int spider_db_mysql_util::append_start_transaction(
3489 spider_string *str
3490) {
3491 DBUG_ENTER("spider_db_mysql_util::append_start_transaction");
3492 DBUG_PRINT("info",("spider this=%p", this));
3493 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN +
3494 SPIDER_SQL_START_TRANSACTION_LEN))
3495 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3496 if (str->length())
3497 {
3498 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
3499 }
3500 str->q_append(SPIDER_SQL_START_TRANSACTION_STR,
3501 SPIDER_SQL_START_TRANSACTION_LEN);
3502 DBUG_RETURN(0);
3503}
3504
3505int spider_db_mysql_util::append_xa_start(
3506 spider_string *str,
3507 XID *xid
3508) {
3509 DBUG_ENTER("spider_db_mysql_util::append_xa_start");
3510 DBUG_PRINT("info",("spider this=%p", this));
3511 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN +
3512 SPIDER_SQL_XA_START_LEN + XIDDATASIZE + sizeof(long) + 9))
3513 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3514 if (str->length())
3515 {
3516 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
3517 }
3518 str->q_append(SPIDER_SQL_XA_START_STR, SPIDER_SQL_XA_START_LEN);
3519 spider_db_append_xid_str(str, xid);
3520 DBUG_RETURN(0);
3521}
3522
3523int spider_db_mysql_util::append_lock_table_head(
3524 spider_string *str
3525) {
3526 DBUG_ENTER("spider_db_mysql_util::append_lock_table_head");
3527 DBUG_PRINT("info",("spider this=%p", this));
3528 if (str->reserve(SPIDER_SQL_LOCK_TABLE_LEN))
3529 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3530 str->q_append(SPIDER_SQL_LOCK_TABLE_STR, SPIDER_SQL_LOCK_TABLE_LEN);
3531 DBUG_RETURN(0);
3532}
3533
3534int spider_db_mysql_util::append_lock_table_body(
3535 spider_string *str,
3536 const char *db_name,
3537 uint db_name_length,
3538 CHARSET_INFO *db_name_charset,
3539 const char *table_name,
3540 uint table_name_length,
3541 CHARSET_INFO *table_name_charset,
3542 int lock_type
3543) {
3544 DBUG_ENTER("spider_db_mysql_util::append_lock_table_body");
3545 DBUG_PRINT("info",("spider this=%p", this));
3546 if (str->reserve(SPIDER_SQL_NAME_QUOTE_LEN))
3547 {
3548 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3549 }
3550 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
3551 if (
3552 str->append(db_name, db_name_length, db_name_charset) ||
3553 str->reserve((SPIDER_SQL_NAME_QUOTE_LEN) * 2 + SPIDER_SQL_DOT_LEN)
3554 ) {
3555 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3556 }
3557 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
3558 str->q_append(SPIDER_SQL_DOT_STR, SPIDER_SQL_DOT_LEN);
3559 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
3560 if (
3561 str->append(table_name, table_name_length, table_name_charset) ||
3562 str->reserve(SPIDER_SQL_NAME_QUOTE_LEN +
3563 spider_db_table_lock_len[lock_type])
3564 ) {
3565 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3566 }
3567 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
3568 str->q_append(spider_db_table_lock_str[lock_type],
3569 spider_db_table_lock_len[lock_type]);
3570 DBUG_RETURN(0);
3571}
3572
3573int spider_db_mysql_util::append_lock_table_tail(
3574 spider_string *str
3575) {
3576 DBUG_ENTER("spider_db_mysql_util::append_lock_table_tail");
3577 DBUG_PRINT("info",("spider this=%p", this));
3578 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
3579 DBUG_RETURN(0);
3580}
3581
3582int spider_db_mysql_util::append_unlock_table(
3583 spider_string *str
3584) {
3585 DBUG_ENTER("spider_db_mysql_util::append_unlock_table");
3586 DBUG_PRINT("info",("spider this=%p", this));
3587 if (str->reserve(SPIDER_SQL_UNLOCK_TABLE_LEN))
3588 {
3589 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3590 }
3591 str->q_append(SPIDER_SQL_UNLOCK_TABLE_STR, SPIDER_SQL_UNLOCK_TABLE_LEN);
3592 DBUG_RETURN(0);
3593}
3594
3595int spider_db_mysql_util::open_item_func(
3596 Item_func *item_func,
3597 ha_spider *spider,
3598 spider_string *str,
3599 const char *alias,
3600 uint alias_length,
3601 bool use_fields,
3602 spider_fields *fields
3603) {
3604 uint dbton_id = spider_dbton_mysql.dbton_id;
3605 int error_num;
3606 Item *item, **item_list = item_func->arguments();
3607 uint roop_count, item_count = item_func->argument_count(), start_item = 0;
3608 const char *func_name = SPIDER_SQL_NULL_CHAR_STR,
3609 *separete_str = SPIDER_SQL_NULL_CHAR_STR,
3610 *last_str = SPIDER_SQL_NULL_CHAR_STR;
3611 int func_name_length = SPIDER_SQL_NULL_CHAR_LEN,
3612 separete_str_length = SPIDER_SQL_NULL_CHAR_LEN,
3613 last_str_length = SPIDER_SQL_NULL_CHAR_LEN;
3614 int use_pushdown_udf;
3615 bool merge_func = FALSE;
3616 DBUG_ENTER("spider_db_mysql_util::open_item_func");
3617 if (str)
3618 {
3619 if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
3620 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3621 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
3622 }
3623 DBUG_PRINT("info",("spider functype = %d", item_func->functype()));
3624 switch (item_func->functype())
3625 {
3626 case Item_func::ISNULL_FUNC:
3627 last_str = SPIDER_SQL_IS_NULL_STR;
3628 last_str_length = SPIDER_SQL_IS_NULL_LEN;
3629 break;
3630 case Item_func::ISNOTNULL_FUNC:
3631 last_str = SPIDER_SQL_IS_NOT_NULL_STR;
3632 last_str_length = SPIDER_SQL_IS_NOT_NULL_LEN;
3633 break;
3634 case Item_func::UNKNOWN_FUNC:
3635 func_name = (char*) item_func->func_name();
3636 func_name_length = strlen(func_name);
3637 DBUG_PRINT("info",("spider func_name = %s", func_name));
3638 DBUG_PRINT("info",("spider func_name_length = %d", func_name_length));
3639 if (func_name_length == 1 &&
3640 (
3641 !strncasecmp("+", func_name, func_name_length) ||
3642 !strncasecmp("-", func_name, func_name_length) ||
3643 !strncasecmp("*", func_name, func_name_length) ||
3644 !strncasecmp("/", func_name, func_name_length) ||
3645 !strncasecmp("%", func_name, func_name_length) ||
3646 !strncasecmp("&", func_name, func_name_length) ||
3647 !strncasecmp("|", func_name, func_name_length) ||
3648 !strncasecmp("^", func_name, func_name_length)
3649 )
3650 ) {
3651 /* no action */
3652 break;
3653 } else if (func_name_length == 2 &&
3654 (
3655 !strncasecmp("<<", func_name, func_name_length) ||
3656 !strncasecmp(">>", func_name, func_name_length)
3657 )
3658 ) {
3659 /* no action */
3660 break;
3661 } else if (func_name_length == 3 &&
3662 !strncasecmp("div", func_name, func_name_length)
3663 ) {
3664 /* no action */
3665 break;
3666 } else if (func_name_length == 4)
3667 {
3668 if (
3669 !strncasecmp("rand", func_name, func_name_length) &&
3670#ifdef SPIDER_Item_args_arg_count_IS_PROTECTED
3671 !item_func->argument_count()
3672#else
3673 !item_func->arg_count
3674#endif
3675 ) {
3676 if (str)
3677 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
3678 DBUG_RETURN(spider_db_open_item_int(item_func, spider, str,
3679 alias, alias_length, dbton_id, use_fields, fields));
3680 } else if (
3681 !strncasecmp("case", func_name, func_name_length)
3682 ) {
3683#ifdef ITEM_FUNC_CASE_PARAMS_ARE_PUBLIC
3684 Item_func_case *item_func_case = (Item_func_case *) item_func;
3685 if (str)
3686 {
3687 if (str->reserve(SPIDER_SQL_CASE_LEN))
3688 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3689 str->q_append(SPIDER_SQL_CASE_STR, SPIDER_SQL_CASE_LEN);
3690 }
3691 if (item_func_case->first_expr_num != -1)
3692 {
3693 if ((error_num = spider_db_print_item_type(
3694 item_list[item_func_case->first_expr_num], spider, str,
3695 alias, alias_length, dbton_id, use_fields, fields)))
3696 DBUG_RETURN(error_num);
3697 }
3698 for (roop_count = 0; roop_count < item_func_case->ncases;
3699 roop_count += 2)
3700 {
3701 if (str)
3702 {
3703 if (str->reserve(SPIDER_SQL_WHEN_LEN))
3704 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3705 str->q_append(SPIDER_SQL_WHEN_STR, SPIDER_SQL_WHEN_LEN);
3706 }
3707 if ((error_num = spider_db_print_item_type(
3708 item_list[roop_count], spider, str,
3709 alias, alias_length, dbton_id, use_fields, fields)))
3710 DBUG_RETURN(error_num);
3711 if (str)
3712 {
3713 if (str->reserve(SPIDER_SQL_THEN_LEN))
3714 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3715 str->q_append(SPIDER_SQL_THEN_STR, SPIDER_SQL_THEN_LEN);
3716 }
3717 if ((error_num = spider_db_print_item_type(
3718 item_list[roop_count + 1], spider, str,
3719 alias, alias_length, dbton_id, use_fields, fields)))
3720 DBUG_RETURN(error_num);
3721 }
3722 if (item_func_case->else_expr_num != -1)
3723 {
3724 if (str)
3725 {
3726 if (str->reserve(SPIDER_SQL_ELSE_LEN))
3727 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3728 str->q_append(SPIDER_SQL_ELSE_STR, SPIDER_SQL_ELSE_LEN);
3729 }
3730 if ((error_num = spider_db_print_item_type(
3731 item_list[item_func_case->else_expr_num], spider, str,
3732 alias, alias_length, dbton_id, use_fields, fields)))
3733 DBUG_RETURN(error_num);
3734 }
3735 if (str)
3736 {
3737 if (str->reserve(SPIDER_SQL_END_LEN + SPIDER_SQL_CLOSE_PAREN_LEN))
3738 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3739 str->q_append(SPIDER_SQL_END_STR, SPIDER_SQL_END_LEN);
3740 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
3741 SPIDER_SQL_CLOSE_PAREN_LEN);
3742 }
3743 DBUG_RETURN(0);
3744#else
3745 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
3746#endif
3747 }
3748 } else if (func_name_length == 6 &&
3749 !strncasecmp("istrue", func_name, func_name_length)
3750 ) {
3751 last_str = SPIDER_SQL_IS_TRUE_STR;
3752 last_str_length = SPIDER_SQL_IS_TRUE_LEN;
3753 break;
3754 } else if (func_name_length == 7)
3755 {
3756 if (!strncasecmp("isfalse", func_name, func_name_length))
3757 {
3758 last_str = SPIDER_SQL_IS_FALSE_STR;
3759 last_str_length = SPIDER_SQL_IS_FALSE_LEN;
3760 break;
3761 } else if (
3762 !strncasecmp("sysdate", func_name, func_name_length) ||
3763 !strncasecmp("curdate", func_name, func_name_length) ||
3764 !strncasecmp("curtime", func_name, func_name_length)
3765 ) {
3766 if (str)
3767 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
3768 DBUG_RETURN(spider_db_open_item_string(item_func, spider, str,
3769 alias, alias_length, dbton_id, use_fields, fields));
3770 } else if (
3771 !strncasecmp("convert", func_name, func_name_length)
3772 ) {
3773 if (str)
3774 {
3775 if (str->reserve(func_name_length * 2 + SPIDER_SQL_OPEN_PAREN_LEN))
3776 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3777 str->q_append(func_name, func_name_length);
3778 str->q_append(SPIDER_SQL_OPEN_PAREN_STR,
3779 SPIDER_SQL_OPEN_PAREN_LEN);
3780 last_str = SPIDER_SQL_CLOSE_PAREN_STR;
3781 last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
3782 }
3783 break;
3784 }
3785 } else if (func_name_length == 8 &&
3786 (
3787 !strncasecmp("utc_date", func_name, func_name_length) ||
3788 !strncasecmp("utc_time", func_name, func_name_length)
3789 )
3790 ) {
3791 if (str)
3792 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
3793 DBUG_RETURN(spider_db_open_item_string(item_func, spider, str,
3794 alias, alias_length, dbton_id, use_fields, fields));
3795 } else if (func_name_length == 9 &&
3796 !strncasecmp("isnottrue", func_name, func_name_length)
3797 ) {
3798 last_str = SPIDER_SQL_IS_NOT_TRUE_STR;
3799 last_str_length = SPIDER_SQL_IS_NOT_TRUE_LEN;
3800 break;
3801 } else if (func_name_length == 10)
3802 {
3803 if (!strncasecmp("isnotfalse", func_name, func_name_length))
3804 {
3805 last_str = SPIDER_SQL_IS_NOT_FALSE_STR;
3806 last_str_length = SPIDER_SQL_IS_NOT_FALSE_LEN;
3807 break;
3808 } else if (!strncasecmp("column_get", func_name, func_name_length))
3809 {
3810 if (str)
3811 {
3812 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
3813 if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN))
3814 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3815 str->q_append(func_name, func_name_length);
3816 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
3817 }
3818 func_name = SPIDER_SQL_COMMA_STR;
3819 func_name_length = SPIDER_SQL_COMMA_LEN;
3820 separete_str = SPIDER_SQL_COMMA_STR;
3821 separete_str_length = SPIDER_SQL_COMMA_LEN;
3822 break;
3823 }
3824 } else if (func_name_length == 12)
3825 {
3826 if (!strncasecmp("cast_as_date", func_name, func_name_length))
3827 {
3828 item = item_list[0];
3829 if (item->type() == Item::FUNC_ITEM)
3830 {
3831 DBUG_PRINT("info",("spider child is FUNC_ITEM"));
3832 Item_func *ifunc = (Item_func *) item;
3833 if (ifunc->functype() == Item_func::UNKNOWN_FUNC)
3834 {
3835 const char *child_func_name;
3836 int child_func_name_length;
3837 DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
3838 child_func_name = (char*) ifunc->func_name();
3839 child_func_name_length = strlen(child_func_name);
3840 DBUG_PRINT("info",("spider child func_name is %s", child_func_name));
3841 if (
3842 child_func_name_length == 10 &&
3843 !strncasecmp("column_get", child_func_name, child_func_name_length)
3844 ) {
3845 DBUG_PRINT("info",("spider this is merge func"));
3846 merge_func = TRUE;
3847 }
3848 }
3849 }
3850
3851 if (str)
3852 {
3853 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
3854 if (!merge_func)
3855 {
3856 if (str->reserve(SPIDER_SQL_CAST_LEN))
3857 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3858 str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
3859 }
3860 }
3861 last_str = SPIDER_SQL_AS_DATE_STR;
3862 last_str_length = SPIDER_SQL_AS_DATE_LEN;
3863 break;
3864 } else if (!strncasecmp("cast_as_time", func_name, func_name_length))
3865 {
3866 item = item_list[0];
3867 if (item->type() == Item::FUNC_ITEM)
3868 {
3869 DBUG_PRINT("info",("spider child is FUNC_ITEM"));
3870 Item_func *ifunc = (Item_func *) item;
3871 if (ifunc->functype() == Item_func::UNKNOWN_FUNC)
3872 {
3873 const char *child_func_name;
3874 int child_func_name_length;
3875 DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
3876 child_func_name = (char*) ifunc->func_name();
3877 child_func_name_length = strlen(child_func_name);
3878 DBUG_PRINT("info",("spider child func_name is %s", child_func_name));
3879 if (
3880 child_func_name_length == 10 &&
3881 !strncasecmp("column_get", child_func_name, child_func_name_length)
3882 ) {
3883 DBUG_PRINT("info",("spider this is merge func"));
3884 merge_func = TRUE;
3885 }
3886 }
3887 }
3888
3889 if (str)
3890 {
3891 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
3892 if (!merge_func)
3893 {
3894 if (str->reserve(SPIDER_SQL_CAST_LEN))
3895 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3896 str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
3897 }
3898 }
3899 last_str = SPIDER_SQL_AS_TIME_STR;
3900 last_str_length = SPIDER_SQL_AS_TIME_LEN;
3901 break;
3902 }
3903 } else if (func_name_length == 13)
3904 {
3905 if (!strncasecmp("utc_timestamp", func_name, func_name_length))
3906 {
3907 if (str)
3908 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
3909 DBUG_RETURN(spider_db_open_item_string(item_func, spider, str,
3910 alias, alias_length, dbton_id, use_fields, fields));
3911 } else if (!strncasecmp("timestampdiff", func_name, func_name_length))
3912 {
3913#ifdef ITEM_FUNC_TIMESTAMPDIFF_ARE_PUBLIC
3914 Item_func_timestamp_diff *item_func_timestamp_diff =
3915 (Item_func_timestamp_diff *) item_func;
3916 if (str)
3917 {
3918 const char *interval_str;
3919 uint interval_len;
3920 switch (item_func_timestamp_diff->int_type)
3921 {
3922 case INTERVAL_YEAR:
3923 interval_str = SPIDER_SQL_YEAR_STR;
3924 interval_len = SPIDER_SQL_YEAR_LEN;
3925 break;
3926 case INTERVAL_QUARTER:
3927 interval_str = SPIDER_SQL_QUARTER_STR;
3928 interval_len = SPIDER_SQL_QUARTER_LEN;
3929 break;
3930 case INTERVAL_MONTH:
3931 interval_str = SPIDER_SQL_MONTH_STR;
3932 interval_len = SPIDER_SQL_MONTH_LEN;
3933 break;
3934 case INTERVAL_WEEK:
3935 interval_str = SPIDER_SQL_WEEK_STR;
3936 interval_len = SPIDER_SQL_WEEK_LEN;
3937 break;
3938 case INTERVAL_DAY:
3939 interval_str = SPIDER_SQL_DAY_STR;
3940 interval_len = SPIDER_SQL_DAY_LEN;
3941 break;
3942 case INTERVAL_HOUR:
3943 interval_str = SPIDER_SQL_HOUR_STR;
3944 interval_len = SPIDER_SQL_HOUR_LEN;
3945 break;
3946 case INTERVAL_MINUTE:
3947 interval_str = SPIDER_SQL_MINUTE_STR;
3948 interval_len = SPIDER_SQL_MINUTE_LEN;
3949 break;
3950 case INTERVAL_SECOND:
3951 interval_str = SPIDER_SQL_SECOND_STR;
3952 interval_len = SPIDER_SQL_SECOND_LEN;
3953 break;
3954 case INTERVAL_MICROSECOND:
3955 interval_str = SPIDER_SQL_MICROSECOND_STR;
3956 interval_len = SPIDER_SQL_MICROSECOND_LEN;
3957 break;
3958 default:
3959 interval_str = "";
3960 interval_len = 0;
3961 break;
3962 }
3963 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
3964 if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN +
3965 interval_len + SPIDER_SQL_COMMA_LEN))
3966 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3967 str->q_append(func_name, func_name_length);
3968 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
3969 str->q_append(interval_str, interval_len);
3970 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
3971 }
3972 if ((error_num = spider_db_print_item_type(item_list[0], spider,
3973 str, alias, alias_length, dbton_id, use_fields, fields)))
3974 DBUG_RETURN(error_num);
3975 if (str)
3976 {
3977 if (str->reserve(SPIDER_SQL_COMMA_LEN))
3978 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3979 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
3980 }
3981 if ((error_num = spider_db_print_item_type(item_list[1], spider,
3982 str, alias, alias_length, dbton_id, use_fields, fields)))
3983 DBUG_RETURN(error_num);
3984 if (str)
3985 {
3986 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN))
3987 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3988 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
3989 SPIDER_SQL_CLOSE_PAREN_LEN);
3990 }
3991 DBUG_RETURN(0);
3992#else
3993 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
3994#endif
3995 }
3996 } else if (func_name_length == 14)
3997 {
3998 if (!strncasecmp("cast_as_binary", func_name, func_name_length))
3999 {
4000 item = item_list[0];
4001 if (item->type() == Item::FUNC_ITEM)
4002 {
4003 DBUG_PRINT("info",("spider child is FUNC_ITEM"));
4004 Item_func *ifunc = (Item_func *) item;
4005 if (ifunc->functype() == Item_func::UNKNOWN_FUNC)
4006 {
4007 const char *child_func_name;
4008 int child_func_name_length;
4009 DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
4010 child_func_name = (char*) ifunc->func_name();
4011 child_func_name_length = strlen(child_func_name);
4012 DBUG_PRINT("info",("spider child func_name is %s", child_func_name));
4013 if (
4014 child_func_name_length == 10 &&
4015 !strncasecmp("column_get", child_func_name, child_func_name_length)
4016 ) {
4017 DBUG_PRINT("info",("spider this is merge func"));
4018 merge_func = TRUE;
4019 }
4020 }
4021 }
4022
4023 if (str)
4024 {
4025 char tmp_buf[MAX_FIELD_WIDTH], *tmp_ptr, *tmp_ptr2;
4026 spider_string tmp_str(tmp_buf, MAX_FIELD_WIDTH, str->charset());
4027 tmp_str.init_calc_mem(123);
4028 tmp_str.length(0);
4029 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4030 if (!merge_func)
4031 {
4032 if (str->reserve(SPIDER_SQL_CAST_LEN))
4033 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4034 str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
4035 }
4036#if MYSQL_VERSION_ID < 50500
4037 item_func->print(tmp_str.get_str(), QT_IS);
4038#else
4039 item_func->print(tmp_str.get_str(), QT_TO_SYSTEM_CHARSET);
4040#endif
4041 tmp_str.mem_calc();
4042 if (tmp_str.reserve(1))
4043 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4044 tmp_ptr = tmp_str.c_ptr_quick();
4045 DBUG_PRINT("info",("spider tmp_ptr = %s", tmp_ptr));
4046 while ((tmp_ptr2 = strstr(tmp_ptr, SPIDER_SQL_AS_BINARY_STR)))
4047 tmp_ptr = tmp_ptr2 + 1;
4048 last_str = tmp_ptr - 1;
4049 last_str_length = strlen(last_str) - SPIDER_SQL_CLOSE_PAREN_LEN;
4050 }
4051 break;
4052 } else if (!strncasecmp("cast_as_signed", func_name, func_name_length))
4053 {
4054 item = item_list[0];
4055 if (item->type() == Item::FUNC_ITEM)
4056 {
4057 DBUG_PRINT("info",("spider child is FUNC_ITEM"));
4058 Item_func *ifunc = (Item_func *) item;
4059 if (ifunc->functype() == Item_func::UNKNOWN_FUNC)
4060 {
4061 const char *child_func_name;
4062 int child_func_name_length;
4063 DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
4064 child_func_name = (char*) ifunc->func_name();
4065 child_func_name_length = strlen(child_func_name);
4066 DBUG_PRINT("info",("spider child func_name is %s", child_func_name));
4067 if (
4068 child_func_name_length == 10 &&
4069 !strncasecmp("column_get", child_func_name, child_func_name_length)
4070 ) {
4071 DBUG_PRINT("info",("spider this is merge func"));
4072 merge_func = TRUE;
4073 }
4074 }
4075 }
4076
4077 if (str)
4078 {
4079 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4080 if (!merge_func)
4081 {
4082 if (str->reserve(SPIDER_SQL_CAST_LEN))
4083 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4084 str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
4085 }
4086 }
4087 last_str = SPIDER_SQL_AS_SIGNED_STR;
4088 last_str_length = SPIDER_SQL_AS_SIGNED_LEN;
4089 break;
4090 }
4091 } else if (func_name_length == 16)
4092 {
4093 if (!strncasecmp("cast_as_unsigned", func_name, func_name_length))
4094 {
4095 item = item_list[0];
4096 if (item->type() == Item::FUNC_ITEM)
4097 {
4098 DBUG_PRINT("info",("spider child is FUNC_ITEM"));
4099 Item_func *ifunc = (Item_func *) item;
4100 if (ifunc->functype() == Item_func::UNKNOWN_FUNC)
4101 {
4102 const char *child_func_name;
4103 int child_func_name_length;
4104 DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
4105 child_func_name = (char*) ifunc->func_name();
4106 child_func_name_length = strlen(child_func_name);
4107 DBUG_PRINT("info",("spider child func_name is %s", child_func_name));
4108 if (
4109 child_func_name_length == 10 &&
4110 !strncasecmp("column_get", child_func_name, child_func_name_length)
4111 ) {
4112 DBUG_PRINT("info",("spider this is merge func"));
4113 merge_func = TRUE;
4114 }
4115 }
4116 }
4117
4118 if (str)
4119 {
4120 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4121 if (!merge_func)
4122 {
4123 if (str->reserve(SPIDER_SQL_CAST_LEN))
4124 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4125 str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
4126 }
4127 }
4128 last_str = SPIDER_SQL_AS_UNSIGNED_STR;
4129 last_str_length = SPIDER_SQL_AS_UNSIGNED_LEN;
4130 break;
4131 } else if (!strncasecmp("decimal_typecast", func_name,
4132 func_name_length))
4133 {
4134 item = item_list[0];
4135 if (item->type() == Item::FUNC_ITEM)
4136 {
4137 DBUG_PRINT("info",("spider child is FUNC_ITEM"));
4138 Item_func *ifunc = (Item_func *) item;
4139 if (ifunc->functype() == Item_func::UNKNOWN_FUNC)
4140 {
4141 const char *child_func_name;
4142 int child_func_name_length;
4143 DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
4144 child_func_name = (char*) ifunc->func_name();
4145 child_func_name_length = strlen(child_func_name);
4146 DBUG_PRINT("info",("spider child func_name is %s", child_func_name));
4147 if (
4148 child_func_name_length == 10 &&
4149 !strncasecmp("column_get", child_func_name, child_func_name_length)
4150 ) {
4151 DBUG_PRINT("info",("spider this is merge func"));
4152 merge_func = TRUE;
4153 }
4154 }
4155 }
4156
4157 if (str)
4158 {
4159 char tmp_buf[MAX_FIELD_WIDTH], *tmp_ptr, *tmp_ptr2;
4160 spider_string tmp_str(tmp_buf, MAX_FIELD_WIDTH, str->charset());
4161 tmp_str.init_calc_mem(124);
4162 tmp_str.length(0);
4163 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4164 if (!merge_func)
4165 {
4166 if (str->reserve(SPIDER_SQL_CAST_LEN))
4167 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4168 str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
4169 }
4170#if MYSQL_VERSION_ID < 50500
4171 item_func->print(tmp_str.get_str(), QT_IS);
4172#else
4173 item_func->print(tmp_str.get_str(), QT_TO_SYSTEM_CHARSET);
4174#endif
4175 tmp_str.mem_calc();
4176 if (tmp_str.reserve(1))
4177 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4178 tmp_ptr = tmp_str.c_ptr_quick();
4179 DBUG_PRINT("info",("spider tmp_ptr = %s", tmp_ptr));
4180 while ((tmp_ptr2 = strstr(tmp_ptr, SPIDER_SQL_AS_DECIMAL_STR)))
4181 tmp_ptr = tmp_ptr2 + 1;
4182 last_str = tmp_ptr - 1;
4183 last_str_length = strlen(last_str) - SPIDER_SQL_CLOSE_PAREN_LEN;
4184 }
4185 break;
4186 } else if (!strncasecmp("cast_as_datetime", func_name,
4187 func_name_length))
4188 {
4189 item = item_list[0];
4190 if (item->type() == Item::FUNC_ITEM)
4191 {
4192 DBUG_PRINT("info",("spider child is FUNC_ITEM"));
4193 Item_func *ifunc = (Item_func *) item;
4194 if (ifunc->functype() == Item_func::UNKNOWN_FUNC)
4195 {
4196 const char *child_func_name;
4197 int child_func_name_length;
4198 DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
4199 child_func_name = (char*) ifunc->func_name();
4200 child_func_name_length = strlen(child_func_name);
4201 DBUG_PRINT("info",("spider child func_name is %s", child_func_name));
4202 if (
4203 child_func_name_length == 10 &&
4204 !strncasecmp("column_get", child_func_name, child_func_name_length)
4205 ) {
4206 DBUG_PRINT("info",("spider this is merge func"));
4207 merge_func = TRUE;
4208 }
4209 }
4210 }
4211
4212 if (str)
4213 {
4214 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4215 if (!merge_func)
4216 {
4217 if (str->reserve(SPIDER_SQL_CAST_LEN))
4218 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4219 str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
4220 }
4221 }
4222 last_str = SPIDER_SQL_AS_DATETIME_STR;
4223 last_str_length = SPIDER_SQL_AS_DATETIME_LEN;
4224 break;
4225 }
4226 } else if (func_name_length == 17)
4227 {
4228 if (!strncasecmp("date_add_interval", func_name, func_name_length))
4229 {
4230 Item_date_add_interval *item_date_add_interval =
4231 (Item_date_add_interval *) item_func;
4232 func_name = spider_db_timefunc_interval_str[
4233 item_date_add_interval->int_type];
4234 func_name_length = strlen(func_name);
4235 if ((error_num = spider_db_print_item_type(item_list[0], spider, str,
4236 alias, alias_length, dbton_id, use_fields, fields)))
4237 DBUG_RETURN(error_num);
4238 if (str)
4239 {
4240 if (item_date_add_interval->date_sub_interval)
4241 {
4242 if (str->reserve(SPIDER_SQL_NEGINTERVAL_LEN))
4243 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4244 str->q_append(SPIDER_SQL_NEGINTERVAL_STR,
4245 SPIDER_SQL_NEGINTERVAL_LEN);
4246 } else {
4247 if (str->reserve(SPIDER_SQL_INTERVAL_LEN))
4248 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4249 str->q_append(SPIDER_SQL_INTERVAL_STR, SPIDER_SQL_INTERVAL_LEN);
4250 }
4251 }
4252 if ((error_num = spider_db_print_item_type(item_list[1], spider, str,
4253 alias, alias_length, dbton_id, use_fields, fields)))
4254 DBUG_RETURN(error_num);
4255 if (str)
4256 {
4257 if (str->reserve(func_name_length + SPIDER_SQL_CLOSE_PAREN_LEN))
4258 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4259 str->q_append(func_name, func_name_length);
4260 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
4261 SPIDER_SQL_CLOSE_PAREN_LEN);
4262 }
4263 DBUG_RETURN(0);
4264 }
4265 }
4266 if (str)
4267 {
4268 if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN))
4269 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4270 str->q_append(func_name, func_name_length);
4271 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
4272 }
4273 func_name = SPIDER_SQL_COMMA_STR;
4274 func_name_length = SPIDER_SQL_COMMA_LEN;
4275 separete_str = SPIDER_SQL_COMMA_STR;
4276 separete_str_length = SPIDER_SQL_COMMA_LEN;
4277 last_str = SPIDER_SQL_CLOSE_PAREN_STR;
4278 last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
4279 break;
4280 case Item_func::NOW_FUNC:
4281 if (str)
4282 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4283 DBUG_RETURN(spider_db_open_item_string(item_func, spider, str,
4284 alias, alias_length, dbton_id, use_fields, fields));
4285 case Item_func::CHAR_TYPECAST_FUNC:
4286 DBUG_PRINT("info",("spider CHAR_TYPECAST_FUNC"));
4287 {
4288 item = item_list[0];
4289 if (item->type() == Item::FUNC_ITEM)
4290 {
4291 DBUG_PRINT("info",("spider child is FUNC_ITEM"));
4292 Item_func *ifunc = (Item_func *) item;
4293 if (ifunc->functype() == Item_func::UNKNOWN_FUNC)
4294 {
4295 const char *child_func_name;
4296 int child_func_name_length;
4297 DBUG_PRINT("info",("spider child is UNKNOWN_FUNC"));
4298 child_func_name = (char*) ifunc->func_name();
4299 child_func_name_length = strlen(child_func_name);
4300 DBUG_PRINT("info",("spider child func_name is %s", child_func_name));
4301 if (
4302 child_func_name_length == 10 &&
4303 !strncasecmp("column_get", child_func_name, child_func_name_length)
4304 ) {
4305 DBUG_PRINT("info",("spider this is merge func"));
4306 merge_func = TRUE;
4307 }
4308 }
4309 }
4310
4311 if (str)
4312 {
4313 char tmp_buf[MAX_FIELD_WIDTH], *tmp_ptr, *tmp_ptr2;
4314 spider_string tmp_str(tmp_buf, MAX_FIELD_WIDTH, str->charset());
4315 tmp_str.init_calc_mem(125);
4316 tmp_str.length(0);
4317 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4318 if (!merge_func)
4319 {
4320 if (str->reserve(SPIDER_SQL_CAST_LEN))
4321 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4322 str->q_append(SPIDER_SQL_CAST_STR, SPIDER_SQL_CAST_LEN);
4323 }
4324#if MYSQL_VERSION_ID < 50500
4325 item_func->print(tmp_str.get_str(), QT_IS);
4326#else
4327 item_func->print(tmp_str.get_str(), QT_TO_SYSTEM_CHARSET);
4328#endif
4329 tmp_str.mem_calc();
4330 if (tmp_str.reserve(1))
4331 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4332 tmp_ptr = tmp_str.c_ptr_quick();
4333 DBUG_PRINT("info",("spider tmp_ptr = %s", tmp_ptr));
4334 while ((tmp_ptr2 = strstr(tmp_ptr, SPIDER_SQL_AS_CHAR_STR)))
4335 tmp_ptr = tmp_ptr2 + 1;
4336 last_str = tmp_ptr - 1;
4337 last_str_length = strlen(last_str) - SPIDER_SQL_CLOSE_PAREN_LEN;
4338 }
4339 }
4340 break;
4341 case Item_func::NOT_FUNC:
4342 DBUG_PRINT("info",("spider NOT_FUNC"));
4343 if (item_list[0]->type() == Item::COND_ITEM)
4344 {
4345 DBUG_PRINT("info",("spider item_list[0] is COND_ITEM"));
4346 Item_cond *item_cond = (Item_cond *) item_list[0];
4347 if (item_cond->functype() == Item_func::COND_AND_FUNC)
4348 {
4349 DBUG_PRINT("info",("spider item_cond is COND_AND_FUNC"));
4350 List_iterator_fast<Item> lif(*(item_cond->argument_list()));
4351 bool has_expr_cache_item = FALSE;
4352 bool has_isnotnull_func = FALSE;
4353 bool has_other_item = FALSE;
4354 while((item = lif++))
4355 {
4356#ifdef SPIDER_HAS_EXPR_CACHE_ITEM
4357 if (
4358 item->type() == Item::EXPR_CACHE_ITEM
4359 ) {
4360 DBUG_PRINT("info",("spider EXPR_CACHE_ITEM"));
4361 has_expr_cache_item = TRUE;
4362 } else
4363#endif
4364 if (
4365 item->type() == Item::FUNC_ITEM &&
4366 ((Item_func *) item)->functype() == Item_func::ISNOTNULL_FUNC
4367 ) {
4368 DBUG_PRINT("info",("spider ISNOTNULL_FUNC"));
4369 has_isnotnull_func = TRUE;
4370 } else {
4371 DBUG_PRINT("info",("spider has other item"));
4372 DBUG_PRINT("info",("spider COND type=%d", item->type()));
4373 has_other_item = TRUE;
4374 }
4375 }
4376 if (has_expr_cache_item && has_isnotnull_func && !has_other_item)
4377 {
4378 DBUG_PRINT("info",("spider NOT EXISTS skip"));
4379 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
4380 }
4381 }
4382 }
4383 if (str)
4384 {
4385 func_name = (char*) item_func->func_name();
4386 func_name_length = strlen(func_name);
4387 if (str->reserve(func_name_length + SPIDER_SQL_SPACE_LEN))
4388 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4389 str->q_append(func_name, func_name_length);
4390 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
4391 }
4392 break;
4393 case Item_func::NEG_FUNC:
4394 if (str)
4395 {
4396 func_name = (char*) item_func->func_name();
4397 func_name_length = strlen(func_name);
4398 if (str->reserve(func_name_length + SPIDER_SQL_SPACE_LEN))
4399 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4400 str->q_append(func_name, func_name_length);
4401 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
4402 }
4403 break;
4404 case Item_func::IN_FUNC:
4405 if (((Item_func_opt_neg *) item_func)->negated)
4406 {
4407 func_name = SPIDER_SQL_NOT_IN_STR;
4408 func_name_length = SPIDER_SQL_NOT_IN_LEN;
4409 separete_str = SPIDER_SQL_COMMA_STR;
4410 separete_str_length = SPIDER_SQL_COMMA_LEN;
4411 last_str = SPIDER_SQL_CLOSE_PAREN_STR;
4412 last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
4413 } else {
4414 func_name = SPIDER_SQL_IN_STR;
4415 func_name_length = SPIDER_SQL_IN_LEN;
4416 separete_str = SPIDER_SQL_COMMA_STR;
4417 separete_str_length = SPIDER_SQL_COMMA_LEN;
4418 last_str = SPIDER_SQL_CLOSE_PAREN_STR;
4419 last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
4420 }
4421 break;
4422 case Item_func::BETWEEN:
4423 if (((Item_func_opt_neg *) item_func)->negated)
4424 {
4425 func_name = SPIDER_SQL_NOT_BETWEEN_STR;
4426 func_name_length = SPIDER_SQL_NOT_BETWEEN_LEN;
4427 separete_str = SPIDER_SQL_AND_STR;
4428 separete_str_length = SPIDER_SQL_AND_LEN;
4429 } else {
4430 func_name = (char*) item_func->func_name();
4431 func_name_length = strlen(func_name);
4432 separete_str = SPIDER_SQL_AND_STR;
4433 separete_str_length = SPIDER_SQL_AND_LEN;
4434 }
4435 break;
4436 case Item_func::UDF_FUNC:
4437 use_pushdown_udf = spider_param_use_pushdown_udf(spider->trx->thd,
4438 spider->share->use_pushdown_udf);
4439 if (!use_pushdown_udf)
4440 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
4441 if (str)
4442 {
4443 func_name = (char*) item_func->func_name();
4444 func_name_length = strlen(func_name);
4445 DBUG_PRINT("info",("spider func_name = %s", func_name));
4446 DBUG_PRINT("info",("spider func_name_length = %d", func_name_length));
4447 if (str->reserve(func_name_length + SPIDER_SQL_OPEN_PAREN_LEN))
4448 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4449 str->q_append(func_name, func_name_length);
4450 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
4451 }
4452 func_name = SPIDER_SQL_COMMA_STR;
4453 func_name_length = SPIDER_SQL_COMMA_LEN;
4454 separete_str = SPIDER_SQL_COMMA_STR;
4455 separete_str_length = SPIDER_SQL_COMMA_LEN;
4456 last_str = SPIDER_SQL_CLOSE_PAREN_STR;
4457 last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
4458 break;
4459#ifdef MARIADB_BASE_VERSION
4460 case Item_func::XOR_FUNC:
4461#else
4462 case Item_func::COND_XOR_FUNC:
4463#endif
4464 if (str)
4465 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4466 DBUG_RETURN(
4467 spider_db_open_item_cond((Item_cond *) item_func, spider, str,
4468 alias, alias_length, dbton_id, use_fields, fields));
4469 case Item_func::TRIG_COND_FUNC:
4470 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
4471 case Item_func::GUSERVAR_FUNC:
4472 if (str)
4473 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
4474 if (item_func->result_type() == STRING_RESULT)
4475 DBUG_RETURN(spider_db_open_item_string(item_func, spider, str,
4476 alias, alias_length, dbton_id, use_fields, fields));
4477 else
4478 DBUG_RETURN(spider_db_open_item_int(item_func, spider, str,
4479 alias, alias_length, dbton_id, use_fields, fields));
4480 case Item_func::FT_FUNC:
4481 if (spider_db_check_ft_idx(item_func, spider) == MAX_KEY)
4482 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
4483 start_item = 1;
4484 if (str)
4485 {
4486 if (str->reserve(SPIDER_SQL_MATCH_LEN))
4487 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4488 str->q_append(SPIDER_SQL_MATCH_STR, SPIDER_SQL_MATCH_LEN);
4489 }
4490 separete_str = SPIDER_SQL_COMMA_STR;
4491 separete_str_length = SPIDER_SQL_COMMA_LEN;
4492 last_str = SPIDER_SQL_CLOSE_PAREN_STR;
4493 last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
4494 break;
4495 case Item_func::SP_EQUALS_FUNC:
4496 if (str)
4497 {
4498 func_name = SPIDER_SQL_MBR_EQUAL_STR;
4499 func_name_length = SPIDER_SQL_MBR_EQUAL_LEN;
4500 DBUG_PRINT("info",("spider func_name = %s", func_name));
4501 DBUG_PRINT("info",("spider func_name_length = %d", func_name_length));
4502 if (str->reserve(func_name_length))
4503 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4504 str->q_append(func_name, func_name_length);
4505 }
4506 func_name = SPIDER_SQL_COMMA_STR;
4507 func_name_length = SPIDER_SQL_COMMA_LEN;
4508 separete_str = SPIDER_SQL_COMMA_STR;
4509 separete_str_length = SPIDER_SQL_COMMA_LEN;
4510 last_str = SPIDER_SQL_CLOSE_PAREN_STR;
4511 last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
4512 break;
4513 case Item_func::SP_DISJOINT_FUNC:
4514 case Item_func::SP_INTERSECTS_FUNC:
4515 case Item_func::SP_TOUCHES_FUNC:
4516 case Item_func::SP_CROSSES_FUNC:
4517 case Item_func::SP_WITHIN_FUNC:
4518 case Item_func::SP_CONTAINS_FUNC:
4519 case Item_func::SP_OVERLAPS_FUNC:
4520 if (str)
4521 {
4522 func_name = (char*) item_func->func_name();
4523 func_name_length = strlen(func_name);
4524 DBUG_PRINT("info",("spider func_name = %s", func_name));
4525 DBUG_PRINT("info",("spider func_name_length = %d", func_name_length));
4526 if (str->reserve(
4527#ifndef SPIDER_ITEM_GEOFUNC_NAME_HAS_MBR
4528 SPIDER_SQL_MBR_LEN +
4529#endif
4530 func_name_length + SPIDER_SQL_OPEN_PAREN_LEN))
4531 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4532#ifndef SPIDER_ITEM_GEOFUNC_NAME_HAS_MBR
4533 str->q_append(SPIDER_SQL_MBR_STR, SPIDER_SQL_MBR_LEN);
4534#endif
4535 str->q_append(func_name, func_name_length);
4536 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
4537 }
4538 func_name = SPIDER_SQL_COMMA_STR;
4539 func_name_length = SPIDER_SQL_COMMA_LEN;
4540 separete_str = SPIDER_SQL_COMMA_STR;
4541 separete_str_length = SPIDER_SQL_COMMA_LEN;
4542 last_str = SPIDER_SQL_CLOSE_PAREN_STR;
4543 last_str_length = SPIDER_SQL_CLOSE_PAREN_LEN;
4544 break;
4545 case Item_func::EQ_FUNC:
4546 case Item_func::EQUAL_FUNC:
4547 case Item_func::NE_FUNC:
4548 case Item_func::LT_FUNC:
4549 case Item_func::LE_FUNC:
4550 case Item_func::GE_FUNC:
4551 case Item_func::GT_FUNC:
4552 case Item_func::LIKE_FUNC:
4553 if (str)
4554 {
4555 func_name = (char*) item_func->func_name();
4556 func_name_length = strlen(func_name);
4557 }
4558 break;
4559 default:
4560 THD *thd = spider->trx->thd;
4561 SPIDER_SHARE *share = spider->share;
4562 if (spider_param_skip_default_condition(thd,
4563 share->skip_default_condition))
4564 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
4565 if (str)
4566 {
4567 func_name = (char*) item_func->func_name();
4568 func_name_length = strlen(func_name);
4569 }
4570 break;
4571 }
4572 DBUG_PRINT("info",("spider func_name = %s", func_name));
4573 DBUG_PRINT("info",("spider func_name_length = %d", func_name_length));
4574 DBUG_PRINT("info",("spider separete_str = %s", separete_str));
4575 DBUG_PRINT("info",("spider separete_str_length = %d", separete_str_length));
4576 DBUG_PRINT("info",("spider last_str = %s", last_str));
4577 DBUG_PRINT("info",("spider last_str_length = %d", last_str_length));
4578 if (item_count)
4579 {
4580 item_count--;
4581 for (roop_count = start_item; roop_count < item_count; roop_count++)
4582 {
4583 item = item_list[roop_count];
4584 if ((error_num = spider_db_print_item_type(item, spider, str,
4585 alias, alias_length, dbton_id, use_fields, fields)))
4586 DBUG_RETURN(error_num);
4587 if (roop_count == 1)
4588 {
4589 func_name = separete_str;
4590 func_name_length = separete_str_length;
4591 }
4592 if (str)
4593 {
4594 if (str->reserve(func_name_length + SPIDER_SQL_SPACE_LEN * 2))
4595 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4596 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
4597 str->q_append(func_name, func_name_length);
4598 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
4599 }
4600 }
4601 item = item_list[roop_count];
4602 if ((error_num = spider_db_print_item_type(item, spider, str,
4603 alias, alias_length, dbton_id, use_fields, fields)))
4604 DBUG_RETURN(error_num);
4605 }
4606 if (item_func->functype() == Item_func::FT_FUNC)
4607 {
4608 Item_func_match *item_func_match = (Item_func_match *)item_func;
4609 if (str)
4610 {
4611 if (str->reserve(SPIDER_SQL_AGAINST_LEN))
4612 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4613 str->q_append(SPIDER_SQL_AGAINST_STR, SPIDER_SQL_AGAINST_LEN);
4614 }
4615 item = item_list[0];
4616 if ((error_num = spider_db_print_item_type(item, spider, str,
4617 alias, alias_length, dbton_id, use_fields, fields)))
4618 DBUG_RETURN(error_num);
4619 if (str)
4620 {
4621 if (str->reserve(
4622 ((item_func_match->flags & FT_BOOL) ?
4623 SPIDER_SQL_IN_BOOLEAN_MODE_LEN : 0) +
4624 ((item_func_match->flags & FT_EXPAND) ?
4625 SPIDER_SQL_WITH_QUERY_EXPANSION_LEN : 0)
4626 ))
4627 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4628 if (item_func_match->flags & FT_BOOL)
4629 str->q_append(SPIDER_SQL_IN_BOOLEAN_MODE_STR,
4630 SPIDER_SQL_IN_BOOLEAN_MODE_LEN);
4631 if (item_func_match->flags & FT_EXPAND)
4632 str->q_append(SPIDER_SQL_WITH_QUERY_EXPANSION_STR,
4633 SPIDER_SQL_WITH_QUERY_EXPANSION_LEN);
4634 }
4635 } else if (item_func->functype() == Item_func::UNKNOWN_FUNC)
4636 {
4637 if (
4638 func_name_length == 7 &&
4639 !strncasecmp("convert", func_name, func_name_length)
4640 ) {
4641 if (str)
4642 {
4643 Item_func_conv_charset *item_func_conv_charset =
4644 (Item_func_conv_charset *)item_func;
4645 CHARSET_INFO *conv_charset =
4646 item_func_conv_charset->SPIDER_Item_func_conv_charset_conv_charset;
4647 uint cset_length = strlen(conv_charset->csname);
4648 if (str->reserve(SPIDER_SQL_USING_LEN + cset_length))
4649 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4650 str->q_append(SPIDER_SQL_USING_STR, SPIDER_SQL_USING_LEN);
4651 str->q_append(conv_charset->csname, cset_length);
4652 }
4653 }
4654 }
4655 if (str)
4656 {
4657 if (merge_func)
4658 str->length(str->length() - SPIDER_SQL_CLOSE_PAREN_LEN);
4659 if (str->reserve(last_str_length + SPIDER_SQL_CLOSE_PAREN_LEN))
4660 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4661 str->q_append(last_str, last_str_length);
4662 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
4663 }
4664 DBUG_RETURN(0);
4665}
4666
4667#ifdef HANDLER_HAS_DIRECT_AGGREGATE
4668int spider_db_mysql_util::open_item_sum_func(
4669 Item_sum *item_sum,
4670 ha_spider *spider,
4671 spider_string *str,
4672 const char *alias,
4673 uint alias_length,
4674 bool use_fields,
4675 spider_fields *fields
4676) {
4677 uint dbton_id = spider_dbton_mysql.dbton_id;
4678 uint roop_count, item_count = item_sum->get_arg_count();
4679 int error_num;
4680 DBUG_ENTER("spider_db_mysql_util::open_item_sum_func");
4681 DBUG_PRINT("info",("spider Sumfunctype = %d", item_sum->sum_func()));
4682 switch (item_sum->sum_func())
4683 {
4684 case Item_sum::COUNT_FUNC:
4685 case Item_sum::SUM_FUNC:
4686 case Item_sum::MIN_FUNC:
4687 case Item_sum::MAX_FUNC:
4688 {
4689 const char *func_name = item_sum->func_name();
4690 uint func_name_length = strlen(func_name);
4691 Item *item, **args = item_sum->get_args();
4692 if (str)
4693 {
4694 if (str->reserve(func_name_length))
4695 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4696 str->q_append(func_name, func_name_length);
4697 }
4698 if (item_count)
4699 {
4700 item_count--;
4701 for (roop_count = 0; roop_count < item_count; roop_count++)
4702 {
4703 item = args[roop_count];
4704 if ((error_num = spider_db_print_item_type(item, spider, str,
4705 alias, alias_length, dbton_id, use_fields, fields)))
4706 DBUG_RETURN(error_num);
4707 if (str)
4708 {
4709 if (str->reserve(SPIDER_SQL_COMMA_LEN))
4710 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4711 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
4712 }
4713 }
4714 item = args[roop_count];
4715 if ((error_num = spider_db_print_item_type(item, spider, str,
4716 alias, alias_length, dbton_id, use_fields, fields)))
4717 DBUG_RETURN(error_num);
4718 }
4719 if (str)
4720 {
4721 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN))
4722 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4723 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
4724 SPIDER_SQL_CLOSE_PAREN_LEN);
4725 }
4726 }
4727 break;
4728 case Item_sum::COUNT_DISTINCT_FUNC:
4729 case Item_sum::SUM_DISTINCT_FUNC:
4730 case Item_sum::AVG_FUNC:
4731 case Item_sum::AVG_DISTINCT_FUNC:
4732 case Item_sum::STD_FUNC:
4733 case Item_sum::VARIANCE_FUNC:
4734 case Item_sum::SUM_BIT_FUNC:
4735 case Item_sum::UDF_SUM_FUNC:
4736 case Item_sum::GROUP_CONCAT_FUNC:
4737 default:
4738 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
4739 }
4740 DBUG_RETURN(0);
4741}
4742#endif
4743
4744int spider_db_mysql_util::append_escaped_util(
4745 spider_string *to,
4746 String *from
4747) {
4748 DBUG_ENTER("spider_db_mysql_util::append_escaped_util");
4749 DBUG_PRINT("info",("spider this=%p", this));
4750 DBUG_PRINT("info",("spider from=%s", from->charset()->csname));
4751 DBUG_PRINT("info",("spider to=%s", to->charset()->csname));
4752 to->append_escape_string(from->ptr(), from->length());
4753 DBUG_RETURN(0);
4754}
4755
4756#ifdef SPIDER_HAS_GROUP_BY_HANDLER
4757int spider_db_mysql_util::append_from_and_tables(
4758 spider_fields *fields,
4759 spider_string *str
4760) {
4761 SPIDER_TABLE_HOLDER *table_holder;
4762 int error_num;
4763 uint dbton_id = spider_dbton_mysql.dbton_id, from_length;
4764 spider_mysql_share *db_share;
4765 spider_mysql_handler *dbton_hdl;
4766 ha_spider *spider;
4767 DBUG_ENTER("spider_db_mysql_util::append_from_and_tables");
4768 DBUG_PRINT("info",("spider this=%p", this));
4769
4770 /* calculate from size */
4771 from_length = SPIDER_SQL_FROM_LEN;
4772 fields->set_pos_to_first_table_holder();
4773 while ((table_holder = fields->get_next_table_holder()))
4774 {
4775 spider = table_holder->spider;
4776 db_share = (spider_mysql_share *)
4777 spider->share->dbton_share[dbton_id];
4778 from_length +=
4779 db_share->db_nm_max_length +
4780 SPIDER_SQL_DOT_LEN + /* SPIDER_SQL_NAME_QUOTE_LEN */ 4 +
4781 db_share->table_nm_max_length +
4782 SPIDER_SQL_SPACE_LEN + SPIDER_SQL_COMMA_LEN +
4783 table_holder->alias->length() - SPIDER_SQL_DOT_LEN;
4784 }
4785
4786 if (str->reserve(from_length))
4787 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4788 str->q_append(SPIDER_SQL_FROM_STR, SPIDER_SQL_FROM_LEN);
4789
4790 fields->set_pos_to_first_table_holder();
4791 while ((table_holder = fields->get_next_table_holder()))
4792 {
4793 spider = table_holder->spider;
4794 db_share = (spider_mysql_share *)
4795 spider->share->dbton_share[dbton_id];
4796 dbton_hdl = (spider_mysql_handler *)
4797 spider->dbton_handler[dbton_id];
4798 dbton_hdl->table_name_pos = str->length();
4799 if ((error_num = db_share->append_table_name_with_adjusting(str,
4800 spider->conn_link_idx[dbton_hdl->first_link_idx])))
4801 {
4802 DBUG_RETURN(error_num);
4803 }
4804 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
4805 str->q_append(table_holder->alias->ptr(),
4806 table_holder->alias->length() - SPIDER_SQL_DOT_LEN);
4807 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
4808 }
4809 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
4810 DBUG_RETURN(0);
4811}
4812
4813int spider_db_mysql_util::reappend_tables(
4814 spider_fields *fields,
4815 SPIDER_LINK_IDX_CHAIN *link_idx_chain,
4816 spider_string *str
4817) {
4818 int error_num;
4819 uint dbton_id = spider_dbton_mysql.dbton_id, length;
4820 ha_spider *spider;
4821 spider_mysql_share *db_share;
4822 spider_mysql_handler *dbton_hdl;
4823 SPIDER_TABLE_HOLDER *table_holder;
4824 SPIDER_LINK_IDX_HOLDER *link_idx_holder;
4825 DBUG_ENTER("spider_db_mysql_util::reappend_tables");
4826 DBUG_PRINT("info",("spider this=%p", this));
4827 length = str->length();
4828 fields->set_pos_to_first_table_on_link_idx_chain(link_idx_chain);
4829 fields->set_pos_to_first_table_holder();
4830 while ((table_holder = fields->get_next_table_holder()))
4831 {
4832 link_idx_holder =
4833 fields->get_next_table_on_link_idx_chain(link_idx_chain);
4834 spider = table_holder->spider;
4835 db_share = (spider_mysql_share *)
4836 spider->share->dbton_share[dbton_id];
4837 if (!db_share->same_db_table_name)
4838 {
4839 dbton_hdl = (spider_mysql_handler *) spider->dbton_handler[dbton_id];
4840 str->length(dbton_hdl->table_name_pos);
4841 if ((error_num = db_share->append_table_name_with_adjusting(str,
4842 spider->conn_link_idx[link_idx_holder->link_idx])))
4843 {
4844 DBUG_RETURN(error_num);
4845 }
4846 }
4847 }
4848 str->length(length);
4849 DBUG_RETURN(0);
4850}
4851
4852int spider_db_mysql_util::append_where(
4853 spider_string *str
4854) {
4855 DBUG_ENTER("spider_db_mysql_util::append_where");
4856 DBUG_PRINT("info",("spider this=%p", this));
4857 if (str->reserve(SPIDER_SQL_WHERE_LEN))
4858 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4859 str->q_append(SPIDER_SQL_WHERE_STR, SPIDER_SQL_WHERE_LEN);
4860 DBUG_RETURN(0);
4861}
4862
4863int spider_db_mysql_util::append_having(
4864 spider_string *str
4865) {
4866 DBUG_ENTER("spider_db_mysql_util::append_having");
4867 DBUG_PRINT("info",("spider this=%p", this));
4868 if (str->reserve(SPIDER_SQL_HAVING_LEN))
4869 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4870 str->q_append(SPIDER_SQL_HAVING_STR, SPIDER_SQL_HAVING_LEN);
4871 DBUG_RETURN(0);
4872}
4873#endif
4874
4875spider_mysql_share::spider_mysql_share(
4876 st_spider_share *share
4877) : spider_db_share(
4878 share
4879),
4880 table_select(NULL),
4881 table_select_pos(0),
4882 key_select(NULL),
4883 key_select_pos(NULL),
4884 key_hint(NULL),
4885 show_table_status(NULL),
4886 show_records(NULL),
4887 show_index(NULL),
4888 table_names_str(NULL),
4889 db_names_str(NULL),
4890 db_table_str(NULL),
4891#ifdef SPIDER_HAS_HASH_VALUE_TYPE
4892 db_table_str_hash_value(NULL),
4893#endif
4894 table_nm_max_length(0),
4895 db_nm_max_length(0),
4896 column_name_str(NULL),
4897 same_db_table_name(TRUE),
4898 first_all_link_idx(-1)
4899{
4900 DBUG_ENTER("spider_mysql_share::spider_mysql_share");
4901 DBUG_PRINT("info",("spider this=%p", this));
4902 spider_alloc_calc_mem_init(mem_calc, 71);
4903 spider_alloc_calc_mem(spider_current_trx, mem_calc, sizeof(*this));
4904 DBUG_VOID_RETURN;
4905}
4906
4907spider_mysql_share::~spider_mysql_share()
4908{
4909 DBUG_ENTER("spider_mysql_share::~spider_mysql_share");
4910 DBUG_PRINT("info",("spider this=%p", this));
4911 if (table_select)
4912 delete [] table_select;
4913 if (key_select)
4914 delete [] key_select;
4915 if (key_hint)
4916 delete [] key_hint;
4917 free_show_table_status();
4918 free_show_records();
4919 free_show_index();
4920 free_column_name_str();
4921 free_table_names_str();
4922 if (key_select_pos)
4923 {
4924 spider_free(spider_current_trx, key_select_pos, MYF(0));
4925 }
4926 spider_free_mem_calc(spider_current_trx, mem_calc_id, sizeof(*this));
4927 DBUG_VOID_RETURN;
4928}
4929
4930int spider_mysql_share::init()
4931{
4932 int error_num;
4933 uint roop_count;
4934 TABLE_SHARE *table_share = spider_share->table_share;
4935 uint keys = table_share ? table_share->keys : 0;
4936 DBUG_ENTER("spider_mysql_share::init");
4937 DBUG_PRINT("info",("spider this=%p", this));
4938 if (!(key_select_pos = (int *)
4939 spider_bulk_alloc_mem(spider_current_trx, 112,
4940 __func__, __FILE__, __LINE__, MYF(MY_WME | MY_ZEROFILL),
4941 &key_select_pos,
4942 sizeof(int) * keys,
4943#ifdef SPIDER_HAS_HASH_VALUE_TYPE
4944 &db_table_str_hash_value,
4945 sizeof(my_hash_value_type) * spider_share->all_link_count,
4946#endif
4947 NullS))
4948 ) {
4949 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4950 }
4951
4952 if (keys > 0 &&
4953 !(key_hint = new spider_string[keys])
4954 ) {
4955 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4956 }
4957 for (roop_count = 0; roop_count < keys; roop_count++)
4958 {
4959 key_hint[roop_count].init_calc_mem(189);
4960 key_hint[roop_count].set_charset(spider_share->access_charset);
4961 }
4962 DBUG_PRINT("info",("spider key_hint=%p", key_hint));
4963
4964 if (
4965 !(table_select = new spider_string[1]) ||
4966 (keys > 0 &&
4967 !(key_select = new spider_string[keys])
4968 ) ||
4969 (error_num = create_table_names_str()) ||
4970 (table_share &&
4971 (
4972 (error_num = create_column_name_str()) ||
4973 (error_num = convert_key_hint_str()) ||
4974 (error_num = append_show_table_status()) ||
4975 (error_num = append_show_records()) ||
4976 (error_num = append_show_index())
4977 )
4978 )
4979 ) {
4980 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4981 }
4982
4983 table_select->init_calc_mem(96);
4984 if (table_share && (error_num = append_table_select()))
4985 DBUG_RETURN(error_num);
4986
4987 for (roop_count = 0; roop_count < keys; roop_count++)
4988 {
4989 key_select[roop_count].init_calc_mem(97);
4990 if ((error_num = append_key_select(roop_count)))
4991 DBUG_RETURN(error_num);
4992 }
4993
4994 DBUG_RETURN(error_num);
4995}
4996
4997uint spider_mysql_share::get_column_name_length(
4998 uint field_index
4999) {
5000 DBUG_ENTER("spider_mysql_share::get_column_name_length");
5001 DBUG_PRINT("info",("spider this=%p", this));
5002 DBUG_RETURN(column_name_str[field_index].length());
5003}
5004
5005int spider_mysql_share::append_column_name(
5006 spider_string *str,
5007 uint field_index
5008) {
5009 int error_num;
5010 DBUG_ENTER("spider_mysql_share::append_column_name");
5011 DBUG_PRINT("info",("spider this=%p", this));
5012 error_num = spider_db_mysql_utility.append_name(str,
5013 column_name_str[field_index].ptr(), column_name_str[field_index].length());
5014 DBUG_RETURN(error_num);
5015}
5016
5017int spider_mysql_share::append_column_name_with_alias(
5018 spider_string *str,
5019 uint field_index,
5020 const char *alias,
5021 uint alias_length
5022) {
5023 DBUG_ENTER("spider_mysql_share::append_column_name_with_alias");
5024 DBUG_PRINT("info",("spider this=%p", this));
5025 if (str->reserve(
5026 alias_length +
5027 column_name_str[field_index].length() +
5028 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2))
5029 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5030 str->q_append(alias, alias_length);
5031 append_column_name(str, field_index);
5032 DBUG_RETURN(0);
5033}
5034
5035int spider_mysql_share::append_table_name(
5036 spider_string *str,
5037 int all_link_idx
5038) {
5039 const char *db_nm = db_names_str[all_link_idx].ptr();
5040 uint db_nm_len = db_names_str[all_link_idx].length();
5041 const char *table_nm = table_names_str[all_link_idx].ptr();
5042 uint table_nm_len = table_names_str[all_link_idx].length();
5043 DBUG_ENTER("spider_mysql_share::append_table_name");
5044 DBUG_PRINT("info",("spider this=%p", this));
5045 if (str->reserve(db_nm_len + SPIDER_SQL_DOT_LEN + table_nm_len +
5046 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4))
5047 {
5048 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5049 }
5050 spider_db_mysql_utility.append_name(str, db_nm, db_nm_len);
5051 str->q_append(SPIDER_SQL_DOT_STR, SPIDER_SQL_DOT_LEN);
5052 spider_db_mysql_utility.append_name(str, table_nm, table_nm_len);
5053 DBUG_RETURN(0);
5054}
5055
5056int spider_mysql_share::append_table_name_with_adjusting(
5057 spider_string *str,
5058 int all_link_idx
5059) {
5060 const char *db_nm = db_names_str[all_link_idx].ptr();
5061 uint db_nm_len = db_names_str[all_link_idx].length();
5062 uint db_nm_max_len = db_nm_max_length;
5063 const char *table_nm = table_names_str[all_link_idx].ptr();
5064 uint table_nm_len = table_names_str[all_link_idx].length();
5065 uint table_nm_max_len = table_nm_max_length;
5066 DBUG_ENTER("spider_mysql_share::append_table_name_with_adjusting");
5067 DBUG_PRINT("info",("spider this=%p", this));
5068 spider_db_mysql_utility.append_name(str, db_nm, db_nm_len);
5069 str->q_append(SPIDER_SQL_DOT_STR, SPIDER_SQL_DOT_LEN);
5070 spider_db_mysql_utility.append_name(str, table_nm, table_nm_len);
5071 uint length =
5072 db_nm_max_len - db_nm_len +
5073 table_nm_max_len - table_nm_len;
5074 memset((char *) str->ptr() + str->length(), ' ', length);
5075 str->length(str->length() + length);
5076 DBUG_RETURN(0);
5077}
5078
5079int spider_mysql_share::append_from_with_adjusted_table_name(
5080 spider_string *str,
5081 int *table_name_pos
5082) {
5083 const char *db_nm = db_names_str[0].ptr();
5084 uint db_nm_len = db_names_str[0].length();
5085 uint db_nm_max_len = db_nm_max_length;
5086 const char *table_nm = table_names_str[0].ptr();
5087 uint table_nm_len = table_names_str[0].length();
5088 uint table_nm_max_len = table_nm_max_length;
5089 DBUG_ENTER("spider_mysql_share::append_from_with_adjusted_table_name");
5090 DBUG_PRINT("info",("spider this=%p", this));
5091 if (str->reserve(SPIDER_SQL_FROM_LEN + db_nm_max_length +
5092 SPIDER_SQL_DOT_LEN + table_nm_max_length +
5093 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4))
5094 {
5095 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5096 }
5097 str->q_append(SPIDER_SQL_FROM_STR, SPIDER_SQL_FROM_LEN);
5098 *table_name_pos = str->length();
5099 spider_db_mysql_utility.append_name(str, db_nm, db_nm_len);
5100 str->q_append(SPIDER_SQL_DOT_STR, SPIDER_SQL_DOT_LEN);
5101 spider_db_mysql_utility.append_name(str, table_nm, table_nm_len);
5102 uint length =
5103 db_nm_max_len - db_nm_len +
5104 table_nm_max_len - table_nm_len;
5105 memset((char *) str->ptr() + str->length(), ' ', length);
5106 str->length(str->length() + length);
5107 DBUG_RETURN(0);
5108}
5109
5110int spider_mysql_share::create_table_names_str()
5111{
5112 int error_num, roop_count;
5113 uint table_nm_len, db_nm_len;
5114 spider_string *str, *first_tbl_nm_str, *first_db_nm_str, *first_db_tbl_str;
5115 char *first_tbl_nm, *first_db_nm;
5116 uint dbton_id = spider_dbton_mysql.dbton_id;
5117 DBUG_ENTER("spider_mysql_share::create_table_names_str");
5118 table_names_str = NULL;
5119 db_names_str = NULL;
5120 db_table_str = NULL;
5121 if (
5122 !(table_names_str = new spider_string[spider_share->all_link_count]) ||
5123 !(db_names_str = new spider_string[spider_share->all_link_count]) ||
5124 !(db_table_str = new spider_string[spider_share->all_link_count])
5125 ) {
5126 error_num = HA_ERR_OUT_OF_MEM;
5127 goto error;
5128 }
5129
5130 same_db_table_name = TRUE;
5131 first_tbl_nm = spider_share->tgt_table_names[0];
5132 first_db_nm = spider_share->tgt_dbs[0];
5133 table_nm_len = spider_share->tgt_table_names_lengths[0];
5134 db_nm_len = spider_share->tgt_dbs_lengths[0];
5135 first_tbl_nm_str = &table_names_str[0];
5136 first_db_nm_str = &db_names_str[0];
5137 first_db_tbl_str = &db_table_str[0];
5138 for (roop_count = 0; roop_count < (int) spider_share->all_link_count;
5139 roop_count++)
5140 {
5141 table_names_str[roop_count].init_calc_mem(86);
5142 db_names_str[roop_count].init_calc_mem(87);
5143 db_table_str[roop_count].init_calc_mem(88);
5144 if (spider_share->sql_dbton_ids[roop_count] != dbton_id)
5145 continue;
5146 if (first_all_link_idx == -1)
5147 first_all_link_idx = roop_count;
5148
5149 str = &table_names_str[roop_count];
5150 if (
5151 roop_count != 0 &&
5152 same_db_table_name &&
5153 spider_share->tgt_table_names_lengths[roop_count] == table_nm_len &&
5154 !memcmp(first_tbl_nm, spider_share->tgt_table_names[roop_count],
5155 table_nm_len)
5156 ) {
5157 if (str->copy(*first_tbl_nm_str))
5158 {
5159 error_num = HA_ERR_OUT_OF_MEM;
5160 goto error;
5161 }
5162 } else {
5163 str->set_charset(spider_share->access_charset);
5164 if ((error_num = spider_db_append_name_with_quote_str(str,
5165 spider_share->tgt_table_names[roop_count], dbton_id)))
5166 goto error;
5167 if (roop_count)
5168 {
5169 same_db_table_name = FALSE;
5170 DBUG_PRINT("info", ("spider found different table name %s",
5171 spider_share->tgt_table_names[roop_count]));
5172 if (str->length() > table_nm_max_length)
5173 table_nm_max_length = str->length();
5174 } else
5175 table_nm_max_length = str->length();
5176 }
5177
5178 str = &db_names_str[roop_count];
5179 if (
5180 roop_count != 0 &&
5181 same_db_table_name &&
5182 spider_share->tgt_dbs_lengths[roop_count] == db_nm_len &&
5183 !memcmp(first_db_nm, spider_share->tgt_dbs[roop_count],
5184 db_nm_len)
5185 ) {
5186 if (str->copy(*first_db_nm_str))
5187 {
5188 error_num = HA_ERR_OUT_OF_MEM;
5189 goto error;
5190 }
5191 } else {
5192 str->set_charset(spider_share->access_charset);
5193 if ((error_num = spider_db_append_name_with_quote_str(str,
5194 spider_share->tgt_dbs[roop_count], dbton_id)))
5195 goto error;
5196 if (roop_count)
5197 {
5198 same_db_table_name = FALSE;
5199 DBUG_PRINT("info", ("spider found different db name %s",
5200 spider_share->tgt_dbs[roop_count]));
5201 if (str->length() > db_nm_max_length)
5202 db_nm_max_length = str->length();
5203 } else
5204 db_nm_max_length = str->length();
5205 }
5206
5207 str = &db_table_str[roop_count];
5208 if (
5209 roop_count != 0 &&
5210 same_db_table_name
5211 ) {
5212 if (str->copy(*first_db_tbl_str))
5213 {
5214 error_num = HA_ERR_OUT_OF_MEM;
5215 goto error;
5216 }
5217 } else {
5218 str->set_charset(spider_share->access_charset);
5219 if ((error_num = append_table_name(str, roop_count)))
5220 goto error;
5221 }
5222#ifdef SPIDER_HAS_HASH_VALUE_TYPE
5223 db_table_str_hash_value[roop_count] = my_calc_hash(
5224 &spider_open_connections, (uchar*) str->ptr(), str->length());
5225#endif
5226 }
5227 DBUG_RETURN(0);
5228
5229error:
5230 if (db_table_str)
5231 {
5232 delete [] db_table_str;
5233 db_table_str = NULL;
5234 }
5235 if (db_names_str)
5236 {
5237 delete [] db_names_str;
5238 db_names_str = NULL;
5239 }
5240 if (table_names_str)
5241 {
5242 delete [] table_names_str;
5243 table_names_str = NULL;
5244 }
5245 DBUG_RETURN(error_num);
5246}
5247
5248void spider_mysql_share::free_table_names_str()
5249{
5250 DBUG_ENTER("spider_mysql_share::free_table_names_str");
5251 if (db_table_str)
5252 {
5253 delete [] db_table_str;
5254 db_table_str = NULL;
5255 }
5256 if (db_names_str)
5257 {
5258 delete [] db_names_str;
5259 db_names_str = NULL;
5260 }
5261 if (table_names_str)
5262 {
5263 delete [] table_names_str;
5264 table_names_str = NULL;
5265 }
5266 DBUG_VOID_RETURN;
5267}
5268
5269int spider_mysql_share::create_column_name_str()
5270{
5271 spider_string *str;
5272 int error_num;
5273 Field **field;
5274 TABLE_SHARE *table_share = spider_share->table_share;
5275 uint dbton_id = spider_dbton_mysql.dbton_id;
5276 DBUG_ENTER("spider_mysql_share::create_column_name_str");
5277 if (
5278 table_share->fields &&
5279 !(column_name_str = new spider_string[table_share->fields])
5280 )
5281 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5282 for (field = table_share->field, str = column_name_str;
5283 *field; field++, str++)
5284 {
5285 str->init_calc_mem(89);
5286 str->set_charset(spider_share->access_charset);
5287 if ((error_num = spider_db_append_name_with_quote_str(str,
5288 (char *) (*field)->field_name.str, dbton_id)))
5289 goto error;
5290 }
5291 DBUG_RETURN(0);
5292
5293error:
5294 if (column_name_str)
5295 {
5296 delete [] column_name_str;
5297 column_name_str = NULL;
5298 }
5299 DBUG_RETURN(error_num);
5300}
5301
5302void spider_mysql_share::free_column_name_str()
5303{
5304 DBUG_ENTER("spider_mysql_share::free_column_name_str");
5305 if (column_name_str)
5306 {
5307 delete [] column_name_str;
5308 column_name_str = NULL;
5309 }
5310 DBUG_VOID_RETURN;
5311}
5312
5313int spider_mysql_share::convert_key_hint_str()
5314{
5315 spider_string *tmp_key_hint;
5316 int roop_count;
5317 TABLE_SHARE *table_share = spider_share->table_share;
5318 DBUG_ENTER("spider_mysql_share::convert_key_hint_str");
5319 if (spider_share->access_charset->cset != system_charset_info->cset)
5320 {
5321 /* need convertion */
5322 for (roop_count = 0, tmp_key_hint = key_hint;
5323 roop_count < (int) table_share->keys; roop_count++, tmp_key_hint++)
5324 {
5325 tmp_key_hint->length(0);
5326 if (tmp_key_hint->append(spider_share->key_hint->ptr(),
5327 spider_share->key_hint->length(), system_charset_info))
5328 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5329 }
5330 } else {
5331 for (roop_count = 0, tmp_key_hint = key_hint;
5332 roop_count < (int) table_share->keys; roop_count++, tmp_key_hint++)
5333 {
5334 if (tmp_key_hint->copy(spider_share->key_hint[roop_count]))
5335 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5336 }
5337 }
5338 DBUG_RETURN(0);
5339}
5340
5341int spider_mysql_share::append_show_table_status()
5342{
5343 int roop_count;
5344 spider_string *str;
5345 uint dbton_id = spider_dbton_mysql.dbton_id;
5346 DBUG_ENTER("spider_mysql_append_show_table_status");
5347 if (!(show_table_status =
5348 new spider_string[2 * spider_share->all_link_count]))
5349 goto error;
5350
5351 for (roop_count = 0; roop_count < (int) spider_share->all_link_count;
5352 roop_count++)
5353 {
5354 show_table_status[0 + (2 * roop_count)].init_calc_mem(90);
5355 show_table_status[1 + (2 * roop_count)].init_calc_mem(91);
5356 if (spider_share->sql_dbton_ids[roop_count] != dbton_id)
5357 continue;
5358
5359 if (
5360 show_table_status[0 + (2 * roop_count)].reserve(
5361 SPIDER_SQL_SHOW_TABLE_STATUS_LEN +
5362 db_names_str[roop_count].length() +
5363 SPIDER_SQL_LIKE_LEN + table_names_str[roop_count].length() +
5364 ((SPIDER_SQL_NAME_QUOTE_LEN) * 2) +
5365 ((SPIDER_SQL_VALUE_QUOTE_LEN) * 2)) ||
5366 show_table_status[1 + (2 * roop_count)].reserve(
5367 SPIDER_SQL_SELECT_TABLES_STATUS_LEN +
5368 db_names_str[roop_count].length() +
5369 SPIDER_SQL_AND_LEN + SPIDER_SQL_TABLE_NAME_LEN + SPIDER_SQL_EQUAL_LEN +
5370 table_names_str[roop_count].length() +
5371 ((SPIDER_SQL_VALUE_QUOTE_LEN) * 4))
5372 )
5373 goto error;
5374 str = &show_table_status[0 + (2 * roop_count)];
5375 str->q_append(
5376 SPIDER_SQL_SHOW_TABLE_STATUS_STR, SPIDER_SQL_SHOW_TABLE_STATUS_LEN);
5377 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
5378 str->q_append(db_names_str[roop_count].ptr(),
5379 db_names_str[roop_count].length());
5380 str->q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
5381 str->q_append(SPIDER_SQL_LIKE_STR, SPIDER_SQL_LIKE_LEN);
5382 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5383 str->q_append(table_names_str[roop_count].ptr(),
5384 table_names_str[roop_count].length());
5385 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5386 str = &show_table_status[1 + (2 * roop_count)];
5387 str->q_append(
5388 SPIDER_SQL_SELECT_TABLES_STATUS_STR,
5389 SPIDER_SQL_SELECT_TABLES_STATUS_LEN);
5390 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5391 str->q_append(db_names_str[roop_count].ptr(),
5392 db_names_str[roop_count].length());
5393 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5394 str->q_append(SPIDER_SQL_AND_STR, SPIDER_SQL_AND_LEN);
5395 str->q_append(SPIDER_SQL_TABLE_NAME_STR, SPIDER_SQL_TABLE_NAME_LEN);
5396 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
5397 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5398 str->q_append(table_names_str[roop_count].ptr(),
5399 table_names_str[roop_count].length());
5400 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5401 }
5402 DBUG_RETURN(0);
5403
5404error:
5405 if (show_table_status)
5406 {
5407 delete [] show_table_status;
5408 show_table_status = NULL;
5409 }
5410 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5411}
5412
5413void spider_mysql_share::free_show_table_status()
5414{
5415 DBUG_ENTER("spider_mysql_free_show_table_status");
5416 if (show_table_status)
5417 {
5418 delete [] show_table_status;
5419 show_table_status = NULL;
5420 }
5421 DBUG_VOID_RETURN;
5422}
5423
5424int spider_mysql_share::append_show_records()
5425{
5426 int roop_count;
5427 spider_string *str;
5428 uint dbton_id = spider_dbton_mysql.dbton_id;
5429 DBUG_ENTER("spider_mysql_share::append_show_records");
5430 if (!(show_records = new spider_string[spider_share->all_link_count]))
5431 goto error;
5432
5433 for (roop_count = 0; roop_count < (int) spider_share->all_link_count;
5434 roop_count++)
5435 {
5436 show_records[roop_count].init_calc_mem(92);
5437 if (spider_share->sql_dbton_ids[roop_count] != dbton_id)
5438 continue;
5439
5440 if (
5441 show_records[roop_count].reserve(
5442 SPIDER_SQL_SHOW_RECORDS_LEN +
5443 db_names_str[roop_count].length() +
5444 SPIDER_SQL_DOT_LEN +
5445 table_names_str[roop_count].length() +
5446 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4)
5447 )
5448 goto error;
5449 str = &show_records[roop_count];
5450 str->q_append(SPIDER_SQL_SHOW_RECORDS_STR, SPIDER_SQL_SHOW_RECORDS_LEN);
5451 append_table_name(str, roop_count);
5452 }
5453 DBUG_RETURN(0);
5454
5455error:
5456 if (show_records)
5457 {
5458 delete [] show_records;
5459 show_records = NULL;
5460 }
5461 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5462}
5463
5464void spider_mysql_share::free_show_records()
5465{
5466 DBUG_ENTER("spider_mysql_share::free_show_records");
5467 if (show_records)
5468 {
5469 delete [] show_records;
5470 show_records = NULL;
5471 }
5472 DBUG_VOID_RETURN;
5473}
5474
5475int spider_mysql_share::append_show_index()
5476{
5477 int roop_count;
5478 spider_string *str;
5479 uint dbton_id = spider_dbton_mysql.dbton_id;
5480 DBUG_ENTER("spider_mysql_share::append_show_index");
5481 if (!(show_index = new spider_string[2 * spider_share->all_link_count]))
5482 goto error;
5483
5484 for (roop_count = 0; roop_count < (int) spider_share->all_link_count;
5485 roop_count++)
5486 {
5487 show_index[0 + (2 * roop_count)].init_calc_mem(93);
5488 show_index[1 + (2 * roop_count)].init_calc_mem(94);
5489 if (spider_share->sql_dbton_ids[roop_count] != dbton_id)
5490 continue;
5491
5492 if (
5493 show_index[0 + (2 * roop_count)].reserve(
5494 SPIDER_SQL_SHOW_INDEX_LEN + db_names_str[roop_count].length() +
5495 SPIDER_SQL_DOT_LEN +
5496 table_names_str[roop_count].length() +
5497 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4) ||
5498 show_index[1 + (2 * roop_count)].reserve(
5499 SPIDER_SQL_SELECT_STATISTICS_LEN +
5500 db_names_str[roop_count].length() +
5501 SPIDER_SQL_AND_LEN + SPIDER_SQL_TABLE_NAME_LEN + SPIDER_SQL_EQUAL_LEN +
5502 table_names_str[roop_count].length() +
5503 ((SPIDER_SQL_VALUE_QUOTE_LEN) * 4) +
5504 SPIDER_SQL_GROUP_LEN + SPIDER_SQL_COLUMN_NAME_LEN)
5505 )
5506 goto error;
5507 str = &show_index[0 + (2 * roop_count)];
5508 str->q_append(
5509 SPIDER_SQL_SHOW_INDEX_STR, SPIDER_SQL_SHOW_INDEX_LEN);
5510 append_table_name(str, roop_count);
5511 str = &show_index[1 + (2 * roop_count)];
5512 str->q_append(
5513 SPIDER_SQL_SELECT_STATISTICS_STR, SPIDER_SQL_SELECT_STATISTICS_LEN);
5514 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5515 str->q_append(db_names_str[roop_count].ptr(),
5516 db_names_str[roop_count].length());
5517 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5518 str->q_append(SPIDER_SQL_AND_STR, SPIDER_SQL_AND_LEN);
5519 str->q_append(SPIDER_SQL_TABLE_NAME_STR, SPIDER_SQL_TABLE_NAME_LEN);
5520 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
5521 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5522 str->q_append(table_names_str[roop_count].ptr(),
5523 table_names_str[roop_count].length());
5524 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5525 str->q_append(SPIDER_SQL_GROUP_STR, SPIDER_SQL_GROUP_LEN);
5526 str->q_append(SPIDER_SQL_COLUMN_NAME_STR, SPIDER_SQL_COLUMN_NAME_LEN);
5527 }
5528 DBUG_RETURN(0);
5529
5530error:
5531 if (show_index)
5532 {
5533 delete [] show_index;
5534 show_index = NULL;
5535 }
5536 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5537}
5538
5539void spider_mysql_share::free_show_index()
5540{
5541 DBUG_ENTER("spider_mysql_share::free_show_index");
5542 if (show_index)
5543 {
5544 delete [] show_index;
5545 show_index = NULL;
5546 }
5547 DBUG_VOID_RETURN;
5548}
5549
5550int spider_mysql_share::append_table_select()
5551{
5552 Field **field;
5553 uint field_length;
5554 spider_string *str = table_select;
5555 TABLE_SHARE *table_share = spider_share->table_share;
5556 DBUG_ENTER("spider_mysql_share::append_table_select");
5557 for (field = table_share->field; *field; field++)
5558 {
5559 field_length = column_name_str[(*field)->field_index].length();
5560 if (str->reserve(field_length +
5561 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
5562 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5563 append_column_name(str, (*field)->field_index);
5564 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
5565 }
5566 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
5567 DBUG_RETURN(append_from_with_adjusted_table_name(str, &table_select_pos));
5568}
5569
5570int spider_mysql_share::append_key_select(
5571 uint idx
5572) {
5573 KEY_PART_INFO *key_part;
5574 Field *field;
5575 uint part_num;
5576 uint field_length;
5577 spider_string *str = &key_select[idx];
5578 TABLE_SHARE *table_share = spider_share->table_share;
5579 const KEY *key_info = &table_share->key_info[idx];
5580 DBUG_ENTER("spider_mysql_share::append_key_select");
5581 for (key_part = key_info->key_part, part_num = 0;
5582 part_num < spider_user_defined_key_parts(key_info); key_part++, part_num++)
5583 {
5584 field = key_part->field;
5585 field_length = column_name_str[field->field_index].length();
5586 if (str->reserve(field_length +
5587 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
5588 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5589 append_column_name(str, field->field_index);
5590 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
5591 }
5592 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
5593 DBUG_RETURN(append_from_with_adjusted_table_name(str, &key_select_pos[idx]));
5594}
5595
5596bool spider_mysql_share::need_change_db_table_name()
5597{
5598 DBUG_ENTER("spider_mysql_share::need_change_db_table_name");
5599 DBUG_RETURN(!same_db_table_name);
5600}
5601
5602#ifdef SPIDER_HAS_DISCOVER_TABLE_STRUCTURE
5603int spider_mysql_share::discover_table_structure(
5604 SPIDER_TRX *trx,
5605 SPIDER_SHARE *spider_share,
5606 spider_string *str
5607) {
5608 int roop_count, error_num = HA_ERR_WRONG_COMMAND;
5609 char sql_buf[MAX_FIELD_WIDTH];
5610 spider_string sql_str(sql_buf, sizeof(sql_buf), system_charset_info);
5611 uint dbton_id = spider_dbton_mysql.dbton_id;
5612 uint strlen = str->length();
5613 DBUG_ENTER("spider_mysql_share::discover_table_structure");
5614 DBUG_PRINT("info",("spider this=%p", this));
5615 sql_str.init_calc_mem(228);
5616 for (roop_count = 0; roop_count < (int) spider_share->all_link_count;
5617 roop_count++)
5618 {
5619 if (spider_share->sql_dbton_ids[roop_count] != dbton_id)
5620 {
5621 DBUG_PRINT("info",("spider spider_share->sql_dbton_ids[%d]=%u",
5622 roop_count, spider_share->sql_dbton_ids[roop_count]));
5623 DBUG_PRINT("info",("spider dbton_id=%u", dbton_id));
5624 continue;
5625 }
5626
5627 str->length(strlen);
5628 sql_str.length(0);
5629 if (sql_str.reserve(
5630 SPIDER_SQL_SELECT_COLUMNS_LEN + db_names_str[roop_count].length() +
5631 SPIDER_SQL_AND_LEN + SPIDER_SQL_TABLE_NAME_LEN + SPIDER_SQL_EQUAL_LEN +
5632 table_names_str[roop_count].length() + SPIDER_SQL_ORDER_LEN +
5633 SPIDER_SQL_ORDINAL_POSITION_LEN +
5634 /* SPIDER_SQL_VALUE_QUOTE_LEN */ 8 +
5635 SPIDER_SQL_SEMICOLON_LEN +
5636 SPIDER_SQL_SHOW_INDEX_LEN + db_names_str[roop_count].length() +
5637 SPIDER_SQL_DOT_LEN + table_names_str[roop_count].length() +
5638 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4 +
5639 SPIDER_SQL_SEMICOLON_LEN +
5640 SPIDER_SQL_SHOW_TABLE_STATUS_LEN + db_names_str[roop_count].length() +
5641 SPIDER_SQL_LIKE_LEN + table_names_str[roop_count].length() +
5642 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4
5643 )) {
5644 DBUG_PRINT("info",("spider alloc sql_str error"));
5645 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5646 }
5647 sql_str.q_append(SPIDER_SQL_SELECT_COLUMNS_STR,
5648 SPIDER_SQL_SELECT_COLUMNS_LEN);
5649 sql_str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5650 sql_str.q_append(db_names_str[roop_count].ptr(),
5651 db_names_str[roop_count].length());
5652 sql_str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5653 sql_str.q_append(SPIDER_SQL_AND_STR, SPIDER_SQL_AND_LEN);
5654 sql_str.q_append(SPIDER_SQL_TABLE_NAME_STR, SPIDER_SQL_TABLE_NAME_LEN);
5655 sql_str.q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
5656 sql_str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5657 sql_str.q_append(table_names_str[roop_count].ptr(),
5658 table_names_str[roop_count].length());
5659 sql_str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5660 sql_str.q_append(SPIDER_SQL_ORDER_STR, SPIDER_SQL_ORDER_LEN);
5661 sql_str.q_append(SPIDER_SQL_ORDINAL_POSITION_STR,
5662 SPIDER_SQL_ORDINAL_POSITION_LEN);
5663 sql_str.q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
5664 sql_str.q_append(SPIDER_SQL_SHOW_INDEX_STR, SPIDER_SQL_SHOW_INDEX_LEN);
5665 append_table_name(&sql_str, roop_count);
5666 sql_str.q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
5667 sql_str.q_append(
5668 SPIDER_SQL_SHOW_TABLE_STATUS_STR, SPIDER_SQL_SHOW_TABLE_STATUS_LEN);
5669 sql_str.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
5670 sql_str.q_append(db_names_str[roop_count].ptr(),
5671 db_names_str[roop_count].length());
5672 sql_str.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
5673 sql_str.q_append(SPIDER_SQL_LIKE_STR, SPIDER_SQL_LIKE_LEN);
5674 sql_str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5675 sql_str.q_append(table_names_str[roop_count].ptr(),
5676 table_names_str[roop_count].length());
5677 sql_str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
5678
5679 SPIDER_CONN *conn;
5680 int need_mon;
5681 if (!(conn = spider_get_conn(
5682 spider_share, 0, spider_share->conn_keys[roop_count], trx, NULL, FALSE,
5683 FALSE, SPIDER_CONN_KIND_MYSQL, &error_num))
5684 ) {
5685 DBUG_RETURN(error_num);
5686 }
5687 if (!conn->disable_reconnect)
5688 {
5689 ha_spider tmp_spider;
5690 int need_mon = 0;
5691 uint tmp_conn_link_idx = 0;
5692 tmp_spider.trx = trx;
5693 tmp_spider.share = spider_share;
5694 tmp_spider.need_mons = &need_mon;
5695 tmp_spider.conn_link_idx = &tmp_conn_link_idx;
5696 if ((error_num = spider_db_ping(&tmp_spider, conn, 0)))
5697 {
5698 DBUG_PRINT("info",("spider spider_db_ping error"));
5699 continue;
5700 }
5701 }
5702 pthread_mutex_lock(&conn->mta_conn_mutex);
5703 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
5704 conn->need_mon = &need_mon;
5705 conn->mta_conn_mutex_lock_already = TRUE;
5706 conn->mta_conn_mutex_unlock_later = TRUE;
5707 spider_conn_set_timeout_from_share(conn, roop_count, trx->thd,
5708 spider_share);
5709 if (
5710 (error_num = spider_db_set_names_internal(trx, spider_share, conn,
5711 roop_count, &need_mon)) ||
5712 (
5713 spider_db_query(
5714 conn,
5715 sql_str.ptr(),
5716 sql_str.length(),
5717 -1,
5718 &need_mon) &&
5719 (error_num = spider_db_errorno(conn))
5720 )
5721 ) {
5722 DBUG_PRINT("info",("spider spider_get_trx error"));
5723 conn->mta_conn_mutex_lock_already = FALSE;
5724 conn->mta_conn_mutex_unlock_later = FALSE;
5725 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5726 pthread_mutex_unlock(&conn->mta_conn_mutex);
5727 continue;
5728 }
5729 st_spider_db_request_key request_key;
5730 request_key.spider_thread_id = trx->spider_thread_id;
5731 request_key.query_id = trx->thd->query_id;
5732 request_key.handler = NULL;
5733 request_key.request_id = 1;
5734 request_key.next = NULL;
5735 spider_db_result *res;
5736 /* get column list */
5737 if (!(res = conn->db_conn->store_result(NULL, &request_key, &error_num)))
5738 {
5739 if (error_num || (error_num = spider_db_errorno(conn)))
5740 {
5741 DBUG_PRINT("info",("spider column store error"));
5742 conn->mta_conn_mutex_lock_already = FALSE;
5743 conn->mta_conn_mutex_unlock_later = FALSE;
5744 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5745 pthread_mutex_unlock(&conn->mta_conn_mutex);
5746 continue;
5747 }
5748 /* no record */
5749 DBUG_PRINT("info",("spider column no record error"));
5750 conn->mta_conn_mutex_lock_already = FALSE;
5751 conn->mta_conn_mutex_unlock_later = FALSE;
5752 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5753 pthread_mutex_unlock(&conn->mta_conn_mutex);
5754 continue;
5755 }
5756 if ((error_num = res->fetch_columns_for_discover_table_structure(str,
5757 spider_share->access_charset)))
5758 {
5759 DBUG_PRINT("info",("spider column fetch error"));
5760 res->free_result();
5761 delete res;
5762 conn->mta_conn_mutex_lock_already = FALSE;
5763 conn->mta_conn_mutex_unlock_later = FALSE;
5764 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5765 pthread_mutex_unlock(&conn->mta_conn_mutex);
5766 my_printf_error(ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM,
5767 ER_SPIDER_REMOTE_TABLE_NOT_FOUND_STR, MYF(0),
5768 db_names_str[roop_count].ptr(),
5769 table_names_str[roop_count].ptr());
5770 error_num = ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM;
5771 continue;
5772 }
5773 res->free_result();
5774 delete res;
5775 if (conn->db_conn->next_result())
5776 {
5777 DBUG_PRINT("info",("spider single result error"));
5778 conn->mta_conn_mutex_lock_already = FALSE;
5779 conn->mta_conn_mutex_unlock_later = FALSE;
5780 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5781 pthread_mutex_unlock(&conn->mta_conn_mutex);
5782 continue;
5783 }
5784 /* get index list */
5785 if (!(res = conn->db_conn->store_result(NULL, &request_key, &error_num)))
5786 {
5787 if (error_num || (error_num = spider_db_errorno(conn)))
5788 {
5789 DBUG_PRINT("info",("spider index store error"));
5790 conn->mta_conn_mutex_lock_already = FALSE;
5791 conn->mta_conn_mutex_unlock_later = FALSE;
5792 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5793 pthread_mutex_unlock(&conn->mta_conn_mutex);
5794 continue;
5795 }
5796 /* no record */
5797 DBUG_PRINT("info",("spider index no record error"));
5798 conn->mta_conn_mutex_lock_already = FALSE;
5799 conn->mta_conn_mutex_unlock_later = FALSE;
5800 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5801 pthread_mutex_unlock(&conn->mta_conn_mutex);
5802 continue;
5803 }
5804 if ((error_num = res->fetch_index_for_discover_table_structure(str,
5805 spider_share->access_charset)))
5806 {
5807 DBUG_PRINT("info",("spider index fetch error"));
5808 res->free_result();
5809 delete res;
5810 conn->mta_conn_mutex_lock_already = FALSE;
5811 conn->mta_conn_mutex_unlock_later = FALSE;
5812 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5813 pthread_mutex_unlock(&conn->mta_conn_mutex);
5814 continue;
5815 }
5816 res->free_result();
5817 delete res;
5818 if (conn->db_conn->next_result())
5819 {
5820 DBUG_PRINT("info",("spider dual result error"));
5821 conn->mta_conn_mutex_lock_already = FALSE;
5822 conn->mta_conn_mutex_unlock_later = FALSE;
5823 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5824 pthread_mutex_unlock(&conn->mta_conn_mutex);
5825 continue;
5826 }
5827 /* get table info */
5828 if (!(res = conn->db_conn->store_result(NULL, &request_key, &error_num)))
5829 {
5830 if (error_num || (error_num = spider_db_errorno(conn)))
5831 {
5832 DBUG_PRINT("info",("spider table store error"));
5833 conn->mta_conn_mutex_lock_already = FALSE;
5834 conn->mta_conn_mutex_unlock_later = FALSE;
5835 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5836 pthread_mutex_unlock(&conn->mta_conn_mutex);
5837 continue;
5838 }
5839 /* no record */
5840 DBUG_PRINT("info",("spider table no record error"));
5841 conn->mta_conn_mutex_lock_already = FALSE;
5842 conn->mta_conn_mutex_unlock_later = FALSE;
5843 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5844 pthread_mutex_unlock(&conn->mta_conn_mutex);
5845 continue;
5846 }
5847 if ((error_num = res->fetch_table_for_discover_table_structure(str,
5848 spider_share, spider_share->access_charset)))
5849 {
5850 DBUG_PRINT("info",("spider table fetch error"));
5851 res->free_result();
5852 delete res;
5853 conn->mta_conn_mutex_lock_already = FALSE;
5854 conn->mta_conn_mutex_unlock_later = FALSE;
5855 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5856 pthread_mutex_unlock(&conn->mta_conn_mutex);
5857 continue;
5858 }
5859 res->free_result();
5860 delete res;
5861 conn->mta_conn_mutex_lock_already = FALSE;
5862 conn->mta_conn_mutex_unlock_later = FALSE;
5863 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
5864 pthread_mutex_unlock(&conn->mta_conn_mutex);
5865 if (!error_num)
5866 break;
5867 }
5868 DBUG_RETURN(error_num);
5869}
5870#endif
5871
5872spider_mysql_handler::spider_mysql_handler(
5873 ha_spider *spider,
5874 spider_mysql_share *db_share
5875) : spider_db_handler(
5876 spider,
5877 db_share
5878),
5879 where_pos(0),
5880 order_pos(0),
5881 limit_pos(0),
5882 table_name_pos(0),
5883 ha_read_pos(0),
5884 ha_next_pos(0),
5885 ha_where_pos(0),
5886 ha_limit_pos(0),
5887 ha_table_name_pos(0),
5888 insert_pos(0),
5889 insert_table_name_pos(0),
5890 upd_tmp_tbl(NULL),
5891 tmp_sql_pos1(0),
5892 tmp_sql_pos2(0),
5893 tmp_sql_pos3(0),
5894 tmp_sql_pos4(0),
5895 tmp_sql_pos5(0),
5896 reading_from_bulk_tmp_table(FALSE),
5897 union_table_name_pos_first(NULL),
5898 union_table_name_pos_current(NULL),
5899 mysql_share(db_share),
5900 link_for_hash(NULL)
5901{
5902 DBUG_ENTER("spider_mysql_handler::spider_mysql_handler");
5903 DBUG_PRINT("info",("spider this=%p", this));
5904 spider_alloc_calc_mem_init(mem_calc, 183);
5905 spider_alloc_calc_mem(spider_current_trx, mem_calc, sizeof(*this));
5906 DBUG_VOID_RETURN;
5907}
5908
5909spider_mysql_handler::~spider_mysql_handler()
5910{
5911 DBUG_ENTER("spider_mysql_handler::~spider_mysql_handler");
5912 DBUG_PRINT("info",("spider this=%p", this));
5913 while (union_table_name_pos_first)
5914 {
5915 SPIDER_INT_HLD *tmp_pos = union_table_name_pos_first;
5916 union_table_name_pos_first = tmp_pos->next;
5917 spider_free(spider_current_trx, tmp_pos, MYF(0));
5918 }
5919 if (link_for_hash)
5920 {
5921 spider_free(spider_current_trx, link_for_hash, MYF(0));
5922 }
5923 spider_free_mem_calc(spider_current_trx, mem_calc_id, sizeof(*this));
5924 DBUG_VOID_RETURN;
5925}
5926
5927int spider_mysql_handler::init()
5928{
5929 uint roop_count;
5930 THD *thd = spider->trx->thd;
5931 st_spider_share *share = spider->share;
5932 int init_sql_alloc_size =
5933 spider_param_init_sql_alloc_size(thd, share->init_sql_alloc_size);
5934 TABLE *table = spider->get_table();
5935 DBUG_ENTER("spider_mysql_handler::init");
5936 DBUG_PRINT("info",("spider this=%p", this));
5937 sql.init_calc_mem(59);
5938 sql_part.init_calc_mem(60);
5939 sql_part2.init_calc_mem(61);
5940 ha_sql.init_calc_mem(62);
5941 insert_sql.init_calc_mem(64);
5942 update_sql.init_calc_mem(65);
5943 tmp_sql.init_calc_mem(66);
5944 dup_update_sql.init_calc_mem(166);
5945 if (
5946 (sql.real_alloc(init_sql_alloc_size)) ||
5947 (insert_sql.real_alloc(init_sql_alloc_size)) ||
5948 (update_sql.real_alloc(init_sql_alloc_size)) ||
5949 (tmp_sql.real_alloc(init_sql_alloc_size))
5950 ) {
5951 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5952 }
5953 sql.set_charset(share->access_charset);
5954 sql_part.set_charset(share->access_charset);
5955 ha_sql.set_charset(share->access_charset);
5956 insert_sql.set_charset(share->access_charset);
5957 update_sql.set_charset(share->access_charset);
5958 tmp_sql.set_charset(share->access_charset);
5959 upd_tmp_tbl_prm.init();
5960 upd_tmp_tbl_prm.field_count = 1;
5961 if (!(link_for_hash = (SPIDER_LINK_FOR_HASH *)
5962 spider_bulk_alloc_mem(spider_current_trx, 141,
5963 __func__, __FILE__, __LINE__, MYF(MY_WME | MY_ZEROFILL),
5964 &link_for_hash,
5965 sizeof(SPIDER_LINK_FOR_HASH) * share->link_count,
5966 &minimum_select_bitmap,
5967 table ? sizeof(uchar) * no_bytes_in_map(table->read_set) : 0,
5968 NullS))
5969 ) {
5970 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5971 }
5972 for (roop_count = 0; roop_count < share->link_count; roop_count++)
5973 {
5974 link_for_hash[roop_count].spider = spider;
5975 link_for_hash[roop_count].link_idx = roop_count;
5976 link_for_hash[roop_count].db_table_str =
5977 &mysql_share->db_table_str[roop_count];
5978#ifdef SPIDER_HAS_HASH_VALUE_TYPE
5979 link_for_hash[roop_count].db_table_str_hash_value =
5980 mysql_share->db_table_str_hash_value[roop_count];
5981#endif
5982 }
5983#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5984 hs_upds.init();
5985#endif
5986 DBUG_RETURN(0);
5987}
5988
5989
5990int spider_mysql_handler::append_index_hint(
5991 spider_string *str,
5992 int link_idx,
5993 ulong sql_type
5994 )
5995{
5996 List<Index_hint> *index_hints = spider_get_index_hints(spider);
5997 List_iterator <Index_hint> iter(*index_hints);
5998 Index_hint *hint;
5999// THD *thd = current_thd;
6000 int error_num = 0;
6001 DBUG_ENTER("spider_mysql_handler::append_index_hint");
6002 DBUG_PRINT("info",("spider this=%p", this));
6003
6004 while(index_hints && (hint = iter++))
6005 {
6006// hint->print(thd, str);
6007 if (sql_type != SPIDER_SQL_TYPE_HANDLER)
6008 {
6009 switch(hint->type)
6010 {
6011 case INDEX_HINT_IGNORE:
6012 if (str->reserve(hint->key_name.length+ SPIDER_SQL_INDEX_IGNORE_LEN + SPIDER_SQL_OPEN_PAREN_LEN + SPIDER_SQL_CLOSE_PAREN_LEN))
6013 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6014 str->q_append(SPIDER_SQL_INDEX_IGNORE_STR,SPIDER_SQL_INDEX_IGNORE_LEN);
6015 str->q_append(SPIDER_SQL_OPEN_PAREN_STR,SPIDER_SQL_OPEN_PAREN_LEN);
6016 str->q_append(hint->key_name.str, hint->key_name.length);
6017 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,SPIDER_SQL_CLOSE_PAREN_LEN);
6018 break;
6019 case INDEX_HINT_USE:
6020 if (str->reserve(hint->key_name.length+ SPIDER_SQL_INDEX_USE_LEN + SPIDER_SQL_OPEN_PAREN_LEN + SPIDER_SQL_CLOSE_PAREN_LEN))
6021 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6022 str->q_append(SPIDER_SQL_INDEX_USE_STR,SPIDER_SQL_INDEX_USE_LEN);
6023 str->q_append(SPIDER_SQL_OPEN_PAREN_STR,SPIDER_SQL_OPEN_PAREN_LEN);
6024 str->q_append(hint->key_name.str, hint->key_name.length);
6025 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,SPIDER_SQL_CLOSE_PAREN_LEN);
6026 break;
6027 case INDEX_HINT_FORCE:
6028 if (str->reserve(hint->key_name.length+ SPIDER_SQL_INDEX_FORCE_LEN + SPIDER_SQL_OPEN_PAREN_LEN + SPIDER_SQL_CLOSE_PAREN_LEN))
6029 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6030 str->q_append(SPIDER_SQL_INDEX_FORCE_STR,SPIDER_SQL_INDEX_FORCE_LEN);
6031 str->q_append(SPIDER_SQL_OPEN_PAREN_STR,SPIDER_SQL_OPEN_PAREN_LEN);
6032 str->q_append(hint->key_name.str, hint->key_name.length);
6033 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR,SPIDER_SQL_CLOSE_PAREN_LEN);
6034 break;
6035 default:
6036 // SPIDER_SQL_COMMA_STR
6037 break;
6038 }
6039 }
6040 }
6041 DBUG_RETURN(error_num);
6042}
6043
6044int spider_mysql_handler::append_table_name_with_adjusting(
6045 spider_string *str,
6046 int link_idx,
6047 ulong sql_type
6048) {
6049 int error_num = 0;
6050 DBUG_ENTER("spider_mysql_handler::append_table_name_with_adjusting");
6051 DBUG_PRINT("info",("spider this=%p", this));
6052 if (sql_type == SPIDER_SQL_TYPE_HANDLER)
6053 {
6054 str->q_append(spider->m_handler_cid[link_idx], SPIDER_SQL_HANDLER_CID_LEN);
6055 } else {
6056 error_num = mysql_share->append_table_name_with_adjusting(str,
6057 spider->conn_link_idx[link_idx]);
6058 }
6059 DBUG_RETURN(error_num);
6060}
6061
6062int spider_mysql_handler::append_key_column_types(
6063 const key_range *start_key,
6064 spider_string *str
6065) {
6066 SPIDER_RESULT_LIST *result_list = &spider->result_list;
6067 KEY *key_info = result_list->key_info;
6068 uint key_name_length, key_count;
6069 key_part_map full_key_part_map =
6070 make_prev_keypart_map(spider_user_defined_key_parts(key_info));
6071 key_part_map start_key_part_map;
6072 KEY_PART_INFO *key_part;
6073 Field *field;
6074 char tmp_buf[MAX_FIELD_WIDTH];
6075 spider_string tmp_str(tmp_buf, sizeof(tmp_buf), system_charset_info);
6076 DBUG_ENTER("spider_mysql_handler::append_key_column_types");
6077 DBUG_PRINT("info",("spider this=%p", this));
6078 tmp_str.init_calc_mem(115);
6079
6080 start_key_part_map = start_key->keypart_map & full_key_part_map;
6081 DBUG_PRINT("info", ("spider spider_user_defined_key_parts=%u",
6082 spider_user_defined_key_parts(key_info)));
6083 DBUG_PRINT("info", ("spider full_key_part_map=%lu", full_key_part_map));
6084 DBUG_PRINT("info", ("spider start_key_part_map=%lu", start_key_part_map));
6085
6086 if (!start_key_part_map)
6087 DBUG_RETURN(0);
6088
6089 for (
6090 key_part = key_info->key_part,
6091 key_count = 0;
6092 start_key_part_map;
6093 start_key_part_map >>= 1,
6094 key_part++,
6095 key_count++
6096 ) {
6097 field = key_part->field;
6098 key_name_length = my_sprintf(tmp_buf, (tmp_buf, "c%u", key_count));
6099 if (str->reserve(key_name_length + SPIDER_SQL_SPACE_LEN))
6100 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6101 str->q_append(tmp_buf, key_name_length);
6102 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
6103
6104 if (tmp_str.ptr() != tmp_buf)
6105 tmp_str.set(tmp_buf, sizeof(tmp_buf), system_charset_info);
6106 else
6107 tmp_str.set_charset(system_charset_info);
6108 field->sql_type(*tmp_str.get_str());
6109 tmp_str.mem_calc();
6110 str->append(tmp_str);
6111 if (field->has_charset())
6112 {
6113 CHARSET_INFO *cs = field->charset();
6114 uint coll_length = strlen(cs->name);
6115 if (str->reserve(SPIDER_SQL_COLLATE_LEN + coll_length))
6116 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6117 str->q_append(SPIDER_SQL_COLLATE_STR, SPIDER_SQL_COLLATE_LEN);
6118 str->q_append(cs->name, coll_length);
6119 }
6120
6121 if (str->reserve(SPIDER_SQL_COMMA_LEN))
6122 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6123 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
6124 }
6125 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
6126
6127 DBUG_RETURN(0);
6128}
6129
6130int spider_mysql_handler::append_key_join_columns_for_bka(
6131 const key_range *start_key,
6132 spider_string *str,
6133 const char **table_aliases,
6134 uint *table_alias_lengths
6135) {
6136 KEY *key_info = spider->result_list.key_info;
6137 uint length, key_name_length, key_count;
6138 key_part_map full_key_part_map =
6139 make_prev_keypart_map(spider_user_defined_key_parts(key_info));
6140 key_part_map start_key_part_map;
6141 KEY_PART_INFO *key_part;
6142 Field *field;
6143 char tmp_buf[MAX_FIELD_WIDTH];
6144 bool start_where = ((int) str->length() == where_pos);
6145 DBUG_ENTER("spider_mysql_handler::append_key_join_columns_for_bka");
6146 DBUG_PRINT("info",("spider this=%p", this));
6147 start_key_part_map = start_key->keypart_map & full_key_part_map;
6148 DBUG_PRINT("info", ("spider spider_user_defined_key_parts=%u",
6149 spider_user_defined_key_parts(key_info)));
6150 DBUG_PRINT("info", ("spider full_key_part_map=%lu", full_key_part_map));
6151 DBUG_PRINT("info", ("spider start_key_part_map=%lu", start_key_part_map));
6152
6153 if (!start_key_part_map)
6154 DBUG_RETURN(0);
6155
6156 if (start_where)
6157 {
6158 if (str->reserve(SPIDER_SQL_WHERE_LEN))
6159 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6160 str->q_append(SPIDER_SQL_WHERE_STR, SPIDER_SQL_WHERE_LEN);
6161 } else {
6162 if (str->reserve(SPIDER_SQL_AND_LEN))
6163 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6164 str->q_append(SPIDER_SQL_AND_STR, SPIDER_SQL_AND_LEN);
6165 }
6166
6167 for (
6168 key_part = key_info->key_part,
6169 key_count = 0;
6170 start_key_part_map;
6171 start_key_part_map >>= 1,
6172 key_part++,
6173 key_count++
6174 ) {
6175 field = key_part->field;
6176 key_name_length =
6177 mysql_share->column_name_str[field->field_index].length();
6178 length = my_sprintf(tmp_buf, (tmp_buf, "c%u", key_count));
6179 if (str->reserve(length + table_alias_lengths[0] + key_name_length +
6180 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
6181 table_alias_lengths[1] + SPIDER_SQL_PF_EQUAL_LEN + SPIDER_SQL_AND_LEN))
6182 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6183 str->q_append(table_aliases[0], table_alias_lengths[0]);
6184 str->q_append(tmp_buf, length);
6185 str->q_append(SPIDER_SQL_PF_EQUAL_STR, SPIDER_SQL_PF_EQUAL_LEN);
6186 str->q_append(table_aliases[1], table_alias_lengths[1]);
6187 mysql_share->append_column_name(str, field->field_index);
6188 str->q_append(SPIDER_SQL_AND_STR, SPIDER_SQL_AND_LEN);
6189 }
6190 str->length(str->length() - SPIDER_SQL_AND_LEN);
6191 DBUG_RETURN(0);
6192}
6193
6194int spider_mysql_handler::append_tmp_table_and_sql_for_bka(
6195 const key_range *start_key
6196) {
6197 int error_num;
6198 DBUG_ENTER("spider_mysql_handler::append_tmp_table_and_sql_for_bka");
6199 DBUG_PRINT("info",("spider this=%p", this));
6200 char tmp_table_name[MAX_FIELD_WIDTH * 2],
6201 tgt_table_name[MAX_FIELD_WIDTH * 2];
6202 int tmp_table_name_length;
6203 spider_string tgt_table_name_str(tgt_table_name, MAX_FIELD_WIDTH * 2,
6204 mysql_share->db_names_str[0].charset());
6205 const char *table_names[2], *table_aliases[2], *table_dot_aliases[2];
6206 uint table_name_lengths[2], table_alias_lengths[2],
6207 table_dot_alias_lengths[2];
6208 tgt_table_name_str.init_calc_mem(99);
6209 tgt_table_name_str.length(0);
6210 create_tmp_bka_table_name(tmp_table_name, &tmp_table_name_length,
6211 first_link_idx);
6212 if ((error_num = append_table_name_with_adjusting(&tgt_table_name_str,
6213 first_link_idx, SPIDER_SQL_TYPE_SELECT_SQL)))
6214 {
6215 DBUG_RETURN(error_num);
6216 }
6217 table_names[0] = tmp_table_name;
6218 table_names[1] = tgt_table_name_str.c_ptr_safe();
6219 table_name_lengths[0] = tmp_table_name_length;
6220 table_name_lengths[1] = tgt_table_name_str.length();
6221 table_aliases[0] = SPIDER_SQL_A_STR;
6222 table_aliases[1] = SPIDER_SQL_B_STR;
6223 table_alias_lengths[0] = SPIDER_SQL_A_LEN;
6224 table_alias_lengths[1] = SPIDER_SQL_B_LEN;
6225 table_dot_aliases[0] = SPIDER_SQL_A_DOT_STR;
6226 table_dot_aliases[1] = SPIDER_SQL_B_DOT_STR;
6227 table_dot_alias_lengths[0] = SPIDER_SQL_A_DOT_LEN;
6228 table_dot_alias_lengths[1] = SPIDER_SQL_B_DOT_LEN;
6229 if (
6230 (error_num = append_drop_tmp_bka_table(
6231 &tmp_sql, tmp_table_name, tmp_table_name_length,
6232 &tmp_sql_pos1, &tmp_sql_pos5, TRUE)) ||
6233 (error_num = append_create_tmp_bka_table(
6234 start_key,
6235 &tmp_sql, tmp_table_name,
6236 tmp_table_name_length,
6237 &tmp_sql_pos2, spider->share->table_share->table_charset)) ||
6238 (error_num = append_insert_tmp_bka_table(
6239 start_key,
6240 &tmp_sql, tmp_table_name,
6241 tmp_table_name_length, &tmp_sql_pos3))
6242 )
6243 DBUG_RETURN(error_num);
6244 tmp_sql_pos4 = tmp_sql.length();
6245 if ((error_num = spider_db_append_select(spider)))
6246 DBUG_RETURN(error_num);
6247 if (sql.reserve(SPIDER_SQL_A_DOT_LEN + SPIDER_SQL_ID_LEN +
6248 SPIDER_SQL_COMMA_LEN))
6249 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6250 sql.q_append(SPIDER_SQL_A_DOT_STR, SPIDER_SQL_A_DOT_LEN);
6251 sql.q_append(SPIDER_SQL_ID_STR, SPIDER_SQL_ID_LEN);
6252 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
6253 if (
6254 (error_num = append_select_columns_with_alias(&sql,
6255 SPIDER_SQL_B_DOT_STR, SPIDER_SQL_B_DOT_LEN)) ||
6256 (error_num = spider_db_mysql_utility.append_from_with_alias(&sql,
6257 table_names, table_name_lengths,
6258 table_aliases, table_alias_lengths, 2,
6259 &table_name_pos, FALSE))
6260 )
6261 DBUG_RETURN(error_num);
6262 if (
6263 mysql_share->key_hint &&
6264 (error_num = spider_db_append_hint_after_table(spider,
6265 &sql, &mysql_share->key_hint[spider->active_index]))
6266 )
6267 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6268 where_pos = sql.length();
6269 if (
6270 (error_num = append_key_join_columns_for_bka(
6271 start_key, &sql,
6272 table_dot_aliases, table_dot_alias_lengths)) ||
6273 (error_num = append_condition_part(
6274 SPIDER_SQL_B_DOT_STR, SPIDER_SQL_B_DOT_LEN,
6275 SPIDER_SQL_TYPE_SELECT_SQL, FALSE))
6276 )
6277 DBUG_RETURN(error_num);
6278 if (spider->result_list.direct_order_limit)
6279 {
6280 if ((error_num = append_key_order_for_direct_order_limit_with_alias(&sql,
6281 SPIDER_SQL_B_DOT_STR, SPIDER_SQL_B_DOT_LEN)))
6282 DBUG_RETURN(error_num);
6283 }
6284#ifdef HANDLER_HAS_DIRECT_AGGREGATE
6285 else if (spider->result_list.direct_aggregate)
6286 {
6287 if ((error_num =
6288 append_group_by(&sql, SPIDER_SQL_B_DOT_STR, SPIDER_SQL_B_DOT_LEN)))
6289 DBUG_RETURN(error_num);
6290 }
6291#endif
6292
6293 DBUG_RETURN(0);
6294}
6295
6296int spider_mysql_handler::reuse_tmp_table_and_sql_for_bka()
6297{
6298 DBUG_ENTER("spider_mysql_handler::reuse_tmp_table_and_sql_for_bka");
6299 DBUG_PRINT("info",("spider this=%p", this));
6300 tmp_sql.length(tmp_sql_pos4);
6301 sql.length(limit_pos);
6302 ha_sql.length(ha_limit_pos);
6303 DBUG_RETURN(0);
6304}
6305
6306void spider_mysql_handler::create_tmp_bka_table_name(
6307 char *tmp_table_name,
6308 int *tmp_table_name_length,
6309 int link_idx
6310) {
6311 uint adjust_length, length;
6312 DBUG_ENTER("spider_mysql_handler::create_tmp_bka_table_name");
6313 if (spider_param_bka_table_name_type(current_thd,
6314 mysql_share->spider_share->
6315 bka_table_name_types[spider->conn_link_idx[link_idx]]) == 1)
6316 {
6317 adjust_length =
6318 mysql_share->db_nm_max_length -
6319 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].length() +
6320 mysql_share->table_nm_max_length -
6321 mysql_share->table_names_str[spider->conn_link_idx[link_idx]].length();
6322 *tmp_table_name_length = mysql_share->db_nm_max_length +
6323 mysql_share->table_nm_max_length;
6324 memset(tmp_table_name, ' ', adjust_length);
6325 tmp_table_name += adjust_length;
6326 memcpy(tmp_table_name, mysql_share->db_names_str[link_idx].c_ptr(),
6327 mysql_share->db_names_str[link_idx].length());
6328 tmp_table_name += mysql_share->db_names_str[link_idx].length();
6329 length = my_sprintf(tmp_table_name, (tmp_table_name,
6330 "%s%s%p%s", SPIDER_SQL_DOT_STR, SPIDER_SQL_TMP_BKA_STR, spider,
6331 SPIDER_SQL_UNDERSCORE_STR));
6332 *tmp_table_name_length += length;
6333 tmp_table_name += length;
6334 memcpy(tmp_table_name,
6335 mysql_share->table_names_str[spider->conn_link_idx[link_idx]].c_ptr(),
6336 mysql_share->table_names_str[spider->conn_link_idx[link_idx]].length());
6337 } else {
6338 adjust_length =
6339 mysql_share->db_nm_max_length -
6340 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].length();
6341 *tmp_table_name_length = mysql_share->db_nm_max_length;
6342 memset(tmp_table_name, ' ', adjust_length);
6343 tmp_table_name += adjust_length;
6344 memcpy(tmp_table_name, mysql_share->db_names_str[link_idx].c_ptr(),
6345 mysql_share->db_names_str[link_idx].length());
6346 tmp_table_name += mysql_share->db_names_str[link_idx].length();
6347 length = my_sprintf(tmp_table_name, (tmp_table_name,
6348 "%s%s%p", SPIDER_SQL_DOT_STR, SPIDER_SQL_TMP_BKA_STR, spider));
6349 *tmp_table_name_length += length;
6350 }
6351 DBUG_VOID_RETURN;
6352}
6353
6354int spider_mysql_handler::append_create_tmp_bka_table(
6355 const key_range *start_key,
6356 spider_string *str,
6357 char *tmp_table_name,
6358 int tmp_table_name_length,
6359 int *db_name_pos,
6360 CHARSET_INFO *table_charset
6361) {
6362 int error_num;
6363 SPIDER_SHARE *share = spider->share;
6364 THD *thd = spider->trx->thd;
6365 char *bka_engine = spider_param_bka_engine(thd, share->bka_engine);
6366 uint bka_engine_length = strlen(bka_engine),
6367 cset_length = strlen(table_charset->csname),
6368 coll_length = strlen(table_charset->name);
6369 DBUG_ENTER("spider_mysql_handler::append_create_tmp_bka_table");
6370 if (str->reserve(SPIDER_SQL_CREATE_TMP_LEN + tmp_table_name_length +
6371 SPIDER_SQL_OPEN_PAREN_LEN + SPIDER_SQL_ID_LEN + SPIDER_SQL_ID_TYPE_LEN +
6372 SPIDER_SQL_COMMA_LEN))
6373 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6374 str->q_append(SPIDER_SQL_CREATE_TMP_STR, SPIDER_SQL_CREATE_TMP_LEN);
6375 *db_name_pos = str->length();
6376 str->q_append(tmp_table_name, tmp_table_name_length);
6377 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
6378 str->q_append(SPIDER_SQL_ID_STR, SPIDER_SQL_ID_LEN);
6379 str->q_append(SPIDER_SQL_ID_TYPE_STR, SPIDER_SQL_ID_TYPE_LEN);
6380 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
6381 if ((error_num = append_key_column_types(start_key, str)))
6382 DBUG_RETURN(error_num);
6383 if (str->reserve(SPIDER_SQL_ENGINE_LEN + bka_engine_length +
6384 SPIDER_SQL_DEF_CHARSET_LEN + cset_length + SPIDER_SQL_COLLATE_LEN +
6385 coll_length + SPIDER_SQL_SEMICOLON_LEN))
6386 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6387 str->q_append(SPIDER_SQL_ENGINE_STR, SPIDER_SQL_ENGINE_LEN);
6388 str->q_append(bka_engine, bka_engine_length);
6389 str->q_append(SPIDER_SQL_DEF_CHARSET_STR, SPIDER_SQL_DEF_CHARSET_LEN);
6390 str->q_append(table_charset->csname, cset_length);
6391 str->q_append(SPIDER_SQL_COLLATE_STR, SPIDER_SQL_COLLATE_LEN);
6392 str->q_append(table_charset->name, coll_length);
6393 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
6394 DBUG_RETURN(0);
6395}
6396
6397int spider_mysql_handler::append_drop_tmp_bka_table(
6398 spider_string *str,
6399 char *tmp_table_name,
6400 int tmp_table_name_length,
6401 int *db_name_pos,
6402 int *drop_table_end_pos,
6403 bool with_semicolon
6404) {
6405 DBUG_ENTER("spider_mysql_handler::append_drop_tmp_bka_table");
6406 if (str->reserve(SPIDER_SQL_DROP_TMP_LEN + tmp_table_name_length +
6407 (with_semicolon ? SPIDER_SQL_SEMICOLON_LEN : 0)))
6408 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6409 str->q_append(SPIDER_SQL_DROP_TMP_STR, SPIDER_SQL_DROP_TMP_LEN);
6410 *db_name_pos = str->length();
6411 str->q_append(tmp_table_name, tmp_table_name_length);
6412 *drop_table_end_pos = str->length();
6413 if (with_semicolon)
6414 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
6415 DBUG_RETURN(0);
6416}
6417
6418int spider_mysql_handler::append_insert_tmp_bka_table(
6419 const key_range *start_key,
6420 spider_string *str,
6421 char *tmp_table_name,
6422 int tmp_table_name_length,
6423 int *db_name_pos
6424) {
6425 int error_num;
6426 DBUG_ENTER("spider_mysql_handler::append_insert_tmp_bka_table");
6427 if (str->reserve(SPIDER_SQL_INSERT_LEN + SPIDER_SQL_INTO_LEN +
6428 tmp_table_name_length + SPIDER_SQL_OPEN_PAREN_LEN + SPIDER_SQL_ID_LEN +
6429 SPIDER_SQL_COMMA_LEN))
6430 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6431 str->q_append(SPIDER_SQL_INSERT_STR, SPIDER_SQL_INSERT_LEN);
6432 str->q_append(SPIDER_SQL_INTO_STR, SPIDER_SQL_INTO_LEN);
6433 *db_name_pos = str->length();
6434 str->q_append(tmp_table_name, tmp_table_name_length);
6435 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
6436 str->q_append(SPIDER_SQL_ID_STR, SPIDER_SQL_ID_LEN);
6437 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
6438 if ((error_num = spider_db_append_key_columns(start_key, spider, str)))
6439 DBUG_RETURN(error_num);
6440 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN + SPIDER_SQL_VALUES_LEN +
6441 SPIDER_SQL_OPEN_PAREN_LEN))
6442 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6443 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
6444 str->q_append(SPIDER_SQL_VALUES_STR, SPIDER_SQL_VALUES_LEN);
6445 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
6446 DBUG_RETURN(0);
6447}
6448
6449int spider_mysql_handler::append_union_table_and_sql_for_bka(
6450 const key_range *start_key
6451) {
6452 int error_num;
6453 DBUG_ENTER("spider_mysql_handler::append_union_table_and_sql_for_bka");
6454 DBUG_PRINT("info",("spider this=%p", this));
6455 char tgt_table_name[MAX_FIELD_WIDTH * 2];
6456 spider_string tgt_table_name_str(tgt_table_name, MAX_FIELD_WIDTH * 2,
6457 mysql_share->db_names_str[0].charset());
6458 const char *table_names[2], *table_aliases[2], *table_dot_aliases[2];
6459 uint table_name_lengths[2], table_alias_lengths[2],
6460 table_dot_alias_lengths[2];
6461 tgt_table_name_str.init_calc_mem(233);
6462 tgt_table_name_str.length(0);
6463 if ((error_num = append_table_name_with_adjusting(&tgt_table_name_str,
6464 first_link_idx, SPIDER_SQL_TYPE_SELECT_SQL)))
6465 {
6466 DBUG_RETURN(error_num);
6467 }
6468 table_names[0] = "";
6469 table_names[1] = tgt_table_name_str.c_ptr_safe();
6470 table_name_lengths[0] = 0;
6471 table_name_lengths[1] = tgt_table_name_str.length();
6472 table_aliases[0] = SPIDER_SQL_A_STR;
6473 table_aliases[1] = SPIDER_SQL_B_STR;
6474 table_alias_lengths[0] = SPIDER_SQL_A_LEN;
6475 table_alias_lengths[1] = SPIDER_SQL_B_LEN;
6476 table_dot_aliases[0] = SPIDER_SQL_A_DOT_STR;
6477 table_dot_aliases[1] = SPIDER_SQL_B_DOT_STR;
6478 table_dot_alias_lengths[0] = SPIDER_SQL_A_DOT_LEN;
6479 table_dot_alias_lengths[1] = SPIDER_SQL_B_DOT_LEN;
6480
6481 if ((error_num = spider_db_append_select(spider)))
6482 DBUG_RETURN(error_num);
6483 if (sql.reserve(SPIDER_SQL_A_DOT_LEN + SPIDER_SQL_ID_LEN +
6484 SPIDER_SQL_COMMA_LEN))
6485 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6486 sql.q_append(SPIDER_SQL_A_DOT_STR, SPIDER_SQL_A_DOT_LEN);
6487 sql.q_append(SPIDER_SQL_ID_STR, SPIDER_SQL_ID_LEN);
6488 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
6489 if ((error_num = append_select_columns_with_alias(&sql,
6490 SPIDER_SQL_B_DOT_STR, SPIDER_SQL_B_DOT_LEN)))
6491 DBUG_RETURN(error_num);
6492 if (sql.reserve(SPIDER_SQL_FROM_LEN + (SPIDER_SQL_OPEN_PAREN_LEN * 2)))
6493 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6494 sql.q_append(SPIDER_SQL_FROM_STR, SPIDER_SQL_FROM_LEN);
6495 sql.q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
6496 sql.q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
6497 tmp_sql_pos1 = sql.length();
6498
6499 if (
6500 (error_num = spider_db_mysql_utility.append_from_with_alias(&tmp_sql,
6501 table_names, table_name_lengths,
6502 table_aliases, table_alias_lengths, 2,
6503 &table_name_pos, FALSE))
6504 )
6505 DBUG_RETURN(error_num);
6506 if (
6507 mysql_share->key_hint &&
6508 (error_num = spider_db_append_hint_after_table(spider,
6509 &tmp_sql, &mysql_share->key_hint[spider->active_index]))
6510 )
6511 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6512 where_pos = tmp_sql.length();
6513 if (
6514 (error_num = append_key_join_columns_for_bka(
6515 start_key, &tmp_sql,
6516 table_dot_aliases, table_dot_alias_lengths)) ||
6517 (error_num = append_condition_part(
6518 SPIDER_SQL_B_DOT_STR, SPIDER_SQL_B_DOT_LEN,
6519 SPIDER_SQL_TYPE_TMP_SQL, FALSE))
6520 )
6521 DBUG_RETURN(error_num);
6522 if (spider->result_list.direct_order_limit)
6523 {
6524 if ((error_num =
6525 append_key_order_for_direct_order_limit_with_alias(&tmp_sql,
6526 SPIDER_SQL_B_DOT_STR, SPIDER_SQL_B_DOT_LEN))
6527 )
6528 DBUG_RETURN(error_num);
6529 }
6530#ifdef HANDLER_HAS_DIRECT_AGGREGATE
6531 else if (spider->result_list.direct_aggregate)
6532 {
6533 if ((error_num =
6534 append_group_by(&tmp_sql, SPIDER_SQL_B_DOT_STR, SPIDER_SQL_B_DOT_LEN)))
6535 DBUG_RETURN(error_num);
6536 }
6537#endif
6538
6539 DBUG_RETURN(0);
6540}
6541
6542int spider_mysql_handler::reuse_union_table_and_sql_for_bka()
6543{
6544 DBUG_ENTER("spider_mysql_handler::reuse_union_table_and_sql_for_bka");
6545 DBUG_PRINT("info",("spider this=%p", this));
6546 sql.length(tmp_sql_pos1);
6547 DBUG_RETURN(0);
6548}
6549
6550int spider_mysql_handler::append_insert_for_recovery(
6551 ulong sql_type,
6552 int link_idx
6553) {
6554 const TABLE *table = spider->get_table();
6555 SPIDER_SHARE *share = spider->share;
6556 Field **field;
6557 uint field_name_length = 0;
6558 bool add_value = FALSE;
6559 spider_string *insert_sql;
6560 DBUG_ENTER("spider_mysql_handler::append_insert_for_recovery");
6561 DBUG_PRINT("info",("spider this=%p", this));
6562 if (sql_type == SPIDER_SQL_TYPE_INSERT_SQL)
6563 {
6564 insert_sql = &spider->result_list.insert_sqls[link_idx];
6565 insert_sql->length(0);
6566 } else {
6567 insert_sql = &spider->result_list.update_sqls[link_idx];
6568 }
6569 if (insert_sql->reserve(
6570 SPIDER_SQL_INSERT_LEN + SPIDER_SQL_SQL_IGNORE_LEN +
6571 SPIDER_SQL_INTO_LEN + mysql_share->db_nm_max_length +
6572 SPIDER_SQL_DOT_LEN + mysql_share->table_nm_max_length +
6573 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4 + SPIDER_SQL_OPEN_PAREN_LEN))
6574 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6575 insert_sql->q_append(SPIDER_SQL_INSERT_STR, SPIDER_SQL_INSERT_LEN);
6576 insert_sql->q_append(SPIDER_SQL_SQL_IGNORE_STR, SPIDER_SQL_SQL_IGNORE_LEN);
6577 insert_sql->q_append(SPIDER_SQL_INTO_STR, SPIDER_SQL_INTO_LEN);
6578 mysql_share->append_table_name(insert_sql, spider->conn_link_idx[link_idx]);
6579 insert_sql->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
6580 for (field = table->field; *field; field++)
6581 {
6582 field_name_length =
6583 mysql_share->column_name_str[(*field)->field_index].length();
6584 if (insert_sql->reserve(field_name_length +
6585 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
6586 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6587 mysql_share->append_column_name(insert_sql, (*field)->field_index);
6588 insert_sql->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
6589 }
6590 if (field_name_length)
6591 insert_sql->length(insert_sql->length() - SPIDER_SQL_COMMA_LEN);
6592 if (insert_sql->reserve(SPIDER_SQL_CLOSE_PAREN_LEN + SPIDER_SQL_VALUES_LEN +
6593 SPIDER_SQL_OPEN_PAREN_LEN))
6594 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6595 insert_sql->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
6596 insert_sql->q_append(SPIDER_SQL_VALUES_STR, SPIDER_SQL_VALUES_LEN);
6597 insert_sql->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
6598 for (field = table->field; *field; field++)
6599 {
6600 add_value = TRUE;
6601 if ((*field)->is_null())
6602 {
6603 if (insert_sql->reserve(SPIDER_SQL_NULL_LEN + SPIDER_SQL_COMMA_LEN))
6604 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6605 insert_sql->q_append(SPIDER_SQL_NULL_STR, SPIDER_SQL_NULL_LEN);
6606 } else {
6607 if (
6608 spider_db_mysql_utility.
6609 append_column_value(spider, insert_sql, *field, NULL,
6610 share->access_charset) ||
6611 insert_sql->reserve(SPIDER_SQL_COMMA_LEN)
6612 )
6613 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6614 }
6615 insert_sql->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
6616 }
6617 if (add_value)
6618 insert_sql->length(insert_sql->length() - SPIDER_SQL_COMMA_LEN);
6619 if (insert_sql->reserve(SPIDER_SQL_CLOSE_PAREN_LEN, SPIDER_SQL_COMMA_LEN))
6620 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6621 insert_sql->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
6622 if (sql_type == SPIDER_SQL_TYPE_INSERT_SQL)
6623 {
6624 exec_insert_sql = insert_sql;
6625 }
6626 DBUG_RETURN(0);
6627}
6628
6629int spider_mysql_handler::append_update(
6630 const TABLE *table,
6631 my_ptrdiff_t ptr_diff
6632) {
6633 int error_num;
6634 spider_string *str = &update_sql;
6635 DBUG_ENTER("spider_mysql_handler::append_update");
6636 DBUG_PRINT("info",("spider this=%p", this));
6637 if (str->length() > 0)
6638 {
6639 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN))
6640 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6641 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
6642 }
6643
6644 if (
6645 (error_num = append_update(str, 0)) ||
6646 (error_num = append_update_set(str)) ||
6647 (error_num = append_update_where(str, table, ptr_diff))
6648 )
6649 DBUG_RETURN(error_num);
6650 filled_up = (str->length() >= (uint) spider->result_list.bulk_update_size);
6651 DBUG_RETURN(0);
6652}
6653
6654int spider_mysql_handler::append_update(
6655 const TABLE *table,
6656 my_ptrdiff_t ptr_diff,
6657 int link_idx
6658) {
6659 int error_num;
6660 SPIDER_SHARE *share = spider->share;
6661 spider_string *str = &spider->result_list.update_sqls[link_idx];
6662 DBUG_ENTER("spider_mysql_handler::append_update");
6663 DBUG_PRINT("info",("spider this=%p", this));
6664 if (str->length() > 0)
6665 {
6666 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN))
6667 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6668 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
6669 }
6670
6671 if (
6672 (error_num = append_update(str, link_idx)) ||
6673 (error_num = append_update_set(str)) ||
6674 (error_num = append_update_where(str, table, ptr_diff))
6675 )
6676 DBUG_RETURN(error_num);
6677
6678 if (
6679 spider->pk_update &&
6680 share->link_statuses[link_idx] == SPIDER_LINK_STATUS_RECOVERY
6681 ) {
6682 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN))
6683 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6684 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
6685 if ((error_num = append_insert_for_recovery(
6686 SPIDER_SQL_TYPE_UPDATE_SQL, link_idx)))
6687 DBUG_RETURN(error_num);
6688 }
6689
6690 if (!filled_up)
6691 filled_up = (str->length() >= (uint) spider->result_list.bulk_update_size);
6692 DBUG_RETURN(0);
6693}
6694
6695int spider_mysql_handler::append_delete(
6696 const TABLE *table,
6697 my_ptrdiff_t ptr_diff
6698) {
6699 int error_num;
6700 spider_string *str = &update_sql;
6701 DBUG_ENTER("spider_mysql_handler::append_delete");
6702 DBUG_PRINT("info",("spider this=%p", this));
6703 if (str->length() > 0)
6704 {
6705 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN))
6706 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6707 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
6708 }
6709
6710 if (
6711 (error_num = append_delete(str)) ||
6712 (error_num = append_from(str, SPIDER_SQL_TYPE_DELETE_SQL,
6713 first_link_idx)) ||
6714 (error_num = append_update_where(str, table, ptr_diff))
6715 )
6716 DBUG_RETURN(error_num);
6717 filled_up = (str->length() >= (uint) spider->result_list.bulk_update_size);
6718 DBUG_RETURN(0);
6719}
6720
6721int spider_mysql_handler::append_delete(
6722 const TABLE *table,
6723 my_ptrdiff_t ptr_diff,
6724 int link_idx
6725) {
6726 int error_num;
6727 spider_string *str = &spider->result_list.update_sqls[link_idx];
6728 DBUG_ENTER("spider_mysql_handler::append_delete");
6729 DBUG_PRINT("info",("spider this=%p", this));
6730 if (str->length() > 0)
6731 {
6732 if (str->reserve(SPIDER_SQL_SEMICOLON_LEN))
6733 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6734 str->q_append(SPIDER_SQL_SEMICOLON_STR, SPIDER_SQL_SEMICOLON_LEN);
6735 }
6736
6737 if (
6738 (error_num = append_delete(str)) ||
6739 (error_num = append_from(str, SPIDER_SQL_TYPE_DELETE_SQL, link_idx)) ||
6740 (error_num = append_update_where(str, table, ptr_diff))
6741 )
6742 DBUG_RETURN(error_num);
6743 if (!filled_up)
6744 filled_up = (str->length() >= (uint) spider->result_list.bulk_update_size);
6745 DBUG_RETURN(0);
6746}
6747
6748int spider_mysql_handler::append_insert_part()
6749{
6750 int error_num;
6751 DBUG_ENTER("spider_mysql_handler::append_insert_part");
6752 DBUG_PRINT("info",("spider this=%p", this));
6753 error_num = append_insert(&insert_sql, 0);
6754 DBUG_RETURN(error_num);
6755}
6756
6757int spider_mysql_handler::append_insert(
6758 spider_string *str,
6759 int link_idx
6760) {
6761 SPIDER_SHARE *share = spider->share;
6762 DBUG_ENTER("spider_mysql_handler::append_insert");
6763 if (
6764 (
6765 spider->write_can_replace ||
6766 /* for direct_dup_insert without patch for partition */
6767 spider->sql_command == SQLCOM_REPLACE ||
6768 spider->sql_command == SQLCOM_REPLACE_SELECT
6769 ) &&
6770 spider->direct_dup_insert
6771 ) {
6772 if (str->reserve(SPIDER_SQL_REPLACE_LEN))
6773 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6774 str->q_append(SPIDER_SQL_REPLACE_STR, SPIDER_SQL_REPLACE_LEN);
6775 } else {
6776 if (str->reserve(SPIDER_SQL_INSERT_LEN))
6777 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6778 str->q_append(SPIDER_SQL_INSERT_STR, SPIDER_SQL_INSERT_LEN);
6779 }
6780 if (spider->low_priority)
6781 {
6782 if (str->reserve(SPIDER_SQL_LOW_PRIORITY_LEN))
6783 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6784 str->q_append(SPIDER_SQL_LOW_PRIORITY_STR, SPIDER_SQL_LOW_PRIORITY_LEN);
6785 }
6786 else if (spider->insert_delayed)
6787 {
6788 if (share->internal_delayed)
6789 {
6790 if (str->reserve(SPIDER_SQL_SQL_DELAYED_LEN))
6791 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6792 str->q_append(SPIDER_SQL_SQL_DELAYED_STR, SPIDER_SQL_SQL_DELAYED_LEN);
6793 }
6794 }
6795 else if (
6796 spider->lock_type >= TL_WRITE &&
6797 !spider->write_can_replace &&
6798 /* for direct_dup_insert without patch for partition */
6799 spider->sql_command != SQLCOM_REPLACE &&
6800 spider->sql_command != SQLCOM_REPLACE_SELECT
6801 ) {
6802 if (str->reserve(SPIDER_SQL_HIGH_PRIORITY_LEN))
6803 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6804 str->q_append(SPIDER_SQL_HIGH_PRIORITY_STR, SPIDER_SQL_HIGH_PRIORITY_LEN);
6805 }
6806 if (
6807 spider->ignore_dup_key &&
6808 spider->direct_dup_insert &&
6809 !spider->write_can_replace &&
6810#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
6811 (!spider->insert_with_update || !dup_update_sql.length()) &&
6812#else
6813 !spider->insert_with_update &&
6814#endif
6815 /* for direct_dup_insert without patch for partition */
6816 spider->sql_command != SQLCOM_REPLACE &&
6817 spider->sql_command != SQLCOM_REPLACE_SELECT
6818 ) {
6819 if (str->reserve(SPIDER_SQL_SQL_IGNORE_LEN))
6820 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6821 str->q_append(SPIDER_SQL_SQL_IGNORE_STR, SPIDER_SQL_SQL_IGNORE_LEN);
6822 }
6823 DBUG_RETURN(0);
6824}
6825
6826int spider_mysql_handler::append_update_part()
6827{
6828 int error_num;
6829 DBUG_ENTER("spider_mysql_handler::append_update_part");
6830 DBUG_PRINT("info",("spider this=%p", this));
6831 error_num = append_update(&update_sql, 0);
6832 DBUG_RETURN(error_num);
6833}
6834
6835int spider_mysql_handler::append_update(
6836 spider_string *str,
6837 int link_idx
6838) {
6839 DBUG_ENTER("spider_mysql_handler::append_update");
6840 if (str->reserve(SPIDER_SQL_UPDATE_LEN))
6841 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6842 str->q_append(SPIDER_SQL_UPDATE_STR, SPIDER_SQL_UPDATE_LEN);
6843 if (spider->low_priority)
6844 {
6845 if (str->reserve(SPIDER_SQL_LOW_PRIORITY_LEN))
6846 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6847 str->q_append(SPIDER_SQL_LOW_PRIORITY_STR, SPIDER_SQL_LOW_PRIORITY_LEN);
6848 }
6849 if (
6850 spider->ignore_dup_key &&
6851 !spider->insert_with_update
6852 ) {
6853 if (str->reserve(SPIDER_SQL_SQL_IGNORE_LEN))
6854 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6855 str->q_append(SPIDER_SQL_SQL_IGNORE_STR, SPIDER_SQL_SQL_IGNORE_LEN);
6856 }
6857 if (str->reserve(mysql_share->db_nm_max_length +
6858 SPIDER_SQL_DOT_LEN + mysql_share->table_nm_max_length +
6859 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4))
6860 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6861 table_name_pos = str->length();
6862 append_table_name_with_adjusting(str, link_idx, SPIDER_SQL_TYPE_UPDATE_SQL);
6863 DBUG_RETURN(0);
6864}
6865
6866int spider_mysql_handler::append_delete_part()
6867{
6868 int error_num;
6869 DBUG_ENTER("spider_mysql_handler::append_delete_part");
6870 DBUG_PRINT("info",("spider this=%p", this));
6871 error_num = append_delete(&update_sql);
6872 DBUG_RETURN(error_num);
6873}
6874
6875int spider_mysql_handler::append_delete(
6876 spider_string *str
6877) {
6878 DBUG_ENTER("spider_mysql_handler::append_delete");
6879 if (str->reserve(SPIDER_SQL_DELETE_LEN))
6880 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6881 str->q_append(SPIDER_SQL_DELETE_STR, SPIDER_SQL_DELETE_LEN);
6882 if (spider->low_priority)
6883 {
6884 if (str->reserve(SPIDER_SQL_LOW_PRIORITY_LEN))
6885 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6886 str->q_append(SPIDER_SQL_LOW_PRIORITY_STR, SPIDER_SQL_LOW_PRIORITY_LEN);
6887 }
6888 if (spider->quick_mode)
6889 {
6890 if (str->reserve(SPIDER_SQL_SQL_QUICK_MODE_LEN))
6891 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6892 str->q_append(SPIDER_SQL_SQL_QUICK_MODE_STR,
6893 SPIDER_SQL_SQL_QUICK_MODE_LEN);
6894 }
6895 if (spider->ignore_dup_key)
6896 {
6897 if (str->reserve(SPIDER_SQL_SQL_IGNORE_LEN))
6898 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6899 str->q_append(SPIDER_SQL_SQL_IGNORE_STR, SPIDER_SQL_SQL_IGNORE_LEN);
6900 }
6901 str->length(str->length() - 1);
6902 DBUG_RETURN(0);
6903}
6904
6905#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6906#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
6907int spider_mysql_handler::append_increment_update_set_part()
6908{
6909 int error_num;
6910 DBUG_ENTER("spider_mysql_handler::append_increment_update_set_part");
6911 DBUG_PRINT("info",("spider this=%p", this));
6912 error_num = append_increment_update_set(&update_sql);
6913 DBUG_RETURN(error_num);
6914}
6915
6916int spider_mysql_handler::append_increment_update_set(
6917 spider_string *str
6918) {
6919 uint field_name_length;
6920 uint roop_count;
6921 Field *field;
6922 DBUG_ENTER("spider_mysql_handler::append_increment_update_set");
6923 if (str->reserve(SPIDER_SQL_SET_LEN))
6924 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6925 str->q_append(SPIDER_SQL_SET_STR, SPIDER_SQL_SET_LEN);
6926 const SPIDER_HS_STRING_REF *value = hs_upds.ptr();
6927 for (roop_count = 0; roop_count < hs_upds.size();
6928 roop_count++)
6929 {
6930 if (
6931 value[roop_count].size() == 1 &&
6932 *(value[roop_count].begin()) == '0'
6933 )
6934 continue;
6935
6936 Field *top_table_field =
6937 spider->get_top_table_field(spider->hs_pushed_ret_fields[roop_count]);
6938 if (!(field = spider->field_exchange(top_table_field)))
6939 continue;
6940 field_name_length =
6941 mysql_share->column_name_str[field->field_index].length();
6942
6943 if (str->reserve(field_name_length * 2 + /* SPIDER_SQL_NAME_QUOTE_LEN */
6944 4 + SPIDER_SQL_EQUAL_LEN + SPIDER_SQL_HS_INCREMENT_LEN +
6945 SPIDER_SQL_COMMA_LEN + value[roop_count].size()))
6946 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6947
6948 mysql_share->append_column_name(str, field->field_index);
6949 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
6950 mysql_share->append_column_name(str, field->field_index);
6951 if (spider->hs_increment)
6952 str->q_append(SPIDER_SQL_HS_INCREMENT_STR,
6953 SPIDER_SQL_HS_INCREMENT_LEN);
6954 else
6955 str->q_append(SPIDER_SQL_HS_DECREMENT_STR,
6956 SPIDER_SQL_HS_DECREMENT_LEN);
6957 str->q_append(value[roop_count].begin(), value[roop_count].size());
6958 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
6959 }
6960 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
6961 DBUG_RETURN(0);
6962}
6963#endif
6964#endif
6965
6966int spider_mysql_handler::append_update_set_part()
6967{
6968 int error_num;
6969 DBUG_ENTER("spider_mysql_handler::append_update_set_part");
6970 DBUG_PRINT("info",("spider this=%p", this));
6971 error_num = append_update_set(&update_sql);
6972 where_pos = update_sql.length();
6973 DBUG_RETURN(error_num);
6974}
6975
6976int spider_mysql_handler::append_update_set(
6977 spider_string *str
6978) {
6979 uint field_name_length;
6980 SPIDER_SHARE *share = spider->share;
6981 TABLE *table = spider->get_table();
6982 Field **fields;
6983 DBUG_ENTER("spider_mysql_handler::append_update_set");
6984 if (str->reserve(SPIDER_SQL_SET_LEN))
6985 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6986 str->q_append(SPIDER_SQL_SET_STR, SPIDER_SQL_SET_LEN);
6987 for (fields = table->field; *fields; fields++)
6988 {
6989 if (bitmap_is_set(table->write_set, (*fields)->field_index))
6990 {
6991 field_name_length =
6992 mysql_share->column_name_str[(*fields)->field_index].length();
6993 if ((*fields)->is_null())
6994 {
6995 if (str->reserve(field_name_length + /* SPIDER_SQL_NAME_QUOTE_LEN */
6996 2 + SPIDER_SQL_EQUAL_LEN + SPIDER_SQL_NULL_LEN +
6997 SPIDER_SQL_COMMA_LEN))
6998 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6999 mysql_share->append_column_name(str, (*fields)->field_index);
7000 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
7001 str->q_append(SPIDER_SQL_NULL_STR, SPIDER_SQL_NULL_LEN);
7002 } else {
7003 if (str->reserve(field_name_length + /* SPIDER_SQL_NAME_QUOTE_LEN */
7004 2 + SPIDER_SQL_EQUAL_LEN))
7005 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7006 mysql_share->append_column_name(str, (*fields)->field_index);
7007 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
7008#ifndef DBUG_OFF
7009 my_bitmap_map *tmp_map = dbug_tmp_use_all_columns(table,
7010 table->read_set);
7011#endif
7012 if (
7013 spider_db_mysql_utility.
7014 append_column_value(spider, str, *fields, NULL,
7015 share->access_charset) ||
7016 str->reserve(SPIDER_SQL_COMMA_LEN)
7017 ) {
7018#ifndef DBUG_OFF
7019 dbug_tmp_restore_column_map(table->read_set, tmp_map);
7020#endif
7021 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7022 }
7023#ifndef DBUG_OFF
7024 dbug_tmp_restore_column_map(table->read_set, tmp_map);
7025#endif
7026 }
7027 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
7028 }
7029 }
7030 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
7031 DBUG_RETURN(0);
7032}
7033
7034#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
7035int spider_mysql_handler::append_direct_update_set_part()
7036{
7037 int error_num;
7038 DBUG_ENTER("spider_mysql_handler::append_direct_update_set_part");
7039 DBUG_PRINT("info",("spider this=%p", this));
7040 error_num = append_direct_update_set(&update_sql);
7041 where_pos = update_sql.length();
7042 DBUG_RETURN(error_num);
7043}
7044
7045int spider_mysql_handler::append_direct_update_set(
7046 spider_string *str
7047) {
7048#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7049 uint field_name_length;
7050 SPIDER_SHARE *share = spider->share;
7051#ifndef DBUG_OFF
7052 TABLE *table = spider->get_table();
7053#endif
7054#endif
7055 DBUG_ENTER("spider_mysql_handler::append_direct_update_set");
7056 if (
7057 spider->direct_update_kinds == SPIDER_SQL_KIND_SQL &&
7058 spider->direct_update_fields
7059 ) {
7060 if (str->reserve(SPIDER_SQL_SET_LEN))
7061 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7062 str->q_append(SPIDER_SQL_SET_STR, SPIDER_SQL_SET_LEN);
7063 DBUG_RETURN(spider_db_append_update_columns(spider, str, NULL, 0,
7064 spider_dbton_mysql.dbton_id, FALSE, NULL));
7065 }
7066
7067 if (
7068 (spider->direct_update_kinds & SPIDER_SQL_KIND_SQL)
7069 ) {
7070#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7071 size_t roop_count;
7072 Field *field;
7073 if (str->reserve(SPIDER_SQL_SET_LEN))
7074 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7075 str->q_append(SPIDER_SQL_SET_STR, SPIDER_SQL_SET_LEN);
7076 for (roop_count = 0; roop_count < spider->hs_pushed_ret_fields_num;
7077 roop_count++)
7078 {
7079 Field *top_table_field =
7080 spider->get_top_table_field(spider->hs_pushed_ret_fields[roop_count]);
7081 if (!(field = spider->field_exchange(top_table_field)))
7082 continue;
7083 field_name_length =
7084 mysql_share->column_name_str[field->field_index].length();
7085 if (top_table_field->is_null())
7086 {
7087 if (str->reserve(field_name_length + /* SPIDER_SQL_NAME_QUOTE_LEN */
7088 2 + SPIDER_SQL_EQUAL_LEN + SPIDER_SQL_NULL_LEN +
7089 SPIDER_SQL_COMMA_LEN))
7090 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7091 mysql_share->append_column_name(str, field->field_index);
7092 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
7093 str->q_append(SPIDER_SQL_NULL_STR, SPIDER_SQL_NULL_LEN);
7094 } else {
7095 if (str->reserve(field_name_length + /* SPIDER_SQL_NAME_QUOTE_LEN */
7096 2 + SPIDER_SQL_EQUAL_LEN))
7097 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7098 mysql_share->append_column_name(str, field->field_index);
7099 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
7100#ifndef DBUG_OFF
7101 my_bitmap_map *tmp_map = dbug_tmp_use_all_columns(table,
7102 table->read_set);
7103#endif
7104 if (
7105 spider_db_mysql_utility.
7106 append_column_value(spider, str, top_table_field, NULL,
7107 share->access_charset) ||
7108 str->reserve(SPIDER_SQL_COMMA_LEN)
7109 ) {
7110#ifndef DBUG_OFF
7111 dbug_tmp_restore_column_map(table->read_set, tmp_map);
7112#endif
7113 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7114 }
7115#ifndef DBUG_OFF
7116 dbug_tmp_restore_column_map(table->read_set, tmp_map);
7117#endif
7118 }
7119 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
7120 }
7121 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
7122#else
7123 DBUG_ASSERT(0);
7124#endif
7125 }
7126 DBUG_RETURN(0);
7127}
7128
7129int spider_mysql_handler::append_dup_update_pushdown_part(
7130 const char *alias,
7131 uint alias_length
7132) {
7133 int error_num;
7134 DBUG_ENTER("spider_mysql_handler::append_dup_update_pushdown_part");
7135 DBUG_PRINT("info",("spider this=%p", this));
7136 dup_update_sql.length(0);
7137 error_num = append_update_columns(&dup_update_sql, alias, alias_length);
7138 DBUG_RETURN(error_num);
7139}
7140
7141int spider_mysql_handler::append_update_columns_part(
7142 const char *alias,
7143 uint alias_length
7144) {
7145 int error_num;
7146 DBUG_ENTER("spider_mysql_handler::append_update_columns_part");
7147 DBUG_PRINT("info",("spider this=%p", this));
7148 error_num = append_update_columns(&update_sql, alias, alias_length);
7149 DBUG_RETURN(error_num);
7150}
7151
7152int spider_mysql_handler::check_update_columns_part()
7153{
7154 int error_num;
7155 DBUG_ENTER("spider_mysql_handler::check_update_columns_part");
7156 DBUG_PRINT("info",("spider this=%p", this));
7157 error_num = append_update_columns(NULL, NULL, 0);
7158 DBUG_RETURN(error_num);
7159}
7160
7161int spider_mysql_handler::append_update_columns(
7162 spider_string *str,
7163 const char *alias,
7164 uint alias_length
7165) {
7166 int error_num;
7167 DBUG_ENTER("spider_mysql_handler::append_update_columns");
7168 error_num = spider_db_append_update_columns(spider, str,
7169 alias, alias_length, spider_dbton_mysql.dbton_id, FALSE, NULL);
7170 DBUG_RETURN(error_num);
7171}
7172#endif
7173
7174int spider_mysql_handler::append_select_part(
7175 ulong sql_type
7176) {
7177 int error_num;
7178 spider_string *str;
7179 DBUG_ENTER("spider_mysql_handler::append_select_part");
7180 DBUG_PRINT("info",("spider this=%p", this));
7181 switch (sql_type)
7182 {
7183 case SPIDER_SQL_TYPE_SELECT_SQL:
7184 str = &sql;
7185 break;
7186 case SPIDER_SQL_TYPE_HANDLER:
7187 str = &ha_sql;
7188 break;
7189 default:
7190 DBUG_RETURN(0);
7191 }
7192 error_num = append_select(str, sql_type);
7193 DBUG_RETURN(error_num);
7194}
7195
7196int spider_mysql_handler::append_select(
7197 spider_string *str,
7198 ulong sql_type
7199) {
7200 SPIDER_RESULT_LIST *result_list = &spider->result_list;
7201 DBUG_ENTER("spider_mysql_handler::append_select");
7202 if (sql_type == SPIDER_SQL_TYPE_HANDLER)
7203 {
7204 if (str->reserve(SPIDER_SQL_HANDLER_LEN))
7205 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7206 str->q_append(SPIDER_SQL_HANDLER_STR, SPIDER_SQL_HANDLER_LEN);
7207 } else {
7208 if (str->reserve(SPIDER_SQL_SELECT_LEN))
7209 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7210 str->q_append(SPIDER_SQL_SELECT_STR, SPIDER_SQL_SELECT_LEN);
7211 if (result_list->direct_distinct)
7212 {
7213 if (str->reserve(SPIDER_SQL_DISTINCT_LEN))
7214 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7215 str->q_append(SPIDER_SQL_DISTINCT_STR, SPIDER_SQL_DISTINCT_LEN);
7216 }
7217 if (result_list->lock_type != F_WRLCK && spider->lock_mode < 1)
7218 {
7219 /* no lock */
7220 st_select_lex *select_lex = &spider->trx->thd->lex->select_lex;
7221 if (
7222 select_lex->sql_cache == SELECT_LEX::SQL_CACHE &&
7223 (spider->share->query_cache_sync & 1)
7224 ) {
7225 if (str->reserve(SPIDER_SQL_SQL_CACHE_LEN))
7226 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7227 str->q_append(SPIDER_SQL_SQL_CACHE_STR, SPIDER_SQL_SQL_CACHE_LEN);
7228 } else if (
7229 select_lex->sql_cache == SELECT_LEX::SQL_NO_CACHE &&
7230 (spider->share->query_cache_sync & 2)
7231 ) {
7232 if (str->reserve(SPIDER_SQL_SQL_NO_CACHE_LEN))
7233 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7234 str->q_append(SPIDER_SQL_SQL_NO_CACHE_STR,
7235 SPIDER_SQL_SQL_NO_CACHE_LEN);
7236 } else if (spider->share->query_cache == 1)
7237 {
7238 if (str->reserve(SPIDER_SQL_SQL_CACHE_LEN))
7239 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7240 str->q_append(SPIDER_SQL_SQL_CACHE_STR, SPIDER_SQL_SQL_CACHE_LEN);
7241 } else if (spider->share->query_cache == 2)
7242 {
7243 if (str->reserve(SPIDER_SQL_SQL_NO_CACHE_LEN))
7244 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7245 str->q_append(SPIDER_SQL_SQL_NO_CACHE_STR,
7246 SPIDER_SQL_SQL_NO_CACHE_LEN);
7247 }
7248 }
7249 if (spider->high_priority)
7250 {
7251 if (str->reserve(SPIDER_SQL_HIGH_PRIORITY_LEN))
7252 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7253 str->q_append(SPIDER_SQL_HIGH_PRIORITY_STR,
7254 SPIDER_SQL_HIGH_PRIORITY_LEN);
7255 }
7256 }
7257 DBUG_RETURN(0);
7258}
7259
7260int spider_mysql_handler::append_table_select_part(
7261 ulong sql_type
7262) {
7263 int error_num;
7264 spider_string *str;
7265 DBUG_ENTER("spider_mysql_handler::append_table_select_part");
7266 DBUG_PRINT("info",("spider this=%p", this));
7267 switch (sql_type)
7268 {
7269 case SPIDER_SQL_TYPE_SELECT_SQL:
7270 str = &sql;
7271 break;
7272 default:
7273 DBUG_RETURN(0);
7274 }
7275 error_num = append_table_select(str);
7276 DBUG_RETURN(error_num);
7277}
7278
7279int spider_mysql_handler::append_table_select(
7280 spider_string *str
7281) {
7282 DBUG_ENTER("spider_mysql_handler::append_table_select");
7283 table_name_pos = str->length() + mysql_share->table_select_pos;
7284 if (str->append(*(mysql_share->table_select)))
7285 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7286 DBUG_RETURN(0);
7287}
7288
7289int spider_mysql_handler::append_key_select_part(
7290 ulong sql_type,
7291 uint idx
7292) {
7293 int error_num;
7294 spider_string *str;
7295 DBUG_ENTER("spider_mysql_handler::append_key_select_part");
7296 DBUG_PRINT("info",("spider this=%p", this));
7297 switch (sql_type)
7298 {
7299 case SPIDER_SQL_TYPE_SELECT_SQL:
7300 str = &sql;
7301 break;
7302 default:
7303 DBUG_RETURN(0);
7304 }
7305 error_num = append_key_select(str, idx);
7306 DBUG_RETURN(error_num);
7307}
7308
7309int spider_mysql_handler::append_key_select(
7310 spider_string *str,
7311 uint idx
7312) {
7313 DBUG_ENTER("spider_mysql_handler::append_key_select");
7314 table_name_pos = str->length() + mysql_share->key_select_pos[idx];
7315 if (str->append(mysql_share->key_select[idx]))
7316 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7317 DBUG_RETURN(0);
7318}
7319
7320int spider_mysql_handler::append_minimum_select_part(
7321 ulong sql_type
7322) {
7323 int error_num;
7324 spider_string *str;
7325 DBUG_ENTER("spider_mysql_handler::append_minimum_select_part");
7326 DBUG_PRINT("info",("spider this=%p", this));
7327 switch (sql_type)
7328 {
7329 case SPIDER_SQL_TYPE_SELECT_SQL:
7330 str = &sql;
7331 break;
7332 default:
7333 DBUG_RETURN(0);
7334 }
7335 error_num = append_minimum_select(str, sql_type);
7336 DBUG_RETURN(error_num);
7337}
7338
7339int spider_mysql_handler::append_minimum_select(
7340 spider_string *str,
7341 ulong sql_type
7342) {
7343 TABLE *table = spider->get_table();
7344 Field **field;
7345 int field_length;
7346 bool appended = FALSE;
7347 DBUG_ENTER("spider_mysql_handler::append_minimum_select");
7348 minimum_select_bitmap_create();
7349 for (field = table->field; *field; field++)
7350 {
7351 if (minimum_select_bit_is_set((*field)->field_index))
7352 {
7353/*
7354 spider_set_bit(minimum_select_bitmap, (*field)->field_index);
7355*/
7356 field_length =
7357 mysql_share->column_name_str[(*field)->field_index].length();
7358 if (str->reserve(field_length +
7359 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
7360 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7361 mysql_share->append_column_name(str, (*field)->field_index);
7362 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
7363 appended = TRUE;
7364 }
7365 }
7366 if (appended)
7367 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
7368 else {
7369 if (str->reserve(SPIDER_SQL_ONE_LEN))
7370 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7371 str->q_append(SPIDER_SQL_ONE_STR, SPIDER_SQL_ONE_LEN);
7372 }
7373 DBUG_RETURN(append_from(str, sql_type, first_link_idx));
7374}
7375
7376int spider_mysql_handler::append_table_select_with_alias(
7377 spider_string *str,
7378 const char *alias,
7379 uint alias_length
7380) {
7381 TABLE *table = spider->get_table();
7382 Field **field;
7383 int field_length;
7384 DBUG_ENTER("spider_mysql_handler::append_table_select_with_alias");
7385 for (field = table->field; *field; field++)
7386 {
7387 field_length =
7388 mysql_share->column_name_str[(*field)->field_index].length();
7389 if (str->reserve(alias_length + field_length +
7390 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
7391 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7392 str->q_append(alias, alias_length);
7393 mysql_share->append_column_name(str, (*field)->field_index);
7394 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
7395 }
7396 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
7397 DBUG_RETURN(0);
7398}
7399
7400int spider_mysql_handler::append_key_select_with_alias(
7401 spider_string *str,
7402 const KEY *key_info,
7403 const char *alias,
7404 uint alias_length
7405) {
7406 KEY_PART_INFO *key_part;
7407 Field *field;
7408 uint part_num;
7409 int field_length;
7410 DBUG_ENTER("spider_mysql_handler::append_key_select_with_alias");
7411 for (key_part = key_info->key_part, part_num = 0;
7412 part_num < spider_user_defined_key_parts(key_info); key_part++, part_num++)
7413 {
7414 field = key_part->field;
7415 field_length = mysql_share->column_name_str[field->field_index].length();
7416 if (str->reserve(alias_length + field_length +
7417 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
7418 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7419 str->q_append(alias, alias_length);
7420 mysql_share->append_column_name(str, field->field_index);
7421 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
7422 }
7423 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
7424 DBUG_RETURN(0);
7425}
7426
7427int spider_mysql_handler::append_minimum_select_with_alias(
7428 spider_string *str,
7429 const char *alias,
7430 uint alias_length
7431) {
7432 TABLE *table = spider->get_table();
7433 Field **field;
7434 int field_length;
7435 bool appended = FALSE;
7436 DBUG_ENTER("spider_mysql_handler::append_minimum_select_with_alias");
7437 minimum_select_bitmap_create();
7438 for (field = table->field; *field; field++)
7439 {
7440 if (minimum_select_bit_is_set((*field)->field_index))
7441 {
7442/*
7443 spider_set_bit(minimum_select_bitmap, (*field)->field_index);
7444*/
7445 field_length =
7446 mysql_share->column_name_str[(*field)->field_index].length();
7447 if (str->reserve(alias_length + field_length +
7448 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
7449 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7450 str->q_append(alias, alias_length);
7451 mysql_share->append_column_name(str, (*field)->field_index);
7452 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
7453 appended = TRUE;
7454 }
7455 }
7456 if (appended)
7457 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
7458 else {
7459 if (str->reserve(SPIDER_SQL_ONE_LEN))
7460 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7461 str->q_append(SPIDER_SQL_ONE_STR, SPIDER_SQL_ONE_LEN);
7462 }
7463 DBUG_RETURN(0);
7464}
7465
7466int spider_mysql_handler::append_select_columns_with_alias(
7467 spider_string *str,
7468 const char *alias,
7469 uint alias_length
7470) {
7471 int error_num;
7472 SPIDER_RESULT_LIST *result_list = &spider->result_list;
7473 DBUG_ENTER("spider_mysql_handler::append_select_columns_with_alias");
7474#ifdef HANDLER_HAS_DIRECT_AGGREGATE
7475 if (
7476 result_list->direct_aggregate &&
7477 (error_num = append_sum_select(str, alias, alias_length))
7478 )
7479 DBUG_RETURN(error_num);
7480#endif
7481 if ((error_num = append_match_select(str, alias, alias_length)))
7482 DBUG_RETURN(error_num);
7483 if (!spider->select_column_mode)
7484 {
7485 if (result_list->keyread)
7486 DBUG_RETURN(append_key_select_with_alias(
7487 str, result_list->key_info, alias, alias_length));
7488 else
7489 DBUG_RETURN(append_table_select_with_alias(
7490 str, alias, alias_length));
7491 }
7492 DBUG_RETURN(append_minimum_select_with_alias(str, alias, alias_length));
7493}
7494
7495int spider_mysql_handler::append_hint_after_table_part(
7496 ulong sql_type
7497) {
7498 int error_num;
7499 spider_string *str;
7500 DBUG_ENTER("spider_mysql_handler::append_hint_after_table_part");
7501 DBUG_PRINT("info",("spider this=%p", this));
7502 switch (sql_type)
7503 {
7504 case SPIDER_SQL_TYPE_SELECT_SQL:
7505 case SPIDER_SQL_TYPE_TMP_SQL:
7506 str = &sql;
7507 break;
7508 case SPIDER_SQL_TYPE_INSERT_SQL:
7509 case SPIDER_SQL_TYPE_UPDATE_SQL:
7510 case SPIDER_SQL_TYPE_DELETE_SQL:
7511 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
7512 str = &update_sql;
7513 break;
7514 case SPIDER_SQL_TYPE_HANDLER:
7515 str = &ha_sql;
7516 break;
7517 default:
7518 DBUG_RETURN(0);
7519 }
7520 error_num = append_hint_after_table(str);
7521 DBUG_RETURN(error_num);
7522}
7523
7524int spider_mysql_handler::append_hint_after_table(
7525 spider_string *str
7526) {
7527 int error_num;
7528 DBUG_ENTER("spider_mysql_handler::append_hint_after_table");
7529 DBUG_PRINT("info",("spider this=%p", this));
7530 if (
7531 mysql_share->key_hint &&
7532 (error_num = spider_db_append_hint_after_table(spider,
7533 str, &mysql_share->key_hint[spider->active_index]))
7534 )
7535 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7536 DBUG_RETURN(0);
7537}
7538
7539void spider_mysql_handler::set_where_pos(
7540 ulong sql_type
7541) {
7542 DBUG_ENTER("spider_mysql_handler::set_where_pos");
7543 switch (sql_type)
7544 {
7545 case SPIDER_SQL_TYPE_SELECT_SQL:
7546 case SPIDER_SQL_TYPE_TMP_SQL:
7547 where_pos = sql.length();
7548 break;
7549 case SPIDER_SQL_TYPE_INSERT_SQL:
7550 case SPIDER_SQL_TYPE_UPDATE_SQL:
7551 case SPIDER_SQL_TYPE_DELETE_SQL:
7552 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
7553 where_pos = update_sql.length();
7554 break;
7555 case SPIDER_SQL_TYPE_HANDLER:
7556 ha_read_pos = ha_sql.length();
7557 break;
7558 default:
7559 break;
7560 }
7561 DBUG_VOID_RETURN;
7562}
7563
7564void spider_mysql_handler::set_where_to_pos(
7565 ulong sql_type
7566) {
7567 DBUG_ENTER("spider_mysql_handler::set_where_to_pos");
7568 switch (sql_type)
7569 {
7570 case SPIDER_SQL_TYPE_SELECT_SQL:
7571 case SPIDER_SQL_TYPE_TMP_SQL:
7572 sql.length(where_pos);
7573 break;
7574 case SPIDER_SQL_TYPE_INSERT_SQL:
7575 case SPIDER_SQL_TYPE_UPDATE_SQL:
7576 case SPIDER_SQL_TYPE_DELETE_SQL:
7577 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
7578 update_sql.length(where_pos);
7579 break;
7580 case SPIDER_SQL_TYPE_HANDLER:
7581 ha_sql.length(ha_read_pos);
7582 break;
7583 default:
7584 break;
7585 }
7586 DBUG_VOID_RETURN;
7587}
7588
7589int spider_mysql_handler::check_item_type(
7590 Item *item
7591) {
7592 int error_num;
7593 DBUG_ENTER("spider_mysql_handler::check_item_type");
7594 DBUG_PRINT("info",("spider this=%p", this));
7595 error_num = spider_db_print_item_type(item, spider, NULL, NULL, 0,
7596 spider_dbton_mysql.dbton_id, FALSE, NULL);
7597 DBUG_RETURN(error_num);
7598}
7599
7600int spider_mysql_handler::append_values_connector_part(
7601 ulong sql_type
7602) {
7603 int error_num;
7604 spider_string *str;
7605 DBUG_ENTER("spider_mysql_handler::append_values_connector_part");
7606 DBUG_PRINT("info",("spider this=%p", this));
7607 switch (sql_type)
7608 {
7609 case SPIDER_SQL_TYPE_SELECT_SQL:
7610 str = &sql;
7611 break;
7612 case SPIDER_SQL_TYPE_TMP_SQL:
7613 str = &tmp_sql;
7614 break;
7615 default:
7616 DBUG_RETURN(0);
7617 }
7618 error_num = append_values_connector(str);
7619 DBUG_RETURN(error_num);
7620}
7621
7622int spider_mysql_handler::append_values_connector(
7623 spider_string *str
7624) {
7625 DBUG_ENTER("spider_mysql_handler::append_values_connector");
7626 DBUG_PRINT("info",("spider this=%p", this));
7627 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN +
7628 SPIDER_SQL_COMMA_LEN + SPIDER_SQL_OPEN_PAREN_LEN))
7629 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7630 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
7631 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
7632 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
7633 DBUG_RETURN(0);
7634}
7635
7636int spider_mysql_handler::append_values_terminator_part(
7637 ulong sql_type
7638) {
7639 int error_num;
7640 spider_string *str;
7641 DBUG_ENTER("spider_mysql_handler::append_values_terminator_part");
7642 DBUG_PRINT("info",("spider this=%p", this));
7643 switch (sql_type)
7644 {
7645 case SPIDER_SQL_TYPE_SELECT_SQL:
7646 str = &sql;
7647 break;
7648 case SPIDER_SQL_TYPE_TMP_SQL:
7649 str = &tmp_sql;
7650 break;
7651 default:
7652 DBUG_RETURN(0);
7653 }
7654 error_num = append_values_terminator(str);
7655 DBUG_RETURN(error_num);
7656}
7657
7658int spider_mysql_handler::append_values_terminator(
7659 spider_string *str
7660) {
7661 DBUG_ENTER("spider_mysql_handler::append_values_terminator");
7662 DBUG_PRINT("info",("spider this=%p", this));
7663 str->length(str->length() -
7664 SPIDER_SQL_COMMA_LEN - SPIDER_SQL_OPEN_PAREN_LEN);
7665 DBUG_RETURN(0);
7666}
7667
7668int spider_mysql_handler::append_union_table_connector_part(
7669 ulong sql_type
7670) {
7671 int error_num;
7672 spider_string *str;
7673 DBUG_ENTER("spider_mysql_handler::append_union_table_connector_part");
7674 DBUG_PRINT("info",("spider this=%p", this));
7675 switch (sql_type)
7676 {
7677 case SPIDER_SQL_TYPE_SELECT_SQL:
7678 str = &sql;
7679 break;
7680 case SPIDER_SQL_TYPE_TMP_SQL:
7681 str = &tmp_sql;
7682 break;
7683 default:
7684 DBUG_RETURN(0);
7685 }
7686 error_num = append_union_table_connector(str);
7687 DBUG_RETURN(error_num);
7688}
7689
7690int spider_mysql_handler::append_union_table_connector(
7691 spider_string *str
7692) {
7693 DBUG_ENTER("spider_mysql_handler::append_union_table_connector");
7694 DBUG_PRINT("info",("spider this=%p", this));
7695 if (str->reserve((SPIDER_SQL_SPACE_LEN * 2) + SPIDER_SQL_UNION_ALL_LEN))
7696 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7697 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
7698 str->q_append(SPIDER_SQL_UNION_ALL_STR, SPIDER_SQL_UNION_ALL_LEN);
7699 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
7700 DBUG_RETURN(0);
7701}
7702
7703int spider_mysql_handler::append_union_table_terminator_part(
7704 ulong sql_type
7705) {
7706 int error_num;
7707 spider_string *str;
7708 DBUG_ENTER("spider_mysql_handler::append_union_table_terminator_part");
7709 DBUG_PRINT("info",("spider this=%p", this));
7710 switch (sql_type)
7711 {
7712 case SPIDER_SQL_TYPE_SELECT_SQL:
7713 str = &sql;
7714 break;
7715 default:
7716 DBUG_RETURN(0);
7717 }
7718 error_num = append_union_table_terminator(str);
7719 DBUG_RETURN(error_num);
7720}
7721
7722int spider_mysql_handler::append_union_table_terminator(
7723 spider_string *str
7724) {
7725 DBUG_ENTER("spider_mysql_handler::append_union_table_terminator");
7726 DBUG_PRINT("info",("spider this=%p", this));
7727 str->length(str->length() -
7728 ((SPIDER_SQL_SPACE_LEN * 2) + SPIDER_SQL_UNION_ALL_LEN));
7729 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
7730 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
7731 table_name_pos = str->length() + SPIDER_SQL_SPACE_LEN + SPIDER_SQL_A_LEN +
7732 SPIDER_SQL_COMMA_LEN;
7733 if (str->reserve(tmp_sql.length() - SPIDER_SQL_FROM_LEN))
7734 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7735 str->q_append(tmp_sql.ptr() + SPIDER_SQL_FROM_LEN,
7736 tmp_sql.length() - SPIDER_SQL_FROM_LEN);
7737 DBUG_RETURN(0);
7738}
7739
7740int spider_mysql_handler::append_key_column_values_part(
7741 const key_range *start_key,
7742 ulong sql_type
7743) {
7744 int error_num;
7745 spider_string *str;
7746 DBUG_ENTER("spider_mysql_handler::append_key_column_values_part");
7747 switch (sql_type)
7748 {
7749 case SPIDER_SQL_TYPE_SELECT_SQL:
7750 str = &sql;
7751 break;
7752 case SPIDER_SQL_TYPE_TMP_SQL:
7753 str = &tmp_sql;
7754 break;
7755 default:
7756 DBUG_RETURN(0);
7757 }
7758 error_num = append_key_column_values(str, start_key);
7759 DBUG_RETURN(error_num);
7760}
7761
7762int spider_mysql_handler::append_key_column_values(
7763 spider_string *str,
7764 const key_range *start_key
7765) {
7766 int error_num;
7767 const uchar *ptr;
7768 SPIDER_RESULT_LIST *result_list = &spider->result_list;
7769 SPIDER_SHARE *share = spider->share;
7770 KEY *key_info = result_list->key_info;
7771 uint length;
7772 uint store_length;
7773 key_part_map full_key_part_map =
7774 make_prev_keypart_map(spider_user_defined_key_parts(key_info));
7775 key_part_map start_key_part_map;
7776 KEY_PART_INFO *key_part;
7777 Field *field;
7778 DBUG_ENTER("spider_mysql_handler::append_key_column_values");
7779 start_key_part_map = start_key->keypart_map & full_key_part_map;
7780 DBUG_PRINT("info", ("spider spider_user_defined_key_parts=%u",
7781 spider_user_defined_key_parts(key_info)));
7782 DBUG_PRINT("info", ("spider full_key_part_map=%lu", full_key_part_map));
7783 DBUG_PRINT("info", ("spider start_key_part_map=%lu", start_key_part_map));
7784
7785 if (!start_key_part_map)
7786 DBUG_RETURN(0);
7787
7788 for (
7789 key_part = key_info->key_part,
7790 length = 0;
7791 start_key_part_map;
7792 start_key_part_map >>= 1,
7793 key_part++,
7794 length += store_length
7795 ) {
7796 store_length = key_part->store_length;
7797 ptr = start_key->key + length;
7798 field = key_part->field;
7799 if ((error_num = spider_db_append_null_value(str, key_part, &ptr)))
7800 {
7801 if (error_num > 0)
7802 DBUG_RETURN(error_num);
7803 } else {
7804 if (spider_db_mysql_utility.append_column_value(spider, str, field, ptr,
7805 share->access_charset))
7806 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7807 }
7808
7809 if (str->reserve(SPIDER_SQL_COMMA_LEN))
7810 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7811 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
7812 }
7813 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
7814 DBUG_RETURN(0);
7815}
7816
7817int spider_mysql_handler::append_key_column_values_with_name_part(
7818 const key_range *start_key,
7819 ulong sql_type
7820) {
7821 int error_num;
7822 spider_string *str;
7823 DBUG_ENTER("spider_mysql_handler::append_key_column_values_with_name_part");
7824 switch (sql_type)
7825 {
7826 case SPIDER_SQL_TYPE_SELECT_SQL:
7827 str = &sql;
7828 break;
7829 case SPIDER_SQL_TYPE_TMP_SQL:
7830 str = &tmp_sql;
7831 break;
7832 default:
7833 DBUG_RETURN(0);
7834 }
7835 error_num = append_key_column_values_with_name(str, start_key);
7836 DBUG_RETURN(error_num);
7837}
7838
7839int spider_mysql_handler::append_key_column_values_with_name(
7840 spider_string *str,
7841 const key_range *start_key
7842) {
7843 int error_num;
7844 const uchar *ptr;
7845 SPIDER_RESULT_LIST *result_list = &spider->result_list;
7846 SPIDER_SHARE *share = spider->share;
7847 KEY *key_info = result_list->key_info;
7848 uint length;
7849 uint key_name_length, key_count;
7850 uint store_length;
7851 key_part_map full_key_part_map =
7852 make_prev_keypart_map(spider_user_defined_key_parts(key_info));
7853 key_part_map start_key_part_map;
7854 KEY_PART_INFO *key_part;
7855 Field *field;
7856 char tmp_buf[MAX_FIELD_WIDTH];
7857 DBUG_ENTER("spider_mysql_handler::append_key_column_values_with_name");
7858 start_key_part_map = start_key->keypart_map & full_key_part_map;
7859 DBUG_PRINT("info", ("spider spider_user_defined_key_parts=%u",
7860 spider_user_defined_key_parts(key_info)));
7861 DBUG_PRINT("info", ("spider full_key_part_map=%lu", full_key_part_map));
7862 DBUG_PRINT("info", ("spider start_key_part_map=%lu", start_key_part_map));
7863
7864 if (!start_key_part_map)
7865 DBUG_RETURN(0);
7866
7867 for (
7868 key_part = key_info->key_part,
7869 length = 0,
7870 key_count = 0;
7871 start_key_part_map;
7872 start_key_part_map >>= 1,
7873 key_part++,
7874 length += store_length,
7875 key_count++
7876 ) {
7877 store_length = key_part->store_length;
7878 ptr = start_key->key + length;
7879 field = key_part->field;
7880 if ((error_num = spider_db_append_null_value(str, key_part, &ptr)))
7881 {
7882 if (error_num > 0)
7883 DBUG_RETURN(error_num);
7884 } else {
7885 if (spider_db_mysql_utility.append_column_value(spider, str, field, ptr,
7886 share->access_charset))
7887 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7888 }
7889
7890 key_name_length = my_sprintf(tmp_buf, (tmp_buf, "c%u", key_count));
7891 if (str->reserve(SPIDER_SQL_SPACE_LEN + key_name_length +
7892 SPIDER_SQL_COMMA_LEN))
7893 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7894 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
7895 str->q_append(tmp_buf, key_name_length);
7896 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
7897 }
7898 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
7899 DBUG_RETURN(0);
7900}
7901
7902int spider_mysql_handler::append_key_where_part(
7903 const key_range *start_key,
7904 const key_range *end_key,
7905 ulong sql_type
7906) {
7907 int error_num;
7908 spider_string *str, *str_part = NULL, *str_part2 = NULL;
7909 bool set_order;
7910 DBUG_ENTER("spider_mysql_handler::append_key_where_part");
7911 switch (sql_type)
7912 {
7913 case SPIDER_SQL_TYPE_SELECT_SQL:
7914 str = &sql;
7915 set_order = FALSE;
7916 break;
7917 case SPIDER_SQL_TYPE_TMP_SQL:
7918 str = &tmp_sql;
7919 set_order = FALSE;
7920 break;
7921 case SPIDER_SQL_TYPE_INSERT_SQL:
7922 case SPIDER_SQL_TYPE_UPDATE_SQL:
7923 case SPIDER_SQL_TYPE_DELETE_SQL:
7924 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
7925 str = &update_sql;
7926 set_order = FALSE;
7927 break;
7928 case SPIDER_SQL_TYPE_HANDLER:
7929 str = &ha_sql;
7930 ha_read_pos = str->length();
7931 str_part = &sql_part;
7932 str_part2 = &sql_part2;
7933 str_part->length(0);
7934 str_part2->length(0);
7935 set_order = TRUE;
7936 break;
7937 default:
7938 DBUG_RETURN(0);
7939 }
7940 error_num = append_key_where(str, str_part, str_part2, start_key, end_key,
7941 sql_type, set_order);
7942 DBUG_RETURN(error_num);
7943}
7944
7945int spider_mysql_handler::append_key_where(
7946 spider_string *str,
7947 spider_string *str_part,
7948 spider_string *str_part2,
7949 const key_range *start_key,
7950 const key_range *end_key,
7951 ulong sql_type,
7952 bool set_order
7953) {
7954 int error_num;
7955 DBUG_ENTER("spider_mysql_handler::append_key_where");
7956 error_num = spider_db_append_key_where_internal(str, str_part, str_part2,
7957 start_key, end_key, spider, set_order, sql_type,
7958 spider_dbton_mysql.dbton_id);
7959 DBUG_RETURN(error_num);
7960}
7961
7962int spider_mysql_handler::append_is_null_part(
7963 ulong sql_type,
7964 KEY_PART_INFO *key_part,
7965 const key_range *key,
7966 const uchar **ptr,
7967 bool key_eq,
7968 bool tgt_final
7969) {
7970 int error_num;
7971 spider_string *str, *str_part = NULL, *str_part2 = NULL;
7972 DBUG_ENTER("spider_mysql_handler::append_is_null_part");
7973 DBUG_PRINT("info",("spider this=%p", this));
7974 switch (sql_type)
7975 {
7976 case SPIDER_SQL_TYPE_SELECT_SQL:
7977 case SPIDER_SQL_TYPE_TMP_SQL:
7978 str = &sql;
7979 break;
7980 case SPIDER_SQL_TYPE_INSERT_SQL:
7981 case SPIDER_SQL_TYPE_UPDATE_SQL:
7982 case SPIDER_SQL_TYPE_DELETE_SQL:
7983 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
7984 str = &update_sql;
7985 break;
7986 case SPIDER_SQL_TYPE_HANDLER:
7987 str = &ha_sql;
7988 str_part = &sql_part;
7989 str_part2 = &sql_part2;
7990 break;
7991 default:
7992 DBUG_RETURN(0);
7993 }
7994 error_num = append_is_null(sql_type, str, str_part, str_part2,
7995 key_part, key, ptr, key_eq, tgt_final);
7996 DBUG_RETURN(error_num);
7997}
7998
7999int spider_mysql_handler::append_is_null(
8000 ulong sql_type,
8001 spider_string *str,
8002 spider_string *str_part,
8003 spider_string *str_part2,
8004 KEY_PART_INFO *key_part,
8005 const key_range *key,
8006 const uchar **ptr,
8007 bool key_eq,
8008 bool tgt_final
8009) {
8010 DBUG_ENTER("spider_mysql_handler::append_is_null");
8011 DBUG_PRINT("info",("spider this=%p", this));
8012 DBUG_PRINT("info",("spider key_eq=%s", key_eq ? "TRUE" : "FALSE"));
8013 if (key_part->null_bit)
8014 {
8015 if (*(*ptr)++)
8016 {
8017 if (sql_type == SPIDER_SQL_TYPE_HANDLER)
8018 {
8019 if (
8020 key_eq ||
8021 key->flag == HA_READ_KEY_EXACT ||
8022 key->flag == HA_READ_KEY_OR_NEXT
8023 ) {
8024 if (tgt_final)
8025 {
8026 if (str->reserve(SPIDER_SQL_EQUAL_LEN))
8027 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8028 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
8029 }
8030 str = str_part;
8031 if (str->reserve(SPIDER_SQL_NULL_LEN))
8032 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8033 str->q_append(SPIDER_SQL_NULL_STR, SPIDER_SQL_NULL_LEN);
8034 } else {
8035 if (str_part->length() == SPIDER_SQL_OPEN_PAREN_LEN)
8036 {
8037 str = str_part;
8038 /* first index column */
8039 str->length(str->length() - SPIDER_SQL_OPEN_PAREN_LEN);
8040 ha_next_pos = str->length();
8041 if (str->reserve(SPIDER_SQL_FIRST_LEN))
8042 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8043 str->q_append(SPIDER_SQL_FIRST_STR, SPIDER_SQL_FIRST_LEN);
8044 spider->result_list.ha_read_kind = 1;
8045 } else if (tgt_final)
8046 {
8047 if (str->reserve(SPIDER_SQL_GT_LEN))
8048 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8049 str->q_append(SPIDER_SQL_GT_STR, SPIDER_SQL_GT_LEN);
8050 str = str_part;
8051 if (str->reserve(SPIDER_SQL_NULL_LEN))
8052 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8053 str->q_append(SPIDER_SQL_NULL_STR, SPIDER_SQL_NULL_LEN);
8054 }
8055 }
8056 str = str_part2;
8057 }
8058 if (
8059 key_eq ||
8060 key->flag == HA_READ_KEY_EXACT ||
8061 key->flag == HA_READ_KEY_OR_NEXT
8062 ) {
8063 if (str->reserve(SPIDER_SQL_IS_NULL_LEN +
8064 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
8065 mysql_share->column_name_str[key_part->field->field_index].length()))
8066 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8067 mysql_share->append_column_name(str, key_part->field->field_index);
8068 str->q_append(SPIDER_SQL_IS_NULL_STR, SPIDER_SQL_IS_NULL_LEN);
8069 } else {
8070 if (str->reserve(SPIDER_SQL_IS_NOT_NULL_LEN +
8071 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
8072 mysql_share->column_name_str[key_part->field->field_index].length()))
8073 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8074 mysql_share->append_column_name(str, key_part->field->field_index);
8075 str->q_append(SPIDER_SQL_IS_NOT_NULL_STR, SPIDER_SQL_IS_NOT_NULL_LEN);
8076 }
8077 DBUG_RETURN(-1);
8078 }
8079 }
8080 DBUG_RETURN(0);
8081}
8082
8083int spider_mysql_handler::append_where_terminator_part(
8084 ulong sql_type,
8085 bool set_order,
8086 int key_count
8087) {
8088 int error_num;
8089 spider_string *str, *str_part = NULL, *str_part2 = NULL;
8090 DBUG_ENTER("spider_mysql_handler::append_where_terminator_part");
8091 DBUG_PRINT("info",("spider this=%p", this));
8092 switch (sql_type)
8093 {
8094 case SPIDER_SQL_TYPE_SELECT_SQL:
8095 case SPIDER_SQL_TYPE_TMP_SQL:
8096 str = &sql;
8097 break;
8098 case SPIDER_SQL_TYPE_INSERT_SQL:
8099 case SPIDER_SQL_TYPE_UPDATE_SQL:
8100 case SPIDER_SQL_TYPE_DELETE_SQL:
8101 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
8102 str = &update_sql;
8103 break;
8104 case SPIDER_SQL_TYPE_HANDLER:
8105 str = &ha_sql;
8106 str_part = &sql_part;
8107 str_part2 = &sql_part2;
8108 break;
8109 default:
8110 DBUG_RETURN(0);
8111 }
8112 error_num = append_where_terminator(sql_type, str, str_part, str_part2,
8113 set_order, key_count);
8114 DBUG_RETURN(error_num);
8115}
8116
8117int spider_mysql_handler::append_where_terminator(
8118 ulong sql_type,
8119 spider_string *str,
8120 spider_string *str_part,
8121 spider_string *str_part2,
8122 bool set_order,
8123 int key_count
8124) {
8125 SPIDER_RESULT_LIST *result_list = &spider->result_list;
8126 DBUG_ENTER("spider_mysql_handler::append_where_terminator");
8127 DBUG_PRINT("info",("spider this=%p", this));
8128 if (sql_type != SPIDER_SQL_TYPE_HANDLER)
8129 {
8130 str->length(str->length() - SPIDER_SQL_AND_LEN);
8131 if (!set_order)
8132 result_list->key_order = key_count;
8133 } else {
8134 str_part2->length(str_part2->length() - SPIDER_SQL_AND_LEN);
8135
8136 str_part->length(str_part->length() - SPIDER_SQL_COMMA_LEN);
8137 if (!result_list->ha_read_kind)
8138 str_part->q_append(SPIDER_SQL_CLOSE_PAREN_STR,
8139 SPIDER_SQL_CLOSE_PAREN_LEN);
8140 if (str->append(*str_part))
8141 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8142 uint clause_length = str->length() - ha_next_pos;
8143 if (clause_length < SPIDER_SQL_NEXT_LEN)
8144 {
8145 int roop_count;
8146 clause_length = SPIDER_SQL_NEXT_LEN - clause_length;
8147 if (str->reserve(clause_length))
8148 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8149 for (roop_count = 0; roop_count < (int) clause_length; roop_count++)
8150 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
8151 }
8152 }
8153 DBUG_RETURN(0);
8154}
8155
8156int spider_mysql_handler::append_match_where_part(
8157 ulong sql_type
8158) {
8159 int error_num;
8160 spider_string *str;
8161 DBUG_ENTER("spider_mysql_handler::append_match_where_part");
8162 switch (sql_type)
8163 {
8164 case SPIDER_SQL_TYPE_SELECT_SQL:
8165 str = &sql;
8166 break;
8167 default:
8168 DBUG_ASSERT(0);
8169 DBUG_RETURN(0);
8170 }
8171 error_num = append_match_where(str);
8172 DBUG_RETURN(error_num);
8173}
8174
8175int spider_mysql_handler::append_match_where(
8176 spider_string *str
8177) {
8178 int error_num;
8179 bool first = TRUE;
8180 st_spider_ft_info *ft_info = spider->ft_first;
8181 DBUG_ENTER("spider_mysql_handler::append_match_where");
8182 if (spider->ft_current)
8183 {
8184 while (TRUE)
8185 {
8186 if (ft_info->used_in_where)
8187 {
8188 if (first)
8189 {
8190 if (str->reserve(SPIDER_SQL_WHERE_LEN))
8191 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8192 str->q_append(SPIDER_SQL_WHERE_STR, SPIDER_SQL_WHERE_LEN);
8193 first = FALSE;
8194 }
8195 if ((error_num = append_match_against(str, ft_info, NULL, 0)))
8196 DBUG_RETURN(error_num);
8197 if (str->reserve(SPIDER_SQL_AND_LEN))
8198 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8199 str->q_append(SPIDER_SQL_AND_STR, SPIDER_SQL_AND_LEN);
8200 }
8201
8202 if (ft_info == spider->ft_current)
8203 break;
8204 ft_info = ft_info->next;
8205 }
8206 if (!first)
8207 str->length(str->length() - SPIDER_SQL_AND_LEN);
8208 }
8209 DBUG_RETURN(0);
8210}
8211
8212int spider_mysql_handler::append_update_where(
8213 spider_string *str,
8214 const TABLE *table,
8215 my_ptrdiff_t ptr_diff
8216) {
8217 uint field_name_length;
8218 Field **field;
8219 SPIDER_SHARE *share = spider->share;
8220 DBUG_ENTER("spider_mysql_handler::append_update_where");
8221 DBUG_PRINT("info", ("spider table->s->primary_key=%s",
8222 table->s->primary_key != MAX_KEY ? "TRUE" : "FALSE"));
8223 if (str->reserve(SPIDER_SQL_WHERE_LEN))
8224 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8225 str->q_append(SPIDER_SQL_WHERE_STR, SPIDER_SQL_WHERE_LEN);
8226 for (field = table->field; *field; field++)
8227 {
8228 DBUG_PRINT("info", ("spider bitmap=%s",
8229 bitmap_is_set(table->read_set, (*field)->field_index) ?
8230 "TRUE" : "FALSE"));
8231 if (
8232 table->s->primary_key == MAX_KEY ||
8233 bitmap_is_set(table->read_set, (*field)->field_index)
8234 ) {
8235 field_name_length =
8236 mysql_share->column_name_str[(*field)->field_index].length();
8237 if ((*field)->is_null(ptr_diff))
8238 {
8239 if (str->reserve(field_name_length +
8240 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
8241 SPIDER_SQL_IS_NULL_LEN + SPIDER_SQL_AND_LEN))
8242 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8243 mysql_share->append_column_name(str, (*field)->field_index);
8244 str->q_append(SPIDER_SQL_IS_NULL_STR, SPIDER_SQL_IS_NULL_LEN);
8245 } else {
8246 if (str->reserve(field_name_length +
8247 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
8248 SPIDER_SQL_EQUAL_LEN))
8249 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8250 mysql_share->append_column_name(str, (*field)->field_index);
8251 str->q_append(SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN);
8252 (*field)->move_field_offset(ptr_diff);
8253 if (
8254 spider_db_mysql_utility.
8255 append_column_value(spider, str, *field, NULL,
8256 share->access_charset) ||
8257 str->reserve(SPIDER_SQL_AND_LEN)
8258 )
8259 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8260 (*field)->move_field_offset(-ptr_diff);
8261 }
8262 str->q_append(SPIDER_SQL_AND_STR, SPIDER_SQL_AND_LEN);
8263 }
8264 }
8265 str->length(str->length() - SPIDER_SQL_AND_LEN);
8266 if (str->reserve(SPIDER_SQL_LIMIT1_LEN))
8267 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8268 str->q_append(SPIDER_SQL_LIMIT1_STR, SPIDER_SQL_LIMIT1_LEN);
8269 DBUG_RETURN(0);
8270}
8271
8272int spider_mysql_handler::append_condition_part(
8273 const char *alias,
8274 uint alias_length,
8275 ulong sql_type,
8276 bool test_flg
8277) {
8278 int error_num;
8279 spider_string *str;
8280 bool start_where = FALSE;
8281 DBUG_ENTER("spider_mysql_handler::append_condition_part");
8282 switch (sql_type)
8283 {
8284 case SPIDER_SQL_TYPE_SELECT_SQL:
8285 if (test_flg)
8286 {
8287 str = NULL;
8288 } else {
8289 str = &sql;
8290 start_where = ((int) str->length() == where_pos);
8291 }
8292 break;
8293 case SPIDER_SQL_TYPE_TMP_SQL:
8294 if (test_flg)
8295 {
8296 str = NULL;
8297 } else {
8298 str = &tmp_sql;
8299 start_where = ((int) str->length() == where_pos);
8300 }
8301 break;
8302 case SPIDER_SQL_TYPE_INSERT_SQL:
8303 case SPIDER_SQL_TYPE_UPDATE_SQL:
8304 case SPIDER_SQL_TYPE_DELETE_SQL:
8305 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
8306 if (test_flg)
8307 {
8308 str = NULL;
8309 } else {
8310 str = &update_sql;
8311 start_where = ((int) str->length() == where_pos);
8312 }
8313 break;
8314 case SPIDER_SQL_TYPE_HANDLER:
8315 if (test_flg)
8316 {
8317 str = NULL;
8318 } else {
8319 str = &ha_sql;
8320 start_where = TRUE;
8321 if (spider->active_index == MAX_KEY)
8322 {
8323 set_where_pos(SPIDER_SQL_TYPE_HANDLER);
8324 if (str->reserve(SPIDER_SQL_READ_LEN + SPIDER_SQL_FIRST_LEN))
8325 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8326 str->q_append(SPIDER_SQL_READ_STR, SPIDER_SQL_READ_LEN);
8327 ha_next_pos = str->length();
8328 str->q_append(SPIDER_SQL_FIRST_STR, SPIDER_SQL_FIRST_LEN);
8329 sql_part2.length(0);
8330 }
8331 ha_where_pos = str->length();
8332
8333 if (
8334 spider->sql_command == SQLCOM_HA_READ ||
8335 !spider->result_list.use_both_key
8336 ) {
8337 if (sql_part2.length())
8338 {
8339 str->append(sql_part2);
8340 start_where = FALSE;
8341 }
8342 } else {
8343 DBUG_RETURN(0);
8344 }
8345 }
8346 break;
8347 default:
8348 DBUG_RETURN(0);
8349 }
8350 error_num = append_condition(str, alias, alias_length, start_where,
8351 sql_type);
8352 DBUG_RETURN(error_num);
8353}
8354
8355int spider_mysql_handler::append_condition(
8356 spider_string *str,
8357 const char *alias,
8358 uint alias_length,
8359 bool start_where,
8360 ulong sql_type
8361) {
8362 int error_num, restart_pos = 0, start_where_pos;
8363 SPIDER_CONDITION *tmp_cond = spider->condition;
8364 DBUG_ENTER("spider_mysql_handler::append_condition");
8365 if (str && start_where)
8366 {
8367 start_where_pos = str->length();
8368 } else {
8369 start_where_pos = 0;
8370 }
8371
8372 if (spider->is_clone && !tmp_cond)
8373 {
8374 tmp_cond = spider->pt_clone_source_handler->condition;
8375 }
8376
8377 while (tmp_cond)
8378 {
8379 if (str)
8380 {
8381 restart_pos = str->length();
8382 if (start_where)
8383 {
8384 if (str->reserve(SPIDER_SQL_WHERE_LEN))
8385 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8386 str->q_append(SPIDER_SQL_WHERE_STR, SPIDER_SQL_WHERE_LEN);
8387 start_where = FALSE;
8388 } else {
8389 if (str->reserve(SPIDER_SQL_AND_LEN))
8390 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8391 str->q_append(SPIDER_SQL_AND_STR, SPIDER_SQL_AND_LEN);
8392 }
8393 }
8394 if ((error_num = spider_db_print_item_type(
8395 (Item *) tmp_cond->cond, spider, str, alias, alias_length,
8396 spider_dbton_mysql.dbton_id, FALSE, NULL)))
8397 {
8398 if (str && error_num == ER_SPIDER_COND_SKIP_NUM)
8399 {
8400 DBUG_PRINT("info",("spider COND skip"));
8401 str->length(restart_pos);
8402 start_where = (restart_pos == start_where_pos);
8403 } else
8404 DBUG_RETURN(error_num);
8405 }
8406 tmp_cond = tmp_cond->next;
8407 }
8408 DBUG_RETURN(0);
8409}
8410
8411int spider_mysql_handler::append_match_against_part(
8412 ulong sql_type,
8413 st_spider_ft_info *ft_info,
8414 const char *alias,
8415 uint alias_length
8416) {
8417 int error_num;
8418 spider_string *str;
8419 DBUG_ENTER("spider_mysql_handler::append_match_against_part");
8420 DBUG_PRINT("info",("spider this=%p", this));
8421 switch (sql_type)
8422 {
8423 case SPIDER_SQL_TYPE_SELECT_SQL:
8424 str = &sql;
8425 break;
8426 default:
8427 DBUG_RETURN(0);
8428 }
8429 error_num = append_match_against(str, ft_info, alias, alias_length);
8430 DBUG_RETURN(error_num);
8431}
8432
8433int spider_mysql_handler::append_match_against(
8434 spider_string *str,
8435 st_spider_ft_info *ft_info,
8436 const char *alias,
8437 uint alias_length
8438) {
8439 SPIDER_SHARE *share = spider->share;
8440 TABLE *table = spider->get_table();
8441 String *ft_init_key;
8442 KEY *key_info;
8443 uint key_name_length;
8444 int key_count;
8445 KEY_PART_INFO *key_part;
8446 Field *field;
8447 DBUG_ENTER("spider_mysql_handler::append_match_against");
8448 DBUG_PRINT("info",("spider this=%p", this));
8449 if (str->reserve(SPIDER_SQL_MATCH_LEN))
8450 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8451 str->q_append(SPIDER_SQL_MATCH_STR, SPIDER_SQL_MATCH_LEN);
8452
8453 ft_init_key = ft_info->key;
8454 key_info = &table->key_info[ft_info->inx];
8455 DBUG_PRINT("info", ("spider spider_user_defined_key_parts=%u",
8456 spider_user_defined_key_parts(key_info)));
8457
8458 for (
8459 key_part = key_info->key_part,
8460 key_count = 0;
8461 key_count < (int) spider_user_defined_key_parts(key_info);
8462 key_part++,
8463 key_count++
8464 ) {
8465 field = key_part->field;
8466 key_name_length =
8467 mysql_share->column_name_str[field->field_index].length();
8468 if (alias_length)
8469 {
8470 if (str->reserve(alias_length + key_name_length +
8471 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
8472 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8473 str->q_append(alias, alias_length);
8474 } else {
8475 if (str->reserve(key_name_length +
8476 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
8477 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8478 }
8479 mysql_share->append_column_name(str, field->field_index);
8480 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
8481 }
8482 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
8483 if (str->reserve(SPIDER_SQL_AGAINST_LEN + SPIDER_SQL_VALUE_QUOTE_LEN))
8484 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8485 str->q_append(SPIDER_SQL_AGAINST_STR, SPIDER_SQL_AGAINST_LEN);
8486 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
8487
8488 char buf[MAX_FIELD_WIDTH];
8489 spider_string tmp_str(buf, MAX_FIELD_WIDTH, share->access_charset);
8490 tmp_str.init_calc_mem(116);
8491 tmp_str.length(0);
8492 if (
8493 tmp_str.append(ft_init_key->ptr(), ft_init_key->length(),
8494 ft_init_key->charset()) ||
8495 str->reserve(tmp_str.length() * 2) ||
8496 spider_db_mysql_utility.append_escaped_util(str, tmp_str.get_str())
8497 )
8498 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8499 str->mem_calc();
8500
8501 if (str->reserve(
8502 SPIDER_SQL_VALUE_QUOTE_LEN + SPIDER_SQL_CLOSE_PAREN_LEN +
8503 ((ft_info->flags & FT_BOOL) ? SPIDER_SQL_IN_BOOLEAN_MODE_LEN : 0) +
8504 ((ft_info->flags & FT_EXPAND) ?
8505 SPIDER_SQL_WITH_QUERY_EXPANSION_LEN : 0)
8506 ))
8507 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8508 str->q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
8509 if (ft_info->flags & FT_BOOL)
8510 str->q_append(SPIDER_SQL_IN_BOOLEAN_MODE_STR,
8511 SPIDER_SQL_IN_BOOLEAN_MODE_LEN);
8512 if (ft_info->flags & FT_EXPAND)
8513 str->q_append(SPIDER_SQL_WITH_QUERY_EXPANSION_STR,
8514 SPIDER_SQL_WITH_QUERY_EXPANSION_LEN);
8515 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
8516 DBUG_RETURN(0);
8517}
8518
8519int spider_mysql_handler::append_match_select_part(
8520 ulong sql_type,
8521 const char *alias,
8522 uint alias_length
8523) {
8524 int error_num;
8525 spider_string *str;
8526 DBUG_ENTER("spider_mysql_handler::append_match_select_part");
8527 DBUG_PRINT("info",("spider this=%p", this));
8528 switch (sql_type)
8529 {
8530 case SPIDER_SQL_TYPE_SELECT_SQL:
8531 str = &sql;
8532 break;
8533 default:
8534 DBUG_RETURN(0);
8535 }
8536 error_num = append_match_select(str, alias, alias_length);
8537 DBUG_RETURN(error_num);
8538}
8539
8540int spider_mysql_handler::append_match_select(
8541 spider_string *str,
8542 const char *alias,
8543 uint alias_length
8544) {
8545 int error_num;
8546 DBUG_ENTER("spider_mysql_handler::append_match_select");
8547 DBUG_PRINT("info",("spider this=%p", this));
8548 if (spider->ft_current)
8549 {
8550 st_spider_ft_info *ft_info = spider->ft_first;
8551 while (TRUE)
8552 {
8553 if ((error_num = append_match_against(str, ft_info,
8554 alias, alias_length)))
8555 DBUG_RETURN(error_num);
8556 if (str->reserve(SPIDER_SQL_COMMA_LEN))
8557 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8558 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
8559 if (ft_info == spider->ft_current)
8560 break;
8561 ft_info = ft_info->next;
8562 }
8563 }
8564 DBUG_RETURN(0);
8565}
8566
8567#ifdef HANDLER_HAS_DIRECT_AGGREGATE
8568int spider_mysql_handler::append_sum_select_part(
8569 ulong sql_type,
8570 const char *alias,
8571 uint alias_length
8572) {
8573 int error_num;
8574 spider_string *str;
8575 DBUG_ENTER("spider_mysql_handler::append_sum_select_part");
8576 DBUG_PRINT("info",("spider this=%p", this));
8577 switch (sql_type)
8578 {
8579 case SPIDER_SQL_TYPE_SELECT_SQL:
8580 str = &sql;
8581 break;
8582 default:
8583 DBUG_RETURN(0);
8584 }
8585 error_num = append_sum_select(str, alias, alias_length);
8586 DBUG_RETURN(error_num);
8587}
8588
8589int spider_mysql_handler::append_sum_select(
8590 spider_string *str,
8591 const char *alias,
8592 uint alias_length
8593) {
8594 int error_num;
8595 st_select_lex *select_lex;
8596 DBUG_ENTER("spider_mysql_handler::append_sum_select");
8597 DBUG_PRINT("info",("spider this=%p", this));
8598 select_lex = spider_get_select_lex(spider);
8599 JOIN *join = select_lex->join;
8600 Item_sum **item_sum_ptr;
8601 for (item_sum_ptr = join->sum_funcs; *item_sum_ptr; ++item_sum_ptr)
8602 {
8603 if ((error_num = spider_db_mysql_utility.open_item_sum_func(*item_sum_ptr,
8604 spider, str, alias, alias_length, FALSE, NULL)))
8605 {
8606 DBUG_RETURN(error_num);
8607 }
8608 if (str->reserve(SPIDER_SQL_COMMA_LEN))
8609 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8610 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
8611 }
8612 DBUG_RETURN(0);
8613}
8614#endif
8615
8616void spider_mysql_handler::set_order_pos(
8617 ulong sql_type
8618) {
8619 DBUG_ENTER("spider_mysql_handler::set_order_pos");
8620 switch (sql_type)
8621 {
8622 case SPIDER_SQL_TYPE_SELECT_SQL:
8623 case SPIDER_SQL_TYPE_TMP_SQL:
8624 order_pos = sql.length();
8625 break;
8626 case SPIDER_SQL_TYPE_INSERT_SQL:
8627 case SPIDER_SQL_TYPE_UPDATE_SQL:
8628 case SPIDER_SQL_TYPE_DELETE_SQL:
8629 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
8630 order_pos = update_sql.length();
8631 break;
8632 case SPIDER_SQL_TYPE_HANDLER:
8633 ha_next_pos = ha_sql.length();
8634 break;
8635 default:
8636 DBUG_ASSERT(0);
8637 break;
8638 }
8639 DBUG_VOID_RETURN;
8640}
8641
8642void spider_mysql_handler::set_order_to_pos(
8643 ulong sql_type
8644) {
8645 DBUG_ENTER("spider_mysql_handler::set_order_to_pos");
8646 switch (sql_type)
8647 {
8648 case SPIDER_SQL_TYPE_SELECT_SQL:
8649 case SPIDER_SQL_TYPE_TMP_SQL:
8650 sql.length(order_pos);
8651 break;
8652 case SPIDER_SQL_TYPE_INSERT_SQL:
8653 case SPIDER_SQL_TYPE_UPDATE_SQL:
8654 case SPIDER_SQL_TYPE_DELETE_SQL:
8655 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
8656 update_sql.length(order_pos);
8657 break;
8658 case SPIDER_SQL_TYPE_HANDLER:
8659 ha_sql.length(ha_next_pos);
8660 break;
8661 default:
8662 DBUG_ASSERT(0);
8663 break;
8664 }
8665 DBUG_VOID_RETURN;
8666}
8667
8668#ifdef HANDLER_HAS_DIRECT_AGGREGATE
8669int spider_mysql_handler::append_group_by_part(
8670 const char *alias,
8671 uint alias_length,
8672 ulong sql_type
8673) {
8674 int error_num;
8675 spider_string *str;
8676 DBUG_ENTER("spider_mysql_handler::append_group_by_part");
8677 DBUG_PRINT("info",("spider this=%p", this));
8678 switch (sql_type)
8679 {
8680 case SPIDER_SQL_TYPE_SELECT_SQL:
8681 case SPIDER_SQL_TYPE_TMP_SQL:
8682 str = &sql;
8683 break;
8684 case SPIDER_SQL_TYPE_INSERT_SQL:
8685 case SPIDER_SQL_TYPE_UPDATE_SQL:
8686 case SPIDER_SQL_TYPE_DELETE_SQL:
8687 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
8688 str = &update_sql;
8689 break;
8690 case SPIDER_SQL_TYPE_HANDLER:
8691 str = &ha_sql;
8692 break;
8693 default:
8694 DBUG_RETURN(0);
8695 }
8696 error_num = append_group_by(str, alias, alias_length);
8697 DBUG_RETURN(error_num);
8698}
8699
8700int spider_mysql_handler::append_group_by(
8701 spider_string *str,
8702 const char *alias,
8703 uint alias_length
8704) {
8705 int error_num;
8706 st_select_lex *select_lex;
8707 DBUG_ENTER("spider_mysql_handler::append_group_by");
8708 DBUG_PRINT("info",("spider this=%p", this));
8709 select_lex = spider_get_select_lex(spider);
8710 ORDER *group = (ORDER *) select_lex->group_list.first;
8711 if (group)
8712 {
8713 if (str->reserve(SPIDER_SQL_GROUP_LEN))
8714 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8715 str->q_append(SPIDER_SQL_GROUP_STR, SPIDER_SQL_GROUP_LEN);
8716 for (; group; group = group->next)
8717 {
8718 if ((error_num = spider_db_print_item_type((*group->item), spider, str,
8719 alias, alias_length, spider_dbton_mysql.dbton_id, FALSE, NULL)))
8720 {
8721 DBUG_RETURN(error_num);
8722 }
8723 if (str->reserve(SPIDER_SQL_COMMA_LEN))
8724 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8725 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
8726 }
8727 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
8728 }
8729 DBUG_RETURN(0);
8730}
8731#endif
8732
8733int spider_mysql_handler::append_key_order_for_merge_with_alias_part(
8734 const char *alias,
8735 uint alias_length,
8736 ulong sql_type
8737) {
8738 int error_num;
8739 spider_string *str;
8740 DBUG_ENTER("spider_mysql_handler::append_key_order_for_merge_with_alias_part");
8741 DBUG_PRINT("info",("spider this=%p", this));
8742 switch (sql_type)
8743 {
8744 case SPIDER_SQL_TYPE_SELECT_SQL:
8745 case SPIDER_SQL_TYPE_TMP_SQL:
8746 str = &sql;
8747 break;
8748 case SPIDER_SQL_TYPE_INSERT_SQL:
8749 case SPIDER_SQL_TYPE_UPDATE_SQL:
8750 case SPIDER_SQL_TYPE_DELETE_SQL:
8751 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
8752 str = &update_sql;
8753 break;
8754 case SPIDER_SQL_TYPE_HANDLER:
8755 str = &ha_sql;
8756 ha_limit_pos = ha_sql.length();
8757 break;
8758 default:
8759 DBUG_RETURN(0);
8760 }
8761 error_num = append_key_order_for_merge_with_alias(str, alias, alias_length);
8762 DBUG_RETURN(error_num);
8763}
8764
8765int spider_mysql_handler::append_key_order_for_merge_with_alias(
8766 spider_string *str,
8767 const char *alias,
8768 uint alias_length
8769) {
8770 /* sort for index merge */
8771 TABLE *table = spider->get_table();
8772 int length;
8773 Field *field;
8774 uint key_name_length;
8775 DBUG_ENTER("spider_mysql_handler::append_key_order_for_merge_with_alias");
8776 DBUG_PRINT("info",("spider this=%p", this));
8777#ifdef HANDLER_HAS_DIRECT_AGGREGATE
8778 if (spider->result_list.direct_aggregate)
8779 {
8780 int error_num;
8781 if ((error_num = append_group_by(str, alias, alias_length)))
8782 DBUG_RETURN(error_num);
8783 }
8784#endif
8785 if (table->s->primary_key < MAX_KEY)
8786 {
8787 /* sort by primary key */
8788 KEY *key_info = &table->key_info[table->s->primary_key];
8789 KEY_PART_INFO *key_part;
8790 for (
8791 key_part = key_info->key_part,
8792 length = 1;
8793 length <= (int) spider_user_defined_key_parts(key_info);
8794 key_part++,
8795 length++
8796 ) {
8797 field = key_part->field;
8798 key_name_length =
8799 mysql_share->column_name_str[field->field_index].length();
8800 if (length == 1)
8801 {
8802 if (str->reserve(SPIDER_SQL_ORDER_LEN))
8803 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8804 str->q_append(SPIDER_SQL_ORDER_STR, SPIDER_SQL_ORDER_LEN);
8805 }
8806 if (str->reserve(alias_length + key_name_length +
8807 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
8808 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8809 str->q_append(alias, alias_length);
8810 mysql_share->append_column_name(str, field->field_index);
8811 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
8812 }
8813 if (length > 1)
8814 {
8815 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
8816 }
8817 } else {
8818 /* sort by all columns */
8819 Field **fieldp;
8820 for (
8821 fieldp = table->field, length = 1;
8822 *fieldp;
8823 fieldp++, length++
8824 ) {
8825 key_name_length =
8826 mysql_share->column_name_str[(*fieldp)->field_index].length();
8827 if (length == 1)
8828 {
8829 if (str->reserve(SPIDER_SQL_ORDER_LEN))
8830 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8831 str->q_append(SPIDER_SQL_ORDER_STR, SPIDER_SQL_ORDER_LEN);
8832 }
8833 if (str->reserve(alias_length + key_name_length +
8834 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
8835 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8836 str->q_append(alias, alias_length);
8837 mysql_share->append_column_name(str, (*fieldp)->field_index);
8838 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
8839 }
8840 if (length > 1)
8841 {
8842 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
8843 }
8844 }
8845 limit_pos = str->length();
8846 DBUG_RETURN(0);
8847}
8848
8849int spider_mysql_handler::append_key_order_for_direct_order_limit_with_alias_part(
8850 const char *alias,
8851 uint alias_length,
8852 ulong sql_type
8853) {
8854 int error_num;
8855 spider_string *str;
8856 DBUG_ENTER("spider_mysql_handler::append_key_order_for_direct_order_limit_with_alias_part");
8857 DBUG_PRINT("info",("spider this=%p", this));
8858 switch (sql_type)
8859 {
8860 case SPIDER_SQL_TYPE_SELECT_SQL:
8861 case SPIDER_SQL_TYPE_TMP_SQL:
8862 str = &sql;
8863 break;
8864 case SPIDER_SQL_TYPE_INSERT_SQL:
8865 case SPIDER_SQL_TYPE_UPDATE_SQL:
8866 case SPIDER_SQL_TYPE_DELETE_SQL:
8867 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
8868 str = &update_sql;
8869 break;
8870 case SPIDER_SQL_TYPE_HANDLER:
8871 str = &ha_sql;
8872 break;
8873 default:
8874 DBUG_RETURN(0);
8875 }
8876 error_num = append_key_order_for_direct_order_limit_with_alias(
8877 str, alias, alias_length);
8878 DBUG_RETURN(error_num);
8879}
8880
8881int spider_mysql_handler::append_key_order_for_direct_order_limit_with_alias(
8882 spider_string *str,
8883 const char *alias,
8884 uint alias_length
8885) {
8886 int error_num;
8887 ORDER *order;
8888 st_select_lex *select_lex;
8889 longlong select_limit;
8890 longlong offset_limit;
8891 DBUG_ENTER("spider_mysql_handler::append_key_order_for_direct_order_limit_with_alias");
8892 DBUG_PRINT("info",("spider this=%p", this));
8893#ifdef HANDLER_HAS_DIRECT_AGGREGATE
8894 if (spider->result_list.direct_aggregate)
8895 {
8896 if ((error_num = append_group_by(str, alias, alias_length)))
8897 DBUG_RETURN(error_num);
8898 }
8899#endif
8900 spider_get_select_limit(spider, &select_lex, &select_limit,
8901 &offset_limit);
8902 if (select_lex->order_list.first)
8903 {
8904 if (str->reserve(SPIDER_SQL_ORDER_LEN))
8905 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8906 str->q_append(SPIDER_SQL_ORDER_STR, SPIDER_SQL_ORDER_LEN);
8907 for (order = (ORDER *) select_lex->order_list.first; order;
8908 order = order->next)
8909 {
8910 if ((error_num =
8911 spider_db_print_item_type((*order->item), spider, str, alias,
8912 alias_length, spider_dbton_mysql.dbton_id, FALSE, NULL)))
8913 {
8914 DBUG_PRINT("info",("spider error=%d", error_num));
8915 DBUG_RETURN(error_num);
8916 }
8917 if (SPIDER_order_direction_is_asc(order))
8918 {
8919 if (str->reserve(SPIDER_SQL_COMMA_LEN))
8920 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8921 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
8922 } else {
8923 if (str->reserve(SPIDER_SQL_DESC_LEN + SPIDER_SQL_COMMA_LEN))
8924 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8925 str->q_append(SPIDER_SQL_DESC_STR, SPIDER_SQL_DESC_LEN);
8926 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
8927 }
8928 }
8929 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
8930 }
8931 limit_pos = str->length();
8932 DBUG_RETURN(0);
8933}
8934
8935int spider_mysql_handler::append_key_order_with_alias_part(
8936 const char *alias,
8937 uint alias_length,
8938 ulong sql_type
8939) {
8940 int error_num;
8941 spider_string *str;
8942 DBUG_ENTER("spider_mysql_handler::append_key_order_with_alias_part");
8943 DBUG_PRINT("info",("spider this=%p", this));
8944 switch (sql_type)
8945 {
8946 case SPIDER_SQL_TYPE_SELECT_SQL:
8947 case SPIDER_SQL_TYPE_TMP_SQL:
8948 str = &sql;
8949 break;
8950 case SPIDER_SQL_TYPE_INSERT_SQL:
8951 case SPIDER_SQL_TYPE_UPDATE_SQL:
8952 case SPIDER_SQL_TYPE_DELETE_SQL:
8953 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
8954 str = &update_sql;
8955 break;
8956 case SPIDER_SQL_TYPE_HANDLER:
8957 str = &ha_sql;
8958 error_num = append_key_order_for_handler(str, alias, alias_length);
8959 DBUG_RETURN(error_num);
8960 default:
8961 DBUG_RETURN(0);
8962 }
8963 error_num = append_key_order_with_alias(str, alias, alias_length);
8964 DBUG_RETURN(error_num);
8965}
8966
8967int spider_mysql_handler::append_key_order_for_handler(
8968 spider_string *str,
8969 const char *alias,
8970 uint alias_length
8971) {
8972 DBUG_ENTER("spider_mysql_handler::append_key_order_for_handler");
8973 DBUG_PRINT("info",("spider this=%p", this));
8974 DBUG_PRINT("info",("spider ha_next_pos=%d", ha_next_pos));
8975 DBUG_PRINT("info",("spider ha_where_pos=%d", ha_where_pos));
8976 str->q_append(alias, alias_length);
8977 memset((char *) str->ptr() + str->length(), ' ',
8978 ha_where_pos - ha_next_pos - alias_length);
8979 DBUG_RETURN(0);
8980}
8981
8982int spider_mysql_handler::append_key_order_with_alias(
8983 spider_string *str,
8984 const char *alias,
8985 uint alias_length
8986) {
8987 SPIDER_RESULT_LIST *result_list = &spider->result_list;
8988 KEY *key_info = result_list->key_info;
8989 int length;
8990 KEY_PART_INFO *key_part;
8991 Field *field;
8992 uint key_name_length;
8993 DBUG_ENTER("spider_mysql_handler::append_key_order_with_alias");
8994 DBUG_PRINT("info",("spider this=%p", this));
8995#ifdef HANDLER_HAS_DIRECT_AGGREGATE
8996 if (spider->result_list.direct_aggregate)
8997 {
8998 int error_num;
8999 if ((error_num = append_group_by(str, alias, alias_length)))
9000 DBUG_RETURN(error_num);
9001 }
9002#endif
9003 if (result_list->sorted == TRUE)
9004 {
9005 if (result_list->desc_flg == TRUE)
9006 {
9007 for (
9008 key_part = key_info->key_part + result_list->key_order,
9009 length = 1;
9010 length + result_list->key_order <
9011 (int) spider_user_defined_key_parts(key_info) &&
9012 length < result_list->max_order;
9013 key_part++,
9014 length++
9015 ) {
9016 field = key_part->field;
9017 key_name_length =
9018 mysql_share->column_name_str[field->field_index].length();
9019 if (length == 1)
9020 {
9021 if (str->reserve(SPIDER_SQL_ORDER_LEN))
9022 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9023 str->q_append(SPIDER_SQL_ORDER_STR, SPIDER_SQL_ORDER_LEN);
9024 }
9025 if (key_part->key_part_flag & HA_REVERSE_SORT)
9026 {
9027 if (str->reserve(alias_length + key_name_length +
9028 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
9029 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9030 str->q_append(alias, alias_length);
9031 mysql_share->append_column_name(str, field->field_index);
9032 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9033 } else {
9034 if (str->reserve(alias_length + key_name_length +
9035 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
9036 SPIDER_SQL_DESC_LEN + SPIDER_SQL_COMMA_LEN))
9037 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9038 str->q_append(alias, alias_length);
9039 mysql_share->append_column_name(str, field->field_index);
9040 str->q_append(SPIDER_SQL_DESC_STR, SPIDER_SQL_DESC_LEN);
9041 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9042 }
9043 }
9044 if (
9045 length + result_list->key_order <=
9046 (int) spider_user_defined_key_parts(key_info) &&
9047 length <= result_list->max_order
9048 ) {
9049 field = key_part->field;
9050 key_name_length =
9051 mysql_share->column_name_str[field->field_index].length();
9052 if (length == 1)
9053 {
9054 if (str->reserve(SPIDER_SQL_ORDER_LEN))
9055 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9056 str->q_append(SPIDER_SQL_ORDER_STR, SPIDER_SQL_ORDER_LEN);
9057 }
9058 if (key_part->key_part_flag & HA_REVERSE_SORT)
9059 {
9060 if (str->reserve(alias_length + key_name_length +
9061 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2))
9062 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9063 str->q_append(alias, alias_length);
9064 mysql_share->append_column_name(str, field->field_index);
9065 } else {
9066 if (str->reserve(alias_length + key_name_length +
9067 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_DESC_LEN))
9068 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9069 str->q_append(alias, alias_length);
9070 mysql_share->append_column_name(str, field->field_index);
9071 str->q_append(SPIDER_SQL_DESC_STR, SPIDER_SQL_DESC_LEN);
9072 }
9073 }
9074 } else {
9075 for (
9076 key_part = key_info->key_part + result_list->key_order,
9077 length = 1;
9078 length + result_list->key_order <
9079 (int) spider_user_defined_key_parts(key_info) &&
9080 length < result_list->max_order;
9081 key_part++,
9082 length++
9083 ) {
9084 field = key_part->field;
9085 key_name_length =
9086 mysql_share->column_name_str[field->field_index].length();
9087 if (length == 1)
9088 {
9089 if (str->reserve(SPIDER_SQL_ORDER_LEN))
9090 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9091 str->q_append(SPIDER_SQL_ORDER_STR, SPIDER_SQL_ORDER_LEN);
9092 }
9093 if (key_part->key_part_flag & HA_REVERSE_SORT)
9094 {
9095 if (str->reserve(alias_length + key_name_length +
9096 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
9097 SPIDER_SQL_DESC_LEN + SPIDER_SQL_COMMA_LEN))
9098 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9099 str->q_append(alias, alias_length);
9100 mysql_share->append_column_name(str, field->field_index);
9101 str->q_append(SPIDER_SQL_DESC_STR, SPIDER_SQL_DESC_LEN);
9102 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9103 } else {
9104 if (str->reserve(alias_length + key_name_length +
9105 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
9106 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9107 str->q_append(alias, alias_length);
9108 mysql_share->append_column_name(str, field->field_index);
9109 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9110 }
9111 }
9112 if (
9113 length + result_list->key_order <=
9114 (int) spider_user_defined_key_parts(key_info) &&
9115 length <= result_list->max_order
9116 ) {
9117 field = key_part->field;
9118 key_name_length =
9119 mysql_share->column_name_str[field->field_index].length();
9120 if (length == 1)
9121 {
9122 if (str->reserve(SPIDER_SQL_ORDER_LEN))
9123 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9124 str->q_append(SPIDER_SQL_ORDER_STR, SPIDER_SQL_ORDER_LEN);
9125 }
9126 if (key_part->key_part_flag & HA_REVERSE_SORT)
9127 {
9128 if (str->reserve(alias_length + key_name_length +
9129 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_DESC_LEN))
9130 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9131 str->q_append(alias, alias_length);
9132 mysql_share->append_column_name(str, field->field_index);
9133 str->q_append(SPIDER_SQL_DESC_STR, SPIDER_SQL_DESC_LEN);
9134 } else {
9135 if (str->reserve(alias_length + key_name_length +
9136 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2))
9137 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9138 str->q_append(alias, alias_length);
9139 mysql_share->append_column_name(str, field->field_index);
9140 }
9141 }
9142 }
9143 }
9144 limit_pos = str->length();
9145 DBUG_RETURN(0);
9146}
9147
9148int spider_mysql_handler::append_limit_part(
9149 longlong offset,
9150 longlong limit,
9151 ulong sql_type
9152) {
9153 int error_num;
9154 spider_string *str;
9155 DBUG_ENTER("spider_mysql_handler::append_limit_part");
9156 DBUG_PRINT("info",("spider this=%p", this));
9157 switch (sql_type)
9158 {
9159 case SPIDER_SQL_TYPE_SELECT_SQL:
9160 str = &sql;
9161 limit_pos = str->length();
9162 break;
9163 case SPIDER_SQL_TYPE_TMP_SQL:
9164 str = &tmp_sql;
9165 limit_pos = str->length();
9166 break;
9167 case SPIDER_SQL_TYPE_INSERT_SQL:
9168 case SPIDER_SQL_TYPE_UPDATE_SQL:
9169 case SPIDER_SQL_TYPE_DELETE_SQL:
9170 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
9171 str = &update_sql;
9172 limit_pos = str->length();
9173 break;
9174 case SPIDER_SQL_TYPE_HANDLER:
9175 str = &ha_sql;
9176 ha_limit_pos = str->length();
9177 break;
9178 default:
9179 DBUG_RETURN(0);
9180 }
9181 error_num = append_limit(str, offset, limit);
9182 DBUG_RETURN(error_num);
9183}
9184
9185int spider_mysql_handler::reappend_limit_part(
9186 longlong offset,
9187 longlong limit,
9188 ulong sql_type
9189) {
9190 int error_num;
9191 spider_string *str;
9192 DBUG_ENTER("spider_mysql_handler::reappend_limit_part");
9193 DBUG_PRINT("info",("spider this=%p", this));
9194 switch (sql_type)
9195 {
9196 case SPIDER_SQL_TYPE_SELECT_SQL:
9197 str = &sql;
9198 str->length(limit_pos);
9199 break;
9200 case SPIDER_SQL_TYPE_TMP_SQL:
9201 str = &tmp_sql;
9202 str->length(limit_pos);
9203 break;
9204 case SPIDER_SQL_TYPE_INSERT_SQL:
9205 case SPIDER_SQL_TYPE_UPDATE_SQL:
9206 case SPIDER_SQL_TYPE_DELETE_SQL:
9207 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
9208 str = &update_sql;
9209 str->length(limit_pos);
9210 break;
9211 case SPIDER_SQL_TYPE_HANDLER:
9212 str = &ha_sql;
9213 str->length(ha_limit_pos);
9214 break;
9215 default:
9216 DBUG_RETURN(0);
9217 }
9218 error_num = append_limit(str, offset, limit);
9219 DBUG_RETURN(error_num);
9220}
9221
9222int spider_mysql_handler::append_limit(
9223 spider_string *str,
9224 longlong offset,
9225 longlong limit
9226) {
9227 char buf[SPIDER_LONGLONG_LEN + 1];
9228 uint32 length;
9229 DBUG_ENTER("spider_mysql_handler::append_limit");
9230 DBUG_PRINT("info",("spider this=%p", this));
9231 DBUG_PRINT("info", ("spider offset=%lld", offset));
9232 DBUG_PRINT("info", ("spider limit=%lld", limit));
9233 if (offset || limit < 9223372036854775807LL)
9234 {
9235 if (str->reserve(SPIDER_SQL_LIMIT_LEN + SPIDER_SQL_COMMA_LEN +
9236 ((SPIDER_LONGLONG_LEN) * 2)))
9237 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9238 str->q_append(SPIDER_SQL_LIMIT_STR, SPIDER_SQL_LIMIT_LEN);
9239 if (offset)
9240 {
9241 length = (uint32) (my_charset_bin.cset->longlong10_to_str)(
9242 &my_charset_bin, buf, SPIDER_LONGLONG_LEN + 1, -10, offset);
9243 str->q_append(buf, length);
9244 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9245 }
9246 length = (uint32) (my_charset_bin.cset->longlong10_to_str)(
9247 &my_charset_bin, buf, SPIDER_LONGLONG_LEN + 1, -10, limit);
9248 str->q_append(buf, length);
9249 }
9250 DBUG_RETURN(0);
9251}
9252
9253int spider_mysql_handler::append_select_lock_part(
9254 ulong sql_type
9255) {
9256 int error_num;
9257 spider_string *str;
9258 DBUG_ENTER("spider_mysql_handler::append_select_lock_part");
9259 DBUG_PRINT("info",("spider this=%p", this));
9260 switch (sql_type)
9261 {
9262 case SPIDER_SQL_TYPE_SELECT_SQL:
9263 str = &sql;
9264 break;
9265 default:
9266 DBUG_RETURN(0);
9267 }
9268 error_num = append_select_lock(str);
9269 DBUG_RETURN(error_num);
9270}
9271
9272int spider_mysql_handler::append_select_lock(
9273 spider_string *str
9274) {
9275 int lock_mode = spider_conn_lock_mode(spider);
9276 DBUG_ENTER("spider_mysql_handler::append_select_lock");
9277 DBUG_PRINT("info",("spider this=%p", this));
9278 if (lock_mode == SPIDER_LOCK_MODE_EXCLUSIVE)
9279 {
9280 if (str->reserve(SPIDER_SQL_FOR_UPDATE_LEN))
9281 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9282 str->q_append(SPIDER_SQL_FOR_UPDATE_STR, SPIDER_SQL_FOR_UPDATE_LEN);
9283 } else if (lock_mode == SPIDER_LOCK_MODE_SHARED)
9284 {
9285 if (str->reserve(SPIDER_SQL_SHARED_LOCK_LEN))
9286 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9287 str->q_append(SPIDER_SQL_SHARED_LOCK_STR, SPIDER_SQL_SHARED_LOCK_LEN);
9288 }
9289 DBUG_RETURN(0);
9290}
9291
9292int spider_mysql_handler::append_union_all_start_part(
9293 ulong sql_type
9294) {
9295 int error_num;
9296 spider_string *str;
9297 DBUG_ENTER("spider_mysql_handler::append_union_all_start_part");
9298 DBUG_PRINT("info",("spider this=%p", this));
9299 switch (sql_type)
9300 {
9301 case SPIDER_SQL_TYPE_SELECT_SQL:
9302 str = &sql;
9303 break;
9304 default:
9305 DBUG_RETURN(0);
9306 }
9307 error_num = append_union_all_start(str);
9308 DBUG_RETURN(error_num);
9309}
9310
9311int spider_mysql_handler::append_union_all_start(
9312 spider_string *str
9313) {
9314 DBUG_ENTER("spider_mysql_handler::append_union_all_start");
9315 DBUG_PRINT("info",("spider this=%p", this));
9316 if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
9317 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9318 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
9319 DBUG_RETURN(0);
9320}
9321
9322int spider_mysql_handler::append_union_all_part(
9323 ulong sql_type
9324) {
9325 int error_num;
9326 spider_string *str;
9327 DBUG_ENTER("spider_mysql_handler::append_union_all_part");
9328 DBUG_PRINT("info",("spider this=%p", this));
9329 switch (sql_type)
9330 {
9331 case SPIDER_SQL_TYPE_SELECT_SQL:
9332 str = &sql;
9333 break;
9334 default:
9335 DBUG_RETURN(0);
9336 }
9337 error_num = append_union_all(str);
9338 DBUG_RETURN(error_num);
9339}
9340
9341int spider_mysql_handler::append_union_all(
9342 spider_string *str
9343) {
9344 DBUG_ENTER("spider_mysql_handler::append_union_all");
9345 DBUG_PRINT("info",("spider this=%p", this));
9346 if (str->reserve(SPIDER_SQL_UNION_ALL_LEN))
9347 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9348 str->q_append(SPIDER_SQL_UNION_ALL_STR, SPIDER_SQL_UNION_ALL_LEN);
9349 DBUG_RETURN(0);
9350}
9351
9352int spider_mysql_handler::append_union_all_end_part(
9353 ulong sql_type
9354) {
9355 int error_num;
9356 spider_string *str;
9357 DBUG_ENTER("spider_mysql_handler::append_union_all_end_part");
9358 DBUG_PRINT("info",("spider this=%p", this));
9359 switch (sql_type)
9360 {
9361 case SPIDER_SQL_TYPE_SELECT_SQL:
9362 str = &sql;
9363 break;
9364 default:
9365 DBUG_RETURN(0);
9366 }
9367 error_num = append_union_all_end(str);
9368 DBUG_RETURN(error_num);
9369}
9370
9371int spider_mysql_handler::append_union_all_end(
9372 spider_string *str
9373) {
9374 DBUG_ENTER("spider_mysql_handler::append_union_all_end");
9375 DBUG_PRINT("info",("spider this=%p", this));
9376 str->length(str->length() -
9377 SPIDER_SQL_UNION_ALL_LEN + SPIDER_SQL_CLOSE_PAREN_LEN);
9378 DBUG_RETURN(0);
9379}
9380
9381int spider_mysql_handler::append_multi_range_cnt_part(
9382 ulong sql_type,
9383 uint multi_range_cnt,
9384 bool with_comma
9385) {
9386 int error_num;
9387 spider_string *str;
9388 DBUG_ENTER("spider_mysql_handler::append_multi_range_cnt_part");
9389 DBUG_PRINT("info",("spider this=%p", this));
9390 switch (sql_type)
9391 {
9392 case SPIDER_SQL_TYPE_SELECT_SQL:
9393 str = &sql;
9394 break;
9395 case SPIDER_SQL_TYPE_TMP_SQL:
9396 str = &tmp_sql;
9397 break;
9398 default:
9399 DBUG_RETURN(0);
9400 }
9401 error_num = append_multi_range_cnt(str, multi_range_cnt, with_comma);
9402 DBUG_RETURN(error_num);
9403}
9404
9405int spider_mysql_handler::append_multi_range_cnt(
9406 spider_string *str,
9407 uint multi_range_cnt,
9408 bool with_comma
9409) {
9410 int range_cnt_length;
9411 char range_cnt_str[SPIDER_SQL_INT_LEN];
9412 DBUG_ENTER("spider_mysql_handler::append_multi_range_cnt");
9413 DBUG_PRINT("info",("spider this=%p", this));
9414 range_cnt_length = my_sprintf(range_cnt_str, (range_cnt_str, "%u",
9415 multi_range_cnt));
9416 if (with_comma)
9417 {
9418 if (str->reserve(range_cnt_length + SPIDER_SQL_COMMA_LEN))
9419 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9420 str->q_append(range_cnt_str, range_cnt_length);
9421 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9422 } else {
9423 if (str->reserve(range_cnt_length))
9424 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9425 str->q_append(range_cnt_str, range_cnt_length);
9426 }
9427 DBUG_RETURN(0);
9428}
9429
9430int spider_mysql_handler::append_multi_range_cnt_with_name_part(
9431 ulong sql_type,
9432 uint multi_range_cnt
9433) {
9434 int error_num;
9435 spider_string *str;
9436 DBUG_ENTER("spider_mysql_handler::append_multi_range_cnt_with_name_part");
9437 DBUG_PRINT("info",("spider this=%p", this));
9438 switch (sql_type)
9439 {
9440 case SPIDER_SQL_TYPE_SELECT_SQL:
9441 str = &sql;
9442 break;
9443 case SPIDER_SQL_TYPE_TMP_SQL:
9444 str = &tmp_sql;
9445 break;
9446 default:
9447 DBUG_RETURN(0);
9448 }
9449 error_num = append_multi_range_cnt_with_name(str, multi_range_cnt);
9450 DBUG_RETURN(error_num);
9451}
9452
9453int spider_mysql_handler::append_multi_range_cnt_with_name(
9454 spider_string *str,
9455 uint multi_range_cnt
9456) {
9457 int range_cnt_length;
9458 char range_cnt_str[SPIDER_SQL_INT_LEN];
9459 DBUG_ENTER("spider_mysql_handler::append_multi_range_cnt_with_name");
9460 DBUG_PRINT("info",("spider this=%p", this));
9461 range_cnt_length = my_sprintf(range_cnt_str, (range_cnt_str, "%u",
9462 multi_range_cnt));
9463 if (str->reserve(range_cnt_length + SPIDER_SQL_SPACE_LEN +
9464 SPIDER_SQL_ID_LEN + SPIDER_SQL_COMMA_LEN))
9465 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9466 str->q_append(range_cnt_str, range_cnt_length);
9467 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
9468 str->q_append(SPIDER_SQL_ID_STR, SPIDER_SQL_ID_LEN);
9469 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9470 DBUG_RETURN(0);
9471}
9472
9473int spider_mysql_handler::append_open_handler_part(
9474 ulong sql_type,
9475 uint handler_id,
9476 SPIDER_CONN *conn,
9477 int link_idx
9478) {
9479 int error_num;
9480 spider_string *str;
9481 DBUG_ENTER("spider_mysql_handler::append_open_handler_part");
9482 DBUG_PRINT("info",("spider this=%p", this));
9483 switch (sql_type)
9484 {
9485 case SPIDER_SQL_TYPE_HANDLER:
9486 str = &ha_sql;
9487 break;
9488 default:
9489 DBUG_RETURN(0);
9490 }
9491 error_num = append_open_handler(str, handler_id, conn, link_idx);
9492 exec_ha_sql = str;
9493 DBUG_RETURN(error_num);
9494}
9495
9496int spider_mysql_handler::append_open_handler(
9497 spider_string *str,
9498 uint handler_id,
9499 SPIDER_CONN *conn,
9500 int link_idx
9501) {
9502 int error_num;
9503 DBUG_ENTER("spider_mysql_handler::append_open_handler");
9504 DBUG_PRINT("info",("spider this=%p", this));
9505 DBUG_PRINT("info",("spider link_idx=%d", link_idx));
9506 DBUG_PRINT("info",("spider m_handler_cid=%s",
9507 spider->m_handler_cid[link_idx]));
9508 if (str->reserve(SPIDER_SQL_HANDLER_LEN))
9509 {
9510 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9511 }
9512 str->q_append(SPIDER_SQL_HANDLER_STR, SPIDER_SQL_HANDLER_LEN);
9513 if ((error_num = mysql_share->append_table_name(str,
9514 spider->conn_link_idx[link_idx])))
9515 DBUG_RETURN(error_num);
9516 if (str->reserve(SPIDER_SQL_OPEN_LEN + SPIDER_SQL_AS_LEN +
9517 SPIDER_SQL_HANDLER_CID_LEN))
9518 {
9519 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9520 }
9521 str->q_append(SPIDER_SQL_OPEN_STR, SPIDER_SQL_OPEN_LEN);
9522 str->q_append(SPIDER_SQL_AS_STR, SPIDER_SQL_AS_LEN);
9523 str->q_append(spider->m_handler_cid[link_idx], SPIDER_SQL_HANDLER_CID_LEN);
9524 DBUG_RETURN(0);
9525}
9526
9527int spider_mysql_handler::append_close_handler_part(
9528 ulong sql_type,
9529 int link_idx
9530) {
9531 int error_num;
9532 spider_string *str;
9533 DBUG_ENTER("spider_mysql_handler::append_close_handler_part");
9534 DBUG_PRINT("info",("spider this=%p", this));
9535 switch (sql_type)
9536 {
9537 case SPIDER_SQL_TYPE_HANDLER:
9538 str = &ha_sql;
9539 break;
9540 default:
9541 DBUG_RETURN(0);
9542 }
9543 error_num = append_close_handler(str, link_idx);
9544 exec_ha_sql = str;
9545 DBUG_RETURN(error_num);
9546}
9547
9548int spider_mysql_handler::append_close_handler(
9549 spider_string *str,
9550 int link_idx
9551) {
9552 DBUG_ENTER("spider_mysql_handler::append_close_handler");
9553 DBUG_PRINT("info",("spider this=%p", this));
9554 if (str->reserve(SPIDER_SQL_HANDLER_LEN + SPIDER_SQL_CLOSE_LEN +
9555 SPIDER_SQL_HANDLER_CID_LEN))
9556 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9557 str->q_append(SPIDER_SQL_HANDLER_STR, SPIDER_SQL_HANDLER_LEN);
9558 str->q_append(spider->m_handler_cid[link_idx],
9559 SPIDER_SQL_HANDLER_CID_LEN);
9560 str->q_append(SPIDER_SQL_CLOSE_STR, SPIDER_SQL_CLOSE_LEN);
9561 DBUG_RETURN(0);
9562}
9563
9564int spider_mysql_handler::append_insert_terminator_part(
9565 ulong sql_type
9566) {
9567 int error_num;
9568 spider_string *str;
9569 DBUG_ENTER("spider_mysql_handler::append_insert_terminator_part");
9570 DBUG_PRINT("info",("spider this=%p", this));
9571 switch (sql_type)
9572 {
9573 case SPIDER_SQL_TYPE_INSERT_SQL:
9574 str = &insert_sql;
9575 break;
9576 default:
9577 DBUG_RETURN(0);
9578 }
9579 error_num = append_insert_terminator(str);
9580 DBUG_RETURN(error_num);
9581}
9582
9583int spider_mysql_handler::append_insert_terminator(
9584 spider_string *str
9585) {
9586 DBUG_ENTER("spider_mysql_handler::append_insert_terminator");
9587 DBUG_PRINT("info",("spider this=%p", this));
9588 DBUG_PRINT("info",("spider dup_update_sql.length=%u", dup_update_sql.length()));
9589 if (
9590 spider->result_list.insert_dup_update_pushdown &&
9591 dup_update_sql.length()
9592 ) {
9593 DBUG_PRINT("info",("spider add duplicate key update"));
9594 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
9595 if (str->reserve(SPIDER_SQL_DUPLICATE_KEY_UPDATE_LEN +
9596 dup_update_sql.length()))
9597 {
9598 str->length(0);
9599 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9600 }
9601 str->q_append(SPIDER_SQL_DUPLICATE_KEY_UPDATE_STR,
9602 SPIDER_SQL_DUPLICATE_KEY_UPDATE_LEN);
9603 if (str->append(dup_update_sql))
9604 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9605 } else {
9606 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
9607 }
9608 DBUG_RETURN(0);
9609}
9610
9611int spider_mysql_handler::append_insert_values_part(
9612 ulong sql_type
9613) {
9614 int error_num;
9615 spider_string *str;
9616 DBUG_ENTER("spider_mysql_handler::append_insert_values_part");
9617 DBUG_PRINT("info",("spider this=%p", this));
9618 switch (sql_type)
9619 {
9620 case SPIDER_SQL_TYPE_INSERT_SQL:
9621 str = &insert_sql;
9622 break;
9623 default:
9624 DBUG_RETURN(0);
9625 }
9626 error_num = append_insert_values(str);
9627 DBUG_RETURN(error_num);
9628}
9629
9630int spider_mysql_handler::append_insert_values(
9631 spider_string *str
9632) {
9633 SPIDER_SHARE *share = spider->share;
9634 TABLE *table = spider->get_table();
9635 Field **field;
9636 bool add_value = FALSE;
9637 DBUG_ENTER("spider_mysql_handler::append_insert_values");
9638 DBUG_PRINT("info",("spider this=%p", this));
9639 if (str->reserve(SPIDER_SQL_OPEN_PAREN_LEN))
9640 {
9641 str->length(0);
9642 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9643 }
9644 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
9645 for (field = table->field; *field; field++)
9646 {
9647 DBUG_PRINT("info",("spider field_index=%u", (*field)->field_index));
9648 if (
9649 bitmap_is_set(table->write_set, (*field)->field_index) ||
9650 bitmap_is_set(table->read_set, (*field)->field_index)
9651 ) {
9652#ifndef DBUG_OFF
9653 my_bitmap_map *tmp_map =
9654 dbug_tmp_use_all_columns(table, table->read_set);
9655#endif
9656 add_value = TRUE;
9657 DBUG_PRINT("info",("spider is_null()=%s",
9658 (*field)->is_null() ? "TRUE" : "FALSE"));
9659 DBUG_PRINT("info",("spider table->next_number_field=%p",
9660 table->next_number_field));
9661 DBUG_PRINT("info",("spider *field=%p", *field));
9662 DBUG_PRINT("info",("spider force_auto_increment=%s",
9663 (table->next_number_field && spider->force_auto_increment) ?
9664 "TRUE" : "FALSE"));
9665 if (
9666 (*field)->is_null() ||
9667 (
9668 table->next_number_field == *field &&
9669 !table->auto_increment_field_not_null &&
9670 !spider->force_auto_increment
9671 )
9672 ) {
9673 if (str->reserve(SPIDER_SQL_NULL_LEN + SPIDER_SQL_COMMA_LEN))
9674 {
9675#ifndef DBUG_OFF
9676 dbug_tmp_restore_column_map(table->read_set, tmp_map);
9677#endif
9678 str->length(0);
9679 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9680 }
9681 str->q_append(SPIDER_SQL_NULL_STR, SPIDER_SQL_NULL_LEN);
9682 } else {
9683 if (
9684 spider_db_mysql_utility.
9685 append_column_value(spider, str, *field, NULL,
9686 share->access_charset) ||
9687 str->reserve(SPIDER_SQL_COMMA_LEN)
9688 ) {
9689#ifndef DBUG_OFF
9690 dbug_tmp_restore_column_map(table->read_set, tmp_map);
9691#endif
9692 str->length(0);
9693 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9694 }
9695 }
9696 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9697#ifndef DBUG_OFF
9698 dbug_tmp_restore_column_map(table->read_set, tmp_map);
9699#endif
9700 }
9701 }
9702 if (add_value)
9703 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
9704 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN + SPIDER_SQL_COMMA_LEN))
9705 {
9706 str->length(0);
9707 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9708 }
9709 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
9710 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9711 DBUG_RETURN(0);
9712}
9713
9714int spider_mysql_handler::append_into_part(
9715 ulong sql_type
9716) {
9717 int error_num;
9718 spider_string *str;
9719 DBUG_ENTER("spider_mysql_handler::append_into_part");
9720 DBUG_PRINT("info",("spider this=%p", this));
9721 switch (sql_type)
9722 {
9723 case SPIDER_SQL_TYPE_INSERT_SQL:
9724 str = &insert_sql;
9725 break;
9726 default:
9727 DBUG_RETURN(0);
9728 }
9729 error_num = append_into(str);
9730 DBUG_RETURN(error_num);
9731}
9732
9733int spider_mysql_handler::append_into(
9734 spider_string *str
9735) {
9736 const TABLE *table = spider->get_table();
9737 Field **field;
9738 uint field_name_length = 0;
9739 DBUG_ENTER("spider_mysql_handler::append_into");
9740 DBUG_PRINT("info",("spider this=%p", this));
9741 if (str->reserve(SPIDER_SQL_INTO_LEN + mysql_share->db_nm_max_length +
9742 SPIDER_SQL_DOT_LEN + mysql_share->table_nm_max_length +
9743 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4 + SPIDER_SQL_OPEN_PAREN_LEN))
9744 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9745 str->q_append(SPIDER_SQL_INTO_STR, SPIDER_SQL_INTO_LEN);
9746 insert_table_name_pos = str->length();
9747 append_table_name_with_adjusting(str, first_link_idx,
9748 SPIDER_SQL_TYPE_INSERT_SQL);
9749 str->q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
9750 for (field = table->field; *field; field++)
9751 {
9752 if (
9753 bitmap_is_set(table->write_set, (*field)->field_index) ||
9754 bitmap_is_set(table->read_set, (*field)->field_index)
9755 ) {
9756 field_name_length =
9757 mysql_share->column_name_str[(*field)->field_index].length();
9758 if (str->reserve(field_name_length +
9759 /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 + SPIDER_SQL_COMMA_LEN))
9760 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9761 mysql_share->append_column_name(str, (*field)->field_index);
9762 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
9763 }
9764 }
9765 if (field_name_length)
9766 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
9767 if (str->reserve(SPIDER_SQL_CLOSE_PAREN_LEN + SPIDER_SQL_VALUES_LEN))
9768 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9769 str->q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
9770 str->q_append(SPIDER_SQL_VALUES_STR, SPIDER_SQL_VALUES_LEN);
9771 insert_pos = str->length();
9772 DBUG_RETURN(0);
9773}
9774
9775void spider_mysql_handler::set_insert_to_pos(
9776 ulong sql_type
9777) {
9778 DBUG_ENTER("spider_mysql_handler::set_insert_to_pos");
9779 switch (sql_type)
9780 {
9781 case SPIDER_SQL_TYPE_INSERT_SQL:
9782 insert_sql.length(insert_pos);
9783 break;
9784 default:
9785 DBUG_ASSERT(0);
9786 break;
9787 }
9788 DBUG_VOID_RETURN;
9789}
9790
9791int spider_mysql_handler::append_from_part(
9792 ulong sql_type,
9793 int link_idx
9794) {
9795 int error_num;
9796 spider_string *str;
9797 DBUG_ENTER("spider_mysql_handler::append_from_part");
9798 DBUG_PRINT("info",("spider this=%p", this));
9799 switch (sql_type)
9800 {
9801 case SPIDER_SQL_TYPE_HANDLER:
9802 str = &ha_sql;
9803 break;
9804 case SPIDER_SQL_TYPE_UPDATE_SQL:
9805 case SPIDER_SQL_TYPE_DELETE_SQL:
9806 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
9807 str = &update_sql;
9808 break;
9809 default:
9810 str = &sql;
9811 break;
9812 }
9813 error_num = append_from(str, sql_type, link_idx);
9814 DBUG_RETURN(error_num);
9815}
9816
9817int spider_mysql_handler::append_from(
9818 spider_string *str,
9819 ulong sql_type,
9820 int link_idx
9821) {
9822 DBUG_ENTER("spider_mysql_handler::append_from");
9823 DBUG_PRINT("info",("spider this=%p", this));
9824 DBUG_PRINT("info",("spider link_idx=%d", link_idx));
9825 int error_num = 0;
9826 if (sql_type == SPIDER_SQL_TYPE_HANDLER)
9827 {
9828 ha_table_name_pos = str->length();
9829 DBUG_PRINT("info",("spider ha_table_name_pos=%u", ha_table_name_pos));
9830 ha_sql_handler_id = spider->m_handler_id[link_idx];
9831 DBUG_PRINT("info",("spider ha_sql_handler_id=%u", ha_sql_handler_id));
9832 if (str->reserve(SPIDER_SQL_HANDLER_CID_LEN))
9833 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9834 str->q_append(spider->m_handler_cid[link_idx], SPIDER_SQL_HANDLER_CID_LEN);
9835 DBUG_PRINT("info",("spider m_handler_cid=%s",
9836 spider->m_handler_cid[link_idx]));
9837 } else {
9838 if (str->reserve(SPIDER_SQL_FROM_LEN + mysql_share->db_nm_max_length +
9839 SPIDER_SQL_DOT_LEN + mysql_share->table_nm_max_length +
9840 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4 + SPIDER_SQL_OPEN_PAREN_LEN))
9841 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9842 str->q_append(SPIDER_SQL_FROM_STR, SPIDER_SQL_FROM_LEN);
9843 table_name_pos = str->length();
9844 append_table_name_with_adjusting(str, link_idx, sql_type);
9845 if(spider_param_index_hint_pushdown(spider->trx->thd))
9846 {
9847 if((error_num = append_index_hint(str, link_idx, sql_type)))
9848 {
9849 DBUG_RETURN(error_num);
9850 }
9851 }
9852 }
9853 DBUG_RETURN(0);
9854}
9855
9856int spider_mysql_handler::append_flush_tables_part(
9857 ulong sql_type,
9858 int link_idx,
9859 bool lock
9860) {
9861 int error_num;
9862 spider_string *str;
9863 DBUG_ENTER("spider_mysql_handler::append_flush_tables_part");
9864 DBUG_PRINT("info",("spider this=%p", this));
9865 switch (sql_type)
9866 {
9867 case SPIDER_SQL_TYPE_OTHER_SQL:
9868 str = &spider->result_list.sqls[link_idx];
9869 break;
9870 default:
9871 DBUG_RETURN(0);
9872 }
9873 error_num = append_flush_tables(str, link_idx, lock);
9874 DBUG_RETURN(error_num);
9875}
9876
9877int spider_mysql_handler::append_flush_tables(
9878 spider_string *str,
9879 int link_idx,
9880 bool lock
9881) {
9882 DBUG_ENTER("spider_mysql_handler::append_flush_tables");
9883 DBUG_PRINT("info",("spider this=%p", this));
9884 if (lock)
9885 {
9886 if (str->reserve(SPIDER_SQL_FLUSH_TABLES_LEN +
9887 SPIDER_SQL_WITH_READ_LOCK_LEN))
9888 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9889 str->q_append(SPIDER_SQL_FLUSH_TABLES_STR, SPIDER_SQL_FLUSH_TABLES_LEN);
9890 str->q_append(SPIDER_SQL_WITH_READ_LOCK_STR,
9891 SPIDER_SQL_WITH_READ_LOCK_LEN);
9892 } else {
9893 if (str->reserve(SPIDER_SQL_FLUSH_TABLES_LEN))
9894 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9895 str->q_append(SPIDER_SQL_FLUSH_TABLES_STR, SPIDER_SQL_FLUSH_TABLES_LEN);
9896 }
9897 DBUG_RETURN(0);
9898}
9899
9900int spider_mysql_handler::append_optimize_table_part(
9901 ulong sql_type,
9902 int link_idx
9903) {
9904 int error_num;
9905 spider_string *str;
9906 DBUG_ENTER("spider_mysql_handler::append_optimize_table_part");
9907 DBUG_PRINT("info",("spider this=%p", this));
9908 switch (sql_type)
9909 {
9910 case SPIDER_SQL_TYPE_OTHER_SQL:
9911 str = &spider->result_list.sqls[link_idx];
9912 break;
9913 default:
9914 DBUG_RETURN(0);
9915 }
9916 error_num = append_optimize_table(str, link_idx);
9917 DBUG_RETURN(error_num);
9918}
9919
9920int spider_mysql_handler::append_optimize_table(
9921 spider_string *str,
9922 int link_idx
9923) {
9924 SPIDER_SHARE *share = spider->share;
9925 int conn_link_idx = spider->conn_link_idx[link_idx];
9926 int local_length = spider_param_internal_optimize_local(spider->trx->thd,
9927 share->internal_optimize_local) * SPIDER_SQL_SQL_LOCAL_LEN;
9928 DBUG_ENTER("spider_mysql_handler::append_optimize_table");
9929 DBUG_PRINT("info",("spider this=%p", this));
9930 if (str->reserve(SPIDER_SQL_SQL_OPTIMIZE_LEN + SPIDER_SQL_SQL_TABLE_LEN +
9931 local_length +
9932 mysql_share->db_names_str[conn_link_idx].length() +
9933 SPIDER_SQL_DOT_LEN +
9934 mysql_share->table_names_str[conn_link_idx].length() +
9935 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4))
9936 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9937 str->q_append(SPIDER_SQL_SQL_OPTIMIZE_STR, SPIDER_SQL_SQL_OPTIMIZE_LEN);
9938 if (local_length)
9939 str->q_append(SPIDER_SQL_SQL_LOCAL_STR, SPIDER_SQL_SQL_LOCAL_LEN);
9940 str->q_append(SPIDER_SQL_SQL_TABLE_STR, SPIDER_SQL_SQL_TABLE_LEN);
9941 mysql_share->append_table_name(str, conn_link_idx);
9942 DBUG_RETURN(0);
9943}
9944
9945int spider_mysql_handler::append_analyze_table_part(
9946 ulong sql_type,
9947 int link_idx
9948) {
9949 int error_num;
9950 spider_string *str;
9951 DBUG_ENTER("spider_mysql_handler::append_analyze_table_part");
9952 DBUG_PRINT("info",("spider this=%p", this));
9953 switch (sql_type)
9954 {
9955 case SPIDER_SQL_TYPE_OTHER_SQL:
9956 str = &spider->result_list.sqls[link_idx];
9957 break;
9958 default:
9959 DBUG_RETURN(0);
9960 }
9961 error_num = append_analyze_table(str, link_idx);
9962 DBUG_RETURN(error_num);
9963}
9964
9965int spider_mysql_handler::append_analyze_table(
9966 spider_string *str,
9967 int link_idx
9968) {
9969 SPIDER_SHARE *share = spider->share;
9970 int conn_link_idx = spider->conn_link_idx[link_idx];
9971 int local_length = spider_param_internal_optimize_local(spider->trx->thd,
9972 share->internal_optimize_local) * SPIDER_SQL_SQL_LOCAL_LEN;
9973 DBUG_ENTER("spider_mysql_handler::append_analyze_table");
9974 DBUG_PRINT("info",("spider this=%p", this));
9975 if (str->reserve(SPIDER_SQL_SQL_ANALYZE_LEN + SPIDER_SQL_SQL_TABLE_LEN +
9976 local_length +
9977 mysql_share->db_names_str[conn_link_idx].length() +
9978 SPIDER_SQL_DOT_LEN +
9979 mysql_share->table_names_str[conn_link_idx].length() +
9980 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4))
9981 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9982 str->q_append(SPIDER_SQL_SQL_ANALYZE_STR, SPIDER_SQL_SQL_ANALYZE_LEN);
9983 if (local_length)
9984 str->q_append(SPIDER_SQL_SQL_LOCAL_STR, SPIDER_SQL_SQL_LOCAL_LEN);
9985 str->q_append(SPIDER_SQL_SQL_TABLE_STR, SPIDER_SQL_SQL_TABLE_LEN);
9986 mysql_share->append_table_name(str, conn_link_idx);
9987 DBUG_RETURN(0);
9988}
9989
9990int spider_mysql_handler::append_repair_table_part(
9991 ulong sql_type,
9992 int link_idx,
9993 HA_CHECK_OPT* check_opt
9994) {
9995 int error_num;
9996 spider_string *str;
9997 DBUG_ENTER("spider_mysql_handler::append_repair_table_part");
9998 DBUG_PRINT("info",("spider this=%p", this));
9999 switch (sql_type)
10000 {
10001 case SPIDER_SQL_TYPE_OTHER_SQL:
10002 str = &spider->result_list.sqls[link_idx];
10003 break;
10004 default:
10005 DBUG_RETURN(0);
10006 }
10007 error_num = append_repair_table(str, link_idx, check_opt);
10008 DBUG_RETURN(error_num);
10009}
10010
10011int spider_mysql_handler::append_repair_table(
10012 spider_string *str,
10013 int link_idx,
10014 HA_CHECK_OPT* check_opt
10015) {
10016 SPIDER_SHARE *share = spider->share;
10017 int conn_link_idx = spider->conn_link_idx[link_idx];
10018 int local_length = spider_param_internal_optimize_local(spider->trx->thd,
10019 share->internal_optimize_local) * SPIDER_SQL_SQL_LOCAL_LEN;
10020 DBUG_ENTER("spider_mysql_handler::append_repair_table");
10021 DBUG_PRINT("info",("spider this=%p", this));
10022 if (str->reserve(SPIDER_SQL_SQL_REPAIR_LEN + SPIDER_SQL_SQL_TABLE_LEN +
10023 local_length +
10024 mysql_share->db_names_str[conn_link_idx].length() +
10025 SPIDER_SQL_DOT_LEN +
10026 mysql_share->table_names_str[conn_link_idx].length() +
10027 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4))
10028 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10029 str->q_append(SPIDER_SQL_SQL_REPAIR_STR, SPIDER_SQL_SQL_REPAIR_LEN);
10030 if (local_length)
10031 str->q_append(SPIDER_SQL_SQL_LOCAL_STR, SPIDER_SQL_SQL_LOCAL_LEN);
10032 str->q_append(SPIDER_SQL_SQL_TABLE_STR, SPIDER_SQL_SQL_TABLE_LEN);
10033 mysql_share->append_table_name(str, conn_link_idx);
10034 if (check_opt->flags & T_QUICK)
10035 {
10036 if (str->reserve(SPIDER_SQL_SQL_QUICK_LEN))
10037 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10038 str->q_append(SPIDER_SQL_SQL_QUICK_STR, SPIDER_SQL_SQL_QUICK_LEN);
10039 }
10040 if (check_opt->flags & T_EXTEND)
10041 {
10042 if (str->reserve(SPIDER_SQL_SQL_EXTENDED_LEN))
10043 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10044 str->q_append(SPIDER_SQL_SQL_EXTENDED_STR, SPIDER_SQL_SQL_EXTENDED_LEN);
10045 }
10046 if (check_opt->sql_flags & TT_USEFRM)
10047 {
10048 if (str->reserve(SPIDER_SQL_SQL_USE_FRM_LEN))
10049 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10050 str->q_append(SPIDER_SQL_SQL_USE_FRM_STR, SPIDER_SQL_SQL_USE_FRM_LEN);
10051 }
10052 DBUG_RETURN(0);
10053}
10054
10055int spider_mysql_handler::append_check_table_part(
10056 ulong sql_type,
10057 int link_idx,
10058 HA_CHECK_OPT* check_opt
10059) {
10060 int error_num;
10061 spider_string *str;
10062 DBUG_ENTER("spider_mysql_handler::append_check_table_part");
10063 DBUG_PRINT("info",("spider this=%p", this));
10064 switch (sql_type)
10065 {
10066 case SPIDER_SQL_TYPE_OTHER_SQL:
10067 str = &spider->result_list.sqls[link_idx];
10068 break;
10069 default:
10070 DBUG_RETURN(0);
10071 }
10072 error_num = append_check_table(str, link_idx, check_opt);
10073 DBUG_RETURN(error_num);
10074}
10075
10076int spider_mysql_handler::append_check_table(
10077 spider_string *str,
10078 int link_idx,
10079 HA_CHECK_OPT* check_opt
10080) {
10081 int conn_link_idx = spider->conn_link_idx[link_idx];
10082 DBUG_ENTER("spider_mysql_handler::append_check_table");
10083 DBUG_PRINT("info",("spider this=%p", this));
10084 if (str->reserve(SPIDER_SQL_SQL_CHECK_TABLE_LEN +
10085 mysql_share->db_names_str[conn_link_idx].length() +
10086 SPIDER_SQL_DOT_LEN +
10087 mysql_share->table_names_str[conn_link_idx].length() +
10088 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4))
10089 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10090 str->q_append(SPIDER_SQL_SQL_CHECK_TABLE_STR,
10091 SPIDER_SQL_SQL_CHECK_TABLE_LEN);
10092 mysql_share->append_table_name(str, conn_link_idx);
10093 if (check_opt->flags & T_QUICK)
10094 {
10095 if (str->reserve(SPIDER_SQL_SQL_QUICK_LEN))
10096 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10097 str->q_append(SPIDER_SQL_SQL_QUICK_STR, SPIDER_SQL_SQL_QUICK_LEN);
10098 }
10099 if (check_opt->flags & T_FAST)
10100 {
10101 if (str->reserve(SPIDER_SQL_SQL_FAST_LEN))
10102 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10103 str->q_append(SPIDER_SQL_SQL_FAST_STR, SPIDER_SQL_SQL_FAST_LEN);
10104 }
10105 if (check_opt->flags & T_MEDIUM)
10106 {
10107 if (str->reserve(SPIDER_SQL_SQL_MEDIUM_LEN))
10108 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10109 str->q_append(SPIDER_SQL_SQL_MEDIUM_STR, SPIDER_SQL_SQL_MEDIUM_LEN);
10110 }
10111 if (check_opt->flags & T_EXTEND)
10112 {
10113 if (str->reserve(SPIDER_SQL_SQL_EXTENDED_LEN))
10114 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10115 str->q_append(SPIDER_SQL_SQL_EXTENDED_STR, SPIDER_SQL_SQL_EXTENDED_LEN);
10116 }
10117 DBUG_RETURN(0);
10118}
10119
10120int spider_mysql_handler::append_enable_keys_part(
10121 ulong sql_type,
10122 int link_idx
10123) {
10124 int error_num;
10125 spider_string *str;
10126 DBUG_ENTER("spider_mysql_handler::append_enable_keys_part");
10127 DBUG_PRINT("info",("spider this=%p", this));
10128 switch (sql_type)
10129 {
10130 case SPIDER_SQL_TYPE_OTHER_SQL:
10131 str = &spider->result_list.sqls[link_idx];
10132 break;
10133 default:
10134 DBUG_RETURN(0);
10135 }
10136 error_num = append_enable_keys(str, link_idx);
10137 DBUG_RETURN(error_num);
10138}
10139
10140int spider_mysql_handler::append_enable_keys(
10141 spider_string *str,
10142 int link_idx
10143) {
10144 int conn_link_idx = spider->conn_link_idx[link_idx];
10145 DBUG_ENTER("spider_mysql_handler::append_enable_keys");
10146 DBUG_PRINT("info",("spider this=%p", this));
10147 if (str->reserve(SPIDER_SQL_SQL_ALTER_TABLE_LEN +
10148 mysql_share->db_names_str[conn_link_idx].length() +
10149 SPIDER_SQL_DOT_LEN +
10150 mysql_share->table_names_str[conn_link_idx].length() +
10151 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4 + SPIDER_SQL_SQL_ENABLE_KEYS_LEN))
10152 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10153 str->q_append(SPIDER_SQL_SQL_ALTER_TABLE_STR,
10154 SPIDER_SQL_SQL_ALTER_TABLE_LEN);
10155 mysql_share->append_table_name(str, conn_link_idx);
10156 str->q_append(SPIDER_SQL_SQL_ENABLE_KEYS_STR,
10157 SPIDER_SQL_SQL_ENABLE_KEYS_LEN);
10158 DBUG_RETURN(0);
10159}
10160
10161int spider_mysql_handler::append_disable_keys_part(
10162 ulong sql_type,
10163 int link_idx
10164) {
10165 int error_num;
10166 spider_string *str;
10167 DBUG_ENTER("spider_mysql_handler::append_disable_keys_part");
10168 DBUG_PRINT("info",("spider this=%p", this));
10169 switch (sql_type)
10170 {
10171 case SPIDER_SQL_TYPE_OTHER_SQL:
10172 str = &spider->result_list.sqls[link_idx];
10173 break;
10174 default:
10175 DBUG_RETURN(0);
10176 }
10177 error_num = append_disable_keys(str, link_idx);
10178 DBUG_RETURN(error_num);
10179}
10180
10181int spider_mysql_handler::append_disable_keys(
10182 spider_string *str,
10183 int link_idx
10184) {
10185 int conn_link_idx = spider->conn_link_idx[link_idx];
10186 DBUG_ENTER("spider_mysql_handler::append_disable_keys");
10187 DBUG_PRINT("info",("spider this=%p", this));
10188 if (str->reserve(SPIDER_SQL_SQL_ALTER_TABLE_LEN +
10189 mysql_share->db_names_str[conn_link_idx].length() +
10190 SPIDER_SQL_DOT_LEN +
10191 mysql_share->table_names_str[conn_link_idx].length() +
10192 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4 + SPIDER_SQL_SQL_DISABLE_KEYS_LEN))
10193 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10194 str->q_append(SPIDER_SQL_SQL_ALTER_TABLE_STR,
10195 SPIDER_SQL_SQL_ALTER_TABLE_LEN);
10196 mysql_share->append_table_name(str, conn_link_idx);
10197 str->q_append(SPIDER_SQL_SQL_DISABLE_KEYS_STR,
10198 SPIDER_SQL_SQL_DISABLE_KEYS_LEN);
10199 DBUG_RETURN(0);
10200}
10201
10202int spider_mysql_handler::append_delete_all_rows_part(
10203 ulong sql_type
10204) {
10205 int error_num;
10206 spider_string *str;
10207 DBUG_ENTER("spider_mysql_handler::append_delete_all_rows_part");
10208 DBUG_PRINT("info",("spider this=%p", this));
10209 switch (sql_type)
10210 {
10211 case SPIDER_SQL_TYPE_DELETE_SQL:
10212 str = &update_sql;
10213 break;
10214 default:
10215 DBUG_RETURN(0);
10216 }
10217 error_num = append_delete_all_rows(str, sql_type);
10218 DBUG_RETURN(error_num);
10219}
10220
10221int spider_mysql_handler::append_delete_all_rows(
10222 spider_string *str,
10223 ulong sql_type
10224) {
10225 int error_num;
10226 DBUG_ENTER("spider_mysql_handler::append_delete_all_rows");
10227 DBUG_PRINT("info",("spider this=%p", this));
10228 if (spider->sql_command == SQLCOM_TRUNCATE)
10229 {
10230 if ((error_num = append_truncate(str, sql_type, first_link_idx)))
10231 DBUG_RETURN(error_num);
10232 } else {
10233 if (
10234 (error_num = append_delete(str)) ||
10235 (error_num = append_from(str, sql_type, first_link_idx))
10236 )
10237 DBUG_RETURN(error_num);
10238 }
10239 DBUG_RETURN(0);
10240}
10241
10242int spider_mysql_handler::append_truncate(
10243 spider_string *str,
10244 ulong sql_type,
10245 int link_idx
10246) {
10247 DBUG_ENTER("spider_mysql_handler::append_truncate");
10248 if (str->reserve(SPIDER_SQL_TRUNCATE_TABLE_LEN +
10249 mysql_share->db_nm_max_length +
10250 SPIDER_SQL_DOT_LEN + mysql_share->table_nm_max_length +
10251 /* SPIDER_SQL_NAME_QUOTE_LEN */ 4 + SPIDER_SQL_OPEN_PAREN_LEN))
10252 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10253 str->q_append(SPIDER_SQL_TRUNCATE_TABLE_STR, SPIDER_SQL_TRUNCATE_TABLE_LEN);
10254 table_name_pos = str->length();
10255 append_table_name_with_adjusting(str, link_idx, sql_type);
10256 DBUG_RETURN(0);
10257}
10258
10259int spider_mysql_handler::append_explain_select_part(
10260 key_range *start_key,
10261 key_range *end_key,
10262 ulong sql_type,
10263 int link_idx
10264) {
10265 int error_num;
10266 spider_string *str;
10267 DBUG_ENTER("spider_mysql_handler::append_explain_select_part");
10268 DBUG_PRINT("info",("spider this=%p", this));
10269 switch (sql_type)
10270 {
10271 case SPIDER_SQL_TYPE_OTHER_SQL:
10272 str = &spider->result_list.sqls[link_idx];
10273 break;
10274 default:
10275 DBUG_RETURN(0);
10276 }
10277 error_num =
10278 append_explain_select(str, start_key, end_key, sql_type, link_idx);
10279 DBUG_RETURN(error_num);
10280}
10281
10282int spider_mysql_handler::append_explain_select(
10283 spider_string *str,
10284 key_range *start_key,
10285 key_range *end_key,
10286 ulong sql_type,
10287 int link_idx
10288) {
10289 int error_num;
10290 DBUG_ENTER("spider_mysql_handler::append_explain_select");
10291 DBUG_PRINT("info",("spider this=%p", this));
10292 if (str->reserve(SPIDER_SQL_EXPLAIN_SELECT_LEN))
10293 {
10294 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10295 }
10296 str->q_append(SPIDER_SQL_EXPLAIN_SELECT_STR, SPIDER_SQL_EXPLAIN_SELECT_LEN);
10297 if (
10298 (error_num = append_from(str, sql_type, link_idx)) ||
10299 (error_num = append_key_where(str, NULL, NULL, start_key, end_key,
10300 sql_type, FALSE))
10301 ) {
10302 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10303 }
10304 DBUG_RETURN(0);
10305}
10306
10307/********************************************************************
10308 * Determine whether the current query's projection list
10309 * consists solely of the specified column.
10310 *
10311 * Params IN - field_index:
10312 * Field index of the column of interest within
10313 * its table.
10314 *
10315 * Returns TRUE - if the query's projection list consists
10316 * solely of the specified column.
10317 * FALSE - otherwise.
10318 ********************************************************************/
10319bool spider_mysql_handler::is_sole_projection_field(
10320 uint16 field_index
10321) {
10322 // Determine whether the projection list consists solely of the field of interest
10323 bool is_field_in_projection_list = FALSE;
10324 TABLE* table = spider->get_table();
10325 uint16 projection_field_count = 0;
10326 uint16 projection_field_index;
10327 Field** field;
10328 DBUG_ENTER( "spider_mysql_handler::is_sole_projection_field" );
10329
10330 for ( field = table->field; *field ; field++ )
10331 {
10332 projection_field_index = ( *field )->field_index;
10333
10334 if ( !( minimum_select_bit_is_set( projection_field_index ) ) )
10335 {
10336 // Current field is not in the projection list
10337 continue;
10338 }
10339
10340 projection_field_count++;
10341
10342 if ( !is_field_in_projection_list )
10343 {
10344 if ( field_index == projection_field_index )
10345 {
10346 // Field of interest is in the projection list
10347 is_field_in_projection_list = TRUE;
10348 }
10349 }
10350
10351 if ( is_field_in_projection_list && ( projection_field_count != 1 ) )
10352 {
10353 // Field of interest is not the sole column in the projection list
10354 DBUG_RETURN( FALSE );
10355 }
10356 }
10357
10358 if ( is_field_in_projection_list && ( projection_field_count == 1 ) )
10359 {
10360 // Field of interest is the only column in the projection list
10361 DBUG_RETURN( TRUE );
10362 }
10363
10364 DBUG_RETURN( FALSE );
10365}
10366
10367bool spider_mysql_handler::is_bulk_insert_exec_period(
10368 bool bulk_end
10369) {
10370 DBUG_ENTER("spider_mysql_handler::is_bulk_insert_exec_period");
10371 DBUG_PRINT("info",("spider this=%p", this));
10372 DBUG_PRINT("info",("spider insert_sql.length=%u", insert_sql.length()));
10373 DBUG_PRINT("info",("spider insert_pos=%d", insert_pos));
10374 DBUG_PRINT("info",("spider insert_sql=%s", insert_sql.c_ptr_safe()));
10375 if (
10376 (bulk_end || (int) insert_sql.length() >= spider->bulk_size) &&
10377 (int) insert_sql.length() > insert_pos
10378 ) {
10379 DBUG_RETURN(TRUE);
10380 }
10381 DBUG_RETURN(FALSE);
10382}
10383
10384bool spider_mysql_handler::sql_is_filled_up(
10385 ulong sql_type
10386) {
10387 DBUG_ENTER("spider_mysql_handler::sql_is_filled_up");
10388 DBUG_PRINT("info",("spider this=%p", this));
10389 DBUG_RETURN(filled_up);
10390}
10391
10392bool spider_mysql_handler::sql_is_empty(
10393 ulong sql_type
10394) {
10395 bool is_empty;
10396 DBUG_ENTER("spider_mysql_handler::sql_is_empty");
10397 DBUG_PRINT("info",("spider this=%p", this));
10398 switch (sql_type)
10399 {
10400 case SPIDER_SQL_TYPE_SELECT_SQL:
10401 is_empty = (sql.length() == 0);
10402 break;
10403 case SPIDER_SQL_TYPE_INSERT_SQL:
10404 is_empty = (insert_sql.length() == 0);
10405 break;
10406 case SPIDER_SQL_TYPE_UPDATE_SQL:
10407 case SPIDER_SQL_TYPE_DELETE_SQL:
10408 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
10409 is_empty = (update_sql.length() == 0);
10410 break;
10411 case SPIDER_SQL_TYPE_TMP_SQL:
10412 is_empty = (tmp_sql.length() == 0);
10413 break;
10414 case SPIDER_SQL_TYPE_HANDLER:
10415 is_empty = (ha_sql.length() == 0);
10416 break;
10417 default:
10418 is_empty = TRUE;
10419 break;
10420 }
10421 DBUG_RETURN(is_empty);
10422}
10423
10424bool spider_mysql_handler::support_multi_split_read()
10425{
10426 DBUG_ENTER("spider_mysql_handler::support_multi_split_read");
10427 DBUG_PRINT("info",("spider this=%p", this));
10428 DBUG_RETURN(TRUE);
10429}
10430
10431bool spider_mysql_handler::support_bulk_update()
10432{
10433 DBUG_ENTER("spider_mysql_handler::support_bulk_update");
10434 DBUG_PRINT("info",("spider this=%p", this));
10435 DBUG_RETURN(TRUE);
10436}
10437
10438int spider_mysql_handler::bulk_tmp_table_insert()
10439{
10440 int error_num;
10441 DBUG_ENTER("spider_mysql_handler::bulk_tmp_table_insert");
10442 DBUG_PRINT("info",("spider this=%p", this));
10443 error_num = store_sql_to_bulk_tmp_table(&update_sql, upd_tmp_tbl);
10444 DBUG_RETURN(error_num);
10445}
10446
10447int spider_mysql_handler::bulk_tmp_table_insert(
10448 int link_idx
10449) {
10450 int error_num;
10451 DBUG_ENTER("spider_mysql_handler::bulk_tmp_table_insert");
10452 DBUG_PRINT("info",("spider this=%p", this));
10453 error_num = store_sql_to_bulk_tmp_table(
10454 &spider->result_list.update_sqls[link_idx],
10455 spider->result_list.upd_tmp_tbls[link_idx]);
10456 DBUG_RETURN(error_num);
10457}
10458
10459int spider_mysql_handler::bulk_tmp_table_end_bulk_insert()
10460{
10461 int error_num;
10462 DBUG_ENTER("spider_mysql_handler::bulk_tmp_table_end_bulk_insert");
10463 DBUG_PRINT("info",("spider this=%p", this));
10464 if ((error_num = upd_tmp_tbl->file->ha_end_bulk_insert()))
10465 {
10466 DBUG_RETURN(error_num);
10467 }
10468 DBUG_RETURN(0);
10469}
10470
10471int spider_mysql_handler::bulk_tmp_table_rnd_init()
10472{
10473 int error_num;
10474 DBUG_ENTER("spider_mysql_handler::bulk_tmp_table_rnd_init");
10475 DBUG_PRINT("info",("spider this=%p", this));
10476 upd_tmp_tbl->file->extra(HA_EXTRA_CACHE);
10477 if ((error_num = upd_tmp_tbl->file->ha_rnd_init(TRUE)))
10478 {
10479 DBUG_RETURN(error_num);
10480 }
10481 reading_from_bulk_tmp_table = TRUE;
10482 DBUG_RETURN(0);
10483}
10484
10485int spider_mysql_handler::bulk_tmp_table_rnd_next()
10486{
10487 int error_num;
10488 DBUG_ENTER("spider_mysql_handler::bulk_tmp_table_rnd_next");
10489 DBUG_PRINT("info",("spider this=%p", this));
10490#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50200
10491 error_num = upd_tmp_tbl->file->ha_rnd_next(upd_tmp_tbl->record[0]);
10492#else
10493 error_num = upd_tmp_tbl->file->rnd_next(upd_tmp_tbl->record[0]);
10494#endif
10495 if (!error_num)
10496 {
10497 error_num = restore_sql_from_bulk_tmp_table(&insert_sql, upd_tmp_tbl);
10498 }
10499 DBUG_RETURN(error_num);
10500}
10501
10502int spider_mysql_handler::bulk_tmp_table_rnd_end()
10503{
10504 int error_num;
10505 DBUG_ENTER("spider_mysql_handler::bulk_tmp_table_rnd_end");
10506 DBUG_PRINT("info",("spider this=%p", this));
10507 reading_from_bulk_tmp_table = FALSE;
10508 if ((error_num = upd_tmp_tbl->file->ha_rnd_end()))
10509 {
10510 DBUG_RETURN(error_num);
10511 }
10512 DBUG_RETURN(0);
10513}
10514
10515bool spider_mysql_handler::need_copy_for_update(
10516 int link_idx
10517) {
10518 int all_link_idx = spider->conn_link_idx[link_idx];
10519 DBUG_ENTER("spider_mysql_handler::need_copy_for_update");
10520 DBUG_PRINT("info",("spider this=%p", this));
10521 DBUG_RETURN(!mysql_share->same_db_table_name ||
10522 spider->share->link_statuses[all_link_idx] == SPIDER_LINK_STATUS_RECOVERY);
10523}
10524
10525bool spider_mysql_handler::bulk_tmp_table_created()
10526{
10527 DBUG_ENTER("spider_mysql_handler::bulk_tmp_table_created");
10528 DBUG_PRINT("info",("spider this=%p", this));
10529 DBUG_RETURN(upd_tmp_tbl);
10530}
10531
10532int spider_mysql_handler::mk_bulk_tmp_table_and_bulk_start()
10533{
10534 THD *thd = spider->trx->thd;
10535 TABLE *table = spider->get_table();
10536 DBUG_ENTER("spider_mysql_handler::mk_bulk_tmp_table_and_bulk_start");
10537 DBUG_PRINT("info",("spider this=%p", this));
10538 if (!upd_tmp_tbl)
10539 {
10540 if (!(upd_tmp_tbl = spider_mk_sys_tmp_table(
10541 thd, table, &upd_tmp_tbl_prm, "a", update_sql.charset())))
10542 {
10543 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10544 }
10545 upd_tmp_tbl->file->extra(HA_EXTRA_WRITE_CACHE);
10546 upd_tmp_tbl->file->ha_start_bulk_insert((ha_rows) 0);
10547 }
10548 DBUG_RETURN(0);
10549}
10550
10551void spider_mysql_handler::rm_bulk_tmp_table()
10552{
10553 DBUG_ENTER("spider_mysql_handler::rm_bulk_tmp_table");
10554 DBUG_PRINT("info",("spider this=%p", this));
10555 if (upd_tmp_tbl)
10556 {
10557 spider_rm_sys_tmp_table(spider->trx->thd, upd_tmp_tbl, &upd_tmp_tbl_prm);
10558 upd_tmp_tbl = NULL;
10559 }
10560 DBUG_VOID_RETURN;
10561}
10562
10563int spider_mysql_handler::store_sql_to_bulk_tmp_table(
10564 spider_string *str,
10565 TABLE *tmp_table
10566) {
10567 int error_num;
10568 DBUG_ENTER("spider_mysql_handler::store_sql_to_bulk_tmp_table");
10569 DBUG_PRINT("info",("spider this=%p", this));
10570 tmp_table->field[0]->set_notnull();
10571 tmp_table->field[0]->store(str->ptr(), str->length(), str->charset());
10572 if ((error_num = tmp_table->file->ha_write_row(tmp_table->record[0])))
10573 DBUG_RETURN(error_num);
10574 DBUG_RETURN(0);
10575}
10576
10577int spider_mysql_handler::restore_sql_from_bulk_tmp_table(
10578 spider_string *str,
10579 TABLE *tmp_table
10580) {
10581 DBUG_ENTER("spider_mysql_handler::restore_sql_from_bulk_tmp_table");
10582 DBUG_PRINT("info",("spider this=%p", this));
10583 tmp_table->field[0]->val_str(str->get_str());
10584 str->mem_calc();
10585 DBUG_RETURN(0);
10586}
10587
10588int spider_mysql_handler::insert_lock_tables_list(
10589 SPIDER_CONN *conn,
10590 int link_idx
10591) {
10592 spider_db_mysql *db_conn = (spider_db_mysql *) conn->db_conn;
10593 SPIDER_LINK_FOR_HASH *tmp_link_for_hash2 = &link_for_hash[link_idx];
10594 DBUG_ENTER("spider_mysql_handler::insert_lock_tables_list");
10595 DBUG_PRINT("info",("spider this=%p", this));
10596 uint old_elements =
10597 db_conn->lock_table_hash.array.max_element;
10598#ifdef HASH_UPDATE_WITH_HASH_VALUE
10599 if (my_hash_insert_with_hash_value(
10600 &db_conn->lock_table_hash,
10601 tmp_link_for_hash2->db_table_str_hash_value,
10602 (uchar*) tmp_link_for_hash2))
10603#else
10604 if (my_hash_insert(&db_conn->lock_table_hash,
10605 (uchar*) tmp_link_for_hash2))
10606#endif
10607 {
10608 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10609 }
10610 if (db_conn->lock_table_hash.array.max_element > old_elements)
10611 {
10612 spider_alloc_calc_mem(spider_current_trx,
10613 db_conn->lock_table_hash,
10614 (db_conn->lock_table_hash.array.max_element - old_elements) *
10615 db_conn->lock_table_hash.array.size_of_element);
10616 }
10617 DBUG_RETURN(0);
10618}
10619
10620int spider_mysql_handler::append_lock_tables_list(
10621 SPIDER_CONN *conn,
10622 int link_idx,
10623 int *appended
10624) {
10625 int error_num;
10626 SPIDER_LINK_FOR_HASH *tmp_link_for_hash, *tmp_link_for_hash2;
10627 int conn_link_idx = spider->conn_link_idx[link_idx];
10628 spider_db_mysql *db_conn = (spider_db_mysql *) conn->db_conn;
10629 DBUG_ENTER("spider_mysql_handler::append_lock_tables_list");
10630 DBUG_PRINT("info",("spider this=%p", this));
10631 tmp_link_for_hash2 = &link_for_hash[link_idx];
10632 tmp_link_for_hash2->db_table_str =
10633 &mysql_share->db_table_str[conn_link_idx];
10634#ifdef SPIDER_HAS_HASH_VALUE_TYPE
10635 tmp_link_for_hash2->db_table_str_hash_value =
10636 mysql_share->db_table_str_hash_value[conn_link_idx];
10637 if (!(tmp_link_for_hash = (SPIDER_LINK_FOR_HASH *)
10638 my_hash_search_using_hash_value(
10639 &db_conn->lock_table_hash,
10640 tmp_link_for_hash2->db_table_str_hash_value,
10641 (uchar*) tmp_link_for_hash2->db_table_str->ptr(),
10642 tmp_link_for_hash2->db_table_str->length())))
10643#else
10644 if (!(tmp_link_for_hash = (SPIDER_LINK_FOR_HASH *) my_hash_search(
10645 &db_conn->lock_table_hash,
10646 (uchar*) tmp_link_for_hash2->db_table_str->ptr(),
10647 tmp_link_for_hash2->db_table_str->length())))
10648#endif
10649 {
10650 if ((error_num = insert_lock_tables_list(conn, link_idx)))
10651 DBUG_RETURN(error_num);
10652 *appended = 1;
10653 } else {
10654 if (tmp_link_for_hash->spider->lock_type < spider->lock_type)
10655 {
10656#ifdef HASH_UPDATE_WITH_HASH_VALUE
10657 my_hash_delete_with_hash_value(
10658 &db_conn->lock_table_hash,
10659 tmp_link_for_hash->db_table_str_hash_value,
10660 (uchar*) tmp_link_for_hash);
10661#else
10662 my_hash_delete(&db_conn->lock_table_hash,
10663 (uchar*) tmp_link_for_hash);
10664#endif
10665 uint old_elements =
10666 db_conn->lock_table_hash.array.max_element;
10667#ifdef HASH_UPDATE_WITH_HASH_VALUE
10668 if (my_hash_insert_with_hash_value(
10669 &db_conn->lock_table_hash,
10670 tmp_link_for_hash2->db_table_str_hash_value,
10671 (uchar*) tmp_link_for_hash2))
10672#else
10673 if (my_hash_insert(&db_conn->lock_table_hash,
10674 (uchar*) tmp_link_for_hash2))
10675#endif
10676 {
10677 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10678 }
10679 if (db_conn->lock_table_hash.array.max_element > old_elements)
10680 {
10681 spider_alloc_calc_mem(spider_current_trx,
10682 db_conn->lock_table_hash,
10683 (db_conn->lock_table_hash.array.max_element - old_elements) *
10684 db_conn->lock_table_hash.array.size_of_element);
10685 }
10686 }
10687 }
10688 DBUG_RETURN(0);
10689}
10690
10691int spider_mysql_handler::realloc_sql(
10692 ulong *realloced
10693) {
10694 THD *thd = spider->trx->thd;
10695 st_spider_share *share = spider->share;
10696 int init_sql_alloc_size =
10697 spider_param_init_sql_alloc_size(thd, share->init_sql_alloc_size);
10698 DBUG_ENTER("spider_mysql_handler::realloc_sql");
10699 DBUG_PRINT("info",("spider this=%p", this));
10700 if ((int) sql.alloced_length() > init_sql_alloc_size * 2)
10701 {
10702 sql.free();
10703 if (sql.real_alloc(init_sql_alloc_size))
10704 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10705 *realloced |= SPIDER_SQL_TYPE_SELECT_SQL;
10706 }
10707 if ((int) ha_sql.alloced_length() > init_sql_alloc_size * 2)
10708 {
10709 ha_sql.free();
10710 if (ha_sql.real_alloc(init_sql_alloc_size))
10711 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10712 *realloced |= SPIDER_SQL_TYPE_SELECT_SQL;
10713 }
10714 if ((int) dup_update_sql.alloced_length() > init_sql_alloc_size * 2)
10715 {
10716 dup_update_sql.free();
10717 if (dup_update_sql.real_alloc(init_sql_alloc_size))
10718 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10719 }
10720 if ((int) insert_sql.alloced_length() > init_sql_alloc_size * 2)
10721 {
10722 insert_sql.free();
10723 if (insert_sql.real_alloc(init_sql_alloc_size))
10724 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10725 *realloced |= SPIDER_SQL_TYPE_INSERT_SQL;
10726 }
10727 if ((int) update_sql.alloced_length() > init_sql_alloc_size * 2)
10728 {
10729 update_sql.free();
10730 if (update_sql.real_alloc(init_sql_alloc_size))
10731 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10732 *realloced |= (SPIDER_SQL_TYPE_UPDATE_SQL | SPIDER_SQL_TYPE_DELETE_SQL);
10733 }
10734 update_sql.length(0);
10735 if ((int) tmp_sql.alloced_length() > init_sql_alloc_size * 2)
10736 {
10737 tmp_sql.free();
10738 if (tmp_sql.real_alloc(init_sql_alloc_size))
10739 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10740 *realloced |= SPIDER_SQL_TYPE_TMP_SQL;
10741 }
10742 DBUG_RETURN(0);
10743}
10744
10745int spider_mysql_handler::reset_sql(
10746 ulong sql_type
10747) {
10748 DBUG_ENTER("spider_mysql_handler::reset_sql");
10749 DBUG_PRINT("info",("spider this=%p", this));
10750 if (sql_type & SPIDER_SQL_TYPE_SELECT_SQL)
10751 {
10752 sql.length(0);
10753 }
10754 if (sql_type & SPIDER_SQL_TYPE_INSERT_SQL)
10755 {
10756 insert_sql.length(0);
10757 }
10758 if (sql_type & (SPIDER_SQL_TYPE_UPDATE_SQL | SPIDER_SQL_TYPE_DELETE_SQL |
10759 SPIDER_SQL_TYPE_BULK_UPDATE_SQL))
10760 {
10761 update_sql.length(0);
10762 }
10763 if (sql_type & SPIDER_SQL_TYPE_TMP_SQL)
10764 {
10765 tmp_sql.length(0);
10766 }
10767 if (sql_type & SPIDER_SQL_TYPE_HANDLER)
10768 {
10769 ha_sql.length(0);
10770 }
10771 DBUG_RETURN(0);
10772}
10773
10774#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
10775int spider_mysql_handler::reset_keys(
10776 ulong sql_type
10777) {
10778 DBUG_ENTER("spider_mysql_handler::reset_keys");
10779 DBUG_PRINT("info",("spider this=%p", this));
10780 DBUG_ASSERT(0);
10781 DBUG_RETURN(0);
10782}
10783
10784int spider_mysql_handler::reset_upds(
10785 ulong sql_type
10786) {
10787 DBUG_ENTER("spider_mysql_handler::reset_upds");
10788 DBUG_PRINT("info",("spider this=%p", this));
10789 hs_upds.clear();
10790 DBUG_RETURN(0);
10791}
10792
10793int spider_mysql_handler::reset_strs(
10794 ulong sql_type
10795) {
10796 DBUG_ENTER("spider_mysql_handler::reset_strs");
10797 DBUG_PRINT("info",("spider this=%p", this));
10798 DBUG_ASSERT(0);
10799 DBUG_RETURN(0);
10800}
10801
10802int spider_mysql_handler::reset_strs_pos(
10803 ulong sql_type
10804) {
10805 DBUG_ENTER("spider_mysql_handler::reset_strs_pos");
10806 DBUG_PRINT("info",("spider this=%p", this));
10807 DBUG_ASSERT(0);
10808 DBUG_RETURN(0);
10809}
10810
10811int spider_mysql_handler::push_back_upds(
10812 SPIDER_HS_STRING_REF &info
10813) {
10814 int error_num;
10815 DBUG_ENTER("spider_mysql_handler::push_back_upds");
10816 DBUG_PRINT("info",("spider this=%p", this));
10817 error_num = hs_upds.push_back(info);
10818 DBUG_RETURN(error_num);
10819}
10820#endif
10821
10822bool spider_mysql_handler::need_lock_before_set_sql_for_exec(
10823 ulong sql_type
10824) {
10825 DBUG_ENTER("spider_mysql_handler::need_lock_before_set_sql_for_exec");
10826 DBUG_PRINT("info",("spider this=%p", this));
10827 DBUG_RETURN(FALSE);
10828}
10829
10830#ifdef SPIDER_HAS_GROUP_BY_HANDLER
10831int spider_mysql_handler::set_sql_for_exec(
10832 ulong sql_type,
10833 int link_idx,
10834 SPIDER_LINK_IDX_CHAIN *link_idx_chain
10835) {
10836 int error_num;
10837 DBUG_ENTER("spider_mysql_handler::set_sql_for_exec");
10838 DBUG_PRINT("info",("spider this=%p", this));
10839 if (sql_type & SPIDER_SQL_TYPE_SELECT_SQL)
10840 {
10841 if ((error_num = spider_db_mysql_utility.reappend_tables(
10842 spider->fields, link_idx_chain, &sql)))
10843 DBUG_RETURN(error_num);
10844 exec_sql = &sql;
10845 }
10846 DBUG_RETURN(0);
10847}
10848#endif
10849
10850int spider_mysql_handler::set_sql_for_exec(
10851 ulong sql_type,
10852 int link_idx
10853) {
10854 int error_num;
10855 uint tmp_pos;
10856 SPIDER_SHARE *share = spider->share;
10857 SPIDER_RESULT_LIST *result_list = &spider->result_list;
10858 int all_link_idx = spider->conn_link_idx[link_idx];
10859 DBUG_ENTER("spider_mysql_handler::set_sql_for_exec");
10860 DBUG_PRINT("info",("spider this=%p", this));
10861 if (sql_type & (SPIDER_SQL_TYPE_SELECT_SQL | SPIDER_SQL_TYPE_TMP_SQL))
10862 {
10863 if (mysql_share->same_db_table_name || link_idx == first_link_idx)
10864 {
10865 if (sql_type & SPIDER_SQL_TYPE_SELECT_SQL)
10866 exec_sql = &sql;
10867 if (sql_type & SPIDER_SQL_TYPE_TMP_SQL)
10868 exec_tmp_sql = &tmp_sql;
10869 } else {
10870 char tmp_table_name[MAX_FIELD_WIDTH * 2],
10871 tgt_table_name[MAX_FIELD_WIDTH * 2];
10872 int tmp_table_name_length;
10873 spider_string tgt_table_name_str(tgt_table_name,
10874 MAX_FIELD_WIDTH * 2,
10875 mysql_share->db_names_str[link_idx].charset());
10876 const char *table_names[2], *table_aliases[2];
10877 uint table_name_lengths[2], table_alias_lengths[2];
10878 tgt_table_name_str.init_calc_mem(104);
10879 tgt_table_name_str.length(0);
10880 if (result_list->tmp_table_join && spider->bka_mode != 2)
10881 {
10882 create_tmp_bka_table_name(tmp_table_name, &tmp_table_name_length,
10883 link_idx);
10884 append_table_name_with_adjusting(&tgt_table_name_str, link_idx,
10885 SPIDER_SQL_TYPE_TMP_SQL);
10886 table_names[0] = tmp_table_name;
10887 table_names[1] = tgt_table_name_str.ptr();
10888 table_name_lengths[0] = tmp_table_name_length;
10889 table_name_lengths[1] = tgt_table_name_str.length();
10890 table_aliases[0] = SPIDER_SQL_A_STR;
10891 table_aliases[1] = SPIDER_SQL_B_STR;
10892 table_alias_lengths[0] = SPIDER_SQL_A_LEN;
10893 table_alias_lengths[1] = SPIDER_SQL_B_LEN;
10894 }
10895 if (sql_type & SPIDER_SQL_TYPE_SELECT_SQL)
10896 {
10897 exec_sql = &result_list->sqls[link_idx];
10898 if (exec_sql->copy(sql))
10899 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10900 else if (result_list->use_union)
10901 {
10902 if ((error_num = reset_union_table_name(exec_sql, link_idx,
10903 SPIDER_SQL_TYPE_SELECT_SQL)))
10904 DBUG_RETURN(error_num);
10905 } else {
10906 tmp_pos = exec_sql->length();
10907 exec_sql->length(table_name_pos);
10908 if (result_list->tmp_table_join && spider->bka_mode != 2)
10909 {
10910 if ((error_num = spider_db_mysql_utility.append_from_with_alias(
10911 exec_sql, table_names, table_name_lengths,
10912 table_aliases, table_alias_lengths, 2,
10913 &table_name_pos, TRUE))
10914 )
10915 DBUG_RETURN(error_num);
10916 exec_sql->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
10917 } else {
10918 append_table_name_with_adjusting(exec_sql, link_idx,
10919 SPIDER_SQL_TYPE_SELECT_SQL);
10920 }
10921 exec_sql->length(tmp_pos);
10922 }
10923 }
10924 if (sql_type & SPIDER_SQL_TYPE_TMP_SQL)
10925 {
10926 exec_tmp_sql = &result_list->tmp_sqls[link_idx];
10927 if (result_list->tmp_table_join && spider->bka_mode != 2)
10928 {
10929 if (exec_tmp_sql->copy(tmp_sql))
10930 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10931 else {
10932 tmp_pos = exec_tmp_sql->length();
10933 exec_tmp_sql->length(tmp_sql_pos1);
10934 exec_tmp_sql->q_append(tmp_table_name, tmp_table_name_length);
10935 exec_tmp_sql->length(tmp_sql_pos2);
10936 exec_tmp_sql->q_append(tmp_table_name, tmp_table_name_length);
10937 exec_tmp_sql->length(tmp_sql_pos3);
10938 exec_tmp_sql->q_append(tmp_table_name, tmp_table_name_length);
10939 exec_tmp_sql->length(tmp_pos);
10940 }
10941 }
10942 }
10943 }
10944 }
10945 if (sql_type & SPIDER_SQL_TYPE_INSERT_SQL)
10946 {
10947 if (mysql_share->same_db_table_name || link_idx == first_link_idx)
10948 exec_insert_sql = &insert_sql;
10949 else {
10950 exec_insert_sql = &result_list->insert_sqls[link_idx];
10951 if (exec_insert_sql->copy(insert_sql))
10952 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10953 DBUG_PRINT("info",("spider exec_insert_sql=%s",
10954 exec_insert_sql->c_ptr_safe()));
10955 tmp_pos = exec_insert_sql->length();
10956 exec_insert_sql->length(insert_table_name_pos);
10957 append_table_name_with_adjusting(exec_insert_sql, link_idx,
10958 sql_type);
10959 exec_insert_sql->length(tmp_pos);
10960 DBUG_PRINT("info",("spider exec_insert_sql->length=%u",
10961 exec_insert_sql->length()));
10962 DBUG_PRINT("info",("spider exec_insert_sql=%s",
10963 exec_insert_sql->c_ptr_safe()));
10964 }
10965 }
10966 if (sql_type & SPIDER_SQL_TYPE_BULK_UPDATE_SQL)
10967 {
10968 if (reading_from_bulk_tmp_table)
10969 {
10970 if (
10971 mysql_share->same_db_table_name &&
10972 share->link_statuses[all_link_idx] != SPIDER_LINK_STATUS_RECOVERY
10973 ) {
10974 exec_update_sql = &insert_sql;
10975 } else if (!spider->result_list.upd_tmp_tbls[link_idx])
10976 {
10977 DBUG_RETURN(ER_SPIDER_COND_SKIP_NUM);
10978 } else {
10979 exec_update_sql = &spider->result_list.insert_sqls[link_idx];
10980 if ((error_num = restore_sql_from_bulk_tmp_table(exec_update_sql,
10981 spider->result_list.upd_tmp_tbls[link_idx])))
10982 {
10983 DBUG_RETURN(error_num);
10984 }
10985 }
10986 } else {
10987 if (
10988 mysql_share->same_db_table_name &&
10989 share->link_statuses[all_link_idx] != SPIDER_LINK_STATUS_RECOVERY
10990 ) {
10991 exec_update_sql = &update_sql;
10992 } else {
10993 exec_update_sql = &spider->result_list.update_sqls[link_idx];
10994 }
10995 }
10996 } else if (sql_type &
10997 (SPIDER_SQL_TYPE_UPDATE_SQL | SPIDER_SQL_TYPE_DELETE_SQL))
10998 {
10999 if (mysql_share->same_db_table_name || link_idx == first_link_idx)
11000 exec_update_sql = &update_sql;
11001 else {
11002 exec_update_sql = &spider->result_list.update_sqls[link_idx];
11003 if (exec_update_sql->copy(update_sql))
11004 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
11005 tmp_pos = exec_update_sql->length();
11006 exec_update_sql->length(table_name_pos);
11007 append_table_name_with_adjusting(exec_update_sql, link_idx,
11008 sql_type);
11009 exec_update_sql->length(tmp_pos);
11010 }
11011 }
11012 if (sql_type & SPIDER_SQL_TYPE_HANDLER)
11013 {
11014 if (spider->m_handler_id[link_idx] == ha_sql_handler_id)
11015 exec_ha_sql = &ha_sql;
11016 else {
11017 exec_ha_sql = &result_list->sqls[link_idx];
11018 if (exec_ha_sql->copy(ha_sql))
11019 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
11020 else {
11021 tmp_pos = exec_ha_sql->length();
11022 exec_ha_sql->length(ha_table_name_pos);
11023 append_table_name_with_adjusting(exec_ha_sql, link_idx,
11024 SPIDER_SQL_TYPE_HANDLER);
11025 exec_ha_sql->length(tmp_pos);
11026 }
11027 }
11028 }
11029 DBUG_RETURN(0);
11030}
11031
11032int spider_mysql_handler::set_sql_for_exec(
11033 spider_db_copy_table *tgt_ct,
11034 ulong sql_type
11035) {
11036 spider_mysql_copy_table *mysql_ct = (spider_mysql_copy_table *) tgt_ct;
11037 DBUG_ENTER("spider_mysql_handler::set_sql_for_exec");
11038 DBUG_PRINT("info",("spider this=%p", this));
11039 switch (sql_type)
11040 {
11041 case SPIDER_SQL_TYPE_INSERT_SQL:
11042 exec_insert_sql = &mysql_ct->sql;
11043 break;
11044 default:
11045 DBUG_ASSERT(0);
11046 break;
11047 }
11048 DBUG_RETURN(0);
11049}
11050
11051int spider_mysql_handler::execute_sql(
11052 ulong sql_type,
11053 SPIDER_CONN *conn,
11054 int quick_mode,
11055 int *need_mon
11056) {
11057 spider_string *tgt_sql;
11058 uint tgt_length;
11059 DBUG_ENTER("spider_mysql_handler::execute_sql");
11060 DBUG_PRINT("info",("spider this=%p", this));
11061 switch (sql_type)
11062 {
11063 case SPIDER_SQL_TYPE_SELECT_SQL:
11064 DBUG_PRINT("info",("spider SPIDER_SQL_TYPE_SELECT_SQL"));
11065 tgt_sql = exec_sql;
11066 tgt_length = tgt_sql->length();
11067 break;
11068 case SPIDER_SQL_TYPE_INSERT_SQL:
11069 DBUG_PRINT("info",("spider SPIDER_SQL_TYPE_SELECT_SQL"));
11070 tgt_sql = exec_insert_sql;
11071 tgt_length = tgt_sql->length();
11072 break;
11073 case SPIDER_SQL_TYPE_UPDATE_SQL:
11074 case SPIDER_SQL_TYPE_DELETE_SQL:
11075 case SPIDER_SQL_TYPE_BULK_UPDATE_SQL:
11076 DBUG_PRINT("info",("spider %s",
11077 sql_type == SPIDER_SQL_TYPE_UPDATE_SQL ? "SPIDER_SQL_TYPE_UPDATE_SQL" :
11078 sql_type == SPIDER_SQL_TYPE_DELETE_SQL ? "SPIDER_SQL_TYPE_DELETE_SQL" :
11079 "SPIDER_SQL_TYPE_BULK_UPDATE_SQL"
11080 ));
11081 tgt_sql = exec_update_sql;
11082 tgt_length = tgt_sql->length();
11083 break;
11084 case SPIDER_SQL_TYPE_TMP_SQL:
11085 DBUG_PRINT("info",("spider SPIDER_SQL_TYPE_TMP_SQL"));
11086 tgt_sql = exec_tmp_sql;
11087 tgt_length = tgt_sql->length();
11088 break;
11089 case SPIDER_SQL_TYPE_DROP_TMP_TABLE_SQL:
11090 DBUG_PRINT("info",("spider SPIDER_SQL_TYPE_DROP_TMP_TABLE_SQL"));
11091 tgt_sql = exec_tmp_sql;
11092 tgt_length = tmp_sql_pos5;
11093 break;
11094 case SPIDER_SQL_TYPE_HANDLER:
11095 DBUG_PRINT("info",("spider SPIDER_SQL_TYPE_HANDLER"));
11096 tgt_sql = exec_ha_sql;
11097 tgt_length = tgt_sql->length();
11098 break;
11099 default:
11100 /* nothing to do */
11101 DBUG_PRINT("info",("spider default"));
11102 DBUG_RETURN(0);
11103 }
11104 DBUG_RETURN(spider_db_query(
11105 conn,
11106 tgt_sql->ptr(),
11107 tgt_length,
11108 quick_mode,
11109 need_mon
11110 ));
11111}
11112
11113int spider_mysql_handler::reset()
11114{
11115 DBUG_ENTER("spider_mysql_handler::reset");
11116 DBUG_PRINT("info",("spider this=%p", this));
11117 update_sql.length(0);
11118 DBUG_RETURN(0);
11119}
11120
11121int spider_mysql_handler::sts_mode_exchange(
11122 int sts_mode
11123) {
11124 DBUG_ENTER("spider_mysql_handler::sts_mode_exchange");
11125 DBUG_PRINT("info",("spider sts_mode=%d", sts_mode));
11126 DBUG_RETURN(sts_mode);
11127}
11128
11129int spider_mysql_handler::show_table_status(
11130 int link_idx,
11131 int sts_mode,
11132 uint flag
11133) {
11134 int error_num;
11135 SPIDER_CONN *conn = spider->conns[link_idx];
11136 SPIDER_DB_RESULT *res;
11137 SPIDER_SHARE *share = spider->share;
11138 uint pos = (2 * spider->conn_link_idx[link_idx]);
11139 ulonglong auto_increment_value = 0;
11140 DBUG_ENTER("spider_mysql_handler::show_table_status");
11141 DBUG_PRINT("info",("spider sts_mode=%d", sts_mode));
11142
11143 if (sts_mode == 1)
11144 {
11145 pthread_mutex_lock(&conn->mta_conn_mutex);
11146 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
11147 conn->need_mon = &spider->need_mons[link_idx];
11148 conn->mta_conn_mutex_lock_already = TRUE;
11149 conn->mta_conn_mutex_unlock_later = TRUE;
11150 conn->disable_connect_retry = TRUE;
11151 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11152 share);
11153 if (
11154 (error_num = spider_db_set_names(spider, conn, link_idx)) ||
11155 (
11156 spider_db_query(
11157 conn,
11158 mysql_share->show_table_status[0 + pos].ptr(),
11159 mysql_share->show_table_status[0 + pos].length(),
11160 -1,
11161 &spider->need_mons[link_idx]) &&
11162 (error_num = spider_db_errorno(conn))
11163 )
11164 ) {
11165 if (
11166 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
11167 !conn->disable_reconnect
11168 ) {
11169 /* retry */
11170 if ((error_num = spider_db_ping(spider, conn, link_idx)))
11171 {
11172 conn->disable_connect_retry = FALSE;
11173 conn->mta_conn_mutex_lock_already = FALSE;
11174 conn->mta_conn_mutex_unlock_later = FALSE;
11175 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11176 pthread_mutex_unlock(&conn->mta_conn_mutex);
11177 DBUG_RETURN(error_num);
11178 }
11179 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
11180 {
11181 conn->disable_connect_retry = FALSE;
11182 conn->mta_conn_mutex_lock_already = FALSE;
11183 conn->mta_conn_mutex_unlock_later = FALSE;
11184 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11185 pthread_mutex_unlock(&conn->mta_conn_mutex);
11186 DBUG_RETURN(error_num);
11187 }
11188 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11189 share);
11190 if (spider_db_query(
11191 conn,
11192 mysql_share->show_table_status[0 + pos].ptr(),
11193 mysql_share->show_table_status[0 + pos].length(),
11194 -1,
11195 &spider->need_mons[link_idx])
11196 ) {
11197 conn->disable_connect_retry = FALSE;
11198 conn->mta_conn_mutex_lock_already = FALSE;
11199 conn->mta_conn_mutex_unlock_later = FALSE;
11200 DBUG_RETURN(spider_db_errorno(conn));
11201 }
11202 } else {
11203 conn->disable_connect_retry = FALSE;
11204 conn->mta_conn_mutex_lock_already = FALSE;
11205 conn->mta_conn_mutex_unlock_later = FALSE;
11206 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11207 pthread_mutex_unlock(&conn->mta_conn_mutex);
11208 DBUG_RETURN(error_num);
11209 }
11210 }
11211 st_spider_db_request_key request_key;
11212 request_key.spider_thread_id = spider->trx->spider_thread_id;
11213 request_key.query_id = spider->trx->thd->query_id;
11214 request_key.handler = spider;
11215 request_key.request_id = 1;
11216 request_key.next = NULL;
11217 if (spider_param_dry_access())
11218 {
11219 conn->disable_connect_retry = FALSE;
11220 conn->mta_conn_mutex_lock_already = FALSE;
11221 conn->mta_conn_mutex_unlock_later = FALSE;
11222 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11223 pthread_mutex_unlock(&conn->mta_conn_mutex);
11224 DBUG_RETURN(0);
11225 }
11226 if (!(res = conn->db_conn->store_result(NULL, &request_key, &error_num)))
11227 {
11228 conn->disable_connect_retry = FALSE;
11229 conn->mta_conn_mutex_lock_already = FALSE;
11230 conn->mta_conn_mutex_unlock_later = FALSE;
11231 if (error_num || (error_num = spider_db_errorno(conn)))
11232 DBUG_RETURN(error_num);
11233 else
11234 {
11235 my_printf_error(ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM,
11236 ER_SPIDER_REMOTE_TABLE_NOT_FOUND_STR, MYF(0),
11237 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].ptr(),
11238 mysql_share->table_names_str[spider->conn_link_idx[
11239 link_idx]].ptr());
11240 DBUG_RETURN(ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM);
11241 }
11242 }
11243 conn->disable_connect_retry = FALSE;
11244 conn->mta_conn_mutex_lock_already = FALSE;
11245 conn->mta_conn_mutex_unlock_later = FALSE;
11246 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11247 pthread_mutex_unlock(&conn->mta_conn_mutex);
11248 error_num = res->fetch_table_status(
11249 sts_mode,
11250 share->records,
11251 share->mean_rec_length,
11252 share->data_file_length,
11253 share->max_data_file_length,
11254 share->index_file_length,
11255 auto_increment_value,
11256 share->create_time,
11257 share->update_time,
11258 share->check_time
11259 );
11260 res->free_result();
11261 delete res;
11262 if (error_num)
11263 {
11264 switch (error_num)
11265 {
11266 case ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM:
11267 my_printf_error(ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM,
11268 ER_SPIDER_REMOTE_TABLE_NOT_FOUND_STR, MYF(0),
11269 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].ptr(),
11270 mysql_share->table_names_str[spider->conn_link_idx[
11271 link_idx]].ptr());
11272 break;
11273 case ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM:
11274 my_printf_error(ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM,
11275 ER_SPIDER_INVALID_REMOTE_TABLE_INFO_STR, MYF(0),
11276 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].ptr(),
11277 mysql_share->table_names_str[spider->conn_link_idx[
11278 link_idx]].ptr());
11279 break;
11280 default:
11281 break;
11282 }
11283 DBUG_RETURN(error_num);
11284 }
11285 } else {
11286 pthread_mutex_lock(&conn->mta_conn_mutex);
11287 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
11288 conn->need_mon = &spider->need_mons[link_idx];
11289 conn->mta_conn_mutex_lock_already = TRUE;
11290 conn->mta_conn_mutex_unlock_later = TRUE;
11291 conn->disable_connect_retry = TRUE;
11292 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11293 share);
11294 if (
11295 (error_num = spider_db_set_names(spider, conn, link_idx)) ||
11296 (
11297 spider_db_query(
11298 conn,
11299 mysql_share->show_table_status[1 + pos].ptr(),
11300 mysql_share->show_table_status[1 + pos].length(),
11301 -1,
11302 &spider->need_mons[link_idx]) &&
11303 (error_num = spider_db_errorno(conn))
11304 )
11305 ) {
11306 if (
11307 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
11308 !conn->disable_reconnect
11309 ) {
11310 /* retry */
11311 if ((error_num = spider_db_ping(spider, conn, link_idx)))
11312 {
11313 conn->disable_connect_retry = FALSE;
11314 conn->mta_conn_mutex_lock_already = FALSE;
11315 conn->mta_conn_mutex_unlock_later = FALSE;
11316 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11317 pthread_mutex_unlock(&conn->mta_conn_mutex);
11318 DBUG_RETURN(error_num);
11319 }
11320 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
11321 {
11322 conn->disable_connect_retry = FALSE;
11323 conn->mta_conn_mutex_lock_already = FALSE;
11324 conn->mta_conn_mutex_unlock_later = FALSE;
11325 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11326 pthread_mutex_unlock(&conn->mta_conn_mutex);
11327 DBUG_RETURN(error_num);
11328 }
11329 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11330 share);
11331 if (spider_db_query(
11332 conn,
11333 mysql_share->show_table_status[1 + pos].ptr(),
11334 mysql_share->show_table_status[1 + pos].length(),
11335 -1,
11336 &spider->need_mons[link_idx])
11337 ) {
11338 conn->disable_connect_retry = FALSE;
11339 conn->mta_conn_mutex_lock_already = FALSE;
11340 conn->mta_conn_mutex_unlock_later = FALSE;
11341 DBUG_RETURN(spider_db_errorno(conn));
11342 }
11343 } else {
11344 conn->disable_connect_retry = FALSE;
11345 conn->mta_conn_mutex_lock_already = FALSE;
11346 conn->mta_conn_mutex_unlock_later = FALSE;
11347 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11348 pthread_mutex_unlock(&conn->mta_conn_mutex);
11349 DBUG_RETURN(error_num);
11350 }
11351 }
11352 st_spider_db_request_key request_key;
11353 request_key.spider_thread_id = spider->trx->spider_thread_id;
11354 request_key.query_id = spider->trx->thd->query_id;
11355 request_key.handler = spider;
11356 request_key.request_id = 1;
11357 request_key.next = NULL;
11358 if (spider_param_dry_access())
11359 {
11360 conn->disable_connect_retry = FALSE;
11361 conn->mta_conn_mutex_lock_already = FALSE;
11362 conn->mta_conn_mutex_unlock_later = FALSE;
11363 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11364 pthread_mutex_unlock(&conn->mta_conn_mutex);
11365 DBUG_RETURN(0);
11366 }
11367 if (!(res = conn->db_conn->store_result(NULL, &request_key, &error_num)))
11368 {
11369 conn->disable_connect_retry = FALSE;
11370 conn->mta_conn_mutex_lock_already = FALSE;
11371 conn->mta_conn_mutex_unlock_later = FALSE;
11372 if (error_num || (error_num = spider_db_errorno(conn)))
11373 DBUG_RETURN(error_num);
11374 else
11375 DBUG_RETURN(ER_QUERY_ON_FOREIGN_DATA_SOURCE);
11376 }
11377 conn->disable_connect_retry = FALSE;
11378 conn->mta_conn_mutex_lock_already = FALSE;
11379 conn->mta_conn_mutex_unlock_later = FALSE;
11380 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11381 pthread_mutex_unlock(&conn->mta_conn_mutex);
11382 error_num = res->fetch_table_status(
11383 sts_mode,
11384 share->records,
11385 share->mean_rec_length,
11386 share->data_file_length,
11387 share->max_data_file_length,
11388 share->index_file_length,
11389 auto_increment_value,
11390 share->create_time,
11391 share->update_time,
11392 share->check_time
11393 );
11394 res->free_result();
11395 delete res;
11396 if (error_num)
11397 {
11398 switch (error_num)
11399 {
11400 case ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM:
11401 my_printf_error(ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM,
11402 ER_SPIDER_REMOTE_TABLE_NOT_FOUND_STR, MYF(0),
11403 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].ptr(),
11404 mysql_share->table_names_str[spider->conn_link_idx[
11405 link_idx]].ptr());
11406 break;
11407 case ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM:
11408 my_printf_error(ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM,
11409 ER_SPIDER_INVALID_REMOTE_TABLE_INFO_STR, MYF(0),
11410 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].ptr(),
11411 mysql_share->table_names_str[spider->conn_link_idx[
11412 link_idx]].ptr());
11413 break;
11414 default:
11415 break;
11416 }
11417 DBUG_RETURN(error_num);
11418 }
11419 }
11420 if (share->static_records_for_status != -1)
11421 {
11422 share->records = (ha_rows) share->static_records_for_status;
11423 }
11424 if (share->static_mean_rec_length != -1)
11425 {
11426 share->mean_rec_length = (ulong) share->static_mean_rec_length;
11427 }
11428 if (auto_increment_value > share->lgtm_tblhnd_share->auto_increment_value)
11429 {
11430 share->lgtm_tblhnd_share->auto_increment_value = auto_increment_value;
11431 DBUG_PRINT("info",("spider auto_increment_value=%llu",
11432 share->lgtm_tblhnd_share->auto_increment_value));
11433 }
11434
11435 DBUG_RETURN(0);
11436}
11437
11438int spider_mysql_handler::crd_mode_exchange(
11439 int crd_mode
11440) {
11441 DBUG_ENTER("spider_mysql_handler::crd_mode_exchange");
11442 DBUG_PRINT("info",("spider crd_mode=%d", crd_mode));
11443 DBUG_RETURN(crd_mode);
11444}
11445
11446int spider_mysql_handler::show_index(
11447 int link_idx,
11448 int crd_mode
11449) {
11450 int error_num;
11451 SPIDER_CONN *conn = spider->conns[link_idx];
11452 SPIDER_SHARE *share = spider->share;
11453 TABLE *table = spider->get_table();
11454 SPIDER_DB_RESULT *res;
11455 int roop_count;
11456 longlong *tmp_cardinality;
11457 uint pos = (2 * spider->conn_link_idx[link_idx]);
11458 DBUG_ENTER("spider_mysql_handler::show_index");
11459 DBUG_PRINT("info",("spider crd_mode=%d", crd_mode));
11460 if (crd_mode == 1)
11461 {
11462 pthread_mutex_lock(&conn->mta_conn_mutex);
11463 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
11464 conn->need_mon = &spider->need_mons[link_idx];
11465 conn->mta_conn_mutex_lock_already = TRUE;
11466 conn->mta_conn_mutex_unlock_later = TRUE;
11467 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11468 share);
11469 if (
11470 (error_num = spider_db_set_names(spider, conn, link_idx)) ||
11471 (
11472 spider_db_query(
11473 conn,
11474 mysql_share->show_index[0 + pos].ptr(),
11475 mysql_share->show_index[0 + pos].length(),
11476 -1,
11477 &spider->need_mons[link_idx]) &&
11478 (error_num = spider_db_errorno(conn))
11479 )
11480 ) {
11481 if (
11482 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
11483 !conn->disable_reconnect
11484 ) {
11485 /* retry */
11486 if ((error_num = spider_db_ping(spider, conn, link_idx)))
11487 {
11488 conn->mta_conn_mutex_lock_already = FALSE;
11489 conn->mta_conn_mutex_unlock_later = FALSE;
11490 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11491 pthread_mutex_unlock(&conn->mta_conn_mutex);
11492 DBUG_RETURN(error_num);
11493 }
11494 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
11495 {
11496 conn->mta_conn_mutex_lock_already = FALSE;
11497 conn->mta_conn_mutex_unlock_later = FALSE;
11498 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11499 pthread_mutex_unlock(&conn->mta_conn_mutex);
11500 DBUG_RETURN(error_num);
11501 }
11502 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11503 share);
11504 if (spider_db_query(
11505 conn,
11506 mysql_share->show_index[0 + pos].ptr(),
11507 mysql_share->show_index[0 + pos].length(),
11508 -1,
11509 &spider->need_mons[link_idx])
11510 ) {
11511 conn->mta_conn_mutex_lock_already = FALSE;
11512 conn->mta_conn_mutex_unlock_later = FALSE;
11513 DBUG_RETURN(spider_db_errorno(conn));
11514 }
11515 } else {
11516 conn->mta_conn_mutex_lock_already = FALSE;
11517 conn->mta_conn_mutex_unlock_later = FALSE;
11518 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11519 pthread_mutex_unlock(&conn->mta_conn_mutex);
11520 DBUG_RETURN(error_num);
11521 }
11522 }
11523 st_spider_db_request_key request_key;
11524 request_key.spider_thread_id = spider->trx->spider_thread_id;
11525 request_key.query_id = spider->trx->thd->query_id;
11526 request_key.handler = spider;
11527 request_key.request_id = 1;
11528 request_key.next = NULL;
11529 if (!(res = conn->db_conn->store_result(NULL, &request_key, &error_num)))
11530 {
11531 if (error_num || (error_num = spider_db_errorno(conn)))
11532 {
11533 conn->mta_conn_mutex_lock_already = FALSE;
11534 conn->mta_conn_mutex_unlock_later = FALSE;
11535 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11536 pthread_mutex_unlock(&conn->mta_conn_mutex);
11537 DBUG_RETURN(error_num);
11538 }
11539 /* no record is ok */
11540 }
11541 conn->mta_conn_mutex_lock_already = FALSE;
11542 conn->mta_conn_mutex_unlock_later = FALSE;
11543 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11544 pthread_mutex_unlock(&conn->mta_conn_mutex);
11545 if (res)
11546 {
11547 error_num = res->fetch_table_cardinality(
11548 crd_mode,
11549 table,
11550 share->cardinality,
11551 share->cardinality_upd,
11552 share->bitmap_size
11553 );
11554 }
11555 for (roop_count = 0, tmp_cardinality = share->cardinality;
11556 roop_count < (int) table->s->fields;
11557 roop_count++, tmp_cardinality++)
11558 {
11559 if (!spider_bit_is_set(share->cardinality_upd, roop_count))
11560 {
11561 DBUG_PRINT("info",
11562 ("spider uninitialized column cardinality id=%d", roop_count));
11563 *tmp_cardinality = -1;
11564 }
11565 }
11566 if (res)
11567 {
11568 res->free_result();
11569 delete res;
11570 }
11571 if (error_num)
11572 {
11573 switch (error_num)
11574 {
11575 case ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM:
11576 my_printf_error(ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM,
11577 ER_SPIDER_REMOTE_TABLE_NOT_FOUND_STR, MYF(0),
11578 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].ptr(),
11579 mysql_share->table_names_str[spider->conn_link_idx[
11580 link_idx]].ptr());
11581 break;
11582 case ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM:
11583 my_printf_error(ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM,
11584 ER_SPIDER_INVALID_REMOTE_TABLE_INFO_STR, MYF(0),
11585 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].ptr(),
11586 mysql_share->table_names_str[spider->conn_link_idx[
11587 link_idx]].ptr());
11588 break;
11589 default:
11590 break;
11591 }
11592 DBUG_RETURN(error_num);
11593 }
11594 } else {
11595 pthread_mutex_lock(&conn->mta_conn_mutex);
11596 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
11597 conn->need_mon = &spider->need_mons[link_idx];
11598 conn->mta_conn_mutex_lock_already = TRUE;
11599 conn->mta_conn_mutex_unlock_later = TRUE;
11600 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11601 share);
11602 if (
11603 (error_num = spider_db_set_names(spider, conn, link_idx)) ||
11604 (
11605 spider_db_query(
11606 conn,
11607 mysql_share->show_index[1 + pos].ptr(),
11608 mysql_share->show_index[1 + pos].length(),
11609 -1,
11610 &spider->need_mons[link_idx]) &&
11611 (error_num = spider_db_errorno(conn))
11612 )
11613 ) {
11614 if (
11615 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
11616 !conn->disable_reconnect
11617 ) {
11618 /* retry */
11619 if ((error_num = spider_db_ping(spider, conn, link_idx)))
11620 {
11621 conn->mta_conn_mutex_lock_already = FALSE;
11622 conn->mta_conn_mutex_unlock_later = FALSE;
11623 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11624 pthread_mutex_unlock(&conn->mta_conn_mutex);
11625 DBUG_RETURN(error_num);
11626 }
11627 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
11628 {
11629 conn->mta_conn_mutex_lock_already = FALSE;
11630 conn->mta_conn_mutex_unlock_later = FALSE;
11631 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11632 pthread_mutex_unlock(&conn->mta_conn_mutex);
11633 DBUG_RETURN(error_num);
11634 }
11635 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11636 share);
11637 if (spider_db_query(
11638 conn,
11639 mysql_share->show_index[1 + pos].ptr(),
11640 mysql_share->show_index[1 + pos].length(),
11641 -1,
11642 &spider->need_mons[link_idx])
11643 ) {
11644 conn->mta_conn_mutex_lock_already = FALSE;
11645 conn->mta_conn_mutex_unlock_later = FALSE;
11646 DBUG_RETURN(spider_db_errorno(conn));
11647 }
11648 } else {
11649 conn->mta_conn_mutex_lock_already = FALSE;
11650 conn->mta_conn_mutex_unlock_later = FALSE;
11651 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11652 pthread_mutex_unlock(&conn->mta_conn_mutex);
11653 DBUG_RETURN(error_num);
11654 }
11655 }
11656 st_spider_db_request_key request_key;
11657 request_key.spider_thread_id = spider->trx->spider_thread_id;
11658 request_key.query_id = spider->trx->thd->query_id;
11659 request_key.handler = spider;
11660 request_key.request_id = 1;
11661 request_key.next = NULL;
11662 if (!(res = conn->db_conn->store_result(NULL, &request_key, &error_num)))
11663 {
11664 if (error_num || (error_num = spider_db_errorno(conn)))
11665 {
11666 conn->mta_conn_mutex_lock_already = FALSE;
11667 conn->mta_conn_mutex_unlock_later = FALSE;
11668 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11669 pthread_mutex_unlock(&conn->mta_conn_mutex);
11670 DBUG_RETURN(error_num);
11671 }
11672 /* no record is ok */
11673 }
11674 conn->mta_conn_mutex_lock_already = FALSE;
11675 conn->mta_conn_mutex_unlock_later = FALSE;
11676 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11677 pthread_mutex_unlock(&conn->mta_conn_mutex);
11678 if (res)
11679 {
11680 error_num = res->fetch_table_cardinality(
11681 crd_mode,
11682 table,
11683 share->cardinality,
11684 share->cardinality_upd,
11685 share->bitmap_size
11686 );
11687 }
11688 for (roop_count = 0, tmp_cardinality = share->cardinality;
11689 roop_count < (int) table->s->fields;
11690 roop_count++, tmp_cardinality++)
11691 {
11692 if (!spider_bit_is_set(share->cardinality_upd, roop_count))
11693 {
11694 DBUG_PRINT("info",
11695 ("spider uninitialized column cardinality id=%d", roop_count));
11696 *tmp_cardinality = -1;
11697 }
11698 }
11699 if (res)
11700 {
11701 res->free_result();
11702 delete res;
11703 }
11704 if (error_num)
11705 {
11706 switch (error_num)
11707 {
11708 case ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM:
11709 my_printf_error(ER_SPIDER_REMOTE_TABLE_NOT_FOUND_NUM,
11710 ER_SPIDER_REMOTE_TABLE_NOT_FOUND_STR, MYF(0),
11711 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].ptr(),
11712 mysql_share->table_names_str[spider->conn_link_idx[
11713 link_idx]].ptr());
11714 break;
11715 case ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM:
11716 my_printf_error(ER_SPIDER_INVALID_REMOTE_TABLE_INFO_NUM,
11717 ER_SPIDER_INVALID_REMOTE_TABLE_INFO_STR, MYF(0),
11718 mysql_share->db_names_str[spider->conn_link_idx[link_idx]].ptr(),
11719 mysql_share->table_names_str[spider->conn_link_idx[
11720 link_idx]].ptr());
11721 break;
11722 default:
11723 break;
11724 }
11725 DBUG_RETURN(error_num);
11726 }
11727 }
11728 DBUG_RETURN(0);
11729}
11730
11731int spider_mysql_handler::show_records(
11732 int link_idx
11733) {
11734 int error_num;
11735 SPIDER_CONN *conn = spider->conns[link_idx];
11736 SPIDER_DB_RESULT *res;
11737 SPIDER_SHARE *share = spider->share;
11738 uint pos = spider->conn_link_idx[link_idx];
11739 DBUG_ENTER("spider_mysql_handler::show_records");
11740 pthread_mutex_lock(&conn->mta_conn_mutex);
11741 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
11742 conn->need_mon = &spider->need_mons[link_idx];
11743 conn->mta_conn_mutex_lock_already = TRUE;
11744 conn->mta_conn_mutex_unlock_later = TRUE;
11745 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11746 share);
11747 if (
11748 (error_num = spider_db_set_names(spider, conn, link_idx)) ||
11749 (
11750 spider_db_query(
11751 conn,
11752 mysql_share->show_records[pos].ptr(),
11753 mysql_share->show_records[pos].length(),
11754 -1,
11755 &spider->need_mons[link_idx]) &&
11756 (error_num = spider_db_errorno(conn))
11757 )
11758 ) {
11759 if (
11760 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
11761 !conn->disable_reconnect
11762 ) {
11763 /* retry */
11764 if ((error_num = spider_db_ping(spider, conn, link_idx)))
11765 {
11766 conn->mta_conn_mutex_lock_already = FALSE;
11767 conn->mta_conn_mutex_unlock_later = FALSE;
11768 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11769 pthread_mutex_unlock(&conn->mta_conn_mutex);
11770 DBUG_PRINT("info", ("spider error_num=%d 1", error_num));
11771 DBUG_RETURN(error_num);
11772 }
11773 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
11774 {
11775 conn->mta_conn_mutex_lock_already = FALSE;
11776 conn->mta_conn_mutex_unlock_later = FALSE;
11777 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11778 pthread_mutex_unlock(&conn->mta_conn_mutex);
11779 DBUG_PRINT("info", ("spider error_num=%d 2", error_num));
11780 DBUG_RETURN(error_num);
11781 }
11782 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11783 share);
11784 if (spider_db_query(
11785 conn,
11786 mysql_share->show_records[pos].ptr(),
11787 mysql_share->show_records[pos].length(),
11788 -1,
11789 &spider->need_mons[link_idx])
11790 ) {
11791 conn->mta_conn_mutex_lock_already = FALSE;
11792 conn->mta_conn_mutex_unlock_later = FALSE;
11793 DBUG_PRINT("info", ("spider error_num=%d 3", error_num));
11794 DBUG_RETURN(spider_db_errorno(conn));
11795 }
11796 } else {
11797 conn->mta_conn_mutex_lock_already = FALSE;
11798 conn->mta_conn_mutex_unlock_later = FALSE;
11799 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11800 pthread_mutex_unlock(&conn->mta_conn_mutex);
11801 DBUG_PRINT("info", ("spider error_num=%d 4", error_num));
11802 DBUG_RETURN(error_num);
11803 }
11804 }
11805 st_spider_db_request_key request_key;
11806 request_key.spider_thread_id = spider->trx->spider_thread_id;
11807 request_key.query_id = spider->trx->thd->query_id;
11808 request_key.handler = spider;
11809 request_key.request_id = 1;
11810 request_key.next = NULL;
11811 if (!(res = conn->db_conn->store_result(NULL, &request_key, &error_num)))
11812 {
11813 conn->mta_conn_mutex_lock_already = FALSE;
11814 conn->mta_conn_mutex_unlock_later = FALSE;
11815 if (error_num || (error_num = spider_db_errorno(conn)))
11816 {
11817 DBUG_PRINT("info", ("spider error_num=%d 5", error_num));
11818 DBUG_RETURN(error_num);
11819 } else {
11820 DBUG_PRINT("info", ("spider error_num=%d 6",
11821 ER_QUERY_ON_FOREIGN_DATA_SOURCE));
11822 DBUG_RETURN(ER_QUERY_ON_FOREIGN_DATA_SOURCE);
11823 }
11824 }
11825 conn->mta_conn_mutex_lock_already = FALSE;
11826 conn->mta_conn_mutex_unlock_later = FALSE;
11827 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11828 pthread_mutex_unlock(&conn->mta_conn_mutex);
11829 error_num = res->fetch_table_records(
11830 1,
11831 spider->table_rows
11832 );
11833 res->free_result();
11834 delete res;
11835 if (error_num)
11836 {
11837 DBUG_PRINT("info", ("spider error_num=%d 7", error_num));
11838 DBUG_RETURN(error_num);
11839 }
11840 spider->trx->direct_aggregate_count++;
11841 DBUG_RETURN(0);
11842}
11843
11844int spider_mysql_handler::show_last_insert_id(
11845 int link_idx,
11846 ulonglong &last_insert_id
11847) {
11848 SPIDER_CONN *conn = spider->conns[link_idx];
11849 DBUG_ENTER("spider_mysql_handler::show_last_insert_id");
11850 last_insert_id = conn->db_conn->last_insert_id();
11851 DBUG_RETURN(0);
11852}
11853
11854ha_rows spider_mysql_handler::explain_select(
11855 key_range *start_key,
11856 key_range *end_key,
11857 int link_idx
11858) {
11859 int error_num;
11860 SPIDER_CONN *conn = spider->conns[link_idx];
11861 SPIDER_RESULT_LIST *result_list = &spider->result_list;
11862 spider_string *str = &result_list->sqls[link_idx];
11863 SPIDER_DB_RESULT *res;
11864 ha_rows rows;
11865 spider_db_handler *dbton_hdl = spider->dbton_handler[conn->dbton_id];
11866 DBUG_ENTER("spider_mysql_handler::explain_select");
11867 if ((error_num = dbton_hdl->append_explain_select_part(
11868 start_key, end_key, SPIDER_SQL_TYPE_OTHER_SQL, link_idx)))
11869 {
11870 my_errno = error_num;
11871 DBUG_RETURN(HA_POS_ERROR);
11872 }
11873
11874 pthread_mutex_lock(&conn->mta_conn_mutex);
11875 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
11876 conn->need_mon = &spider->need_mons[link_idx];
11877 conn->mta_conn_mutex_lock_already = TRUE;
11878 conn->mta_conn_mutex_unlock_later = TRUE;
11879 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11880 spider->share);
11881 if (
11882 (error_num = spider_db_set_names(spider, conn, link_idx)) ||
11883 (
11884 spider_db_query(
11885 conn,
11886 str->ptr(),
11887 str->length(),
11888 -1,
11889 &spider->need_mons[link_idx]) &&
11890 (error_num = spider_db_errorno(conn))
11891 )
11892 ) {
11893 if (
11894 error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM &&
11895 !conn->disable_reconnect
11896 ) {
11897 /* retry */
11898 if ((error_num = spider_db_ping(spider, conn, link_idx)))
11899 {
11900 if (spider->check_error_mode(error_num))
11901 my_errno = error_num;
11902 conn->mta_conn_mutex_lock_already = FALSE;
11903 conn->mta_conn_mutex_unlock_later = FALSE;
11904 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11905 pthread_mutex_unlock(&conn->mta_conn_mutex);
11906 DBUG_RETURN(HA_POS_ERROR);
11907 }
11908 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
11909 {
11910 if (spider->check_error_mode(error_num))
11911 my_errno = error_num;
11912 conn->mta_conn_mutex_lock_already = FALSE;
11913 conn->mta_conn_mutex_unlock_later = FALSE;
11914 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11915 pthread_mutex_unlock(&conn->mta_conn_mutex);
11916 DBUG_RETURN(HA_POS_ERROR);
11917 }
11918 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
11919 spider->share);
11920 if (spider_db_query(
11921 conn,
11922 str->ptr(),
11923 str->length(),
11924 -1,
11925 &spider->need_mons[link_idx])
11926 ) {
11927 error_num = spider_db_errorno(conn);
11928 if (spider->check_error_mode(error_num))
11929 my_errno = error_num;
11930 conn->mta_conn_mutex_lock_already = FALSE;
11931 conn->mta_conn_mutex_unlock_later = FALSE;
11932 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11933 pthread_mutex_unlock(&conn->mta_conn_mutex);
11934 DBUG_RETURN(HA_POS_ERROR);
11935 }
11936 } else {
11937 if (spider->check_error_mode(error_num))
11938 my_errno = error_num;
11939 conn->mta_conn_mutex_lock_already = FALSE;
11940 conn->mta_conn_mutex_unlock_later = FALSE;
11941 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11942 pthread_mutex_unlock(&conn->mta_conn_mutex);
11943 DBUG_RETURN(HA_POS_ERROR);
11944 }
11945 }
11946 st_spider_db_request_key request_key;
11947 request_key.spider_thread_id = spider->trx->spider_thread_id;
11948 request_key.query_id = spider->trx->thd->query_id;
11949 request_key.handler = spider;
11950 request_key.request_id = 1;
11951 request_key.next = NULL;
11952 if (!(res = conn->db_conn->store_result(NULL, &request_key, &error_num)))
11953 {
11954 if (error_num || (error_num = spider_db_errorno(conn)))
11955 {
11956 if (spider->check_error_mode(error_num))
11957 my_errno = error_num;
11958 conn->mta_conn_mutex_lock_already = FALSE;
11959 conn->mta_conn_mutex_unlock_later = FALSE;
11960 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11961 pthread_mutex_unlock(&conn->mta_conn_mutex);
11962 DBUG_RETURN(HA_POS_ERROR);
11963 } else {
11964 my_errno = ER_QUERY_ON_FOREIGN_DATA_SOURCE;
11965 conn->mta_conn_mutex_lock_already = FALSE;
11966 conn->mta_conn_mutex_unlock_later = FALSE;
11967 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11968 pthread_mutex_unlock(&conn->mta_conn_mutex);
11969 DBUG_RETURN(HA_POS_ERROR);
11970 }
11971 }
11972 conn->mta_conn_mutex_lock_already = FALSE;
11973 conn->mta_conn_mutex_unlock_later = FALSE;
11974 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
11975 pthread_mutex_unlock(&conn->mta_conn_mutex);
11976 error_num = res->fetch_table_records(
11977 2,
11978 rows
11979 );
11980 res->free_result();
11981 delete res;
11982 if (error_num)
11983 {
11984 my_errno = error_num;
11985 DBUG_RETURN(HA_POS_ERROR);
11986 }
11987 DBUG_RETURN(rows);
11988}
11989
11990int spider_mysql_handler::lock_tables(
11991 int link_idx
11992) {
11993 int error_num;
11994 SPIDER_CONN *conn = spider->conns[link_idx];
11995 spider_string *str = &sql;
11996 DBUG_ENTER("spider_mysql_handler::lock_tables");
11997 str->length(0);
11998 if ((error_num = conn->db_conn->append_lock_tables(str)))
11999 {
12000 DBUG_RETURN(error_num);
12001 }
12002 if (str->length())
12003 {
12004 pthread_mutex_lock(&conn->mta_conn_mutex);
12005 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
12006 conn->need_mon = &spider->need_mons[link_idx];
12007 conn->mta_conn_mutex_lock_already = TRUE;
12008 conn->mta_conn_mutex_unlock_later = TRUE;
12009 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
12010 {
12011 conn->mta_conn_mutex_lock_already = FALSE;
12012 conn->mta_conn_mutex_unlock_later = FALSE;
12013 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12014 pthread_mutex_unlock(&conn->mta_conn_mutex);
12015 DBUG_RETURN(error_num);
12016 }
12017 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
12018 spider->share);
12019 if (spider_db_query(
12020 conn,
12021 str->ptr(),
12022 str->length(),
12023 -1,
12024 &spider->need_mons[link_idx])
12025 ) {
12026 conn->mta_conn_mutex_lock_already = FALSE;
12027 conn->mta_conn_mutex_unlock_later = FALSE;
12028 DBUG_RETURN(spider_db_errorno(conn));
12029 }
12030 conn->mta_conn_mutex_lock_already = FALSE;
12031 conn->mta_conn_mutex_unlock_later = FALSE;
12032 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12033 pthread_mutex_unlock(&conn->mta_conn_mutex);
12034 }
12035 if (!conn->table_locked)
12036 {
12037 conn->table_locked = TRUE;
12038 spider->trx->locked_connections++;
12039 }
12040 DBUG_RETURN(0);
12041}
12042
12043int spider_mysql_handler::unlock_tables(
12044 int link_idx
12045) {
12046 int error_num;
12047 SPIDER_CONN *conn = spider->conns[link_idx];
12048 DBUG_ENTER("spider_mysql_handler::unlock_tables");
12049 if (conn->table_locked)
12050 {
12051 spider_string *str = &sql;
12052 conn->table_locked = FALSE;
12053 spider->trx->locked_connections--;
12054
12055 str->length(0);
12056 if ((error_num = conn->db_conn->append_unlock_tables(str)))
12057 {
12058 DBUG_RETURN(error_num);
12059 }
12060 if (str->length())
12061 {
12062 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
12063 spider->share);
12064 if (spider_db_query(
12065 conn,
12066 str->ptr(),
12067 str->length(),
12068 -1,
12069 &spider->need_mons[link_idx])
12070 )
12071 DBUG_RETURN(spider_db_errorno(conn));
12072 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12073 pthread_mutex_unlock(&conn->mta_conn_mutex);
12074 }
12075 }
12076 DBUG_RETURN(0);
12077}
12078
12079int spider_mysql_handler::disable_keys(
12080 SPIDER_CONN *conn,
12081 int link_idx
12082) {
12083 int error_num;
12084 SPIDER_SHARE *share = spider->share;
12085 spider_string *str = &spider->result_list.sqls[link_idx];
12086 DBUG_ENTER("spider_mysql_handler::disable_keys");
12087 DBUG_PRINT("info",("spider this=%p", this));
12088 str->length(0);
12089 if ((error_num = append_disable_keys_part(SPIDER_SQL_TYPE_OTHER_HS,
12090 link_idx)))
12091 {
12092 DBUG_RETURN(error_num);
12093 }
12094 pthread_mutex_lock(&conn->mta_conn_mutex);
12095 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
12096 conn->need_mon = &spider->need_mons[link_idx];
12097 conn->mta_conn_mutex_lock_already = TRUE;
12098 conn->mta_conn_mutex_unlock_later = TRUE;
12099 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
12100 {
12101 conn->mta_conn_mutex_lock_already = FALSE;
12102 conn->mta_conn_mutex_unlock_later = FALSE;
12103 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12104 pthread_mutex_unlock(&conn->mta_conn_mutex);
12105 DBUG_RETURN(error_num);
12106 }
12107 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
12108 share);
12109 if (spider_db_query(
12110 conn,
12111 str->ptr(),
12112 str->length(),
12113 -1,
12114 &spider->need_mons[link_idx])
12115 ) {
12116 conn->mta_conn_mutex_lock_already = FALSE;
12117 conn->mta_conn_mutex_unlock_later = FALSE;
12118 error_num = spider_db_errorno(conn);
12119 DBUG_RETURN(error_num);
12120 }
12121 conn->mta_conn_mutex_lock_already = FALSE;
12122 conn->mta_conn_mutex_unlock_later = FALSE;
12123 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12124 pthread_mutex_unlock(&conn->mta_conn_mutex);
12125 DBUG_RETURN(0);
12126}
12127
12128int spider_mysql_handler::enable_keys(
12129 SPIDER_CONN *conn,
12130 int link_idx
12131) {
12132 int error_num;
12133 SPIDER_SHARE *share = spider->share;
12134 spider_string *str = &spider->result_list.sqls[link_idx];
12135 DBUG_ENTER("spider_mysql_handler::enable_keys");
12136 DBUG_PRINT("info",("spider this=%p", this));
12137 str->length(0);
12138 if ((error_num = append_enable_keys_part(SPIDER_SQL_TYPE_OTHER_HS,
12139 link_idx)))
12140 {
12141 DBUG_RETURN(error_num);
12142 }
12143 pthread_mutex_lock(&conn->mta_conn_mutex);
12144 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
12145 conn->need_mon = &spider->need_mons[link_idx];
12146 conn->mta_conn_mutex_lock_already = TRUE;
12147 conn->mta_conn_mutex_unlock_later = TRUE;
12148 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
12149 {
12150 conn->mta_conn_mutex_lock_already = FALSE;
12151 conn->mta_conn_mutex_unlock_later = FALSE;
12152 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12153 pthread_mutex_unlock(&conn->mta_conn_mutex);
12154 DBUG_RETURN(error_num);
12155 }
12156 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
12157 share);
12158 if (spider_db_query(
12159 conn,
12160 str->ptr(),
12161 str->length(),
12162 -1,
12163 &spider->need_mons[link_idx])
12164 ) {
12165 conn->mta_conn_mutex_lock_already = FALSE;
12166 conn->mta_conn_mutex_unlock_later = FALSE;
12167 error_num = spider_db_errorno(conn);
12168 DBUG_RETURN(error_num);
12169 }
12170 conn->mta_conn_mutex_lock_already = FALSE;
12171 conn->mta_conn_mutex_unlock_later = FALSE;
12172 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12173 pthread_mutex_unlock(&conn->mta_conn_mutex);
12174 DBUG_RETURN(0);
12175}
12176
12177int spider_mysql_handler::check_table(
12178 SPIDER_CONN *conn,
12179 int link_idx,
12180 HA_CHECK_OPT* check_opt
12181) {
12182 int error_num;
12183 SPIDER_SHARE *share = spider->share;
12184 spider_string *str = &spider->result_list.sqls[link_idx];
12185 DBUG_ENTER("spider_mysql_handler::check_table");
12186 DBUG_PRINT("info",("spider this=%p", this));
12187 str->length(0);
12188 if ((error_num = append_check_table_part(SPIDER_SQL_TYPE_OTHER_HS,
12189 link_idx, check_opt)))
12190 {
12191 DBUG_RETURN(error_num);
12192 }
12193 pthread_mutex_lock(&conn->mta_conn_mutex);
12194 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
12195 conn->need_mon = &spider->need_mons[link_idx];
12196 conn->mta_conn_mutex_lock_already = TRUE;
12197 conn->mta_conn_mutex_unlock_later = TRUE;
12198 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
12199 {
12200 conn->mta_conn_mutex_lock_already = FALSE;
12201 conn->mta_conn_mutex_unlock_later = FALSE;
12202 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12203 pthread_mutex_unlock(&conn->mta_conn_mutex);
12204 DBUG_RETURN(error_num);
12205 }
12206 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
12207 share);
12208 if (spider_db_query(
12209 conn,
12210 str->ptr(),
12211 str->length(),
12212 -1,
12213 &spider->need_mons[link_idx])
12214 ) {
12215 conn->mta_conn_mutex_lock_already = FALSE;
12216 conn->mta_conn_mutex_unlock_later = FALSE;
12217 error_num = spider_db_errorno(conn);
12218 DBUG_RETURN(error_num);
12219 }
12220 conn->mta_conn_mutex_lock_already = FALSE;
12221 conn->mta_conn_mutex_unlock_later = FALSE;
12222 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12223 pthread_mutex_unlock(&conn->mta_conn_mutex);
12224 DBUG_RETURN(0);
12225}
12226
12227int spider_mysql_handler::repair_table(
12228 SPIDER_CONN *conn,
12229 int link_idx,
12230 HA_CHECK_OPT* check_opt
12231) {
12232 int error_num;
12233 SPIDER_SHARE *share = spider->share;
12234 spider_string *str = &spider->result_list.sqls[link_idx];
12235 DBUG_ENTER("spider_mysql_handler::repair_table");
12236 DBUG_PRINT("info",("spider this=%p", this));
12237 str->length(0);
12238 if ((error_num = append_repair_table_part(SPIDER_SQL_TYPE_OTHER_HS,
12239 link_idx, check_opt)))
12240 {
12241 DBUG_RETURN(error_num);
12242 }
12243 pthread_mutex_lock(&conn->mta_conn_mutex);
12244 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
12245 conn->need_mon = &spider->need_mons[link_idx];
12246 conn->mta_conn_mutex_lock_already = TRUE;
12247 conn->mta_conn_mutex_unlock_later = TRUE;
12248 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
12249 {
12250 conn->mta_conn_mutex_lock_already = FALSE;
12251 conn->mta_conn_mutex_unlock_later = FALSE;
12252 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12253 pthread_mutex_unlock(&conn->mta_conn_mutex);
12254 DBUG_RETURN(error_num);
12255 }
12256 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
12257 share);
12258 if (spider_db_query(
12259 conn,
12260 str->ptr(),
12261 str->length(),
12262 -1,
12263 &spider->need_mons[link_idx])
12264 ) {
12265 conn->mta_conn_mutex_lock_already = FALSE;
12266 conn->mta_conn_mutex_unlock_later = FALSE;
12267 error_num = spider_db_errorno(conn);
12268 DBUG_RETURN(error_num);
12269 }
12270 conn->mta_conn_mutex_lock_already = FALSE;
12271 conn->mta_conn_mutex_unlock_later = FALSE;
12272 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12273 pthread_mutex_unlock(&conn->mta_conn_mutex);
12274 DBUG_RETURN(0);
12275}
12276
12277int spider_mysql_handler::analyze_table(
12278 SPIDER_CONN *conn,
12279 int link_idx
12280) {
12281 int error_num;
12282 SPIDER_SHARE *share = spider->share;
12283 spider_string *str = &spider->result_list.sqls[link_idx];
12284 DBUG_ENTER("spider_mysql_handler::analyze_table");
12285 DBUG_PRINT("info",("spider this=%p", this));
12286 str->length(0);
12287 if ((error_num = append_analyze_table_part(SPIDER_SQL_TYPE_OTHER_HS,
12288 link_idx)))
12289 {
12290 DBUG_RETURN(error_num);
12291 }
12292 pthread_mutex_lock(&conn->mta_conn_mutex);
12293 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
12294 conn->need_mon = &spider->need_mons[link_idx];
12295 conn->mta_conn_mutex_lock_already = TRUE;
12296 conn->mta_conn_mutex_unlock_later = TRUE;
12297 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
12298 {
12299 conn->mta_conn_mutex_lock_already = FALSE;
12300 conn->mta_conn_mutex_unlock_later = FALSE;
12301 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12302 pthread_mutex_unlock(&conn->mta_conn_mutex);
12303 DBUG_RETURN(error_num);
12304 }
12305 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
12306 share);
12307 if (spider_db_query(
12308 conn,
12309 str->ptr(),
12310 str->length(),
12311 -1,
12312 &spider->need_mons[link_idx])
12313 ) {
12314 conn->mta_conn_mutex_lock_already = FALSE;
12315 conn->mta_conn_mutex_unlock_later = FALSE;
12316 error_num = spider_db_errorno(conn);
12317 DBUG_RETURN(error_num);
12318 }
12319 conn->mta_conn_mutex_lock_already = FALSE;
12320 conn->mta_conn_mutex_unlock_later = FALSE;
12321 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12322 pthread_mutex_unlock(&conn->mta_conn_mutex);
12323 DBUG_RETURN(0);
12324}
12325
12326int spider_mysql_handler::optimize_table(
12327 SPIDER_CONN *conn,
12328 int link_idx
12329) {
12330 int error_num;
12331 SPIDER_SHARE *share = spider->share;
12332 spider_string *str = &spider->result_list.sqls[link_idx];
12333 DBUG_ENTER("spider_mysql_handler::optimize_table");
12334 DBUG_PRINT("info",("spider this=%p", this));
12335 str->length(0);
12336 if ((error_num = append_optimize_table_part(SPIDER_SQL_TYPE_OTHER_HS,
12337 link_idx)))
12338 {
12339 DBUG_RETURN(error_num);
12340 }
12341 pthread_mutex_lock(&conn->mta_conn_mutex);
12342 SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
12343 conn->need_mon = &spider->need_mons[link_idx];
12344 conn->mta_conn_mutex_lock_already = TRUE;
12345 conn->mta_conn_mutex_unlock_later = TRUE;
12346 if ((error_num = spider_db_set_names(spider, conn, link_idx)))
12347 {
12348 conn->mta_conn_mutex_lock_already = FALSE;
12349 conn->mta_conn_mutex_unlock_later = FALSE;
12350 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12351 pthread_mutex_unlock(&conn->mta_conn_mutex);
12352 DBUG_RETURN(error_num);
12353 }
12354 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
12355 share);
12356 if (spider_db_query(
12357 conn,
12358 str->ptr(),
12359 str->length(),
12360 -1,
12361 &spider->need_mons[link_idx])
12362 ) {
12363 conn->mta_conn_mutex_lock_already = FALSE;
12364 conn->mta_conn_mutex_unlock_later = FALSE;
12365 error_num = spider_db_errorno(conn);
12366 DBUG_RETURN(error_num);
12367 }
12368 conn->mta_conn_mutex_lock_already = FALSE;
12369 conn->mta_conn_mutex_unlock_later = FALSE;
12370 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12371 pthread_mutex_unlock(&conn->mta_conn_mutex);
12372 DBUG_RETURN(0);
12373}
12374
12375int spider_mysql_handler::flush_tables(
12376 SPIDER_CONN *conn,
12377 int link_idx,
12378 bool lock
12379) {
12380 int error_num;
12381 SPIDER_SHARE *share = spider->share;
12382 spider_string *str = &spider->result_list.sqls[link_idx];
12383 DBUG_ENTER("spider_mysql_handler::flush_tables");
12384 DBUG_PRINT("info",("spider this=%p", this));
12385 str->length(0);
12386 if ((error_num = append_flush_tables_part(SPIDER_SQL_TYPE_OTHER_HS,
12387 link_idx, lock)))
12388 {
12389 DBUG_RETURN(error_num);
12390 }
12391 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
12392 share);
12393 if (spider_db_query(
12394 conn,
12395 str->ptr(),
12396 str->length(),
12397 -1,
12398 &spider->need_mons[link_idx])
12399 ) {
12400 error_num = spider_db_errorno(conn);
12401 DBUG_RETURN(error_num);
12402 }
12403 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12404 pthread_mutex_unlock(&conn->mta_conn_mutex);
12405 DBUG_RETURN(0);
12406}
12407
12408int spider_mysql_handler::flush_logs(
12409 SPIDER_CONN *conn,
12410 int link_idx
12411) {
12412 int error_num;
12413 SPIDER_SHARE *share = spider->share;
12414 DBUG_ENTER("spider_mysql_handler::flush_logs");
12415 DBUG_PRINT("info",("spider this=%p", this));
12416 spider_conn_set_timeout_from_share(conn, link_idx, spider->trx->thd,
12417 share);
12418 if (spider_db_query(
12419 conn,
12420 SPIDER_SQL_FLUSH_LOGS_STR,
12421 SPIDER_SQL_FLUSH_LOGS_LEN,
12422 -1,
12423 &spider->need_mons[link_idx])
12424 ) {
12425 error_num = spider_db_errorno(conn);
12426 DBUG_RETURN(error_num);
12427 }
12428 SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
12429 pthread_mutex_unlock(&conn->mta_conn_mutex);
12430 DBUG_RETURN(0);
12431}
12432
12433int spider_mysql_handler::insert_opened_handler(
12434 SPIDER_CONN *conn,
12435 int link_idx
12436) {
12437 spider_db_mysql *db_conn = (spider_db_mysql *) conn->db_conn;
12438 SPIDER_LINK_FOR_HASH *tmp_link_for_hash = &link_for_hash[link_idx];
12439 DBUG_ASSERT(tmp_link_for_hash->spider == spider);
12440 DBUG_ASSERT(tmp_link_for_hash->link_idx == link_idx);
12441 uint old_elements = db_conn->handler_open_array.max_element;
12442 DBUG_ENTER("spider_mysql_handler::insert_opened_handler");
12443 DBUG_PRINT("info",("spider this=%p", this));
12444 if (insert_dynamic(&db_conn->handler_open_array,
12445 (uchar*) &tmp_link_for_hash))
12446 {
12447 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
12448 }
12449 if (db_conn->handler_open_array.max_element > old_elements)
12450 {
12451 spider_alloc_calc_mem(spider_current_trx,
12452 db_conn->handler_open_array,
12453 (db_conn->handler_open_array.max_element - old_elements) *
12454 db_conn->handler_open_array.size_of_element);
12455 }
12456 DBUG_RETURN(0);
12457}
12458
12459int spider_mysql_handler::delete_opened_handler(
12460 SPIDER_CONN *conn,
12461 int link_idx
12462) {
12463 spider_db_mysql *db_conn = (spider_db_mysql *) conn->db_conn;
12464 uint roop_count, elements = db_conn->handler_open_array.elements;
12465 SPIDER_LINK_FOR_HASH *tmp_link_for_hash;
12466 DBUG_ENTER("spider_mysql_handler::delete_opened_handler");
12467 DBUG_PRINT("info",("spider this=%p", this));
12468 for (roop_count = 0; roop_count < elements; roop_count++)
12469 {
12470 get_dynamic(&db_conn->handler_open_array, (uchar *) &tmp_link_for_hash,
12471 roop_count);
12472 if (tmp_link_for_hash == &link_for_hash[link_idx])
12473 {
12474 delete_dynamic_element(&db_conn->handler_open_array, roop_count);
12475 break;
12476 }
12477 }
12478 DBUG_ASSERT(roop_count < elements);
12479 DBUG_RETURN(0);
12480}
12481
12482int spider_mysql_handler::sync_from_clone_source(
12483 spider_db_handler *dbton_hdl
12484) {
12485 DBUG_ENTER("spider_mysql_handler::sync_from_clone_source");
12486 DBUG_PRINT("info",("spider this=%p", this));
12487 DBUG_RETURN(0);
12488}
12489
12490bool spider_mysql_handler::support_use_handler(
12491 int use_handler
12492) {
12493 DBUG_ENTER("spider_mysql_handler::support_use_handler");
12494 DBUG_PRINT("info",("spider this=%p", this));
12495 DBUG_RETURN(TRUE);
12496}
12497
12498void spider_mysql_handler::minimum_select_bitmap_create()
12499{
12500 TABLE *table = spider->get_table();
12501 Field **field_p;
12502 DBUG_ENTER("spider_mysql_handler::minimum_select_bitmap_create");
12503 DBUG_PRINT("info",("spider this=%p", this));
12504 memset(minimum_select_bitmap, 0, no_bytes_in_map(table->read_set));
12505 if (
12506 spider->use_index_merge ||
12507#ifdef HA_CAN_BULK_ACCESS
12508 (spider->is_clone && !spider->is_bulk_access_clone)
12509#else
12510 spider->is_clone
12511#endif
12512 ) {
12513 /* need preparing for cmp_ref */
12514 TABLE_SHARE *table_share = table->s;
12515 if (
12516 table_share->primary_key == MAX_KEY
12517 ) {
12518 /* need all columns */
12519 memset(minimum_select_bitmap, 0xFF, no_bytes_in_map(table->read_set));
12520 DBUG_VOID_RETURN;
12521 } else {
12522 /* need primary key columns */
12523 uint roop_count;
12524 KEY *key_info;
12525 KEY_PART_INFO *key_part;
12526 Field *field;
12527 key_info = &table_share->key_info[table_share->primary_key];
12528 key_part = key_info->key_part;
12529 for (roop_count = 0;
12530 roop_count < spider_user_defined_key_parts(key_info);
12531 roop_count++)
12532 {
12533 field = key_part[roop_count].field;
12534 spider_set_bit(minimum_select_bitmap, field->field_index);
12535 }
12536 }
12537 }
12538 DBUG_PRINT("info",("spider searched_bitmap=%p", spider->searched_bitmap));
12539 for (field_p = table->field; *field_p; field_p++)
12540 {
12541 uint field_index = (*field_p)->field_index;
12542 DBUG_PRINT("info",("spider field_index=%u", field_index));
12543 DBUG_PRINT("info",("spider ft_discard_bitmap=%s",
12544 spider_bit_is_set(spider->ft_discard_bitmap, field_index) ?
12545 "TRUE" : "FALSE"));
12546 DBUG_PRINT("info",("spider searched_bitmap=%s",
12547 spider_bit_is_set(spider->searched_bitmap, field_index) ?
12548 "TRUE" : "FALSE"));
12549 DBUG_PRINT("info",("spider read_set=%s",
12550 bitmap_is_set(table->read_set, field_index) ?
12551 "TRUE" : "FALSE"));
12552 DBUG_PRINT("info",("spider write_set=%s",
12553 bitmap_is_set(table->write_set, field_index) ?
12554 "TRUE" : "FALSE"));
12555 if (
12556 spider_bit_is_set(spider->ft_discard_bitmap, field_index) &
12557 (
12558 spider_bit_is_set(spider->searched_bitmap, field_index) |
12559 bitmap_is_set(table->read_set, field_index) |
12560 bitmap_is_set(table->write_set, field_index)
12561 )
12562 ) {
12563 spider_set_bit(minimum_select_bitmap, field_index);
12564 }
12565 }
12566 DBUG_VOID_RETURN;
12567}
12568
12569bool spider_mysql_handler::minimum_select_bit_is_set(
12570 uint field_index
12571) {
12572 DBUG_ENTER("spider_mysql_handler::minimum_select_bit_is_set");
12573 DBUG_PRINT("info",("spider this=%p", this));
12574 DBUG_PRINT("info",("spider field_index=%u", field_index));
12575 DBUG_PRINT("info",("spider minimum_select_bitmap=%s",
12576 spider_bit_is_set(minimum_select_bitmap, field_index) ?
12577 "TRUE" : "FALSE"));
12578 DBUG_RETURN(spider_bit_is_set(minimum_select_bitmap, field_index));
12579}
12580
12581void spider_mysql_handler::copy_minimum_select_bitmap(
12582 uchar *bitmap
12583) {
12584 int roop_count;
12585 TABLE *table = spider->get_table();
12586 DBUG_ENTER("spider_mysql_handler::copy_minimum_select_bitmap");
12587 for (roop_count = 0;
12588 roop_count < (int) ((table->s->fields + 7) / 8);
12589 roop_count++)
12590 {
12591 bitmap[roop_count] =
12592 minimum_select_bitmap[roop_count];
12593 DBUG_PRINT("info",("spider roop_count=%d", roop_count));
12594 DBUG_PRINT("info",("spider bitmap=%d",
12595 bitmap[roop_count]));
12596 }
12597 DBUG_VOID_RETURN;
12598}
12599
12600int spider_mysql_handler::init_union_table_name_pos()
12601{
12602 DBUG_ENTER("spider_mysql_handler::init_union_table_name_pos");
12603 DBUG_PRINT("info",("spider this=%p", this));
12604 if (!union_table_name_pos_first)
12605 {
12606 if (!spider_bulk_malloc(spider_current_trx, 236, MYF(MY_WME),
12607 &union_table_name_pos_first, sizeof(SPIDER_INT_HLD),
12608 NullS)
12609 ) {
12610 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
12611 }
12612 union_table_name_pos_first->next = NULL;
12613 }
12614 union_table_name_pos_current = union_table_name_pos_first;
12615 union_table_name_pos_current->tgt_num = 0;
12616 DBUG_RETURN(0);
12617}
12618
12619int spider_mysql_handler::set_union_table_name_pos()
12620{
12621 DBUG_ENTER("spider_mysql_handler::set_union_table_name_pos");
12622 DBUG_PRINT("info",("spider this=%p", this));
12623 if (union_table_name_pos_current->tgt_num >= SPIDER_INT_HLD_TGT_SIZE)
12624 {
12625 if (!union_table_name_pos_current->next)
12626 {
12627 if (!spider_bulk_malloc(spider_current_trx, 237, MYF(MY_WME),
12628 &union_table_name_pos_current->next, sizeof(SPIDER_INT_HLD),
12629 NullS)
12630 ) {
12631 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
12632 }
12633 union_table_name_pos_current->next->next = NULL;
12634 }
12635 union_table_name_pos_current = union_table_name_pos_current->next;
12636 union_table_name_pos_current->tgt_num = 0;
12637 }
12638 union_table_name_pos_current->tgt[union_table_name_pos_current->tgt_num] =
12639 table_name_pos;
12640 ++union_table_name_pos_current->tgt_num;
12641 DBUG_RETURN(0);
12642}
12643
12644int spider_mysql_handler::reset_union_table_name(
12645 spider_string *str,
12646 int link_idx,
12647 ulong sql_type
12648) {
12649 DBUG_ENTER("spider_mysql_handler::reset_union_table_name");
12650 DBUG_PRINT("info",("spider this=%p", this));
12651 if (!union_table_name_pos_current)
12652 DBUG_RETURN(0);
12653
12654 SPIDER_INT_HLD *tmp_pos = union_table_name_pos_first;
12655 uint cur_num, pos_backup = str->length();
12656 while(TRUE)
12657 {
12658 for (cur_num = 0; cur_num < tmp_pos->tgt_num; ++cur_num)
12659 {
12660 str->length(tmp_pos->tgt[cur_num]);
12661 append_table_name_with_adjusting(str, link_idx, sql_type);
12662 }
12663 if (tmp_pos == union_table_name_pos_current)
12664 break;
12665 tmp_pos = tmp_pos->next;
12666 }
12667 str->length(pos_backup);
12668 DBUG_RETURN(0);
12669}
12670
12671#ifdef SPIDER_HAS_GROUP_BY_HANDLER
12672int spider_mysql_handler::append_from_and_tables_part(
12673 spider_fields *fields,
12674 ulong sql_type
12675) {
12676 int error_num;
12677 spider_string *str;
12678 DBUG_ENTER("spider_mysql_handler::append_from_and_tables_part");
12679 DBUG_PRINT("info",("spider this=%p", this));
12680 switch (sql_type)
12681 {
12682 case SPIDER_SQL_TYPE_SELECT_SQL:
12683 str = &sql;
12684 break;
12685 default:
12686 DBUG_RETURN(0);
12687 }
12688 error_num = spider_db_mysql_utility.append_from_and_tables(fields, str);
12689 DBUG_RETURN(error_num);
12690}
12691
12692int spider_mysql_handler::reappend_tables_part(
12693 spider_fields *fields,
12694 ulong sql_type
12695) {
12696 int error_num;
12697 spider_string *str;
12698 DBUG_ENTER("spider_mysql_handler::reappend_tables_part");
12699 DBUG_PRINT("info",("spider this=%p", this));
12700 switch (sql_type)
12701 {
12702 case SPIDER_SQL_TYPE_SELECT_SQL:
12703 str = &sql;
12704 break;
12705 default:
12706 DBUG_RETURN(0);
12707 }
12708 error_num = spider_db_mysql_utility.reappend_tables(fields,
12709 link_idx_chain, str);
12710 DBUG_RETURN(error_num);
12711}
12712
12713int spider_mysql_handler::append_where_part(
12714 ulong sql_type
12715) {
12716 int error_num;
12717 spider_string *str;
12718 DBUG_ENTER("spider_mysql_handler::append_where_part");
12719 DBUG_PRINT("info",("spider this=%p", this));
12720 switch (sql_type)
12721 {
12722 case SPIDER_SQL_TYPE_SELECT_SQL:
12723 str = &sql;
12724 break;
12725 default:
12726 DBUG_RETURN(0);
12727 }
12728 error_num = spider_db_mysql_utility.append_where(str);
12729 DBUG_RETURN(error_num);
12730}
12731
12732int spider_mysql_handler::append_having_part(
12733 ulong sql_type
12734) {
12735 int error_num;
12736 spider_string *str;
12737 DBUG_ENTER("spider_mysql_handler::append_having_part");
12738 DBUG_PRINT("info",("spider this=%p", this));
12739 switch (sql_type)
12740 {
12741 case SPIDER_SQL_TYPE_SELECT_SQL:
12742 str = &sql;
12743 break;
12744 default:
12745 DBUG_RETURN(0);
12746 }
12747 error_num = spider_db_mysql_utility.append_having(str);
12748 DBUG_RETURN(error_num);
12749}
12750
12751int spider_mysql_handler::append_item_type_part(
12752 Item *item,
12753 const char *alias,
12754 uint alias_length,
12755 bool use_fields,
12756 spider_fields *fields,
12757 ulong sql_type
12758) {
12759 int error_num;
12760 spider_string *str;
12761 DBUG_ENTER("spider_mysql_handler::append_item_type_part");
12762 DBUG_PRINT("info",("spider this=%p", this));
12763 switch (sql_type)
12764 {
12765 case SPIDER_SQL_TYPE_SELECT_SQL:
12766 str = &sql;
12767 break;
12768 default:
12769 DBUG_RETURN(0);
12770 }
12771 error_num = spider_db_print_item_type(item, spider, str, alias, alias_length,
12772 spider_dbton_mysql.dbton_id, use_fields, fields);
12773 DBUG_RETURN(error_num);
12774}
12775
12776int spider_mysql_handler::append_list_item_select_part(
12777 List<Item> *select,
12778 const char *alias,
12779 uint alias_length,
12780 bool use_fields,
12781 spider_fields *fields,
12782 ulong sql_type
12783) {
12784 int error_num;
12785 spider_string *str;
12786 DBUG_ENTER("spider_mysql_handler::append_list_item_select_part");
12787 DBUG_PRINT("info",("spider this=%p", this));
12788 switch (sql_type)
12789 {
12790 case SPIDER_SQL_TYPE_SELECT_SQL:
12791 str = &sql;
12792 break;
12793 default:
12794 DBUG_RETURN(0);
12795 }
12796 error_num = append_list_item_select(select, str, alias, alias_length,
12797 use_fields, fields);
12798 DBUG_RETURN(error_num);
12799}
12800
12801int spider_mysql_handler::append_list_item_select(
12802 List<Item> *select,
12803 spider_string *str,
12804 const char *alias,
12805 uint alias_length,
12806 bool use_fields,
12807 spider_fields *fields
12808) {
12809 int error_num;
12810 uint dbton_id = spider_dbton_mysql.dbton_id, length;
12811 List_iterator_fast<Item> it(*select);
12812 Item *item;
12813 Field **field_ptr;
12814 DBUG_ENTER("spider_mysql_handler::append_list_item_select");
12815 DBUG_PRINT("info",("spider this=%p", this));
12816 while ((item = it++))
12817 {
12818 if ((error_num = spider_db_print_item_type(item, spider, str,
12819 alias, alias_length, dbton_id, use_fields, fields)))
12820 {
12821 DBUG_RETURN(error_num);
12822 }
12823 field_ptr = fields->get_next_field_ptr();
12824 length = (*field_ptr)->field_name.length;
12825 if (str->reserve(
12826 SPIDER_SQL_COMMA_LEN + /* SPIDER_SQL_NAME_QUOTE_LEN */ 2 +
12827 SPIDER_SQL_SPACE_LEN + length
12828 ))
12829 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
12830 str->q_append(SPIDER_SQL_SPACE_STR, SPIDER_SQL_SPACE_LEN);
12831 if ((error_num = spider_db_mysql_utility.append_name(str,
12832 (*field_ptr)->field_name.str, length)))
12833 {
12834 DBUG_RETURN(error_num);
12835 }
12836 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
12837 }
12838 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
12839 DBUG_RETURN(0);
12840}
12841
12842int spider_mysql_handler::append_group_by_part(
12843 ORDER *order,
12844 const char *alias,
12845 uint alias_length,
12846 bool use_fields,
12847 spider_fields *fields,
12848 ulong sql_type
12849) {
12850 int error_num;
12851 spider_string *str;
12852 DBUG_ENTER("spider_mysql_handler::append_group_by_part");
12853 DBUG_PRINT("info",("spider this=%p", this));
12854 switch (sql_type)
12855 {
12856 case SPIDER_SQL_TYPE_SELECT_SQL:
12857 str = &sql;
12858 break;
12859 default:
12860 DBUG_RETURN(0);
12861 }
12862 error_num = append_group_by(order, str, alias, alias_length,
12863 use_fields, fields);
12864 DBUG_RETURN(error_num);
12865}
12866
12867int spider_mysql_handler::append_group_by(
12868 ORDER *order,
12869 spider_string *str,
12870 const char *alias,
12871 uint alias_length,
12872 bool use_fields,
12873 spider_fields *fields
12874) {
12875 int error_num;
12876 uint dbton_id = spider_dbton_mysql.dbton_id;
12877 DBUG_ENTER("spider_mysql_handler::append_group_by");
12878 DBUG_PRINT("info",("spider this=%p", this));
12879 if (order)
12880 {
12881 if (str->reserve(SPIDER_SQL_GROUP_LEN))
12882 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
12883 str->q_append(SPIDER_SQL_GROUP_STR, SPIDER_SQL_GROUP_LEN);
12884 for (; order; order = order->next)
12885 {
12886 if ((error_num = spider_db_print_item_type((*order->item), spider, str,
12887 alias, alias_length, dbton_id, use_fields, fields)))
12888 {
12889 DBUG_RETURN(error_num);
12890 }
12891 if (str->reserve(SPIDER_SQL_COMMA_LEN))
12892 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
12893 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
12894 }
12895 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
12896 }
12897 DBUG_RETURN(0);
12898}
12899
12900int spider_mysql_handler::append_order_by_part(
12901 ORDER *order,
12902 const char *alias,
12903 uint alias_length,
12904 bool use_fields,
12905 spider_fields *fields,
12906 ulong sql_type
12907) {
12908 int error_num;
12909 spider_string *str;
12910 DBUG_ENTER("spider_mysql_handler::append_order_by_part");
12911 DBUG_PRINT("info",("spider this=%p", this));
12912 switch (sql_type)
12913 {
12914 case SPIDER_SQL_TYPE_SELECT_SQL:
12915 str = &sql;
12916 break;
12917 default:
12918 DBUG_RETURN(0);
12919 }
12920 error_num = append_order_by(order, str, alias, alias_length,
12921 use_fields, fields);
12922 DBUG_RETURN(error_num);
12923}
12924
12925int spider_mysql_handler::append_order_by(
12926 ORDER *order,
12927 spider_string *str,
12928 const char *alias,
12929 uint alias_length,
12930 bool use_fields,
12931 spider_fields *fields
12932) {
12933 int error_num;
12934 uint dbton_id = spider_dbton_mysql.dbton_id;
12935 DBUG_ENTER("spider_mysql_handler::append_order_by");
12936 DBUG_PRINT("info",("spider this=%p", this));
12937 if (order)
12938 {
12939 if (str->reserve(SPIDER_SQL_ORDER_LEN))
12940 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
12941 str->q_append(SPIDER_SQL_ORDER_STR, SPIDER_SQL_ORDER_LEN);
12942 for (; order; order = order->next)
12943 {
12944 if ((error_num = spider_db_print_item_type((*order->item), spider, str,
12945 alias, alias_length, dbton_id, use_fields, fields)))
12946 {
12947 DBUG_RETURN(error_num);
12948 }
12949 if (SPIDER_order_direction_is_asc(order))
12950 {
12951 if (str->reserve(SPIDER_SQL_COMMA_LEN))
12952 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
12953 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
12954 } else {
12955 if (str->reserve(SPIDER_SQL_COMMA_LEN + SPIDER_SQL_DESC_LEN))
12956 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
12957 str->q_append(SPIDER_SQL_DESC_STR, SPIDER_SQL_DESC_LEN);
12958 str->q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
12959 }
12960 }
12961 str->length(str->length() - SPIDER_SQL_COMMA_LEN);
12962 }
12963 DBUG_RETURN(0);
12964}
12965#endif
12966
12967spider_mysql_copy_table::spider_mysql_copy_table(
12968 spider_mysql_share *db_share
12969) : spider_db_copy_table(
12970 db_share
12971),
12972 mysql_share(db_share)
12973{
12974 DBUG_ENTER("spider_mysql_copy_table::spider_mysql_copy_table");
12975 DBUG_PRINT("info",("spider this=%p", this));
12976 DBUG_VOID_RETURN;
12977}
12978
12979spider_mysql_copy_table::~spider_mysql_copy_table()
12980{
12981 DBUG_ENTER("spider_mysql_copy_table::~spider_mysql_copy_table");
12982 DBUG_PRINT("info",("spider this=%p", this));
12983 DBUG_VOID_RETURN;
12984}
12985
12986int spider_mysql_copy_table::init()
12987{
12988 DBUG_ENTER("spider_mysql_copy_table::init");
12989 DBUG_PRINT("info",("spider this=%p", this));
12990 sql.init_calc_mem(78);
12991 DBUG_RETURN(0);
12992}
12993
12994void spider_mysql_copy_table::set_sql_charset(
12995 CHARSET_INFO *cs
12996) {
12997 DBUG_ENTER("spider_mysql_copy_table::set_sql_charset");
12998 DBUG_PRINT("info",("spider this=%p", this));
12999 sql.set_charset(cs);
13000 DBUG_VOID_RETURN;
13001}
13002
13003int spider_mysql_copy_table::append_select_str()
13004{
13005 DBUG_ENTER("spider_mysql_copy_table::append_select_str");
13006 DBUG_PRINT("info",("spider this=%p", this));
13007 if (sql.reserve(SPIDER_SQL_SELECT_LEN))
13008 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13009 sql.q_append(SPIDER_SQL_SELECT_STR, SPIDER_SQL_SELECT_LEN);
13010 DBUG_RETURN(0);
13011}
13012
13013int spider_mysql_copy_table::append_insert_str(
13014 int insert_flg
13015) {
13016 DBUG_ENTER("spider_mysql_copy_table::append_insert_str");
13017 DBUG_PRINT("info",("spider this=%p", this));
13018 if (insert_flg & SPIDER_DB_INSERT_REPLACE)
13019 {
13020 if (sql.reserve(SPIDER_SQL_REPLACE_LEN))
13021 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13022 sql.q_append(SPIDER_SQL_REPLACE_STR, SPIDER_SQL_REPLACE_LEN);
13023 } else {
13024 if (sql.reserve(SPIDER_SQL_INSERT_LEN))
13025 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13026 sql.q_append(SPIDER_SQL_INSERT_STR, SPIDER_SQL_INSERT_LEN);
13027 }
13028 if (insert_flg & SPIDER_DB_INSERT_LOW_PRIORITY)
13029 {
13030 if (sql.reserve(SPIDER_SQL_LOW_PRIORITY_LEN))
13031 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13032 sql.q_append(SPIDER_SQL_LOW_PRIORITY_STR, SPIDER_SQL_LOW_PRIORITY_LEN);
13033 }
13034 else if (insert_flg & SPIDER_DB_INSERT_DELAYED)
13035 {
13036 if (sql.reserve(SPIDER_SQL_SQL_DELAYED_LEN))
13037 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13038 sql.q_append(SPIDER_SQL_SQL_DELAYED_STR, SPIDER_SQL_SQL_DELAYED_LEN);
13039 }
13040 else if (insert_flg & SPIDER_DB_INSERT_HIGH_PRIORITY)
13041 {
13042 if (sql.reserve(SPIDER_SQL_HIGH_PRIORITY_LEN))
13043 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13044 sql.q_append(SPIDER_SQL_HIGH_PRIORITY_STR, SPIDER_SQL_HIGH_PRIORITY_LEN);
13045 }
13046 if (insert_flg & SPIDER_DB_INSERT_IGNORE)
13047 {
13048 if (sql.reserve(SPIDER_SQL_SQL_IGNORE_LEN))
13049 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13050 sql.q_append(SPIDER_SQL_SQL_IGNORE_STR, SPIDER_SQL_SQL_IGNORE_LEN);
13051 }
13052 DBUG_RETURN(0);
13053}
13054
13055int spider_mysql_copy_table::append_table_columns(
13056 TABLE_SHARE *table_share
13057) {
13058 int error_num;
13059 Field **field;
13060 DBUG_ENTER("spider_mysql_copy_table::append_table_columns");
13061 DBUG_PRINT("info",("spider this=%p", this));
13062 for (field = table_share->field; *field; field++)
13063 {
13064 if (sql.reserve(SPIDER_SQL_NAME_QUOTE_LEN))
13065 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13066 sql.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
13067 if ((error_num = spider_db_append_name_with_quote_str(&sql,
13068 (char *) (*field)->field_name.str, spider_dbton_mysql.dbton_id)))
13069 DBUG_RETURN(error_num);
13070 if (sql.reserve(SPIDER_SQL_NAME_QUOTE_LEN + SPIDER_SQL_COMMA_LEN))
13071 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13072 sql.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
13073 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
13074 }
13075 sql.length(sql.length() - SPIDER_SQL_COMMA_LEN);
13076 DBUG_RETURN(0);
13077}
13078
13079int spider_mysql_copy_table::append_from_str()
13080{
13081 DBUG_ENTER("spider_mysql_copy_table::append_from_str");
13082 DBUG_PRINT("info",("spider this=%p", this));
13083 if (sql.reserve(SPIDER_SQL_FROM_LEN))
13084 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13085 sql.q_append(SPIDER_SQL_FROM_STR, SPIDER_SQL_FROM_LEN);
13086 DBUG_RETURN(0);
13087}
13088
13089int spider_mysql_copy_table::append_table_name(
13090 int link_idx
13091) {
13092 int error_num;
13093 DBUG_ENTER("spider_mysql_copy_table::append_table_name");
13094 DBUG_PRINT("info",("spider this=%p", this));
13095 error_num = mysql_share->append_table_name(&sql, link_idx);
13096 DBUG_RETURN(error_num);
13097}
13098
13099void spider_mysql_copy_table::set_sql_pos()
13100{
13101 DBUG_ENTER("spider_mysql_copy_table::set_sql_pos");
13102 DBUG_PRINT("info",("spider this=%p", this));
13103 pos = sql.length();
13104 DBUG_VOID_RETURN;
13105}
13106
13107void spider_mysql_copy_table::set_sql_to_pos()
13108{
13109 DBUG_ENTER("spider_mysql_copy_table::set_sql_to_pos");
13110 DBUG_PRINT("info",("spider this=%p", this));
13111 sql.length(pos);
13112 DBUG_VOID_RETURN;
13113}
13114
13115int spider_mysql_copy_table::append_copy_where(
13116 spider_db_copy_table *source_ct,
13117 KEY *key_info,
13118 ulong *last_row_pos,
13119 ulong *last_lengths
13120) {
13121 int error_num, roop_count, roop_count2;
13122 DBUG_ENTER("spider_mysql_copy_table::append_copy_where");
13123 DBUG_PRINT("info",("spider this=%p", this));
13124 if (sql.reserve(SPIDER_SQL_WHERE_LEN + SPIDER_SQL_OPEN_PAREN_LEN))
13125 {
13126 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13127 }
13128 sql.q_append(SPIDER_SQL_WHERE_STR, SPIDER_SQL_WHERE_LEN);
13129 sql.q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
13130 Field *field;
13131 KEY_PART_INFO *key_part = key_info->key_part;
13132 for (roop_count = spider_user_defined_key_parts(key_info) - 1;
13133 roop_count >= 0; roop_count--)
13134 {
13135 for (roop_count2 = 0; roop_count2 < roop_count; roop_count2++)
13136 {
13137 field = key_part[roop_count2].field;
13138 if ((error_num = copy_key_row(source_ct,
13139 field, &last_row_pos[field->field_index],
13140 &last_lengths[field->field_index],
13141 SPIDER_SQL_EQUAL_STR, SPIDER_SQL_EQUAL_LEN)))
13142 {
13143 DBUG_RETURN(error_num);
13144 }
13145 }
13146 field = key_part[roop_count2].field;
13147 if ((error_num = copy_key_row(source_ct,
13148 field, &last_row_pos[field->field_index],
13149 &last_lengths[field->field_index],
13150 SPIDER_SQL_GT_STR, SPIDER_SQL_GT_LEN)))
13151 {
13152 DBUG_RETURN(error_num);
13153 }
13154 sql.length(sql.length() - SPIDER_SQL_AND_LEN);
13155 if (sql.reserve(SPIDER_SQL_CLOSE_PAREN_LEN +
13156 SPIDER_SQL_OR_LEN + SPIDER_SQL_OPEN_PAREN_LEN))
13157 {
13158 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13159 }
13160 sql.q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
13161 sql.q_append(SPIDER_SQL_OR_STR, SPIDER_SQL_OR_LEN);
13162 sql.q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
13163 }
13164 sql.length(sql.length() - SPIDER_SQL_OR_LEN - SPIDER_SQL_OPEN_PAREN_LEN);
13165 DBUG_RETURN(0);
13166}
13167
13168int spider_mysql_copy_table::append_key_order_str(
13169 KEY *key_info,
13170 int start_pos,
13171 bool desc_flg
13172) {
13173 int length, error_num;
13174 KEY_PART_INFO *key_part;
13175 Field *field;
13176 DBUG_ENTER("spider_mysql_copy_table::append_key_order_str");
13177 DBUG_PRINT("info",("spider this=%p", this));
13178 if ((int) spider_user_defined_key_parts(key_info) > start_pos)
13179 {
13180 if (sql.reserve(SPIDER_SQL_ORDER_LEN))
13181 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13182 sql.q_append(SPIDER_SQL_ORDER_STR, SPIDER_SQL_ORDER_LEN);
13183 if (desc_flg == TRUE)
13184 {
13185 for (
13186 key_part = key_info->key_part + start_pos,
13187 length = 0;
13188 length + start_pos < (int) spider_user_defined_key_parts(key_info);
13189 key_part++,
13190 length++
13191 ) {
13192 field = key_part->field;
13193 if (sql.reserve(SPIDER_SQL_NAME_QUOTE_LEN))
13194 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13195 sql.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
13196 if ((error_num = spider_db_append_name_with_quote_str(&sql,
13197 (char *) field->field_name.str, spider_dbton_mysql.dbton_id)))
13198 DBUG_RETURN(error_num);
13199 if (key_part->key_part_flag & HA_REVERSE_SORT)
13200 {
13201 if (sql.reserve(SPIDER_SQL_NAME_QUOTE_LEN + SPIDER_SQL_COMMA_LEN))
13202 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13203 sql.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
13204 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
13205 } else {
13206 if (sql.reserve(SPIDER_SQL_NAME_QUOTE_LEN + SPIDER_SQL_DESC_LEN +
13207 SPIDER_SQL_COMMA_LEN))
13208 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13209 sql.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
13210 sql.q_append(SPIDER_SQL_DESC_STR, SPIDER_SQL_DESC_LEN);
13211 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
13212 }
13213 }
13214 } else {
13215 for (
13216 key_part = key_info->key_part + start_pos,
13217 length = 0;
13218 length + start_pos < (int) spider_user_defined_key_parts(key_info);
13219 key_part++,
13220 length++
13221 ) {
13222 field = key_part->field;
13223 if (sql.reserve(SPIDER_SQL_NAME_QUOTE_LEN))
13224 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13225 sql.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
13226 if ((error_num = spider_db_append_name_with_quote_str(&sql,
13227 (char *) field->field_name.str, spider_dbton_mysql.dbton_id)))
13228 DBUG_RETURN(error_num);
13229 if (key_part->key_part_flag & HA_REVERSE_SORT)
13230 {
13231 if (sql.reserve(SPIDER_SQL_NAME_QUOTE_LEN + SPIDER_SQL_DESC_LEN +
13232 SPIDER_SQL_COMMA_LEN))
13233 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13234 sql.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
13235 sql.q_append(SPIDER_SQL_DESC_STR, SPIDER_SQL_DESC_LEN);
13236 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
13237 } else {
13238 if (sql.reserve(SPIDER_SQL_NAME_QUOTE_LEN + SPIDER_SQL_COMMA_LEN))
13239 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13240 sql.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
13241 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
13242 }
13243 }
13244 }
13245 sql.length(sql.length() - SPIDER_SQL_COMMA_LEN);
13246 }
13247 DBUG_RETURN(0);
13248}
13249
13250int spider_mysql_copy_table::append_limit(
13251 longlong offset,
13252 longlong limit
13253) {
13254 char buf[SPIDER_LONGLONG_LEN + 1];
13255 uint32 length;
13256 DBUG_ENTER("spider_mysql_copy_table::append_limit");
13257 DBUG_PRINT("info",("spider this=%p", this));
13258 if (offset || limit < 9223372036854775807LL)
13259 {
13260 if (sql.reserve(SPIDER_SQL_LIMIT_LEN + SPIDER_SQL_COMMA_LEN +
13261 ((SPIDER_LONGLONG_LEN) * 2)))
13262 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13263 sql.q_append(SPIDER_SQL_LIMIT_STR, SPIDER_SQL_LIMIT_LEN);
13264 if (offset)
13265 {
13266 length = (uint32) (my_charset_bin.cset->longlong10_to_str)(
13267 &my_charset_bin, buf, SPIDER_LONGLONG_LEN + 1, -10, offset);
13268 sql.q_append(buf, length);
13269 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
13270 }
13271 length = (uint32) (my_charset_bin.cset->longlong10_to_str)(
13272 &my_charset_bin, buf, SPIDER_LONGLONG_LEN + 1, -10, limit);
13273 sql.q_append(buf, length);
13274 }
13275 DBUG_RETURN(0);
13276}
13277
13278int spider_mysql_copy_table::append_into_str()
13279{
13280 DBUG_ENTER("spider_mysql_copy_table::append_into_str");
13281 DBUG_PRINT("info",("spider this=%p", this));
13282 if (sql.reserve(SPIDER_SQL_INTO_LEN))
13283 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13284 sql.q_append(SPIDER_SQL_INTO_STR, SPIDER_SQL_INTO_LEN);
13285 DBUG_RETURN(0);
13286}
13287
13288int spider_mysql_copy_table::append_open_paren_str()
13289{
13290 DBUG_ENTER("spider_mysql_copy_table::append_open_paren_str");
13291 DBUG_PRINT("info",("spider this=%p", this));
13292 if (sql.reserve(SPIDER_SQL_OPEN_PAREN_LEN))
13293 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13294 sql.q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
13295 DBUG_RETURN(0);
13296}
13297
13298int spider_mysql_copy_table::append_values_str()
13299{
13300 DBUG_ENTER("spider_mysql_copy_table::append_values_str");
13301 DBUG_PRINT("info",("spider this=%p", this));
13302 if (sql.reserve(SPIDER_SQL_CLOSE_PAREN_LEN + SPIDER_SQL_VALUES_LEN +
13303 SPIDER_SQL_OPEN_PAREN_LEN))
13304 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13305 sql.q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
13306 sql.q_append(SPIDER_SQL_VALUES_STR, SPIDER_SQL_VALUES_LEN);
13307 sql.q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
13308 DBUG_RETURN(0);
13309}
13310
13311int spider_mysql_copy_table::append_select_lock_str(
13312 int lock_mode
13313) {
13314 DBUG_ENTER("spider_mysql_copy_table::append_select_lock_str");
13315 DBUG_PRINT("info",("spider this=%p", this));
13316 if (lock_mode == SPIDER_LOCK_MODE_EXCLUSIVE)
13317 {
13318 if (sql.reserve(SPIDER_SQL_FOR_UPDATE_LEN))
13319 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13320 sql.q_append(SPIDER_SQL_FOR_UPDATE_STR, SPIDER_SQL_FOR_UPDATE_LEN);
13321 } else if (lock_mode == SPIDER_LOCK_MODE_SHARED)
13322 {
13323 if (sql.reserve(SPIDER_SQL_SHARED_LOCK_LEN))
13324 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13325 sql.q_append(SPIDER_SQL_SHARED_LOCK_STR, SPIDER_SQL_SHARED_LOCK_LEN);
13326 }
13327 DBUG_RETURN(0);
13328}
13329
13330int spider_mysql_copy_table::exec_query(
13331 SPIDER_CONN *conn,
13332 int quick_mode,
13333 int *need_mon
13334) {
13335 int error_num;
13336 DBUG_ENTER("spider_mysql_copy_table::exec_query");
13337 DBUG_PRINT("info",("spider this=%p", this));
13338 error_num = spider_db_query(conn, sql.ptr(), sql.length(), quick_mode,
13339 need_mon);
13340 DBUG_RETURN(error_num);
13341}
13342
13343int spider_mysql_copy_table::copy_key_row(
13344 spider_db_copy_table *source_ct,
13345 Field *field,
13346 ulong *row_pos,
13347 ulong *length,
13348 const char *joint_str,
13349 const int joint_length
13350) {
13351 int error_num;
13352 spider_string *source_str = &((spider_mysql_copy_table *) source_ct)->sql;
13353 DBUG_ENTER("spider_mysql_copy_table::copy_key_row");
13354 DBUG_PRINT("info",("spider this=%p", this));
13355 if (sql.reserve(SPIDER_SQL_NAME_QUOTE_LEN))
13356 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13357 sql.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
13358 if ((error_num = spider_db_append_name_with_quote_str(&sql,
13359 (char *) field->field_name.str, spider_dbton_mysql.dbton_id)))
13360 DBUG_RETURN(error_num);
13361 if (sql.reserve(SPIDER_SQL_NAME_QUOTE_LEN + joint_length + *length +
13362 SPIDER_SQL_AND_LEN))
13363 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13364 sql.q_append(SPIDER_SQL_NAME_QUOTE_STR, SPIDER_SQL_NAME_QUOTE_LEN);
13365 sql.q_append(joint_str, joint_length);
13366 sql.q_append(source_str->ptr() + *row_pos, *length);
13367 sql.q_append(SPIDER_SQL_AND_STR, SPIDER_SQL_AND_LEN);
13368 DBUG_RETURN(0);
13369}
13370
13371int spider_mysql_copy_table::copy_row(
13372 Field *field,
13373 SPIDER_DB_ROW *row
13374) {
13375 int error_num;
13376 DBUG_ENTER("spider_mysql_copy_table::copy_row");
13377 DBUG_PRINT("info",("spider this=%p", this));
13378 if (row->is_null())
13379 {
13380 if (sql.reserve(SPIDER_SQL_NULL_LEN + SPIDER_SQL_COMMA_LEN))
13381 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13382 sql.q_append(SPIDER_SQL_NULL_STR, SPIDER_SQL_NULL_LEN);
13383 } else if (field->str_needs_quotes())
13384 {
13385 if (sql.reserve(SPIDER_SQL_VALUE_QUOTE_LEN))
13386 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13387 sql.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
13388 if ((error_num = row->append_escaped_to_str(&sql,
13389 spider_dbton_mysql.dbton_id)))
13390 DBUG_RETURN(error_num);
13391 if (sql.reserve(SPIDER_SQL_VALUE_QUOTE_LEN + SPIDER_SQL_COMMA_LEN))
13392 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13393 sql.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
13394 } else {
13395 if ((error_num = row->append_to_str(&sql)))
13396 DBUG_RETURN(error_num);
13397 if (sql.reserve(SPIDER_SQL_COMMA_LEN))
13398 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13399 }
13400 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
13401 DBUG_RETURN(0);
13402}
13403
13404int spider_mysql_copy_table::copy_rows(
13405 TABLE *table,
13406 SPIDER_DB_ROW *row,
13407 ulong **last_row_pos,
13408 ulong **last_lengths
13409) {
13410 int error_num;
13411 Field **field;
13412 ulong *lengths2, *row_pos2;
13413 DBUG_ENTER("spider_mysql_copy_table::copy_rows");
13414 DBUG_PRINT("info",("spider this=%p", this));
13415 row_pos2 = *last_row_pos;
13416 lengths2 = *last_lengths;
13417
13418 for (
13419 field = table->field;
13420 *field;
13421 field++,
13422 lengths2++
13423 ) {
13424 *row_pos2 = sql.length();
13425 if ((error_num =
13426 copy_row(*field, row)))
13427 DBUG_RETURN(error_num);
13428 *lengths2 = sql.length() - *row_pos2 - SPIDER_SQL_COMMA_LEN;
13429 row->next();
13430 row_pos2++;
13431 }
13432 sql.length(sql.length() - SPIDER_SQL_COMMA_LEN);
13433 if (sql.reserve(SPIDER_SQL_CLOSE_PAREN_LEN +
13434 SPIDER_SQL_COMMA_LEN + SPIDER_SQL_OPEN_PAREN_LEN))
13435 {
13436 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13437 }
13438 sql.q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
13439 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
13440 sql.q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
13441 DBUG_RETURN(0);
13442}
13443
13444int spider_mysql_copy_table::copy_rows(
13445 TABLE *table,
13446 SPIDER_DB_ROW *row
13447) {
13448 int error_num;
13449 Field **field;
13450 DBUG_ENTER("spider_mysql_copy_table::copy_rows");
13451 DBUG_PRINT("info",("spider this=%p", this));
13452 for (
13453 field = table->field;
13454 *field;
13455 field++
13456 ) {
13457 if ((error_num =
13458 copy_row(*field, row)))
13459 DBUG_RETURN(error_num);
13460 row->next();
13461 }
13462 sql.length(sql.length() - SPIDER_SQL_COMMA_LEN);
13463 if (sql.reserve(SPIDER_SQL_CLOSE_PAREN_LEN +
13464 SPIDER_SQL_COMMA_LEN + SPIDER_SQL_OPEN_PAREN_LEN))
13465 {
13466 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13467 }
13468 sql.q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
13469 sql.q_append(SPIDER_SQL_COMMA_STR, SPIDER_SQL_COMMA_LEN);
13470 sql.q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
13471 DBUG_RETURN(0);
13472}
13473
13474int spider_mysql_copy_table::append_insert_terminator()
13475{
13476 DBUG_ENTER("spider_mysql_copy_table::append_insert_terminator");
13477 DBUG_PRINT("info",("spider this=%p", this));
13478 sql.length(sql.length() - SPIDER_SQL_COMMA_LEN - SPIDER_SQL_OPEN_PAREN_LEN);
13479 DBUG_RETURN(0);
13480}
13481
13482int spider_mysql_copy_table::copy_insert_values(
13483 spider_db_copy_table *source_ct
13484) {
13485 spider_mysql_copy_table *tmp_ct = (spider_mysql_copy_table *) source_ct;
13486 spider_string *source_str = &tmp_ct->sql;
13487 int values_length = source_str->length() - tmp_ct->pos;
13488 const char *values_ptr = source_str->ptr() + tmp_ct->pos;
13489 DBUG_ENTER("spider_mysql_copy_table::copy_insert_values");
13490 DBUG_PRINT("info",("spider this=%p", this));
13491 if (sql.reserve(values_length))
13492 {
13493 DBUG_RETURN(HA_ERR_OUT_OF_MEM);
13494 }
13495 sql.q_append(values_ptr, values_length);
13496 DBUG_RETURN(0);
13497}
13498