21
21
#ifndef DRIZZLED_SESSION_H
22
22
#define DRIZZLED_SESSION_H
24
/* Classes in mysql */
24
26
#include "drizzled/plugin.h"
25
#include "drizzled/sql_locale.h"
27
#include <drizzled/sql_locale.h>
26
28
#include "drizzled/resource_context.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"
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/statistics_variables.h>
35
#include <drizzled/xid.h>
34
36
#include "drizzled/query_id.h"
35
37
#include "drizzled/named_savepoint.h"
36
38
#include "drizzled/transaction_context.h"
37
#include "drizzled/util/storable.h"
38
40
#include "drizzled/my_hash.h"
39
#include "drizzled/pthread_globals.h"
42
#include <sys/resource.h>
50
#include "drizzled/security_context.h"
51
#include "drizzled/open_tables_state.h"
52
#include "drizzled/internal_error_handler.h"
53
#include "drizzled/diagnostics_area.h"
54
#include "drizzled/plugin/authorization.h"
48
#include <drizzled/security_context.h>
49
#include <drizzled/open_tables_state.h>
51
#include <drizzled/internal_error_handler.h>
52
#include <drizzled/diagnostics_area.h>
54
#include <drizzled/plugin/authorization.h>
56
56
#include <boost/unordered_map.hpp>
58
#include <boost/thread/thread.hpp>
59
57
#include <boost/thread/mutex.hpp>
60
#include <boost/thread/shared_mutex.hpp>
61
58
#include <boost/thread/condition_variable.hpp>
62
#include <boost/make_shared.hpp>
65
60
#define MIN_HANDSHAKE_SIZE 6
399
379
LEX *lex; /**< parse tree descriptor */
405
380
/** query associated with this statement */
406
typedef boost::shared_ptr<const std::string> QueryString;
408
boost::shared_ptr<std::string> query;
410
// Never allow for a modification of this outside of the class. c_str()
411
// requires under some setup non const, you must copy the QueryString in
414
QueryString getQueryString() const
419
void resetQueryString()
426
We need to copy the lock on the string in order to make sure we have a stable string.
427
Once this is done we can use it to build a const char* which can be handed off for
428
a method to use (Innodb is currently the only engine using this).
430
const char *getQueryStringCopy(size_t &length)
432
QueryString tmp_string(getQueryString());
441
length= tmp_string->length();
442
char *to_return= strmake(tmp_string->c_str(), tmp_string->length());
447
std::vector <char> _query;
450
typedef boost::shared_ptr<State> const_shared_ptr;
452
State(const char *in_packet, size_t in_packet_length)
454
if (in_packet_length)
456
size_t minimum= std::min(in_packet_length, static_cast<size_t>(PROCESS_LIST_WIDTH));
457
_query.resize(minimum + 1);
458
memcpy(&_query[0], in_packet, minimum);
466
const char *query() const
474
const char *query(size_t &size) const
478
size= _query.size() -1;
486
friend class Session;
487
typedef boost::shared_ptr<State> shared_ptr;
490
State::shared_ptr _state;
493
State::const_shared_ptr state()
499
384
Name of the current (default) database.
532
405
memory::Root warn_root; /**< Allocation area for warnings and errors */
533
406
plugin::Client *client; /**< Pointer to client object */
535
void setClient(plugin::Client *client_arg);
537
plugin::Client *getClient()
542
407
plugin::Scheduler *scheduler; /**< Pointer to scheduler object */
543
408
void *scheduler_arg; /**< Pointer to the optional scheduler argument */
545
410
typedef boost::unordered_map< std::string, user_var_entry *, util::insensitive_hash, util::insensitive_equal_to> UserVars;
547
411
typedef std::pair< UserVars::iterator, UserVars::iterator > UserVarsRange;
548
412
UserVars user_vars; /**< Hash of user variables defined during the session's lifetime */
552
const UserVars &getUserVariables() const
557
drizzle_system_variables variables; /**< Mutable local variables local to the session */
415
struct system_variables variables; /**< Mutable local variables local to the session */
558
416
struct system_status_var status_var; /**< Session-local status counters */
559
417
THR_LOCK_INFO lock_info; /**< Locking information for this session */
560
418
THR_LOCK_OWNER main_lock_id; /**< To use for conventional queries */
561
419
THR_LOCK_OWNER *lock_id; /**< If not main_lock_id, points to the lock_id of a cursor. */
421
boost::mutex LOCK_delete; /**< Locked before session is deleted */
429
void unlockForDelete()
431
LOCK_delete.unlock();
435
* A peek into the query string for the session. This is a best effort
436
* delivery, there is no guarantee whether the content is meaningful.
438
char process_list_info[PROCESS_LIST_WIDTH+1];
564
441
* A pointer to the stack frame of the scheduler thread
681
535
ResourceContext *getResourceContext(const plugin::MonitoredInTransaction *monitored,
682
536
size_t index= 0);
685
* Structure used to manage "statement transactions" and
686
* "normal transactions". In autocommit mode, the normal transaction is
687
* equivalent to the statement transaction.
689
* Storage engines will be registered here when they participate in
690
* a transaction. No engine is registered more than once.
692
538
struct st_transactions {
693
539
std::deque<NamedSavepoint> savepoints;
696
* The normal transaction (since BEGIN WORK).
698
* Contains a list of all engines that have participated in any of the
699
* statement transactions started within the context of the normal
702
* @note In autocommit mode, this is empty.
704
TransactionContext all;
707
* The statment transaction.
709
* Contains a list of all engines participating in the given statement.
711
* @note In autocommit mode, this will be used to commit/rollback the
712
* normal transaction.
714
TransactionContext stmt;
540
TransactionContext all; ///< Trans since BEGIN WORK
541
TransactionContext stmt; ///< Trans for current statement
716
542
XID_STATE xid_state;
849
659
create_sort_index(); may differ from examined_row_count.
851
661
uint32_t row_count;
852
session_id_t thread_id;
662
pthread_t real_id; /**< For debugging */
853
664
uint32_t tmp_table;
854
enum global_read_lock_t
857
GOT_GLOBAL_READ_LOCK= 1,
858
MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT= 2
861
global_read_lock_t _global_read_lock;
865
global_read_lock_t isGlobalReadLock() const
867
return _global_read_lock;
870
void setGlobalReadLock(global_read_lock_t arg)
872
_global_read_lock= arg;
875
DrizzleLock *lockTables(Table **tables, uint32_t count, uint32_t flags, bool *need_reopen);
876
bool lockGlobalReadLock();
877
bool lock_table_names(TableList *table_list);
878
bool lock_table_names_exclusively(TableList *table_list);
879
bool makeGlobalReadLockBlockCommit();
880
bool abortLockForThread(Table *table);
881
bool wait_if_global_read_lock(bool abort_on_refresh, bool is_not_commit);
882
int lock_table_name(TableList *table_list);
883
void abortLock(Table *table);
884
void removeLock(Table *table);
885
void unlockReadTables(DrizzleLock *sql_lock);
886
void unlockSomeTables(Table **table, uint32_t count);
887
void unlockTables(DrizzleLock *sql_lock);
888
void startWaitingGlobalReadLock();
889
void unlockGlobalReadLock();
892
int unlock_external(Table **table, uint32_t count);
893
int lock_external(Table **tables, uint32_t count);
894
bool wait_for_locked_table_names(TableList *table_list);
895
DrizzleLock *get_lock_data(Table **table_ptr, uint32_t count,
896
bool should_lock, Table **write_lock_used);
665
uint32_t global_read_lock;
899
666
uint32_t server_status;
900
667
uint32_t open_options;
901
668
uint32_t select_number; /**< number of select (used for EXPLAIN) */
1214
977
* Schedule a session to be run on the default scheduler.
1216
static bool schedule(Session::shared_ptr&);
1218
static void unlink(Session::shared_ptr&);
1221
982
For enter_cond() / exit_cond() to work the mutex must be got before
1222
983
enter_cond(); this mutex is then released by exit_cond().
1223
984
Usage must be: lock mutex; enter_cond(); your code; exit_cond().
1225
const char* enter_cond(boost::condition_variable_any &cond, boost::mutex &mutex, const char* msg);
986
const char* enter_cond(boost::condition_variable &cond, boost::mutex &mutex, const char* msg);
1226
987
void exit_cond(const char* old_msg);
1228
989
inline time_t query_start() { return start_time; }
1229
990
inline void set_time()
1231
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1232
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1233
start_utime= utime_after_lock= (mytime-epoch).total_microseconds();
1237
994
start_time= user_time;
1238
connect_microseconds= start_utime;
995
connect_microseconds= start_utime= utime_after_lock= my_micro_time();
1241
start_time= (mytime-epoch).total_seconds();
998
start_utime= utime_after_lock= my_micro_time_and_time(&start_time);
1243
1000
inline void set_current_time() { start_time= time(NULL); }
1244
1001
inline void set_time(time_t t)
1246
1003
start_time= user_time= t;
1247
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1248
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1249
uint64_t t_mark= (mytime-epoch).total_microseconds();
1251
start_utime= utime_after_lock= t_mark;
1253
void set_time_after_lock() {
1254
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1255
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1256
utime_after_lock= (mytime-epoch).total_microseconds();
1004
start_utime= utime_after_lock= my_micro_time();
1006
void set_time_after_lock() { utime_after_lock= my_micro_time(); }
1259
1008
* Returns the current micro-timestamp
1261
1010
inline uint64_t getCurrentTimestamp()
1263
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1264
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1265
uint64_t t_mark= (mytime-epoch).total_microseconds();
1012
return my_micro_time();
1269
1014
inline uint64_t found_rows(void)
1701
1424
bool openTablesLock(TableList *tables);
1427
* Open all tables in list and process derived tables
1429
* @param Pointer to a list of tables for open
1430
* @param Bitmap of flags to modify how the tables will be open:
1431
* DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
1432
* done a flush or namelock on it.
1441
* This is to be used on prepare stage when you don't read any
1442
* data from the tables.
1444
bool openTables(TableList *tables, uint32_t flags= 0);
1703
1446
int open_tables_from_list(TableList **start, uint32_t *counter, uint32_t flags= 0);
1705
1448
Table *openTableLock(TableList *table_list, thr_lock_type lock_type);
1706
1449
Table *openTable(TableList *table_list, bool *refresh, uint32_t flags= 0);
1708
1451
void unlink_open_table(Table *find);
1709
void drop_open_table(Table *table, const TableIdentifier &identifier);
1452
void drop_open_table(Table *table, TableIdentifier &identifier);
1710
1453
void close_cached_table(Table *table);
1712
1455
/* Create a lock in the cache */
1713
table::Placeholder *table_cache_insert_placeholder(const TableIdentifier &identifier);
1714
bool lock_table_name_if_not_cached(const TableIdentifier &identifier, Table **table);
1456
Table *table_cache_insert_placeholder(const char *db_name, const char *table_name);
1457
bool lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table);
1716
1459
typedef boost::unordered_map<std::string, message::Table, util::insensitive_hash, util::insensitive_equal_to> TableMessageCache;
1720
TableMessageCache table_message_cache;
1723
bool storeTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1724
bool removeTableMessage(const TableIdentifier &identifier);
1725
bool getTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1726
bool doesTableMessageExist(const TableIdentifier &identifier);
1727
bool renameTableMessage(const TableIdentifier &from, const TableIdentifier &to);
1460
TableMessageCache table_message_cache;
1462
bool storeTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1463
bool removeTableMessage(const TableIdentifier &identifier);
1464
bool getTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1465
bool doesTableMessageExist(const TableIdentifier &identifier);
1466
bool renameTableMessage(const TableIdentifier &from, const TableIdentifier &to);
1468
/* Work with temporary tables */
1469
Table *find_temporary_table(TableList *table_list);
1470
Table *find_temporary_table(const char *db, const char *table_name);
1471
Table *find_temporary_table(TableIdentifier &identifier);
1473
void doGetTableNames(CachedDirectory &directory,
1474
const SchemaIdentifier &schema_identifier,
1475
std::set<std::string>& set_of_names);
1476
void doGetTableNames(const SchemaIdentifier &schema_identifier,
1477
std::set<std::string>& set_of_names);
1479
void doGetTableIdentifiers(CachedDirectory &directory,
1480
const SchemaIdentifier &schema_identifier,
1481
TableIdentifiers &set_of_identifiers);
1482
void doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
1483
TableIdentifiers &set_of_identifiers);
1485
int doGetTableDefinition(const drizzled::TableIdentifier &identifier,
1486
message::Table &table_proto);
1487
bool doDoesTableExist(const drizzled::TableIdentifier &identifier);
1489
void close_temporary_tables();
1490
void close_temporary_table(Table *table);
1491
// The method below just handles the de-allocation of the table. In
1492
// a better memory type world, this would not be needed.
1731
TableMessages _table_message_cache;
1494
void nukeTable(Table *table);
1734
TableMessages &getMessageCache()
1736
return _table_message_cache;
1497
void dumpTemporaryTableNames(const char *id);
1498
int drop_temporary_table(TableList *table_list);
1499
bool rm_temporary_table(plugin::StorageEngine *base, TableIdentifier &identifier);
1500
bool rm_temporary_table(TableIdentifier &identifier, bool best_effort= false);
1501
Table *open_temporary_table(TableIdentifier &identifier,
1502
bool link_in_list= true);
1739
1504
/* Reopen operations */
1740
1505
bool reopen_tables(bool get_locks, bool mark_share_as_old);
1506
bool reopen_name_locked_table(TableList* table_list, bool link_in);
1741
1507
bool close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders);
1743
void wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond);
1509
void wait_for_condition(boost::mutex &mutex, boost::condition_variable &cond);
1744
1510
int setup_conds(TableList *leaves, COND **conds);
1745
1511
int lock_tables(TableList *tables, uint32_t count, bool *need_reopen);
1747
drizzled::util::Storable *getProperty(const std::string &arg)
1749
return life_properties[arg];
1753
bool setProperty(const std::string &arg, T *value)
1755
life_properties[arg]= value;
1513
Table *create_virtual_tmp_table(List<CreateField> &field_list);
1761
1518
Return the default storage engine