~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Olaf van der Spek
  • Date: 2011-03-23 10:31:37 UTC
  • mto: (2247.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2248.
  • Revision ID: olafvdspek@gmail.com-20110323103137-lwevis2tfchgu18u
Propogate return void

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
 
#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/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/num.h"
76
 
#include "drizzled/field/timestamp.h"
77
 
#include "drizzled/field/datetime.h"
78
 
#include "drizzled/field/varstring.h"
79
 
#include "drizzled/field/uuid.h"
80
 
 
81
 
#include "drizzled/definition/cache.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
 
 
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
#include <drizzled/plugin/storage_engine.h>
 
84
#include <drizzled/definition/cache.h>
 
85
#include <drizzled/typelib.h>
 
86
#include <drizzled/refresh_version.h>
 
87
#include <drizzled/key.h>
82
88
 
83
89
using namespace std;
84
90
 
87
93
 
88
94
extern size_t table_def_size;
89
95
 
90
 
/*****************************************************************************
91
 
  Functions to handle table definition cach (TableShare)
92
 
 *****************************************************************************/
93
 
 
94
 
/*
95
 
  Mark that we are not using table share anymore.
96
 
 
97
 
  SYNOPSIS
98
 
  release()
99
 
  share         Table share
100
 
 
101
 
  IMPLEMENTATION
102
 
  If ref_count goes to zero and (we have done a refresh or if we have
103
 
  already too many open table shares) then delete the definition.
104
 
*/
105
 
 
106
 
void TableShare::release(TableShare *share)
107
 
{
108
 
  bool to_be_deleted= false;
109
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
110
 
 
111
 
  share->lock();
112
 
  if (!--share->ref_count)
113
 
  {
114
 
    to_be_deleted= true;
115
 
  }
116
 
  share->unlock();
117
 
 
118
 
  if (to_be_deleted)
119
 
  {
120
 
    definition::Cache::singleton().erase(share->getCacheKey());
121
 
  }
122
 
}
123
 
 
124
 
void TableShare::release(TableShare::shared_ptr &share)
125
 
{
126
 
  bool to_be_deleted= false;
127
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
128
 
 
129
 
  share->lock();
130
 
  if (!--share->ref_count)
131
 
  {
132
 
    to_be_deleted= true;
133
 
  }
134
 
  share->unlock();
135
 
 
136
 
  if (to_be_deleted)
137
 
  {
138
 
    definition::Cache::singleton().erase(share->getCacheKey());
139
 
  }
140
 
}
141
 
 
142
 
void TableShare::release(const TableIdentifier &identifier)
143
 
{
144
 
  TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
145
 
  if (share)
146
 
  {
147
 
    share->version= 0;                          // Mark for delete
148
 
    if (share->ref_count == 0)
149
 
    {
150
 
      definition::Cache::singleton().erase(identifier.getKey());
151
 
    }
152
 
  }
153
 
}
154
 
 
155
 
 
156
 
static TableShare::shared_ptr foundTableShare(TableShare::shared_ptr share)
157
 
{
158
 
  /*
159
 
    We found an existing table definition. Return it if we didn't get
160
 
    an error when reading the table definition from file.
161
 
  */
162
 
 
163
 
  /* We must do a lock to ensure that the structure is initialized */
164
 
  if (share->error)
165
 
  {
166
 
    /* Table definition contained an error */
167
 
    share->open_table_error(share->error, share->open_errno, share->errarg);
168
 
 
169
 
    return TableShare::shared_ptr();
170
 
  }
171
 
 
172
 
  share->incrementTableCount();
173
 
 
174
 
  return share;
175
 
}
176
 
 
177
 
/*
178
 
  Get TableShare for a table.
179
 
 
180
 
  get_table_share()
181
 
  session                       Thread handle
182
 
  table_list            Table that should be opened
183
 
  key                   Table cache key
184
 
  key_length            Length of key
185
 
  error                 out: Error code from open_table_def()
186
 
 
187
 
  IMPLEMENTATION
188
 
  Get a table definition from the table definition cache.
189
 
  If it doesn't exist, create a new from the table definition file.
190
 
 
191
 
  NOTES
192
 
  We must have wrlock on table::Cache::singleton().mutex() when we come here
193
 
  (To be changed later)
194
 
 
195
 
  RETURN
196
 
  0  Error
197
 
#  Share for table
198
 
*/
199
 
 
200
 
TableShare::shared_ptr TableShare::getShareCreate(Session *session, 
201
 
                                                  const TableIdentifier &identifier,
202
 
                                                  int &in_error)
203
 
{
204
 
  TableShare::shared_ptr share;
205
 
 
206
 
  in_error= 0;
207
 
 
208
 
  /* Read table definition from cache */
209
 
  if ((share= definition::Cache::singleton().find(identifier.getKey())))
210
 
    return foundTableShare(share);
211
 
 
212
 
  share.reset(new TableShare(message::Table::STANDARD, identifier));
213
 
  
214
 
  if (share->open_table_def(*session, identifier))
215
 
  {
216
 
    in_error= share->error;
217
 
 
218
 
    return TableShare::shared_ptr();
219
 
  }
220
 
  share->ref_count++;                           // Mark in use
221
 
  
222
 
  plugin::EventObserver::registerTableEvents(*share);
223
 
 
224
 
  bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
225
 
 
226
 
  if (not ret)
227
 
    return TableShare::shared_ptr();
228
 
 
229
 
  return share;
230
 
}
231
 
 
232
 
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
233
 
{
234
 
  enum_field_types field_type;
235
 
 
236
 
  switch(proto_field_type)
 
96
 
 
97
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
 
98
{
 
99
  switch(field.type())
237
100
  {
238
101
  case message::Table::Field::INTEGER:
239
 
    field_type= DRIZZLE_TYPE_LONG;
240
 
    break;
 
102
    return DRIZZLE_TYPE_LONG;
 
103
 
241
104
  case message::Table::Field::DOUBLE:
242
 
    field_type= DRIZZLE_TYPE_DOUBLE;
243
 
    break;
244
 
  case message::Table::Field::TIMESTAMP:
245
 
    field_type= DRIZZLE_TYPE_TIMESTAMP;
246
 
    break;
 
105
    return DRIZZLE_TYPE_DOUBLE;
 
106
 
 
107
  case message::Table::Field::EPOCH:
 
108
    if (field.has_time_options() and field.time_options().microseconds())
 
109
      return DRIZZLE_TYPE_MICROTIME;
 
110
 
 
111
    return DRIZZLE_TYPE_TIMESTAMP;
 
112
 
247
113
  case message::Table::Field::BIGINT:
248
 
    field_type= DRIZZLE_TYPE_LONGLONG;
249
 
    break;
 
114
    return DRIZZLE_TYPE_LONGLONG;
 
115
 
250
116
  case message::Table::Field::DATETIME:
251
 
    field_type= DRIZZLE_TYPE_DATETIME;
252
 
    break;
 
117
    return DRIZZLE_TYPE_DATETIME;
 
118
 
253
119
  case message::Table::Field::DATE:
254
 
    field_type= DRIZZLE_TYPE_DATE;
255
 
    break;
 
120
    return DRIZZLE_TYPE_DATE;
 
121
 
256
122
  case message::Table::Field::VARCHAR:
257
 
    field_type= DRIZZLE_TYPE_VARCHAR;
258
 
    break;
 
123
    return DRIZZLE_TYPE_VARCHAR;
 
124
 
259
125
  case message::Table::Field::DECIMAL:
260
 
    field_type= DRIZZLE_TYPE_DECIMAL;
261
 
    break;
 
126
    return DRIZZLE_TYPE_DECIMAL;
 
127
 
262
128
  case message::Table::Field::ENUM:
263
 
    field_type= DRIZZLE_TYPE_ENUM;
264
 
    break;
 
129
    return DRIZZLE_TYPE_ENUM;
 
130
 
265
131
  case message::Table::Field::BLOB:
266
 
    field_type= DRIZZLE_TYPE_BLOB;
267
 
    break;
 
132
    return DRIZZLE_TYPE_BLOB;
 
133
 
268
134
  case message::Table::Field::UUID:
269
 
    field_type= DRIZZLE_TYPE_UUID;
270
 
    break;
271
 
  default:
272
 
    assert(0);
273
 
    abort(); // Programming error
 
135
    return  DRIZZLE_TYPE_UUID;
 
136
 
 
137
  case message::Table::Field::BOOLEAN:
 
138
    return DRIZZLE_TYPE_BOOLEAN;
 
139
 
 
140
  case message::Table::Field::TIME:
 
141
    return DRIZZLE_TYPE_TIME;
274
142
  }
275
143
 
276
 
  return field_type;
 
144
  abort();
277
145
}
278
146
 
279
147
static Item *default_value_item(enum_field_types field_type,
298
166
                                                                NULL,
299
167
                                                                &error),
300
168
                               default_value->length());
 
169
 
 
170
    if (error && error != -1) /* was an error and wasn't a negative number */
 
171
    {
 
172
      delete default_item;
 
173
      return NULL;
 
174
    }
 
175
 
301
176
    break;
302
177
  case DRIZZLE_TYPE_DOUBLE:
303
178
    default_item= new Item_float(default_value->c_str(),
308
183
    abort();
309
184
  case DRIZZLE_TYPE_TIMESTAMP:
310
185
  case DRIZZLE_TYPE_DATETIME:
 
186
  case DRIZZLE_TYPE_TIME:
311
187
  case DRIZZLE_TYPE_DATE:
312
188
  case DRIZZLE_TYPE_ENUM:
313
189
  case DRIZZLE_TYPE_UUID:
 
190
  case DRIZZLE_TYPE_MICROTIME:
 
191
  case DRIZZLE_TYPE_BOOLEAN:
314
192
    default_item= new Item_string(default_value->c_str(),
315
193
                                  default_value->length(),
316
194
                                  system_charset_info);
349
227
 */
350
228
bool TableShare::fieldInPrimaryKey(Field *in_field) const
351
229
{
352
 
  assert(table_proto != NULL);
 
230
  assert(getTableMessage());
353
231
 
354
 
  size_t num_indexes= table_proto->indexes_size();
 
232
  size_t num_indexes= getTableMessage()->indexes_size();
355
233
 
356
234
  for (size_t x= 0; x < num_indexes; ++x)
357
235
  {
358
 
    const message::Table::Index &index= table_proto->indexes(x);
 
236
    const message::Table::Index &index= getTableMessage()->indexes(x);
359
237
    if (index.is_primary())
360
238
    {
361
239
      size_t num_parts= index.index_part_size();
369
247
  return false;
370
248
}
371
249
 
372
 
TableShare::TableShare(const TableIdentifier::Type type_arg) :
 
250
TableShare::TableShare(const identifier::Table::Type type_arg) :
373
251
  table_category(TABLE_UNKNOWN_CATEGORY),
374
252
  found_next_number_field(NULL),
375
253
  timestamp_field(NULL),
376
254
  key_info(NULL),
377
255
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
378
256
  all_set(),
 
257
  db(NULL_LEX_STRING),
 
258
  table_name(NULL_LEX_STRING),
 
259
  path(NULL_LEX_STRING),
 
260
  normalized_path(NULL_LEX_STRING),
379
261
  block_size(0),
380
262
  version(0),
381
263
  timestamp_offset(0),
382
264
  reclength(0),
383
265
  stored_rec_length(0),
384
266
  max_rows(0),
385
 
  table_proto(NULL),
 
267
  _table_message(NULL),
386
268
  storage_engine(NULL),
387
269
  tmp_table(type_arg),
388
 
  ref_count(0),
 
270
  _ref_count(0),
389
271
  null_bytes(0),
390
272
  last_null_bit_pos(0),
391
 
  fields(0),
 
273
  _field_size(0),
392
274
  rec_buff_length(0),
393
275
  keys(0),
394
276
  key_parts(0),
410
292
  error(0),
411
293
  open_errno(0),
412
294
  errarg(0),
413
 
  blob_ptr_size(0),
 
295
  blob_ptr_size(portable_sizeof_char_ptr),
414
296
  db_low_byte_first(false),
415
297
  keys_in_use(0),
416
 
  keys_for_keyread(0),
417
 
  event_observers(NULL)
 
298
  keys_for_keyread(0)
418
299
{
419
 
 
420
 
  table_charset= 0;
421
 
  memset(&db, 0, sizeof(LEX_STRING));
422
 
  memset(&table_name, 0, sizeof(LEX_STRING));
423
 
  memset(&path, 0, sizeof(LEX_STRING));
424
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
425
 
 
426
300
  if (type_arg == message::Table::INTERNAL)
427
301
  {
428
 
    TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
 
302
    identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
429
303
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
430
304
  }
431
305
  else
434
308
  }
435
309
}
436
310
 
437
 
TableShare::TableShare(const TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
 
311
TableShare::TableShare(const identifier::Table &identifier, const identifier::Table::Key &key) :// Used by placeholder
438
312
  table_category(TABLE_UNKNOWN_CATEGORY),
439
313
  found_next_number_field(NULL),
440
314
  timestamp_field(NULL),
441
315
  key_info(NULL),
442
316
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
317
  table_charset(0),
443
318
  all_set(),
 
319
  db(NULL_LEX_STRING),
 
320
  table_name(NULL_LEX_STRING),
 
321
  path(NULL_LEX_STRING),
 
322
  normalized_path(NULL_LEX_STRING),
444
323
  block_size(0),
445
324
  version(0),
446
325
  timestamp_offset(0),
447
326
  reclength(0),
448
327
  stored_rec_length(0),
449
328
  max_rows(0),
450
 
  table_proto(NULL),
 
329
  _table_message(NULL),
451
330
  storage_engine(NULL),
452
331
  tmp_table(message::Table::INTERNAL),
453
 
  ref_count(0),
 
332
  _ref_count(0),
454
333
  null_bytes(0),
455
334
  last_null_bit_pos(0),
456
 
  fields(0),
 
335
  _field_size(0),
457
336
  rec_buff_length(0),
458
337
  keys(0),
459
338
  key_parts(0),
475
354
  error(0),
476
355
  open_errno(0),
477
356
  errarg(0),
478
 
  blob_ptr_size(0),
 
357
  blob_ptr_size(portable_sizeof_char_ptr),
479
358
  db_low_byte_first(false),
480
359
  keys_in_use(0),
481
 
  keys_for_keyread(0),
482
 
  event_observers(NULL)
 
360
  keys_for_keyread(0)
483
361
{
484
362
  assert(identifier.getKey() == key);
485
363
 
486
 
  table_charset= 0;
487
 
  memset(&path, 0, sizeof(LEX_STRING));
488
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
489
 
 
490
364
  private_key_for_cache= key;
491
365
 
492
366
  table_category=         TABLE_CATEGORY_TEMPORARY;
509
383
}
510
384
 
511
385
 
512
 
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
 
386
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
513
387
  table_category(TABLE_UNKNOWN_CATEGORY),
514
388
  found_next_number_field(NULL),
515
389
  timestamp_field(NULL),
516
390
  key_info(NULL),
517
391
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
392
  table_charset(0),
518
393
  all_set(),
 
394
  db(NULL_LEX_STRING),
 
395
  table_name(NULL_LEX_STRING),
 
396
  path(NULL_LEX_STRING),
 
397
  normalized_path(NULL_LEX_STRING),
519
398
  block_size(0),
520
399
  version(0),
521
400
  timestamp_offset(0),
522
401
  reclength(0),
523
402
  stored_rec_length(0),
524
403
  max_rows(0),
525
 
  table_proto(NULL),
 
404
  _table_message(NULL),
526
405
  storage_engine(NULL),
527
406
  tmp_table(identifier.getType()),
528
 
  ref_count(0),
 
407
  _ref_count(0),
529
408
  null_bytes(0),
530
409
  last_null_bit_pos(0),
531
 
  fields(0),
 
410
  _field_size(0),
532
411
  rec_buff_length(0),
533
412
  keys(0),
534
413
  key_parts(0),
550
429
  error(0),
551
430
  open_errno(0),
552
431
  errarg(0),
553
 
  blob_ptr_size(0),
 
432
  blob_ptr_size(portable_sizeof_char_ptr),
554
433
  db_low_byte_first(false),
555
434
  keys_in_use(0),
556
 
  keys_for_keyread(0),
557
 
  event_observers(NULL)
 
435
  keys_for_keyread(0)
558
436
{
559
 
  table_charset= 0;
560
 
  memset(&db, 0, sizeof(LEX_STRING));
561
 
  memset(&table_name, 0, sizeof(LEX_STRING));
562
 
  memset(&path, 0, sizeof(LEX_STRING));
563
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
564
 
 
565
437
  private_key_for_cache= identifier.getKey();
566
438
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
567
439
  private_normalized_path.resize(identifier.getPath().size() + 1);
584
456
/*
585
457
  Used for shares that will go into the cache.
586
458
*/
587
 
TableShare::TableShare(const TableIdentifier::Type type_arg,
588
 
                       const TableIdentifier &identifier,
 
459
TableShare::TableShare(const identifier::Table::Type type_arg,
 
460
                       const identifier::Table &identifier,
589
461
                       char *path_arg,
590
462
                       uint32_t path_length_arg) :
591
463
  table_category(TABLE_UNKNOWN_CATEGORY),
593
465
  timestamp_field(NULL),
594
466
  key_info(NULL),
595
467
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
468
  table_charset(0),
596
469
  all_set(),
 
470
  db(NULL_LEX_STRING),
 
471
  table_name(NULL_LEX_STRING),
 
472
  path(NULL_LEX_STRING),
 
473
  normalized_path(NULL_LEX_STRING),
597
474
  block_size(0),
598
475
  version(0),
599
476
  timestamp_offset(0),
600
477
  reclength(0),
601
478
  stored_rec_length(0),
602
479
  max_rows(0),
603
 
  table_proto(NULL),
 
480
  _table_message(NULL),
604
481
  storage_engine(NULL),
605
482
  tmp_table(type_arg),
606
 
  ref_count(0),
 
483
  _ref_count(0),
607
484
  null_bytes(0),
608
485
  last_null_bit_pos(0),
609
 
  fields(0),
 
486
  _field_size(0),
610
487
  rec_buff_length(0),
611
488
  keys(0),
612
489
  key_parts(0),
628
505
  error(0),
629
506
  open_errno(0),
630
507
  errarg(0),
631
 
  blob_ptr_size(0),
 
508
  blob_ptr_size(portable_sizeof_char_ptr),
632
509
  db_low_byte_first(false),
633
510
  keys_in_use(0),
634
 
  keys_for_keyread(0),
635
 
  event_observers(NULL)
 
511
  keys_for_keyread(0)
636
512
{
637
 
  table_charset= 0;
638
 
  memset(&db, 0, sizeof(LEX_STRING));
639
 
  memset(&table_name, 0, sizeof(LEX_STRING));
640
 
  memset(&path, 0, sizeof(LEX_STRING));
641
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
642
 
 
643
513
  char *path_buff;
644
514
  std::string _path;
645
515
 
659
529
  }
660
530
  else
661
531
  {
662
 
    TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
 
532
    identifier::Table::build_table_filename(_path, db.str, table_name.str, false);
663
533
  }
664
534
 
665
535
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
694
564
 
695
565
TableShare::~TableShare() 
696
566
{
697
 
  assert(ref_count == 0);
698
 
 
699
567
  storage_engine= NULL;
700
568
 
701
 
  delete table_proto;
702
 
  table_proto= NULL;
703
 
 
704
 
  plugin::EventObserver::deregisterTableEvents(*this);
705
 
 
706
569
  mem_root.free_root(MYF(0));                 // Free's share
707
570
}
708
571
 
709
 
void TableShare::setIdentifier(const TableIdentifier &identifier_arg)
 
572
void TableShare::setIdentifier(const identifier::Table &identifier_arg)
710
573
{
711
574
  private_key_for_cache= identifier_arg.getKey();
712
575
 
719
582
  table_name.str=    db.str + db.length + 1;
720
583
  table_name.length= strlen(table_name.str);
721
584
 
722
 
  table_proto->set_name(identifier_arg.getTableName());
723
 
  table_proto->set_schema(identifier_arg.getSchemaName());
 
585
  getTableMessage()->set_name(identifier_arg.getTableName());
 
586
  getTableMessage()->set_schema(identifier_arg.getSchemaName());
724
587
}
725
588
 
726
 
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
 
589
bool TableShare::parse_table_proto(Session& session, const message::Table &table)
727
590
{
728
 
  int local_error= 0;
 
591
  drizzled::error_t local_error= EE_OK;
729
592
 
730
593
  if (! table.IsInitialized())
731
594
  {
732
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
 
595
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
596
             table.name().empty() ? " " :  table.name().c_str(),
 
597
             table.InitializationErrorString().c_str());
 
598
 
733
599
    return ER_CORRUPT_TABLE_DEFINITION;
734
600
  }
735
601
 
736
 
  setTableProto(new(nothrow) message::Table(table));
 
602
  setTableMessage(table);
737
603
 
738
604
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
739
605
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
759
625
 
760
626
  table_charset= get_charset(table_options.collation_id());
761
627
 
762
 
  if (! table_charset)
 
628
  if (not table_charset)
763
629
  {
764
 
    char errmsg[100];
765
 
    snprintf(errmsg, sizeof(errmsg),
766
 
             _("Table %s has invalid/unknown collation: %d,%s"),
767
 
             getPath(),
768
 
             table_options.collation_id(),
769
 
             table_options.collation().c_str());
770
 
    errmsg[99]='\0';
 
630
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
 
631
             table_options.collation().c_str(),
 
632
             table.name().c_str());
771
633
 
772
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
773
 
    return ER_CORRUPT_TABLE_DEFINITION;
 
634
    return ER_CORRUPT_TABLE_DEFINITION; // Historical
774
635
  }
775
636
 
776
637
  db_record_offset= 1;
777
638
 
778
 
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
779
 
 
780
639
  keys= table.indexes_size();
781
640
 
782
641
  key_parts= 0;
926
785
  keys_for_keyread.reset();
927
786
  set_prefix(keys_in_use, keys);
928
787
 
929
 
  fields= table.field_size();
 
788
  _field_size= table.field_size();
930
789
 
931
 
  setFields(fields + 1);
932
 
  field[fields]= NULL;
 
790
  setFields(_field_size + 1);
 
791
  _fields[_field_size]= NULL;
933
792
 
934
793
  uint32_t local_null_fields= 0;
935
794
  reclength= 0;
937
796
  std::vector<uint32_t> field_offsets;
938
797
  std::vector<uint32_t> field_pack_length;
939
798
 
940
 
  field_offsets.resize(fields);
941
 
  field_pack_length.resize(fields);
 
799
  field_offsets.resize(_field_size);
 
800
  field_pack_length.resize(_field_size);
942
801
 
943
802
  uint32_t interval_count= 0;
944
803
  uint32_t interval_parts= 0;
945
804
 
946
805
  uint32_t stored_columns_reclength= 0;
947
806
 
948
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
807
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
949
808
  {
950
809
    message::Table::Field pfield= table.field(fieldnr);
951
 
    if (pfield.constraints().is_nullable())
952
 
      local_null_fields++;
 
810
    if (pfield.constraints().is_nullable()) // Historical reference
 
811
    {
 
812
      local_null_fields++;
 
813
    }
 
814
    else if (not pfield.constraints().is_notnull())
 
815
    {
 
816
      local_null_fields++;
 
817
    }
953
818
 
954
 
    enum_field_types drizzle_field_type=
955
 
      proto_field_type_to_drizzle_type(pfield.type());
 
819
    enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
956
820
 
957
821
    field_offsets[fieldnr]= stored_columns_reclength;
958
822
 
991
855
      {
992
856
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
993
857
 
994
 
        field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
 
858
        field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
995
859
      }
996
860
      break;
997
861
    default:
1041
905
 
1042
906
  uint32_t interval_nr= 0;
1043
907
 
1044
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
908
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1045
909
  {
1046
910
    message::Table::Field pfield= table.field(fieldnr);
1047
911
 
1053
917
 
1054
918
    if (field_options.field_value_size() > Field_enum::max_supported_elements)
1055
919
    {
1056
 
      char errmsg[100];
1057
 
      snprintf(errmsg, sizeof(errmsg),
1058
 
               _("ENUM column %s has greater than %d possible values"),
1059
 
               pfield.name().c_str(),
1060
 
               Field_enum::max_supported_elements);
1061
 
      errmsg[99]='\0';
 
920
      my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
1062
921
 
1063
 
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
1064
 
      return ER_CORRUPT_TABLE_DEFINITION;
 
922
      return ER_CORRUPT_TABLE_DEFINITION_ENUM; // Historical
1065
923
    }
1066
924
 
1067
925
 
1104
962
  /* and read the fields */
1105
963
  interval_nr= 0;
1106
964
 
1107
 
  bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
 
965
  bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
1108
966
 
1109
967
  unsigned char* null_pos= getDefaultValues();
1110
968
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
1111
969
 
1112
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
970
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1113
971
  {
1114
972
    message::Table::Field pfield= table.field(fieldnr);
1115
973
 
1164
1022
 
1165
1023
    enum_field_types field_type;
1166
1024
 
1167
 
    field_type= proto_field_type_to_drizzle_type(pfield.type());
 
1025
    field_type= proto_field_type_to_drizzle_type(pfield);
1168
1026
 
1169
1027
    const CHARSET_INFO *charset= &my_charset_bin;
1170
1028
 
1209
1067
      {
1210
1068
        if (fo.scale() > DECIMAL_MAX_SCALE)
1211
1069
        {
1212
 
          local_error= 4;
 
1070
          local_error= ER_NOT_FORM_FILE;
1213
1071
 
1214
 
          return local_error;
 
1072
          return true;
1215
1073
        }
1216
1074
        decimals= static_cast<uint8_t>(fo.scale());
1217
1075
      }
1228
1086
                                        pfield.options().default_null(),
1229
1087
                                        &pfield.options().default_value(),
1230
1088
                                        &pfield.options().default_bin_value());
 
1089
      if (default_value == NULL)
 
1090
      {
 
1091
        my_error(ER_INVALID_DEFAULT, MYF(0), pfield.name().c_str());
 
1092
        return true;
 
1093
      }
1231
1094
    }
1232
1095
 
1233
1096
 
1234
 
    blob_ptr_size= portable_sizeof_char_ptr;
1235
 
 
1236
1097
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1237
1098
 
 
1099
    // We set field_length in this loop.
1238
1100
    switch (field_type)
1239
1101
    {
1240
1102
    case DRIZZLE_TYPE_BLOB:
1266
1128
            decimals != NOT_FIXED_DEC)
1267
1129
        {
1268
1130
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1269
 
          local_error= 1;
1270
 
 
1271
 
          return local_error;
 
1131
          local_error= ER_M_BIGGER_THAN_D;
 
1132
          return true;
1272
1133
        }
1273
1134
        break;
1274
1135
      }
1276
1137
      {
1277
1138
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1278
1139
 
1279
 
        field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
 
1140
        field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
1280
1141
                                                     false);
1281
1142
        break;
1282
1143
      }
1283
 
    case DRIZZLE_TYPE_TIMESTAMP:
1284
1144
    case DRIZZLE_TYPE_DATETIME:
1285
1145
      field_length= DateTime::MAX_STRING_LENGTH;
1286
1146
      break;
1313
1173
      }
1314
1174
      break;
1315
1175
    case DRIZZLE_TYPE_LONGLONG:
1316
 
      field_length= MAX_BIGINT_WIDTH;
 
1176
      {
 
1177
        uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
 
1178
        field_length= MAX_BIGINT_WIDTH+sign_len;
 
1179
      }
1317
1180
      break;
1318
1181
    case DRIZZLE_TYPE_UUID:
1319
1182
      field_length= field::Uuid::max_string_length();
1320
1183
      break;
 
1184
    case DRIZZLE_TYPE_BOOLEAN:
 
1185
      field_length= field::Boolean::max_string_length();
 
1186
      break;
 
1187
    case DRIZZLE_TYPE_MICROTIME:
 
1188
      field_length= field::Microtime::max_string_length();
 
1189
      break;
 
1190
    case DRIZZLE_TYPE_TIMESTAMP:
 
1191
      field_length= field::Epoch::max_string_length();
 
1192
      break;
 
1193
    case DRIZZLE_TYPE_TIME:
 
1194
      field_length= field::Time::max_string_length();
 
1195
      break;
1321
1196
    case DRIZZLE_TYPE_NULL:
1322
1197
      abort(); // Programming error
1323
1198
    }
1324
1199
 
1325
 
    assert(enum_field_types_size == 12);
1326
 
 
1327
 
    Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
 
1200
    bool is_not_null= false;
 
1201
 
 
1202
    if (not pfield.constraints().is_nullable())
 
1203
    {
 
1204
      is_not_null= true;
 
1205
    }
 
1206
    else if (pfield.constraints().is_notnull())
 
1207
    {
 
1208
      is_not_null= true;
 
1209
    }
 
1210
 
 
1211
    Field* f= make_field(pfield,
 
1212
                         record + field_offsets[fieldnr] + data_offset,
1328
1213
                         field_length,
1329
 
                         pfield.constraints().is_nullable(),
 
1214
                         not is_not_null,
1330
1215
                         null_pos,
1331
1216
                         null_bit_pos,
1332
1217
                         decimals,
1334
1219
                         charset,
1335
1220
                         MTYP_TYPENR(unireg_type),
1336
1221
                         ((field_type == DRIZZLE_TYPE_ENUM) ?  &intervals[interval_nr++] : (TYPELIB*) 0),
1337
 
                         getTableProto()->field(fieldnr).name().c_str());
 
1222
                         getTableMessage()->field(fieldnr).name().c_str());
1338
1223
 
1339
 
    field[fieldnr]= f;
 
1224
    _fields[fieldnr]= f;
1340
1225
 
1341
1226
    // Insert post make_field code here.
1342
1227
    switch (field_type)
1346
1231
    case DRIZZLE_TYPE_DOUBLE:
1347
1232
    case DRIZZLE_TYPE_DECIMAL:
1348
1233
    case DRIZZLE_TYPE_TIMESTAMP:
 
1234
    case DRIZZLE_TYPE_TIME:
1349
1235
    case DRIZZLE_TYPE_DATETIME:
 
1236
    case DRIZZLE_TYPE_MICROTIME:
1350
1237
    case DRIZZLE_TYPE_DATE:
1351
1238
    case DRIZZLE_TYPE_ENUM:
1352
1239
    case DRIZZLE_TYPE_LONG:
1353
1240
    case DRIZZLE_TYPE_LONGLONG:
1354
1241
    case DRIZZLE_TYPE_NULL:
1355
1242
    case DRIZZLE_TYPE_UUID:
 
1243
    case DRIZZLE_TYPE_BOOLEAN:
1356
1244
      break;
1357
1245
    }
1358
1246
 
1380
1268
      if (res != 0 && res != 3) /* @TODO Huh? */
1381
1269
      {
1382
1270
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1383
 
        local_error= 1;
 
1271
        local_error= ER_INVALID_DEFAULT;
1384
1272
 
1385
 
        return local_error;
 
1273
        return true;
1386
1274
      }
1387
1275
    }
1388
1276
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
1401
1289
 
1402
1290
    f->setPosition(fieldnr);
1403
1291
    f->comment= comment;
1404
 
    if (! default_value &&
1405
 
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
 
1292
    if (not default_value &&
 
1293
        not (f->unireg_check==Field::NEXT_NUMBER) &&
1406
1294
        (f->flags & NOT_NULL_FLAG) &&
1407
 
        (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
 
1295
        (not f->is_timestamp()))
1408
1296
    {
1409
1297
      f->flags|= NO_DEFAULT_VALUE_FLAG;
1410
1298
    }
1411
1299
 
1412
1300
    if (f->unireg_check == Field::NEXT_NUMBER)
1413
 
      found_next_number_field= &(field[fieldnr]);
 
1301
      found_next_number_field= &(_fields[fieldnr]);
1414
1302
 
1415
1303
    if (use_hash) /* supposedly this never fails... but comments lie */
1416
1304
    {
1417
 
      const char *local_field_name= field[fieldnr]->field_name;
1418
 
      name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
 
1305
      const char *local_field_name= _fields[fieldnr]->field_name;
 
1306
      name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
1419
1307
    }
1420
1308
  }
1421
1309
 
1471
1359
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1472
1360
        {
1473
1361
          uint32_t fieldnr= key_part[i].fieldnr;
1474
 
          if (! fieldnr ||
1475
 
              field[fieldnr-1]->null_ptr ||
1476
 
              field[fieldnr-1]->key_length() != key_part[i].length)
 
1362
          if (not fieldnr ||
 
1363
              _fields[fieldnr-1]->null_ptr ||
 
1364
              _fields[fieldnr-1]->key_length() != key_part[i].length)
1477
1365
          {
1478
1366
            local_primary_key= MAX_KEY; // Can't be used
1479
1367
            break;
1488
1376
        {
1489
1377
          return ENOMEM;
1490
1378
        }
1491
 
        local_field= key_part->field= field[key_part->fieldnr-1];
 
1379
        local_field= key_part->field= _fields[key_part->fieldnr-1];
1492
1380
        key_part->type= local_field->key_type();
1493
1381
        if (local_field->null_ptr)
1494
1382
        {
1594
1482
                            &next_number_keypart)) < 0)
1595
1483
    {
1596
1484
      /* Wrong field definition */
1597
 
      local_error= 4;
 
1485
      local_error= ER_NOT_FORM_FILE;
1598
1486
 
1599
 
      return local_error;
 
1487
      return true;
1600
1488
    }
1601
1489
    else
1602
1490
    {
1610
1498
    blob_field.resize(blob_fields);
1611
1499
    uint32_t *save= &blob_field[0];
1612
1500
    uint32_t k= 0;
1613
 
    for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
 
1501
    for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
1614
1502
    {
1615
1503
      if ((*iter)->flags & BLOB_FLAG)
1616
1504
        (*save++)= k;
1618
1506
  }
1619
1507
 
1620
1508
  all_set.clear();
1621
 
  all_set.resize(fields);
 
1509
  all_set.resize(_field_size);
1622
1510
  all_set.set();
1623
1511
 
1624
 
  return local_error;
1625
 
}
1626
 
 
1627
 
int TableShare::parse_table_proto(Session& session, message::Table &table)
1628
 
{
1629
 
  int local_error= inner_parse_table_proto(session, table);
1630
 
 
1631
 
  if (not local_error)
1632
 
    return 0;
1633
 
 
1634
 
  error= local_error;
1635
 
  open_errno= errno;
1636
 
  errarg= 0;
1637
 
  open_table_error(local_error, open_errno, 0);
1638
 
 
1639
 
  return local_error;
1640
 
}
1641
 
 
 
1512
  return local_error != EE_OK;
 
1513
}
1642
1514
 
1643
1515
/*
1644
1516
  Read table definition from a binary / text based .frm cursor
1664
1536
  6    Unknown .frm version
1665
1537
*/
1666
1538
 
1667
 
int TableShare::open_table_def(Session& session, const TableIdentifier &identifier)
 
1539
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1668
1540
{
1669
 
  int local_error;
1670
 
  bool error_given;
1671
 
 
1672
 
  local_error= 1;
1673
 
  error_given= 0;
1674
 
 
1675
 
  message::table::shared_ptr table;
1676
 
 
1677
 
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1678
 
 
1679
 
  if (local_error != EEXIST)
 
1541
  drizzled::error_t local_error= EE_OK;
 
1542
 
 
1543
  message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
 
1544
 
 
1545
  if (table and table->IsInitialized())
1680
1546
  {
1681
 
    if (local_error > 0)
 
1547
    if (parse_table_proto(session, *table))
1682
1548
    {
1683
 
      errno= local_error;
1684
 
      local_error= 1;
 
1549
      local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
 
1550
      my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1685
1551
    }
1686
1552
    else
1687
1553
    {
1688
 
      if (not table->IsInitialized())
1689
 
      {
1690
 
        local_error= 4;
1691
 
      }
 
1554
      setTableCategory(TABLE_CATEGORY_USER);
 
1555
      local_error= EE_OK;
1692
1556
    }
1693
 
    goto err_not_open;
1694
 
  }
1695
 
 
1696
 
  local_error= parse_table_proto(session, *table);
1697
 
 
1698
 
  setTableCategory(TABLE_CATEGORY_USER);
1699
 
 
1700
 
err_not_open:
1701
 
  if (local_error && !error_given)
1702
 
  {
1703
 
    error= local_error;
1704
 
    open_table_error(error, (open_errno= errno), 0);
1705
 
  }
1706
 
 
1707
 
  return(error);
 
1557
  }
 
1558
  else if (table and not table->IsInitialized())
 
1559
  {
 
1560
    local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
 
1561
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
 
1562
  }
 
1563
  else
 
1564
  {
 
1565
    local_error= ER_TABLE_UNKNOWN;
 
1566
    my_error(ER_TABLE_UNKNOWN, identifier);
 
1567
  }
 
1568
 
 
1569
  return static_cast<int>(local_error);
1708
1570
}
1709
1571
 
1710
1572
 
1732
1594
  7    Table definition has changed in engine
1733
1595
*/
1734
1596
int TableShare::open_table_from_share(Session *session,
1735
 
                                      const TableIdentifier &identifier,
 
1597
                                      const identifier::Table &identifier,
1736
1598
                                      const char *alias,
1737
1599
                                      uint32_t db_stat, uint32_t ha_open_flags,
1738
1600
                                      Table &outparam)
1749
1611
  if (not error_reported)
1750
1612
    open_table_error(ret, errno, 0);
1751
1613
 
1752
 
  delete outparam.cursor;
 
1614
  boost::checked_delete(outparam.cursor);
1753
1615
  outparam.cursor= 0;                           // For easier error checking
1754
1616
  outparam.db_stat= 0;
1755
1617
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
1768
1630
  unsigned char *record= NULL;
1769
1631
  Field **field_ptr;
1770
1632
 
1771
 
  /* Parsing of partitioning information from .frm needs session->lex set up. */
1772
 
  assert(session->lex->is_lex_started);
1773
 
 
1774
1633
  local_error= 1;
1775
1634
  outparam.resetTable(session, this, db_stat);
1776
1635
 
1822
1681
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1823
1682
  }
1824
1683
 
1825
 
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
 
1684
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((_field_size+1)* sizeof(Field*)))))
1826
1685
  {
1827
1686
    return local_error;
1828
1687
  }
1834
1693
  outparam.null_flags= (unsigned char*) record+1;
1835
1694
 
1836
1695
  /* Setup copy of fields from share, but use the right alias and record */
1837
 
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
 
1696
  for (uint32_t i= 0 ; i < _field_size; i++, field_ptr++)
1838
1697
  {
1839
 
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
 
1698
    if (!((*field_ptr)= _fields[i]->clone(outparam.getMemRoot(), &outparam)))
1840
1699
      return local_error;
1841
1700
  }
1842
1701
  (*field_ptr)= 0;                              // End marker
1845
1704
    outparam.found_next_number_field=
1846
1705
      outparam.getField(positionFields(found_next_number_field));
1847
1706
  if (timestamp_field)
1848
 
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field->position());
 
1707
    outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
1849
1708
 
1850
1709
  /* Fix key->name and key_part->field */
1851
1710
  if (key_parts)
1894
1753
 
1895
1754
  /* Allocate bitmaps */
1896
1755
 
1897
 
  outparam.def_read_set.resize(fields);
1898
 
  outparam.def_write_set.resize(fields);
1899
 
  outparam.tmp_set.resize(fields);
 
1756
  outparam.def_read_set.resize(_field_size);
 
1757
  outparam.def_write_set.resize(_field_size);
 
1758
  outparam.tmp_set.resize(_field_size);
1900
1759
  outparam.default_column_bitmaps();
1901
1760
 
1902
1761
  return 0;
1903
1762
}
1904
1763
 
1905
 
int TableShare::open_table_cursor_inner(const TableIdentifier &identifier,
 
1764
int TableShare::open_table_cursor_inner(const identifier::Table &identifier,
1906
1765
                                        uint32_t db_stat, uint32_t ha_open_flags,
1907
1766
                                        Table &outparam,
1908
1767
                                        bool &error_reported)
1953
1812
/* error message when opening a form cursor */
1954
1813
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1955
1814
{
1956
 
  int err_no;
1957
1815
  char buff[FN_REFLEN];
1958
1816
  myf errortype= ME_ERROR+ME_WAITTANG;
1959
1817
 
1962
1820
  case 1:
1963
1821
    if (db_errno == ENOENT)
1964
1822
    {
1965
 
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
 
1823
      identifier::Table identifier(db.str, table_name.str);
 
1824
      my_error(ER_TABLE_UNKNOWN, identifier);
1966
1825
    }
1967
1826
    else
1968
1827
    {
1973
1832
    break;
1974
1833
  case 2:
1975
1834
    {
 
1835
      drizzled::error_t err_no;
 
1836
 
1976
1837
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
1977
1838
        ER_FILE_USED : ER_CANT_OPEN_FILE;
 
1839
 
1978
1840
      my_error(err_no, errortype, normalized_path.str, db_errno);
1979
1841
      break;
1980
1842
    }
2010
1872
  return;
2011
1873
} /* open_table_error */
2012
1874
 
2013
 
Field *TableShare::make_field(unsigned char *ptr,
 
1875
Field *TableShare::make_field(const message::Table::Field &pfield,
 
1876
                              unsigned char *ptr,
2014
1877
                              uint32_t field_length,
2015
1878
                              bool is_nullable,
2016
1879
                              unsigned char *null_pos,
2022
1885
                              TYPELIB *interval,
2023
1886
                              const char *field_name)
2024
1887
{
 
1888
  return make_field(pfield,
 
1889
                    ptr,
 
1890
                    field_length,
 
1891
                    is_nullable,
 
1892
                    null_pos,
 
1893
                    null_bit,
 
1894
                    decimals,
 
1895
                    field_type,
 
1896
                    field_charset,
 
1897
                    unireg_check,
 
1898
                    interval,
 
1899
                    field_name,
 
1900
                    pfield.constraints().is_unsigned());
 
1901
}
 
1902
 
 
1903
Field *TableShare::make_field(const message::Table::Field &,
 
1904
                              unsigned char *ptr,
 
1905
                              uint32_t field_length,
 
1906
                              bool is_nullable,
 
1907
                              unsigned char *null_pos,
 
1908
                              unsigned char null_bit,
 
1909
                              uint8_t decimals,
 
1910
                              enum_field_types field_type,
 
1911
                              const CHARSET_INFO * field_charset,
 
1912
                              Field::utype unireg_check,
 
1913
                              TYPELIB *interval,
 
1914
                              const char *field_name, 
 
1915
                              bool is_unsigned)
 
1916
{
2025
1917
  if (! is_nullable)
2026
1918
  {
2027
1919
    null_pos=0;
2032
1924
    null_bit= ((unsigned char) 1) << null_bit;
2033
1925
  }
2034
1926
 
2035
 
  switch (field_type) 
2036
 
  {
2037
 
  case DRIZZLE_TYPE_DATE:
2038
 
  case DRIZZLE_TYPE_DATETIME:
2039
 
  case DRIZZLE_TYPE_TIMESTAMP:
2040
 
  case DRIZZLE_TYPE_UUID:
2041
 
    field_charset= &my_charset_bin;
2042
 
  default: break;
2043
 
  }
2044
 
 
2045
1927
  switch (field_type)
2046
1928
  {
2047
1929
  case DRIZZLE_TYPE_ENUM:
2048
1930
    return new (&mem_root) Field_enum(ptr,
2049
 
                                 field_length,
2050
 
                                 null_pos,
2051
 
                                 null_bit,
2052
 
                                 field_name,
2053
 
                                 interval,
2054
 
                                 field_charset);
 
1931
                                      field_length,
 
1932
                                      null_pos,
 
1933
                                      null_bit,
 
1934
                                      field_name,
 
1935
                                      interval,
 
1936
                                      field_charset);
2055
1937
  case DRIZZLE_TYPE_VARCHAR:
2056
1938
    setVariableWidth();
2057
1939
    return new (&mem_root) Field_varstring(ptr,field_length,
2061
1943
                                      field_charset);
2062
1944
  case DRIZZLE_TYPE_BLOB:
2063
1945
    return new (&mem_root) Field_blob(ptr,
2064
 
                                 null_pos,
2065
 
                                 null_bit,
2066
 
                                 field_name,
2067
 
                                 this,
2068
 
                                 field_charset);
 
1946
                                      null_pos,
 
1947
                                      null_bit,
 
1948
                                      field_name,
 
1949
                                      this,
 
1950
                                      field_charset);
2069
1951
  case DRIZZLE_TYPE_DECIMAL:
2070
1952
    return new (&mem_root) Field_decimal(ptr,
2071
 
                                    field_length,
2072
 
                                    null_pos,
2073
 
                                    null_bit,
2074
 
                                    unireg_check,
2075
 
                                    field_name,
2076
 
                                    decimals,
2077
 
                                    false,
2078
 
                                    false /* is_unsigned */);
 
1953
                                         field_length,
 
1954
                                         null_pos,
 
1955
                                         null_bit,
 
1956
                                         unireg_check,
 
1957
                                         field_name,
 
1958
                                         decimals);
2079
1959
  case DRIZZLE_TYPE_DOUBLE:
2080
1960
    return new (&mem_root) Field_double(ptr,
2081
1961
                                   field_length,
2092
1972
                                       null_pos,
2093
1973
                                       null_bit,
2094
1974
                                       field_name);
 
1975
  case DRIZZLE_TYPE_BOOLEAN:
 
1976
    return new (&mem_root) field::Boolean(ptr,
 
1977
                                          field_length,
 
1978
                                          null_pos,
 
1979
                                          null_bit,
 
1980
                                          field_name,
 
1981
                                          is_unsigned);
2095
1982
  case DRIZZLE_TYPE_LONG:
2096
1983
    return new (&mem_root) field::Int32(ptr,
2097
1984
                                        field_length,
2100
1987
                                        unireg_check,
2101
1988
                                        field_name);
2102
1989
  case DRIZZLE_TYPE_LONGLONG:
2103
 
    return new (&mem_root) field::Int64(ptr,
2104
 
                                        field_length,
2105
 
                                        null_pos,
2106
 
                                        null_bit,
2107
 
                                        unireg_check,
2108
 
                                        field_name);
 
1990
    {
 
1991
      if (is_unsigned)
 
1992
      {
 
1993
        return new (&mem_root) field::Size(ptr,
 
1994
                                           field_length,
 
1995
                                           null_pos,
 
1996
                                           null_bit,
 
1997
                                           unireg_check,
 
1998
                                           field_name);
 
1999
      }
 
2000
 
 
2001
      return new (&mem_root) field::Int64(ptr,
 
2002
                                          field_length,
 
2003
                                          null_pos,
 
2004
                                          null_bit,
 
2005
                                          unireg_check,
 
2006
                                          field_name);
 
2007
    }
 
2008
  case DRIZZLE_TYPE_MICROTIME:
 
2009
    return new (&mem_root) field::Microtime(ptr,
 
2010
                                            null_pos,
 
2011
                                            null_bit,
 
2012
                                            unireg_check,
 
2013
                                            field_name,
 
2014
                                            this);
2109
2015
  case DRIZZLE_TYPE_TIMESTAMP:
2110
 
    return new (&mem_root) Field_timestamp(ptr,
2111
 
                                      field_length,
2112
 
                                      null_pos,
2113
 
                                      null_bit,
2114
 
                                      unireg_check,
2115
 
                                      field_name,
2116
 
                                      this,
2117
 
                                      field_charset);
 
2016
    return new (&mem_root) field::Epoch(ptr,
 
2017
                                        null_pos,
 
2018
                                        null_bit,
 
2019
                                        unireg_check,
 
2020
                                        field_name,
 
2021
                                        this);
 
2022
  case DRIZZLE_TYPE_TIME:
 
2023
    return new (&mem_root) field::Time(ptr,
 
2024
                                       field_length,
 
2025
                                       null_pos,
 
2026
                                       null_bit,
 
2027
                                       field_name);
2118
2028
  case DRIZZLE_TYPE_DATE:
2119
2029
    return new (&mem_root) Field_date(ptr,
2120
2030
                                 null_pos,
2121
2031
                                 null_bit,
2122
 
                                 field_name,
2123
 
                                 field_charset);
 
2032
                                 field_name);
2124
2033
  case DRIZZLE_TYPE_DATETIME:
2125
2034
    return new (&mem_root) Field_datetime(ptr,
2126
2035
                                     null_pos,
2127
2036
                                     null_bit,
2128
 
                                     field_name,
2129
 
                                     field_charset);
 
2037
                                     field_name);
2130
2038
  case DRIZZLE_TYPE_NULL:
2131
2039
    return new (&mem_root) Field_null(ptr,
2132
 
                                 field_length,
2133
 
                                 field_name,
2134
 
                                 field_charset);
 
2040
                                      field_length,
 
2041
                                      field_name);
2135
2042
  }
2136
2043
  assert(0);
2137
2044
  abort();
2138
2045
}
2139
2046
 
 
2047
void TableShare::refreshVersion()
 
2048
{
 
2049
  version= refresh_version;
 
2050
}
 
2051
 
2140
2052
 
2141
2053
} /* namespace drizzled */