~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_class.h

  • Committer: Monty Taylor
  • Date: 2008-08-04 19:37:18 UTC
  • mto: (261.2.2 codestyle)
  • mto: This revision was merged to the branch mainline in revision 262.
  • Revision ID: monty@inaugust.com-20080804193718-f0rz13uli4429ozb
Changed gettext_noop() to N_()

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
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
 
23
16
 
24
17
/* Classes in mysql */
25
18
 
26
 
#include <drizzled/global.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/sql_error.h>
37
 
#include <bitset>
 
19
#ifdef USE_PRAGMA_INTERFACE
 
20
#pragma interface                       /* gcc class implementation */
 
21
#endif
 
22
 
 
23
#include <drizzled/plugin_audit.h>
 
24
#include "log.h"
 
25
#include "rpl_tblmap.h"
38
26
 
39
27
class Relay_log_info;
40
28
 
43
31
class Slave_log_event;
44
32
class Lex_input_stream;
45
33
class Rows_log_event;
46
 
class user_var_entry;
47
 
class Copy_field;
48
 
class Table_ident;
49
34
 
 
35
enum enum_enable_or_disable { LEAVE_AS_IS, ENABLE, DISABLE };
 
36
enum enum_ha_read_modes { RFIRST, RNEXT, RPREV, RLAST, RKEY, RNEXT_SAME };
 
37
enum enum_duplicates { DUP_ERROR, DUP_REPLACE, DUP_UPDATE };
 
38
enum enum_delay_key_write { DELAY_KEY_WRITE_NONE, DELAY_KEY_WRITE_ON,
 
39
                            DELAY_KEY_WRITE_ALL };
 
40
enum enum_slave_exec_mode { SLAVE_EXEC_MODE_STRICT,
 
41
                            SLAVE_EXEC_MODE_IDEMPOTENT,
 
42
                            SLAVE_EXEC_MODE_LAST_BIT};
 
43
enum enum_mark_columns
 
44
{ MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE};
 
45
enum enum_filetype { FILETYPE_CSV, FILETYPE_XML };
50
46
 
51
47
extern char internal_table_name[2];
52
48
extern char empty_c_string[1];
57
53
 
58
54
#define TC_HEURISTIC_RECOVER_COMMIT   1
59
55
#define TC_HEURISTIC_RECOVER_ROLLBACK 2
60
 
extern uint32_t tc_heuristic_recover;
 
56
extern uint tc_heuristic_recover;
61
57
 
62
58
typedef struct st_user_var_events
63
59
{
65
61
  char *value;
66
62
  ulong length;
67
63
  Item_result type;
68
 
  uint32_t charset_number;
 
64
  uint charset_number;
69
65
} BINLOG_USER_VAR_EVENT;
70
66
 
71
67
#define RP_LOCK_LOG_IS_ALREADY_LOCKED 1
100
96
} COPY_INFO;
101
97
 
102
98
 
103
 
 
104
 
 
 
99
class Key_part_spec :public Sql_alloc {
 
100
public:
 
101
  LEX_STRING field_name;
 
102
  uint length;
 
103
  Key_part_spec(const LEX_STRING &name, uint len)
 
104
    : field_name(name), length(len)
 
105
  {}
 
106
  Key_part_spec(const char *name, const size_t name_len, uint len)
 
107
    : length(len)
 
108
  { field_name.str= (char *)name; field_name.length= name_len; }
 
109
  bool operator==(const Key_part_spec& other) const;
 
110
  /**
 
111
    Construct a copy of this Key_part_spec. field_name is copied
 
112
    by-pointer as it is known to never change. At the same time
 
113
    'length' may be reset in mysql_prepare_create_table, and this
 
114
    is why we supply it with a copy.
 
115
 
 
116
    @return If out of memory, 0 is returned and an error is set in
 
117
    THD.
 
118
  */
 
119
  Key_part_spec *clone(MEM_ROOT *mem_root) const
 
120
  { return new (mem_root) Key_part_spec(*this); }
 
121
};
 
122
 
 
123
 
 
124
class Alter_drop :public Sql_alloc {
 
125
public:
 
126
  enum drop_type {KEY, COLUMN };
 
127
  const char *name;
 
128
  enum drop_type type;
 
129
  Alter_drop(enum drop_type par_type,const char *par_name)
 
130
    :name(par_name), type(par_type) {}
 
131
  /**
 
132
    Used to make a clone of this object for ALTER/CREATE TABLE
 
133
    @sa comment for Key_part_spec::clone
 
134
  */
 
135
  Alter_drop *clone(MEM_ROOT *mem_root) const
 
136
    { return new (mem_root) Alter_drop(*this); }
 
137
};
 
138
 
 
139
 
 
140
class Alter_column :public Sql_alloc {
 
141
public:
 
142
  const char *name;
 
143
  Item *def;
 
144
  Alter_column(const char *par_name,Item *literal)
 
145
    :name(par_name), def(literal) {}
 
146
  /**
 
147
    Used to make a clone of this object for ALTER/CREATE TABLE
 
148
    @sa comment for Key_part_spec::clone
 
149
  */
 
150
  Alter_column *clone(MEM_ROOT *mem_root) const
 
151
    { return new (mem_root) Alter_column(*this); }
 
152
};
 
153
 
 
154
 
 
155
class Key :public Sql_alloc {
 
156
public:
 
157
  enum Keytype { PRIMARY, UNIQUE, MULTIPLE, FOREIGN_KEY};
 
158
  enum Keytype type;
 
159
  KEY_CREATE_INFO key_create_info;
 
160
  List<Key_part_spec> columns;
 
161
  LEX_STRING name;
 
162
  bool generated;
 
163
 
 
164
  Key(enum Keytype type_par, const LEX_STRING &name_arg,
 
165
      KEY_CREATE_INFO *key_info_arg,
 
166
      bool generated_arg, List<Key_part_spec> &cols)
 
167
    :type(type_par), key_create_info(*key_info_arg), columns(cols),
 
168
    name(name_arg), generated(generated_arg)
 
169
  {}
 
170
  Key(enum Keytype type_par, const char *name_arg, size_t name_len_arg,
 
171
      KEY_CREATE_INFO *key_info_arg, bool generated_arg,
 
172
      List<Key_part_spec> &cols)
 
173
    :type(type_par), key_create_info(*key_info_arg), columns(cols),
 
174
    generated(generated_arg)
 
175
  {
 
176
    name.str= (char *)name_arg;
 
177
    name.length= name_len_arg;
 
178
  }
 
179
  Key(const Key &rhs, MEM_ROOT *mem_root);
 
180
  virtual ~Key() {}
 
181
  /* Equality comparison of keys (ignoring name) */
 
182
  friend bool foreign_key_prefix(Key *a, Key *b);
 
183
  /**
 
184
    Used to make a clone of this object for ALTER/CREATE TABLE
 
185
    @sa comment for Key_part_spec::clone
 
186
  */
 
187
  virtual Key *clone(MEM_ROOT *mem_root) const
 
188
    { return new (mem_root) Key(*this, mem_root); }
 
189
};
 
190
 
 
191
class Table_ident;
 
192
 
 
193
class Foreign_key: public Key {
 
194
public:
 
195
  enum fk_match_opt { FK_MATCH_UNDEF, FK_MATCH_FULL,
 
196
                      FK_MATCH_PARTIAL, FK_MATCH_SIMPLE};
 
197
  enum fk_option { FK_OPTION_UNDEF, FK_OPTION_RESTRICT, FK_OPTION_CASCADE,
 
198
                   FK_OPTION_SET_NULL, FK_OPTION_NO_ACTION, FK_OPTION_DEFAULT};
 
199
 
 
200
  Table_ident *ref_table;
 
201
  List<Key_part_spec> ref_columns;
 
202
  uint delete_opt, update_opt, match_opt;
 
203
  Foreign_key(const LEX_STRING &name_arg, List<Key_part_spec> &cols,
 
204
              Table_ident *table,   List<Key_part_spec> &ref_cols,
 
205
              uint delete_opt_arg, uint update_opt_arg, uint match_opt_arg)
 
206
    :Key(FOREIGN_KEY, name_arg, &default_key_create_info, 0, cols),
 
207
    ref_table(table), ref_columns(ref_cols),
 
208
    delete_opt(delete_opt_arg), update_opt(update_opt_arg),
 
209
    match_opt(match_opt_arg)
 
210
  {}
 
211
  Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root);
 
212
  /**
 
213
    Used to make a clone of this object for ALTER/CREATE TABLE
 
214
    @sa comment for Key_part_spec::clone
 
215
  */
 
216
  virtual Key *clone(MEM_ROOT *mem_root) const
 
217
  { return new (mem_root) Foreign_key(*this, mem_root); }
 
218
};
105
219
 
106
220
typedef struct st_mysql_lock
107
221
{
108
 
  Table **table;
109
 
  uint32_t table_count,lock_count;
 
222
  TABLE **table;
 
223
  uint table_count,lock_count;
110
224
  THR_LOCK_DATA **locks;
111
 
} DRIZZLE_LOCK;
 
225
} MYSQL_LOCK;
112
226
 
113
227
 
114
228
class LEX_COLUMN : public Sql_alloc
115
229
{
116
230
public:
117
231
  String column;
118
 
  uint32_t rights;
 
232
  uint rights;
119
233
  LEX_COLUMN (const String& x,const  uint& y ): column (x),rights (y) {}
120
234
};
121
235
 
 
236
#include "sql_lex.h"                            /* Must be here */
 
237
 
122
238
class select_result;
123
239
class Time_zone;
124
240
 
125
 
#define Session_SENTRY_MAGIC 0xfeedd1ff
126
 
#define Session_SENTRY_GONE  0xdeadbeef
 
241
#define THD_SENTRY_MAGIC 0xfeedd1ff
 
242
#define THD_SENTRY_GONE  0xdeadbeef
127
243
 
128
 
#define Session_CHECK_SENTRY(session) assert(session->dbug_sentry == Session_SENTRY_MAGIC)
 
244
#define THD_CHECK_SENTRY(thd) assert(thd->dbug_sentry == THD_SENTRY_MAGIC)
129
245
 
130
246
struct system_variables
131
247
{
140
256
  */ 
141
257
  ulong dynamic_variables_version;
142
258
  char* dynamic_variables_ptr;
143
 
  uint32_t dynamic_variables_head;  /* largest valid variable offset */
144
 
  uint32_t dynamic_variables_size;  /* how many bytes are in use */
 
259
  uint dynamic_variables_head;  /* largest valid variable offset */
 
260
  uint dynamic_variables_size;  /* how many bytes are in use */
145
261
  
146
262
  uint64_t myisam_max_extra_sort_file_size;
147
263
  uint64_t myisam_max_sort_file_size;
148
264
  uint64_t max_heap_table_size;
149
265
  uint64_t tmp_table_size;
 
266
  uint64_t long_query_time;
150
267
  ha_rows select_limit;
151
268
  ha_rows max_join_size;
152
269
  ulong auto_increment_increment, auto_increment_offset;
179
296
  /* A bitmap for switching optimizations on/off */
180
297
  ulong optimizer_switch;
181
298
  ulong preload_buff_size;
 
299
  ulong profiling_history_size;
 
300
  ulong query_cache_type;
182
301
  ulong read_buff_size;
183
302
  ulong read_rnd_buff_size;
184
303
  ulong div_precincrement;
197
316
  ulong trans_prealloc_size;
198
317
  ulong log_warnings;
199
318
  ulong group_concat_max_len;
 
319
  ulong binlog_format; // binlog format for this thd (see enum_binlog_format)
200
320
  /*
201
321
    In slave thread we need to know in behalf of which
202
322
    thread the query is being run to replicate temp tables properly
203
323
  */
204
 
  /* TODO: change this to my_thread_id - but have to fix set_var first */
205
 
  uint64_t pseudo_thread_id;
 
324
  my_thread_id pseudo_thread_id;
206
325
 
207
326
  bool low_priority_updates;
208
327
  bool new_mode;
219
338
  plugin_ref table_plugin;
220
339
 
221
340
  /* Only charset part of these variables is sensible */
222
 
  const CHARSET_INFO  *character_set_filesystem;
223
 
  const CHARSET_INFO  *character_set_client;
224
 
  const CHARSET_INFO  *character_set_results;
 
341
  CHARSET_INFO  *character_set_filesystem;
 
342
  CHARSET_INFO  *character_set_client;
 
343
  CHARSET_INFO  *character_set_results;
225
344
 
226
345
  /* Both charset and collation parts of these variables are important */
227
 
  const CHARSET_INFO    *collation_server;
228
 
  const CHARSET_INFO    *collation_database;
229
 
  const CHARSET_INFO  *collation_connection;
 
346
  CHARSET_INFO  *collation_server;
 
347
  CHARSET_INFO  *collation_database;
 
348
  CHARSET_INFO  *collation_connection;
230
349
 
231
350
  /* Locale Support */
232
351
  MY_LOCALE *lc_time_names;
241
360
 
242
361
};
243
362
 
244
 
extern struct system_variables global_system_variables;
245
 
 
246
 
#include "sql_lex.h"  /* only for SQLCOM_END */
247
363
 
248
364
/* per thread status variables */
249
365
 
326
442
 
327
443
#define last_system_status_var questions
328
444
 
329
 
void mark_transaction_to_rollback(Session *session, bool all);
330
 
 
331
 
#ifdef DRIZZLE_SERVER
 
445
void mark_transaction_to_rollback(THD *thd, bool all);
 
446
 
 
447
#ifdef MYSQL_SERVER
 
448
 
 
449
#define INIT_ARENA_DBUG_INFO is_backup_arena= 0
332
450
 
333
451
class Query_arena
334
452
{
339
457
  */
340
458
  Item *free_list;
341
459
  MEM_ROOT *mem_root;                   // Pointer to current memroot
342
 
 
343
 
  Query_arena(MEM_ROOT *mem_root_arg) :
344
 
    free_list(0), mem_root(mem_root_arg)
345
 
  { }
 
460
  bool is_backup_arena; /* True if this arena is used for backup. */
 
461
 
 
462
  /*
 
463
    The states relfects three diffrent life cycles for three
 
464
    different types of statements:
 
465
    Prepared statement: INITIALIZED -> PREPARED -> EXECUTED.
 
466
    Stored procedure:   INITIALIZED_FOR_SP -> EXECUTED.
 
467
    Other statements:   CONVENTIONAL_EXECUTION never changes.
 
468
  */
 
469
  enum enum_state
 
470
  {
 
471
    INITIALIZED= 0,
 
472
    CONVENTIONAL_EXECUTION= 3, EXECUTED= 4, ERROR= -1
 
473
  };
 
474
 
 
475
  enum_state state;
 
476
 
 
477
  Query_arena(MEM_ROOT *mem_root_arg, enum enum_state state_arg) :
 
478
    free_list(0), mem_root(mem_root_arg), state(state_arg)
 
479
  { INIT_ARENA_DBUG_INFO; }
346
480
  /*
347
481
    This constructor is used only when Query_arena is created as
348
482
    backup storage for another instance of Query_arena.
349
483
  */
350
 
  Query_arena() { }
 
484
  Query_arena() { INIT_ARENA_DBUG_INFO; }
351
485
 
352
486
  virtual ~Query_arena() {};
353
487
 
 
488
  inline bool is_conventional() const
 
489
  { assert(state == CONVENTIONAL_EXECUTION); return state == CONVENTIONAL_EXECUTION; }
 
490
 
354
491
  inline void* alloc(size_t size) { return alloc_root(mem_root,size); }
355
492
  inline void* calloc(size_t size)
356
493
  {
365
502
  { return strmake_root(mem_root,str,size); }
366
503
  inline void *memdup(const void *str, size_t size)
367
504
  { return memdup_root(mem_root,str,size); }
368
 
  inline void *memdup_w_gap(const void *str, size_t size, uint32_t gap)
 
505
  inline void *memdup_w_gap(const void *str, size_t size, uint gap)
369
506
  {
370
507
    void *ptr;
371
508
    if ((ptr= alloc_root(mem_root,size+gap)))
373
510
    return ptr;
374
511
  }
375
512
 
 
513
  void set_query_arena(Query_arena *set);
 
514
 
376
515
  void free_items();
 
516
  /* Close the active state associated with execution of this statement */
 
517
  virtual void cleanup_stmt();
377
518
};
378
519
 
379
520
 
384
525
  One connection can contain a lot of simultaneously running statements,
385
526
  some of which could be:
386
527
   - prepared, that is, contain placeholders,
387
 
  To perform some action with statement we reset Session part to the state  of
388
 
  that statement, do the action, and then save back modified state from Session
 
528
  To perform some action with statement we reset THD part to the state  of
 
529
  that statement, do the action, and then save back modified state from THD
389
530
  to the statement. It will be changed in near future, and Statement will
390
531
  be used explicitly.
391
532
*/
406
547
                        handler of fields used is set
407
548
    MARK_COLUMNS_READ:  Means a bit in read set is set to inform handler
408
549
                        that the field is to be read. If field list contains
409
 
                        duplicates, then session->dup_field is set to point
 
550
                        duplicates, then thd->dup_field is set to point
410
551
                        to the last found duplicate.
411
552
    MARK_COLUMNS_WRITE: Means a bit is set in write set to inform handler
412
553
                        that it needs to update this field in write_row
414
555
  */
415
556
  enum enum_mark_columns mark_used_columns;
416
557
 
 
558
  LEX_STRING name; /* name for named prepared statements */
417
559
  LEX *lex;                                     // parse tree descriptor
418
560
  /*
419
561
    Points to the query associated with this statement. It's const, but
425
567
    LOCK_thread_count mutex. And (B) we are ONLY allowed to set query to a
426
568
    non-NULL value if its previous value is NULL. We do not need to protect
427
569
    operation (B) with any mutex. To avoid crashes in races, if we do not
428
 
    know that session->query cannot change at the moment, one should print
429
 
    session->query like this:
 
570
    know that thd->query cannot change at the moment, one should print
 
571
    thd->query like this:
430
572
      (1) reserve the LOCK_thread_count mutex;
431
 
      (2) check if session->query is NULL;
432
 
      (3) if not NULL, then print at most session->query_length characters from
 
573
      (2) check if thd->query is NULL;
 
574
      (3) if not NULL, then print at most thd->query_length characters from
433
575
      it. We will see the query_length field as either 0, or the right value
434
576
      for it.
435
577
    Assuming that the write and read of an n-bit memory field in an n-bit
449
591
    valid database name.
450
592
 
451
593
    @note this attribute is set and alloced by the slave SQL thread (for
452
 
    the Session of that thread); that thread is (and must remain, for now) the
 
594
    the THD of that thread); that thread is (and must remain, for now) the
453
595
    only responsible for freeing this member.
454
596
  */
455
597
 
456
598
  char *db;
457
 
  uint32_t db_length;
 
599
  uint db_length;
458
600
 
459
601
public:
460
602
 
461
603
  /* This constructor is called for backup statements */
462
604
  Statement() {}
463
605
 
464
 
  Statement(LEX *lex_arg, MEM_ROOT *mem_root_arg, ulong id_arg);
 
606
  Statement(LEX *lex_arg, MEM_ROOT *mem_root_arg,
 
607
            enum enum_state state_arg, ulong id_arg);
465
608
  ~Statement() {}
 
609
 
 
610
  /* Assign execution context (note: not all members) of given stmt to self */
 
611
  void set_statement(Statement *stmt);
 
612
  void set_n_backup_statement(Statement *stmt, Statement *backup);
 
613
  void restore_backup_statement(Statement *stmt, Statement *backup);
466
614
};
467
615
 
468
616
struct st_savepoint {
469
617
  struct st_savepoint *prev;
470
618
  char                *name;
471
 
  uint32_t                 length;
 
619
  uint                 length;
472
620
  Ha_trx_info         *ha_list;
473
621
};
474
622
 
479
627
  /* For now, this is only used to catch duplicated external xids */
480
628
  XID  xid;                           // transaction identifier
481
629
  enum xa_states xa_state;            // used by external XA only
482
 
  bool in_session;
 
630
  bool in_thd;
483
631
} XID_STATE;
484
632
 
485
633
extern pthread_mutex_t LOCK_xid_cache;
493
641
 
494
642
/**
495
643
  @class Security_context
496
 
  @brief A set of Session members describing the current authenticated user.
 
644
  @brief A set of THD members describing the current authenticated user.
497
645
*/
498
646
 
499
647
class Security_context {
506
654
    priv_user - The user privilege we are using. May be "" for anonymous user.
507
655
    ip - client IP
508
656
  */
509
 
  char *user; 
510
 
  char *ip;
 
657
  char   *host, *user, *priv_user, *ip;
 
658
  /* The host privilege we are using */
 
659
  char   priv_host[MAX_HOSTNAME];
 
660
  /* points to host if host is available, otherwise points to ip */
 
661
  const char *host_or_ip;
 
662
  ulong db_access;                     /* Privileges for current db */
511
663
 
512
664
  void init();
513
665
  void destroy();
514
666
  void skip_grants();
515
667
  inline char *priv_host_name()
516
668
  {
517
 
    return (ip ? ip : (char *)"%");
 
669
    return (*priv_host ? priv_host : (char *)"%");
518
670
  }
519
671
};
520
672
 
543
695
    List of regular tables in use by this thread. Contains temporary and
544
696
    base tables that were opened with @see open_tables().
545
697
  */
546
 
  Table *open_tables;
 
698
  TABLE *open_tables;
547
699
  /**
548
700
    List of temporary tables used by this thread. Contains user-level
549
701
    temporary tables, created with CREATE TEMPORARY TABLE, and
551
703
    or for an intermediate table used in ALTER.
552
704
    XXX Why are internal temporary tables added to this list?
553
705
  */
554
 
  Table *temporary_tables;
 
706
  TABLE *temporary_tables;
555
707
  /**
556
708
    List of tables that were opened with HANDLER OPEN and are
557
709
    still in use by this thread.
558
710
  */
559
 
  Table *handler_tables;
560
 
  Table *derived_tables;
 
711
  TABLE *handler_tables;
 
712
  TABLE *derived_tables;
561
713
  /*
562
714
    During a MySQL session, one can lock tables in two modes: automatic
563
715
    or manual. In automatic mode all necessary tables are locked just before
571
723
    the 'LOCK_TABLES' chapter of the MySQL manual.
572
724
    See also lock_tables() for details.
573
725
  */
574
 
  DRIZZLE_LOCK *lock;
 
726
  MYSQL_LOCK *lock;
575
727
  /*
576
728
    Tables that were locked with explicit or implicit LOCK TABLES.
577
729
    (Implicit LOCK TABLES happens when we are prelocking tables for
578
730
     execution of statement which uses stored routines. See description
579
 
     Session::prelocked_mode for more info.)
 
731
     THD::prelocked_mode for more info.)
580
732
  */
581
 
  DRIZZLE_LOCK *locked_tables;
 
733
  MYSQL_LOCK *locked_tables;
582
734
 
583
735
  /*
584
736
    CREATE-SELECT keeps an extra lock for the table being
585
737
    created. This field is used to keep the extra lock available for
586
738
    lower level routines, which would otherwise miss that lock.
587
739
   */
588
 
  DRIZZLE_LOCK *extra_lock;
 
740
  MYSQL_LOCK *extra_lock;
589
741
 
590
742
  ulong version;
591
 
  uint32_t current_tablenr;
 
743
  uint current_tablenr;
592
744
 
593
745
  enum enum_flags {
594
746
    BACKUPS_AVAIL = (1U << 0)     /* There are backups available */
597
749
  /*
598
750
    Flags with information about the open tables state.
599
751
  */
600
 
  uint32_t state_flags;
 
752
  uint state_flags;
601
753
 
602
754
  /*
603
755
    This constructor serves for creation of Open_tables_state instances
620
772
  }
621
773
};
622
774
 
623
 
 
624
 
/* Flags for the Session::system_thread variable */
 
775
/**
 
776
  @class Sub_statement_state
 
777
  @brief Used to save context when executing a function or trigger
 
778
*/
 
779
 
 
780
/* Defines used for Sub_statement_state::in_sub_stmt */
 
781
 
 
782
#define SUB_STMT_TRIGGER 1
 
783
#define SUB_STMT_FUNCTION 2
 
784
 
 
785
 
 
786
class Sub_statement_state
 
787
{
 
788
public:
 
789
  uint64_t options;
 
790
  uint64_t first_successful_insert_id_in_prev_stmt;
 
791
  uint64_t first_successful_insert_id_in_cur_stmt, insert_id_for_cur_row;
 
792
  Discrete_interval auto_inc_interval_for_cur_row;
 
793
  Discrete_intervals_list auto_inc_intervals_forced;
 
794
  uint64_t limit_found_rows;
 
795
  ha_rows    cuted_fields, sent_row_count, examined_row_count;
 
796
  ulong client_capabilities;
 
797
  uint in_sub_stmt;
 
798
  bool enable_slow_log;
 
799
  bool last_insert_id_used;
 
800
  SAVEPOINT *savepoints;
 
801
};
 
802
 
 
803
 
 
804
/* Flags for the THD::system_thread variable */
625
805
enum enum_thread_type
626
806
{
627
807
  NON_SYSTEM_THREAD,
655
835
    from the anticipated conditions trapped during runtime.
656
836
 
657
837
    This mechanism is similar to C++ try/throw/catch:
658
 
    - 'try' correspond to <code>Session::push_internal_handler()</code>,
 
838
    - 'try' correspond to <code>THD::push_internal_handler()</code>,
659
839
    - 'throw' correspond to <code>my_error()</code>,
660
840
    which invokes <code>my_message_sql()</code>,
661
841
    - 'catch' correspond to checking how/if an internal handler was invoked,
662
842
    before removing it from the exception stack with
663
 
    <code>Session::pop_internal_handler()</code>.
 
843
    <code>THD::pop_internal_handler()</code>.
664
844
 
665
845
    @param sql_errno the error number
666
846
    @param level the error level
667
 
    @param session the calling thread
 
847
    @param thd the calling thread
668
848
    @return true if the error is handled
669
849
  */
670
 
  virtual bool handle_error(uint32_t sql_errno,
 
850
  virtual bool handle_error(uint sql_errno,
671
851
                            const char *message,
672
 
                            DRIZZLE_ERROR::enum_warning_level level,
673
 
                            Session *session) = 0;
 
852
                            MYSQL_ERROR::enum_warning_level level,
 
853
                            THD *thd) = 0;
674
854
};
675
855
 
676
856
 
702
882
  /** Set to make set_error_status after set_{ok,eof}_status possible. */
703
883
  bool can_overwrite_status;
704
884
 
705
 
  void set_ok_status(Session *session, ha_rows affected_rows_arg,
 
885
  void set_ok_status(THD *thd, ha_rows affected_rows_arg,
706
886
                     uint64_t last_insert_id_arg,
707
887
                     const char *message);
708
 
  void set_eof_status(Session *session);
709
 
  void set_error_status(Session *session, uint32_t sql_errno_arg, const char *message_arg);
 
888
  void set_eof_status(THD *thd);
 
889
  void set_error_status(THD *thd, uint sql_errno_arg, const char *message_arg);
710
890
 
711
891
  void disable_status();
712
892
 
722
902
  const char *message() const
723
903
  { assert(m_status == DA_ERROR || m_status == DA_OK); return m_message; }
724
904
 
725
 
  uint32_t sql_errno() const
 
905
  uint sql_errno() const
726
906
  { assert(m_status == DA_ERROR); return m_sql_errno; }
727
907
 
728
 
  uint32_t server_status() const
 
908
  uint server_status() const
729
909
  {
730
910
    assert(m_status == DA_OK || m_status == DA_EOF);
731
911
    return m_server_status;
737
917
  uint64_t last_insert_id() const
738
918
  { assert(m_status == DA_OK); return m_last_insert_id; }
739
919
 
740
 
  uint32_t total_warn_count() const
 
920
  uint total_warn_count() const
741
921
  {
742
922
    assert(m_status == DA_OK || m_status == DA_EOF);
743
923
    return m_total_warn_count;
747
927
 
748
928
private:
749
929
  /** Message buffer. Can be used by OK or ERROR status. */
750
 
  char m_message[DRIZZLE_ERRMSG_SIZE];
 
930
  char m_message[MYSQL_ERRMSG_SIZE];
751
931
  /**
752
932
    SQL error number. One of ER_ codes from share/errmsg.txt.
753
933
    Set by set_error_status.
754
934
  */
755
 
  uint32_t m_sql_errno;
 
935
  uint m_sql_errno;
756
936
 
757
937
  /**
758
 
    Copied from session->server_status when the diagnostics area is assigned.
 
938
    Copied from thd->server_status when the diagnostics area is assigned.
759
939
    We need this member as some places in the code use the following pattern:
760
 
    session->server_status|= ...
761
 
    my_eof(session);
762
 
    session->server_status&= ~...
 
940
    thd->server_status|= ...
 
941
    my_eof(thd);
 
942
    thd->server_status&= ~...
763
943
    Assigned by OK, EOF or ERROR.
764
944
  */
765
 
  uint32_t m_server_status;
 
945
  uint m_server_status;
766
946
  /**
767
947
    The number of rows affected by the last statement. This is
768
 
    semantically close to session->row_count_func, but has a different
769
 
    life cycle. session->row_count_func stores the value returned by
 
948
    semantically close to thd->row_count_func, but has a different
 
949
    life cycle. thd->row_count_func stores the value returned by
770
950
    function ROW_COUNT() and is cleared only by statements that
771
951
    update its value, such as INSERT, UPDATE, DELETE and few others.
772
952
    This member is cleared at the beginning of the next statement.
773
953
 
774
 
    We could possibly merge the two, but life cycle of session->row_count_func
 
954
    We could possibly merge the two, but life cycle of thd->row_count_func
775
955
    can not be changed.
776
956
  */
777
957
  ha_rows    m_affected_rows;
778
958
  /**
779
959
    Similarly to the previous member, this is a replacement of
780
 
    session->first_successful_insert_id_in_prev_stmt, which is used
 
960
    thd->first_successful_insert_id_in_prev_stmt, which is used
781
961
    to implement LAST_INSERT_ID().
782
962
  */
783
963
  uint64_t   m_last_insert_id;
785
965
  uint       m_total_warn_count;
786
966
  enum_diagnostics_status m_status;
787
967
  /**
788
 
    @todo: the following Session members belong here:
 
968
    @todo: the following THD members belong here:
789
969
    - warn_list, warn_count,
790
970
  */
791
971
};
819
999
 
820
1000
 
821
1001
/**
822
 
  @class Session
823
 
  For each client connection we create a separate thread with Session serving as
 
1002
  @class THD
 
1003
  For each client connection we create a separate thread with THD serving as
824
1004
  a thread/connection descriptor
825
1005
*/
826
1006
 
827
 
class Session :public Statement,
 
1007
class THD :public Statement,
828
1008
           public Open_tables_state
829
1009
{
830
1010
public:
832
1012
  Relay_log_info* rli_fake;
833
1013
 
834
1014
  /*
835
 
    Constant for Session::where initialization in the beginning of every query.
 
1015
    Constant for THD::where initialization in the beginning of every query.
836
1016
 
837
 
    It's needed because we do not save/restore Session::where normally during
 
1017
    It's needed because we do not save/restore THD::where normally during
838
1018
    primary (non subselect) query execution.
839
1019
  */
840
1020
  static const char * const DEFAULT_WHERE;
854
1034
  THR_LOCK_OWNER main_lock_id;          // To use for conventional queries
855
1035
  THR_LOCK_OWNER *lock_id;              // If not main_lock_id, points to
856
1036
                                        // the lock_id of a cursor.
857
 
  pthread_mutex_t LOCK_delete;          // Locked before session is deleted
 
1037
  pthread_mutex_t LOCK_delete;          // Locked before thd is deleted
858
1038
  /*
859
1039
    A pointer to the stack frame of handle_one_connection(),
860
1040
    which is called first in the thread for handling a client
868
1048
 
869
1049
  /**
870
1050
    @note
871
 
    Some members of Session (currently 'Statement::db',
 
1051
    Some members of THD (currently 'Statement::db',
872
1052
    'catalog' and 'query')  are set and alloced by the slave SQL thread
873
 
    (for the Session of that thread); that thread is (and must remain, for now)
 
1053
    (for the THD of that thread); that thread is (and must remain, for now)
874
1054
    the only responsible for freeing these 3 members. If you add members
875
1055
    here, and you add code to set them in replication, don't forget to
876
1056
    free_them_and_set_them_to_0 in replication properly. For details see
884
1064
 
885
1065
  /*
886
1066
    Points to info-string that we show in SHOW PROCESSLIST
887
 
    You are supposed to call Session_SET_PROC_INFO only if you have coded
 
1067
    You are supposed to call THD_SET_PROC_INFO only if you have coded
888
1068
    a time-consuming piece that MySQL can get stuck in for a long time.
889
1069
 
890
 
    Set it using the  session_proc_info(Session *thread, const char *message)
 
1070
    Set it using the  thd_proc_info(THD *thread, const char *message)
891
1071
    macro/function.
892
1072
  */
893
 
  void        set_proc_info(const char *info) { proc_info= info; }
894
 
  const char* get_proc_info() const { return proc_info; }
 
1073
#define THD_SET_PROC_INFO(thd, info) \
 
1074
    (thd)->proc_info= (info)
 
1075
 
 
1076
  inline const char* get_proc_info() { return proc_info;}
 
1077
 
 
1078
  /* left public for the the storage engines, please avoid direct use */
 
1079
  const char *proc_info;
895
1080
 
896
1081
  /*
897
1082
    Used in error messages to tell user in what part of MySQL we found an
910
1095
    points to a lock object if the lock is present. See item_func.cc and
911
1096
    chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK. 
912
1097
  */
913
 
  uint32_t dbug_sentry; // watch out for memory corruption
 
1098
  uint dbug_sentry; // watch out for memory corruption
914
1099
  struct st_my_thread_var *mysys_var;
915
1100
  /*
916
1101
    Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. Set from
927
1112
  
928
1113
  thr_lock_type update_lock_default;
929
1114
 
 
1115
  /* <> 0 if we are inside of trigger or stored function. */
 
1116
  uint in_sub_stmt;
 
1117
 
930
1118
  /* container for handler's private per-connection data */
931
1119
  Ha_data ha_data[MAX_HA];
932
1120
 
933
1121
  /* Place to store various things */
934
 
  void *session_marker;
 
1122
  void *thd_marker;
 
1123
#ifndef MYSQL_CLIENT
935
1124
  int binlog_setup_trx_data();
936
1125
 
937
1126
  /*
939
1128
  */
940
1129
  void binlog_start_trans_and_stmt();
941
1130
  void binlog_set_stmt_begin();
942
 
  int binlog_write_table_map(Table *table, bool is_transactional);
943
 
  int binlog_write_row(Table* table, bool is_transactional,
944
 
                       const unsigned char *new_data);
945
 
  int binlog_delete_row(Table* table, bool is_transactional,
946
 
                        const unsigned char *old_data);
947
 
  int binlog_update_row(Table* table, bool is_transactional,
948
 
                        const unsigned char *old_data, const unsigned char *new_data);
 
1131
  int binlog_write_table_map(TABLE *table, bool is_transactional);
 
1132
  int binlog_write_row(TABLE* table, bool is_transactional,
 
1133
                       const uchar *new_data);
 
1134
  int binlog_delete_row(TABLE* table, bool is_transactional,
 
1135
                        const uchar *old_data);
 
1136
  int binlog_update_row(TABLE* table, bool is_transactional,
 
1137
                        const uchar *old_data, const uchar *new_data);
949
1138
 
950
1139
  void set_server_id(uint32_t sid) { server_id = sid; }
951
1140
 
953
1142
    Member functions to handle pending event for row-level logging.
954
1143
  */
955
1144
  template <class RowsEventT> Rows_log_event*
956
 
    binlog_prepare_pending_rows_event(Table* table, uint32_t serv_id,
 
1145
    binlog_prepare_pending_rows_event(TABLE* table, uint32_t serv_id,
957
1146
                                      size_t needed,
958
1147
                                      bool is_transactional,
959
1148
                                      RowsEventT* hint);
962
1151
  int binlog_flush_pending_rows_event(bool stmt_end);
963
1152
 
964
1153
private:
965
 
  uint32_t binlog_table_maps; // Number of table maps currently in the binlog
 
1154
  uint binlog_table_maps; // Number of table maps currently in the binlog
 
1155
 
 
1156
  enum enum_binlog_flag {
 
1157
    BINLOG_FLAG_UNSAFE_STMT_PRINTED,
 
1158
    BINLOG_FLAG_COUNT
 
1159
  };
966
1160
 
967
1161
  /**
968
1162
     Flags with per-thread information regarding the status of the
970
1164
   */
971
1165
  uint32_t binlog_flags;
972
1166
public:
973
 
  uint32_t get_binlog_table_maps() const {
 
1167
  uint get_binlog_table_maps() const {
974
1168
    return binlog_table_maps;
975
1169
  }
976
1170
  void clear_binlog_table_maps() {
977
1171
    binlog_table_maps= 0;
978
1172
  }
 
1173
#endif /* MYSQL_CLIENT */
979
1174
 
980
1175
public:
981
1176
 
982
1177
  struct st_transactions {
983
1178
    SAVEPOINT *savepoints;
984
 
    Session_TRANS all;                  // Trans since BEGIN WORK
985
 
    Session_TRANS stmt;                 // Trans for current statement
 
1179
    THD_TRANS all;                      // Trans since BEGIN WORK
 
1180
    THD_TRANS stmt;                     // Trans for current statement
986
1181
    bool on;                            // see ha_enable_transaction()
987
1182
    XID_STATE xid_state;
988
1183
    Rows_log_event *m_pending_rows_event;
992
1187
       List contain only transactional tables, that not invalidated in query
993
1188
       cache (instead of full list of changed in transaction tables).
994
1189
    */
995
 
    CHANGED_TableList* changed_tables;
 
1190
    CHANGED_TABLE_LIST* changed_tables;
996
1191
    MEM_ROOT mem_root; // Transaction-life memory allocation pool
997
1192
    void cleanup()
998
1193
    {
1002
1197
    }
1003
1198
    st_transactions()
1004
1199
    {
1005
 
      memset(this, 0, sizeof(*this));
 
1200
      memset((char*)this, 0, sizeof(*this));
1006
1201
      xid_state.xid.null();
1007
1202
      init_sql_alloc(&mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
1008
1203
    }
1009
1204
  } transaction;
1010
1205
  Field      *dup_field;
1011
1206
  sigset_t signals;
 
1207
#ifdef SIGNAL_WITH_VIO_CLOSE
 
1208
  Vio* active_vio;
 
1209
#endif
1012
1210
  /*
1013
1211
    This is to track items changed during execution of a prepared
1014
1212
    statement/stored procedure. It's created by
1015
 
    register_item_tree_change() in memory root of Session, and freed in
 
1213
    register_item_tree_change() in memory root of THD, and freed in
1016
1214
    rollback_item_tree_changes(). For conventional execution it's always
1017
1215
    empty.
1018
1216
  */
1019
1217
  Item_change_list change_list;
1020
1218
 
 
1219
  /*
 
1220
    A permanent memory area of the statement. For conventional
 
1221
    execution, the parsed tree and execution runtime reside in the same
 
1222
    memory root. In this case stmt_arena points to THD. In case of
 
1223
    a prepared statement or a stored procedure statement, thd->mem_root
 
1224
    conventionally points to runtime memory, and thd->stmt_arena
 
1225
    points to the memory of the PS/SP, where the parsed tree of the
 
1226
    statement resides. Whenever you need to perform a permanent
 
1227
    transformation of a parsed tree, you should allocate new memory in
 
1228
    stmt_arena, to allow correct re-execution of PS/SP.
 
1229
    Note: in the parser, stmt_arena == thd, even for PS/SP.
 
1230
  */
 
1231
  Query_arena *stmt_arena;
1021
1232
  /* Tells if LAST_INSERT_ID(#) was called for the current statement */
1022
1233
  bool arg_of_last_insert_id_function;
1023
1234
  /*
1034
1245
  */
1035
1246
  uint64_t  first_successful_insert_id_in_prev_stmt;
1036
1247
  /*
 
1248
    Variant of the above, used for storing in statement-based binlog. The
 
1249
    difference is that the one above can change as the execution of a stored
 
1250
    function progresses, while the one below is set once and then does not
 
1251
    change (which is the value which statement-based binlog needs).
 
1252
  */
 
1253
  uint64_t  first_successful_insert_id_in_prev_stmt_for_binlog;
 
1254
  /*
1037
1255
    This is the first autogenerated insert id which was *successfully*
1038
1256
    inserted by the current statement. It is maintained only to set
1039
1257
    first_successful_insert_id_in_prev_stmt when statement ends.
1127
1345
  {
1128
1346
    if (!stmt_depends_on_first_successful_insert_id_in_prev_stmt)
1129
1347
    {
 
1348
      /* It's the first time we read it */
 
1349
      first_successful_insert_id_in_prev_stmt_for_binlog=
 
1350
        first_successful_insert_id_in_prev_stmt;
1130
1351
      stmt_depends_on_first_successful_insert_id_in_prev_stmt= 1;
1131
1352
    }
1132
1353
    return first_successful_insert_id_in_prev_stmt;
1161
1382
  /*
1162
1383
    The set of those tables whose fields are referenced in all subqueries
1163
1384
    of the query.
1164
 
    TODO: possibly this it is incorrect to have used tables in Session because
 
1385
    TODO: possibly this it is incorrect to have used tables in THD because
1165
1386
    with more than one subquery, it is not clear what does the field mean.
1166
1387
  */
1167
1388
  table_map  used_tables;
1168
1389
  USER_CONN *user_connect;
1169
 
  const CHARSET_INFO *db_charset;
 
1390
  CHARSET_INFO *db_charset;
1170
1391
  /*
1171
1392
    FIXME: this, and some other variables like 'count_cuted_fields'
1172
1393
    maybe should be statement/cursor local, that is, moved to Statement
1173
1394
    class. With current implementation warnings produced in each prepared
1174
1395
    statement/cursor settle here.
1175
1396
  */
1176
 
  List       <DRIZZLE_ERROR> warn_list;
1177
 
  uint       warn_count[(uint) DRIZZLE_ERROR::WARN_LEVEL_END];
 
1397
  List       <MYSQL_ERROR> warn_list;
 
1398
  uint       warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_END];
1178
1399
  uint       total_warn_count;
1179
1400
  Diagnostics_area main_da;
1180
1401
 
1205
1426
  uint       tmp_table, global_read_lock;
1206
1427
  uint       server_status,open_options;
1207
1428
  enum enum_thread_type system_thread;
1208
 
  uint32_t       select_number;             //number of select (used for EXPLAIN)
 
1429
  uint       select_number;             //number of select (used for EXPLAIN)
1209
1430
  /* variables.transaction_isolation is reset to this after each commit */
1210
1431
  enum_tx_isolation session_tx_isolation;
1211
1432
  enum_check_fields count_cuted_fields;
1215
1436
 
1216
1437
  enum killed_state
1217
1438
  {
1218
 
    NOT_KILLED,
1219
 
    KILL_BAD_DATA,
1220
 
    KILL_CONNECTION,
1221
 
    KILL_QUERY,
 
1439
    NOT_KILLED=0,
 
1440
    KILL_BAD_DATA=1,
 
1441
    KILL_CONNECTION=ER_SERVER_SHUTDOWN,
 
1442
    KILL_QUERY=ER_QUERY_INTERRUPTED,
1222
1443
    KILLED_NO_VALUE      /* means neither of the states */
1223
1444
  };
1224
1445
  killed_state volatile killed;
1226
1447
  /* scramble - random string sent to client on handshake */
1227
1448
  char       scramble[SCRAMBLE_LENGTH+1];
1228
1449
 
1229
 
  bool       slave_thread;
 
1450
  bool       slave_thread, one_shot_set;
 
1451
  /* tells if current statement should binlog row-based(1) or stmt-based(0) */
 
1452
  bool       current_stmt_binlog_row_based;
1230
1453
  bool       some_tables_deleted;
1231
1454
  bool       last_cuted_field;
1232
1455
  bool       no_errors, password;
1235
1458
    can not continue. In particular, disables activation of
1236
1459
    CONTINUE or EXIT handlers of stored routines.
1237
1460
    Reset in the end of processing of the current user request, in
1238
 
    @see mysql_reset_session_for_next_command().
 
1461
    @see mysql_reset_thd_for_next_command().
1239
1462
  */
1240
1463
  bool is_fatal_error;
1241
1464
  /**
1256
1479
    Reset to false when we leave the sub-statement mode.
1257
1480
  */
1258
1481
  bool       is_fatal_sub_stmt_error;
1259
 
  bool       query_start_used; 
 
1482
  bool       query_start_used, rand_used, time_zone_used;
1260
1483
  /* for IS NULL => = last_insert_id() fix in remove_eq_conds() */
1261
1484
  bool       substitute_null_with_insert_id;
1262
1485
  bool       in_lock_tables;
1267
1490
    it returned an error on master, and this is OK on the slave.
1268
1491
  */
1269
1492
  bool       is_slave_error;
1270
 
  bool       cleanup_done;
 
1493
  bool       bootstrap, cleanup_done;
1271
1494
  
1272
1495
  /**  is set if some thread specific value(s) used in a statement. */
1273
1496
  bool       thread_specific_used;
1274
1497
  bool       charset_is_system_charset, charset_is_collation_connection;
1275
1498
  bool       charset_is_character_set_filesystem;
 
1499
  bool       enable_slow_log;   /* enable slow log for current statement */
1276
1500
  bool       abort_on_warning;
1277
1501
  bool       got_warning;       /* Set on call to push_warning() */
1278
1502
  bool       no_warnings_for_error; /* no warnings on call to my_error() */
1298
1522
  
1299
1523
  struct {
1300
1524
    /* 
1301
 
      If true, drizzle_bin_log::write(Log_event) call will not write events to 
 
1525
      If true, mysql_bin_log::write(Log_event) call will not write events to 
1302
1526
      binlog, and maintain 2 below variables instead (use
1303
 
      drizzle_bin_log.start_union_events to turn this on)
 
1527
      mysql_bin_log.start_union_events to turn this on)
1304
1528
    */
1305
1529
    bool do_union;
1306
1530
    /*
1307
 
      If true, at least one drizzle_bin_log::write(Log_event) call has been
1308
 
      made after last drizzle_bin_log.start_union_events() call.
 
1531
      If true, at least one mysql_bin_log::write(Log_event) call has been
 
1532
      made after last mysql_bin_log.start_union_events() call.
1309
1533
    */
1310
1534
    bool unioned_events;
1311
1535
    /*
1312
 
      If true, at least one drizzle_bin_log::write(Log_event e), where 
 
1536
      If true, at least one mysql_bin_log::write(Log_event e), where 
1313
1537
      e.cache_stmt == true call has been made after last 
1314
 
      drizzle_bin_log.start_union_events() call.
 
1538
      mysql_bin_log.start_union_events() call.
1315
1539
    */
1316
1540
    bool unioned_events_trans;
1317
1541
    
1318
1542
    /* 
1319
1543
      'queries' (actually SP statements) that run under inside this binlog
1320
 
      union have session->query_id >= first_query_id.
 
1544
      union have thd->query_id >= first_query_id.
1321
1545
    */
1322
1546
    query_id_t first_query_id;
1323
1547
  } binlog_evt_union;
1331
1555
  */
1332
1556
  Lex_input_stream *m_lip;
1333
1557
 
1334
 
  Session();
1335
 
  ~Session();
 
1558
  THD();
 
1559
  ~THD();
1336
1560
 
1337
1561
  void init(void);
1338
1562
  /*
1339
1563
    Initialize memory roots necessary for query processing and (!)
1340
 
    pre-allocate memory for it. We can't do that in Session constructor because
 
1564
    pre-allocate memory for it. We can't do that in THD constructor because
1341
1565
    there are use cases (acl_init, watcher threads,
1342
1566
    killing mysqld) where it's vital to not allocate excessive and not used
1343
1567
    memory. Note, that we still don't return error from init_for_queries():
1349
1573
  void cleanup(void);
1350
1574
  void cleanup_after_query();
1351
1575
  bool store_globals();
1352
 
  void awake(Session::killed_state state_to_set);
 
1576
#ifdef SIGNAL_WITH_VIO_CLOSE
 
1577
  inline void set_active_vio(Vio* vio)
 
1578
  {
 
1579
    pthread_mutex_lock(&LOCK_delete);
 
1580
    active_vio = vio;
 
1581
    pthread_mutex_unlock(&LOCK_delete);
 
1582
  }
 
1583
  inline void clear_active_vio()
 
1584
  {
 
1585
    pthread_mutex_lock(&LOCK_delete);
 
1586
    active_vio = 0;
 
1587
    pthread_mutex_unlock(&LOCK_delete);
 
1588
  }
 
1589
  void close_active_vio();
 
1590
#endif
 
1591
  void awake(THD::killed_state state_to_set);
1353
1592
 
 
1593
#ifndef MYSQL_CLIENT
1354
1594
  enum enum_binlog_query_type {
1355
1595
    /*
1356
1596
      The query can be logged row-based or statement-based
1366
1606
      The query represents a change to a table in the "mysql"
1367
1607
      database and is currently mapped to ROW_QUERY_TYPE.
1368
1608
    */
1369
 
    DRIZZLE_QUERY_TYPE,
 
1609
    MYSQL_QUERY_TYPE,
1370
1610
    QUERY_TYPE_COUNT
1371
1611
  };
1372
1612
  
1373
1613
  int binlog_query(enum_binlog_query_type qtype,
1374
1614
                   char const *query, ulong query_len,
1375
1615
                   bool is_trans, bool suppress_use,
1376
 
                   Session::killed_state killed_err_arg= Session::KILLED_NO_VALUE);
 
1616
                   THD::killed_state killed_err_arg= THD::KILLED_NO_VALUE);
 
1617
#endif
1377
1618
 
1378
1619
  /*
1379
1620
    For enter_cond() / exit_cond() to work the mutex must be got before
1387
1628
    safe_mutex_assert_owner(mutex);
1388
1629
    mysys_var->current_mutex = mutex;
1389
1630
    mysys_var->current_cond = cond;
1390
 
    this->set_proc_info(msg);
 
1631
    thd_proc_info(this, msg);
1391
1632
    return old_msg;
1392
1633
  }
1393
1634
  inline void exit_cond(const char* old_msg)
1396
1637
      Putting the mutex unlock in exit_cond() ensures that
1397
1638
      mysys_var->current_mutex is always unlocked _before_ mysys_var->mutex is
1398
1639
      locked (if that would not be the case, you'll get a deadlock if someone
1399
 
      does a Session::awake() on you).
 
1640
      does a THD::awake() on you).
1400
1641
    */
1401
1642
    pthread_mutex_unlock(mysys_var->current_mutex);
1402
1643
    pthread_mutex_lock(&mysys_var->mutex);
1403
1644
    mysys_var->current_mutex = 0;
1404
1645
    mysys_var->current_cond = 0;
1405
 
    this->set_proc_info(old_msg);
 
1646
    thd_proc_info(this, old_msg);
1406
1647
    pthread_mutex_unlock(&mysys_var->mutex);
1407
1648
  }
1408
1649
  inline time_t query_start() { query_start_used=1; return start_time; }
1442
1683
  }
1443
1684
 
1444
1685
  LEX_STRING *make_lex_string(LEX_STRING *lex_str,
1445
 
                              const char* str, uint32_t length,
 
1686
                              const char* str, uint length,
1446
1687
                              bool allocate_lex_string);
1447
1688
 
1448
 
  bool convert_string(LEX_STRING *to, const CHARSET_INFO * const to_cs,
1449
 
                      const char *from, uint32_t from_length,
1450
 
                      const CHARSET_INFO * const from_cs);
1451
 
 
1452
 
  bool convert_string(String *s, const CHARSET_INFO * const from_cs, const CHARSET_INFO * const to_cs);
1453
 
 
1454
 
  void add_changed_table(Table *table);
 
1689
  bool convert_string(LEX_STRING *to, CHARSET_INFO *to_cs,
 
1690
                      const char *from, uint from_length,
 
1691
                      CHARSET_INFO *from_cs);
 
1692
 
 
1693
  bool convert_string(String *s, CHARSET_INFO *from_cs, CHARSET_INFO *to_cs);
 
1694
 
 
1695
  void add_changed_table(TABLE *table);
1455
1696
  void add_changed_table(const char *key, long key_length);
1456
 
  CHANGED_TableList * changed_table_dup(const char *key, long key_length);
 
1697
  CHANGED_TABLE_LIST * changed_table_dup(const char *key, long key_length);
1457
1698
  int send_explain_fields(select_result *result);
1458
1699
  /**
1459
1700
    Clear the current error, if any.
1470
1711
    return;
1471
1712
  }
1472
1713
  inline bool vio_ok() const { return net.vio != 0; }
1473
 
 
 
1714
  /** Return false if connection to client is broken. */
 
1715
  bool vio_is_connected();
1474
1716
  /**
1475
1717
    Mark the current error as fatal. Warning: this does not
1476
1718
    set any error, it sets a property of the error, so must be
1495
1737
    To raise this flag, use my_error().
1496
1738
  */
1497
1739
  inline bool is_error() const { return main_da.is_error(); }
1498
 
  inline const CHARSET_INFO *charset() { return variables.character_set_client; }
 
1740
  inline CHARSET_INFO *charset() { return variables.character_set_client; }
1499
1741
  void update_charset();
1500
1742
 
1501
1743
  void change_item_tree(Item **place, Item *new_value)
1502
1744
  {
 
1745
    /* TODO: check for OOM condition here */
 
1746
    if (!stmt_arena->is_conventional())
 
1747
      nocheck_register_item_tree_change(place, *place, mem_root);
1503
1748
    *place= new_value;
1504
1749
  }
1505
1750
  void nocheck_register_item_tree_change(Item **place, Item *old_value,
1525
1770
  void set_status_var_init();
1526
1771
  void reset_n_backup_open_tables_state(Open_tables_state *backup);
1527
1772
  void restore_backup_open_tables_state(Open_tables_state *backup);
 
1773
  void restore_sub_statement_state(Sub_statement_state *backup);
 
1774
  void set_n_backup_active_arena(Query_arena *set, Query_arena *backup);
 
1775
  void restore_active_arena(Query_arena *set, Query_arena *backup);
 
1776
 
 
1777
  inline void set_current_stmt_binlog_row_based_if_mixed()
 
1778
  {
 
1779
    /*
 
1780
      If in a stored/function trigger, the caller should already have done the
 
1781
      change. We test in_sub_stmt to prevent introducing bugs where people
 
1782
      wouldn't ensure that, and would switch to row-based mode in the middle
 
1783
      of executing a stored function/trigger (which is too late, see also
 
1784
      reset_current_stmt_binlog_row_based()); this condition will make their
 
1785
      tests fail and so force them to propagate the
 
1786
      lex->binlog_row_based_if_mixed upwards to the caller.
 
1787
    */
 
1788
    if ((variables.binlog_format == BINLOG_FORMAT_MIXED) &&
 
1789
        (in_sub_stmt == 0))
 
1790
      current_stmt_binlog_row_based= true;
 
1791
  }
 
1792
  inline void set_current_stmt_binlog_row_based()
 
1793
  {
 
1794
    current_stmt_binlog_row_based= true;
 
1795
  }
 
1796
  inline void clear_current_stmt_binlog_row_based()
 
1797
  {
 
1798
    current_stmt_binlog_row_based= false;
 
1799
  }
 
1800
  inline void reset_current_stmt_binlog_row_based()
 
1801
  {
 
1802
    /*
 
1803
      If there are temporary tables, don't reset back to
 
1804
      statement-based. Indeed it could be that:
 
1805
      CREATE TEMPORARY TABLE t SELECT UUID(); # row-based
 
1806
      # and row-based does not store updates to temp tables
 
1807
      # in the binlog.
 
1808
      INSERT INTO u SELECT * FROM t; # stmt-based
 
1809
      and then the INSERT will fail as data inserted into t was not logged.
 
1810
      So we continue with row-based until the temp table is dropped.
 
1811
      If we are in a stored function or trigger, we mustn't reset in the
 
1812
      middle of its execution (as the binary logging way of a stored function
 
1813
      or trigger is decided when it starts executing, depending for example on
 
1814
      the caller (for a stored function: if caller is SELECT or
 
1815
      INSERT/UPDATE/DELETE...).
 
1816
 
 
1817
      Don't reset binlog format for NDB binlog injector thread.
 
1818
    */
 
1819
    if ((temporary_tables == NULL) && (in_sub_stmt == 0))
 
1820
    {
 
1821
      current_stmt_binlog_row_based= 
 
1822
        test(variables.binlog_format == BINLOG_FORMAT_ROW);
 
1823
    }
 
1824
  }
1528
1825
 
1529
1826
  /**
1530
1827
    Set the current database; use deep copy of C-string.
1554
1851
      memcpy(db, new_db, new_db_len+1);
1555
1852
    else
1556
1853
    {
1557
 
      if (db)
1558
 
        free(db);
 
1854
      x_free(db);
1559
1855
      if (new_db)
1560
1856
        db= my_strndup(new_db, new_db_len, MYF(MY_WME | ME_FATALERROR));
1561
1857
      else
1587
1883
    a statement is parsed but before it's executed.
1588
1884
  */
1589
1885
  bool copy_db_to(char **p_db, size_t *p_db_length);
1590
 
  session_scheduler scheduler;
 
1886
  thd_scheduler scheduler;
1591
1887
 
1592
1888
public:
1593
1889
  /**
1602
1898
    @param level the error level
1603
1899
    @return true if the error is handled
1604
1900
  */
1605
 
  virtual bool handle_error(uint32_t sql_errno, const char *message,
1606
 
                            DRIZZLE_ERROR::enum_warning_level level);
 
1901
  virtual bool handle_error(uint sql_errno, const char *message,
 
1902
                            MYSQL_ERROR::enum_warning_level level);
1607
1903
 
1608
1904
  /**
1609
1905
    Remove the error handler last pushed.
1611
1907
  void pop_internal_handler();
1612
1908
 
1613
1909
private:
1614
 
  const char *proc_info;
1615
 
 
1616
1910
  /** The current internal error handler for this thread, or NULL. */
1617
1911
  Internal_error_handler *m_internal_handler;
1618
1912
  /**
1634
1928
};
1635
1929
 
1636
1930
 
1637
 
/** A short cut for session->main_da.set_ok_status(). */
 
1931
/** A short cut for thd->main_da.set_ok_status(). */
1638
1932
 
1639
1933
inline void
1640
 
my_ok(Session *session, ha_rows affected_rows= 0, uint64_t id= 0,
 
1934
my_ok(THD *thd, ha_rows affected_rows= 0, uint64_t id= 0,
1641
1935
        const char *message= NULL)
1642
1936
{
1643
 
  session->main_da.set_ok_status(session, affected_rows, id, message);
 
1937
  thd->main_da.set_ok_status(thd, affected_rows, id, message);
1644
1938
}
1645
1939
 
1646
1940
 
1647
 
/** A short cut for session->main_da.set_eof_status(). */
 
1941
/** A short cut for thd->main_da.set_eof_status(). */
1648
1942
 
1649
1943
inline void
1650
 
my_eof(Session *session)
 
1944
my_eof(THD *thd)
1651
1945
{
1652
 
  session->main_da.set_eof_status(session);
 
1946
  thd->main_da.set_eof_status(thd);
1653
1947
}
1654
1948
 
1655
1949
#define tmp_disable_binlog(A)       \
1674
1968
  bool opt_enclosed;
1675
1969
  bool dumpfile;
1676
1970
  ulong skip_lines;
1677
 
  const CHARSET_INFO *cs;
 
1971
  CHARSET_INFO *cs;
1678
1972
  sql_exchange(char *name, bool dumpfile_flag,
1679
1973
               enum_filetype filetype_arg= FILETYPE_CSV);
1680
1974
};
1689
1983
 
1690
1984
class select_result :public Sql_alloc {
1691
1985
protected:
1692
 
  Session *session;
 
1986
  THD *thd;
1693
1987
  SELECT_LEX_UNIT *unit;
1694
1988
public:
1695
1989
  select_result();
1696
1990
  virtual ~select_result() {};
1697
 
  virtual int prepare(List<Item> &,
 
1991
  virtual int prepare(List<Item> &list __attribute__((unused)),
1698
1992
                      SELECT_LEX_UNIT *u)
1699
1993
  {
1700
1994
    unit= u;
1706
2000
    we need to know number of columns in the result set (if
1707
2001
    there is a result set) apart from sending columns metadata.
1708
2002
  */
1709
 
  virtual uint32_t field_count(List<Item> &fields) const
 
2003
  virtual uint field_count(List<Item> &fields) const
1710
2004
  { return fields.elements; }
1711
 
  virtual bool send_fields(List<Item> &list, uint32_t flags)=0;
 
2005
  virtual bool send_fields(List<Item> &list, uint flags)=0;
1712
2006
  virtual bool send_data(List<Item> &items)=0;
1713
 
  virtual bool initialize_tables (JOIN *)
 
2007
  virtual bool initialize_tables (JOIN  __attribute__((unused)) *join=0)
1714
2008
  { return 0; }
1715
 
  virtual void send_error(uint32_t errcode,const char *err);
 
2009
  virtual void send_error(uint errcode,const char *err);
1716
2010
  virtual bool send_eof()=0;
1717
2011
  /**
1718
2012
    Check if this query returns a result set and therefore is allowed in
1728
2022
    statement/stored procedure.
1729
2023
  */
1730
2024
  virtual void cleanup();
1731
 
  void set_session(Session *session_arg) { session= session_arg; }
 
2025
  void set_thd(THD *thd_arg) { thd= thd_arg; }
1732
2026
  void begin_dataset() {}
1733
2027
};
1734
2028
 
1743
2037
{
1744
2038
public:
1745
2039
  select_result_interceptor() {}              /* Remove gcc warning */
1746
 
  uint32_t field_count(List<Item> &) const
 
2040
  uint field_count(List<Item> &fields __attribute__((unused))) const
1747
2041
  { return 0; }
1748
 
  bool send_fields(List<Item> &,
1749
 
                   uint32_t)
1750
 
  { return false; }
 
2042
  bool send_fields(List<Item> &fields __attribute__((unused)),
 
2043
                   uint flag __attribute__((unused))) { return false; }
1751
2044
};
1752
2045
 
1753
2046
 
1760
2053
  bool is_result_set_started;
1761
2054
public:
1762
2055
  select_send() :is_result_set_started(false) {}
1763
 
  bool send_fields(List<Item> &list, uint32_t flags);
 
2056
  bool send_fields(List<Item> &list, uint flags);
1764
2057
  bool send_data(List<Item> &items);
1765
2058
  bool send_eof();
1766
2059
  virtual bool check_simple_select() const { return false; }
1781
2074
  select_to_file(sql_exchange *ex) :exchange(ex), file(-1),row_count(0L)
1782
2075
  { path[0]=0; }
1783
2076
  ~select_to_file();
1784
 
  void send_error(uint32_t errcode,const char *err);
 
2077
  void send_error(uint errcode,const char *err);
1785
2078
  bool send_eof();
1786
2079
  void cleanup();
1787
2080
};
1797
2090
 
1798
2091
 
1799
2092
class select_export :public select_to_file {
1800
 
  uint32_t field_term_length;
 
2093
  uint field_term_length;
1801
2094
  int field_sep_char,escape_char,line_sep_char;
1802
2095
  int field_term_char; // first char of FIELDS TERMINATED BY or MAX_INT
1803
2096
  /*
1837
2130
 
1838
2131
class select_insert :public select_result_interceptor {
1839
2132
 public:
1840
 
  TableList *table_list;
1841
 
  Table *table;
 
2133
  TABLE_LIST *table_list;
 
2134
  TABLE *table;
1842
2135
  List<Item> *fields;
1843
2136
  uint64_t autoinc_value_of_last_inserted_row; // autogenerated or not
1844
2137
  COPY_INFO info;
1845
2138
  bool insert_into_view;
1846
 
  select_insert(TableList *table_list_par,
1847
 
                Table *table_par, List<Item> *fields_par,
 
2139
  select_insert(TABLE_LIST *table_list_par,
 
2140
                TABLE *table_par, List<Item> *fields_par,
1848
2141
                List<Item> *update_fields, List<Item> *update_values,
1849
2142
                enum_duplicates duplic, bool ignore);
1850
2143
  ~select_insert();
1853
2146
  bool send_data(List<Item> &items);
1854
2147
  virtual void store_values(List<Item> &values);
1855
2148
  virtual bool can_rollback_data() { return 0; }
1856
 
  void send_error(uint32_t errcode,const char *err);
 
2149
  void send_error(uint errcode,const char *err);
1857
2150
  bool send_eof();
1858
2151
  void abort();
1859
2152
  /* not implemented: select_insert is never re-used in prepared statements */
1862
2155
 
1863
2156
 
1864
2157
class select_create: public select_insert {
1865
 
  order_st *group;
1866
 
  TableList *create_table;
 
2158
  ORDER *group;
 
2159
  TABLE_LIST *create_table;
1867
2160
  HA_CREATE_INFO *create_info;
1868
 
  TableList *select_tables;
 
2161
  TABLE_LIST *select_tables;
1869
2162
  Alter_info *alter_info;
1870
2163
  Field **field;
1871
2164
  /* lock data for tmp table */
1872
 
  DRIZZLE_LOCK *m_lock;
1873
 
  /* m_lock or session->extra_lock */
1874
 
  DRIZZLE_LOCK **m_plock;
 
2165
  MYSQL_LOCK *m_lock;
 
2166
  /* m_lock or thd->extra_lock */
 
2167
  MYSQL_LOCK **m_plock;
1875
2168
public:
1876
 
  select_create (TableList *table_arg,
 
2169
  select_create (TABLE_LIST *table_arg,
1877
2170
                 HA_CREATE_INFO *create_info_par,
1878
2171
                 Alter_info *alter_info_arg,
1879
2172
                 List<Item> &select_fields,enum_duplicates duplic, bool ignore,
1880
 
                 TableList *select_tables_arg)
 
2173
                 TABLE_LIST *select_tables_arg)
1881
2174
    :select_insert (NULL, NULL, &select_fields, 0, 0, duplic, ignore),
1882
2175
    create_table(table_arg),
1883
2176
    create_info(create_info_par),
1887
2180
    {}
1888
2181
  int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
1889
2182
 
1890
 
  void binlog_show_create_table(Table **tables, uint32_t count);
 
2183
  void binlog_show_create_table(TABLE **tables, uint count);
1891
2184
  void store_values(List<Item> &values);
1892
 
  void send_error(uint32_t errcode,const char *err);
 
2185
  void send_error(uint errcode,const char *err);
1893
2186
  bool send_eof();
1894
2187
  void abort();
1895
2188
  virtual bool can_rollback_data() { return 1; }
1896
2189
 
1897
 
  // Needed for access from local class MY_HOOKS in prepare(), since session is proteted.
1898
 
  const Session *get_session(void) { return session; }
 
2190
  // Needed for access from local class MY_HOOKS in prepare(), since thd is proteted.
 
2191
  const THD *get_thd(void) { return thd; }
1899
2192
  const HA_CREATE_INFO *get_create_info() { return create_info; };
1900
2193
  int prepare2(void) { return 0; }
1901
2194
};
1920
2213
  List<Item> save_copy_funcs;
1921
2214
  Copy_field *copy_field, *copy_field_end;
1922
2215
  Copy_field *save_copy_field, *save_copy_field_end;
1923
 
  unsigned char     *group_buff;
 
2216
  uchar     *group_buff;
1924
2217
  Item      **items_to_copy;                    /* Fields in tmp table */
1925
2218
  MI_COLUMNDEF *recinfo,*start_recinfo;
1926
2219
  KEY *keyinfo;
1927
2220
  ha_rows end_write_records;
1928
2221
  uint  field_count,sum_func_count,func_count;
1929
 
  uint32_t  hidden_field_count;
 
2222
  uint  hidden_field_count;
1930
2223
  uint  group_parts,group_length,group_null_parts;
1931
2224
  uint  quick_group;
1932
2225
  bool  using_indirect_summary_function;
1933
2226
  /* If >0 convert all blob fields to varchar(convert_blob_length) */
1934
 
  uint32_t  convert_blob_length; 
1935
 
  const CHARSET_INFO *table_charset; 
 
2227
  uint  convert_blob_length; 
 
2228
  CHARSET_INFO *table_charset; 
1936
2229
  bool schema_table;
1937
2230
  /*
1938
2231
    True if GROUP BY and its aggregate functions are already computed
1960
2253
    cleanup();
1961
2254
  }
1962
2255
  void init(void);
1963
 
  void cleanup(void);
 
2256
  inline void cleanup(void)
 
2257
  {
 
2258
    if (copy_field)                             /* Fix for Intel compiler */
 
2259
    {
 
2260
      delete [] copy_field;
 
2261
      save_copy_field= copy_field= 0;
 
2262
    }
 
2263
  }
1964
2264
};
1965
2265
 
1966
2266
class select_union :public select_result_interceptor
1967
2267
{
1968
2268
  TMP_TABLE_PARAM tmp_table_param;
1969
2269
public:
1970
 
  Table *table;
 
2270
  TABLE *table;
1971
2271
 
1972
2272
  select_union() :table(0) {}
1973
2273
  int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
1975
2275
  bool send_eof();
1976
2276
  bool flush();
1977
2277
  void cleanup();
1978
 
  bool create_result_table(Session *session, List<Item> *column_types,
 
2278
  bool create_result_table(THD *thd, List<Item> *column_types,
1979
2279
                           bool is_distinct, uint64_t options,
1980
2280
                           const char *alias, bool bit_fields_as_long);
1981
2281
};
2034
2334
  Field *field;                         /* Field to sort */
2035
2335
  Item  *item;                          /* Item if not sorting fields */
2036
2336
  uint   length;                        /* Length of sort field */
2037
 
  uint32_t   suffix_length;                 /* Length suffix (0-4) */
 
2337
  uint   suffix_length;                 /* Length suffix (0-4) */
2038
2338
  Item_result result_type;              /* Type of item */
2039
2339
  bool reverse;                         /* if descending sort */
2040
2340
  bool need_strxnfrm;                   /* If we have to use strxnfrm() */
2042
2342
 
2043
2343
 
2044
2344
typedef struct st_sort_buffer {
2045
 
  uint32_t index;                                       /* 0 or 1 */
2046
 
  uint32_t sort_orders;
2047
 
  uint32_t change_pos;                          /* If sort-fields changed */
 
2345
  uint index;                                   /* 0 or 1 */
 
2346
  uint sort_orders;
 
2347
  uint change_pos;                              /* If sort-fields changed */
2048
2348
  char **buff;
2049
2349
  SORT_FIELD *sortorder;
2050
2350
} SORT_BUFFER;
2057
2357
  LEX_STRING db;
2058
2358
  LEX_STRING table;
2059
2359
  SELECT_LEX_UNIT *sel;
2060
 
  inline Table_ident(Session *session, LEX_STRING db_arg, LEX_STRING table_arg,
 
2360
  inline Table_ident(THD *thd, LEX_STRING db_arg, LEX_STRING table_arg,
2061
2361
                     bool force)
2062
2362
    :table(table_arg), sel((SELECT_LEX_UNIT *)0)
2063
2363
  {
2064
 
    if (!force && (session->client_capabilities & CLIENT_NO_SCHEMA))
 
2364
    if (!force && (thd->client_capabilities & CLIENT_NO_SCHEMA))
2065
2365
      db.str=0;
2066
2366
    else
2067
2367
      db= db_arg;
2104
2404
  Item_result type;
2105
2405
  bool unsigned_flag;
2106
2406
 
2107
 
  double val_real(bool *null_value);
2108
 
  int64_t val_int(bool *null_value) const;
2109
 
  String *val_str(bool *null_value, String *str, uint32_t decimals);
2110
 
  my_decimal *val_decimal(bool *null_value, my_decimal *result);
 
2407
  double val_real(my_bool *null_value);
 
2408
  int64_t val_int(my_bool *null_value) const;
 
2409
  String *val_str(my_bool *null_value, String *str, uint decimals);
 
2410
  my_decimal *val_decimal(my_bool *null_value, my_decimal *result);
2111
2411
  DTCollation collation;
2112
2412
};
2113
2413
 
2126
2426
  uint64_t max_in_memory_size;
2127
2427
  IO_CACHE file;
2128
2428
  TREE tree;
2129
 
  unsigned char *record_pointers;
 
2429
  uchar *record_pointers;
2130
2430
  bool flush();
2131
 
  uint32_t size;
 
2431
  uint size;
2132
2432
 
2133
2433
public:
2134
2434
  ulong elements;
2135
2435
  Unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
2136
 
         uint32_t size_arg, uint64_t max_in_memory_size_arg);
 
2436
         uint size_arg, uint64_t max_in_memory_size_arg);
2137
2437
  ~Unique();
2138
2438
  ulong elements_in_tree() { return tree.elements_in_tree; }
2139
2439
  inline bool unique_add(void *ptr)
2143
2443
    return(!tree_insert(&tree, ptr, 0, tree.custom_arg));
2144
2444
  }
2145
2445
 
2146
 
  bool get(Table *table);
2147
 
  static double get_use_cost(uint32_t *buffer, uint32_t nkeys, uint32_t key_size, 
 
2446
  bool get(TABLE *table);
 
2447
  static double get_use_cost(uint *buffer, uint nkeys, uint key_size, 
2148
2448
                             uint64_t max_in_memory_size);
2149
 
  inline static int get_cost_calc_buff_size(ulong nkeys, uint32_t key_size, 
 
2449
  inline static int get_cost_calc_buff_size(ulong nkeys, uint key_size, 
2150
2450
                                            uint64_t max_in_memory_size)
2151
2451
  {
2152
2452
    register uint64_t max_elems_in_tree=
2157
2457
  void reset();
2158
2458
  bool walk(tree_walk_action action, void *walk_action_arg);
2159
2459
 
2160
 
  friend int unique_write_to_file(unsigned char* key, element_count count, Unique *unique);
2161
 
  friend int unique_write_to_ptrs(unsigned char* key, element_count count, Unique *unique);
 
2460
  friend int unique_write_to_file(uchar* key, element_count count, Unique *unique);
 
2461
  friend int unique_write_to_ptrs(uchar* key, element_count count, Unique *unique);
2162
2462
};
2163
2463
 
2164
2464
 
2165
2465
class multi_delete :public select_result_interceptor
2166
2466
{
2167
 
  TableList *delete_tables, *table_being_deleted;
 
2467
  TABLE_LIST *delete_tables, *table_being_deleted;
2168
2468
  Unique **tempfiles;
2169
2469
  ha_rows deleted, found;
2170
 
  uint32_t num_of_tables;
 
2470
  uint num_of_tables;
2171
2471
  int error;
2172
2472
  bool do_delete;
2173
2473
  /* True if at least one table we delete from is transactional */
2182
2482
  bool error_handled;
2183
2483
 
2184
2484
public:
2185
 
  multi_delete(TableList *dt, uint32_t num_of_tables);
 
2485
  multi_delete(TABLE_LIST *dt, uint num_of_tables);
2186
2486
  ~multi_delete();
2187
2487
  int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
2188
2488
  bool send_data(List<Item> &items);
2189
2489
  bool initialize_tables (JOIN *join);
2190
 
  void send_error(uint32_t errcode,const char *err);
 
2490
  void send_error(uint errcode,const char *err);
2191
2491
  int  do_deletes();
2192
2492
  bool send_eof();
2193
2493
  virtual void abort();
2196
2496
 
2197
2497
class multi_update :public select_result_interceptor
2198
2498
{
2199
 
  TableList *all_tables; /* query/update command tables */
2200
 
  TableList *leaves;     /* list of leves of join table tree */
2201
 
  TableList *update_tables, *table_being_updated;
2202
 
  Table **tmp_tables, *main_table, *table_to_update;
 
2499
  TABLE_LIST *all_tables; /* query/update command tables */
 
2500
  TABLE_LIST *leaves;     /* list of leves of join table tree */
 
2501
  TABLE_LIST *update_tables, *table_being_updated;
 
2502
  TABLE **tmp_tables, *main_table, *table_to_update;
2203
2503
  TMP_TABLE_PARAM *tmp_table_param;
2204
2504
  ha_rows updated, found;
2205
2505
  List <Item> *fields, *values;
2206
2506
  List <Item> **fields_for_table, **values_for_table;
2207
 
  uint32_t table_count;
 
2507
  uint table_count;
2208
2508
  /*
2209
2509
   List of tables referenced in the CHECK OPTION condition of
2210
2510
   the updated view excluding the updated table. 
2211
2511
  */
2212
 
  List <Table> unupdated_check_opt_tables;
 
2512
  List <TABLE> unupdated_check_opt_tables;
2213
2513
  Copy_field *copy_field;
2214
2514
  enum enum_duplicates handle_duplicates;
2215
2515
  bool do_update, trans_safe;
2223
2523
  bool error_handled;
2224
2524
 
2225
2525
public:
2226
 
  multi_update(TableList *ut, TableList *leaves_list,
 
2526
  multi_update(TABLE_LIST *ut, TABLE_LIST *leaves_list,
2227
2527
               List<Item> *fields, List<Item> *values,
2228
2528
               enum_duplicates handle_duplicates, bool ignore);
2229
2529
  ~multi_update();
2230
2530
  int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
2231
2531
  bool send_data(List<Item> &items);
2232
2532
  bool initialize_tables (JOIN *join);
2233
 
  void send_error(uint32_t errcode,const char *err);
 
2533
  void send_error(uint errcode,const char *err);
2234
2534
  int  do_updates();
2235
2535
  bool send_eof();
2236
2536
  virtual void abort();
2240
2540
public:
2241
2541
  LEX_STRING s;
2242
2542
  bool local;
2243
 
  uint32_t offset;
 
2543
  uint offset;
2244
2544
  enum_field_types type;
2245
 
  my_var (LEX_STRING& j, bool i, uint32_t o, enum_field_types t)
 
2545
  my_var (LEX_STRING& j, bool i, uint o, enum_field_types t)
2246
2546
    :s(j), local(i), offset(o), type(t)
2247
2547
  {}
2248
2548
  ~my_var() {}
2263
2563
 
2264
2564
/* Bits in sql_command_flags */
2265
2565
 
2266
 
enum sql_command_flag_bits {
2267
 
  CF_BIT_CHANGES_DATA,
2268
 
  CF_BIT_HAS_ROW_COUNT,
2269
 
  CF_BIT_STATUS_COMMAND,
2270
 
  CF_BIT_SHOW_TABLE_COMMAND,
2271
 
  CF_BIT_WRITE_LOGS_COMMAND,
2272
 
  CF_BIT_SIZE
2273
 
};
2274
 
 
2275
 
static const std::bitset<CF_BIT_SIZE> CF_CHANGES_DATA(1 << CF_BIT_CHANGES_DATA);
2276
 
static const std::bitset<CF_BIT_SIZE> CF_HAS_ROW_COUNT(1 << CF_BIT_HAS_ROW_COUNT);
2277
 
static const std::bitset<CF_BIT_SIZE> CF_STATUS_COMMAND(1 << CF_BIT_STATUS_COMMAND);
2278
 
static const std::bitset<CF_BIT_SIZE> CF_SHOW_TABLE_COMMAND(1 << CF_BIT_SHOW_TABLE_COMMAND);
2279
 
static const std::bitset<CF_BIT_SIZE> CF_WRITE_LOGS_COMMAND(1 << CF_BIT_WRITE_LOGS_COMMAND);
 
2566
#define CF_CHANGES_DATA         1
 
2567
#define CF_HAS_ROW_COUNT        2
 
2568
#define CF_STATUS_COMMAND       4
 
2569
#define CF_SHOW_TABLE_COMMAND   8
 
2570
#define CF_WRITE_LOGS_COMMAND  16
2280
2571
 
2281
2572
/* Functions in sql_class.cc */
2282
2573
 
2285
2576
void add_diff_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var,
2286
2577
                        STATUS_VAR *dec_var);
2287
2578
 
2288
 
void close_connection(Session *session, uint32_t errcode, bool lock);
2289
 
 
2290
 
/* Some inline functions for more speed */
2291
 
 
2292
 
inline bool add_item_to_list(Session *session, Item *item)
2293
 
{
2294
 
  return session->lex->current_select->add_item_to_list(session, item);
2295
 
}
2296
 
 
2297
 
inline bool add_value_to_list(Session *session, Item *value)
2298
 
{
2299
 
  return session->lex->value_list.push_back(value);
2300
 
}
2301
 
 
2302
 
inline bool add_order_to_list(Session *session, Item *item, bool asc)
2303
 
{
2304
 
  return session->lex->current_select->add_order_to_list(session, item, asc);
2305
 
}
2306
 
 
2307
 
inline bool add_group_to_list(Session *session, Item *item, bool asc)
2308
 
{
2309
 
  return session->lex->current_select->add_group_to_list(session, item, asc);
2310
 
}
2311
 
 
2312
 
#endif /* DRIZZLE_SERVER */
2313
 
 
2314
 
#endif /* DRIZZLED_SQL_CLASS_H */
 
2579
#endif /* MYSQL_SERVER */