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/identifier.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
#include <boost/thread/condition_variable.hpp>
62
#include <boost/make_shared.hpp>
65
59
#define MIN_HANDSHAKE_SIZE 6
399
355
LEX *lex; /**< parse tree descriptor */
405
356
/** 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());
440
length= tmp_string->length();
441
char *to_return= strmake(tmp_string->c_str(), tmp_string->length());
446
std::vector <char> _query;
449
typedef boost::shared_ptr<State> const_shared_ptr;
451
State(const char *in_packet, size_t in_packet_length)
453
if (in_packet_length)
455
size_t minimum= std::min(in_packet_length, static_cast<size_t>(PROCESS_LIST_WIDTH));
456
_query.resize(minimum + 1);
457
memcpy(&_query[0], in_packet, minimum);
465
const char *query() const
473
const char *query(size_t &size) const
477
size= _query.size() -1;
485
friend class Session;
486
typedef boost::shared_ptr<State> shared_ptr;
489
State::shared_ptr _state;
492
State::const_shared_ptr state()
498
360
Name of the current (default) database.
531
381
memory::Root warn_root; /**< Allocation area for warnings and errors */
532
382
plugin::Client *client; /**< Pointer to client object */
534
void setClient(plugin::Client *client_arg);
536
plugin::Client *getClient()
541
383
plugin::Scheduler *scheduler; /**< Pointer to scheduler object */
542
384
void *scheduler_arg; /**< Pointer to the optional scheduler argument */
544
386
typedef boost::unordered_map< std::string, user_var_entry *, util::insensitive_hash, util::insensitive_equal_to> UserVars;
546
387
typedef std::pair< UserVars::iterator, UserVars::iterator > UserVarsRange;
547
388
UserVars user_vars; /**< Hash of user variables defined during the session's lifetime */
551
const UserVars &getUserVariables() const
556
drizzle_system_variables variables; /**< Mutable local variables local to the session */
391
struct system_variables variables; /**< Mutable local variables local to the session */
557
392
struct system_status_var status_var; /**< Session-local status counters */
558
393
THR_LOCK_INFO lock_info; /**< Locking information for this session */
559
394
THR_LOCK_OWNER main_lock_id; /**< To use for conventional queries */
560
395
THR_LOCK_OWNER *lock_id; /**< If not main_lock_id, points to the lock_id of a cursor. */
397
boost::mutex LOCK_delete; /**< Locked before session is deleted */
405
void unlockForDelete()
407
LOCK_delete.unlock();
411
* A peek into the query string for the session. This is a best effort
412
* delivery, there is no guarantee whether the content is meaningful.
414
char process_list_info[PROCESS_LIST_WIDTH+1];
563
417
* A pointer to the stack frame of the scheduler thread
683
511
ResourceContext *getResourceContext(const plugin::MonitoredInTransaction *monitored,
684
512
size_t index= 0);
687
* Structure used to manage "statement transactions" and
688
* "normal transactions". In autocommit mode, the normal transaction is
689
* equivalent to the statement transaction.
691
* Storage engines will be registered here when they participate in
692
* a transaction. No engine is registered more than once.
694
514
struct st_transactions {
695
515
std::deque<NamedSavepoint> savepoints;
698
* The normal transaction (since BEGIN WORK).
700
* Contains a list of all engines that have participated in any of the
701
* statement transactions started within the context of the normal
704
* @note In autocommit mode, this is empty.
706
TransactionContext all;
709
* The statment transaction.
711
* Contains a list of all engines participating in the given statement.
713
* @note In autocommit mode, this will be used to commit/rollback the
714
* normal transaction.
716
TransactionContext stmt;
516
TransactionContext all; ///< Trans since BEGIN WORK
517
TransactionContext stmt; ///< Trans for current statement
718
518
XID_STATE xid_state;
851
635
create_sort_index(); may differ from examined_row_count.
853
637
uint32_t row_count;
854
session_id_t thread_id;
638
pthread_t real_id; /**< For debugging */
855
640
uint32_t tmp_table;
856
enum global_read_lock_t
859
GOT_GLOBAL_READ_LOCK= 1,
860
MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT= 2
863
global_read_lock_t _global_read_lock;
867
global_read_lock_t isGlobalReadLock() const
869
return _global_read_lock;
872
void setGlobalReadLock(global_read_lock_t arg)
874
_global_read_lock= arg;
877
DrizzleLock *lockTables(Table **tables, uint32_t count, uint32_t flags, bool *need_reopen);
878
bool lockGlobalReadLock();
879
bool lock_table_names(TableList *table_list);
880
bool lock_table_names_exclusively(TableList *table_list);
881
bool makeGlobalReadLockBlockCommit();
882
bool abortLockForThread(Table *table);
883
bool wait_if_global_read_lock(bool abort_on_refresh, bool is_not_commit);
884
int lock_table_name(TableList *table_list);
885
void abortLock(Table *table);
886
void removeLock(Table *table);
887
void unlockReadTables(DrizzleLock *sql_lock);
888
void unlockSomeTables(Table **table, uint32_t count);
889
void unlockTables(DrizzleLock *sql_lock);
890
void startWaitingGlobalReadLock();
891
void unlockGlobalReadLock();
894
int unlock_external(Table **table, uint32_t count);
895
int lock_external(Table **tables, uint32_t count);
896
bool wait_for_locked_table_names(TableList *table_list);
897
DrizzleLock *get_lock_data(Table **table_ptr, uint32_t count,
898
bool should_lock, Table **write_lock_used);
641
uint32_t global_read_lock;
901
642
uint32_t server_status;
902
643
uint32_t open_options;
903
644
uint32_t select_number; /**< number of select (used for EXPLAIN) */
1216
953
* Schedule a session to be run on the default scheduler.
1218
static bool schedule(Session::shared_ptr&);
1220
static void unlink(Session::shared_ptr&);
1223
958
For enter_cond() / exit_cond() to work the mutex must be got before
1224
959
enter_cond(); this mutex is then released by exit_cond().
1225
960
Usage must be: lock mutex; enter_cond(); your code; exit_cond().
1227
const char* enter_cond(boost::condition_variable_any &cond, boost::mutex &mutex, const char* msg);
962
const char* enter_cond(pthread_cond_t *cond, pthread_mutex_t* mutex, const char* msg);
1228
963
void exit_cond(const char* old_msg);
1230
965
inline time_t query_start() { return start_time; }
1231
966
inline void set_time()
1233
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1234
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1235
start_utime= utime_after_lock= (mytime-epoch).total_microseconds();
1239
970
start_time= user_time;
1240
connect_microseconds= start_utime;
971
connect_microseconds= start_utime= utime_after_lock= my_micro_time();
1243
start_time= (mytime-epoch).total_seconds();
974
start_utime= utime_after_lock= my_micro_time_and_time(&start_time);
1245
976
inline void set_current_time() { start_time= time(NULL); }
1246
977
inline void set_time(time_t t)
1248
979
start_time= user_time= t;
1249
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1250
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1251
uint64_t t_mark= (mytime-epoch).total_microseconds();
1253
start_utime= utime_after_lock= t_mark;
1255
void set_time_after_lock() {
1256
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1257
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1258
utime_after_lock= (mytime-epoch).total_microseconds();
980
start_utime= utime_after_lock= my_micro_time();
982
void set_time_after_lock() { utime_after_lock= my_micro_time(); }
1261
984
* Returns the current micro-timestamp
1263
986
inline uint64_t getCurrentTimestamp()
1265
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
1266
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
1267
uint64_t t_mark= (mytime-epoch).total_microseconds();
988
return my_micro_time();
1271
990
inline uint64_t found_rows(void)
1703
1400
bool openTablesLock(TableList *tables);
1403
* Open all tables in list and process derived tables
1405
* @param Pointer to a list of tables for open
1406
* @param Bitmap of flags to modify how the tables will be open:
1407
* DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
1408
* done a flush or namelock on it.
1417
* This is to be used on prepare stage when you don't read any
1418
* data from the tables.
1420
bool openTables(TableList *tables, uint32_t flags= 0);
1705
1422
int open_tables_from_list(TableList **start, uint32_t *counter, uint32_t flags= 0);
1707
1424
Table *openTableLock(TableList *table_list, thr_lock_type lock_type);
1708
1425
Table *openTable(TableList *table_list, bool *refresh, uint32_t flags= 0);
1710
1427
void unlink_open_table(Table *find);
1711
void drop_open_table(Table *table, const TableIdentifier &identifier);
1428
void drop_open_table(Table *table, TableIdentifier &identifier);
1712
1429
void close_cached_table(Table *table);
1714
1431
/* Create a lock in the cache */
1715
table::Placeholder *table_cache_insert_placeholder(const TableIdentifier &identifier);
1716
bool lock_table_name_if_not_cached(const TableIdentifier &identifier, Table **table);
1432
Table *table_cache_insert_placeholder(const char *db_name, const char *table_name);
1433
bool lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table);
1718
1435
typedef boost::unordered_map<std::string, message::Table, util::insensitive_hash, util::insensitive_equal_to> TableMessageCache;
1722
TableMessageCache table_message_cache;
1725
bool storeTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1726
bool removeTableMessage(const TableIdentifier &identifier);
1727
bool getTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1728
bool doesTableMessageExist(const TableIdentifier &identifier);
1729
bool renameTableMessage(const TableIdentifier &from, const TableIdentifier &to);
1436
TableMessageCache table_message_cache;
1438
bool storeTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1439
bool removeTableMessage(const TableIdentifier &identifier);
1440
bool getTableMessage(const TableIdentifier &identifier, message::Table &table_message);
1441
bool doesTableMessageExist(const TableIdentifier &identifier);
1442
bool renameTableMessage(const TableIdentifier &from, const TableIdentifier &to);
1444
/* Work with temporary tables */
1445
Table *find_temporary_table(TableList *table_list);
1446
Table *find_temporary_table(const char *db, const char *table_name);
1447
Table *find_temporary_table(TableIdentifier &identifier);
1449
void doGetTableNames(CachedDirectory &directory,
1450
const SchemaIdentifier &schema_identifier,
1451
std::set<std::string>& set_of_names);
1452
void doGetTableNames(const SchemaIdentifier &schema_identifier,
1453
std::set<std::string>& set_of_names);
1455
void doGetTableIdentifiers(CachedDirectory &directory,
1456
const SchemaIdentifier &schema_identifier,
1457
TableIdentifiers &set_of_identifiers);
1458
void doGetTableIdentifiers(const SchemaIdentifier &schema_identifier,
1459
TableIdentifiers &set_of_identifiers);
1461
int doGetTableDefinition(const drizzled::TableIdentifier &identifier,
1462
message::Table &table_proto);
1463
bool doDoesTableExist(const drizzled::TableIdentifier &identifier);
1465
void close_temporary_tables();
1466
void close_temporary_table(Table *table);
1467
// The method below just handles the de-allocation of the table. In
1468
// a better memory type world, this would not be needed.
1733
TableMessages _table_message_cache;
1470
void nukeTable(Table *table);
1736
TableMessages &getMessageCache()
1738
return _table_message_cache;
1473
void dumpTemporaryTableNames(const char *id);
1474
int drop_temporary_table(TableList *table_list);
1475
bool rm_temporary_table(plugin::StorageEngine *base, TableIdentifier &identifier);
1476
bool rm_temporary_table(TableIdentifier &identifier, bool best_effort= false);
1477
Table *open_temporary_table(TableIdentifier &identifier,
1478
bool link_in_list= true);
1741
1480
/* Reopen operations */
1742
1481
bool reopen_tables(bool get_locks, bool mark_share_as_old);
1482
bool reopen_name_locked_table(TableList* table_list, bool link_in);
1743
1483
bool close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders);
1745
void wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond);
1485
void wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond);
1746
1486
int setup_conds(TableList *leaves, COND **conds);
1747
1487
int lock_tables(TableList *tables, uint32_t count, bool *need_reopen);
1749
drizzled::util::Storable *getProperty(const std::string &arg)
1751
return life_properties[arg];
1755
bool setProperty(const std::string &arg, T *value)
1757
life_properties[arg]= value;
1489
Table *create_virtual_tmp_table(List<CreateField> &field_list);
1763
1494
Return the default storage engine