~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.cc

  • Committer: Brian Aker
  • Date: 2011-02-17 10:09:00 UTC
  • mfrom: (2173.2.1 clean-include-usuage)
  • Revision ID: brian@tangent.org-20110217100900-4tpuxxzdl1sj00sh
Merge Monty for headers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
21
21
 * @file Implementation of the Session class and API
22
22
 */
23
23
 
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/table/instance.h"
53
 
 
54
 
#include "plugin/myisam/myisam.h"
55
 
#include "drizzled/internal/iocache.h"
56
 
#include "drizzled/internal/thread_var.h"
57
 
#include "drizzled/plugin/event_observer.h"
58
 
 
59
 
#include "drizzled/util/functors.h"
60
 
 
61
 
#include "drizzled/display.h"
62
 
 
63
 
#include <fcntl.h>
 
24
#include <config.h>
 
25
 
 
26
#include <drizzled/copy_field.h>
 
27
#include <drizzled/session.h>
 
28
#include <drizzled/session/cache.h>
 
29
#include <drizzled/error.h>
 
30
#include <drizzled/gettext.h>
 
31
#include <drizzled/query_id.h>
 
32
#include <drizzled/data_home.h>
 
33
#include <drizzled/sql_base.h>
 
34
#include <drizzled/lock.h>
 
35
#include <drizzled/item/cache.h>
 
36
#include <drizzled/item/float.h>
 
37
#include <drizzled/item/return_int.h>
 
38
#include <drizzled/item/empty_string.h>
 
39
#include <drizzled/show.h>
 
40
#include <drizzled/plugin/client.h>
 
41
#include <drizzled/plugin/scheduler.h>
 
42
#include <drizzled/plugin/authentication.h>
 
43
#include <drizzled/plugin/logging.h>
 
44
#include <drizzled/plugin/transactional_storage_engine.h>
 
45
#include <drizzled/plugin/query_rewrite.h>
 
46
#include <drizzled/probes.h>
 
47
#include <drizzled/table_proto.h>
 
48
#include <drizzled/pthread_globals.h>
 
49
#include <drizzled/transaction_services.h>
 
50
#include <drizzled/drizzled.h>
 
51
#include <drizzled/select_to_file.h>
 
52
#include <drizzled/select_export.h>
 
53
#include <drizzled/select_dump.h>
 
54
#include <drizzled/select_subselect.h>
 
55
#include <drizzled/select_singlerow_subselect.h>
 
56
#include <drizzled/select_max_min_finder_subselect.h>
 
57
#include <drizzled/select_exists_subselect.h>
 
58
#include <drizzled/tmp_table_param.h>
 
59
#include <drizzled/internal_error_handler.h>
 
60
 
 
61
#include <drizzled/identifier.h>
 
62
 
 
63
#include <drizzled/refresh_version.h>
 
64
 
 
65
#include <drizzled/table/singular.h>
 
66
 
 
67
#include <plugin/myisam/myisam.h>
 
68
#include <drizzled/internal/iocache.h>
 
69
#include <drizzled/internal/thread_var.h>
 
70
#include <drizzled/plugin/event_observer.h>
 
71
 
 
72
#include <drizzled/user_var_entry.h>
 
73
 
 
74
#include <drizzled/util/functors.h>
 
75
 
 
76
#include <drizzled/display.h>
 
77
 
64
78
#include <algorithm>
65
79
#include <climits>
 
80
#include <fcntl.h>
 
81
#include <sys/stat.h>
 
82
 
66
83
#include <boost/filesystem.hpp>
67
 
 
68
 
#include "drizzled/util/backtrace.h"
 
84
#include <boost/checked_delete.hpp>
 
85
 
 
86
#include <drizzled/util/backtrace.h>
 
87
 
 
88
#include <drizzled/schema.h>
69
89
 
70
90
using namespace std;
71
91
 
99
119
/*
100
120
  The following functions form part of the C plugin API
101
121
*/
102
 
int mysql_tmpfile(const char *prefix)
 
122
int tmpfile(const char *prefix)
103
123
{
104
124
  char filename[FN_REFLEN];
105
125
  int fd = internal::create_temp_file(filename, drizzle_tmpdir.c_str(), prefix, MYF(MY_WME));
110
130
  return fd;
111
131
}
112
132
 
113
 
int session_tablespace_op(const Session *session)
114
 
{
115
 
  return test(session->tablespace_op);
116
 
}
117
 
 
118
 
/**
119
 
   Set the process info field of the Session structure.
120
 
 
121
 
   This function is used by plug-ins. Internally, the
122
 
   Session::set_proc_info() function should be used.
123
 
 
124
 
   @see Session::set_proc_info
125
 
 */
126
 
void set_session_proc_info(Session *session, const char *info)
127
 
{
128
 
  session->set_proc_info(info);
129
 
}
130
 
 
131
 
const char *get_session_proc_info(Session *session)
132
 
{
133
 
  return session->get_proc_info();
134
 
}
135
 
 
136
133
void **Session::getEngineData(const plugin::MonitoredInTransaction *monitored)
137
134
{
138
135
  return static_cast<void **>(&ha_data[monitored->getId()].ha_ptr);
149
146
  return session->options & test_options;
150
147
}
151
148
 
152
 
int session_sql_command(const Session *session)
153
 
{
154
 
  return (int) session->lex->sql_command;
155
 
}
156
 
 
157
 
enum_tx_isolation session_tx_isolation(const Session *session)
158
 
{
159
 
  return (enum_tx_isolation)session->variables.tx_isolation;
160
 
}
161
 
 
162
 
Session::Session(plugin::Client *client_arg) :
 
149
Session::Session(plugin::Client *client_arg, catalog::Instance::shared_ptr catalog_arg) :
163
150
  Open_tables_state(refresh_version),
164
151
  mem_root(&main_mem_root),
165
152
  xa_id(0),
166
153
  lex(&main_lex),
167
154
  query(new std::string),
168
155
  _schema(new std::string("")),
169
 
  catalog("LOCAL"),
170
156
  client(client_arg),
171
157
  scheduler(NULL),
172
158
  scheduler_arg(NULL),
173
159
  lock_id(&main_lock_id),
174
 
  user_time(0),
 
160
  thread_stack(NULL),
 
161
  security_ctx(identifier::User::make_shared()),
 
162
  _where(Session::DEFAULT_WHERE),
 
163
  dbug_sentry(Session_SENTRY_MAGIC),
 
164
  mysys_var(0),
 
165
  command(COM_CONNECT),
 
166
  file_id(0),
 
167
  _epoch(boost::gregorian::date(1970,1,1)),
 
168
  _connect_time(boost::posix_time::microsec_clock::universal_time()),
 
169
  utime_after_lock(0),
175
170
  ha_data(plugin::num_trx_monitored_objects),
 
171
  query_id(0),
 
172
  warn_query_id(0),
176
173
  concurrent_execute_allowed(true),
177
174
  arg_of_last_insert_id_function(false),
178
175
  first_successful_insert_id_in_prev_stmt(0),
179
176
  first_successful_insert_id_in_cur_stmt(0),
180
177
  limit_found_rows(0),
 
178
  options(session_startup_options),
 
179
  row_count_func(-1),
 
180
  sent_row_count(0),
 
181
  examined_row_count(0),
 
182
  used_tables(0),
 
183
  total_warn_count(0),
 
184
  col_access(0),
 
185
  statement_id_counter(0),
 
186
  row_count(0),
 
187
  thread_id(0),
 
188
  tmp_table(0),
181
189
  _global_read_lock(NONE),
 
190
  count_cuted_fields(CHECK_FIELD_ERROR_FOR_NULL),
182
191
  _killed(NOT_KILLED),
183
192
  some_tables_deleted(false),
184
193
  no_errors(false),
186
195
  is_fatal_error(false),
187
196
  transaction_rollback_request(false),
188
197
  is_fatal_sub_stmt_error(0),
 
198
  tablespace_op(false),
189
199
  derived_tables_processing(false),
190
 
  tablespace_op(false),
191
200
  m_lip(NULL),
192
201
  cached_table(0),
193
202
  transaction_message(NULL),
194
203
  statement_message(NULL),
195
204
  session_event_observers(NULL),
 
205
  _catalog(catalog_arg),
196
206
  use_usage(false)
197
207
{
198
208
  client->setSession(this);
203
213
    will be re-initialized in init_for_queries().
204
214
  */
205
215
  memory::init_sql_alloc(&main_mem_root, memory::ROOT_MIN_BLOCK_SIZE, 0);
206
 
  thread_stack= NULL;
207
 
  count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
208
 
  col_access= 0;
209
 
  tmp_table= 0;
210
 
  used_tables= 0;
211
216
  cuted_fields= sent_row_count= row_count= 0L;
212
 
  row_count_func= -1;
213
 
  statement_id_counter= 0UL;
214
217
  // Must be reset to handle error with Session's created for init of mysqld
215
218
  lex->current_select= 0;
216
 
  start_time=(time_t) 0;
217
 
  start_utime= 0L;
218
 
  utime_after_lock= 0L;
219
219
  memset(&variables, 0, sizeof(variables));
220
 
  thread_id= 0;
221
 
  file_id = 0;
222
 
  query_id= 0;
223
 
  warn_query_id= 0;
224
 
  mysys_var= 0;
225
220
  scoreboard_index= -1;
226
 
  dbug_sentry=Session_SENTRY_MAGIC;
227
221
  cleanup_done= abort_on_warning= no_warnings_for_error= false;  
228
222
 
229
223
  /* query_cache init */
232
226
 
233
227
  /* Variables with default values */
234
228
  proc_info="login";
235
 
  where= Session::DEFAULT_WHERE;
236
 
  command= COM_CONNECT;
237
229
 
238
230
  plugin_sessionvar_init(this);
239
231
  /*
243
235
  */
244
236
  variables.pseudo_thread_id= thread_id;
245
237
  server_status= SERVER_STATUS_AUTOCOMMIT;
246
 
  options= session_startup_options;
247
238
 
248
239
  if (variables.max_join_size == HA_POS_ERROR)
249
240
    options |= OPTION_BIG_SELECTS;
255
246
  session_tx_isolation= (enum_tx_isolation) variables.tx_isolation;
256
247
  warn_list.empty();
257
248
  memset(warn_count, 0, sizeof(warn_count));
258
 
  total_warn_count= 0;
259
249
  memset(&status_var, 0, sizeof(status_var));
260
250
 
261
251
  /* Initialize sub structures */
291
281
  m_internal_handler= handler;
292
282
}
293
283
 
294
 
bool Session::handle_error(uint32_t sql_errno, const char *message,
295
 
                       DRIZZLE_ERROR::enum_warning_level level)
 
284
bool Session::handle_error(drizzled::error_t sql_errno, const char *message,
 
285
                           DRIZZLE_ERROR::enum_warning_level level)
296
286
{
297
287
  if (m_internal_handler)
298
288
  {
328
318
  m_internal_handler= NULL;
329
319
}
330
320
 
331
 
void Session::get_xid(DRIZZLE_XID *xid)
 
321
void Session::get_xid(DrizzleXid *xid)
332
322
{
333
 
  *xid = *(DRIZZLE_XID *) &transaction.xid_state.xid;
 
323
  *xid = *(DrizzleXid *) &transaction.xid_state.xid;
334
324
}
335
325
 
336
326
/* Do operations that may take a long time */
348
338
#endif
349
339
  {
350
340
    TransactionServices &transaction_services= TransactionServices::singleton();
351
 
    transaction_services.rollbackTransaction(this, true);
 
341
    transaction_services.rollbackTransaction(*this, true);
352
342
    xid_cache_delete(&transaction.xid_state);
353
343
  }
354
344
 
357
347
       iter++)
358
348
  {
359
349
    user_var_entry *entry= (*iter).second;
360
 
    delete entry;
 
350
    boost::checked_delete(entry);
361
351
  }
362
352
  user_vars.clear();
363
353
 
376
366
{
377
367
  this->checkSentry();
378
368
 
379
 
  if (client->isConnected())
 
369
  if (client and client->isConnected())
380
370
  {
 
371
    assert(security_ctx);
381
372
    if (global_system_variables.log_warnings)
382
 
        errmsg_printf(ERRMSG_LVL_WARN, ER(ER_FORCING_CLOSE),internal::my_progname,
383
 
                      thread_id,
384
 
                      (getSecurityContext().getUser().c_str() ?
385
 
                       getSecurityContext().getUser().c_str() : ""));
386
 
    disconnect(0, false);
 
373
    {
 
374
      errmsg_printf(error::WARN, ER(ER_FORCING_CLOSE),
 
375
                    internal::my_progname,
 
376
                    thread_id,
 
377
                    security_ctx->username().c_str());
 
378
    }
 
379
 
 
380
    disconnect();
387
381
  }
388
382
 
389
383
  /* Close connection */
390
 
  client->close();
391
 
  delete client;
 
384
  if (client)
 
385
  {
 
386
    client->close();
 
387
    boost::checked_delete(client);
 
388
    client= NULL;
 
389
  }
392
390
 
393
391
  if (cleanup_done == false)
394
392
    cleanup();
406
404
 
407
405
  plugin::Logging::postEndDo(this);
408
406
  plugin::EventObserver::deregisterSessionEvents(*this); 
409
 
 
410
 
  for (PropertyMap::iterator iter= life_properties.begin(); iter != life_properties.end(); iter++)
411
 
  {
412
 
    delete (*iter).second;
413
 
  }
414
 
  life_properties.clear();
415
407
}
416
408
 
417
409
void Session::setClient(plugin::Client *client_arg)
530
522
{
531
523
  if (storeGlobals())
532
524
  {
533
 
    disconnect(ER_OUT_OF_RESOURCES, true);
 
525
    disconnect(ER_OUT_OF_RESOURCES);
534
526
    status_var.aborted_connects++;
535
527
    return true;
536
528
  }
541
533
{
542
534
  if (initGlobals() || authenticate())
543
535
  {
544
 
    disconnect(0, true);
 
536
    disconnect();
545
537
    return;
546
538
  }
547
539
 
553
545
      break;
554
546
  }
555
547
 
556
 
  disconnect(0, true);
 
548
  disconnect();
557
549
}
558
550
 
559
551
bool Session::schedule(Session::shared_ptr &arg)
561
553
  arg->scheduler= plugin::Scheduler::getScheduler();
562
554
  assert(arg->scheduler);
563
555
 
564
 
  connection_count.increment();
565
 
 
566
 
  if (connection_count > current_global_counters.max_used_connections)
 
556
  ++connection_count;
 
557
 
 
558
  long current_connections= connection_count;
 
559
 
 
560
  if (current_connections > 0 and static_cast<uint64_t>(current_connections) > current_global_counters.max_used_connections)
567
561
  {
568
 
    current_global_counters.max_used_connections= connection_count;
 
562
    current_global_counters.max_used_connections= static_cast<uint64_t>(connection_count);
569
563
  }
570
564
 
571
565
  current_global_counters.connections++;
603
597
/*
604
598
  Is this session viewable by the current user?
605
599
*/
606
 
bool Session::isViewable() const
 
600
bool Session::isViewable(identifier::User::const_reference user_arg) const
607
601
{
608
 
  return plugin::Authorization::isAuthorized(current_session->getSecurityContext(),
609
 
                                             this,
610
 
                                             false);
 
602
  return plugin::Authorization::isAuthorized(user_arg, *this, false);
611
603
}
612
604
 
613
605
 
638
630
 
639
631
bool Session::authenticate()
640
632
{
641
 
  lex->start(this);
642
633
  if (client->authenticate())
643
634
    return false;
644
635
 
651
642
                        const std::string &in_db)
652
643
{
653
644
  bool is_authenticated=
654
 
    plugin::Authentication::isAuthenticated(getSecurityContext(),
655
 
                                            passwd_str);
 
645
    plugin::Authentication::isAuthenticated(*user(), passwd_str);
656
646
 
657
647
  if (is_authenticated != true)
658
648
  {
664
654
  /* Change database if necessary */
665
655
  if (not in_db.empty())
666
656
  {
667
 
    SchemaIdentifier identifier(in_db);
668
 
    if (mysql_change_db(this, identifier))
 
657
    identifier::Schema identifier(in_db);
 
658
    if (schema::change(*this, identifier))
669
659
    {
670
 
      /* mysql_change_db() has pushed the error message. */
 
660
      /* change_db() has pushed the error message. */
671
661
      return false;
672
662
    }
673
663
  }
735
725
    plugin::QueryRewriter::rewriteQuery(*_schema, *new_query);
736
726
  }
737
727
  query.reset(new_query);
738
 
  _state.reset(new State(in_packet, in_packet_length));
 
728
  _state.reset(new session::State(in_packet, in_packet_length));
739
729
 
740
730
  return true;
741
731
}
760
750
       * (Which of course should never happen...)
761
751
       */
762
752
      server_status&= ~SERVER_STATUS_IN_TRANS;
763
 
      if (transaction_services.commitTransaction(this, true))
 
753
      if (transaction_services.commitTransaction(*this, true))
764
754
        result= false;
765
755
      options&= ~(OPTION_BEGIN);
766
756
      break;
777
767
    case ROLLBACK_AND_CHAIN:
778
768
    {
779
769
      server_status&= ~SERVER_STATUS_IN_TRANS;
780
 
      if (transaction_services.rollbackTransaction(this, true))
 
770
      if (transaction_services.rollbackTransaction(*this, true))
781
771
        result= false;
782
772
      options&= ~(OPTION_BEGIN);
783
773
      if (result == true && (completion == ROLLBACK_AND_CHAIN))
791
781
 
792
782
  if (result == false)
793
783
  {
794
 
    my_error(killed_errno(), MYF(0));
 
784
    my_error(static_cast<drizzled::error_t>(killed_errno()), MYF(0));
795
785
  }
796
786
  else if ((result == true) && do_release)
797
787
  {
814
804
  if (options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
815
805
  {
816
806
    server_status&= ~SERVER_STATUS_IN_TRANS;
817
 
    if (transaction_services.commitTransaction(this, true))
 
807
    if (transaction_services.commitTransaction(*this, true))
818
808
      result= false;
819
809
  }
820
810
  options&= ~(OPTION_BEGIN);
825
815
{
826
816
  bool result= true;
827
817
 
828
 
  if (! endActiveTransaction())
 
818
  assert(! inTransaction());
 
819
 
 
820
  options|= OPTION_BEGIN;
 
821
  server_status|= SERVER_STATUS_IN_TRANS;
 
822
 
 
823
  if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
829
824
  {
830
825
    result= false;
831
826
  }
832
 
  else
833
 
  {
834
 
    options|= OPTION_BEGIN;
835
 
    server_status|= SERVER_STATUS_IN_TRANS;
836
 
 
837
 
    if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
838
 
    {
839
 
      result= false;
840
 
    }
841
 
  }
842
827
 
843
828
  return result;
844
829
}
860
845
    first_successful_insert_id_in_cur_stmt= 0;
861
846
    substitute_null_with_insert_id= true;
862
847
  }
 
848
 
863
849
  arg_of_last_insert_id_function= false;
 
850
 
864
851
  /* Free Items that were created during this execution */
865
852
  free_items();
866
 
  /* Reset where. */
867
 
  where= Session::DEFAULT_WHERE;
 
853
 
 
854
  /* Reset _where. */
 
855
  _where= Session::DEFAULT_WHERE;
868
856
 
869
857
  /* Reset the temporary shares we built */
870
858
  for_each(temporary_shares.begin(),
895
883
                                     bool allocate_lex_string)
896
884
{
897
885
  if (allocate_lex_string)
898
 
    if (!(lex_str= (LEX_STRING *)alloc(sizeof(LEX_STRING))))
 
886
    if (!(lex_str= (LEX_STRING *)getMemRoot()->allocate(sizeof(LEX_STRING))))
899
887
      return 0;
900
888
  if (!(lex_str->str= mem_root->strmake_root(str, length)))
901
889
    return 0;
941
929
  return (result->send_fields(field_list));
942
930
}
943
931
 
944
 
void select_result::send_error(uint32_t errcode, const char *err)
 
932
void select_result::send_error(drizzled::error_t errcode, const char *err)
945
933
{
946
934
  my_message(errcode, err, MYF(0));
947
935
}
950
938
  Handling writing to file
951
939
************************************************************************/
952
940
 
953
 
void select_to_file::send_error(uint32_t errcode,const char *err)
 
941
void select_to_file::send_error(drizzled::error_t errcode,const char *err)
954
942
{
955
943
  my_message(errcode, err, MYF(0));
956
944
  if (file > 0)
1504
1492
bool select_max_min_finder_subselect::cmp_decimal()
1505
1493
{
1506
1494
  Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
1507
 
  my_decimal cval, *cvalue= cache->val_decimal(&cval);
1508
 
  my_decimal mval, *mvalue= maxmin->val_decimal(&mval);
 
1495
  type::Decimal cval, *cvalue= cache->val_decimal(&cval);
 
1496
  type::Decimal mval, *mvalue= maxmin->val_decimal(&mval);
1509
1497
  if (fmax)
1510
1498
    return (cache->null_value && !maxmin->null_value) ||
1511
1499
      (!cache->null_value && !maxmin->null_value &&
1512
 
       my_decimal_cmp(cvalue, mvalue) > 0) ;
 
1500
       class_decimal_cmp(cvalue, mvalue) > 0) ;
1513
1501
  return (maxmin->null_value && !cache->null_value) ||
1514
1502
    (!cache->null_value && !maxmin->null_value &&
1515
 
     my_decimal_cmp(cvalue,mvalue) < 0);
 
1503
     class_decimal_cmp(cvalue,mvalue) < 0);
1516
1504
}
1517
1505
 
1518
1506
bool select_max_min_finder_subselect::cmp_str()
1598
1586
  /* Fix for Intel compiler */
1599
1587
  if (copy_field)
1600
1588
  {
1601
 
    delete [] copy_field;
1602
 
    save_copy_field= copy_field= 0;
 
1589
    boost::checked_array_delete(copy_field);
 
1590
    save_copy_field= save_copy_field_end= copy_field= copy_field_end= 0;
1603
1591
  }
1604
1592
}
1605
1593
 
1606
1594
void Session::send_kill_message() const
1607
1595
{
1608
 
  int err= killed_errno();
1609
 
  if (err)
 
1596
  drizzled::error_t err= static_cast<drizzled::error_t>(killed_errno());
 
1597
  if (err != EE_OK)
1610
1598
    my_message(err, ER(err), MYF(0));
1611
1599
}
1612
1600
 
1636
1624
  @param  session   Thread handle
1637
1625
  @param  all   true <=> rollback main transaction.
1638
1626
*/
1639
 
void mark_transaction_to_rollback(Session *session, bool all)
 
1627
void Session::markTransactionForRollback(bool all)
1640
1628
{
1641
 
  if (session)
1642
 
  {
1643
 
    session->is_fatal_sub_stmt_error= true;
1644
 
    session->transaction_rollback_request= all;
1645
 
  }
 
1629
  is_fatal_sub_stmt_error= true;
 
1630
  transaction_rollback_request= all;
1646
1631
}
1647
1632
 
1648
 
void Session::disconnect(uint32_t errcode, bool should_lock)
 
1633
void Session::disconnect(enum error_t errcode)
1649
1634
{
1650
1635
  /* Allow any plugins to cleanup their session variables */
1651
1636
  plugin_sessionvar_cleanup(this);
1660
1645
  {
1661
1646
    if (not getKilled() && variables.log_warnings > 1)
1662
1647
    {
1663
 
      SecurityContext *sctx= &security_ctx;
1664
 
 
1665
 
      errmsg_printf(ERRMSG_LVL_WARN, ER(ER_NEW_ABORTING_CONNECTION)
 
1648
      errmsg_printf(error::WARN, ER(ER_NEW_ABORTING_CONNECTION)
1666
1649
                  , thread_id
1667
1650
                  , (_schema->empty() ? "unconnected" : _schema->c_str())
1668
 
                  , sctx->getUser().empty() == false ? sctx->getUser().c_str() : "unauthenticated"
1669
 
                  , sctx->getIp().c_str()
 
1651
                  , security_ctx->username().empty() == false ? security_ctx->username().c_str() : "unauthenticated"
 
1652
                  , security_ctx->address().c_str()
1670
1653
                  , (main_da.is_error() ? main_da.message() : ER(ER_UNKNOWN_ERROR)));
1671
1654
    }
1672
1655
  }
1673
1656
 
1674
 
  /* Close out our connection to the client */
1675
 
  if (should_lock)
1676
 
    session::Cache::singleton().mutex().lock();
1677
 
 
1678
1657
  setKilled(Session::KILL_CONNECTION);
1679
1658
 
1680
1659
  if (client->isConnected())
1681
1660
  {
1682
 
    if (errcode)
 
1661
    if (errcode != EE_OK)
1683
1662
    {
1684
1663
      /*my_error(errcode, ER(errcode));*/
1685
1664
      client->sendError(errcode, ER(errcode));
1686
1665
    }
1687
1666
    client->close();
1688
1667
  }
1689
 
 
1690
 
  if (should_lock)
1691
 
  {
1692
 
    session::Cache::singleton().mutex().unlock();
1693
 
  }
1694
1668
}
1695
1669
 
1696
1670
void Session::reset_for_next_command()
1781
1755
  table->free_io_cache();
1782
1756
  table->delete_table();
1783
1757
 
1784
 
  TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath());
 
1758
  identifier::Table identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath());
1785
1759
  rm_temporary_table(table_type, identifier);
1786
1760
 
1787
 
  delete table->getMutableShare();
 
1761
  boost::checked_delete(table->getMutableShare());
1788
1762
 
1789
 
  /* This makes me sad, but we're allocating it via malloc */
1790
 
  delete table;
 
1763
  boost::checked_delete(table);
1791
1764
}
1792
1765
 
1793
1766
/** Clear most status variables. */
1810
1783
 
1811
1784
user_var_entry *Session::getVariable(const std::string  &name, bool create_if_not_exists)
1812
1785
{
1813
 
  UserVarsRange ppp= user_vars.equal_range(name);
 
1786
  if (cleanup_done)
 
1787
    return NULL;
1814
1788
 
1815
 
  for (UserVars::iterator iter= ppp.first;
1816
 
       iter != ppp.second; ++iter)
1817
 
  {
 
1789
  UserVars::iterator iter= user_vars.find(name);
 
1790
  if (iter != user_vars.end())
1818
1791
    return (*iter).second;
1819
 
  }
1820
1792
 
1821
1793
  if (not create_if_not_exists)
1822
1794
    return NULL;
1831
1803
 
1832
1804
  if (not returnable.second)
1833
1805
  {
1834
 
    delete entry;
 
1806
    boost::checked_delete(entry);
1835
1807
  }
1836
1808
 
1837
1809
  return entry;
1840
1812
void Session::setVariable(const std::string &name, const std::string &value)
1841
1813
{
1842
1814
  user_var_entry *updateable_var= getVariable(name.c_str(), true);
1843
 
 
1844
 
  updateable_var->update_hash(false,
1845
 
                              (void*)value.c_str(),
1846
 
                              static_cast<uint32_t>(value.length()), STRING_RESULT,
1847
 
                              &my_charset_bin,
1848
 
                              DERIVATION_IMPLICIT, false);
 
1815
  if (updateable_var)
 
1816
  {
 
1817
    updateable_var->update_hash(false,
 
1818
                                (void*)value.c_str(),
 
1819
                                static_cast<uint32_t>(value.length()), STRING_RESULT,
 
1820
                                &my_charset_bin,
 
1821
                                DERIVATION_IMPLICIT, false);
 
1822
  }
1849
1823
}
1850
1824
 
1851
1825
void Open_tables_state::mark_temp_tables_as_free_for_reuse()
1900
1874
  {
1901
1875
    TransactionServices &transaction_services= TransactionServices::singleton();
1902
1876
    main_da.can_overwrite_status= true;
1903
 
    transaction_services.autocommitOrRollback(this, is_error());
 
1877
    transaction_services.autocommitOrRollback(*this, is_error());
1904
1878
    main_da.can_overwrite_status= false;
1905
1879
    transaction.stmt.reset();
1906
1880
  }
1922
1896
  /*
1923
1897
    Note that we need to hold table::Cache::singleton().mutex() while changing the
1924
1898
    open_tables list. Another thread may work on it.
1925
 
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
 
1899
    (See: table::Cache::singleton().removeTable(), wait_completed_table())
1926
1900
    Closing a MERGE child before the parent would be fatal if the
1927
1901
    other thread tries to abort the MERGE lock in between.
1928
1902
  */
1956
1930
 
1957
1931
    if (not lock_tables(tables, counter, &need_reopen))
1958
1932
      break;
 
1933
 
1959
1934
    if (not need_reopen)
1960
1935
      return true;
 
1936
 
1961
1937
    close_tables_for_reopen(&tables);
1962
1938
  }
1963
 
  if ((mysql_handle_derived(lex, &mysql_derived_prepare) ||
1964
 
       (
1965
 
        mysql_handle_derived(lex, &mysql_derived_filling))))
 
1939
 
 
1940
  if ((handle_derived(lex, &derived_prepare) || (handle_derived(lex, &derived_filling))))
1966
1941
    return true;
1967
1942
 
1968
1943
  return false;
1974
1949
  might be an issue (lame engines).
1975
1950
*/
1976
1951
 
1977
 
bool Open_tables_state::rm_temporary_table(const TableIdentifier &identifier, bool best_effort)
 
1952
bool Open_tables_state::rm_temporary_table(const identifier::Table &identifier, bool best_effort)
1978
1953
{
1979
 
  if (plugin::StorageEngine::dropTable(*static_cast<Session *>(this), identifier))
 
1954
  if (not plugin::StorageEngine::dropTable(*static_cast<Session *>(this), identifier))
1980
1955
  {
1981
1956
    if (not best_effort)
1982
1957
    {
1983
1958
      std::string path;
1984
1959
      identifier.getSQLPath(path);
1985
 
      errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
 
1960
      errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
1986
1961
                    path.c_str(), errno);
1987
1962
    }
1988
1963
 
1992
1967
  return false;
1993
1968
}
1994
1969
 
1995
 
bool Open_tables_state::rm_temporary_table(plugin::StorageEngine *base, const TableIdentifier &identifier)
 
1970
bool Open_tables_state::rm_temporary_table(plugin::StorageEngine *base, const identifier::Table &identifier)
1996
1971
{
 
1972
  drizzled::error_t error;
1997
1973
  assert(base);
1998
1974
 
1999
 
  if (plugin::StorageEngine::dropTable(*static_cast<Session *>(this), *base, identifier))
 
1975
  if (not plugin::StorageEngine::dropTable(*static_cast<Session *>(this), *base, identifier, error))
2000
1976
  {
2001
1977
    std::string path;
2002
1978
    identifier.getSQLPath(path);
2003
 
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
2004
 
                  path.c_str(), errno);
 
1979
    errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
 
1980
                  path.c_str(), error);
2005
1981
 
2006
1982
    return true;
2007
1983
  }
2025
2001
  {
2026
2002
    bool have_proto= false;
2027
2003
 
2028
 
    message::Table *proto= table->getShare()->getTableProto();
2029
 
    if (table->getShare()->getTableProto())
 
2004
    message::Table *proto= table->getShare()->getTableMessage();
 
2005
    if (table->getShare()->getTableMessage())
2030
2006
      have_proto= true;
2031
2007
 
2032
2008
    const char *answer= have_proto ? "true" : "false";
2043
2019
  }
2044
2020
}
2045
2021
 
2046
 
bool Session::TableMessages::storeTableMessage(const TableIdentifier &identifier, message::Table &table_message)
2047
 
{
2048
 
  table_message_cache.insert(make_pair(identifier.getPath(), table_message));
2049
 
 
2050
 
  return true;
2051
 
}
2052
 
 
2053
 
bool Session::TableMessages::removeTableMessage(const TableIdentifier &identifier)
2054
 
{
2055
 
  TableMessageCache::iterator iter;
2056
 
 
2057
 
  iter= table_message_cache.find(identifier.getPath());
2058
 
 
2059
 
  if (iter == table_message_cache.end())
2060
 
    return false;
2061
 
 
2062
 
  table_message_cache.erase(iter);
2063
 
 
2064
 
  return true;
2065
 
}
2066
 
 
2067
 
bool Session::TableMessages::getTableMessage(const TableIdentifier &identifier, message::Table &table_message)
2068
 
{
2069
 
  TableMessageCache::iterator iter;
2070
 
 
2071
 
  iter= table_message_cache.find(identifier.getPath());
2072
 
 
2073
 
  if (iter == table_message_cache.end())
2074
 
    return false;
2075
 
 
2076
 
  table_message.CopyFrom(((*iter).second));
2077
 
 
2078
 
  return true;
2079
 
}
2080
 
 
2081
 
bool Session::TableMessages::doesTableMessageExist(const TableIdentifier &identifier)
2082
 
{
2083
 
  TableMessageCache::iterator iter;
2084
 
 
2085
 
  iter= table_message_cache.find(identifier.getPath());
2086
 
 
2087
 
  if (iter == table_message_cache.end())
2088
 
  {
2089
 
    return false;
2090
 
  }
2091
 
 
2092
 
  return true;
2093
 
}
2094
 
 
2095
 
bool Session::TableMessages::renameTableMessage(const TableIdentifier &from, const TableIdentifier &to)
2096
 
{
2097
 
  TableMessageCache::iterator iter;
2098
 
 
2099
 
  table_message_cache[to.getPath()]= table_message_cache[from.getPath()];
2100
 
 
2101
 
  iter= table_message_cache.find(to.getPath());
2102
 
 
2103
 
  if (iter == table_message_cache.end())
2104
 
  {
2105
 
    return false;
2106
 
  }
2107
 
 
2108
 
  (*iter).second.set_schema(to.getSchemaName());
2109
 
  (*iter).second.set_name(to.getTableName());
2110
 
 
2111
 
  return true;
2112
 
}
2113
 
 
2114
 
table::Instance *Session::getInstanceTable()
2115
 
{
2116
 
  temporary_shares.push_back(new table::Instance()); // This will not go into the tableshare cache, so no key is used.
2117
 
 
2118
 
  table::Instance *tmp_share= temporary_shares.back();
 
2022
table::Singular *Session::getInstanceTable()
 
2023
{
 
2024
  temporary_shares.push_back(new table::Singular()); // This will not go into the tableshare cache, so no key is used.
 
2025
 
 
2026
  table::Singular *tmp_share= temporary_shares.back();
2119
2027
 
2120
2028
  assert(tmp_share);
2121
2029
 
2141
2049
  @return
2142
2050
    0 if out of memory, Table object in case of success
2143
2051
*/
2144
 
table::Instance *Session::getInstanceTable(List<CreateField> &field_list)
 
2052
table::Singular *Session::getInstanceTable(List<CreateField> &field_list)
2145
2053
{
2146
 
  temporary_shares.push_back(new table::Instance(this, field_list)); // This will not go into the tableshare cache, so no key is used.
 
2054
  temporary_shares.push_back(new table::Singular(this, field_list)); // This will not go into the tableshare cache, so no key is used.
2147
2055
 
2148
 
  table::Instance *tmp_share= temporary_shares.back();
 
2056
  table::Singular *tmp_share= temporary_shares.back();
2149
2057
 
2150
2058
  assert(tmp_share);
2151
2059