~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
1
/* -*- mode: c++; c-basic-offset: 2; i/dent-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
 
5
 *  Copyright (C) 2009 Sun Microsystems, Inc.
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"
46
45
 
47
46
#include "drizzled/table.h"
48
47
#include "drizzled/table/shell.h"
64
63
#include "drizzled/field/str.h"
65
64
#include "drizzled/field/num.h"
66
65
#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/long.h"
74
 
#include "drizzled/field/int64_t.h"
 
73
#include "drizzled/field/int32.h"
 
74
#include "drizzled/field/int64.h"
 
75
#include "drizzled/field/size.h"
75
76
#include "drizzled/field/num.h"
76
 
#include "drizzled/field/timestamp.h"
 
77
#include "drizzled/field/time.h"
 
78
#include "drizzled/field/epoch.h"
77
79
#include "drizzled/field/datetime.h"
 
80
#include "drizzled/field/microtime.h"
78
81
#include "drizzled/field/varstring.h"
 
82
#include "drizzled/field/uuid.h"
 
83
 
 
84
#include "drizzled/plugin/storage_engine.h"
 
85
 
 
86
#include "drizzled/definition/cache.h"
 
87
 
 
88
#include <drizzled/refresh_version.h>
79
89
 
80
90
using namespace std;
81
91
 
84
94
 
85
95
extern size_t table_def_size;
86
96
 
87
 
/*****************************************************************************
88
 
  Functions to handle table definition cach (TableShare)
89
 
 *****************************************************************************/
90
 
 
91
 
/*
92
 
  Mark that we are not using table share anymore.
93
 
 
94
 
  SYNOPSIS
95
 
  release()
96
 
  share         Table share
97
 
 
98
 
  IMPLEMENTATION
99
 
  If ref_count goes to zero and (we have done a refresh or if we have
100
 
  already too many open table shares) then delete the definition.
101
 
*/
102
 
 
103
 
void TableShare::release(TableShare *share)
104
 
{
105
 
  bool to_be_deleted= false;
106
 
  safe_mutex_assert_owner(LOCK_open.native_handle);
107
 
 
108
 
  share->lock();
109
 
  if (!--share->ref_count)
110
 
  {
111
 
    to_be_deleted= true;
112
 
  }
113
 
 
114
 
  if (to_be_deleted)
115
 
  {
116
 
    TableIdentifier identifier(share->getSchemaName(), share->getTableName());
117
 
   
118
 
    definition::Cache::singleton().erase(identifier);
119
 
    return;
120
 
  }
121
 
  share->unlock();
122
 
}
123
 
 
124
 
void TableShare::release(TableSharePtr &share)
125
 
{
126
 
  bool to_be_deleted= false;
127
 
  safe_mutex_assert_owner(LOCK_open.native_handle);
128
 
 
129
 
  share->lock();
130
 
  if (!--share->ref_count)
131
 
  {
132
 
    to_be_deleted= true;
133
 
  }
134
 
 
135
 
  if (to_be_deleted)
136
 
  {
137
 
    TableIdentifier identifier(share->getSchemaName(), share->getTableName());
138
 
   
139
 
    definition::Cache::singleton().erase(identifier);
140
 
    return;
141
 
  }
142
 
  share->unlock();
143
 
}
144
 
 
145
 
void TableShare::release(TableIdentifier &identifier)
146
 
{
147
 
  TableSharePtr share= definition::Cache::singleton().find(identifier);
148
 
  if (share)
149
 
  {
150
 
    share->version= 0;                          // Mark for delete
151
 
    if (share->ref_count == 0)
152
 
    {
153
 
      share->lock();
154
 
      definition::Cache::singleton().erase(identifier);
155
 
    }
156
 
  }
157
 
}
158
 
 
159
 
 
160
 
static TableSharePtr foundTableShare(TableSharePtr share)
161
 
{
162
 
  /*
163
 
    We found an existing table definition. Return it if we didn't get
164
 
    an error when reading the table definition from file.
165
 
  */
166
 
 
167
 
  /* We must do a lock to ensure that the structure is initialized */
168
 
  if (share->error)
169
 
  {
170
 
    /* Table definition contained an error */
171
 
    share->open_table_error(share->error, share->open_errno, share->errarg);
172
 
 
173
 
    return TableSharePtr();
174
 
  }
175
 
 
176
 
  share->incrementTableCount();
177
 
 
178
 
  return share;
179
 
}
180
 
 
181
 
/*
182
 
  Get TableShare for a table.
183
 
 
184
 
  get_table_share()
185
 
  session                       Thread handle
186
 
  table_list            Table that should be opened
187
 
  key                   Table cache key
188
 
  key_length            Length of key
189
 
  error                 out: Error code from open_table_def()
190
 
 
191
 
  IMPLEMENTATION
192
 
  Get a table definition from the table definition cache.
193
 
  If it doesn't exist, create a new from the table definition file.
194
 
 
195
 
  NOTES
196
 
  We must have wrlock on LOCK_open when we come here
197
 
  (To be changed later)
198
 
 
199
 
  RETURN
200
 
  0  Error
201
 
#  Share for table
202
 
*/
203
 
 
204
 
TableSharePtr TableShare::getShareCreate(Session *session, 
205
 
                                         TableIdentifier &identifier,
206
 
                                         int &in_error)
207
 
{
208
 
  TableSharePtr share;
209
 
 
210
 
  in_error= 0;
211
 
 
212
 
  /* Read table definition from cache */
213
 
  if ((share= definition::Cache::singleton().find(identifier)))
214
 
    return foundTableShare(share);
215
 
 
216
 
  share.reset(new TableShare(message::Table::STANDARD, identifier));
217
 
  
218
 
  /*
219
 
    Lock mutex to be able to read table definition from file without
220
 
    conflicts
221
 
  */
222
 
  share->lock();
223
 
 
224
 
  bool ret= definition::Cache::singleton().insert(identifier, share);
225
 
 
226
 
  if (not ret)
227
 
    return TableSharePtr();
228
 
 
229
 
  if (share->open_table_def(*session, identifier))
230
 
  {
231
 
    in_error= share->error;
232
 
    definition::Cache::singleton().erase(identifier);
233
 
 
234
 
    return TableSharePtr();
235
 
  }
236
 
  share->ref_count++;                           // Mark in use
237
 
  
238
 
  plugin::EventObserver::registerTableEvents(*share);
239
 
  
240
 
  share->unlock();
241
 
 
242
 
  return share;
243
 
}
244
 
 
245
 
 
246
 
/*
247
 
  Check if table definition exits in cache
248
 
 
249
 
  SYNOPSIS
250
 
  get_cached_table_share()
251
 
  db                    Database name
252
 
  table_name            Table name
253
 
 
254
 
  RETURN
255
 
  0  Not cached
256
 
#  TableShare for table
257
 
*/
258
 
TableSharePtr TableShare::getShare(TableIdentifier &identifier)
259
 
{
260
 
  safe_mutex_assert_owner(LOCK_open.native_handle);
261
 
 
262
 
  return definition::Cache::singleton().find(identifier);
263
 
}
264
 
 
265
 
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
266
 
{
267
 
  enum_field_types field_type;
268
 
 
269
 
  switch(proto_field_type)
 
97
 
 
98
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
 
99
{
 
100
  switch(field.type())
270
101
  {
271
102
  case message::Table::Field::INTEGER:
272
 
    field_type= DRIZZLE_TYPE_LONG;
273
 
    break;
 
103
    return DRIZZLE_TYPE_LONG;
 
104
 
274
105
  case message::Table::Field::DOUBLE:
275
 
    field_type= DRIZZLE_TYPE_DOUBLE;
276
 
    break;
277
 
  case message::Table::Field::TIMESTAMP:
278
 
    field_type= DRIZZLE_TYPE_TIMESTAMP;
279
 
    break;
 
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
 
280
114
  case message::Table::Field::BIGINT:
281
 
    field_type= DRIZZLE_TYPE_LONGLONG;
282
 
    break;
 
115
    return DRIZZLE_TYPE_LONGLONG;
 
116
 
283
117
  case message::Table::Field::DATETIME:
284
 
    field_type= DRIZZLE_TYPE_DATETIME;
285
 
    break;
 
118
    return DRIZZLE_TYPE_DATETIME;
 
119
 
286
120
  case message::Table::Field::DATE:
287
 
    field_type= DRIZZLE_TYPE_DATE;
288
 
    break;
 
121
    return DRIZZLE_TYPE_DATE;
 
122
 
289
123
  case message::Table::Field::VARCHAR:
290
 
    field_type= DRIZZLE_TYPE_VARCHAR;
291
 
    break;
 
124
    return DRIZZLE_TYPE_VARCHAR;
 
125
 
292
126
  case message::Table::Field::DECIMAL:
293
 
    field_type= DRIZZLE_TYPE_DECIMAL;
294
 
    break;
 
127
    return DRIZZLE_TYPE_DECIMAL;
 
128
 
295
129
  case message::Table::Field::ENUM:
296
 
    field_type= DRIZZLE_TYPE_ENUM;
297
 
    break;
 
130
    return DRIZZLE_TYPE_ENUM;
 
131
 
298
132
  case message::Table::Field::BLOB:
299
 
    field_type= DRIZZLE_TYPE_BLOB;
300
 
    break;
301
 
  default:
302
 
    field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
303
 
    assert(1);
 
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;
304
143
  }
305
144
 
306
 
  return field_type;
 
145
  abort();
307
146
}
308
147
 
309
148
static Item *default_value_item(enum_field_types field_type,
334
173
                                 default_value->length());
335
174
    break;
336
175
  case DRIZZLE_TYPE_NULL:
337
 
    assert(false);
 
176
    assert(0);
 
177
    abort();
338
178
  case DRIZZLE_TYPE_TIMESTAMP:
339
179
  case DRIZZLE_TYPE_DATETIME:
 
180
  case DRIZZLE_TYPE_TIME:
340
181
  case DRIZZLE_TYPE_DATE:
341
182
  case DRIZZLE_TYPE_ENUM:
 
183
  case DRIZZLE_TYPE_UUID:
 
184
  case DRIZZLE_TYPE_MICROTIME:
 
185
  case DRIZZLE_TYPE_BOOLEAN:
342
186
    default_item= new Item_string(default_value->c_str(),
343
187
                                  default_value->length(),
344
188
                                  system_charset_info);
377
221
 */
378
222
bool TableShare::fieldInPrimaryKey(Field *in_field) const
379
223
{
380
 
  assert(table_proto != NULL);
 
224
  assert(getTableMessage());
381
225
 
382
 
  size_t num_indexes= table_proto->indexes_size();
 
226
  size_t num_indexes= getTableMessage()->indexes_size();
383
227
 
384
228
  for (size_t x= 0; x < num_indexes; ++x)
385
229
  {
386
 
    const message::Table::Index &index= table_proto->indexes(x);
 
230
    const message::Table::Index &index= getTableMessage()->indexes(x);
387
231
    if (index.is_primary())
388
232
    {
389
233
      size_t num_parts= index.index_part_size();
390
234
      for (size_t y= 0; y < num_parts; ++y)
391
235
      {
392
 
        if (index.index_part(y).fieldnr() == in_field->field_index)
 
236
        if (index.index_part(y).fieldnr() == in_field->position())
393
237
          return true;
394
238
      }
395
239
    }
397
241
  return false;
398
242
}
399
243
 
400
 
TableShare::TableShare(TableIdentifier::Type type_arg) :
 
244
TableShare::TableShare(const identifier::Table::Type type_arg) :
401
245
  table_category(TABLE_UNKNOWN_CATEGORY),
402
246
  found_next_number_field(NULL),
403
247
  timestamp_field(NULL),
404
248
  key_info(NULL),
405
249
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
406
250
  all_set(),
 
251
  db(NULL_LEX_STRING),
 
252
  table_name(NULL_LEX_STRING),
 
253
  path(NULL_LEX_STRING),
 
254
  normalized_path(NULL_LEX_STRING),
407
255
  block_size(0),
408
256
  version(0),
409
257
  timestamp_offset(0),
410
258
  reclength(0),
411
259
  stored_rec_length(0),
412
260
  max_rows(0),
413
 
  table_proto(NULL),
 
261
  _table_message(NULL),
414
262
  storage_engine(NULL),
415
263
  tmp_table(type_arg),
416
 
  ref_count(0),
 
264
  _ref_count(0),
417
265
  null_bytes(0),
418
266
  last_null_bit_pos(0),
419
 
  fields(0),
 
267
  _field_size(0),
420
268
  rec_buff_length(0),
421
269
  keys(0),
422
270
  key_parts(0),
426
274
  uniques(0),
427
275
  null_fields(0),
428
276
  blob_fields(0),
429
 
  timestamp_field_offset(0),
430
277
  has_variable_width(false),
431
278
  db_create_options(0),
432
279
  db_options_in_use(0),
439
286
  error(0),
440
287
  open_errno(0),
441
288
  errarg(0),
442
 
  blob_ptr_size(0),
 
289
  blob_ptr_size(portable_sizeof_char_ptr),
443
290
  db_low_byte_first(false),
444
291
  keys_in_use(0),
445
 
  keys_for_keyread(0),
446
 
  event_observers(NULL)
 
292
  keys_for_keyread(0)
447
293
{
448
 
 
449
 
  table_charset= 0;
450
 
  memset(&db, 0, sizeof(LEX_STRING));
451
 
  memset(&table_name, 0, sizeof(LEX_STRING));
452
 
  memset(&path, 0, sizeof(LEX_STRING));
453
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
454
 
 
455
294
  if (type_arg == message::Table::INTERNAL)
456
295
  {
457
 
    TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
 
296
    identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
458
297
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
459
298
  }
460
299
  else
463
302
  }
464
303
}
465
304
 
466
 
TableShare::TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
 
305
TableShare::TableShare(const identifier::Table &identifier, const identifier::Table::Key &key) :// Used by placeholder
467
306
  table_category(TABLE_UNKNOWN_CATEGORY),
468
307
  found_next_number_field(NULL),
469
308
  timestamp_field(NULL),
470
309
  key_info(NULL),
471
310
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
311
  table_charset(0),
472
312
  all_set(),
 
313
  db(NULL_LEX_STRING),
 
314
  table_name(NULL_LEX_STRING),
 
315
  path(NULL_LEX_STRING),
 
316
  normalized_path(NULL_LEX_STRING),
473
317
  block_size(0),
474
318
  version(0),
475
319
  timestamp_offset(0),
476
320
  reclength(0),
477
321
  stored_rec_length(0),
478
322
  max_rows(0),
479
 
  table_proto(NULL),
 
323
  _table_message(NULL),
480
324
  storage_engine(NULL),
481
325
  tmp_table(message::Table::INTERNAL),
482
 
  ref_count(0),
 
326
  _ref_count(0),
483
327
  null_bytes(0),
484
328
  last_null_bit_pos(0),
485
 
  fields(0),
 
329
  _field_size(0),
486
330
  rec_buff_length(0),
487
331
  keys(0),
488
332
  key_parts(0),
492
336
  uniques(0),
493
337
  null_fields(0),
494
338
  blob_fields(0),
495
 
  timestamp_field_offset(0),
496
339
  has_variable_width(false),
497
340
  db_create_options(0),
498
341
  db_options_in_use(0),
505
348
  error(0),
506
349
  open_errno(0),
507
350
  errarg(0),
508
 
  blob_ptr_size(0),
 
351
  blob_ptr_size(portable_sizeof_char_ptr),
509
352
  db_low_byte_first(false),
510
353
  keys_in_use(0),
511
 
  keys_for_keyread(0),
512
 
  event_observers(NULL)
 
354
  keys_for_keyread(0)
513
355
{
514
356
  assert(identifier.getKey() == key);
515
357
 
516
 
  table_charset= 0;
517
 
  memset(&path, 0, sizeof(LEX_STRING));
518
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
519
 
 
520
358
  private_key_for_cache= key;
521
359
 
522
360
  table_category=         TABLE_CATEGORY_TEMPORARY;
539
377
}
540
378
 
541
379
 
542
 
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
 
380
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
543
381
  table_category(TABLE_UNKNOWN_CATEGORY),
544
382
  found_next_number_field(NULL),
545
383
  timestamp_field(NULL),
546
384
  key_info(NULL),
547
385
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
386
  table_charset(0),
548
387
  all_set(),
 
388
  db(NULL_LEX_STRING),
 
389
  table_name(NULL_LEX_STRING),
 
390
  path(NULL_LEX_STRING),
 
391
  normalized_path(NULL_LEX_STRING),
549
392
  block_size(0),
550
393
  version(0),
551
394
  timestamp_offset(0),
552
395
  reclength(0),
553
396
  stored_rec_length(0),
554
397
  max_rows(0),
555
 
  table_proto(NULL),
 
398
  _table_message(NULL),
556
399
  storage_engine(NULL),
557
400
  tmp_table(identifier.getType()),
558
 
  ref_count(0),
 
401
  _ref_count(0),
559
402
  null_bytes(0),
560
403
  last_null_bit_pos(0),
561
 
  fields(0),
 
404
  _field_size(0),
562
405
  rec_buff_length(0),
563
406
  keys(0),
564
407
  key_parts(0),
568
411
  uniques(0),
569
412
  null_fields(0),
570
413
  blob_fields(0),
571
 
  timestamp_field_offset(0),
572
414
  has_variable_width(false),
573
415
  db_create_options(0),
574
416
  db_options_in_use(0),
581
423
  error(0),
582
424
  open_errno(0),
583
425
  errarg(0),
584
 
  blob_ptr_size(0),
 
426
  blob_ptr_size(portable_sizeof_char_ptr),
585
427
  db_low_byte_first(false),
586
428
  keys_in_use(0),
587
 
  keys_for_keyread(0),
588
 
  event_observers(NULL)
 
429
  keys_for_keyread(0)
589
430
{
590
 
  table_charset= 0;
591
 
  memset(&db, 0, sizeof(LEX_STRING));
592
 
  memset(&table_name, 0, sizeof(LEX_STRING));
593
 
  memset(&path, 0, sizeof(LEX_STRING));
594
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
595
 
 
596
431
  private_key_for_cache= identifier.getKey();
597
432
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
598
433
  private_normalized_path.resize(identifier.getPath().size() + 1);
615
450
/*
616
451
  Used for shares that will go into the cache.
617
452
*/
618
 
TableShare::TableShare(TableIdentifier::Type type_arg,
619
 
                       TableIdentifier &identifier,
 
453
TableShare::TableShare(const identifier::Table::Type type_arg,
 
454
                       const identifier::Table &identifier,
620
455
                       char *path_arg,
621
456
                       uint32_t path_length_arg) :
622
457
  table_category(TABLE_UNKNOWN_CATEGORY),
624
459
  timestamp_field(NULL),
625
460
  key_info(NULL),
626
461
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
462
  table_charset(0),
627
463
  all_set(),
 
464
  db(NULL_LEX_STRING),
 
465
  table_name(NULL_LEX_STRING),
 
466
  path(NULL_LEX_STRING),
 
467
  normalized_path(NULL_LEX_STRING),
628
468
  block_size(0),
629
469
  version(0),
630
470
  timestamp_offset(0),
631
471
  reclength(0),
632
472
  stored_rec_length(0),
633
473
  max_rows(0),
634
 
  table_proto(NULL),
 
474
  _table_message(NULL),
635
475
  storage_engine(NULL),
636
476
  tmp_table(type_arg),
637
 
  ref_count(0),
 
477
  _ref_count(0),
638
478
  null_bytes(0),
639
479
  last_null_bit_pos(0),
640
 
  fields(0),
 
480
  _field_size(0),
641
481
  rec_buff_length(0),
642
482
  keys(0),
643
483
  key_parts(0),
647
487
  uniques(0),
648
488
  null_fields(0),
649
489
  blob_fields(0),
650
 
  timestamp_field_offset(0),
651
490
  has_variable_width(false),
652
491
  db_create_options(0),
653
492
  db_options_in_use(0),
660
499
  error(0),
661
500
  open_errno(0),
662
501
  errarg(0),
663
 
  blob_ptr_size(0),
 
502
  blob_ptr_size(portable_sizeof_char_ptr),
664
503
  db_low_byte_first(false),
665
504
  keys_in_use(0),
666
 
  keys_for_keyread(0),
667
 
  event_observers(NULL)
 
505
  keys_for_keyread(0)
668
506
{
669
 
  table_charset= 0;
670
 
  memset(&db, 0, sizeof(LEX_STRING));
671
 
  memset(&table_name, 0, sizeof(LEX_STRING));
672
 
  memset(&path, 0, sizeof(LEX_STRING));
673
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
674
 
 
675
507
  char *path_buff;
676
508
  std::string _path;
677
509
 
691
523
  }
692
524
  else
693
525
  {
694
 
    TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
 
526
    identifier::Table::build_table_filename(_path, db.str, table_name.str, false);
695
527
  }
696
528
 
697
529
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
705
537
  else
706
538
  {
707
539
    assert(0); // We should throw here.
 
540
    abort();
708
541
  }
709
542
}
710
543
 
725
558
 
726
559
TableShare::~TableShare() 
727
560
{
728
 
  assert(ref_count == 0);
729
 
 
730
 
  /*
731
 
    If someone is waiting for this to be deleted, inform it about this.
732
 
    Don't do a delete until we know that no one is refering to this anymore.
733
 
  */
734
 
  if (tmp_table == message::Table::STANDARD)
735
 
  {
736
 
    /* No thread refers to this anymore */
737
 
    mutex.unlock();
738
 
  }
739
 
 
740
561
  storage_engine= NULL;
741
562
 
742
 
  delete table_proto;
743
 
  table_proto= NULL;
744
 
 
745
 
  plugin::EventObserver::deregisterTableEvents(*this);
746
 
 
747
563
  mem_root.free_root(MYF(0));                 // Free's share
748
564
}
749
565
 
750
 
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
 
566
void TableShare::setIdentifier(const identifier::Table &identifier_arg)
751
567
{
752
568
  private_key_for_cache= identifier_arg.getKey();
753
569
 
760
576
  table_name.str=    db.str + db.length + 1;
761
577
  table_name.length= strlen(table_name.str);
762
578
 
763
 
  table_proto->set_name(identifier_arg.getTableName());
764
 
  table_proto->set_schema(identifier_arg.getSchemaName());
 
579
  getTableMessage()->set_name(identifier_arg.getTableName());
 
580
  getTableMessage()->set_schema(identifier_arg.getSchemaName());
765
581
}
766
582
 
767
 
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
 
583
bool TableShare::parse_table_proto(Session& session, message::Table &table)
768
584
{
769
 
  int local_error= 0;
 
585
  drizzled::error_t local_error= EE_OK;
770
586
 
771
587
  if (! table.IsInitialized())
772
588
  {
773
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
 
589
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
590
             table.name().empty() ? " " :  table.name().c_str(),
 
591
             table.InitializationErrorString().c_str());
 
592
 
774
593
    return ER_CORRUPT_TABLE_DEFINITION;
775
594
  }
776
595
 
777
 
  setTableProto(new(nothrow) message::Table(table));
 
596
  setTableMessage(table);
778
597
 
779
598
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
780
599
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
800
619
 
801
620
  table_charset= get_charset(table_options.collation_id());
802
621
 
803
 
  if (! table_charset)
 
622
  if (not table_charset)
804
623
  {
805
 
    char errmsg[100];
806
 
    snprintf(errmsg, sizeof(errmsg),
807
 
             _("Table %s has invalid/unknown collation: %d,%s"),
808
 
             getPath(),
809
 
             table_options.collation_id(),
810
 
             table_options.collation().c_str());
811
 
    errmsg[99]='\0';
 
624
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
 
625
             table_options.collation().c_str(),
 
626
             table.name().c_str());
812
627
 
813
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
814
 
    return ER_CORRUPT_TABLE_DEFINITION;
 
628
    return ER_CORRUPT_TABLE_DEFINITION; // Historical
815
629
  }
816
630
 
817
631
  db_record_offset= 1;
818
632
 
819
 
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
820
 
 
821
633
  keys= table.indexes_size();
822
634
 
823
635
  key_parts= 0;
967
779
  keys_for_keyread.reset();
968
780
  set_prefix(keys_in_use, keys);
969
781
 
970
 
  fields= table.field_size();
 
782
  _field_size= table.field_size();
971
783
 
972
 
  setFields(fields + 1);
973
 
  field[fields]= NULL;
 
784
  setFields(_field_size + 1);
 
785
  _fields[_field_size]= NULL;
974
786
 
975
787
  uint32_t local_null_fields= 0;
976
788
  reclength= 0;
977
789
 
978
 
  vector<uint32_t> field_offsets;
979
 
  vector<uint32_t> field_pack_length;
 
790
  std::vector<uint32_t> field_offsets;
 
791
  std::vector<uint32_t> field_pack_length;
980
792
 
981
 
  field_offsets.resize(fields);
982
 
  field_pack_length.resize(fields);
 
793
  field_offsets.resize(_field_size);
 
794
  field_pack_length.resize(_field_size);
983
795
 
984
796
  uint32_t interval_count= 0;
985
797
  uint32_t interval_parts= 0;
986
798
 
987
799
  uint32_t stored_columns_reclength= 0;
988
800
 
989
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
801
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
990
802
  {
991
803
    message::Table::Field pfield= table.field(fieldnr);
992
 
    if (pfield.constraints().is_nullable())
993
 
      local_null_fields++;
 
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
    }
994
812
 
995
 
    enum_field_types drizzle_field_type=
996
 
      proto_field_type_to_drizzle_type(pfield.type());
 
813
    enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
997
814
 
998
815
    field_offsets[fieldnr]= stored_columns_reclength;
999
816
 
1032
849
      {
1033
850
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1034
851
 
1035
 
        field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
 
852
        field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
1036
853
      }
1037
854
      break;
1038
855
    default:
1082
899
 
1083
900
  uint32_t interval_nr= 0;
1084
901
 
1085
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
902
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1086
903
  {
1087
904
    message::Table::Field pfield= table.field(fieldnr);
1088
905
 
1094
911
 
1095
912
    if (field_options.field_value_size() > Field_enum::max_supported_elements)
1096
913
    {
1097
 
      char errmsg[100];
1098
 
      snprintf(errmsg, sizeof(errmsg),
1099
 
               _("ENUM column %s has greater than %d possible values"),
1100
 
               pfield.name().c_str(),
1101
 
               Field_enum::max_supported_elements);
1102
 
      errmsg[99]='\0';
 
914
      my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
1103
915
 
1104
 
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
1105
 
      return ER_CORRUPT_TABLE_DEFINITION;
 
916
      return ER_CORRUPT_TABLE_DEFINITION_ENUM; // Historical
1106
917
    }
1107
918
 
1108
919
 
1145
956
  /* and read the fields */
1146
957
  interval_nr= 0;
1147
958
 
1148
 
  bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
 
959
  bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
1149
960
 
1150
961
  unsigned char* null_pos= getDefaultValues();
1151
962
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
1152
963
 
1153
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
964
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1154
965
  {
1155
966
    message::Table::Field pfield= table.field(fieldnr);
1156
967
 
1176
987
        unireg_type= Field::TIMESTAMP_DN_FIELD;
1177
988
      }
1178
989
      else
1179
 
        assert(1); // Invalid update value.
 
990
      {
 
991
        assert(0); // Invalid update value.
 
992
        abort();
 
993
      }
1180
994
    }
1181
995
    else if (pfield.has_options() &&
1182
996
             pfield.options().has_update_expression() &&
1202
1016
 
1203
1017
    enum_field_types field_type;
1204
1018
 
1205
 
    field_type= proto_field_type_to_drizzle_type(pfield.type());
 
1019
    field_type= proto_field_type_to_drizzle_type(pfield);
1206
1020
 
1207
1021
    const CHARSET_INFO *charset= &my_charset_bin;
1208
1022
 
1247
1061
      {
1248
1062
        if (fo.scale() > DECIMAL_MAX_SCALE)
1249
1063
        {
1250
 
          local_error= 4;
 
1064
          local_error= ER_NOT_FORM_FILE;
1251
1065
 
1252
 
          return local_error;
 
1066
          return true;
1253
1067
        }
1254
1068
        decimals= static_cast<uint8_t>(fo.scale());
1255
1069
      }
1269
1083
    }
1270
1084
 
1271
1085
 
1272
 
    db_low_byte_first= true; //Cursor->low_byte_first();
1273
 
    blob_ptr_size= portable_sizeof_char_ptr;
1274
 
 
1275
1086
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1276
1087
 
 
1088
    // We set field_length in this loop.
1277
1089
    switch (field_type)
1278
1090
    {
1279
1091
    case DRIZZLE_TYPE_BLOB:
1305
1117
            decimals != NOT_FIXED_DEC)
1306
1118
        {
1307
1119
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1308
 
          local_error= 1;
1309
 
 
1310
 
          return local_error;
 
1120
          local_error= ER_M_BIGGER_THAN_D;
 
1121
          return true;
1311
1122
        }
1312
1123
        break;
1313
1124
      }
1315
1126
      {
1316
1127
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1317
1128
 
1318
 
        field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
 
1129
        field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
1319
1130
                                                     false);
1320
1131
        break;
1321
1132
      }
1322
 
    case DRIZZLE_TYPE_TIMESTAMP:
1323
1133
    case DRIZZLE_TYPE_DATETIME:
1324
1134
      field_length= DateTime::MAX_STRING_LENGTH;
1325
1135
      break;
1352
1162
      }
1353
1163
      break;
1354
1164
    case DRIZZLE_TYPE_LONGLONG:
1355
 
      field_length= MAX_BIGINT_WIDTH;
 
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();
1356
1184
      break;
1357
1185
    case DRIZZLE_TYPE_NULL:
1358
1186
      abort(); // Programming error
1359
1187
    }
1360
1188
 
1361
 
    Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
1362
 
                                field_length,
1363
 
                                pfield.constraints().is_nullable(),
1364
 
                                null_pos,
1365
 
                                null_bit_pos,
1366
 
                                decimals,
1367
 
                                field_type,
1368
 
                                charset,
1369
 
                                MTYP_TYPENR(unireg_type),
1370
 
                                ((field_type == DRIZZLE_TYPE_ENUM) ?
1371
 
                                 &intervals[interval_nr++]
1372
 
                                 : (TYPELIB*) 0),
1373
 
                                getTableProto()->field(fieldnr).name().c_str());
1374
 
 
1375
 
    field[fieldnr]= f;
 
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
    }
1376
1235
 
1377
1236
    // This needs to go, we should be setting the "use" on the field so that
1378
1237
    // it does not reference the share/table.
1398
1257
      if (res != 0 && res != 3) /* @TODO Huh? */
1399
1258
      {
1400
1259
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1401
 
        local_error= 1;
 
1260
        local_error= ER_INVALID_DEFAULT;
1402
1261
 
1403
 
        return local_error;
 
1262
        return true;
1404
1263
      }
1405
1264
    }
1406
 
    else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
1407
 
             (f->flags & NOT_NULL_FLAG))
 
1265
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
1408
1266
    {
1409
1267
      f->set_notnull();
1410
1268
      f->store((int64_t) 1, true);
1418
1276
    f->setTable(NULL);
1419
1277
    f->orig_table= NULL;
1420
1278
 
1421
 
    f->field_index= fieldnr;
 
1279
    f->setPosition(fieldnr);
1422
1280
    f->comment= comment;
1423
 
    if (! default_value &&
1424
 
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
 
1281
    if (not default_value &&
 
1282
        not (f->unireg_check==Field::NEXT_NUMBER) &&
1425
1283
        (f->flags & NOT_NULL_FLAG) &&
1426
 
        (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
 
1284
        (not f->is_timestamp()))
1427
1285
    {
1428
1286
      f->flags|= NO_DEFAULT_VALUE_FLAG;
1429
1287
    }
1430
1288
 
1431
1289
    if (f->unireg_check == Field::NEXT_NUMBER)
1432
 
      found_next_number_field= &(field[fieldnr]);
1433
 
 
1434
 
    if (timestamp_field == f)
1435
 
      timestamp_field_offset= fieldnr;
 
1290
      found_next_number_field= &(_fields[fieldnr]);
1436
1291
 
1437
1292
    if (use_hash) /* supposedly this never fails... but comments lie */
1438
1293
    {
1439
 
      const char *local_field_name= field[fieldnr]->field_name;
1440
 
      name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
 
1294
      const char *local_field_name= _fields[fieldnr]->field_name;
 
1295
      name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
1441
1296
    }
1442
 
 
1443
1297
  }
1444
1298
 
1445
1299
  keyinfo= key_info;
1464
1318
    We need to set the unused bits to 1. If the number of bits is a multiple
1465
1319
    of 8 there are no unused bits.
1466
1320
  */
1467
 
 
1468
1321
  if (null_count & 7)
1469
1322
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1470
1323
 
1495
1348
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1496
1349
        {
1497
1350
          uint32_t fieldnr= key_part[i].fieldnr;
1498
 
          if (! fieldnr ||
1499
 
              field[fieldnr-1]->null_ptr ||
1500
 
              field[fieldnr-1]->key_length() != key_part[i].length)
 
1351
          if (not fieldnr ||
 
1352
              _fields[fieldnr-1]->null_ptr ||
 
1353
              _fields[fieldnr-1]->key_length() != key_part[i].length)
1501
1354
          {
1502
1355
            local_primary_key= MAX_KEY; // Can't be used
1503
1356
            break;
1512
1365
        {
1513
1366
          return ENOMEM;
1514
1367
        }
1515
 
        local_field= key_part->field= field[key_part->fieldnr-1];
 
1368
        local_field= key_part->field= _fields[key_part->fieldnr-1];
1516
1369
        key_part->type= local_field->key_type();
1517
1370
        if (local_field->null_ptr)
1518
1371
        {
1618
1471
                            &next_number_keypart)) < 0)
1619
1472
    {
1620
1473
      /* Wrong field definition */
1621
 
      local_error= 4;
 
1474
      local_error= ER_NOT_FORM_FILE;
1622
1475
 
1623
 
      return local_error;
 
1476
      return true;
1624
1477
    }
1625
1478
    else
1626
1479
    {
1634
1487
    blob_field.resize(blob_fields);
1635
1488
    uint32_t *save= &blob_field[0];
1636
1489
    uint32_t k= 0;
1637
 
    for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
 
1490
    for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
1638
1491
    {
1639
1492
      if ((*iter)->flags & BLOB_FLAG)
1640
1493
        (*save++)= k;
1641
1494
    }
1642
1495
  }
1643
1496
 
1644
 
  db_low_byte_first= true; // @todo Question this.
1645
1497
  all_set.clear();
1646
 
  all_set.resize(fields);
 
1498
  all_set.resize(_field_size);
1647
1499
  all_set.set();
1648
1500
 
1649
 
  return local_error;
1650
 
}
1651
 
 
1652
 
int TableShare::parse_table_proto(Session& session, message::Table &table)
1653
 
{
1654
 
  int local_error= inner_parse_table_proto(session, table);
1655
 
 
1656
 
  if (not local_error)
1657
 
    return 0;
1658
 
 
1659
 
  error= local_error;
1660
 
  open_errno= errno;
1661
 
  errarg= 0;
1662
 
  open_table_error(local_error, open_errno, 0);
1663
 
 
1664
 
  return local_error;
1665
 
}
1666
 
 
 
1501
  return local_error != EE_OK;
 
1502
}
1667
1503
 
1668
1504
/*
1669
1505
  Read table definition from a binary / text based .frm cursor
1689
1525
  6    Unknown .frm version
1690
1526
*/
1691
1527
 
1692
 
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
 
1528
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1693
1529
{
1694
 
  int local_error;
1695
 
  bool error_given;
1696
 
 
1697
 
  local_error= 1;
1698
 
  error_given= 0;
1699
 
 
1700
 
  message::TablePtr table;
1701
 
 
1702
 
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1703
 
 
1704
 
  if (local_error != EEXIST)
 
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())
1705
1535
  {
1706
 
    if (local_error > 0)
 
1536
    if (parse_table_proto(session, *table))
1707
1537
    {
1708
 
      errno= local_error;
1709
 
      local_error= 1;
 
1538
      local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
 
1539
      my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1710
1540
    }
1711
1541
    else
1712
1542
    {
1713
 
      if (not table->IsInitialized())
1714
 
      {
1715
 
        local_error= 4;
1716
 
      }
 
1543
      setTableCategory(TABLE_CATEGORY_USER);
 
1544
      local_error= EE_OK;
1717
1545
    }
1718
 
    goto err_not_open;
1719
 
  }
1720
 
 
1721
 
  local_error= parse_table_proto(session, *table);
1722
 
 
1723
 
  setTableCategory(TABLE_CATEGORY_USER);
1724
 
 
1725
 
err_not_open:
1726
 
  if (local_error && !error_given)
1727
 
  {
1728
 
    error= local_error;
1729
 
    open_table_error(error, (open_errno= errno), 0);
1730
 
  }
1731
 
 
1732
 
  return(error);
 
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);
1733
1559
}
1734
1560
 
1735
1561
 
1757
1583
  7    Table definition has changed in engine
1758
1584
*/
1759
1585
int TableShare::open_table_from_share(Session *session,
1760
 
                                      const TableIdentifier &identifier,
 
1586
                                      const identifier::Table &identifier,
1761
1587
                                      const char *alias,
1762
1588
                                      uint32_t db_stat, uint32_t ha_open_flags,
1763
1589
                                      Table &outparam)
1774
1600
  if (not error_reported)
1775
1601
    open_table_error(ret, errno, 0);
1776
1602
 
1777
 
  delete outparam.cursor;
 
1603
  boost::checked_delete(outparam.cursor);
1778
1604
  outparam.cursor= 0;                           // For easier error checking
1779
1605
  outparam.db_stat= 0;
1780
1606
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
1793
1619
  unsigned char *record= NULL;
1794
1620
  Field **field_ptr;
1795
1621
 
1796
 
  /* Parsing of partitioning information from .frm needs session->lex set up. */
1797
 
  assert(session->lex->is_lex_started);
1798
 
 
1799
1622
  local_error= 1;
1800
1623
  outparam.resetTable(session, this, db_stat);
1801
1624
 
1847
1670
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1848
1671
  }
1849
1672
 
1850
 
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
 
1673
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((_field_size+1)* sizeof(Field*)))))
1851
1674
  {
1852
1675
    return local_error;
1853
1676
  }
1859
1682
  outparam.null_flags= (unsigned char*) record+1;
1860
1683
 
1861
1684
  /* Setup copy of fields from share, but use the right alias and record */
1862
 
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
 
1685
  for (uint32_t i= 0 ; i < _field_size; i++, field_ptr++)
1863
1686
  {
1864
 
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
 
1687
    if (!((*field_ptr)= _fields[i]->clone(outparam.getMemRoot(), &outparam)))
1865
1688
      return local_error;
1866
1689
  }
1867
1690
  (*field_ptr)= 0;                              // End marker
1870
1693
    outparam.found_next_number_field=
1871
1694
      outparam.getField(positionFields(found_next_number_field));
1872
1695
  if (timestamp_field)
1873
 
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
1874
 
 
 
1696
    outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
1875
1697
 
1876
1698
  /* Fix key->name and key_part->field */
1877
1699
  if (key_parts)
1920
1742
 
1921
1743
  /* Allocate bitmaps */
1922
1744
 
1923
 
  outparam.def_read_set.resize(fields);
1924
 
  outparam.def_write_set.resize(fields);
1925
 
  outparam.tmp_set.resize(fields);
 
1745
  outparam.def_read_set.resize(_field_size);
 
1746
  outparam.def_write_set.resize(_field_size);
 
1747
  outparam.tmp_set.resize(_field_size);
1926
1748
  outparam.default_column_bitmaps();
1927
1749
 
1928
1750
  return 0;
1929
1751
}
1930
1752
 
1931
 
int TableShare::open_table_cursor_inner(const TableIdentifier &identifier,
 
1753
int TableShare::open_table_cursor_inner(const identifier::Table &identifier,
1932
1754
                                        uint32_t db_stat, uint32_t ha_open_flags,
1933
1755
                                        Table &outparam,
1934
1756
                                        bool &error_reported)
1979
1801
/* error message when opening a form cursor */
1980
1802
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1981
1803
{
1982
 
  int err_no;
1983
1804
  char buff[FN_REFLEN];
1984
1805
  myf errortype= ME_ERROR+ME_WAITTANG;
1985
1806
 
1988
1809
  case 1:
1989
1810
    if (db_errno == ENOENT)
1990
1811
    {
1991
 
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
 
1812
      identifier::Table identifier(db.str, table_name.str);
 
1813
      my_error(ER_TABLE_UNKNOWN, identifier);
1992
1814
    }
1993
1815
    else
1994
1816
    {
1999
1821
    break;
2000
1822
  case 2:
2001
1823
    {
 
1824
      drizzled::error_t err_no;
 
1825
 
2002
1826
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
2003
1827
        ER_FILE_USED : ER_CANT_OPEN_FILE;
 
1828
 
2004
1829
      my_error(err_no, errortype, normalized_path.str, db_errno);
2005
1830
      break;
2006
1831
    }
2036
1861
  return;
2037
1862
} /* open_table_error */
2038
1863
 
2039
 
Field *TableShare::make_field(unsigned char *ptr,
 
1864
Field *TableShare::make_field(const message::Table::Field &pfield,
 
1865
                              unsigned char *ptr,
2040
1866
                              uint32_t field_length,
2041
1867
                              bool is_nullable,
2042
1868
                              unsigned char *null_pos,
2048
1874
                              TYPELIB *interval,
2049
1875
                              const char *field_name)
2050
1876
{
 
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
{
2051
1906
  if (! is_nullable)
2052
1907
  {
2053
1908
    null_pos=0;
2062
1917
  {
2063
1918
  case DRIZZLE_TYPE_DATE:
2064
1919
  case DRIZZLE_TYPE_DATETIME:
2065
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
1920
  case DRIZZLE_TYPE_UUID:
2066
1921
    field_charset= &my_charset_bin;
2067
1922
  default: break;
2068
1923
  }
2071
1926
  {
2072
1927
  case DRIZZLE_TYPE_ENUM:
2073
1928
    return new (&mem_root) Field_enum(ptr,
2074
 
                                 field_length,
2075
 
                                 null_pos,
2076
 
                                 null_bit,
2077
 
                                 field_name,
2078
 
                                 interval,
2079
 
                                 field_charset);
 
1929
                                      field_length,
 
1930
                                      null_pos,
 
1931
                                      null_bit,
 
1932
                                      field_name,
 
1933
                                      interval,
 
1934
                                      field_charset);
2080
1935
  case DRIZZLE_TYPE_VARCHAR:
2081
1936
    setVariableWidth();
2082
1937
    return new (&mem_root) Field_varstring(ptr,field_length,
2086
1941
                                      field_charset);
2087
1942
  case DRIZZLE_TYPE_BLOB:
2088
1943
    return new (&mem_root) Field_blob(ptr,
2089
 
                                 null_pos,
2090
 
                                 null_bit,
2091
 
                                 field_name,
2092
 
                                 this,
2093
 
                                 field_charset);
 
1944
                                      null_pos,
 
1945
                                      null_bit,
 
1946
                                      field_name,
 
1947
                                      this,
 
1948
                                      field_charset);
2094
1949
  case DRIZZLE_TYPE_DECIMAL:
2095
1950
    return new (&mem_root) Field_decimal(ptr,
2096
 
                                    field_length,
2097
 
                                    null_pos,
2098
 
                                    null_bit,
2099
 
                                    unireg_check,
2100
 
                                    field_name,
2101
 
                                    decimals,
2102
 
                                    false,
2103
 
                                    false /* is_unsigned */);
 
1951
                                         field_length,
 
1952
                                         null_pos,
 
1953
                                         null_bit,
 
1954
                                         unireg_check,
 
1955
                                         field_name,
 
1956
                                         decimals);
2104
1957
  case DRIZZLE_TYPE_DOUBLE:
2105
1958
    return new (&mem_root) Field_double(ptr,
2106
1959
                                   field_length,
2111
1964
                                   decimals,
2112
1965
                                   false,
2113
1966
                                   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);
2114
1980
  case DRIZZLE_TYPE_LONG:
2115
 
    return new (&mem_root) Field_long(ptr,
2116
 
                                 field_length,
2117
 
                                 null_pos,
2118
 
                                 null_bit,
2119
 
                                 unireg_check,
2120
 
                                 field_name,
2121
 
                                 false,
2122
 
                                 false /* is_unsigned */);
 
1981
    return new (&mem_root) field::Int32(ptr,
 
1982
                                        field_length,
 
1983
                                        null_pos,
 
1984
                                        null_bit,
 
1985
                                        unireg_check,
 
1986
                                        field_name);
2123
1987
  case DRIZZLE_TYPE_LONGLONG:
2124
 
    return new (&mem_root) Field_int64_t(ptr,
2125
 
                                    field_length,
2126
 
                                    null_pos,
2127
 
                                    null_bit,
2128
 
                                    unireg_check,
2129
 
                                    field_name,
2130
 
                                    false,
2131
 
                                    false /* is_unsigned */);
 
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);
2132
2013
  case DRIZZLE_TYPE_TIMESTAMP:
2133
 
    return new (&mem_root) Field_timestamp(ptr,
2134
 
                                      field_length,
2135
 
                                      null_pos,
2136
 
                                      null_bit,
2137
 
                                      unireg_check,
2138
 
                                      field_name,
2139
 
                                      this,
2140
 
                                      field_charset);
 
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);
2141
2027
  case DRIZZLE_TYPE_DATE:
2142
2028
    return new (&mem_root) Field_date(ptr,
2143
2029
                                 null_pos,
2155
2041
                                 field_length,
2156
2042
                                 field_name,
2157
2043
                                 field_charset);
2158
 
  default: // Impossible (Wrong version)
2159
 
    break;
2160
2044
  }
2161
 
  return 0;
 
2045
  assert(0);
 
2046
  abort();
 
2047
}
 
2048
 
 
2049
void TableShare::refreshVersion()
 
2050
{
 
2051
  version= refresh_version;
2162
2052
}
2163
2053
 
2164
2054