24
24
#include "config.h"
26
#include <drizzled/copy_field.h>
27
25
#include "drizzled/session.h"
28
26
#include "drizzled/session/cache.h"
29
28
#include "drizzled/error.h"
30
29
#include "drizzled/gettext.h"
31
30
#include "drizzled/query_id.h"
53
52
#include "drizzled/identifier.h"
55
#include <drizzled/refresh_version.h>
57
#include "drizzled/table/singular.h"
54
#include "drizzled/table/instance.h"
59
56
#include "plugin/myisam/myisam.h"
60
57
#include "drizzled/internal/iocache.h"
115
int session_tablespace_op(const Session *session)
117
return test(session->tablespace_op);
121
Set the process info field of the Session structure.
123
This function is used by plug-ins. Internally, the
124
Session::set_proc_info() function should be used.
126
@see Session::set_proc_info
128
void set_session_proc_info(Session *session, const char *info)
130
session->set_proc_info(info);
133
const char *get_session_proc_info(Session *session)
135
return session->get_proc_info();
121
138
void **Session::getEngineData(const plugin::MonitoredInTransaction *monitored)
123
140
return static_cast<void **>(&ha_data[monitored->getId()].ha_ptr);
134
151
return session->options & test_options;
137
Session::Session(plugin::Client *client_arg, catalog::Instance::shared_ptr catalog_arg) :
154
int session_sql_command(const Session *session)
156
return (int) session->lex->sql_command;
159
enum_tx_isolation session_tx_isolation(const Session *session)
161
return (enum_tx_isolation)session->variables.tx_isolation;
164
Session::Session(plugin::Client *client_arg) :
138
165
Open_tables_state(refresh_version),
139
166
mem_root(&main_mem_root),
142
169
query(new std::string),
143
170
_schema(new std::string("")),
144
172
client(client_arg),
146
174
scheduler_arg(NULL),
147
175
lock_id(&main_lock_id),
148
176
thread_stack(NULL),
149
177
security_ctx(identifier::User::make_shared()),
150
_where(Session::DEFAULT_WHERE),
151
dbug_sentry(Session_SENTRY_MAGIC),
153
command(COM_CONNECT),
155
_epoch(boost::gregorian::date(1970,1,1)),
156
_connect_time(boost::posix_time::microsec_clock::universal_time()),
158
179
ha_data(plugin::num_trx_monitored_objects),
161
180
concurrent_execute_allowed(true),
162
181
arg_of_last_insert_id_function(false),
163
182
first_successful_insert_id_in_prev_stmt(0),
164
183
first_successful_insert_id_in_cur_stmt(0),
165
184
limit_found_rows(0),
166
options(session_startup_options),
169
examined_row_count(0),
173
statement_id_counter(0),
177
185
_global_read_lock(NONE),
178
count_cuted_fields(CHECK_FIELD_ERROR_FOR_NULL),
179
186
_killed(NOT_KILLED),
180
187
some_tables_deleted(false),
181
188
no_errors(false),
183
190
is_fatal_error(false),
184
191
transaction_rollback_request(false),
185
192
is_fatal_sub_stmt_error(0),
193
derived_tables_processing(false),
186
194
tablespace_op(false),
187
derived_tables_processing(false),
190
197
transaction_message(NULL),
191
198
statement_message(NULL),
192
199
session_event_observers(NULL),
193
_catalog(catalog_arg),
196
202
client->setSession(this);
201
207
will be re-initialized in init_for_queries().
203
209
memory::init_sql_alloc(&main_mem_root, memory::ROOT_MIN_BLOCK_SIZE, 0);
210
count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
204
214
cuted_fields= sent_row_count= row_count= 0L;
216
statement_id_counter= 0UL;
205
217
// Must be reset to handle error with Session's created for init of mysqld
206
218
lex->current_select= 0;
219
start_time=(time_t) 0;
221
utime_after_lock= 0L;
207
222
memset(&variables, 0, sizeof(variables));
208
228
scoreboard_index= -1;
229
dbug_sentry=Session_SENTRY_MAGIC;
209
230
cleanup_done= abort_on_warning= no_warnings_for_error= false;
211
232
/* query_cache init */
224
247
variables.pseudo_thread_id= thread_id;
225
248
server_status= SERVER_STATUS_AUTOCOMMIT;
249
options= session_startup_options;
227
251
if (variables.max_join_size == HA_POS_ERROR)
228
252
options |= OPTION_BIG_SELECTS;
234
258
session_tx_isolation= (enum_tx_isolation) variables.tx_isolation;
235
259
warn_list.empty();
236
260
memset(warn_count, 0, sizeof(warn_count));
237
262
memset(&status_var, 0, sizeof(status_var));
239
264
/* Initialize sub structures */
269
294
m_internal_handler= handler;
272
bool Session::handle_error(drizzled::error_t sql_errno, const char *message,
273
DRIZZLE_ERROR::enum_warning_level level)
297
bool Session::handle_error(uint32_t sql_errno, const char *message,
298
DRIZZLE_ERROR::enum_warning_level level)
275
300
if (m_internal_handler)
328
353
TransactionServices &transaction_services= TransactionServices::singleton();
329
transaction_services.rollbackTransaction(*this, true);
354
transaction_services.rollbackTransaction(this, true);
330
355
xid_cache_delete(&transaction.xid_state);
359
384
assert(security_ctx);
360
385
if (global_system_variables.log_warnings)
362
errmsg_printf(error::WARN, ER(ER_FORCING_CLOSE),
387
errmsg_printf(ERRMSG_LVL_WARN, ER(ER_FORCING_CLOSE),
363
388
internal::my_progname,
365
390
security_ctx->username().c_str());
393
417
plugin::Logging::postEndDo(this);
394
418
plugin::EventObserver::deregisterSessionEvents(*this);
420
for (PropertyMap::iterator iter= life_properties.begin(); iter != life_properties.end(); iter++)
422
delete (*iter).second;
424
life_properties.clear();
397
427
void Session::setClient(plugin::Client *client_arg)
541
571
arg->scheduler= plugin::Scheduler::getScheduler();
542
572
assert(arg->scheduler);
546
long current_connections= connection_count;
548
if (current_connections > 0 and static_cast<uint64_t>(current_connections) > current_global_counters.max_used_connections)
574
connection_count.increment();
576
if (connection_count > current_global_counters.max_used_connections)
550
current_global_counters.max_used_connections= static_cast<uint64_t>(connection_count);
578
current_global_counters.max_used_connections= connection_count;
553
581
current_global_counters.connections++;
642
671
/* Change database if necessary */
643
672
if (not in_db.empty())
645
identifier::Schema identifier(in_db);
674
SchemaIdentifier identifier(in_db);
646
675
if (change_db(this, identifier))
648
677
/* change_db() has pushed the error message. */
713
742
plugin::QueryRewriter::rewriteQuery(*_schema, *new_query);
715
744
query.reset(new_query);
716
_state.reset(new session::State(in_packet, in_packet_length));
745
_state.reset(new State(in_packet, in_packet_length));
738
767
* (Which of course should never happen...)
740
769
server_status&= ~SERVER_STATUS_IN_TRANS;
741
if (transaction_services.commitTransaction(*this, true))
770
if (transaction_services.commitTransaction(this, true))
743
772
options&= ~(OPTION_BEGIN);
755
784
case ROLLBACK_AND_CHAIN:
757
786
server_status&= ~SERVER_STATUS_IN_TRANS;
758
if (transaction_services.rollbackTransaction(*this, true))
787
if (transaction_services.rollbackTransaction(this, true))
760
789
options&= ~(OPTION_BEGIN);
761
790
if (result == true && (completion == ROLLBACK_AND_CHAIN))
792
821
if (options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
794
823
server_status&= ~SERVER_STATUS_IN_TRANS;
795
if (transaction_services.commitTransaction(*this, true))
824
if (transaction_services.commitTransaction(this, true))
798
827
options&= ~(OPTION_BEGIN);
804
833
bool result= true;
806
assert(! inTransaction());
808
options|= OPTION_BEGIN;
809
server_status|= SERVER_STATUS_IN_TRANS;
811
if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
835
if (! endActiveTransaction())
841
options|= OPTION_BEGIN;
842
server_status|= SERVER_STATUS_IN_TRANS;
844
if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
833
867
first_successful_insert_id_in_cur_stmt= 0;
834
868
substitute_null_with_insert_id= true;
837
870
arg_of_last_insert_id_function= false;
839
871
/* Free Items that were created during this execution */
843
_where= Session::DEFAULT_WHERE;
874
where= Session::DEFAULT_WHERE;
845
876
/* Reset the temporary shares we built */
846
877
for_each(temporary_shares.begin(),
871
902
bool allocate_lex_string)
873
904
if (allocate_lex_string)
874
if (!(lex_str= (LEX_STRING *)getMemRoot()->allocate(sizeof(LEX_STRING))))
905
if (!(lex_str= (LEX_STRING *)alloc(sizeof(LEX_STRING))))
876
907
if (!(lex_str->str= mem_root->strmake_root(str, length)))
917
948
return (result->send_fields(field_list));
920
void select_result::send_error(drizzled::error_t errcode, const char *err)
951
void select_result::send_error(uint32_t errcode, const char *err)
922
953
my_message(errcode, err, MYF(0));
926
957
Handling writing to file
927
958
************************************************************************/
929
void select_to_file::send_error(drizzled::error_t errcode,const char *err)
960
void select_to_file::send_error(uint32_t errcode,const char *err)
931
962
my_message(errcode, err, MYF(0));
1574
1605
/* Fix for Intel compiler */
1575
1606
if (copy_field)
1577
boost::checked_array_delete(copy_field);
1578
save_copy_field= save_copy_field_end= copy_field= copy_field_end= 0;
1608
delete [] copy_field;
1609
save_copy_field= copy_field= 0;
1582
1613
void Session::send_kill_message() const
1584
drizzled::error_t err= static_cast<drizzled::error_t>(killed_errno());
1615
int err= killed_errno();
1586
1617
my_message(err, ER(err), MYF(0));
1612
1643
@param session Thread handle
1613
1644
@param all true <=> rollback main transaction.
1615
void Session::markTransactionForRollback(bool all)
1646
void mark_transaction_to_rollback(Session *session, bool all)
1617
is_fatal_sub_stmt_error= true;
1618
transaction_rollback_request= all;
1650
session->is_fatal_sub_stmt_error= true;
1651
session->transaction_rollback_request= all;
1621
void Session::disconnect(enum error_t errcode)
1655
void Session::disconnect(enum drizzled_error_code errcode)
1623
1657
/* Allow any plugins to cleanup their session variables */
1624
1658
plugin_sessionvar_cleanup(this);
1634
1668
if (not getKilled() && variables.log_warnings > 1)
1636
errmsg_printf(error::WARN, ER(ER_NEW_ABORTING_CONNECTION)
1670
errmsg_printf(ERRMSG_LVL_WARN, ER(ER_NEW_ABORTING_CONNECTION)
1638
1672
, (_schema->empty() ? "unconnected" : _schema->c_str())
1639
1673
, security_ctx->username().empty() == false ? security_ctx->username().c_str() : "unauthenticated"
1743
1777
table->free_io_cache();
1744
1778
table->delete_table();
1746
identifier::Table identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath());
1780
TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath());
1747
1781
rm_temporary_table(table_type, identifier);
1749
boost::checked_delete(table->getMutableShare());
1783
delete table->getMutableShare();
1751
boost::checked_delete(table);
1785
/* This makes me sad, but we're allocating it via malloc */
1754
1789
/** Clear most status variables. */
1772
1807
user_var_entry *Session::getVariable(const std::string &name, bool create_if_not_exists)
1809
UserVarsRange ppp= user_vars.equal_range(name);
1777
UserVars::iterator iter= user_vars.find(name);
1778
if (iter != user_vars.end())
1811
for (UserVars::iterator iter= ppp.first;
1812
iter != ppp.second; ++iter)
1779
1814
return (*iter).second;
1781
1817
if (not create_if_not_exists)
1800
1836
void Session::setVariable(const std::string &name, const std::string &value)
1802
1838
user_var_entry *updateable_var= getVariable(name.c_str(), true);
1805
updateable_var->update_hash(false,
1806
(void*)value.c_str(),
1807
static_cast<uint32_t>(value.length()), STRING_RESULT,
1809
DERIVATION_IMPLICIT, false);
1840
updateable_var->update_hash(false,
1841
(void*)value.c_str(),
1842
static_cast<uint32_t>(value.length()), STRING_RESULT,
1844
DERIVATION_IMPLICIT, false);
1813
1847
void Open_tables_state::mark_temp_tables_as_free_for_reuse()
1863
1897
TransactionServices &transaction_services= TransactionServices::singleton();
1864
1898
main_da.can_overwrite_status= true;
1865
transaction_services.autocommitOrRollback(*this, is_error());
1899
transaction_services.autocommitOrRollback(this, is_error());
1866
1900
main_da.can_overwrite_status= false;
1867
1901
transaction.stmt.reset();
1919
1953
if (not lock_tables(tables, counter, &need_reopen))
1922
1955
if (not need_reopen)
1925
1957
close_tables_for_reopen(&tables);
1928
if ((handle_derived(lex, &derived_prepare) || (handle_derived(lex, &derived_filling))))
1959
if ((handle_derived(lex, &derived_prepare) ||
1961
handle_derived(lex, &derived_filling))))
1937
1970
might be an issue (lame engines).
1940
bool Open_tables_state::rm_temporary_table(const identifier::Table &identifier, bool best_effort)
1973
bool Open_tables_state::rm_temporary_table(const TableIdentifier &identifier, bool best_effort)
1942
if (not plugin::StorageEngine::dropTable(*static_cast<Session *>(this), identifier))
1975
if (plugin::StorageEngine::dropTable(*static_cast<Session *>(this), identifier))
1944
1977
if (not best_effort)
1946
1979
std::string path;
1947
1980
identifier.getSQLPath(path);
1948
errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
1981
errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
1949
1982
path.c_str(), errno);
1958
bool Open_tables_state::rm_temporary_table(plugin::StorageEngine *base, const identifier::Table &identifier)
1991
bool Open_tables_state::rm_temporary_table(plugin::StorageEngine *base, const TableIdentifier &identifier)
1960
drizzled::error_t error;
1963
if (not plugin::StorageEngine::dropTable(*static_cast<Session *>(this), *base, identifier, error))
1995
if (plugin::StorageEngine::dropTable(*static_cast<Session *>(this), *base, identifier))
1965
1997
std::string path;
1966
1998
identifier.getSQLPath(path);
1967
errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
1968
path.c_str(), error);
1999
errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
2000
path.c_str(), errno);
1990
2022
bool have_proto= false;
1992
message::Table *proto= table->getShare()->getTableMessage();
1993
if (table->getShare()->getTableMessage())
2024
message::Table *proto= table->getShare()->getTableProto();
2025
if (table->getShare()->getTableProto())
1994
2026
have_proto= true;
1996
2028
const char *answer= have_proto ? "true" : "false";
2010
table::Singular *Session::getInstanceTable()
2012
temporary_shares.push_back(new table::Singular()); // This will not go into the tableshare cache, so no key is used.
2014
table::Singular *tmp_share= temporary_shares.back();
2042
bool Session::TableMessages::storeTableMessage(const TableIdentifier &identifier, message::Table &table_message)
2044
table_message_cache.insert(make_pair(identifier.getPath(), table_message));
2049
bool Session::TableMessages::removeTableMessage(const TableIdentifier &identifier)
2051
TableMessageCache::iterator iter;
2053
iter= table_message_cache.find(identifier.getPath());
2055
if (iter == table_message_cache.end())
2058
table_message_cache.erase(iter);
2063
bool Session::TableMessages::getTableMessage(const TableIdentifier &identifier, message::Table &table_message)
2065
TableMessageCache::iterator iter;
2067
iter= table_message_cache.find(identifier.getPath());
2069
if (iter == table_message_cache.end())
2072
table_message.CopyFrom(((*iter).second));
2077
bool Session::TableMessages::doesTableMessageExist(const TableIdentifier &identifier)
2079
TableMessageCache::iterator iter;
2081
iter= table_message_cache.find(identifier.getPath());
2083
if (iter == table_message_cache.end())
2091
bool Session::TableMessages::renameTableMessage(const TableIdentifier &from, const TableIdentifier &to)
2093
TableMessageCache::iterator iter;
2095
table_message_cache[to.getPath()]= table_message_cache[from.getPath()];
2097
iter= table_message_cache.find(to.getPath());
2099
if (iter == table_message_cache.end())
2104
(*iter).second.set_schema(to.getSchemaName());
2105
(*iter).second.set_name(to.getTableName());
2110
table::Instance *Session::getInstanceTable()
2112
temporary_shares.push_back(new table::Instance()); // This will not go into the tableshare cache, so no key is used.
2114
table::Instance *tmp_share= temporary_shares.back();
2016
2116
assert(tmp_share);
2038
2138
0 if out of memory, Table object in case of success
2040
table::Singular *Session::getInstanceTable(List<CreateField> &field_list)
2140
table::Instance *Session::getInstanceTable(List<CreateField> &field_list)
2042
temporary_shares.push_back(new table::Singular(this, field_list)); // This will not go into the tableshare cache, so no key is used.
2142
temporary_shares.push_back(new table::Instance(this, field_list)); // This will not go into the tableshare cache, so no key is used.
2044
table::Singular *tmp_share= temporary_shares.back();
2144
table::Instance *tmp_share= temporary_shares.back();
2046
2146
assert(tmp_share);