~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.h

Merged from Toru - removal of my_time_t.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
 
 
21
#ifndef DRIZZLED_SESSION_H
 
22
#define DRIZZLED_SESSION_H
16
23
 
17
24
/* Classes in mysql */
 
25
 
18
26
#include <drizzled/global.h>
19
 
#include "log.h"
20
 
#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>
21
41
 
22
42
class Relay_log_info;
23
43
 
26
46
class Slave_log_event;
27
47
class Lex_input_stream;
28
48
class Rows_log_event;
 
49
class user_var_entry;
 
50
class Copy_field;
 
51
class Table_ident;
29
52
 
30
 
enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE };
31
 
enum enum_ha_read_modes { RFIRST, RNEXT, RPREV, RLAST, RKEY, RNEXT_SAME };
32
 
enum enum_duplicates { DUP_ERROR, DUP_REPLACE, DUP_UPDATE };
33
 
enum enum_delay_key_write { DELAY_KEY_WRITE_NONE, DELAY_KEY_WRITE_ON,
34
 
                            DELAY_KEY_WRITE_ALL };
35
 
enum enum_slave_exec_mode { SLAVE_EXEC_MODE_STRICT,
36
 
                            SLAVE_EXEC_MODE_IDEMPOTENT,
37
 
                            SLAVE_EXEC_MODE_LAST_BIT};
38
 
enum enum_mark_columns
39
 
{ MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE};
40
 
enum enum_filetype { FILETYPE_CSV, FILETYPE_XML };
41
53
 
42
54
extern char internal_table_name[2];
43
55
extern char empty_c_string[1];
48
60
 
49
61
#define TC_HEURISTIC_RECOVER_COMMIT   1
50
62
#define TC_HEURISTIC_RECOVER_ROLLBACK 2
51
 
extern uint tc_heuristic_recover;
 
63
extern uint32_t tc_heuristic_recover;
52
64
 
53
65
typedef struct st_user_var_events
54
66
{
56
68
  char *value;
57
69
  ulong length;
58
70
  Item_result type;
59
 
  uint charset_number;
 
71
  uint32_t charset_number;
60
72
} BINLOG_USER_VAR_EVENT;
61
73
 
62
74
#define RP_LOCK_LOG_IS_ALREADY_LOCKED 1
91
103
} COPY_INFO;
92
104
 
93
105
 
94
 
class Key_part_spec :public Sql_alloc {
95
 
public:
96
 
  LEX_STRING field_name;
97
 
  uint length;
98
 
  Key_part_spec(const LEX_STRING &name, uint len)
99
 
    : field_name(name), length(len)
100
 
  {}
101
 
  Key_part_spec(const char *name, const size_t name_len, uint len)
102
 
    : length(len)
103
 
  { field_name.str= (char *)name; field_name.length= name_len; }
104
 
  bool operator==(const Key_part_spec& other) const;
105
 
  /**
106
 
    Construct a copy of this Key_part_spec. field_name is copied
107
 
    by-pointer as it is known to never change. At the same time
108
 
    'length' may be reset in mysql_prepare_create_table, and this
109
 
    is why we supply it with a copy.
110
 
 
111
 
    @return If out of memory, 0 is returned and an error is set in
112
 
    THD.
113
 
  */
114
 
  Key_part_spec *clone(MEM_ROOT *mem_root) const
115
 
  { return new (mem_root) Key_part_spec(*this); }
116
 
};
117
 
 
118
 
 
119
 
class Alter_drop :public Sql_alloc {
120
 
public:
121
 
  enum drop_type {KEY, COLUMN };
122
 
  const char *name;
123
 
  enum drop_type type;
124
 
  Alter_drop(enum drop_type par_type,const char *par_name)
125
 
    :name(par_name), type(par_type) {}
126
 
  /**
127
 
    Used to make a clone of this object for ALTER/CREATE TABLE
128
 
    @sa comment for Key_part_spec::clone
129
 
  */
130
 
  Alter_drop *clone(MEM_ROOT *mem_root) const
131
 
    { return new (mem_root) Alter_drop(*this); }
132
 
};
133
 
 
134
 
 
135
 
class Alter_column :public Sql_alloc {
136
 
public:
137
 
  const char *name;
138
 
  Item *def;
139
 
  Alter_column(const char *par_name,Item *literal)
140
 
    :name(par_name), def(literal) {}
141
 
  /**
142
 
    Used to make a clone of this object for ALTER/CREATE TABLE
143
 
    @sa comment for Key_part_spec::clone
144
 
  */
145
 
  Alter_column *clone(MEM_ROOT *mem_root) const
146
 
    { return new (mem_root) Alter_column(*this); }
147
 
};
148
 
 
149
 
 
150
 
class Key :public Sql_alloc {
151
 
public:
152
 
  enum Keytype { PRIMARY, UNIQUE, MULTIPLE, FOREIGN_KEY};
153
 
  enum Keytype type;
154
 
  KEY_CREATE_INFO key_create_info;
155
 
  List<Key_part_spec> columns;
156
 
  LEX_STRING name;
157
 
  bool generated;
158
 
 
159
 
  Key(enum Keytype type_par, const LEX_STRING &name_arg,
160
 
      KEY_CREATE_INFO *key_info_arg,
161
 
      bool generated_arg, List<Key_part_spec> &cols)
162
 
    :type(type_par), key_create_info(*key_info_arg), columns(cols),
163
 
    name(name_arg), generated(generated_arg)
164
 
  {}
165
 
  Key(enum Keytype type_par, const char *name_arg, size_t name_len_arg,
166
 
      KEY_CREATE_INFO *key_info_arg, bool generated_arg,
167
 
      List<Key_part_spec> &cols)
168
 
    :type(type_par), key_create_info(*key_info_arg), columns(cols),
169
 
    generated(generated_arg)
170
 
  {
171
 
    name.str= (char *)name_arg;
172
 
    name.length= name_len_arg;
173
 
  }
174
 
  Key(const Key &rhs, MEM_ROOT *mem_root);
175
 
  virtual ~Key() {}
176
 
  /* Equality comparison of keys (ignoring name) */
177
 
  friend bool foreign_key_prefix(Key *a, Key *b);
178
 
  /**
179
 
    Used to make a clone of this object for ALTER/CREATE TABLE
180
 
    @sa comment for Key_part_spec::clone
181
 
  */
182
 
  virtual Key *clone(MEM_ROOT *mem_root) const
183
 
    { return new (mem_root) Key(*this, mem_root); }
184
 
};
185
 
 
186
 
class Table_ident;
187
 
 
188
 
class Foreign_key: public Key {
189
 
public:
190
 
  enum fk_match_opt { FK_MATCH_UNDEF, FK_MATCH_FULL,
191
 
                      FK_MATCH_PARTIAL, FK_MATCH_SIMPLE};
192
 
  enum fk_option { FK_OPTION_UNDEF, FK_OPTION_RESTRICT, FK_OPTION_CASCADE,
193
 
                   FK_OPTION_SET_NULL, FK_OPTION_NO_ACTION, FK_OPTION_DEFAULT};
194
 
 
195
 
  Table_ident *ref_table;
196
 
  List<Key_part_spec> ref_columns;
197
 
  uint delete_opt, update_opt, match_opt;
198
 
  Foreign_key(const LEX_STRING &name_arg, List<Key_part_spec> &cols,
199
 
              Table_ident *table,   List<Key_part_spec> &ref_cols,
200
 
              uint delete_opt_arg, uint update_opt_arg, uint match_opt_arg)
201
 
    :Key(FOREIGN_KEY, name_arg, &default_key_create_info, 0, cols),
202
 
    ref_table(table), ref_columns(ref_cols),
203
 
    delete_opt(delete_opt_arg), update_opt(update_opt_arg),
204
 
    match_opt(match_opt_arg)
205
 
  {}
206
 
  Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root);
207
 
  /**
208
 
    Used to make a clone of this object for ALTER/CREATE TABLE
209
 
    @sa comment for Key_part_spec::clone
210
 
  */
211
 
  virtual Key *clone(MEM_ROOT *mem_root) const
212
 
  { return new (mem_root) Foreign_key(*this, mem_root); }
213
 
};
 
106
 
 
107
 
214
108
 
215
109
typedef struct st_mysql_lock
216
110
{
217
111
  Table **table;
218
 
  uint table_count,lock_count;
 
112
  uint32_t table_count,lock_count;
219
113
  THR_LOCK_DATA **locks;
220
114
} DRIZZLE_LOCK;
221
115
 
224
118
{
225
119
public:
226
120
  String column;
227
 
  uint rights;
 
121
  uint32_t rights;
228
122
  LEX_COLUMN (const String& x,const  uint& y ): column (x),rights (y) {}
229
123
};
230
124
 
231
 
#include "sql_lex.h"                            /* Must be here */
232
 
 
233
125
class select_result;
234
126
class Time_zone;
235
127
 
236
 
#define THD_SENTRY_MAGIC 0xfeedd1ff
237
 
#define THD_SENTRY_GONE  0xdeadbeef
 
128
#define Session_SENTRY_MAGIC 0xfeedd1ff
 
129
#define Session_SENTRY_GONE  0xdeadbeef
238
130
 
239
 
#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)
240
132
 
241
133
struct system_variables
242
134
{
243
135
  /*
244
136
    How dynamically allocated system variables are handled:
245
 
    
 
137
 
246
138
    The global_system_variables and max_system_variables are "authoritative"
247
139
    They both should have the same 'version' and 'size'.
248
140
    When attempting to access a dynamic variable, if the session version
249
141
    is out of date, then the session version is updated and realloced if
250
142
    neccessary and bytes copied from global to make up for missing data.
251
 
  */ 
 
143
  */
252
144
  ulong dynamic_variables_version;
253
145
  char* dynamic_variables_ptr;
254
 
  uint dynamic_variables_head;  /* largest valid variable offset */
255
 
  uint dynamic_variables_size;  /* how many bytes are in use */
256
 
  
 
146
  uint32_t dynamic_variables_head;  /* largest valid variable offset */
 
147
  uint32_t dynamic_variables_size;  /* how many bytes are in use */
 
148
 
257
149
  uint64_t myisam_max_extra_sort_file_size;
258
150
  uint64_t myisam_max_sort_file_size;
259
151
  uint64_t max_heap_table_size;
260
152
  uint64_t tmp_table_size;
261
 
  uint64_t long_query_time;
262
153
  ha_rows select_limit;
263
154
  ha_rows max_join_size;
264
 
  ulong auto_increment_increment, auto_increment_offset;
265
 
  ulong bulk_insert_buff_size;
266
 
  ulong join_buff_size;
267
 
  ulong max_allowed_packet;
268
 
  ulong max_error_count;
269
 
  ulong max_length_for_sort_data;
270
 
  ulong max_sort_length;
271
 
  ulong max_tmp_tables;
272
 
  ulong min_examined_row_limit;
273
 
  ulong myisam_repair_threads;
274
 
  ulong myisam_sort_buff_size;
275
 
  ulong myisam_stats_method;
276
 
  ulong net_buffer_length;
277
 
  ulong net_interactive_timeout;
278
 
  ulong net_read_timeout;
279
 
  ulong net_retry_count;
280
 
  ulong net_wait_timeout;
281
 
  ulong net_write_timeout;
282
 
  ulong optimizer_prune_level;
283
 
  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;
284
176
  /*
285
177
    Controls use of Engine-MRR:
286
178
      0 - auto, based on cost
287
179
      1 - force MRR when the storage engine is capable of doing it
288
180
      2 - disable MRR.
289
181
  */
290
 
  ulong optimizer_use_mrr; 
 
182
  uint32_t optimizer_use_mrr;
291
183
  /* A bitmap for switching optimizations on/off */
292
 
  ulong optimizer_switch;
293
 
  ulong preload_buff_size;
294
 
  ulong profiling_history_size;
295
 
  ulong query_cache_type;
296
 
  ulong read_buff_size;
297
 
  ulong read_rnd_buff_size;
298
 
  ulong div_precincrement;
299
 
  ulong sortbuff_size;
300
 
  ulong thread_handling;
301
 
  ulong tx_isolation;
302
 
  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;
303
193
  /* Determines which non-standard SQL behaviour should be enabled */
304
 
  ulong sql_mode;
305
 
  ulong default_week_format;
306
 
  ulong max_seeks_for_key;
307
 
  ulong range_alloc_block_size;
308
 
  ulong query_alloc_block_size;
309
 
  ulong query_prealloc_size;
310
 
  ulong trans_alloc_block_size;
311
 
  ulong trans_prealloc_size;
312
 
  ulong log_warnings;
313
 
  ulong group_concat_max_len;
314
 
  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;
315
204
  /*
316
205
    In slave thread we need to know in behalf of which
317
206
    thread the query is being run to replicate temp tables properly
318
207
  */
319
 
  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;
320
210
 
321
211
  bool low_priority_updates;
322
212
  bool new_mode;
323
 
  /* 
 
213
  /*
324
214
    compatibility option:
325
 
      - 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
326
216
  */
327
217
  bool old_mode;
328
218
  bool engine_condition_pushdown;
355
245
 
356
246
};
357
247
 
 
248
extern struct system_variables global_system_variables;
 
249
 
 
250
#include "sql_lex.h"  /* only for SQLCOM_END */
358
251
 
359
252
/* per thread status variables */
360
253
 
379
272
  ulong ha_update_count;
380
273
  ulong ha_write_count;
381
274
  ulong ha_prepare_count;
382
 
  ulong ha_discover_count;
383
275
  ulong ha_savepoint_count;
384
276
  ulong ha_savepoint_rollback_count;
385
277
 
437
329
 
438
330
#define last_system_status_var questions
439
331
 
440
 
void mark_transaction_to_rollback(THD *thd, bool all);
 
332
void mark_transaction_to_rollback(Session *session, bool all);
441
333
 
442
334
#ifdef DRIZZLE_SERVER
443
335
 
444
 
#define INIT_ARENA_DBUG_INFO is_backup_arena= 0
445
 
 
446
 
class Query_arena
447
 
{
448
 
public:
449
 
  /*
450
 
    List of items created in the parser for this query. Every item puts
451
 
    itself to the list on creation (see Item::Item() for details))
452
 
  */
453
 
  Item *free_list;
454
 
  MEM_ROOT *mem_root;                   // Pointer to current memroot
455
 
  bool is_backup_arena; /* True if this arena is used for backup. */
456
 
 
457
 
  /*
458
 
    The states relfects three diffrent life cycles for three
459
 
    different types of statements:
460
 
    Prepared statement: INITIALIZED -> PREPARED -> EXECUTED.
461
 
    Stored procedure:   INITIALIZED_FOR_SP -> EXECUTED.
462
 
    Other statements:   CONVENTIONAL_EXECUTION never changes.
463
 
  */
464
 
  enum enum_state
465
 
  {
466
 
    INITIALIZED= 0,
467
 
    CONVENTIONAL_EXECUTION= 3, EXECUTED= 4, ERROR= -1
468
 
  };
469
 
 
470
 
  enum_state state;
471
 
 
472
 
  Query_arena(MEM_ROOT *mem_root_arg, enum enum_state state_arg) :
473
 
    free_list(0), mem_root(mem_root_arg), state(state_arg)
474
 
  { INIT_ARENA_DBUG_INFO; }
475
 
  /*
476
 
    This constructor is used only when Query_arena is created as
477
 
    backup storage for another instance of Query_arena.
478
 
  */
479
 
  Query_arena() { INIT_ARENA_DBUG_INFO; }
480
 
 
481
 
  virtual ~Query_arena() {};
482
 
 
483
 
  inline bool is_conventional() const
484
 
  { assert(state == CONVENTIONAL_EXECUTION); return state == CONVENTIONAL_EXECUTION; }
485
 
 
486
 
  inline void* alloc(size_t size) { return alloc_root(mem_root,size); }
487
 
  inline void* calloc(size_t size)
488
 
  {
489
 
    void *ptr;
490
 
    if ((ptr=alloc_root(mem_root,size)))
491
 
      memset(ptr, 0, size);
492
 
    return ptr;
493
 
  }
494
 
  inline char *strdup(const char *str)
495
 
  { return strdup_root(mem_root,str); }
496
 
  inline char *strmake(const char *str, size_t size)
497
 
  { return strmake_root(mem_root,str,size); }
498
 
  inline void *memdup(const void *str, size_t size)
499
 
  { return memdup_root(mem_root,str,size); }
500
 
  inline void *memdup_w_gap(const void *str, size_t size, uint gap)
501
 
  {
502
 
    void *ptr;
503
 
    if ((ptr= alloc_root(mem_root,size+gap)))
504
 
      memcpy(ptr,str,size);
505
 
    return ptr;
506
 
  }
507
 
 
508
 
  void set_query_arena(Query_arena *set);
509
 
 
510
 
  void free_items();
511
 
  /* Close the active state associated with execution of this statement */
512
 
  virtual void cleanup_stmt();
513
 
};
514
 
 
515
 
 
516
336
/**
517
337
  @class Statement
518
338
  @brief State of a single command executed against this connection.
520
340
  One connection can contain a lot of simultaneously running statements,
521
341
  some of which could be:
522
342
   - prepared, that is, contain placeholders,
523
 
  To perform some action with statement we reset THD part to the state  of
524
 
  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
525
345
  to the statement. It will be changed in near future, and Statement will
526
346
  be used explicitly.
527
347
*/
542
362
                        handler of fields used is set
543
363
    MARK_COLUMNS_READ:  Means a bit in read set is set to inform handler
544
364
                        that the field is to be read. If field list contains
545
 
                        duplicates, then thd->dup_field is set to point
 
365
                        duplicates, then session->dup_field is set to point
546
366
                        to the last found duplicate.
547
367
    MARK_COLUMNS_WRITE: Means a bit is set in write set to inform handler
548
368
                        that it needs to update this field in write_row
550
370
  */
551
371
  enum enum_mark_columns mark_used_columns;
552
372
 
553
 
  LEX_STRING name; /* name for named prepared statements */
554
373
  LEX *lex;                                     // parse tree descriptor
555
374
  /*
556
375
    Points to the query associated with this statement. It's const, but
562
381
    LOCK_thread_count mutex. And (B) we are ONLY allowed to set query to a
563
382
    non-NULL value if its previous value is NULL. We do not need to protect
564
383
    operation (B) with any mutex. To avoid crashes in races, if we do not
565
 
    know that thd->query cannot change at the moment, one should print
566
 
    thd->query like this:
 
384
    know that session->query cannot change at the moment, one should print
 
385
    session->query like this:
567
386
      (1) reserve the LOCK_thread_count mutex;
568
 
      (2) check if thd->query is NULL;
569
 
      (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
570
389
      it. We will see the query_length field as either 0, or the right value
571
390
      for it.
572
391
    Assuming that the write and read of an n-bit memory field in an n-bit
573
 
    computer is atomic, we can avoid races in the above way. 
 
392
    computer is atomic, we can avoid races in the above way.
574
393
    This printing is needed at least in SHOW PROCESSLIST and SHOW INNODB
575
394
    STATUS.
576
395
  */
586
405
    valid database name.
587
406
 
588
407
    @note this attribute is set and alloced by the slave SQL thread (for
589
 
    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
590
409
    only responsible for freeing this member.
591
410
  */
592
411
 
593
412
  char *db;
594
 
  uint db_length;
 
413
  uint32_t db_length;
595
414
 
596
415
public:
597
416
 
598
417
  /* This constructor is called for backup statements */
599
418
  Statement() {}
600
419
 
601
 
  Statement(LEX *lex_arg, MEM_ROOT *mem_root_arg,
602
 
            enum enum_state state_arg, ulong id_arg);
 
420
  Statement(LEX *lex_arg, MEM_ROOT *mem_root_arg, ulong id_arg);
603
421
  ~Statement() {}
604
 
 
605
 
  /* Assign execution context (note: not all members) of given stmt to self */
606
 
  void set_statement(Statement *stmt);
607
 
  void set_n_backup_statement(Statement *stmt, Statement *backup);
608
 
  void restore_backup_statement(Statement *stmt, Statement *backup);
609
422
};
610
423
 
611
424
struct st_savepoint {
612
425
  struct st_savepoint *prev;
613
426
  char                *name;
614
 
  uint                 length;
 
427
  uint32_t                 length;
615
428
  Ha_trx_info         *ha_list;
616
429
};
617
430
 
622
435
  /* For now, this is only used to catch duplicated external xids */
623
436
  XID  xid;                           // transaction identifier
624
437
  enum xa_states xa_state;            // used by external XA only
625
 
  bool in_thd;
 
438
  bool in_session;
626
439
} XID_STATE;
627
440
 
628
441
extern pthread_mutex_t LOCK_xid_cache;
636
449
 
637
450
/**
638
451
  @class Security_context
639
 
  @brief A set of THD members describing the current authenticated user.
 
452
  @brief A set of Session members describing the current authenticated user.
640
453
*/
641
454
 
642
455
class Security_context {
643
456
public:
644
 
  Security_context() {}                       /* Remove gcc warning */
 
457
  Security_context() {}
645
458
  /*
646
459
    host - host of the client
647
460
    user - user of the client, set to NULL until the user has been read from
649
462
    priv_user - The user privilege we are using. May be "" for anonymous user.
650
463
    ip - client IP
651
464
  */
652
 
  char *user; 
653
 
  char *ip;
 
465
  std::string user;
 
466
  std::string ip;
654
467
 
655
 
  void init();
656
 
  void destroy();
657
468
  void skip_grants();
658
 
  inline char *priv_host_name()
 
469
  inline const char *priv_host_name()
659
470
  {
660
 
    return (ip ? ip : (char *)"%");
 
471
    return (ip.c_str() ? ip.c_str() : (char *)"%");
661
472
  }
662
473
};
663
474
 
719
530
    Tables that were locked with explicit or implicit LOCK TABLES.
720
531
    (Implicit LOCK TABLES happens when we are prelocking tables for
721
532
     execution of statement which uses stored routines. See description
722
 
     THD::prelocked_mode for more info.)
 
533
     Session::prelocked_mode for more info.)
723
534
  */
724
535
  DRIZZLE_LOCK *locked_tables;
725
536
 
731
542
  DRIZZLE_LOCK *extra_lock;
732
543
 
733
544
  ulong version;
734
 
  uint current_tablenr;
 
545
  uint32_t current_tablenr;
735
546
 
736
547
  enum enum_flags {
737
548
    BACKUPS_AVAIL = (1U << 0)     /* There are backups available */
740
551
  /*
741
552
    Flags with information about the open tables state.
742
553
  */
743
 
  uint state_flags;
 
554
  uint32_t state_flags;
744
555
 
745
556
  /*
746
557
    This constructor serves for creation of Open_tables_state instances
763
574
  }
764
575
};
765
576
 
766
 
/**
767
 
  @class Sub_statement_state
768
 
  @brief Used to save context when executing a function or trigger
769
 
*/
770
 
 
771
 
/* Defines used for Sub_statement_state::in_sub_stmt */
772
 
 
773
 
#define SUB_STMT_TRIGGER 1
774
 
#define SUB_STMT_FUNCTION 2
775
 
 
776
 
 
777
 
class Sub_statement_state
778
 
{
779
 
public:
780
 
  uint64_t options;
781
 
  uint64_t first_successful_insert_id_in_prev_stmt;
782
 
  uint64_t first_successful_insert_id_in_cur_stmt, insert_id_for_cur_row;
783
 
  Discrete_interval auto_inc_interval_for_cur_row;
784
 
  Discrete_intervals_list auto_inc_intervals_forced;
785
 
  uint64_t limit_found_rows;
786
 
  ha_rows    cuted_fields, sent_row_count, examined_row_count;
787
 
  ulong client_capabilities;
788
 
  uint in_sub_stmt;
789
 
  bool enable_slow_log;
790
 
  bool last_insert_id_used;
791
 
  SAVEPOINT *savepoints;
792
 
};
793
 
 
794
 
 
795
 
/* Flags for the THD::system_thread variable */
 
577
 
 
578
/* Flags for the Session::system_thread variable */
796
579
enum enum_thread_type
797
580
{
798
581
  NON_SYSTEM_THREAD,
826
609
    from the anticipated conditions trapped during runtime.
827
610
 
828
611
    This mechanism is similar to C++ try/throw/catch:
829
 
    - 'try' correspond to <code>THD::push_internal_handler()</code>,
 
612
    - 'try' correspond to <code>Session::push_internal_handler()</code>,
830
613
    - 'throw' correspond to <code>my_error()</code>,
831
614
    which invokes <code>my_message_sql()</code>,
832
615
    - 'catch' correspond to checking how/if an internal handler was invoked,
833
616
    before removing it from the exception stack with
834
 
    <code>THD::pop_internal_handler()</code>.
 
617
    <code>Session::pop_internal_handler()</code>.
835
618
 
836
619
    @param sql_errno the error number
837
620
    @param level the error level
838
 
    @param thd the calling thread
 
621
    @param session the calling thread
839
622
    @return true if the error is handled
840
623
  */
841
 
  virtual bool handle_error(uint sql_errno,
 
624
  virtual bool handle_error(uint32_t sql_errno,
842
625
                            const char *message,
843
626
                            DRIZZLE_ERROR::enum_warning_level level,
844
 
                            THD *thd) = 0;
 
627
                            Session *session) = 0;
845
628
};
846
629
 
847
630
 
873
656
  /** Set to make set_error_status after set_{ok,eof}_status possible. */
874
657
  bool can_overwrite_status;
875
658
 
876
 
  void set_ok_status(THD *thd, ha_rows affected_rows_arg,
 
659
  void set_ok_status(Session *session, ha_rows affected_rows_arg,
877
660
                     uint64_t last_insert_id_arg,
878
661
                     const char *message);
879
 
  void set_eof_status(THD *thd);
880
 
  void set_error_status(THD *thd, uint 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);
881
664
 
882
665
  void disable_status();
883
666
 
893
676
  const char *message() const
894
677
  { assert(m_status == DA_ERROR || m_status == DA_OK); return m_message; }
895
678
 
896
 
  uint sql_errno() const
 
679
  uint32_t sql_errno() const
897
680
  { assert(m_status == DA_ERROR); return m_sql_errno; }
898
681
 
899
 
  uint server_status() const
 
682
  uint32_t server_status() const
900
683
  {
901
684
    assert(m_status == DA_OK || m_status == DA_EOF);
902
685
    return m_server_status;
908
691
  uint64_t last_insert_id() const
909
692
  { assert(m_status == DA_OK); return m_last_insert_id; }
910
693
 
911
 
  uint total_warn_count() const
 
694
  uint32_t total_warn_count() const
912
695
  {
913
696
    assert(m_status == DA_OK || m_status == DA_EOF);
914
697
    return m_total_warn_count;
923
706
    SQL error number. One of ER_ codes from share/errmsg.txt.
924
707
    Set by set_error_status.
925
708
  */
926
 
  uint m_sql_errno;
 
709
  uint32_t m_sql_errno;
927
710
 
928
711
  /**
929
 
    Copied from thd->server_status when the diagnostics area is assigned.
 
712
    Copied from session->server_status when the diagnostics area is assigned.
930
713
    We need this member as some places in the code use the following pattern:
931
 
    thd->server_status|= ...
932
 
    my_eof(thd);
933
 
    thd->server_status&= ~...
 
714
    session->server_status|= ...
 
715
    my_eof(session);
 
716
    session->server_status&= ~...
934
717
    Assigned by OK, EOF or ERROR.
935
718
  */
936
 
  uint m_server_status;
 
719
  uint32_t m_server_status;
937
720
  /**
938
721
    The number of rows affected by the last statement. This is
939
 
    semantically close to thd->row_count_func, but has a different
940
 
    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
941
724
    function ROW_COUNT() and is cleared only by statements that
942
725
    update its value, such as INSERT, UPDATE, DELETE and few others.
943
726
    This member is cleared at the beginning of the next statement.
944
727
 
945
 
    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
946
729
    can not be changed.
947
730
  */
948
731
  ha_rows    m_affected_rows;
949
732
  /**
950
733
    Similarly to the previous member, this is a replacement of
951
 
    thd->first_successful_insert_id_in_prev_stmt, which is used
 
734
    session->first_successful_insert_id_in_prev_stmt, which is used
952
735
    to implement LAST_INSERT_ID().
953
736
  */
954
737
  uint64_t   m_last_insert_id;
956
739
  uint       m_total_warn_count;
957
740
  enum_diagnostics_status m_status;
958
741
  /**
959
 
    @todo: the following THD members belong here:
 
742
    @todo: the following Session members belong here:
960
743
    - warn_list, warn_count,
961
744
  */
962
745
};
990
773
 
991
774
 
992
775
/**
993
 
  @class THD
994
 
  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
995
778
  a thread/connection descriptor
996
779
*/
997
780
 
998
 
class THD :public Statement,
 
781
class Session :public Statement,
999
782
           public Open_tables_state
1000
783
{
1001
784
public:
1003
786
  Relay_log_info* rli_fake;
1004
787
 
1005
788
  /*
1006
 
    Constant for THD::where initialization in the beginning of every query.
 
789
    Constant for Session::where initialization in the beginning of every query.
1007
790
 
1008
 
    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
1009
792
    primary (non subselect) query execution.
1010
793
  */
1011
794
  static const char * const DEFAULT_WHERE;
1025
808
  THR_LOCK_OWNER main_lock_id;          // To use for conventional queries
1026
809
  THR_LOCK_OWNER *lock_id;              // If not main_lock_id, points to
1027
810
                                        // the lock_id of a cursor.
1028
 
  pthread_mutex_t LOCK_delete;          // Locked before thd is deleted
 
811
  pthread_mutex_t LOCK_delete;          // Locked before session is deleted
1029
812
  /*
1030
813
    A pointer to the stack frame of handle_one_connection(),
1031
814
    which is called first in the thread for handling a client
1039
822
 
1040
823
  /**
1041
824
    @note
1042
 
    Some members of THD (currently 'Statement::db',
 
825
    Some members of Session (currently 'Statement::db',
1043
826
    'catalog' and 'query')  are set and alloced by the slave SQL thread
1044
 
    (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)
1045
828
    the only responsible for freeing these 3 members. If you add members
1046
829
    here, and you add code to set them in replication, don't forget to
1047
830
    free_them_and_set_them_to_0 in replication properly. For details see
1050
833
    @see handle_slave_sql
1051
834
  */
1052
835
 
1053
 
  Security_context main_security_ctx;
1054
 
  Security_context *security_ctx;
 
836
  Security_context security_ctx;
1055
837
 
1056
838
  /*
1057
839
    Points to info-string that we show in SHOW PROCESSLIST
1058
 
    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
1059
841
    a time-consuming piece that MySQL can get stuck in for a long time.
1060
842
 
1061
 
    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)
1062
844
    macro/function.
1063
845
  */
1064
846
  void        set_proc_info(const char *info) { proc_info= info; }
1079
861
  /*
1080
862
    One thread can hold up to one named user-level lock. This variable
1081
863
    points to a lock object if the lock is present. See item_func.cc and
1082
 
    chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK. 
 
864
    chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK.
1083
865
  */
1084
 
  uint dbug_sentry; // watch out for memory corruption
 
866
  uint32_t dbug_sentry; // watch out for memory corruption
1085
867
  struct st_my_thread_var *mysys_var;
1086
868
  /*
1087
869
    Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. Set from
1095
877
  time_t     start_time, user_time;
1096
878
  uint64_t  connect_utime, thr_create_utime; // track down slow pthread_create
1097
879
  uint64_t  start_utime, utime_after_lock;
1098
 
  
 
880
 
1099
881
  thr_lock_type update_lock_default;
1100
882
 
1101
 
  /* <> 0 if we are inside of trigger or stored function. */
1102
 
  uint in_sub_stmt;
 
883
  /*
 
884
    Both of the following container points in session will be converted to an API.
 
885
  */
1103
886
 
1104
887
  /* container for handler's private per-connection data */
1105
888
  Ha_data ha_data[MAX_HA];
1106
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
 
1107
895
  /* Place to store various things */
1108
 
  void *thd_marker;
1109
 
#ifndef DRIZZLE_CLIENT
1110
 
  int binlog_setup_trx_data();
1111
 
 
1112
 
  /*
1113
 
    Public interface to write RBR events to the binlog
1114
 
  */
1115
 
  void binlog_start_trans_and_stmt();
1116
 
  void binlog_set_stmt_begin();
1117
 
  int binlog_write_table_map(Table *table, bool is_transactional);
1118
 
  int binlog_write_row(Table* table, bool is_transactional,
1119
 
                       const uchar *new_data);
1120
 
  int binlog_delete_row(Table* table, bool is_transactional,
1121
 
                        const uchar *old_data);
1122
 
  int binlog_update_row(Table* table, bool is_transactional,
1123
 
                        const uchar *old_data, const uchar *new_data);
 
896
  void *session_marker;
1124
897
 
1125
898
  void set_server_id(uint32_t sid) { server_id = sid; }
1126
899
 
1127
 
  /*
1128
 
    Member functions to handle pending event for row-level logging.
1129
 
  */
1130
 
  template <class RowsEventT> Rows_log_event*
1131
 
    binlog_prepare_pending_rows_event(Table* table, uint32_t serv_id,
1132
 
                                      size_t needed,
1133
 
                                      bool is_transactional,
1134
 
                                      RowsEventT* hint);
1135
 
  Rows_log_event* binlog_get_pending_rows_event() const;
1136
 
  void            binlog_set_pending_rows_event(Rows_log_event* ev);
1137
 
  int binlog_flush_pending_rows_event(bool stmt_end);
1138
 
 
1139
900
private:
1140
 
  uint binlog_table_maps; // Number of table maps currently in the binlog
1141
 
 
1142
 
  enum enum_binlog_flag {
1143
 
    BINLOG_FLAG_UNSAFE_STMT_PRINTED,
1144
 
    BINLOG_FLAG_COUNT
1145
 
  };
 
901
  uint32_t binlog_table_maps; // Number of table maps currently in the binlog
1146
902
 
1147
903
  /**
1148
904
     Flags with per-thread information regarding the status of the
1150
906
   */
1151
907
  uint32_t binlog_flags;
1152
908
public:
1153
 
  uint get_binlog_table_maps() const {
 
909
  uint32_t get_binlog_table_maps() const {
1154
910
    return binlog_table_maps;
1155
911
  }
1156
912
  void clear_binlog_table_maps() {
1157
913
    binlog_table_maps= 0;
1158
914
  }
1159
 
#endif /* DRIZZLE_CLIENT */
1160
915
 
1161
916
public:
1162
917
 
1163
918
  struct st_transactions {
1164
919
    SAVEPOINT *savepoints;
1165
 
    THD_TRANS all;                      // Trans since BEGIN WORK
1166
 
    THD_TRANS stmt;                     // Trans for current statement
 
920
    Session_TRANS all;                  // Trans since BEGIN WORK
 
921
    Session_TRANS stmt;                 // Trans for current statement
1167
922
    bool on;                            // see ha_enable_transaction()
1168
923
    XID_STATE xid_state;
1169
924
    Rows_log_event *m_pending_rows_event;
1190
945
  } transaction;
1191
946
  Field      *dup_field;
1192
947
  sigset_t signals;
1193
 
#ifdef SIGNAL_WITH_VIO_CLOSE
1194
 
  Vio* active_vio;
1195
 
#endif
1196
948
  /*
1197
949
    This is to track items changed during execution of a prepared
1198
950
    statement/stored procedure. It's created by
1199
 
    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
1200
952
    rollback_item_tree_changes(). For conventional execution it's always
1201
953
    empty.
1202
954
  */
1203
955
  Item_change_list change_list;
1204
956
 
1205
 
  /*
1206
 
    A permanent memory area of the statement. For conventional
1207
 
    execution, the parsed tree and execution runtime reside in the same
1208
 
    memory root. In this case stmt_arena points to THD. In case of
1209
 
    a prepared statement or a stored procedure statement, thd->mem_root
1210
 
    conventionally points to runtime memory, and thd->stmt_arena
1211
 
    points to the memory of the PS/SP, where the parsed tree of the
1212
 
    statement resides. Whenever you need to perform a permanent
1213
 
    transformation of a parsed tree, you should allocate new memory in
1214
 
    stmt_arena, to allow correct re-execution of PS/SP.
1215
 
    Note: in the parser, stmt_arena == thd, even for PS/SP.
1216
 
  */
1217
 
  Query_arena *stmt_arena;
1218
957
  /* Tells if LAST_INSERT_ID(#) was called for the current statement */
1219
958
  bool arg_of_last_insert_id_function;
1220
959
  /*
1231
970
  */
1232
971
  uint64_t  first_successful_insert_id_in_prev_stmt;
1233
972
  /*
1234
 
    Variant of the above, used for storing in statement-based binlog. The
1235
 
    difference is that the one above can change as the execution of a stored
1236
 
    function progresses, while the one below is set once and then does not
1237
 
    change (which is the value which statement-based binlog needs).
1238
 
  */
1239
 
  uint64_t  first_successful_insert_id_in_prev_stmt_for_binlog;
1240
 
  /*
1241
973
    This is the first autogenerated insert id which was *successfully*
1242
974
    inserted by the current statement. It is maintained only to set
1243
975
    first_successful_insert_id_in_prev_stmt when statement ends.
1261
993
    first_successful_insert_id_in_cur_stmt.
1262
994
  */
1263
995
  /*
1264
 
    stmt_depends_on_first_successful_insert_id_in_prev_stmt is set when
1265
 
    LAST_INSERT_ID() is used by a statement.
1266
 
    If it is set, first_successful_insert_id_in_prev_stmt_for_binlog will be
1267
 
    stored in the statement-based binlog.
1268
 
    This variable is CUMULATIVE along the execution of a stored function or
1269
 
    trigger: if one substatement sets it to 1 it will stay 1 until the
1270
 
    function/trigger ends, thus making sure that
1271
 
    first_successful_insert_id_in_prev_stmt_for_binlog does not change anymore
1272
 
    and is propagated to the caller for binlogging.
1273
 
  */
1274
 
  bool       stmt_depends_on_first_successful_insert_id_in_prev_stmt;
1275
 
  /*
1276
996
    List of auto_increment intervals reserved by the thread so far, for
1277
997
    storage in the statement-based binlog.
1278
998
    Note that its minimum is not first_successful_insert_id_in_cur_stmt:
1329
1049
  }
1330
1050
  inline uint64_t read_first_successful_insert_id_in_prev_stmt(void)
1331
1051
  {
1332
 
    if (!stmt_depends_on_first_successful_insert_id_in_prev_stmt)
1333
 
    {
1334
 
      /* It's the first time we read it */
1335
 
      first_successful_insert_id_in_prev_stmt_for_binlog=
1336
 
        first_successful_insert_id_in_prev_stmt;
1337
 
      stmt_depends_on_first_successful_insert_id_in_prev_stmt= 1;
1338
 
    }
1339
1052
    return first_successful_insert_id_in_prev_stmt;
1340
1053
  }
1341
1054
  /*
1368
1081
  /*
1369
1082
    The set of those tables whose fields are referenced in all subqueries
1370
1083
    of the query.
1371
 
    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
1372
1085
    with more than one subquery, it is not clear what does the field mean.
1373
1086
  */
1374
1087
  table_map  used_tables;
1412
1125
  uint       tmp_table, global_read_lock;
1413
1126
  uint       server_status,open_options;
1414
1127
  enum enum_thread_type system_thread;
1415
 
  uint       select_number;             //number of select (used for EXPLAIN)
 
1128
  uint32_t       select_number;             //number of select (used for EXPLAIN)
1416
1129
  /* variables.transaction_isolation is reset to this after each commit */
1417
1130
  enum_tx_isolation session_tx_isolation;
1418
1131
  enum_check_fields count_cuted_fields;
1422
1135
 
1423
1136
  enum killed_state
1424
1137
  {
1425
 
    NOT_KILLED=0,
1426
 
    KILL_BAD_DATA=1,
1427
 
    KILL_CONNECTION=ER_SERVER_SHUTDOWN,
1428
 
    KILL_QUERY=ER_QUERY_INTERRUPTED,
 
1138
    NOT_KILLED,
 
1139
    KILL_BAD_DATA,
 
1140
    KILL_CONNECTION,
 
1141
    KILL_QUERY,
1429
1142
    KILLED_NO_VALUE      /* means neither of the states */
1430
1143
  };
1431
1144
  killed_state volatile killed;
1433
1146
  /* scramble - random string sent to client on handshake */
1434
1147
  char       scramble[SCRAMBLE_LENGTH+1];
1435
1148
 
1436
 
  bool       slave_thread, one_shot_set;
1437
 
  /* tells if current statement should binlog row-based(1) or stmt-based(0) */
1438
 
  bool       current_stmt_binlog_row_based;
 
1149
  bool       slave_thread;
1439
1150
  bool       some_tables_deleted;
1440
1151
  bool       last_cuted_field;
1441
1152
  bool       no_errors, password;
1444
1155
    can not continue. In particular, disables activation of
1445
1156
    CONTINUE or EXIT handlers of stored routines.
1446
1157
    Reset in the end of processing of the current user request, in
1447
 
    @see mysql_reset_thd_for_next_command().
 
1158
    @see mysql_reset_session_for_next_command().
1448
1159
  */
1449
1160
  bool is_fatal_error;
1450
1161
  /**
1465
1176
    Reset to false when we leave the sub-statement mode.
1466
1177
  */
1467
1178
  bool       is_fatal_sub_stmt_error;
1468
 
  bool       query_start_used, rand_used, time_zone_used;
 
1179
  bool       query_start_used;
1469
1180
  /* for IS NULL => = last_insert_id() fix in remove_eq_conds() */
1470
1181
  bool       substitute_null_with_insert_id;
1471
1182
  bool       in_lock_tables;
1476
1187
    it returned an error on master, and this is OK on the slave.
1477
1188
  */
1478
1189
  bool       is_slave_error;
1479
 
  bool       bootstrap, cleanup_done;
1480
 
  
 
1190
  bool       cleanup_done;
 
1191
 
1481
1192
  /**  is set if some thread specific value(s) used in a statement. */
1482
1193
  bool       thread_specific_used;
1483
1194
  bool       charset_is_system_charset, charset_is_collation_connection;
1484
1195
  bool       charset_is_character_set_filesystem;
1485
 
  bool       enable_slow_log;   /* enable slow log for current statement */
1486
1196
  bool       abort_on_warning;
1487
1197
  bool       got_warning;       /* Set on call to push_warning() */
1488
1198
  bool       no_warnings_for_error; /* no warnings on call to my_error() */
1505
1215
    ulong     ulong_value;
1506
1216
    uint64_t uint64_t_value;
1507
1217
  } sys_var_tmp;
1508
 
  
 
1218
 
1509
1219
  struct {
1510
 
    /* 
1511
 
      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
1512
1222
      binlog, and maintain 2 below variables instead (use
1513
 
      mysql_bin_log.start_union_events to turn this on)
 
1223
      drizzle_bin_log.start_union_events to turn this on)
1514
1224
    */
1515
1225
    bool do_union;
1516
1226
    /*
1517
 
      If true, at least one mysql_bin_log::write(Log_event) call has been
1518
 
      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.
1519
1229
    */
1520
1230
    bool unioned_events;
1521
1231
    /*
1522
 
      If true, at least one mysql_bin_log::write(Log_event e), where 
1523
 
      e.cache_stmt == true call has been made after last 
1524
 
      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.
1525
1235
    */
1526
1236
    bool unioned_events_trans;
1527
 
    
1528
 
    /* 
 
1237
 
 
1238
    /*
1529
1239
      'queries' (actually SP statements) that run under inside this binlog
1530
 
      union have thd->query_id >= first_query_id.
 
1240
      union have session->query_id >= first_query_id.
1531
1241
    */
1532
1242
    query_id_t first_query_id;
1533
1243
  } binlog_evt_union;
1541
1251
  */
1542
1252
  Lex_input_stream *m_lip;
1543
1253
 
1544
 
  THD();
1545
 
  ~THD();
 
1254
  Session();
 
1255
  ~Session();
1546
1256
 
1547
1257
  void init(void);
1548
1258
  /*
1549
1259
    Initialize memory roots necessary for query processing and (!)
1550
 
    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
1551
1261
    there are use cases (acl_init, watcher threads,
1552
1262
    killing mysqld) where it's vital to not allocate excessive and not used
1553
1263
    memory. Note, that we still don't return error from init_for_queries():
1554
1264
    if preallocation fails, we should notice that at the first call to
1555
 
    alloc_root. 
 
1265
    alloc_root.
1556
1266
  */
1557
1267
  void init_for_queries();
1558
 
  void change_user(void);
1559
1268
  void cleanup(void);
1560
1269
  void cleanup_after_query();
1561
1270
  bool store_globals();
1562
 
#ifdef SIGNAL_WITH_VIO_CLOSE
1563
 
  inline void set_active_vio(Vio* vio)
1564
 
  {
1565
 
    pthread_mutex_lock(&LOCK_delete);
1566
 
    active_vio = vio;
1567
 
    pthread_mutex_unlock(&LOCK_delete);
1568
 
  }
1569
 
  inline void clear_active_vio()
1570
 
  {
1571
 
    pthread_mutex_lock(&LOCK_delete);
1572
 
    active_vio = 0;
1573
 
    pthread_mutex_unlock(&LOCK_delete);
1574
 
  }
1575
 
  void close_active_vio();
1576
 
#endif
1577
 
  void awake(THD::killed_state state_to_set);
 
1271
  void awake(Session::killed_state state_to_set);
1578
1272
 
1579
 
#ifndef DRIZZLE_CLIENT
1580
1273
  enum enum_binlog_query_type {
1581
1274
    /*
1582
1275
      The query can be logged row-based or statement-based
1583
1276
    */
1584
1277
    ROW_QUERY_TYPE,
1585
 
    
 
1278
 
1586
1279
    /*
1587
1280
      The query has to be logged statement-based
1588
1281
    */
1589
1282
    STMT_QUERY_TYPE,
1590
 
    
 
1283
 
1591
1284
    /*
1592
1285
      The query represents a change to a table in the "mysql"
1593
1286
      database and is currently mapped to ROW_QUERY_TYPE.
1595
1288
    DRIZZLE_QUERY_TYPE,
1596
1289
    QUERY_TYPE_COUNT
1597
1290
  };
1598
 
  
1599
 
  int binlog_query(enum_binlog_query_type qtype,
1600
 
                   char const *query, ulong query_len,
1601
 
                   bool is_trans, bool suppress_use,
1602
 
                   THD::killed_state killed_err_arg= THD::KILLED_NO_VALUE);
1603
 
#endif
1604
1291
 
1605
1292
  /*
1606
1293
    For enter_cond() / exit_cond() to work the mutex must be got before
1614
1301
    safe_mutex_assert_owner(mutex);
1615
1302
    mysys_var->current_mutex = mutex;
1616
1303
    mysys_var->current_cond = cond;
1617
 
    thd_proc_info(this, msg);
 
1304
    this->set_proc_info(msg);
1618
1305
    return old_msg;
1619
1306
  }
1620
1307
  inline void exit_cond(const char* old_msg)
1623
1310
      Putting the mutex unlock in exit_cond() ensures that
1624
1311
      mysys_var->current_mutex is always unlocked _before_ mysys_var->mutex is
1625
1312
      locked (if that would not be the case, you'll get a deadlock if someone
1626
 
      does a THD::awake() on you).
 
1313
      does a Session::awake() on you).
1627
1314
    */
1628
1315
    pthread_mutex_unlock(mysys_var->current_mutex);
1629
1316
    pthread_mutex_lock(&mysys_var->mutex);
1630
1317
    mysys_var->current_mutex = 0;
1631
1318
    mysys_var->current_cond = 0;
1632
 
    thd_proc_info(this, old_msg);
 
1319
    this->set_proc_info(old_msg);
1633
1320
    pthread_mutex_unlock(&mysys_var->mutex);
1634
1321
  }
1635
1322
  inline time_t query_start() { query_start_used=1; return start_time; }
1643
1330
    else
1644
1331
      start_utime= utime_after_lock= my_micro_time_and_time(&start_time);
1645
1332
  }
1646
 
  inline void   set_current_time()    { start_time= my_time(MY_WME); }
 
1333
  inline void   set_current_time()    { start_time= time(NULL); }
1647
1334
  inline void   set_time(time_t t)
1648
1335
  {
1649
1336
    start_time= user_time= t;
1669
1356
  }
1670
1357
 
1671
1358
  LEX_STRING *make_lex_string(LEX_STRING *lex_str,
1672
 
                              const char* str, uint length,
 
1359
                              const char* str, uint32_t length,
1673
1360
                              bool allocate_lex_string);
1674
1361
 
1675
1362
  bool convert_string(LEX_STRING *to, const CHARSET_INFO * const to_cs,
1676
 
                      const char *from, uint from_length,
 
1363
                      const char *from, uint32_t from_length,
1677
1364
                      const CHARSET_INFO * const from_cs);
1678
1365
 
1679
1366
  bool convert_string(String *s, const CHARSET_INFO * const from_cs, const CHARSET_INFO * const to_cs);
1697
1384
    return;
1698
1385
  }
1699
1386
  inline bool vio_ok() const { return net.vio != 0; }
1700
 
  /** Return false if connection to client is broken. */
1701
 
  bool vio_is_connected();
 
1387
 
1702
1388
  /**
1703
1389
    Mark the current error as fatal. Warning: this does not
1704
1390
    set any error, it sets a property of the error, so must be
1728
1414
 
1729
1415
  void change_item_tree(Item **place, Item *new_value)
1730
1416
  {
1731
 
    /* TODO: check for OOM condition here */
1732
 
    if (!stmt_arena->is_conventional())
1733
 
      nocheck_register_item_tree_change(place, *place, mem_root);
1734
1417
    *place= new_value;
1735
1418
  }
1736
1419
  void nocheck_register_item_tree_change(Item **place, Item *old_value,
1756
1439
  void set_status_var_init();
1757
1440
  void reset_n_backup_open_tables_state(Open_tables_state *backup);
1758
1441
  void restore_backup_open_tables_state(Open_tables_state *backup);
1759
 
  void restore_sub_statement_state(Sub_statement_state *backup);
1760
 
  void set_n_backup_active_arena(Query_arena *set, Query_arena *backup);
1761
 
  void restore_active_arena(Query_arena *set, Query_arena *backup);
1762
 
 
1763
 
  inline void set_current_stmt_binlog_row_based_if_mixed()
1764
 
  {
1765
 
    /*
1766
 
      If in a stored/function trigger, the caller should already have done the
1767
 
      change. We test in_sub_stmt to prevent introducing bugs where people
1768
 
      wouldn't ensure that, and would switch to row-based mode in the middle
1769
 
      of executing a stored function/trigger (which is too late, see also
1770
 
      reset_current_stmt_binlog_row_based()); this condition will make their
1771
 
      tests fail and so force them to propagate the
1772
 
      lex->binlog_row_based_if_mixed upwards to the caller.
1773
 
    */
1774
 
    if ((variables.binlog_format == BINLOG_FORMAT_MIXED) &&
1775
 
        (in_sub_stmt == 0))
1776
 
      current_stmt_binlog_row_based= true;
1777
 
  }
1778
 
  inline void set_current_stmt_binlog_row_based()
1779
 
  {
1780
 
    current_stmt_binlog_row_based= true;
1781
 
  }
1782
 
  inline void clear_current_stmt_binlog_row_based()
1783
 
  {
1784
 
    current_stmt_binlog_row_based= false;
1785
 
  }
1786
 
  inline void reset_current_stmt_binlog_row_based()
1787
 
  {
1788
 
    /*
1789
 
      If there are temporary tables, don't reset back to
1790
 
      statement-based. Indeed it could be that:
1791
 
      CREATE TEMPORARY TABLE t SELECT UUID(); # row-based
1792
 
      # and row-based does not store updates to temp tables
1793
 
      # in the binlog.
1794
 
      INSERT INTO u SELECT * FROM t; # stmt-based
1795
 
      and then the INSERT will fail as data inserted into t was not logged.
1796
 
      So we continue with row-based until the temp table is dropped.
1797
 
      If we are in a stored function or trigger, we mustn't reset in the
1798
 
      middle of its execution (as the binary logging way of a stored function
1799
 
      or trigger is decided when it starts executing, depending for example on
1800
 
      the caller (for a stored function: if caller is SELECT or
1801
 
      INSERT/UPDATE/DELETE...).
1802
 
 
1803
 
      Don't reset binlog format for NDB binlog injector thread.
1804
 
    */
1805
 
    if ((temporary_tables == NULL) && (in_sub_stmt == 0))
1806
 
    {
1807
 
      current_stmt_binlog_row_based= 
1808
 
        test(variables.binlog_format == BINLOG_FORMAT_ROW);
1809
 
    }
1810
 
  }
1811
1442
 
1812
1443
  /**
1813
1444
    Set the current database; use deep copy of C-string.
1830
1461
      @retval false Success
1831
1462
      @retval true  Out-of-memory error
1832
1463
  */
1833
 
  bool set_db(const char *new_db, size_t new_db_len)
1834
 
  {
1835
 
    /* Do not reallocate memory if current chunk is big enough. */
1836
 
    if (db && new_db && db_length >= new_db_len)
1837
 
      memcpy(db, new_db, new_db_len+1);
1838
 
    else
1839
 
    {
1840
 
      x_free(db);
1841
 
      if (new_db)
1842
 
        db= my_strndup(new_db, new_db_len, MYF(MY_WME | ME_FATALERROR));
1843
 
      else
1844
 
        db= NULL;
1845
 
    }
1846
 
    db_length= db ? new_db_len : 0;
1847
 
    return new_db && !db;
1848
 
  }
 
1464
  bool set_db(const char *new_db, size_t new_db_len);
1849
1465
 
1850
1466
  /**
1851
1467
    Set the current database; use shallow copy of C-string.
1869
1485
    a statement is parsed but before it's executed.
1870
1486
  */
1871
1487
  bool copy_db_to(char **p_db, size_t *p_db_length);
1872
 
  thd_scheduler scheduler;
 
1488
  session_scheduler scheduler;
1873
1489
 
1874
1490
public:
1875
1491
  /**
1884
1500
    @param level the error level
1885
1501
    @return true if the error is handled
1886
1502
  */
1887
 
  virtual bool handle_error(uint sql_errno, const char *message,
 
1503
  virtual bool handle_error(uint32_t sql_errno, const char *message,
1888
1504
                            DRIZZLE_ERROR::enum_warning_level level);
1889
1505
 
1890
1506
  /**
1892
1508
  */
1893
1509
  void pop_internal_handler();
1894
1510
 
 
1511
  /**
 
1512
    Close the current connection.
 
1513
  */
 
1514
  void close_connection(uint32_t errcode, bool lock);
 
1515
 
1895
1516
private:
1896
1517
  const char *proc_info;
1897
1518
 
1916
1537
};
1917
1538
 
1918
1539
 
1919
 
/** A short cut for thd->main_da.set_ok_status(). */
 
1540
 
 
1541
/** A short cut for session->main_da.set_ok_status(). */
1920
1542
 
1921
1543
inline void
1922
 
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,
1923
1545
        const char *message= NULL)
1924
1546
{
1925
 
  thd->main_da.set_ok_status(thd, affected_rows, id, message);
 
1547
  session->main_da.set_ok_status(session, affected_rows, id, message);
1926
1548
}
1927
1549
 
1928
1550
 
1929
 
/** A short cut for thd->main_da.set_eof_status(). */
 
1551
/** A short cut for session->main_da.set_eof_status(). */
1930
1552
 
1931
1553
inline void
1932
 
my_eof(THD *thd)
 
1554
my_eof(Session *session)
1933
1555
{
1934
 
  thd->main_da.set_eof_status(thd);
 
1556
  session->main_da.set_eof_status(session);
1935
1557
}
1936
1558
 
1937
1559
#define tmp_disable_binlog(A)       \
1950
1572
class sql_exchange :public Sql_alloc
1951
1573
{
1952
1574
public:
1953
 
  enum enum_filetype filetype; /* load XML, Added by Arnold & Erik */ 
 
1575
  enum enum_filetype filetype; /* load XML, Added by Arnold & Erik */
1954
1576
  char *file_name;
1955
1577
  String *field_term,*enclosed,*line_term,*line_start,*escaped;
1956
1578
  bool opt_enclosed;
1971
1593
 
1972
1594
class select_result :public Sql_alloc {
1973
1595
protected:
1974
 
  THD *thd;
 
1596
  Session *session;
1975
1597
  SELECT_LEX_UNIT *unit;
1976
1598
public:
1977
1599
  select_result();
1978
1600
  virtual ~select_result() {};
1979
 
  virtual int prepare(List<Item> &list __attribute__((unused)),
 
1601
  virtual int prepare(List<Item> &,
1980
1602
                      SELECT_LEX_UNIT *u)
1981
1603
  {
1982
1604
    unit= u;
1988
1610
    we need to know number of columns in the result set (if
1989
1611
    there is a result set) apart from sending columns metadata.
1990
1612
  */
1991
 
  virtual uint field_count(List<Item> &fields) const
 
1613
  virtual uint32_t field_count(List<Item> &fields) const
1992
1614
  { return fields.elements; }
1993
 
  virtual bool send_fields(List<Item> &list, uint flags)=0;
 
1615
  virtual bool send_fields(List<Item> &list, uint32_t flags)=0;
1994
1616
  virtual bool send_data(List<Item> &items)=0;
1995
 
  virtual bool initialize_tables (JOIN  __attribute__((unused)) *join=0)
 
1617
  virtual bool initialize_tables (JOIN *)
1996
1618
  { return 0; }
1997
 
  virtual void send_error(uint errcode,const char *err);
 
1619
  virtual void send_error(uint32_t errcode,const char *err);
1998
1620
  virtual bool send_eof()=0;
1999
1621
  /**
2000
1622
    Check if this query returns a result set and therefore is allowed in
2010
1632
    statement/stored procedure.
2011
1633
  */
2012
1634
  virtual void cleanup();
2013
 
  void set_thd(THD *thd_arg) { thd= thd_arg; }
 
1635
  void set_session(Session *session_arg) { session= session_arg; }
2014
1636
  void begin_dataset() {}
2015
1637
};
2016
1638
 
2025
1647
{
2026
1648
public:
2027
1649
  select_result_interceptor() {}              /* Remove gcc warning */
2028
 
  uint field_count(List<Item> &fields __attribute__((unused))) const
 
1650
  uint32_t field_count(List<Item> &) const
2029
1651
  { return 0; }
2030
 
  bool send_fields(List<Item> &fields __attribute__((unused)),
2031
 
                   uint flag __attribute__((unused))) { return false; }
 
1652
  bool send_fields(List<Item> &,
 
1653
                   uint32_t)
 
1654
  { return false; }
2032
1655
};
2033
1656
 
2034
1657
 
2041
1664
  bool is_result_set_started;
2042
1665
public:
2043
1666
  select_send() :is_result_set_started(false) {}
2044
 
  bool send_fields(List<Item> &list, uint flags);
 
1667
  bool send_fields(List<Item> &list, uint32_t flags);
2045
1668
  bool send_data(List<Item> &items);
2046
1669
  bool send_eof();
2047
1670
  virtual bool check_simple_select() const { return false; }
2062
1685
  select_to_file(sql_exchange *ex) :exchange(ex), file(-1),row_count(0L)
2063
1686
  { path[0]=0; }
2064
1687
  ~select_to_file();
2065
 
  void send_error(uint errcode,const char *err);
 
1688
  void send_error(uint32_t errcode,const char *err);
2066
1689
  bool send_eof();
2067
1690
  void cleanup();
2068
1691
};
2078
1701
 
2079
1702
 
2080
1703
class select_export :public select_to_file {
2081
 
  uint field_term_length;
 
1704
  uint32_t field_term_length;
2082
1705
  int field_sep_char,escape_char,line_sep_char;
2083
1706
  int field_term_char; // first char of FIELDS TERMINATED BY or MAX_INT
2084
1707
  /*
2134
1757
  bool send_data(List<Item> &items);
2135
1758
  virtual void store_values(List<Item> &values);
2136
1759
  virtual bool can_rollback_data() { return 0; }
2137
 
  void send_error(uint errcode,const char *err);
 
1760
  void send_error(uint32_t errcode,const char *err);
2138
1761
  bool send_eof();
2139
1762
  void abort();
2140
1763
  /* not implemented: select_insert is never re-used in prepared statements */
2151
1774
  Field **field;
2152
1775
  /* lock data for tmp table */
2153
1776
  DRIZZLE_LOCK *m_lock;
2154
 
  /* m_lock or thd->extra_lock */
 
1777
  /* m_lock or session->extra_lock */
2155
1778
  DRIZZLE_LOCK **m_plock;
2156
1779
public:
2157
1780
  select_create (TableList *table_arg,
2168
1791
    {}
2169
1792
  int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
2170
1793
 
2171
 
  void binlog_show_create_table(Table **tables, uint count);
 
1794
  void binlog_show_create_table(Table **tables, uint32_t count);
2172
1795
  void store_values(List<Item> &values);
2173
 
  void send_error(uint errcode,const char *err);
 
1796
  void send_error(uint32_t errcode,const char *err);
2174
1797
  bool send_eof();
2175
1798
  void abort();
2176
1799
  virtual bool can_rollback_data() { return 1; }
2177
1800
 
2178
 
  // Needed for access from local class MY_HOOKS in prepare(), since thd is proteted.
2179
 
  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; }
2180
1803
  const HA_CREATE_INFO *get_create_info() { return create_info; };
2181
1804
  int prepare2(void) { return 0; }
2182
1805
};
2183
1806
 
2184
1807
#include <storage/myisam/myisam.h>
2185
1808
 
2186
 
/* 
2187
 
  Param to create temporary tables when doing SELECT:s 
 
1809
/*
 
1810
  Param to create temporary tables when doing SELECT:s
2188
1811
  NOTE
2189
1812
    This structure is copied using memcpy as a part of JOIN.
2190
1813
*/
2201
1824
  List<Item> save_copy_funcs;
2202
1825
  Copy_field *copy_field, *copy_field_end;
2203
1826
  Copy_field *save_copy_field, *save_copy_field_end;
2204
 
  uchar     *group_buff;
 
1827
  unsigned char     *group_buff;
2205
1828
  Item      **items_to_copy;                    /* Fields in tmp table */
2206
1829
  MI_COLUMNDEF *recinfo,*start_recinfo;
2207
1830
  KEY *keyinfo;
2208
1831
  ha_rows end_write_records;
2209
1832
  uint  field_count,sum_func_count,func_count;
2210
 
  uint  hidden_field_count;
 
1833
  uint32_t  hidden_field_count;
2211
1834
  uint  group_parts,group_length,group_null_parts;
2212
1835
  uint  quick_group;
2213
1836
  bool  using_indirect_summary_function;
2214
1837
  /* If >0 convert all blob fields to varchar(convert_blob_length) */
2215
 
  uint  convert_blob_length; 
2216
 
  const CHARSET_INFO *table_charset; 
 
1838
  uint32_t  convert_blob_length;
 
1839
  const CHARSET_INFO *table_charset;
2217
1840
  bool schema_table;
2218
1841
  /*
2219
1842
    True if GROUP BY and its aggregate functions are already computed
2241
1864
    cleanup();
2242
1865
  }
2243
1866
  void init(void);
2244
 
  inline void cleanup(void)
2245
 
  {
2246
 
    if (copy_field)                             /* Fix for Intel compiler */
2247
 
    {
2248
 
      delete [] copy_field;
2249
 
      save_copy_field= copy_field= 0;
2250
 
    }
2251
 
  }
 
1867
  void cleanup(void);
2252
1868
};
2253
1869
 
2254
1870
class select_union :public select_result_interceptor
2263
1879
  bool send_eof();
2264
1880
  bool flush();
2265
1881
  void cleanup();
2266
 
  bool create_result_table(THD *thd, List<Item> *column_types,
 
1882
  bool create_result_table(Session *session, List<Item> *column_types,
2267
1883
                           bool is_distinct, uint64_t options,
2268
1884
                           const char *alias, bool bit_fields_as_long);
2269
1885
};
2322
1938
  Field *field;                         /* Field to sort */
2323
1939
  Item  *item;                          /* Item if not sorting fields */
2324
1940
  uint   length;                        /* Length of sort field */
2325
 
  uint   suffix_length;                 /* Length suffix (0-4) */
 
1941
  uint32_t   suffix_length;                 /* Length suffix (0-4) */
2326
1942
  Item_result result_type;              /* Type of item */
2327
1943
  bool reverse;                         /* if descending sort */
2328
1944
  bool need_strxnfrm;                   /* If we have to use strxnfrm() */
2330
1946
 
2331
1947
 
2332
1948
typedef struct st_sort_buffer {
2333
 
  uint index;                                   /* 0 or 1 */
2334
 
  uint sort_orders;
2335
 
  uint change_pos;                              /* If sort-fields changed */
 
1949
  uint32_t index;                                       /* 0 or 1 */
 
1950
  uint32_t sort_orders;
 
1951
  uint32_t change_pos;                          /* If sort-fields changed */
2336
1952
  char **buff;
2337
1953
  SORT_FIELD *sortorder;
2338
1954
} SORT_BUFFER;
2345
1961
  LEX_STRING db;
2346
1962
  LEX_STRING table;
2347
1963
  SELECT_LEX_UNIT *sel;
2348
 
  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,
2349
1965
                     bool force)
2350
1966
    :table(table_arg), sel((SELECT_LEX_UNIT *)0)
2351
1967
  {
2352
 
    if (!force && (thd->client_capabilities & CLIENT_NO_SCHEMA))
 
1968
    if (!force && (session->client_capabilities & CLIENT_NO_SCHEMA))
2353
1969
      db.str=0;
2354
1970
    else
2355
1971
      db= db_arg;
2356
1972
  }
2357
 
  inline Table_ident(LEX_STRING table_arg) 
 
1973
  inline Table_ident(LEX_STRING table_arg)
2358
1974
    :table(table_arg), sel((SELECT_LEX_UNIT *)0)
2359
1975
  {
2360
1976
    db.str=0;
2394
2010
 
2395
2011
  double val_real(bool *null_value);
2396
2012
  int64_t val_int(bool *null_value) const;
2397
 
  String *val_str(bool *null_value, String *str, uint decimals);
 
2013
  String *val_str(bool *null_value, String *str, uint32_t decimals);
2398
2014
  my_decimal *val_decimal(bool *null_value, my_decimal *result);
2399
2015
  DTCollation collation;
2400
2016
};
2401
2017
 
2402
2018
/*
2403
 
   Unique -- class for unique (removing of duplicates). 
 
2019
   Unique -- class for unique (removing of duplicates).
2404
2020
   Puts all values to the TREE. If the tree becomes too big,
2405
2021
   it's dumped to the file. User can request sorted values, or
2406
2022
   just iterate through them. In the last case tree merging is performed in
2411
2027
{
2412
2028
  DYNAMIC_ARRAY file_ptrs;
2413
2029
  ulong max_elements;
2414
 
  uint64_t max_in_memory_size;
 
2030
  size_t max_in_memory_size;
2415
2031
  IO_CACHE file;
2416
2032
  TREE tree;
2417
 
  uchar *record_pointers;
 
2033
  unsigned char *record_pointers;
2418
2034
  bool flush();
2419
 
  uint size;
 
2035
  uint32_t size;
2420
2036
 
2421
2037
public:
2422
2038
  ulong elements;
2423
2039
  Unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
2424
 
         uint size_arg, uint64_t max_in_memory_size_arg);
 
2040
         uint32_t size_arg, size_t max_in_memory_size_arg);
2425
2041
  ~Unique();
2426
2042
  ulong elements_in_tree() { return tree.elements_in_tree; }
2427
2043
  inline bool unique_add(void *ptr)
2432
2048
  }
2433
2049
 
2434
2050
  bool get(Table *table);
2435
 
  static double get_use_cost(uint *buffer, uint nkeys, uint key_size, 
2436
 
                             uint64_t max_in_memory_size);
2437
 
  inline static int get_cost_calc_buff_size(ulong nkeys, uint key_size, 
2438
 
                                            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)
2439
2055
  {
2440
 
    register uint64_t max_elems_in_tree=
 
2056
    register size_t max_elems_in_tree=
2441
2057
      (1 + max_in_memory_size / ALIGN_SIZE(sizeof(TREE_ELEMENT)+key_size));
2442
2058
    return (int) (sizeof(uint)*(1 + nkeys/max_elems_in_tree));
2443
2059
  }
2445
2061
  void reset();
2446
2062
  bool walk(tree_walk_action action, void *walk_action_arg);
2447
2063
 
2448
 
  friend int unique_write_to_file(uchar* key, element_count count, Unique *unique);
2449
 
  friend int unique_write_to_ptrs(uchar* key, element_count count, Unique *unique);
 
2064
  friend int unique_write_to_file(unsigned char* key, element_count count, Unique *unique);
 
2065
  friend int unique_write_to_ptrs(unsigned char* key, element_count count, Unique *unique);
2450
2066
};
2451
2067
 
2452
2068
 
2455
2071
  TableList *delete_tables, *table_being_deleted;
2456
2072
  Unique **tempfiles;
2457
2073
  ha_rows deleted, found;
2458
 
  uint num_of_tables;
 
2074
  uint32_t num_of_tables;
2459
2075
  int error;
2460
2076
  bool do_delete;
2461
2077
  /* True if at least one table we delete from is transactional */
2470
2086
  bool error_handled;
2471
2087
 
2472
2088
public:
2473
 
  multi_delete(TableList *dt, uint num_of_tables);
 
2089
  multi_delete(TableList *dt, uint32_t num_of_tables);
2474
2090
  ~multi_delete();
2475
2091
  int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
2476
2092
  bool send_data(List<Item> &items);
2477
2093
  bool initialize_tables (JOIN *join);
2478
 
  void send_error(uint errcode,const char *err);
 
2094
  void send_error(uint32_t errcode,const char *err);
2479
2095
  int  do_deletes();
2480
2096
  bool send_eof();
2481
2097
  virtual void abort();
2492
2108
  ha_rows updated, found;
2493
2109
  List <Item> *fields, *values;
2494
2110
  List <Item> **fields_for_table, **values_for_table;
2495
 
  uint table_count;
 
2111
  uint32_t table_count;
2496
2112
  /*
2497
2113
   List of tables referenced in the CHECK OPTION condition of
2498
 
   the updated view excluding the updated table. 
 
2114
   the updated view excluding the updated table.
2499
2115
  */
2500
2116
  List <Table> unupdated_check_opt_tables;
2501
2117
  Copy_field *copy_field;
2504
2120
  /* True if the update operation has made a change in a transactional table */
2505
2121
  bool transactional_tables;
2506
2122
  bool ignore;
2507
 
  /* 
 
2123
  /*
2508
2124
     error handling (rollback and binlogging) can happen in send_eof()
2509
2125
     so that afterward send_error() needs to find out that.
2510
2126
  */
2518
2134
  int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
2519
2135
  bool send_data(List<Item> &items);
2520
2136
  bool initialize_tables (JOIN *join);
2521
 
  void send_error(uint errcode,const char *err);
 
2137
  void send_error(uint32_t errcode,const char *err);
2522
2138
  int  do_updates();
2523
2139
  bool send_eof();
2524
2140
  virtual void abort();
2528
2144
public:
2529
2145
  LEX_STRING s;
2530
2146
  bool local;
2531
 
  uint offset;
 
2147
  uint32_t offset;
2532
2148
  enum_field_types type;
2533
 
  my_var (LEX_STRING& j, bool i, uint o, enum_field_types t)
 
2149
  my_var (LEX_STRING& j, bool i, uint32_t o, enum_field_types t)
2534
2150
    :s(j), local(i), offset(o), type(t)
2535
2151
  {}
2536
2152
  ~my_var() {}
2551
2167
 
2552
2168
/* Bits in sql_command_flags */
2553
2169
 
2554
 
#define CF_CHANGES_DATA         1
2555
 
#define CF_HAS_ROW_COUNT        2
2556
 
#define CF_STATUS_COMMAND       4
2557
 
#define CF_SHOW_TABLE_COMMAND   8
2558
 
#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);
2559
2184
 
2560
2185
/* Functions in sql_class.cc */
2561
2186
 
2564
2189
void add_diff_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var,
2565
2190
                        STATUS_VAR *dec_var);
2566
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
 
2567
2214
#endif /* DRIZZLE_SERVER */
 
2215
 
 
2216
#endif /* DRIZZLED_SQL_CLASS_H */