summaryrefslogtreecommitdiff
path: root/sql/sql_class.h
diff options
context:
space:
mode:
Diffstat (limited to 'sql/sql_class.h')
-rw-r--r--sql/sql_class.h1154
1 files changed, 626 insertions, 528 deletions
diff --git a/sql/sql_class.h b/sql/sql_class.h
index 4fac86dc405..7875870bd1a 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -20,25 +20,26 @@
#pragma interface /* gcc class implementation */
#endif
-// TODO: create log.h and move all the log header stuff there
+#include "log.h"
+#include "rpl_tblmap.h"
+
+class Relay_log_info;
class Query_log_event;
class Load_log_event;
class Slave_log_event;
-class Format_description_log_event;
class sp_rcontext;
class sp_cache;
class Lex_input_stream;
+class Rows_log_event;
enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE };
enum enum_ha_read_modes { RFIRST, RNEXT, RPREV, RLAST, RKEY, RNEXT_SAME };
enum enum_duplicates { DUP_ERROR, DUP_REPLACE, DUP_UPDATE };
-enum enum_log_type { LOG_CLOSED, LOG_TO_BE_OPENED, LOG_NORMAL, LOG_NEW, LOG_BIN};
enum enum_delay_key_write { DELAY_KEY_WRITE_NONE, DELAY_KEY_WRITE_ON,
DELAY_KEY_WRITE_ALL };
-
-enum enum_check_fields { CHECK_FIELD_IGNORE, CHECK_FIELD_WARN,
- CHECK_FIELD_ERROR_FOR_NULL };
+enum enum_mark_columns
+{ MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE};
extern char internal_table_name[2];
extern char empty_c_string[1];
@@ -51,118 +52,6 @@ extern const char **errmesg;
#define TC_HEURISTIC_RECOVER_ROLLBACK 2
extern uint tc_heuristic_recover;
-/*
- Transaction Coordinator log - a base abstract class
- for two different implementations
-*/
-class TC_LOG
-{
- public:
- int using_heuristic_recover();
- TC_LOG() {}
- virtual ~TC_LOG() {}
-
- virtual int open(const char *opt_name)=0;
- virtual void close()=0;
- virtual int log_xid(THD *thd, my_xid xid)=0;
- virtual void unlog(ulong cookie, my_xid xid)=0;
-};
-
-class TC_LOG_DUMMY: public TC_LOG // use it to disable the logging
-{
-public:
- TC_LOG_DUMMY() {} /* Remove gcc warning */
- int open(const char *opt_name) { return 0; }
- void close() { }
- int log_xid(THD *thd, my_xid xid) { return 1; }
- void unlog(ulong cookie, my_xid xid) { }
-};
-
-#ifdef HAVE_MMAP
-class TC_LOG_MMAP: public TC_LOG
-{
- public: // only to keep Sun Forte on sol9x86 happy
- typedef enum {
- POOL, // page is in pool
- ERROR, // last sync failed
- DIRTY // new xids added since last sync
- } PAGE_STATE;
-
- private:
- typedef struct st_page {
- struct st_page *next; // page a linked in a fifo queue
- my_xid *start, *end; // usable area of a page
- my_xid *ptr; // next xid will be written here
- int size, free; // max and current number of free xid slots on the page
- int waiters; // number of waiters on condition
- PAGE_STATE state; // see above
- pthread_mutex_t lock; // to access page data or control structure
- pthread_cond_t cond; // to wait for a sync
- } PAGE;
-
- char logname[FN_REFLEN];
- File fd;
- my_off_t file_length;
- uint npages, inited;
- uchar *data;
- struct st_page *pages, *syncing, *active, *pool, *pool_last;
- /*
- note that, e.g. LOCK_active is only used to protect
- 'active' pointer, to protect the content of the active page
- one has to use active->lock.
- Same for LOCK_pool and LOCK_sync
- */
- pthread_mutex_t LOCK_active, LOCK_pool, LOCK_sync;
- pthread_cond_t COND_pool, COND_active;
-
- public:
- TC_LOG_MMAP(): inited(0) {}
- int open(const char *opt_name);
- void close();
- int log_xid(THD *thd, my_xid xid);
- void unlog(ulong cookie, my_xid xid);
- int recover();
-
- private:
- void get_active_from_pool();
- int sync();
- int overflow();
-};
-#else
-#define TC_LOG_MMAP TC_LOG_DUMMY
-#endif
-
-extern TC_LOG *tc_log;
-extern TC_LOG_MMAP tc_log_mmap;
-extern TC_LOG_DUMMY tc_log_dummy;
-
-/* log info errors */
-#define LOG_INFO_EOF -1
-#define LOG_INFO_IO -2
-#define LOG_INFO_INVALID -3
-#define LOG_INFO_SEEK -4
-#define LOG_INFO_MEM -6
-#define LOG_INFO_FATAL -7
-#define LOG_INFO_IN_USE -8
-
-/* bitmap to SQL_LOG::close() */
-#define LOG_CLOSE_INDEX 1
-#define LOG_CLOSE_TO_BE_OPENED 2
-#define LOG_CLOSE_STOP_EVENT 4
-
-struct st_relay_log_info;
-
-typedef struct st_log_info
-{
- char log_file_name[FN_REFLEN];
- my_off_t index_file_offset, index_file_start_offset;
- my_off_t pos;
- bool fatal; // if the purge happens to give us a negative offset
- pthread_mutex_t lock;
- st_log_info():fatal(0) { pthread_mutex_init(&lock, MY_MUTEX_INIT_FAST);}
- ~st_log_info() { pthread_mutex_destroy(&lock);}
-} LOG_INFO;
-
typedef struct st_user_var_events
{
user_var_entry *user_var_event;
@@ -175,188 +64,6 @@ typedef struct st_user_var_events
#define RP_LOCK_LOG_IS_ALREADY_LOCKED 1
#define RP_FORCE_ROTATE 2
-class Log_event;
-
-/*
- TODO split MYSQL_LOG into base MYSQL_LOG and
- MYSQL_QUERY_LOG, MYSQL_SLOW_LOG, MYSQL_BIN_LOG
- most of the code from MYSQL_LOG should be in the MYSQL_BIN_LOG
- only (TC_LOG included)
-
- TODO use mmap instead of IO_CACHE for binlog
- (mmap+fsync is two times faster than write+fsync)
-*/
-
-class MYSQL_LOG: public TC_LOG
-{
- private:
- /* LOCK_log and LOCK_index are inited by init_pthread_objects() */
- pthread_mutex_t LOCK_log, LOCK_index;
- pthread_mutex_t LOCK_prep_xids;
- pthread_cond_t COND_prep_xids;
- pthread_cond_t update_cond;
- ulonglong bytes_written;
- time_t last_time,query_start;
- IO_CACHE log_file;
- IO_CACHE index_file;
- char *name;
- char time_buff[20],db[NAME_LEN+1];
- char log_file_name[FN_REFLEN],index_file_name[FN_REFLEN];
- /*
- The max size before rotation (usable only if log_type == LOG_BIN: binary
- logs and relay logs).
- For a binlog, max_size should be max_binlog_size.
- For a relay log, it should be max_relay_log_size if this is non-zero,
- max_binlog_size otherwise.
- max_size is set in init(), and dynamically changed (when one does SET
- GLOBAL MAX_BINLOG_SIZE|MAX_RELAY_LOG_SIZE) by fix_max_binlog_size and
- fix_max_relay_log_size).
- */
- ulong max_size;
- ulong prepared_xids; /* for tc log - number of xids to remember */
- volatile enum_log_type log_type;
- enum cache_type io_cache_type;
- // current file sequence number for load data infile binary logging
- uint file_id;
- uint open_count; // For replication
- int readers_count;
- bool write_error, inited;
- bool need_start_event;
- /*
- no_auto_events means we don't want any of these automatic events :
- Start/Rotate/Stop. That is, in 4.x when we rotate a relay log, we don't
- want a Rotate_log event to be written to the relay log. When we start a
- relay log etc. So in 4.x this is 1 for relay logs, 0 for binlogs.
- In 5.0 it's 0 for relay logs too!
- */
- bool no_auto_events;
- friend class Log_event;
-
-public:
- /*
- These describe the log's format. This is used only for relay logs.
- _for_exec is used by the SQL thread, _for_queue by the I/O thread. It's
- necessary to have 2 distinct objects, because the I/O thread may be reading
- events in a different format from what the SQL thread is reading (consider
- the case of a master which has been upgraded from 5.0 to 5.1 without doing
- RESET MASTER, or from 4.x to 5.0).
- */
- Format_description_log_event *description_event_for_exec,
- *description_event_for_queue;
-
- MYSQL_LOG();
- /*
- note that there's no destructor ~MYSQL_LOG() !
- The reason is that we don't want it to be automatically called
- on exit() - but only during the correct shutdown process
- */
-
- int open(const char *opt_name);
- void close();
- int log_xid(THD *thd, my_xid xid);
- void unlog(ulong cookie, my_xid xid);
- int recover(IO_CACHE *log, Format_description_log_event *fdle);
- void reset_bytes_written()
- {
- bytes_written = 0;
- }
- void harvest_bytes_written(ulonglong* counter)
- {
-#ifndef DBUG_OFF
- char buf1[22],buf2[22];
-#endif
- DBUG_ENTER("harvest_bytes_written");
- (*counter)+=bytes_written;
- DBUG_PRINT("info",("counter: %s bytes_written: %s", llstr(*counter,buf1),
- llstr(bytes_written,buf2)));
- bytes_written=0;
- DBUG_VOID_RETURN;
- }
- void set_max_size(ulong max_size_arg);
- void signal_update();
- void wait_for_update(THD* thd, bool master_or_slave);
- void set_need_start_event() { need_start_event = 1; }
- void init(enum_log_type log_type_arg,
- enum cache_type io_cache_type_arg,
- bool no_auto_events_arg, ulong max_size);
- void init_pthread_objects();
- void cleanup();
- bool open(const char *log_name,
- enum_log_type log_type,
- const char *new_name,
- enum cache_type io_cache_type_arg,
- bool no_auto_events_arg, ulong max_size,
- bool null_created);
- const char *generate_name(const char *log_name, const char *suffix,
- bool strip_ext, char *buff);
- /* simplified open_xxx wrappers for the gigantic open above */
- bool open_query_log(const char *log_name)
- {
- char buf[FN_REFLEN];
- return open(generate_name(log_name, ".log", 0, buf),
- LOG_NORMAL, 0, WRITE_CACHE, 0, 0, 0);
- }
- bool open_slow_log(const char *log_name)
- {
- char buf[FN_REFLEN];
- return open(generate_name(log_name, "-slow.log", 0, buf),
- LOG_NORMAL, 0, WRITE_CACHE, 0, 0, 0);
- }
- bool open_index_file(const char *index_file_name_arg,
- const char *log_name);
- void new_file(bool need_lock);
- bool write(THD *thd, enum enum_server_command command,
- const char *format, ...) ATTRIBUTE_FORMAT(printf, 4, 5);
- bool write(THD *thd, const char *query, uint query_length,
- time_t query_start=0);
- bool write(Log_event* event_info); // binary log write
- bool write(THD *thd, IO_CACHE *cache, Log_event *commit_event);
-
- void start_union_events(THD *thd, query_id_t query_id_param);
- void stop_union_events(THD *thd);
- bool is_query_in_union(THD *thd, query_id_t query_id_param);
-
- /*
- v stands for vector
- invoked as appendv(buf1,len1,buf2,len2,...,bufn,lenn,0)
- */
- bool appendv(const char* buf,uint len,...);
- bool append(Log_event* ev);
-
- int generate_new_name(char *new_name,const char *old_name);
- void make_log_name(char* buf, const char* log_ident);
- bool is_active(const char* log_file_name);
- int update_log_index(LOG_INFO* linfo, bool need_update_threads);
- void rotate_and_purge(uint flags);
- bool flush_and_sync();
- int purge_logs(const char *to_log, bool included,
- bool need_mutex, bool need_update_threads,
- ulonglong *decrease_log_space);
- int purge_logs_before_date(time_t purge_time);
- int purge_first_log(struct st_relay_log_info* rli, bool included);
- bool reset_logs(THD* thd);
- void close(uint exiting);
-
- // iterating through the log index file
- int find_log_pos(LOG_INFO* linfo, const char* log_name,
- bool need_mutex);
- int find_next_log(LOG_INFO* linfo, bool need_mutex);
- int get_current_log(LOG_INFO* linfo);
- int raw_get_current_log(LOG_INFO* linfo);
- uint next_file_id();
- inline bool is_open() { return log_type != LOG_CLOSED; }
- inline char* get_index_fname() { return index_file_name;}
- inline char* get_log_fname() { return log_file_name; }
- inline char* get_name() { return name; }
- inline pthread_mutex_t* get_log_lock() { return &LOCK_log; }
- inline IO_CACHE* get_log_file() { return &log_file; }
-
- inline void lock_index() { pthread_mutex_lock(&LOCK_index);}
- inline void unlock_index() { pthread_mutex_unlock(&LOCK_index);}
- inline IO_CACHE *get_index_file() { return &index_file;}
- inline uint32 get_open_count() { return open_count; }
-};
-
/*
The COPY_INFO structure is used by INSERT/REPLACE code.
The schema of the row counting by the INSERT/INSERT ... ON DUPLICATE KEY
@@ -387,12 +94,23 @@ typedef struct st_copy_info {
} COPY_INFO;
-class key_part_spec :public Sql_alloc {
+class Key_part_spec :public Sql_alloc {
public:
const char *field_name;
uint length;
- key_part_spec(const char *name,uint len=0) :field_name(name), length(len) {}
- bool operator==(const key_part_spec& other) const;
+ Key_part_spec(const char *name,uint len=0) :field_name(name), length(len) {}
+ bool operator==(const Key_part_spec& other) const;
+ /**
+ Construct a copy of this Key_part_spec. field_name is copied
+ by-pointer as it is known to never change. At the same time
+ 'length' may be reset in mysql_prepare_create_table, and this
+ is why we supply it with a copy.
+
+ @return If out of memory, 0 is returned and an error is set in
+ THD.
+ */
+ Key_part_spec *clone(MEM_ROOT *mem_root) const
+ { return new (mem_root) Key_part_spec(*this); }
};
@@ -403,6 +121,12 @@ public:
enum drop_type type;
Alter_drop(enum drop_type par_type,const char *par_name)
:name(par_name), type(par_type) {}
+ /**
+ Used to make a clone of this object for ALTER/CREATE TABLE
+ @sa comment for Key_part_spec::clone
+ */
+ Alter_drop *clone(MEM_ROOT *mem_root) const
+ { return new (mem_root) Alter_drop(*this); }
};
@@ -412,6 +136,12 @@ public:
Item *def;
Alter_column(const char *par_name,Item *literal)
:name(par_name), def(literal) {}
+ /**
+ Used to make a clone of this object for ALTER/CREATE TABLE
+ @sa comment for Key_part_spec::clone
+ */
+ Alter_column *clone(MEM_ROOT *mem_root) const
+ { return new (mem_root) Alter_column(*this); }
};
@@ -419,24 +149,32 @@ class Key :public Sql_alloc {
public:
enum Keytype { PRIMARY, UNIQUE, MULTIPLE, FULLTEXT, SPATIAL, FOREIGN_KEY};
enum Keytype type;
- enum ha_key_alg algorithm;
- List<key_part_spec> columns;
+ KEY_CREATE_INFO key_create_info;
+ List<Key_part_spec> columns;
const char *name;
bool generated;
- Key(enum Keytype type_par, const char *name_arg, enum ha_key_alg alg_par,
- bool generated_arg, List<key_part_spec> &cols)
- :type(type_par), algorithm(alg_par), columns(cols), name(name_arg),
- generated(generated_arg)
+ Key(enum Keytype type_par, const char *name_arg,
+ KEY_CREATE_INFO *key_info_arg,
+ bool generated_arg, List<Key_part_spec> &cols)
+ :type(type_par), key_create_info(*key_info_arg), columns(cols),
+ name(name_arg), generated(generated_arg)
{}
- ~Key() {}
+ Key(const Key &rhs, MEM_ROOT *mem_root);
+ virtual ~Key() {}
/* Equality comparison of keys (ignoring name) */
friend bool foreign_key_prefix(Key *a, Key *b);
+ /**
+ Used to make a clone of this object for ALTER/CREATE TABLE
+ @sa comment for Key_part_spec::clone
+ */
+ virtual Key *clone(MEM_ROOT *mem_root) const
+ { return new (mem_root) Key(*this, mem_root); }
};
class Table_ident;
-class foreign_key: public Key {
+class Foreign_key: public Key {
public:
enum fk_match_opt { FK_MATCH_UNDEF, FK_MATCH_FULL,
FK_MATCH_PARTIAL, FK_MATCH_SIMPLE};
@@ -444,16 +182,23 @@ public:
FK_OPTION_SET_NULL, FK_OPTION_NO_ACTION, FK_OPTION_DEFAULT};
Table_ident *ref_table;
- List<key_part_spec> ref_columns;
+ List<Key_part_spec> ref_columns;
uint delete_opt, update_opt, match_opt;
- foreign_key(const char *name_arg, List<key_part_spec> &cols,
- Table_ident *table, List<key_part_spec> &ref_cols,
+ Foreign_key(const char *name_arg, List<Key_part_spec> &cols,
+ Table_ident *table, List<Key_part_spec> &ref_cols,
uint delete_opt_arg, uint update_opt_arg, uint match_opt_arg)
- :Key(FOREIGN_KEY, name_arg, HA_KEY_ALG_UNDEF, 0, cols),
+ :Key(FOREIGN_KEY, name_arg, &default_key_create_info, 0, cols),
ref_table(table), ref_columns(ref_cols),
delete_opt(delete_opt_arg), update_opt(update_opt_arg),
match_opt(match_opt_arg)
{}
+ Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root);
+ /**
+ Used to make a clone of this object for ALTER/CREATE TABLE
+ @sa comment for Key_part_spec::clone
+ */
+ virtual Key *clone(MEM_ROOT *mem_root) const
+ { return new (mem_root) Foreign_key(*this, mem_root); }
};
typedef struct st_mysql_lock
@@ -474,29 +219,9 @@ public:
#include "sql_lex.h" /* Must be here */
-/* Needed to be able to have an I_List of char* strings in mysqld.cc. */
-
-class i_string: public ilink
-{
-public:
- char* ptr;
- i_string():ptr(0) { }
- i_string(char* s) : ptr(s) {}
-};
-
-/* needed for linked list of two strings for replicate-rewrite-db */
-class i_string_pair: public ilink
-{
-public:
- char* key;
- char* val;
- i_string_pair():key(0),val(0) { }
- i_string_pair(char* key_arg, char* val_arg) : key(key_arg),val(val_arg) {}
-};
-
-
class Delayed_insert;
class select_result;
+class Time_zone;
#define THD_SENTRY_MAGIC 0xfeedd1ff
#define THD_SENTRY_GONE 0xdeadbeef
@@ -505,22 +230,37 @@ class select_result;
struct system_variables
{
+ /*
+ How dynamically allocated system variables are handled:
+
+ The global_system_variables and max_system_variables are "authoritative"
+ They both should have the same 'version' and 'size'.
+ When attempting to access a dynamic variable, if the session version
+ is out of date, then the session version is updated and realloced if
+ neccessary and bytes copied from global to make up for missing data.
+ */
+ ulong dynamic_variables_version;
+ char* dynamic_variables_ptr;
+ uint dynamic_variables_head; /* largest valid variable offset */
+ uint dynamic_variables_size; /* how many bytes are in use */
+
ulonglong myisam_max_extra_sort_file_size;
ulonglong myisam_max_sort_file_size;
ulonglong max_heap_table_size;
ulonglong tmp_table_size;
+ ulonglong long_query_time;
ha_rows select_limit;
ha_rows max_join_size;
ulong auto_increment_increment, auto_increment_offset;
ulong bulk_insert_buff_size;
ulong join_buff_size;
- ulong long_query_time;
ulong max_allowed_packet;
ulong max_error_count;
ulong max_length_for_sort_data;
ulong max_sort_length;
ulong max_tmp_tables;
ulong max_insert_delayed_threads;
+ ulong min_examined_row_limit;
ulong multi_range_count;
ulong myisam_repair_threads;
ulong myisam_sort_buff_size;
@@ -539,7 +279,7 @@ struct system_variables
ulong read_rnd_buff_size;
ulong div_precincrement;
ulong sortbuff_size;
- ulong table_type;
+ ulong thread_handling;
ulong tx_isolation;
ulong completion_type;
/* Determines which non-standard SQL behaviour should be enabled */
@@ -556,30 +296,37 @@ struct system_variables
ulong trans_prealloc_size;
ulong log_warnings;
ulong group_concat_max_len;
+ ulong ndb_autoincrement_prefetch_sz;
+ ulong ndb_index_stat_cache_entries;
+ ulong ndb_index_stat_update_freq;
+ ulong binlog_format; // binlog format for this thd (see enum_binlog_format)
/*
In slave thread we need to know in behalf of which
thread the query is being run to replicate temp tables properly
*/
- ulong pseudo_thread_id;
+ my_thread_id pseudo_thread_id;
my_bool low_priority_updates;
my_bool new_mode;
+ /*
+ compatibility option:
+ - index usage hints (USE INDEX without a FOR clause) behave as in 5.0
+ */
+ my_bool old_mode;
my_bool query_cache_wlock_invalidate;
my_bool engine_condition_pushdown;
my_bool keep_files_on_create;
-
-#ifdef HAVE_INNOBASE_DB
- my_bool innodb_table_locks;
- my_bool innodb_support_xa;
-#endif /* HAVE_INNOBASE_DB */
-#ifdef HAVE_NDBCLUSTER_DB
- ulong ndb_autoincrement_prefetch_sz;
my_bool ndb_force_send;
+ my_bool ndb_use_copying_alter_table;
my_bool ndb_use_exact_count;
my_bool ndb_use_transactions;
-#endif /* HAVE_NDBCLUSTER_DB */
+ my_bool ndb_index_stat_enable;
+
+ my_bool old_alter_table;
my_bool old_passwords;
+ plugin_ref table_plugin;
+
/* Only charset part of these variables is sensible */
CHARSET_INFO *character_set_filesystem;
CHARSET_INFO *character_set_client;
@@ -600,6 +347,7 @@ struct system_variables
DATE_TIME_FORMAT *datetime_format;
DATE_TIME_FORMAT *time_format;
my_bool sysdate_is_now;
+
};
@@ -641,6 +389,7 @@ typedef struct system_status_var
ulong net_big_packet_count;
ulong opened_tables;
+ ulong opened_shares;
ulong select_full_join_count;
ulong select_full_range_join_count;
ulong select_range_count;
@@ -674,6 +423,9 @@ typedef struct system_status_var
#define last_system_status_var com_stmt_close
+void mark_transaction_to_rollback(THD *thd, bool all);
+
+#ifdef MYSQL_SERVER
void free_tmp_table(THD *thd, TABLE *entry);
@@ -685,7 +437,6 @@ void free_tmp_table(THD *thd, TABLE *entry);
#define INIT_ARENA_DBUG_INFO
#endif
-
class Query_arena
{
public:
@@ -742,24 +493,24 @@ public:
inline bool is_conventional() const
{ return state == CONVENTIONAL_EXECUTION; }
- inline gptr alloc(unsigned int size) { return alloc_root(mem_root,size); }
- inline gptr calloc(unsigned int size)
+ inline void* alloc(size_t size) { return alloc_root(mem_root,size); }
+ inline void* calloc(size_t size)
{
- gptr ptr;
+ void *ptr;
if ((ptr=alloc_root(mem_root,size)))
- bzero((char*) ptr,size);
+ bzero(ptr, size);
return ptr;
}
inline char *strdup(const char *str)
{ return strdup_root(mem_root,str); }
- inline char *strmake(const char *str, uint size)
+ inline char *strmake(const char *str, size_t size)
{ return strmake_root(mem_root,str,size); }
- inline char *memdup(const char *str, uint size)
+ inline void *memdup(const void *str, size_t size)
{ return memdup_root(mem_root,str,size); }
- inline char *memdup_w_gap(const char *str, uint size, uint gap)
+ inline void *memdup_w_gap(const void *str, size_t size, uint gap)
{
- gptr ptr;
- if ((ptr=alloc_root(mem_root,size+gap)))
+ void *ptr;
+ if ((ptr= alloc_root(mem_root,size+gap)))
memcpy(ptr,str,size);
return ptr;
}
@@ -783,7 +534,7 @@ class Server_side_cursor;
- prepared, that is, contain placeholders,
- opened as cursors. We maintain 1 to 1 relationship between
statement and cursor - if user wants to create another cursor for his
- query, we create another statement for it.
+ query, we create another statement for it.
To perform some action with statement we reset THD part to the state of
that statement, do the action, and then save back modified state from THD
to the statement. It will be changed in near future, and Statement will
@@ -802,10 +553,17 @@ public:
ulong id;
/*
- - if set_query_id=1, we set field->query_id for all fields. In that case
- field list can not contain duplicates.
- */
- bool set_query_id;
+ MARK_COLUMNS_NONE: Means mark_used_colums is not set and no indicator to
+ handler of fields used is set
+ MARK_COLUMNS_READ: Means a bit in read set is set to inform handler
+ that the field is to be read. If field list contains
+ duplicates, then thd->dup_field is set to point
+ to the last found duplicate.
+ MARK_COLUMNS_WRITE: Means a bit is set in write set to inform handler
+ that it needs to update this field in write_row
+ and update_row.
+ */
+ enum enum_mark_columns mark_used_columns;
LEX_STRING name; /* name for named prepared statements */
LEX *lex; // parse tree descriptor
@@ -827,7 +585,7 @@ public:
it. We will see the query_length field as either 0, or the right value
for it.
Assuming that the write and read of an n-bit memory field in an n-bit
- computer is atomic, we can avoid races in the above way.
+ computer is atomic, we can avoid races in the above way.
This printing is needed at least in SHOW PROCESSLIST and SHOW INNODB
STATUS.
*/
@@ -835,6 +593,22 @@ public:
uint32 query_length; // current query length
Server_side_cursor *cursor;
+ /**
+ Name of the current (default) database.
+
+ If there is the current (default) database, "db" contains its name. If
+ there is no current (default) database, "db" is NULL and "db_length" is
+ 0. In other words, "db", "db_length" must either be NULL, or contain a
+ valid database name.
+
+ @note this attribute is set and alloced by the slave SQL thread (for
+ the THD of that thread); that thread is (and must remain, for now) the
+ only responsible for freeing this member.
+ */
+
+ char *db;
+ uint db_length;
+
public:
/* This constructor is called for backup statements */
@@ -874,7 +648,7 @@ public:
Statement *find_by_name(LEX_STRING *name)
{
Statement *stmt;
- stmt= (Statement*)hash_search(&names_hash, (byte*)name->str,
+ stmt= (Statement*)hash_search(&names_hash, (uchar*)name->str,
name->length);
return stmt;
}
@@ -884,7 +658,7 @@ public:
if (last_found_statement == 0 || id != last_found_statement->id)
{
Statement *stmt;
- stmt= (Statement *) hash_search(&st_hash, (byte *) &id, sizeof(id));
+ stmt= (Statement *) hash_search(&st_hash, (uchar *) &id, sizeof(id));
if (stmt && stmt->name.str)
return NULL;
last_found_statement= stmt;
@@ -963,6 +737,20 @@ public:
{
return (*priv_host ? priv_host : (char *)"%");
}
+
+ bool set_user(char *user_arg);
+
+#ifndef NO_EMBEDDED_ACCESS_CHECKS
+ bool
+ change_security_context(THD *thd,
+ LEX_STRING *definer_user,
+ LEX_STRING *definer_host,
+ LEX_STRING *db,
+ Security_context **backup);
+
+ void
+ restore_security_context(THD *thd, Security_context *backup);
+#endif
};
@@ -1035,6 +823,14 @@ public:
THD::prelocked_mode for more info.)
*/
MYSQL_LOCK *locked_tables;
+
+ /*
+ CREATE-SELECT keeps an extra lock for the table being
+ created. This field is used to keep the extra lock available for
+ lower level routines, which would otherwise miss that lock.
+ */
+ MYSQL_LOCK *extra_lock;
+
/*
prelocked_mode_type enum and prelocked_mode member are used for
indicating whenever "prelocked mode" is on, and what type of
@@ -1062,11 +858,20 @@ public:
ulong version;
uint current_tablenr;
+ enum enum_flags {
+ BACKUPS_AVAIL = (1U << 0) /* There are backups available */
+ };
+
+ /*
+ Flags with information about the open tables state.
+ */
+ uint state_flags;
+
/*
This constructor serves for creation of Open_tables_state instances
which are used as backup storage.
*/
- Open_tables_state() {};
+ Open_tables_state() : state_flags(0U) { }
Open_tables_state(ulong version_arg);
@@ -1078,8 +883,9 @@ public:
void reset_open_tables_state()
{
open_tables= temporary_tables= handler_tables= derived_tables= 0;
- lock= locked_tables= 0;
+ extra_lock= lock= locked_tables= 0;
prelocked_mode= NON_PRELOCKED;
+ state_flags= 0U;
}
};
@@ -1098,17 +904,33 @@ class Sub_statement_state
{
public:
ulonglong options;
- ulonglong last_insert_id, next_insert_id, current_insert_id;
+ ulonglong first_successful_insert_id_in_prev_stmt;
+ ulonglong first_successful_insert_id_in_cur_stmt, insert_id_for_cur_row;
+ Discrete_interval auto_inc_interval_for_cur_row;
ulonglong limit_found_rows;
ha_rows cuted_fields, sent_row_count, examined_row_count;
ulong client_capabilities;
uint in_sub_stmt;
- bool enable_slow_log, insert_id_used, clear_next_insert_id;
+ bool enable_slow_log;
bool last_insert_id_used;
my_bool no_send_ok;
SAVEPOINT *savepoints;
};
+
+/* Flags for the THD::system_thread variable */
+enum enum_thread_type
+{
+ NON_SYSTEM_THREAD= 0,
+ SYSTEM_THREAD_DELAYED_INSERT= 1,
+ SYSTEM_THREAD_SLAVE_IO= 2,
+ SYSTEM_THREAD_SLAVE_SQL= 4,
+ SYSTEM_THREAD_NDBCLUSTER_BINLOG= 8,
+ SYSTEM_THREAD_EVENT_SCHEDULER= 16,
+ SYSTEM_THREAD_EVENT_WORKER= 32
+};
+
+
/**
This class represents the interface for internal error handlers.
Internal error handlers are exception handlers used by the server
@@ -1162,6 +984,9 @@ class THD :public Statement,
public Open_tables_state
{
public:
+ /* Used to execute base64 coded binlog events in MySQL server */
+ Relay_log_info* rli_fake;
+
/*
Constant for THD::where initialization in the beginning of every query.
@@ -1192,8 +1017,8 @@ public:
NET net; // client connection descriptor
MEM_ROOT warn_root; // For warnings and errors
Protocol *protocol; // Current protocol
- Protocol_simple protocol_simple; // Normal protocol
- Protocol_prep protocol_prep; // Binary protocol
+ Protocol_text protocol_text; // Normal protocol
+ Protocol_binary protocol_binary; // Binary protocol
HASH user_vars; // hash for user variables
String packet; // dynamic buffer for network I/O
String convert_buffer; // buffer for charset conversions
@@ -1201,6 +1026,7 @@ public:
struct rand_struct rand; // used for authentication
struct system_variables variables; // Changeable local variables
struct system_status_var status_var; // Per thread statistic vars
+ struct system_status_var *initial_status_var; /* used by show status */
THR_LOCK_INFO lock_info; // Locking info of this thread
THR_LOCK_OWNER main_lock_id; // To use for conventional queries
THR_LOCK_OWNER *lock_id; // If not main_lock_id, points to
@@ -1214,23 +1040,24 @@ public:
*/
char *thread_stack;
+ /**
+ Currently selected catalog.
+ */
+ char *catalog;
+
/*
- db - currently selected database
- catalog - currently selected catalog
- WARNING: some members of THD (currently 'db', 'catalog' and 'query') are
- set and alloced by the slave SQL thread (for the THD of that thread); that
- thread is (and must remain, for now) the only responsible for freeing these
- 3 members. If you add members here, and you add code to set them in
- replication, don't forget to free_them_and_set_them_to_0 in replication
- properly. For details see the 'err:' label of the handle_slave_sql()
- in sql/slave.cc.
- */
- char *db, *catalog;
+ WARNING: some members of THD (currently 'Statement::db',
+ 'catalog' and 'query') are set and alloced by the slave SQL thread
+ (for the THD of that thread); that thread is (and must remain, for now)
+ the only responsible for freeing these 3 members. If you add members
+ here, and you add code to set them in replication, don't forget to
+ free_them_and_set_them_to_0 in replication properly. For details see
+ the 'err:' label of the handle_slave_sql() in sql/slave.cc.
+ */
+
Security_context main_security_ctx;
Security_context *security_ctx;
- /* remote (peer) port */
- uint16 peer_port;
/*
Points to info-string that we show in SHOW PROCESSLIST
You are supposed to update thd->proc_info only if you have coded
@@ -1238,6 +1065,14 @@ public:
*/
const char *proc_info;
+ /*
+ Used in error messages to tell user in what part of MySQL we found an
+ error. E. g. when where= "having clause", if fix_fields() fails, user
+ will know that the error was in having clause.
+ */
+ const char *where;
+
+ double tmp_double_value; /* Used in set_var.cc */
ulong client_capabilities; /* What the client supports */
ulong max_client_packet_length;
@@ -1245,7 +1080,7 @@ public:
/*
One thread can hold up to one named user-level lock. This variable
points to a lock object if the lock is present. See item_func.cc and
- chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK.
+ chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK.
*/
User_level_lock *ull;
#ifndef DBUG_OFF
@@ -1259,14 +1094,12 @@ public:
enum enum_server_command command;
uint32 server_id;
uint32 file_id; // for LOAD DATA INFILE
- /*
- Used in error messages to tell user in what part of MySQL we found an
- error. E. g. when where= "having clause", if fix_fields() fails, user
- will know that the error was in having clause.
- */
- const char *where;
- time_t start_time,time_after_lock,user_time;
- time_t connect_time,thr_create_time; // track down slow pthread_create
+ /* remote (peer) port */
+ uint16 peer_port;
+ time_t start_time, user_time;
+ ulonglong connect_utime, thr_create_utime; // track down slow pthread_create
+ ulonglong start_utime, utime_after_lock;
+
thr_lock_type update_lock_default;
Delayed_insert *di;
@@ -1275,12 +1108,74 @@ public:
/* container for handler's private per-connection data */
void *ha_data[MAX_HA];
+
+#ifndef MYSQL_CLIENT
+ int binlog_setup_trx_data();
+
+ /*
+ Public interface to write RBR events to the binlog
+ */
+ void binlog_start_trans_and_stmt();
+ int binlog_flush_transaction_cache();
+ void binlog_set_stmt_begin();
+ int binlog_write_table_map(TABLE *table, bool is_transactional);
+ int binlog_write_row(TABLE* table, bool is_transactional,
+ MY_BITMAP const* cols, size_t colcnt,
+ const uchar *buf);
+ int binlog_delete_row(TABLE* table, bool is_transactional,
+ MY_BITMAP const* cols, size_t colcnt,
+ const uchar *buf);
+ int binlog_update_row(TABLE* table, bool is_transactional,
+ MY_BITMAP const* cols, size_t colcnt,
+ const uchar *old_data, const uchar *new_data);
+
+ void set_server_id(uint32 sid) { server_id = sid; }
+
+ /*
+ Member functions to handle pending event for row-level logging.
+ */
+ template <class RowsEventT> Rows_log_event*
+ binlog_prepare_pending_rows_event(TABLE* table, uint32 serv_id,
+ MY_BITMAP const* cols,
+ size_t colcnt,
+ size_t needed,
+ bool is_transactional,
+ RowsEventT* hint);
+ Rows_log_event* binlog_get_pending_rows_event() const;
+ void binlog_set_pending_rows_event(Rows_log_event* ev);
+ int binlog_flush_pending_rows_event(bool stmt_end);
+
+private:
+ uint binlog_table_maps; // Number of table maps currently in the binlog
+
+ enum enum_binlog_flag {
+ BINLOG_FLAG_UNSAFE_STMT_PRINTED,
+ BINLOG_FLAG_COUNT
+ };
+
+ /**
+ Flags with per-thread information regarding the status of the
+ binary log.
+ */
+ uint32 binlog_flags;
+public:
+ uint get_binlog_table_maps() const {
+ return binlog_table_maps;
+ }
+#endif /* MYSQL_CLIENT */
+
+public:
+
struct st_transactions {
SAVEPOINT *savepoints;
THD_TRANS all; // Trans since BEGIN WORK
THD_TRANS stmt; // Trans for current statement
bool on; // see ha_enable_transaction()
+ XID xid; // transaction identifier
+ enum xa_states xa_state; // used by external XA only
XID_STATE xid_state;
+ Rows_log_event *m_pending_rows_event;
+
/*
Tables changed in transaction (that must be invalidated in query cache).
List contain only transactional tables, that not invalidated in query
@@ -1307,9 +1202,9 @@ public:
#endif
}
} transaction;
- Field *dupp_field;
+ Field *dup_field;
#ifndef __WIN__
- sigset_t signals,block_signals;
+ sigset_t signals;
#endif
#ifdef SIGNAL_WITH_VIO_CLOSE
Vio* active_vio;
@@ -1336,35 +1231,137 @@ public:
Note: in the parser, stmt_arena == thd, even for PS/SP.
*/
Query_arena *stmt_arena;
+ /* Tells if LAST_INSERT_ID(#) was called for the current statement */
+ bool arg_of_last_insert_id_function;
/*
- next_insert_id is set on SET INSERT_ID= #. This is used as the next
- generated auto_increment value in handler.cc
+ ALL OVER THIS FILE, "insert_id" means "*automatically generated* value for
+ insertion into an auto_increment column".
*/
- ulonglong next_insert_id;
- /* Remember last next_insert_id to reset it if something went wrong */
- ulonglong prev_insert_id;
-
/*
- At the beginning of the statement last_insert_id holds the first
- generated value of the previous statement. During statement
- execution it is updated to the value just generated, but then
- restored to the value that was generated first, so for the next
- statement it will again be "the first generated value of the
- previous statement".
-
- It may also be set with "LAST_INSERT_ID(expr)" or
- "@@LAST_INSERT_ID= expr", but the effect of such setting will be
- seen only in the next statement.
+ This is the first autogenerated insert id which was *successfully*
+ inserted by the previous statement (exactly, if the previous statement
+ didn't successfully insert an autogenerated insert id, then it's the one
+ of the statement before, etc).
+ It can also be set by SET LAST_INSERT_ID=# or SELECT LAST_INSERT_ID(#).
+ It is returned by LAST_INSERT_ID().
+ */
+ ulonglong first_successful_insert_id_in_prev_stmt;
+ /*
+ Variant of the above, used for storing in statement-based binlog. The
+ difference is that the one above can change as the execution of a stored
+ function progresses, while the one below is set once and then does not
+ change (which is the value which statement-based binlog needs).
*/
- ulonglong last_insert_id;
-
+ ulonglong first_successful_insert_id_in_prev_stmt_for_binlog;
+ /*
+ This is the first autogenerated insert id which was *successfully*
+ inserted by the current statement. It is maintained only to set
+ first_successful_insert_id_in_prev_stmt when statement ends.
+ */
+ ulonglong first_successful_insert_id_in_cur_stmt;
+ /*
+ We follow this logic:
+ - when stmt starts, first_successful_insert_id_in_prev_stmt contains the
+ first insert id successfully inserted by the previous stmt.
+ - as stmt makes progress, handler::insert_id_for_cur_row changes; every
+ time get_auto_increment() is called, auto_inc_intervals_for_binlog is
+ augmented with the reserved interval (if statement-based binlogging).
+ - at first successful insertion of an autogenerated value,
+ first_successful_insert_id_in_cur_stmt is set to
+ handler::insert_id_for_cur_row.
+ - when stmt goes to binlog, auto_inc_intervals_for_binlog is
+ binlogged if non-empty.
+ - when stmt ends, first_successful_insert_id_in_prev_stmt is set to
+ first_successful_insert_id_in_cur_stmt.
+ */
+ /*
+ stmt_depends_on_first_successful_insert_id_in_prev_stmt is set when
+ LAST_INSERT_ID() is used by a statement.
+ If it is set, first_successful_insert_id_in_prev_stmt_for_binlog will be
+ stored in the statement-based binlog.
+ This variable is CUMULATIVE along the execution of a stored function or
+ trigger: if one substatement sets it to 1 it will stay 1 until the
+ function/trigger ends, thus making sure that
+ first_successful_insert_id_in_prev_stmt_for_binlog does not change anymore
+ and is propagated to the caller for binlogging.
+ */
+ bool stmt_depends_on_first_successful_insert_id_in_prev_stmt;
+ /*
+ List of auto_increment intervals reserved by the thread so far, for
+ storage in the statement-based binlog.
+ Note that its minimum is not first_successful_insert_id_in_cur_stmt:
+ assuming a table with an autoinc column, and this happens:
+ INSERT INTO ... VALUES(3);
+ SET INSERT_ID=3; INSERT IGNORE ... VALUES (NULL);
+ then the latter INSERT will insert no rows
+ (first_successful_insert_id_in_cur_stmt == 0), but storing "INSERT_ID=3"
+ in the binlog is still needed; the list's minimum will contain 3.
+ */
+ Discrete_intervals_list auto_inc_intervals_in_cur_stmt_for_binlog;
+ /* Used by replication and SET INSERT_ID */
+ Discrete_intervals_list auto_inc_intervals_forced;
/*
- current_insert_id remembers the first generated value of the
- previous statement, and does not change during statement
- execution. Its value returned from LAST_INSERT_ID() and
- @@LAST_INSERT_ID.
+ There is BUG#19630 where statement-based replication of stored
+ functions/triggers with two auto_increment columns breaks.
+ We however ensure that it works when there is 0 or 1 auto_increment
+ column; our rules are
+ a) on master, while executing a top statement involving substatements,
+ first top- or sub- statement to generate auto_increment values wins the
+ exclusive right to see its values be written to binlog (the write
+ will be done by the statement or its caller), and the losers won't see
+ their values be written to binlog.
+ b) on slave, while replicating a top statement involving substatements,
+ first top- or sub- statement to need to read auto_increment values from
+ the master's binlog wins the exclusive right to read them (so the losers
+ won't read their values from binlog but instead generate on their own).
+ a) implies that we mustn't backup/restore
+ auto_inc_intervals_in_cur_stmt_for_binlog.
+ b) implies that we mustn't backup/restore auto_inc_intervals_forced.
+
+ If there are more than 1 auto_increment columns, then intervals for
+ different columns may mix into the
+ auto_inc_intervals_in_cur_stmt_for_binlog list, which is logically wrong,
+ but there is no point in preventing this mixing by preventing intervals
+ from the secondly inserted column to come into the list, as such
+ prevention would be wrong too.
+ What will happen in the case of
+ INSERT INTO t1 (auto_inc) VALUES(NULL);
+ where t1 has a trigger which inserts into an auto_inc column of t2, is
+ that in binlog we'll store the interval of t1 and the interval of t2 (when
+ we store intervals, soon), then in slave, t1 will use both intervals, t2
+ will use none; if t1 inserts the same number of rows as on master,
+ normally the 2nd interval will not be used by t1, which is fine. t2's
+ values will be wrong if t2's internal auto_increment counter is different
+ from what it was on master (which is likely). In 5.1, in mixed binlogging
+ mode, row-based binlogging is used for such cases where two
+ auto_increment columns are inserted.
+ */
+ inline void record_first_successful_insert_id_in_cur_stmt(ulonglong id_arg)
+ {
+ if (first_successful_insert_id_in_cur_stmt == 0)
+ first_successful_insert_id_in_cur_stmt= id_arg;
+ }
+ inline ulonglong read_first_successful_insert_id_in_prev_stmt(void)
+ {
+ if (!stmt_depends_on_first_successful_insert_id_in_prev_stmt)
+ {
+ /* It's the first time we read it */
+ first_successful_insert_id_in_prev_stmt_for_binlog=
+ first_successful_insert_id_in_prev_stmt;
+ stmt_depends_on_first_successful_insert_id_in_prev_stmt= 1;
+ }
+ return first_successful_insert_id_in_prev_stmt;
+ }
+ /*
+ Used by Intvar_log_event::do_apply_event() and by "SET INSERT_ID=#"
+ (mysqlbinlog). We'll soon add a variant which can take many intervals in
+ argument.
*/
- ulonglong current_insert_id;
+ inline void force_one_auto_inc_interval(ulonglong next_id)
+ {
+ auto_inc_intervals_forced.empty(); // in case of multiple SET INSERT_ID
+ auto_inc_intervals_forced.append(next_id, ULONGLONG_MAX, 0);
+ }
ulonglong limit_found_rows;
ulonglong options; /* Bitmap of states */
@@ -1398,17 +1395,20 @@ public:
update auto-updatable fields (like auto_increment and timestamp).
*/
query_id_t query_id, warn_id;
- ulong thread_id, col_access;
+ ulong col_access;
+#ifdef ERROR_INJECT_SUPPORT
+ ulong error_inject_value;
+#endif
/* Statement id is thread-wide. This counter is used to generate ids */
ulong statement_id_counter;
ulong rand_saved_seed1, rand_saved_seed2;
ulong row_count; // Row counter, mainly for errors and warnings
- long dbug_thread_id;
- pthread_t real_id;
+ pthread_t real_id; /* For debugging */
+ my_thread_id thread_id;
uint tmp_table, global_read_lock;
- uint server_status,open_options,system_thread;
- uint db_length;
+ uint server_status,open_options;
+ enum enum_thread_type system_thread;
uint select_number; //number of select (used for EXPLAIN)
/* variables.transaction_isolation is reset to this after each commit */
enum_tx_isolation session_tx_isolation;
@@ -1431,6 +1431,8 @@ public:
char scramble[SCRAMBLE_LENGTH+1];
bool slave_thread, one_shot_set;
+ /* tells if current statement should binlog row-based(1) or stmt-based(0) */
+ bool current_stmt_binlog_row_based;
bool locked, some_tables_deleted;
bool last_cuted_field;
bool no_errors, password;
@@ -1461,40 +1463,10 @@ public:
*/
bool is_fatal_sub_stmt_error;
bool query_start_used, rand_used, time_zone_used;
-
- /*
- last_insert_id_used is set when current statement calls
- LAST_INSERT_ID() or reads @@LAST_INSERT_ID.
- */
- bool last_insert_id_used;
-
- /*
- last_insert_id_used is set when current statement or any stored
- function called from this statement calls LAST_INSERT_ID() or
- reads @@LAST_INSERT_ID, so that binary log LAST_INSERT_ID_EVENT be
- generated. Required for statement-based binary log for issuing
- "SET LAST_INSERT_ID= #" before "SELECT func()", if func() reads
- LAST_INSERT_ID.
- */
- bool last_insert_id_used_bin_log;
-
- /*
- insert_id_used is set when current statement updates
- THD::last_insert_id, so that binary log INSERT_ID_EVENT be
- generated.
- */
- bool insert_id_used;
-
- /*
- clear_next_insert_id is set if engine was called at least once
- for this statement to generate auto_increment value.
- */
- bool clear_next_insert_id;
/* for IS NULL => = last_insert_id() fix in remove_eq_conds() */
bool substitute_null_with_insert_id;
bool in_lock_tables;
bool query_error, bootstrap, cleanup_done;
- bool tmp_table_used;
/** is set if some thread specific value(s) used in a statement. */
bool thread_specific_used;
@@ -1522,14 +1494,15 @@ public:
/* Used by the sys_var class to store temporary values */
union
{
- my_bool my_bool_value;
- long long_value;
- ulong ulong_value;
+ my_bool my_bool_value;
+ long long_value;
+ ulong ulong_value;
+ ulonglong ulonglong_value;
} sys_var_tmp;
-
+
struct {
- /*
- If true, mysql_bin_log::write(Log_event) call will not write events to
+ /*
+ If true, mysql_bin_log::write(Log_event) call will not write events to
binlog, and maintain 2 below variables instead (use
mysql_bin_log.start_union_events to turn this on)
*/
@@ -1540,13 +1513,13 @@ public:
*/
bool unioned_events;
/*
- If TRUE, at least one mysql_bin_log::write(Log_event e), where
- e.cache_stmt == TRUE call has been made after last
+ If TRUE, at least one mysql_bin_log::write(Log_event e), where
+ e.cache_stmt == TRUE call has been made after last
mysql_bin_log.start_union_events() call.
*/
bool unioned_events_trans;
-
- /*
+
+ /*
'queries' (actually SP statements) that run under inside this binlog
union have thd->query_id >= first_query_id.
*/
@@ -1562,6 +1535,10 @@ public:
*/
Lex_input_stream *m_lip;
+#ifdef WITH_PARTITION_STORAGE_ENGINE
+ partition_info *work_part_info;
+#endif
+
THD();
~THD();
@@ -1573,7 +1550,7 @@ public:
killing mysqld) where it's vital to not allocate excessive and not used
memory. Note, that we still don't return error from init_for_queries():
if preallocation fails, we should notice that at the first call to
- alloc_root.
+ alloc_root.
*/
void init_for_queries();
void change_user(void);
@@ -1596,6 +1573,33 @@ public:
void close_active_vio();
#endif
void awake(THD::killed_state state_to_set);
+
+#ifndef MYSQL_CLIENT
+ enum enum_binlog_query_type {
+ /*
+ The query can be logged row-based or statement-based
+ */
+ ROW_QUERY_TYPE,
+
+ /*
+ The query has to be logged statement-based
+ */
+ STMT_QUERY_TYPE,
+
+ /*
+ The query represents a change to a table in the "mysql"
+ database and is currently mapped to ROW_QUERY_TYPE.
+ */
+ MYSQL_QUERY_TYPE,
+ QUERY_TYPE_COUNT
+ };
+
+ int binlog_query(enum_binlog_query_type qtype,
+ char const *query, ulong query_len,
+ bool is_trans, bool suppress_use,
+ THD::killed_state killed_err_arg= THD::KILLED_NO_VALUE);
+#endif
+
/*
For enter_cond() / exit_cond() to work the mutex must be got before
enter_cond(); this mutex is then released by exit_cond().
@@ -1626,33 +1630,25 @@ public:
proc_info = old_msg;
pthread_mutex_unlock(&mysys_var->mutex);
}
-
- static inline void safe_time(time_t *t)
+ inline time_t query_start() { query_start_used=1; return start_time; }
+ inline void set_time()
{
- /**
- Wrapper around time() which retries on error (-1)
-
- @details
- This is needed because, despite the documentation, time() may fail
- in some circumstances. Here we retry time() until it succeeds, and
- log the failure so that performance problems related to this can be
- identified.
- */
- while(unlikely(time(t) == ((time_t) -1)))
- sql_print_information("time() failed with %d", errno);
+ if (user_time)
+ {
+ start_time= user_time;
+ start_utime= utime_after_lock= my_micro_time();
+ }
+ else
+ start_utime= utime_after_lock= my_micro_time_and_time(&start_time);
}
-
- inline time_t query_start() { query_start_used=1; return start_time; }
- inline void set_time() { if (user_time) start_time=time_after_lock=user_time; else { safe_time(&start_time); time_after_lock= start_time; }}
- inline void end_time() { safe_time(&start_time); }
- inline void set_time(time_t t) { time_after_lock=start_time=user_time=t; }
- inline void lock_time() { safe_time(&time_after_lock); }
- inline void insert_id(ulonglong id_arg)
+ inline void set_current_time() { start_time= my_time(MY_WME); }
+ inline void set_time(time_t t)
{
- last_insert_id= id_arg;
- insert_id_used=1;
- substitute_null_with_insert_id= TRUE;
+ start_time= user_time= t;
+ start_utime= utime_after_lock= my_micro_time();
}
+ void set_time_after_lock() { utime_after_lock= my_micro_time(); }
+ ulonglong current_utime() { return my_micro_time(); }
inline ulonglong found_rows(void)
{
return limit_found_rows;
@@ -1669,11 +1665,19 @@ public:
{
return !stmt_arena->is_stmt_prepare() && !lex->only_view_structure();
}
- inline gptr trans_alloc(unsigned int size)
+ inline bool fill_information_schema_tables()
+ {
+ return !stmt_arena->is_stmt_prepare();
+ }
+ inline void* trans_alloc(unsigned int size)
{
return alloc_root(&transaction.mem_root,size);
}
+ LEX_STRING *make_lex_string(LEX_STRING *lex_str,
+ const char* str, uint length,
+ bool allocate_lex_string);
+
bool convert_string(LEX_STRING *to, CHARSET_INFO *to_cs,
const char *from, uint from_length,
CHARSET_INFO *from_cs);
@@ -1687,10 +1691,12 @@ public:
#ifndef EMBEDDED_LIBRARY
inline void clear_error()
{
+ DBUG_ENTER("clear_error");
net.last_error[0]= 0;
net.last_errno= 0;
net.report_error= 0;
query_error= 0;
+ DBUG_VOID_RETURN;
}
inline bool vio_ok() const { return net.vio != 0; }
#else
@@ -1764,13 +1770,78 @@ public:
void set_n_backup_active_arena(Query_arena *set, Query_arena *backup);
void restore_active_arena(Query_arena *set, Query_arena *backup);
- /*
- Initialize the current database from a NULL-terminated string with length
- If we run out of memory, we free the current database and return TRUE.
- This way the user will notice the error as there will be no current
- database selected (in addition to the error message set by malloc).
+ inline void set_current_stmt_binlog_row_based_if_mixed()
+ {
+ /*
+ If in a stored/function trigger, the caller should already have done the
+ change. We test in_sub_stmt to prevent introducing bugs where people
+ wouldn't ensure that, and would switch to row-based mode in the middle
+ of executing a stored function/trigger (which is too late, see also
+ reset_current_stmt_binlog_row_based()); this condition will make their
+ tests fail and so force them to propagate the
+ lex->binlog_row_based_if_mixed upwards to the caller.
+ */
+ if ((variables.binlog_format == BINLOG_FORMAT_MIXED) &&
+ (in_sub_stmt == 0))
+ current_stmt_binlog_row_based= TRUE;
+ }
+ inline void set_current_stmt_binlog_row_based()
+ {
+ current_stmt_binlog_row_based= TRUE;
+ }
+ inline void clear_current_stmt_binlog_row_based()
+ {
+ current_stmt_binlog_row_based= FALSE;
+ }
+ inline void reset_current_stmt_binlog_row_based()
+ {
+ /*
+ If there are temporary tables, don't reset back to
+ statement-based. Indeed it could be that:
+ CREATE TEMPORARY TABLE t SELECT UUID(); # row-based
+ # and row-based does not store updates to temp tables
+ # in the binlog.
+ INSERT INTO u SELECT * FROM t; # stmt-based
+ and then the INSERT will fail as data inserted into t was not logged.
+ So we continue with row-based until the temp table is dropped.
+ If we are in a stored function or trigger, we mustn't reset in the
+ middle of its execution (as the binary logging way of a stored function
+ or trigger is decided when it starts executing, depending for example on
+ the caller (for a stored function: if caller is SELECT or
+ INSERT/UPDATE/DELETE...).
+
+ Don't reset binlog format for NDB binlog injector thread.
+ */
+ if ((temporary_tables == NULL) && (in_sub_stmt == 0) &&
+ (system_thread != SYSTEM_THREAD_NDBCLUSTER_BINLOG))
+ {
+ current_stmt_binlog_row_based=
+ test(variables.binlog_format == BINLOG_FORMAT_ROW);
+ }
+ }
+
+ /**
+ Set the current database; use deep copy of C-string.
+
+ @param new_db a pointer to the new database name.
+ @param new_db_len length of the new database name.
+
+ Initialize the current database from a NULL-terminated string with
+ length. If we run out of memory, we free the current database and
+ return TRUE. This way the user will notice the error as there will be
+ no current database selected (in addition to the error message set by
+ malloc).
+
+ @note This operation just sets {db, db_length}. Switching the current
+ database usually involves other actions, like switching other database
+ attributes including security context. In the future, this operation
+ will be made private and more convenient interface will be provided.
+
+ @return Operation status
+ @retval FALSE Success
+ @retval TRUE Out-of-memory error
*/
- bool set_db(const char *new_db, uint new_db_len)
+ bool set_db(const char *new_db, size_t new_db_len)
{
/* Do not reallocate memory if current chunk is big enough. */
if (db && new_db && db_length >= new_db_len)
@@ -1778,13 +1849,24 @@ public:
else
{
x_free(db);
- db= new_db ? my_strdup_with_length(new_db, new_db_len, MYF(MY_WME)) :
- NULL;
+ db= new_db ? my_strndup(new_db, new_db_len, MYF(MY_WME)) : NULL;
}
db_length= db ? new_db_len : 0;
return new_db && !db;
}
- void reset_db(char *new_db, uint new_db_len)
+
+ /**
+ Set the current database; use shallow copy of C-string.
+
+ @param new_db a pointer to the new database name.
+ @param new_db_len length of the new database name.
+
+ @note This operation just sets {db, db_length}. Switching the current
+ database usually involves other actions, like switching other database
+ attributes including security context. In the future, this operation
+ will be made private and more convenient interface will be provided.
+ */
+ void reset_db(char *new_db, size_t new_db_len)
{
db= new_db;
db_length= new_db_len;
@@ -1794,7 +1876,7 @@ public:
allocate memory for a deep copy: current database may be freed after
a statement is parsed but before it's executed.
*/
- bool copy_db_to(char **p_db, uint *p_db_length)
+ bool copy_db_to(char **p_db, size_t *p_db_length)
{
if (db == NULL)
{
@@ -1802,10 +1884,10 @@ public:
return TRUE;
}
*p_db= strmake(db, db_length);
- if (p_db_length)
- *p_db_length= db_length;
+ *p_db_length= db_length;
return FALSE;
}
+ thd_scheduler scheduler;
public:
/**
@@ -1856,10 +1938,6 @@ private:
#define reenable_binlog(A) (A)->options= tmp_disable_binlog__save_options;}
-/* Flags for the THD::system_thread (bitmap) variable */
-#define SYSTEM_THREAD_DELAYED_INSERT 1
-#define SYSTEM_THREAD_SLAVE_IO 2
-#define SYSTEM_THREAD_SLAVE_SQL 4
/*
Used to hold information about file and file structure in exchange
@@ -2026,7 +2104,7 @@ class select_insert :public select_result_interceptor {
TABLE_LIST *table_list;
TABLE *table;
List<Item> *fields;
- ulonglong last_insert_id;
+ ulonglong autoinc_value_of_last_inserted_row; // autogenerated or not
COPY_INFO info;
bool insert_into_view;
@@ -2039,6 +2117,7 @@ class select_insert :public select_result_interceptor {
int prepare2(void);
bool send_data(List<Item> &items);
virtual void store_values(List<Item> &values);
+ virtual bool can_rollback_data() { return 0; }
void send_error(uint errcode,const char *err);
bool send_eof();
void abort();
@@ -2051,32 +2130,39 @@ class select_create: public select_insert {
ORDER *group;
TABLE_LIST *create_table;
HA_CREATE_INFO *create_info;
+ TABLE_LIST *select_tables;
Alter_info *alter_info;
- MYSQL_LOCK *lock;
Field **field;
public:
- select_create(TABLE_LIST *table_arg,
- HA_CREATE_INFO *create_info_arg,
- Alter_info *alter_info_arg,
- List<Item> &select_fields,
- enum_duplicates duplic, bool ignore)
- :select_insert(NULL, NULL, &select_fields, 0, 0, duplic, ignore),
+ select_create (TABLE_LIST *table_arg,
+ HA_CREATE_INFO *create_info_par,
+ Alter_info *alter_info_arg,
+ List<Item> &select_fields,enum_duplicates duplic, bool ignore,
+ TABLE_LIST *select_tables_arg)
+ :select_insert (NULL, NULL, &select_fields, 0, 0, duplic, ignore),
create_table(table_arg),
- create_info(create_info_arg),
- alter_info(alter_info_arg),
- lock(0)
- {}
+ create_info(create_info_par),
+ select_tables(select_tables_arg),
+ alter_info(alter_info_arg)
+ {}
int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
+
+ void binlog_show_create_table(TABLE **tables, uint count);
void store_values(List<Item> &values);
void send_error(uint errcode,const char *err);
bool send_eof();
void abort();
+ virtual bool can_rollback_data() { return 1; }
+
+ // Needed for access from local class MY_HOOKS in prepare(), since thd is proteted.
+ const THD *get_thd(void) { return thd; }
+ const HA_CREATE_INFO *get_create_info() { return create_info; };
};
#include <myisam.h>
-/*
- Param to create temporary tables when doing SELECT:s
+/*
+ Param to create temporary tables when doing SELECT:s
NOTE
This structure is copied using memcpy as a part of JOIN.
*/
@@ -2093,7 +2179,7 @@ public:
List<Item> save_copy_funcs;
Copy_field *copy_field, *copy_field_end;
Copy_field *save_copy_field, *save_copy_field_end;
- byte *group_buff;
+ uchar *group_buff;
Item **items_to_copy; /* Fields in tmp table */
MI_COLUMNDEF *recinfo,*start_recinfo;
KEY *keyinfo;
@@ -2104,8 +2190,8 @@ public:
uint quick_group;
bool using_indirect_summary_function;
/* If >0 convert all blob fields to varchar(convert_blob_length) */
- uint convert_blob_length;
- CHARSET_INFO *table_charset;
+ uint convert_blob_length;
+ CHARSET_INFO *table_charset;
bool schema_table;
/*
True if GROUP BY and its aggregate functions are already computed
@@ -2239,7 +2325,7 @@ public:
else
db= db_arg;
}
- inline Table_ident(LEX_STRING table_arg)
+ inline Table_ident(LEX_STRING table_arg)
:table(table_arg), sel((SELECT_LEX_UNIT *)0)
{
db.str=0;
@@ -2285,7 +2371,7 @@ class user_var_entry
};
/*
- Unique -- class for unique (removing of duplicates).
+ Unique -- class for unique (removing of duplicates).
Puts all values to the TREE. If the tree becomes too big,
it's dumped to the file. User can request sorted values, or
just iterate through them. In the last case tree merging is performed in
@@ -2299,7 +2385,7 @@ class Unique :public Sql_alloc
ulonglong max_in_memory_size;
IO_CACHE file;
TREE tree;
- byte *record_pointers;
+ uchar *record_pointers;
bool flush();
uint size;
@@ -2319,9 +2405,9 @@ public:
}
bool get(TABLE *table);
- static double get_use_cost(uint *buffer, uint nkeys, uint key_size,
+ static double get_use_cost(uint *buffer, uint nkeys, uint key_size,
ulonglong max_in_memory_size);
- inline static int get_cost_calc_buff_size(ulong nkeys, uint key_size,
+ inline static int get_cost_calc_buff_size(ulong nkeys, uint key_size,
ulonglong max_in_memory_size)
{
register ulonglong max_elems_in_tree=
@@ -2332,8 +2418,8 @@ public:
void reset();
bool walk(tree_walk_action action, void *walk_action_arg);
- friend int unique_write_to_file(gptr key, element_count count, Unique *unique);
- friend int unique_write_to_ptrs(gptr key, element_count count, Unique *unique);
+ friend int unique_write_to_file(uchar* key, element_count count, Unique *unique);
+ friend int unique_write_to_ptrs(uchar* key, element_count count, Unique *unique);
};
@@ -2431,8 +2517,20 @@ public:
void cleanup();
};
+/* Bits in sql_command_flags */
+
+#define CF_CHANGES_DATA 1
+#define CF_HAS_ROW_COUNT 2
+#define CF_STATUS_COMMAND 4
+#define CF_SHOW_TABLE_COMMAND 8
+#define CF_WRITE_LOGS_COMMAND 16
+
/* Functions in sql_class.cc */
void add_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var);
+
+void add_diff_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var,
+ STATUS_VAR *dec_var);
void mark_transaction_to_rollback(THD *thd, bool all);
+#endif /* MYSQL_SERVER */