26#include <boost/checked_delete.hpp>
27#include <boost/filesystem.hpp>
28#include <boost/ptr_container/ptr_container.hpp>
29#include <drizzled/copy_field.h>
30#include <drizzled/catalog/local.h>
31#include <drizzled/diagnostics_area.h>
32#include <drizzled/display.h>
33#include <drizzled/drizzled.h>
34#include <drizzled/error.h>
35#include <drizzled/gettext.h>
36#include <drizzled/ha_data.h>
37#include <drizzled/identifier.h>
38#include <drizzled/internal/iocache.h>
39#include <drizzled/internal/thread_var.h>
40#include <drizzled/item/cache.h>
41#include <drizzled/item/empty_string.h>
42#include <drizzled/item/float.h>
43#include <drizzled/item/return_int.h>
44#include <drizzled/item/subselect.h>
45#include <drizzled/lock.h>
46#include <drizzled/open_tables_state.h>
47#include <drizzled/plugin/authentication.h>
48#include <drizzled/plugin/authorization.h>
49#include <drizzled/plugin/client.h>
50#include <drizzled/plugin/event_observer.h>
51#include <drizzled/plugin/logging.h>
52#include <drizzled/plugin/query_rewrite.h>
53#include <drizzled/plugin/scheduler.h>
54#include <drizzled/plugin/transactional_storage_engine.h>
55#include <drizzled/probes.h>
56#include <drizzled/pthread_globals.h>
57#include <drizzled/schema.h>
58#include <drizzled/select_dump.h>
59#include <drizzled/select_exists_subselect.h>
60#include <drizzled/select_export.h>
61#include <drizzled/select_max_min_finder_subselect.h>
62#include <drizzled/select_singlerow_subselect.h>
63#include <drizzled/select_subselect.h>
64#include <drizzled/select_to_file.h>
65#include <drizzled/session.h>
66#include <drizzled/session/cache.h>
67#include <drizzled/session/state.h>
68#include <drizzled/session/table_messages.h>
69#include <drizzled/session/times.h>
70#include <drizzled/session/transactions.h>
72#include <drizzled/sql_base.h>
73#include <drizzled/sql_lex.h>
74#include <drizzled/system_variables.h>
75#include <drizzled/statement.h>
76#include <drizzled/statistics_variables.h>
77#include <drizzled/table/singular.h>
78#include <drizzled/table_proto.h>
79#include <drizzled/tmp_table_param.h>
80#include <drizzled/transaction_services.h>
81#include <drizzled/user_var_entry.h>
82#include <drizzled/util/backtrace.h>
83#include <drizzled/util/find_ptr.h>
84#include <drizzled/util/functors.h>
85#include <drizzled/util/storable.h>
86#include <plugin/myisam/myisam.h>
95namespace fs= boost::filesystem;
101uint64_t g_refresh_version = 1;
103bool Key_part_spec::operator==(
const Key_part_spec& other)
const
105 return length == other.length
106 && field_name.size() == other.field_name.size()
107 && not system_charset_info->strcasecmp(field_name.data(), other.field_name.data());
110Open_tables_state::Open_tables_state(
Session& session, uint64_t version_arg) :
111 version(version_arg),
114 open_tables_= temporary_tables= derived_tables= NULL;
115 extra_lock= lock= NULL;
123 char filename[FN_REFLEN];
124 int fd = internal::create_temp_file(filename, drizzle_tmpdir.c_str(), prefix, MYF(MY_WME));
130void **Session::getEngineData(
const plugin::MonitoredInTransaction *monitored)
132 return static_cast<void **
>(&ha_data[monitored->getId()].ha_ptr);
135ResourceContext& Session::getResourceContext(
const plugin::MonitoredInTransaction& monitored,
size_t index)
137 return ha_data[monitored.getId()].resource_context[index];
140int64_t session_test_options(
const Session *session, int64_t test_options)
142 return session->options & test_options;
148 typedef std::unordered_map<std::string, util::Storable*, util::insensitive_hash, util::insensitive_equal_to> properties_t;
149 typedef std::map<std::string, plugin::EventObserverList*> schema_event_observers_t;
151 impl_c(Session& session) :
152 open_tables(session, g_refresh_version),
153 schema(std::make_shared<std::string>())
159 for (properties_t::reference it : properties)
174 properties_t properties;
175 schema_event_observers_t schema_event_observers;
178 util::string::mptr schema;
179 std::shared_ptr<session::State> state;
180 boost::ptr_vector<table::Singular> temporary_shares;
187 std::shared_ptr<catalog::Instance> instance_arg)
189 assert(instance_arg);
190 return std::make_shared<Session>(client, instance_arg);
193Session::Session(
plugin::Client *client_arg, catalog::Instance::shared_ptr catalog_arg) :
194 impl_(new impl_c(*this)),
195 mem(impl_->mem_root),
196 mem_root(&impl_->mem_root),
197 query(new
std::string),
199 variables(impl_->variables),
200 status_var(impl_->status_var),
201 lock_id(&main_lock_id),
203 _where(
Session::DEFAULT_WHERE),
205 command(COM_CONNECT),
206 ha_data(plugin::num_trx_monitored_objects),
209 transaction(impl_->transaction),
210 open_tables(impl_->open_tables),
212 first_successful_insert_id_in_prev_stmt(0),
213 first_successful_insert_id_in_cur_stmt(0),
215 options(session_startup_options),
218 examined_row_count(0),
224 _global_read_lock(NONE),
225 count_cuted_fields(CHECK_FIELD_ERROR_FOR_NULL),
228 is_fatal_error(false),
229 transaction_rollback_request(false),
230 is_fatal_sub_stmt_error(0),
231 derived_tables_processing(false),
233 arg_of_last_insert_id_function(false),
234 _catalog(catalog_arg),
235 transaction_message(NULL),
236 statement_message(NULL),
237 session_event_observers(NULL),
239 concurrent_execute_allowed(true),
240 tablespace_op(false),
242 security_ctx(identifier::User::make_shared()),
243 originating_server_uuid_set(false),
246 client->setSession(
this);
253 mem.init(memory::ROOT_MIN_BLOCK_SIZE);
256 lex().current_select= 0;
258 scoreboard_index= -1;
259 originating_server_uuid=
"";
260 originating_commit_id= 0;
268 plugin_sessionvar_init(
this);
275 server_status= SERVER_STATUS_AUTOCOMMIT;
277 if (
variables.max_join_size == HA_POS_ERROR)
280 options &= ~OPTION_BIG_SELECTS;
282 open_options=ha_open_options;
283 update_lock_default= TL_WRITE;
284 session_tx_isolation= (enum_tx_isolation)
variables.tx_isolation;
286 memset(&status_var, 0,
sizeof(status_var));
295 plugin::EventObserver::registerSessionEvents(*
this);
298Diagnostics_area& Session::main_da()
300 return impl_->diagnostics;
303const LEX& Session::lex()
const
313enum_sql_command Session::getSqlCommand()
const
315 return lex().sql_command;
318session::TableMessages& Session::getMessageCache()
320 return impl_->table_message_cache;
323void statement::Statement::set_command(enum_sql_command v)
325 session().lex().sql_command= v;
328LEX& statement::Statement::lex()
330 return session().lex();
333session::Transactions& statement::Statement::transaction()
335 return session().transaction;
338void Session::add_item_to_list(Item *item)
340 lex().current_select->add_item_to_list(
this, item);
343void Session::add_value_to_list(Item *value)
345 lex().value_list.push_back(value);
348void Session::add_order_to_list(Item *item,
bool asc)
350 lex().current_select->add_order_to_list(
this, item, asc);
353void Session::add_group_to_list(Item *item,
bool asc)
355 lex().current_select->add_group_to_list(
this, item, asc);
368void Session::setAbort(
bool arg)
370 mysys_var->abort= arg;
373void Session::lockOnSys()
379 boost::mutex::scoped_lock scopedLock(mysys_var->mutex);
380 if (mysys_var->current_cond)
382 mysys_var->current_mutex->lock();
383 mysys_var->current_cond->notify_all();
384 mysys_var->current_mutex->unlock();
388void Session::get_xid(DrizzleXid *xid)
const
390 *xid = *(DrizzleXid *) &transaction.xid_state.xid;
395void Session::cleanup()
397 assert(not cleanup_done);
399 setKilled(KILL_CONNECTION);
404 TransactionServices::rollbackTransaction(*
this,
true);
406 for (UserVars::reference iter :
user_vars)
407 boost::checked_delete(iter.second);
410 open_tables.close_temporary_tables();
412 if (global_read_lock)
413 unlockGlobalReadLock();
420 if (client and client->isConnected())
422 assert(security_ctx);
423 if (global_system_variables.log_warnings)
425 errmsg_printf(error::WARN, ER(ER_FORCING_CLOSE), internal::my_progname, thread_id, security_ctx->username().c_str());
435 boost::checked_delete(client);
439 if (not cleanup_done)
443 plugin_sessionvar_cleanup(
this);
448 impl_->mem_root.free_root(MYF(0));
449 setCurrentMemRoot(NULL);
450 setCurrentSession(NULL);
452 plugin::Logging::postEndDo(
this);
453 plugin::EventObserver::deregisterSessionEvents(session_event_observers);
455 for (impl_c::schema_event_observers_t::reference it : impl_->schema_event_observers)
456 plugin::EventObserver::deregisterSchemaEvents(it.second);
459void Session::setClient(plugin::Client *client_arg)
462 client->setSession(
this);
465void Session::awake(Session::killed_state_t state_to_set)
467 if (state_to_set == Session::KILL_QUERY &&
command == COM_SLEEP)
470 setKilled(state_to_set);
473 if (state_to_set != Session::KILL_QUERY)
475 DRIZZLE_CONNECTION_DONE(thread_id);
480 boost::mutex::scoped_lock scopedLock(mysys_var->mutex);
501 if (mysys_var->current_cond && mysys_var->current_mutex)
503 mysys_var->current_mutex->lock();
504 mysys_var->current_cond->notify_all();
505 mysys_var->current_mutex->unlock();
514void Session::storeGlobals()
521 setCurrentSession(
this);
522 setCurrentMemRoot(&mem);
524 mysys_var= internal::my_thread_var2().get();
530 mysys_var->id= thread_id;
547 if (
variables.max_join_size == HA_POS_ERROR)
550 open_tables.version= g_refresh_version;
556 transaction.xid_state.xid.set_null();
557 transaction.xid_state.in_session=1;
571 while (not client->haveError() && getKilled() != KILL_CONNECTION)
579bool Session::schedule(
const shared_ptr& arg)
581 arg->scheduler= plugin::Scheduler::getScheduler();
582 assert(arg->scheduler);
586 long current_connections= connection_count;
588 if (current_connections > 0 and
static_cast<uint64_t
>(current_connections) > current_global_counters.max_used_connections)
590 current_global_counters.max_used_connections=
static_cast<uint64_t
>(connection_count);
593 current_global_counters.connections++;
594 arg->thread_id= arg->variables.pseudo_thread_id= global_thread_id++;
596 session::Cache::insert(arg);
598 if (DRIZZLE_UNLIKELY(plugin::EventObserver::connectSession(*arg)))
603 if (plugin::Scheduler::getScheduler()->addSession(arg))
605 DRIZZLE_CONNECTION_START(arg->getSessionId());
606 char error_message_buff[DRIZZLE_ERRMSG_SIZE];
608 arg->setKilled(Session::KILL_CONNECTION);
610 arg->status_var.aborted_connects++;
614 snprintf(error_message_buff,
sizeof(error_message_buff), ER(ER_CANT_CREATE_THREAD), 1);
615 arg->client->sendError(ER_CANT_CREATE_THREAD, error_message_buff);
631const char* Session::enter_cond(boost::condition_variable_any &cond, boost::mutex &mutex,
const char* msg)
633 const char* old_msg = get_proc_info();
634 safe_mutex_assert_owner(mutex);
635 mysys_var->current_mutex = &mutex;
636 mysys_var->current_cond = &cond;
641void Session::exit_cond(
const char* old_msg)
649 mysys_var->current_mutex->unlock();
650 boost::mutex::scoped_lock scopedLock(mysys_var->mutex);
651 mysys_var->current_mutex = 0;
652 mysys_var->current_cond = 0;
658 if (client->authenticate())
661 status_var.aborted_connects++;
668 if (not plugin::Authentication::isAuthenticated(*user(), passwd_str))
670 status_var.access_denied++;
676 if (not in_db.empty() && schema::change(*
this,
identifier::Schema(catalog().identifier(), in_db)))
690 lex().current_select= 0;
692 main_da().reset_diagnostics_area();
694 uint32_t packet_length;
695 if (not client->readCommand(&l_packet, packet_length))
698 if (getKilled() == KILL_CONNECTION)
701 if (packet_length == 0)
704 enum_server_command l_command=
static_cast<enum_server_command
>(l_packet[0]);
709 assert(packet_length);
710 return not
dispatch_command(l_command, *
this, str_ref(l_packet + 1, packet_length - 1));
716 while (not v.empty() && charset()->isspace(v.front()))
718 while (not v.empty() && (v.back() ==
';' || charset()->isspace(v.back())))
721 util::string::mptr new_query= std::make_shared<std::string>(v.data(), v.size());
724 impl_->state= std::make_shared<session::State>(v);
732 if (transaction.xid_state.xa_state != XA_NOTR)
734 my_error(ER_XAER_RMFAIL, MYF(0), xa_state_names[transaction.xid_state.xa_state]);
745 server_status&= ~SERVER_STATUS_IN_TRANS;
752 case COMMIT_AND_CHAIN:
753 result= endActiveTransaction();
754 if (result ==
true && completion == COMMIT_AND_CHAIN)
755 result= startTransaction();
757 case ROLLBACK_RELEASE:
760 case ROLLBACK_AND_CHAIN:
762 server_status&= ~SERVER_STATUS_IN_TRANS;
763 if (TransactionServices::rollbackTransaction(*
this,
true))
766 if (result ==
true && (completion == ROLLBACK_AND_CHAIN))
767 result= startTransaction();
771 my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
777 my_error(
static_cast<drizzled::error_t
>(killed_errno()), MYF(0));
779 else if (result && do_release)
781 setKilled(Session::KILL_CONNECTION);
787bool Session::endActiveTransaction()
791 if (transaction.xid_state.xa_state != XA_NOTR)
793 my_error(ER_XAER_RMFAIL, MYF(0), xa_state_names[transaction.xid_state.xa_state]);
796 if (
options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
798 server_status&= ~SERVER_STATUS_IN_TRANS;
806bool Session::startTransaction(start_transaction_option_t opt)
811 server_status|= SERVER_STATUS_IN_TRANS;
832 arg_of_last_insert_id_function=
false;
841 impl_->temporary_shares.clear();
858 lex_str->assign(
mem_root->strdup(str), str.size());
867 field_list.push_back(
new Item_return_int(
"id",3, DRIZZLE_TYPE_LONGLONG));
873 field_list.push_back(item=
new Item_empty_string(
"possible_keys", NAME_CHAR_LEN*MAX_KEY, cs));
877 field_list.push_back(item=
new Item_empty_string(
"key_len", MAX_KEY * (MAX_KEY_LENGTH_DECIMAL_WIDTH + 1 ), cs));
879 field_list.push_back(item=
new Item_empty_string(
"ref", NAME_CHAR_LEN*MAX_REF_PARTS, cs));
881 field_list.push_back(item=
new Item_return_int(
"rows", 10, DRIZZLE_TYPE_LONGLONG));
882 if (lex().describe & DESCRIBE_EXTENDED)
884 field_list.push_back(item=
new Item_float(
"filtered", 0.1234, 2, 4));
888 field_list.push_back(
new Item_empty_string(
"Extra", 255, cs));
889 result->send_fields(field_list);
892void select_result::send_error(drizzled::error_t errcode,
const char *err)
894 my_message(errcode, err, MYF(0));
901void select_to_file::send_error(drizzled::error_t errcode,
const char *err)
903 my_message(errcode, err, MYF(0));
906 (void) cache->end_io_cache();
907 (void) internal::my_close(file, MYF(0));
908 (void) internal::my_delete(path.string().c_str(), MYF(0));
914bool select_to_file::send_eof()
916 int error= test(cache->end_io_cache());
917 if (internal::my_close(file, MYF(MY_WME)))
933void select_to_file::cleanup()
938 (void) cache->end_io_cache();
939 (void) internal::my_close(file, MYF(0));
946select_to_file::select_to_file(file_exchange *ex)
949 cache(static_cast<internal::io_cache_st *>(memory::sql_calloc(sizeof(internal::io_cache_st)))),
955select_to_file::~select_to_file()
964select_export::~select_export()
966 session->sent_row_count=row_count;
986static int create_file(
Session& session,
987 fs::path &target_path,
991 fs::path to_file(exchange->file_name);
993 if (not to_file.has_root_directory())
995 target_path= fs::system_complete(catalog::local_identifier().getPath());
996 util::string::ptr schema(session.schema());
997 if (not schema->empty())
999 int count_elements= 0;
1000 for (fs::path::iterator it= to_file.begin(); it != to_file.end(); it++)
1002 if (count_elements == 1)
1003 target_path /= *schema;
1005 target_path /= to_file;
1009 target_path = exchange->file_name;
1012 if (not secure_file_priv.string().empty())
1014 if (target_path.string().substr(0, secure_file_priv.string().size()) != secure_file_priv.string())
1017 my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
"--secure-file-priv");
1022 if (!access(target_path.string().c_str(), F_OK))
1024 my_error(ER_FILE_EXISTS_ERROR, MYF(0), exchange->file_name);
1028 int file= internal::my_create(target_path.string().c_str(), 0666, O_WRONLY|O_EXCL, MYF(MY_WME));
1031 (void) fchmod(file, 0666);
1032 if (cache->init_io_cache(file, 0, internal::WRITE_CACHE, 0L, 1, MYF(MY_WME)))
1034 internal::my_close(file, MYF(0));
1035 internal::my_delete(target_path.string().c_str(), MYF(0));
1043select_export::prepare(
List<Item> &list, Select_Lex_Unit *u)
1046 bool string_results=
false, non_string_results=
false;
1048 if ((uint32_t) strlen(exchange->file_name) + NAME_LEN >= FN_REFLEN)
1050 path= exchange->file_name;
1055 List<Item>::iterator li(list.begin());
1056 while (Item* item= li++)
1058 if (item->max_length >= MAX_BLOB_WIDTH)
1064 if (item->result_type() == STRING_RESULT)
1065 string_results=
true;
1067 non_string_results=
true;
1070 field_term_length=exchange->field_term->length();
1071 field_term_char= field_term_length ?
1072 (int) (
unsigned char) (*exchange->field_term)[0] : INT_MAX;
1073 if (!exchange->line_term->length())
1074 exchange->line_term=exchange->field_term;
1075 field_sep_char= exchange->enclosed->length() ? (int) (
unsigned char) (*exchange->enclosed)[0] : field_term_char;
1076 escape_char= exchange->escaped->length() ? (int) (
unsigned char) (*exchange->escaped)[0] : -1;
1077 is_ambiguous_field_sep= test(strchr(ESCAPE_CHARS, field_sep_char));
1078 is_unsafe_field_sep= test(strchr(NUMERIC_CHARS, field_sep_char));
1079 line_sep_char= exchange->line_term->length() ? (int) (
unsigned char) (*exchange->line_term)[0] : INT_MAX;
1080 if (!field_term_length)
1081 exchange->opt_enclosed=0;
1082 if (!exchange->enclosed->length())
1083 exchange->opt_enclosed=1;
1084 fixed_row_size= (!field_term_length && !exchange->enclosed->length() &&
1086 if ((is_ambiguous_field_sep && exchange->enclosed->empty() && (string_results || is_unsafe_field_sep)) ||
1087 (exchange->opt_enclosed && non_string_results && field_term_length && strchr(NUMERIC_CHARS, field_term_char)))
1089 my_error(ER_AMBIGUOUS_FIELD_TERM, MYF(0));
1093 if ((file= create_file(*session, path, exchange, cache)) < 0)
1099bool select_export::send_data(
List<Item> &items)
1101 char buff[MAX_FIELD_WIDTH],null_buff[2],space[MAX_FIELD_WIDTH];
1102 bool space_inited=0;
1103 String tmp(buff,
sizeof(buff),&my_charset_bin),*res;
1106 if (unit->offset_limit_cnt)
1108 unit->offset_limit_cnt--;
1112 uint32_t used_length=0,items_left=items.size();
1113 List<Item>::iterator li(items.begin());
1115 if (cache->write(exchange->line_start->ptr(), exchange->line_start->length()))
1118 while (Item* item=li++)
1120 Item_result result_type=item->result_type();
1121 bool enclosed = (exchange->enclosed->length() &&
1122 (!exchange->opt_enclosed || result_type == STRING_RESULT));
1123 res=item->str_result(&tmp);
1124 if (res && enclosed)
1126 if (cache->write(exchange->enclosed->ptr(), exchange->enclosed->length()))
1131 if (!fixed_row_size)
1133 if (escape_char != -1)
1135 null_buff[0]=escape_char;
1137 if (cache->write(null_buff, 2))
1140 else if (cache->write(
"NULL", 4))
1151 used_length= min(res->length(),
static_cast<size_t>(item->max_length));
1153 used_length= res->length();
1155 if ((result_type == STRING_RESULT || is_unsafe_field_sep) &&
1158 char *pos, *start, *end;
1159 const charset_info_st*
const res_charset= res->charset();
1161 for (start= pos= (
char*) res->ptr(),end=pos+used_length; pos != end; pos++)
1163 if (use_mb(res_charset))
1165 if (
int l= my_ismbchar(res_charset, pos, end))
1204 if (needs_escaping(*pos, enclosed) &&
1209 (enclosed || !is_ambiguous_field_term ||
1210 (
int) (
unsigned char) *pos != field_term_char))
1213 tmp_buff[0]= ((int) (
unsigned char) *pos == field_sep_char &&
1214 is_ambiguous_field_sep) ? field_sep_char : escape_char;
1215 tmp_buff[1]= *pos ? *pos :
'0';
1216 if (cache->write(start, pos - start) || cache->write(tmp_buff, 2))
1221 if (cache->write(start, pos - start))
1224 else if (cache->write(res->ptr(), used_length))
1229 if (item->max_length > used_length)
1235 memset(space,
' ',
sizeof(space));
1237 uint32_t length=item->max_length-used_length;
1238 for (; length >
sizeof(space) ; length-=
sizeof(space))
1240 if (cache->write(space,
sizeof(space)))
1243 if (cache->write(space, length))
1247 if (res && enclosed)
1249 if (cache->write(exchange->enclosed->ptr(), exchange->enclosed->length()))
1254 if (cache->write(exchange->field_term->ptr(), field_term_length))
1258 if (cache->write(exchange->line_term->ptr(), exchange->line_term->length()))
1273select_dump::prepare(
List<Item> &, Select_Lex_Unit *u)
1276 return (file= create_file(*session, path, exchange, cache)) < 0;
1280bool select_dump::send_data(
List<Item> &items)
1282 List<Item>::iterator li(items.begin());
1283 char buff[MAX_FIELD_WIDTH];
1284 String tmp(buff,
sizeof(buff),&my_charset_bin),*res;
1287 if (unit->offset_limit_cnt)
1289 unit->offset_limit_cnt--;
1292 if (row_count++ > 1)
1294 my_message(ER_TOO_MANY_ROWS, ER(ER_TOO_MANY_ROWS), MYF(0));
1297 while (Item* item=li++)
1299 res=item->str_result(&tmp);
1302 if (cache->write(
"", 1))
1305 else if (cache->write(res->ptr(), res->length()))
1307 my_error(ER_ERROR_ON_WRITE, MYF(0), path.string().c_str(), errno);
1321bool select_singlerow_subselect::send_data(
List<Item> &items)
1323 Item_singlerow_subselect *it= (Item_singlerow_subselect *)item;
1326 my_message(ER_SUBQUERY_NO_1_ROW, ER(ER_SUBQUERY_NO_1_ROW), MYF(0));
1329 if (unit->offset_limit_cnt)
1331 unit->offset_limit_cnt--;
1334 List<Item>::iterator li(items.begin());
1336 for (uint32_t i= 0; (val_item= li++); i++)
1337 it->store(i, val_item);
1343void select_max_min_finder_subselect::cleanup()
1349bool select_max_min_finder_subselect::send_data(
List<Item> &items)
1351 Item_maxmin_subselect *it= (Item_maxmin_subselect *)item;
1352 List<Item>::iterator li(items.begin());
1353 Item *val_item= li++;
1354 it->register_value();
1357 cache->store(val_item);
1359 it->store(0, cache);
1365 cache= Item_cache::get_cache(val_item);
1366 switch (val_item->result_type())
1369 op= &select_max_min_finder_subselect::cmp_real;
1372 op= &select_max_min_finder_subselect::cmp_int;
1375 op= &select_max_min_finder_subselect::cmp_str;
1377 case DECIMAL_RESULT:
1378 op= &select_max_min_finder_subselect::cmp_decimal;
1386 cache->store(val_item);
1387 it->store(0, cache);
1393bool select_max_min_finder_subselect::cmp_real()
1395 Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
1396 double val1= cache->val_real(), val2= maxmin->val_real();
1398 return (cache->null_value && !maxmin->null_value) ||
1399 (!cache->null_value && !maxmin->null_value &&
1401 return (maxmin->null_value && !cache->null_value) ||
1402 (!cache->null_value && !maxmin->null_value &&
1406bool select_max_min_finder_subselect::cmp_int()
1408 Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
1409 int64_t val1= cache->val_int(), val2= maxmin->val_int();
1411 return (cache->null_value && !maxmin->null_value) ||
1412 (!cache->null_value && !maxmin->null_value &&
1414 return (maxmin->null_value && !cache->null_value) ||
1415 (!cache->null_value && !maxmin->null_value &&
1419bool select_max_min_finder_subselect::cmp_decimal()
1421 Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
1422 type::Decimal cval, *cvalue= cache->val_decimal(&cval);
1423 type::Decimal mval, *mvalue= maxmin->val_decimal(&mval);
1425 return (cache->null_value && !maxmin->null_value) ||
1426 (!cache->null_value && !maxmin->null_value &&
1428 return (maxmin->null_value && !cache->null_value) ||
1429 (!cache->null_value && !maxmin->null_value &&
1433bool select_max_min_finder_subselect::cmp_str()
1435 String *val1, *val2, buf1, buf2;
1436 Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
1441 val1= cache->val_str(&buf1);
1442 val2= maxmin->val_str(&buf1);
1444 return (cache->null_value && !maxmin->null_value) ||
1445 (!cache->null_value && !maxmin->null_value &&
1446 sortcmp(val1, val2, cache->collation.collation) > 0) ;
1447 return (maxmin->null_value && !cache->null_value) ||
1448 (!cache->null_value && !maxmin->null_value &&
1449 sortcmp(val1, val2, cache->collation.collation) < 0);
1452bool select_exists_subselect::send_data(
List<Item> &)
1454 Item_exists_subselect *it= (Item_exists_subselect *)item;
1455 if (unit->offset_limit_cnt)
1457 unit->offset_limit_cnt--;
1476str_ref Session::copy_db_to()
const
1478 if (not impl_->schema->empty())
1479 return str_ref(mem.
strdup(*impl_->schema), impl_->schema->size());
1480 my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
1488void Tmp_Table_Param::init()
1490 field_count= sum_func_count= func_count= hidden_field_count= 0;
1491 group_parts= group_length= group_null_parts= 0;
1494 precomputed_group_by= 0;
1497void Tmp_Table_Param::cleanup()
1502 boost::checked_array_delete(copy_field);
1503 save_copy_field= save_copy_field_end= copy_field= copy_field_end= 0;
1507void Session::send_kill_message()
const
1509 drizzled::error_t err=
static_cast<drizzled::error_t
>(killed_errno());
1511 my_message(err, ER(err), MYF(0));
1516 impl_->schema = std::make_shared<std::string>(new_db);
1535 plugin_sessionvar_cleanup(
this);
1538 if (getKilled() || client->wasAborted())
1540 status_var.aborted_threads++;
1543 if (client->wasAborted())
1545 if (not getKilled() &&
variables.log_warnings)
1547 errmsg_printf(error::WARN, ER(ER_NEW_ABORTING_CONNECTION)
1549 , (impl_->schema->empty() ?
"unconnected" : impl_->schema->c_str())
1550 , security_ctx->username().empty() ?
"unauthenticated" : security_ctx->username().c_str()
1551 , security_ctx->address().c_str()
1552 , (main_da().
is_error() ? main_da().message() : ER(ER_UNKNOWN_ERROR)));
1556 setKilled(Session::KILL_CONNECTION);
1558 if (client->isConnected())
1560 if (errcode != EE_OK)
1563 client->sendError(errcode, ER(errcode));
1575 server_status&= ~ (SERVER_MORE_RESULTS_EXISTS |
1576 SERVER_QUERY_NO_INDEX_USED |
1577 SERVER_QUERY_NO_GOOD_INDEX_USED);
1580 main_da().reset_diagnostics_area();
1589void Open_tables_state::close_temporary_tables()
1599 tmp_next= table->getNext();
1609void Open_tables_state::close_temporary_table(
Table *table)
1611 if (table->getPrev())
1613 table->getPrev()->setNext(table->getNext());
1614 if (table->getPrev()->getNext())
1616 table->getNext()->setPrev(table->getPrev());
1631 table->getNext()->setPrev(NULL);
1645void Open_tables_state::nukeTable(
Table *table)
1647 plugin::StorageEngine& table_type= *table->getShare()->db_type();
1648 table->free_io_cache();
1649 table->delete_table();
1650 rm_temporary_table(table_type, identifier::Table(table->getShare()->getTableIdentifier().getCatalog(), table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath()));
1651 boost::checked_delete(table->getMutableShare());
1652 boost::checked_delete(table);
1658void Session::refresh_status()
1661 memset(&status_var, 0,
sizeof(status_var));
1664 current_global_counters.max_used_connections= 1;
1665 current_global_counters.connections= 0;
1668user_var_entry* Session::getVariable(str_ref name0,
bool create_if_not_exists)
1673 string name(name0.data(), name0.size());
1674 if (UserVars::mapped_type* iter= find_ptr(
user_vars, name))
1677 if (not create_if_not_exists)
1680 user_var_entry *entry=
new user_var_entry(name.c_str(), query_id);
1682 std::pair<UserVars::iterator, bool> returnable=
user_vars.insert(make_pair(name, entry));
1684 if (not returnable.second)
1686 boost::checked_delete(entry);
1692void Session::setVariable(
const std::string &name,
const std::string &value)
1694 if (user_var_entry* var= getVariable(name,
true))
1696 var->update_hash(
false, value, STRING_RESULT, &my_charset_bin, DERIVATION_IMPLICIT,
false);
1704 if (table->query_id == session_.getQueryId())
1707 table->cursor->ha_reset();
1723 open_tables.clearDerivedTables();
1728 open_tables.mark_temp_tables_as_free_for_reuse();
1738 main_da().can_overwrite_status=
true;
1740 main_da().can_overwrite_status=
false;
1741 transaction.stmt.reset();
1744 if (open_tables.lock)
1746 unlockTables(open_tables.lock);
1747 open_tables.lock= 0;
1756 if (open_tables.open_tables_)
1757 open_tables.close_open_tables();
1766 if (lex().first_not_own_table() == *tables)
1768 lex().chop_off_not_own_tables();
1781 if (open_tables_from_list(&tables, &counter))
1784 if (not lock_tables(tables, counter, &need_reopen))
1787 if (not need_reopen)
1793 return handle_derived(&lex(), &derived_prepare) || handle_derived(&lex(), &derived_filling);
1802bool Open_tables_state::rm_temporary_table(
const identifier::Table &identifier,
bool best_effort)
1804 if (plugin::StorageEngine::dropTable(session_, identifier))
1806 if (not best_effort)
1807 errmsg_printf(error::WARN, _(
"Could not remove temporary table: '%s', error: %d"), identifier.getSQLPath().c_str(), errno);
1811bool Open_tables_state::rm_temporary_table(plugin::StorageEngine& base,
const identifier::Table &identifier)
1813 drizzled::error_t error;
1814 if (plugin::StorageEngine::dropTable(session_, base, identifier, error))
1816 errmsg_printf(error::WARN, _(
"Could not remove temporary table: '%s', error: %d"), identifier.getSQLPath().c_str(), error);
1822 impl_->temporary_shares.push_back(
new table::Singular);
1823 return impl_->temporary_shares.back();
1847 impl_->temporary_shares.push_back(
new table::Singular(
this, field_list));
1848 return impl_->temporary_shares.back();
1851void Session::clear_error(
bool full)
1857 drizzle_reset_errors(*
this,
true);
1860void Session::clearDiagnostics()
1880 return impl_->diagnostics.is_error();
1884void Session::my_ok(ha_rows affected_rows, ha_rows found_rows_arg, uint64_t passed_id,
const char *message)
1886 main_da().set_ok_status(
this, affected_rows, found_rows_arg, passed_id, message);
1893 main_da().set_eof_status(
this);
1898 return variables.storage_engine ?
variables.storage_engine : global_system_variables.storage_engine;
1901enum_tx_isolation Session::getTxIsolation()
const
1903 return (enum_tx_isolation)
variables.tx_isolation;
1908 return impl_->properties[arg];
1911void Session::setProperty0(
const std::string& arg, drizzled::util::Storable* value)
1914 impl_->properties[arg]= value;
1919 if (impl_c::schema_event_observers_t::mapped_type* i= find_ptr(impl_->schema_event_observers, db_name))
1926 impl_->schema_event_observers.erase(db_name);
1928 impl_->schema_event_observers[db_name] = observers;
1932util::string::ptr Session::schema()
const
1934 return impl_->schema;
1937void Session::resetQueryString()
1940 impl_->state.reset();
1943const std::shared_ptr<session::State>& Session::state()
1945 return impl_->state;
1948const std::string& display::type(drizzled::Session::global_read_lock_t
type)
1950 static const std::string NONE=
"NONE";
1951 static const std::string GOT_GLOBAL_READ_LOCK=
"HAS GLOBAL READ LOCK";
1952 static const std::string MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT=
"HAS GLOBAL READ LOCK WITH BLOCKING COMMIT";
1959 case Session::GOT_GLOBAL_READ_LOCK:
1960 return GOT_GLOBAL_READ_LOCK;
1961 case Session::MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT:
1962 return MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT;
1966size_t display::max_string_length(drizzled::Session::global_read_lock_t)
1968 return display::type(Session::MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT).size();
void reset_diagnostics_area()
void mark_temp_tables_as_free_for_reuse()
bool transaction_rollback_request
void set_proc_info(const char *info)
void readAndStoreQuery(str_ref)
drizzle_system_variables & variables
void cleanup_after_query()
void set_schema(const std::string &)
enum_server_command command
plugin::StorageEngine * getDefaultStorageEngine()
bool no_warnings_for_error
bool isViewable(const identifier::User &) const
bool substitute_null_with_insert_id
void markTransactionForRollback(bool all)
bool is_fatal_sub_stmt_error
void reset_for_next_command()
THR_LOCK_OWNER main_lock_id
void my_ok(ha_rows affected_rows=0, ha_rows found_rows_arg=0, uint64_t passed_id=0, const char *message=NULL)
bool endTransaction(enum enum_mysql_completiontype completion)
uint64_t first_successful_insert_id_in_cur_stmt
void close_tables_for_reopen(TableList **tables)
void resetResultsetMessage()
lex_string_t * make_lex_string(lex_string_t *, str_ref)
void close_thread_tables()
uint64_t first_successful_insert_id_in_prev_stmt
plugin::Scheduler * scheduler
bool checkUser(const std::string &passwd, const std::string &db)
uint32_t warn_count[(uint32_t) DRIZZLE_ERROR::WARN_LEVEL_END]
void disconnect(enum error_t errcode=EE_OK)
static const char *const DEFAULT_WHERE
bool openTablesLock(TableList *)
ha_rows examined_row_count
bool inTransaction() const
static int commitTransaction(Session &, bool all)
static int autocommitOrRollback(Session &, int error)
A set of Session members describing the current authenticated user.
char * strdup(const char *)
Duplicate a null-terminated string into memory allocated from within the specified Root.
void init(size_t block_size=ROOT_MIN_BLOCK_SIZE)
Initialize memory root.
static bool isAuthorized(const drizzled::identifier::User &user_ctx, const identifier::Schema &schema_identifier, bool send_error=true)
static void rewriteQuery(const std::string &schema, std::string &to_rewrite)
static void closeConnection(Session &)
static int notifyStartTransaction(Session *session, start_transaction_option_t options)
bool dispatch_command(enum_server_command command, Session &session, str_ref packet)
Convert from decimal to binary representation.
TODO: Rename this file - func.h is stupid.
DRIZZLED_API int tmpfile(const char *prefix)
int class_decimal_cmp(const type::Decimal *a, const type::Decimal *b)