1
/* Copyright (C) 2000-2006 MySQL AB
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.
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.
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 */
17
/*****************************************************************************
19
** This file implements classes defined in sql_class.h
20
** Especially the classes to handle a result from a select
22
*****************************************************************************/
24
#ifdef USE_PRAGMA_IMPLEMENTATION
25
#pragma implementation // gcc: Class implementation
28
#include "mysql_priv.h"
30
#include "rpl_record.h"
32
#include <my_bitmap.h>
33
#include "log_event.h"
36
#include <thr_alarm.h>
37
#include <mysys_err.h>
40
The following is used to initialise Table_ident with a internal
43
char internal_table_name[2]= "*";
44
char empty_c_string[1]= {0}; /* used for not defined db */
46
const char * const THD::DEFAULT_WHERE= "field list";
49
/*****************************************************************************
50
** Instansiate templates
51
*****************************************************************************/
53
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
55
template class List<Key>;
56
template class List_iterator<Key>;
57
template class List<Key_part_spec>;
58
template class List_iterator<Key_part_spec>;
59
template class List<Alter_drop>;
60
template class List_iterator<Alter_drop>;
61
template class List<Alter_column>;
62
template class List_iterator<Alter_column>;
65
/****************************************************************************
67
****************************************************************************/
69
extern "C" uchar *get_var_key(user_var_entry *entry, size_t *length,
70
my_bool not_used __attribute__((unused)))
72
*length= entry->name.length;
73
return (uchar*) entry->name.str;
76
extern "C" void free_user_var(user_var_entry *entry)
78
char *pos= (char*) entry+ALIGN_SIZE(sizeof(*entry));
79
if (entry->value && entry->value != pos)
80
my_free(entry->value, MYF(0));
81
my_free((char*) entry,MYF(0));
84
bool Key_part_spec::operator==(const Key_part_spec& other) const
86
return length == other.length &&
87
field_name.length == other.field_name.length &&
88
!strcmp(field_name.str, other.field_name.str);
92
Construct an (almost) deep copy of this key. Only those
93
elements that are known to never change are not copied.
94
If out of memory, a partial copy is returned and an error is set
98
Key::Key(const Key &rhs, MEM_ROOT *mem_root)
100
key_create_info(rhs.key_create_info),
101
columns(rhs.columns, mem_root),
103
generated(rhs.generated)
105
list_copy_and_replace_each_value(columns, mem_root);
109
Construct an (almost) deep copy of this foreign key. Only those
110
elements that are known to never change are not copied.
111
If out of memory, a partial copy is returned and an error is set
115
Foreign_key::Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root)
117
ref_table(rhs.ref_table),
118
ref_columns(rhs.ref_columns),
119
delete_opt(rhs.delete_opt),
120
update_opt(rhs.update_opt),
121
match_opt(rhs.match_opt)
123
list_copy_and_replace_each_value(ref_columns, mem_root);
127
Test if a foreign key (= generated key) is a prefix of the given key
128
(ignoring key name, key type and order of columns)
131
This is only used to test if an index for a FOREIGN KEY exists
134
We only compare field names
137
0 Generated key is a prefix of other key
141
bool foreign_key_prefix(Key *a, Key *b)
143
/* Ensure that 'a' is the generated key */
146
if (b->generated && a->columns.elements > b->columns.elements)
147
swap_variables(Key*, a, b); // Put shorter key in 'a'
152
return TRUE; // No foreign key
153
swap_variables(Key*, a, b); // Put generated key in 'a'
156
/* Test if 'a' is a prefix of 'b' */
157
if (a->columns.elements > b->columns.elements)
158
return TRUE; // Can't be prefix
160
List_iterator<Key_part_spec> col_it1(a->columns);
161
List_iterator<Key_part_spec> col_it2(b->columns);
162
const Key_part_spec *col1, *col2;
164
#ifdef ENABLE_WHEN_INNODB_CAN_HANDLE_SWAPED_FOREIGN_KEY_COLUMNS
165
while ((col1= col_it1++))
169
while ((col2= col_it2++))
178
return TRUE; // Error
180
return FALSE; // Is prefix
182
while ((col1= col_it1++))
185
if (!(*col1 == *col2))
188
return FALSE; // Is prefix
193
/****************************************************************************
194
** Thread specific functions
195
****************************************************************************/
197
Open_tables_state::Open_tables_state(ulong version_arg)
198
:version(version_arg), state_flags(0U)
200
reset_open_tables_state();
204
The following functions form part of the C plugin API
207
extern "C" int mysql_tmpfile(const char *prefix)
209
char filename[FN_REFLEN];
210
File fd = create_temp_file(filename, mysql_tmpdir, prefix,
211
O_CREAT | O_EXCL | O_RDWR | O_TEMPORARY,
215
This can be removed once the following bug is fixed:
216
Bug #28903 create_temp_file() doesn't honor O_TEMPORARY option
217
(file not removed) (Unix)
227
int thd_in_lock_tables(const THD *thd)
229
return test(thd->in_lock_tables);
234
int thd_tablespace_op(const THD *thd)
236
return test(thd->tablespace_op);
241
const char *set_thd_proc_info(THD *thd, const char *info,
242
const char *calling_function,
243
const char *calling_file,
244
const unsigned int calling_line)
246
const char *old_info= thd->proc_info;
247
DBUG_PRINT("proc_info", ("%s:%d %s", calling_file, calling_line,
248
(info != NULL) ? info : "(null)"));
249
thd->proc_info= info;
254
void **thd_ha_data(const THD *thd, const struct handlerton *hton)
256
return (void **) &thd->ha_data[hton->slot].ha_ptr;
260
long long thd_test_options(const THD *thd, long long test_options)
262
return thd->options & test_options;
266
int thd_sql_command(const THD *thd)
268
return (int) thd->lex->sql_command;
272
int thd_tx_isolation(const THD *thd)
274
return (int) thd->variables.tx_isolation;
278
void thd_inc_row_count(THD *thd)
284
Dumps a text description of a thread, its security context
285
(user, host) and the current query.
288
thd_security_context()
289
thd current thread context
290
buffer pointer to preferred result buffer
291
length length of buffer
292
max_query_len how many chars of query to copy (0 for all)
298
char *thd_security_context(THD *thd, char *buffer, unsigned int length,
299
unsigned int max_query_len)
301
String str(buffer, length, &my_charset_latin1);
302
const Security_context *sctx= &thd->main_security_ctx;
306
len= my_snprintf(header, sizeof(header),
307
"MySQL thread id %lu, query id %lu",
308
thd->thread_id, (ulong) thd->query_id);
310
str.append(header, len);
315
str.append(sctx->host);
321
str.append(sctx->ip);
327
str.append(sctx->user);
333
str.append(thd->proc_info);
338
if (max_query_len < 1)
339
len= thd->query_length;
341
len= min(thd->query_length, max_query_len);
343
str.append(thd->query, len);
345
if (str.c_ptr_safe() == buffer)
347
return thd->strmake(str.ptr(), str.length());
351
Clear this diagnostics area.
353
Normally called at the end of a statement.
357
Diagnostics_area::reset_diagnostics_area()
360
can_overwrite_status= FALSE;
361
/** Don't take chances in production */
367
m_total_warn_count= 0;
370
/** Tiny reset in debug mode to see garbage right away */
376
Set OK status -- ends commands that do not return a
377
result set, e.g. INSERT/UPDATE/DELETE.
381
Diagnostics_area::set_ok_status(THD *thd, ha_rows affected_rows_arg,
382
ulonglong last_insert_id_arg,
383
const char *message_arg)
385
DBUG_ASSERT(! is_set());
388
In production, refuse to overwrite an error or a custom response
391
if (is_error() || is_disabled())
394
/** Only allowed to report success if has not yet reported an error */
396
m_server_status= thd->server_status;
397
m_total_warn_count= thd->total_warn_count;
398
m_affected_rows= affected_rows_arg;
399
m_last_insert_id= last_insert_id_arg;
401
strmake(m_message, message_arg, sizeof(m_message) - 1);
413
Diagnostics_area::set_eof_status(THD *thd)
415
/** Only allowed to report eof if has not yet reported an error */
417
DBUG_ASSERT(! is_set());
420
In production, refuse to overwrite an error or a custom response
423
if (is_error() || is_disabled())
427
m_server_status= thd->server_status;
429
If inside a stored procedure, do not return the total
430
number of warnings, since they are not available to the client
433
m_total_warn_count= thd->total_warn_count;
443
Diagnostics_area::set_error_status(THD *thd, uint sql_errno_arg,
444
const char *message_arg)
447
Only allowed to report error if has not yet reported a success
448
The only exception is when we flush the message to the client,
449
an error can happen during the flush.
451
DBUG_ASSERT(! is_set() || can_overwrite_status);
454
In production, refuse to overwrite a custom response with an
461
m_sql_errno= sql_errno_arg;
462
strmake(m_message, message_arg, sizeof(m_message) - 1);
469
Mark the diagnostics area as 'DISABLED'.
471
This is used in rare cases when the COM_ command at hand sends a response
472
in a custom format. One example is the query cache, another is
477
Diagnostics_area::disable_status()
479
DBUG_ASSERT(! is_set());
480
m_status= DA_DISABLED;
485
:Statement(&main_lex, &main_mem_root, CONVENTIONAL_EXECUTION,
486
/* statement id */ 0),
487
Open_tables_state(refresh_version), rli_fake(0),
488
lock_id(&main_lock_id),
489
user_time(0), in_sub_stmt(0),
490
binlog_table_maps(0), binlog_flags(0UL),
491
arg_of_last_insert_id_function(FALSE),
492
first_successful_insert_id_in_prev_stmt(0),
493
first_successful_insert_id_in_prev_stmt_for_binlog(0),
494
first_successful_insert_id_in_cur_stmt(0),
495
stmt_depends_on_first_successful_insert_id_in_prev_stmt(FALSE),
498
transaction_rollback_request(0),
499
is_fatal_sub_stmt_error(0),
504
derived_tables_processing(FALSE),
507
@todo The following is a work around for online backup and the DDL blocker.
508
It should be removed when the generalized solution is in place.
509
This is needed to ensure the restore (which uses DDL) is not blocked
510
when the DDL blocker is engaged.
517
Pass nominal parameters to init_alloc_root only to ensure that
518
the destructor works OK in case of an error. The main_mem_root
519
will be re-initialized in init_for_queries().
521
init_sql_alloc(&main_mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
524
catalog= (char*)"std"; // the only catalog we have for now
525
main_security_ctx.init();
526
security_ctx= &main_security_ctx;
527
some_tables_deleted=no_errors=password= 0;
529
count_cuted_fields= CHECK_FIELD_IGNORE;
532
is_slave_error= thread_specific_used= FALSE;
533
hash_clear(&handler_tables_hash);
536
cuted_fields= sent_row_count= row_count= 0L;
539
statement_id_counter= 0UL;
540
// Must be reset to handle error with THD's created for init of mysqld
541
lex->current_select= 0;
542
start_time=(time_t) 0;
544
utime_after_lock= 0L;
547
bzero(&variables, sizeof(variables));
553
db_charset= global_system_variables.collation_database;
554
bzero(ha_data, sizeof(ha_data));
556
binlog_evt_union.do_union= FALSE;
559
dbug_sentry=THD_SENTRY_MAGIC;
562
client_capabilities= 0; // minimalistic client
563
system_thread= NON_SYSTEM_THREAD;
564
cleanup_done= abort_on_warning= no_warnings_for_error= 0;
565
peer_port= 0; // For SHOW PROCESSLIST
566
transaction.m_pending_rows_event= 0;
568
#ifdef SIGNAL_WITH_VIO_CLOSE
571
pthread_mutex_init(&LOCK_delete, MY_MUTEX_INIT_FAST);
573
/* Variables with default values */
575
where= THD::DEFAULT_WHERE;
576
server_id = ::server_id;
582
/* Initialize sub structures */
583
init_sql_alloc(&warn_root, WARN_ALLOC_BLOCK_SIZE, WARN_ALLOC_PREALLOC_SIZE);
584
user_connect=(USER_CONN *)0;
585
hash_init(&user_vars, system_charset_info, USER_VARS_HASH_SIZE, 0, 0,
586
(hash_get_key) get_var_key,
587
(hash_free_key) free_user_var, 0);
589
/* For user vars replication*/
591
my_init_dynamic_array(&user_var_events,
592
sizeof(BINLOG_USER_VAR_EVENT *), 16, 16);
594
bzero((char*) &user_var_events, sizeof(user_var_events));
597
protocol= &protocol_text; // Default protocol
598
protocol_text.init(this);
601
tmp= sql_rnd_with_mutex();
602
randominit(&rand, tmp + (ulong) &rand, tmp + (ulong) ::global_query_id);
603
substitute_null_with_insert_id = FALSE;
604
thr_lock_info_init(&lock_info); /* safety: will be reset after start */
605
thr_lock_owner_init(&main_lock_id, &lock_info);
607
m_internal_handler= NULL;
611
void THD::push_internal_handler(Internal_error_handler *handler)
614
TODO: The current implementation is limited to 1 handler at a time only.
615
THD and sp_rcontext need to be modified to use a common handler stack.
617
DBUG_ASSERT(m_internal_handler == NULL);
618
m_internal_handler= handler;
622
bool THD::handle_error(uint sql_errno, const char *message,
623
MYSQL_ERROR::enum_warning_level level)
625
if (m_internal_handler)
627
return m_internal_handler->handle_error(sql_errno, message, level, this);
630
return FALSE; // 'FALSE', as per coding style
634
void THD::pop_internal_handler()
636
DBUG_ASSERT(m_internal_handler != NULL);
637
m_internal_handler= NULL;
641
void *thd_alloc(MYSQL_THD thd, unsigned int size)
643
return thd->alloc(size);
647
void *thd_calloc(MYSQL_THD thd, unsigned int size)
649
return thd->calloc(size);
653
char *thd_strdup(MYSQL_THD thd, const char *str)
655
return thd->strdup(str);
659
char *thd_strmake(MYSQL_THD thd, const char *str, unsigned int size)
661
return thd->strmake(str, size);
665
LEX_STRING *thd_make_lex_string(THD *thd, LEX_STRING *lex_str,
666
const char *str, unsigned int size,
667
int allocate_lex_string)
669
return thd->make_lex_string(lex_str, str, size,
670
(bool) allocate_lex_string);
674
void *thd_memdup(MYSQL_THD thd, const void* str, unsigned int size)
676
return thd->memdup(str, size);
680
void thd_get_xid(const MYSQL_THD thd, MYSQL_XID *xid)
682
*xid = *(MYSQL_XID *) &thd->transaction.xid_state.xid;
686
Init common variables that has to be reset on start and on change_user
691
pthread_mutex_lock(&LOCK_global_system_variables);
692
plugin_thdvar_init(this);
693
variables.time_format= date_time_format_copy((THD*) 0,
694
variables.time_format);
695
variables.date_format= date_time_format_copy((THD*) 0,
696
variables.date_format);
697
variables.datetime_format= date_time_format_copy((THD*) 0,
698
variables.datetime_format);
700
variables= global_system_variables above has reset
701
variables.pseudo_thread_id to 0. We need to correct it here to
702
avoid temporary tables replication failure.
704
variables.pseudo_thread_id= thread_id;
705
pthread_mutex_unlock(&LOCK_global_system_variables);
706
server_status= SERVER_STATUS_AUTOCOMMIT;
707
options= thd_startup_options;
709
if (variables.max_join_size == HA_POS_ERROR)
710
options |= OPTION_BIG_SELECTS;
712
options &= ~OPTION_BIG_SELECTS;
714
transaction.all.modified_non_trans_table= transaction.stmt.modified_non_trans_table= FALSE;
715
open_options=ha_open_options;
716
update_lock_default= (variables.low_priority_updates ?
717
TL_WRITE_LOW_PRIORITY :
719
session_tx_isolation= (enum_tx_isolation) variables.tx_isolation;
721
bzero((char*) warn_count, sizeof(warn_count));
724
reset_current_stmt_binlog_row_based();
725
bzero((char *) &status_var, sizeof(status_var));
730
Init THD for query processing.
731
This has to be called once before we call mysql_parse.
732
See also comments in sql_class.h.
735
void THD::init_for_queries()
738
ha_enable_transaction(this,TRUE);
740
reset_root_defaults(mem_root, variables.query_alloc_block_size,
741
variables.query_prealloc_size);
742
reset_root_defaults(&transaction.mem_root,
743
variables.trans_alloc_block_size,
744
variables.trans_prealloc_size);
745
transaction.xid_state.xid.null();
746
transaction.xid_state.in_thd=1;
750
/* Do operations that may take a long time */
752
void THD::cleanup(void)
754
DBUG_ENTER("THD::cleanup");
755
DBUG_ASSERT(cleanup_done == 0);
757
killed= KILL_CONNECTION;
758
#ifdef ENABLE_WHEN_BINLOG_WILL_BE_ABLE_TO_PREPARE
759
if (transaction.xid_state.xa_state == XA_PREPARED)
761
#error xid_state in the cache should be replaced by the allocated value
766
xid_cache_delete(&transaction.xid_state);
770
lock=locked_tables; locked_tables=0;
771
close_thread_tables(this);
773
mysql_ha_cleanup(this);
774
delete_dynamic(&user_var_events);
775
hash_free(&user_vars);
776
close_temporary_tables(this);
777
my_free((char*) variables.time_format, MYF(MY_ALLOW_ZERO_PTR));
778
my_free((char*) variables.date_format, MYF(MY_ALLOW_ZERO_PTR));
779
my_free((char*) variables.datetime_format, MYF(MY_ALLOW_ZERO_PTR));
781
if (global_read_lock)
782
unlock_global_read_lock(this);
790
Set the proc_info field.
791
Do not use this method directly, use THD_SET_PROC_INFO instead.
794
void THD::set_proc_info(const char* file, int line, const char* info)
798
file and line correspond to the __FILE__ and __LINE__ where
799
THD_SET_PROC_INFO was called.
800
These two parameters are provided to help instrumenting the code.
803
DBUG_PRINT("info", ("THD::set_proc_info(%s, %d, %s)",
804
file, line, (info ? info : NullS)));
813
THD_CHECK_SENTRY(this);
814
DBUG_ENTER("~THD()");
815
/* Ensure that no one is using THD */
816
pthread_mutex_lock(&LOCK_delete);
817
pthread_mutex_unlock(&LOCK_delete);
818
add_to_status(&global_status_var, &status_var);
820
/* Close connection */
829
ha_close_connection(this);
830
plugin_thdvar_cleanup(this);
832
DBUG_PRINT("info", ("freeing security context"));
833
main_security_ctx.destroy();
835
free_root(&warn_root,MYF(0));
836
free_root(&transaction.mem_root,MYF(0));
837
mysys_var=0; // Safety (shouldn't be needed)
838
pthread_mutex_destroy(&LOCK_delete);
840
dbug_sentry= THD_SENTRY_GONE;
848
free_root(&main_mem_root, MYF(0));
854
Add all status variables to another status variable array
858
to_var add to this array
859
from_var from this array
862
This function assumes that all variables are long/ulong.
863
If this assumption will change, then we have to explictely add
864
the other variables after the while loop
867
void add_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var)
869
ulong *end= (ulong*) ((uchar*) to_var +
870
offsetof(STATUS_VAR, last_system_status_var) +
872
ulong *to= (ulong*) to_var, *from= (ulong*) from_var;
879
Add the difference between two status variable arrays to another one.
883
to_var add to this array
884
from_var from this array
885
dec_var minus this array
888
This function assumes that all variables are long/ulong.
891
void add_diff_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var,
894
ulong *end= (ulong*) ((uchar*) to_var + offsetof(STATUS_VAR,
895
last_system_status_var) +
897
ulong *to= (ulong*) to_var, *from= (ulong*) from_var, *dec= (ulong*) dec_var;
900
*(to++)+= *(from++) - *(dec++);
904
void THD::awake(THD::killed_state state_to_set)
906
DBUG_ENTER("THD::awake");
907
DBUG_PRINT("enter", ("this: 0x%lx", (long) this));
908
THD_CHECK_SENTRY(this);
909
safe_mutex_assert_owner(&LOCK_delete);
911
killed= state_to_set;
912
if (state_to_set != THD::KILL_QUERY)
914
thr_alarm_kill(thread_id);
916
thread_scheduler.post_kill_notification(this);
917
#ifdef SIGNAL_WITH_VIO_CLOSE
918
if (this != current_thd)
921
In addition to a signal, let's close the socket of the thread that
922
is being killed. This is to make sure it does not block if the
923
signal is lost. This needs to be done only on platforms where
924
signals are not a reliable interruption mechanism.
926
If we're killing ourselves, we know that we're not blocked, so this
936
pthread_mutex_lock(&mysys_var->mutex);
937
if (!system_thread) // Don't abort locks
940
This broadcast could be up in the air if the victim thread
941
exits the cond in the time between read and broadcast, but that is
942
ok since all we want to do is to make the victim thread get out
943
of waiting on current_cond.
944
If we see a non-zero current_cond: it cannot be an old value (because
945
then exit_cond() should have run and it can't because we have mutex); so
946
it is the true value but maybe current_mutex is not yet non-zero (we're
947
in the middle of enter_cond() and there is a "memory order
948
inversion"). So we test the mutex too to not lock 0.
950
Note that there is a small chance we fail to kill. If victim has locked
951
current_mutex, but hasn't yet entered enter_cond() (which means that
952
current_cond and current_mutex are 0), then the victim will not get
953
a signal and it may wait "forever" on the cond (until
954
we issue a second KILL or the status it's waiting for happens).
955
It's true that we have set its thd->killed but it may not
956
see it immediately and so may have time to reach the cond_wait().
958
if (mysys_var->current_cond && mysys_var->current_mutex)
960
pthread_mutex_lock(mysys_var->current_mutex);
961
pthread_cond_broadcast(mysys_var->current_cond);
962
pthread_mutex_unlock(mysys_var->current_mutex);
964
pthread_mutex_unlock(&mysys_var->mutex);
970
Remember the location of thread info, the structure needed for
971
sql_alloc() and the structure for the net buffer
974
bool THD::store_globals()
977
Assert that thread_stack is initialized: it's necessary to be able
978
to track stack overrun.
980
DBUG_ASSERT(thread_stack);
982
if (my_pthread_setspecific_ptr(THR_THD, this) ||
983
my_pthread_setspecific_ptr(THR_MALLOC, &mem_root))
985
mysys_var=my_thread_var;
987
Let mysqld define the thread id (not mysys)
988
This allows us to move THD to different threads if needed.
990
mysys_var->id= thread_id;
991
real_id= pthread_self(); // For debugging
994
We have to call thr_lock_info_init() again here as THD may have been
995
created in another thread
997
thr_lock_info_init(&lock_info);
1003
Cleanup after query.
1006
THD::cleanup_after_query()
1009
This function is used to reset thread data to its default state.
1012
This function is not suitable for setting thread data to some
1013
non-default values, as there is only one replication thread, so
1014
different master threads may overwrite data of each other on
1018
void THD::cleanup_after_query()
1021
Reset rand_used so that detection of calls to rand() will save random
1022
seeds if needed by the slave.
1024
Do not reset rand_used if inside a stored function or trigger because
1025
only the call to these operations is logged. Thus only the calling
1026
statement needs to detect rand() calls made by its substatements. These
1027
substatements must not set rand_used to 0 because it would remove the
1028
detection of rand() by the calling statement.
1030
if (!in_sub_stmt) /* stored functions and triggers are a special case */
1032
/* Forget those values, for next binlogger: */
1033
stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
1034
auto_inc_intervals_in_cur_stmt_for_binlog.empty();
1037
if (first_successful_insert_id_in_cur_stmt > 0)
1039
/* set what LAST_INSERT_ID() will return */
1040
first_successful_insert_id_in_prev_stmt=
1041
first_successful_insert_id_in_cur_stmt;
1042
first_successful_insert_id_in_cur_stmt= 0;
1043
substitute_null_with_insert_id= TRUE;
1045
arg_of_last_insert_id_function= 0;
1046
/* Free Items that were created during this execution */
1049
where= THD::DEFAULT_WHERE;
1054
Create a LEX_STRING in this connection.
1056
@param lex_str pointer to LEX_STRING object to be initialized
1057
@param str initializer to be copied into lex_str
1058
@param length length of str, in bytes
1059
@param allocate_lex_string if TRUE, allocate new LEX_STRING object,
1060
instead of using lex_str value
1061
@return NULL on failure, or pointer to the LEX_STRING object
1063
LEX_STRING *THD::make_lex_string(LEX_STRING *lex_str,
1064
const char* str, uint length,
1065
bool allocate_lex_string)
1067
if (allocate_lex_string)
1068
if (!(lex_str= (LEX_STRING *)alloc(sizeof(LEX_STRING))))
1070
if (!(lex_str->str= strmake_root(mem_root, str, length)))
1072
lex_str->length= length;
1078
Convert a string to another character set
1082
to Store new allocated string here
1083
to_cs New character set for allocated string
1084
from String to convert
1085
from_length Length of string to convert
1086
from_cs Original character set
1089
to will be 0-terminated to make it easy to pass to system funcs
1094
In this case to->str will point to 0 and to->length will be 0.
1097
bool THD::convert_string(LEX_STRING *to, CHARSET_INFO *to_cs,
1098
const char *from, uint from_length,
1099
CHARSET_INFO *from_cs)
1101
DBUG_ENTER("convert_string");
1102
size_t new_length= to_cs->mbmaxlen * from_length;
1104
if (!(to->str= (char*) alloc(new_length+1)))
1106
to->length= 0; // Safety fix
1107
DBUG_RETURN(1); // EOM
1109
to->length= copy_and_convert((char*) to->str, new_length, to_cs,
1110
from, from_length, from_cs, &dummy_errors);
1111
to->str[to->length]=0; // Safety
1117
Convert string from source character set to target character set inplace.
1123
Convert string using convert_buffer - buffer for character set
1124
conversion shared between all protocols.
1131
bool THD::convert_string(String *s, CHARSET_INFO *from_cs, CHARSET_INFO *to_cs)
1134
if (convert_buffer.copy(s->ptr(), s->length(), from_cs, to_cs, &dummy_errors))
1136
/* If convert_buffer >> s copying is more efficient long term */
1137
if (convert_buffer.alloced_length() >= convert_buffer.length() * 2 ||
1140
return s->copy(convert_buffer);
1142
s->swap(convert_buffer);
1148
Update some cache variables when character set changes
1151
void THD::update_charset()
1154
charset_is_system_charset= !String::needs_conversion(0,charset(),
1155
system_charset_info,
1157
charset_is_collation_connection=
1158
!String::needs_conversion(0,charset(),variables.collation_connection,
1160
charset_is_character_set_filesystem=
1161
!String::needs_conversion(0, charset(),
1162
variables.character_set_filesystem, ¬_used);
1166
/* routings to adding tables to list of changed in transaction tables */
1168
inline static void list_include(CHANGED_TABLE_LIST** prev,
1169
CHANGED_TABLE_LIST* curr,
1170
CHANGED_TABLE_LIST* new_table)
1175
(*prev)->next = curr;
1179
/* add table to list of changed in transaction tables */
1181
void THD::add_changed_table(TABLE *table)
1183
DBUG_ENTER("THD::add_changed_table(table)");
1185
DBUG_ASSERT((options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) &&
1186
table->file->has_transactions());
1187
add_changed_table(table->s->table_cache_key.str,
1188
(long) table->s->table_cache_key.length);
1193
void THD::add_changed_table(const char *key, long key_length)
1195
DBUG_ENTER("THD::add_changed_table(key)");
1196
CHANGED_TABLE_LIST **prev_changed = &transaction.changed_tables;
1197
CHANGED_TABLE_LIST *curr = transaction.changed_tables;
1199
for (; curr; prev_changed = &(curr->next), curr = curr->next)
1201
int cmp = (long)curr->key_length - (long)key_length;
1204
list_include(prev_changed, curr, changed_table_dup(key, key_length));
1206
("key_length: %ld %u", key_length,
1207
(*prev_changed)->key_length));
1212
cmp = memcmp(curr->key, key, curr->key_length);
1215
list_include(prev_changed, curr, changed_table_dup(key, key_length));
1217
("key_length: %ld %u", key_length,
1218
(*prev_changed)->key_length));
1223
DBUG_PRINT("info", ("already in list"));
1228
*prev_changed = changed_table_dup(key, key_length);
1229
DBUG_PRINT("info", ("key_length: %ld %u", key_length,
1230
(*prev_changed)->key_length));
1235
CHANGED_TABLE_LIST* THD::changed_table_dup(const char *key, long key_length)
1237
CHANGED_TABLE_LIST* new_table =
1238
(CHANGED_TABLE_LIST*) trans_alloc(ALIGN_SIZE(sizeof(CHANGED_TABLE_LIST))+
1242
my_error(EE_OUTOFMEMORY, MYF(ME_BELL),
1243
ALIGN_SIZE(sizeof(TABLE_LIST)) + key_length + 1);
1244
killed= KILL_CONNECTION;
1248
new_table->key= ((char*)new_table)+ ALIGN_SIZE(sizeof(CHANGED_TABLE_LIST));
1249
new_table->next = 0;
1250
new_table->key_length = key_length;
1251
::memcpy(new_table->key, key, key_length);
1256
int THD::send_explain_fields(select_result *result)
1258
List<Item> field_list;
1260
CHARSET_INFO *cs= system_charset_info;
1261
field_list.push_back(new Item_return_int("id",3, MYSQL_TYPE_LONGLONG));
1262
field_list.push_back(new Item_empty_string("select_type", 19, cs));
1263
field_list.push_back(item= new Item_empty_string("table", NAME_CHAR_LEN, cs));
1264
item->maybe_null= 1;
1265
field_list.push_back(item= new Item_empty_string("type", 10, cs));
1266
item->maybe_null= 1;
1267
field_list.push_back(item=new Item_empty_string("possible_keys",
1268
NAME_CHAR_LEN*MAX_KEY, cs));
1270
field_list.push_back(item=new Item_empty_string("key", NAME_CHAR_LEN, cs));
1272
field_list.push_back(item=
1273
new Item_empty_string("key_len",
1275
(MAX_KEY_LENGTH_DECIMAL_WIDTH + 1 /* for comma */),
1278
field_list.push_back(item=new Item_empty_string("ref",
1279
NAME_CHAR_LEN*MAX_REF_PARTS,
1282
field_list.push_back(item= new Item_return_int("rows", 10,
1283
MYSQL_TYPE_LONGLONG));
1284
if (lex->describe & DESCRIBE_EXTENDED)
1286
field_list.push_back(item= new Item_float("filtered", 0.1234, 2, 4));
1289
item->maybe_null= 1;
1290
field_list.push_back(new Item_empty_string("Extra", 255, cs));
1291
return (result->send_fields(field_list,
1292
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF));
1295
#ifdef SIGNAL_WITH_VIO_CLOSE
1296
void THD::close_active_vio()
1298
DBUG_ENTER("close_active_vio");
1299
safe_mutex_assert_owner(&LOCK_delete);
1302
vio_close(active_vio);
1310
struct Item_change_record: public ilink
1314
/* Placement new was hidden by `new' in ilink (TODO: check): */
1315
static void *operator new(size_t size, void *mem) { return mem; }
1316
static void operator delete(void *ptr, size_t size) {}
1317
static void operator delete(void *ptr, void *mem) { /* never called */ }
1322
Register an item tree tree transformation, performed by the query
1323
optimizer. We need a pointer to runtime_memroot because it may be !=
1324
thd->mem_root (due to possible set_n_backup_active_arena called for thd).
1327
void THD::nocheck_register_item_tree_change(Item **place, Item *old_value,
1328
MEM_ROOT *runtime_memroot)
1330
Item_change_record *change;
1332
Now we use one node per change, which adds some memory overhead,
1333
but still is rather fast as we use alloc_root for allocations.
1334
A list of item tree changes of an average query should be short.
1336
void *change_mem= alloc_root(runtime_memroot, sizeof(*change));
1337
if (change_mem == 0)
1340
OOM, thd->fatal_error() is called by the error handler of the
1341
memroot. Just return.
1345
change= new (change_mem) Item_change_record;
1346
change->place= place;
1347
change->old_value= old_value;
1348
change_list.append(change);
1352
void THD::rollback_item_tree_changes()
1354
I_List_iterator<Item_change_record> it(change_list);
1355
Item_change_record *change;
1356
DBUG_ENTER("rollback_item_tree_changes");
1358
while ((change= it++))
1359
*change->place= change->old_value;
1360
/* We can forget about changes memory: it's allocated in runtime memroot */
1361
change_list.empty();
1367
Check that the endpoint is still available.
1370
bool THD::vio_is_connected()
1374
/* End of input is signaled by poll if the socket is aborted. */
1375
if (vio_poll_read(net.vio, 0))
1378
/* Socket is aborted if signaled but no data is available. */
1379
if (vio_peek_read(net.vio, &bytes))
1382
return bytes ? TRUE : FALSE;
1386
/*****************************************************************************
1387
** Functions to provide a interface to select results
1388
*****************************************************************************/
1390
select_result::select_result()
1395
void select_result::send_error(uint errcode,const char *err)
1397
my_message(errcode, err, MYF(0));
1401
void select_result::cleanup()
1406
bool select_result::check_simple_select() const
1408
my_error(ER_SP_BAD_CURSOR_QUERY, MYF(0));
1413
static String default_line_term("\n",default_charset_info);
1414
static String default_escaped("\\",default_charset_info);
1415
static String default_field_term("\t",default_charset_info);
1416
static String default_xml_row_term("<row>", default_charset_info);
1418
sql_exchange::sql_exchange(char *name, bool flag,
1419
enum enum_filetype filetype_arg)
1420
:file_name(name), opt_enclosed(0), dumpfile(flag), skip_lines(0)
1422
filetype= filetype_arg;
1423
field_term= &default_field_term;
1424
enclosed= line_start= &my_empty_string;
1425
line_term= filetype == FILETYPE_CSV ?
1426
&default_line_term : &default_xml_row_term;
1427
escaped= &default_escaped;
1431
bool select_send::send_fields(List<Item> &list, uint flags)
1434
if (!(res= thd->protocol->send_fields(&list, flags)))
1435
is_result_set_started= 1;
1439
void select_send::abort()
1441
DBUG_ENTER("select_send::abort");
1447
Cleanup an instance of this class for re-use
1448
at next execution of a prepared statement/
1449
stored procedure statement.
1452
void select_send::cleanup()
1454
is_result_set_started= FALSE;
1457
/* Send data to client. Returns 0 if ok */
1459
bool select_send::send_data(List<Item> &items)
1461
if (unit->offset_limit_cnt)
1462
{ // using limit offset,count
1463
unit->offset_limit_cnt--;
1468
We may be passing the control from mysqld to the client: release the
1469
InnoDB adaptive hash S-latch to avoid thread deadlocks if it was reserved
1472
ha_release_temporary_latches(thd);
1474
List_iterator_fast<Item> li(items);
1475
Protocol *protocol= thd->protocol;
1476
char buff[MAX_FIELD_WIDTH];
1477
String buffer(buff, sizeof(buff), &my_charset_bin);
1478
DBUG_ENTER("select_send::send_data");
1480
protocol->prepare_for_resend();
1484
if (item->send(protocol, &buffer))
1486
protocol->free(); // Free used buffer
1487
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
1491
thd->sent_row_count++;
1492
if (thd->is_error())
1494
protocol->remove_last_row();
1498
DBUG_RETURN(protocol->write());
1502
bool select_send::send_eof()
1505
We may be passing the control from mysqld to the client: release the
1506
InnoDB adaptive hash S-latch to avoid thread deadlocks if it was reserved
1509
ha_release_temporary_latches(thd);
1511
/* Unlock tables before sending packet to gain some speed */
1514
mysql_unlock_tables(thd, thd->lock);
1518
is_result_set_started= 0;
1523
/************************************************************************
1524
Handling writing to file
1525
************************************************************************/
1527
void select_to_file::send_error(uint errcode,const char *err)
1529
my_message(errcode, err, MYF(0));
1532
(void) end_io_cache(&cache);
1533
(void) my_close(file,MYF(0));
1534
(void) my_delete(path,MYF(0)); // Delete file on error
1540
bool select_to_file::send_eof()
1542
int error= test(end_io_cache(&cache));
1543
if (my_close(file,MYF(MY_WME)))
1548
In order to remember the value of affected rows for ROW_COUNT()
1549
function, SELECT INTO has to have an own SQLCOM.
1550
TODO: split from SQLCOM_SELECT
1552
::my_ok(thd,row_count);
1559
void select_to_file::cleanup()
1561
/* In case of error send_eof() may be not called: close the file here. */
1564
(void) end_io_cache(&cache);
1565
(void) my_close(file,MYF(0));
1573
select_to_file::~select_to_file()
1576
{ // This only happens in case of error
1577
(void) end_io_cache(&cache);
1578
(void) my_close(file,MYF(0));
1583
/***************************************************************************
1584
** Export of select to textfile
1585
***************************************************************************/
1587
select_export::~select_export()
1589
thd->sent_row_count=row_count;
1594
Create file with IO cache
1600
exchange Excange class
1609
static File create_file(THD *thd, char *path, sql_exchange *exchange,
1613
uint option= MY_UNPACK_FILENAME | MY_RELATIVE_PATH;
1615
#ifdef DONT_ALLOW_FULL_LOAD_DATA_PATHS
1616
option|= MY_REPLACE_DIR; // Force use of db directory
1619
if (!dirname_length(exchange->file_name))
1621
strxnmov(path, FN_REFLEN-1, mysql_real_data_home, thd->db ? thd->db : "",
1623
(void) fn_format(path, exchange->file_name, path, "", option);
1626
(void) fn_format(path, exchange->file_name, mysql_real_data_home, "", option);
1628
if (opt_secure_file_priv &&
1629
strncmp(opt_secure_file_priv, path, strlen(opt_secure_file_priv)))
1631
/* Write only allowed to dir or subdir specified by secure_file_priv */
1632
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
1636
if (!access(path, F_OK))
1638
my_error(ER_FILE_EXISTS_ERROR, MYF(0), exchange->file_name);
1641
/* Create the file world readable */
1642
if ((file= my_create(path, 0666, O_WRONLY|O_EXCL, MYF(MY_WME))) < 0)
1645
(void) fchmod(file, 0666); // Because of umask()
1647
(void) chmod(path, 0666);
1649
if (init_io_cache(cache, file, 0L, WRITE_CACHE, 0L, 1, MYF(MY_WME)))
1651
my_close(file, MYF(0));
1652
my_delete(path, MYF(0)); // Delete file on error, it was just created
1660
select_export::prepare(List<Item> &list, SELECT_LEX_UNIT *u)
1663
bool string_results= FALSE, non_string_results= FALSE;
1665
if ((uint) strlen(exchange->file_name) + NAME_LEN >= FN_REFLEN)
1666
strmake(path,exchange->file_name,FN_REFLEN-1);
1668
if ((file= create_file(thd, path, exchange, &cache)) < 0)
1670
/* Check if there is any blobs in data */
1672
List_iterator_fast<Item> li(list);
1676
if (item->max_length >= MAX_BLOB_WIDTH)
1681
if (item->result_type() == STRING_RESULT)
1682
string_results= TRUE;
1684
non_string_results= TRUE;
1687
field_term_length=exchange->field_term->length();
1688
field_term_char= field_term_length ?
1689
(int) (uchar) (*exchange->field_term)[0] : INT_MAX;
1690
if (!exchange->line_term->length())
1691
exchange->line_term=exchange->field_term; // Use this if it exists
1692
field_sep_char= (exchange->enclosed->length() ?
1693
(int) (uchar) (*exchange->enclosed)[0] : field_term_char);
1694
escape_char= (exchange->escaped->length() ?
1695
(int) (uchar) (*exchange->escaped)[0] : -1);
1696
is_ambiguous_field_sep= test(strchr(ESCAPE_CHARS, field_sep_char));
1697
is_unsafe_field_sep= test(strchr(NUMERIC_CHARS, field_sep_char));
1698
line_sep_char= (exchange->line_term->length() ?
1699
(int) (uchar) (*exchange->line_term)[0] : INT_MAX);
1700
if (!field_term_length)
1701
exchange->opt_enclosed=0;
1702
if (!exchange->enclosed->length())
1703
exchange->opt_enclosed=1; // A little quicker loop
1704
fixed_row_size= (!field_term_length && !exchange->enclosed->length() &&
1706
if ((is_ambiguous_field_sep && exchange->enclosed->is_empty() &&
1707
(string_results || is_unsafe_field_sep)) ||
1708
(exchange->opt_enclosed && non_string_results &&
1709
field_term_length && strchr(NUMERIC_CHARS, field_term_char)))
1711
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1712
ER_AMBIGUOUS_FIELD_TERM, ER(ER_AMBIGUOUS_FIELD_TERM));
1713
is_ambiguous_field_term= TRUE;
1716
is_ambiguous_field_term= FALSE;
1722
#define NEED_ESCAPING(x) ((int) (uchar) (x) == escape_char || \
1723
(enclosed ? (int) (uchar) (x) == field_sep_char \
1724
: (int) (uchar) (x) == field_term_char) || \
1725
(int) (uchar) (x) == line_sep_char || \
1728
bool select_export::send_data(List<Item> &items)
1731
DBUG_ENTER("select_export::send_data");
1732
char buff[MAX_FIELD_WIDTH],null_buff[2],space[MAX_FIELD_WIDTH];
1733
bool space_inited=0;
1734
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1737
if (unit->offset_limit_cnt)
1738
{ // using limit offset,count
1739
unit->offset_limit_cnt--;
1744
uint used_length=0,items_left=items.elements;
1745
List_iterator_fast<Item> li(items);
1747
if (my_b_write(&cache,(uchar*) exchange->line_start->ptr(),
1748
exchange->line_start->length()))
1752
Item_result result_type=item->result_type();
1753
bool enclosed = (exchange->enclosed->length() &&
1754
(!exchange->opt_enclosed || result_type == STRING_RESULT));
1755
res=item->str_result(&tmp);
1756
if (res && enclosed)
1758
if (my_b_write(&cache,(uchar*) exchange->enclosed->ptr(),
1759
exchange->enclosed->length()))
1764
if (!fixed_row_size)
1766
if (escape_char != -1) // Use \N syntax
1768
null_buff[0]=escape_char;
1770
if (my_b_write(&cache,(uchar*) null_buff,2))
1773
else if (my_b_write(&cache,(uchar*) "NULL",4))
1778
used_length=0; // Fill with space
1784
used_length=min(res->length(),item->max_length);
1786
used_length=res->length();
1787
if ((result_type == STRING_RESULT || is_unsafe_field_sep) &&
1790
char *pos, *start, *end;
1791
CHARSET_INFO *res_charset= res->charset();
1792
CHARSET_INFO *character_set_client= thd->variables.
1793
character_set_client;
1794
bool check_second_byte= (res_charset == &my_charset_bin) &&
1795
character_set_client->
1796
escape_with_backslash_is_dangerous;
1797
DBUG_ASSERT(character_set_client->mbmaxlen == 2 ||
1798
!character_set_client->escape_with_backslash_is_dangerous);
1799
for (start=pos=(char*) res->ptr(),end=pos+used_length ;
1804
if (use_mb(res_charset))
1807
if ((l=my_ismbchar(res_charset, pos, end)))
1816
Special case when dumping BINARY/VARBINARY/BLOB values
1817
for the clients with character sets big5, cp932, gbk and sjis,
1818
which can have the escape character (0x5C "\" by default)
1819
as the second byte of a multi-byte sequence.
1822
- pos[0] is a valid multi-byte head (e.g 0xEE) and
1823
- pos[1] is 0x00, which will be escaped as "\0",
1825
then we'll get "0xEE + 0x5C + 0x30" in the output file.
1827
If this file is later loaded using this sequence of commands:
1829
mysql> create table t1 (a varchar(128)) character set big5;
1830
mysql> LOAD DATA INFILE 'dump.txt' INTO TABLE t1;
1832
then 0x5C will be misinterpreted as the second byte
1833
of a multi-byte character "0xEE + 0x5C", instead of
1834
escape character for 0x00.
1836
To avoid this confusion, we'll escape the multi-byte
1837
head character too, so the sequence "0xEE + 0x00" will be
1838
dumped as "0x5C + 0xEE + 0x5C + 0x30".
1840
Note, in the condition below we only check if
1841
mbcharlen is equal to 2, because there are no
1842
character sets with mbmaxlen longer than 2
1843
and with escape_with_backslash_is_dangerous set.
1844
DBUG_ASSERT before the loop makes that sure.
1847
if ((NEED_ESCAPING(*pos) ||
1848
(check_second_byte &&
1849
my_mbcharlen(character_set_client, (uchar) *pos) == 2 &&
1851
NEED_ESCAPING(pos[1]))) &&
1853
Don't escape field_term_char by doubling - doubling is only
1854
valid for ENCLOSED BY characters:
1856
(enclosed || !is_ambiguous_field_term ||
1857
(int) (uchar) *pos != field_term_char))
1860
tmp_buff[0]= ((int) (uchar) *pos == field_sep_char &&
1861
is_ambiguous_field_sep) ?
1862
field_sep_char : escape_char;
1863
tmp_buff[1]= *pos ? *pos : '0';
1864
if (my_b_write(&cache,(uchar*) start,(uint) (pos-start)) ||
1865
my_b_write(&cache,(uchar*) tmp_buff,2))
1870
if (my_b_write(&cache,(uchar*) start,(uint) (pos-start)))
1873
else if (my_b_write(&cache,(uchar*) res->ptr(),used_length))
1877
{ // Fill with space
1878
if (item->max_length > used_length)
1880
/* QQ: Fix by adding a my_b_fill() function */
1884
bfill(space,sizeof(space),' ');
1886
uint length=item->max_length-used_length;
1887
for (; length > sizeof(space) ; length-=sizeof(space))
1889
if (my_b_write(&cache,(uchar*) space,sizeof(space)))
1892
if (my_b_write(&cache,(uchar*) space,length))
1896
if (res && enclosed)
1898
if (my_b_write(&cache, (uchar*) exchange->enclosed->ptr(),
1899
exchange->enclosed->length()))
1904
if (my_b_write(&cache, (uchar*) exchange->field_term->ptr(),
1909
if (my_b_write(&cache,(uchar*) exchange->line_term->ptr(),
1910
exchange->line_term->length()))
1918
/***************************************************************************
1919
** Dump of select to a binary file
1920
***************************************************************************/
1924
select_dump::prepare(List<Item> &list __attribute__((unused)),
1928
return (int) ((file= create_file(thd, path, exchange, &cache)) < 0);
1932
bool select_dump::send_data(List<Item> &items)
1934
List_iterator_fast<Item> li(items);
1935
char buff[MAX_FIELD_WIDTH];
1936
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1939
DBUG_ENTER("select_dump::send_data");
1941
if (unit->offset_limit_cnt)
1942
{ // using limit offset,count
1943
unit->offset_limit_cnt--;
1946
if (row_count++ > 1)
1948
my_message(ER_TOO_MANY_ROWS, ER(ER_TOO_MANY_ROWS), MYF(0));
1953
res=item->str_result(&tmp);
1954
if (!res) // If NULL
1956
if (my_b_write(&cache,(uchar*) "",1))
1959
else if (my_b_write(&cache,(uchar*) res->ptr(),res->length()))
1961
my_error(ER_ERROR_ON_WRITE, MYF(0), path, my_errno);
1971
select_subselect::select_subselect(Item_subselect *item_arg)
1977
bool select_singlerow_subselect::send_data(List<Item> &items)
1979
DBUG_ENTER("select_singlerow_subselect::send_data");
1980
Item_singlerow_subselect *it= (Item_singlerow_subselect *)item;
1983
my_message(ER_SUBQUERY_NO_1_ROW, ER(ER_SUBQUERY_NO_1_ROW), MYF(0));
1986
if (unit->offset_limit_cnt)
1987
{ // Using limit offset,count
1988
unit->offset_limit_cnt--;
1991
List_iterator_fast<Item> li(items);
1993
for (uint i= 0; (val_item= li++); i++)
1994
it->store(i, val_item);
2000
void select_max_min_finder_subselect::cleanup()
2002
DBUG_ENTER("select_max_min_finder_subselect::cleanup");
2008
bool select_max_min_finder_subselect::send_data(List<Item> &items)
2010
DBUG_ENTER("select_max_min_finder_subselect::send_data");
2011
Item_maxmin_subselect *it= (Item_maxmin_subselect *)item;
2012
List_iterator_fast<Item> li(items);
2013
Item *val_item= li++;
2014
it->register_value();
2017
cache->store(val_item);
2019
it->store(0, cache);
2025
cache= Item_cache::get_cache(val_item);
2026
switch (val_item->result_type())
2029
op= &select_max_min_finder_subselect::cmp_real;
2032
op= &select_max_min_finder_subselect::cmp_int;
2035
op= &select_max_min_finder_subselect::cmp_str;
2037
case DECIMAL_RESULT:
2038
op= &select_max_min_finder_subselect::cmp_decimal;
2041
// This case should never be choosen
2046
cache->store(val_item);
2047
it->store(0, cache);
2053
bool select_max_min_finder_subselect::cmp_real()
2055
Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
2056
double val1= cache->val_real(), val2= maxmin->val_real();
2058
return (cache->null_value && !maxmin->null_value) ||
2059
(!cache->null_value && !maxmin->null_value &&
2061
return (maxmin->null_value && !cache->null_value) ||
2062
(!cache->null_value && !maxmin->null_value &&
2066
bool select_max_min_finder_subselect::cmp_int()
2068
Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
2069
longlong val1= cache->val_int(), val2= maxmin->val_int();
2071
return (cache->null_value && !maxmin->null_value) ||
2072
(!cache->null_value && !maxmin->null_value &&
2074
return (maxmin->null_value && !cache->null_value) ||
2075
(!cache->null_value && !maxmin->null_value &&
2079
bool select_max_min_finder_subselect::cmp_decimal()
2081
Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
2082
my_decimal cval, *cvalue= cache->val_decimal(&cval);
2083
my_decimal mval, *mvalue= maxmin->val_decimal(&mval);
2085
return (cache->null_value && !maxmin->null_value) ||
2086
(!cache->null_value && !maxmin->null_value &&
2087
my_decimal_cmp(cvalue, mvalue) > 0) ;
2088
return (maxmin->null_value && !cache->null_value) ||
2089
(!cache->null_value && !maxmin->null_value &&
2090
my_decimal_cmp(cvalue,mvalue) < 0);
2093
bool select_max_min_finder_subselect::cmp_str()
2095
String *val1, *val2, buf1, buf2;
2096
Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
2098
as far as both operand is Item_cache buf1 & buf2 will not be used,
2099
but added for safety
2101
val1= cache->val_str(&buf1);
2102
val2= maxmin->val_str(&buf1);
2104
return (cache->null_value && !maxmin->null_value) ||
2105
(!cache->null_value && !maxmin->null_value &&
2106
sortcmp(val1, val2, cache->collation.collation) > 0) ;
2107
return (maxmin->null_value && !cache->null_value) ||
2108
(!cache->null_value && !maxmin->null_value &&
2109
sortcmp(val1, val2, cache->collation.collation) < 0);
2112
bool select_exists_subselect::send_data(List<Item> &items)
2114
DBUG_ENTER("select_exists_subselect::send_data");
2115
Item_exists_subselect *it= (Item_exists_subselect *)item;
2116
if (unit->offset_limit_cnt)
2117
{ // Using limit offset,count
2118
unit->offset_limit_cnt--;
2127
/***************************************************************************
2128
Dump of select to variables
2129
***************************************************************************/
2131
int select_dumpvar::prepare(List<Item> &list, SELECT_LEX_UNIT *u)
2135
if (var_list.elements != list.elements)
2137
my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
2138
ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT), MYF(0));
2145
bool select_dumpvar::check_simple_select() const
2147
my_error(ER_SP_BAD_CURSOR_SELECT, MYF(0));
2152
void select_dumpvar::cleanup()
2158
void Query_arena::free_items()
2161
DBUG_ENTER("Query_arena::free_items");
2162
/* This works because items are allocated with sql_alloc() */
2163
for (; free_list; free_list= next)
2165
next= free_list->next;
2166
free_list->delete_self();
2168
/* Postcondition: free_list is 0 */
2173
void Query_arena::set_query_arena(Query_arena *set)
2175
mem_root= set->mem_root;
2176
free_list= set->free_list;
2181
void Query_arena::cleanup_stmt()
2183
DBUG_ASSERT("Query_arena::cleanup_stmt()" == "not implemented");
2190
Statement::Statement(LEX *lex_arg, MEM_ROOT *mem_root_arg,
2191
enum enum_state state_arg, ulong id_arg)
2192
:Query_arena(mem_root_arg, state_arg),
2194
mark_used_columns(MARK_COLUMNS_READ),
2205
void Statement::set_statement(Statement *stmt)
2208
mark_used_columns= stmt->mark_used_columns;
2211
query_length= stmt->query_length;
2216
Statement::set_n_backup_statement(Statement *stmt, Statement *backup)
2218
DBUG_ENTER("Statement::set_n_backup_statement");
2219
backup->set_statement(this);
2220
set_statement(stmt);
2225
void Statement::restore_backup_statement(Statement *stmt, Statement *backup)
2227
DBUG_ENTER("Statement::restore_backup_statement");
2228
stmt->set_statement(this);
2229
set_statement(backup);
2234
void THD::end_statement()
2236
/* Cleanup SQL processing state to reuse this statement in next query. */
2240
/* Note that free_list is freed in cleanup_after_query() */
2243
Don't free mem_root, as mem_root is freed in the end of dispatch_command
2244
(once for any command).
2249
void THD::set_n_backup_active_arena(Query_arena *set, Query_arena *backup)
2251
DBUG_ENTER("THD::set_n_backup_active_arena");
2252
DBUG_ASSERT(backup->is_backup_arena == FALSE);
2254
backup->set_query_arena(this);
2255
set_query_arena(set);
2257
backup->is_backup_arena= TRUE;
2263
void THD::restore_active_arena(Query_arena *set, Query_arena *backup)
2265
DBUG_ENTER("THD::restore_active_arena");
2266
DBUG_ASSERT(backup->is_backup_arena);
2267
set->set_query_arena(this);
2268
set_query_arena(backup);
2270
backup->is_backup_arena= FALSE;
2275
bool select_dumpvar::send_data(List<Item> &items)
2277
List_iterator_fast<my_var> var_li(var_list);
2278
List_iterator<Item> it(items);
2281
DBUG_ENTER("select_dumpvar::send_data");
2283
if (unit->offset_limit_cnt)
2284
{ // using limit offset,count
2285
unit->offset_limit_cnt--;
2290
my_message(ER_TOO_MANY_ROWS, ER(ER_TOO_MANY_ROWS), MYF(0));
2293
while ((mv= var_li++) && (item= it++))
2297
Item_func_set_user_var *suv= new Item_func_set_user_var(mv->s, item);
2298
suv->fix_fields(thd, 0);
2303
DBUG_RETURN(thd->is_error());
2306
bool select_dumpvar::send_eof()
2309
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2310
ER_SP_FETCH_NO_DATA, ER(ER_SP_FETCH_NO_DATA));
2312
In order to remember the value of affected rows for ROW_COUNT()
2313
function, SELECT INTO has to have an own SQLCOM.
2314
TODO: split from SQLCOM_SELECT
2316
::my_ok(thd,row_count);
2320
/****************************************************************************
2322
****************************************************************************/
2324
void TMP_TABLE_PARAM::init()
2326
DBUG_ENTER("TMP_TABLE_PARAM::init");
2327
DBUG_PRINT("enter", ("this: 0x%lx", (ulong)this));
2328
field_count= sum_func_count= func_count= hidden_field_count= 0;
2329
group_parts= group_length= group_null_parts= 0;
2332
precomputed_group_by= 0;
2333
bit_fields_as_long= 0;
2338
void thd_increment_bytes_sent(ulong length)
2340
THD *thd=current_thd;
2341
if (likely(thd != 0))
2342
{ /* current_thd==0 when close_connection() calls net_send_error() */
2343
thd->status_var.bytes_sent+= length;
2348
void thd_increment_bytes_received(ulong length)
2350
current_thd->status_var.bytes_received+= length;
2354
void thd_increment_net_big_packet_count(ulong length)
2356
current_thd->status_var.net_big_packet_count+= length;
2360
void THD::set_status_var_init()
2362
bzero((char*) &status_var, sizeof(status_var));
2366
void Security_context::init()
2368
host= user= priv_user= ip= 0;
2369
host_or_ip= "connecting host";
2374
void Security_context::destroy()
2376
// If not pointer to constant
2377
if (host != my_localhost)
2384
void Security_context::skip_grants()
2386
/* privileges for the user are unknown everything is allowed */
2387
host_or_ip= (char *)"";
2388
priv_user= (char *)"";
2393
/****************************************************************************
2394
Handling of open and locked tables states.
2396
This is used when we want to open/lock (and then close) some tables when
2397
we already have a set of tables open and locked. We use these methods for
2398
access to mysql.proc table to find definitions of stored routines.
2399
****************************************************************************/
2401
void THD::reset_n_backup_open_tables_state(Open_tables_state *backup)
2403
DBUG_ENTER("reset_n_backup_open_tables_state");
2404
backup->set_open_tables_state(this);
2405
reset_open_tables_state();
2406
state_flags|= Open_tables_state::BACKUPS_AVAIL;
2411
void THD::restore_backup_open_tables_state(Open_tables_state *backup)
2413
DBUG_ENTER("restore_backup_open_tables_state");
2415
Before we will throw away current open tables state we want
2416
to be sure that it was properly cleaned up.
2418
DBUG_ASSERT(open_tables == 0 && temporary_tables == 0 &&
2419
handler_tables == 0 && derived_tables == 0 &&
2420
lock == 0 && locked_tables == 0);
2421
set_open_tables_state(backup);
2426
Check the killed state of a user thread
2427
@param thd user thread
2428
@retval 0 the user thread is active
2429
@retval 1 the user thread has been killed
2431
extern "C" int thd_killed(const MYSQL_THD thd)
2433
return(thd->killed);
2437
Return the thread id of a user thread
2438
@param thd user thread
2441
extern "C" unsigned long thd_get_thread_id(const MYSQL_THD thd)
2443
return((unsigned long)thd->thread_id);
2447
#ifdef INNODB_COMPATIBILITY_HOOKS
2448
extern "C" struct charset_info_st *thd_charset(MYSQL_THD thd)
2450
return(thd->charset());
2453
extern "C" char **thd_query(MYSQL_THD thd)
2455
return(&thd->query);
2458
extern "C" int thd_slave_thread(const MYSQL_THD thd)
2460
return(thd->slave_thread);
2463
extern "C" int thd_non_transactional_update(const MYSQL_THD thd)
2465
return(thd->transaction.all.modified_non_trans_table);
2468
extern "C" int thd_binlog_format(const MYSQL_THD thd)
2470
return (int) thd->variables.binlog_format;
2473
extern "C" void thd_mark_transaction_to_rollback(MYSQL_THD thd, bool all)
2475
mark_transaction_to_rollback(thd, all);
2477
#endif // INNODB_COMPATIBILITY_HOOKS */
2481
Mark transaction to rollback and mark error as fatal to a sub-statement.
2483
@param thd Thread handle
2484
@param all TRUE <=> rollback main transaction.
2487
void mark_transaction_to_rollback(THD *thd, bool all)
2491
thd->is_fatal_sub_stmt_error= TRUE;
2492
thd->transaction_rollback_request= all;
2495
/***************************************************************************
2496
Handling of XA id cacheing
2497
***************************************************************************/
2499
pthread_mutex_t LOCK_xid_cache;
2502
extern "C" uchar *xid_get_hash_key(const uchar *, size_t *, my_bool);
2503
extern "C" void xid_free_hash(void *);
2505
uchar *xid_get_hash_key(const uchar *ptr, size_t *length,
2506
my_bool not_used __attribute__((unused)))
2508
*length=((XID_STATE*)ptr)->xid.key_length();
2509
return ((XID_STATE*)ptr)->xid.key();
2512
void xid_free_hash(void *ptr)
2514
if (!((XID_STATE*)ptr)->in_thd)
2515
my_free((uchar*)ptr, MYF(0));
2518
bool xid_cache_init()
2520
pthread_mutex_init(&LOCK_xid_cache, MY_MUTEX_INIT_FAST);
2521
return hash_init(&xid_cache, &my_charset_bin, 100, 0, 0,
2522
xid_get_hash_key, xid_free_hash, 0) != 0;
2525
void xid_cache_free()
2527
if (hash_inited(&xid_cache))
2529
hash_free(&xid_cache);
2530
pthread_mutex_destroy(&LOCK_xid_cache);
2534
XID_STATE *xid_cache_search(XID *xid)
2536
pthread_mutex_lock(&LOCK_xid_cache);
2537
XID_STATE *res=(XID_STATE *)hash_search(&xid_cache, xid->key(), xid->key_length());
2538
pthread_mutex_unlock(&LOCK_xid_cache);
2543
bool xid_cache_insert(XID *xid, enum xa_states xa_state)
2547
pthread_mutex_lock(&LOCK_xid_cache);
2548
if (hash_search(&xid_cache, xid->key(), xid->key_length()))
2550
else if (!(xs=(XID_STATE *)my_malloc(sizeof(*xs), MYF(MY_WME))))
2554
xs->xa_state=xa_state;
2557
res=my_hash_insert(&xid_cache, (uchar*)xs);
2559
pthread_mutex_unlock(&LOCK_xid_cache);
2564
bool xid_cache_insert(XID_STATE *xid_state)
2566
pthread_mutex_lock(&LOCK_xid_cache);
2567
DBUG_ASSERT(hash_search(&xid_cache, xid_state->xid.key(),
2568
xid_state->xid.key_length())==0);
2569
my_bool res=my_hash_insert(&xid_cache, (uchar*)xid_state);
2570
pthread_mutex_unlock(&LOCK_xid_cache);
2575
void xid_cache_delete(XID_STATE *xid_state)
2577
pthread_mutex_lock(&LOCK_xid_cache);
2578
hash_delete(&xid_cache, (uchar *)xid_state);
2579
pthread_mutex_unlock(&LOCK_xid_cache);
2583
Implementation of interface to write rows to the binary log through the
2584
thread. The thread is responsible for writing the rows it has
2585
inserted/updated/deleted.
2588
#ifndef MYSQL_CLIENT
2591
Template member function for ensuring that there is an rows log
2592
event of the apropriate type before proceeding.
2595
- Events of type 'RowEventT' have the type code 'type_code'.
2598
If a non-NULL pointer is returned, the pending event for thread 'thd' will
2599
be an event of type 'RowEventT' (which have the type code 'type_code')
2600
will either empty or have enough space to hold 'needed' bytes. In
2601
addition, the columns bitmap will be correct for the row, meaning that
2602
the pending event will be flushed if the columns in the event differ from
2603
the columns suppled to the function.
2606
If no error, a non-NULL pending event (either one which already existed or
2607
the newly created one).
2611
template <class RowsEventT> Rows_log_event*
2612
THD::binlog_prepare_pending_rows_event(TABLE* table, uint32 serv_id,
2614
bool is_transactional,
2615
RowsEventT *hint __attribute__((unused)))
2617
DBUG_ENTER("binlog_prepare_pending_rows_event");
2618
/* Pre-conditions */
2619
DBUG_ASSERT(table->s->table_map_id != ~0UL);
2621
/* Fetch the type code for the RowsEventT template parameter */
2622
int const type_code= RowsEventT::TYPE_CODE;
2625
There is no good place to set up the transactional data, so we
2628
if (binlog_setup_trx_data())
2631
Rows_log_event* pending= binlog_get_pending_rows_event();
2633
if (unlikely(pending && !pending->is_valid()))
2637
Check if the current event is non-NULL and a write-rows
2638
event. Also check if the table provided is mapped: if it is not,
2639
then we have switched to writing to a new table.
2640
If there is no pending event, we need to create one. If there is a pending
2641
event, but it's not about the same table id, or not of the same type
2642
(between Write, Update and Delete), or not the same affected columns, or
2643
going to be too big, flush this event to disk and create a new pending
2646
The last test is necessary for the Cluster injector to work
2647
correctly. The reason is that the Cluster can inject two write
2648
rows with different column bitmaps if there is an insert followed
2649
by an update in the same transaction, and these are grouped into a
2650
single epoch/transaction when fed to the injector.
2652
TODO: Fix the code so that the last test can be removed.
2655
pending->server_id != serv_id ||
2656
pending->get_table_id() != table->s->table_map_id ||
2657
pending->get_type_code() != type_code ||
2658
pending->get_data_size() + needed > opt_binlog_rows_event_max_size ||
2659
!bitmap_cmp(pending->get_cols(), table->write_set))
2661
/* Create a new RowsEventT... */
2662
Rows_log_event* const
2663
ev= new RowsEventT(this, table, table->s->table_map_id,
2667
ev->server_id= serv_id; // I don't like this, it's too easy to forget.
2669
flush the pending event and replace it with the newly created
2672
if (unlikely(mysql_bin_log.flush_and_set_pending_rows_event(this, ev)))
2678
DBUG_RETURN(ev); /* This is the new pending event */
2680
DBUG_RETURN(pending); /* This is the current pending event */
2683
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
2685
Instantiate the versions we need, we have -fno-implicit-template as
2688
template Rows_log_event*
2689
THD::binlog_prepare_pending_rows_event(TABLE*, uint32, size_t, bool,
2690
Write_rows_log_event*);
2692
template Rows_log_event*
2693
THD::binlog_prepare_pending_rows_event(TABLE*, uint32, size_t, bool,
2694
Delete_rows_log_event *);
2696
template Rows_log_event*
2697
THD::binlog_prepare_pending_rows_event(TABLE*, uint32, size_t, bool,
2698
Update_rows_log_event *);
2703
Class to handle temporary allocation of memory for row data.
2705
The responsibilities of the class is to provide memory for
2706
packing one or two rows of packed data (depending on what
2707
constructor is called).
2709
In order to make the allocation more efficient for "simple" rows,
2710
i.e., rows that do not contain any blobs, a pointer to the
2711
allocated memory is of memory is stored in the table structure
2712
for simple rows. If memory for a table containing a blob field
2713
is requested, only memory for that is allocated, and subsequently
2714
released when the object is destroyed.
2717
class Row_data_memory {
2720
Build an object to keep track of a block-local piece of memory
2721
for storing a row of data.
2724
Table where the pre-allocated memory is stored.
2727
Length of data that is needed, if the record contain blobs.
2729
Row_data_memory(TABLE *table, size_t const len1)
2733
m_alloc_checked= FALSE;
2735
allocate_memory(table, len1);
2736
m_ptr[0]= has_memory() ? m_memory : 0;
2740
Row_data_memory(TABLE *table, size_t const len1, size_t const len2)
2744
m_alloc_checked= FALSE;
2746
allocate_memory(table, len1 + len2);
2747
m_ptr[0]= has_memory() ? m_memory : 0;
2748
m_ptr[1]= has_memory() ? m_memory + len1 : 0;
2753
if (m_memory != 0 && m_release_memory_on_destruction)
2754
my_free((uchar*) m_memory, MYF(MY_WME));
2758
Is there memory allocated?
2760
@retval true There is memory allocated
2761
@retval false Memory allocation failed
2763
bool has_memory() const {
2765
m_alloc_checked= TRUE;
2767
return m_memory != 0;
2772
DBUG_ASSERT(s < sizeof(m_ptr)/sizeof(*m_ptr));
2773
DBUG_ASSERT(m_ptr[s] != 0);
2774
DBUG_ASSERT(m_alloc_checked == TRUE);
2779
void allocate_memory(TABLE *const table, size_t const total_length)
2781
if (table->s->blob_fields == 0)
2784
The maximum length of a packed record is less than this
2785
length. We use this value instead of the supplied length
2786
when allocating memory for records, since we don't know how
2787
the memory will be used in future allocations.
2789
Since table->s->reclength is for unpacked records, we have
2790
to add two bytes for each field, which can potentially be
2791
added to hold the length of a packed field.
2793
size_t const maxlen= table->s->reclength + 2 * table->s->fields;
2796
Allocate memory for two records if memory hasn't been
2797
allocated. We allocate memory for two records so that it can
2798
be used when processing update rows as well.
2800
if (table->write_row_record == 0)
2801
table->write_row_record=
2802
(uchar *) alloc_root(&table->mem_root, 2 * maxlen);
2803
m_memory= table->write_row_record;
2804
m_release_memory_on_destruction= FALSE;
2808
m_memory= (uchar *) my_malloc(total_length, MYF(MY_WME));
2809
m_release_memory_on_destruction= TRUE;
2814
mutable bool m_alloc_checked;
2816
bool m_release_memory_on_destruction;
2823
int THD::binlog_write_row(TABLE* table, bool is_trans,
2824
uchar const *record)
2826
DBUG_ASSERT(current_stmt_binlog_row_based && mysql_bin_log.is_open());
2829
Pack records into format for transfer. We are allocating more
2830
memory than needed, but that doesn't matter.
2832
Row_data_memory memory(table, max_row_length(table, record));
2833
if (!memory.has_memory())
2834
return HA_ERR_OUT_OF_MEM;
2836
uchar *row_data= memory.slot(0);
2838
size_t const len= pack_row(table, table->write_set, row_data, record);
2840
Rows_log_event* const ev=
2841
binlog_prepare_pending_rows_event(table, server_id, len, is_trans,
2842
static_cast<Write_rows_log_event*>(0));
2844
if (unlikely(ev == 0))
2845
return HA_ERR_OUT_OF_MEM;
2847
return ev->add_row_data(row_data, len);
2850
int THD::binlog_update_row(TABLE* table, bool is_trans,
2851
const uchar *before_record,
2852
const uchar *after_record)
2854
DBUG_ASSERT(current_stmt_binlog_row_based && mysql_bin_log.is_open());
2856
size_t const before_maxlen = max_row_length(table, before_record);
2857
size_t const after_maxlen = max_row_length(table, after_record);
2859
Row_data_memory row_data(table, before_maxlen, after_maxlen);
2860
if (!row_data.has_memory())
2861
return HA_ERR_OUT_OF_MEM;
2863
uchar *before_row= row_data.slot(0);
2864
uchar *after_row= row_data.slot(1);
2866
size_t const before_size= pack_row(table, table->read_set, before_row,
2868
size_t const after_size= pack_row(table, table->write_set, after_row,
2872
Don't print debug messages when running valgrind since they can
2873
trigger false warnings.
2876
DBUG_DUMP("before_record", before_record, table->s->reclength);
2877
DBUG_DUMP("after_record", after_record, table->s->reclength);
2878
DBUG_DUMP("before_row", before_row, before_size);
2879
DBUG_DUMP("after_row", after_row, after_size);
2882
Rows_log_event* const ev=
2883
binlog_prepare_pending_rows_event(table, server_id,
2884
before_size + after_size, is_trans,
2885
static_cast<Update_rows_log_event*>(0));
2887
if (unlikely(ev == 0))
2888
return HA_ERR_OUT_OF_MEM;
2891
ev->add_row_data(before_row, before_size) ||
2892
ev->add_row_data(after_row, after_size);
2895
int THD::binlog_delete_row(TABLE* table, bool is_trans,
2896
uchar const *record)
2898
DBUG_ASSERT(current_stmt_binlog_row_based && mysql_bin_log.is_open());
2901
Pack records into format for transfer. We are allocating more
2902
memory than needed, but that doesn't matter.
2904
Row_data_memory memory(table, max_row_length(table, record));
2905
if (unlikely(!memory.has_memory()))
2906
return HA_ERR_OUT_OF_MEM;
2908
uchar *row_data= memory.slot(0);
2910
DBUG_DUMP("table->read_set", (uchar*) table->read_set->bitmap, (table->s->fields + 7) / 8);
2911
size_t const len= pack_row(table, table->read_set, row_data, record);
2913
Rows_log_event* const ev=
2914
binlog_prepare_pending_rows_event(table, server_id, len, is_trans,
2915
static_cast<Delete_rows_log_event*>(0));
2917
if (unlikely(ev == 0))
2918
return HA_ERR_OUT_OF_MEM;
2920
return ev->add_row_data(row_data, len);
2924
int THD::binlog_flush_pending_rows_event(bool stmt_end)
2926
DBUG_ENTER("THD::binlog_flush_pending_rows_event");
2928
We shall flush the pending event even if we are not in row-based
2929
mode: it might be the case that we left row-based mode before
2930
flushing anything (e.g., if we have explicitly locked tables).
2932
if (!mysql_bin_log.is_open())
2936
Mark the event as the last event of a statement if the stmt_end
2940
if (Rows_log_event *pending= binlog_get_pending_rows_event())
2944
pending->set_flags(Rows_log_event::STMT_END_F);
2945
pending->flags|= LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F;
2946
binlog_table_maps= 0;
2949
error= mysql_bin_log.flush_and_set_pending_rows_event(this, 0);
2957
Member function that will log query, either row-based or
2958
statement-based depending on the value of the 'current_stmt_binlog_row_based'
2959
the value of the 'qtype' flag.
2961
This function should be called after the all calls to ha_*_row()
2962
functions have been issued, but before tables are unlocked and
2966
There shall be no writes to any system table after calling
2967
binlog_query(), so these writes has to be moved to before the call
2968
of binlog_query() for correct functioning.
2970
This is necessesary not only for RBR, but the master might crash
2971
after binlogging the query but before changing the system tables.
2972
This means that the slave and the master are not in the same state
2973
(after the master has restarted), so therefore we have to
2974
eliminate this problem.
2977
Error code, or 0 if no error.
2979
int THD::binlog_query(THD::enum_binlog_query_type qtype, char const *query_arg,
2980
ulong query_len, bool is_trans, bool suppress_use,
2981
THD::killed_state killed_status_arg)
2983
DBUG_ENTER("THD::binlog_query");
2984
DBUG_PRINT("enter", ("qtype: %d query: '%s'", qtype, query_arg));
2985
DBUG_ASSERT(query_arg && mysql_bin_log.is_open());
2987
if (int error= binlog_flush_pending_rows_event(TRUE))
2991
If we are in statement mode and trying to log an unsafe statement,
2992
we should print a warning.
2994
if (lex->is_stmt_unsafe() &&
2995
variables.binlog_format == BINLOG_FORMAT_STMT)
2997
DBUG_ASSERT(this->query != NULL);
2998
push_warning(this, MYSQL_ERROR::WARN_LEVEL_WARN,
2999
ER_BINLOG_UNSAFE_STATEMENT,
3000
ER(ER_BINLOG_UNSAFE_STATEMENT));
3001
if (!(binlog_flags & BINLOG_FLAG_UNSAFE_STMT_PRINTED))
3003
char warn_buf[MYSQL_ERRMSG_SIZE];
3004
my_snprintf(warn_buf, MYSQL_ERRMSG_SIZE, "%s Statement: %s",
3005
ER(ER_BINLOG_UNSAFE_STATEMENT), this->query);
3006
sql_print_warning(warn_buf);
3007
binlog_flags|= BINLOG_FLAG_UNSAFE_STMT_PRINTED;
3012
case THD::ROW_QUERY_TYPE:
3013
if (current_stmt_binlog_row_based)
3015
/* Otherwise, we fall through */
3016
case THD::MYSQL_QUERY_TYPE:
3018
Using this query type is a conveniece hack, since we have been
3019
moving back and forth between using RBR for replication of
3020
system tables and not using it.
3022
Make sure to change in check_table_binlog_row_based() according
3023
to how you treat this.
3025
case THD::STMT_QUERY_TYPE:
3027
The MYSQL_LOG::write() function will set the STMT_END_F flag and
3028
flush the pending rows event if necessary.
3031
Query_log_event qinfo(this, query_arg, query_len, is_trans, suppress_use,
3033
qinfo.flags|= LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F;
3035
Binlog table maps will be irrelevant after a Query_log_event
3036
(they are just removed on the slave side) so after the query
3037
log event is written to the binary log, we pretend that no
3038
table maps were written.
3040
int error= mysql_bin_log.write(&qinfo);
3041
binlog_table_maps= 0;
3046
case THD::QUERY_TYPE_COUNT:
3048
DBUG_ASSERT(0 <= qtype && qtype < QUERY_TYPE_COUNT);
3053
bool Discrete_intervals_list::append(ulonglong start, ulonglong val,
3056
DBUG_ENTER("Discrete_intervals_list::append");
3057
/* first, see if this can be merged with previous */
3058
if ((head == NULL) || tail->merge_if_contiguous(start, val, incr))
3060
/* it cannot, so need to add a new interval */
3061
Discrete_interval *new_interval= new Discrete_interval(start, val, incr);
3062
DBUG_RETURN(append(new_interval));
3067
bool Discrete_intervals_list::append(Discrete_interval *new_interval)
3069
DBUG_ENTER("Discrete_intervals_list::append");
3070
if (unlikely(new_interval == NULL))
3072
DBUG_PRINT("info",("adding new auto_increment interval"));
3074
head= current= new_interval;
3076
tail->next= new_interval;
3082
#endif /* !defined(MYSQL_CLIENT) */