21
21
#ifndef DRIZZLED_SESSION_H
22
22
#define DRIZZLED_SESSION_H
24
/* Classes in mysql */
26
24
#include "drizzled/plugin.h"
27
#include <drizzled/sql_locale.h>
25
#include "drizzled/sql_locale.h"
28
26
#include "drizzled/resource_context.h"
29
#include <drizzled/cursor.h>
30
#include <drizzled/current_session.h>
31
#include <drizzled/sql_error.h>
32
#include <drizzled/file_exchange.h>
33
#include <drizzled/select_result_interceptor.h>
34
#include <drizzled/xid.h>
27
#include "drizzled/cursor.h"
28
#include "drizzled/current_session.h"
29
#include "drizzled/sql_error.h"
30
#include "drizzled/file_exchange.h"
31
#include "drizzled/select_result_interceptor.h"
32
#include "drizzled/statistics_variables.h"
33
#include "drizzled/xid.h"
35
34
#include "drizzled/query_id.h"
36
35
#include "drizzled/named_savepoint.h"
37
36
#include "drizzled/transaction_context.h"
37
#include "drizzled/util/storable.h"
38
#include "drizzled/my_hash.h"
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>
46
#include "drizzled/internal/getrusage.h"
47
#include "drizzled/security_context.h"
48
#include "drizzled/open_tables_state.h"
49
#include "drizzled/internal_error_handler.h"
50
#include "drizzled/diagnostics_area.h"
51
#include "drizzled/plugin/authorization.h"
53
#include <boost/unordered_map.hpp>
54
#include <boost/thread/mutex.hpp>
55
#include <boost/thread/shared_mutex.hpp>
56
#include <boost/thread/condition_variable.hpp>
53
58
#define MIN_HANDSHAKE_SIZE 6
222
248
namespace drizzled
226
* Per-session local status counters
228
typedef struct system_status_var
230
uint64_t bytes_received;
233
ulong com_stat[(uint32_t) SQLCOM_END];
234
ulong created_tmp_disk_tables;
235
ulong created_tmp_tables;
236
ulong ha_commit_count;
237
ulong ha_delete_count;
238
ulong ha_read_first_count;
239
ulong ha_read_last_count;
240
ulong ha_read_key_count;
241
ulong ha_read_next_count;
242
ulong ha_read_prev_count;
243
ulong ha_read_rnd_count;
244
ulong ha_read_rnd_next_count;
245
ulong ha_rollback_count;
246
ulong ha_update_count;
247
ulong ha_write_count;
248
ulong ha_prepare_count;
249
ulong ha_savepoint_count;
250
ulong ha_savepoint_rollback_count;
252
/* KEY_CACHE parts. These are copies of the original */
253
ulong key_blocks_changed;
254
ulong key_blocks_used;
255
ulong key_cache_r_requests;
256
ulong key_cache_read;
257
ulong key_cache_w_requests;
258
ulong key_cache_write;
259
/* END OF KEY_CACHE parts */
261
ulong net_big_packet_count;
262
ulong select_full_join_count;
263
ulong select_full_range_join_count;
264
ulong select_range_count;
265
ulong select_range_check_count;
266
ulong select_scan_count;
267
ulong long_query_count;
268
ulong filesort_merge_passes;
269
ulong filesort_range_count;
271
ulong filesort_scan_count;
273
Number of statements sent from the client
279
SEE last_system_status_var DEFINITION BELOW.
281
Below 'last_system_status_var' are all variables which doesn't make any
282
sense to add to the /global/ status variable counter.
284
double last_query_cost;
288
This is used for 'SHOW STATUS'. It must be updated to the last ulong
289
variable in system_status_var which is makes sens to add to the global
293
#define last_system_status_var questions
295
251
void mark_transaction_to_rollback(Session *session, bool all);
297
extern pthread_mutex_t LOCK_xid_cache;
298
extern HASH xid_cache;
302
254
Storage engine specific thread local data.
367
326
enum enum_mark_columns mark_used_columns;
368
327
inline void* alloc(size_t size)
370
return alloc_root(mem_root,size);
329
return mem_root->alloc_root(size);
372
331
inline void* calloc(size_t size)
375
if ((ptr= alloc_root(mem_root,size)))
334
if ((ptr= mem_root->alloc_root(size)))
376
335
memset(ptr, 0, size);
379
338
inline char *strdup(const char *str)
381
return strdup_root(mem_root,str);
340
return mem_root->strdup_root(str);
383
342
inline char *strmake(const char *str, size_t size)
385
return strmake_root(mem_root,str,size);
344
return mem_root->strmake_root(str,size);
387
346
inline void *memdup(const void *str, size_t size)
389
return memdup_root(mem_root,str,size);
348
return mem_root->memdup_root(str, size);
391
350
inline void *memdup_w_gap(const void *str, size_t size, uint32_t gap)
394
if ((ptr= alloc_root(mem_root,size+gap)))
353
if ((ptr= mem_root->alloc_root(size + gap)))
395
354
memcpy(ptr,str,size);
413
391
LEX *lex; /**< parse tree descriptor */
414
397
/** query associated with this statement */
398
typedef boost::shared_ptr<const std::string> QueryString;
400
boost::shared_ptr<std::string> query;
402
// Never allow for a modification of this outside of the class. c_str()
403
// requires under some setup non const, you must copy the QueryString in
406
QueryString getQueryString() const
411
void resetQueryString()
413
return query.reset(new std::string);
417
We need to copy the lock on the string in order to make sure we have a stable string.
418
Once this is done we can use it to build a const char* which can be handed off for
419
a method to use (Innodb is currently the only engine using this).
421
const char *getQueryStringCopy(size_t &length)
423
QueryString tmp_string(getQueryString());
425
length= tmp_string->length();
426
char *to_return= strmake(tmp_string->c_str(), tmp_string->length());
418
431
Name of the current (default) database.
439
459
memory::Root warn_root; /**< Allocation area for warnings and errors */
440
460
plugin::Client *client; /**< Pointer to client object */
462
void setClient(plugin::Client *client_arg);
464
plugin::Client *getClient()
441
469
plugin::Scheduler *scheduler; /**< Pointer to scheduler object */
442
470
void *scheduler_arg; /**< Pointer to the optional scheduler argument */
443
HASH user_vars; /**< Hash of user variables defined during the session's lifetime */
444
struct system_variables variables; /**< Mutable local variables local to the session */
472
typedef boost::unordered_map< std::string, user_var_entry *, util::insensitive_hash, util::insensitive_equal_to> UserVars;
474
typedef std::pair< UserVars::iterator, UserVars::iterator > UserVarsRange;
475
UserVars user_vars; /**< Hash of user variables defined during the session's lifetime */
479
const UserVars &getUserVariables() const
484
drizzle_system_variables variables; /**< Mutable local variables local to the session */
445
485
struct system_status_var status_var; /**< Session-local status counters */
446
struct system_status_var *initial_status_var; /* used by show status */
447
486
THR_LOCK_INFO lock_info; /**< Locking information for this session */
448
487
THR_LOCK_OWNER main_lock_id; /**< To use for conventional queries */
449
488
THR_LOCK_OWNER *lock_id; /**< If not main_lock_id, points to the lock_id of a cursor. */
450
pthread_mutex_t LOCK_delete; /**< Locked before session is deleted */
453
* A peek into the query string for the session. This is a best effort
454
* delivery, there is no guarantee whether the content is meaningful.
456
char process_list_info[PROCESS_LIST_WIDTH+1];
459
491
* A pointer to the stack frame of the scheduler thread
541
592
ResourceContext *getResourceContext(const plugin::MonitoredInTransaction *monitored,
542
593
size_t index= 0);
596
* Structure used to manage "statement transactions" and
597
* "normal transactions". In autocommit mode, the normal transaction is
598
* equivalent to the statement transaction.
600
* Storage engines will be registered here when they participate in
601
* a transaction. No engine is registered more than once.
544
603
struct st_transactions {
545
604
std::deque<NamedSavepoint> savepoints;
546
TransactionContext all; ///< Trans since BEGIN WORK
547
TransactionContext stmt; ///< Trans for current statement
607
* The normal transaction (since BEGIN WORK).
609
* Contains a list of all engines that have participated in any of the
610
* statement transactions started within the context of the normal
613
* @note In autocommit mode, this is empty.
615
TransactionContext all;
618
* The statment transaction.
620
* Contains a list of all engines participating in the given statement.
622
* @note In autocommit mode, this will be used to commit/rollback the
623
* normal transaction.
625
TransactionContext stmt;
548
627
XID_STATE xid_state;
665
760
create_sort_index(); may differ from examined_row_count.
667
762
uint32_t row_count;
668
pthread_t real_id; /**< For debugging */
763
session_id_t thread_id;
670
764
uint32_t tmp_table;
671
uint32_t global_read_lock;
765
enum global_read_lock_t
768
GOT_GLOBAL_READ_LOCK= 1,
769
MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT= 2
772
global_read_lock_t _global_read_lock;
776
global_read_lock_t isGlobalReadLock() const
778
return _global_read_lock;
781
void setGlobalReadLock(global_read_lock_t arg)
783
_global_read_lock= arg;
786
DrizzleLock *lockTables(Table **tables, uint32_t count, uint32_t flags, bool *need_reopen);
787
bool lockGlobalReadLock();
788
bool lock_table_names(TableList *table_list);
789
bool lock_table_names_exclusively(TableList *table_list);
790
bool makeGlobalReadLockBlockCommit();
791
bool abortLockForThread(Table *table);
792
bool wait_if_global_read_lock(bool abort_on_refresh, bool is_not_commit);
793
int lock_table_name(TableList *table_list);
794
void abortLock(Table *table);
795
void removeLock(Table *table);
796
void unlockReadTables(DrizzleLock *sql_lock);
797
void unlockSomeTables(Table **table, uint32_t count);
798
void unlockTables(DrizzleLock *sql_lock);
799
void startWaitingGlobalReadLock();
800
void unlockGlobalReadLock();
803
int unlock_external(Table **table, uint32_t count);
804
int lock_external(Table **tables, uint32_t count);
805
bool wait_for_locked_table_names(TableList *table_list);
806
DrizzleLock *get_lock_data(Table **table_ptr, uint32_t count,
807
bool should_lock, Table **write_lock_used);
672
810
uint32_t server_status;
673
811
uint32_t open_options;
674
812
uint32_t select_number; /**< number of select (used for EXPLAIN) */
1249
1404
statement_message= in_message;
1408
* Sets the active Resultset message used by the Query Cache
1411
* @param[in] Pointer to the message
1413
void setResultsetMessage(message::Resultset *in_message)
1415
resultset= in_message;
1418
* reset the active Resultset message used by the Query Cache
1422
void resetResultsetMessage()
1252
1428
/** Pointers to memory managed by the ReplicationServices component */
1253
1429
message::Transaction *transaction_message;
1254
1430
message::Statement *statement_message;
1255
/** Microsecond timestamp of when Session connected */
1431
/* Pointer to the current resultset of Select query */
1432
message::Resultset *resultset;
1433
plugin::EventObserverList *session_event_observers;
1435
/* Schema observers are mapped to databases. */
1436
std::map<std::string, plugin::EventObserverList *> schema_event_observers;
1440
plugin::EventObserverList *getSessionObservers()
1442
return session_event_observers;
1445
void setSessionObservers(plugin::EventObserverList *observers)
1447
session_event_observers= observers;
1450
/* For schema event observers there is one set of observers per database. */
1451
plugin::EventObserverList *getSchemaObservers(const std::string &db_name)
1453
std::map<std::string, plugin::EventObserverList *>::iterator it;
1455
it= schema_event_observers.find(db_name);
1456
if (it == schema_event_observers.end())
1462
void setSchemaObservers(const std::string &db_name, plugin::EventObserverList *observers)
1464
std::map<std::string, plugin::EventObserverList *>::iterator it;
1466
it= schema_event_observers.find(db_name);
1467
if (it != schema_event_observers.end())
1468
schema_event_observers.erase(it);;
1471
schema_event_observers[db_name] = observers;
1476
/** Microsecond timestamp of when Session connected */
1256
1477
uint64_t connect_microseconds;
1257
1478
const char *proc_info;
1416
1608
void close_cached_table(Table *table);
1418
1610
/* Create a lock in the cache */
1419
Table *table_cache_insert_placeholder(const char *key, uint32_t key_length);
1611
table::Placeholder *table_cache_insert_placeholder(const TableIdentifier &identifier);
1420
1612
bool lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table);
1421
bool lock_table_name_if_not_cached(const char *db,
1422
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
/* Work with temporary tables */
1434
Table *find_temporary_table(TableList *table_list);
1435
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
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.
1614
typedef boost::unordered_map<std::string, message::Table, util::insensitive_hash, util::insensitive_equal_to> TableMessageCache;
1618
TableMessageCache table_message_cache;
1621
bool storeTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1622
bool removeTableMessage(const TableIdentifier &identifier);
1623
bool getTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1624
bool doesTableMessageExist(const TableIdentifier &identifier);
1625
bool renameTableMessage(const TableIdentifier &from, const TableIdentifier &to);
1459
void nukeTable(Table *table);
1629
TableMessages _table_message_cache;
1462
void dumpTemporaryTableNames(const char *id);
1463
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);
1632
TableMessages &getMessageCache()
1634
return _table_message_cache;
1469
1637
/* Reopen operations */
1470
1638
bool reopen_tables(bool get_locks, bool mark_share_as_old);
1471
bool reopen_name_locked_table(TableList* table_list, bool link_in);
1472
1639
bool close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders);
1474
void wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond);
1641
void wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond);
1475
1642
int setup_conds(TableList *leaves, COND **conds);
1476
1643
int lock_tables(TableList *tables, uint32_t count, bool *need_reopen);
1645
drizzled::util::Storable *getProperty(const std::string &arg)
1647
return life_properties[arg];
1651
bool setProperty(const std::string &arg, T *value)
1653
life_properties[arg]= value;
1480
1659
Return the default storage engine
1489
1668
if (variables.storage_engine)
1490
1669
return variables.storage_engine;
1491
1670
return global_system_variables.storage_engine;
1494
static void unlink(Session *session);
1673
void get_xid(DRIZZLE_XID *xid); // Innodb only
1675
table::Instance *getInstanceTable();
1676
table::Instance *getInstanceTable(List<CreateField> &field_list);
1681
if (getrusage(RUSAGE_THREAD, &usage))
1690
void setUsage(bool arg)
1695
const struct rusage &getUsage()
1701
// This lives throughout the life of Session
1703
PropertyMap life_properties;
1704
std::vector<table::Instance *> temporary_shares;
1705
struct rusage usage;
1500
1710
#define ESCAPE_CHARS "ntrb0ZN" // keep synchronous with READ_INFO::unescape