1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#ifndef DRIZZLED_SESSION_H
22
#define DRIZZLED_SESSION_H
1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24
17
/* Classes in mysql */
26
#include <drizzled/global.h>
27
#include <drizzled/log.h>
28
#include <drizzled/replication/tblmap.h>
29
#include <drizzled/protocol.h>
30
#include <libdrizzle/password.h> // rand_struct
31
#include <drizzled/sql_locale.h>
32
#include <drizzled/scheduler.h>
33
#include <drizzled/ha_trx_info.h>
34
#include <mysys/my_tree.h>
35
#include <drizzled/handler.h>
36
#include <drizzled/sql_error.h>
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface /* gcc class implementation */
23
#include <drizzled/plugin_audit.h>
25
#include "rpl_tblmap.h"
39
27
class Relay_log_info;
99
class Key_part_spec :public Sql_alloc {
101
LEX_STRING field_name;
103
Key_part_spec(const LEX_STRING &name, uint len)
104
: field_name(name), length(len)
106
Key_part_spec(const char *name, const size_t name_len, uint len)
108
{ field_name.str= (char *)name; field_name.length= name_len; }
109
bool operator==(const Key_part_spec& other) const;
111
Construct a copy of this Key_part_spec. field_name is copied
112
by-pointer as it is known to never change. At the same time
113
'length' may be reset in mysql_prepare_create_table, and this
114
is why we supply it with a copy.
116
@return If out of memory, 0 is returned and an error is set in
119
Key_part_spec *clone(MEM_ROOT *mem_root) const
120
{ return new (mem_root) Key_part_spec(*this); }
124
class Alter_drop :public Sql_alloc {
126
enum drop_type {KEY, COLUMN };
129
Alter_drop(enum drop_type par_type,const char *par_name)
130
:name(par_name), type(par_type) {}
132
Used to make a clone of this object for ALTER/CREATE TABLE
133
@sa comment for Key_part_spec::clone
135
Alter_drop *clone(MEM_ROOT *mem_root) const
136
{ return new (mem_root) Alter_drop(*this); }
140
class Alter_column :public Sql_alloc {
144
Alter_column(const char *par_name,Item *literal)
145
:name(par_name), def(literal) {}
147
Used to make a clone of this object for ALTER/CREATE TABLE
148
@sa comment for Key_part_spec::clone
150
Alter_column *clone(MEM_ROOT *mem_root) const
151
{ return new (mem_root) Alter_column(*this); }
155
class Key :public Sql_alloc {
157
enum Keytype { PRIMARY, UNIQUE, MULTIPLE, FOREIGN_KEY};
159
KEY_CREATE_INFO key_create_info;
160
List<Key_part_spec> columns;
164
Key(enum Keytype type_par, const LEX_STRING &name_arg,
165
KEY_CREATE_INFO *key_info_arg,
166
bool generated_arg, List<Key_part_spec> &cols)
167
:type(type_par), key_create_info(*key_info_arg), columns(cols),
168
name(name_arg), generated(generated_arg)
170
Key(enum Keytype type_par, const char *name_arg, size_t name_len_arg,
171
KEY_CREATE_INFO *key_info_arg, bool generated_arg,
172
List<Key_part_spec> &cols)
173
:type(type_par), key_create_info(*key_info_arg), columns(cols),
174
generated(generated_arg)
176
name.str= (char *)name_arg;
177
name.length= name_len_arg;
179
Key(const Key &rhs, MEM_ROOT *mem_root);
181
/* Equality comparison of keys (ignoring name) */
182
friend bool foreign_key_prefix(Key *a, Key *b);
184
Used to make a clone of this object for ALTER/CREATE TABLE
185
@sa comment for Key_part_spec::clone
187
virtual Key *clone(MEM_ROOT *mem_root) const
188
{ return new (mem_root) Key(*this, mem_root); }
193
class Foreign_key: public Key {
195
enum fk_match_opt { FK_MATCH_UNDEF, FK_MATCH_FULL,
196
FK_MATCH_PARTIAL, FK_MATCH_SIMPLE};
197
enum fk_option { FK_OPTION_UNDEF, FK_OPTION_RESTRICT, FK_OPTION_CASCADE,
198
FK_OPTION_SET_NULL, FK_OPTION_NO_ACTION, FK_OPTION_DEFAULT};
200
Table_ident *ref_table;
201
List<Key_part_spec> ref_columns;
202
uint delete_opt, update_opt, match_opt;
203
Foreign_key(const LEX_STRING &name_arg, List<Key_part_spec> &cols,
204
Table_ident *table, List<Key_part_spec> &ref_cols,
205
uint delete_opt_arg, uint update_opt_arg, uint match_opt_arg)
206
:Key(FOREIGN_KEY, name_arg, &default_key_create_info, 0, cols),
207
ref_table(table), ref_columns(ref_cols),
208
delete_opt(delete_opt_arg), update_opt(update_opt_arg),
209
match_opt(match_opt_arg)
211
Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root);
213
Used to make a clone of this object for ALTER/CREATE TABLE
214
@sa comment for Key_part_spec::clone
216
virtual Key *clone(MEM_ROOT *mem_root) const
217
{ return new (mem_root) Foreign_key(*this, mem_root); }
106
220
typedef struct st_mysql_lock
109
uint32_t table_count,lock_count;
223
uint table_count,lock_count;
110
224
THR_LOCK_DATA **locks;
114
228
class LEX_COLUMN : public Sql_alloc
119
233
LEX_COLUMN (const String& x,const uint& y ): column (x),rights (y) {}
236
#include "sql_lex.h" /* Must be here */
122
238
class select_result;
125
#define Session_SENTRY_MAGIC 0xfeedd1ff
126
#define Session_SENTRY_GONE 0xdeadbeef
241
#define THD_SENTRY_MAGIC 0xfeedd1ff
242
#define THD_SENTRY_GONE 0xdeadbeef
128
#define Session_CHECK_SENTRY(session) assert(session->dbug_sentry == Session_SENTRY_MAGIC)
244
#define THD_CHECK_SENTRY(thd) assert(thd->dbug_sentry == THD_SENTRY_MAGIC)
130
246
struct system_variables
341
459
MEM_ROOT *mem_root; // Pointer to current memroot
343
Query_arena(MEM_ROOT *mem_root_arg) :
344
free_list(0), mem_root(mem_root_arg)
460
bool is_backup_arena; /* True if this arena is used for backup. */
463
The states relfects three diffrent life cycles for three
464
different types of statements:
465
Prepared statement: INITIALIZED -> PREPARED -> EXECUTED.
466
Stored procedure: INITIALIZED_FOR_SP -> EXECUTED.
467
Other statements: CONVENTIONAL_EXECUTION never changes.
472
CONVENTIONAL_EXECUTION= 3, EXECUTED= 4, ERROR= -1
477
Query_arena(MEM_ROOT *mem_root_arg, enum enum_state state_arg) :
478
free_list(0), mem_root(mem_root_arg), state(state_arg)
479
{ INIT_ARENA_DBUG_INFO; }
347
481
This constructor is used only when Query_arena is created as
348
482
backup storage for another instance of Query_arena.
484
Query_arena() { INIT_ARENA_DBUG_INFO; }
352
486
virtual ~Query_arena() {};
488
inline bool is_conventional() const
489
{ assert(state == CONVENTIONAL_EXECUTION); return state == CONVENTIONAL_EXECUTION; }
354
491
inline void* alloc(size_t size) { return alloc_root(mem_root,size); }
355
492
inline void* calloc(size_t size)
655
835
from the anticipated conditions trapped during runtime.
657
837
This mechanism is similar to C++ try/throw/catch:
658
- 'try' correspond to <code>Session::push_internal_handler()</code>,
838
- 'try' correspond to <code>THD::push_internal_handler()</code>,
659
839
- 'throw' correspond to <code>my_error()</code>,
660
840
which invokes <code>my_message_sql()</code>,
661
841
- 'catch' correspond to checking how/if an internal handler was invoked,
662
842
before removing it from the exception stack with
663
<code>Session::pop_internal_handler()</code>.
843
<code>THD::pop_internal_handler()</code>.
665
845
@param sql_errno the error number
666
846
@param level the error level
667
@param session the calling thread
847
@param thd the calling thread
668
848
@return true if the error is handled
670
virtual bool handle_error(uint32_t sql_errno,
850
virtual bool handle_error(uint sql_errno,
671
851
const char *message,
672
DRIZZLE_ERROR::enum_warning_level level,
673
Session *session) = 0;
852
MYSQL_ERROR::enum_warning_level level,
749
929
/** Message buffer. Can be used by OK or ERROR status. */
750
char m_message[DRIZZLE_ERRMSG_SIZE];
930
char m_message[MYSQL_ERRMSG_SIZE];
752
932
SQL error number. One of ER_ codes from share/errmsg.txt.
753
933
Set by set_error_status.
755
uint32_t m_sql_errno;
758
Copied from session->server_status when the diagnostics area is assigned.
938
Copied from thd->server_status when the diagnostics area is assigned.
759
939
We need this member as some places in the code use the following pattern:
760
session->server_status|= ...
762
session->server_status&= ~...
940
thd->server_status|= ...
942
thd->server_status&= ~...
763
943
Assigned by OK, EOF or ERROR.
765
uint32_t m_server_status;
945
uint m_server_status;
767
947
The number of rows affected by the last statement. This is
768
semantically close to session->row_count_func, but has a different
769
life cycle. session->row_count_func stores the value returned by
948
semantically close to thd->row_count_func, but has a different
949
life cycle. thd->row_count_func stores the value returned by
770
950
function ROW_COUNT() and is cleared only by statements that
771
951
update its value, such as INSERT, UPDATE, DELETE and few others.
772
952
This member is cleared at the beginning of the next statement.
774
We could possibly merge the two, but life cycle of session->row_count_func
954
We could possibly merge the two, but life cycle of thd->row_count_func
775
955
can not be changed.
777
957
ha_rows m_affected_rows;
779
959
Similarly to the previous member, this is a replacement of
780
session->first_successful_insert_id_in_prev_stmt, which is used
960
thd->first_successful_insert_id_in_prev_stmt, which is used
781
961
to implement LAST_INSERT_ID().
783
963
uint64_t m_last_insert_id;
940
1129
void binlog_start_trans_and_stmt();
941
1130
void binlog_set_stmt_begin();
942
int binlog_write_table_map(Table *table, bool is_transactional);
943
int binlog_write_row(Table* table, bool is_transactional,
944
const unsigned char *new_data);
945
int binlog_delete_row(Table* table, bool is_transactional,
946
const unsigned char *old_data);
947
int binlog_update_row(Table* table, bool is_transactional,
948
const unsigned char *old_data, const unsigned char *new_data);
1131
int binlog_write_table_map(TABLE *table, bool is_transactional);
1132
int binlog_write_row(TABLE* table, bool is_transactional,
1133
const uchar *new_data);
1134
int binlog_delete_row(TABLE* table, bool is_transactional,
1135
const uchar *old_data);
1136
int binlog_update_row(TABLE* table, bool is_transactional,
1137
const uchar *old_data, const uchar *new_data);
950
1139
void set_server_id(uint32_t sid) { server_id = sid; }
1003
1198
st_transactions()
1005
memset(this, 0, sizeof(*this));
1200
memset((char*)this, 0, sizeof(*this));
1006
1201
xid_state.xid.null();
1007
1202
init_sql_alloc(&mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
1010
1205
Field *dup_field;
1011
1206
sigset_t signals;
1207
#ifdef SIGNAL_WITH_VIO_CLOSE
1013
1211
This is to track items changed during execution of a prepared
1014
1212
statement/stored procedure. It's created by
1015
register_item_tree_change() in memory root of Session, and freed in
1213
register_item_tree_change() in memory root of THD, and freed in
1016
1214
rollback_item_tree_changes(). For conventional execution it's always
1019
1217
Item_change_list change_list;
1220
A permanent memory area of the statement. For conventional
1221
execution, the parsed tree and execution runtime reside in the same
1222
memory root. In this case stmt_arena points to THD. In case of
1223
a prepared statement or a stored procedure statement, thd->mem_root
1224
conventionally points to runtime memory, and thd->stmt_arena
1225
points to the memory of the PS/SP, where the parsed tree of the
1226
statement resides. Whenever you need to perform a permanent
1227
transformation of a parsed tree, you should allocate new memory in
1228
stmt_arena, to allow correct re-execution of PS/SP.
1229
Note: in the parser, stmt_arena == thd, even for PS/SP.
1231
Query_arena *stmt_arena;
1021
1232
/* Tells if LAST_INSERT_ID(#) was called for the current statement */
1022
1233
bool arg_of_last_insert_id_function;
1301
If true, drizzle_bin_log::write(Log_event) call will not write events to
1525
If true, mysql_bin_log::write(Log_event) call will not write events to
1302
1526
binlog, and maintain 2 below variables instead (use
1303
drizzle_bin_log.start_union_events to turn this on)
1527
mysql_bin_log.start_union_events to turn this on)
1307
If true, at least one drizzle_bin_log::write(Log_event) call has been
1308
made after last drizzle_bin_log.start_union_events() call.
1531
If true, at least one mysql_bin_log::write(Log_event) call has been
1532
made after last mysql_bin_log.start_union_events() call.
1310
1534
bool unioned_events;
1312
If true, at least one drizzle_bin_log::write(Log_event e), where
1536
If true, at least one mysql_bin_log::write(Log_event e), where
1313
1537
e.cache_stmt == true call has been made after last
1314
drizzle_bin_log.start_union_events() call.
1538
mysql_bin_log.start_union_events() call.
1316
1540
bool unioned_events_trans;
1319
1543
'queries' (actually SP statements) that run under inside this binlog
1320
union have session->query_id >= first_query_id.
1544
union have thd->query_id >= first_query_id.
1322
1546
query_id_t first_query_id;
1323
1547
} binlog_evt_union;
1444
1685
LEX_STRING *make_lex_string(LEX_STRING *lex_str,
1445
const char* str, uint32_t length,
1686
const char* str, uint length,
1446
1687
bool allocate_lex_string);
1448
bool convert_string(LEX_STRING *to, const CHARSET_INFO * const to_cs,
1449
const char *from, uint32_t from_length,
1450
const CHARSET_INFO * const from_cs);
1452
bool convert_string(String *s, const CHARSET_INFO * const from_cs, const CHARSET_INFO * const to_cs);
1454
void add_changed_table(Table *table);
1689
bool convert_string(LEX_STRING *to, CHARSET_INFO *to_cs,
1690
const char *from, uint from_length,
1691
CHARSET_INFO *from_cs);
1693
bool convert_string(String *s, CHARSET_INFO *from_cs, CHARSET_INFO *to_cs);
1695
void add_changed_table(TABLE *table);
1455
1696
void add_changed_table(const char *key, long key_length);
1456
CHANGED_TableList * changed_table_dup(const char *key, long key_length);
1697
CHANGED_TABLE_LIST * changed_table_dup(const char *key, long key_length);
1457
1698
int send_explain_fields(select_result *result);
1459
1700
Clear the current error, if any.
1525
1770
void set_status_var_init();
1526
1771
void reset_n_backup_open_tables_state(Open_tables_state *backup);
1527
1772
void restore_backup_open_tables_state(Open_tables_state *backup);
1773
void restore_sub_statement_state(Sub_statement_state *backup);
1774
void set_n_backup_active_arena(Query_arena *set, Query_arena *backup);
1775
void restore_active_arena(Query_arena *set, Query_arena *backup);
1777
inline void set_current_stmt_binlog_row_based_if_mixed()
1780
If in a stored/function trigger, the caller should already have done the
1781
change. We test in_sub_stmt to prevent introducing bugs where people
1782
wouldn't ensure that, and would switch to row-based mode in the middle
1783
of executing a stored function/trigger (which is too late, see also
1784
reset_current_stmt_binlog_row_based()); this condition will make their
1785
tests fail and so force them to propagate the
1786
lex->binlog_row_based_if_mixed upwards to the caller.
1788
if ((variables.binlog_format == BINLOG_FORMAT_MIXED) &&
1790
current_stmt_binlog_row_based= true;
1792
inline void set_current_stmt_binlog_row_based()
1794
current_stmt_binlog_row_based= true;
1796
inline void clear_current_stmt_binlog_row_based()
1798
current_stmt_binlog_row_based= false;
1800
inline void reset_current_stmt_binlog_row_based()
1803
If there are temporary tables, don't reset back to
1804
statement-based. Indeed it could be that:
1805
CREATE TEMPORARY TABLE t SELECT UUID(); # row-based
1806
# and row-based does not store updates to temp tables
1808
INSERT INTO u SELECT * FROM t; # stmt-based
1809
and then the INSERT will fail as data inserted into t was not logged.
1810
So we continue with row-based until the temp table is dropped.
1811
If we are in a stored function or trigger, we mustn't reset in the
1812
middle of its execution (as the binary logging way of a stored function
1813
or trigger is decided when it starts executing, depending for example on
1814
the caller (for a stored function: if caller is SELECT or
1815
INSERT/UPDATE/DELETE...).
1817
Don't reset binlog format for NDB binlog injector thread.
1819
if ((temporary_tables == NULL) && (in_sub_stmt == 0))
1821
current_stmt_binlog_row_based=
1822
test(variables.binlog_format == BINLOG_FORMAT_ROW);
1530
1827
Set the current database; use deep copy of C-string.
1864
2157
class select_create: public select_insert {
1866
TableList *create_table;
2159
TABLE_LIST *create_table;
1867
2160
HA_CREATE_INFO *create_info;
1868
TableList *select_tables;
2161
TABLE_LIST *select_tables;
1869
2162
Alter_info *alter_info;
1871
2164
/* lock data for tmp table */
1872
DRIZZLE_LOCK *m_lock;
1873
/* m_lock or session->extra_lock */
1874
DRIZZLE_LOCK **m_plock;
2166
/* m_lock or thd->extra_lock */
2167
MYSQL_LOCK **m_plock;
1876
select_create (TableList *table_arg,
2169
select_create (TABLE_LIST *table_arg,
1877
2170
HA_CREATE_INFO *create_info_par,
1878
2171
Alter_info *alter_info_arg,
1879
2172
List<Item> &select_fields,enum_duplicates duplic, bool ignore,
1880
TableList *select_tables_arg)
2173
TABLE_LIST *select_tables_arg)
1881
2174
:select_insert (NULL, NULL, &select_fields, 0, 0, duplic, ignore),
1882
2175
create_table(table_arg),
1883
2176
create_info(create_info_par),
1920
2213
List<Item> save_copy_funcs;
1921
2214
Copy_field *copy_field, *copy_field_end;
1922
2215
Copy_field *save_copy_field, *save_copy_field_end;
1923
unsigned char *group_buff;
1924
2217
Item **items_to_copy; /* Fields in tmp table */
1925
2218
MI_COLUMNDEF *recinfo,*start_recinfo;
1927
2220
ha_rows end_write_records;
1928
2221
uint field_count,sum_func_count,func_count;
1929
uint32_t hidden_field_count;
2222
uint hidden_field_count;
1930
2223
uint group_parts,group_length,group_null_parts;
1931
2224
uint quick_group;
1932
2225
bool using_indirect_summary_function;
1933
2226
/* If >0 convert all blob fields to varchar(convert_blob_length) */
1934
uint32_t convert_blob_length;
1935
const CHARSET_INFO *table_charset;
2227
uint convert_blob_length;
2228
CHARSET_INFO *table_charset;
1936
2229
bool schema_table;
1938
2231
True if GROUP BY and its aggregate functions are already computed
2197
2497
class multi_update :public select_result_interceptor
2199
TableList *all_tables; /* query/update command tables */
2200
TableList *leaves; /* list of leves of join table tree */
2201
TableList *update_tables, *table_being_updated;
2202
Table **tmp_tables, *main_table, *table_to_update;
2499
TABLE_LIST *all_tables; /* query/update command tables */
2500
TABLE_LIST *leaves; /* list of leves of join table tree */
2501
TABLE_LIST *update_tables, *table_being_updated;
2502
TABLE **tmp_tables, *main_table, *table_to_update;
2203
2503
TMP_TABLE_PARAM *tmp_table_param;
2204
2504
ha_rows updated, found;
2205
2505
List <Item> *fields, *values;
2206
2506
List <Item> **fields_for_table, **values_for_table;
2207
uint32_t table_count;
2209
2509
List of tables referenced in the CHECK OPTION condition of
2210
2510
the updated view excluding the updated table.
2212
List <Table> unupdated_check_opt_tables;
2512
List <TABLE> unupdated_check_opt_tables;
2213
2513
Copy_field *copy_field;
2214
2514
enum enum_duplicates handle_duplicates;
2215
2515
bool do_update, trans_safe;
2285
2576
void add_diff_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var,
2286
2577
STATUS_VAR *dec_var);
2288
void close_connection(Session *session, uint32_t errcode, bool lock);
2290
/* Some inline functions for more speed */
2292
inline bool add_item_to_list(Session *session, Item *item)
2294
return session->lex->current_select->add_item_to_list(session, item);
2297
inline bool add_value_to_list(Session *session, Item *value)
2299
return session->lex->value_list.push_back(value);
2302
inline bool add_order_to_list(Session *session, Item *item, bool asc)
2304
return session->lex->current_select->add_order_to_list(session, item, asc);
2307
inline bool add_group_to_list(Session *session, Item *item, bool asc)
2309
return session->lex->current_select->add_group_to_list(session, item, asc);
2312
#endif /* DRIZZLE_SERVER */
2314
#endif /* DRIZZLED_SQL_CLASS_H */
2579
#endif /* MYSQL_SERVER */