~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.cc

Remove PLUGIN and MODULES.

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 <drizzled/server_includes.h>
 
24
#include "config.h"
25
25
#include <drizzled/session.h>
 
26
#include "drizzled/session_list.h"
26
27
#include <sys/stat.h>
27
 
#include <mysys/mysys_err.h>
 
28
#include "drizzled/my_error.h"
28
29
#include <drizzled/error.h>
29
30
#include <drizzled/gettext.h>
30
31
#include <drizzled/query_id.h>
41
42
#include "drizzled/plugin/authentication.h"
42
43
#include "drizzled/probes.h"
43
44
#include "drizzled/table_proto.h"
44
 
 
 
45
#include "drizzled/db.h"
 
46
#include "drizzled/pthread_globals.h"
 
47
 
 
48
#include "plugin/myisam/myisam.h"
 
49
#include "drizzled/internal/iocache.h"
 
50
 
 
51
#include <fcntl.h>
45
52
#include <algorithm>
 
53
#include <climits>
46
54
 
47
55
using namespace std;
48
56
using namespace drizzled;
66
74
extern uint32_t max_used_connections;
67
75
extern drizzled::atomic<uint32_t> connection_count;
68
76
 
69
 
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
70
 
/* Used templates */
71
 
template class List<Key>;
72
 
template class List_iterator<Key>;
73
 
template class List<Key_part_spec>;
74
 
template class List_iterator<Key_part_spec>;
75
 
template class List<AlterDrop>;
76
 
template class List_iterator<AlterDrop>;
77
 
template class List<AlterColumn>;
78
 
template class List_iterator<AlterColumn>;
79
 
#endif
80
77
 
81
78
/****************************************************************************
82
79
** User variables
111
108
extern "C" int mysql_tmpfile(const char *prefix)
112
109
{
113
110
  char filename[FN_REFLEN];
114
 
  File fd = create_temp_file(filename, drizzle_tmpdir, prefix, MYF(MY_WME));
 
111
  int fd = create_temp_file(filename, drizzle_tmpdir, prefix, MYF(MY_WME));
115
112
  if (fd >= 0) {
116
113
    unlink(filename);
117
114
  }
145
142
  return session->get_proc_info();
146
143
}
147
144
 
148
 
extern "C"
149
 
void **session_ha_data(const Session *session, const plugin::StorageEngine *engine)
150
 
{
151
 
  return (void **) &session->ha_data[engine->slot].ha_ptr;
 
145
void **Session::getEngineData(const plugin::StorageEngine *engine)
 
146
{
 
147
  return static_cast<void **>(&ha_data[engine->slot].ha_ptr);
 
148
}
 
149
 
 
150
Ha_trx_info *Session::getEngineInfo(const plugin::StorageEngine *engine,
 
151
                                    size_t index)
 
152
{
 
153
  return &ha_data[engine->getSlot()].ha_info[index];
152
154
}
153
155
 
154
156
extern "C"
205
207
    the destructor works OK in case of an error. The main_mem_root
206
208
    will be re-initialized in init_for_queries().
207
209
  */
208
 
  init_sql_alloc(&main_mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
 
210
  memory::init_sql_alloc(&main_mem_root, memory::ROOT_MIN_BLOCK_SIZE, 0);
209
211
  thread_stack= NULL;
210
212
  count_cuted_fields= CHECK_FIELD_IGNORE;
211
213
  killed= NOT_KILLED;
226
228
  query_id= 0;
227
229
  query= NULL;
228
230
  query_length= 0;
229
 
  warn_id= 0;
 
231
  warn_query_id= 0;
230
232
  memset(ha_data, 0, sizeof(ha_data));
231
233
  replication_data= 0;
232
234
  mysys_var= 0;
265
267
  memset(&status_var, 0, sizeof(status_var));
266
268
 
267
269
  /* Initialize sub structures */
268
 
  init_sql_alloc(&warn_root, WARN_ALLOC_BLOCK_SIZE, WARN_ALLOC_PREALLOC_SIZE);
 
270
  memory::init_sql_alloc(&warn_root, WARN_ALLOC_BLOCK_SIZE, WARN_ALLOC_PREALLOC_SIZE);
269
271
  hash_init(&user_vars, system_charset_info, USER_VARS_HASH_SIZE, 0, 0,
270
272
            (hash_get_key) get_var_key,
271
273
            (hash_free_key) free_user_var, 0);
280
282
void Session::free_items()
281
283
{
282
284
  Item *next;
283
 
  /* This works because items are allocated with sql_alloc() */
 
285
  /* This works because items are allocated with memory::sql_alloc() */
284
286
  for (; free_list; free_list= next)
285
287
  {
286
288
    next= free_list->next;
511
513
 
512
514
/*
513
515
  Remember the location of thread info, the structure needed for
514
 
  sql_alloc() and the structure for the net buffer
 
516
  memory::sql_alloc() and the structure for the net buffer
515
517
*/
516
518
bool Session::storeGlobals()
517
519
{
603
605
  scheduler= plugin::Scheduler::getScheduler();
604
606
  assert(scheduler);
605
607
 
606
 
  ++connection_count;
 
608
  connection_count.increment();
607
609
 
608
610
  if (connection_count > max_used_connections)
609
611
    max_used_connections= connection_count;
611
613
  thread_id= variables.pseudo_thread_id= global_thread_id++;
612
614
 
613
615
  pthread_mutex_lock(&LOCK_thread_count);
614
 
  session_list.push_back(this);
 
616
  getSessionList().push_back(this);
615
617
  pthread_mutex_unlock(&LOCK_thread_count);
616
618
 
617
619
  if (scheduler->addSession(this))
634
636
  return false;
635
637
}
636
638
 
 
639
 
 
640
const char* Session::enter_cond(pthread_cond_t *cond,
 
641
                                pthread_mutex_t* mutex,
 
642
                                const char* msg)
 
643
{
 
644
  const char* old_msg = get_proc_info();
 
645
  safe_mutex_assert_owner(mutex);
 
646
  mysys_var->current_mutex = mutex;
 
647
  mysys_var->current_cond = cond;
 
648
  this->set_proc_info(msg);
 
649
  return old_msg;
 
650
}
 
651
 
 
652
void Session::exit_cond(const char* old_msg)
 
653
{
 
654
  /*
 
655
    Putting the mutex unlock in exit_cond() ensures that
 
656
    mysys_var->current_mutex is always unlocked _before_ mysys_var->mutex is
 
657
    locked (if that would not be the case, you'll get a deadlock if someone
 
658
    does a Session::awake() on you).
 
659
  */
 
660
  pthread_mutex_unlock(mysys_var->current_mutex);
 
661
  pthread_mutex_lock(&mysys_var->mutex);
 
662
  mysys_var->current_mutex = 0;
 
663
  mysys_var->current_cond = 0;
 
664
  this->set_proc_info(old_msg);
 
665
  pthread_mutex_unlock(&mysys_var->mutex);
 
666
}
 
667
 
637
668
bool Session::authenticate()
638
669
{
639
670
  lex_start(this);
1008
1039
  return (result->send_fields(field_list));
1009
1040
}
1010
1041
 
 
1042
void select_result::send_error(uint32_t errcode, const char *err)
 
1043
{
 
1044
  my_message(errcode, err, MYF(0));
 
1045
}
 
1046
 
1011
1047
/************************************************************************
1012
1048
  Handling writing to file
1013
1049
************************************************************************/
1017
1053
  my_message(errcode, err, MYF(0));
1018
1054
  if (file > 0)
1019
1055
  {
1020
 
    (void) end_io_cache(&cache);
 
1056
    (void) end_io_cache(cache);
1021
1057
    (void) my_close(file, MYF(0));
1022
1058
    (void) my_delete(path, MYF(0));             // Delete file on error
1023
1059
    file= -1;
1027
1063
 
1028
1064
bool select_to_file::send_eof()
1029
1065
{
1030
 
  int error= test(end_io_cache(&cache));
 
1066
  int error= test(end_io_cache(cache));
1031
1067
  if (my_close(file, MYF(MY_WME)))
1032
1068
    error= 1;
1033
1069
  if (!error)
1049
1085
  /* In case of error send_eof() may be not called: close the file here. */
1050
1086
  if (file >= 0)
1051
1087
  {
1052
 
    (void) end_io_cache(&cache);
 
1088
    (void) end_io_cache(cache);
1053
1089
    (void) my_close(file, MYF(0));
1054
1090
    file= -1;
1055
1091
  }
1057
1093
  row_count= 0;
1058
1094
}
1059
1095
 
 
1096
select_to_file::select_to_file(file_exchange *ex)
 
1097
  : exchange(ex),
 
1098
    file(-1),
 
1099
    cache(static_cast<IO_CACHE *>(memory::sql_calloc(sizeof(IO_CACHE)))),
 
1100
    row_count(0L)
 
1101
{
 
1102
  path[0]=0;
 
1103
}
1060
1104
 
1061
1105
select_to_file::~select_to_file()
1062
1106
{
1063
 
  if (file >= 0)
1064
 
  {                                     // This only happens in case of error
1065
 
    (void) end_io_cache(&cache);
1066
 
    (void) my_close(file, MYF(0));
1067
 
    file= -1;
1068
 
  }
 
1107
  cleanup();
1069
1108
}
1070
1109
 
1071
1110
/***************************************************************************
1094
1133
*/
1095
1134
 
1096
1135
 
1097
 
static File create_file(Session *session, char *path, file_exchange *exchange, IO_CACHE *cache)
 
1136
static int create_file(Session *session, char *path, file_exchange *exchange, IO_CACHE *cache)
1098
1137
{
1099
 
  File file;
 
1138
  int file;
1100
1139
  uint32_t option= MY_UNPACK_FILENAME | MY_RELATIVE_PATH;
1101
1140
 
1102
1141
#ifdef DONT_ALLOW_FULL_LOAD_DATA_PATHS
1129
1168
  /* Create the file world readable */
1130
1169
  if ((file= my_create(path, 0666, O_WRONLY|O_EXCL, MYF(MY_WME))) < 0)
1131
1170
    return file;
1132
 
#ifdef HAVE_FCHMOD
1133
1171
  (void) fchmod(file, 0666);                    // Because of umask()
1134
 
#else
1135
 
  (void) chmod(path, 0666);
1136
 
#endif
1137
1172
  if (init_io_cache(cache, file, 0L, WRITE_CACHE, 0L, 1, MYF(MY_WME)))
1138
1173
  {
1139
1174
    my_close(file, MYF(0));
1198
1233
    return 1;
1199
1234
  }
1200
1235
 
1201
 
  if ((file= create_file(session, path, exchange, &cache)) < 0)
 
1236
  if ((file= create_file(session, path, exchange, cache)) < 0)
1202
1237
    return 1;
1203
1238
 
1204
1239
  return 0;
1228
1263
  uint32_t used_length=0,items_left=items.elements;
1229
1264
  List_iterator_fast<Item> li(items);
1230
1265
 
1231
 
  if (my_b_write(&cache,(unsigned char*) exchange->line_start->ptr(),
 
1266
  if (my_b_write(cache,(unsigned char*) exchange->line_start->ptr(),
1232
1267
                 exchange->line_start->length()))
1233
1268
    goto err;
1234
1269
  while ((item=li++))
1239
1274
    res=item->str_result(&tmp);
1240
1275
    if (res && enclosed)
1241
1276
    {
1242
 
      if (my_b_write(&cache,(unsigned char*) exchange->enclosed->ptr(),
 
1277
      if (my_b_write(cache,(unsigned char*) exchange->enclosed->ptr(),
1243
1278
                     exchange->enclosed->length()))
1244
1279
        goto err;
1245
1280
    }
1251
1286
        {
1252
1287
          null_buff[0]=escape_char;
1253
1288
          null_buff[1]='N';
1254
 
          if (my_b_write(&cache,(unsigned char*) null_buff,2))
 
1289
          if (my_b_write(cache,(unsigned char*) null_buff,2))
1255
1290
            goto err;
1256
1291
        }
1257
 
        else if (my_b_write(&cache,(unsigned char*) "NULL",4))
 
1292
        else if (my_b_write(cache,(unsigned char*) "NULL",4))
1258
1293
          goto err;
1259
1294
      }
1260
1295
      else
1344
1379
                          is_ambiguous_field_sep) ?
1345
1380
              field_sep_char : escape_char;
1346
1381
            tmp_buff[1]= *pos ? *pos : '0';
1347
 
            if (my_b_write(&cache,(unsigned char*) start,(uint32_t) (pos-start)) ||
1348
 
                my_b_write(&cache,(unsigned char*) tmp_buff,2))
 
1382
            if (my_b_write(cache,(unsigned char*) start,(uint32_t) (pos-start)) ||
 
1383
                my_b_write(cache,(unsigned char*) tmp_buff,2))
1349
1384
              goto err;
1350
1385
            start=pos+1;
1351
1386
          }
1352
1387
        }
1353
 
        if (my_b_write(&cache,(unsigned char*) start,(uint32_t) (pos-start)))
 
1388
        if (my_b_write(cache,(unsigned char*) start,(uint32_t) (pos-start)))
1354
1389
          goto err;
1355
1390
      }
1356
 
      else if (my_b_write(&cache,(unsigned char*) res->ptr(),used_length))
 
1391
      else if (my_b_write(cache,(unsigned char*) res->ptr(),used_length))
1357
1392
        goto err;
1358
1393
    }
1359
1394
    if (fixed_row_size)
1369
1404
        uint32_t length=item->max_length-used_length;
1370
1405
        for (; length > sizeof(space) ; length-=sizeof(space))
1371
1406
        {
1372
 
          if (my_b_write(&cache,(unsigned char*) space,sizeof(space)))
 
1407
          if (my_b_write(cache,(unsigned char*) space,sizeof(space)))
1373
1408
            goto err;
1374
1409
        }
1375
 
        if (my_b_write(&cache,(unsigned char*) space,length))
 
1410
        if (my_b_write(cache,(unsigned char*) space,length))
1376
1411
          goto err;
1377
1412
      }
1378
1413
    }
1379
1414
    if (res && enclosed)
1380
1415
    {
1381
 
      if (my_b_write(&cache, (unsigned char*) exchange->enclosed->ptr(),
 
1416
      if (my_b_write(cache, (unsigned char*) exchange->enclosed->ptr(),
1382
1417
                     exchange->enclosed->length()))
1383
1418
        goto err;
1384
1419
    }
1385
1420
    if (--items_left)
1386
1421
    {
1387
 
      if (my_b_write(&cache, (unsigned char*) exchange->field_term->ptr(),
 
1422
      if (my_b_write(cache, (unsigned char*) exchange->field_term->ptr(),
1388
1423
                     field_term_length))
1389
1424
        goto err;
1390
1425
    }
1391
1426
  }
1392
 
  if (my_b_write(&cache,(unsigned char*) exchange->line_term->ptr(),
 
1427
  if (my_b_write(cache,(unsigned char*) exchange->line_term->ptr(),
1393
1428
                 exchange->line_term->length()))
1394
1429
    goto err;
1395
1430
  return(0);
1407
1442
select_dump::prepare(List<Item> &, Select_Lex_Unit *u)
1408
1443
{
1409
1444
  unit= u;
1410
 
  return (int) ((file= create_file(session, path, exchange, &cache)) < 0);
 
1445
  return (int) ((file= create_file(session, path, exchange, cache)) < 0);
1411
1446
}
1412
1447
 
1413
1448
 
1434
1469
    res=item->str_result(&tmp);
1435
1470
    if (!res)                                   // If NULL
1436
1471
    {
1437
 
      if (my_b_write(&cache,(unsigned char*) "",1))
 
1472
      if (my_b_write(cache,(unsigned char*) "",1))
1438
1473
        goto err;
1439
1474
    }
1440
 
    else if (my_b_write(&cache,(unsigned char*) res->ptr(),res->length()))
 
1475
    else if (my_b_write(cache,(unsigned char*) res->ptr(),res->length()))
1441
1476
    {
1442
 
      my_error(ER_ERROR_ON_WRITE, MYF(0), path, my_errno);
 
1477
      my_error(ER_ERROR_ON_WRITE, MYF(0), path, errno);
1443
1478
      goto err;
1444
1479
    }
1445
1480
  }
1691
1726
  set_open_tables_state(backup);
1692
1727
}
1693
1728
 
1694
 
 
1695
1729
bool Session::set_db(const char *new_db, size_t length)
1696
1730
{
1697
1731
  /* Do not reallocate memory if current chunk is big enough. */
1704
1738
}
1705
1739
 
1706
1740
 
 
1741
 
 
1742
 
1707
1743
/**
1708
1744
  Check the killed state of a user thread
1709
1745
  @param session  user thread
1726
1762
}
1727
1763
 
1728
1764
 
1729
 
extern "C"
1730
 
LEX_STRING *session_make_lex_string(Session *session, LEX_STRING *lex_str,
1731
 
                                const char *str, unsigned int size,
1732
 
                                int allocate_lex_string)
1733
 
{
1734
 
  return session->make_lex_string(lex_str, str, size,
1735
 
                              (bool) allocate_lex_string);
1736
 
}
1737
 
 
1738
1765
const struct charset_info_st *session_charset(Session *session)
1739
1766
{
1740
1767
  return(session->charset());
1866
1893
*/
1867
1894
 
1868
1895
void Session::close_temporary_table(Table *table)
1869
 
                         
1870
1896
{
1871
1897
  if (table->prev)
1872
1898
  {
1908
1934
  rm_temporary_table(table_type, table->s->path.str);
1909
1935
 
1910
1936
  table->s->free_table_share();
 
1937
 
1911
1938
  /* This makes me sad, but we're allocating it via malloc */
1912
1939
  free(table);
1913
1940
}
2126
2153
 
2127
2154
  assert(base);
2128
2155
 
2129
 
  if (delete_table_proto_file(identifier.getPath()))
 
2156
  if (plugin::StorageEngine::deleteDefinitionFromPath(identifier))
2130
2157
    error= true;
2131
2158
 
2132
2159
  if (base->doDropTable(*this, identifier.getPath()))
2133
2160
  {
2134
2161
    error= true;
2135
2162
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
2136
 
                  identifier.getPath(), my_errno);
 
2163
                  identifier.getPath(), errno);
2137
2164
  }
2138
2165
  return error;
2139
2166
}
2151
2178
  {
2152
2179
    error= true;
2153
2180
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
2154
 
                  path, my_errno);
 
2181
                  path, errno);
2155
2182
  }
2156
2183
  return error;
2157
2184
}
2158
 
 
2159