~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_class.h

  • Committer: Monty Taylor
  • Date: 2008-10-20 08:48:34 UTC
  • mfrom: (520.1.22 drizzle)
  • Revision ID: monty@inaugust.com-20081020084834-xpb3w01vkcp55o02
Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
113
113
    is why we supply it with a copy.
114
114
 
115
115
    @return If out of memory, 0 is returned and an error is set in
116
 
    THD.
 
116
    Session.
117
117
  */
118
118
  Key_part_spec *clone(MEM_ROOT *mem_root) const
119
119
  { return new (mem_root) Key_part_spec(*this); }
237
237
class select_result;
238
238
class Time_zone;
239
239
 
240
 
#define THD_SENTRY_MAGIC 0xfeedd1ff
241
 
#define THD_SENTRY_GONE  0xdeadbeef
 
240
#define Session_SENTRY_MAGIC 0xfeedd1ff
 
241
#define Session_SENTRY_GONE  0xdeadbeef
242
242
 
243
 
#define THD_CHECK_SENTRY(thd) assert(thd->dbug_sentry == THD_SENTRY_MAGIC)
 
243
#define Session_CHECK_SENTRY(session) assert(session->dbug_sentry == Session_SENTRY_MAGIC)
244
244
 
245
245
struct system_variables
246
246
{
312
312
  ulong trans_prealloc_size;
313
313
  ulong log_warnings;
314
314
  ulong group_concat_max_len;
315
 
  ulong binlog_format; // binlog format for this thd (see enum_binlog_format)
 
315
  ulong binlog_format; // binlog format for this session (see enum_binlog_format)
316
316
  /*
317
317
    In slave thread we need to know in behalf of which
318
318
    thread the query is being run to replicate temp tables properly
439
439
 
440
440
#define last_system_status_var questions
441
441
 
442
 
void mark_transaction_to_rollback(THD *thd, bool all);
 
442
void mark_transaction_to_rollback(Session *session, bool all);
443
443
 
444
444
#ifdef DRIZZLE_SERVER
445
445
 
497
497
  One connection can contain a lot of simultaneously running statements,
498
498
  some of which could be:
499
499
   - prepared, that is, contain placeholders,
500
 
  To perform some action with statement we reset THD part to the state  of
501
 
  that statement, do the action, and then save back modified state from THD
 
500
  To perform some action with statement we reset Session part to the state  of
 
501
  that statement, do the action, and then save back modified state from Session
502
502
  to the statement. It will be changed in near future, and Statement will
503
503
  be used explicitly.
504
504
*/
519
519
                        handler of fields used is set
520
520
    MARK_COLUMNS_READ:  Means a bit in read set is set to inform handler
521
521
                        that the field is to be read. If field list contains
522
 
                        duplicates, then thd->dup_field is set to point
 
522
                        duplicates, then session->dup_field is set to point
523
523
                        to the last found duplicate.
524
524
    MARK_COLUMNS_WRITE: Means a bit is set in write set to inform handler
525
525
                        that it needs to update this field in write_row
538
538
    LOCK_thread_count mutex. And (B) we are ONLY allowed to set query to a
539
539
    non-NULL value if its previous value is NULL. We do not need to protect
540
540
    operation (B) with any mutex. To avoid crashes in races, if we do not
541
 
    know that thd->query cannot change at the moment, one should print
542
 
    thd->query like this:
 
541
    know that session->query cannot change at the moment, one should print
 
542
    session->query like this:
543
543
      (1) reserve the LOCK_thread_count mutex;
544
 
      (2) check if thd->query is NULL;
545
 
      (3) if not NULL, then print at most thd->query_length characters from
 
544
      (2) check if session->query is NULL;
 
545
      (3) if not NULL, then print at most session->query_length characters from
546
546
      it. We will see the query_length field as either 0, or the right value
547
547
      for it.
548
548
    Assuming that the write and read of an n-bit memory field in an n-bit
562
562
    valid database name.
563
563
 
564
564
    @note this attribute is set and alloced by the slave SQL thread (for
565
 
    the THD of that thread); that thread is (and must remain, for now) the
 
565
    the Session of that thread); that thread is (and must remain, for now) the
566
566
    only responsible for freeing this member.
567
567
  */
568
568
 
592
592
  /* For now, this is only used to catch duplicated external xids */
593
593
  XID  xid;                           // transaction identifier
594
594
  enum xa_states xa_state;            // used by external XA only
595
 
  bool in_thd;
 
595
  bool in_session;
596
596
} XID_STATE;
597
597
 
598
598
extern pthread_mutex_t LOCK_xid_cache;
606
606
 
607
607
/**
608
608
  @class Security_context
609
 
  @brief A set of THD members describing the current authenticated user.
 
609
  @brief A set of Session members describing the current authenticated user.
610
610
*/
611
611
 
612
612
class Security_context {
689
689
    Tables that were locked with explicit or implicit LOCK TABLES.
690
690
    (Implicit LOCK TABLES happens when we are prelocking tables for
691
691
     execution of statement which uses stored routines. See description
692
 
     THD::prelocked_mode for more info.)
 
692
     Session::prelocked_mode for more info.)
693
693
  */
694
694
  DRIZZLE_LOCK *locked_tables;
695
695
 
734
734
};
735
735
 
736
736
 
737
 
/* Flags for the THD::system_thread variable */
 
737
/* Flags for the Session::system_thread variable */
738
738
enum enum_thread_type
739
739
{
740
740
  NON_SYSTEM_THREAD,
768
768
    from the anticipated conditions trapped during runtime.
769
769
 
770
770
    This mechanism is similar to C++ try/throw/catch:
771
 
    - 'try' correspond to <code>THD::push_internal_handler()</code>,
 
771
    - 'try' correspond to <code>Session::push_internal_handler()</code>,
772
772
    - 'throw' correspond to <code>my_error()</code>,
773
773
    which invokes <code>my_message_sql()</code>,
774
774
    - 'catch' correspond to checking how/if an internal handler was invoked,
775
775
    before removing it from the exception stack with
776
 
    <code>THD::pop_internal_handler()</code>.
 
776
    <code>Session::pop_internal_handler()</code>.
777
777
 
778
778
    @param sql_errno the error number
779
779
    @param level the error level
780
 
    @param thd the calling thread
 
780
    @param session the calling thread
781
781
    @return true if the error is handled
782
782
  */
783
783
  virtual bool handle_error(uint32_t sql_errno,
784
784
                            const char *message,
785
785
                            DRIZZLE_ERROR::enum_warning_level level,
786
 
                            THD *thd) = 0;
 
786
                            Session *session) = 0;
787
787
};
788
788
 
789
789
 
815
815
  /** Set to make set_error_status after set_{ok,eof}_status possible. */
816
816
  bool can_overwrite_status;
817
817
 
818
 
  void set_ok_status(THD *thd, ha_rows affected_rows_arg,
 
818
  void set_ok_status(Session *session, ha_rows affected_rows_arg,
819
819
                     uint64_t last_insert_id_arg,
820
820
                     const char *message);
821
 
  void set_eof_status(THD *thd);
822
 
  void set_error_status(THD *thd, uint32_t sql_errno_arg, const char *message_arg);
 
821
  void set_eof_status(Session *session);
 
822
  void set_error_status(Session *session, uint32_t sql_errno_arg, const char *message_arg);
823
823
 
824
824
  void disable_status();
825
825
 
868
868
  uint32_t m_sql_errno;
869
869
 
870
870
  /**
871
 
    Copied from thd->server_status when the diagnostics area is assigned.
 
871
    Copied from session->server_status when the diagnostics area is assigned.
872
872
    We need this member as some places in the code use the following pattern:
873
 
    thd->server_status|= ...
874
 
    my_eof(thd);
875
 
    thd->server_status&= ~...
 
873
    session->server_status|= ...
 
874
    my_eof(session);
 
875
    session->server_status&= ~...
876
876
    Assigned by OK, EOF or ERROR.
877
877
  */
878
878
  uint32_t m_server_status;
879
879
  /**
880
880
    The number of rows affected by the last statement. This is
881
 
    semantically close to thd->row_count_func, but has a different
882
 
    life cycle. thd->row_count_func stores the value returned by
 
881
    semantically close to session->row_count_func, but has a different
 
882
    life cycle. session->row_count_func stores the value returned by
883
883
    function ROW_COUNT() and is cleared only by statements that
884
884
    update its value, such as INSERT, UPDATE, DELETE and few others.
885
885
    This member is cleared at the beginning of the next statement.
886
886
 
887
 
    We could possibly merge the two, but life cycle of thd->row_count_func
 
887
    We could possibly merge the two, but life cycle of session->row_count_func
888
888
    can not be changed.
889
889
  */
890
890
  ha_rows    m_affected_rows;
891
891
  /**
892
892
    Similarly to the previous member, this is a replacement of
893
 
    thd->first_successful_insert_id_in_prev_stmt, which is used
 
893
    session->first_successful_insert_id_in_prev_stmt, which is used
894
894
    to implement LAST_INSERT_ID().
895
895
  */
896
896
  uint64_t   m_last_insert_id;
898
898
  uint       m_total_warn_count;
899
899
  enum_diagnostics_status m_status;
900
900
  /**
901
 
    @todo: the following THD members belong here:
 
901
    @todo: the following Session members belong here:
902
902
    - warn_list, warn_count,
903
903
  */
904
904
};
932
932
 
933
933
 
934
934
/**
935
 
  @class THD
936
 
  For each client connection we create a separate thread with THD serving as
 
935
  @class Session
 
936
  For each client connection we create a separate thread with Session serving as
937
937
  a thread/connection descriptor
938
938
*/
939
939
 
940
 
class THD :public Statement,
 
940
class Session :public Statement,
941
941
           public Open_tables_state
942
942
{
943
943
public:
945
945
  Relay_log_info* rli_fake;
946
946
 
947
947
  /*
948
 
    Constant for THD::where initialization in the beginning of every query.
 
948
    Constant for Session::where initialization in the beginning of every query.
949
949
 
950
 
    It's needed because we do not save/restore THD::where normally during
 
950
    It's needed because we do not save/restore Session::where normally during
951
951
    primary (non subselect) query execution.
952
952
  */
953
953
  static const char * const DEFAULT_WHERE;
967
967
  THR_LOCK_OWNER main_lock_id;          // To use for conventional queries
968
968
  THR_LOCK_OWNER *lock_id;              // If not main_lock_id, points to
969
969
                                        // the lock_id of a cursor.
970
 
  pthread_mutex_t LOCK_delete;          // Locked before thd is deleted
 
970
  pthread_mutex_t LOCK_delete;          // Locked before session is deleted
971
971
  /*
972
972
    A pointer to the stack frame of handle_one_connection(),
973
973
    which is called first in the thread for handling a client
981
981
 
982
982
  /**
983
983
    @note
984
 
    Some members of THD (currently 'Statement::db',
 
984
    Some members of Session (currently 'Statement::db',
985
985
    'catalog' and 'query')  are set and alloced by the slave SQL thread
986
 
    (for the THD of that thread); that thread is (and must remain, for now)
 
986
    (for the Session of that thread); that thread is (and must remain, for now)
987
987
    the only responsible for freeing these 3 members. If you add members
988
988
    here, and you add code to set them in replication, don't forget to
989
989
    free_them_and_set_them_to_0 in replication properly. For details see
997
997
 
998
998
  /*
999
999
    Points to info-string that we show in SHOW PROCESSLIST
1000
 
    You are supposed to call THD_SET_PROC_INFO only if you have coded
 
1000
    You are supposed to call Session_SET_PROC_INFO only if you have coded
1001
1001
    a time-consuming piece that MySQL can get stuck in for a long time.
1002
1002
 
1003
 
    Set it using the  thd_proc_info(THD *thread, const char *message)
 
1003
    Set it using the  session_proc_info(Session *thread, const char *message)
1004
1004
    macro/function.
1005
1005
  */
1006
1006
  void        set_proc_info(const char *info) { proc_info= info; }
1044
1044
  Ha_data ha_data[MAX_HA];
1045
1045
 
1046
1046
  /* Place to store various things */
1047
 
  void *thd_marker;
 
1047
  void *session_marker;
1048
1048
  int binlog_setup_trx_data();
1049
1049
 
1050
1050
  /*
1099
1099
 
1100
1100
  struct st_transactions {
1101
1101
    SAVEPOINT *savepoints;
1102
 
    THD_TRANS all;                      // Trans since BEGIN WORK
1103
 
    THD_TRANS stmt;                     // Trans for current statement
 
1102
    Session_TRANS all;                  // Trans since BEGIN WORK
 
1103
    Session_TRANS stmt;                 // Trans for current statement
1104
1104
    bool on;                            // see ha_enable_transaction()
1105
1105
    XID_STATE xid_state;
1106
1106
    Rows_log_event *m_pending_rows_event;
1130
1130
  /*
1131
1131
    This is to track items changed during execution of a prepared
1132
1132
    statement/stored procedure. It's created by
1133
 
    register_item_tree_change() in memory root of THD, and freed in
 
1133
    register_item_tree_change() in memory root of Session, and freed in
1134
1134
    rollback_item_tree_changes(). For conventional execution it's always
1135
1135
    empty.
1136
1136
  */
1289
1289
  /*
1290
1290
    The set of those tables whose fields are referenced in all subqueries
1291
1291
    of the query.
1292
 
    TODO: possibly this it is incorrect to have used tables in THD because
 
1292
    TODO: possibly this it is incorrect to have used tables in Session because
1293
1293
    with more than one subquery, it is not clear what does the field mean.
1294
1294
  */
1295
1295
  table_map  used_tables;
1365
1365
    can not continue. In particular, disables activation of
1366
1366
    CONTINUE or EXIT handlers of stored routines.
1367
1367
    Reset in the end of processing of the current user request, in
1368
 
    @see mysql_reset_thd_for_next_command().
 
1368
    @see mysql_reset_session_for_next_command().
1369
1369
  */
1370
1370
  bool is_fatal_error;
1371
1371
  /**
1447
1447
    
1448
1448
    /* 
1449
1449
      'queries' (actually SP statements) that run under inside this binlog
1450
 
      union have thd->query_id >= first_query_id.
 
1450
      union have session->query_id >= first_query_id.
1451
1451
    */
1452
1452
    query_id_t first_query_id;
1453
1453
  } binlog_evt_union;
1461
1461
  */
1462
1462
  Lex_input_stream *m_lip;
1463
1463
 
1464
 
  THD();
1465
 
  ~THD();
 
1464
  Session();
 
1465
  ~Session();
1466
1466
 
1467
1467
  void init(void);
1468
1468
  /*
1469
1469
    Initialize memory roots necessary for query processing and (!)
1470
 
    pre-allocate memory for it. We can't do that in THD constructor because
 
1470
    pre-allocate memory for it. We can't do that in Session constructor because
1471
1471
    there are use cases (acl_init, watcher threads,
1472
1472
    killing mysqld) where it's vital to not allocate excessive and not used
1473
1473
    memory. Note, that we still don't return error from init_for_queries():
1479
1479
  void cleanup(void);
1480
1480
  void cleanup_after_query();
1481
1481
  bool store_globals();
1482
 
  void awake(THD::killed_state state_to_set);
 
1482
  void awake(Session::killed_state state_to_set);
1483
1483
 
1484
1484
  enum enum_binlog_query_type {
1485
1485
    /*
1503
1503
  int binlog_query(enum_binlog_query_type qtype,
1504
1504
                   char const *query, ulong query_len,
1505
1505
                   bool is_trans, bool suppress_use,
1506
 
                   THD::killed_state killed_err_arg= THD::KILLED_NO_VALUE);
 
1506
                   Session::killed_state killed_err_arg= Session::KILLED_NO_VALUE);
1507
1507
 
1508
1508
  /*
1509
1509
    For enter_cond() / exit_cond() to work the mutex must be got before
1526
1526
      Putting the mutex unlock in exit_cond() ensures that
1527
1527
      mysys_var->current_mutex is always unlocked _before_ mysys_var->mutex is
1528
1528
      locked (if that would not be the case, you'll get a deadlock if someone
1529
 
      does a THD::awake() on you).
 
1529
      does a Session::awake() on you).
1530
1530
    */
1531
1531
    pthread_mutex_unlock(mysys_var->current_mutex);
1532
1532
    pthread_mutex_lock(&mysys_var->mutex);
1748
1748
    a statement is parsed but before it's executed.
1749
1749
  */
1750
1750
  bool copy_db_to(char **p_db, size_t *p_db_length);
1751
 
  thd_scheduler scheduler;
 
1751
  session_scheduler scheduler;
1752
1752
 
1753
1753
public:
1754
1754
  /**
1795
1795
};
1796
1796
 
1797
1797
 
1798
 
/** A short cut for thd->main_da.set_ok_status(). */
 
1798
/** A short cut for session->main_da.set_ok_status(). */
1799
1799
 
1800
1800
inline void
1801
 
my_ok(THD *thd, ha_rows affected_rows= 0, uint64_t id= 0,
 
1801
my_ok(Session *session, ha_rows affected_rows= 0, uint64_t id= 0,
1802
1802
        const char *message= NULL)
1803
1803
{
1804
 
  thd->main_da.set_ok_status(thd, affected_rows, id, message);
 
1804
  session->main_da.set_ok_status(session, affected_rows, id, message);
1805
1805
}
1806
1806
 
1807
1807
 
1808
 
/** A short cut for thd->main_da.set_eof_status(). */
 
1808
/** A short cut for session->main_da.set_eof_status(). */
1809
1809
 
1810
1810
inline void
1811
 
my_eof(THD *thd)
 
1811
my_eof(Session *session)
1812
1812
{
1813
 
  thd->main_da.set_eof_status(thd);
 
1813
  session->main_da.set_eof_status(session);
1814
1814
}
1815
1815
 
1816
1816
#define tmp_disable_binlog(A)       \
1850
1850
 
1851
1851
class select_result :public Sql_alloc {
1852
1852
protected:
1853
 
  THD *thd;
 
1853
  Session *session;
1854
1854
  SELECT_LEX_UNIT *unit;
1855
1855
public:
1856
1856
  select_result();
1889
1889
    statement/stored procedure.
1890
1890
  */
1891
1891
  virtual void cleanup();
1892
 
  void set_thd(THD *thd_arg) { thd= thd_arg; }
 
1892
  void set_session(Session *session_arg) { session= session_arg; }
1893
1893
  void begin_dataset() {}
1894
1894
};
1895
1895
 
2030
2030
  Field **field;
2031
2031
  /* lock data for tmp table */
2032
2032
  DRIZZLE_LOCK *m_lock;
2033
 
  /* m_lock or thd->extra_lock */
 
2033
  /* m_lock or session->extra_lock */
2034
2034
  DRIZZLE_LOCK **m_plock;
2035
2035
public:
2036
2036
  select_create (TableList *table_arg,
2054
2054
  void abort();
2055
2055
  virtual bool can_rollback_data() { return 1; }
2056
2056
 
2057
 
  // Needed for access from local class MY_HOOKS in prepare(), since thd is proteted.
2058
 
  const THD *get_thd(void) { return thd; }
 
2057
  // Needed for access from local class MY_HOOKS in prepare(), since session is proteted.
 
2058
  const Session *get_session(void) { return session; }
2059
2059
  const HA_CREATE_INFO *get_create_info() { return create_info; };
2060
2060
  int prepare2(void) { return 0; }
2061
2061
};
2142
2142
  bool send_eof();
2143
2143
  bool flush();
2144
2144
  void cleanup();
2145
 
  bool create_result_table(THD *thd, List<Item> *column_types,
 
2145
  bool create_result_table(Session *session, List<Item> *column_types,
2146
2146
                           bool is_distinct, uint64_t options,
2147
2147
                           const char *alias, bool bit_fields_as_long);
2148
2148
};
2224
2224
  LEX_STRING db;
2225
2225
  LEX_STRING table;
2226
2226
  SELECT_LEX_UNIT *sel;
2227
 
  inline Table_ident(THD *thd, LEX_STRING db_arg, LEX_STRING table_arg,
 
2227
  inline Table_ident(Session *session, LEX_STRING db_arg, LEX_STRING table_arg,
2228
2228
                     bool force)
2229
2229
    :table(table_arg), sel((SELECT_LEX_UNIT *)0)
2230
2230
  {
2231
 
    if (!force && (thd->client_capabilities & CLIENT_NO_SCHEMA))
 
2231
    if (!force && (session->client_capabilities & CLIENT_NO_SCHEMA))
2232
2232
      db.str=0;
2233
2233
    else
2234
2234
      db= db_arg;