~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table/instance/base.cc

edit

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
*/
26
26
 
27
27
/* Basic functions needed by many modules */
28
 
#include <config.h>
 
28
#include "config.h"
29
29
 
30
30
#include <pthread.h>
31
31
#include <float.h>
37
37
 
38
38
#include <cassert>
39
39
 
40
 
#include <drizzled/error.h>
41
 
#include <drizzled/gettext.h>
42
 
#include <drizzled/sql_base.h>
43
 
#include <drizzled/pthread_globals.h>
44
 
#include <drizzled/internal/my_pthread.h>
45
 
 
46
 
#include <drizzled/table.h>
47
 
#include <drizzled/table/shell.h>
48
 
 
49
 
#include <drizzled/session.h>
50
 
 
51
 
#include <drizzled/charset.h>
52
 
#include <drizzled/internal/m_string.h>
53
 
#include <drizzled/internal/my_sys.h>
54
 
 
55
 
#include <drizzled/item/string.h>
56
 
#include <drizzled/item/int.h>
57
 
#include <drizzled/item/decimal.h>
58
 
#include <drizzled/item/float.h>
59
 
#include <drizzled/item/null.h>
60
 
#include <drizzled/temporal.h>
61
 
 
62
 
#include <drizzled/field.h>
63
 
#include <drizzled/field/str.h>
64
 
#include <drizzled/field/num.h>
65
 
#include <drizzled/field/blob.h>
66
 
#include <drizzled/field/boolean.h>
67
 
#include <drizzled/field/enum.h>
68
 
#include <drizzled/field/null.h>
69
 
#include <drizzled/field/date.h>
70
 
#include <drizzled/field/decimal.h>
71
 
#include <drizzled/field/real.h>
72
 
#include <drizzled/field/double.h>
73
 
#include <drizzled/field/int32.h>
74
 
#include <drizzled/field/int64.h>
75
 
#include <drizzled/field/size.h>
76
 
#include <drizzled/field/num.h>
77
 
#include <drizzled/field/time.h>
78
 
#include <drizzled/field/epoch.h>
79
 
#include <drizzled/field/datetime.h>
80
 
#include <drizzled/field/microtime.h>
81
 
#include <drizzled/field/varstring.h>
82
 
#include <drizzled/field/uuid.h>
83
 
 
84
 
#include <drizzled/plugin/storage_engine.h>
85
 
 
86
 
#include <drizzled/definition/cache.h>
87
 
#include <drizzled/typelib.h>
88
 
 
89
 
#include <drizzled/refresh_version.h>
 
40
#include "drizzled/error.h"
 
41
#include "drizzled/gettext.h"
 
42
#include "drizzled/sql_base.h"
 
43
#include "drizzled/pthread_globals.h"
 
44
#include "drizzled/internal/my_pthread.h"
 
45
#include "drizzled/plugin/event_observer.h"
 
46
 
 
47
#include "drizzled/table.h"
 
48
#include "drizzled/table/shell.h"
 
49
 
 
50
#include "drizzled/session.h"
 
51
 
 
52
#include "drizzled/charset.h"
 
53
#include "drizzled/internal/m_string.h"
 
54
#include "drizzled/internal/my_sys.h"
 
55
 
 
56
#include "drizzled/item/string.h"
 
57
#include "drizzled/item/int.h"
 
58
#include "drizzled/item/decimal.h"
 
59
#include "drizzled/item/float.h"
 
60
#include "drizzled/item/null.h"
 
61
#include "drizzled/temporal.h"
 
62
 
 
63
#include "drizzled/field.h"
 
64
#include "drizzled/field/str.h"
 
65
#include "drizzled/field/num.h"
 
66
#include "drizzled/field/blob.h"
 
67
#include "drizzled/field/boolean.h"
 
68
#include "drizzled/field/enum.h"
 
69
#include "drizzled/field/null.h"
 
70
#include "drizzled/field/date.h"
 
71
#include "drizzled/field/decimal.h"
 
72
#include "drizzled/field/real.h"
 
73
#include "drizzled/field/double.h"
 
74
#include "drizzled/field/int32.h"
 
75
#include "drizzled/field/int64.h"
 
76
#include "drizzled/field/size.h"
 
77
#include "drizzled/field/num.h"
 
78
#include "drizzled/field/time.h"
 
79
#include "drizzled/field/epoch.h"
 
80
#include "drizzled/field/datetime.h"
 
81
#include "drizzled/field/microtime.h"
 
82
#include "drizzled/field/varstring.h"
 
83
#include "drizzled/field/uuid.h"
 
84
 
 
85
#include "drizzled/definition/cache.h"
90
86
 
91
87
using namespace std;
92
88
 
222
218
 */
223
219
bool TableShare::fieldInPrimaryKey(Field *in_field) const
224
220
{
225
 
  assert(getTableMessage());
 
221
  assert(table_proto != NULL);
226
222
 
227
 
  size_t num_indexes= getTableMessage()->indexes_size();
 
223
  size_t num_indexes= table_proto->indexes_size();
228
224
 
229
225
  for (size_t x= 0; x < num_indexes; ++x)
230
226
  {
231
 
    const message::Table::Index &index= getTableMessage()->indexes(x);
 
227
    const message::Table::Index &index= table_proto->indexes(x);
232
228
    if (index.is_primary())
233
229
    {
234
230
      size_t num_parts= index.index_part_size();
249
245
  key_info(NULL),
250
246
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
251
247
  all_set(),
252
 
  db(NULL_LEX_STRING),
253
 
  table_name(NULL_LEX_STRING),
254
 
  path(NULL_LEX_STRING),
255
 
  normalized_path(NULL_LEX_STRING),
256
248
  block_size(0),
257
249
  version(0),
258
250
  timestamp_offset(0),
259
251
  reclength(0),
260
252
  stored_rec_length(0),
261
253
  max_rows(0),
262
 
  _table_message(NULL),
 
254
  table_proto(NULL),
263
255
  storage_engine(NULL),
264
256
  tmp_table(type_arg),
265
257
  _ref_count(0),
287
279
  error(0),
288
280
  open_errno(0),
289
281
  errarg(0),
290
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
282
  blob_ptr_size(0),
291
283
  db_low_byte_first(false),
292
284
  keys_in_use(0),
293
 
  keys_for_keyread(0)
 
285
  keys_for_keyread(0),
 
286
  event_observers(NULL)
294
287
{
 
288
 
 
289
  table_charset= 0;
 
290
  memset(&db, 0, sizeof(LEX_STRING));
 
291
  memset(&table_name, 0, sizeof(LEX_STRING));
 
292
  memset(&path, 0, sizeof(LEX_STRING));
 
293
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
294
 
295
295
  if (type_arg == message::Table::INTERNAL)
296
296
  {
297
297
    identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
309
309
  timestamp_field(NULL),
310
310
  key_info(NULL),
311
311
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
312
 
  table_charset(0),
313
312
  all_set(),
314
 
  db(NULL_LEX_STRING),
315
 
  table_name(NULL_LEX_STRING),
316
 
  path(NULL_LEX_STRING),
317
 
  normalized_path(NULL_LEX_STRING),
318
313
  block_size(0),
319
314
  version(0),
320
315
  timestamp_offset(0),
321
316
  reclength(0),
322
317
  stored_rec_length(0),
323
318
  max_rows(0),
324
 
  _table_message(NULL),
 
319
  table_proto(NULL),
325
320
  storage_engine(NULL),
326
321
  tmp_table(message::Table::INTERNAL),
327
322
  _ref_count(0),
349
344
  error(0),
350
345
  open_errno(0),
351
346
  errarg(0),
352
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
347
  blob_ptr_size(0),
353
348
  db_low_byte_first(false),
354
349
  keys_in_use(0),
355
 
  keys_for_keyread(0)
 
350
  keys_for_keyread(0),
 
351
  event_observers(NULL)
356
352
{
357
353
  assert(identifier.getKey() == key);
358
354
 
 
355
  table_charset= 0;
 
356
  memset(&path, 0, sizeof(LEX_STRING));
 
357
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
358
 
359
359
  private_key_for_cache= key;
360
360
 
361
361
  table_category=         TABLE_CATEGORY_TEMPORARY;
384
384
  timestamp_field(NULL),
385
385
  key_info(NULL),
386
386
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
387
 
  table_charset(0),
388
387
  all_set(),
389
 
  db(NULL_LEX_STRING),
390
 
  table_name(NULL_LEX_STRING),
391
 
  path(NULL_LEX_STRING),
392
 
  normalized_path(NULL_LEX_STRING),
393
388
  block_size(0),
394
389
  version(0),
395
390
  timestamp_offset(0),
396
391
  reclength(0),
397
392
  stored_rec_length(0),
398
393
  max_rows(0),
399
 
  _table_message(NULL),
 
394
  table_proto(NULL),
400
395
  storage_engine(NULL),
401
396
  tmp_table(identifier.getType()),
402
397
  _ref_count(0),
424
419
  error(0),
425
420
  open_errno(0),
426
421
  errarg(0),
427
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
422
  blob_ptr_size(0),
428
423
  db_low_byte_first(false),
429
424
  keys_in_use(0),
430
 
  keys_for_keyread(0)
 
425
  keys_for_keyread(0),
 
426
  event_observers(NULL)
431
427
{
 
428
  table_charset= 0;
 
429
  memset(&db, 0, sizeof(LEX_STRING));
 
430
  memset(&table_name, 0, sizeof(LEX_STRING));
 
431
  memset(&path, 0, sizeof(LEX_STRING));
 
432
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
433
 
432
434
  private_key_for_cache= identifier.getKey();
433
435
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
434
436
  private_normalized_path.resize(identifier.getPath().size() + 1);
460
462
  timestamp_field(NULL),
461
463
  key_info(NULL),
462
464
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
463
 
  table_charset(0),
464
465
  all_set(),
465
 
  db(NULL_LEX_STRING),
466
 
  table_name(NULL_LEX_STRING),
467
 
  path(NULL_LEX_STRING),
468
 
  normalized_path(NULL_LEX_STRING),
469
466
  block_size(0),
470
467
  version(0),
471
468
  timestamp_offset(0),
472
469
  reclength(0),
473
470
  stored_rec_length(0),
474
471
  max_rows(0),
475
 
  _table_message(NULL),
 
472
  table_proto(NULL),
476
473
  storage_engine(NULL),
477
474
  tmp_table(type_arg),
478
475
  _ref_count(0),
500
497
  error(0),
501
498
  open_errno(0),
502
499
  errarg(0),
503
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
500
  blob_ptr_size(0),
504
501
  db_low_byte_first(false),
505
502
  keys_in_use(0),
506
 
  keys_for_keyread(0)
 
503
  keys_for_keyread(0),
 
504
  event_observers(NULL)
507
505
{
 
506
  table_charset= 0;
 
507
  memset(&db, 0, sizeof(LEX_STRING));
 
508
  memset(&table_name, 0, sizeof(LEX_STRING));
 
509
  memset(&path, 0, sizeof(LEX_STRING));
 
510
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
511
 
508
512
  char *path_buff;
509
513
  std::string _path;
510
514
 
561
565
{
562
566
  storage_engine= NULL;
563
567
 
 
568
  delete table_proto;
 
569
  table_proto= NULL;
 
570
 
 
571
  plugin::EventObserver::deregisterTableEvents(*this);
 
572
 
564
573
  mem_root.free_root(MYF(0));                 // Free's share
565
574
}
566
575
 
577
586
  table_name.str=    db.str + db.length + 1;
578
587
  table_name.length= strlen(table_name.str);
579
588
 
580
 
  getTableMessage()->set_name(identifier_arg.getTableName());
581
 
  getTableMessage()->set_schema(identifier_arg.getSchemaName());
 
589
  table_proto->set_name(identifier_arg.getTableName());
 
590
  table_proto->set_schema(identifier_arg.getSchemaName());
582
591
}
583
592
 
584
 
bool TableShare::parse_table_proto(Session& session, message::Table &table)
 
593
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
585
594
{
586
 
  drizzled::error_t local_error= EE_OK;
 
595
  int local_error= 0;
587
596
 
588
597
  if (! table.IsInitialized())
589
598
  {
594
603
    return ER_CORRUPT_TABLE_DEFINITION;
595
604
  }
596
605
 
597
 
  setTableMessage(table);
 
606
  setTableProto(new(nothrow) message::Table(table));
598
607
 
599
608
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
600
609
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
620
629
 
621
630
  table_charset= get_charset(table_options.collation_id());
622
631
 
623
 
  if (not table_charset)
 
632
  if (! table_charset)
624
633
  {
625
634
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
626
635
             table_options.collation().c_str(),
631
640
 
632
641
  db_record_offset= 1;
633
642
 
 
643
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
 
644
 
634
645
  keys= table.indexes_size();
635
646
 
636
647
  key_parts= 0;
1062
1073
      {
1063
1074
        if (fo.scale() > DECIMAL_MAX_SCALE)
1064
1075
        {
1065
 
          local_error= ER_NOT_FORM_FILE;
 
1076
          local_error= 4;
1066
1077
 
1067
 
          return true;
 
1078
          return local_error;
1068
1079
        }
1069
1080
        decimals= static_cast<uint8_t>(fo.scale());
1070
1081
      }
1084
1095
    }
1085
1096
 
1086
1097
 
 
1098
    blob_ptr_size= portable_sizeof_char_ptr;
 
1099
 
1087
1100
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1088
1101
 
1089
1102
    // We set field_length in this loop.
1118
1131
            decimals != NOT_FIXED_DEC)
1119
1132
        {
1120
1133
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1121
 
          local_error= ER_M_BIGGER_THAN_D;
1122
 
          return true;
 
1134
          local_error= 1;
 
1135
 
 
1136
          return local_error;
1123
1137
        }
1124
1138
        break;
1125
1139
      }
1209
1223
                         charset,
1210
1224
                         MTYP_TYPENR(unireg_type),
1211
1225
                         ((field_type == DRIZZLE_TYPE_ENUM) ?  &intervals[interval_nr++] : (TYPELIB*) 0),
1212
 
                         getTableMessage()->field(fieldnr).name().c_str());
 
1226
                         getTableProto()->field(fieldnr).name().c_str());
1213
1227
 
1214
1228
    _fields[fieldnr]= f;
1215
1229
 
1258
1272
      if (res != 0 && res != 3) /* @TODO Huh? */
1259
1273
      {
1260
1274
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1261
 
        local_error= ER_INVALID_DEFAULT;
 
1275
        local_error= 1;
1262
1276
 
1263
 
        return true;
 
1277
        return local_error;
1264
1278
      }
1265
1279
    }
1266
1280
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
1472
1486
                            &next_number_keypart)) < 0)
1473
1487
    {
1474
1488
      /* Wrong field definition */
1475
 
      local_error= ER_NOT_FORM_FILE;
 
1489
      local_error= 4;
1476
1490
 
1477
 
      return true;
 
1491
      return local_error;
1478
1492
    }
1479
1493
    else
1480
1494
    {
1499
1513
  all_set.resize(_field_size);
1500
1514
  all_set.set();
1501
1515
 
1502
 
  return local_error != EE_OK;
1503
 
}
 
1516
  return local_error;
 
1517
}
 
1518
 
 
1519
int TableShare::parse_table_proto(Session& session, message::Table &table)
 
1520
{
 
1521
  int local_error= inner_parse_table_proto(session, table);
 
1522
 
 
1523
  if (not local_error)
 
1524
    return 0;
 
1525
 
 
1526
  error= local_error;
 
1527
  open_errno= errno;
 
1528
  errarg= 0;
 
1529
  open_table_error(local_error, open_errno, 0);
 
1530
 
 
1531
  return local_error;
 
1532
}
 
1533
 
1504
1534
 
1505
1535
/*
1506
1536
  Read table definition from a binary / text based .frm cursor
1528
1558
 
1529
1559
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1530
1560
{
1531
 
  drizzled::error_t local_error= EE_OK;
1532
 
 
1533
 
  message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
1534
 
 
1535
 
  if (table and table->IsInitialized())
1536
 
  {
1537
 
    if (parse_table_proto(session, *table))
1538
 
    {
1539
 
      local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1540
 
      my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1541
 
    }
1542
 
    else
1543
 
    {
1544
 
      setTableCategory(TABLE_CATEGORY_USER);
1545
 
      local_error= EE_OK;
1546
 
    }
1547
 
  }
1548
 
  else if (table and not table->IsInitialized())
1549
 
  {
1550
 
    local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1551
 
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1552
 
  }
1553
 
  else
1554
 
  {
1555
 
    local_error= ER_TABLE_UNKNOWN;
1556
 
    my_error(ER_TABLE_UNKNOWN, identifier);
1557
 
  }
1558
 
 
1559
 
  return static_cast<int>(local_error);
 
1561
  int local_error;
 
1562
  bool error_given;
 
1563
 
 
1564
  local_error= 1;
 
1565
  error_given= 0;
 
1566
 
 
1567
  message::table::shared_ptr table;
 
1568
 
 
1569
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
 
1570
 
 
1571
  do {
 
1572
    if (local_error != EEXIST)
 
1573
    {
 
1574
      if (local_error > 0)
 
1575
      {
 
1576
        errno= local_error;
 
1577
        local_error= 1;
 
1578
      }
 
1579
      else
 
1580
      {
 
1581
        if (not table->IsInitialized())
 
1582
        {
 
1583
          local_error= 4;
 
1584
        }
 
1585
      }
 
1586
      break;
 
1587
    }
 
1588
 
 
1589
    local_error= parse_table_proto(session, *table);
 
1590
 
 
1591
    setTableCategory(TABLE_CATEGORY_USER);
 
1592
  } while (0);
 
1593
 
 
1594
  if (local_error && !error_given)
 
1595
  {
 
1596
    error= local_error;
 
1597
    open_table_error(error, (open_errno= errno), 0);
 
1598
  }
 
1599
 
 
1600
  return(error);
1560
1601
}
1561
1602
 
1562
1603
 
1620
1661
  unsigned char *record= NULL;
1621
1662
  Field **field_ptr;
1622
1663
 
 
1664
  /* Parsing of partitioning information from .frm needs session->lex set up. */
 
1665
  assert(session->lex->is_lex_started);
 
1666
 
1623
1667
  local_error= 1;
1624
1668
  outparam.resetTable(session, this, db_stat);
1625
1669
 
1810
1854
  case 1:
1811
1855
    if (db_errno == ENOENT)
1812
1856
    {
1813
 
      identifier::Table identifier(db.str, table_name.str);
1814
 
      my_error(ER_TABLE_UNKNOWN, identifier);
 
1857
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
1815
1858
    }
1816
1859
    else
1817
1860
    {
1914
1957
    null_bit= ((unsigned char) 1) << null_bit;
1915
1958
  }
1916
1959
 
 
1960
  switch (field_type) 
 
1961
  {
 
1962
  case DRIZZLE_TYPE_DATE:
 
1963
  case DRIZZLE_TYPE_DATETIME:
 
1964
  case DRIZZLE_TYPE_UUID:
 
1965
    field_charset= &my_charset_bin;
 
1966
  default: break;
 
1967
  }
 
1968
 
1917
1969
  switch (field_type)
1918
1970
  {
1919
1971
  case DRIZZLE_TYPE_ENUM:
1933
1985
                                      field_charset);
1934
1986
  case DRIZZLE_TYPE_BLOB:
1935
1987
    return new (&mem_root) Field_blob(ptr,
1936
 
                                      null_pos,
1937
 
                                      null_bit,
1938
 
                                      field_name,
1939
 
                                      this,
1940
 
                                      field_charset);
 
1988
                                 null_pos,
 
1989
                                 null_bit,
 
1990
                                 field_name,
 
1991
                                 this,
 
1992
                                 field_charset);
1941
1993
  case DRIZZLE_TYPE_DECIMAL:
1942
1994
    return new (&mem_root) Field_decimal(ptr,
1943
 
                                         field_length,
1944
 
                                         null_pos,
1945
 
                                         null_bit,
1946
 
                                         unireg_check,
1947
 
                                         field_name,
1948
 
                                         decimals);
 
1995
                                    field_length,
 
1996
                                    null_pos,
 
1997
                                    null_bit,
 
1998
                                    unireg_check,
 
1999
                                    field_name,
 
2000
                                    decimals,
 
2001
                                    false,
 
2002
                                    false /* is_unsigned */);
1949
2003
  case DRIZZLE_TYPE_DOUBLE:
1950
2004
    return new (&mem_root) Field_double(ptr,
1951
2005
                                   field_length,
2014
2068
                                       field_length,
2015
2069
                                       null_pos,
2016
2070
                                       null_bit,
2017
 
                                       field_name);
 
2071
                                       field_name,
 
2072
                                       field_charset);
2018
2073
  case DRIZZLE_TYPE_DATE:
2019
2074
    return new (&mem_root) Field_date(ptr,
2020
2075
                                 null_pos,
2021
2076
                                 null_bit,
2022
 
                                 field_name);
 
2077
                                 field_name,
 
2078
                                 field_charset);
2023
2079
  case DRIZZLE_TYPE_DATETIME:
2024
2080
    return new (&mem_root) Field_datetime(ptr,
2025
2081
                                     null_pos,
2026
2082
                                     null_bit,
2027
 
                                     field_name);
 
2083
                                     field_name,
 
2084
                                     field_charset);
2028
2085
  case DRIZZLE_TYPE_NULL:
2029
2086
    return new (&mem_root) Field_null(ptr,
2030
 
                                      field_length,
2031
 
                                      field_name);
 
2087
                                 field_length,
 
2088
                                 field_name,
 
2089
                                 field_charset);
2032
2090
  }
2033
2091
  assert(0);
2034
2092
  abort();
2035
2093
}
2036
2094
 
2037
 
void TableShare::refreshVersion()
2038
 
{
2039
 
  version= refresh_version;
2040
 
}
2041
 
 
2042
2095
 
2043
2096
} /* namespace drizzled */