~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.cc

Merge Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <drizzled/session.h>
26
26
#include "drizzled/session_list.h"
27
27
#include <sys/stat.h>
28
 
#include <mysys/mysys_err.h>
 
28
#include "drizzled/my_error.h"
29
29
#include <drizzled/error.h>
30
30
#include <drizzled/gettext.h>
31
31
#include <drizzled/query_id.h>
45
45
#include "drizzled/db.h"
46
46
#include "drizzled/pthread_globals.h"
47
47
 
 
48
#include "plugin/myisam/myisam.h"
 
49
#include "drizzled/internal/iocache.h"
48
50
 
49
51
#include <fcntl.h>
50
52
#include <algorithm>
645
647
  return false;
646
648
}
647
649
 
 
650
 
 
651
const char* Session::enter_cond(pthread_cond_t *cond,
 
652
                                pthread_mutex_t* mutex,
 
653
                                const char* msg)
 
654
{
 
655
  const char* old_msg = get_proc_info();
 
656
  safe_mutex_assert_owner(mutex);
 
657
  mysys_var->current_mutex = mutex;
 
658
  mysys_var->current_cond = cond;
 
659
  this->set_proc_info(msg);
 
660
  return old_msg;
 
661
}
 
662
 
 
663
void Session::exit_cond(const char* old_msg)
 
664
{
 
665
  /*
 
666
    Putting the mutex unlock in exit_cond() ensures that
 
667
    mysys_var->current_mutex is always unlocked _before_ mysys_var->mutex is
 
668
    locked (if that would not be the case, you'll get a deadlock if someone
 
669
    does a Session::awake() on you).
 
670
  */
 
671
  pthread_mutex_unlock(mysys_var->current_mutex);
 
672
  pthread_mutex_lock(&mysys_var->mutex);
 
673
  mysys_var->current_mutex = 0;
 
674
  mysys_var->current_cond = 0;
 
675
  this->set_proc_info(old_msg);
 
676
  pthread_mutex_unlock(&mysys_var->mutex);
 
677
}
 
678
 
648
679
bool Session::authenticate()
649
680
{
650
681
  lex_start(this);
1022
1053
  return (result->send_fields(field_list));
1023
1054
}
1024
1055
 
 
1056
void select_result::send_error(uint32_t errcode, const char *err)
 
1057
{
 
1058
  my_message(errcode, err, MYF(0));
 
1059
}
 
1060
 
1025
1061
/************************************************************************
1026
1062
  Handling writing to file
1027
1063
************************************************************************/
1031
1067
  my_message(errcode, err, MYF(0));
1032
1068
  if (file > 0)
1033
1069
  {
1034
 
    (void) end_io_cache(&cache);
 
1070
    (void) end_io_cache(cache);
1035
1071
    (void) my_close(file, MYF(0));
1036
1072
    (void) my_delete(path, MYF(0));             // Delete file on error
1037
1073
    file= -1;
1041
1077
 
1042
1078
bool select_to_file::send_eof()
1043
1079
{
1044
 
  int error= test(end_io_cache(&cache));
 
1080
  int error= test(end_io_cache(cache));
1045
1081
  if (my_close(file, MYF(MY_WME)))
1046
1082
    error= 1;
1047
1083
  if (!error)
1063
1099
  /* In case of error send_eof() may be not called: close the file here. */
1064
1100
  if (file >= 0)
1065
1101
  {
1066
 
    (void) end_io_cache(&cache);
 
1102
    (void) end_io_cache(cache);
1067
1103
    (void) my_close(file, MYF(0));
1068
1104
    file= -1;
1069
1105
  }
1071
1107
  row_count= 0;
1072
1108
}
1073
1109
 
 
1110
select_to_file::select_to_file(file_exchange *ex)
 
1111
  : exchange(ex),
 
1112
    file(-1),
 
1113
    cache(static_cast<IO_CACHE *>(sql_calloc(sizeof(IO_CACHE)))),
 
1114
    row_count(0L)
 
1115
{
 
1116
  path[0]=0;
 
1117
}
1074
1118
 
1075
1119
select_to_file::~select_to_file()
1076
1120
{
1077
 
  if (file >= 0)
1078
 
  {                                     // This only happens in case of error
1079
 
    (void) end_io_cache(&cache);
1080
 
    (void) my_close(file, MYF(0));
1081
 
    file= -1;
1082
 
  }
 
1121
  cleanup();
1083
1122
}
1084
1123
 
1085
1124
/***************************************************************************
1208
1247
    return 1;
1209
1248
  }
1210
1249
 
1211
 
  if ((file= create_file(session, path, exchange, &cache)) < 0)
 
1250
  if ((file= create_file(session, path, exchange, cache)) < 0)
1212
1251
    return 1;
1213
1252
 
1214
1253
  return 0;
1238
1277
  uint32_t used_length=0,items_left=items.elements;
1239
1278
  List_iterator_fast<Item> li(items);
1240
1279
 
1241
 
  if (my_b_write(&cache,(unsigned char*) exchange->line_start->ptr(),
 
1280
  if (my_b_write(cache,(unsigned char*) exchange->line_start->ptr(),
1242
1281
                 exchange->line_start->length()))
1243
1282
    goto err;
1244
1283
  while ((item=li++))
1249
1288
    res=item->str_result(&tmp);
1250
1289
    if (res && enclosed)
1251
1290
    {
1252
 
      if (my_b_write(&cache,(unsigned char*) exchange->enclosed->ptr(),
 
1291
      if (my_b_write(cache,(unsigned char*) exchange->enclosed->ptr(),
1253
1292
                     exchange->enclosed->length()))
1254
1293
        goto err;
1255
1294
    }
1261
1300
        {
1262
1301
          null_buff[0]=escape_char;
1263
1302
          null_buff[1]='N';
1264
 
          if (my_b_write(&cache,(unsigned char*) null_buff,2))
 
1303
          if (my_b_write(cache,(unsigned char*) null_buff,2))
1265
1304
            goto err;
1266
1305
        }
1267
 
        else if (my_b_write(&cache,(unsigned char*) "NULL",4))
 
1306
        else if (my_b_write(cache,(unsigned char*) "NULL",4))
1268
1307
          goto err;
1269
1308
      }
1270
1309
      else
1354
1393
                          is_ambiguous_field_sep) ?
1355
1394
              field_sep_char : escape_char;
1356
1395
            tmp_buff[1]= *pos ? *pos : '0';
1357
 
            if (my_b_write(&cache,(unsigned char*) start,(uint32_t) (pos-start)) ||
1358
 
                my_b_write(&cache,(unsigned char*) tmp_buff,2))
 
1396
            if (my_b_write(cache,(unsigned char*) start,(uint32_t) (pos-start)) ||
 
1397
                my_b_write(cache,(unsigned char*) tmp_buff,2))
1359
1398
              goto err;
1360
1399
            start=pos+1;
1361
1400
          }
1362
1401
        }
1363
 
        if (my_b_write(&cache,(unsigned char*) start,(uint32_t) (pos-start)))
 
1402
        if (my_b_write(cache,(unsigned char*) start,(uint32_t) (pos-start)))
1364
1403
          goto err;
1365
1404
      }
1366
 
      else if (my_b_write(&cache,(unsigned char*) res->ptr(),used_length))
 
1405
      else if (my_b_write(cache,(unsigned char*) res->ptr(),used_length))
1367
1406
        goto err;
1368
1407
    }
1369
1408
    if (fixed_row_size)
1379
1418
        uint32_t length=item->max_length-used_length;
1380
1419
        for (; length > sizeof(space) ; length-=sizeof(space))
1381
1420
        {
1382
 
          if (my_b_write(&cache,(unsigned char*) space,sizeof(space)))
 
1421
          if (my_b_write(cache,(unsigned char*) space,sizeof(space)))
1383
1422
            goto err;
1384
1423
        }
1385
 
        if (my_b_write(&cache,(unsigned char*) space,length))
 
1424
        if (my_b_write(cache,(unsigned char*) space,length))
1386
1425
          goto err;
1387
1426
      }
1388
1427
    }
1389
1428
    if (res && enclosed)
1390
1429
    {
1391
 
      if (my_b_write(&cache, (unsigned char*) exchange->enclosed->ptr(),
 
1430
      if (my_b_write(cache, (unsigned char*) exchange->enclosed->ptr(),
1392
1431
                     exchange->enclosed->length()))
1393
1432
        goto err;
1394
1433
    }
1395
1434
    if (--items_left)
1396
1435
    {
1397
 
      if (my_b_write(&cache, (unsigned char*) exchange->field_term->ptr(),
 
1436
      if (my_b_write(cache, (unsigned char*) exchange->field_term->ptr(),
1398
1437
                     field_term_length))
1399
1438
        goto err;
1400
1439
    }
1401
1440
  }
1402
 
  if (my_b_write(&cache,(unsigned char*) exchange->line_term->ptr(),
 
1441
  if (my_b_write(cache,(unsigned char*) exchange->line_term->ptr(),
1403
1442
                 exchange->line_term->length()))
1404
1443
    goto err;
1405
1444
  return(0);
1417
1456
select_dump::prepare(List<Item> &, Select_Lex_Unit *u)
1418
1457
{
1419
1458
  unit= u;
1420
 
  return (int) ((file= create_file(session, path, exchange, &cache)) < 0);
 
1459
  return (int) ((file= create_file(session, path, exchange, cache)) < 0);
1421
1460
}
1422
1461
 
1423
1462
 
1444
1483
    res=item->str_result(&tmp);
1445
1484
    if (!res)                                   // If NULL
1446
1485
    {
1447
 
      if (my_b_write(&cache,(unsigned char*) "",1))
 
1486
      if (my_b_write(cache,(unsigned char*) "",1))
1448
1487
        goto err;
1449
1488
    }
1450
 
    else if (my_b_write(&cache,(unsigned char*) res->ptr(),res->length()))
 
1489
    else if (my_b_write(cache,(unsigned char*) res->ptr(),res->length()))
1451
1490
    {
1452
 
      my_error(ER_ERROR_ON_WRITE, MYF(0), path, my_errno);
 
1491
      my_error(ER_ERROR_ON_WRITE, MYF(0), path, errno);
1453
1492
      goto err;
1454
1493
    }
1455
1494
  }
2135
2174
  {
2136
2175
    error= true;
2137
2176
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
2138
 
                  identifier.getPath(), my_errno);
 
2177
                  identifier.getPath(), errno);
2139
2178
  }
2140
2179
  return error;
2141
2180
}
2153
2192
  {
2154
2193
    error= true;
2155
2194
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
2156
 
                  path, my_errno);
 
2195
                  path, errno);
2157
2196
  }
2158
2197
  return error;
2159
2198
}