~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.h

  • Committer: jay
  • Date: 2008-12-23 00:18:10 UTC
  • Revision ID: jay@piggy.tangent.org-20081223001810-026ibij22q2842k1
Had a --regex-replace by accident. Should have been --replace_column call.  Only showed up in make test, not running single test, because InnoDB key numbers were different with multiple test running.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 */
19
19
 
20
20
 
 
21
#ifndef DRIZZLED_SESSION_H
 
22
#define DRIZZLED_SESSION_H
 
23
 
21
24
/* Classes in mysql */
 
25
 
22
26
#include <drizzled/global.h>
23
 
#include "log.h"
24
 
#include "rpl_tblmap.h"
 
27
#include <drizzled/log.h>
 
28
#include <drizzled/replication/tblmap.h>
 
29
#include <drizzled/protocol.h>
 
30
#include <libdrizzle/password.h>     // rand_struct
 
31
#include <drizzled/sql_locale.h>
 
32
#include <drizzled/scheduler.h>
 
33
#include <drizzled/ha_trx_info.h>
 
34
#include <mysys/my_tree.h>
 
35
#include <drizzled/handler.h>
 
36
#include <drizzled/current_session.h>
 
37
#include <drizzled/sql_error.h>
 
38
#include <drizzled/query_arena.h>
 
39
#include <string>
 
40
#include <bitset>
25
41
 
26
42
class Relay_log_info;
27
43
 
30
46
class Slave_log_event;
31
47
class Lex_input_stream;
32
48
class Rows_log_event;
 
49
class user_var_entry;
 
50
class Copy_field;
 
51
class Table_ident;
33
52
 
34
 
enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE };
35
 
enum enum_ha_read_modes { RFIRST, RNEXT, RPREV, RLAST, RKEY, RNEXT_SAME };
36
 
enum enum_duplicates { DUP_ERROR, DUP_REPLACE, DUP_UPDATE };
37
 
enum enum_delay_key_write { DELAY_KEY_WRITE_NONE, DELAY_KEY_WRITE_ON,
38
 
                            DELAY_KEY_WRITE_ALL };
39
 
enum enum_slave_exec_mode { SLAVE_EXEC_MODE_STRICT,
40
 
                            SLAVE_EXEC_MODE_IDEMPOTENT,
41
 
                            SLAVE_EXEC_MODE_LAST_BIT};
42
 
enum enum_mark_columns
43
 
{ MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE};
44
 
enum enum_filetype { FILETYPE_CSV, FILETYPE_XML };
45
53
 
46
54
extern char internal_table_name[2];
47
55
extern char empty_c_string[1];
95
103
} COPY_INFO;
96
104
 
97
105
 
98
 
class Key_part_spec :public Sql_alloc {
99
 
public:
100
 
  LEX_STRING field_name;
101
 
  uint32_t length;
102
 
  Key_part_spec(const LEX_STRING &name, uint32_t len)
103
 
    : field_name(name), length(len)
104
 
  {}
105
 
  Key_part_spec(const char *name, const size_t name_len, uint32_t len)
106
 
    : length(len)
107
 
  { field_name.str= (char *)name; field_name.length= name_len; }
108
 
  bool operator==(const Key_part_spec& other) const;
109
 
  /**
110
 
    Construct a copy of this Key_part_spec. field_name is copied
111
 
    by-pointer as it is known to never change. At the same time
112
 
    'length' may be reset in mysql_prepare_create_table, and this
113
 
    is why we supply it with a copy.
114
 
 
115
 
    @return If out of memory, 0 is returned and an error is set in
116
 
    THD.
117
 
  */
118
 
  Key_part_spec *clone(MEM_ROOT *mem_root) const
119
 
  { return new (mem_root) Key_part_spec(*this); }
120
 
};
121
 
 
122
 
 
123
 
class Alter_drop :public Sql_alloc {
124
 
public:
125
 
  enum drop_type {KEY, COLUMN };
126
 
  const char *name;
127
 
  enum drop_type type;
128
 
  Alter_drop(enum drop_type par_type,const char *par_name)
129
 
    :name(par_name), type(par_type) {}
130
 
  /**
131
 
    Used to make a clone of this object for ALTER/CREATE TABLE
132
 
    @sa comment for Key_part_spec::clone
133
 
  */
134
 
  Alter_drop *clone(MEM_ROOT *mem_root) const
135
 
    { return new (mem_root) Alter_drop(*this); }
136
 
};
137
 
 
138
 
 
139
 
class Alter_column :public Sql_alloc {
140
 
public:
141
 
  const char *name;
142
 
  Item *def;
143
 
  Alter_column(const char *par_name,Item *literal)
144
 
    :name(par_name), def(literal) {}
145
 
  /**
146
 
    Used to make a clone of this object for ALTER/CREATE TABLE
147
 
    @sa comment for Key_part_spec::clone
148
 
  */
149
 
  Alter_column *clone(MEM_ROOT *mem_root) const
150
 
    { return new (mem_root) Alter_column(*this); }
151
 
};
152
 
 
153
 
 
154
 
class Key :public Sql_alloc {
155
 
public:
156
 
  enum Keytype { PRIMARY, UNIQUE, MULTIPLE, FOREIGN_KEY};
157
 
  enum Keytype type;
158
 
  KEY_CREATE_INFO key_create_info;
159
 
  List<Key_part_spec> columns;
160
 
  LEX_STRING name;
161
 
  bool generated;
162
 
 
163
 
  Key(enum Keytype type_par, const LEX_STRING &name_arg,
164
 
      KEY_CREATE_INFO *key_info_arg,
165
 
      bool generated_arg, List<Key_part_spec> &cols)
166
 
    :type(type_par), key_create_info(*key_info_arg), columns(cols),
167
 
    name(name_arg), generated(generated_arg)
168
 
  {}
169
 
  Key(enum Keytype type_par, const char *name_arg, size_t name_len_arg,
170
 
      KEY_CREATE_INFO *key_info_arg, bool generated_arg,
171
 
      List<Key_part_spec> &cols)
172
 
    :type(type_par), key_create_info(*key_info_arg), columns(cols),
173
 
    generated(generated_arg)
174
 
  {
175
 
    name.str= (char *)name_arg;
176
 
    name.length= name_len_arg;
177
 
  }
178
 
  Key(const Key &rhs, MEM_ROOT *mem_root);
179
 
  virtual ~Key() {}
180
 
  /* Equality comparison of keys (ignoring name) */
181
 
  friend bool foreign_key_prefix(Key *a, Key *b);
182
 
  /**
183
 
    Used to make a clone of this object for ALTER/CREATE TABLE
184
 
    @sa comment for Key_part_spec::clone
185
 
  */
186
 
  virtual Key *clone(MEM_ROOT *mem_root) const
187
 
    { return new (mem_root) Key(*this, mem_root); }
188
 
};
189
 
 
190
 
class Table_ident;
191
 
 
192
 
class Foreign_key: public Key {
193
 
public:
194
 
  enum fk_match_opt { FK_MATCH_UNDEF, FK_MATCH_FULL,
195
 
                      FK_MATCH_PARTIAL, FK_MATCH_SIMPLE};
196
 
  enum fk_option { FK_OPTION_UNDEF, FK_OPTION_RESTRICT, FK_OPTION_CASCADE,
197
 
                   FK_OPTION_SET_NULL, FK_OPTION_NO_ACTION, FK_OPTION_DEFAULT};
198
 
 
199
 
  Table_ident *ref_table;
200
 
  List<Key_part_spec> ref_columns;
201
 
  uint32_t delete_opt, update_opt, match_opt;
202
 
  Foreign_key(const LEX_STRING &name_arg, List<Key_part_spec> &cols,
203
 
              Table_ident *table,   List<Key_part_spec> &ref_cols,
204
 
              uint32_t delete_opt_arg, uint32_t update_opt_arg, uint32_t match_opt_arg)
205
 
    :Key(FOREIGN_KEY, name_arg, &default_key_create_info, 0, cols),
206
 
    ref_table(table), ref_columns(ref_cols),
207
 
    delete_opt(delete_opt_arg), update_opt(update_opt_arg),
208
 
    match_opt(match_opt_arg)
209
 
  {}
210
 
  Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root);
211
 
  /**
212
 
    Used to make a clone of this object for ALTER/CREATE TABLE
213
 
    @sa comment for Key_part_spec::clone
214
 
  */
215
 
  virtual Key *clone(MEM_ROOT *mem_root) const
216
 
  { return new (mem_root) Foreign_key(*this, mem_root); }
217
 
};
 
106
 
 
107
 
218
108
 
219
109
typedef struct st_mysql_lock
220
110
{
235
125
class select_result;
236
126
class Time_zone;
237
127
 
238
 
#define THD_SENTRY_MAGIC 0xfeedd1ff
239
 
#define THD_SENTRY_GONE  0xdeadbeef
 
128
#define Session_SENTRY_MAGIC 0xfeedd1ff
 
129
#define Session_SENTRY_GONE  0xdeadbeef
240
130
 
241
 
#define THD_CHECK_SENTRY(thd) assert(thd->dbug_sentry == THD_SENTRY_MAGIC)
 
131
#define Session_CHECK_SENTRY(session) assert(session->dbug_sentry == Session_SENTRY_MAGIC)
242
132
 
243
133
struct system_variables
244
134
{
245
135
  /*
246
136
    How dynamically allocated system variables are handled:
247
 
    
 
137
 
248
138
    The global_system_variables and max_system_variables are "authoritative"
249
139
    They both should have the same 'version' and 'size'.
250
140
    When attempting to access a dynamic variable, if the session version
251
141
    is out of date, then the session version is updated and realloced if
252
142
    neccessary and bytes copied from global to make up for missing data.
253
 
  */ 
 
143
  */
254
144
  ulong dynamic_variables_version;
255
145
  char* dynamic_variables_ptr;
256
146
  uint32_t dynamic_variables_head;  /* largest valid variable offset */
257
147
  uint32_t dynamic_variables_size;  /* how many bytes are in use */
258
 
  
 
148
 
259
149
  uint64_t myisam_max_extra_sort_file_size;
260
150
  uint64_t myisam_max_sort_file_size;
261
151
  uint64_t max_heap_table_size;
262
152
  uint64_t tmp_table_size;
263
 
  uint64_t long_query_time;
264
153
  ha_rows select_limit;
265
154
  ha_rows max_join_size;
266
 
  ulong auto_increment_increment, auto_increment_offset;
267
 
  ulong bulk_insert_buff_size;
268
 
  ulong join_buff_size;
269
 
  ulong max_allowed_packet;
270
 
  ulong max_error_count;
271
 
  ulong max_length_for_sort_data;
272
 
  ulong max_sort_length;
273
 
  ulong max_tmp_tables;
274
 
  ulong min_examined_row_limit;
275
 
  ulong myisam_repair_threads;
276
 
  ulong myisam_sort_buff_size;
277
 
  ulong myisam_stats_method;
278
 
  ulong net_buffer_length;
279
 
  ulong net_interactive_timeout;
280
 
  ulong net_read_timeout;
281
 
  ulong net_retry_count;
282
 
  ulong net_wait_timeout;
283
 
  ulong net_write_timeout;
284
 
  ulong optimizer_prune_level;
285
 
  ulong optimizer_search_depth;
 
155
  uint32_t auto_increment_increment;
 
156
  uint32_t auto_increment_offset;
 
157
  uint64_t bulk_insert_buff_size;
 
158
  uint64_t join_buff_size;
 
159
  uint32_t max_allowed_packet;
 
160
  uint64_t max_error_count;
 
161
  uint64_t max_length_for_sort_data;
 
162
  uint64_t max_sort_length;
 
163
  uint64_t max_tmp_tables;
 
164
  uint64_t min_examined_row_limit;
 
165
  uint32_t myisam_repair_threads;
 
166
  size_t myisam_sort_buff_size;
 
167
  uint32_t myisam_stats_method;
 
168
  uint32_t net_buffer_length;
 
169
  uint32_t net_interactive_timeout;
 
170
  uint32_t net_read_timeout;
 
171
  uint32_t net_retry_count;
 
172
  uint32_t net_wait_timeout;
 
173
  uint32_t net_write_timeout;
 
174
  bool optimizer_prune_level;
 
175
  uint32_t optimizer_search_depth;
286
176
  /*
287
177
    Controls use of Engine-MRR:
288
178
      0 - auto, based on cost
289
179
      1 - force MRR when the storage engine is capable of doing it
290
180
      2 - disable MRR.
291
181
  */
292
 
  ulong optimizer_use_mrr; 
 
182
  uint32_t optimizer_use_mrr;
293
183
  /* A bitmap for switching optimizations on/off */
294
 
  ulong optimizer_switch;
295
 
  ulong preload_buff_size;
296
 
  ulong profiling_history_size;
297
 
  ulong query_cache_type;
298
 
  ulong read_buff_size;
299
 
  ulong read_rnd_buff_size;
300
 
  ulong div_precincrement;
301
 
  ulong sortbuff_size;
302
 
  ulong thread_handling;
303
 
  ulong tx_isolation;
304
 
  ulong completion_type;
 
184
  uint32_t optimizer_switch;
 
185
  uint64_t preload_buff_size;
 
186
  uint32_t read_buff_size;
 
187
  uint32_t read_rnd_buff_size;
 
188
  uint32_t div_precincrement;
 
189
  size_t sortbuff_size;
 
190
  uint32_t thread_handling;
 
191
  uint32_t tx_isolation;
 
192
  uint32_t completion_type;
305
193
  /* Determines which non-standard SQL behaviour should be enabled */
306
 
  ulong sql_mode;
307
 
  ulong default_week_format;
308
 
  ulong max_seeks_for_key;
309
 
  ulong range_alloc_block_size;
310
 
  ulong query_alloc_block_size;
311
 
  ulong query_prealloc_size;
312
 
  ulong trans_alloc_block_size;
313
 
  ulong trans_prealloc_size;
314
 
  ulong log_warnings;
315
 
  ulong group_concat_max_len;
316
 
  ulong binlog_format; // binlog format for this thd (see enum_binlog_format)
 
194
  uint32_t sql_mode;
 
195
  uint32_t default_week_format;
 
196
  uint64_t max_seeks_for_key;
 
197
  size_t range_alloc_block_size;
 
198
  uint32_t query_alloc_block_size;
 
199
  uint32_t query_prealloc_size;
 
200
  uint32_t trans_alloc_block_size;
 
201
  uint32_t trans_prealloc_size;
 
202
  bool log_warnings;
 
203
  uint64_t group_concat_max_len;
317
204
  /*
318
205
    In slave thread we need to know in behalf of which
319
206
    thread the query is being run to replicate temp tables properly
320
207
  */
321
 
  my_thread_id pseudo_thread_id;
 
208
  /* TODO: change this to my_thread_id - but have to fix set_var first */
 
209
  uint64_t pseudo_thread_id;
322
210
 
323
211
  bool low_priority_updates;
324
212
  bool new_mode;
325
 
  /* 
 
213
  /*
326
214
    compatibility option:
327
 
      - index usage hints (USE INDEX without a FOR clause) behave as in 5.0 
 
215
      - index usage hints (USE INDEX without a FOR clause) behave as in 5.0
328
216
  */
329
217
  bool old_mode;
330
218
  bool engine_condition_pushdown;
357
245
 
358
246
};
359
247
 
 
248
extern struct system_variables global_system_variables;
 
249
 
360
250
#include "sql_lex.h"  /* only for SQLCOM_END */
361
251
 
362
252
/* per thread status variables */
382
272
  ulong ha_update_count;
383
273
  ulong ha_write_count;
384
274
  ulong ha_prepare_count;
385
 
  ulong ha_discover_count;
386
275
  ulong ha_savepoint_count;
387
276
  ulong ha_savepoint_rollback_count;
388
277
 
440
329
 
441
330
#define last_system_status_var questions
442
331
 
443
 
void mark_transaction_to_rollback(THD *thd, bool all);
 
332
void mark_transaction_to_rollback(Session *session, bool all);
444
333
 
445
334
#ifdef DRIZZLE_SERVER
446
335
 
447
 
class Query_arena
448
 
{
449
 
public:
450
 
  /*
451
 
    List of items created in the parser for this query. Every item puts
452
 
    itself to the list on creation (see Item::Item() for details))
453
 
  */
454
 
  Item *free_list;
455
 
  MEM_ROOT *mem_root;                   // Pointer to current memroot
456
 
 
457
 
  Query_arena(MEM_ROOT *mem_root_arg) :
458
 
    free_list(0), mem_root(mem_root_arg)
459
 
  { }
460
 
  /*
461
 
    This constructor is used only when Query_arena is created as
462
 
    backup storage for another instance of Query_arena.
463
 
  */
464
 
  Query_arena() { }
465
 
 
466
 
  virtual ~Query_arena() {};
467
 
 
468
 
  inline void* alloc(size_t size) { return alloc_root(mem_root,size); }
469
 
  inline void* calloc(size_t size)
470
 
  {
471
 
    void *ptr;
472
 
    if ((ptr=alloc_root(mem_root,size)))
473
 
      memset(ptr, 0, size);
474
 
    return ptr;
475
 
  }
476
 
  inline char *strdup(const char *str)
477
 
  { return strdup_root(mem_root,str); }
478
 
  inline char *strmake(const char *str, size_t size)
479
 
  { return strmake_root(mem_root,str,size); }
480
 
  inline void *memdup(const void *str, size_t size)
481
 
  { return memdup_root(mem_root,str,size); }
482
 
  inline void *memdup_w_gap(const void *str, size_t size, uint32_t gap)
483
 
  {
484
 
    void *ptr;
485
 
    if ((ptr= alloc_root(mem_root,size+gap)))
486
 
      memcpy(ptr,str,size);
487
 
    return ptr;
488
 
  }
489
 
 
490
 
  void free_items();
491
 
};
492
 
 
493
 
 
494
336
/**
495
337
  @class Statement
496
338
  @brief State of a single command executed against this connection.
498
340
  One connection can contain a lot of simultaneously running statements,
499
341
  some of which could be:
500
342
   - prepared, that is, contain placeholders,
501
 
  To perform some action with statement we reset THD part to the state  of
502
 
  that statement, do the action, and then save back modified state from THD
 
343
  To perform some action with statement we reset Session part to the state  of
 
344
  that statement, do the action, and then save back modified state from Session
503
345
  to the statement. It will be changed in near future, and Statement will
504
346
  be used explicitly.
505
347
*/
520
362
                        handler of fields used is set
521
363
    MARK_COLUMNS_READ:  Means a bit in read set is set to inform handler
522
364
                        that the field is to be read. If field list contains
523
 
                        duplicates, then thd->dup_field is set to point
 
365
                        duplicates, then session->dup_field is set to point
524
366
                        to the last found duplicate.
525
367
    MARK_COLUMNS_WRITE: Means a bit is set in write set to inform handler
526
368
                        that it needs to update this field in write_row
539
381
    LOCK_thread_count mutex. And (B) we are ONLY allowed to set query to a
540
382
    non-NULL value if its previous value is NULL. We do not need to protect
541
383
    operation (B) with any mutex. To avoid crashes in races, if we do not
542
 
    know that thd->query cannot change at the moment, one should print
543
 
    thd->query like this:
 
384
    know that session->query cannot change at the moment, one should print
 
385
    session->query like this:
544
386
      (1) reserve the LOCK_thread_count mutex;
545
 
      (2) check if thd->query is NULL;
546
 
      (3) if not NULL, then print at most thd->query_length characters from
 
387
      (2) check if session->query is NULL;
 
388
      (3) if not NULL, then print at most session->query_length characters from
547
389
      it. We will see the query_length field as either 0, or the right value
548
390
      for it.
549
391
    Assuming that the write and read of an n-bit memory field in an n-bit
550
 
    computer is atomic, we can avoid races in the above way. 
 
392
    computer is atomic, we can avoid races in the above way.
551
393
    This printing is needed at least in SHOW PROCESSLIST and SHOW INNODB
552
394
    STATUS.
553
395
  */
563
405
    valid database name.
564
406
 
565
407
    @note this attribute is set and alloced by the slave SQL thread (for
566
 
    the THD of that thread); that thread is (and must remain, for now) the
 
408
    the Session of that thread); that thread is (and must remain, for now) the
567
409
    only responsible for freeing this member.
568
410
  */
569
411
 
593
435
  /* For now, this is only used to catch duplicated external xids */
594
436
  XID  xid;                           // transaction identifier
595
437
  enum xa_states xa_state;            // used by external XA only
596
 
  bool in_thd;
 
438
  bool in_session;
597
439
} XID_STATE;
598
440
 
599
441
extern pthread_mutex_t LOCK_xid_cache;
607
449
 
608
450
/**
609
451
  @class Security_context
610
 
  @brief A set of THD members describing the current authenticated user.
 
452
  @brief A set of Session members describing the current authenticated user.
611
453
*/
612
454
 
613
455
class Security_context {
614
456
public:
615
 
  Security_context() {}                       /* Remove gcc warning */
 
457
  Security_context() {}
616
458
  /*
617
459
    host - host of the client
618
460
    user - user of the client, set to NULL until the user has been read from
620
462
    priv_user - The user privilege we are using. May be "" for anonymous user.
621
463
    ip - client IP
622
464
  */
623
 
  char *user; 
624
 
  char *ip;
 
465
  std::string user;
 
466
  std::string ip;
625
467
 
626
 
  void init();
627
 
  void destroy();
628
468
  void skip_grants();
629
 
  inline char *priv_host_name()
 
469
  inline const char *priv_host_name()
630
470
  {
631
 
    return (ip ? ip : (char *)"%");
 
471
    return (ip.c_str() ? ip.c_str() : (char *)"%");
632
472
  }
633
473
};
634
474
 
690
530
    Tables that were locked with explicit or implicit LOCK TABLES.
691
531
    (Implicit LOCK TABLES happens when we are prelocking tables for
692
532
     execution of statement which uses stored routines. See description
693
 
     THD::prelocked_mode for more info.)
 
533
     Session::prelocked_mode for more info.)
694
534
  */
695
535
  DRIZZLE_LOCK *locked_tables;
696
536
 
735
575
};
736
576
 
737
577
 
738
 
/* Flags for the THD::system_thread variable */
 
578
/* Flags for the Session::system_thread variable */
739
579
enum enum_thread_type
740
580
{
741
581
  NON_SYSTEM_THREAD,
769
609
    from the anticipated conditions trapped during runtime.
770
610
 
771
611
    This mechanism is similar to C++ try/throw/catch:
772
 
    - 'try' correspond to <code>THD::push_internal_handler()</code>,
 
612
    - 'try' correspond to <code>Session::push_internal_handler()</code>,
773
613
    - 'throw' correspond to <code>my_error()</code>,
774
614
    which invokes <code>my_message_sql()</code>,
775
615
    - 'catch' correspond to checking how/if an internal handler was invoked,
776
616
    before removing it from the exception stack with
777
 
    <code>THD::pop_internal_handler()</code>.
 
617
    <code>Session::pop_internal_handler()</code>.
778
618
 
779
619
    @param sql_errno the error number
780
620
    @param level the error level
781
 
    @param thd the calling thread
 
621
    @param session the calling thread
782
622
    @return true if the error is handled
783
623
  */
784
624
  virtual bool handle_error(uint32_t sql_errno,
785
625
                            const char *message,
786
626
                            DRIZZLE_ERROR::enum_warning_level level,
787
 
                            THD *thd) = 0;
 
627
                            Session *session) = 0;
788
628
};
789
629
 
790
630
 
816
656
  /** Set to make set_error_status after set_{ok,eof}_status possible. */
817
657
  bool can_overwrite_status;
818
658
 
819
 
  void set_ok_status(THD *thd, ha_rows affected_rows_arg,
 
659
  void set_ok_status(Session *session, ha_rows affected_rows_arg,
820
660
                     uint64_t last_insert_id_arg,
821
661
                     const char *message);
822
 
  void set_eof_status(THD *thd);
823
 
  void set_error_status(THD *thd, uint32_t sql_errno_arg, const char *message_arg);
 
662
  void set_eof_status(Session *session);
 
663
  void set_error_status(Session *session, uint32_t sql_errno_arg, const char *message_arg);
824
664
 
825
665
  void disable_status();
826
666
 
869
709
  uint32_t m_sql_errno;
870
710
 
871
711
  /**
872
 
    Copied from thd->server_status when the diagnostics area is assigned.
 
712
    Copied from session->server_status when the diagnostics area is assigned.
873
713
    We need this member as some places in the code use the following pattern:
874
 
    thd->server_status|= ...
875
 
    my_eof(thd);
876
 
    thd->server_status&= ~...
 
714
    session->server_status|= ...
 
715
    my_eof(session);
 
716
    session->server_status&= ~...
877
717
    Assigned by OK, EOF or ERROR.
878
718
  */
879
719
  uint32_t m_server_status;
880
720
  /**
881
721
    The number of rows affected by the last statement. This is
882
 
    semantically close to thd->row_count_func, but has a different
883
 
    life cycle. thd->row_count_func stores the value returned by
 
722
    semantically close to session->row_count_func, but has a different
 
723
    life cycle. session->row_count_func stores the value returned by
884
724
    function ROW_COUNT() and is cleared only by statements that
885
725
    update its value, such as INSERT, UPDATE, DELETE and few others.
886
726
    This member is cleared at the beginning of the next statement.
887
727
 
888
 
    We could possibly merge the two, but life cycle of thd->row_count_func
 
728
    We could possibly merge the two, but life cycle of session->row_count_func
889
729
    can not be changed.
890
730
  */
891
731
  ha_rows    m_affected_rows;
892
732
  /**
893
733
    Similarly to the previous member, this is a replacement of
894
 
    thd->first_successful_insert_id_in_prev_stmt, which is used
 
734
    session->first_successful_insert_id_in_prev_stmt, which is used
895
735
    to implement LAST_INSERT_ID().
896
736
  */
897
737
  uint64_t   m_last_insert_id;
899
739
  uint       m_total_warn_count;
900
740
  enum_diagnostics_status m_status;
901
741
  /**
902
 
    @todo: the following THD members belong here:
 
742
    @todo: the following Session members belong here:
903
743
    - warn_list, warn_count,
904
744
  */
905
745
};
933
773
 
934
774
 
935
775
/**
936
 
  @class THD
937
 
  For each client connection we create a separate thread with THD serving as
 
776
  @class Session
 
777
  For each client connection we create a separate thread with Session serving as
938
778
  a thread/connection descriptor
939
779
*/
940
780
 
941
 
class THD :public Statement,
 
781
class Session :public Statement,
942
782
           public Open_tables_state
943
783
{
944
784
public:
946
786
  Relay_log_info* rli_fake;
947
787
 
948
788
  /*
949
 
    Constant for THD::where initialization in the beginning of every query.
 
789
    Constant for Session::where initialization in the beginning of every query.
950
790
 
951
 
    It's needed because we do not save/restore THD::where normally during
 
791
    It's needed because we do not save/restore Session::where normally during
952
792
    primary (non subselect) query execution.
953
793
  */
954
794
  static const char * const DEFAULT_WHERE;
968
808
  THR_LOCK_OWNER main_lock_id;          // To use for conventional queries
969
809
  THR_LOCK_OWNER *lock_id;              // If not main_lock_id, points to
970
810
                                        // the lock_id of a cursor.
971
 
  pthread_mutex_t LOCK_delete;          // Locked before thd is deleted
 
811
  pthread_mutex_t LOCK_delete;          // Locked before session is deleted
972
812
  /*
973
813
    A pointer to the stack frame of handle_one_connection(),
974
814
    which is called first in the thread for handling a client
982
822
 
983
823
  /**
984
824
    @note
985
 
    Some members of THD (currently 'Statement::db',
 
825
    Some members of Session (currently 'Statement::db',
986
826
    'catalog' and 'query')  are set and alloced by the slave SQL thread
987
 
    (for the THD of that thread); that thread is (and must remain, for now)
 
827
    (for the Session of that thread); that thread is (and must remain, for now)
988
828
    the only responsible for freeing these 3 members. If you add members
989
829
    here, and you add code to set them in replication, don't forget to
990
830
    free_them_and_set_them_to_0 in replication properly. For details see
993
833
    @see handle_slave_sql
994
834
  */
995
835
 
996
 
  Security_context main_security_ctx;
997
 
  Security_context *security_ctx;
 
836
  Security_context security_ctx;
998
837
 
999
838
  /*
1000
839
    Points to info-string that we show in SHOW PROCESSLIST
1001
 
    You are supposed to call THD_SET_PROC_INFO only if you have coded
 
840
    You are supposed to call Session_SET_PROC_INFO only if you have coded
1002
841
    a time-consuming piece that MySQL can get stuck in for a long time.
1003
842
 
1004
 
    Set it using the  thd_proc_info(THD *thread, const char *message)
 
843
    Set it using the  session_proc_info(Session *thread, const char *message)
1005
844
    macro/function.
1006
845
  */
1007
846
  void        set_proc_info(const char *info) { proc_info= info; }
1022
861
  /*
1023
862
    One thread can hold up to one named user-level lock. This variable
1024
863
    points to a lock object if the lock is present. See item_func.cc and
1025
 
    chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK. 
 
864
    chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK.
1026
865
  */
1027
866
  uint32_t dbug_sentry; // watch out for memory corruption
1028
867
  struct st_my_thread_var *mysys_var;
1038
877
  time_t     start_time, user_time;
1039
878
  uint64_t  connect_utime, thr_create_utime; // track down slow pthread_create
1040
879
  uint64_t  start_utime, utime_after_lock;
1041
 
  
 
880
 
1042
881
  thr_lock_type update_lock_default;
1043
882
 
1044
 
  /* <> 0 if we are inside of trigger or stored function. */
1045
 
  uint32_t in_sub_stmt;
 
883
  /*
 
884
    Both of the following container points in session will be converted to an API.
 
885
  */
1046
886
 
1047
887
  /* container for handler's private per-connection data */
1048
888
  Ha_data ha_data[MAX_HA];
1049
889
 
 
890
  /* container for replication data */
 
891
  void *replication_data;
 
892
  inline void setReplicationData (void *data) { replication_data= data; }
 
893
  inline void *getReplicationData () { return replication_data; }
 
894
 
1050
895
  /* Place to store various things */
1051
 
  void *thd_marker;
1052
 
  int binlog_setup_trx_data();
1053
 
 
1054
 
  /*
1055
 
    Public interface to write RBR events to the binlog
1056
 
  */
1057
 
  void binlog_start_trans_and_stmt();
1058
 
  void binlog_set_stmt_begin();
1059
 
  int binlog_write_table_map(Table *table, bool is_transactional);
1060
 
  int binlog_write_row(Table* table, bool is_transactional,
1061
 
                       const unsigned char *new_data);
1062
 
  int binlog_delete_row(Table* table, bool is_transactional,
1063
 
                        const unsigned char *old_data);
1064
 
  int binlog_update_row(Table* table, bool is_transactional,
1065
 
                        const unsigned char *old_data, const unsigned char *new_data);
 
896
  void *session_marker;
1066
897
 
1067
898
  void set_server_id(uint32_t sid) { server_id = sid; }
1068
899
 
1069
 
  /*
1070
 
    Member functions to handle pending event for row-level logging.
1071
 
  */
1072
 
  template <class RowsEventT> Rows_log_event*
1073
 
    binlog_prepare_pending_rows_event(Table* table, uint32_t serv_id,
1074
 
                                      size_t needed,
1075
 
                                      bool is_transactional,
1076
 
                                      RowsEventT* hint);
1077
 
  Rows_log_event* binlog_get_pending_rows_event() const;
1078
 
  void            binlog_set_pending_rows_event(Rows_log_event* ev);
1079
 
  int binlog_flush_pending_rows_event(bool stmt_end);
1080
 
 
1081
900
private:
1082
901
  uint32_t binlog_table_maps; // Number of table maps currently in the binlog
1083
902
 
1084
 
  enum enum_binlog_flag {
1085
 
    BINLOG_FLAG_UNSAFE_STMT_PRINTED,
1086
 
    BINLOG_FLAG_COUNT
1087
 
  };
1088
 
 
1089
903
  /**
1090
904
     Flags with per-thread information regarding the status of the
1091
905
     binary log.
1103
917
 
1104
918
  struct st_transactions {
1105
919
    SAVEPOINT *savepoints;
1106
 
    THD_TRANS all;                      // Trans since BEGIN WORK
1107
 
    THD_TRANS stmt;                     // Trans for current statement
 
920
    Session_TRANS all;                  // Trans since BEGIN WORK
 
921
    Session_TRANS stmt;                 // Trans for current statement
1108
922
    bool on;                            // see ha_enable_transaction()
1109
923
    XID_STATE xid_state;
1110
924
    Rows_log_event *m_pending_rows_event;
1134
948
  /*
1135
949
    This is to track items changed during execution of a prepared
1136
950
    statement/stored procedure. It's created by
1137
 
    register_item_tree_change() in memory root of THD, and freed in
 
951
    register_item_tree_change() in memory root of Session, and freed in
1138
952
    rollback_item_tree_changes(). For conventional execution it's always
1139
953
    empty.
1140
954
  */
1156
970
  */
1157
971
  uint64_t  first_successful_insert_id_in_prev_stmt;
1158
972
  /*
1159
 
    Variant of the above, used for storing in statement-based binlog. The
1160
 
    difference is that the one above can change as the execution of a stored
1161
 
    function progresses, while the one below is set once and then does not
1162
 
    change (which is the value which statement-based binlog needs).
1163
 
  */
1164
 
  uint64_t  first_successful_insert_id_in_prev_stmt_for_binlog;
1165
 
  /*
1166
973
    This is the first autogenerated insert id which was *successfully*
1167
974
    inserted by the current statement. It is maintained only to set
1168
975
    first_successful_insert_id_in_prev_stmt when statement ends.
1186
993
    first_successful_insert_id_in_cur_stmt.
1187
994
  */
1188
995
  /*
1189
 
    stmt_depends_on_first_successful_insert_id_in_prev_stmt is set when
1190
 
    LAST_INSERT_ID() is used by a statement.
1191
 
    If it is set, first_successful_insert_id_in_prev_stmt_for_binlog will be
1192
 
    stored in the statement-based binlog.
1193
 
    This variable is CUMULATIVE along the execution of a stored function or
1194
 
    trigger: if one substatement sets it to 1 it will stay 1 until the
1195
 
    function/trigger ends, thus making sure that
1196
 
    first_successful_insert_id_in_prev_stmt_for_binlog does not change anymore
1197
 
    and is propagated to the caller for binlogging.
1198
 
  */
1199
 
  bool       stmt_depends_on_first_successful_insert_id_in_prev_stmt;
1200
 
  /*
1201
996
    List of auto_increment intervals reserved by the thread so far, for
1202
997
    storage in the statement-based binlog.
1203
998
    Note that its minimum is not first_successful_insert_id_in_cur_stmt:
1254
1049
  }
1255
1050
  inline uint64_t read_first_successful_insert_id_in_prev_stmt(void)
1256
1051
  {
1257
 
    if (!stmt_depends_on_first_successful_insert_id_in_prev_stmt)
1258
 
    {
1259
 
      /* It's the first time we read it */
1260
 
      first_successful_insert_id_in_prev_stmt_for_binlog=
1261
 
        first_successful_insert_id_in_prev_stmt;
1262
 
      stmt_depends_on_first_successful_insert_id_in_prev_stmt= 1;
1263
 
    }
1264
1052
    return first_successful_insert_id_in_prev_stmt;
1265
1053
  }
1266
1054
  /*
1293
1081
  /*
1294
1082
    The set of those tables whose fields are referenced in all subqueries
1295
1083
    of the query.
1296
 
    TODO: possibly this it is incorrect to have used tables in THD because
 
1084
    TODO: possibly this it is incorrect to have used tables in Session because
1297
1085
    with more than one subquery, it is not clear what does the field mean.
1298
1086
  */
1299
1087
  table_map  used_tables;
1347
1135
 
1348
1136
  enum killed_state
1349
1137
  {
1350
 
    NOT_KILLED=0,
1351
 
    KILL_BAD_DATA=1,
1352
 
    KILL_CONNECTION=ER_SERVER_SHUTDOWN,
1353
 
    KILL_QUERY=ER_QUERY_INTERRUPTED,
 
1138
    NOT_KILLED,
 
1139
    KILL_BAD_DATA,
 
1140
    KILL_CONNECTION,
 
1141
    KILL_QUERY,
1354
1142
    KILLED_NO_VALUE      /* means neither of the states */
1355
1143
  };
1356
1144
  killed_state volatile killed;
1358
1146
  /* scramble - random string sent to client on handshake */
1359
1147
  char       scramble[SCRAMBLE_LENGTH+1];
1360
1148
 
1361
 
  bool       slave_thread, one_shot_set;
1362
 
  /* tells if current statement should binlog row-based(1) or stmt-based(0) */
1363
 
  bool       current_stmt_binlog_row_based;
 
1149
  bool       slave_thread;
1364
1150
  bool       some_tables_deleted;
1365
1151
  bool       last_cuted_field;
1366
1152
  bool       no_errors, password;
1369
1155
    can not continue. In particular, disables activation of
1370
1156
    CONTINUE or EXIT handlers of stored routines.
1371
1157
    Reset in the end of processing of the current user request, in
1372
 
    @see mysql_reset_thd_for_next_command().
 
1158
    @see mysql_reset_session_for_next_command().
1373
1159
  */
1374
1160
  bool is_fatal_error;
1375
1161
  /**
1390
1176
    Reset to false when we leave the sub-statement mode.
1391
1177
  */
1392
1178
  bool       is_fatal_sub_stmt_error;
1393
 
  bool       query_start_used, rand_used, time_zone_used;
 
1179
  bool       query_start_used;
1394
1180
  /* for IS NULL => = last_insert_id() fix in remove_eq_conds() */
1395
1181
  bool       substitute_null_with_insert_id;
1396
1182
  bool       in_lock_tables;
1401
1187
    it returned an error on master, and this is OK on the slave.
1402
1188
  */
1403
1189
  bool       is_slave_error;
1404
 
  bool       bootstrap, cleanup_done;
1405
 
  
 
1190
  bool       cleanup_done;
 
1191
 
1406
1192
  /**  is set if some thread specific value(s) used in a statement. */
1407
1193
  bool       thread_specific_used;
1408
1194
  bool       charset_is_system_charset, charset_is_collation_connection;
1409
1195
  bool       charset_is_character_set_filesystem;
1410
 
  bool       enable_slow_log;   /* enable slow log for current statement */
1411
1196
  bool       abort_on_warning;
1412
1197
  bool       got_warning;       /* Set on call to push_warning() */
1413
1198
  bool       no_warnings_for_error; /* no warnings on call to my_error() */
1430
1215
    ulong     ulong_value;
1431
1216
    uint64_t uint64_t_value;
1432
1217
  } sys_var_tmp;
1433
 
  
 
1218
 
1434
1219
  struct {
1435
 
    /* 
1436
 
      If true, mysql_bin_log::write(Log_event) call will not write events to 
 
1220
    /*
 
1221
      If true, drizzle_bin_log::write(Log_event) call will not write events to
1437
1222
      binlog, and maintain 2 below variables instead (use
1438
 
      mysql_bin_log.start_union_events to turn this on)
 
1223
      drizzle_bin_log.start_union_events to turn this on)
1439
1224
    */
1440
1225
    bool do_union;
1441
1226
    /*
1442
 
      If true, at least one mysql_bin_log::write(Log_event) call has been
1443
 
      made after last mysql_bin_log.start_union_events() call.
 
1227
      If true, at least one drizzle_bin_log::write(Log_event) call has been
 
1228
      made after last drizzle_bin_log.start_union_events() call.
1444
1229
    */
1445
1230
    bool unioned_events;
1446
1231
    /*
1447
 
      If true, at least one mysql_bin_log::write(Log_event e), where 
1448
 
      e.cache_stmt == true call has been made after last 
1449
 
      mysql_bin_log.start_union_events() call.
 
1232
      If true, at least one drizzle_bin_log::write(Log_event e), where
 
1233
      e.cache_stmt == true call has been made after last
 
1234
      drizzle_bin_log.start_union_events() call.
1450
1235
    */
1451
1236
    bool unioned_events_trans;
1452
 
    
1453
 
    /* 
 
1237
 
 
1238
    /*
1454
1239
      'queries' (actually SP statements) that run under inside this binlog
1455
 
      union have thd->query_id >= first_query_id.
 
1240
      union have session->query_id >= first_query_id.
1456
1241
    */
1457
1242
    query_id_t first_query_id;
1458
1243
  } binlog_evt_union;
1466
1251
  */
1467
1252
  Lex_input_stream *m_lip;
1468
1253
 
1469
 
  THD();
1470
 
  ~THD();
 
1254
  Session();
 
1255
  ~Session();
1471
1256
 
1472
1257
  void init(void);
1473
1258
  /*
1474
1259
    Initialize memory roots necessary for query processing and (!)
1475
 
    pre-allocate memory for it. We can't do that in THD constructor because
 
1260
    pre-allocate memory for it. We can't do that in Session constructor because
1476
1261
    there are use cases (acl_init, watcher threads,
1477
1262
    killing mysqld) where it's vital to not allocate excessive and not used
1478
1263
    memory. Note, that we still don't return error from init_for_queries():
1479
1264
    if preallocation fails, we should notice that at the first call to
1480
 
    alloc_root. 
 
1265
    alloc_root.
1481
1266
  */
1482
1267
  void init_for_queries();
1483
 
  void change_user(void);
1484
1268
  void cleanup(void);
1485
1269
  void cleanup_after_query();
1486
1270
  bool store_globals();
1487
 
  void awake(THD::killed_state state_to_set);
 
1271
  void awake(Session::killed_state state_to_set);
1488
1272
 
1489
1273
  enum enum_binlog_query_type {
1490
1274
    /*
1491
1275
      The query can be logged row-based or statement-based
1492
1276
    */
1493
1277
    ROW_QUERY_TYPE,
1494
 
    
 
1278
 
1495
1279
    /*
1496
1280
      The query has to be logged statement-based
1497
1281
    */
1498
1282
    STMT_QUERY_TYPE,
1499
 
    
 
1283
 
1500
1284
    /*
1501
1285
      The query represents a change to a table in the "mysql"
1502
1286
      database and is currently mapped to ROW_QUERY_TYPE.
1504
1288
    DRIZZLE_QUERY_TYPE,
1505
1289
    QUERY_TYPE_COUNT
1506
1290
  };
1507
 
  
1508
 
  int binlog_query(enum_binlog_query_type qtype,
1509
 
                   char const *query, ulong query_len,
1510
 
                   bool is_trans, bool suppress_use,
1511
 
                   THD::killed_state killed_err_arg= THD::KILLED_NO_VALUE);
1512
1291
 
1513
1292
  /*
1514
1293
    For enter_cond() / exit_cond() to work the mutex must be got before
1522
1301
    safe_mutex_assert_owner(mutex);
1523
1302
    mysys_var->current_mutex = mutex;
1524
1303
    mysys_var->current_cond = cond;
1525
 
    thd_proc_info(this, msg);
 
1304
    this->set_proc_info(msg);
1526
1305
    return old_msg;
1527
1306
  }
1528
1307
  inline void exit_cond(const char* old_msg)
1531
1310
      Putting the mutex unlock in exit_cond() ensures that
1532
1311
      mysys_var->current_mutex is always unlocked _before_ mysys_var->mutex is
1533
1312
      locked (if that would not be the case, you'll get a deadlock if someone
1534
 
      does a THD::awake() on you).
 
1313
      does a Session::awake() on you).
1535
1314
    */
1536
1315
    pthread_mutex_unlock(mysys_var->current_mutex);
1537
1316
    pthread_mutex_lock(&mysys_var->mutex);
1538
1317
    mysys_var->current_mutex = 0;
1539
1318
    mysys_var->current_cond = 0;
1540
 
    thd_proc_info(this, old_msg);
 
1319
    this->set_proc_info(old_msg);
1541
1320
    pthread_mutex_unlock(&mysys_var->mutex);
1542
1321
  }
1543
1322
  inline time_t query_start() { query_start_used=1; return start_time; }
1551
1330
    else
1552
1331
      start_utime= utime_after_lock= my_micro_time_and_time(&start_time);
1553
1332
  }
1554
 
  inline void   set_current_time()    { start_time= my_time(MY_WME); }
 
1333
  inline void   set_current_time()    { start_time= time(NULL); }
1555
1334
  inline void   set_time(time_t t)
1556
1335
  {
1557
1336
    start_time= user_time= t;
1661
1440
  void reset_n_backup_open_tables_state(Open_tables_state *backup);
1662
1441
  void restore_backup_open_tables_state(Open_tables_state *backup);
1663
1442
 
1664
 
  inline void set_current_stmt_binlog_row_based_if_mixed()
1665
 
  {
1666
 
    /*
1667
 
      If in a stored/function trigger, the caller should already have done the
1668
 
      change. We test in_sub_stmt to prevent introducing bugs where people
1669
 
      wouldn't ensure that, and would switch to row-based mode in the middle
1670
 
      of executing a stored function/trigger (which is too late, see also
1671
 
      reset_current_stmt_binlog_row_based()); this condition will make their
1672
 
      tests fail and so force them to propagate the
1673
 
      lex->binlog_row_based_if_mixed upwards to the caller.
1674
 
    */
1675
 
    if ((variables.binlog_format == BINLOG_FORMAT_MIXED) &&
1676
 
        (in_sub_stmt == 0))
1677
 
      current_stmt_binlog_row_based= true;
1678
 
  }
1679
 
  inline void set_current_stmt_binlog_row_based()
1680
 
  {
1681
 
    current_stmt_binlog_row_based= true;
1682
 
  }
1683
 
  inline void clear_current_stmt_binlog_row_based()
1684
 
  {
1685
 
    current_stmt_binlog_row_based= false;
1686
 
  }
1687
 
  inline void reset_current_stmt_binlog_row_based()
1688
 
  {
1689
 
    /*
1690
 
      If there are temporary tables, don't reset back to
1691
 
      statement-based. Indeed it could be that:
1692
 
      CREATE TEMPORARY TABLE t SELECT UUID(); # row-based
1693
 
      # and row-based does not store updates to temp tables
1694
 
      # in the binlog.
1695
 
      INSERT INTO u SELECT * FROM t; # stmt-based
1696
 
      and then the INSERT will fail as data inserted into t was not logged.
1697
 
      So we continue with row-based until the temp table is dropped.
1698
 
      If we are in a stored function or trigger, we mustn't reset in the
1699
 
      middle of its execution (as the binary logging way of a stored function
1700
 
      or trigger is decided when it starts executing, depending for example on
1701
 
      the caller (for a stored function: if caller is SELECT or
1702
 
      INSERT/UPDATE/DELETE...).
1703
 
 
1704
 
      Don't reset binlog format for NDB binlog injector thread.
1705
 
    */
1706
 
    if ((temporary_tables == NULL) && (in_sub_stmt == 0))
1707
 
    {
1708
 
      current_stmt_binlog_row_based= 
1709
 
        test(variables.binlog_format == BINLOG_FORMAT_ROW);
1710
 
    }
1711
 
  }
1712
 
 
1713
1443
  /**
1714
1444
    Set the current database; use deep copy of C-string.
1715
1445
 
1731
1461
      @retval false Success
1732
1462
      @retval true  Out-of-memory error
1733
1463
  */
1734
 
  bool set_db(const char *new_db, size_t new_db_len)
1735
 
  {
1736
 
    /* Do not reallocate memory if current chunk is big enough. */
1737
 
    if (db && new_db && db_length >= new_db_len)
1738
 
      memcpy(db, new_db, new_db_len+1);
1739
 
    else
1740
 
    {
1741
 
      if (db)
1742
 
        free(db);
1743
 
      if (new_db)
1744
 
        db= my_strndup(new_db, new_db_len, MYF(MY_WME | ME_FATALERROR));
1745
 
      else
1746
 
        db= NULL;
1747
 
    }
1748
 
    db_length= db ? new_db_len : 0;
1749
 
    return new_db && !db;
1750
 
  }
 
1464
  bool set_db(const char *new_db, size_t new_db_len);
1751
1465
 
1752
1466
  /**
1753
1467
    Set the current database; use shallow copy of C-string.
1771
1485
    a statement is parsed but before it's executed.
1772
1486
  */
1773
1487
  bool copy_db_to(char **p_db, size_t *p_db_length);
1774
 
  thd_scheduler scheduler;
 
1488
  session_scheduler scheduler;
1775
1489
 
1776
1490
public:
1777
1491
  /**
1794
1508
  */
1795
1509
  void pop_internal_handler();
1796
1510
 
 
1511
  /**
 
1512
    Close the current connection.
 
1513
  */
 
1514
  void close_connection(uint32_t errcode, bool lock);
 
1515
 
1797
1516
private:
1798
1517
  const char *proc_info;
1799
1518
 
1818
1537
};
1819
1538
 
1820
1539
 
1821
 
/** A short cut for thd->main_da.set_ok_status(). */
 
1540
 
 
1541
/** A short cut for session->main_da.set_ok_status(). */
1822
1542
 
1823
1543
inline void
1824
 
my_ok(THD *thd, ha_rows affected_rows= 0, uint64_t id= 0,
 
1544
my_ok(Session *session, ha_rows affected_rows= 0, uint64_t id= 0,
1825
1545
        const char *message= NULL)
1826
1546
{
1827
 
  thd->main_da.set_ok_status(thd, affected_rows, id, message);
 
1547
  session->main_da.set_ok_status(session, affected_rows, id, message);
1828
1548
}
1829
1549
 
1830
1550
 
1831
 
/** A short cut for thd->main_da.set_eof_status(). */
 
1551
/** A short cut for session->main_da.set_eof_status(). */
1832
1552
 
1833
1553
inline void
1834
 
my_eof(THD *thd)
 
1554
my_eof(Session *session)
1835
1555
{
1836
 
  thd->main_da.set_eof_status(thd);
 
1556
  session->main_da.set_eof_status(session);
1837
1557
}
1838
1558
 
1839
1559
#define tmp_disable_binlog(A)       \
1852
1572
class sql_exchange :public Sql_alloc
1853
1573
{
1854
1574
public:
1855
 
  enum enum_filetype filetype; /* load XML, Added by Arnold & Erik */ 
 
1575
  enum enum_filetype filetype; /* load XML, Added by Arnold & Erik */
1856
1576
  char *file_name;
1857
1577
  String *field_term,*enclosed,*line_term,*line_start,*escaped;
1858
1578
  bool opt_enclosed;
1873
1593
 
1874
1594
class select_result :public Sql_alloc {
1875
1595
protected:
1876
 
  THD *thd;
 
1596
  Session *session;
1877
1597
  SELECT_LEX_UNIT *unit;
1878
1598
public:
1879
1599
  select_result();
1880
1600
  virtual ~select_result() {};
1881
 
  virtual int prepare(List<Item> &list __attribute__((unused)),
 
1601
  virtual int prepare(List<Item> &,
1882
1602
                      SELECT_LEX_UNIT *u)
1883
1603
  {
1884
1604
    unit= u;
1894
1614
  { return fields.elements; }
1895
1615
  virtual bool send_fields(List<Item> &list, uint32_t flags)=0;
1896
1616
  virtual bool send_data(List<Item> &items)=0;
1897
 
  virtual bool initialize_tables (JOIN  __attribute__((unused)) *join=0)
 
1617
  virtual bool initialize_tables (JOIN *)
1898
1618
  { return 0; }
1899
1619
  virtual void send_error(uint32_t errcode,const char *err);
1900
1620
  virtual bool send_eof()=0;
1912
1632
    statement/stored procedure.
1913
1633
  */
1914
1634
  virtual void cleanup();
1915
 
  void set_thd(THD *thd_arg) { thd= thd_arg; }
 
1635
  void set_session(Session *session_arg) { session= session_arg; }
1916
1636
  void begin_dataset() {}
1917
1637
};
1918
1638
 
1927
1647
{
1928
1648
public:
1929
1649
  select_result_interceptor() {}              /* Remove gcc warning */
1930
 
  uint32_t field_count(List<Item> &fields __attribute__((unused))) const
 
1650
  uint32_t field_count(List<Item> &) const
1931
1651
  { return 0; }
1932
 
  bool send_fields(List<Item> &fields __attribute__((unused)),
1933
 
                   uint32_t flag __attribute__((unused))) { return false; }
 
1652
  bool send_fields(List<Item> &,
 
1653
                   uint32_t)
 
1654
  { return false; }
1934
1655
};
1935
1656
 
1936
1657
 
2053
1774
  Field **field;
2054
1775
  /* lock data for tmp table */
2055
1776
  DRIZZLE_LOCK *m_lock;
2056
 
  /* m_lock or thd->extra_lock */
 
1777
  /* m_lock or session->extra_lock */
2057
1778
  DRIZZLE_LOCK **m_plock;
2058
1779
public:
2059
1780
  select_create (TableList *table_arg,
2077
1798
  void abort();
2078
1799
  virtual bool can_rollback_data() { return 1; }
2079
1800
 
2080
 
  // Needed for access from local class MY_HOOKS in prepare(), since thd is proteted.
2081
 
  const THD *get_thd(void) { return thd; }
 
1801
  // Needed for access from local class MY_HOOKS in prepare(), since session is proteted.
 
1802
  const Session *get_session(void) { return session; }
2082
1803
  const HA_CREATE_INFO *get_create_info() { return create_info; };
2083
1804
  int prepare2(void) { return 0; }
2084
1805
};
2085
1806
 
2086
1807
#include <storage/myisam/myisam.h>
2087
1808
 
2088
 
/* 
2089
 
  Param to create temporary tables when doing SELECT:s 
 
1809
/*
 
1810
  Param to create temporary tables when doing SELECT:s
2090
1811
  NOTE
2091
1812
    This structure is copied using memcpy as a part of JOIN.
2092
1813
*/
2114
1835
  uint  quick_group;
2115
1836
  bool  using_indirect_summary_function;
2116
1837
  /* If >0 convert all blob fields to varchar(convert_blob_length) */
2117
 
  uint32_t  convert_blob_length; 
2118
 
  const CHARSET_INFO *table_charset; 
 
1838
  uint32_t  convert_blob_length;
 
1839
  const CHARSET_INFO *table_charset;
2119
1840
  bool schema_table;
2120
1841
  /*
2121
1842
    True if GROUP BY and its aggregate functions are already computed
2143
1864
    cleanup();
2144
1865
  }
2145
1866
  void init(void);
2146
 
  inline void cleanup(void)
2147
 
  {
2148
 
    if (copy_field)                             /* Fix for Intel compiler */
2149
 
    {
2150
 
      delete [] copy_field;
2151
 
      save_copy_field= copy_field= 0;
2152
 
    }
2153
 
  }
 
1867
  void cleanup(void);
2154
1868
};
2155
1869
 
2156
1870
class select_union :public select_result_interceptor
2165
1879
  bool send_eof();
2166
1880
  bool flush();
2167
1881
  void cleanup();
2168
 
  bool create_result_table(THD *thd, List<Item> *column_types,
 
1882
  bool create_result_table(Session *session, List<Item> *column_types,
2169
1883
                           bool is_distinct, uint64_t options,
2170
1884
                           const char *alias, bool bit_fields_as_long);
2171
1885
};
2247
1961
  LEX_STRING db;
2248
1962
  LEX_STRING table;
2249
1963
  SELECT_LEX_UNIT *sel;
2250
 
  inline Table_ident(THD *thd, LEX_STRING db_arg, LEX_STRING table_arg,
 
1964
  inline Table_ident(Session *session, LEX_STRING db_arg, LEX_STRING table_arg,
2251
1965
                     bool force)
2252
1966
    :table(table_arg), sel((SELECT_LEX_UNIT *)0)
2253
1967
  {
2254
 
    if (!force && (thd->client_capabilities & CLIENT_NO_SCHEMA))
 
1968
    if (!force && (session->client_capabilities & CLIENT_NO_SCHEMA))
2255
1969
      db.str=0;
2256
1970
    else
2257
1971
      db= db_arg;
2258
1972
  }
2259
 
  inline Table_ident(LEX_STRING table_arg) 
 
1973
  inline Table_ident(LEX_STRING table_arg)
2260
1974
    :table(table_arg), sel((SELECT_LEX_UNIT *)0)
2261
1975
  {
2262
1976
    db.str=0;
2302
2016
};
2303
2017
 
2304
2018
/*
2305
 
   Unique -- class for unique (removing of duplicates). 
 
2019
   Unique -- class for unique (removing of duplicates).
2306
2020
   Puts all values to the TREE. If the tree becomes too big,
2307
2021
   it's dumped to the file. User can request sorted values, or
2308
2022
   just iterate through them. In the last case tree merging is performed in
2313
2027
{
2314
2028
  DYNAMIC_ARRAY file_ptrs;
2315
2029
  ulong max_elements;
2316
 
  uint64_t max_in_memory_size;
 
2030
  size_t max_in_memory_size;
2317
2031
  IO_CACHE file;
2318
2032
  TREE tree;
2319
2033
  unsigned char *record_pointers;
2323
2037
public:
2324
2038
  ulong elements;
2325
2039
  Unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
2326
 
         uint32_t size_arg, uint64_t max_in_memory_size_arg);
 
2040
         uint32_t size_arg, size_t max_in_memory_size_arg);
2327
2041
  ~Unique();
2328
2042
  ulong elements_in_tree() { return tree.elements_in_tree; }
2329
2043
  inline bool unique_add(void *ptr)
2334
2048
  }
2335
2049
 
2336
2050
  bool get(Table *table);
2337
 
  static double get_use_cost(uint32_t *buffer, uint32_t nkeys, uint32_t key_size, 
2338
 
                             uint64_t max_in_memory_size);
2339
 
  inline static int get_cost_calc_buff_size(ulong nkeys, uint32_t key_size, 
2340
 
                                            uint64_t max_in_memory_size)
 
2051
  static double get_use_cost(uint32_t *buffer, uint32_t nkeys, uint32_t key_size,
 
2052
                             size_t max_in_memory_size);
 
2053
  inline static int get_cost_calc_buff_size(ulong nkeys, uint32_t key_size,
 
2054
                                            size_t max_in_memory_size)
2341
2055
  {
2342
 
    register uint64_t max_elems_in_tree=
 
2056
    register size_t max_elems_in_tree=
2343
2057
      (1 + max_in_memory_size / ALIGN_SIZE(sizeof(TREE_ELEMENT)+key_size));
2344
2058
    return (int) (sizeof(uint)*(1 + nkeys/max_elems_in_tree));
2345
2059
  }
2397
2111
  uint32_t table_count;
2398
2112
  /*
2399
2113
   List of tables referenced in the CHECK OPTION condition of
2400
 
   the updated view excluding the updated table. 
 
2114
   the updated view excluding the updated table.
2401
2115
  */
2402
2116
  List <Table> unupdated_check_opt_tables;
2403
2117
  Copy_field *copy_field;
2406
2120
  /* True if the update operation has made a change in a transactional table */
2407
2121
  bool transactional_tables;
2408
2122
  bool ignore;
2409
 
  /* 
 
2123
  /*
2410
2124
     error handling (rollback and binlogging) can happen in send_eof()
2411
2125
     so that afterward send_error() needs to find out that.
2412
2126
  */
2453
2167
 
2454
2168
/* Bits in sql_command_flags */
2455
2169
 
2456
 
#define CF_CHANGES_DATA         1
2457
 
#define CF_HAS_ROW_COUNT        2
2458
 
#define CF_STATUS_COMMAND       4
2459
 
#define CF_SHOW_TABLE_COMMAND   8
2460
 
#define CF_WRITE_LOGS_COMMAND  16
 
2170
enum sql_command_flag_bits {
 
2171
  CF_BIT_CHANGES_DATA,
 
2172
  CF_BIT_HAS_ROW_COUNT,
 
2173
  CF_BIT_STATUS_COMMAND,
 
2174
  CF_BIT_SHOW_TABLE_COMMAND,
 
2175
  CF_BIT_WRITE_LOGS_COMMAND,
 
2176
  CF_BIT_SIZE
 
2177
};
 
2178
 
 
2179
static const std::bitset<CF_BIT_SIZE> CF_CHANGES_DATA(1 << CF_BIT_CHANGES_DATA);
 
2180
static const std::bitset<CF_BIT_SIZE> CF_HAS_ROW_COUNT(1 << CF_BIT_HAS_ROW_COUNT);
 
2181
static const std::bitset<CF_BIT_SIZE> CF_STATUS_COMMAND(1 << CF_BIT_STATUS_COMMAND);
 
2182
static const std::bitset<CF_BIT_SIZE> CF_SHOW_TABLE_COMMAND(1 << CF_BIT_SHOW_TABLE_COMMAND);
 
2183
static const std::bitset<CF_BIT_SIZE> CF_WRITE_LOGS_COMMAND(1 << CF_BIT_WRITE_LOGS_COMMAND);
2461
2184
 
2462
2185
/* Functions in sql_class.cc */
2463
2186
 
2466
2189
void add_diff_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var,
2467
2190
                        STATUS_VAR *dec_var);
2468
2191
 
 
2192
/* Some inline functions for more speed */
 
2193
 
 
2194
inline bool add_item_to_list(Session *session, Item *item)
 
2195
{
 
2196
  return session->lex->current_select->add_item_to_list(session, item);
 
2197
}
 
2198
 
 
2199
inline bool add_value_to_list(Session *session, Item *value)
 
2200
{
 
2201
  return session->lex->value_list.push_back(value);
 
2202
}
 
2203
 
 
2204
inline bool add_order_to_list(Session *session, Item *item, bool asc)
 
2205
{
 
2206
  return session->lex->current_select->add_order_to_list(session, item, asc);
 
2207
}
 
2208
 
 
2209
inline bool add_group_to_list(Session *session, Item *item, bool asc)
 
2210
{
 
2211
  return session->lex->current_select->add_group_to_list(session, item, asc);
 
2212
}
 
2213
 
2469
2214
#endif /* DRIZZLE_SERVER */
 
2215
 
 
2216
#endif /* DRIZZLED_SQL_CLASS_H */