~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.h

  • Committer: Brian Aker
  • Date: 2010-08-12 17:21:13 UTC
  • mfrom: (1689.5.5 remove_pthread_calls)
  • Revision ID: brian@tangent.org-20100812172113-i0ox868kcbzjim2h
Merge in removal of pthread dead wrappers.

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
21
21
#ifndef DRIZZLED_SESSION_H
22
22
#define DRIZZLED_SESSION_H
23
23
 
 
24
/* Classes in mysql */
 
25
 
24
26
#include "drizzled/plugin.h"
25
 
#include "drizzled/sql_locale.h"
 
27
#include <drizzled/sql_locale.h>
26
28
#include "drizzled/resource_context.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"
 
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/statistics_variables.h>
 
35
#include <drizzled/xid.h>
34
36
#include "drizzled/query_id.h"
35
37
#include "drizzled/named_savepoint.h"
36
38
#include "drizzled/transaction_context.h"
37
 
#include "drizzled/util/storable.h"
 
39
 
38
40
#include "drizzled/my_hash.h"
39
 
#include "drizzled/pthread_globals.h"
 
41
 
40
42
#include <netdb.h>
41
 
#include <sys/time.h>
42
 
#include <sys/resource.h>
43
 
 
44
 
#include <algorithm>
 
43
#include <map>
 
44
#include <string>
45
45
#include <bitset>
46
46
#include <deque>
47
 
#include <map>
48
 
#include <string>
49
 
 
50
 
#include "drizzled/identifier.h"
51
 
#include "drizzled/open_tables_state.h"
52
 
#include "drizzled/internal_error_handler.h"
53
 
#include "drizzled/diagnostics_area.h"
54
 
#include "drizzled/plugin/authorization.h"
 
47
 
 
48
#include <drizzled/security_context.h>
 
49
#include <drizzled/open_tables_state.h>
 
50
 
 
51
#include <drizzled/internal_error_handler.h>
 
52
#include <drizzled/diagnostics_area.h>
 
53
 
 
54
#include <drizzled/plugin/authorization.h>
55
55
 
56
56
#include <boost/unordered_map.hpp>
57
 
 
58
 
#include <boost/thread/thread.hpp>
59
57
#include <boost/thread/mutex.hpp>
60
 
#include <boost/thread/shared_mutex.hpp>
61
 
#include <boost/thread/condition_variable.hpp>
62
 
#include <boost/make_shared.hpp>
63
 
 
64
58
 
65
59
#define MIN_HANDSHAKE_SIZE      6
66
60
 
78
72
{
79
73
class Transaction;
80
74
class Statement;
81
 
class Resultset;
82
75
}
83
 
 
84
76
namespace internal
85
77
{
86
78
struct st_my_thread_var;
87
79
}
88
80
 
89
 
namespace table
90
 
{
91
 
class Placeholder;
92
 
}
93
 
 
94
81
class Lex_input_stream;
95
82
class user_var_entry;
96
83
class CopyField;
126
113
      of the INSERT ... ON DUPLICATE KEY UPDATE no matter whether the row
127
114
      was actually changed or not.
128
115
*/
129
 
class CopyInfo 
 
116
typedef struct st_copy_info 
130
117
{
131
 
public:
132
118
  ha_rows records; /**< Number of processed records */
133
119
  ha_rows deleted; /**< Number of deleted records */
134
120
  ha_rows updated; /**< Number of updated records */
142
128
  List<Item> *update_fields;
143
129
  List<Item> *update_values;
144
130
  /* for VIEW ... WITH CHECK OPTION */
145
 
 
146
 
  CopyInfo() :
147
 
    records(0),
148
 
    deleted(0),
149
 
    updated(0),
150
 
    copied(0),
151
 
    error_count(0),
152
 
    touched(0),
153
 
    escape_char(0),
154
 
    last_errno(0),
155
 
    ignore(0),
156
 
    update_fields(0),
157
 
    update_values(0)
158
 
  { }
159
 
 
160
 
};
 
131
} COPY_INFO;
 
132
 
 
133
typedef struct drizzled_lock_st
 
134
{
 
135
  Table **table;
 
136
  uint32_t table_count;
 
137
  uint32_t lock_count;
 
138
  THR_LOCK_DATA **locks;
 
139
} DRIZZLE_LOCK;
161
140
 
162
141
} /* namespace drizzled */
163
142
 
173
152
#define Session_SENTRY_MAGIC 0xfeedd1ff
174
153
#define Session_SENTRY_GONE  0xdeadbeef
175
154
 
176
 
struct drizzle_system_variables
 
155
struct system_variables
177
156
{
178
 
  drizzle_system_variables()
179
 
  {}
 
157
  system_variables() {};
180
158
  /*
181
159
    How dynamically allocated system variables are handled:
182
160
 
213
191
  uint64_t preload_buff_size;
214
192
  uint32_t read_buff_size;
215
193
  uint32_t read_rnd_buff_size;
216
 
  bool replicate_query;
217
194
  size_t sortbuff_size;
218
195
  uint32_t thread_handling;
219
196
  uint32_t tx_isolation;
220
 
  size_t transaction_message_threshold;
221
197
  uint32_t completion_type;
222
198
  /* Determines which non-standard SQL behaviour should be enabled */
223
199
  uint32_t sql_mode;
247
223
  Time_zone *time_zone;
248
224
};
249
225
 
250
 
extern struct drizzle_system_variables global_system_variables;
 
226
extern struct system_variables global_system_variables;
251
227
 
252
228
} /* namespace drizzled */
253
229
 
310
286
 * all member variables that are not critical to non-internal operations of the
311
287
 * session object.
312
288
 */
313
 
typedef int64_t session_id_t;
314
 
 
315
289
class Session : public Open_tables_state
316
290
{
317
291
public:
318
 
  // Plugin storage in Session.
319
 
  typedef boost::unordered_map<std::string, util::Storable *, util::insensitive_hash, util::insensitive_equal_to> PropertyMap;
320
 
  typedef Session* Ptr;
321
 
  typedef boost::shared_ptr<Session> shared_ptr;
322
 
 
323
292
  /*
324
293
    MARK_COLUMNS_NONE:  Means mark_used_colums is not set and no indicator to
325
294
                        handler of fields used is set
376
345
  {
377
346
    return mem_root;
378
347
  }
379
 
 
380
 
  uint64_t xa_id;
381
 
 
382
 
  uint64_t getXaId()
383
 
  {
384
 
    return xa_id;
385
 
  }
386
 
 
387
 
  void setXaId(uint64_t in_xa_id)
388
 
  {
389
 
    xa_id= in_xa_id; 
390
 
  }
391
 
 
392
348
  /**
393
349
   * Uniquely identifies each statement object in thread scope; change during
394
350
   * statement lifetime.
397
353
   */
398
354
  uint32_t id;
399
355
  LEX *lex; /**< parse tree descriptor */
400
 
 
401
 
  LEX *getLex() 
402
 
  {
403
 
    return lex;
404
 
  }
405
356
  /** query associated with this statement */
406
 
  typedef boost::shared_ptr<const std::string> QueryString;
407
 
private:
408
 
  boost::shared_ptr<std::string> query;
409
 
 
410
 
  // Never allow for a modification of this outside of the class. c_str()
411
 
  // requires under some setup non const, you must copy the QueryString in
412
 
  // order to use it.
413
 
public:
414
 
  QueryString getQueryString() const
415
 
  {
416
 
    return query;
417
 
  }
418
 
 
419
 
  void resetQueryString()
420
 
  {
421
 
    query.reset();
422
 
    _state.reset();
423
 
  }
424
 
 
425
 
  /*
426
 
    We need to copy the lock on the string in order to make sure we have a stable string.
427
 
    Once this is done we can use it to build a const char* which can be handed off for
428
 
    a method to use (Innodb is currently the only engine using this).
429
 
  */
430
 
  const char *getQueryStringCopy(size_t &length)
431
 
  {
432
 
    QueryString tmp_string(getQueryString());
433
 
 
434
 
    if (not tmp_string)
435
 
    {
436
 
      length= 0;
437
 
      return NULL;
438
 
    }
439
 
 
440
 
    length= tmp_string->length();
441
 
    char *to_return= strmake(tmp_string->c_str(), tmp_string->length());
442
 
    return to_return;
443
 
  }
444
 
 
445
 
  class State {
446
 
    std::vector <char> _query;
447
 
 
448
 
  public:
449
 
    typedef boost::shared_ptr<State> const_shared_ptr;
450
 
 
451
 
    State(const char *in_packet, size_t in_packet_length)
452
 
    {
453
 
      if (in_packet_length)
454
 
      {
455
 
        size_t minimum= std::min(in_packet_length, static_cast<size_t>(PROCESS_LIST_WIDTH));
456
 
        _query.resize(minimum + 1);
457
 
        memcpy(&_query[0], in_packet, minimum);
458
 
      }
459
 
      else
460
 
      {
461
 
        _query.resize(0);
462
 
      }
463
 
    }
464
 
 
465
 
    const char *query() const
466
 
    {
467
 
      if (_query.size())
468
 
        return &_query[0];
469
 
 
470
 
      return "";
471
 
    }
472
 
 
473
 
    const char *query(size_t &size) const
474
 
    {
475
 
      if (_query.size())
476
 
      {
477
 
        size= _query.size() -1;
478
 
        return &_query[0];
479
 
      }
480
 
 
481
 
      size= 0;
482
 
      return "";
483
 
    }
484
 
  protected:
485
 
    friend class Session;
486
 
    typedef boost::shared_ptr<State> shared_ptr;
487
 
  };
488
 
private:
489
 
  State::shared_ptr  _state; 
490
 
public:
491
 
 
492
 
  State::const_shared_ptr state()
493
 
  {
494
 
    return _state;
495
 
  }
 
357
  std::string query;
496
358
 
497
359
  /**
498
360
    Name of the current (default) database.
506
368
    the Session of that thread); that thread is (and must remain, for now) the
507
369
    only responsible for freeing this member.
508
370
  */
509
 
private:
510
 
  util::string::shared_ptr _schema;
511
 
public:
512
 
 
513
 
  util::string::const_shared_ptr schema() const
514
 
  {
515
 
    if (_schema)
516
 
      return _schema;
517
 
 
518
 
    return util::string::const_shared_ptr(new std::string(""));
519
 
  }
520
 
  std::string catalog;
521
 
  /* current cache key */
522
 
  std::string query_cache_key;
 
371
  std::string db;
 
372
 
523
373
  /**
524
374
    Constant for Session::where initialization in the beginning of every query.
525
375
 
530
380
 
531
381
  memory::Root warn_root; /**< Allocation area for warnings and errors */
532
382
  plugin::Client *client; /**< Pointer to client object */
533
 
 
534
 
  void setClient(plugin::Client *client_arg);
535
 
 
536
 
  plugin::Client *getClient()
537
 
  {
538
 
    return client;
539
 
  }
540
 
 
541
383
  plugin::Scheduler *scheduler; /**< Pointer to scheduler object */
542
384
  void *scheduler_arg; /**< Pointer to the optional scheduler argument */
543
 
 
 
385
private:
544
386
  typedef boost::unordered_map< std::string, user_var_entry *, util::insensitive_hash, util::insensitive_equal_to> UserVars;
545
 
private:
546
387
  typedef std::pair< UserVars::iterator, UserVars::iterator > UserVarsRange;
547
388
  UserVars user_vars; /**< Hash of user variables defined during the session's lifetime */
548
389
 
549
390
public:
550
 
 
551
 
  const UserVars &getUserVariables() const
552
 
  {
553
 
    return user_vars;
554
 
  }
555
 
 
556
 
  drizzle_system_variables variables; /**< Mutable local variables local to the session */
 
391
  struct system_variables variables; /**< Mutable local variables local to the session */
557
392
  struct system_status_var status_var; /**< Session-local status counters */
558
393
  THR_LOCK_INFO lock_info; /**< Locking information for this session */
559
394
  THR_LOCK_OWNER main_lock_id; /**< To use for conventional queries */
560
395
  THR_LOCK_OWNER *lock_id; /**< If not main_lock_id, points to the lock_id of a cursor. */
 
396
private:
 
397
  boost::mutex LOCK_delete; /**< Locked before session is deleted */
 
398
public:
 
399
 
 
400
  void lockForDelete()
 
401
  {
 
402
    LOCK_delete.lock();
 
403
  }
 
404
 
 
405
  void unlockForDelete()
 
406
  {
 
407
    LOCK_delete.unlock();
 
408
  }
 
409
 
 
410
  /**
 
411
   * A peek into the query string for the session. This is a best effort
 
412
   * delivery, there is no guarantee whether the content is meaningful.
 
413
   */
 
414
  char process_list_info[PROCESS_LIST_WIDTH+1];
561
415
 
562
416
  /**
563
417
   * A pointer to the stack frame of the scheduler thread
566
420
  char *thread_stack;
567
421
 
568
422
private:
569
 
  identifier::User::shared_ptr security_ctx;
 
423
  SecurityContext security_ctx;
570
424
 
571
425
  int32_t scoreboard_index;
572
426
 
575
429
    assert(this->dbug_sentry == Session_SENTRY_MAGIC);
576
430
  }
577
431
public:
578
 
  identifier::User::const_shared_ptr user() const
 
432
  const SecurityContext& getSecurityContext() const
579
433
  {
580
 
    if (security_ctx)
581
 
      return security_ctx;
582
 
 
583
 
    return identifier::User::const_shared_ptr();
 
434
    return security_ctx;
584
435
  }
585
436
 
586
 
  void setUser(identifier::User::shared_ptr arg)
 
437
  SecurityContext& getSecurityContext()
587
438
  {
588
 
    security_ctx= arg;
 
439
    return security_ctx;
589
440
  }
590
441
 
591
442
  int32_t getScoreboardIndex()
601
452
  /**
602
453
   * Is this session viewable by the current user?
603
454
   */
604
 
  bool isViewable() const;
 
455
  bool isViewable() const
 
456
  {
 
457
    return plugin::Authorization::isAuthorized(current_session->getSecurityContext(),
 
458
                                               this,
 
459
                                               false);
 
460
  }
605
461
 
606
462
  /**
607
463
    Used in error messages to tell user in what part of MySQL we found an
616
472
    chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK.
617
473
  */
618
474
  uint32_t dbug_sentry; /**< watch for memory corruption */
619
 
private:
620
 
  boost::thread::id boost_thread_id;
621
 
  boost_thread_shared_ptr _thread;
622
 
  boost::this_thread::disable_interruption *interrupt;
623
 
 
624
475
  internal::st_my_thread_var *mysys_var;
625
 
public:
626
 
 
627
 
  boost_thread_shared_ptr &getThread()
628
 
  {
629
 
    return _thread;
630
 
  }
631
 
 
632
 
  void pushInterrupt(boost::this_thread::disable_interruption *interrupt_arg)
633
 
  {
634
 
    interrupt= interrupt_arg;
635
 
  }
636
 
 
637
 
  boost::this_thread::disable_interruption &getThreadInterupt()
638
 
  {
639
 
    assert(interrupt);
640
 
    return *interrupt;
641
 
  }
642
 
 
643
 
  internal::st_my_thread_var *getThreadVar()
644
 
  {
645
 
    return mysys_var;
646
 
  }
647
 
 
648
476
  /**
649
477
   * Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. Set from
650
478
   * first byte of the packet in executeStatement()
683
511
  ResourceContext *getResourceContext(const plugin::MonitoredInTransaction *monitored,
684
512
                                      size_t index= 0);
685
513
 
686
 
  /**
687
 
   * Structure used to manage "statement transactions" and
688
 
   * "normal transactions". In autocommit mode, the normal transaction is
689
 
   * equivalent to the statement transaction.
690
 
   *
691
 
   * Storage engines will be registered here when they participate in
692
 
   * a transaction. No engine is registered more than once.
693
 
   */
694
514
  struct st_transactions {
695
515
    std::deque<NamedSavepoint> savepoints;
696
 
 
697
 
    /**
698
 
     * The normal transaction (since BEGIN WORK).
699
 
     *
700
 
     * Contains a list of all engines that have participated in any of the
701
 
     * statement transactions started within the context of the normal
702
 
     * transaction.
703
 
     *
704
 
     * @note In autocommit mode, this is empty.
705
 
     */
706
 
    TransactionContext all;
707
 
 
708
 
    /**
709
 
     * The statment transaction.
710
 
     *
711
 
     * Contains a list of all engines participating in the given statement.
712
 
     *
713
 
     * @note In autocommit mode, this will be used to commit/rollback the
714
 
     * normal transaction.
715
 
     */
716
 
    TransactionContext stmt;
717
 
 
 
516
    TransactionContext all; ///< Trans since BEGIN WORK
 
517
    TransactionContext stmt; ///< Trans for current statement
718
518
    XID_STATE xid_state;
719
519
 
720
520
    void cleanup()
732
532
  Field *dup_field;
733
533
  sigset_t signals;
734
534
 
735
 
  // As of right now we do not allow a concurrent execute to launch itself
736
 
private:
737
 
  bool concurrent_execute_allowed;
738
 
public:
739
 
 
740
 
  void setConcurrentExecute(bool arg)
741
 
  {
742
 
    concurrent_execute_allowed= arg;
743
 
  }
744
 
 
745
 
  bool isConcurrentExecuteAllowed() const
746
 
  {
747
 
    return concurrent_execute_allowed;
748
 
  }
749
 
 
750
535
  /* Tells if LAST_INSERT_ID(#) was called for the current statement */
751
536
  bool arg_of_last_insert_id_function;
752
 
 
753
537
  /*
754
538
    ALL OVER THIS FILE, "insert_id" means "*automatically generated* value for
755
539
    insertion into an auto_increment column".
851
635
    create_sort_index(); may differ from examined_row_count.
852
636
  */
853
637
  uint32_t row_count;
854
 
  session_id_t thread_id;
 
638
  pthread_t real_id; /**< For debugging */
 
639
  uint64_t thread_id;
855
640
  uint32_t tmp_table;
856
 
  enum global_read_lock_t
857
 
  {
858
 
    NONE= 0,
859
 
    GOT_GLOBAL_READ_LOCK= 1,
860
 
    MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT= 2
861
 
  };
862
 
private:
863
 
  global_read_lock_t _global_read_lock;
864
 
 
865
 
public:
866
 
 
867
 
  global_read_lock_t isGlobalReadLock() const
868
 
  {
869
 
    return _global_read_lock;
870
 
  }
871
 
 
872
 
  void setGlobalReadLock(global_read_lock_t arg)
873
 
  {
874
 
    _global_read_lock= arg;
875
 
  }
876
 
 
877
 
  DrizzleLock *lockTables(Table **tables, uint32_t count, uint32_t flags, bool *need_reopen);
878
 
  bool lockGlobalReadLock();
879
 
  bool lock_table_names(TableList *table_list);
880
 
  bool lock_table_names_exclusively(TableList *table_list);
881
 
  bool makeGlobalReadLockBlockCommit();
882
 
  bool abortLockForThread(Table *table);
883
 
  bool wait_if_global_read_lock(bool abort_on_refresh, bool is_not_commit);
884
 
  int lock_table_name(TableList *table_list);
885
 
  void abortLock(Table *table);
886
 
  void removeLock(Table *table);
887
 
  void unlockReadTables(DrizzleLock *sql_lock);
888
 
  void unlockSomeTables(Table **table, uint32_t count);
889
 
  void unlockTables(DrizzleLock *sql_lock);
890
 
  void startWaitingGlobalReadLock();
891
 
  void unlockGlobalReadLock();
892
 
 
893
 
private:
894
 
  int unlock_external(Table **table, uint32_t count);
895
 
  int lock_external(Table **tables, uint32_t count);
896
 
  bool wait_for_locked_table_names(TableList *table_list);
897
 
  DrizzleLock *get_lock_data(Table **table_ptr, uint32_t count,
898
 
                             bool should_lock, Table **write_lock_used);
899
 
public:
900
 
 
 
641
  uint32_t global_read_lock;
901
642
  uint32_t server_status;
902
643
  uint32_t open_options;
903
644
  uint32_t select_number; /**< number of select (used for EXPLAIN) */
905
646
  enum_tx_isolation session_tx_isolation;
906
647
  enum_check_fields count_cuted_fields;
907
648
 
908
 
  enum killed_state_t
 
649
  enum killed_state
909
650
  {
910
651
    NOT_KILLED,
911
652
    KILL_BAD_DATA,
913
654
    KILL_QUERY,
914
655
    KILLED_NO_VALUE /* means none of the above states apply */
915
656
  };
916
 
private:
917
 
  killed_state_t volatile _killed;
918
 
 
919
 
public:
920
 
 
921
 
  void setKilled(killed_state_t arg)
922
 
  {
923
 
    _killed= arg;
924
 
  }
925
 
 
926
 
  killed_state_t getKilled()
927
 
  {
928
 
    return _killed;
929
 
  }
930
 
 
931
 
  volatile killed_state_t *getKilledPtr() // Do not use this method, it is here for historical convience.
932
 
  {
933
 
    return &_killed;
934
 
  }
935
 
 
936
 
  bool is_admin_connection;
 
657
  killed_state volatile killed;
 
658
 
937
659
  bool some_tables_deleted;
938
660
  bool no_errors;
939
661
  bool password;
1022
744
  }
1023
745
 
1024
746
  /** Returns the current query ID */
1025
 
  query_id_t getQueryId()  const
 
747
  inline query_id_t getQueryId()  const
1026
748
  {
1027
749
    return query_id;
1028
750
  }
1040
762
    return warn_query_id;
1041
763
  }
1042
764
 
 
765
  /** Returns the current query text */
 
766
  inline const std::string &getQueryString()  const
 
767
  {
 
768
    return query;
 
769
  }
 
770
 
 
771
  /** Returns the length of the current query text */
 
772
  inline size_t getQueryLength() const
 
773
  {
 
774
    if (! query.empty())
 
775
      return query.length();
 
776
    else
 
777
      return 0;
 
778
  }
 
779
 
1043
780
  /** Accessor method returning the session's ID. */
1044
 
  inline session_id_t getSessionId()  const
 
781
  inline uint64_t getSessionId()  const
1045
782
  {
1046
783
    return thread_id;
1047
784
  }
1132
869
   */
1133
870
  void cleanup_after_query();
1134
871
  bool storeGlobals();
1135
 
  void awake(Session::killed_state_t state_to_set);
 
872
  void awake(Session::killed_state state_to_set);
1136
873
  /**
1137
874
   * Pulls thread-specific variables into Session state.
1138
875
   *
1215
952
  /**
1216
953
   * Schedule a session to be run on the default scheduler.
1217
954
   */
1218
 
  static bool schedule(Session::shared_ptr&);
1219
 
 
1220
 
  static void unlink(Session::shared_ptr&);
 
955
  bool schedule();
1221
956
 
1222
957
  /*
1223
958
    For enter_cond() / exit_cond() to work the mutex must be got before
1224
959
    enter_cond(); this mutex is then released by exit_cond().
1225
960
    Usage must be: lock mutex; enter_cond(); your code; exit_cond().
1226
961
  */
1227
 
  const char* enter_cond(boost::condition_variable_any &cond, boost::mutex &mutex, const char* msg);
 
962
  const char* enter_cond(pthread_cond_t *cond, pthread_mutex_t* mutex, const char* msg);
1228
963
  void exit_cond(const char* old_msg);
1229
964
 
1230
965
  inline time_t query_start() { return start_time; }
1231
966
  inline void set_time()
1232
967
  {
1233
 
    boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1234
 
    boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1235
 
    start_utime= utime_after_lock= (mytime-epoch).total_microseconds();
1236
 
 
1237
968
    if (user_time)
1238
969
    {
1239
970
      start_time= user_time;
1240
 
      connect_microseconds= start_utime;
 
971
      connect_microseconds= start_utime= utime_after_lock= my_micro_time();
1241
972
    }
1242
 
    else 
1243
 
      start_time= (mytime-epoch).total_seconds();
 
973
    else
 
974
      start_utime= utime_after_lock= my_micro_time_and_time(&start_time);
1244
975
  }
1245
976
  inline void   set_current_time()    { start_time= time(NULL); }
1246
977
  inline void   set_time(time_t t)
1247
978
  {
1248
979
    start_time= user_time= t;
1249
 
    boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1250
 
    boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1251
 
    uint64_t t_mark= (mytime-epoch).total_microseconds();
1252
 
 
1253
 
    start_utime= utime_after_lock= t_mark;
1254
 
  }
1255
 
  void set_time_after_lock()  { 
1256
 
     boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1257
 
     boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1258
 
     utime_after_lock= (mytime-epoch).total_microseconds();
1259
 
  }
 
980
    start_utime= utime_after_lock= my_micro_time();
 
981
  }
 
982
  void set_time_after_lock()  { utime_after_lock= my_micro_time(); }
1260
983
  /**
1261
984
   * Returns the current micro-timestamp
1262
985
   */
1263
986
  inline uint64_t getCurrentTimestamp()  
1264
987
  { 
1265
 
    boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1266
 
    boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1267
 
    uint64_t t_mark= (mytime-epoch).total_microseconds();
1268
 
 
1269
 
    return t_mark; 
 
988
    return my_micro_time(); 
1270
989
  }
1271
990
  inline uint64_t found_rows(void)
1272
991
  {
1277
996
  {
1278
997
    return server_status & SERVER_STATUS_IN_TRANS;
1279
998
  }
 
999
  inline bool fill_derived_tables()
 
1000
  {
 
1001
    return !lex->only_view_structure();
 
1002
  }
 
1003
 
1280
1004
  LEX_STRING *make_lex_string(LEX_STRING *lex_str,
1281
1005
                              const char* str, uint32_t length,
1282
1006
                              bool allocate_lex_string);
1292
1016
    @todo: To silence an error, one should use Internal_error_handler
1293
1017
    mechanism. In future this function will be removed.
1294
1018
  */
1295
 
  inline void clear_error(bool full= false)
 
1019
  inline void clear_error()
1296
1020
  {
1297
1021
    if (main_da.is_error())
1298
1022
      main_da.reset_diagnostics_area();
1299
 
 
1300
 
    if (full)
1301
 
    {
1302
 
      drizzle_reset_errors(this, true);
1303
 
    }
1304
 
  }
1305
 
 
1306
 
  void clearDiagnostics()
1307
 
  {
1308
 
    main_da.reset_diagnostics_area();
 
1023
    return;
1309
1024
  }
1310
1025
 
1311
1026
  /**
1349
1064
  void end_statement();
1350
1065
  inline int killed_errno() const
1351
1066
  {
1352
 
    killed_state_t killed_val; /* to cache the volatile 'killed' */
1353
 
    return (killed_val= _killed) != KILL_BAD_DATA ? killed_val : 0;
 
1067
    killed_state killed_val; /* to cache the volatile 'killed' */
 
1068
    return (killed_val= killed) != KILL_BAD_DATA ? killed_val : 0;
1354
1069
  }
1355
1070
  void send_kill_message() const;
1356
1071
  /* return true if we will abort query if we make a warning now */
1379
1094
    database usually involves other actions, like switching other database
1380
1095
    attributes including security context. In the future, this operation
1381
1096
    will be made private and more convenient interface will be provided.
 
1097
 
 
1098
    @return Operation status
 
1099
      @retval false Success
 
1100
      @retval true  Out-of-memory error
1382
1101
  */
1383
 
  void set_db(const std::string &new_db);
 
1102
  bool set_db(const std::string &new_db);
1384
1103
 
1385
1104
  /*
1386
1105
    Copy the current database to the argument. Use the current arena to
1445
1164
   * Current implementation does not depend on that, but future changes
1446
1165
   * should be done with this in mind; 
1447
1166
   *
1448
 
   * @param passwd Scrambled password received from client
1449
 
   * @param db Database name to connect to, may be NULL
 
1167
   * @param  Scrambled password received from client
 
1168
   * @param  Length of scrambled password
 
1169
   * @param  Database name to connect to, may be NULL
1450
1170
   */
1451
 
  bool checkUser(const std::string &passwd, const std::string &db);
 
1171
  bool checkUser(const char *passwd, uint32_t passwd_len, const char *db);
1452
1172
  
1453
1173
  /**
1454
1174
   * Returns the timestamp (in microseconds) of when the Session 
1477
1197
  {
1478
1198
    return statement_message;
1479
1199
  }
1480
 
  
1481
 
  /**
1482
 
   * Returns a pointer to the current Resulset message for this
1483
 
   * Session, or NULL if no active message.
1484
 
   */
1485
 
  message::Resultset *getResultsetMessage() const
1486
 
  {
1487
 
    return resultset;
1488
 
  }
 
1200
 
1489
1201
  /**
1490
1202
   * Sets the active transaction message used by the ReplicationServices
1491
1203
   * component.
1507
1219
  {
1508
1220
    statement_message= in_message;
1509
1221
  }
1510
 
 
1511
 
  /**
1512
 
   * Sets the active Resultset message used by the Query Cache
1513
 
   * plugin.
1514
 
   *
1515
 
   * @param[in] Pointer to the message
1516
 
   */
1517
 
  void setResultsetMessage(message::Resultset *in_message)
1518
 
  {
1519
 
    resultset= in_message;
1520
 
  }
1521
 
  /**
1522
 
   * reset the active Resultset message used by the Query Cache
1523
 
   * plugin.
1524
 
   */
1525
 
 
1526
 
  void resetResultsetMessage()
1527
 
  { 
1528
 
    resultset= NULL;
1529
 
  }
1530
 
 
1531
1222
private:
1532
1223
  /** Pointers to memory managed by the ReplicationServices component */
1533
1224
  message::Transaction *transaction_message;
1534
1225
  message::Statement *statement_message;
1535
 
  /* Pointer to the current resultset of Select query */
1536
 
  message::Resultset *resultset;
1537
1226
  plugin::EventObserverList *session_event_observers;
1538
1227
  
1539
1228
  /* Schema observers are mapped to databases. */
1619
1308
   * set to query_id of original query.
1620
1309
   */
1621
1310
  void mark_used_tables_as_free_for_reuse(Table *table);
 
1311
  /**
 
1312
    Mark all temporary tables which were used by the current statement or
 
1313
    substatement as free for reuse, but only if the query_id can be cleared.
 
1314
 
 
1315
    @param session thread context
 
1316
 
 
1317
    @remark For temp tables associated with a open SQL HANDLER the query_id
 
1318
            is not reset until the HANDLER is closed.
 
1319
  */
 
1320
  void mark_temp_tables_as_free_for_reuse();
1622
1321
 
1623
1322
public:
1624
1323
 
1660
1359
  }
1661
1360
  void refresh_status();
1662
1361
  user_var_entry *getVariable(LEX_STRING &name, bool create_if_not_exists);
1663
 
  user_var_entry *getVariable(const std::string  &name, bool create_if_not_exists);
1664
 
  void setVariable(const std::string &name, const std::string &value);
1665
1362
  
1666
1363
  /**
1667
1364
   * Closes all tables used by the current substatement, or all tables
1671
1368
  void close_old_data_files(bool morph_locks= false,
1672
1369
                            bool send_refresh= false);
1673
1370
  void close_open_tables();
1674
 
  void close_data_files_and_morph_locks(const TableIdentifier &identifier);
 
1371
  void close_data_files_and_morph_locks(TableIdentifier &identifier);
1675
1372
 
1676
1373
private:
1677
1374
  bool free_cached_table();
1702
1399
   */
1703
1400
  bool openTablesLock(TableList *tables);
1704
1401
 
 
1402
  /**
 
1403
   * Open all tables in list and process derived tables
 
1404
   *
 
1405
   * @param Pointer to a list of tables for open
 
1406
   * @param Bitmap of flags to modify how the tables will be open:
 
1407
   *        DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
 
1408
   *        done a flush or namelock on it.
 
1409
   *
 
1410
   * @retval
 
1411
   *  false - ok
 
1412
   * @retval
 
1413
   *  true  - error
 
1414
   *
 
1415
   * @note
 
1416
   *
 
1417
   * This is to be used on prepare stage when you don't read any
 
1418
   * data from the tables.
 
1419
   */
 
1420
  bool openTables(TableList *tables, uint32_t flags= 0);
 
1421
 
1705
1422
  int open_tables_from_list(TableList **start, uint32_t *counter, uint32_t flags= 0);
1706
1423
 
1707
1424
  Table *openTableLock(TableList *table_list, thr_lock_type lock_type);
1708
1425
  Table *openTable(TableList *table_list, bool *refresh, uint32_t flags= 0);
1709
1426
 
1710
1427
  void unlink_open_table(Table *find);
1711
 
  void drop_open_table(Table *table, const TableIdentifier &identifier);
 
1428
  void drop_open_table(Table *table, TableIdentifier &identifier);
1712
1429
  void close_cached_table(Table *table);
1713
1430
 
1714
1431
  /* Create a lock in the cache */
1715
 
  table::Placeholder *table_cache_insert_placeholder(const TableIdentifier &identifier);
1716
 
  bool lock_table_name_if_not_cached(const TableIdentifier &identifier, Table **table);
 
1432
  Table *table_cache_insert_placeholder(const char *db_name, const char *table_name);
 
1433
  bool lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table);
1717
1434
 
1718
1435
  typedef boost::unordered_map<std::string, message::Table, util::insensitive_hash, util::insensitive_equal_to> TableMessageCache;
1719
 
 
1720
 
  class TableMessages
1721
 
  {
1722
 
    TableMessageCache table_message_cache;
1723
 
 
1724
 
  public:
1725
 
    bool storeTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1726
 
    bool removeTableMessage(const TableIdentifier &identifier);
1727
 
    bool getTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1728
 
    bool doesTableMessageExist(const TableIdentifier &identifier);
1729
 
    bool renameTableMessage(const TableIdentifier &from, const TableIdentifier &to);
1730
 
 
1731
 
  };
 
1436
  TableMessageCache table_message_cache;
 
1437
 
 
1438
  bool storeTableMessage(const TableIdentifier &identifier, message::Table &table_message);
 
1439
  bool removeTableMessage(const TableIdentifier &identifier);
 
1440
  bool getTableMessage(const TableIdentifier &identifier, message::Table &table_message);
 
1441
  bool doesTableMessageExist(const TableIdentifier &identifier);
 
1442
  bool renameTableMessage(const TableIdentifier &from, const TableIdentifier &to);
 
1443
 
 
1444
  /* Work with temporary tables */
 
1445
  Table *find_temporary_table(TableList *table_list);
 
1446
  Table *find_temporary_table(const char *db, const char *table_name);
 
1447
  Table *find_temporary_table(TableIdentifier &identifier);
 
1448
 
 
1449
  void doGetTableNames(CachedDirectory &directory,
 
1450
                       const SchemaIdentifier &schema_identifier,
 
1451
                       std::set<std::string>& set_of_names);
 
1452
  void doGetTableNames(const SchemaIdentifier &schema_identifier,
 
1453
                       std::set<std::string>& set_of_names);
 
1454
 
 
1455
  void doGetTableIdentifiers(CachedDirectory &directory,
 
1456
                             const SchemaIdentifier &schema_identifier,
 
1457
                             TableIdentifiers &set_of_identifiers);
 
1458
  void doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
 
1459
                             TableIdentifiers &set_of_identifiers);
 
1460
 
 
1461
  int doGetTableDefinition(const drizzled::TableIdentifier &identifier,
 
1462
                           message::Table &table_proto);
 
1463
  bool doDoesTableExist(const drizzled::TableIdentifier &identifier);
 
1464
 
 
1465
  void close_temporary_tables();
 
1466
  void close_temporary_table(Table *table);
 
1467
  // The method below just handles the de-allocation of the table. In
 
1468
  // a better memory type world, this would not be needed.
1732
1469
private:
1733
 
  TableMessages _table_message_cache;
1734
 
 
 
1470
  void nukeTable(Table *table);
1735
1471
public:
1736
 
  TableMessages &getMessageCache()
1737
 
  {
1738
 
    return _table_message_cache;
1739
 
  }
 
1472
 
 
1473
  void dumpTemporaryTableNames(const char *id);
 
1474
  int drop_temporary_table(TableList *table_list);
 
1475
  bool rm_temporary_table(plugin::StorageEngine *base, TableIdentifier &identifier);
 
1476
  bool rm_temporary_table(TableIdentifier &identifier, bool best_effort= false);
 
1477
  Table *open_temporary_table(TableIdentifier &identifier,
 
1478
                              bool link_in_list= true);
1740
1479
 
1741
1480
  /* Reopen operations */
1742
1481
  bool reopen_tables(bool get_locks, bool mark_share_as_old);
 
1482
  bool reopen_name_locked_table(TableList* table_list, bool link_in);
1743
1483
  bool close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders);
1744
1484
 
1745
 
  void wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond);
 
1485
  void wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond);
1746
1486
  int setup_conds(TableList *leaves, COND **conds);
1747
1487
  int lock_tables(TableList *tables, uint32_t count, bool *need_reopen);
1748
1488
 
1749
 
  drizzled::util::Storable *getProperty(const std::string &arg)
1750
 
  {
1751
 
    return life_properties[arg];
1752
 
  }
1753
 
 
1754
 
  template<class T>
1755
 
  bool setProperty(const std::string &arg, T *value)
1756
 
  {
1757
 
    life_properties[arg]= value;
1758
 
 
1759
 
    return true;
1760
 
  }
 
1489
  Table *create_virtual_tmp_table(List<CreateField> &field_list);
 
1490
 
 
1491
 
1761
1492
 
1762
1493
  /**
1763
1494
    Return the default storage engine
1772
1503
    if (variables.storage_engine)
1773
1504
      return variables.storage_engine;
1774
1505
    return global_system_variables.storage_engine;
1775
 
  }
 
1506
  };
 
1507
 
 
1508
  static void unlink(Session *session);
1776
1509
 
1777
1510
  void get_xid(DRIZZLE_XID *xid); // Innodb only
1778
1511
 
1779
 
  table::Instance *getInstanceTable();
1780
 
  table::Instance *getInstanceTable(List<CreateField> &field_list);
1781
 
 
1782
1512
private:
1783
 
  bool resetUsage()
1784
 
  {
1785
 
    if (getrusage(RUSAGE_THREAD, &usage))
1786
 
    {
1787
 
      return false;
1788
 
    }
 
1513
  std::vector<TableShareInstance *> temporary_shares;
1789
1514
 
1790
 
    return true;
1791
 
  }
1792
1515
public:
1793
 
 
1794
 
  void setUsage(bool arg)
1795
 
  {
1796
 
    use_usage= arg;
1797
 
  }
1798
 
 
1799
 
  const struct rusage &getUsage()
1800
 
  {
1801
 
    return usage;
1802
 
  }
1803
 
 
1804
 
private:
1805
 
  // This lives throughout the life of Session
1806
 
  bool use_usage;
1807
 
  PropertyMap life_properties;
1808
 
  std::vector<table::Instance *> temporary_shares;
1809
 
  struct rusage usage;
 
1516
  TableShareInstance *getTemporaryShare(TableIdentifier::Type type_arg);
1810
1517
};
1811
1518
 
1812
1519
class Join;
1835
1542
 * A structure used to describe sort information
1836
1543
 * for a field or item used in ORDER BY.
1837
1544
 */
1838
 
class SortField 
 
1545
typedef struct st_sort_field 
1839
1546
{
1840
 
public:
1841
1547
  Field *field; /**< Field to sort */
1842
1548
  Item  *item; /**< Item if not sorting fields */
1843
1549
  size_t length; /**< Length of sort field */
1845
1551
  Item_result result_type; /**< Type of item */
1846
1552
  bool reverse; /**< if descending sort */
1847
1553
  bool need_strxnfrm;   /**< If we have to use strxnfrm() */
1848
 
 
1849
 
  SortField() :
1850
 
    field(0),
1851
 
    item(0),
1852
 
    length(0),
1853
 
    suffix_length(0),
1854
 
    result_type(STRING_RESULT),
1855
 
    reverse(0),
1856
 
    need_strxnfrm(0)
1857
 
  { }
1858
 
 
1859
 
};
 
1554
} SORT_FIELD;
 
1555
 
 
1556
typedef struct st_sort_buffer 
 
1557
{
 
1558
  uint32_t index;       /* 0 or 1 */
 
1559
  uint32_t sort_orders;
 
1560
  uint32_t change_pos; /* If sort-fields changed */
 
1561
  char **buff;
 
1562
  SORT_FIELD *sortorder;
 
1563
} SORT_BUFFER;
1860
1564
 
1861
1565
} /* namespace drizzled */
1862
1566
 
1889
1593
static const std::bitset<CF_BIT_SIZE> CF_SHOW_TABLE_COMMAND(1 << CF_BIT_SHOW_TABLE_COMMAND);
1890
1594
static const std::bitset<CF_BIT_SIZE> CF_WRITE_LOGS_COMMAND(1 << CF_BIT_WRITE_LOGS_COMMAND);
1891
1595
 
1892
 
namespace display  {
1893
 
const std::string &type(drizzled::Session::global_read_lock_t type);
1894
 
size_t max_string_length(drizzled::Session::global_read_lock_t type);
1895
 
} /* namespace display */
1896
 
 
1897
1596
} /* namespace drizzled */
1898
1597
 
1899
1598
#endif /* DRIZZLED_SESSION_H */