~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.cc

  • Committer: Brian Aker
  • Date: 2010-12-24 21:27:54 UTC
  • mto: (2035.1.1 clean)
  • mto: This revision was merged to the branch mainline in revision 2037.
  • Revision ID: brian@tangent.org-20101224212754-85xvjullymvhibr9
Merge in cast() for BOOLEAN.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * @file Implementation of the Session class and API
22
22
 */
23
23
 
24
 
#include <config.h>
25
 
 
26
 
#include <drizzled/copy_field.h>
27
 
#include <drizzled/data_home.h>
28
 
#include <drizzled/display.h>
29
 
#include <drizzled/drizzled.h>
30
 
#include <drizzled/error.h>
31
 
#include <drizzled/gettext.h>
32
 
#include <drizzled/identifier.h>
33
 
#include <drizzled/internal/iocache.h>
34
 
#include <drizzled/internal/thread_var.h>
35
 
#include <drizzled/internal_error_handler.h>
36
 
#include <drizzled/item/cache.h>
37
 
#include <drizzled/item/empty_string.h>
38
 
#include <drizzled/item/float.h>
39
 
#include <drizzled/item/return_int.h>
40
 
#include <drizzled/lock.h>
41
 
#include <drizzled/plugin/authentication.h>
42
 
#include <drizzled/plugin/client.h>
43
 
#include <drizzled/plugin/event_observer.h>
44
 
#include <drizzled/plugin/logging.h>
45
 
#include <drizzled/plugin/query_rewrite.h>
46
 
#include <drizzled/plugin/scheduler.h>
47
 
#include <drizzled/plugin/transactional_storage_engine.h>
48
 
#include <drizzled/probes.h>
49
 
#include <drizzled/pthread_globals.h>
50
 
#include <drizzled/query_id.h>
51
 
#include <drizzled/refresh_version.h>
52
 
#include <drizzled/select_dump.h>
53
 
#include <drizzled/select_exists_subselect.h>
54
 
#include <drizzled/select_export.h>
55
 
#include <drizzled/select_max_min_finder_subselect.h>
56
 
#include <drizzled/select_singlerow_subselect.h>
57
 
#include <drizzled/select_subselect.h>
58
 
#include <drizzled/select_to_file.h>
59
 
#include <drizzled/session.h>
60
 
#include <drizzled/session/cache.h>
61
 
#include <drizzled/show.h>
62
 
#include <drizzled/sql_base.h>
63
 
#include <drizzled/table/singular.h>
64
 
#include <drizzled/table_proto.h>
65
 
#include <drizzled/tmp_table_param.h>
66
 
#include <drizzled/transaction_services.h>
67
 
#include <drizzled/user_var_entry.h>
68
 
#include <drizzled/util/functors.h>
69
 
#include <plugin/myisam/myisam.h>
70
 
 
 
24
#include "config.h"
 
25
#include "drizzled/session.h"
 
26
#include "drizzled/session/cache.h"
 
27
#include <sys/stat.h>
 
28
#include "drizzled/error.h"
 
29
#include "drizzled/gettext.h"
 
30
#include "drizzled/query_id.h"
 
31
#include "drizzled/data_home.h"
 
32
#include "drizzled/sql_base.h"
 
33
#include "drizzled/lock.h"
 
34
#include "drizzled/item/cache.h"
 
35
#include "drizzled/item/float.h"
 
36
#include "drizzled/item/return_int.h"
 
37
#include "drizzled/item/empty_string.h"
 
38
#include "drizzled/show.h"
 
39
#include "drizzled/plugin/client.h"
 
40
#include "drizzled/plugin/scheduler.h"
 
41
#include "drizzled/plugin/authentication.h"
 
42
#include "drizzled/plugin/logging.h"
 
43
#include "drizzled/plugin/transactional_storage_engine.h"
 
44
#include "drizzled/plugin/query_rewrite.h"
 
45
#include "drizzled/probes.h"
 
46
#include "drizzled/table_proto.h"
 
47
#include "drizzled/db.h"
 
48
#include "drizzled/pthread_globals.h"
 
49
#include "drizzled/transaction_services.h"
 
50
#include "drizzled/drizzled.h"
 
51
 
 
52
#include "drizzled/identifier.h"
 
53
 
 
54
#include "drizzled/table/instance.h"
 
55
 
 
56
#include "plugin/myisam/myisam.h"
 
57
#include "drizzled/internal/iocache.h"
 
58
#include "drizzled/internal/thread_var.h"
 
59
#include "drizzled/plugin/event_observer.h"
 
60
 
 
61
#include "drizzled/util/functors.h"
 
62
 
 
63
#include "drizzled/display.h"
 
64
 
 
65
#include <fcntl.h>
71
66
#include <algorithm>
72
67
#include <climits>
73
 
#include <fcntl.h>
74
 
#include <sys/stat.h>
75
 
 
76
68
#include <boost/filesystem.hpp>
77
 
#include <boost/checked_delete.hpp>
78
 
 
79
 
#include <drizzled/util/backtrace.h>
80
 
 
81
 
#include <drizzled/schema.h>
 
69
 
 
70
#include "drizzled/util/backtrace.h"
82
71
 
83
72
using namespace std;
84
73
 
112
101
/*
113
102
  The following functions form part of the C plugin API
114
103
*/
115
 
int tmpfile(const char *prefix)
 
104
int mysql_tmpfile(const char *prefix)
116
105
{
117
106
  char filename[FN_REFLEN];
118
107
  int fd = internal::create_temp_file(filename, drizzle_tmpdir.c_str(), prefix, MYF(MY_WME));
123
112
  return fd;
124
113
}
125
114
 
 
115
int session_tablespace_op(const Session *session)
 
116
{
 
117
  return test(session->tablespace_op);
 
118
}
 
119
 
 
120
/**
 
121
   Set the process info field of the Session structure.
 
122
 
 
123
   This function is used by plug-ins. Internally, the
 
124
   Session::set_proc_info() function should be used.
 
125
 
 
126
   @see Session::set_proc_info
 
127
 */
 
128
void set_session_proc_info(Session *session, const char *info)
 
129
{
 
130
  session->set_proc_info(info);
 
131
}
 
132
 
 
133
const char *get_session_proc_info(Session *session)
 
134
{
 
135
  return session->get_proc_info();
 
136
}
 
137
 
126
138
void **Session::getEngineData(const plugin::MonitoredInTransaction *monitored)
127
139
{
128
140
  return static_cast<void **>(&ha_data[monitored->getId()].ha_ptr);
139
151
  return session->options & test_options;
140
152
}
141
153
 
142
 
Session::Session(plugin::Client *client_arg, catalog::Instance::shared_ptr catalog_arg) :
 
154
int session_sql_command(const Session *session)
 
155
{
 
156
  return (int) session->lex->sql_command;
 
157
}
 
158
 
 
159
enum_tx_isolation session_tx_isolation(const Session *session)
 
160
{
 
161
  return (enum_tx_isolation)session->variables.tx_isolation;
 
162
}
 
163
 
 
164
Session::Session(plugin::Client *client_arg) :
143
165
  Open_tables_state(refresh_version),
144
166
  mem_root(&main_mem_root),
145
167
  xa_id(0),
146
168
  lex(&main_lex),
147
169
  query(new std::string),
148
170
  _schema(new std::string("")),
 
171
  catalog("LOCAL"),
149
172
  client(client_arg),
150
173
  scheduler(NULL),
151
174
  scheduler_arg(NULL),
152
175
  lock_id(&main_lock_id),
153
176
  thread_stack(NULL),
154
177
  security_ctx(identifier::User::make_shared()),
155
 
  _where(Session::DEFAULT_WHERE),
156
 
  dbug_sentry(Session_SENTRY_MAGIC),
157
 
  mysys_var(0),
158
 
  command(COM_CONNECT),
159
 
  file_id(0),
160
 
  _epoch(boost::gregorian::date(1970,1,1)),
161
 
  _connect_time(boost::posix_time::microsec_clock::universal_time()),
162
 
  utime_after_lock(0),
 
178
  user_time(0),
163
179
  ha_data(plugin::num_trx_monitored_objects),
164
 
  query_id(0),
165
 
  warn_query_id(0),
166
180
  concurrent_execute_allowed(true),
167
181
  arg_of_last_insert_id_function(false),
168
182
  first_successful_insert_id_in_prev_stmt(0),
169
183
  first_successful_insert_id_in_cur_stmt(0),
170
184
  limit_found_rows(0),
171
 
  options(session_startup_options),
172
 
  row_count_func(-1),
173
 
  sent_row_count(0),
174
 
  examined_row_count(0),
175
 
  used_tables(0),
176
 
  total_warn_count(0),
177
 
  col_access(0),
178
 
  statement_id_counter(0),
179
 
  row_count(0),
180
 
  thread_id(0),
181
 
  tmp_table(0),
182
185
  _global_read_lock(NONE),
183
 
  count_cuted_fields(CHECK_FIELD_ERROR_FOR_NULL),
184
186
  _killed(NOT_KILLED),
185
187
  some_tables_deleted(false),
186
188
  no_errors(false),
188
190
  is_fatal_error(false),
189
191
  transaction_rollback_request(false),
190
192
  is_fatal_sub_stmt_error(0),
 
193
  derived_tables_processing(false),
191
194
  tablespace_op(false),
192
 
  derived_tables_processing(false),
193
195
  m_lip(NULL),
194
196
  cached_table(0),
195
197
  transaction_message(NULL),
196
198
  statement_message(NULL),
197
199
  session_event_observers(NULL),
198
 
  _catalog(catalog_arg),
199
200
  use_usage(false)
200
201
{
201
202
  client->setSession(this);
206
207
    will be re-initialized in init_for_queries().
207
208
  */
208
209
  memory::init_sql_alloc(&main_mem_root, memory::ROOT_MIN_BLOCK_SIZE, 0);
 
210
  count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
 
211
  col_access= 0;
 
212
  tmp_table= 0;
 
213
  used_tables= 0;
209
214
  cuted_fields= sent_row_count= row_count= 0L;
 
215
  row_count_func= -1;
 
216
  statement_id_counter= 0UL;
210
217
  // Must be reset to handle error with Session's created for init of mysqld
211
218
  lex->current_select= 0;
 
219
  start_time=(time_t) 0;
 
220
  start_utime= 0L;
 
221
  utime_after_lock= 0L;
212
222
  memset(&variables, 0, sizeof(variables));
 
223
  thread_id= 0;
 
224
  file_id = 0;
 
225
  query_id= 0;
 
226
  warn_query_id= 0;
 
227
  mysys_var= 0;
213
228
  scoreboard_index= -1;
 
229
  dbug_sentry=Session_SENTRY_MAGIC;
214
230
  cleanup_done= abort_on_warning= no_warnings_for_error= false;  
215
231
 
216
232
  /* query_cache init */
219
235
 
220
236
  /* Variables with default values */
221
237
  proc_info="login";
 
238
  where= Session::DEFAULT_WHERE;
 
239
  command= COM_CONNECT;
222
240
 
223
241
  plugin_sessionvar_init(this);
224
242
  /*
228
246
  */
229
247
  variables.pseudo_thread_id= thread_id;
230
248
  server_status= SERVER_STATUS_AUTOCOMMIT;
 
249
  options= session_startup_options;
231
250
 
232
251
  if (variables.max_join_size == HA_POS_ERROR)
233
252
    options |= OPTION_BIG_SELECTS;
237
256
  open_options=ha_open_options;
238
257
  update_lock_default= TL_WRITE;
239
258
  session_tx_isolation= (enum_tx_isolation) variables.tx_isolation;
240
 
  warn_list.clear();
 
259
  warn_list.empty();
241
260
  memset(warn_count, 0, sizeof(warn_count));
 
261
  total_warn_count= 0;
242
262
  memset(&status_var, 0, sizeof(status_var));
243
263
 
244
264
  /* Initialize sub structures */
274
294
  m_internal_handler= handler;
275
295
}
276
296
 
277
 
bool Session::handle_error(drizzled::error_t sql_errno, const char *message,
278
 
                           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)
279
299
{
280
300
  if (m_internal_handler)
281
301
  {
311
331
  m_internal_handler= NULL;
312
332
}
313
333
 
314
 
void Session::get_xid(DrizzleXid *xid)
 
334
void Session::get_xid(DRIZZLE_XID *xid)
315
335
{
316
 
  *xid = *(DrizzleXid *) &transaction.xid_state.xid;
 
336
  *xid = *(DRIZZLE_XID *) &transaction.xid_state.xid;
317
337
}
318
338
 
319
339
/* Do operations that may take a long time */
331
351
#endif
332
352
  {
333
353
    TransactionServices &transaction_services= TransactionServices::singleton();
334
 
    transaction_services.rollbackTransaction(*this, true);
 
354
    transaction_services.rollbackTransaction(this, true);
335
355
    xid_cache_delete(&transaction.xid_state);
336
356
  }
337
357
 
340
360
       iter++)
341
361
  {
342
362
    user_var_entry *entry= (*iter).second;
343
 
    boost::checked_delete(entry);
 
363
    delete entry;
344
364
  }
345
365
  user_vars.clear();
346
366
 
359
379
{
360
380
  this->checkSentry();
361
381
 
362
 
  if (client and client->isConnected())
 
382
  if (client->isConnected())
363
383
  {
364
384
    assert(security_ctx);
365
385
    if (global_system_variables.log_warnings)
366
386
    {
367
 
      errmsg_printf(error::WARN, ER(ER_FORCING_CLOSE),
 
387
      errmsg_printf(ERRMSG_LVL_WARN, ER(ER_FORCING_CLOSE),
368
388
                    internal::my_progname,
369
389
                    thread_id,
370
390
                    security_ctx->username().c_str());
371
391
    }
372
392
 
373
 
    disconnect();
 
393
    disconnect(0, false);
374
394
  }
375
395
 
376
396
  /* Close connection */
377
 
  if (client)
378
 
  {
379
 
    client->close();
380
 
    boost::checked_delete(client);
381
 
    client= NULL;
382
 
  }
 
397
  client->close();
 
398
  delete client;
383
399
 
384
400
  if (cleanup_done == false)
385
401
    cleanup();
397
413
 
398
414
  plugin::Logging::postEndDo(this);
399
415
  plugin::EventObserver::deregisterSessionEvents(*this); 
 
416
 
 
417
  for (PropertyMap::iterator iter= life_properties.begin(); iter != life_properties.end(); iter++)
 
418
  {
 
419
    delete (*iter).second;
 
420
  }
 
421
  life_properties.clear();
400
422
}
401
423
 
402
424
void Session::setClient(plugin::Client *client_arg)
515
537
{
516
538
  if (storeGlobals())
517
539
  {
518
 
    disconnect(ER_OUT_OF_RESOURCES);
 
540
    disconnect(ER_OUT_OF_RESOURCES, true);
519
541
    status_var.aborted_connects++;
520
542
    return true;
521
543
  }
526
548
{
527
549
  if (initGlobals() || authenticate())
528
550
  {
529
 
    disconnect();
 
551
    disconnect(0, true);
530
552
    return;
531
553
  }
532
554
 
538
560
      break;
539
561
  }
540
562
 
541
 
  disconnect();
 
563
  disconnect(0, true);
542
564
}
543
565
 
544
566
bool Session::schedule(Session::shared_ptr &arg)
546
568
  arg->scheduler= plugin::Scheduler::getScheduler();
547
569
  assert(arg->scheduler);
548
570
 
549
 
  ++connection_count;
550
 
 
551
 
  long current_connections= connection_count;
552
 
 
553
 
  if (current_connections > 0 and static_cast<uint64_t>(current_connections) > current_global_counters.max_used_connections)
 
571
  connection_count.increment();
 
572
 
 
573
  if (connection_count > current_global_counters.max_used_connections)
554
574
  {
555
 
    current_global_counters.max_used_connections= static_cast<uint64_t>(connection_count);
 
575
    current_global_counters.max_used_connections= connection_count;
556
576
  }
557
577
 
558
578
  current_global_counters.connections++;
590
610
/*
591
611
  Is this session viewable by the current user?
592
612
*/
593
 
bool Session::isViewable(identifier::User::const_reference user_arg) const
 
613
bool Session::isViewable() const
594
614
{
595
 
  return plugin::Authorization::isAuthorized(user_arg, *this, false);
 
615
  return plugin::Authorization::isAuthorized(current_session->user(),
 
616
                                             this,
 
617
                                             false);
596
618
}
597
619
 
598
620
 
623
645
 
624
646
bool Session::authenticate()
625
647
{
 
648
  lex->start(this);
626
649
  if (client->authenticate())
627
650
    return false;
628
651
 
635
658
                        const std::string &in_db)
636
659
{
637
660
  bool is_authenticated=
638
 
    plugin::Authentication::isAuthenticated(*user(), passwd_str);
 
661
    plugin::Authentication::isAuthenticated(user(), passwd_str);
639
662
 
640
663
  if (is_authenticated != true)
641
664
  {
647
670
  /* Change database if necessary */
648
671
  if (not in_db.empty())
649
672
  {
650
 
    identifier::Schema identifier(in_db);
651
 
    if (schema::change(*this, identifier))
 
673
    SchemaIdentifier identifier(in_db);
 
674
    if (mysql_change_db(this, identifier))
652
675
    {
653
 
      /* change_db() has pushed the error message. */
 
676
      /* mysql_change_db() has pushed the error message. */
654
677
      return false;
655
678
    }
656
679
  }
718
741
    plugin::QueryRewriter::rewriteQuery(*_schema, *new_query);
719
742
  }
720
743
  query.reset(new_query);
721
 
  _state.reset(new session::State(in_packet, in_packet_length));
 
744
  _state.reset(new State(in_packet, in_packet_length));
722
745
 
723
746
  return true;
724
747
}
743
766
       * (Which of course should never happen...)
744
767
       */
745
768
      server_status&= ~SERVER_STATUS_IN_TRANS;
746
 
      if (transaction_services.commitTransaction(*this, true))
 
769
      if (transaction_services.commitTransaction(this, true))
747
770
        result= false;
748
771
      options&= ~(OPTION_BEGIN);
749
772
      break;
760
783
    case ROLLBACK_AND_CHAIN:
761
784
    {
762
785
      server_status&= ~SERVER_STATUS_IN_TRANS;
763
 
      if (transaction_services.rollbackTransaction(*this, true))
 
786
      if (transaction_services.rollbackTransaction(this, true))
764
787
        result= false;
765
788
      options&= ~(OPTION_BEGIN);
766
789
      if (result == true && (completion == ROLLBACK_AND_CHAIN))
774
797
 
775
798
  if (result == false)
776
799
  {
777
 
    my_error(static_cast<drizzled::error_t>(killed_errno()), MYF(0));
 
800
    my_error(killed_errno(), MYF(0));
778
801
  }
779
802
  else if ((result == true) && do_release)
780
803
  {
797
820
  if (options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
798
821
  {
799
822
    server_status&= ~SERVER_STATUS_IN_TRANS;
800
 
    if (transaction_services.commitTransaction(*this, true))
 
823
    if (transaction_services.commitTransaction(this, true))
801
824
      result= false;
802
825
  }
803
826
  options&= ~(OPTION_BEGIN);
808
831
{
809
832
  bool result= true;
810
833
 
811
 
  assert(! inTransaction());
812
 
 
813
 
  options|= OPTION_BEGIN;
814
 
  server_status|= SERVER_STATUS_IN_TRANS;
815
 
 
816
 
  if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
 
834
  if (! endActiveTransaction())
817
835
  {
818
836
    result= false;
819
837
  }
 
838
  else
 
839
  {
 
840
    options|= OPTION_BEGIN;
 
841
    server_status|= SERVER_STATUS_IN_TRANS;
 
842
 
 
843
    if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
 
844
    {
 
845
      result= false;
 
846
    }
 
847
  }
820
848
 
821
849
  return result;
822
850
}
838
866
    first_successful_insert_id_in_cur_stmt= 0;
839
867
    substitute_null_with_insert_id= true;
840
868
  }
841
 
 
842
869
  arg_of_last_insert_id_function= false;
843
 
 
844
870
  /* Free Items that were created during this execution */
845
871
  free_items();
846
 
 
847
 
  /* Reset _where. */
848
 
  _where= Session::DEFAULT_WHERE;
 
872
  /* Reset where. */
 
873
  where= Session::DEFAULT_WHERE;
849
874
 
850
875
  /* Reset the temporary shares we built */
851
876
  for_each(temporary_shares.begin(),
876
901
                                     bool allocate_lex_string)
877
902
{
878
903
  if (allocate_lex_string)
879
 
    if (!(lex_str= (LEX_STRING *)getMemRoot()->allocate(sizeof(LEX_STRING))))
 
904
    if (!(lex_str= (LEX_STRING *)alloc(sizeof(LEX_STRING))))
880
905
      return 0;
881
906
  if (!(lex_str->str= mem_root->strmake_root(str, length)))
882
907
    return 0;
922
947
  return (result->send_fields(field_list));
923
948
}
924
949
 
925
 
void select_result::send_error(drizzled::error_t errcode, const char *err)
 
950
void select_result::send_error(uint32_t errcode, const char *err)
926
951
{
927
952
  my_message(errcode, err, MYF(0));
928
953
}
931
956
  Handling writing to file
932
957
************************************************************************/
933
958
 
934
 
void select_to_file::send_error(drizzled::error_t errcode,const char *err)
 
959
void select_to_file::send_error(uint32_t errcode,const char *err)
935
960
{
936
961
  my_message(errcode, err, MYF(0));
937
962
  if (file > 0)
1090
1115
 
1091
1116
  /* Check if there is any blobs in data */
1092
1117
  {
1093
 
    List<Item>::iterator li(list.begin());
 
1118
    List_iterator_fast<Item> li(list);
1094
1119
    Item *item;
1095
1120
    while ((item=li++))
1096
1121
    {
1155
1180
  row_count++;
1156
1181
  Item *item;
1157
1182
  uint32_t used_length=0,items_left=items.elements;
1158
 
  List<Item>::iterator li(items.begin());
 
1183
  List_iterator_fast<Item> li(items);
1159
1184
 
1160
1185
  if (my_b_write(cache,(unsigned char*) exchange->line_start->ptr(),
1161
1186
                 exchange->line_start->length()))
1344
1369
 
1345
1370
bool select_dump::send_data(List<Item> &items)
1346
1371
{
1347
 
  List<Item>::iterator li(items.begin());
 
1372
  List_iterator_fast<Item> li(items);
1348
1373
  char buff[MAX_FIELD_WIDTH];
1349
1374
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1350
1375
  tmp.length(0);
1397
1422
    unit->offset_limit_cnt--;
1398
1423
    return(0);
1399
1424
  }
1400
 
  List<Item>::iterator li(items.begin());
 
1425
  List_iterator_fast<Item> li(items);
1401
1426
  Item *val_item;
1402
1427
  for (uint32_t i= 0; (val_item= li++); i++)
1403
1428
    it->store(i, val_item);
1415
1440
bool select_max_min_finder_subselect::send_data(List<Item> &items)
1416
1441
{
1417
1442
  Item_maxmin_subselect *it= (Item_maxmin_subselect *)item;
1418
 
  List<Item>::iterator li(items.begin());
 
1443
  List_iterator_fast<Item> li(items);
1419
1444
  Item *val_item= li++;
1420
1445
  it->register_value();
1421
1446
  if (it->assigned())
1485
1510
bool select_max_min_finder_subselect::cmp_decimal()
1486
1511
{
1487
1512
  Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
1488
 
  type::Decimal cval, *cvalue= cache->val_decimal(&cval);
1489
 
  type::Decimal mval, *mvalue= maxmin->val_decimal(&mval);
 
1513
  my_decimal cval, *cvalue= cache->val_decimal(&cval);
 
1514
  my_decimal mval, *mvalue= maxmin->val_decimal(&mval);
1490
1515
  if (fmax)
1491
1516
    return (cache->null_value && !maxmin->null_value) ||
1492
1517
      (!cache->null_value && !maxmin->null_value &&
1493
 
       class_decimal_cmp(cvalue, mvalue) > 0) ;
 
1518
       my_decimal_cmp(cvalue, mvalue) > 0) ;
1494
1519
  return (maxmin->null_value && !cache->null_value) ||
1495
1520
    (!cache->null_value && !maxmin->null_value &&
1496
 
     class_decimal_cmp(cvalue,mvalue) < 0);
 
1521
     my_decimal_cmp(cvalue,mvalue) < 0);
1497
1522
}
1498
1523
 
1499
1524
bool select_max_min_finder_subselect::cmp_str()
1579
1604
  /* Fix for Intel compiler */
1580
1605
  if (copy_field)
1581
1606
  {
1582
 
    boost::checked_array_delete(copy_field);
1583
 
    save_copy_field= save_copy_field_end= copy_field= copy_field_end= 0;
 
1607
    delete [] copy_field;
 
1608
    save_copy_field= copy_field= 0;
1584
1609
  }
1585
1610
}
1586
1611
 
1587
1612
void Session::send_kill_message() const
1588
1613
{
1589
 
  drizzled::error_t err= static_cast<drizzled::error_t>(killed_errno());
1590
 
  if (err != EE_OK)
 
1614
  int err= killed_errno();
 
1615
  if (err)
1591
1616
    my_message(err, ER(err), MYF(0));
1592
1617
}
1593
1618
 
1617
1642
  @param  session   Thread handle
1618
1643
  @param  all   true <=> rollback main transaction.
1619
1644
*/
1620
 
void Session::markTransactionForRollback(bool all)
 
1645
void mark_transaction_to_rollback(Session *session, bool all)
1621
1646
{
1622
 
  is_fatal_sub_stmt_error= true;
1623
 
  transaction_rollback_request= all;
 
1647
  if (session)
 
1648
  {
 
1649
    session->is_fatal_sub_stmt_error= true;
 
1650
    session->transaction_rollback_request= all;
 
1651
  }
1624
1652
}
1625
1653
 
1626
 
void Session::disconnect(enum error_t errcode)
 
1654
void Session::disconnect(uint32_t errcode, bool should_lock)
1627
1655
{
1628
1656
  /* Allow any plugins to cleanup their session variables */
1629
1657
  plugin_sessionvar_cleanup(this);
1638
1666
  {
1639
1667
    if (not getKilled() && variables.log_warnings > 1)
1640
1668
    {
1641
 
      errmsg_printf(error::WARN, ER(ER_NEW_ABORTING_CONNECTION)
 
1669
      errmsg_printf(ERRMSG_LVL_WARN, ER(ER_NEW_ABORTING_CONNECTION)
1642
1670
                  , thread_id
1643
1671
                  , (_schema->empty() ? "unconnected" : _schema->c_str())
1644
1672
                  , security_ctx->username().empty() == false ? security_ctx->username().c_str() : "unauthenticated"
1647
1675
    }
1648
1676
  }
1649
1677
 
 
1678
  /* Close out our connection to the client */
 
1679
  if (should_lock)
 
1680
    session::Cache::singleton().mutex().lock();
 
1681
 
1650
1682
  setKilled(Session::KILL_CONNECTION);
1651
1683
 
1652
1684
  if (client->isConnected())
1653
1685
  {
1654
 
    if (errcode != EE_OK)
 
1686
    if (errcode)
1655
1687
    {
1656
1688
      /*my_error(errcode, ER(errcode));*/
1657
1689
      client->sendError(errcode, ER(errcode));
1658
1690
    }
1659
1691
    client->close();
1660
1692
  }
 
1693
 
 
1694
  if (should_lock)
 
1695
  {
 
1696
    session::Cache::singleton().mutex().unlock();
 
1697
  }
1661
1698
}
1662
1699
 
1663
1700
void Session::reset_for_next_command()
1748
1785
  table->free_io_cache();
1749
1786
  table->delete_table();
1750
1787
 
1751
 
  identifier::Table identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath());
 
1788
  TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath());
1752
1789
  rm_temporary_table(table_type, identifier);
1753
1790
 
1754
 
  boost::checked_delete(table->getMutableShare());
 
1791
  delete table->getMutableShare();
1755
1792
 
1756
 
  boost::checked_delete(table);
 
1793
  /* This makes me sad, but we're allocating it via malloc */
 
1794
  delete table;
1757
1795
}
1758
1796
 
1759
1797
/** Clear most status variables. */
1776
1814
 
1777
1815
user_var_entry *Session::getVariable(const std::string  &name, bool create_if_not_exists)
1778
1816
{
1779
 
  if (cleanup_done)
1780
 
    return NULL;
 
1817
  UserVarsRange ppp= user_vars.equal_range(name);
1781
1818
 
1782
 
  UserVars::iterator iter= user_vars.find(name);
1783
 
  if (iter != user_vars.end())
 
1819
  for (UserVars::iterator iter= ppp.first;
 
1820
       iter != ppp.second; ++iter)
 
1821
  {
1784
1822
    return (*iter).second;
 
1823
  }
1785
1824
 
1786
1825
  if (not create_if_not_exists)
1787
1826
    return NULL;
1796
1835
 
1797
1836
  if (not returnable.second)
1798
1837
  {
1799
 
    boost::checked_delete(entry);
 
1838
    delete entry;
1800
1839
  }
1801
1840
 
1802
1841
  return entry;
1805
1844
void Session::setVariable(const std::string &name, const std::string &value)
1806
1845
{
1807
1846
  user_var_entry *updateable_var= getVariable(name.c_str(), true);
1808
 
  if (updateable_var)
1809
 
  {
1810
 
    updateable_var->update_hash(false,
1811
 
                                (void*)value.c_str(),
1812
 
                                static_cast<uint32_t>(value.length()), STRING_RESULT,
1813
 
                                &my_charset_bin,
1814
 
                                DERIVATION_IMPLICIT, false);
1815
 
  }
 
1847
 
 
1848
  updateable_var->update_hash(false,
 
1849
                              (void*)value.c_str(),
 
1850
                              static_cast<uint32_t>(value.length()), STRING_RESULT,
 
1851
                              &my_charset_bin,
 
1852
                              DERIVATION_IMPLICIT, false);
1816
1853
}
1817
1854
 
1818
1855
void Open_tables_state::mark_temp_tables_as_free_for_reuse()
1867
1904
  {
1868
1905
    TransactionServices &transaction_services= TransactionServices::singleton();
1869
1906
    main_da.can_overwrite_status= true;
1870
 
    transaction_services.autocommitOrRollback(*this, is_error());
 
1907
    transaction_services.autocommitOrRollback(this, is_error());
1871
1908
    main_da.can_overwrite_status= false;
1872
1909
    transaction.stmt.reset();
1873
1910
  }
1889
1926
  /*
1890
1927
    Note that we need to hold table::Cache::singleton().mutex() while changing the
1891
1928
    open_tables list. Another thread may work on it.
1892
 
    (See: table::Cache::singleton().removeTable(), wait_completed_table())
 
1929
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
1893
1930
    Closing a MERGE child before the parent would be fatal if the
1894
1931
    other thread tries to abort the MERGE lock in between.
1895
1932
  */
1923
1960
 
1924
1961
    if (not lock_tables(tables, counter, &need_reopen))
1925
1962
      break;
1926
 
 
1927
1963
    if (not need_reopen)
1928
1964
      return true;
1929
 
 
1930
1965
    close_tables_for_reopen(&tables);
1931
1966
  }
1932
 
 
1933
 
  if ((handle_derived(lex, &derived_prepare) || (handle_derived(lex, &derived_filling))))
 
1967
  if ((mysql_handle_derived(lex, &mysql_derived_prepare) ||
 
1968
       (
 
1969
        mysql_handle_derived(lex, &mysql_derived_filling))))
1934
1970
    return true;
1935
1971
 
1936
1972
  return false;
1942
1978
  might be an issue (lame engines).
1943
1979
*/
1944
1980
 
1945
 
bool Open_tables_state::rm_temporary_table(const identifier::Table &identifier, bool best_effort)
 
1981
bool Open_tables_state::rm_temporary_table(const TableIdentifier &identifier, bool best_effort)
1946
1982
{
1947
 
  if (not plugin::StorageEngine::dropTable(*static_cast<Session *>(this), identifier))
 
1983
  if (plugin::StorageEngine::dropTable(*static_cast<Session *>(this), identifier))
1948
1984
  {
1949
1985
    if (not best_effort)
1950
1986
    {
1951
1987
      std::string path;
1952
1988
      identifier.getSQLPath(path);
1953
 
      errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
 
1989
      errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
1954
1990
                    path.c_str(), errno);
1955
1991
    }
1956
1992
 
1960
1996
  return false;
1961
1997
}
1962
1998
 
1963
 
bool Open_tables_state::rm_temporary_table(plugin::StorageEngine *base, const identifier::Table &identifier)
 
1999
bool Open_tables_state::rm_temporary_table(plugin::StorageEngine *base, const TableIdentifier &identifier)
1964
2000
{
1965
 
  drizzled::error_t error;
1966
2001
  assert(base);
1967
2002
 
1968
 
  if (not plugin::StorageEngine::dropTable(*static_cast<Session *>(this), *base, identifier, error))
 
2003
  if (plugin::StorageEngine::dropTable(*static_cast<Session *>(this), *base, identifier))
1969
2004
  {
1970
2005
    std::string path;
1971
2006
    identifier.getSQLPath(path);
1972
 
    errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
1973
 
                  path.c_str(), error);
 
2007
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
 
2008
                  path.c_str(), errno);
1974
2009
 
1975
2010
    return true;
1976
2011
  }
1994
2029
  {
1995
2030
    bool have_proto= false;
1996
2031
 
1997
 
    message::Table *proto= table->getShare()->getTableMessage();
1998
 
    if (table->getShare()->getTableMessage())
 
2032
    message::Table *proto= table->getShare()->getTableProto();
 
2033
    if (table->getShare()->getTableProto())
1999
2034
      have_proto= true;
2000
2035
 
2001
2036
    const char *answer= have_proto ? "true" : "false";
2012
2047
  }
2013
2048
}
2014
2049
 
2015
 
table::Singular *Session::getInstanceTable()
2016
 
{
2017
 
  temporary_shares.push_back(new table::Singular()); // This will not go into the tableshare cache, so no key is used.
2018
 
 
2019
 
  table::Singular *tmp_share= temporary_shares.back();
 
2050
bool Session::TableMessages::storeTableMessage(const TableIdentifier &identifier, message::Table &table_message)
 
2051
{
 
2052
  table_message_cache.insert(make_pair(identifier.getPath(), table_message));
 
2053
 
 
2054
  return true;
 
2055
}
 
2056
 
 
2057
bool Session::TableMessages::removeTableMessage(const TableIdentifier &identifier)
 
2058
{
 
2059
  TableMessageCache::iterator iter;
 
2060
 
 
2061
  iter= table_message_cache.find(identifier.getPath());
 
2062
 
 
2063
  if (iter == table_message_cache.end())
 
2064
    return false;
 
2065
 
 
2066
  table_message_cache.erase(iter);
 
2067
 
 
2068
  return true;
 
2069
}
 
2070
 
 
2071
bool Session::TableMessages::getTableMessage(const TableIdentifier &identifier, message::Table &table_message)
 
2072
{
 
2073
  TableMessageCache::iterator iter;
 
2074
 
 
2075
  iter= table_message_cache.find(identifier.getPath());
 
2076
 
 
2077
  if (iter == table_message_cache.end())
 
2078
    return false;
 
2079
 
 
2080
  table_message.CopyFrom(((*iter).second));
 
2081
 
 
2082
  return true;
 
2083
}
 
2084
 
 
2085
bool Session::TableMessages::doesTableMessageExist(const TableIdentifier &identifier)
 
2086
{
 
2087
  TableMessageCache::iterator iter;
 
2088
 
 
2089
  iter= table_message_cache.find(identifier.getPath());
 
2090
 
 
2091
  if (iter == table_message_cache.end())
 
2092
  {
 
2093
    return false;
 
2094
  }
 
2095
 
 
2096
  return true;
 
2097
}
 
2098
 
 
2099
bool Session::TableMessages::renameTableMessage(const TableIdentifier &from, const TableIdentifier &to)
 
2100
{
 
2101
  TableMessageCache::iterator iter;
 
2102
 
 
2103
  table_message_cache[to.getPath()]= table_message_cache[from.getPath()];
 
2104
 
 
2105
  iter= table_message_cache.find(to.getPath());
 
2106
 
 
2107
  if (iter == table_message_cache.end())
 
2108
  {
 
2109
    return false;
 
2110
  }
 
2111
 
 
2112
  (*iter).second.set_schema(to.getSchemaName());
 
2113
  (*iter).second.set_name(to.getTableName());
 
2114
 
 
2115
  return true;
 
2116
}
 
2117
 
 
2118
table::Instance *Session::getInstanceTable()
 
2119
{
 
2120
  temporary_shares.push_back(new table::Instance()); // This will not go into the tableshare cache, so no key is used.
 
2121
 
 
2122
  table::Instance *tmp_share= temporary_shares.back();
2020
2123
 
2021
2124
  assert(tmp_share);
2022
2125
 
2042
2145
  @return
2043
2146
    0 if out of memory, Table object in case of success
2044
2147
*/
2045
 
table::Singular *Session::getInstanceTable(List<CreateField> &field_list)
 
2148
table::Instance *Session::getInstanceTable(List<CreateField> &field_list)
2046
2149
{
2047
 
  temporary_shares.push_back(new table::Singular(this, field_list)); // This will not go into the tableshare cache, so no key is used.
 
2150
  temporary_shares.push_back(new table::Instance(this, field_list)); // This will not go into the tableshare cache, so no key is used.
2048
2151
 
2049
 
  table::Singular *tmp_share= temporary_shares.back();
 
2152
  table::Instance *tmp_share= temporary_shares.back();
2050
2153
 
2051
2154
  assert(tmp_share);
2052
2155