~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/definition/table.cc

  • Committer: Brian Aker
  • Date: 2010-12-07 09:12:12 UTC
  • mto: This revision was merged to the branch mainline in revision 1985.
  • Revision ID: brian@tangent.org-20101207091212-1m0w20tck6z7632m
This is a fix for bug lp:686197

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; i/dent-tabs-mode: nil; -*-
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2010 Brian Aker
5
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 
5
 *  Copyright (C) 2009 Sun Microsystems
6
6
 *
7
7
 *  This program is free software; you can redistribute it and/or modify
8
8
 *  it under the terms of the GNU General Public License as published by
42
42
#include "drizzled/sql_base.h"
43
43
#include "drizzled/pthread_globals.h"
44
44
#include "drizzled/internal/my_pthread.h"
 
45
#include "drizzled/plugin/event_observer.h"
45
46
 
46
47
#include "drizzled/table.h"
47
48
#include "drizzled/table/shell.h"
63
64
#include "drizzled/field/str.h"
64
65
#include "drizzled/field/num.h"
65
66
#include "drizzled/field/blob.h"
66
 
#include "drizzled/field/boolean.h"
67
67
#include "drizzled/field/enum.h"
68
68
#include "drizzled/field/null.h"
69
69
#include "drizzled/field/date.h"
70
70
#include "drizzled/field/decimal.h"
71
71
#include "drizzled/field/real.h"
72
72
#include "drizzled/field/double.h"
73
 
#include "drizzled/field/int32.h"
74
 
#include "drizzled/field/int64.h"
75
 
#include "drizzled/field/size.h"
 
73
#include "drizzled/field/long.h"
 
74
#include "drizzled/field/int64_t.h"
76
75
#include "drizzled/field/num.h"
77
 
#include "drizzled/field/time.h"
78
 
#include "drizzled/field/epoch.h"
 
76
#include "drizzled/field/timestamp.h"
79
77
#include "drizzled/field/datetime.h"
80
 
#include "drizzled/field/microtime.h"
81
78
#include "drizzled/field/varstring.h"
82
 
#include "drizzled/field/uuid.h"
83
 
 
84
 
#include "drizzled/plugin/storage_engine.h"
85
79
 
86
80
#include "drizzled/definition/cache.h"
87
81
 
88
 
#include <drizzled/refresh_version.h>
89
 
 
90
82
using namespace std;
91
83
 
92
84
namespace drizzled
94
86
 
95
87
extern size_t table_def_size;
96
88
 
97
 
 
98
 
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
99
 
{
100
 
  switch(field.type())
 
89
/*****************************************************************************
 
90
  Functions to handle table definition cach (TableShare)
 
91
 *****************************************************************************/
 
92
 
 
93
/*
 
94
  Mark that we are not using table share anymore.
 
95
 
 
96
  SYNOPSIS
 
97
  release()
 
98
  share         Table share
 
99
 
 
100
  IMPLEMENTATION
 
101
  If ref_count goes to zero and (we have done a refresh or if we have
 
102
  already too many open table shares) then delete the definition.
 
103
*/
 
104
 
 
105
void TableShare::release(TableShare *share)
 
106
{
 
107
  bool to_be_deleted= false;
 
108
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
 
109
 
 
110
  share->lock();
 
111
  if (!--share->ref_count)
 
112
  {
 
113
    to_be_deleted= true;
 
114
  }
 
115
  share->unlock();
 
116
 
 
117
  if (to_be_deleted)
 
118
  {
 
119
    definition::Cache::singleton().erase(share->getCacheKey());
 
120
  }
 
121
}
 
122
 
 
123
void TableShare::release(TableShare::shared_ptr &share)
 
124
{
 
125
  bool to_be_deleted= false;
 
126
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
 
127
 
 
128
  share->lock();
 
129
  if (!--share->ref_count)
 
130
  {
 
131
    to_be_deleted= true;
 
132
  }
 
133
  share->unlock();
 
134
 
 
135
  if (to_be_deleted)
 
136
  {
 
137
    definition::Cache::singleton().erase(share->getCacheKey());
 
138
  }
 
139
}
 
140
 
 
141
void TableShare::release(const TableIdentifier &identifier)
 
142
{
 
143
  TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
 
144
  if (share)
 
145
  {
 
146
    share->version= 0;                          // Mark for delete
 
147
    if (share->ref_count == 0)
 
148
    {
 
149
      definition::Cache::singleton().erase(identifier.getKey());
 
150
    }
 
151
  }
 
152
}
 
153
 
 
154
 
 
155
static TableShare::shared_ptr foundTableShare(TableShare::shared_ptr share)
 
156
{
 
157
  /*
 
158
    We found an existing table definition. Return it if we didn't get
 
159
    an error when reading the table definition from file.
 
160
  */
 
161
 
 
162
  /* We must do a lock to ensure that the structure is initialized */
 
163
  if (share->error)
 
164
  {
 
165
    /* Table definition contained an error */
 
166
    share->open_table_error(share->error, share->open_errno, share->errarg);
 
167
 
 
168
    return TableShare::shared_ptr();
 
169
  }
 
170
 
 
171
  share->incrementTableCount();
 
172
 
 
173
  return share;
 
174
}
 
175
 
 
176
/*
 
177
  Get TableShare for a table.
 
178
 
 
179
  get_table_share()
 
180
  session                       Thread handle
 
181
  table_list            Table that should be opened
 
182
  key                   Table cache key
 
183
  key_length            Length of key
 
184
  error                 out: Error code from open_table_def()
 
185
 
 
186
  IMPLEMENTATION
 
187
  Get a table definition from the table definition cache.
 
188
  If it doesn't exist, create a new from the table definition file.
 
189
 
 
190
  NOTES
 
191
  We must have wrlock on table::Cache::singleton().mutex() when we come here
 
192
  (To be changed later)
 
193
 
 
194
  RETURN
 
195
  0  Error
 
196
#  Share for table
 
197
*/
 
198
 
 
199
TableShare::shared_ptr TableShare::getShareCreate(Session *session, 
 
200
                                                  const TableIdentifier &identifier,
 
201
                                                  int &in_error)
 
202
{
 
203
  TableShare::shared_ptr share;
 
204
 
 
205
  in_error= 0;
 
206
 
 
207
  /* Read table definition from cache */
 
208
  if ((share= definition::Cache::singleton().find(identifier.getKey())))
 
209
    return foundTableShare(share);
 
210
 
 
211
  share.reset(new TableShare(message::Table::STANDARD, identifier));
 
212
  
 
213
  if (share->open_table_def(*session, identifier))
 
214
  {
 
215
    in_error= share->error;
 
216
 
 
217
    return TableShare::shared_ptr();
 
218
  }
 
219
  share->ref_count++;                           // Mark in use
 
220
  
 
221
  plugin::EventObserver::registerTableEvents(*share);
 
222
 
 
223
  bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
 
224
 
 
225
  if (not ret)
 
226
    return TableShare::shared_ptr();
 
227
 
 
228
  return share;
 
229
}
 
230
 
 
231
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
 
232
{
 
233
  enum_field_types field_type;
 
234
 
 
235
  switch(proto_field_type)
101
236
  {
102
237
  case message::Table::Field::INTEGER:
103
 
    return DRIZZLE_TYPE_LONG;
104
 
 
 
238
    field_type= DRIZZLE_TYPE_LONG;
 
239
    break;
105
240
  case message::Table::Field::DOUBLE:
106
 
    return DRIZZLE_TYPE_DOUBLE;
107
 
 
108
 
  case message::Table::Field::EPOCH:
109
 
    if (field.has_time_options() and field.time_options().microseconds())
110
 
      return DRIZZLE_TYPE_MICROTIME;
111
 
 
112
 
    return DRIZZLE_TYPE_TIMESTAMP;
113
 
 
 
241
    field_type= DRIZZLE_TYPE_DOUBLE;
 
242
    break;
 
243
  case message::Table::Field::TIMESTAMP:
 
244
    field_type= DRIZZLE_TYPE_TIMESTAMP;
 
245
    break;
114
246
  case message::Table::Field::BIGINT:
115
 
    return DRIZZLE_TYPE_LONGLONG;
116
 
 
 
247
    field_type= DRIZZLE_TYPE_LONGLONG;
 
248
    break;
117
249
  case message::Table::Field::DATETIME:
118
 
    return DRIZZLE_TYPE_DATETIME;
119
 
 
 
250
    field_type= DRIZZLE_TYPE_DATETIME;
 
251
    break;
120
252
  case message::Table::Field::DATE:
121
 
    return DRIZZLE_TYPE_DATE;
122
 
 
 
253
    field_type= DRIZZLE_TYPE_DATE;
 
254
    break;
123
255
  case message::Table::Field::VARCHAR:
124
 
    return DRIZZLE_TYPE_VARCHAR;
125
 
 
 
256
    field_type= DRIZZLE_TYPE_VARCHAR;
 
257
    break;
126
258
  case message::Table::Field::DECIMAL:
127
 
    return DRIZZLE_TYPE_DECIMAL;
128
 
 
 
259
    field_type= DRIZZLE_TYPE_DECIMAL;
 
260
    break;
129
261
  case message::Table::Field::ENUM:
130
 
    return DRIZZLE_TYPE_ENUM;
131
 
 
 
262
    field_type= DRIZZLE_TYPE_ENUM;
 
263
    break;
132
264
  case message::Table::Field::BLOB:
133
 
    return DRIZZLE_TYPE_BLOB;
134
 
 
135
 
  case message::Table::Field::UUID:
136
 
    return  DRIZZLE_TYPE_UUID;
137
 
 
138
 
  case message::Table::Field::BOOLEAN:
139
 
    return DRIZZLE_TYPE_BOOLEAN;
140
 
 
141
 
  case message::Table::Field::TIME:
142
 
    return DRIZZLE_TYPE_TIME;
 
265
    field_type= DRIZZLE_TYPE_BLOB;
 
266
    break;
 
267
  default:
 
268
    field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
 
269
    assert(1);
143
270
  }
144
271
 
145
 
  abort();
 
272
  return field_type;
146
273
}
147
274
 
148
275
static Item *default_value_item(enum_field_types field_type,
173
300
                                 default_value->length());
174
301
    break;
175
302
  case DRIZZLE_TYPE_NULL:
176
 
    assert(0);
177
 
    abort();
 
303
    assert(false);
178
304
  case DRIZZLE_TYPE_TIMESTAMP:
179
305
  case DRIZZLE_TYPE_DATETIME:
180
 
  case DRIZZLE_TYPE_TIME:
181
306
  case DRIZZLE_TYPE_DATE:
182
307
  case DRIZZLE_TYPE_ENUM:
183
 
  case DRIZZLE_TYPE_UUID:
184
 
  case DRIZZLE_TYPE_MICROTIME:
185
 
  case DRIZZLE_TYPE_BOOLEAN:
186
308
    default_item= new Item_string(default_value->c_str(),
187
309
                                  default_value->length(),
188
310
                                  system_charset_info);
221
343
 */
222
344
bool TableShare::fieldInPrimaryKey(Field *in_field) const
223
345
{
224
 
  assert(getTableMessage());
 
346
  assert(table_proto != NULL);
225
347
 
226
 
  size_t num_indexes= getTableMessage()->indexes_size();
 
348
  size_t num_indexes= table_proto->indexes_size();
227
349
 
228
350
  for (size_t x= 0; x < num_indexes; ++x)
229
351
  {
230
 
    const message::Table::Index &index= getTableMessage()->indexes(x);
 
352
    const message::Table::Index &index= table_proto->indexes(x);
231
353
    if (index.is_primary())
232
354
    {
233
355
      size_t num_parts= index.index_part_size();
234
356
      for (size_t y= 0; y < num_parts; ++y)
235
357
      {
236
 
        if (index.index_part(y).fieldnr() == in_field->position())
 
358
        if (index.index_part(y).fieldnr() == in_field->field_index)
237
359
          return true;
238
360
      }
239
361
    }
241
363
  return false;
242
364
}
243
365
 
244
 
TableShare::TableShare(const identifier::Table::Type type_arg) :
 
366
TableShare::TableShare(const TableIdentifier::Type type_arg) :
245
367
  table_category(TABLE_UNKNOWN_CATEGORY),
246
368
  found_next_number_field(NULL),
247
369
  timestamp_field(NULL),
248
370
  key_info(NULL),
249
371
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
250
372
  all_set(),
251
 
  db(NULL_LEX_STRING),
252
 
  table_name(NULL_LEX_STRING),
253
 
  path(NULL_LEX_STRING),
254
 
  normalized_path(NULL_LEX_STRING),
255
373
  block_size(0),
256
374
  version(0),
257
375
  timestamp_offset(0),
258
376
  reclength(0),
259
377
  stored_rec_length(0),
260
378
  max_rows(0),
261
 
  _table_message(NULL),
 
379
  table_proto(NULL),
262
380
  storage_engine(NULL),
263
381
  tmp_table(type_arg),
264
 
  _ref_count(0),
 
382
  ref_count(0),
265
383
  null_bytes(0),
266
384
  last_null_bit_pos(0),
267
 
  _field_size(0),
 
385
  fields(0),
268
386
  rec_buff_length(0),
269
387
  keys(0),
270
388
  key_parts(0),
274
392
  uniques(0),
275
393
  null_fields(0),
276
394
  blob_fields(0),
 
395
  timestamp_field_offset(0),
277
396
  has_variable_width(false),
278
397
  db_create_options(0),
279
398
  db_options_in_use(0),
286
405
  error(0),
287
406
  open_errno(0),
288
407
  errarg(0),
289
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
408
  blob_ptr_size(0),
290
409
  db_low_byte_first(false),
291
410
  keys_in_use(0),
292
 
  keys_for_keyread(0)
 
411
  keys_for_keyread(0),
 
412
  event_observers(NULL)
293
413
{
 
414
 
 
415
  table_charset= 0;
 
416
  memset(&db, 0, sizeof(LEX_STRING));
 
417
  memset(&table_name, 0, sizeof(LEX_STRING));
 
418
  memset(&path, 0, sizeof(LEX_STRING));
 
419
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
420
 
294
421
  if (type_arg == message::Table::INTERNAL)
295
422
  {
296
 
    identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
 
423
    TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
297
424
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
298
425
  }
299
426
  else
302
429
  }
303
430
}
304
431
 
305
 
TableShare::TableShare(const identifier::Table &identifier, const identifier::Table::Key &key) :// Used by placeholder
 
432
TableShare::TableShare(const TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
306
433
  table_category(TABLE_UNKNOWN_CATEGORY),
307
434
  found_next_number_field(NULL),
308
435
  timestamp_field(NULL),
309
436
  key_info(NULL),
310
437
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
311
 
  table_charset(0),
312
438
  all_set(),
313
 
  db(NULL_LEX_STRING),
314
 
  table_name(NULL_LEX_STRING),
315
 
  path(NULL_LEX_STRING),
316
 
  normalized_path(NULL_LEX_STRING),
317
439
  block_size(0),
318
440
  version(0),
319
441
  timestamp_offset(0),
320
442
  reclength(0),
321
443
  stored_rec_length(0),
322
444
  max_rows(0),
323
 
  _table_message(NULL),
 
445
  table_proto(NULL),
324
446
  storage_engine(NULL),
325
447
  tmp_table(message::Table::INTERNAL),
326
 
  _ref_count(0),
 
448
  ref_count(0),
327
449
  null_bytes(0),
328
450
  last_null_bit_pos(0),
329
 
  _field_size(0),
 
451
  fields(0),
330
452
  rec_buff_length(0),
331
453
  keys(0),
332
454
  key_parts(0),
336
458
  uniques(0),
337
459
  null_fields(0),
338
460
  blob_fields(0),
 
461
  timestamp_field_offset(0),
339
462
  has_variable_width(false),
340
463
  db_create_options(0),
341
464
  db_options_in_use(0),
348
471
  error(0),
349
472
  open_errno(0),
350
473
  errarg(0),
351
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
474
  blob_ptr_size(0),
352
475
  db_low_byte_first(false),
353
476
  keys_in_use(0),
354
 
  keys_for_keyread(0)
 
477
  keys_for_keyread(0),
 
478
  event_observers(NULL)
355
479
{
356
480
  assert(identifier.getKey() == key);
357
481
 
 
482
  table_charset= 0;
 
483
  memset(&path, 0, sizeof(LEX_STRING));
 
484
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
485
 
358
486
  private_key_for_cache= key;
359
487
 
360
488
  table_category=         TABLE_CATEGORY_TEMPORARY;
377
505
}
378
506
 
379
507
 
380
 
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
 
508
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
381
509
  table_category(TABLE_UNKNOWN_CATEGORY),
382
510
  found_next_number_field(NULL),
383
511
  timestamp_field(NULL),
384
512
  key_info(NULL),
385
513
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
386
 
  table_charset(0),
387
514
  all_set(),
388
 
  db(NULL_LEX_STRING),
389
 
  table_name(NULL_LEX_STRING),
390
 
  path(NULL_LEX_STRING),
391
 
  normalized_path(NULL_LEX_STRING),
392
515
  block_size(0),
393
516
  version(0),
394
517
  timestamp_offset(0),
395
518
  reclength(0),
396
519
  stored_rec_length(0),
397
520
  max_rows(0),
398
 
  _table_message(NULL),
 
521
  table_proto(NULL),
399
522
  storage_engine(NULL),
400
523
  tmp_table(identifier.getType()),
401
 
  _ref_count(0),
 
524
  ref_count(0),
402
525
  null_bytes(0),
403
526
  last_null_bit_pos(0),
404
 
  _field_size(0),
 
527
  fields(0),
405
528
  rec_buff_length(0),
406
529
  keys(0),
407
530
  key_parts(0),
411
534
  uniques(0),
412
535
  null_fields(0),
413
536
  blob_fields(0),
 
537
  timestamp_field_offset(0),
414
538
  has_variable_width(false),
415
539
  db_create_options(0),
416
540
  db_options_in_use(0),
423
547
  error(0),
424
548
  open_errno(0),
425
549
  errarg(0),
426
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
550
  blob_ptr_size(0),
427
551
  db_low_byte_first(false),
428
552
  keys_in_use(0),
429
 
  keys_for_keyread(0)
 
553
  keys_for_keyread(0),
 
554
  event_observers(NULL)
430
555
{
 
556
  table_charset= 0;
 
557
  memset(&db, 0, sizeof(LEX_STRING));
 
558
  memset(&table_name, 0, sizeof(LEX_STRING));
 
559
  memset(&path, 0, sizeof(LEX_STRING));
 
560
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
561
 
431
562
  private_key_for_cache= identifier.getKey();
432
563
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
433
564
  private_normalized_path.resize(identifier.getPath().size() + 1);
450
581
/*
451
582
  Used for shares that will go into the cache.
452
583
*/
453
 
TableShare::TableShare(const identifier::Table::Type type_arg,
454
 
                       const identifier::Table &identifier,
 
584
TableShare::TableShare(const TableIdentifier::Type type_arg,
 
585
                       const TableIdentifier &identifier,
455
586
                       char *path_arg,
456
587
                       uint32_t path_length_arg) :
457
588
  table_category(TABLE_UNKNOWN_CATEGORY),
459
590
  timestamp_field(NULL),
460
591
  key_info(NULL),
461
592
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
462
 
  table_charset(0),
463
593
  all_set(),
464
 
  db(NULL_LEX_STRING),
465
 
  table_name(NULL_LEX_STRING),
466
 
  path(NULL_LEX_STRING),
467
 
  normalized_path(NULL_LEX_STRING),
468
594
  block_size(0),
469
595
  version(0),
470
596
  timestamp_offset(0),
471
597
  reclength(0),
472
598
  stored_rec_length(0),
473
599
  max_rows(0),
474
 
  _table_message(NULL),
 
600
  table_proto(NULL),
475
601
  storage_engine(NULL),
476
602
  tmp_table(type_arg),
477
 
  _ref_count(0),
 
603
  ref_count(0),
478
604
  null_bytes(0),
479
605
  last_null_bit_pos(0),
480
 
  _field_size(0),
 
606
  fields(0),
481
607
  rec_buff_length(0),
482
608
  keys(0),
483
609
  key_parts(0),
487
613
  uniques(0),
488
614
  null_fields(0),
489
615
  blob_fields(0),
 
616
  timestamp_field_offset(0),
490
617
  has_variable_width(false),
491
618
  db_create_options(0),
492
619
  db_options_in_use(0),
499
626
  error(0),
500
627
  open_errno(0),
501
628
  errarg(0),
502
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
629
  blob_ptr_size(0),
503
630
  db_low_byte_first(false),
504
631
  keys_in_use(0),
505
 
  keys_for_keyread(0)
 
632
  keys_for_keyread(0),
 
633
  event_observers(NULL)
506
634
{
 
635
  table_charset= 0;
 
636
  memset(&db, 0, sizeof(LEX_STRING));
 
637
  memset(&table_name, 0, sizeof(LEX_STRING));
 
638
  memset(&path, 0, sizeof(LEX_STRING));
 
639
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
640
 
507
641
  char *path_buff;
508
642
  std::string _path;
509
643
 
523
657
  }
524
658
  else
525
659
  {
526
 
    identifier::Table::build_table_filename(_path, db.str, table_name.str, false);
 
660
    TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
527
661
  }
528
662
 
529
663
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
537
671
  else
538
672
  {
539
673
    assert(0); // We should throw here.
540
 
    abort();
541
674
  }
542
675
}
543
676
 
558
691
 
559
692
TableShare::~TableShare() 
560
693
{
 
694
  assert(ref_count == 0);
 
695
 
561
696
  storage_engine= NULL;
562
697
 
 
698
  delete table_proto;
 
699
  table_proto= NULL;
 
700
 
 
701
  plugin::EventObserver::deregisterTableEvents(*this);
 
702
 
563
703
  mem_root.free_root(MYF(0));                 // Free's share
564
704
}
565
705
 
566
 
void TableShare::setIdentifier(const identifier::Table &identifier_arg)
 
706
void TableShare::setIdentifier(const TableIdentifier &identifier_arg)
567
707
{
568
708
  private_key_for_cache= identifier_arg.getKey();
569
709
 
576
716
  table_name.str=    db.str + db.length + 1;
577
717
  table_name.length= strlen(table_name.str);
578
718
 
579
 
  getTableMessage()->set_name(identifier_arg.getTableName());
580
 
  getTableMessage()->set_schema(identifier_arg.getSchemaName());
 
719
  table_proto->set_name(identifier_arg.getTableName());
 
720
  table_proto->set_schema(identifier_arg.getSchemaName());
581
721
}
582
722
 
583
 
bool TableShare::parse_table_proto(Session& session, message::Table &table)
 
723
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
584
724
{
585
 
  drizzled::error_t local_error= EE_OK;
 
725
  int local_error= 0;
586
726
 
587
727
  if (! table.IsInitialized())
588
728
  {
589
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
590
 
             table.name().empty() ? " " :  table.name().c_str(),
591
 
             table.InitializationErrorString().c_str());
592
 
 
 
729
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
593
730
    return ER_CORRUPT_TABLE_DEFINITION;
594
731
  }
595
732
 
596
 
  setTableMessage(table);
 
733
  setTableProto(new(nothrow) message::Table(table));
597
734
 
598
735
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
599
736
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
619
756
 
620
757
  table_charset= get_charset(table_options.collation_id());
621
758
 
622
 
  if (not table_charset)
 
759
  if (! table_charset)
623
760
  {
624
 
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
625
 
             table_options.collation().c_str(),
626
 
             table.name().c_str());
 
761
    char errmsg[100];
 
762
    snprintf(errmsg, sizeof(errmsg),
 
763
             _("Table %s has invalid/unknown collation: %d,%s"),
 
764
             getPath(),
 
765
             table_options.collation_id(),
 
766
             table_options.collation().c_str());
 
767
    errmsg[99]='\0';
627
768
 
628
 
    return ER_CORRUPT_TABLE_DEFINITION; // Historical
 
769
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
 
770
    return ER_CORRUPT_TABLE_DEFINITION;
629
771
  }
630
772
 
631
773
  db_record_offset= 1;
632
774
 
 
775
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
 
776
 
633
777
  keys= table.indexes_size();
634
778
 
635
779
  key_parts= 0;
779
923
  keys_for_keyread.reset();
780
924
  set_prefix(keys_in_use, keys);
781
925
 
782
 
  _field_size= table.field_size();
 
926
  fields= table.field_size();
783
927
 
784
 
  setFields(_field_size + 1);
785
 
  _fields[_field_size]= NULL;
 
928
  setFields(fields + 1);
 
929
  field[fields]= NULL;
786
930
 
787
931
  uint32_t local_null_fields= 0;
788
932
  reclength= 0;
790
934
  std::vector<uint32_t> field_offsets;
791
935
  std::vector<uint32_t> field_pack_length;
792
936
 
793
 
  field_offsets.resize(_field_size);
794
 
  field_pack_length.resize(_field_size);
 
937
  field_offsets.resize(fields);
 
938
  field_pack_length.resize(fields);
795
939
 
796
940
  uint32_t interval_count= 0;
797
941
  uint32_t interval_parts= 0;
798
942
 
799
943
  uint32_t stored_columns_reclength= 0;
800
944
 
801
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
945
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
802
946
  {
803
947
    message::Table::Field pfield= table.field(fieldnr);
804
 
    if (pfield.constraints().is_nullable()) // Historical reference
805
 
    {
806
 
      local_null_fields++;
807
 
    }
808
 
    else if (not pfield.constraints().is_notnull())
809
 
    {
810
 
      local_null_fields++;
811
 
    }
 
948
    if (pfield.constraints().is_nullable())
 
949
      local_null_fields++;
812
950
 
813
 
    enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
 
951
    enum_field_types drizzle_field_type=
 
952
      proto_field_type_to_drizzle_type(pfield.type());
814
953
 
815
954
    field_offsets[fieldnr]= stored_columns_reclength;
816
955
 
849
988
      {
850
989
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
851
990
 
852
 
        field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
 
991
        field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
853
992
      }
854
993
      break;
855
994
    default:
899
1038
 
900
1039
  uint32_t interval_nr= 0;
901
1040
 
902
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1041
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
903
1042
  {
904
1043
    message::Table::Field pfield= table.field(fieldnr);
905
1044
 
911
1050
 
912
1051
    if (field_options.field_value_size() > Field_enum::max_supported_elements)
913
1052
    {
914
 
      my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
 
1053
      char errmsg[100];
 
1054
      snprintf(errmsg, sizeof(errmsg),
 
1055
               _("ENUM column %s has greater than %d possible values"),
 
1056
               pfield.name().c_str(),
 
1057
               Field_enum::max_supported_elements);
 
1058
      errmsg[99]='\0';
915
1059
 
916
 
      return ER_CORRUPT_TABLE_DEFINITION_ENUM; // Historical
 
1060
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
 
1061
      return ER_CORRUPT_TABLE_DEFINITION;
917
1062
    }
918
1063
 
919
1064
 
956
1101
  /* and read the fields */
957
1102
  interval_nr= 0;
958
1103
 
959
 
  bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
 
1104
  bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
960
1105
 
961
1106
  unsigned char* null_pos= getDefaultValues();
962
1107
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
963
1108
 
964
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1109
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
965
1110
  {
966
1111
    message::Table::Field pfield= table.field(fieldnr);
967
1112
 
987
1132
        unireg_type= Field::TIMESTAMP_DN_FIELD;
988
1133
      }
989
1134
      else
990
 
      {
991
 
        assert(0); // Invalid update value.
992
 
        abort();
993
 
      }
 
1135
        assert(1); // Invalid update value.
994
1136
    }
995
1137
    else if (pfield.has_options() &&
996
1138
             pfield.options().has_update_expression() &&
1016
1158
 
1017
1159
    enum_field_types field_type;
1018
1160
 
1019
 
    field_type= proto_field_type_to_drizzle_type(pfield);
 
1161
    field_type= proto_field_type_to_drizzle_type(pfield.type());
1020
1162
 
1021
1163
    const CHARSET_INFO *charset= &my_charset_bin;
1022
1164
 
1061
1203
      {
1062
1204
        if (fo.scale() > DECIMAL_MAX_SCALE)
1063
1205
        {
1064
 
          local_error= ER_NOT_FORM_FILE;
 
1206
          local_error= 4;
1065
1207
 
1066
 
          return true;
 
1208
          return local_error;
1067
1209
        }
1068
1210
        decimals= static_cast<uint8_t>(fo.scale());
1069
1211
      }
1083
1225
    }
1084
1226
 
1085
1227
 
 
1228
    db_low_byte_first= true; //Cursor->low_byte_first();
 
1229
    blob_ptr_size= portable_sizeof_char_ptr;
 
1230
 
1086
1231
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1087
1232
 
1088
 
    // We set field_length in this loop.
1089
1233
    switch (field_type)
1090
1234
    {
1091
1235
    case DRIZZLE_TYPE_BLOB:
1117
1261
            decimals != NOT_FIXED_DEC)
1118
1262
        {
1119
1263
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1120
 
          local_error= ER_M_BIGGER_THAN_D;
1121
 
          return true;
 
1264
          local_error= 1;
 
1265
 
 
1266
          return local_error;
1122
1267
        }
1123
1268
        break;
1124
1269
      }
1126
1271
      {
1127
1272
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1128
1273
 
1129
 
        field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
 
1274
        field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
1130
1275
                                                     false);
1131
1276
        break;
1132
1277
      }
 
1278
    case DRIZZLE_TYPE_TIMESTAMP:
1133
1279
    case DRIZZLE_TYPE_DATETIME:
1134
1280
      field_length= DateTime::MAX_STRING_LENGTH;
1135
1281
      break;
1162
1308
      }
1163
1309
      break;
1164
1310
    case DRIZZLE_TYPE_LONGLONG:
1165
 
      {
1166
 
        uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1167
 
        field_length= MAX_BIGINT_WIDTH+sign_len;
1168
 
      }
1169
 
      break;
1170
 
    case DRIZZLE_TYPE_UUID:
1171
 
      field_length= field::Uuid::max_string_length();
1172
 
      break;
1173
 
    case DRIZZLE_TYPE_BOOLEAN:
1174
 
      field_length= field::Boolean::max_string_length();
1175
 
      break;
1176
 
    case DRIZZLE_TYPE_MICROTIME:
1177
 
      field_length= field::Microtime::max_string_length();
1178
 
      break;
1179
 
    case DRIZZLE_TYPE_TIMESTAMP:
1180
 
      field_length= field::Epoch::max_string_length();
1181
 
      break;
1182
 
    case DRIZZLE_TYPE_TIME:
1183
 
      field_length= field::Time::max_string_length();
 
1311
      field_length= MAX_BIGINT_WIDTH;
1184
1312
      break;
1185
1313
    case DRIZZLE_TYPE_NULL:
1186
1314
      abort(); // Programming error
1187
1315
    }
1188
1316
 
1189
 
    bool is_not_null= false;
1190
 
 
1191
 
    if (not pfield.constraints().is_nullable())
1192
 
    {
1193
 
      is_not_null= true;
1194
 
    }
1195
 
    else if (pfield.constraints().is_notnull())
1196
 
    {
1197
 
      is_not_null= true;
1198
 
    }
1199
 
 
1200
 
    Field* f= make_field(pfield,
1201
 
                         record + field_offsets[fieldnr] + data_offset,
1202
 
                         field_length,
1203
 
                         not is_not_null,
1204
 
                         null_pos,
1205
 
                         null_bit_pos,
1206
 
                         decimals,
1207
 
                         field_type,
1208
 
                         charset,
1209
 
                         MTYP_TYPENR(unireg_type),
1210
 
                         ((field_type == DRIZZLE_TYPE_ENUM) ?  &intervals[interval_nr++] : (TYPELIB*) 0),
1211
 
                         getTableMessage()->field(fieldnr).name().c_str());
1212
 
 
1213
 
    _fields[fieldnr]= f;
1214
 
 
1215
 
    // Insert post make_field code here.
1216
 
    switch (field_type)
1217
 
    {
1218
 
    case DRIZZLE_TYPE_BLOB:
1219
 
    case DRIZZLE_TYPE_VARCHAR:
1220
 
    case DRIZZLE_TYPE_DOUBLE:
1221
 
    case DRIZZLE_TYPE_DECIMAL:
1222
 
    case DRIZZLE_TYPE_TIMESTAMP:
1223
 
    case DRIZZLE_TYPE_TIME:
1224
 
    case DRIZZLE_TYPE_DATETIME:
1225
 
    case DRIZZLE_TYPE_MICROTIME:
1226
 
    case DRIZZLE_TYPE_DATE:
1227
 
    case DRIZZLE_TYPE_ENUM:
1228
 
    case DRIZZLE_TYPE_LONG:
1229
 
    case DRIZZLE_TYPE_LONGLONG:
1230
 
    case DRIZZLE_TYPE_NULL:
1231
 
    case DRIZZLE_TYPE_UUID:
1232
 
    case DRIZZLE_TYPE_BOOLEAN:
1233
 
      break;
1234
 
    }
 
1317
    Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
 
1318
                                field_length,
 
1319
                                pfield.constraints().is_nullable(),
 
1320
                                null_pos,
 
1321
                                null_bit_pos,
 
1322
                                decimals,
 
1323
                                field_type,
 
1324
                                charset,
 
1325
                                MTYP_TYPENR(unireg_type),
 
1326
                                ((field_type == DRIZZLE_TYPE_ENUM) ?
 
1327
                                 &intervals[interval_nr++]
 
1328
                                 : (TYPELIB*) 0),
 
1329
                                getTableProto()->field(fieldnr).name().c_str());
 
1330
 
 
1331
    field[fieldnr]= f;
1235
1332
 
1236
1333
    // This needs to go, we should be setting the "use" on the field so that
1237
1334
    // it does not reference the share/table.
1257
1354
      if (res != 0 && res != 3) /* @TODO Huh? */
1258
1355
      {
1259
1356
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1260
 
        local_error= ER_INVALID_DEFAULT;
 
1357
        local_error= 1;
1261
1358
 
1262
 
        return true;
 
1359
        return local_error;
1263
1360
      }
1264
1361
    }
1265
 
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
 
1362
    else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
 
1363
             (f->flags & NOT_NULL_FLAG))
1266
1364
    {
1267
1365
      f->set_notnull();
1268
1366
      f->store((int64_t) 1, true);
1276
1374
    f->setTable(NULL);
1277
1375
    f->orig_table= NULL;
1278
1376
 
1279
 
    f->setPosition(fieldnr);
 
1377
    f->field_index= fieldnr;
1280
1378
    f->comment= comment;
1281
 
    if (not default_value &&
1282
 
        not (f->unireg_check==Field::NEXT_NUMBER) &&
 
1379
    if (! default_value &&
 
1380
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
1283
1381
        (f->flags & NOT_NULL_FLAG) &&
1284
 
        (not f->is_timestamp()))
 
1382
        (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
1285
1383
    {
1286
1384
      f->flags|= NO_DEFAULT_VALUE_FLAG;
1287
1385
    }
1288
1386
 
1289
1387
    if (f->unireg_check == Field::NEXT_NUMBER)
1290
 
      found_next_number_field= &(_fields[fieldnr]);
 
1388
      found_next_number_field= &(field[fieldnr]);
 
1389
 
 
1390
    if (timestamp_field == f)
 
1391
      timestamp_field_offset= fieldnr;
1291
1392
 
1292
1393
    if (use_hash) /* supposedly this never fails... but comments lie */
1293
1394
    {
1294
 
      const char *local_field_name= _fields[fieldnr]->field_name;
1295
 
      name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
 
1395
      const char *local_field_name= field[fieldnr]->field_name;
 
1396
      name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
1296
1397
    }
 
1398
 
1297
1399
  }
1298
1400
 
1299
1401
  keyinfo= key_info;
1318
1420
    We need to set the unused bits to 1. If the number of bits is a multiple
1319
1421
    of 8 there are no unused bits.
1320
1422
  */
 
1423
 
1321
1424
  if (null_count & 7)
1322
1425
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1323
1426
 
1348
1451
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1349
1452
        {
1350
1453
          uint32_t fieldnr= key_part[i].fieldnr;
1351
 
          if (not fieldnr ||
1352
 
              _fields[fieldnr-1]->null_ptr ||
1353
 
              _fields[fieldnr-1]->key_length() != key_part[i].length)
 
1454
          if (! fieldnr ||
 
1455
              field[fieldnr-1]->null_ptr ||
 
1456
              field[fieldnr-1]->key_length() != key_part[i].length)
1354
1457
          {
1355
1458
            local_primary_key= MAX_KEY; // Can't be used
1356
1459
            break;
1365
1468
        {
1366
1469
          return ENOMEM;
1367
1470
        }
1368
 
        local_field= key_part->field= _fields[key_part->fieldnr-1];
 
1471
        local_field= key_part->field= field[key_part->fieldnr-1];
1369
1472
        key_part->type= local_field->key_type();
1370
1473
        if (local_field->null_ptr)
1371
1474
        {
1471
1574
                            &next_number_keypart)) < 0)
1472
1575
    {
1473
1576
      /* Wrong field definition */
1474
 
      local_error= ER_NOT_FORM_FILE;
 
1577
      local_error= 4;
1475
1578
 
1476
 
      return true;
 
1579
      return local_error;
1477
1580
    }
1478
1581
    else
1479
1582
    {
1487
1590
    blob_field.resize(blob_fields);
1488
1591
    uint32_t *save= &blob_field[0];
1489
1592
    uint32_t k= 0;
1490
 
    for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
 
1593
    for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
1491
1594
    {
1492
1595
      if ((*iter)->flags & BLOB_FLAG)
1493
1596
        (*save++)= k;
1494
1597
    }
1495
1598
  }
1496
1599
 
 
1600
  db_low_byte_first= true; // @todo Question this.
1497
1601
  all_set.clear();
1498
 
  all_set.resize(_field_size);
 
1602
  all_set.resize(fields);
1499
1603
  all_set.set();
1500
1604
 
1501
 
  return local_error != EE_OK;
1502
 
}
 
1605
  return local_error;
 
1606
}
 
1607
 
 
1608
int TableShare::parse_table_proto(Session& session, message::Table &table)
 
1609
{
 
1610
  int local_error= inner_parse_table_proto(session, table);
 
1611
 
 
1612
  if (not local_error)
 
1613
    return 0;
 
1614
 
 
1615
  error= local_error;
 
1616
  open_errno= errno;
 
1617
  errarg= 0;
 
1618
  open_table_error(local_error, open_errno, 0);
 
1619
 
 
1620
  return local_error;
 
1621
}
 
1622
 
1503
1623
 
1504
1624
/*
1505
1625
  Read table definition from a binary / text based .frm cursor
1525
1645
  6    Unknown .frm version
1526
1646
*/
1527
1647
 
1528
 
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
 
1648
int TableShare::open_table_def(Session& session, const TableIdentifier &identifier)
1529
1649
{
1530
 
  drizzled::error_t local_error= EE_OK;
1531
 
 
1532
 
  message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
1533
 
 
1534
 
  if (table and table->IsInitialized())
 
1650
  int local_error;
 
1651
  bool error_given;
 
1652
 
 
1653
  local_error= 1;
 
1654
  error_given= 0;
 
1655
 
 
1656
  message::table::shared_ptr table;
 
1657
 
 
1658
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
 
1659
 
 
1660
  if (local_error != EEXIST)
1535
1661
  {
1536
 
    if (parse_table_proto(session, *table))
 
1662
    if (local_error > 0)
1537
1663
    {
1538
 
      local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1539
 
      my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
 
1664
      errno= local_error;
 
1665
      local_error= 1;
1540
1666
    }
1541
1667
    else
1542
1668
    {
1543
 
      setTableCategory(TABLE_CATEGORY_USER);
1544
 
      local_error= EE_OK;
 
1669
      if (not table->IsInitialized())
 
1670
      {
 
1671
        local_error= 4;
 
1672
      }
1545
1673
    }
1546
 
  }
1547
 
  else if (table and not table->IsInitialized())
1548
 
  {
1549
 
    local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1550
 
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1551
 
  }
1552
 
  else
1553
 
  {
1554
 
    local_error= ER_TABLE_UNKNOWN;
1555
 
    my_error(ER_TABLE_UNKNOWN, identifier);
1556
 
  }
1557
 
 
1558
 
  return static_cast<int>(local_error);
 
1674
    goto err_not_open;
 
1675
  }
 
1676
 
 
1677
  local_error= parse_table_proto(session, *table);
 
1678
 
 
1679
  setTableCategory(TABLE_CATEGORY_USER);
 
1680
 
 
1681
err_not_open:
 
1682
  if (local_error && !error_given)
 
1683
  {
 
1684
    error= local_error;
 
1685
    open_table_error(error, (open_errno= errno), 0);
 
1686
  }
 
1687
 
 
1688
  return(error);
1559
1689
}
1560
1690
 
1561
1691
 
1583
1713
  7    Table definition has changed in engine
1584
1714
*/
1585
1715
int TableShare::open_table_from_share(Session *session,
1586
 
                                      const identifier::Table &identifier,
 
1716
                                      const TableIdentifier &identifier,
1587
1717
                                      const char *alias,
1588
1718
                                      uint32_t db_stat, uint32_t ha_open_flags,
1589
1719
                                      Table &outparam)
1600
1730
  if (not error_reported)
1601
1731
    open_table_error(ret, errno, 0);
1602
1732
 
1603
 
  boost::checked_delete(outparam.cursor);
 
1733
  delete outparam.cursor;
1604
1734
  outparam.cursor= 0;                           // For easier error checking
1605
1735
  outparam.db_stat= 0;
1606
1736
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
1619
1749
  unsigned char *record= NULL;
1620
1750
  Field **field_ptr;
1621
1751
 
 
1752
  /* Parsing of partitioning information from .frm needs session->lex set up. */
 
1753
  assert(session->lex->is_lex_started);
 
1754
 
1622
1755
  local_error= 1;
1623
1756
  outparam.resetTable(session, this, db_stat);
1624
1757
 
1670
1803
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1671
1804
  }
1672
1805
 
1673
 
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((_field_size+1)* sizeof(Field*)))))
 
1806
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
1674
1807
  {
1675
1808
    return local_error;
1676
1809
  }
1682
1815
  outparam.null_flags= (unsigned char*) record+1;
1683
1816
 
1684
1817
  /* Setup copy of fields from share, but use the right alias and record */
1685
 
  for (uint32_t i= 0 ; i < _field_size; i++, field_ptr++)
 
1818
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
1686
1819
  {
1687
 
    if (!((*field_ptr)= _fields[i]->clone(outparam.getMemRoot(), &outparam)))
 
1820
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
1688
1821
      return local_error;
1689
1822
  }
1690
1823
  (*field_ptr)= 0;                              // End marker
1693
1826
    outparam.found_next_number_field=
1694
1827
      outparam.getField(positionFields(found_next_number_field));
1695
1828
  if (timestamp_field)
1696
 
    outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
 
1829
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
 
1830
 
1697
1831
 
1698
1832
  /* Fix key->name and key_part->field */
1699
1833
  if (key_parts)
1742
1876
 
1743
1877
  /* Allocate bitmaps */
1744
1878
 
1745
 
  outparam.def_read_set.resize(_field_size);
1746
 
  outparam.def_write_set.resize(_field_size);
1747
 
  outparam.tmp_set.resize(_field_size);
 
1879
  outparam.def_read_set.resize(fields);
 
1880
  outparam.def_write_set.resize(fields);
 
1881
  outparam.tmp_set.resize(fields);
1748
1882
  outparam.default_column_bitmaps();
1749
1883
 
1750
1884
  return 0;
1751
1885
}
1752
1886
 
1753
 
int TableShare::open_table_cursor_inner(const identifier::Table &identifier,
 
1887
int TableShare::open_table_cursor_inner(const TableIdentifier &identifier,
1754
1888
                                        uint32_t db_stat, uint32_t ha_open_flags,
1755
1889
                                        Table &outparam,
1756
1890
                                        bool &error_reported)
1801
1935
/* error message when opening a form cursor */
1802
1936
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1803
1937
{
 
1938
  int err_no;
1804
1939
  char buff[FN_REFLEN];
1805
1940
  myf errortype= ME_ERROR+ME_WAITTANG;
1806
1941
 
1809
1944
  case 1:
1810
1945
    if (db_errno == ENOENT)
1811
1946
    {
1812
 
      identifier::Table identifier(db.str, table_name.str);
1813
 
      my_error(ER_TABLE_UNKNOWN, identifier);
 
1947
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
1814
1948
    }
1815
1949
    else
1816
1950
    {
1821
1955
    break;
1822
1956
  case 2:
1823
1957
    {
1824
 
      drizzled::error_t err_no;
1825
 
 
1826
1958
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
1827
1959
        ER_FILE_USED : ER_CANT_OPEN_FILE;
1828
 
 
1829
1960
      my_error(err_no, errortype, normalized_path.str, db_errno);
1830
1961
      break;
1831
1962
    }
1861
1992
  return;
1862
1993
} /* open_table_error */
1863
1994
 
1864
 
Field *TableShare::make_field(const message::Table::Field &pfield,
1865
 
                              unsigned char *ptr,
 
1995
Field *TableShare::make_field(unsigned char *ptr,
1866
1996
                              uint32_t field_length,
1867
1997
                              bool is_nullable,
1868
1998
                              unsigned char *null_pos,
1874
2004
                              TYPELIB *interval,
1875
2005
                              const char *field_name)
1876
2006
{
1877
 
  return make_field(pfield,
1878
 
                    ptr,
1879
 
                    field_length,
1880
 
                    is_nullable,
1881
 
                    null_pos,
1882
 
                    null_bit,
1883
 
                    decimals,
1884
 
                    field_type,
1885
 
                    field_charset,
1886
 
                    unireg_check,
1887
 
                    interval,
1888
 
                    field_name,
1889
 
                    pfield.constraints().is_unsigned());
1890
 
}
1891
 
 
1892
 
Field *TableShare::make_field(const message::Table::Field &,
1893
 
                              unsigned char *ptr,
1894
 
                              uint32_t field_length,
1895
 
                              bool is_nullable,
1896
 
                              unsigned char *null_pos,
1897
 
                              unsigned char null_bit,
1898
 
                              uint8_t decimals,
1899
 
                              enum_field_types field_type,
1900
 
                              const CHARSET_INFO * field_charset,
1901
 
                              Field::utype unireg_check,
1902
 
                              TYPELIB *interval,
1903
 
                              const char *field_name, 
1904
 
                              bool is_unsigned)
1905
 
{
1906
2007
  if (! is_nullable)
1907
2008
  {
1908
2009
    null_pos=0;
1917
2018
  {
1918
2019
  case DRIZZLE_TYPE_DATE:
1919
2020
  case DRIZZLE_TYPE_DATETIME:
1920
 
  case DRIZZLE_TYPE_UUID:
 
2021
  case DRIZZLE_TYPE_TIMESTAMP:
1921
2022
    field_charset= &my_charset_bin;
1922
2023
  default: break;
1923
2024
  }
1926
2027
  {
1927
2028
  case DRIZZLE_TYPE_ENUM:
1928
2029
    return new (&mem_root) Field_enum(ptr,
1929
 
                                      field_length,
1930
 
                                      null_pos,
1931
 
                                      null_bit,
1932
 
                                      field_name,
1933
 
                                      interval,
1934
 
                                      field_charset);
 
2030
                                 field_length,
 
2031
                                 null_pos,
 
2032
                                 null_bit,
 
2033
                                 field_name,
 
2034
                                 interval,
 
2035
                                 field_charset);
1935
2036
  case DRIZZLE_TYPE_VARCHAR:
1936
2037
    setVariableWidth();
1937
2038
    return new (&mem_root) Field_varstring(ptr,field_length,
1941
2042
                                      field_charset);
1942
2043
  case DRIZZLE_TYPE_BLOB:
1943
2044
    return new (&mem_root) Field_blob(ptr,
1944
 
                                      null_pos,
1945
 
                                      null_bit,
1946
 
                                      field_name,
1947
 
                                      this,
1948
 
                                      field_charset);
 
2045
                                 null_pos,
 
2046
                                 null_bit,
 
2047
                                 field_name,
 
2048
                                 this,
 
2049
                                 field_charset);
1949
2050
  case DRIZZLE_TYPE_DECIMAL:
1950
2051
    return new (&mem_root) Field_decimal(ptr,
1951
 
                                         field_length,
1952
 
                                         null_pos,
1953
 
                                         null_bit,
1954
 
                                         unireg_check,
1955
 
                                         field_name,
1956
 
                                         decimals);
 
2052
                                    field_length,
 
2053
                                    null_pos,
 
2054
                                    null_bit,
 
2055
                                    unireg_check,
 
2056
                                    field_name,
 
2057
                                    decimals,
 
2058
                                    false,
 
2059
                                    false /* is_unsigned */);
1957
2060
  case DRIZZLE_TYPE_DOUBLE:
1958
2061
    return new (&mem_root) Field_double(ptr,
1959
2062
                                   field_length,
1964
2067
                                   decimals,
1965
2068
                                   false,
1966
2069
                                   false /* is_unsigned */);
1967
 
  case DRIZZLE_TYPE_UUID:
1968
 
    return new (&mem_root) field::Uuid(ptr,
1969
 
                                       field_length,
1970
 
                                       null_pos,
1971
 
                                       null_bit,
1972
 
                                       field_name);
1973
 
  case DRIZZLE_TYPE_BOOLEAN:
1974
 
    return new (&mem_root) field::Boolean(ptr,
1975
 
                                          field_length,
1976
 
                                          null_pos,
1977
 
                                          null_bit,
1978
 
                                          field_name,
1979
 
                                          is_unsigned);
1980
2070
  case DRIZZLE_TYPE_LONG:
1981
 
    return new (&mem_root) field::Int32(ptr,
1982
 
                                        field_length,
1983
 
                                        null_pos,
1984
 
                                        null_bit,
1985
 
                                        unireg_check,
1986
 
                                        field_name);
 
2071
    return new (&mem_root) Field_long(ptr,
 
2072
                                 field_length,
 
2073
                                 null_pos,
 
2074
                                 null_bit,
 
2075
                                 unireg_check,
 
2076
                                 field_name,
 
2077
                                 false,
 
2078
                                 false /* is_unsigned */);
1987
2079
  case DRIZZLE_TYPE_LONGLONG:
1988
 
    {
1989
 
      if (is_unsigned)
1990
 
      {
1991
 
        return new (&mem_root) field::Size(ptr,
1992
 
                                           field_length,
1993
 
                                           null_pos,
1994
 
                                           null_bit,
1995
 
                                           unireg_check,
1996
 
                                           field_name);
1997
 
      }
1998
 
 
1999
 
      return new (&mem_root) field::Int64(ptr,
2000
 
                                          field_length,
2001
 
                                          null_pos,
2002
 
                                          null_bit,
2003
 
                                          unireg_check,
2004
 
                                          field_name);
2005
 
    }
2006
 
  case DRIZZLE_TYPE_MICROTIME:
2007
 
    return new (&mem_root) field::Microtime(ptr,
2008
 
                                            null_pos,
2009
 
                                            null_bit,
2010
 
                                            unireg_check,
2011
 
                                            field_name,
2012
 
                                            this);
 
2080
    return new (&mem_root) Field_int64_t(ptr,
 
2081
                                    field_length,
 
2082
                                    null_pos,
 
2083
                                    null_bit,
 
2084
                                    unireg_check,
 
2085
                                    field_name,
 
2086
                                    false,
 
2087
                                    false /* is_unsigned */);
2013
2088
  case DRIZZLE_TYPE_TIMESTAMP:
2014
 
    return new (&mem_root) field::Epoch(ptr,
2015
 
                                        null_pos,
2016
 
                                        null_bit,
2017
 
                                        unireg_check,
2018
 
                                        field_name,
2019
 
                                        this);
2020
 
  case DRIZZLE_TYPE_TIME:
2021
 
    return new (&mem_root) field::Time(ptr,
2022
 
                                       field_length,
2023
 
                                       null_pos,
2024
 
                                       null_bit,
2025
 
                                       field_name,
2026
 
                                       field_charset);
 
2089
    return new (&mem_root) Field_timestamp(ptr,
 
2090
                                      field_length,
 
2091
                                      null_pos,
 
2092
                                      null_bit,
 
2093
                                      unireg_check,
 
2094
                                      field_name,
 
2095
                                      this,
 
2096
                                      field_charset);
2027
2097
  case DRIZZLE_TYPE_DATE:
2028
2098
    return new (&mem_root) Field_date(ptr,
2029
2099
                                 null_pos,
2041
2111
                                 field_length,
2042
2112
                                 field_name,
2043
2113
                                 field_charset);
 
2114
  default: // Impossible (Wrong version)
 
2115
    break;
2044
2116
  }
2045
 
  assert(0);
2046
 
  abort();
2047
 
}
2048
 
 
2049
 
void TableShare::refreshVersion()
2050
 
{
2051
 
  version= refresh_version;
 
2117
  return 0;
2052
2118
}
2053
2119
 
2054
2120