~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.h

  • Committer: Brian Aker
  • Date: 2010-11-11 04:16:59 UTC
  • mto: (1932.2.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 1930.
  • Revision ID: brian@tangent.org-20101111041659-5xb7ymjrasq1520p
Adding in support for EXECUTE to have WITH NO RETURN.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#ifndef DRIZZLED_SESSION_H
22
22
#define DRIZZLED_SESSION_H
23
23
 
24
 
/* Classes in mysql */
25
 
 
26
24
#include "drizzled/plugin.h"
27
 
#include <drizzled/sql_locale.h>
 
25
#include "drizzled/sql_locale.h"
28
26
#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>
 
27
#include "drizzled/cursor.h"
 
28
#include "drizzled/current_session.h"
 
29
#include "drizzled/sql_error.h"
 
30
#include "drizzled/file_exchange.h"
 
31
#include "drizzled/select_result_interceptor.h"
 
32
#include "drizzled/statistics_variables.h"
 
33
#include "drizzled/xid.h"
35
34
#include "drizzled/query_id.h"
36
35
#include "drizzled/named_savepoint.h"
37
36
#include "drizzled/transaction_context.h"
 
37
#include "drizzled/util/storable.h"
 
38
#include "drizzled/my_hash.h"
38
39
 
39
40
#include <netdb.h>
40
41
#include <map>
42
43
#include <bitset>
43
44
#include <deque>
44
45
 
45
 
#include <drizzled/security_context.h>
46
 
#include <drizzled/open_tables_state.h>
47
 
 
48
 
#include <drizzled/internal_error_handler.h>
49
 
#include <drizzled/diagnostics_area.h>
50
 
 
51
 
#include <drizzled/plugin/authorization.h>
 
46
#include "drizzled/internal/getrusage.h"
 
47
#include "drizzled/security_context.h"
 
48
#include "drizzled/open_tables_state.h"
 
49
#include "drizzled/internal_error_handler.h"
 
50
#include "drizzled/diagnostics_area.h"
 
51
#include "drizzled/plugin/authorization.h"
 
52
 
 
53
#include <boost/unordered_map.hpp>
 
54
#include <boost/thread/mutex.hpp>
 
55
#include <boost/thread/shared_mutex.hpp>
 
56
#include <boost/thread/condition_variable.hpp>
52
57
 
53
58
#define MIN_HANDSHAKE_SIZE      6
54
59
 
59
64
{
60
65
class Client;
61
66
class Scheduler;
 
67
class EventObserverList;
62
68
}
 
69
 
63
70
namespace message
64
71
{
65
72
class Transaction;
66
73
class Statement;
 
74
class Resultset;
67
75
}
 
76
 
68
77
namespace internal
69
78
{
70
79
struct st_my_thread_var;
71
80
}
72
81
 
 
82
namespace table
 
83
{
 
84
class Placeholder;
 
85
}
 
86
 
73
87
class Lex_input_stream;
74
88
class user_var_entry;
75
89
class CopyField;
76
90
class Table_ident;
77
91
 
 
92
class TableShareInstance;
 
93
 
78
94
extern char internal_table_name[2];
79
95
extern char empty_c_string[1];
80
96
extern const char **errmesg;
103
119
      of the INSERT ... ON DUPLICATE KEY UPDATE no matter whether the row
104
120
      was actually changed or not.
105
121
*/
106
 
typedef struct st_copy_info 
 
122
class CopyInfo 
107
123
{
 
124
public:
108
125
  ha_rows records; /**< Number of processed records */
109
126
  ha_rows deleted; /**< Number of deleted records */
110
127
  ha_rows updated; /**< Number of updated records */
118
135
  List<Item> *update_fields;
119
136
  List<Item> *update_values;
120
137
  /* 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;
 
138
 
 
139
  CopyInfo() :
 
140
    records(0),
 
141
    deleted(0),
 
142
    updated(0),
 
143
    copied(0),
 
144
    error_count(0),
 
145
    touched(0),
 
146
    escape_char(0),
 
147
    last_errno(0),
 
148
    ignore(0),
 
149
    update_fields(0),
 
150
    update_values(0)
 
151
  { }
 
152
 
 
153
};
130
154
 
131
155
} /* namespace drizzled */
132
156
 
142
166
#define Session_SENTRY_MAGIC 0xfeedd1ff
143
167
#define Session_SENTRY_GONE  0xdeadbeef
144
168
 
145
 
#define Session_CHECK_SENTRY(session) assert(session->dbug_sentry == Session_SENTRY_MAGIC)
146
 
 
147
 
struct system_variables
 
169
struct drizzle_system_variables
148
170
{
149
 
  system_variables() {};
 
171
  drizzle_system_variables()
 
172
  {}
150
173
  /*
151
174
    How dynamically allocated system variables are handled:
152
175
 
186
209
  size_t sortbuff_size;
187
210
  uint32_t thread_handling;
188
211
  uint32_t tx_isolation;
 
212
  size_t transaction_message_threshold;
189
213
  uint32_t completion_type;
190
214
  /* Determines which non-standard SQL behaviour should be enabled */
191
215
  uint32_t sql_mode;
215
239
  Time_zone *time_zone;
216
240
};
217
241
 
218
 
extern struct system_variables global_system_variables;
 
242
extern struct drizzle_system_variables global_system_variables;
219
243
 
220
244
} /* namespace drizzled */
221
245
 
224
248
namespace drizzled
225
249
{
226
250
 
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
251
void mark_transaction_to_rollback(Session *session, bool all);
298
252
 
299
 
extern pthread_mutex_t LOCK_xid_cache;
300
 
extern HASH xid_cache;
301
 
 
302
 
 
303
253
/**
304
254
  Storage engine specific thread local data.
305
255
*/
352
302
 * all member variables that are not critical to non-internal operations of the
353
303
 * session object.
354
304
 */
 
305
typedef int64_t session_id_t;
 
306
 
355
307
class Session : public Open_tables_state
356
308
{
357
309
public:
 
310
  // Plugin storage in Session.
 
311
  typedef boost::unordered_map<std::string, util::Storable *, util::insensitive_hash, util::insensitive_equal_to> PropertyMap;
 
312
 
358
313
  /*
359
314
    MARK_COLUMNS_NONE:  Means mark_used_colums is not set and no indicator to
360
315
                        handler of fields used is set
369
324
  enum enum_mark_columns mark_used_columns;
370
325
  inline void* alloc(size_t size)
371
326
  {
372
 
    return alloc_root(mem_root,size);
 
327
    return mem_root->alloc_root(size);
373
328
  }
374
329
  inline void* calloc(size_t size)
375
330
  {
376
331
    void *ptr;
377
 
    if ((ptr= alloc_root(mem_root,size)))
 
332
    if ((ptr= mem_root->alloc_root(size)))
378
333
      memset(ptr, 0, size);
379
334
    return ptr;
380
335
  }
381
336
  inline char *strdup(const char *str)
382
337
  {
383
 
    return strdup_root(mem_root,str);
 
338
    return mem_root->strdup_root(str);
384
339
  }
385
340
  inline char *strmake(const char *str, size_t size)
386
341
  {
387
 
    return strmake_root(mem_root,str,size);
 
342
    return mem_root->strmake_root(str,size);
388
343
  }
389
344
  inline void *memdup(const void *str, size_t size)
390
345
  {
391
 
    return memdup_root(mem_root,str,size);
 
346
    return mem_root->memdup_root(str, size);
392
347
  }
393
348
  inline void *memdup_w_gap(const void *str, size_t size, uint32_t gap)
394
349
  {
395
350
    void *ptr;
396
 
    if ((ptr= alloc_root(mem_root,size+gap)))
 
351
    if ((ptr= mem_root->alloc_root(size + gap)))
397
352
      memcpy(ptr,str,size);
398
353
    return ptr;
399
354
  }
405
360
   */
406
361
  Item *free_list;
407
362
  memory::Root *mem_root; /**< Pointer to current memroot */
 
363
 
 
364
 
 
365
  memory::Root *getMemRoot()
 
366
  {
 
367
    return mem_root;
 
368
  }
 
369
 
 
370
  uint64_t xa_id;
 
371
 
 
372
  uint64_t getXaId()
 
373
  {
 
374
    return xa_id;
 
375
  }
 
376
 
 
377
  void setXaId(uint64_t in_xa_id)
 
378
  {
 
379
    xa_id= in_xa_id; 
 
380
  }
 
381
 
408
382
  /**
409
383
   * Uniquely identifies each statement object in thread scope; change during
410
384
   * statement lifetime.
413
387
   */
414
388
  uint32_t id;
415
389
  LEX *lex; /**< parse tree descriptor */
 
390
 
 
391
  LEX *getLex() 
 
392
  {
 
393
    return lex;
 
394
  }
416
395
  /** query associated with this statement */
417
396
  std::string query;
418
397
 
429
408
    only responsible for freeing this member.
430
409
  */
431
410
  std::string db;
432
 
 
 
411
  std::string catalog;
 
412
  /* current cache key */
 
413
  std::string query_cache_key;
433
414
  /**
434
415
    Constant for Session::where initialization in the beginning of every query.
435
416
 
440
421
 
441
422
  memory::Root warn_root; /**< Allocation area for warnings and errors */
442
423
  plugin::Client *client; /**< Pointer to client object */
 
424
 
 
425
  void setClient(plugin::Client *client_arg);
 
426
 
 
427
  plugin::Client *getClient()
 
428
  {
 
429
    return client;
 
430
  }
 
431
 
443
432
  plugin::Scheduler *scheduler; /**< Pointer to scheduler object */
444
433
  void *scheduler_arg; /**< Pointer to the optional scheduler argument */
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 */
 
434
private:
 
435
  typedef boost::unordered_map< std::string, user_var_entry *, util::insensitive_hash, util::insensitive_equal_to> UserVars;
 
436
  typedef std::pair< UserVars::iterator, UserVars::iterator > UserVarsRange;
 
437
  UserVars user_vars; /**< Hash of user variables defined during the session's lifetime */
 
438
 
 
439
public:
 
440
  drizzle_system_variables variables; /**< Mutable local variables local to the session */
447
441
  struct system_status_var status_var; /**< Session-local status counters */
448
 
  struct system_status_var *initial_status_var; /* used by show status */
449
442
  THR_LOCK_INFO lock_info; /**< Locking information for this session */
450
443
  THR_LOCK_OWNER main_lock_id; /**< To use for conventional queries */
451
444
  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 */
 
445
private:
 
446
  boost::mutex LOCK_delete; /**< Locked before session is deleted */
 
447
public:
 
448
 
 
449
  void lockForDelete()
 
450
  {
 
451
    LOCK_delete.lock();
 
452
  }
 
453
 
 
454
  void unlockForDelete()
 
455
  {
 
456
    LOCK_delete.unlock();
 
457
  }
453
458
 
454
459
  /**
455
460
   * A peek into the query string for the session. This is a best effort
465
470
 
466
471
private:
467
472
  SecurityContext security_ctx;
 
473
 
 
474
  int32_t scoreboard_index;
 
475
 
 
476
  inline void checkSentry() const
 
477
  {
 
478
    assert(this->dbug_sentry == Session_SENTRY_MAGIC);
 
479
  }
468
480
public:
469
481
  const SecurityContext& getSecurityContext() const
470
482
  {
476
488
    return security_ctx;
477
489
  }
478
490
 
 
491
  int32_t getScoreboardIndex()
 
492
  {
 
493
    return scoreboard_index;
 
494
  }
 
495
 
 
496
  void setScoreboardIndex(int32_t in_scoreboard_index)
 
497
  {
 
498
    scoreboard_index= in_scoreboard_index;
 
499
  }
 
500
 
479
501
  /**
480
502
   * Is this session viewable by the current user?
481
503
   */
499
521
    chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK.
500
522
  */
501
523
  uint32_t dbug_sentry; /**< watch for memory corruption */
 
524
private:
502
525
  internal::st_my_thread_var *mysys_var;
 
526
public:
 
527
 
 
528
  internal::st_my_thread_var *getThreadVar()
 
529
  {
 
530
    return mysys_var;
 
531
  }
 
532
 
 
533
  void resetThreadVar()
 
534
  {
 
535
    mysys_var= NULL;
 
536
  }
503
537
  /**
504
538
   * Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. Set from
505
539
   * first byte of the packet in executeStatement()
538
572
  ResourceContext *getResourceContext(const plugin::MonitoredInTransaction *monitored,
539
573
                                      size_t index= 0);
540
574
 
 
575
  /**
 
576
   * Structure used to manage "statement transactions" and
 
577
   * "normal transactions". In autocommit mode, the normal transaction is
 
578
   * equivalent to the statement transaction.
 
579
   *
 
580
   * Storage engines will be registered here when they participate in
 
581
   * a transaction. No engine is registered more than once.
 
582
   */
541
583
  struct st_transactions {
542
584
    std::deque<NamedSavepoint> savepoints;
543
 
    TransactionContext all; ///< Trans since BEGIN WORK
544
 
    TransactionContext stmt; ///< Trans for current statement
 
585
 
 
586
    /**
 
587
     * The normal transaction (since BEGIN WORK).
 
588
     *
 
589
     * Contains a list of all engines that have participated in any of the
 
590
     * statement transactions started within the context of the normal
 
591
     * transaction.
 
592
     *
 
593
     * @note In autocommit mode, this is empty.
 
594
     */
 
595
    TransactionContext all;
 
596
 
 
597
    /**
 
598
     * The statment transaction.
 
599
     *
 
600
     * Contains a list of all engines participating in the given statement.
 
601
     *
 
602
     * @note In autocommit mode, this will be used to commit/rollback the
 
603
     * normal transaction.
 
604
     */
 
605
    TransactionContext stmt;
 
606
 
545
607
    XID_STATE xid_state;
546
608
 
547
609
    void cleanup()
662
724
    create_sort_index(); may differ from examined_row_count.
663
725
  */
664
726
  uint32_t row_count;
665
 
  pthread_t real_id; /**< For debugging */
666
 
  uint64_t thread_id;
 
727
  session_id_t thread_id;
667
728
  uint32_t tmp_table;
668
 
  uint32_t global_read_lock;
 
729
  enum global_read_lock_t
 
730
  {
 
731
    NONE= 0,
 
732
    GOT_GLOBAL_READ_LOCK= 1,
 
733
    MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT= 2
 
734
  };
 
735
private:
 
736
  global_read_lock_t _global_read_lock;
 
737
 
 
738
public:
 
739
 
 
740
  global_read_lock_t isGlobalReadLock() const
 
741
  {
 
742
    return _global_read_lock;
 
743
  }
 
744
 
 
745
  void setGlobalReadLock(global_read_lock_t arg)
 
746
  {
 
747
    _global_read_lock= arg;
 
748
  }
 
749
 
 
750
  DrizzleLock *lockTables(Table **tables, uint32_t count, uint32_t flags, bool *need_reopen);
 
751
  bool lockGlobalReadLock();
 
752
  bool lock_table_names(TableList *table_list);
 
753
  bool lock_table_names_exclusively(TableList *table_list);
 
754
  bool makeGlobalReadLockBlockCommit();
 
755
  bool abortLockForThread(Table *table);
 
756
  bool wait_if_global_read_lock(bool abort_on_refresh, bool is_not_commit);
 
757
  int lock_table_name(TableList *table_list);
 
758
  void abortLock(Table *table);
 
759
  void removeLock(Table *table);
 
760
  void unlockReadTables(DrizzleLock *sql_lock);
 
761
  void unlockSomeTables(Table **table, uint32_t count);
 
762
  void unlockTables(DrizzleLock *sql_lock);
 
763
  void startWaitingGlobalReadLock();
 
764
  void unlockGlobalReadLock();
 
765
 
 
766
private:
 
767
  int unlock_external(Table **table, uint32_t count);
 
768
  int lock_external(Table **tables, uint32_t count);
 
769
  bool wait_for_locked_table_names(TableList *table_list);
 
770
  DrizzleLock *get_lock_data(Table **table_ptr, uint32_t count,
 
771
                             bool should_lock, Table **write_lock_used);
 
772
public:
 
773
 
669
774
  uint32_t server_status;
670
775
  uint32_t open_options;
671
776
  uint32_t select_number; /**< number of select (used for EXPLAIN) */
673
778
  enum_tx_isolation session_tx_isolation;
674
779
  enum_check_fields count_cuted_fields;
675
780
 
676
 
  enum killed_state
 
781
  enum killed_state_t
677
782
  {
678
783
    NOT_KILLED,
679
784
    KILL_BAD_DATA,
681
786
    KILL_QUERY,
682
787
    KILLED_NO_VALUE /* means none of the above states apply */
683
788
  };
684
 
  killed_state volatile killed;
 
789
private:
 
790
  killed_state_t volatile _killed;
 
791
 
 
792
public:
 
793
 
 
794
  void setKilled(killed_state_t arg)
 
795
  {
 
796
    _killed= arg;
 
797
  }
 
798
 
 
799
  killed_state_t getKilled()
 
800
  {
 
801
    return _killed;
 
802
  }
 
803
 
 
804
  volatile killed_state_t *getKilledPtr() // Do not use this method, it is here for historical convience.
 
805
  {
 
806
    return &_killed;
 
807
  }
685
808
 
686
809
  bool some_tables_deleted;
687
810
  bool no_errors;
805
928
  }
806
929
 
807
930
  /** Accessor method returning the session's ID. */
808
 
  inline uint64_t getSessionId()  const
 
931
  inline session_id_t getSessionId()  const
809
932
  {
810
933
    return thread_id;
811
934
  }
896
1019
   */
897
1020
  void cleanup_after_query();
898
1021
  bool storeGlobals();
899
 
  void awake(Session::killed_state state_to_set);
 
1022
  void awake(Session::killed_state_t state_to_set);
900
1023
  /**
901
1024
   * Pulls thread-specific variables into Session state.
902
1025
   *
986
1109
    enter_cond(); this mutex is then released by exit_cond().
987
1110
    Usage must be: lock mutex; enter_cond(); your code; exit_cond().
988
1111
  */
989
 
  const char* enter_cond(pthread_cond_t *cond, pthread_mutex_t* mutex, const char* msg);
 
1112
  const char* enter_cond(boost::condition_variable_any &cond, boost::mutex &mutex, const char* msg);
990
1113
  void exit_cond(const char* old_msg);
991
1114
 
992
1115
  inline time_t query_start() { return start_time; }
1023
1146
  {
1024
1147
    return server_status & SERVER_STATUS_IN_TRANS;
1025
1148
  }
1026
 
  inline bool fill_derived_tables()
1027
 
  {
1028
 
    return !lex->only_view_structure();
1029
 
  }
1030
 
 
1031
1149
  LEX_STRING *make_lex_string(LEX_STRING *lex_str,
1032
1150
                              const char* str, uint32_t length,
1033
1151
                              bool allocate_lex_string);
1043
1161
    @todo: To silence an error, one should use Internal_error_handler
1044
1162
    mechanism. In future this function will be removed.
1045
1163
  */
1046
 
  inline void clear_error()
 
1164
  inline void clear_error(bool full= false)
1047
1165
  {
1048
1166
    if (main_da.is_error())
1049
1167
      main_da.reset_diagnostics_area();
1050
 
    return;
 
1168
 
 
1169
    if (full)
 
1170
    {
 
1171
      drizzle_reset_errors(this, true);
 
1172
    }
1051
1173
  }
1052
1174
 
1053
1175
  /**
1091
1213
  void end_statement();
1092
1214
  inline int killed_errno() const
1093
1215
  {
1094
 
    killed_state killed_val; /* to cache the volatile 'killed' */
1095
 
    return (killed_val= killed) != KILL_BAD_DATA ? killed_val : 0;
 
1216
    killed_state_t killed_val; /* to cache the volatile 'killed' */
 
1217
    return (killed_val= _killed) != KILL_BAD_DATA ? killed_val : 0;
1096
1218
  }
1097
1219
  void send_kill_message() const;
1098
1220
  /* return true if we will abort query if we make a warning now */
1100
1222
  {
1101
1223
    return (abort_on_warning);
1102
1224
  }
 
1225
 
 
1226
  void setAbort(bool arg);
 
1227
  void lockOnSys();
1103
1228
  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);
1106
1229
 
1107
1230
  /**
1108
1231
    Set the current database; use deep copy of C-string.
1190
1313
   * Current implementation does not depend on that, but future changes
1191
1314
   * should be done with this in mind; 
1192
1315
   *
1193
 
   * @param  Scrambled password received from client
1194
 
   * @param  Length of scrambled password
1195
 
   * @param  Database name to connect to, may be NULL
 
1316
   * @param passwd Scrambled password received from client
 
1317
   * @param db Database name to connect to, may be NULL
1196
1318
   */
1197
 
  bool checkUser(const char *passwd, uint32_t passwd_len, const char *db);
 
1319
  bool checkUser(const std::string &passwd, const std::string &db);
1198
1320
  
1199
1321
  /**
1200
1322
   * Returns the timestamp (in microseconds) of when the Session 
1223
1345
  {
1224
1346
    return statement_message;
1225
1347
  }
1226
 
 
 
1348
  
 
1349
  /**
 
1350
   * Returns a pointer to the current Resulset message for this
 
1351
   * Session, or NULL if no active message.
 
1352
   */
 
1353
  message::Resultset *getResultsetMessage() const
 
1354
  {
 
1355
    return resultset;
 
1356
  }
1227
1357
  /**
1228
1358
   * Sets the active transaction message used by the ReplicationServices
1229
1359
   * component.
1245
1375
  {
1246
1376
    statement_message= in_message;
1247
1377
  }
 
1378
 
 
1379
  /**
 
1380
   * Sets the active Resultset message used by the Query Cache
 
1381
   * plugin.
 
1382
   *
 
1383
   * @param[in] Pointer to the message
 
1384
   */
 
1385
  void setResultsetMessage(message::Resultset *in_message)
 
1386
  {
 
1387
    resultset= in_message;
 
1388
  }
 
1389
  /**
 
1390
   * reset the active Resultset message used by the Query Cache
 
1391
   * plugin.
 
1392
   */
 
1393
 
 
1394
  void resetResultsetMessage()
 
1395
  { 
 
1396
    resultset= NULL;
 
1397
  }
 
1398
 
1248
1399
private:
1249
1400
  /** Pointers to memory managed by the ReplicationServices component */
1250
1401
  message::Transaction *transaction_message;
1251
1402
  message::Statement *statement_message;
1252
 
  /** Microsecond timestamp of when Session connected */
 
1403
  /* Pointer to the current resultset of Select query */
 
1404
  message::Resultset *resultset;
 
1405
  plugin::EventObserverList *session_event_observers;
 
1406
  
 
1407
  /* Schema observers are mapped to databases. */
 
1408
  std::map<std::string, plugin::EventObserverList *> schema_event_observers;
 
1409
 
 
1410
 
 
1411
public:
 
1412
  plugin::EventObserverList *getSessionObservers() 
 
1413
  { 
 
1414
    return session_event_observers;
 
1415
  }
 
1416
  
 
1417
  void setSessionObservers(plugin::EventObserverList *observers) 
 
1418
  { 
 
1419
    session_event_observers= observers;
 
1420
  }
 
1421
  
 
1422
  /* For schema event observers there is one set of observers per database. */
 
1423
  plugin::EventObserverList *getSchemaObservers(const std::string &db_name) 
 
1424
  { 
 
1425
    std::map<std::string, plugin::EventObserverList *>::iterator it;
 
1426
    
 
1427
    it= schema_event_observers.find(db_name);
 
1428
    if (it == schema_event_observers.end())
 
1429
      return NULL;
 
1430
      
 
1431
    return it->second;
 
1432
  }
 
1433
  
 
1434
  void setSchemaObservers(const std::string &db_name, plugin::EventObserverList *observers) 
 
1435
  { 
 
1436
    std::map<std::string, plugin::EventObserverList *>::iterator it;
 
1437
 
 
1438
    it= schema_event_observers.find(db_name);
 
1439
    if (it != schema_event_observers.end())
 
1440
      schema_event_observers.erase(it);;
 
1441
 
 
1442
    if (observers)
 
1443
      schema_event_observers[db_name] = observers;
 
1444
  }
 
1445
  
 
1446
  
 
1447
 private:
 
1448
 /** Microsecond timestamp of when Session connected */
1253
1449
  uint64_t connect_microseconds;
1254
1450
  const char *proc_info;
1255
1451
 
1342
1538
  }
1343
1539
  void refresh_status();
1344
1540
  user_var_entry *getVariable(LEX_STRING &name, bool create_if_not_exists);
 
1541
  user_var_entry *getVariable(const std::string  &name, bool create_if_not_exists);
 
1542
  void setVariable(const std::string &name, const std::string &value);
1345
1543
  
1346
1544
  /**
1347
1545
   * Closes all tables used by the current substatement, or all tables
1352
1550
                            bool send_refresh= false);
1353
1551
  void close_open_tables();
1354
1552
  void close_data_files_and_morph_locks(TableIdentifier &identifier);
1355
 
  void close_data_files_and_morph_locks(const char *db, const char *table_name);
1356
1553
 
1357
1554
private:
1358
1555
  bool free_cached_table();
1383
1580
   */
1384
1581
  bool openTablesLock(TableList *tables);
1385
1582
 
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
 
 
1406
1583
  int open_tables_from_list(TableList **start, uint32_t *counter, uint32_t flags= 0);
1407
1584
 
1408
1585
  Table *openTableLock(TableList *table_list, thr_lock_type lock_type);
1413
1590
  void close_cached_table(Table *table);
1414
1591
 
1415
1592
  /* Create a lock in the cache */
1416
 
  Table *table_cache_insert_placeholder(const char *key, uint32_t key_length);
 
1593
  table::Placeholder *table_cache_insert_placeholder(const TableIdentifier &identifier);
1417
1594
  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
1595
 
1421
 
  typedef drizzled::hash_map<std::string, message::Table> TableMessageCache;
 
1596
  typedef boost::unordered_map<std::string, message::Table, util::insensitive_hash, util::insensitive_equal_to> TableMessageCache;
1422
1597
  TableMessageCache table_message_cache;
1423
1598
 
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);
 
1599
  bool storeTableMessage(const TableIdentifier &identifier, message::Table &table_message);
 
1600
  bool removeTableMessage(const TableIdentifier &identifier);
 
1601
  bool getTableMessage(const TableIdentifier &identifier, message::Table &table_message);
 
1602
  bool doesTableMessageExist(const TableIdentifier &identifier);
 
1603
  bool renameTableMessage(const TableIdentifier &from, const TableIdentifier &to);
1429
1604
 
1430
1605
  /* 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);
 
1606
  Table *find_temporary_table(const TableIdentifier &identifier);
1434
1607
 
1435
1608
  void doGetTableNames(CachedDirectory &directory,
1436
 
                       SchemaIdentifier &schema_identifier,
1437
 
                       std::set<std::string>& set_of_names);
1438
 
  void doGetTableNames(SchemaIdentifier &schema_identifier,
1439
 
                       std::set<std::string>& set_of_names);
1440
 
 
1441
 
  int doGetTableDefinition(drizzled::TableIdentifier &identifier,
 
1609
                       const SchemaIdentifier &schema_identifier,
 
1610
                       std::set<std::string>& set_of_names);
 
1611
  void doGetTableNames(const SchemaIdentifier &schema_identifier,
 
1612
                       std::set<std::string>& set_of_names);
 
1613
 
 
1614
  void doGetTableIdentifiers(CachedDirectory &directory,
 
1615
                             const SchemaIdentifier &schema_identifier,
 
1616
                             TableIdentifiers &set_of_identifiers);
 
1617
  void doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
 
1618
                             TableIdentifiers &set_of_identifiers);
 
1619
 
 
1620
  int doGetTableDefinition(const drizzled::TableIdentifier &identifier,
1442
1621
                           message::Table &table_proto);
1443
 
  bool doDoesTableExist(TableIdentifier &identifier);
 
1622
  bool doDoesTableExist(const drizzled::TableIdentifier &identifier);
1444
1623
 
 
1624
private:
1445
1625
  void close_temporary_tables();
 
1626
public:
1446
1627
  void close_temporary_table(Table *table);
1447
1628
  // The method below just handles the de-allocation of the table. In
1448
1629
  // a better memory type world, this would not be needed.
1451
1632
public:
1452
1633
 
1453
1634
  void dumpTemporaryTableNames(const char *id);
1454
 
  int drop_temporary_table(TableList *table_list);
 
1635
  int drop_temporary_table(const drizzled::TableIdentifier &identifier);
1455
1636
  bool rm_temporary_table(plugin::StorageEngine *base, TableIdentifier &identifier);
1456
 
  bool rm_temporary_table(TableIdentifier &identifier);
 
1637
  bool rm_temporary_table(TableIdentifier &identifier, bool best_effort= false);
1457
1638
  Table *open_temporary_table(TableIdentifier &identifier,
1458
1639
                              bool link_in_list= true);
1459
1640
 
1460
1641
  /* Reopen operations */
1461
1642
  bool reopen_tables(bool get_locks, bool mark_share_as_old);
1462
 
  bool reopen_name_locked_table(TableList* table_list, bool link_in);
1463
1643
  bool close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders);
1464
1644
 
1465
 
  void wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond);
 
1645
  void wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond);
1466
1646
  int setup_conds(TableList *leaves, COND **conds);
1467
1647
  int lock_tables(TableList *tables, uint32_t count, bool *need_reopen);
1468
1648
 
 
1649
  drizzled::util::Storable *getProperty(const std::string &arg)
 
1650
  {
 
1651
    return life_properties[arg];
 
1652
  }
 
1653
 
 
1654
  template<class T>
 
1655
  bool setProperty(const std::string &arg, T *value)
 
1656
  {
 
1657
    life_properties[arg]= value;
 
1658
 
 
1659
    return true;
 
1660
  }
1469
1661
 
1470
1662
  /**
1471
1663
    Return the default storage engine
1480
1672
    if (variables.storage_engine)
1481
1673
      return variables.storage_engine;
1482
1674
    return global_system_variables.storage_engine;
1483
 
  };
 
1675
  }
1484
1676
 
1485
1677
  static void unlink(Session *session);
1486
1678
 
 
1679
  void get_xid(DRIZZLE_XID *xid); // Innodb only
 
1680
 
 
1681
  table::Instance *getInstanceTable();
 
1682
  table::Instance *getInstanceTable(List<CreateField> &field_list);
 
1683
 
 
1684
private:
 
1685
  bool resetUsage()
 
1686
  {
 
1687
    if (getrusage(RUSAGE_THREAD, &usage))
 
1688
    {
 
1689
      return false;
 
1690
    }
 
1691
 
 
1692
    return true;
 
1693
  }
 
1694
public:
 
1695
 
 
1696
  void setUsage(bool arg)
 
1697
  {
 
1698
    use_usage= arg;
 
1699
  }
 
1700
 
 
1701
  const struct rusage &getUsage()
 
1702
  {
 
1703
    return usage;
 
1704
  }
 
1705
 
 
1706
private:
 
1707
  // This lives throughout the life of Session
 
1708
  bool use_usage;
 
1709
  PropertyMap life_properties;
 
1710
  std::vector<table::Instance *> temporary_shares;
 
1711
  struct rusage usage;
1487
1712
};
1488
1713
 
1489
 
class JOIN;
 
1714
class Join;
1490
1715
 
1491
1716
#define ESCAPE_CHARS "ntrb0ZN" // keep synchronous with READ_INFO::unescape
1492
1717
 
1512
1737
 * A structure used to describe sort information
1513
1738
 * for a field or item used in ORDER BY.
1514
1739
 */
1515
 
typedef struct st_sort_field 
 
1740
class SortField 
1516
1741
{
 
1742
public:
1517
1743
  Field *field; /**< Field to sort */
1518
1744
  Item  *item; /**< Item if not sorting fields */
1519
1745
  size_t length; /**< Length of sort field */
1521
1747
  Item_result result_type; /**< Type of item */
1522
1748
  bool reverse; /**< if descending sort */
1523
1749
  bool need_strxnfrm;   /**< If we have to use strxnfrm() */
1524
 
} SORT_FIELD;
1525
 
 
1526
 
typedef struct st_sort_buffer 
1527
 
{
1528
 
  uint32_t index;       /* 0 or 1 */
1529
 
  uint32_t sort_orders;
1530
 
  uint32_t change_pos; /* If sort-fields changed */
1531
 
  char **buff;
1532
 
  SORT_FIELD *sortorder;
1533
 
} SORT_BUFFER;
 
1750
 
 
1751
  SortField() :
 
1752
    field(0),
 
1753
    item(0),
 
1754
    length(0),
 
1755
    suffix_length(0),
 
1756
    result_type(STRING_RESULT),
 
1757
    reverse(0),
 
1758
    need_strxnfrm(0)
 
1759
  { }
 
1760
 
 
1761
};
1534
1762
 
1535
1763
} /* namespace drizzled */
1536
1764
 
1539
1767
#include <drizzled/table_ident.h>
1540
1768
#include <drizzled/user_var_entry.h>
1541
1769
#include <drizzled/unique.h>
1542
 
#include <drizzled/my_var.h>
 
1770
#include <drizzled/var.h>
1543
1771
#include <drizzled/select_dumpvar.h>
1544
1772
 
1545
1773
namespace drizzled
1563
1791
static const std::bitset<CF_BIT_SIZE> CF_SHOW_TABLE_COMMAND(1 << CF_BIT_SHOW_TABLE_COMMAND);
1564
1792
static const std::bitset<CF_BIT_SIZE> CF_WRITE_LOGS_COMMAND(1 << CF_BIT_WRITE_LOGS_COMMAND);
1565
1793
 
1566
 
/* Functions in sql_class.cc */
1567
 
void add_to_status(system_status_var *to_var, system_status_var *from_var);
1568
 
 
1569
 
void add_diff_to_status(system_status_var *to_var, system_status_var *from_var,
1570
 
                        system_status_var *dec_var);
1571
 
 
1572
1794
} /* namespace drizzled */
1573
1795
 
1574
1796
#endif /* DRIZZLED_SESSION_H */