~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.cc

Remove PLUGIN and MODULES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
/* Some general useful functions */
18
18
 
19
 
#include <drizzled/server_includes.h>
 
19
#include "config.h"
 
20
 
 
21
#include <float.h>
 
22
#include <fcntl.h>
 
23
 
 
24
#include <string>
 
25
#include <vector>
 
26
#include <algorithm>
 
27
 
20
28
#include <drizzled/error.h>
21
29
#include <drizzled/gettext.h>
22
30
 
 
31
#include "drizzled/plugin/info_schema_table.h"
23
32
#include <drizzled/nested_join.h>
24
33
#include <drizzled/sql_parse.h>
25
34
#include <drizzled/item/sum.h>
32
41
#include <drizzled/field/double.h>
33
42
#include <drizzled/unireg.h>
34
43
#include <drizzled/message/table.pb.h>
 
44
#include "drizzled/sql_table.h"
 
45
#include "drizzled/charset.h"
 
46
#include "drizzled/internal/m_string.h"
 
47
#include "plugin/myisam/myisam.h"
35
48
 
36
49
#include <drizzled/item/string.h>
37
50
#include <drizzled/item/int.h>
42
55
 
43
56
#include "drizzled/table_proto.h"
44
57
 
45
 
#include <string>
46
 
#include <vector>
47
 
#include <algorithm>
48
 
 
49
 
using namespace drizzled;
50
58
using namespace std;
51
59
using namespace drizzled;
52
60
 
 
61
extern pid_t current_pid;
 
62
extern plugin::StorageEngine *heap_engine;
 
63
extern plugin::StorageEngine *myisam_engine;
 
64
 
53
65
/* Functions defined in this cursor */
54
66
 
55
67
void open_table_error(TableShare *share, int error, int db_errno,
98
110
TableShare *alloc_table_share(TableList *table_list, char *key,
99
111
                               uint32_t key_length)
100
112
{
101
 
  MEM_ROOT mem_root;
 
113
  memory::Root mem_root;
102
114
  TableShare *share;
103
115
  char *key_buff, *path_buff;
104
116
  char path[FN_REFLEN];
107
119
  path_length= build_table_filename(path, sizeof(path) - 1,
108
120
                                    table_list->db,
109
121
                                    table_list->table_name, false);
110
 
  init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
122
  memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
111
123
  if (multi_alloc_root(&mem_root,
112
124
                       &share, sizeof(*share),
113
125
                       &key_buff, key_length,
247
259
                                TableShare *share)
248
260
{
249
261
  int error= 0;
250
 
  Cursor *handler_file= NULL;
251
262
 
252
263
  share->setTableProto(new(nothrow) message::Table(table));
253
264
 
306
317
 
307
318
  share->blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
308
319
 
309
 
  share->db_low_byte_first= true;
310
 
 
311
320
  share->keys= table.indexes_size();
312
321
 
313
322
  share->key_parts= 0;
390
399
    case message::Table::Index::BTREE:
391
400
      keyinfo->algorithm= HA_KEY_ALG_BTREE;
392
401
      break;
393
 
    case message::Table::Index::RTREE:
394
 
      keyinfo->algorithm= HA_KEY_ALG_RTREE;
395
 
      break;
396
402
    case message::Table::Index::HASH:
397
403
      keyinfo->algorithm= HA_KEY_ALG_HASH;
398
404
      break;
399
 
    case message::Table::Index::FULLTEXT:
400
 
      keyinfo->algorithm= HA_KEY_ALG_FULLTEXT;
401
405
 
402
406
    default:
403
407
      /* TODO: suitable warning ? */
818
822
    memset(&temp_table, 0, sizeof(temp_table));
819
823
    temp_table.s= share;
820
824
    temp_table.in_use= &session;
821
 
    temp_table.s->db_low_byte_first= 1; //Cursor->low_byte_first();
 
825
    temp_table.s->db_low_byte_first= true; //Cursor->low_byte_first();
822
826
    temp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
823
827
 
824
828
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1015
1019
  free(field_pack_length);
1016
1020
  field_pack_length= NULL;
1017
1021
 
1018
 
  if (! (handler_file= share->db_type()->getCursor(*share, session.mem_root)))
1019
 
    abort(); // FIXME
1020
 
 
1021
1022
  /* Fix key stuff */
1022
1023
  if (share->key_parts)
1023
1024
  {
1090
1091
        if (field->key_length() == key_part->length &&
1091
1092
            !(field->flags & BLOB_FLAG))
1092
1093
        {
1093
 
          if (handler_file->index_flags(key, i, 0) & HA_KEYREAD_ONLY)
 
1094
          enum ha_key_alg algo= share->key_info[key].algorithm;
 
1095
          if (share->db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1094
1096
          {
1095
1097
            share->keys_for_keyread.set(key);
1096
1098
            field->part_of_key.set(key);
1097
1099
            field->part_of_key_not_clustered.set(key);
1098
1100
          }
1099
 
          if (handler_file->index_flags(key, i, 1) & HA_READ_ORDER)
 
1101
          if (share->db_type()->index_flags(algo) & HA_READ_ORDER)
1100
1102
            field->part_of_sortkey.set(key);
1101
1103
        }
1102
1104
        if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1127
1129
      set_if_bigger(share->max_key_length,keyinfo->key_length+
1128
1130
                    keyinfo->key_parts);
1129
1131
      share->total_key_length+= keyinfo->key_length;
1130
 
      /*
1131
 
        MERGE tables do not have unique indexes. But every key could be
1132
 
        an unique index on the underlying MyISAM table. (Bug #10400)
1133
 
      */
1134
 
      if ((keyinfo->flags & HA_NOSAME) ||
1135
 
          (handler_file->getEngine()->check_flag(HTON_BIT_ANY_INDEX_MAY_BE_UNIQUE)))
 
1132
 
 
1133
      if (keyinfo->flags & HA_NOSAME)
 
1134
      {
1136
1135
        set_if_bigger(share->max_unique_length,keyinfo->key_length);
 
1136
      }
1137
1137
    }
1138
1138
    if (primary_key < MAX_KEY &&
1139
1139
        (share->keys_in_use.test(primary_key)))
1194
1194
    }
1195
1195
  }
1196
1196
 
1197
 
  share->db_low_byte_first= handler_file->low_byte_first();
 
1197
  share->db_low_byte_first= true; // @todo Question this.
1198
1198
  share->column_bitmap_size= bitmap_buffer_size(share->fields);
1199
1199
 
1200
1200
  my_bitmap_map *bitmaps;
1205
1205
  share->all_set.init(bitmaps, share->fields);
1206
1206
  share->all_set.setAll();
1207
1207
 
1208
 
  if (handler_file)
1209
 
    delete handler_file;
1210
1208
  return (0);
1211
1209
 
1212
1210
err:
1216
1214
    free(field_pack_length);
1217
1215
 
1218
1216
  share->error= error;
1219
 
  share->open_errno= my_errno;
 
1217
  share->open_errno= errno;
1220
1218
  share->errarg= 0;
1221
1219
  hash_free(&share->name_hash);
1222
 
  if (handler_file)
1223
 
    delete handler_file;
1224
1220
  share->open_table_error(error, share->open_errno, 0);
1225
1221
 
1226
1222
  return error;
1270
1266
  {
1271
1267
    if (error>0)
1272
1268
    {
1273
 
      my_errno= error;
 
1269
      errno= error;
1274
1270
      error= 1;
1275
1271
    }
1276
1272
    else
1294
1290
  if (error && !error_given)
1295
1291
  {
1296
1292
    share->error= error;
1297
 
    share->open_table_error(error, (share->open_errno= my_errno), 0);
 
1293
    share->open_table_error(error, (share->open_errno= errno), 0);
1298
1294
  }
1299
1295
 
1300
1296
  return(error);
1312
1308
    db_stat             open flags (for example HA_OPEN_KEYFILE|
1313
1309
                        HA_OPEN_RNDFILE..) can be 0 (example in
1314
1310
                        ha_example_table)
1315
 
    prgflag             READ_ALL etc..
1316
1311
    ha_open_flags       HA_OPEN_ABORT_IF_LOCKED etc..
1317
1312
    outparam            result table
1318
1313
 
1327
1322
*/
1328
1323
 
1329
1324
int open_table_from_share(Session *session, TableShare *share, const char *alias,
1330
 
                          uint32_t db_stat, uint32_t prgflag, uint32_t ha_open_flags,
 
1325
                          uint32_t db_stat, uint32_t ha_open_flags,
1331
1326
                          Table *outparam)
1332
1327
{
1333
1328
  int error;
1347
1342
    goto err;
1348
1343
 
1349
1344
  /* Allocate Cursor */
1350
 
  if (!(prgflag & OPEN_FRM_FILE_ONLY))
1351
 
  {
1352
 
    if (!(outparam->cursor= share->db_type()->getCursor(*share, &outparam->mem_root)))
1353
 
      goto err;
1354
 
  }
1355
 
  else
1356
 
  {
1357
 
    assert(!db_stat);
1358
 
  }
 
1345
  if (!(outparam->cursor= share->db_type()->getCursor(*share, &outparam->mem_root)))
 
1346
    goto err;
1359
1347
 
1360
1348
  error= 4;
1361
1349
  records= 0;
1362
 
  if ((db_stat & HA_OPEN_KEYFILE) || (prgflag & DELAYED_OPEN))
 
1350
  if ((db_stat & HA_OPEN_KEYFILE))
1363
1351
    records=1;
1364
 
  if (prgflag & (READ_ALL+EXTRA_RECORD))
1365
 
    records++;
 
1352
 
 
1353
  records++;
1366
1354
 
1367
1355
  if (!(record= (unsigned char*) alloc_root(&outparam->mem_root,
1368
1356
                                   share->rec_buff_length * records)))
1501
1489
            as if the .frm cursor didn't exist
1502
1490
          */
1503
1491
          error= 1;
1504
 
          my_errno= ENOENT;
 
1492
          errno= ENOENT;
1505
1493
          break;
1506
1494
        case EMFILE:
1507
1495
          /*
1509
1497
            cursor can't open
1510
1498
           */
1511
1499
          error= 1;
1512
 
          my_errno= EMFILE;
 
1500
          errno= EMFILE;
1513
1501
          break;
1514
1502
        default:
1515
1503
          outparam->print_error(ha_err, MYF(0));
1531
1519
  return (0);
1532
1520
 
1533
1521
 err:
1534
 
  if (!error_reported && !(prgflag & DONT_GIVE_ERROR))
1535
 
    share->open_table_error(error, my_errno, 0);
 
1522
  if (!error_reported)
 
1523
    share->open_table_error(error, errno, 0);
1536
1524
  delete outparam->cursor;
1537
1525
  outparam->cursor= 0;                          // For easier error checking
1538
1526
  outparam->db_stat= 0;
1585
1573
}
1586
1574
 
1587
1575
 
 
1576
void Table::resetTable(Session *session,
 
1577
                       TableShare *share,
 
1578
                       uint32_t db_stat_arg)
 
1579
{
 
1580
  s= share;
 
1581
  field= NULL;
 
1582
 
 
1583
  cursor= NULL;
 
1584
  next= NULL;
 
1585
  prev= NULL;
 
1586
 
 
1587
  read_set= NULL;
 
1588
  write_set= NULL;
 
1589
 
 
1590
  tablenr= 0;
 
1591
  db_stat= db_stat_arg;
 
1592
 
 
1593
  in_use= session;
 
1594
  record[0]= (unsigned char *) NULL;
 
1595
  record[1]= (unsigned char *) NULL;
 
1596
 
 
1597
  insert_values= NULL;
 
1598
  key_info= NULL;
 
1599
  next_number_field= NULL;
 
1600
  found_next_number_field= NULL;
 
1601
  timestamp_field= NULL;
 
1602
 
 
1603
  pos_in_table_list= NULL;
 
1604
  group= NULL;
 
1605
  alias= NULL;
 
1606
  null_flags= NULL;
 
1607
 
 
1608
  lock_position= 0;
 
1609
  lock_data_start= 0;
 
1610
  lock_count= 0;
 
1611
  used_fields= 0;
 
1612
  status= 0;
 
1613
  derived_select_number= 0;
 
1614
  current_lock= F_UNLCK;
 
1615
  copy_blobs= false;
 
1616
 
 
1617
  maybe_null= false;
 
1618
 
 
1619
  null_row= false;
 
1620
 
 
1621
  force_index= false;
 
1622
  distinct= false;
 
1623
  const_table= false;
 
1624
  no_rows= false;
 
1625
  key_read= false;
 
1626
  no_keyread= false;
 
1627
 
 
1628
  open_placeholder= false;
 
1629
  locked_by_name= false;
 
1630
  no_cache= false;
 
1631
 
 
1632
  auto_increment_field_not_null= false;
 
1633
  alias_name_used= false;
 
1634
 
 
1635
  query_id= 0;
 
1636
  quick_condition_rows= 0;
 
1637
 
 
1638
  timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
 
1639
  map= 0;
 
1640
 
 
1641
  reginfo.reset();
 
1642
 
 
1643
  covering_keys.reset();
 
1644
 
 
1645
  quick_keys.reset();
 
1646
  merge_keys.reset();
 
1647
 
 
1648
  keys_in_use_for_query.reset();
 
1649
  keys_in_use_for_group_by.reset();
 
1650
  keys_in_use_for_order_by.reset();
 
1651
 
 
1652
  memset(quick_rows, 0, sizeof(ha_rows) * MAX_KEY);
 
1653
  memset(const_key_parts, 0, sizeof(ha_rows) * MAX_KEY);
 
1654
 
 
1655
  memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
 
1656
  memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
 
1657
 
 
1658
  memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
1659
  memset(&sort, 0, sizeof(filesort_info_st));
 
1660
}
 
1661
 
 
1662
 
 
1663
 
1588
1664
/* Deallocate temporary blob storage */
1589
1665
 
1590
1666
void free_blobs(register Table *table)
1670
1746
} /* open_table_error */
1671
1747
 
1672
1748
 
1673
 
TYPELIB *typelib(MEM_ROOT *mem_root, List<String> &strings)
 
1749
TYPELIB *typelib(memory::Root *mem_root, List<String> &strings)
1674
1750
{
1675
1751
  TYPELIB *result= (TYPELIB*) alloc_root(mem_root, sizeof(TYPELIB));
1676
1752
  if (!result)
1875
1951
  return check_identifier_name(org_name);
1876
1952
}
1877
1953
 
1878
 
 
1879
1954
/*
1880
1955
  Allow anything as a table name, as long as it doesn't contain an
1881
1956
  ' ' at the end
2293
2368
                 uint64_t select_options, ha_rows rows_limit,
2294
2369
                 const char *table_alias)
2295
2370
{
2296
 
  MEM_ROOT *mem_root_save, own_root;
 
2371
  memory::Root *mem_root_save, own_root;
2297
2372
  Table *table;
2298
2373
  TableShare *share;
2299
2374
  uint  i,field_count,null_count,null_pack_length;
2367
2442
  if (param->precomputed_group_by)
2368
2443
    copy_func_count+= param->sum_func_count;
2369
2444
 
2370
 
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
2445
  memory::init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
2371
2446
 
2372
2447
  if (!multi_alloc_root(&own_root,
2373
2448
                        &table, sizeof(*table),
2900
2975
      key_part_info->length=   (uint16_t) (*reg_field)->pack_length();
2901
2976
      /* TODO:
2902
2977
        The below method of computing the key format length of the
2903
 
        key part is a copy/paste from opt_range.cc, and table.cc.
 
2978
        key part is a copy/paste from optimizer/range.cc, and table.cc.
2904
2979
        This should be factored out, e.g. as a method of Field.
2905
2980
        In addition it is not clear if any of the Field::*_length
2906
2981
        methods is supposed to compute the same length. If so, it
3232
3307
 
3233
3308
void Table::free_tmp_table(Session *session)
3234
3309
{
3235
 
  MEM_ROOT own_root= mem_root;
 
3310
  memory::Root own_root= mem_root;
3236
3311
  const char *save_proc_info;
3237
3312
 
3238
3313
  save_proc_info=session->get_proc_info();
3493
3568
  memset(null_flags, 255, s->null_bytes);
3494
3569
}
3495
3570
 
 
3571
Table::Table()
 
3572
  : s(NULL),
 
3573
    field(NULL),
 
3574
    cursor(NULL),
 
3575
    next(NULL),
 
3576
    prev(NULL),
 
3577
    read_set(NULL),
 
3578
    write_set(NULL),
 
3579
    tablenr(0),
 
3580
    db_stat(0),
 
3581
    in_use(NULL),
 
3582
    insert_values(NULL),
 
3583
    key_info(NULL),
 
3584
    next_number_field(NULL),
 
3585
    found_next_number_field(NULL),
 
3586
    timestamp_field(NULL),
 
3587
    pos_in_table_list(NULL),
 
3588
    group(NULL),
 
3589
    alias(NULL),
 
3590
    null_flags(NULL),
 
3591
    lock_position(0),
 
3592
    lock_data_start(0),
 
3593
    lock_count(0),
 
3594
    used_fields(0),
 
3595
    status(0),
 
3596
    derived_select_number(0),
 
3597
    current_lock(F_UNLCK),
 
3598
    copy_blobs(false),
 
3599
    maybe_null(false),
 
3600
    null_row(false),
 
3601
    force_index(false),
 
3602
    distinct(false),
 
3603
    const_table(false),
 
3604
    no_rows(false),
 
3605
    key_read(false),
 
3606
    no_keyread(false),
 
3607
    open_placeholder(false),
 
3608
    locked_by_name(false),
 
3609
    no_cache(false),
 
3610
    auto_increment_field_not_null(false),
 
3611
    alias_name_used(false),
 
3612
    query_id(0),
 
3613
    quick_condition_rows(0),
 
3614
    timestamp_field_type(TIMESTAMP_NO_AUTO_SET),
 
3615
    map(0)
 
3616
{
 
3617
  record[0]= (unsigned char *) 0;
 
3618
  record[1]= (unsigned char *) 0;
 
3619
 
 
3620
  covering_keys.reset();
 
3621
 
 
3622
  quick_keys.reset();
 
3623
  merge_keys.reset();
 
3624
 
 
3625
  keys_in_use_for_query.reset();
 
3626
  keys_in_use_for_group_by.reset();
 
3627
  keys_in_use_for_order_by.reset();
 
3628
 
 
3629
  memset(quick_rows, 0, sizeof(ha_rows) * MAX_KEY);
 
3630
  memset(const_key_parts, 0, sizeof(ha_rows) * MAX_KEY);
 
3631
 
 
3632
  memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
 
3633
  memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
 
3634
 
 
3635
  memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
3636
  memset(&sort, 0, sizeof(filesort_info_st));
 
3637
}
 
3638
 
3496
3639
/*****************************************************************************
3497
3640
  The different ways to read a record
3498
3641
  Returns -1 if row was not found, 0 if row was found and 1 on errors
3594
3737
  return false;
3595
3738
}
3596
3739
 
3597
 
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
3598
 
template class List<String>;
3599
 
template class List_iterator<String>;
3600
 
#endif