24
24
/* Classes in mysql */
26
#include "drizzled/plugin.h"
26
#include <drizzled/plugin/protocol.h>
27
27
#include <drizzled/sql_locale.h>
28
#include "drizzled/resource_context.h"
29
#include <drizzled/cursor.h>
28
#include <drizzled/ha_trx_info.h>
29
#include <mysys/my_alloc.h>
30
#include <mysys/my_tree.h>
31
#include <drizzled/handler.h>
30
32
#include <drizzled/current_session.h>
31
33
#include <drizzled/sql_error.h>
32
34
#include <drizzled/file_exchange.h>
33
35
#include <drizzled/select_result_interceptor.h>
36
#include <drizzled/authentication.h>
37
#include <drizzled/db.h>
34
38
#include <drizzled/xid.h>
35
#include "drizzled/query_id.h"
36
#include "drizzled/named_savepoint.h"
37
#include "drizzled/transaction_context.h"
45
#include <drizzled/security_context.h>
46
#include <drizzled/open_tables_state.h>
48
#include <drizzled/internal_error_handler.h>
49
#include <drizzled/diagnostics_area.h>
51
#include <drizzled/plugin/authorization.h>
53
44
#define MIN_HANDSHAKE_SIZE 6
70
struct st_my_thread_var;
73
46
class Lex_input_stream;
74
47
class user_var_entry;
295
265
void mark_transaction_to_rollback(Session *session, bool all);
297
extern pthread_mutex_t LOCK_xid_cache;
298
extern HASH xid_cache;
302
Storage engine specific thread local data.
307
Storage engine specific thread local data.
308
Lifetime: one user connection.
312
* Resource contexts for both the "statement" and "normal"
315
* Resource context at index 0:
317
* Life time: one statement within a transaction. If @@autocommit is
318
* on, also represents the entire transaction.
320
* Resource context at index 1:
322
* Life time: one transaction within a connection.
326
* If the storage engine does not participate in a transaction,
327
* there will not be a resource context.
329
drizzled::ResourceContext resource_context[2];
331
Ha_data() :ha_ptr(NULL) {}
335
* Represents a client connection to the database server.
337
* Contains the client/server protocol object, the current statement
338
* being executed, local-to-session variables and status counters, and
339
* a host of other information.
343
* The Session class should have a vector of Statement object pointers which
344
* comprise the statements executed on the Session. Until this architectural
345
* change is done, we can forget about parallel operations inside a session.
349
* Make member variables private and have inlined accessors and setters. Hide
350
* all member variables that are not critical to non-internal operations of the
268
* Single command executed against this connection.
272
* One connection can contain a lot of simultaneously running statements,
273
* some of which could be prepared, that is, contain placeholders.
275
* To perform some action with statement we reset Session part to the state of
276
* that statement, do the action, and then save back modified state from Session
277
* to the statement. It will be changed in near future, and Statement will
278
* be used explicitly.
282
* The above comment is bullshit in Drizzle. See TODO markers on Session to
283
* completely detach the inheritance of Session from Statement.
353
class Session : public Open_tables_state
287
Statement(const Statement &rhs); /* not implemented: */
288
Statement &operator=(const Statement &rhs); /* non-copyable */
291
* List of items created in the parser for this query. Every item puts
292
* itself to the list on creation (see Item::Item() for details))
295
MEM_ROOT *mem_root; /**< Pointer to current memroot */
297
* Uniquely identifies each statement object in thread scope; change during
298
* statement lifetime.
300
* @todo should be const
357
305
MARK_COLUMNS_NONE: Means mark_used_colums is not set and no indicator to
358
306
handler of fields used is set
367
315
enum enum_mark_columns mark_used_columns;
317
LEX *lex; /**< parse tree descriptor */
319
Points to the query associated with this statement. It's const, but
320
we need to declare it char * because all table handlers are written
321
in C and need to point to it.
323
Note that (A) if we set query = NULL, we must at the same time set
324
query_length = 0, and protect the whole operation with the
325
LOCK_thread_count mutex. And (B) we are ONLY allowed to set query to a
326
non-NULL value if its previous value is NULL. We do not need to protect
327
operation (B) with any mutex. To avoid crashes in races, if we do not
328
know that session->query cannot change at the moment, one should print
329
session->query like this:
330
(1) reserve the LOCK_thread_count mutex;
331
(2) check if session->query is NULL;
332
(3) if not NULL, then print at most session->query_length characters from
333
it. We will see the query_length field as either 0, or the right value
335
Assuming that the write and read of an n-bit memory field in an n-bit
336
computer is atomic, we can avoid races in the above way.
337
This printing is needed at least in SHOW PROCESSLIST and SHOW INNODB
341
uint32_t query_length; /**< current query length */
344
Name of the current (default) database.
346
If there is the current (default) database, "db" contains its name. If
347
there is no current (default) database, "db" is NULL and "db_length" is
348
0. In other words, "db", "db_length" must either be NULL, or contain a
351
@note this attribute is set and alloced by the slave SQL thread (for
352
the Session of that thread); that thread is (and must remain, for now) the
353
only responsible for freeing this member.
356
uint32_t db_length; /**< Length of current schema name */
360
/* This constructor is called for backup statements */
363
Statement(LEX *lex_arg, MEM_ROOT *mem_root_arg, uint32_t id_arg)
366
mem_root(mem_root_arg),
368
mark_used_columns(MARK_COLUMNS_READ),
375
virtual ~Statement() {}
368
376
inline void* alloc(size_t size)
370
378
return alloc_root(mem_root,size);
398
406
/** Frees all items attached to this Statement */
399
407
void free_items();
401
* List of items created in the parser for this query. Every item puts
402
* itself to the list on creation (see Item::Item() for details))
405
memory::Root *mem_root; /**< Pointer to current memroot */
407
* Uniquely identifies each statement object in thread scope; change during
408
* statement lifetime.
410
* @todo should be const
413
LEX *lex; /**< parse tree descriptor */
414
/** query associated with this statement */
418
Name of the current (default) database.
420
If there is the current (default) database, "db" contains its name. If
421
there is no current (default) database, "db" is NULL and "db_length" is
422
0. In other words, "db", "db_length" must either be NULL, or contain a
425
@note this attribute is set and alloced by the slave SQL thread (for
426
the Session of that thread); that thread is (and must remain, for now) the
427
only responsible for freeing this member.
412
struct st_savepoint *prev;
415
Ha_trx_info *ha_list;
418
extern pthread_mutex_t LOCK_xid_cache;
419
extern HASH xid_cache;
421
#include <drizzled/security_context.h>
422
#include <drizzled/open_tables_state.h>
424
#include <drizzled/internal_error_handler.h>
425
#include <drizzled/diagnostics_area.h>
428
Storage engine specific thread local data.
433
Storage engine specific thread local data.
434
Lifetime: one user connection.
438
0: Life time: one statement within a transaction. If @@autocommit is
439
on, also represents the entire transaction.
440
@sa trans_register_ha()
442
1: Life time: one transaction within a connection.
443
If the storage engine does not participate in a transaction,
444
this should not be used.
445
@sa trans_register_ha()
447
Ha_trx_info ha_info[2];
449
Ha_data() :ha_ptr(NULL) {}
453
* Represents a client connection to the database server.
455
* Contains the client/server protocol object, the current statement
456
* being executed, local-to-session variables and status counters, and
457
* a host of other information.
461
* Session should NOT inherit from Statement, but rather it should have a
462
* vector of Statement object pointers which comprise the statements executed
463
* on the Session. Until this architectural change is done, we can forget
464
* about parallel operations inside a session.
468
* Make member variables private and have inlined accessors and setters. Hide
469
* all member variables that are not critical to non-internal operations of the
472
class Session :public Statement, public Open_tables_state
432
476
Constant for Session::where initialization in the beginning of every query.
456
500
char process_list_info[PROCESS_LIST_WIDTH+1];
459
* A pointer to the stack frame of the scheduler thread
503
* A pointer to the stack frame of handle_one_connection(),
460
504
* which is called first in the thread for handling a client
462
506
char *thread_stack;
465
SecurityContext security_ctx;
467
inline void checkSentry() const
469
assert(this->dbug_sentry == Session_SENTRY_MAGIC);
472
const SecurityContext& getSecurityContext() const
477
SecurityContext& getSecurityContext()
483
* Is this session viewable by the current user?
485
bool isViewable() const
487
return plugin::Authorization::isAuthorized(current_session->getSecurityContext(),
510
Some members of Session (currently 'Statement::db',
511
'query') are set and alloced by the slave SQL thread
512
(for the Session of that thread); that thread is (and must remain, for now)
513
the only responsible for freeing these 3 members. If you add members
514
here, and you add code to set them in replication, don't forget to
515
free_them_and_set_them_to_0 in replication properly. For details see
516
the 'err:' label of the handle_slave_sql() in sql/slave.cc.
518
@see handle_slave_sql
520
Security_context security_ctx;
493
523
Used in error messages to tell user in what part of MySQL we found an
523
555
Both of the following container points in session will be converted to an API.
527
558
/* container for handler's private per-connection data */
528
std::vector<Ha_data> ha_data;
530
Id of current query. Statement can be reused to execute several queries
531
query_id is global in context of the whole MySQL server.
532
ID is automatically generated from an atomic counter.
533
It's used in Cursor code for various purposes: to check which columns
534
from table are necessary for this select, to check if it's necessary to
535
update auto-updatable fields (like auto_increment and timestamp).
538
query_id_t warn_query_id;
540
void **getEngineData(const plugin::MonitoredInTransaction *monitored);
541
ResourceContext *getResourceContext(const plugin::MonitoredInTransaction *monitored,
559
Ha_data ha_data[MAX_HA];
561
/* container for replication data */
562
void *replication_data;
544
564
struct st_transactions {
545
std::deque<NamedSavepoint> savepoints;
546
TransactionContext all; ///< Trans since BEGIN WORK
547
TransactionContext stmt; ///< Trans for current statement
565
SAVEPOINT *savepoints;
566
Session_TRANS all; // Trans since BEGIN WORK
567
Session_TRANS stmt; // Trans for current statement
568
bool on; // see ha_enable_transaction()
548
569
XID_STATE xid_state;
572
Tables changed in transaction (that must be invalidated in query cache).
573
List contain only transactional tables, that not invalidated in query
574
cache (instead of full list of changed in transaction tables).
576
CHANGED_TableList* changed_tables;
577
MEM_ROOT mem_root; // Transaction-life memory allocation pool
582
free_root(&mem_root,MYF(MY_KEEP_PREALLOC));
586
memset(this, 0, sizeof(*this));
587
xid_state.xid.null();
588
init_sql_alloc(&mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
562
591
Field *dup_field;
563
592
sigset_t signals;
973
1005
bool authenticate();
978
* This will initialize the session and begin the command loop.
983
* Schedule a session to be run on the default scheduler.
988
1008
For enter_cond() / exit_cond() to work the mutex must be got before
989
1009
enter_cond(); this mutex is then released by exit_cond().
990
1010
Usage must be: lock mutex; enter_cond(); your code; exit_cond().
992
const char* enter_cond(pthread_cond_t *cond, pthread_mutex_t* mutex, const char* msg);
993
void exit_cond(const char* old_msg);
1012
inline const char* enter_cond(pthread_cond_t *cond, pthread_mutex_t* mutex, const char* msg)
1014
const char* old_msg = get_proc_info();
1015
safe_mutex_assert_owner(mutex);
1016
mysys_var->current_mutex = mutex;
1017
mysys_var->current_cond = cond;
1018
this->set_proc_info(msg);
1021
inline void exit_cond(const char* old_msg)
1024
Putting the mutex unlock in exit_cond() ensures that
1025
mysys_var->current_mutex is always unlocked _before_ mysys_var->mutex is
1026
locked (if that would not be the case, you'll get a deadlock if someone
1027
does a Session::awake() on you).
1029
pthread_mutex_unlock(mysys_var->current_mutex);
1030
pthread_mutex_lock(&mysys_var->mutex);
1031
mysys_var->current_mutex = 0;
1032
mysys_var->current_cond = 0;
1033
this->set_proc_info(old_msg);
1034
pthread_mutex_unlock(&mysys_var->mutex);
995
1036
inline time_t query_start() { return start_time; }
996
1037
inline void set_time()
1208
1269
return connect_microseconds;
1212
* Returns a pointer to the active Transaction message for this
1213
* Session being managed by the ReplicationServices component, or
1214
* NULL if no active message.
1216
message::Transaction *getTransactionMessage() const
1218
return transaction_message;
1222
* Returns a pointer to the active Statement message for this
1223
* Session, or NULL if no active message.
1225
message::Statement *getStatementMessage() const
1227
return statement_message;
1231
* Sets the active transaction message used by the ReplicationServices
1234
* @param[in] Pointer to the message
1236
void setTransactionMessage(message::Transaction *in_message)
1238
transaction_message= in_message;
1242
* Sets the active statement message used by the ReplicationServices
1245
* @param[in] Pointer to the message
1247
void setStatementMessage(message::Statement *in_message)
1249
statement_message= in_message;
1252
/** Pointers to memory managed by the ReplicationServices component */
1253
message::Transaction *transaction_message;
1254
message::Statement *statement_message;
1255
1273
/** Microsecond timestamp of when Session connected */
1256
1274
uint64_t connect_microseconds;
1257
1275
const char *proc_info;
1404
1419
* This is to be used on prepare stage when you don't read any
1405
1420
* data from the tables.
1407
bool openTables(TableList *tables, uint32_t flags= 0);
1409
int open_tables_from_list(TableList **start, uint32_t *counter, uint32_t flags= 0);
1411
Table *openTableLock(TableList *table_list, thr_lock_type lock_type);
1412
Table *openTable(TableList *table_list, bool *refresh, uint32_t flags= 0);
1422
bool open_normal_and_derived_tables(TableList *tables, uint32_t flags);
1423
int open_tables_from_list(TableList **start, uint32_t *counter, uint32_t flags);
1424
Table *open_ltable(TableList *table_list, thr_lock_type lock_type);
1425
Table *open_table(TableList *table_list, bool *refresh, uint32_t flags);
1414
1426
void unlink_open_table(Table *find);
1415
void drop_open_table(Table *table, TableIdentifier &identifier);
1427
void drop_open_table(Table *table, const char *db_name,
1428
const char *table_name);
1416
1429
void close_cached_table(Table *table);
1418
1431
/* Create a lock in the cache */
1419
1432
Table *table_cache_insert_placeholder(const char *key, uint32_t key_length);
1420
bool lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table);
1421
bool lock_table_name_if_not_cached(const char *db,
1433
bool lock_table_name_if_not_cached(const char *db,
1422
1434
const char *table_name, Table **table);
1424
typedef drizzled::hash_map<std::string, message::Table> TableMessageCache;
1425
TableMessageCache table_message_cache;
1427
bool storeTableMessage(TableIdentifier &identifier, message::Table &table_message);
1428
bool removeTableMessage(TableIdentifier &identifier);
1429
bool getTableMessage(TableIdentifier &identifier, message::Table &table_message);
1430
bool doesTableMessageExist(TableIdentifier &identifier);
1431
bool renameTableMessage(TableIdentifier &from, TableIdentifier &to);
1433
1436
/* Work with temporary tables */
1434
1437
Table *find_temporary_table(TableList *table_list);
1435
1438
Table *find_temporary_table(const char *db, const char *table_name);
1436
Table *find_temporary_table(TableIdentifier &identifier);
1438
void doGetTableNames(CachedDirectory &directory,
1439
SchemaIdentifier &schema_identifier,
1440
std::set<std::string>& set_of_names);
1441
void doGetTableNames(SchemaIdentifier &schema_identifier,
1442
std::set<std::string>& set_of_names);
1444
void doGetTableIdentifiers(CachedDirectory &directory,
1445
SchemaIdentifier &schema_identifier,
1446
TableIdentifiers &set_of_identifiers);
1447
void doGetTableIdentifiers(SchemaIdentifier &schema_identifier,
1448
TableIdentifiers &set_of_identifiers);
1450
int doGetTableDefinition(drizzled::TableIdentifier &identifier,
1451
message::Table &table_proto);
1452
bool doDoesTableExist(TableIdentifier &identifier);
1454
1439
void close_temporary_tables();
1455
void close_temporary_table(Table *table);
1456
// The method below just handles the de-allocation of the table. In
1457
// a better memory type world, this would not be needed.
1459
void nukeTable(Table *table);
1462
void dumpTemporaryTableNames(const char *id);
1440
void close_temporary_table(Table *table, bool free_share, bool delete_table);
1463
1441
int drop_temporary_table(TableList *table_list);
1464
bool rm_temporary_table(plugin::StorageEngine *base, TableIdentifier &identifier);
1465
bool rm_temporary_table(TableIdentifier &identifier);
1466
Table *open_temporary_table(TableIdentifier &identifier,
1467
bool link_in_list= true);
1469
1443
/* Reopen operations */
1470
1444
bool reopen_tables(bool get_locks, bool mark_share_as_old);
1471
1445
bool reopen_name_locked_table(TableList* table_list, bool link_in);
1472
bool close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders);
1474
1447
void wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond);
1475
int setup_conds(TableList *leaves, COND **conds);
1476
int lock_tables(TableList *tables, uint32_t count, bool *need_reopen);
1480
Return the default storage engine
1482
@param getDefaultStorageEngine()
1485
pointer to plugin::StorageEngine
1487
plugin::StorageEngine *getDefaultStorageEngine()
1489
if (variables.storage_engine)
1490
return variables.storage_engine;
1491
return global_system_variables.storage_engine;
1494
static void unlink(Session *session);
1500
1452
#define ESCAPE_CHARS "ntrb0ZN" // keep synchronous with READ_INFO::unescape
1502
} /* namespace drizzled */
1504
/** @TODO why is this in the middle of the file */
1505
1454
#include <drizzled/select_to_file.h>
1506
1455
#include <drizzled/select_export.h>
1507
1456
#include <drizzled/select_dump.h>
1508
1457
#include <drizzled/select_insert.h>
1509
1458
#include <drizzled/select_create.h>
1459
#include <plugin/myisam/myisam.h>
1510
1460
#include <drizzled/tmp_table_param.h>
1511
1461
#include <drizzled/select_union.h>
1512
1462
#include <drizzled/select_subselect.h>