~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/definition/table.cc

Merge Stewart - fix bug 668143: drizzleslap with --commit runs second iteration data load in a transaction

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
 
 
86
 
#include "drizzled/definition/cache.h"
87
 
 
88
 
#include <drizzled/refresh_version.h>
89
79
 
90
80
using namespace std;
91
81
 
94
84
 
95
85
extern size_t table_def_size;
96
86
 
97
 
 
98
 
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
99
 
{
100
 
  switch(field.type())
 
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
    plugin::EventObserver::deregisterTableEvents(*share);
 
118
   
 
119
    definition::Cache::singleton().erase(identifier);
 
120
    return;
 
121
  }
 
122
  share->unlock();
 
123
}
 
124
 
 
125
void TableShare::release(TableSharePtr &share)
 
126
{
 
127
  bool to_be_deleted= false;
 
128
  safe_mutex_assert_owner(LOCK_open.native_handle);
 
129
 
 
130
  share->lock();
 
131
  if (!--share->ref_count)
 
132
  {
 
133
    to_be_deleted= true;
 
134
  }
 
135
 
 
136
  if (to_be_deleted)
 
137
  {
 
138
    TableIdentifier identifier(share->getSchemaName(), share->getTableName());
 
139
    plugin::EventObserver::deregisterTableEvents(*share);
 
140
   
 
141
    definition::Cache::singleton().erase(identifier);
 
142
    return;
 
143
  }
 
144
  share->unlock();
 
145
}
 
146
 
 
147
void TableShare::release(TableIdentifier &identifier)
 
148
{
 
149
  TableSharePtr share= definition::Cache::singleton().find(identifier);
 
150
  if (share)
 
151
  {
 
152
    share->version= 0;                          // Mark for delete
 
153
    if (share->ref_count == 0)
 
154
    {
 
155
      share->lock();
 
156
      plugin::EventObserver::deregisterTableEvents(*share);
 
157
      definition::Cache::singleton().erase(identifier);
 
158
    }
 
159
  }
 
160
}
 
161
 
 
162
 
 
163
static TableSharePtr foundTableShare(TableSharePtr share)
 
164
{
 
165
  /*
 
166
    We found an existing table definition. Return it if we didn't get
 
167
    an error when reading the table definition from file.
 
168
  */
 
169
 
 
170
  /* We must do a lock to ensure that the structure is initialized */
 
171
  if (share->error)
 
172
  {
 
173
    /* Table definition contained an error */
 
174
    share->open_table_error(share->error, share->open_errno, share->errarg);
 
175
 
 
176
    return TableSharePtr();
 
177
  }
 
178
 
 
179
  share->incrementTableCount();
 
180
 
 
181
  return share;
 
182
}
 
183
 
 
184
/*
 
185
  Get TableShare for a table.
 
186
 
 
187
  get_table_share()
 
188
  session                       Thread handle
 
189
  table_list            Table that should be opened
 
190
  key                   Table cache key
 
191
  key_length            Length of key
 
192
  error                 out: Error code from open_table_def()
 
193
 
 
194
  IMPLEMENTATION
 
195
  Get a table definition from the table definition cache.
 
196
  If it doesn't exist, create a new from the table definition file.
 
197
 
 
198
  NOTES
 
199
  We must have wrlock on LOCK_open when we come here
 
200
  (To be changed later)
 
201
 
 
202
  RETURN
 
203
  0  Error
 
204
#  Share for table
 
205
*/
 
206
 
 
207
TableSharePtr TableShare::getShareCreate(Session *session, 
 
208
                                         TableIdentifier &identifier,
 
209
                                         int *in_error)
 
210
{
 
211
  TableSharePtr share;
 
212
 
 
213
  *in_error= 0;
 
214
 
 
215
  /* Read table definition from cache */
 
216
  if ((share= definition::Cache::singleton().find(identifier)))
 
217
    return foundTableShare(share);
 
218
 
 
219
  share.reset(new TableShare(message::Table::STANDARD, identifier));
 
220
  
 
221
  /*
 
222
    Lock mutex to be able to read table definition from file without
 
223
    conflicts
 
224
  */
 
225
  share->lock();
 
226
 
 
227
  bool ret= definition::Cache::singleton().insert(identifier, share);
 
228
 
 
229
  if (not ret)
 
230
    return TableSharePtr();
 
231
 
 
232
  if (share->open_table_def(*session, identifier))
 
233
  {
 
234
    *in_error= share->error;
 
235
    definition::Cache::singleton().erase(identifier);
 
236
 
 
237
    return TableSharePtr();
 
238
  }
 
239
  share->ref_count++;                           // Mark in use
 
240
  
 
241
  plugin::EventObserver::registerTableEvents(*share);
 
242
  
 
243
  share->unlock();
 
244
 
 
245
  return share;
 
246
}
 
247
 
 
248
 
 
249
/*
 
250
  Check if table definition exits in cache
 
251
 
 
252
  SYNOPSIS
 
253
  get_cached_table_share()
 
254
  db                    Database name
 
255
  table_name            Table name
 
256
 
 
257
  RETURN
 
258
  0  Not cached
 
259
#  TableShare for table
 
260
*/
 
261
TableSharePtr TableShare::getShare(TableIdentifier &identifier)
 
262
{
 
263
  safe_mutex_assert_owner(LOCK_open.native_handle);
 
264
 
 
265
  return definition::Cache::singleton().find(identifier);
 
266
}
 
267
 
 
268
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
 
269
{
 
270
  enum_field_types field_type;
 
271
 
 
272
  switch(proto_field_type)
101
273
  {
102
274
  case message::Table::Field::INTEGER:
103
 
    return DRIZZLE_TYPE_LONG;
104
 
 
 
275
    field_type= DRIZZLE_TYPE_LONG;
 
276
    break;
105
277
  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
 
 
 
278
    field_type= DRIZZLE_TYPE_DOUBLE;
 
279
    break;
 
280
  case message::Table::Field::TIMESTAMP:
 
281
    field_type= DRIZZLE_TYPE_TIMESTAMP;
 
282
    break;
114
283
  case message::Table::Field::BIGINT:
115
 
    return DRIZZLE_TYPE_LONGLONG;
116
 
 
 
284
    field_type= DRIZZLE_TYPE_LONGLONG;
 
285
    break;
117
286
  case message::Table::Field::DATETIME:
118
 
    return DRIZZLE_TYPE_DATETIME;
119
 
 
 
287
    field_type= DRIZZLE_TYPE_DATETIME;
 
288
    break;
120
289
  case message::Table::Field::DATE:
121
 
    return DRIZZLE_TYPE_DATE;
122
 
 
 
290
    field_type= DRIZZLE_TYPE_DATE;
 
291
    break;
123
292
  case message::Table::Field::VARCHAR:
124
 
    return DRIZZLE_TYPE_VARCHAR;
125
 
 
 
293
    field_type= DRIZZLE_TYPE_VARCHAR;
 
294
    break;
126
295
  case message::Table::Field::DECIMAL:
127
 
    return DRIZZLE_TYPE_DECIMAL;
128
 
 
 
296
    field_type= DRIZZLE_TYPE_DECIMAL;
 
297
    break;
129
298
  case message::Table::Field::ENUM:
130
 
    return DRIZZLE_TYPE_ENUM;
131
 
 
 
299
    field_type= DRIZZLE_TYPE_ENUM;
 
300
    break;
132
301
  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;
 
302
    field_type= DRIZZLE_TYPE_BLOB;
 
303
    break;
 
304
  default:
 
305
    field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
 
306
    assert(1);
143
307
  }
144
308
 
145
 
  abort();
 
309
  return field_type;
146
310
}
147
311
 
148
312
static Item *default_value_item(enum_field_types field_type,
173
337
                                 default_value->length());
174
338
    break;
175
339
  case DRIZZLE_TYPE_NULL:
176
 
    assert(0);
177
 
    abort();
 
340
    assert(false);
178
341
  case DRIZZLE_TYPE_TIMESTAMP:
179
342
  case DRIZZLE_TYPE_DATETIME:
180
 
  case DRIZZLE_TYPE_TIME:
181
343
  case DRIZZLE_TYPE_DATE:
182
344
  case DRIZZLE_TYPE_ENUM:
183
 
  case DRIZZLE_TYPE_UUID:
184
 
  case DRIZZLE_TYPE_MICROTIME:
185
 
  case DRIZZLE_TYPE_BOOLEAN:
186
345
    default_item= new Item_string(default_value->c_str(),
187
346
                                  default_value->length(),
188
347
                                  system_charset_info);
221
380
 */
222
381
bool TableShare::fieldInPrimaryKey(Field *in_field) const
223
382
{
224
 
  assert(getTableMessage());
 
383
  assert(table_proto != NULL);
225
384
 
226
 
  size_t num_indexes= getTableMessage()->indexes_size();
 
385
  size_t num_indexes= table_proto->indexes_size();
227
386
 
228
387
  for (size_t x= 0; x < num_indexes; ++x)
229
388
  {
230
 
    const message::Table::Index &index= getTableMessage()->indexes(x);
 
389
    const message::Table::Index &index= table_proto->indexes(x);
231
390
    if (index.is_primary())
232
391
    {
233
392
      size_t num_parts= index.index_part_size();
234
393
      for (size_t y= 0; y < num_parts; ++y)
235
394
      {
236
 
        if (index.index_part(y).fieldnr() == in_field->position())
 
395
        if (index.index_part(y).fieldnr() == in_field->field_index)
237
396
          return true;
238
397
      }
239
398
    }
241
400
  return false;
242
401
}
243
402
 
244
 
TableShare::TableShare(const identifier::Table::Type type_arg) :
 
403
TableShare::TableShare(TableIdentifier::Type type_arg) :
245
404
  table_category(TABLE_UNKNOWN_CATEGORY),
246
405
  found_next_number_field(NULL),
247
406
  timestamp_field(NULL),
248
407
  key_info(NULL),
249
408
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
250
409
  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
410
  block_size(0),
256
411
  version(0),
257
412
  timestamp_offset(0),
258
413
  reclength(0),
259
414
  stored_rec_length(0),
260
415
  max_rows(0),
261
 
  _table_message(NULL),
 
416
  table_proto(NULL),
262
417
  storage_engine(NULL),
263
418
  tmp_table(type_arg),
264
 
  _ref_count(0),
 
419
  ref_count(0),
265
420
  null_bytes(0),
266
421
  last_null_bit_pos(0),
267
 
  _field_size(0),
 
422
  fields(0),
268
423
  rec_buff_length(0),
269
424
  keys(0),
270
425
  key_parts(0),
274
429
  uniques(0),
275
430
  null_fields(0),
276
431
  blob_fields(0),
 
432
  timestamp_field_offset(0),
277
433
  has_variable_width(false),
278
434
  db_create_options(0),
279
435
  db_options_in_use(0),
286
442
  error(0),
287
443
  open_errno(0),
288
444
  errarg(0),
289
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
445
  blob_ptr_size(0),
290
446
  db_low_byte_first(false),
 
447
  name_lock(false),
 
448
  replace_with_name_lock(false),
 
449
  waiting_on_cond(false),
291
450
  keys_in_use(0),
292
 
  keys_for_keyread(0)
 
451
  keys_for_keyread(0),
 
452
  event_observers(NULL)
293
453
{
 
454
 
 
455
  table_charset= 0;
 
456
  memset(&db, 0, sizeof(LEX_STRING));
 
457
  memset(&table_name, 0, sizeof(LEX_STRING));
 
458
  memset(&path, 0, sizeof(LEX_STRING));
 
459
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
460
 
294
461
  if (type_arg == message::Table::INTERNAL)
295
462
  {
296
 
    identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
 
463
    TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
297
464
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
298
465
  }
299
466
  else
302
469
  }
303
470
}
304
471
 
305
 
TableShare::TableShare(const identifier::Table &identifier, const identifier::Table::Key &key) :// Used by placeholder
 
472
TableShare::TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
306
473
  table_category(TABLE_UNKNOWN_CATEGORY),
307
474
  found_next_number_field(NULL),
308
475
  timestamp_field(NULL),
309
476
  key_info(NULL),
310
477
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
311
 
  table_charset(0),
312
478
  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
479
  block_size(0),
318
480
  version(0),
319
481
  timestamp_offset(0),
320
482
  reclength(0),
321
483
  stored_rec_length(0),
322
484
  max_rows(0),
323
 
  _table_message(NULL),
 
485
  table_proto(NULL),
324
486
  storage_engine(NULL),
325
487
  tmp_table(message::Table::INTERNAL),
326
 
  _ref_count(0),
 
488
  ref_count(0),
327
489
  null_bytes(0),
328
490
  last_null_bit_pos(0),
329
 
  _field_size(0),
 
491
  fields(0),
330
492
  rec_buff_length(0),
331
493
  keys(0),
332
494
  key_parts(0),
336
498
  uniques(0),
337
499
  null_fields(0),
338
500
  blob_fields(0),
 
501
  timestamp_field_offset(0),
339
502
  has_variable_width(false),
340
503
  db_create_options(0),
341
504
  db_options_in_use(0),
348
511
  error(0),
349
512
  open_errno(0),
350
513
  errarg(0),
351
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
514
  blob_ptr_size(0),
352
515
  db_low_byte_first(false),
 
516
  name_lock(false),
 
517
  replace_with_name_lock(false),
 
518
  waiting_on_cond(false),
353
519
  keys_in_use(0),
354
 
  keys_for_keyread(0)
 
520
  keys_for_keyread(0),
 
521
  event_observers(NULL)
355
522
{
356
523
  assert(identifier.getKey() == key);
357
524
 
 
525
  table_charset= 0;
 
526
  memset(&path, 0, sizeof(LEX_STRING));
 
527
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
528
 
358
529
  private_key_for_cache= key;
359
530
 
360
531
  table_category=         TABLE_CATEGORY_TEMPORARY;
368
539
  path.str= (char *)"";
369
540
  normalized_path.str= path.str;
370
541
  path.length= normalized_path.length= 0;
371
 
 
372
 
  std::string tb_name(identifier.getTableName());
373
 
  std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
374
 
  assert(strcmp(tb_name.c_str(), table_name.str) == 0);
375
 
 
 
542
  assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 0);
376
543
  assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
377
544
}
378
545
 
379
546
 
380
 
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
 
547
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
381
548
  table_category(TABLE_UNKNOWN_CATEGORY),
382
549
  found_next_number_field(NULL),
383
550
  timestamp_field(NULL),
384
551
  key_info(NULL),
385
552
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
386
 
  table_charset(0),
387
553
  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
554
  block_size(0),
393
555
  version(0),
394
556
  timestamp_offset(0),
395
557
  reclength(0),
396
558
  stored_rec_length(0),
397
559
  max_rows(0),
398
 
  _table_message(NULL),
 
560
  table_proto(NULL),
399
561
  storage_engine(NULL),
400
562
  tmp_table(identifier.getType()),
401
 
  _ref_count(0),
 
563
  ref_count(0),
402
564
  null_bytes(0),
403
565
  last_null_bit_pos(0),
404
 
  _field_size(0),
 
566
  fields(0),
405
567
  rec_buff_length(0),
406
568
  keys(0),
407
569
  key_parts(0),
411
573
  uniques(0),
412
574
  null_fields(0),
413
575
  blob_fields(0),
 
576
  timestamp_field_offset(0),
414
577
  has_variable_width(false),
415
578
  db_create_options(0),
416
579
  db_options_in_use(0),
423
586
  error(0),
424
587
  open_errno(0),
425
588
  errarg(0),
426
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
589
  blob_ptr_size(0),
427
590
  db_low_byte_first(false),
 
591
  name_lock(false),
 
592
  replace_with_name_lock(false),
 
593
  waiting_on_cond(false),
428
594
  keys_in_use(0),
429
 
  keys_for_keyread(0)
 
595
  keys_for_keyread(0),
 
596
  event_observers(NULL)
430
597
{
 
598
  table_charset= 0;
 
599
  memset(&db, 0, sizeof(LEX_STRING));
 
600
  memset(&table_name, 0, sizeof(LEX_STRING));
 
601
  memset(&path, 0, sizeof(LEX_STRING));
 
602
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
603
 
431
604
  private_key_for_cache= identifier.getKey();
432
605
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
433
606
  private_normalized_path.resize(identifier.getPath().size() + 1);
450
623
/*
451
624
  Used for shares that will go into the cache.
452
625
*/
453
 
TableShare::TableShare(const identifier::Table::Type type_arg,
454
 
                       const identifier::Table &identifier,
 
626
TableShare::TableShare(TableIdentifier::Type type_arg,
 
627
                       TableIdentifier &identifier,
455
628
                       char *path_arg,
456
629
                       uint32_t path_length_arg) :
457
630
  table_category(TABLE_UNKNOWN_CATEGORY),
459
632
  timestamp_field(NULL),
460
633
  key_info(NULL),
461
634
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
462
 
  table_charset(0),
463
635
  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
636
  block_size(0),
469
637
  version(0),
470
638
  timestamp_offset(0),
471
639
  reclength(0),
472
640
  stored_rec_length(0),
473
641
  max_rows(0),
474
 
  _table_message(NULL),
 
642
  table_proto(NULL),
475
643
  storage_engine(NULL),
476
644
  tmp_table(type_arg),
477
 
  _ref_count(0),
 
645
  ref_count(0),
478
646
  null_bytes(0),
479
647
  last_null_bit_pos(0),
480
 
  _field_size(0),
 
648
  fields(0),
481
649
  rec_buff_length(0),
482
650
  keys(0),
483
651
  key_parts(0),
487
655
  uniques(0),
488
656
  null_fields(0),
489
657
  blob_fields(0),
 
658
  timestamp_field_offset(0),
490
659
  has_variable_width(false),
491
660
  db_create_options(0),
492
661
  db_options_in_use(0),
499
668
  error(0),
500
669
  open_errno(0),
501
670
  errarg(0),
502
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
671
  blob_ptr_size(0),
503
672
  db_low_byte_first(false),
 
673
  name_lock(false),
 
674
  replace_with_name_lock(false),
 
675
  waiting_on_cond(false),
504
676
  keys_in_use(0),
505
 
  keys_for_keyread(0)
 
677
  keys_for_keyread(0),
 
678
  event_observers(NULL)
506
679
{
 
680
  table_charset= 0;
 
681
  memset(&db, 0, sizeof(LEX_STRING));
 
682
  memset(&table_name, 0, sizeof(LEX_STRING));
 
683
  memset(&path, 0, sizeof(LEX_STRING));
 
684
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
685
 
507
686
  char *path_buff;
508
687
  std::string _path;
509
688
 
523
702
  }
524
703
  else
525
704
  {
526
 
    identifier::Table::build_table_filename(_path, db.str, table_name.str, false);
 
705
    TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
527
706
  }
528
707
 
529
708
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
537
716
  else
538
717
  {
539
718
    assert(0); // We should throw here.
540
 
    abort();
541
719
  }
542
720
}
543
721
 
558
736
 
559
737
TableShare::~TableShare() 
560
738
{
 
739
  assert(ref_count == 0);
 
740
 
 
741
  /*
 
742
    If someone is waiting for this to be deleted, inform it about this.
 
743
    Don't do a delete until we know that no one is refering to this anymore.
 
744
  */
 
745
  if (tmp_table == message::Table::STANDARD)
 
746
  {
 
747
    /* share->mutex is locked in release_table_share() */
 
748
    while (waiting_on_cond)
 
749
    {
 
750
      cond.notify_all();
 
751
      boost::mutex::scoped_lock scoped(mutex, boost::adopt_lock_t());
 
752
      cond.wait(scoped);
 
753
      scoped.release();
 
754
    }
 
755
    /* No thread refers to this anymore */
 
756
    mutex.unlock();
 
757
  }
 
758
 
561
759
  storage_engine= NULL;
562
760
 
 
761
  delete table_proto;
 
762
  table_proto= NULL;
 
763
 
563
764
  mem_root.free_root(MYF(0));                 // Free's share
564
765
}
565
766
 
566
 
void TableShare::setIdentifier(const identifier::Table &identifier_arg)
 
767
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
567
768
{
568
769
  private_key_for_cache= identifier_arg.getKey();
569
770
 
576
777
  table_name.str=    db.str + db.length + 1;
577
778
  table_name.length= strlen(table_name.str);
578
779
 
579
 
  getTableMessage()->set_name(identifier_arg.getTableName());
580
 
  getTableMessage()->set_schema(identifier_arg.getSchemaName());
 
780
  table_proto->set_name(identifier_arg.getTableName());
 
781
  table_proto->set_schema(identifier_arg.getSchemaName());
581
782
}
582
783
 
583
 
bool TableShare::parse_table_proto(Session& session, message::Table &table)
 
784
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
584
785
{
585
 
  drizzled::error_t local_error= EE_OK;
 
786
  int local_error= 0;
586
787
 
587
788
  if (! table.IsInitialized())
588
789
  {
589
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
590
 
             table.name().empty() ? " " :  table.name().c_str(),
591
 
             table.InitializationErrorString().c_str());
592
 
 
 
790
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
593
791
    return ER_CORRUPT_TABLE_DEFINITION;
594
792
  }
595
793
 
596
 
  setTableMessage(table);
 
794
  setTableProto(new(nothrow) message::Table(table));
597
795
 
598
796
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
599
797
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
619
817
 
620
818
  table_charset= get_charset(table_options.collation_id());
621
819
 
622
 
  if (not table_charset)
 
820
  if (! table_charset)
623
821
  {
624
 
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
625
 
             table_options.collation().c_str(),
626
 
             table.name().c_str());
 
822
    char errmsg[100];
 
823
    snprintf(errmsg, sizeof(errmsg),
 
824
             _("Table %s has invalid/unknown collation: %d,%s"),
 
825
             getPath(),
 
826
             table_options.collation_id(),
 
827
             table_options.collation().c_str());
 
828
    errmsg[99]='\0';
627
829
 
628
 
    return ER_CORRUPT_TABLE_DEFINITION; // Historical
 
830
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
 
831
    return ER_CORRUPT_TABLE_DEFINITION;
629
832
  }
630
833
 
631
834
  db_record_offset= 1;
632
835
 
 
836
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
 
837
 
633
838
  keys= table.indexes_size();
634
839
 
635
840
  key_parts= 0;
779
984
  keys_for_keyread.reset();
780
985
  set_prefix(keys_in_use, keys);
781
986
 
782
 
  _field_size= table.field_size();
 
987
  fields= table.field_size();
783
988
 
784
 
  setFields(_field_size + 1);
785
 
  _fields[_field_size]= NULL;
 
989
  setFields(fields + 1);
 
990
  field[fields]= NULL;
786
991
 
787
992
  uint32_t local_null_fields= 0;
788
993
  reclength= 0;
789
994
 
790
 
  std::vector<uint32_t> field_offsets;
791
 
  std::vector<uint32_t> field_pack_length;
 
995
  vector<uint32_t> field_offsets;
 
996
  vector<uint32_t> field_pack_length;
792
997
 
793
 
  field_offsets.resize(_field_size);
794
 
  field_pack_length.resize(_field_size);
 
998
  field_offsets.resize(fields);
 
999
  field_pack_length.resize(fields);
795
1000
 
796
1001
  uint32_t interval_count= 0;
797
1002
  uint32_t interval_parts= 0;
798
1003
 
799
1004
  uint32_t stored_columns_reclength= 0;
800
1005
 
801
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1006
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
802
1007
  {
803
1008
    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
 
    }
 
1009
    if (pfield.constraints().is_nullable())
 
1010
      local_null_fields++;
812
1011
 
813
 
    enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
 
1012
    enum_field_types drizzle_field_type=
 
1013
      proto_field_type_to_drizzle_type(pfield.type());
814
1014
 
815
1015
    field_offsets[fieldnr]= stored_columns_reclength;
816
1016
 
849
1049
      {
850
1050
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
851
1051
 
852
 
        field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
 
1052
        field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
853
1053
      }
854
1054
      break;
855
1055
    default:
899
1099
 
900
1100
  uint32_t interval_nr= 0;
901
1101
 
902
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1102
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
903
1103
  {
904
1104
    message::Table::Field pfield= table.field(fieldnr);
905
1105
 
911
1111
 
912
1112
    if (field_options.field_value_size() > Field_enum::max_supported_elements)
913
1113
    {
914
 
      my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
 
1114
      char errmsg[100];
 
1115
      snprintf(errmsg, sizeof(errmsg),
 
1116
               _("ENUM column %s has greater than %d possible values"),
 
1117
               pfield.name().c_str(),
 
1118
               Field_enum::max_supported_elements);
 
1119
      errmsg[99]='\0';
915
1120
 
916
 
      return ER_CORRUPT_TABLE_DEFINITION_ENUM; // Historical
 
1121
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
 
1122
      return ER_CORRUPT_TABLE_DEFINITION;
917
1123
    }
918
1124
 
919
1125
 
956
1162
  /* and read the fields */
957
1163
  interval_nr= 0;
958
1164
 
959
 
  bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
 
1165
  bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
960
1166
 
961
1167
  unsigned char* null_pos= getDefaultValues();
962
1168
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
963
1169
 
964
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1170
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
965
1171
  {
966
1172
    message::Table::Field pfield= table.field(fieldnr);
967
1173
 
987
1193
        unireg_type= Field::TIMESTAMP_DN_FIELD;
988
1194
      }
989
1195
      else
990
 
      {
991
 
        assert(0); // Invalid update value.
992
 
        abort();
993
 
      }
 
1196
        assert(1); // Invalid update value.
994
1197
    }
995
1198
    else if (pfield.has_options() &&
996
1199
             pfield.options().has_update_expression() &&
1016
1219
 
1017
1220
    enum_field_types field_type;
1018
1221
 
1019
 
    field_type= proto_field_type_to_drizzle_type(pfield);
 
1222
    field_type= proto_field_type_to_drizzle_type(pfield.type());
1020
1223
 
1021
1224
    const CHARSET_INFO *charset= &my_charset_bin;
1022
1225
 
1061
1264
      {
1062
1265
        if (fo.scale() > DECIMAL_MAX_SCALE)
1063
1266
        {
1064
 
          local_error= ER_NOT_FORM_FILE;
 
1267
          local_error= 4;
1065
1268
 
1066
 
          return true;
 
1269
          return local_error;
1067
1270
        }
1068
1271
        decimals= static_cast<uint8_t>(fo.scale());
1069
1272
      }
1083
1286
    }
1084
1287
 
1085
1288
 
 
1289
    db_low_byte_first= true; //Cursor->low_byte_first();
 
1290
    blob_ptr_size= portable_sizeof_char_ptr;
 
1291
 
1086
1292
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1087
1293
 
1088
 
    // We set field_length in this loop.
1089
1294
    switch (field_type)
1090
1295
    {
1091
1296
    case DRIZZLE_TYPE_BLOB:
1117
1322
            decimals != NOT_FIXED_DEC)
1118
1323
        {
1119
1324
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1120
 
          local_error= ER_M_BIGGER_THAN_D;
1121
 
          return true;
 
1325
          local_error= 1;
 
1326
 
 
1327
          return local_error;
1122
1328
        }
1123
1329
        break;
1124
1330
      }
1126
1332
      {
1127
1333
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1128
1334
 
1129
 
        field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
 
1335
        field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
1130
1336
                                                     false);
1131
1337
        break;
1132
1338
      }
 
1339
    case DRIZZLE_TYPE_TIMESTAMP:
1133
1340
    case DRIZZLE_TYPE_DATETIME:
1134
1341
      field_length= DateTime::MAX_STRING_LENGTH;
1135
1342
      break;
1162
1369
      }
1163
1370
      break;
1164
1371
    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();
 
1372
      field_length= MAX_BIGINT_WIDTH;
1184
1373
      break;
1185
1374
    case DRIZZLE_TYPE_NULL:
1186
1375
      abort(); // Programming error
1187
1376
    }
1188
1377
 
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
 
    }
 
1378
    Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
 
1379
                                field_length,
 
1380
                                pfield.constraints().is_nullable(),
 
1381
                                null_pos,
 
1382
                                null_bit_pos,
 
1383
                                decimals,
 
1384
                                field_type,
 
1385
                                charset,
 
1386
                                (Field::utype) MTYP_TYPENR(unireg_type),
 
1387
                                ((field_type == DRIZZLE_TYPE_ENUM) ?
 
1388
                                 &intervals[interval_nr++]
 
1389
                                 : (TYPELIB*) 0),
 
1390
                                getTableProto()->field(fieldnr).name().c_str());
 
1391
 
 
1392
    field[fieldnr]= f;
1235
1393
 
1236
1394
    // This needs to go, we should be setting the "use" on the field so that
1237
1395
    // it does not reference the share/table.
1257
1415
      if (res != 0 && res != 3) /* @TODO Huh? */
1258
1416
      {
1259
1417
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1260
 
        local_error= ER_INVALID_DEFAULT;
 
1418
        local_error= 1;
1261
1419
 
1262
 
        return true;
 
1420
        return local_error;
1263
1421
      }
1264
1422
    }
1265
 
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
 
1423
    else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
 
1424
             (f->flags & NOT_NULL_FLAG))
1266
1425
    {
1267
1426
      f->set_notnull();
1268
1427
      f->store((int64_t) 1, true);
1276
1435
    f->setTable(NULL);
1277
1436
    f->orig_table= NULL;
1278
1437
 
1279
 
    f->setPosition(fieldnr);
 
1438
    f->field_index= fieldnr;
1280
1439
    f->comment= comment;
1281
 
    if (not default_value &&
1282
 
        not (f->unireg_check==Field::NEXT_NUMBER) &&
 
1440
    if (! default_value &&
 
1441
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
1283
1442
        (f->flags & NOT_NULL_FLAG) &&
1284
 
        (not f->is_timestamp()))
 
1443
        (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
1285
1444
    {
1286
1445
      f->flags|= NO_DEFAULT_VALUE_FLAG;
1287
1446
    }
1288
1447
 
1289
1448
    if (f->unireg_check == Field::NEXT_NUMBER)
1290
 
      found_next_number_field= &(_fields[fieldnr]);
 
1449
      found_next_number_field= &(field[fieldnr]);
 
1450
 
 
1451
    if (timestamp_field == f)
 
1452
      timestamp_field_offset= fieldnr;
1291
1453
 
1292
1454
    if (use_hash) /* supposedly this never fails... but comments lie */
1293
1455
    {
1294
 
      const char *local_field_name= _fields[fieldnr]->field_name;
1295
 
      name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
 
1456
      const char *local_field_name= field[fieldnr]->field_name;
 
1457
      name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
1296
1458
    }
 
1459
 
1297
1460
  }
1298
1461
 
1299
1462
  keyinfo= key_info;
1318
1481
    We need to set the unused bits to 1. If the number of bits is a multiple
1319
1482
    of 8 there are no unused bits.
1320
1483
  */
 
1484
 
1321
1485
  if (null_count & 7)
1322
1486
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1323
1487
 
1348
1512
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1349
1513
        {
1350
1514
          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)
 
1515
          if (! fieldnr ||
 
1516
              field[fieldnr-1]->null_ptr ||
 
1517
              field[fieldnr-1]->key_length() != key_part[i].length)
1354
1518
          {
1355
1519
            local_primary_key= MAX_KEY; // Can't be used
1356
1520
            break;
1365
1529
        {
1366
1530
          return ENOMEM;
1367
1531
        }
1368
 
        local_field= key_part->field= _fields[key_part->fieldnr-1];
 
1532
        local_field= key_part->field= field[key_part->fieldnr-1];
1369
1533
        key_part->type= local_field->key_type();
1370
1534
        if (local_field->null_ptr)
1371
1535
        {
1471
1635
                            &next_number_keypart)) < 0)
1472
1636
    {
1473
1637
      /* Wrong field definition */
1474
 
      local_error= ER_NOT_FORM_FILE;
 
1638
      local_error= 4;
1475
1639
 
1476
 
      return true;
 
1640
      return local_error;
1477
1641
    }
1478
1642
    else
1479
1643
    {
1487
1651
    blob_field.resize(blob_fields);
1488
1652
    uint32_t *save= &blob_field[0];
1489
1653
    uint32_t k= 0;
1490
 
    for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
 
1654
    for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
1491
1655
    {
1492
1656
      if ((*iter)->flags & BLOB_FLAG)
1493
1657
        (*save++)= k;
1494
1658
    }
1495
1659
  }
1496
1660
 
 
1661
  db_low_byte_first= true; // @todo Question this.
1497
1662
  all_set.clear();
1498
 
  all_set.resize(_field_size);
 
1663
  all_set.resize(fields);
1499
1664
  all_set.set();
1500
1665
 
1501
 
  return local_error != EE_OK;
1502
 
}
 
1666
  return local_error;
 
1667
}
 
1668
 
 
1669
int TableShare::parse_table_proto(Session& session, message::Table &table)
 
1670
{
 
1671
  int local_error= inner_parse_table_proto(session, table);
 
1672
 
 
1673
  if (not local_error)
 
1674
    return 0;
 
1675
 
 
1676
  error= local_error;
 
1677
  open_errno= errno;
 
1678
  errarg= 0;
 
1679
  open_table_error(local_error, open_errno, 0);
 
1680
 
 
1681
  return local_error;
 
1682
}
 
1683
 
1503
1684
 
1504
1685
/*
1505
1686
  Read table definition from a binary / text based .frm cursor
1525
1706
  6    Unknown .frm version
1526
1707
*/
1527
1708
 
1528
 
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
 
1709
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1529
1710
{
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())
 
1711
  int local_error;
 
1712
  bool error_given;
 
1713
 
 
1714
  local_error= 1;
 
1715
  error_given= 0;
 
1716
 
 
1717
  message::Table table;
 
1718
 
 
1719
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
 
1720
 
 
1721
  if (local_error != EEXIST)
1535
1722
  {
1536
 
    if (parse_table_proto(session, *table))
 
1723
    if (local_error > 0)
1537
1724
    {
1538
 
      local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1539
 
      my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
 
1725
      errno= local_error;
 
1726
      local_error= 1;
1540
1727
    }
1541
1728
    else
1542
1729
    {
1543
 
      setTableCategory(TABLE_CATEGORY_USER);
1544
 
      local_error= EE_OK;
 
1730
      if (not table.IsInitialized())
 
1731
      {
 
1732
        local_error= 4;
 
1733
      }
1545
1734
    }
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);
 
1735
    goto err_not_open;
 
1736
  }
 
1737
 
 
1738
  local_error= parse_table_proto(session, table);
 
1739
 
 
1740
  setTableCategory(TABLE_CATEGORY_USER);
 
1741
 
 
1742
err_not_open:
 
1743
  if (local_error && !error_given)
 
1744
  {
 
1745
    error= local_error;
 
1746
    open_table_error(error, (open_errno= errno), 0);
 
1747
  }
 
1748
 
 
1749
  return(error);
1559
1750
}
1560
1751
 
1561
1752
 
1583
1774
  7    Table definition has changed in engine
1584
1775
*/
1585
1776
int TableShare::open_table_from_share(Session *session,
1586
 
                                      const identifier::Table &identifier,
 
1777
                                      const TableIdentifier &identifier,
1587
1778
                                      const char *alias,
1588
1779
                                      uint32_t db_stat, uint32_t ha_open_flags,
1589
1780
                                      Table &outparam)
1600
1791
  if (not error_reported)
1601
1792
    open_table_error(ret, errno, 0);
1602
1793
 
1603
 
  boost::checked_delete(outparam.cursor);
 
1794
  delete outparam.cursor;
1604
1795
  outparam.cursor= 0;                           // For easier error checking
1605
1796
  outparam.db_stat= 0;
1606
1797
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
1619
1810
  unsigned char *record= NULL;
1620
1811
  Field **field_ptr;
1621
1812
 
 
1813
  /* Parsing of partitioning information from .frm needs session->lex set up. */
 
1814
  assert(session->lex->is_lex_started);
 
1815
 
1622
1816
  local_error= 1;
1623
1817
  outparam.resetTable(session, this, db_stat);
1624
1818
 
1670
1864
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1671
1865
  }
1672
1866
 
1673
 
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((_field_size+1)* sizeof(Field*)))))
 
1867
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
1674
1868
  {
1675
1869
    return local_error;
1676
1870
  }
1682
1876
  outparam.null_flags= (unsigned char*) record+1;
1683
1877
 
1684
1878
  /* 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++)
 
1879
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
1686
1880
  {
1687
 
    if (!((*field_ptr)= _fields[i]->clone(outparam.getMemRoot(), &outparam)))
 
1881
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
1688
1882
      return local_error;
1689
1883
  }
1690
1884
  (*field_ptr)= 0;                              // End marker
1693
1887
    outparam.found_next_number_field=
1694
1888
      outparam.getField(positionFields(found_next_number_field));
1695
1889
  if (timestamp_field)
1696
 
    outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
 
1890
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
 
1891
 
1697
1892
 
1698
1893
  /* Fix key->name and key_part->field */
1699
1894
  if (key_parts)
1742
1937
 
1743
1938
  /* Allocate bitmaps */
1744
1939
 
1745
 
  outparam.def_read_set.resize(_field_size);
1746
 
  outparam.def_write_set.resize(_field_size);
1747
 
  outparam.tmp_set.resize(_field_size);
 
1940
  outparam.def_read_set.resize(fields);
 
1941
  outparam.def_write_set.resize(fields);
 
1942
  outparam.tmp_set.resize(fields);
1748
1943
  outparam.default_column_bitmaps();
1749
1944
 
1750
1945
  return 0;
1751
1946
}
1752
1947
 
1753
 
int TableShare::open_table_cursor_inner(const identifier::Table &identifier,
 
1948
int TableShare::open_table_cursor_inner(const TableIdentifier &identifier,
1754
1949
                                        uint32_t db_stat, uint32_t ha_open_flags,
1755
1950
                                        Table &outparam,
1756
1951
                                        bool &error_reported)
1801
1996
/* error message when opening a form cursor */
1802
1997
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1803
1998
{
 
1999
  int err_no;
1804
2000
  char buff[FN_REFLEN];
1805
2001
  myf errortype= ME_ERROR+ME_WAITTANG;
1806
2002
 
1809
2005
  case 1:
1810
2006
    if (db_errno == ENOENT)
1811
2007
    {
1812
 
      identifier::Table identifier(db.str, table_name.str);
1813
 
      my_error(ER_TABLE_UNKNOWN, identifier);
 
2008
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
1814
2009
    }
1815
2010
    else
1816
2011
    {
1821
2016
    break;
1822
2017
  case 2:
1823
2018
    {
1824
 
      drizzled::error_t err_no;
1825
 
 
1826
2019
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
1827
2020
        ER_FILE_USED : ER_CANT_OPEN_FILE;
1828
 
 
1829
2021
      my_error(err_no, errortype, normalized_path.str, db_errno);
1830
2022
      break;
1831
2023
    }
1861
2053
  return;
1862
2054
} /* open_table_error */
1863
2055
 
1864
 
Field *TableShare::make_field(const message::Table::Field &pfield,
1865
 
                              unsigned char *ptr,
 
2056
Field *TableShare::make_field(unsigned char *ptr,
1866
2057
                              uint32_t field_length,
1867
2058
                              bool is_nullable,
1868
2059
                              unsigned char *null_pos,
1874
2065
                              TYPELIB *interval,
1875
2066
                              const char *field_name)
1876
2067
{
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
2068
  if (! is_nullable)
1907
2069
  {
1908
2070
    null_pos=0;
1917
2079
  {
1918
2080
  case DRIZZLE_TYPE_DATE:
1919
2081
  case DRIZZLE_TYPE_DATETIME:
1920
 
  case DRIZZLE_TYPE_UUID:
 
2082
  case DRIZZLE_TYPE_TIMESTAMP:
1921
2083
    field_charset= &my_charset_bin;
1922
2084
  default: break;
1923
2085
  }
1926
2088
  {
1927
2089
  case DRIZZLE_TYPE_ENUM:
1928
2090
    return new (&mem_root) Field_enum(ptr,
1929
 
                                      field_length,
1930
 
                                      null_pos,
1931
 
                                      null_bit,
1932
 
                                      field_name,
1933
 
                                      interval,
1934
 
                                      field_charset);
 
2091
                                 field_length,
 
2092
                                 null_pos,
 
2093
                                 null_bit,
 
2094
                                 field_name,
 
2095
                                 interval,
 
2096
                                 field_charset);
1935
2097
  case DRIZZLE_TYPE_VARCHAR:
1936
2098
    setVariableWidth();
1937
2099
    return new (&mem_root) Field_varstring(ptr,field_length,
1941
2103
                                      field_charset);
1942
2104
  case DRIZZLE_TYPE_BLOB:
1943
2105
    return new (&mem_root) Field_blob(ptr,
1944
 
                                      null_pos,
1945
 
                                      null_bit,
1946
 
                                      field_name,
1947
 
                                      this,
1948
 
                                      field_charset);
 
2106
                                 null_pos,
 
2107
                                 null_bit,
 
2108
                                 field_name,
 
2109
                                 this,
 
2110
                                 calc_pack_length(DRIZZLE_TYPE_LONG, 0),
 
2111
                                 field_charset);
1949
2112
  case DRIZZLE_TYPE_DECIMAL:
1950
2113
    return new (&mem_root) Field_decimal(ptr,
1951
 
                                         field_length,
1952
 
                                         null_pos,
1953
 
                                         null_bit,
1954
 
                                         unireg_check,
1955
 
                                         field_name,
1956
 
                                         decimals);
 
2114
                                    field_length,
 
2115
                                    null_pos,
 
2116
                                    null_bit,
 
2117
                                    unireg_check,
 
2118
                                    field_name,
 
2119
                                    decimals,
 
2120
                                    false,
 
2121
                                    false /* is_unsigned */);
1957
2122
  case DRIZZLE_TYPE_DOUBLE:
1958
2123
    return new (&mem_root) Field_double(ptr,
1959
2124
                                   field_length,
1964
2129
                                   decimals,
1965
2130
                                   false,
1966
2131
                                   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
2132
  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);
 
2133
    return new (&mem_root) Field_long(ptr,
 
2134
                                 field_length,
 
2135
                                 null_pos,
 
2136
                                 null_bit,
 
2137
                                 unireg_check,
 
2138
                                 field_name,
 
2139
                                 false,
 
2140
                                 false /* is_unsigned */);
1987
2141
  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);
 
2142
    return new (&mem_root) Field_int64_t(ptr,
 
2143
                                    field_length,
 
2144
                                    null_pos,
 
2145
                                    null_bit,
 
2146
                                    unireg_check,
 
2147
                                    field_name,
 
2148
                                    false,
 
2149
                                    false /* is_unsigned */);
2013
2150
  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);
 
2151
    return new (&mem_root) Field_timestamp(ptr,
 
2152
                                      field_length,
 
2153
                                      null_pos,
 
2154
                                      null_bit,
 
2155
                                      unireg_check,
 
2156
                                      field_name,
 
2157
                                      this,
 
2158
                                      field_charset);
2027
2159
  case DRIZZLE_TYPE_DATE:
2028
2160
    return new (&mem_root) Field_date(ptr,
2029
2161
                                 null_pos,
2041
2173
                                 field_length,
2042
2174
                                 field_name,
2043
2175
                                 field_charset);
 
2176
  default: // Impossible (Wrong version)
 
2177
    break;
2044
2178
  }
2045
 
  assert(0);
2046
 
  abort();
2047
 
}
2048
 
 
2049
 
void TableShare::refreshVersion()
2050
 
{
2051
 
  version= refresh_version;
 
2179
  return 0;
2052
2180
}
2053
2181
 
2054
2182