~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.h

  • Committer: Djellel E. Difallah
  • Date: 2010-03-27 10:10:49 UTC
  • mto: This revision was merged to the branch mainline in revision 1429.
  • Revision ID: ded@ubuntu-20100327101049-oo3arvatjoyku124
merge my_decimal and decimal

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
 
20
 
20
21
#ifndef DRIZZLED_SESSION_H
21
22
#define DRIZZLED_SESSION_H
22
23
 
23
 
#include <algorithm>
24
 
#include <bitset>
25
 
#include <boost/make_shared.hpp>
26
 
#include <boost/thread/condition_variable.hpp>
27
 
#include <boost/thread/mutex.hpp>
28
 
#include <boost/thread/shared_mutex.hpp>
29
 
#include <boost/thread/thread.hpp>
 
24
/* Classes in mysql */
 
25
 
 
26
#include "drizzled/plugin.h"
 
27
#include <drizzled/sql_locale.h>
 
28
#include "drizzled/resource_context.h"
 
29
#include <drizzled/cursor.h>
 
30
#include <drizzled/current_session.h>
 
31
#include <drizzled/sql_error.h>
 
32
#include <drizzled/file_exchange.h>
 
33
#include <drizzled/select_result_interceptor.h>
 
34
#include <drizzled/xid.h>
 
35
#include "drizzled/query_id.h"
 
36
#include "drizzled/named_savepoint.h"
 
37
#include "drizzled/transaction_context.h"
 
38
 
 
39
#include <netdb.h>
30
40
#include <map>
31
 
#include <netdb.h>
32
41
#include <string>
33
 
#include <sys/resource.h>
34
 
#include <sys/time.h>
35
 
 
36
 
#include <drizzled/catalog/instance.h>
37
 
#include <drizzled/catalog/local.h>
38
 
#include <drizzled/copy_info.h>
39
 
#include <drizzled/cursor.h>
 
42
#include <bitset>
 
43
#include <deque>
 
44
 
 
45
#include <drizzled/security_context.h>
 
46
#include <drizzled/open_tables_state.h>
 
47
 
 
48
#include <drizzled/internal_error_handler.h>
40
49
#include <drizzled/diagnostics_area.h>
41
 
#include <drizzled/file_exchange.h>
42
 
#include <drizzled/ha_data.h>
43
 
#include <drizzled/identifier.h>
44
 
#include <drizzled/lex_column.h>
45
 
#include <drizzled/my_hash.h>
46
 
#include <drizzled/named_savepoint.h>
47
 
#include <drizzled/open_tables_state.h>
48
 
#include <drizzled/plugin.h>
 
50
 
49
51
#include <drizzled/plugin/authorization.h>
50
 
#include <drizzled/pthread_globals.h>
51
 
#include <drizzled/query_id.h>
52
 
#include <drizzled/resource_context.h>
53
 
#include <drizzled/session/property_map.h>
54
 
#include <drizzled/session/state.h>
55
 
#include <drizzled/session/table_messages.h>
56
 
#include <drizzled/session/transactions.h>
57
 
#include <drizzled/sql_error.h>
58
 
#include <drizzled/sql_lex.h>
59
 
#include <drizzled/sql_locale.h>
60
 
#include <drizzled/statistics_variables.h>
61
 
#include <drizzled/system_variables.h>
62
 
#include <drizzled/system_variables.h>
63
 
#include <drizzled/table_ident.h>
64
 
#include <drizzled/transaction_context.h>
65
 
#include <drizzled/util/storable.h>
66
 
#include <drizzled/var.h>
67
 
#include <drizzled/visibility.h>
68
52
 
69
53
#define MIN_HANDSHAKE_SIZE      6
70
54
 
75
59
{
76
60
class Client;
77
61
class Scheduler;
78
 
class EventObserverList;
79
62
}
80
 
 
81
63
namespace message
82
64
{
83
65
class Transaction;
84
66
class Statement;
85
 
class Resultset;
86
 
}
87
 
 
88
 
namespace internal { struct st_my_thread_var; }
89
 
namespace table { class Placeholder; }
90
 
 
 
67
}
 
68
namespace internal
 
69
{
 
70
struct st_my_thread_var;
 
71
}
 
72
 
 
73
class Lex_input_stream;
 
74
class user_var_entry;
91
75
class CopyField;
92
 
class DrizzleXid;
93
 
class Internal_error_handler;
94
 
class Lex_input_stream;
95
 
class TableShareInstance;
96
76
class Table_ident;
97
 
class Time_zone;
98
 
class select_result;
99
 
class user_var_entry;
100
77
 
101
78
extern char internal_table_name[2];
102
79
extern char empty_c_string[1];
106
83
#define TC_HEURISTIC_RECOVER_ROLLBACK 2
107
84
extern uint32_t tc_heuristic_recover;
108
85
 
 
86
/**
 
87
  @brief
 
88
  Local storage for proto that are tmp table. This should be enlarged
 
89
  to hande the entire table-share for a local table. Once Hash is done,
 
90
  we should consider exchanging the map for it.
 
91
*/
 
92
typedef std::map <std::string, message::Table> ProtoCache;
 
93
 
 
94
/**
 
95
  The COPY_INFO structure is used by INSERT/REPLACE code.
 
96
  The schema of the row counting by the INSERT/INSERT ... ON DUPLICATE KEY
 
97
  UPDATE code:
 
98
    If a row is inserted then the copied variable is incremented.
 
99
    If a row is updated by the INSERT ... ON DUPLICATE KEY UPDATE and the
 
100
      new data differs from the old one then the copied and the updated
 
101
      variables are incremented.
 
102
    The touched variable is incremented if a row was touched by the update part
 
103
      of the INSERT ... ON DUPLICATE KEY UPDATE no matter whether the row
 
104
      was actually changed or not.
 
105
*/
 
106
typedef struct st_copy_info 
 
107
{
 
108
  ha_rows records; /**< Number of processed records */
 
109
  ha_rows deleted; /**< Number of deleted records */
 
110
  ha_rows updated; /**< Number of updated records */
 
111
  ha_rows copied;  /**< Number of copied records */
 
112
  ha_rows error_count;
 
113
  ha_rows touched; /* Number of touched records */
 
114
  enum enum_duplicates handle_duplicates;
 
115
  int escape_char, last_errno;
 
116
  bool ignore;
 
117
  /* for INSERT ... UPDATE */
 
118
  List<Item> *update_fields;
 
119
  List<Item> *update_values;
 
120
  /* for VIEW ... WITH CHECK OPTION */
 
121
} COPY_INFO;
 
122
 
 
123
typedef struct drizzled_lock_st
 
124
{
 
125
  Table **table;
 
126
  uint32_t table_count;
 
127
  uint32_t lock_count;
 
128
  THR_LOCK_DATA **locks;
 
129
} DRIZZLE_LOCK;
 
130
 
 
131
} /* namespace drizzled */
 
132
 
 
133
/** @TODO why is this in the middle of the file */
 
134
#include <drizzled/lex_column.h>
 
135
 
 
136
namespace drizzled
 
137
{
 
138
 
 
139
class select_result;
 
140
class Time_zone;
 
141
 
109
142
#define Session_SENTRY_MAGIC 0xfeedd1ff
110
143
#define Session_SENTRY_GONE  0xdeadbeef
111
144
 
112
 
extern DRIZZLED_API struct drizzle_system_variables global_system_variables;
 
145
#define Session_CHECK_SENTRY(session) assert(session->dbug_sentry == Session_SENTRY_MAGIC)
 
146
 
 
147
struct system_variables
 
148
{
 
149
  system_variables() {};
 
150
  /*
 
151
    How dynamically allocated system variables are handled:
 
152
 
 
153
    The global_system_variables and max_system_variables are "authoritative"
 
154
    They both should have the same 'version' and 'size'.
 
155
    When attempting to access a dynamic variable, if the session version
 
156
    is out of date, then the session version is updated and realloced if
 
157
    neccessary and bytes copied from global to make up for missing data.
 
158
  */
 
159
  ulong dynamic_variables_version;
 
160
  char * dynamic_variables_ptr;
 
161
  uint32_t dynamic_variables_head;  /* largest valid variable offset */
 
162
  uint32_t dynamic_variables_size;  /* how many bytes are in use */
 
163
 
 
164
  uint64_t myisam_max_extra_sort_file_size;
 
165
  uint64_t max_heap_table_size;
 
166
  uint64_t tmp_table_size;
 
167
  ha_rows select_limit;
 
168
  ha_rows max_join_size;
 
169
  uint64_t auto_increment_increment;
 
170
  uint64_t auto_increment_offset;
 
171
  uint64_t bulk_insert_buff_size;
 
172
  uint64_t join_buff_size;
 
173
  uint32_t max_allowed_packet;
 
174
  uint64_t max_error_count;
 
175
  uint64_t max_length_for_sort_data;
 
176
  size_t max_sort_length;
 
177
  uint64_t min_examined_row_limit;
 
178
  bool optimizer_prune_level;
 
179
  bool log_warnings;
 
180
 
 
181
  uint32_t optimizer_search_depth;
 
182
  uint32_t div_precincrement;
 
183
  uint64_t preload_buff_size;
 
184
  uint32_t read_buff_size;
 
185
  uint32_t read_rnd_buff_size;
 
186
  size_t sortbuff_size;
 
187
  uint32_t thread_handling;
 
188
  uint32_t tx_isolation;
 
189
  uint32_t completion_type;
 
190
  /* Determines which non-standard SQL behaviour should be enabled */
 
191
  uint32_t sql_mode;
 
192
  uint64_t max_seeks_for_key;
 
193
  size_t range_alloc_block_size;
 
194
  uint32_t query_alloc_block_size;
 
195
  uint32_t query_prealloc_size;
 
196
  uint64_t group_concat_max_len;
 
197
  uint64_t pseudo_thread_id;
 
198
 
 
199
  plugin::StorageEngine *storage_engine;
 
200
 
 
201
  /* Only charset part of these variables is sensible */
 
202
  const CHARSET_INFO  *character_set_filesystem;
 
203
 
 
204
  /* Both charset and collation parts of these variables are important */
 
205
  const CHARSET_INFO    *collation_server;
 
206
 
 
207
  inline const CHARSET_INFO  *getCollation(void) 
 
208
  {
 
209
    return collation_server;
 
210
  }
 
211
 
 
212
  /* Locale Support */
 
213
  MY_LOCALE *lc_time_names;
 
214
 
 
215
  Time_zone *time_zone;
 
216
};
 
217
 
 
218
extern struct system_variables global_system_variables;
 
219
 
 
220
} /* namespace drizzled */
 
221
 
 
222
#include "drizzled/sql_lex.h"
 
223
 
 
224
namespace drizzled
 
225
{
 
226
 
 
227
/**
 
228
 * Per-session local status counters
 
229
 */
 
230
typedef struct system_status_var
 
231
{
 
232
  uint64_t bytes_received;
 
233
  uint64_t bytes_sent;
 
234
  ulong com_other;
 
235
  ulong com_stat[(uint32_t) SQLCOM_END];
 
236
  ulong created_tmp_disk_tables;
 
237
  ulong created_tmp_tables;
 
238
  ulong ha_commit_count;
 
239
  ulong ha_delete_count;
 
240
  ulong ha_read_first_count;
 
241
  ulong ha_read_last_count;
 
242
  ulong ha_read_key_count;
 
243
  ulong ha_read_next_count;
 
244
  ulong ha_read_prev_count;
 
245
  ulong ha_read_rnd_count;
 
246
  ulong ha_read_rnd_next_count;
 
247
  ulong ha_rollback_count;
 
248
  ulong ha_update_count;
 
249
  ulong ha_write_count;
 
250
  ulong ha_prepare_count;
 
251
  ulong ha_savepoint_count;
 
252
  ulong ha_savepoint_rollback_count;
 
253
 
 
254
  /* KEY_CACHE parts. These are copies of the original */
 
255
  ulong key_blocks_changed;
 
256
  ulong key_blocks_used;
 
257
  ulong key_cache_r_requests;
 
258
  ulong key_cache_read;
 
259
  ulong key_cache_w_requests;
 
260
  ulong key_cache_write;
 
261
  /* END OF KEY_CACHE parts */
 
262
 
 
263
  ulong net_big_packet_count;
 
264
  ulong select_full_join_count;
 
265
  ulong select_full_range_join_count;
 
266
  ulong select_range_count;
 
267
  ulong select_range_check_count;
 
268
  ulong select_scan_count;
 
269
  ulong long_query_count;
 
270
  ulong filesort_merge_passes;
 
271
  ulong filesort_range_count;
 
272
  ulong filesort_rows;
 
273
  ulong filesort_scan_count;
 
274
  /*
 
275
    Number of statements sent from the client
 
276
  */
 
277
  ulong questions;
 
278
 
 
279
  /*
 
280
    IMPORTANT!
 
281
    SEE last_system_status_var DEFINITION BELOW.
 
282
 
 
283
    Below 'last_system_status_var' are all variables which doesn't make any
 
284
    sense to add to the /global/ status variable counter.
 
285
  */
 
286
  double last_query_cost;
 
287
} system_status_var;
 
288
 
 
289
/*
 
290
  This is used for 'SHOW STATUS'. It must be updated to the last ulong
 
291
  variable in system_status_var which is makes sens to add to the global
 
292
  counter
 
293
*/
 
294
 
 
295
#define last_system_status_var questions
 
296
 
 
297
void mark_transaction_to_rollback(Session *session, bool all);
 
298
 
 
299
extern pthread_mutex_t LOCK_xid_cache;
 
300
extern HASH xid_cache;
 
301
 
 
302
 
 
303
/**
 
304
  Storage engine specific thread local data.
 
305
*/
 
306
struct Ha_data
 
307
{
 
308
  /**
 
309
    Storage engine specific thread local data.
 
310
    Lifetime: one user connection.
 
311
  */
 
312
  void *ha_ptr;
 
313
  /**
 
314
   * Resource contexts for both the "statement" and "normal"
 
315
   * transactions.
 
316
   *
 
317
   * Resource context at index 0:
 
318
   *
 
319
   * Life time: one statement within a transaction. If @@autocommit is
 
320
   * on, also represents the entire transaction.
 
321
   *
 
322
   * Resource context at index 1:
 
323
   *
 
324
   * Life time: one transaction within a connection. 
 
325
   *
 
326
   * @note
 
327
   *
 
328
   * If the storage engine does not participate in a transaction, 
 
329
   * there will not be a resource context.
 
330
   */
 
331
  drizzled::ResourceContext resource_context[2];
 
332
 
 
333
  Ha_data() :ha_ptr(NULL) {}
 
334
};
113
335
 
114
336
/**
115
337
 * Represents a client connection to the database server.
130
352
 * all member variables that are not critical to non-internal operations of the
131
353
 * session object.
132
354
 */
133
 
 
134
 
class DRIZZLED_API Session : public Open_tables_state
 
355
class Session : public Open_tables_state
135
356
{
136
357
public:
137
 
  // Plugin storage in Session.
138
 
  typedef boost::shared_ptr<Session> shared_ptr;
139
 
  typedef Session& reference;
140
 
  typedef const Session& const_reference;
141
 
  typedef const Session* const_pointer;
142
 
  typedef Session* pointer;
143
 
 
144
 
  static shared_ptr make_shared(plugin::Client *client, catalog::Instance::shared_ptr instance_arg)
145
 
  {
146
 
    assert(instance_arg);
147
 
    return boost::make_shared<Session>(client, instance_arg);
148
 
  }
149
 
 
150
358
  /*
151
359
    MARK_COLUMNS_NONE:  Means mark_used_colums is not set and no indicator to
152
360
                        handler of fields used is set
159
367
                        and update_row.
160
368
  */
161
369
  enum enum_mark_columns mark_used_columns;
 
370
  inline void* alloc(size_t size)
 
371
  {
 
372
    return alloc_root(mem_root,size);
 
373
  }
162
374
  inline void* calloc(size_t size)
163
375
  {
164
376
    void *ptr;
165
 
    if ((ptr= mem_root->alloc_root(size)))
 
377
    if ((ptr= alloc_root(mem_root,size)))
166
378
      memset(ptr, 0, size);
167
379
    return ptr;
168
380
  }
 
381
  inline char *strdup(const char *str)
 
382
  {
 
383
    return strdup_root(mem_root,str);
 
384
  }
169
385
  inline char *strmake(const char *str, size_t size)
170
386
  {
171
 
    return mem_root->strmake_root(str,size);
172
 
  }
173
 
 
 
387
    return strmake_root(mem_root,str,size);
 
388
  }
 
389
  inline void *memdup(const void *str, size_t size)
 
390
  {
 
391
    return memdup_root(mem_root,str,size);
 
392
  }
174
393
  inline void *memdup_w_gap(const void *str, size_t size, uint32_t gap)
175
394
  {
176
395
    void *ptr;
177
 
    if ((ptr= mem_root->alloc_root(size + gap)))
 
396
    if ((ptr= alloc_root(mem_root,size+gap)))
178
397
      memcpy(ptr,str,size);
179
398
    return ptr;
180
399
  }
181
400
  /** Frees all items attached to this Statement */
182
401
  void free_items();
183
 
 
184
402
  /**
185
403
   * List of items created in the parser for this query. Every item puts
186
404
   * itself to the list on creation (see Item::Item() for details))
187
405
   */
188
406
  Item *free_list;
189
407
  memory::Root *mem_root; /**< Pointer to current memroot */
190
 
 
191
 
 
192
 
  memory::Root *getMemRoot()
193
 
  {
194
 
    return mem_root;
195
 
  }
196
 
 
197
 
  uint64_t xa_id;
198
 
 
199
 
  uint64_t getXaId()
200
 
  {
201
 
    return xa_id;
202
 
  }
203
 
 
204
 
  void setXaId(uint64_t in_xa_id)
205
 
  {
206
 
    xa_id= in_xa_id; 
207
 
  }
208
 
 
209
408
  /**
210
409
   * Uniquely identifies each statement object in thread scope; change during
211
410
   * statement lifetime.
213
412
   * @todo should be const
214
413
   */
215
414
  uint32_t id;
216
 
private:
217
415
  LEX *lex; /**< parse tree descriptor */
218
 
 
219
 
public:
220
 
  LEX *getLex() 
221
 
  {
222
 
    return lex;
223
 
  }
224
 
 
225
 
  enum_sql_command getSqlCommand() const
226
 
  {
227
 
    return lex->sql_command;
228
 
  }
229
 
 
230
416
  /** query associated with this statement */
231
 
  typedef boost::shared_ptr<const std::string> QueryString;
232
 
 
233
 
private:
234
 
  boost::shared_ptr<std::string> query;
235
 
 
236
 
  // Never allow for a modification of this outside of the class. c_str()
237
 
  // requires under some setup non const, you must copy the QueryString in
238
 
  // order to use it.
239
 
public:
240
 
  QueryString getQueryString() const
241
 
  {
242
 
    return query;
243
 
  }
244
 
 
245
 
  void resetQueryString()
246
 
  {
247
 
    query.reset();
248
 
    _state.reset();
249
 
  }
250
 
 
251
 
  /*
252
 
    We need to copy the lock on the string in order to make sure we have a stable string.
253
 
    Once this is done we can use it to build a const char* which can be handed off for
254
 
    a method to use (Innodb is currently the only engine using this).
255
 
  */
256
 
  const char *getQueryStringCopy(size_t &length)
257
 
  {
258
 
    QueryString tmp_string(getQueryString());
259
 
 
260
 
    if (not tmp_string)
261
 
    {
262
 
      length= 0;
263
 
      return NULL;
264
 
    }
265
 
 
266
 
    length= tmp_string->length();
267
 
    char *to_return= strmake(tmp_string->c_str(), tmp_string->length());
268
 
    return to_return;
269
 
  }
270
 
 
271
 
private:
272
 
  session::State::shared_ptr  _state; 
273
 
 
274
 
public:
275
 
 
276
 
  session::State::const_shared_ptr state()
277
 
  {
278
 
    return _state;
279
 
  }
 
417
  std::string query;
280
418
 
281
419
  /**
282
420
    Name of the current (default) database.
290
428
    the Session of that thread); that thread is (and must remain, for now) the
291
429
    only responsible for freeing this member.
292
430
  */
293
 
private:
294
 
  util::string::shared_ptr _schema;
295
 
 
296
 
public:
297
 
 
298
 
  util::string::const_shared_ptr schema() const
299
 
  {
300
 
    if (_schema)
301
 
      return _schema;
302
 
 
303
 
    return util::string::const_shared_ptr(new std::string(""));
304
 
  }
305
 
 
306
 
  /* current cache key */
307
 
  std::string query_cache_key;
 
431
  std::string db;
 
432
 
308
433
  /**
309
434
    Constant for Session::where initialization in the beginning of every query.
310
435
 
314
439
  static const char * const DEFAULT_WHERE;
315
440
 
316
441
  memory::Root warn_root; /**< Allocation area for warnings and errors */
317
 
private:
318
442
  plugin::Client *client; /**< Pointer to client object */
319
 
 
320
 
public:
321
 
 
322
 
  void setClient(plugin::Client *client_arg);
323
 
 
324
 
  plugin::Client *getClient()
325
 
  {
326
 
    return client;
327
 
  }
328
 
 
329
 
  plugin::Client *getClient() const
330
 
  {
331
 
    return client;
332
 
  }
333
 
 
334
443
  plugin::Scheduler *scheduler; /**< Pointer to scheduler object */
335
444
  void *scheduler_arg; /**< Pointer to the optional scheduler argument */
336
 
 
337
 
  typedef boost::unordered_map< std::string, user_var_entry *, util::insensitive_hash, util::insensitive_equal_to> UserVars;
338
 
 
339
 
private:
340
 
  typedef std::pair< UserVars::iterator, UserVars::iterator > UserVarsRange;
341
 
  UserVars user_vars; /**< Hash of user variables defined during the session's lifetime */
342
 
 
343
 
public:
344
 
  const UserVars &getUserVariables() const
345
 
  {
346
 
    return user_vars;
347
 
  }
348
 
 
349
 
  drizzle_system_variables variables; /**< Mutable local variables local to the session */
350
 
 
351
 
  enum_tx_isolation getTxIsolation()
352
 
  {
353
 
    return (enum_tx_isolation)variables.tx_isolation;
354
 
  }
355
 
 
 
445
  HASH user_vars; /**< Hash of user variables defined during the session's lifetime */
 
446
  struct system_variables variables; /**< Mutable local variables local to the session */
356
447
  struct system_status_var status_var; /**< Session-local status counters */
 
448
  struct system_status_var *initial_status_var; /* used by show status */
357
449
  THR_LOCK_INFO lock_info; /**< Locking information for this session */
358
450
  THR_LOCK_OWNER main_lock_id; /**< To use for conventional queries */
359
451
  THR_LOCK_OWNER *lock_id; /**< If not main_lock_id, points to the lock_id of a cursor. */
 
452
  pthread_mutex_t LOCK_delete; /**< Locked before session is deleted */
 
453
 
 
454
  /**
 
455
   * A peek into the query string for the session. This is a best effort
 
456
   * delivery, there is no guarantee whether the content is meaningful.
 
457
   */
 
458
  char process_list_info[PROCESS_LIST_WIDTH+1];
360
459
 
361
460
  /**
362
461
   * A pointer to the stack frame of the scheduler thread
365
464
  char *thread_stack;
366
465
 
367
466
private:
368
 
  identifier::User::shared_ptr security_ctx;
369
 
 
370
 
  int32_t scoreboard_index;
371
 
 
372
 
  inline void checkSentry() const
373
 
  {
374
 
    assert(this->dbug_sentry == Session_SENTRY_MAGIC);
375
 
  }
376
 
 
 
467
  SecurityContext security_ctx;
377
468
public:
378
 
  identifier::User::const_shared_ptr user() const
379
 
  {
380
 
    if (security_ctx)
381
 
      return security_ctx;
382
 
 
383
 
    return identifier::User::const_shared_ptr();
384
 
  }
385
 
 
386
 
  void setUser(identifier::User::shared_ptr arg)
387
 
  {
388
 
    security_ctx= arg;
389
 
  }
390
 
 
391
 
  int32_t getScoreboardIndex()
392
 
  {
393
 
    return scoreboard_index;
394
 
  }
395
 
 
396
 
  void setScoreboardIndex(int32_t in_scoreboard_index)
397
 
  {
398
 
    scoreboard_index= in_scoreboard_index;
 
469
  const SecurityContext& getSecurityContext() const
 
470
  {
 
471
    return security_ctx;
 
472
  }
 
473
 
 
474
  SecurityContext& getSecurityContext()
 
475
  {
 
476
    return security_ctx;
399
477
  }
400
478
 
401
479
  /**
402
480
   * Is this session viewable by the current user?
403
481
   */
404
 
  bool isViewable(identifier::User::const_reference) const;
 
482
  bool isViewable() const
 
483
  {
 
484
    return plugin::Authorization::isAuthorized(current_session->getSecurityContext(),
 
485
                                               this,
 
486
                                               false);
 
487
  }
405
488
 
406
 
private:
407
489
  /**
408
490
    Used in error messages to tell user in what part of MySQL we found an
409
491
    error. E. g. when where= "having clause", if fix_fields() fails, user
410
492
    will know that the error was in having clause.
411
493
  */
412
 
  const char *_where;
413
 
 
414
 
public:
415
 
  const char *where()
416
 
  {
417
 
    return _where;
418
 
  }
419
 
 
420
 
  void setWhere(const char *arg)
421
 
  {
422
 
    _where= arg;
423
 
  }
 
494
  const char *where;
424
495
 
425
496
  /*
426
497
    One thread can hold up to one named user-level lock. This variable
428
499
    chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK.
429
500
  */
430
501
  uint32_t dbug_sentry; /**< watch for memory corruption */
431
 
 
432
 
private:
433
 
  boost::thread::id boost_thread_id;
434
 
  boost_thread_shared_ptr _thread;
435
 
  boost::this_thread::disable_interruption *interrupt;
436
 
 
437
502
  internal::st_my_thread_var *mysys_var;
438
 
 
439
 
public:
440
 
  boost_thread_shared_ptr &getThread()
441
 
  {
442
 
    return _thread;
443
 
  }
444
 
 
445
 
  void pushInterrupt(boost::this_thread::disable_interruption *interrupt_arg)
446
 
  {
447
 
    interrupt= interrupt_arg;
448
 
  }
449
 
 
450
 
  boost::this_thread::disable_interruption &getThreadInterupt()
451
 
  {
452
 
    assert(interrupt);
453
 
    return *interrupt;
454
 
  }
455
 
 
456
 
  internal::st_my_thread_var *getThreadVar()
457
 
  {
458
 
    return mysys_var;
459
 
  }
460
 
 
461
503
  /**
462
504
   * Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. Set from
463
505
   * first byte of the packet in executeStatement()
466
508
  uint32_t file_id;     /**< File ID for LOAD DATA INFILE */
467
509
  /* @note the following three members should likely move to Client */
468
510
  uint32_t max_client_packet_length; /**< Maximum number of bytes a client can send in a single packet */
469
 
 
470
 
private:
471
 
  boost::posix_time::ptime _epoch;
472
 
  boost::posix_time::ptime _connect_time;
473
 
  boost::posix_time::ptime _start_timer;
474
 
  boost::posix_time::ptime _end_timer;
475
 
 
476
 
  boost::posix_time::ptime _user_time;
477
 
public:
478
 
  uint64_t utime_after_lock; // This used by Innodb.
479
 
 
480
 
  void resetUserTime()
481
 
  {
482
 
    _user_time= boost::posix_time::not_a_date_time;
483
 
  }
484
 
 
485
 
  const boost::posix_time::ptime &start_timer() const
486
 
  {
487
 
    return _start_timer;
488
 
  }
489
 
 
490
 
  void getTimeDifference(boost::posix_time::time_duration &result_arg, const boost::posix_time::ptime &arg) const
491
 
  {
492
 
    result_arg=  arg - _start_timer;
493
 
  }
 
511
  time_t start_time;
 
512
  time_t user_time;
 
513
  uint64_t thr_create_utime; /**< track down slow pthread_create */
 
514
  uint64_t start_utime;
 
515
  uint64_t utime_after_lock;
494
516
 
495
517
  thr_lock_type update_lock_default;
496
518
 
511
533
  */
512
534
  query_id_t query_id;
513
535
  query_id_t warn_query_id;
514
 
 
515
536
public:
516
537
  void **getEngineData(const plugin::MonitoredInTransaction *monitored);
517
538
  ResourceContext *getResourceContext(const plugin::MonitoredInTransaction *monitored,
518
539
                                      size_t index= 0);
519
540
 
520
 
  session::Transactions transaction;
 
541
  struct st_transactions {
 
542
    std::deque<NamedSavepoint> savepoints;
 
543
    TransactionContext all; ///< Trans since BEGIN WORK
 
544
    TransactionContext stmt; ///< Trans for current statement
 
545
    XID_STATE xid_state;
 
546
 
 
547
    void cleanup()
 
548
    {
 
549
      savepoints.clear();
 
550
    }
 
551
    st_transactions() :
 
552
      savepoints(),
 
553
      all(),
 
554
      stmt(),
 
555
      xid_state()
 
556
    { }
 
557
  } transaction;
521
558
 
522
559
  Field *dup_field;
523
560
  sigset_t signals;
524
561
 
525
 
  // As of right now we do not allow a concurrent execute to launch itself
526
 
private:
527
 
  bool concurrent_execute_allowed;
528
 
 
529
 
public:
530
 
 
531
 
  void setConcurrentExecute(bool arg)
532
 
  {
533
 
    concurrent_execute_allowed= arg;
534
 
  }
535
 
 
536
 
  bool isConcurrentExecuteAllowed() const
537
 
  {
538
 
    return concurrent_execute_allowed;
539
 
  }
540
 
 
541
562
  /* Tells if LAST_INSERT_ID(#) was called for the current statement */
542
563
  bool arg_of_last_insert_id_function;
543
 
 
544
564
  /*
545
565
    ALL OVER THIS FILE, "insert_id" means "*automatically generated* value for
546
566
    insertion into an auto_increment column".
594
614
  uint64_t limit_found_rows;
595
615
  uint64_t options; /**< Bitmap of options */
596
616
  int64_t row_count_func; /**< For the ROW_COUNT() function */
597
 
 
598
 
  int64_t rowCount() const
599
 
  {
600
 
    return row_count_func;
601
 
  }
602
 
 
603
617
  ha_rows cuted_fields; /**< Count of "cut" or truncated fields. @todo Kill this friggin thing. */
604
618
 
605
619
  /** 
648
662
    create_sort_index(); may differ from examined_row_count.
649
663
  */
650
664
  uint32_t row_count;
651
 
 
652
 
  uint32_t getRowCount() const
653
 
  {
654
 
    return row_count;
655
 
  }
656
 
 
657
 
  session_id_t thread_id;
 
665
  pthread_t real_id; /**< For debugging */
 
666
  uint64_t thread_id;
658
667
  uint32_t tmp_table;
659
 
  enum global_read_lock_t
660
 
  {
661
 
    NONE= 0,
662
 
    GOT_GLOBAL_READ_LOCK= 1,
663
 
    MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT= 2
664
 
  };
665
 
private:
666
 
  global_read_lock_t _global_read_lock;
667
 
 
668
 
public:
669
 
 
670
 
  global_read_lock_t isGlobalReadLock() const
671
 
  {
672
 
    return _global_read_lock;
673
 
  }
674
 
 
675
 
  void setGlobalReadLock(global_read_lock_t arg)
676
 
  {
677
 
    _global_read_lock= arg;
678
 
  }
679
 
 
680
 
  DrizzleLock *lockTables(Table **tables, uint32_t count, uint32_t flags);
681
 
  bool lockGlobalReadLock();
682
 
  bool lock_table_names(TableList *table_list);
683
 
  bool lock_table_names_exclusively(TableList *table_list);
684
 
  bool makeGlobalReadLockBlockCommit();
685
 
  bool abortLockForThread(Table *table);
686
 
  bool wait_if_global_read_lock(bool abort_on_refresh, bool is_not_commit);
687
 
  int lock_table_name(TableList *table_list);
688
 
  void abortLock(Table *table);
689
 
  void removeLock(Table *table);
690
 
  void unlockReadTables(DrizzleLock *sql_lock);
691
 
  void unlockSomeTables(Table **table, uint32_t count);
692
 
  void unlockTables(DrizzleLock *sql_lock);
693
 
  void startWaitingGlobalReadLock();
694
 
  void unlockGlobalReadLock();
695
 
 
696
 
private:
697
 
  int unlock_external(Table **table, uint32_t count);
698
 
  int lock_external(Table **tables, uint32_t count);
699
 
  bool wait_for_locked_table_names(TableList *table_list);
700
 
  DrizzleLock *get_lock_data(Table **table_ptr, uint32_t count,
701
 
                             bool should_lock, Table **write_lock_used);
702
 
public:
703
 
 
 
668
  uint32_t global_read_lock;
704
669
  uint32_t server_status;
705
670
  uint32_t open_options;
706
671
  uint32_t select_number; /**< number of select (used for EXPLAIN) */
708
673
  enum_tx_isolation session_tx_isolation;
709
674
  enum_check_fields count_cuted_fields;
710
675
 
711
 
  enum killed_state_t
 
676
  enum killed_state
712
677
  {
713
678
    NOT_KILLED,
714
679
    KILL_BAD_DATA,
716
681
    KILL_QUERY,
717
682
    KILLED_NO_VALUE /* means none of the above states apply */
718
683
  };
719
 
private:
720
 
  killed_state_t volatile _killed;
721
 
 
722
 
public:
723
 
 
724
 
  void setKilled(killed_state_t arg)
725
 
  {
726
 
    _killed= arg;
727
 
  }
728
 
 
729
 
  killed_state_t getKilled()
730
 
  {
731
 
    return _killed;
732
 
  }
733
 
 
734
 
  volatile killed_state_t *getKilledPtr() // Do not use this method, it is here for historical convience.
735
 
  {
736
 
    return &_killed;
737
 
  }
738
 
 
739
 
  bool is_admin_connection;
 
684
  killed_state volatile killed;
 
685
 
740
686
  bool some_tables_deleted;
741
687
  bool no_errors;
742
688
  bool password;
745
691
    can not continue. In particular, disables activation of
746
692
    CONTINUE or EXIT handlers of stored routines.
747
693
    Reset in the end of processing of the current user request, in
748
 
    @see reset_session_for_next_command().
 
694
    @see mysql_reset_session_for_next_command().
749
695
  */
750
696
  bool is_fatal_error;
751
697
  /**
770
716
  bool substitute_null_with_insert_id;
771
717
  bool cleanup_done;
772
718
 
773
 
private:
774
719
  bool abort_on_warning;
775
 
  bool tablespace_op; /**< This is true in DISCARD/IMPORT TABLESPACE */
776
 
 
777
 
public:
778
720
  bool got_warning; /**< Set on call to push_warning() */
779
721
  bool no_warnings_for_error; /**< no warnings on call to my_error() */
780
722
  /** set during loop of derived table processing */
781
723
  bool derived_tables_processing;
782
 
 
783
 
  bool doing_tablespace_operation(void)
784
 
  {
785
 
    return tablespace_op;
786
 
  }
787
 
 
788
 
  void setDoingTablespaceOperation(bool doing)
789
 
  {
790
 
    tablespace_op= doing;
791
 
  }
792
 
 
 
724
  bool tablespace_op; /**< This is true in DISCARD/IMPORT TABLESPACE */
793
725
 
794
726
  /** Used by the sys_var class to store temporary values */
795
727
  union
839
771
  }
840
772
 
841
773
  /** Returns the current query ID */
842
 
  query_id_t getQueryId()  const
 
774
  inline query_id_t getQueryId()  const
843
775
  {
844
776
    return query_id;
845
777
  }
857
789
    return warn_query_id;
858
790
  }
859
791
 
 
792
  /** Returns the current query text */
 
793
  inline const std::string &getQueryString()  const
 
794
  {
 
795
    return query;
 
796
  }
 
797
 
 
798
  /** Returns the length of the current query text */
 
799
  inline size_t getQueryLength() const
 
800
  {
 
801
    if (! query.empty())
 
802
      return query.length();
 
803
    else
 
804
      return 0;
 
805
  }
 
806
 
860
807
  /** Accessor method returning the session's ID. */
861
 
  inline session_id_t getSessionId()  const
 
808
  inline uint64_t getSessionId()  const
862
809
  {
863
810
    return thread_id;
864
811
  }
931
878
    auto_inc_intervals_forced.append(next_id, UINT64_MAX, 0);
932
879
  }
933
880
 
934
 
  Session(plugin::Client *client_arg, catalog::Instance::shared_ptr catalog);
 
881
  Session(plugin::Client *client_arg);
935
882
  virtual ~Session();
936
883
 
937
884
  void cleanup(void);
949
896
   */
950
897
  void cleanup_after_query();
951
898
  bool storeGlobals();
952
 
  void awake(Session::killed_state_t state_to_set);
 
899
  void awake(Session::killed_state state_to_set);
953
900
  /**
954
901
   * Pulls thread-specific variables into Session state.
955
902
   *
1014
961
  bool endTransaction(enum enum_mysql_completiontype completion);
1015
962
  bool endActiveTransaction();
1016
963
  bool startTransaction(start_transaction_option_t opt= START_TRANS_NO_OPTIONS);
1017
 
  void markTransactionForRollback(bool all);
1018
964
 
1019
965
  /**
1020
966
   * Authenticates users, with error reporting.
1033
979
  /**
1034
980
   * Schedule a session to be run on the default scheduler.
1035
981
   */
1036
 
  static bool schedule(Session::shared_ptr&);
1037
 
 
1038
 
  static void unlink(session_id_t &session_id);
1039
 
  static void unlink(Session::shared_ptr&);
 
982
  bool schedule();
1040
983
 
1041
984
  /*
1042
985
    For enter_cond() / exit_cond() to work the mutex must be got before
1043
986
    enter_cond(); this mutex is then released by exit_cond().
1044
987
    Usage must be: lock mutex; enter_cond(); your code; exit_cond().
1045
988
  */
1046
 
  const char* enter_cond(boost::condition_variable_any &cond, boost::mutex &mutex, const char* msg);
 
989
  const char* enter_cond(pthread_cond_t *cond, pthread_mutex_t* mutex, const char* msg);
1047
990
  void exit_cond(const char* old_msg);
1048
991
 
1049
 
  type::Time::epoch_t query_start()
1050
 
  {
1051
 
    return getCurrentTimestampEpoch();
1052
 
  }
1053
 
 
1054
 
  void set_time()
1055
 
  {
1056
 
    _end_timer= _start_timer= boost::posix_time::microsec_clock::universal_time();
1057
 
    utime_after_lock= (_start_timer - _epoch).total_microseconds();
1058
 
  }
1059
 
 
1060
 
  void set_time(time_t t) // This is done by a sys_var, as long as user_time is set, we will use that for all references to time
1061
 
  {
1062
 
    _user_time= boost::posix_time::from_time_t(t);
1063
 
  }
1064
 
 
1065
 
  void set_time_after_lock()
1066
 
  { 
1067
 
    boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::universal_time());
1068
 
    utime_after_lock= (mytime - _epoch).total_microseconds();
1069
 
  }
1070
 
 
1071
 
  void set_end_timer()
1072
 
  {
1073
 
    _end_timer= boost::posix_time::microsec_clock::universal_time();
1074
 
    status_var.execution_time_nsec+=(_end_timer - _start_timer).total_microseconds();
1075
 
  }
1076
 
 
1077
 
  uint64_t getElapsedTime() const
1078
 
  {
1079
 
    return (_end_timer - _start_timer).total_microseconds();
1080
 
  }
1081
 
 
 
992
  inline time_t query_start() { return start_time; }
 
993
  inline void set_time()
 
994
  {
 
995
    if (user_time)
 
996
    {
 
997
      start_time= user_time;
 
998
      connect_microseconds= start_utime= utime_after_lock= my_micro_time();
 
999
    }
 
1000
    else
 
1001
      start_utime= utime_after_lock= my_micro_time_and_time(&start_time);
 
1002
  }
 
1003
  inline void   set_current_time()    { start_time= time(NULL); }
 
1004
  inline void   set_time(time_t t)
 
1005
  {
 
1006
    start_time= user_time= t;
 
1007
    start_utime= utime_after_lock= my_micro_time();
 
1008
  }
 
1009
  void set_time_after_lock()  { utime_after_lock= my_micro_time(); }
1082
1010
  /**
1083
1011
   * Returns the current micro-timestamp
1084
1012
   */
1085
 
  type::Time::epoch_t getCurrentTimestamp(bool actual= true) const
1086
 
  { 
1087
 
    type::Time::epoch_t t_mark;
1088
 
 
1089
 
    if (actual)
1090
 
    {
1091
 
      boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::universal_time());
1092
 
      t_mark= (mytime - _epoch).total_microseconds();
1093
 
    }
1094
 
    else
1095
 
    {
1096
 
      t_mark= (_end_timer - _epoch).total_microseconds();
1097
 
    }
1098
 
 
1099
 
    return t_mark; 
1100
 
  }
1101
 
 
1102
 
  // We may need to set user on this
1103
 
  type::Time::epoch_t getCurrentTimestampEpoch() const
1104
 
  { 
1105
 
    if (not _user_time.is_not_a_date_time())
1106
 
      return (_user_time - _epoch).total_seconds();
1107
 
 
1108
 
    return (_start_timer - _epoch).total_seconds();
1109
 
  }
1110
 
 
1111
 
  type::Time::epoch_t getCurrentTimestampEpoch(type::Time::usec_t &fraction_arg) const
1112
 
  { 
1113
 
    if (not _user_time.is_not_a_date_time())
1114
 
    {
1115
 
      fraction_arg= 0;
1116
 
      return (_user_time - _epoch).total_seconds();
1117
 
    }
1118
 
 
1119
 
    fraction_arg= _start_timer.time_of_day().fractional_seconds() % 1000000;
1120
 
    return (_start_timer - _epoch).total_seconds();
1121
 
  }
1122
 
 
1123
 
  uint64_t found_rows(void) const
 
1013
  inline uint64_t getCurrentTimestamp()  
 
1014
  { 
 
1015
    return my_micro_time(); 
 
1016
  }
 
1017
  inline uint64_t found_rows(void)
1124
1018
  {
1125
1019
    return limit_found_rows;
1126
1020
  }
1127
 
 
1128
1021
  /** Returns whether the session is currently inside a transaction */
1129
 
  bool inTransaction() const
 
1022
  inline bool inTransaction()
1130
1023
  {
1131
1024
    return server_status & SERVER_STATUS_IN_TRANS;
1132
1025
  }
 
1026
  inline bool fill_derived_tables()
 
1027
  {
 
1028
    return !lex->only_view_structure();
 
1029
  }
1133
1030
 
1134
1031
  LEX_STRING *make_lex_string(LEX_STRING *lex_str,
1135
1032
                              const char* str, uint32_t length,
1136
1033
                              bool allocate_lex_string);
1137
 
 
1138
1034
  LEX_STRING *make_lex_string(LEX_STRING *lex_str,
1139
1035
                              const std::string &str,
1140
1036
                              bool allocate_lex_string);
1141
1037
 
1142
1038
  int send_explain_fields(select_result *result);
1143
 
 
1144
1039
  /**
1145
1040
    Clear the current error, if any.
1146
1041
    We do not clear is_fatal_error or is_fatal_sub_stmt_error since we
1148
1043
    @todo: To silence an error, one should use Internal_error_handler
1149
1044
    mechanism. In future this function will be removed.
1150
1045
  */
1151
 
  inline void clear_error(bool full= false)
 
1046
  inline void clear_error()
1152
1047
  {
1153
1048
    if (main_da.is_error())
1154
1049
      main_da.reset_diagnostics_area();
1155
 
 
1156
 
    if (full)
1157
 
    {
1158
 
      drizzle_reset_errors(this, true);
1159
 
    }
1160
 
  }
1161
 
 
1162
 
  void clearDiagnostics()
1163
 
  {
1164
 
    main_da.reset_diagnostics_area();
 
1050
    return;
1165
1051
  }
1166
1052
 
1167
1053
  /**
1205
1091
  void end_statement();
1206
1092
  inline int killed_errno() const
1207
1093
  {
1208
 
    killed_state_t killed_val; /* to cache the volatile 'killed' */
1209
 
    return (killed_val= _killed) != KILL_BAD_DATA ? killed_val : 0;
 
1094
    killed_state killed_val; /* to cache the volatile 'killed' */
 
1095
    return (killed_val= killed) != KILL_BAD_DATA ? killed_val : 0;
1210
1096
  }
1211
1097
  void send_kill_message() const;
1212
1098
  /* return true if we will abort query if we make a warning now */
1213
 
  inline bool abortOnWarning()
1214
 
  {
1215
 
    return abort_on_warning;
1216
 
  }
1217
 
 
1218
 
  inline void setAbortOnWarning(bool arg)
1219
 
  {
1220
 
    abort_on_warning= arg;
1221
 
  }
1222
 
 
1223
 
  void setAbort(bool arg);
1224
 
  void lockOnSys();
 
1099
  inline bool really_abort_on_warning()
 
1100
  {
 
1101
    return (abort_on_warning);
 
1102
  }
1225
1103
  void set_status_var_init();
 
1104
  void reset_n_backup_open_tables_state(Open_tables_state *backup);
 
1105
  void restore_backup_open_tables_state(Open_tables_state *backup);
1226
1106
 
1227
1107
  /**
1228
1108
    Set the current database; use deep copy of C-string.
1240
1120
    database usually involves other actions, like switching other database
1241
1121
    attributes including security context. In the future, this operation
1242
1122
    will be made private and more convenient interface will be provided.
 
1123
 
 
1124
    @return Operation status
 
1125
      @retval false Success
 
1126
      @retval true  Out-of-memory error
1243
1127
  */
1244
 
  void set_db(const std::string &new_db);
 
1128
  bool set_db(const char *new_db, size_t new_db_len);
1245
1129
 
1246
1130
  /*
1247
1131
    Copy the current database to the argument. Use the current arena to
1263
1147
    @param level the error level
1264
1148
    @return true if the error is handled
1265
1149
  */
1266
 
  virtual bool handle_error(drizzled::error_t sql_errno, const char *message,
 
1150
  virtual bool handle_error(uint32_t sql_errno, const char *message,
1267
1151
                            DRIZZLE_ERROR::enum_warning_level level);
1268
1152
 
1269
1153
  /**
1291
1175
   * updates any status variables necessary.
1292
1176
   *
1293
1177
   * @param errcode     Error code to print to console
 
1178
   * @param should_lock 1 if we have have to lock LOCK_thread_count
1294
1179
   *
1295
1180
   * @note  For the connection that is doing shutdown, this is called twice
1296
1181
   */
1297
 
  void disconnect(enum error_t errcode= EE_OK);
 
1182
  void disconnect(uint32_t errcode, bool lock);
1298
1183
 
1299
1184
  /**
1300
1185
   * Check if user exists and the password supplied is correct.
1305
1190
   * Current implementation does not depend on that, but future changes
1306
1191
   * should be done with this in mind; 
1307
1192
   *
1308
 
   * @param passwd Scrambled password received from client
1309
 
   * @param db Database name to connect to, may be NULL
 
1193
   * @param  Scrambled password received from client
 
1194
   * @param  Length of scrambled password
 
1195
   * @param  Database name to connect to, may be NULL
1310
1196
   */
1311
 
  bool checkUser(const std::string &passwd, const std::string &db);
 
1197
  bool checkUser(const char *passwd, uint32_t passwd_len, const char *db);
1312
1198
  
1313
1199
  /**
1314
1200
   * Returns the timestamp (in microseconds) of when the Session 
1315
1201
   * connected to the server.
1316
1202
   */
1317
 
  uint64_t getConnectMicroseconds() const
1318
 
  {
1319
 
    return (_connect_time - _epoch).total_microseconds();
1320
 
  }
1321
 
 
1322
 
  uint64_t getConnectSeconds() const
1323
 
  {
1324
 
    return (_connect_time - _epoch).total_seconds();
 
1203
  inline uint64_t getConnectMicroseconds() const
 
1204
  {
 
1205
    return connect_microseconds;
1325
1206
  }
1326
1207
 
1327
1208
  /**
1342
1223
  {
1343
1224
    return statement_message;
1344
1225
  }
1345
 
  
1346
 
  /**
1347
 
   * Returns a pointer to the current Resulset message for this
1348
 
   * Session, or NULL if no active message.
1349
 
   */
1350
 
  message::Resultset *getResultsetMessage() const
1351
 
  {
1352
 
    return resultset;
1353
 
  }
 
1226
 
1354
1227
  /**
1355
1228
   * Sets the active transaction message used by the ReplicationServices
1356
1229
   * component.
1372
1245
  {
1373
1246
    statement_message= in_message;
1374
1247
  }
1375
 
 
1376
 
  /**
1377
 
   * Sets the active Resultset message used by the Query Cache
1378
 
   * plugin.
1379
 
   *
1380
 
   * @param[in] Pointer to the message
1381
 
   */
1382
 
  void setResultsetMessage(message::Resultset *in_message)
1383
 
  {
1384
 
    resultset= in_message;
1385
 
  }
1386
 
  /**
1387
 
   * reset the active Resultset message used by the Query Cache
1388
 
   * plugin.
1389
 
   */
1390
 
 
1391
 
  void resetResultsetMessage()
1392
 
  { 
1393
 
    resultset= NULL;
1394
 
  }
1395
 
 
1396
1248
private:
1397
1249
  /** Pointers to memory managed by the ReplicationServices component */
1398
1250
  message::Transaction *transaction_message;
1399
1251
  message::Statement *statement_message;
1400
 
  /* Pointer to the current resultset of Select query */
1401
 
  message::Resultset *resultset;
1402
 
  plugin::EventObserverList *session_event_observers;
1403
 
  
1404
 
  /* Schema observers are mapped to databases. */
1405
 
  std::map<std::string, plugin::EventObserverList *> schema_event_observers;
1406
 
 
1407
 
 
1408
 
public:
1409
 
  plugin::EventObserverList *getSessionObservers() 
1410
 
  { 
1411
 
    return session_event_observers;
1412
 
  }
1413
 
  
1414
 
  void setSessionObservers(plugin::EventObserverList *observers) 
1415
 
  { 
1416
 
    session_event_observers= observers;
1417
 
  }
1418
 
  
1419
 
  /* For schema event observers there is one set of observers per database. */
1420
 
  plugin::EventObserverList *getSchemaObservers(const std::string &db_name) 
1421
 
  { 
1422
 
    std::map<std::string, plugin::EventObserverList *>::iterator it;
1423
 
    
1424
 
    it= schema_event_observers.find(db_name);
1425
 
    if (it == schema_event_observers.end())
1426
 
      return NULL;
1427
 
      
1428
 
    return it->second;
1429
 
  }
1430
 
  
1431
 
  void setSchemaObservers(const std::string &db_name, plugin::EventObserverList *observers) 
1432
 
  { 
1433
 
    std::map<std::string, plugin::EventObserverList *>::iterator it;
1434
 
 
1435
 
    it= schema_event_observers.find(db_name);
1436
 
    if (it != schema_event_observers.end())
1437
 
      schema_event_observers.erase(it);;
1438
 
 
1439
 
    if (observers)
1440
 
      schema_event_observers[db_name] = observers;
1441
 
  }
1442
 
  
1443
 
  
1444
 
 private:
 
1252
  /** Microsecond timestamp of when Session connected */
 
1253
  uint64_t connect_microseconds;
1445
1254
  const char *proc_info;
1446
1255
 
1447
1256
  /** The current internal error handler for this thread, or NULL. */
1482
1291
   * set to query_id of original query.
1483
1292
   */
1484
1293
  void mark_used_tables_as_free_for_reuse(Table *table);
 
1294
  /**
 
1295
    Mark all temporary tables which were used by the current statement or
 
1296
    substatement as free for reuse, but only if the query_id can be cleared.
 
1297
 
 
1298
    @param session thread context
 
1299
 
 
1300
    @remark For temp tables associated with a open SQL HANDLER the query_id
 
1301
            is not reset until the HANDLER is closed.
 
1302
  */
 
1303
  void mark_temp_tables_as_free_for_reuse();
1485
1304
 
1486
1305
public:
1487
1306
 
1523
1342
  }
1524
1343
  void refresh_status();
1525
1344
  user_var_entry *getVariable(LEX_STRING &name, bool create_if_not_exists);
1526
 
  user_var_entry *getVariable(const std::string  &name, bool create_if_not_exists);
1527
 
  void setVariable(const std::string &name, const std::string &value);
1528
1345
  
1529
1346
  /**
1530
1347
   * Closes all tables used by the current substatement, or all tables
1534
1351
  void close_old_data_files(bool morph_locks= false,
1535
1352
                            bool send_refresh= false);
1536
1353
  void close_open_tables();
1537
 
  void close_data_files_and_morph_locks(const identifier::Table &identifier);
 
1354
  void close_data_files_and_morph_locks(TableIdentifier &identifier);
 
1355
  void close_data_files_and_morph_locks(const char *db, const char *table_name);
1538
1356
 
1539
1357
private:
1540
 
  bool free_cached_table(boost::mutex::scoped_lock &scopedLock);
1541
 
 
 
1358
  bool free_cached_table();
1542
1359
public:
1543
1360
 
1544
1361
  /**
1566
1383
   */
1567
1384
  bool openTablesLock(TableList *tables);
1568
1385
 
 
1386
  /**
 
1387
   * Open all tables in list and process derived tables
 
1388
   *
 
1389
   * @param Pointer to a list of tables for open
 
1390
   * @param Bitmap of flags to modify how the tables will be open:
 
1391
   *        DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
 
1392
   *        done a flush or namelock on it.
 
1393
   *
 
1394
   * @retval
 
1395
   *  false - ok
 
1396
   * @retval
 
1397
   *  true  - error
 
1398
   *
 
1399
   * @note
 
1400
   *
 
1401
   * This is to be used on prepare stage when you don't read any
 
1402
   * data from the tables.
 
1403
   */
 
1404
  bool openTables(TableList *tables, uint32_t flags= 0);
 
1405
 
1569
1406
  int open_tables_from_list(TableList **start, uint32_t *counter, uint32_t flags= 0);
1570
1407
 
1571
1408
  Table *openTableLock(TableList *table_list, thr_lock_type lock_type);
1572
1409
  Table *openTable(TableList *table_list, bool *refresh, uint32_t flags= 0);
1573
1410
 
1574
1411
  void unlink_open_table(Table *find);
1575
 
  void drop_open_table(Table *table, const identifier::Table &identifier);
 
1412
  void drop_open_table(Table *table, TableIdentifier &identifier);
1576
1413
  void close_cached_table(Table *table);
1577
1414
 
1578
1415
  /* Create a lock in the cache */
1579
 
  table::Placeholder *table_cache_insert_placeholder(const identifier::Table &identifier);
1580
 
  bool lock_table_name_if_not_cached(const identifier::Table &identifier, Table **table);
1581
 
 
 
1416
  Table *table_cache_insert_placeholder(const char *key, uint32_t key_length);
 
1417
  bool lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table);
 
1418
  bool lock_table_name_if_not_cached(const char *db,
 
1419
                                     const char *table_name, Table **table);
 
1420
 
 
1421
  typedef drizzled::hash_map<std::string, message::Table> TableMessageCache;
 
1422
  TableMessageCache table_message_cache;
 
1423
 
 
1424
  bool storeTableMessage(TableIdentifier &identifier, message::Table &table_message);
 
1425
  bool removeTableMessage(TableIdentifier &identifier);
 
1426
  bool getTableMessage(TableIdentifier &identifier, message::Table &table_message);
 
1427
  bool doesTableMessageExist(TableIdentifier &identifier);
 
1428
  bool renameTableMessage(TableIdentifier &from, TableIdentifier &to);
 
1429
 
 
1430
  /* Work with temporary tables */
 
1431
  Table *find_temporary_table(TableList *table_list);
 
1432
  Table *find_temporary_table(const char *db, const char *table_name);
 
1433
  Table *find_temporary_table(TableIdentifier &identifier);
 
1434
 
 
1435
  void doGetTableNames(CachedDirectory &directory,
 
1436
                       const std::string& db_name,
 
1437
                       std::set<std::string>& set_of_names);
 
1438
  void doGetTableNames(const std::string& db_name,
 
1439
                       std::set<std::string>& set_of_names);
 
1440
 
 
1441
  int doGetTableDefinition(drizzled::TableIdentifier &identifier,
 
1442
                           message::Table &table_proto);
 
1443
  bool doDoesTableExist(TableIdentifier &identifier);
 
1444
 
 
1445
  void close_temporary_tables();
 
1446
  void close_temporary_table(Table *table);
 
1447
  // The method below just handles the de-allocation of the table. In
 
1448
  // a better memory type world, this would not be needed.
1582
1449
private:
1583
 
  session::TableMessages _table_message_cache;
1584
 
 
 
1450
  void close_temporary(Table *table);
1585
1451
public:
1586
 
  session::TableMessages &getMessageCache()
1587
 
  {
1588
 
    return _table_message_cache;
1589
 
  }
 
1452
 
 
1453
  int drop_temporary_table(TableList *table_list);
 
1454
  bool rm_temporary_table(plugin::StorageEngine *base, const char *path);
 
1455
  bool rm_temporary_table(TableIdentifier &identifier);
 
1456
  Table *open_temporary_table(TableIdentifier &identifier,
 
1457
                              bool link_in_list= true);
1590
1458
 
1591
1459
  /* Reopen operations */
1592
 
  bool reopen_tables();
 
1460
  bool reopen_tables(bool get_locks, bool mark_share_as_old);
 
1461
  bool reopen_name_locked_table(TableList* table_list, bool link_in);
1593
1462
  bool close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders);
1594
1463
 
1595
 
  void wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond);
 
1464
  void wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond);
1596
1465
  int setup_conds(TableList *leaves, COND **conds);
1597
1466
  int lock_tables(TableList *tables, uint32_t count, bool *need_reopen);
1598
1467
 
1599
 
  drizzled::util::Storable *getProperty(const std::string &arg)
1600
 
  {
1601
 
    return life_properties.getProperty(arg);
1602
 
  }
1603
 
 
1604
 
  template<class T>
1605
 
  bool setProperty(const std::string &arg, T *value)
1606
 
  {
1607
 
    life_properties.setProperty(arg, value);
1608
 
 
1609
 
    return true;
1610
 
  }
1611
1468
 
1612
1469
  /**
1613
1470
    Return the default storage engine
1622
1479
    if (variables.storage_engine)
1623
1480
      return variables.storage_engine;
1624
1481
    return global_system_variables.storage_engine;
1625
 
  }
1626
 
 
1627
 
  void get_xid(DrizzleXid *xid); // Innodb only
1628
 
 
1629
 
  table::Singular *getInstanceTable();
1630
 
  table::Singular *getInstanceTable(List<CreateField> &field_list);
1631
 
 
1632
 
private:
1633
 
  bool resetUsage()
1634
 
  {
1635
 
    if (getrusage(RUSAGE_THREAD, &usage))
1636
 
    {
1637
 
      return false;
1638
 
    }
1639
 
 
1640
 
    return true;
1641
 
  }
1642
 
 
1643
 
public:
1644
 
 
1645
 
  void setUsage(bool arg)
1646
 
  {
1647
 
    use_usage= arg;
1648
 
  }
1649
 
 
1650
 
  const struct rusage &getUsage()
1651
 
  {
1652
 
    return usage;
1653
 
  }
1654
 
 
1655
 
  catalog::Instance::const_reference catalog() const
1656
 
  {
1657
 
    return *(_catalog.get());
1658
 
  }
1659
 
 
1660
 
  catalog::Instance::reference catalog()
1661
 
  {
1662
 
    return *(_catalog.get());
1663
 
  }
1664
 
 
1665
 
private:
1666
 
  catalog::Instance::shared_ptr _catalog;
1667
 
 
1668
 
  // This lives throughout the life of Session
1669
 
  bool use_usage;
1670
 
  session::PropertyMap life_properties;
1671
 
  std::vector<table::Singular *> temporary_shares;
1672
 
  struct rusage usage;
 
1482
  };
 
1483
 
 
1484
  static void unlink(Session *session);
 
1485
 
1673
1486
};
1674
1487
 
 
1488
class JOIN;
 
1489
 
1675
1490
#define ESCAPE_CHARS "ntrb0ZN" // keep synchronous with READ_INFO::unescape
1676
1491
 
 
1492
} /* namespace drizzled */
 
1493
 
 
1494
/** @TODO why is this in the middle of the file */
 
1495
#include <drizzled/select_to_file.h>
 
1496
#include <drizzled/select_export.h>
 
1497
#include <drizzled/select_dump.h>
 
1498
#include <drizzled/select_insert.h>
 
1499
#include <drizzled/select_create.h>
 
1500
#include <drizzled/tmp_table_param.h>
 
1501
#include <drizzled/select_union.h>
 
1502
#include <drizzled/select_subselect.h>
 
1503
#include <drizzled/select_singlerow_subselect.h>
 
1504
#include <drizzled/select_max_min_finder_subselect.h>
 
1505
#include <drizzled/select_exists_subselect.h>
 
1506
 
 
1507
namespace drizzled
 
1508
{
 
1509
 
 
1510
/**
 
1511
 * A structure used to describe sort information
 
1512
 * for a field or item used in ORDER BY.
 
1513
 */
 
1514
typedef struct st_sort_field 
 
1515
{
 
1516
  Field *field; /**< Field to sort */
 
1517
  Item  *item; /**< Item if not sorting fields */
 
1518
  size_t length; /**< Length of sort field */
 
1519
  uint32_t suffix_length; /**< Length suffix (0-4) */
 
1520
  Item_result result_type; /**< Type of item */
 
1521
  bool reverse; /**< if descending sort */
 
1522
  bool need_strxnfrm;   /**< If we have to use strxnfrm() */
 
1523
} SORT_FIELD;
 
1524
 
 
1525
typedef struct st_sort_buffer 
 
1526
{
 
1527
  uint32_t index;       /* 0 or 1 */
 
1528
  uint32_t sort_orders;
 
1529
  uint32_t change_pos; /* If sort-fields changed */
 
1530
  char **buff;
 
1531
  SORT_FIELD *sortorder;
 
1532
} SORT_BUFFER;
 
1533
 
 
1534
} /* namespace drizzled */
 
1535
 
 
1536
/** @TODO why is this in the middle of the file */
 
1537
 
 
1538
#include <drizzled/table_ident.h>
 
1539
#include <drizzled/user_var_entry.h>
 
1540
#include <drizzled/unique.h>
 
1541
#include <drizzled/my_var.h>
 
1542
#include <drizzled/select_dumpvar.h>
 
1543
 
 
1544
namespace drizzled
 
1545
{
 
1546
 
1677
1547
/* Bits in sql_command_flags */
1678
1548
 
1679
1549
enum sql_command_flag_bits 
1692
1562
static const std::bitset<CF_BIT_SIZE> CF_SHOW_TABLE_COMMAND(1 << CF_BIT_SHOW_TABLE_COMMAND);
1693
1563
static const std::bitset<CF_BIT_SIZE> CF_WRITE_LOGS_COMMAND(1 << CF_BIT_WRITE_LOGS_COMMAND);
1694
1564
 
1695
 
namespace display  {
1696
 
const std::string &type(drizzled::Session::global_read_lock_t type);
1697
 
size_t max_string_length(drizzled::Session::global_read_lock_t type);
 
1565
/* Functions in sql_class.cc */
 
1566
void add_to_status(system_status_var *to_var, system_status_var *from_var);
1698
1567
 
1699
 
} /* namespace display */
 
1568
void add_diff_to_status(system_status_var *to_var, system_status_var *from_var,
 
1569
                        system_status_var *dec_var);
1700
1570
 
1701
1571
} /* namespace drizzled */
1702
1572