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
224
248
namespace drizzled
228
* Per-session local status counters
230
typedef struct system_status_var
232
uint64_t bytes_received;
235
ulong com_stat[(uint32_t) SQLCOM_END];
236
ulong created_tmp_disk_tables;
237
ulong created_tmp_tables;
238
ulong ha_commit_count;
239
ulong ha_delete_count;
240
ulong ha_read_first_count;
241
ulong ha_read_last_count;
242
ulong ha_read_key_count;
243
ulong ha_read_next_count;
244
ulong ha_read_prev_count;
245
ulong ha_read_rnd_count;
246
ulong ha_read_rnd_next_count;
247
ulong ha_rollback_count;
248
ulong ha_update_count;
249
ulong ha_write_count;
250
ulong ha_prepare_count;
251
ulong ha_savepoint_count;
252
ulong ha_savepoint_rollback_count;
254
/* KEY_CACHE parts. These are copies of the original */
255
ulong key_blocks_changed;
256
ulong key_blocks_used;
257
ulong key_cache_r_requests;
258
ulong key_cache_read;
259
ulong key_cache_w_requests;
260
ulong key_cache_write;
261
/* END OF KEY_CACHE parts */
263
ulong net_big_packet_count;
264
ulong select_full_join_count;
265
ulong select_full_range_join_count;
266
ulong select_range_count;
267
ulong select_range_check_count;
268
ulong select_scan_count;
269
ulong long_query_count;
270
ulong filesort_merge_passes;
271
ulong filesort_range_count;
273
ulong filesort_scan_count;
275
Number of statements sent from the client
281
SEE last_system_status_var DEFINITION BELOW.
283
Below 'last_system_status_var' are all variables which doesn't make any
284
sense to add to the /global/ status variable counter.
286
double last_query_cost;
290
This is used for 'SHOW STATUS'. It must be updated to the last ulong
291
variable in system_status_var which is makes sens to add to the global
295
#define last_system_status_var questions
297
251
void mark_transaction_to_rollback(Session *session, bool all);
299
extern pthread_mutex_t LOCK_xid_cache;
300
extern HASH xid_cache;
304
254
Storage engine specific thread local data.
369
324
enum enum_mark_columns mark_used_columns;
370
325
inline void* alloc(size_t size)
372
return alloc_root(mem_root,size);
327
return mem_root->alloc_root(size);
374
329
inline void* calloc(size_t size)
377
if ((ptr= alloc_root(mem_root,size)))
332
if ((ptr= mem_root->alloc_root(size)))
378
333
memset(ptr, 0, size);
381
336
inline char *strdup(const char *str)
383
return strdup_root(mem_root,str);
338
return mem_root->strdup_root(str);
385
340
inline char *strmake(const char *str, size_t size)
387
return strmake_root(mem_root,str,size);
342
return mem_root->strmake_root(str,size);
389
344
inline void *memdup(const void *str, size_t size)
391
return memdup_root(mem_root,str,size);
346
return mem_root->memdup_root(str, size);
393
348
inline void *memdup_w_gap(const void *str, size_t size, uint32_t gap)
396
if ((ptr= alloc_root(mem_root,size+gap)))
351
if ((ptr= mem_root->alloc_root(size + gap)))
397
352
memcpy(ptr,str,size);
441
422
memory::Root warn_root; /**< Allocation area for warnings and errors */
442
423
plugin::Client *client; /**< Pointer to client object */
425
void setClient(plugin::Client *client_arg);
427
plugin::Client *getClient()
443
432
plugin::Scheduler *scheduler; /**< Pointer to scheduler object */
444
433
void *scheduler_arg; /**< Pointer to the optional scheduler argument */
445
HASH user_vars; /**< Hash of user variables defined during the session's lifetime */
446
struct system_variables variables; /**< Mutable local variables local to the session */
435
typedef boost::unordered_map< std::string, user_var_entry *, util::insensitive_hash, util::insensitive_equal_to> UserVars;
436
typedef std::pair< UserVars::iterator, UserVars::iterator > UserVarsRange;
437
UserVars user_vars; /**< Hash of user variables defined during the session's lifetime */
440
drizzle_system_variables variables; /**< Mutable local variables local to the session */
447
441
struct system_status_var status_var; /**< Session-local status counters */
448
struct system_status_var *initial_status_var; /* used by show status */
449
442
THR_LOCK_INFO lock_info; /**< Locking information for this session */
450
443
THR_LOCK_OWNER main_lock_id; /**< To use for conventional queries */
451
444
THR_LOCK_OWNER *lock_id; /**< If not main_lock_id, points to the lock_id of a cursor. */
452
pthread_mutex_t LOCK_delete; /**< Locked before session is deleted */
446
boost::mutex LOCK_delete; /**< Locked before session is deleted */
454
void unlockForDelete()
456
LOCK_delete.unlock();
455
460
* A peek into the query string for the session. This is a best effort
538
572
ResourceContext *getResourceContext(const plugin::MonitoredInTransaction *monitored,
539
573
size_t index= 0);
576
* Structure used to manage "statement transactions" and
577
* "normal transactions". In autocommit mode, the normal transaction is
578
* equivalent to the statement transaction.
580
* Storage engines will be registered here when they participate in
581
* a transaction. No engine is registered more than once.
541
583
struct st_transactions {
542
584
std::deque<NamedSavepoint> savepoints;
543
TransactionContext all; ///< Trans since BEGIN WORK
544
TransactionContext stmt; ///< Trans for current statement
587
* The normal transaction (since BEGIN WORK).
589
* Contains a list of all engines that have participated in any of the
590
* statement transactions started within the context of the normal
593
* @note In autocommit mode, this is empty.
595
TransactionContext all;
598
* The statment transaction.
600
* Contains a list of all engines participating in the given statement.
602
* @note In autocommit mode, this will be used to commit/rollback the
603
* normal transaction.
605
TransactionContext stmt;
545
607
XID_STATE xid_state;
662
724
create_sort_index(); may differ from examined_row_count.
664
726
uint32_t row_count;
665
pthread_t real_id; /**< For debugging */
727
session_id_t thread_id;
667
728
uint32_t tmp_table;
668
uint32_t global_read_lock;
729
enum global_read_lock_t
732
GOT_GLOBAL_READ_LOCK= 1,
733
MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT= 2
736
global_read_lock_t _global_read_lock;
740
global_read_lock_t isGlobalReadLock() const
742
return _global_read_lock;
745
void setGlobalReadLock(global_read_lock_t arg)
747
_global_read_lock= arg;
750
DrizzleLock *lockTables(Table **tables, uint32_t count, uint32_t flags, bool *need_reopen);
751
bool lockGlobalReadLock();
752
bool lock_table_names(TableList *table_list);
753
bool lock_table_names_exclusively(TableList *table_list);
754
bool makeGlobalReadLockBlockCommit();
755
bool abortLockForThread(Table *table);
756
bool wait_if_global_read_lock(bool abort_on_refresh, bool is_not_commit);
757
int lock_table_name(TableList *table_list);
758
void abortLock(Table *table);
759
void removeLock(Table *table);
760
void unlockReadTables(DrizzleLock *sql_lock);
761
void unlockSomeTables(Table **table, uint32_t count);
762
void unlockTables(DrizzleLock *sql_lock);
763
void startWaitingGlobalReadLock();
764
void unlockGlobalReadLock();
767
int unlock_external(Table **table, uint32_t count);
768
int lock_external(Table **tables, uint32_t count);
769
bool wait_for_locked_table_names(TableList *table_list);
770
DrizzleLock *get_lock_data(Table **table_ptr, uint32_t count,
771
bool should_lock, Table **write_lock_used);
669
774
uint32_t server_status;
670
775
uint32_t open_options;
671
776
uint32_t select_number; /**< number of select (used for EXPLAIN) */
1190
1313
* Current implementation does not depend on that, but future changes
1191
1314
* should be done with this in mind;
1193
* @param Scrambled password received from client
1194
* @param Length of scrambled password
1195
* @param Database name to connect to, may be NULL
1316
* @param passwd Scrambled password received from client
1317
* @param db Database name to connect to, may be NULL
1197
bool checkUser(const char *passwd, uint32_t passwd_len, const char *db);
1319
bool checkUser(const std::string &passwd, const std::string &db);
1200
1322
* Returns the timestamp (in microseconds) of when the Session
1246
1376
statement_message= in_message;
1380
* Sets the active Resultset message used by the Query Cache
1383
* @param[in] Pointer to the message
1385
void setResultsetMessage(message::Resultset *in_message)
1387
resultset= in_message;
1390
* reset the active Resultset message used by the Query Cache
1394
void resetResultsetMessage()
1249
1400
/** Pointers to memory managed by the ReplicationServices component */
1250
1401
message::Transaction *transaction_message;
1251
1402
message::Statement *statement_message;
1252
/** Microsecond timestamp of when Session connected */
1403
/* Pointer to the current resultset of Select query */
1404
message::Resultset *resultset;
1405
plugin::EventObserverList *session_event_observers;
1407
/* Schema observers are mapped to databases. */
1408
std::map<std::string, plugin::EventObserverList *> schema_event_observers;
1412
plugin::EventObserverList *getSessionObservers()
1414
return session_event_observers;
1417
void setSessionObservers(plugin::EventObserverList *observers)
1419
session_event_observers= observers;
1422
/* For schema event observers there is one set of observers per database. */
1423
plugin::EventObserverList *getSchemaObservers(const std::string &db_name)
1425
std::map<std::string, plugin::EventObserverList *>::iterator it;
1427
it= schema_event_observers.find(db_name);
1428
if (it == schema_event_observers.end())
1434
void setSchemaObservers(const std::string &db_name, plugin::EventObserverList *observers)
1436
std::map<std::string, plugin::EventObserverList *>::iterator it;
1438
it= schema_event_observers.find(db_name);
1439
if (it != schema_event_observers.end())
1440
schema_event_observers.erase(it);;
1443
schema_event_observers[db_name] = observers;
1448
/** Microsecond timestamp of when Session connected */
1253
1449
uint64_t connect_microseconds;
1254
1450
const char *proc_info;
1384
1581
bool openTablesLock(TableList *tables);
1387
* Open all tables in list and process derived tables
1389
* @param Pointer to a list of tables for open
1390
* @param Bitmap of flags to modify how the tables will be open:
1391
* DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
1392
* done a flush or namelock on it.
1401
* This is to be used on prepare stage when you don't read any
1402
* data from the tables.
1404
bool openTables(TableList *tables, uint32_t flags= 0);
1406
1583
int open_tables_from_list(TableList **start, uint32_t *counter, uint32_t flags= 0);
1408
1585
Table *openTableLock(TableList *table_list, thr_lock_type lock_type);
1413
1590
void close_cached_table(Table *table);
1415
1592
/* Create a lock in the cache */
1416
Table *table_cache_insert_placeholder(const char *key, uint32_t key_length);
1593
table::Placeholder *table_cache_insert_placeholder(const TableIdentifier &identifier);
1417
1594
bool lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table);
1418
bool lock_table_name_if_not_cached(const char *db,
1419
const char *table_name, Table **table);
1421
typedef drizzled::hash_map<std::string, message::Table> TableMessageCache;
1596
typedef boost::unordered_map<std::string, message::Table, util::insensitive_hash, util::insensitive_equal_to> TableMessageCache;
1422
1597
TableMessageCache table_message_cache;
1424
bool storeTableMessage(TableIdentifier &identifier, message::Table &table_message);
1425
bool removeTableMessage(TableIdentifier &identifier);
1426
bool getTableMessage(TableIdentifier &identifier, message::Table &table_message);
1427
bool doesTableMessageExist(TableIdentifier &identifier);
1428
bool renameTableMessage(TableIdentifier &from, TableIdentifier &to);
1599
bool storeTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1600
bool removeTableMessage(const TableIdentifier &identifier);
1601
bool getTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1602
bool doesTableMessageExist(const TableIdentifier &identifier);
1603
bool renameTableMessage(const TableIdentifier &from, const TableIdentifier &to);
1430
1605
/* Work with temporary tables */
1431
Table *find_temporary_table(TableList *table_list);
1432
Table *find_temporary_table(const char *db, const char *table_name);
1433
Table *find_temporary_table(TableIdentifier &identifier);
1606
Table *find_temporary_table(const TableIdentifier &identifier);
1435
1608
void doGetTableNames(CachedDirectory &directory,
1436
SchemaIdentifier &schema_identifier,
1437
std::set<std::string>& set_of_names);
1438
void doGetTableNames(SchemaIdentifier &schema_identifier,
1439
std::set<std::string>& set_of_names);
1441
int doGetTableDefinition(drizzled::TableIdentifier &identifier,
1609
const SchemaIdentifier &schema_identifier,
1610
std::set<std::string>& set_of_names);
1611
void doGetTableNames(const SchemaIdentifier &schema_identifier,
1612
std::set<std::string>& set_of_names);
1614
void doGetTableIdentifiers(CachedDirectory &directory,
1615
const SchemaIdentifier &schema_identifier,
1616
TableIdentifiers &set_of_identifiers);
1617
void doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
1618
TableIdentifiers &set_of_identifiers);
1620
int doGetTableDefinition(const drizzled::TableIdentifier &identifier,
1442
1621
message::Table &table_proto);
1443
bool doDoesTableExist(TableIdentifier &identifier);
1622
bool doDoesTableExist(const drizzled::TableIdentifier &identifier);
1445
1625
void close_temporary_tables();
1446
1627
void close_temporary_table(Table *table);
1447
1628
// The method below just handles the de-allocation of the table. In
1448
1629
// a better memory type world, this would not be needed.
1453
1634
void dumpTemporaryTableNames(const char *id);
1454
int drop_temporary_table(TableList *table_list);
1635
int drop_temporary_table(const drizzled::TableIdentifier &identifier);
1455
1636
bool rm_temporary_table(plugin::StorageEngine *base, TableIdentifier &identifier);
1456
bool rm_temporary_table(TableIdentifier &identifier);
1637
bool rm_temporary_table(TableIdentifier &identifier, bool best_effort= false);
1457
1638
Table *open_temporary_table(TableIdentifier &identifier,
1458
1639
bool link_in_list= true);
1460
1641
/* Reopen operations */
1461
1642
bool reopen_tables(bool get_locks, bool mark_share_as_old);
1462
bool reopen_name_locked_table(TableList* table_list, bool link_in);
1463
1643
bool close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders);
1465
void wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond);
1645
void wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond);
1466
1646
int setup_conds(TableList *leaves, COND **conds);
1467
1647
int lock_tables(TableList *tables, uint32_t count, bool *need_reopen);
1649
drizzled::util::Storable *getProperty(const std::string &arg)
1651
return life_properties[arg];
1655
bool setProperty(const std::string &arg, T *value)
1657
life_properties[arg]= value;
1471
1663
Return the default storage engine
1480
1672
if (variables.storage_engine)
1481
1673
return variables.storage_engine;
1482
1674
return global_system_variables.storage_engine;
1485
1677
static void unlink(Session *session);
1679
void get_xid(DRIZZLE_XID *xid); // Innodb only
1681
table::Instance *getInstanceTable();
1682
table::Instance *getInstanceTable(List<CreateField> &field_list);
1687
if (getrusage(RUSAGE_THREAD, &usage))
1696
void setUsage(bool arg)
1701
const struct rusage &getUsage()
1707
// This lives throughout the life of Session
1709
PropertyMap life_properties;
1710
std::vector<table::Instance *> temporary_shares;
1711
struct rusage usage;
1491
1716
#define ESCAPE_CHARS "ntrb0ZN" // keep synchronous with READ_INFO::unescape
1563
1791
static const std::bitset<CF_BIT_SIZE> CF_SHOW_TABLE_COMMAND(1 << CF_BIT_SHOW_TABLE_COMMAND);
1564
1792
static const std::bitset<CF_BIT_SIZE> CF_WRITE_LOGS_COMMAND(1 << CF_BIT_WRITE_LOGS_COMMAND);
1566
/* Functions in sql_class.cc */
1567
void add_to_status(system_status_var *to_var, system_status_var *from_var);
1569
void add_diff_to_status(system_status_var *to_var, system_status_var *from_var,
1570
system_status_var *dec_var);
1572
1794
} /* namespace drizzled */
1574
1796
#endif /* DRIZZLED_SESSION_H */