~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.cc

  • Committer: Brian Aker
  • Date: 2010-12-23 22:20:46 UTC
  • mto: (2026.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2027.
  • Revision ID: brian@tangent.org-20101223222046-152kydsd1cyexfme
Update to tests, we just want to look at our own tables, not global.

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
 
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());
377
397
  if (client)
378
398
  {
379
399
    client->close();
380
 
    boost::checked_delete(client);
381
 
    client= NULL;
 
400
    delete client;
382
401
  }
383
402
 
384
403
  if (cleanup_done == false)
397
416
 
398
417
  plugin::Logging::postEndDo(this);
399
418
  plugin::EventObserver::deregisterSessionEvents(*this); 
 
419
 
 
420
  for (PropertyMap::iterator iter= life_properties.begin(); iter != life_properties.end(); iter++)
 
421
  {
 
422
    delete (*iter).second;
 
423
  }
 
424
  life_properties.clear();
400
425
}
401
426
 
402
427
void Session::setClient(plugin::Client *client_arg)
546
571
  arg->scheduler= plugin::Scheduler::getScheduler();
547
572
  assert(arg->scheduler);
548
573
 
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)
 
574
  connection_count.increment();
 
575
 
 
576
  if (connection_count > current_global_counters.max_used_connections)
554
577
  {
555
 
    current_global_counters.max_used_connections= static_cast<uint64_t>(connection_count);
 
578
    current_global_counters.max_used_connections= connection_count;
556
579
  }
557
580
 
558
581
  current_global_counters.connections++;
592
615
*/
593
616
bool Session::isViewable(identifier::User::const_reference user_arg) const
594
617
{
595
 
  return plugin::Authorization::isAuthorized(user_arg, *this, false);
 
618
  return plugin::Authorization::isAuthorized(user_arg, this, false);
596
619
}
597
620
 
598
621
 
623
646
 
624
647
bool Session::authenticate()
625
648
{
 
649
  lex->start(this);
626
650
  if (client->authenticate())
627
651
    return false;
628
652
 
635
659
                        const std::string &in_db)
636
660
{
637
661
  bool is_authenticated=
638
 
    plugin::Authentication::isAuthenticated(*user(), passwd_str);
 
662
    plugin::Authentication::isAuthenticated(user(), passwd_str);
639
663
 
640
664
  if (is_authenticated != true)
641
665
  {
647
671
  /* Change database if necessary */
648
672
  if (not in_db.empty())
649
673
  {
650
 
    identifier::Schema identifier(in_db);
651
 
    if (schema::change(*this, identifier))
 
674
    SchemaIdentifier identifier(in_db);
 
675
    if (mysql_change_db(this, identifier))
652
676
    {
653
 
      /* change_db() has pushed the error message. */
 
677
      /* mysql_change_db() has pushed the error message. */
654
678
      return false;
655
679
    }
656
680
  }
718
742
    plugin::QueryRewriter::rewriteQuery(*_schema, *new_query);
719
743
  }
720
744
  query.reset(new_query);
721
 
  _state.reset(new session::State(in_packet, in_packet_length));
 
745
  _state.reset(new State(in_packet, in_packet_length));
722
746
 
723
747
  return true;
724
748
}
743
767
       * (Which of course should never happen...)
744
768
       */
745
769
      server_status&= ~SERVER_STATUS_IN_TRANS;
746
 
      if (transaction_services.commitTransaction(*this, true))
 
770
      if (transaction_services.commitTransaction(this, true))
747
771
        result= false;
748
772
      options&= ~(OPTION_BEGIN);
749
773
      break;
760
784
    case ROLLBACK_AND_CHAIN:
761
785
    {
762
786
      server_status&= ~SERVER_STATUS_IN_TRANS;
763
 
      if (transaction_services.rollbackTransaction(*this, true))
 
787
      if (transaction_services.rollbackTransaction(this, true))
764
788
        result= false;
765
789
      options&= ~(OPTION_BEGIN);
766
790
      if (result == true && (completion == ROLLBACK_AND_CHAIN))
774
798
 
775
799
  if (result == false)
776
800
  {
777
 
    my_error(static_cast<drizzled::error_t>(killed_errno()), MYF(0));
 
801
    my_error(killed_errno(), MYF(0));
778
802
  }
779
803
  else if ((result == true) && do_release)
780
804
  {
797
821
  if (options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
798
822
  {
799
823
    server_status&= ~SERVER_STATUS_IN_TRANS;
800
 
    if (transaction_services.commitTransaction(*this, true))
 
824
    if (transaction_services.commitTransaction(this, true))
801
825
      result= false;
802
826
  }
803
827
  options&= ~(OPTION_BEGIN);
808
832
{
809
833
  bool result= true;
810
834
 
811
 
  assert(! inTransaction());
812
 
 
813
 
  options|= OPTION_BEGIN;
814
 
  server_status|= SERVER_STATUS_IN_TRANS;
815
 
 
816
 
  if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
 
835
  if (! endActiveTransaction())
817
836
  {
818
837
    result= false;
819
838
  }
 
839
  else
 
840
  {
 
841
    options|= OPTION_BEGIN;
 
842
    server_status|= SERVER_STATUS_IN_TRANS;
 
843
 
 
844
    if (plugin::TransactionalStorageEngine::notifyStartTransaction(this, opt))
 
845
    {
 
846
      result= false;
 
847
    }
 
848
  }
820
849
 
821
850
  return result;
822
851
}
838
867
    first_successful_insert_id_in_cur_stmt= 0;
839
868
    substitute_null_with_insert_id= true;
840
869
  }
841
 
 
842
870
  arg_of_last_insert_id_function= false;
843
 
 
844
871
  /* Free Items that were created during this execution */
845
872
  free_items();
846
 
 
847
 
  /* Reset _where. */
848
 
  _where= Session::DEFAULT_WHERE;
 
873
  /* Reset where. */
 
874
  where= Session::DEFAULT_WHERE;
849
875
 
850
876
  /* Reset the temporary shares we built */
851
877
  for_each(temporary_shares.begin(),
876
902
                                     bool allocate_lex_string)
877
903
{
878
904
  if (allocate_lex_string)
879
 
    if (!(lex_str= (LEX_STRING *)getMemRoot()->allocate(sizeof(LEX_STRING))))
 
905
    if (!(lex_str= (LEX_STRING *)alloc(sizeof(LEX_STRING))))
880
906
      return 0;
881
907
  if (!(lex_str->str= mem_root->strmake_root(str, length)))
882
908
    return 0;
922
948
  return (result->send_fields(field_list));
923
949
}
924
950
 
925
 
void select_result::send_error(drizzled::error_t errcode, const char *err)
 
951
void select_result::send_error(uint32_t errcode, const char *err)
926
952
{
927
953
  my_message(errcode, err, MYF(0));
928
954
}
931
957
  Handling writing to file
932
958
************************************************************************/
933
959
 
934
 
void select_to_file::send_error(drizzled::error_t errcode,const char *err)
 
960
void select_to_file::send_error(uint32_t errcode,const char *err)
935
961
{
936
962
  my_message(errcode, err, MYF(0));
937
963
  if (file > 0)
1090
1116
 
1091
1117
  /* Check if there is any blobs in data */
1092
1118
  {
1093
 
    List<Item>::iterator li(list.begin());
 
1119
    List_iterator_fast<Item> li(list);
1094
1120
    Item *item;
1095
1121
    while ((item=li++))
1096
1122
    {
1155
1181
  row_count++;
1156
1182
  Item *item;
1157
1183
  uint32_t used_length=0,items_left=items.elements;
1158
 
  List<Item>::iterator li(items.begin());
 
1184
  List_iterator_fast<Item> li(items);
1159
1185
 
1160
1186
  if (my_b_write(cache,(unsigned char*) exchange->line_start->ptr(),
1161
1187
                 exchange->line_start->length()))
1344
1370
 
1345
1371
bool select_dump::send_data(List<Item> &items)
1346
1372
{
1347
 
  List<Item>::iterator li(items.begin());
 
1373
  List_iterator_fast<Item> li(items);
1348
1374
  char buff[MAX_FIELD_WIDTH];
1349
1375
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1350
1376
  tmp.length(0);
1397
1423
    unit->offset_limit_cnt--;
1398
1424
    return(0);
1399
1425
  }
1400
 
  List<Item>::iterator li(items.begin());
 
1426
  List_iterator_fast<Item> li(items);
1401
1427
  Item *val_item;
1402
1428
  for (uint32_t i= 0; (val_item= li++); i++)
1403
1429
    it->store(i, val_item);
1415
1441
bool select_max_min_finder_subselect::send_data(List<Item> &items)
1416
1442
{
1417
1443
  Item_maxmin_subselect *it= (Item_maxmin_subselect *)item;
1418
 
  List<Item>::iterator li(items.begin());
 
1444
  List_iterator_fast<Item> li(items);
1419
1445
  Item *val_item= li++;
1420
1446
  it->register_value();
1421
1447
  if (it->assigned())
1485
1511
bool select_max_min_finder_subselect::cmp_decimal()
1486
1512
{
1487
1513
  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);
 
1514
  my_decimal cval, *cvalue= cache->val_decimal(&cval);
 
1515
  my_decimal mval, *mvalue= maxmin->val_decimal(&mval);
1490
1516
  if (fmax)
1491
1517
    return (cache->null_value && !maxmin->null_value) ||
1492
1518
      (!cache->null_value && !maxmin->null_value &&
1493
 
       class_decimal_cmp(cvalue, mvalue) > 0) ;
 
1519
       my_decimal_cmp(cvalue, mvalue) > 0) ;
1494
1520
  return (maxmin->null_value && !cache->null_value) ||
1495
1521
    (!cache->null_value && !maxmin->null_value &&
1496
 
     class_decimal_cmp(cvalue,mvalue) < 0);
 
1522
     my_decimal_cmp(cvalue,mvalue) < 0);
1497
1523
}
1498
1524
 
1499
1525
bool select_max_min_finder_subselect::cmp_str()
1579
1605
  /* Fix for Intel compiler */
1580
1606
  if (copy_field)
1581
1607
  {
1582
 
    boost::checked_array_delete(copy_field);
1583
 
    save_copy_field= save_copy_field_end= copy_field= copy_field_end= 0;
 
1608
    delete [] copy_field;
 
1609
    save_copy_field= copy_field= 0;
1584
1610
  }
1585
1611
}
1586
1612
 
1587
1613
void Session::send_kill_message() const
1588
1614
{
1589
 
  drizzled::error_t err= static_cast<drizzled::error_t>(killed_errno());
1590
 
  if (err != EE_OK)
 
1615
  int err= killed_errno();
 
1616
  if (err)
1591
1617
    my_message(err, ER(err), MYF(0));
1592
1618
}
1593
1619
 
1617
1643
  @param  session   Thread handle
1618
1644
  @param  all   true <=> rollback main transaction.
1619
1645
*/
1620
 
void Session::markTransactionForRollback(bool all)
 
1646
void mark_transaction_to_rollback(Session *session, bool all)
1621
1647
{
1622
 
  is_fatal_sub_stmt_error= true;
1623
 
  transaction_rollback_request= all;
 
1648
  if (session)
 
1649
  {
 
1650
    session->is_fatal_sub_stmt_error= true;
 
1651
    session->transaction_rollback_request= all;
 
1652
  }
1624
1653
}
1625
1654
 
1626
 
void Session::disconnect(enum error_t errcode)
 
1655
void Session::disconnect(enum drizzled_error_code errcode)
1627
1656
{
1628
1657
  /* Allow any plugins to cleanup their session variables */
1629
1658
  plugin_sessionvar_cleanup(this);
1638
1667
  {
1639
1668
    if (not getKilled() && variables.log_warnings > 1)
1640
1669
    {
1641
 
      errmsg_printf(error::WARN, ER(ER_NEW_ABORTING_CONNECTION)
 
1670
      errmsg_printf(ERRMSG_LVL_WARN, ER(ER_NEW_ABORTING_CONNECTION)
1642
1671
                  , thread_id
1643
1672
                  , (_schema->empty() ? "unconnected" : _schema->c_str())
1644
1673
                  , security_ctx->username().empty() == false ? security_ctx->username().c_str() : "unauthenticated"
1748
1777
  table->free_io_cache();
1749
1778
  table->delete_table();
1750
1779
 
1751
 
  identifier::Table identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath());
 
1780
  TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), table->getShare()->getPath());
1752
1781
  rm_temporary_table(table_type, identifier);
1753
1782
 
1754
 
  boost::checked_delete(table->getMutableShare());
 
1783
  delete table->getMutableShare();
1755
1784
 
1756
 
  boost::checked_delete(table);
 
1785
  /* This makes me sad, but we're allocating it via malloc */
 
1786
  delete table;
1757
1787
}
1758
1788
 
1759
1789
/** Clear most status variables. */
1776
1806
 
1777
1807
user_var_entry *Session::getVariable(const std::string  &name, bool create_if_not_exists)
1778
1808
{
1779
 
  if (cleanup_done)
1780
 
    return NULL;
 
1809
  UserVarsRange ppp= user_vars.equal_range(name);
1781
1810
 
1782
 
  UserVars::iterator iter= user_vars.find(name);
1783
 
  if (iter != user_vars.end())
 
1811
  for (UserVars::iterator iter= ppp.first;
 
1812
       iter != ppp.second; ++iter)
 
1813
  {
1784
1814
    return (*iter).second;
 
1815
  }
1785
1816
 
1786
1817
  if (not create_if_not_exists)
1787
1818
    return NULL;
1796
1827
 
1797
1828
  if (not returnable.second)
1798
1829
  {
1799
 
    boost::checked_delete(entry);
 
1830
    delete entry;
1800
1831
  }
1801
1832
 
1802
1833
  return entry;
1805
1836
void Session::setVariable(const std::string &name, const std::string &value)
1806
1837
{
1807
1838
  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
 
  }
 
1839
 
 
1840
  updateable_var->update_hash(false,
 
1841
                              (void*)value.c_str(),
 
1842
                              static_cast<uint32_t>(value.length()), STRING_RESULT,
 
1843
                              &my_charset_bin,
 
1844
                              DERIVATION_IMPLICIT, false);
1816
1845
}
1817
1846
 
1818
1847
void Open_tables_state::mark_temp_tables_as_free_for_reuse()
1867
1896
  {
1868
1897
    TransactionServices &transaction_services= TransactionServices::singleton();
1869
1898
    main_da.can_overwrite_status= true;
1870
 
    transaction_services.autocommitOrRollback(*this, is_error());
 
1899
    transaction_services.autocommitOrRollback(this, is_error());
1871
1900
    main_da.can_overwrite_status= false;
1872
1901
    transaction.stmt.reset();
1873
1902
  }
1889
1918
  /*
1890
1919
    Note that we need to hold table::Cache::singleton().mutex() while changing the
1891
1920
    open_tables list. Another thread may work on it.
1892
 
    (See: table::Cache::singleton().removeTable(), wait_completed_table())
 
1921
    (See: table::Cache::singleton().removeTable(), mysql_wait_completed_table())
1893
1922
    Closing a MERGE child before the parent would be fatal if the
1894
1923
    other thread tries to abort the MERGE lock in between.
1895
1924
  */
1923
1952
 
1924
1953
    if (not lock_tables(tables, counter, &need_reopen))
1925
1954
      break;
1926
 
 
1927
1955
    if (not need_reopen)
1928
1956
      return true;
1929
 
 
1930
1957
    close_tables_for_reopen(&tables);
1931
1958
  }
1932
 
 
1933
 
  if ((handle_derived(lex, &derived_prepare) || (handle_derived(lex, &derived_filling))))
 
1959
  if ((mysql_handle_derived(lex, &mysql_derived_prepare) ||
 
1960
       (
 
1961
        mysql_handle_derived(lex, &mysql_derived_filling))))
1934
1962
    return true;
1935
1963
 
1936
1964
  return false;
1942
1970
  might be an issue (lame engines).
1943
1971
*/
1944
1972
 
1945
 
bool Open_tables_state::rm_temporary_table(const identifier::Table &identifier, bool best_effort)
 
1973
bool Open_tables_state::rm_temporary_table(const TableIdentifier &identifier, bool best_effort)
1946
1974
{
1947
 
  if (not plugin::StorageEngine::dropTable(*static_cast<Session *>(this), identifier))
 
1975
  if (plugin::StorageEngine::dropTable(*static_cast<Session *>(this), identifier))
1948
1976
  {
1949
1977
    if (not best_effort)
1950
1978
    {
1951
1979
      std::string path;
1952
1980
      identifier.getSQLPath(path);
1953
 
      errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
 
1981
      errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
1954
1982
                    path.c_str(), errno);
1955
1983
    }
1956
1984
 
1960
1988
  return false;
1961
1989
}
1962
1990
 
1963
 
bool Open_tables_state::rm_temporary_table(plugin::StorageEngine *base, const identifier::Table &identifier)
 
1991
bool Open_tables_state::rm_temporary_table(plugin::StorageEngine *base, const TableIdentifier &identifier)
1964
1992
{
1965
 
  drizzled::error_t error;
1966
1993
  assert(base);
1967
1994
 
1968
 
  if (not plugin::StorageEngine::dropTable(*static_cast<Session *>(this), *base, identifier, error))
 
1995
  if (plugin::StorageEngine::dropTable(*static_cast<Session *>(this), *base, identifier))
1969
1996
  {
1970
1997
    std::string path;
1971
1998
    identifier.getSQLPath(path);
1972
 
    errmsg_printf(error::WARN, _("Could not remove temporary table: '%s', error: %d"),
1973
 
                  path.c_str(), error);
 
1999
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
 
2000
                  path.c_str(), errno);
1974
2001
 
1975
2002
    return true;
1976
2003
  }
1994
2021
  {
1995
2022
    bool have_proto= false;
1996
2023
 
1997
 
    message::Table *proto= table->getShare()->getTableMessage();
1998
 
    if (table->getShare()->getTableMessage())
 
2024
    message::Table *proto= table->getShare()->getTableProto();
 
2025
    if (table->getShare()->getTableProto())
1999
2026
      have_proto= true;
2000
2027
 
2001
2028
    const char *answer= have_proto ? "true" : "false";
2012
2039
  }
2013
2040
}
2014
2041
 
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();
 
2042
bool Session::TableMessages::storeTableMessage(const TableIdentifier &identifier, message::Table &table_message)
 
2043
{
 
2044
  table_message_cache.insert(make_pair(identifier.getPath(), table_message));
 
2045
 
 
2046
  return true;
 
2047
}
 
2048
 
 
2049
bool Session::TableMessages::removeTableMessage(const TableIdentifier &identifier)
 
2050
{
 
2051
  TableMessageCache::iterator iter;
 
2052
 
 
2053
  iter= table_message_cache.find(identifier.getPath());
 
2054
 
 
2055
  if (iter == table_message_cache.end())
 
2056
    return false;
 
2057
 
 
2058
  table_message_cache.erase(iter);
 
2059
 
 
2060
  return true;
 
2061
}
 
2062
 
 
2063
bool Session::TableMessages::getTableMessage(const TableIdentifier &identifier, message::Table &table_message)
 
2064
{
 
2065
  TableMessageCache::iterator iter;
 
2066
 
 
2067
  iter= table_message_cache.find(identifier.getPath());
 
2068
 
 
2069
  if (iter == table_message_cache.end())
 
2070
    return false;
 
2071
 
 
2072
  table_message.CopyFrom(((*iter).second));
 
2073
 
 
2074
  return true;
 
2075
}
 
2076
 
 
2077
bool Session::TableMessages::doesTableMessageExist(const TableIdentifier &identifier)
 
2078
{
 
2079
  TableMessageCache::iterator iter;
 
2080
 
 
2081
  iter= table_message_cache.find(identifier.getPath());
 
2082
 
 
2083
  if (iter == table_message_cache.end())
 
2084
  {
 
2085
    return false;
 
2086
  }
 
2087
 
 
2088
  return true;
 
2089
}
 
2090
 
 
2091
bool Session::TableMessages::renameTableMessage(const TableIdentifier &from, const TableIdentifier &to)
 
2092
{
 
2093
  TableMessageCache::iterator iter;
 
2094
 
 
2095
  table_message_cache[to.getPath()]= table_message_cache[from.getPath()];
 
2096
 
 
2097
  iter= table_message_cache.find(to.getPath());
 
2098
 
 
2099
  if (iter == table_message_cache.end())
 
2100
  {
 
2101
    return false;
 
2102
  }
 
2103
 
 
2104
  (*iter).second.set_schema(to.getSchemaName());
 
2105
  (*iter).second.set_name(to.getTableName());
 
2106
 
 
2107
  return true;
 
2108
}
 
2109
 
 
2110
table::Instance *Session::getInstanceTable()
 
2111
{
 
2112
  temporary_shares.push_back(new table::Instance()); // This will not go into the tableshare cache, so no key is used.
 
2113
 
 
2114
  table::Instance *tmp_share= temporary_shares.back();
2020
2115
 
2021
2116
  assert(tmp_share);
2022
2117
 
2042
2137
  @return
2043
2138
    0 if out of memory, Table object in case of success
2044
2139
*/
2045
 
table::Singular *Session::getInstanceTable(List<CreateField> &field_list)
 
2140
table::Instance *Session::getInstanceTable(List<CreateField> &field_list)
2046
2141
{
2047
 
  temporary_shares.push_back(new table::Singular(this, field_list)); // This will not go into the tableshare cache, so no key is used.
 
2142
  temporary_shares.push_back(new table::Instance(this, field_list)); // This will not go into the tableshare cache, so no key is used.
2048
2143
 
2049
 
  table::Singular *tmp_share= temporary_shares.back();
 
2144
  table::Instance *tmp_share= temporary_shares.back();
2050
2145
 
2051
2146
  assert(tmp_share);
2052
2147